Exemple #1
0
        /// <summary>
        /// Merges two QuizProgDataRoots into one, by adding all quiz progress datas into one. If duplicates exist, the one with best progress will be selected.
        /// If same, progressFile1 will have priority
        /// </summary>
        /// <param name="progressFile1">The path to the first quiz progress data file. This one has priority over progressFile2</param>
        /// <param name="progressFile2">The path to the second quiz progress data file.</param>
        /// <param name="savePath">The path where the merged file should be saved</param>
        /// <returns>True if the merge was successful, otherwise false</returns>
        public static bool Merge(string progressFile1, string progressFile2, string savePath)
        {
            BackupHelper.BackupFile(progressFile1, Path.Combine(Path.GetDirectoryName(progressFile1), "SteelQuiz Progress Backups"), false);
            BackupHelper.BackupFile(progressFile2, Path.Combine(Path.GetDirectoryName(progressFile2), "SteelQuiz Progress Backups"), false);
            if (File.Exists(savePath) && savePath != progressFile1 && savePath != progressFile2)
            {
                BackupHelper.BackupFile(savePath, Path.Combine(Path.GetDirectoryName(savePath), "SteelQuiz Progress Backups"));
            }

            QuizProgressDataRoot prog1;
            QuizProgressDataRoot prog2;

            try
            {
                prog1 = JsonConvert.DeserializeObject <QuizProgressDataRoot>(AtomicIO.AtomicRead(progressFile1));

                prog2 = JsonConvert.DeserializeObject <QuizProgressDataRoot>(AtomicIO.AtomicRead(progressFile2));
            }
            catch (AtomicException ex)
            {
                MessageBox.Show("Could not load progress data files:\r\n\r\n" + ex.ToString(), "SteelQuiz", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }

            QuizProgressDataRoot merged = Merge(prog1, prog2);

#if DEBUG
            AtomicIO.AtomicWrite(savePath, JsonConvert.SerializeObject(merged, Formatting.Indented));
#else
            AtomicIO.AtomicWrite(savePath, JsonConvert.SerializeObject(merged));
#endif

            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Saves the quiz progress data contained in the specified quiz.
        /// </summary>
        /// <param name="quiz">The Quiz object containing the progress data</param>
        public static void SaveQuizProgress(Quiz quiz)
        {
            string dataRaw  = AtomicIO.AtomicRead(ConfigManager.Config.StorageConfig.QuizProgressFile);
            var    dataRoot = JsonConvert.DeserializeObject <QuizProgressDataRoot>(dataRaw);

            if (dataRoot.FileFormatVersion == null || new Version(dataRoot.FileFormatVersion).CompareTo(MetaData.GetLatestQuizVersion()) < 0)
            {
                // Existing quiz file has an older file format - backup before upgrade
                BackupProgress();
            }

            dataRoot.FileFormatVersion = MetaData.QUIZ_FILE_FORMAT_VERSION;

            var data = dataRoot.QuizProgressData.Where(x => x.QuizGUID == quiz.GUID).FirstOrDefault();

            if (data != null)
            {
                dataRoot.QuizProgressData[dataRoot.QuizProgressData.IndexOf(data)] = quiz.ProgressData;
            }
            else
            {
                dataRoot.QuizProgressData.Add(quiz.ProgressData);
            }

#if DEBUG
            dataRaw = JsonConvert.SerializeObject(dataRoot, Formatting.Indented);
#else
            dataRaw = JsonConvert.SerializeObject(dataRoot);
#endif
            AtomicIO.AtomicWrite(ConfigManager.Config.StorageConfig.QuizProgressFile, dataRaw);
        }
Exemple #3
0
        /// <summary>
        /// Saves QuizAccessTimes and QuizIdentities to the progress data file.
        /// </summary>
        public static void SaveQuizAccessData()
        {
            Directory.CreateDirectory(APP_CFG_DIR);

            QuizProgressDataRoot dataRoot;
            string dataRaw;

            if (File.Exists(ConfigManager.Config.StorageConfig.QuizProgressFile))
            {
                dataRaw  = AtomicIO.AtomicRead(ConfigManager.Config.StorageConfig.QuizProgressFile);
                dataRoot = JsonConvert.DeserializeObject <QuizProgressDataRoot>(dataRaw);

                if (dataRoot.FileFormatVersion == null || new Version(dataRoot.FileFormatVersion).CompareTo(MetaData.GetLatestQuizVersion()) < 0)
                {
                    // Existing quiz file has an older file format - backup before upgrade
                    BackupProgress();
                }

                dataRoot.FileFormatVersion = MetaData.QUIZ_FILE_FORMAT_VERSION;
            }
            else
            {
                dataRoot = new QuizProgressDataRoot(MetaData.QUIZ_FILE_FORMAT_VERSION);
            }

            dataRoot.QuizAccessTimes = QuizAccessTimes;
            dataRoot.QuizIdentities  = QuizIdentities;

#if DEBUG
            dataRaw = JsonConvert.SerializeObject(dataRoot, Formatting.Indented);
#else
            dataRaw = JsonConvert.SerializeObject(dataRoot);
#endif
            AtomicIO.AtomicWrite(ConfigManager.Config.StorageConfig.QuizProgressFile, dataRaw);
        }
Exemple #4
0
 public static void SaveConfig()
 {
     Directory.CreateDirectory(QuizCore.APP_CFG_DIR);
     AtomicIO.AtomicWrite(CONFIG_FILE, JsonConvert.SerializeObject(Config, Formatting.Indented));
 }
Exemple #5
0
        public static void SaveQuiz(Quiz quiz, string path)
        {
            string quizRaw = JsonConvert.SerializeObject(quiz, Formatting.Indented);

            AtomicIO.AtomicWrite(path, quizRaw);
        }