v0.0.1-Alpha (#42)

This commit is contained in:
Kamron Batman 2019-08-02 18:16:11 -07:00 • committed by GitHub
parent d30f93c454
commit a36796c2c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3552 changed files with 2476 additions and 15223 deletions

View file

@ -0,0 +1,82 @@
/***************************************************************************
* BinaryMemoryWriter.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.IO;
namespace Server
{
public sealed class BinaryMemoryWriter : BinaryFileWriter
{
private static byte[] indexBuffer;
private MemoryStream stream;
public BinaryMemoryWriter()
: base(new MemoryStream(512), true)
{
stream = UnderlyingStream as MemoryStream;
}
protected override int BufferSize => 512;
public int CommitTo(SequentialFileWriter dataFile, SequentialFileWriter indexFile, int typeCode, uint serial)
{
Flush();
byte[] buffer = stream.GetBuffer();
int length = (int)stream.Length;
long position = dataFile.Position;
dataFile.Write(buffer, 0, length);
if (indexBuffer == null) indexBuffer = new byte[20];
indexBuffer[0] = (byte)typeCode;
indexBuffer[1] = (byte)(typeCode >> 8);
indexBuffer[2] = (byte)(typeCode >> 16);
indexBuffer[3] = (byte)(typeCode >> 24);
indexBuffer[4] = (byte)serial;
indexBuffer[5] = (byte)(serial >> 8);
indexBuffer[6] = (byte)(serial >> 16);
indexBuffer[7] = (byte)(serial >> 24);
indexBuffer[8] = (byte)position;
indexBuffer[9] = (byte)(position >> 8);
indexBuffer[10] = (byte)(position >> 16);
indexBuffer[11] = (byte)(position >> 24);
indexBuffer[12] = (byte)(position >> 32);
indexBuffer[13] = (byte)(position >> 40);
indexBuffer[14] = (byte)(position >> 48);
indexBuffer[15] = (byte)(position >> 56);
indexBuffer[16] = (byte)length;
indexBuffer[17] = (byte)(length >> 8);
indexBuffer[18] = (byte)(length >> 16);
indexBuffer[19] = (byte)(length >> 24);
indexFile.Write(indexBuffer, 0, indexBuffer.Length);
stream.SetLength(0);
return length;
}
}
}

View file

@ -0,0 +1,48 @@
/***************************************************************************
* DualSaveStrategy.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.Threading;
namespace Server
{
public sealed class DualSaveStrategy : StandardSaveStrategy
{
public override string Name => "Dual";
public override void Save(bool permitBackgroundWrite)
{
PermitBackgroundWrite = permitBackgroundWrite;
Thread saveThread = new Thread(delegate() { SaveItems(); });
saveThread.Name = "Item Save Subset";
saveThread.Start();
SaveMobiles();
SaveGuilds();
saveThread.Join();
if (permitBackgroundWrite && UseSequentialWriters
) //If we're permitted to write in the background, but we don't anyways, then notify.
World.NotifyDiskWriteComplete();
}
}
}

View file

@ -0,0 +1,278 @@
/***************************************************************************
* DynamicSaveStrategy.cs
* -------------------
* begin : December 16, 2010
* 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.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
using Server.Guilds;
namespace Server
{
public sealed class DynamicSaveStrategy : SaveStrategy
{
private ConcurrentBag<Item> _decayBag;
private SequentialFileWriter _guildData, _guildIndex;
private BlockingCollection<QueuedMemoryWriter> _guildThreadWriters;
private SequentialFileWriter _itemData, _itemIndex;
private BlockingCollection<QueuedMemoryWriter> _itemThreadWriters;
private SequentialFileWriter _mobileData, _mobileIndex;
private BlockingCollection<QueuedMemoryWriter> _mobileThreadWriters;
public DynamicSaveStrategy()
{
_decayBag = new ConcurrentBag<Item>();
_itemThreadWriters = new BlockingCollection<QueuedMemoryWriter>();
_mobileThreadWriters = new BlockingCollection<QueuedMemoryWriter>();
_guildThreadWriters = new BlockingCollection<QueuedMemoryWriter>();
}
public override string Name => "Dynamic";
public override void Save(bool permitBackgroundWrite)
{
OpenFiles();
Task[] saveTasks = new Task[3];
saveTasks[0] = SaveItems();
saveTasks[1] = SaveMobiles();
saveTasks[2] = SaveGuilds();
SaveTypeDatabases();
if (permitBackgroundWrite)
{
//This option makes it finish the writing to disk in the background, continuing even after Save() returns.
Task.Factory.ContinueWhenAll(saveTasks, _ =>
{
CloseFiles();
World.NotifyDiskWriteComplete();
});
}
else
{
Task.WaitAll(saveTasks); //Waits for the completion of all of the tasks(committing to disk)
CloseFiles();
}
}
private Task StartCommitTask(BlockingCollection<QueuedMemoryWriter> threadWriter, SequentialFileWriter data,
SequentialFileWriter index)
{
Task commitTask = Task.Factory.StartNew(() =>
{
while (!threadWriter.IsCompleted)
{
QueuedMemoryWriter writer;
try
{
writer = threadWriter.Take();
}
catch (InvalidOperationException)
{
//Per MSDN, it's fine if we're here, successful completion of adding can rarely put us into this state.
break;
}
writer.CommitTo(data, index);
}
});
return commitTask;
}
private Task SaveItems()
{
//Start the blocking consumer; this runs in background.
Task commitTask = StartCommitTask(_itemThreadWriters, _itemData, _itemIndex);
IEnumerable<Item> items = World.Items.Values;
//Start the producer.
Parallel.ForEach(items, () => new QueuedMemoryWriter(),
(item, state, writer) =>
{
long startPosition = writer.Position;
item.Serialize(writer);
int size = (int)(writer.Position - startPosition);
writer.QueueForIndex(item, size);
if (item.Decays && item.Parent == null && item.Map != Map.Internal &&
DateTime.UtcNow > item.LastMoved + item.DecayTime) _decayBag.Add(item);
return writer;
},
writer =>
{
writer.Flush();
_itemThreadWriters.Add(writer);
});
_itemThreadWriters.CompleteAdding(); //We only get here after the Parallel.ForEach completes. Lets our task
return commitTask;
}
private Task SaveMobiles()
{
//Start the blocking consumer; this runs in background.
Task commitTask = StartCommitTask(_mobileThreadWriters, _mobileData, _mobileIndex);
IEnumerable<Mobile> mobiles = World.Mobiles.Values;
//Start the producer.
Parallel.ForEach(mobiles, () => new QueuedMemoryWriter(),
(mobile, state, writer) =>
{
long startPosition = writer.Position;
mobile.Serialize(writer);
int size = (int)(writer.Position - startPosition);
writer.QueueForIndex(mobile, size);
return writer;
},
writer =>
{
writer.Flush();
_mobileThreadWriters.Add(writer);
});
_mobileThreadWriters
.CompleteAdding(); //We only get here after the Parallel.ForEach completes. Lets our task tell the consumer that we're done
return commitTask;
}
private Task SaveGuilds()
{
//Start the blocking consumer; this runs in background.
Task commitTask = StartCommitTask(_guildThreadWriters, _guildData, _guildIndex);
IEnumerable<BaseGuild> guilds = BaseGuild.List.Values;
//Start the producer.
Parallel.ForEach(guilds, () => new QueuedMemoryWriter(),
(guild, state, writer) =>
{
long startPosition = writer.Position;
guild.Serialize(writer);
int size = (int)(writer.Position - startPosition);
writer.QueueForIndex(guild, size);
return writer;
},
writer =>
{
writer.Flush();
_guildThreadWriters.Add(writer);
});
_guildThreadWriters.CompleteAdding(); //We only get here after the Parallel.ForEach completes. Lets our task
return commitTask;
}
public override void ProcessDecay()
{
while (_decayBag.TryTake(out Item item))
if (item.OnDecay())
item.Delete();
}
private void OpenFiles()
{
_itemData = new SequentialFileWriter(World.ItemDataPath);
_itemIndex = new SequentialFileWriter(World.ItemIndexPath);
_mobileData = new SequentialFileWriter(World.MobileDataPath);
_mobileIndex = new SequentialFileWriter(World.MobileIndexPath);
_guildData = new SequentialFileWriter(World.GuildDataPath);
_guildIndex = new SequentialFileWriter(World.GuildIndexPath);
WriteCount(_itemIndex, World.Items.Count);
WriteCount(_mobileIndex, World.Mobiles.Count);
WriteCount(_guildIndex, BaseGuild.List.Count);
}
private void CloseFiles()
{
_itemData.Close();
_itemIndex.Close();
_mobileData.Close();
_mobileIndex.Close();
_guildData.Close();
_guildIndex.Close();
}
private void WriteCount(SequentialFileWriter indexFile, int count)
{
//Equiv to GenericWriter.Write( (int)count );
byte[] buffer = new byte[4];
buffer[0] = (byte)count;
buffer[1] = (byte)(count >> 8);
buffer[2] = (byte)(count >> 16);
buffer[3] = (byte)(count >> 24);
indexFile.Write(buffer, 0, buffer.Length);
}
private void SaveTypeDatabases()
{
SaveTypeDatabase(World.ItemTypesPath, World.m_ItemTypes);
SaveTypeDatabase(World.MobileTypesPath, World.m_MobileTypes);
}
private void SaveTypeDatabase(string path, List<Type> types)
{
BinaryFileWriter bfw = new BinaryFileWriter(path, false);
bfw.Write(types.Count);
foreach (Type type in types) bfw.Write(type.FullName);
bfw.Flush();
bfw.Close();
}
}
}

View file

@ -0,0 +1,113 @@
/***************************************************************************
* FileOperations.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.IO;
#if !MONO
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
#endif
namespace Server
{
public static class FileOperations
{
public const int KB = 1024;
public const int MB = 1024 * KB;
public static int BufferSize{ get; set; } = 1 * MB;
public static int Concurrency{ get; set; } = 1;
public static bool Unbuffered{ get; set; } = true;
public static bool AreSynchronous => Concurrency < 1;
public static bool AreAsynchronous => Concurrency > 0;
public static FileStream OpenSequentialStream(string path, FileMode mode, FileAccess access, FileShare share)
{
FileOptions options = FileOptions.SequentialScan;
if (Concurrency > 0)
options |= FileOptions.Asynchronous;
#if MONO
return new FileStream( path, mode, access, share, BufferSize, options );
#else
if (Unbuffered)
options |= NoBuffering;
else
return new FileStream(path, mode, access, share, BufferSize, options);
SafeFileHandle fileHandle =
UnsafeNativeMethods.CreateFile(path, (int)access, share, IntPtr.Zero, mode, (int)options, IntPtr.Zero);
if (fileHandle.IsInvalid) throw new IOException();
return new UnbufferedFileStream(fileHandle, access, BufferSize, Concurrency > 0);
#endif
}
#if !MONO
private class UnbufferedFileStream : FileStream
{
private SafeFileHandle fileHandle;
public UnbufferedFileStream(SafeFileHandle fileHandle, FileAccess access, int bufferSize, bool isAsync)
: base(fileHandle, access, bufferSize, isAsync)
{
this.fileHandle = fileHandle;
}
public override void Write(byte[] array, int offset, int count)
{
base.Write(array, offset, BufferSize);
}
public override IAsyncResult BeginWrite(byte[] array, int offset, int numBytes, AsyncCallback userCallback,
object stateObject)
{
return base.BeginWrite(array, offset, BufferSize, userCallback, stateObject);
}
protected override void Dispose(bool disposing)
{
if (!fileHandle.IsClosed) fileHandle.Close();
base.Dispose(disposing);
}
}
#endif
#if !MONO
private const FileOptions NoBuffering = (FileOptions)0x20000000;
internal static class UnsafeNativeMethods
{
[DllImport("Kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, FileShare dwShareMode,
IntPtr securityAttrs, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
}
#endif
}
}

View file

@ -0,0 +1,231 @@
/***************************************************************************
* FileQueue.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.Buffers;
using System.Collections.Generic;
using System.Threading;
namespace Server
{
public delegate void FileCommitCallback(FileQueue.Chunk chunk);
public sealed class FileQueue : IDisposable
{
private static int bufferSize;
private Chunk[] active;
private int activeCount;
private Page buffered;
private FileCommitCallback callback;
private ManualResetEvent idle;
private Queue<Page> pending;
private object syncRoot;
static FileQueue()
{
bufferSize = FileOperations.BufferSize;
}
public FileQueue(int concurrentWrites, FileCommitCallback callback)
{
if (concurrentWrites < 1) throw new ArgumentOutOfRangeException("concurrentWrites");
if (bufferSize < 1)
throw new ArgumentOutOfRangeException("bufferSize");
syncRoot = new object();
active = new Chunk[concurrentWrites];
pending = new Queue<Page>();
this.callback = callback;
idle = new ManualResetEvent(true);
}
public long Position{ get; private set; }
public void Dispose()
{
if (idle != null)
{
idle.Close();
idle = null;
}
}
private void Append(Page page)
{
lock (syncRoot)
{
if (activeCount == 0) idle.Reset();
++activeCount;
for (int slot = 0; slot < active.Length; ++slot)
if (active[slot] == null)
{
active[slot] = new Chunk(this, slot, page.buffer, 0, page.length);
callback(active[slot]);
return;
}
pending.Enqueue(page);
}
}
public void Flush()
{
if (buffered.buffer != null)
{
Append(buffered);
buffered.buffer = null;
buffered.length = 0;
}
/*lock ( syncRoot ) {
if ( pending.Count > 0 ) {
idle.Reset();
}
for ( int slot = 0; slot < active.Length && pending.Count > 0; ++slot ) {
if ( active[slot] == null ) {
Page page = pending.Dequeue();
active[slot] = new Chunk( this, slot, page.buffer, 0, page.length );
++activeCount;
callback( active[slot] );
}
}
}*/
idle.WaitOne();
}
private void Commit(Chunk chunk, int slot)
{
if (slot < 0 || slot >= active.Length) throw new ArgumentOutOfRangeException("slot");
lock (syncRoot)
{
if (active[slot] != chunk) throw new ArgumentException();
ArrayPool<byte>.Shared.Return(chunk.Buffer);
if (pending.Count > 0)
{
Page page = pending.Dequeue();
active[slot] = new Chunk(this, slot, page.buffer, 0, page.length);
callback(active[slot]);
}
else
{
active[slot] = null;
}
--activeCount;
if (activeCount == 0) idle.Set();
}
}
public void Enqueue(byte[] buffer, int offset, int size)
{
if (buffer == null) throw new ArgumentNullException("buffer");
if (offset < 0) throw new ArgumentOutOfRangeException("offset");
if (size < 0) throw new ArgumentOutOfRangeException("size");
if (buffer.Length - offset < size) throw new ArgumentException();
Position += size;
while (size > 0)
{
if (buffered.buffer == null)
buffered.buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
byte[] page = buffered.buffer; // buffer page
int pageSpace = page.Length - buffered.length; // available bytes in page
int byteCount = size > pageSpace ? pageSpace : size; // how many bytes we can copy over
Buffer.BlockCopy(buffer, offset, page, buffered.length, byteCount);
buffered.length += byteCount;
offset += byteCount;
size -= byteCount;
if (buffered.length == page.Length)
{
// page full
Append(buffered);
buffered.buffer = null;
buffered.length = 0;
}
}
}
public sealed class Chunk
{
private int offset;
private FileQueue owner;
private int slot;
public Chunk(FileQueue owner, int slot, byte[] buffer, int offset, int size)
{
this.owner = owner;
this.slot = slot;
Buffer = buffer;
this.offset = offset;
Size = size;
}
public byte[] Buffer{ get; }
public int Offset => 0;
public int Size{ get; }
public void Commit()
{
owner.Commit(this, slot);
}
}
private struct Page
{
public byte[] buffer;
public int length;
}
}
}

