This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
ScrapYardWrapper.cs
641 lines (589 loc) · 22.3 KB
/
ScrapYardWrapper.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
631
632
633
634
635
636
637
638
639
640
641
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
//TODO: Change namespace to your mod's namespace
namespace ScrapYard
{
//DO NOT CHANGE ANYTHING BELOW THIS LINE
public sealed class ScrapYardWrapper
{
private static bool? available;
private static Type SYType;
private static object _instance;
/// <summary>
/// The part tracker type to reference
/// </summary>
public enum TrackType
{
/// <summary>
/// Total number of builds/uses combining new and reused
/// </summary>
TOTAL,
/// <summary>
/// Only new builds/uses of the part
/// </summary>
NEW,
/// <summary>
/// Only reused builds/uses of the part
/// </summary>
INVENTORIED
}
/// <summary>
/// The strictness of comparing two parts for equivalency
/// </summary>
public enum ComparisonStrength
{
/// <summary>
/// Equivalent if their names match
/// </summary>
NAME,
/// <summary>
/// EqualEquivalent if name and dry cost match
/// </summary>
COSTS,
/// <summary>
/// Equaivalent if name, dry cost, and Modules (except ModuleSYPartTracker) match
/// </summary>
MODULES,
/// <summary>
/// Equivalent if name, dry cost, Modules, and TimesRecovered match
/// </summary>
TRACKER,
/// <summary>
/// Equivalent if name, dry cost, Modules, TimesRecovered and IDs match
/// </summary>
STRICT
}
/// <summary>
/// True if ScrapYard is available, false if not
/// </summary>
public static bool Available
{
get
{
if (available == null)
{
SYType = AssemblyLoader.loadedAssemblies
.Select(a => a.assembly.GetExportedTypes())
.SelectMany(t => t)
.FirstOrDefault(t => t.FullName == "ScrapYard.APIManager");
available = SYType != null;
}
return available.GetValueOrDefault();
}
}
#region Public Methods
#region Inventory Manipulation
/// <summary>
/// Takes a List of Parts and returns the Parts that are present in the inventory.
/// </summary>
/// <param name="sourceParts">Source list of parts</param>
/// <param name="strictness">How strict of a comparison to use. Defaults to MODULES</param>
/// <returns>List of Parts that are in the inventory</returns>
public static IList<Part> GetPartsInInventory(IEnumerable<Part> sourceParts, ComparisonStrength strictness = ComparisonStrength.MODULES)
{
if (!Available)
{
return null;
}
return (IList<Part>)invokeMethod("GetPartsInInventory_Parts", sourceParts, strictness.ToString());
//Why do a ToString on an enum instead of casting to int? Because if the internal enum changes then the intended strictness is kept.
}
/// <summary>
/// Takes a List of part ConfigNodes and returns the ConfigNodes that are present in the inventory.
/// </summary>
/// <param name="sourceParts">Source list of parts</param>
/// <param name="strictness">How strict of a comparison to use. Defaults to MODULES</param>
/// <returns>List of part ConfigNodes that are in the inventory</returns>
public static IList<ConfigNode> GetPartsInInventory(IEnumerable<ConfigNode> sourceParts, ComparisonStrength strictness = ComparisonStrength.MODULES)
{
if (!Available)
{
return null;
}
return (IList<ConfigNode>)invokeMethod("GetPartsInInventory_ConfigNodes", sourceParts, strictness.ToString());
//Why do a ToString on an enum instead of casting to int? Because if the internal enum changes then the intended strictness is kept.
}
/// <summary>
/// Adds a list of parts to the Inventory
/// </summary>
/// <param name="parts">The list of parts to add</param>
/// <param name="incrementRecovery">If true, increments the number of recoveries in the tracker</param>
public static void AddPartsToInventory(IEnumerable<Part> parts, bool incrementRecovery)
{
if (Available)
{
invokeMethod("AddPartsToInventory_Parts", parts, incrementRecovery);
}
}
/// <summary>
/// Adds a list of parts to the Inventory
/// </summary>
/// <param name="parts">The list of parts to add</param>
/// <param name="incrementRecovery">If true, increments the number of recoveries in the tracker</param>
public static void AddPartsToInventory(IEnumerable<ConfigNode> parts, bool incrementRecovery)
{
if (Available)
{
invokeMethod("AddPartsToInventory_Nodes", parts, incrementRecovery);
}
}
/// <summary>
/// Adds a part to the Inventory
/// </summary>
/// <param name="part">The part to add</param>
/// <param name="incrementRecovery">If true, increments the counter for how many times the part was recovered</param>
/// <returns>True if added, false otherwise</returns>
public static bool AddPartToInventory(Part part, bool incrementRecovery)
{
if (!Available)
{
return false;
}
return (bool)invokeMethod("AddPartToInventory_Part", part, incrementRecovery);
}
/// <summary>
/// Adds a part to the Inventory
/// </summary>
/// <param name="part">The part to add</param>
/// <param name="incrementRecovery">If true, increments the counter for how many times the part was recovered</param>
/// <returns>True if added, false otherwise</returns>
public static bool AddPartToInventory(ConfigNode part, bool incrementRecovery)
{
if (!Available)
{
return false;
}
return (bool)invokeMethod("AddPartToInventory_Node", part, incrementRecovery);
}
/// <summary>
/// Removes a part from the Inventory using the given strictness for finding the part
/// </summary>
/// <param name="part">The part to remove</param>
/// <param name="strictness">The strictenss to use when searching for the part. Defaults to MODULES</param>
/// <returns>True if removed, false otherwise.</returns>
public static bool RemovePartFromInventory(Part part, ComparisonStrength strictness = ComparisonStrength.MODULES)
{
if (!Available)
{
return false;
}
return (bool)invokeMethod("RemovePartFromInventory_Part", part, strictness.ToString());
}
/// <summary>
/// Removes a part from the Inventory using the given strictness for finding the part
/// </summary>
/// <param name="part">The part to remove</param>
/// <param name="strictness">The strictenss to use when searching for the part. Defaults to MODULES</param>
/// <returns>True if removed, false otherwise.</returns>
public static bool RemovePartFromInventory(ConfigNode part, ComparisonStrength strictness = ComparisonStrength.MODULES)
{
if (!Available)
{
return false;
}
return (bool)invokeMethod("RemovePartFromInventory_Node", part, strictness.ToString());
}
/// <summary>
/// Sells/Discards parts out of the inventory
/// </summary>
/// <param name="parts">The parts to sell</param>
/// <returns>The total value of the sale</returns>
public static double SellParts(IEnumerable<Part> parts)
{
if (!Available)
{
return 0;
}
return (double)invokeMethod("SellParts_Parts", parts);
}
/// <summary>
/// Sells/Discards parts out of the inventory
/// </summary>
/// <param name="parts">The parts to sell</param>
/// <returns>The total value of the sale</returns>
public static double SellParts(IEnumerable<ConfigNode> parts)
{
if (!Available)
{
return 0;
}
return (double)invokeMethod("SellParts_Nodes", parts);
}
/// <summary>
/// Finds a part in the inventory for the given part
/// </summary>
/// <param name="part">The part to search for</param>
/// <param name="strictness">The strictness to use when searching for the part. Defaults to MODULES.</param>
/// <returns>A ConfigNode representing the InventoryPart, or null if none found.</returns>
public static ConfigNode FindInventoryPart(Part part, ComparisonStrength strictness = ComparisonStrength.MODULES)
{
if (!Available)
{
return null;
}
return invokeMethod("FindInventoryPart_Part", part, strictness.ToString()) as ConfigNode;
}
/// <summary>
/// Finds a part in the inventory for the given part
/// </summary>
/// <param name="part">The part to search for</param>
/// <param name="strictness">The strictness to use when searching for the part. Defaults to MODULES.</param>
/// <returns>A ConfigNode representing the InventoryPart, or null if none found.</returns>
public static ConfigNode FindInventoryPart(ConfigNode part, ComparisonStrength strictness = ComparisonStrength.MODULES)
{
if (!Available)
{
return null;
}
return invokeMethod("FindInventoryPart_Node", part, strictness.ToString()) as ConfigNode;
}
/// <summary>
/// Finds a part in the inventory for the given id
/// </summary>
/// <param name="id">The id of the part to search for.</param>
/// <returns>A ConfigNode representing the InventoryPart, or null if none found.</returns>
public static ConfigNode FindInventoryPart(string id)
{
if (!Available)
{
return null;
}
return invokeMethod("FindInventoryPart_ID", id) as ConfigNode;
}
/// <summary>
/// Gets all parts in the inventory as a list of ConfigNodes
/// </summary>
/// <returns>The list of all inventory parts</returns>
public static IList<ConfigNode> GetAllInventoryParts()
{
if (!Available)
{
return null;
}
return invokeMethod("GetAllInventoryParts") as IList<ConfigNode>;
}
/// <summary>
/// Refreshes a part node to be fresh and not from the inventory
/// </summary>
/// <param name="partNode">The part to refresh</param>
/// <returns>Success</returns>
public static bool RefreshPart(ConfigNode partNode)
{
if (!Available)
{
return false;
}
return (bool)invokeMethod("RefreshPart_Node", partNode);
}
#endregion Inventory Manipulation
#region Vessel Processing
/// <summary>
/// Removes inventory parts, refunds funds, marks it as tracked
/// </summary>
/// <param name="parts">The vessel as a List of Parts</param>
/// <returns>True if processed, false otherwise</returns>
public static bool ProcessVessel(IEnumerable<Part> parts)
{
if (!Available)
{
return false;
}
return (bool)invokeMethod("ProcessVessel_Parts", parts);
}
/// <summary>
/// Removes inventory parts, refunds funds, marks it as tracked
/// </summary>
/// <param name="parts">The vessel as a List of part ConfigNodes</param>
/// <returns>True if processed, false otherwise</returns>
public static bool ProcessVessel(IEnumerable<ConfigNode> parts)
{
if (!Available)
{
return false;
}
return (bool)invokeMethod("ProcessVessel_Nodes", parts);
}
/// <summary>
/// Records a build in the part tracker
/// </summary>
/// <param name="parts">The vessel as a list of Parts.</param>
public static void RecordBuild(IEnumerable<Part> parts)
{
if (Available)
{
invokeMethod("RecordBuild_Parts", parts);
}
}
/// <summary>
/// Records a build in the part tracker
/// </summary>
/// <param name="parts">The vessel as a list of ConfigNodes.</param>
public static void RecordBuild(IEnumerable<ConfigNode> parts)
{
if (Available)
{
invokeMethod("RecordBuild_Nodes", parts);
}
}
/// <summary>
/// Sets whether a vessel is tracked or not
/// </summary>
/// <param name="id">The ID of the vessel</param>
/// <param name="newStatus">The status to set</param>
/// <returns>The previous status</returns>
public static bool SetProcessedStatus(string id, bool newStatus)
{
if (Available)
{
return (bool)invokeMethod("SetProcessedStatus_ID", id, newStatus);
}
return false;
}
#endregion Vessel Processing
#region Part Tracker
/// <summary>
/// Gets the number of builds for a part
/// </summary>
/// <param name="part">The part to check</param>
/// <returns>Number of builds for the part</returns>
public static int GetBuildCount(Part part, TrackType trackType = TrackType.TOTAL)
{
if (!Available)
{
return 0;
}
return (int)invokeMethod("GetBuildCount_Part", part, trackType.ToString());
}
/// <summary>
/// Gets the number of builds for a part
/// </summary>
/// <param name="partNode">The ConfigNode of the part to check</param>
/// <returns>Number of builds for the part</returns>
public static int GetBuildCount(ConfigNode part, TrackType trackType = TrackType.TOTAL)
{
if (!Available)
{
return 0;
}
return (int)invokeMethod("GetBuildCount_Node", part, trackType.ToString());
}
/// <summary>
/// Gets the number of total uses of a part
/// </summary>
/// <param name="part">The part to check</param>
/// <returns>Number of uses of the part</returns>
public static int GetUseCount(Part part, TrackType trackType = TrackType.TOTAL)
{
if (!Available)
{
return 0;
}
return (int)invokeMethod("GetUseCount_Part", part, trackType.ToString());
}
/// <summary>
/// Gets the number of total uses of a part
/// </summary>
/// <param name="partNode">The ConfigNode of the part to check</param>
/// <returns>Number of uses of the part</returns>
public static int GetUseCount(ConfigNode part, TrackType trackType = TrackType.TOTAL)
{
if (!Available)
{
return 0;
}
return (int)invokeMethod("GetUseCount_Node", part, trackType.ToString());
}
/// <summary>
/// Gets the unique ID for the current part.
/// It is recommended to cache this.
/// </summary>
/// <param name="part">The part to get the ID of</param>
/// <returns>The part's ID (a Guid) as a string or null if it can't be gotten</returns>
public static string GetPartID(Part part)
{
if (!Available)
{
return null;
}
return invokeMethod("GetPartID_Part", part) as string;
}
/// <summary>
/// Gets the unique ID for the current part.
/// It is recommended to cache this.
/// </summary>
/// <param name="part">The part to get the ID of</param>
/// <returns>The part's ID (a Guid) as a string or null if it can't be gotten</returns>
public static string GetPartID(ConfigNode part)
{
if (!Available)
{
return null;
}
return invokeMethod("GetPartID_Node", part) as string;
}
/// <summary>
/// Gets the number of times a part has been recovered.
/// It is recommended to cache this.
/// </summary>
/// <param name="part">The part to get the TimesRecovered count of.</param>
/// <returns>The number of times the part has been recovered.</returns>
public static int GetTimesUsed(Part part)
{
if (!Available)
{
return 0;
}
return (int)invokeMethod("GetTimesUsed_Part", part);
}
/// <summary>
/// Gets the number of times a part has been recovered.
/// It is recommended to cache this.
/// </summary>
/// <param name="part">The part to get the TimesRecovered count of.</param>
/// <returns>The number of times the part has been recovered.</returns>
public static int GetTimesUsed(ConfigNode part)
{
if (!Available)
{
return 0;
}
return (int)invokeMethod("GetTimesUsed_Node", part);
}
/// <summary>
/// Checks if the part is pulled from the inventory or is new
/// </summary>
/// <param name="part">The part to check</param>
/// <returns>True if from inventory, false if new</returns>
public static bool PartIsFromInventory(Part part)
{
if (!Available)
{
return false;
}
return (bool)invokeMethod("PartIsFromInventory_Part", part);
}
/// <summary>
/// Checks if the part is pulled from the inventory or is new
/// </summary>
/// <param name="part">The part to check</param>
/// <returns>True if from inventory, false if new</returns>
public static bool PartIsFromInventory(ConfigNode part)
{
if (!Available)
{
return false;
}
return (bool)invokeMethod("PartIsFromInventory_Node", part);
}
#endregion Part Tracker
#region Settings
/// <summary>
/// The list of part names that are blacklisted
/// </summary>
public static IEnumerable<string> PartBlacklist
{
get
{
return Available ? invokeMethod("GetSetting_PartBlacklist") as IEnumerable<string> : null;
}
}
/// <summary>
/// Whether or not to automatically apply the inventory while building ships in the editor
/// </summary>
public static bool AutoApplyInventory
{
get
{
return Available ? (bool)invokeMethod("GetSetting_AutoApplyInventory") : false;
}
set
{
if (Available)
{
invokeMethod("SetSetting_AutoApplyInventory", value);
}
}
}
/// <summary>
/// Whether the mod is enabled for this save
/// </summary>
public static bool ModEnabled
{
get
{
return Available ? (bool)invokeMethod("GetSetting_ModEnabled") : false;
}
}
/// <summary>
/// Whether the inventory is in use for this save
/// </summary>
public static bool UseInventory
{
get
{
return Available ? (bool)invokeMethod("GetSetting_UseInventory") : false;
}
}
/// <summary>
/// Whether the part use tracker is enabled for this save
/// </summary>
public static bool UseTracker
{
get
{
return Available ? (bool)invokeMethod("GetSetting_UseTracker") : false;
}
}
/// <summary>
/// Whether the Override Funds option is in use for this save
/// </summary>
public static bool OverrideFunds
{
get
{
return Available ? (bool)invokeMethod("GetSetting_OverrideFunds") : false;
}
}
/// <summary>
/// The percentage of funds returned for selling parts when OverrideFunds is active, for this save
/// </summary>
public static int FundsSalePercent
{
get
{
return Available ? (int)invokeMethod("GetSetting_FundsSalePercent") : 100;
}
}
#endregion Settings
#endregion Public Methods
#region Private Methods
/// <summary>
/// The static instance of the APIManager within ScrapYard
/// </summary>
private static object Instance
{
get
{
if (Available && _instance == null)
{
_instance = SYType.GetProperty("Instance").GetValue(null, null);
}
return _instance;
}
}
/// <summary>
/// Invokes a method on the ScrapYard API
/// </summary>
/// <param name="methodName">The name of the method</param>
/// <param name="parameters">Parameters to pass to the method</param>
/// <returns>The response</returns>
private static object invokeMethod(string methodName, params object[] parameters)
{
MethodInfo method = SYType.GetMethod(methodName);
return method?.Invoke(Instance, parameters);
}
#endregion Private Methods
}
}