SteamController: Use Roslyn Scripting to compile UserProfiles

- This looks into `UserProfiles/` and compiles user profiles
- This exposes a very minimal scripting interface as defined by `Dynamic.Globals`
This commit is contained in:
Kamil Trzciński 2022-12-10 14:39:42 +01:00
parent b24ae302b1
commit e5debff45b
8 changed files with 504 additions and 11 deletions

View file

@ -10,6 +10,12 @@ namespace SteamController
public const String Title = "Steam Controller";
public static readonly String TitleWithVersion = Title + " v" + Application.ProductVersion.ToString();
public static readonly Dictionary<String, Profiles.Profile> PreconfiguredUserProfiles = new Dictionary<String, Profiles.Profile>()
{
{ "*.desktop.cs", new Profiles.Predefined.DesktopProfile() { Name = "Desktop" } },
{ "*.x360.cs", new Profiles.Predefined.X360HapticProfile() { Name = "X360" } }
};
Container components = new Container();
NotifyIcon notifyIcon;
StartupManager startupManager = new StartupManager(Title);
@ -55,17 +61,39 @@ namespace SteamController
startupManager.Startup = false;
});
// Set available profiles
ProfilesSettings.Helpers.ProfileStringConverter.Profiles = context.Profiles.
Where((profile) => profile.Visible).
Select((profile) => profile.Name).ToArray();
Instance.RunOnce(TitleWithVersion, "Global\\SteamController");
Instance.RunUpdater(TitleWithVersion);
if (Instance.WantsRunOnStartup)
startupManager.Startup = true;
notifyIcon = new NotifyIcon(components);
notifyIcon.Icon = WindowsDarkMode.IsDarkModeEnabled ? Resources.microsoft_xbox_controller_off_white : Resources.microsoft_xbox_controller_off;
notifyIcon.Text = TitleWithVersion;
notifyIcon.Visible = true;
#if DEBUG
foreach (var profile in Profiles.Dynamic.RoslynDynamicProfile.GetUserProfiles(PreconfiguredUserProfiles))
{
profile.ErrorsChanged += (errors) =>
{
notifyIcon.ShowBalloonTip(
3000, profile.Name,
String.Join("\n", errors),
ToolTipIcon.Error
);
};
profile.Compile();
profile.Watch();
context.Profiles.Add(profile);
}
#endif
// Set available profiles
ProfilesSettings.Helpers.ProfileStringConverter.Profiles = context.Profiles.
Where((profile) => profile.Visible).
Select((profile) => profile.Name).ToArray();
var contextMenu = new ContextMenuStrip(components);
var enabledItem = new ToolStripMenuItem("&Enabled");
@ -81,7 +109,12 @@ namespace SteamController
var profileItem = new ToolStripMenuItem(profile.Name);
profileItem.Click += delegate { context.SelectProfile(profile.Name); };
contextMenu.Opening += delegate { profileItem.Checked = context.CurrentProfile == profile; };
contextMenu.Opening += delegate
{
profileItem.Checked = context.CurrentProfile == profile;
profileItem.ToolTipText = String.Join("\n", profile.Errors ?? new string[0]);
profileItem.Enabled = profile.Errors is null;
};
contextMenu.Items.Add(profileItem);
}
@ -116,10 +149,6 @@ namespace SteamController
var exitItem = contextMenu.Items.Add("&Exit");
exitItem.Click += delegate { Application.Exit(); };
notifyIcon = new NotifyIcon(components);
notifyIcon.Icon = WindowsDarkMode.IsDarkModeEnabled ? Resources.microsoft_xbox_controller_off_white : Resources.microsoft_xbox_controller_off;
notifyIcon.Text = TitleWithVersion;
notifyIcon.Visible = true;
notifyIcon.ContextMenuStrip = contextMenu;
var contextStateUpdate = new System.Windows.Forms.Timer(components);