public byte[] Export(FileEntry[] Resources) { if (!Initialized) { throw new Exception("You Need Import Before Export"); } if (Resources.Length != EntryCount) { throw new Exception("You Can't Add or Delete Resources."); } int TotalSize = 0; for (int i = 0; i < Resources.Length; i++) { TotalSize += Resources[i].Data.Length + (FixOffsets && i + 1 != Resources.Length ? 4 - ((StartPos + TotalSize + Resources[i].Data.Length) % 4) : 0); } byte[] ResTable = new byte[TotalSize]; TotalSize = 0; byte[] MainData = CutAt(packget, StartPos); for (int i = 0; i < Resources.Length; i++) { byte[] file = Resources[i].Data; file.CopyTo(ResTable, TotalSize); PSBStrMan.CreateOffset(OffsetSize, TotalSize).CopyTo(MainData, OffsetTablePos + (i * OffsetSize)); PSBStrMan.CreateOffset(ResSizeOffSize, file.Length).CopyTo(MainData, ResSizeOffTablePos + (i * ResSizeOffSize)); //If need FixOffsets and is the last loop time... TotalSize += file.Length + (FixOffsets && i + 1 != Resources.Length ? 4 - ((StartPos + TotalSize + file.Length) % 4) : 0); } byte[] ResultPackget = new byte[MainData.Length + ResTable.Length]; MainData.CopyTo(ResultPackget, 0); ResTable.CopyTo(ResultPackget, MainData.Length); return(CompressPackget ? PSBStrMan.CompressMDF(ResultPackget) : ResultPackget); }
public PSBAnalyzer(byte[] Script) { var Status = PSBStrMan.GetPackgetStatus(Script); if (Status == PSBStrMan.PackgetStatus.MDF) { Script = PSBStrMan.ExtractMDF(Script); } Status = PSBStrMan.GetPackgetStatus(Script); if (Status != PSBStrMan.PackgetStatus.PSB) { throw new Exception("Bad File Format"); } this.Script = new byte[Script.Length]; Script.CopyTo(this.Script, 0); StringManager = new PSBStrMan(Script) { CompressPackage = true, ForceMaxOffsetLength = ExtendStringLimit }; ByteCodeStart = ReadOffset(this.Script, 0x24, 4); ByteCodeLen = ReadOffset(this.Script, 0x10, 4) - ByteCodeStart; if (ByteCodeLen + ByteCodeStart > Script.Length) { throw new Exception("Corrupted Script"); } }
private int[] GetOffsetInfo(byte[] file, int pos) { int OffSize = PSBStrMan.ConvertSize(file[pos]); int Count = PSBStrMan.ReadOffset(file, pos + 1, OffSize); pos += 1 + OffSize; //return[0] = OffsetSize; //return[1] = StartOffsetPos; //return[2] = EntryCount; return(new int[] { PSBStrMan.ConvertSize(file[pos]), pos + 1, Count }); }
private int[] GetValues(byte[] file, int pos) { int[] tmp = GetOffsetInfo(file, pos); int[] Result = new int[tmp[2]]; pos = tmp[1]; int OffSize = tmp[0]; for (int i = 0; i < Result.Length; i++) { Result[i] = PSBStrMan.ReadOffset(file, pos + (i * OffSize), OffSize); } return(Result); }
public FileEntry[] Import(byte[] script) { PSBStrMan.PackgetStatus Status = PSBStrMan.GetPackgetStatus(script); if (Status == PSBStrMan.PackgetStatus.MDF) { script = PSBStrMan.ExtractMDF(script); } Status = PSBStrMan.GetPackgetStatus(script); if (Status != PSBStrMan.PackgetStatus.PSB) { throw new Exception("Bad File Format"); } packget = script; StartPos = PSBStrMan.ReadOffset(packget, 0x20, 4); OffsetPos = PSBStrMan.ReadOffset(packget, 0x18, 4); int[] tmp = GetOffsetInfo(packget, OffsetPos); OffsetSize = tmp[0]; OffsetTablePos = tmp[1]; ResSizePos = PSBStrMan.ReadOffset(packget, 0x1C, 4); tmp = GetOffsetInfo(packget, ResSizePos); ResSizeOffSize = tmp[0]; ResSizeOffTablePos = tmp[1]; int[] Offsets = GetValues(packget, OffsetPos); int[] Sizes = GetValues(packget, ResSizePos); EntryCount = Offsets.Length; FileEntry[] Files = new FileEntry[EntryCount]; for (int i = 0; i < EntryCount; i++) { int EndPos = Offsets[i] + Sizes[i]; byte[] data = new byte[Sizes[i]]; for (int ind = Offsets[i]; ind < EndPos; ind++) { data[ind - Offsets[i]] = packget[ind + StartPos]; } Files[i] = new FileEntry() { Data = data }; } Initialized = true; return(Files); }