-
Notifications
You must be signed in to change notification settings - Fork 5
/
tas.lua
812 lines (708 loc) · 21.4 KB
/
tas.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
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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
require "deepcopy"
local class = require("30log")
local tas = class("tas")
local console = require("console")
tas.hud_w = 48
tas.hud_h = 0
tas.scale = 6
tas.pianoroll_w=65
--wrapper functions
-- recieves the index of the frame to check the key state of
-- index defaults to the current frame if nil
function tas:key_down(i, frame)
frame = frame or self:frame_count() + 1
return bit.band(self.keystates[frame],2^i)~=0
end
function tas:key_held(i)
return bit.band(self.hold, 2^i)~=0
end
function tas:toggle_key(i, frame)
frame = frame or self:frame_count() + 1
self.keystates[frame]=bit.bxor(self.keystates[frame],2^i)
--disabling a key on the current frame should also disable that hold
if not self:key_down(i) and frame == self:frame_count() + 1 then
self.hold=bit.band(self.hold,bit.bnot(2^i))
end
end
function tas:toggle_hold(i)
self.hold=bit.bxor(self.hold,2^i)
-- holding a key should also set it
if self:key_held(i) then
self.keystates[self:frame_count()+1]=bit.bor(self.keystates[self:frame_count()+1],2^i)
end
end
function tas:reset_keys()
self.keystates[self:frame_count()+1]=0
end
function tas:insert_keystate()
table.insert(self.keystates, self:frame_count() + 1, self.hold)
end
function tas:duplicate_keystate()
table.insert(self.keystates, self:frame_count() + 1, self.keystates[self:frame_count()+1])
end
function tas:delete_keystate()
table.remove(self.keystates, self:frame_count() + 1)
if self:frame_count() + 1 > #self.keystates then
table.insert(self.keystates, 0)
end
end
function tas:delete_selection()
for i=self:frame_count()+1, self.last_selected_frame do
self:delete_keystate()
end
self.last_selected_frame = -1
end
function tas:reset_hold()
self.hold=0
end
local function update_buttons(self, input_idx)
for i = 0, #pico8.keymap[0] do
local v = pico8.keypressed[0][i]
if self:key_down(i, input_idx) then
pico8.keypressed[0][i] = (v or -1) + 1
else
pico8.keypressed[0][i] = nil
end
end
end
-- returns the keystate of the next frame
-- depending on its current state, whether the the user is holding down inputs, and the current hold
--
-- i.e, if the input is currently right, and up is held, up+right will be returned
function tas:advance_keystate(curr_keystate)
curr_keystate = curr_keystate or 0
curr_keystate= bit.bor(curr_keystate, self.hold)
if not self.realtime_playback then
for i=0, #pico8.keymap[0] do
for _, testkey in pairs(pico8.keymap[0][i]) do
if love.keyboard.isDown(testkey) then
curr_keystate = bit.bor(curr_keystate, 2^i)
break
end
end
end
end
return curr_keystate
end
-- if save_state is true - deepcopy the current state, and push it to the stack
-- if save_state is false - push a dummy (empty) state to the stack - as a marker we didn't save this frame
-- if frame_count is overloaded, it must be updated/increased *before* calling pushstate
function tas:pushstate(save_state)
if save_state then
-- don't copy any non-cart functions
local clone=deepcopy_no_api(pico8)
--push the actual pico8 instance (and not the clone) to support undoing
table.insert(self.states,pico8)
pico8 = clone
else
table.insert(self.states, self.EMPTY_STATE)
end
if self.keystates[self:frame_count()+1] == nil then
self.keystates[self:frame_count()+1] = 0
end
end
-- pop the top state off the state stack. if it was an unsaved state, it will just be tas.EMPTY_STATE
function tas:popstate()
return table.remove(self.states)
end
--clone the current state of the pico8 (for backup's sake)
function tas:clonestate()
return deepcopy_no_api(pico8)
end
function tas:clearstates()
self.states={}
end
function tas:state_iter()
local i = 0
local n = #self.states
return function()
i = i + 1
if (i <= n) then
return self.states[i]
elseif i == n+1 then
return pico8
end
end
end
-- advance the pico8 state ignoring buttons or backing up the state
local function rawstep()
pico8.frames = pico8.frames + 1
if pico8.cart._update60 then
pico8.cart._update60()
elseif pico8.cart._update then
pico8.cart._update()
end
love.graphics.push()
restore_clip()
restore_camera()
if pico8.cart._draw then
pico8.cart._draw()
end
love.graphics.pop()
end
function tas:step()
local input_idx = self:frame_count() + 1
--store the state
local save_state = #self.states == 0 or (self.savestate_interval~=0 and (input_idx-1)%self.savestate_interval==0)
self:pushstate(save_state)
--update based on the buttons of the 'previous' frame
--TODO: make this cleaner
update_buttons(self,input_idx)
love.graphics.setCanvas(pico8.screen)
rawstep()
--advance the state of pressed keys
self.keystates[self:frame_count()+1] = self:advance_keystate(self.keystates[self:frame_count()+1])
end
function tas:rewind()
if #self.states <= 0 then
return
end
-- pop states until reaching a state that was saved
repeat pico8 = self:popstate() until pico8 ~= self.EMPTY_STATE
love.graphics.setCanvas(pico8.screen)
pico8.draw_shader:send("palette", shdr_unpack(pico8.draw_palette))
pico8.sprite_shader:send("palette", shdr_unpack(pico8.draw_palette))
pico8.text_shader:send("palette", shdr_unpack(pico8.draw_palette))
pico8.display_shader:send("palette", shdr_unpack(pico8.display_palette))
restore_clip()
restore_camera()
self.keystates[self:frame_count()+1] = self:advance_keystate(self.keystates[self:frame_count()+1])
end
--rewind to the first frame
function tas:full_rewind()
while #self.states>1 do
pico8 = self:popstate()
end
-- for the last frame, call rewind to update the canvas, and allow overloaded funcs to update variables
self:rewind()
end
function tas:goto_frame(i)
if self:frame_count() < i then
self.seek = {
finish_condition = function()
return self:frame_count() >= i
end,
on_finish = function() end,
fast_forward = true,
}
elseif 0 <= i and i < self:frame_count() then
while i < self:frame_count() - 1 do
self:popstate()
end
self:rewind()
end
end
function tas:full_reset()
self:full_rewind()
self.hold=0
self.keystates={0}
end
function tas:init()
--unique identifier for empty states
self.EMPTY_STATE = {}
self.states={}
self.keystates={0}
self.realtime_playback=false
self.hold = 0
self.savestate_interval = 1
tas.screen = love.graphics.newCanvas((pico8.resolution[1]+self.hud_w + self.pianoroll_w)*self.scale, (pico8.resolution[2] + self.hud_h)*self.scale)
-- duplicated code is kinda eh but i am lazy
local tas_w, tas_h = tas.screen:getDimensions()
local screen_w, screen_h = love.graphics.getDimensions()
scale = math.min(screen_w/tas_w, screen_h/tas_h) -- scale for drawing to the screen
self.last_selected_frame = -1
self.undo_states = {}
self.undo_idx = 0
self:push_undo_state()
console.ENV = setmetatable({print=print}, {
__index = function(table,key) return pico8.cart[key] end,
__newindex = function(table, key, val) pico8.cart[key] = val end
})
rawset(console.ENV, "goto_frame", function(...) self:goto_frame(...) end)
rawset(console.ENV, "savestate_every", function(new_interval) self.savestate_interval = new_interval end)
-- support goto_frame x for absolute, or goto_frame +x or -x for relative
console.COMMANDS["goto_frame"] = function(arg)
print("goto_frame "..(arg or ""))
if arg == nil then
console.colorprint({console.ERROR_COLOR, "must give a single argument (number or offset)"})
return
end
local offset = 0
if arg:sub(1,1) == "+" or arg:sub(1,1) == "-" then
offset = self:frame_count()
end
local value = tonumber(arg)
if value == nil then
console.colorprint({console.ERROR_COLOR, "argument must be a number or offset"})
return
end
self:goto_frame(value + offset)
end
--(func)on_finish, (func)finish_condition, (bool)fast_forward, (bool)finish_on_interrupt
self.seek=nil
end
function tas:update()
if self.realtime_playback then
self:step()
elseif self.seek then
if self.seek.fast_forward then
local t=love.timer.getTime()
repeat
self:step()
until self.seek.finish_condition() or love.timer.getTime()-t>pico8.frametime*0.75
else
self:step()
end
if self.seek.finish_condition() then
self.seek.on_finish()
self.seek=nil
end
end
end
local function shallow_copy(t)
local r={}
for k,v in pairs(t) do
r[k]=v
end
return r
end
-- states are shallow copyied (for perf reasons), meaning mutating them directly will cause undo to desync
function tas:get_editor_state()
return {states = shallow_copy(self.states), keystates = deepcopy(self.keystates), pico8 = pico8}
end
function tas:load_editor_state(state)
self.states = shallow_copy(state.states)
self.keystates = deepcopy(state.keystates)
pico8 = state.pico8
end
--undo_idx points the state to be loaded if undo is preformed
--places past it are stored to support redo
function tas:push_undo_state()
self.undo_idx = self.undo_idx + 1
while #self.undo_states >= self.undo_idx do
table.remove(self.undo_states)
end
table.insert(self.undo_states, self:get_editor_state())
-- limit undo history to depth 30, to avoid overusing memory
if #self.undo_states>30 then
table.remove(self.undo_states,1)
self.undo_idx = self.undo_idx - 1
end
end
function tas:preform_undo()
if self.undo_idx == 0 then
return
end
local new_state = self.undo_states[self.undo_idx]
local curr_state = self:get_editor_state()
self:load_editor_state(new_state)
self.undo_states[self.undo_idx] = curr_state
self.undo_idx = self.undo_idx - 1
self.hold = 0
self.last_selected_frame = -1
end
function tas:perform_redo()
if self.undo_idx == #self.undo_states then
return
end
self.undo_idx = self.undo_idx + 1
local new_state = self.undo_states[self.undo_idx]
local curr_state = self:get_editor_state()
self:load_editor_state(new_state)
self.undo_states[self.undo_idx] = curr_state
self.hold = 0
self.last_selected_frame = -1
end
function setPicoColor(c)
local r,g,b,a = unpack(pico8.palette[c])
love.graphics.setColor(r/255, g/255, b/255, a/255)
end
function tas:draw_button(x,y,i)
if self:key_held(i) then
if not self:key_down(i) then
-- this is a weird state that's a bit hard (but possible) to get into
setPicoColor(9)
else
setPicoColor(8)
end
elseif self:key_down(i) then
setPicoColor(7)
else
setPicoColor(1)
end
love.graphics.rectangle("fill", x, y, 3, 3)
end
function tas:draw_input_display(x,y)
setPicoColor(0)
love.graphics.rectangle("fill", x, y, 25,11)
self:draw_button(x + 12, y + 6, 0) -- l
self:draw_button(x + 20, y + 6, 1) -- r
self:draw_button(x + 16, y + 2, 2) -- u
self:draw_button(x + 16, y + 6, 3) -- d
self:draw_button(x + 2, y + 6, 4) -- z
self:draw_button(x + 6, y + 6, 5) -- x
end
-- can be overloaded to define different timing methods
function tas:frame_count()
return #self.states
end
--returns the width of the counter
function tas:draw_frame_counter(x,y)
setPicoColor(0)
local frame_count_str = tostring(self:frame_count())
local width = 4*math.max(#frame_count_str,3)+1
love.graphics.rectangle("fill", x, y, width, 7)
setPicoColor(7)
love.graphics.print(frame_count_str, x+1,y+1)
return width
end
--tbl is a table of coloredTexts, of the entries of the table
local function draw_inputs_row(tbl, x, y, c, frame_num)
local box_w = 48/#tbl
-- draw the frame number
setPicoColor(c)
love.graphics.rectangle("fill", x + 1, y, 17, 7)
setPicoColor(0)
love.graphics.rectangle("line", x + 1, y, 17, 7)
love.graphics.printf(tostring(frame_num), x + 1 , y+1, 17, "right")
for i=1, #tbl do
setPicoColor(c)
love.graphics.rectangle("fill", x+(i-1)*box_w+18, y, box_w, 7)
setPicoColor(0)
love.graphics.rectangle("line", x+(i-1)*box_w+18, y, box_w, 7)
love.graphics.setColor(1,1,1,1)
love.graphics.printf(tbl[i], x+(i-1)*box_w+18, y+1, box_w, "center")
end
end
function tas:draw_piano_roll()
local x=pico8.resolution[1] + self.hud_w
local y=0
local inputs={"l","r","u","d","z","x"}
local header={}
for i,v in ipairs(inputs) do
header[i]={{0,0,0},v}
end
draw_inputs_row(header,x,y,10,"idx")
local num_rows= math.floor(pico8.resolution[2]/7)-1
local frame_count = self:frame_count() + 1
--use 1/3rd of the rows for frames before, and 2/3rds for the frames after the curr frame
--use make sure to use all the rows on the edges
local start_row = math.max(frame_count - math.floor(num_rows/3), self.last_selected_frame - num_rows + 2,1)
if start_row + num_rows - 1 > #self.keystates then
start_row = math.max(#self.keystates - num_rows + 1, 1)
end
for i=start_row, math.min(start_row + num_rows - 1, #self.keystates) do
local current_frame = i == frame_count
local s={}
for j=1, #inputs do
if self:key_down(j-1, i) then
if current_frame and self:key_held(j-1) then
local r,g,b,a=unpack(pico8.palette[8])
s[j]={{r/255,g/255, b/255,a/255},inputs[j]}
else
s[j]={{0,0,0},inputs[j]}
end
else
s[j]=" "
end
end
local corrosponding_state_idx = #self.states + 1 - frame_count + i
local non_savestate_frame = corrosponding_state_idx <= #self.states and self.states[corrosponding_state_idx] == self.EMPTY_STATE
local row_color = current_frame and 12 or (i > frame_count and i <= self.last_selected_frame) and 13 or non_savestate_frame and 5 or 7
draw_inputs_row(s,x,y+7*(i - start_row + 1), row_color, i-1)
end
end
function tas:draw()
love.graphics.setColor(1,1,1,1)
love.graphics.setCanvas(tas.screen)
love.graphics.setShader(pico8.display_shader)
love.graphics.origin()
love.graphics.setScissor()
love.graphics.clear(0.1, 0.1, 0.1)
love.graphics.scale(tas.scale,tas.scale)
love.graphics.draw(pico8.screen, self.hud_w, self.hud_h, 0)
love.graphics.setShader()
-- tas tool ui drawing here
local frame_count_width = self:draw_frame_counter(1,1)
self:draw_input_display(1+frame_count_width+1,1)
self:draw_piano_roll()
love.graphics.setColor(1,1,1,1)
end
function tas:draw_gif_overlay()
local frame_count_width = self:draw_frame_counter(1,1)
self:draw_input_display(1+frame_count_width+1,1)
end
function tas:keypressed(key, isrepeat)
local ctrl = love.keyboard.isDown('lctrl', 'rctrl', 'lgui', 'rgui')
if self.realtime_playback then
-- pressing any key during realtime playback stops it
self.realtime_playback = false
elseif self.seek then
-- pressing any key during seek interrupts it
if self.seek.finish_on_interrupt then
self.seek.on_finish()
end
self.seek=nil
elseif key=='p' then
if love.keyboard.isDown('lshift', 'rshift') then
self:goto_frame(#self.keystates - 1)
else
self.realtime_playback = not self.realtime_playback
end
--TODO: block keypresses even when overloading this func
elseif self.last_selected_frame ~= -1 then
self:selection_keypress(key, isrepeat)
elseif key=='l' then
if love.keyboard.isDown('lshift', 'rshift') then
if self:frame_count() + 1 < #self.keystates then
self.last_selected_frame = self:frame_count() + 2
end
else
self:step()
end
elseif key=='k' then
self:rewind()
elseif key=='d' then
self:full_rewind()
elseif key=='r' and love.keyboard.isDown('lshift','rshift') then
self:push_undo_state()
self:full_reset()
elseif key=='m' then
self:save_input_file()
elseif key=='w' and love.keyboard.isDown('lshift', 'rshift') then
self:push_undo_state()
self:load_input_file()
elseif key=='insert' then
self:push_undo_state()
if ctrl then
self:duplicate_keystate()
else
self:insert_keystate()
end
elseif key=='delete' then
self:push_undo_state()
self:delete_keystate()
elseif key == 'v' and ctrl then
self:push_undo_state()
self:paste_inputs()
elseif key == 'z' and ctrl then
if love.keyboard.isDown('lshift', 'rshift') then
self:perform_redo()
else
self:preform_undo()
end
else
for i = 0, #pico8.keymap[0] do
for _, testkey in pairs(pico8.keymap[0][i]) do
if key == testkey and not isrepeat then
if love.keyboard.isDown("lshift", "rshift") then
self:push_undo_state()
self:toggle_hold(i)
else
self:push_undo_state()
self:toggle_key(i)
end
break
end
end
end
end
end
function tas:selection_keypress(key, isrepeat)
local ctrl = love.keyboard.isDown("lctrl", "rctrl", "lgui", "rgui")
if key == 'l' then
self.last_selected_frame = math.min(self.last_selected_frame + 1, #self.keystates)
elseif key == 'k' then
self.last_selected_frame = self.last_selected_frame - 1
if self.last_selected_frame <= self:frame_count() + 1 then
self.last_selected_frame = -1
end
elseif key == 'escape' then
self.last_selected_frame = -1
elseif key=='delete' then
self:push_undo_state()
self:delete_selection()
elseif key == 'c' and ctrl then
love.system.setClipboardText(self:get_input_str(self:frame_count() + 1, self.last_selected_frame))
elseif key == 'x' and ctrl then
love.system.setClipboardText(self:get_input_str(self:frame_count() + 1, self.last_selected_frame))
self:push_undo_state()
self:delete_selection()
elseif key == 'v' and ctrl then
self:push_undo_state()
self:delete_selection()
self:paste_inputs()
elseif key == 'z' and ctrl then
if love.keyboard.isDown('lshift', 'rshift') then
self:perform_redo()
else
self:preform_undo()
end
elseif key == 'home' then
self.last_selected_frame = self:frame_count() + 2
elseif key=='end' then
self.last_selected_frame = #self.keystates
elseif not isrepeat then
-- change the state of the key in all selected frames
-- if alt is held, toggle the state in all the frames
-- otherwise, toggle it in the first frame, and set all other selected frames to match it
for i = 0, #pico8.keymap[0] do
for _, testkey in pairs(pico8.keymap[0][i]) do
if key == testkey then
self:push_undo_state()
self:toggle_key(i)
for frame = self:frame_count() + 2, self.last_selected_frame do
if love.keyboard.isDown("lalt", "ralt") or self:key_down(i,frame) ~= self:key_down(i) then
self:toggle_key(i, frame)
end
end
break
end
end
end
end
end
-- b is a bitmask of the inputs
local function set_btn_state(b)
for i = 0, #pico8.keymap[0] do
local v = pico8.keypressed[0][i]
if bit.band(b, 2^i)~=0 then
pico8.keypressed[0][i] = (v or -1) + 1
else
pico8.keypressed[0][i] = nil
end
end
end
-- check whether the predicate returns truthy within num frames
-- if respect_inputs is true the current inputs are used
-- inputs has 3 options
-- a table, in which case those inputs are used
-- true, in which case the inputs currently inputted are used
-- nil, in which case no inputs will be used (all neutral)
function tas:predict(pred, num, inputs)
if pred() then
return true
end
--Backup gfx state
--TODO: handle this better/clean this up
love.graphics.push()
local canvas=love.graphics.getCanvas()
local shader=love.graphics.getShader()
local p8state = pico8
--set gfx stuff?
pico8 = deepcopy_no_api(pico8)
love.graphics.setCanvas(pico8.screen)
local ret=false
local input_tbl
if type(inputs)=="table" then
input_tbl=inputs
elseif inputs then
input_tbl={table.unpack(self.keystates,self:frame_count()+1)}
else
input_tbl={}
end
for i=1,num do
set_btn_state(input_tbl[i] or 0)
rawstep()
if pred() then
ret=true
break
end
end
pico8 = p8state
love.graphics.setCanvas(canvas)
love.graphics.setShader(shader)
love.graphics.pop()
return ret
end
--returns number of loaded frames on success, nil if the input_str is invalid
--i is the index to insert the inputs at
--if i is nil, the current inputs will be replaced by the new ones
function tas:load_input_str(input_str, i)
local new_inputs={}
for input in input_str:gmatch("[^,]+") do
if tonumber(input) == nil then
print("invalid input file")
return
else
table.insert(new_inputs, tonumber(input))
end
end
if i == nil then
self:full_reset()
self.keystates = new_inputs
else
--insert the new inputs before index i
for j,v in ipairs(new_inputs) do
table.insert(self.keystates, i+j-1, v)
end
end
return #new_inputs
end
-- i,j optional indices for start and end
function tas:get_input_str(i,j)
return table.concat(self.keystates, ",", i, j)
end
-- get the file object of the input file
-- overloads should returns nil if the file cannot be created
function tas:get_input_file_obj()
local stripped_cartname = cartname:match("[^.]+")
local filename = stripped_cartname .. ".tas"
return love.filesystem.newFile(filename)
end
function tas:save_input_file()
local f = self:get_input_file_obj()
if not f then
return
end
if f:open("w") then
f:write(self:get_input_str())
print("saved file to ".. love.filesystem.getRealDirectory(f:getFilename()).."/"..f:getFilename())
else
print("error saving input file")
end
end
--returns number of loaded frames on success, nil if loading failed
function tas:load_input_file(f)
f = f or self:get_input_file_obj()
if not f then
return
end
if f:open("r") then
local data = f:read()
return self:load_input_str(data)
else
print("error opening input file")
end
end
function tas:paste_inputs()
local cnt = self:load_input_str(love.system.getClipboardText(), self:frame_count() + 1)
if cnt then
self.last_selected_frame = self:frame_count() + cnt
end
end
--called on tas tool crash
--save a backup of the current inputs, and return the path
function tas:save_backup()
local stripped_cartname = cartname:match("[^.]+")
local filename = cartname .. "-" .. os.time() .. ".tas"
if not love.filesystem.getInfo("backups", "directory") then
if not love.filesystem.createDirectory("backups") then
print("error creating backup directory")
return nil
end
end
local f = love.filesystem.newFile('backups/'..filename)
if not f then
return
end
if f:open("w") then
f:write(self:get_input_str())
local path = love.filesystem.getRealDirectory(f:getFilename()).."/"..f:getFilename()
print("saved backup file to ".. path)
return path
else
print("error saving backup of file")
end
end
return tas