-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewer.script
518 lines (451 loc) · 14.7 KB
/
Viewer.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
-- Prototypia Viewer[1.x Branch] Main Logics
-- Copyright(c) 2024- 1dealGas, under the Apache 2.0 License.
------------------------------------
-- Stuff to Manage:
-- Audio Context
-- Chart[Fumen] Context
-- Fumen File(*.lua/*.arf/*.ar) Listening
-- Message / PlayPause Button
-- Nemesis Context
------------------------------------
-- L0: Caches
--
local hash = hash
local go_set = go.set
local msg_url, msg_post = msg.url, msg.post
local defos_set_window_title = defos.set_window_title
local sys_set_update_frequency = sys.set_update_frequency
local lfs_attributes = lfs.attributes
local string_format = string.format
local GetTime = AcAudio.GetTime
local CheckPlaying = AcAudio.CheckPlaying
local UpdateArf = Arf3.UpdateArf
local SetCam = Arf3.SetCam
-- Hash & String Caches
--
local CreateWish, CreateHint, CreateEcho, CreateAnim, ViewerUI
local GUI_NEW_MSG, GUI_SHOW_PLAY, GUI_SHOW_PAUSE = hash("GUI_NEW_MSG"), hash("GUI_SHOW_PLAY"), hash("GUI_SHOW_PAUSE")
local Enable, Disable, Touch = hash("enable"), hash("disable"), hash("touch")
local Sprite, tint_w, tint = hash("Sprite"), hash("tint.w"), hash("tint")
local Prototypia = hash("Prototypia")
local PrototypiaWorks = "Prototypia Works"
local PrototypiaWorksPath = sys.get_save_file(PrototypiaWorks, "")
local titlefmt, compilingfmt = "[%d][%d] Prototypia Viewer -- %s -- %d Objects", "[Compiling] Prototypia Viewer -- %s"
local msfmt, msbarfmt = "[#] %d(%d ms)", "[#] %d(%d ms) · %d(%d Bar)"
local nemesis_error = "[*] Nemesis Compiler Error:\n"
local modification = "modification"
-- L1: Context
--
local Before, ObjCount = 0, 0
local WGOs, HGOs, EGOs, AlGOs, ArGOs
local WishSprites, HintSprites, EchoSprites, AlSprites, ArSprites
local WishTints, HintTints, EchoTints, AnimTints
local XScale, YScale, XDelta, YDelta, Rotdeg = 1, 1, 0, 0, 0
local LastWgoUsed, LastHgoUsed, LastEgoUsed, LastAgoUsed = 0, 0, 0, 0
local ContextMs, CurrentAudioRes, CurrentAudioUnit
-- Viewer-Specific Context
-- Global: Message, UserInput
--
local LastMs = 0
local AudioLen = 0 -- Adjust Before to AudioLen if Before>AudioLen
local ContextType = false -- false(Not Opened), 0(*.ar), 1(*.arf), 2(*.lua Nemesis User Script)
local FumenPath, FumenModification
local MaxWU, MaxEU, BarBefore
-- Temporary Stuff
-- No need to manually maintain them.
--
local PlayTimerHandle
local CtrlPressed, AltPressed = false, false
local ScrollState
-- L2: Context Methods
--
local function CloseAudioContext() -- No memory leaking risk; don't manually call this outside the OpenAudioContext() func.
AudioLen = 0
CurrentAudioUnit = AcAudio.ReleaseUnit(CurrentAudioUnit) and nil
CurrentAudioRes = AcAudio.ReleaseResource(CurrentAudioRes) and nil
end
local function CloseFumenContext(deep)
-- Clear Scalars
--
Before, ObjCount, MaxWU, MaxEU, BarBefore = 0, 0, 0, 0, 0
LastMs, LastWgoUsed, LastHgoUsed, LastEgoUsed, LastAgoUsed = 0, 0, 0, 0, 0
XScale, YScale, XDelta, YDelta, Rotdeg = 1, 1, 0, 0, 0
if deep then
FumenPath, FumenModification = nil, nil
ContextType, ContextMs = false, 0
end
-- Do Other Work
-- No need to manually nullify tables.
--
local go_delete = go.delete
for i = 1, #WGOs do go_delete(WGOs[i]) end
for i = 1, #HGOs do go_delete(HGOs[i]) end
for i = 1, #EGOs do go_delete(EGOs[i]) end
for i = 1, #AlGOs do go_delete(AlGOs[i]) go_delete(ArGOs[i]) end
Arf3.FinalArf()
end
local function OpenAudioContext()
local base_path = sys.get_save_file(PrototypiaWorks, Message)
local CLEARUP = CloseAudioContext, CloseAudioContext()
-- Try *.opus first.
--
local current_path = base_path .. ".opus"
if lfs_attributes(current_path) then
local OK, buf = pcall(sys.load_buffer, current_path)
if OK then
OK, CurrentAudioRes = AcAudio.CreateResource(buf)
if OK then
OK, CurrentAudioUnit, AudioLen = AcAudio.CreateUnit(CurrentAudioRes)
if OK then
return true
end
end
end
end
-- Try *.ogg then.
--
CLEARUP()
current_path = base_path .. ".ogg"
if lfs_attributes(current_path) then
local OK, buf = pcall(sys.load_buffer, current_path)
if OK then
OK, CurrentAudioRes = AcAudio.CreateResource(buf)
if OK then
OK, CurrentAudioUnit, AudioLen = AcAudio.CreateUnit(CurrentAudioRes)
if OK then
return true
end
end
end
end
-- Try *.wav for the last.
--
CLEARUP()
current_path = base_path .. ".wav"
if lfs_attributes(current_path) then
local OK, buf = pcall(sys.load_buffer, current_path)
if OK then
OK, CurrentAudioRes = AcAudio.CreateResource(buf)
if OK then
OK, CurrentAudioUnit, AudioLen = AcAudio.CreateUnit(CurrentAudioRes)
if OK then
return true
end
end
end
end
CLEARUP()
return false, "[!] Audio Loading Failed."
end
local function LoadFumenContext() -- Considering Both Opening & Reloading Usages.
local current_modification = lfs_attributes(FumenPath, modification)
if current_modification then
local OK, buf, wgo_required, hgo_required, ego_required
if ContextType < 0 then -- *.ar
OK, buf = pcall(sys.load_buffer, FumenPath)
if buf then
OK, Before, ObjCount, wgo_required, hgo_required, ego_required = pcall(Arf3.InitArf2, buf, true)
end
elseif ContextType == 1 then -- *.arf
OK, buf = pcall(sys.load_buffer, FumenPath)
if buf then
OK, Before, ObjCount, wgo_required, hgo_required, ego_required = pcall(Arf3.InitArf3, buf, true)
end
else -- *.lua
defos_set_window_title( string_format(compilingfmt, FumenPath) )
OK, Before, ObjCount, wgo_required, hgo_required, ego_required = pcall(NemesisCompile, FumenPath)
if OK then
BarBefore = MsToBar(Before) or 0
else
CloseFumenContext(true)
return false, nemesis_error .. Before -- The 2nd arg is the error message here.
end
end
if OK then
local ago_required = hgo_required + ego_required
local nt, vmath_vector4, factory_create = Arf3.NewTable, vmath.vector4, factory.create
-- Create Tables
--
WGOs, HGOs, EGOs, AlGOs, ArGOs = nt(wgo_required, 0), nt(hgo_required, 0), nt(ego_required, 0), nt(ago_required, 0), nt(ago_required, 0)
WishSprites, HintSprites, EchoSprites, AlSprites, ArSprites = nt(wgo_required, 0), nt(hgo_required, 0), nt(ego_required, 0), nt(ago_required, 0), nt(ago_required, 0)
WishTints, HintTints, EchoTints, AnimTints = nt(wgo_required, 0), nt(hgo_required, 0), nt(ego_required, 0), nt(ago_required, 0)
-- Fill Tables
--
for i = 1, wgo_required do
local W = factory_create(CreateWish)
WGOs[i], WishSprites[i] = W, msg_url(Prototypia, W, Sprite)
msg_post(W, Disable)
end
for i = 1, hgo_required do
local H = factory_create(CreateHint)
HGOs[i], HintSprites[i], HintTints[i] = H, msg_url(Prototypia, H, Sprite), vmath_vector4(0,0,0,1)
msg_post(H, Disable)
end
for i = 1, ego_required do
local E = factory_create(CreateEcho)
EGOs[i], EchoSprites[i], EchoTints[i] = E, msg_url(Prototypia, E, Sprite), vmath_vector4()
msg_post(E, Disable)
end
for i = 1, ago_required do
local AL, AR = factory_create(CreateAnim), factory_create(CreateAnim)
AlGOs[i], ArGOs[i], AlSprites[i], ArSprites[i], AnimTints[i] = AL, AR, msg_url(Prototypia, AL, Sprite), msg_url(Prototypia, AR, Sprite), vmath_vector4()
msg_post(AL, Disable) msg_post(AR, Disable)
end
FumenModification = current_modification
return true
else
CloseFumenContext(true) -- For error handling usages.
return false, "[!] Fumen Loading Failed."
end
end
CloseFumenContext(true) -- For error handling usages.
return false, "[!] Fumen Loading Failed."
end
local function OpenFumenContext()
local base_path = sys.get_save_file(PrototypiaWorks, Message)
CloseFumenContext(true)
-- Try *.lua first.
--
FumenPath = base_path .. ".lua"
if lfs_attributes(FumenPath) then
ContextType = 2
return LoadFumenContext()
end
-- Try *.arf then.
--
FumenPath = base_path .. ".arf"
if lfs_attributes(FumenPath) then
ContextType = 1
return LoadFumenContext()
end
-- Try *.ar for the last.
--
FumenPath = base_path .. ".ar"
if lfs_attributes(FumenPath) then
ContextType = 0
return LoadFumenContext()
end
return false, "[!] Fumen Loading Failed."
end
local function UpdateFumenContext()
if ContextType then
SetCam(XScale, YScale, XDelta, YDelta, Rotdeg)
local _,_, wgo_used, hgo_used, ego_used, ago_used = UpdateArf(ContextMs, WGOs, HGOs, EGOs, AlGOs, ArGOs, WishTints, HintTints, EchoTints, AnimTints)
-- Mutate Tints
--
for i = 1, wgo_used do go_set(WishSprites[i], tint_w, WishTints[i]) end
for i = 1, hgo_used do go_set(HintSprites[i], tint, HintTints[i]) end
for i = 1, ego_used do go_set(EchoSprites[i], tint, EchoTints[i]) end
for i = 1, ago_used do
go_set(AlSprites[i], tint, AnimTints[i])
go_set(ArSprites[i], tint, AnimTints[i])
end
-- Enable & Disable Components
--
if wgo_used > LastWgoUsed then
for i = LastWgoUsed+1, wgo_used do msg_post(WishSprites[i], Enable) end
if wgo_used > MaxWU then MaxWU = wgo_used end
elseif wgo_used < LastWgoUsed then
for i = wgo_used+1, LastWgoUsed do
msg_post(WishSprites[i], Disable)
end
end
if hgo_used > LastHgoUsed then
for i = LastHgoUsed+1, hgo_used do
msg_post(HGOs[i], Enable)
end
elseif hgo_used < LastHgoUsed then
for i = hgo_used+1, LastHgoUsed do
msg_post(HGOs[i], Disable)
end
end
if ego_used > LastEgoUsed then
for i = LastEgoUsed+1, ego_used do msg_post(EGOs[i], Enable) end
if ego_used > MaxEU then MaxEU = ego_used end
elseif ego_used < LastEgoUsed then
for i = ego_used+1, LastEgoUsed do
msg_post(EGOs[i], Disable)
end
end
if ago_used > LastAgoUsed then
for i = LastAgoUsed+1, ago_used do
msg_post(AlSprites[i], Enable)
msg_post(ArSprites[i], Enable)
end
elseif ago_used < LastAgoUsed then
for i = ago_used+1, LastAgoUsed do
msg_post(AlSprites[i], Disable)
msg_post(ArSprites[i], Disable)
end
end
LastWgoUsed, LastEgoUsed = wgo_used, ego_used
LastHgoUsed, LastAgoUsed = hgo_used, ago_used
end
end
local function ShowTime()
if ContextType then
if ContextType < 2 then
Message = string.format(msfmt, ContextMs, Before)
else
Message = string.format(msbarfmt, ContextMs, Before, MsToBar(ContextMs) or 0, BarBefore)
end
else
Message = "[#] 0(0 ms)"
end
msg_post(ViewerUI, GUI_NEW_MSG)
end
-- L3: Lifecycle Funcs
--
function init()
-- Initial Settings
--
defos.set_window_size(nil, nil, 1200, 720)
defos_set_window_title( "Prototypia Viewer -- " .. PrototypiaWorksPath )
collectgarbage("stop")
-- URL Preparations
--
local Viewer = hash("/Viewer")
CreateWish = msg_url(Prototypia, Viewer, hash "CreateWish")
CreateHint = msg_url(Prototypia, Viewer, hash "CreateHint")
CreateEcho = msg_url(Prototypia, Viewer, hash "CreateEcho")
CreateAnim = msg_url(Prototypia, Viewer, hash "CreateAnim")
ViewerUI = msg_url(Prototypia, Viewer, hash "ViewerUI")
-- Write Licenses
--
local licensefile = io.open( PrototypiaWorksPath .. "Viewer.license", "w" )
if licensefile then
licensefile:write( sys.load_resource("/Viewer.license") )
licensefile:close()
end
-- Do Energy-Saving Strategies
--
sys_set_update_frequency(15)
collectgarbage("setstepmul", 1)
collectgarbage("setpause", 1048576)
end
function update()
if ContextType then
-- Sync Context Time if Audio is Playing
--
if CheckPlaying(CurrentAudioUnit) then
ContextMs = GetTime(CurrentAudioUnit) -- There will be an offset value added
ContextMs = ContextMs>Before and Before or ContextMs
end
-- Update Context
--
if ContextMs ~= LastMs then
UpdateFumenContext()
defos_set_window_title( string_format(titlefmt, MaxWU, MaxEU, FumenPath, ObjCount) )
LastMs = ContextMs
ShowTime()
end
end
end
function fixed_update()
-- File Listening
--
if ContextType then
local mdf = lfs_attributes(FumenPath, modification)
if mdf and mdf~=FumenModification then
local OK = CloseFumenContext(false) -- Shallow, for this is a file-listening behavior.
OK, Message = LoadFumenContext()
if OK then
FumenModification = mdf
defos_set_window_title( string_format(titlefmt, MaxWU, MaxEU, FumenPath, ObjCount) )
UpdateFumenContext()
else
CloseFumenContext(true)
msg_post(ViewerUI, GUI_NEW_MSG)
end
end
end
-- Energy-Saving Strategy & GC
--
if CurrentAudioUnit then
if CheckPlaying(CurrentAudioUnit) then
sys_set_update_frequency(0)
else
msg_post(ViewerUI, GUI_SHOW_PLAY)
if ScrollState == 1 then
sys_set_update_frequency(0)
ScrollState = 2
elseif ScrollState == 2 then
ScrollState = 0
else
sys_set_update_frequency(15)
end
end
end
collectgarbage()
end
-- L4: User Actions
--
local Actions = { -- WIP
[hash "BUTTON_C"] = function(action)
if action.released then
CtrlPressed = false
elseif action.pressed then
CtrlPressed = true
end
end,
[hash "BUTTON_A"] = function(action)
if action.released then
AltPressed = false
elseif action.pressed then
AltPressed = true
end
end,
[hash "SCROLL_UP"] = function(action)
if action.value == 1 then
if CurrentAudioUnit and not CheckPlaying(CurrentAudioUnit) then
local t = (CtrlPressed or AltPressed) and (ContextMs - 370) or (ContextMs - 37)
ContextMs = (t>0) and t or 0
ScrollState = 1
end
end
end,
[hash "SCROLL_DOWN"] = function(action)
if action.value == 1 then
if CurrentAudioUnit and not CheckPlaying(CurrentAudioUnit) then
local t = (CtrlPressed or AltPressed) and (ContextMs + 370) or (ContextMs + 37)
ContextMs = (t<AudioLen-10) and t or (AudioLen-10)
ScrollState = 1
end
end
end,
[hash "GUI_LOAD"] = function()
local OK do OK, Message = OpenAudioContext() end
if OK then OK, Message = OpenFumenContext() end
if OK then
UpdateFumenContext() -- Initial Update
defos_set_window_title( string_format(titlefmt, MaxWU, MaxEU, FumenPath, ObjCount) )
ShowTime()
else
defos_set_window_title( "Prototypia Viewer -- " .. PrototypiaWorksPath )
msg_post(ViewerUI, GUI_NEW_MSG)
end
end,
[hash "GUI_SEEK_BAR"] = nil,
[hash "GUI_SEEK_MS"] = nil,
[hash "GUI_XD"] = nil,
[hash "GUI_YD"] = nil,
[hash "GUI_XS"] = nil,
[hash "GUI_YS"] = nil,
[hash "GUI_DEG"] = nil,
[hash "GUI_EXPORT"] = nil,
[hash "GUI_OWV"] = (sys.get_sys_info().system_name == "Darwin")
and function() os.execute( "open " .. string.gsub( PrototypiaWorksPath, "%s", "\\ ") ) end
or function() os.execute( "explorer " .. PrototypiaWorksPath ) end
,
[hash "GUI_PLAY"] = nil
}
-- L5: User Action Dispatchers
--
local function Default(message) end
local function InputShowTime(action, action_id)
if action_id == Touch and action.released then
ShowTime()
end
end
function on_message(self, message_id, message) (Actions[message_id] or Default)(message) end
function on_input(self, action_id, action) (Actions[action_id] or InputShowTime)(action, action_id) end