Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions Project-Aurora/Project-Aurora/Profiles/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class Application : ObjectSettings<ApplicationSettings>, IInit, ILightEve
protected UserControl control;
public virtual UserControl Control { get { return control ?? (control = (UserControl)Activator.CreateInstance(this.Config.OverviewControlType, this)); } }

internal Dictionary<string, IEffectScript> EffectScripts { get; set; }
internal Dictionary<string, Func<IEffectScript>> EffectScripts { get; set; }
#endregion

public event PropertyChangedEventHandler PropertyChanged;
Expand All @@ -106,7 +106,7 @@ public Application(LightEventConfig config)
}
}
};
EffectScripts = new Dictionary<string, IEffectScript>();
EffectScripts = new Dictionary<string, Func<IEffectScript>>();
if (config.GameStateType != null)
ParameterLookup = new GameStateParameterLookup(config.GameStateType);
}
Expand Down Expand Up @@ -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<IEffectScript> obj)
{
if (Disposed)
return false;
Expand Down Expand Up @@ -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<IEffectScript> 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;
Expand All @@ -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<IEffectScript> 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -10,6 +10,7 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Aurora.Settings.Overrides;
using Aurora.Utils;

namespace Aurora.Settings.Layers
{
Expand All @@ -20,6 +21,7 @@ public class ScriptLayerHandlerProperties : LayerHandlerProperties<ScriptLayerHa
[JsonIgnore]
public string Script { get { return Logic._Script ?? _Script ?? String.Empty; } }


/*public ScriptSettings _ScriptSettings { get; set; } = null;

[JsonIgnore]
Expand Down Expand Up @@ -47,6 +49,8 @@ public class ScriptLayerHandler : LayerHandler<ScriptLayerHandlerProperties>, IN
{
internal Application profileManager;

public IEffectScript ScriptInstance { get; set; }

public event PropertyChangedEventHandler PropertyChanged;

[JsonIgnore]
Expand All @@ -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[])
Expand All @@ -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]
Expand Down