EntityManager.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "EntityManager.h"
  2. template <typename T>
  3. struct UniList
  4. {
  5. void* klass;
  6. void* monitor;
  7. UniArray<T>* store;
  8. int32_t size;
  9. int32_t version;
  10. typedef T* iterator;
  11. typedef const T* const_iterator;
  12. iterator begin() { return (*store)[0]; }
  13. const_iterator begin() const { return (*store)[0]; }
  14. iterator end() { return (*store)[size]; }
  15. const_iterator end() const { return (*store)[size]; }
  16. std::vector<T> vec()
  17. {
  18. auto result = std::vector<T>();
  19. result.reserve(size);
  20. for (auto i = begin(); i < end(); i++)
  21. result.push_back(*i);
  22. return result;
  23. }
  24. };
  25. #define TO_UNI_COLLECTION(field, collection) reinterpret_cast<collection*>(field)
  26. #define TO_UNI_LIST(field, type) TO_UNI_COLLECTION(field, UniList<type>)
  27. namespace cheat::game {
  28. EntityManager::EntityManager() : m_AvatarEntity(nullptr) {
  29. }
  30. EntityManager& EntityManager::getInstance() {
  31. static EntityManager instance;
  32. return instance;
  33. }
  34. Entity* EntityManager::entity(uint32_t runtimeID, bool unsafe) {
  35. auto entityManager = app::MoleMole_InLevelDrumPageContext_get_ENTITY(nullptr);
  36. if (entityManager == nullptr)
  37. return nullptr;
  38. auto rawEntity = app::MoleMole_EntityManager_GetValidEntity(entityManager, runtimeID);
  39. if (unsafe)
  40. return new Entity(rawEntity);
  41. return entity(rawEntity);
  42. }
  43. std::vector<app::BaseEntity*> EntityManager::rawEntities()
  44. {
  45. auto entityManager = app::MoleMole_InLevelDrumPageContext_get_ENTITY(nullptr);
  46. if (entityManager == nullptr)
  47. return {};
  48. /*LOG_DEBUG("before MoleMole_EntityManager_GetEntities");
  49. app::MoleMole_EntityManager_GetEntities(entityManager, nullptr);
  50. LOG_DEBUG("AFTQERELKFJQWEP{JFQWIEI{O MoleMole_EntityManager_GetEntities");*/
  51. auto entities = TO_UNI_LIST(app::MoleMole_EntityManager_GetEntities(entityManager), app::BaseEntity*);
  52. if (entities == nullptr)
  53. return {};
  54. std::vector<app::BaseEntity*> aliveEntities;
  55. aliveEntities.reserve(entities->size);
  56. for (const auto& entity : *entities)
  57. {
  58. if (entity != nullptr && app::MoleMole_BaseEntity_IsActive(entity))
  59. aliveEntities.push_back(entity);
  60. }
  61. return aliveEntities;
  62. }
  63. std::vector<Entity*> EntityManager::entities()
  64. {
  65. std::vector<Entity*> entityVector;
  66. for (auto& rawEntity : rawEntities())
  67. {
  68. auto ent = entity(rawEntity);
  69. if (ent != s_EmptyEntity)
  70. entityVector.push_back(ent);
  71. }
  72. return entityVector;
  73. }
  74. std::vector<Entity*> EntityManager::entities(const IEntityFilter& filter)
  75. {
  76. std::vector<Entity*> entityVector;
  77. for (auto& entity : entities())
  78. {
  79. if (filter.IsValid(entity))
  80. entityVector.push_back(entity);
  81. }
  82. return entityVector;
  83. }
  84. std::vector<Entity*> EntityManager::entities(Validator validator)
  85. {
  86. std::vector<Entity*> entityVector;
  87. for (auto& entity : entities())
  88. {
  89. if (validator(entity))
  90. entityVector.push_back(entity);
  91. }
  92. return entityVector;
  93. }
  94. Entity* EntityManager::avatar() {
  95. auto entityManager = app::MoleMole_InLevelDrumPageContext_get_ENTITY(nullptr);
  96. if (entityManager == nullptr)
  97. return s_EmptyEntity;
  98. auto avatarRaw = app::MoleMole_EntityManager_GetLocalAvatarEntity(entityManager);
  99. if (m_AvatarEntity.raw() != avatarRaw)
  100. m_AvatarEntity = Entity(avatarRaw);
  101. return &m_AvatarEntity;
  102. }
  103. cheat::game::Entity* EntityManager::entity(app::BaseEntity* rawEntity) {
  104. if (rawEntity == nullptr || !app::MoleMole_BaseEntity_IsActive(rawEntity))
  105. return s_EmptyEntity;
  106. std::lock_guard<std::mutex> lock(m_EntityCacheLock);
  107. if (m_EntityCache.count(rawEntity) > 0) {
  108. auto& entry = m_EntityCache[rawEntity];
  109. if (app::MoleMole_BaseEntity_get_runtimeID(rawEntity) == entry.second)
  110. return entry.first;
  111. delete m_EntityCache[rawEntity].first;
  112. m_EntityCache.erase(rawEntity);
  113. //entityDestroyEvent(entry.first);
  114. }
  115. if (app::MoleMole_BaseEntity_get_rootGameObject(rawEntity) == nullptr)
  116. return s_EmptyEntity;
  117. Entity* ent = new Entity(rawEntity);
  118. /*if (ent->isChest()) {
  119. delete ent;
  120. ent = new Chest(rawEntity);
  121. }*/
  122. m_EntityCache[rawEntity] = { ent, ent->runtimeID() };
  123. return ent;
  124. }
  125. app::CameraEntity* EntityManager::mainCamera() {
  126. auto entityManager = app::MoleMole_InLevelDrumPageContext_get_ENTITY(nullptr);
  127. if (entityManager == nullptr)
  128. return nullptr;
  129. auto cameraEntity = app::MoleMole_EntityManager_GetMainCameraEntity(entityManager);
  130. return cameraEntity;
  131. }
  132. }