Skip to content

Commit

Permalink
console cmd_handler + small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tobii-dev committed Jan 28, 2021
1 parent c15e831 commit 77aa790
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ To compile the DLL:


## TODO
- Clean up the d3d9 hooks
[DONE] Clean up the d3d9 hooks
- Add more hooks to game events
- Plugins
- Find IDE independent way to create DLL
74 changes: 50 additions & 24 deletions source/blur_console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@



//TODO: command handler, close button, tab complete, colours, game input
//TODO: buttons, better screen,
DWORD WINAPI input_thread(void* arg) {
std::cout << "HELLO WORLD :: Console has been created!" << std::endl;
std::string* input = (std::string*) arg;
Expand All @@ -19,33 +19,17 @@ DWORD WINAPI input_thread(void* arg) {
char c = NULL;
char prompt = '+';
while (true) {
//UP1 \x1b^[[1A
//DOWN1 \x1b^[[1B
std::cout << "\r" << prompt << " " << *input;
c = _getch();
if ((c == EOF) || (c == '\n') || (c == '\r')) { //parse the stuff
std::cout << std::endl;
cmd_args = split(*input, " ");
cmd = *input;
input->clear();
if (!cmd_args.empty()) {
cmd = cmd_args[0];
if (cmd == "hey") {
blurAPI->console.print("blurAPI->console.print() from console thread");
} else if (cmd == "name") {
if (cmd_args.size() == 2) {
if (blurAPI->set_LAN_name(cmd_args[1])) {
blurAPI->console.print("Name changed to ["+cmd_args[1]+"], exit multiplayer menu to use it.");
blurAPI->config.user_name = cmd_args[1];
} else {
blurAPI->console.print("Could not change name to ["+cmd_args[1]+"]");
}
} else if (cmd_args.size() == 1) {
blurAPI->console.print("Current name is [" + blurAPI->config.user_name + "]");
} else {
blurAPI->console.print("USAGE: name <your_username>");
}
} else if (cmd == "fps") {
blurAPI->console.print(" " + std::to_string((float) blurAPI->config.fps));
}
}
blurAPI->console.cmd_handler(cmd);
prompt = '=';
cmd.clear();
} else if (c == '\b') {
if (!input->empty()) {
prompt = '-';
Expand All @@ -65,18 +49,24 @@ DWORD WINAPI input_thread(void* arg) {

gameConsole::gameConsole() {
(void)_setmode(_fileno(stdout), _O_U16TEXT); //let me use unicode some day
// enabling VT100 style in current console
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD l_mode;
GetConsoleMode(hStdout, &l_mode);
SetConsoleMode(hStdout, l_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN);
AllocConsole();
freopen_s(&f, "CONOUT$", "w", stdout);
std::cout.clear();
std::cin.clear();
freopen_s(&f, "CONIN$", "r", stdin);
SetConsoleTitle("BLUR CONSOLE");
input = new std::string; //we should do this differently...
input = new std::string; //not like this...
}


void gameConsole::start() {
std::cout << "Loading console..." << std::endl;
std::ios_base::sync_with_stdio(false);
HANDLE input_thread_handle = CreateThread(NULL, 0, input_thread, input, 0, NULL);
}

Expand All @@ -100,6 +90,42 @@ void gameConsole::print(std::string text) {
}


bool gameConsole::cmd_handler(std::string cmd) {
bool isCmd = false;
std::vector<std::string> cmd_args = split(cmd, " ");
if (!cmd_args.empty()) {
if (cmd_args[0] == "hey") {
print("blurAPI->console.print(HELLO!)");
isCmd = true;
} else if (cmd_args[0] == "name") {
if (cmd_args.size() > 1) {
if ((cmd_args[1].front() == '"') && (cmd_args.back().back() == '"')) {
std::vector<std::string> tmp = split(cmd, "\"");
if (tmp.size() == 2) {
if (blurAPI->set_LAN_name(tmp[1])) {
print("Name changed to \""+tmp[1]+"\", exit multiplayer menu to use it.");
blurAPI->config.user_name = tmp[1];
} else {
print("Unable to change name to \""+tmp[1]+"\"");
}
} else {
print("*Error* USAGE: name \"your_username\"");
}
} else {
print("*Error*(Try with quotes?) USAGE: name \"your_username\"");
}
} else {
print("Current name is \"" + blurAPI->config.user_name + "\"");
}
isCmd = true;
} else if (cmd_args[0] == "fps") {
print(" " + std::to_string((float) blurAPI->config.fps));
isCmd = true;
}
}
return isCmd;
}

//util func to split strings
std::vector<std::string> split(std::string str, std::string delim) {
std::vector<std::string> tokens;
Expand Down
3 changes: 3 additions & 0 deletions source/blur_console.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

std::vector<std::string> split(std::string str, std::string delim);



struct gameConsole {
FILE* f;
std::string* input;
gameConsole();
void start();
void print(std::string t);
void close();
bool cmd_handler(std::string cmd);
};

6 changes: 4 additions & 2 deletions source/d3d9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ bool WINAPI DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved) {



/* D3D9 HOOKED FUNCTIONS */
void init_d3d9_hooks() {
char sz_dll[MAX_PATH];
GetSystemDirectory(sz_dll, MAX_PATH);
Expand All @@ -51,7 +52,6 @@ void init_d3d9_hooks() {
}


/* D3D9 HOOKED FUNCTIONS */

//called when the game renders a frame
HRESULT f_IDirect3DDevice9::Present(CONST RECT *pSourceRect, CONST RECT *pDestRect, HWND hDestWindowOverride, CONST RGNDATA *pDirtyRegion) {
Expand Down Expand Up @@ -112,6 +112,7 @@ HRESULT f_IDirect3DDevice9::Present(CONST RECT *pSourceRect, CONST RECT *pDestRe
}


//when game starts
HRESULT f_iD3D9::CreateDevice(UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviorFlags, D3DPRESENT_PARAMETERS *pPresentationParameters, IDirect3DDevice9 **ppReturnedDeviceInterface) {
LPDIRECT3DDEVICE9 *temp = ppReturnedDeviceInterface;

Expand All @@ -132,6 +133,7 @@ HRESULT f_iD3D9::CreateDevice(UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWi
}


//changing resolution / toggle fullscreen
HRESULT f_IDirect3DDevice9::Reset(D3DPRESENT_PARAMETERS *pPresentationParameters) {
// NOTE: call onLostDevice for custom D3D components here.

Expand All @@ -144,7 +146,7 @@ HRESULT f_IDirect3DDevice9::Reset(D3DPRESENT_PARAMETERS *pPresentationParameters
}



//when frame is done rendering
HRESULT f_IDirect3DDevice9::EndScene() {
static bool bPressed = false;
static bool bDrawBox = false;
Expand Down

0 comments on commit 77aa790

Please sign in to comment.