Move partial classes to extension methods, the ones that can't, turn into Lua compatibility members

This commit is contained in:
Evil Factory
2026-03-28 12:51:53 -03:00
parent 2ea97d3d5e
commit 5bfa15564a
9 changed files with 226 additions and 224 deletions
@@ -1,8 +1,11 @@
using Barotrauma.LuaCs.Data;
using Barotrauma.Networking;
using MoonSharp.Interpreter;
using MoonSharp.Interpreter.Interop;
using MoonSharp.Interpreter.Interop.BasicDescriptors;
using Sigil;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Numerics;
using System.Reflection;
@@ -23,6 +26,30 @@ public class DefaultLuaRegistrar : IDefaultLuaRegistrar
private readonly ISafeLuaUserDataService _safeUserDataService;
private readonly ILoggerService _loggerService;
private class SteamIDMemberDescriptor : IMemberDescriptor
{
public bool IsStatic => false;
public string Name => "SteamID";
public MemberDescriptorAccess MemberAccess => MemberDescriptorAccess.CanRead;
public DynValue GetValue(Script script, object obj)
{
if (obj is Client client)
{
return DynValue.FromObject(script, ModUtils.Client.GetSteamId(client));
}
throw new System.NotImplementedException();
}
public void SetValue(Script script, object obj, DynValue value)
{
throw new System.NotImplementedException();
}
}
public DefaultLuaRegistrar(ILoggerService loggerService, ILuaUserDataService userDataService, ISafeLuaUserDataService safeUserDataService)
{
_userDataService = userDataService;
@@ -152,6 +179,28 @@ public class DefaultLuaRegistrar : IDefaultLuaRegistrar
_userDataService.RegisterType("Barotrauma.PrefabSelector`1");
_userDataService.RegisterType("Barotrauma.Pair`2");
_userDataService.RegisterExtensionType("Barotrauma.MathUtils");
_userDataService.RegisterExtensionType("Barotrauma.XMLExtensions");
var itemDescriptor = (StandardUserDataDescriptor)_userDataService.RegisterType("Barotrauma.Item");
itemDescriptor.AddMember("GetItemPrefab", new MethodMemberDescriptor(typeof(ModUtils.Item).GetMethod(nameof(ModUtils.Item.GetItemPrefab), BindingFlags.NonPublic | BindingFlags.Static)));
var clientDescriptor = (StandardUserDataDescriptor)_userDataService.RegisterType("Barotrauma.Networking.Client");
clientDescriptor.AddMember("ClientList", new PropertyMemberDescriptor(typeof(ModUtils.Client).GetProperty(nameof(ModUtils.Client.ClientList), BindingFlags.NonPublic | BindingFlags.Static), InteropAccessMode.LazyOptimized));
clientDescriptor.AddMember("SteamID", new SteamIDMemberDescriptor());
#if SERVER
clientDescriptor.AddMember("UnbanPlayer", new MethodMemberDescriptor(typeof(ModUtils.Client).GetMethod(nameof(ModUtils.Client.UnbanPlayer), BindingFlags.NonPublic | BindingFlags.Static), InteropAccessMode.LazyOptimized));
clientDescriptor.AddMember("BanPlayer", new MethodMemberDescriptor(typeof(ModUtils.Client).GetMethod(nameof(ModUtils.Client.BanPlayer), BindingFlags.NonPublic | BindingFlags.Static), InteropAccessMode.LazyOptimized));
#endif
_userDataService.RegisterExtensionType(typeof(ClientExtensions).FullName);
_userDataService.RegisterExtensionType(typeof(ItemExtensions).FullName);
_userDataService.RegisterExtensionType(typeof(MapEntityExtensions).FullName);
_userDataService.RegisterExtensionType(typeof(QualityExtensions).FullName);
var toolBox = UserData.RegisterType(typeof(ToolBox));
#if CLIENT
_userDataService.RemoveMember(toolBox, "OpenFileWithShell");
@@ -1,110 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
using MoonSharp.Interpreter;
using Microsoft.Xna.Framework;
using Barotrauma.Networking;
namespace Barotrauma.Networking
{
partial class Client
{
public static IReadOnlyList<Client> ClientList
{
get
{
if (GameMain.IsSingleplayer) { return new List<Client>(); }
#if SERVER
return GameMain.Server.ConnectedClients;
#else
return GameMain.Client.ConnectedClients;
#endif
}
}
public ulong SteamID
{
get
{
if (AccountId.TryUnwrap(out AccountId outValue) && outValue is SteamId steamId)
{
return steamId.Value;
}
else
{
return 0;
}
}
}
}
}
namespace Barotrauma
{
using Barotrauma.Networking;
using System.Linq;
using System.Reflection;
partial class Character
{
}
partial class Item
{
public object GetComponentString(string component)
{
Type type = LuaCsSetup.Instance.PluginManagementService.GetType("Barotrauma.Items.Components." + component);
if (type == null)
{
return null;
}
MethodInfo method = typeof(Item).GetMethod(nameof(Item.GetComponent));
MethodInfo generic = method.MakeGenericMethod(type);
return generic.Invoke(this, null);
}
}
partial class ItemPrefab
{
public static ItemPrefab GetItemPrefab(string itemNameOrId)
{
ItemPrefab itemPrefab =
(MapEntityPrefab.Find(itemNameOrId, identifier: null, showErrorMessages: false) ??
MapEntityPrefab.Find(null, identifier: itemNameOrId, showErrorMessages: false)) as ItemPrefab;
return itemPrefab;
}
}
abstract partial class MapEntity
{
public void AddLinked(MapEntity entity)
{
linkedTo.Add(entity);
}
}
}
namespace Barotrauma.Items.Components
{
using Barotrauma.Networking;
partial class CustomInterface
{
}
partial struct Signal
{
}
}
@@ -322,10 +322,11 @@ public class LuaUserDataService : ILuaUserDataService
descriptor.RemoveMember(methodName);
descriptor.AddMember(methodName, new ObjectCallbackMemberDescriptor(methodName, (object arg1, ScriptExecutionContext arg2, CallbackArguments arg3) =>
{
/*if (GameMain.LuaCs != null)
return GameMain.LuaCs.CallLuaFunction(function, arg3.GetArray());
return null;*/
throw new NotImplementedException();
if (LuaCsSetup.Instance != null)
{
return LuaCsSetup.Instance.CallLuaFunction(function, arg3.GetArray());
}
return null;
}));
}