-
Notifications
You must be signed in to change notification settings - Fork 17
/
line.lua
347 lines (283 loc) · 7.9 KB
/
line.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
local line = _G.line or {}
local require = require("require")
line.speed = 1
line.love_envs = line.love_envs or table.weak()
pvars.Setup("line_enable_audio", true)
pvars.Setup("line_version", "0.10.1")
do
local function base_typeOf(self, str)
return str == self.name
end
local function base_type(self)
return self.name
end
local created = table.weak()
local registered = {}
function line.TypeTemplate(name)
local META = {}
META.__line_type = name
return META
end
function line.RegisterType(META)
META.__index = META
META.typeOf = base_typeOf
META.type = base_type
registered[META.__line_type] = META
-- some löve scripts get it from here
debug.getregistry()[META.__line_type] = META
if created[META.__line_type] then
for i, v in ipairs(created[META.__line_type]) do
setmetatable(v, META)
end
end
end
function line.CreateObject(name)
local META = registered[name]
local self = setmetatable({}, META)
created[META.__line_type] = created[META.__line_type] or {}
list.insert(created[META.__line_type], self)
return self
end
function line.Type(v)
local t = type(v)
if t == "table" and v.__line_type then return v.__line_type end
return t
end
function line.GetCreatedObjects(name)
return created[name] or {}
end
end
function line.ErrorNotSupported(str, level)
wlog("[line] " .. str)
end
function line.CreateLoveEnv(version)
if VULKAN then return end
version = version or pvars.Get("line_version")
local love = {}
love._version = version
local version = version:split(".")
love._version_major = tonumber(version[1])
love._version_minor = tonumber(version[2])
love._version_revision = tonumber(version[3])
love._line_env = {}
love.package_loaders = {}
runfile("lua/libraries/love/libraries/*", love)
list.insert(line.love_envs, love)
setmetatable(
love,
{
__newindex = function(t, k, v)
if type(v) == "function" then
llog("love.%s = %s", k, v)
event.Call("LoveNewIndex", t, k, v)
end
rawset(t, k, v)
end,
}
)
return love
end
do
local current_love
local on_error = function(msg)
current_love._line_env.error_message = msg .. "\n" .. debug.traceback()
logn(current_love._line_env.error_message)
end
function line.pcall(love, func, ...)
if love._line_env.error_message then return end
current_love = love
local ret = {xpcall(func, on_error, ...)}
if ret[1] then return select(2, unpack(ret)) end
end
end
function line.CallEvent(what, a, b, c, d, e, f)
for i, love in ipairs(line.love_envs) do
if love[what] and not love._line_env.error_message then
local a, b, c, d, e, f = line.pcall(love, love[what], a, b, c, d, e, f)
if a then return a, b, c, d, e, f end
end
end
end
function line.FixPath(path)
if path:starts_with("/") or path:starts_with("\\") then return path:sub(2) end
return path
end
function line.RunGame(folder, ...)
local love = line.CreateLoveEnv()
llog("mounting love game folder: ", R(folder .. "/"))
vfs.CreateDirectory("data/love/")
vfs.AddModuleDirectory("lua/modules/", love.package_loaders)
vfs.AddModuleDirectory("data/love/", love.package_loaders)
vfs.Mount(R(folder .. "/"))
vfs.AddModuleDirectory(folder .. "/", love.package_loaders)
local os = {}
for k, v in pairs(_G.os) do
os[k] = v
end
function os.execute(str)
print("os.execute: ", str)
if str:find("__LOVE_BINARY__") then
local path = vfs.FixPathSlashes(str:match(".+\"(.+%.love)\""))
print(path, "!!!")
if vfs.IsFile(path) then line.RunGame(path) end
return
end
os.execute(str)
end
local package_loaded = {}
local env
local require = require
env = setmetatable(
{
os = os,
love = love,
require = function(name, ...)
if name == "strict" then return true end
if name == "socket.core" then
env.socket = sockets.core.luasocket
return env.socket
end
if package_loaded[name] then return package_loaded[name] end
if name:starts_with("love.") and love[name:match(".+%.(.+)")] then
return love[name:match(".+%.(.+)")]
end
local func, err, path = require.load(name, love.package_loaders)
--llog("require: ", name, " (", path , ")")
if type(func) == "function" then
if debug.getinfo(func).what ~= "C" then setfenv(func, env) end
local res = assert(require.require_function(name, func, path, name, love.package_loaders))
package_loaded[name] = res
return res
end
if pcall(require, name) then return require(name) end
if not func then error(err, 2) end
return func
end,
type = function(v)
local t = _G.type(v)
if t == "table" and v.__line_type then return "userdata" end
return t
end,
pcall = function(func, ...)
if type(func) == "function" and debug.getinfo(func).what ~= "C" then
setfenv(func, env)
end
return _G.pcall(func, ...)
end,
xpcall = function(func, err, ...)
if type(func) == "function" and debug.getinfo(func).what ~= "C" then
setfenv(func, env)
end
if type(err) == "function" and debug.getinfo(err).what ~= "C" then
setfenv(err, env)
end
return _G.xpcall(func, err, ...)
end,
loadstring = function(...)
local a, b = _G.loadstring(...)
if type(a) == "function" then setfenv(a, env) end
return a, b
end,
},
{
__index = _G,
}
)
env._G = env
env.arg = {...}
setmetatable(
love,
{
__newindex = function(t, k, v)
if type(v) == "function" then
llog("love.%s = %s", k, v)
event.Call("LoveNewIndex", t, k, v)
setfenv(v, env)
end
rawset(t, k, v)
end,
}
)
do -- config
line.config = {
screen = {},
window = {},
modules = {},
height = 600,
width = 800,
title = "LINE no title",
author = "who knows",
}
if vfs.IsFile("conf.lua") then
local func = assert(vfs.LoadFile("conf.lua"))
setfenv(func, env)
func()
end
love.conf(line.config)
end
--check if line.config.screen exists
if not line.config.screen then line.config.screen = {} end
local w = line.config.screen.width or line.config.window.width or 800
local h = line.config.screen.height or line.config.window.height or 600
local title = line.config.title or "Line"
love.window.setMode(w, h)
love.window.setTitle(title)
local main = assert(vfs.LoadFile("main.lua"))
setfenv(main, env)
setfenv(love.line_update, env)
setfenv(love.line_draw, env)
line.pcall(love, main)
line.pcall(
love,
love.load,
{[-2] = "__LOVE_BINARY__", [-1] = "embedded boot.lua", [1] = folder .. "/"}
)
love.filesystem.setIdentity(love.filesystem.getIdentity())
vfs.Mount(love.filesystem.getUserDirectory())
line.current_game = love
love._line_env.love_game_update_draw_hack = false
return love
end
function line.IsGameRunning()
return line.current_game ~= nil
end
commands.Add("love_run=string,var_arg", function(name, ...)
local found
if vfs.IsDirectory("lovers/" .. name) then
found = line.RunGame("lovers/" .. name, ...)
elseif vfs.IsFile("lovers/" .. name .. ".love") then
found = line.RunGame("lovers/" .. name .. ".love", ...)
elseif name:find("github") then
local url = name
if name:starts_with("github/") then
url = name:gsub("github/", "https://github.com/") .. "/archive/master.zip"
else
url = url .. "/archive/master.zip"
end
local args = {...}
resource.Download(url):Then(function(full_path)
full_path = full_path .. "/" .. name:match(".+/(.+)") .. "-master"
logn("running downloaded löve game: ", full_path)
line.RunGame(full_path, unpack(args))
end)
else
for _, file_name in ipairs(vfs.Find("lovers/")) do
if file_name:compare(name) and vfs.IsDirectory("lovers/" .. file_name) then
found = line.RunGame("lovers/" .. file_name)
break
end
end
end
if found then
if menu then menu.Close() end
else
return false, "love game " .. name .. " does not exist"
end
end)
event.AddListener("WindowDrop", "line", function(wnd, path)
if vfs.IsDirectory(path) and vfs.IsFile(path .. "/main.lua") then
line.RunGame(path)
if menu then menu.Close() end
end
end)
return line