rebase to origin/unstable-tests
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using MoonSharp.Interpreter;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
class LuaRequire {
|
||||
private Script lua { get; set; }
|
||||
private Dictionary<Tuple<string, Table>, DynValue> LoadedModules { get; set; }
|
||||
|
||||
private bool GetExistingReturnValue(Tuple<string, Table> key, ref DynValue returnValue) {
|
||||
return LoadedModules.TryGetValue(key, out returnValue);
|
||||
}
|
||||
|
||||
private void ExecuteModule(Tuple<string, Table> key, ref DynValue returnValue) {
|
||||
string moduleName = key.Item1;
|
||||
Table globalContext = key.Item2;
|
||||
returnValue = lua.Call(lua.RequireModule(moduleName, globalContext));
|
||||
LoadedModules[key] = returnValue;
|
||||
}
|
||||
|
||||
// Lua modules that have been previously loaded by require() will
|
||||
// not be loaded again; instead, their initial return value is
|
||||
// preserved and returned again on subsequent attempts.
|
||||
public DynValue Require(string moduleName, Table globalContext) {
|
||||
DynValue returnValue = DynValue.Nil;
|
||||
var key = new Tuple<string, Table>(moduleName, globalContext);
|
||||
|
||||
if (GetExistingReturnValue(key, ref returnValue)) return returnValue;
|
||||
ExecuteModule(key, ref returnValue);
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
public LuaRequire(Script lua) {
|
||||
this.lua = lua;
|
||||
LoadedModules = new Dictionary<Tuple<string, Table>, DynValue>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,7 @@ namespace Barotrauma
|
||||
public LuaCsHook Hook { get; private set; }
|
||||
public LuaCsNetworking Networking { get; private set; }
|
||||
public LuaCsModStore ModStore { get; private set; }
|
||||
private LuaRequire require { get; set; }
|
||||
|
||||
public CsScriptLoader CsScriptLoader { get; private set; }
|
||||
public CsLua Lua { get; private set; }
|
||||
@@ -71,9 +72,9 @@ namespace Barotrauma
|
||||
}
|
||||
|
||||
|
||||
public static ContentPackage GetPackage(Identifier name)
|
||||
public static ContentPackage GetPackage(Identifier name, bool fallbackToAll = true)
|
||||
{
|
||||
foreach (ContentPackage package in ContentPackageManager.LocalPackages)
|
||||
foreach (ContentPackage package in ContentPackageManager.EnabledPackages.All)
|
||||
{
|
||||
if (package.NameMatches(name))
|
||||
{
|
||||
@@ -81,11 +82,22 @@ namespace Barotrauma
|
||||
}
|
||||
}
|
||||
|
||||
foreach (ContentPackage package in ContentPackageManager.AllPackages)
|
||||
if (fallbackToAll)
|
||||
{
|
||||
if (package.NameMatches(name))
|
||||
foreach (ContentPackage package in ContentPackageManager.LocalPackages)
|
||||
{
|
||||
return package;
|
||||
if (package.NameMatches(name))
|
||||
{
|
||||
return package;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (ContentPackage package in ContentPackageManager.AllPackages)
|
||||
{
|
||||
if (package.NameMatches(name))
|
||||
{
|
||||
return package;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -264,22 +276,18 @@ namespace Barotrauma
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private DynValue Require(string modname, Table globalContext)
|
||||
public DynValue Require(string moduleName, Table globalContexts)
|
||||
{
|
||||
try
|
||||
{
|
||||
return lua.Call(lua.RequireModule(modname, globalContext));
|
||||
|
||||
return require.Require(moduleName, globalContexts);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
HandleException(e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public object CallLuaFunction(object function, params object[] arguments)
|
||||
{
|
||||
try
|
||||
@@ -362,6 +370,8 @@ namespace Barotrauma
|
||||
Lua = new CsLua(this);
|
||||
CsScript = new CsScriptRunner(this);
|
||||
|
||||
require = new LuaRequire(lua);
|
||||
|
||||
Game = new LuaGame();
|
||||
Networking = new LuaCsNetworking();
|
||||
Hook.Initialize();
|
||||
|
||||
Reference in New Issue
Block a user