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

@ -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);
}
}
}