-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathAbstractStrategy.cs
More file actions
130 lines (117 loc) · 4.72 KB
/
Copy pathAbstractStrategy.cs
File metadata and controls
130 lines (117 loc) · 4.72 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
using System;
using System.IO;
using System.Threading.Tasks;
using GeneralUpdate.Core.FileSystem;
using GeneralUpdate.Core.Event;
using GeneralUpdate.Core.Pipeline;
using GeneralUpdate.Core;
using GeneralUpdate.Core.Configuration;
using GeneralUpdate.Core.Network;
namespace GeneralUpdate.Core.Strategy
{
public abstract class AbstractStrategy : IStrategy
{
private const string Patchs = "patchs";
protected GlobalConfigInfo _configinfo = new();
public virtual void Execute() => throw new NotImplementedException();
public virtual void StartApp() => throw new NotImplementedException();
public virtual async Task ExecuteAsync()
{
try
{
var status = ReportType.None;
var patchPath = StorageManager.GetTempDirectory(Patchs);
foreach (var version in _configinfo.UpdateVersions)
{
try
{
var context = CreatePipelineContext(version, patchPath);
var pipelineBuilder = BuildPipeline(context);
await pipelineBuilder.Build();
status = ReportType.Success;
}
catch (Exception e)
{
status = ReportType.Failure;
HandleExecuteException(e);
}
finally
{
await VersionService.Report(_configinfo.ReportUrl
, version.RecordId
, status
, version.AppType
, _configinfo.Scheme
, _configinfo.Token);
}
}
Clear(patchPath);
Clear(_configinfo.TempPath);
OnExecuteComplete();
}
catch (Exception e)
{
HandleExecuteException(e);
}
}
public virtual void Create(GlobalConfigInfo parameter) => _configinfo = parameter;
/// <summary>
/// Creates the pipeline context with common and platform-specific parameters.
/// Override this method to add platform-specific context parameters.
/// </summary>
protected virtual PipelineContext CreatePipelineContext(VersionInfo version, string patchPath)
{
var context = new PipelineContext();
// Common parameters
context.Add("ZipFilePath", Path.Combine(_configinfo.TempPath, $"{version.Name}{_configinfo.Format}"));
// Hash middleware
context.Add("Hash", version.Hash);
// Zip middleware
context.Add("Format", _configinfo.Format);
context.Add("Name", version.Name);
context.Add("Encoding", _configinfo.Encoding);
// Patch middleware
context.Add("SourcePath", _configinfo.InstallPath);
context.Add("PatchPath", patchPath);
context.Add("PatchEnabled", _configinfo.PatchEnabled);
return context;
}
/// <summary>
/// Builds the pipeline with middleware components.
/// Override this method to customize the pipeline for specific platforms.
/// </summary>
protected abstract PipelineBuilder BuildPipeline(PipelineContext context);
/// <summary>
/// Called after ExecuteAsync completes successfully.
/// Override this method to add platform-specific post-execution logic.
/// </summary>
protected virtual void OnExecuteComplete()
{
// Default implementation does nothing
}
/// <summary>
/// Handles exceptions during execution.
/// Override this method to customize error handling.
/// </summary>
protected virtual void HandleExecuteException(Exception e)
{
GeneralTracer.Error($"Strategy execution exception.", e);
EventManager.Instance.Dispatch(this, new ExceptionEventArgs(e, e.Message));
}
/// <summary>
/// Checks if a file exists at the specified path.
/// </summary>
protected static string CheckPath(string path, string name)
{
if (string.IsNullOrWhiteSpace(path) || string.IsNullOrWhiteSpace(name))
return string.Empty;
var tempPath = Path.Combine(path, name);
return File.Exists(tempPath) ? tempPath : string.Empty;
}
private static void Clear(string path)
{
if (Directory.Exists(path))
StorageManager.DeleteDirectory(path);
}
}
}