Skip to content

Commit e839120

Browse files
committed
fix: sm-dllinjector patch
1 parent 3efe3bc commit e839120

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed

SM-KeyAPI/src/dllmain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved) {
5757
lua_pop(L, 2);
5858

5959
INFO("Injected SM-KeyAPI into Lua environment!");
60-
}, true);
60+
}, false);
6161

62-
INFO("SM-KeyAPI loaded!");
62+
//INFO("SM-KeyAPI loaded!");
6363
}
6464

6565
if (dwReason == DLL_PROCESS_DETACH) {

SM-KeyAPI/vendor/include/carbon/lua/executor.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ class LuaExecutor {
6464
/// </summary>
6565
/// <param name="func">The function to call.</param>
6666
void OnUpdate(std::function<void(lua_State*)> func, bool removeAfterExecution = false) {
67-
this->updateFuncs.push({ func, removeAfterExecution });
67+
if (removeAfterExecution) {
68+
this->tempUpdateFuncs.push(func);
69+
}
70+
else {
71+
this->updateFuncs.emplace_back(func);
72+
}
6873
}
6974

7075
/// <summary>
@@ -74,20 +79,24 @@ class LuaExecutor {
7479
/// <param name="func">The function to call.</param>
7580
/// <param name="immediateIfPlaying">Whether to also call the function immediately if the game is already playing.</param>
7681
void OnInitialize(std::function<void(lua_State*)> func, bool immediateIfPlaying = true) {
77-
this->initFuncs.push_back(func);
82+
this->initFuncs.emplace_back(func);
7883

7984
if (immediateIfPlaying && Carbon::SM::Contraption::IsPlaying()) {
8085
INFO("Game is already playing, calling supposed init function immediately");
81-
this->updateFuncs.emplace(func, true); // Only call once
86+
this->tempUpdateFuncs.push(func); // Only call once
8287
}
8388
}
8489

8590
private:
8691
void Update(lua_State* L) {
87-
while (!this->updateFuncs.empty()) {
88-
auto& [func, remove] = this->updateFuncs.front();
92+
while (!this->tempUpdateFuncs.empty()) {
93+
auto& func = this->tempUpdateFuncs.front();
94+
func(L);
95+
this->tempUpdateFuncs.pop();
96+
}
97+
98+
for (auto& func : this->updateFuncs) {
8999
func(L);
90-
if (remove) this->updateFuncs.pop();
91100
}
92101
}
93102

@@ -131,6 +140,8 @@ class LuaExecutor {
131140
std::optional<PLH::NatDetour> updateDetour = std::nullopt;
132141
std::optional<PLH::NatDetour> initDetour = std::nullopt;
133142

134-
std::queue<std::pair<std::function<void(lua_State*)>, bool>> updateFuncs;
143+
std::queue<std::function<void(lua_State*)>> tempUpdateFuncs;
144+
145+
std::vector<std::function<void(lua_State*)>> updateFuncs;
135146
std::vector<std::function<void(lua_State*)>> initFuncs;
136147
};

SM-KeyAPI/vendor/include/carbon/lua/lauxlib.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ namespace Carbon::Lua::Prototypes {
8282
typedef void (luaL_addstring_t)(luaL_Buffer* B, const char* s);
8383
typedef void (luaL_addvalue_t)(luaL_Buffer* B);
8484
typedef void (luaL_pushresult_t)(luaL_Buffer* B);
85+
typedef void (luaL_openlibs_t)(lua_State* L);
8586
} // namespace Carbon::Lua::Prototypes
8687

8788
/// <summary>
@@ -527,6 +528,17 @@ inline void luaL_pushresult(luaL_Buffer* B) {
527528
return fn(B);
528529
}
529530

531+
/// <summary>
532+
/// Opens the libraries.
533+
/// </summary>
534+
/// <param name="L">The Lua state.</param>
535+
/// <returns></returns>
536+
inline void luaL_openlibs(lua_State* L) {
537+
using namespace Carbon::Lua::Prototypes;
538+
static auto fn = GetFunc<luaL_openlibs_t>("luaL_openlibs");
539+
return fn(L);
540+
}
541+
530542
/// ============= ///
531543
/// Useful macros ///
532544
/// ============= ///

SM-KeyAPI/vendor/include/carbon/tools.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212
// LOG template fn that takes in a colour and message and uses fmt::format to format the message
1313
template <typename... Args>
1414
void LOG(Carbon::SM::UTILS::Colour colour, Carbon::SM::UTILS::LogType type, const std::string& message, Args&&... args) {
15-
Carbon::SM::UTILS::Console* console = Carbon::SM::Contraption::GetInstance()->console;
15+
using namespace Carbon::SM;
16+
Contraption* contraption = Contraption::GetInstance();
17+
if (!contraption) {
18+
//std::cerr << "tried to log << " << message << " but Contraption::GetInstance() returned nullptr" << std::endl;
19+
return;
20+
}
21+
22+
UTILS::Console* console = contraption->console;
1623

1724
if (console) {
1825
console->Log(fmt::format(fmt::runtime(message), std::forward<Args>(args)...), colour, type);

SM-KeyAPI/vendor/include/carbon/utils.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,5 @@
1919
public: \
2020
[[nodiscard]] static type* GetInstance() { \
2121
auto instance = *reinterpret_cast<type**>(Carbon::Offsets::Rebased::type); \
22-
while (!instance) { \
23-
instance = *reinterpret_cast<type**>(Carbon::Offsets::Rebased::type); \
24-
} \
2522
return instance; \
2623
}

0 commit comments

Comments
 (0)