Skip to content

Commit

Permalink
Merge pull request #1 from TechnologicNick/main
Browse files Browse the repository at this point in the history
Add SMLuaHook plugin and various other changes
  • Loading branch information
TechnologicNick authored Oct 10, 2021
2 parents 7686f84 + 8f58ccd commit f18a317
Show file tree
Hide file tree
Showing 24 changed files with 1,151 additions and 320 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 HardCoded
Copyright (c) 2021 HardCoded and TechnologicNick

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
124 changes: 124 additions & 0 deletions PluginDevFolder/SMLuaHook/SMLuaHook.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="hooks.h" />
<ClInclude Include="lua_hook_config.h" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{93661f1d-21dc-4b5b-88ec-6bea337df56b}</ProjectGuid>
<RootNamespace>SMLuaHook</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>$(SolutionDir)..\SMLibrary\include;$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>$(SolutionDir)..\SMLibrary\include;$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;SMLUAHOOK_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>smlibrary.lib</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>if not exist "$(OutputPath)plugins\" mkdir "$(OutputPath)plugins\"
copy /B "$(LocalDebuggerCommand)" "$(OutputPath)plugins\$(TargetFileName)"</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;SMLUAHOOK_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>smlibrary.lib</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>if not exist "$(OutputPath)plugins\" mkdir "$(OutputPath)plugins\"
copy /B "$(LocalDebuggerCommand)" "$(OutputPath)plugins\$(TargetFileName)"</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="..\..\SMInjector\packages\luajit.native.2.0.5\build\native\luajit.native.targets" Condition="Exists('..\..\SMInjector\packages\luajit.native.2.0.5\build\native\luajit.native.targets')" />
<Import Project="..\..\SMInjector\packages\nlohmann.json.3.10.2\build\native\nlohmann.json.targets" Condition="Exists('..\..\SMInjector\packages\nlohmann.json.3.10.2\build\native\nlohmann.json.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\SMInjector\packages\luajit.native.2.0.5\build\native\luajit.native.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\SMInjector\packages\luajit.native.2.0.5\build\native\luajit.native.targets'))" />
<Error Condition="!Exists('..\..\SMInjector\packages\nlohmann.json.3.10.2\build\native\nlohmann.json.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\SMInjector\packages\nlohmann.json.3.10.2\build\native\nlohmann.json.targets'))" />
</Target>
</Project>
33 changes: 33 additions & 0 deletions PluginDevFolder/SMLuaHook/SMLuaHook.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="hooks.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="lua_hook_config.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
</Project>
86 changes: 86 additions & 0 deletions PluginDevFolder/SMLuaHook/hooks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#pragma once
#include <stdio.h>
#include <gamehook.h>
#include <path_helper.h>
#include <lua.hpp>

#include <console.h>
using Console::Color;

#include "lua_hook_config.h"


// LUAL_REGISTER
typedef void (*pluaL_register)(lua_State*, const char*, const luaL_Reg*);
GameHook* hck_luaL_register;

// LUAL_LOADSTRING
typedef int (*pluaL_loadstring)(lua_State*, const char*);
GameHook* hck_luaL_loadstring;

// LUA_NEWSTATE
typedef lua_State* (*plua_newstate)(lua_Alloc, void*);
GameHook* hck_lua_newstate;

// LUAL_LOADBUFFER
typedef int (*pluaL_loadbuffer)(lua_State*, const char*, size_t, const char*);
GameHook* hck_luaL_loadbuffer;

// =============

namespace LuaHook::Hooks {
void hook_luaL_register(lua_State* L, const char* libname, const luaL_Reg* l) {
Console::log(Color::Aqua, "hook_luaL_register: libname=[%s]", libname);

const luaL_Reg* ptr = l;

int i = 0;
while (ptr->name != NULL) {
Console::log(Color::Aqua, "hook_luaL_register: luaL_Reg[%d] name=[%s] func=[%p]", i++, ptr->name, (void*)ptr->func);

ptr++;
}

return ((pluaL_register)*hck_luaL_register)(L, libname, l);
}

int hook_luaL_loadstring(lua_State* L, const char* s) {
Console::log(Color::Aqua, "hook_luaL_loadstring: s=[ ... ]");

std::map<std::string, std::any> fields = {
{"s", &s}
};

std::string input(s);

if (LuaHook::runLuaHook("luaL_loadstring", &input, fields)) {
Console::log(Color::Green, "Set contents to:\n%s", input.c_str());
return ((pluaL_loadstring)*hck_luaL_loadstring)(L, input.c_str());
}

return ((pluaL_loadstring)*hck_luaL_loadstring)(L, s);
}

lua_State* hook_lua_newstate(lua_Alloc f, void* ud) {
Console::log(Color::Aqua, "hck_lua_newstate: ud=[%p]", ud);
return ((plua_newstate)*hck_lua_newstate)(f, ud);
}

int hook_luaL_loadbuffer(lua_State* L, const char* buff, size_t sz, const char* name) {
Console::log(Color::Aqua, "hck_luaL_loadbuffer: buff=[ ... ], sz=[%zu], name=[%s]", sz, name);

std::map<std::string, std::any> fields = {
{"buff", &buff},
{"name", &name}
};

std::string input(buff, sz);

if (size_t executeCount = LuaHook::runLuaHook("luaL_loadbuffer", &input, fields)) {
Console::log(Color::Green, "Set contents to:\n%s", input.c_str());
return ((pluaL_loadbuffer)*hck_luaL_loadbuffer)(L, input.c_str(), input.size(), name);
}

return ((pluaL_loadbuffer)*hck_luaL_loadbuffer)(L, buff, sz, name);
}
}
Loading

0 comments on commit f18a317

Please sign in to comment.