using System; using System.Collections.Generic; using System.IO; using NW4RTools; namespace ConsoleApp { public class ExportTexturesCommand : ResFileCommand { public ExportTexturesCommand() : base("export texture(s) to PNG") { } protected override void OperateOnFile(string[] args) { if (args.Length < 1) { Console.WriteLine("invalid syntax.\n" + GetHelp(null)); return; } bool exportAll = (args.Length == 1); var list = new List(); for (int i = 1; i < args.Length; i++) { list.Add(args[i]); } var texDict = TargetFile.GetTextureGroup(); if (texDict == null || texDict.Count == 0) { Console.WriteLine("this file has no textures."); return; } int exportCount = 0; string currentDir = Directory.GetCurrentDirectory(); string imgDir = Path.Combine(currentDir, args[0]); // one last check if (!Directory.Exists(imgDir)) { Console.WriteLine("the destination directory does not exist:\n {0}", imgDir); return; } foreach (var kv in texDict) { if (exportAll || list.Contains(kv.Key)) { for (int i = 0; i < kv.Value.Images.Length; i++) { var img = kv.Value.Images[i]; string imgName; if (i == 0) { // first (possibly only?) mipmap imgName = String.Format("{0}.png", kv.Key); } else { imgName = String.Format("{0}__{1}.png", kv.Key, i); } Console.WriteLine("{0}[{4}] {1}x{2} {3}", kv.Key, img.Width, img.Height, kv.Value.Format, i); img.Save(Path.Combine(imgDir, imgName)); } exportCount++; } } Console.WriteLine("done! {0} textures exported", exportCount); } public override string GetHelp(string[] args) { return "syntax: export-textures [texture-names...]\n" + " exports texture(s) as .png files.\n\n" + " dest-path will be the folder in which the textures are placed (eg. images).\n" + " texture-names is a list of textures. if not specified, every texture is exported."; } } public class ReplaceImageCommand : ResFileCommand { public ReplaceImageCommand() : base("replace image used for a texture") { } protected override void OperateOnFile(string[] args) { if (args.Length != 2) { Console.WriteLine("invalid syntax.\n" + GetHelp(null)); return; } var texDict = TargetFile.GetTextureGroup(); if (texDict == null || texDict.Count == 0) { Console.WriteLine("this file has no textures."); return; } if (!texDict.ContainsKey(args[0])) { Console.WriteLine("the specified texture [{0}] does not exist", args[0]); return; } var texture = texDict[args[0]]; Console.WriteLine("texture {0}: {1} image(s)", args[0], texture.Images.Length); System.Drawing.Bitmap bitmap; try { bitmap = new System.Drawing.Bitmap(args[1]); } catch (Exception ex) { Console.WriteLine("error reading image [{0}]:", args[1]); Console.WriteLine(ex.ToString()); return; } texture.Images[0] = bitmap; Console.WriteLine("replaced"); SaveFile(); } public override string GetHelp(string[] args) { return "syntax: replace-image \n" + " replaces the image used by a texture.\n\n" + " texture-name is the affected texture.\n" + " image-filename is the image to replace it with (preferably a PNG)."; } } }