Skip to content

Commit cdd29b0

Browse files
committed
Move debug Console to a separate file
1 parent 589e0e5 commit cdd29b0

File tree

5 files changed

+209
-201
lines changed

5 files changed

+209
-201
lines changed

Phobos.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
<ClCompile Include="src\Misc\Hooks.Savegame.cpp" />
105105
<ClCompile Include="src\Misc\Selection.cpp" />
106106
<ClCompile Include="src\Utilities\Debug.cpp" />
107+
<ClCompile Include="src\Utilities\Console.cpp" />
107108
<ClCompile Include="src\Ext\BuildingType\Body.cpp" />
108109
<ClCompile Include="src\Ext\BuildingType\Hooks.cpp" />
109110
<ClCompile Include="src\Ext\WarheadType\Body.cpp" />

src/Utilities/Console.cpp

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#include "Console.h"
2+
#include "Debug.h"
3+
#include <CRT.h>
4+
#include <cstdio>
5+
6+
static DWORD _Real_Debug_Log = 0x4A4AF9;
7+
void __declspec(naked) _Fake_Debug_Log()
8+
{
9+
// va_start(args, pFormat);
10+
// Console::WriteWithVArgs(pFormat, args);
11+
// // va_end(args);
12+
// No need to use va_end here.
13+
//
14+
// As Console::WriteWithVArgs uses __fastcall,
15+
// ECX: pFormat, EDX: args
16+
__asm { mov ecx, [esp + 0x4] }
17+
__asm { lea edx, [esp + 0x8] }
18+
__asm { call Console::WriteWithVArgs }
19+
// __asm { mov edx, 0}
20+
21+
// goto original bytes
22+
__asm { mov eax, _Real_Debug_Log }
23+
__asm { jmp eax }
24+
}
25+
26+
Console::ConsoleTextAttribute Console::TextAttribute;
27+
HANDLE Console::ConsoleHandle;
28+
29+
bool Console::Create()
30+
{
31+
if (FALSE == AllocConsole())
32+
return false;
33+
34+
ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
35+
if (NULL == ConsoleHandle)
36+
return false;
37+
38+
SetConsoleTitle("Phobos Debug Console");
39+
40+
CONSOLE_SCREEN_BUFFER_INFO csbi;
41+
GetConsoleScreenBufferInfo(ConsoleHandle, &csbi);
42+
TextAttribute.AsWord = csbi.wAttributes;
43+
44+
PatchLog(0x4A4AC0, _Fake_Debug_Log, &_Real_Debug_Log);
45+
PatchLog(0x4068E0, _Fake_Debug_Log, nullptr);
46+
47+
return true;
48+
}
49+
50+
void Console::Release()
51+
{
52+
if (NULL != ConsoleHandle)
53+
FreeConsole();
54+
}
55+
56+
void Console::SetForeColor(ConsoleColor color)
57+
{
58+
if (NULL == ConsoleHandle)
59+
return;
60+
61+
if (TextAttribute.Foreground == color)
62+
return;
63+
64+
TextAttribute.Foreground = color;
65+
SetConsoleTextAttribute(ConsoleHandle, TextAttribute.AsWord);
66+
}
67+
68+
void Console::SetBackColor(ConsoleColor color)
69+
{
70+
if (NULL == ConsoleHandle)
71+
return;
72+
73+
if (TextAttribute.Background == color)
74+
return;
75+
76+
TextAttribute.Background = color;
77+
SetConsoleTextAttribute(ConsoleHandle, TextAttribute.AsWord);
78+
}
79+
80+
void Console::EnableUnderscore(bool enable)
81+
{
82+
if (NULL == ConsoleHandle)
83+
return;
84+
85+
if (TextAttribute.Underscore == enable)
86+
return;
87+
88+
TextAttribute.Underscore = enable;
89+
SetConsoleTextAttribute(ConsoleHandle, TextAttribute.AsWord);
90+
}
91+
92+
void Console::Write(const char* str, int len)
93+
{
94+
if (NULL != ConsoleHandle)
95+
WriteConsole(ConsoleHandle, str, len, nullptr, nullptr);
96+
}
97+
98+
void Console::WriteLine(const char* str, int len)
99+
{
100+
Write(str, len);
101+
Write("\n");
102+
}
103+
104+
void __fastcall Console::WriteWithVArgs(const char* pFormat, va_list args)
105+
{
106+
vsprintf_s(Debug::StringBuffer, pFormat, args);
107+
Write(Debug::StringBuffer, strlen(Debug::StringBuffer));
108+
}
109+
110+
void Console::WriteFormat(const char* pFormat, ...)
111+
{
112+
va_list args;
113+
va_start(args, pFormat);
114+
WriteWithVArgs(pFormat, args);
115+
va_end(args);
116+
}
117+
118+
void Console::PatchLog(DWORD dwAddr, void* fakeFunc, DWORD* pdwRealFunc)
119+
{
120+
#pragma pack(push, 1)
121+
struct JMP_STRUCT
122+
{
123+
byte opcode;
124+
DWORD offset;
125+
} *pInst;
126+
#pragma pack(pop)
127+
128+
DWORD dwOldFlag;
129+
VirtualProtect((LPVOID)dwAddr, 5, PAGE_EXECUTE_READWRITE, &dwOldFlag);
130+
131+
pInst = (JMP_STRUCT*)dwAddr;
132+
133+
if (pdwRealFunc && pInst->opcode == 0xE9) // If this function is hooked by Ares
134+
*pdwRealFunc = pInst->offset + dwAddr + 5;
135+
136+
pInst->offset = reinterpret_cast<DWORD>(fakeFunc) - dwAddr - 5;
137+
138+
VirtualProtect((LPVOID)dwAddr, 5, dwOldFlag, NULL);
139+
}

