diff --git a/Server/ClientVersion.cs b/Server/ClientVersion.cs index d580f776f..c3f67dd94 100644 --- a/Server/ClientVersion.cs +++ b/Server/ClientVersion.cs @@ -98,7 +98,7 @@ namespace Server m_Patch = pat; m_Type = type; - m_SourceString = ToString(); + m_SourceString = _ToStringImpl(); } public static bool operator == ( ClientVersion l, ClientVersion r ) @@ -153,36 +153,41 @@ namespace Server && m_Type == v.m_Type; } - public override string ToString() + private string _ToStringImpl() { - StringBuilder builder = new StringBuilder( 16 ); + StringBuilder builder = new StringBuilder(16); - builder.Append( m_Major ); - builder.Append( '.' ); - builder.Append( m_Minor ); - builder.Append( '.' ); - builder.Append( m_Revision ); + builder.Append(m_Major); + builder.Append('.'); + builder.Append(m_Minor); + builder.Append('.'); + builder.Append(m_Revision); - if( m_Major <= 5 && m_Minor <= 0 && m_Revision <= 6 ) //Anything before 5.0.7 + if (m_Major <= 5 && m_Minor <= 0 && m_Revision <= 6) //Anything before 5.0.7 { - if( m_Patch > 0 ) - builder.Append( (char)('a' + (m_Patch - 1)) ); + if (m_Patch > 0) + builder.Append((char)('a' + (m_Patch - 1))); } else { - builder.Append( '.' ); - builder.Append( m_Patch ); + builder.Append('.'); + builder.Append(m_Patch); } - if ( m_Type != ClientType.Regular ) + if (m_Type != ClientType.Regular) { - builder.Append( ' ' ); - builder.Append( m_Type.ToString() ); + builder.Append(' '); + builder.Append(m_Type.ToString()); } return builder.ToString(); } + public override string ToString() + { + return _ToStringImpl(); + } + public ClientVersion( string fmt ) { m_SourceString = fmt; diff --git a/Server/EventSink.cs b/Server/EventSink.cs index 2293c80ca..85de32d6c 100644 --- a/Server/EventSink.cs +++ b/Server/EventSink.cs @@ -796,7 +796,7 @@ namespace Server } } - public class FastWalkEventArgs + public class FastWalkEventArgs : EventArgs { private NetState m_State; private bool m_Blocked; diff --git a/Server/Main.cs b/Server/Main.cs index b078eabe2..8037637ae 100644 --- a/Server/Main.cs +++ b/Server/Main.cs @@ -356,7 +356,7 @@ namespace Server } } - private enum ConsoleEventType + internal enum ConsoleEventType { CTRL_C_EVENT, CTRL_BREAK_EVENT, @@ -365,18 +365,20 @@ namespace Server CTRL_SHUTDOWN_EVENT } - private delegate bool ConsoleEventHandler( ConsoleEventType type ); - private static ConsoleEventHandler m_ConsoleEventHandler; + internal delegate bool ConsoleEventHandler( ConsoleEventType type ); + internal static ConsoleEventHandler m_ConsoleEventHandler; - [DllImport( "Kernel32" )] - private static extern bool SetConsoleCtrlHandler( ConsoleEventHandler callback, bool add ); + internal class UnsafeNativeMethods { + [DllImport("Kernel32")] + internal static extern bool SetConsoleCtrlHandler(ConsoleEventHandler callback, bool add); + } private static bool OnConsoleEvent( ConsoleEventType type ) { if( World.Saving || ( m_Service && type == ConsoleEventType.CTRL_LOGOFF_EVENT ) ) return true; - Kill(); //Kill -> HandleClosed will hadnle waiting for the completion of flushign to disk + Kill(); //Kill -> HandleClosed will handle waiting for the completion of flushing to disk return true; } @@ -530,7 +532,7 @@ namespace Server } else { m_ConsoleEventHandler = new ConsoleEventHandler( OnConsoleEvent ); - SetConsoleCtrlHandler( m_ConsoleEventHandler, true ); + UnsafeNativeMethods.SetConsoleCtrlHandler( m_ConsoleEventHandler, true ); } if ( GCSettings.IsServerGC ) diff --git a/Server/Mobile.cs b/Server/Mobile.cs index 0a19f6656..f2479c26a 100644 --- a/Server/Mobile.cs +++ b/Server/Mobile.cs @@ -475,6 +475,7 @@ namespace Server } #endregion + [Serializable] public class MobileNotConnectedException : Exception { public MobileNotConnectedException( Mobile source, string message ) diff --git a/Server/NativeReader.cs b/Server/NativeReader.cs index c737773ee..dbffdde77 100644 --- a/Server/NativeReader.cs +++ b/Server/NativeReader.cs @@ -20,6 +20,7 @@ using System; using System.Runtime.InteropServices; +using System.Threading; namespace Server { public static class NativeReader { @@ -43,26 +44,35 @@ namespace Server { } public sealed class NativeReaderWin32 : INativeReader { - [DllImport( "kernel32" )] - private unsafe static extern int _lread( IntPtr hFile, void *lpBuffer, int wBytes ); + internal class UnsafeNativeMethods { + /*[DllImport("kernel32")] + internal unsafe static extern int _lread(IntPtr hFile, void* lpBuffer, int wBytes);*/ + + [DllImport("kernel32")] + internal unsafe static extern bool ReadFile(IntPtr hFile, void* lpBuffer, uint nNumberOfBytesToRead, ref uint lpNumberOfBytesRead, NativeOverlapped* lpOverlapped); + } public NativeReaderWin32() { } public unsafe void Read( IntPtr ptr, void *buffer, int length ) { - _lread( ptr, buffer, length ); + //UnsafeNativeMethods._lread( ptr, buffer, length ); + uint lpNumberOfBytesRead = 0; + UnsafeNativeMethods.ReadFile(ptr, buffer, (uint)length, ref lpNumberOfBytesRead, null); } } public sealed class NativeReaderUnix : INativeReader { - [DllImport( "libc" )] - private unsafe static extern int read( IntPtr ptr, void *buffer, int length ); + internal class UnsafeNativeMethods { + [DllImport("libc")] + internal unsafe static extern int read(IntPtr ptr, void* buffer, int length); + } public NativeReaderUnix() { } public unsafe void Read( IntPtr ptr, void *buffer, int length ) { - read( ptr, buffer, length ); + UnsafeNativeMethods.read( ptr, buffer, length ); } } } \ No newline at end of file diff --git a/Server/Network/Compression.cs b/Server/Network/Compression.cs index 90c5e2478..87546f194 100644 --- a/Server/Network/Compression.cs +++ b/Server/Network/Compression.cs @@ -227,149 +227,157 @@ namespace Server.Network { } public sealed class Compressor32 : ICompressor { - [DllImport( "zlib32" )] - private static extern string zlibVersion(); + internal class SafeNativeMethods { + [DllImport( "zlib32" )] + internal static extern string zlibVersion(); - [DllImport( "zlib32" )] - private static extern ZLibError compress( byte[] dest, ref int destLength, byte[] source, int sourceLength ); + [DllImport( "zlib32" )] + internal static extern ZLibError compress(byte[] dest, ref int destLength, byte[] source, int sourceLength); - [DllImport( "zlib32" )] - private static extern ZLibError compress2( byte[] dest, ref int destLength, byte[] source, int sourceLength, ZLibQuality quality ); + [DllImport( "zlib32" )] + internal static extern ZLibError compress2(byte[] dest, ref int destLength, byte[] source, int sourceLength, ZLibQuality quality); - [DllImport( "zlib32" )] - private static extern ZLibError uncompress( byte[] dest, ref int destLen, byte[] source, int sourceLen ); + [DllImport( "zlib32" )] + internal static extern ZLibError uncompress(byte[] dest, ref int destLen, byte[] source, int sourceLen); + } public Compressor32() { } public string Version { get { - return zlibVersion(); + return SafeNativeMethods.zlibVersion(); } } public ZLibError Compress( byte[] dest, ref int destLength, byte[] source, int sourceLength ) { - return compress( dest, ref destLength, source, sourceLength ); + return SafeNativeMethods.compress(dest, ref destLength, source, sourceLength); } public ZLibError Compress( byte[] dest, ref int destLength, byte[] source, int sourceLength, ZLibQuality quality ) { - return compress2( dest, ref destLength, source, sourceLength, quality ); + return SafeNativeMethods.compress2(dest, ref destLength, source, sourceLength, quality); } public ZLibError Decompress( byte[] dest, ref int destLength, byte[] source, int sourceLength ) { - return uncompress( dest, ref destLength, source, sourceLength ); + return SafeNativeMethods.uncompress(dest, ref destLength, source, sourceLength); } } public sealed class Compressor64 : ICompressor { - [DllImport( "zlib64" )] - private static extern string zlibVersion(); + internal class SafeNativeMethods { + [DllImport("zlib64")] + internal static extern string zlibVersion(); - [DllImport( "zlib64" )] - private static extern ZLibError compress( byte[] dest, ref int destLength, byte[] source, int sourceLength ); + [DllImport("zlib64")] + internal static extern ZLibError compress(byte[] dest, ref int destLength, byte[] source, int sourceLength); - [DllImport( "zlib64" )] - private static extern ZLibError compress2( byte[] dest, ref int destLength, byte[] source, int sourceLength, ZLibQuality quality ); + [DllImport("zlib64")] + internal static extern ZLibError compress2(byte[] dest, ref int destLength, byte[] source, int sourceLength, ZLibQuality quality); - [DllImport( "zlib64" )] - private static extern ZLibError uncompress( byte[] dest, ref int destLen, byte[] source, int sourceLen ); + [DllImport("zlib64")] + internal static extern ZLibError uncompress(byte[] dest, ref int destLen, byte[] source, int sourceLen); + } public Compressor64() { } public string Version { get { - return zlibVersion(); + return SafeNativeMethods.zlibVersion(); } } public ZLibError Compress( byte[] dest, ref int destLength, byte[] source, int sourceLength ) { - return compress( dest, ref destLength, source, sourceLength ); + return SafeNativeMethods.compress(dest, ref destLength, source, sourceLength); } public ZLibError Compress( byte[] dest, ref int destLength, byte[] source, int sourceLength, ZLibQuality quality ) { - return compress2( dest, ref destLength, source, sourceLength, quality ); + return SafeNativeMethods.compress2(dest, ref destLength, source, sourceLength, quality); } public ZLibError Decompress( byte[] dest, ref int destLength, byte[] source, int sourceLength ) { - return uncompress( dest, ref destLength, source, sourceLength ); + return SafeNativeMethods.uncompress(dest, ref destLength, source, sourceLength); } } public sealed class CompressorUnix32 : ICompressor { - [DllImport( "libz" )] - private static extern string zlibVersion(); + internal class SafeNativeMethods { + [DllImport("libz")] + internal static extern string zlibVersion(); - [DllImport( "libz" )] - private static extern ZLibError compress( byte[] dest, ref int destLength, byte[] source, int sourceLength ); + [DllImport("libz")] + internal static extern ZLibError compress(byte[] dest, ref int destLength, byte[] source, int sourceLength); - [DllImport( "libz" )] - private static extern ZLibError compress2( byte[] dest, ref int destLength, byte[] source, int sourceLength, ZLibQuality quality ); + [DllImport("libz")] + internal static extern ZLibError compress2(byte[] dest, ref int destLength, byte[] source, int sourceLength, ZLibQuality quality); - [DllImport( "libz" )] - private static extern ZLibError uncompress( byte[] dest, ref int destLen, byte[] source, int sourceLen ); + [DllImport("libz")] + internal static extern ZLibError uncompress(byte[] dest, ref int destLen, byte[] source, int sourceLen); + } public CompressorUnix32() { } public string Version { get { - return zlibVersion(); + return SafeNativeMethods.zlibVersion(); } } public ZLibError Compress( byte[] dest, ref int destLength, byte[] source, int sourceLength ) { - return compress( dest, ref destLength, source, sourceLength ); + return SafeNativeMethods.compress(dest, ref destLength, source, sourceLength); } public ZLibError Compress( byte[] dest, ref int destLength, byte[] source, int sourceLength, ZLibQuality quality ) { - return compress2( dest, ref destLength, source, sourceLength, quality ); + return SafeNativeMethods.compress2(dest, ref destLength, source, sourceLength, quality); } public ZLibError Decompress( byte[] dest, ref int destLength, byte[] source, int sourceLength ) { - return uncompress( dest, ref destLength, source, sourceLength ); + return SafeNativeMethods.uncompress(dest, ref destLength, source, sourceLength); } } public sealed class CompressorUnix64 : ICompressor { - [DllImport( "libz" )] - private static extern string zlibVersion(); + internal class SafeNativeMethods { + [DllImport("libz")] + internal static extern string zlibVersion(); - [DllImport( "libz" )] - private static extern ZLibError compress( byte[] dest, ref ulong destLength, byte[] source, int sourceLength ); + [DllImport("libz")] + internal static extern ZLibError compress(byte[] dest, ref ulong destLength, byte[] source, int sourceLength); - [DllImport( "libz" )] - private static extern ZLibError compress2( byte[] dest, ref ulong destLength, byte[] source, int sourceLength, ZLibQuality quality ); + [DllImport("libz")] + internal static extern ZLibError compress2(byte[] dest, ref ulong destLength, byte[] source, int sourceLength, ZLibQuality quality); - [DllImport( "libz" )] - private static extern ZLibError uncompress( byte[] dest, ref ulong destLen, byte[] source, int sourceLen ); + [DllImport("libz")] + internal static extern ZLibError uncompress(byte[] dest, ref ulong destLen, byte[] source, int sourceLen); + } public CompressorUnix64() { } public string Version { get { - return zlibVersion(); + return SafeNativeMethods.zlibVersion(); } } public ZLibError Compress( byte[] dest, ref int destLength, byte[] source, int sourceLength ) { ulong destLengthLong = (ulong)destLength; - ZLibError z = compress( dest, ref destLengthLong, source, sourceLength ); + ZLibError z = SafeNativeMethods.compress(dest, ref destLengthLong, source, sourceLength); destLength = (int)destLengthLong; return z; } public ZLibError Compress( byte[] dest, ref int destLength, byte[] source, int sourceLength, ZLibQuality quality ) { ulong destLengthLong = (ulong)destLength; - ZLibError z = compress2( dest, ref destLengthLong, source, sourceLength, quality ); + ZLibError z = SafeNativeMethods.compress2(dest, ref destLengthLong, source, sourceLength, quality); destLength = (int)destLengthLong; return z; } public ZLibError Decompress( byte[] dest, ref int destLength, byte[] source, int sourceLength ) { ulong destLengthLong = (ulong)destLength; - ZLibError z = uncompress( dest, ref destLengthLong, source, sourceLength ); + ZLibError z = SafeNativeMethods.uncompress(dest, ref destLengthLong, source, sourceLength); destLength = (int)destLengthLong; return z; } diff --git a/Server/Persistence/FileOperations.cs b/Server/Persistence/FileOperations.cs index ce132ae53..ce334e74e 100644 --- a/Server/Persistence/FileOperations.cs +++ b/Server/Persistence/FileOperations.cs @@ -37,8 +37,10 @@ namespace Server { #if !MONO private const FileOptions NoBuffering = ( FileOptions ) 0x20000000; - [DllImport( "Kernel32", CharSet = CharSet.Auto, SetLastError = true )] - private static extern SafeFileHandle CreateFile( string lpFileName, int dwDesiredAccess, FileShare dwShareMode, IntPtr securityAttrs, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile ); + internal static class UnsafeNativeMethods { + [DllImport("Kernel32", CharSet = CharSet.Unicode, SetLastError = true)] + internal static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, FileShare dwShareMode, IntPtr securityAttrs, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile); + } #endif private static int bufferSize = 1 * MB; @@ -101,7 +103,7 @@ namespace Server { return new FileStream( path, mode, access, share, bufferSize, options ); } - SafeFileHandle fileHandle = CreateFile( path, (int) access, share, IntPtr.Zero, mode, (int) options, IntPtr.Zero ); + SafeFileHandle fileHandle = UnsafeNativeMethods.CreateFile(path, (int)access, share, IntPtr.Zero, mode, (int)options, IntPtr.Zero); if ( fileHandle.IsInvalid ) { throw new IOException(); diff --git a/Server/Random.cs b/Server/Random.cs index 280c412fb..d68b31f81 100644 --- a/Server/Random.cs +++ b/Server/Random.cs @@ -116,6 +116,7 @@ namespace Server { private int _Index = 0; private object _sync = new object(); + private object _syncB = new object(); public CSPRandom() { _CSP.GetBytes(_Working); @@ -127,7 +128,7 @@ namespace Server { if (_Index + c < BUFFER_SIZE) return; - lock (_Buffer) { + lock (_syncB) { byte[] b = _Working; _Working = _Buffer; _Buffer = b; @@ -138,7 +139,7 @@ namespace Server { } private void Fill(object o) { - lock (_Buffer) + lock (_syncB) lock (_CSP) _CSP.GetBytes(_Buffer); } @@ -214,11 +215,13 @@ namespace Server { } public sealed class RDRand32 : IRandomImpl, IHardwareRNG { - [DllImport("rdrand32")] - private static extern RDRandError rdrand_32(ref uint rand, bool retry); + internal class SafeNativeMethods { + [DllImport("rdrand32")] + internal static extern RDRandError rdrand_32(ref uint rand, bool retry); - [DllImport("rdrand32")] - private static extern RDRandError rdrand_get_bytes(int n, byte[] buffer); + [DllImport("rdrand32")] + internal static extern RDRandError rdrand_get_bytes(int n, byte[] buffer); + } private static int BUFFER_SIZE = 0x10000; private static int LARGE_REQUEST = 0x40; @@ -229,15 +232,16 @@ namespace Server { private int _Index = 0; private object _sync = new object(); + private object _syncB = new object(); public RDRand32() { - rdrand_get_bytes(BUFFER_SIZE, _Working); + SafeNativeMethods.rdrand_get_bytes(BUFFER_SIZE, _Working); ThreadPool.QueueUserWorkItem(new WaitCallback(Fill)); } public bool IsSupported() { uint r = 0; - return rdrand_32(ref r, true) == RDRandError.Success; + return SafeNativeMethods.rdrand_32(ref r, true) == RDRandError.Success; } private void CheckSwap(int c) { @@ -245,7 +249,7 @@ namespace Server { if (_Index + c < BUFFER_SIZE) return; - lock (_Buffer) { + lock (_syncB) { byte[] b = _Working; _Working = _Buffer; _Buffer = b; @@ -256,8 +260,8 @@ namespace Server { } private void Fill(object o) { - lock (_Buffer) - rdrand_get_bytes(BUFFER_SIZE, _Buffer); + lock (_syncB) + SafeNativeMethods.rdrand_get_bytes(BUFFER_SIZE, _Buffer); } private void _GetBytes(byte[] b) { @@ -299,7 +303,7 @@ namespace Server { int c = b.Length; if (c >= LARGE_REQUEST) { - rdrand_get_bytes(c, b); + SafeNativeMethods.rdrand_get_bytes(c, b); return; } _GetBytes(b); @@ -330,11 +334,13 @@ namespace Server { } public sealed class RDRand64 : IRandomImpl, IHardwareRNG { - [DllImport("rdrand64")] - private static extern RDRandError rdrand_64(ref ulong rand, bool retry); + internal class SafeNativeMethods { + [DllImport("rdrand64")] + internal static extern RDRandError rdrand_64(ref ulong rand, bool retry); - [DllImport("rdrand64")] - private static extern RDRandError rdrand_get_bytes(int n, byte[] buffer); + [DllImport("rdrand64")] + internal static extern RDRandError rdrand_get_bytes(int n, byte[] buffer); + } private static int BUFFER_SIZE = 0x10000; private static int LARGE_REQUEST = 0x40; @@ -345,15 +351,16 @@ namespace Server { private int _Index = 0; private object _sync = new object(); + private object _syncB = new object(); public RDRand64() { - rdrand_get_bytes(BUFFER_SIZE, _Working); + SafeNativeMethods.rdrand_get_bytes(BUFFER_SIZE, _Working); ThreadPool.QueueUserWorkItem(new WaitCallback(Fill)); } public bool IsSupported() { ulong r = 0; - return rdrand_64(ref r, true) == RDRandError.Success; + return SafeNativeMethods.rdrand_64(ref r, true) == RDRandError.Success; } private void CheckSwap(int c) { @@ -361,7 +368,7 @@ namespace Server { if (_Index + c < BUFFER_SIZE) return; - lock (_Buffer) { + lock (_syncB) { byte[] b = _Working; _Working = _Buffer; _Buffer = b; @@ -372,8 +379,8 @@ namespace Server { } private void Fill(object o) { - lock (_Buffer) - rdrand_get_bytes(BUFFER_SIZE, _Buffer); + lock (_syncB) + SafeNativeMethods.rdrand_get_bytes(BUFFER_SIZE, _Buffer); } private void _GetBytes(byte[] b) { @@ -415,7 +422,7 @@ namespace Server { int c = b.Length; if (c >= LARGE_REQUEST) { - rdrand_get_bytes(c, b); + SafeNativeMethods.rdrand_get_bytes(c, b); return; } _GetBytes(b); diff --git a/Server/ScriptCompiler.cs b/Server/ScriptCompiler.cs index e97251b27..f58ad16cd 100644 --- a/Server/ScriptCompiler.cs +++ b/Server/ScriptCompiler.cs @@ -462,7 +462,7 @@ namespace Server Utility.PushColor( ConsoleColor.DarkYellow ); foreach( CompilerError e in list ) - Console.WriteLine( " {0}: Line {1}: {3}", e.ErrorNumber, e.Line, e.Column, e.ErrorText ); + Console.WriteLine( " {0}: Line {1}: {2}", e.ErrorNumber, e.Line, e.ErrorText ); Utility.PopColor(); } @@ -487,7 +487,7 @@ namespace Server Utility.PushColor( ConsoleColor.DarkRed ); foreach( CompilerError e in list ) - Console.WriteLine( " {0}: Line {1}: {3}", e.ErrorNumber, e.Line, e.Column, e.ErrorText ); + Console.WriteLine( " {0}: Line {1}: {2}", e.ErrorNumber, e.Line, e.ErrorText ); Utility.PopColor(); } diff --git a/Server/Timer.cs b/Server/Timer.cs index 07bd95f77..1b622d032 100644 --- a/Server/Timer.cs +++ b/Server/Timer.cs @@ -410,7 +410,7 @@ namespace Server get{ return true; } } - public virtual void RegCreation() + public void RegCreation() { TimerProfile prof = GetProfile();