delete the console log window, always show LogWindow instead

This commit is contained in:
adelikat 2019-12-29 14:39:44 -06:00
parent ab812ab081
commit bdd0d56a70
7 changed files with 6 additions and 253 deletions

View File

@ -115,7 +115,6 @@ namespace BizHawk.Client.Common
public bool AutofireLagFrames = true;
public int SaveSlot = 0; // currently selected savestate slot
public bool AutoLoadLastSaveSlot = false;
public bool WIN32_CONSOLE = true;
public bool SkipLagFrame = false;
public bool SuppressAskSave = false;
public bool AVI_CaptureOSD = false;

View File

@ -1,18 +1,13 @@
using System;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using BizHawk.Common;
using BizHawk.Client.Common;
// thanks! - http://sharp-developer.net/ru/CodeBank/WinForms/GuiConsole.aspx
// todo - quit using Console.WriteLine (well, we can leave it hooked up as a backstop)
// use a different method instead, so we can collect unicode data
// also, collect log data independently of whether the log window is open
// we also need to dice it into lines so that we can have a backlog policy
namespace BizHawk.Client.EmuHawk
{
internal static class LogConsole
@ -68,120 +63,7 @@ namespace BizHawk.Client.EmuHawk
public Action<string> Emit;
}
internal static string SkipEverythingButProgramInCommandLine(string cmdLine)
{
// skip past the program name. can anyone think of a better way to do this?
// we could use CommandLineToArgvW (commented out below) but then we would just have to re-assemble and potentially re-quote it
int childCmdLine = 0;
int lastSlash = 0;
int lastGood = 0;
bool quote = false;
for (; ; )
{
char cur = cmdLine[childCmdLine];
childCmdLine++;
if (childCmdLine == cmdLine.Length) break;
bool thisIsQuote = (cur == '\"');
if (cur == '\\' || cur == '/')
{
lastSlash = childCmdLine;
}
if (quote)
{
if (thisIsQuote)
quote = false;
else lastGood = childCmdLine;
}
else
{
if (cur == ' ' || cur == '\t')
break;
if (thisIsQuote)
quote = true;
lastGood = childCmdLine;
}
}
string remainder = cmdLine.Substring(childCmdLine);
string path = cmdLine.Substring(lastSlash, lastGood - lastSlash);
return $"{path} {remainder}";
}
private static IntPtr _oldOut, _conOut;
private static bool _hasConsole;
private static bool _attachedConsole;
private static bool _shouldRedirectStdout;
public static void CreateConsole()
{
// (see desmume for the basis of some of this logic)
if (_hasConsole)
{
return;
}
if (_oldOut == IntPtr.Zero)
{
_oldOut = ConsoleImports.GetStdHandle( -11 ); // STD_OUTPUT_HANDLE
}
var fileType = ConsoleImports.GetFileType(_oldOut);
// stdout is already connected to something. keep using it and don't let the console interfere
_shouldRedirectStdout = (fileType == ConsoleImports.FileType.FileTypeUnknown || fileType == ConsoleImports.FileType.FileTypePipe);
// attach to an existing console
_attachedConsole = false;
// ever since a recent KB, XP-based systems glitch out when AttachConsole is called and there's no console to attach to.
if (Environment.OSVersion.Version.Major != 5)
{
if (ConsoleImports.AttachConsole(-1))
{
_hasConsole = true;
_attachedConsole = true;
}
}
if (!_attachedConsole)
{
ConsoleImports.FreeConsole();
if (ConsoleImports.AllocConsole())
{
//set icons for the console so we can tell them apart from the main window
Win32Imports.SendMessage(ConsoleImports.GetConsoleWindow(), 0x0080/*WM_SETICON*/, (IntPtr)0/*ICON_SMALL*/, Properties.Resources.console16x16.GetHicon());
Win32Imports.SendMessage(ConsoleImports.GetConsoleWindow(), 0x0080/*WM_SETICON*/, (IntPtr)1/*ICON_LARGE*/, Properties.Resources.console32x32.GetHicon());
_hasConsole = true;
}
else
{
MessageBox.Show($"Couldn't allocate win32 console: {Marshal.GetLastWin32Error()}");
}
}
if (_hasConsole)
{
IntPtr ptr = ConsoleImports.GetCommandLine();
string commandLine = Marshal.PtrToStringAuto(ptr);
Console.Title = SkipEverythingButProgramInCommandLine(commandLine);
}
if (_shouldRedirectStdout)
{
_conOut = ConsoleImports.CreateFile("CONOUT$", 0x40000000, 2, IntPtr.Zero, 3, 0, IntPtr.Zero);
if (!ConsoleImports.SetStdHandle(-11, _conOut))
throw new Exception($"{nameof(ConsoleImports.SetStdHandle)}() failed");
}
//DotNetRewireConout();
_hasConsole = true;
if (_attachedConsole)
{
Console.WriteLine();
Console.WriteLine("use cmd /c {0} to get more sensible console behaviour", Path.GetFileName(PathManager.GetGlobalBasePathAbsolute()));
}
}
static void ReleaseConsole()
{
@ -190,61 +72,20 @@ namespace BizHawk.Client.EmuHawk
return;
}
if (_shouldRedirectStdout)
{
ConsoleImports.CloseHandle(_conOut);
}
if (!_attachedConsole)
{
ConsoleImports.FreeConsole();
}
ConsoleImports.SetStdHandle(-11, _oldOut);
_conOut = IntPtr.Zero;
_hasConsole = false;
}
/// <summary>
/// pops the console in front of the main window (where it should probably go after booting up the game).
/// maybe this should be optional, or maybe we can somehow position the console sensibly.
/// sometimes it annoys me, but i really need it on top while debugging or else i will be annoyed.
/// best of all would be to position it beneath the BizHawk main window somehow.
/// </summary>
public static void PositionConsole()
{
if (ConsoleVisible == false)
{
return;
}
if (Global.Config.WIN32_CONSOLE)
{
IntPtr x = ConsoleImports.GetConsoleWindow();
ConsoleImports.SetForegroundWindow(x);
}
}
public static void ShowConsole(MainForm parent)
{
if (ConsoleVisible) return;
ConsoleVisible = true;
if (Global.Config.WIN32_CONSOLE)
{
_needToRelease = true;
CreateConsole();
}
else
{
_logStream = new LogStream();
Log.HACK_LOG_STREAM = _logStream;
Console.SetOut(new StreamWriter(_logStream) { AutoFlush = true });
_window = new LogWindow(parent);
_window.Show();
_logStream.Emit = str => { _window.Append(str); };
}
_logStream = new LogStream();
Log.HACK_LOG_STREAM = _logStream;
Console.SetOut(new StreamWriter(_logStream) { AutoFlush = true });
_window = new LogWindow(parent);
_window.Show();
_logStream.Emit = str => { _window.Append(str); };
}
public static void HideConsole()

