Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizations #506

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 13 additions & 1 deletion src/TQVaultAE.Data/ItemProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,19 @@ public class ItemProvider : IItemProvider
"CONTAGIONMAXSPREAD",
"CONTAGIONRADIUS",
"NOHIGHLIGHTDEFAULTCOLORA", // AMS: New property on most EE items
"FORCEIGNORERUNSPEEDCAPS" // hguy: New property on EE "Potion of Speed"
"FORCEIGNORERUNSPEEDCAPS", // hguy: New property on EE "Potion of Speed"
"LOOTRANDOMIZERSCALE",
"PROJECTILEFRAGMENTSLAUNCHNUMBERMAX",
"PROJECTILEFRAGMENTSLAUNCHNUMBERMIN",
"SPAWNOBJECTSRANDOMROTATION",
"SKILLPROJECTILETARGETGROUNDONLY",
"OFFENSIVETOTALDAMAGEGLOBAL",
"OFFENSIVETOTALDAMAGEXOR",
// hguy : HCDUNGEON ITEMS
"SKILLALLOWSWARMUP",
"ONHITACTIVATIONCHANCE",
"DECREMENTSTATTYPE",
"ALLSKILLENHANCEMENT",
};

internal static readonly string[] requirementTags =
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
27 changes: 25 additions & 2 deletions src/TQVaultAE.Domain/Entities/RecordId.ForItems.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
using System;
using System.Linq;

namespace TQVaultAE.Domain.Entities;

public partial class RecordId
{

#region IsHCDungeonEE

bool? _IsHardCoreDungeonEE;
/// <summary>
/// This <see cref="RecordId"/> leads to the EE Hardcore Dungeon.
/// </summary>
public bool IsHardCoreDungeonEE
{
get
{
if (_IsHardCoreDungeonEE is null)
_IsHardCoreDungeonEE = this.Normalized.Contains(@"\HCDUNGEON\");
return _IsHardCoreDungeonEE.Value;
}
}

#endregion

#region IsRelic

private readonly string[] HCDungeonRelic = new[] { "03_X4_ESSENCEOFORDER_CHARM", "03_X4_ESSENCEOFCHAOS" };

bool? _IsRelic;
/// <summary>
/// This <see cref="RecordId"/> leads to a Relic content.
Expand All @@ -17,7 +38,8 @@ public bool IsRelic
{
if (_IsRelic is null)
_IsRelic = (this.Dlc == GameDlc.TitanQuest && this.Normalized.Contains(@"RELICS") && !IsCharm) // Is base game
|| this.Normalized.Contains(@"\RELICS\");// Is part of an extension
|| this.Normalized.Contains(@"\RELICS\")// Is part of an extension
|| (this.IsHardCoreDungeonEE && HCDungeonRelic.Any(n => this.Normalized.Contains(n)));// items that break the rule
return _IsRelic.Value;
}
}
Expand All @@ -36,7 +58,8 @@ public bool IsCharm
{
if (_IsCharm is null)
_IsCharm = (this.Dlc == GameDlc.TitanQuest && this.Normalized.Contains(@"ANIMALRELICS")) // Is base game
|| this.Normalized.Contains(@"\CHARMS\");// Is part of an extension
|| this.Normalized.Contains(@"\CHARMS\")// Is part of an extension
|| (this.IsHardCoreDungeonEE && this.Normalized.Contains(@"GOLDENSCARAB"));// items that break the rule
return _IsCharm.Value;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/TQVaultAE.Domain/Entities/RecordId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public GameDlc Dlc
if (_Dlc is null)
_Dlc = this.Normalized switch
{
var x when x.Contains(@"\XPACK4\") => GameDlc.EternalEmbers,
var x when x.Contains(@"\XPACK4\") || this.IsHardCoreDungeonEE => GameDlc.EternalEmbers,
var x when x.Contains(@"\XPACK3\") => GameDlc.Atlantis,
var x when x.Contains(@"\XPACK2\") => GameDlc.Ragnarok,
var x when x.Contains(@"\XPACK\") => GameDlc.ImmortalThrone,
Expand Down
2 changes: 1 addition & 1 deletion src/TQVaultAE.Domain/Entities/SessionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public void FindHighlight()
availableItems = availableItems.Where(i => i.FriendlyNames.ItemSet != null);
}

this.HighlightedItems.AddRange(availableItems.Select(i => i.Item));
this.HighlightedItems.AddRange(availableItems.Select(i => i.Item).ToList());
return;
}
ResetHighlight();
Expand Down
Loading