Skip to content

Commit f615ed8

Browse files
Update referenced assemblies loop, cleanup
1 parent b357957 commit f615ed8

3 files changed

Lines changed: 64 additions & 52 deletions

File tree

src/CSharpPluginLoader.cs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System;
77
using System.Collections.Generic;
88
using System.Linq;
9+
using System.Reflection;
910
using System.Text.RegularExpressions;
1011
using Oxide.CSharp.Common;
1112
using Oxide.Pooling;
@@ -15,14 +16,26 @@ namespace Oxide.Plugins
1516
public class CSharpPluginLoader : PluginLoader
1617
{
1718
public static string[] DefaultReferences = { "mscorlib", "Oxide.Core", "Oxide.CSharp", "Oxide.Common", "System", "System.Core", "System.Data", "System.Xml" };
18-
public static HashSet<string> PluginReferences = new HashSet<string>(DefaultReferences);
19+
public static HashSet<string> PluginReferences = new(DefaultReferences);
1920
public static CSharpPluginLoader Instance;
2021

2122
private static CSharpExtension extension;
2223
private static Dictionary<string, CompilablePlugin> plugins = new Dictionary<string, CompilablePlugin>();
2324
private static readonly string[] AssemblyBlacklist = { "Newtonsoft.Json", "protobuf-net", "websocket-sharp" };
2425
private Core.Libraries.Timer timer { get; } = Interface.Oxide.GetLibrary<Core.Libraries.Timer>();
2526

27+
private readonly List<CompilablePlugin> _compilationQueue = new List<CompilablePlugin>();
28+
private readonly CompilerService _compiler;
29+
30+
public override string FileExtension => ".cs";
31+
32+
public CSharpPluginLoader(CSharpExtension extension)
33+
{
34+
Instance = this;
35+
CSharpPluginLoader.extension = extension;
36+
_compiler = new CompilerService(extension);
37+
}
38+
2639
public static CompilablePlugin GetCompilablePlugin(string directory, string name)
2740
{
2841
string className = Regex.Replace(name, "_", "");
@@ -35,18 +48,6 @@ public static CompilablePlugin GetCompilablePlugin(string directory, string name
3548
return plugin;
3649
}
3750

38-
public override string FileExtension => ".cs";
39-
40-
private readonly List<CompilablePlugin> _compilationQueue = new List<CompilablePlugin>();
41-
private readonly CompilerService _compiler;
42-
43-
public CSharpPluginLoader(CSharpExtension extension)
44-
{
45-
Instance = this;
46-
CSharpPluginLoader.extension = extension;
47-
_compiler = new CompilerService(extension);
48-
}
49-
5051
public void OnModLoaded()
5152
{
5253
_compiler.Precheck();
@@ -59,7 +60,7 @@ public void OnModLoaded()
5960
continue;
6061
}
6162

62-
System.Reflection.Assembly assembly = extension.GetType().Assembly;
63+
Assembly assembly = extension.GetType().Assembly;
6364
string assemblyName = assembly.GetName().Name;
6465

6566
if (AssemblyBlacklist.Contains(assemblyName))
@@ -68,12 +69,18 @@ public void OnModLoaded()
6869
}
6970

7071
PluginReferences.Add(assemblyName);
71-
foreach (System.Reflection.AssemblyName reference in assembly.GetReferencedAssemblies())
72+
73+
AssemblyName[] referencedAssemblies = assembly.GetReferencedAssemblies();
74+
int referencedAssemblyCount = referencedAssemblies.Length;
75+
for (int i = 0; i < referencedAssemblyCount; i++)
7276
{
73-
if (reference != null)
77+
AssemblyName reference = referencedAssemblies[i];
78+
if (reference == null)
7479
{
75-
PluginReferences.Add(reference.Name);
80+
continue;
7681
}
82+
83+
PluginReferences.Add(reference.Name);
7784
}
7885
}
7986
}

src/CompiledAssembly.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ private void ValidateAssembly(Action<byte[], byte[]> callback)
170170

171171
Interface.Oxide.RootLogger.WriteDebug(LogType.Info, LogEvent.Compile, "CSharp",
172172
$"Patching DirectCallMethod on {typeDefinition.Name}");
173+
173174
new DirectCallMethod(assemblyDefinition.MainModule, typeDefinition, baseAssembly);
174175
}
175176
catch (Exception exception)

src/CompilerService.cs

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace Oxide.CSharp
2727
internal class CompilerService
2828
{
2929
private readonly Hash<int, Compilation> _compilations;
30-
private readonly Queue<CompilerMessage> _messageQueue = new Queue<CompilerMessage>();
30+
private readonly Queue<CompilerMessage> _messageQueue = new();
3131
private Process? _compilerProcess;
3232
private volatile int _lastId;
3333
private volatile bool _ready;
@@ -116,22 +116,24 @@ internal bool Precheck()
116116
"OXIDEMOD"
117117
};
118118

