forked from Xeeynamo/sonic-hybrid-rsdk
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
569 additions
and
436 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.