protected ProfileSettings CloneSettings(ProfileSettings settings) { return((ProfileSettings)JsonConvert.DeserializeObject( JsonConvert.SerializeObject(settings, SettingsType, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }), SettingsType, new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace, TypeNameHandling = TypeNameHandling.All } )); //I know this is bad. You can laugh at me for this one. :( }
public ProfileManager(string name, string internal_name, string[] process_names, ProfileSettings settings, LightEvent game_event) { Name = name; InternalName = internal_name; ProcessNames = process_names; Icon = null; Control = null; Settings = settings; Event = game_event; Profiles = new Dictionary <string, ProfileSettings>(); EffectScripts = new Dictionary <string, dynamic>(); LoadProfiles(); }
internal virtual void SaveProfile(string path, ProfileSettings profile) { try { string content = JsonConvert.SerializeObject(profile, Formatting.Indented); Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path)); File.WriteAllText(path, content, Encoding.UTF8); } catch (Exception exc) { Global.logger.LogLine(string.Format("Exception Saving Profile: {0}, Exception: {1}", path, exc), Logging_Level.Error); } }
internal ProfileSettings LoadProfile(string path) { try { if (File.Exists(path)) { string profile_content = File.ReadAllText(path, Encoding.UTF8); if (!String.IsNullOrWhiteSpace(profile_content)) { ProfileSettings prof = (ProfileSettings)JsonConvert.DeserializeObject(profile_content, SettingsType, new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace, TypeNameHandling = TypeNameHandling.All }); foreach (Layer lyr in prof.Layers) { lyr.AnythingChanged += this.SaveProfilesEvent; } prof.Layers.CollectionChanged += (s, e) => { if (e.NewItems != null) { foreach (Layer lyr in e.NewItems) { if (lyr == null) { continue; } lyr.AnythingChanged += this.SaveProfilesEvent; } } this.SaveProfiles(); }; return(prof); } } } catch (Exception exc) { Global.logger.LogLine(string.Format("Exception Loading Profile: {0}, Exception: {1}", path, exc), Logging_Level.Error); } return(null); }
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)) { this.LoadScripts(profiles_path); foreach (string profile in Directory.EnumerateFiles(profiles_path, "*.json", SearchOption.TopDirectoryOnly)) { string profile_name = Path.GetFileNameWithoutExtension(profile); ProfileSettings profile_settings = LoadProfile(profile); if (profile_settings != null) { this.InitalizeScriptSettings(profile_settings); if (profile_name.Equals("default")) { Settings = profile_settings; } else { if (!Profiles.ContainsKey(profile_name)) { Profiles.Add(profile_name, profile_settings); } } } } } else { Global.logger.LogLine(string.Format("Profiles directory for {0} does not exist.", Name), Logging_Level.Info, false); } }
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. } } foreach (string profile in Directory.EnumerateFiles(profiles_path, "*.json", SearchOption.TopDirectoryOnly)) { string profile_name = Path.GetFileNameWithoutExtension(profile); ProfileSettings profile_settings = LoadProfile(profile); 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); } if (profile_name.Equals("default")) { Settings = profile_settings; } else { if (!Profiles.ContainsKey(profile_name)) { Profiles.Add(profile_name, profile_settings); } } } } } else { Global.logger.LogLine(string.Format("Profiles directory for {0} does not exist.", Name), Logging_Level.Info, false); } }