Compiler.cs 37 KB

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