From 877e5a711e5f874b296ab9daad4aefa7e3dcccf0 Mon Sep 17 00:00:00 2001 From: ZioPao Date: Fri, 17 Nov 2023 20:05:41 +0100 Subject: [PATCH] Some fixes to ModDataHandler, preventing desync with the table from ModData --- .../client/TOC/Handlers/ModDataHandler.lua | 27 +++++++++++++++---- media/lua/client/TOC/Tests.lua | 10 ++++--- media/lua/server/TOC/ServerDataHandler.lua | 5 +++- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/media/lua/client/TOC/Handlers/ModDataHandler.lua b/media/lua/client/TOC/Handlers/ModDataHandler.lua index a196a2f..a732295 100644 --- a/media/lua/client/TOC/Handlers/ModDataHandler.lua +++ b/media/lua/client/TOC/Handlers/ModDataHandler.lua @@ -21,24 +21,32 @@ function ModDataHandler:new(username, isResetForced) o.username = username local key = CommandsData.GetKey(username) - ModData.request(key) - o.tocData = ModData.get(key) + -- 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) + 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) diff --git a/media/lua/client/TOC/Tests.lua b/media/lua/client/TOC/Tests.lua index d5ee17c..ceca66f 100644 --- a/media/lua/client/TOC/Tests.lua +++ b/media/lua/client/TOC/Tests.lua @@ -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") \ No newline at end of file + + +--PerfTest.RegisterMethod("PlayerHandler", PlayerHandler, "UpdateCicatrization") +--PerfTest.RegisterMethod("CachedDataHandler", CachedDataHandler, "CalculateHighestAmputatedLimbs") +--PerfTest.RegisterMethod("ISHealthPanel", ISHealthPanel, "render") \ No newline at end of file diff --git a/media/lua/server/TOC/ServerDataHandler.lua b/media/lua/server/TOC/ServerDataHandler.lua index 0d64a53..430f0c6 100644 --- a/media/lua/server/TOC/ServerDataHandler.lua +++ b/media/lua/server/TOC/ServerDataHandler.lua @@ -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