Reworking initialization and caching

This commit is contained in:
ZioPao
2024-01-07 21:21:05 +01:00
parent 1098333247
commit 4cf97a2e79
7 changed files with 331 additions and 261 deletions

View File

@@ -1,7 +1,7 @@
{
"todo-tree.tree.scanMode": "workspace",
"zomboid_user_folder": "C:/Users/picch/Zomboid",
"zomboid_folder": "C:\\Program Files (x86)\\Steam\\steamapps\\common\\ProjectZomboid",
"zomboid_folder": "E:\\Steam\\steamapps\\common\\ProjectZomboid",
"zomboid_server_folder": "E:\\Steam\\steamapps\\common\\Project Zomboid Dedicated Server",
"Lua.diagnostics.globals": [
"ModOptions",

View File

@@ -19,8 +19,11 @@ CachedDataHandler.amputatedLimbs = {}
---Calculate the currently amputated limbs for a certain player
---@param username string
function CachedDataHandler.CalculateAmputatedLimbs(username)
TOC_DEBUG.print("[CachedDataHandler] Calculating amputated limbs for " .. username)
CachedDataHandler.amputatedLimbs[username] = {}
local modDataHandler = ModDataHandler.GetInstance(username)
-- TODO If the data hasn't arrived, this won't work
for i=1, #StaticData.LIMBS_STR do
local limbName = StaticData.LIMBS_STR[i]
if modDataHandler:getIsCut(limbName) then
@@ -29,6 +32,8 @@ function CachedDataHandler.CalculateAmputatedLimbs(username)
end
end
---Add an amputated limb to the cached list for that user
---@param username string
---@param limbName string
@@ -52,6 +57,13 @@ CachedDataHandler.highestAmputatedLimbs = {}
---Calcualate the highest point of amputations achieved by the player
---@param username string
function CachedDataHandler.CalculateHighestAmputatedLimbs(username)
TOC_DEBUG.print("[CachedDataHandler] Triggered CalculateHighestAmputatedLimbs")
local modDataHandler = ModDataHandler.GetInstance(username)
if modDataHandler == nil then
TOC_DEBUG.print("ModDataHandler not found for " .. username)
return
end
if CachedDataHandler.amputatedLimbs == nil or CachedDataHandler.amputatedLimbs[username] == nil then
--- This function gets ran pretty early, we need to account for the Bob stuff
-- if username == "Bob" then
@@ -66,11 +78,6 @@ function CachedDataHandler.CalculateHighestAmputatedLimbs(username)
local amputatedLimbs = CachedDataHandler.amputatedLimbs[username]
CachedDataHandler.highestAmputatedLimbs[username] = {}
--TOC_DEBUG.print("Searching highest amputations for " .. username)
local modDataHandler = ModDataHandler.GetInstance(username)
if modDataHandler == nil then
TOC_DEBUG.print("ModDataHandler not found for " .. username)
return
end
for k, _ in pairs(amputatedLimbs) do
local limbName = k
@@ -82,6 +89,7 @@ function CachedDataHandler.CalculateHighestAmputatedLimbs(username)
end
end
---Get the cached highest point of amputation for each side
---@param username string
---@return table

View File

@@ -6,6 +6,8 @@ local StaticData = require("TOC/StaticData")
---@class ModDataHandler
---@field username string
---@field tocData tocModData
---@field isDataReady boolean
---@field isResetForced boolean
local ModDataHandler = {}
ModDataHandler.instances = {}
@@ -14,6 +16,8 @@ ModDataHandler.instances = {}
---@param isResetForced boolean?
---@return ModDataHandler
function ModDataHandler:new(username, isResetForced)
TOC_DEBUG.print("[ModDataHandler] NEW for " .. username)
--error("TEST TRIGGER")
local o = {}
setmetatable(o, self)
self.__index = self
@@ -24,32 +28,23 @@ function ModDataHandler:new(username, isResetForced)
-- 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.isResetForced = isResetForced
o.isDataReady = false
elseif isResetForced then
self:setup(key)
o.isResetForced = false
o.isDataReady = true
end
-- FIXME This is working like a placeholder at the moment, it's gonna get replaced later in reapplyTocData
o.tocData = ModData.get(key)
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
-- 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)
TOC_DEBUG.print("[ModDataHandler] Running setup")
---@type tocModData
self.tocData = {
@@ -89,9 +84,9 @@ 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)
function ModDataHandler:reapplyTocData(tocData)
local key = CommandsData.GetKey(self.username)
ModData.add(key, tocData)
self.tocData = ModData.get(key)
end
@@ -99,6 +94,10 @@ end
-----------------
--* Setters *--
function ModDataHandler:setIsDataReady(isDataReady)
self.isDataReady = isDataReady
end
---Set a generic boolean that toggles varies function of the mod
---@param isAnyLimbCut boolean
function ModDataHandler:setIsAnyLimbCut(isAnyLimbCut)
@@ -171,6 +170,11 @@ end
-----------------
--* Getters *--
---comment
---@return boolean
function ModDataHandler:getIsDataReady()
return self.isDataReady
end
---Set a generic boolean that toggles varies function of the mod
---@return boolean
function ModDataHandler:getIsAnyLimbCut()
@@ -187,6 +191,8 @@ end
---@param limbName string
---@return boolean
function ModDataHandler:getIsCut(limbName)
if not self.isDataReady then return false end
if self.tocData.limbs[limbName] then
return self.tocData.limbs[limbName].isCut
else
@@ -198,6 +204,8 @@ end
---@param limbName string
---@return boolean
function ModDataHandler:getIsVisible(limbName)
if not self.isDataReady then return false end
return self.tocData.limbs[limbName].isVisible
end
@@ -314,12 +322,10 @@ function ModDataHandler.ReceiveData(key, table)
if not isClient() then
TOC_DEBUG.print("SP, skipping ModDataHandler.ReceiveData")
end
TOC_DEBUG.print("receiving data from server")
-- During startup the game can return Bob as the player username, adding a useless ModData table
if key == "TOC_Bob" then return end
TOC_DEBUG.print("receive data for " .. key)
TOC_DEBUG.print("[ModDataHandler] ReceiveData for " .. key)
if table == {} or table == nil then
TOC_DEBUG.print("table is nil... returning")
return
@@ -327,8 +333,39 @@ function ModDataHandler.ReceiveData(key, table)
-- 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):reapplyTocData(key, table)
-- FIXME Triggers an error at startup during initialization
local handler = ModDataHandler.GetInstance(username)
if handler.isResetForced then
handler:setup(key)
handler.isResetForced = false
else
handler:reapplyTocData(table)
end
-- if handler.isResetForced or handler.tocData == nil or handler.tocData.limbs == nil or handler.tocData.limbs.Hand_L == nil or handler.tocData.limbs.Hand_L.isCut == nil then
-- TOC_DEBUG.print("tocData in ModDataHandler for " .. handler.username .. " is nil, creating it now")
-- handler:setup(key)
-- handler.isResetForced = false
-- elseif table then
-- TOC_DEBUG.print("Reapply toc data for " .. handler.username)
-- handler:reapplyTocData(table)
-- end
handler:setIsDataReady(true)
-- Event
triggerEvent("OnReceivedTocData", handler.username)
-- Transmit it to the server
ModData.transmit(key)
TOC_DEBUG.print("[ModDataHandler] Transmitting data after receiving it for: " .. handler.username)
end
Events.OnReceiveGlobalModData.Add(ModDataHandler.ReceiveData)
-------------------
@@ -341,6 +378,7 @@ function ModDataHandler.GetInstance(username)
end
if ModDataHandler.instances[username] == nil then
TOC_DEBUG.print("[ModDataHandler] Creating NEW instance for " .. username)
return ModDataHandler:new(username)
else
return ModDataHandler.instances[username]

