-
Notifications
You must be signed in to change notification settings - Fork 0
/
__ViewerRef__.script
648 lines (578 loc) · 17.4 KB
/
__ViewerRef__.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
-- Prototypia Viewer Main Logics
-- Copyright(c) 2024- 1dealGas, under the MIT License.
------------------------------------
-- Our Workflow:
-- Viewer
-- · Status
-- ·· Audio
-- ·· Chart[Fumen] Context
-- ·· File Listening Params
-- · GUI
-- Python Part
------------------------------------
-- Hash & String Caches
--
local hash = hash
local disable, enable, touch = hash("disable"), hash("enable"), hash("touch")
local Sprite, Helper = hash("Sprite"), hash("Helper")
local tint_w, tint = hash("tint.w"), hash("tint")
local msfmt = "[MS] %d · %d"
local titlefmt = "[%d] Prototypia Viewer -- %s -- %d Hints"
local modification = "modification"
local BUTTON_P, BUTTON_SP = hash("BUTTON_P"), hash("BUTTON_SP")
local SCROLL_UP, BUTTON_UP = hash("SCROLL_UP"), hash("BUTTON_UP")
local SCROLL_DOWN, BUTTON_DOWN = hash("SCROLL_DOWN"), hash("BUTTON_DOWN")
local BUTTON_C, BUTTON_A = hash("BUTTON_C"), hash("BUTTON_A")
local GUPDATE, SHOW_PAUSE, SHOW_PLAY = hash("GUPDATE"), hash("SHOW_PAUSE"), hash("SHOW_PLAY")
local GUI_LOAD, GUI_SEEK, GUI_PLAY, GUI_OWV = hash("GUI_LOAD"), hash("GUI_SEEK"), hash("GUI_PLAY"), hash("GUI_OWV")
-- Function Caches
-- Only functions used in a for loop, or the Update callback will be cached.
--
local InitArf = Arf2.InitArf
local UpdateArf = Arf2.UpdateArf
local FinalArf = Arf2.FinalArf
local CheckPlaying = AcAudio.CheckPlaying
local GetTime = AcAudio.GetTime
local msg_url = msg.url
local msg_post = msg.post
local collectgarbage = collectgarbage
local defos_set_window_title = defos.set_window_title
local lfs_attributes = lfs.attributes
local string_format = string.format
local go_set = go.set -- Set tint / tint.w
local sys_set_update_frequency = sys.set_update_frequency
-- Environment
--
local CreateWish, CreateHint, CreateAnim, ViewerGUI
local Path, OpenWorksVolume = sys.get_save_file("Prototypia Works", "")
if sys.get_sys_info().system_name == "Darwin" then
OpenWorksVolume = function()
os.execute( "open " .. string.gsub(Path, "%s", "\\ ") )
end
else
OpenWorksVolume = function()
os.execute("explorer " .. Path)
end
end
-- Chart[Fumen] Context
--
local context_opened, context_ms = false, 0 -- Instead of mstime from audio
local hint_count = 0
local wu_max = 0 -- A Chart[Fumen] Param that needs to be acquired at the runtime
local wtint, htint, atint -- Tables of tint datas
local wgos, hgos, agos_l, agos_r -- Tables of Game Objects' URLs
local w_sprites, h_sprites, h_helpers, al_sprites, ar_sprites -- Tables of Sprite Components' URLs
local last_wu, last_hu, last_au = 0, 0, 0 -- Used to send "enable" and "disable" messages
local function EndContext()
-- Release Game Objects
--
local go_delete = go.delete
for i = 1, #wgos do go_delete(wgos[i]) end
for i = 1, #hgos do
go_delete(hgos[i])
go_delete(agos_l[i])
go_delete(agos_r[i])
end
-- Reset Variables
--
wtint, htint, atint = nil, nil, nil
wgos, hgos, agos_l, agos_r = nil, nil, nil, nil
w_sprites, h_sprites, h_helpers, al_sprites, ar_sprites = nil, nil, nil, nil, nil
last_wu, last_hu, last_au = 0, 0, 0
-- Clean Up
--
collectgarbage()
context_ms, hint_count, wu_max = 0, 0, 0
context_opened = false
FinalArf()
end
local function NewContext(buf)
local b, t, w, h = InitArf(buf, true)
-- Context Creation
--
if b then
-- Cache Functions Here to avoid upvalues exceed
--
local NewTable = Arf2.NewTable
local factory_create = factory.create
local vmath_vector4 = vmath.vector4
-- Create Tables
--
wgos, hgos, agos_l, agos_r = NewTable(w, 0), NewTable(h, 0), NewTable(h, 0), NewTable(h, 0)
w_sprites, h_sprites, h_helpers, al_sprites, ar_sprites = NewTable(w, 0), NewTable(h, 0), NewTable(h, 0), NewTable(h, 0), NewTable(h, 0)
wtint, htint, atint = NewTable(w, 0), NewTable(h, 0), NewTable(h, 0)
-- Fill Tables
--
for i = 1, w do
local W = factory_create(CreateWish)
wgos[i], w_sprites[i] = W, msg_url(Ar, W, Sprite)
msg_post(W, disable)
end
for i = 1, h do
local H = factory_create(CreateHint)
hgos[i], h_sprites[i], h_helpers[i] = H, msg_url(Ar, H, Sprite), msg_url(Ar, H, Helper)
msg_post(H, disable)
local AL = factory_create(CreateAnim)
agos_l[i], al_sprites[i] = AL, msg_url(Ar, AL, Sprite)
msg_post(AL, disable)
local AR = factory_create(CreateAnim)
agos_r[i], ar_sprites[i] = AR, msg_url(Ar, AR, Sprite)
msg_post(AR, disable)
htint[i] = vmath_vector4(0,0,0,1)
atint[i] = vmath_vector4(0,0,0,1)
end
-- Clean Up
--
hint_count, context_opened = t, true
collectgarbage()
return true
else
return false
end
end
local function UpdateContext(mstime)
if context_opened then
local hl, wu, hu, au = UpdateArf(mstime, wgos, hgos, agos_l, agos_r, wtint, htint, atint)
if not hl then
wu, hu, au = 0, 0, 0
end
-- Mutate tint
--
for i = 1, wu do go_set(w_sprites[i], tint_w, wtint[i]) end
for i = 1, hu do
local helper_tintw = htint[i].x * 1.73
go_set( h_helpers[i], tint_w, (helper_tintw>0.37) and 0.37 or helper_tintw )
go_set(h_sprites[i], tint, htint[i])
end
for i = 1, au do
go_set(al_sprites[i], tint, atint[i])
go_set(ar_sprites[i], tint, atint[i])
end
-- Enables & Disables
--
if wu > last_wu then
if wu > wu_max then
wu_max = wu
end
for i = last_wu+1, wu do
local wsi = w_sprites[i]
if wsi then
msg_post(wsi, enable)
end
end
elseif wu < last_wu then
for i = wu+1, last_wu do
local wsi = w_sprites[i]
if wsi then
msg_post(wsi, disable)
end
end
end
if hu > last_hu then
for i = last_hu+1, hu do
local hgi = hgos[i]
if hgi then
msg_post(hgi, enable)
end
end
elseif hu < last_hu then
for i = hu+1, last_hu do
local hgi = hgos[i]
if hgi then
msg_post(hgi, disable)
end
end
end
if au > last_au then
for i = last_au+1, au do
local asli = al_sprites[i]
if asli then
msg_post(asli, enable)
msg_post(ar_sprites[i], enable)
end
end
elseif au < last_au then
for i = au+1, last_au do
local asli = al_sprites[i]
if asli then
msg_post(asli, disable)
msg_post(ar_sprites[i], disable)
end
end
end
-- Update Caches
--
last_wu, last_hu, last_au = wu, hu, au
end
end
-- Status & Script Callbacks
--
local FumenModification = 0 -- Reset it to 0 when loading a new Chart[Fumen]
local AudioRes, Audio, AudioLen, FumenPath -- Nullify them when exception happens
local function write_license()
local licensefile = io.open( Path .. "LICENSE.viewer", "w" )
licensefile:write( sys.load_resource("/LICENSE.viewer") )
licensefile:close()
end
function init(self)
-- Initial Settings
--
defos.set_window_size(nil, nil, 1200, 720)
defos_set_window_title( "Prototypia Viewer -- " .. Path )
msg_post(".", "acquire_input_focus")
collectgarbage("stop")
-- URL Preparations
--
local p, v = hash("Prototypia"), hash("/Viewer")
CreateWish = msg_url( p, v, hash("CreateWish") )
CreateHint = msg_url( p, v, hash("CreateHint") )
CreateAnim = msg_url( p, v, hash("CreateAnim") )
ViewerGUI = msg_url( p, v, hash("UI") )
-- Write Licenses & Do Energy-Saving Strategies
--
pcall(write_license)
sys_set_update_frequency(15)
collectgarbage("setstepmul", 1)
collectgarbage("setpause", 1048576)
end
local last_ms = 0 -- No need to clear this manually
function update(self, dt)
if Audio and context_opened then
-- Sync Context Time if Audio is Playing
--
if CheckPlaying(Audio) then
context_ms = GetTime(Audio) -- There will be an offset value added
end
-- Update Context
--
if context_ms ~= last_ms then
UpdateContext(context_ms)
msg_post( ViewerGUI, GUPDATE, {string_format(msfmt, context_ms, AudioLen)} )
defos_set_window_title( string_format(titlefmt, wu_max, FumenPath, hint_count) )
last_ms = context_ms
end
end
end
local scrolling = 0
function fixed_update(self, dt) -- File listening & GC
if FumenPath then
local mdf = lfs_attributes(FumenPath, modification)
if mdf then -- mdf == nil when the *.ar file is deleted
if mdf ~= FumenModification then
local _, buf = pcall(sys.load_buffer, FumenPath)
if buf then
if context_opened then
EndContext()
else
FinalArf()
end
local _, ok = pcall(NewContext, buf)
if ok then
UpdateContext(last_ms)
defos_set_window_title( string_format(titlefmt, wu_max, FumenPath, hint_count) )
FumenModification = mdf
context_ms = last_ms -- context_ms will be set to 0 when calling EndContext()
else
if context_opened then
EndContext()
else
FinalArf()
end
AcAudio.ReleaseUnit(Audio)
AcAudio.ReleaseResource(AudioRes)
msg_post( ViewerGUI, GUPDATE, {"[!] Fatal error happened when syncing the chart[fumen]"} )
AudioRes, Audio, AudioLen = nil, nil, 0
FumenModification = 0
FumenPath = nil
end
else
msg_post( ViewerGUI, GUPDATE, {"[*] File syncing failed"} )
FumenModification = mdf
end
end
end
end
if Audio then
if CheckPlaying(Audio) then
sys_set_update_frequency(0)
else
msg_post(ViewerGUI, SHOW_PLAY)
if scrolling == 1 then
sys_set_update_frequency(0)
scrolling = 2
elseif scrolling == 2 then
scrolling = 0
else
sys_set_update_frequency(15)
end
end
end
collectgarbage()
end
------------ Update: Play Later ------------
local WAIT_SEC = 0.05
local pWaiting, pHandle = false
local function play_later()
AcAudio.PlayUnit(Audio, false) -- Non-looping Play
if CheckPlaying(Audio) then
msg_post(ViewerGUI, SHOW_PAUSE)
else
msg_post(ViewerGUI, SHOW_PLAY)
end
pWaiting, pHandle = false, nil
end
----------------------------------------------
local msg_functions = {
[GUI_LOAD] = function(self, message) -- message -> {basename_str}
-- Free the Previous Stuff
--
if Audio then
AcAudio.ReleaseUnit(Audio)
AcAudio.ReleaseResource(AudioRes)
AudioRes, Audio = nil, nil
end
FumenPath = nil
AudioLen, FumenModification = 0, 0
if context_opened then
EndContext()
else
FinalArf()
end
-- Load Audio
--
local wav_path = sys.get_save_file("Prototypia Works", message[1] .. ".wav")
local wav_attr = lfs_attributes(wav_path)
if wav_attr then
local _, audiobuf = pcall(sys.load_buffer, wav_path)
if audiobuf then
local res_ok, res_ret = AcAudio.CreateResource(audiobuf)
if res_ok then
local unit_ok, unit, len = AcAudio.CreateUnit(res_ret)
if unit_ok then
AudioRes, Audio, AudioLen = res_ret, unit, len
else
AcAudio.ReleaseResource(res_ret)
msg_post(ViewerGUI, GUPDATE, {res_ret.." (wav)"})
end
else
msg_post(ViewerGUI, GUPDATE, {res_ret.." (wav)"})
end
else
msg_post( ViewerGUI, GUPDATE, {string_format("[!] Failed to load \"%s.wav\"", message[1])} )
end
else
local mp3_path = sys.get_save_file("Prototypia Works", message[1] .. ".mp3")
local mp3_attr = lfs_attributes(mp3_path)
if mp3_attr then
local _, audiobuf = pcall(sys.load_buffer, mp3_path)
if audiobuf then
local res_ok, res_ret = AcAudio.CreateResource(audiobuf)
if res_ok then
local unit_ok, unit, len = AcAudio.CreateUnit(res_ret)
if unit_ok then
AudioRes, Audio, AudioLen = res_ret, unit, len
else
AcAudio.ReleaseResource(res_ret)
msg_post(ViewerGUI, GUPDATE, {res_ret.." (mp3)"})
end
else
msg_post(ViewerGUI, GUPDATE, {res_ret.." (mp3)"})
end
else
msg_post( ViewerGUI, GUPDATE, {string_format("[!] Failed to load \"%s.mp3\"", message[1])} )
end
else
msg_post( ViewerGUI, GUPDATE, {string_format("[!] Both of file \"%s.wav\" and \"%s.mp3\" don't exist", message[1], message[1])} )
end
end
-- Load Context
--
if Audio then
local context_path = sys.get_save_file("Prototypia Works", message[1] .. ".ar")
local context_attr = lfs_attributes(context_path)
if context_attr then
local _, fmbuf = pcall(sys.load_buffer, context_path)
if fmbuf then
local _, ok = pcall(NewContext, fmbuf)
if ok then
--
-- File Listening Set Here
--
FumenPath = context_path
FumenModification = context_attr.modification
else
if context_opened then
EndContext()
else
FinalArf()
end
AcAudio.ReleaseUnit(Audio)
AcAudio.ReleaseResource(AudioRes)
AudioRes, Audio, AudioLen = nil, nil, 0
msg_post( ViewerGUI, GUPDATE, {string_format("[!] Failed to parse \"%s.ar\"", message[1])} )
end
else
AcAudio.ReleaseUnit(Audio)
AcAudio.ReleaseResource(AudioRes)
AudioRes, Audio, AudioLen = nil, nil, 0
msg_post( ViewerGUI, GUPDATE, {string_format("[!] Failed to load \"%s.ar\"", message[1])} )
end
else
AcAudio.ReleaseUnit(Audio)
AcAudio.ReleaseResource(AudioRes)
AudioRes, Audio, AudioLen = nil, nil, 0
msg_post( ViewerGUI, GUPDATE, {string_format("[!] File \"%s.ar\" doesn't exist", message[1])} )
end
end
-- Initial Update
--
if context_opened then
UpdateContext(0)
msg_post( ViewerGUI, GUPDATE, {string_format(msfmt, 0, AudioLen)} )
defos_set_window_title( string_format(titlefmt, wu_max, FumenPath, hint_count) )
last_ms = 0
else
defos_set_window_title( "Prototypia Viewer -- " .. Path )
context_ms = 0
last_ms = 0
end
collectgarbage()
end,
[GUI_SEEK] = function(self, message) -- message -> {mstime_str}
if Audio and ( not CheckPlaying(Audio) ) then
local ok, t = pcall(tonumber, message[1])
if ok then
t = (t>0) and t or 0
context_ms = (t<AudioLen-10) and t or AudioLen-10
else
msg_post( ViewerGUI, GUPDATE, {"[*] Mstime input invalid"} )
end
else
msg_post( ViewerGUI, GUPDATE, {"[*] Before seeking somewhere, you need to load a chart[fumen]"} )
end
end,
[GUI_PLAY] = function()
if Audio then
if CheckPlaying(Audio) then
AcAudio.StopUnit(Audio, false) -- Pause
msg_post(ViewerGUI, SHOW_PLAY)
elseif not pb_waiting then
AcAudio.SetTime(Audio, context_ms)
pHandle = timer.delay(WAIT_SEC, false, play_later)
pWaiting = true
end
else
msg_post( ViewerGUI, GUPDATE, {"[*] Before playing, you need to load a chart[fumen]"} )
end
end,
[GUI_OWV] = OpenWorksVolume
}
function on_message(self, message_id, message)
if msg_functions[message_id] then
msg_functions[message_id](self, message)
end
end
local sp_init, ctrl_pressed, alt_pressed = nil, false, false -- No need to clear this manually
local input_functions = {
[BUTTON_P] = function(self, action)
if (ctrl_pressed or alt_pressed) and action.pressed then
if Audio then
if CheckPlaying(Audio) then
AcAudio.StopUnit(Audio, false) -- Pause
msg_post(ViewerGUI, SHOW_PLAY)
elseif not pb_waiting then
AcAudio.SetTime(Audio, context_ms)
timer.delay(WAIT_SEC, false, play_later)
pWaiting = true
end
else
msg_post( ViewerGUI, GUPDATE, {"[*] Before playing, you need to load a chart[fumen]"} )
end
end
end,
[BUTTON_SP] = function(self, action) -- Unstable
if action.released then
if Audio and sp_init then
AcAudio.StopUnit(Audio, false) -- Audio Reset
AcAudio.SetTime(Audio, sp_init)
if pHandle then -- Handle Reset
timer.cancel(pHandle)
pWaiting, pHandle = false, nil
end
context_ms = sp_init -- Other Reset
msg_post(ViewerGUI, SHOW_PLAY)
end
sp_init = nil
elseif (ctrl_pressed or alt_pressed) and action.pressed then
if Audio and ( not CheckPlaying(Audio) ) then
sp_init = context_ms
AcAudio.SetTime(Audio, context_ms)
timer.delay(WAIT_SEC, false, play_later)
pWaiting = true
end
end
end,
[SCROLL_UP] = function(self, action)
if action.value == 1 then
if Audio and ( not CheckPlaying(Audio) ) then
local t = (ctrl_pressed or alt_pressed) and (context_ms - 370) or (context_ms - 37)
t = (t>0) and t or 0
context_ms = (t<AudioLen-10) and t or (AudioLen-10)
scrolling = 1
end
end
end,
[BUTTON_UP] = function(self, action)
if action.pressed or action.repeated then
if Audio and ( not CheckPlaying(Audio) ) then
local t = (ctrl_pressed or alt_pressed) and (context_ms - 370) or (context_ms - 37)
t = (t>0) and t or 0
context_ms = (t<AudioLen-10) and t or (AudioLen-10)
end
end
end,
[SCROLL_DOWN] = function(self, action)
if action.value == 1 then
if Audio and ( not CheckPlaying(Audio) ) then
local t = (ctrl_pressed or alt_pressed) and (context_ms + 370) or (context_ms + 37)
t = (t>0) and t or 0
context_ms = (t<AudioLen-10) and t or (AudioLen-10)
scrolling = 1
end
end
end,
[BUTTON_DOWN] = function(self, action)
if action.pressed or action.repeated then
if Audio and ( not CheckPlaying(Audio) ) then
local t = (ctrl_pressed or alt_pressed) and (context_ms + 370) or (context_ms + 37)
t = (t>0) and t or 0
context_ms = (t<AudioLen-10) and t or (AudioLen-10)
end
end
end,
[BUTTON_C] = function(self, action)
if action.released then
ctrl_pressed = false
elseif action.pressed then
ctrl_pressed = true
end
end,
[BUTTON_A] = function(self, action)
if action.released then
alt_pressed = false
elseif action.pressed then
alt_pressed = true
end
end
}
function on_input(self, action_id, action)
if action_id and input_functions[action_id] then
input_functions[action_id](self, action)
elseif action_id == touch and action.released then
if context_opened then
msg_post( ViewerGUI, GUPDATE, {string_format(msfmt, last_ms, AudioLen)} )
else
msg_post( ViewerGUI, GUPDATE, {"[MS] 0 · 0"} )
end
end
end