Improved startup performance

This commit is contained in:
Souryo 2017-08-13 17:17:14 -04:00
parent 222f35f33c
commit bdf6cea4a7
11 changed files with 85 additions and 70 deletions

View file

@ -323,6 +323,7 @@ enum class Language
Spanish = 5, Spanish = 5,
Ukrainian = 6, Ukrainian = 6,
Portuguese = 7, Portuguese = 7,
Catalan = 8
}; };
enum class StereoFilter enum class StereoFilter

View file

@ -73,18 +73,23 @@ namespace Mesen.GUI.Config
LoadConfig(); LoadConfig();
} }
private static object _initLock = new object();
private static void LoadConfig() private static void LoadConfig()
{ {
if(_config == null) { if(_config == null) {
if(File.Exists(ConfigFile)) { lock(_initLock) {
_config = Configuration.Deserialize(ConfigFile); if(_config == null) {
_dirtyConfig = Configuration.Deserialize(ConfigFile); if(File.Exists(ConfigFile)) {
} else { _config = Configuration.Deserialize(ConfigFile);
//Create new config file and save it to disk _dirtyConfig = Configuration.Deserialize(ConfigFile);
_config = new Configuration(); } else {
_dirtyConfig = new Configuration(); //Create new config file and save it to disk
_config.Save(); _config = new Configuration();
_dirtyConfig = new Configuration();
_config.Save();
}
}
} }
} }
} }
@ -229,12 +234,17 @@ namespace Mesen.GUI.Config
} }
} }
private static DateTime _lastSaveTime = DateTime.MinValue;
public static void ApplyChanges() public static void ApplyChanges()
{ {
_config.NeedToSave = false; _config.NeedToSave = false;
_config = _dirtyConfig.Clone(); _config = _dirtyConfig.Clone();
_config.NeedToSave = true; _config.NeedToSave = true;
_config.Save();
if((DateTime.Now - _lastSaveTime).Seconds > 1) {
ConfigManager.SaveConfig();
_lastSaveTime = DateTime.Now;
}
} }
public static void RejectChanges() public static void RejectChanges()

View file

@ -286,9 +286,11 @@ namespace Mesen.GUI.Controls
private void ctrlNsfPlayer_VisibleChanged(object sender, EventArgs e) private void ctrlNsfPlayer_VisibleChanged(object sender, EventArgs e)
{ {
this.Repeat = this.Repeat; if(!this.DesignMode && this.Visible) {
this.Shuffle = this.Shuffle; this.Repeat = this.Repeat;
btnPause.Focus(); this.Shuffle = this.Shuffle;
btnPause.Focus();
}
} }
private void btnNext_MouseDown(object sender, MouseEventArgs e) private void btnNext_MouseDown(object sender, MouseEventArgs e)

View file

