Skip to content

Commit

Permalink
Remove passing procname by argv, instead use DOORSTOP_PROCESS_PATH en…
Browse files Browse the repository at this point in the history
…vvar
  • Loading branch information
ghorsington committed May 23, 2020
1 parent f9d7cb4 commit dcef761
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 58 deletions.
56 changes: 26 additions & 30 deletions DoorstopTest/Loader.cs
@@ -1,31 +1,27 @@
using System;
using System.IO;

namespace Doorstop
{
public static class Loader
{
public static void Main(string[] args)
{
using (TextWriter tw = File.CreateText("doorstop_is_alive.txt"))
{
tw.WriteLine($"Hello! This text file was generated by Doorstop on {DateTime.Now:R}!");
tw.WriteLine($"I was called with {args.Length} params!");

for (int i = 0; i < args.Length; i++)
tw.WriteLine($"{i} => {args[i]}");

tw.WriteLine($"Command line: {Environment.CommandLine}");

tw.WriteLine();
tw.WriteLine("Doorstop also set the following environment variables:");
tw.WriteLine($"DOORSTOP_INVOKE_PATH = {Environment.GetEnvironmentVariable("DOORSTOP_INVOKE_DLL_PATH")}");
tw.WriteLine($"DOORSTOP_MANAGED_FOLDER_DIR = {Environment.GetEnvironmentVariable("DOORSTOP_MANAGED_FOLDER_DIR")}");

tw.WriteLine("Replace this DLL with a custom-made one to do whatever you want!");

tw.Flush();
}
}
}
using System;
using System.IO;

namespace Doorstop
{
public static class Loader
{
public static void Main(string[] args)
{
using (TextWriter tw = File.CreateText("doorstop_is_alive.txt"))
{
tw.WriteLine($"Hello! This text file was generated by Doorstop on {DateTime.Now:R}!");
tw.WriteLine($"Command line: {Environment.CommandLine}");

tw.WriteLine();
tw.WriteLine("Doorstop also set the following environment variables:");
tw.WriteLine($"DOORSTOP_INVOKE_PATH = {Environment.GetEnvironmentVariable("DOORSTOP_INVOKE_DLL_PATH")}");
tw.WriteLine($"DOORSTOP_MANAGED_FOLDER_DIR = {Environment.GetEnvironmentVariable("DOORSTOP_MANAGED_FOLDER_DIR")}");
tw.WriteLine($"DOORSTOP_PROCESS_PATH = {Environment.GetEnvironmentVariable("DOORSTOP_PROCESS_PATH")}");

tw.WriteLine("Replace this DLL with a custom-made one to do whatever you want!");

tw.Flush();
}
}
}
}
23 changes: 6 additions & 17 deletions Proxy/main.c
Expand Up @@ -97,12 +97,16 @@ void *init_doorstop(const char *root_domain_name, const char *runtime_version)
char *dll_path = memalloc(sizeof(char) * len);
WideCharToMultiByte(CP_UTF8, 0, target_assembly, -1, dll_path, len, NULL, NULL);

wchar_t *app_path = NULL;
get_module_path(NULL, &app_path, NULL, 0);
SetEnvironmentVariableW(L"DOORSTOP_PROCESS_PATH", app_path);

LOG("Loading assembly: %s\n", dll_path);
// Load our custom assembly into the domain
void *assembly = mono_domain_assembly_open(domain, dll_path);

if (assembly == NULL)
LOG("Failed to load assembly\n");
LOG("Failed to load assembly\n");

memfree(dll_path);
ASSERT_SOFT(assembly != NULL, domain);
Expand All @@ -111,8 +115,6 @@ void *init_doorstop(const char *root_domain_name, const char *runtime_version)
void *image = mono_assembly_get_image(assembly);
ASSERT_SOFT(image != NULL, domain);

// Note: we use the runtime_invoke route since jit_exec will not work on DLLs

// Create a descriptor for a random Main method
void *desc = mono_method_desc_new("*:Main", FALSE);

Expand All @@ -126,28 +128,15 @@ void *init_doorstop(const char *root_domain_name, const char *runtime_version)
UINT32 params = mono_signature_get_param_count(signature);

void **args = NULL;
wchar_t *app_path = NULL;
if (params == 1)
{
// If there is a parameter, it's most likely a string[].
// Populate it as follows
// 0 => path to the game's executable
// 1 => --doorstop-invoke

get_module_path(NULL, &app_path, NULL, 0);

void *exe_path = MONO_STRING(app_path);
void *doorstop_handle = MONO_STRING(L"--doorstop-invoke");

void *args_array = mono_array_new(domain, mono_get_string_class(), 2);

SET_ARRAY_REF(args_array, 0, exe_path);
SET_ARRAY_REF(args_array, 1, doorstop_handle);

args = memalloc(sizeof(void*) * 1);
args[0] = args_array;
}

// Note: we use the runtime_invoke route since jit_exec will not work on DLLs
LOG("Invoking method!\n");
mono_runtime_invoke(method, NULL, args, NULL);

Expand Down
11 changes: 0 additions & 11 deletions Proxy/mono.h
Expand Up @@ -14,13 +14,6 @@
// Creates a MonoString based from a C wide string
#define MONO_STRING(str) mono_string_new_utf16(domain, str, wcslen(str))

// Set MonoArray's index to a reference type value (i.e. string)
#define SET_ARRAY_REF(arr, index, refVal) \
{ \
void **p = (void**) mono_array_addr_with_size(arr, sizeof(void*), index); \
mono_gc_wbarrier_set_arrayref(arr, p, refVal); \
}

// Here we define the pointers to some functions within mono.dll
// Note to C learners: these are not signature definitions, but rather "variable"
// definitions with the function pointer type.
Expand All @@ -46,8 +39,6 @@ UINT32 (*mono_signature_get_param_count)(void *sig);
void (*mono_domain_set_config)(void* domain, char* base_dir, char* config_file_name);

void *(*mono_array_new)(void *domain, void *eclass, uintptr_t n);
void (*mono_gc_wbarrier_set_arrayref)(void *arr, void *slot_ptr, void *value);
char *(*mono_array_addr_with_size)(void *arr, int size, uintptr_t idx);

void *(*mono_get_string_class)();
void *(*mono_string_new_utf16)(void *domain, const wchar_t *text, INT32 len);
Expand Down Expand Up @@ -78,8 +69,6 @@ inline void load_mono_functions(HMODULE mono_lib)
GET_MONO_PROC(mono_array_new);
GET_MONO_PROC(mono_get_string_class);
GET_MONO_PROC(mono_string_new_utf16);
GET_MONO_PROC(mono_gc_wbarrier_set_arrayref);
GET_MONO_PROC(mono_array_addr_with_size);
GET_MONO_PROC(mono_assembly_getrootdir);
GET_MONO_PROC(mono_thread_current);
GET_MONO_PROC(mono_thread_set_main);
Expand Down

0 comments on commit dcef761

Please sign in to comment.