Skip to content

Commit

Permalink
Merge branch 'modernuo:main' into Utility-Partial
Browse files Browse the repository at this point in the history
  • Loading branch information
DerekGooding authored Sep 16, 2024
2 parents c3fdf91 + 465d3c8 commit c9ef9e4
Show file tree
Hide file tree
Showing 21 changed files with 564 additions and 361 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ dotnet_style_qualification_for_field=false:suggestion
dotnet_style_qualification_for_method=false:suggestion
dotnet_style_qualification_for_property=false:suggestion
dotnet_style_require_accessibility_modifiers=for_non_interface_members:suggestion
# Added by Derek
dotnet_diagnostic.IDE0022.severity = none # expression bodies
dotnet_diagnostic.IDE0130.severity = none # namespace not matching file location
dotnet_diagnostic.IDE0290.severity = none # primary constructors
dotnet_diagnostic.IDE1006.severity = none # naming violations with "_"
dotnet_diagnostic.IDE0038.severity = none # pattern matching
dotnet_diagnostic.IDE0008.severity = none # using var
# probably should be ruled var or not var across the board but it's just suggestions anyway.
# Leave this here in case we want to turn it on in the future
# csharp_style_var_for_built_in_types = true:suggestion # suggest using var (implied) variables
# csharp_style_var_when_type_is_apparent = true:suggestion # suggest using var (implied) variables
# csharp_style_var_elsewhere = true:suggestion # suggest using var (implied) variables

# ReSharper properties
resharper_apply_auto_detected_rules=false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public Mobile this[int index]
public Serial Serial { get; }
public void Deserialize(IGenericReader reader) => throw new NotImplementedException();

public byte SerializedThread { get; set; }
public int SerializedPosition { get; set; }
public int SerializedLength { get; set; }

public void Serialize(IGenericWriter writer) => throw new NotImplementedException();

public bool Deleted { get; }
Expand Down
4 changes: 4 additions & 0 deletions Projects/Server/Guild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ protected BaseGuild()
[CommandProperty(AccessLevel.GameMaster, readOnly: true)]
public DateTime Created { get; set; } = Core.Now;

public byte SerializedThread { get; set; }
public int SerializedPosition { get; set; }
public int SerializedLength { get; set; }

public abstract void Serialize(IGenericWriter writer);

public abstract void Deserialize(IGenericReader reader);
Expand Down
4 changes: 4 additions & 0 deletions Projects/Server/IEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ public void Deserialize(IGenericReader reader)
Timer.StartTimer(Delete);
}

public byte SerializedThread { get; set; }
public int SerializedPosition { get; set; }
public int SerializedLength { get; set; }

public void Serialize(IGenericWriter writer)
{
}
Expand Down
4 changes: 4 additions & 0 deletions Projects/Server/Items/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,10 @@ public virtual void GetProperties(IPropertyList list)
[CommandProperty(AccessLevel.Counselor)]
public Serial Serial { get; }

public byte SerializedThread { get; set; }
public int SerializedPosition { get; set; }
public int SerializedLength { get; set; }

