From 593d913f559b832935c87fe2a4302f8638189c89 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Mon, 21 Sep 2015 22:15:36 +0100 Subject: [PATCH 1/3] Remove sync on actor - as it doesn't currently work. --- OpenRA.Game/Actor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OpenRA.Game/Actor.cs b/OpenRA.Game/Actor.cs index c7605549bc..e4378cee19 100644 --- a/OpenRA.Game/Actor.cs +++ b/OpenRA.Game/Actor.cs @@ -30,7 +30,7 @@ namespace OpenRA public readonly uint ActorID; - [Sync] public Player Owner { get; set; } + public Player Owner { get; set; } public bool IsInWorld { get; internal set; } public bool Disposed { get; private set; } From 6b62aa1ad5d8a0aea589f5cf1b59ceebabd79b35 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Wed, 29 Jul 2015 00:54:01 +0100 Subject: [PATCH 2/3] Add missing sync interfaces. --- OpenRA.Game/Traits/World/Shroud.cs | 2 +- OpenRA.Mods.Common/Effects/GravityBomb.cs | 2 +- OpenRA.Mods.Common/Traits/ExternalCapturable.cs | 2 +- OpenRA.Mods.Common/Traits/ParaDrop.cs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/OpenRA.Game/Traits/World/Shroud.cs b/OpenRA.Game/Traits/World/Shroud.cs index baeddde339..cd3dc6f125 100644 --- a/OpenRA.Game/Traits/World/Shroud.cs +++ b/OpenRA.Game/Traits/World/Shroud.cs @@ -20,7 +20,7 @@ namespace OpenRA.Traits public object Create(ActorInitializer init) { return new Shroud(init.Self); } } - public class Shroud + public class Shroud : ISync { [Sync] public bool Disabled; diff --git a/OpenRA.Mods.Common/Effects/GravityBomb.cs b/OpenRA.Mods.Common/Effects/GravityBomb.cs index 2038ad1017..a81e347947 100644 --- a/OpenRA.Mods.Common/Effects/GravityBomb.cs +++ b/OpenRA.Mods.Common/Effects/GravityBomb.cs @@ -38,7 +38,7 @@ namespace OpenRA.Mods.Common.Effects public IEffect Create(ProjectileArgs args) { return new GravityBomb(this, args); } } - public class GravityBomb : IEffect + public class GravityBomb : IEffect, ISync { readonly GravityBombInfo info; readonly Animation anim; diff --git a/OpenRA.Mods.Common/Traits/ExternalCapturable.cs b/OpenRA.Mods.Common/Traits/ExternalCapturable.cs index ccc4fc8741..a4949a7fe5 100644 --- a/OpenRA.Mods.Common/Traits/ExternalCapturable.cs +++ b/OpenRA.Mods.Common/Traits/ExternalCapturable.cs @@ -49,7 +49,7 @@ namespace OpenRA.Mods.Common.Traits public object Create(ActorInitializer init) { return new ExternalCapturable(init.Self, this); } } - public class ExternalCapturable : ITick + public class ExternalCapturable : ITick, ISync { [Sync] public int CaptureProgressTime = 0; [Sync] public Actor Captor; diff --git a/OpenRA.Mods.Common/Traits/ParaDrop.cs b/OpenRA.Mods.Common/Traits/ParaDrop.cs index f9a5999b30..79dad067bd 100644 --- a/OpenRA.Mods.Common/Traits/ParaDrop.cs +++ b/OpenRA.Mods.Common/Traits/ParaDrop.cs @@ -27,7 +27,7 @@ namespace OpenRA.Mods.Common.Traits public object Create(ActorInitializer init) { return new ParaDrop(init.Self, this); } } - public class ParaDrop : ITick, INotifyRemovedFromWorld + public class ParaDrop : ITick, ISync, INotifyRemovedFromWorld { readonly ParaDropInfo info; readonly Actor self; From 3a63a427e6ba5379b02614eb25259f44d69d3ceb Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Fri, 18 Sep 2015 19:45:57 +0100 Subject: [PATCH 3/3] Improved sync lint pass. Now also checks for classes with Sync members lacking the ISync interface. --- .../Lint/CheckSyncAnnotations.cs | 47 ++++++++++++++----- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/OpenRA.Mods.Common/Lint/CheckSyncAnnotations.cs b/OpenRA.Mods.Common/Lint/CheckSyncAnnotations.cs index 9215590ec2..d2ebd50715 100644 --- a/OpenRA.Mods.Common/Lint/CheckSyncAnnotations.cs +++ b/OpenRA.Mods.Common/Lint/CheckSyncAnnotations.cs @@ -9,6 +9,7 @@ #endregion using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; using OpenRA.Traits; @@ -19,23 +20,43 @@ namespace OpenRA.Mods.Common.Lint { public void Run(Action emitError, Action emitWarning) { - /* first, check all the types implementing ISync */ - foreach (var t in Game.ModData.ObjectCreator.GetTypesImplementing()) - if (!HasAnySyncFields(t)) - emitWarning("{0} has ISync but nothing marked with [Sync]".F(t.Name)); + var modTypes = Game.ModData.ObjectCreator.GetTypes(); + CheckTypesWithSyncableMembersImplementSyncInterface(modTypes, emitWarning); + CheckTypesImplementingSyncInterfaceHaveSyncableMembers(modTypes, emitWarning); } - bool HasAnySyncFields(Type t) + static readonly Type SyncInterface = typeof(ISync); + + static bool TypeImplementsSync(Type type) { - var flags = BindingFlags.Public | BindingFlags.NonPublic - | BindingFlags.Instance; + return type.GetInterfaces().Contains(SyncInterface); + } - var fs = t.GetFields(flags); - var ps = t.GetProperties(flags); + static bool AnyTypeMemberIsSynced(Type type) + { + const BindingFlags Flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly; + while (type != null) + { + if (((MemberInfo[])type.GetFields(Flags)).Concat(type.GetProperties(Flags)).Any(x => x.HasAttribute())) + return true; + type = type.BaseType; + } - return fs.Any(f => f.HasAttribute()) || - ps.Any(p => p.HasAttribute()) || - HasAnySyncFields(t.BaseType); + return false; + } + + static void CheckTypesWithSyncableMembersImplementSyncInterface(IEnumerable types, Action emitWarning) + { + foreach (var type in types) + if (!TypeImplementsSync(type) && AnyTypeMemberIsSynced(type)) + emitWarning("{0} has members with the Sync attribute but does not implement ISync".F(type.FullName)); + } + + static void CheckTypesImplementingSyncInterfaceHaveSyncableMembers(IEnumerable types, Action emitWarning) + { + foreach (var type in types) + if (TypeImplementsSync(type) && !AnyTypeMemberIsSynced(type)) + emitWarning("{0} implements ISync but does not use the Sync attribute on any members.".F(type.FullName)); } } -} +} \ No newline at end of file