This commit is contained in:
VDH
2026-03-18 17:35:52 +07:00
parent f28efd552f
commit 4e24e1101c
6 changed files with 138 additions and 21 deletions
@@ -6,7 +6,9 @@ namespace BrewMonster.Scripts
{
public static class DebugCmdHistoryStore
{
private const string PlayerPrefsKey = "DebugCmdHistory_v1";
private const string ResourcesJsonPathNoExt = "DebugCmdHistory";
private const string PlayerPrefsJsonKey = "DebugCmdHistory_v1";
private const string PlayerPrefsInitedKey = "DebugCmdHistory_inited_v1";
private const int MaxEntries = 200;
[Serializable]
@@ -29,31 +31,21 @@ namespace BrewMonster.Scripts
public static List<Entry> Load()
{
string json = PlayerPrefs.GetString(PlayerPrefsKey, string.Empty);
if (string.IsNullOrWhiteSpace(json))
return new List<Entry>();
try
// First-run: seed from Resources into PlayerPrefs so runtime can modify via PlayerPrefs.
if (!IsInited())
{
var wrapper = JsonUtility.FromJson<EntryListWrapper>(json);
if (wrapper == null || wrapper.items == null)
return new List<Entry>();
var seed = LoadFromResources();
SaveToPlayerPrefs(seed);
SetInited();
return seed;
}
wrapper.items.RemoveAll(e => e == null);
return wrapper.items;
}
catch
{
return new List<Entry>();
}
return LoadFromPlayerPrefs();
}
public static void Save(List<Entry> entries)
{
var wrapper = new EntryListWrapper { items = entries ?? new List<Entry>() };
string json = JsonUtility.ToJson(wrapper);
PlayerPrefs.SetString(PlayerPrefsKey, json);
PlayerPrefs.Save();
SaveToPlayerPrefs(entries);
}
public static bool Upsert(List<Entry> entries, int header, int param, bool hasParam, string describe)
@@ -111,7 +103,8 @@ namespace BrewMonster.Scripts
public static void Clear()
{
PlayerPrefs.DeleteKey(PlayerPrefsKey);
PlayerPrefs.DeleteKey(PlayerPrefsJsonKey);
PlayerPrefs.DeleteKey(PlayerPrefsInitedKey);
PlayerPrefs.Save();
}
@@ -128,6 +121,70 @@ namespace BrewMonster.Scripts
return true;
}
private static List<Entry> LoadFromResources()
{
try
{
var asset = Resources.Load<TextAsset>(ResourcesJsonPathNoExt);
if (asset == null || string.IsNullOrWhiteSpace(asset.text))
return new List<Entry>();
var wrapper = JsonUtility.FromJson<EntryListWrapper>(asset.text);
if (wrapper == null || wrapper.items == null)
return new List<Entry>();
wrapper.items.RemoveAll(e => e == null);
return wrapper.items;
}
catch
{
return new List<Entry>();
}
}
private static List<Entry> LoadFromPlayerPrefs()
{
string json = PlayerPrefs.GetString(PlayerPrefsJsonKey, string.Empty);
if (string.IsNullOrWhiteSpace(json))
return new List<Entry>();
try
{
var wrapper = JsonUtility.FromJson<EntryListWrapper>(json);
if (wrapper == null || wrapper.items == null)
return new List<Entry>();
wrapper.items.RemoveAll(e => e == null);
return wrapper.items;
}
catch
{
return new List<Entry>();
}
}
private static void SaveToPlayerPrefs(List<Entry> entries)
{
try
{
var wrapper = new EntryListWrapper { items = entries ?? new List<Entry>() };
string json = JsonUtility.ToJson(wrapper);
PlayerPrefs.SetString(PlayerPrefsJsonKey, json);
PlayerPrefs.Save();
}
catch (Exception ex)
{
Debug.LogWarning($"[DebugCmdHistoryStore] Save failed: {ex.Message}");
}
}
private static bool IsInited() => PlayerPrefs.GetInt(PlayerPrefsInitedKey, 0) == 1;
private static void SetInited()
{
PlayerPrefs.SetInt(PlayerPrefsInitedKey, 1);
PlayerPrefs.Save();
}
private static int FindIndex(List<Entry> entries, int header, int param, bool hasParam)
{
for (int i = 0; i < entries.Count; i++)