fix: Fixes file reading. (#958)

.NET 6 introduced a breaking change where lseek and sys-calls are not used for reading and file streams. This effectively broke out native reader. Since the file reading is fairly optimized, and honestly we don't need it to be "fast" in this case, I have removed the native reader.

https://devblogs.microsoft.com/dotnet/file-io-improvements-in-dotnet-6/
This commit is contained in:
Kamron Batman 2022-03-11 01:45:03 -08:00 committed by GitHub
parent bdee7bc671
commit 16c4268b54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 81 deletions

View file

@ -1,62 +0,0 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
namespace Server
{
public static class NativeReader
{
private static readonly INativeReader m_NativeReader;
static NativeReader() => m_NativeReader = Core.Unix ? new NativeReaderUnix() : new NativeReaderWin32();
public static unsafe int Read(FileStream source, void* buffer, int length) => Read(source, buffer, 0, length);
public static unsafe int Read(FileStream source, void* buffer, int bufferIndex, int length) =>
m_NativeReader.Read(source, buffer, bufferIndex, length);
}
public interface INativeReader
{
unsafe int Read(FileStream source, void* buffer, int bufferIndex, int length);
}
public sealed class NativeReaderWin32 : INativeReader
{
internal class UnsafeNativeMethods
{
[DllImport("kernel32")]
internal static extern unsafe bool ReadFile(IntPtr hFile, void* lpBuffer, uint nNumberOfBytesToRead, ref uint lpNumberOfBytesRead, NativeOverlapped* lpOverlapped);
}
public unsafe int Read(FileStream source, void* buffer, int bufferIndex, int length) => InternalRead(source, buffer, bufferIndex, length);
internal static unsafe int InternalRead(FileStream source, void* buffer, int bufferIndex, int length)
{
var byteCount = 0U;
if (UnsafeNativeMethods.ReadFile(source.SafeFileHandle!.DangerousGetHandle(), (byte*)buffer + bufferIndex, (uint)length, ref byteCount, null))
{
return (int)byteCount;
}
return -1;
}
}
public sealed class NativeReaderUnix : INativeReader
{
internal class UnsafeNativeMethods
{
[DllImport("libc")]
internal static extern unsafe int read(IntPtr ptr, void* buffer, int length);
}
public unsafe int Read(FileStream source, void* buffer, int bufferIndex, int length) =>
InternalRead(source, buffer, bufferIndex, length);
internal unsafe int InternalRead(FileStream source, void* buffer, int bufferIndex, int length) =>
UnsafeNativeMethods.read(source.SafeFileHandle!.DangerousGetHandle(), (byte*)buffer + bufferIndex, length);
}
}

View file

@ -71,9 +71,9 @@ namespace Server
if (fileIndex != 0x7F)
{
fileIndex = Pre6000ClientSupport && mapID == 1 ? 0 : fileIndex;
var mapFileIndex = Pre6000ClientSupport && mapID == 1 ? 0 : fileIndex;
var mapPath = Core.FindDataFile($"map{fileIndex}.mul", false);
var mapPath = Core.FindDataFile($"map{mapFileIndex}.mul", false);
if (mapPath != null)
{
@ -81,7 +81,7 @@ namespace Server
}
else
{
mapPath = Core.FindDataFile($"map{fileIndex}LegacyMUL.uop", false);
mapPath = Core.FindDataFile($"map{mapFileIndex}LegacyMUL.uop", false);
if (mapPath != null)
{
@ -90,11 +90,11 @@ namespace Server
}
else
{
logger.Warning($"map{fileIndex}.mul was not found.");
logger.Warning($"map{mapFileIndex}.mul was not found.");
}
}
var indexPath = Core.FindDataFile($"staidx{fileIndex}.mul", false);
var indexPath = Core.FindDataFile($"staidx{mapFileIndex}.mul", false);
if (indexPath != null)
{
@ -103,10 +103,10 @@ namespace Server
}
else
{
logger.Warning($"staidx{fileIndex}.mul was not found.");
logger.Warning($"staidx{mapFileIndex}.mul was not found.");
}
var staticsPath = Core.FindDataFile($"statics{fileIndex}.mul", false);
var staticsPath = Core.FindDataFile($"statics{mapFileIndex}.mul", false);
if (staticsPath != null)
{
@ -137,7 +137,7 @@ namespace Server
_staticPatches = new int[BlockWidth][];
_landPatches = new int[BlockWidth][];
Patch = new TileMatrixPatch(this, mapID);
Patch = new TileMatrixPatch(this, fileIndex);
}
public StaticTile[][][] EmptyStaticBlock => _emptyStaticBlock;
@ -365,11 +365,11 @@ namespace Server
m_TileBuffer = new StaticTile[count];
}
var staTiles = m_TileBuffer; //new StaticTile[tileCount];
var staTiles = m_TileBuffer;
fixed (StaticTile* pTiles = staTiles)
{
NativeReader.Read(DataStream, pTiles, length);
DataStream.Read(new Span<byte>(pTiles, length));
if (m_Lists == null)
{
@ -455,7 +455,7 @@ namespace Server
fixed (LandTile* pTiles = tiles)
{
NativeReader.Read(MapStream, pTiles, 192);
MapStream.Read(new Span<byte>(pTiles, 192));
}
return tiles;

View file

@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Server
{
@ -63,10 +64,9 @@ namespace Server
fsData.Seek(4, SeekOrigin.Current);
var tiles = new LandTile[64];
fixed (LandTile* pTiles = tiles)
{
NativeReader.Read(fsData, pTiles, 192);
fsData.Read(new Span<byte>(pTiles, 192));
}
matrix.SetLandBlock(x, y, tiles);
@ -81,8 +81,8 @@ namespace Server
using var fsData = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.Read);
using var fsIndex = new FileStream(indexPath, FileMode.Open, FileAccess.Read, FileShare.Read);
using var fsLookup = new FileStream(lookupPath, FileMode.Open, FileAccess.Read, FileShare.Read);
var indexReader = new BinaryReader(fsIndex);
var lookupReader = new BinaryReader(fsLookup);
using var indexReader = new BinaryReader(fsIndex);
using var lookupReader = new BinaryReader(fsLookup);
var count = (int)(indexReader.BaseStream.Length / 4);
@ -127,7 +127,7 @@ namespace Server
fixed (StaticTile* pTiles = staTiles)
{
NativeReader.Read(fsData, pTiles, length);
fsData.Read(new Span<byte>(pTiles, length));
StaticTile* pCur = pTiles, pEnd = pTiles + tileCount;
@ -153,9 +153,6 @@ namespace Server
}
}
indexReader.Close();
lookupReader.Close();
return count;
}
}