static void OnPlayModeStateChanged(PlayModeStateChange stateChange)
 {
     if (stateChange == PlayModeStateChange.ExitingPlayMode)
     {
         patcher = null;
         loadedAssemblies.Clear();
     }
 }
        public static void HotPatch(string assemblyName)
        {
            if (!EditorApplication.isPlaying)
            {
                return;
            }

            try {
                if (patcher == null)
                {
                    patcher = new ILDynaRec.HotPatcher();
                    LoadAllAssemblies();
                }

                var checkAssemblies = loadedAssemblies.ToArray();
                if (assemblyName != null)
                {
                    checkAssemblies = checkAssemblies.Where((name) => name == assemblyName).ToArray();
                }

                int i = 0;
                foreach (var assembly in checkAssemblies)
                {
                    if (EditorUtility.DisplayCancelableProgressBar("Hot patching", $"Compiling {assembly}", (float)(i++) / checkAssemblies.Length))
                    {
                        throw new UnityException("Hotpatch cancelled");
                    }

                    var compiler = new UnityCompiler {
                        assemblyModifiedTime = assemblyTimestamps[assembly],
                    };
                    var outputName = Path.GetFileNameWithoutExtension(assembly) + "--hotpatch.dll";
                    if (!compiler.InvokeCompiler(assembly, outputName))
                    {
                        UnityCompiler.Trace($"Did not compile compile {assembly}.");
                        continue;
                    }

                    UnityCompiler.Trace($"Compiled assembly {assembly} as {compiler.OutputAssemblyPath}, running hot patcher.");
                    patcher.HotPatch(compiler.OutputAssemblyPath);
                    assemblyTimestamps[assembly] = DateTime.Now;
                }
            }
            finally {
                EditorUtility.ClearProgressBar();
            }
        }