-
Notifications
You must be signed in to change notification settings - Fork 22
/
TCAScenario.cs
221 lines (201 loc) · 8.13 KB
/
TCAScenario.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Author:
// Allis Tauri <[email protected]>
//
// Copyright (c) 2015 Allis Tauri
//
// This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.
// To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/
// or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
using System;
using System.Linq;
using System.Collections.Generic;
using AT_Utils;
using AT_Utils.UI;
namespace ThrottleControlledAvionics
{
[KSPScenario(ScenarioCreationOptions.AddToAllGames,
new[]
{
GameScenes.SPACECENTER,
GameScenes.FLIGHT,
GameScenes.EDITOR
})]
public class TCAScenario : ScenarioModule
{
public const string MACROSNAME = "TCA.macro";
public const string NAMED_NODE = "NAMED";
static TCAMacroLibrary macros;
public static TCAMacroLibrary Macros
{
get
{
if(macros == null)
{
macros = new TCAMacroLibrary();
var node = loadNode(Globals.Instance.PluginFolder(MACROSNAME));
if(node != null) macros.Load(node);
}
return macros;
}
}
public static void SaveMacro(TCAMacro macro)
{
Macros.SaveMacro(macro, true);
var node = new ConfigNode();
Macros.Save(node);
node.Save(Globals.Instance.PluginFolder(MACROSNAME));
}
public static VesselConfig VAB_DefaultConfig { get; private set; } = new VesselConfig();
public static VesselConfig SPH_DefaultConfig { get; private set; } = new VesselConfig();
public static SortedList<string, NamedConfig> NamedConfigs = new SortedList<string, NamedConfig>();
public static bool ConfigsLoaded { get; private set; }
public static PathDB Paths = new PathDB();
#region ModuleInfo
public static bool HasTCA { get { return !Globals.Instance.IntegrateIntoCareer || Utils.PartIsPurchased(Globals.TCA_PART); } }
public static bool HavePatchedConics { get; private set; }
public static bool ModuleInstalled { get; private set; }
public static bool HavePersistentRotation { get; private set; }
public static string ModuleStatusString()
{ return HasTCA ? Colors.Good.Tag("<b>Software Installed</b>") : Colors.Danger.Tag("Unavailable"); }
#endregion
#region Runtime Interface
public static NamedConfig NewNamedConfig(string name)
{
if(NamedConfigs.ContainsKey(name)) return null;
var c = new NamedConfig(name);
NamedConfigs[name] = c;
return c;
}
public static NamedConfig GetConfig(string name)
{ return NamedConfigs.ContainsKey(name) ? NamedConfigs[name] : null; }
public static NamedConfig GetConfig(int index)
{ return NamedConfigs.Count > index ? NamedConfigs.Values[index] : null; }
public static bool SaveNamedConfig(string name, VesselConfig config, bool overwrite = false)
{
if(name == string.Empty || //do not allow empty name
NamedConfigs.ContainsKey(name) && !overwrite) return false;
NamedConfigs[name] = NamedConfig.FromVesselConfig(name, config);
return true;
}
public static VesselConfig GetDefaultConfig(EditorFacility facility)
{
return facility == EditorFacility.SPH ?
SPH_DefaultConfig : VAB_DefaultConfig;
}
public static void UpdateDefaultConfig(EditorFacility facility, VesselConfig config)
{
if(facility == EditorFacility.SPH)
SPH_DefaultConfig.Copy(config);
else
VAB_DefaultConfig.Copy(config);
}
#endregion
#region Save/Load
static ConfigNode loadNode(string filepath)
{
var node = ConfigNode.Load(filepath);
if(node == null)
Utils.Log("TCAScenario: Unable to read {}", filepath);
return node;
}
public static void LoadConfigs(ConfigNode node)
{
if(ConfigsLoaded) return;
NamedConfigs.Clear();
foreach(var n in node.GetNodes())
{
if(n.name == NAMED_NODE)
{
foreach(var c in n.GetNodes(NamedConfig.NODE_NAME))
{
var config = new NamedConfig();
config.Load(c);
NamedConfigs[config.Name] = config;
}
}
}
VAB_DefaultConfig.LoadFrom(node, "VAB_DefaultConfig");
SPH_DefaultConfig.LoadFrom(node, "SPH_DefaultConfig");
ConfigsLoaded = true;
}
public static void SaveConfigs(ConfigNode node)
{
var current_vessels = new HashSet<Guid>(HighLogic.CurrentGame.flightState.protoVessels.Select(p => p.vesselID));
var fg = FlightGlobals.fetch;
if(fg != null) fg.vessels.ForEach(v => current_vessels.Add(v.id));
if(NamedConfigs.Count > 0)
{
var n = node.AddNode(NAMED_NODE);
foreach(var c in NamedConfigs.Keys)
NamedConfigs[c].SaveInto(n);
}
VAB_DefaultConfig.SaveInto(node, "VAB_DefaultConfig");
SPH_DefaultConfig.SaveInto(node, "SPH_DefaultConfig");
}
#endregion
public override void OnLoad(ConfigNode node)
{
Globals.Load();
#if DEBUG
//UI = ConfigNodeObjectGUI.FromObject(Globals.Instance);
#endif
LoadConfigs(node);
//navigation paths
var paths = node.GetNode(PathDB.NODE_NAME);
if(paths != null) Paths.Load(paths);
else Paths.Clear();
//patched conics availability
HavePatchedConics = GameVariables.Instance
.GetOrbitDisplayMode(ScenarioUpgradeableFacilities.GetFacilityLevel(SpaceCenterFacility.TrackingStation))
== GameVariables.OrbitDisplayMode.PatchedConics;
//check if MM is successfully installed ModuleTCA in any of the parts
ModuleInstalled = false;
foreach(var p in PartLoader.LoadedPartsList)
{
if(p.partPrefab != null && p.partPrefab.HasModule<ModuleTCA>())
{
ModuleInstalled = true;
break;
}
}
if(!ModuleInstalled) TCAManual.ShowStatus();
//check for PersistentRotation
HavePersistentRotation = AssemblyLoader.loadedAssemblies.FirstOrDefault(a => a.name == Globals.Instance.PersistentRotationName) != null;
}
public override void OnSave(ConfigNode node)
{
SaveConfigs(node);
Paths.SaveInto(node);
}
#if DEBUG
//bool show;
//Rect pos = new Rect();
//public ConfigNodeObjectGUI UI;
//void drawGlobalsUI(int windowID)
//{
// GUILayout.BeginVertical();
// if(GUILayout.Button(show? "Hide" : "Show", Styles.active_button, GUILayout.ExpandWidth(true)))
// show = !show;
// if(show)
// {
// UI.Draw();
// if(GUILayout.Button("Save", Styles.danger_button, GUILayout.ExpandWidth(true)))
// Globals.SaveOverride();
// }
// GUILayout.EndVertical();
// GUIWindowBase.TooltipsAndDragWindow();
//}
//void OnGUI()
//{
// if(Event.current.type != EventType.Layout && Event.current.type != EventType.Repaint) return;
// if(Globals.Instance != null && GUIWindowBase.HUD_enabled)
// {
// Styles.Init();
// pos = GUILayout.Window(GetInstanceID(), pos, drawGlobalsUI, "Globals",
// GUILayout.Width(600), GUILayout.Height(show? 400 : 50))
// .clampToScreen();
// }
//}
#endif
}
}