-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathThemeRepo.pluto
538 lines (451 loc) · 13.9 KB
/
ThemeRepo.pluto
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
-- Auto Updater from https://github.com/hexarobi/stand-lua-auto-updater
local status, auto_updater = pcall(require, 'auto-updater')
if not status then
local auto_update_complete = nil
util.toast('Installing auto-updater...', TOAST_ALL)
async_http.init('raw.githubusercontent.com', '/hexarobi/stand-lua-auto-updater/main/auto-updater.lua',
function(result, headers, status_code)
local function parse_auto_update_result(result, headers, status_code)
local error_prefix = 'Error downloading auto-updater: '
if status_code ~= 200 then
util.toast(error_prefix .. status_code, TOAST_ALL)
return false
end
if not result or result == '' then
util.toast(error_prefix .. 'Found empty file.', TOAST_ALL)
return false
end
filesystem.mkdir(filesystem.scripts_dir() .. 'lib')
local file = io.open(filesystem.scripts_dir() .. 'lib\\auto-updater.lua', 'wb')
if file == nil then
util.toast(error_prefix .. 'Could not open file for writing.', TOAST_ALL)
return false
end
file:write(result)
file:close()
util.toast('Successfully installed auto-updater lib', TOAST_ALL)
return true
end
auto_update_complete = parse_auto_update_result(result, headers, status_code)
end, function()
util.toast('Error downloading auto-updater lib. Update failed to download.', TOAST_ALL)
end)
async_http.dispatch()
local i = 1
while (auto_update_complete == nil and i < 40) do
util.yield(250)
i = i + 1
end
if auto_update_complete == nil then
error('Error downloading auto-updater lib. HTTP Request timeout')
end
auto_updater = require('auto-updater')
end
if auto_updater == true then
error('Invalid auto-updater lib. Please delete your Stand/Lua Scripts/lib/auto-updater.lua and try again')
end
local auto_update_cfg = {
source_url = 'https://raw.githubusercontent.com/stagnate6628/stand-themerepo/main/ThemeRepo.pluto',
script_relpath = SCRIPT_RELPATH,
script_run_name = SCRIPT_NAME:lower(),
}
auto_updater.run_auto_update(auto_update_cfg)
local async_http, filesystem, io, menu, util = async_http, filesystem, io, menu, util
local root = menu.my_root()
local SCRIPT_VERSION = '1.5.3'
if not SCRIPT_SILENT_START then
if not async_http.have_access() then
util.toast('Internet access is disabled for this script, functionality is limited.')
end
end
local function get_github_auth()
if file := io.open(filesystem.resources_dir() .. 'ThemeRepo\\.github', 'r') then
local token = file:read('a')
file:close()
return token:startswith('ghp_') and token:len() == 40 and token
end
return nil
end
local function fetch(host, path, callback)
if not async_http.have_access() then
return
end
local finished = false
async_http.init(host, path, function(body, headers, status_code)
if status_code == 403 then
local toast_msg = 'You are currently being ratelimited by Github.'
if headers['X-Ratelimit-Reset'] then
local expire_at = os.date('%d-%m %H:%M', headers['X-Ratelimit-Reset'])
toast_msg ..= $"\nThis will expire at {expire_at}."
end
util.log(toast_msg)
for i = 1, 5 do
util.toast(toast_msg)
end
util.stop_script()
elseif status_code == 404 then
goto continue
end
pcall(callback, body, headers, status_code)
::continue::
finished = true
end, function()
util.toast($"Request to {host}{path} failed to send.", TOAST_ALL)
finished = true
end)
if token := get_github_auth() then
async_http.add_header('Authorization', $"Bearer {token}")
end
async_http.dispatch()
repeat
util.yield()
until finished
end
local fs_log = false
local function make_request(path, callback)
fetch(
'https://api.github.com',
$"/repos/stagnate6628/stand-themerepo/contents/{path}",
function(body, headers, status_code)
pcall(callback, body, headers, status_code)
end
)
end
local function get_file_name(path, use_backslashes = false)
local regex = '.+/([^/]+)$'
if use_backslashes then
regex = '.+\\([^\\]+)$'
end
return path:match(regex)
end
local og_util_log = util.log
util.log = function(msg)
if not fs_log then
return
end
og_util_log(msg:gsub(filesystem.appdata_dir(), ''))
end
local og_io_copyto = io.copy
io.copy = function (from, to)
if not filesystem.is_dir(to:match('^(.+)[\\/]')) then
filesystem.mkdirs(to:match('^(.+)[\\/]'))
end
local file_name = get_file_name(from, true)
og_io_copyto(from, to)
util.log($"Copying {file_name} to {to}")
end
local function write_file(path, body)
if not filesystem.is_dir(path:match('^(.+)[\\/]')) then
filesystem.mkdirs(path:match('^(.+)[\\/]'))
end
local file = assert(io.open(path, 'wb'))
file:write(body)
file:close()
end
local function download_file(host, path, file_path, callback)
fetch(host, path, function(body, headers, status_code)
if type(file_path) == 'string' then
file_path = {file_path}
end
local file_name = get_file_name(path)
for file_path as _path do
write_file(_path, body)
util.log($"Downloaded {file_name} to {_path}")
end
end)
end
local function empty_dir(dir)
if not filesystem.is_dir(dir) then
util.log($"Directory {dir} not found, skipping.")
return
end
util.log($"Emptying {dir} (%{#filesystem.list_files(dir)})")
io.remove(dir, true)
repeat
util.yield()
until not filesystem.is_dir(dir)
assert(
not filesystem.is_dir(dir),
'Failed to empty dir ' .. dir:gsub(filesystem.appdata_dir(), '')
)
util.yield(100)
end
local stand_dir = filesystem.stand_dir()
local header_dir = $"{stand_dir}Headers\\Custom Header\\"
local theme_dir = $"{stand_dir}Theme\\"
local custom_dir = $"{theme_dir}Custom\\"
local tabs_dir = $"{theme_dir}Tabs\\"
local resources_dir = $"{filesystem.resources_dir()}ThemeRepo\\"
local themes_dir = $"{resources_dir}Themes\\"
local headers_dir = $"{resources_dir}Headers\\"
local display_download_status = true
local is_downloading = false
local function reload_textures()
menu.ref_by_path('Stand>Settings>Appearance>Textures>Reload Textures'):trigger()
end
local function reload_font()
menu.ref_by_path('Stand>Settings>Appearance>Font & Text>Reload Font'):trigger()
end
local function header_hide()
menu.ref_by_path('Stand>Settings>Appearance>Header>Header>Be Gone'):trigger()
end
local function header_custom()
menu.ref_by_path('Stand>Settings>Appearance>Header>Header>Custom'):trigger()
end
local function get_all_files(path, files = {})
make_request(path, function(body, headers, status_code)
body = soup.json.decode(body)
for body as file do
if file.type == 'file' then
table.insert(files, file.path)
continue
end
get_all_files(file.path, files)
end
end)
return files
end
local function get_manifest(path, type, menu_name)
local files = {}
path ..= $"{menu_name}\\.manifest."
if not filesystem.is_regular_file(path) then
files = get_all_files($"{type}/{menu_name}")
write_file(path, table.concat(files, '\n'))
util.log($"Creating manifest for \"{menu_name}\" at {path}")
else
local file = io.open(path, 'r')
files = file:read('*a'):split('\n')
file:close()
util.log($"Found manifest for \"{menu_name}\" at {path}")
end
return files
end
local function load_theme(theme_name)
util.toast($"Loading \"{theme_name}\".", TOAST_ALL)
reload_textures()
reload_font()
menu.ref_by_path('Stand'):refByRelPath('Profiles'):trigger()
util.yield(100)
menu.ref_by_path('Stand'):trigger()
util.yield(100)
menu.ref_by_path('Stand'):refByRelPath('Profiles'):trigger()
menu.ref_by_path('Stand'):refByRelPath('Lua Scripts'):trigger()
util.yield(100)
menu.ref_by_path('Stand'):trigger()
util.yield(100)
menu.ref_by_path('Stand'):refByRelPath('Lua Scripts'):trigger()
util.yield(100)
local ref = menu.ref_by_path($"Stand>Profiles>{theme_name}")
if not ref:isValid() then
util.toast($"Profile {theme_name} could not be found.", TOAST_ALL)
return
end
ref:refByRelPath('Load'):trigger()
ref:refByRelPath('Load'):trigger()
ref:refByRelPath('Active'):trigger()
util.yield(100)
util.toast('Done!')
end
local function download_theme(theme_name)
empty_dir(theme_dir)
empty_dir(header_dir)
header_hide()
local files = get_manifest(themes_dir, 'Themes', theme_name)
local i = 0
util.create_tick_handler(function()
if i == #files then
return false
end
if display_download_status then
util.draw_debug_text($"{theme_name}: {i}/{#files}")
end
return true
end)
for files as file do
i += 1
local base_path = file:gsub('/', '\\')
local local_path = base_path:gsub('Themes\\(.-)\\', '')
local dir = local_path:match('^(.-)[\\/][^\\/]*$')
local paths = { $"{resources_dir}{base_path}" }
if dir and dir != 'Interaction Header' then
table.insert(paths, $"{stand_dir}{local_path}")
end
if filesystem.is_regular_file(paths[1]) then
if paths[2] then
io.copy(paths[1], paths[2])
end
continue
end
download_file(
'https://raw.githubusercontent.com',
$"/stagnate6628/stand-themerepo/main/{file}",
paths
)
end
load_theme(theme_name)
end
local function download_header(header_name)
empty_dir(header_dir)
header_hide()
local files = get_manifest(headers_dir, 'Headers', header_name)
local i = 0
util.create_tick_handler(function()
if i == #files then
return false
end
if display_download_status then
util.draw_debug_text($"{header_name}: {i}/{#files}")
end
return true
end)
for files as file do
i += 1
local base_path = file:gsub('/', '\\')
local paths = { $"{resources_dir}{base_path}" }
table.insert(paths, header_dir .. paths[1]:match('[^\\/]*$'))
if filesystem.is_regular_file(paths[1]) and not filesystem.is_regular_file(paths[2]) then
io.copy(paths[1], paths[2])
continue
end
download_file(
'https://raw.githubusercontent.com',
$"/stagnate6628/stand-themerepo/main/{file}",
paths
)
end
header_custom()
end
local all_children = {}
do
for { 'Headers', 'Themes' } as v do
local file_name = v:lower() .. '.txt'
local path = resources_dir .. file_name
if not filesystem.is_regular_file(path) then
util.toast($"Downloading missing file \"{file_name}\".", TOAST_ALL)
download_file(
'https://raw.githubusercontent.com',
$"/stagnate6628/stand-themerepo/main/{file_name}", path
)
end
local f = io.open(path, 'r')
local body = f:read('*a'):split('\n')
f:close()
local fn = v == 'Headers' ? download_header : download_theme
local ref = root:list_action(v, {}, '', body, function(_, menu_name)
if is_downloading then
util.toast('A download has already started. Restart the script if that is not the case.')
return
end
is_downloading = true
util.toast($"Downloading \"{menu_name}\".", TOAST_ALL)
fn(menu_name)
util.yield(100)
is_downloading = false
end)
for ref:getChildren() as child do
table.insert(all_children, child)
end
end
end
local config_root = root:list('Configuration', {}, '')
config_root:divider('General')
config_root:toggle('Download Status', {}, 'Displays a counter using the "Info Text" system to show what is being downloaded and the file count.', function(s)
display_download_status = s
end, display_download_status)
config_root:toggle('Filesystem Logging', {}, 'Filesystem calls (copying/writing/removing) are logged with their source and destination to Log.txt, respective to the action taken.', function(s)
fs_log = s
end, fs_log)
config_root:divider('Utilities')
local search_root = config_root:list('Search', {}, '')
search_root:text_input('Query', {'themereposearch'}, '', function(input, click_type)
if input != '' and table.contains({ 1, 4 }, click_type) != nil then
if not search_root:isFocused() then
search_root:trigger()
end
for search_root:getChildren() as child do
if child:getType() == COMMAND_INPUT then
continue
end
child:delete()
end
menu.collect_garbage()
for all_children as child do
if child.menu_name:lower():contains(input:lower()) then
search_root:link(child, true)
end
end
if #search_root:getChildren() == 1 then
util.toast($"No results found under \"{input}\", try expanding or even narrowing your search query.")
end
end
end):setTemporary()
config_root:list_action('Reset', {}, '', {
{ 'Theme', {}, $"The \"Theme\" folder.\n{theme_dir}" },
{ 'Header', {}, $"The \"Headers\" folder.\n{header_dir}" },
{ 'Font', {}, $"The \"menu font\" file.\n{theme_dir}Font.spritefont" },
{ 'Tags', {}, $"The menu \"tags\".\n{custom_dir}" },
{ 'Tabs', {}, $"The menu \"tabs\".\n{tabs_dir}" },
{ 'Appearance', {}, 'Reverts any visual modifications to their defaults (from Stand>Settings>Appearance). Make sure to save the profile to keep the changes.' },
}, function(index)
switch index do
case 1:
empty_dir(theme_dir)
reload_textures()
reload_font()
break
case 2:
empty_dir(header_dir)
header_hide()
header_custom()
header_hide()
break
case 3:
io.remove($"{theme_dir}Font.spritefont")
reload_font()
break
case 4:
empty_dir(custom_dir)
reload_textures()
break
case 5:
empty_dir(tabs_dir)
reload_textures()
break
case 6:
local meta_state = util.read_colons_and_tabs_file($"{stand_dir}Meta State.txt")
local active_profile = meta_state['Active Profile'] or 'Main'
for k in util.read_colons_and_tabs_file(
$"{stand_dir}Profiles\\{active_profile}.txt"
) do
if k:startswith('Stand>Settings>Appearance') then
local ref = menu.ref_by_path(k)
if ref:isValid() then
ref:applyDefaultState()
end
end
util.yield()
end
break
end
end)
config_root:list_action('Update', {}, '', {
{ 'Headers', {}, 'The list of headers available the script relies on.' },
{ 'Themes', {}, 'The list of themes available the script relies on.' },
{ 'Script', {}, 'This is automatically done daily, but you can still invoke a manual check.' }
}, function(idx, menu_name)
if idx == 1 or idx == 2 then
local name = menu_name:lower() .. '.txt'
download_file(
'https://raw.githubusercontent.com',
$"/stagnate6628/stand-themerepo/main/{name}",
$"{resources_dir}{name}"
)
util.restart_script()
else
auto_update_cfg.check_interval = 0
if auto_updater.run_auto_update(auto_update_cfg) then
util.toast('No updates found.')
end
end
end)
config_root:readonly('Version', SCRIPT_VERSION)