JD2022-TU1/main/tools/apps/UAFLauncher/Application.cs

136 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using JD.Core.ProjectManaging;
using Ubisoft.CommandLine;
using Ubisoft.CommandLine.Application;
namespace Launcher
{
public class Application : CommandLineApplication<ApplicationOptions>
{
[STAThread]
public static int Main(string[] args)
{
int returnCode = 0;
try
{
var app = new Application();
if (!args.Any())
{
// When the user launches the UAF launcher explicitly, try to use the data root
// that matches the tool.
ProjectManager.Instance.InitializeDataRootFromCurrentTool(forceSave: true);
}
ProjectManager.Instance.Initialize();
ProjectManager.Instance.RegisterFileAssociations();
ProjectManager.Instance.RegisterWindowsExplorerContextMenus();
returnCode = app.Run(args);
//If the parsing of arguments fails and we have one argument, we consider that it's a file path
//and that the command is "open"
if (returnCode == -1 && args.Count() == 1)
{
try
{
var path = Path.GetFullPath(args[0]);
OpenFile(path);
}
catch (Exception)
{
return -1;
}
}
//If we don't have any parameters we don't want to trigger an error
else if (returnCode == -1 && !args.Any())
{
return 0;
}
}
catch (Exception ex)
{
MessageBox.Show("An error occurred. Details: " + ex);
return -1;
}
return returnCode;
}
protected override int ApplyOptions(CommandLineParser parser, ApplicationOptions options)
{
if (options.Open && !string.IsNullOrEmpty(options.FilePath))
{
OpenFile(options.FilePath);
return 0;
}
if (options.CopyRelativePath && !string.IsNullOrEmpty(options.FilePath))
{
CopyRelativePath(options.FilePath);
return 0;
}
if (options.CopyAbsolutePath && !string.IsNullOrEmpty(options.FilePath))
{
CopyAbsolutePath(options.FilePath);
return 0;
}
if (options.InitializeDataRoot)
{
// Everything already done.
return 0;
}
return -1;
}
static void OpenFile(string filePath)
{
var application = ProjectManager.Instance.GetAssociatedApplicationPath(filePath);
if (!string.IsNullOrEmpty(application))
{
var launchAppProcess = new Process();
launchAppProcess.StartInfo.FileName = application;
launchAppProcess.StartInfo.Arguments = GetAdditionalArguments(application) + filePath;
launchAppProcess.Start();
}
else
{
MessageBox.Show("Can't find any application associated to this file.", "No file association found.", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Environment.Exit(0);
}
private static string GetAdditionalArguments(string applicationName)
{
// Sets English as the default language for ua_engine
return applicationName.EndsWith("ua_engine.exe", StringComparison.InvariantCultureIgnoreCase) ? "language=0;" : string.Empty;
}
static void CopyRelativePath(string filePath)
{
var project = ProjectManager.Instance.GetProjectFromPath(filePath);
if (project != null)
{
string relativePath;
DataPath.ExtractRelativePath(project.DataPath, filePath, out relativePath, keepCasing:true);
Clipboard.SetText(relativePath);
}
else
{
MessageBox.Show("This file is not located in a valid UAF project.", "Not a valid project.", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
static void CopyAbsolutePath(string filePath)
{
Clipboard.SetText(filePath);
}
}
}