order validation; don't hold refs in local orders; etc

This commit is contained in:
Chris Forbes
2009-12-02 19:10:12 +13:00
parent e24d1938a8
commit fb7f1f2817
2 changed files with 41 additions and 13 deletions

View File

@@ -11,23 +11,41 @@ namespace OpenRa.Game
{ {
public readonly Player Player; public readonly Player Player;
public readonly string OrderString; public readonly string OrderString;
public readonly Actor Subject; readonly uint SubjectId;
public readonly Actor TargetActor; readonly uint TargetActorId;
public readonly int2 TargetLocation; public readonly int2 TargetLocation;
public readonly string TargetString; public readonly string TargetString;
public bool IsImmediate; public bool IsImmediate;
public Actor Subject { get { return ActorFromUInt(SubjectId); } }
public Actor TargetActor { get { return ActorFromUInt(TargetActorId); } }
public Order(Player player, string orderString, Actor subject, public Order(Player player, string orderString, Actor subject,
Actor targetActor, int2 targetLocation, string targetString) Actor targetActor, int2 targetLocation, string targetString)
: this( player, orderString, UIntFromActor( subject ),
UIntFromActor( targetActor ), targetLocation, targetString ) {}
Order(Player player, string orderString, uint subjectId,
uint targetActorId, int2 targetLocation, string targetString)
{ {
this.Player = player; this.Player = player;
this.OrderString = orderString; this.OrderString = orderString;
this.Subject = subject; this.SubjectId = subjectId;
this.TargetActor = targetActor; this.TargetActorId = targetActorId;
this.TargetLocation = targetLocation; this.TargetLocation = targetLocation;
this.TargetString = targetString; this.TargetString = targetString;
} }
public bool Validate()
{
if ((SubjectId != 0xffffffff) && Subject == null)
return false;
if ((TargetActorId != 0xffffffff) && TargetActor == null)
return false;
return true;
}
public byte[] Serialize() public byte[] Serialize()
{ {
if (IsImmediate) /* chat, whatever */ if (IsImmediate) /* chat, whatever */
@@ -55,8 +73,8 @@ namespace OpenRa.Game
w.Write( (byte)0xFF ); w.Write( (byte)0xFF );
w.Write( (uint)Player.Index ); w.Write( (uint)Player.Index );
w.Write(OrderString); w.Write(OrderString);
w.Write(Subject == null ? 0xFFFFFFFF : Subject.ActorID); w.Write(SubjectId);
w.Write(TargetActor == null ? 0xFFFFFFFF : TargetActor.ActorID); w.Write(TargetActorId);
w.Write(TargetLocation.X); w.Write(TargetLocation.X);
w.Write(TargetLocation.Y); w.Write(TargetLocation.Y);
w.Write(TargetString != null); w.Write(TargetString != null);
@@ -82,8 +100,8 @@ namespace OpenRa.Game
{ {
var playerID = r.ReadUInt32(); var playerID = r.ReadUInt32();
var order = r.ReadString(); var order = r.ReadString();
var subject = ActorFromUInt(r.ReadUInt32()); var subjectId = r.ReadUInt32();
var targetActor = ActorFromUInt(r.ReadUInt32()); var targetActorId = r.ReadUInt32();
var targetLocation = new int2(r.ReadInt32(), 0); var targetLocation = new int2(r.ReadInt32(), 0);
targetLocation.Y = r.ReadInt32(); targetLocation.Y = r.ReadInt32();
var targetString = null as string; var targetString = null as string;
@@ -91,7 +109,7 @@ namespace OpenRa.Game
targetString = r.ReadString(); targetString = r.ReadString();
return new Order( LookupPlayer(playerID), return new Order( LookupPlayer(playerID),
order, subject, targetActor, targetLocation, order, subjectId, targetActorId, targetLocation,
targetString); targetString);
} }
@@ -110,10 +128,16 @@ namespace OpenRa.Game
} }
} }
static uint UIntFromActor(Actor a)
{
if (a == null) return 0xffffffff;
return a.ActorID;
}
static Actor ActorFromUInt(uint aID) static Actor ActorFromUInt(uint aID)
{ {
if (aID == 0xFFFFFFFF) return null; if (aID == 0xFFFFFFFF) return null;
return Game.world.Actors.Where(x => x.ActorID == aID).First(); return Game.world.Actors.SingleOrDefault(x => x.ActorID == aID);
} }
// Named constructors for Orders. // Named constructors for Orders.

View File

@@ -1,10 +1,8 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using IjwFramework.Types;
using OpenRa.Game.GameRules; using OpenRa.Game.GameRules;
using OpenRa.Game.Traits; using OpenRa.Game.Traits;
using IjwFramework.Types;
using System.Diagnostics;
namespace OpenRa.Game namespace OpenRa.Game
{ {
@@ -12,6 +10,12 @@ namespace OpenRa.Game
{ {
public static void ProcessOrder( Order order ) public static void ProcessOrder( Order order )
{ {
if (!order.Validate())
{
/* todo: log this if we care */
return;
}
switch( order.OrderString ) switch( order.OrderString )
{ {
case "Move": case "Move":