From 27e9a2b22b47964fb7ef62da8e60ee0fe5bff460 Mon Sep 17 00:00:00 2001 From: Jarod42 Date: Mon, 13 May 2024 18:56:15 +0200 Subject: [PATCH] Replace `memcpy` by `ranges::copy`. So fix copy of `ResInfo` in `CclCopyUnitType` --- src/ui/botpanel.cpp | 15 ++++++--------- src/unit/script_unittype.cpp | 14 ++++++++------ 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/ui/botpanel.cpp b/src/ui/botpanel.cpp index bbf30c67eb..a2ac818a8a 100644 --- a/src/ui/botpanel.cpp +++ b/src/ui/botpanel.cpp @@ -528,24 +528,20 @@ void DrawPopup(const ButtonAction &button, const CUIButton &uibutton, int x, int LastDrawnButtonPopup = const_cast(&button); } - int popupWidth, popupHeight; - int Costs[ManaResCost + 1]; - memset(Costs, 0, sizeof(Costs)); - + int Costs[ManaResCost + 1]{}; switch (button.Action) { case ButtonCmd::Research: - memcpy(Costs, AllUpgrades[button.Value]->Costs, sizeof(AllUpgrades[button.Value]->Costs)); + ranges::copy(AllUpgrades[button.Value]->Costs, std::begin(Costs)); break; case ButtonCmd::SpellCast: - memcpy(Costs, SpellTypeTable[button.Value]->Costs, sizeof(SpellTypeTable[button.Value]->Costs)); + ranges::copy(SpellTypeTable[button.Value]->Costs, std::begin(Costs)); Costs[ManaResCost] = SpellTypeTable[button.Value]->ManaCost; break; case ButtonCmd::Build: case ButtonCmd::Train: case ButtonCmd::UpgradeTo: - memcpy(Costs, - getUnitTypes()[button.Value]->Stats[ThisPlayer->Index].Costs, - sizeof(getUnitTypes()[button.Value]->Stats[ThisPlayer->Index].Costs)); + ranges::copy(getUnitTypes()[button.Value]->Stats[ThisPlayer->Index].Costs, + std::begin(Costs)); Costs[FoodCost] = getUnitTypes()[button.Value] ->Stats[ThisPlayer->Index] .Variables[DEMAND_INDEX] @@ -555,6 +551,7 @@ void DrawPopup(const ButtonAction &button, const CUIButton &uibutton, int x, int break; } + int popupWidth, popupHeight; if (useCache) { popupWidth = popupCache.popupWidth; popupHeight = popupCache.popupHeight; diff --git a/src/unit/script_unittype.cpp b/src/unit/script_unittype.cpp index 0b447dbe01..a77c42c75e 100644 --- a/src/unit/script_unittype.cpp +++ b/src/unit/script_unittype.cpp @@ -1239,9 +1239,9 @@ static int CclCopyUnitType(lua_State *l) to->Portrait.Files = from.Portrait.Files; to->Portrait.Mngs.resize(to->Portrait.Files.size()); #endif - memcpy(to->DefaultStat.Costs, from.DefaultStat.Costs, sizeof(from.DefaultStat.Costs)); - memcpy(to->DefaultStat.Storing, from.DefaultStat.Storing, sizeof(from.DefaultStat.Storing)); - memcpy(to->DefaultStat.ImproveIncomes, from.DefaultStat.ImproveIncomes, sizeof(from.DefaultStat.ImproveIncomes)); + ranges::copy(from.DefaultStat.Costs, std::begin(to->DefaultStat.Costs)); + ranges::copy(from.DefaultStat.Storing, std::begin(to->DefaultStat.Storing)); + ranges::copy(from.DefaultStat.ImproveIncomes, std::begin(to->DefaultStat.ImproveIncomes)); to->Construction = from.Construction; to->DrawLevel = from.DrawLevel; to->MaxOnBoard = from.MaxOnBoard; @@ -1310,7 +1310,7 @@ static int CclCopyUnitType(lua_State *l) to->CanAttack = from.CanAttack; to->RepairRange = from.RepairRange; to->RepairHP = from.RepairHP; - memcpy(to->RepairCosts, from.RepairCosts, sizeof(from.RepairCosts)); + ranges::copy(from.RepairCosts, std::begin(to->RepairCosts)); to->CanTarget = from.CanTarget; to->Building = from.Building; to->BuildingRules.clear(); @@ -1343,9 +1343,11 @@ static int CclCopyUnitType(lua_State *l) to->BoolFlag[i].CanTargetFlag = from.BoolFlag[i].CanTargetFlag; to->BoolFlag[i].AiPriorityTarget = from.BoolFlag[i].AiPriorityTarget; } - memcpy(to->ResInfo, from.ResInfo, sizeof(from.ResInfo)); + for (std::size_t i = 0; i != std::size(to->ResInfo); ++i) { + from.ResInfo[i] = to->ResInfo[i] ? std::make_unique(*to->ResInfo[i]) : nullptr; + } to->GivesResource = from.GivesResource; - memcpy(to->CanStore, from.CanStore, sizeof(from.CanStore)); + ranges::copy(from.CanStore, std::begin(to->CanStore)); to->CanCastSpell = from.CanCastSpell; to->AutoCastActive = from.AutoCastActive; to->Sound.Selected.Name = from.Sound.Selected.Name;