Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add and run pre-commit config #172

Merged
merged 7 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
17 changes: 17 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: trailing-whitespace
exclude: ^vendor/
- id: end-of-file-fixer
exclude: ^vendor/
- id: check-added-large-files
exclude: ^vendor/
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v19.1.7
hooks:
- id: clang-format
exclude: ^vendor/
14 changes: 9 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ The project is using [C++20](https://en.cppreference.com/w/cpp/20), any C++20 fe
### General

* Use [EditorConfig](https://editorconfig.org/) and [clang-format](https://clang.llvm.org/docs/ClangFormat.html) to style your code before pushing.
* Ideally, set up pre-commit hook using [pre-commit](https://pre-commit.com).
All that should be required to set it up is to have [Python 3.x](https://www.python.org/) with [pip](https://pypi.org/project/pip/) installed and run the following command inside of the cloned repository folder: `pip install pre-commit && pre-commit install`
* Note that first commit after setting up the hook will take longer as it has to download required packages.
* You don't have to run it every single time there is some update, hook is auto-updated after initial installation.
wopss marked this conversation as resolved.
Show resolved Hide resolved
* Use names that describe the purpose or intent of the object. Names should be self-explanatory and easily recognizable in the context.
* Minimize the use of abbreviations that would likely be unknown to someone outside the project.
* Use `auto` whenever it is possible, if the intended type is the correct one (in case of primitive types).
Expand Down Expand Up @@ -74,15 +78,15 @@ Fix #1

## Declare reverse engineered content

SDK doesn't include every member of classes. Most of the knowledge is generated using [RED4.RTTIDumper](https://github.com/WopsS/RED4.RTTIDumper),
SDK doesn't include every member of classes. Most of the knowledge is generated using [RED4.RTTIDumper](https://github.com/WopsS/RED4.RTTIDumper),
through the RTTI system.

Say you discovered a new member in a class, and you want to write a PR. This section will give you a tour to make it
Say you discovered a new member in a class, and you want to write a PR. This section will give you a tour to make it
right and compliant with the SDK.

### Prepare your environment

Clone and setup [RED4.RTTIDumper](https://github.com/WopsS/RED4.RTTIDumper), read through the [build](https://github.com/WopsS/RED4.RTTIDumper?tab=readme-ov-file#build-instructions)
Clone and setup [RED4.RTTIDumper](https://github.com/WopsS/RED4.RTTIDumper), read through the [build](https://github.com/WopsS/RED4.RTTIDumper?tab=readme-ov-file#build-instructions)
instructions.
Make sure to update RED4ext.SDK dependency, in `<RTTIDumper>/deps/red4ext.sdk` to use the latest snapshot of the SDK.
You can comment each line adding a [custom writer](https://github.com/WopsS/RED4.RTTIDumper/blob/4feaad12de106e395b7a637031432ea3caa2cf10/src/Main.cpp#L29).
Expand Down Expand Up @@ -165,12 +169,12 @@ using VehicleObject = vehicle::BaseObject;
4. Copy the new generated folder `Natives` from the dump and paste it within RED4ext.SDK.
5. If you see a difference on files you didn't touch, ask around before proceeding.
6. It should have [generated a stub](https://github.com/WopsS/RED4ext.SDK/blob/8730efb3aa0fed84e040997ca3e07571d5c86803/include/RED4ext/Scripting/Natives/Generated/vehicle/BaseObject.hpp)
for the file in `Generated` folder. It *disables* the generated file using comments, and include the native
for the file in `Generated` folder. It *disables* the generated file using comments, and include the native
declaration we added.

### Ready to push

After isolating the changes we made in the clone of SDK in RTTI.Dumper, we can mirror them in our own clone of
After isolating the changes we made in the clone of SDK in RTTI.Dumper, we can mirror them in our own clone of
RED4ext.SDK.
You are now ready to write a nice commit, push and send a PR.

Expand Down
18 changes: 9 additions & 9 deletions THIRD_PARTY_LICENSES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

Copyright (c) 2019-2025 Advanced Micro Devices, Inc. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 changes: 9 additions & 9 deletions examples/native_class_redscript/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <RED4ext/RED4ext.hpp>
#include <RED4ext/Scripting/IScriptable.hpp>
#include <RED4ext/RTTITypes.hpp>
#include <RED4ext/Scripting/IScriptable.hpp>

struct MyCustomClass : RED4ext::IScriptable
{
Expand Down Expand Up @@ -33,15 +33,15 @@ RED4EXT_C_EXPORT void RED4EXT_CALL RegisterTypes()

RED4EXT_C_EXPORT void RED4EXT_CALL PostRegisterTypes()
{
auto rtti = RED4ext::CRTTISystem::Get();
auto scriptable = rtti->GetClass("IScriptable");
cls.parent = scriptable;
auto rtti = RED4ext::CRTTISystem::Get();
auto scriptable = rtti->GetClass("IScriptable");
cls.parent = scriptable;

RED4ext::CBaseFunction::Flags flags = { .isNative = true };
auto getNumber = RED4ext::CClassFunction::Create(&cls, "GetNumber", "GetNumber", &GetNumber);
getNumber->flags = flags;
getNumber->SetReturnType("Float");
cls.RegisterFunction(getNumber);
RED4ext::CBaseFunction::Flags flags = {.isNative = true};
auto getNumber = RED4ext::CClassFunction::Create(&cls, "GetNumber", "GetNumber", &GetNumber);
getNumber->flags = flags;
getNumber->SetReturnType("Float");
cls.RegisterFunction(getNumber);
}

RED4EXT_C_EXPORT bool RED4EXT_CALL Main(RED4ext::PluginHandle aHandle, RED4ext::EMainReason aReason,
Expand Down
2 changes: 1 addition & 1 deletion examples/native_globals_redscript/CustomGlobals.reds
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// These are headers that can go in any redscript file
// You can then call them from anywhere
public static native func SummonVehiclePlease() -> Void
public static native func CustomFunctionTwo(var_one: Float, var_two: Vector4) -> Void
public static native func CustomFunctionTwo(var_one: Float, var_two: Vector4) -> Void
2 changes: 1 addition & 1 deletion include/RED4ext/Api/v0/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct Logger

void (*Info)(PluginHandle aHandle, const char* aMessage);
void (*InfoF)(PluginHandle aHandle, const char* aFormat, ...);

void (*InfoW)(PluginHandle aHandle, const wchar_t* aMessage);
void (*InfoWF)(PluginHandle aHandle, const wchar_t* aFormat, ...);

Expand Down
3 changes: 1 addition & 2 deletions include/RED4ext/Buffer-inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,5 @@ RED4EXT_INLINE void RED4ext::DeferredDataBufferCopyToken::OnLoaded(LoadedCallbac
{
JobQueue jobQueue;
jobQueue.Wait(job);
jobQueue.Dispatch([self = self.Lock(), callback = std::move(aCallback)]()
{ callback(self); });
jobQueue.Dispatch([self = self.Lock(), callback = std::move(aCallback)]() { callback(self); });
}
2 changes: 1 addition & 1 deletion include/RED4ext/CNamePool-inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <RED4ext/CNamePool.hpp>
#endif

#include <RED4ext/Detail/AddressHashes.hpp>
#include <RED4ext/Common.hpp>
#include <RED4ext/Detail/AddressHashes.hpp>
#include <RED4ext/Relocation.hpp>

RED4EXT_INLINE RED4ext::CName RED4ext::CNamePool::Add(const char* aText)
Expand Down
6 changes: 4 additions & 2 deletions include/RED4ext/CString-inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ RED4EXT_INLINE RED4ext::CString::CString(const char* aText, Memory::IAllocator*
RED4EXT_INLINE RED4ext::CString::CString(const char* aText, uint32_t aLength, Memory::IAllocator* aAllocator)
: CString(aAllocator)
{
static UniversalRelocFunc<CString* (*)(CString*, const char*, uint32_t)> func(Detail::AddressHashes::CString_ctor_span);
static UniversalRelocFunc<CString* (*)(CString*, const char*, uint32_t)> func(
Detail::AddressHashes::CString_ctor_span);
func(this, aText, aLength);
}

Expand All @@ -40,7 +41,8 @@ RED4EXT_INLINE RED4ext::CString::CString(const std::string& aText, Memory::IAllo
RED4EXT_INLINE RED4ext::CString::CString(const std::string_view& aText, Memory::IAllocator* aAllocator)
: CString(aAllocator)
{
static UniversalRelocFunc<CString* (*)(CString*, const char*, uint32_t)> func(Detail::AddressHashes::CString_ctor_span);
static UniversalRelocFunc<CString* (*)(CString*, const char*, uint32_t)> func(
Detail::AddressHashes::CString_ctor_span);
func(this, aText.data(), static_cast<uint32_t>(aText.size()));
}

Expand Down
4 changes: 2 additions & 2 deletions include/RED4ext/Detail/Memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ template<typename T>
using ResolveAllocatorType = typename StaticAllocatorTypeResolver<T>::type;

template<typename T>
concept IsSafeDestructible = std::is_destructible_v<T> &&
(!std::is_polymorphic_v<T> || std::has_virtual_destructor_v<T>);
concept IsSafeDestructible =
std::is_destructible_v<T> && (!std::is_polymorphic_v<T> || std::has_virtual_destructor_v<T>);

template<typename T>
concept IsAllocator = std::is_base_of_v<Memory::IAllocator, T>;
Expand Down
97 changes: 50 additions & 47 deletions include/RED4ext/Dump/Reflection-inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ RED4EXT_INLINE void Dump(std::filesystem::path aOutPath, std::filesystem::path a
std::unordered_map<std::string, std::vector<std::string>> prefixHierarchy;

// Trim the preceeding lower-case suffix, this seems to be either a namespace or directory, or both
auto GetPrefix = [](const std::string& aInput) -> std::string {
auto GetPrefix = [](const std::string& aInput) -> std::string
{
size_t i = 0;

// Special case for AI
Expand Down Expand Up @@ -67,50 +68,52 @@ RED4EXT_INLINE void Dump(std::filesystem::path aOutPath, std::filesystem::path a
};

// First pass gather all properties and descriptors
rttiSystem->types.for_each([&descriptorMap, GetPrefix, &prefixHierarchy,
aPropertyHolders](RED4ext::CName aName, RED4ext::CBaseRTTIType*& aType) {
if (aType->GetType() == RED4ext::ERTTIType::Class)
rttiSystem->types.for_each(
[&descriptorMap, GetPrefix, &prefixHierarchy, aPropertyHolders](RED4ext::CName aName,
RED4ext::CBaseRTTIType*& aType)
{
auto classType = static_cast<const RED4ext::CClass*>(aType);
if (classType->flags.isNative)
if (aType->GetType() == RED4ext::ERTTIType::Class)
{
ClassDependencyBuilder builder;
builder.pType = classType;

for (uint32_t i = 0; i < classType->unk118.size; ++i)
auto classType = static_cast<const RED4ext::CClass*>(aType);
if (classType->flags.isNative)
{
auto prop = classType->unk118.entries[i];
if (!prop->flags.inValueHolder)
{
builder.mPropertyMap.emplace(prop->valueOffset, prop);
}
else if (aPropertyHolders)
ClassDependencyBuilder builder;
builder.pType = classType;

for (uint32_t i = 0; i < classType->unk118.size; ++i)
{
builder.mHolderPropertyMap.emplace(prop->valueOffset, prop);
auto prop = classType->unk118.entries[i];
if (!prop->flags.inValueHolder)
{
builder.mPropertyMap.emplace(prop->valueOffset, prop);
}
else if (aPropertyHolders)
{
builder.mHolderPropertyMap.emplace(prop->valueOffset, prop);
}
}
}

descriptorMap.emplace(classType, builder);
descriptorMap.emplace(classType, builder);
}
}
}

switch (aType->GetType())
{
case RED4ext::ERTTIType::Class:
case RED4ext::ERTTIType::Enum:
case RED4ext::ERTTIType::BitField:
{
std::string prefix = GetPrefix(aName.ToString());
if (!prefix.empty())
switch (aType->GetType())
{
case RED4ext::ERTTIType::Class:
case RED4ext::ERTTIType::Enum:
case RED4ext::ERTTIType::BitField:
{
prefixHierarchy[prefix] = std::vector<std::string>();
std::string prefix = GetPrefix(aName.ToString());
if (!prefix.empty())
{
prefixHierarchy[prefix] = std::vector<std::string>();
}
break;
}
break;
}
default:
break;
}
});
default:
break;
}
});

// Build a mapped list of nested prefixes
for (auto& [prefix, children] : prefixHierarchy)
Expand Down Expand Up @@ -194,9 +197,8 @@ RED4EXT_INLINE void Dump(std::filesystem::path aOutPath, std::filesystem::path a
if (it != prefixHierarchy.end())
{
pathPrefix = std::accumulate(it->second.begin(), it->second.end(), std::string(),
[](const std::string& a, const std::string& b) -> std::string {
return a + (a.length() > 0 ? "/" : "") + b;
});
[](const std::string& a, const std::string& b) -> std::string
{ return a + (a.length() > 0 ? "/" : "") + b; });
}

pathPrefix += "/";
Expand Down Expand Up @@ -252,25 +254,26 @@ RED4EXT_INLINE void Dump(std::filesystem::path aOutPath, std::filesystem::path a
};

// Remove the prefix from the class
auto SanitizeType = [GetPrefix](const RED4ext::CBaseRTTIType* aType) -> std::string {
auto SanitizeType = [GetPrefix](const RED4ext::CBaseRTTIType* aType) -> std::string
{
auto name = aType->GetName();
std::string fullName = name.ToString();
auto prefix = GetPrefix(fullName);
return fullName.substr(prefix.size(), fullName.size() - prefix.size());
};

// Convert the prefixes into a namespace
auto GetNamespace = [&prefixHierarchy](const std::string& aPrefix) -> std::string {
auto GetNamespace = [&prefixHierarchy](const std::string& aPrefix) -> std::string
{
std::string ns;
if (!aPrefix.empty())
{
auto it = prefixHierarchy.find(aPrefix);
if (it != prefixHierarchy.end())
{
ns = std::accumulate(it->second.begin(), it->second.end(), std::string(),
[](const std::string& a, const std::string& b) -> std::string {
return a + (a.length() > 0 ? "::" : "") + b;
});
[](const std::string& a, const std::string& b) -> std::string
{ return a + (a.length() > 0 ? "::" : "") + b; });
}
}

Expand Down Expand Up @@ -312,8 +315,8 @@ RED4EXT_INLINE void Dump(std::filesystem::path aOutPath, std::filesystem::path a

std::regex invalidChars(INVALID_CHARACTERS);
std::regex invalidKeywords(INVALID_KEYWORDS);
NameSantizer nameSanitizer = [invalidChars, invalidKeywords](const std::string& input,
bool& modify) -> std::string {
NameSantizer nameSanitizer = [invalidChars, invalidKeywords](const std::string& input, bool& modify) -> std::string
{
modify = std::regex_search(input, invalidChars) || std::regex_search(input, invalidKeywords);
std::string output = std::regex_replace(std::regex_replace(input, invalidChars, "_"), invalidKeywords, "$&_");
if (!input.empty() && isdigit(input[0])) // Starting with a number is invalid, prefix it
Expand Down Expand Up @@ -514,7 +517,7 @@ RED4EXT_INLINE void EnumFileDescriptor::EmitFile(std::filesystem::path aOutPath,
if (nsIndex != std::string::npos)
{
auto ns = nameQualified.substr(0, nsIndex - 1);
o << "namespace " << ns << " { " << std::endl;
o << "namespace " << ns << " {" << std::endl;
o << "enum class " << name;
}
else
Expand Down Expand Up @@ -693,7 +696,7 @@ RED4EXT_INLINE void BitfieldFileDescriptor::EmitFile(std::filesystem::path aOutP
if (nsIndex != std::string::npos)
{
auto ns = nameQualified.substr(0, nsIndex - 1);
o << "namespace " << ns << " { " << std::endl;
o << "namespace " << ns << " {" << std::endl;
o << "struct " << name;
}
else
Expand Down
2 changes: 1 addition & 1 deletion include/RED4ext/GpuApi/D3D12MemAlloc-inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ RED4EXT_INLINE HRESULT D3D12MA::Allocator::CreateResource(const ALLOCATION_DESC*
static RED4ext::UniversalRelocFunc<func_t> func(RED4ext::Detail::AddressHashes::Allocator_CreateResource);
return func(this, pAllocDesc, pResourceDesc, InitialResourceState, pOptimizedClearValue, ppAllocation, riidResource,
ppvResource);
}
}
4 changes: 2 additions & 2 deletions include/RED4ext/Hashing/Murmur3.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#pragma once

#include <cstdint>
#include <RED4ext/Common.hpp>
#include <cstdint>

namespace RED4ext
{
uint32_t Murmur3_32(const char* aText, const uint32_t aSeed = 0X5EEDBA5E);
uint32_t Murmur3_32(const uint8_t* aKey, const size_t aLength, const uint32_t aSeed = 0X5EEDBA5E);
}
} // namespace RED4ext

#ifdef RED4EXT_HEADER_ONLY
#include <RED4ext/Hashing/Murmur3-inl.hpp>
Expand Down
2 changes: 1 addition & 1 deletion include/RED4ext/ISerializable-inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include <RED4ext/ISerializable.hpp>
#endif

#include <RED4ext/Detail/AddressHashes.hpp>
#include <RED4ext/CString.hpp>
#include <RED4ext/Detail/AddressHashes.hpp>
#include <RED4ext/Memory/Allocators.hpp>
#include <RED4ext/RTTITypes.hpp>
#include <RED4ext/Relocation.hpp>
Expand Down
2 changes: 1 addition & 1 deletion include/RED4ext/Memory/SharedPtr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

#include <Windows.h>

#include <RED4ext/Detail/AddressHashes.hpp>
#include <RED4ext/Common.hpp>
#include <RED4ext/Detail/AddressHashes.hpp>
#include <RED4ext/Memory/Utils.hpp>
#include <RED4ext/Relocation.hpp>

Expand Down
Loading