| | | 1 | | #if UNITY_EDITOR |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Text; |
| | | 6 | | using UnityEditor; |
| | | 7 | | using UnityEngine; |
| | | 8 | | using Object = UnityEngine.Object; |
| | | 9 | | #if UNITY_6000_4_OR_NEWER |
| | | 10 | | using EntityId = UnityEngine.EntityId; |
| | | 11 | | #else |
| | | 12 | | using EntityId = System.Int32; |
| | | 13 | | #endif |
| | | 14 | | |
| | | 15 | | namespace Hertzole.ScriptableValues.Debugging |
| | | 16 | | { |
| | | 17 | | internal static class UserSettings |
| | | 18 | | { |
| | | 19 | | // Used to keep track of temporary objects, like objects used in testing. |
| | 12 | 20 | | private static readonly Dictionary<EntityId, bool> temporaryCollectStackTraces = new Dictionary<EntityId, bool>( |
| | | 21 | | |
| | | 22 | | private static UserSettingsData? data; |
| | | 23 | | |
| | 12 | 24 | | private static readonly string dataPath = |
| | | 25 | | Path.GetFullPath(Application.dataPath + "/../UserSettings/Packages/se.hertzole.scriptable-values/UserSetting |
| | | 26 | | |
| | | 27 | | private static UserSettingsData Data |
| | | 28 | | { |
| | | 29 | | get |
| | 6774 | 30 | | { |
| | 6774 | 31 | | if (data.HasValue) |
| | 4770 | 32 | | { |
| | 4770 | 33 | | return data.Value; |
| | | 34 | | } |
| | | 35 | | |
| | 2004 | 36 | | MakeSurePathExists(); |
| | | 37 | | |
| | 2004 | 38 | | if (!File.Exists(dataPath)) |
| | 2004 | 39 | | { |
| | 2004 | 40 | | return new UserSettingsData |
| | | 41 | | { |
| | | 42 | | collectStackTraces = false, |
| | | 43 | | collectStackTracesData = new List<CollectStackTracesData>() |
| | | 44 | | }; |
| | | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | string json = File.ReadAllText(dataPath, Encoding.UTF8); |
| | 0 | 48 | | data = JsonUtility.FromJson<UserSettingsData>(json); |
| | 0 | 49 | | return data.Value; |
| | 6774 | 50 | | } |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | public static bool CollectStackTraces |
| | | 54 | | { |
| | 10206 | 55 | | get { return Data.collectStackTraces; } |
| | | 56 | | set |
| | 336 | 57 | | { |
| | 336 | 58 | | if (Data.collectStackTraces == value) |
| | 0 | 59 | | { |
| | 0 | 60 | | return; |
| | | 61 | | } |
| | | 62 | | |
| | 336 | 63 | | UserSettingsData localData = Data; |
| | 336 | 64 | | localData.collectStackTraces = value; |
| | 336 | 65 | | data = localData; |
| | | 66 | | |
| | 336 | 67 | | SaveData(); |
| | 336 | 68 | | } |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | public static bool GetCollectStackTraces(Object obj) |
| | 44031 | 72 | | { |
| | 44031 | 73 | | string guid = GetKey(obj); |
| | | 74 | | |
| | | 75 | | // If the object is not an asset, we use the temporary dictionary. |
| | 44031 | 76 | | if (string.IsNullOrEmpty(guid)) |
| | 42051 | 77 | | { |
| | 42051 | 78 | | return temporaryCollectStackTraces.GetValueOrDefault(GetEntityId(obj), false); |
| | | 79 | | } |
| | | 80 | | |
| | 1980 | 81 | | if (Data.TryGetCollectStackTraces(guid, out bool value)) |
| | 0 | 82 | | { |
| | 0 | 83 | | return value; |
| | | 84 | | } |
| | | 85 | | |
| | 1980 | 86 | | return false; |
| | 44031 | 87 | | } |
| | | 88 | | |
| | | 89 | | public static void SetCollectStackTraces(Object obj, bool value) |
| | 192 | 90 | | { |
| | 192 | 91 | | string guid = GetKey(obj); |
| | | 92 | | |
| | | 93 | | // If the object is not an asset, we use the temporary dictionary. |
| | 192 | 94 | | if (string.IsNullOrEmpty(guid)) |
| | 192 | 95 | | { |
| | 192 | 96 | | temporaryCollectStackTraces[GetEntityId(obj)] = value; |
| | 192 | 97 | | } |
| | | 98 | | |
| | 192 | 99 | | Data.SetCollectStackTraces(guid, value); |
| | 192 | 100 | | SaveData(); |
| | 192 | 101 | | } |
| | | 102 | | |
| | | 103 | | private static string GetKey(Object obj) |
| | 44223 | 104 | | { |
| | 44223 | 105 | | return AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(obj)); |
| | 44223 | 106 | | } |
| | | 107 | | |
| | | 108 | | private static void SaveData() |
| | 528 | 109 | | { |
| | 528 | 110 | | MakeSurePathExists(); |
| | | 111 | | |
| | 528 | 112 | | string json = JsonUtility.ToJson(Data, true); |
| | 528 | 113 | | File.WriteAllText(dataPath, json, Encoding.UTF8); |
| | 528 | 114 | | } |
| | | 115 | | |
| | | 116 | | private static void MakeSurePathExists() |
| | 2532 | 117 | | { |
| | 2532 | 118 | | string directoryPath = Path.GetDirectoryName(dataPath); |
| | | 119 | | |
| | 2532 | 120 | | if (!Directory.Exists(directoryPath)) |
| | 6 | 121 | | { |
| | 6 | 122 | | Directory.CreateDirectory(directoryPath!); |
| | 6 | 123 | | } |
| | 2532 | 124 | | } |
| | | 125 | | |
| | | 126 | | private static EntityId GetEntityId(Object obj) |
| | 42243 | 127 | | { |
| | | 128 | | #if UNITY_6000_4_OR_NEWER |
| | 14258 | 129 | | return obj.GetEntityId(); |
| | | 130 | | #else |
| | 27985 | 131 | | return obj.GetInstanceID(); |
| | | 132 | | #endif |
| | 42243 | 133 | | } |
| | | 134 | | |
| | | 135 | | [Serializable] |
| | | 136 | | public struct UserSettingsData : IEquatable<UserSettingsData> |
| | | 137 | | { |
| | | 138 | | public bool collectStackTraces; |
| | | 139 | | public List<CollectStackTracesData> collectStackTracesData; |
| | | 140 | | |
| | | 141 | | public bool TryGetCollectStackTraces(string key, out bool value) |
| | 1980 | 142 | | { |
| | 3960 | 143 | | for (int i = 0; i < collectStackTracesData.Count; i++) |
| | 0 | 144 | | { |
| | 0 | 145 | | if (collectStackTracesData[i].key == key) |
| | 0 | 146 | | { |
| | 0 | 147 | | value = collectStackTracesData[i].value; |
| | 0 | 148 | | return true; |
| | | 149 | | } |
| | 0 | 150 | | } |
| | | 151 | | |
| | 1980 | 152 | | value = false; |
| | 1980 | 153 | | return false; |
| | 1980 | 154 | | } |
| | | 155 | | |
| | | 156 | | public void SetCollectStackTraces(string key, bool value) |
| | 192 | 157 | | { |
| | 384 | 158 | | for (int i = 0; i < collectStackTracesData.Count; i++) |
| | 186 | 159 | | { |
| | 186 | 160 | | if (collectStackTracesData[i].key == key) |
| | 186 | 161 | | { |
| | 186 | 162 | | collectStackTracesData[i] = new CollectStackTracesData { key = key, value = value }; |
| | 186 | 163 | | return; |
| | | 164 | | } |
| | 0 | 165 | | } |
| | | 166 | | |
| | 6 | 167 | | collectStackTracesData.Add(new CollectStackTracesData { key = key, value = value }); |
| | 192 | 168 | | } |
| | | 169 | | |
| | | 170 | | /// <inheritdoc /> |
| | | 171 | | public bool Equals(UserSettingsData other) |
| | 0 | 172 | | { |
| | 0 | 173 | | if (collectStackTraces != other.collectStackTraces || collectStackTracesData.Count != other.collectStack |
| | 0 | 174 | | { |
| | 0 | 175 | | return false; |
| | | 176 | | } |
| | | 177 | | |
| | 0 | 178 | | for (int i = 0; i < collectStackTracesData.Count; i++) |
| | 0 | 179 | | { |
| | 0 | 180 | | if (!collectStackTracesData[i].Equals(other.collectStackTracesData[i])) |
| | 0 | 181 | | { |
| | 0 | 182 | | return false; |
| | | 183 | | } |
| | 0 | 184 | | } |
| | | 185 | | |
| | 0 | 186 | | return true; |
| | 0 | 187 | | } |
| | | 188 | | |
| | | 189 | | /// <inheritdoc /> |
| | | 190 | | public override bool Equals(object obj) |
| | 0 | 191 | | { |
| | 0 | 192 | | return obj is UserSettingsData other && Equals(other); |
| | 0 | 193 | | } |
| | | 194 | | |
| | | 195 | | /// <inheritdoc /> |
| | | 196 | | public override int GetHashCode() |
| | 0 | 197 | | { |
| | | 198 | | unchecked |
| | 0 | 199 | | { |
| | 0 | 200 | | return (collectStackTraces.GetHashCode() * 397) ^ (collectStackTracesData != null ? collectStackTrac |
| | | 201 | | } |
| | 0 | 202 | | } |
| | | 203 | | |
| | | 204 | | public static bool operator ==(UserSettingsData left, UserSettingsData right) |
| | 0 | 205 | | { |
| | 0 | 206 | | return left.Equals(right); |
| | 0 | 207 | | } |
| | | 208 | | |
| | | 209 | | public static bool operator !=(UserSettingsData left, UserSettingsData right) |
| | 0 | 210 | | { |
| | 0 | 211 | | return !left.Equals(right); |
| | 0 | 212 | | } |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | [Serializable] |
| | | 216 | | public struct CollectStackTracesData : IEquatable<CollectStackTracesData> |
| | | 217 | | { |
| | | 218 | | public string key; |
| | | 219 | | public bool value; |
| | | 220 | | |
| | | 221 | | /// <inheritdoc /> |
| | | 222 | | public bool Equals(CollectStackTracesData other) |
| | 0 | 223 | | { |
| | 0 | 224 | | return key == other.key && value == other.value; |
| | 0 | 225 | | } |
| | | 226 | | |
| | | 227 | | /// <inheritdoc /> |
| | | 228 | | public override bool Equals(object obj) |
| | 0 | 229 | | { |
| | 0 | 230 | | return obj is CollectStackTracesData other && Equals(other); |
| | 0 | 231 | | } |
| | | 232 | | |
| | | 233 | | /// <inheritdoc /> |
| | | 234 | | public override int GetHashCode() |
| | 0 | 235 | | { |
| | | 236 | | unchecked |
| | 0 | 237 | | { |
| | 0 | 238 | | return ((key != null ? key.GetHashCode() : 0) * 397) ^ value.GetHashCode(); |
| | | 239 | | } |
| | 0 | 240 | | } |
| | | 241 | | |
| | | 242 | | public static bool operator ==(CollectStackTracesData left, CollectStackTracesData right) |
| | 0 | 243 | | { |
| | 0 | 244 | | return left.Equals(right); |
| | 0 | 245 | | } |
| | | 246 | | |
| | | 247 | | public static bool operator !=(CollectStackTracesData left, CollectStackTracesData right) |
| | 0 | 248 | | { |
| | 0 | 249 | | return !left.Equals(right); |
| | 0 | 250 | | } |
| | | 251 | | } |
| | | 252 | | } |
| | | 253 | | } |
| | | 254 | | #endif |