This repository has been archived by the owner on Dec 20, 2020. It is now read-only.
/
index.lua
1454 lines (1369 loc) · 43.2 KB
/
index.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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
Homebr3w
A Homebrew Application to download other Homebrew
Apps based on TitleDB.com's database
Homebr3w is licensed under GPL 3.0, the full license
can be found in the root of this repository inside of
the LICENSE or LICENSE.md file.
Copyright (C) 2016 https://github.com/Wolvan
]]--
-- START OF PROGRAM
-- BEGIN UTILITY CODE DECLARATION
--[[
Define some constants for easier usage
later on
]]--
local WHITE = Color.new(255,255,255)
local YELLOW = Color.new(255,205,66)
local RED = Color.new(255,0,0)
local GREEN = Color.new(55,255,0)
local BLUE = Color.new(70,50,250)
local APP_VERSION = "1.3.0"
local APP_DIR = "/Homebr3w"
local APP_CACHE = APP_DIR.."/Cache"
local APP_CONFIG = APP_DIR.."/config.json"
local APP_CIA_DIR = APP_DIR.."/CIAs"
local APP_LIBS_DIR = APP_DIR.."/Libraries"
local APP_TEMP_DIR = APP_DIR.."/tmp"
local INSTALLED_STATE = {
NOT_INSTALLED = 4,
LATEST_VERSION = 3,
OUTDATED_VERSION = 1,
VERSION_UNKNOWN = 2
}
local API_URL = "https://api.titledb.com/"
local LIB_TYPES = {
LIBRARY = 1,
ARCHIVE = 2
}
--[[
Defined libraries that the tool needs here. There are 2 formats for libraries available:
Both types share the following key/value pairs:
name --Defined the name the library is going to be available as later. Also gets shown when downloading library
filename --Name the file will be saved as under APP_DIR/Libraries. Can be any name
downloadPath --The URL the library's package will be downloaded from
type --The type of library that can be used. Can either be LIB_TYPES.LIBRARY for unzipped or LIB_TYPES.ARCHIVE for zipped libraries
Zipped libraries also has 1 more key/value pair that needs to be defined:
fileToExtract --The filename of the file inside of the archive that needs to be extracted to APP_DIR/Libraries
Example library definitions:
{
name = "dkjson",
filename = "dklibraries["dkjson"].lua",
downloadPath = "http://dkolf.de/src/dkjson-lua.fsl/raw/dklibraries["dkjson"].lua?name=16cbc26080996d9da827df42cb0844a25518eeb3",
type = LIB_TYPES.LIBRARY
},
{
name = "luaqrcode",
filename = "qrencode.lua",
downloadPath = "https://github.com/speedata/luaqrcode/zipball/master",
type = LIB_TYPES.ARCHIVE,
fileToExtract = "speedata-luaqrcode-726a866/qrencode.lua"
}
]]--
local REQUIRED_LIBRARIES = {
{
name = "dkjson",
filename = "dkjson.lua",
downloadPath = "http://dkolf.de/src/dkjson-lua.fsl/raw/dkjson.lua?name=16cbc26080996d9da827df42cb0844a25518eeb3",
type = LIB_TYPES.LIBRARY
},
{
name = "luaqrcode",
filename = "qrencode.lua",
downloadPath = "https://raw.githubusercontent.com/speedata/luaqrcode/master/qrencode.lua",
type = LIB_TYPES.LIBRARY
}
}
--[[
Modes to sort the Applist
]]--
local sortModes = {
{
text = "ID (ascending)",
sortFunction = function(a, b)
return a.id < b.id
end
},
{
text = "ID (descending)",
sortFunction = function(a, b)
return a.id > b.id
end
},
{
text = "Alphabet (ascending)",
sortFunction = function(a, b)
local aName = a.name
local bName = b.name
aName = aName:sub(1,1):upper()..aName:sub(2)
bName = bName:sub(1,1):upper()..bName:sub(2)
return aName < bName
end
},
{
text = "Alphabet (descending)",
sortFunction = function(a, b)
local aName = a.name
local bName = b.name
aName = aName:sub(1,1):upper()..aName:sub(2)
bName = bName:sub(1,1):upper()..bName:sub(2)
return aName > bName
end
},
{
text = "Size (ascending)",
sortFunction = function(a, b)
return a.size < b.size
end
},
{
text = "Size (descending)",
sortFunction = function(a, b)
return a.size > b.size
end
}
}
--[[
Define various variables for later
use in the application
]]--
local config = {
enableUpdateCheck = {
text = "Enable Update Check",
value = true
},
deleteCIAAfterInstall = {
text = "Delete CIA after install",
value = true
},
downloadRetryCount = {
text = "Download Retries",
value = 3,
minValue = 1,
maxValue = 10
},
defaultSortMode = {
text = "Default sorting mode",
value = 3,
minValue = 1,
maxValue = #sortModes
},
leftRightJump = {
text = "Left/Right Jump",
value = 10,
minValue = 1,
maxValue = 15
},
groupInstalledApps = {
text = "Group installed apps",
value = true
}
}
local config_backup = {}
local libraries = {}
local parsedApplist = {}
local fullApplist = {}
local installed = {}
local blacklistedApps = {}
local remVer = nil
local locVer = nil
local canUpdate = nil
local imageCache = {}
local mtimeCache = {}
local selectedCIA = 1
local menuOffset = 0
local selection = 1
local screenHeightVar = 13
local menu_selection = 1
local options_selection = 1
local sortMode = 3
local currentFilter = ""
local home = "Homemenu"
if System.checkBuild() ~= 1 then
home = "Homebrew Launcher"
end
local pad = Controls.read()
local oldpad = pad
local kbState = nil
local scrollTimer = Timer.new()
local isScrolling = false
--[[
Poor Man's Breakpoint
This function makes debugging a lot easier
since this can be used as a makeshift
breakpoint to halt execution anywhere in the
Script
]]--
function STOP()
while true do
pad = Controls.read()
if Controls.check(pad, KEY_A) and not Controls.check(oldpad, KEY_A) then
System.exit()
end
oldpad = pad
end
end
--[[
Function for different data types that Lua doesn't
come with natively.
]]--
--[[
String manipulation functions
split() takes a string and returns a table of substrings
split at the separator.
startsWith() returns true if the string starts with
a given string.
]]--
function string.split(self, sep)
local sep, fields = sep or ":", {}
local pattern = string.format("([^%s]+)", sep)
self:gsub(pattern, function(c) fields[#fields+1] = c end)
return fields
end
function string.startsWith(String, Start)
return string.sub(String,1,string.len(Start))==Start
end
--[[
Table manipulation functions
deepcopy() is a way to copy tables without
linking them through references.
countTableElements() returns the actual number
if elements a table contains instead of only
the highest number index like # does.
table.filter is a Lua implementation of JS'
Array.filter() function.
table.dump is a debugging tool that allows
dumping contents of a table to file for later
inspection quickly.
]]--
function deepcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[deepcopy(orig_key)] = deepcopy(orig_value)
end
setmetatable(copy, deepcopy(getmetatable(orig)))
else -- number, string, boolean, etc
copy = orig
end
return copy
end
function countTableElements(tbl)
local i = 0
for k,v in pairs(tbl) do
i = i + 1
end
return i
end
table.filter = function(workingTable, filterIter)
local out = {}
for index, value in pairs(workingTable) do
if filterIter(value, index, workingTable) then out[index] = value end
end
return out
end
table.dump = function(tbl, filename)
if not filename then filename = "tbl_dump.json" end
local jsonString = libraries["dkjson"].encode(tbl, { indent = true })
local file = io.open("/"..filename, FCREATE)
io.write(file, 0, jsonString, jsonString:len())
io.close(file)
end
--[[
Functions to save and load any table to or from
a JSON encoded file somewhere on the SD card
]]--
function saveTable(filename, tbl)
if not tbl then tbl = {} end
if not filename then filename = APP_DIR.."/tbl.json" end
local jsonString = libraries["dkjson"].encode(tbl, { indent = true })
local currentPath = ""
local splitPath = filename:split("/")
for i = 1, #splitPath - 1 do
if splitPath[i] then
currentPath = currentPath.."/"..splitPath[i]
System.createDirectory(currentPath)
end
end
local file = io.open(filename, FCREATE)
io.write(file, 0, jsonString, jsonString:len())
io.close(file)
end
function loadTable(filename, defaulttbl)
if not filename then filename = APP_DIR.."/tbl.json" end
if not defaulttbl then defaulttbl = {} end
if not System.doesFileExist(filename) then
saveTable(filename, defaulttbl)
end
local file = io.open(filename, FREAD)
local filesize = 0
filesize = tonumber(io.size(file))
if filesize == 0 then
io.close(file)
saveTable(filename, defaulttbl)
file = io.open(filename, FREAD)
end
local file_contents = io.read(file, 0, tonumber(io.size(file)))
io.close(file)
local loaded_config = libraries["dkjson"].decode(file_contents)
if type(loaded_config) == "table" then
return loaded_config
else
return nil
end
end
--[[
Functions to save and load config from a config
file. The config table gets encoded as JSON file
and saved to the SD.
Loading reads that file (or creates it if it
doesn't exist before reading), decodes the JSON
and then overwrites each value of the config
table that is defined in the decoded JSON Object.
This way, settings that are not stored in the config
yet just use the default value set in the config table.
]]--
function saveConfig()
saveTable(APP_CONFIG, config)
end
function loadConfig()
local loaded_config = loadTable(APP_CONFIG, config)
if type(loaded_config) == "table" then
for k,v in pairs(loaded_config) do
config[k] = v
end
else
return false
end
return true
end
--[[
This function just presents an error to
the user. Overriding the keypressFunction
allows changing behavior of the error
]]--
function showError(errorMsg, keypressFunction)
keypressFunction = keypressFunction or function()
pad = Controls.read()
if Controls.check(pad, KEY_A) and not Controls.check(oldpad, KEY_A) then
Screen.waitVblankStart()
Screen.flip()
main()
end
oldpad = pad
end
Screen.refresh()
Screen.clear(TOP_SCREEN)
Screen.clear(BOTTOM_SCREEN)
Screen.waitVblankStart()
Screen.flip()
local splitString = errorMsg:split("\n")
for k,v in pairs(splitString) do
Screen.debugPrint(5, ((k-1)*15)+5, v, RED, TOP_SCREEN)
end
Screen.debugPrint(5, 95, "GitHub can be found at", WHITE, BOTTOM_SCREEN)
Screen.debugPrint(5, 110, "https://github.com/Wolvan", WHITE, BOTTOM_SCREEN)
Screen.debugPrint(5, 125, "/Homebr3w", WHITE, BOTTOM_SCREEN)
while true do
keypressFunction()
end
end
--[[
Functions to parse and compare SemVer compliant
versions. parseVersion accepts and parses Strings
in the format MAJOR.MINOR.PATCH and returns a table
{major, minor, version} which can be used by
isUpdateAvailable
]]--
function parseVersion(verString)
if verString == nil or verString == "" then
verString = "0.0.0"
end
verString = verString:gsub(" ", "")
local version = {}
local splitVersion = verString:split(".")
if splitVersion[1]:lower():startsWith("v") then
splitVersion[1] = splitVersion[1]:sub(2)
end
version.major = tonumber(splitVersion[1]) or 0
version.minor = tonumber(splitVersion[2]) or 0
version.patch = tonumber(splitVersion[3]) or 0
return version
end
function isUpdateAvailable(localVersion, remoteVersion)
if remoteVersion.major > localVersion.major then
return true
end
if (remoteVersion.minor > localVersion.minor) and (remoteVersion.major >= localVersion.major) then
return true
end
if (remoteVersion.patch > localVersion.patch) and (remoteVersion.major >= localVersion.major) and (remoteVersion.minor >= localVersion.minor) then
return true
end
return false
end
--[[
Check if the User has Wi-Fi disabled and an
Internet connection is available
]]--
function checkWifi()
if not Network.isWifiEnabled() then
showError("Wi-Fi is disabled. Restart and try again.\nPress A to go back to "..home..".", function()
pad = Controls.read()
if Controls.check(pad, KEY_A) and not Controls.check(oldpad, KEY_A) then
System.exit()
end
oldpad = pad
end)
end
end
--[[
Check App State to close the App in case
of exitting from home menu.
Function should be used in every while
loop that allows going to Homemenu by
pressing HOME!
]]--
function checkForExit()
if System.checkStatus() == APP_EXITING then
System.exit()
end
end
--[[
Function to download File from Internet
]]--
function getFile(path, downloadURL, method, data)
if not method or (method ~= "GET" and method ~= "POST" and method ~= "HEAD") then method = "GET" end
if not data then data = {} end
System.deleteFile(path)
local jsondata = "[]"
if libraries["dkjson"] then
jsondata = libraries["dkjson"].encode(data)
end
Network.downloadFile(downloadURL, path, "User-Agent: Homebr3w/"..APP_VERSION, method, jsondata)
local filesize = 0
if System.doesFileExist(path) then
local encTitleKeys = io.open(path, FREAD)
filesize = tonumber(io.size(encTitleKeys))
io.close(encTitleKeys)
end
if filesize == 0 then
System.deleteFile(path)
return false
end
return true
end
--[[
Function that gets a json string from
the web and decodes it into a table
]]--
function getJSON(url, method, data)
if not method or (method ~= "GET" and method ~= "POST" and method ~= "HEAD") then method = "POST" end
if not data then
data = {
action = "list",
fields = { "id", "titleid", "author", "description", "name", "create_time", "update_time", "size", "mtime" }
}
end
local tbl = {}
local remoteData = Network.requestString(url, "User-Agent: Homebr3w/"..APP_VERSION, method, libraries["dkjson"].encode(data))
if remoteData ~= "" and remoteData ~= nil and type(remoteData) == "string" then
tbl = libraries["dkjson"].decode(remoteData)
else
return false, tbl
end
return true, tbl
end
--[[
Get a title from the Applist by titleid
]]--
function getTitleByID(titleid)
for k,v in pairs(parsedApplist) do
if v.titleid == titleid then
return v
end
end
end
--[[
Take a Title ID and generate a
Download QR Code Image for it
]]--
function drawQRToTopScreen(tid)
local multiplier = 7
local offset_x, offset_y = 75, 5
local screen = TOP_SCREEN
local function drawSquare(a,b,color)
local x, y = (a + 1) * multiplier, (b + 1) * multiplier
for i = 0, multiplier do
for j = 0, multiplier do
Screen.drawPixel(x + i + offset_x, y + j + offset_y, color, screen)
end
end
end
oldpad = pad
local ok, data = libraries["luaqrcode"].qrcode(API_URL.."v0/proxy/"..tid)
if ok then
local qr = Screen.createImage((#data[1]+4) * multiplier, (#data+4) * multiplier, WHITE)
Screen.drawImage(offset_x,offset_y,qr,screen)
local color = WHITE
for k,v in pairs(data) do
for a,b in pairs(v) do
if b < 0 then color = WHITE
elseif b > 0 then color = Color.new(0,0,0) end
drawSquare(k, a, color)
end
end
while true do
pad = Controls.read()
if Controls.check(pad, KEY_A) and not Controls.check(oldpad, KEY_A) then
Screen.freeImage(qr)
main()
elseif Controls.check(pad, KEY_B) and not Controls.check(oldpad, KEY_B) then
Screen.freeImage(qr)
main()
elseif Controls.check(pad, KEY_X) and not Controls.check(oldpad, KEY_X) then
Screen.freeImage(qr)
main()
elseif Controls.check(pad, KEY_Y) and not Controls.check(oldpad, KEY_Y) then
Screen.freeImage(qr)
main()
end
oldpad = pad
end
end
end
-- END UTILITY CODE DECLARATION
-- BEGIN MAIN PROGRAM CODE
--[[
Go through all of the icon files in the
Cache directory and download icons that
are missing.
]]--
function checkLibraries()
local function getLib(lib)
System.createDirectory(APP_LIBS_DIR)
local path = APP_LIBS_DIR.."/"..lib.filename
if lib.type == LIB_TYPES.ARCHIVE then
System.createDirectory(APP_TEMP_DIR)
path = APP_TEMP_DIR.."/"..lib.filename..".zip"
end
local downloadURL = lib.downloadPath
local success = getFile(path, downloadURL)
local tries = 0
while (tries < config.downloadRetryCount.value) and (not success) do
success = getFile(path, downloadURL)
tries = tries + 1
end
if success then
if lib.type == LIB_TYPES.ARCHIVE then
System.extractFromZIP(path, lib.fileToExtract, APP_LIBS_DIR.."/"..lib.filename)
System.deleteFile(path)
System.deleteDirectory(APP_TEMP_DIR)
end
end
return success
end
for k,v in pairs(REQUIRED_LIBRARIES) do
Screen.clear(BOTTOM_SCREEN)
Screen.debugPrint(5, 5, "Checking library "..k.." of "..#REQUIRED_LIBRARIES.."...", WHITE, BOTTOM_SCREEN)
if not System.doesFileExist(APP_LIBS_DIR.."/"..v.filename) then
Screen.debugPrint(5, 20, "Downloading "..v.name, WHITE, BOTTOM_SCREEN)
if not getLib(v) then
showError("Failed to download library!\nUnable to continue, please\ntry restarting the app and\ntry again.\n \nPress A to go back to "..home..".", function()
pad = Controls.read()
if Controls.check(pad, KEY_A) and not Controls.check(oldpad, KEY_A) then
System.exit()
end
oldpad = pad
end)
end
end
end
Screen.clear(BOTTOM_SCREEN)
end
--[[
Go through all of the icon files in the
Cache directory and download icons that
are missing.
]]--
function checkCache(tbl)
local function cache(titleid)
System.createDirectory(APP_CACHE)
local path = APP_CACHE.."/"..titleid..".png"
local downloadURL = API_URL.."images/"..titleid..".png"
local success = getFile(path, downloadURL)
local tries = 0
while (tries < config.downloadRetryCount.value) and (not success) do
success = getFile(path, downloadURL)
tries = tries + 1
end
return success
end
local tid = "0004000000130800"
local failed = 0
for k,v in pairs(tbl) do
Screen.clear(BOTTOM_SCREEN)
tid = v.titleid
Screen.debugPrint(5, 5, "Checking Icon "..k.." of "..#tbl.."...", WHITE, BOTTOM_SCREEN)
if not System.doesFileExist(APP_CACHE.."/"..tid..".png") then
Screen.debugPrint(5, 20, "Downloading "..tid, WHITE, BOTTOM_SCREEN)
if not cache(tid) then failed = failed + 1 end
end
if failed > 0 then Screen.debugPrint(5, 35, "Failed downloading "..failed.." Icons", WHITE, BOTTOM_SCREEN) end
end
Screen.clear(BOTTOM_SCREEN)
end
--[[
Return a subset of the applist that
contains all titleIDs of apps that
are installed on the 3ds in a lookup
table kind of way
isInstalled[TITLEID] = true or nil(false)
]]--
function checkInstalled()
local sysapps = System.listCIA()
local installed = table.filter(parsedApplist, function (item)
local dectid = tonumber(item.titleid:gsub("0004000", ""), 16)
for k,v in pairs(sysapps) do
if dectid == v.unique_id then
return true
end
end
return false
end)
local tbl = {}
for k,v in pairs(installed) do
tbl[v.titleid] = true
end
return tbl
end
--[[
Return the install state of an application
All values come from
INSTALLED_STATE =
{
VERSION_UNKNOWN,
OUTDATED_VERSION,
LATEST_VERSION,
NOT_INSTALLED
}
]]--
function getInstalledState(tid)
local title = getTitleByID(tid)
if installed[title.titleid] and not mtimeCache[title.titleid] then
return INSTALLED_STATE.VERSION_UNKNOWN
elseif installed[title.titleid] and mtimeCache[title.titleid] < title.mtime then
return INSTALLED_STATE.OUTDATED_VERSION
elseif installed[title.titleid] and mtimeCache[title.titleid] >= title.mtime then
return INSTALLED_STATE.LATEST_VERSION
end
return INSTALLED_STATE.NOT_INSTALLED
end
--[[
Sort app list with selected sorting mode
and then group them by install state if
the config option is enabled
]]
function sortAppList()
local parsedApplistFiltered = table.filter(fullApplist, function(item)
return item.name:lower():find(currentFilter:lower())
end)
local parsedApplistKeyFixed = {}
for k,v in pairs(parsedApplistFiltered) do
table.insert(parsedApplistKeyFixed, v)
end
parsedApplist = parsedApplistKeyFixed
table.sort(parsedApplist, sortModes[sortMode].sortFunction)
if config.groupInstalledApps.value then
local appsByState = {}
local iS = INSTALLED_STATE.NOT_INSTALLED
for k,v in pairs(parsedApplist) do
iS = getInstalledState(v.titleid)
if not appsByState[iS] then appsByState[iS] = {} end
table.insert(appsByState[iS], v)
end
local tbl = {}
local debugTBL = {}
for i = 1, 4 do
if appsByState[i] then
for i,j in pairs(appsByState[i]) do
table.insert(tbl, j)
end
end
end
parsedApplist = tbl
end
end
--[[
Take a title ID, parse it into it's
hexadecimal value and launch it from
SD Card
]]--
function launchByTitleId(titleid)
System.launchCIA(tonumber(titleid:gsub("0004000", ""), 16), SDMC)
end
--[[
Clear the image cache that is being
created in RAM for showing the icons
on the details page
]]--
function clearImageCache()
for k,v in pairs(imageCache) do
Screen.freeImage(v)
imageCache[k] = nil
end
end
--[[
Take a TitleID, download the corresponding
.cia from TitleDB.com and then install it
to SD
]]--
function downloadAndInstall(titleid)
oldpad = pad
Screen.waitVblankStart()
Screen.refresh()
Screen.clear(BOTTOM_SCREEN)
Screen.clear(TOP_SCREEN)
Screen.flip()
System.createDirectory(APP_CIA_DIR)
local title = getTitleByID(titleid)
if title then
Screen.debugPrint(5, 5, title.name, WHITE, TOP_SCREEN)
local line = 20
Screen.debugPrint(5, line, "Downloading...", WHITE, TOP_SCREEN)
local path = APP_CIA_DIR.."/"..title.titleid.."_"..title.name..".cia"
local downloadURL = API_URL.."v0/proxy/"..title.titleid
local success = getFile(path, downloadURL)
local tries = 0
while (tries < config.downloadRetryCount.value) and (not success) do
success = getFile(path, downloadURL)
tries = tries + 1
end
if success then
Screen.debugPrint(270, line, "[OK]", GREEN, TOP_SCREEN)
if System.checkBuild() == 1 then
line = 35
Screen.debugPrint(5, line, "Installing...", WHITE, TOP_SCREEN)
System.installCIA(path, SDMC)
if config.deleteCIAAfterInstall.value then System.deleteFile(path) end
installed[titleid] = true
Screen.debugPrint(270, line, "[OK]", GREEN, TOP_SCREEN)
mtimeCache[title.titleid] = title.mtime
saveTable(APP_DIR.."/mtime.json", mtimeCache)
Screen.debugPrint(5, 180, "Press A to launch the title", WHITE, BOTTOM_SCREEN)
else
mtimeCache[title.titleid] = title.mtime
saveTable(APP_DIR.."/mtime.json", mtimeCache)
Screen.debugPrint(5, 5, "Download finished! Unfortunately,", WHITE, BOTTOM_SCREEN)
Screen.debugPrint(5, 20, "the Ninjhax build of Homebr3w", WHITE, BOTTOM_SCREEN)
Screen.debugPrint(5, 35, "can not install the App", WHITE, BOTTOM_SCREEN)
Screen.debugPrint(5, 50, "automatically. Please use a", WHITE, BOTTOM_SCREEN)
Screen.debugPrint(5, 65, "titlemanager (like FBI) and ", WHITE, BOTTOM_SCREEN)
Screen.debugPrint(5, 80, "install the .cia manually, it's", WHITE, BOTTOM_SCREEN)
Screen.debugPrint(5, 95, "saved in the '/Homebr3w/CIAs'", WHITE, BOTTOM_SCREEN)
Screen.debugPrint(5, 110, "directory.", WHITE, BOTTOM_SCREEN)
end
Screen.debugPrint(5, 195, "Press B to go to title list", WHITE, BOTTOM_SCREEN)
Screen.debugPrint(5, 210, "Press Start to go to "..home, WHITE, BOTTOM_SCREEN)
while true do
pad = Controls.read()
if Controls.check(pad, KEY_A) and not Controls.check(oldpad, KEY_A) then
oldpad = pad
if System.checkBuild() == 1 then launchByTitleId(parsedApplist[selectedCIA].titleid) end
elseif Controls.check(pad, KEY_START) and not Controls.check(oldpad, KEY_START) then
System.exit()
elseif Controls.check(pad, KEY_B) and not Controls.check(oldpad, KEY_B) then
oldpad = pad
main()
end
oldpad = pad
end
else
Screen.debugPrint(270, line, "[FAILED]", RED, TOP_SCREEN)
Screen.debugPrint(5, 195, "Press B to go to title list", WHITE, BOTTOM_SCREEN)
Screen.debugPrint(5, 210, "Press Start to go to "..home, WHITE, BOTTOM_SCREEN)
while true do
pad = Controls.read()
if Controls.check(pad, KEY_B) and not Controls.check(oldpad, KEY_B) then
oldpad = pad
main()
elseif Controls.check(pad, KEY_START) and not Controls.check(oldpad, KEY_START) then
System.exit()
end
oldpad = pad
end
return false
end
end
return false
end
function optionsMenu()
oldpad = pad
Screen.waitVblankStart()
Screen.refresh()
Screen.clear(TOP_SCREEN)
Screen.debugPrint(5, 5, "Options", YELLOW, TOP_SCREEN)
Screen.debugPrint(20, (options_selection * 15) + 5, ">", WHITE, TOP_SCREEN)
local config_keys = {}
local i = 1
for k,v in pairs(config) do
Screen.debugPrint(30, (i * 15) + 5, v.text, WHITE, TOP_SCREEN)
if type(v.value) == "boolean" then
if v.value then
Screen.debugPrint(350, (i * 15) + 5, "On", GREEN, TOP_SCREEN)
else
Screen.debugPrint(350, (i * 15) + 5, "Off", RED, TOP_SCREEN)
end
elseif type(v.value) == "number" then
Screen.debugPrint(350, (i * 15) + 5, v.value, YELLOW, TOP_SCREEN)
end
config_keys[#config_keys+1] = k
i = i + 1
end
Screen.clear(BOTTOM_SCREEN)
Screen.debugPrint(5, 110, "up/down - Select option", WHITE, BOTTOM_SCREEN)
Screen.debugPrint(5, 125, "left/right - Change setting", WHITE, BOTTOM_SCREEN)
Screen.debugPrint(5, 140, "A - Save", WHITE, BOTTOM_SCREEN)
Screen.debugPrint(5, 155, "B - Cancel", WHITE, BOTTOM_SCREEN)
Screen.flip()
while true do
pad = Controls.read()
if Controls.check(pad, KEY_DDOWN) and not Controls.check(oldpad, KEY_DDOWN) then
options_selection = options_selection + 1
if (options_selection > #config_keys) then
options_selection = 1
end
elseif Controls.check(pad, KEY_DUP) and not Controls.check(oldpad, KEY_DUP) then
options_selection = options_selection - 1
if (options_selection < 1) then
options_selection = #config_keys
end
elseif Controls.check(pad, KEY_DLEFT) and not Controls.check(oldpad, KEY_DLEFT) then
local currentSetting = config[config_keys[options_selection]]
if type(currentSetting.value) == "boolean" then
currentSetting.value = not currentSetting.value
elseif type(currentSetting.value) == "number" then
currentSetting.value = currentSetting.value - 1
if currentSetting.minValue then
if currentSetting.value < currentSetting.minValue then currentSetting.value = currentSetting.minValue end
end
config[config_keys[options_selection]].value = currentSetting.value
end
elseif Controls.check(pad, KEY_DRIGHT) and not Controls.check(oldpad, KEY_DRIGHT) then
local currentSetting = config[config_keys[options_selection]]
if type(currentSetting.value) == "boolean" then
currentSetting.value = not currentSetting.value
elseif type(currentSetting.value) == "number" then
currentSetting.value = currentSetting.value + 1
if currentSetting.maxValue then
if currentSetting.value > currentSetting.maxValue then currentSetting.value = currentSetting.maxValue end
end
config[config_keys[options_selection]].value = currentSetting.value
end
elseif Controls.check(pad, KEY_A) and not Controls.check(oldpad, KEY_A) then
oldpad = pad
config_backup = deepcopy(config)
saveConfig()
sortAppList()
menu()
elseif Controls.check(pad, KEY_B) and not Controls.check(oldpad, KEY_B) then
oldpad = pad
config = deepcopy(config_backup)
menu()
end
oldpad = pad
optionsMenu()
end
end
function searchApp()
Screen.waitVblankStart()
Screen.refresh()
Screen.clear(TOP_SCREEN)
Screen.debugPrint(5, 5, "Filter apps by name", WHITE, TOP_SCREEN)
Screen.debugPrint(5, 20, "Current Filter:", WHITE, TOP_SCREEN)
Screen.debugPrint(5, 35, Keyboard.getInput(), WHITE, TOP_SCREEN)
Screen.clear(BOTTOM_SCREEN)
if kbState ~= FINISHED then
kbState = Keyboard.getState()
Keyboard.show()
if kbState ~= NOT_PRESSED then
if kbState == CLEANED then
currentFilter = ""
sortAppList()
menu()
end
end
else
currentFilter = Keyboard.getInput()
selectedCIA = 1
selection = 1
menuOffset = 0
sortAppList()
menu()
end
Screen.flip()
searchApp()
end
function markAsLatest()
oldpad = pad
Screen.waitVblankStart()
Screen.refresh()
Screen.clear(TOP_SCREEN)
Screen.debugPrint(5, 5, "Marking all installed Apps as latest...", WHITE, TOP_SCREEN)
Screen.clear(BOTTOM_SCREEN)
Screen.flip()
for k,v in pairs(checkInstalled()) do
if v and not mtimeCache[k] then
mtimeCache[k] = getTitleByID(k).mtime
end
end
saveTable(APP_DIR.."/mtime.json", mtimeCache)
Screen.debugPrint(5, 20, "Done! Press A to continue.", WHITE, TOP_SCREEN)
while true do
pad = Controls.read()
if Controls.check(pad, KEY_A) and not Controls.check(oldpad, KEY_A) then
menu()
end
oldpad = pad
end
end
function menu()
local menu_options = {
{
text = "Set search filter",
callback = function()
kbState = nil
Keyboard.show()
searchApp()
end
},{
text = "Settings",