Skip to content
This repository has been archived by the owner on Dec 14, 2024. It is now read-only.

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Jan 22, 2024
2 parents 1315579 + 56a680b commit f3bae89
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 26 deletions.
8 changes: 5 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@
<label for="clear-cache-on-close">Clear Cache on close</label>
<input id="clear-cache-on-close" class="no-text" type="checkbox">
</div>
<div class="insecure-ssl-field field">
<label for="clear-cache-on-close">Ignore Certificate errors</label>
<input class="no-text" id="insecure-ssl" type="checkbox">
</div>
<div class="button-group">
<button id="clear-cache">Clear Cache</button>
<button id="save-app-config">Save</button>
Expand All @@ -109,9 +113,7 @@ <h3>Put this in your <code>config.json</code> file to include it installation-wi
<button onclick="copyText()">Copy</button>
<button onclick="toggleExportConfig()">Close</button>
<script>
const gamesList = JSON.parse(window.localStorage.getItem("gameList") || "[]");
const appConfig = JSON.parse(window.localStorage.getItem("appConfig") || "{}");
const config = {...appConfig, games: gamesList};
const config = await window.api.localAppConfig();
code = document.getElementById("export-text");
const text = JSON.stringify(config, null, 4);
txt = document.createTextNode(text);
Expand Down
18 changes: 16 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const windowsData = {} as WindowsData;
let partitionId: number = 0;

