4 using System.Collections;
5 using System.Collections.Generic;
9 using SiliconStudio.Core.Diagnostics;
10 using SiliconStudio.Core.IO;
11 using SiliconStudio.Core.Yaml;
13 namespace SiliconStudio.Presentation.Settings
23 private static readonly Dictionary<UFile, SettingsKey> SettingsKeys =
new Dictionary<UFile, SettingsKey>();
30 private static readonly List<SettingsProfile> ProfileList =
new List<SettingsProfile>();
36 ProfileList.Add(DefaultProfile);
37 currentProfile = DefaultProfile;
49 public static SettingsProfile CurrentProfile {
get {
return currentProfile; } set { ChangeCurrentProfile(currentProfile, value); } }
68 return (includeNonEditable ? SettingsKeys.Values : SettingsKeys.Values.Where(x => x.IsEditable)).ToList();
80 ProfileList.Add(profile);
82 CurrentProfile = profile;
96 if (filePath == null)
throw new ArgumentNullException(
"filePath");
98 if (!
File.Exists(filePath))
100 Logger.Error(
"Settings file [{0}] was not found", filePath);
108 using (var stream =
new FileStream(filePath,
FileMode.Open, FileAccess.Read, FileShare.Read))
112 profile =
new SettingsProfile(parent ?? DefaultProfile) { FilePath = filePath };
114 foreach (var settings
in settingsFile.Settings)
117 var value = settings.Value;
118 if (SettingsKeys.TryGetValue(settings.Key, out key))
120 value = key.ConvertValue(value);
122 profile.SetValue(settings.Key, value);
127 Logger.Error(
"Error while loading settings file [{0}]: {1}", e, filePath, Extensions.StringExtensions.FormatExceptionForReport(e));
131 ProfileList.Add(profile);
134 CurrentProfile = profile;
137 var handler = SettingsFileLoaded;
151 var filePath = profile.FilePath;
152 if (filePath == null)
throw new ArgumentException(
"profile");
153 if (!
File.Exists(filePath))
155 Logger.Error(
"Settings file [{0}] was not found", filePath);
156 throw new ArgumentException(
"profile");
162 using (var stream =
new FileStream(filePath,
FileMode.Open, FileAccess.Read, FileShare.Read))
167 foreach (var settings
in settingsFile.Settings)
170 var value = settings.Value;
171 if (SettingsKeys.TryGetValue(settings.Key, out key))
173 value = key.ConvertValue(value);
175 profile.SetValue(settings.Key, value);
180 Logger.Error(
"Error while loading settings file [{0}]: {1}", e, filePath, Extensions.StringExtensions.FormatExceptionForReport(e));
183 var handler = SettingsFileLoaded;
196 if (profile == DefaultProfile)
197 throw new ArgumentException(
"The default profile cannot be unloaded");
198 if (profile == CurrentProfile)
199 throw new InvalidOperationException(
"Unable to unload the current profile.");
200 ProfileList.Remove(profile);
211 if (profile == null)
throw new ArgumentNullException(
"profile");
214 profile.Saving =
true;
215 Directory.CreateDirectory(filePath.GetFullDirectory());
218 foreach (var entry
in profile.Settings.Values)
220 settingsFile.Settings.Add(entry.Name, entry.GetSerializableValue());
223 using (var stream =
new FileStream(filePath,
FileMode.Create, FileAccess.Write, FileShare.Write))
225 YamlSerializer.Serialize(stream, settingsFile);
230 Logger.Error(
"Error while saving settings file [{0}]: {1}", e, filePath, Extensions.StringExtensions.FormatExceptionForReport(e));
235 profile.Saving =
false;
248 SettingsKeys.TryGetValue(name, out key);
257 CurrentProfile = DefaultProfile;
258 CurrentProfile.ValidateSettingsChanges();
260 DefaultProfile.Settings.Clear();
261 SettingsKeys.Clear();
264 internal static void RegisterSettingsKey(
UFile name,
object defaultValue,
SettingsKey settingsKey)
266 SettingsKeys.Add(name, settingsKey);
267 var entry = SettingsEntry.CreateFromValue(DefaultProfile, name, defaultValue);
268 DefaultProfile.RegisterEntry(entry);
270 foreach (var profile
in Profiles.Where(x => x != DefaultProfile))
272 if (profile.Settings.TryGetValue(name, out entry))
274 var convertedValue = settingsKey.ConvertValue(entry.Value);
275 entry = SettingsEntry.CreateFromValue(profile, name, convertedValue);
276 profile.Settings[name] = entry;
281 private static void ChangeCurrentProfile(SettingsProfile oldProfile, SettingsProfile newProfile)
283 if (oldProfile == null)
throw new ArgumentNullException(
"oldProfile");
284 if (newProfile == null)
throw new ArgumentNullException(
"newProfile");
285 currentProfile = newProfile;
287 foreach (var key
in SettingsKeys)
290 oldProfile.GetValue(key.Key, out oldValue,
true,
false);
292 newProfile.GetValue(key.Key, out newValue,
true,
false);
293 var oldList = oldValue as IList;
294 var newList = newValue as IList;
297 if (oldList != null && newList != null)
299 isDifferent = oldList.Count != newList.Count;
300 for (
int i = 0; i < oldList.Count && !isDifferent; ++i)
302 if (!Equals(oldList[i], newList[i]))
308 isDifferent = !Equals(oldValue, newValue);
312 newProfile.NotifyEntryChanged(key.Key);
317 newProfile.ValidateSettingsChanges();
static void ClearSettings()
Clears the current settings, including registered SettingsKey and SettingsProfile instances...
static SettingsProfile CreateSettingsProfile(bool setAsCurrent, SettingsProfile parent=null)
Creates a new settings profile.
SiliconStudio.Core.Diagnostics.LoggerResult LoggerResult
A logger that stores messages locally useful for internal log scenarios.
This class represents a collection of values for all registered SettingsKey. It may also contains val...
System.IO.FileMode FileMode
static SettingsKey GetSettingsKey(UFile name)
Gets the settings key that matches the given name.
This class represents property to store in the settings that is identified by a key.
static EventHandler< SettingsFileLoadedEventArgs > SettingsFileLoaded
Raised when a settings file has been loaded.
static List< SettingsKey > GetAllSettingsKeys(bool includeNonEditable)
Gets a list of all registered SettingsKey instances.
Base implementation for ILogger.
static bool SaveSettingsProfile(SettingsProfile profile, UFile filePath)
Saves the given settings profile to a file at the given path.
static void ReloadSettingsProfile(SettingsProfile profile)
Reloads a profile from its file, updating the value that have changed.
This class represents a set of settings that can be stored in a file. This class is public for serial...
Default Yaml serializer used to serialize assets by default.
Arguments of the SettingsService.SettingsFileLoaded event.
static void UnloadSettingsProfile(SettingsProfile profile)
Unloads a profile that was previously loaded.
A static class that manages settings loading and saving for an application.
static object Deserialize(Stream stream)
Deserializes an object from the specified stream (expecting a YAML string).
static SettingsProfile LoadSettingsProfile(UFile filePath, bool setAsCurrent, SettingsProfile parent=null)
Loads a settings profile from the given file.
Defines a normalized file path. See UPath for details. This class cannot be inherited.