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++)
@@ -155,6 +155,7 @@ namespace BrewMonster.Scripts
private void SendCmdDebug(int header, int param, bool hasParam)
{
BMLogger.LogError($"[DlgConsole] Sending Debug Cmd: {(hasParam ? $"{header} {param}" : $"{header}")}");
if (hasParam)
UnityGameSession.c2s_CmdDebug((ushort)header, param);
else
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace BrewMonster.Scripts.Editor
{
public static class DebugCmdHistoryExporter
{
private const string OutputAssetPath = "Assets/PerfectWorld/Resources/DebugCmdHistory.json";
[MenuItem("Tools/DebugCmd/Export History (User -> Resources)")]
public static void ExportUserHistoryToResources()
{
var entries = DebugCmdHistoryStore.Load();
var json = WrapToJson(entries);
Directory.CreateDirectory(Path.GetDirectoryName(OutputAssetPath));
File.WriteAllText(OutputAssetPath, json);
AssetDatabase.ImportAsset(OutputAssetPath);
Debug.Log($"[DebugCmdHistoryExporter] Exported {entries.Count} entries to {OutputAssetPath}");
}
private static string WrapToJson(List<DebugCmdHistoryStore.Entry> entries)
{
// Mirror EntryListWrapper shape: { "items": [...] }
var wrapper = new Wrapper { items = entries ?? new List<DebugCmdHistoryStore.Entry>() };
return JsonUtility.ToJson(wrapper, true);
}
[System.Serializable]
private class Wrapper
{
public List<DebugCmdHistoryStore.Entry> items;
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 7e0f54d86164c2e4c86036e4aadacd04