//saves a game
        public static void SaveGame(string name)
        {
            string fileName = AppSettings.getCommonApplicationDataPath() + "\\saves\\" + name + ".sav";

            Stopwatch sw = new Stopwatch();
            sw.Start();

            SaveObject so = new SaveObject();
            so.airportsList = new List<Airport>();
            so.airlinesList = new List<Airline>();
            so.airlinersList = new List<Airliner>();
            so.calendaritemsList = new List<CalendarItem>();
            so.configurationList = new List<Configuration>();
            so.eventsList = new List<RandomEvent>();

            so.airlinesList.AddRange(Airlines.GetAllAirlines());
            so.airportsList.AddRange(Airports.GetAllAirports());
            so.airlinersList.AddRange(Airliners.GetAllAirliners());
            so.calendaritemsList.AddRange(CalendarItems.GetCalendarItems());
            so.configurationList.AddRange(Configurations.GetConfigurations());
            so.eventsList.AddRange(RandomEvents.GetEvents());

            so.instance = GameObject.GetInstance();

            var settings = new XmlWriterSettings
            {
                Encoding = Encoding.UTF8,
                Indent = true,
                NewLineChars = Environment.NewLine

            };
             DataContractSerializer serializer = new DataContractSerializer(typeof(SaveObject), null,
                      Int32.MaxValue,
                      false,
                      true,
                      null);
             using (Stream stream = new FileStream(fileName, FileMode.Create))
             {
             using (GZipStream compress = new GZipStream(stream, CompressionMode.Compress))
             {
             serializer.WriteObject(compress, so);

             }
             }
            /*
            using (var buffer = new FileStream(fileName, FileMode.Create))
            {
                var serializer = new DataContractSerializer(typeof(SaveObject), null,
                      Int32.MaxValue,
                      false,
                      true,
                      null); //new XmlSerializer(typeof(SaveObject));

                Stream stream = new GZipStream(buffer, CompressionMode.Compress);

                using (var writer = XmlWriter.Create(stream, settings))
                {
                    serializer.WriteObject(writer, so);

                }
                stream.Close();
            }

             * */
            /*
            using (var file = File.Create("c:\\bbm\\person.bin"))
            {
                Serializer.Serialize<SaveObject>(file, so);
            }*/
            sw.Stop();
        }
        //saves a game
        public static void SaveGame(string name)
        {
            //Pause the game so we can save without the clock running :)
            GameObjectWorker.GetInstance().pause();

            string fileName = AppSettings.getCommonApplicationDataPath() + "\\saves\\" + name + ".sav";

            Stopwatch sw = new Stopwatch();

            sw.Start();

            SaveObject so = new SaveObject();

            Parallel.Invoke(() =>
            {
                so.airportsList = new List <Airport>();
                so.airportsList.AddRange(Airports.GetAllAirports());
            }, () =>
            {
                so.airlinesList = new List <Airline>();
                so.airlinesList.AddRange(Airlines.GetAllAirlines());
            }, () =>
            {
                so.airlinersList = new List <Airliner>();
                so.airlinersList.AddRange(Airliners.GetAllAirliners());
            }, () =>
            {
                so.calendaritemsList = new List <CalendarItem>();
                so.calendaritemsList.AddRange(CalendarItems.GetCalendarItems());
            }, () =>
            {
                so.configurationList = new List <Configuration>();
                so.configurationList.AddRange(Configurations.GetConfigurations());
            }, () =>
            {
                so.eventsList = new List <RandomEvent>();
                so.eventsList.AddRange(RandomEvents.GetEvents());
            }, () =>
            {
                so.allianceList = new List <Alliance>();
                so.allianceList.AddRange(Alliances.GetAlliances());
            }, () =>
            {
                so.Airportfacilitieslist = new List <AirportFacility>();
                so.Airportfacilitieslist.AddRange(AirportFacilities.GetFacilities());
            }, () =>
            {
                so.instance = GameObject.GetInstance();
            });


            DataContractSerializer serializer = new DataContractSerializer(typeof(SaveObject), null,
                                                                           Int32.MaxValue,
                                                                           false,
                                                                           true,
                                                                           null);

            using (Stream stream = new FileStream(fileName, FileMode.Create))
            {
                using (DeflateStream compress = new DeflateStream(stream, CompressionMode.Compress))
                {
                    serializer.WriteObject(compress, so);
                }
            }

            sw.Stop();
        }
