diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5e0ba41b83..e57a9b412a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -222,6 +222,7 @@ add_files( language.h league_base.h league_cmd.cpp + league_cmd.h league_gui.h league_gui.cpp league_type.h diff --git a/src/command.cpp b/src/command.cpp index c9e744af3b..8294e8d8f8 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -259,7 +259,7 @@ CommandProc CmdSetTimetableStart; CommandProc CmdOpenCloseAirport; -CommandProc CmdCreateLeagueTable; +CommandProcEx CmdCreateLeagueTable; CommandProcEx CmdCreateLeagueTableElement; CommandProc CmdUpdateLeagueTableElementData; CommandProcEx CmdUpdateLeagueTableElementScore; @@ -510,8 +510,8 @@ static const Command _command_proc_table[] = { DEF_CMD(CmdOpenCloseAirport, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_OPEN_CLOSE_AIRPORT - DEF_CMD(CmdCreateLeagueTable, CMD_STR_SEP | CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CREATE_LEAGUE_TABLE - DEF_CMD(CmdCreateLeagueTableElement, CMD_STR_SEP | CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CREATE_LEAGUE_TABLE_ELEMENT + DEF_CMD(CmdCreateLeagueTable, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CREATE_LEAGUE_TABLE + DEF_CMD(CmdCreateLeagueTableElement, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CREATE_LEAGUE_TABLE_ELEMENT DEF_CMD(CmdUpdateLeagueTableElementData, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_UPDATE_LEAGUE_TABLE_ELEMENT_DATA DEF_CMD(CmdUpdateLeagueTableElementScore, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_UPDATE_LEAGUE_TABLE_ELEMENT_SCORE DEF_CMD(CmdRemoveLeagueTableElement, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_REMOVE_LEAGUE_TABLE_ELEMENT diff --git a/src/league_cmd.cpp b/src/league_cmd.cpp index e74a1ac8de..2b3b3ff038 100644 --- a/src/league_cmd.cpp +++ b/src/league_cmd.cpp @@ -9,6 +9,7 @@ #include "stdafx.h" #include "league_base.h" +#include "league_cmd.h" #include "command_type.h" #include "command_func.h" #include "industry.h" @@ -175,28 +176,22 @@ CommandCost CmdRemoveLeagueTableElement(DoCommandFlag flags, LeagueTableElementI return CommandCost(); } -CommandCost CmdCreateLeagueTable(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) +CommandCost CmdCreateLeagueTable(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, uint64 p3, const char *text, const CommandAuxiliaryBase *aux_data) { - std::string title, header, footer; - text = StrConsumeToSeparator(title, text); - if (text == nullptr) return CMD_ERROR; - text = StrConsumeToSeparator(header, text); - if (text == nullptr) return CMD_ERROR; - text = StrConsumeToSeparator(footer, text); - if (text != nullptr) return CMD_ERROR; + CommandAuxData data; + CommandCost ret = data.Load(aux_data); + if (ret.Failed()) return ret; - auto [res, id] = CmdCreateLeagueTable(flags, title, header, footer); + auto [res, id] = CmdCreateLeagueTable(flags, data->title, data->header, data->footer); res.SetResultData(id); return res; } CommandCost CmdCreateLeagueTableElement(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, uint64 p3, const char *text, const CommandAuxiliaryBase *aux_data) { - std::string text_str, score; - text = StrConsumeToSeparator(text_str, text); - if (text == nullptr) return CMD_ERROR; - text = StrConsumeToSeparator(score, text); - if (text != nullptr) return CMD_ERROR; + CommandAuxData data; + CommandCost ret = data.Load(aux_data); + if (ret.Failed()) return ret; LeagueTableID table = GB(p1, 0, 8); int64 rating = p3; @@ -204,21 +199,13 @@ CommandCost CmdCreateLeagueTableElement(TileIndex tile, DoCommandFlag flags, uin LinkType link_type = (LinkType)GB(p1, 16, 8); LinkTargetID link_target = (LinkTargetID)p2; - auto [res, id] = CmdCreateLeagueTableElement(flags, table, rating, company, text_str, score, link_type, link_target); + auto [res, id] = CmdCreateLeagueTableElement(flags, table, rating, company, data->text_str, data->score, link_type, link_target); res.SetResultData(id); return res; } CommandCost CmdUpdateLeagueTableElementData(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - std::string title, header, footer; - text = StrConsumeToSeparator(title, text); - if (text == nullptr) return CMD_ERROR; - text = StrConsumeToSeparator(header, text); - if (text == nullptr) return CMD_ERROR; - text = StrConsumeToSeparator(footer, text); - if (text != nullptr) return CMD_ERROR; - LeagueTableElementID element = GB(p1, 0, 16); CompanyID company = (CompanyID)GB(p1, 16, 8); LinkType link_type = (LinkType)GB(p1, 24, 8); diff --git a/src/league_cmd.h b/src/league_cmd.h new file mode 100644 index 0000000000..f2905208b4 --- /dev/null +++ b/src/league_cmd.h @@ -0,0 +1,54 @@ +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file league_cmd.h Command definitions related to league tables. */ + +#ifndef LEAGUE_CMD_H +#define LEAGUE_CMD_H + +#include "command_aux.h" + +struct LeagueTableCmdData : public CommandAuxiliarySerialisable { + std::string title; + std::string header; + std::string footer; + + virtual void Serialise(CommandSerialisationBuffer &buffer) const override + { + buffer.Send_string(this->title); + buffer.Send_string(this->header); + buffer.Send_string(this->footer); + } + + CommandCost Deserialise(CommandDeserialisationBuffer &buffer) + { + buffer.Recv_string(this->title, SVS_ALLOW_CONTROL_CODE | SVS_REPLACE_WITH_QUESTION_MARK); + buffer.Recv_string(this->header, SVS_ALLOW_CONTROL_CODE | SVS_REPLACE_WITH_QUESTION_MARK); + buffer.Recv_string(this->footer, SVS_ALLOW_CONTROL_CODE | SVS_REPLACE_WITH_QUESTION_MARK); + return CommandCost(); + } +}; + +struct LeagueTableElementCmdData : public CommandAuxiliarySerialisable { + std::string text_str; + std::string score; + + virtual void Serialise(CommandSerialisationBuffer &buffer) const override + { + buffer.Send_string(this->text_str); + buffer.Send_string(this->score); + } + + CommandCost Deserialise(CommandDeserialisationBuffer &buffer) + { + buffer.Recv_string(this->text_str, SVS_ALLOW_CONTROL_CODE | SVS_REPLACE_WITH_QUESTION_MARK); + buffer.Recv_string(this->score, SVS_ALLOW_CONTROL_CODE | SVS_REPLACE_WITH_QUESTION_MARK); + return CommandCost(); + } +}; + +#endif /* LEAGUE_CMD_H */ diff --git a/src/script/api/script_league.cpp b/src/script/api/script_league.cpp index fd3ac7233e..9b53f792b9 100644 --- a/src/script/api/script_league.cpp +++ b/src/script/api/script_league.cpp @@ -14,6 +14,7 @@ #include "../script_instance.hpp" #include "script_error.hpp" #include "../../league_base.h" +#include "../../league_cmd.h" #include "../../string_func.h" #include "../../safeguards.h" @@ -35,13 +36,12 @@ const char *encoded_title = title->GetEncodedText(); EnforcePreconditionEncodedText(LEAGUE_TABLE_INVALID, encoded_title); - std::string cmd_text = encoded_title; - cmd_text.push_back(0x1F); - if (header != nullptr) cmd_text += header->GetEncodedText(); - cmd_text.push_back(0x1F); - if (footer != nullptr) cmd_text += footer->GetEncodedText(); + LeagueTableCmdData data; + data.title = encoded_title; + data.header = header->GetEncodedText(); + data.footer = footer->GetEncodedText(); - if (!ScriptObject::DoCommand(0, 0, 0, CMD_CREATE_LEAGUE_TABLE, cmd_text.c_str(), &ScriptInstance::DoCommandReturnLeagueTableID)) return LEAGUE_TABLE_INVALID; + if (!ScriptObject::DoCommandEx(0, 0, 0, 0, CMD_CREATE_LEAGUE_TABLE, nullptr, &data, &ScriptInstance::DoCommandReturnLeagueTableID)) return LEAGUE_TABLE_INVALID; /* In case of test-mode, we return LeagueTableID 0 */ return (ScriptLeagueTable::LeagueTableID)0; @@ -76,10 +76,11 @@ EnforcePrecondition(LEAGUE_TABLE_ELEMENT_INVALID, IsValidLink(Link((::LinkType)link_type, link_target))); - std::string cmd_text = std::move(encoded_text); - cmd_text.push_back(0x1F); - cmd_text += encoded_score; - if (!ScriptObject::DoCommandEx(0, table | (c << 8) | (link_type << 16), link_target, rating, CMD_CREATE_LEAGUE_TABLE_ELEMENT, cmd_text.c_str(), 0, &ScriptInstance::DoCommandReturnLeagueTableElementID)) return LEAGUE_TABLE_ELEMENT_INVALID; + LeagueTableElementCmdData data; + data.text_str = std::move(encoded_text); + data.score = encoded_score; + + if (!ScriptObject::DoCommandEx(0, table | (c << 8) | (link_type << 16), link_target, rating, CMD_CREATE_LEAGUE_TABLE_ELEMENT, nullptr, &data, &ScriptInstance::DoCommandReturnLeagueTableElementID)) return LEAGUE_TABLE_ELEMENT_INVALID; /* In case of test-mode, we return LeagueTableElementID 0 */ return (ScriptLeagueTable::LeagueTableElementID)0;