86 lines
4.0 KiB
JavaScript
86 lines
4.0 KiB
JavaScript
// Writes script sources from src/ into a .rbxlx place without touching the map.
|
|
// Only scripts that sit directly in their service (ReplicatedFirst, ReplicatedStorage,
|
|
// ServerScriptService, StarterPlayer.StarterPlayerScripts) are updated; copies in
|
|
// ServerStorage.Backup etc. are left alone. Missing scripts are reported, not added.
|
|
// usage: node tools/sync-scripts-into-place.js in.rbxlx out.rbxlx
|
|
'use strict';
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const [,, inP, outP] = process.argv;
|
|
const root = path.join(__dirname, '..');
|
|
const dirs = {
|
|
ReplicatedFirst: 'src/ReplicatedFirst',
|
|
ReplicatedStorage: 'src/ReplicatedStorage',
|
|
ServerScriptService: 'src/ServerScriptService',
|
|
StarterPlayerScripts: 'src/StarterPlayer/StarterPlayerScripts',
|
|
};
|
|
const wanted = new Map(); // "Service/Name/Class" -> source
|
|
for (const [svc, rel] of Object.entries(dirs)) {
|
|
const d = path.join(root, rel);
|
|
if (!fs.existsSync(d)) continue;
|
|
for (const f of fs.readdirSync(d)) {
|
|
if (!f.endsWith('.lua')) continue;
|
|
let cls = 'ModuleScript', name = f.slice(0, -4);
|
|
if (f.endsWith('.server.lua')) { cls = 'Script'; name = f.slice(0, -11); }
|
|
else if (f.endsWith('.client.lua')) { cls = 'LocalScript'; name = f.slice(0, -11); }
|
|
const src = fs.readFileSync(path.join(d, f), 'utf8');
|
|
if (src.includes(']]>')) throw new Error('"]]>" not allowed in ' + f);
|
|
wanted.set(svc + '/' + name + '/' + cls, { src, file: rel + '/' + f, done: false });
|
|
}
|
|
}
|
|
|
|
const text = fs.readFileSync(inP, 'latin1'); // byte-preserving
|
|
const toBytes = (s) => Buffer.from(s, 'utf8').toString('latin1');
|
|
const ITEM_RE = /<Item class="([^"]+)" referent="[^"]+">\s*<Properties>/g;
|
|
const NAME_RE = /<string name="Name">([^<]*)<\/string>/;
|
|
// Track nesting to know each script's parent service.
|
|
const out = [];
|
|
let last = 0;
|
|
const stack = []; // {cls, name}
|
|
const TOKEN_RE = /<Item class="([^"]+)" referent="[^"]+">|<\/Item>/g;
|
|
let m;
|
|
const edits = []; // {start, end, replacement}
|
|
while ((m = TOKEN_RE.exec(text))) {
|
|
if (m[0] === '</Item>') { stack.pop(); continue; }
|
|
const cls = m[1];
|
|
const propsStart = text.indexOf('<Properties>', m.index);
|
|
const propsEnd = text.indexOf('</Properties>', propsStart);
|
|
// Name may come after a long Source block, so search the whole Properties block
|
|
// with the Source CDATA cut out.
|
|
const seg = text.slice(propsStart, propsEnd).replace(/<!\[CDATA\[[\s\S]*?\]\]>/g, '');
|
|
const nm = NAME_RE.exec(seg);
|
|
const name = nm ? Buffer.from(nm[1], 'latin1').toString('utf8') : '';
|
|
const parent = stack.length ? stack[stack.length - 1] : null;
|
|
stack.push({ cls, name });
|
|
if ((cls === 'Script' || cls === 'LocalScript' || cls === 'ModuleScript') && parent) {
|
|
const svc = parent.cls === 'StarterPlayerScripts' ? 'StarterPlayerScripts' : parent.cls;
|
|
const key = svc + '/' + name + '/' + cls;
|
|
const w = wanted.get(key);
|
|
if (w) {
|
|
if (w.done) console.log('WARNING: duplicate script in place, updating both:', key);
|
|
const full = text.slice(propsStart, propsEnd);
|
|
const sm = /<ProtectedString name="Source">[\s\S]*?<\/ProtectedString>/.exec(full);
|
|
const tag = '<ProtectedString name="Source"><![CDATA[' + toBytes(w.src) + ']]></ProtectedString>';
|
|
if (sm) edits.push({ start: propsStart + sm.index, end: propsStart + sm.index + sm[0].length, rep: tag });
|
|
else edits.push({ start: propsEnd, end: propsEnd, rep: tag });
|
|
w.done = true;
|
|
}
|
|
}
|
|
// skip over the Properties block so nested strings are not scanned as items
|
|
TOKEN_RE.lastIndex = propsEnd;
|
|
}
|
|
edits.sort((a, b) => a.start - b.start);
|
|
const fd = fs.openSync(outP, 'w');
|
|
let pos = 0;
|
|
for (const e of edits) {
|
|
fs.writeSync(fd, Buffer.from(text.slice(pos, e.start), 'latin1'));
|
|
fs.writeSync(fd, Buffer.from(e.rep, 'latin1'));
|
|
pos = e.end;
|
|
}
|
|
fs.writeSync(fd, Buffer.from(text.slice(pos), 'latin1'));
|
|
fs.closeSync(fd);
|
|
let missing = 0;
|
|
for (const [k, w] of wanted) if (!w.done) { missing++; console.log('MISSING in place:', k, '(' + w.file + ')'); }
|
|
console.log('updated', edits.length, 'scripts, missing', missing);
|