Skip to content

Commit f71fddf

Browse files
committed
feat: display json in window tools
1 parent 3f74dfa commit f71fddf

File tree

1 file changed

+96
-38
lines changed

1 file changed

+96
-38
lines changed
Lines changed: 96 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
#if UNITY_EDITOR
22
using System;
3+
using System.IO;
4+
using System.Text;
5+
using Code.Infrastructure.Services.PersistenceProgress.Player;
36
using Sirenix.OdinInspector.Editor;
47
using Sirenix.Serialization;
58
using Sirenix.Utilities.Editor;
69
using UnityEditor;
710
using UnityEngine;
8-
using SerializationUtility = Sirenix.Serialization.SerializationUtility;
911

1012
namespace Code.Editor
1113
{
1214
public class SaveWindowEditor : OdinEditorWindow
1315
{
14-
private const string PlayerDataKey = "PlayerData";
16+
private const string PlayerPrefsKey = "PlayerData";
17+
private const string JsonFileName = "player_data.json";
1518

16-
private string savePath => Application.persistentDataPath;
17-
private Vector2 scrollPos;
19+
private string SavePath => Application.persistentDataPath;
20+
private string JsonFilePath => Path.Combine(SavePath, JsonFileName);
21+
22+
private Vector2 scrollPrefs;
23+
private Vector2 scrollJson;
24+
25+
private string DecodedPrefsData;
26+
private string DecodedJsonData;
27+
28+
private string PrefsMessage = "❌ No PlayerPrefs data found.";
29+
private string JsonMessage = "❌ No JSON file found.";
1830

1931
[MenuItem("Tools/Save Window Editor")]
2032
private static void OpenWindow()
@@ -25,93 +37,139 @@ private static void OpenWindow()
2537
window.Show();
2638
}
2739

28-
public string DecodedPlayerData;
29-
public string Message = "❌ No PlayerData found.";
30-
3140
private void OnEnable()
3241
{
3342
Refresh();
3443
}
3544

3645
protected override void DrawEditor(int index)
3746
{
38-
EditorGUILayout.Space();
47+
DrawSection("🧠 PlayerPrefs Preview", SavePath, PrefsMessage, DecodedPrefsData, ref scrollPrefs,
48+
Refresh, DeletePlayerPrefs);
49+
50+
GUILayout.Space(20);
3951

40-
// Main Box Group
41-
SirenixEditorGUI.Title("🧠 PlayerPrefs Preview", null, TextAlignment.Left, true);
52+
DrawSection("📄 JSON File Preview", JsonFilePath, JsonMessage, DecodedJsonData, ref scrollJson,
53+
Refresh, DeleteJson);
54+
}
55+
56+
private void DrawSection(string title, string path, string message, string data, ref Vector2 scrollPos,
57+
Action refreshAction,
58+
Action deleteAction)
59+
{
60+
SirenixEditorGUI.Title(title, null, TextAlignment.Left, true);
4261
SirenixEditorGUI.BeginBox();
43-
62+
4463
EditorGUILayout.LabelField("📁 Save Location", EditorStyles.boldLabel);
45-
EditorGUILayout.HelpBox(savePath, MessageType.Info);
64+
EditorGUILayout.HelpBox(path, MessageType.Info);
65+
66+
GUILayout.Space(10);
4667

47-
EditorGUILayout.Space();
48-
49-
if (!string.IsNullOrEmpty(DecodedPlayerData))
68+
if (!string.IsNullOrEmpty(data))
5069
{
5170
scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Height(300));
52-
EditorGUILayout.TextArea(DecodedPlayerData, GUILayout.ExpandHeight(true));
71+
EditorGUILayout.TextArea(data, GUILayout.ExpandHeight(true));
5372
EditorGUILayout.EndScrollView();
5473
}
5574
else
5675
{
57-
EditorGUILayout.HelpBox(Message, MessageType.Warning);
76+
EditorGUILayout.HelpBox(message, MessageType.Warning);
5877
}
5978

6079
GUILayout.Space(10);
61-
62-
// Buttons
6380
GUI.backgroundColor = new Color(0.6f, 0.9f, 1f);
6481
if (GUILayout.Button("🔄 Refresh", GUILayout.Height(35)))
65-
Refresh();
82+
refreshAction.Invoke();
6683

6784
GUILayout.Space(5);
68-
6985
GUI.backgroundColor = new Color(1f, 0.4f, 0.4f);
70-
if (GUILayout.Button("🗑 Delete PlayerData", GUILayout.Height(35)))
71-
DeletePlayerData();
86+
if (GUILayout.Button("🗑 Delete", GUILayout.Height(35)))
87+
deleteAction.Invoke();
7288

7389
GUI.backgroundColor = Color.white;
74-
7590
SirenixEditorGUI.EndBox();
7691
}
7792

