* Major structural change: Begun converting Events to use (caller, args) syntax to conform with .NET guidelines.

* OnChatFromViewer has been converted as an example.
* Bug: SimpleApp's NPC client does not implement a Scene property and will likely crash with a NullReferenceException when it attempts to chat.
afrisby
Adam Frisby 2007-10-19 22:02:07 +00:00
parent 9a17bc58a6
commit 1313544ac7
8 changed files with 209 additions and 28 deletions

View File

@ -34,8 +34,108 @@ using OpenSim.Framework.Types;
namespace OpenSim.Framework.Interfaces
{
// Base Args Interface
public interface IEventArgs
{
IScene Scene
{
get;
set;
}
IClientAPI Sender
{
get;
set;
}
}
public delegate void ViewerEffectEventHandler(IClientAPI sender, ViewerEffectPacket.EffectBlock[] effectBlock);
public delegate void ChatFromViewer(byte[] message, byte type, int channel, LLVector3 fromPos, string fromName, LLUUID fromAgentID);
public delegate void ChatFromViewer(Object sender, ChatFromViewerArgs e);
public enum ChatTypeEnum { Whisper = 0, Say = 1, Shout = 2, Broadcast = 0xFF };
/// <summary>
/// ChatFromViewer Arguments
/// </summary>
public class ChatFromViewerArgs : EventArgs, IEventArgs
{
protected string m_message;
protected ChatTypeEnum m_type;
protected int m_channel;
protected LLVector3 m_position;
protected string m_from;
protected IClientAPI m_sender;
protected IScene m_scene;
/// <summary>
/// The message sent by the user
/// </summary>
public string Message
{
get { return m_message; }
set { m_message = value; }
}
/// <summary>
/// The type of message, eg say, shout, broadcast.
/// </summary>
public ChatTypeEnum Type
{
get { return m_type; }
set { m_type = value; }
}
/// <summary>
/// Which channel was this message sent on? Different channels may have different listeners. Public chat is on channel zero.
/// </summary>
public int Channel
{
get { return m_channel; }
set { m_channel = value; }
}
/// <summary>
/// The position of the sender at the time of the message broadcast.
/// </summary>
public LLVector3 Position
{
get { return m_position; }
set { m_position = value; }
}
/// <summary>
/// The name of the sender (needed for scripts)
/// </summary>
public string From
{
get { return m_from; }
set { m_from = value; }
}
/// <summary>
/// The client responsible for sending the message, or null.
/// </summary>
public IClientAPI Sender
{
get { return m_sender; }
set { m_sender = value; }
}
public IScene Scene
{
get { return m_scene; }
set { m_scene = value; }
}
public ChatFromViewerArgs()
{
m_position = new LLVector3();
}
}
public delegate void ImprovedInstantMessage(LLUUID fromAgentID, LLUUID fromAgentSession, LLUUID toAgentID, LLUUID imSessionID, uint timestamp, string fromAgentName, string message, byte dialog); // Cut down from full list
public delegate void RezObject(IClientAPI remoteClient, LLUUID itemID, LLVector3 pos);
public delegate void ModifyTerrain(float height, float seconds, byte size, byte action, float north, float west, IClientAPI remoteClient);

View File

@ -100,7 +100,17 @@ namespace OpenSim.Region.ClientStack
if (OnChatFromViewer != null)
{
this.OnChatFromViewer(message, type, channel, fromPos, fromName, fromAgentID);
ChatFromViewerArgs args = new ChatFromViewerArgs();
args.Channel = channel;
args.From = fromName;
args.Message = Util.FieldToString(message);
args.Type = (ChatTypeEnum)type;
args.Position = fromPos;
args.Scene = Scene;
args.Sender = this;
this.OnChatFromViewer(this, args);
}
break;
case PacketType.ImprovedInstantMessage:

View File

@ -68,7 +68,14 @@ namespace OpenSim.Region.ClientStack
//private AgentAssetUpload UploadAssets;
private LLUUID newAssetFolder = LLUUID.Zero;
private int debug = 0;
protected IScene m_scene;
public IScene Scene
{
get { return m_scene; }
}
private ClientManager m_clientManager;
private AssetCache m_assetCache;
// private InventoryCache m_inventoryCache;

View File

@ -32,6 +32,6 @@ namespace OpenSim.Region.Environment.Interfaces
{
public interface ISimChat
{
void SimChat(byte[] message, byte type, int channel, LLVector3 fromPos, string fromName, LLUUID fromAgentID);
void SimChat(System.Object sender, OpenSim.Framework.Interfaces.ChatFromViewerArgs e);
}
}

View File

@ -33,15 +33,16 @@ using System.Threading;
using libsecondlife;
using OpenSim.Framework.Interfaces;
using OpenSim.Framework.Utilities;
using OpenSim.Framework.Console;
using OpenSim.Region.Environment.Interfaces;
using OpenSim.Region.Environment.Scenes;
using Nini.Config;
namespace OpenSim.Region.Environment.Modules
{
public class ChatModule : IRegionModule, ISimChat
{
private Scene m_scene;
private Scene m_scene;
private LogBase m_log;
private string m_server = null;
private int m_port = 6668;
@ -65,10 +66,12 @@ namespace OpenSim.Region.Environment.Modules
m_nick = "OSimBot" + Util.RandomClass.Next(1, 99);
m_irc = null;
m_ircWriter = null;
m_ircReader = null;
m_ircReader = null;
m_log = OpenSim.Framework.Console.MainLog.Instance;
}
public void Initialise(Scene scene, IConfigSource config)
public void Initialise(Scene scene, Nini.Config.IConfigSource config)
{
try {
m_server = config.Configs["IRC"].GetString("server");
@ -175,41 +178,80 @@ namespace OpenSim.Region.Environment.Modules
}
}
public void SimChat(byte[] message, byte type, int channel, LLVector3 fromPos, string fromName,
LLUUID fromAgentID)
public void SimChat(Object sender, ChatFromViewerArgs e)
{
ScenePresence avatar = null;
avatar = m_scene.GetScenePresence(fromAgentID);
//TODO: Move ForEachScenePresence and others into IScene.
Scene scene = (Scene)e.Scene;
//TODO: Remove the need for this check
if (scene == null)
scene = m_scene;
// Filled in since it's easier than rewriting right now.
LLVector3 fromPos = e.Position;
string fromName = e.From;
string message = e.Message;
byte type = (byte)e.Type;
LLUUID fromAgentID = LLUUID.Zero;
if (e.Sender != null)
avatar = scene.GetScenePresence(e.Sender.AgentId);
if (avatar != null)
{
fromPos = avatar.AbsolutePosition;
fromName = avatar.Firstname + " " + avatar.Lastname;
fromAgentID = e.Sender.AgentId;
avatar = null;
}
}
string typeName;
switch (e.Type)
{
case ChatTypeEnum.Broadcast:
typeName = "broadcasts";
break;
case ChatTypeEnum.Say:
typeName = "says";
break;
case ChatTypeEnum.Shout:
typeName = "shouts";
break;
case ChatTypeEnum.Whisper:
typeName = "whispers";
break;
default:
typeName = "unknown";
break;
}
m_log.Verbose("CHAT", fromName + " (" + e.Channel + ") " + typeName + ": " + e.Message);
if (connected)
{
m_ircWriter.WriteLine("PRIVMSG " + m_channel + " :" + "<" + fromName + ">: " +
Util.FieldToString(message));
e.Message);
m_ircWriter.Flush();
}
if (channel == 0)
if (e.Channel == 0)
{
m_scene.ForEachScenePresence(delegate(ScenePresence presence)
scene.ForEachScenePresence(delegate(ScenePresence presence)
{
int dis = -1000;
//err ??? the following code seems to be request a scenePresence when it already has a ref to it
avatar = m_scene.GetScenePresence(presence.ControllingClient.AgentId);
avatar = scene.GetScenePresence(presence.ControllingClient.AgentId);
if (avatar != null)
{
dis = (int) avatar.AbsolutePosition.GetDistanceTo(fromPos);
}
switch (type)
switch (e.Type)
{
case 0: // Whisper
case ChatTypeEnum.Whisper:
if ((dis < 10) && (dis > -10))
{
//should change so the message is sent through the avatar rather than direct to the ClientView
@ -220,7 +262,7 @@ namespace OpenSim.Region.Environment.Modules
fromAgentID);
}
break;
case 1: // Say
case ChatTypeEnum.Say:
if ((dis < 30) && (dis > -30))
{
//Console.WriteLine("sending chat");
@ -231,7 +273,7 @@ namespace OpenSim.Region.Environment.Modules
fromAgentID);
}
break;
case 2: // Shout
case ChatTypeEnum.Shout:
if ((dis < 100) && (dis > -100))
{
presence.ControllingClient.SendChatMessage(message,
@ -242,7 +284,7 @@ namespace OpenSim.Region.Environment.Modules
}
break;
case 0xff: // Broadcast
case ChatTypeEnum.Broadcast:
presence.ControllingClient.SendChatMessage(message, type,
fromPos,
fromName,

View File

@ -119,13 +119,12 @@ namespace OpenSim.Region.Environment.Modules
client.OnChatFromViewer += DeliverClientMessage;
}
private void DeliverClientMessage(byte[] message, byte type, int channel, LLVector3 fromPos, string fromName,
LLUUID fromAgentID)
private void DeliverClientMessage(Object sender, ChatFromViewerArgs e)
{
ASCIIEncoding ae = new ASCIIEncoding();
DeliverMessage(fromAgentID.ToString(), type, channel, fromName, ae.GetString(message));
DeliverMessage(e.Sender.AgentId.ToString(),
(int)e.Type, e.Channel,
e.Sender.FirstName + " " + e.Sender.LastName,
e.Message);
}
public int Listen(uint localID, LLUUID itemID, LLUUID hostID, int channel, string name, string id, string msg)

View File

@ -70,7 +70,22 @@ namespace OpenSim.Region.Environment.Scenes
{
if (m_simChatModule != null)
{
m_simChatModule.SimChat(message, type, channel, fromPos, fromName, fromAgentID);
ChatFromViewerArgs args = new ChatFromViewerArgs();
args.Message = OpenSim.Framework.Utilities.Util.FieldToString(message);
args.Channel = channel;
args.Type = (ChatTypeEnum)type;
args.Position = fromPos;
ScenePresence user = this.GetScenePresence(fromAgentID);
if (user != null)
args.Sender = user.ControllingClient;
else
args.Sender = null;
args.From = fromName;
m_simChatModule.SimChat(this, args);
}
}

View File

@ -245,7 +245,15 @@ namespace SimpleApp
{
if (OnChatFromViewer != null)
{
this.OnChatFromViewer(enc.GetBytes("Kind of quiet around here, isn't it! \0"), 2, 0, new LLVector3(128, 128, 26), this.FirstName + " " + this.LastName, this.AgentId);
ChatFromViewerArgs args = new ChatFromViewerArgs();
args.Message = "Kinda quiet around here, isn't it?";
args.Channel = 0;
args.From = this.FirstName + " " + this.LastName;
args.Position = new LLVector3(128, 128, 26);
args.Sender = this;
args.Type = ChatTypeEnum.Shout;
this.OnChatFromViewer(this, args);
}
count = -1;