Convert LogWindow to a tool that implements IAutoToolformConfig

This commit is contained in:
adelikat 2019-12-29 15:05:45 -06:00
parent bdd0d56a70
commit 6e49f1c07d
8 changed files with 110 additions and 216 deletions

View File

@ -105,7 +105,6 @@ namespace BizHawk.Client.Common
public bool MoviesInAWE = false;
public bool HotkeyConfigAutoTab = true;
public bool InputConfigAutoTab = true;
public bool ShowLogWindow = false;
public bool BackupSavestates = true;
public bool SaveScreenshotWithStates = true;
public int BigScreenshotSize = 128 * 1024;
@ -312,13 +311,6 @@ namespace BizHawk.Client.Common
public string SoundDevice = "";
public int SoundBufferSizeMs = 100;
// Log Window
public bool LogWindowSaveWindowPosition = true;
public int LogWindowWndx = -1;
public int LogWindowWndy = -1;
public int LogWindowWidth = -1;
public int LogWindowHeight = -1;
// Lua
public RecentFiles RecentLua = new RecentFiles(8);
public RecentFiles RecentLuaSession = new RecentFiles(8);

View File

@ -658,7 +658,6 @@
<Compile Include="IControlMainform.cs" />
<Compile Include="Input\IPCKeyInput.cs" />
<Compile Include="JumpLists.cs" />
<Compile Include="LogConsole.cs" />
<Compile Include="LogWindow.cs">
<SubType>Form</SubType>
</Compile>

View File

@ -1,131 +0,0 @@
using System;
using System.Text;
using System.IO;
using BizHawk.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
{
public static bool ConsoleVisible { get; private set; }
private static LogWindow _window;
private static LogStream _logStream;
private static bool _needToRelease;
private class LogStream : Stream
{
public override bool CanRead => false;
public override bool CanSeek => false;
public override bool CanWrite => true;
public override void Flush()
{
//TODO - maybe this will help with decoding
}
public override long Length => throw new NotImplementedException();
public override long Position
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
// TODO - buffer undecoded characters (this may be important)
//(use decoder = System.Text.Encoding.Unicode.GetDecoder())
string str = Encoding.ASCII.GetString(buffer, offset, count);
Emit?.Invoke(str);
}
public Action<string> Emit;
}
private static bool _hasConsole;
static void ReleaseConsole()
{
if (!_hasConsole)
{
return;
}
_hasConsole = false;
}
public static void ShowConsole(MainForm parent)
{
if (ConsoleVisible) return;
ConsoleVisible = true;
_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()
{
if (ConsoleVisible == false)
{
return;
}
Console.SetOut(TextWriter.Null);
ConsoleVisible = false;
if (_needToRelease)
{
ReleaseConsole();
_needToRelease = false;
}
else
{
_logStream.Close();
_logStream = null;
Log.HACK_LOG_STREAM = null;
_window.Close();
_window = null;
}
}
public static void NotifyLogWindowClosing()
{
Console.SetOut(TextWriter.Null);
ConsoleVisible = false;
_logStream?.Close();
Log.HACK_LOG_STREAM = null;
}
public static void SaveConfigSettings()
{
if (_window != null && _window.IsHandleCreated)
{
_window.SaveConfigSettings();
}
}
}
}

View File

