forked from Nivekk/KOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkOSProcessor.cs
217 lines (172 loc) · 5.79 KB
/
kOSProcessor.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
using KSP.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEngine;
namespace kOS
{
public class kOSProcessor : PartModule
{
public CPU cpu;
public Harddisk hardDisk = null;
private int vesselPartCount = 0;
private List<kOSProcessor> sisterProcs = new List<kOSProcessor>();
private Dictionary<uint, uint> partIdentifiers;
private static int MemSize = 10000;
private static int cpuIdMax;
[KSPEvent(guiActive = true, guiName = "Open Terminal")]
public void Activate()
{
Core.OpenWindow(cpu);
}
[KSPEvent(guiActive = true, guiName = "Toggle Power")]
public void TogglePower()
{
if (cpu == null) return;
if (cpu.Mode != CPU.Modes.OFF)
{
cpu.Mode = CPU.Modes.OFF;
}
else
{
cpu.Mode = CPU.Modes.STARVED;
}
}
[KSPAction("Open Terminal", actionGroup = KSPActionGroup.None)]
public void Activate(KSPActionParam param) {
Activate();
}
[KSPAction("Toggle Power", actionGroup = KSPActionGroup.None)]
public void TogglePower(KSPActionParam param) {
TogglePower();
}
[KSPField(isPersistant = true, guiName = "kOS Unit ID", guiActive = true)]
public int UnitID = -1;
[KSPField(isPersistant = true, guiActive = false)]
public int MaxPartID = 0;
public override void OnStart(PartModule.StartState state)
{
//Do not start from editor and at KSP first loading
if (state == StartState.Editor || state == StartState.None)
{
return;
}
if (hardDisk == null) hardDisk = new Harddisk(MemSize);
initCpu();
}
public void initCpu()
{
if (cpu == null)
{
cpu = new CPU(this, "ksp");
cpu.AttachHardDisk(hardDisk);
cpu.Boot();
}
}
public void RegisterkOSExternalFunction(object[] parameters)
{
Debug.Log("*** External Function Registration Succeeded");
cpu.RegisterkOSExternalFunction(parameters);
}
private void assignPartIdentifiers()
{
foreach (Part part in vessel.parts)
{
if (!partIdentifiers.ContainsKey(part.flightID))
{
}
}
}
public static int AssignNewID()
{
int id;
PluginConfiguration config = PluginConfiguration.CreateForType<kOSProcessor>();
config.load();
id = config.GetValue<int>("CpuIDMax") + 1;
config.SetValue("CpuIDMax", id);
config.save();
return id;
}
public void Update()
{
if (cpu == null) return;
if (part.State == PartStates.DEAD)
{
cpu.Mode = CPU.Modes.OFF;
return;
}
cpu.Update(Time.deltaTime);
cpu.ProcessElectricity(this.part, TimeWarp.fixedDeltaTime);
UpdateParts();
}
public void UpdateParts()
{
// Trigger whenever the number of parts in the vessel changes (like when staging, docking or undocking)
if (vessel.parts.Count != vesselPartCount)
{
List<Volume> attachedVolumes = new List<Volume>();
attachedVolumes.Add(cpu.archive);
attachedVolumes.Add(this.hardDisk);
// Look for sister units that have newly been added to the vessel
sisterProcs.Clear();
foreach (Part part in vessel.parts)
{
kOSProcessor sisterProc;
if (part != this.part && PartIsKosProc(part, out sisterProc))
{
sisterProcs.Add(sisterProc);
attachedVolumes.Add(sisterProc.hardDisk);
}
}
cpu.UpdateVolumeMounts(attachedVolumes);
vesselPartCount = vessel.parts.Count;
}
}
public bool PartIsKosProc(Part input, out kOSProcessor proc)
{
foreach (PartModule module in input.Modules)
{
if (module is kOSProcessor)
{
proc = (kOSProcessor)module;
return true;
}
}
proc = null;
return false;
}
public override void OnFixedUpdate()
{
}
public override void OnLoad(ConfigNode node)
{
// KSP Seems to want to make an instance of my partModule during initial load
if (vessel == null) return;
foreach (ConfigNode hdNode in node.GetNodes("harddisk"))
{
Harddisk newDisk = new Harddisk(hdNode);
this.hardDisk = newDisk;
}
Debug.Log("******************************* ON LOAD ");
initCpu();
Debug.Log("******************************* CPU Inited ");
if (cpu != null) cpu.OnLoad(node);
base.OnLoad(node);
}
public override void OnSave(ConfigNode node)
{
if (hardDisk != null)
{
ConfigNode hdNode = hardDisk.Save("harddisk");
node.AddNode(hdNode);
}
if (cpu != null)
{
cpu.OnSave(node);
}
base.OnSave(node);
}
}
}