generated from MrSphay/codex-agent-repository-kit
Make MrTrust executable standalone
All checks were successful
Build MrTrust / build (push) Successful in 3m49s
All checks were successful
Build MrTrust / build (push) Successful in 3m49s
This commit is contained in:
@@ -1,34 +1,51 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
|
||||
#pragma warning disable CS8600, CS8602
|
||||
|
||||
namespace MrTrust
|
||||
{
|
||||
internal static class MrTrustLauncher
|
||||
{
|
||||
[STAThread]
|
||||
private static int Main()
|
||||
{
|
||||
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
|
||||
string scriptPath = Path.Combine(baseDirectory, "MrTrust.ps1");
|
||||
private const string PayloadResourcePrefix = "MrTrust.Payload.";
|
||||
|
||||
if (!File.Exists(scriptPath))
|
||||
{
|
||||
MessageBox.Show(
|
||||
"MrTrust.ps1 was not found next to MrTrust.exe.",
|
||||
"MrTrust",
|
||||
MessageBoxButtons.OK,
|
||||
MessageBoxIcon.Error);
|
||||
return 1;
|
||||
}
|
||||
private static readonly PayloadFile[] PayloadFiles =
|
||||
{
|
||||
new PayloadFile("MrTrust.ps1", "MrTrust.ps1"),
|
||||
new PayloadFile("scripts.Build-MrTrustExe.ps1", Path.Combine("scripts", "Build-MrTrustExe.ps1")),
|
||||
new PayloadFile("scripts.Install-MrTrust.ps1", Path.Combine("scripts", "Install-MrTrust.ps1")),
|
||||
new PayloadFile("scripts.New-MrTrustCertificate.ps1", Path.Combine("scripts", "New-MrTrustCertificate.ps1")),
|
||||
new PayloadFile("scripts.New-MrTrustIcon.ps1", Path.Combine("scripts", "New-MrTrustIcon.ps1")),
|
||||
new PayloadFile("scripts.New-MrTrustRelease.ps1", Path.Combine("scripts", "New-MrTrustRelease.ps1")),
|
||||
new PayloadFile("scripts.Sign-MrTrustProject.ps1", Path.Combine("scripts", "Sign-MrTrustProject.ps1")),
|
||||
new PayloadFile("scripts.Start-MrTrustGui.ps1", Path.Combine("scripts", "Start-MrTrustGui.ps1")),
|
||||
new PayloadFile("scripts.Uninstall-MrTrust.ps1", Path.Combine("scripts", "Uninstall-MrTrust.ps1")),
|
||||
new PayloadFile("assets.MrTrust.ico", Path.Combine("assets", "MrTrust.ico")),
|
||||
new PayloadFile("assets.certificates.MrSphay-LocalTrust-Root.cer", Path.Combine("assets", "certificates", "MrSphay-LocalTrust-Root.cer")),
|
||||
new PayloadFile("assets.certificates.MrSphay-CodeSigning.cer", Path.Combine("assets", "certificates", "MrSphay-CodeSigning.cer")),
|
||||
new PayloadFile("assets.certificates.thumbprints.txt", Path.Combine("assets", "certificates", "thumbprints.txt"))
|
||||
};
|
||||
|
||||
[STAThread]
|
||||
private static int Main(string[] args)
|
||||
{
|
||||
string baseDirectory = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
baseDirectory = ExtractPayload();
|
||||
string scriptPath = Path.Combine(baseDirectory, "MrTrust.ps1");
|
||||
string commandArguments = BuildCommandArguments(args);
|
||||
|
||||
ProcessStartInfo startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "powershell.exe",
|
||||
Arguments = "-NoProfile -ExecutionPolicy Bypass -File \"" + scriptPath + "\" gui",
|
||||
Arguments = "-NoProfile -ExecutionPolicy Bypass -File " + QuoteArgument(scriptPath) + " " + commandArguments,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
WorkingDirectory = baseDirectory
|
||||
@@ -40,9 +57,10 @@ namespace MrTrust
|
||||
{
|
||||
throw new InvalidOperationException("PowerShell could not be started.");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
process.WaitForExit();
|
||||
return process.ExitCode;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -53,6 +71,123 @@ namespace MrTrust
|
||||
MessageBoxIcon.Error);
|
||||
return 1;
|
||||
}
|
||||
finally
|
||||
{
|
||||
TryDeleteDirectory(baseDirectory);
|
||||
}
|
||||
}
|
||||
|
||||
private static string ExtractPayload()
|
||||
{
|
||||
Assembly assembly = Assembly.GetExecutingAssembly();
|
||||
string versionKey = GetPayloadVersionKey(assembly);
|
||||
string targetDirectory = Path.Combine(
|
||||
Path.GetTempPath(),
|
||||
"MrTrust",
|
||||
"standalone",
|
||||
versionKey,
|
||||
Guid.NewGuid().ToString("N"));
|
||||
|
||||
foreach (PayloadFile payloadFile in PayloadFiles)
|
||||
{
|
||||
string targetPath = Path.Combine(targetDirectory, payloadFile.RelativePath);
|
||||
string targetParent = Path.GetDirectoryName(targetPath);
|
||||
if (!string.IsNullOrEmpty(targetParent))
|
||||
{
|
||||
Directory.CreateDirectory(targetParent);
|
||||
}
|
||||
|
||||
using (Stream stream = assembly.GetManifestResourceStream(PayloadResourcePrefix + payloadFile.ResourceName))
|
||||
{
|
||||
if (stream == null)
|
||||
{
|
||||
throw new FileNotFoundException("Embedded MrTrust payload file was not found.", payloadFile.RelativePath);
|
||||
}
|
||||
|
||||
using (FileStream file = File.Create(targetPath))
|
||||
{
|
||||
stream.CopyTo(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return targetDirectory;
|
||||
}
|
||||
|
||||
private static void TryDeleteDirectory(string directory)
|
||||
{
|
||||
if (string.IsNullOrEmpty(directory) || !Directory.Exists(directory))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Directory.Delete(directory, true);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Best-effort cleanup only. A locked icon or antivirus scan should not mask the command result.
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetPayloadVersionKey(Assembly assembly)
|
||||
{
|
||||
string location = Application.ExecutablePath;
|
||||
if (File.Exists(location))
|
||||
{
|
||||
FileInfo fileInfo = new FileInfo(location);
|
||||
return fileInfo.Length.ToString("x") + "-" + fileInfo.LastWriteTimeUtc.Ticks.ToString("x");
|
||||
}
|
||||
|
||||
return assembly.GetName().Version.ToString();
|
||||
}
|
||||
|
||||
private static string BuildCommandArguments(string[] args)
|
||||
{
|
||||
string[] effectiveArgs = args.Length == 0 ? new[] { "gui" } : args;
|
||||
return string.Join(" ", effectiveArgs.Select(QuoteArgument).ToArray());
|
||||
}
|
||||
|
||||
private static string QuoteArgument(string value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
return "\"\"";
|
||||
}
|
||||
|
||||
if (value.IndexOfAny(new[] { ' ', '\t', '\n', '\r', '"' }) < 0)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.Append('"');
|
||||
foreach (char character in value)
|
||||
{
|
||||
if (character == '"')
|
||||
{
|
||||
builder.Append('\\');
|
||||
}
|
||||
|
||||
builder.Append(character);
|
||||
}
|
||||
|
||||
builder.Append('"');
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
private sealed class PayloadFile
|
||||
{
|
||||
public PayloadFile(string resourceName, string relativePath)
|
||||
{
|
||||
ResourceName = resourceName;
|
||||
RelativePath = relativePath;
|
||||
}
|
||||
|
||||
public string ResourceName { get; private set; }
|
||||
|
||||
public string RelativePath { get; private set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,23 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<PublishSingleFile>true</PublishSingleFile>
|
||||
<SelfContained>false</SelfContained>
|
||||
<SelfContained>true</SelfContained>
|
||||
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
|
||||
<ApplicationIcon>..\assets\MrTrust.ico</ApplicationIcon>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="..\MrTrust.ps1" LogicalName="MrTrust.Payload.MrTrust.ps1" />
|
||||
<EmbeddedResource Include="..\scripts\Build-MrTrustExe.ps1" LogicalName="MrTrust.Payload.scripts.Build-MrTrustExe.ps1" />
|
||||
<EmbeddedResource Include="..\scripts\Install-MrTrust.ps1" LogicalName="MrTrust.Payload.scripts.Install-MrTrust.ps1" />
|
||||
<EmbeddedResource Include="..\scripts\New-MrTrustCertificate.ps1" LogicalName="MrTrust.Payload.scripts.New-MrTrustCertificate.ps1" />
|
||||
<EmbeddedResource Include="..\scripts\New-MrTrustIcon.ps1" LogicalName="MrTrust.Payload.scripts.New-MrTrustIcon.ps1" />
|
||||
<EmbeddedResource Include="..\scripts\New-MrTrustRelease.ps1" LogicalName="MrTrust.Payload.scripts.New-MrTrustRelease.ps1" />
|
||||
<EmbeddedResource Include="..\scripts\Sign-MrTrustProject.ps1" LogicalName="MrTrust.Payload.scripts.Sign-MrTrustProject.ps1" />
|
||||
<EmbeddedResource Include="..\scripts\Start-MrTrustGui.ps1" LogicalName="MrTrust.Payload.scripts.Start-MrTrustGui.ps1" />
|
||||
<EmbeddedResource Include="..\scripts\Uninstall-MrTrust.ps1" LogicalName="MrTrust.Payload.scripts.Uninstall-MrTrust.ps1" />
|
||||
<EmbeddedResource Include="..\assets\MrTrust.ico" LogicalName="MrTrust.Payload.assets.MrTrust.ico" />
|
||||
<EmbeddedResource Include="..\assets\certificates\MrSphay-LocalTrust-Root.cer" LogicalName="MrTrust.Payload.assets.certificates.MrSphay-LocalTrust-Root.cer" />
|
||||
<EmbeddedResource Include="..\assets\certificates\MrSphay-CodeSigning.cer" LogicalName="MrTrust.Payload.assets.certificates.MrSphay-CodeSigning.cer" />
|
||||
<EmbeddedResource Include="..\assets\certificates\thumbprints.txt" LogicalName="MrTrust.Payload.assets.certificates.thumbprints.txt" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user