armcord/src/utils.ts

99 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-01-15 21:21:51 +00:00
import * as storage from "electron-json-storage";
2022-01-16 18:07:00 +00:00
import * as fs from "fs";
import { app } from "electron";
2022-01-30 19:48:32 +00:00
import path from "path";
export var firstRun: boolean;
2022-01-16 18:07:00 +00:00
2021-12-24 21:56:49 +00:00
//utillity functions that are used all over the codebase or just too obscure to be put in the file used in
export function addStyle(styleString: string) {
2022-01-15 21:21:51 +00:00
const style = document.createElement("style");
style.textContent = styleString;
document.head.append(style);
}
2021-12-24 21:56:49 +00:00
export function addScript(scriptString: string) {
var script = document.createElement("script");
script.textContent = scriptString;
document.body.append(script);
}
2022-01-15 21:21:51 +00:00
export function setup() {
2022-02-26 21:26:16 +00:00
console.log("Setting up temporary ArmCord settings.");
2022-01-15 21:21:51 +00:00
storage.set(
"settings",
{
2022-02-26 21:26:16 +00:00
windowStyle: "default",
2022-01-15 21:21:51 +00:00
channel: "stable",
2022-01-30 19:48:32 +00:00
doneSetup: true,
2022-01-15 21:21:51 +00:00
armcordCSP: true,
2022-02-26 21:26:16 +00:00
minimizeToTray: true,
automaticPatches: false,
2022-01-30 19:48:32 +00:00
mods: "cumcord",
2022-02-26 21:26:16 +00:00
blurType: "acrylic",
2022-01-15 21:21:51 +00:00
},
function (error) {
if (error) throw error;
}
);
}
2022-01-15 21:21:51 +00:00
export async function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
2022-02-26 21:26:16 +00:00
export async function checkIfConfigIsNew() {
if (await getConfigUnsafe("automaticPatches") == undefined) {
firstRun = true;
}
}
2022-01-15 21:21:51 +00:00
export function saveSettings(
2022-02-26 21:26:16 +00:00
windowStyle: string,
2022-01-15 21:21:51 +00:00
channelSetting: string,
armcordCSPSetting: boolean,
2022-02-26 21:26:16 +00:00
minimizeToTray: boolean,
automaticPatches: boolean,
modsSetting: string,
blurType: string
2022-01-15 21:21:51 +00:00
) {
console.log("Setting up ArmCord settings.");
2022-01-15 21:21:51 +00:00
storage.set(
"settings",
{
2022-02-26 21:26:16 +00:00
windowStyle: windowStyle,
2022-01-15 21:21:51 +00:00
channel: channelSetting,
2022-01-30 19:48:32 +00:00
doneSetup: true,
2022-01-15 21:21:51 +00:00
armcordCSP: armcordCSPSetting,
2022-02-26 21:26:16 +00:00
minimizeToTray: minimizeToTray,
automaticPatches: automaticPatches,
2022-01-15 21:21:51 +00:00
mods: modsSetting,
2022-02-26 21:26:16 +00:00
blurType: blurType
2022-01-15 21:21:51 +00:00
},
function (error) {
if (error) throw error;
2022-01-15 21:21:51 +00:00
}
);
}
2022-01-16 18:07:00 +00:00
export async function getConfigUnsafe(object: string) {
2022-01-30 19:48:32 +00:00
try {
const userDataPath = app.getPath("userData");
const storagePath = path.join(userDataPath, "/storage/");
let rawdata = fs.readFileSync(storagePath + "settings.json", "utf-8");
let returndata = JSON.parse(rawdata);
console.log(returndata[object]);
return returndata[object];
} catch (e) {
console.log("Config probably doesn't exist yet. Returning setup value.");
firstRun = true;
return "setup";
}
2022-01-16 18:07:00 +00:00
}
2022-01-15 21:21:51 +00:00
export function getVersion() {
2022-01-16 18:07:00 +00:00
//to-do better way of doing this
2022-01-30 19:48:32 +00:00
return "3.1.0";
}
2022-02-26 21:26:16 +00:00
export async function injectJS(inject: string) {
const js = await (await fetch(`${inject}`)).text();
const el = document.createElement("script");
el.appendChild(document.createTextNode(js));
document.body.appendChild(el);
}