[Bobbot-cvs] Other/UltimaOnline.Sphere.Connection AssemblyInfo.cs,NONE,1.1 MessageParserException.cs
Status: Alpha
Brought to you by:
iainmckay
From: Iain M. <iai...@us...> - 2005-07-17 21:56:47
|
Update of /cvsroot/bobbot/Other/UltimaOnline.Sphere.Connection In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1283/UltimaOnline.Sphere.Connection Added Files: AssemblyInfo.cs MessageParserException.cs SphereConnection.cs Types.cs UltimaOnline.Sphere.Connection.sln Log Message: Changes: - Renamed Bot.Other.SphereConnection to UltimaOnline.Sphere.Connection for use in other projects Added: - New WIP assembly UltimaOnline.Sphere.WorldParser which is to be used in an upcoming plugin and client-side application --- NEW FILE: MessageParserException.cs --- #region LGPL License /* Bastard of a Bot Library Copyright (C) 2004 Bastard of a Bot Team This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #endregion using System; namespace UltimaOnline.Sphere.Connection { /// <summary> /// Summary description for MessageParserException. /// </summary> public class MessageParserException : ApplicationException { private string m_Data; /// <summary> /// Message the parser ran into problems with. /// </summary> public string Data { get { return m_Data; } } public MessageParserException(string message, string data) : base(message) { m_Data = data; } } } --- NEW FILE: UltimaOnline.Sphere.Connection.sln --- Microsoft Visual Studio Solution File, Format Version 8.00 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UltimaOnline.Sphere.Connection", "UltimaOnline.Sphere.Connection.csproj", "{603FDB64-0CA4-4B0C-9AD6-7695885B21E6}" ProjectSection(ProjectDependencies) = postProject EndProjectSection EndProject Global GlobalSection(SolutionConfiguration) = preSolution Debug = Debug Release = Release EndGlobalSection GlobalSection(ProjectConfiguration) = postSolution {603FDB64-0CA4-4B0C-9AD6-7695885B21E6}.Debug.ActiveCfg = Debug|.NET {603FDB64-0CA4-4B0C-9AD6-7695885B21E6}.Debug.Build.0 = Debug|.NET {603FDB64-0CA4-4B0C-9AD6-7695885B21E6}.Release.ActiveCfg = Release|.NET {603FDB64-0CA4-4B0C-9AD6-7695885B21E6}.Release.Build.0 = Release|.NET EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution EndGlobalSection GlobalSection(ExtensibilityAddIns) = postSolution EndGlobalSection EndGlobal --- NEW FILE: SphereConnection.cs --- #region LGPL License /* Bastard of a Bot Library Copyright (C) 2004 Bastard of a Bot Team This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #endregion using System; using System.IO; using System.Text; using System.Timers; using System.Threading; using System.Reflection; using System.Collections; using System.ComponentModel; using System.Net; using System.Net.Sockets; using UltimaOnline.Sphere.Connection.Messages; namespace UltimaOnline.Sphere.Connection { public class SphereConnection { #region Singleton implementation private static SphereConnection _instance; public static SphereConnection Instance { get { return _instance; } } static SphereConnection() { _instance = new SphereConnection(); } #endregion #region Public properties public virtual ISynchronizeInvoke SyncRoot { get { return _syncRoot; } set { _syncRoot = value; } } public virtual bool IsConnected { get { return _connected; } } public virtual string Password { get { return _password; } set { _password = value; } } public virtual int ClientCount { get { return _clientCount; } } public virtual ArrayList ValidAccountProperties { get { return _accountProperties; } } #endregion #region Private members protected bool _connected; protected int _clientCount; protected string _password; protected Socket _socket; protected IPEndPoint _endPoint; protected Thread _clientThread; protected ArrayList _accountProperties; protected ArrayList _messageParsers; protected ArrayList _queryBuffer; protected ArrayList _accountQueryBuffer; protected object _queryTempValue; protected object _accountQueryTempValue; protected ISynchronizeInvoke _syncRoot; protected DisconnectedEventHandler _onDisconnected; protected DisconnectedEventHandler _onConnectionLost; protected MessageReceivedEventHandler _onMessageReceived; #endregion #region Events public event ConnectedEventHandler Connected; public event DisconnectedEventHandler Disconnected; public event DisconnectedEventHandler ConnectionLost; public event DisconnectedEventHandler ConnectionFailed; public event MessageReceivedEventHandler MessageReceived; public event AdminWelcomeEventHandler AdminWelcome; public event AdminBadPasswordEventHandler AdminBadPassword; public event AdminAccountInUseEventHandler AdminAccountInUse; public event ClientPageEventHandler ClientPage; public event ClientLoginEventHandler ClientLogin; public event ClientKilledEventHandler ClientKilled; public event ClientCommandEventHandler ClientCommand; public event ClientConnectedEventHandler ClientConnected; public event ClientBadPasswordEventHandler ClientBadPassword; public event ClientDisconnectedEventHandler ClientDisconnected; public event ClientCharacterSelectedEventHandler ClientCharacterSelected; public event WorldSaveEventHandler WorldSave; public event ServerInfoEventHandler ServerInfo; public event HttpRequestEventHandler HttpRequest; public event SecureModeEnabledEventHandler SecureModeEnabled; public event SecureModeDisabledEventHandler SecureModeDisabled; public event VerboseModeEnabledEventHandler VerboseModeEnabled; public event VerboseModeDisabledEventHandler VerboseModeDisabled; public event ResyncPauseEventHandler ResyncPause; public event ResyncRestartEventHandler ResyncRestart; public event ResyncCompleteEventHandler ResyncComplete; public event EventHandler CustomEvent; public event CustomLogEventHandler CustomLog; public event ShowValueEventHandler ShowValue; public event ScriptErrorEventHandler ScriptError; public event UnknownCommandEventHandler UnknownCommand; public event AccountDoesNotExistEventHandler AccountDoesNotExist; public event RideLostEventHandler RideLost; public event ClientKickedEventHandler ClientKicked; #endregion #region Constructor protected SphereConnection() { _onDisconnected = new DisconnectedEventHandler(OnDisconnected); _onConnectionLost = new DisconnectedEventHandler(OnConnectionLost); _onMessageReceived = new MessageReceivedEventHandler(OnMessageReceived); _messageParsers = new ArrayList(); _messageParsers.Add(typeof(WorldSaveMessage)); _messageParsers.Add(typeof(ShowValueMessage)); //m_MessageParsers.Add(typeof(CustomLogMessage)); _messageParsers.Add(typeof(ClientPageMessage)); _messageParsers.Add(typeof(ServerInfoMessage)); //m_MessageParsers.Add(typeof(ScriptErrorMessage)); _messageParsers.Add(typeof(ClientLoginMessage)); _messageParsers.Add(typeof(HttpRequestMessage)); _messageParsers.Add(typeof(ResyncPauseMessage)); _messageParsers.Add(typeof(ClientKilledMessage)); _messageParsers.Add(typeof(AdminWelcomeMessage)); _messageParsers.Add(typeof(ResyncRestartMessage)); _messageParsers.Add(typeof(ClientCommandMessage)); _messageParsers.Add(typeof(UnknownCommandMessage)); _messageParsers.Add(typeof(ResyncCompleteMessage)); _messageParsers.Add(typeof(ClientConnectedMessage)); _messageParsers.Add(typeof(AdminBadPasswordMessage)); _messageParsers.Add(typeof(SecureModeEnabledMessage)); _messageParsers.Add(typeof(ClientBadPasswordMessage)); _messageParsers.Add(typeof(AdminAccountInUseMessage)); _messageParsers.Add(typeof(SecureModeDisabledMessage)); _messageParsers.Add(typeof(VerboseModeEnabledMessage)); _messageParsers.Add(typeof(ClientDisconnectedMessage)); _messageParsers.Add(typeof(VerboseModeDisabledMessage)); _messageParsers.Add(typeof(AccountDoesNotExistMessage)); _messageParsers.Add(typeof(ClientCharacterSelectedMessage)); _messageParsers.Add(typeof(RideLostMessage)); _messageParsers.Add(typeof(ClientKickedMessage)); _queryBuffer = new ArrayList(); _accountQueryBuffer = new ArrayList(); string[] accountProps = new string[] { "NAME", "PLEVEL", "PRIV", "PASSWORD", "TOTALCONNECTTIME", "LASTCONNECTTIME", "LASTCHARUID", "CHARUID", "FIRSTCONNECTDATE", "FIRSTIP", "LASTCONNECTDATE", "LASTIP", "CHATNAME", "LANG", "CHARS", "JAIL", "BLOCK" }; _accountProperties = new ArrayList(); _accountProperties.AddRange(accountProps); } #endregion #region Public methods public void RegisterHandler(Type toRegister) { if(!toRegister.IsSubclassOf(typeof(BaseMessage))) { throw new ArgumentException("type must be a subclass of BaseMessage", "toRegister"); } if(_messageParsers.Contains(toRegister) == false) { _messageParsers.Add(toRegister); } } public void Connect(IPAddress addr, int port) { if(IsConnected) { return; } _endPoint = new IPEndPoint(addr, port); _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _socket.BeginConnect(_endPoint, new AsyncCallback(OnConnected), this); } public void Disconnect() { if(_socket == null) { return; } _clientThread.Abort(); _clientThread = null; _socket.Shutdown(SocketShutdown.Both); _socket.Close(); _socket = null; OnDisconnectedSync(); } public void Write(byte[] bytes) { if(!IsConnected) { return; } _socket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, null, this); } public void Write(string message) { if(!IsConnected) { return; } Write(message, true); } public void WriteLine(string message, params object[] args) { if (!IsConnected) { return; } Write(String.Format(message, args), true); } public object GetProperty(string property) { return GetProperty(property, typeof(string)); } public object GetProperty(string property, Type type) { int timeout = 0; if(!IsConnected) { return null; } InternalValueMessage queryMsg = new InternalValueMessage(property, type); _queryTempValue = null; WriteLine("show {0}", property); while((IsMessageComplete(queryMsg) == false) && (timeout < 10000)) { Thread.Sleep(50); timeout += 50; } _queryBuffer.Clear(); return _queryTempValue; } public object GetItemProperty(int item, string property, Type type) { return GetProperty(String.Format("uid.0{0:X}.{1}", item, property), type); } public string GetItemProperty(int item, string property) { return (GetItemProperty(item, property, typeof(string)) as String); } public string GetAccountProperty(string account, string property) { return (GetAccountProperty(account, property, typeof(string)) as String); } public object GetAccountProperty(string account, string property, Type type) { if (!IsConnected) { return null; } InternalAccountMessage queryMsg = new InternalAccountMessage(account, property, type); _accountQueryTempValue = null; int timeout = 0; WriteLine("account {0} show {1}", account, property); while((IsAccountMessageComplete(queryMsg) == false) && (timeout < 5000)) { Thread.Sleep( 50 ); timeout += 50; } _accountQueryBuffer.Clear(); return _accountQueryTempValue; } public BaseMessage ParseMessage(string rawMessage) { // Needs rethinking, something more efficient. foreach(Type msgObj in _messageParsers) { BaseMessage msg = (BaseMessage) msgObj.Assembly.CreateInstance(msgObj.FullName); if(msg.CanParse(rawMessage) == false) { continue; } msg.Parse(rawMessage); return msg; } return null; } #endregion #region Private methods private void DataPump() { string msgBuffer = ""; while(Thread.CurrentThread.IsAlive == true) { byte[] buffer = new byte[1024]; try { int numBytes = _socket.Receive(buffer, 0, buffer.Length, SocketFlags.None); if(numBytes != 0) { string message = String.Format("{0}{1}", msgBuffer, Encoding.ASCII.GetString(buffer)); string[] subMessages = message.Split('\n'); for(int i = 0; i < subMessages.Length; i++) { if(subMessages[i].EndsWith("\r")) { OnMessageReceivedSync(new MessageReceivedEventArgs(subMessages[i].Trim('\r', '\n', ' '))); } else { message += subMessages[i]; } } } else { OnConnectionLostSync(); break; } } catch(SocketException ex) { if(ex.NativeErrorCode == 10054) { OnConnectionLostSync(); break; } } } } private void Write(string message, bool appendNewline) { if(!IsConnected) { return; } if(appendNewline) { message += "\n"; } byte[] buffer = Encoding.ASCII.GetBytes(message); _socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, null, this); } private bool IsMessageComplete(InternalValueMessage property) { for (int i = 0; i < _queryBuffer.Count; i++) { if (_queryBuffer[i].GetType() != typeof(InternalValueMessage)) { continue; } InternalValueMessage j = (InternalValueMessage) _queryBuffer[i]; if((j.Item != null) && (j.Property.ToLower() == property.Property.ToLower())) { try { _queryTempValue = Convert.ChangeType(j.Value, property.Type); } catch(FormatException) { _queryTempValue = null; } return true; } } return false; } private bool IsAccountMessageComplete(InternalAccountMessage account) { for(int i = 0; i < _accountQueryBuffer.Count; i++) { if (_accountQueryBuffer[i].GetType() != typeof(InternalAccountMessage)) { continue; } InternalAccountMessage j = (InternalAccountMessage) _accountQueryBuffer[i]; if((j.Property == null) || (j.Value == null)) { _accountQueryTempValue = null; return true; } if((j.Account.ToLower() == account.Account.ToLower()) && (j.Property.ToLower() == account.Property.ToLower())) { try { _accountQueryTempValue = Convert.ChangeType(j.Value, account.Type); } catch(FormatException e) { Console.WriteLine(e.ToString()); _accountQueryTempValue = null; } return true; } } return false; } #endregion #region Event handlers internal void OnMessageReceivedSync(MessageReceivedEventArgs args) { // Hack to get properties working ShowValueMessage msg = new ShowValueMessage(); if(msg.CanParse(args.Message)) { msg.Parse(args.Message); if((msg.Value != null) && (msg.Property != null)) { if (msg.Property.ToLower() == "clients") { _clientCount = Int32.Parse(msg.Value); } if(this.ValidAccountProperties.Contains(msg.Property.ToUpper())) { _accountQueryBuffer.Add(new InternalAccountMessage(msg.Object, msg.Property, msg.Value, null)); } else { _queryBuffer.Add(new InternalValueMessage(msg.Property, msg.Object, msg.Value, null)); } } } if((_syncRoot != null) && (_syncRoot.InvokeRequired == true)) { _syncRoot.BeginInvoke(_onMessageReceived, new object[] { this, args }); } else { OnMessageReceived(this, args); } } internal void OnDisconnectedSync() { if((_syncRoot != null) && (_syncRoot.InvokeRequired == true)) { _syncRoot.Invoke(_onDisconnected, new object[] { this, new DisconnectedEventArgs(_endPoint) }); } else { OnDisconnected(this, new DisconnectedEventArgs(_endPoint)); } } internal void OnConnectionLostSync() { if((_syncRoot != null) && (_syncRoot.InvokeRequired == true)) { _syncRoot.Invoke(_onConnectionLost, new object[] { this, new DisconnectedEventArgs(_endPoint) }); } else { OnConnectionLost(this, new DisconnectedEventArgs(_endPoint)); } } internal void OnConnected(IAsyncResult ar) { if(_socket.Connected) { _connected = true; Write(new byte[] {1}); Thread.Sleep( 500 ); // Pause between login code & password to ensure connection success. Write(Password); _clientThread = new Thread(new ThreadStart(DataPump)); _clientThread.IsBackground = true; //_clientThread.Priority = ThreadPriority.Highest; _clientThread.Start(); if(Connected != null) { Connected(this, new ConnectedEventArgs(_endPoint)); } } else { _connected = false; if(ConnectionFailed != null) { ConnectionFailed(this, new DisconnectedEventArgs(_endPoint)); } } } internal void OnConnectionLost(object sender, DisconnectedEventArgs args) { _connected = false; if(ConnectionLost != null) { ConnectionLost(this, new DisconnectedEventArgs(_endPoint)); } } internal void OnDisconnected(object sender, DisconnectedEventArgs args) { _connected = false; if(Disconnected != null) { Disconnected(this, args); } } internal void OnMessageReceived(object sender, MessageReceivedEventArgs args) { BaseMessage msg = ParseMessage(args.Message); if(msg == null) { // Try to see if its a script error message. msg = new ScriptErrorMessage(); if (msg.CanParse(args.Message)) { msg.Parse(args.Message); } else { // Try to see if its a custom log message. msg = new CustomLogMessage(); if(msg.CanParse(args.Message)) { msg.Parse(args.Message); } else { return; } } } msg.Notify(this); if(MessageReceived != null) { MessageReceived(this, args); } } internal void OnClientConnected(ClientConnectedEventArgs args) { _clientCount = args.Message.ClientCount; if(ClientConnected != null) { ClientConnected(this, args); } } internal void OnClientLogin(ClientLoginEventArgs args) { if(ClientLogin != null) { ClientLogin(this, args); } } internal void OnClientCharacterSelected(ClientCharacterSelectedEventArgs args) { if(ClientCharacterSelected != null) { ClientCharacterSelected(this, args); } } internal void OnClientBadPassword(ClientBadPasswordEventArgs args) { if(ClientBadPassword != null) { ClientBadPassword(this, args); } } internal void OnClientKilled(ClientKilledEventArgs args) { if(ClientKilled != null) { ClientKilled(this, args); } } internal void OnClientDisconnected(ClientDisconnectedEventArgs args) { _clientCount = args.Message.ClientCount; if(ClientDisconnected != null) { ClientDisconnected(this, args); } } internal void OnServerInfo(ServerInfoEventArgs args) { if(ServerInfo != null) { ServerInfo(this, args); } } internal void OnHttpRequest(HttpRequestEventArgs args) { if(HttpRequest != null) { HttpRequest(this, args); } } internal void OnWorldSave(WorldSaveEventArgs args) { if(WorldSave != null) { WorldSave(this, args); } } internal void OnAdminWelcome(AdminWelcomeEventArgs args) { if(AdminWelcome != null) { AdminWelcome(this, args); } } internal void OnAdminBadPassword(AdminBadPasswordEventArgs args) { if(AdminBadPassword != null) { AdminBadPassword(this, args); } } internal void OnAdminAccountInUse(AdminAccountInUseEventArgs args) { if(AdminAccountInUse != null) { AdminAccountInUse(this, args); } } internal void OnVerboseModeEnabled(VerboseModeEnabledEventArgs args) { if(VerboseModeEnabled != null) { VerboseModeEnabled(this, args); } } internal void OnVerboseModeDisabled(VerboseModeDisabledEventArgs args) { if(VerboseModeDisabled != null) { VerboseModeDisabled(this, args); } } internal void OnSecureModeEnabled(SecureModeEnabledEventArgs args) { if(SecureModeEnabled != null) { SecureModeEnabled(this, args); } } internal void OnSecureModeDisabled(SecureModeDisabledEventArgs args) { if(SecureModeDisabled != null) { SecureModeDisabled(this, args); } } internal void OnResyncPause(ResyncPauseEventArgs args) { if(ResyncPause != null) { ResyncPause(this, args); } } internal void OnResyncRestart(ResyncRestartEventArgs args) { if(ResyncRestart != null) { ResyncRestart(this, args); } } internal void OnResyncComplete(ResyncCompleteEventArgs args) { if(ResyncComplete != null) { ResyncComplete(this, args); } } internal void OnClientPage(ClientPageEventArgs args) { if(ClientPage != null) { ClientPage(this, args); } } internal void OnClientCommand(ClientCommandEventArgs args) { if(ClientCommand != null) { ClientCommand(this, args); } } internal void OnShowValue(ShowValueEventArgs args) { if (ShowValue != null) { ShowValue(this, args); } } internal void OnScriptError(ScriptErrorEventArgs args) { if (ScriptError != null) { ScriptError(this, args); } } internal void OnCustomLog(CustomLogEventArgs args) { if(CustomLog != null) { CustomLog(this, args); } } internal void OnAccountDoesNotExist(AccountDoesNotExistEventArgs args) { _accountQueryBuffer.Add(new InternalAccountMessage(args.Message.Account, null, null)); if (AccountDoesNotExist != null) { AccountDoesNotExist(this, args); } } internal void OnUnknownCommand(UnknownCommandEventArgs args) { if(args.Message.Command.ToLower().StartsWith("account ")) { string[] command = args.Message.Command.Split(' '); if (command.Length >= 3) { if (command[2].ToLower() == "show") { _accountQueryBuffer.Add(new InternalAccountMessage(command[1], command[3], null)); } } } else if (args.Message.Command.ToLower().StartsWith("show ")) { _queryBuffer.Add(new InternalValueMessage(args.Message.Command.Split(' ')[1], null)); } if (UnknownCommand != null) { UnknownCommand(this, args); } } internal void OnRideLost(RideLostEventArgs args) { if (RideLost != null) { RideLost(this, args); } } internal void OnClientKicked(ClientKickedEventArgs args) { if (ClientKicked != null) { ClientKicked(this, args); } } public void OnCustomEvent(EventArgs args) { if(CustomEvent != null) { CustomEvent(this, args); } } #endregion #region Internal classes private class InternalValueMessage { #region Private members private string _property; private string _item; private object _value; private Type _type; #endregion #region Public properties public string Property { get { return _property; } set { _property = value; } } public string Item { get { return _item; } set { _item = value; } } public object Value { get { return _value; } set { _value = value; } } public Type Type { get { return _type; } set { _type = value; } } #endregion #region Constructor public InternalValueMessage(string property, Type type) : this(property, null, null, type) { } public InternalValueMessage(string property, string item, object value, Type type) { _property = property; _item = item; _value = value; _type = type; } #endregion } private class InternalAccountMessage { #region Private members private string _account; private string _property; private object _value; private Type _type; #endregion #region Public properties public string Account { get { return _account; } set { _account = value; } } public string Property { get { return _property; } set { _property = value; } } public object Value { get { return _value; } set { _value = value; } } public Type Type { get { return _type; } set { _type = value; } } #endregion #region Constructor public InternalAccountMessage(string account, string property, Type type) : this(account, property, null, type) { } public InternalAccountMessage(string account, string property, object value, Type type) { _account = account; _property = property; _value = value; _type = type; } #endregion } #endregion } #region Delegates and event args public delegate void ConnectedEventHandler(object sender, ConnectedEventArgs args); public delegate void ConnectingEventHandler(object sender, ConnectingEventArgs args); public delegate void DisconnectedEventHandler(object sender, DisconnectedEventArgs args); public delegate void MessageReceivedEventHandler(object sender, MessageReceivedEventArgs args); public class ConnectingEventArgs : EventArgs { private IPEndPoint _endPoint; public IPEndPoint EndPoint { get { return _endPoint; } } public ConnectingEventArgs(IPEndPoint endpoint) { _endPoint = endpoint; } } public class ConnectedEventArgs : EventArgs { private IPEndPoint _endPoint; public IPEndPoint EndPoint { get { return _endPoint; } } public ConnectedEventArgs(IPEndPoint endpoint) { _endPoint = endpoint; } } public class DisconnectedEventArgs : EventArgs { private IPEndPoint _endPoint; public IPEndPoint EndPoint { get { return _endPoint; } } public DisconnectedEventArgs(IPEndPoint endpoint) { _endPoint = endpoint; } } public class MessageReceivedEventArgs : EventArgs { private string _message; public string Message { get { return _message; } } public MessageReceivedEventArgs(string message) { _message = message; } } #endregion } --- NEW FILE: AssemblyInfo.cs --- using System.Reflection; using System.Runtime.CompilerServices; // // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. // [assembly: AssemblyTitle("UltimaOnline.Sphere.Connection")] [assembly: AssemblyDescription("Provides an event-based interface to the remote admin of a sphere server.")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("UltimaOnline.Sphere")] [assembly: AssemblyCopyright("Iain Mckay")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] // // Version information for an assembly consists of the following four values: // // Major Version // Minor Version // Build Number // Revision // // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: [assembly: AssemblyVersion("1.0.*")] // // In order to sign your assembly you must specify a key to use. Refer to the // Microsoft .NET Framework documentation for more information on assembly signing. // // Use the attributes below to control which key is used for signing. // // Notes: // (*) If no key is specified, the assembly is not signed. // (*) KeyName refers to a key that has been installed in the Crypto Service // Provider (CSP) on your machine. KeyFile refers to a file which contains // a key. // (*) If the KeyFile and the KeyName values are both specified, the // following processing occurs: // (1) If the KeyName can be found in the CSP, that key is used. // (2) If the KeyName does not exist and the KeyFile does exist, the key // in the KeyFile is installed into the CSP and used. // (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility. // When specifying the KeyFile, the location of the KeyFile should be // relative to the project output directory which is // %Project Directory%\obj\<configuration>. For example, if your KeyFile is // located in the project directory, you would specify the AssemblyKeyFile // attribute as [assembly: AssemblyKeyFile("..\\..\\mykey.snk")] // (*) Delay Signing is an advanced option - see the Microsoft .NET Framework // documentation for more information on this. // [assembly: AssemblyDelaySign(false)] [assembly: AssemblyKeyFile("")] [assembly: AssemblyKeyName("")] --- NEW FILE: Types.cs --- #region LGPL License /* Bastard of a Bot Library Copyright (C) 2004 Bastard of a Bot Team This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #endregion using System; namespace UltimaOnline.Sphere.Connection { public enum MobileType { Player, Npc } public struct Mobile { #region Public members public MobileType Type; public string Name; #endregion #region Constructor public Mobile(MobileType type, string name) { Type = type; Name = name; } #endregion } /// <summary> /// Represents a position in the game world. /// </summary> public struct Position { /// <summary> /// Location on the x-axis. /// </summary> public int X; /// <summary> /// Location on the y-axis. /// </summary> public int Y; /// <summary> /// Location on the z-axis. /// </summary> public int Z; /// <summary> /// Plane they are on. /// </summary> public int Plane; /// <summary> /// Parses the given position string. /// </summary> /// <param name="position">Position to parse.</param> /// <returns> /// A new <see cref="Position"/> structure representing the position. /// </returns> public Position Parse(string position) { string[] parts = position.Split(','); Position pos = new Position(); if (parts.Length == 4) { try { pos.X = Int32.Parse(parts[0]); pos.Y = Int32.Parse(parts[1]); pos.Z = Int32.Parse(parts[2]); pos.Plane = Int32.Parse(parts[3]); } catch { } } else if (parts.Length == 3) { try { pos.X = Int32.Parse(parts[0]); pos.Y = Int32.Parse(parts[1]); pos.Z = Int32.Parse(parts[2]); } catch { } } else if (parts.Length == 2) { try { pos.X = Int32.Parse(parts[0]); pos.Y = Int32.Parse(parts[1]); } catch { } } return pos; } /// <summary> /// Overriden to return the position as it would be seen in-game. /// </summary> /// <returns>Position as it would be in-game.</returns> public override string ToString() { if (Plane != 0) return String.Format("{0},{1},{2},{3}", X, Y, Z, Plane); else if (Z != 0) return String.Format("{0},{1},{2}", X, Y, Z); else return String.Format("{0},{1}", X, Y); //return String.Format("{0},{1},{2},{3}", X, Y, Z, Plane); } } } |