2022-01-16 18:07:00 +00:00
import * as fs from "fs" ;
2022-04-18 10:05:06 +00:00
import { app , dialog } from "electron" ;
2022-01-30 19:48:32 +00:00
import path from "path" ;
export var firstRun : boolean ;
2022-04-18 11:03:26 +00:00
export var contentPath : string ;
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-03-04 17:53:18 +00:00
const style = document . createElement ( "style" ) ;
style . textContent = styleString ;
document . head . append ( style ) ;
2022-01-15 21:21:51 +00:00
}
2021-12-24 21:56:49 +00:00
export function addScript ( scriptString : string ) {
2022-03-04 17:53:18 +00:00
var script = document . createElement ( "script" ) ;
script . textContent = scriptString ;
document . body . append ( script ) ;
2021-12-24 21:56:49 +00:00
}
2022-03-04 16:30:23 +00:00
export async function sleep ( ms : number ) {
2022-03-04 17:53:18 +00:00
return new Promise ( ( resolve ) = > setTimeout ( resolve , ms ) ) ;
2022-03-04 16:30:23 +00:00
}
2022-04-18 10:05:06 +00:00
export async function checkIfConfigIsBroken() {
2022-04-21 13:23:06 +00:00
if ( await getConfig ( "0" ) == "d" ) {
2022-04-18 10:05:06 +00:00
console . log ( "Detected a corrupted config" ) ;
setup ( ) ;
dialog . showErrorBox (
"Oops, something went wrong." ,
"ArmCord has detected that your configuration file is corrupted, please restart the app and set your settings again. If this issue persists, report it on the support server/Github issues."
) ;
2022-03-04 17:53:18 +00:00
}
2022-03-04 16:30:23 +00:00
}
2022-04-18 10:20:38 +00:00
2022-01-15 21:21:51 +00:00
export function setup() {
2022-03-04 17:53:18 +00:00
console . log ( "Setting up temporary ArmCord settings." ) ;
const defaults : Settings = {
windowStyle : "default" ,
channel : "stable" ,
armcordCSP : true ,
minimizeToTray : true ,
automaticPatches : false ,
mods : "cumcord" ,
2022-04-18 10:20:38 +00:00
blurType : "acrylic" ,
2022-04-19 17:59:52 +00:00
inviteWebsocket : true ,
2022-04-18 10:20:38 +00:00
doneSetup : false
2022-03-04 17:53:18 +00:00
} ;
2022-04-18 11:03:26 +00:00
setConfigBulk ( {
. . . defaults
} ) ;
2021-12-26 21:41:09 +00:00
}
2022-04-18 10:25:10 +00:00
2022-01-15 21:21:51 +00:00
export function getVersion() {
2022-03-04 17:53:18 +00:00
//to-do better way of doing this
return "3.1.0" ;
2022-01-15 18:31:51 +00:00
}
2022-02-26 21:26:16 +00:00
export async function injectJS ( inject : string ) {
2022-03-04 17:53:18 +00:00
const js = await ( await fetch ( ` ${ inject } ` ) ) . text ( ) ;
2022-02-26 21:26:16 +00:00
2022-03-04 17:53:18 +00:00
const el = document . createElement ( "script" ) ;
2022-02-26 21:26:16 +00:00
2022-03-04 17:53:18 +00:00
el . appendChild ( document . createTextNode ( js ) ) ;
2022-02-26 21:26:16 +00:00
2022-03-04 17:53:18 +00:00
document . body . appendChild ( el ) ;
}
2022-04-18 10:20:38 +00:00
//ArmCord Settings/Storage manager
2022-04-18 11:03:26 +00:00
2022-04-18 10:20:38 +00:00
export interface Settings {
windowStyle : string ;
channel : string ;
armcordCSP : boolean ;
minimizeToTray : boolean ;
automaticPatches : boolean ;
mods : string ;
blurType : string ;
2022-04-19 17:59:52 +00:00
inviteWebsocket : boolean ;
2022-04-18 10:20:38 +00:00
doneSetup : boolean ;
}
export async function getConfig ( object : string ) {
try {
const userDataPath = app . getPath ( "userData" ) ;
const storagePath = path . join ( userDataPath , "/storage/" ) ;
2022-04-18 11:03:26 +00:00
const settingsFile = storagePath + "settings.json" ;
let rawdata = fs . readFileSync ( settingsFile , "utf-8" ) ;
2022-04-18 10:20:38 +00:00
let returndata = JSON . parse ( rawdata ) ;
2022-04-20 18:30:52 +00:00
console . log ( object + ": " + returndata [ object ] ) ;
2022-04-18 10:20:38 +00:00
return returndata [ object ] ;
} catch ( e ) {
console . log ( "Config probably doesn't exist yet. Returning setup value." ) ;
firstRun = true ;
return "setup" ;
}
}
export async function setConfig ( object : string , toSet : any ) {
try {
const userDataPath = app . getPath ( "userData" ) ;
const storagePath = path . join ( userDataPath , "/storage/" ) ;
2022-04-18 11:03:26 +00:00
const settingsFile = storagePath + "settings.json" ;
let rawdata = fs . readFileSync ( settingsFile , "utf-8" ) ;
2022-04-18 10:20:38 +00:00
let parsed = JSON . parse ( rawdata ) ;
parsed [ object ] = toSet ;
2022-04-18 11:03:26 +00:00
let toSave = JSON . stringify ( parsed ) ;
fs . writeFileSync ( settingsFile , toSave , "utf-8" ) ;
2022-04-18 10:20:38 +00:00
} catch ( e ) {
console . log ( "Config probably doesn't exist yet. Returning setup value." ) ;
firstRun = true ;
return "setup" ;
}
}
export async function setConfigBulk ( object : Settings ) {
try {
const userDataPath = app . getPath ( "userData" ) ;
const storagePath = path . join ( userDataPath , "/storage/" ) ;
2022-04-18 11:03:26 +00:00
const settingsFile = storagePath + "settings.json" ;
let toSave = JSON . stringify ( object ) ;
fs . writeFileSync ( settingsFile , toSave , "utf-8" ) ;
2022-04-18 10:20:38 +00:00
} catch ( e ) {
console . log ( "Config probably doesn't exist yet. Returning setup value." ) ;
firstRun = true ;
return "setup" ;
}
2022-04-18 11:03:26 +00:00
}
export async function checkIfConfigExists() {
const userDataPath = app . getPath ( "userData" ) ;
const storagePath = path . join ( userDataPath , "/storage/" ) ;
const settingsFile = storagePath + "settings.json" ;
2022-04-20 18:30:52 +00:00
2022-04-18 11:03:26 +00:00
if ( ! fs . existsSync ( settingsFile ) ) {
2022-04-20 18:30:52 +00:00
if ( ! fs . existsSync ( storagePath ) ) {
fs . mkdirSync ( storagePath ) ;
console . log ( "Created missing storage folder" ) ;
}
2022-04-18 11:03:26 +00:00
console . log ( "First run of the ArmCord. Starting setup." ) ;
setup ( ) ;
contentPath = path . join ( __dirname , "/content/setup.html" ) ;
if ( ! contentPath . includes ( "ts-out" ) ) {
contentPath = path . join ( __dirname , "/ts-out/content/setup.html" ) ;
}
} else {
console . log ( "ArmCord has been run before. Skipping setup." ) ;
contentPath = path . join ( __dirname , "/content/splash.html" ) ;
if ( ! contentPath . includes ( "ts-out" ) ) {
contentPath = path . join ( __dirname , "/ts-out/content/splash.html" ) ;
}
}
}