Added runtime type size check to ComponentWrapper to avoid corruption when types don't match
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include "Entity.h"
|
#include "Entity.h"
|
||||||
#include "ComponentInfo.h"
|
#include "ComponentInfo.h"
|
||||||
#include "Util/Any.h"
|
#include "Util/Any.h"
|
||||||
Minecraft hard drilling
|
|
||||||
struct ComponentWrapper
|
struct ComponentWrapper
|
||||||
{
|
{
|
||||||
ComponentWrapper(const ComponentInfo& componentInfo, char* data)
|
ComponentWrapper(const ComponentInfo& componentInfo, char* data)
|
||||||
@@ -26,8 +26,13 @@ struct ComponentWrapper
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
T& Field(std::string name)
|
T& Field(std::string name)
|
||||||
{
|
{
|
||||||
unsigned int offset = Info.Fields.at(name).Offset;
|
const ComponentInfo::Field_t& field = Info.Fields.at(name);
|
||||||
return *reinterpret_cast<T*>(&Data[offset]);
|
if (sizeof(T) > field.Stride) {
|
||||||
|
std::stringstream message;
|
||||||
|
message << "Type size of \"" << typeid(T).name() << "\" doesn't match size of component field \"" << Info.Name << "." << name << "\"!";
|
||||||
|
throw new std::runtime_error(message.str().c_str());
|
||||||
|
}
|
||||||
|
return *reinterpret_cast<T*>(&Data[field.Offset]);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
|
|||||||
Reference in New Issue
Block a user