Skip to content

Commit

Permalink
Fix SteamPath value reading + small refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
hguy committed Aug 19, 2023
1 parent 3d69961 commit 632b023
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 99 deletions.
4 changes: 2 additions & 2 deletions src/ARZExplorer/ExtractProgress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 5 additions & 7 deletions src/ARZExplorer/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -525,12 +525,12 @@ private void BuildTreeView()
{
this.treeViewTOC.BeginUpdate();

RecordId[] dataRecords;
IEnumerable<RecordId> 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;

Expand Down Expand Up @@ -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++)
{
Expand All @@ -597,7 +597,6 @@ private void BuildTreeView()

Thread = recordID,
Key = currnodeKey,
RecIdx = recIdx,
TokIdx = tokIdx,

Text = token,
Expand Down Expand Up @@ -638,7 +637,6 @@ void GetRootNode(string arcPrefix, TreeNode rootNode, out TreeNode arcRootNode)

Thread = null,
Key = arcPrefix,
RecIdx = 0,
TokIdx = 0,

Text = arcRootNode.Text,
Expand Down
1 change: 0 additions & 1 deletion src/ARZExplorer/Models/NodeTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
42 changes: 6 additions & 36 deletions src/TQVaultAE.Data/ArcFileProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@ public ArcFileProvider(ILogger<ArcFileProvider> log, ITQDataService tQData)
this.TQData = tQData;
}

/// <summary>
/// Gets the sorted list of directoryEntries.
/// </summary>
/// <returns>string array holding the sorted list</returns>
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

/// <summary>
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -284,25 +272,6 @@ public bool ExtractArcFile(ArcFile file, string destination)

#region ArcFile Private Methods

/// <summary>
/// Builds a sorted list of entries in the directoryEntries dictionary. Used to build a tree structure of the names.
/// </summary>
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);
}

/// <summary>
/// Read the table of contents of the ARC file
/// </summary>
Expand Down Expand Up @@ -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)
Expand Down
34 changes: 2 additions & 32 deletions src/TQVaultAE.Data/ArzFileProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,19 +36,6 @@ public ArzFileProvider(ILogger<ArzFileProvider> log, IRecordInfoProvider recordI
this.infoProv = recordInfoProvider;
}


/// <summary>
/// Gets the list of keys from the recordInfo dictionary.
/// </summary>
/// <returns>string array holding the sorted list</returns>
public RecordId[] GetKeyTable(ArzFile file)
{
if (file.Keys == null || file.Keys.Length == 0)
this.BuildKeyTable(file);

return (RecordId[])file.Keys.Clone();
}

/// <summary>
/// Reads the ARZ file.
/// </summary>
Expand Down Expand Up @@ -156,25 +145,6 @@ public DBRecordCollection GetItem(ArzFile file, RecordId recordId)
public DBRecordCollection GetRecordNotCached(ArzFile file, RecordId recordId)
=> infoProv.Decompress(file, file.RecordInfo[recordId]);

/// <summary>
/// Builds a list of the keys for this file. Used to help build the tree structure.
/// </summary>
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);
}


/// <summary>
/// Reads the whole string table into memory from a stream.
Expand Down
9 changes: 3 additions & 6 deletions src/TQVaultAE.Domain/Contracts/Providers/IArcFileProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using TQVaultAE.Domain.Entities;
using System.Collections.Generic;
using TQVaultAE.Domain.Entities;

namespace TQVaultAE.Domain.Contracts.Providers
{
Expand All @@ -21,11 +22,7 @@ public interface IArcFileProvider
/// Read the table of contents of the ARC file
/// </summary>
void ReadARCToC(ArcFile file);
/// <summary>
/// Gets the sorted list of directoryEntries.
/// </summary>
/// <returns>string array holding the sorted list</returns>
RecordId[] GetKeyTable(ArcFile file);

/// <summary>
/// Reads the ARC file table of contents to determine if the file is readable.
/// </summary>
Expand Down
9 changes: 3 additions & 6 deletions src/TQVaultAE.Domain/Contracts/Providers/IArzFileProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using TQVaultAE.Domain.Entities;
using System.Collections.Generic;
using TQVaultAE.Domain.Entities;

namespace TQVaultAE.Domain.Contracts.Providers
{
Expand All @@ -10,11 +11,7 @@ public interface IArzFileProvider
/// <param name="recordId">string ID of the record will be normalized internally</param>
/// <returns>DBRecord corresponding to the string ID.</returns>
DBRecordCollection GetItem(ArzFile file, RecordId recordId);
/// <summary>
/// Gets the list of keys from the recordInfo dictionary.
/// </summary>
/// <returns>string array holding the sorted list</returns>
RecordId[] GetKeyTable(ArzFile file);

/// <summary>
/// Gets a database record without adding it to the cache.
/// </summary>
Expand Down
9 changes: 5 additions & 4 deletions src/TQVaultAE.Domain/Entities/ArcFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace TQVaultAE.Domain.Entities
{
using System.Collections.Generic;
using System.Linq;

/// <summary>
/// Reads and decodes a Titan Quest ARC file.
Expand All @@ -20,12 +21,12 @@ public class ArcFile
/// <summary>
/// Dictionary of the directory entries.
/// </summary>
public Dictionary<RecordId, ArcDirEntry> DirectoryEntries;
public Dictionary<RecordId, ArcDirEntry> DirectoryEntries = new();

/// <summary>
/// Holds the keys for the directoryEntries dictionary.
/// Ordered keys for the directoryEntries dictionary.
/// </summary>
public RecordId[] Keys;
public IEnumerable<RecordId> Keys => this.DirectoryEntries.Keys.OrderBy(v => v);

/// <summary>
/// Initializes a new instance of the ArcFile class.
Expand All @@ -42,7 +43,7 @@ public ArcFile(string fileName)
/// <summary>
/// Gets the number of Directory entries
/// </summary>
public int Count => this.DirectoryEntries?.Count ?? 0;
public int Count => this.DirectoryEntries.Count;

}
}
7 changes: 4 additions & 3 deletions src/TQVaultAE.Domain/Entities/ArzFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand All @@ -28,12 +29,12 @@ public class ArzFile
/// <summary>
/// RecordInfo keyed by their ID
/// </summary>
public Dictionary<RecordId, RecordInfo> RecordInfo = new Dictionary<RecordId, RecordInfo>();
public Dictionary<RecordId, RecordInfo> RecordInfo = new();

/// <summary>
/// Holds the keys for the recordInfo Dictionary
/// Ordered keys for the recordInfo Dictionary
/// </summary>
public RecordId[] Keys;
public IEnumerable<RecordId> Keys => this.RecordInfo.Keys.OrderBy(v => v);

/// <summary>
/// Initializes a new instance of the ArzFile class.
Expand Down
5 changes: 3 additions & 2 deletions src/TQVaultAE.Services.Win32/GamePathServiceWin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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))
Expand Down

0 comments on commit 632b023

Please sign in to comment.