Skip to content

Commit

Permalink
refactor: adapt to latest api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
3Shain committed Jun 17, 2024
1 parent 5990098 commit d4fa0f1
Show file tree
Hide file tree
Showing 11 changed files with 268 additions and 106 deletions.
8 changes: 8 additions & 0 deletions src/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ body {
background-size: cover;
}

.game-logo {
display: block;
position: absolute;
background-size: cover;
top: 0;
left: 0;
};

.version-icon {
display: block;
position: absolute;
Expand Down
3 changes: 2 additions & 1 deletion src/channel-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ export interface ChannelClient {
uiContent: {
background: string;
url: string;
iconImage: string;
iconImage?: string;
launchButtonLocation?: "left" | "right";
logo?: string;
};

dismissPredownload(): void;
Expand Down
2 changes: 1 addition & 1 deletion src/clients/hk4ecn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const DEFAULT_WINE_DISTRO_URL =
export const DEFAULT_WINE_DISTRO_TAG = "v9.2-mingw";

const SERVER_DEFINITION: Server = {
id: "CN",
id: "hk4e_cn",
update_url: CN_UPDATE_URL,
channel_id: 1,
subchannel_id: 1,
Expand Down
2 changes: 1 addition & 1 deletion src/clients/hk4eos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const DEFAULT_WINE_DISTRO_URL =
export const DEFAULT_WINE_DISTRO_TAG = "v9.2-mingw";

const SERVER_DEFINITION: Server = {
id: "OS",
id: "hk4e_global",
update_url: OS_UPDATE_URL,
channel_id: 1,
subchannel_id: 0,
Expand Down
2 changes: 1 addition & 1 deletion src/clients/hkrpgos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const DEFAULT_WINE_DISTRO_URL =
export const DEFAULT_WINE_DISTRO_TAG = "v9.2-mingw";

export const SERVER_DEFINITION: Server = {
id: "hkrpg_os",
id: "hkrpg_global",
update_url: HKRPG_OS_UPDATE_URL,
channel_id: 1,
subchannel_id: 1,
Expand Down
128 changes: 82 additions & 46 deletions src/clients/mhy/hk4e/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Server } from "../../../constants";
import { Locale } from "../../../locale";
import {
assertValueDefined,
exec,
getFreeSpace,
getKey,
getKeyOrDefault,
Expand Down Expand Up @@ -37,13 +38,25 @@ import { createWorkaround3Config } from "./config/workaround-3";
import createPatchOff from "./config/patch-off";
import { getGameVersion } from "../unity";
import {
LauncherContentData,
LauncherResourceData,
HoyoConnectGameDisplay,
HoyoConnectGameId,
HoyoConnectGamePackageMainfest,
HoyoConnectGetGamePackagesResponse,
HoyoConnectGetGamesResponse,
VoicePackNames,
} from "../launcher-info";

const CURRENT_SUPPORTED_VERSION = "4.7.0";

async function fetch(url: string) {
const { stdOut } = await exec(["curl", url]);
return {
async json() {
return JSON.parse(stdOut);
},
};
}

export async function createHK4EChannelClient({
server,
locale,
Expand All @@ -56,32 +69,23 @@ export async function createHK4EChannelClient({
wine: Wine;
}): Promise<ChannelClient> {
const {
data: {
adv: { background, url, icon },
display: {
background: { url: background },
logo: { url: logo_url },
},
}: LauncherContentData = await (
await fetch(
server.adv_url +
(server.id == "CN"
? `&language=zh-cn` // CN server has no other language support
: `&language=${locale.get("CONTENT_LANG_ID")}`)
)
).json();
} = await getLatestAdvInfo(locale, server);
const {
data: {
game: {
diffs,
latest: {
version: GAME_LATEST_VERSION,
segments,
decompressed_path,
voice_packs,
size,
},
main: {
major: {
version: GAME_LATEST_VERSION,
game_pkgs,
res_list_url: decompressed_path,
},
pre_download_game,
patches,
},
}: LauncherResourceData = await getLatestVersionInfo(server);
pre_download,
} = await getLatestVersionInfo(server);

await waitImageReady(background);

const { gameInstalled, gameInstallDir, gameVersion } = await checkGameState(
Expand All @@ -94,11 +98,11 @@ export async function createHK4EChannelClient({
);
const [showPredownloadPrompt, setShowPredownloadPrompt] =
createSignal<boolean>(
pre_download_game != null && //exist pre_download_game data in server response
pre_download.major != null && //exist pre_download_game data in server response
(await getKeyOrDefault("predownloaded_all", "NOTFOUND")) ==
"NOTFOUND" && // not downloaded yet
gameInstalled && // game installed
gt(pre_download_game.latest.version, gameVersion) // predownload version is greater
gt(pre_download.major.version, gameVersion) // predownload version is greater
);
const [_gameInstallDir, setGameInstallDir] = createSignal(
gameInstallDir ?? ""
Expand All @@ -114,10 +118,10 @@ export async function createHK4EChannelClient({
updateRequired,
uiContent: {
background,
iconImage: icon,
url,
url: "",
logo: logo_url,
},
predownloadVersion: () => pre_download_game?.latest.version ?? "",
predownloadVersion: () => pre_download?.major?.version ?? "",
dismissPredownload() {
setShowPredownloadPrompt(false);
},
Expand All @@ -126,8 +130,11 @@ export async function createHK4EChannelClient({
await stats(join(selection, "pkg_version"));
} catch {
const freeSpaceGB = await getFreeSpace(selection, "g");
const requiredSpaceGB =
Math.ceil(parseInt(size) / Math.pow(1024, 3)) * 1.2;
const totalSize = game_pkgs
.map(x => x.size)
.map(parseInt)
.reduce((a, b) => a + b, 0);
const requiredSpaceGB = Math.ceil(totalSize / Math.pow(1024, 3)) * 1.2;
if (freeSpaceGB < requiredSpaceGB) {
await locale.alert(
"NO_ENOUGH_DISKSPACE",
Expand All @@ -140,7 +147,7 @@ export async function createHK4EChannelClient({
yield* downloadAndInstallGameProgram({
aria2,
gameDir: selection,
gameSegmentZips: segments.map(x => x.path),
gameSegmentZips: game_pkgs.map(x => x.url),
// gameAudioZip: voice_packs.find((x) => x.language == "zh-cn")!
// .path,
gameVersion: GAME_LATEST_VERSION,
Expand All @@ -164,7 +171,7 @@ export async function createHK4EChannelClient({
);
return;
} else if (lt(gameVersion, GAME_LATEST_VERSION)) {
const updateTarget = diffs.find(x => x.version == gameVersion);
const updateTarget = patches.find(x => x.version == gameVersion);
if (!updateTarget) {
await locale.prompt(
"UNSUPPORTED_VERSION",
Expand Down Expand Up @@ -197,14 +204,14 @@ export async function createHK4EChannelClient({
},
async *predownload() {
setShowPredownloadPrompt(false);
if (pre_download_game == null) return;
const updateTarget = pre_download_game.diffs.find(
if (pre_download.major == null) return;
const updateTarget = pre_download.patches.find(
x => x.version == gameCurrentVersion()
);
if (updateTarget == null) return;
const voicePacks = (
await Promise.all(
updateTarget.voice_packs.map(async x => {
updateTarget.audio_pkgs.map(async x => {
try {
await stats(
join(
Expand All @@ -224,15 +231,20 @@ export async function createHK4EChannelClient({
assertValueDefined(x);
return x;
});
if (updateTarget.game_pkgs.length != 1) {
throw new Error(
"assertation failed (game_pkgs.length!= 1)! please file an issue."
);
}
yield* predownloadGameProgram({
aria2,
updateFileZip: updateTarget.path,
updateFileZip: updateTarget.game_pkgs[0].url,
gameDir: _gameInstallDir(),
updateVoicePackZips: voicePacks.map(x => x.path),
updateVoicePackZips: voicePacks.map(x => x.url),
});
},
async *update() {
const updateTarget = diffs.find(x => x.version == gameCurrentVersion());
const updateTarget = patches.find(x => x.version == gameCurrentVersion());
if (!updateTarget) {
await locale.prompt(
"UNSUPPORTED_VERSION",
Expand All @@ -249,7 +261,7 @@ export async function createHK4EChannelClient({
}
const voicePacks = (
await Promise.all(
updateTarget.voice_packs.map(async x => {
updateTarget.audio_pkgs.map(async x => {
try {
await stats(
join(
Expand All @@ -269,14 +281,19 @@ export async function createHK4EChannelClient({
assertValueDefined(x);
return x;
});
if (updateTarget.game_pkgs.length != 1) {
throw new Error(
"assertation failed (game_pkgs.length!= 1)! please file an issue."
);
}
yield* updateGameProgram({
aria2,
server,
currentGameVersion: gameCurrentVersion(),
updatedGameVersion: GAME_LATEST_VERSION,
updateFileZip: updateTarget.path,
updateFileZip: updateTarget.game_pkgs[0].url,
gameDir: _gameInstallDir(),
updateVoicePackZips: voicePacks.map(x => x.path),
updateVoicePackZips: voicePacks.map(x => x.url),
});
batch(() => {
setGameVersion(GAME_LATEST_VERSION);
Expand Down Expand Up @@ -337,7 +354,7 @@ export async function createHK4EChannelClient({
const [PO] = await createPatchOff({ locale, config });

return function () {
return [<W3 />, <PO />];
return ["Game Version: ", gameCurrentVersion(), <W3 />, <PO />];
};
},
};
Expand Down Expand Up @@ -367,9 +384,28 @@ async function checkGameState(locale: Locale, server: Server) {

async function getLatestVersionInfo(
server: Server
): Promise<LauncherResourceData> {
const ret: LauncherResourceData = await (
): Promise<HoyoConnectGamePackageMainfest> {
const ret: HoyoConnectGetGamePackagesResponse = await (
await fetch(server.update_url)
).json();
return ret;
const game = ret.data.game_packages.find(x => x.game.biz == server.id);
if (!game) throw new Error(`failed to fetch game information: ${server.id}`);
return game;
}

async function getLatestAdvInfo(
locale: Locale,
server: Server
): Promise<HoyoConnectGameId & HoyoConnectGameDisplay> {
const ret: HoyoConnectGetGamesResponse = await (
await fetch(
server.adv_url +
(server.id == "CN"
? `&language=zh-cn` // CN server has no other language support
: `&language=${locale.get("CONTENT_LANG_ID")}`)
)
).json();
const game = ret.data.games.find(x => x.biz == server.id);
if (!game) throw new Error(`failed to fetch game information: ${server.id}`);
return game;
}
Loading

0 comments on commit d4fa0f1

Please sign in to comment.