Merge pull request #72 from oiltanker/master

Cs source folder filter
This commit is contained in:
Evil Factory
2022-04-28 13:33:00 -03:00
committed by GitHub
3 changed files with 45 additions and 10 deletions
@@ -22,11 +22,11 @@ namespace Barotrauma
public bool IsDisposed { get; private set; } public bool IsDisposed { get; private set; }
/// Mod initialization
public ACsMod() public ACsMod()
{ {
IsDisposed = false; IsDisposed = false;
LoadedMods.Add(this); LoadedMods.Add(this);
Start();
} }
public void Dispose() { public void Dispose() {
@@ -35,10 +35,6 @@ namespace Barotrauma
IsDisposed = true; IsDisposed = true;
} }
// TODO: some hooks
/// Mod initialization
public abstract void Start();
/// Error or client exit /// Error or client exit
public abstract void Stop(); public abstract void Stop();
} }
@@ -9,6 +9,7 @@ using Microsoft.CodeAnalysis;
using System.Runtime.Loader; using System.Runtime.Loader;
using System.Reflection.PortableExecutable; using System.Reflection.PortableExecutable;
using System.Reflection.Metadata; using System.Reflection.Metadata;
using System.Text.RegularExpressions;
namespace Barotrauma namespace Barotrauma
{ {
@@ -44,18 +45,53 @@ namespace Barotrauma
public bool HasSources { get => sources.Count > 0; } public bool HasSources { get => sources.Count > 0; }
private enum SourceCategory { Shared, Server, Client };
private Regex rMaskPathValid = new Regex(@"^[^/]+/[^/]+/csharp(/(shared|client|server))?(/[^/]+)+\.cs$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private Regex rMaskPathCategory1 = new Regex(@"/(shared|client|server)(/[^/]+)+\.cs$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private Regex rMaskPathCategory2 = new Regex(@"^/(shared|client|server)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private void RunFolder(string folder) private void RunFolder(string folder)
{ {
var offendingSources = new List<string>();
foreach (var str in DirSearch(folder)) foreach (var str in DirSearch(folder))
{ {
var s = str.Replace("\\", "/"); var s = str.Replace("\\", "/");
if (s.EndsWith(".cs") && LuaCsFile.IsPathAllowedCsException(s)) if (s.EndsWith(".cs") && LuaCsFile.IsPathAllowedCsException(s))
{ {
if (sources.ContainsKey(folder)) sources[folder].Add(s); if (rMaskPathValid.IsMatch(s)) // valid path
else sources.Add(folder, new List<string> { s }); {
var sourceCategory = SourceCategory.Shared;
{
var match = rMaskPathCategory1.Match(s);
if (match.Success) match = rMaskPathCategory2.Match(match.Value);
if (match.Success)
{
if (match.Value.EndsWith("shared", StringComparison.OrdinalIgnoreCase)) sourceCategory = SourceCategory.Shared;
else if (match.Value.EndsWith("server", StringComparison.OrdinalIgnoreCase)) sourceCategory = SourceCategory.Server;
else if (match.Value.EndsWith("client", StringComparison.OrdinalIgnoreCase)) sourceCategory = SourceCategory.Client;
}
}
var belongsInAssembly = false;
{
if (sourceCategory == SourceCategory.Shared) belongsInAssembly = true;
else if (sourceCategory == SourceCategory.Server && LuaCsSetup.IsServer) belongsInAssembly = true;
else if (sourceCategory == SourceCategory.Client && LuaCsSetup.IsClient) belongsInAssembly = true;
}
if (belongsInAssembly)
{
if (sources.ContainsKey(folder)) sources[folder].Add(s);
else sources.Add(folder, new List<string> { s });
}
}
else offendingSources.Add(s);
} }
} }
if (offendingSources.Count > 0)
{
LuaCsSetup.PrintCsError($"All C# sources must belong to <mod_folder>/CSharp/*\n Offending sources:{offendingSources.Select(s => $"\n {s}").Aggregate((s1, s2) => $"{s1}{s2}")}");
}
} }
private IEnumerable<SyntaxTree> ParseSources() { private IEnumerable<SyntaxTree> ParseSources() {
@@ -138,9 +138,12 @@ namespace Barotrauma
} }
else else
{ {
if (exceptionType == ExceptionType.Lua) PrintError(ex); string msg = ex.StackTrace != null
else if (exceptionType == ExceptionType.CSharp) PrintCsError(ex); ? ex.ToString()
else PrintBothError(ex); : $"{ex}\n{Environment.StackTrace}";
if (exceptionType == ExceptionType.Lua) PrintError(msg);
else if (exceptionType == ExceptionType.CSharp) PrintCsError(msg);
else PrintBothError(msg);
} }
} }