MonoBehaviourProcessor.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using Mono.CecilX;
  2. namespace Mirror.Weaver
  3. {
  4. /// <summary>
  5. /// only shows warnings in case we use SyncVars etc. for MonoBehaviour.
  6. /// </summary>
  7. static class MonoBehaviourProcessor
  8. {
  9. public static void Process(TypeDefinition td)
  10. {
  11. ProcessSyncVars(td);
  12. ProcessMethods(td);
  13. }
  14. static void ProcessSyncVars(TypeDefinition td)
  15. {
  16. // find syncvars
  17. foreach (FieldDefinition fd in td.Fields)
  18. {
  19. if (fd.HasCustomAttribute<SyncVarAttribute>())
  20. Weaver.Error($"SyncVar {fd.Name} must be inside a NetworkBehaviour. {td.Name} is not a NetworkBehaviour", fd);
  21. if (SyncObjectInitializer.ImplementsSyncObject(fd.FieldType))
  22. {
  23. Weaver.Error($"{fd.Name} is a SyncObject and must be inside a NetworkBehaviour. {td.Name} is not a NetworkBehaviour", fd);
  24. }
  25. }
  26. }
  27. static void ProcessMethods(TypeDefinition td)
  28. {
  29. // find command and RPC functions
  30. foreach (MethodDefinition md in td.Methods)
  31. {
  32. if (md.HasCustomAttribute<CommandAttribute>())
  33. Weaver.Error($"Command {md.Name} must be declared inside a NetworkBehaviour", md);
  34. if (md.HasCustomAttribute<ClientRpcAttribute>())
  35. Weaver.Error($"ClientRpc {md.Name} must be declared inside a NetworkBehaviour", md);
  36. if (md.HasCustomAttribute<TargetRpcAttribute>())
  37. Weaver.Error($"TargetRpc {md.Name} must be declared inside a NetworkBehaviour", md);
  38. }
  39. }
  40. }
  41. }