using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace MoonSharp.Interpreter.Loaders { public abstract class ScriptLoaderBase : IScriptLoader { /// /// Checks if a script file exists. /// /// The script filename. /// public abstract bool ScriptFileExists(string name); /// /// Opens a file for reading the script code. /// It can return either a string, a byte[] or a Stream. /// If a byte[] is returned, the content is assumed to be a serialized (dumped) bytecode. If it's a string, it's /// assumed to be either a script or the output of a string.dump call. If a Stream, autodetection takes place. /// /// The file. /// The global context. /// /// A string, a byte[] or a Stream. /// public abstract object LoadFile(string file, Table globalContext); /// /// Resolves the name of a module on a set of paths. /// /// The modname. /// The paths. /// protected virtual string ResolveModuleName(string modname, string[] paths) { modname = modname.Replace('.', '/'); foreach (string path in paths) { string file = path.Replace("?", modname); if (ScriptFileExists(file)) return file; } return null; } /// /// Resolves the name of a module to a filename (which will later be passed to OpenScriptFile). /// The resolution happens first on paths included in the LUA_PATH global variable, and - /// if the variable does not exist - by consulting the /// ScriptOptions.ModulesPaths array. Override to provide a different behaviour. /// /// The modname. /// The global context. /// public virtual string ResolveModuleName(string modname, Table globalContext) { DynValue s = globalContext.RawGet("LUA_PATH"); if (s != null && s.Type == DataType.String) return ResolveModuleName(modname, UnpackStringPaths(s.String)); return ResolveModuleName(modname, this.ModulePaths); } /// /// Gets or sets the modules paths used by the "require" function. If null, the default paths are used (using /// environment variables etc.). /// public string[] ModulePaths { get; set; } /// /// Unpacks a string path in a form like "?;?.lua" to an array /// public static string[] UnpackStringPaths(string str) { return str.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Trim()) .Where(s => !string.IsNullOrEmpty(s)) .ToArray(); } /// /// Gets the default environment paths. /// public static string[] GetDefaultEnvironmentPaths() { string[] modulePaths = null; if (modulePaths == null) { string env = Script.Platform.GetEnvironmentVariable("MOONSHARP_PATH"); if (!string.IsNullOrEmpty(env)) modulePaths = UnpackStringPaths(env); if (modulePaths == null) { env = Script.Platform.GetEnvironmentVariable("LUA_PATH"); if (!string.IsNullOrEmpty(env)) modulePaths = UnpackStringPaths(env); } if (modulePaths == null) modulePaths = UnpackStringPaths("?;?.lua"); } return modulePaths; } /// /// Resolves a filename [applying paths, etc.] /// /// The filename. /// The global context. /// public virtual string ResolveFileName(string filename, Table globalContext) { return filename; } } }