Skip to content

Commit

Permalink
Added a basic savestate system.
Browse files Browse the repository at this point in the history
Reduced default KeyPress duration to 30ms.
Updated README.md
  • Loading branch information
uberhalit committed Feb 11, 2017
1 parent 230350b commit cb94e1e
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 26 deletions.
2 changes: 1 addition & 1 deletion CitraTouchControl/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<value>False</value>
</setting>
<setting name="KeyPressDuration" serializeAs="String">
<value>50</value>
<value>30</value>
</setting>
<setting name="IsTapOnly" serializeAs="String">
<value>False</value>
Expand Down
3 changes: 3 additions & 0 deletions CitraTouchControl/CitraTouchControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Management" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
Expand Down
2 changes: 1 addition & 1 deletion CitraTouchControl/GlobalVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GlobalVars

internal static bool IsTouchEnabled = false;
internal static bool AreControlsHidden = false;
internal static int KeyPressDuration = 50;
internal static int KeyPressDuration = 30;
internal static bool IsTapOnly = false;
}
}
5 changes: 1 addition & 4 deletions CitraTouchControl/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CitraTouchControl"
mc:Ignorable="d"
Title="CitraTouchControl" SnapsToDevicePixels="True" Topmost="True" Height="1080" Width="860" AllowsTransparency="True" Background="#01FFFFFF" WindowStyle="None" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded">
<!--<Window.Background>
<SolidColorBrush Color="White" Opacity="0.2"/>
</Window.Background>-->
Title="CitraTouchControl" SnapsToDevicePixels="True" Topmost="True" Height="1080" Width="860" AllowsTransparency="True" Background="#01FFFFFF" WindowStyle="None" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded" Closing="Window_Closing">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25*"/>
Expand Down
55 changes: 55 additions & 0 deletions CitraTouchControl/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;
using System.IO;
using System.Linq;
using System.Windows;
using System.Threading;
using System.Management;
using System.Diagnostics;
using System.Windows.Media;
using System.Windows.Input;
Expand All @@ -11,13 +15,25 @@ namespace CitraTouchControl
{
public partial class MainWindow : Window
{
internal Mutex mutex;
internal bool isMenuWindow = false;
internal string userPath;
private static IntPtr citraHwnd = IntPtr.Zero;
private static IntPtr citraMainControlHwnd = IntPtr.Zero;
private List<IntPtr> pressedControls = new List<IntPtr>();

public MainWindow()
{
// check if we are the only instance of CitraTouchControl
bool isNew = false;
mutex = new Mutex(true, "CitraTouchControl", out isNew);
if (!isNew)
{
MessageBox.Show(this, "ERROR: There is already an instance of CitraTouchControl running.", "CitraTouchControl");
Environment.Exit(0);
}
GC.KeepAlive(mutex);

InitializeComponent();
}

Expand All @@ -32,6 +48,7 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
}
// get citra process handle
citraHwnd = processes[0].MainWindowHandle;

// check if citra is minimized
if (IsIconic(citraHwnd))
{
Expand All @@ -47,6 +64,18 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
Environment.Exit(0);
}

// get Citra savegame folder
userPath = Path.Combine(Path.GetDirectoryName(GetProcessPath(processes[0])), "user\\sdmc");
if (!Directory.Exists(userPath))
{
userPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Citra\\sdmc");
if (!Directory.Exists(userPath))
{
MessageBox.Show(this, "WARNING: Savegame folder could not be located!\nSavestate functionality will not work.", "CitraTouchControl");
userPath = null;
}
}

ResizeOverlay();

// load user settings into fast accessable global vars
Expand Down Expand Up @@ -76,6 +105,11 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
GlobalVars.KeyPressDuration = Properties.Settings.Default.KeyPressDuration;
}

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
mutex.ReleaseMutex();
}

/// <summary>
/// OnTouchDown handler.
/// </summary>
Expand Down Expand Up @@ -308,6 +342,27 @@ internal void ToggleControls(bool hide)
gMainGrid.Visibility = hide ? Visibility.Collapsed : Visibility.Visible;
}

/// <summary>
/// Retrieves the full path to the executable of a process.
/// </summary>
/// <param name="process">The process.</param>
/// <returns></returns>
internal static string GetProcessPath(Process process)
{
string MethodResult = "";
string Query = "SELECT ExecutablePath FROM Win32_Process WHERE ProcessId = " + process.Id;

using (ManagementObjectSearcher mos = new ManagementObjectSearcher(Query))
{
using (ManagementObjectCollection moc = mos.Get())
{
string ExecutablePath = (from mo in moc.Cast<ManagementObject>() select mo["ExecutablePath"]).First().ToString();
MethodResult = ExecutablePath;
}
}
return MethodResult;
}

#region IMPORTS

