@jtff/miztemplate-lib 3.0.0-rc1 → 3.0.0-rc11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/mizlib.js CHANGED
@@ -1,336 +1,385 @@
1
1
  "use strict";
2
2
 
3
3
  const jszip = require("jszip");
4
- const lstat = promisify(fs.lstat);
5
4
  const path = require("path");
6
5
  const {format, parse} = require("lua-json");
7
6
  const {promisify} = require("util");
7
+ const fs = require("fs");
8
+ const lstat = promisify(fs.lstat);
8
9
 
9
- function injectLuaScriptsInMissionObject(tObject, trObject, mrObject, strTitle, scriptFilesArray, timingInSeconds, hexColor) {
10
- let nextIndex = Object.keys(trObject).length + 1;
11
- if (nextIndex === 1) {
12
- tObject['actions'] = {};
13
- tObject['func'] = {};
14
- tObject['conditions'] = {};
15
- tObject['flag'] = {};
16
- trObject = {};
10
+ class Mizlib {
11
+ constructor() {
12
+ // no need for initialization
13
+ }
14
+
15
+ injectLuaScriptAndUpdateWorkspace(workspacePath, folderPath, luaScriptsArray, titleString, missionObject, mapResourceObject, timing, colorString) {
16
+ let tuple = this.injectLuaScriptsInMissionObject(
17
+ missionObject['trig'],
18
+ missionObject['trigrules'],
19
+ mapResourceObject,
20
+ titleString,
21
+ luaScriptsArray,
22
+ timing,
23
+ colorString
24
+ );
25
+ for (let luaScript of luaScriptsArray) {
26
+ fs.copyFileSync(
27
+ [
28
+ 'node_modules/@jtff/miztemplate-lib/lua/',
29
+ folderPath,
30
+ '/',
31
+ luaScript
32
+ ].join(""),
33
+ [
34
+ workspacePath,
35
+ "/",
36
+ folderPath,
37
+ "/",
38
+ path.basename(luaScript)
39
+ ].join("")
40
+ );
41
+ }
42
+ return tuple;
17
43
  }
18
- let actionSentence = "";
19
- let actionsObject = {};
20
- for (const [index, scriptFile] of scriptFilesArray.entries()) {
21
- actionSentence += "a_do_script_file(getValueResourceByKey(\"" + scriptFile + "\")); "
22
- actionsObject[index + 1] = {
23
- file: scriptFile,
24
- predicate: 'a_do_script_file',
44
+
45
+ injectLuaScriptsInMissionObject(tObject, trObject, mrObject, strTitle, scriptFilesArray, timingInSeconds, hexColor) {
46
+ let nextIndex = Object.keys(trObject).length + 1;
47
+ if (nextIndex === 1) {
48
+ tObject['actions'] = {};
49
+ tObject['func'] = {};
50
+ tObject['conditions'] = {};
51
+ tObject['flag'] = {};
52
+ trObject = {};
53
+ }
54
+ let actionSentence = "";
55
+ let actionsObject = {};
56
+ for (const [index, scriptFile] of scriptFilesArray.entries()) {
57
+ actionSentence += "a_do_script_file(getValueResourceByKey(\"" + scriptFile + "\")); "
58
+ actionsObject[index + 1] = {
59
+ file: scriptFile,
60
+ predicate: 'a_do_script_file',
61
+ };
62
+ mrObject[scriptFile] = scriptFile;
63
+ }
64
+ actionSentence += "mission.trig.func[" + nextIndex + "]=nil;"
65
+ tObject['actions'][nextIndex] = actionSentence;
66
+ tObject['func'][nextIndex] = "if mission.trig.conditions[" + nextIndex + "]() then mission.trig.actions[" + nextIndex + "]() end";
67
+ tObject['conditions'][nextIndex] = "return(c_time_after(" + timingInSeconds + ") )";
68
+ tObject['flag'][nextIndex] = true;
69
+ trObject[nextIndex] = {
70
+ rules: {
71
+ 1: {
72
+ coalitionlist: 'red',
73
+ seconds: timingInSeconds,
74
+ predicate: 'c_time_after',
75
+ zone: ''
76
+ }
77
+ },
78
+ eventlist: '',
79
+ comment: strTitle,
80
+ actions: actionsObject,
81
+ predicate: 'triggerOnce',
82
+ colorItem: hexColor
25
83
  };
26
- mrObject[scriptFile] = scriptFile;
84
+ return {tObject: tObject, trObject: trObject, mrObject: mrObject};
27
85
  }
28
- actionSentence += "mission.trig.func[" + nextIndex + "]=nil;"
29
- tObject['actions'][nextIndex] = actionSentence;
30
- tObject['func'][nextIndex] = "if mission.trig.conditions[" + nextIndex + "]() then mission.trig.actions[" + nextIndex + "]() end";
31
- tObject['conditions'][nextIndex] = "return(c_time_after(" + timingInSeconds + ") )";
32
- tObject['flag'][nextIndex] = true;
33
- trObject[nextIndex] = {
34
- rules: {
35
- 1: {
36
- coalitionlist: 'red',
37
- seconds: timingInSeconds,
38
- predicate: 'c_time_after',
39
- zone: ''
86
+
87
+ async getZipObjectFromMizPath(mizPath) {
88
+ var MizFile = new jszip();
89
+ const mizData = fs.readFileSync(mizPath);
90
+ return MizFile.loadAsync(mizData);
91
+ }
92
+
93
+ async mizUpdate(mizPath, copyPath, strTheatreSettings) {
94
+ const zip = await this.getZipObjectFromMizPath(mizPath);
95
+ this.injectLuaFilesFromFolderIntoZipObject(zip, 'src');
96
+ this.injectLuaFilesFromFolderIntoZipObject(zip, 'lib');
97
+ if (strTheatreSettings === null) {
98
+ // TODO: Inject fake data
99
+ console.log("NO THEATRE SPECIFIED. RADIO PRESETS WILL NOT BE AVAILABLE !");
100
+ } else {
101
+ await this.mizUpdateRadioPresets(zip, 'resources/radios/' + strTheatreSettings);
102
+ }
103
+ this.mizUpdateSettingsLuaFiles(zip, strTheatreSettings);
104
+ await this.mizUpdateSoundFolders(zip);
105
+ const inputZip = await zip.generateAsync({
106
+ type: 'nodebuffer',
107
+ streamFiles: true,
108
+ compression: "DEFLATE",
109
+ compressionOptions: {
110
+ level: 9
40
111
  }
41
- },
42
- eventlist: '',
43
- comment: strTitle,
44
- actions: actionsObject,
45
- predicate: 'triggerOnce',
46
- colorItem: hexColor
47
- };
48
- return { tObject: tObject, trObject: trObject, mrObject: mrObject };
49
- }
112
+ });
113
+ fs.writeFileSync(copyPath ? copyPath : mizPath, inputZip);
114
+ }
50
115
 
51
- async function getZipObjectFromMizPath(mizPath) {
52
- var MizFile = new jszip();
53
- const mizData = fs.readFileSync(mizPath);
54
- return MizFile.loadAsync(mizData);
55
- }
116
+ async mizInjectMissionDataFile(mizPath, missionObject) {
117
+ const zip = await this.getZipObjectFromMizPath(mizPath);
118
+ this.mizUpdateMissionDataFile(zip, missionObject);
119
+ const inputZip = await zip.generateAsync({
120
+ type: 'nodebuffer',
121
+ streamFiles: true,
122
+ compression: "DEFLATE",
123
+ compressionOptions: {
124
+ level: 9
125
+ }
126
+ });
127
+ fs.writeFileSync(mizPath, inputZip);
128
+ }
56
129
 
57
- async function mizUpdate(mizPath, copyPath, strTheatreSettings) {
58
- const zip = await getZipObjectFromMizPath(mizPath);
59
- injectLuaFilesFromFolderIntoZipObject(zip,'src');
60
- injectLuaFilesFromFolderIntoZipObject(zip,'lib');
61
- if (strTheatreSettings === null) {
62
- // TODO: Inject fake data
63
- console.log("NO THEATRE SPECIFIED. RADIO PRESETS WILL NOT BE AVAILABLE !");
64
- } else {
65
- await mizUpdateRadioPresets(zip, 'resources/radios/' + strTheatreSettings);
130
+ async injectLuaSettingsFromFolderPathToMizPath(mizPath, settingsFolder) {
131
+ const zip = await this.getZipObjectFromMizPath(mizPath);
132
+ await this.injectLuaSettingsFromFolderPathToZipObject(zip, settingsFolder);
133
+ const outputZip = await zip.generateAsync({
134
+ type: 'nodebuffer',
135
+ streamFiles: true,
136
+ compression: "DEFLATE",
137
+ compressionOptions: {
138
+ level: 9
139
+ }
140
+ });
141
+ fs.writeFileSync(mizPath, outputZip);
66
142
  }
67
- mizUpdateSettingsLuaFiles(zip, strTheatreSettings);
68
- await mizUpdateSoundFolders(zip);
69
- const inputZip = await zip.generateAsync({
70
- type: 'nodebuffer',
71
- streamFiles: true,
72
- compression: "DEFLATE",
73
- compressionOptions: {
74
- level: 9
75
- }
76
- });
77
- fs.writeFileSync(copyPath ? copyPath : mizPath, inputZip);
78
- }
79
143
 
80
- async function mizInjectMissionDataFile(mizPath, missionObject) {
81
- const zip = await getZipObjectFromMizPath(mizPath);
82
- mizUpdateMissionDataFile(zip, missionObject);
83
- const inputZip = await zip.generateAsync({
84
- type: 'nodebuffer',
85
- streamFiles: true,
86
- compression: "DEFLATE",
87
- compressionOptions: {
88
- level: 9
144
+ async injectLuaSettingsFromFolderPathToZipObject(zip, settingsFolder) {
145
+ for (let file of fs.readdirSync(settingsFolder).filter(file => file.endsWith(".lua"))) {
146
+ this.injectFileIntoZipObject(zip, [
147
+ settingsFolder,
148
+ "/",
149
+ file].join(""));
89
150
  }
90
- });
91
- fs.writeFileSync(mizPath, inputZip);
92
- }
151
+ }
93
152
 
94
- async function injectLuaSettingsFromFolderPathToMizPath(mizPath, settingsFolder) {
95
- const zip = await getZipObjectFromMizPath(mizPath);
96
- await injectLuaSettingsFromFolderPathToZipObject(zip,settingsFolder);
97
- const outputZip = await zip.generateAsync({
98
- type: 'nodebuffer',
99
- streamFiles: true,
100
- compression: "DEFLATE",
101
- compressionOptions: {
102
- level: 9
103
- }
104
- });
105
- fs.writeFileSync(mizPath, outputZip);
106
- }
107
153
 
108
- async function injectLuaSettingsFromFolderPathToZipObject(zip, settingsFolder) {
109
- for (let file of fs.readdirSync(settingsFolder).filter(file => file.endsWith(".lua"))) {
110
- injectFileIntoZipObject(zip, [
111
- settingsFolder,
112
- "/",
113
- file].join(""));
154
+ async mizInjectMapResourceFile(mizPath, mapResourceObject) {
155
+ const zip = await this.getZipObjectFromMizPath(mizPath);
156
+ this.mizUpdateMapResourceFile(zip, mapResourceObject);
157
+ const inputZip = await zip.generateAsync({
158
+ type: 'nodebuffer',
159
+ streamFiles: true,
160
+ compression: "DEFLATE",
161
+ compressionOptions: {
162
+ level: 9
163
+ }
164
+ });
165
+ fs.writeFileSync(mizPath, inputZip);
114
166
  }
115
- }
116
167
 
168
+ injectFileIntoZipObject(zip, filePath) {
169
+ zip.remove("l10n/DEFAULT/" + path.basename(filePath));
170
+ var stream = fs.createReadStream(filePath);
171
+ zip.file("l10n/DEFAULT/" + path.basename(filePath), stream);
172
+ }
117
173
 
118
- async function mizInjectMapResourceFile(mizPath, mapResourceObject) {
119
- const zip = await getZipObjectFromMizPath(mizPath);
120
- mizUpdateMapResourceFile(zip, mapResourceObject);
121
- const inputZip = await zip.generateAsync({
122
- type: 'nodebuffer',
123
- streamFiles: true,
124
- compression: "DEFLATE",
125
- compressionOptions: {
126
- level: 9
127
- }
128
- });
129
- fs.writeFileSync(mizPath, inputZip);
130
- }
174
+ mizUpdateMissionDataFile(zip, missionObject) {
175
+ zip.remove("mission");
176
+ let missionLuaT = format(missionObject, {singleQuote: false})
177
+ missionLuaT = missionLuaT
178
+ .split('\n')
179
+ .slice(1, -1)
180
+ .join('\n')
181
+ .slice(0, -1)
182
+ .replace(/\[\"(\d+)\"\] = /g, "[$1] = ");
183
+ zip.file("mission", missionLuaT);
184
+ }
131
185
 
132
- function injectFileIntoZipObject(zip, filePath) {
133
- zip.remove("l10n/DEFAULT/" + path.basename(filePath));
134
- var stream = fs.createReadStream(filePath);
135
- zip.file("l10n/DEFAULT/" + path.basename(filePath), stream);
136
- }
186
+ mizUpdateMapResourceFile(zip, mapResourceObject) {
187
+ zip.remove("l10n/DEFAULT/mapResource");
188
+ let mapResourceLuaT = format(mapResourceObject, {singleQuote: false})
189
+ mapResourceLuaT = mapResourceLuaT
190
+ .split('\n')
191
+ .slice(1, -1)
192
+ .join('\n')
193
+ .slice(0, -1)
194
+ .replace(/\[\"(\d+)\"\] = /g, "[$1] = ");
195
+ zip.file("l10n/DEFAULT/mapResource", mapResourceLuaT);
196
+ }
137
197
 
138
- function mizUpdateMissionDataFile(zip, missionObject) {
139
- zip.remove("mission");
140
- let missionLuaT = format(missionObject, { singleQuote: false })
141
- missionLuaT = missionLuaT
142
- .split('\n')
143
- .slice(1, -1)
144
- .join('\n')
145
- .slice(0, -1)
146
- .replace(/\[\"(\d+)\"\] = /g, "[$1] = ");
147
- zip.file("mission", missionLuaT);
148
- }
198
+ getMissionLuaStringFromZipObject(zipStream) {
199
+ return zipStream.file("mission").async("string");
200
+ }
149
201
 
150
- function mizUpdateMapResourceFile(zip, mapResourceObject) {
151
- zip.remove("l10n/DEFAULT/mapResource");
152
- let mapResourceLuaT = format(mapResourceObject, { singleQuote: false })
153
- mapResourceLuaT = mapResourceLuaT
154
- .split('\n')
155
- .slice(1, -1)
156
- .join('\n')
157
- .slice(0, -1)
158
- .replace(/\[\"(\d+)\"\] = /g, "[$1] = ");
159
- zip.file("l10n/DEFAULT/mapResource", mapResourceLuaT);
160
- }
202
+ getMapResourceLuaStringFromZipObject(zipStream) {
203
+ return zipStream.file("l10n/DEFAULT/mapResource").async("string");
204
+ }
161
205
 
162
- function getMissionLuaStringFromZipObject(zipStream) {
163
- return zipStream.file("mission").async("string");
164
- }
206
+ injectLuaFilesFromFolderIntoZipObject(zip, folderPath) {
207
+ for (let file of fs.readdirSync(folderPath).filter(file => file.endsWith(".lua"))) {
208
+ console.log('injecting up2date ' + folderPath + '/' + file + ' file in archive');
209
+ this.injectFileIntoZipObject(zip, folderPath + "/" + file);
210
+ };
211
+ }
165
212
 
166
- function getMapResourceLuaStringFromZipObject(zipStream) {
167
- return zipStream.file("l10n/DEFAULT/mapResource").async("string");
168
- }
169
213
 
170
- function injectLuaFilesFromFolderIntoZipObject(zip, folderPath) {
171
- for (let file of fs.readdirSync(folderPath).filter(file => file.endsWith(".lua"))) {
172
- console.log('injecting up2date ' + folderPath + '/' + file + ' file in archive');
173
- injectFileIntoZipObject(zip, folderPath + "/" + file);
174
- };
175
- }
214
+ async mizUpdateRadioPresets(zip, preset_folder) {
215
+ // Create folder Avionics to make sure it exists then delete it to remove any old preset in the template
216
+ // Allows adding presets for A-10C
217
+ zip.folder("Avionics");
218
+ zip.remove("Avionics");
176
219
 
220
+ const mission_object = await this.getMissionObjectFromZipObject(zip);
221
+ for (let file of fs.readdirSync(preset_folder).filter(file => file.endsWith(".lua"))) {
222
+ const file_data = fs.readFileSync(preset_folder + '/' + file).toString();
223
+ const lua_string = file_data.substring(0, file_data.indexOf("radio_descriptor_table =") - 1);
224
+ const radio_descriptor_table = parse("return {" + lua_string + "}").descriptor;
177
225
 
178
- async function mizUpdateRadioPresets(zip, preset_folder) {
179
- // Create folder Avionics to make sure it exists then delete it to remove any old preset in the template
180
- // Allows adding presets for A-10C
181
- zip.folder("Avionics");
182
- zip.remove("Avionics");
183
-
184
- const mission_object = await getMissionObjectFromZipObject(zip);
185
- for (let file of fs.readdirSync(preset_folder).filter(file => file.endsWith(".lua"))) {
186
- const file_data = fs.readFileSync(preset_folder + '/' + file).toString();
187
- const lua_string = file_data.substring(0, file_data.indexOf("radio_descriptor_table =") - 1);
188
- const radio_descriptor_table = parse("return {" + lua_string + "}").descriptor;
189
-
190
- console.log('updating radio presets (aircraft: ' + radio_descriptor_table["aircraft"] + ', group_name: ' + radio_descriptor_table["group_name"] + ') with preset in ' + preset_folder + ' folder');
191
- const dcs_radio_presets = file_data.substring(file_data.indexOf("radio_descriptor_table =") + 24);
192
-
193
- for (const coalition_key in mission_object.coalition) {
194
- const coalition = mission_object.coalition[coalition_key];
195
- for (const country_list_key in coalition) {
196
- if (country_list_key != "country") continue;
197
- const country_list = coalition[country_list_key];
198
- for (const country_key in country_list) {
199
- const country = country_list[country_key];
200
- for (const plane_key in country["plane"]) {
201
- const plane = country["plane"][plane_key];
202
- for (const group_key in plane) {
203
- const group = plane[group_key];
204
- if (group["name"].match(radio_descriptor_table["group_name"]) == null) continue;
205
- for (const unit_key in group) {
206
- if (unit_key != "units") continue;
207
- const unit = group[unit_key];
208
- for (const sub_unit_key in unit) {
209
- const sub_unit = unit[sub_unit_key];
210
- if (sub_unit["skill"] != "Client") continue;
211
- // Aircraft is an A10CII, use A10C mode by creating files in the root with the unit id
212
- // if (radio_descriptor_table["aircraft"] == 'A-10C_2') {
213
- // const unit_id = sub_unit["unitId"];
214
- // ["UHF_RADIO", "VHF_AM_RADIO", "VHF_FM_RADIO"].forEach(folder => {
215
- // var zip_folder = zip.folder("Avionics/A-10C_2/" + unit_id + "/" + folder);
216
- // var file = parse("return " + dcs_radio_presets)[folder];
217
- // file = format(file, { singleQuote: false });
218
- // file = file
219
- // .split('\n')
220
- // .slice(1, -1)
221
- // .join('\n')
222
- // .slice(0, -1)
223
- // .replace(/\[\"(\d+)\"\] = /g, "[$1] = ");
224
- // zip_folder.file("SETTINGS.lua", file);
225
- // });
226
- // continue;
227
- // }
228
- if (sub_unit["type"] != radio_descriptor_table["aircraft"]) continue;
229
- // GROUP FOUND, SET RADIOS
230
- sub_unit["Radio"] = parse("return " + dcs_radio_presets)
226
+ console.log('updating radio presets (aircraft: ' + radio_descriptor_table["aircraft"] + ', group_name: ' + radio_descriptor_table["group_name"] + ') with preset in ' + preset_folder + ' folder');
227
+ const dcs_radio_presets = file_data.substring(file_data.indexOf("radio_descriptor_table =") + 24);
228
+
229
+ for (const coalition_key in mission_object.coalition) {
230
+ const coalition = mission_object.coalition[coalition_key];
231
+ for (const country_list_key in coalition) {
232
+ if (country_list_key != "country") continue;
233
+ const country_list = coalition[country_list_key];
234
+ for (const country_key in country_list) {
235
+ const country = country_list[country_key];
236
+ for (const plane_key in country["plane"]) {
237
+ const plane = country["plane"][plane_key];
238
+ for (const group_key in plane) {
239
+ const group = plane[group_key];
240
+ if (group["name"].match(radio_descriptor_table["group_name"]) == null) continue;
241
+ for (const unit_key in group) {
242
+ if (unit_key != "units") continue;
243
+ const unit = group[unit_key];
244
+ for (const sub_unit_key in unit) {
245
+ const sub_unit = unit[sub_unit_key];
246
+ if (sub_unit["skill"] != "Client") continue;
247
+ // Aircraft is an A10CII, use A10C mode by creating files in the root with the unit id
248
+ // if (radio_descriptor_table["aircraft"] == 'A-10C_2') {
249
+ // const unit_id = sub_unit["unitId"];
250
+ // ["UHF_RADIO", "VHF_AM_RADIO", "VHF_FM_RADIO"].forEach(folder => {
251
+ // var zip_folder = zip.folder("Avionics/A-10C_2/" + unit_id + "/" + folder);
252
+ // var file = parse("return " + dcs_radio_presets)[folder];
253
+ // file = format(file, { singleQuote: false });
254
+ // file = file
255
+ // .split('\n')
256
+ // .slice(1, -1)
257
+ // .join('\n')
258
+ // .slice(0, -1)
259
+ // .replace(/\[\"(\d+)\"\] = /g, "[$1] = ");
260
+ // zip_folder.file("SETTINGS.lua", file);
261
+ // });
262
+ // continue;
263
+ // }
264
+ if (sub_unit["type"] != radio_descriptor_table["aircraft"]) continue;
265
+ // GROUP FOUND, SET RADIOS
266
+ sub_unit["Radio"] = parse("return " + dcs_radio_presets)
267
+ }
231
268
  }
232
269
  }
233
270
  }
234
271
  }
235
272
  }
236
273
  }
274
+ };
275
+ this.mizUpdateMissionDataFile(zip, {mission: mission_object});
276
+ }
277
+
278
+ mizUpdateSettingsLuaFiles(zip, strTheatre) {
279
+ for (let file of fs.readdirSync('settings/' + strTheatre).filter(file => file.endsWith(".lua"))) {
280
+ console.log('updating settings/' + strTheatre + '/' + file + ' file in miz file');
281
+ this.injectFileIntoZipObject(zip, 'settings/' + strTheatre + '/' + file);
237
282
  }
238
- };
239
- mizUpdateMissionDataFile(zip, {mission: mission_object});
240
- }
283
+ ;
284
+ }
241
285
 
242
- function mizUpdateSettingsLuaFiles(zip, strTheatre) {
243
- for (let file of fs.readdirSync('settings/' + strTheatre).filter(file => file.endsWith(".lua"))) {
244
- console.log('updating settings/' + strTheatre + '/' + file + ' file in miz file');
245
- injectFileIntoZipObject(zip, 'settings/' + strTheatre + '/' + file);
246
- };
247
- }
286
+ async mizUpdateSoundFolders(zip) {
287
+ if (fs.existsSync('resources/sounds') && fs.lstatSync('resources/sounds').isDirectory()) {
288
+ const folderArray = fs.readdirSync('resources/sounds');
289
+ for (const folder of folderArray) {
290
+ await this.injectSingleSoundFolderIntoZipObject(zip, folder);
291
+ }
292
+ }
293
+ }
248
294
 
249
- async function mizUpdateSoundFolders(zip) {
250
- if (fs.existsSync('resources/sounds') && fs.lstatSync('resources/sounds').isDirectory()) {
251
- const folderArray = fs.readdirSync('resources/sounds');
252
- for (const folder of folderArray) {
253
- await mizUpdateSingleSoundFolder(zip, folder);
295
+ async updateWorkspaceWithSingleSoundFolder(workspacePath, folderString) {
296
+ if (
297
+ fs.existsSync(
298
+ [
299
+ workspacePath,
300
+ "/resources/sounds/",
301
+ folderString
302
+ ].join("")
303
+ ) &&
304
+ fs.lstatSync([
305
+ workspacePath,
306
+ "/resources/sounds/",
307
+ folderString
308
+ ].join("")).isDirectory()
309
+ ) {
310
+ fs.rmSync([
311
+ workspacePath,
312
+ "/resources/sounds/",
313
+ folderString
314
+ ].join(""), {recursive: true});
315
+ }
316
+ if (fs.existsSync('node_modules/@jtff/miztemplate-lib/resources/sounds/' + folderString) && fs.lstatSync('node_modules/@jtff/miztemplate-lib/resources/sounds/' + folderString).isDirectory()) {
317
+ fs.mkdirSync([
318
+ workspacePath,
319
+ "/resources/sounds/",
320
+ folderString
321
+ ].join(""), {recursive: true});
322
+ fs.cpSync('node_modules/@jtff/miztemplate-lib/resources/sounds/' + folderString,[
323
+ workspacePath,
324
+ "/resources/sounds/",
325
+ folderString
326
+ ].join(""), {recursive: true});
254
327
  }
255
328
  }
256
- }
257
329
 
258
- async function mizUpdateSingleSoundFolder(zip, folder) {
259
- if (fs.existsSync('resources/sounds/' + folder) && fs.lstatSync('resources/sounds/' + folder).isDirectory()) {
260
- console.log('adding sound files from resources/sounds/' + folder + ' folder...');
261
- zip = zip.remove(folder).folder(folder);
262
- await addFilesToZip(zip, 'resources/sounds/' + folder, fs.readdirSync('resources/sounds/' + folder));
330
+ async injectSingleSoundFolderIntoZipObject(zip, folder) {
331
+ if (fs.existsSync('resources/sounds/' + folder) && fs.lstatSync('resources/sounds/' + folder).isDirectory()) {
332
+ console.log('adding sound files from resources/sounds/' + folder + ' folder...');
333
+ zip = zip.remove(folder).folder(folder);
334
+ await this.addFilesToZip(zip, 'resources/sounds/' + folder, fs.readdirSync('resources/sounds/' + folder));
335
+ }
263
336
  }
264
- }
265
337
 
266
- async function addFilesToZip(zip, directoryPath, filesToInclude) {
267
- const promiseArr = await filesToInclude.map(async file => {
268
- const filePath = path.join(directoryPath, file)
269
- try {
270
- const fileStats = await lstat(filePath)
271
- const isDirectory = fileStats.isDirectory()
272
- if (isDirectory) {
273
- const directory = zip.remove(file).folder(file)
274
- const subFiles = fs.readdirSync(filePath)
275
- return addFilesToZip(directory, filePath, subFiles)
276
- } else {
277
- // console.log('added file : '+file);
278
- return zip.file(file, fs.createReadStream(filePath))
338
+ async addFilesToZip(zip, directoryPath, filesToInclude) {
339
+ const promiseArr = await filesToInclude.map(async file => {
340
+ const filePath = path.join(directoryPath, file)
341
+ try {
342
+ const fileStats = await lstat(filePath)
343
+ const isDirectory = fileStats.isDirectory()
344
+ if (isDirectory) {
345
+ const directory = zip.remove(file).folder(file)
346
+ const subFiles = fs.readdirSync(filePath)
347
+ return this.addFilesToZip(directory, filePath, subFiles)
348
+ } else {
349
+ // console.log('added file : '+file);
350
+ return zip.file(file, fs.createReadStream(filePath))
351
+ }
352
+ } catch (err) {
353
+ console.log(err)
354
+ return Promise.resolve()
279
355
  }
280
- } catch (err) {
281
- console.log(err)
282
- return Promise.resolve()
283
- }
284
- })
285
- return Promise.all(promiseArr)
286
- }
356
+ })
357
+ return Promise.all(promiseArr)
358
+ }
287
359
 
288
- async function copyMiz(srcMizPath, dstMizPath) {
289
- await fs.createReadStream(srcMizPath).pipe(fs.createWriteStream(dstMizPath));
290
- }
360
+ async copyMiz(srcMizPath, dstMizPath) {
361
+ await fs.createReadStream(srcMizPath).pipe(fs.createWriteStream(dstMizPath));
362
+ }
291
363
 
292
- async function getMissionObjectFromMizPath(MizPath) {
293
- let luaTable = 'return { \n' + await getMissionLuaStringFromZipObject(await getZipObjectFromMizPath(MizPath)) + ' }';
294
- return parse(luaTable).mission;
295
- }
364
+ async getMissionObjectFromMizPath(MizPath) {
365
+ let luaTable = 'return { \n' + await this.getMissionLuaStringFromZipObject(await this.getZipObjectFromMizPath(MizPath)) + ' }';
366
+ return parse(luaTable).mission;
367
+ }
296
368
 
297
- async function getMissionObjectFromZipObject(zip) {
298
- let luaTable = 'return { \n' + await getMissionLuaStringFromZipObject(zip) + ' }';
299
- return parse(luaTable).mission;
300
- }
369
+ async getMissionObjectFromZipObject(zip) {
370
+ let luaTable = 'return { \n' + await this.getMissionLuaStringFromZipObject(zip) + ' }';
371
+ return parse(luaTable).mission;
372
+ }
301
373
 
302
- async function getMapResourceObjectFromMizPath(MizPath) {
303
- let luaTable = 'return { \n' + await getMapResourceLuaStringFromZipObject(await getZipObjectFromMizPath(MizPath)) + ' }';
304
- return parse(luaTable).mapResource;
305
- }
374
+ async getMapResourceObjectFromMizPath(MizPath) {
375
+ let luaTable = 'return { \n' + await this.getMapResourceLuaStringFromZipObject(await this.getZipObjectFromMizPath(MizPath)) + ' }';
376
+ return parse(luaTable).mapResource;
377
+ }
306
378
 
307
- async function getMapResourceObjectFromZipObject(zip) {
308
- let luaTable = 'return { \n' + await getMapResourceLuaStringFromZipObject(zip) + ' }';
309
- return parse(luaTable).mapResource;
379
+ async getMapResourceObjectFromZipObject(zip) {
380
+ let luaTable = 'return { \n' + await this.getMapResourceLuaStringFromZipObject(zip) + ' }';
381
+ return parse(luaTable).mapResource;
382
+ }
310
383
  }
311
384
 
312
- module.exports = {
313
- injectLuaScriptsInMissionObject: injectLuaScriptsInMissionObject,
314
- getZipObjectFromMizPath: getZipObjectFromMizPath,
315
- mizUpdate: mizUpdate,
316
- mizInjectMissionDataFile: mizInjectMissionDataFile,
317
- injectLuaSettingsFromFolderPathToMizPath: injectLuaSettingsFromFolderPathToMizPath,
318
- injectLuaSettingsFromFolderPathToZipObject: injectLuaSettingsFromFolderPathToZipObject,
319
- mizInjectMapResourceFile: mizInjectMapResourceFile,
320
- injectFileIntoZipObject: injectFileIntoZipObject,
321
- mizUpdateMissionDataFile: mizUpdateMissionDataFile,
322
- mizUpdateMapResourceFile: mizUpdateMapResourceFile,
323
- getMissionLuaStringFromZipObject: getMissionLuaStringFromZipObject,
324
- getMapResourceLuaStringFromZipObject: getMapResourceLuaStringFromZipObject,
325
- mizUpdateRadioPresets: mizUpdateRadioPresets,
326
- mizUpdateSettingsLuaFiles: mizUpdateSettingsLuaFiles,
327
- mizUpdateSoundFolders: mizUpdateSoundFolders,
328
- mizUpdateSingleSoundFolder: mizUpdateSingleSoundFolder,
329
- addFilesToZip: addFilesToZip,
330
- copyMiz: copyMiz,
331
- getMissionObjectFromMizPath: getMissionObjectFromMizPath,
332
- getMissionObjectFromZipObject: getMissionObjectFromZipObject,
333
- getMapResourceObjectFromMizPath: getMapResourceObjectFromMizPath,
334
- getMapResourceObjectFromZipObject: getMapResourceObjectFromZipObject,
335
- injectLuaFilesFromFolderIntoZipObject: injectLuaFilesFromFolderIntoZipObject
336
- };
385
+ module.exports = Mizlib