14#include "third_party/nlohmann/json.hpp"
25 return static_cast<uint8_t>(value < 0 ? 0 : (value > 255 ? 255 : value));
41 default:
return "Unknown";
53 param.boolValue = value;
61 param.intValue = value;
69 param.floatValue = value;
77 param.stringValue = value;
101 param.colorValue = { r, g, b, a };
109 param.entityRefValue = entityId;
140 [](
unsigned char c) { return std::tolower(c); });
181 return static_cast<float>(
intValue);
211 std::ostringstream
oss;
220 std::ostringstream
oss;
251 s.erase(std::remove(
s.begin(),
s.end(),
'('),
s.end());
252 s.erase(std::remove(
s.begin(),
s.end(),
')'),
s.end());
254 std::istringstream
iss(
s);
262 token.erase(0,
token.find_first_not_of(
" \t\n\r"));
263 token.erase(
token.find_last_not_of(
" \t\n\r") + 1);
279 return Vector(0.0f, 0.0f, 0.0f);
293 if (!
s.empty() &&
s[0] ==
'#' && (
s.length() == 7 ||
s.length() == 9))
296 unsigned long val = std::stoul(
s.substr(1),
nullptr, 16);
297 if (
s.length() == 7) {
315 SYSTEM_LOG <<
"Failed to parse color from hex string: " <<
s << std::endl;
319 else if (
s.find(
"rgb") == 0)
321 std::string
nums =
s.substr(
s.find(
'(') + 1);
326 int values[4] = { 0, 0, 0, 255 };
332 token.erase(0,
token.find_first_not_of(
" \t\n\r"));
333 token.erase(
token.find_last_not_of(
" \t\n\r") + 1);
397 const std::string& componentType,
421 <<
"', got " <<
jsonValue.type_name() << std::endl;
432 <<
"', got " <<
jsonValue.type_name() << std::endl;
443 <<
"', got " <<
jsonValue.type_name() << std::endl;
458 float x =
static_cast<float>(
jsonValue[
"x"].get<
double>());
459 float y =
static_cast<float>(
jsonValue[
"y"].get<
double>());
468 <<
"', got " <<
jsonValue.type_name() << std::endl;
475 float x =
static_cast<float>(
jsonValue[
"x"].get<
double>());
476 float y =
static_cast<float>(
jsonValue[
"y"].get<
double>());
477 float z =
static_cast<float>(
jsonValue[
"z"].get<
double>());
487 <<
"', got " <<
jsonValue.type_name() << std::endl;
516 <<
"', got " <<
jsonValue.type_name() << std::endl;
530 SYSTEM_LOG <<
" [WARN] Failed to parse EntityRef from string for '" <<
paramName <<
"'" << std::endl;
538 <<
"', got " <<
jsonValue.type_name() << std::endl;
549 <<
"', got " <<
jsonValue.type_name() << std::endl;
559 #ifdef DEBUG_PARAMETER_PARSING
562 <<
" (from schema)" << std::endl;
570 #ifdef DEBUG_PARAMETER_PARSING
571 SYSTEM_LOG <<
" [WARN] No schema for '" << componentType <<
"." <<
paramName <<
"', using heuristics" << std::endl;
585 #ifdef DEBUG_PARAMETER_PARSING
602 else if (
paramName.find(
"entity") != std::string::npos ||
603 paramName.find(
"Entity") != std::string::npos ||
604 paramName.find(
"ref") != std::string::npos ||
605 paramName.find(
"Ref") != std::string::npos)
625 float x =
static_cast<float>(
jsonValue[
"x"].get<
double>());
626 float y =
static_cast<float>(
jsonValue[
"y"].get<
double>());
627 float z =
static_cast<float>(
jsonValue[
"z"].get<
double>());
632 float x =
static_cast<float>(
jsonValue[
"x"].get<
double>());
633 float y =
static_cast<float>(
jsonValue[
"y"].get<
double>());
661 if (
paramName.find(
"waypoint") != std::string::npos ||
662 paramName.find(
"path") != std::string::npos ||
663 paramName.find(
"points") != std::string::npos ||
664 paramName.find(
"Path") != std::string::npos)
745 SYSTEM_LOG <<
"Warning: Component definition missing 'type' field" << std::endl;
757 const std::string&
key =
it.key();
763 const auto& value =
it.value();
771 catch (
const std::exception&
e)
773 SYSTEM_LOG <<
"Error parsing component JSON: " <<
e.what() << std::endl;
792 return &(
it->second);
std::string ParameterTypeToString(ComponentParameter::Type type)
static uint8_t ClampColorValue(int value)
ComponentParameter ParseParameterWithSchema(const std::string &componentType, const std::string ¶mName, const nlohmann::json &jsonValue)
ComponentParameter ParseParameterWithSchema(const std::string &componentType, const std::string ¶mName, const nlohmann::json &jsonValue)
ComponentTypeID GetComponentTypeID_Static()
const EntityID INVALID_ENTITY_ID
static ParameterSchemaRegistry & GetInstance()
const ComponentSchema * GetComponentSchema(const std::string &componentType) const
std::string componentType
const ComponentParameter * GetParameter(const std::string &name) const
std::map< std::string, ComponentParameter > parameters
bool HasParameter(const std::string &name) const
static ComponentDefinition FromJSON(const nlohmann::json &jsonObj)
std::shared_ptr< nlohmann::json > arrayValue
EntityID AsEntityRef() const
static ComponentParameter FromBool(bool value)
static ComponentParameter FromFloat(float value)
static ComponentParameter FromVector2(float x, float y)
static ComponentParameter FromEntityRef(EntityID entityId)
static ComponentParameter FromVector3(float x, float y, float z)
static ComponentParameter FromColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
const nlohmann::json & AsArray() const
std::string AsString() const
static ComponentParameter FromInt(int value)
static ComponentParameter FromArray(const nlohmann::json &arrayData)
SDL_Color AsColor() const
static ComponentParameter FromString(const std::string &value)
std::map< std::string, ParameterSchemaEntry > parameters