Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Split event hooks into separate files
  • Loading branch information
michael-fadely committed Aug 19, 2015
1 parent bf28fb3 commit 2cebbb2
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 113 deletions.
85 changes: 85 additions & 0 deletions SA2ModLoader/Events.cpp
@@ -0,0 +1,85 @@
#include "stdafx.h"
#include "Events.h"
#include "CodeParser.hpp"

std::vector<ModEvent> modFrameEvents;
std::vector<ModEvent> modInputEvents;
CodeParser codeParser;

/**
* Registers an event to the specified event list.
* @param eventList The event list to add to.
* @param module The module for the mod DLL.
* @param name The name of the exported function from the module (i.e OnFrame)
*/
void RegisterEvent(std::vector<ModEvent>& eventList, HMODULE module, const char* name)
{
const ModEvent modEvent = (const ModEvent)GetProcAddress(module, name);

if (modEvent != nullptr)
eventList.push_back(modEvent);
}

void __cdecl OnFrame()
{
codeParser.processCodeList();
RaiseEvents(modFrameEvents);
}

#pragma region OnFrame Initialization

void* caseDefault_ptr = (void*)0x004340CC;
void* case08_ptr = (void*)0x0043405D;
void* case09_ptr = (void*)0x0043407E;
void* case10_ptr = (void*)0x0043407E;

void __declspec(naked) OnFrame_MidJump()
{
__asm
{
push eax
call OnFrame
pop eax

pop edi
pop esi
pop ebp
pop ebx
pop ecx
retn
}
}

void* OnFrame_Hook_ptr = (void*)0x004340E7;
void __declspec(naked) OnFrame_Hook()
{
__asm
{
push eax
call OnFrame
pop eax
retn
}
}

void InitOnFrame()
{
WriteJump(case08_ptr, OnFrame_MidJump);
WriteJump(case09_ptr, OnFrame_MidJump);
WriteJump(case10_ptr, OnFrame_MidJump);

// OnFrame caseDefault
// Occurs if the current game mode isn't 8, 9 or 10, and byte_174AFF9 == 1
WriteJump(caseDefault_ptr, OnFrame_MidJump);

// OnFrame OnFrame_Hook
// Occurs at the end of the function (effectively the "else" to the statement above)
WriteJump(OnFrame_Hook_ptr, OnFrame_Hook);
}

#pragma endregion

void __cdecl OnInput()
{
RaiseEvents(modInputEvents);
}
25 changes: 25 additions & 0 deletions SA2ModLoader/Events.h
@@ -0,0 +1,25 @@
#pragma once

#include "SA2ModLoader.h"
#include "CodeParser.hpp"
#include <vector>

extern std::vector<ModEvent> modFrameEvents;
extern std::vector<ModEvent> modInputEvents;
extern CodeParser codeParser;

void RegisterEvent(std::vector<ModEvent>& eventList, HMODULE module, const char* name);

/**
* Calls all registered events in the specified event list.
* @param eventList The list of events to trigger.
*/
static inline void RaiseEvents(const std::vector<ModEvent>& eventList)
{
for (auto& i : eventList)
i();
}

void InitOnFrame();
void __cdecl OnInput();
void __cdecl OnFrame();
6 changes: 4 additions & 2 deletions SA2ModLoader/SA2ModLoader.vcxproj
Expand Up @@ -50,7 +50,7 @@
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;SA2MODLOADER_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;WIN32;_DEBUG;_WINDOWS;_USRDLL;SA2MODLOADER_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
Expand All @@ -68,7 +68,7 @@
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;SA2MODLOADER_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;_WINDOWS;_USRDLL;SA2MODLOADER_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
Expand All @@ -87,6 +87,7 @@
<ItemGroup>
<ClInclude Include="AnimationFile.h" />
<ClInclude Include="CodeParser.hpp" />
<ClInclude Include="Events.h" />
<ClInclude Include="FileMap.hpp" />
<ClInclude Include="include\ModLoader\MemAccess.h" />
<ClInclude Include="include\ninja.h" />
Expand All @@ -113,6 +114,7 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
</PrecompiledHeader>
</ClCompile>
<ClCompile Include="Events.cpp" />
<ClCompile Include="FileMap.cpp" />
<ClCompile Include="IniFile.cpp" />
<ClCompile Include="LandTableInfo.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions SA2ModLoader/SA2ModLoader.vcxproj.filters
Expand Up @@ -69,6 +69,9 @@
<ClInclude Include="TextConv.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Events.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
Expand Down Expand Up @@ -98,5 +101,8 @@
<ClCompile Include="TextConv.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Events.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
105 changes: 8 additions & 97 deletions SA2ModLoader/dllmain.cpp
Expand Up @@ -13,8 +13,13 @@
#include "SA2ModLoader.h"
#include "ModelInfo.h"
#include "CodeParser.hpp"
#include "Events.h"
using namespace std;

