Skip to content

Commit

Permalink
No longer possible to open two instances of app. Second instance will…
Browse files Browse the repository at this point in the history
… close and original instance will be activated.
  • Loading branch information
Razzmatazzz committed Mar 12, 2021
1 parent 2d10229 commit 7574990
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 17 deletions.
92 changes: 78 additions & 14 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using System.Collections.Specialized;
using System.Collections;
using RazzTools;
using System.Windows.Interop;
using System.Runtime.InteropServices;

namespace ValheimSaveShield
{
Expand All @@ -24,8 +26,8 @@ namespace ValheimSaveShield
/// </summary>
public partial class MainWindow : Window
{
private static string defaultBackupFolder = $@"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}\AppData\LocalLow\IronGate\Valheim\backups";
private static string defaultSaveFolder = $@"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}\AppData\LocalLow\IronGate\Valheim";
private static string DefaultBackupFolder { get { return $@"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}\AppData\LocalLow\IronGate\Valheim\backups"; } }
private static string DefaultSaveFolder { get { return $@"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}\AppData\LocalLow\IronGate\Valheim"; } }
private List<SaveBackup> listBackups;
private Boolean suppressLog;
private Color defaultTextColor;
Expand All @@ -38,6 +40,9 @@ public partial class MainWindow : Window

private Thread ftpDirectorySync = null;

private readonly Mutex _mutex;
private const string mutexName = "MUTEX_VALHEIMSAVESHIELD";

private bool IsBackupCurrent {
get {
foreach (var saveDirPath in Properties.Settings.Default.SaveFolders)
Expand Down Expand Up @@ -95,15 +100,36 @@ private StringCollection SavePaths

~MainWindow()
{
if (notifyIcon != null)
{
notifyIcon.Dispose();
}
if (ftpDirectorySync != null)
{
ftpDirectorySync.Abort();
}
if (_mutex != null)
{
_mutex.Dispose();
}
}

public MainWindow()
{
InitializeComponent();
bool firstInstance;
_mutex = new Mutex(true, mutexName, out firstInstance);
if (!firstInstance)
{
NativeMethods.PostMessage(
(IntPtr)NativeMethods.HWND_BROADCAST,
NativeMethods.WM_SHOWME,
IntPtr.Zero,
IntPtr.Zero
);
Close();
return;
}
suppressLog = false;
if (Properties.Settings.Default.CreateLogFile)
{
Expand All @@ -115,8 +141,10 @@ public MainWindow()
if (Properties.Settings.Default.UpgradeRequired)
{
Properties.Settings.Default.Upgrade();
logMessage($"Previous backup folder: {Properties.Settings.Default.GetPreviousVersion("BackupFolder")}");
Properties.Settings.Default.UpgradeRequired = false;
Properties.Settings.Default.Save();
logMessage($"Current backup folder: {Properties.Settings.Default.BackupFolder}");
}
Width = Properties.Settings.Default.MainWindowWidth;
Height = Properties.Settings.Default.MainWindowHeight;
Expand Down Expand Up @@ -150,20 +178,20 @@ public MainWindow()
Properties.Settings.Default.WorldFileExtensions = new StringCollection();
Properties.Settings.Default.Save();
}
if (Properties.Settings.Default.BackupFolder.Length == 0)
saveWatchers = new List<SaveWatcher>();
if (Properties.Settings.Default.BackupFolder == "")
{
logMessage("Backup folder not set; reverting to default.");
Properties.Settings.Default.BackupFolder = defaultBackupFolder;
Properties.Settings.Default.BackupFolder = DefaultBackupFolder;
Properties.Settings.Default.Save();
}
else if (!Directory.Exists(Properties.Settings.Default.BackupFolder) && !Properties.Settings.Default.BackupFolder.Equals(defaultBackupFolder))
else if (!Directory.Exists(Properties.Settings.Default.BackupFolder) && !Properties.Settings.Default.BackupFolder.Equals(DefaultBackupFolder))
{
logMessage($"Backup folder {Properties.Settings.Default.BackupFolder}) not found; reverting to default.");
Properties.Settings.Default.BackupFolder = defaultBackupFolder;
Properties.Settings.Default.BackupFolder = DefaultBackupFolder;
Properties.Settings.Default.Save();
}
saveWatchers = new List<SaveWatcher>();
if (Properties.Settings.Default.SaveFolders != null && Properties.Settings.Default.SaveFolders.Count > 0)
if (Properties.Settings.Default.SaveFolders.Count > 0)
{
foreach (var path in Properties.Settings.Default.SaveFolders)
{
Expand All @@ -188,12 +216,12 @@ public MainWindow()
else
{
logMessage("Reverting to default save folder.");
lstSaveFolders.Items.Add(defaultSaveFolder);
AddToSaveWatchers(defaultSaveFolder);
lstSaveFolders.Items.Add(DefaultSaveFolder);
AddToSaveWatchers(DefaultSaveFolder);

lstSaveFolders.Items.Refresh();
Properties.Settings.Default.SaveFolders.Add(defaultSaveFolder);
Properties.Settings.Default.FtpSaveDest = defaultSaveFolder;
Properties.Settings.Default.SaveFolders.Add(DefaultSaveFolder);
Properties.Settings.Default.FtpSaveDest = DefaultSaveFolder;
Properties.Settings.Default.Save();
}
// start the directory syncing if user has the correct settings for it
Expand All @@ -212,6 +240,35 @@ public MainWindow()
notifyIcon.Click += NotifyIcon_Click;
storedWindowState = WindowState.Normal;
}
//This event is raised to support interoperation with Win32
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
source.AddHook(WndProc);
}
//Receive and act on messages
IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
// Handle messages...
if (msg == NativeMethods.WM_SHOWME)
{
Show();
Activate();
WindowState = storedWindowState;
}
return IntPtr.Zero;
}
//dll import magic
internal class NativeMethods
{
public const int HWND_BROADCAST = 0xffff;
public static readonly int WM_SHOWME = RegisterWindowMessage("WM_SHOWME");
[DllImport("user32")]
public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
[DllImport("user32")]
public static extern int RegisterWindowMessage(string message);
}

private void SaveWatcher_LogMessage(object sender, SaveWatcherLogMessageEventArgs e)
{
Expand Down Expand Up @@ -244,6 +301,7 @@ private void AddToSaveWatchers(string path)
private void NotifyIcon_Click(object sender, EventArgs e)
{
Show();
Activate();
WindowState = storedWindowState;
}

Expand Down Expand Up @@ -960,13 +1018,19 @@ private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs

private void Window_Closed(object sender, EventArgs e)
{
notifyIcon.Dispose();
notifyIcon = null;
if (notifyIcon != null)
{
notifyIcon.Dispose();
}
if (ftpDirectorySync != null)
{
ftpDirectorySync.Abort();
ftpDirectorySync = null;
}
if (_mutex != null)
{
_mutex.Dispose();
}
}

private void BtnBackupFolder_Click(object sender, RoutedEventArgs e)
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.4.8.0")]
[assembly: AssemblyFileVersion("0.4.8.0")]
[assembly: AssemblyVersion("0.4.9.0")]
[assembly: AssemblyFileVersion("0.4.9.0")]
3 changes: 2 additions & 1 deletion ValheimSaveShield.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@
<PropertyGroup>
<PostBuildEvent>copy "$(TargetDir)ModernWpf.dll" "$(TargetDir)app.publish\"
copy "$(TargetDir)ValheimSaveShield.exe.config" "$(TargetDir)app.publish\"
copy "$(TargetDir)WinSCPnet.dll" "$(TargetDir)app.publish\"</PostBuildEvent>
copy "$(TargetDir)WinSCPnet.dll" "$(TargetDir)app.publish\"
if $(ConfigurationName) == Release "C:\Program Files\7-Zip\7z.exe" a %25userprofile%25\Desktop\vss.zip "$(TargetDir)app.publish\*"</PostBuildEvent>
</PropertyGroup>
<Import Project="packages\WinSCP.5.17.10\build\WinSCP.targets" Condition="Exists('packages\WinSCP.5.17.10\build\WinSCP.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down

0 comments on commit 7574990

Please sign in to comment.