Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.

Commit 4913257

Browse files
committed
resources!
1 parent 6411e7c commit 4913257

File tree

4 files changed

+164
-1
lines changed

4 files changed

+164
-1
lines changed

TypeWriter/Libs/CompileHelper.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ return function(Path)
1212
Compiled.Code = {}
1313
Compiled.Resources = {}
1414

15+
Logger.Debug(Path)
16+
1517
function ScanDir(Dir, SubDir)
1618
Logger.Debug("Scanning " .. Dir)
1719
Logger.Debug(SubDir)
@@ -32,8 +34,29 @@ return function(Path)
3234
end
3335
end
3436

37+
function ReadResources(Dir, SubDir)
38+
Logger.Debug("Reading " .. Dir)
39+
Logger.Debug(SubDir)
40+
41+
for i, v in FS.scandirSync(Dir) do
42+
Logger.Debug(i .. " " .. v)
43+
44+
if v == "directory" then
45+
ReadResources(Dir .. "/" .. i, SubDir .. "/" .. i)
46+
elseif v == "file" then
47+
local SplitFile = Split(i, ".")
48+
local CurrentPath = table.concat(Split(SubDir, "/"), "/") .. "/" .. table.concat(SplitFile, ".", 1, #SplitFile - 0)
49+
50+
Logger.Debug(CurrentPath)
51+
Logger.Debug(Dir .. "/" .. i)
52+
Compiled.Resources[CurrentPath] = require("base64").encode(FS.readFileSync(Dir .. "/" .. i))
53+
end
54+
end
55+
end
56+
3557

3658
ScanDir(Path .. "lua", "")
59+
ReadResources(Path .. "resources", "")
3760

3861

3962
return Compiled

TypeWriter/Libs/LoadExecutionValues.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ return function()
1010
_G.LoadPackage = require("Run/LoadPackage")
1111
_G.LoadInternal = require("Run/LoadInternal")
1212
_G.CallEntrypoint = require("Run/CallEntrypoint")
13-
_G.CallEntrypoint = require("Run/ResourceHelper")
13+
_G.ResourceHelper = require("Run/ResourceHelper")
1414

1515
_G.Class = require("core").Object
1616
_G.Emitter = require("core").Emitter

TypeWriter/Libs/Run/ResourceHelper.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,30 @@
11
local ResourceHelper = {}
2+
local Logger = require("Logger")
3+
local Base64 = require("base64")
4+
local Json = require("json")
5+
6+
function ResourceHelper.GetRaw(Id, Path, Suppress)
7+
8+
local Data = LoadedPackages[Id].Resources[Path]
9+
10+
if not Data then
11+
if not Suppress then
12+
Logger.Error("Tried to load a not existing resource.")
13+
end
14+
15+
return nil
16+
end
17+
18+
return require("base64").decode(Data)
19+
20+
end
21+
22+
23+
function ResourceHelper.GetJson(Id, Path, Suppress)
24+
25+
return Json.decode(ResourceHelper.GetRaw(Id, Path, Suppress))
26+
27+
end
228

329

430

TypeWriter/deps/base64.lua

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
--[[lit-meta
2+
name = "creationix/base64"
3+
description = "A pure lua implemention of base64 using bitop"
4+
tags = {"crypto", "base64", "bitop"}
5+
version = "2.0.0"
6+
license = "MIT"
7+
author = { name = "Tim Caswell" }
8+
]]
9+
10+
local bit = require 'bit'
11+
local rshift = bit.rshift
12+
local lshift = bit.lshift
13+
local bor = bit.bor
14+
local band = bit.band
15+
local char = string.char
16+
local byte = string.byte
17+
local concat = table.concat
18+
local codes = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
19+
20+
-- Loop over input 3 bytes at a time
21+
-- a,b,c are 3 x 8-bit numbers
22+
-- they are encoded into groups of 4 x 6-bit numbers
23+
-- aaaaaa aabbbb bbbbcc cccccc
24+
-- if there is no c, then pad the 4th with =
25+
-- if there is also no b then pad the 3rd with =
26+
local function base64Encode(str)
27+
local parts = {}
28+
local j = 1
29+
for i = 1, #str, 3 do
30+
local a, b, c = byte(str, i, i + 2)
31+
parts[j] = char(
32+
-- Higher 6 bits of a
33+
byte(codes, rshift(a, 2) + 1),
34+
-- Lower 2 bits of a + high 4 bits of b
35+
byte(codes, bor(
36+
lshift(band(a, 3), 4),
37+
b and rshift(b, 4) or 0
38+
) + 1),
39+
-- Low 4 bits of b + High 2 bits of c
40+
b and byte(codes, bor(
41+
lshift(band(b, 15), 2),
42+
c and rshift(c, 6) or 0
43+
) + 1) or 61, -- 61 is '='
44+
-- Lower 6 bits of c
45+
c and byte(codes, band(c, 63) + 1) or 61 -- 61 is '='
46+
)
47+
j = j + 1
48+
end
49+
return concat(parts)
50+
end
51+
52+
-- Reverse map from character code to 6-bit integer
53+
local map = {}
54+
for i = 1, #codes do
55+
map[byte(codes, i)] = i - 1
56+
end
57+
58+
-- loop over input 4 characters at a time
59+
-- The characters are mapped to 4 x 6-bit integers a,b,c,d
60+
-- They need to be reassalbled into 3 x 8-bit bytes
61+
-- aaaaaabb bbbbcccc ccdddddd
62+
-- if d is padding then there is no 3rd byte
63+
-- if c is padding then there is no 2nd byte
64+
local function base64Decode(data)
65+
local bytes = {}
66+
local j = 1
67+
for i = 1, #data, 4 do
68+
local a = map[byte(data, i)]
69+
local b = map[byte(data, i + 1)]
70+
local c = map[byte(data, i + 2)]
71+
local d = map[byte(data, i + 3)]
72+
73+
-- higher 6 bits are the first char
74+
-- lower 2 bits are upper 2 bits of second char
75+
bytes[j] = char(bor(lshift(a, 2), rshift(b, 4)))
76+
77+
-- if the third char is not padding, we have a second byte
78+
if c < 64 then
79+
-- high 4 bits come from lower 4 bits in b
80+
-- low 4 bits come from high 4 bits in c
81+
bytes[j + 1] = char(bor(lshift(band(b, 0xf), 4), rshift(c, 2)))
82+
83+
-- if the fourth char is not padding, we have a third byte
84+
if d < 64 then
85+
-- Upper 2 bits come from Lower 2 bits of c
86+
-- Lower 6 bits come from d
87+
bytes[j + 2] = char(bor(lshift(band(c, 3), 6), d))
88+
end
89+
end
90+
j = j + 3
91+
end
92+
return concat(bytes)
93+
end
94+
95+
assert(base64Encode("") == "")
96+
assert(base64Encode("f") == "Zg==")
97+
assert(base64Encode("fo") == "Zm8=")
98+
assert(base64Encode("foo") == "Zm9v")
99+
assert(base64Encode("foob") == "Zm9vYg==")
100+
assert(base64Encode("fooba") == "Zm9vYmE=")
101+
assert(base64Encode("foobar") == "Zm9vYmFy")
102+
103+
assert(base64Decode("") == "")
104+
assert(base64Decode("Zg==") == "f")
105+
assert(base64Decode("Zm8=") == "fo")
106+
assert(base64Decode("Zm9v") == "foo")
107+
assert(base64Decode("Zm9vYg==") == "foob")
108+
assert(base64Decode("Zm9vYmE=") == "fooba")
109+
assert(base64Decode("Zm9vYmFy") == "foobar")
110+
111+
return {
112+
encode = base64Encode,
113+
decode = base64Decode,
114+
}

0 commit comments

Comments
 (0)