1
0
mirror of https://github.com/narzoul/DDrawCompat synced 2024-12-30 08:55:36 +01:00

Added config parser

This commit is contained in:
narzoul 2021-04-25 23:58:57 +02:00
parent 97f56131ac
commit 6f92c8ef22
9 changed files with 158 additions and 10 deletions

View File

@ -1,7 +1,13 @@
#include <Common/Path.h>
#include <Dll/Dll.h>
namespace Compat
{
std::filesystem::path getEnvPath(const char* envVar)
{
return Dll::getEnvVar(envVar);
}
std::filesystem::path getModulePath(HMODULE module)
{
wchar_t path[MAX_PATH];

View File

@ -6,6 +6,7 @@
namespace Compat
{
std::filesystem::path getEnvPath(const char* envVar);
std::filesystem::path getModulePath(HMODULE module);
std::filesystem::path getSystemPath();
bool isEqual(const std::filesystem::path& p1, const std::filesystem::path& p2);

View 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);
}
}
}

View 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);
}
}

View File

@ -178,6 +178,7 @@
<ClInclude Include="Common\ScopedCriticalSection.h" />
<ClInclude Include="Common\Time.h" />
<ClInclude Include="Config\Config.h" />
<ClInclude Include="Config\Parser.h" />
<ClInclude Include="D3dDdi\Adapter.h" />
<ClInclude Include="D3dDdi\AdapterCallbacks.h" />
<ClInclude Include="D3dDdi\AdapterFuncs.h" />
@ -271,6 +272,7 @@
<ClCompile Include="Common\Hook.cpp" />
<ClCompile Include="Common\Path.cpp" />
<ClCompile Include="Common\Time.cpp" />
<ClCompile Include="Config\Parser.cpp" />
<ClCompile Include="D3dDdi\Adapter.cpp" />
<ClCompile Include="D3dDdi\AdapterCallbacks.cpp" />
<ClCompile Include="D3dDdi\AdapterFuncs.cpp" />

View File

@ -79,6 +79,9 @@
<Filter Include="Source Files\D3dDdi\Log">
<UniqueIdentifier>{c4d03748-2333-40af-90c7-ae875b07434f}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\Config">
<UniqueIdentifier>{9d313de7-b8ca-49d0-84d2-217f2818d9d3}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Gdi\Gdi.h">
@ -390,6 +393,9 @@
<ClInclude Include="Common\Path.h">
<Filter>Header Files\Common</Filter>
</ClInclude>
<ClInclude Include="Config\Parser.h">
<Filter>Header Files\Config</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Gdi\Gdi.cpp">
@ -611,6 +617,9 @@
<ClCompile Include="Common\Path.cpp">
<Filter>Source Files\Common</Filter>
</ClCompile>
<ClCompile Include="Config\Parser.cpp">
<Filter>Source Files\Config</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="DDrawCompat.rc">

View File

@ -20,6 +20,18 @@ namespace Dll
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)
{
HMODULE dummy = nullptr;

View File

@ -1,5 +1,7 @@
#pragma once
#include <string>
#include <Windows.h>
#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);
std::string getEnvVar(const char* var);
void pinModule(HMODULE module);
void pinModule(LPCSTR moduleName);
void pinModule(LPCWSTR moduleName);

View File

@ -9,6 +9,7 @@
#include <Common/Log.h>
#include <Common/Path.h>
#include <Common/Time.h>
#include <Config/Parser.h>
#include <D3dDdi/Hooks.h>
#include <DDraw/DirectDraw.h>
#include <DDraw/Hooks.h>
@ -111,14 +112,7 @@ namespace
void printEnvironmentVariable(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();
}
Compat::Log() << "Environment variable " << var << " = \"" << value << '"';
Compat::Log() << "Environment variable " << var << " = \"" << Dll::getEnvVar(var) << '"';
}
void setDpiAwareness()
@ -175,11 +169,13 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
auto processPath(Compat::getModulePath(nullptr));
Compat::Log::initLogging(processPath);
Compat::Log() << "Process path: " << processPath.u8string();
printEnvironmentVariable("__COMPAT_LAYER");
auto currentDllPath(Compat::getModulePath(hinstDLL));
Compat::Log() << "Loading DDrawCompat " << (lpvReserved ? "statically" : "dynamically") << " from " << currentDllPath.u8string();
printEnvironmentVariable("__COMPAT_LAYER");
Config::Parser::loadAllConfigFiles(processPath);
auto systemPath(Compat::getSystemPath());
if (Compat::isEqual(currentDllPath.parent_path(), systemPath))