Skip to content

Commit

Permalink
add to_id for future uses
Browse files Browse the repository at this point in the history
  • Loading branch information
iojon committed Jan 7, 2021
1 parent 53b7c68 commit 754039d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
15 changes: 13 additions & 2 deletions crates/injected/cxx/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,35 @@
#include <cstdio>

const int NAME_TO_INDEX = 0x387c8;
using EntityMap = std::unordered_map<std::string, uint16_t>;

rust::Vec<EntityItem>
list_entities(size_t map_ptr)
{
if (!map_ptr)
return {};

using EntityMap = std::unordered_map<std::string, uint16_t>;
auto map = reinterpret_cast<EntityMap *>(map_ptr + NAME_TO_INDEX);
auto entities = reinterpret_cast<EntityDB *>(map_ptr);

rust::Vec<EntityItem> result;
for (const auto &kv : *map)
{
result.push_back(EntityItem{kv.first, kv.second});
// auto entities = reinterpret_cast<EntityDB *>(map_ptr);
// EntityDB *entity = &entities[kv.second];
// printf("%d\n", entity->id);
}

return result;
}

size_t
to_id(size_t map_ptr, rust::String id) {
if(!map_ptr) {
return -1;
}

auto map = reinterpret_cast<EntityMap *>(map_ptr + NAME_TO_INDEX);
auto it = map->find(std::string(id.data(), id.size()));
return it != map->end() ? it->second : -1;
}
1 change: 1 addition & 0 deletions crates/injected/cxx/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ using AnimationMap = std::unordered_map<uint8_t, Animation>;

struct EntityItem;
rust::Vec<EntityItem> list_entities(size_t map_ptr);
size_t to_id(size_t map_ptr, rust::String id);
1 change: 1 addition & 0 deletions crates/injected/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub mod ffi {
unsafe extern "C++" {
include!("cxx/entity.hpp");
fn list_entities(ptr: usize) -> Vec<EntityItem>;
fn to_id(ptr: usize, id: String) -> usize;
}
}

Expand Down

0 comments on commit 754039d

Please sign in to comment.