Skip to content

Commit

Permalink
Added support for Custom Application Profiles to use Scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
GREAT_BIG_BUSHY_BEARD committed Jan 9, 2017
1 parent f61b5e5 commit 5a62bab
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
149 changes: 79 additions & 70 deletions Project-Aurora/Settings/ProfileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -267,66 +263,88 @@ public virtual void UpdateEffectScripts(Queue<EffectLayer> 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))
{
Expand All @@ -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;
Expand Down

0 comments on commit 5a62bab

Please sign in to comment.