Skip to content

Commit

Permalink
Feature - AutoUpdate Improvements V. 5.1.5.2 🔥
Browse files Browse the repository at this point in the history
  • Loading branch information
tsgsOFFICIAL committed Nov 6, 2024
1 parent 474f3c4 commit c654ef3
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 23 deletions.
54 changes: 44 additions & 10 deletions CS2-AutoAccept/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,51 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.IO.MemoryMappedFiles;
using System.Threading;
using System.Windows;
using System.IO;
using System;

namespace CS2_AutoAccept
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
// Check if another instance of the app is already running
Mutex mutex = new Mutex(true, "CS2-AutoAccept by tsgsOFFICIAL", out bool createdNew);
string[] args = Environment.GetCommandLineArgs();
bool updated = false;

foreach (string arg in args)
{
// Application was updated
if (arg.ToLower().Equals("--updated"))
{
updated = true;
break;
}
}

// If another instance exists, trigger the event and exit
if (!createdNew && !updated)
{
// Create a MemoryMappedFile to notify the other instance
using (MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen("CS2_AutoAccept_MMF", 1024))
using (MemoryMappedViewStream view = mmf.CreateViewStream())
{
BinaryWriter writer = new BinaryWriter(view);
EventWaitHandle signal = new EventWaitHandle(false, EventResetMode.AutoReset, "CS2_AutoAccept_Event");
writer.Write("New instance started");
signal.Set(); // Signal the other instance that it should come to the front
}

// Shutdown the second instance
Current.Shutdown();
mutex.ReleaseMutex();
return;
}

// Run the WPF application
base.OnStartup(e);
}
}
}
}
2 changes: 1 addition & 1 deletion CS2-AutoAccept/CS2-AutoAccept.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<Description>A program to automatically find and press "ACCEPT" for you, when entering a competitive match in CS2.</Description>
<Copyright>©️ tsgsOFFICIAL - 2024</Copyright>
<FileVersion>5.1.5.1</FileVersion>
<FileVersion>5.1.5.2</FileVersion>
<AssemblyName>CS2-AutoAccept</AssemblyName>
</PropertyGroup>

Expand Down
71 changes: 62 additions & 9 deletions CS2-AutoAccept/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Threading.Tasks;
using System.Net.Http.Headers;
using System.Collections.Generic;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;
using Microsoft.Toolkit.Uwp.Notifications;

Expand All @@ -28,7 +29,6 @@ namespace CS2_AutoAccept
/// </summary>
public partial class MainWindow : Window
{
#region DLL Imports
#region DLL Imports For Mouse Manipulation
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
Expand All @@ -38,7 +38,6 @@ public partial class MainWindow : Window
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
#endregion
#endregion

private Updater? updater;
private Screen? _activeScreen;
Expand Down Expand Up @@ -78,7 +77,9 @@ public bool IsTrayIconVisible
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;

// Subscribe to the event when another instance tries to start
Task.Run(() => OnAnotherInstanceStarted());
IsTrayIconVisible = true;
DataContext = this;

Expand All @@ -91,6 +92,8 @@ public MainWindow()
_basePath = Path.Combine(Environment.ExpandEnvironmentVariables("%APPDATA%"), "CS2 AutoAccept");
_updatePath = Path.Combine(_basePath, "UPDATE");

Loaded += MainWindow_Loaded;

RestoreSizeIfSaved();

if (!debugMode)
Expand Down Expand Up @@ -189,7 +192,7 @@ public MainWindow()
}

// Start the updated program, in the new default path
Process.Start(Path.Combine(_basePath, "CS2-AutoAccept"));
Process.Start(Path.Combine(_basePath, "CS2-AutoAccept"), "--updated");
Environment.Exit(0);
}
else
Expand Down Expand Up @@ -231,6 +234,41 @@ private void UpdateTrayIconVisibility()
MyNotifyIcon.Visibility = IsTrayIconVisible ? Visibility.Visible : Visibility.Collapsed;
}
#region EventHandlers
// Handle the event when another instance tries to start
private void OnAnotherInstanceStarted()
{
using (MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen("CS2_AutoAccept_MMF", 1024))
using (MemoryMappedViewStream view = mmf.CreateViewStream())
{
BinaryReader reader = new BinaryReader(view);
EventWaitHandle signal = new EventWaitHandle(false, EventResetMode.AutoReset, "CS2_AutoAccept_Event");
Mutex mutex = new Mutex(false, "CS2-AutoAccept by tsgsOFFICIAL");

while (true)
{
signal.WaitOne();
mutex.WaitOne();
reader.BaseStream.Position = 0;
string message = reader.ReadString();

if (message == "New instance started")
{
Dispatcher.Invoke(() =>
{
// Bring the existing window to the front if it's minimized
if (WindowState == WindowState.Minimized)
{
Show();
WindowState = WindowState.Normal;
}
});
}

mutex.ReleaseMutex();
}
}
}

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
// Access command line arguments
Expand All @@ -245,10 +283,25 @@ private void MainWindow_Loaded(object sender, RoutedEventArgs e)
_gameRunExtraDelay = 15;
}

// Application was started minimized
if (arg.ToLower().Equals("--minimize"))
{
WindowState = WindowState.Minimized;
}

// Application was updated
if (arg.ToLower().Equals("--updated"))
{
// Try to delete the update folder
try
{
Directory.Delete(_updatePath, true);
}
catch (Exception)
{ }

ShowNotification("CS2 AutoAccept", "CS2 AutoAccept has been updated!");
}
}
}
protected override void OnStateChanged(EventArgs e)
Expand Down Expand Up @@ -583,13 +636,14 @@ private void RestoreSizeIfSaved()

mainWindow.SizeChanged += WindowSizeChangedEventHandler;
}

/// <summary>
/// Control the location of the application,
/// If not in the correct place Move it
/// Ensures the application is running from the correct folder. If not, it moves the necessary files and folders
/// to the correct base path and restarts the application from there.
/// </summary>
private void ControlLocation()
{
// Was the program ran from the correct folder?
// Was the program ran from the correct folder (_basePath or _updatePath)?
string runPath = AppContext.BaseDirectory;

if (runPath.LastIndexOf('\\') == runPath.Length - 1)
Expand Down Expand Up @@ -651,7 +705,6 @@ private void ControlLocation()
// Start the updated program, in the new default path
Process.Start(Path.Combine(_basePath, "CS2-AutoAccept"));
Environment.Exit(0);

}
}
/// <summary>
Expand Down Expand Up @@ -1191,7 +1244,7 @@ private Bitmap OptimiseImage(Bitmap bitmap)
{
Process.Start(Path.Combine(_basePath, "CS2-AutoAccept"));
ShowNotification("Error", ex.Message);
//Environment.Exit(0);
Environment.Exit(0);
return ("", 100);
}

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
11 changes: 8 additions & 3 deletions CS2-AutoAccept/updateInfo.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{
"version": "5.1.5.1",
"type": "Bugfix",
"changelog": "Fixed a bug where the application would not start with windows.",
"version": "5.1.5.2",
"type": "Feature",
"changelog": "Improved the automatic update flow.",
"historic_versions": [
{
"version": "5.1.5.1",
"type": "Bugfix",
"changelog": "Fixed a bug where the application would not start with windows."
},
{
"version": "5.1.5.0",
"type": "major",
Expand Down

0 comments on commit c654ef3

Please sign in to comment.