7893
private void Refresh()
7994
{
80-
DecodedPlayerData = string.Empty;
95+
RefreshPlayerPrefs();
96+
RefreshJsonFile();
97+
Repaint();
98+
}
99+
100+
private void RefreshPlayerPrefs()
101+
{
102+
DecodedPrefsData = string.Empty;
81103

82-
if (PlayerPrefs.HasKey(PlayerDataKey))
104+
if (PlayerPrefs.HasKey(PlayerPrefsKey))
83105
{
84106
try
85107
{
86-
string base64 = PlayerPrefs.GetString(PlayerDataKey);
108+
string base64 = PlayerPrefs.GetString(PlayerPrefsKey);
87109
byte[] data = Convert.FromBase64String(base64);
88-
object deserialized = SerializationUtility.DeserializeValue<object>(data, DataFormat.JSON);
89-
DecodedPlayerData = JsonUtility.ToJson(deserialized, true);
90-
Message = string.Empty;
110+
var deserialized = Sirenix.Serialization.SerializationUtility.DeserializeValue<PlayerData>(data, DataFormat.JSON);
111+
string json = Encoding.UTF8.GetString(
112+
Sirenix.Serialization.SerializationUtility.SerializeValue(deserialized, DataFormat.JSON)
113+
);
114+
DecodedPrefsData = json;
115+
PrefsMessage = string.Empty;
91116
}
92117
catch (Exception e)
93118
{
94-
DecodedPlayerData = $"❌ Failed to decode PlayerData:\n{e.Message}";
119+
DecodedPrefsData = $"❌ Failed to decode PlayerPrefs:\n{e.Message}";
120+
PrefsMessage = "❌ PlayerPrefs data decode failed.";
95121
}
96122
}
97123
else
98124
{
99-
Message = "❌ No PlayerData found.";
125+
PrefsMessage = "❌ No PlayerPrefs data found.";
100126
}
127+
}
101128

102-
Repaint();
129+
private void RefreshJsonFile()
130+
{
131+
DecodedJsonData = string.Empty;
132+
133+
if (File.Exists(JsonFilePath))
134+
{
135+
try
136+
{
137+
DecodedJsonData = File.ReadAllText(JsonFilePath);
138+
JsonMessage = string.Empty;
139+
}
140+
catch (Exception e)
141+
{
142+
DecodedJsonData = $"❌ Failed to read JSON:\n{e.Message}";
143+
JsonMessage = "❌ Failed to load JSON file.";
144+
}
145+
}
146+
else
147+
{
148+
JsonMessage = "❌ No JSON file found.";
149+
}
103150
}
104151

105-
private void DeletePlayerData()
152+
153+
private void DeletePlayerPrefs()
106154
{
107-
if (PlayerPrefs.HasKey(PlayerDataKey))
155+
if (PlayerPrefs.HasKey(PlayerPrefsKey))
108156
{
109-
PlayerPrefs.DeleteKey(PlayerDataKey);
157+
PlayerPrefs.DeleteKey(PlayerPrefsKey);
110158
PlayerPrefs.Save();
159+
Debug.Log("🧹 PlayerPrefs deleted.");
160+
Refresh();
161+
}
162+
}
163+
164+
private void DeleteJson()
165+
{
166+
if (File.Exists(JsonFilePath))
167+
{
168+
File.Delete(JsonFilePath);
169+
Debug.Log("🧹 JSON file deleted.");
111170
Refresh();
112-
Debug.Log("🧹 PlayerData deleted.");
113171
}
114172
}
115173
}
116174
}
117-
#endif
175+
#endif

0 commit comments

Comments
 (0)