using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Amphora;
using Blacklight.Core;
using Microsoft.Win32.SafeHandles;
namespace Foundations
{
///
/// Does what every desktop program does first, which is read its command line and bring graphics up.
///
public static class DesktopStartup
{
///
/// The exit code of a run that found no graphics adapter.
///
public const int NoAdapterExitCode = 1;
private const uint AttachParentProcess = 0xFFFFFFFF;
private const int StandardOutput = -11;
private const int StandardError = -12;
private const uint GenericWrite = 0x40000000;
private const uint ShareReadWrite = 3;
private const uint OpenExisting = 3;
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AttachConsole(uint processId);
[DllImport("kernel32.dll")]
private static extern IntPtr GetStdHandle(int standardHandle);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern SafeFileHandle CreateFile(string fileName, uint access, uint share, IntPtr security,
uint creation, uint attributes, IntPtr template);
private static bool IsUsable(IntPtr handle)
{
return handle != IntPtr.Zero && handle != new IntPtr(-1);
}
private static void Write(Stream stream, string text)
{
using (var writer = new StreamWriter(stream))
{
writer.WriteLine(text);
}
}
// A windowed program has no console of its own. Its text goes where it was redirected, else to the console it
// was started from, else to a message box, which is what a double-click needs.
private static void Show(string text, string caption, int standardHandle)
{
IntPtr redirected = GetStdHandle(standardHandle);
if (IsUsable(redirected))
{
Write(new FileStream(new SafeFileHandle(redirected, ownsHandle: false), FileAccess.Write), text);
return;
}
if (AttachConsole(AttachParentProcess))
{
SafeFileHandle console = CreateFile("CONOUT$", GenericWrite, ShareReadWrite, IntPtr.Zero, OpenExisting,
attributes: 0, IntPtr.Zero);
if (!console.IsInvalid)
{
Write(new FileStream(console, FileAccess.Write), text);
return;
}
}
MessageBox.Show(text, caption);
}
///
/// Reads the command line, shows help or what was wrong with it, and brings graphics up. Returns whether the
/// program should go on to run, and otherwise what it should exit with.
///
///
/// Call it first in Main, and then ask the flags for the program's own. It chooses the graphics adapter and
/// the vertical sync mode, from the adapter flag of Blacklight.Core and the vsync flag of DesktopFlags. From
/// here on, an exception nothing catches shows a report and ends the program.
///
public static bool TryBegin(string[] arguments, out Flags flags, out int exitCode)
{
if (!Startup.TryRead(FlagRegistry.ScanLoadedAssemblies(), arguments, out flags, out string message,
out exitCode))
{
int standardHandle = StandardOutput;
if (exitCode != 0)
{
standardHandle = StandardError;
}
Show(message, "Command line", standardHandle);
return false;
}
// Ahead of graphics, so a failure to bring it up is reported too.
DesktopCrashHandler.Install(flags);
GraphicsAdapterManager.Singleton.ForceStartup();
if (!GraphicsAdapterManager.Singleton.ChooseModule(flags.Get(GraphicsFlags.Adapter)))
{
MessageBox.Show("Could not locate any Blacklight graphics adapters (eg: DirectX). Will now exit.",
"Critical Error");
exitCode = NoAdapterExitCode;
return false;
}
GraphicsAdapterManager.Singleton.CurrentModule.VerticalSync = VerticalSyncMode.Off;
if (flags.Get(DesktopFlags.Vsync))
{
GraphicsAdapterManager.Singleton.CurrentModule.VerticalSync = VerticalSyncMode.Driver;
}
return true;
}
}
}