119-
Extension? game = Interface.Oxide.GetAllExtensions().SingleOrDefault(e => e.IsGameExtension);
120-
if (game != null)
119+
IEnumerable<Extension> extensions = Interface.Oxide.GetAllExtensions();
120+
121+
Extension? gameExtension = extensions.SingleOrDefault(extension => extension.IsGameExtension);
122+
if (gameExtension != null)
121123
{
122-
string name = game.Name.ToUpperInvariant();
123-
string branch = game.Branch?.ToUpperInvariant() ?? "PUBLIC";
124+
string name = gameExtension.Name.ToUpperInvariant();
125+
string branch = gameExtension.Branch?.ToUpperInvariant() ?? "PUBLIC";
124126
preprocessors.Add(EscapeSymbolName(name));
125127
preprocessors.Add(EscapeSymbolName(name + "_" + branch));
126128

127-
if (game.Version != default)
129+
if (gameExtension.Version != default)
128130
{
129-
preprocessors.Add(EscapeSymbolName(name + "_" + game.Version));
130-
preprocessors.Add(EscapeSymbolName(name + "_" + game.Version + "_" + branch));
131+
preprocessors.Add(EscapeSymbolName(name + "_" + gameExtension.Version));
132+
preprocessors.Add(EscapeSymbolName(name + "_" + gameExtension.Version + "_" + branch));
131133
}
132134
}
133135

134-
foreach (Extension extension in Interface.Oxide.GetAllExtensions())
136+
foreach (Extension extension in extensions)
135137
{
136138
try
137139
{
@@ -212,9 +214,9 @@ private bool Start()
212214
Thread.Sleep(100);
213215
}
214216
}
215-
catch (Exception e)
217+
catch (Exception exception)
216218
{
217-
Log(LogType.Error, e.Message);
219+
Log(LogType.Error, exception.Message);
218220
return false;
219221
}
220222

@@ -243,11 +245,11 @@ private bool Start()
243245
_compilerProcess.Exited += OnCompilerProcessExited;
244246
_compilerProcess.Start();
245247
}
246-
catch (Exception ex)
248+
catch (Exception exception)
247249
{
248250
_compilerProcess?.Dispose();
249251
_compilerProcess = null;
250-
Interface.Oxide.LogException($"Exception while starting compiler", ex);
252+
Interface.Oxide.LogException($"Exception while starting compiler", exception);
251253
if (_filePath.Contains("'"))
252254
{
253255
Interface.Oxide.LogError("Server directory path contains an apostrophe, compiler will not work until path is renamed");
@@ -257,15 +259,15 @@ private bool Start()
257259
Interface.Oxide.LogError("Compiler may not be set as executable; chmod +x or 0744/0755 required");
258260
}
259261

260-
if (ex.GetBaseException() != ex)
262+
Exception baseException = exception.GetBaseException();
263+
if (baseException != exception)
261264
{
262-
Interface.Oxide.LogException("BaseException: ", ex.GetBaseException());
265+
Interface.Oxide.LogException("BaseException: ", baseException);
263266
}
264267

265-
Win32Exception win32 = ex as Win32Exception;
266-
if (win32 != null)
268+
if (exception is Win32Exception win32Exception)
267269
{
268-
Interface.Oxide.LogError($"Win32 NativeErrorCode: {win32.NativeErrorCode} ErrorCode: {win32.ErrorCode} HelpLink: {win32.HelpLink}");
270+
Interface.Oxide.LogError($"Win32 NativeErrorCode: {win32Exception.NativeErrorCode} ErrorCode: {win32Exception.ErrorCode} HelpLink: {win32Exception.HelpLink}");
269271
}
270272
}
271273

@@ -308,16 +310,19 @@ private void OnMessageReceived(CompilerMessage message)
308310

