LocalPlayerPrefsDataCtr.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System;
  6. using LuaInterface;
  7. using System.IO;
  8. public class LocalPlayerPrefsDataCtr : EditorWindow
  9. {
  10. private static void OnQuit(PlayModeStateChange state)
  11. {
  12. if (state == PlayModeStateChange.EnteredEditMode)
  13. {
  14. for (int i = 0; i < clearList.Count; i++)
  15. {
  16. PlayerPrefs.DeleteKey(clearList[i]);
  17. }
  18. }
  19. }
  20. string[] keyTypes = new string[] { "Cache_Skill_Key", "PrivateChatInfos" };
  21. string[] keyTypeDes = new string[] { "技能红点数据", "私聊信息数据" };
  22. static int index = 0;
  23. static PlayerPrefPair[] pairs;
  24. static long uid = 0;
  25. static LocalPlayerPrefsDataCtr window;
  26. [MenuItem("Tools/LocalPlayerPrefsDataCtr")]
  27. static void Init()
  28. {
  29. uidKeys = new Dictionary<string, List<string>>();
  30. uidKeyList = new List<string>();
  31. otherKeyList = new List<string>();
  32. clearList = new List<string>();
  33. index = 0;
  34. uid = 0;
  35. pairs = GetAll();
  36. DisposalData();
  37. GetUserUidByLua();
  38. window = (LocalPlayerPrefsDataCtr)GetWindow(typeof(LocalPlayerPrefsDataCtr));
  39. EditorApplication.playModeStateChanged += OnQuit;
  40. window.Show();
  41. }
  42. static Dictionary<string, List<string>> uidKeys = new Dictionary<string, List<string>>();
  43. static List<string> uidKeyList = new List<string>();
  44. static List<string> otherKeyList = new List<string>();
  45. static List<string> clearList = new List<string>();
  46. private static void DisposalData()
  47. {
  48. foreach (var item in pairs)
  49. {
  50. int index = FindCharIndex(item.Key);
  51. if (index == 1)
  52. {
  53. //无效key uid为0
  54. string uid = item.Key.Substring(0, 1);
  55. string value = item.Key.Substring(1);
  56. if (uidKeys.ContainsKey(uid))
  57. {
  58. uidKeys[uid].Add(value);
  59. }
  60. else
  61. {
  62. uidKeyList.Add(uid);
  63. uidKeys[uid] = new List<string>();
  64. uidKeys[uid].Add(value);
  65. }
  66. }
  67. else if (index == 19)
  68. {
  69. string uid = item.Key.Substring(0, 19);
  70. string value = item.Key.Substring(19);
  71. if (uidKeys.ContainsKey(uid))
  72. {
  73. uidKeys[uid].Add(value);
  74. }
  75. else
  76. {
  77. uidKeyList.Add(uid);
  78. uidKeys[uid] = new List<string>();
  79. uidKeys[uid].Add(value);
  80. }
  81. }
  82. else
  83. {
  84. otherKeyList.Add(item.Key);
  85. }
  86. }
  87. }
  88. private static int FindCharIndex(string key)
  89. {
  90. for (int i = 0; i < key.Length; i++)
  91. {
  92. if (!(key[i] >= (char)48 && key[i] <= (char)58))
  93. {
  94. return i;
  95. }
  96. }
  97. return 0;
  98. }
  99. bool isShowUid = false;
  100. bool isShowOhter = false;
  101. bool isShowcurrUid = false;
  102. bool isShowKeyDes = false;
  103. Vector2 pos;
  104. static void GetUserUidByLua()
  105. {
  106. if (Application.isPlaying &&LuaMgr.Instance != null && LuaMgr.GetMainState() != null)
  107. {
  108. try
  109. {
  110. LuaTable table = LuaMgr.GetMainState().GetTable("ManagerContainer.DataMgr.UserData");
  111. if (table == null) return;
  112. LuaFunction func = table.GetLuaFunction("GetUserId");
  113. func.BeginPCall();
  114. func.Push(table);
  115. func.PCall();
  116. uid = func.CheckLong();
  117. func.EndPCall();
  118. }
  119. catch (LuaException e)
  120. {
  121. Debug.LogError("LuaBuffer.ManagerContainer.DataMgr.UserData " + e.Message);
  122. return;
  123. }
  124. }
  125. }
  126. private void OnGUI()
  127. {
  128. pos = EditorGUILayout.BeginScrollView(pos);
  129. EditorGUILayout.LabelField("-------------对应key的描述(持续添加)-------------");
  130. isShowKeyDes = EditorGUILayout.Foldout(isShowKeyDes, "显示隐藏对应key描述");
  131. if (isShowKeyDes)
  132. {
  133. for (int i = 0; i < keyTypes.Length; i++)
  134. {
  135. EditorGUILayout.BeginHorizontal();
  136. EditorGUILayout.LabelField(keyTypes[i]);
  137. EditorGUILayout.LabelField(keyTypeDes[i]);
  138. EditorGUILayout.EndHorizontal();
  139. }
  140. }
  141. if (uid != 0)
  142. {
  143. EditorGUILayout.LabelField("-------------当前玩家UID所有key数据-------------");
  144. isShowcurrUid = EditorGUILayout.Foldout(isShowcurrUid, "显示隐藏当前玩家UID所有key数据");
  145. if (isShowcurrUid)
  146. {
  147. if (uidKeys.ContainsKey(uid.ToString()))
  148. {
  149. for (int i = 0; i < uidKeys[uid.ToString()].Count; i++)
  150. {
  151. EditorGUILayout.BeginHorizontal();
  152. EditorGUILayout.LabelField(uid.ToString() + uidKeys[uid.ToString()][i]);
  153. if (GUILayout.Button("打印", GUILayout.Width(50), GUILayout.Height(20)))
  154. {
  155. string data = PlayerPrefs.GetString(uid.ToString() + uidKeys[uid.ToString()][i]);
  156. if (data != null)
  157. {
  158. Debug.Log(data);
  159. }
  160. }
  161. if (GUILayout.Button("清除", GUILayout.Width(50), GUILayout.Height(20)))
  162. {
  163. if (!clearList.Contains(uid.ToString() + uidKeys[uid.ToString()][i]))
  164. {
  165. clearList.Add(uid.ToString() + uidKeys[uid.ToString()][i]);
  166. }
  167. }
  168. EditorGUILayout.EndHorizontal();
  169. }
  170. }
  171. }
  172. }
  173. EditorGUILayout.LabelField("-------------UID类型key数据-------------");
  174. isShowUid = EditorGUILayout.Foldout(isShowUid, "显示隐藏UID类型key数据");
  175. if (isShowUid)
  176. {
  177. EditorGUILayout.BeginHorizontal();
  178. EditorGUILayout.LabelField("请选择UID");
  179. index = EditorGUILayout.Popup(index, uidKeyList.ToArray());
  180. EditorGUILayout.EndHorizontal();
  181. for (int i = 0; i < uidKeys[uidKeyList[index]].Count; i++)
  182. {
  183. EditorGUILayout.BeginHorizontal();
  184. EditorGUILayout.LabelField(uidKeyList[index] + uidKeys[uidKeyList[index]][i]);
  185. if (GUILayout.Button("打印", GUILayout.Width(50), GUILayout.Height(20)))
  186. {
  187. string data = PlayerPrefs.GetString(uidKeyList[index] + uidKeys[uidKeyList[index]][i]);
  188. if (data != null)
  189. {
  190. Debug.Log(data);
  191. }
  192. }
  193. if (GUILayout.Button("清除", GUILayout.Width(50), GUILayout.Height(20)))
  194. {
  195. PlayerPrefs.DeleteKey(uidKeyList[index] + uidKeys[uidKeyList[index]][i]);
  196. }
  197. EditorGUILayout.EndHorizontal();
  198. }
  199. }
  200. EditorGUILayout.LabelField("-------------其他类型key数据-------------");
  201. isShowOhter = EditorGUILayout.Foldout(isShowOhter, "显示隐藏其他类型key数据");
  202. if (isShowOhter)
  203. {
  204. for (int i = 0; i < otherKeyList.Count; i++)
  205. {
  206. EditorGUILayout.BeginHorizontal();
  207. EditorGUILayout.LabelField(otherKeyList[i]);
  208. if (GUILayout.Button("打印", GUILayout.Width(50), GUILayout.Height(20)))
  209. {
  210. string data = PlayerPrefs.GetString(otherKeyList[i]);
  211. if (data != null)
  212. {
  213. Debug.Log(data);
  214. }
  215. }
  216. if (GUILayout.Button("清除", GUILayout.Width(50), GUILayout.Height(20)))
  217. {
  218. PlayerPrefs.DeleteKey(otherKeyList[i]);
  219. }
  220. EditorGUILayout.EndHorizontal();
  221. }
  222. }
  223. EditorGUILayout.EndScrollView();
  224. Repaint();
  225. }
  226. private void OnDestroy()
  227. {
  228. EditorApplication.playModeStateChanged -= OnQuit;
  229. }
  230. [Serializable]
  231. public struct PlayerPrefPair
  232. {
  233. public string Key { get; set; }
  234. public object Value { get; set; }
  235. }
  236. public static PlayerPrefPair[] GetAll()
  237. {
  238. return GetAll(PlayerSettings.companyName, PlayerSettings.productName);
  239. }
  240. public static PlayerPrefPair[] GetAll(string companyName, string productName)
  241. {
  242. if (Application.platform == RuntimePlatform.WindowsEditor)
  243. {
  244. // From Unity docs: On Windows, PlayerPrefs are stored in the registry under HKCU\Software\[company name]\[product name] key, where company and product names are the names set up in Project Settings.
  245. #if UNITY_5_5_OR_NEWER
  246. // From Unity 5.5 editor player prefs moved to a specific location
  247. Microsoft.Win32.RegistryKey registryKey =
  248. Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Unity\\UnityEditor\\" + companyName + "\\" + productName);
  249. #else
  250. Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\" + companyName + "\\" + productName);
  251. #endif
  252. // Parse the registry if the specified registryKey exists
  253. if (registryKey != null)
  254. {
  255. // Get an array of what keys (registry value names) are stored
  256. string[] valueNames = registryKey.GetValueNames();
  257. // Create the array of the right size to take the saved player prefs
  258. PlayerPrefPair[] tempPlayerPrefs = new PlayerPrefPair[valueNames.Length];
  259. // Parse and convert the registry saved player prefs into our array
  260. int i = 0;
  261. foreach (string valueName in valueNames)
  262. {
  263. string key = valueName;
  264. //移除注册表中的存储时间字符串后缀
  265. // Remove the _h193410979 style suffix used on player pref keys in Windows registry
  266. int index = key.LastIndexOf("_");
  267. key = key.Remove(index, key.Length - index);
  268. // Get the value from the registry
  269. object ambiguousValue = registryKey.GetValue(valueName);
  270. // Unfortunately floats will come back as an int (at least on 64 bit) because the float is stored as
  271. // 64 bit but marked as 32 bit - which confuses the GetValue() method greatly!
  272. if (ambiguousValue.GetType() == typeof(int))
  273. {
  274. // If the player pref is not actually an int then it must be a float, this will evaluate to true
  275. // (impossible for it to be 0 and -1 at the same time)
  276. if (PlayerPrefs.GetInt(key, -1) == -1 && PlayerPrefs.GetInt(key, 0) == 0)
  277. {
  278. // Fetch the float value from PlayerPrefs in memory
  279. ambiguousValue = PlayerPrefs.GetFloat(key);
  280. }
  281. }
  282. else if (ambiguousValue.GetType() == typeof(byte[]))
  283. {
  284. // On Unity 5 a string may be stored as binary, so convert it back to a string
  285. ambiguousValue = System.Text.Encoding.Default.GetString((byte[])ambiguousValue);
  286. }
  287. // Assign the key and value into the respective record in our output array
  288. tempPlayerPrefs[i] = new PlayerPrefPair() { Key = key, Value = ambiguousValue };
  289. i++;
  290. }
  291. // Return the results
  292. return tempPlayerPrefs;
  293. }
  294. else
  295. {
  296. // No existing player prefs saved (which is valid), so just return an empty array
  297. return new PlayerPrefPair[0];
  298. }
  299. }
  300. else
  301. {
  302. throw new NotSupportedException("PlayerPrefsEditor doesn't support this Unity Editor platform");
  303. }
  304. }
  305. }