-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart.lua
More file actions
294 lines (249 loc) · 6.7 KB
/
cart.lua
File metadata and controls
294 lines (249 loc) · 6.7 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
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
local api=require("api")
local compression_map={}
for entry in ("\n 0123456789abcdefghijklmnopqrstuvwxyz!#%(){}[]<>+=/*:;.,~_"):gmatch(".") do
table.insert(compression_map, entry)
end
local cart={}
function cart.load_p8(filename)
updateStatus('Loading cart')
local lua=""
pico8.quads={}
pico8.spritesheet_table={}
for i =0, 127 do
pico8.spritesheet_table[i] = {}
for j = 0, 127 do
pico8.spritesheet_table[i][j] = 0
end
end
pico8.spritesheet_pointsByColor={}
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
--no support for pngs yet
local f=love.filesystem.newFile(filename, 'r')
if not f and filename:sub(1, 5) == "game/" then
--if running in regular love, it won't be in the game directory
filename = filename:sub(5)
f=love.filesystem.newFile(filename, 'r')
end
if not f then
error(string.format("Unable to open: %s", filename))
end
local data, size=f:read()
f:close()
if not data then
error("invalid cart")
end
updateStatus('have valid cart')
-- strip carriage returns pico-8 style
data=data:gsub("\r.", "\n")
-- tack on a fake header
if data:sub(-1) ~= "\n" then
data=data.."\n"
end
data=data.."__eof__\n"
-- check for header and vesion
local header="pico-8 cartridge // http://www.pico-8.com\nversion "
local start=data:find("pico%-8 cartridge // http://www.pico%-8%.com\nversion ")
if start==nil then
error("invalid cart")
end
local next_line=data:find("\n", start+#header)
local version_str=data:sub(start+#header, next_line-1)
local version=tonumber(version_str)
updateStatus("version " .. version)
-- extract the lua
lua=data:match("\n__lua__.-\n(.-)\n__") or ""
--get sprite sheet data and store in table
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_table[col][row] = v
local point = {col, row}
if pico8.spritesheet_pointsByColor[v] == nil then
pico8.spritesheet_pointsByColor[v] = {}
end
pico8.spritesheet_pointsByColor[v][#pico8.spritesheet_pointsByColor[v]+1]=point
col=col+1
if col==128 then break end
end
row=row+1
if row==128 then break end
end
end
--convert spritesheet table into canvas
pico8.spritesheet_data=getSpritesheetCanvas()
love.graphics.setColor(1, 1, 1, 1)
love.graphics.setCanvas()
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_table[sx][sy]
local hi=pico8.spritesheet_table[sx+1][sy]
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
local os = love.system.getOS()
local offset = 0;
if os ~= "3DS" and os ~= "Horizon" then
offset = -1
end
-- generate a quad for each sprite index
for y=0, 15 do
for x=0, 15 do
pico8.quads[y*16+x]=love.graphics.newQuad(8*x + offset, 8*y + offset, 8, 8, 128, 128)
end
end
-- 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
end
--store data in cart section of pico8 for reload if necessary
pico8.cartrom.spritesheet_table={}
for i =0, 127 do
pico8.cartrom.spritesheet_table[i] = {}
for j = 0, 127 do
pico8.cartrom.spritesheet_table[i][j] = pico8.spritesheet_table[i][j]
end
end
pico8.cartrom.map={}
for y=0, 63 do
pico8.cartrom.map[y]={}
for x=0, 127 do
pico8.cartrom.map[y][x]=pico8.map[y][x]
end
end
pico8.cartrom.spriteflags={}
for i=0, 255 do
pico8.cartrom.spriteflags[i]=pico8.spriteflags[i]
end
-- patch the lua
lua=lua:gsub("!=", "~=").."\n"
-- rewrite shorthand if statements eg. if (not b) i=1 j=2
lua=lua:gsub("if%s*(%b())%s*([^\n]*)\n", function(a, b)
local nl=a:find('\n', nil, true)
local th=b:find('%f[%w]then%f[%W]')
local an=b:find('%f[%w]and%f[%W]')
local o=b:find('%f[%w]or%f[%W]')
local ce=b:find('--', nil, true)
if not (nl or th or an or o) then
if ce then
local c, t=b:match("(.-)(%s-%-%-.*)")
return "if "..a:sub(2, -2).." then "..c.." end"..t.."\n"
else
return "if "..a:sub(2, -2).." then "..b.." end\n"
end
end
end)
-- rewrite assignment operators
lua=lua:gsub("(%S+)%s*([%+-%*/%%])=", "%1 = %1 %2 ")
-- convert binary literals to hex literals
lua=lua:gsub("([^%w_])0[bB]([01.]+)", function(a, b)
local p1, p2=b, ""
if b:find('.', nil, true) then
p1, p2=b:match("(.-)%.(.*)")
end
-- pad to 4 characters
p2=p2..string.rep("0", 3-((#p2-1)%4))
p1, p2=tonumber(p1, 2), tonumber(p2, 2)
if p1 and p2 then
return string.format("%s0x%x.%x", a, p1, p2)
end
end)
local cart_env={}
for k, v in pairs(api) do
cart_env[k]=v
end
cart_env.lua = lua
cart_env._ENV=cart_env -- Lua 5.2 compatibility hack
updateStatus('load patched lua')
local ok, f, e=pcall(loadstring, lua, "@"..filename)
if not ok or f==nil then
local ln=1
lua="1:"..lua:gsub("\n", function(a) ln=ln+1 return "\n"..ln..":" end)
--updateStatus('=======8<========')
--updateStatus(lua)
--write the lua we're actually using for debug purposes
local name = "parsed.lua"
local myFile = love.filesystem.newFile(name, "w")
myFile:write(lua)
myFile:flush()
myFile:close()
--updateStatus('=======>8========')
--force show debug info if the cart doesn't load
toggleShowDebugInfo(true)
updateStatus("Error loading lua (writing parsed lua to disk): "..tostring(e),0)
else
local result
updateStatus('pcalling patched lua')
setfenv(f, cart_env)
love.graphics.setCanvas(pico8.screen)
love.graphics.clear()
love.graphics.origin()
restore_clip()
ok, result=pcall(f)
if not ok then
updateStatus("Error running lua: "..tostring(result))
else
updateStatus("lua completed")
end
end
updateStatus("finished loading cart", filename)
love.graphics.setCanvas()
return cart_env
end
return cart