summaryrefslogtreecommitdiff
path: root/ConsoleApp/TextureCommands.cs
blob: 3fad0c7d5750db048b4f57cf3ef48c98df4d294d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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<string>();

			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 <brres-filename> <dest-path> [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.";
		}
	}
}