-
Notifications
You must be signed in to change notification settings - Fork 0
/
flirt_script
executable file
·780 lines (687 loc) · 17.5 KB
/
flirt_script
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
#!/usr/bin/env lua
--[[ generated on Fri Apr 6 22:28:18 2018 rev v0.3.0
Flirt is Copyright (c) 2015 Kyle Mclamb
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
]]--
do
--- A module for loading LOVE conf.lua files.
--
-- @module loadconf
local loadconf = {}
local function xload(str, name, env)
local chunk, err
if setfenv then -- lua 5.1
chunk, err = loadstring(str, name)
if not chunk then return nil, err end
setfenv(chunk, env)
else -- lua 5.2, 5.3
chunk, err = load(str, name, "bt", env)
if not chunk then return nil, err end
end
return chunk
end
local sandbox = {
assert=assert,
error=error,
getmetatable=getmetatable,
ipairs=ipairs,
next=next,
pairs=pairs,
pcall=pcall,
print=print,
rawequal=rawequal,
rawget=rawget,
rawset=rawset,
select=select,
setmetatable=setmetatable,
tonumber=tonumber,
tostring=tostring,
type=type,
unpack=unpack,
_VERSION=_VERSION,
xpcall=xpcall,
coroutine=coroutine,
string=string,
table=table,
math=math,
os = {
clock=os.clock,
date=os.date,
difftime=os.difftime,
getenv=os.getenv,
time=os.time,
tmpname=os.tmpname
},
newproxy=newproxy
}
sandbox._G = sandbox
local function merge(from, into)
for k, v in pairs(from) do
if type(v) == 'table' then
merge(v, into[k])
elseif not into[k] then
into[k] = v
end
end
end
-- format complex strings.
local function complex_fmt(str, data, shape)
shape = shape or {}
-- FIXME: ignore escaped {}
return str:gsub("%b{}", function(k)
k = k:sub(2, -2)
local s = data[k]
assert(s ~= nil)
if not shape[k] then
-- no shape given, just use tostring
s = tostring(s)
elseif type(shape[k]) == 'string' then
-- assume shape is a format string
s = string.format(shape[k], s)
else
-- assume shape is callable and returns a valid string
s = shape[k](s)
end
return s
end)
end
local function slurp(fname)
local f, s, err
f, err = io.open(fname, 'r')
if not f then return nil, err end
s, err = f:read('*a')
if not s then return nil, err end
f:close()
return s
end
local function line_of(body, n)
local err
if body:sub(1, 1) == '@' then
body, err = slurp(body:sub(2))
if not body then
return nil, err
end
end
if body:sub(-1) ~= '\n' then body = body..'\n' end
local line_i = 1
for line in string.gmatch(body, "(.-)\n") do
if n == line_i then
return line
end
line_i = line_i + 1
end
return nil, "line out of range"
end
local friendly_msg = [[
{conf} could not be safely loaded.
If {conf} works inside LOVE but not here, then maybe it
has more complex behavior than {program} can recognize.
In that case you should wrap that behavior in a guard, like so:
if love.filesystem then
{broken_line}
end
Actual error:
{orig}
]]
-- Tells the user that they should guard against complex behavior
local function friendly_error(opts)
if opts.friendly ~= true then
return function(...) return ... end
end
return function(err)
local info = debug.getinfo(2, 'lS')
if info.short_src:match("loadconf.lua") then
-- this is actually an internal error
return err
end
local line = line_of(info.source, info.currentline)
if not line then
-- could not retrieve source data, return internal err
-- instead
return err
end
line = line:gsub("^%s+", "")
local name = "string conf.lua"
if info.source:match("^@") then
name = info.short_src
end
return complex_fmt(friendly_msg, {
conf = name,
program = opts.program or loadconf.default_opts.program,
broken_line = line,
orig = err
})
end
end
local friendly_parse_msg = [[
{conf} could not be parsed. This is usually a syntax error. Keep in
mind that {conf} should be valid {lua_version}!
{orig}
]]
-- Tells the user that their conf.lua failed to parse
local function friendly_parse_error(err, name, opts)
if opts.friendly ~= true then
return err
end
return complex_fmt(friendly_parse_msg, {
conf = name or "string conf.lua",
lua_version = _VERSION,
orig = err
})
end
--- Given the string contents of a conf.lua, returns a table containing the
-- configuration it represents.
-- @param str The contents of conf.lua
-- @param name The name of conf.lua used in error messages. Uses same format as `load`. Optional.
-- @param[type=options] opts A configuration table. Optional.
-- @return `love_config`
-- @error
function loadconf.parse_string(str, name, opts)
opts = opts or loadconf.default_opts
--name = name
local ok, chunk, err
local env = setmetatable({love = {}}, {__index = sandbox})
--assert(type(name) == "string")
ok, chunk, maybe_err = pcall(xload, str, name, env)
if not ok then return nil, chunk end
if not chunk then
return nil, friendly_parse_error(maybe_err, name, opts)
end
ok, err = xpcall(chunk, friendly_error(opts))
if not ok then return nil, err end
if not env.love.conf then
return {} -- No configuration
end
local t = { window = {}, audio = {}, screen = {}, modules = {} }
ok, err = xpcall(function()
env.love.conf(t)
end, friendly_error(opts))
if ok then
if not t.version then
t.version = loadconf.latest_stable_version
end
if opts.include_defaults == true and loadconf.defaults[t.version] then
merge(loadconf.defaults[t.version], t)
end
return t
else
return nil, err
end
end
--- Given the filename of a valid conf.lua file, returns a table containing the
-- configuration it represents.
-- @param fname The path to the conf.lua file
-- @tparam options opts A configuration table. Optional.
-- @return `love_config`
-- @error
function loadconf.parse_file(fname, opts)
opts = opts or loadconf.default_opts
local str, err = slurp(fname)
if not str then return nil, err end
return loadconf.parse_string(str, "@"..fname, opts)
end
--- The configuration tables produced by running `love.conf`.
-- @table love_config
-- @see love/Config_Files
--- The optional table all loadconf functions take. customize according to your
-- use case.
-- @table options
-- @field[opt="loadconf"] program What is the program called? Used for friendly errors
-- @field[opt=false] friendly Enable user-friendly errors
-- @field[opt=false] include_defaults Return default values in parsed configs
loadconf.default_opts = {
program = "loadconf",
friendly = false,
include_defaults = false
}
--- The current stable love version, which right now is "11.0". Please
-- submit an issue/pull request if this is out of date, sorry~
loadconf.stable_love = "11.0"
--- A table containing the default config tables for each version of love.
-- @usage assert(loadconf.defaults["0.9.2"].window.fullscreentype == "normal")
loadconf.defaults = {}
local function defaults_copy(old_v, version)
local old = loadconf.defaults[old_v]
local t = {}
for k, v in pairs(old) do
t[k] = v
end
t.version = version
loadconf.defaults[version] = t
end
-- default values for 11.0 {{{
loadconf.defaults["11.0"] = {
identity = nil,
appendidentity = false,
version = "11.0",
console = false,
accelerometerjoystick = true,
externalstorage = false,
gammacorrect = false,
audio = {
mixwithsystem = true
},
window = {
title = "Untitled",
icon = nil,
width = 800,
height = 600,
borderless = false,
resizable = false,
minwidth = 1,
minheight = 1,
fullscreen = false,
fullscreentype = "desktop",
vsync = 1,
msaa = 0,
display = 1,
highdpi = false,
x = nil,
y = nil
},
modules = {
audio = true,
data = true,
event = true,
font = true,
graphics = true,
image = true,
joystick = true,
keyboard = true,
math = true,
mouse = true,
physics = true,
sound = true,
system = true,
thread = true,
timer = true,
touch = true,
video = true,
window = true
}
}
-- }}}
-- default values for 0.10.X {{{
loadconf.defaults["0.10.2"] = {
identity = nil,
version = "0.10.2",
console = false,
gammacorrect = false,
externalstorage = false,
accelerometerjoystick = true,
window = {
title = "Untitled",
icon = nil,
width = 800,
height = 600,
borderless = false,
resizable = false,
minwidth = 1,
minheight = 1,
fullscreen = false,
fullscreentype = "desktop",
vsync = true,
msaa = 0,
display = 1,
highdpi = false,
x = nil,
y = nil
},
modules = {
audio = true,
event = true,
graphics = true,
image = true,
joystick = true,
keyboard = true,
math = true,
mouse = true,
physics = true,
sound = true,
system = true,
timer = true,
touch = true,
video = true,
window = true,
thread = true,
}
}
defaults_copy("0.10.2", "0.10.1")
defaults_copy("0.10.2", "0.10.0")
loadconf.defaults["0.10.0"].externalstorage = nil
-- }}}
-- default values for 0.9.X {{{
loadconf.defaults["0.9.2"] = {
identity = nil,
version = "0.9.2",
console = false,
window = {
title = "Untitled",
icon = nil,
width = 800,
height = 600,
borderless = false,
resizable = false,
minwidth = 1,
minheight = 1,
fullscreen = false,
fullscreentype = "normal",
vsync = true,
fsaa = 0,
display = 1,
highdpi = false,
srgb = false,
x = nil,
y = nil
},
modules = {
audio = true,
event = true,
graphics = true,
image = true,
joystick = true,
keyboard = true,
math = true,
mouse = true,
physics = true,
sound = true,
system = true,
timer = true,
window = true,
thread = true,
}
}
defaults_copy("0.9.2", "0.9.1")
defaults_copy("0.9.2", "0.9.0")
-- }}}
-- default values for 0.8.X {{{
loadconf.defaults["0.8.0"] = {
identity = nil,
version = "0.8.0",
console = false,
release = false,
title = "Untitled",
author = "Unnamed",
screen = {
width = 800,
height = 600,
fullscreen = false,
vsync = true,
fsaa = 0
},
modules = {
audio = true,
event = true,
graphics = true,
image = true,
joystick = true,
keyboard = true,
mouse = true,
physics = true,
sound = true,
timer = true,
thread = true
}
}
-- }}}
local Loadconf = {}
local Loadconf_mt = {__index = Loadconf}
---
-- Create an instanced version of loadconf. This carries its configuration
-- state in object-oriented way, if you prefer that.
-- @param[type=options] opts
-- @return a `Loadconf` instance
function loadconf.new(opts)
local t = {}
for k, v in pairs(loadconf.default_opts) do
if opts[k] == nil then
t[k] = v
else
t[k] = opts[k]
end
end
return setmetatable(t, Loadconf_mt)
end
--- An object-oriented instance of the loadconf module. This carries
-- configuration state internally so you can set-and-forget the `options`
-- table.
--
-- @type Loadconf
--- @see loadconf.parse_string
function Loadconf:parse_string(str, name)
return loadconf.parse_string(str, name, self)
end
--- @see loadconf.parse_file
function Loadconf:parse_file(fname)
return loadconf.parse_file(fname, self)
end
package.loaded.loadconf = loadconf
end
do
local loadconf = require 'loadconf'
-- FIXME
local config_path = ("%s/.config/flirt/conf.lua"):format(os.getenv("HOME"))
local config = {
stable = loadconf.stable_love, -- versions past this number are considered prerelease
}
assert(type(config.stable) == 'string')
local function fail(...)
io.stderr:write("Error: " .. string.format(...) .. "\n")
io.stderr:flush()
end
local function log(...)
io.stdout:write("flirt: " .. string.format(...) .. "\n")
io.stdout:flush()
end
local function load_versions()
local chunk, err = loadfile(config_path)
if chunk then
local t = chunk()
for k, v in pairs(t) do
config[k] = v
end
else
return nil, err
end
end
local function save_versions()
local dir = config_path:gsub("/conf.lua$", "")
assert(os.execute(("mkdir -p %q"):format(dir))) -- FIXME
local f = assert(io.open(config_path, 'w'))
local s_pairs = {}
for k, v in pairs(config) do
table.insert(s_pairs, string.format("[%q] = %q", k, v))
end
f:write("return {\n\t" .. table.concat(s_pairs, ",\n\t") .. "\n}")
f:close()
log("config saved to %q", config_path)
end
local function sopen(exec_str)
local f, str, err
f, err = io.popen(exec_str)
if not f then return nil, err end
str, err = f:read('*a')
if not str then return nil, err end
f:close()
return str
end
local function which(exe)
return sopen(string.format("which '%s' 2> /dev/null", exe)) -- FIXME
end
local names = {
"love",
"love07",
"love08",
"love09",
"love10",
"love11",
"love-hg"
}
local function trim(s)
return (s:gsub("^%s+",""):gsub("%s+$", ""))
end
local function get_version_for(exe)
local s = sopen(string.format("%q --version", exe))
if s then
local v = s:match("%d+%.%d+%.%d+")
if v then
return v
end
end
return nil, ("invalid exectuable: %q"):format(exe)
end
local function add_exe(s)
local v, err = get_version_for(s)
if not v then
return v, err
end
log("found LOVE %s at %q", v, s)
config[v] = s
return true
end
local function guess()
for _, name in ipairs(names) do
local s = which(name)
if s and s ~= "" then
s = trim(s)
add_exe(s)
end
end
end
local function exists(fname)
local f = io.open(fname) -- FIXME: does not work in windows
if f then
f:close()
return true
end
return false
end
local function is_dir(fname)
return os.execute(string.format("test -d %q", fname)) or false -- FIXME
end
local function vsplit(v)
local maj, min, rev = string.match(v, "(%d+)%.(%d+)%.(%d+)")
return tonumber(maj), tonumber(min), tonumber(rev)
end
local function best_exe_for(version)
if config[version] then
return config[version]
end
local maj, min, rev = vsplit(version)
for v, path in pairs(config) do
if v ~= 'stable' then
local imaj, imin, irev = vsplit(v)
if imaj == maj and imin == min and irev > rev then -- better patch
return path
end
end
end
return nil
end
local function gt(a, b)
if a == nil then
return false
elseif b == nil then
return true
end
local amaj, amin, arev = vsplit(a)
local bmaj, bmin, brev = vsplit(b)
if amaj ~= bmaj then
return amaj > bmaj
elseif amin ~= bmin then
return amin > bmin
else
return arev > brev
end
end
local function most_recent()
local c = nil
local stable = config.stable
for v, _ in pairs(config) do
if v ~= "stable" then
if gt(v, c) and not gt(v, stable) then
c = v
end
end
end
return c
end
--- Given the filename of a .love archive, returns a table containing its
-- configuration. Requires UnZip to be installed to your path.
-- @param fname The path to the .love file
-- @return the configuration table, or `nil, err` if an error occured.
local function parse_archive(fname)
-- FIXME: use a proper cross-platform zip library
local f, str, err
f, err = io.popen(("unzip -p %q conf.lua 2> /dev/null"):format(fname))
if not f then return nil, err end
str, err = f:read('*a')
if not str then return nil, err end
f:close()
return loadconf.parse_string(str, "@"..fname.."/conf.lua", {
program = "flirt",
friendly = true
})
end
local function bash_escape(s)
return s:gsub("\"", "\\\""):gsub("^~", os.getenv("HOME"))
end
local function run(...)
local s = ""
local sep = ""
for i=1, select('#', ...) do
s = s .. sep
s = s .. "\"" .. bash_escape(select(i, ...)) .. "\""
sep = " "
end
return os.execute(s)
end
local function main(...)
load_versions()
assert(config.stable)
local fname = ...
local version = config.stable
if fname == "--autoconf" then
guess()
save_versions()
return
elseif fname == "--add-exe" then
assert(add_exe((select(2, ...))))
save_versions()
return
elseif fname ~= nil and exists(fname) then
if is_dir(fname) and exists(fname .. "/conf.lua") then
version = assert(loadconf.parse_file(fname.."/conf.lua", {
program = "flirt",
friendly = true
})).version
else
version = assert(parse_archive(fname)).version
end
if version == nil then -- no version specified
version = most_recent()
end
end
local p = best_exe_for(version)
if not p then
fail("No LOVE executable for version " .. version)
return
end
run(p, ...)
return
end
main(...)
end