CResource* CResourceManager::Load( const std::string& name ) { filePath absPath; if ( !resFileRoot->GetFullPathFromRoot( ( name + '/' ).c_str(), false, absPath ) || absPath.empty() ) return NULL; CResource *res = Create( absPath, name ); if ( !res ) return NULL; // Load all .lua scripts res->m_fileRoot.ScanDirectory( "/", "*.lua", false, NULL, loadscript, &res->GetVM() ); return res; }
int CPlayerNatives::Create(int * VM) { // Just an example how the new scripting works with a lua or a squirrel vm its not needed to create a native for every language CResource* pResource = CServer::GetInstance()->GetResourceManager()->Get(VM); if(pResource) { // We do not have to split it here cause our CScriptVM class provides the nessessary push and pop/get methods CScriptVM* pVM = pResource->GetVM(); CVector3 keks; pVM->PopVector(keks); // Create player kekse pVM->PushBool(true); pVM->ResetStackIndex(); } return 0; }
int CLuaFunctionDefs::Call ( lua_State* luaVM ) { CResource * pResource = NULL; SString strFunctionName = ""; CScriptArgReader argStream ( luaVM ); argStream.ReadUserData ( pResource ); argStream.ReadString ( strFunctionName ); if ( !argStream.HasErrors ( ) ) { // Grab our VM CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { // Grab this resource CResource* pThisResource = pLuaMain->GetResource (); if ( pThisResource ) { if ( pResource ) { //Get the target Lua VM lua_State* targetLuaVM = pResource->GetVM()->GetVM(); // Read out the vargs CLuaArguments args; args.ReadArguments ( luaVM, 3 ); CLuaArguments returns; LUA_CHECKSTACK ( targetLuaVM, 1 ); // Ensure some room //Lets grab the original hidden variables so we can restore them later lua_getglobal ( targetLuaVM, "sourceResource" ); CLuaArgument OldResource ( luaVM, -1 ); lua_pop( targetLuaVM, 1 ); lua_getglobal ( targetLuaVM, "sourceResourceRoot" ); CLuaArgument OldResourceRoot ( luaVM, -1 ); lua_pop( targetLuaVM, 1 ); //Set the new values for the current sourceResource, and sourceResourceRoot lua_pushresource ( targetLuaVM, pThisResource ); lua_setglobal ( targetLuaVM, "sourceResource" ); lua_pushelement ( targetLuaVM, pThisResource->GetResourceEntity() ); lua_setglobal ( targetLuaVM, "sourceResourceRoot" ); // Call the exported function with the given name and the args if ( pResource->CallExportedFunction ( strFunctionName, args, returns, *pThisResource ) ) { // Push return arguments returns.PushArguments ( luaVM ); //Restore the old variables OldResource.Push ( targetLuaVM ); lua_setglobal ( targetLuaVM, "sourceResource" ); OldResourceRoot.Push ( targetLuaVM ); lua_setglobal ( targetLuaVM, "sourceResourceRoot" ); return returns.Count (); } else { //Restore the old variables OldResource.Push ( targetLuaVM ); lua_setglobal ( targetLuaVM, "sourceResource" ); OldResourceRoot.Push ( targetLuaVM ); lua_setglobal ( targetLuaVM, "sourceResourceRoot" ); m_pScriptDebugging->LogError ( luaVM, "call: failed to call '%s:%s'", pResource->GetName (), *strFunctionName ); } } else { m_pScriptDebugging->LogBadType ( luaVM ); } } } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() ); // Failed lua_pushboolean ( luaVM, false ); return 1; }
int CScriptClasses::CreateEntity(int * VM) { CResource* pResource = CResourceManager::GetInstance()->Get(VM); if(pResource) { CScriptVM* pVM = pResource->GetVM(); CString strEntity = "HJHKJ"; /*pVM->Pop(strEntity); pVM->ResetStackIndex();*/ if(strEntity == "3DLABEL") { C3DLabelEntity* p3DLabel = new C3DLabelEntity(); pVM->SetClassInstance("C3DLabelEntity", (void*)p3DLabel); CEntityNatives::Register(pVM); C3DLabelNatives::Register(pVM); } else if(strEntity == "ACTOR") { CActorEntity* pActor = new CActorEntity(); pVM->SetClassInstance("CActorEntity", (void*)pActor); CEntityNatives::Register(pVM); CActorNatives::Register(pVM); } else if(strEntity == "BLIP") { CBlipEntity * pBlip = new CBlipEntity(); pVM->SetClassInstance("CBlipEntity", (void*)pBlip); CEntityNatives::Register(pVM); CBlipNatives::Register(pVM); } else if(strEntity == "CHECKPOINT") { CCheckpointEntity* pCheckpoint = new CCheckpointEntity(); pVM->SetClassInstance("CCheckpointEntity", (void*)pCheckpoint); CEntityNatives::Register(pVM); CCheckpointNatives::Register(pVM); } else if(strEntity == "FIRE") { CFireEntity* pFire = new CFireEntity(); pVM->SetClassInstance("CFireEntity", (void*)pFire); CEntityNatives::Register(pVM); CFireNatives::Register(pVM); } else if(strEntity == "OBJECT") { CObjectEntity* pObject = new CObjectEntity(); pVM->SetClassInstance("CObjectEntity", (void*)pObject); CEntityNatives::Register(pVM); CObjectNatives::Register(pVM); } else if(strEntity == "PICKUP") { CPickupEntity* pPickup = new CPickupEntity(); pVM->SetClassInstance("CPickupEntity", (void*)pPickup); CEntityNatives::Register(pVM); CPickupNatives::Register(pVM); } else if(strEntity == "PLAYER") { CPlayerEntity* pPlayer = new CPlayerEntity(); pVM->SetClassInstance("CPlayerEntity", (void*)pPlayer); CEntityNatives::Register(pVM); CPlayerNatives::Register(pVM); } else /*if(strEntity == "VEHICLE")*/ { CVehicleEntity* pVehicle = new CVehicleEntity(); /* now read the additional params for vehicle spawning */ // Memo: add params count check in VM Pop methods // Merge the pop events so we use only Pop instead of PopVector //CVector3 vecPos; //CVector3 vecRot; //pVM->Pop(vecPos); //pVM->Pop(vecRot); //// Do we need the id; maybe internal for easier sync but definetly not public to the scripting engine pVehicle->SetId(CServer::GetInstance()->GetVehicleManager()->Add(pVehicle)); // pVM->SetClassInstance("CVehicleEntity", (void*)pVehicle); //CEntityNatives::Register(pVM); //CVehicleNatives::Register(pVM); //pVehicle->Spawn(); } } return 1; }
int CLuaFunctionDefs::Call ( lua_State* luaVM ) { // Grab our VM CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { // Grab this resource CResource* pThisResource = pLuaMain->GetResource (); if ( pThisResource ) { // Typechecking if ( lua_istype ( luaVM, 1, LUA_TLIGHTUSERDATA ) && lua_istype ( luaVM, 2, LUA_TSTRING ) ) { // Grab the resource CResource* pResource = lua_toresource ( luaVM, 1 ); if ( pResource ) { //Get the target Lua VM lua_State* targetLuaVM = pResource->GetVM()->GetVM(); // The function name const char* szFunctionName = lua_tostring ( luaVM, 2 ); // Read out the vargs CLuaArguments args; args.ReadArguments ( luaVM, 3 ); CLuaArguments returns; //Lets grab the original hidden variables so we can restore them later lua_getglobal ( targetLuaVM, "sourceResource" ); CLuaArgument OldResource ( luaVM, -1 ); lua_getglobal ( targetLuaVM, "sourceResourceRoot" ); CLuaArgument OldResourceRoot ( luaVM, -1 ); //Set the new values for the current sourceResource, and sourceResourceRoot lua_pushresource ( targetLuaVM, pThisResource ); lua_setglobal ( targetLuaVM, "sourceResource" ); lua_pushelement ( targetLuaVM, pThisResource->GetResourceEntity() ); lua_setglobal ( targetLuaVM, "sourceResourceRoot" ); // Call the exported function with the given name and the args if ( pResource->CallExportedFunction ( szFunctionName, args, returns, *pThisResource ) ) { // Push return arguments returns.PushArguments ( luaVM ); //Restore the old variables OldResource.Push ( targetLuaVM ); lua_setglobal ( targetLuaVM, "sourceResource" ); OldResourceRoot.Push ( targetLuaVM ); lua_setglobal ( targetLuaVM, "sourceResourceRoot" ); return returns.Count (); } else { //Restore the old variables OldResource.Push ( targetLuaVM ); lua_setglobal ( targetLuaVM, "sourceResource" ); OldResourceRoot.Push ( targetLuaVM ); lua_setglobal ( targetLuaVM, "sourceResourceRoot" ); m_pScriptDebugging->LogError ( luaVM, "call: failed to call '%s:%s'", pResource->GetName (), szFunctionName ); } } else { m_pScriptDebugging->LogBadPointer ( luaVM, "call", "resource", 1 ); } } else { m_pScriptDebugging->LogBadType ( luaVM, "call" ); } } } // Failed lua_pushboolean ( luaVM, false ); return 1; }