/// <summary> /// The save env. /// </summary> /// <param name="env"> /// The env. /// </param> /// <param name="filePath"> /// The file path. /// </param> public void SaveEnv(MonitorEnvironment env, string filePath) { var data = new List<KeyValue<Type, ConfigInfo>>(); if (env.ArchiveManager != null) { data.Add(new KeyValue<Type, ConfigInfo>(typeof(IArchiveManager), new ConfigInfo(env.ArchiveManager))); } if (env.FareDataProvider != null) { data.Add(new KeyValue<Type, ConfigInfo>(typeof(IFareDataProvider), new ConfigInfo(env.FareDataProvider))); } if (env.FareDatabase != null) { data.Add(new KeyValue<Type, ConfigInfo>(typeof(IFareDatabase), new ConfigInfo(env.FareDatabase))); } var syncDb = env.FareDatabase as ISyncableDatabase; if (syncDb != null && syncDb.DataSynchronizer != null) { data.Add(new KeyValue<Type, ConfigInfo>(typeof(IDatabaseSyncer<>), new ConfigInfo(syncDb.DataSynchronizer))); } if (env.CurrencyProvider != null) { data.Add(new KeyValue<Type, ConfigInfo>(typeof(ICurrencyProvider), new ConfigInfo(env.CurrencyProvider))); } if (env.BackgroundServices != null) { var allServices = env.BackgroundServices.GetAll(); foreach (var s in allServices) { if (s != env.CurrencyProvider) { data.Add(new KeyValue<Type, ConfigInfo>(s.GetType(), new ConfigInfo(s))); } } } var rawData = new byte[data.Count][]; var formatter = new TolerantBinaryFormatter(this.Logger); using (var ms = new MemoryStream()) { for (int i = 0; i < data.Count; i++) { formatter.Serialize(ms, data[i]); rawData[i] = ms.ToArray(); ms.SetLength(0); } } using (var stream = File.Open(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) { using (var writer = new BinaryWriter(stream)) { writer.Write(AppUtil.ProductName); writer.Write(AppUtil.ProductVersion); formatter.Serialize(stream, rawData); } } }
/// <summary> /// Save the route to the catalogue /// </summary> /// <param name="route"> /// Travel route /// </param> /// <returns> /// The saved route's information /// </returns> private RouteInfo SaveRoute(StorageRoute route) { var logger = AppContext.Logger; int max = 0; using (var fs = File.Open(this.CatalogFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { var formatter = new TolerantBinaryFormatter(logger); try { while (fs.Position != fs.Length) { var existRoute = formatter.Deserialize(fs) as RouteInfo; if (existRoute != null) { if (existRoute.Id > max) { max = existRoute.Id; } if (existRoute.Route.Equals(route)) { return existRoute; } } } } catch (Exception ex) { logger.Error("Failed to read route information: " + ex.Message); fs.SetLength(0); fs.Position = 0; } var newItem = new RouteInfo(++max, route); formatter.Serialize(fs, newItem); return new RouteInfo(max, route); } }