Skip to content
This repository has been archived by the owner on Apr 2, 2020. It is now read-only.

Commit

Permalink
Version 1.0.2.4
Browse files Browse the repository at this point in the history
+ Add watch mode
+ Add output path argument
+ Add help argument
+ Change argument handling
+ Clear code
  • Loading branch information
Luzifix committed Oct 3, 2016
1 parent e8dc60c commit 999821e
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 64 deletions.
1 change: 1 addition & 0 deletions ADTConvert/ADTConvert.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ConsoleConfig.cs" />
<Compile Include="Helper.cs" />
<Compile Include="Main.cs" />
<Compile Include="Program.cs" />
Expand Down
65 changes: 65 additions & 0 deletions ADTConvert/ConsoleConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ADTConvert
{
class ConsoleConfig
{
private static ConsoleConfig instance;

public string Input { get; set; }
public string Output { get; set; }
public bool SilentMode { get; set; }
public bool Verbose { get; set; }
public bool NoUpdate { get; set; }
public bool Watch { get; set; }
public bool Help { get; set; }

private ConsoleConfig() {}

public static ConsoleConfig Instance
{
get
{
if (instance == null)
{
instance = new ConsoleConfig();
}
return instance;
}
}

public ConsoleConfig ReadArgs(string[] args)
{
if (args.Length <= 0)
return this;

SilentMode = (args.Contains("-s") || args.Contains("--silent"));
Verbose = (args.Contains("-v") || args.Contains("--verbose"));
NoUpdate = (args.Contains("-noUpdate") || args.Contains("--disableUpdateCheck"));
Watch = (args.Contains("-w") || args.Contains("--watch"));
Help = (args.Contains("-h") || args.Contains("--help"));

Input = Path.GetFullPath(args[0]);

IEnumerable<string> outPath;

outPath = args.Where(s => s.StartsWith("-o="));
if (outPath.Count() == 1)
{
Output = Path.GetFullPath(outPath.First().Split('=').Last());
}

outPath = args.Where(s => s.StartsWith("--output="));
if (Output == null && outPath.Count() == 1)
{
Output = Path.GetFullPath(outPath.First().Split('=').Last());
}

return this;
}
}
}
168 changes: 126 additions & 42 deletions ADTConvert/Main.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;

namespace ADTConvert
{
Expand All @@ -17,15 +18,73 @@ struct DataChunk
private string adtName = "";
private string exportPath = "";
List<DataChunk> chunks = new List<DataChunk>();
ConsoleConfig config = ConsoleConfig.Instance;
private HashSet<string> filesToProcess = new HashSet<string>();

public Main(string input, bool verbose = false, int version = 6)
public Main()
{
if(config.Watch)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = config.Input;
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = "*.adt";
watcher.Changed += new FileSystemEventHandler(OnADTChanged);
watcher.EnableRaisingEvents = true;
watcher.IncludeSubdirectories = true;

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Watcher active");
Console.ResetColor();

Console.WriteLine("\nPress any key to stop the converter watcher");
Console.ReadKey();
watcher.Dispose();
}
else
{
convertADT(config.Input);
}
}

private void OnADTChanged(object sender, FileSystemEventArgs e)
{
if(filesToProcess.Contains(e.FullPath))
{
filesToProcess.Remove(e.FullPath);
return;
}

Thread.Sleep(250);
convertADT(e.FullPath);
Clear();

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Convert complete");
Console.ResetColor();

Console.WriteLine("\nPress any key to stop the converter watcher");
filesToProcess.Add(e.FullPath);
}

