Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
420 changes: 393 additions & 27 deletions .gitignore

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project>
<Target Name="CreateM3ScriptHookImportLibAlias"
AfterTargets="Link"
Condition="'$(MSBuildProjectName)' == 'M3ScriptHook' and Exists('$(OutDir)dxgi.lib')">
<Copy SourceFiles="$(OutDir)dxgi.lib"
DestinationFiles="$(OutDir)M3ScriptHook.lib"
SkipUnchangedFiles="true" />
</Target>
</Project>
6 changes: 3 additions & 3 deletions ExampleDLLPlugin/ExampleDLLPlugin.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
<PropertyGroup Label="Globals">
<ProjectGuid>{A7650B52-67CB-406C-BD46-731ED539C9FE}</ProjectGuid>
<RootNamespace>ExampleDLLPlugin</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
<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>
<CharacterSet>MultiByte</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v145</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v145</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down
25 changes: 2 additions & 23 deletions M3ScriptHook.sln
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
# Visual Studio Version 18
VisualStudioVersion = 18.7.11925.98 stable
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "M3ScriptHookLauncher", "M3ScriptHookLauncher\M3ScriptHookLauncher.vcxproj", "{CA29DBC0-E648-4470-AA81-9082B143FBDA}"
ProjectSection(ProjectDependencies) = postProject
{D4AAA849-F280-2B84-46B2-C9524602F05F} = {D4AAA849-F280-2B84-46B2-C9524602F05F}
{8D715BC3-5BBC-42E2-9F0D-29FBAFB6490B} = {8D715BC3-5BBC-42E2-9F0D-29FBAFB6490B}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "M3ScriptHook", "M3ScriptHook\M3ScriptHook.vcxproj", "{8D715BC3-5BBC-42E2-9F0D-29FBAFB6490B}"
ProjectSection(ProjectDependencies) = postProject
{D4AAA849-F280-2B84-46B2-C9524602F05F} = {D4AAA849-F280-2B84-46B2-C9524602F05F}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ExampleDLLPlugin", "ExampleDLLPlugin\ExampleDLLPlugin.vcxproj", "{A7650B52-67CB-406C-BD46-731ED539C9FE}"
ProjectSection(ProjectDependencies) = postProject
{D4AAA849-F280-2B84-46B2-C9524602F05F} = {D4AAA849-F280-2B84-46B2-C9524602F05F}
{CA29DBC0-E648-4470-AA81-9082B143FBDA} = {CA29DBC0-E648-4470-AA81-9082B143FBDA}
{8D715BC3-5BBC-42E2-9F0D-29FBAFB6490B} = {8D715BC3-5BBC-42E2-9F0D-29FBAFB6490B}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libudis86", "shared\include\hooking\libudis86.vcxproj", "{D4AAA849-F280-2B84-46B2-C9524602F05F}"
EndProject
Global
Expand All @@ -29,18 +16,10 @@ Global
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CA29DBC0-E648-4470-AA81-9082B143FBDA}.Debug|x64.ActiveCfg = Debug|x64
{CA29DBC0-E648-4470-AA81-9082B143FBDA}.Debug|x64.Build.0 = Debug|x64
{CA29DBC0-E648-4470-AA81-9082B143FBDA}.Release|x64.ActiveCfg = Release|x64
{CA29DBC0-E648-4470-AA81-9082B143FBDA}.Release|x64.Build.0 = Release|x64
{8D715BC3-5BBC-42E2-9F0D-29FBAFB6490B}.Debug|x64.ActiveCfg = Debug|x64
{8D715BC3-5BBC-42E2-9F0D-29FBAFB6490B}.Debug|x64.Build.0 = Debug|x64
{8D715BC3-5BBC-42E2-9F0D-29FBAFB6490B}.Release|x64.ActiveCfg = Release|x64
{8D715BC3-5BBC-42E2-9F0D-29FBAFB6490B}.Release|x64.Build.0 = Release|x64
{A7650B52-67CB-406C-BD46-731ED539C9FE}.Debug|x64.ActiveCfg = Debug|x64
{A7650B52-67CB-406C-BD46-731ED539C9FE}.Debug|x64.Build.0 = Debug|x64
{A7650B52-67CB-406C-BD46-731ED539C9FE}.Release|x64.ActiveCfg = Release|x64
{A7650B52-67CB-406C-BD46-731ED539C9FE}.Release|x64.Build.0 = Release|x64
{D4AAA849-F280-2B84-46B2-C9524602F05F}.Debug|x64.ActiveCfg = Debug|x64
{D4AAA849-F280-2B84-46B2-C9524602F05F}.Debug|x64.Build.0 = Debug|x64
{D4AAA849-F280-2B84-46B2-C9524602F05F}.Release|x64.ActiveCfg = Release|x64
Expand Down
31 changes: 21 additions & 10 deletions M3ScriptHook/M3ScriptHook.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@
<ProjectGuid>{8D715BC3-5BBC-42E2-9F0D-29FBAFB6490B}</ProjectGuid>
<RootNamespace>M3ScriptHook</RootNamespace>
<ProjectName>M3ScriptHook</ProjectName>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
<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>
<CharacterSet>MultiByte</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v145</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v145</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand All @@ -41,14 +41,16 @@
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<IncludePath>C:\Users\Joel\include;$(IncludePath)</IncludePath>
<IncludePath>$(IncludePath)</IncludePath>
<OutDir>$(SolutionDir)build\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)build\$(Configuration)\tmp\$(ProjectName)\</IntDir>
<TargetName>dxgi</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<IncludePath>C:\Users\Joel\include;$(IncludePath)</IncludePath>
<IncludePath>$(IncludePath)</IncludePath>
<OutDir>$(SolutionDir)build\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)build\$(Configuration)\tmp\$(ProjectName)\</IntDir>
<TargetName>dxgi</TargetName>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
Expand All @@ -64,19 +66,26 @@
<AdditionalDependencies>$(SolutionDir)build\$(Configuration)\lib\libudis86.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>$(SolutionDir)\copydebug.bat</Command>
<Command>"$(SolutionDir)\copydebug.bat"</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MinSpace</Optimization>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<FavorSizeOrSpeed>Size</FavorSizeOrSpeed>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<AdditionalIncludeDirectories>include;..\shared\include;..\vendor\LUA</AdditionalIncludeDirectories>
<PreprocessorDefinitions>LUA_BUILD_AS_DLL;_CRT_SECURE_NO_WARNINGS;LUA_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<OmitFramePointers>true</OmitFramePointers>
<StringPooling>true</StringPooling>
<BufferSecurityCheck>false</BufferSecurityCheck>
<FloatingPointModel>Fast</FloatingPointModel>
<RuntimeTypeInfo />
<OpenMPSupport>
</OpenMPSupport>
<ConformanceMode>false</ConformanceMode>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand All @@ -86,9 +95,11 @@
</IgnoreSpecificDefaultLibraries>
<ModuleDefinitionFile>
</ModuleDefinitionFile>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>$(SolutionDir)build\$(Configuration)\lib\libudis86.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>$(SolutionDir)\copyrelease.bat</Command>
<Command>"$(SolutionDir)\copyrelease.bat"</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
27 changes: 26 additions & 1 deletion M3ScriptHook/include/M3ScriptHook.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include <map>
#include <mutex>
#include <vector>
#include <deque>
#include <functional>

