fix some of the formatting and remove usage of CsScriptFilter as it's pretty much useless

This commit is contained in:
Evil Factory
2022-06-29 12:12:47 -03:00
parent 95de8a721b
commit 47e0351403
5 changed files with 30 additions and 31 deletions
@@ -20,6 +20,11 @@ namespace Barotrauma
public const string NET_ONE_TIME_SCRIPT_ASSEMBLY = "NetOneTimeScriptAssembly"; public const string NET_ONE_TIME_SCRIPT_ASSEMBLY = "NetOneTimeScriptAssembly";
public const string NET_SCRIPT_ASSEMBLY = "NetScriptAssembly"; public const string NET_SCRIPT_ASSEMBLY = "NetScriptAssembly";
public static readonly string[] LoadedAssemblyName = {
CsScriptBase.NET_SCRIPT_ASSEMBLY,
CsScriptBase.NET_ONE_TIME_SCRIPT_ASSEMBLY
};
public static Dictionary<string, object> Revision = new Dictionary<string, object>() public static Dictionary<string, object> Revision = new Dictionary<string, object>()
{ {
{ NET_SCRIPT_ASSEMBLY, 0}, { NET_SCRIPT_ASSEMBLY, 0},
@@ -1,3 +1,5 @@
// unused
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -151,8 +151,6 @@ namespace Barotrauma
foreach (var file in src) foreach (var file in src)
{ {
var tree = SyntaxFactory.ParseSyntaxTree(File.ReadAllText(file), ParseOptions, file); var tree = SyntaxFactory.ParseSyntaxTree(File.ReadAllText(file), ParseOptions, file);
var error = CsScriptFilter.FilterSyntaxTree(tree as CSharpSyntaxTree); // Check file content for prohibited stuff
if (error != null) throw new Exception(error);
syntaxTrees.Add(tree); syntaxTrees.Add(tree);
} }
@@ -166,15 +164,15 @@ namespace Barotrauma
return syntaxTrees; return syntaxTrees;
} }
public List<Type> Compile() public List<Type> Compile()
{ {
IEnumerable<SyntaxTree> syntaxTrees = ParseSources(); IEnumerable<SyntaxTree> syntaxTrees = ParseSources();
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary) var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
.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(NET_SCRIPT_ASSEMBLY, syntaxTrees, defaultReferences, options);
using (var mem = new MemoryStream()) using (var mem = new MemoryStream())
{ {
@@ -191,20 +189,18 @@ namespace Barotrauma
else else
{ {
mem.Seek(0, SeekOrigin.Begin); mem.Seek(0, SeekOrigin.Begin);
var errStr = CsScriptFilter.FilterMetadata(new PEReader(mem).GetMetadataReader()); Assembly = LoadFromStream(mem);
if (errStr == null)
{
mem.Seek(0, SeekOrigin.Begin);
Assembly = LoadFromStream(mem);
}
else LuaCsSetup.PrintCsError(errStr);
} }
} }
if (Assembly != null) if (Assembly != null)
{
return Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(ACsMod))).ToList(); return Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(ACsMod))).ToList();
}
else else
{
throw new Exception("Unable to create net mods assembly."); throw new Exception("Unable to create net mods assembly.");
}
} }
private static string[] DirSearch(string sDir) private static string[] DirSearch(string sDir)
@@ -89,28 +89,22 @@ namespace Barotrauma
else else
{ {
mem.Seek(0, SeekOrigin.Begin); mem.Seek(0, SeekOrigin.Begin);
var errStr = CsScriptFilter.FilterOneTimeMetadata(new PEReader(mem).GetMetadataReader()); assembly = LoadFromStream(mem);
if (errStr == null) var runner = assembly.CreateInstance("NetOneTimeScript.NetOneTimeScriptRunner");
if (runner != null)
{ {
mem.Seek(0, SeekOrigin.Begin); var method = runner.GetType().GetMethod("Run", BindingFlags.Public | BindingFlags.Instance);
assembly = LoadFromStream(mem); if (method != null)
var runner = assembly.CreateInstance("NetOneTimeScript.NetOneTimeScriptRunner"); {
if (runner != null) scriptResilt = method.Invoke(runner, null);
{ foreach (var type in assembly.GetTypes())
var method = runner.GetType().GetMethod("Run", BindingFlags.Public | BindingFlags.Instance); {
if (method != null) UserData.UnregisterType(type, true);
{
scriptResilt = method.Invoke(runner, null);
foreach (var type in assembly.GetTypes())
{
UserData.UnregisterType(type, true);
}
} }
else LuaCsSetup.PrintCsError("Script Error - no run method detected");
} }
else LuaCsSetup.PrintCsError("Script Error - no runner class detected"); else { LuaCsSetup.PrintCsError("Script Error - no run method detected"); }
} }
else LuaCsSetup.PrintCsError(errStr); else { LuaCsSetup.PrintCsError("Script Error - no runner class detected"); }
} }
} }
Unload(); Unload();
@@ -15,7 +15,7 @@ namespace Barotrauma
if (type != null) return type; if (type != null) return type;
foreach (var a in AppDomain.CurrentDomain.GetAssemblies()) foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
{ {
if (CsScriptFilter.LoadedAssemblyName.Contains(a.GetName().Name)) if (CsScriptBase.LoadedAssemblyName.Contains(a.GetName().Name))
{ {
var attrs = a.GetCustomAttributes<AssemblyMetadataAttribute>(); var attrs = a.GetCustomAttributes<AssemblyMetadataAttribute>();
var revision = attrs.FirstOrDefault(attr => attr.Key == "Revision")?.Value; var revision = attrs.FirstOrDefault(attr => attr.Key == "Revision")?.Value;
@@ -23,7 +23,9 @@ namespace Barotrauma
} }
type = a.GetType(typeName); type = a.GetType(typeName);
if (type != null) if (type != null)
{
return type; return type;
}
} }
return null; return null;
} }