feat(ui): add persistent bilingual settings and combat HUD
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Ironspore
|
||||
{
|
||||
// Settings have their own version and file; progression and recovery receipts stay in ProfileStore.
|
||||
public sealed class SettingsStore
|
||||
{
|
||||
readonly string path;
|
||||
readonly GameSettingsData defaults;
|
||||
public GameSettingsData Data { get; private set; }
|
||||
public string Error { get; private set; }
|
||||
|
||||
public SettingsStore(string path, GameSettingsData defaults = null)
|
||||
{
|
||||
this.path = path;
|
||||
this.defaults = (defaults ?? new GameSettingsData()).Copy();
|
||||
Data = Load(path) ?? Load(path + ".bak") ?? this.defaults.Copy();
|
||||
}
|
||||
|
||||
GameSettingsData Load(string file)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!File.Exists(file))
|
||||
return null;
|
||||
var data = defaults.Copy();
|
||||
JsonUtility.FromJsonOverwrite(File.ReadAllText(file), data);
|
||||
return data.version == 1 ? data : null;
|
||||
}
|
||||
catch (Exception) { return null; }
|
||||
}
|
||||
|
||||
public bool Save(GameSettingsData candidate)
|
||||
{
|
||||
if (candidate == null || candidate.version != 1)
|
||||
{
|
||||
Error = "Unsupported settings version.";
|
||||
return false;
|
||||
}
|
||||
try
|
||||
{
|
||||
var snapshot = candidate.Copy();
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(path)));
|
||||
string temp = path + ".tmp";
|
||||
using (var stream = new FileStream(temp, FileMode.Create, FileAccess.Write, FileShare.None))
|
||||
{
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(JsonUtility.ToJson(snapshot, true));
|
||||
stream.Write(bytes, 0, bytes.Length);
|
||||
stream.Flush(true);
|
||||
}
|
||||
if (File.Exists(path))
|
||||
File.Replace(temp, path, path + ".bak");
|
||||
else
|
||||
File.Move(temp, path);
|
||||
Data = snapshot;
|
||||
Error = null;
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex) { Error = ex.Message; return false; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user