109 lines
2.6 KiB
Lua
109 lines
2.6 KiB
Lua
-- Tweaks
|
|
local plainRenderer = Renderer
|
|
Renderer = function(...)
|
|
local r = plainRenderer(...)
|
|
local mt = getmetatable(r)
|
|
local oldFunc = mt.__index.loadTexture
|
|
local textureWatcher = setmetatable({}, {__mode="k"})
|
|
local lastCallTime = os.time()
|
|
mt.__index.loadTexture = function(...)
|
|
print("loadTexture invoked.")
|
|
local text = oldFunc(...)
|
|
textureWatcher[text] = true
|
|
if os.time() - lastCallTime > 3 then
|
|
local cnt = 0
|
|
for t, b in pairs(textureWatcher) do
|
|
cnt = cnt + 1
|
|
end
|
|
print("Total weak texture: ", cnt)
|
|
if cnt > 100 then
|
|
collectgarbage()
|
|
end
|
|
lastCallTime = os.time()
|
|
end
|
|
return text
|
|
end
|
|
Renderer = plainRenderer
|
|
return r
|
|
end
|
|
|
|
-- Promise, Async/Await Helpers
|
|
function Promise(callback)
|
|
local p = {
|
|
__waiting = {},
|
|
__status = 0
|
|
}
|
|
|
|
p.resolve = function(...)
|
|
p.__ret = table.pack(...)
|
|
p.__status = 1
|
|
while #p.__waiting > 0 do
|
|
local co = p.__waiting[1]
|
|
table.remove(p.__waiting, 1)
|
|
coroutine.resume(co)
|
|
end
|
|
end
|
|
|
|
p.reject = function(err)
|
|
p.__ret = err
|
|
p.__status = 2
|
|
while #p.__waiting > 0 do
|
|
local co = p.__waiting[1]
|
|
table.remove(p.__waiting, 1)
|
|
coroutine.resume(co)
|
|
end
|
|
end
|
|
|
|
callback(p.resolve, p.reject)
|
|
|
|
return p
|
|
end
|
|
|
|
function await(p)
|
|
if p.__status ~= 0 then
|
|
if p.__status == 1 then
|
|
return table.unpack(p.__ret)
|
|
else
|
|
error(p.__ret)
|
|
end
|
|
else
|
|
local current = coroutine.running()
|
|
table.insert(p.__waiting, current)
|
|
coroutine.yield(p)
|
|
end
|
|
end
|
|
|
|
function async(fn)
|
|
return function()
|
|
local co = coroutine.create(fn)
|
|
local ret = table.pack(coroutine.resume(fn))
|
|
if coroutine.status(co) == "dead" then
|
|
local success = table.remove(ret, 1)
|
|
ret.n = ret.n - 1
|
|
local p = {}
|
|
if success then
|
|
p.__status = 1 -- resolved
|
|
p.__ret = ret
|
|
else
|
|
p.__status = 2 -- rejected
|
|
p.__ret = ret[1]
|
|
end
|
|
return p
|
|
else
|
|
return ret[2]
|
|
end
|
|
end
|
|
end
|
|
|
|
-- Load & run game
|
|
local gameMain, err = loadfile("code/network_test.lua")
|
|
if not gameMain then
|
|
print("Failed to load chunk.")
|
|
print(err)
|
|
else
|
|
xpcall(gameMain, function(err)
|
|
print("LuaMain Exception: ", err)
|
|
print(debug.traceback())
|
|
end)
|
|
end
|