-
Notifications
You must be signed in to change notification settings - Fork 7
/
parsePlg.js
executable file
·176 lines (149 loc) · 4.68 KB
/
parsePlg.js
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
#!/usr/bin/env node
'use strict';
var readline = require('readline');
var mkdirp = require('mkdirp');
var gumyen = require('gumyen');
var writeUTF16 = require('./util').writeUTF16;
function main(args) {
var fs = require('fs-extra');
var path = require('path');
var config = require(path.join(process.cwd(), 'plgconfig'));
var directory;
var filename;
var dialogsOnly = false;
if (args.length === 0) {
directory = config.srcDir;
filename = path.join(config.importDir, config.pluginFilename);
} else if (args.length === 1 && args[0] === 'test') {
directory = config.testDir;
filename = path.join(config.importDir, 'Test' + config.pluginFilename);
} else if (args.length === 1 && args[0] === 'dialogs') {
directory = config.srcDir;
filename = path.join(config.importDir, config.pluginFilename);
dialogsOnly = true;
} else {
filename = args[0];
directory = args[1];
}
if (!directory || !filename) {
console.log("Usage: parsePlg [plugin-filename output-directory | test | dialogs ]" );
console.log("No args - parses configured plugin" );
console.log("Arg is 'test' - parses configured test plugin" );
console.log("Plugin file must be in configured import directory");
process.exit(1);
}
var data = '';
var currentModule = '';
var currentModulePath = '';
var filesWritten = {};
var level = 0;
var globals = [];
var functionRegex = /^\s*(\w+)\s+"(\(.*\))\s+\{/;
var endFunctionRegex = /^(.*)}"$/;
var moduleLineRegex = /^\s*\/\/\$module\(([\w./]+)\)/;
var dialogRegex = /^\s*(\w+)\s+"Dialog"\s*$/;
var startDialogSegmentRegex = /^\s*\{/;
var endDialogSegmentRegex = /^\s*}/;
mkdirp.sync(directory);
mkdirp.sync(path.join(directory, 'dialog'));
var encoding = gumyen.encodingSync(filename);
console.log('Processing: %s (%s)', filename, encoding);
var rd = readline.createInterface({
input: fs.createReadStream(filename, {encoding: encoding}),
output: process.stdout,
terminal: false
});
var processFn = processLine;
var n = 0;
rd.on('line', function(line) {
processFn(line);
});
rd.on('close', function() {
if (!dialogsOnly) {
writeGlobals();
}
console.log('Finished: written to ' + directory);
});
function writeGlobals() {
writeUTF16(path.join(directory, 'GLOBALS.mss'), globals.join('\n'));
}
function processLine(line) {
var func = functionRegex.exec(line);
if (func) {
console.log('function ' + func[1] + func[2]);
data = 'function ' + func[1] + func[2] + ' ' + '{\n';
var filename = path.join(directory, func[1] + '.mss');
processFn = processFunctionLineFunc(filename);
return;
}
var dialog = dialogRegex.exec(line);
if (dialog) {
console.log('dialog ' + dialog[1]);
data = line;
data += '\n';
processFn = processDialogLineFunc(path.join(directory, 'dialog', dialog[1] + '.msd'));
return;
}
if (line[0] === '\uffef' || line[0] === '\ufeff') {
line = line.substring(1);
}
line = line.trim();
if (line !== '}' && line !== '{' && line !== '\ufeff' && line !== '\uffef' && line.length > 0) {
globals.push(line);
}
}
function processDialogLineFunc(filename) {
return function (line) {
if (startDialogSegmentRegex.exec(line)) level++;
if (endDialogSegmentRegex.exec(line)) level--;
data += line;
data += '\n';
if (level === 0) {
writeUTF16(filename, data);
processFn = processLine;
}
}
}
function processFunctionLineFunc(filename) {
return function (line) {
if (dialogsOnly) {
return;
}
var isModuleLine = moduleLineRegex.exec(line);
if (isModuleLine) {
currentModulePath = path.join(directory, path.dirname(isModuleLine[1]));
currentModule = path.join(directory, isModuleLine[1]);
data += line;
data += '\n';
return;
}
var isEndFunc = endFunctionRegex.exec(line);
if (isEndFunc) {
var ending = isEndFunc[1];
if (ending.length) {
ending += '\n';
}
data += ending + '} //$end\n\n';
if (currentModule) {
var opts = {encoding: encoding};
if (filesWritten[currentModule]) {
opts.flag = 'a';
}
mkdirp(currentModulePath);
writeUTF16(currentModule, data, opts);
filesWritten[currentModule] = true;
currentModule = '';
} else {
console.log(filename);
writeUTF16(filename, data);
filesWritten[filename] = true;
}
processFn = processLine;
return;
}
data += line;
data += '\n';
}
}
}
main(process.argv.slice(2));