@jtff/miztemplate-lib 3.0.0-rc2 → 3.0.0-rc20

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