-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathActorBaker.cs
More file actions
240 lines (218 loc) · 12 KB
/
ActorBaker.cs
File metadata and controls
240 lines (218 loc) · 12 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UAssetAPI;
using UAssetAPI.PropertyTypes;
using UAssetAPI.StructTypes;
namespace AstroModIntegrator
{
public class ActorBaker
{
private readonly Export refData1B; // ObjectProperty
private readonly Export refData2B; // Template category
private readonly Export refData3B; // SCS_Node
public ActorBaker()
{
UAsset y = new UAsset(IntegratorUtils.EngineVersion);
y.Read(new AssetBinaryReader(new MemoryStream(Properties.Resources.ActorTemplate), y));
refData1B = y.Exports[6];
refData2B = y.Exports[5];
refData3B = y.Exports[10];
}
public UAsset Bake(string[] newComponents, byte[] superRawData)
{
UAsset y = new UAsset(IntegratorUtils.EngineVersion);
y.UseSeparateBulkDataFiles = true;
y.Read(new AssetBinaryReader(new MemoryStream(superRawData), y));
int scsLocation = -1;
int bgcLocation = -1;
int cdoLocation = -1;
int nodeOffset = 0;
for (int i = 0; i < y.Exports.Count; i++)
{
Export baseUs = y.Exports[i];
if (baseUs is NormalExport)
{
switch (baseUs.ClassIndex.IsImport() ? baseUs.ClassIndex.ToImport(y).ObjectName.Value.Value : string.Empty)
{
case "SimpleConstructionScript":
scsLocation = i;
break;
case "BlueprintGeneratedClass":
bgcLocation = i;
break;
case "SCS_Node":
nodeOffset++;
break;
}
if (baseUs.ObjectFlags.HasFlag(EObjectFlags.RF_ClassDefaultObject)) cdoLocation = i;
}
}
if (scsLocation < 0) throw new FormatException("Unable to find SimpleConstructionScript");
if (bgcLocation < 0) throw new FormatException("Unable to find BlueprintGeneratedClass");
if (cdoLocation < 0) throw new FormatException("Unable to find CDO");
int objectPropertyLink = y.SearchForImport(new FName("/Script/CoreUObject"), new FName("Class"), new FName("ObjectProperty"));
int objectPropertyLink2 = y.SearchForImport(new FName("/Script/CoreUObject"), new FName("ObjectProperty"), new FName("Default__ObjectProperty"));
int scsNodeLink = y.SearchForImport(new FName("/Script/CoreUObject"), new FName("Class"), new FName("SCS_Node"));
int scsNodeLink2 = y.SearchForImport(new FName("/Script/Engine"), new FName("SCS_Node"), new FName("Default__SCS_Node"));
byte[] noneRef = BitConverter.GetBytes((long)y.SearchNameReference(FString.FromString("None")));
y.AddNameReference(new FString("bAutoActivate"));
foreach (string componentPathRaw in newComponents)
{
Export refData1 = (Export)refData1B.Clone();
Export refData2 = (Export)refData2B.Clone();
Export refData3 = (Export)refData3B.Clone();
string componentPath = componentPathRaw;
string component = Path.GetFileNameWithoutExtension(componentPathRaw);
if (componentPathRaw.Contains("."))
{
string[] tData = componentPathRaw.Split(new char[] { '.' });
componentPath = tData[0];
component = tData[1].Remove(tData[1].Length - 2);
}
y.AddNameReference(new FString(componentPath));
y.AddNameReference(new FString("Default__" + component + "_C"));
y.AddNameReference(new FString(component + "_C"));
y.AddNameReference(new FString(component + "_GEN_VARIABLE"));
y.AddNameReference(new FString(component));
y.AddNameReference(new FString("SCS_Node"));
Import firstLink = new Import("/Script/CoreUObject", "Package", FPackageIndex.FromRawIndex(0), componentPath);
FPackageIndex bigFirstLink = y.AddImport(firstLink);
Import newLink = new Import("/Script/Engine", "BlueprintGeneratedClass", bigFirstLink, component + "_C");
FPackageIndex bigNewLink = y.AddImport(newLink);
Import newLink2 = new Import(componentPath, component + "_C", bigFirstLink, "Default__" + component + "_C");
FPackageIndex bigNewLink2 = y.AddImport(newLink2);
refData2.ClassIndex = bigNewLink;
refData2.ObjectName = new FName(component + "_GEN_VARIABLE");
refData2.TemplateIndex = bigNewLink2;
refData1.ClassIndex = FPackageIndex.FromRawIndex(objectPropertyLink);
refData1.ObjectName = new FName(component);
refData1.TemplateIndex = FPackageIndex.FromRawIndex(objectPropertyLink2);
refData3.ClassIndex = FPackageIndex.FromRawIndex(scsNodeLink);
refData3.ObjectName = new FName("SCS_Node");
refData3.TemplateIndex = FPackageIndex.FromRawIndex(scsNodeLink2);
List<byte> rawData = new List<byte>();
// Here we specify the raw data for our ObjectProperty category, including necessary flags and such
rawData.AddRange(noneRef);
rawData.AddRange(new byte[] {
0x00,
0x00,
0x00,
0x00,
0x01,
0x00,
0x00,
0x00,
0x04,
0x00,
0x00,
0x00,
0x04,
0x00,
0x00,
0x00,
});
rawData.AddRange(noneRef);
rawData.Add((byte)0);
rawData.AddRange(BitConverter.GetBytes(bigNewLink.Index));
refData1.OuterIndex = FPackageIndex.FromRawIndex(bgcLocation + 1); // BlueprintGeneratedClass category
refData2.OuterIndex = FPackageIndex.FromRawIndex(bgcLocation + 1); // BlueprintGeneratedClass category
refData3.OuterIndex = FPackageIndex.FromRawIndex(scsLocation + 1);
// Note that category links are set to one more than you'd think since categories in the category list index from 1 instead of 0
// First we add the template category
NormalExport templateCat = refData2.ConvertToChildExport<NormalExport>();
templateCat.SerializationBeforeSerializationDependencies.Add(FPackageIndex.FromRawIndex(bgcLocation + 1));
templateCat.SerializationBeforeCreateDependencies.Add(bigNewLink);
templateCat.SerializationBeforeCreateDependencies.Add(bigNewLink2);
templateCat.CreateBeforeCreateDependencies.Add(FPackageIndex.FromRawIndex(bgcLocation + 1));
templateCat.Extras = new byte[4] { 0, 0, 0, 0 };
templateCat.Data = new List<PropertyData>
{
new BoolPropertyData(new FName("bAutoActivate"))
{
Value = true
}
};
y.Exports.Add(templateCat);
NormalExport cdoCategory = (NormalExport)y.Exports[cdoLocation];
cdoCategory.SerializationBeforeSerializationDependencies.Add(FPackageIndex.FromRawIndex(y.Exports.Count));
// Then the ObjectProperty category
RawExport objectCat = refData1.ConvertToChildExport<RawExport>();
objectCat.CreateBeforeSerializationDependencies.Add(bigNewLink);
objectCat.CreateBeforeCreateDependencies.Add(FPackageIndex.FromRawIndex(bgcLocation + 1));
objectCat.Extras = new byte[0];
objectCat.Data = rawData.ToArray();
y.Exports.Add(objectCat);
// Then the SCS_Node
NormalExport scsCat = refData3.ConvertToChildExport<NormalExport>();
scsCat.ObjectName = new FName("SCS_Node", ++nodeOffset);
scsCat.Extras = new byte[4] { 0, 0, 0, 0 };
scsCat.CreateBeforeSerializationDependencies.Add(bigNewLink);
scsCat.CreateBeforeSerializationDependencies.Add(FPackageIndex.FromRawIndex(y.Exports.Count - 1));
scsCat.SerializationBeforeCreateDependencies.Add(FPackageIndex.FromRawIndex(scsNodeLink));
scsCat.SerializationBeforeCreateDependencies.Add(FPackageIndex.FromRawIndex(scsNodeLink2));
scsCat.CreateBeforeCreateDependencies.Add(FPackageIndex.FromRawIndex(scsLocation + 1));
scsCat.Data = new List<PropertyData>
{
new ObjectPropertyData(new FName("ComponentClass"))
{
Value = bigNewLink
},
new ObjectPropertyData(new FName("ComponentTemplate"))
{
Value = FPackageIndex.FromRawIndex(y.Exports.Count - 1) // the first NormalCategory
},
new StructPropertyData(new FName("VariableGuid"), new FName("Guid"))
{
Value = new List<PropertyData>
{
new GuidPropertyData(new FName("VariableGuid"))
{
Value = Guid.NewGuid()
}
}
},
new NamePropertyData(new FName("InternalVariableName"))
{
Value = new FName(component)
}
};
y.Exports.Add(scsCat);
// We update the BlueprintGeneratedClass data to include our new ActorComponent
FPackageIndex[] oldData = ((StructExport)y.Exports[bgcLocation]).Children;
FPackageIndex[] newData = new FPackageIndex[oldData.Length + 1];
Array.Copy(oldData, 0, newData, 0, oldData.Length);
newData[oldData.Length] = FPackageIndex.FromRawIndex(y.Exports.Count - 1); // the RawCategory
((StructExport)y.Exports[bgcLocation]).Children = newData;
// Here we update the SimpleConstructionScript so that the parser constructs our new ActorComponent
NormalExport scsCategory = (NormalExport)y.Exports[scsLocation];
scsCategory.CreateBeforeSerializationDependencies.Add(FPackageIndex.FromRawIndex(y.Exports.Count));
cdoCategory.SerializationBeforeSerializationDependencies.Add(FPackageIndex.FromRawIndex(y.Exports.Count));
for (int j = 0; j < scsCategory.Data.Count; j++)
{
PropertyData bit = scsCategory.Data[j];
if (bit is ArrayPropertyData)
{
switch (bit.Name.Value.Value)
{
case "AllNodes":
case "RootNodes":
PropertyData[] ourArr = ((ArrayPropertyData)bit).Value;
int oldSize = ourArr.Length;
Array.Resize(ref ourArr, oldSize + 1);
refData3.ObjectName = new FName(refData3.ObjectName.Value, oldSize + 2);
ourArr[oldSize] = new ObjectPropertyData(bit.Name)
{
Value = FPackageIndex.FromRawIndex(y.Exports.Count) // the SCS_Node
};
((ArrayPropertyData)bit).Value = ourArr;
break;
}
}
}
}
return y;
}
}
}