Exemplo n.º 1
0
        private void DeSerialize_Click(object sender, RoutedEventArgs e)
        {
            string    fileName      = "";
            string    fileExtension = "";
            AllModels newObject     = new AllModels(null, null);


            OpenFileDialog openFileDialog = new OpenFileDialog();

            if (openFileDialog.ShowDialog() == true)
            {
                fileName = openFileDialog.FileName;
            }
            fileExtension = System.IO.Path.GetExtension(fileName);

            FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite);

            foreach (var plugin in Plugins)
            {
                if (fileExtension.Substring(1, fileExtension.Length - 1) == plugin.Key.ToLower())
                {
                    file = plugin.Value.DeArchive(file, ref fileExtension);
                }
            }


            foreach (ISerializers serializer in Serializers)
            {
                string serializerExtension = serializer.Format.Substring(serializer.Format.LastIndexOf('.'), serializer.Format.Length - serializer.Format.LastIndexOf('.'));

                if (serializerExtension == fileExtension)
                {
                    SelectedSerializer = serializer;
                    newObject          = SelectedSerializer.DeSeriazlixeObject(file) as AllModels;
                    break;
                }
            }

            try
            {
                var modulesCount  = newObject.MyModules.Count;
                var vehiclesCount = newObject.MyVehicles.Count;
                for (int i = 0; i < modulesCount; i++)
                {
                    appViewModel.ModulesTab.Modules.Add(newObject.MyModules[i]);
                }
                for (int i = 0; i < vehiclesCount; i++)
                {
                    appViewModel.VehiclesTab.Vehicles.Add(newObject.MyVehicles[i]);
                }
            }
            catch
            {
            }

            file.Close();
            if (file.Name != fileName)
            {
                File.Delete(file.Name);
            }
        }