Skip to content

Commit

Permalink
Support per-project engine arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
MineBill committed Jan 10, 2024
1 parent ec85820 commit a29e7b6
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Seed/Models/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public record Project(string Name, string Path, EngineVersion? EngineVersion = n
/// </summary>
public bool IsTemplate { get; set; }

/// <summary>
/// The engine arguments to use for this project.
/// </summary>
public string? ProjectArguments { get; set; }

/// <summary>
/// The associated engine reference with this project.
/// This is filled after loading the projects(if the engine is found) and is provided
Expand Down
16 changes: 13 additions & 3 deletions Seed/Services/Implementations/ProjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void RunProject(Project project)
{
FileName = "/bin/sh",
Arguments =
$" -c \"setsid {engine.GetExecutablePath(engine.PreferredConfiguration)} -project \"{Path.GetFullPath(project.Path)}\" \"",
$" -c \"setsid {engine.GetExecutablePath(engine.PreferredConfiguration)} -project \"{Path.GetFullPath(project.Path)}\" {project.ProjectArguments ?? string.Empty} \"",
CreateNoWindow = true,
};

Expand All @@ -73,7 +73,7 @@ public void RunProject(Project project)
var info = new ProcessStartInfo
{
FileName = engine.GetExecutablePath(engine.PreferredConfiguration),
Arguments = $"-project \"{Path.GetFullPath(project.Path)}\"",
Arguments = $"-project \"{Path.GetFullPath(project.Path)}\" {project.ProjectArguments ?? string.Empty}",
};

Process.Start(info);
Expand Down Expand Up @@ -121,7 +121,17 @@ private void LoadProjects()
foreach (var project in Projects)
{
// BUG: This will fail if you remove all engines but have some projects.
project.Engine = _engineManager.Engines.First(x => x.Version == project.EngineVersion);
try
{
project.Engine = _engineManager.Engines.First(x => x.Version == project.EngineVersion);
}
catch (InvalidOperationException ioe)
{
// NOTE: We do not perform any kind of automatic increase in the project version.
// If a new patch gets released, the user must explicitly set their project to use
// that engine version.
project.Engine = null;
}
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions Seed/ViewModels/CommandLineOptionsViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Reactive;
using System.Windows.Input;
using ReactiveUI;
using Seed.Models;

namespace Seed.ViewModels;

public class CommandLineOptionsViewModel: ViewModelBase
{
public ReactiveCommand<Unit, string> SaveCommand { get; set; }
private string _arguments;

public string Arguments
{
get => _arguments;
set => this.RaiseAndSetIfChanged(ref _arguments, value);
}

private string _title;

public string Title
{
get => _title;
set => this.RaiseAndSetIfChanged(ref _title, value);
}

public CommandLineOptionsViewModel(Project project)
{
Title = $"Editing arguments for {project.Name}";
Arguments = project.ProjectArguments ?? string.Empty;
SaveCommand = ReactiveCommand.Create(() => Arguments);
}
}
23 changes: 23 additions & 0 deletions Seed/ViewModels/ProjectViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.IO;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Windows.Input;
using Avalonia.Input;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using MsBox.Avalonia;
using NLog;
using ReactiveUI;
using Seed.Models;
using Seed.Services;
Expand All @@ -15,6 +18,7 @@ namespace Seed.ViewModels;

public class ProjectViewModel : ViewModelBase
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly IProjectManager _projectManager;

public Project Project { get; }
Expand Down Expand Up @@ -56,6 +60,9 @@ public bool IsTemplate
public ICommand? MarkAsTemplateCommand { get; }
public ICommand? ClearCacheCommand { get; }
public ICommand? OpenProjectFolderCommand { get; }
public ICommand? EditProjectArgumentsCommand { get; }

public readonly Interaction<CommandLineOptionsViewModel, string> OpenCommandLineOptionsEditor = new();

public ProjectViewModel(IEngineManager engineManager, IProjectManager projectManager, IFilesService filesService,
Project project)
Expand Down Expand Up @@ -84,6 +91,17 @@ public ProjectViewModel(IEngineManager engineManager, IProjectManager projectMan
{
Project.IsTemplate = !Project.IsTemplate;
this.RaisePropertyChanged(nameof(IsProjectTemplate));
_projectManager.Save();
});

EditProjectArgumentsCommand = ReactiveCommand.Create(async () =>
{
var vm = new CommandLineOptionsViewModel(Project);
var result = await OpenCommandLineOptionsEditor.Handle(vm);
Logger.Info($"Result: {result}");
Project.ProjectArguments = result;
_projectManager.Save();
});
Task.Run(LoadIcon);
Expand Down Expand Up @@ -156,4 +174,9 @@ change the associated engine version with this project in the project settings.
box.ShowWindowDialogAsync(App.Current.MainWindow);
}
}

