Improved sync lint pass.
Now also checks for classes with Sync members lacking the ISync interface.
This commit is contained in:
@@ -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<string> emitError, Action<string> emitWarning)
|
||||
{
|
||||
/* first, check all the types implementing ISync */
|
||||
foreach (var t in Game.ModData.ObjectCreator.GetTypesImplementing<ISync>())
|
||||
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<SyncAttribute>()))
|
||||
return true;
|
||||
type = type.BaseType;
|
||||
}
|
||||
|
||||
return fs.Any(f => f.HasAttribute<SyncAttribute>()) ||
|
||||
ps.Any(p => p.HasAttribute<SyncAttribute>()) ||
|
||||
HasAnySyncFields(t.BaseType);
|
||||
return false;
|
||||
}
|
||||
|
||||
static void CheckTypesWithSyncableMembersImplementSyncInterface(IEnumerable<Type> types, Action<string> 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<Type> types, Action<string> 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user