Skip to content

Commit

Permalink
Bugfixes for plugins:
Browse files Browse the repository at this point in the history
 - async D3DTexture methods now correctly return a Task and can be awaited
 - OmsiRemoteMethods now actually has the option of using the local OmsiHookInvoker instead of RPC if available
 - This also fixes the race condition when loading a plugin using OmsiHook might have required the RPC plugin to already be loaded
 - Updated OmsiHookPlugin demo
 - Started work on function hooking code
  • Loading branch information
space928 committed Dec 14, 2023
1 parent 79ca64c commit dbf97be
Show file tree
Hide file tree
Showing 11 changed files with 395 additions and 263 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(), new OmsiRemoteMethods.Rectangle() { left=0, top=0, right=texWidth/2, bottom=texHeight/2});
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!");
}
}
}
12 changes: 6 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,7 +123,7 @@ 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));
Expand All @@ -132,7 +132,7 @@ public void UpdateTexture<T>(Memory<T> textureData, Rectangle? updateArea = null
uint dataWidth = (updateArea?.right - updateArea?.left) ?? width;
uint dataHeight = (updateArea?.bottom - updateArea?.top) ?? height;

HRESULT hr = OmsiUpdateTextureAsync(TextureAddress, remoteStagingBufferPtr, dataWidth, dataHeight, updateArea).Result;
HRESULT hr = await OmsiUpdateTextureAsync(TextureAddress, remoteStagingBufferPtr, dataWidth, dataHeight, updateArea);
if (HRESULTFailed(hr))
throw new Exception("Couldn't update D3D texture! Result: " + hr);
}
Expand Down
10 changes: 8 additions & 2 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,8 +91,8 @@ 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)
{
omsiMemory = new Memory();
Expand All @@ -113,7 +119,7 @@ public async Task AttachToOMSI(bool initialiseRemoteMethods = true)

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

stateMonitorTask = new(MonitorStateTask);
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 dbf97be

Please sign in to comment.