private const uint WM_KEYDOWN = 0x0100;
Expand Down
26 changes: 15 additions & 11 deletions CitraTouchControl/MenuWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CitraTouchControl"
mc:Ignorable="d"
Title="CitraTouchControl - Menu" Height="450" Width="400" Topmost="True" WindowStartupLocation="CenterOwner" WindowStyle="ToolWindow" Background="LightGray" Loaded="Window_Loaded" Closing="Window_Closing">
Title="CitraTouchControl - Menu" Height="500" Width="400" Topmost="True" WindowStartupLocation="CenterOwner" WindowStyle="ToolWindow" Background="LightGray" Loaded="Window_Loaded" Closing="Window_Closing">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
Expand All @@ -21,16 +21,20 @@
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Button x:Name="bResize" Grid.Row="0" Grid.Column="0" Content="Readjust Overlay" FontSize="24" FontWeight="Bold" Margin="10,4,10,4" BorderThickness="1" Focusable="False" Click="bResize_Click" Grid.ColumnSpan="3"/>
<Button x:Name="bTouch" Grid.Row="1" Grid.Column="0" Content="Enable Touch" FontSize="24" FontWeight="Bold" Margin="10,4,10,4" BorderThickness="1" Focusable="False" Click="bTouch_Click" Grid.ColumnSpan="3"/>
<Button x:Name="bControls" Grid.Row="2" Grid.Column="0" Content="Hide Controls" FontSize="24" FontWeight="Bold" Margin="10,4,10,4" BorderThickness="1" Focusable="False" Click="bControls_Click" Grid.ColumnSpan="3"/>
<Button x:Name="bTap" Grid.Row="3" Grid.Column="0" Content="Touch: Tap only" FontSize="24" FontWeight="Bold" Margin="10,4,10,4" BorderThickness="1" Focusable="False" Grid.ColumnSpan="3" Click="bTap_Click"/>
<Button x:Name="bDPlus" Grid.Row="4" Grid.Column="0" Content="+" FontSize="24" FontWeight="Bold" Margin="10,4,5,4" BorderThickness="1" Focusable="False" Click="bDPlus_Click"/>
<Button x:Name="bDuration" Grid.Row="4" Grid.Column="1" Content="KeyPress: 50ms" FontSize="24" FontWeight="Bold" Margin="5,4,5,4" BorderThickness="1" Focusable="False" />
<Button x:Name="bDMinus" Grid.Row="4" Grid.Column="2" Content="-" FontSize="24" FontWeight="Bold" Margin="5,4,10,4" BorderThickness="1" Focusable="False" Click="bDMinus_Click" />
<Button x:Name="bKeys" Grid.Row="5" Grid.Column="0" Content="Configure Keys" FontSize="24" FontWeight="Bold" Margin="10,4,10,4" BorderThickness="1" Focusable="False" Click="bKeys_Click" Grid.ColumnSpan="3"/>
<Button x:Name="bExit" Grid.Row="6" Grid.Column="0" Content="Exit CitraTouchControl" FontSize="24" FontWeight="Bold" Foreground="DarkRed" Margin="10,4,10,4" BorderThickness="1" Focusable="False" Click="bExit_Click" Grid.ColumnSpan="3"/>
<Button x:Name="bClose" Grid.Row="7" Grid.Column="0" Content="Close Window" FontSize="24" FontWeight="Bold" Margin="10,4,10,4" BorderThickness="1" Focusable="False" Click="bClose_Click" Grid.ColumnSpan="3"/>
<Button x:Name="bSave" Grid.Row="0" Grid.Column="0" Content="Save Savegames" FontSize="24" FontWeight="Bold" Margin="10,4,10,4" BorderThickness="1" Focusable="False" Grid.ColumnSpan="3" Click="bSave_Click"/>
<Button x:Name="bLoad" Grid.Row="1" Grid.Column="0" Content="Load Savegames" FontSize="24" FontWeight="Bold" Margin="10,4,10,4" BorderThickness="1" Focusable="False" Grid.ColumnSpan="3" Click="bLoad_Click"/>
<Button x:Name="bResize" Grid.Row="2" Grid.Column="0" Content="Readjust Overlay" FontSize="24" FontWeight="Bold" Margin="10,4,10,4" BorderThickness="1" Focusable="False" Click="bResize_Click" Grid.ColumnSpan="3"/>
<Button x:Name="bTouch" Grid.Row="3" Grid.Column="0" Content="Enable Touch" FontSize="24" FontWeight="Bold" Margin="10,4,10,4" BorderThickness="1" Focusable="False" Click="bTouch_Click" Grid.ColumnSpan="3"/>
<Button x:Name="bControls" Grid.Row="4" Grid.Column="0" Content="Hide Controls" FontSize="24" FontWeight="Bold" Margin="10,4,10,4" BorderThickness="1" Focusable="False" Click="bControls_Click" Grid.ColumnSpan="3"/>
<Button x:Name="bTap" Grid.Row="5" Grid.Column="0" Content="Touch: Tap only" FontSize="24" FontWeight="Bold" Margin="10,4,10,4" BorderThickness="1" Focusable="False" Grid.ColumnSpan="3" Click="bTap_Click"/>
<Button x:Name="bDPlus" Grid.Row="6" Grid.Column="0" Content="+" FontSize="24" FontWeight="Bold" Margin="10,4,5,4" BorderThickness="1" Focusable="False" Click="bDPlus_Click"/>
<Button x:Name="bDuration" Grid.Row="6" Grid.Column="1" Content="KeyPress: 30ms" FontSize="24" FontWeight="Bold" Margin="5,4,5,4" BorderThickness="1" Focusable="False" />
<Button x:Name="bDMinus" Grid.Row="6" Grid.Column="2" Content="-" FontSize="24" FontWeight="Bold" Margin="5,4,10,4" BorderThickness="1" Focusable="False" Click="bDMinus_Click" />
<Button x:Name="bKeys" Grid.Row="7" Grid.Column="0" Content="Configure Keys" FontSize="24" FontWeight="Bold" Margin="10,4,10,4" BorderThickness="1" Focusable="False" Click="bKeys_Click" Grid.ColumnSpan="3"/>
<Button x:Name="bExit" Grid.Row="8" Grid.Column="0" Content="Exit CitraTouchControl" FontSize="24" FontWeight="Bold" Foreground="DarkRed" Margin="10,4,10,4" BorderThickness="1" Focusable="False" Click="bExit_Click" Grid.ColumnSpan="3"/>
<Button x:Name="bClose" Grid.Row="9" Grid.Column="0" Content="Close Window" FontSize="24" FontWeight="Bold" Margin="10,4,10,4" BorderThickness="1" Focusable="False" Click="bClose_Click" Grid.ColumnSpan="3"/>
</Grid>
</Window>
66 changes: 66 additions & 0 deletions CitraTouchControl/MenuWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.IO;
using System.Windows;
using System.IO.Compression;

