Remove #if WINDOWS, using runtime check where applicable

This commit is contained in:
YoshiRulz 2019-05-18 20:17:02 +10:00
parent 1887420244
commit 35056ae2d9
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
10 changed files with 75 additions and 80 deletions

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Drawing;
using BizHawk.Common;
using BizHawk.Emulation.Common;
// ReSharper disable FieldCanBeMadeReadOnly.Global
@ -370,11 +371,9 @@ namespace BizHawk.Client.Common
public int DispCropBottom = 0;
// Sound options
#if WINDOWS
public ESoundOutputMethod SoundOutputMethod = ESoundOutputMethod.DirectSound;
#else
public ESoundOutputMethod SoundOutputMethod = ESoundOutputMethod.OpenAL;
#endif
public ESoundOutputMethod SoundOutputMethod = OSTailoredCode.CurrentOS == OSTailoredCode.DistinctOS.Windows
? ESoundOutputMethod.DirectSound
: ESoundOutputMethod.OpenAL; // force OpenAL for Unix when config is generated
public bool SoundEnabled = true;
public bool SoundEnabledNormal = true;
public bool SoundEnabledRWFF = true;

View File

@ -1,5 +1,8 @@
using System;
using System.Runtime.InteropServices;
using BizHawk.Common;
using NLua;
// TODO - evaluate for re-entrancy problems
@ -18,12 +21,10 @@ namespace BizHawk.Client.Common
private string _currentDirectory;
#if WINDOWS
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetCurrentDirectoryW(byte* lpPathName);
[DllImport("kernel32.dll", SetLastError=true)]
static extern uint GetCurrentDirectoryW(uint nBufferLength, byte* pBuffer);
#endif
private bool CoolSetCurrentDirectory(string path, string currDirSpeedHack = null)
{
@ -42,40 +43,43 @@ namespace BizHawk.Client.Common
return true;
}
// WARNING: setting the current directory is SLOW!!! security checks for some reason.
// so we're bypassing it with windows hacks
#if WINDOWS
if (OSTailoredCode.CurrentOS == OSTailoredCode.DistinctOS.Windows)
{
// WARNING: setting the current directory is SLOW!!! security checks for some reason.
// so we're bypassing it with windows hacks
fixed (byte* pstr = &System.Text.Encoding.Unicode.GetBytes($"{target}\0")[0])
return SetCurrentDirectoryW(pstr);
#else
if (System.IO.Directory.Exists(CurrentDirectory)) // race condition for great justice
{
Environment.CurrentDirectory = CurrentDirectory; // thats right, you can't set a directory as current that doesnt exist because .net's got to do SENSELESS SLOW-ASS SECURITY CHECKS on it and it can't do that on a NONEXISTENT DIRECTORY
return true;
}
else
{
return false;
}
#endif
}
else
{
if (System.IO.Directory.Exists(_currentDirectory)) // race condition for great justice
{
Environment.CurrentDirectory = _currentDirectory; // thats right, you can't set a directory as current that doesnt exist because .net's got to do SENSELESS SLOW-ASS SECURITY CHECKS on it and it can't do that on a NONEXISTENT DIRECTORY
return true;
}
else
{
return false;
}
}
}
private string CoolGetCurrentDirectory()
{
// GUESS WHAT!
// .NET DOES A SECURITY CHECK ON THE DIRECTORY WE JUST RETRIEVED
// AS IF ASKING FOR THE CURRENT DIRECTORY IS EQUIVALENT TO TRYING TO ACCESS IT
// SCREW YOU
#if WINDOWS
if (OSTailoredCode.CurrentOS == OSTailoredCode.DistinctOS.Windows)
{
// GUESS WHAT!
// .NET DOES A SECURITY CHECK ON THE DIRECTORY WE JUST RETRIEVED
// AS IF ASKING FOR THE CURRENT DIRECTORY IS EQUIVALENT TO TRYING TO ACCESS IT
// SCREW YOU
var buf = new byte[32768];
fixed(byte* pBuf = &buf[0])
{
uint ret = GetCurrentDirectoryW(32767, pBuf);
return System.Text.Encoding.Unicode.GetString(buf, 0, (int)ret*2);
}
#else
fixed (byte* pBuf = &buf[0])
return System.Text.Encoding.Unicode.GetString(buf, 0, 2 * (int) GetCurrentDirectoryW(32767, pBuf));
}
else
{
return Environment.CurrentDirectory;
#endif
}
}
private void Sandbox(Action callback, Action exceptionCallback)