src/Utilities/Console.h

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#pragma once
2+
3+
#include <Windows.h>
4+
5+
class Console
6+
{
7+
public:
8+
enum class ConsoleColor
9+
{
10+
Black = 0,
11+
DarkBlue = 1,
12+
DarkGreen = 2,
13+
DarkRed = 4,
14+
Intensity = 8,
15+
16+
DarkCyan = DarkBlue | DarkGreen,
17+
DarkMagenta = DarkBlue | DarkRed,
18+
DarkYellow = DarkGreen | DarkRed,
19+
Gray = DarkBlue | DarkGreen | DarkRed,
20+
DarkGray = Black | Intensity,
21+
22+
Blue = DarkBlue | Intensity,
23+
Green = DarkGreen | Intensity,
24+
Red = DarkRed | Intensity,
25+
Cyan = Blue | Green,
26+
Magenta = Blue | Red,
27+
Yellow = Green | Red,
28+
White = Red | Green | Blue,
29+
};
30+
31+
union ConsoleTextAttribute
32+
{
33+
WORD AsWord;
34+
struct
35+
{
36+
ConsoleColor Foreground : 4;
37+
ConsoleColor Background : 4;
38+
bool LeadingByte : 1;
39+
bool TrailingByte : 1;
40+
bool GridTopHorizontal : 1;
41+
bool GridLeftVertical : 1;
42+
bool GridRightVerticle : 1;
43+
bool ReverseVideo : 1; // Reverse fore/back ground attribute
44+
bool Underscore : 1;
45+
bool Unused : 1;
46+
};
47+
};
48+
static ConsoleTextAttribute TextAttribute;
49+
static HANDLE ConsoleHandle;
50+
51+
static bool Create();
52+
static void Release();
53+
54+
template<size_t Length>
55+
constexpr static void Write(const char (&str)[Length])
56+
{
57+
Write(str, Length - 1); // -1 because there is a '\0' here
58+
}
59+
static void SetForeColor(ConsoleColor color);
60+
static void SetBackColor(ConsoleColor color);
61+
static void EnableUnderscore(bool enable);
62+
static void Write(const char* str, int len);
63+
static void WriteLine(const char* str, int len);
64+
static void __fastcall WriteWithVArgs(const char* pFormat, va_list args);
65+
static void WriteFormat(const char* pFormat, ...);
66+
67+
private:
68+
static void PatchLog(DWORD dwAddr, void* realFunc, DWORD* pdwRealFunc);
69+
};

src/Utilities/Debug.cpp