View File

@@ -22,15 +22,16 @@ local PlayerHandler = {}
---@param isForced boolean?
function PlayerHandler.InitializePlayer(playerObj, isForced)
local username = playerObj:getUsername()
TOC_DEBUG.print("initializing local player: " .. username)
TOC_DEBUG.print("[PlayerHandler] Initializing local player: " .. username)
ModDataHandler:new(username, isForced)
PlayerHandler.playerObj = playerObj
PlayerHandler.username = username
-- Calculate amputated limbs and highest point of amputations at startup
CachedDataHandler.CalculateAmputatedLimbs(username)
CachedDataHandler.CalculateHighestAmputatedLimbs(username)
--CachedDataHandler.CalculateAmputatedLimbs(username)
--CachedDataHandler.CalculateHighestAmputatedLimbs(username)
--Setup the CicatrizationUpdate event and triggers it once
Events.OnAmputatedLimb.Add(PlayerHandler.ToggleUpdateAmputations)
@@ -42,6 +43,10 @@ function PlayerHandler.InitializePlayer(playerObj, isForced)
ItemsHandler.Player.DeleteAllOldAmputationItems(playerObj)
CachedDataHandler.Reset(username)
end
-- Overrides various functions on the Health Panel ONLY after we're done initializing the player
OverrideHealthPanelForTOC()
end
---Handles the traits
@@ -235,7 +240,15 @@ function PlayerHandler.UpdateAmputations()
--local bodyPart = bd:getBodyPart(bptEnum)
local modifier = 0.01 * SandboxVars.TOC.WoundDirtynessMultiplier
local dirtyness = visual:getDirt(bbptEnum) + visual:getBlood(bbptEnum) + modDataHandler:getWoundDirtyness(limbName) + modifier
--------------
-- TEST SECTION
local dirtynessVis = visual:getDirt(bbptEnum) + visual:getBlood(bbptEnum)
local dirtynessWound = modDataHandler:getWoundDirtyness(limbName) + modifier
--------------
local dirtyness = dirtynessVis + dirtynessWound
if dirtyness > 1 then
dirtyness = 1
@@ -268,7 +281,7 @@ end
---Starts safely the loop to update cicatrzation
function PlayerHandler.ToggleUpdateAmputations()
TOC_DEBUG.print("activating amputation handling loop (if it wasn't active before)")
TOC_DEBUG.print("[PlayerHandler] Activating amputation handling loop (if it wasn't active before)")
CommonMethods.SafeStartEvent("EveryHours", PlayerHandler.UpdateAmputations)
end

