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 {Config, EmoteConfig} from "data";
import {EmoteConfig} from "data";
import {Utilities, WebpackModules, DataStore, DiscordModules, Events, Settings, Strings} from "modules";
import BDEmote from "../ui/emote";
import Toasts from "../ui/toasts";
import FormattableString from "../structs/string";
const request = require("request");
// const fs = require("fs");
const EmoteURLs = {
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`),
FrankerFaceZ: new FormattableString(`https://cdn.frankerfacez.com/emoticon/{{id}}/1`),
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 = {
TwitchGlobal: {},
TwitchSubscriber: {},
BTTV: {},
FrankerFaceZ: {},
BTTV2: {}
};
const bdEmoteSettingIDs = {
TwitchGlobal: "twitch",
TwitchSubscriber: "twitch",
BTTV: "bttv",
FrankerFaceZ: "ffz",
BTTV2: "bttv"
FrankerFaceZ: {}
};
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"];
export default new class EmoteModule extends Builtin {
@ -49,10 +30,10 @@ export default new class EmoteModule extends Builtin {
get collection() {return "settings";}
get category() {return "general";}
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");}
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);}
@ -63,7 +44,6 @@ export default new class EmoteModule extends Builtin {
get TwitchSubscriber() {return Emotes.TwitchSubscriber;}
get BTTV() {return Emotes.BTTV;}
get FrankerFaceZ() {return Emotes.FrankerFaceZ;}
get BTTV2() {return Emotes.BTTV2;}
get blacklist() {return blacklist;}
get favorites() {return this.favoriteEmotes;}
getUrl(category, name) {return EmoteURLs[category].format({id: Emotes[category][name]});}
@ -74,19 +54,16 @@ export default new class EmoteModule extends Builtin {
initialize() {
super.initialize();
window.emoteModule = this;
this.favoriteEmotes = {};
const fe = DataStore.getBDData("bdfavemotes");
if (fe !== "" && fe !== null) this.favoriteEmotes = JSON.parse(window.atob(fe));
this.saveFavorites();
const storedFavorites = DataStore.getBDData("favoriteEmotes");
this.favoriteEmotes = storedFavorites || {};
this.addFavorite = this.addFavorite.bind(this);
this.removeFavorite = this.removeFavorite.bind(this);
// EmoteConfig;
// emoteCollection.button = {title: "Clear Emote Cache", onClick: () => { this.clearEmoteData(); this.loadEmoteData(EmoteInfo); }};
this.onCategoryToggle = this.onCategoryToggle.bind(this);
this.resetEmotes = this.resetEmotes.bind(this);
}
async enabled() {
Settings.registerCollection("emotes", "Emotes", EmoteConfig, {title: Strings.Emotes.clearEmotes, onClick: () => {this.clearEmoteData(); this.loadEmoteData();}});
// Disable emote module for now because it's annoying and slow
Settings.registerCollection("emotes", "Emotes", EmoteConfig, {title: Strings.Emotes.clearEmotes, onClick: this.resetEmotes});
await this.getBlacklist();
await this.loadEmoteData();
@ -94,9 +71,11 @@ export default new class EmoteModule extends Builtin {
this.patchMessageContent();
Events.on("emotes-favorite-added", this.addFavorite);
Events.on("emotes-favorite-removed", this.removeFavorite);
Events.on("setting-updated", this.onCategoryToggle);
}
disabled() {
Events.off("setting-updated", this.onCategoryToggle);
Events.off("emotes-favorite-added", this.addFavorite);
Events.off("emotes-favorite-removed", this.removeFavorite);
Settings.removeCollection("emotes");
@ -106,6 +85,12 @@ export default new class EmoteModule extends Builtin {
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) {
if (!this.favoriteEmotes.hasOwnProperty(name)) this.favoriteEmotes[name] = url;
this.saveFavorites();
@ -122,7 +107,7 @@ export default new class EmoteModule extends Builtin {
}
saveFavorites() {
DataStore.setBDData("bdfavemotes", window.btoa(JSON.stringify(this.favoriteEmotes)));
DataStore.setBDData("favoriteEmotes", this.favoriteEmotes);
}
emptyEmotes() {
@ -160,15 +145,17 @@ export default new class EmoteModule extends Builtin {
if (Emotes.TwitchGlobal[emoteName]) current = "TwitchGlobal";
else if (Emotes.TwitchSubscriber[emoteName]) current = "TwitchSubscriber";
}
else if (emoteOverride === "subscriber") {
if (Emotes.TwitchSubscriber[emoteName]) current = "TwitchSubscriber";
}
else if (emoteOverride === "bttv") {
if (Emotes.BTTV[emoteName]) current = "BTTV";
else if (Emotes.BTTV2[emoteName]) current = "BTTV2";
}
else if (emoteOverride === "ffz") {
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]|$)`));
if (!results) continue;
const pre = nodes[n].substring(0, results.index + results[1].length);
@ -198,7 +185,7 @@ export default new class EmoteModule extends Builtin {
}
async getBlacklist() {
const category = "blacklist";
const category = "Blacklist";
const exists = DataStore.emotesExist(category);
const valid = await this.isCacheValid(category);
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"});
this.emotesLoaded = false;
for (const category of this.categories) {
for (const category of categories) {
const exists = DataStore.emotesExist(category);
const valid = await this.isCacheValid(category);
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"});
}
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) {
const url = this.getRemoteFile(category);
this.log(`Downloading ${category} from ${url}`);
@ -267,14 +265,11 @@ export default new class EmoteModule extends Builtin {
});
}
clearEmoteData() {
const _fs = require("fs");
const emoteFile = "emote_data.json";
const file = Config.dataPath + emoteFile;
const exists = _fs.existsSync(file);
if (exists) _fs.unlinkSync(file);
DataStore.setBDData("emoteCacheDate", (new Date()).toJSON());
for (const category in Emotes) Object.assign(Emotes, {[category]: {}});
resetEmotes() {
const categories = Object.keys(Emotes);
this.unloadEmoteData(categories);
for (const cat of categories) DataStore.invalidateCache("emotes", cat);
this.loadEmoteData();
}
};

View File

@ -21,7 +21,8 @@ export default [
collapsible: true,
settings: [
{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}
]
}

View File

@ -120,7 +120,7 @@ export default {
name: "General",
download: {
name: "Download Emotes",
note: "Download emotes once a week to stay up to date"
note: "Download emotes whenever they are out of date"
},
emoteMenu: {
name: "Emote Menu",
@ -151,7 +151,11 @@ export default {
name: "Categories",
twitch: {
name: "Twitch",
note: "Show Twitch global & subscriber emotes"
note: "Show Twitch global emotes"
},
twitchsubscriber: {
name: "Twitch",
note: "Show Twitch subscriber emotes"
},
ffz: {
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.emoteFolder)) fs.mkdirSync(this.emoteFolder);
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, "");
const dataFiles = fs.readdirSync(this.dataFolder).filter(f => !fs.statSync(path.resolve(this.dataFolder, f)).isDirectory() && f.endsWith(".json"));
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 emoteFolder() {return this._emoteFolder || (this._emoteFolder = path.resolve(this.baseFolder, `emotes`));}
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");}
@ -81,6 +79,12 @@ export default new class DataStore {
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`));
}

View File

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