309311
if (message.Errors != null)
310312
{
311-
foreach (CompilerError error in message.Errors)
313+
int errorCount = message.Errors.Count;
314+
for (int i = 0; i < errorCount; i++)
312315
{
316+
CompilerError error = message.Errors[i];
313317
Log(LogType.Error, $"Compiler error for compilation {compilation.name}: {error.Message}");
314318

315319
CompilablePlugin? compilablePlugin =
316320
compilation.plugins.SingleOrDefault(pl => pl.ScriptName == error.File);
317321

318322
if (compilablePlugin == null)
319323
{
320-
Interface.Oxide.LogError($"Unable to resolve script error to {error.File}: {error.Message}");
324+
Interface.Oxide.LogError(
325+
$"Unable to resolve script error to {error.File}: {error.Message}");
321326
continue;
322327
}
323328

@@ -327,9 +332,10 @@ private void OnMessageReceived(CompilerMessage message)
327332
string[] missingRequirementsArray = missingRequirements.ToArray();
328333
if (missingRequirementsArray.Length > 0)
329334
{
330-
compilablePlugin.CompilerErrors.Add($"Missing dependencies: {string.Join(",", missingRequirementsArray)}");
335+
compilablePlugin.CompilerErrors.Add(
336+
$"Missing dependencies: {missingRequirementsArray.JoinValues(',')}");
331337

332-
Log(LogType.Error, $"[{error.File}] Missing dependencies: {string.Join(",", missingRequirementsArray)}");
338+
Log(LogType.Error, $"[{error.File}] Missing dependencies: {missingRequirementsArray.JoinValues(',')}");
333339
}
334340
else
335341
{
@@ -341,7 +347,7 @@ private void OnMessageReceived(CompilerMessage message)
341347
}
342348

343349
CompilationResult? compilationResult = Constants.Serializer.Deserialize<CompilationResult>(message.Data);
344-
if (compilationResult.Data == null || compilationResult.Data.Length == 0)
350+
if (compilationResult?.Data == null || compilationResult.Data.Length == 0)
345351
{
346352
compilation.Completed();
347353
}
@@ -368,9 +374,9 @@ private void OnMessageReceived(CompilerMessage message)
368374
return;
369375
}
370376

371-
foreach (CompilablePlugin p in compilation.plugins)
377+
foreach (CompilablePlugin compilablePlugin in compilation.plugins)
372378
{
373-
p.CompilerErrors.Add(errorMessage);
379+
compilablePlugin.CompilerErrors.Add(errorMessage);
374380
}
375381

376382
compilation.Completed();
@@ -380,6 +386,7 @@ private void OnMessageReceived(CompilerMessage message)
380386
{
381387
string logMessage =
382388
$"Ready signal received from compiler (Startup took: {Math.Round((Interface.Oxide.Now - _startTime) * 1000f)}ms)";
389+
383390
switch (_messageQueue.Count)
384391
{
385392
case 0:
@@ -672,19 +679,19 @@ private static bool SetFilePermissions(string filePath)
672679
Log(LogType.Info, $"{name} is executable");
673680
}
674681
}
675-
catch (Exception ex)
682+
catch (Exception exception)
676683
{
677-
Interface.Oxide.LogException($"Unable to check {name} for executable permission", ex);
684+
Interface.Oxide.LogException($"Unable to check {name} for executable permission", exception);
678685
}
679686
try
680687
{
681688
Syscall.chmod(filePath, FilePermissions.S_IRWXU);
682689
Interface.Oxide.LogInfo($"File permissions set for {name}");
683690
return true;
684691
}
685-
catch (Exception ex)
692+
catch (Exception exception)
686693
{
687-
Interface.Oxide.LogException($"Could not set {filePath} as executable, please set manually", ex);
694+
Interface.Oxide.LogException($"Could not set {filePath} as executable, please set manually", exception);
688695
}
689696

690697
return false;
@@ -716,10 +723,7 @@ private static bool DownloadFile(string url, string path, int retries = 3)
716723
Interface.Oxide.LogInfo($"[CSharp] Downloading {fileName}. . .");
717724
}
718725

719-
byte[] data;
720-
int code;
721-
bool newerFound;
722-
if (!TryDownload(url, retries, ref retry, last, out data, out code, out newerFound, ref md5))
726+
if (!TryDownload(url, retries, ref retry, last, out byte[] data, out int code, out bool newerFound, ref md5))
723727
{
724728
string attemptVerb = retries == 1 ? "attempt" : "attempts";
725729
Interface.Oxide.LogError($"[CSharp] Failed to download {fileName} after {retry} {attemptVerb} with response code '{code}', please manually download it from {url} and save it here {path}");
@@ -750,9 +754,9 @@ private static bool DownloadFile(string url, string path, int retries = 3)
750754

751755
return true;
752756
}
753-
catch (Exception e)
757+
catch (Exception exception)
754758
{
755-
Interface.Oxide.LogException($"Unexpected error occurred while trying to download {fileName}, please manually download it from {url} and save it here {path}", e);
759+
Interface.Oxide.LogException($"Unexpected error occurred while trying to download {fileName}, please manually download it from {url} and save it here {path}", exception);
756760
return false;
757761
}
758762
}

0 commit comments

Comments
 (0)