static const char *BindableKeys[] = {
"VK_LBUTTON", "VK_RBUTTON", "VK_CANCEL", "VK_MBUTTON", "VK_XBUTTON1",
Expand Down Expand Up @@ -95,8 +97,15 @@ class M3ScriptHook : public singleton<M3ScriptHook>
bool m_bEnded = false;
//std::vector<M3KeyBind*> keyBinds = {};
std::map<uint8_t, std::string> keyBinds = {};
std::map<uint8_t, bool> keyWasDown = {};
std::recursive_mutex _keyBindMutex;

std::deque<std::function<void(lua_State*)>> m_luaQueue;
std::mutex m_luaQueueMutex;
// Guards the actual execution in DrainLuaQueue, in case the main-thread
// hook site ever turns out to be reachable from more than one thread.
std::mutex m_luaExecMutex;

public:
M3ScriptHook();
virtual ~M3ScriptHook() = default;
Expand All @@ -108,13 +117,29 @@ class M3ScriptHook : public singleton<M3ScriptHook>
void LoadScript(const std::string &file);
void LoadLuaFile(lua_State *L, const std::string &name);
bool ExecuteLua(lua_State *L, const std::string &lua);

// Thread-safe: queues Lua source to be run later on the thread that
// actually owns the game's Lua state (see DrainLuaQueue). Any caller
// that isn't already known to be on that thread must go through this
// instead of calling ExecuteLua directly.
void QueueLua(const std::string &lua);

// Thread-safe: same as QueueLua but for arbitrary work that needs to
// touch the Lua state (e.g. registering C closures, starting plugins)
// rather than just running a chunk of Lua source.
void QueueWork(std::function<void(lua_State*)> work);

// Must only be called from the thread that owns the game's Lua state
// (i.e. from the main-thread hook installed in LuaFunctions::Process).
void DrainLuaQueue(lua_State *L);

static uint32_t WINAPI mainThread(LPVOID);
void StartThreads();
bool HasEnded();
void Shutdown();

void CreateKeyBind(const char *key, const char *context);
void DestroyKeyBind(const char *key, const char *context);
void DestroyKeyBind(const char *key);
void ProcessKeyBinds();
};

Expand Down
Loading