220 lines
No EOL
7.7 KiB
C#
220 lines
No EOL
7.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Runtime.CompilerServices;
|
|
using JD.Collections.Extensions;
|
|
using JD.Kernel.StaticHelpers;
|
|
using JD.LogService;
|
|
using JD.TeaBoxAddinSystem.Addins.Management;
|
|
|
|
namespace JD.TeaBoxAddinSystem.Bootstrap
|
|
{
|
|
/// <summary>
|
|
/// Static class that holds many properties of the current application.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Note that these properties are only valid after an <see cref="ApplicationStartup"/> has been started.
|
|
/// </remarks>
|
|
public static class Runtime
|
|
{
|
|
private static bool _commandLineMode = true; // True by default so that it's true in unit tests.
|
|
private static ApplicationConfiguration _configuration;
|
|
|
|
[MethodImpl(MethodImplOptions.NoInlining)] // Ensures that Assembly.GetCallingAssembly() works as expected
|
|
static Runtime()
|
|
{
|
|
EntryAssembly = Assembly.GetCallingAssembly();
|
|
|
|
var entryAssembly = Assembly.GetEntryAssembly();
|
|
ApplicationName = entryAssembly != null ? entryAssembly.GetName().Name : "Unknown application";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Whether the runtime has been initialized.
|
|
/// </summary>
|
|
public static bool Initialized { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Entry Assembly of the application..
|
|
/// </summary>
|
|
public static Assembly EntryAssembly { get; }
|
|
|
|
/// <summary>
|
|
/// Unique identifier used to identify the application.
|
|
/// </summary>
|
|
public static string UniqueIdentifier
|
|
{
|
|
get { return EntryAssembly.FullName; }
|
|
}
|
|
|
|
// Useful for shared data (playlists, scripts)
|
|
// Takes deployed apps (BuildBox) into account
|
|
public static string DeploymentPath
|
|
{
|
|
get
|
|
{
|
|
string deploymentPath = null;
|
|
|
|
// First check if the working directory contains a Scripts folder and use that if it does
|
|
// Useful for forcing a specific scripts folder when launching from VS
|
|
string currentDirectory = Environment.CurrentDirectory;
|
|
|
|
// HACK: Would be nice if we didn't have to hardcode the folder name
|
|
foreach (string folderName in new[] {"Script", "Scripts"})
|
|
{
|
|
string scriptDirectory = Path.Combine(currentDirectory, folderName);
|
|
if (Directory.Exists(scriptDirectory) &&
|
|
!Directory.GetFiles(scriptDirectory, "*.cs", SearchOption.AllDirectories).IsEmpty())
|
|
{
|
|
deploymentPath = currentDirectory;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (deploymentPath == null)
|
|
{
|
|
deploymentPath = ApplicationRootPath;
|
|
}
|
|
|
|
return FileUtility.CleanPath(deploymentPath);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Arguments that were used to start the application.
|
|
/// </summary>
|
|
public static Arguments Arguments { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Application Root Path.
|
|
/// </summary>
|
|
public static string ApplicationRootPath { get; private set; } = Environment.CurrentDirectory;
|
|
|
|
/// <summary>
|
|
/// Whether the application was started in command-line mode.
|
|
/// </summary>
|
|
public static bool CommandLineMode
|
|
{
|
|
get { return _commandLineMode; }
|
|
set
|
|
{
|
|
if (_commandLineMode != value)
|
|
{
|
|
_commandLineMode = value;
|
|
MessageService.MessageService.IsCommandLine = _commandLineMode;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static ApplicationType ApplicationType
|
|
{
|
|
get { return CommandLineMode ? ApplicationType.Default : ApplicationType.Gui; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// The mode into which this application was started.
|
|
/// </summary>
|
|
public static RuntimeMode RuntimeMode { get; private set; } = RuntimeMode.Normal;
|
|
|
|
/// <summary>
|
|
/// Whether the app is running in embedded mode.
|
|
/// </summary>
|
|
public static bool IsEmbedded
|
|
{
|
|
get
|
|
{
|
|
switch (RuntimeMode)
|
|
{
|
|
case RuntimeMode.Normal:
|
|
return false;
|
|
|
|
case RuntimeMode.EmbeddedInProcess:
|
|
case RuntimeMode.SystemTests:
|
|
return true;
|
|
|
|
default:
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Application Name.
|
|
/// </summary>
|
|
public static string ApplicationName { get; private set; }
|
|
|
|
public const int SuccessErrorCode = 0;
|
|
public const int FailureErrorCode = 1;
|
|
|
|
/// <summary>
|
|
/// Gets or sets the exit code of the application.
|
|
/// </summary>
|
|
public static int ExitCode { get; set; } = SuccessErrorCode;
|
|
|
|
/// <summary>
|
|
/// The directory used to store user-specific preferences.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// By default, this is ApplicationData/ApplicationName in the MyDocuments folder of the current user.
|
|
/// </remarks>
|
|
public static string ConfigDirectory
|
|
{
|
|
get { return _configuration == null ? null : _configuration.ConfigDirectory; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// The directory used to store global resources.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// By default, this is ApplicationRootPath/Data.
|
|
/// </remarks>
|
|
public static string DataDirectory
|
|
{
|
|
get { return _configuration == null ? null : _configuration.DataDirectory; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Paths in the Addin Tree where CommandLineHandlers are.
|
|
/// </summary>
|
|
public static IEnumerable<string> CommandLineHandlerPaths
|
|
{
|
|
get { return _configuration == null ? new string[0] : _configuration.CommandLineHandlerPaths; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Addin Repository of this application.
|
|
/// </summary>
|
|
public static AddinRepository AddinRepository { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Whether this application should start its own instance.
|
|
/// </summary>
|
|
public static bool ShouldStartOwnInstance
|
|
{
|
|
get
|
|
{
|
|
return (CommandLineMode || Arguments.Parameters.Count == 0 || !ApplicationInstanceManager.IsInstanceRunning(UniqueIdentifier));
|
|
}
|
|
}
|
|
|
|
internal static void Initialize(ApplicationStartup startup, ILogger logger)
|
|
{
|
|
Initialized = true;
|
|
|
|
ApplicationRootPath = !string.IsNullOrEmpty(startup.Configuration.ApplicationRootPath)
|
|
? startup.Configuration.ApplicationRootPath
|
|
: Path.GetDirectoryName(EntryAssembly.Location);
|
|
|
|
RuntimeMode = startup.RuntimeMode;
|
|
Arguments = startup.Arguments;
|
|
ApplicationName = startup.ApplicationName;
|
|
AddinRepository = new AddinRepository(ApplicationRootPath);
|
|
_configuration = startup.Configuration;
|
|
|
|
var defaultPropertiesFileName = $"{Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName)}Properties.xml";
|
|
var defaultPropertiesFilePath = Path.Combine(_configuration.ConfigDirectory, defaultPropertiesFileName);
|
|
PropertyService.PropertyService.DefaultFilePath = defaultPropertiesFilePath;
|
|
}
|
|
}
|
|
} |