Repository navigation
Expand file tree
/
Copy pathThreadWorkerViewModel.cs
More file actions
138 lines (123 loc) · 4.66 KB
/
Copy pathThreadWorkerViewModel.cs
File metadata and controls
138 lines (123 loc) · 4.66 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
133
134
135
136
137
138
using System;
using System.Threading;
using System.Threading.Tasks;
using CUE4Parse.UE4.Exceptions;
using FModel.Framework;
using FModel.Services;
using FModel.Views.Resources.Controls;
using Serilog;
namespace FModel.ViewModels;
public class ThreadWorkerViewModel : ViewModel
{
private bool _statusChangeAttempted;
public bool StatusChangeAttempted
{
get => _statusChangeAttempted;
private set => SetProperty(ref _statusChangeAttempted, value);
}
private bool _operationCancelled;
public bool OperationCancelled
{
get => _operationCancelled;
private set => SetProperty(ref _operationCancelled, value);
}
private CancellationTokenSource _currentCancellationTokenSource;
public CancellationTokenSource CurrentCancellationTokenSource
{
get => _currentCancellationTokenSource;
set
{
if (_currentCancellationTokenSource == value) return;
SetProperty(ref _currentCancellationTokenSource, value);
RaisePropertyChanged("CanBeCanceled");
}
}
public bool CanBeCanceled => CurrentCancellationTokenSource != null;
private ApplicationViewModel _applicationView => ApplicationService.ApplicationView;
private readonly AsyncQueue<Action<CancellationToken>> _jobs;
public ThreadWorkerViewModel()
{
_jobs = new AsyncQueue<Action<CancellationToken>>();
}
public async Task Begin(Action<CancellationToken> action)
{
if (_applicationView.CUE4Parse.IsSnooperOpen)
_applicationView.CUE4Parse.SnooperViewer.Close();
else if (!_applicationView.Status.IsReady)
{
SignalOperationInProgress();
return;
}
CurrentCancellationTokenSource ??= new CancellationTokenSource();
_jobs.Enqueue(action);
await ProcessQueues();
}
public void Cancel()
{
if (!CanBeCanceled)
{
SignalOperationInProgress();
return;
}
CurrentCancellationTokenSource.Cancel();
}
private async Task ProcessQueues()
{
if (_jobs.Count > 0)
{
_applicationView.Status.SetStatus(EStatusKind.Loading);
await foreach (var job in _jobs)
{
try
{
// will end in "catch" if canceled
await Task.Run(() => job(CurrentCancellationTokenSource.Token));
}
catch (OperationCanceledException)
{
_applicationView.Status.SetStatus(EStatusKind.Stopped);
if (_applicationView.CUE4Parse.IsSnooperOpen)
_applicationView.CUE4Parse.SnooperViewer.Close();
CurrentCancellationTokenSource = null; // kill token
OperationCancelled = true;
OperationCancelled = false;
return;
}
catch (Exception e)
{
_applicationView.Status.SetStatus(EStatusKind.Failed);
CurrentCancellationTokenSource = null; // kill token
Log.Error("{Exception}", e);
switch (e)
{
case MappingException:
FLogger.Append(ELog.Error, () =>
{
FLogger.Text("Package has unversioned properties but mapping file (.usmap) is missing, can't serialize. See: ", Constants.WHITE);
FLogger.Link("→ link ←", Constants.MAPPING_ISSUE_LINK, true);
});
break;
case VersionException v: // Error might be unrelated to version, but it's usually the case
FLogger.Append(ELog.Error, () =>
{
FLogger.Text(v.Message[..^1] + ", can't serialize. Make sure the correct UE version is configured. See: ", Constants.WHITE);
FLogger.Link("→ link ←", Constants.VERSION_ISSUE_LINK, true);
});
break;
default:
FLogger.Append(e);
break;
}
return;
}
}
_applicationView.Status.SetStatus(EStatusKind.Completed);
CurrentCancellationTokenSource = null; // kill token
}
}
public void SignalOperationInProgress()
{
StatusChangeAttempted = true;
StatusChangeAttempted = false;
}
}