View File

@ -6,6 +6,7 @@ using System.Diagnostics;
using System.Windows.Forms;
using BizHawk.Client.Common;
using BizHawk.Common;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk
@ -85,12 +86,9 @@ namespace BizHawk.Client.EmuHawk
try
{
_ffmpeg = new Process();
#if WINDOWS
_ffmpeg.StartInfo.FileName = Path.Combine(PathManager.GetDllDirectory(), "ffmpeg.exe");
#else
ffmpeg.StartInfo.FileName = "ffmpeg"; // expecting native version to be in path
#endif
_ffmpeg.StartInfo.FileName = OSTailoredCode.CurrentOS == OSTailoredCode.DistinctOS.Windows
? Path.Combine(PathManager.GetDllDirectory(), "ffmpeg.exe")
: "ffmpeg";
_ffmpeg.StartInfo.Arguments = $"-y -f nut -i - {_token.Commandline} \"{_baseName}{(_segment == 0 ? string.Empty : $"_{_segment}")}{_ext}\"";
_ffmpeg.StartInfo.CreateNoWindow = true;

View File

@ -1052,7 +1052,7 @@ namespace BizHawk.Client.EmuHawk
if (!_inFullscreen)
{
SuspendLayout();
#if WINDOWS
// Work around an AMD driver bug in >= vista:
// It seems windows will activate opengl fullscreen mode when a GL control is occupying the exact space of a screen (0,0 and dimensions=screensize)
// AMD cards manifest a problem under these circumstances, flickering other monitors.
@ -1060,7 +1060,9 @@ namespace BizHawk.Client.EmuHawk
// (this could be determined with more work; other side affects of the fullscreen mode include: corrupted taskbar, no modal boxes on top of GL control, no screenshots)
// At any rate, we can solve this by adding a 1px black border around the GL control
// Please note: It is important to do this before resizing things, otherwise momentarily a GL control without WS_BORDER will be at the magic dimensions and cause the flakeout
if (Global.Config.DispFullscreenHacks && Global.Config.DispMethod == Config.EDispMethod.OpenGL)
if (OSTailoredCode.CurrentOS == OSTailoredCode.DistinctOS.Windows
&& Global.Config.DispFullscreenHacks
&& Global.Config.DispMethod == Config.EDispMethod.OpenGL)
{
//ATTENTION: this causes the statusbar to not work well, since the backcolor is now set to black instead of SystemColors.Control.
//It seems that some statusbar elements composite with the backcolor.
@ -1071,7 +1073,6 @@ namespace BizHawk.Client.EmuHawk
// FUTURE WORK:
// re-add this padding back into the display manager (so the image will get cut off a little but, but a few more resolutions will fully fit into the screen)
}
#endif
_windowedLocation = Location;
@ -1088,13 +1089,15 @@ namespace BizHawk.Client.EmuHawk
WindowState = FormWindowState.Normal;
#if WINDOWS
// do this even if DispFullscreenHacks arent enabled, to restore it in case it changed underneath us or something
Padding = new Padding(0);
// it's important that we set the form color back to this, because the statusbar icons blend onto the mainform, not onto the statusbar--
// so we need the statusbar and mainform backdrop color to match
BackColor = SystemColors.Control;
#endif
if (OSTailoredCode.CurrentOS == OSTailoredCode.DistinctOS.Windows)
{
// do this even if DispFullscreenHacks arent enabled, to restore it in case it changed underneath us or something
Padding = new Padding(0);
// it's important that we set the form color back to this, because the statusbar icons blend onto the mainform, not onto the statusbar--
// so we need the statusbar and mainform backdrop color to match
BackColor = SystemColors.Control;
}
_inFullscreen = false;

View File

@ -6,9 +6,6 @@ using sysdrawing2d=System.Drawing.Drawing2D;
using System.IO;
using System.Threading;
using System.Windows.Forms;
#if WINDOWS
using SlimDX;
#endif
using BizHawk.Client.Common;
using BizHawk.Bizware.BizwareGL;

View File

@ -5,9 +5,8 @@ using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
#if WINDOWS
using Microsoft.VisualBasic.ApplicationServices;
#endif
using BizHawk.Common;
using BizHawk.Client.Common;
@ -265,7 +264,6 @@ REDO_DISPMETHOD:
return GlobalWin.ExitCode;
} //SubMain
#if WINDOWS
//declared here instead of a more usual place to avoid dependencies on the more usual place
[DllImport("kernel32.dll", SetLastError = true)]
@ -292,7 +290,6 @@ REDO_DISPMETHOD:
RemoveMOTW(fi.FullName);
}
}
#endif
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
@ -336,7 +333,6 @@ REDO_DISPMETHOD:
}
}
#if WINDOWS
private class SingleInstanceController : WindowsFormsApplicationBase
{
private readonly string[] cmdArgs;
@ -365,6 +361,5 @@ REDO_DISPMETHOD:
GlobalWin.ExitCode = ((MainForm)MainForm).ProgramRunLoop();
}
}
#endif
}
}

