-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontract.lua
More file actions
76 lines (62 loc) · 2.63 KB
/
contract.lua
File metadata and controls
76 lines (62 loc) · 2.63 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
local PATH = (...):match("^(.-)[^%.]+$")
local mintmousse = require(PATH .. "conf")
local codec = require(PATH .. "codec")
local loggerContract = mintmousse._logger:extend("Contract")
local contract = { }
local componentTypesChannel = love.thread.getChannel(mintmousse.READONLY_BASIC_TYPES_ID)
local timeoutValue = mintmousse.COMPONENT_PARSE_TIMEOUT
contract.waitForComponents = function()
contract.componentTypes = componentTypesChannel:peek()
if contract.componentTypes ~= nil then
-- Components are already loaded and ready to go
if not love.isThread then -- is Main thread
loggerContract:info("MintMousse components successfully loaded")
end
return
end
local thread = love.thread.getChannel(mintmousse.READONLY_THREAD_LOCATION):peek()
loggerContract:assert(thread, "MintMousse Thread object is unexpectedly missing.")
loggerContract:info("Blocking thread to wait for MintMousse component load.")
local timeout = false
local start = love.timer.getTime()
while thread:isRunning() do
contract.componentTypes = componentTypesChannel:peek()
if contract.componentTypes ~= nil then
break
end
if love.timer.getTime() - start >= timeoutValue then
timeout = true
break
end
love.timer.sleep(5e-4)
end
local timeElapsed = love.timer.getTime() - start
if contract.componentTypes ~= nil then
contract.componentTypes = codec.decode(contract.componentTypes)
loggerContract:info("MintMousse components successfully loaded",
("(blocked main thread for %.2fms)."):format(timeElapsed*1000))
return
end
if timeout then
loggerContract:warning("Timeout reached ("..timeoutValue.."s) while waiting for MintMousse Thread to load components.",
"Consider increasing the timeout ("..timeoutValue.."s).")
end
if not thread:isRunning() then
loggerContract:warning("Thread isn't running while waiting for componentTypes. Checking for errors.")
local errorMessage = thread:getError()
if errorMessage then
if type(love.handlers) == "table" and love.handlers["threaderror"] then
-- love.handlers shouldn't be initialised yet; but check anyway
pcall(love.handlers["threaderror"], thread, errorMessage)
elseif love.event then -- fallback
love.event.push("threaderrror", thread, errorMessage)
end
loggerContract:error("MintMousse's thread encountered an error:", errorMessage)
else
loggerContract:warning("The thread object reported no error.",
"This suggests the MintMousse Thread is stuck or overloaded.",
"Consider increasing the timeout ("..timeoutValue.."s).")
end
end
end
return contract