Skip to content

Commit

Permalink
feat(clientdb): read locale strings in single locale
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Jan 15, 2024
1 parent a413f08 commit a59c6ae
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
9 changes: 6 additions & 3 deletions src/lib/clientdb/ClientDb.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { IoSource, IoStream, openStream } from '@wowserhq/io';
import ClientDbRecord from './ClientDbRecord.js';
import * as dbIo from './io.js';
import { DB_LOCALE } from './const.js';

interface Constructor<T> {
new (...args: any[]): T;
}

class ClientDb<T extends ClientDbRecord> {
#RecordClass: Constructor<T>;
#locale: DB_LOCALE;

#minId = 0xfffffff;
#maxId = 0;
Expand All @@ -17,8 +19,9 @@ class ClientDb<T extends ClientDbRecord> {
#records: T[];
#recordsById: Record<number, T>;

constructor(RecordClass: Constructor<T>) {
constructor(RecordClass: Constructor<T>, locale = DB_LOCALE.LOCALE_ENUS) {
this.#RecordClass = RecordClass;
this.#locale = locale;
}

get records() {
Expand All @@ -37,7 +40,7 @@ class ClientDb<T extends ClientDbRecord> {
const recordsById = {};

for (let i = 0; i < this.#recordCount; i++) {
const record = new this.#RecordClass().load(stream, stringBlockStream);
const record = new this.#RecordClass().load(stream, stringBlockStream, this.#locale);

this.#minId = record.id < this.#minId ? record.id : this.#minId;
this.#maxId = record.id > this.#maxId ? record.id : this.#maxId;
Expand Down Expand Up @@ -92,4 +95,4 @@ class ClientDb<T extends ClientDbRecord> {
}

export default ClientDb;
export { ClientDb };
export { ClientDb, DB_LOCALE };
21 changes: 9 additions & 12 deletions src/lib/clientdb/ClientDbRecord.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IoStream, IoType } from '@wowserhq/io';
import * as dbIo from './io.js';
import { DB_LOCALE } from './const.js';

class ClientDbRecord {
id: number;
Expand All @@ -10,15 +11,15 @@ class ClientDbRecord {
this.#recordIo = recordIo;
}

load(stream: IoStream, stringBlock: IoStream) {
load(stream: IoStream, stringBlock: IoStream, locale: DB_LOCALE) {
const record = this.#recordIo.read(stream);

for (const [key, value] of Object.entries(record)) {
if (key.endsWith('.locstring')) {
const strings = this.#readLocString(value as number[], stringBlock);
const string = this.#readLocString(value as number[], stringBlock, locale);
const normalizedKey = key.replace('.locstring', '');

this[normalizedKey] = strings;
this[normalizedKey] = string;
} else {
this[key] = value;
}
Expand All @@ -27,22 +28,18 @@ class ClientDbRecord {
return this;
}

#readLocString(value: number[], stringBlock: IoStream) {
#readLocString(value: number[], stringBlock: IoStream, locale: DB_LOCALE) {
// All values before last are offsets in the string block
const offsets = value.slice(0, -1);

// Last value is flags
const flags = value.at(-1);

const strings: string[] = [];
// Read locale-specific string
stringBlock.offset = offsets[locale];
const localeString = dbIo.string.read(stringBlock);

for (const offset of offsets) {
stringBlock.offset = offset;
const string = dbIo.string.read(stringBlock);
strings.push(string);
}

return strings;
return localeString;
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/lib/clientdb/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
enum DB_LOCALE {
LOCALE_ENUS = 0,
LOCALE_KOKR,
LOCALE_FRFR,
LOCALE_DEDE,
LOCALE_ZHCN,
LOCALE_ZHTW,
LOCALE_ESES,
LOCALE_ESMX,
LOCALE_RURU,
}

export { DB_LOCALE };
2 changes: 1 addition & 1 deletion src/lib/clientdb/record/AreaTableRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AreaTableRecord extends ClientDbRecord {
zoneMusic: number;
introSound: number;
explorationLevel: number;
areaName: string[];
areaName: string;
factionGroupMask: number;
liquidTypeId: number[];
minElevation: number;
Expand Down
2 changes: 1 addition & 1 deletion src/spec/clientdb/ClientDb.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('ClientDb', () => {
const record = dbc.getRecordByIndex(1);

expect(record.id).toBe(2);
expect(record.areaName[0]).toBe('Longshore');
expect(record.areaName).toBe('Longshore');
expect(record.minElevation).toBe(-500);
});
});
Expand Down

0 comments on commit a59c6ae

Please sign in to comment.