axmol/extensions/Particle3D/PU/CCPUScriptTranslator.cpp

774 lines
28 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (C) 2013 Henry van Merode. All rights reserved.
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
2022-02-24 18:51:36 +08:00
https://adxeproject.github.io/
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCPUScriptTranslator.h"
#include "extensions/Particle3D/PU/CCPUTranslateManager.h"
NS_CC_BEGIN
2021-12-25 10:04:45 +08:00
const std::string token[1000] = {
2019-11-23 20:27:39 +08:00
// Generic
2021-12-25 10:04:45 +08:00
"group_mask", "acceleration", "max_increment", "min_increment", "number_of_iterations", "colour_change",
"initial_colour", "random_initial_colour", "use_vertex_colours", "use_own_rotation", "max_deviation", "time_step",
"box_width", "box_height", "box_depth", "velocity", "speed", "rotation_speed", "rotation", "rotation_axis",
"normal", "step", "number_of_segments", "max_elements", "update_interval", "distance_threshold", "material",
"mesh_name", "radius", "enabled", "position", "keep_local", "less_than", "greater_than", "equals",
"visual_particle", "emitter_particle", "affector_particle", "technique_particle", "system_particle", "point",
"vertex", "increase", "alias", "use_alias", "since_start_system",
2019-11-23 20:27:39 +08:00
// Particle System
2021-12-25 10:04:45 +08:00
"system", "iteration_interval", "nonvisible_update_timeout", "fixed_timeout", "lod_distances", "main_camera_name",
"smooth_lod", "fast_forward", "scale", "scale_velocity", "scale_time", "tight_bounding_box", "category",
2019-11-23 20:27:39 +08:00
// Particle Technique
2021-12-25 10:04:45 +08:00
"technique", "visual_particle_quota", "emitted_emitter_quota", "emitted_affector_quota", "emitted_technique_quota",
"emitted_system_quota", "lod_index", "default_particle_width", "default_particle_height", "default_particle_depth",
"spatial_hashing_cell_dimension", "spatial_hashing_cell_overlap", "spatial_hashtable_size",
"spatial_hashing_update_interval", "max_velocity",
2019-11-23 20:27:39 +08:00
// Particle Renderer
2021-12-25 10:04:45 +08:00
"renderer", "render_queue_group", "sorting", "texture_coords_define", "texture_coords_rows", "texture_coords_set",
"texture_coords_columns", "use_soft_particles", "soft_particles_contrast_power", "soft_particles_scale",
2019-11-23 20:27:39 +08:00
"soft_particles_delta",
// Particle Emitter
2021-12-25 10:04:45 +08:00
"emitter", "direction", "orientation", "range_start_orientation", "range_end_orientation",
"start_orientation_range", "end_orientation_range", "duration", "repeat_delay", "emits", "angle", "emission_rate",
"time_to_live", "mass", "start_texture_coords", "end_texture_coords", "start_texture_coords_range",
"end_texture_coords_range", "texture_coords", "start_colour_range", "end_colour_range", "colour",
"all_particle_dimensions", "particle_width", "particle_height", "particle_depth", "auto_direction",
2019-11-23 20:27:39 +08:00
"force_emission",
// Particle Affector
2021-12-25 10:04:45 +08:00
"affector", "mass_affector", "exclude_emitter", "affect_specialisation", "special_default", "special_ttl_increase",
2019-11-23 20:27:39 +08:00
"special_ttl_decrease",
// Particle Observer
2021-12-25 10:04:45 +08:00
"observer", "observe_particle_type", "observe_interval", "observe_until_event",
2019-11-23 20:27:39 +08:00
// Particle Event Handler
"handler",
// Particle Behaviour
"behaviour",
// Extern
2021-12-25 10:04:45 +08:00
"extern", "attachable_distance_threshold",
2019-11-23 20:27:39 +08:00
// Dynamic Attribute
2021-12-25 10:04:45 +08:00
"control_point", "min", "max", "oscillate_frequency", "oscillate_phase", "oscillate_base", "oscillate_amplitude",
"oscillate_type", "dyn_oscillate", "dyn_random", "dyn_curved_linear", "dyn_curved_spline", "sine", "square",
2019-11-23 20:27:39 +08:00
// CameraDependency (unused)
"camera_dependency",
// ------------------------- Renderers -------------------------
// BeamRenderer
2021-12-25 10:04:45 +08:00
"beam_update_interval", "beam_max_elements", "beam_deviation", "beam_number_segments", "beam_jump_segments",
"beam_texcoord_direction", "beam_vertex_colours", "tcd_u", "tcd_v",
2019-11-23 20:27:39 +08:00
// BillboardRenderer
2021-12-25 10:04:45 +08:00
"billboard_type", "billboard_origin", "billboard_rotation_type", "common_direction", "common_up_vector",
"point_rendering", "accurate_facing", "oriented_common", "oriented_self", "oriented_shape", "perpendicular_common",
"perpendicular_self", "top_left", "top_center", "top_right", "center_left", "center_right", "center", "bottom_left",
"bottom_center", "bottom_right", "texcoord",
2019-11-23 20:27:39 +08:00
// BoxRenderer: No properties itself
// EntityRenderer
2021-12-25 10:04:45 +08:00
"entity_renderer_mesh_name", "entity_orientation_type", "ent_oriented_self", "ent_oriented_self_mirrored",
2019-11-23 20:27:39 +08:00
"ent_oriented_shape",
// LightRenderer
2021-12-25 10:04:45 +08:00
"light_renderer_light_type", "light_renderer_queue_group", "light_renderer_specular", "light_renderer_att_range",
"light_renderer_att_constant", "light_renderer_att_linear", "light_renderer_att_quadratic",
"light_renderer_spot_inner", "light_renderer_spot_outer", "light_renderer_falloff", "light_renderer_powerscale",
"spot", "flash_frequency", "flash_length", "flash_random",
2019-11-23 20:27:39 +08:00
// RibbonTrailRenderer
2021-12-25 10:04:45 +08:00
"ribbontrail_vertex_colours", "ribbontrail_max_elements", "ribbontrail_length", "ribbontrail_width",
"ribbontrail_random_initial_colour", "ribbontrail_initial_colour", "ribbontrail_colour_change",
2019-11-23 20:27:39 +08:00
// SphereRenderer: No properties itself
// ------------------------- Emitters -------------------------
// BoxEmitter
2021-12-25 10:04:45 +08:00
"box_em_width", "box_em_height", "box_em_depth",
2019-11-23 20:27:39 +08:00
// CircleEmitter
2021-12-25 10:04:45 +08:00
"circle_em_radius", "circle_em_step", "circle_em_angle", "circle_em_random", "emit_random", "circle_em_normal",
2019-11-23 20:27:39 +08:00
// LineEmitter
2021-12-25 10:04:45 +08:00
"line_em_end", "line_em_max_increment", "line_em_min_increment", "line_em_max_deviation",
2019-11-23 20:27:39 +08:00
// MeshSurfaceEmitter
2021-12-25 10:04:45 +08:00
"mesh_surface_mesh_name", "mesh_surface_distribution", "mesh_surface_scale", "edge", "heterogeneous_1",
"heterogeneous_2", "homogeneous",
2019-11-23 20:27:39 +08:00
// PointEmitter: No properties itself
// PositionEmitter
2021-12-25 10:04:45 +08:00
"add_position", "random_position",
2019-11-23 20:27:39 +08:00
// SlaveEmitter
2021-12-25 10:04:45 +08:00
"master_technique_name", "master_emitter_name",
2019-11-23 20:27:39 +08:00
// SphereSurfaceEmitter
"sphere_surface_em_radius",
// VertexEmitter
2021-12-25 10:04:45 +08:00
"vertex_em_step", "vertex_em_segments", "vertex_em_iterations", "vertex_em_mesh_name",
2019-11-23 20:27:39 +08:00
// ------------------------- Affectors -------------------------
// AlignAffector
2021-12-25 10:04:45 +08:00
"align_aff_resize", "resize",
2019-11-23 20:27:39 +08:00
// BoxCollider
2021-12-25 10:04:45 +08:00
"box_collider_width", "box_collider_height", "box_collider_depth",
2019-11-23 20:27:39 +08:00
// BaseCollider
2021-12-25 10:04:45 +08:00
"friction", "bouncyness", "intersection", "collision_friction", "collision_bouncyness", "collision_intersection",
"collision_type", "bounce", "flow", "none", "point", "box", "inner_collision",
2019-11-23 20:27:39 +08:00
// CollisionAvoidanceAffector
"avoidance_radius",
// ColourAffector
2021-12-25 10:04:45 +08:00
"time_colour", "colour_aff_time_colour", "colour_operation", "multiply", "set",
2019-11-23 20:27:39 +08:00
// FlockCenteringAffector: No properties itself
// BaseForceAffector
2021-12-25 10:04:45 +08:00
"force_vector", "force_application", "force_aff_vector", "force_aff_application", "add", "average",
2019-11-23 20:27:39 +08:00
// ForceFieldAffector
2021-12-25 10:04:45 +08:00
"forcefield_type", "realtime", "matrix", "delta", "force", "octaves", "frequency", "amplitude", "persistence",
"forcefield_size", "worldsize", "ignore_negative_x", "ignore_negative_y", "ignore_negative_z", "movement",
2019-11-23 20:27:39 +08:00
"movement_frequency",
// GeometryRotator
2021-12-25 10:04:45 +08:00
"geom_rot_use_own_rotation", "geom_rot_rotation_speed", "geom_rot_axis",
2019-11-23 20:27:39 +08:00
// GravityAffector
"gravity",
// InterParticleCollider
2021-12-25 10:04:45 +08:00
"adjustment", "collision_response", "ip_adjustment", "ip_collision_response", "average_velocity",
2019-11-23 20:27:39 +08:00
"angle_based_velocity",
// JetAffector
"jet_aff_accel",
// LineAffector
2021-12-25 10:04:45 +08:00
"end", "drift", "line_aff_max_deviation", "line_aff_time_step", "line_aff_end", "line_aff_drift",
2019-11-23 20:27:39 +08:00
// LinearForceAffector: No properties itself
// ParticleFollower
2021-12-25 10:04:45 +08:00
"follower_max_distance", "follower_min_distance", "max_distance", "min_distance",
2019-11-23 20:27:39 +08:00
// PathFollower
"path_follower_point",
// PlaneCollider
"plane_collider_normal",
// Randomiser
2021-12-25 10:04:45 +08:00
"rand_aff_max_deviation_x", "rand_aff_max_deviation_y", "rand_aff_max_deviation_z", "max_deviation_x",
"max_deviation_y", "max_deviation_z", "rand_aff_time_step", "rand_aff_direction", "use_direction",
2019-11-23 20:27:39 +08:00
// ScaleAffector
2021-12-25 10:04:45 +08:00
"xyz_scale", "x_scale", "y_scale", "z_scale",
2019-11-23 20:27:39 +08:00
// ScaleVelocityAffector
2021-12-25 10:04:45 +08:00
"velocity_scale", "stop_at_flip",
2019-11-23 20:27:39 +08:00
// SineForceAffector
2021-12-25 10:04:45 +08:00
"sinef_aff_frequency_min", "sinef_aff_frequency_max", "min_frequency", "max_frequency",
2019-11-23 20:27:39 +08:00
// SphereCollider
"sphere_collider_radius",
// TextureAnimator
2021-12-25 10:04:45 +08:00
"time_step_animation", "texture_coords_start", "texture_coords_end", "start_texture_coords_range",
"end_texture_coords_range", "texture_animation_type", "texture_start_random", "loop", "up_down", "random",
2019-11-23 20:27:39 +08:00
// TextureRotator
2021-12-25 10:04:45 +08:00
"tex_rot_use_own_rotation", "tex_rot_speed", "tex_rot_rotation",
2019-11-23 20:27:39 +08:00
// VelocityMatchingAffector
"velocity_matching_radius",
// VortexAffector
2021-12-25 10:04:45 +08:00
"vortex_aff_vector", "vortex_aff_speed",
2019-11-23 20:27:39 +08:00
// ------------------------- Observers -------------------------
// OnClearObserver: No properties itself
// OnCollisionObserver: No properties itself
// OnCountObserver
"count_threshold",
// OnEmissionObserver: No properties itself
// OnEventFlagObserver
"event_flag",
// OnExpireObserver: No properties itself
// OnPositionObserver
2021-12-25 10:04:45 +08:00
"position_x", "position_y", "position_z",
2019-11-23 20:27:39 +08:00
// OnQuotaObserver: No properties itself
// OnRandomObserver
"random_threshold",
// OnTimeObserver
"on_time",
// OnVelocityObserver
"velocity_threshold",
// ------------------------- Event Handlers -------------------------
// DoAffectorEventHandler
2021-12-25 10:04:45 +08:00
"force_affector", "pre_post",
2019-11-23 20:27:39 +08:00
// DoEnableComponentEventHandler
2021-12-25 10:04:45 +08:00
"enable_component", "emitter_component", "affector_component", "technique_component", "observer_component",
2019-11-23 20:27:39 +08:00
// DoExpireEventHandler: No properties itself
// DoFreezeEventHandler: No properties itself
// DoPlacementParticleEventHandler
2021-12-25 10:04:45 +08:00
"force_emitter", "number_of_particles", "inherit_position", "inherit_direction", "inherit_orientation",
"inherit_time_to_live", "inherit_mass", "inherit_texture_coord", "inherit_colour", "inherit_width",
"inherit_height", "inherit_depth",
2019-11-23 20:27:39 +08:00
// DoScaleEventHandler
2021-12-25 10:04:45 +08:00
"scale_fraction", "scale_type", "st_time_to_live", "st_velocity",
2019-11-23 20:27:39 +08:00
// DoStopSystemEventHandler: No properties itself
// ------------------------- Behaviours -------------------------
// SlaveBehaviour: No properties itself
// ------------------------- Externs -------------------------
// BoxColliderExtern: Defined in BoxCollider
// GravityExtern: Defined in GravityAffector
// PhysXActorExtern
2021-12-25 10:04:45 +08:00
"physx_shape", "physx_actor_group", "physx_shape_group", "physx_group_mask", "physx_angular_velocity",
"physx_angular_damping", "physx_material_index", "shape", "actor_group", "shape_group", "angular_velocity",
"angular_damping", "material_index", "Box", "Sphere", "Capsule",
2019-11-23 20:27:39 +08:00
// PhysXFluidExtern
2021-12-25 10:04:45 +08:00
"rest_particles_per_meter", "rest_density", "kernel_radius_multiplier", "motion_limit_multiplier",
"collision_distance_multiplier", "packet_size_multiplier", "stiffness", "viscosity", "surface_tension", "damping",
"external_acceleration", "restitution_for_static_shapes", "dynamic_friction_for_static_shapes",
"static_friction_for_static_shapes", "attraction_for_static_shapes", "restitution_for_dynamic_shapes",
"dynamic_friction_for_dynamic_shapes", "static_friction_for_dynamic_shapes", "attraction_for_dynamic_shapes",
"collision_response_coefficient", "collision_group", "simulation_method", "collision_method", "flags",
"visualization", "disable_gravity", "collision_twoway", "fluid_enabled", "hardware", "priority_mode",
"project_to_plane", "strict_cooking_format", "intercollision", "no_intercollision", "mix_intercollision", "static",
2019-11-23 20:27:39 +08:00
"dynamic",
// SceneDecoratorExtern
2021-12-25 10:04:45 +08:00
"scene_mesh_name", "scene_material_name", "scene_node_scale", "scene_node_position",
2019-11-23 20:27:39 +08:00
// SphereColliderExtern: Defined in SphereCollider
// VortexExtern: Defined in VortexAffector
};
2021-12-25 10:04:45 +08:00
PUScriptTranslator::PUScriptTranslator() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PUScriptTranslator::~PUScriptTranslator() {}
2019-11-23 20:27:39 +08:00
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
bool PUScriptTranslator::getFloat(const PUAbstractNode& node, float* result)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (node.type != ANT_ATOM)
2019-11-23 20:27:39 +08:00
return false;
2021-12-25 10:04:45 +08:00
PUAtomAbstractNode* atom = (PUAtomAbstractNode*)(&node);
int n = sscanf(atom->value.c_str(), "%f", result);
if (n == 0 || n == EOF)
return false; // Conversion failed
2019-11-23 20:27:39 +08:00
return true;
}
2021-12-25 10:04:45 +08:00
bool PUScriptTranslator::getInt(const PUAbstractNode& node, int* result)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (node.type != ANT_ATOM)
2019-11-23 20:27:39 +08:00
return false;
2021-12-25 10:04:45 +08:00
PUAtomAbstractNode* atom = (PUAtomAbstractNode*)&node;
int n = sscanf(atom->value.c_str(), "%d", result);
if (n == 0 || n == EOF)
return false; // Conversion failed
2019-11-23 20:27:39 +08:00
return true;
}
2021-12-25 10:04:45 +08:00
bool PUScriptTranslator::getUInt(const PUAbstractNode& node, unsigned int* result)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (node.type != ANT_ATOM)
2019-11-23 20:27:39 +08:00
return false;
2021-12-25 10:04:45 +08:00
PUAtomAbstractNode* atom = (PUAtomAbstractNode*)&node;
int n = sscanf(atom->value.c_str(), "%u", result);
if (n == 0 || n == EOF)
return false; // Conversion failed
2019-11-23 20:27:39 +08:00
return true;
}
2021-12-25 10:04:45 +08:00
bool PUScriptTranslator::getBoolean(const PUAbstractNode& node, bool* result)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (node.type != ANT_ATOM)
2019-11-23 20:27:39 +08:00
return false;
2021-12-25 10:04:45 +08:00
PUAtomAbstractNode* atom = (PUAtomAbstractNode*)&node;
// if(atom->id != 1 && atom->id != 2)
// return false;
2019-11-23 20:27:39 +08:00
*result = atom->value == "true" ? true : false;
return true;
}
2021-12-25 10:04:45 +08:00
bool PUScriptTranslator::getString(const PUAbstractNode& node, std::string* result)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (node.type != ANT_ATOM)
2019-11-23 20:27:39 +08:00
return false;
2021-12-25 10:04:45 +08:00
PUAtomAbstractNode* atom = (PUAtomAbstractNode*)&node;
*result = atom->value;
2019-11-23 20:27:39 +08:00
return true;
}
bool PUScriptTranslator::getVector2(PUAbstractNodeList::const_iterator i,
2021-12-25 10:04:45 +08:00
PUAbstractNodeList::const_iterator end,
Vec2* result,
int maxEntries)
2019-11-23 20:27:39 +08:00
{
int n = 0;
2021-12-25 10:04:45 +08:00
while (i != end && n < maxEntries)
2019-11-23 20:27:39 +08:00
{
float v = 0;
2021-12-25 10:04:45 +08:00
if (getFloat(**i, &v))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
switch (n)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
case 0:
result->x = v;
break;
case 1:
result->y = v;
break;
2019-11-23 20:27:39 +08:00
}
}
else
{
return false;
}
++n;
++i;
}
// return error if we found less than xy before end, unless constrained
return (n >= 2 || n == maxEntries);
}
//-------------------------------------------------------------------------
bool PUScriptTranslator::getVector3(PUAbstractNodeList::const_iterator i,
2021-12-25 10:04:45 +08:00
PUAbstractNodeList::const_iterator end,
Vec3* result,
int maxEntries)
2019-11-23 20:27:39 +08:00
{
int n = 0;
2021-12-25 10:04:45 +08:00
while (i != end && n < maxEntries)
2019-11-23 20:27:39 +08:00
{
float v = 0;
2021-12-25 10:04:45 +08:00
if (getFloat(**i, &v))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
switch (n)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
case 0:
result->x = v;
break;
case 1:
result->y = v;
break;
case 2:
result->z = v;
break;
2019-11-23 20:27:39 +08:00
}
}
else
{
return false;
}
++n;
++i;
}
// return error if we found less than xyz before end, unless constrained
return (n >= 3 || n == maxEntries);
}
//-------------------------------------------------------------------------
bool PUScriptTranslator::getVector4(PUAbstractNodeList::const_iterator i,
2021-12-25 10:04:45 +08:00
PUAbstractNodeList::const_iterator end,
Vec4* result,
int maxEntries)
2019-11-23 20:27:39 +08:00
{
int n = 0;
2021-12-25 10:04:45 +08:00
while (i != end && n < maxEntries)
2019-11-23 20:27:39 +08:00
{
float v = 0;
2021-12-25 10:04:45 +08:00
if (getFloat(**i, &v))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
switch (n)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
case 0:
result->x = v;
break;
case 1:
result->y = v;
break;
case 2:
result->z = v;
break;
case 3:
result->w = v;
break;
2019-11-23 20:27:39 +08:00
}
}
else
{
return false;
}
++n;
++i;
}
// return error if we found less than xyz before end, unless constrained
return (n >= 4 || n == maxEntries);
}
//-------------------------------------------------------------------------
bool PUScriptTranslator::getQuaternion(PUAbstractNodeList::const_iterator i,
2021-12-25 10:04:45 +08:00
PUAbstractNodeList::const_iterator end,
Quaternion* result,
int maxEntries)
2019-11-23 20:27:39 +08:00
{
int n = 0;
2021-12-25 10:04:45 +08:00
while (i != end && n < maxEntries)
2019-11-23 20:27:39 +08:00
{
float v = 0;
2021-12-25 10:04:45 +08:00
if (getFloat(**i, &v))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
switch (n)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
case 0:
result->w = v;
break;
case 1:
result->x = v;
break;
case 2:
result->y = v;
break;
case 3:
result->z = v;
break;
2019-11-23 20:27:39 +08:00
}
}
else
{
return false;
}
++n;
++i;
}
// return error if we found less than xyz before end, unless constrained
return (n >= 4 || n == maxEntries);
}
//-------------------------------------------------------------------------
bool PUScriptTranslator::passValidateProperty(PUScriptCompiler* compiler,
2021-12-25 10:04:45 +08:00
PUPropertyAbstractNode* prop,
std::string_view token2,
2021-12-25 10:04:45 +08:00
ValidationType validationType)
2019-11-23 20:27:39 +08:00
{
if (!passValidatePropertyNoValues(compiler, prop, token2))
{
return false;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
bool ret = true;
2021-12-25 10:04:45 +08:00
switch (validationType)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
case VAL_BOOL:
{
ret = passValidatePropertyNumberOfValues(compiler, prop, token2, 1) &&
passValidatePropertyValidBool(compiler, prop);
}
break;
case VAL_COLOURVALUE:
{
ret = passValidatePropertyNumberOfValuesRange(compiler, prop, token2, 3, 4);
}
break;
case VAL_INT:
{
ret = passValidatePropertyNumberOfValues(compiler, prop, token2, 1) &&
passValidatePropertyValidInt(compiler, prop);
}
break;
case VAL_QUATERNION:
{
ret = passValidatePropertyNumberOfValues(compiler, prop, token2, 4) &&
passValidatePropertyValidQuaternion(compiler, prop);
}
break;
case VAL_REAL:
{
ret = passValidatePropertyNumberOfValues(compiler, prop, token2, 1) &&
passValidatePropertyValidReal(compiler, prop);
}
break;
case VAL_STRING:
{
ret = passValidatePropertyNumberOfValues(compiler, prop, token2, 1);
}
break;
case VAL_UINT:
{
ret = passValidatePropertyNumberOfValues(compiler, prop, token2, 1) &&
passValidatePropertyValidUint(compiler, prop);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
break;
case VAL_VECTOR2:
{
ret = passValidatePropertyNumberOfValues(compiler, prop, token2, 2) &&
passValidatePropertyValidVector2(compiler, prop);
}
break;
case VAL_VECTOR3:
{
ret = passValidatePropertyNumberOfValues(compiler, prop, token2, 3) &&
passValidatePropertyValidVector3(compiler, prop);
}
break;
case VAL_VECTOR4:
{
ret = passValidatePropertyNumberOfValues(compiler, prop, token2, 4) &&
passValidatePropertyValidVector4(compiler, prop);
}
break;
}
2019-11-23 20:27:39 +08:00
return ret;
}
//-------------------------------------------------------------------------
bool PUScriptTranslator::passValidatePropertyNoValues(PUScriptCompiler* /*compiler*/,
2021-12-25 10:04:45 +08:00
PUPropertyAbstractNode* prop,
std::string_view /*token2*/)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (prop->values.empty())
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line, "PU Compiler: No
// values found for " + token2 + ".");
2019-11-23 20:27:39 +08:00
return false;
}
return true;
}
//-------------------------------------------------------------------------
bool PUScriptTranslator::passValidatePropertyNumberOfValues(PUScriptCompiler* /*compiler*/,
2021-12-25 10:04:45 +08:00
PUPropertyAbstractNode* prop,
std::string_view /*token2*/,
2021-12-25 10:04:45 +08:00
unsigned short numberOfValues)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (prop->values.size() > numberOfValues)
2019-11-23 20:27:39 +08:00
{
return false;
}
return true;
}
//-------------------------------------------------------------------------
bool PUScriptTranslator::passValidatePropertyNumberOfValuesRange(PUScriptCompiler* /*compiler*/,
2021-12-25 10:04:45 +08:00
PUPropertyAbstractNode* prop,
std::string_view /*token2*/,
2021-12-25 10:04:45 +08:00
unsigned short minNumberOfValues,
unsigned short maxNumberOfValues)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (prop->values.size() < minNumberOfValues || prop->values.size() > maxNumberOfValues)
2019-11-23 20:27:39 +08:00
{
return false;
}
return true;
}
//-------------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
bool PUScriptTranslator::passValidatePropertyValidReal(PUScriptCompiler* /*compiler*/, PUPropertyAbstractNode* prop)
2019-11-23 20:27:39 +08:00
{
float val = 0.0f;
2021-12-25 10:04:45 +08:00
if (getFloat(*(prop->values.front()), &val))
2019-11-23 20:27:39 +08:00
{
return true;
}
2021-12-25 10:04:45 +08:00
// compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line,
// "PU Compiler: " + prop->values.front()->getValue() + " is not a valid Real");
2019-11-23 20:27:39 +08:00
return false;
}
//-------------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
bool PUScriptTranslator::passValidatePropertyValidInt(PUScriptCompiler* /*compiler*/, PUPropertyAbstractNode* prop)
2019-11-23 20:27:39 +08:00
{
int val = 0;
2021-12-25 10:04:45 +08:00
if (getInt(*prop->values.front(), &val))
2019-11-23 20:27:39 +08:00
{
return true;
}
2021-12-25 10:04:45 +08:00
// compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line,
// "PU Compiler: " + prop->values.front()->getValue() + " is not a valid int");
2019-11-23 20:27:39 +08:00
return false;
}
//-------------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
bool PUScriptTranslator::passValidatePropertyValidUint(PUScriptCompiler* /*compiler*/, PUPropertyAbstractNode* prop)
2019-11-23 20:27:39 +08:00
{
unsigned int val = 0;
2021-12-25 10:04:45 +08:00
if (getUInt(*prop->values.front(), &val))
2019-11-23 20:27:39 +08:00
{
return true;
}
2021-12-25 10:04:45 +08:00
// compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line,
// "PU Compiler: " + prop->values.front()->getValue() + " is not a valid uint");
2019-11-23 20:27:39 +08:00
return false;
}
//-------------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
bool PUScriptTranslator::passValidatePropertyValidBool(PUScriptCompiler* /*compiler*/, PUPropertyAbstractNode* prop)
2019-11-23 20:27:39 +08:00
{
bool val;
2021-12-25 10:04:45 +08:00
if (getBoolean(*prop->values.front(), &val))
2019-11-23 20:27:39 +08:00
{
return true;
}
2021-12-25 10:04:45 +08:00
// compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line,
// "PU Compiler: " + prop->values.front()->getValue() + " is not a valid bool");
2019-11-23 20:27:39 +08:00
return false;
}
//-------------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
bool PUScriptTranslator::passValidatePropertyValidVector2(PUScriptCompiler* /*compiler*/, PUPropertyAbstractNode* prop)
2019-11-23 20:27:39 +08:00
{
Vec2 val;
2021-12-25 10:04:45 +08:00
if (getVector2(prop->values.begin(), prop->values.end(), &val))
2019-11-23 20:27:39 +08:00
{
return true;
}
2021-12-25 10:04:45 +08:00
// compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line,
// "PU Compiler: " + prop->values.front()->getValue() + " is not a valid Vector2");
2019-11-23 20:27:39 +08:00
return false;
}
//-------------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
bool PUScriptTranslator::passValidatePropertyValidVector3(PUScriptCompiler* /*compiler*/, PUPropertyAbstractNode* prop)
2019-11-23 20:27:39 +08:00
{
Vec3 val;
2021-12-25 10:04:45 +08:00
if (getVector3(prop->values.begin(), prop->values.end(), &val))
2019-11-23 20:27:39 +08:00
{
return true;
}
2021-12-25 10:04:45 +08:00
// compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line,
// "PU Compiler: " + prop->values.front()->getValue() + " is not a valid Vector3");
2019-11-23 20:27:39 +08:00
return false;
}
//-------------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
bool PUScriptTranslator::passValidatePropertyValidVector4(PUScriptCompiler* /*compiler*/, PUPropertyAbstractNode* prop)
2019-11-23 20:27:39 +08:00
{
Vec4 val;
2021-12-25 10:04:45 +08:00
if (getVector4(prop->values.begin(), prop->values.end(), &val))
2019-11-23 20:27:39 +08:00
{
return true;
}
2021-12-25 10:04:45 +08:00
// compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line,
// "PU Compiler: " + prop->values.front()->getValue() + " is not a valid Vector4");
2019-11-23 20:27:39 +08:00
return false;
}
//-------------------------------------------------------------------------
bool PUScriptTranslator::passValidatePropertyValidQuaternion(PUScriptCompiler* /*compiler*/,
2021-12-25 10:04:45 +08:00
PUPropertyAbstractNode* prop)
2019-11-23 20:27:39 +08:00
{
Quaternion val;
2021-12-25 10:04:45 +08:00
if (getQuaternion(prop->values.begin(), prop->values.end(), &val))
2019-11-23 20:27:39 +08:00
{
return true;
}
2021-12-25 10:04:45 +08:00
// compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line,
// "PU Compiler: " + prop->values.front()->getValue() + " is not a valid Quaternion");
2019-11-23 20:27:39 +08:00
return false;
}
//-------------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
void PUScriptTranslator::errorUnexpectedToken(PUScriptCompiler* /*compiler*/, PUAbstractNode* /*token2*/)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// CCLOGERROR("PU Compiler: token2 is not recognized tokenFile:%s tokenLine:%s",)
// printf()
// compiler->addError(ScriptCompiler::CE_UNEXPECTEDTOKEN, token.getPointer()->file, token.getPointer()->line,
// );
2019-11-23 20:27:39 +08:00
}
//-------------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
void PUScriptTranslator::errorUnexpectedProperty(PUScriptCompiler* /*compiler*/, PUPropertyAbstractNode* /*prop*/)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// compiler->addError(ScriptCompiler::CE_UNEXPECTEDTOKEN, prop->file, prop->line,
// "PU Compiler: token \"" + prop->name + "\" is not recognized");
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void PUScriptTranslator::processNode(PUScriptCompiler* compiler, PUAbstractNode* node)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (node->type != ANT_OBJECT)
2019-11-23 20:27:39 +08:00
return;
// Abstract objects are completely skipped
2021-12-25 10:04:45 +08:00
if ((reinterpret_cast<PUObjectAbstractNode*>(node))->abstract)
2019-11-23 20:27:39 +08:00
return;
// Retrieve the translator to use
2021-12-25 10:04:45 +08:00
PUScriptTranslator* translator = PUTranslateManager::Instance()->getTranslator(node);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if (translator)
2019-11-23 20:27:39 +08:00
translator->translate(compiler, node);
}
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
NS_CC_END