forked from TyKonKet/FS17_CreatorTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
291 lines (261 loc) · 9.01 KB
/
gulpfile.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
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
const os = require('os');
const gulp = require('gulp');
const gutil = require("gulp-util");
const fs = require("fs");
const path = require('path');
const del = require("del");
const size = require('gulp-size');
const zip = require("gulp-zip");
const run = require('gulp-run');
const replace = require('gulp-replace');
const execFile = require('child_process').execFile;
const ftp = require("vinyl-ftp");
const xmlbuilder = require("xmlbuilder");
const _ = require("lodash");
const xml2js = require("xml2js");
const Promise = require("bluebird");
const xmlescape = require('xml-escape');
var mod = JSON.parse(fs.readFileSync("mod.json"));
var package = JSON.parse(fs.readFileSync("package.json"));
const modName = mod.name;
const zipName = `${mod.name}.zip`;
const destination = parseAPath(mod.destination);
const zipSources = mod.zipSources;
const start = mod.start;
const fsDocPath = parseAPath(mod.fsDocPath);
const clearLog = mod.clearLog;
const serverUrl = `${mod.server.protocol}://${mod.server.host}:${mod.server.port}/`;
gulp.task("clean:log", () => {
if (clearLog) {
return del(`${fsDocPath}log.txt`, { force: true });
}
return;
});
gulp.task("clean:out", () => {
return del(`./out/${zipName}`);
});
gulp.task("clean:install", () => {
return del(`${destination}${zipName}`, { force: true });
});
gulp.task("startMode", () => {
return gulp
.src(`${fsDocPath}game.xml`)
.pipe(replace(/<startMode>.<\/startMode>/g, "<startMode>1</startMode>"))
.pipe(gulp.dest(`${fsDocPath}`));
});
gulp.task("build:out", ["clean:out"], () => {
return gulp.src(zipSources, { cwd: './src/' })
.pipe(replace(/{package_author}/g, package.author, { skipBinary: true }))
.pipe(replace(/{package_version}/g, package.version + ".0", { skipBinary: true }))
.pipe(size())
.pipe(zip(zipName))
.pipe(size())
.pipe(gulp.dest(`./out/`));
});
gulp.task("install", ["build:out", "clean:install"], () => {
return gulp.src(`./out/${zipName}`)
.pipe(gulp.dest(destination));
});
gulp.task("build", ["clean:log", "startMode", "install"], () => {
if (start.enabled) {
var params = start.params;
if (start.savegame.enabled) {
params.push("-autoStartSavegameId", start.savegame.number);
}
return execFile(start.path, params);
}
return;
});
gulp.task("release", ["build:out"], () => {
return;
});
gulp.task("default", ["build"]);
function parseAPath(path) {
path = path.replace("${homeDir}", os.homedir());
return path;
}
gulp.task("server:login", serverLogin);
gulp.task("server:stop", ["server:login"], serverStop);
gulp.task("server:start", ["server:login"], serverStart);
gulp.task("server:install:1", ["install", "server:login"], serverStop);
gulp.task("server:install:2", ["server:install:1"], () => {
const conn = new ftp({
host: mod.server.ftp.host,
port: mod.server.ftp.port,
user: mod.server.ftp.user,
pass: mod.server.ftp.password
});
return gulp
.src(`./out/${zipName}`, { buffer: false })
.pipe(conn.dest(mod.server.ftp.path))
.pipe(gutil.noop());
});
gulp.task("server:install", ["server:install:2"], serverStart);
gulp.task("server:build", ["server:install", "clean:log", "startMode"], () => {
if (start.enabled) {
var params = start.params;
if (start.savegame.enabled) {
params.push("-autoStartSavegameId", start.savegame.number);
}
return execFile(start.path, params);
}
return;
});
gulp.task("server:default", ["server:build"]);
function serverLogin() {
const command = `curl_server_login ${mod.server.username} ${mod.server.password} ${serverUrl}`;//*/`curl -X POST -c .cookies --data "username=${mod.server.username}&password=${mod.server.password}&login=Login" -H "Origin: ${serverUrl}" ${serverUrl}index.html > NUL`;
return run(command, { silent: true }).exec()
.pipe(gutil.noop());
}
function serverStop() {
const command = `curl_server_stop ${serverUrl}`;//*/`curl -X POST -b .cookies --data "stop_server=Stop" -H "Origin: ${serverUrl}" ${serverUrl}index.html > NUL`;
return run(command, { silent: true }).exec()
.pipe(gutil.noop());
}
function serverStart() {
const command = `curl_server_start ${mod.server.game.name} ${mod.server.game.adminPassword} ${mod.server.game.gamePassword} ${mod.server.game.savegame} ${serverUrl}`;//*/`curl -X POST -b .cookies --data "game_name=${mod.server.game.name}&admin_password=${mod.server.game.adminPassword}&game_password=${mod.server.game.gamePassword}&savegame=${mod.server.game.savegame}&map_start=default_Map01&difficulty=1&dirt_interval=2&matchmaking_server=2&mp_language=en&auto_save_interval=180&stats_interval=360&pause_game_if_empty=on&start_server=Start" -H "Origin: ${serverUrl}" ${serverUrl}index.html > NUL`;
return run(command, { silent: true }).exec()
.pipe(gutil.noop());
}
// translations generator from Realismus Modding https://github.com/RealismusModding
gulp.task("translations", () => {
const languages = ["br", "cs", "ct", "cz", "de", "en", "es", "fr", "hu", "it", "jp", "kr", "nl", "pl", "pt", "ro", "ru", "tr"];
Promise.reduce(languages, (result, language) => {
return loadXML(language).then((data) => {
result[language] = data;
return result;
});
}, {})
.then((data) => Promise.map(languages, (language) => {
if (language === "en") {
return Promise.resolve();
}
const path = pathForTranslation(language);
return createXML(data, language).then((xml) => {
console.log(`Writing XML file for '${language}'`);
return writeXML(xml, path);
});
}))
.then(() => {
console.log("Finished!");
})
.catch((err) => {
console.log("Error", err);
})
});
function createXML(data, language) {
if (!data[language]) {
data[language] = {
translations: {},
contributors: []
}
}
const xmlTexts = _.reduce(data["en"].translations, (result, value, key) => {
const trValue = data[language].translations[key];
const enTrValue = data["en"].translations[key];
if (language !== "en" && (!trValue || trValue === enTrValue)) {
console.log("Missing translation of '" + key + "' for", language);
result.push({
"#comment": `Missing translation of "${enTrValue}"`
});
}
result.push({
"text": {
"@name": key,
"@text": !!trValue ? trValue : enTrValue
}
});
return result
}, []);
const xmlHeader = {
version: "1.0",
encoding: "utf-8",
standalone: false
};
const xmlOptions = {
stringify: {
convertCommentKey: "#comment"
}
};
const root = xmlbuilder.create("l10n", xmlHeader, {}, xmlOptions);
root.ele("translationContributors", {}, data[language].contributors.join(", "));
root.ele("texts").ele(xmlTexts);
let text = root.end({
pretty: true,
indent: " "
}) + "\n";
let newlines = data["en"].newlines;
const searchReg = new RegExp(/^\s*<text\s+name=\"(.*)\"\s+text=\"(.*)\"\s*\/>$\n/, "igm");
const padding = " ".repeat(8);
text = text.replace(searchReg, (match, name, value, offset, string) => {
if (newlines.includes(name)) {
return padding + "<text name=\"" + name + "\" text=\"" + value + "\" />\n\n";
} else {
return padding + "<text name=\"" + name + "\" text=\"" + value + "\" />\n";
}
});
return Promise.resolve(text);
}
function pathForTranslation(language) {
return path.join(".", "src", "l10n", `modDesc_l10n_${language}.xml`)
}
function readXML(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, { encoding: "utf8" }, (err, data) => {
if (err) {
return reject(err);
}
xml2js.parseString(data, (err, data) => {
if (err) {
console.log(path);
return reject(err);
}
resolve(data)
});
});
});
}
function loadXML(language) {
if (!fs.existsSync(pathForTranslation(language))) {
console.error("Failed loading " + pathForTranslation(language));
return Promise.resolve();
}
return readXML(pathForTranslation(language)).then((xml) => {
console.log(`Read XML file for '${language}'`);
let data = {
translations: {},
contributors: [],
newlines: [],
};
if (!xml.l10n) {
return data;
}
let contribs = _.get(xml, "l10n.translationContributors", [""])[0];
data.contributors = _.map(contribs.split(","), _.trim);
let items = _.get(xml, "l10n.texts.0.text");
data.translations = _.reduce(items, (result, value) => {
if (_.has(value, "$.name")) {
result[_.get(value, "$.name")] = _.get(value, "$.text", "");
}
return result;
}, {});
const fileText = fs.readFileSync(pathForTranslation(language), "utf8");
const reg = new RegExp(/^\s*<text\s+name=\"(.*)\"\s+text=\".*\"\s*\/>$\n\n/, "igm");
let match = reg.exec(fileText);
while (match !== null) {
data.newlines.push(match[1]);
match = reg.exec(fileText);
}
return data;
})
}
function writeXML(xml, path) {
return new Promise((resolve, reject) => {
fs.writeFile(path, xml, { encoding: "utf8" }, (err) => {
if (err) {
return reject(err);
}
resolve();
});
});
}