All features seem to work.
This commit is contained in:
@@ -25,6 +25,7 @@ namespace RemoteControl.Patches
|
||||
{
|
||||
try
|
||||
{
|
||||
RemoteControl.Log("Sending update");
|
||||
RemoteControl.Subscribers.SendUpdate();
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
@@ -3,6 +3,8 @@ using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Assets.Scripts;
|
||||
@@ -38,7 +40,7 @@ namespace RemoteControl
|
||||
private readonly Harmony _harmony = new Harmony(pluginGuid);
|
||||
|
||||
public static WebServer WebServer { get; private set; }
|
||||
internal static SubscriptionModule Subscribers { get; private set; }
|
||||
internal static SubscriptionModule Subscribers { get; } = new SubscriptionModule("/subscribe");
|
||||
|
||||
|
||||
private CancellationTokenSource _modLifecycle = new();
|
||||
@@ -54,13 +56,6 @@ namespace RemoteControl
|
||||
|
||||
private void StartRC()
|
||||
{
|
||||
if (!_modLifecycle.IsCancellationRequested)
|
||||
{
|
||||
_modLifecycle.Cancel();
|
||||
}
|
||||
|
||||
_modLifecycle = new CancellationTokenSource();
|
||||
|
||||
Port = Config.Bind(new ConfigDefinition("Server", "Port"), 39125,
|
||||
new ConfigDescription("The port to listen on"));
|
||||
ListenOnAllInterfaces = Config.Bind(
|
||||
@@ -69,29 +64,51 @@ namespace RemoteControl
|
||||
new ConfigDescription("If set, listen on all interfaces. Otherwise, listen only on localhost"));
|
||||
|
||||
|
||||
var subscriptionModule = new SubscriptionModule("/subscribe");
|
||||
Subscribers = subscriptionModule;
|
||||
WebServer = new WebServer(o =>
|
||||
o.WithUrlPrefix($"http://{(ListenOnAllInterfaces.Value ? "0.0.0.0" : "localhost")}:{Port.Value}/")
|
||||
.WithEmbedIOHttpListener()
|
||||
)
|
||||
.WithWebApi("/api/v1", m => m.WithController<ApiController>())
|
||||
.WithModule(subscriptionModule);
|
||||
WebServer.Start(_modLifecycle.Token);
|
||||
// No need to restart the game after changing settings :-)
|
||||
Port.SettingChanged += WebserverSettingsChanged;
|
||||
ListenOnAllInterfaces.SettingChanged += WebserverSettingsChanged;
|
||||
|
||||
// Subscribers = new SubscriptionModule("/subscribe");
|
||||
RestartWebserver();
|
||||
|
||||
_harmony.PatchAll();
|
||||
foreach (var patchedMethod in _harmony.GetPatchedMethods())
|
||||
{
|
||||
Log($"Patched {patchedMethod.FullDescription()}");
|
||||
}
|
||||
|
||||
Log($"Patched {_harmony.GetPatchedMethods().Count()} methods total");
|
||||
}
|
||||
|
||||
private void WebserverSettingsChanged(object _1, EventArgs _2)
|
||||
{
|
||||
RestartWebserver();
|
||||
}
|
||||
|
||||
private void RestartWebserver()
|
||||
{
|
||||
if (!_modLifecycle.IsCancellationRequested)
|
||||
{
|
||||
_modLifecycle.Cancel();
|
||||
}
|
||||
|
||||
var URL = $"http://{(ListenOnAllInterfaces.Value ? "*" : "localhost")}:{Port.Value}/";
|
||||
Log($"Starting webserver on {URL}");
|
||||
_modLifecycle = new CancellationTokenSource();
|
||||
WebServer = new WebServer(o =>
|
||||
{
|
||||
o.WithUrlPrefix(URL)
|
||||
.WithEmbedIOHttpListener();
|
||||
})
|
||||
.WithWebApi("/api/v1", m => m.WithController<ApiController>())
|
||||
.WithModule(Subscribers);
|
||||
WebServer.Start(_modLifecycle.Token);
|
||||
}
|
||||
|
||||
public void OnLoaded(List<GameObject> prefabs)
|
||||
{
|
||||
// Start();
|
||||
Log("Starting RemoteControl");
|
||||
StartRC();
|
||||
#if DEVELOPMENT_BUILD
|
||||
Debug.Log($"Loaded {prefabs.Count} prefabs");
|
||||
#endif
|
||||
@@ -99,7 +116,8 @@ namespace RemoteControl
|
||||
|
||||
public void OnUnloaded(List<GameObject> prefabs)
|
||||
{
|
||||
// Stop();
|
||||
Log("Tearing down RemoteControl");
|
||||
StopRC();
|
||||
}
|
||||
|
||||
public void StopRC()
|
||||
@@ -110,21 +128,23 @@ namespace RemoteControl
|
||||
|
||||
public void Awake()
|
||||
{
|
||||
Log("Starting RemoteControl");
|
||||
StartRC();
|
||||
// Log("Starting RemoteControl");
|
||||
// StartRC();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
Log("Tearing down RemoteControl");
|
||||
StopRC();
|
||||
// Log("Tearing down RemoteControl");
|
||||
// StopRC();
|
||||
}
|
||||
}
|
||||
|
||||
internal class ApiController : WebApiController
|
||||
{
|
||||
private static readonly Dictionary<string, LogicType> LtByName = EnumCollections.LogicTypes.AsLookupDict(true);
|
||||
private static readonly Dictionary<string, LogicSlotType> StByName = EnumCollections.LogicSlotTypes.AsLookupDict(true);
|
||||
|
||||
private static readonly Dictionary<string, LogicSlotType> StByName =
|
||||
EnumCollections.LogicSlotTypes.AsLookupDict(true);
|
||||
|
||||
private static Device GetDeviceJson(GameDevice device) => new(device);
|
||||
|
||||
@@ -178,6 +198,9 @@ namespace RemoteControl
|
||||
var result = default(T);
|
||||
bool isFound = false;
|
||||
|
||||
// deviceName = WebUtility.UrlDecode(deviceName);
|
||||
// networkName = WebUtility.UrlDecode(networkName);
|
||||
|
||||
GameDevice.AllDevices.ForEach(anaDev =>
|
||||
{
|
||||
if (anaDev is not CableAnalyser analyser) return;
|
||||
@@ -241,6 +264,7 @@ namespace RemoteControl
|
||||
devices.Add(device.ReferenceId, GetDeviceJson(device));
|
||||
}
|
||||
}
|
||||
|
||||
return Task.FromResult<IDictionary<long, Device>>(devices);
|
||||
}
|
||||
}
|
||||
@@ -284,7 +308,9 @@ namespace RemoteControl
|
||||
else
|
||||
{
|
||||
throw HttpException.MethodNotAllowed();
|
||||
};
|
||||
}
|
||||
|
||||
;
|
||||
}
|
||||
|
||||
[Route(HttpVerbs.Patch, "/networks/{networkId}/device/{deviceId}")]
|
||||
@@ -308,17 +334,22 @@ namespace RemoteControl
|
||||
}
|
||||
|
||||
[Route(HttpVerbs.Get, "/networks/{networkId}/device/{deviceId}/code")]
|
||||
public UniTask<string> GetCode(string networkId, string deviceId)
|
||||
public async Task GetCode(string networkId, string deviceId)
|
||||
{
|
||||
var dev = FindUniqueDeviceForHttp<ICircuitHolder>(networkId, deviceId);
|
||||
return UniTask.FromResult(dev.GetSourceCode());
|
||||
var code = dev.GetSourceCode();
|
||||
HttpContext.Response.ContentType = "text/plain";
|
||||
|
||||
var codeBytes = Encoding.UTF8.GetBytes(code);
|
||||
await HttpContext.Response.OutputStream.WriteAsync(codeBytes, 0, codeBytes.Length);
|
||||
}
|
||||
|
||||
[Route(HttpVerbs.Post, "/networks/{networkId}/device/{deviceId}/code")]
|
||||
public UniTask<string> SetCode(string networkId, string deviceId, string code)
|
||||
public async Task SetCode(string networkId, string deviceId)
|
||||
{
|
||||
var code = await HttpContext.GetRequestBodyAsByteArrayAsync();
|
||||
var dev = FindUniqueDeviceForHttp<ICircuitHolder>(networkId, deviceId);
|
||||
return UniTask.FromResult(dev.GetSourceCode());
|
||||
dev.SetSourceCode(Encoding.UTF8.GetString(code));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user