< Summary

Information
Class: Hertzole.ScriptableValues.Debugging.UserSettings
Assembly: Hertzole.ScriptableValues
File(s): ./Packages/se.hertzole.scriptable-values/Runtime/Debugging/UserSettings.cs
Tag: 52_28197466029
Line coverage
55%
Covered lines: 69
Uncovered lines: 55
Coverable lines: 124
Total lines: 254
Line coverage: 55.6%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UserSettings()0%110100%
GetCollectStackTraces(...)0%3.073080%
SetCollectStackTraces(...)0%220100%
GetKey(...)0%110100%
SaveData()0%110100%
MakeSurePathExists()0%220100%
GetEntityId(...)0%110100%
GetEntityId(...)0%110100%
TryGetCollectStackTraces(...)0%4.413046.15%
SetCollectStackTraces(...)0%3.043083.33%
Equals(...)0%30500%
Equals(...)0%6200%
GetHashCode()0%12300%
operator==(...)0%2100%
operator!=(...)0%2100%
Equals(...)0%6200%
Equals(...)0%6200%
GetHashCode()0%12300%
operator==(...)0%2100%
operator!=(...)0%2100%

File(s)

./Packages/se.hertzole.scriptable-values/Runtime/Debugging/UserSettings.cs

#LineLine coverage
 1#if UNITY_EDITOR
 2using System;
 3using System.Collections.Generic;
 4using System.IO;
 5using System.Text;
 6using UnityEditor;
 7using UnityEngine;
 8using Object = UnityEngine.Object;
 9#if UNITY_6000_4_OR_NEWER
 10using EntityId = UnityEngine.EntityId;
 11#else
 12using EntityId = System.Int32;
 13#endif
 14
 15namespace Hertzole.ScriptableValues.Debugging
 16{
 17    internal static class UserSettings
 18    {
 19        // Used to keep track of temporary objects, like objects used in testing.
 1220        private static readonly Dictionary<EntityId, bool> temporaryCollectStackTraces = new Dictionary<EntityId, bool>(
 21
 22        private static UserSettingsData? data;
 23
 1224        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
 677430            {
 677431                if (data.HasValue)
 477032                {
 477033                    return data.Value;
 34                }
 35
 200436                MakeSurePathExists();
 37
 200438                if (!File.Exists(dataPath))
 200439                {
 200440                    return new UserSettingsData
 41                    {
 42                        collectStackTraces = false,
 43                        collectStackTracesData = new List<CollectStackTracesData>()
 44                    };
 45                }
 46
 047                string json = File.ReadAllText(dataPath, Encoding.UTF8);
 048                data = JsonUtility.FromJson<UserSettingsData>(json);
 049                return data.Value;
 677450            }
 51        }
 52
 53        public static bool CollectStackTraces
 54        {
 1020655            get { return Data.collectStackTraces; }
 56            set
 33657            {
 33658                if (Data.collectStackTraces == value)
 059                {
 060                    return;
 61                }
 62
 33663                UserSettingsData localData = Data;
 33664                localData.collectStackTraces = value;
 33665                data = localData;
 66
 33667                SaveData();
 33668            }
 69        }
 70
 71        public static bool GetCollectStackTraces(Object obj)
 4403172        {
 4403173            string guid = GetKey(obj);
 74
 75            // If the object is not an asset, we use the temporary dictionary.
 4403176            if (string.IsNullOrEmpty(guid))
 4205177            {
 4205178                return temporaryCollectStackTraces.GetValueOrDefault(GetEntityId(obj), false);
 79            }
 80
 198081            if (Data.TryGetCollectStackTraces(guid, out bool value))
 082            {
 083                return value;
 84            }
 85
 198086            return false;
 4403187        }
 88
 89        public static void SetCollectStackTraces(Object obj, bool value)
 19290        {
 19291            string guid = GetKey(obj);
 92
 93            // If the object is not an asset, we use the temporary dictionary.
 19294            if (string.IsNullOrEmpty(guid))
 19295            {
 19296                temporaryCollectStackTraces[GetEntityId(obj)] = value;
 19297            }
 98
 19299            Data.SetCollectStackTraces(guid, value);
 192100            SaveData();
 192101        }
 102
 103        private static string GetKey(Object obj)
 44223104        {
 44223105            return AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(obj));
 44223106        }
 107
 108        private static void SaveData()
 528109        {
 528110            MakeSurePathExists();
 111
 528112            string json = JsonUtility.ToJson(Data, true);
 528113            File.WriteAllText(dataPath, json, Encoding.UTF8);
 528114        }
 115
 116        private static void MakeSurePathExists()
 2532117        {
 2532118            string directoryPath = Path.GetDirectoryName(dataPath);
 119
 2532120            if (!Directory.Exists(directoryPath))
 6121            {
 6122                Directory.CreateDirectory(directoryPath!);
 6123            }
 2532124        }
 125
 126        private static EntityId GetEntityId(Object obj)
 42243127        {
 128#if UNITY_6000_4_OR_NEWER
 14258129            return obj.GetEntityId();
 130#else
 27985131            return obj.GetInstanceID();
 132#endif
 42243133        }
 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)
 1980142            {
 3960143                for (int i = 0; i < collectStackTracesData.Count; i++)
 0144                {
 0145                    if (collectStackTracesData[i].key == key)
 0146                    {
 0147                        value = collectStackTracesData[i].value;
 0148                        return true;
 149                    }
 0150                }
 151
 1980152                value = false;
 1980153                return false;
 1980154            }
 155
 156            public void SetCollectStackTraces(string key, bool value)
 192157            {
 384158                for (int i = 0; i < collectStackTracesData.Count; i++)
 186159                {
 186160                    if (collectStackTracesData[i].key == key)
 186161                    {
 186162                        collectStackTracesData[i] = new CollectStackTracesData { key = key, value = value };
 186163                        return;
 164                    }
 0165                }
 166
 6167                collectStackTracesData.Add(new CollectStackTracesData { key = key, value = value });
 192168            }
 169
 170            /// <inheritdoc />
 171            public bool Equals(UserSettingsData other)
 0172            {
 0173                if (collectStackTraces != other.collectStackTraces || collectStackTracesData.Count != other.collectStack
 0174                {
 0175                    return false;
 176                }
 177
 0178                for (int i = 0; i < collectStackTracesData.Count; i++)
 0179                {
 0180                    if (!collectStackTracesData[i].Equals(other.collectStackTracesData[i]))
 0181                    {
 0182                        return false;
 183                    }
 0184                }
 185
 0186                return true;
 0187            }
 188
 189            /// <inheritdoc />
 190            public override bool Equals(object obj)
 0191            {
 0192                return obj is UserSettingsData other && Equals(other);
 0193            }
 194
 195            /// <inheritdoc />
 196            public override int GetHashCode()
 0197            {
 198                unchecked
 0199                {
 0200                    return (collectStackTraces.GetHashCode() * 397) ^ (collectStackTracesData != null ? collectStackTrac
 201                }
 0202            }
 203
 204            public static bool operator ==(UserSettingsData left, UserSettingsData right)
 0205            {
 0206                return left.Equals(right);
 0207            }
 208
 209            public static bool operator !=(UserSettingsData left, UserSettingsData right)
 0210            {
 0211                return !left.Equals(right);
 0212            }
 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)
 0223            {
 0224                return key == other.key && value == other.value;
 0225            }
 226
 227            /// <inheritdoc />
 228            public override bool Equals(object obj)
 0229            {
 0230                return obj is CollectStackTracesData other && Equals(other);
 0231            }
 232
 233            /// <inheritdoc />
 234            public override int GetHashCode()
 0235            {
 236                unchecked
 0237                {
 0238                    return ((key != null ? key.GetHashCode() : 0) * 397) ^ value.GetHashCode();
 239                }
 0240            }
 241
 242            public static bool operator ==(CollectStackTracesData left, CollectStackTracesData right)
 0243            {
 0244                return left.Equals(right);
 0245            }
 246
 247            public static bool operator !=(CollectStackTracesData left, CollectStackTracesData right)
 0248            {
 0249                return !left.Equals(right);
 0250            }
 251        }
 252    }
 253}
 254#endif