* Moves NPC Creation across AppDomains to prevent a major perfomance issue.

arthursv
Adam Frisby 2009-08-21 15:12:50 +10:00
parent 98da8e9b16
commit f7c5eca978
2 changed files with 70 additions and 18 deletions

View File

@ -26,12 +26,14 @@
*/ */
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading;
using OpenMetaverse; using OpenMetaverse;
using Nini.Config; using Nini.Config;
using OpenSim.Region.Framework.Interfaces; using OpenSim.Region.Framework.Interfaces;
using OpenSim.Region.Framework.Scenes; using OpenSim.Region.Framework.Scenes;
using OpenSim.Region.CoreModules.Avatar.NPC; using OpenSim.Region.CoreModules.Avatar.NPC;
using OpenSim.Framework; using OpenSim.Framework;
using Timer=System.Timers.Timer;
namespace OpenSim.Region.OptionalModules.World.NPC namespace OpenSim.Region.OptionalModules.World.NPC
{ {
@ -39,10 +41,25 @@ namespace OpenSim.Region.OptionalModules.World.NPC
{ {
// private const bool m_enabled = false; // private const bool m_enabled = false;
private Mutex m_createMutex = new Mutex(false);
private Timer m_timer = new Timer(500);
private Dictionary<UUID,NPCAvatar> m_avatars = new Dictionary<UUID, NPCAvatar>(); private Dictionary<UUID,NPCAvatar> m_avatars = new Dictionary<UUID, NPCAvatar>();
private Dictionary<UUID,AvatarAppearance> m_appearanceCache = new Dictionary<UUID, AvatarAppearance>(); private Dictionary<UUID,AvatarAppearance> m_appearanceCache = new Dictionary<UUID, AvatarAppearance>();
// Timer vars.
private bool p_inUse = false;
private readonly object p_lock = new object();
// Private Temporary Variables.
private string p_firstname;
private string p_lastname;
private Vector3 p_position;
private Scene p_scene;
private UUID p_cloneAppearanceFrom;
private UUID p_returnUuid;
private AvatarAppearance GetAppearance(UUID target, Scene scene) private AvatarAppearance GetAppearance(UUID target, Scene scene)
{ {
if (m_appearanceCache.ContainsKey(target)) if (m_appearanceCache.ContainsKey(target))
@ -57,29 +74,23 @@ namespace OpenSim.Region.OptionalModules.World.NPC
public UUID CreateNPC(string firstname, string lastname,Vector3 position, Scene scene, UUID cloneAppearanceFrom) public UUID CreateNPC(string firstname, string lastname,Vector3 position, Scene scene, UUID cloneAppearanceFrom)
{ {
NPCAvatar npcAvatar = new NPCAvatar(firstname, lastname, position, scene); // Block.
npcAvatar.CircuitCode = (uint) Util.RandomClass.Next(0, int.MaxValue); m_createMutex.WaitOne();
scene.ClientManager.Add(npcAvatar.CircuitCode, npcAvatar); // Copy Temp Variables for Timer to pick up.
scene.AddNewClient(npcAvatar); lock (p_lock)
ScenePresence sp;
if(scene.TryGetAvatar(npcAvatar.AgentId, out sp))
{ {
AvatarAppearance x = GetAppearance(cloneAppearanceFrom, scene); p_firstname = firstname;
p_lastname = lastname;
List<byte> wearbyte = new List<byte>(); p_position = position;
for (int i = 0; i < x.VisualParams.Length; i++) p_scene = scene;
{ p_cloneAppearanceFrom = cloneAppearanceFrom;
wearbyte.Add(x.VisualParams[i]); p_inUse = true;
}
sp.SetAppearance(x.Texture.GetBytes(), wearbyte);
} }
m_avatars.Add(npcAvatar.AgentId, npcAvatar); m_createMutex.ReleaseMutex();
return npcAvatar.AgentId; return p_returnUuid;
} }
public void Autopilot(UUID agentID, Scene scene, Vector3 pos) public void Autopilot(UUID agentID, Scene scene, Vector3 pos)
@ -116,6 +127,45 @@ namespace OpenSim.Region.OptionalModules.World.NPC
public void Initialise(Scene scene, IConfigSource source) public void Initialise(Scene scene, IConfigSource source)
{ {
scene.RegisterModuleInterface<INPCModule>(this); scene.RegisterModuleInterface<INPCModule>(this);
m_timer.Elapsed += m_timer_Elapsed;
m_timer.Start();
}
void m_timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
lock (p_lock)
{
if (p_inUse)
{
p_inUse = false;
NPCAvatar npcAvatar = new NPCAvatar(p_firstname, p_lastname, p_position, p_scene);
npcAvatar.CircuitCode = (uint) Util.RandomClass.Next(0, int.MaxValue);
p_scene.ClientManager.Add(npcAvatar.CircuitCode, npcAvatar);
p_scene.AddNewClient(npcAvatar);
ScenePresence sp;
if (p_scene.TryGetAvatar(npcAvatar.AgentId, out sp))
{
AvatarAppearance x = GetAppearance(p_cloneAppearanceFrom, p_scene);
List<byte> wearbyte = new List<byte>();
for (int i = 0; i < x.VisualParams.Length; i++)
{
wearbyte.Add(x.VisualParams[i]);
}
sp.SetAppearance(x.Texture.GetBytes(), wearbyte);
}
m_avatars.Add(npcAvatar.AgentId, npcAvatar);
p_returnUuid = npcAvatar.AgentId;
}
}
} }
public void PostInitialise() public void PostInitialise()

View File

@ -31,6 +31,7 @@ using System.Collections.Generic;
using System.Runtime.Remoting.Lifetime; using System.Runtime.Remoting.Lifetime;
using System.Text; using System.Text;
using System.Net; using System.Net;
using System.Threading;
using OpenMetaverse; using OpenMetaverse;
using Nini.Config; using Nini.Config;
using OpenSim; using OpenSim;
@ -1766,6 +1767,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
public LSL_Key osNpcCreate(string firstname, string lastname, LSL_Vector position, LSL_Key cloneFrom) public LSL_Key osNpcCreate(string firstname, string lastname, LSL_Vector position, LSL_Key cloneFrom)
{ {
CheckThreatLevel(ThreatLevel.High, "osNpcCreate"); CheckThreatLevel(ThreatLevel.High, "osNpcCreate");
//QueueUserWorkItem
INPCModule module = World.RequestModuleInterface<INPCModule>(); INPCModule module = World.RequestModuleInterface<INPCModule>();
if (module != null) if (module != null)