/entity/EntityProp.cpp

http://cssmatch-plugin.googlecode.com/ · C++ · 114 lines · 68 code · 14 blank · 32 comment · 10 complexity · 324a2e2fb14afe0eced79f8c592137f5 MD5 · raw file

  1. /*
  2. * Copyright 2008-2013 Nicolas Maingot
  3. *
  4. * This file is part of CSSMatch.
  5. *
  6. * CSSMatch is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * CSSMatch is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with CSSMatch; if not, see <http://www.gnu.org/licenses>.
  18. *
  19. * Additional permission under GNU GPL version 3 section 7
  20. *
  21. * If you modify CSSMatch, or any covered work, by linking or combining
  22. * it with "Source SDK" (or a modified version of that SDK), containing
  23. * parts covered by the terms of Source SDK licence, the licensors of
  24. * CSSMatch grant you additional permission to convey the resulting work.
  25. */
  26. #include "EntityProp.h"
  27. #include "edict.h" // BaseEntity, edict_t
  28. #include "dt_send.h" // SendTable
  29. #include "../plugin/ServerPlugin.h"
  30. #include <sstream>
  31. using namespace cssmatch;
  32. using std::string;
  33. using std::getline;
  34. using std::istringstream;
  35. void EntityProp::getOffset(SendTable * table, istringstream & propPathLeft)
  36. {
  37. // Get the next step into the props table
  38. string pathNextStep;
  39. getline(propPathLeft, pathNextStep, '.');
  40. //Msg("Scanning %s...\n", propPathLeft.c_str());
  41. int nbrProps = table->GetNumProps();
  42. int i=0;
  43. while(i<nbrProps)
  44. {
  45. SendProp * sProp = table->GetProp(i);
  46. if (pathNextStep == sProp->GetName())
  47. {
  48. offset += sProp->GetOffset();
  49. switch(sProp->GetType())
  50. {
  51. case DPT_Int:
  52. case DPT_Float:
  53. case DPT_Vector:
  54. case DPT_String:
  55. case DPT_Array:
  56. // Found the prop itself, the offset is up to date
  57. i = nbrProps; // break
  58. break;
  59. case DPT_DataTable:
  60. // Step reached, go to the next step
  61. getOffset(sProp->GetDataTable(), propPathLeft);
  62. break;
  63. default:
  64. // Prop not found
  65. offset = 0;
  66. i = nbrProps; // break
  67. }
  68. }
  69. i++;
  70. }
  71. }
  72. void EntityProp::initialize()
  73. {
  74. // Get all server classes
  75. IServerGameDLL * serverGameDll = ServerPlugin::getInstance()->getInterfaces()->serverGameDll;
  76. if (serverGameDll != NULL)
  77. {
  78. ServerClass * classes = serverGameDll->GetAllServerClasses();
  79. // Search the suitable class
  80. while (classes != NULL)
  81. {
  82. if (theClass == classes->GetName())
  83. {
  84. istringstream pathToProp(path);
  85. getOffset(classes->m_pTable, pathToProp);
  86. break;
  87. }
  88. else
  89. classes = classes->m_pNext; // until m_pNext is NULL
  90. }
  91. }
  92. else
  93. CSSMATCH_PRINT(string(
  94. "Unable to find the offset of prop ") + theClass +
  95. ", IServerGameDll instance not ready")
  96. }
  97. EntityProp::EntityProp(const string & propClass, const string & propPath)
  98. : initialized(false), offset(0), theClass(propClass), path(propPath)
  99. {
  100. }