From b8867e13788a54f8eba5df7758c8a141eee6ace7 Mon Sep 17 00:00:00 2001 From: Reinder Feenstra Date: Mon, 3 Jan 2022 00:05:29 +0100 Subject: [PATCH] world loader: added loading of legacy command stations and controllers --- server/src/utils/endswith.hpp | 33 +++++++++++++++++++ server/src/utils/stripsuffix.hpp | 37 +++++++++++++++++++++ server/src/world/worldloader.cpp | 56 ++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 server/src/utils/endswith.hpp create mode 100644 server/src/utils/stripsuffix.hpp diff --git a/server/src/utils/endswith.hpp b/server/src/utils/endswith.hpp new file mode 100644 index 00000000..fdd57072 --- /dev/null +++ b/server/src/utils/endswith.hpp @@ -0,0 +1,33 @@ +/** + * server/src/utils/endswith.hpp + * + * This file is part of the traintastic source code. + * + * Copyright (C) 2022 Reinder Feenstra + * + * This program 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; either version 2 + * of the License, or (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef TRAINTASTIC_SERVER_UTILS_ENDSWITH_HPP +#define TRAINTASTIC_SERVER_UTILS_ENDSWITH_HPP + +#include + +constexpr bool endsWith(std::string_view sv, std::string_view suffix) +{ + return sv.size() >= suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), sv.rbegin()); +} + +#endif diff --git a/server/src/utils/stripsuffix.hpp b/server/src/utils/stripsuffix.hpp new file mode 100644 index 00000000..c3bd607d --- /dev/null +++ b/server/src/utils/stripsuffix.hpp @@ -0,0 +1,37 @@ +/** + * server/src/utils/stripsuffix.hpp + * + * This file is part of the traintastic source code. + * + * Copyright (C) 2022 Reinder Feenstra + * + * This program 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; either version 2 + * of the License, or (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef TRAINTASTIC_SERVER_UTILS_STRIPSUFFIX_HPP +#define TRAINTASTIC_SERVER_UTILS_STRIPSUFFIX_HPP + +#include +#include +#include "endswith.hpp" + +constexpr std::string_view stripSuffix(std::string_view sv, std::string_view suffix) +{ + if(endsWith(sv, suffix)) + sv.remove_suffix(suffix.size()); + return sv; +} + +#endif diff --git a/server/src/world/worldloader.cpp b/server/src/world/worldloader.cpp index 895186d5..51e594ba 100644 --- a/server/src/world/worldloader.cpp +++ b/server/src/world/worldloader.cpp @@ -27,6 +27,7 @@ #include #include "world.hpp" #include "../utils/startswith.hpp" +#include "../utils/stripsuffix.hpp" #include "ctwreader.hpp" #include "../board/board.hpp" @@ -199,6 +200,61 @@ void WorldLoader::createObject(ObjectData& objectData) else if(classId == Lua::Script::classId) objectData.object = Lua::Script::create(m_world, id); #endif + // backwards compatibility < 0.1 + else if(classId == "command_station.dccplusplus_serial") + { + objectData.object = DCCPlusPlusInterface::create(m_world, id); + objectData.json["device"] = objectData.json["port"]; + objectData.json.erase("port"); + } + else if(classId == "command_station.loconet_serial") + { + auto object = LocoNetInterface::create(m_world, id); + object->type = LocoNetInterfaceType::Serial; + objectData.object = object; + objectData.json["device"] = objectData.json["port"]; + objectData.json.erase("port"); + } + else if(classId == "command_station.loconet_tcp_binary") + { + auto object = LocoNetInterface::create(m_world, id); + object->type = LocoNetInterfaceType::TCPBinary; + objectData.object = object; + } + else if(classId == "command_station.xpressnet_serial") + { + auto object = XpressNetInterface::create(m_world, id); + object->type = XpressNetInterfaceType::Serial; + objectData.object = object; + objectData.json["device"] = objectData.json["port"]; + objectData.json.erase("port"); + } + else if(classId == "command_station.z21") + { + objectData.object = Z21Interface::create(m_world, id); + } + else if(classId == "controller.wlanmaus") + { + objectData.object = WlanMausInterface::create(m_world, id); + } + else if(classId == "input.loconet") + { + objectData.object = Input::create(m_world, id); + objectData.json["interface"] = stripSuffix(objectData.json["loconet"], ".loconet"); + objectData.json.erase("loconet"); + } + else if(classId == "output.loconet") + { + objectData.object = Output::create(m_world, id); + objectData.json["interface"] = stripSuffix(objectData.json["loconet"], ".loconet"); + objectData.json.erase("loconet"); + } + else if(classId == "input.xpressnet") + { + objectData.object = Input::create(m_world, id); + objectData.json["interface"] = stripSuffix(objectData.json["xpressnet"], ".xpressnet"); + objectData.json.erase("xpressnet"); + } else assert(false);