basic starscript support + a textbox that provides the starscript executed result & compiled script as well as suggestions

This commit is contained in:
GreemDev
2025-07-27 01:09:59 -05:00
parent 45b2e613cf
commit 8a3ccaafe3
9 changed files with 345 additions and 0 deletions

View File

@ -55,6 +55,7 @@
<PackageVersion Include="SkiaSharp" Version="2.88.9" />
<PackageVersion Include="SkiaSharp.NativeAssets.Linux" Version="2.88.9" />
<PackageVersion Include="SPB" Version="0.0.4-build32" />
<PackageVersion Include="Starscript.Net" Version="1.0.35" />
<PackageVersion Include="System.IO.Hashing" Version="9.0.2" />
<PackageVersion Include="System.Management" Version="9.0.2" />
<PackageVersion Include="UnicornEngine.Unicorn" Version="2.0.2-rc1-fb78016" />

View File

@ -73,6 +73,7 @@
<PackageReference Include="Silk.NET.Vulkan.Extensions.EXT" />
<PackageReference Include="Silk.NET.Vulkan.Extensions.KHR" />
<PackageReference Include="SPB" />
<PackageReference Include="Starscript.Net"/>
<PackageReference Include="SharpZipLib" />
</ItemGroup>

View File

@ -0,0 +1,29 @@
using Ryujinx.Ava.Systems.AppLibrary;
using Ryujinx.Common;
using Starscript;
namespace Ryujinx.Ava.Systems.Starscript
{
public static class RyujinxStarscript
{
public static readonly StarscriptHypervisor Hypervisor = StarscriptHypervisor.Create().WithStandardLibrary(true);
static RyujinxStarscript()
{
Hypervisor.Set("ryujinx.releaseChannel",
ReleaseInformation.IsCanaryBuild
? "Canary"
: ReleaseInformation.IsReleaseBuild
? "Stable"
: "Custom");
Hypervisor.Set("ryujinx.version", Program.Version);
Hypervisor.Set("appLibrary", StarscriptHelper.Wrap(RyujinxApp.MainWindow.ApplicationLibrary));
Hypervisor.Set("currentApplication", () =>
RyujinxApp.MainWindow.ApplicationLibrary.FindApplication(
RyujinxApp.MainWindow.ViewModel.AppHost?.ApplicationId ?? 0,
out ApplicationData appData)
? StarscriptHelper.Wrap(appData)
: Value.Null);
}
}
}

View File

@ -0,0 +1,68 @@
using Gommon;
using Ryujinx.Ava.Systems.AppLibrary;
using Starscript;
using System;
namespace Ryujinx.Ava.Systems.Starscript
{
public static class StarscriptHelper
{
public static ValueMap Wrap(ApplicationLibrary appLib)
{
ValueMap lMap = new();
lMap.Set("appCount", () => appLib.Applications.Count);
lMap.Set("dlcCount", () => appLib.DownloadableContents.Count);
lMap.Set("updateCount", () => appLib.TitleUpdates.Count);
lMap.Set("has", ctx =>
{
ulong titleId;
try
{
titleId = ctx.Constrain(Constraint.ExactlyOneArgument).NextString(1).ToULong();
}
catch (FormatException)
{
throw ctx.Error(
$"Invalid input to {ctx.FormattedName}; input must be a hexadecimal number in a string.");
}
return appLib.FindApplication(titleId, out _);
});
lMap.Set("get", ctx =>
{
ulong titleId;
try
{
titleId = ctx.Constrain(Constraint.ExactlyOneArgument).NextString(1).ToULong();
}
catch (FormatException)
{
throw ctx.Error(
$"Invalid input to {ctx.FormattedName}; input must be a hexadecimal number in a string.");
}
return appLib.FindApplication(titleId,
out ApplicationData applicationData)
? Wrap(applicationData)
: null;
});
return lMap;
}
public static ValueMap Wrap(ApplicationData appData)
{
ValueMap aMap = new();
aMap.Set("name", appData.Name);
aMap.Set("version", appData.Version);
aMap.Set("developer", appData.Developer);
aMap.Set("fileExtension", appData.FileExtension);
aMap.Set("fileSize", appData.FileSizeString);
aMap.Set("hasLdnGames", appData.HasLdnGames);
aMap.Set("timePlayed", appData.TimePlayedString);
aMap.Set("isFavorite", appData.Favorite);
return aMap;
}
}
}

