Skip to content

Commit 1447296

Browse files
committed
Add Roslyn native preprocessor directive support
1 parent f8ae6e6 commit 1447296

3 files changed

Lines changed: 54 additions & 38 deletions

File tree

src/Compilation.cs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.IO;
88
using System.Linq;
99
using System.Reflection;
10-
using System.Runtime.InteropServices.ComTypes;
1110
using System.Text.RegularExpressions;
1211
using System.Threading;
1312

@@ -16,7 +15,6 @@ namespace Oxide.Plugins
1615
internal class Compilation
1716
{
1817
public static Compilation Current;
19-
private static readonly Regex SymbolEscapeRegex = new Regex(@"[^\w\d]", RegexOptions.Compiled);
2018

2119
internal int id;
2220
internal string name;
@@ -32,9 +30,6 @@ internal class Compilation
3230

3331
private string includePath;
3432
private string[] extensionNames;
35-
private string gameExtensionNamespace;
36-
private readonly string gameExtensionName;
37-
private readonly string gameExtensionBranch;
3833

3934
internal Compilation(int id, Action<Compilation> callback, CompilablePlugin[] plugins)
4035
{
@@ -55,10 +50,6 @@ internal Compilation(int id, Action<Compilation> callback, CompilablePlugin[] pl
5550

5651
includePath = Path.Combine(Interface.Oxide.PluginDirectory, "include");
5752
extensionNames = Interface.Oxide.GetAllExtensions().Select(ext => ext.Name).ToArray();
58-
Core.Extensions.Extension gameExtension = Interface.Oxide.GetAllExtensions().SingleOrDefault(ext => ext.IsGameExtension);
59-
gameExtensionName = gameExtension?.Name.ToUpper();
60-
gameExtensionNamespace = gameExtension?.GetType().Namespace;
61-
gameExtensionBranch = gameExtension?.Branch?.ToUpper();
6253
}
6354

6455
internal void Started()
@@ -488,18 +479,6 @@ private bool CacheScriptLines(CompilablePlugin plugin)
488479
lines.Add(reader.ReadLine());
489480
}
490481

491-
if (!string.IsNullOrEmpty(gameExtensionName))
492-
{
493-
string gameExtensionNameEscaped = EscapeSymbolName(gameExtensionName);
494-
lines.Insert(0, $"#define {gameExtensionNameEscaped}");
495-
496-
if (!string.IsNullOrEmpty(gameExtensionBranch) && gameExtensionBranch != "public")
497-
{
498-
string gameExtensionBranchEscaped = EscapeSymbolName(gameExtensionBranch);
499-
lines.Insert(0, $"#define {gameExtensionNameEscaped}{gameExtensionBranchEscaped}");
500-
}
501-
}
502-
503482
plugin.ScriptLines = lines.ToArray();
504483
plugin.ScriptEncoding = reader.CurrentEncoding;
505484
}
@@ -560,15 +539,5 @@ private void RemovePlugin(CompilablePlugin plugin)
560539
}
561540
}
562541
}
563-
564-
/// <summary>
565-
/// This allows to handle cases where injected symbols have inappropriate characters (e.g. git branches can have "/", "-" etc. while #define disallows them)
566-
/// </summary>
567-
/// <param name="name"></param>
568-
/// <returns></returns>
569-
private string EscapeSymbolName(string name)
570-
{
571-
return SymbolEscapeRegex.Replace(name, "_");
572-
}
573542
}
574543
}

