forked from Biotronic/TweakScale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScaleConfig.cs
176 lines (153 loc) · 6.41 KB
/
ScaleConfig.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
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace TweakScale
{
[KSPAddon(KSPAddon.Startup.EditorAny, false)]
public class TechUpdater : MonoBehaviour
{
public void Start()
{
Tech.Reload();
}
}
public static class Tech
{
private static HashSet<string> unlockedTechs = new HashSet<string>();
public static void Reload()
{
if (HighLogic.CurrentGame == null)
return;
if (HighLogic.CurrentGame.Mode != Game.Modes.CAREER)
return;
string persistentfile = KSPUtil.ApplicationRootPath + "saves/" + HighLogic.SaveFolder + "/persistent.sfs";
ConfigNode config = ConfigNode.Load(persistentfile);
ConfigNode gameconf = config.GetNode("GAME");
ConfigNode[] scenarios = gameconf.GetNodes("SCENARIO");
ConfigNode thisScenario = scenarios.FirstOrDefault(a => a.GetValue("name") == "ResearchAndDevelopment");
ConfigNode[] techs = thisScenario.GetNodes("Tech");
unlockedTechs = techs.Select(a => a.GetValue("id")).ToHashSet();
unlockedTechs.Add("");
}
public static bool IsUnlocked(string techId)
{
if (HighLogic.CurrentGame == null)
return true;
if (HighLogic.CurrentGame.Mode != Game.Modes.CAREER)
return true;
if (techId == "")
return true;
return unlockedTechs.Contains(techId);
}
}
/// <summary>
/// Configuration values for TweakScale.
/// </summary>
public class ScaleConfig
{
/// <summary>
/// Fetches the scale config with the specified name.
/// </summary>
/// <param name="name">The name of the config to fetch.</param>
/// <returns>The specified config or the default config if none exists by that name.</returns>
private static ScaleConfig GetScaleConfig(string name)
{
var config = GameDatabase.Instance.GetConfigs("SCALETYPE").FirstOrDefault(a => a.name == name);
if (config == null && name != "default")
{
Tools.LogWf("No SCALETYPE with name {0}", name);
}
return (object)config == null ? defaultConfig : new ScaleConfig(config.config);
}
private static ScaleConfig[] configs;
public static ScaleConfig[] AllConfigs
{
get
{
if (configs == null)
{
configs = GameDatabase.Instance.GetConfigs("SCALETYPE").Select(a => new ScaleConfig(a.config)).ToArray();
}
return configs;
}
}
private static ScaleConfig defaultConfig = new ScaleConfig();
private float[] _scaleFactors = { 0.625f, 1.25f, 2.5f, 3.75f, 5f };
private string[] _scaleNames = { "62.5cm", "1.25m", "2.5m", "3.75m", "5m" };
public Dictionary<string, ScaleExponents> exponents = new Dictionary<string, ScaleExponents>();
public bool isFreeScale = false;
public string[] techRequired = { "", "", "", "", "" };
public float minValue = 0.625f;
public float maxValue = 5.0f;
public float defaultScale = 1.25f;
public string suffix = "m";
public string name;
public float[] allScaleFactors
{
get
{
return _scaleFactors;
}
}
public float[] scaleFactors
{
get
{
var result = _scaleFactors.ZipFilter(techRequired, Tech.IsUnlocked).ToArray();
return result;
}
}
public string[] scaleNames
{
get
{
var result = _scaleNames.ZipFilter(techRequired, Tech.IsUnlocked).ToArray();
return result;
}
}
private ScaleConfig()
{
}
public ScaleConfig(ConfigNode config)
{
if ((object)config == null || Tools.ConfigValue(config, "name", "default") == "default")
{
return; // Default values.
}
var type = Tools.ConfigValue(config, "type", "default");
var source = GetScaleConfig(type);
isFreeScale = Tools.ConfigValue(config, "freeScale", defaultValue: source.isFreeScale);
minValue = Tools.ConfigValue(config, "minScale", defaultValue: source.minValue);
maxValue = Tools.ConfigValue(config, "maxScale", defaultValue: source.maxValue);
suffix = Tools.ConfigValue(config, "suffix", defaultValue: source.suffix);
_scaleFactors = Tools.ConfigValue(config, "scaleFactors", defaultValue: source._scaleFactors);
_scaleNames = Tools.ConfigValue(config, "scaleNames", defaultValue: source._scaleNames).Select(a => a.Trim()).ToArray();
techRequired = Tools.ConfigValue(config, "techRequired", defaultValue: source.techRequired).Select(a=>a.Trim()).ToArray();
name = Tools.ConfigValue(config, "name", defaultValue: "unnamed scaletype");
if (_scaleFactors.Length != _scaleNames.Length)
{
Tools.LogWf("Wrong number of scaleFactors compared to scaleNames: {0} vs {1}", _scaleFactors.Length, _scaleNames.Length);
}
if (techRequired.Length < _scaleFactors.Length)
{
techRequired = techRequired.Concat("".Repeat()).Take(_scaleFactors.Length).ToArray();
}
var tmpScale = Tools.ConfigValue(config, "defaultScale", defaultValue: source.defaultScale);
if (!isFreeScale)
{
tmpScale = Tools.Closest(tmpScale, allScaleFactors);
}
defaultScale = Tools.clamp(tmpScale, minValue, maxValue);
exponents = ScaleExponents.CreateExponentsForModule(config, source.exponents);
}
public override string ToString()
{
string result = "ScaleConfig {\n";
result += " isFreeScale = " + isFreeScale.ToString() + "\n";
result += " scaleFactors = " + scaleFactors.ToString() + "\n";
result += " minValue = " + minValue.ToString() + "\n";
result += " maxValue = " + maxValue.ToString() + "\n";
return result + "}";
}
}
}