forked from gvx/Lady
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lady.lua
551 lines (525 loc) · 19.5 KB
/
lady.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
local pairs, ipairs, tostring, type, getmetatable, setmetatable, concat, sort, dump, floor = pairs, ipairs, tostring, type, getmetatable, setmetatable, table.concat, table.sort, string.dump, math.floor
local weakkeys = {__mode = 'k'}
local weakvalues = {__mode = 'v'}
local M = {
_VERSION = 'Lady 0.1',
_DESCRIPTION = 'Easy saving and loading for LÖVE games',
_URL = 'http://github.com/gvx/Lady',
_LICENSE = 'MIT License',
}
local registered_things_by_name = {}
local registered_things_by_value = {}
local joint_bodies = setmetatable({}, weakkeys)
local body_joints = setmetatable({}, weakkeys)
local kw = {['and'] = true, ['break'] = true, ['do'] = true, ['else'] = true,
['elseif'] = true, ['end'] = true, ['false'] = true, ['for'] = true,
['function'] = true, ['goto'] = true, ['if'] = true, ['in'] = true,
['local'] = true, ['nil'] = true, ['not'] = true, ['or'] = true,
['repeat'] = true, ['return'] = true, ['then'] = true, ['true'] = true,
['until'] = true, ['while'] = true}
local function valid_identifier(s)
return s:match '^[_%a][_%w]*$' and not kw[s]
end
local function valid_classname(s)
return s:match '^%a[_%w]*$' and not kw[s]
end
function M.register_class(c, name)
name = name or c.__name__ or c.name
assert(type(name) == 'string', 'class must be given a string name')
assert(valid_classname(name), 'name must be a valid identifier that doesn\'t start with an underscore')
assert(registered_things_by_name[name] == nil, ('class with the name %s has already been registered'):format(name))
assert(registered_things_by_value[c] == nil, 'class has already been registered')
registered_things_by_name[name] = c
registered_things_by_value[c] = name
return c
end
function M.register_resource(r, name)
assert(type(name) == 'string', 'resource must be given a string name')
assert(valid_identifier(name), 'name must be a valid identifier')
assert(registered_things_by_name['_R' .. name] == nil, ('resource with the name %s has already been registered'):format(name))
assert(registered_things_by_value[r] == nil, 'resource has already been registered')
name = '_R' .. name
registered_things_by_name[name] = r
registered_things_by_value[r] = name
return r
end
function M.register_resource_table(rt, tname)
tname = tname and tname .. '_' or ''
for k, v in pairs(rt) do
M.register_resource(v, tname .. k)
end
return rt
end
local function add_body_joint(body, joint)
if not body_joints[body] then
body_joints[body] = {}
end
body_joints[body][#body_joints[body] + 1] = joint
end
if love.physics then
local newDistanceJoint = love.physics.newDistanceJoint
function love.physics.newDistanceJoint(body1, body2, ...)
local joint = newDistanceJoint(body1, body2, ...)
joint_bodies[joint] = setmetatable({body1, body2}, weakvalues)
add_body_joint(body1, joint)
add_body_joint(body2, joint)
return joint
end
local newFrictionJoint = love.physics.newFrictionJoint
function love.physics.newFrictionJoint(body1, body2, ...)
local joint = newFrictionJoint(body1, body2, ...)
joint_bodies[joint] = setmetatable({body1, body2}, weakvalues)
add_body_joint(body1, joint)
add_body_joint(body2, joint)
return joint
end
local newGearJoint = love.physics.newGearJoint
function love.physics.newGearJoint(joint1, joint2, ...)
local joint = newGearJoint(joint1, joint2, ...)
joint_bodies[joint] = setmetatable({joint1, joint2}, weakvalues)
add_body_joint(joint1, joint)
add_body_joint(joint2, joint)
return joint
end
local newMouseJoint = love.physics.newMouseJoint
function love.physics.newMouseJoint(body, ...)
local joint = newMouseJoint(body, ...)
joint_bodies[joint] = setmetatable({body}, weakvalues)
add_body_joint(body, joint)
add_body_joint(body, joint)
return joint
end
local newPrismaticJoint = love.physics.newPrismaticJoint
function love.physics.newPrismaticJoint(body1, body2, ...)
local joint = newPrismaticJoint(body1, body2, ...)
joint_bodies[joint] = setmetatable({body1, body2}, weakvalues)
add_body_joint(body1, joint)
add_body_joint(body2, joint)
return joint
end
local newPulleyJoint = love.physics.newPulleyJoint
function love.physics.newPulleyJoint(body1, body2, ...)
local joint = newPulleyJoint(body1, body2, ...)
joint_bodies[joint] = setmetatable({body1, body2}, weakvalues)
add_body_joint(body1, joint)
add_body_joint(body2, joint)
return joint
end
local newRevoluteJoint = love.physics.newRevoluteJoint
function love.physics.newRevoluteJoint(body1, body2, ...)
local joint = newRevoluteJoint(body1, body2, ...)
joint_bodies[joint] = setmetatable({body1, body2}, weakvalues)
add_body_joint(body1, joint)
add_body_joint(body2, joint)
return joint
end
local newRopeJoint = love.physics.newRopeJoint
function love.physics.newRopeJoint(body1, body2, ...)
local joint = newRopeJoint(body1, body2, ...)
joint_bodies[joint] = setmetatable({body1, body2}, weakvalues)
add_body_joint(body1, joint)
add_body_joint(body2, joint)
return joint
end
local newWeldJoint = love.physics.newWeldJoint
function love.physics.newWeldJoint(body1, body2, ...)
local joint = newWeldJoint(body1, body2, ...)
joint_bodies[joint] = setmetatable({body1, body2}, weakvalues)
add_body_joint(body1, joint)
add_body_joint(body2, joint)
return joint
end
local newWheelJoint = love.physics.newWheelJoint
function love.physics.newWheelJoint(body1, body2, ...)
local joint = newWheelJoint(body1, body2, ...)
joint_bodies[joint] = setmetatable({body1, body2}, weakvalues)
add_body_joint(body1, joint)
add_body_joint(body2, joint)
return joint
end
end
local function getchr(c)
return "\\" .. c:byte()
end
local function make_safe(text)
return ("%q"):format(text):gsub('\n', 'n'):gsub("[\128-\255]", getchr)
end
local oddvals = {inf = '1/0', ['-inf'] = '-1/0', [tostring(0/0)] = '0/0'}
local userdata_constructor = {}
local function write(t, memo, rev_memo)
local ty = type(t)
if ty == 'number' or ty == 'boolean' or ty == 'nil' then
t = tostring(t)
return oddvals[t] or t
elseif ty == 'string' then
return make_safe(t)
elseif ty == 'table' or ty == 'function' or (ty == 'userdata' and userdata_constructor[t:type()] and not registered_things_by_value[t]) then
if not memo[t] then
local index = #rev_memo + 1
memo[t] = index
rev_memo[index] = t
end
return '_' .. memo[t]
elseif ty == 'userdata' then
if registered_things_by_value[t] then
return registered_things_by_value[t]
end
error("Trying to serialize unregistered userdata " .. t:type())
else
error("Trying to serialize unsupported type " .. ty)
end
end
local function write_key_value_pair(k, v, memo, rev_memo, name)
if type(k) == 'string' and valid_identifier(k) then
return (name and name .. '.' or '') .. k ..' = ' .. write(v, memo, rev_memo)
else
return (name or '') .. '[' .. write(k, memo, rev_memo) .. '] = ' .. write(v, memo, rev_memo)
end
end
-- fun fact: this function is not perfect
-- it has a few false positives sometimes
-- but no false negatives, so that's good
local function is_cyclic(memo, sub, super)
local m = memo[sub]
local p = memo[super]
return m and p and m < p
end
function userdata_constructor:World(srefs, memo, rev_memo)
local s = memo[self]
if self:getCallbacks() ~= nil then
srefs[#srefs + 1] = {s, ':', 'setCallbacks', self:getCallbacks()}
end
if self:getContactFilter() ~= nil then
srefs[#srefs + 1] = {s, ':', 'setContactFilter', self:getContactFilter()}
end
local x, y = self:getGravity( )
return x, y, self:isSleepingAllowed()
end
function userdata_constructor:Body(srefs, memo, rev_memo)
local s = memo[self]
srefs[#srefs + 1] = {s, ':', 'setSleepingAllowed', self:isSleepingAllowed()}
srefs[#srefs + 1] = {s, ':', 'setAngle', self:getAngle()}
srefs[#srefs + 1] = {s, ':', 'setAngularDamping', self:getAngularDamping()}
srefs[#srefs + 1] = {s, ':', 'setAngularVelocity', self:getAngularVelocity()}
srefs[#srefs + 1] = {s, ':', 'setGravityScale', self:getGravityScale()}
srefs[#srefs + 1] = {s, ':', 'setInertia', self:getInertia()}
srefs[#srefs + 1] = {s, ':', 'setLinearDamping', self:getLinearDamping()}
srefs[#srefs + 1] = {s, ':', 'setLinearVelocity', self:getLinearVelocity()}
srefs[#srefs + 1] = {s, ':', 'setMass', self:getMass()}
srefs[#srefs + 1] = {s, ':', 'setAwake', self:isAwake()}
srefs[#srefs + 1] = {s, ':', 'setBullet', self:isBullet()}
if body_joints[self] then
for i, joint in ipairs(body_joints[self]) do
if not memo[joint] then
local index = #rev_memo + 1
memo[joint] = index
rev_memo[index] = joint
end
end
end
local x, y = self:getPosition()
return self.getWorld and self:getWorld() or rev_memo[1], x, y, self:getType()
end
function userdata_constructor:ChainShape(srefs)
return false, self:getPoints()
end
function userdata_constructor:CircleShape(srefs)
local x, y = self:getPoint()
return x, y, self:getRadius()
end
function userdata_constructor:EdgeShape(srefs)
return self:getPoints()
end
function userdata_constructor:PolygonShape(srefs)
return self:getPoints()
end
function userdata_constructor:Fixture(srefs, memo, rev_memo)
local s = memo[self]
--the ones commented out are just replicating the data from get/setFilterData
--srefs[#srefs + 1] = {s, ':', 'setCategory', self:getCategory()}
srefs[#srefs + 1] = {s, ':', 'setFilterData', self:getFilterData()}
srefs[#srefs + 1] = {s, ':', 'setFriction', self:getFriction()}
--srefs[#srefs + 1] = {s, ':', 'setGroupIndex', self:getGroupIndex()}
--srefs[#srefs + 1] = {s, ':', 'setMask', self:getMask()}
srefs[#srefs + 1] = {s, ':', 'setRestitution', self:getRestitution()}
srefs[#srefs + 1] = {s, ':', 'setSensor', self:isSensor()}
if self:getUserData() ~= nil then
srefs[#srefs + 1] = {s, ':', 'setUserData', self:getUserData()}
end
return self:getBody(), self:getShape(), self:getDensity()
end
function userdata_constructor:DistanceJoint(srefs)
local s = memo[self]
srefs[#srefs + 1] = {s, ':', 'setDamping', self:getDamping()}
srefs[#srefs + 1] = {s, ':', 'setDampingRatio', self:getDampingRatio()}
srefs[#srefs + 1] = {s, ':', 'setFrequency', self:getFrequency()}
srefs[#srefs + 1] = {s, ':', 'setLength', self:getLength()}
local body1, body2 = unpack(joint_bodies)
local x1, y1, x2, y2 = self:getAnchors()
return body1, body2, x1, y1, x2, y2, self:getCollideConnected()
end
function userdata_constructor:FrictionJoint(srefs)
local s = memo[self]
srefs[#srefs + 1] = {s, ':', 'setMaxForce', self:getMaxForce()}
srefs[#srefs + 1] = {s, ':', 'setMaxTorque', self:getMaxTorque()}
local body1, body2 = unpack(joint_bodies)
local x1, y1 = self:getAnchors()
return body1, body2, x1, y1, self:getCollideConnected()
end
function userdata_constructor:GearJoint(srefs)
local joint1, joint2 = unpack(joint_bodies)
return joint1, joint2, self:getRatio(), self:getCollideConnected()
end
function userdata_constructor:MouseJoint(srefs)
local s = memo[self]
srefs[#srefs + 1] = {s, ':', 'setDampingRatio', self:getDampingRatio()}
srefs[#srefs + 1] = {s, ':', 'setFrequency', self:getFrequency()}
srefs[#srefs + 1] = {s, ':', 'setMaxForce', self:getMaxForce()}
local body = unpack(joint_bodies)
return body, self:getTarget()
end
function userdata_constructor:PrismaticJoint(srefs)
local s = memo[self]
srefs[#srefs + 1] = {s, ':', 'setLimits', self:getLimits()}
srefs[#srefs + 1] = {s, ':', 'setLimitsEnabled', self:hasLimitsEnabled()}
srefs[#srefs + 1] = {s, ':', 'setMaxMotorForce', self:getMaxMotorForce()}
srefs[#srefs + 1] = {s, ':', 'setMotorEnabled', self:isMotorEnabled()}
local body1, body2 = unpack(joint_bodies)
local x1, y1, x2, y2 = self:getAnchors()
return body1, body2, x1, y1, x2, y2, self:getCollideConnected()
end
function userdata_constructor:PulleyJoint(srefs)
local s = memo[self]
srefs[#srefs + 1] = {s, ':', 'setConstant', self:getConstant()}
srefs[#srefs + 1] = {s, ':', 'setMaxLengths', self:getMaxLengths()}
local body1, body2 = unpack(joint_bodies)
local gx1, gy1, gx2, gy2 = self:getGroundAnchors()
local x1, y1, x2, y2 = self:getAnchors()
return body1, body2, gx1, gy1, gx2, gy2, gx1, gy1, gx2, gy2, self:getRatio(), self:getCollideConnected()
end
function userdata_constructor:RevoluteJoint(srefs)
local s = memo[self]
srefs[#srefs + 1] = {s, ':', 'setLimits', self:getLimits()}
srefs[#srefs + 1] = {s, ':', 'setLimitsEnabled', self:hasLimitsEnabled()}
srefs[#srefs + 1] = {s, ':', 'setMaxMotorTorque', self:getMaxMotorTorque()}
srefs[#srefs + 1] = {s, ':', 'setMotorEnabled', self:isMotorEnabled()}
srefs[#srefs + 1] = {s, ':', 'setMotorSpeed', self:getMotorSpeed()}
local body1, body2 = unpack(joint_bodies)
local x1, y1 = self:getAnchors()
return body1, body2, x1, y1, self:getCollideConnected()
end
function userdata_constructor:RopeJoint(srefs)
local body1, body2 = unpack(joint_bodies)
local x1, y1, x2, y2 = self:getAnchors()
return body1, body2, x1, y1, x2, y2, self:getMaxLength(), self:getCollideConnected()
end
function userdata_constructor:WeldJoint(srefs)
local s = memo[self]
srefs[#srefs + 1] = {s, ':', 'setDampingRatio', self:getDampingRatio()}
srefs[#srefs + 1] = {s, ':', 'setFrequency', self:getFrequency()}
local body1, body2 = unpack(joint_bodies)
local x1, y1, x2, y2 = self:getAnchors()
return body1, body2, x1, y1, x2, y2, self:getCollideConnected()
end
function userdata_constructor:WheelJoint(srefs)
local s = memo[self]
srefs[#srefs + 1] = {s, ':', 'setMaxMotorTorque', self:getMaxMotorTorque()}
srefs[#srefs + 1] = {s, ':', 'setMotorEnabled', self:isMotorEnabled()}
srefs[#srefs + 1] = {s, ':', 'setMotorSpeed', self:getMotorSpeed()}
srefs[#srefs + 1] = {s, ':', 'setSpringDampingRatio', self:getSpringDampingRatio()}
srefs[#srefs + 1] = {s, ':', 'setSpringFrequency', self:getSpringFrequency()}
local body1, body2 = unpack(joint_bodies)
local x1, y1, x2, y2 = self:getAnchors()
return body1, body2, x1, y1, x2, y2, self:getCollideConnected()
end
function userdata_constructor:ParticleSystem(srefs)
local s = memo[self]
srefs[#srefs + 1] = {s, ':', 'setAreaSpread', self:getAreaSpread()}
srefs[#srefs + 1] = {s, ':', 'setColors', self:getColors()}
srefs[#srefs + 1] = {s, ':', 'setDirection', self:getDirection()}
srefs[#srefs + 1] = {s, ':', 'setEmissionRate', self:getEmissionRate()}
srefs[#srefs + 1] = {s, ':', 'setEmitterLifetime', self:getEmitterLifetime()}
srefs[#srefs + 1] = {s, ':', 'setInsertMode', self:getInsertMode()}
srefs[#srefs + 1] = {s, ':', 'setLinearAcceleration', self:getLinearAcceleration()}
srefs[#srefs + 1] = {s, ':', 'setOffset', self:getOffset()}
srefs[#srefs + 1] = {s, ':', 'setParticleLifetime', self:getParticleLifetime()}
srefs[#srefs + 1] = {s, ':', 'setPosition', self:getPosition()}
srefs[#srefs + 1] = {s, ':', 'setRadialAcceleration', self:getRadialAcceleration()}
srefs[#srefs + 1] = {s, ':', 'setRelativeRotation', self:getRelativeRotation()}
srefs[#srefs + 1] = {s, ':', 'setRotation', self:getRotation()}
srefs[#srefs + 1] = {s, ':', 'setSizeVariation', self:getSizeVariation()}
srefs[#srefs + 1] = {s, ':', 'setSizes', self:getSizes()}
srefs[#srefs + 1] = {s, ':', 'setSpeed', self:getSpeed()}
srefs[#srefs + 1] = {s, ':', 'setSpin', self:getSpin()}
srefs[#srefs + 1] = {s, ':', 'setSpinVariation', self:getSpinVariation()}
srefs[#srefs + 1] = {s, ':', 'setSpread', self:getSpread()}
srefs[#srefs + 1] = {s, ':', 'setTangentialAcceleration', self:getTangentialAcceleration()}
if self:isActive() then
srefs[#srefs + 1] = {s, ':', 'start('}
end
local body1, body2 = unpack(joint_bodies)
local x1, y1, x2, y2 = self:getAnchors()
return self:getTexture(), self:getBufferSize()
end
local function write_table_ex(t, memo, rev_memo, srefs, name)
if type(t) == 'function' then
return 'local _' .. name .. ' = _L ' .. make_safe(dump(t))
elseif type(t) == 'userdata' then
local m = {'local _' .. name .. ' = love.physics.new' .. t:type() .. '('}
for i, arg in ipairs{userdata_constructor[t:type()](t, srefs, memo, rev_memo)} do
m[#m + 1] = write(arg, memo, rev_memo)
m[#m + 1] = ', '
end
m[#m > 1 and #m or #m + 1] = ')'
return concat(m)
end
-- check for class
local pretable = '{'
local posttable = '}'
local classkey = nil
local classname = nil
if registered_things_by_value[t.class] then
-- assume MiddleClass
classname = registered_things_by_value[t.class]
classkey = 'class'
pretable = '_S({'
posttable = '}, ' .. classname .. '.__instanceDict)'
elseif registered_things_by_value[t.__baseclass] then
-- assume SECS
classname = registered_things_by_value[t.__baseclass]
classkey = '__baseclass'
pretable = '_S({'
posttable = '}, _M(' .. classname .. '))'
elseif registered_things_by_value[getmetatable(t)] then
-- assume hump.class
classname = registered_things_by_value[getmetatable(t)]
pretable = '_S({'
posttable = '}, ' .. classname .. ')'
elseif registered_things_by_value[t.__class__] then
-- assume Slither
classname = registered_things_by_value[t.__class__]
pretable = '_M(' .. classname .. ').allocate {'
posttable = '}'
end
local m = {'local _', name, ' = ', pretable}
local mi = 4
for i = 1, #t do -- don't use ipairs here, we need the gaps
local v = t[i]
if v == t or is_cyclic(memo, v, t) then
srefs[#srefs + 1] = {name, '.', i, v}
m[mi + 1] = 'nil, '
mi = mi + 1
else
m[mi + 1] = write(v, memo, rev_memo)
m[mi + 2] = ', '
mi = mi + 2
end
end
for k,v in pairs(t) do
if type(k) ~= 'number' or floor(k) ~= k or k < 1 or k > #t then
if k == classkey then
m[mi + 1] = k
m[mi + 2] = ' = '
m[mi + 3] = classname
m[mi + 4] = ', '
mi = mi + 4
elseif v == t or k == t or is_cyclic(memo, v, t) or is_cyclic(memo, k, t) then
srefs[#srefs + 1] = {name, '.', k, v}
else
m[mi + 1] = write_key_value_pair(k, v, memo, rev_memo)
m[mi + 2] = ', '
mi = mi + 2
end
end
end
m[mi > 4 and mi or mi + 1] = posttable
return concat(m)
end
local function orderobjects(a, b)
if type(a[2]) == 'userdata' then
if type(b[2]) ~= 'userdata' then
return true
end
if a[2]:typeOf('World') then
return true
end
if b[2]:typeOf('World') then
return false
end
if a[2]:typeOf('Fixture') then
return false
end
if b[2]:typeOf('Fixture') then
return true
end
if a[2]:typeOf('Joint') then
return false
end
if b[2]:typeOf('Joint') then
return true
end
elseif type(b[2]) == 'userdata' then
return false
end
return b[1] < a[1]
end
function M.save_all(savename, ...)
local memo = {}
local rev_memo = {...}
for k, v in ipairs(rev_memo) do
memo[v] = k
end
local srefs = {}
local result = {}
-- phase 1: recursively descend the table structure
local n = 1
while rev_memo[n] do
result[n] = {n, rev_memo[n], write_table_ex(rev_memo[n], memo, rev_memo, srefs, n)}
n = n + 1
end
-- phase 2: the right order
sort(result, orderobjects)
for i = 1, #result do
result[i] = result[i][3]
end
-- phase 3: add all the tricky cyclic stuff
for i, v in ipairs(srefs) do
if v[2] == '.' then
result[n] = write_key_value_pair(v[3], v[4], memo, rev_memo, '_' .. v[1])
else
local tmp = {'_', v[1], ':', v[3], '('}
for i = 4, #v do
tmp[i * 2 - 2] = write(v[i], memo, rev_memo)
tmp[i * 2 - 1] = ', '
end
tmp[#tmp] = ')'
result[n] = concat(tmp)
end
n = n + 1
end
-- phase 4: add something about returning the main tables
local r = {'return '}
for i = 1, select('#', ...) do
r[i * 3 - 1] = '_'
r[i * 3] = tostring(i)
r[i * 3 + 1] = ', '
end
r[#r] = nil
result[n] = concat(r)
-- phase 5: just concatenate everything
local contents = concat(result, '\n')
-- phase 6: store contents
love.filesystem.write(savename, contents)
end
local load_mt = {__index = registered_things_by_name}
function M.load_all(savename)
local contents = love.filesystem.read(savename)
if contents then
local s = loadstring(contents)
setfenv(s, setmetatable({_L = loadstring, _S = setmetatable, _M = getmetatable, love = love}, load_mt))
return s()
else
return nil
end
end
return M