Files
ShrinkDataSaver/Runtime/ShrinkDataSaverPlatform.cs
T
cneicy 67d6814c4b
Publish UPM package / publish (push) Successful in 2s
Publish NuGet packages / publish (push) Successful in 4m2s
feat: update Godot integration and coordinated package dependencies
2026-11-06 19:13:16 +08:00

106 lines
3.8 KiB
C#

#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);
Initialize(storage, rootPath, saveFileExtension, settingsFileName, currentSaveVersion);
}
public static void Initialize(IStorageProvider storage, string? rootPath = null,
string saveFileExtension = ".json", string settingsFileName = "settings.json",
int currentSaveVersion = 1)
{
if (storage == null)
throw new ArgumentNullException(nameof(storage));
rootPath ??= Paths?.PersistentDataPath;
#if UNITY_5_3_OR_NEWER
rootPath ??= UnityEngine.Application.persistentDataPath;
#endif
var settingsPath = string.IsNullOrWhiteSpace(rootPath)
? settingsFileName
: System.IO.Path.Combine(rootPath, settingsFileName);
var savesDir = string.IsNullOrWhiteSpace(rootPath)
? "saves"
: System.IO.Path.Combine(rootPath, "saves");
ShrinkSettings.Initialize(storage, settingsPath);
ShrinkSave.Initialize(storage, savesDir, 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
}
}
}