Files
UVtools/UVtools.Core/Scripting/ScriptParser.cs
T
Tiago Conceição 9276735203 v3.7.2
- **File formats:**
  - (Add) AnyCubic PM3R (#587)
  - (Add) AnyCubic PMX2
  - (Fix) LGS: `LightOffDelay` is `WaitTimeBeforeCure` in this format
- **PrusaSlicer printer:**
  - (Add) AnyCubic Photon M3 Premium
  - (Add) AnyCubic Photon Mono X2
- (Fix) Scripting: Unable to use sub-classes (#583)
- (Fix) Loading an image as file cause application to crash
- (Fix) "File - Send to" doesn't ignore case on file extension names resulting in ignore files in a uppercase even if extension is correct
- (Fix) Do not show layer material milliliters when value is 0 or the percentage is NaN
- (Fix) Auto-upgrade on Linux with AppImage integrated on system causes the file name to grow with hash strings
2022-10-20 21:42:31 +01:00

62 lines
2.3 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.Text.RegularExpressions;
namespace UVtools.Core.Scripting;
public static class ScriptParser
{
public static string ParseScriptFromFile(string path)
{
return ParseScriptFromText(File.ReadAllText(path));
}
/// <summary>
/// Parse the script and clean forbidden keywords
/// </summary>
/// <param name="text">Text to parse</param>
/// <returns>The parsed text</returns>
public static string ParseScriptFromText(string text)
{
if(!Regex.Match(text, @"(void\s+ScriptInit\s*\(\s*\))").Success)
{
throw new ArgumentException("The method \"void ScriptInit()\" was not found on script, please verify the script.");
}
if (!Regex.Match(text, @"(string\s*[?]?\s+ScriptValidate\s*\(\s*\))").Success)
{
throw new ArgumentException("The method \"string ScriptValidate()\" was not found on script, please verify the script.");
}
if (!Regex.Match(text, @"(bool\s+ScriptExecute\s*\(\s*\))").Success)
{
throw new ArgumentException("The method \"bool ScriptExecute()\" was not found on script, please verify the script.");
}
var textLength = text.Length;
sbyte bracketsToRemove = 0;
text = Regex.Replace(text, @"(namespace\s+.+\n*\s*{)", string.Empty);
if (textLength != text.Length) bracketsToRemove++;
else text = Regex.Replace(text, @"(namespace\s+.+\n*\s*;)", string.Empty); // NET >= 6.0
textLength = text.Length;
var regex = new Regex(@"(.*class\s+.*\n*.*{)");
text = regex.Replace(text, string.Empty, 1);
if (textLength != text.Length) bracketsToRemove++;
if (bracketsToRemove <= 0) return text;
for (textLength = text.Length - 1; textLength >= 0 && bracketsToRemove > 0; textLength--)
{
if (text[textLength] == '}') bracketsToRemove--;
}
return text[..textLength];
}
}