don't prevent further cs mods from being executed if one of them has a runtime error

This commit is contained in:
EvilFactory
2022-07-25 09:25:06 -03:00
parent 3fc6892571
commit 1b020ff99e
5 changed files with 24 additions and 17 deletions
@@ -17,18 +17,18 @@ namespace Barotrauma
class CsScriptBase : AssemblyLoadContext class CsScriptBase : AssemblyLoadContext
{ {
public const string NET_ONE_TIME_SCRIPT_ASSEMBLY = "NetOneTimeScriptAssembly"; public const string CsOneTimeScriptAssembly = "NetOneTimeScriptAssembly";
public const string NET_SCRIPT_ASSEMBLY = "NetScriptAssembly"; public const string CsScriptAssembly = "NetScriptAssembly";
public static readonly string[] LoadedAssemblyName = { public static readonly string[] LoadedAssemblyName = {
CsScriptBase.NET_SCRIPT_ASSEMBLY, CsScriptBase.CsScriptAssembly,
CsScriptBase.NET_ONE_TIME_SCRIPT_ASSEMBLY CsScriptBase.CsOneTimeScriptAssembly
}; };
public static Dictionary<string, object> Revision = new Dictionary<string, object>() public static Dictionary<string, object> Revision = new Dictionary<string, object>()
{ {
{ NET_SCRIPT_ASSEMBLY, 0}, { CsScriptAssembly, 0},
{ NET_ONE_TIME_SCRIPT_ASSEMBLY, 0} { CsOneTimeScriptAssembly, 0}
}; };
public CSharpParseOptions ParseOptions { get; protected set; } public CSharpParseOptions ParseOptions { get; protected set; }
@@ -12,8 +12,8 @@ namespace Barotrauma {
class CsScriptFilter class CsScriptFilter
{ {
public static readonly string[] LoadedAssemblyName = { public static readonly string[] LoadedAssemblyName = {
CsScriptBase.NET_SCRIPT_ASSEMBLY, CsScriptBase.CsScriptAssembly,
CsScriptBase.NET_ONE_TIME_SCRIPT_ASSEMBLY CsScriptBase.CsOneTimeScriptAssembly
}; };
private static readonly string[] typesPermitted = { private static readonly string[] typesPermitted = {
@@ -143,7 +143,7 @@ namespace Barotrauma
var syntaxTrees = new List<SyntaxTree>(); var syntaxTrees = new List<SyntaxTree>();
if (sources.Count <= 0) throw new Exception("No Cs sources detected"); if (sources.Count <= 0) throw new Exception("No Cs sources detected");
syntaxTrees.Add(AssemblyInfoSyntaxTree(NET_SCRIPT_ASSEMBLY)); syntaxTrees.Add(AssemblyInfoSyntaxTree(CsScriptAssembly));
foreach ((var folder, var src) in sources) foreach ((var folder, var src) in sources)
{ {
try try
@@ -172,7 +172,7 @@ namespace Barotrauma
.WithMetadataImportOptions(MetadataImportOptions.All) .WithMetadataImportOptions(MetadataImportOptions.All)
.WithOptimizationLevel(OptimizationLevel.Release) .WithOptimizationLevel(OptimizationLevel.Release)
.WithAllowUnsafe(false); .WithAllowUnsafe(false);
var compilation = CSharpCompilation.Create(NET_SCRIPT_ASSEMBLY, syntaxTrees, defaultReferences, options); var compilation = CSharpCompilation.Create(CsScriptAssembly, syntaxTrees, defaultReferences, options);
using (var mem = new MemoryStream()) using (var mem = new MemoryStream())
{ {
@@ -181,7 +181,7 @@ namespace Barotrauma
{ {
IEnumerable<Diagnostic> failures = result.Diagnostics.Where(d => d.IsWarningAsError || d.Severity == DiagnosticSeverity.Error); IEnumerable<Diagnostic> failures = result.Diagnostics.Where(d => d.IsWarningAsError || d.Severity == DiagnosticSeverity.Error);
string errStr = "NET MODS NOT LOADED | Mod compilation errors:"; string errStr = "CS MODS NOT LOADED | Compilation errors:";
foreach (Diagnostic diagnostic in failures) foreach (Diagnostic diagnostic in failures)
errStr += $"\n{diagnostic}"; errStr += $"\n{diagnostic}";
LuaCsSetup.PrintCsError(errStr); LuaCsSetup.PrintCsError(errStr);
@@ -199,7 +199,7 @@ namespace Barotrauma
} }
else else
{ {
throw new Exception("Unable to create net mods assembly."); throw new Exception("Unable to create cs mods assembly.");
} }
} }
@@ -56,7 +56,7 @@ namespace Barotrauma
{ {
code = ToOneTimeScript(code); code = ToOneTimeScript(code);
var syntaxTree = SyntaxFactory.ParseSyntaxTree(code, ParseOptions); var syntaxTree = SyntaxFactory.ParseSyntaxTree(code, ParseOptions);
var compilation = CSharpCompilation.Create(NET_ONE_TIME_SCRIPT_ASSEMBLY, new[] { AssemblyInfoSyntaxTree(NET_ONE_TIME_SCRIPT_ASSEMBLY), syntaxTree }, defaultReferences, compileOptions); var compilation = CSharpCompilation.Create(CsOneTimeScriptAssembly, new[] { AssemblyInfoSyntaxTree(CsOneTimeScriptAssembly), syntaxTree }, defaultReferences, compileOptions);
Assembly assembly = null; Assembly assembly = null;
using (var mem = new MemoryStream()) using (var mem = new MemoryStream())
@@ -13,8 +13,8 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading; using System.Threading;
[assembly: InternalsVisibleTo(Barotrauma.CsScriptBase.NET_SCRIPT_ASSEMBLY, AllInternalsVisible = true)] [assembly: InternalsVisibleTo(Barotrauma.CsScriptBase.CsScriptAssembly, AllInternalsVisible = true)]
[assembly: InternalsVisibleTo(Barotrauma.CsScriptBase.NET_ONE_TIME_SCRIPT_ASSEMBLY, AllInternalsVisible = true)] [assembly: InternalsVisibleTo(Barotrauma.CsScriptBase.CsOneTimeScriptAssembly, AllInternalsVisible = true)]
namespace Barotrauma namespace Barotrauma
{ {
class LuaCsSetupConfig class LuaCsSetupConfig
@@ -314,7 +314,7 @@ namespace Barotrauma
public void Stop() public void Stop()
{ {
foreach (var type in AppDomain.CurrentDomain.GetAssemblies().Where(a => a.GetName().Name == CsScriptBase.NET_SCRIPT_ASSEMBLY).SelectMany(assembly => assembly.GetTypes())) foreach (var type in AppDomain.CurrentDomain.GetAssemblies().Where(a => a.GetName().Name == CsScriptBase.CsScriptAssembly).SelectMany(assembly => assembly.GetTypes()))
{ {
UserData.UnregisterType(type, true); UserData.UnregisterType(type, true);
} }
@@ -457,7 +457,14 @@ namespace Barotrauma
var modTypes = CsScriptLoader.Compile(); var modTypes = CsScriptLoader.Compile();
modTypes.ForEach(t => modTypes.ForEach(t =>
{ {
t.GetConstructor(new Type[] { })?.Invoke(null); try
{
t.GetConstructor(new Type[] { })?.Invoke(null);
}
catch (Exception ex)
{
HandleException(ex, exceptionType: ExceptionType.CSharp);
}
}); });
} }
catch (Exception ex) catch (Exception ex)