static Settings create(SettingsMemberInfo settingsFieldInfo, bool reset, bool throwExceptionIfCouldNotLoadFromStorageFile) { if (!reset && File.Exists(settingsFieldInfo.File)) { try { return((Settings)Cliver.Serialization.Json.Load(settingsFieldInfo.Type, settingsFieldInfo.File, true, true)); } catch (Exception e) { if (throwExceptionIfCouldNotLoadFromStorageFile) { throw new Exception("Error while loading settings " + settingsFieldInfo.FullName + " from file " + settingsFieldInfo.File, e); } } } if (File.Exists(settingsFieldInfo.InitFile)) { FileSystemRoutines.CopyFile(settingsFieldInfo.InitFile, settingsFieldInfo.File, true); try { return((Settings)Cliver.Serialization.Json.Load(settingsFieldInfo.Type, settingsFieldInfo.InitFile, true, true)); } catch (Exception e) { throw new Exception("Error while loading settings " + settingsFieldInfo.FullName + " from initial file " + settingsFieldInfo.InitFile, e); } } return((Settings)Activator.CreateInstance(settingsFieldInfo.Type)); }
/// <summary> /// Can be called from code when ordered load is required due to dependencies. /// </summary> static public void ReloadField(string fullName) { List <Assembly> assemblies = new List <Assembly>(); assemblies.Add(Assembly.GetEntryAssembly()); foreach (AssemblyName assemblyNames in Assembly.GetEntryAssembly().GetReferencedAssemblies().Where(assemblyNames => assemblyNameRegexPattern != null ? Regex.IsMatch(assemblyNames.Name, assemblyNameRegexPattern) : true)) { assemblies.Add(Assembly.Load(assemblyNames)); } foreach (Assembly assembly in assemblies) { Type[] types = assembly.GetTypes(); foreach (Type settingsType in types.Where(t => !t.IsAbstract && t.IsSubclassOf(typeof(Settings)))) { foreach (Type type in types) { FieldInfo settingsTypeFieldInfo = type.GetFields(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public).Where(a => a.FieldType == settingsType && (a.DeclaringType.FullName + "." + a.Name) == fullName).FirstOrDefault(); if (settingsTypeFieldInfo != null) { Serializable serializable; string fileName = fullName + "." + FILE_EXTENSION; string file = (settingsTypeFieldInfo.FieldType.BaseType == typeof(UserSettings) ? UserSettings.StorageDir : (settingsTypeFieldInfo.FieldType.BaseType == typeof(AppSettings) ? AppSettings.StorageDir : UnknownTypeStorageDir)) + System.IO.Path.DirectorySeparatorChar + fileName; try { serializable = Serializable.Load(settingsTypeFieldInfo.FieldType, file); } catch //(Exception e) { //if (!Message.YesNo("Error while loading config file " + file + "\r\n\r\n" + e.Message + "\r\n\r\nWould you like to proceed with restoring the initial config?", null, Message.Icons.Error)) // Environment.Exit(0); //if (!ignore_load_error && !Directory.Exists(StorageDir))//it is newly installed and so files are not expected to be there // ignore_load_error = true; //if (!ignore_load_error) // LogMessage.Error2(e); string initFile = Log.AppDir + System.IO.Path.DirectorySeparatorChar + fileName; if (File.Exists(initFile)) { FileSystemRoutines.CopyFile(initFile, file, true); serializable = Serializable.LoadOrCreate(settingsTypeFieldInfo.FieldType, file); } else { serializable = Serializable.Create(settingsTypeFieldInfo.FieldType, file); } } settingsTypeFieldInfo.SetValue(null, serializable); return; } } } } throw new Exception("Field '" + fullName + "' was not found."); }
static Settings create(SettingsFieldInfo settingsFieldInfo, bool reset) { if (!reset && File.Exists(settingsFieldInfo.File)) { return(loadFromFile(settingsFieldInfo)); } if (File.Exists(settingsFieldInfo.InitFile)) { FileSystemRoutines.CopyFile(settingsFieldInfo.InitFile, settingsFieldInfo.File, true); return(loadFromFile(settingsFieldInfo)); } Settings settings = (Settings)Activator.CreateInstance(settingsFieldInfo.Type); settings.__TypeVersion = settingsFieldInfo.TypeVersion; return(settings); }
static void copy(string file, string file2) { for (int i = 0; ; i++) { try { Log.Inform("Copying " + file + " to " + file2); FileSystemRoutines.CopyFile(file, file2, true); return; } catch (IOException ex)//no access while locked by sycnhronizing app { if (i >= 100) { throw new Exception("Could not copy file '" + file + "' to '" + file2 + "'", ex); } Thread.Sleep(1000); } } }
static void get(bool reset) { lock (objectFullNames2serializable) { objectFullNames2serializable.Clear(); List <Assembly> assemblies = new List <Assembly>(); assemblies.Add(Assembly.GetEntryAssembly()); foreach (AssemblyName assemblyNames in Assembly.GetEntryAssembly().GetReferencedAssemblies().Where(assemblyNames => assemblyNameRegexPattern != null ? Regex.IsMatch(assemblyNames.Name, assemblyNameRegexPattern) : true)) { assemblies.Add(Assembly.Load(assemblyNames)); } HashSet <FieldInfo> settingsTypeFieldInfos = new HashSet <FieldInfo>(); foreach (Assembly assembly in assemblies) { Type[] types = assembly.GetTypes(); foreach (Type settingsType in types.Where(t => !t.IsAbstract && t.IsSubclassOf(typeof(Settings)))) { foreach (Type type in types) { foreach (FieldInfo settingsTypeFieldInfo in type.GetFields(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public).Where(a => a.FieldType.IsAssignableFrom(settingsType))) { settingsTypeFieldInfos.Add(settingsTypeFieldInfo); } } } } foreach (FieldInfo settingsTypeFieldInfo in settingsTypeFieldInfos) { string fullName = settingsTypeFieldInfo.DeclaringType.FullName + "." + settingsTypeFieldInfo.Name; if (null == settingsTypeFieldInfo.GetCustomAttributes <Settings.Obligatory>(false).FirstOrDefault() && (obligatoryObjectNamesRegex == null || !obligatoryObjectNamesRegex.IsMatch(fullName))) { continue; } Serializable serializable; string fileName = fullName + "." + FILE_EXTENSION; string file = (settingsTypeFieldInfo.FieldType.BaseType == typeof(UserSettings) ? UserSettings.StorageDir : (settingsTypeFieldInfo.FieldType.BaseType == typeof(AppSettings) ? AppSettings.StorageDir : UnknownTypeStorageDir)) + System.IO.Path.DirectorySeparatorChar + fileName; if (reset) { string initFile = Log.AppDir + System.IO.Path.DirectorySeparatorChar + fileName; if (File.Exists(initFile)) { FileSystemRoutines.CopyFile(initFile, file, true); serializable = Serializable.LoadOrCreate(settingsTypeFieldInfo.FieldType, file); } else { serializable = Serializable.Create(settingsTypeFieldInfo.FieldType, file); } } else { try { serializable = Serializable.Load(settingsTypeFieldInfo.FieldType, file); } catch //(Exception e) { //if (!Message.YesNo("Error while loading config file " + file + "\r\n\r\n" + e.Message + "\r\n\r\nWould you like to proceed with restoring the initial config?", null, Message.Icons.Error)) // Environment.Exit(0); //if (!ignore_load_error && !Directory.Exists(StorageDir))//it is newly installed and so files are not expected to be there // ignore_load_error = true; //if (!ignore_load_error) // LogMessage.Error2(e); string initFile = Log.AppDir + System.IO.Path.DirectorySeparatorChar + fileName; if (File.Exists(initFile)) { FileSystemRoutines.CopyFile(initFile, file, true); serializable = Serializable.LoadOrCreate(settingsTypeFieldInfo.FieldType, file); } else { serializable = Serializable.Create(settingsTypeFieldInfo.FieldType, file); } } } settingsTypeFieldInfo.SetValue(null, serializable); objectFullNames2serializable[fullName] = serializable; } } }
static void get(bool reset) { lock (objectFullNames2serializable) { objectFullNames2serializable.Clear(); List <Assembly> sas = new List <Assembly>(); sas.Add(Assembly.GetEntryAssembly()); foreach (AssemblyName an in Assembly.GetEntryAssembly().GetReferencedAssemblies().Where(an => assemblyNameRegexPattern != null ? Regex.IsMatch(an.Name, assemblyNameRegexPattern) : true)) { sas.Add(Assembly.Load(an)); } //bool ignore_load_error = false; foreach (Assembly sa in sas) { Type[] ets = sa.GetTypes(); foreach (Type st in ets.Where(t => t.IsSubclassOf(typeof(Settings)))) { List <FieldInfo> fis = new List <FieldInfo>(); foreach (Type et in ets) { fis.AddRange(et.GetFields(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public).Where(a => st.IsAssignableFrom(a.FieldType))); } if (fis.Count < 1) { // throw new Exception("No field of type '" + st.FullName + "' was found."); continue; } //if (fis.Count > 1) // throw new Exception("More then 1 field of type '" + st.FullName + "' was found."); foreach (FieldInfo fi in fis) { string fullName = fi.DeclaringType.FullName + "." + fi.Name; if (null == fi.GetCustomAttributes <Settings.Obligatory>(false).FirstOrDefault() && (obligatoryObjectNamesRegex == null || !obligatoryObjectNamesRegex.IsMatch(fullName))) { continue; } Serializable t; string file = (fi.FieldType.BaseType == typeof(UserSettings) ? UserSettings.StorageDir : (fi.FieldType.BaseType == typeof(AppSettings) ? AppSettings.StorageDir : StorageDir)) + System.IO.Path.DirectorySeparatorChar + fullName + "." + FILE_EXTENSION; if (reset) { string initFile = Log.AppDir + System.IO.Path.DirectorySeparatorChar + fullName + "." + FILE_EXTENSION; if (File.Exists(initFile)) { FileSystemRoutines.CopyFile(initFile, file, true); t = Serializable.LoadOrCreate(fi.FieldType, file); } else { t = Serializable.Create(fi.FieldType, file); } } else { try { t = Serializable.Load(fi.FieldType, file); } catch (Exception e) { //if (!Message.YesNo("Error while loading config file " + file + "\r\n\r\n" + e.Message + "\r\n\r\nWould you like to proceed with restoring the initial config?", null, Message.Icons.Error)) // Environment.Exit(0); //if (!ignore_load_error && !Directory.Exists(StorageDir))//it is newly installed and so files are not expected to be there // ignore_load_error = true; //if (!ignore_load_error) // Log.Message.Error2(e); string initFile = Log.AppDir + System.IO.Path.DirectorySeparatorChar + fullName + "." + FILE_EXTENSION; if (File.Exists(initFile)) { FileSystemRoutines.CopyFile(initFile, file, true); t = Serializable.LoadOrCreate(fi.FieldType, file); } else { t = Serializable.Create(fi.FieldType, file); } } } fi.SetValue(null, t); objectFullNames2serializable[fullName] = t; } } } } }