diff --git a/.gitignore b/.gitignore index b83d378f..b444c010 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ ## ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore +# App tokens +**/ServiceTokens/ + # User-specific files *.suo *.user diff --git a/build/CreateBlankAppCenterToken.ps1 b/build/CreateBlankAppCenterToken.ps1 new file mode 100644 index 00000000..9c1bfe99 --- /dev/null +++ b/build/CreateBlankAppCenterToken.ps1 @@ -0,0 +1,14 @@ +# Create the tokens folder +$TOKENS_FOLDER_RELATIVE_PATH = Join-Path -Path $PSScriptRoot -ChildPath "..\samples\ComputeSharp.SwapChain.Uwp\Assets\ServiceTokens" +if(!(Test-Path -Path $TOKENS_FOLDER_RELATIVE_PATH)) { + New-Item -ItemType directory -Path $TOKENS_FOLDER_RELATIVE_PATH +} +$TOKENS_FOLDER_PATH = Resolve-Path -Path $TOKENS_FOLDER_RELATIVE_PATH + +# AppCenter tokens +$APPCENTER_TOKENS_NAME = "AppCenter.txt" +$APPCENTER_TOKENS_PATH = Join-Path -Path $TOKENS_FOLDER_PATH -ChildPath $APPCENTER_TOKENS_NAME +if (!(Test-Path -Path $APPCENTER_TOKENS_PATH)) +{ + echo "" > $APPCENTER_TOKENS_PATH +} \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/ComputeSharp.SwapChain.Core.Shared.projitems b/samples/ComputeSharp.SwapChain.Core.Shared/ComputeSharp.SwapChain.Core.Shared.projitems index 2a5e911c..80039491 100644 --- a/samples/ComputeSharp.SwapChain.Core.Shared/ComputeSharp.SwapChain.Core.Shared.projitems +++ b/samples/ComputeSharp.SwapChain.Core.Shared/ComputeSharp.SwapChain.Core.Shared.projitems @@ -9,9 +9,12 @@ ComputeSharp.SwapChain.Core.Shared + + + diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/Constants/Event.cs b/samples/ComputeSharp.SwapChain.Core.Shared/Constants/Event.cs new file mode 100644 index 00000000..35cec680 --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Core.Shared/Constants/Event.cs @@ -0,0 +1,16 @@ +namespace ComputeSharp.SwapChain.Core.Constants; + +/// +/// A with the collection of tracked events for analytics. +/// +public static class Event +{ + public const string IsDynamicResolutionEnabledChanged = "[SETTINGS] IsDynamicResolutionEnabled"; + public const string SelectedResolutionScaleChanged = "[SETTINGS] SelectedResolutionScale"; + public const string SelectedComputeShaderChanged = "[SETTINGS] SelectedComputeShader"; + public const string IsRenderingPausedChanged = "[SETTINGS] IsRenderingPaused"; + public const string OpenShaderSelectionPanel = "[SHELL] Open ShaderSelectionPanel"; + public const string CloseShaderSelectionPanel = "[SHELL] Close ShaderSelectionPanel"; + public const string RenderingFailedOnMainPanel = "[SHELL] RenderingFailed (main panel)"; + public const string RenderingFailedOnSelectionPanel = "[SHELL] RenderingFailed (selection panel)"; +} diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/Services/DebugAnalyticsService.cs b/samples/ComputeSharp.SwapChain.Core.Shared/Services/DebugAnalyticsService.cs new file mode 100644 index 00000000..fdd0b425 --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Core.Shared/Services/DebugAnalyticsService.cs @@ -0,0 +1,52 @@ +using System; +using System.Diagnostics; +using System.Text; + +#nullable enable + +namespace ComputeSharp.SwapChain.Core.Services; + +/// +/// A that manages the analytics service in a test environment. +/// +public sealed class DebugAnalyticsService : IAnalyticsService +{ + /// + public void Log(string title, params (string Property, object? Value)[]? data) + { + StringBuilder builder = new(); + + builder.AppendLine($"[EVENT]: \"{title}\""); + + if (data is not null) + { + foreach (var info in data) + { + builder.AppendLine($">> {info.Property}: \"{info.Value ?? ""}\""); + } + } + + Trace.Write(builder); + } + + /// + public void Log(string title, Exception exception, params (string Property, object? Value)[]? data) + { + StringBuilder builder = new(); + + builder.AppendLine($"[EXCEPTION]: \"{title}\""); + builder.AppendLine($">> Type: \"{exception.GetType()}\""); + builder.AppendLine(">> Stack trace"); + builder.AppendLine(exception.StackTrace); + + if (data is not null) + { + foreach (var info in data) + { + builder.AppendLine($">> {info.Property}: \"{info.Value ?? ""}\""); + } + } + + Trace.Write(builder); + } +} diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/Services/IAnalyticsService.cs b/samples/ComputeSharp.SwapChain.Core.Shared/Services/IAnalyticsService.cs new file mode 100644 index 00000000..53081e62 --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Core.Shared/Services/IAnalyticsService.cs @@ -0,0 +1,26 @@ +using System; + +#nullable enable + +namespace ComputeSharp.SwapChain.Core.Services; + +/// +/// The default for an analytics service. +/// +public interface IAnalyticsService +{ + /// + /// Logs an event with a specified title and optional properties. + /// + /// The title of the event to track. + /// The optional event properties. + void Log(string title, params (string Property, object? Value)[]? data); + + /// + /// Logs an exception with a specified title and optional properties. + /// + /// The title of the event to track. + /// The exception that has been thrown. + /// The optional event properties. + void Log(string title, Exception exception, params (string Property, object? Value)[]? data); +} diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/MainViewModel.cs b/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/MainViewModel.cs index db1a5868..259a499f 100644 --- a/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/MainViewModel.cs +++ b/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/MainViewModel.cs @@ -1,7 +1,9 @@ using System.Collections.Generic; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; +using ComputeSharp.SwapChain.Core.Constants; using ComputeSharp.SwapChain.Core.Models; +using ComputeSharp.SwapChain.Core.Services; using ComputeSharp.SwapChain.Core.Shaders.Runners; using ComputeSharp.SwapChain.Shaders; #if WINDOWS_UWP @@ -9,6 +11,7 @@ using ComputeSharp.Uwp; #else using ComputeSharp.WinUI; #endif +using Microsoft.Toolkit.Diagnostics; #nullable enable @@ -19,11 +22,19 @@ namespace ComputeSharp.SwapChain.Core.ViewModels; /// public sealed class MainViewModel : ObservableObject { + /// + /// The instance currently in use. + /// + private readonly IAnalyticsService analyticsService; + /// /// Creates a new instance. /// - public MainViewModel() + public MainViewModel(IAnalyticsService analyticsService) { + Guard.IsNotNull(analyticsService, nameof(analyticsService)); + + this.analyticsService = analyticsService; this.isDynamicResolutionEnabled = true; this.selectedResolutionScale = 100; this.selectedComputeShader = ComputeShaderOptions[0]; @@ -46,7 +57,13 @@ public sealed class MainViewModel : ObservableObject public bool IsDynamicResolutionEnabled { get => this.isDynamicResolutionEnabled; - set => SetProperty(ref this.isDynamicResolutionEnabled, value); + set + { + if (SetProperty(ref this.isDynamicResolutionEnabled, value)) + { + this.analyticsService.Log(Event.IsDynamicResolutionEnabledChanged, (nameof(value), value)); + } + } } private int selectedResolutionScale; @@ -57,7 +74,13 @@ public sealed class MainViewModel : ObservableObject public int SelectedResolutionScale { get => this.selectedResolutionScale; - private set => SetProperty(ref this.selectedResolutionScale, value); + private set + { + if (SetProperty(ref this.selectedResolutionScale, value)) + { + this.analyticsService.Log(Event.SelectedResolutionScaleChanged, (nameof(value), value)); + } + } } /// @@ -78,16 +101,16 @@ public sealed class MainViewModel : ObservableObject /// public IReadOnlyList ComputeShaderOptions { get; } = new ComputeShader[] { - new("Colorful infinity", new ShaderRunner(static time => new((float)time.TotalSeconds))), - new("Extruded truchet", new ShaderRunner(static time => new((float)time.TotalSeconds))), - new("Fractal tiling", new ShaderRunner(static time => new((float)time.TotalSeconds))), - new("Menger Journey", new ShaderRunner(static time => new((float)time.TotalSeconds))), - new("Octagrams", new ShaderRunner(static time => new((float)time.TotalSeconds))), - new("Protean clouds", new ShaderRunner(static time => new((float)time.TotalSeconds))), - new("Two tiled truchet", new ShaderRunner(static time => new((float)time.TotalSeconds))), - new("Pyramid pattern", new ShaderRunner(static time => new((float)time.TotalSeconds))), - new("Triangle grid contouring", new ShaderRunner(static time => new((float)time.TotalSeconds))), - new("Contoured layers", new ContouredLayersRunner()) + new("Colorful infinity", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Extruded truchet", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Fractal tiling", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Menger Journey", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Octagrams", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Protean clouds", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Two tiled truchet", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Pyramid pattern", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Triangle grid contouring", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Contoured layers", new ContouredLayersRunner()) }; private ComputeShader selectedComputeShader; @@ -105,6 +128,8 @@ public sealed class MainViewModel : ObservableObject if (SetProperty(ref this.selectedComputeShader, value) && value is not null) { + this.analyticsService.Log(Event.SelectedComputeShaderChanged, (nameof(value.ShaderRunner), value.ShaderRunner)); + value.IsSelected = true; } } @@ -118,7 +143,13 @@ public sealed class MainViewModel : ObservableObject public bool IsRenderingPaused { get => this.isRenderingPaused; - set => SetProperty(ref this.isRenderingPaused, value); + set + { + if (SetProperty(ref this.isRenderingPaused, value)) + { + this.analyticsService.Log(Event.IsRenderingPausedChanged, (nameof(value), value)); + } + } } /// diff --git a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs index 720413f7..4db963a7 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs +++ b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs @@ -1,10 +1,25 @@ -using ComputeSharp.SwapChain.Uwp.Views; +#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; +using Microsoft.Extensions.DependencyInjection; using Windows.ApplicationModel.Activation; using Windows.ApplicationModel.Core; using Windows.UI; using Windows.UI.ViewManagement; using Windows.UI.Xaml; +#nullable enable + namespace ComputeSharp.SwapChain.Uwp; /// @@ -13,7 +28,7 @@ namespace ComputeSharp.SwapChain.Uwp; sealed partial class App : Application { /// - /// Initializes the singleton application object. This is the first line of authored code + /// Initializes the singleton application object. This is the first line of authored code /// executed, and as such is the logical equivalent of main() or WinMain(). /// public App() @@ -26,6 +41,7 @@ sealed partial class App : Application { if (Window.Current.Content is not MainView) { + ConfigureServices(); StyleTitleBar(); ExpandViewIntoTitleBar(); @@ -38,6 +54,33 @@ sealed partial class App : Application } } + /// + /// Configures the services for the application. + /// + 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 _)) + { + services.AddSingleton(new AppCenterService(secret!)); + } + else + { + services.AddSingleton(); + } +#else + services.AddSingleton(); +#endif + + services.AddTransient(); + + Ioc.Default.ConfigureServices(services.BuildServiceProvider()); + } + /// /// Styles the title bar buttons according to the theme in use /// diff --git a/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj b/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj index 477be62b..697e6c87 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj +++ b/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj @@ -122,8 +122,10 @@ App.xaml + + MainView.xaml @@ -146,6 +148,7 @@ Assets\Textures\RustyMetal.png PreserveNewest + @@ -204,6 +207,15 @@ 7.1.2 + + 4.4.0 + + + 4.4.0 + + + 6.0.0 + 6.2.13 @@ -232,6 +244,9 @@ 14.0 + + powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File $(SolutionDir)\build\CreateBlankAppCenterToken.ps1 + + + + \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Uwp/Services/AppCenterService.cs b/samples/ComputeSharp.SwapChain.Uwp/Services/AppCenterService.cs new file mode 100644 index 00000000..ba7f26b2 --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Uwp/Services/AppCenterService.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using ComputeSharp.SwapChain.Core.Services; +using Microsoft.AppCenter; +using Microsoft.AppCenter.Analytics; +using Microsoft.AppCenter.Crashes; + +#nullable enable + +namespace ComputeSharp.SwapChain.Uwp.Services; + +/// +/// An implementation using AppCenter. +/// +public sealed class AppCenterService : IAnalyticsService +{ + /// + /// The maximum length for any property name and value + /// + /// It's 125, but one character is reserved for the leading '|' to indicate trimming. + private const int PropertyStringMaxLength = 124; + + /// + public AppCenterService(string secret) + { + AppCenter.Start(secret, typeof(Crashes), typeof(Analytics)); + } + + /// + public void Log(string title, params (string Property, object? Value)[]? data) + { + Analytics.TrackEvent(title, GetProperties(data)); + } + + /// + public void Log(string title, Exception exception, params (string Property, object? Value)[]? data) + { + Crashes.TrackError(exception, GetProperties(data)); + } + + /// + /// Gets the additional logging properties from the input data. + /// + /// The optional event properties. + /// The additional logging properties to track. + private static IDictionary? GetProperties((string Property, object? Value)[]? data) + { + return + data?.ToDictionary( + pair => pair.Property, + pair => + { + string text = (pair.Value ?? "").ToString(); + + return text.Length <= PropertyStringMaxLength + ? text + : $"|{text.Substring(text.Length - PropertyStringMaxLength)}"; + }); + } +} diff --git a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml index 5b07b170..f453cc71 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml +++ b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml @@ -11,16 +11,12 @@ xmlns:media="using:Microsoft.Toolkit.Uwp.UI.Media" xmlns:models="using:ComputeSharp.SwapChain.Core.Models" xmlns:ui="using:Microsoft.Toolkit.Uwp.UI" - xmlns:viewmodels="using:ComputeSharp.SwapChain.Core.ViewModels" d:DesignHeight="300" d:DesignWidth="400" SizeChanged="UserControl_SizeChanged" mc:Ignorable="d"> - - - - +