@ -38,6 +38,7 @@ namespace BizHawk.Client.EmuHawk
this.AddToGameDbBtn = new System.Windows.Forms.Button();
this.virtualListView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.MenuStrip = new MenuStripEx();
this.tableLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
@ -107,7 +108,6 @@ namespace BizHawk.Client.EmuHawk
//
// AddToGameDbBtn
//
this.AddToGameDbBtn.Image = global::BizHawk.Client.EmuHawk.Properties.Resources.RetroQuestion;
this.AddToGameDbBtn.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.AddToGameDbBtn.Location = new System.Drawing.Point(246, 3);
this.AddToGameDbBtn.Name = "AddToGameDbBtn";
@ -125,18 +125,26 @@ namespace BizHawk.Client.EmuHawk
this.virtualListView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.virtualListView1.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.virtualListView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
this.virtualListView1.VirtualListSize = 0;
this.virtualListView1.Location = new System.Drawing.Point(0, 0);
this.virtualListView1.HideSelection = false;
this.virtualListView1.Location = new System.Drawing.Point(0, 24);
this.virtualListView1.Name = "virtualListView1";
this.virtualListView1.Size = new System.Drawing.Size(675, 367);
this.virtualListView1.Size = new System.Drawing.Size(675, 343);
this.virtualListView1.TabIndex = 8;
this.virtualListView1.UseCompatibleStateImageBehavior = false;
this.virtualListView1.View = System.Windows.Forms.View.Details;
this.virtualListView1.VirtualMode = true;
this.virtualListView1.RetrieveVirtualItem += new RetrieveVirtualItemEventHandler(this.ListView_QueryItemText);
this.virtualListView1.RetrieveVirtualItem += new System.Windows.Forms.RetrieveVirtualItemEventHandler(this.ListView_QueryItemText);
this.virtualListView1.ClientSizeChanged += new System.EventHandler(this.ListView_ClientSizeChanged);
this.virtualListView1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ListView_KeyDown);
//
// MenuStrip
//
this.MenuStrip.Location = new System.Drawing.Point(0, 0);
this.MenuStrip.Name = "MenuStrip";
this.MenuStrip.Size = new System.Drawing.Size(675, 24);
this.MenuStrip.TabIndex = 9;
this.MenuStrip.Text = "menuStrip1";
//
// LogWindow
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@ -145,6 +153,8 @@ namespace BizHawk.Client.EmuHawk
this.ClientSize = new System.Drawing.Size(675, 397);
this.Controls.Add(this.virtualListView1);
this.Controls.Add(this.tableLayoutPanel1);
this.Controls.Add(this.MenuStrip);
this.MainMenuStrip = this.MenuStrip;
this.MinimumSize = new System.Drawing.Size(171, 97);
this.Name = "LogWindow";
this.ShowIcon = false;
@ -167,5 +177,6 @@ namespace BizHawk.Client.EmuHawk
private System.Windows.Forms.Button buttonCopy;
private System.Windows.Forms.Button buttonCopyAll;
private System.Windows.Forms.Button AddToGameDbBtn;
private MenuStripEx MenuStrip;
}
}

View File

@ -1,52 +1,74 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using BizHawk.Common;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Common.IEmulatorExtensions;
using BizHawk.Client.Common;
//todo - perks - pause, copy to clipboard, backlog length limiting
// todo - perks - pause, copy to clipboard, backlog length limiting
namespace BizHawk.Client.EmuHawk
{
public partial class LogWindow : Form
public partial class LogWindow : ToolFormBase, IToolFormAutoConfig
{
//TODO: only show add to game db when this is a Rom details dialog
//Let user decide what type (instead of always adding it as a good dump)
// TODO: only show add to game db when this is a Rom details dialog
// Let user decide what type (instead of always adding it as a good dump)
private readonly List<string> _lines = new List<string>();
private readonly MainForm _mainForm;
private LogStream _logStream;
public LogWindow(MainForm mainForm)
public LogWindow()
{
_mainForm = mainForm;
InitializeComponent();
Closing += (o, e) =>
{
Global.Config.ShowLogWindow = false;
mainForm.NotifyLogWindowClosing();
LogConsole.NotifyLogWindowClosing();
SaveConfigSettings();
Detach();
};
ListView_ClientSizeChanged(null, null);
Attach();
}
public static void ShowReport(string title, string report, MainForm parent)
public void UpdateValues() { } // TODO
public void NewUpdate(ToolFormUpdateType type) { }
public void FastUpdate() { }
public void Restart() { }
public bool AskSaveChanges() => true;
public bool UpdateBefore => true;
private void Attach()
{
_logStream = new LogStream();
Log.HACK_LOG_STREAM = _logStream;
Console.SetOut(new StreamWriter(_logStream) { AutoFlush = true });
_logStream.Emit = Append;
}
private void Detach()
{
Console.SetOut(TextWriter.Null);
_logStream.Close();
_logStream = null;
Log.HACK_LOG_STREAM = null;
}
public void ShowReport(string title, string report)
{
using var dlg = new LogWindow(parent);
var ss = report.Split('\n');
foreach (var s in ss)
{
dlg._lines.Add(s.TrimEnd('\r'));
_lines.Add(s.TrimEnd('\r'));
}
dlg.virtualListView1.VirtualListSize = ss.Length;
dlg.Text = title;
dlg.btnClear.Visible = false;
dlg.ShowDialog(parent);
virtualListView1.VirtualListSize = ss.Length;
Text = title;
btnClear.Visible = false;
}
public void Append(string str)
@ -76,31 +98,9 @@ namespace BizHawk.Client.EmuHawk
private void LogWindow_Load(object sender, EventArgs e)
{
if (Global.Config.LogWindowSaveWindowPosition)
{
if (Global.Config.LogWindowSaveWindowPosition && Global.Config.LogWindowWndx >= 0 && Global.Config.LogWindowWndy >= 0)
Location = new Point(Global.Config.LogWindowWndx, Global.Config.LogWindowWndy);
if (Global.Config.LogWindowWidth >= 0 && Global.Config.LogWindowHeight >= 0)
{
Size = new Size(Global.Config.LogWindowWidth, Global.Config.LogWindowHeight);
}
}
HideShowGameDbButton();
}
public void SaveConfigSettings()
{
if (Global.Config.LogWindowSaveWindowPosition)
{
Global.Config.LogWindowWndx = Location.X;
Global.Config.LogWindowWndy = Location.Y;
Global.Config.LogWindowWidth = Right - Left;
Global.Config.LogWindowHeight = Bottom - Top;
}
}
private void ListView_QueryItemText(object sender, RetrieveVirtualItemEventArgs e)
{
e.Item = new ListViewItem(_lines[e.ItemIndex]);
@ -153,9 +153,54 @@ namespace BizHawk.Client.EmuHawk
var userDb = Path.Combine(PathManager.GetExeDirectoryAbsolute(), "gamedb", "gamedb_user.txt");
Global.Game.Status = gameDbEntry.Status = picker.PickedStatus;
Database.SaveDatabaseEntry(userDb, gameDbEntry);
_mainForm.UpdateDumpIcon();
MainForm.UpdateDumpIcon();
HideShowGameDbButton();
}
}
private class LogStream : Stream
{
public override bool CanRead => false;
public override bool CanSeek => false;
public override bool CanWrite => true;
public override void Flush()
{
//TODO - maybe this will help with decoding
}
public override long Length => throw new NotImplementedException();
public override long Position
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
// TODO - buffer undecoded characters (this may be important)
//(use decoder = System.Text.Encoding.Unicode.GetDecoder())
string str = Encoding.ASCII.GetString(buffer, offset, count);
Emit?.Invoke(str);
}
public Action<string> Emit;
}
}
}

