ScriptLoaderBase.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. namespace MoonSharp.Interpreter.Loaders
  7. {
  8. public abstract class ScriptLoaderBase : IScriptLoader
  9. {
  10. /// <summary>
  11. /// Checks if a script file exists.
  12. /// </summary>
  13. /// <param name="name">The script filename.</param>
  14. /// <returns></returns>
  15. public abstract bool ScriptFileExists(string name);
  16. /// <summary>
  17. /// Opens a file for reading the script code.
  18. /// It can return either a string, a byte[] or a Stream.
  19. /// If a byte[] is returned, the content is assumed to be a serialized (dumped) bytecode. If it's a string, it's
  20. /// assumed to be either a script or the output of a string.dump call. If a Stream, autodetection takes place.
  21. /// </summary>
  22. /// <param name="file">The file.</param>
  23. /// <param name="globalContext">The global context.</param>
  24. /// <returns>
  25. /// A string, a byte[] or a Stream.
  26. /// </returns>
  27. public abstract object LoadFile(string file, Table globalContext);
  28. /// <summary>
  29. /// Resolves the name of a module on a set of paths.
  30. /// </summary>
  31. /// <param name="modname">The modname.</param>
  32. /// <param name="paths">The paths.</param>
  33. /// <returns></returns>
  34. protected virtual string ResolveModuleName(string modname, string[] paths)
  35. {
  36. modname = modname.Replace('.', '/');
  37. foreach (string path in paths)
  38. {
  39. string file = path.Replace("?", modname);
  40. if (ScriptFileExists(file))
  41. return file;
  42. }
  43. return null;
  44. }
  45. /// <summary>
  46. /// Resolves the name of a module to a filename (which will later be passed to OpenScriptFile).
  47. /// The resolution happens first on paths included in the LUA_PATH global variable, and -
  48. /// if the variable does not exist - by consulting the
  49. /// ScriptOptions.ModulesPaths array. Override to provide a different behaviour.
  50. /// </summary>
  51. /// <param name="modname">The modname.</param>
  52. /// <param name="globalContext">The global context.</param>
  53. /// <returns></returns>
  54. public virtual string ResolveModuleName(string modname, Table globalContext)
  55. {
  56. DynValue s = globalContext.RawGet("LUA_PATH");
  57. if (s != null && s.Type == DataType.String)
  58. return ResolveModuleName(modname, UnpackStringPaths(s.String));
  59. return ResolveModuleName(modname, this.ModulePaths);
  60. }
  61. /// <summary>
  62. /// Gets or sets the modules paths used by the "require" function. If null, the default paths are used (using
  63. /// environment variables etc.).
  64. /// </summary>
  65. public string[] ModulePaths { get; set; }
  66. /// <summary>
  67. /// Unpacks a string path in a form like "?;?.lua" to an array
  68. /// </summary>
  69. public static string[] UnpackStringPaths(string str)
  70. {
  71. return str.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
  72. .Select(s => s.Trim())
  73. .Where(s => !string.IsNullOrEmpty(s))
  74. .ToArray();
  75. }
  76. /// <summary>
  77. /// Gets the default environment paths.
  78. /// </summary>
  79. public static string[] GetDefaultEnvironmentPaths()
  80. {
  81. string[] modulePaths = null;
  82. if (modulePaths == null)
  83. {
  84. string env = Script.Platform.GetEnvironmentVariable("MOONSHARP_PATH");
  85. if (!string.IsNullOrEmpty(env)) modulePaths = UnpackStringPaths(env);
  86. if (modulePaths == null)
  87. {
  88. env = Script.Platform.GetEnvironmentVariable("LUA_PATH");
  89. if (!string.IsNullOrEmpty(env)) modulePaths = UnpackStringPaths(env);
  90. }
  91. if (modulePaths == null)
  92. modulePaths = UnpackStringPaths("?;?.lua");
  93. }
  94. return modulePaths;
  95. }
  96. /// <summary>
  97. /// Resolves a filename [applying paths, etc.]
  98. /// </summary>
  99. /// <param name="filename">The filename.</param>
  100. /// <param name="globalContext">The global context.</param>
  101. /// <returns></returns>
  102. public virtual string ResolveFileName(string filename, Table globalContext)
  103. {
  104. return filename;
  105. }
  106. }
  107. }