public static Bundle Create(byte[] binary, bool fast = false) { Bundle res = new Bundle(); res.iscas = false; res.ebx = new List <ebxtype>(); res.res = new List <restype>(); res.chunk = new List <chunktype>(); BinaryBundle bin = new BinaryBundle(new MemoryStream(binary), fast); if (bin.EbxList != null) { foreach (BinaryBundle.EbxEntry ebx in bin.EbxList) { ebxtype e = new ebxtype(); e.name = ebx._name; e.originalSize = BitConverter.GetBytes((long)ebx.ucsize); e.Sha1 = ebx._sha1; res.ebx.Add(e); } } if (bin.ResList != null) { foreach (BinaryBundle.ResEntry r in bin.ResList) { restype e = new restype(); e.name = r._name; e.osize = BitConverter.GetBytes((long)r.ucsize); e.SHA1 = r._sha1; e.rtype = BitConverter.GetBytes(r.type); res.res.Add(e); } } if (bin.ChunkList != null) { foreach (BinaryBundle.ChunkEntry c in bin.ChunkList) { chunktype e = new chunktype(); e.size = BitConverter.GetBytes((long)c._originalSize); e.SHA1 = c._sha1; e.id = c.id; res.chunk.Add(e); } } return(res); }
public static Bundle Create(byte[] binary, bool fast = false) { Bundle res = new Bundle(); res.iscas = false; res.ebx = new List<ebxtype>(); res.res = new List<restype>(); res.chunk = new List<chunktype>(); BinaryBundle bin = new BinaryBundle(new MemoryStream(binary), fast); if(bin.EbxList != null) foreach (BinaryBundle.EbxEntry ebx in bin.EbxList) { ebxtype e = new ebxtype(); e.name = ebx._name; e.originalSize = BitConverter.GetBytes((long)ebx.ucsize); e.Sha1 = ebx._sha1; res.ebx.Add(e); } if (bin.ResList != null) foreach (BinaryBundle.ResEntry r in bin.ResList) { restype e = new restype(); e.name = r._name; e.osize = BitConverter.GetBytes((long)r.ucsize); e.SHA1 = r._sha1; e.rtype = BitConverter.GetBytes(r.type); res.res.Add(e); } if (bin.ChunkList!= null) foreach (BinaryBundle.ChunkEntry c in bin.ChunkList) { chunktype e = new chunktype(); e.size = BitConverter.GetBytes((long)c._originalSize); e.SHA1 = c._sha1; e.id = c.id; res.chunk.Add(e); } return res; }
private void RefreshPreview() { try { int n = listBox2.SelectedIndex; if (n == -1) return; status.Text = "Getting header infos from db..."; Application.DoEvents(); DBAccess.RESInformation ti = ttprevlist[n]; DBAccess.BundleInformation buni = DBAccess.GetBundleInformationById(ti.bundlepath)[0]; DBAccess.TOCInformation toci = DBAccess.GetTocInformationByIndex(buni.tocIndex); BinaryBundle b = new BinaryBundle(); byte[] resdata = new byte[0]; if (toci.incas) { status.Text = "Getting header data from sha1..."; Application.DoEvents(); resdata = SHA1Access.GetDataBySha1(Helpers.HexStringToByteArray(ti.sha1)); } else { status.Text = "Getting header data from binary bundle..."; Application.DoEvents(); TOCFile toc = new TOCFile(toci.path); byte[] bundledata = toc.ExportBundleDataByPath(buni.bundlepath); b = new BinaryBundle(new MemoryStream(bundledata)); foreach (BinaryBundle.ResEntry res in b.ResList) if (res._name == ti.resname) { resdata = res._data; break; } } hb2.ByteProvider = new DynamicByteProvider(resdata); Mesh mesh = new Mesh(new MemoryStream(resdata)); foreach (Mesh.MeshLOD lod in mesh.header.LODs) { byte[] id = lod.ChunkID; byte[] data = new byte[0]; if (toci.incas) { DBAccess.ChunkInformation ci = DBAccess.GetChunkInformationById(id); if (ci.sha1 == null) continue; data = SHA1Access.GetDataBySha1(ci.sha1); } else { byte t = id[0]; id[0] = id[3]; id[3] = t; t = id[1]; id[1] = id[2]; id[2] = t; t = id[6]; id[6] = id[7]; id[7] = t; t = id[4]; id[4] = id[5]; id[5] = t; foreach (BinaryBundle.ChunkEntry c in b.ChunkList) if (Helpers.ByteArrayCompare(id, c.id)) data = c._data; if (data.Length == 0) { DBAccess.ChunkInformation ci = DBAccess.GetChunkInformationById(id); if (ci.sha1 == null) continue; data = SHA1Access.GetDataBySha1(ci.sha1); } } mesh.LoadChunkData(lod, new MemoryStream(data)); } MeshRenderObject mro = new MeshRenderObject(mesh); renderer.list.Clear(); renderer.list.Add(mro); renderer.worldoffset = -mro.center; renderer.CamDistance = mro.min.Length() + mro.max.Length(); status.Text = "Ready"; } catch (Exception ex) { status.Text = "General error, after state '" + status.Text + "' : " + ex.Message; } }
private void RefreshPreview() { try { int n = listBox2.SelectedIndex; if (n == -1) return; statustextures.Text = "Getting header infos from db..."; Application.DoEvents(); hb3.ByteProvider = new DynamicByteProvider(new byte[0]); hb3.BringToFront(); if (!Directory.Exists("tmp")) Directory.CreateDirectory("tmp"); if (File.Exists("tmp\\tmp.dds")) File.Delete("tmp\\tmp.dds"); DBAccess.TextureInformation ti = ttprevlist[n]; DBAccess.BundleInformation buni = DBAccess.GetBundleInformationByIndex(ti.bundleIndex); DBAccess.TOCInformation toci = DBAccess.GetTocInformationByIndex(buni.tocIndex); byte[] resdata = new byte[0]; if (toci.incas) { statustextures.Text = "Getting header data from sha1..."; Application.DoEvents(); resdata = SHA1Access.GetDataBySha1(ti.sha1); } else { statustextures.Text = "Getting header data from binary bundle..."; Application.DoEvents(); TOCFile toc = new TOCFile(toci.path); byte[] bundledata = toc.ExportBundleDataByPath(buni.bundlepath); BinaryBundle b = new BinaryBundle(new MemoryStream(bundledata)); foreach (BinaryBundle.ResEntry res in b.ResList) if (res._name == ti.name) { resdata = res._data; break; } } hb2.ByteProvider = new DynamicByteProvider(resdata); statustextures.Text = "Getting texture infos from db..."; Application.DoEvents(); TextureMetaResource tmr = new TextureMetaResource(resdata); DBAccess.ChunkInformation ci = DBAccess.GetChunkInformationById(tmr.chunkid); if (ci.bundleIndex == -1) throw new Exception("no chunk info found in db"); DBAccess.BundleInformation buni2 = DBAccess.GetBundleInformationByIndex(ci.bundleIndex); DBAccess.TOCInformation toci2 = DBAccess.GetTocInformationByIndex(buni2.tocIndex); byte[] texdata = new byte[0]; if (toci2.incas) { statustextures.Text = "Getting texture data from sha1..."; Application.DoEvents(); texdata = SHA1Access.GetDataBySha1(ci.sha1); } else { statustextures.Text = "Getting texture data from binary bundle..."; Application.DoEvents(); TOCFile toc = new TOCFile(toci2.path); byte[] bundledata = toc.ExportBundleDataByPath(buni2.bundlepath); BinaryBundle b = new BinaryBundle(new MemoryStream(bundledata)); foreach (BinaryBundle.ChunkEntry chunk in b.ChunkList) if (Helpers.MatchByteArray(chunk.id, ci.id)) { texdata = chunk._data; break; } } if (toolStripButton16.Checked) { hb3.ByteProvider = new DynamicByteProvider(texdata); hb3.BringToFront(); } else { statustextures.Text = "Making Preview..."; Application.DoEvents(); MemoryStream m = new MemoryStream(); tmr.WriteTextureHeader(m); m.Write(texdata, 0, texdata.Length); File.WriteAllBytes("tmp\\tmp.dds", m.ToArray()); try { pb1.Image = DevIL.DevIL.LoadBitmap("tmp\\tmp.dds"); pb1.BringToFront(); } catch (Exception) { statustextures.Text = "Error loading dds, after state '" + statustextures.Text + "'"; hb3.ByteProvider = new DynamicByteProvider(texdata); hb3.BringToFront(); } } statustextures.Text = "Ready"; } catch (Exception) { statustextures.Text = "General error, after state '" + statustextures.Text + "'"; } }
private void RefreshPreview() { try { int n = listBox2.SelectedIndex; if (n == -1) return; status.Text = "Getting header infos from db..."; Application.DoEvents(); DBAccess.RESInformation ti = ttprevlist[n]; DBAccess.BundleInformation buni = DBAccess.GetBundleInformationById(ti.bundlepath)[0]; DBAccess.TOCInformation toci = DBAccess.GetTocInformationByIndex(buni.tocIndex); byte[] resdata = new byte[0]; if (toci.incas) { status.Text = "Getting header data from sha1..."; Application.DoEvents(); resdata = SHA1Access.GetDataBySha1(Helpers.HexStringToByteArray(ti.sha1)); } else { status.Text = "Getting header data from binary bundle..."; Application.DoEvents(); TOCFile toc = new TOCFile(toci.path); byte[] bundledata = toc.ExportBundleDataByPath(buni.bundlepath); BinaryBundle b = new BinaryBundle(new MemoryStream(bundledata)); foreach (BinaryBundle.ResEntry res in b.ResList) if (res._name == ti.resname) { resdata = res._data; break; } } hb2.ByteProvider = new DynamicByteProvider(resdata); status.Text = "Ready"; } catch (Exception ex) { status.Text = "General error, after state '" + status.Text + "' : " + ex.Message; } }
public static void AddBundle(int tocid, bool incas, Bundle b, TOCFile.TOCBundleInfoStruct info, SQLiteConnection con) { Debug.LogLn(" EBX:" + b.ebx.Count + " RES:" + b.res.Count + " CHUNK:" + b.chunk.Count, true); SQLCommand("INSERT INTO bundles (tocfile, frostid, offset, size, base, delta) VALUES (" + tocid + ",'" + info.id + "'," + info.offset + ", " + info.size + ", '" + info.isbase + "', '" + info.isdelta + "' )", con); int bundleid = (int)GetLastRowId(con); TOCInformation toci = GetTocInformationByIndex(tocid); var transaction = con.BeginTransaction(); int counter = 0; if (b.ebx != null) foreach (Bundle.ebxtype ebx in b.ebx) try { if (ebx.name != null && ebx.originalSize != null && ebx.size != null) { EBXInformation inf = new EBXInformation(); inf.basesha1 = Helpers.ByteArrayToHexString(ebx.baseSha1); inf.bundlepath = b.path; inf.casPatchType = ebx.casPatchType; inf.deltasha1 = Helpers.ByteArrayToHexString(ebx.deltaSha1); inf.ebxname = ebx.name; inf.incas = incas; inf.isbase = info.isbase; inf.isdelta = info.isdelta; if (toci.type == TYPE_BASEGAME) inf.isbasegamefile = true; if (toci.type == TYPE_UPDATE) inf.isDLC = true; if (toci.type == TYPE_PATCH) inf.isPatch = true; inf.offset = info.offset; inf.sha1 = Helpers.ByteArrayToHexString(ebx.Sha1); inf.size = info.size; inf.tocfilepath = toci.path; byte[] data = new byte[0]; if (inf.incas) data = SHA1Access.GetDataBySha1(ebx.Sha1, 0x38); else { BinaryBundle bb = null; foreach (AddEBXHelpStruct h in aehelp) if (h.tocpath == inf.tocfilepath && h.bpath == inf.bundlepath) { bb = h.b; break; } if (bb == null) { TOCFile toc = new TOCFile(inf.tocfilepath); byte[] bundledata = toc.ExportBundleDataByPath(inf.bundlepath); bb = new BinaryBundle(new MemoryStream(bundledata)); AddEBXHelpStruct h = new AddEBXHelpStruct(); h.tocpath = inf.tocfilepath; h.bpath = inf.bundlepath; h.b = bb; if (aehelp.Count > 10) aehelp.RemoveAt(0); } foreach (BinaryBundle.EbxEntry ebx2 in bb.EbxList) if (inf.ebxname == ebx2._name) data = ebx2._data; } inf.guid = Helpers.ByteArrayToHexString(data, 0x28, 0x10); AddEBXLUTFile(inf, con); if ((counter++) % 100 == 0) { transaction.Commit(); transaction = con.BeginTransaction(); } } } catch (Exception ex) { throw ex; } transaction.Commit(); transaction = con.BeginTransaction(); if (b.res != null) foreach (Bundle.restype res in b.res) try { if (res.name != null) AddRESFile(res.name, res.SHA1, res.rtype, bundleid, con); } catch (Exception ex) { throw ex; } transaction.Commit(); transaction = con.BeginTransaction(); if (b.chunk != null) foreach (Bundle.chunktype chunk in b.chunk) try { AddChunk(chunk.id, chunk.SHA1, bundleid, con); } catch (Exception ex) { throw ex; } transaction.Commit(); }
public static void AddEBXLUTFile(EBXInformation ebx, SQLiteConnection con) { string ftype = "b"; if (ebx.isDLC) ftype = "u"; if (ebx.isPatch) ftype = "p"; string guid = ""; byte[] data = new byte[0]; if (ebx.incas) data = SHA1Access.GetDataBySha1(Helpers.HexStringToByteArray(ebx.sha1)); else { BinaryBundle b = null; foreach(AddEBXHelpStruct h in aehelp) if (h.tocpath == ebx.tocfilepath && h.bpath == ebx.bundlepath) { b = h.b; break; } if (b == null) { TOCFile toc = new TOCFile(ebx.tocfilepath); byte[] bundledata = toc.ExportBundleDataByPath(ebx.bundlepath); b = new BinaryBundle(new MemoryStream(bundledata)); AddEBXHelpStruct h = new AddEBXHelpStruct(); h.tocpath = ebx.tocfilepath; h.bpath = ebx.bundlepath; h.b = b; if (aehelp.Count > 10) aehelp.RemoveAt(0); } foreach (BinaryBundle.EbxEntry ebx2 in b.EbxList) if (ebx.ebxname == ebx2._name) data = ebx2._data; } guid = Helpers.ByteArrayToHexString(data, 0x28, 0x10); SQLCommand("INSERT INTO ebxlut (path,sha1,basesha1,deltasha1,casptype,guid,bundlepath,offset,size,isbase,isdelta,tocpath,incas,filetype) VALUES ('" + ebx.ebxname.Replace("'","''") + "','" + ebx.sha1 + "','" + ebx.basesha1 + "','" + ebx.deltasha1 + "'," + ebx.casPatchType + ",'" + guid + "','" + ebx.bundlepath + "'," + ebx.offset + "," + ebx.size + ",'" + ebx.isbase + "','" + ebx.isdelta + "','" + ebx.tocfilepath + "','" + ebx.incas + "','" + ftype + "')", con); }
public void RunTextureResJobOnBundleBinary(Mod.ModJob mj, TOCFile toc, string tocpath, string bpath) { GC.Collect(); int count = 0; int index = -1; foreach (TOCFile.TOCBundleInfoStruct buni in toc.bundles) if (count++ > -1 && bpath.ToLower() == buni.id.ToLower()) { DbgPrint(" Found bundle : " + bpath); index = count - 1; break; } //if bundle found if (index != -1) { //find out if base, delta or nothing BJSON.Entry root = toc.lines[0]; BJSON.Field bundles = root.FindField("bundles"); BJSON.Entry bun = ((List<BJSON.Entry>)bundles.data)[index]; BJSON.Field isDeltaField = bun.FindField("delta"); BJSON.Field isBaseField = bun.FindField("base"); //if has base or delta prop, still from patch if (isBaseField != null) if (!ImportBundleBinaryFromBase(toc, tocpath, bpath)) return; toc = new TOCFile(toc.MyPath);//reload toc byte[] bundledataraw = toc.ExportBundleDataByPath(bpath); uint test = BitConverter.ToUInt32(bundledataraw, 4); if (test != 0xD58E799D) { DbgPrint(" Its a real delta bundle, importing from base..."); if (!ImportBundleBinaryFromBase(toc, tocpath, bpath, false)) return; toc.Save(); toc = new TOCFile(toc.MyPath);//reload toc } DbgPrint(" Updating SB file with new data..."); bundledataraw = toc.ExportBundleDataByPath(bpath); BinaryBundle bundle = new BinaryBundle(new MemoryStream(bundledataraw)); bool found = false; byte[] chunkidbuff = new byte[16]; byte[] newchunkid = new byte[16]; //find right res entry for (int j = 0; j < bundle.ResList.Count; j++) if (bundle.ResList[j]._name.ToLower() == mj.respath.ToLower()) { //get res data and extract chunk id for (int k = 0; k < 16; k++) chunkidbuff[k] = bundle.ResList[j]._data[k + 0x1C]; DbgPrint(" Found chunk id : " + Helpers.ByteArrayToHexString(chunkidbuff)); newchunkid = Guid.NewGuid().ToByteArray(); DbgPrint(" Creating new chunk id : " + Helpers.ByteArrayToHexString(newchunkid)); for (int k = 0; k < 16; k++) bundle.ResList[j]._data[k + 0x1C] = newchunkid[k]; found = true; } if (!found) { DbgPrint(" Error: cant find res, skipping!"); return; } found = false; //find right chunk entry MemoryStream m3 = new MemoryStream(); Helpers.WriteLEInt(m3, BitConverter.ToInt32(chunkidbuff, 0)); Helpers.WriteLEUShort(m3, BitConverter.ToUInt16(chunkidbuff, 4)); Helpers.WriteLEUShort(m3, BitConverter.ToUInt16(chunkidbuff, 6)); m3.Write(chunkidbuff, 8, 8); byte[] chunkidswapped = m3.ToArray(); for (int j = 0; j < bundle.ChunkList.Count; j++) if (Helpers.ByteArrayCompare(bundle.ChunkList[j].id, chunkidswapped)) { DbgPrint(" Found chunk"); found = true; BinaryBundle.ChunkEntry chunk = bundle.ChunkList[j]; m3 = new MemoryStream(); Helpers.WriteLEInt(m3, BitConverter.ToInt32(newchunkid, 0)); Helpers.WriteLEUShort(m3, BitConverter.ToUInt16(newchunkid, 4)); Helpers.WriteLEUShort(m3, BitConverter.ToUInt16(newchunkid, 6)); m3.Write(newchunkid, 8, 8); chunk.id = m3.ToArray(); chunk._data = mj.data; bundle.ChunkList[j] = chunk; break; } if (!found) { DbgPrint(" Error: Could not find Chunk by id"); return; } DbgPrint(" Recompiling bundle..."); MemoryStream m = new MemoryStream(); bundle.Save(m); DbgPrint(" Recompiling sb..."); MemoryStream m2 = new MemoryStream(); List<BJSON.Entry> list = ((List<BJSON.Entry>)bundles.data); foreach (TOCFile.TOCBundleInfoStruct buni in toc.bundles) if (!buni.isbase) { byte[] buff = new byte[0]; if (bpath.ToLower() != buni.id.ToLower()) buff = toc.ExportBundleDataByPath(buni.id); else buff = m.ToArray(); for (int i = 0; i < list.Count; i++) if ((string)list[i].FindField("id").data == buni.id) { BJSON.Entry e = list[i]; BJSON.Field f = e.fields[e.FindFieldIndex("offset")]; f.data = BitConverter.GetBytes(m2.Position); e.fields[e.FindFieldIndex("offset")] = f; f = e.fields[e.FindFieldIndex("size")]; f.data = BitConverter.GetBytes(buff.Length); e.fields[e.FindFieldIndex("size")] = f; list[i] = e; break; } m2.Write(buff, 0, buff.Length); } bundles.data = list; root.fields[root.FindFieldIndex("bundles")] = bundles; toc.lines[0] = root; DbgPrint(" Updating TOC..."); toc.Save(); toc = new TOCFile(toc.MyPath);//reload toc DbgPrint(" Saving sb..."); if (tocpath.ToLower().Contains("update")) File.WriteAllBytes(outputPath + Helpers.SkipSubFolder(tocpath.ToLower().Replace(".toc", ".sb"), 2), m2.ToArray()); else File.WriteAllBytes(outputPath + tocpath.ToLower().Replace(".toc", ".sb"), m2.ToArray()); DbgPrint(" Job successfull!"); } }
private void toolStripMenuItem3_Click(object sender, EventArgs e) { OpenFileDialog d = new OpenFileDialog(); d.Filter = "*.bundle|*.bundle"; if (d.ShowDialog() == System.Windows.Forms.DialogResult.OK) { toc = null; toolStrip1.Visible = true; splitContainer2.BringToFront(); MemoryStream m = new MemoryStream(File.ReadAllBytes(d.FileName)); binBundle = new BinaryBundle(m); tabControl1.BringToFront(); RefreshBinary(); RefreshBinaryBundle(); return; } }
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { if (toc == null) return; int n = listBox1.SelectedIndex; if (n == -1) return; TOCFile.TOCBundleInfoStruct info = toc.bundles[n]; if (info.isbase) return; byte[] data = toc.ExportBinaryBundle(info); binBundle = new BinaryBundle(new MemoryStream(data)); RefreshBinaryBundle(); }