@@ -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}
0 commit comments