Skip to content

Commit

Permalink
Merge pull request #31 from opf/bug/39052-add-in-fails-to-open-browse…
Browse files Browse the repository at this point in the history
…r-on-fresh-installs

[#39052] Add-in fails to open browser on fresh installs
  • Loading branch information
Kharonus authored Oct 4, 2021
2 parents 25c2975 + a269651 commit eda8a80
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 182 deletions.
4 changes: 2 additions & 2 deletions src/OpenProject.Browser/OpenProject.Browser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.18" />
<PackageReference Include="RestSharp" Version="106.12.0" />
<PackageReference Include="ZetaIpc" Version="1.0.0.11" />

<PackageReference Include="cef.redist.x64" Version="92.0.26" />
<PackageReference Include="CefSharp.Common" Version="92.0.260" />
<PackageReference Include="CefSharp.Wpf" Version="92.0.260" />
Expand Down Expand Up @@ -65,4 +65,4 @@
<Target Name="ZipEmbeddedLandingPage" BeforeTargets="BeforeBuild" Condition="'$(BuildingInsideVisualStudio)' == 'true'">
<Exec Command="build.cmd CreateEmbeddedLandingPageZip --skip" WorkingDirectory="$(MSBuildThisFileDirectory)\..\.." />
</Target>
</Project>
</Project>
64 changes: 24 additions & 40 deletions src/OpenProject.Browser/Services/ConfigurationHandler.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using Config.Net;
using Newtonsoft.Json;
using OpenProject.Browser.Settings;
using Serilog;

namespace OpenProject.Browser.Services
{
Expand All @@ -27,54 +25,39 @@ static ConfigurationHandler()
}
}

public static IOpenProjectSettings Settings { get; }
private static IOpenProjectSettings Settings { get; }

public static bool ShouldEnableDevelopmentTools() => Settings.EnableDevelopmentTools;

public static List<string> LoadAllInstances() => Settings.GetOpenProjectInstances();

public static void RemoveSavedInstance(string instanceUrl)
{
if (Settings.OpenProjectInstances == null || !Settings.OpenProjectInstances.Any()) return;

var existingValues = Settings.GetOpenProjectInstances();
var existing = existingValues.FirstOrDefault(e => e.ToString() == instanceUrl);
if (existing == null) return;

existingValues.Remove(existing);
Settings.SetOpenProjectInstances(existingValues);
var instances = Settings.GetOpenProjectInstances();
instances.Remove(instanceUrl);
Settings.OpenProjectInstances = JsonConvert.SerializeObject(instances);
}

public static List<string> LoadAllInstances() => Settings.GetOpenProjectInstances().ToList();

public static void SaveLastVisitedPage(string url)
public static void SaveSelectedInstance(string instanceUrl)
{
Settings.LastVisitedPage = url;
RemoveSavedInstance(instanceUrl);
AddOpenProjectInstance(instanceUrl);
}

public static string LastVisitedPage() => Settings.LastVisitedPage ?? string.Empty;

public static void SaveSelectedInstance(string instanceUrl)
private static void AddOpenProjectInstance(string openProjectInstance)
{
var existingValues = Settings.GetOpenProjectInstances();
if (existingValues == null)
{
Settings.SetOpenProjectInstances(new List<string>
{
instanceUrl
});
}
else
{
var existing = existingValues.FirstOrDefault(e => e.ToString() == instanceUrl);
if (existing != null)
{
existingValues.Remove(existing);
}

existingValues.Insert(0, instanceUrl);
Settings.SetOpenProjectInstances(existingValues);
}
if (openProjectInstance == null || string.Empty.Equals(openProjectInstance))
return;

var instances = Settings.GetOpenProjectInstances();
instances.Insert(0, openProjectInstance);
Settings.OpenProjectInstances = JsonConvert.SerializeObject(instances);
}

public static void SaveLastVisitedPage(string url) => Settings.LastVisitedPage = url;

public static string LastVisitedPage() => Settings.LastVisitedPage ?? string.Empty;

