diff --git a/Project-Aurora/Profiles/Generic_Application/GenericApplicationProfileManager.cs b/Project-Aurora/Profiles/Generic_Application/GenericApplicationProfileManager.cs index 4b0d12db8..5a4c24d1e 100644 --- a/Project-Aurora/Profiles/Generic_Application/GenericApplicationProfileManager.cs +++ b/Project-Aurora/Profiles/Generic_Application/GenericApplicationProfileManager.cs @@ -51,17 +51,23 @@ public override void LoadProfiles() if (Directory.Exists(profiles_path)) { + this.LoadScripts(profiles_path); + foreach (string profile in Directory.EnumerateFiles(profiles_path, "*.json", SearchOption.TopDirectoryOnly)) { string profile_name = Path.GetFileNameWithoutExtension(profile); + + if (profile_name.Equals("app_info")) + continue; + ProfileSettings profile_settings = LoadProfile(profile); if (profile_settings != null) { + this.InitalizeScriptSettings(profile_settings); + if (profile_name.Equals("default")) Settings = profile_settings; - else if (profile_name.Equals("app_info")) - continue; else { if (!Profiles.ContainsKey(profile_name)) diff --git a/Project-Aurora/Settings/ProfileManager.cs b/Project-Aurora/Settings/ProfileManager.cs index 58cc90f5e..3ff00e6d4 100644 --- a/Project-Aurora/Settings/ProfileManager.cs +++ b/Project-Aurora/Settings/ProfileManager.cs @@ -161,11 +161,7 @@ public void ResetProfile() { Settings = (ProfileSettings)Activator.CreateInstance(SettingsType); - foreach (string id in this.EffectScripts.Keys) - { - if (!Settings.ScriptSettings.ContainsKey(id)) - Settings.ScriptSettings.Add(id, new ScriptSettings(this.EffectScripts[id])); - } + this.InitalizeScriptSettings(Settings, true); ProfileChanged?.Invoke(this, new EventArgs()); } @@ -267,66 +263,88 @@ public virtual void UpdateEffectScripts(Queue layers, IGameState st } } + protected void LoadScripts(string profiles_path) + { + 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); + switch (ext) + { + case ".py": + var scope = Global.PythonEngine.ExecuteFile(script); + dynamic main_type; + if (scope.TryGetVariable("main", out main_type)) + { + dynamic obj = Global.PythonEngine.Operations.CreateInstance(main_type); + if (obj.ID != null) + { + this.RegisterEffect(obj.ID, obj); + } + else + Global.logger.LogLine(string.Format("Script \"{0}\" does not have a public ID string variable", script), Logging_Level.External); + } + else + Global.logger.LogLine(string.Format("Script \"{0}\" does not contain a public 'main' class", script), Logging_Level.External); + + break; + case ".cs": + System.Reflection.Assembly script_assembly = CSScript.LoadCodeFrom(script); + foreach (Type typ in script_assembly.ExportedTypes) + { + dynamic obj = Activator.CreateInstance(typ); + if (obj.ID != null) + { + this.RegisterEffect(obj.ID, obj); + } + else + Global.logger.LogLine(string.Format("Script \"{0}\" does not have a public ID string variable for the effect {1}", script, typ.FullName), Logging_Level.External); + } + + break; + default: + Global.logger.LogLine(string.Format("Script with path {0} has an unsupported type/ext! ({1})", script, ext), Logging_Level.External); + break; + } + } + catch (Exception exc) + { + Global.logger.LogLine(string.Format("An error occured while trying to load script {0}. Exception: {1}", script, exc, Logging_Level.External)); + //Maybe MessageBox info dialog could be included. + } + } + } + + protected void InitalizeScriptSettings(ProfileSettings profile_settings, bool ignore_removal = false) + { + foreach (string id in this.EffectScripts.Keys) + { + if (!profile_settings.ScriptSettings.ContainsKey(id)) + profile_settings.ScriptSettings.Add(id, new ScriptSettings(this.EffectScripts[id])); + } + + + if (!ignore_removal) + { + foreach (string key in profile_settings.ScriptSettings.Keys.Where(s => !this.EffectScripts.ContainsKey(s)).ToList()) + { + profile_settings.ScriptSettings.Remove(key); + } + } + } + public virtual void LoadProfiles() { string profiles_path = GetProfileFolderPath(); if (Directory.Exists(profiles_path)) - { - 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); - switch (ext) - { - case ".py": - var scope = Global.PythonEngine.ExecuteFile(script); - dynamic main_type; - if (scope.TryGetVariable("main", out main_type)) - { - dynamic obj = Global.PythonEngine.Operations.CreateInstance(main_type); - if (obj.ID != null) - { - this.RegisterEffect(obj.ID, obj); - } - else - Global.logger.LogLine(string.Format("Script \"{0}\" does not have a public ID string variable", script), Logging_Level.External); - } - else - Global.logger.LogLine(string.Format("Script \"{0}\" does not contain a public 'main' class", script), Logging_Level.External); - - break; - case ".cs": - System.Reflection.Assembly script_assembly = CSScript.LoadCodeFrom(script); - foreach (Type typ in script_assembly.ExportedTypes) - { - dynamic obj = Activator.CreateInstance(typ); - if (obj.ID != null) - { - this.RegisterEffect(obj.ID, obj); - } - else - Global.logger.LogLine(string.Format("Script \"{0}\" does not have a public ID string variable for the effect {1}", script, typ.FullName), Logging_Level.External); - } - - break; - default: - Global.logger.LogLine(string.Format("Script with path {0} has an unsupported type/ext! ({1})", script, ext), Logging_Level.External); - break; - } - } - catch (Exception exc) - { - Global.logger.LogLine(string.Format("An error occured while trying to load script {0}. Exception: {1}", script, exc, Logging_Level.External)); - //Maybe MessageBox info dialog could be included. - } - } - + { + this.LoadScripts(profiles_path); foreach (string profile in Directory.EnumerateFiles(profiles_path, "*.json", SearchOption.TopDirectoryOnly)) { @@ -335,16 +353,7 @@ public virtual void LoadProfiles() if (profile_settings != null) { - foreach (string id in this.EffectScripts.Keys) - { - if (!profile_settings.ScriptSettings.ContainsKey(id)) - profile_settings.ScriptSettings.Add(id, new ScriptSettings(this.EffectScripts[id])); - } - - foreach (string key in profile_settings.ScriptSettings.Keys.Where(s => !this.EffectScripts.ContainsKey(s)).ToList()) - { - profile_settings.ScriptSettings.Remove(key); - } + this.InitalizeScriptSettings(profile_settings); if (profile_name.Equals("default")) Settings = profile_settings;