From c06594c0d03cdd4252e2b9db59906e9997952094 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 5 Jan 2022 21:17:40 +0100 Subject: [PATCH] Minor code refactoring --- .../ComputeSharp.SwapChain.Uwp/App.xaml.cs | 32 ++++++-------- .../ComputeSharp.SwapChain.Uwp.csproj | 1 + .../Extensions/AssemblyExtensions.cs | 44 +++++++++++++++++++ 3 files changed, 59 insertions(+), 18 deletions(-) create mode 100644 samples/ComputeSharp.SwapChain.Uwp/Extensions/AssemblyExtensions.cs diff --git a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs index 1dd4a5d7..4db963a7 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs +++ b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs @@ -1,11 +1,15 @@ -using System; +#if !DEBUG +using System; using System.Diagnostics; -using System.IO; 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; using Microsoft.Extensions.DependencyInjection; using Windows.ApplicationModel.Activation; @@ -14,6 +18,8 @@ using Windows.UI; using Windows.UI.ViewManagement; using Windows.UI.Xaml; +#nullable enable + namespace ComputeSharp.SwapChain.Uwp; /// @@ -55,26 +61,16 @@ sealed partial class App : Application { ServiceCollection services = new(); -#if RELEASE - if (Debugger.IsAttached) +#if !DEBUG + if (!Debugger.IsAttached && + Assembly.GetExecutingAssembly().TryReadAllTextFromManifestFile("Assets/ServiceTokens/AppCenter.txt", out string? secret) && + Guid.TryParse(secret, out _)) { - services.AddSingleton(); + services.AddSingleton(new AppCenterService(secret!)); } else { - using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ComputeSharp.SwapChain.Uwp.Assets.ServiceTokens.AppCenter.txt"); - using StreamReader reader = new(stream); - - string appCenterSecret = reader.ReadToEnd().Trim(); - - if (Guid.TryParse(appCenterSecret, out _)) - { - services.AddSingleton(new AppCenterService(appCenterSecret)); - } - else - { - services.AddSingleton(); - } + services.AddSingleton(); } #else services.AddSingleton(); diff --git a/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj b/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj index 24613ecc..697e6c87 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj +++ b/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj @@ -122,6 +122,7 @@ App.xaml + diff --git a/samples/ComputeSharp.SwapChain.Uwp/Extensions/AssemblyExtensions.cs b/samples/ComputeSharp.SwapChain.Uwp/Extensions/AssemblyExtensions.cs new file mode 100644 index 00000000..93146eda --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Uwp/Extensions/AssemblyExtensions.cs @@ -0,0 +1,44 @@ +using System.IO; +using System.Reflection; +using Microsoft.Toolkit.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, nameof(assembly)); + Guard.IsNotNull(filename, nameof(filename)); + + try + { + string manifestFilename = $"{assembly.FullName}.{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; + } + } +}