Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't redirect when redirector launches itself #642

Merged
merged 4 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions post-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,6 @@

This document provides information on additional steps you may want to take after installing Mod Organizer 2.

## Running the original game launcher

1. Open Mod Organizer 2;
2. Click on the cogwheel in the top bar to configure executables:

![topbar executables config](screenshots/topbar_executables_config.png?raw=true "Executables Configuration")

3. Click on the launcher in the list to the left;
4. In the "Binary" text box, add an underscore before the name of the .exe file:

![executables list](screenshots/executables_config_leftside_list.png?raw=true "Executables List")

5. Now you should be able to run the launcher by selecting it in the executables dropdown:

![executables dropdown](screenshots/executables_dropdown.png?raw=true "Executables Dropdown")

## Using alternative proton versions

**IMPORTANT:** Proton 9.0 is the most extensively tested version. The author of this document provides no guarantees that alternative versions will work well.
Expand Down
Binary file removed screenshots/executables_config_leftside_list.png
Binary file not shown.
Binary file removed screenshots/executables_dropdown.png
Binary file not shown.
Binary file removed screenshots/topbar_executables_config.png
Binary file not shown.
2 changes: 1 addition & 1 deletion steam-redirector/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ main.exe: $(WIN_SRC)
$(WIN_C) $(WIN_FLAGS) -mwindows -o main.exe $^

main_debug.exe: $(WIN_SRC)
$(WIN_C) $(WIN_FLAGS) -o main_debug.exe $^
$(WIN_C) $(WIN_FLAGS) -D DEBUG -o main_debug.exe $^
86 changes: 54 additions & 32 deletions steam-redirector/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ char_t* read_path_from_file(const char* file_path) {
FILE* file = fopen(file_path, "r");

if (errno != 0) {
fprintf(stderr, "ERROR: failed to open '%s' - %s\n", file_path, strerror(errno));
fprintf(stderr, "ERROR: failed to open '"PATHSUBST"' - %s\n", file_path, strerror(errno));
return NULL;
}

fseek(file, 0L, SEEK_END);
size_t estimated_line_length = ftell(file);

if (estimated_line_length == 0) {
fprintf(stderr, "ERROR: cannot read empty file '%s'\n", MO2_PATH_FILE);
fprintf(stderr, "ERROR: cannot read empty file '"PATHSUBST"'\n", MO2_PATH_FILE);
return NULL;
}

Expand All @@ -52,15 +52,38 @@ char_t* read_path_from_file(const char* file_path) {

fclose(file);

#ifdef REQUIRE_UNICODE_CONVERSION
char_t* converted_line = convert_from_unicode(line);
#ifdef _WIN32
char_t* wline = convert_utf8_to_wchar(line);
free(line);
return converted_line;
return wline;
#else
return line;
#endif
}

char_t* get_original_launcher(const char_t* redirector_path) {
const char_t* filename = 0;
size_t filename_len = 0;
for (size_t i = 0; redirector_path[i] != '\0'; i++) {
if (redirector_path[i] == PATH_SEPARATOR) {
filename = redirector_path+i+1;
filename_len = 0;
} else {
filename_len++;
}
}

char_t* original_path = (char_t*)malloc(sizeof(char_t)*(filename_len+2));

original_path[0] = '_';
for (size_t i = 0; i < filename_len; i++) {
original_path[i+1] = filename[i];
}
original_path[filename_len+1] = '\0';

return original_path;
}

int check_file_access(const char_t* path) {
errno = 0;
check_can_execute(path);
Expand All @@ -72,48 +95,47 @@ int check_file_access(const char_t* path) {
}
}

int execute_from_path_file(const char* path_file_location, const char_t* arg) {
int MAIN(argc, argv) {
int exit_status = 1;
char_t* executable_path = read_path_from_file(path_file_location);

if (executable_path == NULL) {
char_t *arg = NULL;
if (argc > 1) {
arg = argv[1];
}

char_t *exe_path = NULL;
if (getenv("NO_REDIRECT") == NULL) {
putenv("NO_REDIRECT=1");
exe_path = read_path_from_file(MO2_PATH_FILE);
} else {
exe_path = get_original_launcher(argv[0]);
}

if (exe_path == NULL) {
fprintf(stderr, "ERROR: could not find executable, aborting\n");
goto exit_point;
}

fprintf(stdout, "INFO: read executable location '%s'\n", executable_path);
int has_access = check_file_access(executable_path);
int has_access = check_file_access(exe_path);

if (has_access == 0) {
fprintf(stderr, "ERROR: cannot execute '%s' - %s\n", executable_path, strerror(errno));
fprintf(stderr, "ERROR: cannot execute '"PATHSUBST"' - %s\n", exe_path, strerror(errno));
goto exit_point;
}

fprintf(stdout, "Launching '%s'\n", executable_path);
execute(executable_path, arg);
fprintf(stdout, "Launching '"PATHSUBST"'\n", exe_path);
execute(exe_path, arg);
exit_status = 0;

exit_point:
if (executable_path != NULL) {
free(executable_path);
}

return exit_status;
}

#ifdef REQUIRE_UNICODE_CONVERSION
int wmain(int argc, wchar_t** argv) {
#else
int main(int argc, char** argv) {
#ifdef DEBUG
fprintf(stdout, "DEBUG: Process finished, press enter to exit\n");
getchar();
#endif
int exit_status = 1;

char_t *arg = NULL;
if (argc > 1) {
arg = argv[1];
exit_point:
if (exe_path != NULL) {
free(exe_path);
}

exit_status = execute_from_path_file(MO2_PATH_FILE, arg);

return exit_status;
}

6 changes: 6 additions & 0 deletions steam-redirector/unix_utils.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#pragma once

#define PATH_SEPARATOR '/'

#define PATHSUBST "%s"

#define MAIN(argc, argv) main(int argc, char** argv)

typedef char char_t;

void execute(const char_t* path, const char_t* arg);
Expand Down
3 changes: 2 additions & 1 deletion steam-redirector/win32_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void log_conversion_error() {
}
}

char_t* convert_from_unicode(const char* str) {
char_t* convert_utf8_to_wchar(const char* str) {
int character_count = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
char_t* converted_str = (char_t*)malloc((character_count + 1) * sizeof(char_t));
int bytes_written = MultiByteToWideChar(CP_UTF8, 0, str, -1, converted_str, character_count);
Expand All @@ -39,3 +39,4 @@ char_t* convert_from_unicode(const char* str) {

return converted_str;
}

8 changes: 6 additions & 2 deletions steam-redirector/win32_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

#include <wchar.h>

#define REQUIRE_UNICODE_CONVERSION
#define PATH_SEPARATOR '\\'

#define PATHSUBST "%ls"

#define MAIN(argc, argv) wmain(int argc, wchar_t** argv)

typedef wchar_t char_t;

Expand All @@ -12,5 +16,5 @@ void check_can_execute(const char_t* path);

char_t read_character(FILE* file);

char_t* convert_from_unicode(const char* str);
char_t* convert_utf8_to_wchar(const char* str);

Loading