Пример #1
0
/**
* @brief	Attempts to executes a Lua script.
* 			If it has not been compiled already, it will compile the script
* 			and cache it in the internal script map.
*
* @param	pUser		   	The user running the script.
* @param	pNpc		   	The NPC attached to the script.
* @param	nEventID	   	Identifier for the event.
* @param	bSelectedReward	The reward selected, if applicable.
* @param	filename	   	The script's filename.
*
* @return	true if it succeeds, false if it fails.
*/
bool CLuaEngine::ExecuteScript(CUser * pUser, CNpc * pNpc, int32 nEventID, int8 bSelectedReward, const char * filename)
{
	ScriptBytecodeMap::iterator itr;
	bool result = false;

	m_lock->AcquireReadLock();
	itr = m_scriptMap.find(filename);
	if (itr == m_scriptMap.end())
	{
		// Build full path to script
		std::string szPath = LUA_SCRIPT_DIRECTORY;
		szPath += filename;

		// Release the read lock (we're not reading anymore)
		m_lock->ReleaseReadLock();

		// Attempt to compile 
		BytecodeBuffer bytecode;
		bytecode.reserve(LUA_SCRIPT_BUFFER_SIZE);
		if (!SelectAvailableScript()->CompileScript(szPath.c_str(), bytecode))
		{
			printf("ERROR: Could not compile Lua script.\n");
			printf("FILE: %s\n", szPath.c_str());
			printf("USER: %s\n", pUser->GetName().c_str());
			printf("ZONE: %d\n", pUser->GetZoneID());
			printf("NPC ID: %d\n", pNpc->m_sSid);
			printf("-\n");
			return false;
		}

		// Acquire the write lock (we're adding the compiled script)
		m_lock->AcquireWriteLock();

#if !defined(LUA_SCRIPT_CACHE_DISABLED)
		// Add the script to our map
		m_scriptMap[filename] = bytecode;
#endif

		// Now that we have the bytecode, we can use it.
		result = SelectAvailableScript()->ExecuteScript(pUser, pNpc, nEventID, bSelectedReward, 
			filename, bytecode);

		// Done using the lock.
		m_lock->ReleaseWriteLock();
	}
	else
	{
		// Already have the bytecode, so now we need to use it.
		result = SelectAvailableScript()->ExecuteScript(pUser, pNpc, nEventID, bSelectedReward, 
			filename, itr->second);

		// Done using the lock.
		m_lock->ReleaseReadLock();
	}

	return result;
}
Пример #2
0
/**
 * @brief	Attempts to executes a Lua script.
 * 			If it has not been compiled already, it will compile the script
 * 			and cache it in the internal script map.
 *
 * @param	pUser		   	The user running the script.
 * @param	pNpc		   	The NPC attached to the script.
 * @param	nEventID	   	Identifier for the event.
 * @param	bSelectedReward	The reward selected, if applicable.
 * @param	filename	   	The script's filename.
 *
 * @return	true if it succeeds, false if it fails.
 */
bool CLuaEngine::ExecuteScript(CUser * pUser, CNpc * pNpc, int32 nEventID, int8 bSelectedReward, const char * filename)
{
	ScriptBytecodeMap::iterator itr;
	bool result = false;

	m_lock->AcquireReadLock();
	itr = m_scriptMap.find(filename);
	if (itr == m_scriptMap.end())
	{
		// Build full path to script
		char szPath[_MAX_PATH];
		_snprintf(szPath, sizeof(szPath), LUA_SCRIPT_DIRECTORY "%s", filename);

		// Release the read lock (we're not reading anymore)
		m_lock->ReleaseReadLock();

		// Attempt to compile 
		BytecodeBuffer bytecode;
		bytecode.reserve(LUA_SCRIPT_BUFFER_SIZE);
		if (!SelectAvailableScript()->CompileScript(szPath, bytecode))
		{
			printf("ERROR: Could not compile Lua script `%s`.\n", szPath);
			return false;
		}

		// Acquire the write lock (we're adding the compiled script)
		m_lock->AcquireWriteLock();

#if !defined(LUA_SCRIPT_CACHE_DISABLED)
		// Add the script to our map
		m_scriptMap[filename] = bytecode;
#endif

		// Now that we have the bytecode, we can use it.
		result = SelectAvailableScript()->ExecuteScript(pUser, pNpc, nEventID, bSelectedReward, 
			filename, bytecode);

		// Done using the lock.
		m_lock->ReleaseWriteLock();
	}
	else
	{
		// Already have the bytecode, so now we need to use it.
		result = SelectAvailableScript()->ExecuteScript(pUser, pNpc, nEventID, bSelectedReward, 
			filename, itr->second);

		// Done using the lock.
		m_lock->ReleaseReadLock();
	}

	return result;
}