-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
99 lines (83 loc) · 2.4 KB
/
index.js
File metadata and controls
99 lines (83 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict'
const fs = require('fs')
const path = require('path')
const tileReader = require('./lib/tileReader')
main(process.argv.slice(2))
const SECTION_NAME_SIZE = 4
function main (args=[]) {
const fileName = args.shift()
const DATA_FILE_PATH = path.join(process.cwd(), fileName||'YODESK.DTA')
fs.open(DATA_FILE_PATH, 'r', (err, fd) => {
if (err) {
if (err.code === 'ENOENT') {
console.error(`${DATA_FILE_PATH} does not exist`)
return
}
throw err
}
let keepReading = true
const buffer = fs.readFileSync(fd)
let offset = 0
while (keepReading) {
const section = buffer.toString('ascii',offset,offset+SECTION_NAME_SIZE)
offset += SECTION_NAME_SIZE
console.log('section',section, `0x${offset.toString(16)}`)
switch (section) {
case 'VERS':
offset = versionReader(buffer, offset)
break
case 'STUP':
case 'SNDS':
case 'PUZ2':
case 'CHAR':
case 'CHWP':
case 'CAUX':
case 'TNAM':
offset = genericSectionReader(buffer, offset)
break
case 'TILE':
offset = tileReader(buffer, offset)
break
case 'ZONE':
offset = zoneReader(buffer, offset)
break
case 'ENDF':
keepReading = false
break
default:
throw new Error(`Unknown section: ${section}, offset: ${offset.toString(16)}`)
}
}
})
}
function versionReader (buffer, offset) {
const buf = buffer.slice(offset, offset+4)
console.log('version buffer',buf)
const version = buf.swap16().readUInt32LE()
console.log('version',version)
return offset + 4
}
function genericSectionReader (buffer, offset) {
const sectionLength = buffer.readUInt32LE(offset)
offset += 4
console.log(' sectionLength',`0x${sectionLength.toString(16)}`)
console.log(' sectionData',buffer.slice(offset,offset+20))
return offset + sectionLength
}
function zoneReader (buffer, offset) {
const count = buffer.readUInt16LE(offset)
offset += 2
console.log(' zone count:',count)
for (let i = 0; i < count; i++) {
// unknown
offset += 2
// zoneLength
const zoneLength = buffer.readUInt32LE(offset)
offset += 4
// console.log(' zoneLength',zoneLength)
// zoneData
// console.log(' zoneData',buffer.slice(offset,20))
offset += zoneLength
}
return offset
}