-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKACWrapper.cs
630 lines (552 loc) · 24.3 KB
/
KACWrapper.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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;
namespace KerbalConstructionTime
{
/// <summary>
/// The Wrapper class to access KAC from another plugin
/// </summary>
public class KACWrapper
{
protected static System.Type KACType;
protected static System.Type KACAlarmType;
protected static object actualKAC = null;
/// <summary>
/// This is the Kerbal Alarm Clock object
///
/// SET AFTER INIT
/// </summary>
public static KACAPI KAC = null;
/// <summary>
/// Whether we found the KerbalAlarmClock assembly in the loadedassemblies.
///
/// SET AFTER INIT
/// </summary>
public static bool AssemblyExists { get { return (KACType != null); } }
/// <summary>
/// Whether we managed to hook the running Instance from the assembly.
///
/// SET AFTER INIT
/// </summary>
public static bool InstanceExists { get { return (KAC != null); } }
/// <summary>
/// Whether we managed to wrap all the methods/functions from the instance.
///
/// SET AFTER INIT
/// </summary>
private static bool _KACWrapped = false;
/// <summary>
/// Whether the object has been wrapped and the APIReady flag is set in the real KAC
/// </summary>
public static bool APIReady { get { return _KACWrapped && KAC.APIReady && !NeedUpgrade; } }
public static bool NeedUpgrade { get; private set; }
/// <summary>
/// This method will set up the KAC object and wrap all the methods/functions
/// </summary>
/// <param name="Force">This option will force the Init function to rebind everything</param>
/// <returns></returns>
public static bool InitKACWrapper()
{
//if (!_KACWrapped )
//{
//reset the internal objects
_KACWrapped = false;
actualKAC = null;
KAC = null;
LogFormatted("Attempting to Grab KAC Types...");
//find the base type
KACType = null;
AssemblyLoader.loadedAssemblies.TypeOperation(t =>
{
if (t.FullName == "KerbalAlarmClock.KerbalAlarmClock")
{
KACType = t;
}
});
if (KACType == null)
{
return false;
}
LogFormatted("KAC Version:{0}", KACType.Assembly.GetName().Version.ToString());
if (KACType.Assembly.GetName().Version.CompareTo(new System.Version(3, 0, 0, 5)) < 0)
{
//No TimeEntry or alarmchoice options = need a newer version
NeedUpgrade = true;
}
//now the Alarm Type
KACAlarmType = null;
AssemblyLoader.loadedAssemblies.TypeOperation(t =>
{
if (t.FullName == "KerbalAlarmClock.KACAlarm")
{
KACAlarmType = t;
}
});
if (KACAlarmType == null)
{
return false;
}
//now grab the running instance
LogFormatted("Got Assembly Types, grabbing Instance");
try
{
actualKAC = KACType.GetField("APIInstance", BindingFlags.Public | BindingFlags.Static).GetValue(null);
}
catch (Exception)
{
NeedUpgrade = true;
LogFormatted("No APIInstance found - most likely you have KAC v2 installed");
//throw;
}
if (actualKAC == null)
{
LogFormatted("Failed grabbing Instance");
return false;
}
//If we get this far we can set up the local object and its methods/functions
LogFormatted("Got Instance, Creating Wrapper Objects");
KAC = new KACAPI(actualKAC);
//}
_KACWrapped = true;
return true;
}
/// <summary>
/// The Type that is an analogue of the real KAC. This lets you access all the API-able properties and Methods of the KAC
/// </summary>
public class KACAPI
{
internal KACAPI(object KAC)
{
//store the actual object
actualKAC = KAC;
//these sections get and store the reflection info and actual objects where required. Later in the properties we then read the values from the actual objects
//for events we also add a handler
LogFormatted("Getting APIReady Object");
APIReadyField = KACType.GetField("APIReady", BindingFlags.Public | BindingFlags.Static);
LogFormatted("Success: " + (APIReadyField != null).ToString());
//WORK OUT THE STUFF WE NEED TO HOOK FOR PEOPEL HERE
LogFormatted("Getting Alarms Object");
AlarmsField = KACType.GetField("alarms", BindingFlags.Public | BindingFlags.Static);
actualAlarms = AlarmsField.GetValue(actualKAC);
LogFormatted("Success: " + (actualAlarms != null).ToString());
//Events
LogFormatted("Getting Alarm State Change Event");
onAlarmStateChangedEvent = KACType.GetEvent("onAlarmStateChanged", BindingFlags.Public | BindingFlags.Instance);
LogFormatted_DebugOnly("Success: " + (onAlarmStateChangedEvent != null).ToString());
LogFormatted_DebugOnly("Adding Handler");
AddHandler(onAlarmStateChangedEvent, actualKAC, AlarmStateChanged);
//Methods
LogFormatted("Getting Create Method");
CreateAlarmMethod = KACType.GetMethod("CreateAlarm", BindingFlags.Public | BindingFlags.Instance);
LogFormatted_DebugOnly("Success: " + (CreateAlarmMethod != null).ToString());
LogFormatted("Getting Delete Method");
DeleteAlarmMethod = KACType.GetMethod("DeleteAlarm", BindingFlags.Public | BindingFlags.Instance);
LogFormatted_DebugOnly("Success: " + (DeleteAlarmMethod != null).ToString());
LogFormatted("Getting DrawAlarmAction");
DrawAlarmActionChoiceMethod = KACType.GetMethod("DrawAlarmActionChoiceAPI", BindingFlags.Public | BindingFlags.Instance);
LogFormatted_DebugOnly("Success: " + (DrawAlarmActionChoiceMethod != null).ToString());
//LogFormatted("Getting DrawTimeEntry");
//DrawTimeEntryMethod = KACType.GetMethod("DrawTimeEntryAPI", BindingFlags.Public | BindingFlags.Instance);
//LogFormatted_DebugOnly("Success: " + (DrawTimeEntryMethod != null).ToString());
//Commenting out rubbish lines
//MethodInfo[] mis = KACType.GetMethods(BindingFlags.Public | BindingFlags.Instance);
//foreach (MethodInfo mi in mis)
//{
// LogFormatted("M:{0}-{1}", mi.Name, mi.DeclaringType);
//}
}
private object actualKAC;
private FieldInfo APIReadyField;
/// <summary>
/// Whether the APIReady flag is set in the real KAC
/// </summary>
public bool APIReady
{
get
{
if (APIReadyField == null)
return false;
return (bool)APIReadyField.GetValue(null);
}
}
#region Alarms
private object actualAlarms;
private FieldInfo AlarmsField;
/// <summary>
/// The list of Alarms that are currently active in game
/// </summary>
internal KACAlarmList Alarms
{
get
{
return ExtractAlarmList(actualAlarms);
}
}
/// <summary>
/// This converts the KACAlarmList actual object to a new List for consumption
/// </summary>
/// <param name="actualAlarmList"></param>
/// <returns></returns>
private KACAlarmList ExtractAlarmList(object actualAlarmList)
{
KACAlarmList ListToReturn = new KACAlarmList();
try
{
//iterate each "value" in the dictionary
for (var i = ((IList)actualAlarmList).Count - 1; i >= 0; i--)
{
var item = ((IList)actualAlarmList)[i];
//foreach (var item in (IList)actualAlarmList)
//{
KACAlarm r1 = new KACAlarm(item);
ListToReturn.Add(r1);
}
}
catch (Exception)
{
//LogFormatted("Arrggg: {0}", ex.Message);
//throw ex;
//
}
return ListToReturn;
}
#endregion
#region Events
/// <summary>
/// Takes an EventInfo and binds a method to the event firing
/// </summary>
/// <param name="Event">EventInfo of the event we want to attach to</param>
/// <param name="KACObject">actual object the eventinfo is gathered from</param>
/// <param name="Handler">Method that we are going to hook to the event</param>
protected void AddHandler(EventInfo Event, object KACObject, Action<object> Handler)
{
//build a delegate
Delegate d = Delegate.CreateDelegate(Event.EventHandlerType, Handler.Target, Handler.Method);
//get the Events Add method
MethodInfo addHandler = Event.GetAddMethod();
//and add the delegate
addHandler.Invoke(KACObject, new object[] { d });
}
//the info about the event;
private EventInfo onAlarmStateChangedEvent;
/// <summary>
/// Event that fires when the State of an Alarm changes
/// </summary>
public event AlarmStateChangedHandler onAlarmStateChanged;
/// <summary>
/// Structure of the event delegeate
/// </summary>
/// <param name="e"></param>
public delegate void AlarmStateChangedHandler(AlarmStateChangedEventArgs e);
/// <summary>
/// This is the structure that holds the event arguments
/// </summary>
public class AlarmStateChangedEventArgs
{
public AlarmStateChangedEventArgs(object actualEvent, KACAPI kac)
{
Type type = actualEvent.GetType();
this.alarm = new KACAlarm(type.GetField("alarm").GetValue(actualEvent));
this.eventType = (KACAlarm.AlarmStateEventsEnum)type.GetField("eventType").GetValue(actualEvent);
}
/// <summary>
/// Alarm that has had the state change
/// </summary>
public KACAlarm alarm;
/// <summary>
/// What the state was before the event
/// </summary>
public KACAlarm.AlarmStateEventsEnum eventType;
}
/// <summary>
/// private function that grabs the actual event and fires our wrapped one
/// </summary>
/// <param name="actualEvent">actual event from the KAC</param>
private void AlarmStateChanged(object actualEvent)
{
if (onAlarmStateChanged != null)
{
onAlarmStateChanged(new AlarmStateChangedEventArgs(actualEvent, this));
}
}
#endregion
#region Methods
private MethodInfo CreateAlarmMethod;
/// <summary>
/// Create a new Alarm
/// </summary>
/// <param name="AlarmType">What type of alarm are we creating</param>
/// <param name="Name">Name of the Alarm for the display</param>
/// <param name="UT">Universal Time for the alarm</param>
/// <returns>ID of the newly created alarm</returns>
internal string CreateAlarm(AlarmTypeEnum AlarmType, string Name, double UT)
{
return (string)CreateAlarmMethod.Invoke(actualKAC, new object[] { (int)AlarmType, Name, UT });
}
private MethodInfo DeleteAlarmMethod;
/// <summary>
/// Delete an Alarm
/// </summary>
/// <param name="AlarmID">Unique ID of the alarm</param>
/// <returns>Success of the deletion</returns>
internal bool DeleteAlarm(string AlarmID)
{
return (bool)DeleteAlarmMethod.Invoke(actualKAC, new object[] { AlarmID });
}
private MethodInfo DrawAlarmActionChoiceMethod;
/// <summary>
/// Delete an Alarm
/// </summary>
/// <param name="AlarmID">Unique ID of the alarm</param>
/// <returns>Success of the deletion</returns>
internal bool DrawAlarmActionChoice(ref AlarmActionEnum Choice, string LabelText, int LabelWidth, int ButtonWidth)
{
int InValue = (int)Choice;
int OutValue = (int)DrawAlarmActionChoiceMethod.Invoke(actualKAC, new object[] { InValue, LabelText, LabelWidth, ButtonWidth });
Choice = (AlarmActionEnum)OutValue;
return (InValue != OutValue);
}
//Remmed out due to it borking window layout
//private MethodInfo DrawTimeEntryMethod;
///// <summary>
///// Delete an Alarm
///// </summary>
///// <param name="AlarmID">Unique ID of the alarm</param>
///// <returns>Success of the deletion</returns>
//internal Boolean DrawTimeEntry(ref Double Time, TimeEntryPrecisionEnum Prec, String LabelText, Int32 LabelWidth)
//{
// Double InValue = Time;
// Double OutValue = (Double)DrawTimeEntryMethod.Invoke(actualKAC, new System.Object[] { InValue, (Int32)Prec, LabelText, LabelWidth });
// Time = OutValue;
// return (InValue != OutValue);
//}
#endregion
public class KACAlarm
{
internal KACAlarm(object a)
{
actualAlarm = a;
VesselIDField = KACAlarmType.GetField("VesselID");
IDField = KACAlarmType.GetField("ID");
NameField = KACAlarmType.GetField("Name");
NotesField = KACAlarmType.GetField("Notes");
AlarmTypeField = KACAlarmType.GetField("TypeOfAlarm");
AlarmTimeProperty = KACAlarmType.GetProperty("AlarmTimeUT");
AlarmMarginField = KACAlarmType.GetField("AlarmMarginSecs");
AlarmActionField = KACAlarmType.GetField("AlarmAction");
RemainingField = KACAlarmType.GetField("Remaining");
XferOriginBodyNameField = KACAlarmType.GetField("XferOriginBodyName");
//LogFormatted("XFEROrigin:{0}", XferOriginBodyNameField == null);
XferTargetBodyNameField = KACAlarmType.GetField("XferTargetBodyName");
RepeatAlarmField = KACAlarmType.GetField("RepeatAlarm");
RepeatAlarmPeriodProperty = KACAlarmType.GetProperty("RepeatAlarmPeriodUT");
//PropertyInfo[] pis = KACAlarmType.GetProperties();
//foreach (PropertyInfo pi in pis)
//{
// LogFormatted("P:{0}-{1}", pi.Name, pi.DeclaringType);
//}
//FieldInfo[] fis = KACAlarmType.GetFields();
//foreach (FieldInfo fi in fis)
//{
// LogFormatted("F:{0}-{1}", fi.Name, fi.DeclaringType);
//}
}
private object actualAlarm;
private FieldInfo VesselIDField;
/// <summary>
/// Unique Identifier of the Vessel that the alarm is attached to
/// </summary>
public string VesselID
{
get { return (string)VesselIDField.GetValue(actualAlarm); }
set { VesselIDField.SetValue(actualAlarm, value); }
}
private FieldInfo IDField;
/// <summary>
/// Unique Identifier of this alarm
/// </summary>
public string ID
{
get { return (string)IDField.GetValue(actualAlarm); }
}
private FieldInfo NameField;
/// <summary>
/// Short Text Name for the Alarm
/// </summary>
public string Name
{
get { return (string)NameField.GetValue(actualAlarm); }
set { NameField.SetValue(actualAlarm, value); }
}
private FieldInfo NotesField;
/// <summary>
/// Longer Text Description for the Alarm
/// </summary>
public string Notes
{
get { return (string)NotesField.GetValue(actualAlarm); }
set { NotesField.SetValue(actualAlarm, value); }
}
private FieldInfo XferOriginBodyNameField;
/// <summary>
/// Name of the origin body for a transfer
/// </summary>
public string XferOriginBodyName
{
get { return (string)XferOriginBodyNameField.GetValue(actualAlarm); }
set { XferOriginBodyNameField.SetValue(actualAlarm, value); }
}
private FieldInfo XferTargetBodyNameField;
/// <summary>
/// Name of the destination body for a transfer
/// </summary>
public string XferTargetBodyName
{
get { return (string)XferTargetBodyNameField.GetValue(actualAlarm); }
set { XferTargetBodyNameField.SetValue(actualAlarm, value); }
}
private FieldInfo AlarmTypeField;
/// <summary>
/// What type of Alarm is this - affects icon displayed and some calc options
/// </summary>
public AlarmTypeEnum AlarmType { get { return (AlarmTypeEnum)AlarmTypeField.GetValue(actualAlarm); } }
private PropertyInfo AlarmTimeProperty;
/// <summary>
/// In game UT value of the alarm
/// </summary>
public double AlarmTime
{
get { return (double)AlarmTimeProperty.GetValue(actualAlarm, null); }
set { AlarmTimeProperty.SetValue(actualAlarm, value, null); }
}
private FieldInfo AlarmMarginField;
/// <summary>
/// In game seconds the alarm will fire before the event it is for
/// </summary>
public double AlarmMargin
{
get { return (double)AlarmMarginField.GetValue(actualAlarm); }
set { AlarmMarginField.SetValue(actualAlarm, value); }
}
private FieldInfo AlarmActionField;
/// <summary>
/// What should the Alarm Clock do when the alarm fires
/// </summary>
public AlarmActionEnum AlarmAction
{
get { return (AlarmActionEnum)AlarmActionField.GetValue(actualAlarm); }
set { AlarmActionField.SetValue(actualAlarm, (int)value); }
}
private FieldInfo RemainingField;
/// <summary>
/// How much Game time is left before the alarm fires
/// </summary>
public double Remaining { get { return (double)RemainingField.GetValue(actualAlarm); } }
private FieldInfo RepeatAlarmField;
/// <summary>
/// Whether the alarm will be repeated after it fires
/// </summary>
public bool RepeatAlarm
{
get { return (bool)RepeatAlarmField.GetValue(actualAlarm); }
set { RepeatAlarmField.SetValue(actualAlarm, value); }
}
private PropertyInfo RepeatAlarmPeriodProperty;
/// <summary>
/// Value in Seconds after which the alarm will repeat
/// </summary>
public double RepeatAlarmPeriod
{
get
{
try { return (double)RepeatAlarmPeriodProperty.GetValue(actualAlarm, null); }
catch (Exception) { return 0; }
}
set { RepeatAlarmPeriodProperty.SetValue(actualAlarm, value, null); }
}
public enum AlarmStateEventsEnum
{
Created,
Triggered,
Closed,
Deleted,
}
}
public enum AlarmTypeEnum
{
Raw,
Maneuver,
ManeuverAuto,
Apoapsis,
Periapsis,
AscendingNode,
DescendingNode,
LaunchRendevous,
Closest,
SOIChange,
SOIChangeAuto,
Transfer,
TransferModelled,
Distance,
Crew,
EarthTime,
Contract,
ContractAuto
}
public enum AlarmActionEnum
{
[Description("Do Nothing-Delete When Past")]
DoNothingDeleteWhenPassed,
[Description("Do Nothing")]
DoNothing,
[Description("Message Only-No Affect on warp")]
MessageOnly,
[Description("Kill Warp Only-No Message")]
KillWarpOnly,
[Description("Kill Warp and Message")]
KillWarp,
[Description("Pause Game and Message")]
PauseGame,
}
public enum TimeEntryPrecisionEnum
{
Seconds = 0,
Minutes = 1,
Hours = 2,
Days = 3,
Years = 4
}
public class KACAlarmList : List<KACAlarm>
{
}
}
#region Logging Stuff
/// <summary>
/// Some Structured logging to the debug file - ONLY RUNS WHEN DLL COMPILED IN DEBUG MODE
/// </summary>
/// <param name="Message">Text to be printed - can be formatted as per String.format</param>
/// <param name="strParams">Objects to feed into a String.format</param>
[System.Diagnostics.Conditional("DEBUG")]
internal static void LogFormatted_DebugOnly(string Message, params object[] strParams)
{
LogFormatted(Message, strParams);
}
/// <summary>
/// Some Structured logging to the debug file
/// </summary>
/// <param name="Message">Text to be printed - can be formatted as per String.format</param>
/// <param name="strParams">Objects to feed into a String.format</param>
internal static void LogFormatted(string Message, params object[] strParams)
{
Message = string.Format(Message, strParams);
string strMessageLine = string.Format("{0},{2}-{3},{1}",
DateTime.Now, Message, System.Reflection.Assembly.GetExecutingAssembly().GetName().Name,
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name);
UnityEngine.Debug.Log(strMessageLine);
}
#endregion
}
}