Compiler.cs 34 KB

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