mirror of
				https://github.com/DerTyp7/defrain-shooter-unity.git
				synced 2025-10-31 05:27:07 +01:00 
			
		
		
		
	CHANGED TO MIRROR
This commit is contained in:
		| @@ -0,0 +1,189 @@ | ||||
| // for Unity 2020+ we use ILPostProcessor. | ||||
| // only automatically invoke it for older versions. | ||||
| #if !UNITY_2020_3_OR_NEWER | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.IO; | ||||
| using System.Linq; | ||||
| using Mono.CecilX; | ||||
| using UnityEditor; | ||||
| using UnityEditor.Compilation; | ||||
| using UnityEngine; | ||||
| using UnityAssembly = UnityEditor.Compilation.Assembly; | ||||
|  | ||||
| namespace Mirror.Weaver | ||||
| { | ||||
|     public static class CompilationFinishedHook | ||||
|     { | ||||
|         // needs to be the same as Weaver.MirrorAssemblyName! | ||||
|         const string MirrorRuntimeAssemblyName = "Mirror"; | ||||
|         const string MirrorWeaverAssemblyName = "Mirror.Weaver"; | ||||
|  | ||||
|         // global weaver define so that tests can use it | ||||
|         internal static Weaver weaver; | ||||
|  | ||||
|         // delegate for subscription to Weaver warning messages | ||||
|         public static Action<string> OnWeaverWarning; | ||||
|         // delete for subscription to Weaver error messages | ||||
|         public static Action<string> OnWeaverError; | ||||
|  | ||||
|         // controls weather Weaver errors are reported direct to the Unity console (tests enable this) | ||||
|         public static bool UnityLogEnabled = true; | ||||
|  | ||||
|         [InitializeOnLoadMethod] | ||||
|         public static void OnInitializeOnLoad() | ||||
|         { | ||||
|             CompilationPipeline.assemblyCompilationFinished += OnCompilationFinished; | ||||
|  | ||||
|             // We only need to run this once per session | ||||
|             // after that, all assemblies will be weaved by the event | ||||
|             if (!SessionState.GetBool("MIRROR_WEAVED", false)) | ||||
|             { | ||||
|                 // reset session flag | ||||
|                 SessionState.SetBool("MIRROR_WEAVED", true); | ||||
|                 SessionState.SetBool("MIRROR_WEAVE_SUCCESS", true); | ||||
|  | ||||
|                 WeaveExistingAssemblies(); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public static void WeaveExistingAssemblies() | ||||
|         { | ||||
|             foreach (UnityAssembly assembly in CompilationPipeline.GetAssemblies()) | ||||
|             { | ||||
|                 if (File.Exists(assembly.outputPath)) | ||||
|                 { | ||||
|                     OnCompilationFinished(assembly.outputPath, new CompilerMessage[0]); | ||||
|                 } | ||||
|             } | ||||
|  | ||||
| #if UNITY_2019_3_OR_NEWER | ||||
|             EditorUtility.RequestScriptReload(); | ||||
| #else | ||||
|             UnityEditorInternal.InternalEditorUtility.RequestScriptReload(); | ||||
| #endif | ||||
|         } | ||||
|  | ||||
|         static Assembly FindCompilationPipelineAssembly(string assemblyName) => | ||||
|             CompilationPipeline.GetAssemblies().First(assembly => assembly.name == assemblyName); | ||||
|  | ||||
|         static bool CompilerMessagesContainError(CompilerMessage[] messages) => | ||||
|             messages.Any(msg => msg.type == CompilerMessageType.Error); | ||||
|  | ||||
|         public static void OnCompilationFinished(string assemblyPath, CompilerMessage[] messages) | ||||
|         { | ||||
|             // Do nothing if there were compile errors on the target | ||||
|             if (CompilerMessagesContainError(messages)) | ||||
|             { | ||||
|                 Debug.Log("Weaver: stop because compile errors on target"); | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
|             // Should not run on the editor only assemblies | ||||
|             if (assemblyPath.Contains("-Editor") || assemblyPath.Contains(".Editor")) | ||||
|             { | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
|             // don't weave mirror files | ||||
|             string assemblyName = Path.GetFileNameWithoutExtension(assemblyPath); | ||||
|             if (assemblyName == MirrorRuntimeAssemblyName || assemblyName == MirrorWeaverAssemblyName) | ||||
|             { | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
|             // find Mirror.dll | ||||
|             Assembly mirrorAssembly = FindCompilationPipelineAssembly(MirrorRuntimeAssemblyName); | ||||
|             if (mirrorAssembly == null) | ||||
|             { | ||||
|                 Debug.LogError("Failed to find Mirror runtime assembly"); | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
|             string mirrorRuntimeDll = mirrorAssembly.outputPath; | ||||
|             if (!File.Exists(mirrorRuntimeDll)) | ||||
|             { | ||||
|                 // this is normal, it happens with any assembly that is built before mirror | ||||
|                 // such as unity packages or your own assemblies | ||||
|                 // those don't need to be weaved | ||||
|                 // if any assembly depends on mirror, then it will be built after | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
|             // find UnityEngine.CoreModule.dll | ||||
|             string unityEngineCoreModuleDLL = UnityEditorInternal.InternalEditorUtility.GetEngineCoreModuleAssemblyPath(); | ||||
|             if (string.IsNullOrEmpty(unityEngineCoreModuleDLL)) | ||||
|             { | ||||
|                 Debug.LogError("Failed to find UnityEngine assembly"); | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
|             HashSet<string> dependencyPaths = GetDependencyPaths(assemblyPath); | ||||
|             dependencyPaths.Add(Path.GetDirectoryName(mirrorRuntimeDll)); | ||||
|             dependencyPaths.Add(Path.GetDirectoryName(unityEngineCoreModuleDLL)); | ||||
|  | ||||
|             if (!WeaveFromFile(assemblyPath, dependencyPaths.ToArray())) | ||||
|             { | ||||
|                 // Set false...will be checked in \Editor\EnterPlayModeSettingsCheck.CheckSuccessfulWeave() | ||||
|                 SessionState.SetBool("MIRROR_WEAVE_SUCCESS", false); | ||||
|                 if (UnityLogEnabled) Debug.LogError($"Weaving failed for {assemblyPath}"); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         static HashSet<string> GetDependencyPaths(string assemblyPath) | ||||
|         { | ||||
|             // build directory list for later asm/symbol resolving using CompilationPipeline refs | ||||
|             HashSet<string> dependencyPaths = new HashSet<string> | ||||
|             { | ||||
|                 Path.GetDirectoryName(assemblyPath) | ||||
|             }; | ||||
|             foreach (Assembly assembly in CompilationPipeline.GetAssemblies()) | ||||
|             { | ||||
|                 if (assembly.outputPath == assemblyPath) | ||||
|                 { | ||||
|                     foreach (string reference in assembly.compiledAssemblyReferences) | ||||
|                     { | ||||
|                         dependencyPaths.Add(Path.GetDirectoryName(reference)); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             return dependencyPaths; | ||||
|         } | ||||
|         // helper function to invoke Weaver with an AssemblyDefinition from a | ||||
|         // file path, with dependencies added. | ||||
|         static bool WeaveFromFile(string assemblyPath, string[] dependencies) | ||||
|         { | ||||
|             // resolve assembly from stream | ||||
|             using (DefaultAssemblyResolver asmResolver = new DefaultAssemblyResolver()) | ||||
|             using (AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(assemblyPath, new ReaderParameters{ ReadWrite = true, ReadSymbols = true, AssemblyResolver = asmResolver })) | ||||
|             { | ||||
|                 // add this assembly's path and unity's assembly path | ||||
|                 asmResolver.AddSearchDirectory(Path.GetDirectoryName(assemblyPath)); | ||||
|                 asmResolver.AddSearchDirectory(Helpers.UnityEngineDllDirectoryName()); | ||||
|  | ||||
|                 // add dependencies | ||||
|                 if (dependencies != null) | ||||
|                 { | ||||
|                     foreach (string path in dependencies) | ||||
|                     { | ||||
|                         asmResolver.AddSearchDirectory(path); | ||||
|                     } | ||||
|                 } | ||||
|  | ||||
|                 // create weaver with logger | ||||
|                 weaver = new Weaver(new CompilationFinishedLogger()); | ||||
|                 if (weaver.Weave(assembly, asmResolver, out bool modified)) | ||||
|                 { | ||||
|                     // write changes to file if modified | ||||
|                     if (modified) | ||||
|                         assembly.Write(new WriterParameters{WriteSymbols = true}); | ||||
|  | ||||
|                     return true; | ||||
|                 } | ||||
|                 return false; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| #endif | ||||
| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: de2aeb2e8068f421a9a1febe408f7051 | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
| @@ -0,0 +1,31 @@ | ||||
| // logger for compilation finished hook. | ||||
| // where we need a callback and Debug.Log. | ||||
| // for Unity 2020+ we use ILPostProcessor. | ||||
| #if !UNITY_2020_3_OR_NEWER | ||||
| using Mono.CecilX; | ||||
| using UnityEngine; | ||||
|  | ||||
| namespace Mirror.Weaver | ||||
| { | ||||
|     public class CompilationFinishedLogger : Logger | ||||
|     { | ||||
|         public void Warning(string message) => Warning(message, null); | ||||
|         public void Warning(string message, MemberReference mr) | ||||
|         { | ||||
|             if (mr != null) message = $"{message} (at {mr})"; | ||||
|  | ||||
|             if (CompilationFinishedHook.UnityLogEnabled) Debug.LogWarning(message); | ||||
|             CompilationFinishedHook.OnWeaverWarning?.Invoke(message); | ||||
|         } | ||||
|  | ||||
|         public void Error(string message) => Error(message, null); | ||||
|         public void Error(string message, MemberReference mr) | ||||
|         { | ||||
|             if (mr != null) message = $"{message} (at {mr})"; | ||||
|  | ||||
|             if (CompilationFinishedHook.UnityLogEnabled) Debug.LogError(message); | ||||
|             CompilationFinishedHook.OnWeaverError?.Invoke(message); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| #endif | ||||
| @@ -0,0 +1,3 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 47026732f0fa475c94bd1dd41f1de559 | ||||
| timeCreated: 1629379868 | ||||
							
								
								
									
										44
									
								
								Assets/Mirror/Editor/Weaver/EntryPoint/EnterPlayModeHook.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								Assets/Mirror/Editor/Weaver/EntryPoint/EnterPlayModeHook.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | ||||
| #if !UNITY_2020_3_OR_NEWER | ||||
| // make sure we weaved successfully when entering play mode. | ||||
| using UnityEditor; | ||||
| using UnityEngine; | ||||
|  | ||||
| namespace Mirror | ||||
| { | ||||
|     public class EnterPlayModeSettingsCheck : MonoBehaviour | ||||
|     { | ||||
|         [InitializeOnLoadMethod] | ||||
|         static void OnInitializeOnLoad() | ||||
|         { | ||||
|             // Hook this event to see if we have a good weave every time | ||||
|             // user attempts to enter play mode or tries to do a build | ||||
|             EditorApplication.playModeStateChanged += OnPlayModeStateChanged; | ||||
|         } | ||||
|  | ||||
|         static void OnPlayModeStateChanged(PlayModeStateChange state) | ||||
|         { | ||||
|             // Per Unity docs, this fires "when exiting edit mode before the Editor is in play mode". | ||||
|             // This doesn't fire when closing the editor. | ||||
|             if (state == PlayModeStateChange.ExitingEditMode) | ||||
|             { | ||||
|                 // Check if last weave result was successful | ||||
|                 if (!SessionState.GetBool("MIRROR_WEAVE_SUCCESS", false)) | ||||
|                 { | ||||
|                     // Last weave result was a failure...try to weave again | ||||
|                     // Faults will show in the console that may have been cleared by "Clear on Play" | ||||
|                     SessionState.SetBool("MIRROR_WEAVE_SUCCESS", true); | ||||
|                     Weaver.CompilationFinishedHook.WeaveExistingAssemblies(); | ||||
|  | ||||
|                     // Did that clear things up for us? | ||||
|                     if (!SessionState.GetBool("MIRROR_WEAVE_SUCCESS", false)) | ||||
|                     { | ||||
|                         // Nope, still failed, and console has the issues logged | ||||
|                         Debug.LogError("Can't enter play mode until weaver issues are resolved."); | ||||
|                         EditorApplication.isPlaying = false; | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| #endif | ||||
| @@ -0,0 +1,3 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: b73d0f106ba84aa983baa5142b08a0a9 | ||||
| timeCreated: 1628851346 | ||||
		Reference in New Issue
	
	Block a user
	 DerTyp187
					DerTyp187