-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore.js
198 lines (174 loc) · 5.22 KB
/
store.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const fs = require('fs')
const isBmp = require('is-bmp')
const DRS = require('genie-drs')
const Palette = require('jascpal')
const SLP = require('genie-slp')
const SCX = require('genie-scx')
const defaultPalette = fs.readFileSync(require.resolve('./default-palette.pal'), 'utf8')
module.exports = (state, emitter) => {
state.drs = null
state.viewing = null
state.palette = Palette(defaultPalette)
state.palette.id = 50500
state.slpPlayer = 1
emitter.on('pushState', (route) => {
window.location.hash = route
})
window.addEventListener('hashchange', (event) => {
if (/#palette$/.test(event.newURL)) {
return emitter.emit('viewPalette', state.palette)
}
const match = /#([a-z]+)\/(\d+)$/.exec(event.newURL)
if (match) {
return emitter.emit('view', parseInt(match[2], 10))
}
state.viewing = null
state.fileBuffer = null
state.fileType = null
state.fileData = null
state.fileURL = null
})
emitter.on('drsFile', (drs) => {
state.file = drs
state.drs = DRS(drs)
state.tableStates = Object.create(null)
state.drs.read(() => {
emitter.emit('render')
})
})
emitter.on('usePalette', (palette) => {
state.palette = palette
emitter.emit('render')
})
emitter.on('slpFrame', (id) => {
if (!(state.fileData instanceof SLP)) {
return
}
state.currentSlpFrame = id
emitter.emit('render')
})
emitter.on('slpPlayer', (player) => {
state.slpPlayer = player
emitter.emit('render')
})
emitter.on('collapseDrsTable', ({ table, value }) => {
state.tableStates[table] = value
emitter.emit('render')
})
emitter.on('view', (id) => {
const file = state.drs.getFile(id)
if (state.viewing === file) {
return
}
state.viewing = file
// Reset
state.fileBuffer = null
state.fileType = null
state.fileData = null
state.fileURL = null
state.drs.readFile(id, (err, buffer) => {
if (err) throw err
if (state.viewing !== file) return
state.fileBuffer = buffer
if (file.type === 'bina') {
if (isBmp(buffer)) {
state.fileType = 'bmp'
const blob = new Blob([buffer.buffer], { type: 'image/bmp' })
state.fileData = URL.createObjectURL(blob)
state.fileURL = state.fileData
} else if (isPalette(buffer)) {
state.fileType = 'palette'
state.fileData = Palette(buffer)
state.fileData.id = id
} else if (isInterfaceDescription(buffer)) {
state.fileType = 'interface'
} else if (isRandomMapScript(buffer)) {
state.fileType = 'rms'
} else if (isAIScript(buffer)) {
state.fileType = 'ai'
} else if (isSCX(buffer)) {
state.fileType = 'scx'
SCX(buffer).parse((err, scx) => {
if (err) throw err
state.fileData = scx
const blob = new Blob([buffer.buffer], { type: 'application/octet-stream' })
state.fileURL = URL.createObjectURL(blob)
emitter.emit('render')
})
} else {
state.fileType = 'text'
}
if (state.fileType !== 'bmp') {
const blob = new Blob([buffer.buffer], { type: 'text/plain' })
state.fileURL = URL.createObjectURL(blob)
}
} else if (file.type === 'slp ') {
state.fileType = 'slp'
const blob = new Blob([buffer.buffer], { type: 'application/octet-stream' })
state.fileURL = URL.createObjectURL(blob)
state.fileData = SLP(buffer)
state.fileData.parseHeader()
state.currentSlpFrame = 0
} else if (file.type === 'wav ') {
state.fileType = 'wav'
const blob = new Blob([buffer.buffer], { type: 'audio/wav' })
state.fileURL = URL.createObjectURL(blob)
state.fileData = new Audio(state.fileURL)
}
emitter.emit('render')
})
emitter.emit('render')
})
emitter.on('viewPalette', (palette) => {
const source = palette.toString()
state.viewing = {
id: palette.id,
type: 'bina',
size: source.length,
offset: -1
}
const blob = new Blob([source], { type: 'text/plain' })
state.fileURL = URL.createObjectURL(blob)
state.fileType = 'palette'
state.fileBuffer = Buffer.from(source, 'ascii')
state.fileData = palette
emitter.emit('render')
})
}
function isPalette (buffer) {
return buffer.toString('ascii', 0, 8) === 'JASC-PAL'
}
function isInterfaceDescription (buffer) {
if (buffer.indexOf('background1_files') !== -1) {
return true
}
return false
}
function isRandomMapScript (buffer) {
if (buffer.indexOf('<TERRAIN_GENERATION>') !== -1) {
return true
}
if (buffer.indexOf('#const ') !== -1) {
return true
}
return false
}
function isAIScript (buffer) {
if (buffer.indexOf('(defrule') !== -1) {
return true
}
if (buffer.indexOf('(defconst') !== -1) {
return true
}
if (buffer.indexOf('#load-if-defined') !== -1) {
return true
}
return false
}
function isSCX (buffer) {
const version = buffer.toString('ascii', 0, 8)
const headerLength = buffer.readUInt32LE(4)
const instructionsLength = buffer.readUInt32LE(16)
return isFinite(parseFloat(version)) &&
headerLength === instructionsLength + 20
}