extract replay saving from OrderManager

This commit is contained in:
Bob
2010-10-10 20:32:39 +13:00
parent 07023b6c35
commit 915ad7fb7b
6 changed files with 121 additions and 98 deletions

View File

@@ -35,8 +35,6 @@ namespace OpenRA.Network
new Dictionary<int, Dictionary<int, byte[]>>();
List<Order> localOrders = new List<Order>();
FileStream replaySaveFile;
public void StartGame()
{
if (GameStarted) return;
@@ -51,14 +49,6 @@ namespace OpenRA.Network
Connection = conn;
}
public OrderManager( IConnection conn, string replayFilename )
: this( conn )
{
string path = Game.SupportDir + "Replays" + Path.DirectorySeparatorChar;
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
replaySaveFile = File.Create( path + replayFilename );
}
public void IssueOrders( Order[] orders )
{
foreach( var order in orders )
@@ -94,11 +84,8 @@ namespace OpenRA.Network
} );
foreach( var p in immediatePackets )
{
foreach( var o in p.Second.ToOrderList( world ) )
UnitOrders.ProcessOrder( world, p.First, o );
WriteImmediateToReplay( immediatePackets );
}
}
Dictionary<int, byte[]> syncForFrame = new Dictionary<int, byte[]>();
@@ -189,7 +176,6 @@ namespace OpenRA.Network
var ss = sync.SerializeSync( FrameNumber );
Connection.Send( ss );
WriteToReplay( frameData, ss );
syncReport.UpdateSyncReport();
@@ -198,43 +184,12 @@ namespace OpenRA.Network
++FrameNumber;
}
void WriteToReplay( Dictionary<int, byte[]> frameData, byte[] syncData )
{
if( replaySaveFile == null ) return;
foreach( var f in frameData )
{
replaySaveFile.Write( BitConverter.GetBytes( f.Key ) );
replaySaveFile.Write( BitConverter.GetBytes( f.Value.Length ) );
replaySaveFile.Write( f.Value );
}
replaySaveFile.Write( BitConverter.GetBytes( (int)0 ) );
replaySaveFile.Write( BitConverter.GetBytes( (int)syncData.Length ) );
replaySaveFile.Write( syncData );
}
void WriteImmediateToReplay( List<Pair<int, byte[]>> immediatePackets )
{
if( replaySaveFile == null ) return;
foreach( var i in immediatePackets )
{
replaySaveFile.Write( BitConverter.GetBytes( i.First ) );
replaySaveFile.Write( BitConverter.GetBytes( i.Second.Length ) );
replaySaveFile.Write( i.Second );
}
}
bool disposed;
public void Dispose()
{
if (disposed) return;
if (replaySaveFile != null)
replaySaveFile.Dispose();
var disposableConnection = Connection as IDisposable;
if (disposableConnection != null) disposableConnection.Dispose();
Connection.Dispose();
disposed = true;
GC.SuppressFinalize(this);