From 50451ad95219b65f7a403e0fa5bfca64c7ea622f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 24 Dec 2022 13:31:09 +0100 Subject: [PATCH] Remove reflection from UWP sample startup path --- build/CreateBlankAppCenterToken.ps1 | 24 +++++++++- .../ComputeSharp.SwapChain.Uwp/App.xaml.cs | 8 +--- .../ComputeSharp.SwapChain.Uwp.csproj | 3 +- .../Extensions/AssemblyExtensions.cs | 44 ------------------- 4 files changed, 25 insertions(+), 54 deletions(-) delete mode 100644 samples/ComputeSharp.SwapChain.Uwp/Extensions/AssemblyExtensions.cs diff --git a/build/CreateBlankAppCenterToken.ps1 b/build/CreateBlankAppCenterToken.ps1 index 9c1bfe99d..ec116dad2 100644 --- a/build/CreateBlankAppCenterToken.ps1 +++ b/build/CreateBlankAppCenterToken.ps1 @@ -6,9 +6,29 @@ if(!(Test-Path -Path $TOKENS_FOLDER_RELATIVE_PATH)) { $TOKENS_FOLDER_PATH = Resolve-Path -Path $TOKENS_FOLDER_RELATIVE_PATH # AppCenter tokens -$APPCENTER_TOKENS_NAME = "AppCenter.txt" +$APPCENTER_TOKENS_NAME = "App.AppCenter.cs" $APPCENTER_TOKENS_PATH = Join-Path -Path $TOKENS_FOLDER_PATH -ChildPath $APPCENTER_TOKENS_NAME if (!(Test-Path -Path $APPCENTER_TOKENS_PATH)) { - echo "" > $APPCENTER_TOKENS_PATH + # Prepare the generated source + $APP_APPCENTER_TEXT = +@" +// +#pragma warning disable +#nullable enable + +namespace ComputeSharp.SwapChain.Uwp; + +/// +partial class App +{ + /// + /// The secret token for the AppService connection. + /// + private const string AppServiceSecret = ""; +} +"@.Trim() + + # Output the source to the target file + Add-Content $APP_APPCENTER_TEXT -Path $APPCENTER_TOKENS_PATH -NoNewline } \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs index 763ad27fe..906de4fa4 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs +++ b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs @@ -1,13 +1,11 @@ #if !DEBUG using System; using System.Diagnostics; -using System.Reflection; #endif using CommunityToolkit.Mvvm.DependencyInjection; using ComputeSharp.SwapChain.Core.Services; using ComputeSharp.SwapChain.Core.ViewModels; #if !DEBUG -using ComputeSharp.SwapChain.Uwp.Extensions; using ComputeSharp.SwapChain.Uwp.Services; #endif using ComputeSharp.SwapChain.Uwp.Views; @@ -62,11 +60,9 @@ private static void ConfigureServices() ServiceCollection services = new(); #if !DEBUG - if (!Debugger.IsAttached && - Assembly.GetExecutingAssembly().TryReadAllTextFromManifestFile("Assets/ServiceTokens/AppCenter.txt", out string? secret) && - Guid.TryParse(secret, out _)) + if (Guid.TryParse(AppServiceSecret, out _) && !Debugger.IsAttached) { - services.AddSingleton(new AppCenterService(secret!)); + services.AddSingleton(new AppCenterService(AppServiceSecret)); } else { diff --git a/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj b/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj index dcfd4aff9..7ec98aa38 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj +++ b/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj @@ -78,7 +78,7 @@ App.xaml - + @@ -104,7 +104,6 @@ Assets\Textures\RustyMetal.png PreserveNewest - diff --git a/samples/ComputeSharp.SwapChain.Uwp/Extensions/AssemblyExtensions.cs b/samples/ComputeSharp.SwapChain.Uwp/Extensions/AssemblyExtensions.cs deleted file mode 100644 index 61e44ce98..000000000 --- a/samples/ComputeSharp.SwapChain.Uwp/Extensions/AssemblyExtensions.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System.IO; -using System.Reflection; -using CommunityToolkit.Diagnostics; - -#nullable enable - -namespace ComputeSharp.SwapChain.Uwp.Extensions; - -/// -/// Helpers for the type. -/// -public static class AssemblyExtensions -{ - /// - /// Tries to read a from a specified file in the target . - /// - /// The target to read data from. - /// The target filename to read from. - /// The resulting text, if the read was successful. - /// The contents of the specified file. - public static bool TryReadAllTextFromManifestFile(this Assembly assembly, string filename, out string? text) - { - Guard.IsNotNull(assembly); - Guard.IsNotNull(filename); - - try - { - string manifestFilename = $"{assembly.GetName().Name}.{filename.Replace('/', '.').Replace('\\', '.')}"; - - using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(manifestFilename); - using StreamReader reader = new(stream); - - text = reader.ReadToEnd().Trim(); - - return true; - } - catch - { - text = null; - - return false; - } - } -} \ No newline at end of file