예제 #1
0
        private void    CheckTempFile()
        {
            using (var md5 = MD5.Create())
                using (var stream = File.OpenRead(this.tempFilePath))
                {
                    PrefsManager manager = this.prefManagers[this.currentManager];
                    byte[]       newHash = md5.ComputeHash(stream);

                    for (int i = 0; i < newHash.Length; i++)
                    {
                        if (newHash[i] != this.lastHash[i])
                        {
                            this.lastHash       = newHash;
                            this.lastFileChange = DateTime.Now;

                            manager.values[this.editingStringIndex] = File.ReadAllText(this.tempFilePath);

                            this.Repaint();
                            break;
                        }
                    }
                }
        }
예제 #2
0
        private void    OnGUIPreferences(Rect r)
        {
            PrefsManager manager   = this.prefManagers[this.currentManager];
            float        x         = r.x;
            float        width     = r.width;
            bool         hasLoaded = false;

            try
            {
                for (int k = 0; k < this.filteredIndexes.Count; k++)
                {
                    if (r.y + r.height <= this.scrollPosition.y)
                    {
                        r.y += r.height;
                        continue;
                    }

                    int    i     = this.filteredIndexes[k];
                    string key   = manager.keys[i];
                    object value = manager.values[i];

                    if (value == null)
                    {
                        if (hasLoaded == false)
                        {
                            hasLoaded = true;
                            manager.BeginLoadFromRegistrar();
                        }

                        manager.LoadValueFromRegistrar(i);
                        value = manager.values[i];
                    }

                    r.x     = x;
                    r.width = 20F;
                    if (value == System.Type.Missing || (GUI.Button(r, "X", GeneralStyles.ToolbarCloseButton) == true && ((Event.current.modifiers & Constants.ByPassPromptModifier) != 0 || EditorUtility.DisplayDialog(LC.G("NGPrefs_DeletePref"), string.Format(LC.G("NGPrefs_DeletePrefConfirm"), key), LC.G("Yes"), LC.G("No")) == true)))
                    {
                        manager.DeleteKey(i);

                        this.UpdateFilteredIndexes();

                        if (this.editingStringIndex == i)
                        {
                            this.editingStringIndex = -1;
                        }

                        break;
                    }

                    r.x += r.width;

                    this.content.text    = key;
                    this.content.tooltip = key;

                    bool isAltered = ((value is int && (int)value != manager.GetInt(key)) ||
                                      (value is float && (float)value != manager.GetFloat(key)) ||
                                      (value is string && (string)value != manager.GetString(key)));

                    r.width = (isAltered == true) ? width - r.x - 60F : width - r.x;

                    using (BgColorContentRestorer.Get(isAltered, NGPrefsWindow.AlteredPrefBackgroundColor))
                    {
                        if (value is int)
                        {
                            manager.values[i] = EditorGUI.IntField(r, this.content, (int)value);
                        }
                        else if (value is string)
                        {
                            string content = (string)value;

                            if (content.Length <= NGPrefsWindow.MaxStringLength)
                            {
                                manager.values[i] = EditorGUI.TextField(r, this.content, content);
                            }
                            else
                            {
                                EditorGUI.LabelField(r, this.content);

                                r.x     += labelWidth;
                                r.width -= labelWidth;

                                if (this.editingStringIndex != i)
                                {
                                    if (GUI.Button(r, "String has more than " + NGPrefsWindow.MaxStringLength + " chars. Click to edit.") == true)
                                    {
                                        this.editingStringIndex = i;
                                        this.tempFilePath       = Path.Combine(Application.temporaryCachePath, Path.GetRandomFileName() + ".txt");

                                        File.WriteAllText(this.tempFilePath, content);

                                        this.lastFileChange = DateTime.Now;

                                        using (var md5 = MD5.Create())
                                            using (var stream = File.OpenRead(this.tempFilePath))
                                            {
                                                this.lastHash = md5.ComputeHash(stream);
                                            }

                                        EditorUtility.OpenWithDefaultApp(this.tempFilePath);

                                        Utility.RegisterIntervalCallback(this.CheckTempFile, 100);
                                    }
                                }
                                else
                                {
                                    if (GUI.Button(r, "Editing... Last changed at " + this.lastFileChange.ToString("HH:mm:ss") + ". Click to stop.") == true)
                                    {
                                        this.editingStringIndex = -1;
                                        Utility.UnregisterIntervalCallback(this.CheckTempFile);
                                    }
                                }
                            }
                        }
                        else if (value is float)
                        {
                            manager.values[i] = EditorGUI.FloatField(r, this.content, (float)value);
                        }
                    }

                    r.x += r.width;

                    if (isAltered == true)
                    {
                        r.width = 30F;
                        if (GUI.Button(r, this.resetContent, GeneralStyles.ToolbarAltButton) == true)
                        {
                            GUI.FocusControl(null);

                            if (manager.HasKey(key) == false)
                            {
                                try
                                {
                                    manager.LoadPreferences();
                                    this.UpdateFilteredIndexes();
                                }
                                catch (Exception ex)
                                {
                                    this.errorPopup.exception = ex;
                                }
                                break;
                            }
                            else if (value is int)
                            {
                                manager.values[i] = manager.GetInt(key);
                            }
                            else if (value is float)
                            {
                                manager.values[i] = manager.GetFloat(key);
                            }
                            else if (value is string)
                            {
                                manager.values[i] = manager.GetString(key);
                            }
                        }
                        r.x += r.width;

                        if (GUI.Button(r, this.applyContent, GeneralStyles.ToolbarValidButton) == true)
                        {
                            if (value is int)
                            {
                                manager.SetInt(key, (int)value);
                            }
                            else if (value is float)
                            {
                                manager.SetFloat(key, (float)value);
                            }
                            else if (value is string)
                            {
                                manager.SetString(key, (string)value);
                            }
                        }
                    }

                    r.y += r.height;

                    if (r.y - this.scrollPosition.y >= this.bodyRect.height)
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                this.errorPopup.exception = ex;
            }
            finally
            {
                if (hasLoaded == true)
                {
                    manager.EndLoadFromRegistrar();
                }
            }
        }