Fix various bugs

- Fix an issue removing settings collection
- Fix bugs related to opening in system editor
- Check for guild glasses (in webpack) a little bit later to allow for slower loading
- Add addition try..catch around file operations in DataStore
This commit is contained in:
Zack Rauen 2021-04-15 18:03:59 -04:00
parent fd43ebf7b8
commit 01941c1178
4 changed files with 46 additions and 42 deletions

View File

@ -16,6 +16,8 @@ const React = DiscordModules.React;
const path = require("path");
const fs = require("fs");
const Module = require("module").Module;
const shell = require("electron").shell;
const openItem = shell.openItem || shell.openPath;
const splitRegex = /[^\S\r\n]*?\r?(?:\r\n|\n)[^\S\r\n]*?\*[^\S\r\n]?/;
const escapedAtRegex = /^\\@/;
@ -205,10 +207,6 @@ export default class AddonManager {
}
const addon = __non_webpack_require__(path.resolve(this.addonFolder, filename));
// console.log(addon);
// await Promise.resolve(addon);
// addon = __non_webpack_require__(path.resolve(this.addonFolder, filename));
// console.log(addon);
if (this.addonList.find(c => c.id == addon.id)) return new AddonError(addon.name, filename, Strings.Addons.alreadyExists.format({type: this.prefix, name: addon.name}), this.prefix);
const error = this.initializeAddon(addon);
@ -343,7 +341,7 @@ export default class AddonManager {
const addon = typeof(idOrFileOrAddon) == "string" ? this.addonList.find(c => c.id == idOrFileOrAddon || c.filename == idOrFileOrAddon) : idOrFileOrAddon;
const fullPath = path.resolve(this.addonFolder, addon.filename);
if (typeof(system) == "undefined") system = Settings.get("settings", "addons", "editAction") == "system";
if (system) return require("electron").shell.openItem(`${fullPath}`);
if (system) return openItem(`${fullPath}`);
return this.openDetached(addon);
}

View File

