summaryrefslogtreecommitdiff
path: root/ConsoleApp/TextureCommands.cs
blob: 834b32e929978bc15f0e8d3090945d6332e7676e (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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.";
		}
	}


	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 <brres-filename> <texture-name> <image-filename>\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).";
		}
	}
}