Compiler.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.CodeDom.Compiler;
  29. using System.Collections.Generic;
  30. using System.Globalization;
  31. using System.IO;
  32. using Microsoft.CSharp;
  33. using Microsoft.JScript;
  34. using Microsoft.VisualBasic;
  35. using OpenSim.Region.Environment.Interfaces;
  36. using OpenSim.Region.ScriptEngine.Interfaces;
  37. namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
  38. {
  39. public class Compiler
  40. {
  41. // private static readonly log4net.ILog m_log
  42. // = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  43. // * Uses "LSL2Converter" to convert LSL to C# if necessary.
  44. // * Compiles C#-code into an assembly
  45. // * Returns assembly name ready for AppDomain load.
  46. //
  47. // Assembly is compiled using LSL_BaseClass as base. Look at debug C# code file created when LSL script is compiled for full details.
  48. //
  49. internal enum enumCompileType
  50. {
  51. lsl = 0,
  52. cs = 1,
  53. vb = 2,
  54. js = 3,
  55. yp = 4
  56. }
  57. /// <summary>
  58. /// This contains number of lines WE use for header when compiling script. User will get error in line x-LinesToRemoveOnError when error occurs.
  59. /// </summary>
  60. public int LinesToRemoveOnError = 3;
  61. private enumCompileType DefaultCompileLanguage;
  62. private bool WriteScriptSourceToDebugFile;
  63. private bool CompileWithDebugInformation;
  64. private Dictionary<string, bool> AllowedCompilers = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
  65. private Dictionary<string, enumCompileType> LanguageMapping = new Dictionary<string, enumCompileType>(StringComparer.CurrentCultureIgnoreCase);
  66. private string FilePrefix;
  67. private string ScriptEnginesPath = "ScriptEngines";
  68. private static ICodeConverter LSL_Converter;
  69. private static CSharpCodeProvider CScodeProvider = new CSharpCodeProvider();
  70. private static VBCodeProvider VBcodeProvider = new VBCodeProvider();
  71. private static JScriptCodeProvider JScodeProvider = new JScriptCodeProvider();
  72. private static CSharpCodeProvider YPcodeProvider = new CSharpCodeProvider(); // YP is translated into CSharp
  73. private static YP2CSConverter YP_Converter = new YP2CSConverter();
  74. // private static int instanceID = new Random().Next(0, int.MaxValue); // Unique number to use on our compiled files
  75. private static UInt64 scriptCompileCounter = 0; // And a counter
  76. private bool m_UseCompiler = false;
  77. public IScriptEngine m_scriptEngine;
  78. public Compiler(IScriptEngine scriptEngine)
  79. {
  80. m_scriptEngine = scriptEngine;
  81. ReadConfig();
  82. }
  83. public bool in_startup = true;
  84. public void ReadConfig()
  85. {
  86. // Get some config
  87. m_UseCompiler = m_scriptEngine.Config.GetBoolean("UseNewCompiler", true);
  88. WriteScriptSourceToDebugFile = m_scriptEngine.Config.GetBoolean("WriteScriptSourceToDebugFile", true);
  89. CompileWithDebugInformation = m_scriptEngine.Config.GetBoolean("CompileWithDebugInformation", true);
  90. // Get file prefix from scriptengine name and make it file system safe:
  91. FilePrefix = "CommonCompiler";
  92. foreach (char c in Path.GetInvalidFileNameChars())
  93. {
  94. FilePrefix = FilePrefix.Replace(c, '_');
  95. }
  96. // First time we start? Delete old files
  97. if (in_startup)
  98. {
  99. in_startup = false;
  100. DeleteOldFiles();
  101. }
  102. // Map name and enum type of our supported languages
  103. LanguageMapping.Add(enumCompileType.cs.ToString(), enumCompileType.cs);
  104. LanguageMapping.Add(enumCompileType.vb.ToString(), enumCompileType.vb);
  105. LanguageMapping.Add(enumCompileType.lsl.ToString(), enumCompileType.lsl);
  106. LanguageMapping.Add(enumCompileType.js.ToString(), enumCompileType.js);
  107. LanguageMapping.Add(enumCompileType.yp.ToString(), enumCompileType.yp);
  108. // Allowed compilers
  109. string allowComp = m_scriptEngine.Config.GetString("AllowedCompilers", "lsl,cs,vb,js,yp");
  110. AllowedCompilers.Clear();
  111. #if DEBUG
  112. m_scriptEngine.Log.Debug("[Compiler]: Allowed languages: " + allowComp);
  113. #endif
  114. foreach (string strl in allowComp.Split(','))
  115. {
  116. string strlan = strl.Trim(" \t".ToCharArray()).ToLower();
  117. if (!LanguageMapping.ContainsKey(strlan))
  118. {
  119. m_scriptEngine.Log.Error("[Compiler]: Config error. Compiler is unable to recognize language type \"" + strlan + "\" specified in \"AllowedCompilers\".");
  120. }
  121. else
  122. {
  123. #if DEBUG
  124. //m_scriptEngine.Log.Debug("[Compiler]: Config OK. Compiler recognized language type \"" + strlan + "\" specified in \"AllowedCompilers\".");
  125. #endif
  126. }
  127. AllowedCompilers.Add(strlan, true);
  128. }
  129. if (AllowedCompilers.Count == 0)
  130. m_scriptEngine.Log.Error("[Compiler]: Config error. Compiler could not recognize any language in \"AllowedCompilers\". Scripts will not be executed!");
  131. // Default language
  132. string defaultCompileLanguage = m_scriptEngine.Config.GetString("DefaultCompileLanguage", "lsl").ToLower();
  133. // Is this language recognized at all?
  134. if (!LanguageMapping.ContainsKey(defaultCompileLanguage))
  135. {
  136. m_scriptEngine.Log.Error("[Compiler]: " +
  137. "Config error. Default language \"" + defaultCompileLanguage + "\" specified in \"DefaultCompileLanguage\" is not recognized as a valid language. Changing default to: \"lsl\".");
  138. defaultCompileLanguage = "lsl";
  139. }
  140. // Is this language in allow-list?
  141. if (!AllowedCompilers.ContainsKey(defaultCompileLanguage))
  142. {
  143. m_scriptEngine.Log.Error("[Compiler]: " +
  144. "Config error. Default language \"" + defaultCompileLanguage + "\"specified in \"DefaultCompileLanguage\" is not in list of \"AllowedCompilers\". Scripts may not be executed!");
  145. }
  146. else
  147. {
  148. #if DEBUG
  149. // m_scriptEngine.Log.Debug("[Compiler]: " +
  150. // "Config OK. Default language \"" + defaultCompileLanguage + "\" specified in \"DefaultCompileLanguage\" is recognized as a valid language.");
  151. #endif
  152. // LANGUAGE IS IN ALLOW-LIST
  153. DefaultCompileLanguage = LanguageMapping[defaultCompileLanguage];
  154. }
  155. // We now have an allow-list, a mapping list, and a default language
  156. }
  157. /// <summary>
  158. /// Delete old script files
  159. /// </summary>
  160. private void DeleteOldFiles()
  161. {
  162. // CREATE FOLDER IF IT DOESNT EXIST
  163. if (!Directory.Exists(ScriptEnginesPath))
  164. {
  165. try
  166. {
  167. Directory.CreateDirectory(ScriptEnginesPath);
  168. }
  169. catch (Exception ex)
  170. {
  171. m_scriptEngine.Log.Error("[Compiler]: Exception trying to create ScriptEngine directory \"" + ScriptEnginesPath + "\": " + ex.ToString());
  172. }
  173. }
  174. if (!Directory.Exists(Path.Combine(ScriptEnginesPath,
  175. m_scriptEngine.World.RegionInfo.RegionID.ToString())))
  176. {
  177. try
  178. {
  179. Directory.CreateDirectory(Path.Combine(ScriptEnginesPath,
  180. m_scriptEngine.World.RegionInfo.RegionID.ToString()));
  181. }
  182. catch (Exception ex)
  183. {
  184. m_scriptEngine.Log.Error("[Compiler]: Exception trying to create ScriptEngine directory \"" + Path.Combine(ScriptEnginesPath,
  185. m_scriptEngine.World.RegionInfo.RegionID.ToString())+ "\": " + ex.ToString());
  186. }
  187. }
  188. foreach (string file in Directory.GetFiles(Path.Combine(ScriptEnginesPath,
  189. m_scriptEngine.World.RegionInfo.RegionID.ToString())))
  190. {
  191. //m_scriptEngine.Log.Error("[Compiler]: FILE FOUND: " + file);
  192. if (file.ToLower().StartsWith(FilePrefix + "_compiled_") ||
  193. file.ToLower().StartsWith(FilePrefix + "_source_"))
  194. {
  195. try
  196. {
  197. File.Delete(file);
  198. }
  199. catch (Exception ex)
  200. {
  201. m_scriptEngine.Log.Error("[Compiler]: Exception trying delete old script file \"" + file + "\": " + ex.ToString());
  202. }
  203. }
  204. }
  205. }
  206. ////private ICodeCompiler icc = codeProvider.CreateCompiler();
  207. //public string CompileFromFile(string LSOFileName)
  208. //{
  209. // switch (Path.GetExtension(LSOFileName).ToLower())
  210. // {
  211. // case ".txt":
  212. // case ".lsl":
  213. // Common.ScriptEngineBase.Shared.SendToDebug("Source code is LSL, converting to CS");
  214. // return CompileFromLSLText(File.ReadAllText(LSOFileName));
  215. // case ".cs":
  216. // Common.ScriptEngineBase.Shared.SendToDebug("Source code is CS");
  217. // return CompileFromCSText(File.ReadAllText(LSOFileName));
  218. // default:
  219. // throw new Exception("Unknown script type.");
  220. // }
  221. //}
  222. /// <summary>
  223. /// Converts script from LSL to CS and calls CompileFromCSText
  224. /// </summary>
  225. /// <param name="Script">LSL script</param>
  226. /// <returns>Filename to .dll assembly</returns>
  227. public string PerformScriptCompile(string Script, string asset)
  228. {
  229. string OutFile = Path.Combine(ScriptEnginesPath, Path.Combine(
  230. m_scriptEngine.World.RegionInfo.RegionID.ToString(),
  231. FilePrefix + "_compiled_" + asset + ".dll"));
  232. // string OutFile = Path.Combine(ScriptEnginesPath,
  233. // FilePrefix + "_compiled_" + asset + ".dll");
  234. if (File.Exists(OutFile))
  235. {
  236. m_scriptEngine.Log.DebugFormat("[Compiler] Returning existing assembly for {0}", asset);
  237. return OutFile;
  238. }
  239. if (!Directory.Exists(ScriptEnginesPath))
  240. {
  241. try
  242. {
  243. Directory.CreateDirectory(ScriptEnginesPath);
  244. }
  245. catch (Exception)
  246. {
  247. }
  248. }
  249. if (!Directory.Exists(Path.Combine(ScriptEnginesPath,
  250. m_scriptEngine.World.RegionInfo.RegionID.ToString())))
  251. {
  252. try
  253. {
  254. Directory.CreateDirectory(ScriptEnginesPath);
  255. }
  256. catch (Exception)
  257. {
  258. }
  259. }
  260. enumCompileType l = DefaultCompileLanguage;
  261. if (Script.StartsWith("//c#", true, CultureInfo.InvariantCulture))
  262. l = enumCompileType.cs;
  263. if (Script.StartsWith("//vb", true, CultureInfo.InvariantCulture))
  264. {
  265. l = enumCompileType.vb;
  266. // We need to remove //vb, it won't compile with that
  267. Script = Script.Substring(4, Script.Length - 4);
  268. }
  269. if (Script.StartsWith("//lsl", true, CultureInfo.InvariantCulture))
  270. l = enumCompileType.lsl;
  271. if (Script.StartsWith("//js", true, CultureInfo.InvariantCulture))
  272. l = enumCompileType.js;
  273. if (Script.StartsWith("//yp", true, CultureInfo.InvariantCulture))
  274. l = enumCompileType.yp;
  275. if (!AllowedCompilers.ContainsKey(l.ToString()))
  276. {
  277. // Not allowed to compile to this language!
  278. string errtext = String.Empty;
  279. errtext += "The compiler for language \"" + l.ToString() + "\" is not in list of allowed compilers. Script will not be executed!";
  280. throw new Exception(errtext);
  281. }
  282. string compileScript = Script;
  283. if (l == enumCompileType.lsl)
  284. {
  285. // Its LSL, convert it to C#
  286. //compileScript = LSL_Converter.Convert(Script);
  287. if(m_UseCompiler)
  288. LSL_Converter = (ICodeConverter)new CSCodeGenerator();
  289. else
  290. LSL_Converter = (ICodeConverter)new LSL2CSConverter();
  291. compileScript = LSL_Converter.Convert(Script);
  292. l = enumCompileType.cs;
  293. }
  294. if (l == enumCompileType.yp)
  295. {
  296. // Its YP, convert it to C#
  297. compileScript = YP_Converter.Convert(Script);
  298. // We have our own processor now
  299. //l = enumCompileType.cs;
  300. }
  301. // Insert additional assemblies here
  302. //ADAM: Disabled for the moment until it's working right.
  303. bool enableCommanderLSL = false;
  304. if (enableCommanderLSL == true && ((l == enumCompileType.cs) || (l == enumCompileType.yp)))
  305. {
  306. foreach (KeyValuePair<string,
  307. ICommander> com
  308. in m_scriptEngine.World.GetCommanders())
  309. {
  310. compileScript = com.Value.GenerateRuntimeAPI() + compileScript;
  311. }
  312. }
  313. // End of insert
  314. switch (l)
  315. {
  316. case enumCompileType.cs:
  317. compileScript = CreateCSCompilerScript(compileScript);
  318. break;
  319. case enumCompileType.vb:
  320. compileScript = CreateVBCompilerScript(compileScript);
  321. break;
  322. case enumCompileType.js:
  323. compileScript = CreateJSCompilerScript(compileScript);
  324. break;
  325. case enumCompileType.yp:
  326. compileScript = CreateYPCompilerScript(compileScript);
  327. break;
  328. }
  329. // m_log.Debug("[ScriptEngine.DotNetEngine]: Preparing to compile the following LSL to C# translated code");
  330. // m_log.Debug("");
  331. // m_log.Debug(compileScript);
  332. return CompileFromDotNetText(compileScript, l, asset);
  333. }
  334. private static string CreateJSCompilerScript(string compileScript)
  335. {
  336. compileScript = String.Empty +
  337. "import OpenSim.Region.ScriptEngine.Shared; import System.Collections.Generic;\r\n" +
  338. "package SecondLife {\r\n" +
  339. "class Script extends OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass { \r\n" +
  340. compileScript +
  341. "} }\r\n";
  342. return compileScript;
  343. }
  344. private static string CreateCSCompilerScript(string compileScript)
  345. {
  346. compileScript = String.Empty +
  347. "using OpenSim.Region.ScriptEngine.Shared; using System.Collections.Generic;\r\n" +
  348. String.Empty + "namespace SecondLife { " +
  349. String.Empty + "public class Script : OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass { \r\n" +
  350. @"public Script() { } " +
  351. compileScript +
  352. "} }\r\n";
  353. return compileScript;
  354. }
  355. private static string CreateYPCompilerScript(string compileScript)
  356. {
  357. compileScript = String.Empty +
  358. "using OpenSim.Region.ScriptEngine.Shared.YieldProlog; " +
  359. "using OpenSim.Region.ScriptEngine.Shared; using System.Collections.Generic;\r\n" +
  360. String.Empty + "namespace SecondLife { " +
  361. String.Empty + "public class Script : OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass { \r\n" +
  362. //@"public Script() { } " +
  363. @"static OpenSim.Region.ScriptEngine.Shared.YieldProlog.YP YP=null; " +
  364. @"public Script() { YP= new OpenSim.Region.ScriptEngine.Shared.YieldProlog.YP(); } " +
  365. compileScript +
  366. "} }\r\n";
  367. return compileScript;
  368. }
  369. private static string CreateVBCompilerScript(string compileScript)
  370. {
  371. compileScript = String.Empty +
  372. "Imports OpenSim.Region.ScriptEngine.Shared: Imports System.Collections.Generic: " +
  373. String.Empty + "NameSpace SecondLife:" +
  374. String.Empty + "Public Class Script: Inherits OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass: " +
  375. "\r\nPublic Sub New()\r\nEnd Sub: " +
  376. compileScript +
  377. ":End Class :End Namespace\r\n";
  378. return compileScript;
  379. }
  380. /// <summary>
  381. /// Compile .NET script to .Net assembly (.dll)
  382. /// </summary>
  383. /// <param name="Script">CS script</param>
  384. /// <returns>Filename to .dll assembly</returns>
  385. internal string CompileFromDotNetText(string Script, enumCompileType lang, string asset)
  386. {
  387. string ext = "." + lang.ToString();
  388. // Output assembly name
  389. scriptCompileCounter++;
  390. string OutFile = Path.Combine(ScriptEnginesPath, Path.Combine(
  391. m_scriptEngine.World.RegionInfo.RegionID.ToString(),
  392. FilePrefix + "_compiled_" + asset + ".dll"));
  393. #if DEBUG
  394. // m_scriptEngine.Log.Debug("[Compiler]: Starting compile of \"" + OutFile + "\".");
  395. #endif
  396. try
  397. {
  398. File.Delete(OutFile);
  399. }
  400. catch (Exception e) // NOTLEGIT - Should be just catching FileIOException
  401. {
  402. //m_scriptEngine.Log.Error("[Compiler]: Unable to delete old existring script-file before writing new. Compile aborted: " + e.ToString());
  403. throw new Exception("Unable to delete old existring script-file before writing new. Compile aborted: " + e.ToString());
  404. }
  405. //string OutFile = Path.Combine("ScriptEngines", "SecondLife.Script.dll");
  406. // DEBUG - write source to disk
  407. if (WriteScriptSourceToDebugFile)
  408. {
  409. string srcFileName = FilePrefix + "_source_" + Path.GetFileNameWithoutExtension(OutFile) + ext;
  410. try
  411. {
  412. File.WriteAllText(Path.Combine(Path.Combine(
  413. ScriptEnginesPath,
  414. m_scriptEngine.World.RegionInfo.RegionID.ToString()),
  415. srcFileName), Script);
  416. }
  417. catch (Exception ex) // NOTLEGIT - Should be just catching FileIOException
  418. {
  419. m_scriptEngine.Log.Error("[Compiler]: Exception while trying to write script source to file \"" + srcFileName + "\": " + ex.ToString());
  420. }
  421. }
  422. // Do actual compile
  423. CompilerParameters parameters = new CompilerParameters();
  424. parameters.IncludeDebugInformation = true;
  425. // Add all available assemblies
  426. // foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
  427. // {
  428. // Console.WriteLine("Adding assembly: " + asm.Location);
  429. // parameters.ReferencedAssemblies.Add(asm.Location);
  430. // }
  431. string rootPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
  432. // string rootPathSE = Path.GetDirectoryName(GetType().Assembly.Location);
  433. //Console.WriteLine("Assembly location: " + rootPath);
  434. parameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Shared.dll"));
  435. parameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Shared.Api.Runtime.dll"));
  436. if (lang == enumCompileType.yp)
  437. {
  438. parameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Shared.YieldProlog.dll"));
  439. }
  440. parameters.GenerateExecutable = false;
  441. parameters.OutputAssembly = OutFile;
  442. parameters.IncludeDebugInformation = CompileWithDebugInformation;
  443. //parameters.WarningLevel = 1; // Should be 4?
  444. parameters.TreatWarningsAsErrors = false;
  445. //Console.WriteLine(Script);
  446. CompilerResults results;
  447. switch (lang)
  448. {
  449. case enumCompileType.vb:
  450. results = VBcodeProvider.CompileAssemblyFromSource(parameters, Script);
  451. break;
  452. case enumCompileType.cs:
  453. results = CScodeProvider.CompileAssemblyFromSource(parameters, Script);
  454. break;
  455. case enumCompileType.js:
  456. results = JScodeProvider.CompileAssemblyFromSource(parameters, Script);
  457. break;
  458. case enumCompileType.yp:
  459. results = YPcodeProvider.CompileAssemblyFromSource(parameters, Script);
  460. break;
  461. default:
  462. throw new Exception("Compiler is not able to recongnize language type \"" + lang.ToString() + "\"");
  463. }
  464. // Check result
  465. // Go through errors
  466. //
  467. // WARNINGS AND ERRORS
  468. //
  469. if (results.Errors.Count > 0)
  470. {
  471. string errtext = String.Empty;
  472. foreach (CompilerError CompErr in results.Errors)
  473. {
  474. errtext += "Line number " + (CompErr.Line - LinesToRemoveOnError) +
  475. ", Error Number: " + CompErr.ErrorNumber +
  476. ", '" + CompErr.ErrorText + "'\r\n";
  477. }
  478. Console.WriteLine("[COMPILER ERROR]:" + errtext);
  479. if (!File.Exists(OutFile))
  480. {
  481. throw new Exception(errtext);
  482. }
  483. }
  484. //
  485. // NO ERRORS, BUT NO COMPILED FILE
  486. //
  487. if (!File.Exists(OutFile))
  488. {
  489. string errtext = String.Empty;
  490. errtext += "No compile error. But not able to locate compiled file.";
  491. throw new Exception(errtext);
  492. }
  493. m_scriptEngine.Log.DebugFormat("[Compiler] Compiled new assembly for {0}", asset);
  494. return OutFile;
  495. }
  496. }
  497. }