--- Program.cs	Sun Jun 29 19:43:06 2008
+++ TAHDom.cs	Tue Jan 27 09:01:04 2009
@@ -1,11 +1,11 @@
-ο»Ώusing System;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 
 namespace TAHdecrypt
 {
-    class Program
+    class TAHDom
     {
         static void Main(string[] args)
         {
@@ -64,27 +64,29 @@
                 {
                     dest_path += file_path[i] + "\\";
                 }
-                dest_path += folder_name;
-                System.Console.Out.WriteLine(dest_path);
-                Decrypter myDecrypter = new Decrypter();
-                return myDecrypter.decrypt_archive(source_file, dest_path);
-            }
-            else
-            {
-                //encrypt to TAH archive
-                if (System.IO.Directory.Exists(source_file))
+                string folder_path = dest_path + folder_name;
+                System.Console.Out.WriteLine(folder_path);
+
+                if (System.IO.File.Exists(dest_path + file_name + ".bak"))
                 {
-                    //launch encrypt routine from here...
-                    Decrypter myDecrypter = new Decrypter();
-                    for (int i = 0; i < file_path.Length - 1; i++)
+                    System.Console.Out.WriteLine("Error: {0} π©Β―½ΜΕπ~΅ά·Bν·ι©ΌOπΟX΅Δ­Ύ³’B", dest_path + file_name + ".bak");
+                    return -1;
+                }
+
+                Decrypter myDecrypter = new Decrypter();
+                int version = myDecrypter.decrypt_archive(source_file, folder_path);
+                System.Console.Out.WriteLine("TAH version {0} Ε·B", version);
+                if (version >= 0) {
+                    //encrypt to TAH archive
+                    if (System.IO.Directory.Exists(folder_path))
                     {
-                        dest_path += file_path[i] + "\\";
+                        //launch encrypt routine from here...
+                        System.Console.Out.WriteLine(dest_path);
+                        return myDecrypter.encrypt_archive(file_name, dest_path, folder_path, version);
                     }
-                    System.Console.Out.WriteLine(dest_path);
-                    return myDecrypter.encrypt_archive(file_name + ".tah", dest_path, source_file);
                 }
-                return -1;
             }
+            return -1;
         }
     }
 
@@ -284,7 +286,7 @@
 
         public struct tah_file
         {
-            public header tah_header;
+            //public header tah_header;
             public file_entry[] all_compressed_files;
         }
 
@@ -308,7 +310,7 @@
         {
             public UInt32 id; //TAH2 (843596116)
             public UInt32 index_entry_count;
-            public UInt32 unknown; //1
+            public UInt32 version; //1
             public UInt32 reserved; //0
         }
 
@@ -332,7 +334,7 @@
             public entry_meta_info[] directory_meta_infos;
         }
 
-        public int encrypt_archive(string file_name, string dest_path, string source_path)
+        public int encrypt_archive(string file_name, string dest_path, string source_path, int version)
         {
             //check if file already exists... if yes rename it
             string file_path_name = dest_path + file_name;
@@ -442,7 +444,7 @@
             System.IO.BinaryWriter writer = new System.IO.BinaryWriter(System.IO.File.Create(dest_path + "\\" + file_name));
             writer.Write(System.Text.Encoding.ASCII.GetBytes("TAH2"));
             writer.Write(all_files_count);
-            writer.Write(((UInt32)1));
+            writer.Write(((UInt32)version));
             writer.Write(((UInt32)0));
             UInt32 offset = 16 + 8 * all_files_count + compressed_file_index_length + 4;
             //writer needs this defined offset for adding length lists of the compressed data later on
@@ -553,6 +555,7 @@
 
         public int decrypt_archive(string source_file, string dest_path)
         {
+            header tah_header = new header();
             directory_meta_info directory_meta_infos = new directory_meta_info();
             try
             {
@@ -564,13 +567,14 @@
                 return -1;
             }
             int ret = 0;
-            ret = extract_TAH_directory(ref reader, ref directory_meta_infos);
+
+            ret = extract_TAH_directory(ref reader, ref tah_header, ref directory_meta_infos);
             if (ret >= 0)
             {
                 ret = extract_TAH_resource(ref reader, dest_path, ref directory_meta_infos);
             }
             reader.Close();
-            return ret;
+            return (int)tah_header.version;
         }
 
         // modified:
@@ -666,7 +670,7 @@
 
                 if (nInputPos > 32860)
                 {
-                    int dummy = 0;
+                    //int dummy = 0;
                 }
                 // Are there ANY good matches?
                 if ((nOffset1 != 0) || (nOffset2 != 0))
@@ -991,7 +995,7 @@
                 flag >>= 1;
                 if (act_output_length >= 32860)
                 {
-                    int dummy = 0;
+                    //int dummy = 0;
                 }
                 if ((flag & 0x0100) == 0)
                 {
@@ -1073,10 +1077,9 @@
             }
         }
 
-        public static int extract_TAH_directory(ref System.IO.BinaryReader file_reader,
+        public static int extract_TAH_directory(ref System.IO.BinaryReader file_reader, ref header tah_header,
                                             ref directory_meta_info dir_meta_info)
         {
-            header tah_header;
             UInt32 arc_size;
 
             if (file_reader.BaseStream.Length > 16) //sizeof(header) == 16
@@ -1088,13 +1091,11 @@
                 return -1;
             }
 
-            tah_header = new header();
-
             try
             {
                 tah_header.id = System.BitConverter.ToUInt32(file_reader.ReadBytes(4), 0);
                 tah_header.index_entry_count = System.BitConverter.ToUInt32(file_reader.ReadBytes(4), 0);
-                tah_header.unknown = System.BitConverter.ToUInt32(file_reader.ReadBytes(4), 0);
+                tah_header.version = System.BitConverter.ToUInt32(file_reader.ReadBytes(4), 0);
                 tah_header.reserved = System.BitConverter.ToUInt32(file_reader.ReadBytes(4), 0);
             }
             catch (Exception)
@@ -1281,12 +1282,6 @@
                 //sorting for faster look up...
                 Array.Sort(external_files.hashkeys, external_files.files);
             }
-            else
-            {
-                System.Console.Out.WriteLine("Could not find \"names.txt\" at " + System.Environment.CurrentDirectory.ToString() + ".");
-                System.Console.Out.WriteLine("Press any Key to continue with data extraction without correct file names.");
-                System.Console.ReadKey();
-            }
             //now proceed with decrypting
             for (int i = 0; i < dir_meta_info.index_entry_count; i++)
             {
@@ -1423,12 +1418,19 @@
                     System.Console.Out.WriteLine("Error: Cannot write decompressed file.");
                     return -1;
                 }
+
+                if (write_file_str.Substring(write_file_str.Length - 3, 3).ToLower().CompareTo("tmo") == 0)
+                {
+                    System.Console.Out.WriteLine("Doming File: " + write_file_str);
+                    Dom.UpdateTmo(write_file_str);
+                }
             }
             return 0;
         }
 
         public string[] GetFiles(string source_file)
         {
+            header tah_header = new header();
             directory_meta_info directory_meta_infos = new directory_meta_info();
             try
             {
@@ -1440,7 +1442,7 @@
                 return null;
             }
             int ret = 0;
-            ret = extract_TAH_directory(ref reader, ref directory_meta_infos);
+            ret = extract_TAH_directory(ref reader, ref tah_header, ref directory_meta_infos);
             reader.Close();
             string[] files = new string[directory_meta_infos.index_entry_count];
             for (int count = 0; count < directory_meta_infos.index_entry_count; count++)
