Skip to content

Commit

Permalink
Revert: Reloaded Bootstrapper to not use JSON for smaller binary & fa…
Browse files Browse the repository at this point in the history
…ster boot.
  • Loading branch information
Sewer56 committed Nov 28, 2022
1 parent fe1db13 commit 640440a
Show file tree
Hide file tree
Showing 7 changed files with 22,926 additions and 36 deletions.
27 changes: 1 addition & 26 deletions source/Reloaded.Mod.Launcher.Lib/Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,32 +292,7 @@ private static void SetLoaderPaths(LoaderConfig config, string launcherDirectory
config.UpdatePaths(launcherDirectory, Resources.ErrorLoaderNotFound.Get());

// Update Environment Variables
Task.Run(() =>
{
RegisterLoaderPaths(config);
});
}

internal static void RegisterLoaderPaths(LoaderConfig config, bool onlyEssentialVars = false)
{
// In case user closes launcher before changes are propagated.
Environment.SetEnvironmentVariable("RELOADEDII_LOADER64", config.LoaderPath64, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("RELOADEDII_LOADER32", config.LoaderPath32, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("RELOADEDII_LAUNCHER", config.LauncherPath, EnvironmentVariableTarget.Process);

// DO NOT CHANGE THIS METHOD, BREAKS BACKCOMPAT
// THIS IS SLOW, RUN THIS SECOND
// Get the user classes subkey.
using (var environmentKey = Registry.CurrentUser.OpenSubKey("Environment", true))
{
environmentKey.SetValue("RELOADEDII_LOADER64", config.LoaderPath64);
environmentKey.SetValue("RELOADEDII_LOADER32", config.LoaderPath32);
environmentKey.SetValue("RELOADEDII_LAUNCHER", config.LauncherPath);
}

// Fires WM_SETTINGCHANGE indirectly.
if (!onlyEssentialVars)
Environment.SetEnvironmentVariable("RELOADEDIIMODS", config.GetModConfigDirectory(), EnvironmentVariableTarget.User);
Task.Run(() => Environment.SetEnvironmentVariable("RELOADEDIIMODS", config.GetModConfigDirectory(), EnvironmentVariableTarget.User));
}

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion source/Reloaded.Mod.Launcher.Lib/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ private static void LaunchApplicationAndExit(string applicationToLaunch)
// Acquire arguments
var loaderConfig = IoC.Get<LoaderConfig>();
loaderConfig.UpdatePaths(Paths.CurrentProgramFolder, Resources.ErrorLoaderNotFound.Get());
Setup.RegisterLoaderPaths(loaderConfig, true);
IConfig<LoaderConfig>.ToPath(loaderConfig, Paths.LoaderConfigPath);

_commandLineArguments.TryGetValue(Constants.ParameterArguments, out var arguments);
Expand Down
40 changes: 36 additions & 4 deletions source/Reloaded.Mod.Loader.Bootstrapper/LoaderConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,49 @@
#include "LoaderConfig.h"
#include "Utilities.h"
#include <Shlobj_core.h>
#include <fstream>
#include <locale>
#include <codecvt>
#include "ReloadedPaths.h"

#pragma warning(disable:4996) // _CRT_SECURE_NO_WARNINGS
LoaderConfig::LoaderConfig()
{
// Get path to AppData
char_t buffer[32767]; // Max Windows10+ path.
BOOL result = SHGetSpecialFolderPath(nullptr, buffer, CSIDL_APPDATA, false);

if (!result)
throw std::exception("Failed to obtain the path of the AppData folder.");

// Get path to Reloaded Config
const string_t appData = string_t(buffer);
const string_t reloadedConfigPath = appData + L"\\Reloaded-Mod-Loader-II\\ReloadedII.json";

if (!Utilities::file_exists(reloadedConfigPath))
throw std::exception("Reloaded config has not been found.");

// Get loader path.
std::ifstream configFile = std::ifstream(reloadedConfigPath);
config = json::parse(configFile);
}

string_t LoaderConfig::get_loader_path()
{
#if _WIN64
const string_t loaderPath = _wgetenv(L"RELOADEDII_LOADER64");
const std::string stringLoaderPath = config["LoaderPath64"];
#else
const string_t loaderPath = _wgetenv(L"RELOADEDII_LOADER32");
const std::string stringLoaderPath = config["LoaderPath32"];
#endif


/* std::string is non-wide and will not handle unicode characters on Windows.
* Need to convert back to wide characters.
*
* This is to support file paths with international locale e.g. Cyrillic, Chinese, Japanese.
*/
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
const string_t loaderPath = converter.from_bytes(stringLoaderPath);

if (!Utilities::file_exists(loaderPath))
throw std::exception("Reloaded Mod Loader DLL has not been found.");

Expand All @@ -34,7 +64,9 @@ string_t LoaderConfig::get_runtime_config_path()

string_t LoaderConfig::get_launcher_path()
{
const string_t launcherPath = _wgetenv(L"RELOADEDII_LAUNCHER");
const std::string stringLauncherPath = config["LauncherPath"];
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
const string_t launcherPath = converter.from_bytes(stringLauncherPath);

if (!Utilities::file_exists(launcherPath))
throw std::exception("Reloaded Mod Loader DLL has not been found.");
Expand Down
14 changes: 9 additions & 5 deletions source/Reloaded.Mod.Loader.Bootstrapper/LoaderConfig.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
#pragma once
#include "nlohmann/json.hpp"
#include "CoreCLR.hpp"
#include "ReloadedPaths.h"

using json = nlohmann::json;

class LoaderConfig
{
public:
LoaderConfig() { };
LoaderConfig();
~LoaderConfig() = default;

static string_t get_loader_path();
static string_t get_launcher_path();
static string_t get_runtime_config_path();
json config;

static ReloadedPaths get_loader_paths();
string_t get_loader_path();
string_t get_launcher_path();
string_t get_runtime_config_path();

ReloadedPaths get_loader_paths();
};

Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
<ClInclude Include="LoaderConfig.h" />
<ClInclude Include="nethost\nethost.h" />
<ClInclude Include="framework.h" />
<ClInclude Include="nlohmann\json.hpp" />
<ClInclude Include="pch.h" />
<ClInclude Include="ReloadedPaths.h" />
<ClInclude Include="resource.h" />
Expand Down
4 changes: 4 additions & 0 deletions source/Reloaded.Mod.Loader.Bootstrapper/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
#include "Shlobj_core.h"
#include "ReloadedPaths.h"
#include "CoreCLR.hpp"
#include "nlohmann/json.hpp"

#include <locale>
#include <codecvt>
#include <string>
#include "Utilities.h"
#include "LoaderConfig.h"
#include <shellapi.h>
#include <sstream>
#include "EntryPointParameter.h"

using json = nlohmann::json;

// Constants
static string_t PORTABLE_MODE_FILE = L"ReloadedPortable.txt";
const string_t PARAMETER_LAUNCH = L"--launch";
Expand Down
Loading

0 comments on commit 640440a

Please sign in to comment.