Skip to content

Commit

Permalink
Merge pull request #78 from space928/d3dTextureFixes
Browse files Browse the repository at this point in the history
D3d texture fixes
  • Loading branch information
space928 authored Dec 14, 2023
2 parents 6a59335 + dbf97be commit caff9d9
Show file tree
Hide file tree
Showing 12 changed files with 410 additions and 271 deletions.
6 changes: 4 additions & 2 deletions OmsiExtensionsCLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void CreateTexture()

try
{
texture.CreateD3DTexture(texWidth, texHeight);
texture.CreateD3DTexture(texWidth, texHeight).Wait();
} catch (Exception ex)
{
Console.WriteLine(ex);
Expand Down Expand Up @@ -128,17 +128,19 @@ public void UpdateTexture()
}
iter++;

texture.UpdateTexture(texBuffer.AsMemory());
texture.UpdateTexture(texBuffer.AsMemory(), new OmsiRemoteMethods.Rectangle() { left=0, top=0, right=texWidth, bottom=texHeight}).Wait();
}

private void Omsi_OnOmsiGotD3DContext(object sender, EventArgs e)
{
// d3dGotContext.Set();
Console.WriteLine("Got D3D Context!");
}

private void Omsi_OnMapLoaded(object sender, bool e)
{
d3dGotContext.Set();
Console.WriteLine("Map Loaded!");
}
}
}
15 changes: 9 additions & 6 deletions OmsiHook/D3DTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public D3DTexture() : base() { }
/// <param name="address">the address of the existing texture</param>
/// <exception cref="NullReferenceException"></exception>
/// <exception cref="Exception"></exception>
public async void CreateFromExisting(uint address)
public async Task CreateFromExisting(uint address)
{
if (Address == 0)
throw new NullReferenceException("Texture was already null!");
Expand All @@ -77,10 +77,10 @@ public async void CreateFromExisting(uint address)
/// <param name="height">the height of the texture</param>
/// <param name="format">the <see cref="D3DFORMAT"/> of the texture</param>
/// <exception cref="Exception"></exception>
public async void CreateD3DTexture(uint width, uint height, D3DFORMAT format = D3DFORMAT.D3DFMT_A8R8G8B8)
public async Task CreateD3DTexture(uint width, uint height, D3DFORMAT format = D3DFORMAT.D3DFMT_A8R8G8B8)
{
if(Address != 0)
ReleaseTexture();
await ReleaseTexture();

var (hresult, pTexture) = await OmsiCreateTextureAsync(width, height, format);
if (HRESULTFailed(hresult))
Expand All @@ -100,7 +100,7 @@ public async void CreateD3DTexture(uint width, uint height, D3DFORMAT format = D
/// </summary>
/// <exception cref="NullReferenceException"></exception>
/// <exception cref="Exception"></exception>
public async void ReleaseTexture()
public async Task ReleaseTexture()
{
if(Address == 0)
throw new NullReferenceException("Texture was already null!");
Expand All @@ -123,13 +123,16 @@ public async void ReleaseTexture()
/// must match the size of this rectangle; pass in <see langword="null"/> to update the whole texture</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <exception cref="Exception"></exception>
public void UpdateTexture<T>(Memory<T> textureData, Rectangle? updateArea = null) where T : unmanaged
public async Task UpdateTexture<T>(Memory<T> textureData, Rectangle? updateArea = null) where T : unmanaged
{
if((textureData.Length * Marshal.SizeOf<T>()) > stagingBufferSize)
throw new ArgumentOutOfRangeException(nameof(textureData));
Memory.WriteMemory(remoteStagingBufferPtr, textureData);

HRESULT hr = OmsiUpdateTextureAsync(TextureAddress, remoteStagingBufferPtr, width, height, updateArea).Result;
uint dataWidth = (updateArea?.right - updateArea?.left) ?? width;
uint dataHeight = (updateArea?.bottom - updateArea?.top) ?? height;

HRESULT hr = await OmsiUpdateTextureAsync(TextureAddress, remoteStagingBufferPtr, dataWidth, dataHeight, updateArea);
if (HRESULTFailed(hr))
throw new Exception("Couldn't update D3D texture! Result: " + hr);
}
Expand Down
27 changes: 18 additions & 9 deletions OmsiHook/OmsiHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public class OmsiHook
public event EventHandler OnOmsiGotD3DContext;
/// <summary>
/// An event raised when Omsi gets a DirectX context.
///
/// Note that this is one of the last events raised when exiting the game; it's raised after PluginFinalize is called.
/// </summary>
/// <remarks>
/// <inheritdoc cref="OnOmsiExited"/>
Expand All @@ -71,6 +73,10 @@ public class OmsiHook
public event EventHandler OnMapChange;
/// <summary>
/// An event raised when Omsi has loaded or unloaded a new map. The <c>EventArgs</c> is a boolean representing whether the map is loaded.
///
/// Note that while this event is raised when the map has finished loading; other systems may still be
/// loading (timetables, weather, humans, ai vehicles, and the map camera are loaded later). If you want to be sure
/// everything has loaded, the best bet would be to wait for an <c>AccessVariable()</c> call.
/// </summary>
/// <remarks>
/// <inheritdoc cref="OnOmsiExited"/>
Expand All @@ -85,30 +91,35 @@ public class OmsiHook
/// <summary>
/// Attaches the hooking application to OMSI.exe.
/// Always call this at some point before trying to read and write data.
/// <param name="initialiseRemoteMethods">Try to initialise the connection to OmsiHookRPCPlugin, which is needed if you intend to call Omsi code or allocate memory.</param>
/// </summary>
/// <param name="initialiseRemoteMethods">Try to initialise the connection to OmsiHookRPCPlugin, which is needed if you intend to call Omsi code or allocate memory.</param>
public async Task AttachToOMSI(bool initialiseRemoteMethods = true)
{
Console.WriteLine("Attaching to OMSI.exe...");

omsiMemory = new Memory();

int cursorLine = Console.CursorTop;
int cursorLine = int.MinValue;
try
{
cursorLine = Console.CursorTop;
} catch { }
DateTime startTime = DateTime.Now;
var found = false;
while (!found)
{
(found, process) = omsiMemory.Attach("omsi");
if (!found) {
Console.WriteLine($"Waiting for OMSI.exe (waited for {(DateTime.Now-startTime).TotalSeconds:0} seconds)...");
Console.SetCursorPosition(0, cursorLine);
if (cursorLine != int.MinValue)
{
Console.WriteLine($"Waiting for OMSI.exe (waited for {(DateTime.Now - startTime).TotalSeconds:0} seconds)...");
Console.SetCursorPosition(0, cursorLine);
}
await Task.Delay(250);
}
}

if(initialiseRemoteMethods)
{
await OmsiRemoteMethods.InitRemoteMethods(omsiMemory);
await OmsiRemoteMethods.InitRemoteMethods(omsiMemory, isLocalPlugin: Process.GetCurrentProcess().ProcessName == process.ProcessName);
}

stateMonitorTask = new(MonitorStateTask);
Expand All @@ -117,8 +128,6 @@ public async Task AttachToOMSI(bool initialiseRemoteMethods = true)
OnOmsiGotD3DContext += OmsiHook_OnOmsiGotD3DContext;
OnOmsiLostD3DContext += OmsiHook_OnOmsiLostD3DContext;
OnMapChange += OmsiHook_OnMapChange;

Console.WriteLine("Connected succesfully!");
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions OmsiHook/OmsiHook.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<Description>OmsiHook is a simple library for hooking into Omsi's memory for modding.</Description>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyVersion>2.3.1.1</AssemblyVersion>
<FileVersion>2.3.1.1</FileVersion>
<Version>2.3.1</Version>
<AssemblyVersion>2.3.2.1</AssemblyVersion>
<FileVersion>2.3.2.1</FileVersion>
<Version>2.3.2</Version>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<SignAssembly>False</SignAssembly>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
Loading

0 comments on commit caff9d9

Please sign in to comment.