diff --git a/files/src/modules/config_io.js b/files/src/modules/config_io.js index 51da48e0..4a2a3362 100644 --- a/files/src/modules/config_io.js +++ b/files/src/modules/config_io.js @@ -5,6 +5,8 @@ const fs = require("fs"); const path = require("path"); const querystring = require("querystring"); +const debork_json = require("./debork_json"); + exports.filename = "config.json"; // To avoid using "remote", we rely on the main process passing userData location in the query... @@ -233,18 +235,6 @@ function fix(cfg) { } } -function debork_json(s) { - - // We used to fix JSON containing single \ characters in paths, but now all - // that really needs to be done is to convert totally blank files into {} - - if (s.length < 50 && s.trim() === "") { - return "{}"; - } - - return s; -} - exports.load = () => { let cfg = new Config(); @@ -253,7 +243,13 @@ exports.load = () => { try { if (fs.existsSync(exports.filepath)) { - Object.assign(cfg, JSON.parse(debork_json(fs.readFileSync(exports.filepath, "utf8")))); + let raw = fs.readFileSync(exports.filepath, "utf8"); + try { + Object.assign(cfg, JSON.parse(raw)); + } catch (err) { + console.log(exports.filename, err.toString(), "...trying to debork..."); + Object.assign(cfg, JSON.parse(debork_json(raw))); + } } } catch (err) { console.log(err.toString()); // alert() might not be available. diff --git a/files/src/modules/debork_json.js b/files/src/modules/debork_json.js new file mode 100644 index 00000000..9c9d1883 --- /dev/null +++ b/files/src/modules/debork_json.js @@ -0,0 +1,33 @@ +"use strict"; + +function replace_all(s, search, replace) { + if (!s.includes(search)) { // Seems to improve speed overall. + return s; + } + return s.split(search).join(replace); +} + +function debork_json(s) { + + // Convert totally blank files into {} + + if (s.length < 50 && s.trim() === "") { + s = "{}"; + } + + // Replace fruity quote characters. Note that these could exist in legit JSON, + // which is why we only call this function if the first parse fails... + + s = replace_all(s, '“', '"'); + s = replace_all(s, '”', '"'); + + // Replace single \ characters + + s = replace_all(s, "\\\\", "correct_zbcyg278gfdakjadjk"); + s = replace_all(s, "\\", "\\\\"); + s = replace_all(s, "correct_zbcyg278gfdakjadjk", "\\\\"); + + return s; +} + +module.exports = debork_json; diff --git a/files/src/modules/engineconfig_io.js b/files/src/modules/engineconfig_io.js index c2f7e7f1..694f2c14 100644 --- a/files/src/modules/engineconfig_io.js +++ b/files/src/modules/engineconfig_io.js @@ -5,6 +5,8 @@ const fs = require("fs"); const path = require("path"); const querystring = require("querystring"); +const debork_json = require("./debork_json"); + exports.filename = "engines.json"; // To avoid using "remote", we rely on the main process passing userData location in the query... @@ -44,18 +46,6 @@ function fix(cfg) { } } -function debork_json(s) { - - // We used to fix JSON containing single \ characters in paths, but now all - // that really needs to be done is to convert totally blank files into {} - - if (s.length < 50 && s.trim() === "") { - return "{}"; - } - - return s; -} - exports.newentry = () => { return { "args": [], @@ -74,7 +64,13 @@ exports.load = () => { try { if (fs.existsSync(exports.filepath)) { - Object.assign(cfg, JSON.parse(debork_json(fs.readFileSync(exports.filepath, "utf8")))); + let raw = fs.readFileSync(exports.filepath, "utf8"); + try { + Object.assign(cfg, JSON.parse(raw)) + } catch (err) { + console.log(exports.filename, err.toString(), "...trying to debork..."); + Object.assign(cfg, JSON.parse(debork_json(raw))); + } } } catch (err) { console.log(err.toString()); // alert() might not be available.