Ejemplo n.º 1
0
        public bool RegisterEffect(string key, IEffectScript obj)
        {
            if (this.EffectScripts.ContainsKey(key))
            {
                Global.logger.LogLine(string.Format("Effect script with key {0} already exists!", key), Logging_Level.External);
                return(false);
            }

            this.EffectScripts.Add(key, obj);

            return(true);
        }
Ejemplo n.º 2
0
        public bool RegisterEffect(string key, IEffectScript obj)
        {
            if (Disposed)
            {
                return(false);
            }

            if (this.EffectScripts.ContainsKey(key))
            {
                Global.logger.Warn(string.Format("Effect script with key {0} already exists!", key));
                return(false);
            }

            this.EffectScripts.Add(key, obj);

            return(true);
        }
Ejemplo n.º 3
0
        public override EffectLayer Render(IGameState gamestate)
        {
            EffectLayer layer = null;

            if (this.IsScriptValid)
            {
                try
                {
                    IEffectScript script        = this.profileManager.EffectScripts[this.Properties.Script];
                    object        script_layers = script.UpdateLights(Properties.ScriptProperties, gamestate);
                    if (script_layers is EffectLayer)
                    {
                        layer = (EffectLayer)script_layers;
                    }
                    else if (script_layers is EffectLayer[])
                    {
                        EffectLayer[] layers = (EffectLayer[])script_layers;
                        layer = layers.First();
                        for (int i = 1; i < layers.Length; i++)
                        {
                            layer = layer + layers[i];
                        }
                        //foreach (var layer in (script_layers as EffectLayer[]))
                        //  layers.Enqueue(layer);
                    }
                    ScriptException = null;
                }
                catch (Exception exc)
                {
                    Global.logger.Error("Effect script with key {0} encountered an error. Exception: {1}", this.Properties.Script, exc);
                    ScriptException = exc;
                }
            }

            return(layer ?? new EffectLayer());
        }
Ejemplo n.º 4
0
        protected void LoadScripts(string profiles_path, bool force = false)
        {
            if (!force && ScriptsLoaded)
            {
                return;
            }

            this.EffectScripts.Clear();

            string scripts_path = Path.Combine(profiles_path, Global.ScriptDirectory);

            if (!Directory.Exists(scripts_path))
            {
                Directory.CreateDirectory(scripts_path);
            }

            foreach (string script in Directory.EnumerateFiles(scripts_path, "*.*"))
            {
                try
                {
                    string ext       = Path.GetExtension(script);
                    bool   anyLoaded = false;
                    switch (ext)
                    {
                    case ".py":
                        var scope = Global.PythonEngine.ExecuteFile(script);
                        foreach (var v in scope.GetItems())
                        {
                            if (v.Value is IronPython.Runtime.Types.PythonType)
                            {
                                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)
                                    {
                                        if (!(obj.ID != null && this.RegisterEffect(obj.ID, obj)))
                                        {
                                            Global.logger.Warn($"Script \"{script}\" must have a unique string ID variable for the effect {v.Key}");
                                        }
                                        else
                                        {
                                            anyLoaded = true;
                                        }
                                    }
                                    else
                                    {
                                        Global.logger.Error($"Could not create instance of Effect Script: {v.Key} in script: \"{script}\"");
                                    }
                                }
                            }
                        }


                        break;

                    case ".cs":
                        Assembly script_assembly = CSScript.LoadFile(script);
                        Type     effectType      = typeof(IEffectScript);
                        foreach (Type typ in script_assembly.ExportedTypes)
                        {
                            if (effectType.IsAssignableFrom(typ))
                            {
                                IEffectScript obj = (IEffectScript)Activator.CreateInstance(typ);
                                if (!(obj.ID != null && this.RegisterEffect(obj.ID, obj)))
                                {
                                    Global.logger.Warn(string.Format("Script \"{0}\" must have a unique string ID variable for the effect {1}", script, typ.FullName));
                                }
                                else
                                {
                                    anyLoaded = true;
                                }
                            }
                        }

                        break;

                    default:
                        Global.logger.Warn(string.Format("Script with path {0} has an unsupported type/ext! ({1})", script, ext));
                        continue;
                    }

                    if (!anyLoaded)
                    {
                        Global.logger.Warn($"Script \"{script}\": No compatible effects found. Does this script need to be updated?");
                    }
                }
                catch (Exception exc)
                {
                    Global.logger.Error(string.Format("An error occured while trying to load script {0}. Exception: {1}", script, exc));
                    //Maybe MessageBox info dialog could be included.
                }
            }
        }