< Summary

Class:GDX.Developer.ConsoleVariableSettings
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Developer/ConsoleVariableSettings.cs
Covered lines:10
Uncovered lines:56
Coverable lines:66
Total lines:108
Line coverage:15.1% (10 of 66)
Covered branches:0
Total branches:0
Covered methods:3
Total methods:5
Method coverage:60% (3 of 5)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ConsoleVariableSettings()0%110100%
SaveToFile()0%12300%
TryGetValue(...)0%12300%
UpdateFromFile()0%31.447020.69%
GetConsoleVariableSaveFile()0%110100%

File(s)

./Packages/com.dotbunny.gdx/GDX/Developer/ConsoleVariableSettings.cs

#LineLine coverage
 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
 5using System;
 6using System.IO;
 7using GDX.Collections.Generic;
 8
 9namespace GDX.Developer
 10{
 11#if UNITY_2022_2_OR_NEWER
 12    public static class ConsoleVariableSettings
 13    {
 214        static StringKeyDictionary<string> s_ParsedValues = new StringKeyDictionary<string>(50);
 15
 16        static bool s_HasReadFile;
 17
 18        public static void SaveToFile()
 019        {
 020            int count = Console.GetVariableCount();
 021            TextGenerator textGenerator = new TextGenerator();
 022            string[] knownVariables = Console.GetVariableNamesCopy();
 023            textGenerator.AppendLine("# Generated file of Console Variable (CVAR) settings.");
 024            textGenerator.AppendLine("# All non supported content will be removed on save.");
 025            textGenerator.AppendLine("");
 26
 027            for (int i = 0; i < count; i++)
 028            {
 029                ConsoleVariableBase variable = Console.GetVariable(knownVariables[i]);
 030                if (variable.GetFlags().HasFlags(ConsoleVariableBase.ConsoleVariableFlags.Setting))
 031                {
 032                    textGenerator.AppendLine($"{variable.GetName()} {variable.GetCurrentValueAsString()}");
 033                }
 034            }
 35
 36            try
 037            {
 038                File.WriteAllTextAsync(GetConsoleVariableSaveFile(), textGenerator.ToString());
 039            }
 040            catch (Exception)
 041            {
 42                // ignored
 043            }
 044        }
 45
 46        public static bool TryGetValue(string name, out string foundValue)
 047        {
 048            if (!s_HasReadFile)
 049            {
 050                UpdateFromFile();
 051                s_HasReadFile = true;
 052            }
 53
 054            if (s_ParsedValues.ContainsKey(name))
 055            {
 056                foundValue = s_ParsedValues[name];
 057                return true;
 58            }
 59
 060            foundValue = null;
 061            return false;
 062        }
 63
 64        public static void UpdateFromFile()
 765        {
 66            // Read in the settings from the cvars saved file
 767            string settingsFile = GetConsoleVariableSaveFile();
 768            if (!File.Exists(settingsFile))
 769            {
 770                return;
 71            }
 72
 073            string[] lines = File.ReadAllLines(settingsFile);
 074            int lineCount = lines.Length;
 075            for (int i = 0; i < lineCount; i++)
 076            {
 077                string line = lines[i].Trim();
 078                if ( string.IsNullOrEmpty(line) || line[0] == '#') // Skip comments
 079                {
 080                    continue;
 81                }
 82
 83                // Update settings cache
 084                string[] split = line.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
 085                if (!s_ParsedValues.ContainsKey(split[0]))
 086                {
 087                    s_ParsedValues.AddWithExpandCheck(split[0], split[1]);
 088                }
 89                else
 090                {
 091                    s_ParsedValues[split[0]] = split[1];
 092                }
 93
 94                // Update existing initialized
 095                if (Console.HasVariable(split[0]))
 096                {
 097                    Console.GetVariable(split[0]).SetValueFromString(split[1]);
 098                }
 099            }
 7100        }
 101
 102        static string GetConsoleVariableSaveFile()
 7103        {
 7104            return Path.Combine(Platform.GetOutputFolder(), "cvars.gdx");
 7105        }
 106    }
 107#endif // UNITY_2022_2_OR_NEWER
 108}

Coverage by test methods







Methods/Properties

ConsoleVariableSettings()
SaveToFile()
TryGetValue(System.String, System.String&)
UpdateFromFile()
GetConsoleVariableSaveFile()