| | 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 ConsoleVariableSettings |
| | 13 | | { |
| 2 | 14 | | static StringKeyDictionary<string> s_ParsedValues = new StringKeyDictionary<string>(50); |
| | 15 | |
|
| | 16 | | static bool s_HasReadFile; |
| | 17 | |
|
| | 18 | | public static void SaveToFile() |
| 0 | 19 | | { |
| 0 | 20 | | int count = Console.GetVariableCount(); |
| 0 | 21 | | TextGenerator textGenerator = new TextGenerator(); |
| 0 | 22 | | string[] knownVariables = Console.GetVariableNamesCopy(); |
| 0 | 23 | | textGenerator.AppendLine("# Generated file of Console Variable (CVAR) settings."); |
| 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 | | ConsoleVariableBase variable = Console.GetVariable(knownVariables[i]); |
| 0 | 30 | | if (variable.GetFlags().HasFlags(ConsoleVariableBase.ConsoleVariableFlags.Setting)) |
| 0 | 31 | | { |
| 0 | 32 | | textGenerator.AppendLine($"{variable.GetName()} {variable.GetCurrentValueAsString()}"); |
| 0 | 33 | | } |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | try |
| 0 | 37 | | { |
| 0 | 38 | | File.WriteAllTextAsync(GetConsoleVariableSaveFile(), textGenerator.ToString()); |
| 0 | 39 | | } |
| 0 | 40 | | catch (Exception) |
| 0 | 41 | | { |
| | 42 | | // ignored |
| 0 | 43 | | } |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | public static bool TryGetValue(string name, out string foundValue) |
| 0 | 47 | | { |
| 0 | 48 | | if (!s_HasReadFile) |
| 0 | 49 | | { |
| 0 | 50 | | UpdateFromFile(); |
| 0 | 51 | | s_HasReadFile = true; |
| 0 | 52 | | } |
| | 53 | |
|
| 0 | 54 | | if (s_ParsedValues.ContainsKey(name)) |
| 0 | 55 | | { |
| 0 | 56 | | foundValue = s_ParsedValues[name]; |
| 0 | 57 | | return true; |
| | 58 | | } |
| | 59 | |
|
| 0 | 60 | | foundValue = null; |
| 0 | 61 | | return false; |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | public static void UpdateFromFile() |
| 7 | 65 | | { |
| | 66 | | // Read in the settings from the cvars saved file |
| 7 | 67 | | string settingsFile = GetConsoleVariableSaveFile(); |
| 7 | 68 | | if (!File.Exists(settingsFile)) |
| 7 | 69 | | { |
| 7 | 70 | | return; |
| | 71 | | } |
| | 72 | |
|
| 0 | 73 | | string[] lines = File.ReadAllLines(settingsFile); |
| 0 | 74 | | int lineCount = lines.Length; |
| 0 | 75 | | for (int i = 0; i < lineCount; i++) |
| 0 | 76 | | { |
| 0 | 77 | | string line = lines[i].Trim(); |
| 0 | 78 | | if ( string.IsNullOrEmpty(line) || line[0] == '#') // Skip comments |
| 0 | 79 | | { |
| 0 | 80 | | continue; |
| | 81 | | } |
| | 82 | |
|
| | 83 | | // Update settings cache |
| 0 | 84 | | string[] split = line.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries); |
| 0 | 85 | | if (!s_ParsedValues.ContainsKey(split[0])) |
| 0 | 86 | | { |
| 0 | 87 | | s_ParsedValues.AddWithExpandCheck(split[0], split[1]); |
| 0 | 88 | | } |
| | 89 | | else |
| 0 | 90 | | { |
| 0 | 91 | | s_ParsedValues[split[0]] = split[1]; |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | // Update existing initialized |
| 0 | 95 | | if (Console.HasVariable(split[0])) |
| 0 | 96 | | { |
| 0 | 97 | | Console.GetVariable(split[0]).SetValueFromString(split[1]); |
| 0 | 98 | | } |
| 0 | 99 | | } |
| 7 | 100 | | } |
| | 101 | |
|
| | 102 | | static string GetConsoleVariableSaveFile() |
| 7 | 103 | | { |
| 7 | 104 | | return Path.Combine(Platform.GetOutputFolder(), "cvars.gdx"); |
| 7 | 105 | | } |
| | 106 | | } |
| | 107 | | #endif // UNITY_2022_2_OR_NEWER |
| | 108 | | } |