@ -55,9 +55,6 @@ namespace Mesen.GUI.Controls
lblSaveDate.Font = new Font(_fonts.Families[0], 10); lblSaveDate.Font = new Font(_fonts.Families[0], 10);
picPrevGame.Image.RotateFlip(RotateFlipType.RotateNoneFlipX); picPrevGame.Image.RotateFlip(RotateFlipType.RotateNoneFlipX);
if(!ConfigManager.Config.PreferenceInfo.DisableGameSelectionScreen) {
Initialize();
}
} }
} }
@ -66,7 +63,7 @@ namespace Mesen.GUI.Controls
get { return base.Visible; } get { return base.Visible; }
set set
{ {
if((_initialized && _recentGames.Count == 0) || ConfigManager.Config.PreferenceInfo.DisableGameSelectionScreen) { if(value && ((_initialized && _recentGames.Count == 0) || ConfigManager.Config.PreferenceInfo.DisableGameSelectionScreen)) {
value = false; value = false;
} }

View file

@ -47,7 +47,9 @@ namespace Mesen.GUI.Forms
} }
ConfigManager.Config.InputInfo.DefaultMapping = mappingType; ConfigManager.Config.InputInfo.DefaultMapping = mappingType;
ConfigManager.Config.InitializeDefaults();
ConfigManager.ApplyChanges(); ConfigManager.ApplyChanges();
ConfigManager.SaveConfig();
if(chkCreateShortcut.Checked) { if(chkCreateShortcut.Checked) {
this.CreateShortcut(); this.CreateShortcut();

View file

@ -305,7 +305,6 @@ namespace Mesen.GUI.Forms
this.menuStrip.Size = new System.Drawing.Size(430, 24); this.menuStrip.Size = new System.Drawing.Size(430, 24);
this.menuStrip.TabIndex = 0; this.menuStrip.TabIndex = 0;
this.menuStrip.Text = "menuStrip1"; this.menuStrip.Text = "menuStrip1";
this.menuStrip.VisibleChanged += new System.EventHandler(this.menuStrip_VisibleChanged);
// //
// mnuFile // mnuFile
// //

View file

@ -121,6 +121,7 @@ namespace Mesen.GUI.Forms
ResourceHelper.UpdateEmuLanguage(); ResourceHelper.UpdateEmuLanguage();
ResourceHelper.ApplyResources(this); ResourceHelper.ApplyResources(this);
UpdateMenus(); UpdateMenus();
UpdateRecentFiles();
InitializeFdsDiskMenu(); InitializeFdsDiskMenu();
InitializeNsfMode(true); InitializeNsfMode(true);
ctrlRecentGames.UpdateGameInfo(); ctrlRecentGames.UpdateGameInfo();

View file

@ -58,14 +58,6 @@ namespace Mesen.GUI.Forms
{ {
InitializeComponent(); InitializeComponent();
if(ConfigManager.Config.WindowLocation.HasValue) {
this.StartPosition = FormStartPosition.Manual;
this.Location = ConfigManager.Config.WindowLocation.Value;
} else {
//First launch
this.StartPosition = FormStartPosition.CenterScreen;
}
Version currentVersion = new Version(InteropEmu.GetMesenVersion()); Version currentVersion = new Version(InteropEmu.GetMesenVersion());
lblVersion.Text = currentVersion.ToString(); lblVersion.Text = currentVersion.ToString();
@ -129,6 +121,9 @@ namespace Mesen.GUI.Forms
protected override void OnLoad(EventArgs e) protected override void OnLoad(EventArgs e)
{ {
ResourceHelper.LoadResources(ConfigManager.Config.PreferenceInfo.DisplayLanguage);
ResourceHelper.UpdateEmuLanguage();
base.OnLoad(e); base.OnLoad(e);
#if HIDETESTMENU #if HIDETESTMENU
@ -157,6 +152,14 @@ namespace Mesen.GUI.Forms
UpdateRecentFiles(); UpdateRecentFiles();
UpdateViewerSize(); UpdateViewerSize();
if(ConfigManager.Config.WindowLocation.HasValue) {
this.StartPosition = FormStartPosition.Manual;
this.Location = ConfigManager.Config.WindowLocation.Value;
} else {
//First launch
this.StartPosition = FormStartPosition.CenterScreen;
}
if(ConfigManager.Config.PreferenceInfo.CloudSaveIntegration) { if(ConfigManager.Config.PreferenceInfo.CloudSaveIntegration) {
Task.Run(() => CloudSyncHelper.Sync()); Task.Run(() => CloudSyncHelper.Sync());
@ -198,7 +201,10 @@ namespace Mesen.GUI.Forms
this.Size = ConfigManager.Config.WindowSize.Value; this.Size = ConfigManager.Config.WindowSize.Value;
} }
this.LoadGameFromCommandLine(_commandLineArgs); this.LoadGameFromCommandLine(_commandLineArgs);
this.menuStrip.VisibleChanged += new System.EventHandler(this.menuStrip_VisibleChanged);
this.UpdateRendererLocation();
} }
protected override void OnClosing(CancelEventArgs e) protected override void OnClosing(CancelEventArgs e)
@ -772,6 +778,11 @@ namespace Mesen.GUI.Forms
} }
private void menuStrip_VisibleChanged(object sender, EventArgs e) private void menuStrip_VisibleChanged(object sender, EventArgs e)
{
this.UpdateRendererLocation();
}
private void UpdateRendererLocation()
{ {
if(this.HideMenuStrip) { if(this.HideMenuStrip) {
IntPtr handle = this.Handle; IntPtr handle = this.Handle;

View file

@ -9,6 +9,7 @@ using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using System.Xml.Serialization;
using Mesen.GUI.Config; using Mesen.GUI.Config;
using Mesen.GUI.Forms; using Mesen.GUI.Forms;
@ -61,6 +62,12 @@ namespace Mesen.GUI
private static void Main(string[] args) private static void Main(string[] args)
{ {
try { try {
Task.Run(() => {
//Cache deserializers in another thread
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Configuration));
xmlSerializer = new XmlSerializer(typeof(DebugWorkspace));
});
if(Type.GetType("Mono.Runtime") != null) { if(Type.GetType("Mono.Runtime") != null) {
Program.IsMono = true; Program.IsMono = true;
} }
@ -90,7 +97,6 @@ namespace Mesen.GUI
Directory.CreateDirectory(ConfigManager.HomeFolder); Directory.CreateDirectory(ConfigManager.HomeFolder);
Directory.SetCurrentDirectory(ConfigManager.HomeFolder); Directory.SetCurrentDirectory(ConfigManager.HomeFolder);
try { try {
ResourceHelper.LoadResources(ConfigManager.Config.PreferenceInfo.DisplayLanguage);
if(!ResourceManager.ExtractResources()) { if(!ResourceManager.ExtractResources()) {
return; return;
} }
@ -123,21 +129,17 @@ namespace Mesen.GUI
return; return;
} }
ResourceHelper.UpdateEmuLanguage();
using(SingleInstance singleInstance = new SingleInstance()) { using(SingleInstance singleInstance = new SingleInstance()) {
if(singleInstance.FirstInstance || !Config.ConfigManager.Config.PreferenceInfo.SingleInstance) { if(singleInstance.FirstInstance || !ConfigManager.Config.PreferenceInfo.SingleInstance) {
frmMain frmMain = new frmMain(args); frmMain frmMain = new frmMain(args);
if(Config.ConfigManager.Config.PreferenceInfo.SingleInstance) { singleInstance.ListenForArgumentsFromSuccessiveInstances();
singleInstance.ListenForArgumentsFromSuccessiveInstances(); singleInstance.ArgumentsReceived += (object sender, ArgumentsReceivedEventArgs e) => {
singleInstance.ArgumentsReceived += (object sender, ArgumentsReceivedEventArgs e) => { frmMain.BeginInvoke((MethodInvoker)(() => {
frmMain.BeginInvoke((MethodInvoker)(() => { frmMain.ProcessCommandLineArguments(e.Args, false);
frmMain.ProcessCommandLineArguments(e.Args, false); frmMain.LoadGameFromCommandLine(e.Args);
frmMain.LoadGameFromCommandLine(e.Args); }));
})); };
};
}
Application.Run(frmMain); Application.Run(frmMain);
} else { } else {

View file

@ -12,32 +12,10 @@ namespace Mesen.GUI
{ {
class ResourceManager class ResourceManager
{ {
private static void ExtractResource(string resourceName, string filename)
{
if(File.Exists(filename)) {
try {
File.Delete(filename);
} catch { }
}
Assembly a = Assembly.GetExecutingAssembly();
using(Stream s = a.GetManifestResourceStream(resourceName)) {
byte[] buffer = new byte[s.Length];
s.Read(buffer, 0, (int)s.Length);
try {
File.WriteAllBytes(Path.Combine(ConfigManager.HomeFolder, filename), buffer);
} catch { }
}
}
private static void ExtractFile(ZipArchiveEntry entry, string outputFilename) private static void ExtractFile(ZipArchiveEntry entry, string outputFilename)
{ {
if(File.Exists(outputFilename)) {
try {
File.Delete(outputFilename);
} catch { }
}
try { try {
entry.ExtractToFile(outputFilename); entry.ExtractToFile(outputFilename, true);
} catch { } } catch { }
} }
@ -55,10 +33,6 @@ namespace Mesen.GUI
private static void CleanupOldFiles() private static void CleanupOldFiles()
{ {
try { try {
if(Directory.Exists(Path.Combine(ConfigManager.HomeFolder, "Resources"))) {
Directory.Delete(Path.Combine(ConfigManager.HomeFolder, "Resources"), true);
}
if(Directory.Exists(Path.Combine(ConfigManager.HomeFolder, "WinMesen"))) { if(Directory.Exists(Path.Combine(ConfigManager.HomeFolder, "WinMesen"))) {
Directory.Delete(Path.Combine(ConfigManager.HomeFolder, "WinMesen"), true); Directory.Delete(Path.Combine(ConfigManager.HomeFolder, "WinMesen"), true);
} }

View file

@ -238,9 +238,6 @@ WindowsKeyManager::WindowsKeyManager(HWND hWnd)
} }
} }
_xInput.reset(new XInputManager());
_directInput.reset(new DirectInputManager(hWnd));
StartUpdateDeviceThread(); StartUpdateDeviceThread();
} }
@ -253,6 +250,9 @@ WindowsKeyManager::~WindowsKeyManager()
void WindowsKeyManager::StartUpdateDeviceThread() void WindowsKeyManager::StartUpdateDeviceThread()
{ {
_updateDeviceThread = std::thread([=]() { _updateDeviceThread = std::thread([=]() {
_xInput.reset(new XInputManager());
_directInput.reset(new DirectInputManager(_hWnd));
while(!_stopUpdateDeviceThread) { while(!_stopUpdateDeviceThread) {
//Check for newly plugged in controllers every 5 secs (this takes ~60-70ms when no new controllers are found) //Check for newly plugged in controllers every 5 secs (this takes ~60-70ms when no new controllers are found)
if(_xInput->NeedToUpdate()) { if(_xInput->NeedToUpdate()) {
@ -272,6 +272,10 @@ void WindowsKeyManager::StartUpdateDeviceThread()
void WindowsKeyManager::RefreshState() void WindowsKeyManager::RefreshState()
{ {
if(!_xInput || !_directInput) {
return;
}
_xInput->RefreshState(); _xInput->RefreshState();
_directInput->RefreshState(); _directInput->RefreshState();
} }
@ -279,6 +283,10 @@ void WindowsKeyManager::RefreshState()
bool WindowsKeyManager::IsKeyPressed(uint32_t key) bool WindowsKeyManager::IsKeyPressed(uint32_t key)
{ {
if(key >= 0x10000) { if(key >= 0x10000) {
if(!_xInput || !_directInput) {
return false;
}
if(key >= 0x11000) { if(key >= 0x11000) {
//Directinput key //Directinput key
uint8_t gamepadPort = (key - 0x11000) / 0x100; uint8_t gamepadPort = (key - 0x11000) / 0x100;
@ -309,6 +317,10 @@ bool WindowsKeyManager::IsMouseButtonPressed(MouseButton button)
uint32_t WindowsKeyManager::GetPressedKey() uint32_t WindowsKeyManager::GetPressedKey()
{ {
if(!_xInput || !_directInput) {
return 0;
}
_xInput->RefreshState(); _xInput->RefreshState();
for(int i = 0; i < XUSER_MAX_COUNT; i++) { for(int i = 0; i < XUSER_MAX_COUNT; i++) {
for(int j = 1; j <= 26; j++) { for(int j = 1; j <= 26; j++) {
@ -357,6 +369,10 @@ uint32_t WindowsKeyManager::GetKeyCode(string keyName)
void WindowsKeyManager::UpdateDevices() void WindowsKeyManager::UpdateDevices()
{ {
if(!_xInput || !_directInput) {
return;
}
Console::Pause(); Console::Pause();
_xInput->UpdateDeviceList(); _xInput->UpdateDeviceList();
_directInput->UpdateDeviceList(); _directInput->UpdateDeviceList();