Skip to content

Commit d9b3d26

Browse files
authored
Merge pull request #127 from yktsr/125-fix-plugin-command-mz
2 parents bfeec1f + 9d717d9 commit d9b3d26

9 files changed

+1937
-964
lines changed

Frame2Text.js

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
//= ============================================================================
22
// Frame2Text.js
33
// ----------------------------------------------------------------------------
4-
// (C)2023 Shick
4+
// (C)2023-2024 Shick
55
// This software is released under the MIT License.
66
// http://opensource.org/licenses/mit-license.php
77
// ----------------------------------------------------------------------------
88
// Version
9+
// 1.0.1 2024/09/07:
10+
// ・#125 プラグインコマンドMZを変換する際、オブジェクト型を取り扱えない不具合の修正
911
// 1.0.0 2024/01/20 Initial Version
1012
// 0.1.0 2023/09/25 新規作成
1113
//= ============================================================================
@@ -278,6 +280,12 @@
278280
* プラグイン作者は、いかなる場合も破損したプロジェクトの復元には
279281
* 応じられませんのでご注意ください。
280282
* テキストファイルの文字コードはUTF-8にのみ対応しています。
283+
*
284+
* --------------------------------------
285+
* Version
286+
* --------------------------------------
287+
* 1.0.1
288+
* build: 81bc070a76bcd0246713b2872df90650ffc7ce70
281289
*/
282290
/* eslint-enable spaced-comment */
283291

@@ -901,6 +909,33 @@
901909
else return name
902910
}
903911

