Skip to content

Commit 0aae5f4

Browse files
committed
Converted inline assembly to NASM assembly; Makefile refactored to separate debug and release builds
1 parent 7ece896 commit 0aae5f4

File tree

12 files changed

+121
-171
lines changed

12 files changed

+121
-171
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# CMake generated files
22
out
33

4-
# nvim Coc clangd lsp
5-
compile_flags.txt
6-
74
# Visual Studio nuances
85
# Created by https://www.toptal.com/developers/gitignore/api/visualstudio,c++
96
# Edit at https://www.toptal.com/developers/gitignore?templates=visualstudio,c++

Makefile

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
PROJECT = sp3
22

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

66
LD = i686-w64-mingw32-gcc
7-
LDFLAGS =
7+
LDFLAGS = -shared
8+
9+
ASM = nasm
10+
ASFLAGS = -f win32
811

912
BIN = bin
1013
BUILD = build
@@ -14,33 +17,45 @@ RELEASE = $(OBJ)/release
1417
SRC = src
1518
OBJ = build
1619
SOURCES = $(wildcard $(SRC)/*.c)
17-
OBJECTS = $(patsubst $(SRC)/%.c,$(OBJ)/%.o,$(SOURCES))
18-
20+
DBG_OBJECTS = $(patsubst $(SRC)/%.c,$(DEBUG)/%.o,$(SOURCES))
21+
REL_OBJECTS = $(patsubst $(SRC)/%.c,$(RELEASE)/%.o,$(SOURCES))
1922

2023
INCLUDE = include
21-
INCLUDES = -I$(INCLUDE)
24+
INCLUDES = $(addprefix -I,$(INCLUDE))
2225

23-
LIB_FILES = -ld3d9 -ld3dx9
24-
LIBS = $(LIB_FILES)
26+
LIB_FILES = d3d9 d3dx9
27+
LIBS = $(addprefix -l,$(LIB_FILES))
28+
29+
ASM_TARGET = healthDetour
30+
ASM_SRC = $(SRC)/healthDetour.asm
31+
ASM_OBJ = $(OBJ)/healthDetour.o
2532

2633
all: debug release
2734

2835
debug: $(DEBUG)
2936
release: $(PROJECT)
3037

31-
$(DEBUG): CFLAGS+=-g -DDEBUG
32-
$(DEBUG): $(OBJ) $(BIN) $(OBJECTS)
33-
$(CC) $(CFLAGS) $(OBJECTS) $(LIBS) -o $(BIN)/$(PROJECT)_d.dll
38+
$(DEBUG): CFLAGS += -g
39+
$(DEBUG): $(OBJ) $(BIN) $(ASM_OBJ) $(DBG_OBJECTS)
40+
$(LD) $(LDFLAGS) $(ASM_OBJ) $(DBG_OBJECTS) $(LIBS) -o $(BIN)/$(PROJECT)_d.dll
41+
42+
$(PROJECT): CFLAGS += -O3 -fno-ident -fvisibility=hidden
43+
$(PROJECT): LDFLAGS += -s
44+
$(PROJECT): $(OBJ) $(BIN) $(REL_OBJECTS)
45+
$(LD) $(LDFLAGS) $(ASM_OBJ) $(REL_OBJECTS) $(LIBS) -o $(BIN)/$(PROJECT).dll
3446

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

39-
$(OBJECTS): $(OBJ)/%.o: $(SRC)/%.c
50+
$(DBG_OBJECTS): $(DEBUG)/%.o: $(SRC)/%.c
51+
$(CC) $(CFLAGS) $(INCLUDES) -c $^ -o $@
52+
53+
$(REL_OBJECTS): $(RELEASE)/%.o: $(SRC)/%.c
4054
$(CC) $(CFLAGS) $(INCLUDES) -c $^ -o $@
4155

4256
$(OBJ):
43-
mkdir -p $@
57+
mkdir -p $@/debug
58+
mkdir -p $@/release
4459

4560
$(BIN):
4661
mkdir -p $@

README.md

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- Switch from C to CPP: 08/18/2021
55
- Project completed: 08/19/2021
66
- Switch from CPP to C: 08/20/2021
7+
- Project revisited: 12/09/2021
78

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

43-
## Build Instructions
44-
- WSL
45-
1. install cmake for windows
46-
```
47-
$ ./install_script.sh
48-
```
49-
- CMake
50-
1. install cmake for windows
51-
- Visual Studio 2019
52-
1. open the project folder in Visual Studio 2019
53-
2. ctrl + shift + b
54-
- Powershell
55-
1. open Powershell in project directory
56-
```
57-
$ cmake -G "Visual Studio 2019" -A Win32 -B "build"
58-
$ cmake --build "build" --config "Release"
59-
```
44+
## Build Instructions (Debian)
45+
#### Toolchain
46+
```bash
47+
# Install toolchain for the required compiler
48+
chmod +x ./install-toolchain.sh
49+
sudo ./install-toolchain.sh # Modify it as you please before executing
50+
```
51+
#### Build
52+
```bash
53+
make # defaults to both release and debug builds
54+
```
6055

6156
## Known Issues
6257
If the user presses F3 while the menu is minimized, it will reset position.
63-
64-
## Goals
65-
- [ ] ~~Find out how to draw text~~

TODO.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
- [ ] Remove inline assembly and change it into .asm
21
- [ ] Refactor Makefile
2+
- [x] Create toolchain script for contribution purposes
3+
- [x] Update README
4+
- [x] Remove inline assembly and change it into .asm
35
- [x] Replace tabs with spaces
46
- [x] Create event.h/c and move handle keyboard to event "namespace"
57
- [x] Create render.h/c and move handle keyboard to render "namespace"

compile_flags.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-std=c99
2+
--target=i686-pc-windows
3+
-I/usr/lib/gcc/i686-w64-mingw32/9.3-win32/include
4+
-I/usr/lib/gcc/i686-w64-mingw32/9.3-win32/include-fixed
5+
-I/usr/lib/gcc/i686-w64-mingw32/9.3-win32/../../../../i686-w64-mingw32/include
6+
-Iinclude
7+
-Wall
8+
-Wextra
9+
-Werror
10+
-pedantic

include/mem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @param: uintptr_t ptr, unsigned offsets[], size_t size
1212
* @rype: uintptr_t
1313
*/
14-
uintptr_t FindDMAddress(uintptr_t ptr, unsigned offsets[], size_t size);
14+
uintptr_t FindDynamicAddress(uintptr_t ptr, unsigned offsets[], size_t size);
1515

1616
/**
1717
* Byte replacement from source to destination.

install-toolchain.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Creator: VPR
2+
# Created: December 9, 2021
3+
# Updated: December 9, 2021
4+
5+
# Gets the required mingw compiler
6+
7+
set -o pipefail
8+
set -o errexit
9+
set -o nounset
10+
set -o xtrace
11+
12+
apt update && apt upgrade -y
13+
apt install -y --no-install-recommends \
14+
mingw-w64 \
15+
mingw-w64-common \
16+
mingw-w64-i686-dev \
17+
mingw-w64-x86-64-dev \

src/d3d9hook.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ static HWND g_window;
44

55
#pragma GCC diagnostic push
66
#pragma GCC diagnostic ignored "-Wunused-parameter"
7-
BOOL CALLBACK EnumWindowsCallback(HWND handle, LPARAM lpParam)
7+
BOOL CALLBACK EnumWindowsCallback(HWND handle, LPARAM lParam)
88
{
99
DWORD wndProcId;
1010
GetWindowThreadProcessId(handle, &wndProcId);

src/hacks.c

Lines changed: 19 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ extern bool bGhostMode;
1919
extern bool bShutdown;
2020
extern bool bGodMode;
2121

22+
/*void healthDetour(void); // maybe this works?*/
23+
2224
void hack_GodMode(bool bGodMode)
2325
{
2426
const char* health_op = (char *)(module_base_addr + offsets_health_base);
@@ -29,11 +31,18 @@ void hack_GodMode(bool bGodMode)
2931

3032
if (bGodMode)
3133
{
34+
#pragma GCC diagnostic push
35+
#pragma GCC diagnostic ignored "-Wpedantic"
3236
Detour((void *)health_op, (void *)healthDetour, health_op_size);
37+
#pragma GCC diagnostic pop
38+
3339
}
3440
else
3541
{
42+
#pragma GCC diagnostic push
43+
#pragma GCC diagnostic ignored "-Wpedantic"
3644
Patch((BYTE *)health_op, (BYTE *)health_original, health_op_size);
45+
#pragma GCC diagnostic pop
3746
}
3847

3948
}
@@ -54,13 +63,19 @@ void hack_GhostMode(bool bGhostMode)
5463

5564
if (bGhostMode)
5665
{
66+
#pragma GCC diagnostic push
67+
#pragma GCC diagnostic ignored "-Wpedantic"
5768
Patch((BYTE *)visibility_op, (BYTE *)visibility_patch, visibility_size);
5869
Patch((BYTE *)noise_op, (BYTE *)noise_patch, noise_size);
70+
#pragma GCC diagnostic pop
5971
}
6072
else
6173
{
74+
#pragma GCC diagnostic push
75+
#pragma GCC diagnostic ignored "-Wpedantic"
6276
Patch((BYTE *)visibility_op, (BYTE *)visibility_original, visibility_size);
6377
Patch((BYTE *)noise_op, (BYTE *)noise_original, noise_size);
78+
#pragma GCC diagnostic pop
6479
}
6580

6681
}
@@ -174,11 +189,11 @@ void hack_DisableAlarms(bool bDisableAlarms)
174189

