mirror of
https://github.com/narzoul/DDrawCompat
synced 2024-12-30 08:55:36 +01:00
Added config parser
This commit is contained in:
parent
97f56131ac
commit
6f92c8ef22
@ -1,7 +1,13 @@
|
|||||||
#include <Common/Path.h>
|
#include <Common/Path.h>
|
||||||
|
#include <Dll/Dll.h>
|
||||||
|
|
||||||
namespace Compat
|
namespace Compat
|
||||||
{
|
{
|
||||||
|
std::filesystem::path getEnvPath(const char* envVar)
|
||||||
|
{
|
||||||
|
return Dll::getEnvVar(envVar);
|
||||||
|
}
|
||||||
|
|
||||||
std::filesystem::path getModulePath(HMODULE module)
|
std::filesystem::path getModulePath(HMODULE module)
|
||||||
{
|
{
|
||||||
wchar_t path[MAX_PATH];
|
wchar_t path[MAX_PATH];
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
namespace Compat
|
namespace Compat
|
||||||
{
|
{
|
||||||
|
std::filesystem::path getEnvPath(const char* envVar);
|
||||||
std::filesystem::path getModulePath(HMODULE module);
|
std::filesystem::path getModulePath(HMODULE module);
|
||||||
std::filesystem::path getSystemPath();
|
std::filesystem::path getSystemPath();
|
||||||
bool isEqual(const std::filesystem::path& p1, const std::filesystem::path& p2);
|
bool isEqual(const std::filesystem::path& p1, const std::filesystem::path& p2);
|
||||||
|
101
DDrawCompat/Config/Parser.cpp
Normal file
101
DDrawCompat/Config/Parser.cpp
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
#include <Common/Log.h>
|
||||||
|
#include <Common/Path.h>
|
||||||
|
#include <Config/Parser.h>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
void setConfig(const std::string& name, const std::string& value);
|
||||||
|
std::string trim(const std::string& str);
|
||||||
|
|
||||||
|
void loadConfigFile(const std::string& type, const std::filesystem::path& path)
|
||||||
|
{
|
||||||
|
Compat::Log() << "Loading " << type << " config file: " << path.u8string();
|
||||||
|
std::ifstream f(path);
|
||||||
|
if (!f.is_open())
|
||||||
|
{
|
||||||
|
Compat::Log() << " File not found, skipping";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned lineNumber = 0;
|
||||||
|
std::string line;
|
||||||
|
while (std::getline(f, line))
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
++lineNumber;
|
||||||
|
auto pos = line.find_first_of(";#");
|
||||||
|
if (pos != std::string::npos)
|
||||||
|
{
|
||||||
|
line.resize(pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (line.find_first_not_of(" \t") == std::string::npos)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
pos = line.find('=');
|
||||||
|
if (pos == std::string::npos)
|
||||||
|
{
|
||||||
|
throw Config::ParsingError("missing '=' separator");
|
||||||
|
}
|
||||||
|
|
||||||
|
setConfig(trim(line.substr(0, pos)), trim(line.substr(pos + 1)));
|
||||||
|
}
|
||||||
|
catch (const Config::ParsingError& error)
|
||||||
|
{
|
||||||
|
Compat::Log() << " Line #" << lineNumber << ": " << error.what();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setConfig(const std::string& name, const std::string& /*value*/)
|
||||||
|
{
|
||||||
|
if (name.empty())
|
||||||
|
{
|
||||||
|
throw Config::ParsingError("missing setting name before '='");
|
||||||
|
}
|
||||||
|
|
||||||
|
throw Config::ParsingError("unknown setting: '" + name + "'");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string trim(const std::string& str)
|
||||||
|
{
|
||||||
|
auto result(str);
|
||||||
|
auto pos = str.find_last_not_of(" \t");
|
||||||
|
if (pos != std::string::npos)
|
||||||
|
{
|
||||||
|
result.resize(pos + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
pos = result.find_first_not_of(" \t");
|
||||||
|
if (pos != std::string::npos)
|
||||||
|
{
|
||||||
|
result = result.substr(pos);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace Config
|
||||||
|
{
|
||||||
|
namespace Parser
|
||||||
|
{
|
||||||
|
void loadAllConfigFiles(const std::filesystem::path& processPath)
|
||||||
|
{
|
||||||
|
loadConfigFile("global", Compat::getEnvPath("PROGRAMDATA") / "DDrawCompat" / "DDrawCompat.ini");
|
||||||
|
loadConfigFile("user", Compat::getEnvPath("LOCALAPPDATA") / "DDrawCompat" / "DDrawCompat.ini");
|
||||||
|
loadConfigFile("directory", Compat::replaceFilename(processPath, "DDrawCompat.ini"));
|
||||||
|
|
||||||
|
auto processConfigPath(processPath);
|
||||||
|
if (Compat::isEqual(processConfigPath.extension(), ".exe"))
|
||||||
|
{
|
||||||
|
processConfigPath.replace_extension();
|
||||||
|
}
|
||||||
|
processConfigPath.replace_filename(L"DDrawCompat-" + processConfigPath.filename().native() + L".ini");
|
||||||
|
loadConfigFile("process", processConfigPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
18
DDrawCompat/Config/Parser.h
Normal file
18
DDrawCompat/Config/Parser.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
namespace Config
|
||||||
|
{
|
||||||
|
class ParsingError : public std::runtime_error
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ParsingError(const std::string& error) : runtime_error(error) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace Parser
|
||||||
|
{
|
||||||
|
void loadAllConfigFiles(const std::filesystem::path& processPath);
|
||||||
|
}
|
||||||
|
}
|
@ -178,6 +178,7 @@
|
|||||||
<ClInclude Include="Common\ScopedCriticalSection.h" />
|
<ClInclude Include="Common\ScopedCriticalSection.h" />
|
||||||
<ClInclude Include="Common\Time.h" />
|
<ClInclude Include="Common\Time.h" />
|
||||||
<ClInclude Include="Config\Config.h" />
|
<ClInclude Include="Config\Config.h" />
|
||||||
|
<ClInclude Include="Config\Parser.h" />
|
||||||
<ClInclude Include="D3dDdi\Adapter.h" />
|
<ClInclude Include="D3dDdi\Adapter.h" />
|
||||||
<ClInclude Include="D3dDdi\AdapterCallbacks.h" />
|
<ClInclude Include="D3dDdi\AdapterCallbacks.h" />
|
||||||
<ClInclude Include="D3dDdi\AdapterFuncs.h" />
|
<ClInclude Include="D3dDdi\AdapterFuncs.h" />
|
||||||
@ -271,6 +272,7 @@
|
|||||||
<ClCompile Include="Common\Hook.cpp" />
|
<ClCompile Include="Common\Hook.cpp" />
|
||||||
<ClCompile Include="Common\Path.cpp" />
|
<ClCompile Include="Common\Path.cpp" />
|
||||||
<ClCompile Include="Common\Time.cpp" />
|
<ClCompile Include="Common\Time.cpp" />
|
||||||
|
<ClCompile Include="Config\Parser.cpp" />
|
||||||
<ClCompile Include="D3dDdi\Adapter.cpp" />
|
<ClCompile Include="D3dDdi\Adapter.cpp" />
|
||||||
<ClCompile Include="D3dDdi\AdapterCallbacks.cpp" />
|
<ClCompile Include="D3dDdi\AdapterCallbacks.cpp" />
|
||||||
<ClCompile Include="D3dDdi\AdapterFuncs.cpp" />
|
<ClCompile Include="D3dDdi\AdapterFuncs.cpp" />
|
||||||
|
@ -79,6 +79,9 @@
|
|||||||
<Filter Include="Source Files\D3dDdi\Log">
|
<Filter Include="Source Files\D3dDdi\Log">
|
||||||
<UniqueIdentifier>{c4d03748-2333-40af-90c7-ae875b07434f}</UniqueIdentifier>
|
<UniqueIdentifier>{c4d03748-2333-40af-90c7-ae875b07434f}</UniqueIdentifier>
|
||||||
</Filter>
|
</Filter>
|
||||||
|
<Filter Include="Source Files\Config">
|
||||||
|
<UniqueIdentifier>{9d313de7-b8ca-49d0-84d2-217f2818d9d3}</UniqueIdentifier>
|
||||||
|
</Filter>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Gdi\Gdi.h">
|
<ClInclude Include="Gdi\Gdi.h">
|
||||||
@ -390,6 +393,9 @@
|
|||||||
<ClInclude Include="Common\Path.h">
|
<ClInclude Include="Common\Path.h">
|
||||||
<Filter>Header Files\Common</Filter>
|
<Filter>Header Files\Common</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Config\Parser.h">
|
||||||
|
<Filter>Header Files\Config</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="Gdi\Gdi.cpp">
|
<ClCompile Include="Gdi\Gdi.cpp">
|
||||||
@ -611,6 +617,9 @@
|
|||||||
<ClCompile Include="Common\Path.cpp">
|
<ClCompile Include="Common\Path.cpp">
|
||||||
<Filter>Source Files\Common</Filter>
|
<Filter>Source Files\Common</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Config\Parser.cpp">
|
||||||
|
<Filter>Source Files\Config</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ResourceCompile Include="DDrawCompat.rc">
|
<ResourceCompile Include="DDrawCompat.rc">
|
||||||
|
@ -20,6 +20,18 @@ namespace Dll
|
|||||||
return thread;
|
return thread;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string getEnvVar(const char* var)
|
||||||
|
{
|
||||||
|
const DWORD size = GetEnvironmentVariable(var, nullptr, 0);
|
||||||
|
std::string value(size, 0);
|
||||||
|
if (!value.empty())
|
||||||
|
{
|
||||||
|
GetEnvironmentVariable(var, &value.front(), size);
|
||||||
|
value.pop_back();
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
void pinModule(HMODULE module)
|
void pinModule(HMODULE module)
|
||||||
{
|
{
|
||||||
HMODULE dummy = nullptr;
|
HMODULE dummy = nullptr;
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
|
|
||||||
#define VISIT_PUBLIC_DDRAW_PROCS(visit) \
|
#define VISIT_PUBLIC_DDRAW_PROCS(visit) \
|
||||||
@ -68,6 +70,7 @@ namespace Dll
|
|||||||
};
|
};
|
||||||
|
|
||||||
HANDLE createThread(unsigned(__stdcall* threadProc)(void*), unsigned int* threadId, int priority, unsigned initFlags = 0);
|
HANDLE createThread(unsigned(__stdcall* threadProc)(void*), unsigned int* threadId, int priority, unsigned initFlags = 0);
|
||||||
|
std::string getEnvVar(const char* var);
|
||||||
void pinModule(HMODULE module);
|
void pinModule(HMODULE module);
|
||||||
void pinModule(LPCSTR moduleName);
|
void pinModule(LPCSTR moduleName);
|
||||||
void pinModule(LPCWSTR moduleName);
|
void pinModule(LPCWSTR moduleName);
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
#include <Common/Log.h>
|
#include <Common/Log.h>
|
||||||
#include <Common/Path.h>
|
#include <Common/Path.h>
|
||||||
#include <Common/Time.h>
|
#include <Common/Time.h>
|
||||||
|
#include <Config/Parser.h>
|
||||||
#include <D3dDdi/Hooks.h>
|
#include <D3dDdi/Hooks.h>
|
||||||
#include <DDraw/DirectDraw.h>
|
#include <DDraw/DirectDraw.h>
|
||||||
#include <DDraw/Hooks.h>
|
#include <DDraw/Hooks.h>
|
||||||
@ -111,14 +112,7 @@ namespace
|
|||||||
|
|
||||||
void printEnvironmentVariable(const char* var)
|
void printEnvironmentVariable(const char* var)
|
||||||
{
|
{
|
||||||
const DWORD size = GetEnvironmentVariable(var, nullptr, 0);
|
Compat::Log() << "Environment variable " << var << " = \"" << Dll::getEnvVar(var) << '"';
|
||||||
std::string value(size, 0);
|
|
||||||
if (!value.empty())
|
|
||||||
{
|
|
||||||
GetEnvironmentVariable(var, &value.front(), size);
|
|
||||||
value.pop_back();
|
|
||||||
}
|
|
||||||
Compat::Log() << "Environment variable " << var << " = \"" << value << '"';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setDpiAwareness()
|
void setDpiAwareness()
|
||||||
@ -175,11 +169,13 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
|
|||||||
|
|
||||||
auto processPath(Compat::getModulePath(nullptr));
|
auto processPath(Compat::getModulePath(nullptr));
|
||||||
Compat::Log::initLogging(processPath);
|
Compat::Log::initLogging(processPath);
|
||||||
|
|
||||||
Compat::Log() << "Process path: " << processPath.u8string();
|
Compat::Log() << "Process path: " << processPath.u8string();
|
||||||
printEnvironmentVariable("__COMPAT_LAYER");
|
|
||||||
auto currentDllPath(Compat::getModulePath(hinstDLL));
|
auto currentDllPath(Compat::getModulePath(hinstDLL));
|
||||||
Compat::Log() << "Loading DDrawCompat " << (lpvReserved ? "statically" : "dynamically") << " from " << currentDllPath.u8string();
|
Compat::Log() << "Loading DDrawCompat " << (lpvReserved ? "statically" : "dynamically") << " from " << currentDllPath.u8string();
|
||||||
|
printEnvironmentVariable("__COMPAT_LAYER");
|
||||||
|
|
||||||
|
Config::Parser::loadAllConfigFiles(processPath);
|
||||||
|
|
||||||
auto systemPath(Compat::getSystemPath());
|
auto systemPath(Compat::getSystemPath());
|
||||||
if (Compat::isEqual(currentDllPath.parent_path(), systemPath))
|
if (Compat::isEqual(currentDllPath.parent_path(), systemPath))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user