Skip to content

Commit

Permalink
Split dllmain into several separate files to help with compile time. …
Browse files Browse the repository at this point in the history
…Still more work to do... maybe.
  • Loading branch information
michael-fadely committed Jul 22, 2015
1 parent 8208aa3 commit 45ecc50
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 136 deletions.
36 changes: 36 additions & 0 deletions SADXModLoader/Events.cpp
@@ -0,0 +1,36 @@
#include "stdafx.h"
#include "Events.h"

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

DataPointer(short, word_3B2C464, 0x3B2C464);

/**
* 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 OnInput()
{
RaiseEvents(modInputEvents);
}

void __declspec(naked) OnInput_MidJump()
{
__asm
{
inc word_3B2C464
pop esi
jmp OnInput
}
}
28 changes: 28 additions & 0 deletions SADXModLoader/Events.h
@@ -0,0 +1,28 @@
#pragma once

#include "SADXModLoader.h"
#include <vector>

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

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

/**
* 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);

void __cdecl OnInput();
void __cdecl OnInput_MidJump();
33 changes: 33 additions & 0 deletions SADXModLoader/FileReplacement.cpp
@@ -0,0 +1,33 @@
#include "stdafx.h"
#include "FileReplacement.h"

// File replacement map.
// NOTE: Do NOT mark this as static.
// MediaFns.cpp needs to access the FileMap.
FileMap sadx_fileMap;

/**
* CreateFileA() wrapper using _ReplaceFile().
* @param lpFileName
* @param dwDesiredAccess
* @param dwShareMode
* @param lpSecurityAttibutes
* @param dwCreationDisposition
* @param dwFlagsAndAttributes
* @param hTemplateFile
* @return
*/
HANDLE __stdcall MyCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
{
return CreateFileA(sadx_fileMap.replaceFile(lpFileName), dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
}

/**
* C wrapper to call sadx_fileMap.replaceFile() from asm.
* @param lpFileName Filename.
* @return Replaced filename, or original filename if not replaced by a mod.
*/
const char *_ReplaceFile(const char *lpFileName)
{
return sadx_fileMap.replaceFile(lpFileName);
}
10 changes: 10 additions & 0 deletions SADXModLoader/FileReplacement.h
@@ -0,0 +1,10 @@
#pragma once

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include "FileMap.hpp"

extern FileMap sadx_fileMap;

HANDLE __stdcall MyCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);
const char * _ReplaceFile(const char * lpFileName);
25 changes: 22 additions & 3 deletions SADXModLoader/MediaFns.cpp
Expand Up @@ -5,6 +5,7 @@

#include "stdafx.h"
#include "MediaFns.hpp"
#include "FileReplacement.h"

#include "bass_vgmstream.h"

Expand All @@ -17,9 +18,6 @@ static bool enablevgmstream = false;
static bool musicwmp = true;
static DWORD basschan = 0;

// FileMap from dllmain.cpp.
#include "FileMap.hpp"
extern FileMap sadx_fileMap;

/**
* Initialize media playback.
Expand Down Expand Up @@ -213,3 +211,24 @@ void WMPRelease_r()
{
BASS_Free();
}

__declspec(naked) int PlayVideoFile_r()
{
__asm
{
mov eax, [esp + 4]
push esi
push eax
call _ReplaceFile
add esp, 4
pop esi
mov[esp + 4], eax
jmp PlayVideoFilePtr
}
}

int __cdecl PlayVoiceFile_r(LPCSTR filename)
{
filename = sadx_fileMap.replaceFile(filename);
return PlayVoiceFile(filename);
}
1 change: 1 addition & 0 deletions SADXModLoader/MediaFns.hpp
Expand Up @@ -112,6 +112,7 @@ void __cdecl WMPClose_r(int a1);
void WMPRelease_r();

int PlayVideoFile_r(void);
int __cdecl PlayVoiceFile_r(LPCSTR filename);

#ifdef __cplusplus
}
Expand Down
4 changes: 4 additions & 0 deletions SADXModLoader/SADXModLoader.vcxproj
Expand Up @@ -13,7 +13,9 @@
<ItemGroup>
<ClCompile Include="CodeParser.cpp" />
<ClCompile Include="dllmain.cpp" />
<ClCompile Include="Events.cpp" />
<ClCompile Include="FileMap.cpp" />
<ClCompile Include="FileReplacement.cpp" />
<ClCompile Include="IniFile.cpp" />
<ClCompile Include="MediaFns.cpp" />
<ClCompile Include="stdafx.cpp">
Expand All @@ -25,7 +27,9 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="CodeParser.hpp" />
<ClInclude Include="Events.h" />
<ClInclude Include="FileMap.hpp" />
<ClInclude Include="FileReplacement.h" />
<ClInclude Include="git.h" />
<ClInclude Include="include\ModLoader\MemAccess.h" />
<ClInclude Include="include\ninja.h" />
Expand Down
12 changes: 12 additions & 0 deletions SADXModLoader/SADXModLoader.vcxproj.filters
Expand Up @@ -48,6 +48,12 @@
<ClCompile Include="stdafx.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Events.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="FileReplacement.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="MediaFns.hpp">
Expand Down Expand Up @@ -95,5 +101,11 @@
<ClInclude Include="include\ModLoader\MemAccess.h">
<Filter>Header Files\include\ModLoader</Filter>
</ClInclude>
<ClInclude Include="Events.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="FileReplacement.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

0 comments on commit 45ecc50

Please sign in to comment.