From 632b023f29f72c029bdabd15cbc0108ad4826c20 Mon Sep 17 00:00:00 2001 From: hguy Date: Sat, 19 Aug 2023 17:07:04 +0200 Subject: [PATCH] Fix SteamPath value reading + small refacto --- src/ARZExplorer/ExtractProgress.cs | 4 +- src/ARZExplorer/MainForm.cs | 12 +++--- src/ARZExplorer/Models/NodeTag.cs | 1 - src/TQVaultAE.Data/ArcFileProvider.cs | 42 +++---------------- src/TQVaultAE.Data/ArzFileProvider.cs | 34 +-------------- .../Contracts/Providers/IArcFileProvider.cs | 9 ++-- .../Contracts/Providers/IArzFileProvider.cs | 9 ++-- src/TQVaultAE.Domain/Entities/ArcFile.cs | 9 ++-- src/TQVaultAE.Domain/Entities/ArzFile.cs | 7 ++-- .../GamePathServiceWin.cs | 5 ++- 10 files changed, 33 insertions(+), 99 deletions(-) diff --git a/src/ARZExplorer/ExtractProgress.cs b/src/ARZExplorer/ExtractProgress.cs index f77df39c..a0820d61 100644 --- a/src/ARZExplorer/ExtractProgress.cs +++ b/src/ARZExplorer/ExtractProgress.cs @@ -117,7 +117,7 @@ private void DoArzExtraction() try { bool canceled = false; - foreach (RecordId recordID in arzProv.GetKeyTable(this.MainForm.SelectedFile.ARZFile)) + foreach (RecordId recordID in this.MainForm.SelectedFile.ARZFile.Keys) { if (canceled) break; @@ -160,7 +160,7 @@ private void DoArcExtraction() { bool canceled = false; - foreach (RecordId recordID in arcProv.GetKeyTable(this.MainForm.SelectedFile.ARCFile)) + foreach (RecordId recordID in this.MainForm.SelectedFile.ARCFile.Keys) { if (canceled) break; diff --git a/src/ARZExplorer/MainForm.cs b/src/ARZExplorer/MainForm.cs index 743bfcb3..fd361173 100644 --- a/src/ARZExplorer/MainForm.cs +++ b/src/ARZExplorer/MainForm.cs @@ -525,12 +525,12 @@ private void BuildTreeView() { this.treeViewTOC.BeginUpdate(); - RecordId[] dataRecords; + IEnumerable dataRecords; if (this.SelectedFile.FileType == CompressedFileType.ArzFile) - dataRecords = arzProv.GetKeyTable(this.SelectedFile.ARZFile); + dataRecords = this.SelectedFile.ARZFile.Keys; else if (this.SelectedFile.FileType == CompressedFileType.ArcFile) - dataRecords = arcProv.GetKeyTable(this.SelectedFile.ARCFile); + dataRecords = this.SelectedFile.ARCFile.Keys; else return; @@ -569,9 +569,9 @@ private void BuildTreeView() } } - for (int recIdx = 0; recIdx < dataRecords.Length; recIdx++) + foreach (var record in dataRecords) { - RecordId recordID = arcPrefix == string.Empty ? dataRecords[recIdx] : Path.Combine(arcPrefix, dataRecords[recIdx].Raw); + RecordId recordID = arcPrefix == string.Empty ? record : Path.Combine(arcPrefix, record.Raw); for (int tokIdx = 0; tokIdx < recordID.TokensRaw.Count; tokIdx++) { @@ -597,7 +597,6 @@ private void BuildTreeView() Thread = recordID, Key = currnodeKey, - RecIdx = recIdx, TokIdx = tokIdx, Text = token, @@ -638,7 +637,6 @@ void GetRootNode(string arcPrefix, TreeNode rootNode, out TreeNode arcRootNode) Thread = null, Key = arcPrefix, - RecIdx = 0, TokIdx = 0, Text = arcRootNode.Text, diff --git a/src/ARZExplorer/Models/NodeTag.cs b/src/ARZExplorer/Models/NodeTag.cs index f5077d3f..eedbca59 100644 --- a/src/ARZExplorer/Models/NodeTag.cs +++ b/src/ARZExplorer/Models/NodeTag.cs @@ -12,7 +12,6 @@ internal class NodeTag internal string Text; internal string TextU => Text.ToUpper(); - internal int RecIdx; internal int TokIdx; internal RecordId Key; internal TreeNode thisNode; diff --git a/src/TQVaultAE.Data/ArcFileProvider.cs b/src/TQVaultAE.Data/ArcFileProvider.cs index 58280b12..8fb62c3a 100644 --- a/src/TQVaultAE.Data/ArcFileProvider.cs +++ b/src/TQVaultAE.Data/ArcFileProvider.cs @@ -38,18 +38,6 @@ public ArcFileProvider(ILogger log, ITQDataService tQData) this.TQData = tQData; } - /// - /// Gets the sorted list of directoryEntries. - /// - /// string array holding the sorted list - public RecordId[] GetKeyTable(ArcFile file) - { - if (file.Keys == null || file.Keys.Length == 0) - this.BuildKeyTable(file); - - return (RecordId[])file.Keys.Clone(); - } - #region ArcFile Public Methods /// @@ -63,7 +51,7 @@ public bool Read(ArcFile file) if (!file.FileHasBeenRead) this.ReadARCToC(file); - return file.DirectoryEntries != null; + return file.DirectoryEntries.Any(); } catch (IOException exception) { @@ -125,7 +113,7 @@ public byte[] GetData(ArcFile file, RecordId dataId) if (!file.FileHasBeenRead) this.ReadARCToC(file); - if (file.DirectoryEntries == null) + if (!file.DirectoryEntries.Any()) { if (TQDebug.ArcFileDebugLevel > 1) Log.LogDebug("Error - Could not read {0}", file.FileName); @@ -284,25 +272,6 @@ public bool ExtractArcFile(ArcFile file, string destination) #region ArcFile Private Methods - /// - /// Builds a sorted list of entries in the directoryEntries dictionary. Used to build a tree structure of the names. - /// - private void BuildKeyTable(ArcFile file) - { - if (file.DirectoryEntries == null || file.DirectoryEntries.Count == 0) - return; - - int index = 0; - file.Keys = new RecordId[file.DirectoryEntries.Count]; - foreach (RecordId filename in file.DirectoryEntries.Keys) - { - file.Keys[index] = filename; - index++; - } - - Array.Sort(file.Keys); - } - /// /// Read the table of contents of the ARC file /// @@ -348,13 +317,14 @@ public void ReadARCToC(ArcFile file) Log.LogDebug("File Length={0}", arcFile.Length); // check the file header - if (reader.ReadByte() != 0x41) + byte first; + if ((first = reader.ReadByte()) != 0x41) return; - if (reader.ReadByte() != 0x52) + if ((first = reader.ReadByte()) != 0x52) return; - if (reader.ReadByte() != 0x43) + if ((first = reader.ReadByte()) != 0x43) return; if (arcFile.Length < 0x21) diff --git a/src/TQVaultAE.Data/ArzFileProvider.cs b/src/TQVaultAE.Data/ArzFileProvider.cs index fcba7fa0..2d9f225e 100644 --- a/src/TQVaultAE.Data/ArzFileProvider.cs +++ b/src/TQVaultAE.Data/ArzFileProvider.cs @@ -7,7 +7,9 @@ namespace TQVaultAE.Data { using Microsoft.Extensions.Logging; using System; + using System.Collections.Generic; using System.IO; + using System.Linq; using TQVaultAE.Config; using TQVaultAE.Domain.Contracts.Providers; using TQVaultAE.Domain.Contracts.Services; @@ -34,19 +36,6 @@ public ArzFileProvider(ILogger log, IRecordInfoProvider recordI this.infoProv = recordInfoProvider; } - - /// - /// Gets the list of keys from the recordInfo dictionary. - /// - /// string array holding the sorted list - public RecordId[] GetKeyTable(ArzFile file) - { - if (file.Keys == null || file.Keys.Length == 0) - this.BuildKeyTable(file); - - return (RecordId[])file.Keys.Clone(); - } - /// /// Reads the ARZ file. /// @@ -156,25 +145,6 @@ public DBRecordCollection GetItem(ArzFile file, RecordId recordId) public DBRecordCollection GetRecordNotCached(ArzFile file, RecordId recordId) => infoProv.Decompress(file, file.RecordInfo[recordId]); - /// - /// Builds a list of the keys for this file. Used to help build the tree structure. - /// - private void BuildKeyTable(ArzFile file) - { - if (file.RecordInfo == null || file.RecordInfo.Count == 0) - return; - - int index = 0; - file.Keys = new RecordId[file.RecordInfo.Count]; - foreach (RecordId recordID in file.RecordInfo.Keys) - { - file.Keys[index] = recordID; - index++; - } - - Array.Sort(file.Keys); - } - /// /// Reads the whole string table into memory from a stream. diff --git a/src/TQVaultAE.Domain/Contracts/Providers/IArcFileProvider.cs b/src/TQVaultAE.Domain/Contracts/Providers/IArcFileProvider.cs index b1da00dd..b0a64d43 100644 --- a/src/TQVaultAE.Domain/Contracts/Providers/IArcFileProvider.cs +++ b/src/TQVaultAE.Domain/Contracts/Providers/IArcFileProvider.cs @@ -1,4 +1,5 @@ -using TQVaultAE.Domain.Entities; +using System.Collections.Generic; +using TQVaultAE.Domain.Entities; namespace TQVaultAE.Domain.Contracts.Providers { @@ -21,11 +22,7 @@ public interface IArcFileProvider /// Read the table of contents of the ARC file /// void ReadARCToC(ArcFile file); - /// - /// Gets the sorted list of directoryEntries. - /// - /// string array holding the sorted list - RecordId[] GetKeyTable(ArcFile file); + /// /// Reads the ARC file table of contents to determine if the file is readable. /// diff --git a/src/TQVaultAE.Domain/Contracts/Providers/IArzFileProvider.cs b/src/TQVaultAE.Domain/Contracts/Providers/IArzFileProvider.cs index 5b4299eb..16206ef9 100644 --- a/src/TQVaultAE.Domain/Contracts/Providers/IArzFileProvider.cs +++ b/src/TQVaultAE.Domain/Contracts/Providers/IArzFileProvider.cs @@ -1,4 +1,5 @@ -using TQVaultAE.Domain.Entities; +using System.Collections.Generic; +using TQVaultAE.Domain.Entities; namespace TQVaultAE.Domain.Contracts.Providers { @@ -10,11 +11,7 @@ public interface IArzFileProvider /// string ID of the record will be normalized internally /// DBRecord corresponding to the string ID. DBRecordCollection GetItem(ArzFile file, RecordId recordId); - /// - /// Gets the list of keys from the recordInfo dictionary. - /// - /// string array holding the sorted list - RecordId[] GetKeyTable(ArzFile file); + /// /// Gets a database record without adding it to the cache. /// diff --git a/src/TQVaultAE.Domain/Entities/ArcFile.cs b/src/TQVaultAE.Domain/Entities/ArcFile.cs index 25218c73..de8f0e23 100644 --- a/src/TQVaultAE.Domain/Entities/ArcFile.cs +++ b/src/TQVaultAE.Domain/Entities/ArcFile.cs @@ -6,6 +6,7 @@ namespace TQVaultAE.Domain.Entities { using System.Collections.Generic; + using System.Linq; /// /// Reads and decodes a Titan Quest ARC file. @@ -20,12 +21,12 @@ public class ArcFile /// /// Dictionary of the directory entries. /// - public Dictionary DirectoryEntries; + public Dictionary DirectoryEntries = new(); /// - /// Holds the keys for the directoryEntries dictionary. + /// Ordered keys for the directoryEntries dictionary. /// - public RecordId[] Keys; + public IEnumerable Keys => this.DirectoryEntries.Keys.OrderBy(v => v); /// /// Initializes a new instance of the ArcFile class. @@ -42,7 +43,7 @@ public ArcFile(string fileName) /// /// Gets the number of Directory entries /// - public int Count => this.DirectoryEntries?.Count ?? 0; + public int Count => this.DirectoryEntries.Count; } } \ No newline at end of file diff --git a/src/TQVaultAE.Domain/Entities/ArzFile.cs b/src/TQVaultAE.Domain/Entities/ArzFile.cs index 533aad7b..ae791362 100644 --- a/src/TQVaultAE.Domain/Entities/ArzFile.cs +++ b/src/TQVaultAE.Domain/Entities/ArzFile.cs @@ -8,6 +8,7 @@ namespace TQVaultAE.Domain.Entities using System; using System.Collections.Concurrent; using System.Collections.Generic; + using System.Linq; using TQVaultAE.Domain.Helpers; /// @@ -28,12 +29,12 @@ public class ArzFile /// /// RecordInfo keyed by their ID /// - public Dictionary RecordInfo = new Dictionary(); + public Dictionary RecordInfo = new(); /// - /// Holds the keys for the recordInfo Dictionary + /// Ordered keys for the recordInfo Dictionary /// - public RecordId[] Keys; + public IEnumerable Keys => this.RecordInfo.Keys.OrderBy(v => v); /// /// Initializes a new instance of the ArzFile class. diff --git a/src/TQVaultAE.Services.Win32/GamePathServiceWin.cs b/src/TQVaultAE.Services.Win32/GamePathServiceWin.cs index b987e30a..df42d584 100644 --- a/src/TQVaultAE.Services.Win32/GamePathServiceWin.cs +++ b/src/TQVaultAE.Services.Win32/GamePathServiceWin.cs @@ -619,7 +619,8 @@ public string ResolveGamePath() { if (vdfPathRegex.Match(line.Trim()) is { Success: true } match) { - fullPath = Path.Combine(match.Groups[1].Value, steamTQPath); + var vdfPathValue = match.Groups[1].Value.Replace(@"\\", @"\"); + fullPath = Path.Combine(vdfPathValue, steamTQPath); if (Directory.Exists(fullPath)) { titanQuestGamePath = fullPath; @@ -638,7 +639,7 @@ public string ResolveGamePath() { // Match "path" if (regExPath.Match(line) is { Success: true } match) - steamPath = match.Groups["path"].Value; + steamPath = match.Groups["path"].Value.Replace(@"\\", @"\");// Backslashes ares escaped in this file // Match gameId if (line.Contains(gameIdMarkup))