mono::object CScriptbind_Entity::GetEntitiesByClass(mono::string _class) { IEntityClass *pDesiredClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass(ToCryString(_class)); std::vector<EntityId> classEntities; IEntityItPtr pIt = gEnv->pEntitySystem->GetEntityIterator(); pIt->MoveFirst(); while(!pIt->IsEnd()) { if(IEntity *pEntity = pIt->Next()) { if(pEntity->GetClass() == pDesiredClass) classEntities.push_back(pEntity->GetId()); } } if(classEntities.size()<1) return nullptr; IMonoClass *pEntityIdClass = g_pScriptSystem->GetCryBraryAssembly()->GetClass("EntityId"); IMonoArray *pArray = CreateMonoArray(classEntities.size()); for(auto it = classEntities.begin(); it != classEntities.end(); ++it) pArray->Insert(pEntityIdClass->BoxObject(&mono::entityId(*it))); return pArray->GetManagedObject(); }
mono::object CScriptbind_Entity::QueryAreas(EntityId id, Vec3 vPos, int maxResults, bool forceCalculation) { SAreaManagerResult *pResults = new SAreaManagerResult[maxResults]; int numResults = 0; gEnv->pEntitySystem->GetAreaManager()->QueryAreas(id, vPos, pResults, maxResults, numResults); IMonoArray *pArray = CreateDynamicMonoArray(); IMonoClass *pClass = GetMonoScriptSystem()->GetCryBraryAssembly()->GetClass("AreaQueryResult"); for(int i = 0; i < numResults; i++) { auto result = pResults[i]; if(result.pArea != nullptr) pArray->InsertMonoObject(pClass->BoxObject(&result)); } delete[] pResults; mono::object managedArray = pArray->GetManagedObject(); pArray->Release(false); return managedArray; }
bool CMonoFlowNode::CreatedNode(TFlowNodeId id, const char *name, TFlowNodeTypeId typeId, IFlowNodePtr pNode) { if(pNode==this) { const char *typeName = gEnv->pFlowSystem->GetTypeName(typeId); IMonoObject *pScript = g_pScriptSystem->InstantiateScript(gEnv->pFlowSystem->GetTypeName(typeId), eScriptFlag_FlowNode); IMonoClass *pNodeInfo = g_pScriptSystem->GetCryBraryAssembly()->GetClass("NodeInitializationParams", "CryEngine.Flowgraph.Native"); IMonoArray *pArgs = CreateMonoArray(1); pArgs->InsertMonoObject(pNodeInfo->BoxObject(&SMonoNodeInfo(this, id, m_pActInfo->pGraph->GetGraphId()))); mono::object result = g_pScriptSystem->InitializeScriptInstance(pScript, pArgs); pArgs->Release(); m_pScript = pScript; if(result) { IMonoObject *pResult = *result; bool bResult = pResult->Unbox<bool>(); SAFE_RELEASE(pResult); return bResult; } CryLogAlways("Failed to create node %s", gEnv->pFlowSystem->GetTypeName(typeId)); return false; } return true; }
mono::object CScriptDomain::BoxAnyValue(MonoAnyValue &any) { switch(any.type) { case eMonoAnyType_Boolean: return (mono::object)mono_value_box(m_pDomain, mono_get_boolean_class(), &any.b); case eMonoAnyType_Integer: return (mono::object)mono_value_box(m_pDomain, mono_get_int32_class(), &any.i); case eMonoAnyType_UnsignedInteger: return (mono::object)mono_value_box(m_pDomain, mono_get_uint32_class(), &any.u); case eMonoAnyType_Short: return (mono::object)mono_value_box(m_pDomain, mono_get_int16_class(), &any.i); case eMonoAnyType_UnsignedShort: return (mono::object)mono_value_box(m_pDomain, mono_get_uint16_class(), &any.u); case eMonoAnyType_Float: return (mono::object)mono_value_box(m_pDomain, mono_get_single_class(), &any.f); case eMonoAnyType_String: return (mono::object)CreateMonoString(any.str); case eMonoAnyType_EntityId: { IMonoClass *pEntityIdClass = g_pScriptSystem->GetCryBraryAssembly()->GetClass("EntityId"); return pEntityIdClass->BoxObject(&mono::entityId(any.u), this); } case eMonoAnyType_Vec3: { IMonoClass *pVec3Class = g_pScriptSystem->GetCryBraryAssembly()->GetClass("Vec3"); Vec3 vec3(any.vec4.x, any.vec4.y, any.vec4.z); return pVec3Class->BoxObject(&vec3, this); } break; case eMonoAnyType_Quat: { IMonoClass *pQuatClass = g_pScriptSystem->GetCryBraryAssembly()->GetClass("Quat"); return pQuatClass->BoxObject(&any.vec4, this); } break; case eMonoAnyType_Array: case eMonoAnyType_Unknown: return any.monoObject; } return nullptr; }
mono::object CScriptDomain::BoxAnyValue(MonoAnyValue &any) { switch(any.type) { case eMonoAnyType_Boolean: return (mono::object)mono_value_box(m_pDomain, mono_get_boolean_class(), &any.b); case eMonoAnyType_Integer: return (mono::object)mono_value_box(m_pDomain, mono_get_int32_class(), &any.i); case eMonoAnyType_UnsignedInteger: return (mono::object)mono_value_box(m_pDomain, mono_get_uint32_class(), &any.u); case eMonoAnyType_EntityId: { IMonoClass *pEntityIdClass = g_pScriptSystem->GetCryBraryAssembly()->GetClass("EntityId"); return pEntityIdClass->BoxObject(&mono::entityId(any.u), this)->GetManagedObject(); } case eMonoAnyType_Short: return (mono::object)mono_value_box(m_pDomain, mono_get_int16_class(), &any.i); case eMonoAnyType_UnsignedShort: return (mono::object)mono_value_box(m_pDomain, mono_get_uint16_class(), &any.u); case eMonoAnyType_Float: return (mono::object)mono_value_box(m_pDomain, mono_get_single_class(), &any.f); case eMonoAnyType_String: MonoWarning("IMonoConverter::BoxAnyValue does not support strings, utilize ToMonoString instead"); case eMonoAnyType_Vec3: { IMonoClass *pVec3Class = g_pScriptSystem->GetCryBraryAssembly()->GetClass("Vec3"); Vec3 vec3(any.vec4.x, any.vec4.y, any.vec4.z); return pVec3Class->BoxObject(&vec3, this)->GetManagedObject(); } break; case eMonoAnyType_Quat: { IMonoClass *pQuatClass = g_pScriptSystem->GetCryBraryAssembly()->GetClass("Quat"); return pQuatClass->BoxObject(&any.vec4, this)->GetManagedObject(); } break; } return nullptr; }
int CScriptbind_Physics::RayWorldIntersection(Vec3 origin, Vec3 dir, int objFlags, unsigned int flags, int maxHits, mono::object skipEntities, mono::object &hits) { IPhysicalEntity **pSkipEnts = NULL; int numSkipEnts = 0; if(skipEntities) { IMonoArray *pSkipEntities = *skipEntities; numSkipEnts = pSkipEntities->GetSize(); pSkipEnts = new IPhysicalEntity*[numSkipEnts]; for(int i = 0; i < numSkipEnts; i++) { IMonoObject *pItem = *pSkipEntities->GetItem(i); #ifndef RELEASE if(!pItem) g_pScriptSystem->GetCryBraryAssembly()->GetException("CryEngine", "NullPointerException")->Throw(); #endif pSkipEnts[i] = pItem->Unbox<IPhysicalEntity *>(); SAFE_RELEASE(pItem); } pSkipEntities->Release(); } ray_hit *pHits = new ray_hit[maxHits]; int numHits = gEnv->pPhysicalWorld->RayWorldIntersection(origin, dir, objFlags, flags, pHits, maxHits, pSkipEnts, numSkipEnts); SAFE_DELETE_ARRAY(pSkipEnts); if(numHits > 0) { IMonoClass *pRayHitClass = g_pScriptSystem->GetCryBraryAssembly()->GetClass("RaycastHit"); IMonoArray *pRayHits = CreateMonoArray(numHits);//, pRayHitClass); for(int i = 0; i < numHits; i++) pRayHits->InsertMonoObject(pRayHitClass->BoxObject(&pHits[i])); hits = pRayHits->GetManagedObject(); pRayHits->Release(); } delete[] pHits; return numHits; }
bool CFlowNode::CreatedNode(TFlowNodeId id, const char *name, TFlowNodeTypeId typeId, IFlowNodePtr pNode) { if(pNode==this) { IMonoObject *pScript = g_pScriptSystem->InstantiateScript(gEnv->pFlowSystem->GetTypeName(typeId), eScriptFlag_FlowNode); IMonoClass *pNodeInfo = g_pScriptSystem->GetCryBraryAssembly()->GetClass("NodeInfo", "CryEngine.FlowSystem.Native"); pScript->CallMethod("InternalInitialize", pNodeInfo->BoxObject(&SMonoNodeInfo(this, id, m_pActInfo->pGraph->GetGraphId()))); m_pScript = pScript; return pScript != nullptr; } return true; }
mono::object CScriptbind_Entity::GetEntitiesInBox(AABB bbox, int objTypes) { IPhysicalEntity **pEnts = nullptr; IMonoClass *pEntityIdClass = GetMonoScriptSystem()->GetCryBraryAssembly()->GetClass("EntityId"); int numEnts = gEnv->pPhysicalWorld->GetEntitiesInBox(bbox.min, bbox.max, pEnts, objTypes); IMonoArray *pEntities = CreateDynamicMonoArray(); for(int i = 0; i < numEnts; i++) pEntities->InsertMonoObject(pEntityIdClass->BoxObject(&mono::entityId(gEnv->pPhysicalWorld->GetPhysicalEntityId(pEnts[i])))); auto result = pEntities->GetManagedObject(); pEntities->Release(); return result; }
mono::object CScriptbind_Entity::GetEntitiesInBox(AABB bbox, int objTypes) { IPhysicalEntity **pEnts = nullptr; IMonoClass *pEntityIdClass = g_pScriptSystem->GetCryBraryAssembly()->GetClass("EntityId"); int numEnts = gEnv->pPhysicalWorld->GetEntitiesInBox(bbox.min, bbox.max, pEnts, objTypes); if(numEnts > 0) { IMonoArray *pEntities = CreateMonoArray(numEnts); for(int i = 0; i < numEnts; i++) pEntities->Insert(pEntityIdClass->BoxObject(&mono::entityId(gEnv->pPhysicalWorld->GetPhysicalEntityId(pEnts[i])))); return pEntities->GetManagedObject(); } return nullptr; }
bool CMonoEntityExtension::Init(IGameObject *pGameObject) { SetGameObject(pGameObject); pGameObject->EnablePhysicsEvent( true, eEPE_OnPostStepImmediate ); if (!GetGameObject()->BindToNetwork()) return false; IEntity *pEntity = GetEntity(); IEntityClass *pEntityClass = pEntity->GetClass(); m_pScript = g_pScriptSystem->InstantiateScript(pEntityClass->GetName(), eScriptFlag_Entity); IMonoClass *pEntityInfoClass = g_pScriptSystem->GetCryBraryAssembly()->GetClass("EntityInitializationParams", "CryEngine.Native"); SMonoEntityInfo entityInfo(pEntity); IMonoArray *pArgs = CreateMonoArray(1); pArgs->InsertMonoObject(pEntityInfoClass->BoxObject(&entityInfo)); g_pScriptSystem->InitializeScriptInstance(m_pScript, pArgs); pArgs->Release(); int numProperties; auto pProperties = static_cast<CEntityPropertyHandler *>(pEntityClass->GetPropertyHandler())->GetQueuedProperties(pEntity->GetId(), numProperties); if(pProperties) { for(int i = 0; i < numProperties; i++) { auto queuedProperty = pProperties[i]; SetPropertyValue(queuedProperty.propertyInfo, queuedProperty.value.c_str()); } } m_bInitialized = true; return true; }
void CActorSystem::OnSpawn(IEntity *pEntity,SEntitySpawnParams ¶ms) { EMonoActorType actorType = GetMonoActorType(pEntity->GetClass()->GetName()); if(actorType != EMonoActorType_None) { if(IActor *pActor = gEnv->pGameFramework->GetIActorSystem()->GetActor(pEntity->GetId())) { IMonoObject *pScript = g_pScriptSystem->InstantiateScript(pEntity->GetClass()->GetName(), eScriptFlag_Actor); IMonoClass *pActorInfoClass = g_pScriptSystem->GetCryBraryAssembly()->GetClass("ActorInfo"); SMonoActorInfo actorInfo(pActor); pScript->CallMethod("InternalSpawn", pActorInfoClass->BoxObject(&actorInfo)); if(actorType == EMonoActorType_Managed) static_cast<CActor *>(pActor)->SetScript(pScript); } } }
ICryScriptInstance *CScriptSystem::InstantiateScript(const char *scriptName, EMonoScriptFlags scriptFlags, IMonoArray *pConstructorParameters, bool throwOnFail) { FUNCTION_PROFILER_FAST(GetISystem(), PROFILE_SCRIPT, gEnv->bProfilerEnabled); auto *pInstance = new CCryScriptInstance(scriptFlags); IMonoArray *pScriptCreationArgs = CreateMonoArray(5); pScriptCreationArgs->Insert(scriptName); pScriptCreationArgs->Insert(scriptFlags); pScriptCreationArgs->InsertNativePointer(pInstance); pScriptCreationArgs->InsertMonoObject((pConstructorParameters != nullptr ? pConstructorParameters->GetManagedObject() : nullptr)); pScriptCreationArgs->Insert(throwOnFail); auto result = m_pScriptManager->GetClass()->GetMethod("CreateScriptInstance", 5)->InvokeArray(m_pScriptManager->GetManagedObject(), pScriptCreationArgs); SAFE_RELEASE(pScriptCreationArgs); if(!result) return nullptr; pInstance->SetManagedObject((MonoObject *)result, true); if(scriptFlags & eScriptFlag_GameRules) { IMonoClass *pGameRulesInitParamsClass = m_pCryBraryAssembly->GetClass("GameRulesInitializationParams"); IMonoArray *pArgs = CreateMonoArray(1); SGameRulesInitializationParams params; pArgs->InsertMonoObject(pGameRulesInitParamsClass->BoxObject(¶ms)); InitializeScriptInstance(pInstance, pArgs); SAFE_RELEASE(pArgs); } for each(auto listener in m_listeners) listener->OnScriptInstanceCreated(scriptName, scriptFlags, pInstance); return pInstance; }
void CScriptbind_ActorSystem::OnSpawn(IEntity *pEntity,SEntitySpawnParams ¶ms) { EMonoActorType actorType = GetMonoActorType(pEntity->GetClass()->GetName()); if(actorType != EMonoActorType_None) { if(IActor *pActor = static_cast<CScriptSystem *>(GetMonoScriptSystem())->GetIGameFramework()->GetIActorSystem()->GetActor(pEntity->GetId())) { ICryScriptInstance *pScript = GetMonoScriptSystem()->InstantiateScript(pEntity->GetClass()->GetName(), eScriptFlag_Actor); IMonoClass *pActorInfoClass = GetMonoScriptSystem()->GetCryBraryAssembly()->GetClass("ActorInitializationParams", "CryEngine.Native"); SMonoActorInfo actorInfo(pActor); IMonoArray *pArgs = CreateMonoArray(1); pArgs->InsertMonoObject(pActorInfoClass->BoxObject(&actorInfo)); static_cast<CScriptSystem *>(GetMonoScriptSystem())->InitializeScriptInstance(pScript, pArgs); SAFE_RELEASE(pArgs); } } }
mono::object CScriptbind_Entity::QueryProximity(AABB box, mono::string className, uint32 nEntityFlags) { SEntityProximityQuery query; if(className != nullptr) query.pEntityClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass(ToCryString(className)); query.box = box; query.nEntityFlags = nEntityFlags; gEnv->pEntitySystem->QueryProximity(query); IMonoClass *pEntityIdClass = GetMonoScriptSystem()->GetCryBraryAssembly()->GetClass("EntityId"); IMonoArray *pEntities = CreateDynamicMonoArray(); for(int i = 0; i < query.nCount; i++) pEntities->InsertMonoObject(pEntityIdClass->BoxObject(&mono::entityId(query.pEntities[i]->GetId()))); auto result = pEntities->GetManagedObject(); pEntities->Release(); return result; }
mono::object CScriptbind_Entity::GetEntitiesByClasses(mono::object classes) { IMonoArray *pClassArray = *classes; int numClasses = pClassArray->GetSize(); IEntityClass **pClasses = new IEntityClass *[numClasses]; for(int i = 0; i < numClasses; i++) pClasses[i] = gEnv->pEntitySystem->GetClassRegistry()->FindClass(ToCryString((mono::string)pClassArray->GetManagedObject())); IEntityItPtr pIt = gEnv->pEntitySystem->GetEntityIterator(); IMonoClass *pEntityIdClass = GetMonoScriptSystem()->GetCryBraryAssembly()->GetClass("EntityId"); IMonoArray *pEntities = CreateDynamicMonoArray(); pIt->MoveFirst(); while(!pIt->IsEnd()) { if(IEntity *pEntity = pIt->Next()) { IEntityClass *pEntityClass = pEntity->GetClass(); for(int i = 0; i < numClasses; i++) { if(pEntityClass == pClasses[i]) { pEntities->InsertMonoObject(pEntityIdClass->BoxObject(&mono::entityId(pEntity->GetId()))); break; } } } } auto result = pEntities->GetManagedObject(); pEntities->Release(); return result; }
mono::object CScriptbind_Entity::GetEntitiesByClass(mono::string _class) { IEntityClass *pDesiredClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass(ToCryString(_class)); IEntityItPtr pIt = gEnv->pEntitySystem->GetEntityIterator(); IMonoClass *pEntityIdClass = GetMonoScriptSystem()->GetCryBraryAssembly()->GetClass("EntityId"); IMonoArray *pEntities = CreateDynamicMonoArray(); pIt->MoveFirst(); while(!pIt->IsEnd()) { if(IEntity *pEntity = pIt->Next()) { if(pEntity->GetClass() == pDesiredClass) pEntities->InsertMonoObject(pEntityIdClass->BoxObject(&mono::entityId(pEntity->GetId()))); } } auto result = pEntities->GetManagedObject(); pEntities->Release(); return result; }
void CEntityEventHandler::HandleEntityEvent(EEntityType type, SEntityEvent &event, IEntity *pEntity, mono::object managedObject) { switch(event.event) { case ENTITY_EVENT_RESET: { bool enterGamemode = event.nParam[0]==1; if(!enterGamemode && pEntity->GetFlags() & ENTITY_FLAG_NO_SAVE) { gEnv->pEntitySystem->RemoveEntity(pEntity->GetId()); return; } IMonoArray *pParams = CreateMonoArray(1); pParams->Insert(enterGamemode); m_pClass[type]->GetMethod("OnEditorReset", 1)->InvokeArray(managedObject, pParams); SAFE_RELEASE(pParams); } break; case ENTITY_EVENT_COLLISION: { EventPhysCollision *pCollision = (EventPhysCollision *)event.nParam[0]; SMonoColliderInfo source = SMonoColliderInfo(pCollision, 0); SMonoColliderInfo target = SMonoColliderInfo(pCollision, 1); IMonoClass *pColliderInfoClass = g_pScriptSystem->GetCryBraryAssembly()->GetClass("ColliderInfo"); IMonoArray *pArgs = CreateMonoArray(6); pArgs->InsertMonoObject(pColliderInfoClass->BoxObject(&source)); pArgs->InsertMonoObject(pColliderInfoClass->BoxObject(&target)); pArgs->Insert(pCollision->pt); pArgs->Insert(pCollision->n); pArgs->Insert(pCollision->penetration); pArgs->Insert(pCollision->radius); m_pClass[type]->GetMethod("OnCollision", 6)->InvokeArray(managedObject, pArgs); SAFE_RELEASE(pArgs); } break; case ENTITY_EVENT_START_GAME: m_pClass[type]->GetMethod("OnStartGame")->Invoke(managedObject); break; case ENTITY_EVENT_START_LEVEL: m_pClass[type]->GetMethod("OnStartLevel")->Invoke(managedObject); break; case ENTITY_EVENT_LEVEL_LOADED: m_pClass[type]->GetMethod("OnLevelLoaded")->Invoke(managedObject); break; case ENTITY_EVENT_ENTERAREA: m_pClass[type]->GetMethod("OnEnterArea", 3)->Call(managedObject, (EntityId)event.nParam[0], (int)event.nParam[1], (EntityId)event.nParam[2]); break; case ENTITY_EVENT_MOVEINSIDEAREA: m_pClass[type]->GetMethod("OnMoveInsideArea", 3)->Call(managedObject, (EntityId)event.nParam[0], (int)event.nParam[1], (EntityId)event.nParam[2]); break; case ENTITY_EVENT_LEAVEAREA: m_pClass[type]->GetMethod("OnLeaveArea", 3)->Call(managedObject, (EntityId)event.nParam[0], (int)event.nParam[1], (EntityId)event.nParam[2]); break; case ENTITY_EVENT_ENTERNEARAREA: m_pClass[type]->GetMethod("OnEnterNearArea", 3)->Call(managedObject, (EntityId)event.nParam[0], (int)event.nParam[1], (EntityId)event.nParam[2]); break; case ENTITY_EVENT_MOVENEARAREA: m_pClass[type]->GetMethod("OnMoveNearArea", 4)->Call(managedObject, (EntityId)event.nParam[0], (int)event.nParam[1], (EntityId)event.nParam[2], event.fParam[0]); break; case ENTITY_EVENT_LEAVENEARAREA: m_pClass[type]->GetMethod("OnLeaveNearArea", 3)->Call(managedObject, (EntityId)event.nParam[0], (int)event.nParam[1], (EntityId)event.nParam[2]); break; case ENTITY_EVENT_XFORM: m_pClass[type]->GetMethod("OnMove", 1)->Call(managedObject, (EEntityXFormFlags)event.nParam[0]); break; case ENTITY_EVENT_ATTACH: m_pClass[type]->GetMethod("OnAttach", 1)->Call(managedObject, (EntityId)event.nParam[0]); break; case ENTITY_EVENT_DETACH: m_pClass[type]->GetMethod("OnDetach", 1)->Call(managedObject, (EntityId)event.nParam[0]); break; case ENTITY_EVENT_DETACH_THIS: m_pClass[type]->GetMethod("OnDetachThis", 1)->Call(managedObject, (EntityId)event.nParam[0]); break; case ENTITY_EVENT_ANIM_EVENT: { const AnimEventInstance* pAnimEvent = reinterpret_cast<const AnimEventInstance*>(event.nParam[0]); ICharacterInstance* pCharacter = reinterpret_cast<ICharacterInstance*>(event.nParam[1]); IMonoClass *pAnimationEventClass = g_pScriptSystem->GetCryBraryAssembly()->GetClass("AnimationEvent"); SMonoAnimationEvent animEvent(pAnimEvent); IMonoArray *pArgs = CreateMonoArray(1); pArgs->InsertMonoObject(pAnimationEventClass->BoxObject(&animEvent)); m_pClass[type]->GetMethod("OnAnimEvent", 1)->InvokeArray(managedObject, pArgs); SAFE_RELEASE(pArgs); } break; case ENTITY_EVENT_PREPHYSICSUPDATE: { m_pClass[type]->GetMethod("OnPrePhysicsUpdate")->Invoke(managedObject); } break; } }