Skip to content

Commit

Permalink
Refactor main.js and renderer.js to fix issues with administrator mod…
Browse files Browse the repository at this point in the history
…e and auto-start game
  • Loading branch information
XiaoLinXiaoZhu committed Oct 17, 2024
1 parent 3441851 commit 6f88aaa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
22 changes: 20 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,33 @@ ipcMain.handle('sync-localStorage', async (event, userConfig) => {
ifUseAdmin = userConfig.ifUseAdmin;
ifAutoStartGame = userConfig.ifAutoStartGame;

//debug
console.log(`firstSync: ${firstSync}`);

//如果是第一次同步localStorage,则需要进行一些操作
if (!firstSync) return;

//检查是否 开启了 useAdmin
if (HMC.isAdmin() === false && ifUseAdmin === true) {
// //debug
// console.log(ifUseAdmin);
// //打印一下ifUseAdmin的类型
// console.log(typeof ifUseAdmin);
// console.log(`ifUseAdmin: ${ifUseAdmin}, isAdmin: ${HMC.isAdmin()}`);
// console.log("!HMC.isAdmin: " + !HMC.isAdmin());
// console.log("ifUseAdmin == true: " + ifUseAdmin == "true");
// console.log("!HMC.isAdmin() && ifUseAdmin == true: " + (!HMC.isAdmin() && ifUseAdmin == true));

//为什么这里的ifUseAdmin是字符串……导致ifUseAdmin == true这个判断不成立
if (ifUseAdmin == "true" && !HMC.isAdmin()) {
//如果开启了 useAdmin,则需要以管理员模式重新启动
// 通过管理员模式重新启动
restartAsAdmin();
return;
}

//检查是否 开启了 autoStartGame
//debug
console.log(`ifAutoStartGame: ${ifAutoStartGame}`);
if (ifAutoStartGame === true) {
//启动游戏
// 之后不再在渲染进程中启动游戏,而是在主进程中启动游戏
Expand Down Expand Up @@ -506,7 +522,9 @@ ipcMain.handle('select-image', async () => {
//-------------------自动化-------------------

// 使用管理员模式重新启动
function restartAsAdmin() {
async function restartAsAdmin() {
//debug
console.log(`restart as admin: ${exePath}`);
if (isWindows) {
const exePath = process.execPath;

Expand Down
20 changes: 14 additions & 6 deletions renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ document.addEventListener('DOMContentLoaded', async () => {
autoRefreshInZZZSwitch.addEventListener('change', () => {
ifAutofreshInZZZ = autoRefreshInZZZSwitch.checked;
//保存ifAutofreshInZZZ
localStorage.setItem('auto-refresh-in-zzz', ifAutofreshInZZZ);
localStorage.setItem('ifAutofreshInZZZ', ifAutofreshInZZZ);
//debug
console.log("ifAutofreshInZZZ: " + ifAutofreshInZZZ);
});
Expand All @@ -804,7 +804,7 @@ document.addEventListener('DOMContentLoaded', async () => {
autoStartGameSwitch.addEventListener('change', () => {
ifAutoStartGame = autoStartGameSwitch.checked;
//保存ifAutoStartGame
localStorage.setItem('auto-start-game', ifAutoStartGame);
localStorage.setItem('ifAutoStartGame', ifAutoStartGame);
//debug
console.log("ifAutoStartGame: " + ifAutoStartGame);

Expand All @@ -816,7 +816,7 @@ document.addEventListener('DOMContentLoaded', async () => {
ifAutoStartGame = false;
autoStartGameSwitch.checked = false;
//保存ifAutoStartGame
localStorage.setItem('auto-start-game', ifAutoStartGame);
localStorage.setItem('ifAutoStartGame', ifAutoStartGame);
return;
}
}
Expand All @@ -827,7 +827,7 @@ document.addEventListener('DOMContentLoaded', async () => {
useAdminSwitch.addEventListener('change', () => {
ifUseAdmin = useAdminSwitch.checked;
//保存ifUseAdmin
localStorage.setItem('use-admin', ifUseAdmin);
localStorage.setItem('ifUseAdmin', ifUseAdmin);
//debug
console.log("ifUseAdmin: " + ifUseAdmin);
});
Expand Down Expand Up @@ -1396,7 +1396,7 @@ document.addEventListener('DOMContentLoaded', async () => {

window.addEventListener('unload', function (event) {
localStorage.setItem('fullscreen', isFullScreen);
asyncLocalStorage();


if (!isFullScreen) {
localStorage.setItem('bounds', JSON.stringify({
Expand All @@ -1405,8 +1405,8 @@ document.addEventListener('DOMContentLoaded', async () => {
width: window.outerWidth,
height: window.innerHeight,
}));
asyncLocalStorage();
}
asyncLocalStorage();
});


Expand Down Expand Up @@ -1439,6 +1439,8 @@ document.addEventListener('DOMContentLoaded', async () => {
ifUseAdmin: ifUseAdmin,
theme: theme
};
//debug
console.log(userConfig);

ipcRenderer.invoke('sync-localStorage', userConfig);
}
Expand Down Expand Up @@ -1706,6 +1708,8 @@ document.addEventListener('DOMContentLoaded', async () => {

//使用替换的方式而不是清空再添加的方式实现loadModList,减少页面重绘次数
async function loadModList() {
//debug
console.log("loadModList");
//加载mod列表
mods = await ipcRenderer.invoke('get-mods');
//获取当前modContainer的所有子元素
Expand Down Expand Up @@ -1803,6 +1807,10 @@ document.addEventListener('DOMContentLoaded', async () => {
}

async function init() {
//debug
console.log("init");
// 同步用户设置
asyncLocalStorage();
// 设置窗口位置和大小
await ipcRenderer.invoke('set-bounds', bounds);
// 设置窗口全屏
Expand Down

0 comments on commit 6f88aaa

Please sign in to comment.