175190
unsigned int hack_DisableEnemies(bool bDisableEnemies)
176191
{
177-
EntityList* entity_list = *(EntityList **)FindDMAddress(module_base_addr + offsets_entity_list_base,
192+
EntityList* entity_list = *(EntityList **)FindDynamicAddress(module_base_addr + offsets_entity_list_base,
178193
offsets_entity_list_pointers,
179194
offsets_entity_list_pointers_size);
180195

181-
size_t entity_list_size = *((int *)(FindDMAddress(module_base_addr + offsets_entity_list_base,
196+
size_t entity_list_size = *((int *)(FindDynamicAddress(module_base_addr + offsets_entity_list_base,
182197
offsets_entity_list_pointers,
183198
offsets_entity_list_pointers_size)) + 1);
184199

@@ -206,11 +221,11 @@ unsigned int hack_DisableEnemies(bool bDisableEnemies)
206221

207222
unsigned int hack_UnlockAllDoors(void)
208223
{
209-
EntityList* _entity_list = *(EntityList **)FindDMAddress(module_base_addr + offsets_entity_list_base,
224+
EntityList* _entity_list = *(EntityList **)FindDynamicAddress(module_base_addr + offsets_entity_list_base,
210225
offsets_entity_list_pointers,
211226
offsets_entity_list_pointers_size);
212227

213-
size_t size = *((int *)(FindDMAddress(module_base_addr + offsets_entity_list_base,
228+
size_t size = *((int *)(FindDynamicAddress(module_base_addr + offsets_entity_list_base,
214229
offsets_entity_list_pointers,
215230
offsets_entity_list_pointers_size)) + 1);
216231

@@ -239,83 +254,3 @@ unsigned int hack_UnlockAllDoors(void)
239254

240255
return n_doors_unlocked;
241256
}
242-
243-
/*void hack_InitializeMenuItems()*/
244-
/*{*/
245-
/*strcpy(hackMenu[GOD_MODE].name, "1: God Mode");*/
246-
/*strcpy(hackMenu[GHOST_MODE].name, "2: Ghost Mode");*/
247-
/*strcpy(hackMenu[SUPER_WEAPONS].name, "3: Super Weapons");*/
248-
/*strcpy(hackMenu[DISABLE_ALARMS].name, "4: Disable Alarms");*/
249-
/*strcpy(hackMenu[DISABLE_ENEMIES].name, "5: Disable Enemies");*/
250-
/*strcpy(hackMenu[UNLOCK_ALL_DOORS].name, "6: Unlock All Doors");*/
251-
/*}*/
252-
253-
/*void hack_Menu(IDirect3DDevice9* d3dDevice)*/
254-
/*{*/
255-
/*resolution = *((Resolution *)(0x0009D2A8));*/
256-
257-
/*float factor = 1.0;*/
258-
/*if (bMaximizeMenu)*/
259-
/*{*/
260-
/*// Title Template*/
261-
/*draw_DrawFilledRect(coordinates.x, coordinates.y, 140, 100, color_DarkGrey, d3dDevice);*/
262-
/*draw_DrawBorderBox(coordinates.x, coordinates.y, 140, 100, 4, color_Black, d3dDevice);*/
263-
264-
/*// Row one*/
265-
/*int x1 = 20;*/
266-
/*int y1 = 15;*/
267-
/*for (int i = 3; i < MAX_MENU_ITEMS; i++)*/
268-
/*{*/
269-
/*// If hack is on we display the text colour in green*/
270-
/*draw_DrawFilledRect(coordinates.x + x1, coordinates.y + y1, 25, 20, hackMenu[i].bEnabled ? color_Green : color_LightGrey, d3dDevice);*/
271-
/*draw_DrawBorderBox(coordinates.x + x1, coordinates.y + y1, 25, 20, 2, color_Black, d3dDevice);*/
272-
273-
/*//used to position the next item below*/
274-
/*x1 += 40;*/
275-
/*}*/
276-
/*// Row two*/
277-
/*int x2 = 20;*/
278-
/*int y2 = 55;*/
279-
/*for (int i = 0; i < MAX_MENU_ITEMS - 3; i++)*/
280-
/*{*/
281-
/*// If hack is on we display the text colour in green*/
282-
/*draw_DrawFilledRect(coordinates.x + x2, coordinates.y + y2, 25, 20, hackMenu[i].bEnabled ? color_Green : color_LightGrey, d3dDevice);*/
283-
/*draw_DrawBorderBox(coordinates.x + x2, coordinates.y + y2, 25, 20, 2, color_Black, d3dDevice);*/
284-
285-
/*//used to position the next item*/
286-
/*x2 += 40;*/
287-
/*}*/
288-
/*}*/
289-
/*else*/
290-
/*{*/
291-
/*factor = 0.25;*/
292-
/*// Title Template*/
293-
/*draw_DrawFilledRect(30, 20, (int)(factor*140), (int)(factor*100), color_DarkGrey, d3dDevice);*/
294-
/*draw_DrawBorderBox(30, 20, (int)(factor*140), (int)(factor*100), 2, color_Black, d3dDevice);*/
295-
296-
/*// Row one*/
297-
/*int x1 = 35;*/
298-
/*int y1 = 25;*/
299-
/*for (int i = 3; i < MAX_MENU_ITEMS; i++)*/
300-
/*{*/
301-
/*// If hack is on we display the text colour in green*/
302-
/*draw_DrawFilledRect(x1, y1, (int)(factor*20), (int)(factor*20), hackMenu[i].bEnabled ? color_Green : color_LightGrey, d3dDevice);*/
303-
/*draw_DrawBorderBox(x1, y1, (int)(factor*20), (int)(factor*20), 1, color_Black, d3dDevice);*/
304-
305-
/*//used to position the next item below*/
306-
/*x1 += (int)(factor*40);*/
307-
/*}*/
308-
/*// Row two*/
309-
/*int x2 = 35;*/
310-
/*int y2 = 35;*/
311-
/*for (int i = 0; i < MAX_MENU_ITEMS - 3; i++)*/
312-
/*{*/
313-
/*// If hack is on we display the text colour in green*/
314-
/*draw_DrawFilledRect(x2, y2, (int)(factor*20), (int)(factor*20), hackMenu[i].bEnabled ? color_Green : color_LightGrey, d3dDevice);*/
315-
/*draw_DrawBorderBox(x2, y2, (int)(factor*20), (int)(factor*20), 1, color_Black, d3dDevice);*/
316-
317-
/*//used to position the next item*/
318-
/*x2 += (int)(factor*40);*/
319-
/*}*/
320-
/*}*/
321-
/*}*/

src/healthDetour.asm

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
segment .text
2+
3+
global _healthDetour
4+
5+
_healthDetour:
6+
cmp dword [edi], 0x110E8B50
7+
je $ + 0x08
8+
xor eax, eax
9+
mov eax, eax
10+
mov dword [ebx], eax
11+
mov ebx, eax
12+
mov eax, dword [esp + 0x14]
13+
pop esi
14+
mov dword [eax], ebx
15+
pop ebx
16+
pop ecx
17+
ret 0x8

src/main.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ DWORD WINAPI MainThread(LPVOID lpReserved)
5656

5757
if (GetD3D9Device(d3d9Device, sizeof(d3d9Device)))
5858
{
59-
oEndScene = (tEndScene)TrampHook((char*)d3d9Device[42], (char*)hkEndScene, 7);
59+
#pragma GCC diagnostic push
60+
#pragma GCC diagnostic ignored "-Wpedantic"
61+
oEndScene = (tEndScene)TrampHook((char *)d3d9Device[42], (char *)hkEndScene, 7);
62+
#pragma GCC diagnostic pop
6063
}
6164

6265
return TRUE;

0 commit comments

Comments
 (0)