Exemplo n.º 3
0
        //loads a game
        public static void LoadGame(string file)
        {
            string fileName = AppSettings.getCommonApplicationDataPath() + "\\saves\\" + file + ".sav";

            SaveObject deserializedSaveObject = FileSerializer.Deserialize <SaveObject>(fileName);

            /*
             *
             * DataContractSerializer serializer = new DataContractSerializer(typeof(SaveObject));
             * SaveObject deserializedSaveObject;
             * string loading;
             * int version;
             *
             * using (FileStream stream = new FileStream(fileName, FileMode.Open))
             * {
             * using (DeflateStream decompress = new DeflateStream(stream, CompressionMode.Decompress))
             * {
             *     deserializedSaveObject = (SaveObject)serializer.ReadObject(decompress);
             * }
             * }
             *
             * loading = deserializedSaveObject.savetype;
             * version = deserializedSaveObject.saveversionnumber;
             */
            //Parrarel for loading the game
            Parallel.Invoke(() =>
            {
                Airlines.Clear();

                foreach (Airline airline in deserializedSaveObject.airlinesList)
                {
                    Airlines.AddAirline(airline);
                }
            },
                            () =>
            {
                Setup.LoadAirports();

                var airports = Airports.GetAllAirports();

                Airports.Clear();

                foreach (Airport airport in deserializedSaveObject.airportsList)
                {
                    airport.Statics = new AirportStatics(airport);
                    Airports.AddAirport(airport);
                }

                foreach (string iata in deserializedSaveObject.airportsfromstringList)
                {
                    Airport airport = airports.FirstOrDefault(a => a.Profile.IATACode == iata);

                    if (airport != null)
                    {
                        Airports.AddAirport(airport);
                    }
                }
            },
                            () =>
            {
                Airliners.Clear();

                foreach (Airliner airliner in deserializedSaveObject.airlinersList)
                {
                    Airliners.AddAirliner(airliner);
                }
            },
                            () =>
            {
                CalendarItems.Clear();

                foreach (CalendarItem item in deserializedSaveObject.calendaritemsList)
                {
                    CalendarItems.AddCalendarItem(item);
                }
            },
                            () =>
            {
                Configurations.Clear();

                foreach (Configuration configuration in deserializedSaveObject.configurationList)
                {
                    Configurations.AddConfiguration(configuration);
                }
            },
                            () =>
            {
                RandomEvents.Clear();

                foreach (RandomEvent e in deserializedSaveObject.eventsList)
                {
                    RandomEvents.AddEvent(e);
                }
            },
                            () =>
            {
                Alliances.Clear();

                foreach (Alliance alliance in deserializedSaveObject.allianceList)
                {
                    Alliances.AddAlliance(alliance);
                }
            },
                            () =>
            {
                AirportFacilities.Clear();

                foreach (AirportFacility facility in deserializedSaveObject.Airportfacilitieslist)
                {
                    AirportFacilities.AddFacility(facility);
                }
            },
                            () =>
            {
                FeeTypes.Clear();

                foreach (FeeType type in deserializedSaveObject.feeTypeslist)
                {
                    FeeTypes.AddType(type);
                }
            },
                            () =>
            {
                AdvertisementTypes.Clear();

                foreach (AdvertisementType addtype in deserializedSaveObject.advertisementTypeslist)
                {
                    AdvertisementTypes.AddAdvertisementType(addtype);
                }
            },
                            () =>
            {
                AirlinerFacilities.Clear();

                foreach (AirlinerFacility airlinerfas in deserializedSaveObject.airlinerfacilitieslist)
                {
                    AirlinerFacilities.AddFacility(airlinerfas);
                }
            },
                            () =>
            {
                RouteFacilities.Clear();

                foreach (RouteFacility routefas in deserializedSaveObject.routefacilitieslist)
                {
                    RouteFacilities.AddFacility(routefas);
                }
            },
                            () =>
            {
                GameObject.SetInstance(deserializedSaveObject.instance);
                Settings.SetInstance(deserializedSaveObject.settings);
            },
                            () =>
            {
                if (deserializedSaveObject.airlinefacilitieslist != null)
                {
                    AirlineFacilities.Clear();

                    foreach (AirlineFacility airlinefac in deserializedSaveObject.airlinefacilitieslist)
                    {
                        AirlineFacilities.AddFacility(airlinefac);
                    }
                }
            }); //close parallel.invoke

            //for 0.3.9.2 and the issue with no saved facilities on a route classes
            var emptyRouteClassesFacilities = Configurations.GetConfigurations(Configuration.ConfigurationType.Routeclasses).SelectMany(c => ((RouteClassesConfiguration)c).Classes.Where(cl => cl.Facilities == null));

            foreach (RouteClassConfiguration rClassConfiguration in emptyRouteClassesFacilities)
            {
                rClassConfiguration.Facilities = new List <RouteFacility>();
            }

            Setup.SetupLoadedGame();
            //Maybe this helps? But i doubt this is the best way
            Action action = () =>
            {
                Stopwatch swPax = new Stopwatch();
                swPax.Start();

                PassengerHelpers.CreateDestinationDemand();

                //Console.WriteLine("Demand have been created in {0} ms.", swPax.ElapsedMilliseconds);
                swPax.Stop();
            };

            //create some pilots for the game
            int pilotsPool = 100 * Airlines.GetAllAirlines().Count;

            GeneralHelpers.CreatePilots(pilotsPool);
            int instructorsPool = 75 * Airlines.GetAllAirlines().Count;

            GeneralHelpers.CreateInstructors(instructorsPool);

            //creates some airliners for the game
            AirlinerHelpers.CreateStartUpAirliners();

            //Start the game paused
            GameObjectWorker.GetInstance().startPaused();

            //Task is needed this unlocks the game agian.
            Task.Factory.StartNew(action);
        }
