/// <summary> /// Takes the name of the Unity stored TextAsset and reserializes it to a persistent location for access and modification /// </summary> /// <param name="fileName">Name of the file in the Resources Folder</param> private static void unpackFile(string fileName) { try { TextAsset textAsset = Resources.Load(fileName) as TextAsset; MemoryStream memoryStream = new MemoryStream(textAsset.bytes); string filePath = UnityTools.getPersistentFilePath() + "/" + filenamePrefix + fileName + fileNameExtension; FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write); memoryStream.WriteTo(fileStream); fileStream.Close(); memoryStream.Close(); } catch (IOException) { string message = "When trying to unpack file '" + fileName + "', an IO exception occured and the file was not unpacked."; DS_MessageLogger.logMessageToBuildFile(message, SerializerLogType.Error); return; } catch (Exception) { string message = "When trying to unpack file '" + fileName + "', an unknown exception occured and the file was not unpacked."; DS_MessageLogger.logMessageToBuildFile(message, SerializerLogType.Error); } }
/// <summary> /// Deserializes a file from the Unity Resources folder and returns it as the provided object type. /// </summary> /// <typeparam name="T">Object type to deserialize file into</typeparam> /// <param name="fileName">Name of the file to deserialize</param> /// <returns>Deserialized file in the provided type</returns> private static T loadFromPersistentFolder <T> (string fileName) { string filePath = UnityTools.getPersistentFilePath() + "/" + fileName; //make sure file exists if (SystemTools.fileExists(filePath) == false) { DS_MessageLogger.logMessageToUnityConsole("Failed to find file '" + fileName + "' when loading the" + "file for deserialization on the loadSave() call.", SerializerLogType.Warning); return(default(T)); } //Try to load the file try { BinaryFormatter binaryFormatter = new BinaryFormatter(); FileStream fileStream = new FileStream(filePath, FileMode.Open); T returnObject = (T)binaryFormatter.Deserialize(fileStream); fileStream.Close(); return(returnObject); } catch (IOException) { DS_MessageLogger.logMessageToUnityConsole("An IO exception occurred while trying to load '" + fileName + "' in the Persistent build folder.", SerializerLogType.Error); return(default(T)); } catch (Exception) { DS_MessageLogger.logMessageToUnityConsole("An unknown exception occurred while trying to load '" + fileName + "' in the Persistent build folder.", SerializerLogType.Error); return(default(T)); } }
/// <summary> /// Saves the object to a binary file in the PersistentPath for post deployment saves /// </summary> /// <typeparam name="T">Object type to be serialized</typeparam> /// <param name="objectToSave">Object to be serialized</param> /// <param name="fileName">Name to save the file under</param> private static void serializeObject_Build(object objectToSave, string fileName) { //Initialize objects for use BinaryFormatter binaryFormatter; FileStream fileStream; //Build file name and save reference fileName = filenamePrefix + fileName; string filePath = UnityTools.getPersistentFilePath() + "/" + filenamePrefix + fileName + fileNameExtension; //Serialize the new Object provided by the Dev try { //Create stream and formatter binaryFormatter = new BinaryFormatter(); fileStream = new FileStream(filePath, FileMode.Create); //Serialize file and close stream binaryFormatter.Serialize(fileStream, objectToSave); fileStream.Close(); } catch (IOException) { DS_MessageLogger.logMessageToUnityConsole("An unexpected IO failure occured when trying to serialize the provided object to the" + " Persistent File Path in build. " + "Process is being aborted! \n" + "Class Name: " + SystemTools.getObjectName(objectToSave), SerializerLogType.Error); return; } catch (Exception) { DS_MessageLogger.logMessageToUnityConsole("An unknown failure occured when trying to serialize the provided object to the " + " Persistent File Path in build." + "Process is being aborted! \n" + "Class Name: " + SystemTools.getObjectName(objectToSave), SerializerLogType.Error); return; } //Log the save if messages are enabled DS_MessageLogger.logMessageToUnityConsole(SystemTools.getObjectName(objectToSave) + " was saved successfully", SerializerLogType.Standard); }