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:
@@ -47,7 +47,6 @@ namespace OpenRA
|
|||||||
var map = modData.PrepareMap(uid);
|
var map = modData.PrepareMap(uid);
|
||||||
|
|
||||||
viewport = new Viewport(new float2(Renderer.Resolution), map.TopLeft, map.BottomRight, Renderer);
|
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);
|
world = new World(modData.Manifest, map);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,7 +100,7 @@ namespace OpenRA
|
|||||||
static ConnectionState lastConnectionState = ConnectionState.PreConnecting;
|
static ConnectionState lastConnectionState = ConnectionState.PreConnecting;
|
||||||
public static int LocalClientId { get { return orderManager.Connection.LocalClientId; } }
|
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)
|
if (orderManager.Connection.ConnectionState != lastConnectionState)
|
||||||
{
|
{
|
||||||
@@ -159,7 +158,7 @@ namespace OpenRA
|
|||||||
|
|
||||||
internal static event Action LobbyInfoChanged = () => { };
|
internal static event Action LobbyInfoChanged = () => { };
|
||||||
|
|
||||||
internal static void SyncLobbyInfo(string data)
|
internal static void SyncLobbyInfo( World world, string data)
|
||||||
{
|
{
|
||||||
LobbyInfo = Session.Deserialize(data);
|
LobbyInfo = Session.Deserialize(data);
|
||||||
|
|
||||||
@@ -186,6 +185,7 @@ namespace OpenRA
|
|||||||
public static event Action BeforeGameStart = () => {};
|
public static event Action BeforeGameStart = () => {};
|
||||||
internal static void StartGame(string map)
|
internal static void StartGame(string map)
|
||||||
{
|
{
|
||||||
|
world = null;
|
||||||
BeforeGameStart();
|
BeforeGameStart();
|
||||||
LoadMap(map);
|
LoadMap(map);
|
||||||
if (orderManager.GameStarted) return;
|
if (orderManager.GameStarted) return;
|
||||||
@@ -200,12 +200,11 @@ namespace OpenRA
|
|||||||
|
|
||||||
public static void DispatchMouseInput(MouseInputEvent ev, MouseEventArgs e, Modifiers modifierKeys)
|
public static void DispatchMouseInput(MouseInputEvent ev, MouseEventArgs e, Modifiers modifierKeys)
|
||||||
{
|
{
|
||||||
if (world == null)
|
var world = Game.world;
|
||||||
return;
|
if (world == null) return;
|
||||||
|
|
||||||
int sync = world.SyncHash();
|
|
||||||
var initialWorld = world;
|
|
||||||
|
|
||||||
|
Sync.CheckSyncUnchanged( world, () =>
|
||||||
|
{
|
||||||
var mi = new MouseInput
|
var mi = new MouseInput
|
||||||
{
|
{
|
||||||
Button = (MouseButton)(int)e.Button,
|
Button = (MouseButton)(int)e.Button,
|
||||||
@@ -214,9 +213,7 @@ namespace OpenRA
|
|||||||
Modifiers = modifierKeys,
|
Modifiers = modifierKeys,
|
||||||
};
|
};
|
||||||
Widget.HandleInput( world, mi );
|
Widget.HandleInput( world, mi );
|
||||||
|
} );
|
||||||
if (sync != world.SyncHash() && world == initialWorld)
|
|
||||||
throw new InvalidOperationException("Desync in DispatchMouseInput");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool IsHost
|
public static bool IsHost
|
||||||
@@ -231,16 +228,13 @@ namespace OpenRA
|
|||||||
|
|
||||||
public static void HandleKeyEvent(KeyInput e)
|
public static void HandleKeyEvent(KeyInput e)
|
||||||
{
|
{
|
||||||
if (world == null)
|
var world = Game.world;
|
||||||
return;
|
if( world == null ) return;
|
||||||
|
|
||||||
int sync = world.SyncHash();
|
Sync.CheckSyncUnchanged( world, () =>
|
||||||
|
{
|
||||||
if (Widget.HandleKeyPress(e))
|
Widget.HandleKeyPress( e );
|
||||||
return;
|
} );
|
||||||
|
|
||||||
if (sync != Game.world.SyncHash())
|
|
||||||
throw new InvalidOperationException("Desync in OnKeyPress");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Modifiers modifiers;
|
static Modifiers modifiers;
|
||||||
@@ -324,7 +318,7 @@ namespace OpenRA
|
|||||||
internal static void Run()
|
internal static void Run()
|
||||||
{
|
{
|
||||||
while (!quit)
|
while (!quit)
|
||||||
{
|
{
|
||||||
Tick( world, orderManager, viewport );
|
Tick( world, orderManager, viewport );
|
||||||
Application.DoEvents();
|
Application.DoEvents();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ namespace OpenRA.Graphics
|
|||||||
world.OrderGenerator.RenderBeforeWorld(world);
|
world.OrderGenerator.RenderBeforeWorld(world);
|
||||||
|
|
||||||
foreach( var image in worldSprites )
|
foreach( var image in worldSprites )
|
||||||
image.Sprite.DrawAt(image.Pos, image.Palette);
|
image.Sprite.DrawAt( image.Pos, this.GetPaletteIndex( image.Palette ) );
|
||||||
uiOverlay.Draw(world);
|
uiOverlay.Draw(world);
|
||||||
|
|
||||||
if (world.OrderGenerator != null)
|
if (world.OrderGenerator != null)
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ namespace OpenRA.Network
|
|||||||
}
|
}
|
||||||
case "SyncInfo":
|
case "SyncInfo":
|
||||||
{
|
{
|
||||||
Game.SyncLobbyInfo(order.TargetString);
|
Game.SyncLobbyInfo( world, order.TargetString);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "SetStance":
|
case "SetStance":
|
||||||
|
|||||||
@@ -129,5 +129,22 @@ namespace OpenRA
|
|||||||
return p.Index * 0x567;
|
return p.Index * 0x567;
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -109,8 +109,7 @@ namespace OpenRA.Widgets
|
|||||||
public override string GetCursor(int2 pos)
|
public override string GetCursor(int2 pos)
|
||||||
{
|
{
|
||||||
var world = Game.world;
|
var world = Game.world;
|
||||||
int sync = world.SyncHash();
|
return Sync.CheckSyncUnchanged( world, () =>
|
||||||
try
|
|
||||||
{
|
{
|
||||||
if (!world.GameHasStarted)
|
if (!world.GameHasStarted)
|
||||||
return "default";
|
return "default";
|
||||||
@@ -122,13 +121,8 @@ namespace OpenRA.Widgets
|
|||||||
Modifiers = Game.GetModifierKeys()
|
Modifiers = Game.GetModifierKeys()
|
||||||
};
|
};
|
||||||
|
|
||||||
return Game.world.OrderGenerator.GetCursor( world, Game.viewport.ViewToWorld(mi).ToInt2(), mi );
|
return world.OrderGenerator.GetCursor( world, Game.viewport.ViewToWorld(mi).ToInt2(), mi );
|
||||||
}
|
} );
|
||||||
finally
|
|
||||||
{
|
|
||||||
if( sync != world.SyncHash() )
|
|
||||||
throw new InvalidOperationException( "Desync in InputControllerWidget.GetCursor" );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override bool HandleKeyPressInner(KeyInput e)
|
public override bool HandleKeyPressInner(KeyInput e)
|
||||||
|
|||||||
@@ -35,6 +35,8 @@ namespace OpenRA.Mods.RA.Widgets
|
|||||||
|
|
||||||
public override void DrawInner(World world)
|
public override void DrawInner(World world)
|
||||||
{
|
{
|
||||||
|
if( world.LocalPlayer == null ) return;
|
||||||
|
|
||||||
var playerResources = world.LocalPlayer.PlayerActor.Trait<PlayerResources>();
|
var playerResources = world.LocalPlayer.PlayerActor.Trait<PlayerResources>();
|
||||||
|
|
||||||
var digitCollection = "digits-" + world.LocalPlayer.Country.Race;
|
var digitCollection = "digits-" + world.LocalPlayer.Country.Race;
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ namespace OpenRA.Mods.RA.Widgets
|
|||||||
|
|
||||||
public override void DrawInner(World world)
|
public override void DrawInner(World world)
|
||||||
{
|
{
|
||||||
|
if( world.LocalPlayer == null ) return;
|
||||||
|
|
||||||
powerCollection = "power-" + world.LocalPlayer.Country.Race;
|
powerCollection = "power-" + world.LocalPlayer.Country.Race;
|
||||||
|
|
||||||
var power = world.LocalPlayer.PlayerActor.Trait<PowerManager>();
|
var power = world.LocalPlayer.PlayerActor.Trait<PowerManager>();
|
||||||
|
|||||||
@@ -121,6 +121,8 @@ namespace OpenRA.Mods.RA.Widgets
|
|||||||
|
|
||||||
public override void DrawInner(World world)
|
public override void DrawInner(World world)
|
||||||
{
|
{
|
||||||
|
if( world.LocalPlayer == null ) return;
|
||||||
|
|
||||||
radarCollection = "radar-" + world.LocalPlayer.Country.Race;
|
radarCollection = "radar-" + world.LocalPlayer.Country.Race;
|
||||||
|
|
||||||
Game.Renderer.RgbaSpriteRenderer.DrawSprite(ChromeProvider.GetImage(radarCollection, "left"), radarOrigin);
|
Game.Renderer.RgbaSpriteRenderer.DrawSprite(ChromeProvider.GetImage(radarCollection, "left"), radarOrigin);
|
||||||
|
|||||||
@@ -68,6 +68,8 @@ namespace OpenRA.Mods.RA.Widgets
|
|||||||
{
|
{
|
||||||
buttons.Clear();
|
buttons.Clear();
|
||||||
|
|
||||||
|
if( world.LocalPlayer == null ) return;
|
||||||
|
|
||||||
var powers = world.LocalPlayer.PlayerActor.TraitsImplementing<SupportPower>();
|
var powers = world.LocalPlayer.PlayerActor.TraitsImplementing<SupportPower>();
|
||||||
var numPowers = powers.Count(p => p.IsAvailable);
|
var numPowers = powers.Count(p => p.IsAvailable);
|
||||||
if (numPowers == 0) return;
|
if (numPowers == 0) return;
|
||||||
|
|||||||
Reference in New Issue
Block a user