Client LuaCsForBarotrauma
LuaRequire.cs
1 
2 
3 using System;
4 using System.Collections.Generic;
5 using System.IO;
6 using MoonSharp.Interpreter;
7 
8 namespace Barotrauma
9 {
10  class LuaRequire
11  {
12  private Script lua { get; set; }
13  private Dictionary<string, DynValue> loadedModules { get; set; }
14 
15  private bool GetExistingReturnValue(string moduleName, ref DynValue returnValue)
16  {
17  return loadedModules.TryGetValue(
18  moduleName,
19  out returnValue
20  );
21  }
22 
23  private string FixContentPackagePath(string contentPackagePath)
24  {
25  contentPackagePath = Path.TrimEndingDirectorySeparator(
26  new FileInfo(contentPackagePath) // filelist.xml
27  .Directory
28  .FullName
29  .CleanUpPathCrossPlatform()
30  );
31 
32  return contentPackagePath;
33  }
34  private string GetContentPackagePath(string path)
35  {
36  IEnumerable<ContentPackage> allContentPackages = ContentPackageManager.AllPackages;
37  foreach (ContentPackage contentPackage in allContentPackages)
38  {
39  string contentPackagePath = FixContentPackagePath(contentPackage.Path);
40  if (path.StartsWith(contentPackagePath))
41  {
42  return contentPackagePath;
43  }
44  }
45 
46  // Return null if we can't find a content package that
47  // this module belongs to.
48  return null;
49  }
50 
51  private string GetContentPackagePath(string moduleName, Table environment)
52  {
53  string filePath = lua.Options
54  .ScriptLoader
55  .ResolveModuleName(
56  moduleName,
57  environment
58  );
59  filePath = Path.TrimEndingDirectorySeparator(
60  new FileInfo(filePath)
61  .Directory
62  .FullName
63  .CleanUpPathCrossPlatform()
64  );
65 
66  return GetContentPackagePath(filePath);
67  }
68 
69  private void SaveReturnValue(string moduleName, DynValue returnValue)
70  {
71  loadedModules[moduleName] = returnValue;
72  }
73 
74  private void ExecuteModule(string moduleName, Table environment, ref DynValue returnValue)
75  {
76  DynValue loadFunc = lua.RequireModule(
77  moduleName,
78  environment
79  );
80  string packagePath = GetContentPackagePath(
81  moduleName,
82  environment
83  );
84 
85  returnValue = lua.Call(
86  loadFunc,
87  packagePath
88  );
89 
90  }
91 
92  // Lua modules that have been previously loaded by require() will
93  // not be loaded again; instead, their initial return value is
94  // preserved and returned again on subsequent attempts.
95  public DynValue Require(string moduleName, Table globalContext)
96  {
97  DynValue returnValue = null;
98  Table environment = globalContext ?? lua.Globals;
99 
100  if (GetExistingReturnValue(moduleName, ref returnValue))
101  return returnValue;
102 
103  ExecuteModule(moduleName, environment, ref returnValue);
104  if (
105  returnValue == null
106  || returnValue.IsNil()
107  || returnValue.IsVoid()
108  )
109  returnValue = DynValue.NewBoolean(true);
110  SaveReturnValue(moduleName, returnValue);
111  return returnValue;
112  }
113 
114  public LuaRequire(Script lua)
115  {
116  this.lua = lua;
117  loadedModules = new Dictionary<string, DynValue>();
118  }
119  }
120 }
DynValue Require(string moduleName, Table globalContext)
Definition: LuaRequire.cs:95
LuaRequire(Script lua)
Definition: LuaRequire.cs:114