Skip to content

Commit

Permalink
refactor: change Genshin player info API wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
HoraceHuang-ui committed Jan 27, 2024
1 parent 3bf310d commit 8dd2e8d
Show file tree
Hide file tree
Showing 11 changed files with 30,382 additions and 4,486 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fetch('../../app.asar/package.json')
# Credits

- [electron-vite/electron-vite-vue](https://github.com/electron-vite/electron-vite-vue)
- [enkanetwork.js](https://github.com/Jelosus2/enkanetwork.js)
- [enka-network-api](https://github.com/yuko1101/enka-network-api)
- [Mar-7th/MiHoMo API](https://march7th.xiaohei.moe/zh/resource/mihomo_api.html) | [Mar-7th/StarRailRes](https://github.com/Mar-7th/StarRailRes)

# Translation
Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fetch('../../app.asar/package.json')
# 鸣谢

- [electron-vite/electron-vite-vue](https://github.com/electron-vite/electron-vite-vue)
- [enkanetwork.js](https://github.com/Jelosus2/enkanetwork.js)
- [enka-network-api](https://github.com/yuko1101/enka-network-api)
- [Mar-7th/MiHoMo API](https://march7th.xiaohei.moe/zh/resource/mihomo_api.html) | [Mar-7th/StarRailRes](https://github.com/Mar-7th/StarRailRes)

# 翻译
Expand Down
30 changes: 22 additions & 8 deletions electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ const Store = require('electron-store');
const store = new Store();
const child = require('child_process')
const {dialog} = require('electron');
const {Wrapper} = require('enkanetwork.js')
const {genshin} = new Wrapper({
language: 'zh-CN'
})
const {EnkaClient, TextAssets, DynamicTextAssets} = require("enka-network-api");

// The built directory structure
//
Expand Down Expand Up @@ -153,10 +150,27 @@ async function createWindow() {
});

ipcMain.handle("enka:getGenshinPlayer", async (_event, uid, lang) => {
console.log("genshin enka ipcMain" + uid)
genshin.language = lang
const result = await genshin.getPlayer(uid)
return result
function convertObjectToJson(obj: any) {
if (typeof obj !== "object" || obj === null || obj === undefined) return obj;
const entries = Object.entries(obj)
.filter(([key, value]) => !key.startsWith("_") && !(value instanceof EnkaClient)) // filter out private properties and EnkaClient instance, which has circular object
.map(([key, value]) => [key, convertObjectToJson(value)]);
if (obj instanceof TextAssets) {
entries.push(["text", obj instanceof DynamicTextAssets ? obj.getNullableReplacedText() : obj.getNullable()]); // convert TextAssets to string
}
return Object.fromEntries(entries);
}

const enka = new EnkaClient({
defaultLanguage: lang,
requestTimeout: 10000
})

try {
return convertObjectToJson(await enka.fetchUser(uid, false))
} catch {
return convertObjectToJson(await enka.fetchUser(uid, true))
}
})

ipcMain.handle("path:joinDirnameAsset", (_event, arg) => {
Expand Down
235 changes: 116 additions & 119 deletions electron/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,96 +2,93 @@ import {contextBridge, ipcRenderer} from "electron";


contextBridge.exposeInMainWorld("store", {
set: (key: String, value: any, json: Boolean) => ipcRenderer.send("store:set", key, value, json),
// window.storeAPI.set('genshinPath', 'd:\\...')
get: (key: String) => ipcRenderer.invoke("store:get", key),
delete: (key: String) => ipcRenderer.send("store:delete", key),
clear: () => ipcRenderer.send("store:clear"),
set: (key: String, value: any, json: Boolean) => ipcRenderer.send("store:set", key, value, json),
// window.storeAPI.set('genshinPath', 'd:\\...')
get: (key: String) => ipcRenderer.invoke("store:get", key),
delete: (key: String) => ipcRenderer.send("store:delete", key),
clear: () => ipcRenderer.send("store:clear"),
})
contextBridge.exposeInMainWorld("child", {
exec: (path: String) => ipcRenderer.send("child:exec", path)
exec: (path: String) => ipcRenderer.send("child:exec", path)
})
contextBridge.exposeInMainWorld("dialog", {
show: async (options) => {
const result = await ipcRenderer.invoke("dialog:show", options)
return result
},
showAndCopy: async (options) => {
const result = await ipcRenderer.invoke("dialog:showAndCopy", options)
return result
}
show: async (options) => {
const result = await ipcRenderer.invoke("dialog:show", options)
return result
},
showAndCopy: async (options) => {
const result = await ipcRenderer.invoke("dialog:showAndCopy", options)
return result
}
})
contextBridge.exposeInMainWorld("enka", {
getGenshinPlayer: async (uid: String, lang: String) => {
console.log('ipcRenderer')
const result = await ipcRenderer.invoke("enka:getGenshinPlayer", uid, lang)
console.log(result)
return result
}
getGenshinPlayer: async (uid: String, lang: String) => {
return await ipcRenderer.invoke("enka:getGenshinPlayer", uid, lang)
}
})
contextBridge.exposeInMainWorld("win", {
close: () => {
ipcRenderer.send("win:close")
},
min: () => {
ipcRenderer.send("win:min")
},
tray: () => {
ipcRenderer.send("win:tray")
}
close: () => {
ipcRenderer.send("win:close")
},
min: () => {
ipcRenderer.send("win:min")
},
tray: () => {
ipcRenderer.send("win:tray")
}
})
contextBridge.exposeInMainWorld("path", {
joinDirnameAsset: (arg: String) => ipcRenderer.invoke("path:joinDirnameAsset", arg)
joinDirnameAsset: (arg: String) => ipcRenderer.invoke("path:joinDirnameAsset", arg)
})
contextBridge.exposeInMainWorld("axios", {
post: async (url: String) => {
const result = await ipcRenderer.invoke("axios:post", url)
return result
},
get: async (url: String) => {
const result = await ipcRenderer.invoke("axios:get", url)
return result
}
post: async (url: String) => {
const result = await ipcRenderer.invoke("axios:post", url)
return result
},
get: async (url: String) => {
const result = await ipcRenderer.invoke("axios:get", url)
return result
}
})
contextBridge.exposeInMainWorld("electron", {
openExtLink: (url: String) => {
ipcRenderer.send("elec:openExtLink", url)
}
openExtLink: (url: String) => {
ipcRenderer.send("elec:openExtLink", url)
}
})
contextBridge.exposeInMainWorld("github", {
getLatestRelease: async () => {
const result = await ipcRenderer.invoke("github:getLatestRelease")
return result
}
getLatestRelease: async () => {
const result = await ipcRenderer.invoke("github:getLatestRelease")
return result
}
})

function domReady(
condition: DocumentReadyState[] = ["complete", "interactive"]
condition: DocumentReadyState[] = ["complete", "interactive"]
) {
return new Promise((resolve) => {
if (condition.includes(document.readyState)) {
resolve(true);
} else {
document.addEventListener("readystatechange", () => {
return new Promise((resolve) => {
if (condition.includes(document.readyState)) {
resolve(true);
resolve(true);
} else {
document.addEventListener("readystatechange", () => {
if (condition.includes(document.readyState)) {
resolve(true);
}
});
}
});
}
});
});
}

const safeDOM = {
append(parent: HTMLElement, child: HTMLElement) {
if (!Array.from(parent.children).find((e) => e === child)) {
return parent.appendChild(child);
}
},
remove(parent: HTMLElement, child: HTMLElement) {
if (Array.from(parent.children).find((e) => e === child)) {
return parent.removeChild(child);
}
},
append(parent: HTMLElement, child: HTMLElement) {
if (!Array.from(parent.children).find((e) => e === child)) {
return parent.appendChild(child);
}
},
remove(parent: HTMLElement, child: HTMLElement) {
if (Array.from(parent.children).find((e) => e === child)) {
return parent.removeChild(child);
}
},
};

/**
Expand All @@ -101,44 +98,44 @@ const safeDOM = {
* https://matejkustec.github.io/SpinThatShit
*/
function useLoading() {
// const className = `loaders-css__square-spin`;
// const styleContent = `
// @keyframes square-spin {
// 25% { transform: perspective(100px) rotateX(180deg) rotateY(0); }
// 50% { transform: perspective(100px) rotateX(180deg) rotateY(180deg); }
// 75% { transform: perspective(100px) rotateX(0) rotateY(180deg); }
// 100% { transform: perspective(100px) rotateX(0) rotateY(0); }
// }
// .${className} > div {
// animation-fill-mode: both;
// width: 50px;
// height: 50px;
// background: #fff;
// animation: square-spin 3s 0s cubic-bezier(0.09, 0.57, 0.49, 0.9) infinite;
// }
// .app-loading-wrap {
// position: fixed;
// top: 0;
// left: 0;
// width: 100vw;
// height: 100vh;
// display: flex;
// align-items: center;
// justify-content: center;
// background: #282c34;
// z-index: 9;
// }
// `;
// const oStyle = document.createElement("style");
// const oDiv = document.createElement("div");
// const className = `loaders-css__square-spin`;
// const styleContent = `
// @keyframes square-spin {
// 25% { transform: perspective(100px) rotateX(180deg) rotateY(0); }
// 50% { transform: perspective(100px) rotateX(180deg) rotateY(180deg); }
// 75% { transform: perspective(100px) rotateX(0) rotateY(180deg); }
// 100% { transform: perspective(100px) rotateX(0) rotateY(0); }
// }
// .${className} > div {
// animation-fill-mode: both;
// width: 50px;
// height: 50px;
// background: #fff;
// animation: square-spin 3s 0s cubic-bezier(0.09, 0.57, 0.49, 0.9) infinite;
// }
// .app-loading-wrap {
// position: fixed;
// top: 0;
// left: 0;
// width: 100vw;
// height: 100vh;
// display: flex;
// align-items: center;
// justify-content: center;
// background: #282c34;
// z-index: 9;
// }
// `;
// const oStyle = document.createElement("style");
// const oDiv = document.createElement("div");

// oStyle.id = "app-loading-style";
// oStyle.innerHTML = styleContent;
// oDiv.className = "app-loading-wrap";
// oDiv.innerHTML = `<div class="${className}"><div></div></div>`;
// oStyle.id = "app-loading-style";
// oStyle.innerHTML = styleContent;
// oDiv.className = "app-loading-wrap";
// oDiv.innerHTML = `<div class="${className}"><div></div></div>`;

// const className = "loaders-css__klee-loading";
const styleContent = `
// const className = "loaders-css__klee-loading";
const styleContent = `
.klee-loading {
width: 120px;
height: 120px;
Expand Down Expand Up @@ -168,35 +165,35 @@ function useLoading() {
font-size: 20px;
}
`;
const oStyle = document.createElement("style")
const oDiv = document.createElement("div");
oStyle.id = "app-loading-style";
oStyle.innerHTML = styleContent;
oDiv.className = "app-loading-wrap";
oDiv.innerHTML = `<div class="font-genshin inner-wrapper">
const oStyle = document.createElement("style")
const oDiv = document.createElement("div");
oStyle.id = "app-loading-style";
oStyle.innerHTML = styleContent;
oDiv.className = "app-loading-wrap";
oDiv.innerHTML = `<div class="font-genshin inner-wrapper">
<img class="klee-loading" src="../../src/assets/kleeLoading.gif" />
<div class="below-text">Loading...</div>
</div>`

return {
appendLoading() {
safeDOM.append(document.head, oStyle);
safeDOM.append(document.body, oDiv);
},
removeLoading() {
safeDOM.remove(document.head, oStyle);
safeDOM.remove(document.body, oDiv);
},
};
return {
appendLoading() {
safeDOM.append(document.head, oStyle);
safeDOM.append(document.body, oDiv);
},
removeLoading() {
safeDOM.remove(document.head, oStyle);
safeDOM.remove(document.body, oDiv);
},
};
}

// ----------------------------------------------------------------------

const { appendLoading, removeLoading } = useLoading();
const {appendLoading, removeLoading} = useLoading();
domReady().then(appendLoading);

window.onmessage = (ev) => {
ev.data.payload === "removeLoading" && removeLoading();
ev.data.payload === "removeLoading" && removeLoading();
};

setTimeout(removeLoading, 4999);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"axios": "^1.6.5",
"bootstrap-icons": "^1.11.3",
"electron-store": "^8.1.0",
"enkanetwork.js": "^2.6.6",
"enka-network-api": "^4.1.6",
"marked": "^11.1.1",
"path": "^0.12.7",
"sass": "^1.70.0",
Expand Down
2 changes: 1 addition & 1 deletion src/i18n/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"mainpage_title": "米哈游聚合启动器",
"mainpage_buttonText": "{game}启动",
"genshin_launcherContentsUrl": "https://sdk-static.mihoyo.com/hk4e_cn/mdk/launcher/api/content?filter_adv=false&key=eYd89JmJ&language=zh-cn&launcher_id=18",
"gs_enkaLangCode": "zh-CN",
"gs_enkaLangCode": "chs",
"gs_emptyPlayerTip": "此处将显示角色的详细信息",
"gs_showcaseTip": "当前暂无详细信息,请在游戏内打开“显示角色详情”后再试。",
"gs_loadingPlayerInfo": "正在加载数据,请稍候……",
Expand Down
Loading

0 comments on commit 8dd2e8d

Please sign in to comment.