Add missing ILuaCsNetworking APIs

This commit is contained in:
Evil Factory
2026-02-28 17:57:02 -03:00
parent e2c4282477
commit 28b355911d
2 changed files with 106 additions and 1 deletions

View File

@@ -1,11 +1,20 @@
using Barotrauma.Networking;
using System.Collections.Generic;
namespace Barotrauma.LuaCs.Compatibility;
public interface ILuaCsNetworking : ILuaCsShim
internal interface ILuaCsNetworking : ILuaCsShim
{
void CreateEntityEvent(INetSerializable entity, NetEntityEvent.IData extraData);
ushort LastClientListUpdateID { get; set; }
void HttpRequest(string url, LuaCsAction callback, string data = null, string method = "POST", string contentType = "application/json", Dictionary<string, string> headers = null, string savePath = null);
void HttpPost(string url, LuaCsAction callback, string data, string contentType = "application/json", Dictionary<string, string> headers = null, string savePath = null);
void Receive(string netId, LuaCsAction action);
#if SERVER
int FileSenderMaxPacketsPerUpdate { get; set; }
void ClientWriteLobby(Client client);
void UpdateClientPermissions(Client client);
void Send(IWriteMessage mesage, NetworkConnection connection = null, DeliveryMethod deliveryMethod = DeliveryMethod.Reliable);
#elif CLIENT
void Send(IWriteMessage mesage, DeliveryMethod deliveryMethod = DeliveryMethod.Reliable);

View File

@@ -6,7 +6,9 @@ using FluentResults;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
@@ -275,4 +277,98 @@ internal partial class NetworkingService : INetworkingService
{
IsDisposed = true;
}
#region Compatiblity
private static readonly HttpClient client = new HttpClient();
public async void HttpRequest(string url, LuaCsAction callback, string data = null, string method = "POST", string contentType = "application/json", Dictionary<string, string> headers = null, string savePath = null)
{
try
{
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod(method), url);
if (headers != null)
{
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}
}
if (data != null)
{
request.Content = new StringContent(data, Encoding.UTF8, contentType);
}
HttpResponseMessage response = await client.SendAsync(request);
if (savePath != null)
{
if (LuaCsFile.IsPathAllowedException(savePath))
{
byte[] responseData = await response.Content.ReadAsByteArrayAsync();
using (var fileStream = new FileStream(savePath, FileMode.Create, FileAccess.Write))
{
fileStream.Write(responseData, 0, responseData.Length);
}
}
}
string responseBody = await response.Content.ReadAsStringAsync();
CrossThread.RequestExecutionOnMainThread(() =>
{
callback(responseBody, (int)response.StatusCode, response.Headers);
});
}
catch (HttpRequestException e)
{
CrossThread.RequestExecutionOnMainThread(() => { callback(e.Message, e.StatusCode, null); });
}
catch (Exception e)
{
CrossThread.RequestExecutionOnMainThread(() => { callback(e.Message, null, null); });
}
}
public void HttpPost(string url, LuaCsAction callback, string data, string contentType = "application/json", Dictionary<string, string> headers = null, string savePath = null)
{
HttpRequest(url, callback, data, "POST", contentType, headers, savePath);
}
public void HttpGet(string url, LuaCsAction callback, Dictionary<string, string> headers = null, string savePath = null)
{
HttpRequest(url, callback, null, "GET", null, headers, savePath);
}
public void CreateEntityEvent(INetSerializable entity, NetEntityEvent.IData extraData)
{
GameMain.NetworkMember.CreateEntityEvent(entity, extraData);
}
public ushort LastClientListUpdateID
{
get { return GameMain.NetworkMember.LastClientListUpdateID; }
set { GameMain.NetworkMember.LastClientListUpdateID = value; }
}
#if SERVER
public void ClientWriteLobby(Client client) => GameMain.Server.ClientWriteLobby(client);
public void UpdateClientPermissions(Client client)
{
GameMain.Server.UpdateClientPermissions(client);
}
public int FileSenderMaxPacketsPerUpdate
{
get { return FileSender.FileTransferOut.MaxPacketsPerUpdate; }
set { FileSender.FileTransferOut.MaxPacketsPerUpdate = value; }
}
#endif
#endregion
}