Skip to content

Commit

Permalink
Refactor app to use RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
bbqsrc committed May 8, 2020
1 parent 8b892de commit 08da1a7
Show file tree
Hide file tree
Showing 76 changed files with 6,059 additions and 1,425 deletions.
67 changes: 67 additions & 0 deletions Divvun.Installer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Divvun.Installer", "Divvun.
{D053DF20-5814-46D6-97B6-C59D590AA7C9} = {D053DF20-5814-46D6-97B6-C59D590AA7C9}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pahkat.Sdk", "Pahkat.Sdk\Pahkat.Sdk.csproj", "{20F56DAF-5D1D-421D-8BB0-203F2F591074}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pahkat.Sdk.Rpc", "Pahkat.Sdk.Rpc\Pahkat.Sdk.Rpc.csproj", "{5C78E2EC-FF0C-4AEA-B146-DDCA6291C025}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pahkat.Sdk.Rpc.Example", "Pahkat.Sdk.Rpc.Example\Pahkat.Sdk.Rpc.Example.csproj", "{CF6BC01A-9CBE-4712-8259-2C834679288B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlatBuffers", "FlatBuffers\FlatBuffers.csproj", "{28C00774-1E73-4A75-AD8F-844CD21A064D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x86 = Debug|x86
Debug|x64 = Debug|x64
DebugMockRegistry|Any CPU = DebugMockRegistry|Any CPU
DebugMockRegistry|x86 = DebugMockRegistry|x86
Release|Any CPU = Release|Any CPU
Expand All @@ -30,6 +39,64 @@ Global
{B876899E-C40F-4D5A-84DE-8BF1B0064A28}.Release|Any CPU.Build.0 = Release|Any CPU
{B876899E-C40F-4D5A-84DE-8BF1B0064A28}.Release|x86.ActiveCfg = Release|x86
{B876899E-C40F-4D5A-84DE-8BF1B0064A28}.Release|x86.Build.0 = Release|x86
{B876899E-C40F-4D5A-84DE-8BF1B0064A28}.Debug|x64.ActiveCfg = Debug|x64
{B876899E-C40F-4D5A-84DE-8BF1B0064A28}.Debug|x64.Build.0 = Debug|x64
{20F56DAF-5D1D-421D-8BB0-203F2F591074}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{20F56DAF-5D1D-421D-8BB0-203F2F591074}.Debug|Any CPU.Build.0 = Debug|Any CPU
{20F56DAF-5D1D-421D-8BB0-203F2F591074}.Debug|x86.ActiveCfg = Debug|Any CPU
{20F56DAF-5D1D-421D-8BB0-203F2F591074}.Debug|x86.Build.0 = Debug|Any CPU
{20F56DAF-5D1D-421D-8BB0-203F2F591074}.DebugMockRegistry|Any CPU.ActiveCfg = Debug|Any CPU
{20F56DAF-5D1D-421D-8BB0-203F2F591074}.DebugMockRegistry|Any CPU.Build.0 = Debug|Any CPU
{20F56DAF-5D1D-421D-8BB0-203F2F591074}.DebugMockRegistry|x86.ActiveCfg = Debug|Any CPU
{20F56DAF-5D1D-421D-8BB0-203F2F591074}.DebugMockRegistry|x86.Build.0 = Debug|Any CPU
{20F56DAF-5D1D-421D-8BB0-203F2F591074}.Release|Any CPU.ActiveCfg = Release|Any CPU
{20F56DAF-5D1D-421D-8BB0-203F2F591074}.Release|Any CPU.Build.0 = Release|Any CPU
{20F56DAF-5D1D-421D-8BB0-203F2F591074}.Release|x86.ActiveCfg = Release|Any CPU
{20F56DAF-5D1D-421D-8BB0-203F2F591074}.Release|x86.Build.0 = Release|Any CPU
{20F56DAF-5D1D-421D-8BB0-203F2F591074}.Debug|x64.ActiveCfg = Debug|x64
{20F56DAF-5D1D-421D-8BB0-203F2F591074}.Debug|x64.Build.0 = Debug|x64
{5C78E2EC-FF0C-4AEA-B146-DDCA6291C025}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5C78E2EC-FF0C-4AEA-B146-DDCA6291C025}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5C78E2EC-FF0C-4AEA-B146-DDCA6291C025}.Debug|x86.ActiveCfg = Debug|Any CPU
{5C78E2EC-FF0C-4AEA-B146-DDCA6291C025}.Debug|x86.Build.0 = Debug|Any CPU
{5C78E2EC-FF0C-4AEA-B146-DDCA6291C025}.DebugMockRegistry|Any CPU.ActiveCfg = Debug|Any CPU
{5C78E2EC-FF0C-4AEA-B146-DDCA6291C025}.DebugMockRegistry|Any CPU.Build.0 = Debug|Any CPU
{5C78E2EC-FF0C-4AEA-B146-DDCA6291C025}.DebugMockRegistry|x86.ActiveCfg = Debug|Any CPU
{5C78E2EC-FF0C-4AEA-B146-DDCA6291C025}.DebugMockRegistry|x86.Build.0 = Debug|Any CPU
{5C78E2EC-FF0C-4AEA-B146-DDCA6291C025}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5C78E2EC-FF0C-4AEA-B146-DDCA6291C025}.Release|Any CPU.Build.0 = Release|Any CPU
{5C78E2EC-FF0C-4AEA-B146-DDCA6291C025}.Release|x86.ActiveCfg = Release|Any CPU
{5C78E2EC-FF0C-4AEA-B146-DDCA6291C025}.Release|x86.Build.0 = Release|Any CPU
{5C78E2EC-FF0C-4AEA-B146-DDCA6291C025}.Debug|x64.ActiveCfg = Debug|x64
{5C78E2EC-FF0C-4AEA-B146-DDCA6291C025}.Debug|x64.Build.0 = Debug|x64
{CF6BC01A-9CBE-4712-8259-2C834679288B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CF6BC01A-9CBE-4712-8259-2C834679288B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CF6BC01A-9CBE-4712-8259-2C834679288B}.Debug|x86.ActiveCfg = Debug|Any CPU
{CF6BC01A-9CBE-4712-8259-2C834679288B}.Debug|x86.Build.0 = Debug|Any CPU
{CF6BC01A-9CBE-4712-8259-2C834679288B}.DebugMockRegistry|Any CPU.ActiveCfg = Debug|Any CPU
{CF6BC01A-9CBE-4712-8259-2C834679288B}.DebugMockRegistry|Any CPU.Build.0 = Debug|Any CPU
{CF6BC01A-9CBE-4712-8259-2C834679288B}.DebugMockRegistry|x86.ActiveCfg = Debug|Any CPU
{CF6BC01A-9CBE-4712-8259-2C834679288B}.DebugMockRegistry|x86.Build.0 = Debug|Any CPU
{CF6BC01A-9CBE-4712-8259-2C834679288B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CF6BC01A-9CBE-4712-8259-2C834679288B}.Release|Any CPU.Build.0 = Release|Any CPU
{CF6BC01A-9CBE-4712-8259-2C834679288B}.Release|x86.ActiveCfg = Release|Any CPU
{CF6BC01A-9CBE-4712-8259-2C834679288B}.Release|x86.Build.0 = Release|Any CPU
{CF6BC01A-9CBE-4712-8259-2C834679288B}.Debug|x64.ActiveCfg = Debug|x64
{CF6BC01A-9CBE-4712-8259-2C834679288B}.Debug|x64.Build.0 = Debug|x64
{28C00774-1E73-4A75-AD8F-844CD21A064D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28C00774-1E73-4A75-AD8F-844CD21A064D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28C00774-1E73-4A75-AD8F-844CD21A064D}.Debug|x86.ActiveCfg = Debug|Any CPU
{28C00774-1E73-4A75-AD8F-844CD21A064D}.Debug|x86.Build.0 = Debug|Any CPU
{28C00774-1E73-4A75-AD8F-844CD21A064D}.DebugMockRegistry|Any CPU.ActiveCfg = Debug|Any CPU
{28C00774-1E73-4A75-AD8F-844CD21A064D}.DebugMockRegistry|Any CPU.Build.0 = Debug|Any CPU
{28C00774-1E73-4A75-AD8F-844CD21A064D}.DebugMockRegistry|x86.ActiveCfg = Debug|Any CPU
{28C00774-1E73-4A75-AD8F-844CD21A064D}.DebugMockRegistry|x86.Build.0 = Debug|Any CPU
{28C00774-1E73-4A75-AD8F-844CD21A064D}.Release|Any CPU.ActiveCfg = Debug|Any CPU
{28C00774-1E73-4A75-AD8F-844CD21A064D}.Release|Any CPU.Build.0 = Debug|Any CPU
{28C00774-1E73-4A75-AD8F-844CD21A064D}.Release|x86.ActiveCfg = Debug|Any CPU
{28C00774-1E73-4A75-AD8F-844CD21A064D}.Release|x86.Build.0 = Debug|Any CPU
{28C00774-1E73-4A75-AD8F-844CD21A064D}.Debug|x64.ActiveCfg = Debug|x64
{28C00774-1E73-4A75-AD8F-844CD21A064D}.Debug|x64.Build.0 = Debug|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
15 changes: 15 additions & 0 deletions Divvun.Installer/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Application x:Class="Divvun.Installer.PahkatApp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ui="http://schemas.modernwpf.com/2019"
xmlns:local="clr-namespace:Divvun.Installer"
Startup="OnStartup">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ui:ThemeResources />
<ui:XamlControlsResources />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
149 changes: 53 additions & 96 deletions Divvun.Installer/App.cs → Divvun.Installer/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,59 @@
using System.Diagnostics;
using System.Reactive.Disposables;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Linq;
using System.Reactive.Subjects;
using System.Threading;
using Divvun.Installer.Models;
using Divvun.Installer.Properties;
using Divvun.Installer.Service;
using Divvun.Installer.UI.About;
using Divvun.Installer.UI.Main;
using Divvun.Installer.UI.Settings;
using Divvun.Installer.Util;
using Hardcodet.Wpf.TaskbarNotification;
using SharpRaven;
using SharpRaven.Data;
using Trustsoft.SingleInstanceApp;
using Newtonsoft.Json;
using Pahkat.Sdk;
using Pahkat.Sdk.Rpc;
using Serilog;

