2022-01-16 18:07:00 +00:00
import * as fs from "fs" ;
2022-06-16 15:24:37 +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 ;
2022-07-11 17:13:52 +00:00
//utility functions that are used all over the codebase or just too obscure to be put in the file used in
2021-12-24 21:56:49 +00:00
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-05-14 19:02:09 +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 ,
2022-07-14 18:37:03 +00:00
alternativePaste : false ,
2022-03-04 17:53:18 +00:00
mods : "cumcord" ,
2022-06-10 18:24:13 +00:00
performanceMode : "none" ,
2022-04-19 17:59:52 +00:00
inviteWebsocket : true ,
2022-07-11 17:13:52 +00:00
mobileMode : false ,
2022-07-04 14:39:22 +00:00
trayIcon : "ac_plug_colored" ,
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-07-14 21:34:16 +00:00
//I'm too lazy to replace every mf reference so :p
2022-01-15 21:21:51 +00:00
export function getVersion() {
2022-07-14 21:34:16 +00:00
//Checks if the version # has 4 sections (3.1.0.0) instead of 3 (3.1.0) / Shitty way to check if Kernel Mod is installed
2022-07-14 21:47:30 +00:00
if ( ( ( app . getVersion ( ) ) . split ( '.' ) . length > 3 ) == true ) {
2022-07-14 21:34:16 +00:00
return app . getVersion ( ) . split ( '.' ) [ 0 ] + "." + app . getVersion ( ) . split ( '.' ) [ 1 ] + "." + app . getVersion ( ) . split ( '.' ) [ 2 ] + " [Kernel Mod]" ;
} else {
2022-07-04 14:39:22 +00:00
return app . getVersion ( ) ;
2022-07-14 21:34:16 +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-06-10 18:24:13 +00:00
export async function injectElectronFlags() {
// MIT License
2022-04-18 10:20:38 +00:00
2022-06-10 18:24:13 +00:00
// Copyright (c) 2022 GooseNest
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
const presets = {
2022-06-11 20:14:16 +00:00
performance : ` --enable-gpu-rasterization --enable-zero-copy --ignore-gpu-blocklist --enable-hardware-overlays=single-fullscreen,single-on-top,underlay --enable-features=EnableDrDc,CanvasOopRasterization,BackForwardCache:TimeToLiveInBackForwardCacheInSeconds/300/should_ignore_blocklists/true/enable_same_site/true,ThrottleDisplayNoneAndVisibilityHiddenCrossOriginIframes,UseSkiaRenderer,WebAssemblyLazyCompilation --disable-features=Vulkan --force_high_performance_gpu ` , // Performance
battery : "--enable-features=TurnOffStreamingMediaCachingOnBattery --force_low_power_gpu" // Known to have better battery life for Chromium?
2022-06-10 18:24:13 +00:00
} ;
switch ( await getConfig ( "performanceMode" ) ) {
case "performance" :
console . log ( "Performance mode enabled" ) ;
app . commandLine . appendSwitch ( presets . performance ) ;
break ;
case "battery" :
console . log ( "Battery mode enabled" ) ;
app . commandLine . appendSwitch ( presets . battery ) ;
break ;
default :
console . log ( "No performance modes set" ) ;
}
}
2022-06-11 20:14:16 +00:00
export async function setLang ( language : string ) {
2022-07-11 18:19:50 +00:00
const langConfigFile = path . join ( app . getPath ( "userData" ) , "/storage/" ) + "lang.json" ;
2022-06-11 20:14:16 +00:00
if ( ! fs . existsSync ( langConfigFile ) ) {
fs . writeFileSync ( langConfigFile , "{}" , "utf-8" ) ;
}
let rawdata = fs . readFileSync ( langConfigFile , "utf-8" ) ;
let parsed = JSON . parse ( rawdata ) ;
parsed [ "lang" ] = language ;
let toSave = JSON . stringify ( parsed ) ;
fs . writeFileSync ( langConfigFile , toSave , "utf-8" ) ;
}
2022-06-12 11:18:18 +00:00
var language : string ;
export async function getLang ( object : string ) {
if ( language == undefined ) {
2022-06-17 07:37:18 +00:00
try {
const userDataPath = app . getPath ( "userData" ) ;
const storagePath = path . join ( userDataPath , "/storage/" ) ;
const langConfigFile = storagePath + "lang.json" ;
let rawdata = fs . readFileSync ( langConfigFile , "utf-8" ) ;
let parsed = JSON . parse ( rawdata ) ;
language = parsed [ "lang" ] ;
} catch ( e ) {
console . log ( "Language config file doesn't exist. Fallback to English." )
language = "en-US"
}
2022-06-12 11:18:18 +00:00
}
if ( language . length == 2 ) {
language = language + "-" + language . toUpperCase ( ) ;
}
2022-06-12 11:32:46 +00:00
var langPath = path . join ( __dirname , "../" , "/assets/lang/" + language + ".json" ) ;
if ( ! fs . existsSync ( langPath ) ) {
langPath = path . join ( __dirname , "../" , "/assets/lang/en-US.json" ) ;
}
2022-06-12 11:18:18 +00:00
let rawdata = fs . readFileSync ( langPath , "utf-8" ) ;
let parsed = JSON . parse ( rawdata ) ;
2022-07-11 17:13:52 +00:00
if ( parsed [ object ] == undefined ) {
console . log ( object + " is undefined in " + language )
langPath = path . join ( __dirname , "../" , "/assets/lang/en-US.json" ) ;
rawdata = fs . readFileSync ( langPath , "utf-8" ) ;
parsed = JSON . parse ( rawdata ) ;
return parsed [ object ]
} else {
return parsed [ object ] ;
}
2022-06-12 11:18:18 +00:00
}
2022-06-16 15:24:37 +00:00
//ArmCord Window State manager
export interface WindowState {
width : number ;
height : number ;
isMaximized : boolean ;
}
export async function setWindowState ( object : WindowState ) {
const userDataPath = app . getPath ( "userData" ) ;
const storagePath = path . join ( userDataPath , "/storage/" ) ;
const saveFile = storagePath + "window.json" ;
if ( ! fs . existsSync ( saveFile ) ) {
fs . writeFileSync ( saveFile , "{}" , "utf-8" ) ;
}
let toSave = JSON . stringify ( object ) ;
fs . writeFileSync ( saveFile , toSave , "utf-8" ) ;
}
export async function getWindowState ( object : string ) {
const userDataPath = app . getPath ( "userData" ) ;
const storagePath = path . join ( userDataPath , "/storage/" ) ;
const settingsFile = storagePath + "window.json" ;
let rawdata = fs . readFileSync ( settingsFile , "utf-8" ) ;
let returndata = JSON . parse ( rawdata ) ;
2022-07-11 18:19:50 +00:00
console . log ( "[Window state manager] " + object + ": " + returndata [ object ] ) ;
2022-06-16 15:24:37 +00:00
return returndata [ object ] ;
}
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 ;
2022-07-14 18:37:03 +00:00
alternativePaste : boolean ;
2022-04-18 10:20:38 +00:00
mods : string ;
2022-07-11 17:13:52 +00:00
mobileMode : boolean ,
2022-06-11 20:14:16 +00:00
performanceMode : string ;
2022-04-19 17:59:52 +00:00
inviteWebsocket : boolean ;
2022-07-04 14:39:22 +00:00
trayIcon : string ;
2022-04-18 10:20:38 +00:00
doneSetup : boolean ;
}
2022-07-11 18:19:50 +00:00
export function getConfigLocation() {
const userDataPath = app . getPath ( "userData" ) ;
const storagePath = path . join ( userDataPath , "/storage/" ) ;
return storagePath + "settings.json" ;
2022-04-18 10:20:38 +00:00
}
2022-07-11 18:19:50 +00:00
export async function getConfig ( object : string ) {
let rawdata = fs . readFileSync ( getConfigLocation ( ) , "utf-8" ) ;
let returndata = JSON . parse ( rawdata ) ;
console . log ( "[Config manager] " + object + ": " + returndata [ object ] ) ;
return returndata [ object ] ;
2022-07-04 14:39:22 +00:00
}
2022-04-18 10:20:38 +00:00
export async function setConfig ( object : string , toSet : any ) {
2022-07-11 18:19:50 +00:00
let rawdata = fs . readFileSync ( getConfigLocation ( ) , "utf-8" ) ;
let parsed = JSON . parse ( rawdata ) ;
parsed [ object ] = toSet ;
let toSave = JSON . stringify ( parsed ) ;
fs . writeFileSync ( getConfigLocation ( ) , toSave , "utf-8" ) ;
2022-04-18 10:20:38 +00:00
}
export async function setConfigBulk ( object : Settings ) {
2022-07-11 18:19:50 +00:00
const userDataPath = app . getPath ( "userData" ) ;
const storagePath = path . join ( userDataPath , "/storage/" ) ;
const settingsFile = storagePath + "settings.json" ;
let toSave = JSON . stringify ( object ) ;
fs . writeFileSync ( settingsFile , toSave , "utf-8" ) ;
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-05-14 19:02:09 +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 ( ) ;
2022-07-11 18:19:50 +00:00
firstRun = true ;
2022-04-18 11:03:26 +00:00
} else {
2022-05-22 11:52:26 +00:00
if ( ( await getConfig ( "doneSetup" ) ) == false ) {
2022-05-14 19:02:09 +00:00
console . log ( "First run of the ArmCord. Starting setup." ) ;
setup ( ) ;
2022-07-11 18:19:50 +00:00
firstRun = true ;
2022-05-14 19:02:09 +00:00
} else {
console . log ( "ArmCord has been run before. Skipping setup." ) ;
2022-04-18 11:03:26 +00:00
}
}
}