-135
Original file line numberDiff line numberDiff line change
@@ -67,138 +67,3 @@ DEFINE_PATCH( // Replace SUN.INI with RA2MD.INI in the debug.log
6767
/* Offset */ 0x8332F4,
6868
/* Data */ "-------- Loading RA2MD.INI settings --------\n"
6969
);
70-
71-
static DWORD _Real_Debug_Log = 0x4A4AF9;
72-
void __declspec(naked) _Fake_Debug_Log()
73-
{
74-
// va_start(args, pFormat);
75-
// Console::WriteWithVArgs(pFormat, args);
76-
// // va_end(args);
77-
// No need to use va_end here.
78-
//
79-
// As Console::WriteWithVArgs uses __fastcall,
80-
// ECX: pFormat, EDX: args
81-
__asm { mov ecx, [esp + 0x4] }
82-
__asm { lea edx, [esp + 0x8] }
83-
__asm { call Console::WriteWithVArgs }
84-
// __asm { mov edx, 0}
85-
86-
// goto original bytes
87-
__asm { mov eax, _Real_Debug_Log }
88-
__asm { jmp eax }
89-
}
90-
91-
Console::ConsoleTextAttribute Console::TextAttribute;
92-
HANDLE Console::ConsoleHandle;
93-
94-
bool Console::Create()
95-
{
96-
if (FALSE == AllocConsole())
97-
return false;
98-
99-
ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
100-
if (NULL == ConsoleHandle)
101-
return false;
102-
103-
SetConsoleTitle("Phobos Debug Console");
104-
105-
CONSOLE_SCREEN_BUFFER_INFO csbi;
106-
GetConsoleScreenBufferInfo(ConsoleHandle, &csbi);
107-
TextAttribute.AsWord = csbi.wAttributes;
108-
109-
PatchLog(0x4A4AC0, _Fake_Debug_Log, &_Real_Debug_Log);
110-
PatchLog(0x4068E0, _Fake_Debug_Log, nullptr);
111-
112-
return true;
113-
}
114-
115-
void Console::Release()
116-
{
117-
if (NULL != ConsoleHandle)
118-
FreeConsole();
119-
}
120-
121-
void Console::SetForeColor(ConsoleColor color)
122-
{
123-
if (NULL == ConsoleHandle)
124-
return;
125-
126-
if (TextAttribute.Foreground == color)
127-
return;
128-
129-
TextAttribute.Foreground = color;
130-
SetConsoleTextAttribute(ConsoleHandle, TextAttribute.AsWord);
131-
}
132-
133-
void Console::SetBackColor(ConsoleColor color)
134-
{
135-
if (NULL == ConsoleHandle)
136-
return;
137-
138-
if (TextAttribute.Background == color)
139-
return;
140-
141-
TextAttribute.Background = color;
142-
SetConsoleTextAttribute(ConsoleHandle, TextAttribute.AsWord);
143-
}
144-
145-
void Console::EnableUnderscore(bool enable)
146-
{
147-
if (NULL == ConsoleHandle)
148-
return;
149-
150-
if (TextAttribute.Underscore == enable)
151-
return;
152-
153-
TextAttribute.Underscore = enable;
154-
SetConsoleTextAttribute(ConsoleHandle, TextAttribute.AsWord);
155-
}
156-
157-
void Console::Write(const char* str, int len)
158-
{
159-
if (NULL != ConsoleHandle)
160-
WriteConsole(ConsoleHandle, str, len, nullptr, nullptr);
161-
}
162-
163-
void Console::WriteLine(const char* str, int len)
164-
{
165-
Write(str, len);
166-
Write("\n");
167-
}
168-
169-
void __fastcall Console::WriteWithVArgs(const char* pFormat, va_list args)
170-
{
171-
vsprintf_s(Debug::StringBuffer, pFormat, args);
172-
Write(Debug::StringBuffer, strlen(Debug::StringBuffer));
173-
}
174-
175-
void Console::WriteFormat(const char* pFormat, ...)
176-
{
177-
va_list args;
178-
va_start(args, pFormat);
179-
WriteWithVArgs(pFormat, args);
180-
va_end(args);
181-
}
182-
183-
void Console::PatchLog(DWORD dwAddr, void* fakeFunc, DWORD* pdwRealFunc)
184-
{
185-
#pragma pack(push, 1)
186-
struct JMP_STRUCT
187-
{
188-
byte opcode;
189-
DWORD offset;
190-
} *pInst;
191-
#pragma pack(pop)
192-
193-
DWORD dwOldFlag;
194-
VirtualProtect((LPVOID)dwAddr, 5, PAGE_EXECUTE_READWRITE, &dwOldFlag);
195-
196-
pInst = (JMP_STRUCT*)dwAddr;
197-
198-
if (pdwRealFunc && pInst->opcode == 0xE9) // If this function is hooked by Ares
199-
*pdwRealFunc = pInst->offset + dwAddr + 5;
200-
201-
pInst->offset = reinterpret_cast<DWORD>(fakeFunc) - dwAddr - 5;
202-
203-
VirtualProtect((LPVOID)dwAddr, 5, dwOldFlag, NULL);
204-
}

0 commit comments

Comments
 (0)