Repository navigation
Expand file tree
/
Copy pathntt-objects.js
More file actions
363 lines (363 loc) · 9.82 KB
/
Copy pathntt-objects.js
File metadata and controls
363 lines (363 loc) · 9.82 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
(function() {
let nav = "";
let navIndent = 0;
function navAdd(text) {
nav += "\n" + "\t".repeat(navIndent) + text;
}
//
let out = "";
let indent = 0;
let project = null;
let objName = null;
let parentChain = [];
let objCount = 0;
function linkObject(name) {
return `[${name}](./${name}.html)`;
}
function procDoc(str) {
str = str.trim();
if (str == "") return str;
//str = str.replace(/\[\.(\w+)\]/g, `[$1](${objName}.$1)`);
str = str.replace(/\[(\w+)\]/g, (full, name) => {
if (isObject[name]) return `[${name}](./${name}.html)`;
return full;
});
return str[0].toUpperCase() + str.substring(1);
}
function addNewLine(delta = 0) {
indent += delta;
out += "\n" + "\t".repeat(indent);
}
function addOpen(text) {
addNewLine();
out += text;
indent += 1;
}
function addClose(newLine = true) {
if (newLine) {
addNewLine(-1);
} else indent -= 1;
out += "}";
}
function add(text) {
addNewLine(0);
out += text;
}
function add2(text) {
addNewLine(0);
addNewLine(0);
out += text;
}
function addPara(text) {
for (let line of text.split("\n")) {
addNewLine(0);
out += line;
}
}
//
let builtin = Object.create(null);
for (let name of [
"x", "y",
"hspeed", "vspeed", "speed", "direction",
"alarm",
"image_index", "image_number", "image_speed", "image_single",
"image_xscale", "image_yscale", "image_angle",
"image_blend", "image_alpha"
]) builtin[name] = true;
//
let objectDocs = Object.create(null);
let isObject = Object.create(null);
const rxDocLine = /(\w+)\s*=.+?\/\/\/?(.+)/g;
const rxDocBlock = /\*\/[ \t*](\w+)\s*=/g; // `*/ name =`
const rxStartLine = /^\s*\/\/\/\s*(?:@desc(?:ription)?\s*)?(.+)/;
const rxStartBlock = /^\s*\/\*\*?(?:\/\/)?([\s\S]+?)\*?\*\//;
function parseDocs(gml) {
const extraDocs = Object.create(null);
//
let mt = rxStartLine.exec(gml);
if (mt) extraDocs[""] = procDoc(mt[1]);
//
mt = rxStartBlock.exec(gml);
if (mt) extraDocs[""] = procDoc(mt[1]);
//
rxDocLine.lastIndex = 0;
while (mt = rxDocLine.exec(gml)) {
let doc = mt[2];
if (doc.includes("@is") || doc.includes("@hint")) continue;
extraDocs[mt[1]] = procDoc(doc);
}
//
rxDocBlock.lastIndex = 0;
while (mt = rxDocBlock.exec(gml)) {
let start = gml.lastIndexOf("/*", mt.index);
let snip = gml.substring(start + 2, mt.index);
if (snip.startsWith("*")) snip = snip.substring(1);
if (snip.endsWith("*")) snip = snip.substring(0, snip.length - 1);
extraDocs[mt[1]] = procDoc(snip);
}
return extraDocs;
}
function indexObject() {
isObject[objName] = true;
const createPath = `objects/${objName}/Create_0.gml`;
if (project.existsSync(createPath)) {
const createCode = project.readTextFileSync(createPath);
objectDocs[objName] = parseDocs(createCode);
}
}
function indexScript(scrName) {
const scrPath = (`scripts/${scrName}/${scrName}.gml`);
if (!project.existsSync(scrPath)) {
return objectDocs[scrName] = Object.create(null);
}
const gml = project.readTextFileSync(scrPath);
const extraDocs = parseDocs(gml);
const scriptDoc = extraDocs[""];
if (scriptDoc && scriptDoc.startsWith(scrName + "(") && scriptDoc.indexOf("\n") < 0) {
extraDocs[""] = null;
}
objectDocs[scrName] = extraDocs;
return extraDocs;
}
//
let varSectionMap = Object.create(null);
let varSectionList = [];
function VarSection(name) {
this.name = name;
this.lines = [];
}
function varStart() {
varSectionMap = Object.create(null);
varSectionList = [];
}
function varEnd() {
if (varSectionList.length > 0) add2("Variables:");
for (let sct of varSectionList) {
addOpen(`#[from ${sct.name}](${sct.name}) {`);
for (line of sct.lines) add(line);
addClose();
}
}
//
const needsMdEsc = /([\[\]])/g;
function addVar(comp) {
let doc = comp.doc;
if (!doc) return false;
//
const varName = comp.name;
if (builtin[varName]) return false;
if (varName.startsWith("__")) return false;
if (doc.includes("built-in")) return false;
//
let lines = doc.split("\n");
let origin = null;
const infoPrefix = "ℹ ";
let varTitle = varName;
const isFunc = comp.meta == "function";
const preFunc = isFunc ? varTitle + "(" : "";
for (let i = 0; i < lines.length; ) {
const line = lines[i];
if (line.startsWith("from ")) {
origin = line.substring(5);
lines.splice(i, 1);
continue;
} else if (line.startsWith(infoPrefix)) {
lines[i] = procDoc(line.substring(infoPrefix.length));
} else if (line.startsWith("type ")) {
if (!isFunc) {
varTitle += ": " + line.substring(5).replace(needsMdEsc, "\\$1");
}
lines.splice(i, 1);
continue;
} else if (isFunc && line.startsWith(preFunc)) {
varTitle = line;
lines.splice(i, 1);
continue;
} else if (line == "variable definition") {
lines.splice(i, 1);
continue;
}
i += 1;
}
//
let extraDocs = objectDocs[origin];
if (extraDocs == null) {
extraDocs = indexScript(origin);
}
let extra = extraDocs ? extraDocs[varName] : null;
for (let i = parentChain.length; --i >= 0; ) {
let parName = parentChain[i];
let parDocs = objectDocs[parName];
if (parDocs) {
let ns = GmlAPI.gmlNamespaces[parName];
if (!extra) extra = parDocs[varName];
if (ns) {
let comp = ns.compInst.map[varName];
if (comp) {
origin = parName;
if (lines.length == 0 && comp.doc.includes(infoPrefix)) {
for (let line of comp.doc.split("\n")) {
if (line.startsWith(infoPrefix)) {
line = line.substring(infoPrefix.length);
lines.push(procDoc(line));
}
}
}
}
}
}
}
if (extra) {
if (extra == "!hide") return false;
for (let line of extra.split("\n")) lines.push(line);
}
//
let sct = varSectionMap[origin];
if (sct == null) {
sct = new VarSection(origin);
varSectionMap[origin] = sct;
varSectionList.push(sct);
if (origin != objName) {
if (isObject[origin]) {
sct.lines.push(`Object: ` + linkObject(origin));
} else if (extraDocs[""]) {
sct.lines.push(extraDocs[""]);
}
}
}
//
let sctLines = sct.lines;
sctLines.push(`#[${varTitle}](${varName}) {` + (lines.length > 0 ? "" : "}"));
for (let line of lines) sctLines.push("\t" + line);
if (lines.length > 0) sctLines.push(`}`);
//
return true;
}
function addObject(treeItem, chain) {
objName = treeItem.dataset.ident;
out = "";
//
parentChain = [];
let objParent = project.objectParents[objName];
while (objParent && parentChain.length < 20) {
parentChain.unshift(objParent);
objParent = project.objectParents[objParent];
}
if (!parentChain.includes("GameObject") && objName != "GameObject") return;
//
navAdd(`<a href="../objects/${objName}.html" target="page">${objName}</a>`);
//
add("```set title " + objName + "```");
//
const parentChainMd = parentChain.map(linkObject);
parentChainMd.push(linkObject(objName));
add(parentChainMd.join(" ➜ "));
//
let extraDocs = objectDocs[objName];
let extra = extraDocs ? extraDocs[""] : null;
if (extra) {
addNewLine();
addPara(extra);
}
//
let objChildren = project.objectChildren[objName];
if (objChildren) {
add2("Children: " + objChildren.map(linkObject).join(", "));
}
//
let ns = GmlAPI.gmlNamespaces[objName];
varStart();
for (let comp of ns.getInstComp(0, false)) addVar(comp);
varEnd();
//
let hasAlarms = false;
for (let i = 0; i < 12; i++) {
const alarmPath = `objects/${objName}/Alarm_${i}.gml`;
if (!project.existsSync(alarmPath)) continue;
const alarmCode = project.readTextFileSync(alarmPath);
if (!hasAlarms) {
hasAlarms = true;
addOpen("Alarms: --{");
}
let doc = parseDocs(alarmCode)[""];
let info = `- \`alarm[${i}]\``;
if (doc) info += ": " + doc;
add(info);
}
if (hasAlarms) addClose();
//
objCount += 1;
project.writeTextFileSync(`../nuclearthrone-public/auto-objects/${objName}.dmd`, out);
}
function addFolder(treeDir, chain) {
for (let treeItem of treeDir.treeItems.children) {
if (treeItem.classList.contains("dir")) {
const name = treeItem.dataset.label;
const oldNav = nav;
const oldCount = objCount;
navAdd(`<details open><summary>${name}</summary>`);
navIndent++;
addFolder(treeItem, chain.concat([name]));
navIndent--;
navAdd(`</details>`);
if (objCount == oldCount) nav = oldNav;
} else if (treeItem.dataset.kind === "object") {
addObject(treeItem, chain);
}
}
}
function run() {
project = $gmedit["gml.Project"].current;
out = "";
indent = 0;
let treeDir = document.querySelector(`#tree-td div.dir[data-rel-path="folders/Objects/"]`);
//
objectDocs = Object.create(null);
isObject = Object.create(null);
let objects = [];
for (let treeItem of treeDir.querySelectorAll(`div.item[data-kind="object"]`)) {
objects.push(treeItem.dataset.ident);
}
for (let obj of objects) isObject[obj] = true;
//
for (let obj of objects) {
objName = obj;
indexObject();
}
//
addFolder(treeDir, []);
//navigator.clipboard.writeText(dmd);
//project.writeTextFileSync("datafiles/NTT-Objects-Data.dmd", out);
let navTpl = project.readTextFileSync("../nuclearthrone-public/dmd-templates/object-nav.html");
navTpl = navTpl.replace("<!-- nav -->", nav);
project.writeTextFileSync("../nuclearthrone-public/docs/inc/object-nav.html", navTpl);
console.log("Updated NTT-Objects / " + nav.length);
return out;
}
let aceCommand = null;
let palCommand = null;
function init() {
aceCommand = {
name: "copy-ntt-objects",
exec: function() {
let dmd = run();
console.log(dmd);
//navigator.clipboard.writeText(dmd);
}
};
palCommand = {
name: "Copy NTT-Objects",
exec: "copy-ntt-objects",
};
AceCommands.add(aceCommand);
AceCommands.addToPalette(palCommand);
/*GMEdit.on("projectStateRestore", (project) => {
setTimeout(() => {
run();
}, 100);
});*/
}
GMEdit.register("ntt-objects", {
init: init
});
})();