private static string GetConfigurationFilePath()
{
var configPath = Path.Combine(
Expand All @@ -85,8 +68,9 @@ private static string GetConfigurationFilePath()
if (File.Exists(configPath)) return configPath;

// If the file doesn't yet exist, the default one is created
using Stream configStream = typeof(ConfigurationHandler).Assembly
.GetManifestResourceStream("OpenProject.OpenProject.Configuration.json");
using Stream configStream =
typeof(ConfigurationHandler).Assembly
.GetManifestResourceStream("OpenProject.Browser.Settings.OpenProject.Configuration.json");
if (configStream == null)
throw new ApplicationException("Missing configuration manifest");

Expand Down
24 changes: 10 additions & 14 deletions src/OpenProject.Browser/Settings/OpenProjectSettingsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Serilog;

namespace OpenProject.Browser.Settings
{
public static class OpenProjectSettingsExtensions
{
public static List<string> GetOpenProjectInstances(this IOpenProjectSettings settings)
{
if (settings.OpenProjectInstances == null)
{
return null;
}

return JsonConvert.DeserializeObject<List<string>>(settings.OpenProjectInstances);
}
var result = new List<string>();

public static void SetOpenProjectInstances(this IOpenProjectSettings settings, List<string> openProjectInstances)
{
if (openProjectInstances == null)
try
{
settings.OpenProjectInstances = null;
result = JsonConvert.DeserializeObject<List<string>>(settings.OpenProjectInstances);
}
else
catch (Exception exception)
{
settings.OpenProjectInstances = JsonConvert.SerializeObject(openProjectInstances);
Log.Error(exception, "Invalid settings value for OpenProject instances: {0}.", settings.OpenProjectInstances);
}

return result;
}
}
}
Binary file not shown.
70 changes: 0 additions & 70 deletions src/OpenProject.Revit/Entry/ConfigurationLoader.cs

This file was deleted.

7 changes: 0 additions & 7 deletions src/OpenProject.Revit/Entry/IOpenProjectRevitSettings.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

namespace OpenProject.Revit.Entry
{
public class BcfierIpcHandler
public class IpcHandler
{
private readonly UIApplication _uiApp;
private Action<string> _sendData;
private static readonly object _callbackStackLock = new();
private static readonly Stack<Action> _callbackStack = new();

public BcfierIpcHandler(UIApplication uiApp)
public IpcHandler(UIApplication uiApp)
{
_uiApp = uiApp ?? throw new ArgumentNullException(nameof(uiApp));

Expand All @@ -34,8 +34,7 @@ public BcfierIpcHandler(UIApplication uiApp)
{
if (!_callbackStack.Any()) return;

var action = _callbackStack.Pop();
action.Invoke();
_callbackStack.Pop().Invoke();
}
};
}
Expand Down
69 changes: 37 additions & 32 deletions src/OpenProject.Revit/Entry/RibbonButtonClickHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using OpenProject.Revit.Services;
using OpenProject.Shared;
using Serilog;
using ZetaIpc.Runtime.Helper;

namespace OpenProject.Revit.Entry
Expand All @@ -18,8 +22,8 @@ public static class RibbonButtonClickHandler
public const string RevitVersion = "2019";
#endif

private static Process _bcfierWinProcess;
public static BcfierIpcHandler IpcHandler { get; private set; }
private static Process _opBrowserProcess;
public static IpcHandler IpcHandler { get; private set; }

public static Result OpenMainPluginWindow(ExternalCommandData commandData, ref string message)
{
Expand All @@ -30,9 +34,10 @@ public static Result OpenMainPluginWindow(ExternalCommandData commandData, ref s

return Result.Succeeded;
}
catch (Exception e)
catch (Exception exception)
{
message = e.Message;
message = exception.Message;
Log.Error(exception, message);
return Result.Failed;
}
}
Expand All @@ -46,9 +51,10 @@ public static Result OpenSettingsPluginWindow(ExternalCommandData commandData, r
IpcHandler.SendBringBrowserToForegroundRequestToDesktopApp();
return Result.Succeeded;
}
catch (Exception e)
catch (Exception exception)
{
message = e.Message;
message = exception.Message;
Log.Error(exception, message);
return Result.Failed;
}
}
Expand All @@ -58,38 +64,37 @@ private static void EnsureExternalOpenProjectAppIsRunning(ExternalCommandData co
//Version check
if (!commandData.Application.Application.VersionName.Contains(RevitVersion))
{
using var td = new TaskDialog("Untested version")
{
TitleAutoPrefix = false,
MainInstruction = "Untested Revit Version",
MainContent = "This Add-In was built and tested only for Revit " + RevitVersion +
", proceed at your own risk"
};
td.Show();
MessageHandler.ShowWarning(
"Unexpected version",
"The Revit version does not match the expectations.",
$"This Add-In was built and tested only for Revit {RevitVersion}. Further usage is at your own risk");
}

// Form Running?
if (_bcfierWinProcess != null && !_bcfierWinProcess.HasExited)
{
if (_opBrowserProcess is { HasExited: false })
return;
}

IpcHandler = new BcfierIpcHandler(commandData.Application);
IpcHandler = new IpcHandler(commandData.Application);
var revitServerPort = IpcHandler.StartLocalServerAndReturnPort();
var bcfierWinProcessPath = ConfigurationLoader.GetBcfierWinExecutablePath();
if (!File.Exists(bcfierWinProcessPath))
{
// The configuration can be used to override the path, if there's no valid file given then
// the default installation location is used
var defaultInstallationPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
"OpenProject Revit AddIn", "OpenProject.Browser.exe");
bcfierWinProcessPath = defaultInstallationPath;
}

var bcfierWinServerPort = FreePortHelper.GetFreePort();
var bcfWinProcessArguments = $"ipc {bcfierWinServerPort} {revitServerPort}";
_bcfierWinProcess = Process.Start(bcfierWinProcessPath, bcfWinProcessArguments);
IpcHandler.StartLocalClient(bcfierWinServerPort);
var openProjectBrowserExecutablePath = GetOpenProjectBrowserExecutable();
if (!File.Exists(openProjectBrowserExecutablePath))
throw new SystemException("Browser executable not found.");

var opBrowserServerPort = FreePortHelper.GetFreePort();
var processArguments = $"ipc {opBrowserServerPort} {revitServerPort}";
_opBrowserProcess = Process.Start(openProjectBrowserExecutablePath, processArguments);
IpcHandler.StartLocalClient(opBrowserServerPort);
Log.Information("IPC bridge started between port {port1} and {port2}.",
opBrowserServerPort, revitServerPort);
}

private static string GetOpenProjectBrowserExecutable()
{
var currentAssemblyPathUri = Assembly.GetExecutingAssembly().CodeBase;
var currentAssemblyPath = Uri.UnescapeDataString(new Uri(currentAssemblyPathUri).AbsolutePath).Replace("/", "\\");
var currentFolder = Path.GetDirectoryName(currentAssemblyPath) ?? string.Empty;

return Path.Combine(currentFolder, ConfigurationConstant.OpenProjectBrowserExecutablePath);
}
}
}
3 changes: 0 additions & 3 deletions src/OpenProject.Revit/OpenProject.Revit.Configuration.json

This file was deleted.

Loading

0 comments on commit eda8a80

Please sign in to comment.