namespace Divvun.Installer
{
public abstract class AbstractPahkatApp : Application, ISingleInstanceApp

public partial class PahkatApp : ISingleInstanceApp
{
public abstract IWindowService WindowService { get; protected set; }

// public abstract PackageStore PackageStore { get; protected set; }
public abstract IRavenClient SentryClient { get; protected set; }
public abstract UserPackageSelectionStore UserSelection { get; protected set; }

protected override void OnStartup(StartupEventArgs e) { }

public abstract bool OnActivate(IList<string> args);
}

public enum AppMode
{
Default,
Install
}

public class PahkatApp : AbstractPahkatApp
{
public AppMode Mode { get; private set; } = AppMode.Default;

public const string ArgsSilent = "-s";
public const string ArgsInstall = "-i";
public const string ArgsWindow = "-w";

// public override PackageStore PackageStore { get; protected set; }
public override IWindowService WindowService { get; protected set; } = Service.WindowService.Create(
public IWindowService WindowService { get; protected set; } = Service.WindowService.Create(
CloseHandlingWindowConfig.Create<MainWindow>(),
CloseHandlingWindowConfig.Create<SettingsWindow>(),
CloseHandlingWindowConfig.Create<AboutWindow>()
);

public override IRavenClient SentryClient { get; protected set; }
public override UserPackageSelectionStore UserSelection { get; protected set; }
private CancellationTokenSource? _runningTransaction;
public BehaviorSubject<TransactionState> CurrentTransaction
= new BehaviorSubject<TransactionState>(new TransactionState.NotStarted());

public void StartTransaction(PackageAction[] actions) {
using var guard = PackageStore.Lock();
_runningTransaction?.Cancel();

_runningTransaction = guard.Value.ProcessTransaction(actions, value => {
Console.WriteLine("-- New event: " + value);
var newState = CurrentTransaction.Value.Reduce(value);
CurrentTransaction.OnNext(newState);
});
}


public Mutex<IPahkatClient> PackageStore { get; protected set; }
public IRavenClient SentryClient { get; protected set; }
public UserPackageSelectionStore UserSelection { get; protected set; }
public Settings Settings { get; protected set; }

private CompositeDisposable _bag = new CompositeDisposable();
private TaskbarIcon _icon;
Expand Down Expand Up @@ -138,36 +135,14 @@ protected override void OnStartup(StartupEventArgs e) {

var args = Environment.GetCommandLineArgs();

if (Mode == AppMode.Default) {
CreateNotifyIcon();
}
CreateNotifyIcon();

base.OnStartup(e);

OnActivate(args);
}

public override bool OnActivate(IList<string> args) {
WindowSaveState? windowState = null;

if (args.Contains(ArgsWindow)) {
var index = args.IndexOf(ArgsWindow) + 1;
if (index >= args.Count) {
throw new Exception("Invalid command line arguments provided");
}

windowState = JsonConvert.DeserializeObject<WindowSaveState>(args[index]);
}

if (args.Contains(ArgsInstall)) {
var index = args.IndexOf(ArgsInstall) + 1;
if (index >= args.Count) {
throw new Exception("Invalid command line arguments provided");
}

WindowService.Show<MainWindow>(InstallPage.Create(args[index]), windowState);
}

public bool OnActivate(IList<string> args) {
// If -s, run silently. Used for start-up service.
if (!args.Contains(ArgsSilent)) {
WindowService.Show<MainWindow>();
Expand All @@ -182,9 +157,9 @@ public override bool OnActivate(IList<string> args) {

private const int AttachParentProcess = -1;

[STAThread]
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
public static void Main(string[] args) {
private void OnStartup(object sender, StartupEventArgs e) {
const string key = "Divvun.Installer";
AttachConsole(AttachParentProcess);

Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));
Expand All @@ -193,37 +168,12 @@ public static void Main(string[] args) {
.MinimumLevel.Verbose()
.WriteTo.Console(theme: Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme.Code)
.CreateLogger();


// Divvun.Installer.Sdk.Settings.SetLoggingCallback((level, message, module, path) =>
// {
// if (level == 0)
// {
// // Zero means no logging
// return;
// }
//
// if (level > 5)
// {
// level = 5;
// }
//
// var serilogLevel = 5 - level;
// Log.Write((LogEventLevel)serilogLevel, "[{Module}] {Path} {Message}", module, path, message);
// });

var mode = args.Any((x) => x == ArgsInstall)
? AppMode.Install
: AppMode.Default;

// Check if multiple instances of Divvun Installer are running before creating new app
if (mode == AppMode.Default) {
const string key = "Divvun.Installer";

if (!SingleInstance<PahkatApp>.InitializeAsFirstInstance(key)) {
Log.Information("App already running; aborting.");
Log.CloseAndFlush();
return;
}
if (!SingleInstance<PahkatApp>.InitializeAsFirstInstance(key)) {
Log.Information("App already running; aborting.");
Log.CloseAndFlush();
return;
}

// Set up Sentry exception capturing
Expand All @@ -232,16 +182,23 @@ public static void Main(string[] args) {
sentry.Capture(new SentryEvent((Exception) sargs.ExceptionObject));
};

Settings = Settings.Create();

// Set the UI language
Settings.Mutate(file => {
if (file.Language != null) {
Strings.Culture = Util.Util.GetCulture(file.Language);
}
});

SentryClient = sentry;
PackageStore = new Mutex<IPahkatClient>(PahkatClient.Create());
}

// Start app
var application = new PahkatApp {SentryClient = sentry, Mode = mode};
application.Run();

if (mode == AppMode.Default) {
SingleInstance<PahkatApp>.Cleanup();
}

protected override void OnExit(ExitEventArgs e) {
SingleInstance<PahkatApp>.Cleanup();
Log.CloseAndFlush();
base.OnExit(e);
}
}
}
Loading

0 comments on commit 08da1a7

Please sign in to comment.