2
Fork 0

feat: Allow providing custom DLL search path

This commit is contained in:
Lucas Schwiderski 2022-10-19 18:44:02 +02:00
parent e18ae5b371
commit 4cdd7f1b39
Signed by: lucas
GPG key ID: AA12679AAA6DF4D8
3 changed files with 74 additions and 33 deletions

View file

@ -1,7 +1,16 @@
// oodle-cli.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <locale>
#include <codecvt>
#include <filesystem>
#include <stdlib.h>
#include <windows.h>
#include <libloaderapi.h>
@ -9,22 +18,75 @@
#include "oodle2.h"
const LPCWSTR LIB_NAME = L"oo2core_8_win64";
// Printing a `LPCWSTR` is a pain, so it's much easier to just have this second variable.
// It's not like this is going to change often.
const char* LIB_FILE = "oo2core_8_win64.dll";
void usage() {
std::cerr <<
"Usage: oodle-cli [OPTIONS] <in_file> <out_file> <uncompressed_size>\n"
"Usage: oodle-cli <in_file> <out_file> <uncompressed_size>\n"
"Decompress a <in_file> to <out_file> using the Oodle algorithm.\n"
"The size of the uncompressed data must be known.\n"
"\n"
" -v Increase verbosity. May be specified up to three times.\n";
"The following environmental variables are recognized:\n"
" VERBOSITY: An integer between 0 and 3, with 0 being no logging and 3 being maximum logging.\n"
" DLL_SEARCH_PATH: A color (':') separated list of directories to search for the Oodle library. May be relative to the current working directory.\n";
}
int main(int argc, char* argv[])
{
HINSTANCE hDLL;
hDLL = LoadLibrary(LIB_NAME);
if (argc < 4) {
std::cerr << "Arguments missing!\n\n";
usage();
return 1;
}
char* var;
size_t len;
int verbosity = 0;
if (_dupenv_s(&var, &len, "VERBOSITY") == 0) {
if (var) {
verbosity = std::stoi(std::string(var, len));
std::cout << "Setting Oodle verbosity to " << verbosity << ".\n";
free(var);
}
}
else {
std::cerr << "Failed to read environment variable 'VERBOSITY'. Falling back to default.\n";
}
DWORD load_flags = 0;
if (_dupenv_s(&var, &len, "DLL_SEARCH_PATH") == 0) {
if (var) {
std::string search_path(var, len);
std::istringstream ss(search_path);
std::string token;
while (std::getline(ss, token, ':')) {
auto path = std::filesystem::path(token);
if (!path.is_absolute()) {
path = std::filesystem::absolute(path);
}
if (!path.is_absolute() || !AddDllDirectory(path.c_str())) {
std::cerr << "Failed to add DLL search path: '" << token << "'!\n";
}
else {
std::cout << "Added DLL search path: '" << path << "'.\n";
}
}
load_flags = LOAD_LIBRARY_SEARCH_DEFAULT_DIRS;
free(var);
}
}
else {
std::cerr << "Failed to read environment variable 'DLL_SEARCH_PATH'. Skipping.\n";
}
HINSTANCE hDLL = LoadLibraryEx(LIB_NAME, NULL, load_flags);
if (hDLL == NULL) {
std::cerr << "ERROR: Couldn't find library file '" << LIB_NAME << ".dll'!\n";
std::cerr << "ERROR: Couldn't find library file '" << LIB_FILE << "'!\n";
return 1;
}
@ -33,32 +95,12 @@ int main(int argc, char* argv[])
std::cerr << "ERROR: The library is incompatible!\n";
return 1;
}
int verbosity = 0;
size_t i = 1;
for (; i < argc; i++)
{
if (strcmp(argv[i], "-v") == 0) {
if (verbosity < 3) {
verbosity++;
}
}
else {
break;
}
}
std::string in_name = argv[1];
std::string out_name = argv[2];
size_t raw_buffer_size = std::stoi(argv[3]);
if (argc - i < 3) {
std::cerr << "Arguments missing!\n\n";
usage();
return 1;
}
std::string in_name = argv[i];
std::string out_name = argv[i + 1];
size_t raw_buffer_size = std::stoi(argv[i + 2]);
std::cout << "Attempting to decompress from '" << in_name << "' to '" << out_name << "' (" << raw_buffer_size << " bytes), with verbosity " << verbosity << ".\n";
std::cout << "Attempting to decompress from '" << in_name << "' to '" << out_name << "' (" << raw_buffer_size << " bytes).\n";
std::ifstream in_file(in_name, std::ios::binary | std::ios::ate);
if (!in_file) {

View file

@ -104,6 +104,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@ -118,6 +119,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>

View file

@ -20,9 +20,6 @@
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="\\wsl%24\Ubuntu\home\gerlui\projects\oodle-bin\lib\oodle\oodle2.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="oodle2.h">
<Filter>Header Files</Filter>
</ClInclude>