Skip to content

Commit

Permalink
Merge branch '133-if-you-do-it-quickndirty-your-code-quality-will-suf…
Browse files Browse the repository at this point in the history
…fer' of https://github.com/Ismoh/NoitaMP into 133-if-you-do-it-quickndirty-your-code-quality-will-suffer
  • Loading branch information
Ismoh committed Dec 8, 2023
2 parents 9dde373 + 6d7c686 commit 48ce686
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 71 deletions.
36 changes: 33 additions & 3 deletions .building/lua_noitamp_native/entity_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,10 @@ static int l_NativeEntityMap_getNuidBySerializedString(lua_State * L) {
uint32_t eid, nuid;

g_entity_map.get_ids_by_entry(entity_serialized((void *)s, len), eid, nuid);

lua_pushinteger(L, nuid);
if (nuid == INVALID_ID)
lua_pushnil(L);
else
lua_pushinteger(L, nuid);

return 1;
}
Expand All @@ -175,8 +177,34 @@ static int l_NativeEntityMap_getEntityIdBySerializedString(lua_State * L) {
uint32_t eid, nuid;

g_entity_map.get_ids_by_entry(entity_serialized((void *)s, len), eid, nuid);
if (eid == INVALID_ID)
lua_pushnil(L);
else
lua_pushinteger(L, eid);

return 1;
}

static int l_NativeEntityMap_getSerializedStringByEntityId(lua_State* L) {
uint32_t eid = lua_tonumber(L, 1);

const entity_serialized *e = g_entity_map.get_entity(eid, INVALID_ID);
if (!e)
lua_pushnil(L);
else
lua_pushlstring(L, reinterpret_cast<const char *>(e->p.get()), e->len);

return 1;
}

