This commit is contained in:
mark 2006-06-15 04:14:30 +00:00
commit 47711d616e
2644 changed files with 479454 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: ScriptCompiler.cs 130 2006-04-11 01:06:32Z asayre $
*
***************************************************************************/
/***************************************************************************
*
* 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 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,54 @@
/***************************************************************************
* DualSaveStrategy.cs
* -------------------
* begin : May 1, 2002
* copyright : (C) The RunUO Software Team
* email : info@runuo.com
*
* $Id: ScriptCompiler.cs 130 2006-04-11 01:06:32Z asayre $
*
***************************************************************************/
/***************************************************************************
*
* 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 ) {
Thread saveThread = new Thread( delegate() {
SaveItems( metrics );
} );
saveThread.Name = "Item Save Subset";
saveThread.Start();
SaveMobiles( metrics );
SaveGuilds( metrics );
saveThread.Join();
}
}
}

View file

@ -0,0 +1,141 @@
/***************************************************************************
* FileOperations.cs
* -------------------
* begin : May 1, 2002
* copyright : (C) The RunUO Software Team
* email : info@runuo.com
*
* $Id: ScriptCompiler.cs 130 2006-04-11 01:06:32Z asayre $
*
***************************************************************************/
/***************************************************************************
*
* 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: SendQueue.cs 43 2006-01-20 05:39:57Z krrios $
*
***************************************************************************/
/***************************************************************************
*
* 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,342 @@
/***************************************************************************
* ParallelSaveStrategy.cs
* -------------------
* begin : May 1, 2002
* copyright : (C) The RunUO Software Team
* email : info@runuo.com
*
* $Id: ScriptCompiler.cs 130 2006-04-11 01:06:32Z asayre $
*
***************************************************************************/
/***************************************************************************
*
* 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;
}
private int GetThreadCount() {
return processorCount - 1;
}
private SaveMetrics metrics;
private SequentialFileWriter itemData, itemIndex;
private SequentialFileWriter mobileData, mobileIndex;
private SequentialFileWriter guildData, guildIndex;
private List<Item> decaying;
private Consumer[] consumers;
private int cycle;
private bool finished;
public override void Save( SaveMetrics metrics ) {
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();
if ( decaying != null ) {
foreach ( Item item in decaying ) {
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();
}
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 ) ) {
if ( decaying == null ) {
decaying = new List<Item>();
}
decaying.Add( item );
}
}
private void Save( Mobile mob, BinaryMemoryWriter writer ) {
int length = writer.CommitTo( mobileData, mobileIndex, mob.m_TypeRef, mob.Serial );
if ( metrics != null ) {
metrics.OnItemSaved( 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,129 @@
/***************************************************************************
* SaveMetrics.cs
* -------------------
* begin : May 1, 2002
* copyright : (C) The RunUO Software Team
* email : info@runuo.com
*
* $Id: ScriptCompiler.cs 130 2006-04-11 01:06:32Z asayre $
*
***************************************************************************/
/***************************************************************************
*
* 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.Diagnostics;
namespace Server {
public sealed class SaveMetrics : IDisposable {
private const string PerformanceCategoryName = "RunUO 2.0";
private const string PerformanceCategoryDesc = "Performance counters for RunUO 2.0.";
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
)
);
PerformanceCounterCategory.Create( PerformanceCategoryName, PerformanceCategoryDesc, PerformanceCounterCategoryType.SingleInstance, counters );
}
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,50 @@
/***************************************************************************
* SaveStrategy.cs
* -------------------
* begin : May 1, 2002
* copyright : (C) The RunUO Software Team
* email : info@runuo.com
*
* $Id: ScriptCompiler.cs 130 2006-04-11 01:06:32Z asayre $
*
***************************************************************************/
/***************************************************************************
*
* 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 abstract class SaveStrategy {
public static SaveStrategy Acquire() {
if ( Core.MultiProcessor ) {
int processorCount = Core.ProcessorCount;
if ( processorCount > 8 ) {
return new ParallelSaveStrategy( processorCount );
} else {
return new DualSaveStrategy();
}
} else {
return new StandardSaveStrategy();
}
}
public abstract string Name { get; }
public abstract void Save( SaveMetrics metrics );
}
}

View file

@ -0,0 +1,141 @@
/***************************************************************************
* SequentialFileWriter.cs
* -------------------
* begin : May 1, 2002
* copyright : (C) The RunUO Software Team
* email : info@runuo.com
*
* $Id: ScriptCompiler.cs 130 2006-04-11 01:06:32Z asayre $
*
***************************************************************************/
/***************************************************************************
*
* 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,181 @@
/***************************************************************************
* StandardSaveStrategy.cs
* -------------------
* begin : May 1, 2002
* copyright : (C) The RunUO Software Team
* email : info@runuo.com
*
* $Id: ScriptCompiler.cs 130 2006-04-11 01:06:32Z asayre $
*
***************************************************************************/
/***************************************************************************
*
* 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"; }
}
public StandardSaveStrategy() {
}
public override void Save( SaveMetrics metrics ) {
SaveMobiles( metrics );
SaveItems( metrics );
SaveGuilds( metrics );
}
protected void SaveMobiles( SaveMetrics metrics ) {
Dictionary<Serial, Mobile> mobiles = World.Mobiles;
GenericWriter idx;
GenericWriter tdb;
GenericWriter bin;
if ( World.SaveType == World.SaveOption.Normal ) {
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;
List<Item> decaying = new List<Item>();
GenericWriter idx;
GenericWriter tdb;
GenericWriter bin;
if ( World.SaveType == World.SaveOption.Normal ) {
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 )
decaying.Add( 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();
for ( int i = 0; i < decaying.Count; ++i ) {
Item item = decaying[i];
if ( item.OnDecay() )
item.Delete();
}
}
protected void SaveGuilds( SaveMetrics metrics ) {
GenericWriter idx;
GenericWriter bin;
if ( World.SaveType == World.SaveOption.Normal ) {
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();
}
}
}