-
Notifications
You must be signed in to change notification settings - Fork 1
/
cart.lua
514 lines (452 loc) · 12.1 KB
/
cart.lua
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
local bit = require("numberlua").bit
local api = require("api")
local parse = require("parser/ParseLua")
local format = require("parser/FormatPico8")
local util = require("parser/Util")
local compression_map = {}
for entry in
("\n 0123456789abcdefghijklmnopqrstuvwxyz!#%(){}[]<>+=/*:;.,~_"):gmatch(".")
do
table.insert(compression_map, entry)
end
local function decompress(code)
local lua = ""
local mode = 0
local copy = nil
local i = 8
local codelen = bit.lshift(code:byte(5, 5), 8) + code:byte(6, 6)
log("codelen", codelen)
while #lua < codelen do
i = i + 1
local byte = string.byte(code, i, i)
if byte == nil then
error("reached end of code")
else
if mode == 1 then
lua = lua .. code:sub(i, i)
mode = 0
elseif mode == 2 then
-- copy from buffer
local offset = (copy - 0x3c) * 16 + bit.band(byte, 0xf)
local length = bit.rshift(byte, 4) + 2
offset = #lua - offset
local buffer = lua:sub(offset + 1, offset + length)
lua = lua .. buffer
mode = 0
elseif byte == 0x00 then
-- output next byte
mode = 1
elseif byte >= 0x01 and byte <= 0x3b then
-- output this byte from map
lua = lua .. compression_map[byte]
elseif byte >= 0x3c then
-- copy previous bytes
mode = 2
copy = byte
end
end
end
return lua
end
local cart = {}
function cart.load_p8(filename)
log("Loading", filename)
local lua = ""
pico8.quads = {}
pico8.spritesheet_data = love.image.newImageData(128, 128)
pico8.spritesheet_data:mapPixel(function() return 0, 0, 0, 1 end)
pico8.map = {}
for y = 0, 63 do
pico8.map[y] = {}
for x = 0, 127 do
pico8.map[y][x] = 0
end
end
pico8.spriteflags = {}
for i = 0, 255 do
pico8.spriteflags[i] = 0
end
pico8.sfx = {}
for i = 0, 63 do
pico8.sfx[i] = {
editor_mode = 0,
speed = 16,
loop_start = 0,
loop_end = 0,
}
for j = 0, 31 do
pico8.sfx[i][j] = { 0, 0, 0, 0 }
end
end
pico8.music = {}
for i = 0, 63 do
pico8.music[i] = {
loop = 0,
[0] = 1,
[1] = 2,
[2] = 3,
[3] = 4,
}
end
local fileheader = love.filesystem.read(filename, 8)
if fileheader == "\137PNG\r\n\26\n" then
local data = love.image.newImageData(filename)
if data:getWidth() ~= 160 or data:getHeight() ~= 205 then
error("Image is the wrong size")
end
local outX = 0
local outY = 0
local inbyte = 0
local lastbyte = nil
local mapY = 32
local mapX = 0
local version = nil
local compressed = false
local sprite = 0
for y = 0, 204 do
for x = 0, 159 do
local r, g, b, a = data:getPixel(x, y)
-- extract lowest bits
r = bit.band(r, 0x0003)
g = bit.band(g, 0x0003)
b = bit.band(b, 0x0003)
a = bit.band(a, 0x0003)
data:setPixel(
x,
y,
bit.lshift(r, 6)/255,
bit.lshift(g, 6)/255,
bit.lshift(b, 6)/255,
1
)
local byte = bit.lshift(a, 6) + bit.lshift(r, 4) + bit.lshift(g, 2) + b
local lo = bit.band(byte, 0x0f)
local hi = bit.rshift(byte, 4)
if inbyte < 0x2000 then
-- spritesheet
if outY >= 64 then
pico8.map[mapY][mapX] = byte
mapX = mapX + 1
if mapX == 128 then
mapX = 0
mapY = mapY + 1
end
end
pico8.spritesheet_data:setPixel(outX, outY, lo/15, 0, 0, 1)
outX = outX + 1
pico8.spritesheet_data:setPixel(outX, outY, hi/15, 0, 0, 1)
outX = outX + 1
if outX == 128 then
outY = outY + 1
outX = 0
if outY == 128 then
-- end of spritesheet, generate quads
pico8.spritesheet = love.graphics.newImage(pico8.spritesheet_data)
local spriteCounter = 0
for yy = 0, 15 do
for xx = 0, 15 do
pico8.quads[spriteCounter] = love.graphics.newQuad(
xx * 8,
yy * 8,
8,
8,
pico8.spritesheet:getDimensions()
)
spriteCounter = spriteCounter + 1
end
end
mapY = 0
mapX = 0
-- this variable marks that spritesheet_data has been changed, and the spritesheet needs to be reloaded accordingly
pico8.spritesheet_changed = false
end
end
elseif inbyte < 0x3000 then
-- map data
pico8.map[mapY][mapX] = byte
mapX = mapX + 1
if mapX == 128 then
mapX = 0
mapY = mapY + 1
end
elseif inbyte < 0x3100 then
-- sprite flags
pico8.spriteflags[sprite] = byte
sprite = sprite + 1
elseif inbyte < 0x3200 then
-- music
local _music = math.floor((inbyte - 0x3100) / 4)
pico8.music[_music][inbyte % 4] = bit.band(byte, 0x7F)
pico8.music[_music].loop = bit.bor(
bit.rshift(bit.band(byte, 0x80), 7 - inbyte % 4),
pico8.music[_music].loop
)
elseif inbyte < 0x4300 then
-- sfx
local _sfx = math.floor((inbyte - 0x3200) / 68)
local step = (inbyte - 0x3200) % 68
if step < 64 and inbyte % 2 == 1 then
local note = bit.lshift(byte, 8) + lastbyte
pico8.sfx[_sfx][(step - 1) / 2] = {
bit.band(note, 0x3f),
bit.rshift(bit.band(note, 0x1c0), 6),
bit.rshift(bit.band(note, 0xe00), 9),
bit.rshift(bit.band(note, 0x7000), 12),
}
elseif step == 64 then
pico8.sfx[_sfx].editor_mode = byte
elseif step == 65 then
pico8.sfx[_sfx].speed = byte
elseif step == 66 then
pico8.sfx[_sfx].loop_start = byte
elseif step == 67 then
pico8.sfx[_sfx].loop_end = byte
end
elseif inbyte < 0x8000 then
-- code, possibly compressed
if inbyte == 0x4300 then
compressed = (byte == 58)
end
lua = lua .. string.char(byte)
elseif inbyte == 0x8000 then
version = byte
end
lastbyte = byte
inbyte = inbyte + 1
end
end
-- decompress code
log("version", version)
if version > 8 then
api.print(string.format("unknown file version %d", version), 3)
end
if compressed then
lua = decompress(lua)
else
lua = lua:match("(.-)%f[%z]")
end
else
local data, size = love.filesystem.read(filename)
if not data or size == 0 then
error(string.format("Unable to open: %s", filename))
end
-- strip carriage returns pico-8 style
data = data:gsub("\r\n", "\n")
-- tack on a fake header
if data:sub(-1) ~= "\n" then
data = data .. "\n"
end
data = data .. "__eof__\n"
-- check for header and version
local header = "pico%-8 cartridge"
local header_len = #header - 1 -- subtract escape char
local version_header = "\nversion"
local header_start = data:find(header)
if header_start == nil then
error("invalid cart (missing header)")
end
local header_end = data:find(version_header, header_start + header_len)
if header_end == nil then
error("invalid cart (missing header-version)")
end
if header_end ~= data:find("\n", header_start + header_len) then
error("invalid cart (malformed header)")
end
header_end = header_end + #version_header
local next_line = data:find("\n", header_end)
if next_line == nil then
error("invalid cart (incomplete header)")
end
local version_str = data:sub(header_end, next_line - 1)
local version = tonumber(version_str)
log("version", version)
-- extract the lua
lua = data:match("\n__lua__.-\n(.-)\n__[%w]+__") or ""
-- load the sprites into an imagedata
-- generate a quad for each sprite index
local gfxdata = data:match("\n__gfx__.-\n(.-\n)\n-__")
if gfxdata then
local row = 0
for line in gfxdata:gmatch("(.-)\n") do
local col = 0
for v in line:gmatch(".") do
v = tonumber(v, 16)
pico8.spritesheet_data:setPixel(col, row, v / 15, 0, 0, 1)
col = col + 1
if col == 128 then
break
end
end
row = row + 1
if row == 128 then
break
end
end
end
local shared = 0
if version > 3 then
local tx, ty = 0, 32
for sy = 64, 127 do
for sx = 0, 127, 2 do
-- get the two pixel values and merge them
local lo = pico8.spritesheet_data:getPixel(sx, sy) * 15
local hi = pico8.spritesheet_data:getPixel(sx + 1, sy) * 15
local v = bit.bor(bit.lshift(hi, 4), lo)
pico8.map[ty][tx] = v
shared = shared + 1
tx = tx + 1
if tx == 128 then
tx = 0
ty = ty + 1
end
end
end
end
for y = 0, 15 do
for x = 0, 15 do
pico8.quads[y * 16 + x] =
love.graphics.newQuad(8 * x, 8 * y, 8, 8, 128, 128)
end
end
pico8.spritesheet = love.graphics.newImage(pico8.spritesheet_data)
-- this variable marks that spritesheet_data has been changed, and the spritesheet needs to be reloaded accordingly
pico8.spritesheet_changed = false
-- load the sprite flags
local gffdata = data:match("\n__gff__.-\n(.-\n)\n-__")
if gffdata then
local sprite = 0
local gffpat = (version <= 2 and "." or "..")
for line in gffdata:gmatch("(.-)\n") do
local col = 0
for v in line:gmatch(gffpat) do
v = tonumber(v, 16)
pico8.spriteflags[sprite + col] = v
col = col + 1
if col == 128 then
break
end
end
sprite = sprite + 128
if sprite == 256 then
break
end
end
end
-- convert the tile data to a table
local mapdata = data:match("\n__map__.-\n(.-\n)\n-__")
if mapdata then
local row = 0
local tiles = 0
for line in mapdata:gmatch("(.-)\n") do
local col = 0
for v in line:gmatch("..") do
v = tonumber(v, 16)
pico8.map[row][col] = v
col = col + 1
tiles = tiles + 1
if col == 128 then
break
end
end
row = row + 1
if row == 32 then
break
end
end
--assert(tiles + shared == 128 * 64, string.format("%d + %d != %d", tiles, shared, 128 * 64))
end
-- load sfx
local sfxdata = data:match("\n__sfx__.-\n(.-\n)\n-__")
if sfxdata then
local _sfx = 0
for line in sfxdata:gmatch("(.-)\n") do
pico8.sfx[_sfx].editor_mode = tonumber(line:sub(1, 2), 16)
pico8.sfx[_sfx].speed = tonumber(line:sub(3, 4), 16)
pico8.sfx[_sfx].loop_start = tonumber(line:sub(5, 6), 16)
pico8.sfx[_sfx].loop_end = tonumber(line:sub(7, 8), 16)
local step = 0
for i = 9, #line, 5 do
local v = line:sub(i, i + 4)
assert(#v == 5)
local note = tonumber(line:sub(i, i + 1), 16)
local instr = tonumber(line:sub(i + 2, i + 2), 16)
local vol = tonumber(line:sub(i + 3, i + 3), 16)
local fx = tonumber(line:sub(i + 4, i + 4), 16)
pico8.sfx[_sfx][step] = { note, instr, vol, fx }
step = step + 1
if step == 32 then
break
end
end
_sfx = _sfx + 1
if _sfx == 64 then
break
end
end
end
-- load music
local musicdata = data:match("\n__music__.-\n(.-\n)\n-__")
if musicdata then
local _music = 0
for line in musicdata:gmatch("(.-)\n") do
pico8.music[_music] = {
loop = tonumber(line:sub(1, 2), 16),
[0] = tonumber(line:sub(4, 5), 16),
[1] = tonumber(line:sub(6, 7), 16),
[2] = tonumber(line:sub(8, 9), 16),
[3] = tonumber(line:sub(10, 11), 16),
}
_music = _music + 1
if _music == 64 then
break
end
end
--assert(_music == 64, string.format("%d", _music))
end
end
lua = patch_lua(lua)
lua = lua .. "\n_picolove_end()"
log("finished loading cart", filename)
loaded_code = lua
return true
end
function patch_lua(lua)
--replace glyphs with respective ascii chars
-- very carefully replace these glyphs with the respective ascii chars
-- need to be careful because utf-8 and extended ascii are not compatible, and some of the glyphs are more than 1 char (even more than 1 utf8 char)
--
-- TODO: optimize this code
local gmatch_magic = util.lookupify{'(', ')', '.', '%', '+', '-', '*', '?', '[', '^', '$'}
local i = 1
while i<=#lua do
local c = lua:sub(i,i)
if string.byte(c) >= 128 then
for n, gl in ipairs(pico8_glyphs) do
local escaped_gl
if gmatch_magic[gl] then
escaped_gl = "%"..gl
else
escaped_gl = gl
end
if lua:sub(i):match("^"..escaped_gl) then
lua = lua:sub(1,i-1) .. lua:sub(i):gsub("^"..escaped_gl,string.char(n))
break
end
end
end
i=i+1
end
-- not strictly required, but should help improve performance
lua = "local _ENV = _ENV " .. lua
local status, ast =parse.ParseLua(lua)
if not status then
error(ast)
end
local status, patched = format(ast)
if not status then
error(patched)
end
-- print(patched)
return patched
end
return cart