public static GameRuntimeData LoadArchive(int fileIndex) { string sPath = GetArchiveFilePath(fileIndex); GameRuntimeData tagArchive = null; if (File.Exists(sPath)) { using (FileStream fs = File.OpenRead(sPath)) { byte[] buffer1 = new byte[sizeof(int)]; fs.Read(buffer1, 0, sizeof(int)); int dataLen = BitConverter.ToInt32(buffer1, 0); byte[] archiveData = new byte[dataLen]; fs.Read(archiveData, 0, dataLen); TDES tdesTool = new TDES(); tdesTool.Init(ConStr.DES_KEY); byte[] decryptData = tdesTool.Decrypt(archiveData); string txtData = System.Text.Encoding.Default.GetString(decryptData, 0, decryptData.Length); Hashtable hsData = txtData.hashtableFromJson(); tagArchive = Saveable.Facade.LoadRoot <GameRuntimeData>(hsData); fs.Close(); } } _instance = tagArchive;//记录单例 _instance.SaveIndex = fileIndex; _instance.InitAllRole(); return(tagArchive); }
public void SaveToFile(int fileIndex) { string fileName = string.Format(ARCHIVE_FILE_NAME, fileIndex); string sFolderPath = Application.persistentDataPath + "/" + ARCHIVE_FILE_DIR; if (!Directory.Exists(sFolderPath)) { Directory.CreateDirectory(sFolderPath); } string sPath = sFolderPath + "/" + fileName; using (MemoryStream ms = new MemoryStream()) { //写入存档内容 TDES tdesTool = new TDES(); tdesTool.Init(ConStr.DES_KEY); Hashtable hsData = Save(); byte[] strBuffer = System.Text.Encoding.Default.GetBytes(hsData.toJson()); byte[] encryptDatas = tdesTool.Encrypt(strBuffer); ms.Write(BitConverter.GetBytes(encryptDatas.Length), 0, sizeof(int)); ms.Write(encryptDatas, 0, encryptDatas.Length); File.WriteAllBytes(sPath, ms.GetBuffer()); } }