Skip to content

Commit

Permalink
updated for config_file.js v1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
yuko1101 committed Feb 26, 2025
1 parent 551e744 commit cd5a949
Show file tree
Hide file tree
Showing 34 changed files with 153 additions and 100 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# 4.6.5
- Upgraded dependencies (for config_file.js).
# 4.6.4
- Deprecated ElementalBurst#costElemVal (use ElementalBurst#requiredCharge or ElementalBurst#maxCharge instead).
- Added ElementalBurst#requiredCharge and ElementalBurst#maxCharge.
Expand Down
52 changes: 36 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
"homepage": "https://enka-network-api.vercel.app",
"dependencies": {
"axios": "^1.4.0",
"config_file.js": "^1.3.0",
"enka-system": "^0.3.4",
"config_file.js": "^1.4.0",
"enka-system": "^0.3.6",
"unzip-stream": "^0.3.1"
},
"devDependencies": {
Expand Down
41 changes: 22 additions & 19 deletions src/client/CachedAssetsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import fs from "fs";
import path from "path";
import axios from "axios";
import unzip, { Entry } from "unzip-stream";
import { ConfigFile, JsonArray, JsonObject, bindOptions, move, JsonReader } from "config_file.js";
import { ConfigFile, JsonArray, JsonObject, bindOptions, move, JsonReader, JsonOptions, defaultJsonOptions } from "config_file.js";
import { fetchJSON } from "../utils/axios_utils";
import { ObjectKeysManager } from "./ObjectKeysManager";
import { EnkaClient } from "./EnkaClient";
import { validateCache } from "../utils/cache_utils";

const languages: LanguageCode[] = ["chs", "cht", "de", "en", "es", "fr", "id", "jp", "kr", "pt", "ru", "th", "vi"];

let dataMemory: { [key: string]: JsonReader } = {};
export const excelJsonOptions = {
allowBigint: false,
} as const satisfies JsonOptions;
let dataMemory: { [key: string]: JsonReader<typeof excelJsonOptions> } = {};


const initialLangDataMemory: NullableLanguageMap = { chs: null, cht: null, de: null, en: null, es: null, fr: null, id: null, jp: null, kr: null, pt: null, ru: null, th: null, vi: null };
Expand Down Expand Up @@ -118,7 +121,7 @@ export class CachedAssetsManager {
cacheDirectoryPath: string;

_cacheUpdater: NodeJS.Timer | null;
_githubCache: ConfigFile | null;
_githubCache: ConfigFile<typeof defaultJsonOptions> | null;
_isFetching: boolean;

constructor(enka: EnkaClient) {
Expand Down Expand Up @@ -159,7 +162,7 @@ export class CachedAssetsManager {

const githubCachePath = path.resolve(this.cacheDirectoryPath, "github", "genshin_data.json");
if (!fs.existsSync(githubCachePath) || !this._githubCache) {
this._githubCache = await new ConfigFile(githubCachePath, {
this._githubCache = await new ConfigFile(githubCachePath, defaultJsonOptions, {
"lastUpdate": 0,
"rawLastUpdate": 0,
}).load();
Expand Down Expand Up @@ -397,15 +400,15 @@ export class CachedAssetsManager {
/**
* @param name without extensions (.json)
*/
getGenshinCacheData(name: string): JsonReader {
dataMemory[name] ??= new JsonReader(JSON.parse(fs.readFileSync(this.getJSONDataPath(name), "utf-8")));
getGenshinCacheData(name: string): JsonReader<typeof excelJsonOptions> {
dataMemory[name] ??= new JsonReader(excelJsonOptions, JSON.parse(fs.readFileSync(this.getJSONDataPath(name), "utf-8")));
return dataMemory[name];
}

/**
* @returns text map for a specific language
*/
getLanguageData(lang: LanguageCode, directory?: string): { [key: string]: string } {
getLanguageData(lang: LanguageCode, directory?: string): JsonObject<string> {
// Avoid "js/prototype-polluting-assignment" just in case. (https://github.com/yuko1101/enka-network-api/security/code-scanning/252)
if (["__proto__", "constructor", "prototype"].includes(lang)) return {};
langDataMemory[lang] ??= JSON.parse(fs.readFileSync(this.getLanguageDataPath(lang), "utf-8"));
Expand Down Expand Up @@ -467,14 +470,14 @@ export class CachedAssetsManager {
push(...textMapWhiteList);

data["AvatarExcelConfigData"].forEach(c => {
const json = new JsonReader(c);
const json = new JsonReader(excelJsonOptions, c);
push(
json.getAsNumber("nameTextMapHash"),
json.getAsNumber("descTextMapHash"),
);
});
data["FetterInfoExcelConfigData"].forEach(c => {
const json = new JsonReader(c);
const json = new JsonReader(excelJsonOptions, c);
push(
json.getAsNumber("avatarNativeTextMapHash"),
json.getAsNumber("avatarVisionBeforTextMapHash"),
Expand All @@ -489,49 +492,49 @@ export class CachedAssetsManager {
);
});
data["AvatarCostumeExcelConfigData"].forEach(c => {
const json = new JsonReader(c);
const json = new JsonReader(excelJsonOptions, c);
push(json.getAsNumber("nameTextMapHash"), json.getAsNumber("descTextMapHash"));
});
data["AvatarSkillExcelConfigData"].forEach(s => {
const json = new JsonReader(s);
const json = new JsonReader(excelJsonOptions, s);
push(json.getAsNumber("nameTextMapHash"), json.getAsNumber("descTextMapHash"));
});
data["ProudSkillExcelConfigData"].forEach(p => {
const json = new JsonReader(p);
const json = new JsonReader(excelJsonOptions, p);
push(json.getAsNumber("nameTextMapHash"), json.getAsNumber("descTextMapHash"), ...(json.has("paramDescList") ? json.get("paramDescList").mapArray((_, e) => e.getAsNumber()) : []));
});
data["AvatarTalentExcelConfigData"].forEach(c => {
const json = new JsonReader(c);
const json = new JsonReader(excelJsonOptions, c);
push(json.getAsNumber("nameTextMapHash"), json.getAsNumber("descTextMapHash"));
});

data["WeaponExcelConfigData"].forEach(w => {
const json = new JsonReader(w);
const json = new JsonReader(excelJsonOptions, w);
push(json.getAsNumber("nameTextMapHash"), json.getAsNumber("descTextMapHash"));
});
data["EquipAffixExcelConfigData"].forEach(a => {
const json = new JsonReader(a);
const json = new JsonReader(excelJsonOptions, a);
push(json.getAsNumber("nameTextMapHash"), json.getAsNumber("descTextMapHash"));
});
data["ReliquaryExcelConfigData"].forEach(a => {
const json = new JsonReader(a);
const json = new JsonReader(excelJsonOptions, a);
push(json.getAsNumber("nameTextMapHash"), json.getAsNumber("descTextMapHash"));
});

data["ManualTextMapConfigData"].forEach(m => {
const json = new JsonReader(m);
const json = new JsonReader(excelJsonOptions, m);
const id = json.getAsString("textMapId");
if (!manualTextMapWhiteList.includes(id) && !id.startsWith("FIGHT_REACTION_") && !id.startsWith("FIGHT_PROP_") && !id.startsWith("PROP_") && !id.startsWith("WEAPON_")) return;
push(json.getAsNumber("textMapContentTextMapHash"));
});

data["MaterialExcelConfigData"].forEach(m => {
const json = new JsonReader(m);
const json = new JsonReader(excelJsonOptions, m);
push(json.getAsNumber("nameTextMapHash"), json.getAsNumber("descTextMapHash"));
});

data["ProfilePictureExcelConfigData"].forEach(p => {
const json = new JsonReader(p);
const json = new JsonReader(excelJsonOptions, p);
push(json.getAsNumber("nameTextMapHash"));
});

Expand Down
10 changes: 6 additions & 4 deletions src/client/ObjectKeysManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { JsonReader } from "config_file.js";
import { CachedAssetsManager } from "./CachedAssetsManager";

export class ObjectKeysManager {
Expand All @@ -12,14 +11,17 @@ export class ObjectKeysManager {
readonly elementalBurstRequiredKey: string;

constructor(cachedAssetsManager: CachedAssetsManager) {
const waterAetherSkillDepot = cachedAssetsManager.getGenshinCacheData("AvatarSkillDepotExcelConfigData").findArray((_, p) => p.getAsNumber("id") === 503)?.[1] as JsonReader;
const waterAetherSkillDepot = cachedAssetsManager.getGenshinCacheData("AvatarSkillDepotExcelConfigData").findArray((_, p) => p.getAsNumber("id") === 503)?.[1];
if (!waterAetherSkillDepot) throw new Error("Failed to find the water aether skill depot.");

this.characterArkheKey = waterAetherSkillDepot.findObject((_, p) => p.getValue() === "Ousia")?.[0] as string;

const aetherProfilePicture = cachedAssetsManager.getGenshinCacheData("ProfilePictureExcelConfigData").findArray((_, p) => p.getAsNumber("id") === 1)?.[1] as JsonReader;
const aetherProfilePicture = cachedAssetsManager.getGenshinCacheData("ProfilePictureExcelConfigData").findArray((_, p) => p.getAsNumber("id") === 1)?.[1];
if (!aetherProfilePicture) throw new Error("Failed to find the aether profile picture.");
this.profilePictureTypeKey = aetherProfilePicture.findObject((_, p) => p.getValue() === "PROFILE_PICTURE_UNLOCK_BY_AVATAR")?.[0] as string;

const mavuikaElementalBurst = cachedAssetsManager.getGenshinCacheData("AvatarSkillExcelConfigData").findArray((_, p) => p.getAsNumber("id") === 11065)?.[1] as JsonReader;
const mavuikaElementalBurst = cachedAssetsManager.getGenshinCacheData("AvatarSkillExcelConfigData").findArray((_, p) => p.getAsNumber("id") === 11065)?.[1];
if (!mavuikaElementalBurst) throw new Error("Failed to find the mavuika elemental burst.");
this.elementalBurstMaxChargeKey = mavuikaElementalBurst.findObject((_, p) => p.getValue() === 200)?.[0] as string;
this.elementalBurstRequiredKey = mavuikaElementalBurst.findObject((_, p) => p.getValue() === 100)?.[0] as string;

Expand Down
4 changes: 2 additions & 2 deletions src/models/DetailedGenshinUser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JsonReader, JsonObject } from "config_file.js";
import { JsonReader, JsonObject, defaultJsonOptions } from "config_file.js";
import { EnkaClient } from "../client/EnkaClient";
import { Character } from "./character/Character";
import { GenshinUser } from "./GenshinUser";
Expand All @@ -12,7 +12,7 @@ export class DetailedGenshinUser extends GenshinUser implements IGOODComponentRe
constructor(data: JsonObject, enka: EnkaClient) {
super(data, enka);

const json = new JsonReader(data);
const json = new JsonReader(defaultJsonOptions, data);

this.showCharacterDetails = json.has("avatarInfoList");

Expand Down
3 changes: 2 additions & 1 deletion src/models/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { JsonObject, JsonReader } from "config_file.js";
import { EnkaClient } from "../client/EnkaClient";
import { AssetsNotFoundError } from "../errors/AssetsNotFoundError";
import { TextAssets } from "./assets/TextAssets";
import { excelJsonOptions } from "../client/CachedAssetsManager";

export const elementList = [null, "Fire", "Water", "Grass", "Electric", "Ice", null, "Wind", "Rock"] as const satisfies (ElementType | null)[];

Expand All @@ -16,7 +17,7 @@ export class Element {
this._data = data;
this.enka = enka;

const json = new JsonReader(this._data);
const json = new JsonReader(excelJsonOptions, this._data);

this.id = json.getAsString("textMapId") as ElementType;

Expand Down
4 changes: 2 additions & 2 deletions src/models/GenshinUser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JsonReader, JsonObject } from "config_file.js";
import { JsonReader, JsonObject, defaultJsonOptions } from "config_file.js";
import { EnkaProfile, User } from "enka-system";
import { Costume } from "./character/Costume";
import { Material, NameCard } from "./material/Material";
Expand Down Expand Up @@ -48,7 +48,7 @@ export class GenshinUser extends User {
readonly url: string;

constructor(data: JsonObject, enka: EnkaClient) {
const json = new JsonReader(data);
const json = new JsonReader(defaultJsonOptions, data);
super(json);

this.enka = enka;
Expand Down
12 changes: 7 additions & 5 deletions src/models/ProfilePicture.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { JsonObject, JsonReader } from "config_file.js";
import { defaultJsonOptions, JsonObject, JsonReader } from "config_file.js";
import { EnkaClient } from "../client/EnkaClient";
import { ImageAssets } from "./assets/ImageAssets";
import { TextAssets } from "./assets/TextAssets";
import { Costume } from "./character/Costume";
import { AssetsNotFoundError } from "../errors/AssetsNotFoundError";
import { excelJsonOptions } from "../client/CachedAssetsManager";

export type ProfilePictureType =
| "PROFILE_PICTURE_UNLOCK_BY_AVATAR"
Expand All @@ -25,7 +26,7 @@ export class ProfilePicture {
this.enka = enka;
this._data = data;

const json = new JsonReader(this._data);
const json = new JsonReader(excelJsonOptions, this._data);

this.id = json.getAsNumber("id");

Expand All @@ -38,7 +39,7 @@ export class ProfilePicture {
}

static getById(id: number, enka: EnkaClient): ProfilePicture {
const profilePicture = enka.cachedAssetsManager.getGenshinCacheData("ProfilePictureExcelConfigData").findArray((_, p) => p.getAsNumber("id") === id)?.[1] as JsonReader;
const profilePicture = enka.cachedAssetsManager.getGenshinCacheData("ProfilePictureExcelConfigData").findArray((_, p) => p.getAsNumber("id") === id)?.[1];
if (!profilePicture) throw new AssetsNotFoundError("ProfilePicture", id);

const keys = enka.cachedAssetsManager.getObjectKeysManager();
Expand All @@ -60,7 +61,8 @@ export class ProfilePicture {
const referenceId = costumeId === null ? characterId : costumeId;

const keys = enka.cachedAssetsManager.getObjectKeysManager();
const profilePictureData = enka.cachedAssetsManager.getGenshinCacheData("ProfilePictureExcelConfigData").findArray((_, p) => p.getAsString(keys.profilePictureTypeKey) === iconType && p.getAsNumber("unlockParam") === referenceId)?.[1] as JsonReader;
const profilePictureData = enka.cachedAssetsManager.getGenshinCacheData("ProfilePictureExcelConfigData").findArray((_, p) => p.getAsString(keys.profilePictureTypeKey) === iconType && p.getAsNumber("unlockParam") === referenceId)?.[1];
if (!profilePictureData) throw new AssetsNotFoundError("ProfilePicture", referenceId);

return new CharacterProfilePicture(profilePictureData.getAsJsonObject(), enka);
}
Expand All @@ -75,7 +77,7 @@ export class CharacterProfilePicture extends ProfilePicture {
constructor(data: JsonObject, enka: EnkaClient) {
super(data, enka);

const json = new JsonReader(this._data);
const json = new JsonReader(defaultJsonOptions, this._data);

const keys = enka.cachedAssetsManager.getObjectKeysManager();
const type = json.getAsString(keys.profilePictureTypeKey) as ProfilePictureType;
Expand Down
Loading

0 comments on commit cd5a949

Please sign in to comment.