Compiler.cs 36 KB

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