-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCraftLoader.cs
More file actions
249 lines (234 loc) · 11 KB
/
Copy pathCraftLoader.cs
File metadata and controls
249 lines (234 loc) · 11 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using UnityEngine;
using System.IO;
namespace PersistentTrails
{
public static class CraftLoader
{
public static List<PartValue> getParts(Vessel vessel, bool fetchModel)
{
List<PartValue> partList = new List<PartValue>();
Vector3 rootPosition;
Quaternion rootRotation;
Transform referenceFrame = new GameObject().transform;
Transform localTransform = new GameObject().transform;
localTransform.parent = referenceFrame;
if (vessel != null)
{
if (vessel.parts.Count > 0)
{
//Debug.Log("vessel parts: " + vessel.parts.Count);
rootPosition = vessel.parts[0].transform.position;
rootRotation = vessel.parts[0].transform.rotation;
Quaternion worldUp = Quaternion.Euler((vessel.rigidbody.position - vessel.mainBody.position).normalized);
referenceFrame.position = vessel.transform.position;
referenceFrame.rotation = vessel.transform.rotation;
foreach (Part part in vessel.parts)
{
if (part.name == "launchClamp1" || part.partName == "StrutConnector" || part.partName == "FuelLine")
{
Utilities.debug.debugMessage("Excluding part from crf file: " + part.name);
}
else
{
PartValue newPartValue = new PartValue();
newPartValue.scale = 1f / part.scaleFactor;
localTransform.rotation = part.transform.rotation;
localTransform.position = part.transform.position;
newPartValue.position = localTransform.localPosition;
newPartValue.rotation = localTransform.localRotation;
newPartValue.partName = part.name.Split(' ')[0];
if (fetchModel) newPartValue.model = findPartModel(newPartValue.partName);
partList.Add(newPartValue);
}
}
}
}
return partList;
}
private static string serialize()
{
StringBuilder output = new StringBuilder();
output.AppendLine(Utilities.craftFileFormat.ToString());
foreach (PartValue value in getParts(FlightGlobals.ActiveVessel, false))
{
output.AppendLine(value.partName);
output.AppendLine(Utilities.Vector3ToString(value.position));
output.AppendLine(Utilities.QuaternionToString(value.rotation));
output.AppendLine(value.scale.ToString());
}
output.Append("[EOF]");
return output.ToString();
}
public static void saveCraftToFile()
{
StreamWriter stream = new StreamWriter(Utilities.CraftPath + FlightGlobals.ActiveVessel.vesselName + ".crf");
stream.WriteLine(serialize());
stream.Close();
}
public static List<PartValue> loadCraftFromFile(string fileName)
{
List<PartValue> loadedList = new List<PartValue>();
PartValue newValue = new PartValue();
StreamReader stream = new StreamReader(fileName); // exceptions handled by assembleCraft
string newLine = string.Empty;
int craftFileFormat = 0;
int.TryParse(stream.ReadLine(), out craftFileFormat);
Utilities.debug.debugMessage(String.Concat("Loading crf file, format ", craftFileFormat, ", ", fileName));
try
{
while (!stream.EndOfStream && !(newLine == "[EOF]"))
{
newLine = stream.ReadLine();
newValue.partName = newLine;
newValue.position = Utilities.parseVector3(stream.ReadLine());
newValue.rotation = Utilities.parseQuaternion(stream.ReadLine());
float.TryParse(stream.ReadLine(), out newValue.scale);
//Debug.Log("finding model " + newValue.partName);
newValue.model = findPartModel(newValue.partName);
//Debug.Log("model null? " + (newValue.model == null));
loadedList.Add(newValue.clone());
}
}
catch (Exception e)
{
//Utilities.debug.debugMessage("load craft file error: " + e.ToString());
Utilities.debug.debugMessage("Got expected exception from streamreader, part list done");
}
return loadedList;
}
public static GameObject assembleCraft(string craftName, bool collidersOn) // --- craftName not actually used yet. This should take a saved craft file name as input ---
{
GameObject craft = new GameObject();
Utilities.debug.debugMessage("asembling craft " + craftName);
List<PartValue> pvList;
//List<PartValue> pvList = getParts(FlightGlobals.ActiveVessel, true); // load the craft file here into a partValue list
try
{
pvList = loadCraftFromFile(craftName);
}
catch
{
throw new FileNotFoundException("error loading craft from file", craftName);
}
foreach (PartValue pv in pvList)
{
//Debug.Log("pv.partName is " + pv.partName);
pv.model.SetActive(true);
//Debug.Log("pv.model exists");
pv.model.transform.parent = craft.transform;
pv.model.transform.localPosition = pv.position;
pv.model.transform.localRotation = pv.rotation;
if (pv.scale > 7f) pv.scale /= 10f;
if (pv.scale > 7f) pv.scale /= 10f; // twice to catch both 0.01 scale parts, and 0.1 scales. Gotta find a better way. Need to read the part cfg scale
pv.model.transform.localScale = new Vector3(pv.scale, pv.scale, pv.scale);
//Debug.Log("Part: " + pv.partName + "Scale: " + pv.scale + "/" + pv.model.transform.localScale);
//Debug.Log("Part: " + pv.position);
//Debug.Log("Part: " + pv.rotation);
//Debug.Log("Part: " + pv.scale);
}
setColliderStateInChildren(craft, collidersOn);
setLightStateInChildren(craft, false);
return craft;
}
public static GameObject findPartModel(string partName)
{
UrlDir.UrlConfig[] cfg = GameDatabase.Instance.GetConfigs("PART");
//Debug.Log("looping through " + cfg.Length);
for (int i = 0; i < cfg.Length; i++)
{
string modfiedPartName = partName.Replace('.', '_');
if (modfiedPartName == cfg[i].name)
{
//Debug.Log("found this part: " + cfg[i].url);
//float scale = 0.1337f;
//float.TryParse(cfg[i].config.GetValue("scale"), out scale);
//Debug.Log("scale: " + scale);
string modelPath = cfg[i].parent.parent.url + "/" + "model";
//Debug.Log("model path: " + modelPath);
GameObject newModel = GameDatabase.Instance.GetModel(modelPath);
if (newModel == null)
{
//Debug.Log("model load error, fetching first model available");
newModel = GameDatabase.Instance.GetModelIn(cfg[i].parent.parent.url);
return newModel;
//return new PartValue(newModel, scale);
}
else
{
//Debug.Log("newModel not null");
return newModel;
}
}
}
Utilities.debug.debugMessage("Finding model " + partName + " failed, returning blank GameObject");
return new GameObject();
}
public static void setColliderStateInChildren(GameObject rootObject, bool newValue)
{
//disable colliders by setting them to isTrigger so you can still run code on them
Collider[] colliders = rootObject.GetComponentsInChildren<Collider>(true);
for (int i = 0; i < colliders.Length; i++)
{
colliders[i].isTrigger = !newValue;
colliders[i].material = getPhysicMaterial();
}
}
public static void setLightStateInChildren(GameObject rootObject, bool newValue)
{
Light[] lights = rootObject.GetComponentsInChildren<Light>(true);
for (int i = 0; i < lights.Length; i++)
{
lights[i].enabled = newValue;
}
}
private static PhysicMaterial ghostPhysicMaterial;
public static PhysicMaterial getPhysicMaterial()
{
if (ghostPhysicMaterial == null)
{
ghostPhysicMaterial = new PhysicMaterial("ghostPhysicMat");
ghostPhysicMaterial.dynamicFriction = 0.3f;
ghostPhysicMaterial.dynamicFriction2 = 0.3f;
ghostPhysicMaterial.frictionCombine = PhysicMaterialCombine.Average;
ghostPhysicMaterial.staticFriction = 0.3f;
ghostPhysicMaterial.staticFriction2 = 0.3f;
}
return ghostPhysicMaterial;
}
//public static void setColliderState(GameObject targetObject)
//{
//}
}
public class PartValue
{
public string partName;
public GameObject model;
public Vector3 position;
public Quaternion rotation;
//public Quaternion attachRotation;
public float scale;
public PartValue(GameObject _model, float _scale)
{
model = _model;
scale = _scale;
}
public PartValue()
{
}
public PartValue clone()
{
PartValue cloneValue = new PartValue();
cloneValue.partName = partName;
cloneValue.position = position;
cloneValue.rotation = rotation;
cloneValue.scale = scale;
cloneValue.model = model;
return cloneValue;
}
}
}