From ebbb3ab2afc92fe7ba723699c7bdd5bfeb589df8 Mon Sep 17 00:00:00 2001 From: mark Date: Fri, 14 Sep 2007 07:44:17 +0000 Subject: [PATCH] a very interesting change --- Server/NativeReader.cs | 68 +++++++++++++++++++++++++++++++++++++++ Server/TileMatrix.cs | 53 ++---------------------------- Server/TileMatrixPatch.cs | 53 ++---------------------------- 3 files changed, 72 insertions(+), 102 deletions(-) create mode 100644 Server/NativeReader.cs diff --git a/Server/NativeReader.cs b/Server/NativeReader.cs new file mode 100644 index 000000000..9954db6d1 --- /dev/null +++ b/Server/NativeReader.cs @@ -0,0 +1,68 @@ +/*************************************************************************** + * NativeReader.cs + * ------------------- + * begin : May 1, 2002 + * copyright : (C) The RunUO Software Team + * email : info@runuo.com + * + * $Id$ + * + ***************************************************************************/ + +/*************************************************************************** + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + ***************************************************************************/ + +using System; +using System.Runtime.InteropServices; + +namespace Server { + public class NativeReader { + + private static readonly INativeReader m_NativeReader; + + static NativeReader() { + if ( Core.Unix ) + m_NativeReader = new NativeReaderUnix(); + else + m_NativeReader = new NativeReaderWin32(); + } + + public static unsafe void Read( IntPtr ptr, void *buffer, int length ) { + m_NativeReader.Read( ptr, buffer, length ); + } + } + + public interface INativeReader { + unsafe void Read( IntPtr ptr, void *buffer, int length ); + } + + public sealed class NativeReaderWin32 : INativeReader { + [DllImport( "kernel32" )] + private unsafe static extern int _lread( IntPtr hFile, void *lpBuffer, int wBytes ); + + public NativeReaderWin32() { + } + + public unsafe void Read( IntPtr ptr, void *buffer, int length ) { + _lread( ptr, buffer, length ); + } + } + + public sealed class NativeReaderUnix : INativeReader { + [DllImport( "libc" )] + private 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 ); + } + } +} \ No newline at end of file diff --git a/Server/TileMatrix.cs b/Server/TileMatrix.cs index 97b03890e..291182feb 100644 --- a/Server/TileMatrix.cs +++ b/Server/TileMatrix.cs @@ -50,11 +50,6 @@ namespace Server private int[][] m_StaticPatches; private int[][] m_LandPatches; -#if !MONO - [DllImport( "Kernel32" )] - private unsafe static extern int _lread( IntPtr hFile, void *lpBuffer, int wBytes ); -#endif - public Map Owner { get @@ -376,10 +371,6 @@ namespace Server private static TileList[][] m_Lists; -#if MONO - private static byte[] m_Buffer; -#endif - private static StaticTile[] m_TileBuffer = new StaticTile[128]; private unsafe Tile[][][] ReadStaticBlock( int x, int y ) @@ -408,27 +399,7 @@ namespace Server fixed ( StaticTile *pTiles = staTiles ) { -#if !MONO - _lread( m_Statics.SafeFileHandle.DangerousGetHandle(), pTiles, length ); -#else - if ( m_Buffer == null || length > m_Buffer.Length ) - m_Buffer = new byte[length]; - - m_Statics.Read( m_Buffer, 0, length ); - - fixed ( byte *pbBuffer = m_Buffer ) - { - StaticTile *pCopyBuffer = (StaticTile *)pbBuffer; - StaticTile *pCopyEnd = pCopyBuffer + count; - StaticTile *pCopyCur = pTiles; - - while ( pCopyBuffer < pCopyEnd ) { - *pCopyCur = *pCopyBuffer; - pCopyCur = pCopyCur + 1; - pCopyBuffer = pCopyBuffer + 1; - } - } -#endif + NativeReader.Read( m_Statics.SafeFileHandle.DangerousGetHandle(), pTiles, length ); if ( m_Lists == null ) { @@ -498,27 +469,7 @@ namespace Server fixed ( Tile *pTiles = tiles ) { -#if !MONO - _lread( m_Map.SafeFileHandle.DangerousGetHandle(), pTiles, 192 ); -#else - if ( m_Buffer == null || 192 > m_Buffer.Length ) - m_Buffer = new byte[192]; - - m_Map.Read( m_Buffer, 0, 192 ); - - fixed ( byte *pbBuffer = m_Buffer ) - { - Tile *pBuffer = (Tile *)pbBuffer; - Tile *pEnd = pBuffer + 64; - Tile *pCur = pTiles; - - while ( pBuffer < pEnd ) { - *pCur = *pBuffer; - pCur = pCur + 1; - pBuffer = pBuffer + 1; - } - } -#endif + NativeReader.Read( m_Map.SafeFileHandle.DangerousGetHandle(), pTiles, 192 ); } return tiles; diff --git a/Server/TileMatrixPatch.cs b/Server/TileMatrixPatch.cs index 39e516d75..e7dc58ff5 100644 --- a/Server/TileMatrixPatch.cs +++ b/Server/TileMatrixPatch.cs @@ -42,11 +42,6 @@ namespace Server } } -#if !MONO - [DllImport( "Kernel32" )] - private unsafe static extern int _lread( IntPtr hFile, void *lpBuffer, int wBytes ); -#endif - public int LandBlocks { get @@ -104,27 +99,7 @@ namespace Server fixed ( Tile *pTiles = tiles ) { -#if !MONO - _lread( fsData.SafeFileHandle.DangerousGetHandle(), pTiles, 192 ); -#else - if ( m_Buffer == null || 192 > m_Buffer.Length ) - m_Buffer = new byte[192]; - - fsData.Read( m_Buffer, 0, 192 ); - - fixed ( byte *pbBuffer = m_Buffer ) - { - Tile *pBuffer = (Tile *)pbBuffer; - Tile *pEnd = pBuffer + 64; - Tile *pCur = pTiles; - - while ( pBuffer < pEnd ) { - *pCur = *pBuffer; - pCur = pCur + 1; - pBuffer = pBuffer + 1; - } - } -#endif + NativeReader.Read( fsData.SafeFileHandle.DangerousGetHandle(), pTiles, 192 ); } matrix.SetLandBlock( x, y, tiles ); @@ -137,10 +112,6 @@ namespace Server } } -#if MONO - private static byte[] m_Buffer; -#endif - private static StaticTile[] m_TileBuffer = new StaticTile[128]; private unsafe int PatchStatics( TileMatrix matrix, string dataPath, string indexPath, string lookupPath ) @@ -193,27 +164,7 @@ namespace Server fixed ( StaticTile *pTiles = staTiles ) { -#if !MONO - _lread( fsData.SafeFileHandle.DangerousGetHandle(), pTiles, length ); -#else - if ( m_Buffer == null || length > m_Buffer.Length ) - m_Buffer = new byte[length]; - - fsData.Read( m_Buffer, 0, length ); - - fixed ( byte *pbBuffer = m_Buffer ) - { - StaticTile *pCopyBuffer = (StaticTile *)pbBuffer; - StaticTile *pCopyEnd = pCopyBuffer + tileCount; - StaticTile *pCopyCur = pTiles; - - while ( pCopyBuffer < pCopyEnd ) { - *pCopyCur = *pCopyBuffer; - pCopyCur = pCopyCur + 1; - pCopyBuffer = pCopyBuffer + 1; - } - } -#endif + NativeReader.Read( fsData.SafeFileHandle.DangerousGetHandle(), pTiles, length ); StaticTile *pCur = pTiles, pEnd = pTiles + tileCount;