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

feat: add requirements for script validation #102

Merged
merged 3 commits into from
Dec 14, 2023
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions include/RED4ext/Scripting/ScriptReport-inl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#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 bool RED4ext::ScriptReport::HasErrors() const noexcept
{
return errors && errors->size > 0;
}

RED4EXT_INLINE 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>