Merge pull request 'feature/refactor-SkillUI' (#276) from feature/refactor-SkillUI into develop
Reviewed-on: https://git.pthub.vn/Unity/perfect-world-unity/pulls/276
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BrewMonster.Scripts
|
||||
@@ -31,7 +32,10 @@ namespace BrewMonster.Scripts
|
||||
|
||||
public static List<Entry> Load()
|
||||
{
|
||||
// First-run: seed from Resources into PlayerPrefs so runtime can modify via PlayerPrefs.
|
||||
#if UNITY_EDITOR
|
||||
return LoadFromEditorJsonFile();
|
||||
#else
|
||||
// Runtime: first-run seed from Resources into PlayerPrefs, then keep using PlayerPrefs.
|
||||
if (!IsInited())
|
||||
{
|
||||
var seed = LoadFromResources();
|
||||
@@ -41,11 +45,16 @@ namespace BrewMonster.Scripts
|
||||
}
|
||||
|
||||
return LoadFromPlayerPrefs();
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void Save(List<Entry> entries)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
SaveToEditorJsonFile(entries);
|
||||
#else
|
||||
SaveToPlayerPrefs(entries);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static bool Upsert(List<Entry> entries, int header, int param, bool hasParam, string describe)
|
||||
@@ -103,9 +112,22 @@ namespace BrewMonster.Scripts
|
||||
|
||||
public static void Clear()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
try
|
||||
{
|
||||
var path = GetEditorJsonAbsolutePath();
|
||||
if (File.Exists(path))
|
||||
File.Delete(path);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogWarning($"[DebugCmdHistoryStore] Clear (editor json) failed: {ex.Message}");
|
||||
}
|
||||
#else
|
||||
PlayerPrefs.DeleteKey(PlayerPrefsJsonKey);
|
||||
PlayerPrefs.DeleteKey(PlayerPrefsInitedKey);
|
||||
PlayerPrefs.Save();
|
||||
#endif
|
||||
}
|
||||
|
||||
public static bool Remove(List<Entry> entries, int header, int param, bool hasParam)
|
||||
@@ -121,6 +143,61 @@ namespace BrewMonster.Scripts
|
||||
return true;
|
||||
}
|
||||
|
||||
private static string GetEditorJsonAbsolutePath()
|
||||
{
|
||||
// Keep it under Assets/Resources so it can be included as a TextAsset for runtime seeding.
|
||||
return Path.Combine(Application.dataPath, "Resources", $"{ResourcesJsonPathNoExt}.json");
|
||||
}
|
||||
|
||||
private static List<Entry> LoadFromEditorJsonFile()
|
||||
{
|
||||
try
|
||||
{
|
||||
var path = GetEditorJsonAbsolutePath();
|
||||
if (!File.Exists(path))
|
||||
return LoadFromResources(); // fallback if file not created yet
|
||||
|
||||
var json = File.ReadAllText(path);
|
||||
if (string.IsNullOrWhiteSpace(json))
|
||||
return new List<Entry>();
|
||||
|
||||
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 SaveToEditorJsonFile(List<Entry> entries)
|
||||
{
|
||||
try
|
||||
{
|
||||
var wrapper = new EntryListWrapper { items = entries ?? new List<Entry>() };
|
||||
string json = JsonUtility.ToJson(wrapper, prettyPrint: true);
|
||||
|
||||
var path = GetEditorJsonAbsolutePath();
|
||||
var dir = Path.GetDirectoryName(path);
|
||||
if (!string.IsNullOrWhiteSpace(dir) && !Directory.Exists(dir))
|
||||
Directory.CreateDirectory(dir);
|
||||
|
||||
File.WriteAllText(path, json);
|
||||
|
||||
#if UNITY_EDITOR
|
||||
UnityEditor.AssetDatabase.Refresh();
|
||||
#endif
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogWarning($"[DebugCmdHistoryStore] Save (editor json) failed: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static List<Entry> LoadFromResources()
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user