src/CompilerService.cs

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace Oxide.CSharp
2424
{
2525
internal class CompilerService
2626
{
27+
private static readonly Regex SymbolEscapeRegex = new Regex(@"[^\w\d]", RegexOptions.Compiled);
2728
private const string baseUrl = "https://downloads.oxidemod.com/artifacts/Oxide.Compiler/{0}/";
2829
private Hash<int, Compilation> compilations;
2930
private Queue<CompilerMessage> messageQueue;
@@ -38,6 +39,7 @@ internal class CompilerService
3839
private static Regex fileErrorRegex = new Regex(@"^\[(?'Severity'\S+)\]\[(?'Code'\S+)\]\[(?'File'\S+)\] (?'Message'.+)$", RegexOptions.Compiled);
3940
public bool Installed => File.Exists(filePath);
4041
private float startTime;
42+
private string[] preprocessor = null;
4143

4244
public CompilerService(Extension extension)
4345
{
@@ -97,16 +99,49 @@ private void ExpireFileCache()
9799
}
98100

99101
ArrayPool.Free(toRemove);
100-
101-
if (index <= 0) return;
102-
103-
//Interface.Oxide.LogWarning($"[CSharp] Released {index} cached compiler dependencies, running garbage collection. . .");
104-
GC.Collect();
105102
}
106103
}
107104

108105
internal bool Precheck()
109106
{
107+
List<string> preprocessorList = new List<string>()
108+
{
109+
"OXIDE",
110+
"OXIDEMOD"
111+
};
112+
113+
Extension game = Interface.Oxide.GetAllExtensions().SingleOrDefault(e => e.IsGameExtension);
114+
115+
if (game != null)
116+
{
117+
string name = game.Name.ToUpperInvariant();
118+
string branch = game.Branch?.ToUpperInvariant() ?? "PUBLIC";
119+
preprocessorList.Add(EscapeSymbolName(name));
120+
preprocessorList.Add(EscapeSymbolName(name + "_" + branch));
121+
122+
if (game.Version != default)
123+
{
124+
preprocessorList.Add(EscapeSymbolName(name + "_" + game.Version));
125+
preprocessorList.Add(EscapeSymbolName(name + "_" + game.Version + "_" + branch));
126+
}
127+
}
128+
129+
#if DEBUG
130+
preprocessorList.Add("DEBUG");
131+
#endif
132+
133+
if (Interface.Oxide.Config.Compiler.PreprocessorDirectives.Count > 0)
134+
{
135+
preprocessorList.AddRange(Interface.Oxide.Config.Compiler.PreprocessorDirectives);
136+
}
137+
138+
preprocessor = preprocessorList.Distinct().ToArray();
139+
140+
#if DEBUG
141+
Log(LogType.Debug, $"Preprocessors are: {string.Join(", ", preprocessor)}");
142+
#endif
143+
144+
110145
if (!DownloadFile(remoteName, filePath, 3))
111146
{
112147
return false;
@@ -511,12 +546,13 @@ private void EnqueueCompilation(Compilation compilation)
511546
{
512547
OutputFile = compilation.name,
513548
SourceFiles = sourceFiles.ToArray(),
514-
ReferenceFiles = compilation.references.Values.ToArray()
549+
ReferenceFiles = compilation.references.Values.ToArray(),
550+
Preprocessor = preprocessor
515551
#if DEBUG
516552
, Debug = true
517553
#endif
518554
};
519-
555+
520556
CompilerMessage message = new CompilerMessage { Id = compilation.id, Data = data, Type = CompilerMessageType.Compile };
521557
if (ready)
522558
{
@@ -779,5 +815,15 @@ private static string GenerateFileHash(string file)
779815
return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", string.Empty).ToLowerInvariant();
780816
}
781817
}
818+
819+
/// <summary>
820+
/// This allows to handle cases where injected symbols have inappropriate characters (e.g. git branches can have "/", "-" etc. while #define disallows them)
821+
/// </summary>
822+
/// <param name="name"></param>
823+
/// <returns></returns>
824+
private string EscapeSymbolName(string name)
825+
{
826+
return SymbolEscapeRegex.Replace(name, "_");
827+
}
782828
}
783829
}

src/ObjectStream/Data/CompilerData.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ public CompilerData()
2828
public CompilerLanguageVersion Version { get; set; }
2929
public string Encoding { get; set; }
3030
public bool Debug { get; set; }
31+
public string[] Preprocessor { get; set; }
3132
}
3233
}

0 commit comments

Comments
 (0)