summaryrefslogtreecommitdiff
path: root/ConsoleApp/ResFileCommands.cs
blob: 84ba13abc2cc6085af53d417e171f40465db8fdc (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
using System;
using System.Collections.Generic;
using NW4RTools;

namespace ConsoleApp {
	public abstract class ResFileCommand : Command {
		public ResFileCommand(string description) : base(description) {
		}

		public ResFile TargetFile { get; private set; }
		public string TargetPath { get; private set; }

		public override void Execute(string[] args) {
			if (args.Length == 0) {
				Console.WriteLine("No path entered");
				return;
			}

			// don't override this
			var newArgs = new string[args.Length - 1];

			for (int i = 1; i < args.Length; i++) {
				newArgs[i - 1] = args[i];
			}


			var data = System.IO.File.ReadAllBytes(args[0]);
			TargetFile = BrresReader.LoadFile(data, true);
			TargetPath = args[0];

			OperateOnFile(newArgs);
		}

		protected abstract void OperateOnFile(string[] args);

		protected void SaveFile() {
			var data = BrresWriter.WriteFile(TargetFile);
			System.IO.File.WriteAllBytes(TargetPath, data);
		}
	}


	public class InitCommand : Command {
		// this class doesn't inherit from ResFileCommand because it is a special case:
		// instead of operating on an existing file, it creates one

		public InitCommand() : base("create an empty .brres file") {
		}


		public override void Execute(string[] args) {
			if (args.Length == 0) {
				Console.WriteLine("No path entered");
				return;
			}

			var rf = new ResFile();
			System.IO.File.WriteAllBytes(args[0], BrresWriter.WriteFile(rf));
		}

		public override string GetHelp(string[] args) {
			return "syntax: init <filename> -- creates an empty .brres file";
		}
	}


	public class ListCommand : ResFileCommand {
		public ListCommand() : base("list the resources in a .brres file") {
		}

		protected override void OperateOnFile(string[] args) {
			foreach (var g in TargetFile) {
				Console.WriteLine("{0}", g.Key);

				// time to try hackishness
				var dict = g.Value as System.Collections.Specialized.IOrderedDictionary;

				foreach (var e in dict.Keys) {
					Console.WriteLine(" - {0}", e);
				}
			}
		}

		public override string GetHelp(string[] args) {
			return "syntax: list <filename> -- lists the contents of a .brres file";
		}
	}


	public class ListOffsetsCommand : Command {
		// this class also doesn't inherit from ResFileCommand, because I want to
		// keep the OffsetMap

		public ListOffsetsCommand() : base("list offsets of data within a .brres file") {
		}


		public override void Execute(string[] args) {
			if (args.Length == 0) {
				Console.WriteLine("No path entered");
				return;
			}

			SortedDictionary<int, string> offsetMap;
			var rf = BrresReader.LoadFile(System.IO.File.ReadAllBytes(args[0]), false, out offsetMap);

			foreach (var e in offsetMap) {
				Console.WriteLine("0x{0:X} : {1}", e.Key, e.Value);
			}
		}

		public override string GetHelp(string[] args) {
			return "syntax: init <filename> -- creates an empty .brres file";
		}
	}
}