rtmc/x.py

95 lines
2.1 KiB
Python
Raw Normal View History

2024-08-17 23:08:11 +00:00
#!/usr/bin/python
2024-08-25 18:30:33 +00:00
OUTPUT="rtmc.lua";
2024-08-17 23:08:11 +00:00
FILES= [
2024-08-18 00:34:13 +00:00
"main.lua",
2024-08-25 18:30:33 +00:00
"log.lua",
"json.lua",
2024-08-25 19:56:32 +00:00
"updater.lua",
"notifier.lua"
2024-08-17 23:08:11 +00:00
]
2024-08-25 19:35:33 +00:00
VERSION="1.0.0d"
2024-08-18 11:37:59 +00:00
MINIMISE=True
2024-08-17 23:08:11 +00:00
def read_file(p: str) -> str:
buf = "";
2024-08-18 00:34:13 +00:00
with open("src/"+p, "r", encoding="utf-8") as f:
2024-08-25 19:35:33 +00:00
buf += f"rawset(__BUNDLER_FILES, \"{p.replace(".lua", "")}\", function ()\n";
2024-08-18 00:34:13 +00:00
for line in f.readlines():
2024-08-18 11:37:59 +00:00
buf += " " + line;
buf += "\nend)";
if not MINIMISE:
buf += f"-- FILE END: {p} --\n";
else:
buf += "\n";
2024-08-17 23:08:11 +00:00
return buf;
2024-08-18 11:37:59 +00:00
def minimise(buf: str) -> str:
newbuf = "";
for line in buf.splitlines():
if line.strip().startswith("--"):
continue;
if line.strip() == "":
continue
newbuf += line + "\n";
return newbuf;
2024-08-17 23:08:11 +00:00
2024-08-25 18:30:33 +00:00
# def get_hash(buf: str) -> str:
# hasher = hashlib.sha1();
#
# hasher.update(bytes(buf, "utf-8"));
# return hasher.hexdigest();
2024-08-25 19:35:33 +00:00
REQ_HEADER=f"""local __BUNDLER_FILES = {{}}
local __VERSION = "{VERSION}"
2024-08-25 19:03:37 +00:00
local __DEFAULT_IMPORT = require
local require = function(path)
if __BUNDLER_FILES[path] then
return __BUNDLER_FILES[path]()
elseif __BUNDLER_FILES[path .. ".lua"] then
return __BUNDLER_FILES[path .. ".lua"]()
else
return __DEFAULT_IMPORT(path)
end
end
"""
2024-08-25 19:35:33 +00:00
REQ_FOOTER="""
if pcall(debug.getlocal, 4, 1) then
local exports = {};
for k, v in pairs(__BUNDLER_FILES) do
if k ~= "main" then
exports[k] = v();
end
end
return exports;
else
require("main").main()
end
"""
2024-08-25 19:03:37 +00:00
2024-08-17 23:08:11 +00:00
def main():
2024-08-18 00:34:13 +00:00
buf = ""
2024-08-25 19:03:37 +00:00
buf += REQ_HEADER;
2024-08-25 18:30:33 +00:00
# buf += "local __UPDATE_HASH = __BUNDLER_REPLACE_HASH__\n";
2024-08-17 23:08:11 +00:00
for file in FILES:
print(f"=== FILE: {file}");
buf += read_file(file);
2024-08-18 00:34:13 +00:00
2024-08-25 19:35:33 +00:00
buf += REQ_FOOTER;
2024-08-18 11:37:59 +00:00
if MINIMISE:
buf = minimise(buf);
2024-08-25 18:30:33 +00:00
#update_hash = get_hash(buf);
# buf = buf.replace("__BUNDLER_REPLACE_HASH__", f"\"{update_hash}\"");
2024-08-18 00:34:13 +00:00
2024-08-25 18:30:33 +00:00
#print(f"=== UPDATE HASH: {update_hash}")
2024-08-17 23:08:11 +00:00
with open(OUTPUT, "w", encoding="utf-8") as f:
f.write(buf);
print("DONE");
if __name__ == "__main__":
main();