-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodmain.lua
373 lines (346 loc) · 13.8 KB
/
modmain.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
local _G = GLOBAL
local require = _G.require
local __commonUtils = require('utils/common')(GLOBAL, env)
--local IsServerSide = __commonUtils.IsServerSide
local IsClientSide = __commonUtils.IsClientSide
local AddPlayerPostInit = __commonUtils.AddPlayerPostInit
local Findomizer = require('models/findomizer')
local __highlight = require('utils/highlight')(GLOBAL, env)
--local ApplyHighlight = __highlight.ApplyHighlight
--local ClearHighlight = __highlight.ClearHighlight
--
--local client_option = GetModConfigData("active", true) -- to allow clients to disable highlighting
-- stringify lib
local function val_to_str ( v, indent )
if "string" == type( v ) then
v = string.gsub( v, "\n", "\\n" )
if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
return "'" .. v .. "'"
end
return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
elseif "table" == type( v ) then
if indent == " " then
return "~TooDeep~"
else
return table.stringify( v, indent )
end
else
return tostring( v )
end
end
local function key_to_str ( k )
if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
return k
else
return "[" .. tostring( k ) .. "]"
end
end
table.stringify = function( tbl, indent )
indent = indent or ""
local count = 0
local result, done = {}, {}
if type( tbl ) ~= 'table' then
return val_to_str( tbl )
end
for k, v in ipairs( tbl ) do
table.insert( result, val_to_str( v, indent.." " ) )
done[ k ] = true
count = count + 1
end
for k, v in pairs( tbl ) do
if not done[ k ] then
table.insert( result, indent..key_to_str( k ) .. "=" .. val_to_str( v, indent.." " ) )
count = count + 1
end
end
if count > 1 then
return "{\n" .. table.concat( result, ",\n" ) .. "\n"..indent.."}"
else
return "{ " .. table.concat( result, "," ) .. " }"
end
end
-- /stringify lib
--
--local function filter(chest, item)
-- return chest.components.container and item and
-- chest.components.container:Has(item, 1)
--end
--
---- because of server client communication delay, everything has to be done in a row, in this order:
---- unhighlight at client -> unhighlight at server (set nil) -> server highlight (:set()) -> client highlighted with OnDirtyEventSearchedChest
--
--local function ServerRPCFunction(owner,prefab,source,unhighlighten,highlighten)
-- -- print("serverfkt called")
-- if unhighlighten then
-- local v = nil
-- for i=1,50 do
-- v = owner["mynetvarSearchedChest"..tostring(i)]:value()
-- if v~=nil and not (not v:HasTag("HighlightSourceCraftPot") and source=="CraftPotClose") then
-- -- print("server unhighlight "..tostring(v).." hattag: "..tostring(v:HasTag("HighlightSourceCraftPot")).." source: "..tostring(source))
-- v:RemoveTag("HighlightSourceCraftPot")
-- owner["mynetvarSearchedChest"..tostring(i)]:set(nil) -- remove tag and set nil at serverside
-- end
-- end
-- end
-- if highlighten then
-- if owner and prefab then -- if no prefab is given, nothing will be highlighted :)
-- local x, y, z = owner.Transform:GetWorldPosition()
-- local e = TheSim:FindEntities(x, y, z, 20, nil, {'NOBLOCK', 'player', 'FX'}) or {}
-- for k, v in pairs(e) do
-- if v and v:IsValid() and v.entity:IsVisible() and filter(v,prefab) then
-- -- print("server highlight "..tostring(v))
-- if source=="CraftPot" then
-- v:AddTag("HighlightSourceCraftPot") -- when craft pot closes, it should unhighlight everything that was previously highlighted by craftpot. That's why we use this Tag and all the source stuff
-- end
-- for i=1,50 do
-- if owner["mynetvarSearchedChest"..tostring(i)]:value()==nil then -- look for empty slot
-- v.highlightsource = source -- add this temporary info
-- owner["mynetvarSearchedChest"..tostring(i)]:set(v)
-- break
-- end
-- end
-- end
-- end
-- end
-- end
--end
--
--local function ClientUnhighlightChests(owner,prefab,source,unhighlighten,highlighten)
-- print ('ClientUnhighlightChests')
-- if IsClientSide then -- only client pass
-- print ('I am Client')
-- if unhighlighten then
-- -- print("client unhighlight start")
-- for i=1,50 do -- alle bekannten Chests unhighlighten
-- local chest = owner["mynetvarSearchedChest"..tostring(i)]:value()
-- if chest and chest.components.highlight then
-- -- print("client unhighlight "..tostring(chest).." hattag: "..tostring(chest:HasTag("HighlightSourceCraftPot")).." source: "..tostring(source))
-- if not (not chest:HasTag("HighlightSourceCraftPot") and source=="CraftPotClose") then
--
-- if chest.components.highlight.ApplyColour == ApplyHighlight then
-- chest.components.highlight.ApplyColour = nil
-- end
--
-- if chest.components.highlight.UnHighlight == ClearHighlight then
-- chest.components.highlight.UnHighlight = nil
-- end
--
-- chest.components.highlight:UnHighlight()
-- end
-- end
-- end
-- end
-- if IsServerSide then -- can be both client and server for player 1
-- print("~~~ServerSide call");
-- ServerRPCFunction(owner,prefab,source,unhighlighten,highlighten) -- call it directly without rpc, if we are also server
-- else
-- local rpc = GetModRPC("FinderMod", "CheckContainersItem")
-- SendModRPCToServer(rpc,prefab,source,unhighlighten,highlighten)
-- end
-- end
--end
--
--local function DoHighlightStuff(owner,prefab,source,unhighlighten,highlighten)
-- print ('Do highlight stuff please')
-- if IsClientSide and owner==GLOBAL.ThePlayer then
-- print ('Do highlight as client')
-- ClientUnhighlightChests(owner,prefab,source,unhighlighten,highlighten)
-- end
--end
--
--local function onactiveitem(owner,data)
-- print ('On active item call')
-- local prefab = data.item and data.item.prefab or nil
-- local source = "newactiveitem"
-- if owner and prefab then -- unhighlight + highlight
-- DoHighlightStuff(owner,prefab,source,true,true)
-- else -- only unhighlight
-- DoHighlightStuff(owner,prefab,source,true,false)
-- end
--end
--
--local function OnDirtyEventSearchedChest(inst,i) -- this is called on client, if the server does inst.mynetvarTitleStufff:set(...)
-- -- print("OnDirtyEventSearchedChest i "..tostring(i))
-- if IsClientSide and inst==GLOBAL.ThePlayer then -- only this specific client pass
-- if client_option then
-- local chest = inst["mynetvarSearchedChest"..tostring(i)]:value()
-- if chest then
-- -- print("client highlight event number "..tostring(i).." chest: "..tostring(chest))
-- if not chest.components.highlight then
-- chest:AddComponent('highlight')
-- end
--
-- if chest.components.highlight then
-- chest.components.highlight.ApplyColour = ApplyHighlight
-- chest.components.highlight.UnHighlight = ClearHighlight
-- chest.components.highlight:Highlight(0, 0, 0)
-- end
-- end
-- end
-- end
--end
--
--
--local function RegisterListeners(inst)
-- for i=1,50 do
-- inst:ListenForEvent("DirtyEventSearchedChest"..tostring(i), function(inst) inst:DoTaskInTime(0,OnDirtyEventSearchedChest(inst,i)) end)
-- end
--end
--
--local function init(inst)
-- if not inst then return end
-- for i=1,50 do -- allow up to 50 containers (cause we can't use an array of entities, we use 50 entity netvars)
-- inst["mynetvarSearchedChest"..tostring(i)] = GLOBAL.net_entity(inst.GUID, "SearchedChest"..tostring(i).."NetStuff", "DirtyEventSearchedChest"..tostring(i))
-- inst["mynetvarSearchedChest"..tostring(i)]:set(nil)
-- end
-- inst:DoTaskInTime(0, RegisterListeners)
-- inst:ListenForEvent('newactiveitem', onactiveitem)
--end
--
--AddPlayerPostInit(function (owner)
-- print ('Adding player post init')
-- init(owner)
--end)
--
--AddModRPCHandler("FinderMod", "CheckContainersItem", ServerRPCFunction)
--
---- ######################
--
----[ highlighting when ingredient in recipepopup is hovered
--AddClassPostConstruct("widgets/ingredientui", function(self)
-- local __IngredientUI_OnGainFocus = self.OnGainFocus
--
-- function self:OnGainFocus (...)
-- local tex = self.ing and self.ing.texture and self.ing.texture:match('[^/]+$'):gsub('%.tex$', '')
-- local owner = self.parent and self.parent.parent and self.parent.parent.owner
-- if tex and owner then
-- DoHighlightStuff(owner,tex,"Crafting",true,true)
-- end
-- if __IngredientUI_OnGainFocus then
-- return __IngredientUI_OnGainFocus(self, ...)
-- end
-- end
--
-- local __IngredientUI_OnLoseFocus = self.OnLoseFocus
-- function self:OnLoseFocus(...)
-- local owner = self.parent and self.parent.parent and self.parent.parent.owner
-- DoHighlightStuff(owner,nil,"Crafting",true,false)
-- if __IngredientUI_OnLoseFocus then
-- return __IngredientUI_OnLoseFocus(self,...)
-- end
-- end
--end)
--
--
---- ### Compatible to Craft Pot Mod, to find food with the searched tags
--local function testCraftPot()
-- local FoodIngredientUI = _G.require 'widgets/foodingredientui'
--end
--if GLOBAL.pcall(testCraftPot) then
-- local cooking = _G.require("cooking")
-- local ing = cooking.ingredients
--
-- AddClassPostConstruct("widgets/foodingredientui", function(self)
-- local __FoodIngredientUI_OnGainFocus = self.OnGainFocus
-- function self:OnGainFocus(...)
-- local searchtag = self.prefab -- tag or name
-- local isname = self.is_name
-- local owner = self.owner
-- local prefabs = {} -- find all the prefabs with that cooking tag
--
-- if not isname then
-- for prefab,xyz in pairs(ing) do
-- for tag,number in pairs(xyz.tags) do
-- if tag==searchtag then
-- table.insert(prefabs,prefab)
-- end
-- end
-- end
-- elseif isname and GLOBAL.PREFABDEFINITIONS[searchtag] then
-- table.insert(prefabs,GLOBAL.PREFABDEFINITIONS[searchtag].name)
-- end
-- DoHighlightStuff(owner,nil,"CraftPot",true,false) -- to unhighlight everything
-- for k,prefab in pairs(prefabs) do -- send one prefab after the other, cause sedning an array via rpc does not work..
-- if prefab and owner then
-- DoHighlightStuff(owner,prefab,"CraftPot",false,true) -- highlight every prefab, without unhighlighting in between
-- end
-- end
-- if __FoodIngredientUI_OnGainFocus then
-- return __FoodIngredientUI_OnGainFocus(self,...)
-- end
-- end
-- end)
--
-- AddClassPostConstruct("widgets/foodcrafting", function(self)
-- local _OnLoseFocus = self.OnLoseFocus
-- self.OnLoseFocus = function(...)
-- local owner = self.owner
-- DoHighlightStuff(owner,nil,"CraftPot",true,false) -- to unhighlight
-- if _OnLoseFocus then
-- return _OnLoseFocus(self, ...)
-- end
-- end
--
-- local _Close = self.Close
-- self.Close = function(...)
-- local owner = self.owner
-- DoHighlightStuff(owner,nil,"CraftPotClose",true,false) -- this unhighlight ony should work for highlights made with craft pot
-- if _Close then
-- return _Close(self, ...)
-- end
-- end
-- end)
--end
--
--
--AddClassPostConstruct("widgets/tabgroup", function(self)
-- local __TabGroup_DeselectAll = self.DeselectAll
-- function self:DeselectAll(...)
-- DoHighlightStuff(GLOBAL.ThePlayer,nil,"CraftingClose",true,false)
-- return __TabGroup_DeselectAll(self, ...)
-- end
--end)
--
local function ContainerPostConstruct(replica, inst)
print('initialize container')
if inst:HasTag('structure') then
print('FOUND STRUCTURE TAG')
Findomizer:AddContainer(inst)
inst.components.container_memory:InjectHandlers(replica)
end
end
local function ItemTilePostConstruct(self)
local OriginalGainFocusHandler = self.OnGainFocus
function self:OnGainFocus(...)
local prefab = self.item and self.item.prefab
print('~~~~~Hover a '..tostring(prefab)..', Wooohooo')
if prefab then
-- highlight containers containing hovered item
Findomizer:HighlightItems({prefab})
end
if OriginalGainFocusHandler then
return OriginalGainFocusHandler(self,...)
end
end
local OriginalLoseFocusHandler = self.OnLoseFocus
function self:OnLoseFocus(...)
print('~~~~~Dehover a '..tostring(prefab)..', Wooohooo')
Findomizer:ClearHighlight()
if OriginalLoseFocusHandler then
return OriginalLoseFocusHandler(self,...)
end
end
end
-- first block is used for DST clients, second - for DS/DST Host
if IsClientSide then
AddClassPostConstruct("components/container_replica", ContainerPostConstruct)
AddClassPostConstruct("widgets/itemtile", ItemTilePostConstruct)
print('Running on Clint side')
else
AddPrefabPostInitAny(function(inst)
if inst.components.container then
ContainerPostConstruct(inst.components.container, inst)
end
end)
print('Running on Host')
end