View file

@ -0,0 +1,326 @@
/***************************************************************************
* ParallelSaveStrategy.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;
using System.Collections.Generic;
using System.Threading;
using Server.Guilds;
namespace Server
{
public sealed class ParallelSaveStrategy : SaveStrategy
{
private Queue<Item> _decayQueue;
private Consumer[] consumers;
private int cycle;
private bool finished;
private SequentialFileWriter guildData, guildIndex;
private SequentialFileWriter itemData, itemIndex;
private SequentialFileWriter mobileData, mobileIndex;
private int processorCount;
public ParallelSaveStrategy(int processorCount)
{
this.processorCount = processorCount;
_decayQueue = new Queue<Item>();
}
public override string Name => "Parallel";
private int GetThreadCount()
{
return processorCount - 1;
}
public override void Save(bool permitBackgroundWrite)
{
OpenFiles();
consumers = new Consumer[GetThreadCount()];
for (int i = 0; i < consumers.Length; ++i) consumers[i] = new Consumer(this, 256);
IEnumerable<ISerializable> collection = new Producer();
foreach (ISerializable value in collection)
while (!Enqueue(value))
if (!Commit())
Thread.Sleep(0);
finished = true;
SaveTypeDatabases();
WaitHandle.WaitAll(
Array.ConvertAll<Consumer, WaitHandle>(
consumers,
input => input.completionEvent
)
);
Commit();
CloseFiles();
}
public override void ProcessDecay()
{
while (_decayQueue.Count > 0)
{
Item item = _decayQueue.Dequeue();
if (item.OnDecay()) item.Delete();
}
}
private void SaveTypeDatabases()
{
SaveTypeDatabase(World.ItemTypesPath, World.m_ItemTypes);
SaveTypeDatabase(World.MobileTypesPath, World.m_MobileTypes);
}
private void SaveTypeDatabase(string path, List<Type> types)
{
BinaryFileWriter bfw = new BinaryFileWriter(path, false);
bfw.Write(types.Count);
foreach (Type type in types) bfw.Write(type.FullName);
bfw.Flush();
bfw.Close();
}
private void OpenFiles()
{
itemData = new SequentialFileWriter(World.ItemDataPath);
itemIndex = new SequentialFileWriter(World.ItemIndexPath);
mobileData = new SequentialFileWriter(World.MobileDataPath);
mobileIndex = new SequentialFileWriter(World.MobileIndexPath);
guildData = new SequentialFileWriter(World.GuildDataPath);
guildIndex = new SequentialFileWriter(World.GuildIndexPath);
WriteCount(itemIndex, World.Items.Count);
WriteCount(mobileIndex, World.Mobiles.Count);
WriteCount(guildIndex, BaseGuild.List.Count);
}
private void WriteCount(SequentialFileWriter indexFile, int count)
{
byte[] buffer = new byte[4];
buffer[0] = (byte)count;
buffer[1] = (byte)(count >> 8);
buffer[2] = (byte)(count >> 16);
buffer[3] = (byte)(count >> 24);
indexFile.Write(buffer, 0, buffer.Length);
}
private void CloseFiles()
{
itemData.Close();
itemIndex.Close();
mobileData.Close();
mobileIndex.Close();
guildData.Close();
guildIndex.Close();
World.NotifyDiskWriteComplete();
}
private void OnSerialized(ConsumableEntry entry)
{
ISerializable value = entry.value;
BinaryMemoryWriter writer = entry.writer;
if (value is Item item)
Save(item, writer);
else if (value is Mobile mob)
Save(mob, writer);
else if (value is BaseGuild guild)
Save(guild, writer);
}
private void Save(Item item, BinaryMemoryWriter writer)
{
writer.CommitTo(itemData, itemIndex, item.m_TypeRef, item.Serial);
if (item.Decays && item.Parent == null && item.Map != Map.Internal &&
DateTime.UtcNow > item.LastMoved + item.DecayTime) _decayQueue.Enqueue(item);
}
private void Save(Mobile mob, BinaryMemoryWriter writer)
{
writer.CommitTo(mobileData, mobileIndex, mob.m_TypeRef, mob.Serial);
}
private void Save(BaseGuild guild, BinaryMemoryWriter writer)
{
writer.CommitTo(guildData, guildIndex, 0, guild.Id);
}
private bool Enqueue(ISerializable value)
{
for (int i = 0; i < consumers.Length; ++i)
{
Consumer consumer = consumers[cycle++ % consumers.Length];
if (consumer.tail - consumer.head < consumer.buffer.Length)
{
consumer.buffer[consumer.tail % consumer.buffer.Length].value = value;
consumer.tail++;
return true;
}
}
return false;
}
private bool Commit()
{
bool committed = false;
for (int i = 0; i < consumers.Length; ++i)
{
Consumer consumer = consumers[i];
while (consumer.head < consumer.done)
{
OnSerialized(consumer.buffer[consumer.head % consumer.buffer.Length]);
consumer.head++;
committed = true;
}
}
return committed;
}
private sealed class Producer : IEnumerable<ISerializable>
{
private IEnumerable<BaseGuild> guilds;
private IEnumerable<Item> items;
private IEnumerable<Mobile> mobiles;
public Producer()
{
items = World.Items.Values;
mobiles = World.Mobiles.Values;
guilds = BaseGuild.List.Values;
}
public IEnumerator<ISerializable> GetEnumerator()
{
foreach (Item item in items) yield return item;
foreach (Mobile mob in mobiles) yield return mob;
foreach (BaseGuild guild in guilds) yield return guild;
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
private struct ConsumableEntry
{
public ISerializable value;
public BinaryMemoryWriter writer;
}
private sealed class Consumer
{
public ConsumableEntry[] buffer;
public ManualResetEvent completionEvent;
public int head, done, tail;
private ParallelSaveStrategy owner;
private Thread thread;
public Consumer(ParallelSaveStrategy owner, int bufferSize)
{
this.owner = owner;
buffer = new ConsumableEntry[bufferSize];
for (int i = 0; i < buffer.Length; ++i) buffer[i].writer = new BinaryMemoryWriter();
completionEvent = new ManualResetEvent(false);
thread = new Thread(Processor);
thread.Name = "Parallel Serialization Thread";
thread.Start();
}
private void Processor()
{
try
{
while (!owner.finished)
{
Process();
Thread.Sleep(0);
}
Process();
completionEvent.Set();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
private void Process()
{
ConsumableEntry entry;
while (done < tail)
{
entry = buffer[done % buffer.Length];
entry.value.Serialize(entry.writer);
++done;
}
}
}
}
}

View file

@ -0,0 +1,107 @@
#region References
using System;
using System.IO;
#endregion
namespace Server
{
public static class Persistence
{
public static void Serialize(string path, Action<GenericWriter> serializer)
{
Serialize(new FileInfo(path), serializer);
}
public static void Serialize(FileInfo file, Action<GenericWriter> serializer)
{
file.Refresh();
if (file.Directory?.Exists == false)
file.Directory.Create();
if (!file.Exists) file.Create().Close();
file.Refresh();
using (FileStream fs = file.OpenWrite())
{
BinaryFileWriter writer = new BinaryFileWriter(fs, true);
try
{
serializer(writer);
}
finally
{
writer.Flush();
writer.Close();
}
}
}
public static void Deserialize(string path, Action<GenericReader> deserializer)
{
Deserialize(path, deserializer, true);
}
public static void Deserialize(FileInfo file, Action<GenericReader> deserializer)
{
Deserialize(file, deserializer, true);
}
public static void Deserialize(string path, Action<GenericReader> deserializer, bool ensure)
{
Deserialize(new FileInfo(path), deserializer, ensure);
}
public static void Deserialize(FileInfo file, Action<GenericReader> deserializer, bool ensure)
{
file.Refresh();
if (file.Directory?.Exists == false)
{
if (!ensure)
throw new DirectoryNotFoundException();
file.Directory.Create();
}
if (!file.Exists)
{
if (!ensure)
throw new FileNotFoundException
{
Source = file.FullName
};
file.Create().Close();
}
file.Refresh();
using (FileStream fs = file.OpenRead())
{
BinaryFileReader reader = new BinaryFileReader(new BinaryReader(fs));
try
{
deserializer(reader);
}
catch (EndOfStreamException eos)
{
if (file.Length > 0) Console.WriteLine("[Persistence]: {0}", eos);
}
catch (Exception e)
{
Console.WriteLine("[Persistence]: {0}", e);
}
finally
{
reader.Close();
}
}
}
}
}

View file

@ -0,0 +1,116 @@
/***************************************************************************
* QueuedMemoryWriter.cs
* -------------------
* begin : December 16, 2010
* 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.Collections.Generic;
using System.IO;
namespace Server
{
public sealed class QueuedMemoryWriter : BinaryFileWriter
{
private MemoryStream _memStream;
private List<IndexInfo> _orderedIndexInfo = new List<IndexInfo>();
public QueuedMemoryWriter()
: base(new MemoryStream(1024 * 1024), true)
{
_memStream = UnderlyingStream as MemoryStream;
}
protected override int BufferSize => 512;
public void QueueForIndex(ISerializable serializable, int size)
{
IndexInfo info;
info.size = size;
info.typeCode = serializable.TypeReference; //For guilds, this will automagically be zero.
info.serial = serializable.SerialIdentity;
_orderedIndexInfo.Add(info);
}
public void CommitTo(SequentialFileWriter dataFile, SequentialFileWriter indexFile)
{
Flush();
int memLength = (int)_memStream.Position;
if (memLength > 0)
{
byte[] memBuffer = _memStream.GetBuffer();
long actualPosition = dataFile.Position;
dataFile.Write(memBuffer, 0, memLength); //The buffer contains the data from many items.
//Console.WriteLine("Writing {0} bytes starting at {1}, with {2} things", memLength, actualPosition, _orderedIndexInfo.Count);
byte[] indexBuffer = new byte[20];
//int indexWritten = _orderedIndexInfo.Count * indexBuffer.Length;
//int totalWritten = memLength + indexWritten
for (int i = 0; i < _orderedIndexInfo.Count; i++)
{
IndexInfo info = _orderedIndexInfo[i];
indexBuffer[0] = (byte)info.typeCode;
indexBuffer[1] = (byte)(info.typeCode >> 8);
indexBuffer[2] = (byte)(info.typeCode >> 16);
indexBuffer[3] = (byte)(info.typeCode >> 24);
indexBuffer[4] = (byte)info.serial;
indexBuffer[5] = (byte)(info.serial >> 8);
indexBuffer[6] = (byte)(info.serial >> 16);
indexBuffer[7] = (byte)(info.serial >> 24);
indexBuffer[8] = (byte)actualPosition;
indexBuffer[9] = (byte)(actualPosition >> 8);
indexBuffer[10] = (byte)(actualPosition >> 16);
indexBuffer[11] = (byte)(actualPosition >> 24);
indexBuffer[12] = (byte)(actualPosition >> 32);
indexBuffer[13] = (byte)(actualPosition >> 40);
indexBuffer[14] = (byte)(actualPosition >> 48);
indexBuffer[15] = (byte)(actualPosition >> 56);
indexBuffer[16] = (byte)info.size;
indexBuffer[17] = (byte)(info.size >> 8);
indexBuffer[18] = (byte)(info.size >> 16);
indexBuffer[19] = (byte)(info.size >> 24);
indexFile.Write(indexBuffer, 0, indexBuffer.Length);
actualPosition += info.size;
}
}
Close(); //We're done with this writer.
}
private struct IndexInfo
{
public int size;
public int typeCode;
public uint serial;
}
}
}

View file

@ -0,0 +1,47 @@
/***************************************************************************
* SaveStrategy.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.
*
***************************************************************************/
namespace Server
{
public abstract class SaveStrategy
{
public abstract string Name{ get; }
public static SaveStrategy Acquire()
{
if (Core.MultiProcessor)
{
int processorCount = Core.ProcessorCount;
if (processorCount > 2)
return
new DualSaveStrategy(); // return new DynamicSaveStrategy(); (4.0 or return new ParallelSaveStrategy(processorCount); (2.0)
return new DualSaveStrategy();
}
return new StandardSaveStrategy();
}
public abstract void Save(bool permitBackgroundWrite);
public abstract void ProcessDecay();
}
}

