• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

luttje / Key2Joy / 6724165891

01 Nov 2023 07:19PM UTC coverage: 43.795% (+0.1%) from 43.687%
6724165891

push

github

luttje
Add ResetMouseMoveTriggerCenter to reset the cursor center for games that move the cursor somewhere after we've armed already

765 of 2411 branches covered (0.0%)

Branch coverage included in aggregate %.

28 of 28 new or added lines in 3 files covered. (100.0%)

3900 of 8241 relevant lines covered (47.32%)

16151.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.56
/Core/Key2Joy.Core/Config/ConfigManager.cs
1
using System.IO;
2
using System.Text.Json;
3
using Key2Joy.Contracts;
4
using Key2Joy.Util;
5

6
namespace Key2Joy.Config;
7

8
/// <summary>
9
/// Manages user configurations being loaded from and saved to disk.
10
/// </summary>
11
public class ConfigManager : IConfigManager
12
{
13
    protected const string CONFIG_PATH = "config.json";
14

15
    public bool IsInitialized { get; private set; }
125✔
16

17
    private ConfigState configState;
18

19
    public ConfigManager()
16✔
20
        => this.LoadOrCreate();
16✔
21

22
    /// <returns>The path to where the config file is located.</returns>
23
    protected virtual string GetAppDataDirectory() => Output.GetAppDataDirectory();
×
24

25
    public ConfigState GetConfigState()
26
        => this.configState;
11✔
27

28
    public void Save()
29
    {
30
        var options = GetSerializerOptions();
26✔
31
        var configPath = Path.Combine(
26✔
32
            this.GetAppDataDirectory(),
26✔
33
            CONFIG_PATH);
26✔
34

35
        File.WriteAllText(configPath, JsonSerializer.Serialize(this.configState, options));
26✔
36
    }
26✔
37

38
    /// <summary>
39
    /// Loads the configuration or creates a default one on disk.
40
    /// </summary>
41
    public void LoadOrCreate()
42
    {
43
        var configPath = Path.Combine(
16✔
44
            this.GetAppDataDirectory(),
16✔
45
            CONFIG_PATH);
16✔
46

47
        this.configState = new ConfigState(this);
16✔
48

49
        if (File.Exists(configPath))
16✔
50
        {
51
            var options = GetSerializerOptions();
11✔
52
            // Merge the loaded config state with the default config state
53
            JsonUtilities.PopulateObject(
11✔
54
                File.ReadAllText(configPath),
11✔
55
                this.configState,
11✔
56
                options
11✔
57
            );
11✔
58
        }
59

60
        var assembly = System.Reflection.Assembly.GetEntryAssembly();
16✔
61

62
        // If the assembly is null then we are running in a unit test
63
        if (assembly == null)
16!
64
        {
65
            this.CompleteInitialization();
16✔
66
            return;
16✔
67
        }
68

69
        var executablePath = assembly.Location;
×
70
        if (executablePath.EndsWith("Key2Joy.exe")
×
71
            && this.configState.LastInstallPath != executablePath)
×
72
        {
73
            this.configState.LastInstallPath = executablePath;
×
74
        }
75

76
        this.CompleteInitialization();
×
77
        return;
×
78
    }
79

80
    private void CompleteInitialization()
81
    {
82
        this.IsInitialized = true;
16✔
83

84
        // We save so old properties are removed and new ones are added to the config file immediately
85
        this.Save();
16✔
86
    }
16✔
87

88
    protected static JsonSerializerOptions GetSerializerOptions()
89
    {
90
        JsonSerializerOptions options = new()
37✔
91
        {
37✔
92
            WriteIndented = true,
37✔
93
        };
37✔
94

95
        return options;
37✔
96
    }
97

98
    /// <summary>
99
    /// Turns the plugin path into a relative one from the app directory
100
    /// </summary>
101
    /// <param name="pluginAssemblyPath"></param>
102
    /// <returns></returns>
103
    protected string NormalizePluginPath(string pluginAssemblyPath)
104
    {
105
        var appDirectory = Path.GetDirectoryName(this.configState.LastInstallPath);
20✔
106
        var pluginPath = Path.GetFullPath(pluginAssemblyPath);
20✔
107
        var relativePath = pluginPath.Replace(appDirectory, string.Empty).TrimStart('\\');
20✔
108

109
        return relativePath;
20✔
110
    }
111

112
    /// <summary>
113
    /// Sets a plugin as enabled, the permissions checksum is stored so no changes to the permissions
114
    /// are accepted when loading the plugin later.
115
    ///
116
    /// Set permissionsChecksumOrNull to null to disable the plugin.
117
    /// </summary>
118
    /// <param name="pluginAssemblyPath"></param>
119
    /// <param name="permissionsChecksumOrNull"></param>
120
    public void SetPluginEnabled(string pluginAssemblyPath, string permissionsChecksumOrNull)
121
    {
122
        pluginAssemblyPath = this.NormalizePluginPath(pluginAssemblyPath);
6✔
123

124
        if (permissionsChecksumOrNull != null)
6✔
125
        {
126
            if (!this.configState.EnabledPlugins.ContainsKey(pluginAssemblyPath))
5✔
127
            {
128
                this.configState.EnabledPlugins.Add(pluginAssemblyPath, permissionsChecksumOrNull);
3✔
129
            }
130
            else
131
            {
132
                this.configState.EnabledPlugins[pluginAssemblyPath] = permissionsChecksumOrNull;
2✔
133
            }
134
        }
135
        else
136
        {
137
            if (this.configState.EnabledPlugins.ContainsKey(pluginAssemblyPath))
1✔
138
            {
139
                this.configState.EnabledPlugins.Remove(pluginAssemblyPath);
1✔
140
            }
141
        }
142

143
        this.Save();
6✔
144
    }
6✔
145

146
    public bool IsPluginEnabled(string pluginAssemblyPath)
147
        => this.configState.EnabledPlugins.ContainsKey(this.NormalizePluginPath(pluginAssemblyPath));
6✔
148

149
    public string GetExpectedPluginChecksum(string pluginAssemblyPath)
150
        => this.configState.EnabledPlugins.ContainsKey(this.NormalizePluginPath(pluginAssemblyPath))
5✔
151
            ? this.configState.EnabledPlugins[this.NormalizePluginPath(pluginAssemblyPath)]
5✔
152
            : null;
5✔
153
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc