Reorganize project and use latest API features
This commit is contained in:
parent
eb78bd61e3
commit
8d2d11b736
4 changed files with 51 additions and 99 deletions
|
@ -17,18 +17,16 @@ using uREPL;
|
||||||
|
|
||||||
namespace GCDC
|
namespace GCDC
|
||||||
{
|
{
|
||||||
public class TextBlockUpdateEngine : IDeterministicTimeStopped, IDeterministicTimeRunning, IApiEngine, IUnorderedInitializeOnTimeStoppedModeEntered
|
public class DiscordClient
|
||||||
{
|
{
|
||||||
private string _token;
|
private string _token;
|
||||||
private bool _running;
|
private bool _running;
|
||||||
private Thread _rect;
|
private Thread _rect;
|
||||||
|
private readonly Queue<string> messages = new Queue<string>();
|
||||||
|
private readonly GCDCPlugin plugin;
|
||||||
|
public DiscordClient(GCDCPlugin plugin) => this.plugin = plugin;
|
||||||
public void Ready()
|
public void Ready()
|
||||||
{
|
{
|
||||||
if (!RuntimeCommands.HasRegistered("dc"))
|
|
||||||
RuntimeCommands.Register<string>("dc", SendMessage);
|
|
||||||
if (!RuntimeCommands.HasRegistered("dcsetup"))
|
|
||||||
RuntimeCommands.Register<string>("dcsetup", Setup,
|
|
||||||
"Initial setup for GCDC. The argument is the channel ID first.");
|
|
||||||
if (File.Exists("gcdc.json"))
|
if (File.Exists("gcdc.json"))
|
||||||
{
|
{
|
||||||
var jo = JObject.Load(new JsonTextReader(File.OpenText("gcdc.json")));
|
var jo = JObject.Load(new JsonTextReader(File.OpenText("gcdc.json")));
|
||||||
|
@ -59,7 +57,7 @@ namespace GCDC
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (JObject.Parse(WebUtils.Request("users/get?token=" + tokenOrChannel))["response"].Value<string>() == "OK")
|
if (JObject.Parse(WebUtils.Request("users/get?token=" + tokenOrChannel))["response"]?.Value<string>() == "OK")
|
||||||
{
|
{
|
||||||
_token = tokenOrChannel;
|
_token = tokenOrChannel;
|
||||||
var jo = new JObject {["token"] = tokenOrChannel};
|
var jo = new JObject {["token"] = tokenOrChannel};
|
||||||
|
@ -91,8 +89,7 @@ namespace GCDC
|
||||||
{
|
{
|
||||||
var parameters = "token=" + _token + "&message=" + message;
|
var parameters = "token=" + _token + "&message=" + message;
|
||||||
var resp = JObject.Parse(WebUtils.Request("messages/send?" + parameters, ""));
|
var resp = JObject.Parse(WebUtils.Request("messages/send?" + parameters, ""));
|
||||||
if (resp["response"]
|
if (resp["response"]?.Value<string>() == "OK")
|
||||||
.Value<string>() == "OK")
|
|
||||||
{
|
{
|
||||||
AddMessage("<nobr><" + resp["username"] + "> " + message);
|
AddMessage("<nobr><" + resp["username"] + "> " + message);
|
||||||
Log.Output("Message sent");
|
Log.Output("Message sent");
|
||||||
|
@ -135,46 +132,20 @@ namespace GCDC
|
||||||
_rect.Start();
|
_rect.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public EntitiesDB entitiesDB { get; set; }
|
|
||||||
public string name { get; } = "GCDC-TextUpdate";
|
|
||||||
private volatile Queue<string> messages = new Queue<string>();
|
|
||||||
private volatile bool updatedTextBlock;
|
|
||||||
|
|
||||||
public JobHandle SimulatePhysicsStep(
|
|
||||||
in float deltaTime,
|
|
||||||
in PhysicsUtility utility,
|
|
||||||
in PlayerInput[] playerInputs) //Gamecraft.Blocks.ConsoleBlock.dll
|
|
||||||
{
|
|
||||||
if (updatedTextBlock)
|
|
||||||
return new JobHandle();
|
|
||||||
var txt = messages.Count > 0 ? messages.Aggregate((current, msg) => current + "\n" + msg) : "<No messages yet>";
|
|
||||||
RuntimeCommands.Call("ChangeTextBlockCommand", "Discord", txt);
|
|
||||||
updatedTextBlock = true;
|
|
||||||
|
|
||||||
return new JobHandle();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddMessage(string message)
|
public void AddMessage(string message)
|
||||||
{
|
{
|
||||||
messages.Enqueue(message);
|
messages.Enqueue(message);
|
||||||
if (messages.Count > 10)
|
if (messages.Count > 10)
|
||||||
messages.Dequeue();
|
messages.Dequeue();
|
||||||
updatedTextBlock = false;
|
plugin.Update(messages);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JobHandle OnInitializeTimeStoppedMode()
|
public void Stop()
|
||||||
{
|
|
||||||
updatedTextBlock = false; //Update text block
|
|
||||||
return new JobHandle();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
{
|
||||||
_running = false;
|
_running = false;
|
||||||
_rect.Interrupt();
|
_rect.Interrupt();
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Name { get; } = "GCDCEngine";
|
public void Update() => plugin.Update(messages);
|
||||||
public bool isRemovable { get; } = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,43 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Reflection;
|
|
||||||
using Gamecraft.Blocks.ConsoleBlock;
|
|
||||||
using HarmonyLib;
|
|
||||||
using RobocraftX;
|
|
||||||
using RobocraftX.GUI.CommandLine;
|
|
||||||
using RobocraftX.Multiplayer;
|
|
||||||
using RobocraftX.Services.MultiplayerNetworking;
|
|
||||||
using RobocraftX.StateSync;
|
|
||||||
using Svelto.ECS;
|
|
||||||
using Unity.Entities;
|
|
||||||
using UnityEngine;
|
|
||||||
|
|
||||||
namespace GCDC
|
|
||||||
{
|
|
||||||
[HarmonyPatch]
|
|
||||||
public class DiscordEngineInjectionPatch
|
|
||||||
{
|
|
||||||
static void Postfix(EnginesRoot enginesRoot, in StateSyncRegistrationHelper stateSyncReg, bool isAuthoritative)
|
|
||||||
{
|
|
||||||
if (isAuthoritative)
|
|
||||||
{
|
|
||||||
stateSyncReg.AddDeterministicEngine(new TextBlockUpdateEngine());
|
|
||||||
Debug.Log($"Added Discord text block update engine");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
Debug.Log("Not authoritative, not adding Discord engine");
|
|
||||||
}
|
|
||||||
|
|
||||||
static MethodBase TargetMethod()
|
|
||||||
{
|
|
||||||
return _ComposeMethodInfo(ConsoleBlockCompositionRoot.Compose);
|
|
||||||
}
|
|
||||||
|
|
||||||
private delegate void ComposeAction(EnginesRoot er, in StateSyncRegistrationHelper ssrh,
|
|
||||||
NetworkReceivers networkReceivers, NetworkSender networkSende, bool isAuthoritative);
|
|
||||||
private static MethodInfo _ComposeMethodInfo(ComposeAction a)
|
|
||||||
{
|
|
||||||
return a.Method;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -11,8 +11,8 @@
|
||||||
<AssemblyName>GCDC</AssemblyName>
|
<AssemblyName>GCDC</AssemblyName>
|
||||||
<TargetFramework>net472</TargetFramework>
|
<TargetFramework>net472</TargetFramework>
|
||||||
<FileAlignment>512</FileAlignment>
|
<FileAlignment>512</FileAlignment>
|
||||||
<AssemblyVersion>0.0.2.0</AssemblyVersion>
|
<AssemblyVersion>1.0.0.0</AssemblyVersion>
|
||||||
<FileVersion>0.0.2.0</FileVersion>
|
<FileVersion>1.0.0.0</FileVersion>
|
||||||
<ProductVersion>0.0.2.0</ProductVersion>
|
<ProductVersion>0.0.2.0</ProductVersion>
|
||||||
<Product>GCDC</Product>
|
<Product>GCDC</Product>
|
||||||
<ProductName>GCDC</ProductName>
|
<ProductName>GCDC</ProductName>
|
||||||
|
@ -52,7 +52,7 @@
|
||||||
<Reference Include="Gamecraft.Blocks.ConsoleBlock, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
|
<Reference Include="Gamecraft.Blocks.ConsoleBlock, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
|
||||||
<HintPath>..\..\ref\Gamecraft_Data\Managed\Gamecraft.Blocks.ConsoleBlock.dll</HintPath>
|
<HintPath>..\..\ref\Gamecraft_Data\Managed\Gamecraft.Blocks.ConsoleBlock.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="GamecraftModdingAPI, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null">
|
<Reference Include="GamecraftModdingAPI">
|
||||||
<HintPath>..\..\GamecraftModdingAPI\GamecraftModdingAPI\bin\Debug\net472\GamecraftModdingAPI.dll</HintPath>
|
<HintPath>..\..\GamecraftModdingAPI\GamecraftModdingAPI\bin\Debug\net472\GamecraftModdingAPI.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="IllusionPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
|
<Reference Include="IllusionPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
|
||||||
|
@ -61,7 +61,7 @@
|
||||||
<Reference Include="MultiplayerNetworking, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
|
<Reference Include="MultiplayerNetworking, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
|
||||||
<HintPath>..\..\ref\Gamecraft_Data\Managed\MultiplayerNetworking.dll</HintPath>
|
<HintPath>..\..\ref\Gamecraft_Data\Managed\MultiplayerNetworking.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="Newtonsoft.Json, Version=12.0.1.0, Culture=neutral, PublicKeyToken=null">
|
<Reference Include="Newtonsoft.Json">
|
||||||
<HintPath>..\..\ref\Gamecraft_Data\Managed\Newtonsoft.Json.dll</HintPath>
|
<HintPath>..\..\ref\Gamecraft_Data\Managed\Newtonsoft.Json.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="RobocraftX.AccountPreferences, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
|
<Reference Include="RobocraftX.AccountPreferences, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
|
||||||
|
|
|
@ -1,31 +1,55 @@
|
||||||
using System.Reflection;
|
using System.Collections;
|
||||||
using HarmonyLib;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using GamecraftModdingAPI.App;
|
||||||
|
using GamecraftModdingAPI.Commands;
|
||||||
using IllusionPlugin;
|
using IllusionPlugin;
|
||||||
|
using RobocraftX.Schedulers;
|
||||||
|
using Svelto.Tasks.ExtraLean;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using uREPL;
|
||||||
|
|
||||||
namespace GCDC
|
namespace GCDC
|
||||||
{
|
{
|
||||||
public class GCDCPlugin : IPlugin
|
public class GCDCPlugin : IPlugin
|
||||||
{
|
{
|
||||||
public string Name { get; } = "GCDC";
|
public string Name { get; } = Assembly.GetExecutingAssembly().GetName().Name;
|
||||||
public string Version { get; } = "v0.0.1";
|
public string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString();
|
||||||
public static Harmony harmony { get; protected set; }
|
|
||||||
public const string HarmonyID = "io.github.norbipeti.GCDC";
|
|
||||||
|
|
||||||
public void OnApplicationStart()
|
public void OnApplicationStart()
|
||||||
{
|
{
|
||||||
if (harmony == null)
|
GamecraftModdingAPI.Main.Init();
|
||||||
{
|
var client = new DiscordClient(this);
|
||||||
harmony = new Harmony(HarmonyID);
|
CommandBuilder.Builder("dc", "Send messages to Discord.")
|
||||||
harmony.PatchAll(Assembly.GetExecutingAssembly());
|
.Action<string>(client.SendMessage).Build();
|
||||||
|
CommandBuilder.Builder("dcsetup", "Initial setup for GCDC. The argument is the channel ID first. For example: dcsetup \"420159832423923714\"")
|
||||||
|
.Action<string>(client.Setup).Build();
|
||||||
|
Game.Enter += (sender, e) =>
|
||||||
|
client.Ready();
|
||||||
|
Game.Edit += (sender, e) =>
|
||||||
|
client.Update(); //Update text block
|
||||||
|
Game.Exit += (sender, e) =>
|
||||||
|
client.Stop();
|
||||||
|
Debug.Log("GCDC loaded");
|
||||||
}
|
}
|
||||||
|
|
||||||
Debug.Log("GCDC loaded");
|
public void Update(Queue<string> messages)
|
||||||
|
{
|
||||||
|
UpdateEnum(messages).RunOn(ExtraLean.EveryFrameStepRunner_RUNS_IN_TIME_STOPPED_AND_RUNNING);
|
||||||
|
}
|
||||||
|
|
||||||
|
private IEnumerator UpdateEnum(Queue<string> messages)
|
||||||
|
{
|
||||||
|
var txt = messages.Count > 0
|
||||||
|
? messages.Aggregate((current, msg) => current + "\n" + msg)
|
||||||
|
: "<No messages yet>";
|
||||||
|
RuntimeCommands.Call("ChangeTextBlockCommand", "Discord", txt);
|
||||||
|
yield break;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnApplicationQuit()
|
public void OnApplicationQuit()
|
||||||
{
|
{
|
||||||
harmony?.UnpatchAll(HarmonyID);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnLevelWasLoaded(int level)
|
public void OnLevelWasLoaded(int level)
|
||||||
|
|
Loading…
Reference in a new issue