// TODO: Split file replacement into separate file(s)

#pragma region INI stuff

typedef unordered_map<string, string> IniGroup;
struct IniGroupStr { IniGroup Element; };
typedef unordered_map<string, IniGroupStr> IniDictionary;
Expand Down Expand Up @@ -94,6 +99,8 @@ IniDictionary LoadINI(istream &textfile)
return result;
}

#pragma endregion

HMODULE myhandle;
HMODULE datadllhandle;
FARPROC __stdcall MyGetProcAddress(HMODULE hModule, LPCSTR lpProcName)
Expand All @@ -106,10 +113,7 @@ FARPROC __stdcall MyGetProcAddress(HMODULE hModule, LPCSTR lpProcName)

inline int backslashes(int c)
{
if (c == '/')
return '\\';
else
return c;
return (c == '/') ? '\\' : c;
}

IniGroup settings;
Expand Down Expand Up @@ -430,99 +434,6 @@ void HookTheAPI()
}
}

static CodeParser codeParser;

/**
* Registers an event to the specified event list.
* @param eventList The event list to add to.
* @param module The module for the mod DLL.
* @param name The name of the exported function from the module (i.e OnFrame)
*/
static void RegisterEvent(vector<ModEvent>& eventList, HMODULE module, const char* name)
{
const ModEvent modEvent = (const ModEvent)GetProcAddress(module, name);

if (modEvent != nullptr)
eventList.push_back(modEvent);
}

/**
* Calls all registered events in the specified event list.
* @param eventList The list of events to trigger.
*/
static inline void RaiseEvents(const vector<ModEvent>& eventList)
{
for (auto& i : eventList)
i();
}

static vector<ModEvent> modFrameEvents;
void __cdecl OnFrame()
{
codeParser.processCodeList();
RaiseEvents(modFrameEvents);
}

static vector<ModEvent> modInputEvents;
static void __cdecl OnInput()
{
RaiseEvents(modInputEvents);
}

#pragma region OnFrame Initialization

void* caseDefault_ptr = (void*)0x004340CC;
void* case08_ptr = (void*)0x0043405D;
void* case09_ptr = (void*)0x0043407E;
void* case10_ptr = (void*)0x0043407E;

void __declspec(naked) OnFrame_MidJump()
{
__asm
{
push eax
call OnFrame
pop eax

pop edi
pop esi
pop ebp
pop ebx
pop ecx
retn
}
}

void* OnFrame_Hook_ptr = (void*)0x004340E7;
void __declspec(naked) OnFrame_Hook()
{
__asm
{
push eax
call OnFrame
pop eax
retn
}
}

void InitOnFrame()
{
WriteJump(case08_ptr, OnFrame_MidJump);
WriteJump(case09_ptr, OnFrame_MidJump);
WriteJump(case10_ptr, OnFrame_MidJump);

// OnFrame caseDefault
// Occurs if the current game mode isn't 8, 9 or 10, and byte_174AFF9 == 1
WriteJump(caseDefault_ptr, OnFrame_MidJump);

// OnFrame OnFrame_Hook
// Occurs at the end of the function (effectively the "else" to the statement above)
WriteJump(OnFrame_Hook_ptr, OnFrame_Hook);
}

#pragma endregion


char *ShiftJISToUTF8(char *shiftjis)
{
int cchWcs = MultiByteToWideChar(932, 0, shiftjis, -1, NULL, 0);
Expand Down
29 changes: 15 additions & 14 deletions SA2ModLoader/stdafx.h
Expand Up @@ -14,17 +14,18 @@


// TODO: reference additional headers your program requires here
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

#include "ninja.h"
#include "SA2ModLoader.h"
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

#include "ninja.h"
#include "SA2ModLoader.h"
#include "Events.h"

0 comments on commit 2cebbb2

Please sign in to comment.