/* * Registers all the functions & commands for a server with excel. */ void RegisterServer(LPXLOPER xDLL, int index) { char* section = g_serverSections[index]; bool includeGeneric = INI::GetBoolean(g_ini, section, FS_INCLUDE_GENERIC, true); char* function = INI::GetString(g_ini, section, FS_FUNCTION_NAME, "FS"); char* category = INI::GetString(g_ini, section, FS_FUNCTION_CATEGORY, "General"); bool includeVolatile = INI::GetBoolean(g_ini, section, FS_INCLUDE_VOLATILE, true); char* volatileFunction = INI::GetString(g_ini, section, FS_FUNCTION_NAME_VOLATILE, "FSV"); // Register execute function (if requested) if(includeGeneric) { char fsExecuteFn[MAX_PATH]; sprintf(fsExecuteFn, "FSExecute%d", index); XLUtil::RegisterFunction(xDLL, fsExecuteFn, "RCPPPPPPPPPPPPPPPPPPPP", function, NULL, "1", category, NULL, NULL, NULL, NULL); } // Register execute function - volatile version (if requested) if(includeVolatile) { char fsExecuteVolFn[MAX_PATH]; sprintf(fsExecuteVolFn, "FSExecuteVolatile%d", index); XLUtil::RegisterFunction(xDLL, fsExecuteVolFn, "RCPPPPPPPPPPPPPPPPPPPP!", volatileFunction, NULL, "1", category, NULL, NULL, NULL, NULL); } // Now ask server for a list of functions to register bool disableFunctionList = INI::GetBoolean(g_ini, section, FS_DISABLE_FUNCTION_LIST, false); if(!disableFunctionList) { RegisterFunctions(xDLL, index); } // Sheet Generator - currently disabled //SheetGenerator::Register(xDLL, g_protocol[index], g_ini); }
static int LuaMain( lua_State* L ) { // Get the command line arguments Args* args = (Args*)lua_touserdata( L, lua_upvalueindex( 1 ) ); // Open the standard libraries and register ddlt functions luaL_openlibs( L ); RegisterFunctions( L ); // Push the main function onto the stack if ( luaL_loadbuffer( L, (const char*)ddlc, _ddlc_size, "ddlc.lua" ) != 0 ) { return lua_error( L ); } lua_call( L, 0, 1 ); // Push the arguments array lua_newtable( L ); for ( int i = 1; i < args->argc; i++ ) { lua_pushstring( L, args->argv[ i ] ); lua_rawseti( L, -2, i ); } // Push the DDL compiler DDLT::Compiler::PushNew( L, args->argv[ 0 ], false, 32 ); // Call main lua_call( L, 2, 0 ); return 0; }
void CBuzzController::SetBytecode(const std::string& str_fname) { /* Reset the BuzzVM */ if(m_tBuzzVM) buzzvm_destroy(&m_tBuzzVM); m_tBuzzVM = buzzvm_new(m_unRobotId); /* Save the bytecode filename */ m_strBytecodeFName = str_fname; /* Load the bytecode */ std::ifstream cBCodeFile(str_fname.c_str(), std::ios::binary | std::ios::ate); if(cBCodeFile.fail()) { THROW_ARGOSEXCEPTION("Can't open file \"" << str_fname << "\": " << strerror(errno)); } std::ifstream::pos_type unFileSize = cBCodeFile.tellg(); m_cBytecode.Clear(); m_cBytecode.Resize(unFileSize); cBCodeFile.seekg(0, std::ios::beg); cBCodeFile.read(reinterpret_cast<char*>(m_cBytecode.ToCArray()), unFileSize); /* Load the script */ buzzvm_set_bcode(m_tBuzzVM, m_cBytecode.ToCArray(), m_cBytecode.Size()); if(buzzvm_set_bcode(m_tBuzzVM, m_cBytecode.ToCArray(), m_cBytecode.Size()) != BUZZVM_STATE_READY) { THROW_ARGOSEXCEPTION("Error loading Buzz script \"" << str_fname << "\": " << buzzvm_strerror(m_tBuzzVM)); } /* Register basic function */ if(RegisterFunctions() != BUZZVM_STATE_READY) { THROW_ARGOSEXCEPTION("Error while registering functions"); } /* Execute the global part of the script */ buzzvm_execute_script(m_tBuzzVM); /* Call the Init() function */ buzzvm_function_call(m_tBuzzVM, "init", 0); }
void CLuaManager::InitVM() { lua_VM = luaL_newstate(); luaL_openlibs(lua_VM); DisableFunctions(lua_VM); RegisterPreScript(lua_VM); RegisterFunctions(lua_VM); RegisterMacros(lua_VM); RegisterModuleFunctions(lua_VM); }
void CSraMgr::x_Init(void) { #if defined(SRA_SDK_NEEDS_REGISTER_FUNCTIONS) if ( !s_SraRegistered ) { // try to initialize and retry with registering in case of error try { x_DoInit(); return; // initialized successfully } catch ( CSraException& ) { // retry again after registration RegisterFunctions(); } } #endif x_DoInit(); }
bool Lua::Init() { if( luaInitialized ) { LogMsg(WARN, "Cannot initialize Lua. It is already initialized." ); return( false ); } L = lua_open(); if( !L ) { LogMsg(WARN, "Could not initialize Lua VM." ); return( false ); } luaL_openlibs( L ); RegisterFunctions(); luaInitialized = true; return( true ); }
Eluna::Eluna(): L(luaL_newstate()), m_EventMgr(new EventMgr(*this)), ServerEventBindings(new EventBind<HookMgr::ServerEvents>("ServerEvents", *this)), PlayerEventBindings(new EventBind<HookMgr::PlayerEvents>("PlayerEvents", *this)), GuildEventBindings(new EventBind<HookMgr::GuildEvents>("GuildEvents", *this)), GroupEventBindings(new EventBind<HookMgr::GroupEvents>("GroupEvents", *this)), VehicleEventBindings(new EventBind<HookMgr::VehicleEvents>("VehicleEvents", *this)), PacketEventBindings(new EntryBind<HookMgr::PacketEvents>("PacketEvents", *this)), CreatureEventBindings(new EntryBind<HookMgr::CreatureEvents>("CreatureEvents", *this)), CreatureGossipBindings(new EntryBind<HookMgr::GossipEvents>("GossipEvents (creature)", *this)), GameObjectEventBindings(new EntryBind<HookMgr::GameObjectEvents>("GameObjectEvents", *this)), GameObjectGossipBindings(new EntryBind<HookMgr::GossipEvents>("GossipEvents (gameobject)", *this)), ItemEventBindings(new EntryBind<HookMgr::ItemEvents>("ItemEvents", *this)), ItemGossipBindings(new EntryBind<HookMgr::GossipEvents>("GossipEvents (item)", *this)), playerGossipBindings(new EntryBind<HookMgr::GossipEvents>("GossipEvents (player)", *this)) { // open base lua luaL_openlibs(L); RegisterFunctions(L); // Create hidden table with weak values lua_newtable(L); lua_newtable(L); lua_pushstring(L, "v"); lua_setfield(L, -2, "__mode"); lua_setmetatable(L, -2); userdata_table = luaL_ref(L, LUA_REGISTRYINDEX); // Replace this with map insert if making multithread version ASSERT(!Eluna::GEluna); Eluna::GEluna = this; // run scripts RunScripts(); }
void Eluna::OpenLua() { enabled = eConfigMgr->GetBoolDefault("Eluna.Enabled", true); if (!IsEnabled()) { ELUNA_LOG_INFO("[Eluna]: Eluna is disabled in config"); return; } L = luaL_newstate(); CreateBindStores(); // open base lua libraries luaL_openlibs(L); // open additional lua libraries // Register methods and functions RegisterFunctions(this); // Create hidden table with weak values lua_newtable(L); lua_newtable(L); lua_pushstring(L, "v"); lua_setfield(L, -2, "__mode"); lua_setmetatable(L, -2); lua_setfield(L, LUA_REGISTRYINDEX, ELUNA_OBJECT_STORE); // Set lua require folder paths (scripts folder structure) lua_getglobal(L, "package"); lua_pushstring(L, lua_requirepath.c_str()); lua_setfield(L, -2, "path"); lua_pushstring(L, ""); // erase cpath lua_setfield(L, -2, "cpath"); lua_pop(L, 1); }
void Register(Manager::ScriptState& state) { RegisterFunctions(state, PhasorExportTable, export_table_size); }
// Start or restart eluna. Returns true if started bool StartEluna() { #ifndef ELUNA #ifndef MANGOS { ELUNA_LOG_ERROR("[Eluna]: LuaEngine is Disabled. (If you want to use it please enable in cmake)"); return false; } #endif #endif ELUNA_GUARD(); bool restart = false; if (sEluna->L) { restart = true; sHookMgr->OnEngineRestart(); ELUNA_LOG_INFO("[Eluna]: Stopping Lua Engine"); // Unregisters and stops all timed events sEluna->m_EventMgr.RemoveEvents(); // Remove bindings sEluna->PacketEventBindings.Clear(); sEluna->ServerEventBindings.Clear(); sEluna->PlayerEventBindings.Clear(); sEluna->GuildEventBindings.Clear(); sEluna->GroupEventBindings.Clear(); sEluna->CreatureEventBindings.Clear(); sEluna->CreatureGossipBindings.Clear(); sEluna->GameObjectEventBindings.Clear(); sEluna->GameObjectGossipBindings.Clear(); sEluna->ItemEventBindings.Clear(); sEluna->ItemGossipBindings.Clear(); sEluna->playerGossipBindings.Clear(); sEluna->VehicleEventBindings.Clear(); lua_close(sEluna->L); } else AddElunaScripts(); #ifdef MANGOS // Check config file for eluna is enabled or disabled if (!sWorld->getConfig(CONFIG_BOOL_ELUNA_ENABLED)) { ELUNA_LOG_ERROR("[Eluna]: LuaEngine is Disabled. (If you want to use it please set config in 'mangosd.conf')"); return false; } #endif ELUNA_LOG_INFO("[Eluna]: Starting Lua Engine"); sEluna->L = luaL_newstate(); luaL_openlibs(sEluna->L); RegisterFunctions(sEluna->L); // Create hidden table with weak values lua_newtable(sEluna->L); lua_newtable(sEluna->L); lua_pushstring(sEluna->L, "v"); lua_setfield(sEluna->L, -2, "__mode"); lua_setmetatable(sEluna->L, -2); sHookMgr->userdata_table = luaL_ref(sEluna->L, LUA_REGISTRYINDEX); ScriptPaths scripts; std::string folderpath = sConfigMgr->GetStringDefault("Eluna.ScriptPath", "lua_scripts"); #if PLATFORM == PLATFORM_UNIX || PLATFORM == PLATFORM_APPLE if (folderpath[0] == '~') if (const char* home = getenv("HOME")) folderpath.replace(0, 1, home); #endif ELUNA_LOG_INFO("[Eluna]: Searching scripts from `%s`", folderpath.c_str()); sEluna->GetScripts(folderpath, scripts); sEluna->GetScripts(folderpath + "/extensions", scripts); sEluna->RunScripts(scripts); /* if (restart) { //! Iterate over every supported source type (creature and gameobject) //! Not entirely sure how this will affect units in non-loaded grids. { HashMapHolder<Creature>::ReadGuard g(HashMapHolder<Creature>::GetLock()); HashMapHolder<Creature>::MapType& m = HashMapHolder<Creature>::GetContainer(); for (HashMapHolder<Creature>::MapType::const_iterator itr = m.begin(); itr != m.end(); ++itr) { if (itr->second->IsInWorld()) // must check? // if(sEluna->CreatureEventBindings->GetBindMap(iter->second->GetEntry())) // update all AI or just Eluna? itr->second->AIM_Initialize(); } } { HashMapHolder<GameObject>::ReadGuard g(HashMapHolder<GameObject>::GetLock()); HashMapHolder<GameObject>::MapType& m = HashMapHolder<GameObject>::GetContainer(); for (HashMapHolder<GameObject>::MapType::const_iterator itr = m.begin(); itr != m.end(); ++itr) { if (itr->second->IsInWorld()) // must check? // if(sEluna->GameObjectEventBindings->GetBindMap(iter->second->GetEntry())) // update all AI or just Eluna? itr->second->AIM_Initialize(); } } } */ return true; }
// Start or restart eluna. Returns true if started bool StartEluna() { bool restart = false; if (sEluna.L) { restart = true; sHookMgr.OnEngineRestart(); sLog.outString("[Eluna]: Restarting Lua Engine"); // Unregisters and stops all timed events sEluna.m_EventMgr.RemoveEvents(); // Remove bindings sEluna.PacketEventBindings.Clear(); sEluna.ServerEventBindings.Clear(); sEluna.PlayerEventBindings.Clear(); sEluna.GuildEventBindings.Clear(); sEluna.GroupEventBindings.Clear(); sEluna.CreatureEventBindings.Clear(); sEluna.CreatureGossipBindings.Clear(); sEluna.GameObjectEventBindings.Clear(); sEluna.GameObjectGossipBindings.Clear(); sEluna.ItemEventBindings.Clear(); sEluna.ItemGossipBindings.Clear(); sEluna.playerGossipBindings.Clear(); lua_close(sEluna.L); } // Check config file for eluna is enabled or disabled if (!sWorld.getConfig(CONFIG_BOOL_ELUNA_ENABLED)) { sLog.outError("[Eluna]: LuaEngine is Disabled. (If you want to use it please set config in 'mangosd.conf')"); return false; } sEluna.L = luaL_newstate(); sLog.outString(); sLog.outString("[Eluna]: Lua Engine loaded."); sLog.outString(); LoadedScripts loadedScripts; sEluna.LoadDirectory("lua_scripts", &loadedScripts); luaL_openlibs(sEluna.L); // Register functions here RegisterFunctions(sEluna.L); uint32 count = 0; char filename[200]; for (std::set<std::string>::const_iterator itr = loadedScripts.begin(); itr != loadedScripts.end(); ++itr) { strcpy(filename, itr->c_str()); if (luaL_loadfile(sEluna.L, filename) != 0) { sLog.outErrorEluna("[Eluna]: Error loading file `%s`.", itr->c_str()); sEluna.report(sEluna.L); } else { int err = lua_pcall(sEluna.L, 0, 0, 0); if (err != 0 && err == LUA_ERRRUN) { sLog.outErrorEluna("[Eluna]: Error loading file `%s`.", itr->c_str()); sEluna.report(sEluna.L); } } ++count; } /* if (restart) { //! Iterate over every supported source type (creature and gameobject) //! Not entirely sure how this will affect units in non-loaded grids. { HashMapHolder<Creature>::ReadGuard g(HashMapHolder<Creature>::GetLock()); HashMapHolder<Creature>::MapType& m = HashMapHolder<Creature>::GetContainer(); for (HashMapHolder<Creature>::MapType::const_iterator itr = m.begin(); itr != m.end(); ++itr) { if (itr->second->IsInWorld()) // must check? // if(sEluna.CreatureEventBindings->GetBindMap(iter->second->GetEntry())) // update all AI or just Eluna? itr->second->AIM_Initialize(); } } { HashMapHolder<GameObject>::ReadGuard g(HashMapHolder<GameObject>::GetLock()); HashMapHolder<GameObject>::MapType& m = HashMapHolder<GameObject>::GetContainer(); for (HashMapHolder<GameObject>::MapType::const_iterator itr = m.begin(); itr != m.end(); ++itr) { if (itr->second->IsInWorld()) // must check? // if(sEluna.GameObjectEventBindings->GetBindMap(iter->second->GetEntry())) // update all AI or just Eluna? itr->second->AIM_Initialize(); } } } */ sLog.outString("[Eluna]: Loaded %u Lua scripts..", count); sLog.outString(); return true; }