MonoBehaviourProcessor.cs 2.2 KB

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