Skip to content

Commit

Permalink
Extract default preset on load
Browse files Browse the repository at this point in the history
  • Loading branch information
luttje committed Jun 29, 2022
1 parent e684fdf commit 3f6dadb
Show file tree
Hide file tree
Showing 10 changed files with 390 additions and 17 deletions.
1 change: 1 addition & 0 deletions KeyToJoy/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static void Load()
if (!File.Exists(configPath))
{
Instance = new Config();
Instance.isInitialized = true;
Instance.Save();
return;
}
Expand Down
1 change: 1 addition & 0 deletions KeyToJoy/InitForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public InitForm()
private void InitForm_Load(object sender, EventArgs e)
{
Config.Load();
MappingPreset.ExtractDefaultIfNotExists();
SimGamePad.Instance.Initialize();
Program.GoToNextForm(new MainForm());
}
Expand Down
1 change: 1 addition & 0 deletions KeyToJoy/KeyToJoy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="packages.config" />
<EmbeddedResource Include="default-profile.k2j.json" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
10 changes: 0 additions & 10 deletions KeyToJoy/MainForm.Designer.cs

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

5 changes: 0 additions & 5 deletions KeyToJoy/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,11 +377,6 @@ private void savePresetToolStripMenuItem_Click(object sender, EventArgs e)
MessageBox.Show("When you make changes to a preset, changes are automatically saved. This button is only here to explain that feature to you.", "Preset already saved!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void savePresetCopyAsToolStripMenuItem_Click(object sender, EventArgs e)
{

}

private void openPresetFolderToolStripMenuItem_Click(object sender, EventArgs e)
{
Process.Start(MappingPreset.GetSaveDirectory());
Expand Down
22 changes: 21 additions & 1 deletion KeyToJoy/Mapping/MappingPreset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;

namespace KeyToJoy.Mapping
{
Expand All @@ -14,8 +15,10 @@ internal class MappingPreset
{
const int NO_VERSION = 0;
const int CURRENT_VERSION = 4;


public const string DEFAULT_PRESET_PATH = "default-profile";
public const string EXTENSION = ".k2j.json";

public const string SAVE_DIR = "Presets";

[JsonProperty]
Expand Down Expand Up @@ -93,6 +96,23 @@ private bool PostLoad(string filePath)
return true;
}

internal static void ExtractDefaultIfNotExists()
{
var defaultPath = Path.Combine(GetSaveDirectory(), $"{DEFAULT_PRESET_PATH}{EXTENSION}");

if (File.Exists(defaultPath))
return;

using (var file = new FileStream(defaultPath, FileMode.Create, FileAccess.Write))
using (var writer = new BinaryWriter(file))
{
writer.Write(Properties.Resources.default_profile_k2j);
}

if(Config.Instance.LastLoadedPreset == null)
Config.Instance.LastLoadedPreset = defaultPath;
}

internal static MappingPreset Load(string filePath)
{
var serializer = GetSerializer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public KeyboardTriggerOptionsControl()
cmbPressedState.DataSource = Enum.GetValues(typeof(PressState));
cmbPressedState.SelectedIndex = 0;

ResetTrapping();
StartTrapping();
}

public void Select(BaseTrigger trigger)
Expand All @@ -54,6 +54,7 @@ public void Setup(BaseTrigger trigger)
private void StartTrapping()
{
txtKeyBind.Text = TEXT_CHANGE;
txtKeyBind.Focus();
isTrapping = true;
}

Expand Down
10 changes: 10 additions & 0 deletions KeyToJoy/Properties/Resources.Designer.cs

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

3 changes: 3 additions & 0 deletions KeyToJoy/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,7 @@
<data name="script_code" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Graphics\Icons\script_code.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="default_profile_k2j" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\default-profile.k2j.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
</root>
Loading

0 comments on commit 3f6dadb

Please sign in to comment.