Skip to content

Commit e19716c

Browse files
committed
Improve compatibility with older builds
1 parent e9b7f18 commit e19716c

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed

TACTSharp/Build.cs

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,35 +83,48 @@ public void Load()
8383
timer.Stop();
8484
Console.WriteLine("Group index loaded in " + Math.Ceiling(timer.Elapsed.TotalMilliseconds) + "ms");
8585

86-
timer.Restart();
87-
if (!CDNConfig.Values.TryGetValue("file-index", out var fileIndex))
88-
throw new Exception("No file index found in CDN config");
89-
90-
if (!string.IsNullOrEmpty(Settings.BaseDir) && File.Exists(Path.Combine(Settings.BaseDir, "Data", "indices", fileIndex[0] + ".index")))
86+
ulong decodedEncodingSize = 0;
87+
ulong encodedEncodingSize = 0;
88+
if (BuildConfig.Values.TryGetValue("encoding-size", out var bcEncodingSize))
9189
{
92-
FileIndex = new IndexInstance(Path.Combine(Settings.BaseDir, "Data", "indices", fileIndex[0] + ".index"));
90+
decodedEncodingSize = ulong.Parse(bcEncodingSize[0]);
91+
encodedEncodingSize = ulong.Parse(bcEncodingSize[1]);
9392
}
94-
else
95-
{
96-
try
97-
{
98-
var fileIndexPath = cdn.GetFilePath("data", fileIndex[0] + ".index");
99-
FileIndex = new IndexInstance(fileIndexPath);
10093

101-
}catch(Exception e)
94+
timer.Restart();
95+
Encoding = new EncodingInstance(cdn.GetDecodedFilePath("data", BuildConfig.Values["encoding"][1], encodedEncodingSize, decodedEncodingSize), (int)decodedEncodingSize);
96+
timer.Stop();
97+
Console.WriteLine("Encoding loaded in " + Math.Ceiling(timer.Elapsed.TotalMilliseconds) + "ms");
98+
99+
timer.Restart();
100+
if (CDNConfig.Values.TryGetValue("file-index", out var fileIndex))
101+
{
102+
if (!string.IsNullOrEmpty(Settings.BaseDir) && File.Exists(Path.Combine(Settings.BaseDir, "Data", "indices", fileIndex[0] + ".index")))
102103
{
103-
Console.WriteLine("Failed to load file index: " + e.Message);
104+
FileIndex = new IndexInstance(Path.Combine(Settings.BaseDir, "Data", "indices", fileIndex[0] + ".index"));
104105
}
105-
}
106+
else
107+
{
108+
try
109+
{
110+
var fileIndexPath = cdn.GetFilePath("data", fileIndex[0] + ".index");
111+
FileIndex = new IndexInstance(fileIndexPath);
106112

107-
timer.Stop();
108-
Console.WriteLine("File index loaded in " + Math.Ceiling(timer.Elapsed.TotalMilliseconds) + "ms");
113+
}
114+
catch (Exception e)
115+
{
116+
Console.WriteLine("Failed to load file index: " + e.Message);
117+
}
118+
}
109119

110-
var encodingSize = ulong.Parse(BuildConfig.Values["encoding-size"][0]);
111-
timer.Restart();
112-
Encoding = new EncodingInstance(cdn.GetDecodedFilePath("data", BuildConfig.Values["encoding"][1], ulong.Parse(BuildConfig.Values["encoding-size"][1]), encodingSize), (int)encodingSize);
113-
timer.Stop();
114-
Console.WriteLine("Encoding loaded in " + Math.Ceiling(timer.Elapsed.TotalMilliseconds) + "ms");
120+
timer.Stop();
121+
Console.WriteLine("File index loaded in " + Math.Ceiling(timer.Elapsed.TotalMilliseconds) + "ms");
122+
}
123+
else
124+
{
125+
// TODO: We might want to manually build up file-index based on encoding entries not present in indexes.
126+
Console.WriteLine("No file index found in CDN config, skipping file index loading.");
127+
}
115128

116129
timer.Restart();
117130
if (!BuildConfig.Values.TryGetValue("root", out var rootKey))
@@ -144,7 +157,7 @@ public byte[] OpenFileByFDID(uint fileDataID)
144157
throw new Exception("Root not loaded");
145158

146159
var fileData = Root.GetEntriesByFDID(fileDataID);
147-
if(fileData.Count == 0)
160+
if (fileData.Count == 0)
148161
throw new Exception("File not found in root");
149162

150163
return OpenFileByCKey(fileData[0].md5.AsSpan());
@@ -176,7 +189,7 @@ public byte[] OpenFileByEKey(ReadOnlySpan<byte> eKey, ulong decodedSize = 0)
176189

177190
if (offset == -1)
178191
{
179-
if(FileIndex != null)
192+
if (FileIndex != null)
180193
{
181194
var fileIndexEntry = FileIndex.GetIndexInfo(eKey);
182195
if (fileIndexEntry.size != -1)

TACTSharp/EncodingInstance.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ public class EncodingInstance
2222

2323
public static readonly EncodingResult Zero = new(0, [], 0);
2424

25-
public EncodingInstance(string filePath, int fileSize)
25+
public EncodingInstance(string filePath, int fileSize = 0)
2626
{
2727
_filePath = filePath;
28-
_fileSize = fileSize;
28+
29+
if (fileSize != 0)
30+
_fileSize = fileSize;
31+
else
32+
_fileSize = (int)new FileInfo(filePath).Length;
2933

3034
this.encodingFile = MemoryMappedFile.CreateFromFile(filePath, FileMode.Open, null, 0, MemoryMappedFileAccess.Read);
3135
this.accessor = encodingFile.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read);

0 commit comments

Comments
 (0)