-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathkOSCustomParameters.cs
More file actions
353 lines (313 loc) · 15.7 KB
/
kOSCustomParameters.cs
File metadata and controls
353 lines (313 loc) · 15.7 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
using KSP.IO;
using System;
using System.Reflection;
using System.Linq;
namespace kOS.Module
{
public class kOSCustomParameters : GameParameters.CustomParameterNode
{
private static kOSCustomParameters instance;
public static kOSCustomParameters Instance
{
get
{
if (instance == null)
{
if (HighLogic.CurrentGame != null)
{
instance = HighLogic.CurrentGame.Parameters.CustomParams<kOSCustomParameters>();
}
}
return instance;
}
}
public const string MIGRATION_DIALOG_TEXT = "Some kOS settings are now tracked seperately per-game " +
"using the stock settings menu. " +
"\n\n" +
"You currently seem to have some of these settings stored in kOS's " +
"global folder, probably because you were using a previous version of kOS in the past. " +
"You can migrate these settings from their global (now unused) location into this game's individual settings if you like. " +
"(Note that the telnet server settings are still kept globally, but everything else has moved.)" +
"\n\n<color=#ffffff>" +
"The new place to adjust the settings in-game is in the <color=#ffff00>\"Difficulty Options\"</color> button " +
"of the in-game settings window. (Press <color=#ffff00>ESC</color>, pick <color=#ffff00>Settings</color>, " +
"then <color=#ffff00>Difficulty Options</color>, then <color=#ffff00>kOS</color> to reach them now.)" +
"\n" +
"The settings are there despite them not really being about \"Difficulty\". " +
"That's just the location where KSP allows mods to make custom parameters." +
"</color>\n\n" +
"Would you like to migrate now?";
[GameParameters.CustomParameterUI("")]
public bool migrated = false;
[GameParameters.CustomIntParameterUI("")]
public int version = 0;
[GameParameters.CustomParameterUI("")]
public bool passedClickThroughCheck = false;
// these values constrain and back the InstructionsPerUpdate property so that it is clamped both in the
// user interface and when set from within a script.
private const int ipuMin = 50;
private const int ipuMax = 2000;
private int instructionsPerUpdate = 200;
[GameParameters.CustomIntParameterUI("Instructions per update", minValue = ipuMin, maxValue = ipuMax,
toolTip = "All CPU's run at a speed that executes up to\n" +
"this many kRISC opcodes per physics 'tick'.")]
public int InstructionsPerUpdate
{
get
{
return instructionsPerUpdate;
}
set
{
instructionsPerUpdate = Math.Max(ipuMin, Math.Min(ipuMax, value));
}
}
[GameParameters.CustomParameterUI("Enable compressed storage",
toolTip = "When storing local volumes' data in the saved game,\n"+
"it will be compressed then base64 encoded.")]
public bool useCompressedPersistence = true;
[GameParameters.CustomParameterUI("Show statistics",
toolTip = "After the outermost program is finished, you will\n" +
"see some profiling output describing how fast it ran.")]
public bool showStatistics = false;
[GameParameters.CustomParameterUI("Start on the archive",
toolTip = "When launching a new ship, or reloading a scene,\n" +
"the default volume will start as 0 instead of 1.")]
public bool startOnArchive = false;
[GameParameters.CustomParameterUI("Obey hide UI toggle",
toolTip = "When you press the \"Hide UI\" button (F2 in default bindings)\n" +
"kOS's terminals will hide themselves too.")]
public bool obeyHideUi = true;
[GameParameters.CustomParameterUI("Enable safe mode",
toolTip = "kOS will throw an error if Infinity or Not-A-Number is the result\n" +
"of any expression. This ensures no such values can ever get\n"+
"passed in to KSP's stock API, which doesn't protect itself against their effects.")]
public bool enableSafeMode = true;
[GameParameters.CustomParameterUI("Audible exceptions",
toolTip = "When kOS throws an error, you hear a sound effect.")]
public bool audibleExceptions = true;
[GameParameters.CustomParameterUI("Verbose exceptions",
toolTip = "When kOS has an error, some error messages have alternative longer\n" +
"paragraph-length descriptions that this enables.")]
public bool verboseExceptions = true;
[GameParameters.CustomParameterUI("Only use Blizzy toolbar",
toolTip = "If you have the \"Blizzy Toolbar\" mod installed, only put the kOS\n" +
"button on it instead of both it and the stock toolbar.")]
public bool useBlizzyToolbarOnly = false;
[GameParameters.CustomParameterUI("Debug each opcode",
toolTip = "(For mod developers) Spams the Unity log file with a message for every time\n" +
"an opcode is executed in the virtual machine. Very laggy.")]
public bool debugEachOpcode = false;
public override GameParameters.GameMode GameMode
{
get
{
return GameParameters.GameMode.ANY;
}
}
public override bool HasPresets
{
get
{
return false;
}
}
public override string DisplaySection { get { return "kOS"; } }
public override string Section
{
get
{
return "kOS";
}
}
public override int SectionOrder
{
get
{
return 0;
}
}
public override string Title
{
get
{
return "CONFIG";
}
}
public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);
instance = null;
}
public override bool Enabled(MemberInfo member, GameParameters parameters)
{
if (member.Name == "migrated" || member.Name == "version")
{
return false;
}
return base.Enabled(member, parameters);
}
public void CheckMigrateSettings()
{
Safe.Utilities.SafeHouse.Logger.SuperVerbose("kOSCustomParameters.CheckMigrateSettings()");
if (!migrated)
{
var config = PluginConfiguration.CreateForType<kOSCustomParameters>();
config.load();
var ipu = config.GetValue("InstructionsPerUpdate", -1);
// if the ipu is set below zero, it means that the file was created after we switch to
// the new system, or that the user selected to prevent future migrations.
if (ipu > 0)
{
kOSSettingsChecker.QueueDialog(
0.5f, 0.5f, // causes it to be centered (half of box's own width left and down from center is the corner).
new MultiOptionDialog(
"Migration Dialog",
MIGRATION_DIALOG_TEXT,
"kOS",
HighLogic.UISkin,
new DialogGUIButton("Yes: migrate settings", MigrateSettingsNormal, true),
new DialogGUIButton("Yes: migrate settings this one time,\nbut never ask again for this or any other game", MigrateSettingsPrevent, true),
new DialogGUIButton("No: start with new default settings", DontMigrate, true),
new DialogGUIButton("No: start with new default settings\nand never ask again for this or any other game", DontMigrateAndPrevent, true)
));
}
else
{
Safe.Utilities.SafeHouse.Logger.LogError("ipu: " + ipu.ToString());
migrated = true;
}
}
}
public void CheckClickThroughBlockerExists()
{
if (passedClickThroughCheck)
return;
bool clickThroughExists = false;
var loadedCTBAssembly = AssemblyLoader.loadedAssemblies.FirstOrDefault(a => a.dllName.Equals("ClickThroughBlocker"));
if (loadedCTBAssembly != null)
{
// Must be at least version 0.10 of ClickThroughBlocker:
if (loadedCTBAssembly.versionMajor > 0 || loadedCTBAssembly.versionMinor >= 10)
{
Type ctbType = loadedCTBAssembly.assembly.GetType("ClickThroughFix.CTB", false);
if (ctbType != null)
{
if (ctbType.GetField("focusFollowsclick") != null)
{
clickThroughExists = true;
}
}
}
}
string popupText =
"=======================================\n" +
"<b><color=#ffffff>kOS is Checking for ClickThroughBlocker</color></b>\n" +
"=======================================\n\n" +
"Starting with kOS v1.3, kOS has become dependent on the existence of the ClickThroughBlocker mod. " +
"(And it must be at least version 0.10 of ClickThroughBlocker.)\n\n";
if (clickThroughExists)
{
popupText +=
" <b><color=#ddffdd><<<< CHECK SUCCEEDED >>>>></color></b>\n\n" +
"kOS has found ClickThroughBlocker installed, and it appears to be a version that will work with kOS.\n" +
"\n" +
"Please note that while in the past the kOS terminal has always been click-to-focus, from now " +
"on it will behave however ClickThroughBlocker is set to act, which may be focus-follows-mouse.\n" +
"You can use ClickThroughBlocker's settings to change this behvior like this:\n\n" +
"[Hit Escape] Settings ->\n" +
" Difficulty Options ->\n" +
" ClickThroughBlocker ->\n" +
" [x] Focus Follows Click\n\n";
}
else
{
popupText +=
" <b><color=#ffff88>!!! CHECK FAILED !!!</color></b>\n\n" +
"kOS couldn't find a version of ClickThroughBlocker that works with kOS. This could be " +
"because ClickThroughBlocker is not installed at all, or it could be because its version is too old " +
"(or too new, if ClickThroughBlocker ever renames some things that kOS is using).\n" +
"\n\n" +
"To use kOS v1.3 or higher you'll need to quit Kerbal Space Program and install a version of ClickThroughBlocker that it supports.\n";
}
string buttonText;
global::Callback clickThroughAck;
if (clickThroughExists)
{
clickThroughAck = AcceptClickThrough;
buttonText = "Acknowledged.";
}
else
{
clickThroughAck = FailedClickThrough;
buttonText = "Acknowledged. I'll have to quit and change my mods.";
}
kOSSettingsChecker.QueueDialog(
0.75f, 0.6f,
new MultiOptionDialog(
"ClickThroughBlockerCheck",
popupText,
"kOS ClickThroughBlocker Check",
HighLogic.UISkin,
new DialogGUIButton(buttonText, clickThroughAck, true)
));
}
public void AcceptClickThrough()
{
passedClickThroughCheck = true;
}
public void FailedClickThrough()
{
passedClickThroughCheck = false;
}
public void MigrateSettingsNormal()
{
MigrateSettings(false);
}
public void MigrateSettingsPrevent()
{
MigrateSettings(true);
}
public void MigrateSettings(bool preventFuture)
{
var config = PluginConfiguration.CreateForType<kOSCustomParameters>();
config.load();
InstructionsPerUpdate = config.GetValue("InstructionsPerUpdate", -1);
useCompressedPersistence = config.GetValue<bool>("InstructionsPerUpdate");
showStatistics = config.GetValue<bool>("InstructionsPerUpdate");
startOnArchive = config.GetValue<bool>("StartOnArchive");
obeyHideUi = config.GetValue<bool>("ObeyHideUI");
enableSafeMode = config.GetValue<bool>("EnableSafeMode");
audibleExceptions = config.GetValue<bool>("AudibleExceptions");
verboseExceptions = config.GetValue<bool>("VerboseExceptions");
debugEachOpcode = config.GetValue<bool>("DebugEachOpcode");
useBlizzyToolbarOnly = config.GetValue<bool>("UseBlizzyToolbarOnly");
config.SetValue("SettingMigrationComment", "All settings except telnet settings are now stored in the game's save file. Settings stored here will be ignored.");
if (preventFuture)
{
config.SetValue("InstructionsPerUpdate", -2); // using -2 so it's different from the default value used above
config.SetValue("PreventFutureMigrationComment", "The user selected to prevent future migration notices when loading or creating save files. Change the IPU value to a positive value to re-enable migrations.");
}
config.save();
migrated = true;
}
public void DontMigrate()
{
var config = PluginConfiguration.CreateForType<kOSCustomParameters>();
config.load();
config.SetValue("SettingMigrationComment", "All settings except telnet settings are now stored in the game's save file. Settings stored here will be ignored.");
config.save();
migrated = true;
}
public void DontMigrateAndPrevent()
{
var config = PluginConfiguration.CreateForType<kOSCustomParameters>();
config.load();
config.SetValue("InstructionsPerUpdate", -2); // using -2 so it's different from the default value
config.SetValue("SettingMigrationComment", "All settings except telnet settings are now stored in the game's save file. Settings stored here will be ignored.");
config.SetValue("PreventFutureMigrationComment", "The user selected to prevent future migration notices when loading or creating save files. Change the IPU value to a positive value to re-enable migrations.");
config.save();
migrated = true;
}
}
}