From 549ab74bf26bd5da44cfd89f6c23ea60a0f8bf2f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Mon, 17 Feb 2020 02:47:22 +0100 Subject: [PATCH] process INCLUDE directive when loading file --- engine.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/engine.js b/engine.js index d4b9209..0390d83 100644 --- a/engine.js +++ b/engine.js @@ -26,8 +26,23 @@ function Textfile(txtfile) { fetch.open("GET", txtfile, false); fetch.send(); + function merge(obj, file) { + for (each in file) { + if (Array.isArray(obj[each]) && Array.isArray(file[each])) obj[each] = [ ...obj[each], ...file[each] ]; + else if (Array.isArray(obj[each])) obj[each] = [ ...obj[each], file[each] ]; + else if (Array.isArray(file[each])) obj[each] = [ obj[each], ...file[each] ]; + else if (each in obj) obj[each] = [ obj[each], file[each] ]; + else obj[each] = file[each]; + } + } + var line, key, value, ref = this, section; for (line of fetch.responseText.split('\n')) switch(true) { + // INCLUDE file + case /^INCLUDE .+$/.test(line): + //console.log("Including: " + line.split(/ /)[1]); + merge(ref, new Textfile(line.split(/ /)[1])); + break; // section headers that always become array elements case /^\[(npc|event|layer|dialog)\]$/.test(line): section = line.split(/[\]\[]/)[1]; ref = {}; @@ -41,6 +56,10 @@ function Textfile(txtfile) { else if (!Array.isArray(this[section])) this[section] = [this[section],ref]; else if ( Array.isArray(this[section])) this[section].push(ref); break; + // Clear section on empty line + case /^ *$/.test(line): + ref = this; + break; // frame definition from animation files // frame=#frame,direction,srcX,srcY,width,height,dstX,dstY case /^frame=[0-9]+,[0-7](,[0-9-]+){6}$/.test(line): -- 2.39.2