From d70d316c7ee279120e23639331a0951b1617bb46 Mon Sep 17 00:00:00 2001
From: egornovivan <egornov.ivan@gmail.com>
Date: Fri, 16 Feb 2024 22:55:17 +0500
Subject: [PATCH] Added support for subdir and sync of dll and ini names
 https://github.com/FunkyFr3sh/cnc-ddraw/issues/287

---
 inc/config.h     |  3 +++
 src/config.c     | 24 +++++++++++++++++++++---
 src/render_ogl.c |  2 +-
 3 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/inc/config.h b/inc/config.h
index 34c8c66..562535e 100644
--- a/inc/config.h
+++ b/inc/config.h
@@ -13,8 +13,11 @@ typedef struct CNCDDRAWCONFIG
     int upscaled_state;
     char ini_path[MAX_PATH];
     char game_path[MAX_PATH];
+    char dll_path[MAX_PATH];
     char process_file_name[MAX_PATH];
+    char dll_file_name[MAX_PATH];
     char process_file_ext[MAX_PATH];
+    char dll_file_ext[MAX_PATH];
     INIFILE ini;
     BOOL is_wine;
     
diff --git a/src/config.c b/src/config.c
index 31f5a5f..543def4 100644
--- a/src/config.c
+++ b/src/config.c
@@ -1095,11 +1095,29 @@ static void cfg_init()
         }
     }
 
+    /* get dll filename and directory */
+    if (GetModuleFileNameA(g_ddraw_module, g_config.dll_path, sizeof(g_config.dll_path) - 1) > 0)
+    {
+        _splitpath(g_config.dll_path, NULL, NULL, g_config.dll_file_name, g_config.dll_file_ext);
+
+        int len = strlen(g_config.dll_path) - strlen(g_config.dll_file_name) - strlen(g_config.dll_file_ext);
+        char* end = strstr(g_config.dll_path + len, g_config.dll_file_name);
+
+        if (end)
+        {
+            *end = 0;
+        }
+        else
+        {
+            g_config.dll_path[0] = 0;
+        }
+    }
+
     if (!GetEnvironmentVariableA("CNC_DDRAW_CONFIG_FILE", g_config.ini_path, sizeof(g_config.ini_path) - 1))
     {
-        if (strlen(g_config.game_path) > 0)
+        if (strlen(g_config.dll_path) > 0 && strlen(g_config.dll_file_name) > 0)
         {
-            _snprintf(g_config.ini_path, sizeof(g_config.ini_path) - 1, "%sddraw.ini", g_config.game_path);
+            _snprintf(g_config.ini_path, sizeof(g_config.ini_path) - 1, "%s%s.ini", g_config.dll_path, g_config.dll_file_name);
 
             if (GetFileAttributes(g_config.ini_path) == INVALID_FILE_ATTRIBUTES)
             {
@@ -1108,7 +1126,7 @@ static void cfg_init()
 
             if (GetFileAttributes(g_config.ini_path) == INVALID_FILE_ATTRIBUTES)
             {
-                strncpy(g_config.ini_path, ".\\ddraw.ini", sizeof(g_config.ini_path) - 1);
+                _snprintf(g_config.ini_path, sizeof(g_config.ini_path) - 1, "%s%s.ini", g_config.dll_path, g_config.dll_file_name);
             }
         }
         else
diff --git a/src/render_ogl.c b/src/render_ogl.c
index f666eb5..6ff9a2c 100644
--- a/src/render_ogl.c
+++ b/src/render_ogl.c
@@ -160,7 +160,7 @@ static void ogl_build_programs()
 
             if (GetFileAttributes(shader_path) == INVALID_FILE_ATTRIBUTES)
             {
-                _snprintf(shader_path, sizeof(shader_path) - 1, "%s%s", g_config.game_path, g_config.shader);
+                _snprintf(shader_path, sizeof(shader_path) - 1, "%s%s", g_config.dll_path, g_config.shader);
             }
 
             /* detect common upscaling shaders and disable them if no upscaling is required */