/// <summary> /// Load specified ISavable object to binary file /// </summary> /// <param name="savable">ISavable to save</param> /// <param name="fileName"> full path of destination file</param> public static void SaveToBinaryFile(ISavable savable, string fileName, string password = null) { if (savable == null) { throw new ArgumentNullException("Invalid ISavable object."); } string dir = System.IO.Path.GetDirectoryName(fileName); string name = System.IO.Path.GetFileName(fileName); int tempPostfix = 0; string tempFile = System.IO.Path.Combine(dir, name + "_Tmp" + tempPostfix); while (System.IO.File.Exists(tempFile)) { tempPostfix++; tempFile = System.IO.Path.Combine(dir, name + "_Tmp" + tempPostfix); } PCBinarySaveStream stream = new PCBinarySaveStream(tempFile); try { if (password != null) { string str = SaveToBinaryString(savable); str = SecurePlayerPrefs.Encrypt(str, password); stream.Write(str); } else { SaveToStream(savable, stream); } } catch (Exception) { throw; } finally { stream.Close(); } if (System.IO.File.Exists(fileName)) { System.IO.File.Delete(fileName); } System.IO.File.Move(tempFile, fileName); }
/// <summary> /// Load specified ISavable object to xml file /// </summary> /// <param name="savable">ISavable to save</param> /// <param name="fileName"> full path of destination file</param> public static void SaveToXmlFile(ISavable savable, string fileName, string password = null) { if (savable == null) { throw new ArgumentNullException("Invalid ISavable object."); } string dir = System.IO.Path.GetDirectoryName(fileName); string name = System.IO.Path.GetFileName(fileName); int tempPostfix = 0; string tempFile = System.IO.Path.Combine(dir, name + "_Tmp" + tempPostfix); while (System.IO.File.Exists(tempFile)) { tempPostfix++; tempFile = System.IO.Path.Combine(dir, name + "_Tmp" + tempPostfix); } string xmlContent = SaveToXmlContent(savable); if (password != null) { xmlContent = SecurePlayerPrefs.Encrypt(xmlContent, password); } System.IO.FileStream fileStream = new System.IO.FileStream(tempFile, System.IO.FileMode.Create, System.IO.FileAccess.Write); System.IO.TextWriter writer = new System.IO.StreamWriter(fileStream); writer.Write(xmlContent); writer.Close(); fileStream.Close(); if (System.IO.File.Exists(fileName)) { System.IO.File.Delete(fileName); } System.IO.File.Move(tempFile, fileName); }