-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
371 lines (317 loc) · 10.2 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
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
// === CONFIGURABLE VARIABLES
const bpfoldername = "gen";
const useMinecraftPreview = false; // Whether to target the "Minecraft Preview" version of Minecraft vs. the main store version of Minecraft
const useMinecraftDedicatedServer = false; // Whether to use Bedrock Dedicated Server - see https://www.minecraft.net/download/server/bedrock
const dedicatedServerPath = "C:/mc/bds/1.19.0/"; // if using Bedrock Dedicated Server, where to find the extracted contents of the zip package
// === END CONFIGURABLE VARIABLES
const zip = require("gulp-zip");
const gulp = require("gulp");
const ts = require("gulp-typescript");
const del = require("del");
const os = require("os");
const spawn = require("child_process").spawn;
const sourcemaps = require("gulp-sourcemaps");
const gulpEsbuild = require("gulp-esbuild");
const Vinyl = require("vinyl");
const worldsFolderName = useMinecraftDedicatedServer ? "worlds" : "minecraftWorlds";
const activeWorldFolderName = useMinecraftDedicatedServer ? "Bedrock level" : bpfoldername + "world";
const get_Mojang_dir = () => {
const homeDir = os.homedir();
switch (process.platform) {
case "win32":
return homeDir + useMinecraftPreview
? "/AppData/Local/Packages/Microsoft.MinecraftWindowsBeta_8wekyb3d8bbwe/LocalState/games/com.mojang/"
: "/AppData/Local/Packages/Microsoft.MinecraftUWP_8wekyb3d8bbwe/LocalState/games/com.mojang/";
case "linux":
return homeDir + "/.var/app/io.mrarm.mcpelauncher/data/mcpelauncher/games/com.mojang/";
case "android":
break;
}
};
const mcdir = get_Mojang_dir();
function clean_build(callbackFunction) {
del(["build/behavior_packs/", "build/resource_packs/"]).then(
(value) => {
callbackFunction(); // success
},
(reason) => {
callbackFunction(); // error
}
);
}
function copy_behavior_packs() {
return gulp.src(["behavior_packs/**/*"]).pipe(gulp.dest("build/behavior_packs"));
}
function copy_resource_packs() {
return gulp.src(["resource_packs/**/*"]).pipe(gulp.dest("build/resource_packs"));
}
function string_src(filename, string) {
var src = require("stream").Readable({ objectMode: true });
src._read = function () {
this.push(
new Vinyl({
cwd: "",
base: null,
path: filename,
contents: Buffer.from(string, "utf-8"),
})
);
this.push(null);
};
return src;
}
function esbuild_system() {
return gulp
.src("./build/scripts/main.js")
.pipe(
gulpEsbuild({
outfile: "main.js",
bundle: true,
external: ["@minecraft/server-ui", "@minecraft/server"],
format: "esm",
})
)
.pipe(gulp.dest("./build/behavior_packs/gen/scripts"));
}
function pack_zip() {
return gulp.src("./build/behavior_packs/gen/**/*").pipe(zip("VoxelGeometry.mcpack")).pipe(gulp.dest("./"));
}
function exact_version() {
var pkg = require("./behavior_packs/gen/manifest.json");
return string_src("version", pkg.header.version.join(".")).pipe(gulp.dest("./"));
}
const copy_content = gulp.parallel(copy_behavior_packs, copy_resource_packs);
function compile_scripts() {
return gulp
.src("scripts/*")
.pipe(sourcemaps.init())
.pipe(
ts({
module: "es2020",
moduleResolution: "node",
lib: ["es2020", "dom"],
strict: true,
target: "es2020",
noImplicitAny: true,
})
)
.pipe(gulp.dest("build/scripts"));
}
const build = gulp.series(clean_build, copy_content, compile_scripts, esbuild_system, pack_zip, exact_version);
function clean_localmc(callbackFunction) {
if (!bpfoldername || !bpfoldername.length || bpfoldername.length < 2) {
console.log("No bpfoldername specified.");
callbackFunction();
return;
}
del([mcdir + "development_behavior_packs/" + bpfoldername, mcdir + "development_resource_packs/" + bpfoldername], {
force: true,
}).then(
(value) => {
callbackFunction(); // Success
},
(reason) => {
callbackFunction(); // Error
}
);
}
function deploy_localmc_behavior_packs() {
console.log("Deploying to '" + mcdir + "development_behavior_packs/" + bpfoldername + "'");
return gulp
.src(["build/behavior_packs/" + bpfoldername + "/**/*"])
.pipe(gulp.dest(mcdir + "development_behavior_packs/" + bpfoldername));
}
function deploy_localmc_resource_packs() {
return gulp
.src(["build/resource_packs/" + bpfoldername + "/**/*"])
.pipe(gulp.dest(mcdir + "development_resource_packs/" + bpfoldername));
}
function getTargetWorldPath() {
return mcdir + worldsFolderName + "/" + activeWorldFolderName;
}
function getTargetConfigPath() {
return mcdir + "config";
}
function getTargetWorldBackupPath() {
return "backups/worlds/" + activeWorldFolderName;
}
function getDevConfigPath() {
return "config";
}
function getDevWorldPath() {
return "worlds/default";
}
function getDevWorldBackupPath() {
return "backups/worlds/devdefault";
}
function clean_localmc_world(callbackFunction) {
console.log("Removing '" + getTargetWorldPath() + "'");
del([getTargetWorldPath()], {
force: true,
}).then(
(value) => {
callbackFunction(); // Success
},
(reason) => {
callbackFunction(); // Error
}
);
}
function clean_localmc_config(callbackFunction) {
console.log("Removing '" + getTargetConfigPath() + "'");
del([getTargetWorldPath()], {
force: true,
}).then(
(value) => {
callbackFunction(); // Success
},
(reason) => {
callbackFunction(); // Error
}
);
}
function clean_dev_world(callbackFunction) {
console.log("Removing '" + getDevWorldPath() + "'");
del([getDevWorldPath()], {
force: true,
}).then(
(value) => {
callbackFunction(); // Success
},
(reason) => {
callbackFunction(); // Error
}
);
}
function clean_localmc_world_backup(callbackFunction) {
console.log("Removing backup'" + getTargetWorldBackupPath() + "'");
del([getTargetWorldBackupPath()], {
force: true,
}).then(
(value) => {
callbackFunction(); // Success
},
(reason) => {
callbackFunction(); // Error
}
);
}
function clean_dev_world_backup(callbackFunction) {
console.log("Removing backup'" + getDevWorldBackupPath() + "'");
del([getTargetWorldBackupPath()], {
force: true,
}).then(
(value) => {
callbackFunction(); // Success
},
(reason) => {
callbackFunction(); // Error
}
);
}
function backup_dev_world() {
console.log("Copying world '" + getDevWorldPath() + "' to '" + getDevWorldBackupPath() + "'");
return gulp
.src([getTargetWorldPath() + "/**/*"])
.pipe(gulp.dest(getDevWorldBackupPath() + "/worlds/" + activeWorldFolderName));
}
function deploy_localmc_config() {
console.log("Copying world 'config/' to '" + getTargetConfigPath() + "'");
return gulp.src([getDevConfigPath() + "/**/*"]).pipe(gulp.dest(getTargetConfigPath()));
}
function deploy_localmc_world() {
console.log("Copying world 'worlds/default/' to '" + getTargetWorldPath() + "'");
return gulp.src([getDevWorldPath() + "/**/*"]).pipe(gulp.dest(getTargetWorldPath()));
}
function ingest_localmc_world() {
console.log("Ingesting world '" + getTargetWorldPath() + "' to '" + getDevWorldPath() + "'");
return gulp.src([getTargetWorldPath() + "/**/*"]).pipe(gulp.dest(getDevWorldPath()));
}
function backup_localmc_world() {
console.log("Copying world '" + getTargetWorldPath() + "' to '" + getTargetWorldBackupPath() + "/'");
return gulp
.src([getTargetWorldPath() + "/**/*"])
.pipe(gulp.dest(getTargetWorldBackupPath() + "/" + activeWorldFolderName));
}
const deploy_localmc = gulp.series(
clean_localmc,
function (callbackFunction) {
if (!useMinecraftDedicatedServer) {
console.log("\007"); // annunciate a beep!
}
callbackFunction();
},
gulp.parallel(deploy_localmc_behavior_packs, deploy_localmc_resource_packs)
);
function watch() {
return gulp.watch(
["scripts/**/*.ts", "behavior_packs/**/*", "resource_packs/**/*"],
gulp.series(build, deploy_localmc)
);
}
function serve() {
return gulp.watch(
["scripts/**/*.ts", "behavior_packs/**/*", "resource_packs/**/*"],
gulp.series(stopServer, build, deploy_localmc, startServer)
);
}
let activeServer = null;
function stopServer(callbackFunction) {
if (activeServer) {
activeServer.stdin.write("stop\n");
activeServer = null;
}
callbackFunction();
}
function startServer(callbackFunction) {
if (activeServer) {
activeServer.stdin.write("stop\n");
activeServer = null;
}
activeServer = spawn(dedicatedServerPath + "bedrock_server");
let logBuffer = "";
let serverLogger = function (buffer) {
let incomingBuffer = buffer.toString();
if (incomingBuffer.endsWith("\n")) {
(logBuffer + incomingBuffer).split(/\n/).forEach(function (message) {
if (message) {
if (message.indexOf("Server started.") >= 0) {
activeServer.stdin.write("script debugger listen 19144\n");
console.log("\007"); // annunciate a beep!
}
console.log("Server: " + message);
}
});
logBuffer = "";
} else {
logBuffer += incomingBuffer;
}
};
activeServer.stdout.on("data", serverLogger);
activeServer.stderr.on("data", serverLogger);
callbackFunction();
}
function insert(str, index, value) {
return str.substr(0, index) + value + str.substr(index);
}
exports.clean_build = clean_build;
exports.copy_behavior_packs = copy_behavior_packs;
exports.copy_resource_packs = copy_resource_packs;
exports.compile_scripts = compile_scripts;
exports.copy_content = copy_content;
exports.build = build;
exports.exact_version = exact_version;
exports.clean_localmc = clean_localmc;
exports.deploy_localmc = deploy_localmc;
exports.default = gulp.series(build, deploy_localmc);
exports.clean = gulp.series(clean_build, clean_localmc);
exports.watch = gulp.series(build, deploy_localmc, watch);
exports.serve = gulp.series(build, deploy_localmc, startServer, serve);
exports.updateworld = gulp.series(
clean_localmc_world_backup,
backup_localmc_world,
clean_localmc_world,
deploy_localmc_world
);
exports.ingestworld = gulp.series(clean_dev_world_backup, backup_dev_world, clean_dev_world, ingest_localmc_world);
exports.updateconfig = gulp.series(clean_localmc_config, deploy_localmc_config);