Skip to content

Commit

Permalink
fix: sm-dllinjector patch
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMcAvoy committed Jan 22, 2025
1 parent 3efe3bc commit 82b8c3d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
4 changes: 2 additions & 2 deletions SM-KeyAPI/src/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved) {
lua_pop(L, 2);

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

INFO("SM-KeyAPI loaded!");
//INFO("SM-KeyAPI loaded!");
}

if (dwReason == DLL_PROCESS_DETACH) {
Expand Down
2 changes: 1 addition & 1 deletion SM-KeyAPI/vendor/include/carbon/contraption.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Carbon::SM {

public:
static bool IsPlaying() {
while (Contraption::GetInstance()->gameState <= Null || Contraption::GetInstance()->gameState > WorldBuilder) {
while (!Contraption::GetInstance() || Contraption::GetInstance()->gameState <= Null || Contraption::GetInstance()->gameState > WorldBuilder) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

Expand Down
25 changes: 18 additions & 7 deletions SM-KeyAPI/vendor/include/carbon/lua/executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ class LuaExecutor {
/// </summary>
/// <param name="func">The function to call.</param>
void OnUpdate(std::function<void(lua_State*)> func, bool removeAfterExecution = false) {
this->updateFuncs.push({ func, removeAfterExecution });
if (removeAfterExecution) {
this->tempUpdateFuncs.push(func);
}
else {
this->updateFuncs.emplace_back(func);
}
}

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

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

private:
void Update(lua_State* L) {
while (!this->updateFuncs.empty()) {
auto& [func, remove] = this->updateFuncs.front();
while (!this->tempUpdateFuncs.empty()) {
auto& func = this->tempUpdateFuncs.front();
func(L);
this->tempUpdateFuncs.pop();
}

for (auto& func : this->updateFuncs) {
func(L);
if (remove) this->updateFuncs.pop();
}
}

Expand Down Expand Up @@ -131,6 +140,8 @@ class LuaExecutor {
std::optional<PLH::NatDetour> updateDetour = std::nullopt;
std::optional<PLH::NatDetour> initDetour = std::nullopt;

std::queue<std::pair<std::function<void(lua_State*)>, bool>> updateFuncs;
std::queue<std::function<void(lua_State*)>> tempUpdateFuncs;

std::vector<std::function<void(lua_State*)>> updateFuncs;
std::vector<std::function<void(lua_State*)>> initFuncs;
};
12 changes: 12 additions & 0 deletions SM-KeyAPI/vendor/include/carbon/lua/lauxlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ namespace Carbon::Lua::Prototypes {
typedef void (luaL_addstring_t)(luaL_Buffer* B, const char* s);
typedef void (luaL_addvalue_t)(luaL_Buffer* B);
typedef void (luaL_pushresult_t)(luaL_Buffer* B);
typedef void (luaL_openlibs_t)(lua_State* L);
} // namespace Carbon::Lua::Prototypes

/// <summary>
Expand Down Expand Up @@ -527,6 +528,17 @@ inline void luaL_pushresult(luaL_Buffer* B) {
return fn(B);
}

/// <summary>
/// Opens the libraries.
/// </summary>
/// <param name="L">The Lua state.</param>
/// <returns></returns>
inline void luaL_openlibs(lua_State* L) {
using namespace Carbon::Lua::Prototypes;
static auto fn = GetFunc<luaL_openlibs_t>("luaL_openlibs");
return fn(L);
}

/// ============= ///
/// Useful macros ///
/// ============= ///
Expand Down
9 changes: 8 additions & 1 deletion SM-KeyAPI/vendor/include/carbon/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
// LOG template fn that takes in a colour and message and uses fmt::format to format the message
template <typename... Args>
void LOG(Carbon::SM::UTILS::Colour colour, Carbon::SM::UTILS::LogType type, const std::string& message, Args&&... args) {
Carbon::SM::UTILS::Console* console = Carbon::SM::Contraption::GetInstance()->console;
using namespace Carbon::SM;
Contraption* contraption = Contraption::GetInstance();
if (!contraption) {
//std::cerr << "tried to log << " << message << " but Contraption::GetInstance() returned nullptr" << std::endl;
return;
}

UTILS::Console* console = contraption->console;

if (console) {
console->Log(fmt::format(fmt::runtime(message), std::forward<Args>(args)...), colour, type);
Expand Down
3 changes: 0 additions & 3 deletions SM-KeyAPI/vendor/include/carbon/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,5 @@
public: \
[[nodiscard]] static type* GetInstance() { \
auto instance = *reinterpret_cast<type**>(Carbon::Offsets::Rebased::type); \
while (!instance) { \
instance = *reinterpret_cast<type**>(Carbon::Offsets::Rebased::type); \
} \
return instance; \
}

0 comments on commit 82b8c3d

Please sign in to comment.