-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPrimerConfig.cs
More file actions
96 lines (86 loc) · 3.03 KB
/
PrimerConfig.cs
File metadata and controls
96 lines (86 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.IO;
using System.Text.Json;
using Godot;
namespace GladiatorManager.addons.PrimerTools;
public class PrimerConfig
{
// Just a color palette handler at this point
private const string ConfigPath = "res://primer_config.json";
public ColorPalette Colors { get; set; }
public PrimerConfig()
{
Colors = new ColorPalette();
}
public class ColorPalette
{
public Color Blue { get; set; } = Godot.Colors.DodgerBlue;
public Color Red { get; set; } = Godot.Colors.Crimson;
public Color Green { get; set; } = Godot.Colors.ForestGreen;
public Color Orange { get; set; } = Godot.Colors.DarkOrange;
public Color Purple { get; set; } = Godot.Colors.DarkOrchid;
public Color Yellow { get; set; } = Godot.Colors.Gold;
public Color Gray { get; set; } = Godot.Colors.Gray;
public Color LightGray { get; set; } = Godot.Colors.LightGray;
public Color White { get; set; } = Godot.Colors.White;
public Color Black { get; set; } = Godot.Colors.Black;
}
private static PrimerConfig _instance;
public static PrimerConfig Instance
{
get
{
if (_instance == null)
{
LoadConfig();
}
return _instance;
}
}
private static void LoadConfig(string configPath = ConfigPath)
{
// It's convenient to comment this out when iterating on the palette
// using the default colors above.
// Otherwise, you have to delete the config file between changes.
// return new PrimerConfig();
configPath = ProjectSettings.GlobalizePath(configPath);
try
{
// Try to load custom config
string jsonString = File.ReadAllText(configPath);
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
_instance = JsonSerializer.Deserialize<PrimerConfig>(jsonString, options);
}
catch (FileNotFoundException ex)
{
GD.PushWarning($"Config file not found at: {configPath}. Creating default config.", ex);
_instance = new PrimerConfig();
SaveConfig();
}
catch (JsonException ex)
{
throw new JsonException($"Error parsing JSON from file: {configPath}", ex);
}
}
public static void SaveConfig(string configPath = ConfigPath)
{
configPath = ProjectSettings.GlobalizePath(configPath);
try
{
var options = new JsonSerializerOptions
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
var jsonString = JsonSerializer.Serialize(Instance, options);
File.WriteAllText(configPath, jsonString);
}
catch (Exception ex)
{
throw new Exception($"Failed to save config to {configPath}: {ex.Message}", ex);
}
}
}