View file

@ -0,0 +1,126 @@
/***************************************************************************
* SequentialFileWriter.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.IO;
namespace Server
{
public sealed class SequentialFileWriter : Stream
{
private FileQueue fileQueue;
private FileStream fileStream;
private AsyncCallback writeCallback;
public SequentialFileWriter(string path)
{
if (path == null) throw new ArgumentNullException("path");
fileStream = FileOperations.OpenSequentialStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
fileQueue = new FileQueue(
Math.Max(1, FileOperations.Concurrency),
FileCallback
);
}
public override long Position
{
get => fileQueue.Position;
set => throw new InvalidOperationException();
}
public override bool CanRead => false;
public override bool CanSeek => false;
public override bool CanWrite => true;
public override long Length => Position;
private void FileCallback(FileQueue.Chunk chunk)
{
if (FileOperations.AreSynchronous)
{
fileStream.Write(chunk.Buffer, chunk.Offset, chunk.Size);
chunk.Commit();
}
else
{
if (writeCallback == null) writeCallback = OnWrite;
fileStream.BeginWrite(chunk.Buffer, chunk.Offset, chunk.Size, writeCallback, chunk);
}
}
private void OnWrite(IAsyncResult asyncResult)
{
FileQueue.Chunk chunk = asyncResult.AsyncState as FileQueue.Chunk;
fileStream.EndWrite(asyncResult);
chunk.Commit();
}
public override void Write(byte[] buffer, int offset, int size)
{
fileQueue.Enqueue(buffer, offset, size);
}
public override void Flush()
{
fileQueue.Flush();
fileStream.Flush();
}
protected override void Dispose(bool disposing)
{
if (fileStream != null)
{
Flush();
fileQueue.Dispose();
fileQueue = null;
fileStream.Close();
fileStream = null;
}
base.Dispose(disposing);
}
public override int Read(byte[] buffer, int offset, int count)
{
throw new InvalidOperationException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new InvalidOperationException();
}
public override void SetLength(long value)
{
fileStream.SetLength(value);
}
}
}

View file

@ -0,0 +1,206 @@
/***************************************************************************
* StandardSaveStrategy.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 Server.Guilds;
namespace Server
{
public class StandardSaveStrategy : SaveStrategy
{
public enum SaveOption
{
Normal,
Threaded
}
public static SaveOption SaveType = SaveOption.Normal;
private Queue<Item> _decayQueue;
public StandardSaveStrategy()
{
_decayQueue = new Queue<Item>();
}
public override string Name => "Standard";
protected bool PermitBackgroundWrite{ get; set; }
protected bool UseSequentialWriters => SaveType == SaveOption.Normal || !PermitBackgroundWrite;
public override void Save(bool permitBackgroundWrite)
{
PermitBackgroundWrite = permitBackgroundWrite;
SaveMobiles();
SaveItems();
SaveGuilds();
if (permitBackgroundWrite && UseSequentialWriters
) //If we're permitted to write in the background, but we don't anyways, then notify.
World.NotifyDiskWriteComplete();
}
protected void SaveMobiles()
{
Dictionary<Serial, Mobile> mobiles = World.Mobiles;
GenericWriter idx;
GenericWriter tdb;
GenericWriter bin;
if (UseSequentialWriters)
{
idx = new BinaryFileWriter(World.MobileIndexPath, false);
tdb = new BinaryFileWriter(World.MobileTypesPath, false);
bin = new BinaryFileWriter(World.MobileDataPath, true);
}
else
{
idx = new AsyncWriter(World.MobileIndexPath, false);
tdb = new AsyncWriter(World.MobileTypesPath, false);
bin = new AsyncWriter(World.MobileDataPath, true);
}
idx.Write(mobiles.Count);
foreach (Mobile m in mobiles.Values)
{
long start = bin.Position;
idx.Write(m.m_TypeRef);
idx.Write(m.Serial);
idx.Write(start);
m.Serialize(bin);
idx.Write((int)(bin.Position - start));
m.FreeCache();
}
tdb.Write(World.m_MobileTypes.Count);
for (int i = 0; i < World.m_MobileTypes.Count; ++i)
tdb.Write(World.m_MobileTypes[i].FullName);
idx.Close();
tdb.Close();
bin.Close();
}
protected void SaveItems()
{
Dictionary<Serial, Item> items = World.Items;
GenericWriter idx;
GenericWriter tdb;
GenericWriter bin;
if (UseSequentialWriters)
{
idx = new BinaryFileWriter(World.ItemIndexPath, false);
tdb = new BinaryFileWriter(World.ItemTypesPath, false);
bin = new BinaryFileWriter(World.ItemDataPath, true);
}
else
{
idx = new AsyncWriter(World.ItemIndexPath, false);
tdb = new AsyncWriter(World.ItemTypesPath, false);
bin = new AsyncWriter(World.ItemDataPath, true);
}
idx.Write(items.Count);
DateTime n = DateTime.UtcNow;
foreach (Item item in items.Values)
{
if (item.Decays && item.Parent == null && item.Map != Map.Internal && item.LastMoved + item.DecayTime <= n)
_decayQueue.Enqueue(item);
long start = bin.Position;
idx.Write(item.m_TypeRef);
idx.Write(item.Serial);
idx.Write(start);
item.Serialize(bin);
idx.Write((int)(bin.Position - start));
item.FreeCache();
}
tdb.Write(World.m_ItemTypes.Count);
for (int i = 0; i < World.m_ItemTypes.Count; ++i)
tdb.Write(World.m_ItemTypes[i].FullName);
idx.Close();
tdb.Close();
bin.Close();
}
protected void SaveGuilds()
{
GenericWriter idx;
GenericWriter bin;
if (UseSequentialWriters)
{
idx = new BinaryFileWriter(World.GuildIndexPath, false);
bin = new BinaryFileWriter(World.GuildDataPath, true);
}
else
{
idx = new AsyncWriter(World.GuildIndexPath, false);
bin = new AsyncWriter(World.GuildDataPath, true);
}
idx.Write(BaseGuild.List.Count);
foreach (BaseGuild guild in BaseGuild.List.Values)
{
long start = bin.Position;
idx.Write(0); //guilds have no typeid
idx.Write(guild.Id);
idx.Write(start);
guild.Serialize(bin);
idx.Write((int)(bin.Position - start));
}
idx.Close();
bin.Close();
}
public override void ProcessDecay()
{
while (_decayQueue.Count > 0)
{
Item item = _decayQueue.Dequeue();
if (item.OnDecay()) item.Delete();
}
}
}
}