-
Notifications
You must be signed in to change notification settings - Fork 7
/
VolumeConfigsLibrary.cs
183 lines (168 loc) · 6.48 KB
/
VolumeConfigsLibrary.cs
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// VolumeConfigsLibrary.cs
//
// Author:
// Allis Tauri <[email protected]>
//
// Copyright (c) 2016 Allis Tauri
using System.Collections.Generic;
using System.Linq;
namespace AT_Utils
{
public static class VolumeConfigsLibrary
{
public const string USER_FILE = "VolumeConfigs.user";
private static SortedList<string, VolumeConfiguration> presets;
private static SortedList<string, VolumeConfiguration> user_configs;
public static string UserFile => CustomConfig.GameDataFolder("ConfigurableContainers", USER_FILE);
/// <summary>
/// The library of tank configurations provided by mods.
/// </summary>
public static SortedList<string, VolumeConfiguration> PresetConfigs
{
get
{
if(presets != null)
return presets;
var nodes = GameDatabase.Instance.GetConfigNodes(VolumeConfiguration.NODE_NAME);
presets = new SortedList<string, VolumeConfiguration>(nodes.Length);
foreach(var n in nodes)
{
Utils.Debug("Parsing preset tank configuration:\n{}", n);
var cfg = ConfigNodeObject.FromConfig<VolumeConfiguration>(n);
if(!cfg.Valid)
{
var msg = $"ConfigurableContainers: configuration \"{cfg.name}\" is INVALID.";
Utils.Message(6, msg);
Utils.Error(msg);
continue;
}
try
{
presets.Add(cfg.name, cfg);
}
catch
{
Utils.Warning("SwitchableTankType: ignoring duplicate configuration of '{}' configuration. "
+ "Use ModuleManager to change the existing one.",
cfg.name);
}
}
Utils.Debug("Parsed presets: {}", presets);
return presets;
}
}
/// <summary>
/// The library of tank configurations saved by the user.
/// </summary>
/// <value>The user configs.</value>
public static SortedList<string, VolumeConfiguration> UserConfigs
{
get
{
if(user_configs != null)
return user_configs;
user_configs = new SortedList<string, VolumeConfiguration>();
var node = CustomConfig.LoadNode(UserFile);
Utils.Debug("Loading user configurations from:\n{}\n{}", UserFile, node);
if(node == null)
return user_configs;
foreach(var n in node.GetNodes(VolumeConfiguration.NODE_NAME))
{
var cfg = ConfigNodeObject.FromConfig<VolumeConfiguration>(n);
if(!cfg.Valid)
{
var msg = $"ConfigurableContainers: configuration \"{cfg.name}\" is INVALID.";
Utils.Message(6, msg);
Utils.Error(msg);
continue;
}
if(SwitchableTankType.HaveTankType(cfg.name))
cfg.name += " [cfg]";
if(PresetConfigs.ContainsKey(cfg.name))
cfg.name += " [usr]";
add_unique(cfg, user_configs);
}
Utils.Debug("Parsed user presets: {}", user_configs);
return user_configs;
}
}
private static void add_unique(VolumeConfiguration cfg, IDictionary<string, VolumeConfiguration> db)
{
var index = 1;
var basename = cfg.name;
while(db.ContainsKey(cfg.name))
cfg.name = string.Concat(basename, " ", index++);
db.Add(cfg.name, cfg);
}
private static void save_user_configs()
{
var node = new ConfigNode();
UserConfigs.ForEach(c => c.Value.SaveInto(node));
if(CustomConfig.SaveNode(node, UserFile))
return;
Utils.Message("Unable to save tank configurations.");
}
public static void AddConfig(VolumeConfiguration cfg)
{
add_unique(cfg, UserConfigs);
save_user_configs();
}
public static void AddOrSave(VolumeConfiguration cfg)
{
if(UserConfigs.ContainsKey(cfg.name))
UserConfigs[cfg.name] = cfg;
else
UserConfigs.Add(cfg.name, cfg);
save_user_configs();
}
public static bool RemoveConfig(string cfg_name)
{
if(!UserConfigs.Remove(cfg_name))
return false;
save_user_configs();
return true;
}
public static List<string> AllConfigNames(string[] include, string[] exclude)
{
var names = new List<string>();
if(include != null && include.Length > 0)
exclude = SwitchableTankType.TankTypeNames(null, include).ToArray();
if(exclude != null && exclude.Length > 0)
{
names.AddRange(
from cfg in PresetConfigs
where cfg.Value.ContainsTypes(exclude)
select cfg.Value.name);
names.AddRange(
from cfg in UserConfigs
where cfg.Value.ContainsTypes(exclude)
select cfg.Value.name);
}
else
{
names.AddRange(PresetConfigs.Keys);
names.AddRange(UserConfigs.Keys);
}
return names;
}
public static VolumeConfiguration GetConfig(string name)
{
if(string.IsNullOrEmpty(name))
return null;
if(PresetConfigs.TryGetValue(name, out var cfg))
return cfg;
if(UserConfigs.TryGetValue(name, out cfg))
return cfg;
return null;
}
public static string GetConfigInfo(string name, float volume_conversion = 1)
{
var config = GetConfig(name);
return config == null ? string.Empty : config.Info(volume_conversion);
}
public static bool HaveUserConfig(string name)
{
return UserConfigs.ContainsKey(name);
}
}
}