Some fixes to ModDataHandler, preventing desync with the table from ModData

This commit is contained in:
ZioPao
2023-11-17 20:05:41 +01:00
parent bd8dae128c
commit 877e5a711e
3 changed files with 32 additions and 10 deletions

View File

@@ -21,24 +21,32 @@ function ModDataHandler:new(username, isResetForced)
o.username = username
local key = CommandsData.GetKey(username)
-- We don't want to request ModData if we're in SP, or if we're forcing a reset
if isClient() and not isResetForced then
ModData.request(key)
o.tocData = ModData.get(key)
end
o.tocData = ModData.get(key) -- TODO This is working like a placeholder at the moment, it's gonna get replaced later in reapplyTocData
if isResetForced or o.tocData == nil or o.tocData.limbs == nil or o.tocData.limbs.Hand_L == nil or o.tocData.limbs.Hand_L.isCut == nil then
TOC_DEBUG.print("tocData in ModDataHandler for " .. username .. " is nil, creating it now")
o:setup(key)
end
TOC_DEBUG.print("initialized ModDataHandler for " .. username)
-- Transmit it to the server
ModData.transmit(key)
TOC_DEBUG.print("initialized ModDataHandler for " .. username)
ModDataHandler.instances[username] = o
return o
end
function ModDataHandler:initialize()
end
---Setup a new toc mod data data class
---@param key string
function ModDataHandler:setup(key)
@@ -79,6 +87,13 @@ function ModDataHandler:setup(key)
end
---In case of desync between the table on ModData and the table here
---@param key string
---@param tocData tocModData
function ModDataHandler:reapplyTocData(key, tocData)
ModData.add(key, tocData)
self.tocData = ModData.get(key)
end
-----------------
--* Setters *--
@@ -273,6 +288,7 @@ function ModDataHandler.ReceiveData(key, table)
if not isClient() then
TOC_DEBUG.print("SP, skipping ModDataHandler.ReceiveData")
end
TOC_DEBUG.print("receiving data from server")
if key == "TOC_Bob" then return end -- TODO Fix this
@@ -281,9 +297,10 @@ function ModDataHandler.ReceiveData(key, table)
TOC_DEBUG.print("table is nil... returning")
return
end
ModData.add(key, table) -- Add it to the client mod data (not sure)
-- Create ModDataHandler instance if there was none for that user and reapply the correct ModData table as a reference
local username = key:sub(5)
ModDataHandler.GetInstance(username)
ModDataHandler.GetInstance(username):reapplyTocData(key, table) --tocData = table -- TODO Ugly, use a setter
end
Events.OnReceiveGlobalModData.Add(ModDataHandler.ReceiveData)

View File

@@ -115,10 +115,12 @@ end)
--------------------------------------------------------------------------------------
if not getActivatedMods():contains("PerfTestFramework") or not isDebugEnabled() then return end
local PerfTest = require("PerfTest/main")
local PerfTest = require("PerfTest/main") -- SHould be global anyway
local CachedDataHandler = require("TOC/Handlers/CachedDataHandler")
PerfTest.Init()
PerfTest.RegisterMethod("PlayerHandler", PlayerHandler, "UpdateCicatrization")
PerfTest.RegisterMethod("CachedDataHandler", CachedDataHandler, "CalculateHighestAmputatedLimbs")
PerfTest.RegisterMethod("ISHealthPanel", ISHealthPanel, "render")
--PerfTest.RegisterMethod("PlayerHandler", PlayerHandler, "UpdateCicatrization")
--PerfTest.RegisterMethod("CachedDataHandler", CachedDataHandler, "CalculateHighestAmputatedLimbs")
--PerfTest.RegisterMethod("ISHealthPanel", ISHealthPanel, "render")

View File

@@ -14,7 +14,10 @@ end
---@param key string
---@param table tocModData
function ServerDataHandler.AddTable(key, table)
TOC_DEBUG.print("Adding table with key: " .. tostring(key))
print("TOC: received ModData => " .. key)
TOC_DEBUG.printTable(table)
--TOC_DEBUG.print("Adding table with key: " .. tostring(key))
ModData.add(key, table) -- Add it to the server mod data
ServerDataHandler.modData[key] = table
end