BetterDiscordApp-rauenzi/src/modules/datastore.js

138 lines
5.6 KiB
JavaScript
Raw Normal View History

2019-05-29 05:48:41 +02:00
import {Config} from "data";
2019-06-27 04:31:18 +02:00
import Utilities from "./utilities";
2019-05-28 20:19:48 +02:00
const fs = require("fs");
const path = require("path");
const releaseChannel = DiscordNative.globals.releaseChannel;
2019-06-27 04:31:18 +02:00
// Schema
2019-06-06 21:57:25 +02:00
// =======================
// %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-07 05:52:08 +02:00
this.data = {misc: {}};
2019-05-28 20:19:48 +02:00
this.pluginData = {};
2019-06-28 07:36:05 +02:00
this.cacheData = {};
2019-05-28 20:19:48 +02:00
}
initialize() {
2019-06-07 05:52:08 +02:00
if (!fs.existsSync(this.baseFolder)) fs.mkdirSync(this.baseFolder);
if (!fs.existsSync(this.dataFolder)) fs.mkdirSync(this.dataFolder);
2019-06-27 04:31:18 +02:00
if (!fs.existsSync(this.localeFolder)) fs.mkdirSync(this.localeFolder);
2019-06-28 07:36:05 +02:00
if (!fs.existsSync(this.emoteFolder)) fs.mkdirSync(this.emoteFolder);
if (!fs.existsSync(this.cacheFile)) fs.writeFileSync(this.cacheFile, JSON.stringify({}));
2019-06-10 22:37:50 +02:00
if (!fs.existsSync(this.customCSS)) fs.writeFileSync(this.customCSS, "");
2019-06-07 05:52:08 +02:00
const dataFiles = fs.readdirSync(this.dataFolder).filter(f => !fs.statSync(path.resolve(this.dataFolder, f)).isDirectory() && f.endsWith(".json"));
for (const file of dataFiles) {
this.data[file.split(".")[0]] = __non_webpack_require__(path.resolve(this.dataFolder, file));
}
2019-06-28 07:36:05 +02:00
this.cacheData = Utilities.testJSON(fs.readFileSync(this.cacheFile).toString()) || {};
2019-05-28 20:19:48 +02:00
}
2019-06-10 22:37:50 +02:00
get customCSS() {return this._customCSS || (this._customCSS = path.resolve(this.dataFolder, "custom.css"));}
2019-06-07 05:52:08 +02:00
get baseFolder() {return this._baseFolder || (this._baseFolder = path.resolve(Config.dataPath, "data"));}
get dataFolder() {return this._dataFolder || (this._dataFolder = path.resolve(this.baseFolder, `${releaseChannel}`));}
2019-06-27 04:31:18 +02:00
get localeFolder() {return this._localeFolder || (this._localeFolder = path.resolve(this.baseFolder, `locales`));}
2019-06-28 07:36:05 +02:00
get emoteFolder() {return this._emoteFolder || (this._emoteFolder = path.resolve(this.baseFolder, `emotes`));}
get cacheFile() {return this._cacheFile || (this._cacheFile = path.resolve(this.baseFolder, `.cache`));}
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
2019-06-07 05:52:08 +02:00
_getFile(key) {
if (key == "settings" || key == "plugins" || key == "themes") return path.resolve(this.dataFolder, `${key}.json`);
return path.resolve(this.dataFolder, `misc.json`);
}
2019-06-06 21:57:25 +02:00
getBDData(key) {
2019-06-07 05:52:08 +02:00
return this.data.misc[key] || "";
2019-05-28 20:19:48 +02:00
}
2019-06-06 21:57:25 +02:00
setBDData(key, value) {
2019-06-07 05:52:08 +02:00
this.data.misc[key] = value;
fs.writeFileSync(path.resolve(this.dataFolder, `misc.json`), JSON.stringify(this.data.misc, null, 4));
2019-05-28 20:19:48 +02:00
}
2019-06-27 04:31:18 +02:00
getLocale(locale) {
const file = path.resolve(this.localeFolder, `${locale}.json`);
if (!fs.existsSync(file)) return null;
return Utilities.testJSON(fs.readFileSync(file).toString());
}
saveLocale(locale, strings) {
fs.writeFileSync(path.resolve(this.localeFolder, `${locale}.json`), JSON.stringify(strings, null, 4));
}
2019-06-28 07:36:05 +02:00
getCacheHash(category, key) {
if (!this.cacheData[category]) return "";
if (!fs.existsSync(path.resolve(this.baseFolder, category, `${key}.json`))) return "";
return this.cacheData[category][key] || "";
2019-06-28 01:50:20 +02:00
}
2019-06-28 07:36:05 +02:00
setCacheHash(category, key, hash) {
if (!this.cacheData[category]) this.cacheData[category] = {};
this.cacheData[category][key] = hash;
2019-06-28 07:58:10 +02:00
fs.writeFileSync(this.cacheFile, JSON.stringify(this.cacheData));
2019-06-28 07:36:05 +02:00
}
2019-06-28 22:44:08 +02:00
invalidateCache(category, key) {
if (!this.cacheData[category]) return;
delete this.cacheData[category][key];
fs.writeFileSync(this.cacheFile, JSON.stringify(this.cacheData));
}
2019-06-28 07:36:05 +02:00
emotesExist(category) {
return fs.existsSync(path.resolve(this.emoteFolder, `${category}.json`));
}
getEmoteData(category) {
const file = path.resolve(this.emoteFolder, `${category}.json`);
if (!fs.existsSync(file)) return null;
return Utilities.testJSON(fs.readFileSync(file).toString());
}
saveEmoteData(category, data) {
fs.writeFileSync(path.resolve(this.emoteFolder, `${category}.json`), JSON.stringify(data));
2019-06-28 01:50:20 +02:00
}
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;
2019-06-07 05:52:08 +02:00
fs.writeFileSync(path.resolve(this.dataFolder, `${key}.json`), JSON.stringify(value, null, 4));
2019-05-28 20:19:48 +02:00
}
2019-06-10 22:37:50 +02:00
loadCustomCSS() {
return fs.readFileSync(this.customCSS).toString();
}
saveCustomCSS(css) {
return fs.writeFileSync(this.customCSS, css);
}
2019-05-28 20:19:48 +02:00
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
};