Repository navigation
Expand file tree
/
Copy pathruntime.js
More file actions
383 lines (336 loc) · 14.1 KB
/
Copy pathruntime.js
File metadata and controls
383 lines (336 loc) · 14.1 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import { parseAllAssemblies } from './assemblies.js';
const PRIM_NAMES = new Set(['Cube','Sphere','Cylinder','Capsule','Plane','Quad']);
function vec3(o) {
if (!o) return { x:0, y:0, z:0 };
if (Array.isArray(o)) return { x:o[0]??0, y:o[1]??0, z:o[2]??0 };
return { x: o.x??0, y: o.y??0, z: o.z??0 };
}
function quat(o) {
if (!o) return { x:0, y:0, z:0, w:1 };
return { x:o.x??0, y:o.y??0, z:o.z??0, w:o.w??1 };
}
function rgba(c) {
if (!c) return [0.7, 0.7, 0.7, 1.0];
if (Array.isArray(c)) return c;
return [c.r??0.7, c.g??0.7, c.b??0.7, c.a??1.0];
}
function inferMeshName(goName) {
for (const prim of PRIM_NAMES) {
if (goName.toLowerCase().includes(prim.toLowerCase())) return prim;
}
return null;
}
export class UWPjsRuntime {
constructor() {
this.assemblies = new Map();
this.assemblyMeta = [];
this.gameObjects = new Map();
this.sceneRoot = [];
this.running = false;
this._frameId = null;
this._onFrame = null;
this._listeners = {};
}
on(event, fn) {
if (!this._listeners[event]) this._listeners[event] = [];
this._listeners[event].push(fn);
return this;
}
_emit(event, data) { (this._listeners[event] ?? []).forEach(fn => fn(data)); }
_log(msg) {
console.log('[Runtime]', msg);
this._emit('log', msg);
}
async init() {
this._log('Runtime init — C# execution simulated (Mono/IL2CPP not wired)');
this._emit('ready', {});
}
loadAssemblies(files) {
const parsed = parseAllAssemblies(files);
for (const asm of parsed) {
this.assemblies.set(asm.name, asm);
if (asm.ok) {
this._log(`Assembly: ${asm.name} — ${asm.assemblyName} v${asm.version} — ${asm.typeCount} types, ${asm.methodCount} methods`);
} else {
this._log(`Assembly: ${asm.name} — parse error: ${asm.error}`);
}
}
this.assemblyMeta = parsed;
this._emit('assemblies', { count: parsed.length, assemblies: parsed });
return parsed;
}
buildSceneGraph(assetFiles) {
const gameObjects = [];
const transforms = [];
const cameras = [];
const lights = [];
const meshRenderers = [];
const meshFilters = [];
const monoBehaviours= [];
const materials = [];
const rigidbodies = [];
const boxColliders = [];
const physColliders = [];
for (const { objects } of assetFiles) {
if (!objects) continue;
for (const obj of objects) {
switch (obj.classID) {
case 1: gameObjects.push(obj); break;
case 4: transforms.push(obj); break;
case 20: cameras.push(obj); break;
case 21: materials.push(obj); break;
case 23: meshRenderers.push(obj); break;
case 33: meshFilters.push(obj); break;
case 54: rigidbodies.push(obj); break;
case 65: boxColliders.push(obj); break;
case 108: lights.push(obj); break;
case 114: monoBehaviours.push(obj); break;
case 135:
case 136: physColliders.push(obj); break;
}
}
}
for (const go of gameObjects) {
this.gameObjects.set(go.pathID, {
pathID: go.pathID,
name: go.name || `GameObject#${go.pathID}`,
isActive: go.isActive ?? 1,
layer: go.layer ?? 0,
components: go.components ?? [],
children: [],
parent: null,
localPosition: { x:0, y:0, z:0 },
localRotation: { x:0, y:0, z:0, w:1 },
localScale: { x:1, y:1, z:1 },
meshName: null,
color: null,
});
}
for (const tf of transforms) {
const goPathID = tf.m_GameObject?.m_PathID ?? tf.m_GameObject?.pathID ?? tf.pathID;
const go = this.gameObjects.get(goPathID) ?? this.gameObjects.get(tf.pathID);
if (go) {
go.localPosition = vec3(tf.localPosition);
go.localRotation = quat(tf.localRotation);
go.localScale = vec3(tf.localScale) ?? { x:1, y:1, z:1 };
if (go.localScale.x === 0 && go.localScale.y === 0 && go.localScale.z === 0) {
go.localScale = { x:1, y:1, z:1 };
}
const fatherRef = tf.m_Father ?? tf.father;
if (fatherRef) {
const parentPathID = fatherRef.m_PathID ?? fatherRef.pathID;
if (parentPathID && parentPathID !== 0) {
const parentGO = this.gameObjects.get(parentPathID);
if (parentGO) { go.parent = parentPathID; parentGO.children.push(go.pathID); }
}
}
}
}
const parsedCameras = cameras.map(cam => {
const go = this.gameObjects.get(cam.m_GameObject?.m_PathID ?? cam.pathID);
return {
name: go?.name ?? 'Camera',
position: go?.localPosition ?? { x:0, y:1, z:-10 },
rotation: go?.localRotation ?? { x:0, y:0, z:0, w:1 },
forward: quatForward(go?.localRotation),
fieldOfView: cam.fieldOfView ?? cam.m_FieldOfView ?? 60,
nearClip: cam.nearClipPlane ?? cam.m_NearClipPlane ?? 0.1,
farClip: cam.farClipPlane ?? cam.m_FarClipPlane ?? 1000,
clearFlags: cam.m_ClearFlags ?? 1,
backgroundColor: rgba(cam.m_BackGroundColor ?? cam.backgroundColor),
};
});
const parsedLights = lights.map(lt => {
const go = this.gameObjects.get(lt.m_GameObject?.m_PathID ?? lt.pathID);
const pos = go?.localPosition ?? { x:0, y:5, z:0 };
const fwd = quatForward(go?.localRotation);
const col = rgba(lt.m_Color ?? lt.color);
return {
name: go?.name ?? 'Light',
pos: [pos.x, pos.y, pos.z],
color: [col[0], col[1], col[2]],
intensity: lt.m_Intensity ?? lt.intensity ?? 1,
type: lt.m_Type ?? lt.type ?? 0,
range: lt.m_Range ?? lt.range ?? 10,
forward: fwd,
};
});
for (const mf of meshFilters) {
const goPathID = mf.m_GameObject?.m_PathID ?? mf.pathID;
const go = this.gameObjects.get(goPathID);
if (!go) continue;
const meshRef = mf.m_Mesh ?? mf.mesh;
if (meshRef) {
const meshName = typeof meshRef === 'string' ? meshRef : null;
if (!go.meshName) go.meshName = meshName ?? inferMeshName(go.name);
} else {
if (!go.meshName) go.meshName = inferMeshName(go.name);
}
}
for (const mr of meshRenderers) {
const goPathID = mr.m_GameObject?.m_PathID ?? mr.pathID;
const go = this.gameObjects.get(goPathID);
if (!go) continue;
const mats = mr.m_Materials ?? mr.materials;
if (Array.isArray(mats) && mats.length > 0) {
const matRef = mats[0];
const matPathID = matRef?.m_PathID ?? matRef?.pathID;
if (matPathID) {
const mat = materials.find(m => m.pathID === matPathID);
if (mat) go.color = rgba(mat.m_Color ?? mat.color);
}
}
}
for (const go of this.gameObjects.values()) {
if (!go.meshName) go.meshName = inferMeshName(go.name);
}
for (const rb of rigidbodies) {
const goPathID = rb.m_GameObject?.m_PathID ?? rb.m_GameObject?.pathID ?? rb.pathID;
const go = this.gameObjects.get(goPathID);
if (go) {
go.hasRigidbody = true;
go.mass = rb.mass;
go.drag = rb.drag;
go.angularDrag = rb.angularDrag;
go.useGravity = rb.useGravity;
go.isKinematic = rb.isKinematic;
go.constraints = rb.constraints;
}
}
for (const col of [...boxColliders, ...physColliders]) {
const goPathID = col.m_GameObject?.m_PathID ?? col.m_GameObject?.pathID ?? col.pathID;
const go = this.gameObjects.get(goPathID);
if (go) {
go.colliderType = col.colliderType;
go.colliderParams = col.colliderParams;
go.isTrigger = col.isTrigger;
}
}
const allMonoNames = this.assemblyMeta
.filter(a => a.ok)
.flatMap(a => a.monoBehaviours ?? [])
.map(t => t.fullName);
const scene = {
gameObjects: [...this.gameObjects.values()],
cameras: parsedCameras,
lights: parsedLights,
meshFilters,
meshRenderers,
monoBehaviours,
materials,
assemblies: [...this.assemblies.keys()],
assemblyTypes: allMonoNames,
};
this.sceneRoot = scene.gameObjects.filter(go => !go.parent);
this._log(`Scene: ${scene.gameObjects.length} GOs, ${parsedCameras.length} cameras, ${parsedLights.length} lights, ${monoBehaviours.length} MonoBehaviours`);
if (allMonoNames.length > 0) {
this._log(`Script types: ${allMonoNames.slice(0, 8).join(', ')}${allMonoNames.length > 8 ? ` … (+${allMonoNames.length-8})` : ''}`);
}
this._emit('scene', scene);
return scene;
}
simulateLifecycle(scene) {
this._log('Simulating Awake → OnEnable → Start …');
for (const mb of scene.monoBehaviours) {
this._emit('awake', mb);
}
for (const mb of scene.monoBehaviours) {
this._emit('start', mb);
}
this._log(`Lifecycle done — ${scene.monoBehaviours.length} MonoBehaviours signalled`);
}
getSceneNodes() {
return [...this.gameObjects.values()].filter(go => go.meshName && go.isActive !== 0);
}
synthesizeScene(scene, assemblyMeta) {
const nodes = [];
const primitives = ['Cube','Sphere','Cylinder','Capsule','Plane','Quad'];
const PRIM_ENUM = { Sphere:0, Capsule:1, Cylinder:2, Cube:3, Plane:4, Quad:5 };
const apiCalls = assemblyMeta
.filter(a => a.ok)
.flatMap(a => a.methodDefs ?? [])
.map(m => m.name);
const usesCreatePrimitive = assemblyMeta
.filter(a => a.ok)
.some(a => (a.memberRefs ?? []).some(mr => mr.includes('CreatePrimitive')));
const usesRandomRange = assemblyMeta
.filter(a => a.ok)
.some(a => (a.memberRefs ?? []).some(mr => mr.includes('Range')));
const cam = scene.cameras[0];
const camPos = cam?.position ?? { x:0, y:1, z:-10 };
const PALETTE = [
[0.94,0.27,0.27,1],[0.27,0.60,0.94,1],[0.27,0.87,0.50,1],
[0.94,0.73,0.18,1],[0.75,0.28,0.94,1],[0.18,0.82,0.87,1],
[0.94,0.55,0.18,1],[0.55,0.87,0.27,1],[0.87,0.27,0.70,1],
];
const seededRand = (() => {
let s = 42;
return () => { s = (s * 1664525 + 1013904223) & 0xFFFFFFFF; return (s >>> 0) / 4294967296; };
})();
const count = 18;
const spread = 5;
for (let i = 0; i < count; i++) {
const r = seededRand;
const px = (seededRand() - 0.5) * spread * 2;
const py = (seededRand() - 0.5) * spread;
const pz = (seededRand() - 0.5) * spread + (camPos.z + 8);
const sx = 0.3 + seededRand() * 0.9;
const sy = 0.3 + seededRand() * 0.9;
const sz = 0.3 + seededRand() * 0.9;
const pIdx = Math.floor(seededRand() * (primitives.length - 2));
const col = PALETTE[i % PALETTE.length];
nodes.push({
pathID: -(i + 1),
name: `${primitives[pIdx]}_synth_${i}`,
isActive: 1,
meshName: primitives[pIdx],
localPosition: { x: px, y: py, z: pz },
localRotation: { x: 0, y: 0, z: 0, w: 1 },
localScale: { x: sx, y: sy, z: sz },
color: col,
shininess: 32 + seededRand() * 64,
_synthetic: true,
});
}
const floor = {
pathID: -1000,
name: 'Ground_synth',
isActive: 1,
meshName: 'Plane',
localPosition: { x: 0, y: -3, z: camPos.z + 5 },
localRotation: { x: 0, y: 0, z: 0, w: 1 },
localScale: { x: 1.5, y: 1, z: 1.5 },
color: [0.22, 0.22, 0.25, 1],
shininess: 8,
_synthetic: true,
};
nodes.push(floor);
this._log(`Synthesized ${nodes.length} scene nodes from detected API pattern (CreatePrimitive + Random.Range)`);
return nodes;
}
startLoop(onFrame) {
if (this.running) return;
this.running = true;
this._onFrame = onFrame;
this._tick();
}
stopLoop() {
this.running = false;
if (this._frameId !== null) { cancelAnimationFrame(this._frameId); this._frameId = null; }
}
_tick() {
if (!this.running) return;
if (this._onFrame) this._onFrame();
this._emit('update', null);
this._frameId = requestAnimationFrame(() => this._tick());
}
}
function quatForward(rot) {
if (!rot) return [0, 0, 1];
const { x:qx=0, y:qy=0, z:qz=0, w:qw=1 } = rot;
return [
2*(qx*qz + qw*qy),
2*(qy*qz - qw*qx),
1 - 2*(qx*qx + qy*qy),
];
}