From 279c598fde4b4ec6ef27edfd7ddfdaf907af48ca Mon Sep 17 00:00:00 2001 From: Cambloid Date: Thu, 6 Mar 2025 00:19:55 +0100 Subject: [PATCH 1/5] Add hkt export support --- AlienPAK/Explorer.cs | 89 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/AlienPAK/Explorer.cs b/AlienPAK/Explorer.cs index 3124e62..9da3499 100644 --- a/AlienPAK/Explorer.cs +++ b/AlienPAK/Explorer.cs @@ -5,10 +5,12 @@ using System.IO; using System.Linq; using System.Reflection; +using System.Runtime.CompilerServices; using System.Runtime.Remoting.Metadata; using System.Threading; using System.Windows.Documents; using System.Windows.Forms; +using System.Windows.Markup; using System.Windows.Media; using System.Windows.Media.Media3D; using Assimp; @@ -503,6 +505,82 @@ private void ExportSelectedFile() ExportNode(FileTree.SelectedNode); } + private PAK2.File getPAK2File(string pakFileName) + { + return ( + (PAK2)pak.File).Entries.FirstOrDefault( + o => o.Filename.Replace('\\', '/') == pakFileName.Replace('\\', '/') + ); + } + + private bool hktHasMagicNumber(PAK2.File pakFile, out int startIndex) + { + startIndex = -1; + + try + { + if (pakFile == null) + { + return false; + } + + if (pakFile.Content.Length == 0 || pakFile.Content.Length < 4) + { + return false; + } + + uint magic1 = 0x57E0E057; + uint magic2 = 0x10C0C010; + + // Check only first 20 bytes, since + // the magic numbers appears after 12 bytes + for (int i = 0; i < 20; i++) + { + uint num = BitConverter.ToUInt32(pakFile.Content, i); + + if (num == magic1 || num == magic2) + { + startIndex = i; + return true; + } + } + + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + + return false; + } + + private void extractHktFile(string pakFileName, string saveDestination) + { + try + { + var pakFile = this.getPAK2File(pakFileName); + + if (pakFile == null) + { + return; + } + + if (this.hktHasMagicNumber(pakFile, out int startIdx)) + { + using (FileStream fs = new FileStream(saveDestination, FileMode.Create, FileAccess.Write)) + { + fs.Write(pakFile.Content, startIdx, pakFile.Content.Length - startIdx); + } + } + + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + MessageBox.Show(ex.ToString(), "Export failed", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + /* Export the selected PAK entry as a standalone file */ private void ExportNode(TreeNode node, string outputFolder = "") //If you set outputFolder, the user won't get a filepicker { @@ -520,6 +598,11 @@ private void ExportNode(TreeNode node, string outputFolder = "") //If you set ou string fileName = Path.GetFileName(node.Text); while (Path.GetExtension(fileName).Length != 0) fileName = fileName.Substring(0, fileName.Length - Path.GetExtension(fileName).Length); //Remove extensions from output filename + if (this.hktHasMagicNumber(this.getPAK2File(nodeVal), out int startIndex)) + { + filter += "|Havok file|*.hkt"; + } + string pickedFileName = ""; if (outputFolder == "") { @@ -545,6 +628,7 @@ private void ExportNode(TreeNode node, string outputFolder = "") //If you set ou } Cursor.Current = Cursors.WaitCursor; + try { switch (pak.Type) @@ -552,6 +636,11 @@ private void ExportNode(TreeNode node, string outputFolder = "") //If you set ou case PAKType.ANIMATIONS: case PAKType.UI: case PAKType.CHR_INFO: + if (Path.GetExtension(pickedFileName).ToUpper() == ".HKT") + { + this.extractHktFile(nodeVal, pickedFileName); + break; + } File.WriteAllBytes(pickedFileName, ((PAK2)pak.File).Entries.FirstOrDefault(o => o.Filename.Replace('\\', '/') == nodeVal.Replace('\\', '/'))?.Content); break; case PAKType.TEXTURES: From 321d95fb412b92bd28312a0f2f5fdc2a3b2dc1cf Mon Sep 17 00:00:00 2001 From: Cambloid Date: Thu, 6 Mar 2025 00:30:40 +0100 Subject: [PATCH 2/5] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 1cbdffe..2387f04 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ *.userprefs # Build results +Build/ [Dd]ebug/ [Dd]ebugPublic/ [Rr]elease/ From 0e5ecff4fecab09a87fc272538655e5216f46a82 Mon Sep 17 00:00:00 2001 From: Cambloid Date: Thu, 6 Mar 2025 00:43:42 +0100 Subject: [PATCH 3/5] Update usings --- AlienPAK/Explorer.cs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/AlienPAK/Explorer.cs b/AlienPAK/Explorer.cs index 9da3499..8535a90 100644 --- a/AlienPAK/Explorer.cs +++ b/AlienPAK/Explorer.cs @@ -1,25 +1,15 @@ +using Assimp; +using CATHODE; +using CATHODE.LEGACY; +using DirectXTexNet; using System; using System.Collections.Generic; using System.Diagnostics; -using System.Drawing; using System.IO; using System.Linq; -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.Remoting.Metadata; -using System.Threading; -using System.Windows.Documents; using System.Windows.Forms; -using System.Windows.Markup; using System.Windows.Media; using System.Windows.Media.Media3D; -using Assimp; -using Assimp.Unmanaged; -using CATHODE; -using CATHODE.LEGACY; -using CathodeLib; -using DirectXTexNet; -using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window; using static CATHODE.Materials.Material; namespace AlienPAK From fe0abca563d3dd60e447f50bf0fe0fb3a04abeaa Mon Sep 17 00:00:00 2001 From: Cambloid Date: Thu, 6 Mar 2025 03:23:57 +0100 Subject: [PATCH 4/5] Update Explorer.cs --- AlienPAK/Explorer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/AlienPAK/Explorer.cs b/AlienPAK/Explorer.cs index 8535a90..d37d09f 100644 --- a/AlienPAK/Explorer.cs +++ b/AlienPAK/Explorer.cs @@ -590,7 +590,7 @@ private void ExportNode(TreeNode node, string outputFolder = "") //If you set ou if (this.hktHasMagicNumber(this.getPAK2File(nodeVal), out int startIndex)) { - filter += "|Havok file|*.hkt"; + filter += "|Havok Packfile|*.hkx"; } string pickedFileName = ""; @@ -626,7 +626,7 @@ private void ExportNode(TreeNode node, string outputFolder = "") //If you set ou case PAKType.ANIMATIONS: case PAKType.UI: case PAKType.CHR_INFO: - if (Path.GetExtension(pickedFileName).ToUpper() == ".HKT") + if (Path.GetExtension(pickedFileName).ToUpper() == ".HKX") { this.extractHktFile(nodeVal, pickedFileName); break; From 48e584a8eca226569074c4b323357a5bd46c0b9f Mon Sep 17 00:00:00 2001 From: Cambloid Date: Thu, 6 Mar 2025 03:25:30 +0100 Subject: [PATCH 5/5] Update Explorer.cs --- AlienPAK/Explorer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/AlienPAK/Explorer.cs b/AlienPAK/Explorer.cs index d37d09f..f889dff 100644 --- a/AlienPAK/Explorer.cs +++ b/AlienPAK/Explorer.cs @@ -520,7 +520,7 @@ private bool hktHasMagicNumber(PAK2.File pakFile, out int startIndex) } uint magic1 = 0x57E0E057; - uint magic2 = 0x10C0C010; + //uint magic2 = 0x10C0C010; // Check only first 20 bytes, since // the magic numbers appears after 12 bytes @@ -528,7 +528,7 @@ private bool hktHasMagicNumber(PAK2.File pakFile, out int startIndex) { uint num = BitConverter.ToUInt32(pakFile.Content, i); - if (num == magic1 || num == magic2) + if (num == magic1) { startIndex = i; return true;