Added logging
This commit is contained in:
@@ -1,13 +1,34 @@
|
||||
using System;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using OmegaLeo.HelperLib.Shared.Attributes;
|
||||
|
||||
namespace OmegaLeo.HelperLib.Steamworks
|
||||
{
|
||||
[Documentation(nameof(SteamManager), "Manages Steamworks initialization, callback polling, and shutdown for applications using the Steam API.", null,
|
||||
@"```csharp
|
||||
SteamManager.ConfigureLogger(logger);
|
||||
SteamManager.ConfigureAppId(480);
|
||||
var steam = SteamManager.Instance;
|
||||
steam.Update();
|
||||
```")]
|
||||
public class SteamManager
|
||||
{
|
||||
private static SteamManager _instance;
|
||||
private static uint? _configuredAppId;
|
||||
private static ILogger _logger = NullLogger.Instance;
|
||||
[Documentation(nameof(Instance), "Singleton access to the Steam manager.", null, null)]
|
||||
public static SteamManager Instance => _instance ??= new SteamManager();
|
||||
|
||||
internal static ILogger Logger => _logger;
|
||||
|
||||
[Documentation(nameof(ConfigureLogger), "Configures the logger used by Steamworks helper classes.", new[] { "logger: ILogger instance to receive Steamworks logs." }, null)]
|
||||
public static void ConfigureLogger(ILogger logger)
|
||||
{
|
||||
_logger = logger ?? NullLogger.Instance;
|
||||
}
|
||||
|
||||
[Documentation(nameof(ConfigureAppId), "Configures the Steam AppId used during initialization. Must be called before accessing the singleton instance.", new[] { "appId: Steam application identifier." }, null)]
|
||||
public static bool ConfigureAppId(uint appId)
|
||||
{
|
||||
if (appId == 0)
|
||||
@@ -26,6 +47,7 @@ namespace OmegaLeo.HelperLib.Steamworks
|
||||
return true;
|
||||
}
|
||||
|
||||
[Documentation(nameof(IsSteamworksInitialized), "Indicates whether Steamworks is initialized and ready for API calls.", null, null)]
|
||||
public bool IsSteamworksInitialized { get; private set; }
|
||||
|
||||
private SteamManager()
|
||||
@@ -36,20 +58,21 @@ namespace OmegaLeo.HelperLib.Steamworks
|
||||
if (SteamNativeApi.Initialize(_configuredAppId))
|
||||
{
|
||||
IsSteamworksInitialized = true;
|
||||
Console.WriteLine("Steamworks Initialized Successfully!");
|
||||
_logger.LogInformation("Steamworks initialized successfully.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Steamworks Initialization Failed. Is Steam running?");
|
||||
_logger.LogWarning("Steamworks initialization failed. Ensure Steam is running and AppId context is valid.");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"An exception occurred during Steamworks initialization: {e.Message}");
|
||||
_logger.LogError(e, "An exception occurred during Steamworks initialization.");
|
||||
}
|
||||
}
|
||||
|
||||
// This must be called from your game's main loop (e.g., Update in Unity/Godot)
|
||||
[Documentation(nameof(Update), "Executes Steamworks callbacks. Call every frame from your game loop.", null, null)]
|
||||
public void Update()
|
||||
{
|
||||
if (IsSteamworksInitialized)
|
||||
@@ -58,6 +81,7 @@ namespace OmegaLeo.HelperLib.Steamworks
|
||||
}
|
||||
}
|
||||
|
||||
[Documentation(nameof(Shutdown), "Shuts down Steamworks and clears the initialized state.", null, null)]
|
||||
public void Shutdown()
|
||||
{
|
||||
if (IsSteamworksInitialized)
|
||||
@@ -67,6 +91,7 @@ namespace OmegaLeo.HelperLib.Steamworks
|
||||
}
|
||||
}
|
||||
|
||||
[Documentation(nameof(GetSteamName), "Gets the current Steam persona name. Returns \"Player\" when Steamworks is unavailable.", null, null)]
|
||||
public string GetSteamName()
|
||||
{
|
||||
if (IsSteamworksInitialized)
|
||||
|
||||
Reference in New Issue
Block a user