extract replay saving from OrderManager
This commit is contained in:
@@ -68,7 +68,12 @@ namespace OpenRA
|
|||||||
lastConnectionState = ConnectionState.PreConnecting;
|
lastConnectionState = ConnectionState.PreConnecting;
|
||||||
ConnectionStateChanged();
|
ConnectionStateChanged();
|
||||||
|
|
||||||
orderManager = new OrderManager(new NetworkConnection(host, port), ChooseReplayFilename());
|
var replayFilename = ChooseReplayFilename();
|
||||||
|
string path = Path.Combine( Game.SupportDir, "Replays" );
|
||||||
|
if( !Directory.Exists( path ) ) Directory.CreateDirectory( path );
|
||||||
|
var replayFile = File.Create( Path.Combine( path, replayFilename ) );
|
||||||
|
|
||||||
|
orderManager = new OrderManager( new ReplayRecorderConnection( new NetworkConnection( host, port ), replayFile ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
static string ChooseReplayFilename()
|
static string ChooseReplayFilename()
|
||||||
@@ -117,7 +122,7 @@ namespace OpenRA
|
|||||||
lastTime += Settings.Game.Timestep;
|
lastTime += Settings.Game.Timestep;
|
||||||
Widget.DoTick(world);
|
Widget.DoTick(world);
|
||||||
Sound.Tick();
|
Sound.Tick();
|
||||||
orderManager.TickImmediate(world);
|
Sync.CheckSyncUnchanged( world, () => { orderManager.TickImmediate( world ); } );
|
||||||
|
|
||||||
var isNetTick = LocalTick % NetTickScale == 0;
|
var isNetTick = LocalTick % NetTickScale == 0;
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ namespace OpenRA.Network
|
|||||||
Connected,
|
Connected,
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IConnection
|
interface IConnection : IDisposable
|
||||||
{
|
{
|
||||||
int LocalClientId { get; }
|
int LocalClientId { get; }
|
||||||
ConnectionState ConnectionState { get; }
|
ConnectionState ConnectionState { get; }
|
||||||
@@ -73,9 +73,11 @@ namespace OpenRA.Network
|
|||||||
foreach( var p in packets )
|
foreach( var p in packets )
|
||||||
packetFn( p.FromClient, p.Data );
|
packetFn( p.FromClient, p.Data );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual void Dispose() { }
|
||||||
}
|
}
|
||||||
|
|
||||||
class NetworkConnection : EchoConnection, IDisposable
|
class NetworkConnection : EchoConnection
|
||||||
{
|
{
|
||||||
TcpClient socket;
|
TcpClient socket;
|
||||||
int clientId;
|
int clientId;
|
||||||
@@ -145,7 +147,7 @@ namespace OpenRA.Network
|
|||||||
|
|
||||||
bool disposed = false;
|
bool disposed = false;
|
||||||
|
|
||||||
public void Dispose ()
|
public override void Dispose ()
|
||||||
{
|
{
|
||||||
if (disposed) return;
|
if (disposed) return;
|
||||||
disposed = true;
|
disposed = true;
|
||||||
@@ -160,48 +162,4 @@ namespace OpenRA.Network
|
|||||||
|
|
||||||
~NetworkConnection() { Dispose(); }
|
~NetworkConnection() { Dispose(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
class ReplayConnection : IConnection
|
|
||||||
{
|
|
||||||
//uint nextFrame = 1;
|
|
||||||
FileStream replayStream;
|
|
||||||
|
|
||||||
public ReplayConnection( string replayFilename )
|
|
||||||
{
|
|
||||||
replayStream = File.OpenRead( replayFilename );
|
|
||||||
}
|
|
||||||
|
|
||||||
public int LocalClientId
|
|
||||||
{
|
|
||||||
get { return 0; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConnectionState ConnectionState
|
|
||||||
{
|
|
||||||
get { return ConnectionState.Connected; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Send( byte[] packet )
|
|
||||||
{
|
|
||||||
// do nothing; ignore locally generated orders
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Receive( Action<int, byte[]> packetFn )
|
|
||||||
{
|
|
||||||
if( replayStream == null ) return;
|
|
||||||
|
|
||||||
var reader = new BinaryReader( replayStream );
|
|
||||||
while( replayStream.Position < replayStream.Length )
|
|
||||||
{
|
|
||||||
var client = reader.ReadInt32();
|
|
||||||
var packetLen = reader.ReadInt32();
|
|
||||||
var packet = reader.ReadBytes( packetLen );
|
|
||||||
packetFn( client, packet );
|
|
||||||
|
|
||||||
if( !Game.orderManager.GameStarted )
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
replayStream = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,8 +35,6 @@ namespace OpenRA.Network
|
|||||||
new Dictionary<int, Dictionary<int, byte[]>>();
|
new Dictionary<int, Dictionary<int, byte[]>>();
|
||||||
List<Order> localOrders = new List<Order>();
|
List<Order> localOrders = new List<Order>();
|
||||||
|
|
||||||
FileStream replaySaveFile;
|
|
||||||
|
|
||||||
public void StartGame()
|
public void StartGame()
|
||||||
{
|
{
|
||||||
if (GameStarted) return;
|
if (GameStarted) return;
|
||||||
@@ -51,14 +49,6 @@ namespace OpenRA.Network
|
|||||||
Connection = conn;
|
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 )
|
public void IssueOrders( Order[] orders )
|
||||||
{
|
{
|
||||||
foreach( var order in orders )
|
foreach( var order in orders )
|
||||||
@@ -94,11 +84,8 @@ namespace OpenRA.Network
|
|||||||
} );
|
} );
|
||||||
|
|
||||||
foreach( var p in immediatePackets )
|
foreach( var p in immediatePackets )
|
||||||
{
|
|
||||||
foreach( var o in p.Second.ToOrderList( world ) )
|
foreach( var o in p.Second.ToOrderList( world ) )
|
||||||
UnitOrders.ProcessOrder( world, p.First, o );
|
UnitOrders.ProcessOrder( world, p.First, o );
|
||||||
WriteImmediateToReplay( immediatePackets );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Dictionary<int, byte[]> syncForFrame = new Dictionary<int, byte[]>();
|
Dictionary<int, byte[]> syncForFrame = new Dictionary<int, byte[]>();
|
||||||
@@ -189,7 +176,6 @@ namespace OpenRA.Network
|
|||||||
|
|
||||||
var ss = sync.SerializeSync( FrameNumber );
|
var ss = sync.SerializeSync( FrameNumber );
|
||||||
Connection.Send( ss );
|
Connection.Send( ss );
|
||||||
WriteToReplay( frameData, ss );
|
|
||||||
|
|
||||||
syncReport.UpdateSyncReport();
|
syncReport.UpdateSyncReport();
|
||||||
|
|
||||||
@@ -198,43 +184,12 @@ namespace OpenRA.Network
|
|||||||
++FrameNumber;
|
++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;
|
bool disposed;
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
if (disposed) return;
|
if (disposed) return;
|
||||||
|
|
||||||
if (replaySaveFile != null)
|
Connection.Dispose();
|
||||||
replaySaveFile.Dispose();
|
|
||||||
|
|
||||||
var disposableConnection = Connection as IDisposable;
|
|
||||||
if (disposableConnection != null) disposableConnection.Dispose();
|
|
||||||
|
|
||||||
disposed = true;
|
disposed = true;
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
|
|||||||
103
OpenRA.Game/Network/ReplayConnection.cs
Executable file
103
OpenRA.Game/Network/ReplayConnection.cs
Executable file
@@ -0,0 +1,103 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace OpenRA.Network
|
||||||
|
{
|
||||||
|
class ReplayConnection : IConnection
|
||||||
|
{
|
||||||
|
//uint nextFrame = 1;
|
||||||
|
FileStream replayStream;
|
||||||
|
|
||||||
|
public ReplayConnection( string replayFilename )
|
||||||
|
{
|
||||||
|
replayStream = File.OpenRead( replayFilename );
|
||||||
|
}
|
||||||
|
|
||||||
|
public int LocalClientId
|
||||||
|
{
|
||||||
|
get { return 0; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConnectionState ConnectionState
|
||||||
|
{
|
||||||
|
get { return ConnectionState.Connected; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Send( byte[] packet )
|
||||||
|
{
|
||||||
|
// do nothing; ignore locally generated orders
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Receive( Action<int, byte[]> packetFn )
|
||||||
|
{
|
||||||
|
if( replayStream == null ) return;
|
||||||
|
|
||||||
|
var reader = new BinaryReader( replayStream );
|
||||||
|
while( replayStream.Position < replayStream.Length )
|
||||||
|
{
|
||||||
|
var client = reader.ReadInt32();
|
||||||
|
var packetLen = reader.ReadInt32();
|
||||||
|
var packet = reader.ReadBytes( packetLen );
|
||||||
|
packetFn( client, packet );
|
||||||
|
|
||||||
|
if( !Game.orderManager.GameStarted )
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
replayStream = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose() { }
|
||||||
|
}
|
||||||
|
|
||||||
|
class ReplayRecorderConnection : IConnection
|
||||||
|
{
|
||||||
|
IConnection inner;
|
||||||
|
FileStream replayFile;
|
||||||
|
BinaryWriter writer;
|
||||||
|
|
||||||
|
public ReplayRecorderConnection( IConnection inner, FileStream replayFile )
|
||||||
|
{
|
||||||
|
this.inner = inner;
|
||||||
|
this.replayFile = replayFile;
|
||||||
|
this.writer = new BinaryWriter( replayFile );
|
||||||
|
}
|
||||||
|
|
||||||
|
public int LocalClientId { get { return inner.LocalClientId; } }
|
||||||
|
public ConnectionState ConnectionState { get { return inner.ConnectionState; } }
|
||||||
|
|
||||||
|
public void Send( byte[] packet )
|
||||||
|
{
|
||||||
|
inner.Send( packet );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Receive( Action<int, byte[]> packetFn )
|
||||||
|
{
|
||||||
|
inner.Receive( ( client, data ) =>
|
||||||
|
{
|
||||||
|
writer.Write( client );
|
||||||
|
writer.Write( data.Length );
|
||||||
|
writer.Write( data );
|
||||||
|
packetFn( client, data );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
|
bool disposed;
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
if( disposed )
|
||||||
|
return;
|
||||||
|
|
||||||
|
writer.Close();
|
||||||
|
disposed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
~ReplayRecorderConnection()
|
||||||
|
{
|
||||||
|
Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
|
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
|
||||||
<ProductVersion>9.0.21022</ProductVersion>
|
<ProductVersion>9.0.30729</ProductVersion>
|
||||||
<SchemaVersion>2.0</SchemaVersion>
|
<SchemaVersion>2.0</SchemaVersion>
|
||||||
<ProjectGuid>{0DFB103F-2962-400F-8C6D-E2C28CCBA633}</ProjectGuid>
|
<ProjectGuid>{0DFB103F-2962-400F-8C6D-E2C28CCBA633}</ProjectGuid>
|
||||||
<OutputType>WinExe</OutputType>
|
<OutputType>WinExe</OutputType>
|
||||||
@@ -222,6 +222,7 @@
|
|||||||
<Compile Include="ActorReference.cs" />
|
<Compile Include="ActorReference.cs" />
|
||||||
<Compile Include="ModData.cs" />
|
<Compile Include="ModData.cs" />
|
||||||
<Compile Include="Map.cs" />
|
<Compile Include="Map.cs" />
|
||||||
|
<Compile Include="Network\ReplayConnection.cs" />
|
||||||
<Compile Include="Network\Session.cs" />
|
<Compile Include="Network\Session.cs" />
|
||||||
<Compile Include="ObjectCreator.cs" />
|
<Compile Include="ObjectCreator.cs" />
|
||||||
<Compile Include="Network\SyncReport.cs" />
|
<Compile Include="Network\SyncReport.cs" />
|
||||||
|
|||||||
@@ -140,6 +140,7 @@ namespace OpenRA
|
|||||||
public static T CheckSyncUnchanged<T>( World world, Func<T> fn )
|
public static T CheckSyncUnchanged<T>( World world, Func<T> fn )
|
||||||
{
|
{
|
||||||
int sync = world.SyncHash();
|
int sync = world.SyncHash();
|
||||||
|
bool prevInUnsyncedCode = inUnsyncedCode;
|
||||||
inUnsyncedCode = true;
|
inUnsyncedCode = true;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -147,9 +148,9 @@ namespace OpenRA
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
inUnsyncedCode = false;
|
inUnsyncedCode = prevInUnsyncedCode;
|
||||||
if( sync != world.SyncHash() )
|
if( sync != world.SyncHash() )
|
||||||
throw new InvalidOperationException( "Desync in DispatchMouseInput" );
|
throw new InvalidOperationException( "CheckSyncUnchanged: sync-changing code may not run here" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user