Skip to content

Commit

Permalink
Merge pull request #127 from yktsr/125-fix-plugin-command-mz
Browse files Browse the repository at this point in the history
  • Loading branch information
yktsr authored Sep 8, 2024
2 parents bfeec1f + 9d717d9 commit d9b3d26
Show file tree
Hide file tree
Showing 9 changed files with 1,937 additions and 964 deletions.
45 changes: 41 additions & 4 deletions Frame2Text.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//= ============================================================================
// Frame2Text.js
// ----------------------------------------------------------------------------
// (C)2023 Shick
// (C)2023-2024 Shick
// This software is released under the MIT License.
// http://opensource.org/licenses/mit-license.php
// ----------------------------------------------------------------------------
// Version
// 1.0.1 2024/09/07:
// ・#125 プラグインコマンドMZを変換する際、オブジェクト型を取り扱えない不具合の修正
// 1.0.0 2024/01/20 Initial Version
// 0.1.0 2023/09/25 新規作成
//= ============================================================================
Expand Down Expand Up @@ -278,6 +280,12 @@
* プラグイン作者は、いかなる場合も破損したプロジェクトの復元には
* 応じられませんのでご注意ください。
* テキストファイルの文字コードはUTF-8にのみ対応しています。
*
* --------------------------------------
* Version
* --------------------------------------
* 1.0.1
* build: 81bc070a76bcd0246713b2872df90650ffc7ce70
*/
/* eslint-enable spaced-comment */

Expand Down Expand Up @@ -901,6 +909,33 @@
else return name
}

// MZのプラグインパラメータをパースする補助関数
const parseMzArg = function (args_string) {
const args = []
let buffer = ''
let braceLevel = 0

for (const char of args_string) {
if (char === ',' && braceLevel === 0) {
args.push(buffer.trim())
buffer = ''
} else {
buffer += char
if (char === '[' || char === '{') {
braceLevel++
} else if (char === ']' || char === '}') {
braceLevel--
}
}
}

if (buffer) {
args.push(buffer.trim())
}

return args
}

