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 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; }
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; }
IMPLEMENT_RMI(CMonoActor, ClScriptRMI) { IMonoClass *pActorClass = g_pScriptSystem->GetCryBraryAssembly()->GetClass("Entity"); IMonoArray *pNetworkArgs = CreateMonoArray(3); pNetworkArgs->Insert(ToMonoString(params.methodName.c_str())); pNetworkArgs->InsertMonoObject(params.args); pNetworkArgs->Insert(params.targetId); pActorClass->InvokeArray(nullptr, "OnRemoteInvocation", pNetworkArgs); pNetworkArgs->Release(); return true; }
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; }
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; }
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 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; } }