namespace CitraTouchControl
{
public partial class MenuWindow : Window
{
internal MainWindow mainWindow;
internal string savePath;

public MenuWindow(MainWindow mW)
{
Expand All @@ -25,6 +28,15 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
if (GlobalVars.IsTapOnly)
bTap.Content = "Touch: Tap & Hold";
bDuration.Content = "KeyPress: " + GlobalVars.KeyPressDuration + "ms";
if (mainWindow.userPath == null)
{
bSave.IsEnabled = false;
bLoad.IsEnabled = false;
}
else
{
savePath = Path.Combine(Path.GetDirectoryName(mainWindow.userPath), "save01.zip");
}
}

/// <summary>
Expand All @@ -37,6 +49,60 @@ private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs
Properties.Settings.Default.Save();
}

/// <summary>
/// Saves the current savestate.
/// </summary>
private void bSave_Click(object sender, RoutedEventArgs e)
{
if (File.Exists(savePath))
{
var result = MessageBox.Show(this, "WARNING: Do you really wish to overwrite the last savegames?",
"CitraTouchControl", MessageBoxButton.YesNo);
if (result == MessageBoxResult.No)
return;
File.Delete(savePath);
}
try
{
ZipFile.CreateFromDirectory(mainWindow.userPath, savePath, CompressionLevel.Fastest, false);
}
catch (Exception ex)
{
MessageBox.Show(this, "EXCEPTION: Error while trying to save savegames.\n" + ex.Message, "CitraTouchControl", MessageBoxButton.OK);
}
}

/// <summary>
/// Loads the current savestate.
/// </summary>
private void bLoad_Click(object sender, RoutedEventArgs e)
{
if (!File.Exists(savePath))
return;
var result = MessageBox.Show(this, "WARNING: Do you really wish to overwrite the current savegames?",
"CitraTouchControl", MessageBoxButton.YesNo);
if (result == MessageBoxResult.No)
return;
try
{
// delete all currently existing files which will be extracted from zip as ZipFile.ExtractToDirectory() can not overwrite any files...
using (ZipArchive zipArchive = ZipFile.OpenRead(savePath))
{
foreach (ZipArchiveEntry entry in zipArchive.Entries)
{
var path = Path.Combine(mainWindow.userPath, entry.FullName);
if (File.Exists(path))
File.Delete(path);
}
}
ZipFile.ExtractToDirectory(savePath, mainWindow.userPath);
}
catch (Exception ex)
{
MessageBox.Show(this, "EXCEPTION: Error while trying to load savegames.\n" + ex.Message, "CitraTouchControl", MessageBoxButton.OK);
}
}

/// <summary>
/// Resizes the overlay to match citra again.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions CitraTouchControl/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CitraRemoteTouch")]
[assembly: AssemblyCopyright("Copyright © Uberhalit 2017")]
[assembly: AssemblyCopyright("Copyright © uberhalit 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand Down Expand Up @@ -51,5 +51,5 @@
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
2 changes: 1 addition & 1 deletion CitraTouchControl/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion CitraTouchControl/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="KeyPressDuration" Type="System.Int32" Scope="User">
<Value Profile="(Default)">50</Value>
<Value Profile="(Default)">30</Value>
</Setting>
<Setting Name="IsTapOnly" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
Expand Down
Loading

0 comments on commit cb94e1e

Please sign in to comment.