Example #1
0
        private void OnEnable()
        {
            bool Flagged = EditorPrefs.GetBool("ScriptTrackerFlagged", true);
            bool Check   = EditorPrefs.GetBool("ScriptTrackerCheck", true);

            checkScriptType = Check ? (Flagged ? CST.Flagged : CST.AllScripts) : CST.Disable;
            settings        = ScriptTracker.LoadScriptTrackerSettings();
        }
Example #2
0
        private void OnGUI()
        {
            EditorGUI.BeginChangeCheck();
            checkScriptType = (CST)EditorGUILayout.EnumPopup("Prompt Scripts", checkScriptType);
            if (EditorGUI.EndChangeCheck())
            {
                switch ((int)checkScriptType)
                {
                case 0:
                    EditorPrefs.SetBool("ScriptTrackerFlagged", false);
                    EditorPrefs.SetBool("ScriptTrackerCheck", true);
                    break;

                case 1:
                    EditorPrefs.SetBool("ScriptTrackerFlagged", true);
                    EditorPrefs.SetBool("ScriptTrackerCheck", true);
                    break;

                case 2:
                    EditorPrefs.SetBool("ScriptTrackerFlagged", false);
                    EditorPrefs.SetBool("ScriptTrackerCheck", false);
                    break;
                }
            }

            if (checkScriptType == CST.Flagged)
            {
                ScriptTrackerSettings dummy;
                EditorGUI.BeginChangeCheck();
                dummy = (ScriptTrackerSettings)EditorGUILayout.ObjectField("Settings", settings, typeof(ScriptTrackerSettings), false);
                if (EditorGUI.EndChangeCheck())
                {
                    if (dummy != null)
                    {
                        settings = dummy;
                        PlayerPrefs.SetString(ScriptTracker.PREFS_SETTINGSGUID, AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(settings)));
                    }
                }
            }

            if (GUILayout.Button("Allow Current Project Scripts and DLLs"))
            {
                string[] paths = AssetDatabase.FindAssets("t:Script");
                foreach (var p in paths)
                {
                    EditorPrefs.SetBool(p, true);
                }
                Debug.Log("All Scripts in projects added to whitelist");
            }
        }
Example #3
0
        static void OnPostprocessAllAssets(string[] importedAssets, string[] unused1, string[] unused2, string[] unused3)
        {
            if (importedAssets.Length == 0)
            {
                return;
            }
            ScriptTrackerSettings settings = LoadScriptTrackerSettings();

            check   = EditorPrefs.GetBool("ScriptTrackerCheck", true);
            flagged = EditorPrefs.GetBool("ScriptTrackerFlagged", true);

            if (!check && !checkDLL)
            {
                return;
            }
            try
            {
                AssetDatabase.StartAssetEditing();

                foreach (string path in importedAssets)
                {
                    bool   isScript = Path.GetExtension(path) == ".cs";
                    bool   isDLL    = Path.GetExtension(path) == ".dll";
                    string pathGUID = AssetDatabase.AssetPathToGUID(path);
                    bool   hasKey   = EditorPrefs.HasKey("ScriptTracker-" + pathGUID);

                    if (isScript && check)
                    {
                        bool isLow = false, isNormal = false, isHigh = false;
                        Dictionary <int, string> triggers = new Dictionary <int, string>();

                        string[] scriptLines = File.ReadAllLines(path);

                        string key        = hasKey ? EditorPrefs.GetString("ScriptTracker-" + pathGUID) : "";
                        string scriptHash = StringToHash(string.Join("", scriptLines));
                        bool   isChanged  = key != scriptHash;

                        bool IterateSettings(int lineIndex, List <string> keywords, string flagMessage, ref bool riskBool)
                        {
                            foreach (var k in keywords)
                            {
                                if (string.IsNullOrEmpty(k))
                                {
                                    continue;
                                }
                                if (!KeywordMatch(scriptLines[lineIndex], k))
                                {
                                    continue;
                                }

                                triggers.Add(lineIndex, k);
                                scriptLines[lineIndex] += flagMessage;
                                riskBool = true;
                                return(true);
                            }

                            return(false);
                        }

                        for (int i = 0; i < scriptLines.Length; i++)
                        {
                            if (IterateSettings(i, settings.HighRisk, " //[ST] FLAGGED HIGH RISK", ref isHigh))
                            {
                                break;
                            }
                            if (IterateSettings(i, settings.NormalRisk, " //[ST] FLAGGED NORMAL RISK", ref isNormal))
                            {
                                break;
                            }
                            IterateSettings(i, settings.LowRisk, " //[ST] FLAGGED LOW RISK", ref isLow);
                        }

                        string warnMessage = "Importing Script: " + Path.GetFileNameWithoutExtension(path);
                        if (isHigh)
                        {
                            warnMessage += "\nWARNING: FLAGGED AS HIGH RISK!";
                        }
                        else if (isNormal)
                        {
                            warnMessage += "\nWarning: Flagged as Normal Risk!";
                        }
                        else if (isLow)
                        {
                            warnMessage += "\nWarning: Flagged as Low Risk!";
                        }

                        foreach (var p in triggers)
                        {
                            warnMessage += $"\n Line {p.Key} - {p.Value}";
                        }

                        if ((!hasKey && (isNormal || isLow)) ||
                            (hasKey && isNormal && isChanged) ||
                            isHigh ||
                            !flagged)
                        {
                            switch (EditorUtility.DisplayDialogComplex("Importing Script", warnMessage, "Allow", "Delete", "Revise"))
                            {
                            case 0:
                                EditorPrefs.SetString("ScriptTracker-" + pathGUID, scriptHash);
                                Debug.Log(Path.GetFileNameWithoutExtension(path) + " added to whitelist.");
                                break;

                            case 1:
                                File.Delete(path);
                                File.Delete(path + ".meta");
                                break;

                            case 2:
                                string newFilePath = path.Substring(0, path.Length - 2) + "txt";
                                File.Move(path, newFilePath);
                                File.WriteAllLines(newFilePath, scriptLines);
                                File.Delete(path + ".meta");
                                break;
                            }
                        }
                    }
                    else
                    {
                        if (isDLL)
                        {
                            if ((!hasKey && settings.promptDLL) || (settings.promptDLL && settings.alwaysDLL) || !flagged)
                            {
                                if (EditorUtility.DisplayDialog("Importing DLL", "Importing DLL: " + Path.GetFileNameWithoutExtension(path) + warnDLL, "Allow", "Delete"))
                                {
                                    EditorPrefs.SetString("ScriptTracker-" + AssetDatabase.AssetPathToGUID(path), "Set");
                                }
                                else
                                {
                                    File.Delete(path);
                                    File.Delete(path + ".meta");
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                AssetDatabase.StopAssetEditing();
            }

            PostPackageImport();
            AssetDatabase.Refresh();
        }