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

View File

@@ -17,18 +17,18 @@ namespace Barotrauma
class CsScriptBase : AssemblyLoadContext
{
public const string NET_ONE_TIME_SCRIPT_ASSEMBLY = "NetOneTimeScriptAssembly";
public const string NET_SCRIPT_ASSEMBLY = "NetScriptAssembly";
public const string CsOneTimeScriptAssembly = "NetOneTimeScriptAssembly";
public const string CsScriptAssembly = "NetScriptAssembly";
public static readonly string[] LoadedAssemblyName = {
CsScriptBase.NET_SCRIPT_ASSEMBLY,
CsScriptBase.NET_ONE_TIME_SCRIPT_ASSEMBLY
CsScriptBase.CsScriptAssembly,
CsScriptBase.CsOneTimeScriptAssembly
};
public static Dictionary<string, object> Revision = new Dictionary<string, object>()
{
{ NET_SCRIPT_ASSEMBLY, 0},
{ NET_ONE_TIME_SCRIPT_ASSEMBLY, 0}
{ CsScriptAssembly, 0},
{ CsOneTimeScriptAssembly, 0}
};
public CSharpParseOptions ParseOptions { get; protected set; }

View File

@@ -12,8 +12,8 @@ namespace Barotrauma {
class CsScriptFilter
{
public static readonly string[] LoadedAssemblyName = {
CsScriptBase.NET_SCRIPT_ASSEMBLY,
CsScriptBase.NET_ONE_TIME_SCRIPT_ASSEMBLY
CsScriptBase.CsScriptAssembly,
CsScriptBase.CsOneTimeScriptAssembly
};
private static readonly string[] typesPermitted = {

View File

@@ -143,7 +143,7 @@ namespace Barotrauma
var syntaxTrees = new List<SyntaxTree>();
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)
{
try
@@ -172,7 +172,7 @@ namespace Barotrauma
.WithMetadataImportOptions(MetadataImportOptions.All)
.WithOptimizationLevel(OptimizationLevel.Release)
.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())
{
@@ -181,7 +181,7 @@ namespace Barotrauma
{
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)
errStr += $"\n{diagnostic}";
LuaCsSetup.PrintCsError(errStr);
@@ -199,7 +199,7 @@ namespace Barotrauma
}
else
{
throw new Exception("Unable to create net mods assembly.");
throw new Exception("Unable to create cs mods assembly.");
}
}

View File

@@ -56,7 +56,7 @@ namespace Barotrauma
{
code = ToOneTimeScript(code);
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;
using (var mem = new MemoryStream())

View File

@@ -13,8 +13,8 @@ using System.Linq;
using System.Reflection;
using System.Threading;
[assembly: InternalsVisibleTo(Barotrauma.CsScriptBase.NET_SCRIPT_ASSEMBLY, AllInternalsVisible = true)]
[assembly: InternalsVisibleTo(Barotrauma.CsScriptBase.NET_ONE_TIME_SCRIPT_ASSEMBLY, AllInternalsVisible = true)]
[assembly: InternalsVisibleTo(Barotrauma.CsScriptBase.CsScriptAssembly, AllInternalsVisible = true)]
[assembly: InternalsVisibleTo(Barotrauma.CsScriptBase.CsOneTimeScriptAssembly, AllInternalsVisible = true)]
namespace Barotrauma
{
class LuaCsSetupConfig
@@ -314,7 +314,7 @@ namespace Barotrauma
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);
}
@@ -457,7 +457,14 @@ namespace Barotrauma
var modTypes = CsScriptLoader.Compile();
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)