Serialization interfaces (#62)
This commit is contained in:
parent
5e2be2d7b6
commit
5c8821b11c
2406 changed files with 10100 additions and 9350 deletions
612
Projects/Server/Serialization/AsyncWriter.cs
Normal file
612
Projects/Server/Serialization/AsyncWriter.cs
Normal file
|
|
@ -0,0 +1,612 @@
|
|||
/***************************************************************************
|
||||
* Serialization.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.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using Server.Guilds;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
public sealed class AsyncWriter : IGenericWriter
|
||||
{
|
||||
private int BufferSize;
|
||||
private BinaryWriter m_Bin;
|
||||
private bool m_Closed;
|
||||
private FileStream m_File;
|
||||
|
||||
private long m_LastPos, m_CurPos;
|
||||
|
||||
private MemoryStream m_Mem;
|
||||
private Thread m_WorkerThread;
|
||||
|
||||
private Queue<MemoryStream> m_WriteQueue;
|
||||
private bool PrefixStrings;
|
||||
|
||||
public AsyncWriter(string filename, bool prefix)
|
||||
: this(filename, 1048576, prefix) //1 mb buffer
|
||||
{
|
||||
}
|
||||
|
||||
public AsyncWriter(string filename, int buffSize, bool prefix)
|
||||
{
|
||||
PrefixStrings = prefix;
|
||||
m_Closed = false;
|
||||
m_WriteQueue = new Queue<MemoryStream>();
|
||||
BufferSize = buffSize;
|
||||
|
||||
m_File = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None);
|
||||
m_Mem = new MemoryStream(BufferSize + 1024);
|
||||
m_Bin = new BinaryWriter(m_Mem, Utility.UTF8WithEncoding);
|
||||
}
|
||||
|
||||
public static int ThreadCount{ get; private set; }
|
||||
|
||||
public MemoryStream MemStream
|
||||
{
|
||||
get => m_Mem;
|
||||
set
|
||||
{
|
||||
if (m_Mem.Length > 0)
|
||||
Enqueue(m_Mem);
|
||||
|
||||
m_Mem = value;
|
||||
m_Bin = new BinaryWriter(m_Mem, Utility.UTF8WithEncoding);
|
||||
m_LastPos = 0;
|
||||
m_CurPos = m_Mem.Length;
|
||||
m_Mem.Seek(0, SeekOrigin.End);
|
||||
}
|
||||
}
|
||||
|
||||
public long Position => m_CurPos;
|
||||
|
||||
private void Enqueue(MemoryStream mem)
|
||||
{
|
||||
lock (m_WriteQueue)
|
||||
{
|
||||
m_WriteQueue.Enqueue(mem);
|
||||
}
|
||||
|
||||
if (m_WorkerThread.IsAlive != true)
|
||||
{
|
||||
m_WorkerThread = new Thread(new WorkerThread(this).Worker) { Priority = ThreadPriority.BelowNormal };
|
||||
m_WorkerThread.Start();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnWrite()
|
||||
{
|
||||
long curlen = m_Mem.Length;
|
||||
m_CurPos += curlen - m_LastPos;
|
||||
m_LastPos = curlen;
|
||||
if (curlen >= BufferSize)
|
||||
{
|
||||
Enqueue(m_Mem);
|
||||
m_Mem = new MemoryStream(BufferSize + 1024);
|
||||
m_Bin = new BinaryWriter(m_Mem, Utility.UTF8WithEncoding);
|
||||
m_LastPos = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
Enqueue(m_Mem);
|
||||
m_Closed = true;
|
||||
}
|
||||
|
||||
public void Write(IPAddress value)
|
||||
{
|
||||
m_Bin.Write(Utility.GetLongAddressValue(value));
|
||||
OnWrite();
|
||||
}
|
||||
|
||||
public void Write(string value)
|
||||
{
|
||||
if (PrefixStrings)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
m_Bin.Write((byte)0);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Bin.Write((byte)1);
|
||||
m_Bin.Write(value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Bin.Write(value);
|
||||
}
|
||||
|
||||
OnWrite();
|
||||
}
|
||||
|
||||
public void WriteDeltaTime(DateTime value)
|
||||
{
|
||||
long ticks = value.Ticks;
|
||||
long now = DateTime.UtcNow.Ticks;
|
||||
|
||||
TimeSpan d;
|
||||
|
||||
try
|
||||
{
|
||||
d = new TimeSpan(ticks - now);
|
||||
}
|
||||
catch
|
||||
{
|
||||
d = TimeSpan.MaxValue;
|
||||
}
|
||||
|
||||
Write(d);
|
||||
}
|
||||
|
||||
public void Write(DateTime value)
|
||||
{
|
||||
m_Bin.Write(value.Ticks);
|
||||
OnWrite();
|
||||
}
|
||||
|
||||
public void Write(DateTimeOffset value)
|
||||
{
|
||||
m_Bin.Write(value.Ticks);
|
||||
m_Bin.Write(value.Offset.Ticks);
|
||||
OnWrite();
|
||||
}
|
||||
|
||||
public void Write(TimeSpan value)
|
||||
{
|
||||
m_Bin.Write(value.Ticks);
|
||||
OnWrite();
|
||||
}
|
||||
|
||||
public void Write(decimal value)
|
||||
{
|
||||
m_Bin.Write(value);
|
||||
OnWrite();
|
||||
}
|
||||
|
||||
public void Write(long value)
|
||||
{
|
||||
m_Bin.Write(value);
|
||||
OnWrite();
|
||||
}
|
||||
|
||||
public void Write(ulong value)
|
||||
{
|
||||
m_Bin.Write(value);
|
||||
OnWrite();
|
||||
}
|
||||
|
||||
public void WriteEncodedInt(int value)
|
||||
{
|
||||
uint v = (uint)value;
|
||||
|
||||
while (v >= 0x80)
|
||||
{
|
||||
m_Bin.Write((byte)(v | 0x80));
|
||||
v >>= 7;
|
||||
}
|
||||
|
||||
m_Bin.Write((byte)v);
|
||||
OnWrite();
|
||||
}
|
||||
|
||||
public void Write(int value)
|
||||
{
|
||||
m_Bin.Write(value);
|
||||
OnWrite();
|
||||
}
|
||||
|
||||
public void Write(uint value)
|
||||
{
|
||||
m_Bin.Write(value);
|
||||
OnWrite();
|
||||
}
|
||||
|
||||
public void Write(short value)
|
||||
{
|
||||
m_Bin.Write(value);
|
||||
OnWrite();
|
||||
}
|
||||
|
||||
public void Write(ushort value)
|
||||
{
|
||||
m_Bin.Write(value);
|
||||
OnWrite();
|
||||
}
|
||||
|
||||
public void Write(double value)
|
||||
{
|
||||
m_Bin.Write(value);
|
||||
OnWrite();
|
||||
}
|
||||
|
||||
public void Write(float value)
|
||||
{
|
||||
m_Bin.Write(value);
|
||||
OnWrite();
|
||||
}
|
||||
|
||||
public void Write(char value)
|
||||
{
|
||||
m_Bin.Write(value);
|
||||
OnWrite();
|
||||
}
|
||||
|
||||
public void Write(byte value)
|
||||
{
|
||||
m_Bin.Write(value);
|
||||
OnWrite();
|
||||
}
|
||||
|
||||
public void Write(byte[] value)
|
||||
{
|
||||
Write(value, value.Length);
|
||||
}
|
||||
|
||||
public void Write(byte[] value, int length)
|
||||
{
|
||||
m_Bin.Write(value, 0, length);
|
||||
OnWrite();
|
||||
}
|
||||
public void Write(sbyte value)
|
||||
{
|
||||
m_Bin.Write(value);
|
||||
OnWrite();
|
||||
}
|
||||
|
||||
public void Write(bool value)
|
||||
{
|
||||
m_Bin.Write(value);
|
||||
OnWrite();
|
||||
}
|
||||
|
||||
public void Write(Point3D value)
|
||||
{
|
||||
Write(value.m_X);
|
||||
Write(value.m_Y);
|
||||
Write(value.m_Z);
|
||||
}
|
||||
|
||||
public void Write(Point2D value)
|
||||
{
|
||||
Write(value.m_X);
|
||||
Write(value.m_Y);
|
||||
}
|
||||
|
||||
public void Write(Rectangle2D value)
|
||||
{
|
||||
Write(value.Start);
|
||||
Write(value.End);
|
||||
}
|
||||
|
||||
public void Write(Rectangle3D value)
|
||||
{
|
||||
Write(value.Start);
|
||||
Write(value.End);
|
||||
}
|
||||
|
||||
public void Write(Map value)
|
||||
{
|
||||
if (value != null)
|
||||
Write((byte)value.MapIndex);
|
||||
else
|
||||
Write((byte)0xFF);
|
||||
}
|
||||
|
||||
public void Write(Race value)
|
||||
{
|
||||
if (value != null)
|
||||
Write((byte)value.RaceIndex);
|
||||
else
|
||||
Write((byte)0xFF);
|
||||
}
|
||||
|
||||
public void WriteEntity(IEntity value)
|
||||
{
|
||||
if (value?.Deleted != false)
|
||||
Write(Serial.MinusOne);
|
||||
else
|
||||
Write(value.Serial);
|
||||
}
|
||||
|
||||
public void Write(Item value)
|
||||
{
|
||||
if (value?.Deleted != false)
|
||||
Write(Serial.MinusOne);
|
||||
else
|
||||
Write(value.Serial);
|
||||
}
|
||||
|
||||
public void Write(Mobile value)
|
||||
{
|
||||
if (value?.Deleted != false)
|
||||
Write(Serial.MinusOne);
|
||||
else
|
||||
Write(value.Serial);
|
||||
}
|
||||
|
||||
public void Write(BaseGuild value)
|
||||
{
|
||||
if (value == null)
|
||||
Write(0);
|
||||
else
|
||||
Write(value.Id);
|
||||
}
|
||||
|
||||
public void WriteItem<T>(T value) where T : Item
|
||||
{
|
||||
Write(value);
|
||||
}
|
||||
|
||||
public void WriteMobile<T>(T value) where T : Mobile
|
||||
{
|
||||
Write(value);
|
||||
}
|
||||
|
||||
public void WriteGuild<T>(T value) where T : BaseGuild
|
||||
{
|
||||
Write(value);
|
||||
}
|
||||
|
||||
public void Write(List<Item> list)
|
||||
{
|
||||
Write(list, false);
|
||||
}
|
||||
|
||||
public void Write(List<Item> list, bool tidy)
|
||||
{
|
||||
if (tidy)
|
||||
for (int i = 0; i < list.Count;)
|
||||
if (list[i].Deleted)
|
||||
list.RemoveAt(i);
|
||||
else
|
||||
++i;
|
||||
|
||||
Write(list.Count);
|
||||
|
||||
for (int i = 0; i < list.Count; ++i)
|
||||
Write(list[i]);
|
||||
}
|
||||
|
||||
public void WriteItemList<T>(List<T> list) where T : Item
|
||||
{
|
||||
WriteItemList(list, false);
|
||||
}
|
||||
|
||||
public void WriteItemList<T>(List<T> list, bool tidy) where T : Item
|
||||
{
|
||||
if (tidy)
|
||||
for (int i = 0; i < list.Count;)
|
||||
if (list[i].Deleted)
|
||||
list.RemoveAt(i);
|
||||
else
|
||||
++i;
|
||||
|
||||
Write(list.Count);
|
||||
|
||||
for (int i = 0; i < list.Count; ++i)
|
||||
Write(list[i]);
|
||||
}
|
||||
|
||||
public void Write(HashSet<Item> set)
|
||||
{
|
||||
Write(set, false);
|
||||
}
|
||||
|
||||
public void Write(HashSet<Item> set, bool tidy)
|
||||
{
|
||||
if (tidy) set.RemoveWhere(item => item.Deleted);
|
||||
|
||||
Write(set.Count);
|
||||
|
||||
foreach (Item item in set) Write(item);
|
||||
}
|
||||
|
||||
public void WriteItemSet<T>(HashSet<T> set) where T : Item
|
||||
{
|
||||
WriteItemSet(set, false);
|
||||
}
|
||||
|
||||
public void WriteItemSet<T>(HashSet<T> set, bool tidy) where T : Item
|
||||
{
|
||||
if (tidy) set.RemoveWhere(item => item.Deleted);
|
||||
|
||||
Write(set.Count);
|
||||
|
||||
foreach (T item in set) Write(item);
|
||||
}
|
||||
|
||||
public void Write(List<Mobile> list)
|
||||
{
|
||||
Write(list, false);
|
||||
}
|
||||
|
||||
public void Write(List<Mobile> list, bool tidy)
|
||||
{
|
||||
if (tidy)
|
||||
for (int i = 0; i < list.Count;)
|
||||
if (list[i].Deleted)
|
||||
list.RemoveAt(i);
|
||||
else
|
||||
++i;
|
||||
|
||||
Write(list.Count);
|
||||
|
||||
for (int i = 0; i < list.Count; ++i)
|
||||
Write(list[i]);
|
||||
}
|
||||
|
||||
public void WriteMobileList<T>(List<T> list) where T : Mobile
|
||||
{
|
||||
WriteMobileList(list, false);
|
||||
}
|
||||
|
||||
public void WriteMobileList<T>(List<T> list, bool tidy) where T : Mobile
|
||||
{
|
||||
if (tidy)
|
||||
for (int i = 0; i < list.Count;)
|
||||
if (list[i].Deleted)
|
||||
list.RemoveAt(i);
|
||||
else
|
||||
++i;
|
||||
|
||||
Write(list.Count);
|
||||
|
||||
for (int i = 0; i < list.Count; ++i)
|
||||
Write(list[i]);
|
||||
}
|
||||
|
||||
public void Write(HashSet<Mobile> set)
|
||||
{
|
||||
Write(set, false);
|
||||
}
|
||||
|
||||
public void Write(HashSet<Mobile> set, bool tidy)
|
||||
{
|
||||
if (tidy) set.RemoveWhere(mobile => mobile.Deleted);
|
||||
|
||||
Write(set.Count);
|
||||
|
||||
foreach (Mobile mob in set) Write(mob);
|
||||
}
|
||||
|
||||
public void WriteMobileSet<T>(HashSet<T> set) where T : Mobile
|
||||
{
|
||||
WriteMobileSet(set, false);
|
||||
}
|
||||
|
||||
public void WriteMobileSet<T>(HashSet<T> set, bool tidy) where T : Mobile
|
||||
{
|
||||
if (tidy) set.RemoveWhere(mob => mob.Deleted);
|
||||
|
||||
Write(set.Count);
|
||||
|
||||
foreach (T mob in set) Write(mob);
|
||||
}
|
||||
|
||||
public void Write(List<BaseGuild> list)
|
||||
{
|
||||
Write(list, false);
|
||||
}
|
||||
|
||||
public void Write(List<BaseGuild> list, bool tidy)
|
||||
{
|
||||
if (tidy)
|
||||
for (int i = 0; i < list.Count;)
|
||||
if (list[i].Disbanded)
|
||||
list.RemoveAt(i);
|
||||
else
|
||||
++i;
|
||||
|
||||
Write(list.Count);
|
||||
|
||||
for (int i = 0; i < list.Count; ++i)
|
||||
Write(list[i]);
|
||||
}
|
||||
|
||||
public void WriteGuildList<T>(List<T> list) where T : BaseGuild
|
||||
{
|
||||
WriteGuildList(list, false);
|
||||
}
|
||||
|
||||
public void WriteGuildList<T>(List<T> list, bool tidy) where T : BaseGuild
|
||||
{
|
||||
if (tidy)
|
||||
for (int i = 0; i < list.Count;)
|
||||
if (list[i].Disbanded)
|
||||
list.RemoveAt(i);
|
||||
else
|
||||
++i;
|
||||
|
||||
Write(list.Count);
|
||||
|
||||
for (int i = 0; i < list.Count; ++i)
|
||||
Write(list[i]);
|
||||
}
|
||||
|
||||
public void Write(HashSet<BaseGuild> set)
|
||||
{
|
||||
Write(set, false);
|
||||
}
|
||||
|
||||
public void Write(HashSet<BaseGuild> set, bool tidy)
|
||||
{
|
||||
if (tidy) set.RemoveWhere(guild => guild.Disbanded);
|
||||
|
||||
Write(set.Count);
|
||||
|
||||
foreach (BaseGuild guild in set) Write(guild);
|
||||
}
|
||||
|
||||
public void WriteGuildSet<T>(HashSet<T> set) where T : BaseGuild
|
||||
{
|
||||
WriteGuildSet(set, false);
|
||||
}
|
||||
|
||||
public void WriteGuildSet<T>(HashSet<T> set, bool tidy) where T : BaseGuild
|
||||
{
|
||||
if (tidy) set.RemoveWhere(guild => guild.Disbanded);
|
||||
|
||||
Write(set.Count);
|
||||
|
||||
foreach (T guild in set) Write(guild);
|
||||
}
|
||||
|
||||
private class WorkerThread
|
||||
{
|
||||
private AsyncWriter m_Owner;
|
||||
|
||||
public WorkerThread(AsyncWriter owner) => m_Owner = owner;
|
||||
|
||||
public void Worker()
|
||||
{
|
||||
ThreadCount++;
|
||||
|
||||
int lastCount;
|
||||
|
||||
do
|
||||
{
|
||||
MemoryStream mem = null;
|
||||
|
||||
lock (m_Owner.m_WriteQueue)
|
||||
{
|
||||
if ((lastCount = m_Owner.m_WriteQueue.Count) > 0)
|
||||
mem = m_Owner.m_WriteQueue.Dequeue();
|
||||
}
|
||||
|
||||
if (mem?.Length > 0)
|
||||
mem.WriteTo(m_Owner.m_File);
|
||||
} while (lastCount > 1);
|
||||
|
||||
if (m_Owner.m_Closed)
|
||||
m_Owner.m_File.Close();
|
||||
|
||||
ThreadCount--;
|
||||
|
||||
if (ThreadCount <= 0)
|
||||
World.NotifyDiskWriteComplete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
259
Projects/Server/Serialization/BinaryFileReader.cs
Normal file
259
Projects/Server/Serialization/BinaryFileReader.cs
Normal file
|
|
@ -0,0 +1,259 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using Server.Guilds;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
public sealed class BinaryFileReader : IGenericReader
|
||||
{
|
||||
private BinaryReader m_File;
|
||||
|
||||
public BinaryFileReader(BinaryReader br) => m_File = br;
|
||||
|
||||
public long Position => m_File.BaseStream.Position;
|
||||
|
||||
public void Close()
|
||||
{
|
||||
m_File.Close();
|
||||
}
|
||||
|
||||
public long Seek(long offset, SeekOrigin origin) => m_File.BaseStream.Seek(offset, origin);
|
||||
|
||||
public string ReadString() => ReadByte() != 0 ? m_File.ReadString() : null;
|
||||
|
||||
public DateTime ReadDeltaTime()
|
||||
{
|
||||
long ticks = m_File.ReadInt64();
|
||||
long now = DateTime.UtcNow.Ticks;
|
||||
|
||||
if (ticks > 0 && ticks + now < 0)
|
||||
return DateTime.MaxValue;
|
||||
if (ticks < 0 && ticks + now < 0)
|
||||
return DateTime.MinValue;
|
||||
|
||||
try
|
||||
{
|
||||
return new DateTime(now + ticks);
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (ticks > 0) return DateTime.MaxValue;
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
}
|
||||
|
||||
public IPAddress ReadIPAddress() => new IPAddress(m_File.ReadInt64());
|
||||
|
||||
public int ReadEncodedInt()
|
||||
{
|
||||
int v = 0, shift = 0;
|
||||
byte b;
|
||||
|
||||
do
|
||||
{
|
||||
b = m_File.ReadByte();
|
||||
v |= (b & 0x7F) << shift;
|
||||
shift += 7;
|
||||
} while (b >= 0x80);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
public DateTime ReadDateTime() => new DateTime(m_File.ReadInt64());
|
||||
|
||||
public DateTimeOffset ReadDateTimeOffset()
|
||||
{
|
||||
long ticks = m_File.ReadInt64();
|
||||
TimeSpan offset = new TimeSpan(m_File.ReadInt64());
|
||||
|
||||
return new DateTimeOffset(ticks, offset);
|
||||
}
|
||||
|
||||
public TimeSpan ReadTimeSpan() => new TimeSpan(m_File.ReadInt64());
|
||||
|
||||
public decimal ReadDecimal() => m_File.ReadDecimal();
|
||||
|
||||
public long ReadLong() => m_File.ReadInt64();
|
||||
|
||||
public ulong ReadULong() => m_File.ReadUInt64();
|
||||
|
||||
public int ReadInt() => m_File.ReadInt32();
|
||||
|
||||
public uint ReadUInt() => m_File.ReadUInt32();
|
||||
|
||||
public short ReadShort() => m_File.ReadInt16();
|
||||
|
||||
public ushort ReadUShort() => m_File.ReadUInt16();
|
||||
|
||||
public double ReadDouble() => m_File.ReadDouble();
|
||||
|
||||
public float ReadFloat() => m_File.ReadSingle();
|
||||
|
||||
public char ReadChar() => m_File.ReadChar();
|
||||
|
||||
public byte ReadByte() => m_File.ReadByte();
|
||||
|
||||
public sbyte ReadSByte() => m_File.ReadSByte();
|
||||
|
||||
public bool ReadBool() => m_File.ReadBoolean();
|
||||
|
||||
public Point3D ReadPoint3D() => new Point3D(ReadInt(), ReadInt(), ReadInt());
|
||||
|
||||
public Point2D ReadPoint2D() => new Point2D(ReadInt(), ReadInt());
|
||||
|
||||
public Rectangle2D ReadRect2D() => new Rectangle2D(ReadPoint2D(), ReadPoint2D());
|
||||
|
||||
public Rectangle3D ReadRect3D() => new Rectangle3D(ReadPoint3D(), ReadPoint3D());
|
||||
|
||||
public Map ReadMap() => Map.Maps[ReadByte()];
|
||||
|
||||
public IEntity ReadEntity()
|
||||
{
|
||||
Serial serial = ReadUInt();
|
||||
IEntity entity = World.FindEntity(serial);
|
||||
if (entity == null)
|
||||
return new Entity(serial, new Point3D(0, 0, 0), Map.Internal);
|
||||
return entity;
|
||||
}
|
||||
|
||||
public Item ReadItem() => World.FindItem(ReadUInt());
|
||||
|
||||
public Mobile ReadMobile() => World.FindMobile(ReadUInt());
|
||||
|
||||
public BaseGuild ReadGuild() => BaseGuild.Find(ReadUInt());
|
||||
|
||||
public T ReadItem<T>() where T : Item => ReadItem() as T;
|
||||
|
||||
public T ReadMobile<T>() where T : Mobile => ReadMobile() as T;
|
||||
|
||||
public T ReadGuild<T>() where T : BaseGuild => ReadGuild() as T;
|
||||
|
||||
public List<Item> ReadStrongItemList() => ReadStrongItemList<Item>();
|
||||
|
||||
public List<T> ReadStrongItemList<T>() where T : Item
|
||||
{
|
||||
int count = ReadInt();
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
List<T> list = new List<T>(count);
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
if (ReadItem() is T item)
|
||||
list.Add(item);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
return new List<T>();
|
||||
}
|
||||
|
||||
public HashSet<Item> ReadItemSet() => ReadItemSet<Item>();
|
||||
|
||||
public HashSet<T> ReadItemSet<T>() where T : Item
|
||||
{
|
||||
int count = ReadInt();
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
HashSet<T> set = new HashSet<T>();
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
if (ReadItem() is T item)
|
||||
set.Add(item);
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
return new HashSet<T>();
|
||||
}
|
||||
|
||||
public List<Mobile> ReadStrongMobileList() => ReadStrongMobileList<Mobile>();
|
||||
|
||||
public List<T> ReadStrongMobileList<T>() where T : Mobile
|
||||
{
|
||||
int count = ReadInt();
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
List<T> list = new List<T>(count);
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
if (ReadMobile() is T m)
|
||||
list.Add(m);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
return new List<T>();
|
||||
}
|
||||
|
||||
public HashSet<Mobile> ReadMobileSet() => ReadMobileSet<Mobile>();
|
||||
|
||||
public HashSet<T> ReadMobileSet<T>() where T : Mobile
|
||||
{
|
||||
int count = ReadInt();
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
HashSet<T> set = new HashSet<T>();
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
if (ReadMobile() is T item)
|
||||
set.Add(item);
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
return new HashSet<T>();
|
||||
}
|
||||
|
||||
public List<BaseGuild> ReadStrongGuildList() => ReadStrongGuildList<BaseGuild>();
|
||||
|
||||
public List<T> ReadStrongGuildList<T>() where T : BaseGuild
|
||||
{
|
||||
int count = ReadInt();
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
List<T> list = new List<T>(count);
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
if (ReadGuild() is T g)
|
||||
list.Add(g);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
return new List<T>();
|
||||
}
|
||||
|
||||
public HashSet<BaseGuild> ReadGuildSet() => ReadGuildSet<BaseGuild>();
|
||||
|
||||
public HashSet<T> ReadGuildSet<T>() where T : BaseGuild
|
||||
{
|
||||
int count = ReadInt();
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
HashSet<T> set = new HashSet<T>();
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
if (ReadGuild() is T item)
|
||||
set.Add(item);
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
return new HashSet<T>();
|
||||
}
|
||||
|
||||
public Race ReadRace() => Race.Races[ReadByte()];
|
||||
|
||||
public bool End() => m_File.PeekChar() == -1;
|
||||
}
|
||||
|
||||
}
|
||||
660
Projects/Server/Serialization/BinaryFileWriter.cs
Normal file
660
Projects/Server/Serialization/BinaryFileWriter.cs
Normal file
|
|
@ -0,0 +1,660 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Net;
|
||||
using Server.Guilds;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
public class BinaryFileWriter : IGenericWriter
|
||||
{
|
||||
private const int LargeByteBufferSize = 256;
|
||||
|
||||
private byte[] m_Buffer;
|
||||
|
||||
private byte[] m_CharacterBuffer;
|
||||
|
||||
private Encoding m_Encoding;
|
||||
private Stream m_File;
|
||||
|
||||
private int m_Index;
|
||||
private int m_MaxBufferChars;
|
||||
|
||||
private long m_Position;
|
||||
|
||||
private char[] m_SingleCharBuffer = new char[1];
|
||||
private bool PrefixStrings;
|
||||
|
||||
public BinaryFileWriter(Stream strm, bool prefixStr)
|
||||
{
|
||||
PrefixStrings = prefixStr;
|
||||
m_Encoding = Utility.UTF8;
|
||||
m_Buffer = new byte[BufferSize];
|
||||
m_File = strm;
|
||||
}
|
||||
|
||||
public BinaryFileWriter(string filename, bool prefixStr)
|
||||
{
|
||||
PrefixStrings = prefixStr;
|
||||
m_Buffer = new byte[BufferSize];
|
||||
m_File = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None);
|
||||
m_Encoding = Utility.UTF8WithEncoding;
|
||||
}
|
||||
|
||||
protected virtual int BufferSize => 64 * 1024;
|
||||
|
||||
public long Position => m_Position + m_Index;
|
||||
|
||||
public Stream UnderlyingStream
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_Index > 0)
|
||||
Flush();
|
||||
|
||||
return m_File;
|
||||
}
|
||||
}
|
||||
|
||||
public void Flush()
|
||||
{
|
||||
if (m_Index > 0)
|
||||
{
|
||||
m_Position += m_Index;
|
||||
|
||||
m_File.Write(m_Buffer, 0, m_Index);
|
||||
m_Index = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
if (m_Index > 0)
|
||||
Flush();
|
||||
|
||||
m_File.Close();
|
||||
}
|
||||
|
||||
public void WriteEncodedInt(int value)
|
||||
{
|
||||
uint v = (uint)value;
|
||||
|
||||
while (v >= 0x80)
|
||||
{
|
||||
if (m_Index + 1 > m_Buffer.Length)
|
||||
Flush();
|
||||
|
||||
m_Buffer[m_Index++] = (byte)(v | 0x80);
|
||||
v >>= 7;
|
||||
}
|
||||
|
||||
if (m_Index + 1 > m_Buffer.Length)
|
||||
Flush();
|
||||
|
||||
m_Buffer[m_Index++] = (byte)v;
|
||||
}
|
||||
|
||||
internal void InternalWriteString(string value)
|
||||
{
|
||||
int length = m_Encoding.GetByteCount(value);
|
||||
|
||||
WriteEncodedInt(length);
|
||||
|
||||
if (m_CharacterBuffer == null)
|
||||
{
|
||||
m_CharacterBuffer = new byte[LargeByteBufferSize];
|
||||
m_MaxBufferChars = LargeByteBufferSize / m_Encoding.GetMaxByteCount(1);
|
||||
}
|
||||
|
||||
if (length > LargeByteBufferSize)
|
||||
{
|
||||
int current = 0;
|
||||
int charsLeft = value.Length;
|
||||
|
||||
while (charsLeft > 0)
|
||||
{
|
||||
int charCount = charsLeft > m_MaxBufferChars ? m_MaxBufferChars : charsLeft;
|
||||
int byteLength = m_Encoding.GetBytes(value, current, charCount, m_CharacterBuffer, 0);
|
||||
|
||||
if (m_Index + byteLength > m_Buffer.Length)
|
||||
Flush();
|
||||
|
||||
Buffer.BlockCopy(m_CharacterBuffer, 0, m_Buffer, m_Index, byteLength);
|
||||
m_Index += byteLength;
|
||||
|
||||
current += charCount;
|
||||
charsLeft -= charCount;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int byteLength = m_Encoding.GetBytes(value, 0, value.Length, m_CharacterBuffer, 0);
|
||||
|
||||
if (m_Index + byteLength > m_Buffer.Length)
|
||||
Flush();
|
||||
|
||||
Buffer.BlockCopy(m_CharacterBuffer, 0, m_Buffer, m_Index, byteLength);
|
||||
m_Index += byteLength;
|
||||
}
|
||||
}
|
||||
|
||||
public void Write(string value)
|
||||
{
|
||||
if (PrefixStrings)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
if (m_Index + 1 > m_Buffer.Length)
|
||||
Flush();
|
||||
|
||||
m_Buffer[m_Index++] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (m_Index + 1 > m_Buffer.Length)
|
||||
Flush();
|
||||
|
||||
m_Buffer[m_Index++] = 1;
|
||||
|
||||
InternalWriteString(value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
InternalWriteString(value);
|
||||
}
|
||||
}
|
||||
|
||||
public void Write(DateTime value)
|
||||
{
|
||||
Write(value.Ticks);
|
||||
}
|
||||
|
||||
public void Write(DateTimeOffset value)
|
||||
{
|
||||
Write(value.Ticks);
|
||||
Write(value.Offset.Ticks);
|
||||
}
|
||||
|
||||
public void WriteDeltaTime(DateTime value)
|
||||
{
|
||||
long ticks = value.Ticks;
|
||||
long now = DateTime.UtcNow.Ticks;
|
||||
|
||||
TimeSpan d;
|
||||
|
||||
try
|
||||
{
|
||||
d = new TimeSpan(ticks - now);
|
||||
}
|
||||
catch
|
||||
{
|
||||
d = TimeSpan.MaxValue;
|
||||
}
|
||||
|
||||
Write(d);
|
||||
}
|
||||
|
||||
public void Write(IPAddress value)
|
||||
{
|
||||
Write(Utility.GetLongAddressValue(value));
|
||||
}
|
||||
|
||||
public void Write(TimeSpan value)
|
||||
{
|
||||
Write(value.Ticks);
|
||||
}
|
||||
|
||||
public void Write(decimal value)
|
||||
{
|
||||
int[] bits = decimal.GetBits(value);
|
||||
|
||||
for (int i = 0; i < bits.Length; ++i)
|
||||
Write(bits[i]);
|
||||
}
|
||||
|
||||
public void Write(long value)
|
||||
{
|
||||
if (m_Index + 8 > m_Buffer.Length)
|
||||
Flush();
|
||||
|
||||
m_Buffer[m_Index] = (byte)value;
|
||||
m_Buffer[m_Index + 1] = (byte)(value >> 8);
|
||||
m_Buffer[m_Index + 2] = (byte)(value >> 16);
|
||||
m_Buffer[m_Index + 3] = (byte)(value >> 24);
|
||||
m_Buffer[m_Index + 4] = (byte)(value >> 32);
|
||||
m_Buffer[m_Index + 5] = (byte)(value >> 40);
|
||||
m_Buffer[m_Index + 6] = (byte)(value >> 48);
|
||||
m_Buffer[m_Index + 7] = (byte)(value >> 56);
|
||||
m_Index += 8;
|
||||
}
|
||||
|
||||
public void Write(ulong value)
|
||||
{
|
||||
if (m_Index + 8 > m_Buffer.Length)
|
||||
Flush();
|
||||
|
||||
m_Buffer[m_Index] = (byte)value;
|
||||
m_Buffer[m_Index + 1] = (byte)(value >> 8);
|
||||
m_Buffer[m_Index + 2] = (byte)(value >> 16);
|
||||
m_Buffer[m_Index + 3] = (byte)(value >> 24);
|
||||
m_Buffer[m_Index + 4] = (byte)(value >> 32);
|
||||
m_Buffer[m_Index + 5] = (byte)(value >> 40);
|
||||
m_Buffer[m_Index + 6] = (byte)(value >> 48);
|
||||
m_Buffer[m_Index + 7] = (byte)(value >> 56);
|
||||
m_Index += 8;
|
||||
}
|
||||
|
||||
public void Write(int value)
|
||||
{
|
||||
if (m_Index + 4 > m_Buffer.Length)
|
||||
Flush();
|
||||
|
||||
m_Buffer[m_Index] = (byte)value;
|
||||
m_Buffer[m_Index + 1] = (byte)(value >> 8);
|
||||
m_Buffer[m_Index + 2] = (byte)(value >> 16);
|
||||
m_Buffer[m_Index + 3] = (byte)(value >> 24);
|
||||
m_Index += 4;
|
||||
}
|
||||
|
||||
public void Write(uint value)
|
||||
{
|
||||
if (m_Index + 4 > m_Buffer.Length)
|
||||
Flush();
|
||||
|
||||
m_Buffer[m_Index] = (byte)value;
|
||||
m_Buffer[m_Index + 1] = (byte)(value >> 8);
|
||||
m_Buffer[m_Index + 2] = (byte)(value >> 16);
|
||||
m_Buffer[m_Index + 3] = (byte)(value >> 24);
|
||||
m_Index += 4;
|
||||
}
|
||||
|
||||
public void Write(short value)
|
||||
{
|
||||
if (m_Index + 2 > m_Buffer.Length)
|
||||
Flush();
|
||||
|
||||
m_Buffer[m_Index] = (byte)value;
|
||||
m_Buffer[m_Index + 1] = (byte)(value >> 8);
|
||||
m_Index += 2;
|
||||
}
|
||||
|
||||
public void Write(ushort value)
|
||||
{
|
||||
if (m_Index + 2 > m_Buffer.Length)
|
||||
Flush();
|
||||
|
||||
m_Buffer[m_Index] = (byte)value;
|
||||
m_Buffer[m_Index + 1] = (byte)(value >> 8);
|
||||
m_Index += 2;
|
||||
}
|
||||
|
||||
public unsafe void Write(double value)
|
||||
{
|
||||
if (m_Index + 8 > m_Buffer.Length)
|
||||
Flush();
|
||||
|
||||
fixed (byte* pBuffer = m_Buffer)
|
||||
{
|
||||
*(double*)(pBuffer + m_Index) = value;
|
||||
}
|
||||
|
||||
m_Index += 8;
|
||||
}
|
||||
|
||||
public unsafe void Write(float value)
|
||||
{
|
||||
if (m_Index + 4 > m_Buffer.Length)
|
||||
Flush();
|
||||
|
||||
fixed (byte* pBuffer = m_Buffer)
|
||||
{
|
||||
*(float*)(pBuffer + m_Index) = value;
|
||||
}
|
||||
|
||||
m_Index += 4;
|
||||
}
|
||||
|
||||
public void Write(char value)
|
||||
{
|
||||
if (m_Index + 8 > m_Buffer.Length)
|
||||
Flush();
|
||||
|
||||
m_SingleCharBuffer[0] = value;
|
||||
|
||||
int byteCount = m_Encoding.GetBytes(m_SingleCharBuffer, 0, 1, m_Buffer, m_Index);
|
||||
m_Index += byteCount;
|
||||
}
|
||||
|
||||
public void Write(byte value)
|
||||
{
|
||||
if (m_Index + 1 > m_Buffer.Length)
|
||||
Flush();
|
||||
|
||||
m_Buffer[m_Index++] = value;
|
||||
}
|
||||
|
||||
public void Write(byte[] value)
|
||||
{
|
||||
Write(value, value.Length);
|
||||
}
|
||||
|
||||
public void Write(byte[] value, int length)
|
||||
{
|
||||
if ((m_Index + length) > m_Buffer.Length)
|
||||
Flush();
|
||||
|
||||
Buffer.BlockCopy(value, 0, m_Buffer, m_Index, length);
|
||||
m_Index += length;
|
||||
}
|
||||
public void Write(sbyte value)
|
||||
{
|
||||
if (m_Index + 1 > m_Buffer.Length)
|
||||
Flush();
|
||||
|
||||
m_Buffer[m_Index++] = (byte)value;
|
||||
}
|
||||
|
||||
public void Write(bool value)
|
||||
{
|
||||
if (m_Index + 1 > m_Buffer.Length)
|
||||
Flush();
|
||||
|
||||
m_Buffer[m_Index++] = (byte)(value ? 1 : 0);
|
||||
}
|
||||
|
||||
public void Write(Point3D value)
|
||||
{
|
||||
Write(value.m_X);
|
||||
Write(value.m_Y);
|
||||
Write(value.m_Z);
|
||||
}
|
||||
|
||||
public void Write(Point2D value)
|
||||
{
|
||||
Write(value.m_X);
|
||||
Write(value.m_Y);
|
||||
}
|
||||
|
||||
public void Write(Rectangle2D value)
|
||||
{
|
||||
Write(value.Start);
|
||||
Write(value.End);
|
||||
}
|
||||
|
||||
public void Write(Rectangle3D value)
|
||||
{
|
||||
Write(value.Start);
|
||||
Write(value.End);
|
||||
}
|
||||
|
||||
public void Write(Map value)
|
||||
{
|
||||
if (value != null)
|
||||
Write((byte)value.MapIndex);
|
||||
else
|
||||
Write((byte)0xFF);
|
||||
}
|
||||
|
||||
public void Write(Race value)
|
||||
{
|
||||
if (value != null)
|
||||
Write((byte)value.RaceIndex);
|
||||
else
|
||||
Write((byte)0xFF);
|
||||
}
|
||||
|
||||
public void WriteEntity(IEntity value)
|
||||
{
|
||||
if (value?.Deleted != false)
|
||||
Write(Serial.MinusOne);
|
||||
else
|
||||
Write(value.Serial);
|
||||
}
|
||||
|
||||
public void Write(Item value)
|
||||
{
|
||||
if (value?.Deleted != false)
|
||||
Write(Serial.MinusOne);
|
||||
else
|
||||
Write(value.Serial);
|
||||
}
|
||||
|
||||
public void Write(Mobile value)
|
||||
{
|
||||
if (value?.Deleted != false)
|
||||
Write(Serial.MinusOne);
|
||||
else
|
||||
Write(value.Serial);
|
||||
}
|
||||
|
||||
public void Write(BaseGuild value)
|
||||
{
|
||||
if (value == null)
|
||||
Write(0);
|
||||
else
|
||||
Write(value.Id);
|
||||
}
|
||||
|
||||
public void WriteItem<T>(T value) where T : Item
|
||||
{
|
||||
Write(value);
|
||||
}
|
||||
|
||||
public void WriteMobile<T>(T value) where T : Mobile
|
||||
{
|
||||
Write(value);
|
||||
}
|
||||
|
||||
public void WriteGuild<T>(T value) where T : BaseGuild
|
||||
{
|
||||
Write(value);
|
||||
}
|
||||
|
||||
public void Write(List<Item> list)
|
||||
{
|
||||
Write(list, false);
|
||||
}
|
||||
|
||||
public void Write(List<Item> list, bool tidy)
|
||||
{
|
||||
if (tidy)
|
||||
for (int i = 0; i < list.Count;)
|
||||
if (list[i].Deleted)
|
||||
list.RemoveAt(i);
|
||||
else
|
||||
++i;
|
||||
|
||||
Write(list.Count);
|
||||
|
||||
for (int i = 0; i < list.Count; ++i)
|
||||
Write(list[i]);
|
||||
}
|
||||
|
||||
public void WriteItemList<T>(List<T> list) where T : Item
|
||||
{
|
||||
WriteItemList(list, false);
|
||||
}
|
||||
|
||||
public void WriteItemList<T>(List<T> list, bool tidy) where T : Item
|
||||
{
|
||||
if (tidy)
|
||||
for (int i = 0; i < list.Count;)
|
||||
if (list[i].Deleted)
|
||||
list.RemoveAt(i);
|
||||
else
|
||||
++i;
|
||||
|
||||
Write(list.Count);
|
||||
|
||||
for (int i = 0; i < list.Count; ++i)
|
||||
Write(list[i]);
|
||||
}
|
||||
|
||||
public void Write(HashSet<Item> set)
|
||||
{
|
||||
Write(set, false);
|
||||
}
|
||||
|
||||
public void Write(HashSet<Item> set, bool tidy)
|
||||
{
|
||||
if (tidy) set.RemoveWhere(item => item.Deleted);
|
||||
|
||||
Write(set.Count);
|
||||
|
||||
foreach (Item item in set) Write(item);
|
||||
}
|
||||
|
||||
public void WriteItemSet<T>(HashSet<T> set) where T : Item
|
||||
{
|
||||
WriteItemSet(set, false);
|
||||
}
|
||||
|
||||
public void WriteItemSet<T>(HashSet<T> set, bool tidy) where T : Item
|
||||
{
|
||||
if (tidy) set.RemoveWhere(item => item.Deleted);
|
||||
|
||||
Write(set.Count);
|
||||
|
||||
foreach (T item in set) Write(item);
|
||||
}
|
||||
|
||||
public void Write(List<Mobile> list)
|
||||
{
|
||||
Write(list, false);
|
||||
}
|
||||
|
||||
public void Write(List<Mobile> list, bool tidy)
|
||||
{
|
||||
if (tidy)
|
||||
for (int i = 0; i < list.Count;)
|
||||
if (list[i].Deleted)
|
||||
list.RemoveAt(i);
|
||||
else
|
||||
++i;
|
||||
|
||||
Write(list.Count);
|
||||
|
||||
for (int i = 0; i < list.Count; ++i)
|
||||
Write(list[i]);
|
||||
}
|
||||
|
||||
public void WriteMobileList<T>(List<T> list) where T : Mobile
|
||||
{
|
||||
WriteMobileList(list, false);
|
||||
}
|
||||
|
||||
public void WriteMobileList<T>(List<T> list, bool tidy) where T : Mobile
|
||||
{
|
||||
if (tidy)
|
||||
for (int i = 0; i < list.Count;)
|
||||
if (list[i].Deleted)
|
||||
list.RemoveAt(i);
|
||||
else
|
||||
++i;
|
||||
|
||||
Write(list.Count);
|
||||
|
||||
for (int i = 0; i < list.Count; ++i)
|
||||
Write(list[i]);
|
||||
}
|
||||
|
||||
public void Write(HashSet<Mobile> set)
|
||||
{
|
||||
Write(set, false);
|
||||
}
|
||||
|
||||
public void Write(HashSet<Mobile> set, bool tidy)
|
||||
{
|
||||
if (tidy) set.RemoveWhere(mobile => mobile.Deleted);
|
||||
|
||||
Write(set.Count);
|
||||
|
||||
foreach (Mobile mob in set) Write(mob);
|
||||
}
|
||||
|
||||
public void WriteMobileSet<T>(HashSet<T> set) where T : Mobile
|
||||
{
|
||||
WriteMobileSet(set, false);
|
||||
}
|
||||
|
||||
public void WriteMobileSet<T>(HashSet<T> set, bool tidy) where T : Mobile
|
||||
{
|
||||
if (tidy) set.RemoveWhere(mob => mob.Deleted);
|
||||
|
||||
Write(set.Count);
|
||||
|
||||
foreach (T mob in set) Write(mob);
|
||||
}
|
||||
|
||||
public void Write(List<BaseGuild> list)
|
||||
{
|
||||
Write(list, false);
|
||||
}
|
||||
|
||||
public void Write(List<BaseGuild> list, bool tidy)
|
||||
{
|
||||
if (tidy)
|
||||
for (int i = 0; i < list.Count;)
|
||||
if (list[i].Disbanded)
|
||||
list.RemoveAt(i);
|
||||
else
|
||||
++i;
|
||||
|
||||
Write(list.Count);
|
||||
|
||||
for (int i = 0; i < list.Count; ++i)
|
||||
Write(list[i]);
|
||||
}
|
||||
|
||||
public void WriteGuildList<T>(List<T> list) where T : BaseGuild
|
||||
{
|
||||
WriteGuildList(list, false);
|
||||
}
|
||||
|
||||
public void WriteGuildList<T>(List<T> list, bool tidy) where T : BaseGuild
|
||||
{
|
||||
if (tidy)
|
||||
for (int i = 0; i < list.Count;)
|
||||
if (list[i].Disbanded)
|
||||
list.RemoveAt(i);
|
||||
else
|
||||
++i;
|
||||
|
||||
Write(list.Count);
|
||||
|
||||
for (int i = 0; i < list.Count; ++i)
|
||||
Write(list[i]);
|
||||
}
|
||||
|
||||
public void Write(HashSet<BaseGuild> set)
|
||||
{
|
||||
Write(set, false);
|
||||
}
|
||||
|
||||
public void Write(HashSet<BaseGuild> set, bool tidy)
|
||||
{
|
||||
if (tidy) set.RemoveWhere(guild => guild.Disbanded);
|
||||
|
||||
Write(set.Count);
|
||||
|
||||
foreach (BaseGuild guild in set) Write(guild);
|
||||
}
|
||||
|
||||
public void WriteGuildSet<T>(HashSet<T> set) where T : BaseGuild
|
||||
{
|
||||
WriteGuildSet(set, false);
|
||||
}
|
||||
|
||||
public void WriteGuildSet<T>(HashSet<T> set, bool tidy) where T : BaseGuild
|
||||
{
|
||||
if (tidy) set.RemoveWhere(guild => guild.Disbanded);
|
||||
|
||||
Write(set.Count);
|
||||
|
||||
foreach (T guild in set) Write(guild);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
645
Projects/Server/Serialization/BufferWriter.cs
Normal file
645
Projects/Server/Serialization/BufferWriter.cs
Normal file
|
|
@ -0,0 +1,645 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Net;
|
||||
using Server.Guilds;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
public class BufferWriter : IGenericWriter
|
||||
{
|
||||
private const int LargeByteBufferSize = 256;
|
||||
|
||||
private byte[] m_Buffer;
|
||||
|
||||
private byte[] m_CharacterBuffer;
|
||||
|
||||
private Encoding m_Encoding;
|
||||
|
||||
private int m_Index;
|
||||
private int m_MaxBufferChars;
|
||||
|
||||
private char[] m_SingleCharBuffer = new char[1];
|
||||
private bool PrefixStrings;
|
||||
|
||||
public BufferWriter(bool prefixStr)
|
||||
{
|
||||
PrefixStrings = prefixStr;
|
||||
m_Encoding = Utility.UTF8;
|
||||
m_Buffer = new byte[BufferSize];
|
||||
}
|
||||
|
||||
protected virtual int BufferSize {
|
||||
get {
|
||||
if (m_Buffer != null)
|
||||
{
|
||||
return m_Buffer.Length;
|
||||
}
|
||||
return 64;
|
||||
} }
|
||||
|
||||
public long Position => m_Index;
|
||||
|
||||
public byte[] Data { get { return m_Buffer; } }
|
||||
|
||||
public void Close()
|
||||
{
|
||||
}
|
||||
public void Flush()
|
||||
{
|
||||
m_Index = 0;
|
||||
}
|
||||
|
||||
private void Expand()
|
||||
{
|
||||
byte[] newBuffer = new byte[BufferSize * 2];
|
||||
Buffer.BlockCopy(m_Buffer, 0, newBuffer, 0, m_Buffer.Length);
|
||||
m_Buffer = newBuffer;
|
||||
}
|
||||
|
||||
public void WriteEncodedInt(int value)
|
||||
{
|
||||
uint v = (uint)value;
|
||||
|
||||
while (v >= 0x80)
|
||||
{
|
||||
if (m_Index + 1 > m_Buffer.Length)
|
||||
Expand();
|
||||
|
||||
m_Buffer[m_Index++] = (byte)(v | 0x80);
|
||||
v >>= 7;
|
||||
}
|
||||
|
||||
if (m_Index + 1 > m_Buffer.Length)
|
||||
Expand();
|
||||
|
||||
m_Buffer[m_Index++] = (byte)v;
|
||||
}
|
||||
|
||||
internal void InternalWriteString(string value)
|
||||
{
|
||||
int length = m_Encoding.GetByteCount(value);
|
||||
|
||||
WriteEncodedInt(length);
|
||||
|
||||
if (m_CharacterBuffer == null)
|
||||
{
|
||||
m_CharacterBuffer = new byte[LargeByteBufferSize];
|
||||
m_MaxBufferChars = LargeByteBufferSize / m_Encoding.GetMaxByteCount(1);
|
||||
}
|
||||
|
||||
if (length > LargeByteBufferSize)
|
||||
{
|
||||
int current = 0;
|
||||
int charsLeft = value.Length;
|
||||
|
||||
while (charsLeft > 0)
|
||||
{
|
||||
int charCount = charsLeft > m_MaxBufferChars ? m_MaxBufferChars : charsLeft;
|
||||
int byteLength = m_Encoding.GetBytes(value, current, charCount, m_CharacterBuffer, 0);
|
||||
|
||||
if (m_Index + byteLength > m_Buffer.Length)
|
||||
Expand();
|
||||
|
||||
Buffer.BlockCopy(m_CharacterBuffer, 0, m_Buffer, m_Index, byteLength);
|
||||
m_Index += byteLength;
|
||||
|
||||
current += charCount;
|
||||
charsLeft -= charCount;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int byteLength = m_Encoding.GetBytes(value, 0, value.Length, m_CharacterBuffer, 0);
|
||||
|
||||
if (m_Index + byteLength > m_Buffer.Length)
|
||||
Expand();
|
||||
|
||||
Buffer.BlockCopy(m_CharacterBuffer, 0, m_Buffer, m_Index, byteLength);
|
||||
m_Index += byteLength;
|
||||
}
|
||||
}
|
||||
|
||||
public void Write(string value)
|
||||
{
|
||||
if (PrefixStrings)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
if (m_Index + 1 > m_Buffer.Length)
|
||||
Expand();
|
||||
|
||||
m_Buffer[m_Index++] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (m_Index + 1 > m_Buffer.Length)
|
||||
Expand();
|
||||
|
||||
m_Buffer[m_Index++] = 1;
|
||||
|
||||
InternalWriteString(value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
InternalWriteString(value);
|
||||
}
|
||||
}
|
||||
|
||||
public void Write(DateTime value)
|
||||
{
|
||||
Write(value.Ticks);
|
||||
}
|
||||
|
||||
public void Write(DateTimeOffset value)
|
||||
{
|
||||
Write(value.Ticks);
|
||||
Write(value.Offset.Ticks);
|
||||
}
|
||||
|
||||
public void WriteDeltaTime(DateTime value)
|
||||
{
|
||||
long ticks = value.Ticks;
|
||||
long now = DateTime.UtcNow.Ticks;
|
||||
|
||||
TimeSpan d;
|
||||
|
||||
try
|
||||
{
|
||||
d = new TimeSpan(ticks - now);
|
||||
}
|
||||
catch
|
||||
{
|
||||
d = TimeSpan.MaxValue;
|
||||
}
|
||||
|
||||
Write(d);
|
||||
}
|
||||
|
||||
public void Write(IPAddress value)
|
||||
{
|
||||
Write(Utility.GetLongAddressValue(value));
|
||||
}
|
||||
|
||||
public void Write(TimeSpan value)
|
||||
{
|
||||
Write(value.Ticks);
|
||||
}
|
||||
|
||||
public void Write(decimal value)
|
||||
{
|
||||
int[] bits = decimal.GetBits(value);
|
||||
|
||||
for (int i = 0; i < bits.Length; ++i)
|
||||
Write(bits[i]);
|
||||
}
|
||||
|
||||
public void Write(long value)
|
||||
{
|
||||
if (m_Index + 8 > m_Buffer.Length)
|
||||
Expand();
|
||||
|
||||
m_Buffer[m_Index] = (byte)value;
|
||||
m_Buffer[m_Index + 1] = (byte)(value >> 8);
|
||||
m_Buffer[m_Index + 2] = (byte)(value >> 16);
|
||||
m_Buffer[m_Index + 3] = (byte)(value >> 24);
|
||||
m_Buffer[m_Index + 4] = (byte)(value >> 32);
|
||||
m_Buffer[m_Index + 5] = (byte)(value >> 40);
|
||||
m_Buffer[m_Index + 6] = (byte)(value >> 48);
|
||||
m_Buffer[m_Index + 7] = (byte)(value >> 56);
|
||||
m_Index += 8;
|
||||
}
|
||||
|
||||
public void Write(ulong value)
|
||||
{
|
||||
if (m_Index + 8 > m_Buffer.Length)
|
||||
Expand();
|
||||
|
||||
m_Buffer[m_Index] = (byte)value;
|
||||
m_Buffer[m_Index + 1] = (byte)(value >> 8);
|
||||
m_Buffer[m_Index + 2] = (byte)(value >> 16);
|
||||
m_Buffer[m_Index + 3] = (byte)(value >> 24);
|
||||
m_Buffer[m_Index + 4] = (byte)(value >> 32);
|
||||
m_Buffer[m_Index + 5] = (byte)(value >> 40);
|
||||
m_Buffer[m_Index + 6] = (byte)(value >> 48);
|
||||
m_Buffer[m_Index + 7] = (byte)(value >> 56);
|
||||
m_Index += 8;
|
||||
}
|
||||
|
||||
public void Write(int value)
|
||||
{
|
||||
if (m_Index + 4 > m_Buffer.Length)
|
||||
Expand();
|
||||
|
||||
m_Buffer[m_Index] = (byte)value;
|
||||
m_Buffer[m_Index + 1] = (byte)(value >> 8);
|
||||
m_Buffer[m_Index + 2] = (byte)(value >> 16);
|
||||
m_Buffer[m_Index + 3] = (byte)(value >> 24);
|
||||
m_Index += 4;
|
||||
}
|
||||
|
||||
public void Write(uint value)
|
||||
{
|
||||
if (m_Index + 4 > m_Buffer.Length)
|
||||
Expand();
|
||||
|
||||
m_Buffer[m_Index] = (byte)value;
|
||||
m_Buffer[m_Index + 1] = (byte)(value >> 8);
|
||||
m_Buffer[m_Index + 2] = (byte)(value >> 16);
|
||||
m_Buffer[m_Index + 3] = (byte)(value >> 24);
|
||||
m_Index += 4;
|
||||
}
|
||||
|
||||
public void Write(short value)
|
||||
{
|
||||
if (m_Index + 2 > m_Buffer.Length)
|
||||
Expand();
|
||||
|
||||
m_Buffer[m_Index] = (byte)value;
|
||||
m_Buffer[m_Index + 1] = (byte)(value >> 8);
|
||||
m_Index += 2;
|
||||
}
|
||||
|
||||
public void Write(ushort value)
|
||||
{
|
||||
if (m_Index + 2 > m_Buffer.Length)
|
||||
Expand();
|
||||
|
||||
m_Buffer[m_Index] = (byte)value;
|
||||
m_Buffer[m_Index + 1] = (byte)(value >> 8);
|
||||
m_Index += 2;
|
||||
}
|
||||
|
||||
public unsafe void Write(double value)
|
||||
{
|
||||
if (m_Index + 8 > m_Buffer.Length)
|
||||
Expand();
|
||||
|
||||
fixed (byte* pBuffer = m_Buffer)
|
||||
{
|
||||
*(double*)(pBuffer + m_Index) = value;
|
||||
}
|
||||
|
||||
m_Index += 8;
|
||||
}
|
||||
|
||||
public unsafe void Write(float value)
|
||||
{
|
||||
if (m_Index + 4 > m_Buffer.Length)
|
||||
Expand();
|
||||
|
||||
fixed (byte* pBuffer = m_Buffer)
|
||||
{
|
||||
*(float*)(pBuffer + m_Index) = value;
|
||||
}
|
||||
|
||||
m_Index += 4;
|
||||
}
|
||||
|
||||
public void Write(char value)
|
||||
{
|
||||
if (m_Index + 8 > m_Buffer.Length)
|
||||
Expand();
|
||||
|
||||
m_SingleCharBuffer[0] = value;
|
||||
|
||||
int byteCount = m_Encoding.GetBytes(m_SingleCharBuffer, 0, 1, m_Buffer, m_Index);
|
||||
m_Index += byteCount;
|
||||
}
|
||||
|
||||
public void Write(byte value)
|
||||
{
|
||||
if (m_Index + 1 > m_Buffer.Length)
|
||||
Expand();
|
||||
|
||||
m_Buffer[m_Index++] = value;
|
||||
}
|
||||
|
||||
public void Write(byte[] value)
|
||||
{
|
||||
Write(value, value.Length);
|
||||
}
|
||||
|
||||
public void Write(byte[] value, int length)
|
||||
{
|
||||
if ((m_Index + length) > m_Buffer.Length)
|
||||
Expand();
|
||||
|
||||
Buffer.BlockCopy(value, 0, m_Buffer, m_Index, length);
|
||||
m_Index += length;
|
||||
}
|
||||
public void Write(sbyte value)
|
||||
{
|
||||
if (m_Index + 1 > m_Buffer.Length)
|
||||
Expand();
|
||||
|
||||
m_Buffer[m_Index++] = (byte)value;
|
||||
}
|
||||
|
||||
public void Write(bool value)
|
||||
{
|
||||
if (m_Index + 1 > m_Buffer.Length)
|
||||
Expand();
|
||||
|
||||
m_Buffer[m_Index++] = (byte)(value ? 1 : 0);
|
||||
}
|
||||
|
||||
public void Write(Point3D value)
|
||||
{
|
||||
Write(value.m_X);
|
||||
Write(value.m_Y);
|
||||
Write(value.m_Z);
|
||||
}
|
||||
|
||||
public void Write(Point2D value)
|
||||
{
|
||||
Write(value.m_X);
|
||||
Write(value.m_Y);
|
||||
}
|
||||
|
||||
public void Write(Rectangle2D value)
|
||||
{
|
||||
Write(value.Start);
|
||||
Write(value.End);
|
||||
}
|
||||
|
||||
public void Write(Rectangle3D value)
|
||||
{
|
||||
Write(value.Start);
|
||||
Write(value.End);
|
||||
}
|
||||
|
||||
public void Write(Map value)
|
||||
{
|
||||
if (value != null)
|
||||
Write((byte)value.MapIndex);
|
||||
else
|
||||
Write((byte)0xFF);
|
||||
}
|
||||
|
||||
public void Write(Race value)
|
||||
{
|
||||
if (value != null)
|
||||
Write((byte)value.RaceIndex);
|
||||
else
|
||||
Write((byte)0xFF);
|
||||
}
|
||||
|
||||
public void WriteEntity(IEntity value)
|
||||
{
|
||||
if (value?.Deleted != false)
|
||||
Write(Serial.MinusOne);
|
||||
else
|
||||
Write(value.Serial);
|
||||
}
|
||||
|
||||
public void Write(Item value)
|
||||
{
|
||||
if (value?.Deleted != false)
|
||||
Write(Serial.MinusOne);
|
||||
else
|
||||
Write(value.Serial);
|
||||
}
|
||||
|
||||
public void Write(Mobile value)
|
||||
{
|
||||
if (value?.Deleted != false)
|
||||
Write(Serial.MinusOne);
|
||||
else
|
||||
Write(value.Serial);
|
||||
}
|
||||
|
||||
public void Write(BaseGuild value)
|
||||
{
|
||||
if (value == null)
|
||||
Write(0);
|
||||
else
|
||||
Write(value.Id);
|
||||
}
|
||||
|
||||
public void WriteItem<T>(T value) where T : Item
|
||||
{
|
||||
Write(value);
|
||||
}
|
||||
|
||||
public void WriteMobile<T>(T value) where T : Mobile
|
||||
{
|
||||
Write(value);
|
||||
}
|
||||
|
||||
public void WriteGuild<T>(T value) where T : BaseGuild
|
||||
{
|
||||
Write(value);
|
||||
}
|
||||
|
||||
public void Write(List<Item> list)
|
||||
{
|
||||
Write(list, false);
|
||||
}
|
||||
|
||||
public void Write(List<Item> list, bool tidy)
|
||||
{
|
||||
if (tidy)
|
||||
for (int i = 0; i < list.Count;)
|
||||
if (list[i].Deleted)
|
||||
list.RemoveAt(i);
|
||||
else
|
||||
++i;
|
||||
|
||||
Write(list.Count);
|
||||
|
||||
for (int i = 0; i < list.Count; ++i)
|
||||
Write(list[i]);
|
||||
}
|
||||
|
||||
public void WriteItemList<T>(List<T> list) where T : Item
|
||||
{
|
||||
WriteItemList(list, false);
|
||||
}
|
||||
|
||||
public void WriteItemList<T>(List<T> list, bool tidy) where T : Item
|
||||
{
|
||||
if (tidy)
|
||||
for (int i = 0; i < list.Count;)
|
||||
if (list[i].Deleted)
|
||||
list.RemoveAt(i);
|
||||
else
|
||||
++i;
|
||||
|
||||
Write(list.Count);
|
||||
|
||||
for (int i = 0; i < list.Count; ++i)
|
||||
Write(list[i]);
|
||||
}
|
||||
|
||||
public void Write(HashSet<Item> set)
|
||||
{
|
||||
Write(set, false);
|
||||
}
|
||||
|
||||
public void Write(HashSet<Item> set, bool tidy)
|
||||
{
|
||||
if (tidy) set.RemoveWhere(item => item.Deleted);
|
||||
|
||||
Write(set.Count);
|
||||
|
||||
foreach (Item item in set) Write(item);
|
||||
}
|
||||
|
||||
public void WriteItemSet<T>(HashSet<T> set) where T : Item
|
||||
{
|
||||
WriteItemSet(set, false);
|
||||
}
|
||||
|
||||
public void WriteItemSet<T>(HashSet<T> set, bool tidy) where T : Item
|
||||
{
|
||||
if (tidy) set.RemoveWhere(item => item.Deleted);
|
||||
|
||||
Write(set.Count);
|
||||
|
||||
foreach (T item in set) Write(item);
|
||||
}
|
||||
|
||||
public void Write(List<Mobile> list)
|
||||
{
|
||||
Write(list, false);
|
||||
}
|
||||
|
||||
public void Write(List<Mobile> list, bool tidy)
|
||||
{
|
||||
if (tidy)
|
||||
for (int i = 0; i < list.Count;)
|
||||
if (list[i].Deleted)
|
||||
list.RemoveAt(i);
|
||||
else
|
||||
++i;
|
||||
|
||||
Write(list.Count);
|
||||
|
||||
for (int i = 0; i < list.Count; ++i)
|
||||
Write(list[i]);
|
||||
}
|
||||
|
||||
public void WriteMobileList<T>(List<T> list) where T : Mobile
|
||||
{
|
||||
WriteMobileList(list, false);
|
||||
}
|
||||
|
||||
public void WriteMobileList<T>(List<T> list, bool tidy) where T : Mobile
|
||||
{
|
||||
if (tidy)
|
||||
for (int i = 0; i < list.Count;)
|
||||
if (list[i].Deleted)
|
||||
list.RemoveAt(i);
|
||||
else
|
||||
++i;
|
||||
|
||||
Write(list.Count);
|
||||
|
||||
for (int i = 0; i < list.Count; ++i)
|
||||
Write(list[i]);
|
||||
}
|
||||
|
||||
public void Write(HashSet<Mobile> set)
|
||||
{
|
||||
Write(set, false);
|
||||
}
|
||||
|
||||
public void Write(HashSet<Mobile> set, bool tidy)
|
||||
{
|
||||
if (tidy) set.RemoveWhere(mobile => mobile.Deleted);
|
||||
|
||||
Write(set.Count);
|
||||
|
||||
foreach (Mobile mob in set) Write(mob);
|
||||
}
|
||||
|
||||
public void WriteMobileSet<T>(HashSet<T> set) where T : Mobile
|
||||
{
|
||||
WriteMobileSet(set, false);
|
||||
}
|
||||
|
||||
public void WriteMobileSet<T>(HashSet<T> set, bool tidy) where T : Mobile
|
||||
{
|
||||
if (tidy) set.RemoveWhere(mob => mob.Deleted);
|
||||
|
||||
Write(set.Count);
|
||||
|
||||
foreach (T mob in set) Write(mob);
|
||||
}
|
||||
|
||||
public void Write(List<BaseGuild> list)
|
||||
{
|
||||
Write(list, false);
|
||||
}
|
||||
|
||||
public void Write(List<BaseGuild> list, bool tidy)
|
||||
{
|
||||
if (tidy)
|
||||
for (int i = 0; i < list.Count;)
|
||||
if (list[i].Disbanded)
|
||||
list.RemoveAt(i);
|
||||
else
|
||||
++i;
|
||||
|
||||
Write(list.Count);
|
||||
|
||||
for (int i = 0; i < list.Count; ++i)
|
||||
Write(list[i]);
|
||||
}
|
||||
|
||||
public void WriteGuildList<T>(List<T> list) where T : BaseGuild
|
||||
{
|
||||
WriteGuildList(list, false);
|
||||
}
|
||||
|
||||
public void WriteGuildList<T>(List<T> list, bool tidy) where T : BaseGuild
|
||||
{
|
||||
if (tidy)
|
||||
for (int i = 0; i < list.Count;)
|
||||
if (list[i].Disbanded)
|
||||
list.RemoveAt(i);
|
||||
else
|
||||
++i;
|
||||
|
||||
Write(list.Count);
|
||||
|
||||
for (int i = 0; i < list.Count; ++i)
|
||||
Write(list[i]);
|
||||
}
|
||||
|
||||
public void Write(HashSet<BaseGuild> set)
|
||||
{
|
||||
Write(set, false);
|
||||
}
|
||||
|
||||
public void Write(HashSet<BaseGuild> set, bool tidy)
|
||||
{
|
||||
if (tidy) set.RemoveWhere(guild => guild.Disbanded);
|
||||
|
||||
Write(set.Count);
|
||||
|
||||
foreach (BaseGuild guild in set) Write(guild);
|
||||
}
|
||||
|
||||
public void WriteGuildSet<T>(HashSet<T> set) where T : BaseGuild
|
||||
{
|
||||
WriteGuildSet(set, false);
|
||||
}
|
||||
|
||||
public void WriteGuildSet<T>(HashSet<T> set, bool tidy) where T : BaseGuild
|
||||
{
|
||||
if (tidy) set.RemoveWhere(guild => guild.Disbanded);
|
||||
|
||||
Write(set.Count);
|
||||
|
||||
foreach (T guild in set) Write(guild);
|
||||
}
|
||||
|
||||
public void WriteTo(IGenericWriter writer)
|
||||
{
|
||||
writer.Write(Data, (int)Position);
|
||||
}
|
||||
}
|
||||
}
|
||||
57
Projects/Server/Serialization/IGenericReader.cs
Normal file
57
Projects/Server/Serialization/IGenericReader.cs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using Server.Guilds;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
public interface IGenericReader
|
||||
{
|
||||
string ReadString();
|
||||
DateTime ReadDateTime();
|
||||
DateTimeOffset ReadDateTimeOffset();
|
||||
TimeSpan ReadTimeSpan();
|
||||
DateTime ReadDeltaTime();
|
||||
decimal ReadDecimal();
|
||||
long ReadLong();
|
||||
ulong ReadULong();
|
||||
int ReadInt();
|
||||
uint ReadUInt();
|
||||
short ReadShort();
|
||||
ushort ReadUShort();
|
||||
double ReadDouble();
|
||||
float ReadFloat();
|
||||
char ReadChar();
|
||||
byte ReadByte();
|
||||
sbyte ReadSByte();
|
||||
bool ReadBool();
|
||||
int ReadEncodedInt();
|
||||
IPAddress ReadIPAddress();
|
||||
Point3D ReadPoint3D();
|
||||
Point2D ReadPoint2D();
|
||||
Rectangle2D ReadRect2D();
|
||||
Rectangle3D ReadRect3D();
|
||||
Map ReadMap();
|
||||
IEntity ReadEntity();
|
||||
Item ReadItem();
|
||||
Mobile ReadMobile();
|
||||
BaseGuild ReadGuild();
|
||||
T ReadItem<T>() where T : Item;
|
||||
T ReadMobile<T>() where T : Mobile;
|
||||
T ReadGuild<T>() where T : BaseGuild;
|
||||
List<Item> ReadStrongItemList();
|
||||
List<T> ReadStrongItemList<T>() where T : Item;
|
||||
List<Mobile> ReadStrongMobileList();
|
||||
List<T> ReadStrongMobileList<T>() where T : Mobile;
|
||||
List<BaseGuild> ReadStrongGuildList();
|
||||
List<T> ReadStrongGuildList<T>() where T : BaseGuild;
|
||||
HashSet<Item> ReadItemSet();
|
||||
HashSet<T> ReadItemSet<T>() where T : Item;
|
||||
HashSet<Mobile> ReadMobileSet();
|
||||
HashSet<T> ReadMobileSet<T>() where T : Mobile;
|
||||
HashSet<BaseGuild> ReadGuildSet();
|
||||
HashSet<T> ReadGuildSet<T>() where T : BaseGuild;
|
||||
Race ReadRace();
|
||||
bool End();
|
||||
}
|
||||
}
|
||||
91
Projects/Server/Serialization/IGenericWriter.cs
Normal file
91
Projects/Server/Serialization/IGenericWriter.cs
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using Server.Guilds;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
public interface IGenericWriter
|
||||
{
|
||||
long Position { get; }
|
||||
|
||||
void Close();
|
||||
|
||||
void Write(string value);
|
||||
void Write(DateTime value);
|
||||
void Write(DateTimeOffset value);
|
||||
void Write(TimeSpan value);
|
||||
void Write(decimal value);
|
||||
void Write(long value);
|
||||
void Write(ulong value);
|
||||
void Write(int value);
|
||||
void Write(uint value);
|
||||
void Write(short value);
|
||||
void Write(ushort value);
|
||||
void Write(double value);
|
||||
void Write(float value);
|
||||
void Write(char value);
|
||||
void Write(byte value);
|
||||
void Write(byte[] value);
|
||||
void Write(byte[] value, int length);
|
||||
void Write(sbyte value);
|
||||
void Write(bool value);
|
||||
void WriteEncodedInt(int value);
|
||||
void Write(IPAddress value);
|
||||
|
||||
void WriteDeltaTime(DateTime value);
|
||||
|
||||
void Write(Point3D value);
|
||||
void Write(Point2D value);
|
||||
void Write(Rectangle2D value);
|
||||
void Write(Rectangle3D value);
|
||||
void Write(Map value);
|
||||
|
||||
void WriteEntity(IEntity value);
|
||||
void Write(Item value);
|
||||
void Write(Mobile value);
|
||||
void Write(BaseGuild value);
|
||||
|
||||
void WriteItem<T>(T value) where T : Item;
|
||||
void WriteMobile<T>(T value) where T : Mobile;
|
||||
void WriteGuild<T>(T value) where T : BaseGuild;
|
||||
|
||||
void Write(Race value);
|
||||
|
||||
void Write(List<Item> list);
|
||||
void Write(List<Item> list, bool tidy);
|
||||
|
||||
void WriteItemList<T>(List<T> list) where T : Item;
|
||||
void WriteItemList<T>(List<T> list, bool tidy) where T : Item;
|
||||
|
||||
void Write(HashSet<Item> list);
|
||||
void Write(HashSet<Item> list, bool tidy);
|
||||
|
||||
void WriteItemSet<T>(HashSet<T> set) where T : Item;
|
||||
void WriteItemSet<T>(HashSet<T> set, bool tidy) where T : Item;
|
||||
|
||||
void Write(List<Mobile> list);
|
||||
void Write(List<Mobile> list, bool tidy);
|
||||
|
||||
void WriteMobileList<T>(List<T> list) where T : Mobile;
|
||||
void WriteMobileList<T>(List<T> list, bool tidy) where T : Mobile;
|
||||
|
||||
void Write(HashSet<Mobile> list);
|
||||
void Write(HashSet<Mobile> list, bool tidy);
|
||||
|
||||
void WriteMobileSet<T>(HashSet<T> set) where T : Mobile;
|
||||
void WriteMobileSet<T>(HashSet<T> set, bool tidy) where T : Mobile;
|
||||
|
||||
void Write(List<BaseGuild> list);
|
||||
void Write(List<BaseGuild> list, bool tidy);
|
||||
|
||||
void WriteGuildList<T>(List<T> list) where T : BaseGuild;
|
||||
void WriteGuildList<T>(List<T> list, bool tidy) where T : BaseGuild;
|
||||
|
||||
void Write(HashSet<BaseGuild> list);
|
||||
void Write(HashSet<BaseGuild> list, bool tidy);
|
||||
|
||||
void WriteGuildSet<T>(HashSet<T> set) where T : BaseGuild;
|
||||
void WriteGuildSet<T>(HashSet<T> set, bool tidy) where T : BaseGuild;
|
||||
}
|
||||
}
|
||||
15
Projects/Server/Serialization/ISerializable.cs
Normal file
15
Projects/Server/Serialization/ISerializable.cs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
public interface ISerializable
|
||||
{
|
||||
BufferWriter SaveBuffer { get; }
|
||||
int TypeReference { get; }
|
||||
uint SerialIdentity { get; }
|
||||
void Serialize();
|
||||
void Serialize(IGenericWriter writer);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue