/// <summary> /// Deserializes a datafile /// </summary> /// <typeparam name="T">The type to deserialize from the datafile</typeparam> /// <param name="filename">The datafile name</param> /// <returns></returns> internal static T DeserializeDatafile <T>(string filename) { // Gets the full path string path = Datafile.GetFullPath(filename); try { // Deserializes using (var s = FileHelper.OpenRead(path, false)) { using (GZipStream zs = new GZipStream(s, CompressionMode.Decompress)) { XmlSerializer xs = new XmlSerializer(typeof(T)); return((T)xs.Deserialize(zs)); } } } catch (InvalidOperationException ex) { String message = String.Format("An error occured decompressing {0}, the error message was '{1}' from '{2}'. Try deleting all of the xml.gz files in %APPDATA%\\EVEMon.", filename, ex.Message, ex.Source); throw new ApplicationException(message, ex); } catch (XmlException ex) { String message = String.Format("An error occured reading the XML from {0}, the error message was '{1}' from '{2}'. Try deleting all of the xml.gz files in %APPDATA%\\EVEMon.", filename, ex.Message, ex.Source); throw new ApplicationException(message, ex); } }
/// <summary> /// Deserializes a datafile. /// </summary> /// <typeparam name="T">The type to deserialize from the datafile</typeparam> /// <param name="filename">The datafile name</param> /// <param name="transform"></param> /// <returns></returns> internal static T DeserializeDatafile <T>(string filename, XslCompiledTransform transform = null) { // Gets the full path string path = Datafile.GetFullPath(filename); try { using (Stream stream = FileHelper.OpenRead(path, false)) { GZipStream gZipStream = new GZipStream(stream, CompressionMode.Decompress); XmlSerializer xs = new XmlSerializer(typeof(T)); // Deserialization without transform if (transform == null) { return((T)xs.Deserialize(gZipStream)); } // Deserialization with transform MemoryStream memoryStream = GetMemoryStream(); XmlTextWriter writer = new XmlTextWriter(memoryStream, Encoding.UTF8); XmlTextReader reader = new XmlTextReader(gZipStream); // Apply the XSL transform writer.Formatting = Formatting.Indented; transform.Transform(reader, writer); writer.Flush(); // Deserialize from the given stream memoryStream.Seek(0, SeekOrigin.Begin); return((T)xs.Deserialize(memoryStream)); } } catch (InvalidOperationException ex) { String message = $"An error occurred decompressing {filename}, the error message was '{ex.Message}' from '{ex.Source}'. " + $"Try deleting all of the {Datafile.DatafilesExtension} files in %APPDATA%\\EVEMon."; throw new InvalidOperationException(message, ex); } catch (XmlException ex) { String message = $"An error occurred reading the XML from {filename}, the error message was '{ex.Message}' from '{ex.Source}'. " + $"Try deleting all of the {Datafile.DatafilesExtension} files in %APPDATA%\\EVEMon."; throw new XmlException(message, ex); } }
/// <summary> /// Deletes the data files. /// </summary> public static void DeleteDataFiles() { foreach (string file in Datafile.GetFilesFrom(EveMonClient.EVEMonDataDir, Datafile.DatafilesExtension).Concat(Datafile.GetFilesFrom(EveMonClient. EVEMonDataDir, Datafile.OldDatafileExtension))) { try { FileInfo dataFile = new FileInfo(file); if (dataFile.Exists) { FileHelper.DeleteFile(dataFile.FullName); } } catch (UnauthorizedAccessException e) { ExceptionHandler.LogException(e, false); } } }
/// <summary> /// Downloads the updates. /// </summary> private void DownloadUpdates() { List<SerializableDatafile> datafiles = new List<SerializableDatafile>(); // Copy the list of datafiles m_args.ChangedFiles.ForEach(x => datafiles.Add(x)); foreach (var dfv in datafiles) { // Work out the new names of the files string urn = String.Format(CultureConstants.DefaultCulture, "{0}/{1}", dfv.Url, dfv.Name); string oldFilename = Path.Combine(EveClient.EVEMonDataDir, dfv.Name); string newFilename = String.Format(CultureConstants.DefaultCulture, "{0}.tmp", oldFilename); // If the file already exists delete it if (File.Exists(newFilename)) File.Delete(newFilename); // Show the download dialog, which will download the file using (UpdateDownloadForm f = new UpdateDownloadForm(urn, newFilename)) { if (f.ShowDialog() == DialogResult.OK) { string filename = Path.GetFileName(newFilename); Datafile datafile = new Datafile(filename); if (datafile.MD5Sum != dfv.MD5Sum) { File.Delete(newFilename); continue; } ReplaceDatafile(oldFilename, newFilename); m_args.ChangedFiles.Remove(dfv); } } } }
/// <summary> /// Updates the info in the data files section. /// </summary> /// <param name="control"></param> /// <param name="datafile"></param> private static void UpdateDatafileInfo(DatafileControl control, Datafile datafile) { // Data file info FileInfo fileInfo = new FileInfo(Path.Combine(datafileDir, datafile.Filename)); // Assign info control.gbDatafile.Text = datafile.Filename; control.lblMD5Sum.Text = datafile.MD5Sum; control.dtpDatafiles.Value = fileInfo.LastWriteTime; }