cleanup and fix settings

This commit is contained in:
Zack Rauen 2019-06-28 16:44:08 -04:00
parent 311f87bfc4
commit b2eee5cf4c
9 changed files with 107 additions and 110 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

2
js/main.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1,47 +1,28 @@
import Builtin from "../structs/builtin"; import Builtin from "../structs/builtin";
import {Config, EmoteConfig} from "data"; import {EmoteConfig} from "data";
import {Utilities, WebpackModules, DataStore, DiscordModules, Events, Settings, Strings} from "modules"; import {Utilities, WebpackModules, DataStore, DiscordModules, Events, Settings, Strings} from "modules";
import BDEmote from "../ui/emote"; import BDEmote from "../ui/emote";
import Toasts from "../ui/toasts"; import Toasts from "../ui/toasts";
import FormattableString from "../structs/string"; import FormattableString from "../structs/string";
const request = require("request"); const request = require("request");
// const fs = require("fs");
const EmoteURLs = { const EmoteURLs = {
TwitchGlobal: new FormattableString(`https://static-cdn.jtvnw.net/emoticons/v1/{{id}}/1.0`), TwitchGlobal: new FormattableString(`https://static-cdn.jtvnw.net/emoticons/v1/{{id}}/1.0`),
TwitchSubscriber: new FormattableString(`https://static-cdn.jtvnw.net/emoticons/v1/{{id}}/1.0`), TwitchSubscriber: new FormattableString(`https://static-cdn.jtvnw.net/emoticons/v1/{{id}}/1.0`),
FrankerFaceZ: new FormattableString(`https://cdn.frankerfacez.com/emoticon/{{id}}/1`), FrankerFaceZ: new FormattableString(`https://cdn.frankerfacez.com/emoticon/{{id}}/1`),
BTTV: new FormattableString(`https://cdn.betterttv.net/emote/{{id}}/1x`), BTTV: new FormattableString(`https://cdn.betterttv.net/emote/{{id}}/1x`),
BTTV2: new FormattableString(`https://cdn.betterttv.net/emote/{{id}}/1x`)
};
const EmoteMetaInfo = {
TwitchGlobal: {},
TwitchSubscriber: {},
BTTV: {},
FrankerFaceZ: {},
BTTV2: {}
}; };
const Emotes = { const Emotes = {
TwitchGlobal: {}, TwitchGlobal: {},
TwitchSubscriber: {}, TwitchSubscriber: {},
BTTV: {}, BTTV: {},
FrankerFaceZ: {}, FrankerFaceZ: {}
BTTV2: {}
};
const bdEmoteSettingIDs = {
TwitchGlobal: "twitch",
TwitchSubscriber: "twitch",
BTTV: "bttv",
FrankerFaceZ: "ffz",
BTTV2: "bttv"
}; };
const blacklist = []; const blacklist = [];
const overrides = ["twitch", "bttv", "ffz"]; const overrides = ["twitch", "subscriber", "bttv", "ffz"];
const modifiers = ["flip", "spin", "pulse", "spin2", "spin3", "1spin", "2spin", "3spin", "tr", "bl", "br", "shake", "shake2", "shake3", "flap"]; const modifiers = ["flip", "spin", "pulse", "spin2", "spin3", "1spin", "2spin", "3spin", "tr", "bl", "br", "shake", "shake2", "shake3", "flap"];
export default new class EmoteModule extends Builtin { export default new class EmoteModule extends Builtin {
@ -49,10 +30,10 @@ export default new class EmoteModule extends Builtin {
get collection() {return "settings";} get collection() {return "settings";}
get category() {return "general";} get category() {return "general";}
get id() {return "emotes";} get id() {return "emotes";}
get categories() {return Object.keys(bdEmoteSettingIDs).filter(k => this.isCategoryEnabled(bdEmoteSettingIDs[k]));} get categories() {return Object.keys(Emotes).filter(k => this.isCategoryEnabled(k));}
get shouldDownload() {return Settings.get("emotes", this.category, "download");} get shouldDownload() {return Settings.get("emotes", this.category, "download");}
isCategoryEnabled(id) {return super.get("emotes", "categories", id);} isCategoryEnabled(id) {return super.get("emotes", "categories", id.toLowerCase());}
get(id) {return super.get("emotes", "general", id);} get(id) {return super.get("emotes", "general", id);}
@ -63,7 +44,6 @@ export default new class EmoteModule extends Builtin {
get TwitchSubscriber() {return Emotes.TwitchSubscriber;} get TwitchSubscriber() {return Emotes.TwitchSubscriber;}
get BTTV() {return Emotes.BTTV;} get BTTV() {return Emotes.BTTV;}
get FrankerFaceZ() {return Emotes.FrankerFaceZ;} get FrankerFaceZ() {return Emotes.FrankerFaceZ;}
get BTTV2() {return Emotes.BTTV2;}
get blacklist() {return blacklist;} get blacklist() {return blacklist;}
get favorites() {return this.favoriteEmotes;} get favorites() {return this.favoriteEmotes;}
getUrl(category, name) {return EmoteURLs[category].format({id: Emotes[category][name]});} getUrl(category, name) {return EmoteURLs[category].format({id: Emotes[category][name]});}
@ -74,19 +54,16 @@ export default new class EmoteModule extends Builtin {
initialize() { initialize() {
super.initialize(); super.initialize();
window.emoteModule = this; window.emoteModule = this;
this.favoriteEmotes = {}; const storedFavorites = DataStore.getBDData("favoriteEmotes");
const fe = DataStore.getBDData("bdfavemotes"); this.favoriteEmotes = storedFavorites || {};
if (fe !== "" && fe !== null) this.favoriteEmotes = JSON.parse(window.atob(fe));
this.saveFavorites();
this.addFavorite = this.addFavorite.bind(this); this.addFavorite = this.addFavorite.bind(this);
this.removeFavorite = this.removeFavorite.bind(this); this.removeFavorite = this.removeFavorite.bind(this);
// EmoteConfig; this.onCategoryToggle = this.onCategoryToggle.bind(this);
// emoteCollection.button = {title: "Clear Emote Cache", onClick: () => { this.clearEmoteData(); this.loadEmoteData(EmoteInfo); }}; this.resetEmotes = this.resetEmotes.bind(this);
} }
async enabled() { async enabled() {
Settings.registerCollection("emotes", "Emotes", EmoteConfig, {title: Strings.Emotes.clearEmotes, onClick: () => {this.clearEmoteData(); this.loadEmoteData();}}); Settings.registerCollection("emotes", "Emotes", EmoteConfig, {title: Strings.Emotes.clearEmotes, onClick: this.resetEmotes});
// Disable emote module for now because it's annoying and slow
await this.getBlacklist(); await this.getBlacklist();
await this.loadEmoteData(); await this.loadEmoteData();
@ -94,9 +71,11 @@ export default new class EmoteModule extends Builtin {
this.patchMessageContent(); this.patchMessageContent();
Events.on("emotes-favorite-added", this.addFavorite); Events.on("emotes-favorite-added", this.addFavorite);
Events.on("emotes-favorite-removed", this.removeFavorite); Events.on("emotes-favorite-removed", this.removeFavorite);
Events.on("setting-updated", this.onCategoryToggle);
} }
disabled() { disabled() {
Events.off("setting-updated", this.onCategoryToggle);
Events.off("emotes-favorite-added", this.addFavorite); Events.off("emotes-favorite-added", this.addFavorite);
Events.off("emotes-favorite-removed", this.removeFavorite); Events.off("emotes-favorite-removed", this.removeFavorite);
Settings.removeCollection("emotes"); Settings.removeCollection("emotes");
@ -106,6 +85,12 @@ export default new class EmoteModule extends Builtin {
delete this.cancelEmoteRender; delete this.cancelEmoteRender;
} }
onCategoryToggle(collection, cat, category, enabled) {
if (collection != "emotes" || cat != "categories") return;
if (enabled) return this.loadEmoteData(category);
return this.unloadEmoteData(category);
}
addFavorite(name, url) { addFavorite(name, url) {
if (!this.favoriteEmotes.hasOwnProperty(name)) this.favoriteEmotes[name] = url; if (!this.favoriteEmotes.hasOwnProperty(name)) this.favoriteEmotes[name] = url;
this.saveFavorites(); this.saveFavorites();
@ -122,7 +107,7 @@ export default new class EmoteModule extends Builtin {
} }
saveFavorites() { saveFavorites() {
DataStore.setBDData("bdfavemotes", window.btoa(JSON.stringify(this.favoriteEmotes))); DataStore.setBDData("favoriteEmotes", this.favoriteEmotes);
} }
emptyEmotes() { emptyEmotes() {
@ -160,15 +145,17 @@ export default new class EmoteModule extends Builtin {
if (Emotes.TwitchGlobal[emoteName]) current = "TwitchGlobal"; if (Emotes.TwitchGlobal[emoteName]) current = "TwitchGlobal";
else if (Emotes.TwitchSubscriber[emoteName]) current = "TwitchSubscriber"; else if (Emotes.TwitchSubscriber[emoteName]) current = "TwitchSubscriber";
} }
else if (emoteOverride === "subscriber") {
if (Emotes.TwitchSubscriber[emoteName]) current = "TwitchSubscriber";
}
else if (emoteOverride === "bttv") { else if (emoteOverride === "bttv") {
if (Emotes.BTTV[emoteName]) current = "BTTV"; if (Emotes.BTTV[emoteName]) current = "BTTV";
else if (Emotes.BTTV2[emoteName]) current = "BTTV2";
} }
else if (emoteOverride === "ffz") { else if (emoteOverride === "ffz") {
if (Emotes.FrankerFaceZ[emoteName]) current = "FrankerFaceZ"; if (Emotes.FrankerFaceZ[emoteName]) current = "FrankerFaceZ";
} }
if (!Emotes[current][emoteName] || !Settings.get("emotes", "categories", bdEmoteSettingIDs[current])) continue; if (!Emotes[current][emoteName]) continue;
const results = nodes[n].match(new RegExp(`([\\s]|^)${Utilities.escape(emoteModifier ? emoteName + ":" + emoteModifier : emoteName)}([\\s]|$)`)); const results = nodes[n].match(new RegExp(`([\\s]|^)${Utilities.escape(emoteModifier ? emoteName + ":" + emoteModifier : emoteName)}([\\s]|$)`));
if (!results) continue; if (!results) continue;
const pre = nodes[n].substring(0, results.index + results[1].length); const pre = nodes[n].substring(0, results.index + results[1].length);
@ -198,7 +185,7 @@ export default new class EmoteModule extends Builtin {
} }
async getBlacklist() { async getBlacklist() {
const category = "blacklist"; const category = "Blacklist";
const exists = DataStore.emotesExist(category); const exists = DataStore.emotesExist(category);
const valid = await this.isCacheValid(category); const valid = await this.isCacheValid(category);
const useCache = (valid) || (!valid && exists && !this.shouldDownload); const useCache = (valid) || (!valid && exists && !this.shouldDownload);
@ -216,11 +203,13 @@ export default new class EmoteModule extends Builtin {
}); });
} }
async loadEmoteData() { async loadEmoteData(categories) {
if (!categories) categories = this.categories;
if (!Array.isArray(categories)) categories = [categories];
Toasts.show(Strings.Emotes.loading, {type: "info"}); Toasts.show(Strings.Emotes.loading, {type: "info"});
this.emotesLoaded = false; this.emotesLoaded = false;
for (const category of this.categories) { for (const category of categories) {
const exists = DataStore.emotesExist(category); const exists = DataStore.emotesExist(category);
const valid = await this.isCacheValid(category); const valid = await this.isCacheValid(category);
const useCache = (valid) || (!valid && exists && !this.shouldDownload); const useCache = (valid) || (!valid && exists && !this.shouldDownload);
@ -241,6 +230,15 @@ export default new class EmoteModule extends Builtin {
Toasts.show(Strings.Emotes.loaded, {type: "success"}); Toasts.show(Strings.Emotes.loaded, {type: "success"});
} }
unloadEmoteData(categories) {
if (!categories) categories = this.categories;
if (!Array.isArray(categories)) categories = [categories];
for (const category of categories) {
delete Emotes[category];
Emotes[category] = {};
}
}
downloadEmotes(category) { downloadEmotes(category) {
const url = this.getRemoteFile(category); const url = this.getRemoteFile(category);
this.log(`Downloading ${category} from ${url}`); this.log(`Downloading ${category} from ${url}`);
@ -267,14 +265,11 @@ export default new class EmoteModule extends Builtin {
}); });
} }
clearEmoteData() { resetEmotes() {
const _fs = require("fs"); const categories = Object.keys(Emotes);
const emoteFile = "emote_data.json"; this.unloadEmoteData(categories);
const file = Config.dataPath + emoteFile; for (const cat of categories) DataStore.invalidateCache("emotes", cat);
const exists = _fs.existsSync(file); this.loadEmoteData();
if (exists) _fs.unlinkSync(file);
DataStore.setBDData("emoteCacheDate", (new Date()).toJSON());
for (const category in Emotes) Object.assign(Emotes, {[category]: {}});
} }
}; };

View File

@ -21,7 +21,8 @@ export default [
collapsible: true, collapsible: true,
settings: [ settings: [
{type: "switch", id: "twitch", value: true}, {type: "switch", id: "twitch", value: true},
{type: "switch", id: "ffz", value: true}, {type: "switch", id: "twitchsubscriber", value: false},
{type: "switch", id: "frankerfacez", value: true},
{type: "switch", id: "bttv", value: true} {type: "switch", id: "bttv", value: true}
] ]
} }

View File

@ -120,7 +120,7 @@ export default {
name: "General", name: "General",
download: { download: {
name: "Download Emotes", name: "Download Emotes",
note: "Download emotes once a week to stay up to date" note: "Download emotes whenever they are out of date"
}, },
emoteMenu: { emoteMenu: {
name: "Emote Menu", name: "Emote Menu",
@ -151,7 +151,11 @@ export default {
name: "Categories", name: "Categories",
twitch: { twitch: {
name: "Twitch", name: "Twitch",
note: "Show Twitch global & subscriber emotes" note: "Show Twitch global emotes"
},
twitchsubscriber: {
name: "Twitch",
note: "Show Twitch subscriber emotes"
}, },
ffz: { ffz: {
name: "FrankerFaceZ", name: "FrankerFaceZ",

View File

@ -26,7 +26,6 @@ export default new class DataStore {
if (!fs.existsSync(this.localeFolder)) fs.mkdirSync(this.localeFolder); 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.BDFile)) fs.writeFileSync(this.BDFile, JSON.stringify(this.data.misc, null, 4));
if (!fs.existsSync(this.customCSS)) fs.writeFileSync(this.customCSS, ""); 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")); const dataFiles = fs.readdirSync(this.dataFolder).filter(f => !fs.statSync(path.resolve(this.dataFolder, f)).isDirectory() && f.endsWith(".json"));
for (const file of dataFiles) { for (const file of dataFiles) {
@ -41,7 +40,6 @@ export default new class DataStore {
get localeFolder() {return this._localeFolder || (this._localeFolder = path.resolve(this.baseFolder, `locales`));} 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 emoteFolder() {return this._emoteFolder || (this._emoteFolder = path.resolve(this.baseFolder, `emotes`));}
get cacheFile() {return this._cacheFile || (this._cacheFile = path.resolve(this.baseFolder, `.cache`));} get cacheFile() {return this._cacheFile || (this._cacheFile = path.resolve(this.baseFolder, `.cache`));}
get BDFile() {return this._BDFile || (this._BDFile = path.resolve(Config.dataPath, "data", `${releaseChannel}.json`));}
getPluginFile(pluginName) {return path.resolve(Config.dataPath, "plugins", pluginName + ".config.json");} getPluginFile(pluginName) {return path.resolve(Config.dataPath, "plugins", pluginName + ".config.json");}
@ -81,6 +79,12 @@ export default new class DataStore {
fs.writeFileSync(this.cacheFile, JSON.stringify(this.cacheData)); 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) { emotesExist(category) {
return fs.existsSync(path.resolve(this.emoteFolder, `${category}.json`)); return fs.existsSync(path.resolve(this.emoteFolder, `${category}.json`));
} }

View File

@ -11,13 +11,11 @@ export default new class SettingsManager {
this.state = {}; this.state = {};
this.collections = []; this.collections = [];
this.panels = []; this.panels = [];
this.registerCollection("settings", "Settings", SettingsConfig);
this.updateStrings = this.updateStrings.bind(this); this.updateStrings = this.updateStrings.bind(this);
} }
initialize() { initialize() {
this.loadSettings(); this.registerCollection("settings", "Settings", SettingsConfig);
this.updateStrings();
Events.on("strings-updated", this.updateStrings); Events.on("strings-updated", this.updateStrings);
// this.patchSections(); // this.patchSections();
} }
@ -31,7 +29,8 @@ export default new class SettingsManager {
settings: settings, settings: settings,
button: button button: button
}); });
this.setup(); this.setupCollection(id);
this.loadCollection(id);
this.updateStrings(); this.updateStrings();
} }
@ -64,69 +63,64 @@ export default new class SettingsManager {
return {collection, category, setting}; return {collection, category, setting};
} }
setup() { setupCollection(id) {
for (let c = 0; c < this.collections.length; c++) { const collection = this.collections.find(c => c.id == id);
const collection = this.collections[c]; if (!collection) return;
const categories = this.collections[c].settings; const categories = collection.settings;
if (!this.state[collection.id]) this.state[collection.id] = {}; if (!this.state[collection.id]) this.state[collection.id] = {};
for (let cc = 0; cc < categories.length; cc++) { for (let cc = 0; cc < categories.length; cc++) {
const category = categories[cc]; const category = categories[cc];
if (category.type != "category") {if (!this.state[collection.id].hasOwnProperty(category.id)) this.state[collection.id][category.id] = category.value;} if (category.type != "category") {if (!this.state[collection.id].hasOwnProperty(category.id)) this.state[collection.id][category.id] = category.value;}
else { else {
if (!this.state[collection.id].hasOwnProperty(category.id)) this.state[collection.id][category.id] = {}; if (!this.state[collection.id].hasOwnProperty(category.id)) this.state[collection.id][category.id] = {};
for (let s = 0; s < category.settings.length; s++) { for (let s = 0; s < category.settings.length; s++) {
const setting = category.settings[s]; const setting = category.settings[s];
if (!this.state[collection.id][category.id].hasOwnProperty(setting.id)) this.state[collection.id][category.id][setting.id] = setting.value; if (!this.state[collection.id][category.id].hasOwnProperty(setting.id)) this.state[collection.id][category.id][setting.id] = setting.value;
if (setting.enableWith) { if (setting.hasOwnProperty("disabled")) continue;
const path = this.getPath(setting.enableWith.split("."), collection.id, category.id); if (!setting.enableWith && !setting.disableWith) continue;
if (setting.hasOwnProperty("disabled")) continue; const pathString = setting.enableWith || setting.disableWith;
Object.defineProperty(setting, "disabled", { const path = this.getPath(pathString.split("."), collection.id, category.id);
get: () => { Object.defineProperty(setting, "disabled", {
return !this.state[path.collection][path.category][path.setting]; get: () => {
} const other = this.state[path.collection][path.category][path.setting];
}); return setting.enableWith ? !other : other;
} }
});
if (setting.disableWith) {
const path = this.getPath(setting.disableWith.split("."), collection.id, category.id);
if (setting.hasOwnProperty("disabled")) continue;
Object.defineProperty(setting, "disabled", {
get: () => {
return this.state[path.collection][path.category][path.setting];
}
});
}
}
} }
} }
} }
} }
saveSettings() { saveSettings() {
DataStore.setData("settings", this.state); for (const collection in this.state) this.saveCollection(collection);
} }
loadSettings() { loadSettings() {
const previousState = DataStore.getData("settings"); for (const collection in this.state) this.loadCollection(collection);
if (!previousState) return this.saveSettings(); }
for (const collection in this.state) {
if (!previousState[collection]) Object.assign(previousState, {[collection]: this.state[collection]}); saveCollection(collection) {
for (const category in this.state[collection]) { DataStore.setData(collection, this.state[collection]);
if (!previousState[collection][category]) Object.assign(previousState[collection], {[category]: this.state[collection][category]}); }
for (const setting in this.state[collection][category]) {
if (previousState[collection][category][setting] == undefined) continue; loadCollection(collection) {
this.state[collection][category][setting] = previousState[collection][category][setting]; const previousState = DataStore.getData(collection);
} if (!previousState) return this.saveCollection(collection);
for (const category in this.state[collection]) {
if (!previousState[category]) Object.assign(previousState, {[category]: this.state[collection][category]});
for (const setting in this.state[collection][category]) {
if (previousState[category][setting] == undefined) continue;
this.state[collection][category][setting] = previousState[category][setting];
} }
} }
this.saveSettings(); // in case new things were added this.saveCollection(collection); // in case new things were added
} }
onSettingChange(collection, category, id, value) { onSettingChange(collection, category, id, value) {
this.state[collection][category][id] = value; this.state[collection][category][id] = value;
Events.dispatch("setting-updated", collection, category, id, value); Events.dispatch("setting-updated", collection, category, id, value);
this.saveSettings(); this.saveCollection(collection);
} }
getSetting(collection, category, id) { getSetting(collection, category, id) {