View File

@ -0,0 +1,21 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="using:Ryujinx.Ava.Systems.Starscript"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Ryujinx.Ava.Systems.Starscript.StarscriptTextBox"
x:DataType="local:StarscriptTextBoxViewModel">
<StackPanel Spacing="10">
<TextBlock Text="{Binding ErrorMessage}" IsVisible="{Binding HasError}"/>
<TextBlock Text="{Binding CurrentScriptResult}" IsVisible="{Binding !HasError}"/>
<AutoCompleteBox Name="InputBox"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
FilterMode="Custom"
MinimumPrefixLength="0"
MaxDropDownHeight="400">
</AutoCompleteBox>
</StackPanel>
</UserControl>

View File

@ -0,0 +1,95 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Styling;
using FluentAvalonia.UI.Controls;
using Humanizer;
using Ryujinx.Ava.Common.Locale;
using Ryujinx.Ava.UI.Controls;
using Ryujinx.Ava.UI.Helpers;
using Starscript;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Ryujinx.Ava.Systems.Starscript
{
public partial class StarscriptTextBox : RyujinxControl<StarscriptTextBoxViewModel>
{
public IReadOnlyList<string> CurrentSuggestions => ViewModel.CurrentSuggestions;
public string CurrentScriptSource => ViewModel.CurrentScriptSource;
public Exception Exception => ViewModel.Exception;
public Script CurrentScript => ViewModel.CurrentScript;
public StringSegment CurrentScriptResult => ViewModel.CurrentScriptResult;
public StarscriptTextBox()
{
InitializeComponent();
InputBox.TextInput += HandleTextChanged;
InputBox.AsyncPopulator = GetSuggestionsAsync;
InputBox.MinimumPopulateDelay = 0.Seconds();
InputBox.TextFilter = (_, _) => true;
InputBox.TextSelector = (text, suggestion) =>
{
if (text is not null && suggestion is null)
return text;
if (text is null && suggestion is not null)
return suggestion;
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
if (text is null && suggestion is null)
return string.Empty;
var sb = new StringBuilder(text.Length + suggestion.Length + 1);
sb.Append(text);
for (int i = 0; i < suggestion.Length - 1; i++)
{
if (text.EndsWith(suggestion[..(suggestion.Length - i - 1)]))
{
suggestion = suggestion[(suggestion.Length - i - 1)..];
break;
}
}
sb.Append(suggestion);
return sb.ToString();
};
Style textStyle = new(x => x.OfType<AutoCompleteBox>().Descendant().OfType<TextBlock>());
textStyle.Setters.Add(new Setter(MarginProperty, new Thickness(0, 0)));
Styles.Add(textStyle);
}
private Task<IEnumerable<object>> GetSuggestionsAsync(string input, CancellationToken token)
=> Task.FromResult(ViewModel.GetSuggestions(input, token));
private void HandleTextChanged(object sender, TextInputEventArgs eventArgs)
{
if (sender is AutoCompleteBox)
ViewModel.CurrentScriptSource = eventArgs.Text;
}
public static StarscriptTextBox Create(StarscriptHypervisor hv)
=> new() { ViewModel = new StarscriptTextBoxViewModel(hv) };
public static async Task Show()
{
ContentDialog contentDialog = new()
{
PrimaryButtonText = string.Empty,
SecondaryButtonText = string.Empty,
CloseButtonText = LocaleManager.Instance[LocaleKeys.UserProfilesClose],
Content = new StarscriptTextBox { ViewModel = new() }
};
await ContentDialogHelper.ShowAsync(contentDialog.ApplyStyles());
}
}
}

