예제 #1
0
        internal static Settings Create(SettingsFieldInfo settingsFieldInfo, bool reset, bool throwExceptionIfCouldNotLoadFromStorageFile)
        {
            Settings settings = create(settingsFieldInfo, reset, throwExceptionIfCouldNotLoadFromStorageFile);

            settings.__Info = settingsFieldInfo;
            settings.Loaded();
            return(settings);
        }
예제 #2
0
        internal static Settings Create(SettingsFieldInfo settingsFieldInfo, bool reset)
        {
            Settings settings = create(settingsFieldInfo, reset);

            settings.__Info = settingsFieldInfo;
            settings.Loaded();
            settings.loading = false;
            return(settings);
        }
예제 #3
0
 internal void Save(SettingsFieldInfo correctSettingsFieldInfo)//checks that __Info was not replaced and provides an appropriate exception message
 {
     lock (this)
     {
         if (__Info != correctSettingsFieldInfo)
         {
             //it can happen when:
             //- there are several settings fields of the same type and their __Info's were exchanged from the custom code;
             //- Config was reloaded while an old object was preserved;
             //All this will lead to a confusion.
             throw new Exception("Settings field '" + correctSettingsFieldInfo.FullName + "' cannot be saved because its __Info has been altered.");
         }
         save();
     }
 }
예제 #4
0
        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);
        }
        void polling()
        {
            try
            {
                while (parameters.Synchronize)
                {
                    foreach (string ssfn in synchronizedSettingsFieldFullNames)
                    {
                        SettingsFieldInfo sfi      = Config.GetSettingsFieldInfo(ssfn);
                        Settings          settings = sfi.GetObject();
                        if (settings == null)
                        {
                            continue;
                        }
                        pollDownloadSettingsFile(settings);
                        pollUploadSettingsFile(settings);
                    }
                    //foreach (Type sst in synchronizedSettingsTypes)
                    //{
                    //    List<SettingsFieldInfo> sfis = Config.GetSettingsFieldInfos(sst);
                    //    if (sfis.Count < 1)
                    //        throw new Exception("Settings type " + sst.FullName + " was not found.");
                    //    foreach (SettingsFieldInfo sfi in sfis)
                    //    {
                    //        Settings settings = sfi.GetObject();
                    //        if (settings == null)
                    //            continue;
                    //        pollUploadSettingsFile(settings);
                    //        pollDownloadSettingsFile(settings);
                    //    }
                    //}

                    if (synchronizedFolders != null)
                    {
                        foreach (SynchronizedFolder sf in synchronizedFolders)
                        {
                            string synchronizationFolder = downloadFolder + Path.DirectorySeparatorChar + sf.SynchronizationFolderName;
                            if (Directory.Exists(synchronizationFolder))
                            {
                                foreach (string file in Directory.GetFiles(synchronizationFolder))
                                {
                                    if (sf.FileNameFilter.IsMatch(PathRoutines.GetFileName(file)) == false)
                                    {
                                        continue;
                                    }
                                    try
                                    {
                                        DateTime downloadLWT = File.GetLastWriteTime(file);
                                        if (downloadLWT.AddSeconds(100) > DateTime.Now)//it is being written
                                        {
                                            return;
                                        }
                                        string file2 = sf.Folder + Path.DirectorySeparatorChar + PathRoutines.GetFileName(file);
                                        if (File.Exists(file2) && downloadLWT <= File.GetLastWriteTime(file2))
                                        {
                                            return;
                                        }
                                        copy(file, file2);
                                        onNewerFile(file);
                                    }
                                    catch (Exception e)
                                    {
                                        ErrorHandler(e);
                                    }
                                }
                            }
                            foreach (string file in Directory.GetFiles(sf.Folder))
                            {
                                if (sf.FileNameFilter.IsMatch(PathRoutines.GetFileName(file)) == false)
                                {
                                    continue;
                                }
                                try
                                {
                                    DateTime uploadLWT = File.GetLastWriteTime(file);
                                    if (uploadLWT.AddSeconds(10) > DateTime.Now)//it is being written
                                    {
                                        return;
                                    }
                                    string file2 = FileSystemRoutines.CreateDirectory(uploadFolder + Path.DirectorySeparatorChar + sf.SynchronizationFolderName) + Path.DirectorySeparatorChar + PathRoutines.GetFileName(file);
                                    if (File.Exists(file2) && uploadLWT <= File.GetLastWriteTime(file2))
                                    {
                                        return;
                                    }
                                    copy(file, file2);
                                }
                                catch (Exception e)
                                {
                                    ErrorHandler(e);
                                }
                            }
                        }
                    }

                    string appSetupFile = null;
                    foreach (string file in Directory.GetFiles(appSetupFolder))
                    {
                        Match m = appSetupFileFilter?.Match(file);
                        if (m?.Success != true)
                        {
                            continue;
                        }
                        if (!Version.TryParse(m.Groups[1].Value, out Version v))
                        {
                            continue;
                        }
                        if (v > programVersion && v > lastAppVersion)
                        {
                            lastAppVersion = v;
                            appSetupFile   = file;
                        }
                    }
                    if (appSetupFile != null)
                    {
                        AppNewSetupFile = appSetupFile;
                        onNewerAppVersion(appSetupFile);
                        return;
                    }

                    Thread.Sleep(parameters.PollingPeriodMss);
                }
            }
            catch (Exception e)
            {
                ErrorHandler(new Exception("Synchronization thread exited due to exception.", e));
            }
        }