Compiler.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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 OpenSimulator 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.Reflection;
  32. using System.IO;
  33. using System.Text;
  34. using Microsoft.CSharp;
  35. //using Microsoft.JScript;
  36. using Microsoft.VisualBasic;
  37. using log4net;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Region.ScriptEngine.Interfaces;
  40. using OpenMetaverse;
  41. namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
  42. {
  43. public class Compiler : ICompiler
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. // * Uses "LSL2Converter" to convert LSL to C# if necessary.
  47. // * Compiles C#-code into an assembly
  48. // * Returns assembly name ready for AppDomain load.
  49. //
  50. // Assembly is compiled using LSL_BaseClass as base. Look at debug C# code file created when LSL script is compiled for full details.
  51. //
  52. internal enum enumCompileType
  53. {
  54. lsl = 0,
  55. cs = 1,
  56. vb = 2
  57. }
  58. /// <summary>
  59. /// This contains number of lines WE use for header when compiling script. User will get error in line x-LinesToRemoveOnError when error occurs.
  60. /// </summary>
  61. public int LinesToRemoveOnError = 3;
  62. private enumCompileType DefaultCompileLanguage;
  63. private bool WriteScriptSourceToDebugFile;
  64. private bool CompileWithDebugInformation;
  65. private Dictionary<string, bool> AllowedCompilers = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
  66. private Dictionary<string, enumCompileType> LanguageMapping = new Dictionary<string, enumCompileType>(StringComparer.CurrentCultureIgnoreCase);
  67. private bool m_insertCoopTerminationCalls;
  68. private string FilePrefix;
  69. private string ScriptEnginesPath = null;
  70. // mapping between LSL and C# line/column numbers
  71. private ICodeConverter LSL_Converter;
  72. private List<string> m_warnings = new List<string>();
  73. private static UInt64 scriptCompileCounter = 0; // And a counter
  74. public IScriptEngine m_scriptEngine;
  75. private Dictionary<string, Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>>> m_lineMaps =
  76. new Dictionary<string, Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>>>();
  77. public bool in_startup = true;
  78. public Compiler(IScriptEngine scriptEngine)
  79. {
  80. m_scriptEngine = scriptEngine;
  81. ScriptEnginesPath = scriptEngine.ScriptEnginePath;
  82. ReadConfig();
  83. }
  84. public void ReadConfig()
  85. {
  86. // Get some config
  87. WriteScriptSourceToDebugFile = m_scriptEngine.Config.GetBoolean("WriteScriptSourceToDebugFile", false);
  88. CompileWithDebugInformation = m_scriptEngine.Config.GetBoolean("CompileWithDebugInformation", true);
  89. bool DeleteScriptsOnStartup = m_scriptEngine.Config.GetBoolean("DeleteScriptsOnStartup", true);
  90. m_insertCoopTerminationCalls = m_scriptEngine.Config.GetString("ScriptStopStrategy", "abort") == "co-op";
  91. // Get file prefix from scriptengine name and make it file system safe:
  92. FilePrefix = "CommonCompiler";
  93. foreach (char c in Path.GetInvalidFileNameChars())
  94. {
  95. FilePrefix = FilePrefix.Replace(c, '_');
  96. }
  97. if (in_startup)
  98. {
  99. in_startup = false;
  100. CheckOrCreateScriptsDirectory();
  101. // First time we start? Delete old files
  102. if (DeleteScriptsOnStartup)
  103. DeleteOldFiles();
  104. }
  105. // Map name and enum type of our supported languages
  106. LanguageMapping.Add(enumCompileType.cs.ToString(), enumCompileType.cs);
  107. LanguageMapping.Add(enumCompileType.vb.ToString(), enumCompileType.vb);
  108. LanguageMapping.Add(enumCompileType.lsl.ToString(), enumCompileType.lsl);
  109. // Allowed compilers
  110. string allowComp = m_scriptEngine.Config.GetString("AllowedCompilers", "lsl");
  111. AllowedCompilers.Clear();
  112. #if DEBUG
  113. m_log.Debug("[Compiler]: Allowed languages: " + allowComp);
  114. #endif
  115. foreach (string strl in allowComp.Split(','))
  116. {
  117. string strlan = strl.Trim(" \t".ToCharArray()).ToLower();
  118. if (!LanguageMapping.ContainsKey(strlan))
  119. {
  120. m_log.Error("[Compiler]: Config error. Compiler is unable to recognize language type \"" + strlan + "\" specified in \"AllowedCompilers\".");
  121. }
  122. else
  123. {
  124. #if DEBUG
  125. //m_log.Debug("[Compiler]: Config OK. Compiler recognized language type \"" + strlan + "\" specified in \"AllowedCompilers\".");
  126. #endif
  127. }
  128. AllowedCompilers.Add(strlan, true);
  129. }
  130. if (AllowedCompilers.Count == 0)
  131. m_log.Error("[Compiler]: Config error. Compiler could not recognize any language in \"AllowedCompilers\". Scripts will not be executed!");
  132. // Default language
  133. string defaultCompileLanguage = m_scriptEngine.Config.GetString("DefaultCompileLanguage", "lsl").ToLower();
  134. // Is this language recognized at all?
  135. if (!LanguageMapping.ContainsKey(defaultCompileLanguage))
  136. {
  137. m_log.Error("[Compiler]: " +
  138. "Config error. Default language \"" + defaultCompileLanguage + "\" specified in \"DefaultCompileLanguage\" is not recognized as a valid language. Changing default to: \"lsl\".");
  139. defaultCompileLanguage = "lsl";
  140. }
  141. // Is this language in allow-list?
  142. if (!AllowedCompilers.ContainsKey(defaultCompileLanguage))
  143. {
  144. m_log.Error("[Compiler]: " +
  145. "Config error. Default language \"" + defaultCompileLanguage + "\"specified in \"DefaultCompileLanguage\" is not in list of \"AllowedCompilers\". Scripts may not be executed!");
  146. }
  147. else
  148. {
  149. #if DEBUG
  150. // m_log.Debug("[Compiler]: " +
  151. // "Config OK. Default language \"" + defaultCompileLanguage + "\" specified in \"DefaultCompileLanguage\" is recognized as a valid language.");
  152. #endif
  153. // LANGUAGE IS IN ALLOW-LIST
  154. DefaultCompileLanguage = LanguageMapping[defaultCompileLanguage];
  155. }
  156. // We now have an allow-list, a mapping list, and a default language
  157. }
  158. /// <summary>
  159. /// Create the directory where compiled scripts are stored if it does not already exist.
  160. /// </summary>
  161. private void CheckOrCreateScriptsDirectory()
  162. {
  163. if (!Directory.Exists(ScriptEnginesPath))
  164. {
  165. try
  166. {
  167. Directory.CreateDirectory(ScriptEnginesPath);
  168. }
  169. catch (Exception ex)
  170. {
  171. m_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_log.Error("[Compiler]: Exception trying to create ScriptEngine directory \"" + Path.Combine(ScriptEnginesPath,
  185. m_scriptEngine.World.RegionInfo.RegionID.ToString()) + "\": " + ex.ToString());
  186. }
  187. }
  188. }
  189. /// <summary>
  190. /// Delete old script files
  191. /// </summary>
  192. private void DeleteOldFiles()
  193. {
  194. foreach (string file in Directory.GetFiles(Path.Combine(ScriptEnginesPath,
  195. m_scriptEngine.World.RegionInfo.RegionID.ToString()), FilePrefix + "_compiled*"))
  196. {
  197. try
  198. {
  199. File.Delete(file);
  200. }
  201. catch (Exception ex)
  202. {
  203. m_log.Error("[Compiler]: Exception trying delete old script file \"" + file + "\": " + ex.ToString());
  204. }
  205. }
  206. foreach (string file in Directory.GetFiles(Path.Combine(ScriptEnginesPath,
  207. m_scriptEngine.World.RegionInfo.RegionID.ToString()), FilePrefix + "_source*"))
  208. {
  209. try
  210. {
  211. File.Delete(file);
  212. }
  213. catch (Exception ex)
  214. {
  215. m_log.Error("[Compiler]: Exception trying delete old script file \"" + file + "\": " + ex.ToString());
  216. }
  217. }
  218. }
  219. public string GetCompilerOutput(string assetID)
  220. {
  221. return Path.Combine(ScriptEnginesPath, Path.Combine(
  222. m_scriptEngine.World.RegionInfo.RegionID.ToString(),
  223. FilePrefix + "_compiled_" + assetID + ".dll"));
  224. }
  225. public string GetCompilerOutput(UUID assetID)
  226. {
  227. return GetCompilerOutput(assetID.ToString());
  228. }
  229. public void PerformScriptCompile(
  230. string source, string asset, UUID ownerUUID,
  231. out string assembly, out Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> linemap)
  232. {
  233. PerformScriptCompile(source, asset, ownerUUID, false, out assembly, out linemap);
  234. }
  235. public void PerformScriptCompile(
  236. string source, string asset, UUID ownerUUID, bool alwaysRecompile,
  237. out string assembly, out Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> linemap)
  238. {
  239. // m_log.DebugFormat("[Compiler]: Checking script for asset {0} in {1}\n{2}", asset, m_scriptEngine.World.Name, source);
  240. IScriptModuleComms comms = m_scriptEngine.World.RequestModuleInterface<IScriptModuleComms>();
  241. linemap = null;
  242. m_warnings.Clear();
  243. assembly = GetCompilerOutput(asset);
  244. // m_log.DebugFormat("[Compiler]: Retrieved assembly {0} for asset {1} in {2}", assembly, asset, m_scriptEngine.World.Name);
  245. CheckOrCreateScriptsDirectory();
  246. // Don't recompile if we're not forced to and we already have it
  247. // Performing 3 file exists tests for every script can still be slow
  248. if (!alwaysRecompile && File.Exists(assembly) && File.Exists(assembly + ".text") && File.Exists(assembly + ".map"))
  249. {
  250. // m_log.DebugFormat("[Compiler]: Found existing assembly {0} for asset {1} in {2}", assembly, asset, m_scriptEngine.World.Name);
  251. // If we have already read this linemap file, then it will be in our dictionary.
  252. // Don't build another copy of the dictionary (saves memory) and certainly
  253. // don't keep reading the same file from disk multiple times.
  254. if (!m_lineMaps.ContainsKey(assembly))
  255. m_lineMaps[assembly] = ReadMapFile(assembly + ".map");
  256. linemap = m_lineMaps[assembly];
  257. return;
  258. }
  259. // m_log.DebugFormat("[Compiler]: Compiling assembly {0} for asset {1} in {2}", assembly, asset, m_scriptEngine.World.Name);
  260. if (source == String.Empty)
  261. throw new Exception("Cannot find script assembly and no script text present");
  262. enumCompileType language = DefaultCompileLanguage;
  263. if (source.StartsWith("//c#", true, CultureInfo.InvariantCulture))
  264. language = enumCompileType.cs;
  265. if (source.StartsWith("//vb", true, CultureInfo.InvariantCulture))
  266. {
  267. language = enumCompileType.vb;
  268. // We need to remove //vb, it won't compile with that
  269. source = source.Substring(4, source.Length - 4);
  270. }
  271. if (source.StartsWith("//lsl", true, CultureInfo.InvariantCulture))
  272. language = enumCompileType.lsl;
  273. // m_log.DebugFormat("[Compiler]: Compile language is {0}", language);
  274. if (!AllowedCompilers.ContainsKey(language.ToString()))
  275. {
  276. // Not allowed to compile to this language!
  277. string errtext = String.Empty;
  278. errtext += "The compiler for language \"" + language.ToString() + "\" is not in list of allowed compilers. Script will not be executed!";
  279. throw new Exception(errtext);
  280. }
  281. if (m_scriptEngine.World.Permissions.CanCompileScript(ownerUUID, (int)language) == false)
  282. {
  283. // Not allowed to compile to this language!
  284. string errtext = String.Empty;
  285. errtext += ownerUUID + " is not in list of allowed users for this scripting language. Script will not be executed!";
  286. throw new Exception(errtext);
  287. }
  288. string compileScript = string.Empty;
  289. if (language == enumCompileType.lsl)
  290. {
  291. // Its LSL, convert it to C#
  292. StringBuilder sb = new StringBuilder(16394);
  293. LSL_Converter = (ICodeConverter)new CSCodeGenerator(comms, m_insertCoopTerminationCalls);
  294. AddCSScriptHeader(
  295. m_scriptEngine.ScriptClassName,
  296. m_scriptEngine.ScriptBaseClassName,
  297. m_scriptEngine.ScriptBaseClassParameters,
  298. sb);
  299. LSL_Converter.Convert(source,sb);
  300. AddCSScriptTail(sb);
  301. compileScript = sb.ToString();
  302. // copy converter warnings into our warnings.
  303. foreach (string warning in LSL_Converter.GetWarnings())
  304. {
  305. AddWarning(warning);
  306. }
  307. linemap = ((CSCodeGenerator)LSL_Converter).PositionMap;
  308. // Write the linemap to a file and save it in our dictionary for next time.
  309. m_lineMaps[assembly] = linemap;
  310. WriteMapFile(assembly + ".map", linemap);
  311. LSL_Converter.Clear();
  312. }
  313. else
  314. {
  315. switch (language)
  316. {
  317. case enumCompileType.cs:
  318. compileScript = CreateCSCompilerScript(
  319. source,
  320. m_scriptEngine.ScriptClassName,
  321. m_scriptEngine.ScriptBaseClassName,
  322. m_scriptEngine.ScriptBaseClassParameters);
  323. break;
  324. case enumCompileType.vb:
  325. compileScript = CreateVBCompilerScript(
  326. source, m_scriptEngine.ScriptClassName, m_scriptEngine.ScriptBaseClassName);
  327. break;
  328. }
  329. }
  330. assembly = CompileFromDotNetText(compileScript, language, asset, assembly);
  331. }
  332. public string[] GetWarnings()
  333. {
  334. return m_warnings.ToArray();
  335. }
  336. private void AddWarning(string warning)
  337. {
  338. if (!m_warnings.Contains(warning))
  339. {
  340. m_warnings.Add(warning);
  341. }
  342. }
  343. // private static string CreateJSCompilerScript(string compileScript)
  344. // {
  345. // compileScript = String.Empty +
  346. // "import OpenSim.Region.ScriptEngine.Shared; import System.Collections.Generic;\r\n" +
  347. // "package SecondLife {\r\n" +
  348. // "class Script extends OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass { \r\n" +
  349. // compileScript +
  350. // "} }\r\n";
  351. // return compileScript;
  352. // }
  353. public static void AddCSScriptHeader(string className, string baseClassName, ParameterInfo[] constructorParameters, StringBuilder sb)
  354. {
  355. sb.Append(string.Format(
  356. @"using OpenSim.Region.ScriptEngine.Shared;
  357. using System.Collections.Generic;
  358. namespace SecondLife
  359. {{
  360. public class {0} : {1}
  361. {{
  362. public {0}({2}) : base({3}) {{}}
  363. ",
  364. className,
  365. baseClassName,
  366. constructorParameters != null
  367. ? string.Join(", ", Array.ConvertAll<ParameterInfo, string>(constructorParameters, pi => pi.ToString()))
  368. : "",
  369. constructorParameters != null
  370. ? string.Join(", ", Array.ConvertAll<ParameterInfo, string>(constructorParameters, pi => pi.Name))
  371. : ""
  372. ));
  373. }
  374. public static void AddCSScriptTail(StringBuilder sb)
  375. {
  376. sb.Append(string.Format(" }}\n}}\n"));
  377. }
  378. public static string CreateCSCompilerScript(
  379. string compileScript, string className, string baseClassName, ParameterInfo[] constructorParameters)
  380. {
  381. compileScript = string.Format(
  382. @"using OpenSim.Region.ScriptEngine.Shared;
  383. using System.Collections.Generic;
  384. namespace SecondLife
  385. {{
  386. public class {0} : {1}
  387. {{
  388. public {0}({2}) : base({3}) {{}}
  389. {4}
  390. }}
  391. }}",
  392. className,
  393. baseClassName,
  394. constructorParameters != null
  395. ? string.Join(", ", Array.ConvertAll<ParameterInfo, string>(constructorParameters, pi => pi.ToString()))
  396. : "",
  397. constructorParameters != null
  398. ? string.Join(", ", Array.ConvertAll<ParameterInfo, string>(constructorParameters, pi => pi.Name))
  399. : "",
  400. compileScript);
  401. return compileScript;
  402. }
  403. public static string CreateVBCompilerScript(string compileScript, string className, string baseClassName)
  404. {
  405. compileScript = String.Empty +
  406. "Imports OpenSim.Region.ScriptEngine.Shared: Imports System.Collections.Generic: " +
  407. String.Empty + "NameSpace SecondLife:" +
  408. String.Empty + "Public Class " + className + ": Inherits " + baseClassName +
  409. "\r\nPublic Sub New()\r\nEnd Sub: " +
  410. compileScript +
  411. ":End Class :End Namespace\r\n";
  412. return compileScript;
  413. }
  414. /// <summary>
  415. /// Compile .NET script to .Net assembly (.dll)
  416. /// </summary>
  417. /// <param name="Script">CS script</param>
  418. /// <returns>Filename to .dll assembly</returns>
  419. internal string CompileFromDotNetText(string Script, enumCompileType lang, string asset, string assembly)
  420. {
  421. // m_log.DebugFormat("[Compiler]: Compiling to assembly\n{0}", Script);
  422. string ext = "." + lang.ToString();
  423. // Output assembly name
  424. scriptCompileCounter++;
  425. try
  426. {
  427. if (File.Exists(assembly))
  428. {
  429. File.SetAttributes(assembly, FileAttributes.Normal);
  430. File.Delete(assembly);
  431. }
  432. }
  433. catch (Exception e) // NOTLEGIT - Should be just FileIOException
  434. {
  435. throw new Exception("Unable to delete old existing " +
  436. "script-file before writing new. Compile aborted: " +
  437. e.ToString());
  438. }
  439. // DEBUG - write source to disk
  440. if (WriteScriptSourceToDebugFile)
  441. {
  442. string srcFileName = FilePrefix + "_source_" +
  443. Path.GetFileNameWithoutExtension(assembly) + ext;
  444. try
  445. {
  446. File.WriteAllText(Path.Combine(Path.Combine(
  447. ScriptEnginesPath,
  448. m_scriptEngine.World.RegionInfo.RegionID.ToString()),
  449. srcFileName), Script);
  450. }
  451. catch (Exception ex) //NOTLEGIT - Should be just FileIOException
  452. {
  453. m_log.Error("[Compiler]: Exception while " +
  454. "trying to write script source to file \"" +
  455. srcFileName + "\": " + ex.ToString());
  456. }
  457. }
  458. // Do actual compile
  459. CompilerParameters parameters = new CompilerParameters();
  460. string rootPath = AppDomain.CurrentDomain.BaseDirectory;
  461. parameters.ReferencedAssemblies.Add(Path.Combine(rootPath,
  462. "OpenSim.Region.ScriptEngine.Shared.dll"));
  463. parameters.ReferencedAssemblies.Add(Path.Combine(rootPath,
  464. "OpenSim.Region.ScriptEngine.Shared.Api.Runtime.dll"));
  465. parameters.ReferencedAssemblies.Add(Path.Combine(rootPath,
  466. "OpenMetaverseTypes.dll"));
  467. if (m_scriptEngine.ScriptReferencedAssemblies != null)
  468. Array.ForEach<string>(
  469. m_scriptEngine.ScriptReferencedAssemblies,
  470. a => parameters.ReferencedAssemblies.Add(Path.Combine(rootPath, a)));
  471. parameters.GenerateExecutable = false;
  472. parameters.OutputAssembly = assembly;
  473. parameters.IncludeDebugInformation = CompileWithDebugInformation;
  474. //parameters.WarningLevel = 1; // Should be 4?
  475. parameters.TreatWarningsAsErrors = false;
  476. parameters.GenerateInMemory = false;
  477. // parameters.TempFiles = new TempFileCollection(Path.Combine(ScriptEnginesPath,
  478. // m_scriptEngine.World.RegionInfo.RegionID.ToString()), CompileWithDebugInformation);
  479. CompilerResults results;
  480. CodeDomProvider provider;
  481. switch (lang)
  482. {
  483. case enumCompileType.vb:
  484. provider = CodeDomProvider.CreateProvider("VisualBasic");
  485. break;
  486. case enumCompileType.cs:
  487. case enumCompileType.lsl:
  488. provider = CodeDomProvider.CreateProvider("CSharp");
  489. break;
  490. default:
  491. throw new Exception("Compiler is not able to recongnize " +
  492. "language type \"" + lang.ToString() + "\"");
  493. }
  494. if(provider == null)
  495. throw new Exception("Compiler failed to load ");
  496. bool complete = false;
  497. bool retried = false;
  498. do
  499. {
  500. results = provider.CompileAssemblyFromSource(
  501. parameters, Script);
  502. // Deal with an occasional segv in the compiler.
  503. // Rarely, if ever, occurs twice in succession.
  504. // Line # == 0 and no file name are indications that
  505. // this is a native stack trace rather than a normal
  506. // error log.
  507. if (results.Errors.Count > 0)
  508. {
  509. if (!retried && string.IsNullOrEmpty(results.Errors[0].FileName) &&
  510. results.Errors[0].Line == 0)
  511. {
  512. // System.Console.WriteLine("retrying failed compilation");
  513. retried = true;
  514. }
  515. else
  516. {
  517. complete = true;
  518. }
  519. }
  520. else
  521. {
  522. complete = true;
  523. }
  524. } while (!complete);
  525. //
  526. // WARNINGS AND ERRORS
  527. //
  528. bool hadErrors = false;
  529. string errtext = String.Empty;
  530. if (results.Errors.Count > 0)
  531. {
  532. foreach (CompilerError CompErr in results.Errors)
  533. {
  534. string severity = CompErr.IsWarning ? "Warning" : "Error";
  535. KeyValuePair<int, int> errorPos;
  536. // Show 5 errors max, but check entire list for errors
  537. if (severity == "Error")
  538. {
  539. // C# scripts will not have a linemap since theres no line translation involved.
  540. if (!m_lineMaps.ContainsKey(assembly))
  541. errorPos = new KeyValuePair<int, int>(CompErr.Line, CompErr.Column);
  542. else
  543. errorPos = FindErrorPosition(CompErr.Line, CompErr.Column, m_lineMaps[assembly]);
  544. string text = CompErr.ErrorText;
  545. // Use LSL type names
  546. if (lang == enumCompileType.lsl)
  547. text = ReplaceTypes(CompErr.ErrorText);
  548. // The Second Life viewer's script editor begins
  549. // countingn lines and columns at 0, so we subtract 1.
  550. errtext += String.Format("({0},{1}): {4} {2}: {3}\n",
  551. errorPos.Key - 1, errorPos.Value - 1,
  552. CompErr.ErrorNumber, text, severity);
  553. hadErrors = true;
  554. }
  555. }
  556. }
  557. provider.Dispose();
  558. if (hadErrors)
  559. {
  560. throw new Exception(errtext);
  561. }
  562. // On today's highly asynchronous systems, the result of
  563. // the compile may not be immediately apparent. Wait a
  564. // reasonable amount of time before giving up on it.
  565. if (!File.Exists(assembly))
  566. {
  567. for (int i = 0; i < 20 && !File.Exists(assembly); i++)
  568. {
  569. System.Threading.Thread.Sleep(250);
  570. }
  571. // One final chance...
  572. if (!File.Exists(assembly))
  573. {
  574. errtext = String.Empty;
  575. errtext += "No compile error. But not able to locate compiled file.";
  576. throw new Exception(errtext);
  577. }
  578. }
  579. // m_log.DebugFormat("[Compiler] Compiled new assembly "+
  580. // "for {0}", asset);
  581. // Because windows likes to perform exclusive locks, we simply
  582. // write out a textual representation of the file here
  583. //
  584. // Read the binary file into a buffer
  585. //
  586. FileInfo fi = new FileInfo(assembly);
  587. if (fi == null)
  588. {
  589. errtext = String.Empty;
  590. errtext += "No compile error. But not able to stat file.";
  591. throw new Exception(errtext);
  592. }
  593. Byte[] data = new Byte[fi.Length];
  594. try
  595. {
  596. using (FileStream fs = File.Open(assembly, FileMode.Open, FileAccess.Read))
  597. fs.Read(data, 0, data.Length);
  598. }
  599. catch (Exception)
  600. {
  601. errtext = String.Empty;
  602. errtext += "No compile error. But not able to open file.";
  603. throw new Exception(errtext);
  604. }
  605. // Convert to base64
  606. //
  607. string filetext = System.Convert.ToBase64String(data);
  608. Byte[] buf = Encoding.ASCII.GetBytes(filetext);
  609. using (FileStream sfs = File.Create(assembly + ".text"))
  610. sfs.Write(buf, 0, buf.Length);
  611. return assembly;
  612. }
  613. private class kvpSorter : IComparer<KeyValuePair<KeyValuePair<int, int>, KeyValuePair<int, int>>>
  614. {
  615. public int Compare(KeyValuePair<KeyValuePair<int, int>, KeyValuePair<int, int>> a,
  616. KeyValuePair<KeyValuePair<int, int>, KeyValuePair<int, int>> b)
  617. {
  618. int kc = a.Key.Key.CompareTo(b.Key.Key);
  619. return (kc != 0) ? kc : a.Key.Value.CompareTo(b.Key.Value);
  620. }
  621. }
  622. public static KeyValuePair<int, int> FindErrorPosition(int line,
  623. int col, Dictionary<KeyValuePair<int, int>,
  624. KeyValuePair<int, int>> positionMap)
  625. {
  626. if (positionMap == null || positionMap.Count == 0)
  627. return new KeyValuePair<int, int>(line, col);
  628. KeyValuePair<int, int> ret = new KeyValuePair<int, int>();
  629. if (positionMap.TryGetValue(new KeyValuePair<int, int>(line, col),
  630. out ret))
  631. return ret;
  632. var sorted = new List<KeyValuePair<KeyValuePair<int, int>, KeyValuePair<int, int>>>(positionMap);
  633. sorted.Sort(new kvpSorter());
  634. int l = 1;
  635. int c = 1;
  636. int pl = 1;
  637. foreach (KeyValuePair<KeyValuePair<int, int>, KeyValuePair<int, int>> posmap in sorted)
  638. {
  639. //m_log.DebugFormat("[Compiler]: Scanning line map {0},{1} --> {2},{3}", posmap.Key.Key, posmap.Key.Value, posmap.Value.Key, posmap.Value.Value);
  640. int nl = posmap.Value.Key + line - posmap.Key.Key; // New, translated LSL line and column.
  641. int nc = posmap.Value.Value + col - posmap.Key.Value;
  642. // Keep going until we find the first point passed line,col.
  643. if (posmap.Key.Key > line)
  644. {
  645. //m_log.DebugFormat("[Compiler]: Line is larger than requested {0},{1}, returning {2},{3}", line, col, l, c);
  646. if (pl < line)
  647. {
  648. //m_log.DebugFormat("[Compiler]: Previous line ({0}) is less than requested line ({1}), setting column to 1.", pl, line);
  649. c = 1;
  650. }
  651. break;
  652. }
  653. if (posmap.Key.Key == line && posmap.Key.Value > col)
  654. {
  655. // Never move l,c backwards.
  656. if (nl > l || (nl == l && nc > c))
  657. {
  658. //m_log.DebugFormat("[Compiler]: Using offset relative to this: {0} + {1} - {2}, {3} + {4} - {5} = {6}, {7}",
  659. // posmap.Value.Key, line, posmap.Key.Key, posmap.Value.Value, col, posmap.Key.Value, nl, nc);
  660. l = nl;
  661. c = nc;
  662. }
  663. //m_log.DebugFormat("[Compiler]: Column is larger than requested {0},{1}, returning {2},{3}", line, col, l, c);
  664. break;
  665. }
  666. pl = posmap.Key.Key;
  667. l = posmap.Value.Key;
  668. c = posmap.Value.Value;
  669. }
  670. return new KeyValuePair<int, int>(l, c);
  671. }
  672. string ReplaceTypes(string message)
  673. {
  674. message = message.Replace(
  675. "OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString",
  676. "string");
  677. message = message.Replace(
  678. "OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger",
  679. "integer");
  680. message = message.Replace(
  681. "OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat",
  682. "float");
  683. message = message.Replace(
  684. "OpenSim.Region.ScriptEngine.Shared.LSL_Types.list",
  685. "list");
  686. return message;
  687. }
  688. private static void WriteMapFile(string filename, Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> linemap)
  689. {
  690. StringBuilder mapbuilder = new StringBuilder(1024);
  691. foreach (KeyValuePair<KeyValuePair<int, int>, KeyValuePair<int, int>> kvp in linemap)
  692. {
  693. KeyValuePair<int, int> k = kvp.Key;
  694. KeyValuePair<int, int> v = kvp.Value;
  695. mapbuilder.Append(String.Format("{0},{1},{2},{3}\n", k.Key, k.Value, v.Key, v.Value));
  696. }
  697. Byte[] mapbytes = Encoding.ASCII.GetBytes(mapbuilder.ToString());
  698. using (FileStream mfs = File.Create(filename))
  699. mfs.Write(mapbytes, 0, mapbytes.Length);
  700. }
  701. private static Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> ReadMapFile(string filename)
  702. {
  703. Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> linemap;
  704. try
  705. {
  706. using (StreamReader r = File.OpenText(filename))
  707. {
  708. linemap = new Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>>();
  709. string line;
  710. while ((line = r.ReadLine()) != null)
  711. {
  712. String[] parts = line.Split(new Char[] { ',' });
  713. int kk = System.Convert.ToInt32(parts[0]);
  714. int kv = System.Convert.ToInt32(parts[1]);
  715. int vk = System.Convert.ToInt32(parts[2]);
  716. int vv = System.Convert.ToInt32(parts[3]);
  717. KeyValuePair<int, int> k = new KeyValuePair<int, int>(kk, kv);
  718. KeyValuePair<int, int> v = new KeyValuePair<int, int>(vk, vv);
  719. linemap[k] = v;
  720. }
  721. }
  722. }
  723. catch
  724. {
  725. linemap = new Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>>();
  726. }
  727. return linemap;
  728. }
  729. }
  730. }