-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponentLogic.lua
More file actions
149 lines (118 loc) · 5.02 KB
/
componentLogic.lua
File metadata and controls
149 lines (118 loc) · 5.02 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
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
local PATH = (...):match("^(.-)[^%.]+$")
local lfs = love.filesystem
local mintmousse = require(PATH .. "conf")
local contract = require(PATH .. "contract")
local loggerLogic = mintmousse._logger:extend("Component"):extend("Logic")
local componentLogic = { }
componentLogic.loadComponentLogic = function(componentTypeName)
if componentTypeName == "unknown" then
return
end
local componentType = contract.componentTypes[componentTypeName]
if not componentType.hasComponentLogic then
return -- Nothing to load
end
if componentType.componentLogic then
return -- Already loaded
end
local path
for i = #componentType.directories, 1, -1 do
path = componentType.directories[i] .. componentTypeName .. ".lua"
if lfs.getInfo(path, "file") then
break
end
path = nil
end
if not path then
loggerLogic:warning("Failed to discover path for component logic(" .. componentTypeName .. ") which was previous found in one of these directories:", table.concat(componentType.directories, ", "))
return
end
local componentLogicLoadFail = "Failed to load component logic! For '" .. componentTypeName .. "'. Reason:"
local success, chunk, errorMessage = pcall(lfs.load, path)
loggerLogic:assert(success, componentLogicLoadFail, chunk)
loggerLogic:assert(chunk, componentLogicLoadFail, errorMessage)
local success, componentLogic = pcall(chunk, mintmousse)
loggerLogic:assert(success, "Failed to run component logic! For:", componentTypeName, ". Reason:", componentLogic)
componentType.componentLogic = componentLogic
if type(componentType.componentLogic) ~= "table" then
loggerLogic:warning(componentLogicLoadFail, "Didn't return a table type as expected.")
componentType.componentLogic, componentType.hasComponentLogic = nil, false -- stop it from trying to reload
return
end
-- Per function
if componentType.componentLogic.onCreate ~= nil and type(componentType.componentLogic.onCreate) ~= "function" then
loggerLogic:warning(componentLogicLoadFail, "'onCreate' wasn't type function.")
componentType.componentLogic.onCreate = nil
end
if componentType.componentLogic.onChildCreate ~= nil and type(componentType.componentLogic.onChildCreate) ~= "function" then
loggerLogic:warning(componentLogicLoadFail, "'onChildCreate' wasn't type function.")
componentType.componentLogic.onChildCreate = nil
end
-- All functions
if type(componentType.componentLogic.onCreate) == "nil" and
type(componentType.componentLogic.onChildCreate) == "nil"
then
loggerLogic:warning(componentLogicLoadFail, "Returned component logic table didn't contain any functions for:", table.concat({ "onCreate", "onChildCreate" }, ", "))
componentType.componentLogic, componentType.hasComponentLogic = nil, false -- stop it from trying to reload
end
return
end
local protectedKeys = {
"id", "type", "parentID", "children",
}
local protectedKeyChangeWrapper = function(components, typeName, callbackName, callback, ...)
local savedStates = { }
for i, component in ipairs(components) do
savedStates[i] = { }
for _, key in ipairs(protectedKeys) do
savedStates[i][key] = component[key]
end
end
callback(...)
local componentChangedMsg = "Tried to change component '%s' within '" .. callbackName .. "', type: " .. typeName .. ". This is a protected value at this stage of creation."
for i, component in ipairs(components) do
local savedState = savedStates[i]
for _, key in ipairs(protectedKeys) do
if component[key] ~= savedState[key] then
loggerLogic:error(componentChangedMsg:format(key))
end
end
end
end
componentLogic.runOnCreate = function(component, componentType)
local typeName = component.type
local callback = componentType.componentLogic.onCreate
protectedKeyChangeWrapper({ component }, typeName, "onCreate", callback, component)
end
componentLogic.runOnChildCreate = function(childComponent, parentComponent, parentComponentType)
local typeName = parentComponent.type
local callback = parentComponentType.componentLogic.onChildCreate
protectedKeyChangeWrapper({ parentComponent, childComponent }, typeName, "onChildCreate", callback, parentComponent, childComponent)
end
componentLogic.run = function(component, parentComponent)
componentLogic.loadComponentLogic(component.type)
if parentComponent then
componentLogic.loadComponentLogic(parentComponent.type)
end
local componentType = contract.componentTypes[component.type]
if componentType.componentLogic then
if componentType.componentLogic.onCreate then
componentLogic.runOnCreate(component, componentType)
end
end
-- Parent
if not parentComponent then
return
end
if parentComponent.type == "unknown" then
return
end
local parentComponentType = contract.componentTypes[parentComponent.type]
if not parentComponentType.componentLogic then
return
end
if parentComponentType.componentLogic.onChildCreate then
componentLogic.runOnChildCreate(component, parentComponent, parentComponentType)
end
end
return componentLogic