Skip to content

Commit

Permalink
- Fixed runconfig setup not properly parsing xml dependency data.
Browse files Browse the repository at this point in the history
-  Fixed error caused by plugin execution not being caught on a per plugin basis.
  • Loading branch information
TBN-MapleWheels committed Sep 5, 2023
1 parent 42d4e22 commit 8af927d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ public AssemblyLoadingSuccessState LoadAssemblyPackages()
}

successState = _assemblyManager.LoadAssembliesFromLocations(pair.Value.AssembliesFilePaths, ref id);
// error

// error handling
if (successState is not AssemblyLoadingSuccessState.Success)
{
ModUtils.Logging.PrintError($"{nameof(CsPackageManager)}: Unable to load the binary assemblies for package {pair.Key.Name}. Error: {successState.ToString()}");
Expand Down Expand Up @@ -482,15 +483,8 @@ bool ShouldRunPackage(ContentPackage package, RunConfig config)
{
if (config.AutoGenerated)
return false;
#if CLIENT
return (!_luaCsSetup.Config.TreatForcedModsAsNormal && config.Client.Trim().ToLowerInvariant().Contains("forced"))
|| (config.Client.Trim().ToLowerInvariant().Contains("standard") &&
ContentPackageManager.EnabledPackages.All.Contains(package));
#elif SERVER
return (!_luaCsSetup.Config.TreatForcedModsAsNormal && config.Server.Trim().ToLowerInvariant().Contains("forced"))
|| (config.Server.Trim().ToLowerInvariant().Contains("standard") &&
ContentPackageManager.EnabledPackages.All.Contains(package));
#endif
return (!_luaCsSetup.Config.TreatForcedModsAsNormal && config.IsForced())
|| (ContentPackageManager.EnabledPackages.All.Contains(package) && config.IsForcedOrStandard());
}

void UpdatePackagesToDisable(ref HashSet<ContentPackage> list,
Expand Down Expand Up @@ -521,8 +515,6 @@ public static bool GetOrCreateRunConfig(ContentPackage package, out RunConfig co
config = new RunConfig(true).Sanitize();
return false;
}

ModUtils.Logging.PrintMessage($"Package has RunConfig. Package={package.Name} | Path={path}");
return ModUtils.IO.LoadOrCreateTypeXml(out config, path, () => new RunConfig(true).Sanitize(), false);
}

Expand All @@ -532,17 +524,11 @@ public static bool GetOrCreateRunConfig(ContentPackage package, out RunConfig co

private void AssemblyManagerOnAssemblyUnloading(Assembly assembly)
{
#if DEBUG
ModUtils.Logging.PrintMessage($"ReflectUtils: Removing assembly {assembly}");
#endif
ReflectionUtils.RemoveAssemblyFromCache(assembly);
}

private void AssemblyManagerOnAssemblyLoaded(Assembly assembly)
{
#if DEBUG
ModUtils.Logging.PrintMessage($"ReflectUtils: Adding assembly {assembly}");
#endif
ReflectionUtils.AddNonAbstractAssemblyTypes(assembly);
}

Expand Down Expand Up @@ -625,7 +611,17 @@ private void LoadPlugins(bool force = false)
_loadedPlugins.Add(pair.Key, new());
else if (_loadedPlugins[pair.Key] is null)
_loadedPlugins[pair.Key] = new();
_loadedPlugins[pair.Key].Add((IAssemblyPlugin)Activator.CreateInstance(type));
try
{
_loadedPlugins[pair.Key].Add((IAssemblyPlugin)Activator.CreateInstance(type));
}
catch (Exception e)
{
ModUtils.Logging.PrintError($"{nameof(CsPackageManager)}: Error while instantiating plugin of type {type}");
#if DEBUG
ModUtils.Logging.PrintError($"{nameof(CsPackageManager)}: Details: {e.Message} | {e.InnerException}");
#endif
}
}
}

