-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathStarRailInstall.SophonPatch.cs
More file actions
132 lines (109 loc) · 4.46 KB
/
StarRailInstall.SophonPatch.cs
File metadata and controls
132 lines (109 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using Hi3Helper;
using Hi3Helper.Data;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
// ReSharper disable InvertIf
#pragma warning disable IDE0130
#nullable enable
namespace CollapseLauncher.InstallManager.StarRail
{
internal sealed partial class StarRailInstall
{
public override async Task FilterAssetList<T>(
List<T> itemList,
Func<T, string?> itemPathSelector,
CancellationToken token)
{
string blackListFilePath = Path.Combine(GamePath, @"StarRail_Data\Persistent\DownloadBlacklist.json");
FileInfo fileInfo = new(blackListFilePath);
if (!fileInfo.Exists)
{
return;
}
HashSet<string> blackList = new(StringComparer.OrdinalIgnoreCase);
HashSet<string>.AlternateLookup<ReadOnlySpan<char>> blackListAlt = blackList.GetAlternateLookup<ReadOnlySpan<char>>();
using StreamReader reader = new(fileInfo.OpenRead());
while (await reader.ReadLineAsync(token) is { } line)
{
ReadOnlySpan<char> span = GetFilePathFromJson(line);
if (span.IsEmpty)
{
continue;
}
// Normalize path
ConverterTool.NormalizePathInplaceNoTrim(span);
AddBothPersistentOrStreamingAssets(blackListAlt, span);
}
if (blackList.Count == 0)
{
return;
}
SearchValues<string> searchValues =
SearchValues.Create(blackList.ToArray(), StringComparison.OrdinalIgnoreCase);
List<T> listFiltered = [];
foreach (T patchAsset in itemList)
{
if (itemPathSelector(patchAsset) is not {} filePath)
{
listFiltered.Add(patchAsset);
continue;
}
ConverterTool.NormalizePathInplaceNoTrim(filePath);
int indexOfAny = filePath.IndexOfAny(searchValues);
if (indexOfAny >= 0)
{
Logger.LogWriteLine($"[StarRailInstall::FilterAssetList] Asset: {patchAsset} is ignored due to marked as deleted asset.",
writeToLog: true);
continue;
}
listFiltered.Add(patchAsset);
}
itemList.Clear();
itemList.AddRange(listFiltered);
return;
static ReadOnlySpan<char> GetFilePathFromJson(ReadOnlySpan<char> line)
{
const string first = "\"fileName\":\"";
const char end = '\"';
int firstIndexOf = line.IndexOf(first);
if (firstIndexOf <= 0)
{
return ReadOnlySpan<char>.Empty;
}
line = line[(firstIndexOf + first.Length)..];
int endIndexOf = line.IndexOf(end);
return endIndexOf <= 0
? ReadOnlySpan<char>.Empty
: line[..endIndexOf];
}
}
private static void AddBothPersistentOrStreamingAssets(
HashSet<string>.AlternateLookup<ReadOnlySpan<char>> hashList,
ReadOnlySpan<char> filePath)
{
const string streamingAssetsSegment = @"StarRail_Data\StreamingAssets\";
const string persistentSegment = @"StarRail_Data\Persistent\";
bool isContainStreamingAssets = filePath.Contains(streamingAssetsSegment, StringComparison.OrdinalIgnoreCase);
bool isContainPersistent = filePath.Contains(persistentSegment, StringComparison.OrdinalIgnoreCase);
// Add original path
hashList.Add(filePath);
if (isContainStreamingAssets)
{
string persistentPath = persistentSegment
+ filePath[streamingAssetsSegment.Length..].ToString();
hashList.Add(persistentPath);
}
if (isContainPersistent)
{
string streamingAssetsPath = streamingAssetsSegment
+ filePath[persistentSegment.Length..].ToString();
hashList.Add(streamingAssetsPath);
}
}
}
}