Skip to content

Instantly share code, notes, and snippets.

@Traderain
Created April 21, 2017 21:57
Show Gist options
  • Save Traderain/8622e277882b8cef0458e6cdd3a330b7 to your computer and use it in GitHub Desktop.
Save Traderain/8622e277882b8cef0458e6cdd3a330b7 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Cache
{
public class CollisionCache
{
public byte[] IdString = {(byte) 'C', (byte) 'C', (byte) '3', (byte) 'W'};
public uint Version;
public uint Unknown1;
public uint Unknown2;
public uint FileNameTableEndOffset;
public uint NumberOfFiles;
public uint FileNameTableStartOffset;
public List<string> FileNames;
public CollisionCache(string Filename)
{
this.Read(new BinaryReader(new FileStream(Filename,FileMode.Open)));
}
public void ShowDetails()
{
#region MessageBox
MessageBox.Show($"\r\nIDString:\t\t\t\t\t{new string(Encoding.ASCII.GetChars(this.IdString))}\r\nVersion:\t\t\t\t\t{this.Version}\r\nUnknown1:\t\t\t\t{this.Unknown1}\r\nUnknown2:\t\t\t\t{this.Unknown2}\r\nFilename table start:\t\t\t\t{this.FileNameTableStartOffset}\r\nFilename table end:\t\t\t\t{this.FileNameTableEndOffset}\r\nNumber of files:\t\t\t\t{this.NumberOfFiles}\r\n------------------------------------------------------------------\r\nFiles:\r\n" + FileNames.Aggregate("",(c,n) => c+= "\n" + n),
"Collision.cache info",
MessageBoxButtons.OK,MessageBoxIcon.Information);
#endregion
}
public void Read(BinaryReader br)
{
if(!br.ReadBytes(4).SequenceEqual(IdString))
throw new Exception("Invalid file!");
this.Version = br.ReadUInt32();
this.Unknown1 = br.ReadUInt32();
this.Unknown2 = br.ReadUInt32();
this.FileNameTableEndOffset = br.ReadUInt32();
this.NumberOfFiles = br.ReadUInt32();
this.FileNameTableStartOffset = br.ReadUInt32();
FileNames = new List<string>();
br.BaseStream.Seek(this.FileNameTableStartOffset, SeekOrigin.Begin);
for (int i = 0; i < this.NumberOfFiles; i++)
{
this.FileNames.Add(br.ReadCR2WString());
}
}
}
class Program
{
[STAThread]
static void Main(string[] args)
{
Console.Title = "Collision.cache reader test";
var of = new OpenFileDialog();
of.Filter = "Cache files | *.cache";
of.Title = "Please select a witcher 3 cache file";
if (of.ShowDialog() == DialogResult.OK)
{
var cc = new CollisionCache(of.FileName);
cc.ShowDetails();
}
Console.ReadLine();
}
}
public static class BREXT
{
public static string ReadCR2WString(this BinaryReader file, int len = 0)
{
string str = null;
if (len > 0)
{
str = Encoding.Default.GetString(file.ReadBytes(len));
}
else
{
var sb = new StringBuilder();
while (true)
{
var c = (char)file.ReadByte();
if (c == 0)
break;
sb.Append(c);
}
str = sb.ToString();
}
return str;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment