Skip to content

Commit 480b5ba

Browse files
committed
More using of FileInfo and DirectoryInfo
1 parent 5f30530 commit 480b5ba

File tree

6 files changed

+49
-37
lines changed

6 files changed

+49
-37
lines changed

CollapseLauncher/Classes/CachesManagement/Honkai/Check.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using CollapseLauncher.Interfaces;
1+
using CollapseLauncher.Helper;
2+
using CollapseLauncher.Interfaces;
23
using Hi3Helper;
34
using System;
45
using System.Collections.Generic;
@@ -57,19 +58,31 @@ private async Task<List<CacheAsset>> Check(List<CacheAsset> assetIndex, Cancella
5758

5859
private void CheckUnusedAssets(List<CacheAsset> assetIndex, List<CacheAsset> returnAsset)
5960
{
61+
// Directory info and if the directory doesn't exist, return
62+
DirectoryInfo directoryInfo = new DirectoryInfo(_gamePath);
63+
if (!directoryInfo.Exists)
64+
{
65+
return;
66+
}
67+
6068
// Iterate the file contained in the _gamePath
61-
foreach (string filePath in Directory.EnumerateFiles(_gamePath!, "*", SearchOption.AllDirectories))
69+
foreach (FileInfo fileInfo in directoryInfo.EnumerateFiles("*", SearchOption.AllDirectories)
70+
.EnumerateNoReadOnly())
6271
{
63-
if (!filePath.Contains("output_log") && !filePath.Contains("Crashes")
64-
&& !filePath.Contains("Verify.txt") && !filePath.Contains("APM")
65-
&& !filePath.Contains("FBData") && !filePath.Contains("asb.dat")
66-
&& !assetIndex!.Exists(x => x!.ConcatPath == filePath))
72+
string filePath = fileInfo.FullName;
73+
74+
if (!filePath.Contains("output_log", StringComparison.OrdinalIgnoreCase)
75+
&& !filePath.Contains("Crashes", StringComparison.OrdinalIgnoreCase)
76+
&& !filePath.Contains("Verify.txt", StringComparison.OrdinalIgnoreCase)
77+
&& !filePath.Contains("APM", StringComparison.OrdinalIgnoreCase)
78+
&& !filePath.Contains("FBData", StringComparison.OrdinalIgnoreCase)
79+
&& !filePath.Contains("asb.dat", StringComparison.OrdinalIgnoreCase)
80+
&& !assetIndex.Exists(x => x.ConcatPath == fileInfo.FullName))
6781
{
6882
// Increment the total found count
6983
_progressAllCountFound++;
7084

7185
// Add asset to the returnAsset
72-
FileInfo fileInfo = new FileInfo(filePath);
7386
CacheAsset asset = new CacheAsset()
7487
{
7588
BasePath = Path.GetDirectoryName(filePath),
@@ -106,10 +119,10 @@ private async ValueTask CheckAsset(CacheAsset asset, List<CacheAsset> returnAsse
106119
_status.ActivityAll = string.Format(Lang._CachesPage.CachesTotalStatusChecking!, _progressAllCountCurrent, _progressAllCountTotal);
107120

108121
// Assign the file info.
109-
FileInfo fileInfo = new FileInfo(asset.ConcatPath!);
122+
FileInfo fileInfo = new FileInfo(asset.ConcatPath).EnsureNoReadOnly(out bool isExist);
110123

111124
// Check if the file exist. If not, then add it to asset index.
112-
if (!fileInfo.Exists)
125+
if (!isExist)
113126
{
114127
AddGenericCheckAsset(asset, CacheAssetStatus.New, returnAsset, null, asset.CRCArray);
115128
return;

CollapseLauncher/Classes/CachesManagement/Honkai/Update.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ private async Task UpdateCacheAsset((CacheAsset AssetIndex, IAssetProperty Asset
126126

127127
FileInfo fileInfo = new FileInfo(asset.AssetIndex.ConcatPath!)
128128
.EnsureCreationOfDirectory()
129-
.EnsureNoReadOnly();
129+
.EnsureNoReadOnly(out bool isExist);
130130

131131
// This is a action for Unused asset.
132132
if (asset.AssetIndex.DataType == CacheAssetType.Unused)
133133
{
134-
if (fileInfo.Exists)
134+
if (isExist)
135135
fileInfo.Delete();
136136

137137
LogWriteLine($"Deleted unused file: {fileInfo.FullName}", LogType.Default, true);

CollapseLauncher/Classes/CachesManagement/StarRail/Check.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Hi3Helper;
1+
using CollapseLauncher.Helper;
2+
using Hi3Helper;
23
using Hi3Helper.EncTool.Parser.AssetMetadata.SRMetadataAsset;
34
using System;
45
using System.Collections.Generic;
@@ -79,12 +80,12 @@ private async ValueTask CheckAsset(SRAsset asset, List<SRAsset> returnAsset, str
7980
}
8081

8182
// Get persistent and streaming paths
82-
FileInfo fileInfoPersistent = new FileInfo(Path.Combine(basePersistent!, asset.LocalName!));
83-
FileInfo fileInfoStreaming = new FileInfo(Path.Combine(baseStreaming!, asset.LocalName!));
83+
FileInfo fileInfoPersistent = new FileInfo(Path.Combine(basePersistent!, asset.LocalName!)).EnsureNoReadOnly(out bool isPersistentExist);
84+
FileInfo fileInfoStreaming = new FileInfo(Path.Combine(baseStreaming!, asset.LocalName!)).EnsureNoReadOnly(out bool isStreamingExist);
8485

85-
bool UsePersistent = !fileInfoStreaming.Exists;
86-
bool IsPersistentExist = fileInfoPersistent.Exists && fileInfoPersistent.Length == asset.Size;
87-
bool IsStreamingExist = fileInfoStreaming.Exists && fileInfoStreaming.Length == asset.Size;
86+
bool UsePersistent = !isStreamingExist;
87+
bool IsPersistentExist = isPersistentExist && fileInfoPersistent.Length == asset.Size;
88+
bool IsStreamingExist = isStreamingExist && fileInfoStreaming.Length == asset.Size;
8889
asset.LocalName = UsePersistent ? fileInfoPersistent.FullName : fileInfoStreaming.FullName;
8990

9091
// Check if the file exist. If not, then add it to asset index.

CollapseLauncher/Classes/FileMigrationProcess/FileMigrationProcess.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,21 @@ private async Task<string> StartRoutineInner(FileMigrationProcessUIRef uiRef)
9090

9191
private async Task<string> MoveFile(FileMigrationProcessUIRef uiRef)
9292
{
93-
FileInfo inputPathInfo = new FileInfo(this.inputPath!);
94-
FileInfo outputPathInfo = new FileInfo(this.outputPath!);
93+
FileInfo inputPathInfo = new FileInfo(inputPath);
94+
FileInfo outputPathInfo = new FileInfo(outputPath);
9595

9696
string inputPathDir = Path.GetDirectoryName(inputPathInfo.FullName);
9797
string outputPathDir = Path.GetDirectoryName(outputPathInfo.FullName);
9898

99-
if (!Directory.Exists(outputPathDir))
100-
Directory.CreateDirectory(outputPathDir!);
99+
DirectoryInfo inputPathDirInfo = new DirectoryInfo(inputPathDir);
100+
DirectoryInfo outputPathDirInfo = new DirectoryInfo(inputPathDir);
101+
outputPathDirInfo.Create();
101102

102103
// Update path display
103104
string inputFileBasePath = inputPathInfo.FullName.Substring(inputPathDir!.Length + 1);
104105
UpdateCountProcessed(uiRef, inputFileBasePath);
105106

106-
if (this.IsSameOutputDrive)
107+
if (IsSameOutputDrive)
107108
{
108109
Logger.LogWriteLine($"[FileMigrationProcess::MoveFile()] Moving file in the same drive from: {inputPathInfo.FullName} to {outputPathInfo.FullName}", LogType.Default, true);
109110
inputPathInfo.MoveTo(outputPathInfo.FullName, true);
@@ -112,29 +113,27 @@ private async Task<string> MoveFile(FileMigrationProcessUIRef uiRef)
112113
else
113114
{
114115
Logger.LogWriteLine($"[FileMigrationProcess::MoveFile()] Moving file across different drives from: {inputPathInfo.FullName} to {outputPathInfo.FullName}", LogType.Default, true);
115-
await MoveWriteFile(uiRef, inputPathInfo, outputPathInfo, this.tokenSource == null ? default : this.tokenSource.Token);
116+
await MoveWriteFile(uiRef, inputPathInfo, outputPathInfo, tokenSource == null ? default : tokenSource.Token);
116117
}
117118

118119
return outputPathInfo.FullName;
119120
}
120121

121122
private async Task<string> MoveDirectory(FileMigrationProcessUIRef uiRef)
122123
{
123-
DirectoryInfo inputPathInfo = new DirectoryInfo(this.inputPath!);
124-
if (!Directory.Exists(this.outputPath))
125-
Directory.CreateDirectory(this.outputPath!);
126-
127-
DirectoryInfo outputPathInfo = new DirectoryInfo(this.outputPath);
124+
DirectoryInfo inputPathInfo = new DirectoryInfo(inputPath);
125+
DirectoryInfo outputPathInfo = new DirectoryInfo(outputPath);
126+
outputPathInfo.Create();
128127

129128
int parentInputPathLength = inputPathInfo.Parent!.FullName.Length + 1;
130129
string outputDirBaseNamePath = inputPathInfo.FullName.Substring(parentInputPathLength);
131-
string outputDirPath = Path.Combine(this.outputPath, outputDirBaseNamePath);
130+
string outputDirPath = Path.Combine(outputPath, outputDirBaseNamePath);
132131

133132
await Parallel.ForEachAsync(
134133
inputPathInfo.EnumerateFiles("*", SearchOption.AllDirectories),
135134
new ParallelOptions
136135
{
137-
CancellationToken = this.tokenSource?.Token ?? default,
136+
CancellationToken = tokenSource?.Token ?? default,
138137
MaxDegreeOfParallelism = LauncherConfig.AppCurrentThread
139138
},
140139
async (inputFileInfo, cancellationToken) =>
@@ -147,9 +146,8 @@ await Parallel.ForEachAsync(
147146

148147
string outputTargetPath = Path.Combine(outputPathInfo.FullName, inputFileBasePath);
149148
string outputTargetDirPath = Path.GetDirectoryName(outputTargetPath);
150-
151-
if (!Directory.Exists(outputTargetDirPath))
152-
Directory.CreateDirectory(outputTargetDirPath!);
149+
DirectoryInfo outputTargetDirInfo = new DirectoryInfo(outputTargetDirPath);
150+
outputTargetDirInfo.Create();
153151

154152
if (this.IsSameOutputDrive)
155153
{

CollapseLauncher/Classes/FileMigrationProcess/IO.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ private async Task MoveWriteFile(FileMigrationProcessUIRef uiRef, FileInfo input
1717
{
1818
int bufferSize = 1 << 18; // 256 kB Buffer
1919

20-
if (inputFile!.Length < bufferSize)
20+
if (inputFile.Length < bufferSize)
2121
bufferSize = (int)inputFile.Length;
2222

2323
byte[] buffer = new byte[bufferSize];
2424

2525
await using (FileStream inputStream = inputFile.OpenRead())
26-
await using (FileStream outputStream = outputFile!.Exists && outputFile.Length <= inputFile.Length ?
26+
await using (FileStream outputStream = outputFile.Exists && outputFile.Length <= inputFile.Length ?
2727
outputFile.Open(FileMode.Open) : outputFile.Create())
2828
{
2929
// Set the output file size to inputStream's if the length is more than inputStream

CollapseLauncher/Classes/InstallManagement/BaseClass/InstallManagerBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,9 +991,9 @@ await Task.Run(() =>
991991
var filePath = new FileInfo(
992992
EnsureCreationOfDirectory(Path.Combine(_gamePath, assetName)) +
993993
"_tempSophon").EnsureNoReadOnly();
994-
var origFilePath = new FileInfo(Path.Combine(_gamePath, assetName)).EnsureNoReadOnly();
994+
var origFilePath = new FileInfo(Path.Combine(_gamePath, assetName)).EnsureNoReadOnly(out bool isExist);
995995

996-
if (filePath.Exists)
996+
if (isExist)
997997
{
998998
filePath.MoveTo(origFilePath.FullName, true);
999999
filePath.Refresh();

0 commit comments

Comments
 (0)