// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved. using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading.Tasks; namespace Tools.DotNETCommon { /// /// Utility functions for serializing using the BinaryFormatter /// public static class BinaryFormatterUtils { /// /// Load an object from a file on disk, using the binary formatter /// /// File to read from /// Instance of the object that was read from disk public static T Load(FileReference Location) { using (FileStream Stream = new FileStream(Location.FullName, FileMode.Open, FileAccess.Read)) { BinaryFormatter Formatter = new BinaryFormatter(); return (T)Formatter.Deserialize(Stream); } } /// /// Saves a file to disk, using the binary formatter /// /// File to write to /// Object to serialize public static void Save(FileReference Location, object Object) { DirectoryReference.CreateDirectory(Location.Directory); using (FileStream Stream = new FileStream(Location.FullName, FileMode.Create, FileAccess.Write)) { BinaryFormatter Formatter = new BinaryFormatter(); Formatter.Serialize(Stream, Object); } } /// /// Saves a file to disk using the binary formatter, without updating the timestamp if it hasn't changed /// /// File to write to /// Object to serialize public static void SaveIfDifferent(FileReference Location, object Object) { byte[] Contents; using(MemoryStream Stream = new MemoryStream()) { BinaryFormatter Formatter = new BinaryFormatter(); Formatter.Serialize(Stream, Object); Contents = Stream.ToArray(); } DirectoryReference.CreateDirectory(Location.Directory); FileReference.WriteAllBytesIfDifferent(Location, Contents); } } }