Skip to content

Commit

Permalink
feat: add requirements for script validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jac3km4 committed Dec 13, 2023
1 parent e5a2806 commit d6e8d35
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/RED4ext/Addresses.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,9 @@ constexpr uintptr_t TweakDB_CreateRecord = 0x1401D50D0 - ImageBase; // 48 89 5C
constexpr uintptr_t UpdateRegistrar_RegisterGroupUpdate = 0x1407FC05C - ImageBase; // 48 8B C4 48 89 58 08 48 89 70 10 48 89 78 18 55 41 56 41 57 48 8D 68 B1 48 81 EC D0 00 00 00 48 8B 7D 77 49 8B D9, expected: 1, index: 0
constexpr uintptr_t UpdateRegistrar_RegisterBucketUpdate = 0x1407FBF0C - ImageBase; // 48 8B C4 48 89 58 08 48 89 70 10 48 89 78 18 55 41 56 41 57 48 8D 68 B9 48 81 EC D0 00 00 00 48 8B 7D 77 49 8B D9, expected: 1, index: 0
#pragma endregion

#pragma region ScriptValidator
constexpr uintptr_t ScriptValidator_Validate = 0x14060FC3C - ImageBase; // 48 89 5C 24 ? 48 89 4C 24 ? 55 56 57 41 54 41 55 41 56 41 57 48 8D 6C 24 ? 48 81 EC ? ? ? ? 48 8B C2, expected: 1, index: 0
#pragma endregion
} // namespace RED4ext::Addresses
// clang-format on
79 changes: 79 additions & 0 deletions include/RED4ext/Scripting/ScriptReport-inl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#pragma once

#ifdef RED4EXT_STATIC_LIB
#include <RED4ext/Scripting/ScriptReport.hpp>
#endif

#include <RED4ext/RTTITypes.hpp>

RED4EXT_INLINE RED4ext::ScriptReport::ScriptReport() noexcept
: errors(&unk10)
, maxErrors(0)
, fillErrors(true)
{
}

RED4EXT_INLINE RED4ext::ScriptReport::ScriptReport(RED4ext::DynArray<RED4ext::CString>& aErrors, uint32_t aMaxErrors) noexcept
: errors(&aErrors)
, maxErrors(aMaxErrors)
, fillErrors(true)
{
}

RED4EXT_INLINE void RED4ext::ScriptReport::AddValidationError(const char* aFormat, ...)
{
if (errors && fillErrors)
{
std::va_list args;
va_start(args, aFormat);
errors->PushBack(Format(aFormat, args));
va_end(args);
}
}

RED4EXT_INLINE void RED4ext::ScriptReport::AddBindingError(const char* aFormat, ...)
{
if (errors && fillErrors)
{
std::va_list args;
va_start(args, aFormat);
errors->PushBack(Format(aFormat, args));
va_end(args);
}
}

RED4EXT_INLINE [[nodiscard]] bool RED4ext::ScriptReport::HasErrors() const noexcept
{
return errors && errors->size > 0;
}

RED4EXT_INLINE [[nodiscard]] RED4ext::CString RED4ext::ScriptReport::ToString() const noexcept
{
if (!errors || errors->size == 0)
{
return {};
}

std::string str;
bool eol = false;

for (const auto& error : *errors)
{
if (eol)
{
str.append("\n");
}

str.append(error.c_str());
eol = true;
}

return str.c_str();
}

RED4EXT_INLINE RED4ext::CString RED4ext::ScriptReport::Format(const char* aFormat, std::va_list aArgs)
{
char buffer[4096];
vsnprintf(buffer, sizeof(buffer), aFormat, aArgs);
return buffer;
}
36 changes: 36 additions & 0 deletions include/RED4ext/Scripting/ScriptReport.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <RED4ext/CString.hpp>
#include <RED4ext/DynArray.hpp>
#include <cstdarg>

namespace RED4ext
{
struct ScriptReport
{
ScriptReport() noexcept;

ScriptReport(DynArray<CString>& aErrors, uint32_t aMaxErrors = 0) noexcept;

virtual ~ScriptReport() = default;

virtual void AddValidationError(const char* aFormat, ...);

virtual void AddBindingError(const char* aFormat, ...);

[[nodiscard]] bool HasErrors() const noexcept;

[[nodiscard]] CString ToString() const noexcept;

static CString Format(const char* aFormat, std::va_list aArgs);

bool fillErrors; // 08 - Usually equals to CBaseEngine::scriptsSilentValidation
DynArray<CString> unk10; // 10 - Seems to be unused by the game
DynArray<CString>* errors; // 20 - Usually points to CBaseEngine::scriptsValidationErrors
uint32_t maxErrors; // 28
};
RED4EXT_ASSERT_SIZE(ScriptReport, 0x30);
RED4EXT_ASSERT_OFFSET(ScriptReport, fillErrors, 0x08);
RED4EXT_ASSERT_OFFSET(ScriptReport, errors, 0x20);
RED4EXT_ASSERT_OFFSET(ScriptReport, maxErrors, 0x28);
} // namespace RED4ext
5 changes: 5 additions & 0 deletions src/Scripting/ScriptReport.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#ifndef RED4EXT_STATIC_LIB
#error Please define 'RED4EXT_STATIC_LIB' to compile this file.
#endif

#include <RED4ext/Scripting/ScriptReport-inl.hpp>

0 comments on commit d6e8d35

Please sign in to comment.