static int l_NativeEntityMap_getSerializedStringByNuid(lua_State* L) {
uint32_t nuid = lua_tonumber(L, 1);

lua_pushinteger(L, eid);
const entity_serialized* e = g_entity_map.get_entity(INVALID_ID, nuid);
if (!e)
lua_pushnil(L);
else
lua_pushlstring(L, reinterpret_cast<const char*>(e->p.get()), e->len);

return 1;
}
Expand Down Expand Up @@ -242,6 +270,8 @@ extern "C" void register_NativeEntityMap(lua_State * L) {
luaL_Reg NativeEntityMaplib[] = {
{"getNuidBySerializedString", l_NativeEntityMap_getNuidBySerializedString},
{"getEntityIdBySerializedString", l_NativeEntityMap_getEntityIdBySerializedString},
{"getSerializedStringByEntityId", l_NativeEntityMap_getSerializedStringByEntityId},
{"getSerializedStringByNuid", l_NativeEntityMap_getSerializedStringByNuid},
{"setMappingOfNuidToSerialisedString", l_NativeEntityMap_setMappingOfNuidToSerialisedString},
{"setMappingOfEntityIdToSerialisedString", l_NativeEntityMap_setMappingOfEntityIdToSerialisedString},
{"removeMappingOfEntityId", l_NativeEntityMap_removeMappingOfEntityId},
Expand Down
19 changes: 17 additions & 2 deletions .building/lua_noitamp_native/entity_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@

struct entity_serialized {
std::shared_ptr<uint8_t> p;
uint16_t len;
uint32_t len;

entity_serialized(void* in_p, size_t in_len)
: p(new uint8_t[in_len]), len(in_len)
{
assert(in_len <= UINT16_MAX);
memcpy(p.get(), in_p, in_len);
}

Expand Down Expand Up @@ -79,6 +78,22 @@ struct entity_values {
void get_ids_by_entry(const entity_serialized& ed, uint32_t& eid, uint32_t& nuid);
void remove_ids(uint32_t eid, uint32_t nuid);

const entity_serialized* get_entity(uint32_t eid, uint32_t nuid) {
if (eid != INVALID_ID) {
auto it = eid_to_entity.find(eid);
if (it != eid_to_entity.end()) {
return &it->second->entity;
}
}
else if (nuid != INVALID_ID) {
auto it = nuid_to_entity.find(nuid);
if (it != nuid_to_entity.end()) {
return &it->second->entity;
}
}
return NULL;
}

size_t get_count(void) const {
return entity_ptrs.size();
}
Expand Down
11 changes: 11 additions & 0 deletions .building/lua_noitamp_native/perftest/perftest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ int main(void) {
printf("empty mem usage: %zu bytes\n", em.get_memory_usage());

entity_serialized es = make_random_entity();
const entity_serialized* pes;
uint32_t eid, nuid;

em.add_id_to_entity(INVALID_ID, INVALID_ID, es);
Expand Down Expand Up @@ -51,6 +52,16 @@ int main(void) {

em.add_id_to_entity(3, 4, es);
assert(em.get_count() == 1);
pes = em.get_entity(3, INVALID_ID);
assert(pes);
assert(pes->len == es.len);
assert(memcmp(pes->p.get(), es.p.get(), es.len) == 0);

pes = em.get_entity(INVALID_ID, 4);
assert(pes);
assert(pes->len == es.len);
assert(memcmp(pes->p.get(), es.p.get(), es.len) == 0);

em.remove_ids(INVALID_ID, 4);
assert(em.get_count() == 0);

Expand Down
7 changes: 6 additions & 1 deletion .debug/lua-definitions/native_entity_map.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,10 @@ function NativeEntityMap.removeAllMappings() end

--- Return entity serialized string associated with @entity_id or nil
---@param entity_id number|nil
---@return string serialzed_string binaryString
---@return string binaryString|nil
function NativeEntityMap.getSerializedStringByEntityId(entity_id) end

--- Return entity serialized string associated with @nuid or nil
---@param nuid number|nil
---@return string binaryString|nil
function NativeEntityMap.getSerializedStringByNuid(nuid) end
96 changes: 48 additions & 48 deletions docs/lua-docs/doc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9179,163 +9179,163 @@
},
{
"file": "https://github.com/Ismoh/NoitaMPmods/noita-mp/files/scripts/util/NoitaPatcherUtils.lua",
"finish": 420031,
"start": 420014,
"finish": 440031,
"start": 440014,
"type": "doc.class"
}
],
"fields": [
{
"extends": {
"finish": 720062,
"start": 720041,
"finish": 740062,
"start": 740041,
"type": "select",
"view": "unknown"
},
"file": "https://github.com/Ismoh/NoitaMPmods/noita-mp/files/scripts/util/NoitaPatcherUtils.lua",
"finish": 720038,
"finish": 740038,
"name": "base64",
"start": 720008,
"start": 740008,
"type": "setfield"
},
{
"desc": "Simple profiler that can be used to measure the duration of a function and the memory usage of a function.",
"extends": {
"finish": 580133,
"start": 580049,
"finish": 600133,
"start": 600049,
"type": "binary",
"view": "CustomProfiler"
},
"file": "https://github.com/Ismoh/NoitaMPmods/noita-mp/files/scripts/util/NoitaPatcherUtils.lua",
"finish": 580046,
"finish": 600046,
"name": "customProfiler",
"start": 580008,
"start": 600008,
"type": "setfield"
},
{
"desc": " Deserialize an entity from a serialized base64 string and create it at the given position.\n\n@*param* `entityId` — mostly an empty entity, but required\n\n@*param* `base64String` — serialized entity in base64 format\n\n@*param* `x` — x position to create entity at, but optional.\n\n@*param* `y` — y position to create entity at, but optional.\n\n@*return* `entityId` — of the created entity",
"extends": {
"args": [
{
"finish": 280008,
"start": 280008,
"finish": 290008,
"start": 290008,
"type": "self",
"view": "NoitaPatcherUtils"
},
{
"desc": "mostly an empty entity, but required",
"finish": 280053,
"start": 280045,
"finish": 290053,
"start": 290045,
"type": "local",
"view": "number"
},
{
"desc": "serialized entity in base64 format",
"finish": 280067,
"start": 280055,
"finish": 290067,
"start": 290055,
"type": "local",
"view": "string"
},
{
"desc": "x position to create entity at, but optional.",
"finish": 280070,
"start": 280069,
"finish": 290070,
"start": 290069,
"type": "local",
"view": "number|nil"
},
{
"desc": "y position to create entity at, but optional.",
"finish": 280073,
"start": 280072,
"finish": 290073,
"start": 290072,
"type": "local",
"view": "number|nil"
}
],
"desc": " Deserialize an entity from a serialized base64 string and create it at the given position.\n\n@*param* `entityId` — mostly an empty entity, but required\n\n@*param* `base64String` — serialized entity in base64 format\n\n@*param* `x` — x position to create entity at, but optional.\n\n@*param* `y` — y position to create entity at, but optional.\n\n@*return* `entityId` — of the created entity",
"finish": 350003,
"finish": 370003,
"returns": [
{
"type": "function.return",
"view": "number"
}
],
"start": 280000,
"start": 290000,
"type": "function",
"view": "function NoitaPatcherUtils.deserializeEntity(self: NoitaPatcherUtils, entityId: number, base64String: string, x: number|nil, y: number|nil)\n -> entityId: number"
},
"file": "https://github.com/Ismoh/NoitaMPmods/noita-mp/files/scripts/util/NoitaPatcherUtils.lua",
"finish": 280044,
"finish": 290044,
"name": "deserializeEntity",
"start": 280009,
"start": 290009,
"type": "setmethod"
},
{
"extends": {
"finish": 760080,
"start": 760051,
"finish": 790079,
"start": 790050,
"type": "select",
"view": "unknown"
},
"file": "https://github.com/Ismoh/NoitaMPmods/noita-mp/files/scripts/util/NoitaPatcherUtils.lua",
"finish": 760048,
"name": "luaNoitaMpNative",
"start": 760008,
"finish": 790047,
"name": "nativeEntityMap",
"start": 790008,
"type": "setfield"
},
{
"desc": "NoitaPatcherUtils constructor.\n\n@*param* `customProfiler` — required\n\n@*param* `np` — required",
"extends": {
"args": [
{
"finish": 410008,
"start": 410008,
"finish": 430008,
"start": 430008,
"type": "self",
"view": "NoitaPatcherUtils"
},
{
"desc": "required",
"finish": 410045,
"start": 410031,
"finish": 430045,
"start": 430031,
"type": "local",
"view": "CustomProfiler"
},
{
"desc": "required",
"finish": 410049,
"start": 410047,
"finish": 430049,
"start": 430047,
"type": "local",
"view": "noitapatcher"
}
],
"desc": "NoitaPatcherUtils constructor.\n\n@*param* `customProfiler` — required\n\n@*param* `np` — required",
"finish": 800003,
"finish": 830003,
"returns": [
{
"type": "function.return",
"view": "NoitaPatcherUtils"
}
],
"start": 410000,
"start": 430000,
"type": "function",
"view": "function NoitaPatcherUtils.new(self: NoitaPatcherUtils, customProfiler: CustomProfiler, np: noitapatcher)\n -> NoitaPatcherUtils"
},
"file": "https://github.com/Ismoh/NoitaMPmods/noita-mp/files/scripts/util/NoitaPatcherUtils.lua",
"finish": 410030,
"finish": 430030,
"name": "new",
"start": 410009,
"start": 430009,
"type": "setmethod"
},
{
"extends": {
"finish": 630107,
"start": 630037,
"finish": 650107,
"start": 650037,
"type": "binary",
"view": "noitapatcher"
},
"file": "https://github.com/Ismoh/NoitaMPmods/noita-mp/files/scripts/util/NoitaPatcherUtils.lua",
"finish": 630034,
"finish": 650034,
"name": "np",
"start": 630008,
"start": 650008,
"type": "setfield"
},
{
Expand All @@ -9356,7 +9356,7 @@
}
],
"desc": " Serialize an entity to a base64 and md5 string.\n\n@*return* `base64String` — base64 encoded string",
"finish": 200003,
"finish": 210003,
"returns": [
{
"type": "function.return",
Expand All @@ -9376,15 +9376,15 @@
{
"desc": "Utils class for lazy developers.",
"extends": {
"finish": 680065,
"start": 680040,
"finish": 700065,
"start": 700040,
"type": "select",
"view": "Utils"
},
"file": "https://github.com/Ismoh/NoitaMPmods/noita-mp/files/scripts/util/NoitaPatcherUtils.lua",
"finish": 680037,
"finish": 700037,
"name": "utils",
"start": 680008,
"start": 700008,
"type": "setfield"
}
],
Expand Down
2 changes: 1 addition & 1 deletion docs/lua-docs/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -3240,7 +3240,7 @@ function NoitaPatcherUtils.deserializeEntity(self: NoitaPatcherUtils, entityId:

@*return* `entityId` — of the created entity

### luaNoitaMpNative
### nativeEntityMap


```lua
Expand Down
Loading

0 comments on commit 48ce686

Please sign in to comment.