byte[] ReadEntry(CatEntry e) { _datStream.Seek(e.OrigStreamPos, SeekOrigin.Begin); var data = new byte[e.OrigSize]; _datStream.Read(data, 0, (int)e.OrigSize); return(data); }
private bool DoReduceDDS(CatEntry e, TextureEntry.TECategory cat, long pos, int levels, Res res) { var oldData = new byte[e.OrigSize]; _datStream.Seek(pos, SeekOrigin.Begin); _datStream.Read(oldData, 0, oldData.Length); bool replaced = false; var h = new GCHandle(); try { if (e.Compressed) { oldData = Decompress(oldData); } h = GCHandle.Alloc(oldData, GCHandleType.Pinned); var img = DirectXTexNet.TexHelper.Instance.LoadFromDDSMemory( h.AddrOfPinnedObject(), oldData.Length, DirectXTexNet.DDS_FLAGS.NONE); var imgCount = img.GetImageCount(); if (imgCount > 1) { for (int i = 0; i < Math.Min(imgCount - 1, levels); ++i) { img.FreeFirstImage(); // TODO: replace loop } var ufs = img.SaveToDDSMemory(DirectXTexNet.DDS_FLAGS.NONE); var newData = new byte[ufs.Length]; ufs.Read(newData, 0, newData.Length); res.RemovedBytes += oldData.LongLength - newData.LongLength; ++res.Count; replaced = true; e.Data = e.Compressed ? Compress(newData) : newData; } } catch (Exception ex) { throw new IOException(string.Format("Category: {0}, Path: {1}", cat, e.Path), ex); } finally { if (h.IsAllocated) { h.Free(); } } return(replaced); }
static private string GetChecksum(CatEntry e) { using (var alg = System.Security.Cryptography.MD5.Create()) { byte[] hash = alg.ComputeHash(e.Data); var sb = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { sb.Append(hash[i].ToString("x2")); } return(sb.ToString()); } }
private List <CatEntry> ParseCat(List <string> lines) { var sep = new char[] { ' ' }; var list = new List <CatEntry>(lines.Count); long currentPos = 0; foreach (var l in lines) { var entry = new CatEntry(); var sl = l.Split(sep, StringSplitOptions.RemoveEmptyEntries); if (sl.Length != 4) { entry.ChkSum = sl[sl.Length - 1]; entry.TimeStamp = sl[sl.Length - 2]; entry.OrigSize = long.Parse(sl[sl.Length - 3]); // let exception propagate entry.Path = sl[0]; for (int i = 1; i < sl.Length - 3; ++i) { entry.Path += string.Format(" {0}", sl[i]); } } else { entry.Path = sl[0]; entry.OrigSize = long.Parse(sl[1]); // let exception propagate entry.TimeStamp = sl[2]; entry.ChkSum = sl[3]; } entry.OrigStreamPos = currentPos; entry.Compressed = entry.Path.EndsWith(".gz"); currentPos += entry.OrigSize; list.Add(entry); } return(list); }