Exemplo n.º 4
0
        //saves a game
        public static void SaveGame(string name)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            SaveObject so = new SaveObject();

            Parallel.Invoke(() =>
            {
                so.airportsList           = new List <Airport>();
                so.airportsfromstringList = new List <string>();

                var airportsInUse = Airports.GetAllAirports().Where(a => Airlines.GetAllAirlines().Exists(al => al.Airports.Contains(a)) || a.hasAirlineFacility());
                so.airportsList.AddRange(airportsInUse);

                foreach (Airport airport in Airports.GetAirports(a => !airportsInUse.Contains(a)))
                {
                    so.airportsfromstringList.Add(airport.Profile.IATACode);
                }
            }, () =>
            {
                so.airlinesList = new List <Airline>();
                so.airlinesList.AddRange(Airlines.GetAllAirlines());
            }, () =>
            {
                so.airlinersList = new List <Airliner>();
                so.airlinersList.AddRange(Airliners.GetAllAirliners().Where(a => a.Airline != null));
            }, () =>
            {
                so.calendaritemsList = new List <CalendarItem>();
                so.calendaritemsList.AddRange(CalendarItems.GetCalendarItems());
            }, () =>
            {
                so.configurationList = new List <Configuration>();
                so.configurationList.AddRange(Configurations.GetConfigurations());
            }, () =>
            {
                so.eventsList = new List <RandomEvent>();
                so.eventsList.AddRange(RandomEvents.GetEvents());
            }, () =>
            {
                so.allianceList = new List <Alliance>();
                so.allianceList.AddRange(Alliances.GetAlliances());
            }, () =>
            {
                so.Airportfacilitieslist = new List <AirportFacility>();
                so.Airportfacilitieslist.AddRange(AirportFacilities.GetFacilities());
            }, () =>
            {
                so.feeTypeslist = new List <FeeType>();
                so.feeTypeslist.AddRange(FeeTypes.GetTypes());
            }, () =>
            {
                so.advertisementTypeslist = new List <AdvertisementType>();
                so.advertisementTypeslist.AddRange(AdvertisementTypes.GetTypes());
            }, () =>
            {
                so.airlinerfacilitieslist = new List <AirlinerFacility>();
                so.airlinerfacilitieslist.AddRange(AirlinerFacilities.GetAllFacilities());
            }, () =>
            {
                so.routefacilitieslist = new List <RouteFacility>();
                so.routefacilitieslist.AddRange(RouteFacilities.GetAllFacilities());
            }, () =>
            {
                so.instance = GameObject.GetInstance();
                so.settings = Settings.GetInstance();
            }, () =>
            {
                so.airlinefacilitieslist = new List <AirlineFacility>();
                so.airlinefacilitieslist.AddRange(AirlineFacilities.GetFacilities());
            });

            string fileName = AppSettings.getCommonApplicationDataPath() + "\\saves\\" + name + ".sav";

            FileSerializer.Serialize(fileName, so);

            sw.Stop();
            Console.WriteLine("Saving: {0} ms", sw.ElapsedMilliseconds);



            //Clearing stats because there is no need for saving those.
            if (name != "autosave")
            {
                Airports.GetAllAirports().ForEach(a => a.clearDestinationPassengerStatistics());
                Airports.GetAllAirports().ForEach(a => a.clearDestinationCargoStatistics());
                AirlineHelpers.ClearRoutesStatistics();
                AirlineHelpers.ClearAirlinesStatistics();
                AirportHelpers.ClearAirportStatistics();
            }

            /*
             * SaveObject so = new SaveObject();
             * Parallel.Invoke(() =>
             * {
             *  so.airportsList = new List<Airport>();
             *  so.airportsList.AddRange(Airports.GetAllAirports());
             * }, () =>
             * {
             *  so.airlinesList = new List<Airline>();
             *  so.airlinesList.AddRange(Airlines.GetAllAirlines());
             * }, () =>
             * {
             *  so.airlinersList = new List<Airliner>();
             *  so.airlinersList.AddRange(Airliners.GetAllAirliners());
             * }, () =>
             * {
             *  so.calendaritemsList = new List<CalendarItem>();
             *  so.calendaritemsList.AddRange(CalendarItems.GetCalendarItems());
             * }, () =>
             * {
             *  so.configurationList = new List<Configuration>();
             *  so.configurationList.AddRange(Configurations.GetConfigurations());
             * }, () =>
             * {
             *  so.eventsList = new List<RandomEvent>();
             *  so.eventsList.AddRange(RandomEvents.GetEvents());
             * }, () =>
             * {
             *  so.allianceList = new List<Alliance>();
             *  so.allianceList.AddRange(Alliances.GetAlliances());
             * }, () =>
             * {
             *  so.Airportfacilitieslist = new List<AirportFacility>();
             *  so.Airportfacilitieslist.AddRange(AirportFacilities.GetFacilities());
             * }, () =>
             * {
             *  so.feeTypeslist = new List<FeeType>();
             *  so.feeTypeslist.AddRange(FeeTypes.GetTypes());
             * }, () =>
             * {
             *  so.advertisementTypeslist = new List<AdvertisementType>();
             *  so.advertisementTypeslist.AddRange(AdvertisementTypes.GetTypes());
             * }, () =>
             * {
             *  so.airlinerfacilitieslist = new List<AirlinerFacility>();
             *  so.airlinerfacilitieslist.AddRange(AirlinerFacilities.GetAllFacilities());
             * }, () =>
             * {
             *  so.routefacilitieslist = new List<RouteFacility>();
             *  so.routefacilitieslist.AddRange(RouteFacilities.GetAllFacilities());
             * }, () =>
             * {
             *  so.instance = GameObject.GetInstance();
             *  so.settings = Settings.GetInstance();
             *  so.savetype = "039";
             *  so.saveversionnumber = 1;
             * });
             */
            /*
             * DataContractSerializer serializer = new DataContractSerializer(typeof(SaveObject), null, Int32.MaxValue, false, true, null);
             *
             * using (Stream stream = new FileStream(fileName, FileMode.Create))
             * {
             *  using (DeflateStream compress = new DeflateStream(stream, CompressionLevel.Fastest))
             *  {
             *      serializer.WriteObject(compress, so);
             *  }
             * }
             */
        }
        //saves a game
        public static void SaveGame(string name)
        {
            //Pause the game so we can save without the clock running :)
            GameObjectWorker.GetInstance().pause();

            string fileName = AppSettings.getCommonApplicationDataPath() + "\\saves\\" + name + ".sav";

            Stopwatch sw = new Stopwatch();
            sw.Start();

            SaveObject so = new SaveObject();
            Parallel.Invoke(() =>
            {
                so.airportsList = new List<Airport>();
                so.airportsList.AddRange(Airports.GetAllAirports());
            }, () =>
            {
                so.airlinesList = new List<Airline>();
                so.airlinesList.AddRange(Airlines.GetAllAirlines());
            }, () =>
            {
                so.airlinersList = new List<Airliner>();
                so.airlinersList.AddRange(Airliners.GetAllAirliners());
            }, () =>
            {
                so.calendaritemsList = new List<CalendarItem>();
                so.calendaritemsList.AddRange(CalendarItems.GetCalendarItems());
            }, () =>
            {
                so.configurationList = new List<Configuration>();
                so.configurationList.AddRange(Configurations.GetConfigurations());
            }, () =>
            {
                so.eventsList = new List<RandomEvent>();
                so.eventsList.AddRange(RandomEvents.GetEvents());
            }, () =>
            {
                so.allianceList = new List<Alliance>();
                so.allianceList.AddRange(Alliances.GetAlliances());
            }, () =>
            {
                so.Airportfacilitieslist = new List<AirportFacility>();
                so.Airportfacilitieslist.AddRange(AirportFacilities.GetFacilities());
            }, () =>
            {
                so.instance = GameObject.GetInstance();
            });

             DataContractSerializer serializer = new DataContractSerializer(typeof(SaveObject), null,
                      Int32.MaxValue,
                      false,
                      true,
                      null);
             using (Stream stream = new FileStream(fileName, FileMode.Create))
             {
             using (DeflateStream compress = new DeflateStream(stream, CompressionMode.Compress))
             {
             serializer.WriteObject(compress, so);
             }
             }

            sw.Stop();
        }