-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcompile.lua
More file actions
93 lines (81 loc) · 3.04 KB
/
compile.lua
File metadata and controls
93 lines (81 loc) · 3.04 KB
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
--[[
DEVELOPMENT PURPOSE ONLY
This module helps you compile your whole Love2D project to lua bytecode (Love2D exclusive), so this file is not loaded in Zenitha/init.lua.
How to use (IMPORTANT):
1. Make sure the saving folder is empty, or at least not having same filename to your project folder (will be overwritten)
2. Add `require("Zenitha.compile")()` to THE LAST LINE of main.lua (everything after this line will be stripped)
3. Run your project, and the process will exit immediately after compiling.
4. Check outputs in console, and check saving folder for compiled files.
5. Copy all compiled files to your project folder and choose "replacing all"
6. Build your project.
7. Recover your project folder with version control system, or make a backup before step 5.
]]
---@diagnostic disable-next-line
local _hoverMouseHereToRead
if not love.filesystem then
LOG("COMPILE lib is not loaded (need love.filesystem)")
return setmetatable({},{
__index=function(_,k)
error("attempt to use COMPILE."..k..", but COMPILE lib is not loaded (need love.filesystem)")
end,
})
end
local fs=love.filesystem
local _compileSelf=false
local _stripDebugInfo=false
local function compileFile(path)
if fs.getRealDirectory(path)==fs.getSaveDirectory() then
return false,"Skipped file in save directory: "..path
end
local file=fs.read('string',path)
---@cast file string
if path=='main.lua' then
local requirePos=file:find("require[^\n]*Zenitha[./]compile")
assert(requirePos,"Failed to find the fixed require statement in main.lua, please read instructions carefully.")
file=file:sub(1,requirePos-1)
end
local func,res=loadstring(file,path)
if func then
fs.write(path,string.dump(func,_stripDebugInfo))
return true
else
return false,res
end
end
---@param path string
local function compileObj(path)
if path:sub(1,1)=='.' then
print("Skipped hidden file/directory: "..path)
return
end
local t=fs.getInfo(path).type
if t=='file' then
if path:sub(-4)=='.lua' then
local suc,msg=compileFile(path)
if suc then
print("Compiled "..path)
else
print("Failed to compile "..path..": "..msg)
end
end
elseif t=='directory' and (_compileSelf or path~='Zenitha') then
local contents=fs.getDirectoryItems(path)
if next(contents) then
if #path>0 then fs.createDirectory(path) end
for _,name in next,contents do
compileObj((#path>0 and path..'/' or '')..name)
end
end
end
end
---Compile all .lua files into bytecodes
---@param compileSelf? boolean default to false
---@param stripDebugInfo? boolean default to false
local function start(compileSelf,stripDebugInfo)
if compileSelf==nil then compileSelf=false end
if stripDebugInfo==nil then stripDebugInfo=false end
_compileSelf,_stripDebugInfo=compileSelf,stripDebugInfo
compileObj('')
os.exit()
end
return start