Move partial classes to extension methods, the ones that can't, turn into Lua compatibility members
This commit is contained in:
@@ -1,107 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
partial class Client
|
||||
{
|
||||
public void SetClientCharacter(Character character)
|
||||
{
|
||||
GameMain.Server.SetClientCharacter(this, character);
|
||||
}
|
||||
|
||||
public void Kick(string reason = "")
|
||||
{
|
||||
GameMain.Server.KickClient(this.Connection, reason);
|
||||
}
|
||||
|
||||
public void Ban(string reason = "", float seconds = -1)
|
||||
{
|
||||
if (seconds == -1)
|
||||
{
|
||||
GameMain.Server.BanClient(this, reason, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
GameMain.Server.BanClient(this, reason, TimeSpan.FromSeconds(seconds));
|
||||
}
|
||||
}
|
||||
|
||||
public static void UnbanPlayer(string playerName)
|
||||
{
|
||||
GameMain.Server.UnbanPlayer(playerName);
|
||||
}
|
||||
|
||||
public static void BanPlayer(string player, string reason, bool range = false, float seconds = -1)
|
||||
{
|
||||
if (seconds == -1)
|
||||
{
|
||||
GameMain.Server.BanPlayer(player, reason, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
GameMain.Server.BanPlayer(player, reason, TimeSpan.FromSeconds(seconds));
|
||||
}
|
||||
}
|
||||
|
||||
public bool CheckPermission(ClientPermissions permissions)
|
||||
{
|
||||
return this.Permissions.HasFlag(permissions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
using Microsoft.Xna.Framework;
|
||||
using System.Reflection;
|
||||
|
||||
partial class Item
|
||||
{
|
||||
public object CreateServerEventString(string component)
|
||||
{
|
||||
var comp = GetComponentString(component);
|
||||
|
||||
if (comp == null)
|
||||
return null;
|
||||
|
||||
MethodInfo method = typeof(Item).GetMethod(nameof(Item.CreateServerEvent), new Type[]{ Type.MakeGenericMethodParameter(0) });
|
||||
MethodInfo generic = method.MakeGenericMethod(comp.GetType());
|
||||
return generic.Invoke(this, new object[]{ comp });
|
||||
}
|
||||
|
||||
public object CreateServerEventString(string component, object[] extraData)
|
||||
{
|
||||
var comp = GetComponentString(component);
|
||||
|
||||
if (comp == null)
|
||||
return null;
|
||||
|
||||
MethodInfo method = typeof(Item).GetMethod(nameof(Item.CreateServerEvent), new Type[]{ Type.MakeGenericMethodParameter(0), typeof(object[]) });
|
||||
MethodInfo generic = method.MakeGenericMethod(comp.GetType());
|
||||
return generic.Invoke(this, new object[]{comp, extraData });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace Barotrauma.Items.Components
|
||||
{
|
||||
using Barotrauma.Networking;
|
||||
|
||||
partial struct Signal
|
||||
{
|
||||
public static Signal Create(string value, int stepsTaken = 0, Character sender = null, Item source = null, float power = 0.0f, float strength = 1.0f)
|
||||
{
|
||||
return new Signal(value, stepsTaken, sender, source, power, strength);
|
||||
}
|
||||
}
|
||||
|
||||
partial class Quality
|
||||
{
|
||||
public void SetValue(StatType statType, float value)
|
||||
{
|
||||
statValues[statType] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -170,7 +170,7 @@ partial class NetworkingService : INetworkingService, IEventClientRawNetMessageR
|
||||
{
|
||||
if (connection == null)
|
||||
{
|
||||
foreach (NetworkConnection conn in Client.ClientList.Select(c => c.Connection))
|
||||
foreach (NetworkConnection conn in ModUtils.Client.ClientList.Select(c => c.Connection))
|
||||
{
|
||||
GameMain.Server.ServerPeer.Send(netMessage, conn, deliveryMethod);
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace Barotrauma.Items.Components
|
||||
FiringRateMultiplier
|
||||
}
|
||||
|
||||
private readonly Dictionary<StatType, float> statValues = new Dictionary<StatType, float>();
|
||||
public readonly Dictionary<StatType, float> statValues = new Dictionary<StatType, float>();
|
||||
|
||||
private int qualityLevel;
|
||||
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
using Barotrauma;
|
||||
using Barotrauma.Items.Components;
|
||||
using Barotrauma.Networking;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using static Barotrauma.Items.Components.Quality;
|
||||
|
||||
namespace Barotrauma;
|
||||
|
||||
static class MapEntityExtensions
|
||||
{
|
||||
public static void AddLinked(this MapEntity entity, MapEntity other)
|
||||
{
|
||||
entity.linkedTo.Add(other);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static class ClientExtensions
|
||||
{
|
||||
#if SERVER
|
||||
public static void SetClientCharacter(this Client client, Character character)
|
||||
{
|
||||
GameMain.Server.SetClientCharacter(client, character);
|
||||
}
|
||||
|
||||
public static void Kick(this Client client, string reason = "")
|
||||
{
|
||||
GameMain.Server.KickClient(client.Connection, reason);
|
||||
}
|
||||
|
||||
public static void Ban(this Client client, string reason = "", float seconds = -1)
|
||||
{
|
||||
if (seconds == -1)
|
||||
{
|
||||
GameMain.Server.BanClient(client, reason, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
GameMain.Server.BanClient(client, reason, TimeSpan.FromSeconds(seconds));
|
||||
}
|
||||
}
|
||||
|
||||
public static bool CheckPermission(this Client client, ClientPermissions permissions)
|
||||
{
|
||||
return client.Permissions.HasFlag(permissions);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static class ItemExtensions
|
||||
{
|
||||
public static object GetComponentString(this Item item, 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(item, null);
|
||||
}
|
||||
|
||||
#if SERVER
|
||||
public static object CreateServerEventString(this Item item, string component)
|
||||
{
|
||||
var comp = item.GetComponentString(component);
|
||||
|
||||
if (comp == null)
|
||||
return null;
|
||||
|
||||
MethodInfo method = typeof(Item).GetMethod(
|
||||
nameof(Item.CreateServerEvent),
|
||||
new Type[] { Type.MakeGenericMethodParameter(0) });
|
||||
|
||||
MethodInfo generic = method.MakeGenericMethod(comp.GetType());
|
||||
return generic.Invoke(item, new object[] { comp });
|
||||
}
|
||||
|
||||
public static object CreateServerEventString(this Item item, string component, object[] extraData)
|
||||
{
|
||||
var comp = item.GetComponentString(component);
|
||||
|
||||
if (comp == null)
|
||||
return null;
|
||||
|
||||
MethodInfo method = typeof(Item).GetMethod(
|
||||
nameof(Item.CreateServerEvent),
|
||||
new Type[] { Type.MakeGenericMethodParameter(0), typeof(object[]) });
|
||||
|
||||
MethodInfo generic = method.MakeGenericMethod(comp.GetType());
|
||||
return generic.Invoke(item, new object[] { comp, extraData });
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static class QualityExtensions
|
||||
{
|
||||
public static void SetValue(this Quality quality, StatType statType, float value)
|
||||
{
|
||||
quality.statValues[statType] = value;
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,66 @@ namespace Barotrauma
|
||||
|
||||
public static class ModUtils
|
||||
{
|
||||
public static class Item
|
||||
{
|
||||
internal 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;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Client
|
||||
{
|
||||
internal static ulong GetSteamId(Barotrauma.Networking.Client client)
|
||||
{
|
||||
if (client.AccountId.TryUnwrap(out AccountId outValue) && outValue is SteamId steamId)
|
||||
{
|
||||
return steamId.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
#if SERVER
|
||||
internal static void UnbanPlayer(string playerName)
|
||||
{
|
||||
GameMain.Server.UnbanPlayer(playerName);
|
||||
}
|
||||
|
||||
internal static void BanPlayer(string player, string reason, bool range = false, float seconds = -1)
|
||||
{
|
||||
if (seconds == -1)
|
||||
{
|
||||
GameMain.Server.BanPlayer(player, reason, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
GameMain.Server.BanPlayer(player, reason, TimeSpan.FromSeconds(seconds));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
internal static IReadOnlyList<Barotrauma.Networking.Client> ClientList
|
||||
{
|
||||
get
|
||||
{
|
||||
if (GameMain.IsSingleplayer) { return new List<Barotrauma.Networking.Client>(); }
|
||||
|
||||
#if SERVER
|
||||
return GameMain.Server.ConnectedClients;
|
||||
#else
|
||||
return GameMain.Client.ConnectedClients;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class Definitions
|
||||
{
|
||||
public const string LuaCsForBarotrauma = nameof(LuaCsForBarotrauma);
|
||||
|
||||
@@ -637,7 +637,9 @@ public class PluginManagementService : IAssemblyManagementService
|
||||
|
||||
private string DoSourceCodeTextCompatibilityPass(string sourceCode)
|
||||
{
|
||||
return sourceCode.Replace("GameMain.LuaCs", "LuaCsSetup.Instance");
|
||||
return sourceCode
|
||||
.Replace("GameMain.LuaCs", "LuaCsSetup.Instance")
|
||||
.Replace("Client.ClientList", "ModUtils.Client.ClientList");
|
||||
}
|
||||
|
||||
private IntPtr OnAssemblyLoaderResolvingUnmanaged(Assembly callerAssembly, string targetAssemblyName)
|
||||
|
||||
@@ -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;
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user