fix(core): Fixes BufferWriter Seek and removes World.SaveBuffers (#359)

- [X] Fixes an issue in BufferWriter where if SeekOrigin.End is used, it will yield the wrong index.
- [X] Changes BinaryFileWriter to dispose pattern
- [X] Removes World.SaveBuffers. They were dangerous and should be done in-line while the world is loading, even if it is slower
- [X] Fixes BufferReader Seek too
- [X] Changes BufferWriter to use uninitialized arrays to speed up resizing
- [X] Changes World loading so it doesn't buffer the whole file, even if it makes things slower. It lowers allocations/fragmentation over all which is good.
- [X] Changes World Loading to use uninitialized arrays and directly saves them to the SaveBuffer which eliminates having to open/load the files _again_ which is dangerous. Also looping through items while the world is running is dangerous since async references can and will cause exceptions.

Bumps release version
This commit is contained in:
Kamron Batman 2020-12-24 14:21:17 -08:00 committed by GitHub
parent 4ddb3de026
commit c2143584bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 178 additions and 134 deletions

View file

@ -13,36 +13,38 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.IO;
namespace Server
{
public class BinaryFileWriter : BufferWriter
public class BinaryFileWriter : BufferWriter, IDisposable
{
private readonly Stream m_File;
private long m_Position;
private readonly Stream _file;
private long _position;
public BinaryFileWriter(string filename, bool prefixStr) : base(prefixStr) =>
m_File = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None);
public BinaryFileWriter(string filename, bool prefixStr) :
this(new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None), prefixStr)
{}
public BinaryFileWriter(Stream stream, bool prefixStr) : base(prefixStr)
{
m_File = stream;
m_Position = m_File.Position;
_file = stream;
_position = _file.Position;
}
public override long Position => m_Position + Index;
public override long Position => _position + Index;
protected override int BufferSize => 512;
protected override int BufferSize => 81920;
public override void Flush()
{
if (Index > 0)
{
m_Position += Index;
_position += Index;
m_File.Write(Buffer, 0, (int)Index);
_file.Write(Buffer, 0, (int)Index);
Index = 0;
}
}
@ -54,14 +56,19 @@ namespace Server
Flush();
}
m_File.Close();
_file.Close();
}
public override long Seek(long offset, SeekOrigin origin)
{
Flush();
return m_Position = m_File.Seek(offset, origin);
return _position = _file.Seek(offset, origin);
}
public void Dispose()
{
Close();
}
}
}