Files
UVtools/UVtools.Core/Operations/OperationScripting.cs
T
Tiago Conceição 65ef92d7f0 v2.8.0
* **Scripting:**
   * Add scripting capability, this allow to run external scripts inside the GUI and take advantage of visual layout to display user input fields
   * Scripts run under the "Roslyn Scripting API" and can make use of the whole C# language, this mean a huge boost compared to PowerShell scripts
   * Scripts are written in the same way UVtools is, by learning and programing scripts you are learning the UVtools core
   * For more information see the script sample: https://github.com/sn4k3/UVtools/tree/master/UVtools.ScriptSample
   * To run scripts go to: Tools - Scripting
* **File formats:**
   * Add a check and warning when opening an file that have a diferent layer and file resolution
* **Issues:**
   * Add "Print height" as new type of issue detection, all layers that goes beyond maximum printer Z height will be flagged as PrintHeight issue
   * Print height issues will not be automatical fixed, however user can fix it by remove some layers to counter the problem, still is recommended to resize object on slicer
   * Fix unable to compute issues when only islands or overhangs are selected to be detected alone (#177)
* **Settings:**
   * Add default directory for scripts on "General - File dialogs"
   * Add checkbox on "Issues - Compute - Print height" to enable or disable this type of detection
   * Add numerical on "Issues - Print height - Offset" to define a custom offset from Z top
   * Fix default directories input width to not grow with text, it was overflowing on large strings
* **Menu - Help:**
   * Add web link to "Wiki & tutorials"
   * Add web link to "Facebook group"
   * Add web link to "Report a issue"
   * Add web link to "Ask a question"
   * Add web link to "Suggest an improvement or new features"
2021-04-02 04:26:55 +01:00

169 lines
5.2 KiB
C#

/*
* GNU AFFERO GENERAL PUBLIC LICENSE
* Version 3, 19 November 2007
* Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
using System;
using System.IO;
using System.Xml.Serialization;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using UVtools.Core.FileFormats;
using UVtools.Core.Objects;
using UVtools.Core.Scripting;
namespace UVtools.Core.Operations
{
[Serializable]
public sealed class OperationScripting : Operation
{
#region Members
public event EventHandler OnScriptReload;
private string _filePath;
private string _scriptText;
private ScriptState _scriptState;
#endregion
#region Overrides
public override bool CanHaveProfiles => false;
public override string Title => "Scripting";
public override string Description =>
$"Run external scripts to manipulate the loaded file\n" +
$"The scripts have wide access to your system and able to do modifications, read/write files, etc. " +
$"Make sure to run only the scripts you trust! Or run UVtools in a sandbox while executing this.";
public override string ConfirmationText =>
$"Run script from layers {LayerIndexStart} through {LayerIndexEnd}?";
public override string ProgressTitle =>
$"Scripting from layers {LayerIndexStart} through {LayerIndexEnd}";
public override string ProgressAction => "Scripted layers";
public override StringTag Validate(params object[] parameters)
{
if (!CanExecute)
{
return new StringTag("Script is not loaded.");
}
var scriptValidation = _scriptState.ContinueWithAsync<string>("return ScriptValidate();").Result;
return new StringTag(scriptValidation.ReturnValue);
}
#endregion
#region Enums
#endregion
#region Properties
public ScriptGlobals ScriptGlobals { get; private set; }
public string FilePath
{
get => _filePath;
set
{
if (value is null)
{
RaiseAndSetIfChanged(ref _filePath, null);
}
else
{
if (!value.EndsWith(".csx") && !value.EndsWith(".cs")) return;
if (!File.Exists(value)) return;
if (!RaiseAndSetIfChanged(ref _filePath, value)) return;
}
RaisePropertyChanged(nameof(HaveFile));
}
}
[XmlIgnore]
public string ScriptText
{
get => _scriptText;
set => RaiseAndSetIfChanged(ref _scriptText, value);
}
[XmlIgnore]
public bool CanExecute => !string.IsNullOrWhiteSpace(_filePath) && _scriptState is not null;
[XmlIgnore]
public bool HaveFile => !string.IsNullOrWhiteSpace(_filePath);
/*public override string ToString()
{
var result = $"[{_infillType}] [Wall: {_wallThickness}px] [B: {_infillBrightness}px] [T: {_infillThickness}px] [S: {_infillSpacing}px]" + LayerRangeString;
if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
return result;
}*/
#endregion
#region Constructor
public OperationScripting() { }
public OperationScripting(FileFormat slicerFile) : base(slicerFile)
{ }
#endregion
#region Equality
#endregion
#region Methods
public void ReloadScriptFromFile(string filePath = null)
{
if (!string.IsNullOrWhiteSpace(filePath)) FilePath = filePath;
if (string.IsNullOrWhiteSpace(_filePath) || !File.Exists(_filePath)) return;
ReloadScriptFromText(File.ReadAllText(_filePath));
}
public void ReloadScriptFromText(string text = null)
{
if (!string.IsNullOrWhiteSpace(text)) ScriptText = text;
if (string.IsNullOrWhiteSpace(_scriptText)) return;
ScriptText = ScriptParser.ParseScriptFromText(_scriptText);
ScriptGlobals = new ScriptGlobals { SlicerFile = SlicerFile, Operation = this };
_scriptState = CSharpScript.RunAsync(_scriptText,
ScriptOptions.Default.AddReferences(typeof(About).Assembly).WithAllowUnsafe(true),
ScriptGlobals).Result;
var result = _scriptState.ContinueWithAsync("ScriptInit();").Result;
RaisePropertyChanged(nameof(CanExecute));
OnScriptReload?.Invoke(this, EventArgs.Empty);
}
protected override bool ExecuteInternally(OperationProgress progress)
{
ScriptGlobals.Progress = progress;
var scriptExecute = _scriptState.ContinueWithAsync<bool>("return ScriptExecute();").Result;
return !progress.Token.IsCancellationRequested && scriptExecute.ReturnValue;
}
#endregion
}
}