From ec523c4a12ab1fd32d6838d43ecbd2297860f4b6 Mon Sep 17 00:00:00 2001
From: Eric-Joker <60310368+Eric-Joker@users.noreply.github.com>
Date: Tue, 24 Sep 2024 19:58:36 +0800
Subject: [PATCH 1/2] fix: GetBasicPropertiesAsync() causes inexplicable
occasional crashes (#1101)
---
src/Starward/Services/Cache/CacheBase.cs | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/src/Starward/Services/Cache/CacheBase.cs b/src/Starward/Services/Cache/CacheBase.cs
index fe5c4a6b4..574b65373 100644
--- a/src/Starward/Services/Cache/CacheBase.cs
+++ b/src/Starward/Services/Cache/CacheBase.cs
@@ -162,7 +162,7 @@ public async Task RemoveExpiredAsync(TimeSpan? duration = null)
continue;
}
- if (await IsFileOutOfDateAsync(file, expiryDuration, false).ConfigureAwait(false))
+ if (IsFileOutOfDateAsync(file, expiryDuration, false))
{
filesToDelete.Add(file);
}
@@ -315,16 +315,17 @@ public Task PreCacheAsync(Uri uri, bool throwOnError = false, bool storeToMemory
/// cache duration
/// option to mark uninitialized file as expired
/// bool indicate whether file has expired or not
- protected virtual async Task IsFileOutOfDateAsync(StorageFile file, TimeSpan duration, bool treatNullFileAsOutOfDate = true)
+ protected virtual bool IsFileOutOfDateAsync(StorageFile file, TimeSpan duration, bool treatNullFileAsOutOfDate = true)
{
if (file == null)
{
return treatNullFileAsOutOfDate;
}
- var properties = await file.GetBasicPropertiesAsync().AsTask().ConfigureAwait(false);
+ // Fix #1097: StorageFile.GetBasicPropertiesAsync() causes inexplicable occasional crashes
+ var fileInfo = new FileInfo(file.Path);
- return properties.Size == 0 || DateTime.Now.Subtract(properties.DateModified.DateTime) > duration;
+ return fileInfo.Length == 0 || DateTime.Now.Subtract(fileInfo.LastWriteTime) > duration;
}
///
@@ -409,7 +410,7 @@ protected virtual string GetCacheFileName(Uri uri)
var folder = await GetCacheFolderAsync().ConfigureAwait(false);
var baseFile = await folder.TryGetItemAsync(fileName) as StorageFile;
- bool downloadDataFile = baseFile == null || await IsFileOutOfDateAsync(baseFile, CacheDuration).ConfigureAwait(false);
+ bool downloadDataFile = baseFile == null || IsFileOutOfDateAsync(baseFile, CacheDuration);
if (baseFile == null)
{
@@ -457,9 +458,9 @@ protected virtual string GetCacheFileName(Uri uri)
if (_inMemoryFileStorage.MaxItemCount > 0)
{
- var properties = await baseFile.GetBasicPropertiesAsync().AsTask().ConfigureAwait(false);
+ var fileInfo = new FileInfo(baseFile.Path);
- var msi = new InMemoryStorageItem(fileName, properties.DateModified.DateTime, instance);
+ var msi = new InMemoryStorageItem(fileName, fileInfo.LastWriteTime, instance);
_inMemoryFileStorage.SetItem(msi);
}
}
From 655df9a489cd7f5965d2c9dfc724c89678b2d82c Mon Sep 17 00:00:00 2001
From: Eric-Joker <60310368+Eric-Joker@users.noreply.github.com>
Date: Wed, 25 Sep 2024 12:57:26 +0800
Subject: [PATCH 2/2] fix ArgumentNullException (#1102)
---
src/Starward/Services/Download/InstallGameService.cs | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/src/Starward/Services/Download/InstallGameService.cs b/src/Starward/Services/Download/InstallGameService.cs
index db739e782..980d610ac 100644
--- a/src/Starward/Services/Download/InstallGameService.cs
+++ b/src/Starward/Services/Download/InstallGameService.cs
@@ -625,8 +625,8 @@ public void ClearState()
- protected List _taskItems;
- public List TaskItems => _taskItems;
+ protected Task[] _taskItems;
+ public Task[] TaskItems => _taskItems;
@@ -726,14 +726,14 @@ protected void StartTask(InstallGameState state)
async Task RunTasksAsync()
{
_cancellationTokenSource = new CancellationTokenSource();
- var tasks = new Task[Environment.ProcessorCount];
+ _taskItems = new Task[Environment.ProcessorCount];
for (int i = 0; i < Environment.ProcessorCount; i++)
{
- tasks[i] = ExecuteTaskItemAsync(_cancellationTokenSource.Token);
+ _taskItems[i] = ExecuteTaskItemAsync(_cancellationTokenSource.Token);
}
try
{
- await Task.WhenAll(tasks).ConfigureAwait(false);
+ await Task.WhenAll(_taskItems).ConfigureAwait(false);
}
finally
{