Skip to content

Commit

Permalink
Fix single \ chars and fruity quote chars in JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
rooklift committed Mar 17, 2024
1 parent ad45e48 commit 861347a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 26 deletions.
22 changes: 9 additions & 13 deletions files/src/modules/config_io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down Expand Up @@ -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();
Expand All @@ -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.
Expand Down
33 changes: 33 additions & 0 deletions files/src/modules/debork_json.js
Original file line number Diff line number Diff line change
@@ -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;
22 changes: 9 additions & 13 deletions files/src/modules/engineconfig_io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down Expand Up @@ -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": [],
Expand All @@ -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.
Expand Down

0 comments on commit 861347a

Please sign in to comment.