diff --git a/com.unity.multiplayer.mlapi/Editor/CodeGen/CodeGenHelpers.cs b/com.unity.multiplayer.mlapi/Editor/CodeGen/CodeGenHelpers.cs index ab48d07e7c..b984a1a368 100644 --- a/com.unity.multiplayer.mlapi/Editor/CodeGen/CodeGenHelpers.cs +++ b/com.unity.multiplayer.mlapi/Editor/CodeGen/CodeGenHelpers.cs @@ -13,10 +13,6 @@ using Unity.CompilationPipeline.Common.ILPostProcessing; using UnityEngine; -#if !UNITY_2019_4_OR_NEWER -#error MLAPI requires Unity 2019.4 or newer -#endif - namespace MLAPI.Editor.CodeGen { internal static class CodeGenHelpers diff --git a/com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessCompiledAssembly.cs b/com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessCompiledAssembly.cs deleted file mode 100644 index 3b2fbdc555..0000000000 --- a/com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessCompiledAssembly.cs +++ /dev/null @@ -1,42 +0,0 @@ -#if !UNITY_2020_2_OR_NEWER -using System.IO; -using Unity.CompilationPipeline.Common.ILPostProcessing; - -namespace MLAPI.Editor.CodeGen -{ - internal class ILPostProcessCompiledAssembly : ICompiledAssembly - { - private readonly string m_AssemblyFilename; - private readonly string m_OutputPath; - private InMemoryAssembly m_InMemoryAssembly; - - public ILPostProcessCompiledAssembly(string asmName, string[] refs, string[] defines, string outputPath) - { - m_AssemblyFilename = asmName; - Name = Path.GetFileNameWithoutExtension(m_AssemblyFilename); - References = refs; - Defines = defines; - m_OutputPath = outputPath; - } - - public string Name { get; } - public string[] References { get; } - public string[] Defines { get; } - - public InMemoryAssembly InMemoryAssembly - { - get - { - if (m_InMemoryAssembly == null) - { - m_InMemoryAssembly = new InMemoryAssembly( - File.ReadAllBytes(Path.Combine(m_OutputPath, m_AssemblyFilename)), - File.ReadAllBytes(Path.Combine(m_OutputPath, $"{Path.GetFileNameWithoutExtension(m_AssemblyFilename)}.pdb"))); - } - - return m_InMemoryAssembly; - } - } - } -} -#endif diff --git a/com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessCompiledAssembly.cs.meta b/com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessCompiledAssembly.cs.meta deleted file mode 100644 index 816d39fbe3..0000000000 --- a/com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessCompiledAssembly.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: c63d199856aa44f4581ec4de75bf3f44 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessor.cs b/com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessor.cs deleted file mode 100644 index 20b0c9ed99..0000000000 --- a/com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessor.cs +++ /dev/null @@ -1,13 +0,0 @@ -#if !UNITY_2020_2_OR_NEWER -using Unity.CompilationPipeline.Common.ILPostProcessing; - -namespace MLAPI.Editor.CodeGen -{ - public abstract class ILPostProcessor - { - public abstract bool WillProcess(ICompiledAssembly compiledAssembly); - public abstract ILPostProcessResult Process(ICompiledAssembly compiledAssembly); - public abstract ILPostProcessor GetInstance(); - } -} -#endif diff --git a/com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessor.cs.meta b/com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessor.cs.meta deleted file mode 100644 index 6f20ed67ae..0000000000 --- a/com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessor.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: cbffd9b273517da4a9c4a3218771e0b5 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessorProgram.cs b/com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessorProgram.cs deleted file mode 100644 index 29de2bf690..0000000000 --- a/com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessorProgram.cs +++ /dev/null @@ -1,242 +0,0 @@ -#if !UNITY_2020_2_OR_NEWER -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using Unity.CompilationPipeline.Common.Diagnostics; -using Unity.CompilationPipeline.Common.ILPostProcessing; -using UnityEditor; -using UnityEditor.Compilation; -using UnityEngine; - -using Assembly = System.Reflection.Assembly; - -using ILPPInterface = Unity.CompilationPipeline.Common.ILPostProcessing.ILPostProcessor; - -namespace MLAPI.Editor.CodeGen -{ - // There is a behaviour difference between 2019.4 and 2020+ codegen - // that essentially does checking on the existence of ILPP vs if a CodeGen assembly - // is present. So in order to make sure ILPP runs properly in 2019.4 from a clean - // import of the project we add this dummy ILPP which forces the callback to made - // and meets the internal ScriptCompilation pipeline requirements - internal sealed class ILPP2019CodegenWorkaround : ILPPInterface - { - public override ILPPInterface GetInstance() - { - return this; - } - - public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly) - { - return null; - } - - public override bool WillProcess(ICompiledAssembly compiledAssembly) => compiledAssembly.References.Any(filePath => Path.GetFileNameWithoutExtension(filePath) == CodeGenHelpers.RuntimeAssemblyName); - - } - - internal static class ILPostProcessProgram - { - private static ILPostProcessor[] s_ILPostProcessors { get; set; } - - [InitializeOnLoadMethod] - private static void OnInitializeOnLoad() - { - CompilationPipeline.assemblyCompilationFinished += OnCompilationFinished; - s_ILPostProcessors = FindAllPostProcessors(); - } - - private static ILPostProcessor[] FindAllPostProcessors() - { - var typesDerivedFrom = TypeCache.GetTypesDerivedFrom(); - var localILPostProcessors = new List(typesDerivedFrom.Count); - - foreach (var typeCollection in typesDerivedFrom) - { - try - { - localILPostProcessors.Add((ILPostProcessor)Activator.CreateInstance(typeCollection)); - } - catch (Exception exception) - { - Debug.LogError($"Could not create {nameof(ILPostProcessor)} ({typeCollection.FullName}):{Environment.NewLine}{exception.StackTrace}"); - } - } - - // Default sort by type full name - localILPostProcessors.Sort((left, right) => string.Compare(left.GetType().FullName, right.GetType().FullName, StringComparison.Ordinal)); - - return localILPostProcessors.ToArray(); - } - - private static void OnCompilationFinished(string targetAssembly, CompilerMessage[] messages) - { - if (messages.Length > 0) - { - if (messages.Any(msg => msg.type == CompilerMessageType.Error)) - { - return; - } - } - - // Should not run on the editor only assemblies - if (targetAssembly.Contains("-Editor") || targetAssembly.Contains(".Editor")) - { - return; - } - - // Should not run on Unity Engine modules but we can run on the MLAPI Runtime DLL - if ((targetAssembly.Contains("com.unity") || Path.GetFileName(targetAssembly).StartsWith("Unity")) && !targetAssembly.Contains("Unity.Multiplayer.")) - { - return; - } - - // Debug.Log($"Running MLAPI ILPP on {targetAssembly}"); - - var outputDirectory = $"{Application.dataPath}/../{Path.GetDirectoryName(targetAssembly)}"; - var unityEngine = string.Empty; - var mlapiRuntimeAssemblyPath = string.Empty; - var assemblies = AppDomain.CurrentDomain.GetAssemblies(); - var usesMLAPI = false; - var foundThisAssembly = false; - - var depenencyPaths = new List(); - foreach (var assembly in assemblies) - { - // Find the assembly currently being compiled from domain assembly list and check if it's using unet - if (assembly.GetName().Name == Path.GetFileNameWithoutExtension(targetAssembly)) - { - foundThisAssembly = true; - foreach (var dependency in assembly.GetReferencedAssemblies()) - { - // Since this assembly is already loaded in the domain this is a no-op and returns the - // already loaded assembly - depenencyPaths.Add(Assembly.Load(dependency).Location); - if (dependency.Name.Contains(CodeGenHelpers.RuntimeAssemblyName)) - { - usesMLAPI = true; - } - } - } - - try - { - if (assembly.Location.Contains("UnityEngine.CoreModule")) - { - unityEngine = assembly.Location; - } - - if (assembly.Location.Contains(CodeGenHelpers.RuntimeAssemblyName)) - { - mlapiRuntimeAssemblyPath = assembly.Location; - } - } - catch (NotSupportedException) - { - // in memory assembly, can't get location - } - } - - if (!foundThisAssembly) - { - // Target assembly not found in current domain, trying to load it to check references - // will lead to trouble in the build pipeline, so lets assume it should go to weaver. - // Add all assemblies in current domain to dependency list since there could be a - // dependency lurking there (there might be generated assemblies so ignore file not found exceptions). - // (can happen in runtime test framework on editor platform and when doing full library reimport) - foreach (var assembly in assemblies) - { - try - { - if (!(assembly.ManifestModule is System.Reflection.Emit.ModuleBuilder)) - { - depenencyPaths.Add(Assembly.Load(assembly.GetName().Name).Location); - } - } - catch (FileNotFoundException) - { - } - } - - usesMLAPI = true; - } - - // We check if we are the MLAPI! - if (!usesMLAPI) - { - // we shall also check and see if it we are ourself - usesMLAPI = targetAssembly.Contains(CodeGenHelpers.RuntimeAssemblyName); - } - - if (!usesMLAPI) - { - return; - } - - if (string.IsNullOrEmpty(unityEngine)) - { - Debug.LogError("Failed to find UnityEngine assembly"); - return; - } - - if (string.IsNullOrEmpty(mlapiRuntimeAssemblyPath)) - { - Debug.LogError("Failed to find mlapi runtime assembly"); - return; - } - - var assemblyPathName = Path.GetFileName(targetAssembly); - - var targetCompiledAssembly = new ILPostProcessCompiledAssembly(assemblyPathName, depenencyPaths.ToArray(), null, outputDirectory); - - void WriteAssembly(InMemoryAssembly inMemoryAssembly, string outputPath, string assName) - { - if (inMemoryAssembly == null) - { - throw new ArgumentException("InMemoryAssembly has never been accessed or modified"); - } - - var asmPath = Path.Combine(outputPath, assName); - var pdbFileName = $"{Path.GetFileNameWithoutExtension(assName)}.pdb"; - var pdbPath = Path.Combine(outputPath, pdbFileName); - - File.WriteAllBytes(asmPath, inMemoryAssembly.PeData); - File.WriteAllBytes(pdbPath, inMemoryAssembly.PdbData); - } - - foreach (var i in s_ILPostProcessors) - { - var result = i.Process(targetCompiledAssembly); - if (result == null) - { - continue; - } - - if (result.Diagnostics.Count > 0) - { - Debug.LogError($"{nameof(ILPostProcessor)} - {i.GetType().Name} failed to run on {targetCompiledAssembly.Name}"); - - foreach (var message in result.Diagnostics) - { - switch (message.DiagnosticType) - { - case DiagnosticType.Error: - Debug.LogError($"{nameof(ILPostProcessor)} Error - {message.MessageData} {message.File}:{message.Line}"); - break; - case DiagnosticType.Warning: - Debug.LogWarning($"{nameof(ILPostProcessor)} Warning - {message.MessageData} {message.File}:{message.Line}"); - break; - } - } - - continue; - } - - // we now need to write out the result? - WriteAssembly(result.InMemoryAssembly, outputDirectory, assemblyPathName); - } - } - } -} -#endif diff --git a/com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessorProgram.cs.meta b/com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessorProgram.cs.meta deleted file mode 100644 index e57c9a8c98..0000000000 --- a/com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessorProgram.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 72c7d2bd3d748db4d988e2204fe1083e -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/com.unity.multiplayer.mlapi/Editor/CodeGen/NetworkBehaviourILPP.cs b/com.unity.multiplayer.mlapi/Editor/CodeGen/NetworkBehaviourILPP.cs index d7e44a3127..2e37e6cd40 100644 --- a/com.unity.multiplayer.mlapi/Editor/CodeGen/NetworkBehaviourILPP.cs +++ b/com.unity.multiplayer.mlapi/Editor/CodeGen/NetworkBehaviourILPP.cs @@ -14,11 +14,7 @@ using UnityEngine; using MethodAttributes = Mono.Cecil.MethodAttributes; using ParameterAttributes = Mono.Cecil.ParameterAttributes; -#if UNITY_2020_2_OR_NEWER using ILPPInterface = Unity.CompilationPipeline.Common.ILPostProcessing.ILPostProcessor; -#else -using ILPPInterface = MLAPI.Editor.CodeGen.ILPostProcessor; -#endif namespace MLAPI.Editor.CodeGen { @@ -93,8 +89,8 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly) private MethodReference m_NetworkManager_getIsServer_MethodRef; private MethodReference m_NetworkManager_getIsClient_MethodRef; private FieldReference m_NetworkManager_LogLevel_FieldRef; - private FieldReference m_NetworkManager_ntable_FieldRef; - private MethodReference m_NetworkManager_ntable_Add_MethodRef; + private FieldReference m_NetworkManager_rpc_func_table_FieldRef; + private MethodReference m_NetworkManager_rpc_func_table_Add_MethodRef; private FieldReference m_NetworkManager_rpc_name_table_FieldRef; private MethodReference m_NetworkManager_rpc_name_table_Add_MethodRef; private TypeReference m_NetworkBehaviour_TypeRef; @@ -102,7 +98,7 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly) private MethodReference m_NetworkBehaviour_EndSendServerRpc_MethodRef; private MethodReference m_NetworkBehaviour_BeginSendClientRpc_MethodRef; private MethodReference m_NetworkBehaviour_EndSendClientRpc_MethodRef; - private FieldReference m_NetworkBehaviour_nexec_FieldRef; + private FieldReference m_NetworkBehaviour_rpc_exec_stage_FieldRef; private MethodReference m_NetworkBehaviour_getNetworkManager_MethodRef; private MethodReference m_NetworkBehaviour_getOwnerClientId_MethodRef; private MethodReference m_NetworkHandlerDelegateCtor_MethodRef; @@ -164,25 +160,21 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly) private const string k_NetworkManager_IsServer = nameof(NetworkManager.IsServer); private const string k_NetworkManager_IsClient = nameof(NetworkManager.IsClient); private const string k_NetworkManager_LogLevel = nameof(NetworkManager.LogLevel); -#pragma warning disable 618 - private const string k_NetworkManager_ntable = nameof(NetworkManager.__ntable); + private const string k_NetworkManager_rpc_func_table = nameof(NetworkManager.__rpc_func_table); private const string k_NetworkManager_rpc_name_table = nameof(NetworkManager.__rpc_name_table); private const string k_NetworkBehaviour_BeginSendServerRpc = nameof(NetworkBehaviour.__beginSendServerRpc); private const string k_NetworkBehaviour_EndSendServerRpc = nameof(NetworkBehaviour.__endSendServerRpc); private const string k_NetworkBehaviour_BeginSendClientRpc = nameof(NetworkBehaviour.__beginSendClientRpc); private const string k_NetworkBehaviour_EndSendClientRpc = nameof(NetworkBehaviour.__endSendClientRpc); - private const string k_NetworkBehaviour_nexec = nameof(NetworkBehaviour.__nexec); -#pragma warning restore 618 + private const string k_NetworkBehaviour_rpc_exec_stage = nameof(NetworkBehaviour.__rpc_exec_stage); private const string k_NetworkBehaviour_NetworkManager = nameof(NetworkBehaviour.NetworkManager); private const string k_NetworkBehaviour_OwnerClientId = nameof(NetworkBehaviour.OwnerClientId); private const string k_RpcAttribute_Delivery = nameof(RpcAttribute.Delivery); private const string k_ServerRpcAttribute_RequireOwnership = nameof(ServerRpcAttribute.RequireOwnership); -#pragma warning disable 618 private const string k_RpcParams_Server = nameof(__RpcParams.Server); private const string k_RpcParams_Client = nameof(__RpcParams.Client); -#pragma warning restore 618 private const string k_ServerRpcParams_Receive = nameof(ServerRpcParams.Receive); private const string k_ServerRpcReceiveParams_SenderClientId = nameof(ServerRpcReceiveParams.SenderClientId); @@ -234,9 +226,9 @@ private bool ImportReferences(ModuleDefinition moduleDefinition) case k_NetworkManager_LogLevel: m_NetworkManager_LogLevel_FieldRef = moduleDefinition.ImportReference(fieldInfo); break; - case k_NetworkManager_ntable: - m_NetworkManager_ntable_FieldRef = moduleDefinition.ImportReference(fieldInfo); - m_NetworkManager_ntable_Add_MethodRef = moduleDefinition.ImportReference(fieldInfo.FieldType.GetMethod("Add")); + case k_NetworkManager_rpc_func_table: + m_NetworkManager_rpc_func_table_FieldRef = moduleDefinition.ImportReference(fieldInfo); + m_NetworkManager_rpc_func_table_Add_MethodRef = moduleDefinition.ImportReference(fieldInfo.FieldType.GetMethod("Add")); break; case k_NetworkManager_rpc_name_table: m_NetworkManager_rpc_name_table_FieldRef = moduleDefinition.ImportReference(fieldInfo); @@ -283,13 +275,12 @@ private bool ImportReferences(ModuleDefinition moduleDefinition) { switch (fieldInfo.Name) { - case k_NetworkBehaviour_nexec: - m_NetworkBehaviour_nexec_FieldRef = moduleDefinition.ImportReference(fieldInfo); + case k_NetworkBehaviour_rpc_exec_stage: + m_NetworkBehaviour_rpc_exec_stage_FieldRef = moduleDefinition.ImportReference(fieldInfo); break; } } -#pragma warning disable 618 var networkHandlerDelegateType = typeof(Action); m_NetworkHandlerDelegateCtor_MethodRef = moduleDefinition.ImportReference(networkHandlerDelegateType.GetConstructor(new[] { typeof(object), typeof(IntPtr) })); @@ -307,7 +298,6 @@ private bool ImportReferences(ModuleDefinition moduleDefinition) break; } } -#pragma warning restore 618 var serverRpcParamsType = typeof(ServerRpcParams); m_ServerRpcParams_TypeRef = moduleDefinition.ImportReference(serverRpcParamsType); @@ -584,13 +574,13 @@ private void ProcessNetworkBehaviour(TypeDefinition typeDefinition, string[] ass { typeDefinition.Methods.Add(rpcHandler); - // NetworkManager.__ntable.Add(RpcHash, HandleFunc); - instructions.Add(processor.Create(OpCodes.Ldsfld, m_NetworkManager_ntable_FieldRef)); + // NetworkManager.__rpc_func_table.Add(RpcHash, HandleFunc); + instructions.Add(processor.Create(OpCodes.Ldsfld, m_NetworkManager_rpc_func_table_FieldRef)); instructions.Add(processor.Create(OpCodes.Ldc_I4, unchecked((int)rpcHash))); instructions.Add(processor.Create(OpCodes.Ldnull)); instructions.Add(processor.Create(OpCodes.Ldftn, rpcHandler)); instructions.Add(processor.Create(OpCodes.Newobj, m_NetworkHandlerDelegateCtor_MethodRef)); - instructions.Add(processor.Create(OpCodes.Call, m_NetworkManager_ntable_Add_MethodRef)); + instructions.Add(processor.Create(OpCodes.Call, m_NetworkManager_rpc_func_table_Add_MethodRef)); } foreach (var (rpcHash, rpcName) in rpcNames) @@ -773,13 +763,11 @@ private void InjectWriteAndCallBlocks(MethodDefinition methodDefinition, CustomA var endInstr = processor.Create(OpCodes.Nop); var lastInstr = processor.Create(OpCodes.Nop); - // if (__nexec != NExec.Server) -> ServerRpc - // if (__nexec != NExec.Client) -> ClientRpc + // if (__rpc_exec_stage != __RpcExecStage.Server) -> ServerRpc + // if (__rpc_exec_stage != __RpcExecStage.Client) -> ClientRpc instructions.Add(processor.Create(OpCodes.Ldarg_0)); - instructions.Add(processor.Create(OpCodes.Ldfld, m_NetworkBehaviour_nexec_FieldRef)); -#pragma warning disable 618 - instructions.Add(processor.Create(OpCodes.Ldc_I4, (int)(isServerRpc ? NetworkBehaviour.__NExec.Server : NetworkBehaviour.__NExec.Client))); -#pragma warning restore 618 + instructions.Add(processor.Create(OpCodes.Ldfld, m_NetworkBehaviour_rpc_exec_stage_FieldRef)); + instructions.Add(processor.Create(OpCodes.Ldc_I4, (int)(isServerRpc ? NetworkBehaviour.__RpcExecStage.Server : NetworkBehaviour.__RpcExecStage.Client))); instructions.Add(processor.Create(OpCodes.Ceq)); instructions.Add(processor.Create(OpCodes.Ldc_I4, 0)); instructions.Add(processor.Create(OpCodes.Ceq)); @@ -1719,13 +1707,11 @@ private void InjectWriteAndCallBlocks(MethodDefinition methodDefinition, CustomA var returnInstr = processor.Create(OpCodes.Ret); var lastInstr = processor.Create(OpCodes.Nop); - // if (__nexec == NExec.Server) -> ServerRpc - // if (__nexec == NExec.Client) -> ClientRpc + // if (__rpc_exec_stage == __RpcExecStage.Server) -> ServerRpc + // if (__rpc_exec_stage == __RpcExecStage.Client) -> ClientRpc instructions.Add(processor.Create(OpCodes.Ldarg_0)); - instructions.Add(processor.Create(OpCodes.Ldfld, m_NetworkBehaviour_nexec_FieldRef)); -#pragma warning disable 618 - instructions.Add(processor.Create(OpCodes.Ldc_I4, (int)(isServerRpc ? NetworkBehaviour.__NExec.Server : NetworkBehaviour.__NExec.Client))); -#pragma warning restore 618 + instructions.Add(processor.Create(OpCodes.Ldfld, m_NetworkBehaviour_rpc_exec_stage_FieldRef)); + instructions.Add(processor.Create(OpCodes.Ldc_I4, (int)(isServerRpc ? NetworkBehaviour.__RpcExecStage.Server : NetworkBehaviour.__RpcExecStage.Client))); instructions.Add(processor.Create(OpCodes.Ceq)); instructions.Add(processor.Create(OpCodes.Brfalse, returnInstr)); @@ -2638,13 +2624,11 @@ private MethodDefinition GenerateStaticHandler(MethodDefinition methodDefinition } } - // NetworkBehaviour.__nexec = NExec.Server; -> ServerRpc - // NetworkBehaviour.__nexec = NExec.Client; -> ClientRpc + // NetworkBehaviour.__rpc_exec_stage = __RpcExecStage.Server; -> ServerRpc + // NetworkBehaviour.__rpc_exec_stage = __RpcExecStage.Client; -> ClientRpc processor.Emit(OpCodes.Ldarg_0); -#pragma warning disable 618 - processor.Emit(OpCodes.Ldc_I4, (int)(isServerRpc ? NetworkBehaviour.__NExec.Server : NetworkBehaviour.__NExec.Client)); -#pragma warning restore 618 - processor.Emit(OpCodes.Stfld, m_NetworkBehaviour_nexec_FieldRef); + processor.Emit(OpCodes.Ldc_I4, (int)(isServerRpc ? NetworkBehaviour.__RpcExecStage.Server : NetworkBehaviour.__RpcExecStage.Client)); + processor.Emit(OpCodes.Stfld, m_NetworkBehaviour_rpc_exec_stage_FieldRef); // NetworkBehaviour.XXXRpc(...); processor.Emit(OpCodes.Ldarg_0); @@ -2652,12 +2636,10 @@ private MethodDefinition GenerateStaticHandler(MethodDefinition methodDefinition Enumerable.Range(0, paramCount).ToList().ForEach(paramIndex => processor.Emit(OpCodes.Ldloc, paramLocalMap[paramIndex])); processor.Emit(OpCodes.Callvirt, methodDefinition); - // NetworkBehaviour.__nexec = NExec.None; + // NetworkBehaviour.__rpc_exec_stage = __RpcExecStage.None; processor.Emit(OpCodes.Ldarg_0); -#pragma warning disable 618 - processor.Emit(OpCodes.Ldc_I4, (int)NetworkBehaviour.__NExec.None); -#pragma warning restore 618 - processor.Emit(OpCodes.Stfld, m_NetworkBehaviour_nexec_FieldRef); + processor.Emit(OpCodes.Ldc_I4, (int)NetworkBehaviour.__RpcExecStage.None); + processor.Emit(OpCodes.Stfld, m_NetworkBehaviour_rpc_exec_stage_FieldRef); processor.Emit(OpCodes.Ret); return nhandler; diff --git a/com.unity.multiplayer.mlapi/Editor/CodeGen/RuntimeAccessModifiersILPP.cs b/com.unity.multiplayer.mlapi/Editor/CodeGen/RuntimeAccessModifiersILPP.cs index 0b86b809dd..08033ccfc0 100644 --- a/com.unity.multiplayer.mlapi/Editor/CodeGen/RuntimeAccessModifiersILPP.cs +++ b/com.unity.multiplayer.mlapi/Editor/CodeGen/RuntimeAccessModifiersILPP.cs @@ -1,12 +1,9 @@ -#if UNITY_2020_2_OR_NEWER using System.Collections.Generic; using System.IO; -using System.Linq; using Mono.Cecil; using Mono.Cecil.Cil; using Unity.CompilationPipeline.Common.Diagnostics; using Unity.CompilationPipeline.Common.ILPostProcessing; - using ILPPInterface = Unity.CompilationPipeline.Common.ILPostProcessing.ILPostProcessor; namespace MLAPI.Editor.CodeGen @@ -86,17 +83,14 @@ private void ProcessNetworkManager(TypeDefinition typeDefinition, string[] assem { foreach (var fieldDefinition in typeDefinition.Fields) { - if (fieldDefinition.Name == nameof(NetworkManager.__ntable)) + if (fieldDefinition.Name == nameof(NetworkManager.__rpc_func_table)) { fieldDefinition.IsPublic = true; } - if (assemblyDefines.Contains("UNITY_EDITOR") || assemblyDefines.Contains("DEVELOPMENT_BUILD")) + if (fieldDefinition.Name == nameof(NetworkManager.__rpc_name_table)) { - if (fieldDefinition.Name == nameof(NetworkManager.__rpc_name_table)) - { - fieldDefinition.IsPublic = true; - } + fieldDefinition.IsPublic = true; } } } @@ -105,7 +99,7 @@ private void ProcessNetworkBehaviour(TypeDefinition typeDefinition) { foreach (var nestedType in typeDefinition.NestedTypes) { - if (nestedType.Name == nameof(NetworkBehaviour.__NExec)) + if (nestedType.Name == nameof(NetworkBehaviour.__RpcExecStage)) { nestedType.IsNestedFamily = true; } @@ -113,7 +107,7 @@ private void ProcessNetworkBehaviour(TypeDefinition typeDefinition) foreach (var fieldDefinition in typeDefinition.Fields) { - if (fieldDefinition.Name == nameof(NetworkBehaviour.__nexec)) + if (fieldDefinition.Name == nameof(NetworkBehaviour.__rpc_exec_stage)) { fieldDefinition.IsFamily = true; } @@ -134,4 +128,3 @@ private void ProcessNetworkBehaviour(TypeDefinition typeDefinition) } } } -#endif diff --git a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs index 0c83ecfc84..fa6c530057 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using System.ComponentModel; -using System.Diagnostics; using UnityEngine; using System.Reflection; using System.Linq; @@ -14,7 +12,6 @@ using MLAPI.Reflection; using MLAPI.Serialization; using MLAPI.Serialization.Pooled; -using MLAPI.Spawning; using MLAPI.Transports; using Unity.Profiling; @@ -26,15 +23,8 @@ namespace MLAPI public abstract class NetworkBehaviour : MonoBehaviour { #pragma warning disable IDE1006 // disable naming rule violation check - [Browsable(false)] - [EditorBrowsable(EditorBrowsableState.Never)] -#if UNITY_2020_2_OR_NEWER // RuntimeAccessModifiersILPP will make this `protected` - internal enum __NExec -#else - [Obsolete("Please do not use, will no longer be exposed in the future versions (framework internal)")] - public enum __NExec -#endif + internal enum __RpcExecStage #pragma warning restore IDE1006 // restore naming rule violation check { None = 0, @@ -42,32 +32,17 @@ public enum __NExec Client = 2 } -#pragma warning disable 414 +#pragma warning disable 414 // disable assigned but its value is never used #pragma warning disable IDE1006 // disable naming rule violation check [NonSerialized] - [Browsable(false)] - [EditorBrowsable(EditorBrowsableState.Never)] - [DebuggerBrowsable(DebuggerBrowsableState.Never)] -#if UNITY_2020_2_OR_NEWER // RuntimeAccessModifiersILPP will make this `protected` - internal __NExec __nexec = __NExec.None; -#else - [Obsolete("Please do not use, will no longer be exposed in the future versions (framework internal)")] - public __NExec __nexec = __NExec.None; -#endif -#pragma warning restore 414 -#pragma warning restore IDE1006 // restore naming rule violation check + internal __RpcExecStage __rpc_exec_stage = __RpcExecStage.None; +#pragma warning restore 414 // restore assigned but its value is never used +#pragma warning restore IDE1006 // restore naming rule violation #pragma warning disable IDE1006 // disable naming rule violation check - [Browsable(false)] - [EditorBrowsable(EditorBrowsableState.Never)] -#if UNITY_2020_2_OR_NEWER // RuntimeAccessModifiersILPP will make this `protected` internal NetworkSerializer __beginSendServerRpc(ServerRpcParams serverRpcParams, RpcDelivery rpcDelivery) -#else - [Obsolete("Please do not use, will no longer be exposed in the future versions (framework internal)")] - public NetworkSerializer __beginSendServerRpc(ServerRpcParams serverRpcParams, RpcDelivery rpcDelivery) -#endif #pragma warning restore IDE1006 // restore naming rule violation check { PooledNetworkWriter writer; @@ -104,15 +79,8 @@ public NetworkSerializer __beginSendServerRpc(ServerRpcParams serverRpcParams, R } #pragma warning disable IDE1006 // disable naming rule violation check - [Browsable(false)] - [EditorBrowsable(EditorBrowsableState.Never)] -#if UNITY_2020_2_OR_NEWER // RuntimeAccessModifiersILPP will make this `protected` internal void __endSendServerRpc(NetworkSerializer serializer, ServerRpcParams serverRpcParams, RpcDelivery rpcDelivery) -#else - [Obsolete("Please do not use, will no longer be exposed in the future versions (framework internal)")] - public void __endSendServerRpc(NetworkSerializer serializer, ServerRpcParams serverRpcParams, RpcDelivery rpcDelivery) -#endif #pragma warning restore IDE1006 // restore naming rule violation check { if (serializer == null) @@ -132,15 +100,8 @@ public void __endSendServerRpc(NetworkSerializer serializer, ServerRpcParams ser } #pragma warning disable IDE1006 // disable naming rule violation check - [Browsable(false)] - [EditorBrowsable(EditorBrowsableState.Never)] -#if UNITY_2020_2_OR_NEWER // RuntimeAccessModifiersILPP will make this `protected` internal NetworkSerializer __beginSendClientRpc(ClientRpcParams clientRpcParams, RpcDelivery rpcDelivery) -#else - [Obsolete("Please do not use, will no longer be exposed in the future versions (framework internal)")] - public NetworkSerializer __beginSendClientRpc(ClientRpcParams clientRpcParams, RpcDelivery rpcDelivery) -#endif #pragma warning restore IDE1006 // restore naming rule violation check { PooledNetworkWriter writer; @@ -209,15 +170,8 @@ public NetworkSerializer __beginSendClientRpc(ClientRpcParams clientRpcParams, R } #pragma warning disable IDE1006 // disable naming rule violation check - [Browsable(false)] - [EditorBrowsable(EditorBrowsableState.Never)] -#if UNITY_2020_2_OR_NEWER // RuntimeAccessModifiersILPP will make this `protected` internal void __endSendClientRpc(NetworkSerializer serializer, ClientRpcParams clientRpcParams, RpcDelivery rpcDelivery) -#else - [Obsolete("Please do not use, will no longer be exposed in the future versions (framework internal)")] - public void __endSendClientRpc(NetworkSerializer serializer, ClientRpcParams clientRpcParams, RpcDelivery rpcDelivery) -#endif #pragma warning restore IDE1006 // restore naming rule violation check { if (serializer == null) diff --git a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs index 37c78a3a11..c2c9af165b 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs @@ -1,8 +1,6 @@ using System; using System.Collections; using System.Collections.Generic; -using System.ComponentModel; -using System.Diagnostics; using UnityEngine; using MLAPI.Logging; using MLAPI.Configuration; @@ -29,27 +27,16 @@ namespace MLAPI public class NetworkManager : MonoBehaviour, INetworkUpdateSystem, IProfilableTransportProvider { #pragma warning disable IDE1006 // disable naming rule violation check - [Browsable(false)] - [EditorBrowsable(EditorBrowsableState.Never)] - [DebuggerBrowsable(DebuggerBrowsableState.Never)] -#if UNITY_2020_2_OR_NEWER // RuntimeAccessModifiersILPP will make this `public` - internal static readonly Dictionary> __ntable = new Dictionary>(); + internal static readonly Dictionary> __rpc_func_table = new Dictionary>(); #if UNITY_EDITOR || DEVELOPMENT_BUILD // RuntimeAccessModifiersILPP will make this `public` internal static readonly Dictionary __rpc_name_table = new Dictionary(); #else // !(UNITY_EDITOR || DEVELOPMENT_BUILD) - // RuntimeAccessModifiersILPP and NetworkBehaviourILPP requires a variable with this name to exist when outside of Debug build or Editor. - // If the name doesn't exist, generation and compilation errors occurs. - // For the sake of consistency, we are using the same type. - // Final note: this variable will not be used. Since it's never initialized, it will not be in the memory footprint. - internal static readonly Dictionary __rpc_name_table = null; + // RuntimeAccessModifiersILPP will make this `public` + internal static readonly Dictionary __rpc_name_table = null; // not needed on release builds #endif // UNITY_EDITOR || DEVELOPMENT_BUILD -#else // !UNITY_2020_2_OR_NEWER - [Obsolete("Please do not use, will no longer be exposed in the future versions (framework internal)")] - public static readonly Dictionary> __ntable = new Dictionary>(); -#endif // UNITY_2020_2_OR_NEWER #pragma warning restore IDE1006 // restore naming rule violation check #if DEVELOPMENT_BUILD || UNITY_EDITOR @@ -65,8 +52,8 @@ public class NetworkManager : MonoBehaviour, INetworkUpdateSystem, IProfilableTr // todo: transitional. For the next release, only Snapshot should remain // The booleans allow iterative development and testing in the meantime - static internal bool UseClassicDelta = true; - static internal bool UseSnapshot = false; + internal static bool UseClassicDelta = true; + internal static bool UseSnapshot = false; internal RpcQueueContainer RpcQueueContainer { get; private set; } internal NetworkTickSystem NetworkTickSystem { get; private set; } @@ -390,7 +377,7 @@ private void Init(bool server) // This should never happen, but in the event that it does there should be (at a minimum) a unity error logged. if (RpcQueueContainer != null) { - UnityEngine.Debug.LogError("Init was invoked, but rpcQueueContainer was already initialized! (destroying previous instance)"); + Debug.LogError("Init was invoked, but rpcQueueContainer was already initialized! (destroying previous instance)"); RpcQueueContainer.Dispose(); RpcQueueContainer = null; } @@ -445,7 +432,7 @@ private void Init(bool server) } // Provide the name of the prefab with issues so the user can more easily find the prefab and fix it - UnityEngine.Debug.LogWarning($"{nameof(NetworkPrefab)} (\"{NetworkConfig.NetworkPrefabs[i].Prefab.name}\") will be removed and ignored."); + Debug.LogWarning($"{nameof(NetworkPrefab)} (\"{NetworkConfig.NetworkPrefabs[i].Prefab.name}\") will be removed and ignored."); removeEmptyPrefabs.Add(i); continue; @@ -473,7 +460,7 @@ private void Init(bool server) else { // This should never happen, but in the case it somehow does log an error and remove the duplicate entry - UnityEngine.Debug.LogError($"{nameof(NetworkPrefab)} (\"{NetworkConfig.NetworkPrefabs[i].Prefab.name}\") has a duplicate {nameof(NetworkObject.GlobalObjectIdHash)} {networkObject.GlobalObjectIdHash} entry! Removing entry from list!"); + Debug.LogError($"{nameof(NetworkPrefab)} (\"{NetworkConfig.NetworkPrefabs[i].Prefab.name}\") has a duplicate {nameof(NetworkObject.GlobalObjectIdHash)} {networkObject.GlobalObjectIdHash} entry! Removing entry from list!"); removeEmptyPrefabs.Add(i); } } @@ -497,7 +484,7 @@ private void Init(bool server) else { // Provide the name of the prefab with issues so the user can more easily find the prefab and fix it - UnityEngine.Debug.LogError($"{nameof(NetworkConfig.PlayerPrefab)} (\"{NetworkConfig.PlayerPrefab.name}\") has no NetworkObject assigned to it!."); + Debug.LogError($"{nameof(NetworkConfig.PlayerPrefab)} (\"{NetworkConfig.PlayerPrefab.name}\") has no NetworkObject assigned to it!."); } } @@ -1367,7 +1354,6 @@ private void ReceiveCallback(NetworkBuffer messageBuffer, RpcQueueContainer.Queu /// Called when an inbound queued RPC is invoked /// /// frame queue item to invoke -#pragma warning disable 618 internal void InvokeRpc(RpcFrameQueueItem queueItem) { #if DEVELOPMENT_BUILD || UNITY_EDITOR @@ -1378,7 +1364,7 @@ internal void InvokeRpc(RpcFrameQueueItem queueItem) var networkUpdateStage = queueItem.NetworkReader.ReadByteDirect(); var networkMethodId = queueItem.NetworkReader.ReadUInt32Packed(); - if (__ntable.ContainsKey(networkMethodId)) + if (__rpc_func_table.ContainsKey(networkMethodId)) { if (!SpawnManager.SpawnedObjects.ContainsKey(networkObjectId)) { @@ -1417,9 +1403,8 @@ internal void InvokeRpc(RpcFrameQueueItem queueItem) break; } - __ntable[networkMethodId](networkBehaviour, new NetworkSerializer(queueItem.NetworkReader), rpcParams); + __rpc_func_table[networkMethodId](networkBehaviour, new NetworkSerializer(queueItem.NetworkReader), rpcParams); } -#pragma warning restore 618 #if DEVELOPMENT_BUILD || UNITY_EDITOR s_InvokeRpc.End(); diff --git a/com.unity.multiplayer.mlapi/Runtime/Messaging/RpcParams.cs b/com.unity.multiplayer.mlapi/Runtime/Messaging/RpcParams.cs index 7e0918da8b..31de57e1da 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Messaging/RpcParams.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Messaging/RpcParams.cs @@ -1,5 +1,3 @@ -using System; - namespace MLAPI.Messaging { public struct ServerRpcSendParams @@ -37,13 +35,8 @@ public struct ClientRpcParams } #pragma warning disable IDE1006 // disable naming rule violation check -#if UNITY_2020_2_OR_NEWER // RuntimeAccessModifiersILPP will make this `public` internal struct __RpcParams -#else - [Obsolete("Please do not use, will no longer be exposed in the future versions (framework internal)")] - public struct __RpcParams -#endif #pragma warning restore IDE1006 // restore naming rule violation check { public ServerRpcParams Server; diff --git a/com.unity.multiplayer.mlapi/Runtime/Transports/UNET/RelayTransport.cs b/com.unity.multiplayer.mlapi/Runtime/Transports/UNET/RelayTransport.cs index 1ae167c0d7..411a65f32a 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Transports/UNET/RelayTransport.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Transports/UNET/RelayTransport.cs @@ -1,4 +1,4 @@ -#pragma warning disable 618 +#pragma warning disable 618 // disable is obsolete #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member using System; using System.Collections.Generic; @@ -537,4 +537,4 @@ public InvalidConfigException(string issue) : base(issue) { } } } #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member -#pragma warning restore 618 +#pragma warning restore 618 // restore is obsolete diff --git a/com.unity.multiplayer.mlapi/Runtime/Transports/UNET/UNetTransport.cs b/com.unity.multiplayer.mlapi/Runtime/Transports/UNET/UNetTransport.cs index b7fea06266..697d222be9 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Transports/UNET/UNetTransport.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Transports/UNET/UNetTransport.cs @@ -1,4 +1,4 @@ -#pragma warning disable 618 +#pragma warning disable 618 // disable is obsolete #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member using System; using System.Collections.Generic; @@ -496,4 +496,4 @@ public IReadOnlyDictionary GetTransportProfilerData() } } #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member -#pragma warning restore 618 +#pragma warning restore 618 // restore is obsolete