forked from Asnxthaony/Roco-Kingdom-World-Script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnLuaEx.lua
More file actions
309 lines (280 loc) · 7.33 KB
/
Copy pathUnLuaEx.lua
File metadata and controls
309 lines (280 loc) · 7.33 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
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
UE4 = UE
local setmetatable = _ENV.setmetatable
local setmetatable2 = _ENV.setmetatable2
local str_sub = string.sub
local rawget = _G.rawget
local rawset = _G.rawset
local rawequal = _G.rawequal
local type = _G.type
local getmetatable = _G.getmetatable
local getmetatable2 = _G.getmetatable2
local require = _G.require
local NewObject = _G.NewObject
local GetUProperty = _ENV.GetUProperty
local SetUProperty = _ENV.SetUProperty
local LuaClassIndex = _ENV.LuaClassIndex
local UnluaClass = UnLua.Class
local LuaClassIndexLuaOnly = _ENV.LuaClassIndexLuaOnly
local function RebindIndex(target)
local mt = getmetatable(target)
mt.__index = LuaClassIndex
mt.__newindex = LuaClassNewIndex
setmetatable(target, mt)
end
_NotExist = _NotExist or {}
local NotExist = _NotExist
local function Index(t, k)
local mt = getmetatable(t)
local super = mt
while super do
local v = rawget(super, k)
if nil ~= v and not rawequal(v, NotExist) then
rawset(t, k, v)
return v
end
super = rawget(super, "Super")
end
local p = mt[k]
if nil ~= p then
if "userdata" == type(p) then
return GetUProperty(t, p)
elseif "function" == type(p) then
rawset(t, k, p)
elseif rawequal(p, NotExist) then
return nil
end
else
rawset(mt, k, NotExist)
end
return p
end
local function NewIndex(t, k, v)
local mt = getmetatable(t)
local p = mt[k]
if "userdata" == type(p) then
return SetUProperty(t, p, v)
end
rawset(t, k, v)
end
function CopyFromClass(class, target)
local k = next(class)
while k do
local v = rawget(class, k)
local vt = rawget(target, k)
if not vt then
rawset(target, k, v)
end
k = next(class, k)
end
end
function CopyFromSuper(super, target)
local mt = super
while mt do
CopyFromClass(mt, target)
mt = rawget(mt, "Super")
end
end
local function BuildClassFunc(inst, ...)
local newCla = BuildClass(className, inst)
if newCla.Ctor then
newCla:Ctor(...)
end
return newCla
end
local originProtoClass = {
__call = function(self, ...)
return BuildClassFunc(self, ...)
end,
__tostring = function(self, ...)
return LuaClassToString(self) or ""
end
}
local function ExtendFunc(parentClass, claName)
return Class(claName, parentClass)
end
local function EmptyFunc()
end
local function InitializeFunc(self, ...)
RebindIndex(self)
if self.__Ctor then
self:__Ctor()
end
if self.Ctor then
self:Ctor(...)
end
if self.Initialize then
self:Initialize(...)
end
end
local function InitializeFuncWithoutRebind(self, ...)
if self.__Ctor then
self:__Ctor()
end
if self.Ctor then
self:Ctor(...)
end
if self.Initialize then
self:Initialize(...)
end
end
local function DctorFunction(self)
local __Dctor = LuaClassIndexLuaOnly(self, "__Dctor")
if __Dctor then
__Dctor(self)
end
end
local function SubclassFunc(self, superclass)
local super = self.Super
while super do
if super == superclass then
return true
end
super = super.Super
end
return false
end
local function InstanceOfFunc(self, fromclass)
return self.class == fromclass or self.class:SubclassOf(fromclass)
end
local function GetInstanceFunc(self)
return self.class
end
function BuildClass(className, super_class)
local instance = UnluaClass()
instance.class = instance
instance.Super = super_class
instance.Extend = ExtendFunc
instance.className = className
instance.name = className
instance.Initialize = EmptyFunc
instance.__Initialize = InitializeFunc
instance.SubclassOf = SubclassFunc
instance.InstanceOf = InstanceOfFunc
instance.__call = BuildClassFunc
instance.__index = instance
instance.__gc = DctorFunction
if super_class and "table" == type(super_class) then
setmetatable(instance, super_class)
setmetatable2(instance, super_class)
else
setmetatable(instance, originProtoClass)
setmetatable2(instance, originProtoClass)
end
return instance
end
local function CallClass(className, super_name)
local super_class
if nil ~= super_name then
if "string" == type(super_name) then
super_class = require(super_name)
else
super_class = super_name
end
end
return BuildClass(className, super_class)
end
local Class = {
IsClass = function(t)
return not not t.__CLASS
end,
IsInstance = function(t)
return not not t.__INSTANCE
end
}
setmetatable(Class, {
__call = function(_, ...)
return CallClass(...)
end
})
_G.print = print
_G.Class = Class
_G.NotExist = NotExist
_G.FilterTypeLst = {"TArray"}
_G.BuildVirtualClass = BuildVirtualClass
_G.RebindIndex = RebindIndex
local IS_SHIPPING = _G.RocoEnv.IS_SHIPPING
local SimpleClassOptimization = true
local CopyOrRecurse = false
local AutoAdjustPreAllocSize = true
local MakeSimpleClass
AutoAdjustPreAllocSize = AutoAdjustPreAllocSize and UE.NRCLuaUtils.GetHashSize ~= nil
local function ExtendSimpleClass(InParentKlass, SubClassName)
local SubClass = MakeSimpleClass(SubClassName, InParentKlass)
return SubClass
end
local function DefaultPreCtorFunc(Instance, ...)
end
local function DefaultSetPreAllocSizeFunc(Klass, Size)
local Parent = Klass.Super
local ParentSize = Parent and Parent.PreAllocSize or 4
local Total = ParentSize + Size
Klass.PreAllocSize = Total
end
local function MakeInstance(InKlass, ...)
local Instance = table.new(0, InKlass.PreAllocSize)
local PreCtor = InKlass.PreCtor or DefaultPreCtorFunc
PreCtor(Instance, ...)
Instance = setmetatable(Instance, InKlass)
InitializeFuncWithoutRebind(Instance, ...)
return Instance
end
local function DestructInstance(Instance)
if Instance and Instance.__Dctor then
Instance:__Dctor()
end
end
local function CheckAdjustPreAllocMem(InKlass, Instance)
if not InKlass.autoIncreasePreAllocSize then
return
end
local HashSize = UE.NRCLuaUtils.GetHashSize(Instance)
HashSize = 1 << HashSize - 1
if HashSize <= InKlass.PreAllocSize then
return
end
if not IS_SHIPPING then
if InKlass.className == "Unknown" then
Log.Error("Update HashSize", InKlass.className, HashSize, InKlass.PreAllocSize)
else
Log.Debug("Update HashSize", InKlass.className, HashSize, InKlass.PreAllocSize)
end
end
InKlass.PreAllocSize = HashSize
InKlass.autoIncreasePreAllocSize = false
end
local function MakeAndAnalyzeInstance(InKlass, ...)
local Instance = MakeInstance(InKlass, ...)
CheckAdjustPreAllocMem(InKlass, Instance)
return Instance
end
local CallableFunc = AutoAdjustPreAllocSize and MakeAndAnalyzeInstance or MakeInstance
local CallableMetatable = {__call = CallableFunc}
function MakeSimpleClass(Name, Parent)
local Klass = table.new(0, 16)
if Parent and CopyOrRecurse then
table.copy(Parent, Klass, true)
end
Klass.class = Klass
Klass.name = Name or "Unknown"
Klass.className = Name or "Unknown"
Klass.Super = Parent
Klass.InstanceOf = InstanceOfFunc
Klass.SubclassOf = SubclassFunc
Klass.Extend = ExtendSimpleClass
Klass.__index = Klass
Klass.__gc = DestructInstance
Klass.autoIncreasePreAllocSize = AutoAdjustPreAllocSize
Klass.PreAllocSize = 4
Klass.SetMemberCount = DefaultSetPreAllocSizeFunc
if Parent and not CopyOrRecurse then
setmetatable(Klass, {__call = CallableFunc, __index = Parent})
else
setmetatable(Klass, CallableMetatable)
end
return Klass
end
if SimpleClassOptimization then
_G.MakeSimpleClass = MakeSimpleClass
else
_G.MakeSimpleClass = Class
end