public virtual void Serialize(IGenericWriter writer)
{
writer.Write(9); // version
Expand Down
4 changes: 4 additions & 0 deletions Projects/Server/Mobiles/Mobile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2277,6 +2277,10 @@ public virtual void GetProperties(IPropertyList list)
[CommandProperty(AccessLevel.Counselor)]
public Serial Serial { get; }

public byte SerializedThread { get; set; }
public int SerializedPosition { get; set; }
public int SerializedLength { get; set; }

public virtual void Serialize(IGenericWriter writer)
{
writer.Write(36); // version
Expand Down
12 changes: 2 additions & 10 deletions Projects/Server/Serialization/AdhocPersistence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public static void SerializeAndSnapshot(
{
var fullPath = PathUtility.GetFullPath(filePath, Core.BaseDirectory);
PathUtility.EnsureDirectory(Path.GetDirectoryName(fullPath));
ConcurrentQueue<Type> types = [];
var writer = new MemoryMapFileWriter(new FileStream(filePath, FileMode.Create), sizeHint, types);
HashSet<Type> typesSet = [];
var writer = new MemoryMapFileWriter(new FileStream(filePath, FileMode.Create), sizeHint, typesSet);
serializer(writer);

Task.Run(
Expand All @@ -67,14 +67,6 @@ public static void SerializeAndSnapshot(
writer.Dispose();
fs.Dispose();
HashSet<Type> typesSet = [];
// Dedupe the queue.
foreach (var type in types)
{
typesSet.Add(type);
}
Persistence.WriteSerializedTypesSnapshot(Path.GetDirectoryName(fullPath), typesSet);
},
Core.ClosingTokenSource.Token
Expand Down
109 changes: 109 additions & 0 deletions Projects/Server/Serialization/BinaryFileReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2024 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: BinaryFileReader.cs *
* *
* 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 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/

using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Runtime.CompilerServices;
using System.Text;

namespace Server;

public sealed unsafe class BinaryFileReader : IDisposable, IGenericReader
{
private readonly bool _usePrefixes;
private readonly MemoryMappedFile _mmf;
private readonly MemoryMappedViewStream _accessor;
private readonly UnmanagedDataReader _reader;

public BinaryFileReader(string path, bool usePrefixes = true, Encoding encoding = null)
{
_usePrefixes = usePrefixes;
var fi = new FileInfo(path);

if (fi.Length > 0)
{
_mmf = MemoryMappedFile.CreateFromFile(path, FileMode.Open);
_accessor = _mmf.CreateViewStream();
byte* ptr = null;
_accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr);
_reader = new UnmanagedDataReader(ptr, _accessor.Length, encoding: encoding);
}
else
{
_reader = new UnmanagedDataReader(null, 0, encoding: encoding);
}
}

public long Position => _reader.Position;

public void Dispose()
{
_accessor?.SafeMemoryMappedViewHandle.ReleasePointer();
_accessor?.Dispose();
_mmf?.Dispose();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadString(bool intern = false) => _usePrefixes ? _reader.ReadString(intern) : _reader.ReadStringRaw(intern);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadStringRaw(bool intern = false) => _reader.ReadStringRaw(intern);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long ReadLong() => _reader.ReadLong();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong ReadULong() => _reader.ReadULong();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int ReadInt() => _reader.ReadInt();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint ReadUInt() => _reader.ReadUInt();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public short ReadShort() => _reader.ReadShort();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ushort ReadUShort() => _reader.ReadUShort();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double ReadDouble() => _reader.ReadDouble();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float ReadFloat() => _reader.ReadFloat();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte ReadByte() => _reader.ReadByte();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public sbyte ReadSByte() => _reader.ReadSByte();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool ReadBool() => _reader.ReadBool();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Serial ReadSerial() => _reader.ReadSerial();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Type ReadType() => _reader.ReadType();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Read(Span<byte> buffer) => _reader.Read(buffer);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long Seek(long offset, SeekOrigin origin) => _reader.Seek(offset, origin);
}
8 changes: 1 addition & 7 deletions Projects/Server/Serialization/BufferWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,7 @@ public void Resize(int size)
_buffer = newBuffer;
}

public virtual void Flush()
{
// Need to avoid buffer.Length = 2, buffer * 2 is 4, but we need 8 or 16bytes, causing an exception.
// The least we need is 16bytes + Index, but we use BufferSize since it should always be big enough for a single
// non-dynamic field.
Resize(Math.Max(BufferSize, _buffer.Length * 2));
}
public virtual void Flush() => Resize(Math.Clamp(_buffer.Length * 2, BufferSize, _buffer.Length + 1024 * 1024 * 64));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void FlushIfNeeded(int amount)
Expand Down
Loading

0 comments on commit c9ef9e4

Please sign in to comment.