Skip to content

Commit

Permalink
Converted inline assembly to NASM assembly; Makefile refactored to se…
Browse files Browse the repository at this point in the history
…parate debug and release builds
  • Loading branch information
0xvpr committed Dec 9, 2021
1 parent 7ece896 commit 0aae5f4
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 171 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# CMake generated files
out

# nvim Coc clangd lsp
compile_flags.txt

# Visual Studio nuances
# Created by https://www.toptal.com/developers/gitignore/api/visualstudio,c++
# Edit at https://www.toptal.com/developers/gitignore?templates=visualstudio,c++
Expand Down
45 changes: 30 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
PROJECT = sp3

CC = i686-w64-mingw32-gcc
CFLAGS = -std=c99 -masm=intel -Wall -Wextra -Werror -shared
CFLAGS = -std=c99 -masm=intel -pedantic -Wall -Wextra -Werror -shared

LD = i686-w64-mingw32-gcc
LDFLAGS =
LDFLAGS = -shared

ASM = nasm
ASFLAGS = -f win32

BIN = bin
BUILD = build
Expand All @@ -14,33 +17,45 @@ RELEASE = $(OBJ)/release
SRC = src
OBJ = build
SOURCES = $(wildcard $(SRC)/*.c)
OBJECTS = $(patsubst $(SRC)/%.c,$(OBJ)/%.o,$(SOURCES))

DBG_OBJECTS = $(patsubst $(SRC)/%.c,$(DEBUG)/%.o,$(SOURCES))
REL_OBJECTS = $(patsubst $(SRC)/%.c,$(RELEASE)/%.o,$(SOURCES))

INCLUDE = include
INCLUDES = -I$(INCLUDE)
INCLUDES = $(addprefix -I,$(INCLUDE))

LIB_FILES = -ld3d9 -ld3dx9
LIBS = $(LIB_FILES)
LIB_FILES = d3d9 d3dx9
LIBS = $(addprefix -l,$(LIB_FILES))

ASM_TARGET = healthDetour
ASM_SRC = $(SRC)/healthDetour.asm
ASM_OBJ = $(OBJ)/healthDetour.o

all: debug release

debug: $(DEBUG)
release: $(PROJECT)

$(DEBUG): CFLAGS+=-g -DDEBUG
$(DEBUG): $(OBJ) $(BIN) $(OBJECTS)
$(CC) $(CFLAGS) $(OBJECTS) $(LIBS) -o $(BIN)/$(PROJECT)_d.dll
$(DEBUG): CFLAGS += -g
$(DEBUG): $(OBJ) $(BIN) $(ASM_OBJ) $(DBG_OBJECTS)
$(LD) $(LDFLAGS) $(ASM_OBJ) $(DBG_OBJECTS) $(LIBS) -o $(BIN)/$(PROJECT)_d.dll

$(PROJECT): CFLAGS += -O3 -fno-ident -fvisibility=hidden
$(PROJECT): LDFLAGS += -s
$(PROJECT): $(OBJ) $(BIN) $(REL_OBJECTS)
$(LD) $(LDFLAGS) $(ASM_OBJ) $(REL_OBJECTS) $(LIBS) -o $(BIN)/$(PROJECT).dll

$(PROJECT): CFLAGS+=-s -O2
$(PROJECT): $(OBJ) $(BIN) $(OBJECTS)
$(CC) $(CFLAGS) $(OBJECTS) $(LIBS) -o $(BIN)/$(PROJECT).dll
$(ASM_OBJ): $(OBJ)/%.o: $(SRC)/%.asm
$(ASM) $(ASFLAGS) $^ -o $@

$(OBJECTS): $(OBJ)/%.o: $(SRC)/%.c
$(DBG_OBJECTS): $(DEBUG)/%.o: $(SRC)/%.c
$(CC) $(CFLAGS) $(INCLUDES) -c $^ -o $@

$(REL_OBJECTS): $(RELEASE)/%.o: $(SRC)/%.c
$(CC) $(CFLAGS) $(INCLUDES) -c $^ -o $@

$(OBJ):
mkdir -p $@
mkdir -p $@/debug
mkdir -p $@/release

$(BIN):
mkdir -p $@
Expand Down
32 changes: 12 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Switch from C to CPP: 08/18/2021
- Project completed: 08/19/2021
- Switch from CPP to C: 08/20/2021
- Project revisited: 12/09/2021

## Overview & Demonstration
The main purpose of this project was to get familiar with the C language while also
Expand Down Expand Up @@ -40,26 +41,17 @@ Once the DLL is injected, you will have access to the following Hacks:
- Numpad 5: Disable Enemies
- Numpad 6: Unlock All Doors

## Build Instructions
- WSL
1. install cmake for windows
```
$ ./install_script.sh
```
- CMake
1. install cmake for windows
- Visual Studio 2019
1. open the project folder in Visual Studio 2019
2. ctrl + shift + b
- Powershell
1. open Powershell in project directory
```
$ cmake -G "Visual Studio 2019" -A Win32 -B "build"
$ cmake --build "build" --config "Release"
```
## Build Instructions (Debian)
#### Toolchain
```bash
# Install toolchain for the required compiler
chmod +x ./install-toolchain.sh
sudo ./install-toolchain.sh # Modify it as you please before executing
```
#### Build
```bash
make # defaults to both release and debug builds
```

## Known Issues
If the user presses F3 while the menu is minimized, it will reset position.
## Goals
- [ ] ~~Find out how to draw text~~
4 changes: 3 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
- [ ] Remove inline assembly and change it into .asm
- [ ] Refactor Makefile
- [x] Create toolchain script for contribution purposes
- [x] Update README
- [x] Remove inline assembly and change it into .asm
- [x] Replace tabs with spaces
- [x] Create event.h/c and move handle keyboard to event "namespace"
- [x] Create render.h/c and move handle keyboard to render "namespace"
10 changes: 10 additions & 0 deletions compile_flags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-std=c99
--target=i686-pc-windows
-I/usr/lib/gcc/i686-w64-mingw32/9.3-win32/include
-I/usr/lib/gcc/i686-w64-mingw32/9.3-win32/include-fixed
-I/usr/lib/gcc/i686-w64-mingw32/9.3-win32/../../../../i686-w64-mingw32/include
-Iinclude
-Wall
-Wextra
-Werror
-pedantic
2 changes: 1 addition & 1 deletion include/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @param: uintptr_t ptr, unsigned offsets[], size_t size
* @rype: uintptr_t
*/
uintptr_t FindDMAddress(uintptr_t ptr, unsigned offsets[], size_t size);
uintptr_t FindDynamicAddress(uintptr_t ptr, unsigned offsets[], size_t size);

/**
* Byte replacement from source to destination.
Expand Down
17 changes: 17 additions & 0 deletions install-toolchain.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Creator: VPR
# Created: December 9, 2021
# Updated: December 9, 2021

# Gets the required mingw compiler

set -o pipefail
set -o errexit
set -o nounset
set -o xtrace

apt update && apt upgrade -y
apt install -y --no-install-recommends \
mingw-w64 \
mingw-w64-common \
mingw-w64-i686-dev \
mingw-w64-x86-64-dev \
2 changes: 1 addition & 1 deletion src/d3d9hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ static HWND g_window;

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
BOOL CALLBACK EnumWindowsCallback(HWND handle, LPARAM lpParam)
BOOL CALLBACK EnumWindowsCallback(HWND handle, LPARAM lParam)
{
DWORD wndProcId;
GetWindowThreadProcessId(handle, &wndProcId);
Expand Down
103 changes: 19 additions & 84 deletions src/hacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ extern bool bGhostMode;
extern bool bShutdown;
extern bool bGodMode;

/*void healthDetour(void); // maybe this works?*/

void hack_GodMode(bool bGodMode)
{
const char* health_op = (char *)(module_base_addr + offsets_health_base);
Expand All @@ -29,11 +31,18 @@ void hack_GodMode(bool bGodMode)

if (bGodMode)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
Detour((void *)health_op, (void *)healthDetour, health_op_size);
#pragma GCC diagnostic pop

}
else
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
Patch((BYTE *)health_op, (BYTE *)health_original, health_op_size);
#pragma GCC diagnostic pop
}

}
Expand All @@ -54,13 +63,19 @@ void hack_GhostMode(bool bGhostMode)

