|
| 1 | +#nullable enable |
| 2 | +// <autogenerated /> |
| 3 | +#pragma warning disable // Disable all warnings for this generated file |
| 4 | + |
| 5 | +// Register an embedded sources provider for Hot Reload |
| 6 | +[assembly: global::System.Reflection.AssemblyMetadata("Uno.HotDesign.HotReloadEmbeddedXamlSourceFilesProvider", "MyProject.__Sources__.EmbeddedXamlSourcesProvider")] |
| 7 | + |
| 8 | +namespace MyProject.__Sources__; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Provides access to the embedded XAML sources |
| 12 | +/// </summary> |
| 13 | +/// <remarks> |
| 14 | +/// This class is used to provide the embedded XAML sources to the Hot Reload engine. |
| 15 | +/// This is not intended to be used directly by application code. |
| 16 | +/// WON'T BE GENERATED ON RELEASE BUILDS |
| 17 | +/// </remarks> |
| 18 | +internal static class EmbeddedXamlSourcesProvider |
| 19 | +{ |
| 20 | + // key=absolute file path |
| 21 | + private static global::System.Collections.Generic.IDictionary<string, (string ActualPath, global::System.Func<(string Hash, string Payload)> Getter)>? _XamlSources; |
| 22 | + |
| 23 | + // hash of all the paths |
| 24 | + private static volatile string? _filesListHash; |
| 25 | + |
| 26 | + // get the current value of the update counter |
| 27 | + private static volatile uint _updateCounter; |
| 28 | + |
| 29 | + private static readonly global::System.Text.Encoding _utf8 = global::System.Text.Encoding.UTF8; |
| 30 | + |
| 31 | + // The content of this method only changes when the file list changes |
| 32 | + private static global::System.Collections.Generic.IDictionary<string, (string ActualPath, global::System.Func<(string Hash, string Payload)> Getter)> EnsureInitialize() |
| 33 | + { |
| 34 | + const string currentListHash = "d6cd66944958ced0c513e0a04797b51d"; // that's the hash of all the paths, used to detect changes in the file list following a HR operation |
| 35 | + |
| 36 | + // Determine if the sources have been updated or not initialized yet |
| 37 | + var previousHashList = _XamlSources; |
| 38 | + var needsUpdate = previousHashList is null || _filesListHash != currentListHash; |
| 39 | + |
| 40 | + if (needsUpdate) |
| 41 | + { |
| 42 | + var xamlSources = new global::System.Collections.Generic.Dictionary<string, (string ActualPath, global::System.Func<(string Hash, string Payload)> Getter)>(1, global::System.StringComparer.OrdinalIgnoreCase); |
| 43 | + |
| 44 | + // Use method groups to avoid closure allocation and ensure no lambda is created, to allow proper HR support |
| 45 | + xamlSources[NormalizePath(@"C:/Project/0/MainPage.xaml")] = (NormalizePath(@"C:/Project/0/MainPage.xaml"), GetSources_MainPage_d6cd66944958ced0c513e0a04797b51d); |
| 46 | + |
| 47 | + if (global::System.Threading.Interlocked.CompareExchange(ref _XamlSources, xamlSources, previousHashList) == previousHashList) |
| 48 | + { |
| 49 | + // The sources were updated successfully (no other thread modified them concurrently) |
| 50 | + _filesListHash = currentListHash; |
| 51 | + _updateCounter++; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return _XamlSources; |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Gets the current update counter, used to detect changes in the sources. |
| 60 | + /// </summary> |
| 61 | + /// <remarks> |
| 62 | + /// This counter is incremented each time a Hot Reload sources update is detected. |
| 63 | + /// </remarks> |
| 64 | + public static uint UpdateCounter |
| 65 | + { |
| 66 | + get |
| 67 | + { |
| 68 | + EnsureInitialize(); |
| 69 | + return _updateCounter; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public static global::System.Collections.Generic.IReadOnlyList<string> GetXamlFilesList() => [.. EnsureInitialize().Keys]; |
| 74 | + |
| 75 | + public static string? GetNormalizedFileName(string path) |
| 76 | + { |
| 77 | + // Will return the normalized path if the file exists, or null if it doesn't. |
| 78 | + // (the returned value will be constant for the file and can be used in a dictionary using an ordinal comparer) |
| 79 | + var normalizedPath = NormalizePath(path); |
| 80 | + return EnsureInitialize().TryGetValue(normalizedPath, out var entry) ? entry.ActualPath : null; |
| 81 | + } |
| 82 | + |
| 83 | + public static (string ActualPath, string Hash, string Payload)? GetXamlFile(string path) |
| 84 | + { |
| 85 | + var normalizedPath = NormalizePath(path); |
| 86 | + if (EnsureInitialize().TryGetValue(normalizedPath, out var entry)) |
| 87 | + { |
| 88 | + var sources = entry.Getter(); |
| 89 | + return (entry.ActualPath, sources.Hash, sources.Payload); |
| 90 | + } |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + private static string NormalizePath(string path) => path.Replace('\\', '/'); |
| 95 | + |
| 96 | + #region Sources for C:/Project/0/MainPage.xaml |
| 97 | + private static (string hash, string payload) GetSources_MainPage_d6cd66944958ced0c513e0a04797b51d() |
| 98 | + { |
| 99 | + return ( |
| 100 | + "1c06c16d398ea19bdc8fe30e7d2ba2dcb58578ab", // hash |
| 101 | + _utf8.GetString("""""" |
| 102 | + <Page x:Class="TestRepro.MainPage" |
| 103 | + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
| 104 | + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> |
| 105 | + <!-- This is a comment having potential escaping problems: " "" """ """" """"" --> |
| 106 | + <!-- This is a comment having potential escaping problems: " "" """ """" """"" --> |
| 107 | + </Page> |
| 108 | + """"""u8)); // Stored as UTF8 to minimize impact on assembly size / limitations |
| 109 | + } |
| 110 | + #endregion |
| 111 | +} |
0 commit comments