Skip to content

Commit

Permalink
Storing user preferences to XML and loading them on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
SuiMachine committed Aug 14, 2021
1 parent 6945e53 commit 9094544
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 11 deletions.
56 changes: 45 additions & 11 deletions Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace QuantumOfSolace
{
public partial class Form1 : Form
{
UserPreferences userPreferences;
// Base address value for pointers.
int baseAddress = 0x0;

Expand Down Expand Up @@ -39,28 +40,59 @@ public partial class Form1 : Form
bool disableBalancing;

string labelUrl = "www.pcgamingwiki.com";
string DonateURL = "https://www.twitchalerts.com/donate/suicidemachine";

//Keyboard hook
KeyboardHook.LowLevelKeyboardHook keyboardHook;
Keys toggleKey;

bool initialized = false;


/*------------------
-- INITIALIZATION --
------------------*/
public Form1()
{
userPreferences = UserPreferences.Load();
InitializeComponent();
processName = "JB_LiveEngine_s";
toggleKey = Keys.F5;
LoadUserPreferences();
}

private void LoadUserPreferences()
{
fov = userPreferences.DesiredFOV;
autoModeFOV = userPreferences.DesiredFOVHackEnabled;

fps = userPreferences.DesiredFPS;
autoModeFPS = userPreferences.DesiredFPSHackEnabled;

if (!userPreferences.FullScreenMode)
{
autoModeFullscreen = true;
fullscreen = 0;
}

disableBalancing = userPreferences.NoBalancingMiniGame;
toggleKey = userPreferences.DesiredFPSToggleKey;
}

private void Form1_Shown(object sender, EventArgs e)
{
keyboardHook = new KeyboardHook.LowLevelKeyboardHook();
RegisterHotkeys();
keyboardHook.KeyPressed += KeyboardHook_KeyPressed;

//Set toggles, textboxes
C_AutoModeFOV.Checked = autoModeFOV;
T_InputFOV.Text = fov.ToString();

C_AutoModeFPS.Checked = autoModeFPS;
T_InputFPS.Text = fps.ToString();

C_balance.Checked = disableBalancing;
C_fullscreen.Checked = fullscreen != 0;
initialized = true;
}

private void RegisterHotkeys()
Expand All @@ -73,10 +105,10 @@ private void RegisterHotkeys()

private void KeyboardHook_KeyPressed(object sender, KeyEventArgs e)
{
if(C_AutoModeFOV.InvokeRequired)
if (C_AutoModeFOV.InvokeRequired)
{
KeyboardHook_KeyPressedDelagate d = new KeyboardHook_KeyPressedDelagate(ToggleFPSLock);
this.Invoke(d, e );
this.Invoke(d, e);
}
else
{
Expand Down Expand Up @@ -118,7 +150,6 @@ private void Timer_Tick(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(100);
//baseAddress = myProcess[0].MainModule.BaseAddress.ToInt32();
//var lenght = myProcess[0].MainModule.ModuleMemorySize;
}
foundProcess = true;

Expand Down Expand Up @@ -242,18 +273,21 @@ void freezeBalance()
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
keyboardHook.KeyPressed -= KeyboardHook_KeyPressed;
userPreferences.Save();
}

private void C_AutoModeFOV_CheckedChanged(object sender, EventArgs e)
{
autoModeFOV = C_AutoModeFOV.Checked;
userPreferences.DesiredFOVHackEnabled = autoModeFOV;
}

private void B_set_Click(object sender, EventArgs e)
{
if (float.TryParse(T_InputFOV.Text, out float res))
{
fov = res;
userPreferences.DesiredFOV = fov;
}
}

Expand All @@ -262,6 +296,7 @@ private void B_setFPS_Click(object sender, EventArgs e)
if (int.TryParse(T_InputFPS.Text, out int resFPS))
{
fps = resFPS;
userPreferences.DesiredFPS = fps;
}
}

Expand All @@ -273,6 +308,7 @@ private void linkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs
private void C_AutoModeFPS_CheckedChanged(object sender, EventArgs e)
{
autoModeFPS = C_AutoModeFPS.Checked;
userPreferences.DesiredFPSHackEnabled = autoModeFPS;
}

private void C_windowed_CheckedChanged(object sender, EventArgs e)
Expand All @@ -285,20 +321,18 @@ private void C_windowed_CheckedChanged(object sender, EventArgs e)
else
{
autoModeFullscreen = true;
MessageBox.Show("The game will not run in proper windowed mode! The feature is buggy.\n Basically the game, will always remain on top, no matter what.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
if (initialized)
MessageBox.Show("The game will not run in proper windowed mode! The feature is buggy.\n Basically the game, will always remain on top, no matter what.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
fullscreen = 0;
}
userPreferences.FullScreenMode = fullscreen != 0;
}


private void C_balance_CheckedChanged(object sender, EventArgs e)
{
disableBalancing = C_balance.Checked;
}

private void pictureBox1_Click(object sender, EventArgs e)
{
Process.Start(DonateURL);
userPreferences.NoBalancingMiniGame = disableBalancing;
}

private void TB_ToggleKey_KeyDown(object sender, KeyEventArgs e)
Expand Down
2 changes: 2 additions & 0 deletions QuantumOfSolaceTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Trainer.cs" />
<Compile Include="UserPreferences.cs" />
<Compile Include="XMLSerialization.cs" />
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
</EmbeddedResource>
Expand Down
Binary file modified Release/QuantumOfSolace.exe
Binary file not shown.
Binary file modified Release/QuantumOfSolace.pdb
Binary file not shown.
54 changes: 54 additions & 0 deletions UserPreferences.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuantumOfSolace
{
public class UserPreferences
{
private const string CONFIG_FILE = "QuantumOfSolaceTool.xml";

public bool FullScreenMode { get; set; }
public bool NoBalancingMiniGame { get; set; }

public float DesiredFOV { get; set; }
public bool DesiredFOVHackEnabled { get; set; }

public int DesiredFPS { get; set; }
public bool DesiredFPSHackEnabled { get; set; }

public Keys DesiredFPSToggleKey { get; set; }


public UserPreferences()
{
FullScreenMode = false;
NoBalancingMiniGame = false;
DesiredFOV = 80;
DesiredFOVHackEnabled = false;
DesiredFPS = 60;
DesiredFPSHackEnabled = false;
DesiredFPSToggleKey = Keys.F5;
}

public static UserPreferences Load()
{
if (File.Exists(CONFIG_FILE))
{
var readFile = XMLSerialization.ReadFromXMLFile<UserPreferences>(CONFIG_FILE);
return readFile;
}
else
return new UserPreferences();
}

public void Save()
{
XMLSerialization.SaveObjectToXML<UserPreferences>(this, CONFIG_FILE);
}
}
}
52 changes: 52 additions & 0 deletions XMLSerialization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.IO;
using System.Xml.Serialization;

namespace QuantumOfSolace
{
public class XMLSerialization
{
public static T ReadFromXMLFile<T>(string filePath) where T : new()
{
StreamReader reader = null;
try
{
if (!File.Exists(filePath))
return new T();

XmlSerializer serializer = new XmlSerializer(typeof(T));
reader = new StreamReader(filePath);
return (T)serializer.Deserialize(reader);
}
finally
{
if (reader != null)
reader.Close();
}
}

public static void SaveObjectToXML<T>(T objectToStore, string filePath) where T : new()
{
if (!Directory.Exists(Directory.GetParent(filePath).FullName))
Directory.CreateDirectory(Directory.GetParent(filePath).FullName);

StreamWriter writter = null;

try
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
writter = new StreamWriter(filePath);
serializer.Serialize(writter, objectToStore);
}
catch (Exception e)
{
throw e;
}
finally
{
if (writter != null)
writter.Close();
}
}
}
}

0 comments on commit 9094544

Please sign in to comment.