if (bGhostMode)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
Patch((BYTE *)visibility_op, (BYTE *)visibility_patch, visibility_size);
Patch((BYTE *)noise_op, (BYTE *)noise_patch, noise_size);
#pragma GCC diagnostic pop
}
else
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
Patch((BYTE *)visibility_op, (BYTE *)visibility_original, visibility_size);
Patch((BYTE *)noise_op, (BYTE *)noise_original, noise_size);
#pragma GCC diagnostic pop
}

}
Expand Down Expand Up @@ -174,11 +189,11 @@ void hack_DisableAlarms(bool bDisableAlarms)

unsigned int hack_DisableEnemies(bool bDisableEnemies)
{
EntityList* entity_list = *(EntityList **)FindDMAddress(module_base_addr + offsets_entity_list_base,
EntityList* entity_list = *(EntityList **)FindDynamicAddress(module_base_addr + offsets_entity_list_base,
offsets_entity_list_pointers,
offsets_entity_list_pointers_size);

size_t entity_list_size = *((int *)(FindDMAddress(module_base_addr + offsets_entity_list_base,
size_t entity_list_size = *((int *)(FindDynamicAddress(module_base_addr + offsets_entity_list_base,
offsets_entity_list_pointers,
offsets_entity_list_pointers_size)) + 1);

Expand Down Expand Up @@ -206,11 +221,11 @@ unsigned int hack_DisableEnemies(bool bDisableEnemies)

unsigned int hack_UnlockAllDoors(void)
{
EntityList* _entity_list = *(EntityList **)FindDMAddress(module_base_addr + offsets_entity_list_base,
EntityList* _entity_list = *(EntityList **)FindDynamicAddress(module_base_addr + offsets_entity_list_base,
offsets_entity_list_pointers,
offsets_entity_list_pointers_size);

size_t size = *((int *)(FindDMAddress(module_base_addr + offsets_entity_list_base,
size_t size = *((int *)(FindDynamicAddress(module_base_addr + offsets_entity_list_base,
offsets_entity_list_pointers,
offsets_entity_list_pointers_size)) + 1);

Expand Down Expand Up @@ -239,83 +254,3 @@ unsigned int hack_UnlockAllDoors(void)

return n_doors_unlocked;
}

/*void hack_InitializeMenuItems()*/
/*{*/
/*strcpy(hackMenu[GOD_MODE].name, "1: God Mode");*/
/*strcpy(hackMenu[GHOST_MODE].name, "2: Ghost Mode");*/
/*strcpy(hackMenu[SUPER_WEAPONS].name, "3: Super Weapons");*/
/*strcpy(hackMenu[DISABLE_ALARMS].name, "4: Disable Alarms");*/
/*strcpy(hackMenu[DISABLE_ENEMIES].name, "5: Disable Enemies");*/
/*strcpy(hackMenu[UNLOCK_ALL_DOORS].name, "6: Unlock All Doors");*/
/*}*/

/*void hack_Menu(IDirect3DDevice9* d3dDevice)*/
/*{*/
/*resolution = *((Resolution *)(0x0009D2A8));*/

/*float factor = 1.0;*/
/*if (bMaximizeMenu)*/
/*{*/
/*// Title Template*/
/*draw_DrawFilledRect(coordinates.x, coordinates.y, 140, 100, color_DarkGrey, d3dDevice);*/
/*draw_DrawBorderBox(coordinates.x, coordinates.y, 140, 100, 4, color_Black, d3dDevice);*/

/*// Row one*/
/*int x1 = 20;*/
/*int y1 = 15;*/
/*for (int i = 3; i < MAX_MENU_ITEMS; i++)*/
/*{*/
/*// If hack is on we display the text colour in green*/
/*draw_DrawFilledRect(coordinates.x + x1, coordinates.y + y1, 25, 20, hackMenu[i].bEnabled ? color_Green : color_LightGrey, d3dDevice);*/
/*draw_DrawBorderBox(coordinates.x + x1, coordinates.y + y1, 25, 20, 2, color_Black, d3dDevice);*/

/*//used to position the next item below*/
/*x1 += 40;*/
/*}*/
/*// Row two*/
/*int x2 = 20;*/
/*int y2 = 55;*/
/*for (int i = 0; i < MAX_MENU_ITEMS - 3; i++)*/
/*{*/
/*// If hack is on we display the text colour in green*/
/*draw_DrawFilledRect(coordinates.x + x2, coordinates.y + y2, 25, 20, hackMenu[i].bEnabled ? color_Green : color_LightGrey, d3dDevice);*/
/*draw_DrawBorderBox(coordinates.x + x2, coordinates.y + y2, 25, 20, 2, color_Black, d3dDevice);*/

/*//used to position the next item*/
/*x2 += 40;*/
/*}*/
/*}*/
/*else*/
/*{*/
/*factor = 0.25;*/
/*// Title Template*/
/*draw_DrawFilledRect(30, 20, (int)(factor*140), (int)(factor*100), color_DarkGrey, d3dDevice);*/
/*draw_DrawBorderBox(30, 20, (int)(factor*140), (int)(factor*100), 2, color_Black, d3dDevice);*/

/*// Row one*/
/*int x1 = 35;*/
/*int y1 = 25;*/
/*for (int i = 3; i < MAX_MENU_ITEMS; i++)*/
/*{*/
/*// If hack is on we display the text colour in green*/
/*draw_DrawFilledRect(x1, y1, (int)(factor*20), (int)(factor*20), hackMenu[i].bEnabled ? color_Green : color_LightGrey, d3dDevice);*/
/*draw_DrawBorderBox(x1, y1, (int)(factor*20), (int)(factor*20), 1, color_Black, d3dDevice);*/

/*//used to position the next item below*/
/*x1 += (int)(factor*40);*/
/*}*/
/*// Row two*/
/*int x2 = 35;*/
/*int y2 = 35;*/
/*for (int i = 0; i < MAX_MENU_ITEMS - 3; i++)*/
/*{*/
/*// If hack is on we display the text colour in green*/
/*draw_DrawFilledRect(x2, y2, (int)(factor*20), (int)(factor*20), hackMenu[i].bEnabled ? color_Green : color_LightGrey, d3dDevice);*/
/*draw_DrawBorderBox(x2, y2, (int)(factor*20), (int)(factor*20), 1, color_Black, d3dDevice);*/

/*//used to position the next item*/
/*x2 += (int)(factor*40);*/
/*}*/
/*}*/
/*}*/
17 changes: 17 additions & 0 deletions src/healthDetour.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
segment .text

global _healthDetour

_healthDetour:
cmp dword [edi], 0x110E8B50
je $ + 0x08
xor eax, eax
mov eax, eax
mov dword [ebx], eax
mov ebx, eax
mov eax, dword [esp + 0x14]
pop esi
mov dword [eax], ebx
pop ebx
pop ecx
ret 0x8
5 changes: 4 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ DWORD WINAPI MainThread(LPVOID lpReserved)

if (GetD3D9Device(d3d9Device, sizeof(d3d9Device)))
{
oEndScene = (tEndScene)TrampHook((char*)d3d9Device[42], (char*)hkEndScene, 7);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
oEndScene = (tEndScene)TrampHook((char *)d3d9Device[42], (char *)hkEndScene, 7);
#pragma GCC diagnostic pop
}

return TRUE;
Expand Down
Loading

0 comments on commit 0aae5f4

Please sign in to comment.