ClassTypeStructs.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #pragma once
  2. #define CLASS_TYPE_STRUCTS_VERSION 1
  3. #include <string.h>
  4. #include "other.hpp"
  5. struct RTTI_Complete_Object_Locator;
  6. struct RTTI_Class_Hierarchy_Descriptor;
  7. struct RTTI_Base_Class_Descriptor;
  8. struct RTTI_Type_Descriptor;
  9. struct RTTI_Complete_Object_Locator
  10. {
  11. void* signature;
  12. void* offset;
  13. void* cdOffset;
  14. RTTI_Type_Descriptor* RTTI_Type_Descriptor;
  15. RTTI_Class_Hierarchy_Descriptor* RTTI_Class_Hierarchy_Descriptor;
  16. };
  17. struct RTTI_Class_Hierarchy_Descriptor
  18. {
  19. void* signature;
  20. void* attributes;
  21. void* numBaseClasses;
  22. void* BaseClassArray;
  23. };
  24. struct RTTI_Base_Class_Descriptor
  25. {
  26. RTTI_Type_Descriptor* RTTI_Type_Descriptor;
  27. unsigned int* numContainedBases;
  28. void* mdisp;
  29. void* pdisp;
  30. void* vdisp;
  31. void* attributes;
  32. RTTI_Class_Hierarchy_Descriptor* RTTI_Class_Hierarchy_Descriptor;
  33. };
  34. struct RTTI_Type_Descriptor
  35. {
  36. void* pVFTable;
  37. void* spare;
  38. char name[]; //null-terminated, indefinite length
  39. };
  40. //RTTI_instanceOf((ADDR*)objADDR, ".?AV" "BitStream" "@@");
  41. //Useful for non-ConsoleObject (non-SimObject) classes that aren't exposed to TorqueScript
  42. int RTTI_instanceOf(unsigned int* obj, const char* className)
  43. {
  44. if(obj == NULL)
  45. return -1;
  46. //RTTI_Complete_Object_Locator is located right behind vftable
  47. ADDR* vftable_meta_ptr = (ADDR*)(*obj - 4);
  48. if(vftable_meta_ptr == NULL)
  49. return -2;
  50. RTTI_Complete_Object_Locator* Complete_Object_Locator = (RTTI_Complete_Object_Locator*)(*vftable_meta_ptr);
  51. if(Complete_Object_Locator == NULL)
  52. return -3;
  53. RTTI_Class_Hierarchy_Descriptor* Class_Hierarchy_Descriptor = Complete_Object_Locator->RTTI_Class_Hierarchy_Descriptor;
  54. if(Class_Hierarchy_Descriptor == NULL)
  55. return -4;
  56. ADDR* Base_Class_Array = (ADDR*)(Class_Hierarchy_Descriptor->BaseClassArray);
  57. if(Base_Class_Array == NULL)
  58. return -5;
  59. //loop until pointer is 0
  60. for(unsigned int i = 0; Base_Class_Array[i] != 0; i++)
  61. {
  62. RTTI_Base_Class_Descriptor* Base_Class_Descriptor = (RTTI_Base_Class_Descriptor*)Base_Class_Array[i];
  63. if(Base_Class_Descriptor->RTTI_Type_Descriptor == NULL)
  64. continue;
  65. //.?AV is a C++ class
  66. //@@ at end is terminal
  67. char* testName = Base_Class_Descriptor->RTTI_Type_Descriptor->name;
  68. if(strcmp(className, testName) == 0)
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. const char* RTTI_className(unsigned int* obj)
  74. {
  75. if(obj == NULL)
  76. return "-1";
  77. //RTTI_Complete_Object_Locator is located right behind vftable
  78. ADDR* vftable_meta_ptr = (ADDR*)(*obj - 4);
  79. if(vftable_meta_ptr == NULL)
  80. return "-2";
  81. RTTI_Complete_Object_Locator* Complete_Object_Locator = (RTTI_Complete_Object_Locator*)(*vftable_meta_ptr);
  82. if(Complete_Object_Locator == NULL)
  83. return "-3";
  84. RTTI_Class_Hierarchy_Descriptor* Class_Hierarchy_Descriptor = Complete_Object_Locator->RTTI_Class_Hierarchy_Descriptor;
  85. if(Class_Hierarchy_Descriptor == NULL)
  86. return "-4";
  87. ADDR* Base_Class_Array = (ADDR*)(Class_Hierarchy_Descriptor->BaseClassArray);
  88. if(Base_Class_Array == NULL)
  89. return "-5";
  90. //loop until pointer is 0
  91. for(unsigned int i = 0; Base_Class_Array[i] != 0; i++)
  92. {
  93. RTTI_Base_Class_Descriptor* Base_Class_Descriptor = (RTTI_Base_Class_Descriptor*)Base_Class_Array[i];
  94. if(Base_Class_Descriptor->RTTI_Type_Descriptor == NULL)
  95. continue;
  96. //.?AV is a C++ class
  97. //@@ at end is terminal
  98. char* testName = Base_Class_Descriptor->RTTI_Type_Descriptor->name;
  99. return testName;
  100. }
  101. return "0";
  102. }