push the check-synchash-doesn't-change pattern into a utility fn. furthur reduce the number of uses on Game.world

This commit is contained in:
Bob
2010-10-02 10:20:14 +12:00
committed by Paul Chote
parent f41aa474aa
commit 26d1db778e
9 changed files with 195 additions and 182 deletions

View File

@@ -47,7 +47,6 @@ namespace OpenRA
var map = modData.PrepareMap(uid);
viewport = new Viewport(new float2(Renderer.Resolution), map.TopLeft, map.BottomRight, Renderer);
world = null; // trying to access the old world will NRE, rather than silently doing it wrong.
world = new World(modData.Manifest, map);
}
@@ -101,7 +100,7 @@ namespace OpenRA
static ConnectionState lastConnectionState = ConnectionState.PreConnecting;
public static int LocalClientId { get { return orderManager.Connection.LocalClientId; } }
static void Tick()
static void Tick( World world, OrderManager orderManager, Viewport viewPort )
{
if (orderManager.Connection.ConnectionState != lastConnectionState)
{
@@ -159,7 +158,7 @@ namespace OpenRA
internal static event Action LobbyInfoChanged = () => { };
internal static void SyncLobbyInfo(string data)
internal static void SyncLobbyInfo( World world, string data)
{
LobbyInfo = Session.Deserialize(data);
@@ -186,6 +185,7 @@ namespace OpenRA
public static event Action BeforeGameStart = () => {};
internal static void StartGame(string map)
{
world = null;
BeforeGameStart();
LoadMap(map);
if (orderManager.GameStarted) return;
@@ -200,23 +200,20 @@ namespace OpenRA
public static void DispatchMouseInput(MouseInputEvent ev, MouseEventArgs e, Modifiers modifierKeys)
{
if (world == null)
return;
int sync = world.SyncHash();
var initialWorld = world;
var world = Game.world;
if (world == null) return;
var mi = new MouseInput
Sync.CheckSyncUnchanged( world, () =>
{
Button = (MouseButton)(int)e.Button,
Event = ev,
Location = new int2(e.Location),
Modifiers = modifierKeys,
};
Widget.HandleInput(world, mi);
if (sync != world.SyncHash() && world == initialWorld)
throw new InvalidOperationException("Desync in DispatchMouseInput");
var mi = new MouseInput
{
Button = (MouseButton)(int)e.Button,
Event = ev,
Location = new int2( e.Location ),
Modifiers = modifierKeys,
};
Widget.HandleInput( world, mi );
} );
}
public static bool IsHost
@@ -231,16 +228,13 @@ namespace OpenRA
public static void HandleKeyEvent(KeyInput e)
{
if (world == null)
return;
int sync = world.SyncHash();
if (Widget.HandleKeyPress(e))
return;
var world = Game.world;
if( world == null ) return;
if (sync != Game.world.SyncHash())
throw new InvalidOperationException("Desync in OnKeyPress");
Sync.CheckSyncUnchanged( world, () =>
{
Widget.HandleKeyPress( e );
} );
}
static Modifiers modifiers;
@@ -324,7 +318,7 @@ namespace OpenRA
internal static void Run()
{
while (!quit)
{
{
Tick( world, orderManager, viewport );
Application.DoEvents();
}