Adds style cop (#109)

This commit is contained in:
Kamron Batman 2020-04-26 00:16:02 -07:00 committed by GitHub
parent 3e715bcb60
commit 556a17aba8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
1725 changed files with 33831 additions and 38046 deletions

View file

@ -18,7 +18,6 @@
*
***************************************************************************/
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
@ -30,14 +29,14 @@ namespace Server
public sealed class DynamicSaveStrategy : SaveStrategy
{
private readonly ConcurrentBag<Item> _decayBag;
private SequentialFileWriter _guildData, _guildIndex;
private SequentialFileWriterStream _guildData, _guildIndex;
private readonly BlockingCollection<QueuedMemoryWriter> _guildThreadWriters;
private SequentialFileWriter _itemData, _itemIndex;
private SequentialFileWriterStream _itemData, _itemIndex;
private readonly BlockingCollection<QueuedMemoryWriter> _itemThreadWriters;
private SequentialFileWriter _mobileData, _mobileIndex;
private SequentialFileWriterStream _mobileData, _mobileIndex;
private readonly BlockingCollection<QueuedMemoryWriter> _mobileThreadWriters;
public DynamicSaveStrategy()
@ -54,7 +53,7 @@ namespace Server
{
OpenFiles();
Task[] saveTasks = new Task[3];
var saveTasks = new Task[3];
saveTasks[0] = SaveItems();
saveTasks[1] = SaveMobiles();
@ -64,25 +63,28 @@ namespace Server
if (permitBackgroundWrite)
{
//This option makes it finish the writing to disk in the background, continuing even after Save() returns.
#pragma warning restore CA2008 // Do not create tasks without passing a TaskScheduler *
// This option makes it finish the writing to disk in the background, continuing even after Save() returns.
Task.Factory.ContinueWhenAll(saveTasks, _ =>
{
CloseFiles();
World.NotifyDiskWriteComplete();
});
#pragma warning restore CA2008 // Do not create tasks without passing a TaskScheduler *
}
else
{
Task.WaitAll(saveTasks); //Waits for the completion of all of the tasks(committing to disk)
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)
private Task StartCommitTask(BlockingCollection<QueuedMemoryWriter> threadWriter, SequentialFileWriterStream data,
SequentialFileWriterStream index)
{
Task commitTask = Task.Factory.StartNew(() =>
#pragma warning disable CA2008 // Do not create tasks without passing a TaskScheduler *
var commitTask = Task.Factory.StartNew(() =>
{
while (!threadWriter.IsCompleted)
{
@ -94,33 +96,34 @@ namespace Server
}
catch (InvalidOperationException)
{
//Per MSDN, it's fine if we're here, successful completion of adding can rarely put us into this state.
// 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);
}
});
#pragma warning restore CA2008 // Do not create tasks without passing a TaskScheduler *
return commitTask;
}
private Task SaveItems()
{
//Start the blocking consumer; this runs in background.
Task commitTask = StartCommitTask(_itemThreadWriters, _itemData, _itemIndex);
// Start the blocking consumer; this runs in background.
var commitTask = StartCommitTask(_itemThreadWriters, _itemData, _itemIndex);
IEnumerable<Item> items = World.Items.Values;
//Start the producer.
// Start the producer.
Parallel.ForEach(items, () => new QueuedMemoryWriter(),
(item, state, writer) =>
{
long startPosition = writer.Position;
var startPosition = writer.Position;
item.Serialize(writer);
int size = (int)(writer.Position - startPosition);
var size = (int)(writer.Position - startPosition);
writer.QueueForIndex(item, size);
@ -136,27 +139,27 @@ namespace Server
_itemThreadWriters.Add(writer);
});
_itemThreadWriters.CompleteAdding(); //We only get here after the Parallel.ForEach completes. Lets our task
_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);
// Start the blocking consumer; this runs in background.
var commitTask = StartCommitTask(_mobileThreadWriters, _mobileData, _mobileIndex);
IEnumerable<Mobile> mobiles = World.Mobiles.Values;
//Start the producer.
// Start the producer.
Parallel.ForEach(mobiles, () => new QueuedMemoryWriter(),
(mobile, state, writer) =>
{
long startPosition = writer.Position;
var startPosition = writer.Position;
mobile.Serialize(writer);
int size = (int)(writer.Position - startPosition);
var size = (int)(writer.Position - startPosition);
writer.QueueForIndex(mobile, size);
@ -170,27 +173,27 @@ namespace Server
});
_mobileThreadWriters
.CompleteAdding(); //We only get here after the Parallel.ForEach completes. Lets our task tell the consumer that we're done
.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);
// Start the blocking consumer; this runs in background.
var commitTask = StartCommitTask(_guildThreadWriters, _guildData, _guildIndex);
IEnumerable<BaseGuild> guilds = BaseGuild.List.Values;
//Start the producer.
// Start the producer.
Parallel.ForEach(guilds, () => new QueuedMemoryWriter(),
(guild, state, writer) =>
{
long startPosition = writer.Position;
var startPosition = writer.Position;
guild.Serialize(writer);
int size = (int)(writer.Position - startPosition);
var size = (int)(writer.Position - startPosition);
writer.QueueForIndex(guild, size);
@ -203,28 +206,28 @@ namespace Server
_guildThreadWriters.Add(writer);
});
_guildThreadWriters.CompleteAdding(); //We only get here after the Parallel.ForEach completes. Lets our task
_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))
while (_decayBag.TryTake(out var item))
if (item.OnDecay())
item.Delete();
}
private void OpenFiles()
{
_itemData = new SequentialFileWriter(World.ItemDataPath);
_itemIndex = new SequentialFileWriter(World.ItemIndexPath);
_itemData = new SequentialFileWriterStream(World.ItemDataPath);
_itemIndex = new SequentialFileWriterStream(World.ItemIndexPath);
_mobileData = new SequentialFileWriter(World.MobileDataPath);
_mobileIndex = new SequentialFileWriter(World.MobileIndexPath);
_mobileData = new SequentialFileWriterStream(World.MobileDataPath);
_mobileIndex = new SequentialFileWriterStream(World.MobileIndexPath);
_guildData = new SequentialFileWriter(World.GuildDataPath);
_guildIndex = new SequentialFileWriter(World.GuildIndexPath);
_guildData = new SequentialFileWriterStream(World.GuildDataPath);
_guildIndex = new SequentialFileWriterStream(World.GuildIndexPath);
WriteCount(_itemIndex, World.Items.Count);
WriteCount(_mobileIndex, World.Mobiles.Count);
@ -243,10 +246,10 @@ namespace Server
_guildIndex.Close();
}
private void WriteCount(SequentialFileWriter indexFile, int count)
private void WriteCount(SequentialFileWriterStream indexFile, int count)
{
//Equiv to GenericWriter.Write( (int)count );
byte[] buffer = new byte[4];
// Equiv to GenericWriter.Write( (int)count );
var buffer = new byte[4];
buffer[0] = (byte)count;
buffer[1] = (byte)(count >> 8);
@ -264,15 +267,15 @@ namespace Server
private void SaveTypeDatabase(string path, List<Type> types)
{
BinaryFileWriter bfw = new BinaryFileWriter(path, false);
var bfw = new BinaryFileWriter(path, false);
bfw.Write(types.Count);
foreach (Type type in types) bfw.Write(type.FullName);
foreach (var type in types) bfw.Write(type.FullName);
bfw.Flush();
bfw.Close();
}
}
}
}