Track LocalMods as part of monolith
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
-- Functions for interfacing with Barotrauma.
|
||||
|
||||
local Environment = require 'ConsentRequiredExtended.Util.Environment'
|
||||
local _ENV = Environment.PrepareEnvironment(_ENV)
|
||||
|
||||
-- local Clr = require 'ConsentRequiredExtended.Util.Clr'
|
||||
-- local UserData = require 'ConsentRequiredExtended.Util.UserData'
|
||||
|
||||
---Functions related to working with Barotrauma.AttackResult.
|
||||
AttackResult = {}
|
||||
|
||||
---Initialise AttackResults.
|
||||
-- local function Init_AttackResult()
|
||||
-- -- Registrations.
|
||||
-- UserData.RegisterStandardType("System.Reflection.FieldInfo")
|
||||
|
||||
-- -- Construct a List<Affliction> generic type.
|
||||
-- local afflictionsListClrType = Clr.CreateConstructedGenericType("System.Collections.Generic.List`1", "Barotrauma.Affliction")
|
||||
-- local attackResultAfflictionsField = Clr.GetRawClrType("Barotrauma.AttackResult").GetField("Afflictions")
|
||||
|
||||
-- ---Instantiates a new AttackResult with damage and empty afflictions.
|
||||
-- ---@param damage number An amount of damage.
|
||||
-- function AttackResult.NewAttackResultFromDamage(damage)
|
||||
-- -- Instantiate a new AttackResult.
|
||||
-- local attackResult = _G.AttackResult(damage, nil)
|
||||
|
||||
-- -- Instantiate an empty List<Afflictions> (this is to prevent NREs),
|
||||
-- -- and set it to attackResult.Afflictions. This is a readonly field,
|
||||
-- -- hence the use of reflection.
|
||||
-- local afflictionsList = UserData.FromClrType({}, afflictionsListClrType)
|
||||
-- attackResultAfflictionsField.SetValue(attackResult, afflictionsList)
|
||||
|
||||
-- return attackResult
|
||||
-- end
|
||||
-- end
|
||||
|
||||
---Initialise AttackResults without needing to register system.type and reflections
|
||||
local function Init_AttackResult()
|
||||
-- Registrations.
|
||||
LuaUserData.MakePropertyAccessible(Descriptors['Barotrauma.AttackResult'], 'Damage')
|
||||
|
||||
---Instantiates a new AttackResult with damage and empty afflictions.
|
||||
---@param damage number An amount of damage.
|
||||
function AttackResult.NewAttackResultFromDamage(damage)
|
||||
-- I have not noticed any NREs from affliction list being null
|
||||
-- but just in case here is version which intializes with empty list
|
||||
-- Also uncomment MakePropertyAccessible Damage above
|
||||
local attackResult = _G.AttackResult({}, nil, {})
|
||||
attackResult.Damage = damage
|
||||
|
||||
--local attackResult = _G.AttackResult(damage)
|
||||
|
||||
return attackResult
|
||||
end
|
||||
end
|
||||
|
||||
---Runs at start-up, handles registrations, etc.
|
||||
function Init()
|
||||
Init_AttackResult()
|
||||
end
|
||||
|
||||
function Test()
|
||||
local errors = {}
|
||||
local function AssertEquals(testDescription, expected, got)
|
||||
if expected ~= got then
|
||||
local errorString = string.format(
|
||||
"Test Error: %s\n\texpected = %s\n\tgot = %s",
|
||||
testDescription,
|
||||
tostring(expected),
|
||||
tostring(got)
|
||||
)
|
||||
table.insert(errors, errorString)
|
||||
end
|
||||
end
|
||||
local atkRes = AttackResult.NewAttackResultFromDamage(10)
|
||||
AssertEquals("atkRes.Damage", 10, atkRes.Damage)
|
||||
AssertEquals("atkRes.Afflictions is null", true, atkRes.Afflictions ~= nil)
|
||||
AssertEquals("#atkRes.Afflictions is non-zero", 0, #atkRes.Afflictions)
|
||||
|
||||
if #errors == 0 then
|
||||
print("Tests successful")
|
||||
else
|
||||
for _, err in pairs(errors) do
|
||||
print(err)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Init()
|
||||
return Environment.Export(_ENV)
|
||||
@@ -0,0 +1,74 @@
|
||||
-- Functions for working with CLR types.
|
||||
|
||||
local Environment = require 'ConsentRequiredExtended.Util.Environment'
|
||||
local _ENV = Environment.PrepareEnvironment(_ENV)
|
||||
|
||||
local UserData = require 'ConsentRequiredExtended.Util.UserData'
|
||||
|
||||
---Construct ClrType wrapper table for CLR types.
|
||||
---@param underlyingType userdata The underlying type (CLR type: System.Type).
|
||||
---@return ClrType Wrapper table for working with CLR types.
|
||||
local function New(underlyingType)
|
||||
---@class ClrType
|
||||
local clrType = {}
|
||||
|
||||
---Instantiate an object for the given CLR type.
|
||||
---@return any An instance of the CLR type. Actual Lua type depends on MoonSharp conversions.
|
||||
function clrType:Instantiate()
|
||||
-- TODO: Implement args.
|
||||
return underlyingType.Assembly.CreateInstance(underlyingType.FullName)
|
||||
end
|
||||
|
||||
---Get the underlying type as a raw CLR type object.
|
||||
---@return userdata The raw underlying type (CLR type: System.Type).
|
||||
function clrType:GetUnderlyingType()
|
||||
return underlyingType
|
||||
end
|
||||
|
||||
---Get the full name of the CLR type.
|
||||
---@return string The full name of the type.
|
||||
function clrType:GetFullName()
|
||||
return underlyingType.FullName
|
||||
end
|
||||
|
||||
return clrType
|
||||
end
|
||||
|
||||
---Create a constructed generic type.
|
||||
---@param genericTypeName string The name of the generic type to construct and register.
|
||||
---@vararg string
|
||||
---@return ClrType The constructed generic type.
|
||||
function CreateConstructedGenericType(genericTypeName, ...)
|
||||
local genericTypeArgumentsString = table.pack(...)
|
||||
|
||||
local genericTypeArgumentsType = {}
|
||||
for _, typeString in pairs(genericTypeArgumentsString) do
|
||||
table.insert(genericTypeArgumentsType, GetRawClrType(typeString))
|
||||
end
|
||||
|
||||
local genericTypeDefinition = GetRawClrType(genericTypeName)
|
||||
local constructedGenericType = genericTypeDefinition.MakeGenericType(table.unpack(genericTypeArgumentsType))
|
||||
return New(constructedGenericType)
|
||||
end
|
||||
|
||||
---Get a System.Type object from a type name.
|
||||
---@param typeName string Name of the type.
|
||||
---@return userdata The type object (CLR type: System.Type).
|
||||
function GetRawClrType(typeName)
|
||||
return LuaUserData.GetType(typeName)
|
||||
end
|
||||
|
||||
---Get a ClrType instance wrapping a Type object that matches the type name.
|
||||
---@param typeName string Name of the type.
|
||||
---@return ClrType The CLR type.
|
||||
function GetClrType(typeName)
|
||||
return New(GetRawClrType(typeName))
|
||||
end
|
||||
|
||||
local function Init()
|
||||
UserData.RegisterStandardType("System.Type")
|
||||
UserData.RegisterStandardType("System.Reflection.RuntimeAssembly")
|
||||
end
|
||||
|
||||
Init()
|
||||
return Environment.Export(_ENV)
|
||||
@@ -0,0 +1,32 @@
|
||||
-- Functions for managing the mod's environment.
|
||||
|
||||
---Isolate a function or module's environment from Global.
|
||||
---@param env table The _ENV table.
|
||||
local function prepareEnvironment(env)
|
||||
return setmetatable(
|
||||
{},
|
||||
{
|
||||
__index = _G,
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
local _ENV = prepareEnvironment(_ENV)
|
||||
|
||||
PrepareEnvironment = prepareEnvironment
|
||||
|
||||
---Create an empty table whose metatable indexes non-local variables declared within
|
||||
---a function/module's environment, and is immutable to any changes.
|
||||
---@param env table The _ENV table.
|
||||
---@return table Empty table that interfaces with _ENV.
|
||||
function Export(env)
|
||||
return setmetatable(
|
||||
{},
|
||||
{
|
||||
__index = function(t, k) return env[k] end,
|
||||
__newindex = function() error("Attempted to modify a protected table.") end
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
return Export(_ENV)
|
||||
@@ -0,0 +1,35 @@
|
||||
-- Functions for creating userdata.
|
||||
|
||||
local Environment = require 'ConsentRequiredExtended.Util.Environment'
|
||||
local _ENV = Environment.PrepareEnvironment(_ENV)
|
||||
|
||||
---Create a userdata that references the type in a static context.
|
||||
---@param clrTypeName string The name of the type to point to.
|
||||
---@return userdata A userdata that references the type in a static context.
|
||||
function FromStringStatic(clrTypeName)
|
||||
LuaUserData.CreateStatic(clrTypeName)
|
||||
end
|
||||
|
||||
---Create a userdata that references the type described by a ClrType in a static context.
|
||||
---@param clrType ClrType ClrType that describes the type being referenced.
|
||||
---@return userdata A userdata that references the type in a static context.
|
||||
function FromClrTypeStatic(clrType)
|
||||
return FromStringStatic(clrType:GetFullName())
|
||||
end
|
||||
|
||||
---Create a userdata that references an instance of a CLR type with conversion.
|
||||
---@param value any A Lua value to convert to a CLR object of the given type and wrap up in a userdata.
|
||||
---@param clrType ClrType The CLR type to instantiate and wrap in the userdata.
|
||||
---@return userdata A userdata that references the an instance of the CLR type.
|
||||
function FromClrType(value, clrType)
|
||||
return LuaUserData.CreateUserDataFromType(value, clrType:GetUnderlyingType())
|
||||
end
|
||||
|
||||
---Register standard types with MoonSharp. For generics use RegisterClrType.ConstructedGenericType.
|
||||
---@param typeName string Name of the type to register.
|
||||
function RegisterStandardType(typeName)
|
||||
local desc = LuaUserData.RegisterType(typeName)
|
||||
_G.Descriptors[typeName] = desc
|
||||
end
|
||||
|
||||
return Environment.Export(_ENV)
|
||||
Reference in New Issue
Block a user