4 using System.Collections.Generic;
6 using MoonSharp.Interpreter;
12 private Script lua {
get;
set; }
13 private Dictionary<string, DynValue> loadedModules {
get;
set; }
15 private bool GetExistingReturnValue(
string moduleName, ref DynValue returnValue)
17 return loadedModules.TryGetValue(
23 private string FixContentPackagePath(
string contentPackagePath)
25 contentPackagePath = Path.TrimEndingDirectorySeparator(
26 new FileInfo(contentPackagePath)
29 .CleanUpPathCrossPlatform()
32 return contentPackagePath;
34 private string GetContentPackagePath(
string path)
36 IEnumerable<ContentPackage> allContentPackages = ContentPackageManager.AllPackages;
39 string contentPackagePath = FixContentPackagePath(contentPackage.
Path);
40 if (path.StartsWith(contentPackagePath))
42 return contentPackagePath;
51 private string GetContentPackagePath(
string moduleName, Table environment)
53 string filePath = lua.Options
59 filePath = Path.TrimEndingDirectorySeparator(
60 new FileInfo(filePath)
63 .CleanUpPathCrossPlatform()
66 return GetContentPackagePath(filePath);
69 private void SaveReturnValue(
string moduleName, DynValue returnValue)
71 loadedModules[moduleName] = returnValue;
74 private void ExecuteModule(
string moduleName, Table environment, ref DynValue returnValue)
76 DynValue loadFunc = lua.RequireModule(
80 string packagePath = GetContentPackagePath(
85 returnValue = lua.Call(
95 public DynValue
Require(
string moduleName, Table globalContext)
97 DynValue returnValue =
null;
98 Table environment = globalContext ?? lua.Globals;
100 if (GetExistingReturnValue(moduleName, ref returnValue))
103 ExecuteModule(moduleName, environment, ref returnValue);
106 || returnValue.IsNil()
107 || returnValue.IsVoid()
109 returnValue = DynValue.NewBoolean(
true);
110 SaveReturnValue(moduleName, returnValue);
117 loadedModules =
new Dictionary<string, DynValue>();
DynValue Require(string moduleName, Table globalContext)