diff --git a/Project-Aurora/Project-Aurora/Profiles/Application.cs b/Project-Aurora/Project-Aurora/Profiles/Application.cs index 24dcc99cf..fa2903e06 100755 --- a/Project-Aurora/Project-Aurora/Profiles/Application.cs +++ b/Project-Aurora/Project-Aurora/Profiles/Application.cs @@ -86,7 +86,7 @@ public class Application : ObjectSettings, IInit, ILightEve protected UserControl control; public virtual UserControl Control { get { return control ?? (control = (UserControl)Activator.CreateInstance(this.Config.OverviewControlType, this)); } } - internal Dictionary EffectScripts { get; set; } + internal Dictionary> EffectScripts { get; set; } #endregion public event PropertyChangedEventHandler PropertyChanged; @@ -106,7 +106,7 @@ public Application(LightEventConfig config) } } }; - EffectScripts = new Dictionary(); + EffectScripts = new Dictionary>(); if (config.GameStateType != null) ParameterLookup = new GameStateParameterLookup(config.GameStateType); } @@ -373,7 +373,7 @@ private void Profile_PropertyChanged(object sender, System.ComponentModel.Proper SaveProfile(sender as ApplicationProfile); } - public bool RegisterEffect(string key, IEffectScript obj) + public bool RegisterEffect(string key, Func obj) { if (Disposed) return false; @@ -494,10 +494,11 @@ protected void LoadScripts(string profiles_path, bool force = false) Type typ = ((IronPython.Runtime.Types.PythonType)v.Value).__clrtype__(); if (!typ.IsInterface && typeof(IEffectScript).IsAssignableFrom(typ)) { - IEffectScript obj = Global.PythonEngine.Operations.CreateInstance(v.Value) as IEffectScript; - if (obj != null) + Func scriptCreator = () => Global.PythonEngine.Operations.CreateInstance(v.Value) as IEffectScript; + var scriptSpecimen = scriptCreator.Invoke(); + if (scriptSpecimen != null) { - if (!(obj.ID != null && this.RegisterEffect(obj.ID, obj))) + if (!(scriptSpecimen.ID != null && this.RegisterEffect(scriptSpecimen.ID, scriptCreator))) Global.logger.Warn($"Script \"{script}\" must have a unique string ID variable for the effect {v.Key}"); else anyLoaded = true; @@ -517,8 +518,9 @@ protected void LoadScripts(string profiles_path, bool force = false) { if (effectType.IsAssignableFrom(typ)) { - IEffectScript obj = (IEffectScript)Activator.CreateInstance(typ); - if (!(obj.ID != null && this.RegisterEffect(obj.ID, obj))) + Func scriptCreator = () => (IEffectScript) Activator.CreateInstance(typ); + var scriptSpecimen = scriptCreator.Invoke(); + if (!(scriptSpecimen.ID != null && this.RegisterEffect(scriptSpecimen.ID, scriptCreator))) Global.logger.Warn(string.Format("Script \"{0}\" must have a unique string ID variable for the effect {1}", script, typ.FullName)); else anyLoaded = true; diff --git a/Project-Aurora/Project-Aurora/Settings/Layers/ScriptLayerHandler.cs b/Project-Aurora/Project-Aurora/Settings/Layers/ScriptLayerHandler.cs index 9ce0b8218..6f915d4d9 100755 --- a/Project-Aurora/Project-Aurora/Settings/Layers/ScriptLayerHandler.cs +++ b/Project-Aurora/Project-Aurora/Settings/Layers/ScriptLayerHandler.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -10,6 +10,7 @@ using System.ComponentModel; using System.Runtime.CompilerServices; using Aurora.Settings.Overrides; +using Aurora.Utils; namespace Aurora.Settings.Layers { @@ -20,6 +21,7 @@ public class ScriptLayerHandlerProperties : LayerHandlerProperties, IN { internal Application profileManager; + public IEffectScript ScriptInstance { get; set; } + public event PropertyChangedEventHandler PropertyChanged; [JsonIgnore] @@ -60,8 +64,7 @@ public override EffectLayer Render(IGameState gamestate) { try { - IEffectScript script = this.profileManager.EffectScripts[this.Properties.Script]; - object script_layers = script.UpdateLights(Properties.ScriptProperties, gamestate); + object script_layers = ScriptInstance.UpdateLights(Properties.ScriptProperties, gamestate); if (script_layers is EffectLayer) layer = (EffectLayer)script_layers; else if (script_layers is EffectLayer[]) @@ -87,19 +90,22 @@ public override EffectLayer Render(IGameState gamestate) public VariableRegistry GetScriptPropertyRegistry() { - if (IsScriptValid) - { - return (VariableRegistry)profileManager.EffectScripts[this.Properties._Script].Properties.Clone(); + if (IsScriptValid) { + var tempScriptInstance = profileManager.EffectScripts[this.Properties._Script].Invoke(); + return (VariableRegistry) tempScriptInstance.Properties.Clone(); } return null; } - public void OnScriptChanged() - { + public void OnScriptChanged() { VariableRegistry varRegistry = GetScriptPropertyRegistry(); - if (varRegistry != null) + if (varRegistry != null) { Properties.ScriptProperties.Combine(varRegistry, true); + + // Create a new instance of the script and save it + ScriptInstance ??= profileManager.EffectScripts[this.Properties._Script].Invoke(); + } } [JsonIgnore]