-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: parse npcs from packets using mpq data
- Loading branch information
Showing
28 changed files
with
464 additions
and
380 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export { LangReader, LangNode } from './lang/lang.reader'; | ||
export { MonsterReader, MonsterNode } from './monster/monster.stat.reader'; | ||
|
||
export { Diablo2Mpq } from './mpq.loader'; | ||
export { Diablo2MpqLoader } from './mpq.loader'; | ||
export { Logger } from './log.type'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,71 @@ | ||
import * as path from 'path'; | ||
import { existsSync } from 'fs'; | ||
import { Diablo2MpqLang, Diablo2MpqMonsters } from './mpq.data'; | ||
import { Logger } from './log.type'; | ||
import { Diablo2Mpq, Diablo2MpqData } from '@diablo2/data'; | ||
import { promises as fs } from 'fs'; | ||
import { MonsterReader, MonsterReader2 } from './monster/monster.stat.reader'; | ||
import { LangReader } from './lang/lang.reader'; | ||
|
||
export class Diablo2Mpq { | ||
basePath: string; | ||
lang: Diablo2MpqLang = new Diablo2MpqLang(this); | ||
monsters: Diablo2MpqMonsters = new Diablo2MpqMonsters(this); | ||
export class Diablo2MpqLoader { | ||
static async init(basePath: string, log?: Logger, mpq = Diablo2Mpq): Promise<Diablo2MpqData> { | ||
if (!existsSync(path.join(basePath, 'data'))) { | ||
log?.error({ basePath: basePath }, 'No /data found, mpq needs to be extracted'); | ||
throw new Error('Failed to init no MPQ data found'); | ||
} | ||
|
||
constructor(basePath: string) { | ||
this.basePath = basePath; | ||
mpq.basePath = basePath; | ||
await this.initLang(mpq, log); | ||
await this.initMonsters(mpq, log); | ||
return mpq; | ||
} | ||
|
||
static async create(basePath: string, log?: Logger): Promise<Diablo2Mpq> { | ||
return new Diablo2Mpq(basePath).init(log); | ||
} | ||
/** Load in the lang files */ | ||
static async initLang(mpq: Diablo2MpqData, logger?: Logger): Promise<void> { | ||
// TODO these lang files need to be extracted, would be good to load from the MPQ directly | ||
const LangPath = `${mpq.basePath}/data/local/LNG/ENG`; | ||
const langFolderFiles = await fs.readdir(LangPath); | ||
|
||
/** | ||
* Translate a string using the lang files | ||
* @param id Id to translate | ||
*/ | ||
t(id: string | number | undefined, defaultValue: string | undefined = undefined): string | undefined { | ||
if (id == null) return defaultValue; | ||
if (typeof id == 'number') return this.lang.index[id]?.value; | ||
return this.lang.map.get(id)?.value; | ||
const langFiles = langFolderFiles.filter((f) => LangReader.LangFiles.find((lf) => f.toLowerCase().startsWith(lf))); | ||
for (const langFile of langFiles) { | ||
const startTime = Date.now(); | ||
const bytes = await fs.readFile(path.join(LangPath, langFile)); | ||
const langItems = LangReader.parse(bytes); | ||
const duration = Date.now() - startTime; | ||
|
||
for (const itm of langItems) mpq.lang.add(itm.key, itm.index, itm.value); | ||
logger?.debug({ file: langFile, records: langItems.length, duration }, 'MPQ:Load:Language'); | ||
} | ||
} | ||
|
||
async init(log?: Logger): Promise<Diablo2Mpq> { | ||
if (!existsSync(path.join(this.basePath, 'data'))) { | ||
log?.error({ basePath: this.basePath }, 'No /data found, mpq needs to be extracted'); | ||
throw new Error('Failed to init no MPQ data found'); | ||
/** Load in the lang files */ | ||
static async initMonsters(mpq: Diablo2MpqData, logger?: Logger): Promise<void> { | ||
// TODO these lang files need to be extracted, would be good to load from the MPQ directly | ||
const BinPath = `${mpq.basePath}/data/global/excel`; | ||
const binFolderFiles = await fs.readdir(BinPath); | ||
|
||
const monStatFile = binFolderFiles.find((f) => f.toLowerCase() == 'monstats.bin'); | ||
if (monStatFile) { | ||
const startTime = Date.now(); | ||
const bytes = await fs.readFile(path.join(BinPath, monStatFile)); | ||
const monItems = MonsterReader.raw(bytes as any); | ||
const duration = Date.now() - startTime; | ||
|
||
for (const monster of monItems.monsters) mpq.monsters.add(monster.id, monster.nameLangId); | ||
logger?.debug({ file: monStatFile, records: monItems.monsters.length, duration }, 'MPQ:Load:Monster'); | ||
} | ||
|
||
await this.lang.init(log); | ||
await this.monsters.init(log); | ||
return this; | ||
const monStat2File = binFolderFiles.find((f) => f.toLowerCase() == 'monstats2.bin'); | ||
if (monStat2File) { | ||
const startTime = Date.now(); | ||
const bytes = await fs.readFile(path.join(BinPath, monStat2File)); | ||
const monItems = MonsterReader2.raw(bytes as any); | ||
const duration = Date.now() - startTime; | ||
|
||
for (const mon of monItems.monsters) { | ||
mpq.monsters.addState(mon.id, mon.state); | ||
} | ||
|
||
logger?.debug({ file: monStat2File, records: monItems.monsters.length, duration }, 'MPQ:Load:Monster2'); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,5 @@ | |
"outDir": "./build" | ||
}, | ||
"include": ["src/**/*"], | ||
"references": [ ] | ||
"references": [{"path": "../data"} ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.