-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
HotReloadHelper.cs
199 lines (177 loc) · 5.83 KB
/
HotReloadHelper.cs
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.Hosting;
namespace Microsoft.Maui.HotReload
{
public static class MauiHotReloadHelper
{
static IMauiHandlersCollection? HandlerService;
//static IMauiHandlersServiceProvider? HandlerServiceProvider;
public static void RegisterHandlers(IMauiHandlersCollection handlerService)
{
HandlerService = handlerService;
}
public static void AddActiveView(IHotReloadableView view) => ActiveViews.Add(view);
public static void Reset()
{
replacedViews.Clear();
}
public static bool IsEnabled { get; set; } = Debugger.IsAttached;
internal static bool IsSupported
#if !NETSTANDARD
=> System.Reflection.Metadata.MetadataUpdater.IsSupported;
#else
=> true;
#endif
public static void Register(IHotReloadableView view, params object[] parameters)
{
if (!IsSupported || !IsEnabled)
return;
currentViews[view] = parameters;
}
public static void UnRegister(IHotReloadableView view)
{
if (!IsSupported || !IsEnabled)
return;
currentViews.Remove(view);
}
public static bool IsReplacedView(IHotReloadableView view, IView newView)
{
if (!IsSupported || !IsEnabled)
return false;
if (view == null || newView == null)
return false;
if (!replacedViews.TryGetValue(view.GetType().FullName!, out var newViewType))
return false;
return newView.GetType() == newViewType;
}
public static IView GetReplacedView(IHotReloadableView view)
{
if (!IsSupported || !IsEnabled)
return view;
var viewType = view.GetType();
if (!replacedViews.TryGetValue(viewType.FullName!, out var newViewType) || viewType == newViewType)
return view;
currentViews.TryGetValue(view, out var parameters);
try
{
//TODO: Add in a way to use IoC and DI
var newView = (IView)(parameters?.Length > 0 ? Activator.CreateInstance(newViewType, args: parameters) : Activator.CreateInstance(newViewType))!;
TransferState(view, newView);
return newView;
}
catch (MissingMethodException)
{
Debug.WriteLine("You are using trying to HotReload a view that requires Parameters. Please call `HotReloadHelper.Register(this, params);` in the constructor;");
//TODO: Notify that we couldnt hot reload.
return view;
}
catch (Exception ex)
{
Debug.WriteLine($"Error Hotreloading type: {newViewType}");
Debug.WriteLine(ex);
//TODO: Notify that we couldnt hot reload.
return view;
}
}
static void TransferState(IHotReloadableView oldView, IView newView)
{
oldView.TransferState(newView);
}
static internal readonly WeakList<IHotReloadableView> ActiveViews = new WeakList<IHotReloadableView>();
static Dictionary<string, Type> replacedViews = new(StringComparer.Ordinal);
static Dictionary<IHotReloadableView, object[]> currentViews = new Dictionary<IHotReloadableView, object[]>();
static Dictionary<string, List<KeyValuePair<Type, Type>>> replacedHandlers = new(StringComparer.Ordinal);
[RequiresUnreferencedCode("Hot Reload is not trim compatible")]
#if !NETSTANDARD
[RequiresDynamicCode("Hot Reload is not AOT compatible")]
#endif
public static void RegisterReplacedView(string oldViewType, Type newViewType)
{
if (!IsSupported || !IsEnabled)
return;
Action<MethodInfo> executeStaticMethod = (method) =>
{
try
{
method?.Invoke(null, null);
}
catch (Exception ex)
{
Debug.WriteLine($"Error calling {method.Name} on type: {newViewType}");
Debug.WriteLine(ex);
//TODO: Notify that we couldnt execute OnHotReload for the Method;
}
};
var onHotReloadMethods = newViewType.GetOnHotReloadMethods();
onHotReloadMethods.ForEach(x => executeStaticMethod(x));
if (typeof(IHotReloadableView).IsAssignableFrom(newViewType))
replacedViews[oldViewType] = newViewType;
if (typeof(IViewHandler).IsAssignableFrom(newViewType))
{
if (replacedHandlers.TryGetValue(oldViewType, out var vTypes))
{
foreach (var vType in vTypes)
RegisterHandler(vType, newViewType);
return;
}
_ = HandlerService ?? throw new ArgumentNullException(nameof(HandlerService));
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var t = assemblies.Select(x => x.GetType(oldViewType)).FirstOrDefault(x => x != null);
var views = HandlerService!.Where(x => x.ImplementationType == t).Select(x => new KeyValuePair<Type, Type>(x.ServiceType, x.ImplementationType!)).ToList();
replacedHandlers[oldViewType] = views.ToList();
foreach (var h in views)
{
RegisterHandler(h, newViewType);
}
}
static void RegisterHandler(KeyValuePair<Type, Type> pair, Type newHandler)
{
_ = HandlerService ?? throw new ArgumentNullException(nameof(HandlerService));
var view = pair.Key;
var newType = newHandler;
if (pair.Value.IsGenericType)
newType = pair.Value.GetGenericTypeDefinition().MakeGenericType(newHandler);
HandlerService.AddHandler(view, newType);
}
}
public static void TriggerReload()
{
List<IHotReloadableView>? roots = null;
while (roots == null)
{
try
{
roots = ActiveViews.Where(x => x != null && x.Parent == null).ToList();
}
catch
{
//Sometimes we get list changed exception.
}
}
foreach (var view in roots)
{
view!.Reload();
}
}
#region Metadata Update Handler
[RequiresUnreferencedCode("Hot Reload is not trim compatible")]
#if !NETSTANDARD
[RequiresDynamicCode("Hot Reload is not AOT compatible")]
#endif
public static void UpdateApplication(Type[] types)
{
IsEnabled = true;
foreach (var t in types)
RegisterReplacedView(t.FullName ?? "", t);
}
public static void ClearCache(Type[] types) => TriggerReload();
#endregion
}
}