BetterDiscordApp-rauenzi/src/modules/datastore.js

90 lines
3.4 KiB
JavaScript
Raw Normal View History

2019-05-29 05:48:41 +02:00
import {Config} from "data";
2019-05-28 20:19:48 +02:00
const fs = require("fs");
const path = require("path");
const releaseChannel = DiscordNative.globals.releaseChannel;
2019-06-06 21:57:25 +02:00
// Schema 1
// =======================
// %appdata%\BetterDiscord
// -> data\
// -> [releaseChannel].json (stable/canary/ptb)
// Schema 2
// =======================
// %appdata%\BetterDiscord
// -> data
// -> [releaseChannel]\ (stable/canary/ptb)
// -> settings.json
// -> plugins.json
// -> themes.json
2019-05-29 05:48:41 +02:00
export default new class DataStore {
2019-05-28 20:19:48 +02:00
constructor() {
2019-06-06 21:57:25 +02:00
this.data = {};
2019-05-28 20:19:48 +02:00
this.pluginData = {};
}
initialize() {
2019-06-06 21:57:25 +02:00
if (!fs.existsSync(path.resolve(this.BDFile, ".."))) fs.mkdirSync(path.resolve(this.BDFile, ".."));
2019-05-31 07:53:11 +02:00
if (!fs.existsSync(this.BDFile)) fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4));
2019-06-06 21:57:25 +02:00
this.data = __non_webpack_require__(this.BDFile);
// if (data.hasOwnProperty("settings")) this.data = data;
// if (!fs.existsSync(this.settingsFile)) return;
// let settings = __non_webpack_require__(this.settingsFile);
// fs.unlinkSync(this.settingsFile);
// if (settings.hasOwnProperty("settings")) settings = Object.assign({stable: {}, canary: {}, ptb: {}}, {[releaseChannel]: settings});
// else settings = Object.assign({stable: {}, canary: {}, ptb: {}}, settings);
// this.setBDData("settings", settings);
2019-05-28 20:19:48 +02:00
}
2019-06-06 21:57:25 +02:00
get BDFile() {return this._BDFile || (this._BDFile = path.resolve(Config.dataPath, "data", `${releaseChannel}.json`));}
// get settingsFile() {return this._settingsFile || (this._settingsFile = path.resolve(Config.dataPath, "bdsettings.json"));}
2019-05-28 20:19:48 +02:00
getPluginFile(pluginName) {return path.resolve(Config.dataPath, "plugins", pluginName + ".config.json");}
2019-06-06 21:57:25 +02:00
// getSettingGroup(key) {
// return this.data.settings[key] || null;
// }
// setSettingGroup(key, data) {
// this.data.settings[key] = data;
// fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4));
// }
getBDData(key) {
return this.data[key] || "";
2019-05-28 20:19:48 +02:00
}
2019-06-06 21:57:25 +02:00
setBDData(key, value) {
this.data[key] = value;
2019-05-28 20:19:48 +02:00
fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4));
}
2019-06-06 21:57:25 +02:00
getData(key) {
2019-05-28 20:19:48 +02:00
return this.data[key] || "";
}
2019-06-06 21:57:25 +02:00
setData(key, value) {
2019-05-28 20:19:48 +02:00
this.data[key] = value;
fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4));
}
getPluginData(pluginName, key) {
if (this.pluginData[pluginName] !== undefined) return this.pluginData[pluginName][key] || undefined;
if (!fs.existsSync(this.getPluginFile(pluginName))) return undefined;
this.pluginData[pluginName] = JSON.parse(fs.readFileSync(this.getPluginFile(pluginName)));
return this.pluginData[pluginName][key] || undefined;
}
setPluginData(pluginName, key, value) {
if (value === undefined) return;
if (this.pluginData[pluginName] === undefined) this.pluginData[pluginName] = {};
this.pluginData[pluginName][key] = value;
fs.writeFileSync(this.getPluginFile(pluginName), JSON.stringify(this.pluginData[pluginName], null, 4));
}
deletePluginData(pluginName, key) {
if (this.pluginData[pluginName] === undefined) this.pluginData[pluginName] = {};
delete this.pluginData[pluginName][key];
fs.writeFileSync(this.getPluginFile(pluginName), JSON.stringify(this.pluginData[pluginName], null, 4));
}
2019-05-29 05:48:41 +02:00
};