Skip to content

Commit

Permalink
Start putting back st.db
Browse files Browse the repository at this point in the history
  • Loading branch information
kamronbatman committed Jun 22, 2024
1 parent 5c495a2 commit e74ed6e
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 13 deletions.
40 changes: 37 additions & 3 deletions Projects/Server/Serialization/BinaryFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*************************************************************************/

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
Expand All @@ -27,16 +28,19 @@ public class BinaryFileReader : IGenericReader, IDisposable
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(BinaryFileReader));

private Dictionary<ulong, string> _typesDb;
private BinaryReader _reader;
private Encoding _encoding;

public BinaryFileReader(BinaryReader br, Encoding encoding = null)
public BinaryFileReader(BinaryReader br, Dictionary<ulong, string> typesDb = null, Encoding encoding = null)
{
_reader = br;
_encoding = encoding ?? TextEncoding.UTF8;
_typesDb = typesDb;
}

public BinaryFileReader(Stream stream, Encoding encoding = null) : this(new BinaryReader(stream), encoding)
public BinaryFileReader(Stream stream, Dictionary<ulong, string> typesDb = null, Encoding encoding = null)
: this(new BinaryReader(stream), typesDb, encoding)
{
}

Expand Down Expand Up @@ -115,14 +119,44 @@ public Type ReadTypeByHash()
var hash = ReadULong();
var t = AssemblyHandler.FindTypeByHash(hash);

if (t == null)
if (t != null)
{
return t;
}

if (_typesDb == null)
{
logger.Error(
new Exception($"The file SerializedTypes.db was not loaded. Type hash '{hash}' could not be found."),
"Invalid {Hash} at position {Position}",
hash,
Position
);

return null;
}

if (!_typesDb.TryGetValue(hash, out var typeName))
{
logger.Error(
new Exception($"Type hash '{hash}' is not present in the serialized types database."),
"Invalid type hash {Hash} at position {Position}",
hash,
Position
);

return null;
}

t = AssemblyHandler.FindTypeByFullName(typeName, false);

if (t == null)
{
logger.Error(
new Exception($"Type '{typeName}' was not found."),
"Type {Type} was not found.",
typeName
);
}

return t;
Expand Down
8 changes: 4 additions & 4 deletions Projects/Server/Serialization/BinaryFileWriter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2024 - ModernUO Development Team *
* Copyright 2019-2023 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: BinaryFileWriter.cs *
* *
Expand All @@ -24,11 +24,11 @@ public class BinaryFileWriter : BufferWriter, IDisposable
private readonly Stream _file;
private long _position;

public BinaryFileWriter(string filename, bool prefixStr) :
this(new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None), prefixStr)
public BinaryFileWriter(string filename, bool prefixStr, ConcurrentQueue<Type> types = null) :
this(new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None), prefixStr, types)
{}

public BinaryFileWriter(Stream stream, bool prefixStr, ConcurrentQueue<Type> types = null) : base(prefixStr)
public BinaryFileWriter(Stream stream, bool prefixStr, ConcurrentQueue<Type> types = null) : base(prefixStr, types)

Check failure on line 31 in Projects/Server/Serialization/BinaryFileWriter.cs

View workflow job for this annotation

GitHub Actions / Build (MacOS 14)

Argument 1: cannot convert from 'bool' to 'byte[]'

Check failure on line 31 in Projects/Server/Serialization/BinaryFileWriter.cs

View workflow job for this annotation

GitHub Actions / Build (MacOS 14)

Argument 2: cannot convert from 'System.Collections.Concurrent.ConcurrentQueue<System.Type>' to 'bool'

Check failure on line 31 in Projects/Server/Serialization/BinaryFileWriter.cs

View workflow job for this annotation

GitHub Actions / Build (MacOS 14)

Argument 1: cannot convert from 'bool' to 'byte[]'

Check failure on line 31 in Projects/Server/Serialization/BinaryFileWriter.cs

View workflow job for this annotation

GitHub Actions / Build (MacOS 14)

Argument 2: cannot convert from 'System.Collections.Concurrent.ConcurrentQueue<System.Type>' to 'bool'

Check failure on line 31 in Projects/Server/Serialization/BinaryFileWriter.cs

View workflow job for this annotation

GitHub Actions / Build (MacOS 13)

Argument 1: cannot convert from 'bool' to 'byte[]'

Check failure on line 31 in Projects/Server/Serialization/BinaryFileWriter.cs

View workflow job for this annotation

GitHub Actions / Build (MacOS 13)

Argument 2: cannot convert from 'System.Collections.Concurrent.ConcurrentQueue<System.Type>' to 'bool'

Check failure on line 31 in Projects/Server/Serialization/BinaryFileWriter.cs

View workflow job for this annotation

GitHub Actions / Build (MacOS 13)

Argument 1: cannot convert from 'bool' to 'byte[]'

Check failure on line 31 in Projects/Server/Serialization/BinaryFileWriter.cs

View workflow job for this annotation

GitHub Actions / Build (MacOS 13)

Argument 2: cannot convert from 'System.Collections.Concurrent.ConcurrentQueue<System.Type>' to 'bool'
{
_file = stream;
_position = _file.Position;
Expand Down
48 changes: 42 additions & 6 deletions Projects/Server/Serialization/BufferReader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2024 - ModernUO Development Team *
* Copyright 2019-2023 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: BufferReader.cs *
* *
Expand All @@ -15,6 +15,7 @@

using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
Expand All @@ -28,23 +29,30 @@ public class BufferReader : IGenericReader
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(BufferReader));

private readonly Encoding _encoding;
private Dictionary<ulong, string> _typesDb;
private Encoding _encoding;
private byte[] _buffer;
private int _position;

public long Position => _position;
public long BufferSize => _buffer.Length;

public BufferReader(byte[] buffer, Encoding encoding = null)
public BufferReader(byte[] buffer, Dictionary<ulong, string> typesDb = null, Encoding encoding = null)
{
_buffer = buffer;
_encoding = encoding ?? TextEncoding.UTF8;
_typesDb = typesDb;
}

public BufferReader(byte[] buffer, DateTime lastSerialized) : this(buffer) => LastSerialized = lastSerialized;
public BufferReader(byte[] buffer, DateTime lastSerialized, Dictionary<ulong, string> typesDb = null) : this(buffer)
{
LastSerialized = lastSerialized;
_typesDb = typesDb;
}

public void Reset(byte[] newBuffer)
public void Reset(byte[] newBuffer, out byte[] oldBuffer)
{
oldBuffer = _buffer;
_buffer = newBuffer;
_position = 0;
}
Expand Down Expand Up @@ -156,7 +164,24 @@ public Type ReadTypeByHash()
var hash = ReadULong();
var t = AssemblyHandler.FindTypeByHash(hash);

if (t == null)
if (t != null)
{
return t;
}

if (_typesDb == null)
{
logger.Error(
new Exception($"The file SerializedTypes.db was not loaded. Type hash '{hash}' could not be found."),
"Invalid {Hash} at position {Position}",
hash,
Position
);

return null;
}

if (!_typesDb.TryGetValue(hash, out var typeName))
{
logger.Error(
new Exception($"Type hash '{hash}' is not present in the serialized types database."),
Expand All @@ -168,6 +193,17 @@ public Type ReadTypeByHash()
return null;
}

t = AssemblyHandler.FindTypeByFullName(typeName, false);

if (t == null)
{
logger.Error(
new Exception($"Type '{typeName}' was not found."),
"Type {Type} was not found.",
typeName
);
}

return t;
}

Expand Down

0 comments on commit e74ed6e

Please sign in to comment.