From e5215fb976ddc7cb8e5e2f7eb580624a9c3a2942 Mon Sep 17 00:00:00 2001 From: Oiltanker Date: Thu, 28 Apr 2022 17:08:02 +0300 Subject: [PATCH] Cs source folder filter --- .../SharedSource/LuaCs/Cs/ACsMod.cs | 6 +-- .../SharedSource/LuaCs/Cs/CsScriptLoader.cs | 40 ++++++++++++++++++- .../SharedSource/LuaCs/LuaCsSetup.cs | 9 +++-- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Cs/ACsMod.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Cs/ACsMod.cs index d7d31125c..29c4caf1c 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Cs/ACsMod.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Cs/ACsMod.cs @@ -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(); } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Cs/CsScriptLoader.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Cs/CsScriptLoader.cs index 090926124..33c017f23 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Cs/CsScriptLoader.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Cs/CsScriptLoader.cs @@ -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(); 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 { 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 { s }); + } + } + else offendingSources.Add(s); } } + if (offendingSources.Count > 0) + { + LuaCsSetup.PrintCsError($"All C# sources must belong to /CSharp/*\n Offending sources:{offendingSources.Select(s => $"\n {s}").Aggregate((s1, s2) => $"{s1}{s2}")}"); + } } private IEnumerable ParseSources() { diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs index 5dd5f3fa1..13f0e1978 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs @@ -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); } }