View File

@@ -46,13 +46,19 @@ end
function Main.SetupEvents()
--Triggered when a limb has been amputated
LuaEventManager.AddEvent("OnAmputatedLimb")
-- Triggered when data is ready
LuaEventManager.AddEvent("OnReceivedTocData")
local CachedDataHandler = require("TOC/Handlers/CachedDataHandler")
Events.OnReceivedTocData.Add(CachedDataHandler.CalculateHighestAmputatedLimbs)
end
function Main.Initialize()
---Looop until we've successfully initialized the mod
local function TryToInitialize()
local pl = getPlayer()
TOC_DEBUG.print("Current username in TryToInitialize: " .. pl:getUsername())
TOC_DEBUG.print("[Main] Current username in TryToInitialize: " .. pl:getUsername())
if pl:getUsername() == "Bob" then
TOC_DEBUG.print("Username is still Bob, waiting")
return

View File

@@ -3,7 +3,9 @@ local ModDataHandler = require("TOC/Handlers/ModDataHandler")
local CachedDataHandler = require("TOC/Handlers/CachedDataHandler")
local CutLimbHandler = require("TOC/UI/CutLimbInteractions")
local WoundCleaningHandler = require("TOC/UI/WoundCleaningInteraction")
---------------------------------
function OverrideHealthPanelForTOC()
-- We're overriding ISHealthPanel to add custom textures to the body panel.
-- By doing so we can show the player which limbs have been cut without having to use another menu
@@ -60,7 +62,7 @@ function ISHealthPanel:initialise()
local username = self.character:getUsername()
if username ~= "Bob" then
CachedDataHandler.CalculateHighestAmputatedLimbs(username)
--CachedDataHandler.CalculateHighestAmputatedLimbs(username)
self.highestAmputations = CachedDataHandler.GetHighestAmputatedLimbs(username)
end
@@ -86,7 +88,7 @@ end
---Try to draw the highest amputation in the health panel, based on the cicatrization time
---@param side string L or R
---@param username string
function ISHealthPanel:tryDrawHighestAmputation(side, username)
function ISHealthPanel:tryDrawHighestAmputation(highestAmputations, side, username)
local redColor
local texture
@@ -94,12 +96,21 @@ function ISHealthPanel:tryDrawHighestAmputation(side, username)
redColor = 1
texture = getTexture("media/ui/test_pattern.png")
else
if self.highestAmputations[side] == nil then return end
local limbName = self.highestAmputations[side]
if highestAmputations[side] == nil then return end
local limbName = highestAmputations[side]
TOC_DEBUG.print("Drawing " .. tostring(limbName) .. " for " .. username)
local cicTime = ModDataHandler.GetInstance(username):getCicatrizationTime(limbName)
redColor = GetColorFromCicatrizationTime(cicTime, limbName)
texture = StaticData.HEALTH_PANEL_TEXTURES[self.sexPl][limbName]
local sexPl
if self.character:isFemale() then
sexPl = "Female"
else
sexPl = "Male"
end
texture = StaticData.HEALTH_PANEL_TEXTURES[sexPl][limbName]
end
self:drawTexture(texture, self.healthPanel.x, self.healthPanel.y, 1, redColor, 0, 0)
@@ -109,18 +120,22 @@ local og_ISHealthPanel_render = ISHealthPanel.render
function ISHealthPanel:render()
og_ISHealthPanel_render(self)
local username = self.character:getUsername()
self.highestAmputations = CachedDataHandler.GetHighestAmputatedLimbs(username)
if self.highestAmputations ~= nil then
--CachedDataHandler.CalculateHighestAmputatedLimbs(username)
local highestAmputations = CachedDataHandler.GetHighestAmputatedLimbs(username)
-- TODO Client should send update to other client somehow
if highestAmputations ~= nil then
-- Left Texture
self:tryDrawHighestAmputation("L", username)
self:tryDrawHighestAmputation(highestAmputations, "L", username)
-- Right Texture
self:tryDrawHighestAmputation("R", username)
else
-- Request caching data
TOC_DEBUG.print("highest amputated limbs was nil, calculating and getting it now for" .. username)
CachedDataHandler.CalculateHighestAmputatedLimbs(username)
self:tryDrawHighestAmputation(highestAmputations, "R", username)
-- else
-- -- Request caching data
-- TOC_DEBUG.print("highest amputated limbs was nil, calculating and getting it now for " .. username)
-- CachedDataHandler.CalculateHighestAmputatedLimbs(username)
end
end
@@ -161,8 +176,7 @@ end
local og_ISMedicalCheckAction_perform = ISMedicalCheckAction.perform
function ISMedicalCheckAction:perform()
local username = self.otherPlayer:getUsername()
TOC_DEBUG.print("creating instance for " .. username )
ModDataHandler.GetInstance(username)
TOC_DEBUG.print("Medical Action on " .. username )
-- We need to recalculate them here before we can create the highest amputations point
CachedDataHandler.CalculateAmputatedLimbs(username)
@@ -217,35 +231,26 @@ end
local og_ISHealthPanel_getDamagedParts = ISHealthPanel.getDamagedParts
function ISHealthPanel:getDamagedParts()
-- TODO Overriding it is a lot easier, but ew
-- local result = og_ISHealthPanel_getDamagedParts(self)
-- local bodyParts = self:getPatient():getBodyDamage():getBodyParts()
-- if isClient() and not self:getPatient():isLocalPlayer() then
-- bodyParts = self:getPatient():getBodyDamageRemote():getBodyParts()
-- end
-- for i=1,bodyParts:size() do
-- local bodyPart = bodyParts:get(i-1)
-- table.insert(result, bodyPart)
-- end
-- return result
local result = {}
local bodyParts = self:getPatient():getBodyDamage():getBodyParts()
if isClient() and not self:getPatient():isLocalPlayer() then
bodyParts = self:getPatient():getBodyDamageRemote():getBodyParts()
end
local patientUsername = self:getPatient():getUsername()
local mdh = ModDataHandler.GetInstance(patientUsername)
for i=1,bodyParts:size() do
local bodyPart = bodyParts:get(i-1)
local bodyPartTypeStr = BodyPartType.ToString(bodyPart:getType())
local limbName = StaticData.LIMBS_IND_STR[bodyPartTypeStr]
local bodyPartAction = self.bodyPartAction and self.bodyPartAction[bodyPart]
if ISHealthPanel.cheat or bodyPart:HasInjury() or bodyPart:bandaged() or bodyPart:stitched() or bodyPart:getSplintFactor() > 0 or bodyPart:getAdditionalPain() > 10 or bodyPart:getStiffness() > 5 or ModDataHandler.GetInstance(self:getPatient():getUsername()):getIsCut(limbName) then
if ISHealthPanel.cheat or bodyPart:HasInjury() or bodyPart:bandaged() or bodyPart:stitched() or bodyPart:getSplintFactor() > 0 or bodyPart:getAdditionalPain() > 10 or bodyPart:getStiffness() > 5 or (mdh:getIsCut(limbName) and mdh:getIsVisible(limbName)) then
table.insert(result, bodyPart)
end
end
return result
end
end

View File

@@ -20,7 +20,7 @@ function ServerDataHandler.AddTable(key, table)
-- Check if key is valid
if not luautils.stringStarts(key, StaticData.MOD_NAME .. "_") then return end
TOC_DEBUG.print("received TOC ModData: " .. tostring(key))
TOC_DEBUG.print("[ServerDataHandler] Received TOC ModData: " .. tostring(key))
ModData.add(key, table) -- Add it to the server mod data
ServerDataHandler.modData[key] = table
end