From 5ef79f6426026dbd9ffc983264c13c74c4256997 Mon Sep 17 00:00:00 2001 From: MathiasL Date: Thu, 24 Mar 2022 23:03:53 +0100 Subject: [PATCH] Add AIN file indicator and nav/ain packed indicator * Version increase to 1.2 --- .../DamageCalculator/CsgoHelper.cs | 61 +++++++++++++++++++ .../DamageCalculator/DamageCalculator.csproj | 1 + .../DamageCalculator/MainWindow.xaml | 9 ++- .../DamageCalculator/MainWindow.xaml.cs | 22 +++++++ .../DamageCalculator/Models/CsgoMap.cs | 19 +++++- .../Properties/AssemblyInfo.cs | 4 +- 6 files changed, 112 insertions(+), 4 deletions(-) diff --git a/DamageCalculator/DamageCalculator/CsgoHelper.cs b/DamageCalculator/DamageCalculator/CsgoHelper.cs index f3f5e0f..2753ce4 100644 --- a/DamageCalculator/DamageCalculator/CsgoHelper.cs +++ b/DamageCalculator/DamageCalculator/CsgoHelper.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Globalization; using System.IO; +using System.IO.Compression; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -115,6 +116,13 @@ namespace Damage_Calculator map.NavFilePath = potentialNavFile; } + // Save path to AIN file if available + string potentialAinFile = System.IO.Path.Combine(this.CsgoPath, "csgo\\maps\\graphs", System.IO.Path.GetFileNameWithoutExtension(file) + ".ain"); + if (File.Exists(potentialAinFile)) + { + map.AinFilePath = potentialAinFile; + } + // Set map type switch (System.IO.Path.GetFileNameWithoutExtension(file).Split('_').First().ToLower()) { @@ -409,6 +417,59 @@ namespace Damage_Calculator return null; } + /// + /// Reads packed files from a BSP and returns whether any 1. NAV or 2. AIN files were found. + /// + /// The absolute path to the BSP file. + /// A tuple containing whether nav or ain files were found, in that order. + public (bool, bool) ReadIfPackedNavFilesInBsp(string bspFilePath) + { + bool navFound = false; + bool ainFound = false; + byte[] readZipBytes = null; + + using (var bspFile = File.OpenRead(bspFilePath)) + { + using (var reader = new BinaryReader(bspFile)) + { + // Stuff before lumps + pakfile index * lump array item length + reader.BaseStream.Position = 8 + (40 * 16); + + // Get lump pos and size + int offset = reader.ReadInt32(); + int length = reader.ReadInt32(); + + // Read zip file + reader.BaseStream.Position = offset; + readZipBytes = reader.ReadBytes(length); + } + } + + if (readZipBytes == null) + return (false, false); + + using (var stream = new MemoryStream(readZipBytes)) + { + using(var zip = new ZipArchive(stream, ZipArchiveMode.Read)) + { + foreach(var entry in zip.Entries) + { + if (entry.FullName.EndsWith(".nav")) + // Found a packed NAV file + navFound = true; + if(entry.FullName.EndsWith(".ain")) + // Found a packed AIN file + ainFound = true; + + if (navFound && ainFound) + // If both already found, return prematurely + return (true, true); + } + return (navFound, ainFound); + } + } + } + private bool isLumpUnused(byte[] lump) { for(int i = 0; i < lump.Length; i++) diff --git a/DamageCalculator/DamageCalculator/DamageCalculator.csproj b/DamageCalculator/DamageCalculator/DamageCalculator.csproj index 60a88d0..1503310 100644 --- a/DamageCalculator/DamageCalculator/DamageCalculator.csproj +++ b/DamageCalculator/DamageCalculator/DamageCalculator.csproj @@ -44,6 +44,7 @@ + diff --git a/DamageCalculator/DamageCalculator/MainWindow.xaml b/DamageCalculator/DamageCalculator/MainWindow.xaml index b7ae859..2f1a84f 100644 --- a/DamageCalculator/DamageCalculator/MainWindow.xaml +++ b/DamageCalculator/DamageCalculator/MainWindow.xaml @@ -97,7 +97,14 @@ - + + + + + + + + diff --git a/DamageCalculator/DamageCalculator/MainWindow.xaml.cs b/DamageCalculator/DamageCalculator/MainWindow.xaml.cs index 35b7809..272b4ef 100644 --- a/DamageCalculator/DamageCalculator/MainWindow.xaml.cs +++ b/DamageCalculator/DamageCalculator/MainWindow.xaml.cs @@ -227,7 +227,13 @@ namespace Damage_Calculator // Set indicator checkboxes this.chkHasMapFile.IsChecked = map.BspFilePath != null; + this.chkHasNavFile.IsChecked = map.NavFilePath != null; + this.chkHasAinFile.IsChecked = map.AinFilePath != null; + + // Set packed indicators for indicator checkboxes + this.txtNavFilePacked.Visibility = map.NavFileBspPacked ? Visibility.Visible : Visibility.Collapsed; + this.txtAinFilePacked.Visibility = map.AinFileBspPacked ? Visibility.Visible : Visibility.Collapsed; this.resetCanvas(); this.rightZoomBorder.Reset(); @@ -354,6 +360,22 @@ namespace Damage_Calculator map.SpawnPoints.Add(spawn); } } + + if (map.NavFilePath == null || map.AinFilePath == null) + { + // If either no NAV or no AIN file has been found, try to update them via the BSP pakfile + var navFilesFound = Globals.Settings.CsgoHelper.ReadIfPackedNavFilesInBsp(map.BspFilePath); + if (navFilesFound.Item1) + { + map.NavFileBspPacked = true; + map.NavFilePath = "PACKED"; + } + if (navFilesFound.Item2) + { + map.AinFileBspPacked = true; + map.AinFilePath = "PACKED"; + } + } } private Vector3 stringToVector3(string coords) diff --git a/DamageCalculator/DamageCalculator/Models/CsgoMap.cs b/DamageCalculator/DamageCalculator/Models/CsgoMap.cs index e88976d..7bf41ed 100644 --- a/DamageCalculator/DamageCalculator/Models/CsgoMap.cs +++ b/DamageCalculator/DamageCalculator/Models/CsgoMap.cs @@ -37,11 +37,28 @@ namespace Damage_Calculator.Models /// /// The absolute path to the file that holds this map's navigation meshes and callouts. - /// This might not always be existent, because it is generated by the map builder. + /// This might not always be existent, because it is generated by the map builder, but can be packed inside the BSP. In that case its value is "PACKED". /// It is always created with maps that are in the main game, because they need callouts and bot movements. /// public string NavFilePath { get; set; } + /// + /// Indicates whether the NAV file was packed inside of the BSP PAKFILE lump. + /// + public bool NavFileBspPacked { get; set; } + + /// + /// The absolute path to the file that holds some additional navigational. + /// This might not always be existent, because it is generated by the map builder, but can be packed inside the BSP. In that case its value is "PACKED". + /// It *might* always be created with maps that are in the main game. + /// + public string AinFilePath { get; set; } + + /// + /// Indicates whether the AIN file was packed inside of the BSP PAKFILE lump. + /// + public bool AinFileBspPacked { get; set; } + /// /// The map name as given in the file name, but without the prefix. /// diff --git a/DamageCalculator/DamageCalculator/Properties/AssemblyInfo.cs b/DamageCalculator/DamageCalculator/Properties/AssemblyInfo.cs index 16a197d..82ca610 100644 --- a/DamageCalculator/DamageCalculator/Properties/AssemblyInfo.cs +++ b/DamageCalculator/DamageCalculator/Properties/AssemblyInfo.cs @@ -51,5 +51,5 @@ using System.Windows; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.1.0.2")] -[assembly: AssemblyFileVersion("1.1.0.2")] +[assembly: AssemblyVersion("1.2.0.0")] +[assembly: AssemblyFileVersion("1.2.0.0")]