Skip to content

Commit

Permalink
lib: add interval support
Browse files Browse the repository at this point in the history
  • Loading branch information
SnosMe committed Dec 28, 2024
1 parent 34520fa commit 45c1bf3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pathofexile-dat",
"version": "13.0.0",
"version": "14.0.0",
"type": "module",
"bin": "./dist/cli/run.js",
"exports": {
Expand All @@ -14,7 +14,7 @@
},
"dependencies": {
"ooz-wasm": "^2.0.0",
"pathofexile-dat-schema": "^7.0.0"
"pathofexile-dat-schema": "^8.0.0"
},
"devDependencies": {
"@types/node": "^18.0.0",
Expand Down
1 change: 1 addition & 0 deletions lib/src/cli/export-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ function importHeaders (
offset,
type: {
array: column.array,
interval: column.interval,
integer:
// column.type === 'u8' ? { unsigned: true, size: 1 }
column.type === 'u16' ? { unsigned: true, size: 2 }
Expand Down
13 changes: 7 additions & 6 deletions lib/src/dat-analysis/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ export function validateHeader (header: Header, columns: ColumnStats[]): boolean
const array = header.type.array
const key = header.type.key
const size = integer?.size || decimal?.size
const count = (header.type.interval) ? 2 : 1

const space = columns.length - header.offset
const stats = columns[header.offset]

if (integer) {
// if (size === 1 && !array) return space >= 1
if (size === 2 && !array) return space >= 2
if (size === 4 && !array) return space >= 4
// if (size === 8 && !array) return space >= 8
// if (size === 1 && !array) return space >= 1 * count
if (size === 2 && !array) return space >= 2 * count
if (size === 4 && !array) return space >= 4 * count
// if (size === 8 && !array) return space >= 8 * count

// if (size === 1 && array) return stats.refArray && true
// if (size === 2 && array) return stats.refArray && stats.refArray.short
Expand All @@ -30,8 +31,8 @@ export function validateHeader (header: Header, columns: ColumnStats[]): boolean
}

if (decimal) {
if (size === 4 && !array) return space >= 4
// if (size === 8 && !array) return space >= 8
if (size === 4 && !array) return space >= 4 * count
// if (size === 8 && !array) return space >= 8 * count

if (size === 4 && array) return stats.refArray && stats.refArray.numeric32
// if (size === 8 && array) return stats.refArray && stats.refArray.longLong
Expand Down
8 changes: 5 additions & 3 deletions lib/src/dat/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface Header {
offset: number
type: {
array?: boolean
interval?: boolean
boolean?: {}
integer?: { unsigned: boolean, size: number }
decimal?: { size: number }
Expand All @@ -12,9 +13,10 @@ export interface Header {
}
}

export function getHeaderLength (header: Pick<Header, 'type'>, datFile: DatFile) {
export function getHeaderLength (header: Pick<Header, 'type'>, datFile: DatFile): number {
const { type } = header
const { fieldSize } = datFile
const count = (type.interval) ? 2 : 1

if (type.array) {
return fieldSize.ARRAY
Expand All @@ -23,9 +25,9 @@ export function getHeaderLength (header: Pick<Header, 'type'>, datFile: DatFile)
} else if (type.key) {
return type.key.foreign ? fieldSize.KEY_FOREIGN : fieldSize.KEY
} else if (type.integer) {
return type.integer.size
return type.integer.size * count
} else if (type.decimal) {
return type.decimal.size
return type.decimal.size * count
} else if (type.boolean) {
return fieldSize.BOOL
}
Expand Down
20 changes: 14 additions & 6 deletions lib/src/dat/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,30 @@ export function getFieldReader (header: Header, datFile: DatFile) {
oneFn = (type.key.foreign) ? oneKeyForeign : oneKeySelf
}

if (!type.array) {
return function readScalarAtRow (rowIdx: number): Scalar {
if (type.array) {
return function readArrayAtRow (rowIdx: number): Scalar[] {
const offset = (rowIdx * datFile.rowLength) + header.offset
return oneFn(datFile.readerFixed, offset, datFile.dataVariable)
return readMany(datFile, offset, oneFn, elSize)
}
} else if (type.interval) {
return function readIntervalAtRow (rowIdx: number): [Scalar, Scalar] {
const offset = (rowIdx * datFile.rowLength) + header.offset
return [
oneFn(datFile.readerFixed, offset + 0, datFile.dataVariable),
oneFn(datFile.readerFixed, offset + elSize, datFile.dataVariable)
]
}
} else {
return function readArrayAtRow (rowIdx: number): Scalar[] {
return function readScalarAtRow (rowIdx: number): Scalar {
const offset = (rowIdx * datFile.rowLength) + header.offset
return readMany(datFile, offset, oneFn, elSize)
return oneFn(datFile.readerFixed, offset, datFile.dataVariable)
}
}
}

export function readColumn (header: Header, datFile: DatFile) {
const reader = getFieldReader(header, datFile)
const out: Array<Scalar | Scalar[]> = []
const out: Array<Scalar | Scalar[] | [Scalar, Scalar]> = []
for (let rowIdx = 0; rowIdx < datFile.rowCount; ++rowIdx) {
out.push(reader(rowIdx))
}
Expand Down

0 comments on commit 45c1bf3

Please sign in to comment.