feat: add cross-engine platform services
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using ShrinkSDK.Runtime;
|
||||
|
||||
namespace ShrinkDataSaver
|
||||
{
|
||||
public static class ShrinkDataSaverPlatform
|
||||
{
|
||||
public static IShrinkLogger? Logger { get; private set; }
|
||||
public static IShrinkClock? Clock { get; private set; }
|
||||
public static IShrinkPathProvider? Paths { get; private set; }
|
||||
public static IShrinkScreenshotProvider? Screenshots { get; private set; }
|
||||
public static float SettingsWriteDebounceSeconds { get; set; } = 0.5f;
|
||||
public static int MaxSlots { get; set; }
|
||||
|
||||
public static void Configure(IShrinkPathProvider paths, IShrinkClock clock,
|
||||
IShrinkLogger? logger = null, IShrinkScreenshotProvider? screenshots = null)
|
||||
{
|
||||
Paths = paths ?? throw new ArgumentNullException(nameof(paths));
|
||||
Clock = clock ?? throw new ArgumentNullException(nameof(clock));
|
||||
Logger = logger;
|
||||
Screenshots = screenshots;
|
||||
}
|
||||
|
||||
public static void Initialize(string? rootPath = null, string saveFileExtension = ".json",
|
||||
string settingsFileName = "settings.json", int currentSaveVersion = 1)
|
||||
{
|
||||
rootPath ??= Paths?.PersistentDataPath;
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
rootPath ??= UnityEngine.Application.persistentDataPath;
|
||||
#endif
|
||||
if (string.IsNullOrWhiteSpace(rootPath))
|
||||
throw new InvalidOperationException("ShrinkDataSaver platform paths are not configured.");
|
||||
var storage = new LocalStorageProvider(rootPath);
|
||||
ShrinkSettings.Initialize(storage, System.IO.Path.Combine(rootPath, settingsFileName));
|
||||
ShrinkSave.Initialize(storage, System.IO.Path.Combine(rootPath, "saves"), saveFileExtension, currentSaveVersion);
|
||||
}
|
||||
|
||||
internal static double TimeSeconds
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Clock != null) return Clock.UnscaledTimeSeconds;
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
return UnityEngine.Time.realtimeSinceStartupAsDouble;
|
||||
#else
|
||||
return 0d;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
internal static void Info(string message)
|
||||
{
|
||||
if (Logger != null) Logger.Log(ShrinkLogLevel.Information, message);
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
else UnityEngine.Debug.Log(message);
|
||||
#endif
|
||||
}
|
||||
|
||||
internal static void Warning(string message)
|
||||
{
|
||||
if (Logger != null) Logger.Log(ShrinkLogLevel.Warning, message);
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
else UnityEngine.Debug.LogWarning(message);
|
||||
#endif
|
||||
}
|
||||
|
||||
internal static void Error(string message)
|
||||
{
|
||||
if (Logger != null) Logger.Log(ShrinkLogLevel.Error, message);
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
else UnityEngine.Debug.LogError(message);
|
||||
#endif
|
||||
}
|
||||
|
||||
internal static void Exception(Exception exception)
|
||||
{
|
||||
if (Logger != null) Logger.Log(ShrinkLogLevel.Error, exception.Message, exception);
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
else UnityEngine.Debug.LogException(exception);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user