summaryrefslogtreecommitdiff
path: root/ConsoleApp/ResFileCommands.cs
blob: ac734e4dd3209758c690037fa9091908b5b41bd0 (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
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, false);
			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";
		}
	}
}