Cs source folder filter

This commit is contained in:
Oiltanker
2022-04-28 17:08:02 +03:00
parent cb50c69fc6
commit e5215fb976
3 changed files with 45 additions and 10 deletions

View File

@@ -22,11 +22,11 @@ namespace Barotrauma
public bool IsDisposed { get; private set; }
/// Mod initialization
public ACsMod()
{
IsDisposed = false;
LoadedMods.Add(this);
Start();
}
public void Dispose() {
@@ -35,10 +35,6 @@ namespace Barotrauma
IsDisposed = true;
}
// TODO: some hooks
/// Mod initialization
public abstract void Start();
/// Error or client exit
public abstract void Stop();
}

View File

@@ -9,6 +9,7 @@ using Microsoft.CodeAnalysis;
using System.Runtime.Loader;
using System.Reflection.PortableExecutable;
using System.Reflection.Metadata;
using System.Text.RegularExpressions;
namespace Barotrauma
{
@@ -44,18 +45,53 @@ namespace Barotrauma
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)
{
var offendingSources = new List<string>();
foreach (var str in DirSearch(folder))
{
var s = str.Replace("\\", "/");
if (s.EndsWith(".cs") && LuaCsFile.IsPathAllowedCsException(s))
{
if (sources.ContainsKey(folder)) sources[folder].Add(s);
else sources.Add(folder, new List<string> { s });
if (rMaskPathValid.IsMatch(s)) // valid path
{
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() {

View File

@@ -138,9 +138,12 @@ namespace Barotrauma
}
else
{
if (exceptionType == ExceptionType.Lua) PrintError(ex);
else if (exceptionType == ExceptionType.CSharp) PrintCsError(ex);
else PrintBothError(ex);
string msg = ex.StackTrace != null
? ex.ToString()
: $"{ex}\n{Environment.StackTrace}";
if (exceptionType == ExceptionType.Lua) PrintError(msg);
else if (exceptionType == ExceptionType.CSharp) PrintCsError(msg);
else PrintBothError(msg);
}
}