Skip to content

Commit

Permalink
[Editor] Added file association support for .fbproject (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dyvinia authored Aug 11, 2023
1 parent 4610d3a commit 4bfe582
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
39 changes: 36 additions & 3 deletions FrostyEditor/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
using Frosty.Core;
using Frosty.Core.Controls;
using FrostyCore;
using System.Text;
using System.Linq;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace FrostyEditor
Expand All @@ -36,13 +33,19 @@ public partial class App : Application
public static string Version = "";
public static long StartTime;

public static bool OpenProject { get; set; }

public static string LaunchArgs { get; private set; }

private FrostyConfiguration defaultConfig;

public App()
{
Assembly entryAssembly = Assembly.GetEntryAssembly();
Version = entryAssembly.GetName().Version.ToString();

Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;

Logger = new FrostyLogger();
Logger.Log("Frosty Editor v{0}", Version);

Expand Down Expand Up @@ -201,6 +204,36 @@ private void Application_Startup(object sender, StartupEventArgs e)
}
}

//check args to see if it is loading a project
if (e.Args.Length > 0)
{
string arg = e.Args[0];
if (arg.Contains(".fbproject"))
{
OpenProject = true;
LaunchArgs = arg;

//get game profile from project file
using (NativeReader reader = new NativeReader(new FileStream(arg, FileMode.Open, FileAccess.Read)))
{
if (reader.ReadULong() == 0x00005954534F5246)
{
reader.ReadUInt();
string gameProfile = reader.ReadNullTerminatedString();
try
{
defaultConfig = new FrostyConfiguration(gameProfile);
}
catch
{
FrostyMessageBox.Show("There was an error when trying to load project using the profile: " + gameProfile, "Frosty Editor");
OpenProject = false;
}
}
}
}
}

if (defaultConfig != null)
{
// load profile
Expand Down
5 changes: 5 additions & 0 deletions FrostyEditor/Windows/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,11 @@ private void FrostyWindow_Loaded(object sender, EventArgs e)
m_autoSaveTimer.Start();
}
}

if (App.OpenProject)
{
LoadProject(App.LaunchArgs, false);
}
}

private void logTextBox_TextChanged(object sender, TextChangedEventArgs e)
Expand Down
2 changes: 1 addition & 1 deletion FrostyModManager/Windows/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ private void FrostyWindow_FrostyLoaded(object sender, EventArgs e)
}
else
{
App.Logger.LogWarning("Custom Mods Directory does not exist, using default instead");
App.Logger.Log("Custom Mods Directory does not exist, using default instead");
}

FrostyTaskWindow.Show("Loading Mods", "", (task) =>
Expand Down
64 changes: 64 additions & 0 deletions FrostyPlugin/Windows/OptionsWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
using System.Windows.Data;
using FrostySdk;
using System.IO;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Media;

namespace Frosty.Core.Windows
{
Expand Down Expand Up @@ -101,6 +104,12 @@ public class EditorOptionsData : OptionsExtension
[EbxFieldMeta(EbxFieldType.Boolean)]
public bool RememberChoice { get; set; } = false;

[Category("Editor")]
[DisplayName("Set as Default Installation")]
[Description("Use this installation for .fbproject files.")]
[EbxFieldMeta(EbxFieldType.Boolean)]
public bool DefaultInstallation { get; set; } = false;

[Category("Update Checking")]
[DisplayName("Check for Updates")]
[Description("Check Github for Frosty updates on startup")]
Expand Down Expand Up @@ -159,6 +168,20 @@ public override void Load()

CommandLineArgs = Config.Get<string>("CommandLineArgs", "", ConfigScope.Game);

// Checks the registry for the current association against current frosty installation
string KeyName = "frostyproject";
string OpenWith = Assembly.GetEntryAssembly().Location;

try
{
string openCommand = Registry.CurrentUser.OpenSubKey("Software\\Classes\\" + KeyName).OpenSubKey("shell").OpenSubKey("open").OpenSubKey("command").GetValue("").ToString();
if (openCommand.Contains(OpenWith))
{
DefaultInstallation = true;
}
}
catch { }

//Language = new CustomComboData<string, string>(langs, langs) { SelectedIndex = langs.IndexOf(Config.Get<string>("Init", "Language", "English")) };

//AutosaveEnabled = Config.Get<bool>("Autosave", "Enabled", true);
Expand Down Expand Up @@ -207,6 +230,44 @@ public override void Save()

LocalizedStringDatabase.Current.Initialize();

// Create file association if enabled
if (DefaultInstallation)
{
string Extension = ".fbproject";
string KeyName = "frostyproject";
string OpenWith = Assembly.GetEntryAssembly().Location;
string FileDescription = "Frosty Project";

try
{
RegistryKey BaseKey = Registry.CurrentUser.CreateSubKey($"Software\\Classes\\{Extension}");
BaseKey.SetValue("", KeyName);

RegistryKey OpenMethod = Registry.CurrentUser.CreateSubKey($"Software\\Classes\\{KeyName}");
OpenMethod.SetValue("", FileDescription);
OpenMethod.CreateSubKey("DefaultIcon").SetValue("", $"\"{OpenWith}\",0");

RegistryKey Shell = OpenMethod.CreateSubKey("shell");
Shell.CreateSubKey("edit").CreateSubKey("command").SetValue("", $"\"{OpenWith}\" \"%1\"");
Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", $"\"{OpenWith}\" \"%1\"");
BaseKey.Close();
OpenMethod.Close();
Shell.Close();

RegistryKey CurrentUser = Registry.CurrentUser.OpenSubKey($"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\{Extension}", true);
CurrentUser.DeleteSubKey("UserChoice", false);
CurrentUser.Close();

// Tell explorer the file association has been changed
SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
}
catch (Exception ex)
{
SystemSounds.Hand.Play();
App.Logger.LogError($"Unable to Set File Association: {ex.Message}");
}
}

//Config.Add("Autosave", "Enabled", AutosaveEnabled);
//Config.Add("Autosave", "Period", AutosavePeriod);
//Config.Add("Autosave", "MaxCount", AutosaveMaxSaves);
Expand All @@ -221,6 +282,9 @@ public override void Save()
//Config.Add("Init", "Language", Language.SelectedName);
}

[DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);

public override bool Validate()
{
return true;
Expand Down

0 comments on commit 4bfe582

Please sign in to comment.