Skip to content

Commit 1a29f32

Browse files
committed
Refactor, Part 1
1 parent c6bccfd commit 1a29f32

File tree

9 files changed

+326
-311
lines changed

9 files changed

+326
-311
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
## Ignore Visual Studio temporary files, build results, and
22
## files generated by popular Visual Studio add-ons.
33

4+
!ModuleManager.*.dll
5+
6+
7+
48
# User-specific files
59
*.suo
610
*.user

Source/API/CrewQAPI.cs

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Reflection;
6+
using UnityEngine;
7+
8+
// Include this file in your project in order to support a soft-dependency on CrewQ.
9+
// Do not edit this file.
10+
// Example usage: CrewQ.API.SuppressCrew()
11+
namespace CrewQ
12+
{
13+
public class API
14+
{
15+
private static bool? _available = null;
16+
private static Type _type = null;
17+
private static object _instance;
18+
19+
/// <summary>
20+
/// This indicates if CrewQ is loaded
21+
/// </summary>
22+
public static bool Available
23+
{
24+
get
25+
{
26+
if (_available == null)
27+
{
28+
_type = AssemblyLoader.loadedAssemblies
29+
.Select(a => a.assembly.GetExportedTypes())
30+
.SelectMany(t => t)
31+
.FirstOrDefault(t => t.FullName == "CrewQ.CrewQ");
32+
33+
_available = _type != null;
34+
}
35+
return (bool)_available;
36+
}
37+
}
38+
39+
/// <summary>
40+
/// Returns the Kerbals who are allowed to go on missions, unsorted.
41+
/// </summary>
42+
public static IEnumerable<ProtoCrewMember> AvailableCrew
43+
{
44+
get
45+
{
46+
return (IEnumerable<ProtoCrewMember>) getProperty("AvailableCrew");
47+
}
48+
}
49+
50+
/// <summary>
51+
/// Returns the Kerbals who are not allowed to go on missions.
52+
/// </summary>
53+
public static IEnumerable<ProtoCrewMember> UnavailableCrew
54+
{
55+
get
56+
{
57+
return (IEnumerable<ProtoCrewMember>) getProperty("UnavailableCrew");
58+
}
59+
}
60+
61+
/// <summary>
62+
/// Returns the Kerbals who are allowed to go on missions, sorted inexperienced first.
63+
/// </summary>
64+
public static IEnumerable<ProtoCrewMember> NewbieCrew
65+
{
66+
get
67+
{
68+
return (IEnumerable<ProtoCrewMember>)getProperty("NewbieCrew");
69+
}
70+
}
71+
72+
/// <summary>
73+
/// Returns the Kerbals who are allowed to go on missions, sorted veterans first.
74+
/// </summary>
75+
public static IEnumerable<ProtoCrewMember> VeteranCrew
76+
{
77+
get
78+
{
79+
return (IEnumerable<ProtoCrewMember>)getProperty("VeteranCrew");
80+
}
81+
}
82+
83+
/// <summary>
84+
/// Hides any kerbals who aren't allowed on missions from the vanilla available crew lists
85+
/// </summary>
86+
public void HideVacationingCrew()
87+
{
88+
invokeMethod("HideVacationingCrew");
89+
}
90+
91+
/// <summary>
92+
/// Reverses a previous call to HideVacationingCrew
93+
/// </summary>
94+
public void ShowVacationingCrew()
95+
{
96+
invokeMethod("ShowVacationingCrew");
97+
}
98+
99+
// Generic accessors
100+
internal static object Instance
101+
{
102+
get
103+
{
104+
if (Available && _instance == null)
105+
{
106+
_instance = _type.GetProperty("Instance").GetValue(null, null);
107+
}
108+
109+
return _instance;
110+
}
111+
}
112+
113+
internal static object getProperty(string name, object[] indexes = null)
114+
{
115+
if (Available)
116+
{
117+
System.Reflection.PropertyInfo _property = _type.GetProperty(name, BindingFlags.NonPublic | BindingFlags.Instance);
118+
return _property.GetValue(Instance, indexes);
119+
}
120+
else
121+
{
122+
return null;
123+
}
124+
}
125+
126+
internal static object invokeMethod(string name, object[] parameters = null)
127+
{
128+
if (Available)
129+
{
130+
MethodInfo _method = _type.GetMethod(name, BindingFlags.NonPublic | BindingFlags.Instance);
131+
return _method.Invoke(Instance, parameters);
132+
}
133+
else
134+
{
135+
return null;
136+
}
137+
}
138+
}
139+
140+
public static class APIExtensions
141+
{
142+
/// <summary>
143+
/// Returns the time (in seconds) at which this Kerbal is or was eligible for missions again.
144+
/// </summary>
145+
public static double GetVacationTimer(this ProtoCrewMember kerbal)
146+
{
147+
return (double)API.invokeMethod("GetVacationTimerInternal", new object[] { kerbal });
148+
}
149+
150+
/// <summary>
151+
/// Returns the vacation state of this Kerbal
152+
/// </summary>
153+
public static bool OnVacation(this ProtoCrewMember kerbal)
154+
{
155+
return (bool)API.invokeMethod("OnVacationInternal", new object[] { kerbal });
156+
}
157+
158+
/// <summary>
159+
/// Set the time at which this Kerbal will be eligible for missions again.
160+
/// </summary>
161+
/// <param name="timeout">Time (in seconds) at which this Kerbal will be eligible for missions.</param>
162+
public static void SetVacationTimer(this ProtoCrewMember kerbal, double timeout)
163+
{
164+
API.invokeMethod("SetVacationTimerInternal", new object[] { kerbal, timeout });
165+
}
166+
}
167+
}

0 commit comments

Comments
 (0)