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;
var world = Game.world;
if (world == null) return;
int sync = world.SyncHash();
var initialWorld = world;
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;
var world = Game.world;
if( world == null ) return;
int sync = world.SyncHash();
if (Widget.HandleKeyPress(e))
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();
}

View File

@@ -97,8 +97,8 @@ namespace OpenRA.Graphics
if (world.OrderGenerator != null)
world.OrderGenerator.RenderBeforeWorld(world);
foreach (var image in worldSprites)
image.Sprite.DrawAt(image.Pos, image.Palette);
foreach( var image in worldSprites )
image.Sprite.DrawAt( image.Pos, this.GetPaletteIndex( image.Palette ) );
uiOverlay.Draw(world);
if (world.OrderGenerator != null)

View File

@@ -78,7 +78,7 @@ namespace OpenRA.Network
}
case "SyncInfo":
{
Game.SyncLobbyInfo(order.TargetString);
Game.SyncLobbyInfo( world, order.TargetString);
break;
}
case "SetStance":

View File

@@ -129,5 +129,22 @@ namespace OpenRA
return p.Index * 0x567;
return 0;
}
public static void CheckSyncUnchanged( World world, Action fn )
{
int sync = world.SyncHash();
fn();
if( sync != world.SyncHash() )
throw new InvalidOperationException( "Desync in DispatchMouseInput" );
}
public static T CheckSyncUnchanged<T>( World world, Func<T> fn )
{
int sync = world.SyncHash();
var ret = fn();
if( sync != world.SyncHash() )
throw new InvalidOperationException( "Desync in DispatchMouseInput" );
return ret;
}
}
}

View File

@@ -109,8 +109,7 @@ namespace OpenRA.Widgets
public override string GetCursor(int2 pos)
{
var world = Game.world;
int sync = world.SyncHash();
try
return Sync.CheckSyncUnchanged( world, () =>
{
if (!world.GameHasStarted)
return "default";
@@ -122,13 +121,8 @@ namespace OpenRA.Widgets
Modifiers = Game.GetModifierKeys()
};
return Game.world.OrderGenerator.GetCursor( world, Game.viewport.ViewToWorld(mi).ToInt2(), mi );
}
finally
{
if( sync != world.SyncHash() )
throw new InvalidOperationException( "Desync in InputControllerWidget.GetCursor" );
}
return world.OrderGenerator.GetCursor( world, Game.viewport.ViewToWorld(mi).ToInt2(), mi );
} );
}
public override bool HandleKeyPressInner(KeyInput e)

View File

@@ -35,6 +35,8 @@ namespace OpenRA.Mods.RA.Widgets
public override void DrawInner(World world)
{
if( world.LocalPlayer == null ) return;
var playerResources = world.LocalPlayer.PlayerActor.Trait<PlayerResources>();
var digitCollection = "digits-" + world.LocalPlayer.Country.Race;

View File

@@ -27,6 +27,8 @@ namespace OpenRA.Mods.RA.Widgets
public override void DrawInner(World world)
{
if( world.LocalPlayer == null ) return;
powerCollection = "power-" + world.LocalPlayer.Country.Race;
var power = world.LocalPlayer.PlayerActor.Trait<PowerManager>();

View File

@@ -121,6 +121,8 @@ namespace OpenRA.Mods.RA.Widgets
public override void DrawInner(World world)
{
if( world.LocalPlayer == null ) return;
radarCollection = "radar-" + world.LocalPlayer.Country.Race;
Game.Renderer.RgbaSpriteRenderer.DrawSprite(ChromeProvider.GetImage(radarCollection, "left"), radarOrigin);

View File

@@ -68,6 +68,8 @@ namespace OpenRA.Mods.RA.Widgets
{
buttons.Clear();
if( world.LocalPlayer == null ) return;
var powers = world.LocalPlayer.PlayerActor.TraitsImplementing<SupportPower>();
var numPowers = powers.Count(p => p.IsAvailable);
if (numPowers == 0) return;