├── .gitignore ├── README.md ├── Script Engine.sln ├── Script Engine ├── CVarSystem.cpp ├── CVarSystem.h ├── CmdLineInterface.cpp ├── CmdLineInterface.h ├── Lua │ ├── Callback.cpp │ ├── Callback.h │ ├── LuaAPI.cpp │ ├── LuaAPI.h │ ├── LuaInterface.cpp │ └── LuaInterface.h ├── Main.cpp ├── RemoteProcess.cpp ├── RemoteProcess.h ├── Script Engine.vcxproj ├── Script Engine.vcxproj.filters ├── UserAPI.cpp ├── UserAPI.h └── stdafx.h ├── example.lua └── lua531_windows_files.rar /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | 19 | # Compiled Static libraries 20 | *.lai 21 | *.la 22 | *.a 23 | *.lib 24 | 25 | # Executables 26 | *.exe 27 | *.out 28 | *.app 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # How2Start: 2 | via command line: executablename -process "targetprocess.exe" -file "example.lua" 3 | 4 | 5 | # API: CVarSystem 6 | + bool CVar.RegisterCVar( Name, value ) 7 | + bool CVar.GetBool( Name ) 8 | + float CVar.GetFloat( Name ) 9 | + int CVar.GetInt( Name ) 10 | + bool CVar.SetValue( Name, value as bool or number ) 11 | 12 | 13 | # API: File 14 | + bool File.GetBool( AppName, KeyName, File, optional default_value ) 15 | + float File.GetFloat( AppName, KeyName, File, optional default_value ) 16 | + int File.GetInt( AppName, KeyName, File, optional default_value ) 17 | + bool File.WriteBool( AppName, KeyName, File, value ) 18 | + bool File.WriteFloat( AppName, KeyName, File, value ) 19 | + bool File.WriteInt( AppName, KeyName, File, value ) 20 | 21 | 22 | # API: RemoteProcess 23 | - bool Process.RegisterModule( ModuleName ) 24 | + int Process.FindSignature( ModuleName, Signature, ExtraOffset ) 25 | + int, int Process.GetModuleData( ModuleName ) 26 | + bool Process.SendMessage( Msg, WPARAM, LPARAM ) 27 | + bool Process.ReadBool( Address ) 28 | + int Process.ReadByte( Address ) 29 | + float Process.ReadFloat( Address ) 30 | + int Process.ReadInteger( Address ) 31 | + bool Process.WriteBool( Address, value ) 32 | + bool Process.WriteByte( Address, value ) 33 | + bool Process.WriteFloat( Address, value ) 34 | + bool Process.WriteInt( Address, value ) 35 | -------------------------------------------------------------------------------- /Script Engine.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.31101.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Script Engine", "Script Engine\Script Engine.vcxproj", "{33025A3F-957D-4F25-BA3C-B4E950AF42B4}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Release|Win32 = Release|Win32 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {33025A3F-957D-4F25-BA3C-B4E950AF42B4}.Debug|Win32.ActiveCfg = Debug|Win32 15 | {33025A3F-957D-4F25-BA3C-B4E950AF42B4}.Debug|Win32.Build.0 = Debug|Win32 16 | {33025A3F-957D-4F25-BA3C-B4E950AF42B4}.Release|Win32.ActiveCfg = Release|Win32 17 | {33025A3F-957D-4F25-BA3C-B4E950AF42B4}.Release|Win32.Build.0 = Release|Win32 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /Script Engine/CVarSystem.cpp: -------------------------------------------------------------------------------- 1 | #include "CVarSystem.h" 2 | 3 | std::size_t CVarSystem::GetCVarIndex( std::string strCVarName ) 4 | { 5 | if( strCVarName.empty( ) ) 6 | { 7 | ENGINE_MSG( "CVarSystem::GetCVarIndex: empty CVar name!" ); 8 | return -1; 9 | } 10 | 11 | for( std::size_t i = 0; i < this->m_vstrCVarName.size( ); i++ ) 12 | if( !this->m_vstrCVarName.at( i ).compare( strCVarName ) ) 13 | return i; 14 | 15 | return -1; 16 | } 17 | 18 | CVarSystem::CVarSystem( void ) 19 | { 20 | 21 | } 22 | 23 | CVarSystem::~CVarSystem( void ) 24 | { 25 | this->Release( ); 26 | } 27 | 28 | bool CVarSystem::GetBool( std::string strCVarName ) 29 | { 30 | return ( this->GetFloat( strCVarName ) >= 1.f ); 31 | } 32 | 33 | float CVarSystem::GetFloat( std::string strCVarName ) 34 | { 35 | auto nIndex = this->GetCVarIndex( strCVarName ); 36 | if( nIndex == -1 ) 37 | { 38 | ENGINE_MSG( "CVarSystem::Get: %s isn't registered!" ); 39 | return 0.f; 40 | } 41 | 42 | return this->m_vflValue.at( nIndex ); 43 | } 44 | 45 | int CVarSystem::GetInt( std::string strCVarName ) 46 | { 47 | return ( int )this->GetFloat( strCVarName ); 48 | } 49 | 50 | void CVarSystem::RegisterCVar( std::string strCVarName, bool bActive ) 51 | { 52 | this->RegisterCVar( strCVarName, bActive ? 1.f : 0.f ); 53 | } 54 | 55 | void CVarSystem::RegisterCVar( std::string strCVarName, float flValue ) 56 | { 57 | if( strCVarName.empty( ) ) 58 | { 59 | ENGINE_MSG( "CVarSystem::AddCVar: empty CVar name!" ); 60 | return; 61 | } 62 | 63 | this->m_vflValue.push_back( flValue ); 64 | this->m_vstrCVarName.push_back( strCVarName ); 65 | } 66 | 67 | void CVarSystem::RegisterCVar( std::string strCVarName, int iValue ) 68 | { 69 | this->RegisterCVar( strCVarName, ( float )iValue ); 70 | } 71 | 72 | void CVarSystem::SetValue( std::string strCVarName, bool bValue ) 73 | { 74 | this->SetValue( strCVarName, bValue ? 1.f : 0.f ); 75 | } 76 | 77 | void CVarSystem::SetValue( std::string strCVarName, float flValue ) 78 | { 79 | auto nIndex = this->GetCVarIndex( strCVarName ); 80 | if( nIndex == -1 ) 81 | return; 82 | 83 | this->m_vflValue.at( nIndex ) = flValue; 84 | } 85 | 86 | void CVarSystem::SetValue( std::string strCVarName, int iValue ) 87 | { 88 | this->SetValue( strCVarName, ( float )iValue ); 89 | } 90 | 91 | void CVarSystem::Release( void ) 92 | { 93 | if( !this->m_vflValue.empty( ) ) 94 | this->m_vflValue.clear( ); 95 | 96 | if( !this->m_vstrCVarName.empty( ) ) 97 | this->m_vstrCVarName.clear( ); 98 | } 99 | 100 | CVarSystem* g_pCVarManager = nullptr; -------------------------------------------------------------------------------- /Script Engine/CVarSystem.h: -------------------------------------------------------------------------------- 1 | #ifndef __CVARSYSTEM_H__ 2 | #define __CVARSYSTEM_H__ 3 | 4 | #ifdef _MSC_VER 5 | #pragma once 6 | #endif 7 | 8 | #include "stdafx.h" 9 | 10 | class CVarSystem 11 | { 12 | private: 13 | std::vector< std::string > m_vstrCVarName; 14 | std::vector< float > m_vflValue; 15 | 16 | private: 17 | std::size_t GetCVarIndex( std::string strCVarName ); 18 | 19 | public: 20 | CVarSystem( void ); 21 | ~CVarSystem( void ); 22 | bool GetBool( std::string strCVarName ); 23 | float GetFloat( std::string strCVarName ); 24 | int GetInt( std::string strCVarName ); 25 | void RegisterCVar( std::string strCVarName, bool bActive ); 26 | void RegisterCVar( std::string strCVarName, float flValue ); 27 | void RegisterCVar( std::string strCVarName, int iValue ); 28 | void SetValue( std::string strCVarName, bool bValue ); 29 | void SetValue( std::string strCVarName, float flValue ); 30 | void SetValue( std::string strCVarName, int iValue ); 31 | void Release( void ); 32 | }; 33 | 34 | extern CVarSystem* g_pCVarManager; 35 | 36 | #endif -------------------------------------------------------------------------------- /Script Engine/CmdLineInterface.cpp: -------------------------------------------------------------------------------- 1 | #include "CmdLineInterface.h" 2 | 3 | std::size_t CmdLineInterface::GetCommandIndex( std::string strCommand ) 4 | { 5 | if( strCommand.empty( ) ) 6 | { 7 | ENGINE_MSG( "CmdLineInterface::GetCommandIndex: empty command!" ); 8 | return -1; 9 | } 10 | 11 | for( std::size_t i = 0; i < this->m_vCommands.size( ); i++ ) 12 | if( !this->m_vCommands[ i ].m_strCommand.compare( strCommand ) ) 13 | return i; 14 | 15 | return -1; 16 | } 17 | 18 | CmdLineInterface::CmdLineInterface( void ) 19 | { 20 | 21 | } 22 | 23 | CmdLineInterface::~CmdLineInterface( void ) 24 | { 25 | this->Release( ); 26 | } 27 | 28 | bool CmdLineInterface::GetCommand( std::string strCommand, CmdCommand_t *pCommand ) 29 | { 30 | if( strCommand.empty( ) ) 31 | { 32 | ENGINE_MSG( "CmdLineInterface::GetCommand: empty command!" ); 33 | return false; 34 | } 35 | 36 | auto nIndex = this->GetCommandIndex( strCommand ); 37 | if( nIndex == -1 ) 38 | { 39 | ENGINE_MSG( "CmdLineInterface::GetCommand: %s isn't registered!", strCommand.c_str( ) ); 40 | return false; 41 | } 42 | 43 | if( pCommand ) 44 | *pCommand = this->m_vCommands[ nIndex ]; 45 | 46 | return true; 47 | } 48 | 49 | bool CmdLineInterface::GetCommandsFromCmdLine( int iArgC, char *pszArg[] ) 50 | { 51 | std::vector< std::string > vstrCommandInput; 52 | for( int i = 1; i < iArgC; i++ ) 53 | vstrCommandInput.push_back( pszArg[ i ] ); 54 | 55 | if( vstrCommandInput.size( ) / 2 != this->m_vCommands.size( ) ) 56 | { 57 | ENGINE_MSG( "CmdLineInterface: missing arguemts or parameters!" ); 58 | return false; 59 | } 60 | 61 | for( std::size_t i = 0; i < vstrCommandInput.size( ); i += 2 ) 62 | if( this->is_command( vstrCommandInput[ i ] ) ) 63 | this->SetCommandValue( vstrCommandInput[ i ], vstrCommandInput[ i + 1 ] ); 64 | 65 | return true; 66 | } 67 | 68 | bool CmdLineInterface::is_command( std::string strCommand ) 69 | { 70 | if( strCommand.empty( ) ) 71 | { 72 | ENGINE_MSG( "CmdLineInterface::is_command: empty command!" ); 73 | return false; 74 | } 75 | 76 | return( this->GetCommand( strCommand, nullptr ) ); 77 | } 78 | 79 | void CmdLineInterface::SetCommandValue( std::string strCommand, std::string strValue ) 80 | { 81 | if( strCommand.empty( ) ) 82 | { 83 | ENGINE_MSG( "CmdLineInterface::SetCommandValue: empty command!" ); 84 | return; 85 | } 86 | 87 | auto nIndex = this->GetCommandIndex( strCommand ); 88 | if( nIndex == -1 ) 89 | { 90 | ENGINE_MSG( "CmdLineInterface::SetCommand: %s isn't registered!", strCommand.c_str( ) ); 91 | return; 92 | } 93 | 94 | this->m_vCommands[ nIndex ].m_strValue = strValue; 95 | } 96 | 97 | void CmdLineInterface::RegisterCommand( std::string strCommand ) 98 | { 99 | if( strCommand.empty( ) ) 100 | { 101 | ENGINE_MSG( "CmdLineInterface::RegisterCommand: empty command!" ); 102 | return; 103 | } 104 | 105 | if( this->GetCommandIndex( strCommand ) != -1 ) 106 | { 107 | ENGINE_MSG( "CmdLineInterface:RegisterCommand: %s is already registered!", strCommand.c_str( ) ); 108 | return; 109 | } 110 | 111 | this->m_vCommands.push_back( { strCommand, "" } ); 112 | } 113 | 114 | void CmdLineInterface::Release( void ) 115 | { 116 | this->m_vCommands.clear( ); 117 | } 118 | 119 | CmdLineInterface *g_pCmdLineInterface = nullptr; -------------------------------------------------------------------------------- /Script Engine/CmdLineInterface.h: -------------------------------------------------------------------------------- 1 | #ifndef __CMDLINEINTERFACE_H__ 2 | #define __CMDLINEINTERFACE_H__ 3 | 4 | #ifdef _MSC_VER 5 | #pragma once 6 | #endif 7 | 8 | #include "stdafx.h" 9 | 10 | typedef struct 11 | { 12 | std::string m_strCommand; 13 | std::string m_strValue; 14 | }CmdCommand_t; 15 | 16 | class CmdLineInterface 17 | { 18 | private: 19 | std::vector< CmdCommand_t > m_vCommands; 20 | 21 | private: 22 | std::size_t GetCommandIndex( std::string strCommand ); 23 | 24 | public: 25 | CmdLineInterface( void ); 26 | ~CmdLineInterface( void ); 27 | bool GetCommand( std::string strCommand, CmdCommand_t *pCommand ); 28 | bool GetCommandsFromCmdLine( int iArgC, char *pszArg[] ); 29 | bool is_command( std::string strCommand ); 30 | void SetCommandValue( std::string strCommand, std::string strValue ); 31 | void RegisterCommand( std::string strCommand ); 32 | void Release( void ); 33 | 34 | public: 35 | 36 | }; 37 | 38 | extern CmdLineInterface *g_pCmdLineInterface; 39 | 40 | #endif -------------------------------------------------------------------------------- /Script Engine/Lua/Callback.cpp: -------------------------------------------------------------------------------- 1 | #include "Callback.h" 2 | 3 | using namespace ScriptEngine; 4 | 5 | std::size_t LuaCallback::GetCallbackFunctionIndex( std::string strFunctionName ) 6 | { 7 | if( strFunctionName.empty( ) ) 8 | { 9 | ENGINE_MSG( "GetCallbackFunctionIndex: empty function name!" ); 10 | return -1; 11 | } 12 | 13 | for( std::size_t i = 0; i < this->m_vstrCallbackFunctions.size( ); i++ ) 14 | if( !this->m_vstrCallbackFunctions[ i ].compare( strFunctionName ) ) 15 | return i; 16 | 17 | return -1; 18 | } 19 | 20 | LuaCallback::LuaCallback( void ) 21 | { 22 | 23 | } 24 | 25 | LuaCallback::~LuaCallback( void ) 26 | { 27 | 28 | } 29 | 30 | void LuaCallback::RegisterCallbackFunction( std::string strFunctionName ) 31 | { 32 | if( strFunctionName.empty( ) ) 33 | { 34 | ENGINE_MSG( "RegisterCallbackFunction: empty function name!" ); 35 | return; 36 | } 37 | 38 | if( this->GetCallbackFunctionIndex( strFunctionName ) != -1 ) 39 | { 40 | ENGINE_MSG( "RegisterCallbackFunction: %s is allready registered!", strFunctionName.c_str( ) ); 41 | return; 42 | } 43 | 44 | ENGINE_MSG( "RegisterCallbackFunction: registered %s!", strFunctionName.c_str( ) ); 45 | this->m_vstrCallbackFunctions.push_back( strFunctionName ); 46 | } 47 | 48 | void LuaCallback::UnregisterCallbackFunction( std::string strFunctionName ) 49 | { 50 | if( strFunctionName.empty( ) ) 51 | { 52 | ENGINE_MSG( "UnregisterCallbackFunction: empty function name!" ); 53 | return; 54 | } 55 | auto nIndex = this->GetCallbackFunctionIndex( strFunctionName ); 56 | if( nIndex != -1 ) 57 | { 58 | this->m_vstrCallbackFunctions.erase( this->m_vstrCallbackFunctions.begin( ) + nIndex ); 59 | ENGINE_MSG( "UnregisterCallbackFunction: unregistered function: %s!", strFunctionName.c_str( ) ); 60 | } 61 | } 62 | 63 | void LuaCallback::CallRegisteredFunctions( lua_State *pLua ) 64 | { 65 | if( !pLua ) 66 | { 67 | ENGINE_MSG( "CallRegisteredFunctions: invalid lua interface pointer!" ); 68 | return; 69 | } 70 | 71 | for( std::size_t i = 0; i < this->m_vstrCallbackFunctions.size( ); i++ ) 72 | { 73 | // Skip all invalid callback functions 74 | for( std::size_t j = 0; j < this->m_vnCallbackErrorFunctions.size( ); j++ ) 75 | if( i == this->m_vnCallbackErrorFunctions[ j ] ) 76 | continue; 77 | 78 | auto c = this->m_vstrCallbackFunctions[ i ].c_str( ); 79 | lua_getglobal( pLua, c ); 80 | if( lua_pcall( pLua, 0, 1, 0 ) ) 81 | { 82 | this->m_vnCallbackErrorFunctions.push_back( i ); 83 | ENGINE_MSG( "CallRegisteredFunctions: %s isn't registered!", c ); 84 | } 85 | } 86 | } 87 | 88 | ScriptEngine::LuaCallback *g_pLuaCallback = nullptr; -------------------------------------------------------------------------------- /Script Engine/Lua/Callback.h: -------------------------------------------------------------------------------- 1 | #ifndef __LUA_CALLBACK_H__ 2 | #define __LUA_CALLBACK_H__ 3 | 4 | #ifdef _MSC_VER 5 | #pragma once 6 | #endif 7 | 8 | #include "..\stdafx.h" 9 | 10 | namespace ScriptEngine 11 | { 12 | class LuaCallback 13 | { 14 | private: 15 | std::vector< std::size_t > m_vnCallbackErrorFunctions; 16 | std::vector< std::string > m_vstrCallbackFunctions; 17 | 18 | private: 19 | std::size_t GetCallbackFunctionIndex( std::string strFunctionName ); 20 | public: 21 | LuaCallback( void ); 22 | ~LuaCallback( void ); 23 | void RegisterCallbackFunction( std::string strFunctionName ); 24 | void UnregisterCallbackFunction( std::string strFunctionName ); 25 | void CallRegisteredFunctions( lua_State *pLua ); 26 | }; 27 | } 28 | 29 | extern ScriptEngine::LuaCallback *g_pLuaCallback; 30 | 31 | #endif -------------------------------------------------------------------------------- /Script Engine/Lua/LuaAPI.cpp: -------------------------------------------------------------------------------- 1 | #include "LuaAPI.h" 2 | #include "LuaInterface.h" 3 | 4 | namespace ScriptEngine 5 | { 6 | static bool lua_apicheck_args( lua_State *pLua, int iArgMin, int iArgMax, std::string strFunctionName ) 7 | { 8 | auto iNumberOfArgs = lua_gettop( pLua ); 9 | if( iNumberOfArgs > iArgMax || iNumberOfArgs < iArgMin ) 10 | { 11 | ENGINE_MSG( "%s: %s arguments(%d/%d)!\n", strFunctionName.c_str( ), ( iNumberOfArgs > 2 ) ? "to many" : "missing", iNumberOfArgs, iArgMax ); 12 | return false; 13 | } 14 | 15 | return true; 16 | } 17 | static int lua_api_bitband( lua_State *pLua ) 18 | { 19 | if( !lua_apicheck_args( pLua, 2, 2, "BitBand" ) ) 20 | return lua_ret_int( pLua, 0 ); 21 | 22 | if( !lua_isinteger( pLua, 1 ) || !lua_isinteger( pLua, 2 ) ) 23 | { 24 | ENGINE_MSG( "BitBand: one or more argument isn't an integer!" ); 25 | return lua_ret_int( pLua, 0 ); 26 | } 27 | 28 | return lua_ret_int( pLua, lua_to_int( pLua, 1 ) & lua_to_int( pLua, 2 ) ); 29 | } 30 | static int lua_api_getkeystate( lua_State *pLua ) 31 | { 32 | if( !lua_apicheck_args( pLua, 1, 1, "GetKeyState" ) ) 33 | return lua_ret_int( pLua, 0 ); 34 | 35 | if( !lua_is_digit( pLua, 1 ) ) 36 | { 37 | ENGINE_MSG( "GetKeyState: argument isn't digit!" ); 38 | return lua_ret_int( pLua, 0 ); 39 | } 40 | 41 | return lua_ret_int( pLua, GetKeyState( lua_to_int( pLua, 1 ) ) ); 42 | } 43 | static int lua_api_include( lua_State *pLua ) 44 | { 45 | if( !lua_apicheck_args( pLua, 1, 1, "Include" ) ) 46 | return 0; 47 | 48 | if( !lua_isstring( pLua, 1 ) ) 49 | { 50 | ENGINE_MSG( "Include: argument isn't a string!" ); 51 | return 0; 52 | } 53 | 54 | if( luaL_loadfile( pLua, GetApplicationDirectory( lua_to_string( pLua, 1 ) ).c_str( ) ) || lua_pcall( pLua, 0, 0, 0 ) ) 55 | ENGINE_MSG( "Include: error executing: %s!", luaL_checkstring( pLua, -1 ) ); 56 | 57 | return 0; 58 | } 59 | static int lua_api_sleep( lua_State *pLua ) 60 | { 61 | if( !lua_apicheck_args( pLua, 1, 1, "Sleep" ) ) 62 | return 0; 63 | 64 | if( !lua_is_digit( pLua, 1 ) ) 65 | { 66 | ENGINE_MSG( "Sleep: argument isn't digit!" ); 67 | return 0; 68 | } 69 | 70 | Sleep( ( DWORD )lua_to_int( pLua, 1 ) ); 71 | return 0; 72 | } 73 | static int lua_api_callback_registercallbackfunction( lua_State *pLua ) 74 | { 75 | if( !lua_apicheck_args( pLua, 1, 1, "LuaCallback::RegisterCallbackFunction" ) ) 76 | return lua_ret_bool( pLua ); 77 | 78 | if( !lua_isstring( pLua, 1 ) ) 79 | { 80 | ENGINE_MSG( "LuaCallback::RegisterCallbackFunction: argument isn't a string!" ); 81 | return lua_ret_bool( pLua ); 82 | } 83 | 84 | g_pLuaCallback->RegisterCallbackFunction( lua_to_string( pLua, 1 ) ); 85 | return lua_ret_bool( pLua, true ); 86 | } 87 | static int lua_api_cvar_registercvar( lua_State *pLua ) 88 | { 89 | if( !lua_apicheck_args( pLua, 2, 2, "CVarSystem::RegisterCVar" ) ) 90 | return lua_ret_bool( pLua ); 91 | 92 | if( !lua_isstring( pLua, 1 ) ) 93 | { 94 | ENGINE_MSG( "CVarSystem::RegisterCVar: argument 1 isn't a string!" ); 95 | return lua_ret_bool( pLua ); 96 | } 97 | 98 | auto bDigit = lua_is_digit( pLua, 2 ); 99 | auto bBoolean = ( lua_isboolean( pLua, 2 ) == 1 ); 100 | 101 | if( !bDigit && !bBoolean ) 102 | { 103 | ENGINE_MSG( "CVarSystem::RegisterCVar: argument 2 isn't a boolean or digit number!" ); 104 | return lua_ret_bool( pLua ); 105 | } 106 | 107 | auto flValue = 0.f; 108 | if( bDigit ) 109 | flValue = ( float )lua_to_number( pLua, 2 ); 110 | else flValue = ( lua_to_bool( pLua, 2 ) ? 1.f : 0.f ); 111 | 112 | g_pCVarManager->RegisterCVar( lua_to_string( pLua, 1 ), flValue ); 113 | return lua_ret_bool( pLua, true ); 114 | } 115 | static int lua_api_cvar_getbool( lua_State *pLua ) 116 | { 117 | if( !lua_apicheck_args( pLua, 1, 1, "CVarSystem::GetBool" ) ) 118 | return lua_ret_bool( pLua ); 119 | 120 | if( !lua_isstring( pLua, 1 ) ) 121 | { 122 | ENGINE_MSG( "CVarSystem::GetBool: argument isn't a string!" ); 123 | return lua_ret_bool( pLua ); 124 | } 125 | 126 | return lua_ret_bool( pLua, g_pCVarManager->GetBool( lua_to_string( pLua, 1 ) ) ); 127 | } 128 | static int lua_api_cvar_getfloat( lua_State *pLua ) 129 | { 130 | if( !lua_apicheck_args( pLua, 1, 1, "CVarSystem::GetFloat" ) ) 131 | return lua_ret_number( pLua ); 132 | 133 | if( !lua_isstring( pLua, 1 ) ) 134 | { 135 | ENGINE_MSG( "CVarSystem::GetFloat: argument isn't a string!" ); 136 | return lua_ret_number( pLua ); 137 | } 138 | 139 | return lua_ret_number( pLua, ( double )g_pCVarManager->GetFloat( lua_to_string( pLua, 1 ) ) ); 140 | } 141 | static int lua_api_cvar_getint( lua_State *pLua ) 142 | { 143 | if( !lua_apicheck_args( pLua, 1, 1, "CVarSystem::GetInt" ) ) 144 | return lua_ret_int( pLua ); 145 | 146 | if( !lua_isstring( pLua, 1 ) ) 147 | { 148 | ENGINE_MSG( "CVarSystem::GetInt: argument isn't a string!" ); 149 | return lua_ret_int( pLua ); 150 | } 151 | 152 | return lua_ret_int( pLua, g_pCVarManager->GetInt( lua_to_string( pLua, 1 ) ) ); 153 | } 154 | static int lua_api_cvar_setvalue( lua_State *pLua ) 155 | { 156 | if( !lua_apicheck_args( pLua, 2, 2, "CVarSystem::SetValue" ) ) 157 | return lua_ret_bool( pLua ); 158 | 159 | if( !lua_isstring( pLua, 1 ) ) 160 | { 161 | ENGINE_MSG( "CVarSystem::RegisterCVar: argument 1 isn't a string!" ); 162 | return lua_ret_bool( pLua ); 163 | } 164 | 165 | auto bDigit = lua_is_digit( pLua, 2 ); 166 | auto bBoolean = ( lua_isboolean( pLua, 2 ) == 1 ); 167 | 168 | if( !bDigit && !bBoolean ) 169 | { 170 | ENGINE_MSG( "CVarSystem::RegisterCVar: argument 2 isn't a boolean or digit number!" ); 171 | return lua_ret_bool( pLua ); 172 | } 173 | 174 | auto flValue = 0.f; 175 | if( bDigit ) 176 | flValue = ( float )lua_to_number( pLua, 2 ); 177 | else flValue = ( lua_to_bool( pLua, 2 ) ? 1.f : 0.f ); 178 | 179 | g_pCVarManager->SetValue( lua_to_string( pLua, 1 ), flValue ); 180 | return lua_ret_bool( pLua, true ); 181 | } 182 | static int lua_api_file_readbool( lua_State *pLua ) 183 | { 184 | if( !lua_apicheck_args( pLua, 3, 4, "GetBoolFromFile" ) ) 185 | return lua_ret_bool( pLua ); 186 | 187 | auto bDefaultValue = false; 188 | if( lua_gettop( pLua ) == 4 && lua_is_digit( pLua, 4 ) || lua_isboolean( pLua, 4 ) ) 189 | bDefaultValue = lua_to_bool( pLua, 4 ); 190 | 191 | auto bSucceeded = true; 192 | for( int i = 1; i <= 3; i++ ) 193 | { 194 | if( !lua_isstring( pLua, i ) ) 195 | { 196 | ENGINE_MSG( "GetBoolFromFile: argument %d isn't a string!", i ); 197 | bSucceeded = false; 198 | } 199 | } 200 | 201 | if( !bSucceeded ) 202 | return lua_ret_bool( pLua, bDefaultValue ); 203 | 204 | return lua_ret_bool( pLua, GetBoolFromFile( lua_to_string( pLua, 1 ), lua_to_string( pLua, 2 ), GetApplicationDirectory( lua_to_string( pLua, 3 ) ), bDefaultValue ) ); 205 | } 206 | static int lua_api_file_readfloat( lua_State *pLua ) 207 | { 208 | if( !lua_apicheck_args( pLua, 3, 4, "GetFloatFromFile" ) ) 209 | return lua_ret_number( pLua ); 210 | 211 | auto DefaultValue = 0.0; 212 | if( lua_gettop( pLua ) == 4 && lua_is_digit( pLua, 4 ) ) 213 | DefaultValue = lua_to_number( pLua, 4 ); 214 | 215 | auto bSucceeded = true; 216 | for( int i = 1; i <= 3; i++ ) 217 | { 218 | if( !lua_isstring( pLua, i ) ) 219 | { 220 | ENGINE_MSG( "GetFloatFromFile: argument %d isn't a string!", i ); 221 | bSucceeded = false; 222 | } 223 | } 224 | 225 | if( !bSucceeded ) 226 | return lua_ret_number( pLua, DefaultValue ); 227 | 228 | return lua_ret_number( pLua, ( double )GetFloatFromFile( lua_to_string( pLua, 1 ), lua_to_string( pLua, 2 ), GetApplicationDirectory( lua_to_string( pLua, 3 ) ), ( float )DefaultValue ) ); 229 | } 230 | static int lua_api_file_readint( lua_State *pLua ) 231 | { 232 | if( !lua_apicheck_args( pLua, 3, 4, "GetIntFromFile" ) ) 233 | return lua_ret_int( pLua ); 234 | 235 | auto iDefaultValue = 0; 236 | if( lua_gettop( pLua ) == 4 && lua_is_digit( pLua, 4 ) ) 237 | iDefaultValue = lua_to_int( pLua, 4 ); 238 | 239 | auto bSucceeded = true; 240 | for( int i = 1; i <= 3; i++ ) 241 | { 242 | if( !lua_isstring( pLua, i ) ) 243 | { 244 | ENGINE_MSG( "GetIntFromFile: argument %d isn't a string!", i ); 245 | bSucceeded = false; 246 | } 247 | } 248 | 249 | if( !bSucceeded ) 250 | return lua_ret_int( pLua, iDefaultValue ); 251 | 252 | return lua_ret_int( pLua, GetIntFromFile( lua_to_string( pLua, 1 ), lua_to_string( pLua, 2 ), GetApplicationDirectory( lua_to_string( pLua, 3 ) ), iDefaultValue ) ); 253 | } 254 | static int lua_api_file_writebool( lua_State *pLua ) 255 | { 256 | if( !lua_apicheck_args( pLua, 4, 4, "WriteBoolToFile" ) ) 257 | return lua_ret_bool( pLua ); 258 | 259 | auto bSucceeded = true; 260 | for( int i = 1; i <= 3; i++ ) 261 | { 262 | if( !lua_isstring( pLua, i ) ) 263 | { 264 | ENGINE_MSG( "WriteBoolToFile: argument %d isn't a string!", i ); 265 | bSucceeded = false; 266 | } 267 | } 268 | 269 | if( !lua_is_digit( pLua, 4 ) && !lua_isboolean( pLua, 4 ) ) 270 | { 271 | ENGINE_MSG( "WriteBoolToFile: argument 4 isn't a boolean!" ); 272 | return lua_ret_bool( pLua ); 273 | } 274 | 275 | WriteBoolToFile( lua_to_string( pLua, 1 ), lua_to_string( pLua, 2 ), GetApplicationDirectory( lua_to_string( pLua, 3 ) ), lua_to_bool( pLua, 4 ) ); 276 | return lua_ret_bool( pLua, true ); 277 | } 278 | static int lua_api_file_writefloat( lua_State *pLua ) 279 | { 280 | if( !lua_apicheck_args( pLua, 4, 4, "WriteFloatToFile" ) ) 281 | return lua_ret_bool( pLua ); 282 | 283 | auto bSucceeded = true; 284 | for( int i = 1; i <= 3; i++ ) 285 | { 286 | if( !lua_isstring( pLua, i ) ) 287 | { 288 | ENGINE_MSG( "WriteFloatToFile: argument %d isn't a string!", i ); 289 | bSucceeded = false; 290 | } 291 | } 292 | 293 | if( !lua_is_digit( pLua, 4 ) && !lua_isboolean( pLua, 4 ) ) 294 | { 295 | ENGINE_MSG( "WriteFloatToFile: argument 4 isn't a number!" ); 296 | return lua_ret_bool( pLua ); 297 | } 298 | 299 | WriteFloatToFile( lua_to_string( pLua, 1 ), lua_to_string( pLua, 2 ), GetApplicationDirectory( lua_to_string( pLua, 3 ) ), ( float )lua_to_number( pLua, 4 ) ); 300 | return lua_ret_bool( pLua, true ); 301 | } 302 | static int lua_api_file_writeint( lua_State *pLua ) 303 | { 304 | if( !lua_apicheck_args( pLua, 4, 4, "WriteIntToFile" ) ) 305 | return lua_ret_bool( pLua ); 306 | 307 | auto bSucceeded = true; 308 | for( int i = 1; i <= 3; i++ ) 309 | { 310 | if( !lua_isstring( pLua, i ) ) 311 | { 312 | ENGINE_MSG( "WriteIntToFile: argument %d isn't a string!", i ); 313 | bSucceeded = false; 314 | } 315 | } 316 | 317 | if( !lua_is_digit( pLua, 4 ) && !lua_isboolean( pLua, 4 ) ) 318 | { 319 | ENGINE_MSG( "WriteIntToFile: argument 4 isn't a number!" ); 320 | return lua_ret_bool( pLua ); 321 | } 322 | 323 | WriteIntToFile( lua_to_string( pLua, 1 ), lua_to_string( pLua, 2 ), GetApplicationDirectory( lua_to_string( pLua, 3 ) ), lua_to_int( pLua, 4 ) ); 324 | return lua_ret_bool( pLua, true ); 325 | } 326 | static int lua_api_remoteprocess_registermodule( lua_State *pLua ) 327 | { 328 | if( !lua_apicheck_args( pLua, 1, 1, "RemoteProcess::RegisterModule" ) ) 329 | return lua_ret_bool( pLua, false ); 330 | 331 | if( !lua_isstring( pLua, 1 ) ) 332 | { 333 | ENGINE_MSG( "RemoteProcess::RegisterModule: argument isn't a string!" ); 334 | return lua_ret_bool( pLua, false ); 335 | } 336 | 337 | return lua_ret_bool( pLua, g_pRemoteProcess->RegisterModule( lua_to_string( pLua, 1 ) ) ); 338 | } 339 | static int lua_api_remoteprocess_findsignature( lua_State *pLua ) 340 | { 341 | if( !lua_apicheck_args( pLua, 2, 3, "RemoteProcess::FindSignature" ) ) 342 | return lua_ret_int( pLua ); 343 | 344 | auto bSucceeded = true; 345 | for( int i = 1; i <= 2; i++ ) 346 | { 347 | if( !lua_isstring( pLua, i ) ) 348 | { 349 | ENGINE_MSG( "RemoteProcess::FindSignature: argument %d isn't a string!", i ); 350 | bSucceeded = false; 351 | } 352 | } 353 | 354 | if( !bSucceeded ) 355 | return lua_ret_int( pLua ); 356 | 357 | DWORD_PTR dwExtraOffset = 0; 358 | if( lua_gettop( pLua ) == 3 ) 359 | { 360 | if( !lua_isinteger( pLua, 3 ) ) 361 | { 362 | ENGINE_MSG( "RemoteProcess::FindSignature: argument 3 isn't an integer!" ); 363 | lua_ret_int( pLua ); 364 | } 365 | 366 | dwExtraOffset = ( DWORD_PTR )lua_to_int( pLua, 3 ); 367 | } 368 | 369 | 370 | return lua_ret_int( pLua, ( int )g_pRemoteProcess->FindSignature( lua_to_string( pLua, 1 ), lua_to_string( pLua, 2 ), dwExtraOffset ) ); 371 | } 372 | static int lua_api_remoteprocess_getmoduledata( lua_State *pLua ) 373 | { 374 | if( !lua_apicheck_args( pLua, 1, 1, "RemoteProcess::GetModuleData" ) ) 375 | return lua_ret_int( pLua ); 376 | 377 | if( !lua_isstring( pLua, 1 ) ) 378 | { 379 | ENGINE_MSG( "RemoteProcess::RegisterModule: argument isn't a string!" ); 380 | return lua_ret_bool( pLua, false ); 381 | } 382 | 383 | auto ModuleData = g_pRemoteProcess->GetModuleData( lua_to_string( pLua, 1 ) ); 384 | lua_push_int( pLua, ( int )ModuleData.modBaseAddr ); 385 | lua_push_int( pLua, ( int )ModuleData.modBaseSize ); 386 | return 2; 387 | } 388 | static int lua_api_remoteprocess_sendmessage( lua_State *pLua ) 389 | { 390 | if( !lua_apicheck_args( pLua, 3, 3, "RemoteProcess::SendMessage" ) ) 391 | lua_ret_bool( pLua ); 392 | 393 | auto bSucceeded = true; 394 | for( int i = 0; i < 3; i++ ) 395 | { 396 | if( !lua_isinteger( pLua, i ) ) 397 | { 398 | ENGINE_MSG( "RemoteProcess::SendMeesage: argument %s isn't an integer!", i ); 399 | bSucceeded = false; 400 | } 401 | } 402 | if( !bSucceeded ) 403 | return lua_ret_bool( pLua ); 404 | 405 | SendMessageA( g_pRemoteProcess->GetWindowHandle( ), ( UINT )lua_to_int( pLua, 1 ), ( WPARAM )lua_to_int( pLua, 2 ), ( LPARAM )lua_to_int( pLua, 3 ) ); 406 | return lua_ret_bool( pLua, true ); 407 | } 408 | static int lua_api_remoteprocess_readbool( lua_State *pLua ) 409 | { 410 | if( !lua_apicheck_args( pLua, 1, 1, "RemoteProcess::ReadBool" ) ) 411 | return lua_ret_bool( pLua, false ); 412 | 413 | if( !lua_isinteger( pLua, 1 ) ) 414 | { 415 | ENGINE_MSG( "RemoteProcess::ReadBool: argument isn't an integer!" ); 416 | return lua_ret_bool( pLua, false ); 417 | } 418 | 419 | return lua_ret_bool( pLua, g_pRemoteProcess->Read< bool >( ( DWORD_PTR )lua_to_int( pLua, 1 ) ) ); 420 | } 421 | static int lua_api_remoteprocess_readbyte( lua_State *pLua ) 422 | { 423 | if( !lua_apicheck_args( pLua, 1, 1, "RemoteProcess::ReadByte" ) ) 424 | return lua_ret_int( pLua, 0 ); 425 | 426 | if( !lua_isinteger( pLua, 1 ) ) 427 | { 428 | ENGINE_MSG( "RemoteProcess::ReadByte: argument isn't an integer!" ); 429 | return lua_ret_int( pLua, 0 ); 430 | } 431 | 432 | return lua_ret_int( pLua, ( int )g_pRemoteProcess->Read< BYTE >( ( DWORD_PTR )lua_to_int( pLua, 1 ) ) ); 433 | } 434 | static int lua_api_remoteprocess_readfloat( lua_State *pLua ) 435 | { 436 | if( !lua_apicheck_args( pLua, 1, 1, "RemoteProcess::ReadFloat" ) ) 437 | return lua_ret_number( pLua, 0.0 ); 438 | 439 | if( !lua_isinteger( pLua, 1 ) ) 440 | { 441 | ENGINE_MSG( "RemoteProcess::ReadFloat: argument isn't an integer!" ); 442 | return lua_ret_number( pLua, 0 ); 443 | } 444 | 445 | return lua_ret_number( pLua, ( double )g_pRemoteProcess->Read< float >( ( DWORD_PTR )lua_to_int( pLua, 1 ) ) ); 446 | } 447 | static int lua_api_remoteprocess_readinteger( lua_State *pLua ) 448 | { 449 | if( !lua_apicheck_args( pLua, 1, 1, "RemoteProcess::ReadInteger" ) ) 450 | return lua_ret_int( pLua, 0 ); 451 | 452 | if( !lua_isinteger( pLua, 1 ) ) 453 | { 454 | ENGINE_MSG( "RemoteProcess::ReadInteger: argument isn't an integer!" ); 455 | return lua_ret_int( pLua, 0 ); 456 | } 457 | 458 | return lua_ret_int( pLua, g_pRemoteProcess->Read< int >( ( DWORD_PTR )lua_to_int( pLua, 1 ) ) ); 459 | } 460 | static int lua_api_remoteprocess_readstring( lua_State *pLua ) 461 | { 462 | if( !lua_apicheck_args( pLua, 2, 2, "RemoteProcess::ReadString" ) ) 463 | return lua_ret_number( pLua, 0.0 ); 464 | 465 | if( !lua_isinteger( pLua, 1 ) || !lua_isinteger( pLua, 2 ) ) 466 | { 467 | ENGINE_MSG( "RemoteProcess::ReadString: one or more arguments aren't an integer!" ); 468 | return lua_ret_number( pLua, 0 ); 469 | } 470 | 471 | auto pszStringBuffer = new char[ lua_to_int( pLua, 2 ) ]; 472 | ReadProcessMemory( g_pRemoteProcess->GetProcessHandle( ), ( LPCVOID )( lua_to_int( pLua, 1 ) ), &pszStringBuffer, sizeof( pszStringBuffer ), NULL ); 473 | 474 | lua_push_string( pLua, pszStringBuffer ); 475 | delete[] pszStringBuffer; 476 | 477 | return 1; 478 | } 479 | static int lua_api_remoteprocess_writebool( lua_State *pLua ) 480 | { 481 | if( !lua_apicheck_args( pLua, 2, 2, "RemoteProcess::WriteBool" ) ) 482 | return lua_ret_bool( pLua ); 483 | 484 | if( !lua_isinteger( pLua, 1 ) ) 485 | { 486 | ENGINE_MSG( "RemoteProcess::WriteBool: argument 1 isn't an integer!" ); 487 | return lua_ret_bool( pLua ); 488 | } 489 | if( !lua_isboolean( pLua, 2 ) ) 490 | { 491 | ENGINE_MSG( "RemoteProcess::WriteBool: argument 2 isn't a boolean!" ); 492 | return lua_ret_bool( pLua ); 493 | } 494 | 495 | auto bRetVal = g_pRemoteProcess->Write< bool >( ( DWORD_PTR )lua_to_int( pLua, 1 ), lua_to_bool( pLua, 2 ) ); 496 | return lua_ret_bool( pLua, ( bRetVal == TRUE ? true : false ) ); 497 | } 498 | static int lua_api_remoteprocess_writebyte( lua_State *pLua ) 499 | { 500 | if( !lua_apicheck_args( pLua, 2, 2, "RemoteProcess::WriteByte" ) ) 501 | return lua_ret_bool( pLua ); 502 | 503 | if( !lua_isinteger( pLua, 1 ) ) 504 | { 505 | ENGINE_MSG( "RemoteProcess::WriteByte: argument 1 isn't an integer!" ); 506 | return lua_ret_bool( pLua ); 507 | } 508 | if( !lua_isinteger( pLua, 2 ) ) 509 | { 510 | ENGINE_MSG( "RemoteProcess::WriteByte: argument 2 isn't a byte!" ); 511 | return lua_ret_bool( pLua ); 512 | } 513 | 514 | auto bRetVal = g_pRemoteProcess->Write< BYTE >( ( DWORD_PTR )lua_to_int( pLua, 1 ), lua_to_bool( pLua, 2 ) ); 515 | return lua_ret_bool( pLua, ( bRetVal == TRUE ? true : false ) ); 516 | } 517 | static int lua_api_remoteprocess_writefloat( lua_State *pLua ) 518 | { 519 | if( !lua_apicheck_args( pLua, 2, 2, "RemoteProcess::WriteFloat" ) ) 520 | return lua_ret_bool( pLua ); 521 | 522 | if( !lua_isinteger( pLua, 1 ) ) 523 | { 524 | ENGINE_MSG( "RemoteProcess::WriteFloat: argument 1 isn't an integer!" ); 525 | return lua_ret_bool( pLua ); 526 | } 527 | if( !lua_is_digit( pLua, 2 ) ) 528 | { 529 | ENGINE_MSG( "RemoteProcess::WriteFloat: argument 2 isn't a number!" ); 530 | return lua_ret_bool( pLua ); 531 | } 532 | 533 | auto bRetVal = g_pRemoteProcess->Write< float >( ( DWORD_PTR )lua_to_int( pLua, 1 ), ( float )lua_to_number( pLua, 2 ) ); 534 | return lua_ret_bool( pLua, ( bRetVal == TRUE ? true : false ) ); 535 | } 536 | static int lua_api_remoteprocess_writeinteger( lua_State *pLua ) 537 | { 538 | if( !lua_apicheck_args( pLua, 2, 2, "RemoteProcess::WriteInteger" ) ) 539 | return lua_ret_bool( pLua ); 540 | 541 | if( !lua_isinteger( pLua, 1 ) ) 542 | { 543 | ENGINE_MSG( "RemoteProcess::WriteInteger: argument 1 isn't an integer!" ); 544 | return lua_ret_bool( pLua ); 545 | } 546 | if( !lua_is_digit( pLua, 2 ) ) 547 | { 548 | ENGINE_MSG( "RemoteProcess::WriteInteger: argument 2 isn't a number!" ); 549 | return lua_ret_bool( pLua ); 550 | } 551 | 552 | auto bRetVal = g_pRemoteProcess->Write< int >( ( DWORD_PTR )lua_to_int( pLua, 1 ), lua_to_int( pLua, 2 ) ); 553 | return lua_ret_bool( pLua, ( bRetVal == TRUE ? true : false ) ); 554 | } 555 | 556 | void lua_api_register( lua_State *pLua ) 557 | { 558 | static luaL_Reg CVarSystemLibrary[] = 559 | { 560 | { "RegisterCVar", lua_api_cvar_registercvar }, 561 | { "GetBool", lua_api_cvar_getbool }, 562 | { "GetFloat", lua_api_cvar_getfloat }, 563 | { "GetInt", lua_api_cvar_getint }, 564 | { "SetValue", lua_api_cvar_setvalue }, 565 | { NULL, NULL } 566 | }; 567 | 568 | static luaL_Reg FileSystemLibrary[] = 569 | { 570 | { "GetBool", lua_api_file_readbool }, 571 | { "GetFloat", lua_api_file_readfloat }, 572 | { "GetInt", lua_api_file_readint }, 573 | { "WriteBool", lua_api_file_writebool }, 574 | { "WriteFloat", lua_api_file_writefloat }, 575 | { "WriteInt", lua_api_file_writeint }, 576 | { NULL, NULL } 577 | }; 578 | 579 | static luaL_Reg RemoteProcessLibrary[] = 580 | { 581 | { "RegisterModule", lua_api_remoteprocess_registermodule }, 582 | { "FindSignature", lua_api_remoteprocess_findsignature }, 583 | { "GetModuleData", lua_api_remoteprocess_getmoduledata }, 584 | { "SendMessage", lua_api_remoteprocess_sendmessage }, 585 | { "ReadBool", lua_api_remoteprocess_readbool }, 586 | { "ReadByte", lua_api_remoteprocess_readbyte }, 587 | { "ReadFloat", lua_api_remoteprocess_readfloat }, 588 | { "ReadInteger", lua_api_remoteprocess_readinteger }, 589 | { "ReadString", lua_api_remoteprocess_readstring }, 590 | { "WriteBool", lua_api_remoteprocess_writebool }, 591 | { "WriteByte", lua_api_remoteprocess_writebyte }, 592 | { "WriteFloat", lua_api_remoteprocess_writefloat }, 593 | { "WriteInteger", lua_api_remoteprocess_writeinteger }, 594 | { NULL, NULL } 595 | }; 596 | 597 | lua_export_function( pLua, "RegisterCallbackFunction", lua_api_callback_registercallbackfunction ); 598 | lua_export_function( pLua, "BitBand", lua_api_bitband ); 599 | lua_export_function( pLua, "GetKeyState", lua_api_getkeystate ); 600 | lua_export_function( pLua, "Include", lua_api_include ); 601 | lua_export_function( pLua, "Sleep", lua_api_sleep ); 602 | lua_export_library( pLua, "CVar", CVarSystemLibrary ); 603 | lua_export_library( pLua, "File", FileSystemLibrary ); 604 | lua_export_library( pLua, "Process", RemoteProcessLibrary ); 605 | } 606 | } -------------------------------------------------------------------------------- /Script Engine/Lua/LuaAPI.h: -------------------------------------------------------------------------------- 1 | #ifndef __LUA_LUAAPI_H__ 2 | #define __LUA_LUAAPI_H__ 3 | 4 | #ifdef _MSC_VER 5 | #pragma once 6 | #endif 7 | 8 | #include "..\stdafx.h" 9 | 10 | namespace ScriptEngine 11 | { 12 | extern void lua_api_register( lua_State *pLua ); 13 | } 14 | 15 | #endif -------------------------------------------------------------------------------- /Script Engine/Lua/LuaInterface.cpp: -------------------------------------------------------------------------------- 1 | #include "LuaInterface.h" 2 | 3 | namespace ScriptEngine 4 | { 5 | bool lua_is_digit( lua_State *pLua, int iStackPos ) 6 | { 7 | return( lua_isinteger( pLua, iStackPos ) || lua_isnumber( pLua, iStackPos ) ); 8 | } 9 | 10 | bool lua_to_bool( lua_State *pLua, int iStackPos ) 11 | { 12 | auto iRetVal = lua_toboolean( pLua, iStackPos ); 13 | return ( iRetVal >= 1 ); 14 | } 15 | 16 | double lua_to_number( lua_State *pLua, int iStackPos ) 17 | { 18 | return lua_tonumber( pLua, iStackPos ); 19 | } 20 | 21 | int lua_to_int( lua_State *pLua, int iStackPos ) 22 | { 23 | return ( int )lua_tointeger( pLua, iStackPos ); 24 | } 25 | 26 | int lua_ret_bool( lua_State *pLua, bool bRetVal ) 27 | { 28 | lua_push_bool( pLua, bRetVal ); 29 | return 1; 30 | } 31 | 32 | int lua_ret_int( lua_State *pLua, int iRetVal ) 33 | { 34 | lua_push_int( pLua, iRetVal ); 35 | return 1; 36 | } 37 | 38 | int lua_ret_number( lua_State *pLua, double nRetVal ) 39 | { 40 | lua_push_number( pLua, nRetVal ); 41 | return 1; 42 | } 43 | 44 | int lua_ret_nil( lua_State *pLua ) 45 | { 46 | lua_push_nil( pLua ); 47 | return 1; 48 | } 49 | 50 | int lua_ret_string( lua_State *pLua, std::string strRetVal ) 51 | { 52 | lua_push_string( pLua, strRetVal ); 53 | return 1; 54 | } 55 | 56 | lua_State *lua_attach( void ) 57 | { 58 | lua_State *pLua = luaL_newstate( ); 59 | luaL_openlibs( pLua ); 60 | return pLua; 61 | } 62 | 63 | std::string lua_to_string( lua_State* pLua, int iStackPos ) 64 | { 65 | return std::string( lua_tostring( pLua, iStackPos ) ); 66 | } 67 | 68 | void lua_execute( lua_State *pLua, std::string strLuaFile ) 69 | { 70 | if( strLuaFile.empty( ) ) 71 | { 72 | ENGINE_MSG( "lua_execute: empty lua file!" ); 73 | return; 74 | } 75 | 76 | if( luaL_dofile( pLua, GetApplicationDirectory( strLuaFile ).c_str( ) ) ) 77 | { 78 | ENGINE_MSG( "lua_execute: %s!", lua_tostring( pLua, -1 ) ); 79 | } 80 | } 81 | 82 | void lua_release( lua_State *pLua ) 83 | { 84 | lua_close( pLua ); 85 | } 86 | 87 | void lua_push_bool( lua_State *pLua, bool bRetVal ) 88 | { 89 | lua_pushboolean( pLua, bRetVal ? 1 : 0 ); 90 | } 91 | 92 | void lua_push_int( lua_State *pLua, int iRetVal ) 93 | { 94 | lua_pushinteger( pLua, iRetVal ); 95 | } 96 | 97 | void lua_push_number( lua_State *pLua, double nRetVal ) 98 | { 99 | lua_pushnumber( pLua, nRetVal ); 100 | } 101 | 102 | void lua_push_nil( lua_State *pLua ) 103 | { 104 | lua_pushnil( pLua ); 105 | } 106 | 107 | void lua_push_string( lua_State *pLua, std::string strRetVal ) 108 | { 109 | lua_pushstring( pLua, strRetVal.c_str( ) ); 110 | } 111 | 112 | void lua_export_function( lua_State *pLua, std::string strFunctionName, const lua_CFunction fn ) 113 | { 114 | if( strFunctionName.empty( ) ) 115 | { 116 | ENGINE_MSG( "lua_export_function: empty function name!" ); 117 | return; 118 | } 119 | 120 | lua_register( pLua, strFunctionName.c_str( ), fn ); 121 | } 122 | void lua_export_library( lua_State *pLua, std::string strLibraryName, const luaL_Reg *pLibraryFunctions ) 123 | { 124 | if( strLibraryName.empty( ) ) 125 | { 126 | ENGINE_MSG( "lua_export_library: empty library name!" ); 127 | return; 128 | } 129 | 130 | lua_newtable( pLua ); 131 | lua_pushvalue( pLua, -1 ); 132 | lua_setglobal( pLua, strLibraryName.c_str( ) ); 133 | luaL_setfuncs( pLua, pLibraryFunctions, 0 ); 134 | } 135 | } -------------------------------------------------------------------------------- /Script Engine/Lua/LuaInterface.h: -------------------------------------------------------------------------------- 1 | #ifndef __LUA_LUAINTERFACE_H__ 2 | #define __LUA_LUAINTERFACE_H__ 3 | 4 | #ifdef _MSC_VER 5 | #pragma once 6 | #endif 7 | 8 | #include "..\stdafx.h" 9 | 10 | namespace ScriptEngine 11 | { 12 | extern bool lua_is_digit( lua_State *pLua, int iStackPos ); 13 | extern bool lua_to_bool( lua_State *pLua, int iStackPos ); 14 | extern double lua_to_number( lua_State *pLua, int iStackPos ); 15 | extern int lua_to_int( lua_State *pLua, int iStackPos ); 16 | extern int lua_ret_bool( lua_State *pLua, bool bRetVal = false ); 17 | extern int lua_ret_int( lua_State *pLua, int iRetVal = 0 ); 18 | extern int lua_ret_number( lua_State *pLua, double nRetVal = 0 ); 19 | extern int lua_ret_nil( lua_State *pLua ); 20 | extern int lua_ret_string( lua_State *pLua, std::string strRetVal ); 21 | extern lua_State *lua_attach( void ); 22 | extern std::string lua_to_string( lua_State* pLua, int iStackPos ); 23 | extern void lua_execute( lua_State *pLua, std::string strLuaFile ); 24 | extern void lua_release( lua_State *pLua ); 25 | extern void lua_push_bool( lua_State *pLua, bool bRetVal = false ); 26 | extern void lua_push_int( lua_State *pLua, int iRetVal = 0 ); 27 | extern void lua_push_number( lua_State *pLua, double nRetVal = 0 ); 28 | extern void lua_push_nil( lua_State *pLua ); 29 | extern void lua_push_string( lua_State *pLua, std::string strRetVal ); 30 | extern void lua_export_function( lua_State *pLua, std::string strFunctionName, const lua_CFunction fn ); 31 | extern void lua_export_library( lua_State *pLua, std::string strLibraryName, const luaL_Reg *pLibraryFunctions ); 32 | } 33 | 34 | #endif -------------------------------------------------------------------------------- /Script Engine/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "Lua\LuaAPI.h" 3 | 4 | int main( int iArgC, char *pszArg[] ) 5 | { 6 | printf( "\t\t\t __ _____ _\n" ); 7 | printf( "\t\t\t| | _ _ ___ | __|___ ___|_|___ ___\n" ); 8 | printf( "\t\t\t| |__| | | .'| | __| | . | | | -_|\n" ); 9 | printf( "\t\t\t|_____|___|__,| |_____|_|_|_ |_|_|_|___|\n" ); 10 | printf( "\t\t\t |___|\n\n\n" ); 11 | 12 | // Setup the command line pointer and register the client commands 13 | g_pCmdLineInterface = new CmdLineInterface( ); 14 | g_pCmdLineInterface->RegisterCommand( "-process" ); 15 | g_pCmdLineInterface->RegisterCommand( "-file" ); 16 | 17 | if( g_pCmdLineInterface->GetCommandsFromCmdLine( iArgC, pszArg ) ) 18 | { 19 | CmdCommand_t ProcCmd, FileCmd; 20 | g_pCmdLineInterface->GetCommand( "-process", &ProcCmd ); 21 | g_pCmdLineInterface->GetCommand( "-file", &FileCmd ); 22 | 23 | g_pCVarManager = new CVarSystem( ); 24 | g_pLuaCallback = new ScriptEngine::LuaCallback( ); 25 | g_pRemoteProcess = new RemoteProcess( ); 26 | if( g_pRemoteProcess->Attach( ProcCmd.m_strValue ) ) 27 | { 28 | auto pLua = ScriptEngine::lua_attach( ); 29 | ScriptEngine::lua_api_register( pLua ); 30 | ScriptEngine::lua_execute( pLua, FileCmd.m_strValue ); 31 | 32 | while( !( GetKeyState( VK_F2 ) & 0x8000 ) ) 33 | { 34 | g_pLuaCallback->CallRegisteredFunctions( pLua ); 35 | Sleep( 1 ); 36 | } 37 | 38 | ScriptEngine::lua_release( pLua ); 39 | } 40 | 41 | g_pCVarManager->Release( ); 42 | g_pCmdLineInterface->Release( ); 43 | g_pRemoteProcess->Release( ); 44 | } 45 | 46 | 47 | 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Script Engine/RemoteProcess.cpp: -------------------------------------------------------------------------------- 1 | #include "RemoteProcess.h" 2 | 3 | bool RemoteProcess::DataCompare( const unsigned char *pbData, const unsigned char *pbMask, const char *pszMask ) 4 | { 5 | for( ; *pszMask; ++pszMask, ++pbData, ++pbMask ) 6 | { 7 | if( *pszMask == 'x' && *pbData != *pbMask ) 8 | return false; 9 | } 10 | 11 | return ( *pszMask ) == NULL; 12 | } 13 | 14 | DWORD_PTR RemoteProcess::FindPrivPattern( DWORD_PTR dwAddress, DWORD_PTR dwSize, PBYTE pbMask, char *pszMask ) 15 | { 16 | for( DWORD_PTR i = 0; i < dwSize; i++ ) 17 | { 18 | if( this->DataCompare( PBYTE( dwAddress + i ), pbMask, pszMask ) ) 19 | return dwAddress + i; 20 | } 21 | 22 | return 0; 23 | } 24 | 25 | std::size_t RemoteProcess::GetModuleEntryDataIndex( std::string strModuleName ) 26 | { 27 | if( strModuleName.empty( ) ) 28 | { 29 | ENGINE_MSG( "RemoteProcess::GetModuleEntryDataIndex: empty module name!" ); 30 | return -1; 31 | } 32 | 33 | #ifdef _UNICODE 34 | std::wstring wstrModuleName( strModuleName.begin( ), strModuleName.end( ) ); 35 | #endif 36 | 37 | auto bComparedModuleName = false; 38 | for( std::size_t i = 0; i < this->m_vModuleEntryData.size( ); i++ ) 39 | { 40 | #ifdef _UNICODE 41 | bComparedModuleName = ( !wstrModuleName.compare( this->m_vModuleEntryData[ i ].szModule ) ); 42 | #else 43 | bComparedModuleName = ( !strModuleName.compare( this->m_vModuleEntryData[ i ].szModule ) ); 44 | #endif 45 | 46 | if( bComparedModuleName ) 47 | return i; 48 | } 49 | 50 | return -1; 51 | } 52 | 53 | RemoteProcess::RemoteProcess( void ) 54 | { 55 | this->m_bAttached = false; 56 | this->m_dwProcessId = 0; 57 | this->m_hProcess = NULL; 58 | } 59 | 60 | RemoteProcess::~RemoteProcess( void ) 61 | { 62 | this->Release( ); 63 | } 64 | 65 | bool RemoteProcess::Attach( std::string strProcessName, DWORD dwProcessRights ) 66 | { 67 | if( this->m_bAttached ) 68 | return true; 69 | 70 | if( ( this->m_dwProcessId = GetProcessIdByName( strProcessName ) ) == 0 ) 71 | { 72 | ENGINE_MSG( "RemoteProcess::Attach: failed to find %s", strProcessName.c_str( ) ); 73 | return false; 74 | } 75 | 76 | if( ( this->m_hProcess = OpenProcess( dwProcessRights, FALSE, this->m_dwProcessId ) ) == INVALID_HANDLE_VALUE ) 77 | { 78 | ENGINE_MSG( "RemoteProcess::Attach: failed to open process!" ); 79 | return false; 80 | } 81 | 82 | if( !( this->m_hWindow = GetWindowHandleFromProcessId( this->m_dwProcessId ) ) ) 83 | { 84 | ENGINE_MSG( "RemoteProcess::Attach: failed to find window handle!\n" ); 85 | return false; 86 | } 87 | 88 | char pszBuffer[ MAX_PATH ]; 89 | GetModuleFileNameExA( this->m_hProcess, NULL, pszBuffer, MAX_PATH ); 90 | this->m_strExecutablePath = pszBuffer; 91 | 92 | return ( this->m_bAttached = true ); 93 | } 94 | 95 | DWORD RemoteProcess::GetProcessID( void ) 96 | { 97 | return this->m_dwProcessId; 98 | } 99 | 100 | DWORD_PTR RemoteProcess::FindSignature( std::string strModuleName, std::string strSignature, DWORD_PTR dwExtraOffset ) 101 | { 102 | if( !this->m_bAttached ) 103 | return 0; 104 | 105 | if( strModuleName.empty( ) || strSignature.empty( ) ) 106 | { 107 | ENGINE_MSG( "RemoteProcess::FindSignature: empty arg data" ); 108 | return 0; 109 | } 110 | 111 | auto nIndex = this->GetModuleEntryDataIndex( strModuleName ); 112 | if( nIndex == -1 ) 113 | { 114 | ENGINE_MSG( "RemoteProcess::FindSignature: %s isn't registered!", strModuleName.c_str( ) ); 115 | return 0; 116 | } 117 | 118 | auto ModuleEntryData = this->m_vModuleEntryData[ nIndex ]; 119 | std::size_t nPos = 0; 120 | std::string strMask; 121 | std::vector< BYTE > vBytes; 122 | 123 | while( true ) 124 | { 125 | nPos = strSignature.find_first_of( " \n\0" ); 126 | if( nPos != std::string::npos ) 127 | { 128 | auto bStringPos = ( nPos != std::string::npos ); 129 | bool bLast = ( !strSignature.empty( ) ); 130 | auto strCurrentByte = strSignature.substr( 0, nPos ); 131 | strSignature.erase( 0, nPos + 1 ); 132 | 133 | if( !strCurrentByte.compare( "??" ) ) 134 | { 135 | vBytes.push_back( 0 ); 136 | strMask.append( "?" ); 137 | } 138 | else 139 | { 140 | strMask.append( "x" ); 141 | vBytes.push_back( ( BYTE )std::strtoul( ( std::string( "0x" ) + strCurrentByte ).c_str( ), NULL, 0 ) ); 142 | } 143 | } 144 | else 145 | { 146 | if( !strSignature.empty( ) ) 147 | { 148 | if( !strSignature.compare( "??" ) ) 149 | { 150 | vBytes.push_back( 0 ); 151 | strMask.append( "?" ); 152 | } 153 | else 154 | { 155 | strMask.append( "x" ); 156 | vBytes.push_back( ( BYTE )std::strtoul( ( std::string( "0x" ) + strSignature ).c_str( ), NULL, 0 ) ); 157 | } 158 | } 159 | 160 | break; 161 | } 162 | } 163 | 164 | auto nNumberOfBytes = vBytes.size( ); 165 | auto pbPattern = new BYTE[ vBytes.size( ) ]; 166 | for( std::size_t i = 0; i < nNumberOfBytes; i++ ) 167 | pbPattern[ i ] = vBytes[ i ]; 168 | 169 | auto pszMask = ( char * )strMask.c_str( ); 170 | auto dwOffset = this->FindPattern( ( DWORD_PTR )ModuleEntryData.modBaseAddr, ( DWORD_PTR )ModuleEntryData.modBaseSize, pbPattern, pszMask, dwExtraOffset ); 171 | 172 | delete pbPattern; 173 | 174 | return dwOffset; 175 | 176 | } 177 | 178 | DWORD_PTR RemoteProcess::FindPattern( DWORD_PTR dwAddress, DWORD_PTR dwLen, PBYTE pbMask, char *pszMask, DWORD_PTR dwOffset ) 179 | { 180 | auto dwCurrentAddress = dwAddress; 181 | auto pbPage = new BYTE[ 0x1000 ]; 182 | 183 | do 184 | { 185 | if( ReadProcessMemory( this->m_hProcess, ( LPCVOID )( dwAddress ), pbPage, 0x1000, NULL ) ) 186 | { 187 | DWORD dwPrivPattern = this->FindPrivPattern( DWORD( pbPage ), 0x1000, pbMask, pszMask ); 188 | 189 | if( dwPrivPattern ) 190 | { 191 | if( dwOffset != 0 ) 192 | dwPrivPattern += dwOffset; 193 | dwPrivPattern -= DWORD( pbPage ); 194 | dwPrivPattern += dwAddress; 195 | 196 | delete pbPage; 197 | 198 | return dwPrivPattern; 199 | } 200 | 201 | dwAddress += 0x1000; 202 | } 203 | } while( dwAddress < dwCurrentAddress + dwLen ); 204 | 205 | delete pbPage; 206 | 207 | return 0; 208 | } 209 | 210 | HANDLE RemoteProcess::GetProcessHandle( void ) 211 | { 212 | return this->m_hProcess; 213 | } 214 | 215 | HWND RemoteProcess::GetWindowHandle( void ) 216 | { 217 | return this->m_hWindow; 218 | } 219 | 220 | bool RemoteProcess::RegisterModule( std::string strModuleName ) 221 | { 222 | if( strModuleName.empty( ) ) 223 | { 224 | ENGINE_MSG( "RemoteProcess::RegisterModule: empty module name!" ); 225 | return 0; 226 | } 227 | 228 | MODULEENTRY32 ModuleEntry; 229 | if( GetRemoteModuleEntryData( strModuleName, this->m_dwProcessId, &ModuleEntry ) ) 230 | { 231 | this->m_vModuleEntryData.push_back( ModuleEntry ); 232 | return true; 233 | } 234 | 235 | ENGINE_MSG( "RemoteProcess::RegisterModule: %s isn't registered!\n", strModuleName.c_str( ) ); 236 | return false; 237 | } 238 | 239 | MODULEENTRY32 RemoteProcess::GetModuleData( std::string strModuleName ) 240 | { 241 | static MODULEENTRY32 InvalidModule = { NULL }; 242 | 243 | auto nIndex = this->GetModuleEntryDataIndex( strModuleName ); 244 | if( nIndex == -1 ) 245 | { 246 | ENGINE_MSG( "RemoteProcess::GetModuleData: %s isn't registered!", strModuleName.c_str( ) ); 247 | return InvalidModule; 248 | } 249 | 250 | return this->m_vModuleEntryData[ nIndex ]; 251 | } 252 | 253 | std::string RemoteProcess::GetExecutablePath( void ) 254 | { 255 | return this->m_strExecutablePath; 256 | } 257 | 258 | void RemoteProcess::Release( void ) 259 | { 260 | if( this->m_bAttached ) 261 | { 262 | CloseHandle( this->m_hProcess ); 263 | this->m_dwProcessId = 0; 264 | this->m_hProcess = NULL; 265 | this->m_hWindow = NULL; 266 | this->m_strExecutablePath.clear( ); 267 | this->m_vModuleEntryData.clear( ); 268 | } 269 | } 270 | 271 | RemoteProcess *g_pRemoteProcess = nullptr; -------------------------------------------------------------------------------- /Script Engine/RemoteProcess.h: -------------------------------------------------------------------------------- 1 | #ifndef __REMOTEPROCESS_H__ 2 | #define __REMOTEPROCESS_H__ 3 | 4 | #ifdef _MSC_VER 5 | #pragma once 6 | #endif 7 | 8 | #include "stdafx.h" 9 | 10 | class RemoteProcess 11 | { 12 | private: 13 | bool m_bAttached; 14 | DWORD m_dwProcessId; 15 | HANDLE m_hProcess; 16 | HWND m_hWindow; 17 | std::string m_strExecutablePath; 18 | std::vector< MODULEENTRY32 > m_vModuleEntryData; 19 | 20 | private: 21 | bool DataCompare( const unsigned char *pbData, const unsigned char *pbMask, const char* pszMask ); 22 | DWORD_PTR FindPrivPattern( DWORD_PTR dwAddress, DWORD_PTR dwSize, PBYTE pbMask, char *pszMask ); 23 | std::size_t GetModuleEntryDataIndex( std::string strModuleName ); 24 | 25 | public: 26 | RemoteProcess( void ); 27 | ~RemoteProcess( void ); 28 | bool Attach( std::string strProcessName, DWORD dwProcessRights = PROCESS_ALL_ACCESS ); 29 | bool RegisterModule( std::string strModuleName ); 30 | DWORD GetProcessID( void ); 31 | DWORD_PTR FindSignature( std::string strModuleName, std::string strSiganture, DWORD_PTR dwExtraOffset = 0 ); 32 | DWORD_PTR FindPattern( DWORD_PTR dwAddress, DWORD_PTR dwLen, PBYTE pbMask, char *pszMask, DWORD_PTR dwOffset = 0 ); 33 | HANDLE GetProcessHandle( void ); 34 | HWND GetWindowHandle( void ); 35 | MODULEENTRY32 GetModuleData( std::string strModuleName ); 36 | std::string GetExecutablePath( void ); 37 | void Release( void ); 38 | public: 39 | template< class T > 40 | BOOL Read( DWORD_PTR dwBaseAddress, T lpBuffer ) 41 | { 42 | return ReadProcessMemory( this->m_hProcess, ( LPCVOID )( dwBaseAddress ), &lpBuffer, sizeof( T ), NULL ); 43 | } 44 | template< class T > 45 | T Read( DWORD_PTR dwBaseAddress ) 46 | { 47 | T lpBuffer; 48 | ReadProcessMemory( this->m_hProcess, ( LPCVOID )( dwBaseAddress ), &lpBuffer, sizeof( T ), NULL ); 49 | return lpBuffer; 50 | } 51 | template< class T > 52 | BOOL Write( DWORD_PTR dwBaseAddress, const T lpBuffer ) 53 | { 54 | return WriteProcessMemory( this->m_hProcess, ( LPVOID )( dwBaseAddress ), &lpBuffer, sizeof( T ), NULL ); 55 | } 56 | }; 57 | 58 | extern RemoteProcess *g_pRemoteProcess; 59 | 60 | #endif -------------------------------------------------------------------------------- /Script Engine/Script Engine.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {33025A3F-957D-4F25-BA3C-B4E950AF42B4} 15 | Win32Proj 16 | ScriptEngine 17 | 18 | 19 | 20 | Application 21 | true 22 | v120 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v120 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | 45 | 46 | false 47 | 48 | 49 | 50 | 51 | 52 | Level3 53 | Disabled 54 | WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) 55 | true 56 | $(LUA_DIR)\include;%(AdditionalIncludeDirectories) 57 | 58 | 59 | Console 60 | true 61 | $(LUA_DIR)\lib;%(AdditionalLibraryDirectories) 62 | lua5.3.lib;%(AdditionalDependencies) 63 | 64 | 65 | 66 | 67 | Level3 68 | 69 | 70 | MaxSpeed 71 | true 72 | true 73 | WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) 74 | true 75 | $(LUA_DIR)\include;%(AdditionalIncludeDirectories) 76 | 77 | 78 | Console 79 | true 80 | true 81 | true 82 | $(LUA_DIR)\lib;%(AdditionalLibraryDirectories) 83 | lua5.3.lib;%(AdditionalDependencies) 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /Script Engine/Script Engine.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | {b90616c5-fa3f-4ded-86b7-25dc540cd27f} 18 | 19 | 20 | {4214ef96-ee9d-44f7-ba5e-46c81db9fb61} 21 | 22 | 23 | {ec331b58-bb31-455a-a7c1-f4c82dde6141} 24 | 25 | 26 | {cb041885-a275-43da-83f2-2724bd80033f} 27 | 28 | 29 | {1a87affc-8e4b-4aae-bb2f-554413936819} 30 | 31 | 32 | {6481a1ba-f7b3-4fd8-8f0d-12c53ee4f73f} 33 | 34 | 35 | {0df94ed3-6b3d-4348-b30c-38746ed36b82} 36 | 37 | 38 | 39 | 40 | Headerdateien 41 | 42 | 43 | Script Engine Data\Lua Script Engine\Function Interface 44 | 45 | 46 | Script Engine Data\Lua Script Engine\Callback 47 | 48 | 49 | Script Engine Data\Remote Process 50 | 51 | 52 | Script Engine Data\User API 53 | 54 | 55 | Script Engine Data\Command Line Input 56 | 57 | 58 | Script Engine Data\Lua Script Engine 59 | 60 | 61 | Headerdateien 62 | 63 | 64 | 65 | 66 | Quelldateien 67 | 68 | 69 | Script Engine Data\Lua Script Engine\Function Interface 70 | 71 | 72 | Script Engine Data\Lua Script Engine\Callback 73 | 74 | 75 | Script Engine Data\Remote Process 76 | 77 | 78 | Script Engine Data\User API 79 | 80 | 81 | Script Engine Data\Command Line Input 82 | 83 | 84 | Quelldateien 85 | 86 | 87 | Quelldateien 88 | 89 | 90 | -------------------------------------------------------------------------------- /Script Engine/UserAPI.cpp: -------------------------------------------------------------------------------- 1 | #include "UserAPI.h" 2 | 3 | bool GetBoolFromFile( std::string strAppName, std::string strKeyName, std::string strPath, bool bDefaultValue ) 4 | { 5 | if( strAppName.empty( ) || strKeyName.empty( ) || strPath.empty( ) ) 6 | return bDefaultValue; 7 | 8 | auto pszDefaultValue = ( bDefaultValue ? "true" : "false" ); 9 | 10 | char pszBuffer[ 0x400 ]; 11 | if( !GetPrivateProfileStringA( strAppName.c_str( ), strKeyName.c_str( ), pszDefaultValue, pszBuffer, 0x400, strPath.c_str( ) ) ) 12 | return bDefaultValue; 13 | 14 | std::string strRetVal( pszBuffer ); 15 | std::transform( strRetVal.begin( ), strRetVal.end( ), strRetVal.begin( ), ::tolower ); 16 | 17 | auto bFalse = ( !strRetVal.compare( "false" ) ); 18 | auto bTrue = ( !strRetVal.compare( "true" ) ); 19 | 20 | if( !bTrue && !bFalse ) 21 | return bDefaultValue; 22 | 23 | if( bTrue ) 24 | return true; 25 | 26 | return false; 27 | } 28 | 29 | bool GetRemoteModuleEntryData( std::string strModuleName, DWORD dwProcessId, MODULEENTRY32 *pModuleEntry ) 30 | { 31 | if( strModuleName.empty( ) || !dwProcessId ) 32 | return false; 33 | 34 | auto hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwProcessId ); 35 | if( hSnapshot == INVALID_HANDLE_VALUE ) 36 | return false; 37 | 38 | auto bFoundModuleEntryData = false; 39 | MODULEENTRY32 ModuleEntry = { sizeof( MODULEENTRY32 ) }; 40 | if( auto bFoundCurrentModuleEntryData = Module32First( hSnapshot, &ModuleEntry ) ) 41 | { 42 | #ifdef _UNICODE 43 | std::wstring wstrModuleName( strModuleName.begin( ), strModuleName.end( ) ); 44 | #endif 45 | 46 | bool bComparedModuleName = false; 47 | while( bFoundCurrentModuleEntryData ) 48 | { 49 | #ifdef _UNICODE 50 | bComparedModuleName = ( !wstrModuleName.compare( ModuleEntry.szModule ) ); 51 | #else 52 | bComparedModuleName = ( !strModuleName.compare( ModuleEntry.szModule ) ); 53 | #endif 54 | if( bComparedModuleName ) 55 | { 56 | if( pModuleEntry ) 57 | *pModuleEntry = ModuleEntry; 58 | 59 | bFoundModuleEntryData = true; 60 | break; 61 | } 62 | bFoundCurrentModuleEntryData = Module32Next( hSnapshot, &ModuleEntry ); 63 | } 64 | } 65 | 66 | CloseHandle( hSnapshot ); 67 | 68 | return bFoundModuleEntryData; 69 | } 70 | 71 | DWORD GetProcessIdByName( std::string strProcessName ) 72 | { 73 | if( strProcessName.empty( ) ) 74 | return false; 75 | 76 | auto hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); 77 | if( hSnapshot == INVALID_HANDLE_VALUE ) 78 | return false; 79 | 80 | DWORD dwProcessId = 0; 81 | PROCESSENTRY32 ProcEntry = { sizeof( PROCESSENTRY32 ) }; 82 | if( auto bFoundCurrentProcess = Process32First( hSnapshot, &ProcEntry ) ) 83 | { 84 | #ifdef _UNICODE 85 | std::wstring wstrModuleName( strProcessName.begin( ), strProcessName.end( ) ); 86 | #endif 87 | auto bComparedProcName = false; 88 | while( bFoundCurrentProcess ) 89 | { 90 | #ifdef _UNICODE 91 | bComparedProcName = ( !wstrModuleName.compare( ProcEntry.szExeFile ) ); 92 | #else 93 | bComparedProcName = ( !strProcessName.compare( ProcEntry.szExeFile ) ); 94 | #endif 95 | if( bComparedProcName ) 96 | { 97 | dwProcessId = ProcEntry.th32ProcessID; 98 | break; 99 | } 100 | bFoundCurrentProcess = Process32Next( hSnapshot, &ProcEntry ); 101 | } 102 | } 103 | 104 | CloseHandle( hSnapshot ); 105 | 106 | return dwProcessId; 107 | } 108 | 109 | float GetFloatFromFile( std::string strAppName, std::string strKeyName, std::string strPath, float flDefaultValue ) 110 | { 111 | if( strAppName.empty( ) || strKeyName.empty( ) || strPath.empty( ) ) 112 | return flDefaultValue; 113 | 114 | char pszDefaultValue[ 0x200 ]; 115 | sprintf_s( pszDefaultValue, "%f", flDefaultValue ); 116 | 117 | char pszBuffer[ 0x400 ]; 118 | if( !GetPrivateProfileStringA( strAppName.c_str( ), strKeyName.c_str( ), pszDefaultValue, pszBuffer, 0x400, strPath.c_str( ) ) ) 119 | return flDefaultValue; 120 | 121 | if( !isdigit( pszBuffer[ 0 ] ) ) 122 | return flDefaultValue; 123 | 124 | return std::strtof( pszBuffer, NULL ); 125 | } 126 | 127 | HWND GetWindowHandleFromProcessId( DWORD dwProcessId ) 128 | { 129 | auto hWnd = GetTopWindow( NULL ); 130 | 131 | while( hWnd ) 132 | { 133 | DWORD dwCurrentProcessId = 0; 134 | GetWindowThreadProcessId( hWnd, &dwCurrentProcessId ); 135 | 136 | if( dwCurrentProcessId == dwProcessId ) 137 | break; 138 | 139 | hWnd = GetNextWindow( hWnd, GW_HWNDNEXT ); 140 | } 141 | 142 | return hWnd; 143 | } 144 | 145 | int GetIntFromFile( std::string strAppName, std::string strKeyName, std::string strPath, int iDefaultValue ) 146 | { 147 | if( strAppName.empty( ) || strKeyName.empty( ) || strPath.empty( ) ) 148 | return iDefaultValue; 149 | 150 | return ( int )GetPrivateProfileIntA( strAppName.c_str( ), strKeyName.c_str( ), iDefaultValue, strPath.c_str( ) ); 151 | } 152 | 153 | std::string GetApplicationDirectory( std::string strOptionalFileInDir ) 154 | { 155 | char pszPath[ MAX_PATH ]; 156 | GetCurrentDirectoryA( MAX_PATH, pszPath ); 157 | 158 | auto strApplicationDirectory = std::string( pszPath ) + std::string( "\\" ); 159 | if( !strOptionalFileInDir.empty( ) ) 160 | strApplicationDirectory.append( strOptionalFileInDir ); 161 | 162 | return strApplicationDirectory; 163 | } 164 | 165 | void ENGINE_MSG( const char *pszString, ... ) 166 | { 167 | va_list args; 168 | va_start( args, pszString ); 169 | char pszBuffer[ 0x400 ]; 170 | vsprintf_s( pszBuffer, pszString, args ); 171 | auto hCon = GetStdHandle( STD_OUTPUT_HANDLE ); 172 | SetConsoleTextAttribute( hCon, 10 ); 173 | printf( "[" ); 174 | SetConsoleTextAttribute( hCon, 12 ); 175 | printf( "ScriptEngine" ); 176 | SetConsoleTextAttribute( hCon, 10 ); 177 | printf( "] " ); 178 | SetConsoleTextAttribute( hCon, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED ); 179 | printf( pszBuffer ); 180 | printf( "\n" ); 181 | va_end( args ); 182 | } 183 | 184 | void WriteBoolToFile( std::string strAppName, std::string strKeyName, std::string strPath, bool bValue ) 185 | { 186 | if( strAppName.empty( ) || strKeyName.empty( ) || strPath.empty( ) ) 187 | return; 188 | 189 | char pszBuffer[ 0x20 ]; 190 | sprintf_s( pszBuffer, ( bValue ? "true" : "false" ) ); 191 | 192 | WritePrivateProfileStringA( strAppName.c_str( ), strKeyName.c_str( ), pszBuffer, strPath.c_str( ) ); 193 | } 194 | 195 | void WriteFloatToFile( std::string strAppName, std::string strKeyName, std::string strPath, float flValue ) 196 | { 197 | if( strAppName.empty( ) || strKeyName.empty( ) || strPath.empty( ) ) 198 | return; 199 | 200 | char pszBuffer[ 0x20 ]; 201 | sprintf_s( pszBuffer, "%f", flValue ); 202 | 203 | WritePrivateProfileStringA( strAppName.c_str( ), strKeyName.c_str( ), pszBuffer, strPath.c_str( ) ); 204 | } 205 | 206 | void WriteIntToFile( std::string strAppName, std::string strKeyName, std::string strPath, int iValue ) 207 | { 208 | if( strAppName.empty( ) || strKeyName.empty( ) || strPath.empty( ) ) 209 | return; 210 | 211 | char pszBuffer[ 0x20 ]; 212 | sprintf_s( pszBuffer, "%i", iValue ); 213 | 214 | WritePrivateProfileStringA( strAppName.c_str( ), strKeyName.c_str( ), pszBuffer, strPath.c_str( ) ); 215 | } -------------------------------------------------------------------------------- /Script Engine/UserAPI.h: -------------------------------------------------------------------------------- 1 | #ifndef __USERAPI_H__ 2 | #define __USERAPI_H__ 3 | 4 | #ifdef _MSC_VER 5 | #pragma once 6 | #endif 7 | 8 | #include "stdafx.h" 9 | 10 | extern bool GetBoolFromFile( std::string strAppName, std::string strKeyName, std::string strPath, bool bDefaultValue = false ); 11 | extern bool GetRemoteModuleEntryData( std::string strModuleName, DWORD dwProcessId, MODULEENTRY32 *pModuleEntry ); 12 | extern DWORD GetProcessIdByName( std::string strProcessName ); 13 | extern HWND GetWindowHandleFromProcessId( DWORD dwProcessId ); 14 | extern float GetFloatFromFile( std::string strAppName, std::string strKeyName, std::string strPath, float flDefaultValue = 0.f ); 15 | extern int GetIntFromFile( std::string strAppName, std::string strKeyName, std::string strPath, int iDefaultValue = 0 ); 16 | extern std::string GetApplicationDirectory( std::string strOptionalFileInDir = "" ); 17 | extern void ENGINE_MSG( const char *pszString, ... ); 18 | extern void WriteBoolToFile( std::string strAppName, std::string strKeyName, std::string strPath, bool bValue ); 19 | extern void WriteFloatToFile( std::string strAppName, std::string strKeyName, std::string strPath, float flValue ); 20 | extern void WriteIntToFile( std::string strAppName, std::string strKeyName, std::string strPath, int iValue ); 21 | 22 | #endif -------------------------------------------------------------------------------- /Script Engine/stdafx.h: -------------------------------------------------------------------------------- 1 | #ifndef __STDAFX_H__ 2 | #define __STDAFX_H__ 3 | 4 | #ifdef _MSC_VER 5 | #pragma once 6 | #endif 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "lua.hpp" 16 | 17 | #include "UserAPI.h" 18 | #include "CmdLineInterface.h" 19 | #include "CVarSystem.h" 20 | #include "RemoteProcess.h" 21 | #include "Lua\LuaInterface.h" 22 | #include "Lua\Callback.h" 23 | 24 | #endif -------------------------------------------------------------------------------- /example.lua: -------------------------------------------------------------------------------- 1 | -- file: example.lua 2 | -- usage: test this script with a source engine game. 3 | 4 | local firsttick = true 5 | function MainCallbackFunction( ) 6 | 7 | if( firsttick == true ) then 8 | print( "print from MainCallbackFunction" ) 9 | firsttick = false 10 | end 11 | end 12 | 13 | if( Process.RegisterModule( "client.dll" ) == true and Process.RegisterModule( "engine.dll" ) == true ) then 14 | 15 | local ClientBase, ClientSize = Process.GetModuleData( "client.dll" ) 16 | print( string.format( "found client.dll at 0x%X, size: 0x%X", ClientBase, ClientSize ) ) 17 | 18 | local EngineBase, EngineSize = Process.GetModuleData( "engine.dll" ) 19 | print( string.format( "found engine.dll at 0x%X, size: 0x%X", EngineBase, EngineSize ) ) 20 | 21 | if( ClientBase ~= 0 and EngineBase ~= 0 ) then 22 | RegisterCallbackFunction( "MainCallbackFunction" ) 23 | end 24 | end -------------------------------------------------------------------------------- /lua531_windows_files.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HazeProductions/Lua-Script-Engine/0fac6cc7e77bb586e885a50707453ca164d5723f/lua531_windows_files.rar --------------------------------------------------------------------------------