Skip to content
This repository was archived by the owner on Nov 14, 2017. It is now read-only.

Commit f435511

Browse files
LHCGregLHCGreg
authored andcommitted
npkdiff, a command line program for diffing ImagePacks2 folders.
1 parent 5b99c58 commit f435511

File tree

12 files changed

+481
-0
lines changed

12 files changed

+481
-0
lines changed

DFOToolBox.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DFOToolbox", "DFOToolbox\DF
2121
EndProject
2222
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DFO.Npk.IntegrationTests", "DFO.Npk.IntegrationTests\DFO.Npk.IntegrationTests.csproj", "{24DD7BAA-7B3A-420E-9D99-B0ADD7F41C5E}"
2323
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "npkdiff", "npkdiff\npkdiff.csproj", "{C58E88D3-C68E-485B-9F37-92634216C182}"
25+
EndProject
2426
Global
2527
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2628
Debug|Any CPU = Debug|Any CPU
@@ -63,6 +65,10 @@ Global
6365
{24DD7BAA-7B3A-420E-9D99-B0ADD7F41C5E}.Debug|Any CPU.Build.0 = Debug|Any CPU
6466
{24DD7BAA-7B3A-420E-9D99-B0ADD7F41C5E}.Release|Any CPU.ActiveCfg = Release|Any CPU
6567
{24DD7BAA-7B3A-420E-9D99-B0ADD7F41C5E}.Release|Any CPU.Build.0 = Release|Any CPU
68+
{C58E88D3-C68E-485B-9F37-92634216C182}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
69+
{C58E88D3-C68E-485B-9F37-92634216C182}.Debug|Any CPU.Build.0 = Debug|Any CPU
70+
{C58E88D3-C68E-485B-9F37-92634216C182}.Release|Any CPU.ActiveCfg = Release|Any CPU
71+
{C58E88D3-C68E-485B-9F37-92634216C182}.Release|Any CPU.Build.0 = Release|Any CPU
6672
EndGlobalSection
6773
GlobalSection(SolutionProperties) = preSolution
6874
HideSolutionNode = FALSE

npkdiff/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5+
</startup>
6+
</configuration>

