| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.IO; |
| | 7 | | using GDX.Collections.Generic; |
| | 8 | |
|
| | 9 | | namespace GDX.Developer |
| | 10 | | { |
| | 11 | | #if UNITY_2022_2_OR_NEWER |
| | 12 | | public static class WatchSettings |
| | 13 | | { |
| 2 | 14 | | static StringKeyDictionary<bool> s_ParsedState = new StringKeyDictionary<bool>(50); |
| | 15 | |
|
| | 16 | | static bool s_HasReadFile; |
| | 17 | |
|
| | 18 | | public static void SaveToFile() |
| 0 | 19 | | { |
| 0 | 20 | | int count = WatchProvider.GetTotalCount(); |
| 0 | 21 | | TextGenerator textGenerator = new TextGenerator(); |
| 0 | 22 | | WatchProvider.WatchList watches = WatchProvider.GetWatchList(); |
| 0 | 23 | | textGenerator.AppendLine("# Generated file of Watches states."); |
| 0 | 24 | | textGenerator.AppendLine("# All non supported content will be removed on save."); |
| 0 | 25 | | textGenerator.AppendLine(""); |
| | 26 | |
|
| 0 | 27 | | for (int i = 0; i < count; i++) |
| 0 | 28 | | { |
| 0 | 29 | | textGenerator.AppendLine(watches.IsActive[i] |
| | 30 | | ? $"{watches.Identfiers[i]} 1" |
| | 31 | | : $"{watches.Identfiers[i]} 0"); |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | | try |
| 0 | 35 | | { |
| 0 | 36 | | File.WriteAllTextAsync(GetWatchesSaveFile(), textGenerator.ToString()); |
| 0 | 37 | | } |
| 0 | 38 | | catch (Exception) |
| 0 | 39 | | { |
| | 40 | | // ignored |
| 0 | 41 | | } |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | public static bool TryGetValue(string name, out bool foundValue) |
| 2 | 45 | | { |
| 2 | 46 | | if (!s_HasReadFile) |
| 2 | 47 | | { |
| 2 | 48 | | UpdateFromFile(); |
| 2 | 49 | | s_HasReadFile = true; |
| 2 | 50 | | } |
| | 51 | |
|
| 2 | 52 | | if (s_ParsedState.ContainsKey(name)) |
| 0 | 53 | | { |
| 0 | 54 | | foundValue = s_ParsedState[name]; |
| 0 | 55 | | return true; |
| | 56 | | } |
| | 57 | |
|
| 2 | 58 | | foundValue = false; |
| 2 | 59 | | return false; |
| 2 | 60 | | } |
| | 61 | |
|
| | 62 | | public static void UpdateFromFile() |
| 9 | 63 | | { |
| | 64 | | // Read in the settings from the cvars saved file |
| 9 | 65 | | string settingsFile = GetWatchesSaveFile(); |
| 9 | 66 | | if (!File.Exists(settingsFile)) |
| 9 | 67 | | { |
| 9 | 68 | | return; |
| | 69 | | } |
| | 70 | |
|
| 0 | 71 | | string[] lines = File.ReadAllLines(settingsFile); |
| 0 | 72 | | int lineCount = lines.Length; |
| 0 | 73 | | for (int i = 0; i < lineCount; i++) |
| 0 | 74 | | { |
| 0 | 75 | | string line = lines[i].Trim(); |
| 0 | 76 | | if ( string.IsNullOrEmpty(line) || line[0] == '#') // Skip comments |
| 0 | 77 | | { |
| 0 | 78 | | continue; |
| | 79 | | } |
| | 80 | |
|
| | 81 | | // Update settings cache |
| 0 | 82 | | string[] split = line.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries); |
| 0 | 83 | | bool value = split[1].IsBooleanPositiveValue(); |
| 0 | 84 | | if (!s_ParsedState.ContainsKey(split[0])) |
| 0 | 85 | | { |
| 0 | 86 | | s_ParsedState.AddWithExpandCheck(split[0], value); |
| 0 | 87 | | } |
| | 88 | | else |
| 0 | 89 | | { |
| 0 | 90 | | s_ParsedState[split[0]] = value; |
| 0 | 91 | | } |
| | 92 | |
|
| 0 | 93 | | WatchBase watch = WatchProvider.GetWatch(split[0]); |
| 0 | 94 | | if (watch != null) |
| 0 | 95 | | { |
| 0 | 96 | | WatchProvider.SetState(watch, value); |
| 0 | 97 | | } |
| 0 | 98 | | } |
| 9 | 99 | | } |
| | 100 | |
|
| | 101 | | static string GetWatchesSaveFile() |
| 9 | 102 | | { |
| 9 | 103 | | return Path.Combine(Platform.GetOutputFolder(), "watches.gdx"); |
| 9 | 104 | | } |
| | 105 | | } |
| | 106 | | #endif // UNITY_2022_2_OR_NEWER |
| | 107 | | } |