private void EditProjectArguments()
{

}
}
28 changes: 28 additions & 0 deletions Seed/Views/Dialogs/CommandLineOptionsDialog.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Window 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:vm="using:Seed.ViewModels"
mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="100"
x:Class="Seed.Views.Dialogs.CommandLineOptionsDialog"
x:DataType="vm:CommandLineOptionsViewModel"
WindowStartupLocation="CenterOwner"
Width="400"
Height="100"
Icon="/Assets/seed-logo-blue-64.png"
Title="{Binding Title}">
<Border BorderThickness="5">
<DockPanel>
<Button
DockPanel.Dock="Bottom"
HorizontalAlignment="Center"
Command="{Binding SaveCommand}">Save</Button>
<TextBlock DockPanel.Dock="Top">
See docs.flaxengine.com
</TextBlock>
<TextBox
DockPanel.Dock="Top"
Text="{Binding Arguments}"></TextBox>
</DockPanel>
</Border>
</Window>
22 changes: 22 additions & 0 deletions Seed/Views/Dialogs/CommandLineOptionsDialog.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using Avalonia.Controls;
using Seed.ViewModels;

namespace Seed.Views.Dialogs;

public partial class CommandLineOptionsDialog : Window
{
public CommandLineOptionsDialog()
{
InitializeComponent();
DataContextProperty.Changed.Subscribe(OnDataContextChanged);
}

private void OnDataContextChanged(object _)
{
if (DataContext is CommandLineOptionsViewModel viewModel)
{
viewModel.SaveCommand.Subscribe(Close);
}
}
}
2 changes: 1 addition & 1 deletion Seed/Views/ProjectView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<MenuItem Command="{Binding RunProjectCommand}" Header="Run Project"></MenuItem>
<MenuItem Command="{Binding OpenProjectFolderCommand}" Header="Open in file explorer"></MenuItem>
<MenuItem Command="{Binding MarkAsTemplateCommand}" Header="Mark as template"></MenuItem>
<MenuItem Header="Edit startup arguments(Not implemented)"></MenuItem>
<MenuItem Command="{Binding EditProjectArgumentsCommand}" Header="Edit startup arguments"></MenuItem>
<MenuItem Command="{Binding ClearCacheCommand}" Header="Clear cache"></MenuItem>
<MenuItem Command="{Binding RemoveProjectCommand}" Header="Remove Project"></MenuItem>
</MenuFlyout>
Expand Down
20 changes: 19 additions & 1 deletion Seed/Views/ProjectView.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
using System;
using System.Reactive;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.ReactiveUI;
using ReactiveUI;
using Seed.ViewModels;
using Seed.Views.Dialogs;

namespace Seed.Views;

public partial class ProjectView : UserControl
public partial class ProjectView : ReactiveUserControl<ProjectViewModel>
{
public ProjectView()
{
InitializeComponent();
this.WhenActivated(action =>
action(ViewModel!.OpenCommandLineOptionsEditor.RegisterHandler(OpenCommandLineOptionsEditorHandler)));
}

private async Task OpenCommandLineOptionsEditorHandler(InteractionContext<CommandLineOptionsViewModel, string> obj)
{
var editor = new CommandLineOptionsDialog()
{
DataContext = obj.Input
};

var ret = await editor.ShowDialog<string>(App.Current.MainWindow);
obj.SetOutput(ret);
}

private void InputElement_OnDoubleTapped(object? sender, TappedEventArgs e)
Expand Down

0 comments on commit a29e7b6

Please sign in to comment.