@ -18,8 +18,6 @@ import IPC from "./ipc";
import LoadingIcon from "../loadingicon";
import Styles from "../styles/index.css";
const GuildClasses = DiscordModules.GuildClasses;
export default new class Core {
async startup() {
if (this.hasStarted) return;
@ -89,6 +87,8 @@ export default new class Core {
}
waitForGuilds() {
// TODO: experiment with waiting for CONNECTION_OPEN event instead
const GuildClasses = DiscordModules.GuildClasses;
return new Promise(resolve => {
const checkForGuilds = function () {
if (document.readyState != "complete") setTimeout(checkForGuilds, 100);

View File

@ -37,18 +37,24 @@ export default new class DataStore {
if (!fs.existsSync(this.dataFolder)) fs.mkdirSync(this.dataFolder);
// if (!fs.existsSync(this.localeFolder)) fs.mkdirSync(this.localeFolder);
if (!fs.existsSync(this.emoteFolder)) fs.mkdirSync(this.emoteFolder);
// if (!fs.existsSync(this.emoteFolder)) fs.mkdirSync(this.emoteFolder);
if (!fs.existsSync(this.cacheFile)) fs.writeFileSync(this.cacheFile, JSON.stringify({}));
// if (!fs.existsSync(this.cacheFile)) fs.writeFileSync(this.cacheFile, JSON.stringify({}));
if (!fs.existsSync(this.customCSS)) fs.writeFileSync(this.customCSS, "");
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));
let data = {};
try {data = __non_webpack_require__(path.resolve(this.dataFolder, file));}
catch (e) {Logger.stacktrace("DataStore", `Could not load file ${file}`, e);}
this.data[file.split(".")[0]] = data;
}
this.cacheData = Utilities.testJSON(fs.readFileSync(this.cacheFile).toString()) || {};
// this.cacheData = Utilities.testJSON(fs.readFileSync(this.cacheFile).toString()) || {};
if (!newStorageExists) this.convertOldData(); // Convert old data if it exists (routine checks existence and removes existence)
if (newStorageExists) return;
try {this.convertOldData();} // Convert old data if it exists (routine checks existence and removes existence)
catch (e) {Logger.stacktrace("DataStore", `Could not convert old data.`, e);}
}
convertOldData() {
@ -109,9 +115,9 @@ export default new class DataStore {
get customCSS() {return this._customCSS || (this._customCSS = path.resolve(this.dataFolder, "custom.css"));}
get baseFolder() {return this._baseFolder || (this._baseFolder = path.resolve(Config.dataPath, "data"));}
get dataFolder() {return this._dataFolder || (this._dataFolder = path.resolve(this.baseFolder, `${releaseChannel}`));}
get localeFolder() {return this._localeFolder || (this._localeFolder = path.resolve(this.baseFolder, `locales`));}
get emoteFolder() {return this._emoteFolder || (this._emoteFolder = path.resolve(this.baseFolder, `emotes`));}
get cacheFile() {return this._cacheFile || (this._cacheFile = path.resolve(this.baseFolder, `.cache`));}
// get localeFolder() {return this._localeFolder || (this._localeFolder = path.resolve(this.baseFolder, `locales`));}
// get emoteFolder() {return this._emoteFolder || (this._emoteFolder = path.resolve(this.baseFolder, `emotes`));}
// get cacheFile() {return this._cacheFile || (this._cacheFile = path.resolve(this.baseFolder, `.cache`));}
getPluginFile(pluginName) {return path.resolve(Config.dataPath, "plugins", pluginName + ".config.json");}
@ -139,37 +145,37 @@ export default new class DataStore {
fs.writeFileSync(path.resolve(this.localeFolder, `${locale}.json`), JSON.stringify(strings, null, 4));
}
getCacheHash(category, key) {
if (!this.cacheData[category]) return "";
if (!fs.existsSync(path.resolve(this.baseFolder, category, `${key}.json`))) return "";
return this.cacheData[category][key] || "";
}
// getCacheHash(category, key) {
// if (!this.cacheData[category]) return "";
// if (!fs.existsSync(path.resolve(this.baseFolder, category, `${key}.json`))) return "";
// return this.cacheData[category][key] || "";
// }
setCacheHash(category, key, hash) {
if (!this.cacheData[category]) this.cacheData[category] = {};
this.cacheData[category][key] = hash;
fs.writeFileSync(this.cacheFile, JSON.stringify(this.cacheData));
}
// setCacheHash(category, key, hash) {
// if (!this.cacheData[category]) this.cacheData[category] = {};
// this.cacheData[category][key] = hash;
// fs.writeFileSync(this.cacheFile, JSON.stringify(this.cacheData));
// }
invalidateCache(category, key) {
if (!this.cacheData[category]) return;
delete this.cacheData[category][key];
fs.writeFileSync(this.cacheFile, JSON.stringify(this.cacheData));
}
// invalidateCache(category, key) {
// if (!this.cacheData[category]) return;
// delete this.cacheData[category][key];
// fs.writeFileSync(this.cacheFile, JSON.stringify(this.cacheData));
// }
emotesExist(category) {
return fs.existsSync(path.resolve(this.emoteFolder, `${category}.json`));
}
// 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());
}
// 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));
}
// saveEmoteData(category, data) {
// fs.writeFileSync(path.resolve(this.emoteFolder, `${category}.json`), JSON.stringify(data));
// }
getData(key) {
return this.data[key] || "";

View File

@ -36,7 +36,7 @@ export default new class SettingsManager {
removeCollection(id) {
const location = this.collections.findIndex(c => c.id == id);
if (!location < 0) return Logger.error("Settings", "No collection with id " + id);
if (location < 0) return Logger.error("Settings", "No collection with id " + id);
this.collections.splice(location, 1);
}