Exemplo n.º 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))
            {
                logger.InfoFormat("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)
            {
                logger.ErrorFormat("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))
                {
                    logger.ErrorFormat("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)
            {
                logger.Error(e);
                return(false);
            }
        }
Exemplo n.º 2
0
 private static void LoadAllowedMenuItems()
 {
     if (string.IsNullOrEmpty(Instance.AllowedMenuItemsSettingsAssetPath))
     {
         Instance.AllowedMenuItemsSettings = new AllowedMenuItemsSettings();
     }
     else
     {
         Instance.AllowedMenuItemsSettings = AllowedMenuItemsSettings.Load();
     }
 }