View File

@ -117,4 +117,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="MenuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>

View File

@ -747,13 +747,11 @@ namespace BizHawk.Client.EmuHawk
SwitchToFullscreenMenuItem.ShortcutKeyDisplayString = Config.HotkeyBindings["Full Screen"].Bindings;
DisplayStatusBarMenuItem.Checked = Config.DispChrome_StatusBarWindowed;
DisplayLogWindowMenuItem.Checked = Config.ShowLogWindow;
DisplayLogWindowMenuItem.Checked = Tools.IsLoaded<LogWindow>();
DisplayLagCounterMenuItem.Enabled = Emulator.CanPollInput();
DisplayMessagesMenuItem.Checked = Config.DisplayMessages;
DisplayLogWindowMenuItem.Enabled = !OSTailoredCode.IsUnixHost;
}
private void WindowSizeSubMenu_DropDownOpened(object sender, EventArgs e)
@ -847,16 +845,7 @@ namespace BizHawk.Client.EmuHawk
private void DisplayLogWindowMenuItem_Click(object sender, EventArgs e)
{
Config.ShowLogWindow ^= true;
if (Config.ShowLogWindow)
{
LogConsole.ShowConsole(this);
}
else
{
LogConsole.HideConsole();
}
Tools.Load<LogWindow>();
}
#endregion
@ -3194,7 +3183,8 @@ namespace BizHawk.Client.EmuHawk
if (!string.IsNullOrEmpty(details))
{
Sound.StopSound();
LogWindow.ShowReport("Dump Status Report", details, this);
Tools.Load<LogWindow>();
((LogWindow) Tools.Get<LogWindow>()).ShowReport("Dump Status Report", details);
Sound.StartSound();
}
}

View File

@ -223,11 +223,6 @@ namespace BizHawk.Client.EmuHawk
InitializeComponent();
SetImages();
Game = GameInfo.NullInstance;
if (Config.ShowLogWindow && !OSTailoredCode.IsUnixHost)
{
LogConsole.ShowConsole(this);
DisplayLogWindowMenuItem.Checked = true;
}
_throttle = new Throttle();
@ -1219,11 +1214,6 @@ namespace BizHawk.Client.EmuHawk
Tools.Load<LuaConsole>();
}
public void NotifyLogWindowClosing()
{
DisplayLogWindowMenuItem.Checked = false;
}
public void ClickSpeedItem(int num)
{
if ((ModifierKeys & Keys.Control) != 0)
@ -2406,11 +2396,6 @@ namespace BizHawk.Client.EmuHawk
Config.MainWndy = -1;
}
if (Config.ShowLogWindow)
{
LogConsole.SaveConfigSettings();
}
if (string.IsNullOrEmpty(path))
{
path = PathManager.DefaultIniPath;