/// <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> /// Call this in the game build to take all persistent files and load them into the PersistentPath directory /// </summary> /// <returns>Boolean indicating if the unpack was sucessful</returns> public static bool unpackPersistentSaves() { //Make sure the file exists TextAsset ta = Resources.Load(fileNameTrackerName) as TextAsset; if (ta == null) { DS_MessageLogger.logMessageToBuildFile("Failed to find FileNameTracker File, either no saves were created or file was deleted", SerializerLogType.Warning); return(false); } FileNameTracker fileNameTracker = null; //Deserialize the file to read from try { BinaryFormatter binaryFormatter = new BinaryFormatter(); MemoryStream mStream = new MemoryStream(ta.bytes); fileNameTracker = binaryFormatter.Deserialize(mStream) as FileNameTracker; } catch (IOException) { DS_MessageLogger.logMessageToBuildFile("IO Failure when trying to deserialize the FileNameTracker while unpacking the " + "persistent binaries.", SerializerLogType.Error); return(false); } catch (Exception) { DS_MessageLogger.logMessageToBuildFile("General failure when trying to deserialize the FileNameTracker while unpacking the " + "persistent binaries.", SerializerLogType.Error); return(false); } //Load all saved files into the persistent folder foreach (string fileName in fileNameTracker.fileNames) { unpackFile(fileName); } return(true); }