Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
CCIGAMES committed Dec 18, 2024
1 parent bfb9559 commit 4b600e2
Show file tree
Hide file tree
Showing 7 changed files with 569 additions and 436 deletions.
7 changes: 4 additions & 3 deletions Custom Client/CustomClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<AssemblyName>SonicHybrid</AssemblyName>
<ApplicationIcon>icon.ico</ApplicationIcon>
<StartupObject>SonicHybridUltimate.CustomClient.Program</StartupObject>
<RootNamespace>SonicHybridUltimate</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand All @@ -26,9 +27,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
Expand Down
138 changes: 92 additions & 46 deletions Custom Client/Engines/OxygenEngine.cs
Original file line number Diff line number Diff line change
@@ -1,86 +1,132 @@
using System;
using System.Runtime.InteropServices;
using Microsoft.Extensions.Logging;

namespace SonicHybridUltimate.Engines
{
public class OxygenEngine : IGameEngine
public class OxygenEngine : IDisposable
{
[DllImport("OxygenEngine", CallingConvention = CallingConvention.Cdecl)]
private static extern int InitOxygenEngine(string scriptPath);
private readonly ILogger<OxygenEngine> _logger;
private bool _isInitialized;
private string _currentScript = string.Empty;
private bool _isDisposed;

[DllImport("OxygenEngine", CallingConvention = CallingConvention.Cdecl)]
private static extern void UpdateOxygenEngine();

[DllImport("OxygenEngine", CallingConvention = CallingConvention.Cdecl)]
private static extern void CleanupOxygenEngine();

private bool isInitialized = false;
private string currentGame = "";
private readonly ILogger logger;

public OxygenEngine(ILogger logger)
public OxygenEngine(ILogger<OxygenEngine> logger)
{
this.logger = logger;
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public bool Initialize(string scriptPath)
{
ThrowIfDisposed();

try
{
int result = InitOxygenEngine(scriptPath);
isInitialized = result == 0;
if (isInitialized)
_logger.LogInformation("Initializing Oxygen Engine with script: {ScriptPath}", scriptPath);

if (_isInitialized)
{
currentGame = scriptPath;
logger.Log($"Oxygen Engine initialized with script: {scriptPath}");
_logger.LogWarning("Oxygen Engine is already initialized. Cleaning up first...");
Cleanup();
}

var result = NativeMethods.InitOxygenEngine(scriptPath);
_isInitialized = (result == 1);

if (_isInitialized)
{
_currentScript = scriptPath;
_logger.LogInformation("Oxygen Engine initialized successfully");
}
else
{
logger.LogError($"Failed to initialize Oxygen Engine with script: {scriptPath}");
_logger.LogError("Failed to initialize Oxygen Engine");
}
return isInitialized;

return _isInitialized;
}
catch (Exception ex)
{
logger.LogError($"Error initializing Oxygen Engine: {ex.Message}");
_logger.LogError(ex, "Error initializing Oxygen Engine");
return false;
}
}

public void Update()
{
if (isInitialized)
ThrowIfDisposed();

if (!_isInitialized)
{
try
{
UpdateOxygenEngine();
}
catch (Exception ex)
{
logger.LogError($"Error updating Oxygen Engine: {ex.Message}");
}
return;
}

try
{
NativeMethods.UpdateOxygenEngine();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating Oxygen Engine");
throw;
}
}

public void Cleanup()
{
if (isInitialized)
if (!_isInitialized)
{
try
{
CleanupOxygenEngine();
isInitialized = false;
currentGame = "";
logger.Log("Oxygen Engine cleaned up successfully");
}
catch (Exception ex)
{
logger.LogError($"Error cleaning up Oxygen Engine: {ex.Message}");
}
return;
}

try
{
_logger.LogInformation("Cleaning up Oxygen Engine");
NativeMethods.CleanupOxygenEngine();
_isInitialized = false;
_currentScript = string.Empty;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error cleaning up Oxygen Engine");
throw;
}
}

public bool IsRunning => isInitialized;
public string CurrentGame => currentGame;
private void ThrowIfDisposed()
{
if (_isDisposed)
{
throw new ObjectDisposedException(nameof(OxygenEngine));
}
}

public void Dispose()
{
if (_isDisposed)
{
return;
}

if (_isInitialized)
{
Cleanup();
}

_isDisposed = true;
GC.SuppressFinalize(this);
}

private static class NativeMethods
{
[DllImport("OxygenEngine", CallingConvention = CallingConvention.Cdecl)]
public static extern int InitOxygenEngine(string scriptPath);

[DllImport("OxygenEngine", CallingConvention = CallingConvention.Cdecl)]
public static extern void UpdateOxygenEngine();

[DllImport("OxygenEngine", CallingConvention = CallingConvention.Cdecl)]
public static extern void CleanupOxygenEngine();
}
}
}
Loading

0 comments on commit 4b600e2

Please sign in to comment.