You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.1 KiB

  1. #pragma once
  2. #pragma warning( disable : 4267 )
  3. #include <Windows.h>
  4. #include <shellapi.h>
  5. #include "winapi_util.h"
  6. #include "assert_util.h"
  7. #include "crt.h"
  8. #define CONFIG_NAME L"doorstop_config"
  9. #define DEFAULT_TARGET_ASSEMBLY L"Doorstop.dll"
  10. #define EXE_EXTENSION_LENGTH 4
  11. BOOL enabled = FALSE;
  12. BOOL debug = FALSE;
  13. BOOL debug_server = FALSE;
  14. BOOL debug_info = FALSE;
  15. wchar_t *targetAssembly = NULL;
  16. #define STR_EQUAL(str1, str2) (lstrcmpiW(str1, str2) == 0)
  17. inline void initConfigFile()
  18. {
  19. enabled = TRUE;
  20. WIN32_FIND_DATAW findData;
  21. HANDLE findHandle = FindFirstFileW(L"*_Data", &findData);
  22. if (findHandle == INVALID_HANDLE_VALUE)
  23. {
  24. MessageBoxW(NULL, L"Could not locate game being injected!", L"No files found in current directory matching '*_Data'",
  25. MB_OK | MB_ICONERROR | MB_SYSTEMMODAL | MB_TOPMOST | MB_SETFOREGROUND);
  26. ExitProcess(GetLastError());
  27. }
  28. do
  29. {
  30. if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  31. { // must be a directory
  32. wchar_t* target = memalloc(MAX_PATH * sizeof(wchar_t));
  33. const int EXIT_FAILURE = 1;
  34. ASSERT(target != NULL, L"Address returned by memalloc was NULL!");
  35. wmemset(target, 0, MAX_PATH);
  36. wmemcpy(target, findData.cFileName, wcslen(findData.cFileName));
  37. wmemcpy(target + wcslen(target), L"/Managed/IPA.Injector.dll", 26);
  38. targetAssembly = target;
  39. FindClose(findHandle);
  40. break;
  41. }
  42. }
  43. while (FindNextFileW(findHandle, &findData) != 0);
  44. if (targetAssembly == NULL)
  45. {
  46. MessageBoxW(NULL, L"Could not locate game being injected!", L"No valid directories matching '*_Data'",
  47. MB_OK | MB_ICONERROR | MB_SYSTEMMODAL | MB_TOPMOST | MB_SETFOREGROUND);
  48. ExitProcess(GetLastError());
  49. }
  50. }
  51. inline void initCmdArgs()
  52. {
  53. wchar_t *args = GetCommandLineW();
  54. int argc = 0;
  55. wchar_t **argv = CommandLineToArgvW(args, &argc);
  56. #define IS_ARGUMENT(arg_name) STR_EQUAL(arg, arg_name) && i < argc
  57. for (int i = 0; i < argc; i++)
  58. {
  59. wchar_t *arg = argv[i];
  60. /*if (IS_ARGUMENT(L"--doorstop-enable"))
  61. {
  62. wchar_t *par = argv[++i];
  63. if (STR_EQUAL(par, L"true"))
  64. enabled = TRUE;
  65. else if (STR_EQUAL(par, L"false"))
  66. enabled = FALSE;
  67. }
  68. else if (IS_ARGUMENT(L"--doorstop-target"))
  69. {
  70. if (targetAssembly != NULL)
  71. memfree(targetAssembly);
  72. const size_t len = wcslen(argv[i + 1]) + 1;
  73. targetAssembly = memalloc(sizeof(wchar_t) * len);
  74. lstrcpynW(targetAssembly, argv[++i], len);
  75. LOG("Args; Target assembly: %S\n", targetAssembly);
  76. }
  77. else */if (IS_ARGUMENT(L"--mono-debug"))
  78. {
  79. debug = TRUE;
  80. debug_info = TRUE;
  81. LOG("Enabled debugging\n");
  82. }
  83. else if (IS_ARGUMENT(L"--debug"))
  84. {
  85. debug_info = TRUE;
  86. LOG("Enabled loading of debug info\n");
  87. }
  88. else if (IS_ARGUMENT(L"--server"))
  89. {
  90. debug_server = TRUE;
  91. LOG("Server-mode debugging enabled\n");
  92. }
  93. }
  94. LocalFree(argv);
  95. }
  96. inline void initEnvVars()
  97. {
  98. if (GetEnvironmentVariableW(L"DOORSTOP_DISABLE", NULL, 0) != 0)
  99. {
  100. LOG("DOORSTOP_DISABLE is set! Disabling Doorstop!\n");
  101. enabled = FALSE;
  102. }
  103. }
  104. inline void loadConfig()
  105. {
  106. initConfigFile();
  107. initCmdArgs();
  108. initEnvVars();
  109. }
  110. inline void cleanupConfig()
  111. {
  112. if (targetAssembly != NULL)
  113. memfree(targetAssembly);
  114. }