// 出力するテキスト変数
// Laurus.Frame2Text.EnglishTagの値を別変数に代入
const decompile = function (map_events, EnglishTag) {
Expand Down Expand Up @@ -2428,21 +2463,22 @@
const parameters = event.parameters[0]
const splitParameters = parameters.split(' ')
const outParameters = `[${splitParameters[0]}]`
const lastIndex = text.lastIndexOf('\n')
const lastIndex = text.lastIndexOf('\n<')
const extractedText = text.substring(lastIndex + 1)
text = text.substring(0, lastIndex)

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

// developer mode
if (typeof require !== 'undefined' && typeof require.main !== 'undefined' && require.main === module) {
const program = require('commander')
const { Command } = require('commander')
const program = new Command()
program
.version('1.0.0')
.usage('[options]')
Expand Down
129 changes: 88 additions & 41 deletions Text2Frame.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ function getDefaultExportFromCjs(x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x;
}
function getAugmentedNamespace(n) {
if (n.__esModule)
return n;
if (n.__esModule) return n;
var f = n.default;
if (typeof f == "function") {
var a = function a2() {
Expand All @@ -14,8 +13,7 @@ function getAugmentedNamespace(n) {
return f.apply(this, arguments);
};
a.prototype = f.prototype;
} else
a = {};
} else a = {};
Object.defineProperty(a, "__esModule", { value: true });
Object.keys(n).forEach(function(k) {
var d = Object.getOwnPropertyDescriptor(n, k);
Expand Down Expand Up @@ -183,14 +181,10 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
case "IMPORT_MESSAGE_TO_EVENT":
case "メッセージをイベントにインポート":
addMessage("import message to event. \n/ メッセージをイベントにインポートします。");
if (args[0])
Laurus.Text2Frame.FileFolder = args[0];
if (args[1])
Laurus.Text2Frame.FileName = args[1];
if (args[2])
Laurus.Text2Frame.MapID = args[2];
if (args[3])
Laurus.Text2Frame.EventID = args[3];
if (args[0]) Laurus.Text2Frame.FileFolder = args[0];
if (args[1]) Laurus.Text2Frame.FileName = args[1];
if (args[2]) Laurus.Text2Frame.MapID = args[2];
if (args[3]) Laurus.Text2Frame.EventID = args[3];
if (args[4] && (args[4].toLowerCase() === "true" || args[4].toLowerCase() === "false")) {
Laurus.Text2Frame.IsOverwrite = args[4].toLowerCase() === "true";
addWarning("【警告】5番目の引数に上書き判定を設定することは非推奨に");
Expand All @@ -199,8 +193,7 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
} else if (args[4]) {
Laurus.Text2Frame.PageID = args[4];
}
if (args[5] && args[5].toLowerCase() === "true")
Laurus.Text2Frame.IsOverwrite = true;
if (args[5] && args[5].toLowerCase() === "true") Laurus.Text2Frame.IsOverwrite = true;
if (args[0] || args[1]) {
const { PATH_SEP, BASE_PATH } = getDirParams();
Laurus.Text2Frame.TextPath = `${BASE_PATH}${PATH_SEP}${Laurus.Text2Frame.FileFolder}${PATH_SEP}${Laurus.Text2Frame.FileName}`;
Expand Down Expand Up @@ -362,6 +355,60 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
plugin_command.parameters[0] = text;
return plugin_command;
};
const replacer = function(key, value) {
if (typeof value === "object" && value !== null) {
return value;
}
return String(value);
};
const parseMzArg = function(args_string) {
const args2 = [];
let buffer = "";
let braceLevel = 0;
for (const char of args_string) {
if (char === "," && braceLevel === 0) {
args2.push(buffer.trim());
buffer = "";
} else {
buffer += char;
if (char === "[" || char === "{") {
braceLevel++;
} else if (char === "]" || char === "}") {
braceLevel--;
}
}
}
if (buffer) {
args2.push(buffer.trim());
}
return args2;
};
const parseNestedJSON = function(jsonString) {
let jsonObject;
try {
jsonObject = JSON.parse(jsonString);
} catch (error) {
return jsonString;
}
for (const key in jsonObject) {
if (typeof jsonObject[key] === "string") {
try {
jsonObject[key] = parseNestedJSON(jsonObject[key]);
} catch (error) {
continue;
}
}
}
return jsonObject;
};
const stringifyNestedJSON = function(jsonObject) {
for (const key in jsonObject) {
if (typeof jsonObject[key] === "object" && jsonObject[key] !== null) {
jsonObject[key] = stringifyNestedJSON(jsonObject[key]);
}
}
return JSON.stringify(jsonObject, replacer);
};
const getPluginCommandEventMZ = function(plugin_name, plugin_command, disp_plugin_command, args2) {
const plugin_args = {};
const plugin_command_mz = {
Expand All @@ -380,7 +427,13 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
if (matched) {
const arg_name = matched[1] || "";
const values = matched[2].slice(1, -1).split("][") || [];
plugin_args[arg_name] = values[0] || "";
if (["struct_arg", "bool_array_arg", "number_array_arg"].includes(arg_name)) {
const json_obj = parseNestedJSON(values[0]);
plugin_args[arg_name] = stringifyNestedJSON(json_obj);
} else {
plugin_args[arg_name] = values[0] || "";
plugin_args[arg_name] = plugin_args[arg_name].replace(/\\n/g, "\n").replace(/\\t/g, " ").replace(/\\\\/g, "\\");
}
}
}
return plugin_command_mz;
Expand All @@ -391,7 +444,13 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
if (matched) {
let arg_name = matched[1] || "";
const values = matched[2].slice(1, -1).split("][") || [];
const value = values[0] || "";
let value = values[0] || "";
if (["struct_arg", "bool_array_arg", "number_array_arg"].includes(arg_name)) {
const json_obj = parseNestedJSON(values[0]);
value = stringifyNestedJSON(json_obj);
} else {
value = value.replace(/\\n/g, " ").replace(/\\t/g, " ").replace(/\\\\/g, " ");
}
if (values[1]) {
arg_name = values[1];
}
Expand Down Expand Up @@ -1452,15 +1511,13 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
const getIfWeaponParameters = function(weaponId, params) {
weaponId = Math.max(Number(weaponId) || 1, 1);
let include_equipment = false;
if (params[0])
include_equipment = true;
if (params[0]) include_equipment = true;
return [9, weaponId, include_equipment];
};
const getIfArmorParameters = function(armorId, params) {
armorId = Math.max(Number(armorId) || 1, 1);
let include_equipment = false;
if (params[0])
include_equipment = true;
if (params[0]) include_equipment = true;
return [10, armorId, include_equipment];
};
const getIfButtonParameters = function(params) {
Expand Down Expand Up @@ -2106,22 +2163,16 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
const LOOP_CODE = 112;
const stack = events.reduce((s, e) => {
const code = e.code;
if (code === IF_CODE)
s.push(IF_CODE);
else if (code === ELSE_CODE)
s.push(ELSE_CODE);
else if (code === BOTTOM_CODE)
s.pop();
if (code === IF_CODE) s.push(IF_CODE);
else if (code === ELSE_CODE) s.push(ELSE_CODE);
else if (code === BOTTOM_CODE) s.pop();
return s;
}, []);
const bottom = stack.reduce((b, code) => {
b.push(getCommandBottomEvent());
if (code === IF_CODE)
b.push(getEnd());
else if (code === ELSE_CODE)
b.push(getEnd());
else if (code === LOOP_CODE)
b.push(getRepeatAbove());
if (code === IF_CODE) b.push(getEnd());
else if (code === ELSE_CODE) b.push(getEnd());
else if (code === LOOP_CODE) b.push(getRepeatAbove());
return b;
}, []);
return events.concat(bottom);
Expand Down Expand Up @@ -2305,7 +2356,7 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
return [getPluginCommandEvent(plugin_command[1])];
}
if (plugin_command_mz) {
const params = plugin_command_mz[1].split(",").map((s) => s.trim());
const params = parseMzArg(plugin_command_mz[1]);
const event_command_list3 = [];
if (params.length > 2) {
const arg_plugin_name = params[0];
Expand Down Expand Up @@ -3639,8 +3690,7 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
const params = transfer_player[1].split(",").map((s) => s.trim().toLowerCase());
const regex = /(.*?)\[(\d+)]\[(\d+)]\[(\d+)]/;
const matches = params[0].match(regex);
if (!matches)
throw new Error("Syntax error. / 文法エラーです。:" + params[0]);
if (!matches) throw new Error("Syntax error. / 文法エラーです。:" + params[0]);
const location = getLocationValue(matches[1]);
const mapId = parseInt(matches[2]);
const mapX = parseInt(matches[3]);
Expand All @@ -3654,8 +3704,7 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
const vehicle = getVehicleValue(params[0]);
const regex = /(.*?)\[(\d+)]\[(\d+)]\[(\d+)]/;
const matches = params[1].match(regex);
if (!matches)
throw new Error("Syntax error. / 文法エラーです。:" + params[1]);
if (!matches) throw new Error("Syntax error. / 文法エラーです。:" + params[1]);
const location = getLocationValue(matches[1]);
const mapId = parseInt(matches[2]);
const mapX = parseInt(matches[3]);
Expand All @@ -3667,8 +3716,7 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
const event = getCharacterValue(params[0]);
const regex = /(.*?)\[(.*?)](\[(\d+)])?(\[(\d+)])?/;
const matches = params[1].match(regex);
if (!matches)
throw new Error("Syntax error. / 文法エラーです。:" + params[1]);
if (!matches) throw new Error("Syntax error. / 文法エラーです。:" + params[1]);
const location = getLocationValue(matches[1]);
let mapX = 0;
let mapY = 0;
Expand Down Expand Up @@ -4117,8 +4165,7 @@ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1
const infoType = getLocationInfoTypeValue(params[1]);
const regex = /^(.*?)\[(.*?)](\[(\d+)])?/;
const matches = params[2].match(regex);
if (!matches)
throw new Error("Syntax error. / 文法エラーです。:" + params[2]);
if (!matches) throw new Error("Syntax error. / 文法エラーです。:" + params[2]);
const { locationType, locationX, locationY } = getLocationEvent(matches[1], matches[2], matches[4]);
return [getGetLocationInfo(variableId, infoType, locationType, locationX, locationY)];
}
Expand Down
Loading

0 comments on commit d9b3d26

Please sign in to comment.