-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
242 additions
and
57 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
# Changelog v0.4 | ||
|
||
## v0.4.5 | ||
|
||
- **优化**增加主窗体初始尺寸 | ||
- **新增**更新整合包到指定版本(#20) | ||
|
||
## v0.4.4 | ||
|
||
- **修复**下载中心添加整合包将不再崩溃 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 140 additions & 1 deletion
141
src/Polymerium.App/ViewModels/InstanceUpdateViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,144 @@ | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using Microsoft.UI.Xaml.Controls; | ||
using Polymerium.Abstractions; | ||
using Polymerium.Abstractions.ResourceResolving; | ||
using Polymerium.App.Services; | ||
using Polymerium.App.Views; | ||
using Polymerium.Core; | ||
using Polymerium.Core.Engines; | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using RFile = Polymerium.Abstractions.Resources.File; | ||
|
||
namespace Polymerium.App.ViewModels; | ||
|
||
public class InstanceUpdateViewModel : ObservableObject { } | ||
public class InstanceUpdateViewModel : ObservableObject | ||
{ | ||
private readonly ImportService _importer; | ||
private readonly ResolveEngine _resolver; | ||
private readonly IFileBaseService _fileBase; | ||
private readonly LocalizationService _localizationService; | ||
private readonly INotificationService _notification; | ||
public readonly NavigationService NavigationService; | ||
|
||
public InstanceUpdateViewModel( | ||
ImportService importer, | ||
ResolveEngine resolver, | ||
IFileBaseService fileBase, | ||
LocalizationService localizationService, | ||
INotificationService notification, | ||
NavigationService navigationService | ||
) | ||
{ | ||
_importer = importer; | ||
_resolver = resolver; | ||
_fileBase = fileBase; | ||
_localizationService = localizationService; | ||
_notification = notification; | ||
NavigationService = navigationService; | ||
} | ||
|
||
public async Task ApplyUpdateAsync( | ||
GameInstance instance, | ||
Uri reference, | ||
bool resetLocal, | ||
Action callback | ||
) | ||
{ | ||
var context = new ResolverContext(instance); | ||
var fileResult = await _resolver.ResolveToFileAsync(reference, context); | ||
if (fileResult.IsSuccessful && fileResult.Value.Resource is RFile file) | ||
{ | ||
try | ||
{ | ||
var client = new HttpClient(); | ||
using var stream = await client.GetStreamAsync(file.Source); | ||
var tmpFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); | ||
if (!Directory.Exists(Path.GetDirectoryName(tmpFile))) | ||
Directory.CreateDirectory(Path.GetDirectoryName(tmpFile)!); | ||
var fs = new FileStream(tmpFile, FileMode.Create, FileAccess.Write); | ||
await stream.CopyToAsync(fs); | ||
await fs.FlushAsync(); | ||
fs.Close(); | ||
var importResult = await _importer.ExtractMetadataFromFileAsync( | ||
tmpFile, | ||
reference, | ||
false | ||
); | ||
if (importResult.IsSuccessful) | ||
{ | ||
var oldAttachments = instance.Metadata.Attachments | ||
.Where(x => x.From == instance.ReferenceSource) | ||
.ToList(); | ||
if (resetLocal) | ||
{ | ||
var resolveResult = await _resolver.ResolveAsync( | ||
oldAttachments.Select(x => x.Source), | ||
context | ||
); | ||
if (resolveResult.IsSuccessful) | ||
{ | ||
foreach (var localResult in resolveResult.Value) | ||
{ | ||
if (localResult.Resource is RFile localFile) | ||
{ | ||
var localPath = _fileBase.Locate( | ||
new Uri( | ||
new Uri( | ||
ConstPath.INSTANCE_BASE.Replace("{0}", instance.Id) | ||
), | ||
localFile.FileName | ||
) | ||
); | ||
if (File.Exists(localPath)) | ||
{ | ||
File.Delete(localPath); | ||
} | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
EndedError($"应用本地文件错误: {importResult.Error}"); | ||
} | ||
} | ||
var postError = await _importer.SolidifyAsync(importResult.Value, instance); | ||
if (postError.HasValue) | ||
{ | ||
EndedError($"导入文件错误: {postError.Value}"); | ||
} | ||
else | ||
{ | ||
_notification.Enqueue( | ||
"更新完成", | ||
$"版本更新到 {file.Name}", | ||
InfoBarSeverity.Success | ||
); | ||
} | ||
} | ||
else | ||
{ | ||
EndedError($"提取信息错误: {importResult.Error}"); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
EndedError($"文件系统错误: {e.Message}"); | ||
} | ||
} | ||
else | ||
{ | ||
EndedError($"解析信息错误: {fileResult.Error}"); | ||
} | ||
callback(); | ||
} | ||
|
||
private void EndedError(string message) | ||
{ | ||
_notification.Enqueue("更新失败", message, InfoBarSeverity.Error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.