From 9124653156454fe6fd318095e3aac24241a1f175 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sat, 30 Jan 2021 21:11:25 -0700 Subject: [PATCH] common: move EntData_ function to common --- CMakeLists.txt | 1 + bspinfo/CMakeLists.txt | 1 + bsputil/CMakeLists.txt | 1 + common/entdata.cc | 150 ++++++++++++++++++++++++++++++++++++++ include/common/entdata.h | 31 ++++++++ include/light/entities.hh | 8 +- light/CMakeLists.txt | 3 +- light/entities.cc | 121 ------------------------------ vis/CMakeLists.txt | 1 + 9 files changed, 188 insertions(+), 129 deletions(-) create mode 100644 common/entdata.cc create mode 100644 include/common/entdata.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 83f75c6a..c19f9937 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ include_directories( set(COMMON_INCLUDES ${CMAKE_SOURCE_DIR}/include/common/aabb.hh + ${CMAKE_SOURCE_DIR}/include/common/entdata.h ${CMAKE_SOURCE_DIR}/include/common/mesh.hh ${CMAKE_SOURCE_DIR}/include/common/octree.hh ${CMAKE_SOURCE_DIR}/include/common/qvec.hh diff --git a/bspinfo/CMakeLists.txt b/bspinfo/CMakeLists.txt index 75838b79..448bc23d 100644 --- a/bspinfo/CMakeLists.txt +++ b/bspinfo/CMakeLists.txt @@ -3,6 +3,7 @@ project (bspinfo CXX) set(BSPINFO_SOURCES bspinfo.cc + ${CMAKE_SOURCE_DIR}/common/entdata.cc ${CMAKE_SOURCE_DIR}/common/cmdlib.cc ${CMAKE_SOURCE_DIR}/common/bspfile.cc ${CMAKE_SOURCE_DIR}/common/log.cc diff --git a/bsputil/CMakeLists.txt b/bsputil/CMakeLists.txt index d37ab304..c62aea70 100644 --- a/bsputil/CMakeLists.txt +++ b/bsputil/CMakeLists.txt @@ -8,6 +8,7 @@ set(BSPUTIL_SOURCES ${CMAKE_SOURCE_DIR}/common/cmdlib.cc ${CMAKE_SOURCE_DIR}/common/bspfile.cc ${CMAKE_SOURCE_DIR}/common/bsputils.cc + ${CMAKE_SOURCE_DIR}/common/entdata.cc ${CMAKE_SOURCE_DIR}/common/mathlib.cc ${CMAKE_SOURCE_DIR}/common/qvec.cc ${CMAKE_SOURCE_DIR}/common/polylib.cc diff --git a/common/entdata.cc b/common/entdata.cc new file mode 100644 index 00000000..abf2f517 --- /dev/null +++ b/common/entdata.cc @@ -0,0 +1,150 @@ +/* Copyright (C) 1996-1997 Id Software, Inc. + + 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + See file, 'COPYING', for details. +*/ + +#include + +#include +#include +#include + +#include +#include +#include +#include + +/* + * ================== + * EntData_Parse + * ================== + */ +std::vector +EntData_Parse(const char *entdata) +{ + std::vector result; + const char *data = entdata; + + /* go through all the entities */ + while (1) { + /* parse the opening brace */ + data = COM_Parse(data); + if (!data) + break; + if (com_token[0] != '{') + Error("%s: found %s when expecting {", __func__, com_token); + + /* Allocate a new entity */ + entdict_t entity; + + /* go through all the keys in this entity */ + while (1) { + /* parse key */ + data = COM_Parse(data); + if (!data) + Error("%s: EOF without closing brace", __func__); + + std::string keystr { com_token }; + + if (keystr == "}") + break; + if (keystr.length() > MAX_ENT_KEY - 1) + Error("%s: Key length > %i: '%s'", __func__, MAX_ENT_KEY - 1, keystr.c_str()); + + /* parse value */ + data = COM_Parse(data); + if (!data) + Error("%s: EOF without closing brace", __func__); + + std::string valstring { com_token }; + + if (valstring[0] == '}') + Error("%s: closing brace without data", __func__); + if (valstring.length() > MAX_ENT_VALUE - 1) + Error("%s: Value length > %i", __func__, MAX_ENT_VALUE - 1); + + entity[keystr] = valstring; + } + + result.push_back(entity); + } + + return result; +} + +/* + * ================ + * EntData_Write + * ================ + */ +std::string +EntData_Write(const std::vector &ents) +{ + std::stringstream out; + for (const auto &ent : ents) { + out << "{\n"; + for (const auto &epair : ent) { + out << "\"" << epair.first << "\" \"" << epair.second << "\"\n"; + } + out << "}\n"; + } + return out.str(); +} + +std::string +EntDict_StringForKey(const entdict_t &dict, const std::string key) +{ + auto it = dict.find(key); + if (it != dict.end()) { + return it->second; + } + return ""; +} + +float +EntDict_FloatForKey(const entdict_t &dict, const std::string key) +{ + auto s = EntDict_StringForKey(dict, key); + if (s.empty()) + return 0; + + try { + return std::stof(s); + } catch (std::exception &) { + return 0.0f; + } +} + +void +EntDict_RemoveValueForKey(entdict_t &dict, const std::string &key) +{ + const auto it = dict.find(key); + if (it != dict.end()) { + dict.erase(it); + } + Q_assert(dict.find(key) == dict.end()); +} + +void //mxd +EntDict_RenameKey(entdict_t &dict, const std::string &from, const std::string &to) +{ + const auto it = dict.find(from); + if (it != dict.end()) { + swap(dict[to], it->second); + dict.erase(it); + } +} diff --git a/include/common/entdata.h b/include/common/entdata.h new file mode 100644 index 00000000..2ee983de --- /dev/null +++ b/include/common/entdata.h @@ -0,0 +1,31 @@ +/* Copyright (C) 1996-1997 Id Software, Inc. + + 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + See file, 'COPYING', for details. +*/ + +#include +#include +#include + +using entdict_t = std::map; + +std::vector EntData_Parse(const char *entdata); +std::string EntData_Write(const std::vector &ents); +std::string EntDict_StringForKey(const entdict_t &dict, const std::string key); +float EntDict_FloatForKey(const entdict_t &dict, const std::string key); +void EntDict_RemoveValueForKey(entdict_t &dict, const std::string &key); +void EntDict_RenameKey(entdict_t &dict, const std::string &from, const std::string &to); diff --git a/include/light/entities.hh b/include/light/entities.hh index cf2b2e2d..bd867397 100644 --- a/include/light/entities.hh +++ b/include/light/entities.hh @@ -24,14 +24,13 @@ #include #include +#include #include #include #include #define DEFAULTLIGHTLEVEL 300.0f -using entdict_t = std::map; - /* * Light attenuation formalae * (relative to distance 'x' from the light source) @@ -178,9 +177,6 @@ const entdict_t *FindEntDictWithKeyPair(const std::string &key, const std::strin const char *ValueForKey(const light_t *ent, const char *key); void EntDict_VectorForKey(const entdict_t &ent, const std::string &key, vec3_t vec); -std::string EntDict_StringForKey(const entdict_t &dict, const std::string key); -float EntDict_FloatForKey(const entdict_t &dict, const std::string key); - void SetWorldKeyValue(const std::string &key, const std::string &value); std::string WorldValueForKey(const std::string &key); @@ -196,6 +192,4 @@ bool EntDict_CheckTargetKeysMatched(const mbsp_t *bsp, const entdict_t &entity, bool EntDict_CheckTargetnameKeyMatched(const mbsp_t *bsp, const entdict_t &entity, const std::vector &all_edicts); -std::vector EntData_Parse(const char *entdata); //mxd - #endif /* __LIGHT_ENTITIES_H__ */ diff --git a/light/CMakeLists.txt b/light/CMakeLists.txt index f726459d..9c72a1f8 100644 --- a/light/CMakeLists.txt +++ b/light/CMakeLists.txt @@ -24,7 +24,8 @@ set(LIGHT_SOURCES surflight.cc settings.cc imglib.cc - ${CMAKE_SOURCE_DIR}/common/bspfile.cc + ${CMAKE_SOURCE_DIR}/common/bspfile.cc + ${CMAKE_SOURCE_DIR}/common/entdata.cc ${CMAKE_SOURCE_DIR}/common/cmdlib.cc ${CMAKE_SOURCE_DIR}/common/mathlib.cc ${CMAKE_SOURCE_DIR}/common/qvec.cc diff --git a/light/entities.cc b/light/entities.cc index 8ee7a480..1d649407 100644 --- a/light/entities.cc +++ b/light/entities.cc @@ -893,127 +893,6 @@ static rgba_miptex_t *FindProjectionTexture(const mbsp_t *bsp, const char *texna return nullptr; } -/* - * ================== - * EntData_Parse - * ================== - */ -std::vector -EntData_Parse(const char *entdata) -{ - std::vector result; - const char *data = entdata; - - /* go through all the entities */ - while (1) { - /* parse the opening brace */ - data = COM_Parse(data); - if (!data) - break; - if (com_token[0] != '{') - Error("%s: found %s when expecting {", __func__, com_token); - - /* Allocate a new entity */ - entdict_t entity; - - /* go through all the keys in this entity */ - while (1) { - /* parse key */ - data = COM_Parse(data); - if (!data) - Error("%s: EOF without closing brace", __func__); - - std::string keystr { com_token }; - - if (keystr == "}") - break; - if (keystr.length() > MAX_ENT_KEY - 1) - Error("%s: Key length > %i: '%s'", __func__, MAX_ENT_KEY - 1, keystr.c_str()); - - /* parse value */ - data = COM_Parse(data); - if (!data) - Error("%s: EOF without closing brace", __func__); - - std::string valstring { com_token }; - - if (valstring[0] == '}') - Error("%s: closing brace without data", __func__); - if (valstring.length() > MAX_ENT_VALUE - 1) - Error("%s: Value length > %i", __func__, MAX_ENT_VALUE - 1); - - entity[keystr] = valstring; - } - - result.push_back(entity); - } - - return result; -} - -/* - * ================ - * EntData_Write - * ================ - */ -std::string -EntData_Write(const std::vector &ents) -{ - std::stringstream out; - for (const auto &ent : ents) { - out << "{\n"; - for (const auto &epair : ent) { - out << "\"" << epair.first << "\" \"" << epair.second << "\"\n"; - } - out << "}\n"; - } - return out.str(); -} - -std::string -EntDict_StringForKey(const entdict_t &dict, const std::string key) -{ - auto it = dict.find(key); - if (it != dict.end()) { - return it->second; - } - return ""; -} - -float -EntDict_FloatForKey(const entdict_t &dict, const std::string key) -{ - auto s = EntDict_StringForKey(dict, key); - if (s.empty()) - return 0; - - try { - return std::stof(s); - } catch (std::exception &) { - return 0.0f; - } -} - -void -EntDict_RemoveValueForKey(entdict_t &dict, const std::string &key) -{ - const auto it = dict.find(key); - if (it != dict.end()) { - dict.erase(it); - } - Q_assert(dict.find(key) == dict.end()); -} - -void //mxd -EntDict_RenameKey(entdict_t &dict, const std::string &from, const std::string &to) -{ - const auto it = dict.find(from); - if (it != dict.end()) { - swap(dict[to], it->second); - dict.erase(it); - } -} - static std::string ParseEscapeSequences(const std::string &input) { diff --git a/vis/CMakeLists.txt b/vis/CMakeLists.txt index 1e7340ed..4ec49686 100644 --- a/vis/CMakeLists.txt +++ b/vis/CMakeLists.txt @@ -10,6 +10,7 @@ set(VIS_SOURCES vis.cc soundpvs.cc state.cc + ${CMAKE_SOURCE_DIR}/common/entdata.cc ${CMAKE_SOURCE_DIR}/common/cmdlib.cc ${CMAKE_SOURCE_DIR}/common/mathlib.cc ${CMAKE_SOURCE_DIR}/common/qvec.cc