Skip to content

Commit 0ef3e07

Browse files
committed
Add np.GetGameModeCount and np.GetGameModeName
1 parent 4a314d2 commit 0ef3e07

File tree

6 files changed

+58
-10
lines changed

6 files changed

+58
-10
lines changed

Documentation/noitapatcher.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ function noitapatcher.MagicNumbersGetList() end
216216
---@return integer
217217
function noitapatcher.GetGameModeNr() end
218218

219+
---@return integer
220+
function noitapatcher.GetGameModeCount() end
221+
222+
---@param idx integer?
223+
---@return string
224+
function noitapatcher.GetGameModeCount(idx) end
225+
219226
---Discard and then reload all material files.
220227
---You should call ComponentSetValue(world_state, "changed_materials", "") after calling this.
221228
function noitapatcher.ReloadMaterials() end

src/game_mode.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@
33
namespace np {
44

55
int* game_mode_nr;
6-
void* game_modes_begin;
6+
vs13::vector<game_mode>* game_modes_vec;
77

88
int lua_SetGameModeDeterministic(lua_State* L)
99
{
10-
if (!game_mode_nr || !game_modes_begin)
10+
if (!game_mode_nr || !game_modes_vec)
1111
return 0;
1212

1313
auto deterministic = (bool)lua_toboolean(L, 1);
1414

15-
auto game_modes = *(char**)game_modes_begin;
16-
auto game_mode = game_modes + 0xc0 * *game_mode_nr;
17-
*(int*)(game_mode + 0x64) = deterministic ? 1 : 0;
15+
(*game_modes_vec)[*game_mode_nr].deterministic = deterministic;
1816

1917
return 0;
2018
}
@@ -28,4 +26,27 @@ int lua_GetGameModeNr(lua_State* L)
2826
return 1;
2927
}
3028

29+
int lua_GetGameModeCount(lua_State* L)
30+
{
31+
lua_pushinteger(L, game_modes_vec->size());
32+
return 1;
33+
}
34+
35+
int lua_GetGameModeName(lua_State* L)
36+
{
37+
int nr{};
38+
if (lua_gettop(L) == 0) {
39+
nr = *game_mode_nr;
40+
} else {
41+
nr = luaL_checkinteger(L, 1);
42+
}
43+
44+
if (nr < 0 || nr >= game_modes_vec->size()) {
45+
return luaL_error(L, "Game mode number out of range");
46+
}
47+
48+
lua_pushstring(L, (*game_modes_vec)[nr].name.c_str());
49+
return 1;
50+
}
51+
3152
}

src/game_mode.hpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,31 @@ extern "C" {
66
#include <lauxlib.h>
77
}
88

9+
#include <vs2013/string.hpp>
10+
#include <vs2013/vector.hpp>
11+
912
namespace np {
1013

14+
struct game_mode {
15+
char padding[0x64];
16+
int deterministic;
17+
char padding2[0x10];
18+
vs13::string name;
19+
char padding3[48];
20+
};
21+
22+
static_assert(offsetof(game_mode, deterministic) == 0x64);
23+
static_assert(offsetof(game_mode, name) == 0x78);
24+
static_assert(sizeof(game_mode) == 0xc0);
25+
1126
extern int* game_mode_nr;
12-
extern void* game_modes_begin;
27+
extern vs13::vector<game_mode>* game_modes_vec;
1328

1429

1530
int lua_SetGameModeDeterministic(lua_State*);
1631
int lua_GetGameModeNr(lua_State* L);
32+
int lua_GetGameModeCount(lua_State* L);
33+
int lua_GetGameModeName(lua_State* L);
1734

1835
}
1936

src/lua_util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ std::vector<stack_entry> get_stack_trace(lua_State* L, int start_level, int coun
3232
std::vector<stack_entry> trace;
3333

3434
for (int level = start_level; true; ++level) {
35-
if (count != -1 && count-- != 0)
35+
if (count != -1 && count-- == 0)
3636
break;
3737

3838
auto stack = get_stack_entry(L, level);

src/main.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ void find_game_mode()
448448
Bytes{0x8b, 0x0d},
449449
Capture{"game_modes_end", 4},
450450
Bytes{0xb8, 0xab, 0xaa, 0xaa, 0x2a, 0x56, 0x57, 0x8b, 0x3d},
451-
Capture{"game_modes_begin", 4},
451+
Capture{"game_modes_vec", 4},
452452
Bytes{0x2b, 0xcf, 0xf7, 0xe9, 0x8b, 0x0d},
453453
Capture{"game_mode_nr", 4}
454454
);
@@ -460,11 +460,11 @@ void find_game_mode()
460460
}
461461

462462
np::game_mode_nr = result.get<int*>("game_mode_nr");
463-
np::game_modes_begin = result.get<void*>("game_modes_begin");
463+
np::game_modes_vec = result.get<vs13::vector<np::game_mode>*>("game_modes_vec");
464464

465465
std::cout << "Found game mode items: " << '\n';
466466
std::cout << " game_mode_nr: " << np::game_mode_nr << '\n';
467-
std::cout << " game_modes_begin: " << np::game_modes_begin << '\n';
467+
std::cout << " game_modes_vec: " << np::game_modes_vec << '\n';
468468
}
469469

470470
struct ShootProjectileFiredHooksCreator {
@@ -883,6 +883,8 @@ static const luaL_Reg nplib[] = {
883883
{"PhysBodyGetTransform", np::PhysBodyGetTransform},
884884
{"SetGameModeDeterministic", np::lua_SetGameModeDeterministic},
885885
{"GetGameModeNr", np::lua_GetGameModeNr},
886+
{"GetGameModeName", np::lua_GetGameModeName},
887+
{"GetGameModeCount", np::lua_GetGameModeCount},
886888
{"SetPauseState", SetPauseState},
887889
{"GetPauseState", GetPauseState},
888890
{"GetWorldInfo", GetWorldInfo},

src/noita.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ struct SystemManager {
7676
vs13::vector<void*> mSystemAutoCreators;
7777
vs13::vector<ComponentUpdator*> mSystems;
7878
};
79+
extern SystemManager* system_manager;
7980

8081

8182
// Note: It's really a mix between __fastcall (uses registers) and

0 commit comments

Comments
 (0)