-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathAbstractBootstrap.cs
More file actions
171 lines (148 loc) · 6.82 KB
/
Copy pathAbstractBootstrap.cs
File metadata and controls
171 lines (148 loc) · 6.82 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using GeneralUpdate.Core.Strategy;
using GeneralUpdate.Core;
using GeneralUpdate.Core.Configuration;
using GeneralUpdate.Differential.Abstractions;
namespace GeneralUpdate.Core.Configuration
{
public abstract class AbstractBootstrap<TBootstrap, TStrategy>
where TBootstrap : AbstractBootstrap<TBootstrap, TStrategy>
where TStrategy : IStrategy
{
private readonly ConcurrentDictionary<UpdateOption, UpdateOptionValue> _options;
/// <summary>User-registered extension types for lazy instantiation.</summary>
private readonly Dictionary<Type, Type> _extensions = new();
/// <summary>Registered singleton instances (e.g., BlackListConfig).</summary>
private readonly Dictionary<Type, object> _instances = new();
protected internal AbstractBootstrap()
{
_options = new ConcurrentDictionary<UpdateOption, UpdateOptionValue>();
}
public abstract Task<TBootstrap> LaunchAsync();
public TBootstrap Option<T>(UpdateOption<T> option, T value)
{
if (value == null)
_options.TryRemove(option, out _);
else
_options[option] = new UpdateOptionValue<T>(option, value);
return (TBootstrap)this;
}
protected T GetOption<T>(UpdateOption<T>? option)
{
if (option == null) return default!;
if (_options.TryGetValue(option, out var val) && val != null)
return (T)val.GetValue();
return option.DefaultValue;
}
// ═══════════ Extension point registration ═══════════
public TBootstrap Strategy<T>() where T : IStrategy, new()
{
_extensions[typeof(IStrategy)] = typeof(T);
return (TBootstrap)this;
}
public TBootstrap Hooks<T>() where T : Hooks.IUpdateHooks, new()
{
_extensions[typeof(Hooks.IUpdateHooks)] = typeof(T);
return (TBootstrap)this;
}
public TBootstrap SslPolicy<T>() where T : Security.ISslValidationPolicy, new()
{
_extensions[typeof(Security.ISslValidationPolicy)] = typeof(T);
return (TBootstrap)this;
}
/// <summary>
/// Registers a custom retry/timeout policy for download operations.
/// Only effective when using the default download orchestrator.
/// If <see cref="DownloadOrchestrator{T}"/> is also set, this is ignored.
/// </summary>
public TBootstrap DownloadPolicy<T>() where T : Download.Abstractions.IDownloadPolicy, new()
{
_extensions[typeof(Download.Abstractions.IDownloadPolicy)] = typeof(T);
return (TBootstrap)this;
}
/// <summary>
/// Registers a custom single-file download executor (e.g. for non-HTTP protocols).
/// Only effective when using the default download orchestrator.
/// If <see cref="DownloadOrchestrator{T}"/> is also set, this is ignored.
/// </summary>
public TBootstrap DownloadExecutor<T>() where T : Download.Abstractions.IDownloadExecutor, new()
{
_extensions[typeof(Download.Abstractions.IDownloadExecutor)] = typeof(T);
return (TBootstrap)this;
}
public TBootstrap DownloadSource<T>() where T : Download.Abstractions.IDownloadSource, new()
{
_extensions[typeof(Download.Abstractions.IDownloadSource)] = typeof(T);
return (TBootstrap)this;
}
/// <summary>
/// Registers a custom post-download processing pipeline (hash verification, decryption, virus scan).
/// Only effective when using the default download orchestrator.
/// If <see cref="DownloadOrchestrator{T}"/> is also set, this is ignored.
/// </summary>
public TBootstrap DownloadPipeline<T>() where T : Download.Abstractions.IDownloadPipeline, new()
{
_extensions[typeof(Download.Abstractions.IDownloadPipeline)] = typeof(T);
return (TBootstrap)this;
}
public TBootstrap UpdateReporter<T>() where T : Download.Reporting.IUpdateReporter, new()
{
_extensions[typeof(Download.Reporting.IUpdateReporter)] = typeof(T);
return (TBootstrap)this;
}
public TBootstrap UpdateAuth<T>() where T : Security.IHttpAuthProvider, new()
{
_extensions[typeof(Security.IHttpAuthProvider)] = typeof(T);
return (TBootstrap)this;
}
/// <summary>
/// Registers a custom download orchestrator that handles batch downloads end-to-end.
/// This is the top-level download abstraction. When set, <see cref="DownloadPolicy{T}"/>,
/// <see cref="DownloadExecutor{T}"/>, and <see cref="DownloadPipeline{T}"/> are ignored
/// — the custom orchestrator owns the entire download pipeline.
/// </summary>
public TBootstrap DownloadOrchestrator<T>() where T : Download.Abstractions.IDownloadOrchestrator, new()
{
_extensions[typeof(Download.Abstractions.IDownloadOrchestrator)] = typeof(T);
return (TBootstrap)this;
}
public TBootstrap DirtyStrategy<T>() where T : Differential.IDirtyStrategy, new()
{
_extensions[typeof(Differential.IDirtyStrategy)] = typeof(T);
return (TBootstrap)this;
}
public TBootstrap BinaryDiffer<T>() where T : IBinaryDiffer, new()
{
_extensions[typeof(IBinaryDiffer)] = typeof(T);
return (TBootstrap)this;
}
public TBootstrap ConfigureBlackList(BlackListConfig config)
{
_instances[typeof(BlackListConfig)] = config ?? BlackListConfig.Empty;
return (TBootstrap)this;
}
/// <summary>
/// Configure blacklist via fluent builder action.
/// Usage: <c>.ConfigureBlackList(cfg => cfg.AddBlackFiles("*.log").AddBlackFormats(".pdb"))</c>
/// </summary>
public TBootstrap ConfigureBlackList(Action<FileSystem.BlackListConfigBuilder> configure)
{
var builder = new FileSystem.BlackListConfigBuilder();
configure(builder);
_instances[typeof(BlackListConfig)] = builder.Build();
return (TBootstrap)this;
}
protected TExtension? ResolveExtension<TExtension>() where TExtension : class
{
if (_extensions.TryGetValue(typeof(TExtension), out var t))
return Activator.CreateInstance((Type)t) as TExtension;
if (_instances.TryGetValue(typeof(TExtension), out var instance))
return instance as TExtension;
return null;
}
}
}