Example #1
0
        /// <summary>
        /// Serializes the <paramref name="settings"/> object and saves it into a configuration file at a default path.
        /// </summary>
        /// <exception cref="NullReferenceException">Thrown when parameter is null.</exception>
        public static bool Save(AllowedMenuItemsSettings settings)
        {
            if (string.IsNullOrEmpty(EditorConfigurator.Instance.AllowedMenuItemsSettingsAssetPath))
            {
                Debug.LogFormat("The property \"AllowedMenuItemsSettingsAssetPath\" of the " +
                                "current editor configuration is not set. Thus, the AllowedMenuItemsSettings cannot be saved.");
                return(false);
            }

            const string assets = "Assets/";
            string       path   = EditorConfigurator.Instance.AllowedMenuItemsSettingsAssetPath;

            if (path.StartsWith(assets) == false)
            {
                Debug.LogErrorFormat("The property \"AllowedMenuItemsSettingsAssetPath\" of the current editor configuration" +
                                     " is invalid. It has to start with \"{0}\". Current value: \"{1}\"", assets, path);
                return(false);
            }

            try
            {
                if (settings == null)
                {
                    throw new NullReferenceException("The allowed menu items settings file cannot be saved "
                                                     + "because the settings are null.");
                }

                string serialized = JsonEditorConfigurationSerializer.Serialize(settings);

                string fullPath      = string.Format("{0}/{1}", Application.dataPath, path.Remove(0, assets.Length));
                string directoryPath = Path.GetDirectoryName(fullPath);

                if (string.IsNullOrEmpty(directoryPath))
                {
                    Debug.LogErrorFormat("No valid directory path found in path \"{0}\". The property \"AllowedMenuItemSettingsAssetPath\"" +
                                         " of the current editor configuration is invalid. Current value: \"{1}\"", fullPath, path);
                    return(false);
                }

                if (Directory.Exists(directoryPath) == false)
                {
                    Directory.CreateDirectory(directoryPath);
                }

                StreamWriter writer = new StreamWriter(fullPath, false);
                writer.Write(serialized);
                writer.Close();

                AssetDatabase.ImportAsset(path);

                return(true);
            }
            catch (Exception e)
            {
                Debug.LogError(e);
                return(false);
            }
        }
Example #2
0
        /// <summary>
        /// Loads and returns the <see cref="AllowedMenuItemsSettings"/> object from the default configuration file location.
        /// If the <see cref="AllowedMenuItemsSettingsAssetPath"/> in the editor configuration is empty or null,
        /// it returns an empty <see cref="AllowedMenuItemsSettings"/> object.
        /// </summary>
        public static AllowedMenuItemsSettings Load()
        {
            string path = EditorConfigurator.Instance.AllowedMenuItemsSettingsAssetPath;

            if (string.IsNullOrEmpty(path))
            {
                Debug.Log("The property \"AllowedMenuItemsSettingsAssetPath\" of the current editor " +
                          "configuration is not set. Therefore, it cannot be loaded. A new \"AllowedMenuItemsSettings\" " +
                          "object with all found conditions and behaviors was returned.");
                return(new AllowedMenuItemsSettings());
            }

            TextAsset settings = (TextAsset)AssetDatabase.LoadAssetAtPath(path, typeof(TextAsset));

            if (settings != null)
            {
                return(JsonEditorConfigurationSerializer.Deserialize(settings.text));
            }

            Debug.LogFormat("There is no serialized \"AllowedMenuItemsSettings\" object saved at \"{0}\". A new " +
                            "\"AllowedMenuItemsSettings\" object with all found conditions and behaviors was returned.", path);
            return(new AllowedMenuItemsSettings());
        }