diff --git a/cnc-ddraw.vcxproj b/cnc-ddraw.vcxproj
index 3133efa..924f32f 100644
--- a/cnc-ddraw.vcxproj
+++ b/cnc-ddraw.vcxproj
@@ -49,6 +49,7 @@
+
@@ -82,6 +83,7 @@
+
diff --git a/cnc-ddraw.vcxproj.filters b/cnc-ddraw.vcxproj.filters
index 11979b2..5a253f1 100644
--- a/cnc-ddraw.vcxproj.filters
+++ b/cnc-ddraw.vcxproj.filters
@@ -156,6 +156,9 @@
Source Files
+
+ Source Files
+
@@ -269,6 +272,9 @@
Header Files
+
+ Header Files
+
diff --git a/inc/ini.h b/inc/ini.h
new file mode 100644
index 0000000..9b73604
--- /dev/null
+++ b/inc/ini.h
@@ -0,0 +1,6 @@
+#ifndef INI_H
+#define INI_H
+
+BOOL ini_section_exists(char* section);
+
+#endif
diff --git a/src/config.c b/src/config.c
index ba23739..1e89be9 100644
--- a/src/config.c
+++ b/src/config.c
@@ -10,6 +10,7 @@
#include "hook.h"
#include "debug.h"
#include "dllmain.h"
+#include "ini.h"
static void cfg_init();
static void cfg_create_ini();
@@ -1098,37 +1099,42 @@ static DWORD cfg_get_string(LPCSTR key, LPCSTR default_value, LPSTR out_string,
if (!g_config.ini_path[0])
cfg_init();
- DWORD s = GetPrivateProfileStringA(
- g_config.process_file_name, key, "", out_string, out_size, g_config.ini_path);
-
char buf[MAX_PATH] = { 0 };
- if (s > 0)
+ if (ini_section_exists(g_config.process_file_name))
{
- if (GetPrivateProfileStringA(
- g_config.process_file_name, "checkfile", "", buf, sizeof(buf), g_config.ini_path) > 0)
+ DWORD s = GetPrivateProfileStringA(
+ g_config.process_file_name, key, "", out_string, out_size, g_config.ini_path);
+
+ if (s > 0)
{
- if (FILE_EXISTS(buf))
+ if (GetPrivateProfileStringA(
+ g_config.process_file_name, "checkfile", "", buf, sizeof(buf), g_config.ini_path) > 0)
+ {
+ if (FILE_EXISTS(buf))
+ return s;
+ }
+ else
return s;
}
- else
- return s;
}
- /* Only checking 1 additional section for now (it may be too slow otherwise) */
- for (int i = 2; i < 3; i++)
+ for (int i = 2; i < 10; i++)
{
char section[MAX_PATH] = { 0 };
_snprintf(section, sizeof(section) - 1, "%s/%d", g_config.process_file_name, i);
- s = GetPrivateProfileStringA(section, key, "", out_string, out_size, g_config.ini_path);
-
- if (s > 0)
+ if (ini_section_exists(section))
{
- if (GetPrivateProfileStringA(section, "checkfile", "", buf, sizeof(buf), g_config.ini_path) > 0)
+ DWORD s = GetPrivateProfileStringA(section, key, "", out_string, out_size, g_config.ini_path);
+
+ if (s > 0)
{
- if (FILE_EXISTS(buf))
- return s;
+ if (GetPrivateProfileStringA(section, "checkfile", "", buf, sizeof(buf), g_config.ini_path) > 0)
+ {
+ if (FILE_EXISTS(buf))
+ return s;
+ }
}
}
}
diff --git a/src/ini.c b/src/ini.c
new file mode 100644
index 0000000..38c42bc
--- /dev/null
+++ b/src/ini.c
@@ -0,0 +1,49 @@
+#include
+#include "debug.h"
+#include "config.h"
+#include "crc32.h"
+
+static unsigned long g_ini_section_hashes[1024];
+
+BOOL ini_section_exists(char* section)
+{
+ if (!g_ini_section_hashes[0])
+ {
+ char* buf = calloc(8192, 1);
+ if (buf)
+ {
+ if (GetPrivateProfileSectionNamesA(buf, 8192, g_config.ini_path) > 0)
+ {
+ for (int i = 0; *buf && i < sizeof(g_ini_section_hashes) / sizeof(g_ini_section_hashes[0]); i++)
+ {
+ size_t len = strlen(buf);
+
+ for (char* p = buf; *p; ++p)
+ *p = tolower(*p);
+
+ g_ini_section_hashes[i] = Crc32_ComputeBuf(0, buf, len);
+
+ buf += len + 1;
+ }
+ }
+
+ free(buf);
+ }
+ }
+
+ char s[MAX_PATH];
+ strncpy(s, section, sizeof(s) - 1);
+
+ for (char* p = s; *p; ++p)
+ *p = tolower(*p);
+
+ unsigned long hash = Crc32_ComputeBuf(0, s, strlen(s));
+
+ for (int i = 0; i < sizeof(g_ini_section_hashes) / sizeof(g_ini_section_hashes[0]) && g_ini_section_hashes[i]; i++)
+ {
+ if (g_ini_section_hashes[i] == hash)
+ return TRUE;
+ }
+
+ return FALSE;
+}