Expand All @@ -635,13 +631,33 @@ private void LoadPlugins(bool force = false)
// init
foreach (var plugin in contentPlugins.Value)
{
plugin.Initialize();
try
{
plugin.Initialize();
}
catch (Exception e)
{
ModUtils.Logging.PrintError($"{nameof(CsPackageManager)}: Error while running Initialize() on plugin of type {plugin.GetType()}");
#if DEBUG
ModUtils.Logging.PrintError($"{nameof(CsPackageManager)}: Details: {e.Message} | {e.InnerException}");
#endif
}
}

// post-init
foreach (var plugin in contentPlugins.Value)
{
plugin.OnLoadCompleted();
try
{
plugin.OnLoadCompleted();
}
catch (Exception e)
{
ModUtils.Logging.PrintError($"{nameof(CsPackageManager)}: Error while running OnLoadCompleted() on plugin of type {plugin.GetType()}");
#if DEBUG
ModUtils.Logging.PrintError($"{nameof(CsPackageManager)}: Details: {e.Message} | {e.InnerException}");
#endif
}
}
}

Expand All @@ -654,7 +670,17 @@ private void UnloadPlugins()
{
foreach (var plugin in contentPlugins.Value)
{
plugin.Dispose();
try
{
plugin.Dispose();
}
catch (Exception e)
{
ModUtils.Logging.PrintError($"{nameof(CsPackageManager)}: Error while running Dispose() on plugin of type {plugin.GetType()}");
#if DEBUG
ModUtils.Logging.PrintError($"{nameof(CsPackageManager)}: Details: {e.Message} | {e.InnerException}");
#endif
}
}
contentPlugins.Value.Clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@ public sealed class RunConfig
/// <summary>
/// How should scripts be run on the server.
/// </summary>
[XmlElement(ElementName = "Server")]
public string Server { get; set; }

[XmlElement(ElementName = "Server")] public string Server;

/// <summary>
/// How should scripts be run on the client.
/// </summary>
[XmlElement(ElementName = "Client")]
public string Client { get; set; }
[XmlElement(ElementName = "Client")] public string Client;

/// <summary>
/// List of dependencies by either Steam Workshop ID or by Partial Inclusive Name (ie. "ModDep" will match a mod named "A ModDependency").
/// PIN Dependency checks if ContentPackage names contains the dependency string.
/// </summary>
[XmlArrayItem(ElementName = "Dependencies", IsNullable = true, Type = typeof(Dependency))]
[XmlArrayItem(ElementName = "Dependency", IsNullable = true, Type = typeof(Dependency))]
[XmlArray]
public Dependency[] Dependencies { get; set; }

Expand All @@ -47,13 +45,13 @@ public sealed class Dependency
/// Steam Workshop ID of the dependency.
/// </summary>
[XmlElement(ElementName = "SteamWorkshopId")]
public ulong SteamWorkshopId { get; set; }
public ulong SteamWorkshopId;

/// <summary>
/// Package Name of the dependency. Not needed if SteamWorkshopId is set.
/// </summary>
[XmlElement(ElementName = "PackageName")]
public string PackageName { get; set; }
public string PackageName;
}

public RunConfig Sanitize()
Expand Down Expand Up @@ -83,10 +81,31 @@ static string SanitizeRunSetting(string str) =>
{
null => "None",
"" => "None",
" " => "None",
_ => str[0].ToString().ToUpper() + str.Substring(1).ToLower()
};

return this;
}

public bool IsForced()
{
#if CLIENT
return this.Client.Equals("Forced");
#elif SERVER
return this.Server.Equals("Forced");
#endif
}

public bool IsStandard()
{
#if CLIENT
return this.Client.Equals("Standard");
#elif SERVER
return this.Server.Equals("Standard");
#endif
}

public bool IsForcedOrStandard() => this.IsForced() || this.IsStandard();

}

0 comments on commit 8af927d

Please sign in to comment.