public static ConfigAndGestures ImportPrevousVersion()
        {
            var possibleDirs = Directory.GetDirectories(Directory.GetParent(AppSettings.UserDataDirectory).FullName);
            possibleDirs = possibleDirs.Where(s =>
            {
                var dirName = Path.GetFileName(s);
                int num;
                return dirName.Split('.').Length == 4 && int.TryParse(dirName.Replace(".", string.Empty), out num);
            }).ToArray();

            if (possibleDirs.Length < 2) return null;

            Func<string, int[]> splitToInts = s => Path.GetFileName(s).Split('.').Select(i => int.Parse(i)).ToArray();

            Array.Sort(possibleDirs, (a, b) =>
            {
                var aNums = splitToInts(a);
                var bNums = splitToInts(b);

                var compareResult = 0;
                for (var i = 0; i < aNums.Length; i++)
                {
                    if (aNums[i] == bNums[i]) continue;

                    compareResult = aNums[i].CompareTo(bNums[i]);
                    break;
                }

                return compareResult;
            });

            //获得最近的那个版本的数据目录
            var recent = possibleDirs[possibleDirs.Length - 2];

            var gestures = null as JsonGestureIntentStore;
            var config = null as PlistConfig;

            var gesturesFileV1 = recent + @"\gestures.json";
            var gestureFileV2 = recent + @"\gestures.wg";
            var configFile = recent + @"\config.plist";

            try
            {
                if (File.Exists(gesturesFileV1))
                {
                    gestures = new JsonGestureIntentStore(gesturesFileV1, "1");
                }else if (File.Exists(gestureFileV2))
                {
                    gestures = new JsonGestureIntentStore(gestureFileV2, "2");
                }

                if (File.Exists(configFile))
                {
                    config = new PlistConfig(configFile);
                }
            }
            catch (Exception e)
            {

                throw new MigrateException(e.Message, e);
            }


            return new ConfigAndGestures(config, gestures);

        }
        public static ConfigAndGestures ImportWgb(string wgbFilePath)
        {
            if (!File.Exists(wgbFilePath))
            {
                throw new MigrateException("文件不存在:" + wgbFilePath);
            }

            var config = null as PlistConfig;
            var gestures = null as JsonGestureIntentStore;

            var cofnigFileName = Path.GetFileName(AppSettings.ConfigFilePath);
            var gesturesFileName = Path.GetFileName(AppSettings.GesturesFilePath);

            var arcFile = new StreamingArchiveFile(wgbFilePath);
            var files = arcFile.FileIndex.IndexedFileNames.ToArray();

            /*var fileShortNames = (from f in files select Path.GetFileName(f)).ToArray();
            if (!fileShortNames.Contains(cofnigFileName) || !fileShortNames.Contains(gesturesFileName))
                throw new MigrateException("文件内容不正确(未找到需要的部分): " + wgbFilePath);*/

            try
            {
                // iterate the files in the archive:
                foreach (var fileName in files)
                {
                    // write the name of the file
                    Debug.Print("File: " + fileName);

                    // extract the file:
                    var file = arcFile.GetFile(fileName);
                    //file.SaveAs("text.txt");

                    //config file
                    if (Path.GetFileName(fileName) == cofnigFileName)
                    {
                        config = new PlistConfig(file.GetStream(), closeStream: true);

                    }
                    else
                    {
                        var version = "1";
                        if (fileName.EndsWith(".json"))
                        {
                            version = "1";
                        }
                        else if (fileName.EndsWith(".wg"))
                        {
                            version = "2";
                        }
                        gestures = new JsonGestureIntentStore(file.GetStream(), true, version);
                    }

                }
            }
            catch (Exception e)
            {
                if (e is SystemException) throw;
                throw new MigrateException("文件内容错误");
            }
            

            if(config == null || gestures == null) throw new MigrateException("文件内容错误");

            return new ConfigAndGestures(config, gestures);
            

        }
Exemple #3
0
        private static void LoadFailSafeConfigFile()
        {
#if Scafolding
            config = new PlistConfig(AppSettings.ConfigFilePath){FileVersion = AppSettings.ConfigFileVersion};
            intentStore = new JsonGestureIntentStore(AppSettings.GesturesFilePath, AppSettings.GesturesFileVersion);
            return;
#endif

            if (!File.Exists(AppSettings.ConfigFilePath))
            {
                File.Copy(string.Format("{0}/defaults/config.plist", Path.GetDirectoryName(Application.ExecutablePath)), AppSettings.ConfigFilePath);
            }
            if (!File.Exists(AppSettings.GesturesFilePath))
            {
                File.Copy(string.Format("{0}/defaults/gestures.wg", Path.GetDirectoryName(Application.ExecutablePath)), AppSettings.GesturesFilePath);
            }
            
            try
            { //如果文件损坏,则替换。
                config = new PlistConfig(AppSettings.ConfigFilePath);
            }
            catch (Exception)
            {
                Debug.WriteLine("Program.Main: config文件损坏!");
                File.Delete(AppSettings.ConfigFilePath);
                File.Copy(string.Format("{0}/defaults/config.plist", Path.GetDirectoryName(Application.ExecutablePath)), AppSettings.ConfigFilePath);

                config = new PlistConfig(AppSettings.ConfigFilePath);
            }
            
            try
            {
                intentStore = new JsonGestureIntentStore(AppSettings.GesturesFilePath, AppSettings.GesturesFileVersion);

                if (config.FileVersion != AppSettings.ConfigFileVersion ||
                intentStore.FileVersion != AppSettings.GesturesFileVersion)
                {
                    throw new Exception("配置文件版本不正确");
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("加载配置文件出错:"+e);

                File.Delete(AppSettings.GesturesFilePath);
                File.Copy(string.Format("{0}/defaults/gestures.wg", Path.GetDirectoryName(Application.ExecutablePath)), AppSettings.GesturesFilePath);

                intentStore = new JsonGestureIntentStore(AppSettings.GesturesFilePath, AppSettings.GesturesFileVersion);
            }
        }
        public static ConfigAndGestures Combine(ConfigAndGestures old, ConfigAndGestures current, MergeOption mergeOption)
        {
            var config = null as PlistConfig;
            var gestures = null as JsonGestureIntentStore;

            if (MergeOption.Config == (mergeOption & MergeOption.Config))
            {
                config = new PlistConfig();
                //旧的应该覆盖新的
                config.Import(current.Config, old.Config);
            }

            if (MergeOption.Gestures == (mergeOption & MergeOption.Gestures))
            {
                gestures = current.GestureIntentStore.Clone();

                gestures.Import(old.GestureIntentStore);
            }

            return new ConfigAndGestures(config, gestures);
        }
 public ConfigAndGestures(PlistConfig config, JsonGestureIntentStore gestures)
 {
     Config = config;
     GestureIntentStore = gestures;
 }