#W# Initial Commit: Avatars Conquest

This commit is contained in:
WarrentyExpired 2026-07-03 20:19:48 -04:00
commit 8eae46895e
7512 changed files with 416187 additions and 0 deletions

View file

@ -0,0 +1,86 @@
/***************************************************************************
* 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;
using System.IO;
using System.Collections.Generic;
using System.Text;
namespace Server {
public sealed class BinaryMemoryWriter : BinaryFileWriter {
private MemoryStream stream;
protected override int BufferSize {
get { return 512; }
}
public BinaryMemoryWriter()
: base( new MemoryStream( 512 ), true ) {
this.stream = this.UnderlyingStream as MemoryStream;
}
private static byte[] indexBuffer;
public int CommitTo( SequentialFileWriter dataFile, SequentialFileWriter indexFile, int typeCode, int 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,60 @@
/***************************************************************************
* 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;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using System.Diagnostics;
using Server;
using Server.Guilds;
namespace Server {
public sealed class DualSaveStrategy : StandardSaveStrategy {
public override string Name {
get { return "Dual"; }
}
public DualSaveStrategy() {
}
public override void Save( SaveMetrics metrics, bool permitBackgroundWrite )
{
this.PermitBackgroundWrite = permitBackgroundWrite;
Thread saveThread = new Thread( delegate() {
SaveItems(metrics);
} );
saveThread.Name = "Item Save Subset";
saveThread.Start();
SaveMobiles(metrics);
SaveGuilds(metrics);
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,314 @@
/***************************************************************************
* 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.
*
***************************************************************************/
#if Framework_4_0
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Collections.Concurrent;
using System.Linq;
using Server;
using Server.Guilds;
namespace Server
{
public sealed class DynamicSaveStrategy : SaveStrategy
{
public override string Name { get { return "Dynamic"; } }
private SaveMetrics _metrics;
private SequentialFileWriter _itemData, _itemIndex;
private SequentialFileWriter _mobileData, _mobileIndex;
private SequentialFileWriter _guildData, _guildIndex;
private ConcurrentBag<Item> _decayBag;
private BlockingCollection<QueuedMemoryWriter> _itemThreadWriters;
private BlockingCollection<QueuedMemoryWriter> _mobileThreadWriters;
private BlockingCollection<QueuedMemoryWriter> _guildThreadWriters;
public DynamicSaveStrategy()
{
_decayBag = new ConcurrentBag<Item>();
_itemThreadWriters = new BlockingCollection<QueuedMemoryWriter>();
_mobileThreadWriters = new BlockingCollection<QueuedMemoryWriter>();
_guildThreadWriters = new BlockingCollection<QueuedMemoryWriter>();
}
public override void Save(SaveMetrics metrics, bool permitBackgroundWrite)
{
this._metrics = metrics;
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 item, ParallelLoopState state, QueuedMemoryWriter 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.Now > (item.LastMoved + item.DecayTime))
{
_decayBag.Add(item);
}
if (_metrics != null)
{
_metrics.OnItemSaved(size);
}
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 mobile, ParallelLoopState state, QueuedMemoryWriter writer) =>
{
long startPosition = writer.Position;
mobile.Serialize(writer);
int size = (int)(writer.Position - startPosition);
writer.QueueForIndex(mobile, size);
if (_metrics != null)
{
_metrics.OnMobileSaved(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(),
(BaseGuild guild, ParallelLoopState state, QueuedMemoryWriter writer) =>
{
long startPosition = writer.Position;
guild.Serialize(writer);
int size = (int)(writer.Position - startPosition );
writer.QueueForIndex(guild, size);
if (_metrics != null)
{
_metrics.OnGuildSaved(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()
{
Item item;
while( _decayBag.TryTake( out item ) )
{
item.Delete();
}
}
private void OpenFiles()
{
_itemData = new SequentialFileWriter(World.ItemDataPath, _metrics);
_itemIndex = new SequentialFileWriter(World.ItemIndexPath, _metrics);
_mobileData = new SequentialFileWriter(World.MobileDataPath, _metrics);
_mobileIndex = new SequentialFileWriter(World.MobileIndexPath, _metrics);
_guildData = new SequentialFileWriter(World.GuildDataPath, _metrics);
_guildIndex = new SequentialFileWriter(World.GuildIndexPath, _metrics);
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();
}
}
}
#endif

View file

@ -0,0 +1,141 @@
/***************************************************************************
* 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;
using System.Collections.Generic;
using System.Text;
#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;
#if !MONO
private const FileOptions NoBuffering = ( FileOptions ) 0x20000000;
[DllImport( "Kernel32", CharSet = CharSet.Auto, SetLastError = true )]
private static extern SafeFileHandle CreateFile( string lpFileName, int dwDesiredAccess, FileShare dwShareMode, IntPtr securityAttrs, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile );
#endif
private static int bufferSize = 1 * MB;
private static int concurrency = 1;
private static bool unbuffered = true;
public static int BufferSize {
get {
return bufferSize;
}
set {
bufferSize = value;
}
}
public static int Concurrency {
get {
return concurrency;
}
set {
concurrency = value;
}
}
public static bool Unbuffered {
get {
return unbuffered;
}
set {
unbuffered = value;
}
}
public static bool AreSynchronous {
get {
return ( concurrency < 1 );
}
}
public static bool AreAsynchronous {
get {
return ( 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 = 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
}
}

View file

@ -0,0 +1,251 @@
/***************************************************************************
* 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.IO;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using Server;
using Server.Network;
namespace Server {
public delegate void FileCommitCallback( FileQueue.Chunk chunk );
public sealed class FileQueue : IDisposable {
public sealed class Chunk {
private FileQueue owner;
private int slot;
private byte[] buffer;
private int offset;
private int size;
public byte[] Buffer {
get {
return buffer;
}
}
public int Offset {
get {
return 0;
}
}
public int Size {
get {
return size;
}
}
public Chunk( FileQueue owner, int slot, byte[] buffer, int offset, int size ) {
this.owner = owner;
this.slot = slot;
this.buffer = buffer;
this.offset = offset;
this.size = size;
}
public void Commit() {
owner.Commit( this, this.slot );
}
}
private struct Page {
public byte[] buffer;
public int length;
}
private static int bufferSize;
private static BufferPool bufferPool;
static FileQueue() {
bufferSize = FileOperations.BufferSize;
bufferPool = new BufferPool( "File Buffers", 64, bufferSize );
}
private object syncRoot;
private Chunk[] active;
private int activeCount;
private Queue<Page> pending;
private Page buffered;
private FileCommitCallback callback;
private ManualResetEvent idle;
private long position;
public long Position {
get {
return position;
}
}
public FileQueue( int concurrentWrites, FileCommitCallback callback ) {
if ( concurrentWrites < 1 ) {
throw new ArgumentOutOfRangeException( "concurrentWrites" );
} else if ( bufferSize < 1 ) {
throw new ArgumentOutOfRangeException( "bufferSize" );
} else if ( callback == null ) {
throw new ArgumentNullException( "callback" );
}
this.syncRoot = new object();
this.active = new Chunk[concurrentWrites];
this.pending = new Queue<Page>();
this.callback = callback;
this.idle = new ManualResetEvent( true );
}
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 Dispose() {
if ( idle != null ) {
idle.Close();
idle = null;
}
}
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();
}
bufferPool.ReleaseBuffer( 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" );
} else if ( offset < 0 ) {
throw new ArgumentOutOfRangeException( "offset" );
} else if ( size < 0 ) {
throw new ArgumentOutOfRangeException( "size" );
} else if ( ( buffer.Length - offset ) < size ) {
throw new ArgumentException();
}
position += size;
while ( size > 0 ) {
if ( buffered.buffer == null ) { // nothing yet buffered
buffered.buffer = bufferPool.AcquireBuffer();
}
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;
}
}
}
}
}

View file

@ -0,0 +1,345 @@
/***************************************************************************
* 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.Text;
using System.IO;
using System.Threading;
using System.Diagnostics;
using Server;
using Server.Guilds;
namespace Server {
public sealed class ParallelSaveStrategy : SaveStrategy {
public override string Name {
get { return "Parallel"; }
}
private int processorCount;
public ParallelSaveStrategy( int processorCount ) {
this.processorCount = processorCount;
_decayQueue = new Queue<Item>();
}
private int GetThreadCount() {
return processorCount - 1;
}
private SaveMetrics metrics;
private SequentialFileWriter itemData, itemIndex;
private SequentialFileWriter mobileData, mobileIndex;
private SequentialFileWriter guildData, guildIndex;
private Queue<Item> _decayQueue;
private Consumer[] consumers;
private int cycle;
private bool finished;
public override void Save(SaveMetrics metrics, bool permitBackgroundWrite)
{
this.metrics = metrics;
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,
delegate( Consumer input ) {
return 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, metrics );
itemIndex = new SequentialFileWriter( World.ItemIndexPath, metrics );
mobileData = new SequentialFileWriter( World.MobileDataPath, metrics );
mobileIndex = new SequentialFileWriter( World.MobileIndexPath, metrics );
guildData = new SequentialFileWriter( World.GuildDataPath, metrics );
guildIndex = new SequentialFileWriter( World.GuildIndexPath, metrics );
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;
Item item = value as Item;
if ( item != null ) {
Save( item, writer );
} else {
Mobile mob = value as Mobile;
if ( mob != null ) {
Save( mob, writer );
} else {
BaseGuild guild = value as BaseGuild;
if ( guild != null ) {
Save( guild, writer );
}
}
}
}
private void Save( Item item, BinaryMemoryWriter writer ) {
int length = writer.CommitTo( itemData, itemIndex, item.m_TypeRef, item.Serial );
if ( metrics != null ) {
metrics.OnItemSaved( length );
}
if ( item.Decays && item.Parent == null && item.Map != Map.Internal && DateTime.Now > ( item.LastMoved + item.DecayTime ) ) {
_decayQueue.Enqueue( item );
}
}
private void Save( Mobile mob, BinaryMemoryWriter writer ) {
int length = writer.CommitTo( mobileData, mobileIndex, mob.m_TypeRef, mob.Serial );
if ( metrics != null ) {
metrics.OnMobileSaved( length );
}
}
private void Save( BaseGuild guild, BinaryMemoryWriter writer ) {
int length = writer.CommitTo( guildData, guildIndex, 0, guild.Id );
if ( metrics != null ) {
metrics.OnGuildSaved( length );
}
}
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<Item> items;
private IEnumerable<Mobile> mobiles;
private IEnumerable<BaseGuild> guilds;
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 {
private ParallelSaveStrategy owner;
public ManualResetEvent completionEvent;
public ConsumableEntry[] buffer;
public int head, done, tail;
private Thread thread;
public Consumer( ParallelSaveStrategy owner, int bufferSize ) {
this.owner = owner;
this.buffer = new ConsumableEntry[bufferSize];
for ( int i = 0; i < this.buffer.Length; ++i ) {
this.buffer[i].writer = new BinaryMemoryWriter();
}
this.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,126 @@
/***************************************************************************
* 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;
using System.IO;
using System.Collections.Generic;
using System.Text;
namespace Server
{
public sealed class QueuedMemoryWriter : BinaryFileWriter
{
private struct IndexInfo
{
public int size;
public int typeCode;
public int serial;
}
private MemoryStream _memStream;
private List<IndexInfo> _orderedIndexInfo = new List<IndexInfo>();
protected override int BufferSize
{
get { return 512; }
}
public QueuedMemoryWriter()
: base(new MemoryStream(1024 * 1024), true)
{
this._memStream = this.UnderlyingStream as MemoryStream;
}
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)
{
this.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];
int typeCode = info.typeCode;
int serial = info.serial;
int length = info.size;
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;
}
}
this.Close(); //We're done with this writer.
}
}
}

View file

@ -0,0 +1,131 @@
/***************************************************************************
* SaveMetrics.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.Diagnostics;
using System.Text;
namespace Server {
public sealed class SaveMetrics : IDisposable {
private const string PerformanceCategoryName = "Ultima";
private const string PerformanceCategoryDesc = "Performance counters for Ultima.";
private PerformanceCounter numberOfWorldSaves;
private PerformanceCounter itemsPerSecond;
private PerformanceCounter mobilesPerSecond;
private PerformanceCounter serializedBytesPerSecond;
private PerformanceCounter writtenBytesPerSecond;
public SaveMetrics() {
if ( !PerformanceCounterCategory.Exists( PerformanceCategoryName ) ) {
CounterCreationDataCollection counters = new CounterCreationDataCollection();
counters.Add( new CounterCreationData(
"Save - Count",
"Number of world saves.",
PerformanceCounterType.NumberOfItems32
)
);
counters.Add( new CounterCreationData(
"Save - Items/sec",
"Number of items saved per second.",
PerformanceCounterType.RateOfCountsPerSecond32
)
);
counters.Add( new CounterCreationData(
"Save - Mobiles/sec",
"Number of mobiles saved per second.",
PerformanceCounterType.RateOfCountsPerSecond32
)
);
counters.Add( new CounterCreationData(
"Save - Serialized bytes/sec",
"Amount of world-save bytes serialized per second.",
PerformanceCounterType.RateOfCountsPerSecond32
)
);
counters.Add( new CounterCreationData(
"Save - Written bytes/sec",
"Amount of world-save bytes written to disk per second.",
PerformanceCounterType.RateOfCountsPerSecond32
)
);
#if !MONO
PerformanceCounterCategory.Create( PerformanceCategoryName, PerformanceCategoryDesc, PerformanceCounterCategoryType.SingleInstance, counters );
#endif
}
numberOfWorldSaves = new PerformanceCounter( PerformanceCategoryName, "Save - Count", false );
itemsPerSecond = new PerformanceCounter( PerformanceCategoryName, "Save - Items/sec", false );
mobilesPerSecond = new PerformanceCounter( PerformanceCategoryName, "Save - Mobiles/sec", false );
serializedBytesPerSecond = new PerformanceCounter( PerformanceCategoryName, "Save - Serialized bytes/sec", false );
writtenBytesPerSecond = new PerformanceCounter( PerformanceCategoryName, "Save - Written bytes/sec", false );
// increment number of world saves
numberOfWorldSaves.Increment();
}
public void OnItemSaved( int numberOfBytes ) {
itemsPerSecond.Increment();
serializedBytesPerSecond.IncrementBy( numberOfBytes );
}
public void OnMobileSaved( int numberOfBytes ) {
mobilesPerSecond.Increment();
serializedBytesPerSecond.IncrementBy( numberOfBytes );
}
public void OnGuildSaved( int numberOfBytes ) {
serializedBytesPerSecond.IncrementBy( numberOfBytes );
}
public void OnFileWritten( int numberOfBytes ) {
writtenBytesPerSecond.IncrementBy( numberOfBytes );
}
private bool isDisposed;
public void Dispose() {
if ( !isDisposed ) {
isDisposed = true;
numberOfWorldSaves.Dispose();
itemsPerSecond.Dispose();
mobilesPerSecond.Dispose();
serializedBytesPerSecond.Dispose();
writtenBytesPerSecond.Dispose();
}
}
}
}

View file

@ -0,0 +1,58 @@
/***************************************************************************
* 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.
*
***************************************************************************/
using System;
using Server;
namespace Server
{
public abstract class SaveStrategy
{
public static SaveStrategy Acquire()
{
if (Core.MultiProcessor)
{
int processorCount = Core.ProcessorCount;
if (processorCount > 16)
{
#if Framework_4_0
return new DynamicSaveStrategy();
#else
return new ParallelSaveStrategy(processorCount);
#endif
}
else
{
return new DualSaveStrategy();
}
}
else
{
return new StandardSaveStrategy();
}
}
public abstract string Name { get; }
public abstract void Save(SaveMetrics metrics, bool permitBackgroundWrite);
public abstract void ProcessDecay();
}
}

View file

@ -0,0 +1,141 @@
/***************************************************************************
* 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;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Server {
public sealed class SequentialFileWriter : Stream {
private FileStream fileStream;
private FileQueue fileQueue;
private AsyncCallback writeCallback;
private SaveMetrics metrics;
public SequentialFileWriter( string path, SaveMetrics metrics ) {
if ( path == null ) {
throw new ArgumentNullException( "path" );
}
this.metrics = metrics;
this.fileStream = FileOperations.OpenSequentialStream( path, FileMode.Create, FileAccess.Write, FileShare.None );
fileQueue = new FileQueue(
Math.Max( 1, FileOperations.Concurrency ),
FileCallback
);
}
public override long Position {
get {
return fileQueue.Position;
}
set {
throw new InvalidOperationException();
}
}
private void FileCallback( FileQueue.Chunk chunk ) {
if ( FileOperations.AreSynchronous ) {
fileStream.Write( chunk.Buffer, chunk.Offset, chunk.Size );
if ( metrics != null ) {
metrics.OnFileWritten( chunk.Size );
}
chunk.Commit();
} else {
if ( writeCallback == null ) {
writeCallback = this.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 );
if ( metrics != null ) {
metrics.OnFileWritten( chunk.Size );
}
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 bool CanRead {
get { return false; }
}
public override bool CanSeek {
get { return false; }
}
public override bool CanWrite {
get { return true; }
}
public override long Length {
get { return this.Position; }
}
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,204 @@
/***************************************************************************
* 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 System.Text;
using System.IO;
using System.Threading;
using System.Diagnostics;
using Server;
using Server.Guilds;
namespace Server {
public class StandardSaveStrategy : SaveStrategy {
public override string Name {
get { return "Standard"; }
}
private Queue<Item> _decayQueue;
private bool _permitBackgroundWrite;
public StandardSaveStrategy() {
_decayQueue = new Queue<Item>();
}
protected bool PermitBackgroundWrite { get { return _permitBackgroundWrite; } set { _permitBackgroundWrite = value; } }
protected bool UseSequentialWriters { get { return (World.SaveType == World.SaveOption.Normal || !_permitBackgroundWrite); } }
public override void Save(SaveMetrics metrics, bool permitBackgroundWrite)
{
_permitBackgroundWrite = permitBackgroundWrite;
SaveMobiles(metrics);
SaveItems(metrics);
SaveGuilds(metrics);
if (permitBackgroundWrite && UseSequentialWriters) //If we're permitted to write in the background, but we don't anyways, then notify.
World.NotifyDiskWriteComplete();
}
protected void SaveMobiles(SaveMetrics metrics)
{
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( ( int ) mobiles.Count );
foreach ( Mobile m in mobiles.Values ) {
long start = bin.Position;
idx.Write( ( int ) m.m_TypeRef );
idx.Write( ( int ) m.Serial );
idx.Write( ( long ) start );
m.Serialize( bin );
if ( metrics != null ) {
metrics.OnMobileSaved( ( int ) ( bin.Position - start ) );
}
idx.Write( ( int ) ( bin.Position - start ) );
m.FreeCache();
}
tdb.Write( ( int ) 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(SaveMetrics metrics)
{
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( ( int ) items.Count );
foreach ( Item item in items.Values ) {
if ( item.Decays && item.Parent == null && item.Map != Map.Internal && ( item.LastMoved + item.DecayTime ) <= DateTime.Now ) {
_decayQueue.Enqueue( item );
}
long start = bin.Position;
idx.Write( ( int ) item.m_TypeRef );
idx.Write( ( int ) item.Serial );
idx.Write( ( long ) start );
item.Serialize( bin );
if ( metrics != null ) {
metrics.OnItemSaved( ( int ) ( bin.Position - start ) );
}
idx.Write( ( int ) ( bin.Position - start ) );
item.FreeCache();
}
tdb.Write( ( int ) 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(SaveMetrics metrics)
{
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( ( int ) BaseGuild.List.Count );
foreach ( BaseGuild guild in BaseGuild.List.Values ) {
long start = bin.Position;
idx.Write( ( int ) 0 );//guilds have no typeid
idx.Write( ( int ) guild.Id );
idx.Write( ( long ) start );
guild.Serialize( bin );
if ( metrics != null ) {
metrics.OnGuildSaved( ( int ) ( bin.Position - start ) );
}
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();
}
}
}
}
}