function getSession(): Electron.Session {
if (!getAppConfig().experimentalMultiInstance) return session.defaultSession;
const partitionIdTemp = partitionId;
partitionId++
if (partitionIdTemp == 0)
Expand Down Expand Up @@ -229,17 +228,32 @@ ipcMain.handle("app-version", () => app.getVersion())
function getAppConfig(): AppConfig {
try {
const json = fs.readFileSync(path.join(app.getAppPath(), "config.json")).toString();
const appConfig = JSON.parse(json) as AppConfig;
let appConfig = JSON.parse(json) as AppConfig;
if (appConfig.ignoreCertificateErrors) {
app.commandLine.appendSwitch("ignore-certificate-errors");
}
const userData = getUserData();
appConfig = {...appConfig, ...userData.app};
return appConfig;
} catch (e) {
return {} as AppConfig;
}
}

ipcMain.on("save-app-config", (_e, data: AppConfig) => {
const currentData = getUserData();
currentData.app = {...currentData.app, ...data};
fs.writeFileSync(path.join(app.getPath("userData"), "userData.json"), JSON.stringify(currentData));
});
ipcMain.handle("app-config", getAppConfig);
ipcMain.handle("local-app-config", () => {
try {
const userData = getUserData();
return userData.app ?? {} as AppConfig;
} catch (e) {
return {} as AppConfig;
}
});

ipcMain.handle("select-path", (e) => {
windowsData[e.sender.id].autoLogin = true;
Expand Down
8 changes: 8 additions & 0 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ export type ContextBridgeApi = {
userData: (gameId: string | number) => Promise<GameUserDataDecrypted>;
appVersion: () => Promise<string>;
appConfig: () => Promise<AppConfig>;
localAppConfig: () => Promise<AppConfig>;
cachePath: () => Promise<string>;
setCachePath: (cachePath: string) => void;
returnToServerSelect: () => void;
saveUserData: (data: SaveUserData) => void;
openGame: (id: number | string) => void;
clearCache: () => void;
saveAppConfig: (data: AppConfig) => void;
}
const exposedApi: ContextBridgeApi = {
// request(channel: RequestChannels, ...args: unknown[]): Promise<unknown> {
Expand All @@ -54,6 +56,9 @@ const exposedApi: ContextBridgeApi = {
appConfig() {
return ipcRenderer.invoke("app-config") as Promise<AppConfig>;
},
localAppConfig() {
return ipcRenderer.invoke("local-app-config") as Promise<AppConfig>;
},
appVersion() {
return ipcRenderer.invoke("app-version") as Promise<string>;
},
Expand All @@ -75,6 +80,9 @@ const exposedApi: ContextBridgeApi = {
setCachePath(cachePath: string) {
ipcRenderer.send("cache-path", cachePath);
},
saveAppConfig(data: AppConfig) {
ipcRenderer.send("save-app-config", data);
}


}
Expand Down
68 changes: 48 additions & 20 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,26 @@ function compareSemver(a: string, b: string): number {
return 0
}

document.querySelector("#add-game").addEventListener("click", () => {
async function updateGameList(task: (appConfig: AppConfig) => void) {
const appConfig = await window.api.localAppConfig();
task(appConfig);
window.api.saveAppConfig(appConfig);
}


document.querySelector("#add-game").addEventListener("click", async () => {
const gameUrlField = document.querySelector("#game-url") as HTMLInputElement;
const gameNameField = document.querySelector("#game-name") as HTMLInputElement;
const gameUrl = gameUrlField.value;
const gameName = gameNameField.value;
if (!gameUrl || !gameName) return alert("Please enter a game name and url");
const gameList = window.localStorage.getItem("gameList") || "[]";
const gameListJson: GameConfig[] = JSON.parse(gameList);
const newGameItem = {name: gameName, url: gameUrl, id: Math.round(Math.random() * 1000000)} as GameConfig;
gameListJson.push(newGameItem);
window.localStorage.setItem("gameList", JSON.stringify(gameListJson));
await updateGameList((appConfig) => {
appConfig.games.push(newGameItem);
});
gameUrlField.value = "";
gameNameField.value = "";
createGameItem(newGameItem);
await createGameItem(newGameItem);
});


Expand All @@ -54,9 +60,18 @@ document.querySelector("#save-app-config").addEventListener("click", (e) => {
const textColor = (closeUserConfig.querySelector("#text-color") as HTMLInputElement).value;
const cachePath = (closeUserConfig.querySelector("#cache-path") as HTMLInputElement).value;
const autoCacheClear = (closeUserConfig.querySelector("#cache-path") as HTMLInputElement).checked;
const config = {accentColor, backgroundColor, background, textColor, cachePath, autoCacheClear} as AppConfig;
const ignoreCertificateErrors = (closeUserConfig.querySelector("#insecure-ssl") as HTMLInputElement).checked;
const config = {
accentColor,
backgroundColor,
background,
textColor,
cachePath,
autoCacheClear,
ignoreCertificateErrors
} as AppConfig;
console.log(config);
window.localStorage.setItem("appConfig", JSON.stringify(config));
window.api.saveAppConfig(config);
applyAppConfig(config);
});

Expand All @@ -81,12 +96,11 @@ async function createGameItem(game: GameConfig) {
gameItemList.appendChild(li);
const userConfiguration = li.querySelector("div.user-configuration") as HTMLDivElement;
userConfiguration.style.height = `${userConfiguration.scrollHeight}px`;
userConfiguration.querySelector("#delete-game")?.addEventListener("click", () => {
const gameList = window.localStorage.getItem("gameList") || "[]";
const gameListJson: GameConfig[] = JSON.parse(gameList);
const newGameList = gameListJson.filter((g) => g.id !== game.id);
window.localStorage.setItem("gameList", JSON.stringify(newGameList));
createGameList();
userConfiguration.querySelector("#delete-game")?.addEventListener("click", async () => {
await updateGameList((appConfig) => {
appConfig.games = appConfig.games.filter((g) => g.id !== game.id);
});
await createGameList();
});
const gameId = game.id ?? game.name;
const saveButton = userConfiguration.querySelector("#save-user-data") as HTMLButtonElement;
Expand Down Expand Up @@ -140,14 +154,31 @@ function addStyle(styleString: string) {
document.head.append(style);
}

async function migrateConfig() {
let localAppConfig = await window.api.localAppConfig();
const gameList: GameConfig[] = JSON.parse(window.localStorage.getItem("gameList") || "[]");
if (gameList.length > 0) {
localAppConfig.games = localAppConfig?.games ?? [];
localAppConfig.games.push(...gameList);
window.localStorage.removeItem("gameList");
}
const oldConfigJson = window.localStorage.getItem("appConfig") || "{}";
if (oldConfigJson !== "{}") {
const oldConfig = (JSON.parse(oldConfigJson) as AppConfig);
localAppConfig = {...localAppConfig, ...oldConfig};
window.localStorage.removeItem("appConfig");
}
window.api.saveAppConfig(localAppConfig);
}

async function createGameList() {
await migrateConfig();
let config: AppConfig;
try {
config = await window.api.appConfig();
} catch (e) {
console.log("Failed to load config.json");
}
config = {...config, ...(JSON.parse(window.localStorage.getItem("appConfig") || "{}") as AppConfig)};

addStyle(config.customCSS ?? "");

Expand All @@ -162,17 +193,14 @@ async function createGameList() {

applyAppConfig(config);


gameItemList.childNodes.forEach((value) => {
if (value.nodeName === "template")
return;
value.remove();
}
);
const gameList = window.localStorage.getItem("gameList") || "[]";
let gameListJson: GameConfig[] = JSON.parse(gameList);
gameListJson = [...config.games, ...gameListJson];
gameListJson.forEach(createGameItem);

config.games.forEach(createGameItem);
}

while (!window) {
Expand Down
2 changes: 1 addition & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ type AppConfig = {
autoCacheClear?: boolean;
customCSS?: string;
ignoreCertificateErrors?: boolean;
experimentalMultiInstance?: boolean;
}


type UserData = {
cachePath?: string;
[index: GameId]: GameUserData;
app?: AppConfig;
}

type GameUserData = {
Expand Down

0 comments on commit f3bae89

Please sign in to comment.