this repo has no description
at master 112 lines 3.5 kB view raw
1using GDWeave; 2 3namespace Teemaw.Calico.LexicalTransformer; 4 5public class TransformationRuleScriptModBuilder 6{ 7 private IModInterface? _mod; 8 private string? _name; 9 private string? _scriptPath; 10 private Func<bool> _predicate = () => true; 11 private List<TransformationRule> _rules = []; 12 13 /// <summary> 14 /// Sets the IModInterface to be used by the ScriptMod. 15 /// </summary> 16 /// <param name="mod"></param> 17 /// <returns></returns> 18 public TransformationRuleScriptModBuilder ForMod(IModInterface mod) 19 { 20 _mod = mod; 21 return this; 22 } 23 24 /// <summary> 25 /// Sets the predicate which must return true for this ScriptMod to run. Config options MUST be 26 /// evaluated at patch-time since the environment is not fully known at load-time. Currently, 27 /// this affects mod conflict detection, but there may be other affected features in the future. 28 /// </summary> 29 /// <param name="predicate"></param> 30 /// <returns></returns> 31 public TransformationRuleScriptModBuilder When(Func<bool> predicate) 32 { 33 _predicate = predicate; 34 return this; 35 } 36 37 /// <summary> 38 /// Sets the name of the ScriptMod. Used for logging. 39 /// </summary> 40 /// <param name="name"></param> 41 /// <returns></returns> 42 public TransformationRuleScriptModBuilder Named(string name) 43 { 44 _name = name; 45 return this; 46 } 47 48 /// <summary> 49 /// Sets the Godot resource path of the script to be patched. 50 /// </summary> 51 /// <param name="scriptPath"></param> 52 /// <returns></returns> 53 public TransformationRuleScriptModBuilder Patching(string scriptPath) 54 { 55 _scriptPath = scriptPath; 56 return this; 57 } 58 59 /// <summary> 60 /// Adds a TransformationRule to the TransformationRuleScriptMod. 61 /// </summary> 62 /// <param name="rule"></param> 63 /// <returns></returns> 64 public TransformationRuleScriptModBuilder AddRule(TransformationRule rule) 65 { 66 if (_rules.Select(r => r.Name).Contains(rule.Name)) 67 { 68 throw new InvalidOperationException( 69 $"Another rule with the name '{rule.Name}' already exists!"); 70 } 71 72 _rules.Add(rule); 73 return this; 74 } 75 76 /// <summary> 77 /// Adds the TransformationRule built by calling Build() on the provided builder to the TransformationRuleScriptMod. 78 /// </summary> 79 /// <param name="rule"></param> 80 /// <returns></returns> 81 public TransformationRuleScriptModBuilder AddRule(TransformationRuleBuilder rule) 82 { 83 return AddRule(rule.Build()); 84 } 85 86 /// <summary> 87 /// Build the TransformationRuleScriptMod. 88 /// </summary> 89 /// <returns></returns> 90 /// <exception cref="ArgumentNullException">Thrown if any required fields were not set.</exception> 91 public TransformationRuleScriptMod Build() 92 { 93 if (_mod == null) 94 { 95 throw new ArgumentNullException(nameof(_mod), "Mod cannot be null"); 96 } 97 98 if (string.IsNullOrEmpty(_name)) 99 { 100 throw new ArgumentNullException(nameof(_name), "Name cannot be null or empty"); 101 } 102 103 if (string.IsNullOrEmpty(_scriptPath)) 104 { 105 throw new ArgumentNullException(nameof(_scriptPath), 106 "Script path cannot be null or empty"); 107 } 108 109 return new TransformationRuleScriptMod(_mod, _name, _scriptPath, _predicate, 110 _rules.ToArray()); 111 } 112}