npkdiff/CommandLineArgs.cs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using NDesk.Options;
9+
10+
namespace DFO.npkdiff
11+
{
12+
class CommandLineArgs
13+
{
14+
private bool m_showHelp = false;
15+
public bool ShowHelp { get { return m_showHelp; } private set { m_showHelp = value; } }
16+
17+
private bool m_showVersion = false;
18+
public bool ShowVersion { get { return m_showVersion; } private set { m_showVersion = value; } }
19+
20+
public string NpkDir1 { get; private set; }
21+
public string NpkDir2 { get; private set; }
22+
23+
public CommandLineArgs(string[] args)
24+
{
25+
OptionSet optionSet = GetOptionSet();
26+
optionSet.Parse(args);
27+
28+
if (ShowHelp)
29+
{
30+
DisplayHelp(Console.Error);
31+
Environment.Exit(0);
32+
}
33+
34+
if (ShowVersion)
35+
{
36+
Console.WriteLine("{0} {1}", GetProgramNameWithoutExtension(), GetVersion());
37+
Environment.Exit(0);
38+
}
39+
40+
if (NpkDir1 == null || NpkDir2 == null)
41+
{
42+
DisplayHelp(Console.Error);
43+
Environment.Exit(1);
44+
}
45+
}
46+
47+
public OptionSet GetOptionSet()
48+
{
49+
OptionSet optionSet = new OptionSet()
50+
{
51+
{ "?|h|help", "Show this message and exit.", argExistence => ShowHelp = (argExistence != null) },
52+
{ "v|version", "Show version number and exit.", argExistence => ShowVersion = (argExistence != null) },
53+
{ "<>", AddDir }
54+
};
55+
56+
return optionSet;
57+
}
58+
59+
private void AddDir(string arg)
60+
{
61+
if (NpkDir1 == null)
62+
{
63+
NpkDir1 = arg;
64+
}
65+
else if (NpkDir2 == null)
66+
{
67+
NpkDir2 = arg;
68+
}
69+
else
70+
{
71+
throw new OptionException(string.Format("More than 2 directories specified. Run {0} -h for help.", GetProgramNameWithoutExtension()), "<>");
72+
}
73+
}
74+
75+
public void DisplayHelp(TextWriter writer)
76+
{
77+
writer.WriteLine("Usage: {0} IMAGEPACKDIRECTORY1 IMAGEPACKDIRECTORY2", GetProgramNameWithoutExtension());
78+
writer.WriteLine();
79+
writer.WriteLine("Parameters:");
80+
GetOptionSet().WriteOptionDescriptions(writer);
81+
}
82+
83+
public static string GetProgramNameWithoutExtension()
84+
{
85+
string[] argsWithProgramName = System.Environment.GetCommandLineArgs();
86+
string programName;
87+
if (argsWithProgramName[0].Equals(string.Empty))
88+
{
89+
// "If the file name is not available, the first element is equal to String.Empty."
90+
// Doesn't say why that would happen, but ok...
91+
programName = (new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name);
92+
}
93+
else
94+
{
95+
programName = Path.GetFileNameWithoutExtension(argsWithProgramName[0]);
96+
}
97+
98+
return programName;
99+
}
100+
101+
private static string GetVersion()
102+
{
103+
return typeof(CommandLineArgs).Assembly.GetName().Version.ToString();
104+
}
105+
}
106+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using DFO.Common;
7+
8+
namespace DFO.npkdiff
9+
{
10+
struct FrameCountDifferenceInfo
11+
{
12+
public int FrameCount1 { get; private set; }
13+
public string NpkFileName1 { get; private set; }
14+
public int FrameCount2 { get; private set; }
15+
public string NpkFileName2 { get; private set; }
16+
17+
public FrameCountDifferenceInfo(int frameCount1, string npkFileName1, int frameCount2, string npkFileName2)
18+
: this()
19+
{
20+
FrameCount1 = frameCount1;
21+
FrameCount2 = frameCount2;
22+
NpkFileName1 = npkFileName1;
23+
NpkFileName2 = npkFileName2;
24+
}
25+
}
26+
}

npkdiff/ImgInfo.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using DFO.Common;
7+
8+
namespace DFO.npkdiff
9+
{
10+
struct ImgInfo
11+
{
12+
public string NpkFileName { get; private set; }
13+
public NpkPath Path { get; private set; }
14+
public int FrameCount { get; private set; }
15+
16+
public ImgInfo(string npkFileName, NpkPath path, int frameCount)
17+
: this()
18+
{
19+
NpkFileName = npkFileName;
20+
Path = path;
21+
FrameCount = frameCount;
22+
}
23+
}
24+
}

npkdiff/NpkDirContents.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using DFO.Common;
7+
using DFO.Common.Images;
8+
9+
namespace DFO.npkdiff
10+
{
11+
class NpkDirContents
12+
{
13+
private Dictionary<NpkPath, ImgInfo> _imgFrames;
14+
15+
public NpkDirContents()
16+
{
17+
_imgFrames = new Dictionary<NpkPath, ImgInfo>();
18+
}
19+
20+
public NpkDirContents(string npkFilePath, IReadOnlyDictionary<NpkPath, IReadOnlyList<FrameInfo>> frames)
21+
{
22+
_imgFrames = new Dictionary<NpkPath, ImgInfo>(frames.Count);
23+
foreach (NpkPath npkPath in frames.Keys)
24+
{
25+
_imgFrames[npkPath] = new ImgInfo(npkFilePath, npkPath, frames[npkPath].Count);
26+
}
27+
}
28+
29+
public void Add(NpkDirContents other)
30+
{
31+
foreach (NpkPath npkPath in other._imgFrames.Keys)
32+
{
33+
if (!_imgFrames.ContainsKey(npkPath))
34+
{
35+
_imgFrames[npkPath] = other._imgFrames[npkPath];
36+
}
37+
}
38+
}
39+
40+
public NpkDirDifferences GetDifferences(NpkDirContents other)
41+
{
42+
if (other == null) throw new ArgumentNullException("other");
43+
44+
List<ImgInfo> imgsInFirstButNotSecond = new List<ImgInfo>();
45+
List<ImgInfo> imgsInSecondButNotFirst = new List<ImgInfo>();
46+
Dictionary<NpkPath, FrameCountDifferenceInfo> imgsWithFewerFramcesInSecond = new Dictionary<NpkPath, FrameCountDifferenceInfo>();
47+
Dictionary<NpkPath, FrameCountDifferenceInfo> imgsWithMoreFramesInSecond = new Dictionary<NpkPath, FrameCountDifferenceInfo>();
48+
49+
foreach (NpkPath npkPath in this._imgFrames.Keys)
50+
{
51+
if (!other._imgFrames.ContainsKey(npkPath))
52+
{
53+
imgsInFirstButNotSecond.Add(this._imgFrames[npkPath]);
54+
}
55+
else
56+
{
57+
int thisFrameCount = this._imgFrames[npkPath].FrameCount;
58+
string thisNpkFilePath = this._imgFrames[npkPath].NpkFileName;
59+
int otherFrameCount = other._imgFrames[npkPath].FrameCount;
60+
string otherNpkFilePath = other._imgFrames[npkPath].NpkFileName;
61+
if (thisFrameCount > otherFrameCount)
62+
{
63+
imgsWithFewerFramcesInSecond[npkPath] = new FrameCountDifferenceInfo(thisFrameCount, thisNpkFilePath, otherFrameCount, otherNpkFilePath);
64+
}
65+
else if (thisFrameCount < otherFrameCount)
66+
{
67+
imgsWithMoreFramesInSecond[npkPath] = new FrameCountDifferenceInfo(thisFrameCount, thisNpkFilePath, otherFrameCount, otherNpkFilePath);
68+
}
69+
}
70+
}
71+
72+
foreach (NpkPath npkPath in other._imgFrames.Keys)
73+
{
74+
if (!this._imgFrames.ContainsKey(npkPath))
75+
{
76+
imgsInSecondButNotFirst.Add(other._imgFrames[npkPath]);
77+
}
78+
}
79+
80+
return new NpkDirDifferences(imgsInFirstButNotSecond, imgsWithFewerFramcesInSecond, imgsInSecondButNotFirst, imgsWithMoreFramesInSecond);
81+
}
82+
}
83+
}

npkdiff/NpkDirDifferences.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using DFO.Common;
7+
8+
namespace DFO.npkdiff
9+
{
10+
class NpkDirDifferences
11+
{
12+
public IReadOnlyList<ImgInfo> ImgsInFirstButNotSecond { get; private set; }
13+
public IReadOnlyDictionary<NpkPath, FrameCountDifferenceInfo> ImgsWithFewerFramesInSecond { get; private set; }
14+
public IReadOnlyList<ImgInfo> ImgsInSecondButNotFirst { get; private set; }
15+
public IReadOnlyDictionary<NpkPath, FrameCountDifferenceInfo> ImgsWithMoreFramesInSecond { get; private set; }
16+
17+
public NpkDirDifferences(IReadOnlyList<ImgInfo> imgsInFirstButNotSecond, IReadOnlyDictionary<NpkPath, FrameCountDifferenceInfo> imgsWithFewerFramesInSecond, IReadOnlyList<ImgInfo> imgsInSecondButNotFirst, IReadOnlyDictionary<NpkPath, FrameCountDifferenceInfo> imgsWithMoreFramesInSecond)
18+
{
19+
ImgsInFirstButNotSecond = imgsInFirstButNotSecond;
20+
ImgsWithFewerFramesInSecond = imgsWithFewerFramesInSecond;
21+
ImgsInSecondButNotFirst = imgsInSecondButNotFirst;
22+
ImgsWithMoreFramesInSecond = imgsWithMoreFramesInSecond;
23+
}
24+
}
25+
}

npkdiff/Program.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using DFO.Common;
8+
using DFO.Npk;
9+
10+
namespace DFO.npkdiff
11+
{
12+
class Program
13+
{
14+
static void Main(string[] args)
15+
{
16+
CommandLineArgs cmdline = new CommandLineArgs(args);
17+
18+
// read *.NPK in NpkDir1
19+
// read *.NPK in NpkDir2
20+
// Compare
21+
22+
NpkDirContents dir1Contents = GetNpkDirContents(cmdline.NpkDir1);
23+
NpkDirContents dir2Contents = GetNpkDirContents(cmdline.NpkDir2);
24+
NpkDirDifferences differences = dir1Contents.GetDifferences(dir2Contents);
25+
DisplayDifferences(differences);
26+
}
27+
28+
static NpkDirContents GetNpkDirContents(string npkDir)
29+
{
30+
NpkDirContents allContents = new NpkDirContents();
31+
foreach (string npkFilePath in Directory.GetFiles(npkDir, "*.NPK", SearchOption.TopDirectoryOnly))
32+
{
33+
NpkDirContents npkContents = GetNpkContents(npkFilePath);
34+
allContents.Add(npkContents);
35+
}
36+
return allContents;
37+
}
38+
39+
static NpkDirContents GetNpkContents(string npkFilePath)
40+
{
41+
using (NpkReader npk = new NpkReader(npkFilePath))
42+
{
43+
return new NpkDirContents(Path.GetFileName(npkFilePath), npk.Frames);
44+
}
45+
}
46+
47+
static void DisplayDifferences(NpkDirDifferences differences)
48+
{
49+
foreach (ImgInfo img in differences.ImgsInSecondButNotFirst.OrderBy(x => x.Path.Path))
50+
{
51+
Console.WriteLine("+{0} ({1}): {2}", img.Path, img.FrameCount, img.NpkFileName);
52+
}
53+
foreach (NpkPath imgPath in differences.ImgsWithMoreFramesInSecond.Keys.OrderBy(x => x.Path))
54+
{
55+
string npkFileNameDisplay;
56+
string npkFileName1 = differences.ImgsWithMoreFramesInSecond[imgPath].NpkFileName1;
57+
string npkFileName2 = differences.ImgsWithMoreFramesInSecond[imgPath].NpkFileName2;
58+
if (npkFileName1.Equals(npkFileName2, StringComparison.OrdinalIgnoreCase))
59+
{
60+
npkFileNameDisplay = npkFileName1;
61+
}
62+
else
63+
{
64+
npkFileNameDisplay = string.Format("{0}->{1}", npkFileName1, npkFileName2);
65+
}
66+
Console.WriteLine(">{0} {1}->{2} {3}", imgPath, differences.ImgsWithMoreFramesInSecond[imgPath].FrameCount1, differences.ImgsWithMoreFramesInSecond[imgPath].FrameCount2, npkFileNameDisplay);
67+
}
68+
foreach (NpkPath imgPath in differences.ImgsWithFewerFramesInSecond.Keys.OrderBy(x => x.Path))
69+
{
70+
string npkFileNameDisplay;
71+
string npkFileName1 = differences.ImgsWithFewerFramesInSecond[imgPath].NpkFileName1;
72+
string npkFileName2 = differences.ImgsWithFewerFramesInSecond[imgPath].NpkFileName2;
73+
if (npkFileName1.Equals(npkFileName2, StringComparison.OrdinalIgnoreCase))
74+
{
75+
npkFileNameDisplay = npkFileName1;
76+
}
77+
else
78+
{
79+
npkFileNameDisplay = string.Format("{0}->{1}", npkFileName1, npkFileName2);
80+
}
81+
Console.WriteLine("<{0} {1}->{2}", imgPath, differences.ImgsWithFewerFramesInSecond[imgPath].FrameCount1, differences.ImgsWithFewerFramesInSecond[imgPath].FrameCount2, npkFileNameDisplay);
82+
}
83+
foreach (ImgInfo img in differences.ImgsInFirstButNotSecond.OrderBy(x => x.Path.Path))
84+
{
85+
Console.WriteLine("-{0} ({1}): {2}", img, img.FrameCount, img.NpkFileName);
86+
}
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)