Пример #1
0
    public void Request(Action <string> callback)
    {
        this.callback = callback;
#if UNITY_EDITOR
        this.callback(null);
#elif UNITY_2018_1_OR_NEWER && !ACTK_UWP_NO_IL2CPP
        CodeHashGenerator.Generate();
#endif
    }
Пример #2
0
        public void StartGeneration()
        {
            // This is a good practice to avoid new requests while generator is busy with previous requests.
            if (CodeHashGenerator.Instance.IsBusy)
            {
                return;
            }

            // Just subscribe to generation event and start generation.
            // Generation runs in separate thread avoiding cpu spikes in main thread.
            // It generates hash only once and cache it for any new requests since compiled code does not change in runtime.
            CodeHashGenerator.HashGenerated += OnHashGenerated;
            CodeHashGenerator.Generate();
        }
        // called by Unity
        public void OnPostprocessBuild(BuildReport report)
        {
            if (!ACTkSettings.Instance.PreGenerateBuildHash || !CodeHashGenerator.IsTargetPlatformCompatible())
            {
                return;
            }

            try
            {
                EditorUtility.DisplayProgressBar("ACTk: Generating code hash", "Preparing...", 0);
                var hashedBuilds = GetHashedBuilds(report);

                if (hashedBuilds == null || hashedBuilds.Count == 0)
                {
                    Debug.Log(ACTkConstants.LogPrefix + "Couldn't pre-generate code hash. " +
                              "Please run your build and generate hash with CodeHashGenerator.");
                    return;
                }

                foreach (var hashedBuild in hashedBuilds)
                {
                    hashedBuild.PrintToConsole();
                }

#pragma warning disable 618
                if (HashesGenerate != null)
                {
                    var obsoleteDictionary = new Dictionary <string, string>();
                    foreach (var hashedBuild in hashedBuilds)
                    {
                        obsoleteDictionary.Add(hashedBuild.BuildPath, hashedBuild.SummaryHash);
                    }
                    HashesGenerate.Invoke(report, obsoleteDictionary);
                }
#pragma warning restore 618

                if (HashesGenerated != null)
                {
                    HashesGenerated.Invoke(report, hashedBuilds.ToArray());
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
            finally
            {
                EditorUtility.ClearProgressBar();
            }
        }
        /// <summary>
        /// Calls selection dialog and calculates hash for the selected build.
        /// </summary>
        /// <param name="selectedBuildPath">Selected build path or null if selection was cancelled.</param>
        /// <returns>Calculated hash or null in case of error / user cancellation.</returns>
        public static string CalculateExternalBuildHash(out string selectedBuildPath)
        {
            var buildPath = EditorUtility.OpenFilePanel("Select Standalone Windows build exe or Android build apk / aab", "", "exe,apk,aab");

            selectedBuildPath = buildPath;
            if (string.IsNullOrEmpty(buildPath))
            {
                return(null);
            }

            var extension = Path.GetExtension(selectedBuildPath);

            if (extension == null)
            {
                return(null);
            }

            extension = extension.ToLower(CultureInfo.InvariantCulture);

            string result = null;
            var    sha1   = new SHA1Managed();

            try
            {
                var il2Cpp = PlayerSettings.GetScriptingBackend(EditorUserBuildSettings.selectedBuildTargetGroup) ==
                             ScriptingImplementation.IL2CPP;

                if (extension == ".apk" || extension == ".aab")
                {
                    result = GetApkHash(buildPath, CodeHashGenerator.GetFileFiltersAndroid(il2Cpp), sha1);
                }
                else
                {
                    var buildFolder = Path.GetDirectoryName(selectedBuildPath);
                    result = StandaloneWindowsWorker.GetBuildHash(buildFolder,
                                                                  CodeHashGenerator.GetFileFiltersStandaloneWindows(il2Cpp), sha1);
                }
            }
            catch (Exception e)
            {
                Debug.LogError(ACTkConstants.LogPrefix + "Error while trying to hash build: " + e);
            }
            finally
            {
                sha1.Clear();
                EditorUtility.ClearProgressBar();
            }

            return(result);
        }
Пример #5
0
        private static void DrawHashSection()
        {
            using (var changed = new EditorGUI.ChangeCheckScope())
            {
                var betaColor = ColorTools.GetPurpleString();
                var fold      = GUITools.DrawFoldHeader("Code Hash Generator <color=#" + betaColor + ">BETA</color>", ACTkEditorPrefsSettings.HashFoldout);
                if (changed.changed)
                {
                    ACTkEditorPrefsSettings.HashFoldout = fold;
                }
            }

            if (!ACTkEditorPrefsSettings.HashFoldout)
            {
                return;
            }

            GUILayout.Space(-3f);

            using (GUITools.Vertical(GUITools.PanelWithBackground))
            {
                var option = ACTkSettings.Instance.PreGenerateBuildHash;

                EditorGUI.BeginChangeCheck();
                option = EditorGUILayout.ToggleLeft(new GUIContent("Generate code hash on build completion", "Generates hash after build is finished, prints it to the console & sends it via CodeHashGeneratorPostprocessor."), option);
                if (EditorGUI.EndChangeCheck())
                {
                    ACTkSettings.Instance.PreGenerateBuildHash = option;
                }

                EditorGUILayout.Space();

                GUILayout.Label("Can differ from runtime hash if you post-process code in resulting build (e.g. obfuscate, compress, etc.).", GUITools.RichLabel);

                GUILayout.Space(5f);
                EditorGUILayout.HelpBox("Always make sure post-build hash equals runtime one if you're using it for later comparison",
                                        MessageType.Info, true);

                if (!CodeHashGenerator.IsTargetPlatformCompatible())
                {
                    EditorGUILayout.HelpBox("Current platform is not supported: Windows or Android required",
                                            MessageType.Warning, true);
                }

                GUILayout.Space(3);
            }
        }
        private static FileFilter[] GetFileFilters()
        {
            var il2Cpp = false;

#if UNITY_EDITOR
            il2Cpp = PlayerSettings.GetScriptingBackend(EditorUserBuildSettings.selectedBuildTargetGroup) == ScriptingImplementation.IL2CPP;
#elif ENABLE_IL2CPP
            il2Cpp = true;
#endif

#if UNITY_ANDROID
            return(CodeHashGenerator.GetFileFiltersAndroid(il2Cpp));
#elif UNITY_STANDALONE_WIN
            return(CodeHashGenerator.GetFileFiltersStandaloneWindows(il2Cpp));
#else
            return(null);
#endif
        }
        // called by Unity
        public void OnPostprocessBuild(BuildReport report)
        {
            if (!ACTkSettings.Instance.PreGenerateBuildHash || !CodeHashGenerator.IsTargetPlatformCompatible())
            {
                return;
            }

            try
            {
                EditorUtility.DisplayProgressBar("ACTk: Generating code hash", "Preparing...", 0);
                var hashes = GetHashes(report);

                if (hashes == null || hashes.Count == 0)
                {
                    Debug.Log(ACTkConstants.LogPrefix + "Couldn't pre-generate code hash. " +
                              "Please run your build and generate hash with CodeHashGenerator.");
                    return;
                }

                foreach (var hash in hashes)
                {
                    Debug.Log(ACTkConstants.LogPrefix + "Pre-generated code hash: " + hash.Value + "\nBuild: " + hash.Key);
                }

                if (HashesGenerate != null)
                {
                    HashesGenerate.Invoke(report, hashes);
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
            finally
            {
                EditorUtility.ClearProgressBar();
            }
        }
Пример #8
0
 // just to make sure it's added to the scene and Instance will be not empty
 public void Init()
 {
     CodeHashGenerator.AddToSceneOrGetExisting();
 }
Пример #9
0
 private void OnCheckHashClick()
 {
     status = "Checking...";
     CodeHashGenerator.Generate();
 }