Skip to content

Commit

Permalink
console + d3d9 refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tobii-dev committed Jan 28, 2021
1 parent 2cb8bec commit c15e831
Show file tree
Hide file tree
Showing 7 changed files with 599 additions and 905 deletions.
17 changes: 13 additions & 4 deletions source/blur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ gameConfig::gameConfig(char cfg_name[]) {
*strrchr(cfg, '\\') = '\0';
strcat_s(cfg, "\\");
strcat_s(cfg, cfg_name);
forceWindowedMode = GetPrivateProfileInt("GFX", "boolForceWindowedMode", 0, cfg) != 0;
fps = static_cast<float>(GetPrivateProfileInt("FPS", "intFPSLimit", 0, cfg));
bFPSLimit = fps > 0.0;
//TODO: actual name
Expand All @@ -20,19 +19,30 @@ gameConfig::gameConfig(char cfg_name[]) {
}
}


gameAPI::gameAPI(uintptr_t p) : config("cfg.ini") {
moduleBase = p;
};


//TODO: lets have all the init code here so we dont have to touch d3d9.cpp
void gameAPI::load() {
console.start();
if (install_menu_hook()) {
blurAPI->console.print("!!");
} else {
blurAPI->console.print("ERROR in gameAPI::load() [install_menu_hook() returned false]");
}
}


void gameAPI::unload() {
//TODO: are we leaking mem rn?
console.close();
}

//TODO: move this somewhere sane

//-------------------------------------------
//TODO: move this somewhere sane
bool gameAPI::toggle_SP_drifter() {
uintptr_t modAdr = moduleBase + ADDY_SP_MOD;
int* modPtr = (int*) modAdr;
Expand Down Expand Up @@ -62,7 +72,6 @@ bool gameAPI::set_LAN_name(std::string szName) {
}
return set;
}
//-------------------------------------------


gameAPI* blurAPI = nullptr;
2 changes: 1 addition & 1 deletion source/blur.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

struct gameConfig {
std::string user_name;
bool forceWindowedMode;
float fps;
bool bFPSLimit;
gameConfig(char cfg_name[]);
Expand All @@ -32,6 +31,7 @@ struct gameAPI {
gameConfig config;
gameConsole console;
gameAPI(uintptr_t p);
void load();
void unload();

bool toggle_SP_drifter();
Expand Down
84 changes: 53 additions & 31 deletions source/blur_console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,98 @@
#include <fcntl.h>
#include <string>
#include <vector>
#include <conio.h>



//TODO: command handler, close button, tab complete, colours, game input
DWORD WINAPI input_thread(void* arg) {
std::cout << "HELLO WORLD :: Console has been created!" << std::endl;
std::string buff, cmd;
std::string* input = (std::string*) arg;
std::string cmd;
std::vector<std::string> cmd_args;
std::string tmp;
char* c = NULL;
input->clear();
char c = NULL;
char prompt = '+';
while (true) {
std::cout << "> ";
std::getline(std::cin, buff);
cmd_args = split(buff, " ");
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];
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, " ");
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("Could not change name to ["+cmd_args[1]+"]");
blurAPI->console.print("USAGE: name <your_username>");
}
} else if (cmd_args.size() == 1) {
blurAPI->console.print(" name: [" + 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));
}
} else if (cmd == "fps") {
blurAPI->console.print(" " + std::to_string((float) blurAPI->config.fps));
}
} else if (c == '\b') {
if (!input->empty()) {
prompt = '-';
input->pop_back();
std::cout << "\b ";
} else {
prompt = '>';
}
} else {
input->push_back(c);
prompt = '+';
}
}
return 0;
}

void handler_func() {
std::cout << "hello from *handler_func()!" << std::endl;
}

gameConsole::gameConsole() {
(void)_setmode(_fileno(stdout), _O_U16TEXT); //let me use unicode some day
AllocConsole();
freopen_s(&f, "CONOUT$", "w", stdout);
std::cout.clear();
std::cin.clear();
freopen_s(&f, "CONIN$", "r", stdin);
//_setmode(_fileno(stdout), _O_U16TEXT); //let me use unicode some day
SetConsoleTitle("BLUR CONSOLE");
input = new std::string; //we should do this differently...
}


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


void gameConsole::close() {
print("Closing console"); // might not get printed
print("Closing console..."); // might not get printed
FreeConsole();
fclose(f);
delete input;
}


void gameConsole::print(std::string out) {
void gameConsole::print(std::string text) {
//TODO: mutex with input thread, make nicer, make usable, etc...
std::cout << "] " << out << std::endl;
std::cout << "\r] " << text << std::endl;
if (input->empty()) {
std::cout << "\r> ";
} else {
std::cout << "\n\r: " << *input;
}
}


Expand Down
1 change: 1 addition & 0 deletions source/blur_console.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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);
Expand Down
13 changes: 8 additions & 5 deletions source/blur_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,34 @@ bool install_menu_hook() {
}


fn_ptr_t tmp_global_i_hate_this_variable = nullptr;
//fn_ptr_t tmp_global_i_hate_this_variable = nullptr; //gone?
bool install_menu_hook(fn_ptr_t fn) {
bool hooked = false;
uintptr_t src = blurAPI->moduleBase + HOOK_MENU_FUNC_ADDY;
fn_ptr_t t = install_void_hook((void*) src, menu_hook_func, HOOK_MENU_FUNC_INS_LEN);
if (t) {
blurAPI->hooks.fn = fn;
blurAPI->hooks.fn_trampoline = t;
tmp_global_i_hate_this_variable = t; //PLEASE I DONT LIKE YOU SO JUST WORK
//tmp_global_i_hate_this_variable = t; //PLEASE I DONT LIKE YOU SO JUST WORK (FIXED?)
hooked = true;
}
return hooked;
}

void __declspec(naked) menu_hook_func() {
__asm nop;
__asm nop;
void* f;
f = blurAPI->hooks.fn_trampoline; //like this it works...
__asm PUSHAD;
__asm PUSHFD;
__asm nop;
__asm nop;
(blurAPI->hooks.fn)();
__asm nop;
__asm nop;
__asm POPFD;
__asm POPAD;
__asm jmp [tmp_global_i_hate_this_variable];
__asm jmp [f];
//__asm jmp [tmp_global_i_hate_this_variable];
//__asm jmp [blurAPI->hooks.fn_trampoline];
//(blurAPI->hooks.fn_trampoline)();
}
Expand Down
Loading

0 comments on commit c15e831

Please sign in to comment.