From e5d4a38fd7df098c26e7c78128bd0e1017ff29fa Mon Sep 17 00:00:00 2001 From: Evil Factory <36804725+evilfactory@users.noreply.github.com> Date: Tue, 5 Jul 2022 17:47:52 -0300 Subject: [PATCH] minor refactor to CsScriptLoader plus fixed source category regex not working as expected with some paths --- .../SharedSource/LuaCs/Cs/CsScriptLoader.cs | 107 +++++++++--------- .../SharedSource/LuaCs/LuaCsSetup.cs | 2 +- 2 files changed, 54 insertions(+), 55 deletions(-) diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Cs/CsScriptLoader.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Cs/CsScriptLoader.cs index 96218a864..a9d0e9003 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Cs/CsScriptLoader.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Cs/CsScriptLoader.cs @@ -16,16 +16,13 @@ namespace Barotrauma { class CsScriptLoader : CsScriptBase { - private LuaCsSetup setup; private List defaultReferences; private Dictionary> sources; public Assembly Assembly { get; private set; } - public CsScriptLoader(LuaCsSetup setup) + public CsScriptLoader() { - this.setup = setup; - defaultReferences = AppDomain.CurrentDomain.GetAssemblies() .Where(a => !(a.IsDynamic || string.IsNullOrEmpty(a.Location) || a.Location.Contains("xunit"))) .Select(a => MetadataReference.CreateFromFile(a.Location) as MetadataReference) @@ -59,7 +56,10 @@ namespace Barotrauma LuaCsSetup.PrintCsMessage($"Forced run C# of {cp.Name}"); return true; } - else if (rtValue == RunType.None) return false; + else if (rtValue == RunType.None) + { + return false; + } } } @@ -68,75 +68,69 @@ namespace Barotrauma LuaCsSetup.PrintCsMessage($"Assumed run C# of {cp.Name}"); return true; } - else return false; + else + { + return false; + } } + public void SearchFolders() - { + { var paths = new Dictionary(); foreach (var cp in ContentPackageManager.AllPackages) - { - var path = $"{Path.GetFullPath(Path.GetDirectoryName(cp.Path)).Replace('\\','/')}/"; + { + var path = $"{Path.GetFullPath(Path.GetDirectoryName(cp.Path)).Replace('\\', '/')}/"; if (ShouldRun(cp, path)) { if (paths.ContainsKey(cp.Name)) - { - if (ContentPackageManager.EnabledPackages.All.Contains(cp)) paths[cp.Name] = path; + { + if (ContentPackageManager.EnabledPackages.All.Contains(cp)) + { + paths[cp.Name] = path; + } + } + else + { + paths.Add(cp.Name, path); } - else paths.Add(cp.Name, path); } } - foreach ((var _, var path) in paths) RunFolder(path); + + foreach ((var _, var path) in paths) + { + RunFolder(path); + } } public bool HasSources { get => sources.Count > 0; } - private enum SourceCategory { Shared, Server, Client }; - private Regex rMaskPathValid = new Regex(@"^/?(((?!csharp)[^/])+/)+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(); + private void AddSources(string folder) + { foreach (var str in DirSearch(folder)) { - var s = str.Replace("\\", "/"); + string s = str.Replace("\\", "/"); - if (s.EndsWith(".cs") && LuaCsFile.IsPathAllowedCsException(s)) + if (sources.ContainsKey(folder)) { - 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); + sources[folder].Add(s); + } + else + { + sources.Add(folder, new List { 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 void RunFolder(string folder) + { + + AddSources(folder + "/CSharp/Shared"); + +#if SERVER + AddSources(folder + "/CSharp/Server"); +#else + AddSources(folder + "/CSharp/Client"); +#endif } private IEnumerable ParseSources() { @@ -205,6 +199,11 @@ namespace Barotrauma private static string[] DirSearch(string sDir) { + if (!Directory.Exists(sDir)) + { + return new string[] {}; + } + return Directory.GetFiles(sDir, "*.cs", SearchOption.AllDirectories); } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs index 894c69ff2..a07ae053c 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs @@ -444,7 +444,7 @@ namespace Barotrauma DebugConsole.AddWarning("Cs package active! Cs mods are NOT sandboxed, use it at your own risk!"); } - CsScriptLoader = new CsScriptLoader(this); + CsScriptLoader = new CsScriptLoader(); CsScriptLoader.SearchFolders(); if (CsScriptLoader.HasSources) {