public static void Build(string folderPath, string outputPath, MArchivePacker maPacker = null, IPsbFilter filter = null) { using (FileStream packStream = File.Create(outputPath + ".bin")) using (FileStream psbStream = File.Create(outputPath + ".psb")) { ArchiveV1 archive = new ArchiveV1(); archive.ObjectType = "archive"; archive.Version = 1.0f; archive.FileInfo = new Dictionary <string, List <int> >(); folderPath = Path.GetFullPath(folderPath); foreach (var file in Directory.GetFiles(folderPath, "*", SearchOption.AllDirectories)) { string key = file.Replace(folderPath, string.Empty).TrimStart('/', '\\'); Console.WriteLine($"Packing {key}"); var targetLength = (packStream.Position + ALIGNMENT - 1) / ALIGNMENT * ALIGNMENT; byte[] alignBytes = new byte[targetLength - packStream.Length]; packStream.Write(alignBytes, 0, alignBytes.Length); var currPos = packStream.Position; using (FileStream fs = File.OpenRead(file)) { fs.CopyTo(packStream); archive.FileInfo.Add(key.Replace('\\', '/'), new List <int>() { (int)currPos, (int)fs.Length }); } } JToken root = JToken.FromObject(archive); PsbWriter writer = new PsbWriter(root, null) { Version = 3 }; writer.Write(psbStream, filter); } if (maPacker != null) { maPacker.CompressFile(outputPath + ".psb"); } }
public static void SerializePsb(string path, ushort version, PsbFlags flags, IPsbFilter filter, bool optimize, bool readAsFloat) { string psbPath = Path.ChangeExtension(path, null); if (!psbPath.ToLower().EndsWith(".psb")) { psbPath += ".psb"; } using (StreamReader reader = File.OpenText(path)) using (Stream writer = File.Create(psbPath)) { JsonTextReader jReader = new JsonTextReader(reader) { FloatParseHandling = readAsFloat ? FloatParseHandling.Double : FloatParseHandling.Decimal }; JToken root = JToken.ReadFrom(jReader); IPsbStreamSource streamSource = new CliStreamSource(Path.ChangeExtension(path, ".streams")); PsbWriter psbWriter = new PsbWriter(root, streamSource); psbWriter.Version = version; psbWriter.Flags = flags; psbWriter.Optimize = optimize; psbWriter.Write(writer, filter); } }
public static void MigrateSaves(int sourceSize, int destinationSize, string lunarPath = null) { using (ScpClient scp = new ScpClient("169.254.215.100", "root", "5A7213")) { scp.Connect(); MemoryStream settingsDataStream = new MemoryStream(); scp.Download("/usr/game/save/data_008_0000.bin", settingsDataStream); MemoryStream settingsMetaStream = new MemoryStream(); scp.Download("/usr/game/save/meta_008_0000.bin", settingsMetaStream); byte[] settingsData = new byte[settingsDataStream.Length]; settingsDataStream.Position = 0; settingsDataStream.Read(settingsData, 0, settingsData.Length); SystemData originalSave = SystemData.FromByteArray(settingsData, sourceSize); SystemData migratedSave = new SystemData(destinationSize); migratedSave.Base_Settings = originalSave.Base_Settings; migratedSave.Work_Trial = originalSave.Work_Trial; int savesToCopy = Math.Min(sourceSize, destinationSize); for (int i = 0; i < savesToCopy; i++) { migratedSave.Setting_Games[i] = originalSave.Setting_Games[i]; migratedSave.Sram_Data[i] = originalSave.Sram_Data[i]; } byte[] migratedData = migratedSave.ToByteArray(); using (MemoryStream saveFile = new MemoryStream(migratedData)) { scp.Upload(saveFile, "/usr/game/save/data_008_0000.bin"); if (!string.IsNullOrEmpty(lunarPath)) { File.WriteAllBytes($@"{lunarPath}\data_008_0000.bin", migratedData); } } settingsMetaStream.Position = 0; using (PsbReader psbReader = new PsbReader(settingsMetaStream)) { JToken meta = psbReader.Root; meta["FileSize"] = migratedData.Length; meta["OriginalSize"] = migratedData.Length; (meta["Digest"] as JStream).BinaryData = MD5.Create().ComputeHash(migratedData); PsbWriter psbWriter = new PsbWriter(meta, null); psbWriter.Version = psbReader.Version; using (MemoryStream metaFile = new MemoryStream()) { psbWriter.Write(metaFile, null); metaFile.Position = 0; scp.Upload(metaFile, "/usr/game/save/meta_008_0000.bin"); if (!string.IsNullOrEmpty(lunarPath)) { byte[] metaData = new byte[metaFile.Length]; metaFile.Position = 0; metaFile.Read(metaData, 0, metaData.Length); File.WriteAllBytes($@"{lunarPath}\meta_008_0000.bin", metaData); } } } } }