From 120213509d10790a27043255481af11a74f9720d Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Sun, 5 Oct 2025 02:46:19 +0200 Subject: [PATCH] netpacks for renaming --- client/netlag/PackRollbackGeneratorVisitor.h | 1 + client/widgets/CTextInput.cpp | 3 +++ client/windows/CCastleInterface.cpp | 5 ++++- lib/callback/CCallback.cpp | 6 ++++++ lib/callback/CCallback.h | 1 + lib/callback/IGameActionCallback.h | 1 + lib/gameState/GameStatePackVisitor.cpp | 5 +++++ lib/gameState/GameStatePackVisitor.h | 1 + lib/networkPacks/NetPackVisitor.h | 2 ++ lib/networkPacks/NetPacksLib.cpp | 10 +++++++++ lib/networkPacks/PacksForClient.h | 14 +++++++++++++ lib/networkPacks/PacksForServer.h | 22 ++++++++++++++++++++ lib/serializer/RegisterTypes.h | 2 ++ server/CGameHandler.cpp | 17 +++++++++++++++ server/CGameHandler.h | 1 + server/NetPacksServer.cpp | 8 +++++++ server/ServerNetPackVisitors.h | 1 + 17 files changed, 99 insertions(+), 1 deletion(-) diff --git a/client/netlag/PackRollbackGeneratorVisitor.h b/client/netlag/PackRollbackGeneratorVisitor.h index affb415f2..a26877591 100644 --- a/client/netlag/PackRollbackGeneratorVisitor.h +++ b/client/netlag/PackRollbackGeneratorVisitor.h @@ -85,6 +85,7 @@ private: //void visitSetCommanderProperty(SetCommanderProperty & pack) override; //void visitAddQuest(AddQuest & pack) override; //void visitChangeFormation(ChangeFormation & pack) override; + //void visitChangeTownName(ChangeTownName & pack) override; //void visitChangeSpells(ChangeSpells & pack) override; //void visitSetAvailableHero(SetAvailableHero & pack) override; //void visitChangeObjectVisitors(ChangeObjectVisitors & pack) override; diff --git a/client/widgets/CTextInput.cpp b/client/widgets/CTextInput.cpp index e9bac0b06..9ad7c8798 100644 --- a/client/widgets/CTextInput.cpp +++ b/client/widgets/CTextInput.cpp @@ -91,6 +91,9 @@ void CTextInputWithConfirm::textInputted(const std::string & enteredText) void CTextInputWithConfirm::confirm() { + if(getText().empty()) + setText(initialText); + if(confirmCb && initialText != getText()) confirmCb(); removeFocus(); diff --git a/client/windows/CCastleInterface.cpp b/client/windows/CCastleInterface.cpp index 07a623d40..6308604ee 100644 --- a/client/windows/CCastleInterface.cpp +++ b/client/windows/CCastleInterface.cpp @@ -1436,7 +1436,10 @@ CCastleInterface::CCastleInterface(const CGTownInstance * Town, const CGTownInst garr->setRedrawParent(true); heroes = std::make_shared(town, Point(241, 387), Point(241, 483), garr, true); - title = std::make_shared(Rect(83, 386, 140, 20), FONT_MEDIUM, ETextAlignment::TOPLEFT, town->getNameTranslated(), true, [this](){ std::cout << title->getText(); }); + title = std::make_shared(Rect(83, 386, 140, 20), FONT_MEDIUM, ETextAlignment::TOPLEFT, town->getNameTranslated(), true, [this](){ + std::string name = title->getText(); + GAME->interface()->cb->setTownName(town, name); + }); income = std::make_shared(195, 443, FONT_SMALL, ETextAlignment::CENTER); icon = std::make_shared(AnimationPath::builtin("ITPT"), 0, 0, 15, 387); diff --git a/lib/callback/CCallback.cpp b/lib/callback/CCallback.cpp index a826d2950..f94c8cdf1 100644 --- a/lib/callback/CCallback.cpp +++ b/lib/callback/CCallback.cpp @@ -286,6 +286,12 @@ void CCallback::setFormation(const CGHeroInstance * hero, EArmyFormation mode) sendRequest(pack); } +void CCallback::setTownName(const CGTownInstance * town, std::string & name) +{ + SetTownName pack(town->id, name); + sendRequest(pack); +} + void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero, const HeroTypeID & nextHero) { assert(townOrTavern); diff --git a/lib/callback/CCallback.h b/lib/callback/CCallback.h index cf8484bb9..7eb6a42c0 100644 --- a/lib/callback/CCallback.h +++ b/lib/callback/CCallback.h @@ -76,6 +76,7 @@ public: void trade(const ObjectInstanceID marketId, EMarketMode mode, TradeItemSell id1, TradeItemBuy id2, ui32 val1, const CGHeroInstance * hero = nullptr) override; void trade(const ObjectInstanceID marketId, EMarketMode mode, const std::vector & id1, const std::vector & id2, const std::vector & val1, const CGHeroInstance * hero = nullptr) override; void setFormation(const CGHeroInstance * hero, EArmyFormation mode) override; + void setTownName(const CGTownInstance * town, std::string & name) override; void recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero, const HeroTypeID & nextHero=HeroTypeID::NONE) override; void save(const std::string &fname) override; void sendMessage(const std::string &mess, const CGObjectInstance * currentObject = nullptr) override; diff --git a/lib/callback/IGameActionCallback.h b/lib/callback/IGameActionCallback.h index 946eee67d..8f5a9bcd9 100644 --- a/lib/callback/IGameActionCallback.h +++ b/lib/callback/IGameActionCallback.h @@ -70,6 +70,7 @@ public: virtual void endTurn()=0; virtual void buyArtifact(const CGHeroInstance *hero, ArtifactID aid)=0; //used to buy artifacts in towns (including spell book in the guild and war machines in blacksmith) virtual void setFormation(const CGHeroInstance * hero, EArmyFormation mode)=0; + virtual void setTownName(const CGTownInstance * town, std::string & name)=0; virtual void save(const std::string &fname) = 0; virtual void sendMessage(const std::string &mess, const CGObjectInstance * currentObject = nullptr) = 0; diff --git a/lib/gameState/GameStatePackVisitor.cpp b/lib/gameState/GameStatePackVisitor.cpp index d527d7555..4fc72af4c 100644 --- a/lib/gameState/GameStatePackVisitor.cpp +++ b/lib/gameState/GameStatePackVisitor.cpp @@ -123,6 +123,11 @@ void GameStatePackVisitor::visitChangeFormation(ChangeFormation & pack) gs.getHero(pack.hid)->setFormation(pack.formation); } +void GameStatePackVisitor::visitChangeTownName(ChangeTownName & pack) +{ + gs.getTown(pack.tid)->setCustomName(pack.name); +} + void GameStatePackVisitor::visitHeroVisitCastle(HeroVisitCastle & pack) { CGHeroInstance *h = gs.getHero(pack.hid); diff --git a/lib/gameState/GameStatePackVisitor.h b/lib/gameState/GameStatePackVisitor.h index fa4421e4e..8cb0642a7 100644 --- a/lib/gameState/GameStatePackVisitor.h +++ b/lib/gameState/GameStatePackVisitor.h @@ -87,6 +87,7 @@ public: void visitSetCommanderProperty(SetCommanderProperty & pack) override; void visitAddQuest(AddQuest & pack) override; void visitChangeFormation(ChangeFormation & pack) override; + void visitChangeTownName(ChangeTownName & pack) override; void visitChangeSpells(ChangeSpells & pack) override; void visitSetAvailableHero(SetAvailableHero & pack) override; void visitChangeObjectVisitors(ChangeObjectVisitors & pack) override; diff --git a/lib/networkPacks/NetPackVisitor.h b/lib/networkPacks/NetPackVisitor.h index eef61f439..d1221172f 100644 --- a/lib/networkPacks/NetPackVisitor.h +++ b/lib/networkPacks/NetPackVisitor.h @@ -58,6 +58,7 @@ public: virtual void visitSetCommanderProperty(SetCommanderProperty & pack) {} virtual void visitAddQuest(AddQuest & pack) {} virtual void visitChangeFormation(ChangeFormation & pack) {} + virtual void visitChangeTownName(ChangeTownName & pack) {} virtual void visitRemoveObject(RemoveObject & pack) {} virtual void visitTryMoveHero(TryMoveHero & pack) {} virtual void visitNewStructures(NewStructures & pack) {} @@ -144,6 +145,7 @@ public: virtual void visitBuyArtifact(BuyArtifact & pack) {} virtual void visitTradeOnMarketplace(TradeOnMarketplace & pack) {} virtual void visitSetFormation(SetFormation & pack) {} + virtual void visitSetTownName(SetTownName & pack) {} virtual void visitHireHero(HireHero & pack) {} virtual void visitBuildBoat(BuildBoat & pack) {} virtual void visitQueryReply(QueryReply & pack) {} diff --git a/lib/networkPacks/NetPacksLib.cpp b/lib/networkPacks/NetPacksLib.cpp index 5d7c08477..c7df14a35 100644 --- a/lib/networkPacks/NetPacksLib.cpp +++ b/lib/networkPacks/NetPacksLib.cpp @@ -213,6 +213,11 @@ void ChangeFormation::visitTyped(ICPackVisitor & visitor) visitor.visitChangeFormation(*this); } +void ChangeTownName::visitTyped(ICPackVisitor & visitor) +{ + visitor.visitChangeTownName(*this); +} + void RemoveObject::visitTyped(ICPackVisitor & visitor) { visitor.visitRemoveObject(*this); @@ -643,6 +648,11 @@ void SetFormation::visitTyped(ICPackVisitor & visitor) visitor.visitSetFormation(*this); } +void SetTownName::visitTyped(ICPackVisitor & visitor) +{ + visitor.visitSetTownName(*this); +} + void HireHero::visitTyped(ICPackVisitor & visitor) { visitor.visitHireHero(*this); diff --git a/lib/networkPacks/PacksForClient.h b/lib/networkPacks/PacksForClient.h index 0a6bf1051..cd7608954 100644 --- a/lib/networkPacks/PacksForClient.h +++ b/lib/networkPacks/PacksForClient.h @@ -614,6 +614,20 @@ struct DLL_LINKAGE ChangeFormation : public CPackForClient } }; +struct DLL_LINKAGE ChangeTownName : public CPackForClient +{ + ObjectInstanceID tid; + std::string name; + + void visitTyped(ICPackVisitor & visitor) override; + + template void serialize(Handler & h) + { + h & tid; + h & name; + } +}; + struct DLL_LINKAGE RemoveObject : public CPackForClient { RemoveObject() = default; diff --git a/lib/networkPacks/PacksForServer.h b/lib/networkPacks/PacksForServer.h index 9924d3b4b..f28d64c5f 100644 --- a/lib/networkPacks/PacksForServer.h +++ b/lib/networkPacks/PacksForServer.h @@ -609,6 +609,28 @@ struct DLL_LINKAGE SetFormation : public CPackForServer } }; +struct DLL_LINKAGE SetTownName : public CPackForServer +{ + SetTownName() = default; + ; + SetTownName(const ObjectInstanceID & TID, std::string Name) + : tid(TID) + , name(Name) + { + } + ObjectInstanceID tid; + std::string name; + + void visitTyped(ICPackVisitor & visitor) override; + + template void serialize(Handler & h) + { + h & static_cast(*this); + h & tid; + h & name; + } +}; + struct DLL_LINKAGE HireHero : public CPackForServer { HireHero() = default; diff --git a/lib/serializer/RegisterTypes.h b/lib/serializer/RegisterTypes.h index 22a4c7508..9116a902d 100644 --- a/lib/serializer/RegisterTypes.h +++ b/lib/serializer/RegisterTypes.h @@ -291,6 +291,8 @@ void registerTypes(Serializer &s) s.template registerType(249); s.template registerType(250); s.template registerType(251); + s.template registerType(252); + s.template registerType(253); } VCMI_LIB_NAMESPACE_END diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 722c601d6..d78ace0cf 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -3219,6 +3219,23 @@ bool CGameHandler::setFormation(ObjectInstanceID hid, EArmyFormation formation) return true; } +bool CGameHandler::setTownName(ObjectInstanceID tid, std::string & name) +{ + const CGTownInstance *t = gameInfo().getTown(tid); + if (!t) + { + logGlobal->error("Town doesn't exist!"); + return false; + } + + ChangeTownName ctn; + ctn.tid = tid; + ctn.name = name; + sendAndApply(ctn); + + return true; +} + bool CGameHandler::queryReply(QueryID qid, std::optional answer, PlayerColor player) { logGlobal->trace("Player %s attempts answering query %d with answer:", player, qid); diff --git a/server/CGameHandler.h b/server/CGameHandler.h index f1661b294..e0e7c791e 100644 --- a/server/CGameHandler.h +++ b/server/CGameHandler.h @@ -212,6 +212,7 @@ public: bool queryReply( QueryID qid, std::optional reply, PlayerColor player ); bool buildBoat( ObjectInstanceID objid, PlayerColor player ); bool setFormation( ObjectInstanceID hid, EArmyFormation formation ); + bool setTownName( ObjectInstanceID tid, std::string & name ); bool tradeResources(const IMarket *market, ui32 amountToSell, PlayerColor player, GameResID toSell, GameResID toBuy); bool sacrificeCreatures(const IMarket * market, const CGHeroInstance * hero, const std::vector & slot, const std::vector & count); bool sendResources(ui32 val, PlayerColor player, GameResID r1, PlayerColor r2); diff --git a/server/NetPacksServer.cpp b/server/NetPacksServer.cpp index 47b9c52f0..f02b9975a 100644 --- a/server/NetPacksServer.cpp +++ b/server/NetPacksServer.cpp @@ -362,6 +362,14 @@ void ApplyGhNetPackVisitor::visitSetFormation(SetFormation & pack) result = gh.setFormation(pack.hid, pack.formation); } +void ApplyGhNetPackVisitor::visitSetTownName(SetTownName & pack) +{ + gh.throwIfWrongOwner(connection, &pack, pack.tid); + gh.throwIfPlayerNotActive(connection, &pack); + + result = gh.setTownName(pack.tid, pack.name); +} + void ApplyGhNetPackVisitor::visitHireHero(HireHero & pack) { gh.throwIfWrongPlayer(connection, &pack); diff --git a/server/ServerNetPackVisitors.h b/server/ServerNetPackVisitors.h index a6ffffb63..3b971fa7d 100644 --- a/server/ServerNetPackVisitors.h +++ b/server/ServerNetPackVisitors.h @@ -58,6 +58,7 @@ public: void visitBuyArtifact(BuyArtifact & pack) override; void visitTradeOnMarketplace(TradeOnMarketplace & pack) override; void visitSetFormation(SetFormation & pack) override; + void visitSetTownName(SetTownName & pack) override; void visitHireHero(HireHero & pack) override; void visitBuildBoat(BuildBoat & pack) override; void visitQueryReply(QueryReply & pack) override;