Skip to content

Commit

Permalink
feat: improve support for Buffer.buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed Dec 24, 2024
1 parent 4301b8e commit 9c20f70
Show file tree
Hide file tree
Showing 48 changed files with 56 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/amb/AMB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type ArxAMB = {
}

export class AMB {
static load(decompressedFile: ArrayBuffer): ArxAMB {
static load(decompressedFile: ArrayBufferLike): ArxAMB {
const file = new BinaryIO(decompressedFile)

const { numberOfTracks, isNewerVersion } = AmbHeader.readFrom(file)
Expand Down
2 changes: 1 addition & 1 deletion src/amb/AmbHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class AmbHeader {
/**
* @see https://github.com/arx/ArxLibertatis/blob/1.2.1/src/audio/Ambiance.cpp#L627
*/
static readFrom(binary: BinaryIO): ArxAmbHeader {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxAmbHeader {
binary.readString(4) // identifier - always "GAMB"

const version = binary.readUint32()
Expand Down
2 changes: 1 addition & 1 deletion src/amb/Key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Key {
/**
* @see https://github.com/arx/ArxLibertatis/blob/1.2.1/src/audio/Ambiance.cpp#L187
*/
static readFrom(binary: BinaryIO): ArxKey {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxKey {
binary.readUint32() // flags - always 0

return {
Expand Down
2 changes: 1 addition & 1 deletion src/amb/Setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Setting {
/**
* @see https://github.com/arx/ArxLibertatis/blob/1.2.1/src/audio/Ambiance.cpp#L107
*/
static readFrom(binary: BinaryIO): ArxSetting {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxSetting {
return {
min: binary.readFloat32(),
max: binary.readFloat32(),
Expand Down
2 changes: 1 addition & 1 deletion src/amb/Track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class Track {
/**
* @see https://github.com/arx/ArxLibertatis/blob/1.2.1/src/audio/Ambiance.cpp#L531
*/
static readFrom(binary: BinaryIO, isNewerVersion: boolean): ArxTrack {
static readFrom(binary: BinaryIO<ArrayBufferLike>, isNewerVersion: boolean): ArxTrack {
const filename = Track.toRelativePath(binary.readString())

if (isNewerVersion) {
Expand Down
16 changes: 9 additions & 7 deletions src/common/BinaryIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { decodeText, encodeText, repeat } from '@common/helpers.js'
import { LITTLE_ENDIAN, TRUNCATE_ZERO_BYTES, KEEP_ZERO_BYTES, BYTE_OF_AN_UNKNOWN_CHAR } from '@common/constants.js'
import { type ArxQuaternion, type ArxRotation, type ArxVector3 } from '@common/types.js'

export class BinaryIO extends DataView<ArrayBuffer> {
export class BinaryIO<
TArrayBuffer extends ArrayBufferLike & { BYTES_PER_ELEMENT?: never },
> extends DataView<TArrayBuffer> {
static sizeOfFloat32(): 4 {
return 4 as const
}
Expand Down Expand Up @@ -85,7 +87,7 @@ export class BinaryIO extends DataView<ArrayBuffer> {

public position: number // TODO: make this private - this needs to be public because of TEA

constructor(buffer: ArrayBuffer, byteOffset?: number, byteLength?: number) {
constructor(buffer: TArrayBuffer, byteOffset?: number, byteLength?: number) {
super(buffer, byteOffset, byteLength)
this.position = 0
}
Expand Down Expand Up @@ -244,13 +246,13 @@ export class BinaryIO extends DataView<ArrayBuffer> {
this.position = this.position + BinaryIO.sizeOfUint8()
}

writeUint8Array(values: number[] | ArrayBuffer): void {
if (values instanceof ArrayBuffer) {
new Uint8Array(values).forEach((value) => {
writeUint8Array(values: number[] | ArrayBufferLike): void {
if (Array.isArray(values)) {
values.forEach((value) => {
this.writeUint8(value)
})
} else {
values.forEach((value) => {
new Uint8Array(values).forEach((value) => {
this.writeUint8(value)
})
}
Expand Down Expand Up @@ -379,7 +381,7 @@ export class BinaryIO extends DataView<ArrayBuffer> {
this.writeFloat32Array([w, x, y, z])
}

writeBuffer(buffer: ArrayBuffer): void {
writeBuffer(buffer: ArrayBufferLike): void {
this.writeUint8Array(buffer)
}
}
2 changes: 1 addition & 1 deletion src/common/Color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type ArxColor = {
}

export class Color {
static readFrom(binary: BinaryIO, mode: ColorMode): ArxColor {
static readFrom(binary: BinaryIO<ArrayBufferLike>, mode: ColorMode): ArxColor {
if (mode === 'bgra') {
const [b, g, r, a] = binary.readUint8Array(4)
return { r, g, b, a: a / 255 }
Expand Down
2 changes: 1 addition & 1 deletion src/dlf/DLF.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type ArxDLF = {
}

export class DLF {
static load(decompressedFile: ArrayBuffer): ArxDLF {
static load(decompressedFile: ArrayBufferLike): ArxDLF {
const file = new BinaryIO(decompressedFile)

const { numberOfInteractiveObjects, numberOfFogs, numberOfZonesAndPaths, ...header } = DlfHeader.readFrom(file)
Expand Down
2 changes: 1 addition & 1 deletion src/dlf/DlfHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type ArxDlfHeader = {
}

export class DlfHeader {
static readFrom(binary: BinaryIO): ArxDlfHeader {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxDlfHeader {
binary.readFloat32() // version - always 1.44
binary.readString(16) // identifier - always "DANAE_FILE"

Expand Down
2 changes: 1 addition & 1 deletion src/dlf/Fog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type ArxFog = {
}

export class Fog {
static readFrom(binary: BinaryIO): ArxFog {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxFog {
const dataBlock1 = {
pos: binary.readVector3(),
color: Color.readFrom(binary, 'rgb'),
Expand Down
2 changes: 1 addition & 1 deletion src/dlf/InteactiveObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type ArxInteractiveObject = {
}

export class InteractiveObject {
static readFrom(binary: BinaryIO): ArxInteractiveObject {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxInteractiveObject {
const data: ArxInteractiveObject = {
name: InteractiveObject.toRelativePath(binary.readString(512)),
pos: binary.readVector3(),
Expand Down
2 changes: 1 addition & 1 deletion src/dlf/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type ArxScene = {
}

export class Scene {
static readFrom(binary: BinaryIO): ArxScene {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxScene {
const levelIdx = Scene.pathToLevelIdx(binary.readString(512))

binary.readInt32Array(16) // pad - ?
Expand Down
2 changes: 1 addition & 1 deletion src/dlf/ZoneAndPathHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export type ArxZoneAndPathHeader = {
}

export class ZoneAndPathHeader {
static readFrom(binary: BinaryIO): ArxZoneAndPathHeader {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxZoneAndPathHeader {
const name = binary.readString(64)

binary.readInt16() // idx - always 0
Expand Down
2 changes: 1 addition & 1 deletion src/dlf/ZoneAndPathPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type ArxZoneAndPathPoint = {
}

export class ZoneAndPathPoint {
static readFrom(binary: BinaryIO, pos: ArxVector3): ArxZoneAndPathPoint {
static readFrom(binary: BinaryIO<ArrayBufferLike>, pos: ArxVector3): ArxZoneAndPathPoint {
const rpos = binary.readVector3()
const data = {
pos: {
Expand Down
2 changes: 1 addition & 1 deletion src/ftl/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type ArxAction = {
}

export class Action {
static readFrom(binary: BinaryIO): ArxAction {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxAction {
return {
name: binary.readString(256),
vertexIdx: binary.readInt32(),
Expand Down
2 changes: 1 addition & 1 deletion src/ftl/FTL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type ArxFTL = {
}

export class FTL {
static load(decompressedFile: ArrayBuffer): ArxFTL {
static load(decompressedFile: ArrayBufferLike): ArxFTL {
const file = new BinaryIO(decompressedFile)

const {
Expand Down
2 changes: 1 addition & 1 deletion src/ftl/Face.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type ArxFace = {
}

export class Face {
static readFrom(binary: BinaryIO): ArxFace {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxFace {
const faceType = binary.readInt32()

binary.readUint32Array(3) // rgb - always [0, 0, 0]
Expand Down
2 changes: 1 addition & 1 deletion src/ftl/FtlHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type ArxFtlHeader = {
}

export class FtlHeader {
static readFrom(binary: BinaryIO): ArxFtlHeader {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxFtlHeader {
binary.readString(4) // identifier - always "FTL\0"
binary.readFloat32() // version - always 0.832_570_016_384_124_8
binary.readString(512) // checksum - contains data, but ArxLibertatis doesn't read it
Expand Down
2 changes: 1 addition & 1 deletion src/ftl/FtlTextureContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type ArxFtlTextureContainer = {
}

export class FtlTextureContainer {
static readFrom(binary: BinaryIO): ArxFtlTextureContainer {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxFtlTextureContainer {
return {
filename: TextureContainer.toRelativePath(binary.readString(256)),
}
Expand Down
2 changes: 1 addition & 1 deletion src/ftl/FtlVertex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type ArxFtlVertex = {
}

export class FtlVertex {
static readFrom(binary: BinaryIO): ArxFtlVertex {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxFtlVertex {
binary.readUint8Array(32) // unused

return {
Expand Down
2 changes: 1 addition & 1 deletion src/ftl/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type ArxGroup = {
}

export class Group {
static readFrom(binary: BinaryIO): ArxGroup & { numberOfIndices: number } {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxGroup & { numberOfIndices: number } {
const data = {
name: binary.readString(256),
origin: binary.readUint32(),
Expand Down
2 changes: 1 addition & 1 deletion src/ftl/Selections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type ArxSelection = {
}

export class Selection {
static readFrom(binary: BinaryIO): ArxSelection & { numberOfSelected: number } {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxSelection & { numberOfSelected: number } {
const data = {
name: binary.readString(64),
numberOfSelected: binary.readInt32(),
Expand Down
2 changes: 1 addition & 1 deletion src/fts/Anchor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type ArxAnchor = {
}

export class Anchor {
static readFrom(binary: BinaryIO): ArxAnchor {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxAnchor {
const { numberOfLinkedAnchors, ...anchorData } = AnchorData.readFrom(binary)

return {
Expand Down
2 changes: 1 addition & 1 deletion src/fts/AnchorData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type ArxAnchorData = {
}

export class AnchorData {
static readFrom(binary: BinaryIO): ArxAnchorData {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxAnchorData {
const data: ArxAnchorData = {
pos: binary.readVector3(),
radius: binary.readFloat32(),
Expand Down
2 changes: 1 addition & 1 deletion src/fts/Cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type ArxCell = {
}

export class Cell {
static readFrom(binary: BinaryIO): ArxCell {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxCell {
const { numberOfPolygons, numberOfAnchors } = SceneInfo.readFrom(binary)

const data: ArxCell = {
Expand Down
2 changes: 1 addition & 1 deletion src/fts/EPData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type ArxEPData = {
}

export class EPData {
static readFrom(binary: BinaryIO): ArxEPData {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxEPData {
const [px, py, idx] = binary.readInt16Array(4)
return { cellX: px, cellY: py, polygonIdx: idx }
}
Expand Down
2 changes: 1 addition & 1 deletion src/fts/FTS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export type ArxFTS = {
const IS_AN_UNCOMPRESSED_FTS = 0

export class FTS {
static load(decompressedFile: ArrayBuffer): ArxFTS {
static load(decompressedFile: ArrayBufferLike): ArxFTS {
const file = new BinaryIO(decompressedFile)

const { numberOfUniqueHeaders, ...header } = FtsHeader.readFrom(file)
Expand Down
2 changes: 1 addition & 1 deletion src/fts/FtsHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type ArxFtsHeader = {
}

export class FtsHeader {
static readFrom(binary: BinaryIO): ArxFtsHeader {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxFtsHeader {
const path = binary.readString(256)

const data: ArxFtsHeader = {
Expand Down
2 changes: 1 addition & 1 deletion src/fts/Polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export type ArxPolygon = {
}

export class Polygon {
static readFrom(binary: BinaryIO): ArxPolygon {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxPolygon {
return {
vertices: times(() => {
return Vertex.readFrom(binary)
Expand Down
2 changes: 1 addition & 1 deletion src/fts/Portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type ArxPortal = {
}

export class Portal {
static readFrom(binary: BinaryIO): ArxPortal {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxPortal {
return {
polygon: PortalPolygon.readFrom(binary),
room1: binary.readInt32(), // facing normal
Expand Down
2 changes: 1 addition & 1 deletion src/fts/PortalPolygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export type ArxPortalPolygon = {
}

export class PortalPolygon {
static readFrom(binary: BinaryIO): ArxPortalPolygon {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxPortalPolygon {
binary.readInt32() // type - in 2 occasions on level 2 it's 0, all other cases it's 64 (ArxPolygonFlags.Quad)

const dataBlock = {
Expand Down
2 changes: 1 addition & 1 deletion src/fts/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type ArxRoom = {
}

export class Room {
static readFrom(binary: BinaryIO): ArxRoom {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxRoom {
const { numberOfPortals, numberOfPolygons } = RoomData.readFrom(binary)

return {
Expand Down
2 changes: 1 addition & 1 deletion src/fts/RoomData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type ArxRoomData = {
}

export class RoomData {
static readFrom(binary: BinaryIO): ArxRoomData {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxRoomData {
const data: ArxRoomData = {
numberOfPortals: binary.readInt32(),
numberOfPolygons: binary.readInt32(),
Expand Down
2 changes: 1 addition & 1 deletion src/fts/RoomDistance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type ArxRoomDistance = {
}

export class RoomDistance {
static readFrom(binary: BinaryIO): ArxRoomDistance {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxRoomDistance {
return {
distance: binary.readFloat32(), // -1 means use truedist
startPosition: binary.readVector3(),
Expand Down
2 changes: 1 addition & 1 deletion src/fts/SceneHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type ArxSceneHeader = {
}

export class SceneHeader {
static readFrom(binary: BinaryIO): ArxSceneHeader {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxSceneHeader {
binary.readFloat32() // version - always 0.14100000262260437
binary.readInt32() // sizeX - always 160
binary.readInt32() // sizeZ - always 160
Expand Down
2 changes: 1 addition & 1 deletion src/fts/SceneInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type ArxSceneInfo = {
}

export class SceneInfo {
static readFrom(binary: BinaryIO): ArxSceneInfo {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxSceneInfo {
return {
numberOfPolygons: binary.readInt32(),
numberOfAnchors: binary.readInt32(),
Expand Down
2 changes: 1 addition & 1 deletion src/fts/TextureContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type ArxTextureContainer = {
}

export class TextureContainer {
static readFrom(binary: BinaryIO): ArxTextureContainer {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxTextureContainer {
const id = binary.readInt32()

binary.readInt32() // temp - always 0
Expand Down
2 changes: 1 addition & 1 deletion src/fts/TextureVertex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export type ArxTextureVertex = {
}

export class TextureVertex {
static readFrom(binary: BinaryIO): ArxTextureVertex {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxTextureVertex {
const data = {
pos: binary.readVector3(),
rhw: binary.readFloat32(),
Expand Down
2 changes: 1 addition & 1 deletion src/fts/UniqueHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type ArxUniqueHeader = {
}

export class UniqueHeader {
static readFrom(binary: BinaryIO): ArxUniqueHeader {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxUniqueHeader {
return {
path: binary.readString(256),
check: binary.readUint8Array(512),
Expand Down
2 changes: 1 addition & 1 deletion src/fts/Vertex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type ArxVertex = {
}

export class Vertex {
static readFrom(binary: BinaryIO): ArxVertex {
static readFrom(binary: BinaryIO<ArrayBufferLike>): ArxVertex {
// y before x is not a typo!
const [y, x, z, u, v] = binary.readFloat32Array(5)
return { x, y, z, u, v }
Expand Down
Loading

0 comments on commit 9c20f70

Please sign in to comment.