diff --git a/config.js b/config.js new file mode 100644 index 0000000..f2ca3da --- /dev/null +++ b/config.js @@ -0,0 +1,51 @@ +const path = require('path'); +const fs = require('fs'); + +const configFolder = path.join(process.env['LOCALAPPDATA'], 'EZPPLauncher'); +if (!fs.existsSync(configFolder)) fs.mkdirSync(configFolder); + +const configLocation = path.join(configFolder, `ezpplauncher.${path.basename(process.env['HOME'])}.cfg`); +if (!fs.existsSync(configLocation)) fs.writeFileSync(configLocation, ""); + + + +async function get(key, defaultValue) { + const fileStream = await fs.promises.readFile(configLocation, "utf-8"); + const lines = fileStream.split(/\r?\n/) + for (const line of lines) { + if (line.includes('=')) { + const argsPair = line.split('=', 2); + const keyname = argsPair[0] + const value = argsPair[1]; + if (keyname == key) { + return value; + } + } + } + return defaultValue; +} + +async function set(key, value) { + console.log("setting " + key + " to " + value); + const configValues = new Map(); + const fileStream = await fs.promises.readFile(configLocation, "utf-8"); + const lines = fileStream.split(/\r?\n/) + for (const line of lines) { + if (line.includes('=')) { + const argsPair = line.split('=', 2); + const keyname = argsPair[0] + const value = argsPair[1]; + configValues.set(keyname, value); + } + } + + configValues.set(key, value); + + const arr = []; + for (var [storkey, storvalue] of configValues.entries()) + arr.push(`${storkey}=${storvalue}`); + + await fs.promises.writeFile(configLocation, arr.join('\n')); +} + +module.exports = { get, set } \ No newline at end of file