912+
// MZのプラグインパラメータをパースする補助関数
913+
const parseMzArg = function (args_string) {
914+
const args = []
915+
let buffer = ''
916+
let braceLevel = 0
917+
918+
for (const char of args_string) {
919+
if (char === ',' && braceLevel === 0) {
920+
args.push(buffer.trim())
921+
buffer = ''
922+
} else {
923+
buffer += char
924+
if (char === '[' || char === '{') {
925+
braceLevel++
926+
} else if (char === ']' || char === '}') {
927+
braceLevel--
928+
}
929+
}
930+
}
931+
932+
if (buffer) {
933+
args.push(buffer.trim())
934+
}
935+
936+
return args
937+
}
938+
904939
// 出力するテキスト変数
905940
// Laurus.Frame2Text.EnglishTagの値を別変数に代入
906941
const decompile = function (map_events, EnglishTag) {
@@ -2428,21 +2463,22 @@
24282463
const parameters = event.parameters[0]
24292464
const splitParameters = parameters.split(' ')
24302465
const outParameters = `[${splitParameters[0]}]`
2431-
const lastIndex = text.lastIndexOf('\n')
2466+
const lastIndex = text.lastIndexOf('\n<')
24322467
const extractedText = text.substring(lastIndex + 1)
24332468
text = text.substring(0, lastIndex)
24342469

24352470
// 各引数に対して注釈を付け加える
24362471
mzCount++
24372472
const parametersNum = 2 + mzCount
24382473
// 357で出力したタグを,区切りで取得
2439-
const splitVal = extractedText.split(',')
2474+
const splitVal = parseMzArg(extractedText)
24402475
if (splitVal[parametersNum].endsWith('>')) {
24412476
// 最後の引数の場合は>が含まれている為、削除してから付け足す
24422477
splitVal[parametersNum] = splitVal[parametersNum].slice(0, -1) + outParameters + '>'
24432478
} else {
24442479
splitVal[parametersNum] = splitVal[parametersNum] + outParameters
24452480
}
2481+
splitVal[parametersNum] = splitVal[parametersNum].replace(/\n/g, '\\n')
24462482
addNewLineIndent(indent)
24472483
text += splitVal
24482484
} else {
@@ -2482,7 +2518,8 @@
24822518

24832519
// developer mode
24842520
if (typeof require !== 'undefined' && typeof require.main !== 'undefined' && require.main === module) {
2485-
const program = require('commander')
2521+
const { Command } = require('commander')
2522+
const program = new Command()
24862523
program
24872524
.version('1.0.0')
24882525
.usage('[options]')

Text2Frame.cjs.js

Lines changed: 88 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ function getDefaultExportFromCjs(x) {
33
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x;
44
}
55
function getAugmentedNamespace(n) {
6-
if (n.__esModule)
7-
return n;
6+
if (n.__esModule) return n;
87
var f = n.default;
98
if (typeof f == "function") {
109
var a = function a2() {
@@ -14,8 +13,7 @@ function getAugmentedNamespace(n) {
1413
return f.apply(this, arguments);
1514
};
1615
a.prototype = f.prototype;
17-
} else
18-
a = {};
16+
} else a = {};
1917
Object.defineProperty(a, "__esModule", { value: true });
2018
Object.keys(n).forEach(function(k) {
2119
var d = Object.getOwnPropertyDescriptor(n, k);
@@ -183,14 +181,10 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
183181
case "IMPORT_MESSAGE_TO_EVENT":
184182
case "メッセージをイベントにインポート":
185183
addMessage("import message to event. \n/ メッセージをイベントにインポートします。");
186-
if (args[0])
187-
Laurus.Text2Frame.FileFolder = args[0];
188-
if (args[1])
189-
Laurus.Text2Frame.FileName = args[1];
190-
if (args[2])
191-
Laurus.Text2Frame.MapID = args[2];
192-
if (args[3])
193-
Laurus.Text2Frame.EventID = args[3];
184+
if (args[0]) Laurus.Text2Frame.FileFolder = args[0];
185+
if (args[1]) Laurus.Text2Frame.FileName = args[1];
186+
if (args[2]) Laurus.Text2Frame.MapID = args[2];
187+
if (args[3]) Laurus.Text2Frame.EventID = args[3];
194188
if (args[4] && (args[4].toLowerCase() === "true" || args[4].toLowerCase() === "false")) {
195189
Laurus.Text2Frame.IsOverwrite = args[4].toLowerCase() === "true";
196190
addWarning("【警告】5番目の引数に上書き判定を設定することは非推奨に");
@@ -199,8 +193,7 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
199193
} else if (args[4]) {
200194
Laurus.Text2Frame.PageID = args[4];
201195
}
202-
if (args[5] && args[5].toLowerCase() === "true")
203-
Laurus.Text2Frame.IsOverwrite = true;
196+
if (args[5] && args[5].toLowerCase() === "true") Laurus.Text2Frame.IsOverwrite = true;
204197
if (args[0] || args[1]) {
205198
const { PATH_SEP, BASE_PATH } = getDirParams();
206199
Laurus.Text2Frame.TextPath = `${BASE_PATH}${PATH_SEP}${Laurus.Text2Frame.FileFolder}${PATH_SEP}${Laurus.Text2Frame.FileName}`;
@@ -362,6 +355,60 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
362355
plugin_command.parameters[0] = text;
363356
return plugin_command;
364357
};
358+
const replacer = function(key, value) {
359+
if (typeof value === "object" && value !== null) {
360+
return value;
361+
}
362+
return String(value);
363+
};
364+
const parseMzArg = function(args_string) {
365+
const args2 = [];
366+
let buffer = "";
367+
let braceLevel = 0;
368+
for (const char of args_string) {
369+
if (char === "," && braceLevel === 0) {
370+
args2.push(buffer.trim());
371+
buffer = "";
372+
} else {
373+
buffer += char;
374+
if (char === "[" || char === "{") {
375+
braceLevel++;
376+
} else if (char === "]" || char === "}") {
377+
braceLevel--;
378+
}
379+
}
380+
}
381+
if (buffer) {
382+
args2.push(buffer.trim());
383+
}
384+
return args2;
385+
};
386+
const parseNestedJSON = function(jsonString) {
387+
let jsonObject;
388+
try {
389+
jsonObject = JSON.parse(jsonString);
390+
} catch (error) {
391+
return jsonString;
392+
}
393+
for (const key in jsonObject) {
394+
if (typeof jsonObject[key] === "string") {
395+
try {
396+
jsonObject[key] = parseNestedJSON(jsonObject[key]);
397+
} catch (error) {
398+
continue;
399+
}
400+
}
401+
}
402+
return jsonObject;
403+
};
404+
const stringifyNestedJSON = function(jsonObject) {
405+
for (const key in jsonObject) {
406+
if (typeof jsonObject[key] === "object" && jsonObject[key] !== null) {
407+
jsonObject[key] = stringifyNestedJSON(jsonObject[key]);
408+
}
409+
}
410+
return JSON.stringify(jsonObject, replacer);
411+
};
365412
const getPluginCommandEventMZ = function(plugin_name, plugin_command, disp_plugin_command, args2) {
366413
const plugin_args = {};
367414
const plugin_command_mz = {
@@ -380,7 +427,13 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
380427
if (matched) {
381428
const arg_name = matched[1] || "";
382429
const values = matched[2].slice(1, -1).split("][") || [];
383-
plugin_args[arg_name] = values[0] || "";
430+
if (["struct_arg", "bool_array_arg", "number_array_arg"].includes(arg_name)) {
431+
const json_obj = parseNestedJSON(values[0]);
432+
plugin_args[arg_name] = stringifyNestedJSON(json_obj);
433+
} else {
434+
plugin_args[arg_name] = values[0] || "";
435+
plugin_args[arg_name] = plugin_args[arg_name].replace(/\\n/g, "\n").replace(/\\t/g, " ").replace(/\\\\/g, "\\");
436+
}
384437
}
385438
}
386439
return plugin_command_mz;
@@ -391,7 +444,13 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
391444
if (matched) {
392445
let arg_name = matched[1] || "";
393446
const values = matched[2].slice(1, -1).split("][") || [];
394-
const value = values[0] || "";
447+
let value = values[0] || "";
448+
if (["struct_arg", "bool_array_arg", "number_array_arg"].includes(arg_name)) {
449+
const json_obj = parseNestedJSON(values[0]);
450+
value = stringifyNestedJSON(json_obj);
451+
} else {
452+
value = value.replace(/\\n/g, " ").replace(/\\t/g, " ").replace(/\\\\/g, " ");
453+
}
395454
if (values[1]) {
396455
arg_name = values[1];
397456
}
@@ -1452,15 +1511,13 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
14521511
const getIfWeaponParameters = function(weaponId, params) {
14531512
weaponId = Math.max(Number(weaponId) || 1, 1);
14541513
let include_equipment = false;
1455-
if (params[0])
1456-
include_equipment = true;
1514+
if (params[0]) include_equipment = true;
14571515
return [9, weaponId, include_equipment];
14581516
};
14591517
const getIfArmorParameters = function(armorId, params) {
14601518
armorId = Math.max(Number(armorId) || 1, 1);
14611519
let include_equipment = false;
1462-
if (params[0])
1463-
include_equipment = true;
1520+
if (params[0]) include_equipment = true;
14641521
return [10, armorId, include_equipment];
14651522
};
14661523
const getIfButtonParameters = function(params) {
@@ -2106,22 +2163,16 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
21062163
const LOOP_CODE = 112;
21072164
const stack = events.reduce((s, e) => {
21082165
const code = e.code;
2109-
if (code === IF_CODE)
2110-
s.push(IF_CODE);
2111-
else if (code === ELSE_CODE)
2112-
s.push(ELSE_CODE);
2113-
else if (code === BOTTOM_CODE)
2114-
s.pop();
2166+
if (code === IF_CODE) s.push(IF_CODE);
2167+
else if (code === ELSE_CODE) s.push(ELSE_CODE);
2168+
else if (code === BOTTOM_CODE) s.pop();
21152169
return s;
21162170
}, []);
21172171
const bottom = stack.reduce((b, code) => {
21182172
b.push(getCommandBottomEvent());
2119-
if (code === IF_CODE)
2120-
b.push(getEnd());
2121-
else if (code === ELSE_CODE)
2122-
b.push(getEnd());
2123-
else if (code === LOOP_CODE)
2124-
b.push(getRepeatAbove());
2173+
if (code === IF_CODE) b.push(getEnd());
2174+
else if (code === ELSE_CODE) b.push(getEnd());
2175+
else if (code === LOOP_CODE) b.push(getRepeatAbove());
21252176
return b;
21262177
}, []);
21272178
return events.concat(bottom);
@@ -2305,7 +2356,7 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
23052356
return [getPluginCommandEvent(plugin_command[1])];
23062357
}
23072358
if (plugin_command_mz) {
2308-
const params = plugin_command_mz[1].split(",").map((s) => s.trim());
2359+
const params = parseMzArg(plugin_command_mz[1]);
23092360
const event_command_list3 = [];
23102361
if (params.length > 2) {
23112362
const arg_plugin_name = params[0];
@@ -3639,8 +3690,7 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
36393690
const params = transfer_player[1].split(",").map((s) => s.trim().toLowerCase());
36403691
const regex = /(.*?)\[(\d+)]\[(\d+)]\[(\d+)]/;
36413692
const matches = params[0].match(regex);
3642-
if (!matches)
3643-
throw new Error("Syntax error. / 文法エラーです。:" + params[0]);
3693+
if (!matches) throw new Error("Syntax error. / 文法エラーです。:" + params[0]);
36443694
const location = getLocationValue(matches[1]);
36453695
const mapId = parseInt(matches[2]);
36463696
const mapX = parseInt(matches[3]);
@@ -3654,8 +3704,7 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
36543704
const vehicle = getVehicleValue(params[0]);
36553705
const regex = /(.*?)\[(\d+)]\[(\d+)]\[(\d+)]/;
36563706
const matches = params[1].match(regex);
3657-
if (!matches)
3658-
throw new Error("Syntax error. / 文法エラーです。:" + params[1]);
3707+
if (!matches) throw new Error("Syntax error. / 文法エラーです。:" + params[1]);
36593708
const location = getLocationValue(matches[1]);
36603709
const mapId = parseInt(matches[2]);
36613710
const mapX = parseInt(matches[3]);
@@ -3667,8 +3716,7 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
36673716
const event = getCharacterValue(params[0]);
36683717
const regex = /(.*?)\[(.*?)](\[(\d+)])?(\[(\d+)])?/;
36693718
const matches = params[1].match(regex);
3670-
if (!matches)
3671-
throw new Error("Syntax error. / 文法エラーです。:" + params[1]);
3719+
if (!matches) throw new Error("Syntax error. / 文法エラーです。:" + params[1]);
36723720
const location = getLocationValue(matches[1]);
36733721
let mapX = 0;
36743722
let mapY = 0;
@@ -4117,8 +4165,7 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
41174165
const infoType = getLocationInfoTypeValue(params[1]);
41184166
const regex = /^(.*?)\[(.*?)](\[(\d+)])?/;
41194167
const matches = params[2].match(regex);
4120-
if (!matches)
4121-
throw new Error("Syntax error. / 文法エラーです。:" + params[2]);
4168+
if (!matches) throw new Error("Syntax error. / 文法エラーです。:" + params[2]);
41224169
const { locationType, locationX, locationY } = getLocationEvent(matches[1], matches[2], matches[4]);
41234170
return [getGetLocationInfo(variableId, infoType, locationType, locationX, locationY)];
41244171
}

0 commit comments

Comments
 (0)