View File

@ -512,7 +512,6 @@ namespace BizHawk.Client.EmuHawk
public int ProgramRunLoop()
{
CheckMessages(); // can someone leave a note about why this is needed?
if (!OSTailoredCode.IsUnixHost) LogConsole.PositionConsole();
// needs to be done late, after the log console snaps on top
// fullscreen should snap on top even harder!

View File

@ -69,8 +69,6 @@
this.label13 = new System.Windows.Forms.Label();
this.FrameAdvSkipLagCheckbox = new System.Windows.Forms.CheckBox();
this.BackupSRamCheckbox = new System.Windows.Forms.CheckBox();
this.label4 = new System.Windows.Forms.Label();
this.LogWindowAsConsoleCheckbox = new System.Windows.Forms.CheckBox();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.label9 = new System.Windows.Forms.Label();
this.label10 = new System.Windows.Forms.Label();
@ -307,8 +305,6 @@
this.tabPage3.Controls.Add(this.label13);
this.tabPage3.Controls.Add(this.FrameAdvSkipLagCheckbox);
this.tabPage3.Controls.Add(this.BackupSRamCheckbox);
this.tabPage3.Controls.Add(this.label4);
this.tabPage3.Controls.Add(this.LogWindowAsConsoleCheckbox);
this.tabPage3.Location = new System.Drawing.Point(4, 22);
this.tabPage3.Name = "tabPage3";
this.tabPage3.Size = new System.Drawing.Size(386, 376);
@ -527,25 +523,6 @@
this.BackupSRamCheckbox.Text = "Backup SaveRAM to .SaveRAM.bak";
this.BackupSRamCheckbox.UseVisualStyleBackColor = true;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(24, 23);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(234, 13);
this.label4.TabIndex = 2;
this.label4.Text = "If off, the log window will be a dialog box instead";
//
// LogWindowAsConsoleCheckbox
//
this.LogWindowAsConsoleCheckbox.AutoSize = true;
this.LogWindowAsConsoleCheckbox.Location = new System.Drawing.Point(6, 3);
this.LogWindowAsConsoleCheckbox.Name = "LogWindowAsConsoleCheckbox";
this.LogWindowAsConsoleCheckbox.Size = new System.Drawing.Size(233, 17);
this.LogWindowAsConsoleCheckbox.TabIndex = 1;
this.LogWindowAsConsoleCheckbox.Text = "Create the log window as a console window";
this.LogWindowAsConsoleCheckbox.UseVisualStyleBackColor = true;
//
// label9
//
this.label9.AutoSize = true;
@ -624,8 +601,6 @@
private System.Windows.Forms.Label label3;
private System.Windows.Forms.CheckBox SingleInstanceModeCheckbox;
private System.Windows.Forms.TabPage tabPage3;
private System.Windows.Forms.CheckBox LogWindowAsConsoleCheckbox;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.ToolTip toolTip1;
private System.Windows.Forms.CheckBox BackupSRamCheckbox;
private System.Windows.Forms.CheckBox FrameAdvSkipLagCheckbox;

View File

@ -63,7 +63,6 @@ namespace BizHawk.Client.EmuHawk
groupBox2.Enabled = AutosaveSRAMCheckbox.Checked;
AutosaveSaveRAMSeconds = _config.FlushSaveRamFrames / 60;
FrameAdvSkipLagCheckbox.Checked = _config.SkipLagFrame;
LogWindowAsConsoleCheckbox.Checked = _config.WIN32_CONSOLE;
LuaDuringTurboCheckbox.Checked = _config.RunLuaDuringTurbo;
cbMoviesOnDisk.Checked = _config.MoviesOnDisk;
cbMoviesInAWE.Checked = _config.MoviesInAWE;
@ -79,14 +78,6 @@ namespace BizHawk.Client.EmuHawk
default:
throw new ArgumentOutOfRangeException();
}
if (LogConsole.ConsoleVisible)
{
LogWindowAsConsoleCheckbox.Enabled = false;
toolTip1.SetToolTip(
LogWindowAsConsoleCheckbox,
"This can not be changed while the log window is open. I know, it's annoying.");
}
}
private void OkBtn_Click(object sender, EventArgs e)
@ -109,7 +100,6 @@ namespace BizHawk.Client.EmuHawk
if (_mainForm.AutoFlushSaveRamIn > _config.FlushSaveRamFrames)
_mainForm.AutoFlushSaveRamIn = _config.FlushSaveRamFrames;
_config.SkipLagFrame = FrameAdvSkipLagCheckbox.Checked;
_config.WIN32_CONSOLE = LogWindowAsConsoleCheckbox.Checked;
_config.RunLuaDuringTurbo = LuaDuringTurboCheckbox.Checked;
_config.MoviesOnDisk = cbMoviesOnDisk.Checked;
_config.MoviesInAWE = cbMoviesInAWE.Checked;

View File

@ -101,7 +101,6 @@
<Compile Include="Win32/ThreadHacks.cs" />
<Compile Include="Win32/Win32Imports.cs" />
<Compile Include="Win32\AVIWriterImports.cs" />
<Compile Include="Win32\ConsoleImports.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>

View File

@ -1,50 +0,0 @@
using System;
using System.Runtime.InteropServices;
namespace BizHawk.Common
{
public static class ConsoleImports
{
public enum FileType : uint
{
FileTypeUnknown = 0,
FileTypeDisk = 1,
FileTypeChar = 2,
FileTypePipe = 3,
FileTypeRemote = 0x8000
}
[DllImport("kernel32.dll")]
public static extern FileType GetFileType(IntPtr hFile);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetCommandLine();
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll", SetLastError = true)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool AttachConsole(int dwProcessId);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool AllocConsole();
[DllImport("kernel32.dll", SetLastError = false)]
public static extern bool FreeConsole();
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetStdHandle(int nStdHandle, IntPtr hConsoleOutput);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CreateFile(string fileName, int desiredAccess, int shareMode, IntPtr securityAttributes, int creationDisposition, int flagsAndAttributes, IntPtr templateFile);
[DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
public static extern bool CloseHandle(IntPtr handle);
}
}