| | | 1 | | using pva.Helpers.Extensions; |
| | | 2 | | using System.Text; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | |
| | | 5 | | namespace pva.SuperV.Engine.Processing |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Script definition used in a <see cref="Project"/>. |
| | | 9 | | /// </summary> |
| | | 10 | | public class ScriptDefinition |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Gets the name of the script. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <value> |
| | | 16 | | /// The name. |
| | | 17 | | /// </value> |
| | 674 | 18 | | public string Name { get; } |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets the name of the topic on which the script registers. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <value> |
| | | 23 | | /// The name of the topic. |
| | | 24 | | /// </value> |
| | 122 | 25 | | public string TopicName { get; } |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets the source code of the script. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <value> |
| | | 30 | | /// The source. |
| | | 31 | | /// </value> |
| | 169 | 32 | | public string Source { get; } |
| | | 33 | | /// <summary> |
| | | 34 | | /// The field references used in the script. |
| | | 35 | | /// </summary> |
| | | 36 | | [JsonIgnore] |
| | | 37 | | public List<FieldReference> fieldReferences; |
| | | 38 | | /// <summary> |
| | | 39 | | /// The parsed lines |
| | | 40 | | /// </summary> |
| | | 41 | | [JsonIgnore] |
| | | 42 | | public List<string> lines; |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Initializes a new instance of the <see cref="ScriptDefinition"/> class. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="name">The name.</param> |
| | | 48 | | /// <param name="topicName">Name of the topicon which the script registers.</param> |
| | | 49 | | /// <param name="source">The source code of script.</param> |
| | 162 | 50 | | public ScriptDefinition(string name, string topicName, string source) |
| | 162 | 51 | | { |
| | 162 | 52 | | Name = IdentifierValidation.ValidateIdentifier("script", name); |
| | 162 | 53 | | TopicName = topicName; |
| | 162 | 54 | | Source = source; |
| | 162 | 55 | | lines = ScriptParser.ParseLine(Source); |
| | 162 | 56 | | fieldReferences = ScriptParser.ParseFieldReferences(lines); |
| | 162 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets the code to be used when building project. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <returns>The code for the script.</returns> |
| | | 63 | | public string GetCode() |
| | 115 | 64 | | { |
| | 115 | 65 | | List<string> replacedScriptCode = ScriptParser.ReplaceFieldReferences(ScriptBase.ChangedInstance, lines, fie |
| | 115 | 66 | | StringBuilder generatedCode = new(); |
| | 345 | 67 | | HashSet<string> instanceReferences = [.. fieldReferences.Select(f => f.InstanceName ?? ScriptBase.ChangedIns |
| | 115 | 68 | | generatedCode.AppendLine($"public class {Name}Class : ScriptBase") |
| | 115 | 69 | | .AppendLine("{") |
| | 115 | 70 | | .AppendLine($" public {Name}Class(RunnableProject project, ScriptDefinition scriptDefinition): base(pro |
| | 115 | 71 | | .AppendLine(" {") |
| | 115 | 72 | | .AppendLine(" }") |
| | 115 | 73 | | .AppendLine("public override void HandleFieldValueChange(RunnableProject project, Dictionary<string, IIn |
| | 115 | 74 | | .AppendLine(" {"); |
| | 115 | 75 | | instanceReferences.ForEach(instance => |
| | 231 | 76 | | generatedCode.AppendLine($" var {instance} = instances[\"{instance}\"] as dynamic;")); |
| | 115 | 77 | | replacedScriptCode.ForEach(line => |
| | 230 | 78 | | generatedCode.AppendLine(line)); |
| | 115 | 79 | | generatedCode.AppendLine(" }") |
| | 115 | 80 | | .AppendLine("}"); |
| | 115 | 81 | | return generatedCode.ToString(); |
| | 115 | 82 | | } |
| | | 83 | | } |
| | | 84 | | } |