View File

@ -1,5 +1,4 @@
#if WINDOWS
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
@ -164,4 +163,3 @@ namespace BizHawk.Client.EmuHawk
}
}
}
#endif

View File

@ -1,5 +1,4 @@
#if WINDOWS
using System;
using System;
using System.Collections.Generic;
using System.Linq;
@ -185,4 +184,3 @@ namespace BizHawk.Client.EmuHawk
}
}
}
#endif

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Windows.Forms;
using BizHawk.Client.Common;
using BizHawk.Common;
namespace BizHawk.Client.EmuHawk
{
@ -24,10 +25,14 @@ namespace BizHawk.Client.EmuHawk
cbEnableNormal.Checked = Global.Config.SoundEnabledNormal;
cbEnableRWFF.Checked = Global.Config.SoundEnabledRWFF;
cbMuteFrameAdvance.Checked = Global.Config.MuteFrameAdvance;
#if !WINDOWS
rbOutputMethodDirectSound.Enabled = false;
rbOutputMethodXAudio2.Enabled = false;
#endif
if (OSTailoredCode.CurrentOS != OSTailoredCode.DistinctOS.Windows)
{
// Disable DirectSound and XAudio2 on Mono
rbOutputMethodDirectSound.Enabled = false;
rbOutputMethodXAudio2.Enabled = false;
}
rbOutputMethodDirectSound.Checked = Global.Config.SoundOutputMethod == Config.ESoundOutputMethod.DirectSound;
rbOutputMethodXAudio2.Checked = Global.Config.SoundOutputMethod == Config.ESoundOutputMethod.XAudio2;
rbOutputMethodOpenAL.Checked = Global.Config.SoundOutputMethod == Config.ESoundOutputMethod.OpenAL;
@ -83,11 +88,13 @@ namespace BizHawk.Client.EmuHawk
private void PopulateDeviceList()
{
IEnumerable<string> deviceNames = Enumerable.Empty<string>();
#if WINDOWS
if (rbOutputMethodDirectSound.Checked) deviceNames = DirectSoundSoundOutput.GetDeviceNames();
if (rbOutputMethodXAudio2.Checked) deviceNames = XAudio2SoundOutput.GetDeviceNames();
#endif
if (OSTailoredCode.CurrentOS == OSTailoredCode.DistinctOS.Windows)
{
if (rbOutputMethodDirectSound.Checked) deviceNames = DirectSoundSoundOutput.GetDeviceNames();
if (rbOutputMethodXAudio2.Checked) deviceNames = XAudio2SoundOutput.GetDeviceNames();
}
if (rbOutputMethodOpenAL.Checked) deviceNames = OpenALSoundOutput.GetDeviceNames();
listBoxSoundDevices.Items.Clear();
listBoxSoundDevices.Items.Add("<default>");
listBoxSoundDevices.SelectedIndex = 0;

View File

@ -59,10 +59,8 @@ namespace BizHawk.Common
}
}
#if WINDOWS
[DllImport("kernel32.dll", EntryPoint = "DeleteFileW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern bool DeleteFileW([MarshalAs(UnmanagedType.LPWStr)]string lpFileName);
#endif
static void ThreadProc()
{
@ -94,12 +92,10 @@ namespace BizHawk.Common
{
try
{
// SHUT. UP. THE. EXCEPTIONS.
#if WINDOWS
DeleteFileW(fi.FullName);
#else
fi.Delete();
#endif
if (OSTailoredCode.CurrentOS == OSTailoredCode.DistinctOS.Windows)
DeleteFileW(fi.FullName); // SHUT. UP. THE. EXCEPTIONS.
else
fi.Delete();
}
catch
{