-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleAttacher.cs
219 lines (194 loc) · 5.19 KB
/
ModuleAttacher.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
/*
* Created by SharpDevelop.
* User: Bernhard
* Date: 07.03.2013
* Time: 03:14
*/
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using UnityEngine;
//public class PartlessLoader : KSP.Testing.UnitTest
//{
// public PartlessLoader() : base()
// {
// //Called at the first loading screen
// //When you start the game.
// MyPlugin.Initialize();
// }
//}
//
//public static class MyPlugin
//{
// private static UnityEngine.GameObject MyMonobehaviourObject;
//
// public static void Initialize()
// {
// MyMonobehaviourObject = new UnityEngine.GameObject("ModuleAttacherLoader", new Type[] {typeof(ModuleAttacher)});
// UnityEngine.GameObject.DontDestroyOnLoad(MyMonobehaviourObject);
// }
//}
public class AttachmentRestriction
{
public readonly string property;
public readonly string comparer;
public readonly string value;
public AttachmentRestriction(string prop, string comp, string val)
{
property = prop;
comparer = comp;
value = val;
}
public bool RestrictionMet(Part part)
{
object val;
var type = part.GetType();
var prop = type.GetProperty(property);
if (prop != null)
{
val = prop.GetValue(part, null);
}
else
{
val = type.GetField(property).GetValue(part);
}
switch (val.GetType().ToString())
{
case "System.Int32":
System.Int32 i32v = (System.Int32)val;
System.Int32 i32t = System.Int32.Parse(value);
switch (comparer)
{
case ">": return i32v > i32t;
case "<": return i32v < i32t;
case "!=": return i32v != i32t;
case "==": return i32v == i32t;
case ">=": return i32v >= i32t;
case "<=": return i32v <= i32t;
default: ModuleAttacher.print("unknown comparer for " + val.GetType() + ": " + comparer); return false;
}
default:
ModuleAttacher.print("unknown value type: " + val.GetType());
return false;
}
}
}
public class Attachment
{
public readonly string attachTo;
public readonly string moduleName;
public readonly List<AttachmentRestriction> restrictions = new List<AttachmentRestriction>();
public Attachment(string attachModule, string attachTarget)
{
moduleName = attachModule;
attachTo = attachTarget;
ModuleAttacher.print("new attachment: " + moduleName + " for " + attachTo);
if (attachTo.Contains("["))
{
attachTo = attachTarget.Remove(attachTarget.IndexOf("["));
attachTarget = attachTarget.Substring(attachTarget.IndexOf("[") + 1);
attachTarget = attachTarget.Remove(attachTarget.IndexOf("]"));
string[] rests = attachTarget.Split("\"".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
restrictions.Add(new AttachmentRestriction(rests[0], rests[1], rests[2]));
}
}
public bool RestrictionsMet(Part part)
{
foreach (AttachmentRestriction rest in restrictions)
{
if (!rest.RestrictionMet(part))
{
return false;
}
}
return true;
}
}
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class ModuleAttacher : MonoBehaviour
{
private Vessel last;
private List<Attachment> attachments;
private void LoadCFG()
{
attachments = new List<Attachment>();
string file = "ModuleAttacher.cfg";
if (KSP.IO.File.Exists<ModuleAttacher>(file))
{
string[] ls = KSP.IO.File.ReadAllLines<ModuleAttacher>(file);
foreach (string l in ls)
{
string module = l.Substring(0, l.IndexOf(":"));
string target = l.Substring(l.IndexOf(":") + 1);
if (!l.StartsWith("//")) {
attachments.Add(new Attachment(module, target));
}
// string[] pcs = l.Split(':');
// if (pcs.Length >= 2)
// {
// string part = "";
// for (int i = 1; i < pcs.Length; i++)
// {
// part += ((i > 1) ? ":" : "") + pcs[i];
// }
// attachments.Add(new Attachment(pcs[0], part));
// }
}
print(attachments.Count + " attachments loaded");
}
else {
KSP.IO.File.WriteAllText<ModuleAttacher>("//ModuleToAttach:ModuleToLookFor\n", file);
}
}
private bool Attach(Part part)
{
bool attached = false;
List<string> toAttach = new List<string>();
// print("scanning " + part.name + " for needed attachments");
foreach (PartModule pm in part.Modules)
{
foreach (Attachment attach in attachments.FindAll(a => a.attachTo == pm.moduleName && !part.Modules.Contains(a.moduleName)))
{
if (attach.RestrictionsMet(part))
{
toAttach.Add(attach.moduleName);
}
}
}
foreach (string a in toAttach)
{
print("Attaching " + a + " to " + part.name);
var pm = part.AddModule(a);
attached = true;
}
return attached;
}
public void Update()
{
if (attachments == null)
{
LoadCFG();
}
if (!HighLogic.LoadedSceneIsFlight)
{
return;
}
var ship = FlightGlobals.ActiveVessel;
// try {
if (ship != null && ship.state == Vessel.State.ACTIVE && last != ship) {
foreach (Part p in ship.Parts) {
Attach(p);
}
last = ship;
}
// }
// catch (Exception ex) {
// print(ex.Message);
// }
}
public static void print(string message)
{
MonoBehaviour.print("ModuleAttacher: " + message);
}
}