private static void OperationTex2Png() { string filename = ConsolePath("Full path to .tex file or partial path to txm/txv..?"); if (filename.IsEmpty()) { return; } if (Path.GetExtension(filename).ToLower() == ".txm" || Path.GetExtension(filename).ToLower() == ".txv") { filename = filename.Substring(0, filename.Length - 4); } DelayedStreamCache streams = new DelayedStreamCache(); Txm tex = OpenTex(filename, streams); string path = Path.Combine(Path.GetDirectoryName(filename), Path.GetFileName(filename) + ".ext"); Directory.CreateDirectory(path); for (int i = 0; i < tex.Images.Count; i++) { string file = Path.Combine(path, tex.Images[i].Name); tex.Images[i].PrimaryBitmap.Save(file + ".png", ImageFormat.Png); if (tex.Images[i].SecondaryData != null) { tex.Images[i].SecondaryData.Position = 0; FileStream secondary = new FileStream(file + ".dat", FileMode.Create, FileAccess.Write); Util.StreamCopy(secondary, tex.Images[i].SecondaryData); secondary.Close(); } } streams.Dispose(); }
private static Txm OpenTex(string filename, DelayedStreamCache streams) { Txm tex; if (File.Exists(filename)) { Stream stream = new CachedReadStream(new FileStream(filename, FileMode.Open, FileAccess.Read)); tex = Tex.Create(stream); streams.AddStream(stream); } else { Stream txmstream = new CachedReadStream(new FileStream(filename + ".txm", FileMode.Open, FileAccess.Read)); Stream txvstream = new CachedReadStream(new FileStream(filename + ".txv", FileMode.Open, FileAccess.Read)); tex = new Txm(txmstream, txvstream); streams.AddStream(txmstream); streams.AddStream(txvstream); } return(tex); }
private static void OperationPng2Tex() { string filename = ConsolePath("Full path to tex or txm/txv or png file..?"); if (filename.IsEmpty()) { return; } if (Path.GetExtension(filename).ToLower() == ".txm" || Path.GetExtension(filename).ToLower() == ".txv") { filename = filename.Substring(0, filename.Length - 4); } else if (Path.GetExtension(filename).ToLower() != ".tex") { filename = Path.GetDirectoryName(filename); if (filename.EndsWith(".ext")) { filename = filename.Substring(0, filename.Length - 4); } } DelayedStreamCache streams = new DelayedStreamCache(); Txm tex = OpenTex(filename, streams); Stream txmstream; Stream txvstream; if (File.Exists(filename)) { txmstream = new TemporaryStream(); txvstream = new TemporaryStream(); } else { txmstream = new FileStream(filename + ".txm.new", FileMode.Create); txvstream = new FileStream(filename + ".txv.new", FileMode.Create); } streams.AddStream(txmstream); streams.AddStream(txvstream); string[] files = Directory.GetFiles(filename + ".ext"); files = files.OrderBy(f => f).ToArray(); // Want to make sure the .dat files come before the pngs (hacky, but meh). foreach (string file in files) { string name = Path.GetFileName(file); string basename = Path.GetFileNameWithoutExtension(name); string extension = Path.GetExtension(name); Txm.Image image = tex.Images.FirstOrDefault(i => i.Name == basename); if (image == null) { if (extension != ".dat" && extension != ".png") { continue; } image = new Txm.Image(); image.Name = basename; image.PrimaryEncoding = PixelEncoding.GetEncoding <CI8>(); image.SecondaryData = new MemoryStream(); image.Unknown1 = 1; image.Unknown2 = 2 << 4; image.Unknown3 = 1; tex.Images.Add(image); } Stream stream = new FileStream(file, FileMode.Open); if (extension == ".dat") { image.SecondaryData = new MemoryStream((int)stream.Length); Util.StreamCopy(image.SecondaryData, stream); } else if (extension == ".png") { Bitmap bitmap = new Bitmap(stream); stream.Close(); stream = new MemoryStream(); if (image.SecondaryData is Substream || image.SecondaryData == null) { image.SecondaryData = new MemoryStream(); } image.SecondaryData.Position = 0; try { image.PrimaryEncoding.EncodeImage(stream, bitmap, image.SecondaryData, image.Unknown2 >> 4); } catch (NotImplementedException ex) { if (image.PrimaryEncoding.ID == 0xAAE4) { image.PrimaryEncoding = new ARGB(); image.Unknown1 = 5; image.Unknown2 = 0xa5; image.Unknown3 = 4; } else { image.PrimaryEncoding = PixelEncoding.GetEncoding <CI8>(); image.Unknown1 = 1; image.Unknown2 = 2 << 4; image.Unknown3 = 1; } try { image.PrimaryEncoding.EncodeImage(stream, bitmap, image.SecondaryData, image.Unknown2 >> 4); } catch { throw ex; } } image.Width = (uint)bitmap.Width; image.Height = (uint)bitmap.Height; image.PrimaryData = stream; } streams.AddStream(stream); } tex.Save(txmstream, txvstream); if (File.Exists(filename)) { Stream texstream = new FileStream(filename + ".new", FileMode.Create); streams.AddStream(texstream); FPS4Base fps = new FPS4Base(); fps.DataOffset = 0x60; fps.BlockSize = 0x0C; fps.Files = 3; fps.Flags = FPS4Base.NodeFlags.SectionSize | FPS4Base.NodeFlags.Filesize | FPS4Base.NodeFlags.Offset; fps.Type = new byte[] { 0x74, 0x78, 0x6D, 0x76 }; // "txmv" fps.TypeOffset = 0x40; uint txmlen = (uint)Util.RoundUp(txmstream.Length, 0x20); uint txvlen = (uint)Util.RoundUp(txvstream.Length, 0x20); fps.Nodes.Add(new FPS4Base.Node(0x60, txmlen, (uint)txmstream.Length, "", 0, 0, txmstream)); fps.Nodes.Add(new FPS4Base.Node(0x60 + txmlen, txvlen, (uint)txvstream.Length, "", 0, 0, txvstream)); fps.Nodes.Add(new FPS4Base.Node(0x60 + txmlen + txvlen, 0, 0, "", 0, 0, null)); fps.Save(texstream); } streams.Dispose(); }