private bool convertADT(string input)
{
Console.WriteLine("\n--- Base ADT Load ---");
adtName = Path.GetFileName(input);
exportPath = (Path.GetDirectoryName(input) == "" ? AppDomain.CurrentDomain.BaseDirectory : Path.GetFullPath(Path.GetDirectoryName(input))) + @"\export\";
exportPath = (Path.GetDirectoryName(input) == "" ? AppDomain.CurrentDomain.BaseDirectory : Path.GetFullPath(Path.GetDirectoryName(input))) + (config.Watch ? @"\.." : "") + @"\export\";
exportPath = (config.Output != null ? config.Output + @"\" : exportPath);

if(config.Watch)
{
string folderStruct = input.Replace(config.Input, "");

if (folderStruct != "")
exportPath += Path.GetDirectoryName(folderStruct) + @"\";
}

if (!Directory.Exists(exportPath))
{
if (verbose)
if (config.Verbose)
Console.WriteLine("Debug: Create export dir in {0}", exportPath);

Directory.CreateDirectory(exportPath);
Expand All @@ -35,57 +94,82 @@ public Main(string input, bool verbose = false, int version = 6)
try
{
Console.WriteLine("Info: Open {0}", adtName);
FileStream baseStream = new FileStream(input, FileMode.Open, FileAccess.ReadWrite, FileShare.None);

FileInfo fileInfo = new FileInfo(input);
FileStream baseStream = fileInfo.OpenRead();
inputReader = new BinaryReader(baseStream);
}
catch (Exception e)
{
Program.ConsoleErrorEnd(e.Message);
}

if (!Helper.SeekChunk(inputReader, "MVER") || !Helper.SeekChunk(inputReader, "MHDR") || !Helper.SeekChunk(inputReader, "MCIN"))
{
Program.ConsoleErrorEnd("ADT file is corrupted");
return;
}

#endregion

#region Load all chunks
Console.WriteLine("Info: Load all chunks");
inputReader.BaseStream.Seek(0, SeekOrigin.Begin);
while (inputReader.BaseStream.Position + 8 < inputReader.BaseStream.Length)
using (inputReader)
{
string magic = Helper.Reverse(new string(inputReader.ReadChars(4)));
int size = inputReader.ReadInt32() + 8;
inputReader.BaseStream.Position -= 8;
byte[] data = inputReader.ReadBytes(size);

if(verbose)
Console.WriteLine("Debug: Load {0}", magic);
if (!Helper.SeekChunk(inputReader, "MVER") || !Helper.SeekChunk(inputReader, "MHDR") || !Helper.SeekChunk(inputReader, "MCIN"))
{
Program.ConsoleErrorEnd("ADT file is corrupted");
return false;
}

chunks.Add(new DataChunk
#region Load all chunks
Console.WriteLine("Info: Load all chunks");
inputReader.BaseStream.Seek(0, SeekOrigin.Begin);
while (inputReader.BaseStream.Position + 8 < inputReader.BaseStream.Length)
{
Data = data,
Signature = magic,
Size = size
});
string magic = Helper.Reverse(new string(inputReader.ReadChars(4)));
int size = inputReader.ReadInt32() + 8;
inputReader.BaseStream.Position -= 8;
byte[] data = inputReader.ReadBytes(size);

if (config.Verbose)
Console.WriteLine("Debug: Load {0}", magic);

chunks.Add(new DataChunk
{
Data = data,
Signature = magic,
Size = size
});
}
#endregion

if (createRoot())
if (createTex())
if (createObj())
{
return true;
}
}
#endregion

return false;
}

private void Clear()
{
adtName = "";
exportPath = "";
chunks.Clear();

if (createRoot(input, verbose))
if (createTex(input, verbose))
createObj(input, verbose);
if(inputReader != null)
{
inputReader.Close();
inputReader = null;
}
}

private bool createRoot(string input, bool verbose = false, int version = 6)
private bool createRoot()
{
Console.WriteLine("\n--- Root ADT Convert ---");
BinaryReader rootReader = null;
BinaryWriter rootWriter = null;
List<string> rootChunks = new List<string> { "MVER", /*"MHDR", "MH2O", "MCNK", "MFBO"*/ };
List<string> mcnkSubChunks = new List<string> { "MCTV", "MCVT", "MCLV", "MCCV", "MCNR", "MCSE" };

if (!createBase(ref rootReader, ref rootWriter, rootChunks, ".adt", verbose))
if (!createBase(ref rootReader, ref rootWriter, rootChunks, ".adt"))
return false;

#region Write empty MHDR chunk
Expand Down Expand Up @@ -217,7 +301,7 @@ private bool createRoot(string input, bool verbose = false, int version = 6)
#region Write MCNR sub chunk
if(subChunkName == "MCNR")
{
if (verbose)
if (config.Verbose)
Console.WriteLine("Debug: Write MCNR chunk");

while (Helper.SeekSubChunk(inputReader, "MCNR", false, chunkEnd))
Expand Down Expand Up @@ -304,7 +388,7 @@ private bool createRoot(string input, bool verbose = false, int version = 6)
return true;
}

private bool createTex(string input, bool verbose = false, int version = 6)
private bool createTex()
{
Console.WriteLine("\n--- Tex ADT Convert ---");
string ext0 = "_tex0.adt";
Expand All @@ -315,7 +399,7 @@ private bool createTex(string input, bool verbose = false, int version = 6)
List<string> mcnkSubChunks = new List<string> { "MCLY", "MCSH", "MCAL", "MCMT"};


if (!createBase(ref texReader, ref texWriter, texChunks, ext0, verbose))
if (!createBase(ref texReader, ref texWriter, texChunks, ext0))
return false;

#region if MAMP not exist create a MAMP chunk
Expand Down Expand Up @@ -364,7 +448,7 @@ private bool createTex(string input, bool verbose = false, int version = 6)
inputReader.BaseStream.Position = currentPosition;
if (!Helper.SeekSubChunk(inputReader, "MCMT", false, chunkEnd))
{
if (verbose)
if (config.Verbose)
Console.WriteLine("Info: Create MCMT subchunk");
newSize += 12;

Expand Down Expand Up @@ -399,7 +483,7 @@ private bool createTex(string input, bool verbose = false, int version = 6)
return true;
}

private bool createObj(string input, bool verbose = false, int version = 6)
private bool createObj()
{
Console.WriteLine("\n--- Obj ADT Convert ---");
string ext0 = "_obj0.adt";
Expand All @@ -409,7 +493,7 @@ private bool createObj(string input, bool verbose = false, int version = 6)
List<string> objChunks = new List<string> { "MVER", "MMDX", "MMID", "MWMO", "MWID", "MDDF", "MODF", /*"MCNK"*/ };
List<string> mcnkSubChunks = new List<string> { "MCRD", "MCRW" };

if (!createBase(ref objReader, ref objWriter, objChunks, ext0, verbose))
if (!createBase(ref objReader, ref objWriter, objChunks, ext0))
return false;

#region Copy & clean MCNK
Expand Down Expand Up @@ -457,7 +541,7 @@ private bool createObj(string input, bool verbose = false, int version = 6)
inputReader.BaseStream.Position = currentPosition;
if (Helper.SeekSubChunk(inputReader, "MCRF", false, chunkEnd))
{
if (verbose)
if (config.Verbose)
Console.WriteLine("Info: Read MCRF subchunk");

inputReader.BaseStream.Position += sizeof(UInt32); // size
Expand All @@ -468,13 +552,13 @@ private bool createObj(string input, bool verbose = false, int version = 6)
byte[] mcrdData = inputReader.ReadBytes(mcrdSize);
byte[] mcrwData = inputReader.ReadBytes(mcrwSize);

if (verbose)
if (config.Verbose)
Console.WriteLine("Info: Create MCRD subchunk");
newSize += 8 + (uint)mcrdSize;

writeChunk(objWriter, "MCRD", mcrdSize, mcrdData);

if (verbose)
if (config.Verbose)
Console.WriteLine("Info: Create MCRW subchunk");
newSize += 8 + (uint)mcrwSize;

Expand Down Expand Up @@ -508,15 +592,15 @@ private bool createObj(string input, bool verbose = false, int version = 6)
return true;
}

private bool createBase(ref BinaryReader reader, ref BinaryWriter writer, List<string> allowdChunks, string ext, bool verbose = false)
private bool createBase(ref BinaryReader reader, ref BinaryWriter writer, List<string> allowdChunks, string ext)
{
string baseName = Path.GetFileNameWithoutExtension(adtName) + ext;
string baseADT = exportPath + baseName;

#region Remove adt
if (File.Exists(baseADT))
{
if (verbose)
if (config.Verbose)
Console.WriteLine("Debug: Remove {0}", baseName);

File.Delete(baseADT);
Expand Down
Loading

0 comments on commit 999821e

Please sign in to comment.