View File

@ -0,0 +1,123 @@
using CommunityToolkit.Mvvm.ComponentModel;
using Ryujinx.Ava.UI.ViewModels;
using Starscript;
using Starscript.Internal;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
namespace Ryujinx.Ava.Systems.Starscript
{
public partial class StarscriptTextBoxViewModel : BaseModel
{
private readonly StarscriptHypervisor _hv;
public StarscriptTextBoxViewModel(StarscriptHypervisor hv = null)
{
_hv = hv ?? RyujinxStarscript.Hypervisor;
}
public ObservableCollection<string> CurrentSuggestions { get; } = [];
[ObservableProperty] private bool _hasError;
[ObservableProperty] private StringSegment _currentScriptResult;
[ObservableProperty] private string _errorMessage;
private Exception _exception;
private string _currentScriptSource;
private Script _currentScript;
public Exception Exception
{
get => _exception;
set
{
ErrorMessage = (_exception = value) switch
{
ParseException pe => pe.Error.ToString(),
StarscriptException se => se.Message,
_ => string.Empty
};
OnPropertyChanged();
}
}
public string CurrentScriptSource
{
get => _currentScriptSource;
set
{
_currentScriptSource = value;
if (value is null)
{
CurrentScript = null;
CurrentScriptResult = null;
Exception = null;
HasError = false;
return;
}
try
{
CurrentScript = Compiler.DirectCompile(CurrentScriptSource);
Exception = null;
HasError = false;
}
catch (ParseException pe)
{
CurrentScript = null;
CurrentScriptResult = null;
Exception = pe;
HasError = true;
}
OnPropertyChanged();
}
}
public Script CurrentScript
{
get => _currentScript;
private set
{
try
{
CurrentScriptResult = value?.Execute(_hv)!;
_currentScript = value;
Exception = null;
HasError = false;
}
catch (StarscriptException se)
{
_currentScript = null;
CurrentScriptResult = null;
Exception = se;
HasError = true;
}
OnPropertyChanged();
}
}
public IEnumerable<object> GetSuggestions(string input, CancellationToken token)
{
CurrentScriptSource = input;
_hv.GetCompletions(CurrentScriptSource, CurrentScriptSource.Length, CreateCallback(), token);
OnPropertyChanged(nameof(CurrentSuggestions));
return CurrentSuggestions;
}
private CompletionCallback CreateCallback()
{
CurrentSuggestions.Clear();
return (result, isFunction) =>
CurrentSuggestions.Add(isFunction ? $"{result}(" : result);
}
}
}

View File

@ -253,6 +253,10 @@
Header="{ext:Locale MenuBarHelpAbout}"
Icon="{ext:Icon fa-solid fa-circle-info}"
ToolTip.Tip="{ext:Locale OpenAboutTooltip}" />
<MenuItem
Name="StarscriptDebugMenuItem"
Header="Debug Starscript"
Icon="{ext:Icon fa-solid fa-star}" />
<MenuItem
Name="UpdateMenuItem"
IsEnabled="{Binding CanUpdate}"

View File

@ -8,6 +8,7 @@ using LibHac.Ns;
using Ryujinx.Ava.Common.Locale;
using Ryujinx.Ava.Systems.AppLibrary;
using Ryujinx.Ava.Systems.Configuration;
using Ryujinx.Ava.Systems.Starscript;
using Ryujinx.Ava.UI.Controls;
using Ryujinx.Ava.UI.Helpers;
using Ryujinx.Ava.UI.ViewModels;
@ -51,6 +52,8 @@ namespace Ryujinx.Ava.UI.Views.Main
CompatibilityListMenuItem.Command = Commands.Create(() => CompatibilityListWindow.Show());
UpdateMenuItem.Command = MainWindowViewModel.UpdateCommand;
StarscriptDebugMenuItem.Command = Commands.Create(StarscriptTextBox.Show);
FaqMenuItem.Command =
SetupGuideMenuItem.Command =