Skip to content

Commit

Permalink
Register custom tweak paths
Browse files Browse the repository at this point in the history
  • Loading branch information
psiberx committed May 9, 2023
1 parent d6d5d51 commit ed197a8
Show file tree
Hide file tree
Showing 16 changed files with 265 additions and 59 deletions.
10 changes: 10 additions & 0 deletions lib/Core/Raw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,16 @@ class OffsetPtr
return base + offset;
}

inline static Type* Get(void* aBase)
{
return OffsetPtr(aBase);
}

inline static Type& Ref(void* aBase)
{
return OffsetPtr(aBase);
}

uintptr_t base;
};
}
5 changes: 5 additions & 0 deletions lib/Red/TypeInfo/Resolving.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ inline CClass* ResolveClass()
return CRTTISystem::Get()->GetClass(s_name);
}

inline void RegisterPendingTypes()
{
CRTTISystem::Get()->GetType("Int32");
}

inline bool IsCompatible(CBaseRTTIType* aLhsType, CBaseRTTIType* aRhsType)
{
if (aLhsType != aRhsType)
Expand Down
2 changes: 1 addition & 1 deletion src/App/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ App::Application::Application(HMODULE aHandle, const RED4ext::Sdk*)
Register<Support::SpdlogProvider>();
Register<Support::RedLibProvider>();

Register<App::TweakService>(Env::TweaksDir());
Register<App::TweakService>(Env::GameDir(), Env::TweaksDir());
}
5 changes: 5 additions & 0 deletions src/App/Environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace App::Env
{
inline std::filesystem::path GameDir()
{
return Core::Runtime::GetRootDir();
}

inline std::filesystem::path TweaksDir()
{
return Core::Runtime::GetRootDir() / L"r6" / L"tweaks";
Expand Down
16 changes: 13 additions & 3 deletions src/App/Facade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
#include "Core/Facades/Container.hpp"
#include "Red/LogChannel.hpp"

void App::Facade::Reload()
bool App::Facade::RegisterDir(Red::CString& aPath)
{
Core::Resolve<TweakService>()->LoadTweaks();
return Core::Resolve<TweakService>()->RegisterDirectory(aPath.c_str());
}

bool App::Facade::RegisterTweak(Red::CString& aPath)
{
return Core::Resolve<TweakService>()->RegisterTweak(aPath.c_str());
}

void App::Facade::ImportAll()
Expand Down Expand Up @@ -35,10 +40,15 @@ void App::Facade::ExecuteTweak(Red::CName aName)
Core::Resolve<TweakService>()->ExecuteTweak(aName);
}

void App::Facade::Reload()
{
Core::Resolve<TweakService>()->LoadTweaks();
}

bool App::Facade::Require(Red::CString& aVersion)
{
const auto requirement = semver::from_string_noexcept(aVersion.c_str());
return requirement.has_value() ? Project::Version >= requirement.value() : false;
return requirement.has_value() && Project::Version >= requirement.value();
}

Red::CString App::Facade::GetVersion()
Expand Down
4 changes: 4 additions & 0 deletions src/App/Facade.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace App
class Facade : public Red::IScriptable
{
public:
static bool RegisterDir(Red::CString& aPath);
static bool RegisterTweak(Red::CString& aPath);
static void ImportAll();
static void ImportDir(Red::CString& aPath);
static void ImportTweak(Red::CString& aPath);
Expand All @@ -22,6 +24,8 @@ class Facade : public Red::IScriptable

RTTI_DEFINE_CLASS(App::Facade, App::Project::Name, {
RTTI_ABSTRACT();
RTTI_METHOD(RegisterDir);
RTTI_METHOD(RegisterTweak);
RTTI_METHOD(ImportAll);
RTTI_METHOD(ImportDir);
RTTI_METHOD(ImportTweak, "Import");
Expand Down
2 changes: 1 addition & 1 deletion src/App/Project.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ constexpr auto Author = "psiberx";
constexpr auto NameW = L"TweakXL";
constexpr auto AuthorW = L"psiberx";

constexpr auto Version = semver::from_string_noexcept("1.1.4").value();
constexpr auto Version = semver::from_string_noexcept("1.1.5").value();
}
60 changes: 24 additions & 36 deletions src/App/Tweaks/Declarative/TweakImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,42 @@ App::TweakImporter::TweakImporter(Core::SharedPtr<Red::TweakDBManager> aManager)
{
}

void App::TweakImporter::ImportTweaks(const std::filesystem::path& aDir,
void App::TweakImporter::ImportTweaks(const Core::Vector<std::filesystem::path>& aImportPaths,
const Core::SharedPtr<App::TweakChangelog>& aChangelog,
bool aDryRun)
{
std::error_code error;
if (!std::filesystem::is_directory(aDir, error))
{
LogError(R"(Directory "{}" not found.)", aDir.string());
return;
}

try
{
LogInfo("Scanning for tweaks...");

TweakChangeset changeset;

const auto tweakDirIt = std::filesystem::recursive_directory_iterator(
aDir, std::filesystem::directory_options::follow_directory_symlink);

for (const auto& entry : tweakDirIt)
for (const auto& importPath : aImportPaths)
{
if (entry.is_regular_file())
std::error_code error;

if (std::filesystem::is_directory(importPath, error))
{
Read(changeset, entry.path(), aDir);
const auto dirIt = std::filesystem::recursive_directory_iterator(
importPath, std::filesystem::directory_options::follow_directory_symlink);

for (const auto& entry : dirIt)
{
if (entry.is_regular_file())
{
Read(changeset, entry.path(), importPath);
}
}
continue;
}

if (std::filesystem::is_regular_file(importPath, error))
{
Read(changeset, importPath, importPath.parent_path());
continue;
}

LogWarning("Can't import \"{}\".", importPath.string());
}

if (!aDryRun)
Expand All @@ -51,28 +61,6 @@ void App::TweakImporter::ImportTweaks(const std::filesystem::path& aDir,
}
}

void App::TweakImporter::ImportTweak(const std::filesystem::path& aPath,
const Core::SharedPtr<App::TweakChangelog>& aChangelog,
bool aDryRun)
{
std::error_code error;
if (!std::filesystem::is_regular_file(aPath, error))
{
LogError(R"(Tweak "{}" not found.)", aPath.string());
return;
}

TweakChangeset changeset;

if (Read(changeset, aPath, aPath.parent_path()))
{
if (!aDryRun)
{
Apply(changeset, aChangelog);
}
}
}

bool App::TweakImporter::Read(App::TweakChangeset& aChangeset, const std::filesystem::path& aPath,
const std::filesystem::path& aDir)
{
Expand Down
5 changes: 1 addition & 4 deletions src/App/Tweaks/Declarative/TweakImporter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ class TweakImporter : Core::LoggingAgent
public:
TweakImporter(Core::SharedPtr<Red::TweakDBManager> aManager);

void ImportTweaks(const std::filesystem::path& aPath,
void ImportTweaks(const Core::Vector<std::filesystem::path>& aImportPaths,
const Core::SharedPtr<App::TweakChangelog>& aChangelog = nullptr,
bool aDryRun = false);
void ImportTweak(const std::filesystem::path& aPath,
const Core::SharedPtr<App::TweakChangelog>& aChangelog = nullptr,
bool aDryRun = false);

private:
bool Read(App::TweakChangeset& aChangeset, const std::filesystem::path& aPath, const std::filesystem::path& aDir);
Expand Down
52 changes: 47 additions & 5 deletions src/App/Tweaks/TweakService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
#include "App/Tweaks/Executable/TweakExecutor.hpp"
#include "Red/TweakDB/Raws.hpp"

App::TweakService::TweakService(std::filesystem::path aTweaksDir)
: m_tweaksDir(std::move(aTweaksDir))
App::TweakService::TweakService(std::filesystem::path aGameDir, std::filesystem::path aTweaksDir)
: m_gameDir(std::move(aGameDir))
, m_tweaksDir(std::move(aTweaksDir))
{
m_importPaths.push_back(m_tweaksDir);
}

void App::TweakService::OnBootstrap()
Expand All @@ -28,7 +30,7 @@ void App::TweakService::LoadTweaks()
{
if (m_manager)
{
m_importer->ImportTweaks(m_tweaksDir, m_changelog);
m_importer->ImportTweaks(m_importPaths, m_changelog);
m_executor->ExecuteTweaks();
m_changelog->CheckForIssues(m_manager);
}
Expand All @@ -38,7 +40,7 @@ void App::TweakService::ImportTweaks()
{
if (m_manager)
{
m_importer->ImportTweaks(m_tweaksDir, m_changelog);
m_importer->ImportTweaks(m_importPaths, m_changelog);
}
}

Expand Down Expand Up @@ -67,8 +69,48 @@ void App::TweakService::CreateTweaksDir()
if (!std::filesystem::create_directories(m_tweaksDir, error))
{
LogError("Cannot create tweaks directory \"{}\": {}.",
m_tweaksDir.string(), error.message());
std::filesystem::relative(m_tweaksDir, m_gameDir).string(), error.message());
return;
}
}
}

bool App::TweakService::RegisterTweak(std::filesystem::path aPath)
{
std::error_code error;

if (aPath.is_relative())
{
aPath = m_gameDir / aPath;
}

if (!std::filesystem::exists(aPath, error) || !std::filesystem::is_regular_file(aPath, error))
{
LogError("Can't register non-existing tweak \"{}\".",
std::filesystem::relative(aPath, m_gameDir).string());
return false;
}

m_importPaths.emplace_back(std::move(aPath));
return true;
}

bool App::TweakService::RegisterDirectory(std::filesystem::path aPath)
{
std::error_code error;

if (aPath.is_relative())
{
aPath = m_gameDir / aPath;
}

if (!std::filesystem::exists(aPath, error) || !std::filesystem::is_directory(aPath, error))
{
LogError("Can't register non-existing tweak directory \"{}\".",
std::filesystem::relative(aPath, m_gameDir).string());
return false;
}

m_importPaths.emplace_back(std::move(aPath));
return true;
}
7 changes: 6 additions & 1 deletion src/App/Tweaks/TweakService.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ class TweakService
, public Core::LoggingAgent
{
public:
TweakService(std::filesystem::path aTweaksDir);
TweakService(std::filesystem::path aGameDir, std::filesystem::path aTweaksDir);

bool RegisterTweak(std::filesystem::path aPath);
bool RegisterDirectory(std::filesystem::path aPath);

void LoadTweaks();
void ImportTweaks();
Expand All @@ -28,7 +31,9 @@ class TweakService
void OnBootstrap() override;
void CreateTweaksDir();

std::filesystem::path m_gameDir;
std::filesystem::path m_tweaksDir;
Core::Vector<std::filesystem::path> m_importPaths;
Core::SharedPtr<Red::TweakDBReflection> m_reflection;
Core::SharedPtr<Red::TweakDBManager> m_manager;
Core::SharedPtr<App::TweakChangelog> m_changelog;
Expand Down
8 changes: 4 additions & 4 deletions src/App/Version.rc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#define VER_PRODUCTVERSION 1,1,4,0
#define VER_FILEVERSION 1,1,4,2304202158
#define VER_PRODUCTVERSION 1,1,5,0
#define VER_FILEVERSION 1,1,5,2305091514

#define VER_PRODUCTNAME_STR "TweakXL\0"
#define VER_PRODUCTVERSION_STR "1.1.4\0"
#define VER_FILEVERSION_STR "1.1.4.2304202158\0"
#define VER_PRODUCTVERSION_STR "1.1.5\0"
#define VER_FILEVERSION_STR "1.1.5.2305091514\0"

1 VERSIONINFO
FILEVERSION VER_FILEVERSION
Expand Down
1 change: 1 addition & 0 deletions src/rtti.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "App/Facade.hpp"
3 changes: 0 additions & 3 deletions support/red4ext/ScriptableTweak.hpp

This file was deleted.

Loading

0 comments on commit ed197a8

Please sign in to comment.