Created
January 13, 2017 15:43
-
-
Save marcussacana/723edd2d54889513f564976d13a3e8e8 to your computer and use it in GitHub Desktop.
Criminal Girls Extract/Repacker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
namespace CriminalGirlsLib { | |
public static class PS3FileSystem { | |
public static File[] Open(Stream Packget) { | |
BinaryReader Reader = new BinaryReader(Packget); | |
List<File> Files = new List<File>(); | |
if (Reader.ReadInt64() != 0x31565F5346335350) | |
throw new Exception("Unknowk format"); | |
long Count = Reader.ReadInt64(); | |
for (long i = 0; i < Count; i++) { | |
byte[] Buffer = new byte[0x30]; | |
Reader.Read(Buffer, 0, Buffer.Length); | |
int Len = 0; | |
while (Buffer[Len] != 0x00) | |
Len++; | |
string Name = Encoding.UTF8.GetString(Buffer, 0, Len); | |
long Length = Reader.ReadInt64(); | |
long Offset = Reader.ReadInt64(); | |
Files.Add(new File() { | |
Path = Name, | |
Content = new VirtStream(Packget, Offset, Length) | |
}); | |
} | |
return Files.ToArray(); | |
} | |
public static void Create(File[] Files, ref Stream Output, bool CloseStreams = true) { | |
BinaryWriter Writer = new BinaryWriter(Output); | |
//Generate Header | |
Writer.Write(0x31565F5346335350); | |
Writer.Write(Files.LongLength); | |
long DataSize = (Files.LongLength * 0x40) + 0x10; | |
for (long i = 0; i < Files.LongLength; i++) { | |
byte[] Buffer = new byte[0x30]; | |
Encoding.UTF8.GetBytes(Files[i].Path).CopyTo(Buffer, 0); | |
Writer.Write(Buffer); | |
long Len = Files[i].Content.Length; | |
Writer.Write(Len); | |
Writer.Write(DataSize); | |
DataSize += Len; | |
} | |
for (long i = 0; i < Files.LongLength; i++) { | |
Stream In = Files[i].Content; | |
int Len = 0; | |
byte[] Buffer = new byte[1024 * 1024]; | |
do { | |
Len = In.Read(Buffer, 0, Buffer.Length); | |
Writer.Write(Buffer, 0, Len); | |
} while (Len > 0); | |
if (CloseStreams) | |
In.Close(); | |
} | |
if (CloseStreams) | |
Writer.Close(); | |
} | |
} | |
public struct File { | |
public string Path; | |
public Stream Content; | |
} | |
public class VirtStream : Stream { | |
private Stream Packget; | |
private long FilePos = 0; | |
private long Len; | |
internal VirtStream(Stream Packget, long Pos, long Len) { | |
this.Packget = Packget; | |
FilePos = Pos; | |
this.Len = Len; | |
} | |
public override bool CanRead { | |
get { | |
return true; | |
} | |
} | |
public override bool CanSeek { | |
get { | |
return false; | |
} | |
} | |
public override bool CanWrite { | |
get { | |
return false; | |
} | |
} | |
public override long Length { | |
get { | |
return Len; | |
} | |
} | |
internal long Pos = 0; | |
public override long Position { | |
get { | |
return Pos; | |
} | |
set { | |
Seek(value, SeekOrigin.Begin); | |
} | |
} | |
public override void Flush() { | |
throw new NotImplementedException(); | |
} | |
public override int Read(byte[] buffer, int offset, int count) { | |
long ReadPos = FilePos + Pos; | |
if (ReadPos != Packget.Position) | |
Packget.Position = ReadPos; | |
if (Pos + count > Length) | |
count = (int)(Length - Pos); | |
int Readed = Packget.Read(buffer, offset, count); | |
Pos += Readed; | |
return Readed; | |
} | |
/// <summary> | |
/// Seek the file another location | |
/// </summary> | |
/// <param name="offset">Value to change the pointer location</param> | |
/// <param name="origin">Change from</param> | |
/// <returns>New Position</returns> | |
public override long Seek(long offset, SeekOrigin origin) { | |
if (offset < 0 || offset > Length) | |
throw new Exception("Invalid Position"); | |
switch (origin) { | |
case SeekOrigin.Begin: | |
Packget.Position = FilePos + offset; | |
this.Pos = offset; | |
break; | |
case SeekOrigin.Current: | |
if (Position + offset > Length) | |
throw new Exception("Out of Range"); | |
Packget.Position += offset; | |
this.Pos += offset; | |
break; | |
case SeekOrigin.End: | |
long Pos = Length - offset; | |
this.Pos = Pos; | |
long FP = FilePos + Pos; | |
if (Pos < 0) | |
throw new Exception("Out of Range"); | |
Packget.Position = FP; | |
break; | |
} | |
return Pos; | |
} | |
public override void SetLength(long value) { | |
throw new NotImplementedException(); | |
} | |
public override void Write(byte[] buffer, int offset, int count) { | |
throw new NotImplementedException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment