-
-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:yamashi/CyberEngineTweaks
- Loading branch information
Showing
8 changed files
with
391 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
#include <stdafx.h> | ||
|
||
#include "Enum.h" | ||
|
||
Enum::Enum(const RED4ext::CStackType& stackType) | ||
{ | ||
Get(stackType); | ||
} | ||
|
||
Enum::Enum(const std::string& typeName, const std::string& value) | ||
{ | ||
auto* pType = static_cast<RED4ext::CEnum*>(RED4ext::CRTTISystem::Get()->GetEnum(RED4ext::FNV1a(typeName.c_str()))); | ||
if (pType) | ||
{ | ||
m_type = pType; | ||
SetValueByName(value); | ||
} | ||
} | ||
|
||
Enum::Enum(const std::string& typeName, uint32_t value) | ||
{ | ||
auto* pType = static_cast<RED4ext::CEnum*>(RED4ext::CRTTISystem::Get()->GetEnum(RED4ext::FNV1a(typeName.c_str()))); | ||
if (pType) | ||
{ | ||
m_type = pType; | ||
SetValueSafe(static_cast<uint32_t>(value)); | ||
} | ||
} | ||
|
||
|
||
Enum::Enum(const RED4ext::CEnum* pType, const std::string& value) | ||
: m_type(pType) | ||
{ | ||
SetValueByName(value); | ||
} | ||
|
||
|
||
Enum::Enum(const RED4ext::CEnum* pType, uint32_t value) | ||
: m_type(pType) | ||
{ | ||
SetValueSafe(static_cast<uint32_t>(value)); | ||
} | ||
|
||
void Enum::SetValueSafe(uint64_t value) | ||
{ | ||
for (auto i = 0; i < m_type->valueList.size; ++i) | ||
{ | ||
if (m_type->valueList[i] == value) | ||
{ | ||
m_value = value; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
void Enum::Get(const RED4ext::CStackType& stackType) | ||
{ | ||
m_type = static_cast<RED4ext::CEnum*>(stackType.type); | ||
switch (stackType.type->GetSize()) | ||
{ | ||
case sizeof(uint8_t) : | ||
m_value = *static_cast<uint8_t*>(stackType.value); | ||
break; | ||
case sizeof(uint16_t): | ||
m_value = *static_cast<uint16_t*>(stackType.value); | ||
break; | ||
case sizeof(uint32_t): | ||
m_value = *static_cast<uint32_t*>(stackType.value); | ||
break; | ||
case sizeof(uint64_t): | ||
m_value = *static_cast<uint64_t*>(stackType.value); | ||
break; | ||
} | ||
} | ||
|
||
void Enum::Set(RED4ext::CStackType& stackType, TiltedPhoques::Allocator* apAllocator) | ||
{ | ||
stackType.type = const_cast<RED4ext::CEnum*>(m_type); // Sad cast | ||
switch (m_type->GetSize()) | ||
{ | ||
case sizeof(uint8_t): | ||
stackType.value = apAllocator->New<uint8_t>(static_cast<uint8_t>(m_value)); | ||
break; | ||
case sizeof(uint16_t): | ||
stackType.value = apAllocator->New<uint16_t>(static_cast<uint16_t>(m_value)); | ||
break; | ||
case sizeof(uint32_t): | ||
stackType.value = apAllocator->New<uint32_t>(static_cast<uint32_t>(m_value)); | ||
break; | ||
case sizeof(uint64_t): | ||
stackType.value = apAllocator->New<uint64_t>(static_cast<uint64_t>(m_value)); | ||
break; | ||
} | ||
} | ||
|
||
|
||
std::string Enum::GetValueName() const | ||
{ | ||
for (auto i = 0; i < m_type->valueList.size; ++i) | ||
{ | ||
if (m_type->valueList[i] == m_value) | ||
{ | ||
return RED4ext::CName(m_type->hashList[i]).ToString(); | ||
} | ||
} | ||
|
||
return ""; | ||
} | ||
|
||
|
||
void Enum::SetValueByName(const std::string& value) | ||
{ | ||
for (auto i = 0; i < m_type->hashList.size; ++i) | ||
{ | ||
if (m_type->hashList[i] == RED4ext::FNV1a(value.c_str())) | ||
{ | ||
m_value = m_type->valueList[i]; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
std::string Enum::ToString() const | ||
{ | ||
if (m_type) | ||
{ | ||
RED4ext::CName name; | ||
m_type->GetName(name); | ||
return name.ToString() + std::string(" : ") + GetValueName() + std::string(" (") + std::to_string(m_value) + std::string(")"); | ||
} | ||
|
||
return "Invalid enum"; | ||
} | ||
|
||
const RED4ext::CEnum* Enum::GetType() const | ||
{ | ||
return m_type; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#pragma once | ||
|
||
#include "LuaRED.h" | ||
|
||
struct Enum | ||
{ | ||
Enum(const RED4ext::CEnum*, const std::string& value); | ||
Enum(const RED4ext::CEnum*, uint32_t value); | ||
Enum(const RED4ext::CStackType& stackType); | ||
Enum(const std::string& typeName, const std::string& value); | ||
Enum(const std::string& typeName, uint32_t value); | ||
|
||
void Get(const RED4ext::CStackType& stackType); | ||
void Set(RED4ext::CStackType& stackType, TiltedPhoques::Allocator* apAllocator); | ||
|
||
// Returns the enum value by name | ||
std::string GetValueName() const; | ||
|
||
// Sets value by name in the enum list | ||
void SetValueByName(const std::string& value); | ||
|
||
// Sets by value verified against enum list | ||
void SetValueSafe(uint64_t value); | ||
|
||
std::string ToString() const; | ||
|
||
const RED4ext::CEnum* GetType() const; | ||
|
||
protected: | ||
const RED4ext::CEnum* m_type{ nullptr }; | ||
uint64_t m_value{ 0 }; | ||
}; | ||
|
||
template<> | ||
struct LuaRED<Enum, "Enum"> | ||
{ | ||
sol::object ToLua(RED4ext::CStackType& aResult, sol::state_view aLua) | ||
{ | ||
return make_object(aLua, Enum(aResult)); | ||
} | ||
|
||
RED4ext::CStackType ToRED(sol::object aObject, RED4ext::IRTTIType* apRtti, TiltedPhoques::Allocator* apAllocator) | ||
{ | ||
RED4ext::CStackType result; | ||
if (aObject.is<Enum>()) | ||
{ | ||
auto* pEnum = aObject.as<Enum*>(); | ||
if (pEnum->GetType() == apRtti) | ||
{ | ||
pEnum->Set(result, apAllocator); | ||
} | ||
else // The enum type we were passed isn't the same | ||
{ | ||
result.type = apRtti; | ||
result.value = nullptr; | ||
} | ||
} | ||
else if (aObject.get_type() == sol::type::number) // Enum from number cast | ||
{ | ||
auto* enumType = static_cast<RED4ext::CEnum*>(apRtti); | ||
if (aObject != sol::nil) | ||
{ | ||
Enum en(enumType, aObject.as<uint32_t>()); | ||
en.Set(result, apAllocator); | ||
} | ||
} | ||
else if (aObject.get_type() == sol::type::string) // Enum from string cast | ||
{ | ||
auto* enumType = static_cast<RED4ext::CEnum*>(apRtti); | ||
if (aObject != sol::nil) | ||
{ | ||
sol::state_view v(aObject.lua_state()); | ||
std::string str = v["tostring"](aObject); | ||
Enum en(enumType, str); | ||
en.Set(result, apAllocator); | ||
} | ||
} | ||
else | ||
{ | ||
// Probably not going to like this but ok | ||
result.type = apRtti; | ||
result.value = nullptr; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
size_t Size() const noexcept | ||
{ | ||
return m_pRtti ? m_pRtti->GetSize() : 0; | ||
} | ||
|
||
bool Is(RED4ext::IRTTIType* apRtti) const | ||
{ | ||
if (apRtti->GetType() == RED4ext::ERTTIType::Enum) | ||
{ | ||
m_pRtti = apRtti; | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private: | ||
mutable RED4ext::IRTTIType* m_pRtti{ nullptr }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.