Sync with rauenzi/BetterDiscordApp 587aeaa
fixes for new users (even if lightcord does already this)
This commit is contained in:
parent
588a0f6f3a
commit
b1d0789881
|
@ -1,91 +1,99 @@
|
||||||
const __non_webpack_require__ = window.require
|
const __non_webpack_require__ = window.require
|
||||||
|
|
||||||
import {bdConfig} from "../0globals";
|
import {bdConfig} from "../0globals";
|
||||||
import Utils from "./utils";
|
import Utils from "./utils";
|
||||||
import ContentManager from "./contentManager";
|
import ContentManager from "./contentManager";
|
||||||
|
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const releaseChannel = DiscordNative.globals ? DiscordNative.globals.releaseChannel : DiscordNative.app ? DiscordNative.app.getReleaseChannel() : "stable";
|
const releaseChannel = DiscordNative.globals ? DiscordNative.globals.releaseChannel : DiscordNative.app ? DiscordNative.app.getReleaseChannel() : "stable";
|
||||||
|
|
||||||
export default new class DataStore {
|
let dataPath = "";
|
||||||
constructor() {
|
if (process.platform === "win32") dataPath = process.env.APPDATA;
|
||||||
this.data = {settings: {stable: {}, canary: {}, ptb: {}}};
|
else if (process.platform === "darwin") dataPath = path.join(process.env.HOME, "Library", "Preferences");
|
||||||
this.pluginData = {};
|
else dataPath = path.join(process.env.XDG_CONFIG_HOME ? process.env.XDG_CONFIG_HOME : process.env.HOME, ".config");
|
||||||
window.Lightcord.BetterDiscord.DataStore = this
|
|
||||||
}
|
export default new class DataStore {
|
||||||
|
constructor() {
|
||||||
initialize() {
|
this.data = {settings: {stable: {}, canary: {}, ptb: {}}};
|
||||||
try {
|
this.pluginData = {};
|
||||||
if (!fs.existsSync(this.BDFile)) fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4), "binary");
|
window.Lightcord.BetterDiscord.DataStore = this
|
||||||
const data = JSON.parse(fs.readFileSync(this.BDFile, "binary"))
|
}
|
||||||
if (data.hasOwnProperty("settings")) this.data = data;
|
|
||||||
if (!fs.existsSync(this.settingsFile)) return;
|
initialize() {
|
||||||
let settings = __non_webpack_require__(this.settingsFile);
|
try {
|
||||||
fs.unlinkSync(this.settingsFile);
|
if (!fs.existsSync(dataPath)) fs.mkdirSync(dataPath);
|
||||||
if (settings.hasOwnProperty("settings")) settings = Object.assign({stable: {}, canary: {}, ptb: {}}, {[releaseChannel]: settings});
|
if (!fs.existsSync(path.join(dataPath, "plugins"))) fs.mkdirSync(path.join(dataPath, "plugins"));
|
||||||
else settings = Object.assign({stable: {}, canary: {}, ptb: {}}, settings);
|
if (!fs.existsSync(path.join(dataPath, "themes"))) fs.mkdirSync(path.join(dataPath, "themes"));
|
||||||
this.setBDData("settings", settings);
|
if (!fs.existsSync(this.BDFile)) fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4), "binary");
|
||||||
}
|
const data = JSON.parse(fs.readFileSync(this.BDFile, "binary"))
|
||||||
catch (err) {
|
if (data.hasOwnProperty("settings")) this.data = data;
|
||||||
console.error(err);
|
if (!fs.existsSync(this.settingsFile)) return;
|
||||||
Utils.alert("Corrupt Storage", "The bd storage has somehow become corrupt. You may either try to salvage the file or delete it then reload.");
|
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);
|
||||||
get injectionPath() {
|
this.setBDData("settings", settings);
|
||||||
return this._injectionPath = null;
|
}
|
||||||
if (this._injectionPath) return this._injectionPath;
|
catch (err) {
|
||||||
const electron = require("electron").remote.app;
|
console.error(err);
|
||||||
const base = electron.getAppPath();
|
Utils.alert("Corrupt Storage", "The bd storage has somehow become corrupt. You may either try to salvage the file or delete it then reload.");
|
||||||
const roamingBase = electron.getPath("userData");
|
}
|
||||||
const roamingLocation = path.resolve(roamingBase, electron.getVersion(), "modules", "discord_desktop_core", "injector");
|
}
|
||||||
const location = path.resolve(base, "..", "app");
|
|
||||||
const realLocation = fs.existsSync(location) ? location : fs.existsSync(roamingLocation) ? roamingLocation : null;
|
get injectionPath() {
|
||||||
if (!realLocation) return this._injectionPath = null;
|
return this._injectionPath = null;
|
||||||
return this._injectionPath = realLocation;
|
if (this._injectionPath) return this._injectionPath;
|
||||||
}
|
const electron = require("electron").remote.app;
|
||||||
|
const base = electron.getAppPath();
|
||||||
get configFile() {return this._configFile || (this._configFile = path.resolve(this.injectionPath, "betterdiscord", "config.json"));}
|
const roamingBase = electron.getPath("userData");
|
||||||
get BDFile() {return this._BDFile || (this._BDFile = path.resolve(bdConfig.dataPath, "bdstorage.json"));}
|
const roamingLocation = path.resolve(roamingBase, electron.getVersion(), "modules", "discord_desktop_core", "injector");
|
||||||
get settingsFile() {return this._settingsFile || (this._settingsFile = path.resolve(bdConfig.dataPath, "bdsettings.json"));}
|
const location = path.resolve(base, "..", "app");
|
||||||
getPluginFile(pluginName) {return path.resolve(ContentManager.pluginsFolder, pluginName + ".config.json");}
|
const realLocation = fs.existsSync(location) ? location : fs.existsSync(roamingLocation) ? roamingLocation : null;
|
||||||
|
if (!realLocation) return this._injectionPath = null;
|
||||||
getSettingGroup(key) {
|
return this._injectionPath = realLocation;
|
||||||
return this.data.settings[releaseChannel][key] || null;
|
}
|
||||||
}
|
|
||||||
|
get configFile() {return this._configFile || (this._configFile = path.resolve(this.injectionPath, "betterdiscord", "config.json"));}
|
||||||
setSettingGroup(key, data) {
|
get BDFile() {return this._BDFile || (this._BDFile = path.resolve(dataPath, "bdstorage.json"));}
|
||||||
this.data.settings[releaseChannel][key] = data;
|
get settingsFile() {return this._settingsFile || (this._settingsFile = path.resolve(dataPath, "bdsettings.json"));}
|
||||||
fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4), "binary");
|
getPluginFile(pluginName) {return path.resolve(ContentManager.pluginsFolder, pluginName + ".config.json");}
|
||||||
}
|
|
||||||
|
getSettingGroup(key) {
|
||||||
getBDData(key) {
|
return this.data.settings[releaseChannel][key] || null;
|
||||||
return this.data[key] || "";
|
}
|
||||||
}
|
|
||||||
|
setSettingGroup(key, data) {
|
||||||
setBDData(key, value) {
|
this.data.settings[releaseChannel][key] = data;
|
||||||
this.data[key] = value;
|
fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4), "binary");
|
||||||
fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4), "binary");
|
}
|
||||||
}
|
|
||||||
|
getBDData(key) {
|
||||||
getPluginData(pluginName, key) {
|
return this.data[key] || "";
|
||||||
if (this.pluginData[pluginName] !== undefined) return this.pluginData[pluginName][key];
|
}
|
||||||
if (!fs.existsSync(this.getPluginFile(pluginName))) return undefined;
|
|
||||||
this.pluginData[pluginName] = JSON.parse(fs.readFileSync(this.getPluginFile(pluginName)));
|
setBDData(key, value) {
|
||||||
return this.pluginData[pluginName][key];
|
this.data[key] = value;
|
||||||
}
|
fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4), "binary");
|
||||||
|
}
|
||||||
setPluginData(pluginName, key, value) {
|
|
||||||
if (value === undefined) return;
|
getPluginData(pluginName, key) {
|
||||||
if (this.pluginData[pluginName] === undefined) this.pluginData[pluginName] = {};
|
if (this.pluginData[pluginName] !== undefined) return this.pluginData[pluginName][key];
|
||||||
this.pluginData[pluginName][key] = value;
|
if (!fs.existsSync(this.getPluginFile(pluginName))) return undefined;
|
||||||
fs.writeFileSync(this.getPluginFile(pluginName), JSON.stringify(this.pluginData[pluginName], null, 4), "binary");
|
this.pluginData[pluginName] = JSON.parse(fs.readFileSync(this.getPluginFile(pluginName)));
|
||||||
}
|
return this.pluginData[pluginName][key];
|
||||||
|
}
|
||||||
deletePluginData(pluginName, key) {
|
|
||||||
if (this.pluginData[pluginName] === undefined) this.pluginData[pluginName] = {};
|
setPluginData(pluginName, key, value) {
|
||||||
delete this.pluginData[pluginName][key];
|
if (value === undefined) return;
|
||||||
fs.writeFileSync(this.getPluginFile(pluginName), JSON.stringify(this.pluginData[pluginName], null, 4), "binary");
|
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), "binary");
|
||||||
|
}
|
||||||
|
|
||||||
|
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), "binary");
|
||||||
|
}
|
||||||
};
|
};
|
Loading…
Reference in New Issue