Skip to content

Commit

Permalink
Allow plugin version to run normally if server is detected at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
ZachHembree committed Nov 12, 2023
1 parent 0f3f002 commit e1b9eaa
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 77 deletions.
29 changes: 11 additions & 18 deletions BuildVision2/Data/Scripts/BuildVision2/BuildVision2.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using ProtoBuf;
using RichHudFramework;
using RichHudFramework.Client;
using RichHudFramework.Internal;
using RichHudFramework.IO;
using RichHudFramework.UI;
using RichHudFramework.UI.Client;
using Sandbox.ModAPI;
using System;
using System.Collections.Generic;
using VRage;
using VRage.Game;
using VRage.Game.Components;
using VRage.Game.Entity;
using VRage.Game.ModAPI;
using VRage.ModAPI;
using VRage.Utils;
using VRageMath;
using VRage.Game;
using VRage.Game.ModAPI;
using VRage.Game.Entity;
using Sandbox.ModAPI;
using VRage.Game.Components;
using RichHudFramework;
using RichHudFramework.UI.Client;
using ProtoBuf;

namespace DarkHelmet.BuildVision2
{
Expand All @@ -40,7 +40,7 @@ public BvMain() : base(true, true)

LogIO.FileName = "bvLog.txt";
BvConfig.FileName = "BuildVision2Config.xml";

ExceptionHandler.ModName = modName;
ExceptionHandler.PromptForReload = true;
ExceptionHandler.RecoveryLimit = 3;
Expand Down Expand Up @@ -72,20 +72,13 @@ private void HudInit()
AddChatCommands();
InitSettingsMenu();
TerminalUtilities.Init();
QuickActionHudSpace.Init();
QuickActionHudSpace.Init();

if (BvConfig.WasConfigOld)
RichHudTerminal.OpenToPage(helpMain);

BvConfig.OnConfigLoad += UpdateBindPageVisibility;
UpdateBindPageVisibility();

#if PLUGIN_LOADER
ExceptionHandler.SendChatMessage(
"Build Vision has been launched in client-only mode by the Plugin Loader. " +
"Some functionality will be unavailable."
);
#endif
}
}

Expand Down
85 changes: 59 additions & 26 deletions BuildVision2/Data/Scripts/BuildVision2/Server/BvServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ namespace DarkHelmet.BuildVision2
/// </summary>
public sealed partial class BvServer : BvComponentBase
{
public static bool IsAlive { get; private set; }

#if PLUGIN_LOADER
public const bool IsPlugin = true;
#else
public const bool IsPlugin = false;
#endif

private const ushort serverHandlerID = 16971;
private static BvServer instance;

Expand Down Expand Up @@ -56,6 +64,16 @@ public static void Init()

instance.messageHandler = new SecureMsgHandler(instance.NetworkMessageHandler);
MyAPIGateway.Multiplayer.RegisterSecureMessageHandler(serverHandlerID, instance.messageHandler);

if (ExceptionHandler.IsServer)
{
IsAlive = true;
}
else
{
IsAlive = false;
SendEntityActionToServerInternal(BvServerActions.GetAlive, -1, x => IsAlive = true);
}
}

/// <summary>
Expand All @@ -64,18 +82,26 @@ public static void Init()
public override void Close()
{
MyAPIGateway.Multiplayer.UnregisterSecureMessageHandler(serverHandlerID, messageHandler);
IsAlive = false;
instance = null;
}

public static void SendEntityActionToServer(BvServerActions actionID, long entID, Action<byte[]> callback = null, bool uniqueCallback = true)
{
if (IsAlive)
{
SendEntityActionToServerInternal(actionID, entID, callback, uniqueCallback);
}
}

/// <summary>
/// Sends an entity action to be executed on the server
/// </summary>
public static void SendEntityActionToServer(ServerBlockActions actionID, long entID, Action<byte[]> callback = null, bool uniqueCallback = true)
public static void SendEntityActionToServerInternal(BvServerActions actionID, long entID, Action<byte[]> callback = null, bool uniqueCallback = true)
{
#if !PLUGIN_LOADER
int callbackID = -1;

if ((actionID & ServerBlockActions.RequireReply) == ServerBlockActions.RequireReply)
if ((actionID & BvServerActions.RequireReply) == BvServerActions.RequireReply)
{
if (callback != null)
{
Expand All @@ -89,7 +115,6 @@ public static void SendEntityActionToServer(ServerBlockActions actionID, long en
}

instance.clientOutgoing.Add(new ClientMessage(actionID, callbackID, entID));
#endif
}

/// <summary>
Expand Down Expand Up @@ -241,23 +266,31 @@ private void ProcessMessagesFromClient()

foreach (var message in receivedClientMessages)
{
if (entID != message.Item2.entID)
entity = MyAPIGateway.Entities.GetEntityById(message.Item2.entID);

var actionID = (ServerBlockActions)message.Item2.actionID;
entID = message.Item2.entID;
var actionID = (BvServerActions)message.Item2.actionID;

if ((actionID & ServerBlockActions.MyMechanicalConnection) == ServerBlockActions.MyMechanicalConnection)
{
HandleMechBlockMessages(entity, message, ref currentClient);
}
else if ((actionID & ServerBlockActions.Warhead) == ServerBlockActions.Warhead)
if (message.Item2.entID != -1)
{
HandleWarheadMessages(entity, message, ref currentClient);
if (entID != message.Item2.entID)
entity = MyAPIGateway.Entities.GetEntityById(message.Item2.entID);

entID = message.Item2.entID;

if ((actionID & BvServerActions.MyMechanicalConnection) == BvServerActions.MyMechanicalConnection)
{
HandleMechBlockMessages(entity, message, ref currentClient);
}
else if ((actionID & BvServerActions.Warhead) == BvServerActions.Warhead)
{
HandleWarheadMessages(entity, message, ref currentClient);
}
else if ((actionID & BvServerActions.AirVent) == BvServerActions.AirVent)
{
HandleAirVentMessages(entity, message, ref currentClient);
}
}
else if ((actionID & ServerBlockActions.AirVent) == ServerBlockActions.AirVent)
else if ((actionID & BvServerActions.GetAlive) == BvServerActions.GetAlive)
{
HandleAirVentMessages(entity, message, ref currentClient);
AddServerReply(message, true, ref currentClient);
}
}
}
Expand All @@ -276,19 +309,19 @@ private void ProcessMessagesFromServer()
private void HandleMechBlockMessages(IMyEntity entity, MyTuple<ulong, ClientMessage> message, ref ulong? currentClient)
{
var mechBlock = entity as IMyMechanicalConnectionBlock;
var actionID = (ServerBlockActions)message.Item2.actionID;
var actionID = (BvServerActions)message.Item2.actionID;

if ((actionID & ServerBlockActions.AttachHead) == ServerBlockActions.AttachHead)
if ((actionID & BvServerActions.AttachHead) == BvServerActions.AttachHead)
{
mechBlock.Attach();
}
else if ((actionID & ServerBlockActions.DetachHead) == ServerBlockActions.DetachHead)
else if ((actionID & BvServerActions.DetachHead) == BvServerActions.DetachHead)
{
mechBlock.Detach();
}
else if ((actionID & ServerBlockActions.MotorStator) == ServerBlockActions.MotorStator)
else if ((actionID & BvServerActions.MotorStator) == BvServerActions.MotorStator)
{
if ((actionID & ServerBlockActions.GetAngle) == ServerBlockActions.GetAngle)
if ((actionID & BvServerActions.GetAngle) == BvServerActions.GetAngle)
{
var rotor = entity as IMyMotorStator;
AddServerReply(message, rotor.Angle, ref currentClient);
Expand All @@ -298,9 +331,9 @@ private void HandleMechBlockMessages(IMyEntity entity, MyTuple<ulong, ClientMess

private void HandleWarheadMessages(IMyEntity entity, MyTuple<ulong, ClientMessage> message, ref ulong? currentClient)
{
var actionID = (ServerBlockActions)message.Item2.actionID;
var actionID = (BvServerActions)message.Item2.actionID;

if ((actionID & ServerBlockActions.GetTime) == ServerBlockActions.GetTime)
if ((actionID & BvServerActions.GetTime) == BvServerActions.GetTime)
{
var warhead = entity as IMyWarhead;
AddServerReply(message, warhead.DetonationTime, ref currentClient);
Expand All @@ -309,9 +342,9 @@ private void HandleWarheadMessages(IMyEntity entity, MyTuple<ulong, ClientMessag

private void HandleAirVentMessages(IMyEntity entity, MyTuple<ulong, ClientMessage> message, ref ulong? currentClient)
{
var actionID = (ServerBlockActions)message.Item2.actionID;
var actionID = (BvServerActions)message.Item2.actionID;

if ((actionID & ServerBlockActions.GetOxygen) == ServerBlockActions.GetOxygen)
if ((actionID & BvServerActions.GetOxygen) == BvServerActions.GetOxygen)
{
var vent = entity as IMyAirVent;
AddServerReply(message, vent.GetOxygenLevel(), ref currentClient);
Expand Down
31 changes: 16 additions & 15 deletions BuildVision2/Data/Scripts/BuildVision2/Server/Messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
namespace DarkHelmet.BuildVision2
{
[Flags]
public enum ServerBlockActions : ulong
public enum BvServerActions : ulong
{
None = 0x00000000,
MyMechanicalConnection = 0x00000001,
MotorStator = 0x00000002 | MyMechanicalConnection,
Warhead = 0x00000004,
AirVent = 0x00000008,

GetOrSendData = 0x04000000,
RequireReply = 0x08000000 | GetOrSendData,
AttachHead = 0x10000000 | GetOrSendData,
DetachHead = 0x20000000 | GetOrSendData,
GetAngle = 0x40000000 | RequireReply,
GetTime = 0x80000000 | RequireReply,
GetOxygen = 0x100000000 | RequireReply,
None = 0,
MyMechanicalConnection = 1 << 0,
MotorStator = 1 << 1 | MyMechanicalConnection,
Warhead = 1 << 2,
AirVent = 1 << 3,

GetOrSendData = 1 << 26,
RequireReply = 1 << 27 | GetOrSendData,
AttachHead = 1 << 28 | GetOrSendData,
DetachHead = 1 << 29 | GetOrSendData,
GetAngle = 1 << 30 | RequireReply,
GetTime = 1ul << 31 | RequireReply,
GetOxygen = 1ul << 32 | RequireReply,
GetAlive = 1ul << 33 | RequireReply,
}

public sealed partial class BvServer
Expand Down Expand Up @@ -81,7 +82,7 @@ private struct ClientMessage
[ProtoMember(3)]
public ulong actionID;

public ClientMessage(ServerBlockActions actionID, int callbackID, long entID)
public ClientMessage(BvServerActions actionID, int callbackID, long entID)
{
this.actionID = (ulong)actionID;
this.callbackID = callbackID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static void GetMechActions(PropertyBlock block, List<BlockMemberBase> mem
List<IMyTerminalAction> terminalActions = new List<IMyTerminalAction>();
block.TBlock.GetActions(terminalActions);

#if !PLUGIN_LOADER
if (BvServer.IsAlive) {
members.Add(GetBlockAction(
// Name
MyTexts.GetString(MySpaceTexts.BlockActionTitle_Attach),
Expand All @@ -115,7 +115,7 @@ public static void GetMechActions(PropertyBlock block, List<BlockMemberBase> mem
members.Add(GetBlockAction(
MyTexts.GetString(MySpaceTexts.BlockActionTitle_Detach), null,
block.MechConnection.DetachHead, block));
#endif
}

foreach (IMyTerminalAction tAction in terminalActions)
{
Expand Down Expand Up @@ -156,14 +156,14 @@ public static void GetDoorActions(PropertyBlock block, List<BlockMemberBase> mem
/// </summary>
public static void GetWarheadActions(PropertyBlock block, List<BlockMemberBase> members)
{
#if PLUGIN_LOADER
if (!BvServer.IsAlive) {
members.Add(GetBlockAction(
// Name
MyTexts.GetString(MySpaceTexts.TerminalControlPanel_Warhead_StartCountdown),
// Status
null,
block.Warhead.StartCountdown, block));
#else
} else {

members.Add(GetBlockAction(
// Name
Expand All @@ -177,7 +177,7 @@ public static void GetWarheadActions(PropertyBlock block, List<BlockMemberBase>
x.Append("s)");
},
block.Warhead.StartCountdown, block));
#endif
}
members.Add(GetBlockAction(
MyTexts.GetString(MySpaceTexts.TerminalControlPanel_Warhead_StopCountdown), null,
block.Warhead.StopCountdown, block));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ public override void SetProperty(StringBuilder name, ITerminalProperty<float> pr
{
if (block.SubtypeId.UsesSubtype(TBlockSubtypes.Rotor))
{
#if !PLUGIN_LOADER
if (BvServer.IsAlive) {
GetStatusFunc = GetRotorAngleFunc;
#endif
}
}
else if (block.SubtypeId.UsesSubtype(TBlockSubtypes.Piston))
GetStatusFunc = GetPistonExtensionFunc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public float OxygenLevel
{
BvServer.SendEntityActionToServer
(
ServerBlockActions.AirVent | ServerBlockActions.GetOxygen,
BvServerActions.AirVent | BvServerActions.GetOxygen,
subtype.EntityId,
OxygenCallback
);
Expand Down Expand Up @@ -83,11 +83,11 @@ public override void GetSummary(RichText builder, GlyphFormat nameFormat, GlyphF
builder.Add(MyTexts.GetString(Depressurize ? MySpaceTexts.SwitchText_On : MySpaceTexts.SwitchText_Off), valueFormat);
builder.Add("\n", valueFormat);

#if !PLUGIN_LOADER
if (BvServer.IsAlive) {
// Oxy pct
builder.Add(MyTexts.GetString(MySpaceTexts.HudInfoOxygen), nameFormat);
builder.Add($"{(OxygenLevel * 100f):F2}%\n", valueFormat);
#endif
}
}

// Vent status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override void SetBlock(SuperBlock block)
public void AttachHead()
{
//subtype.Attach();
BvServer.SendEntityActionToServer(ServerBlockActions.MyMechanicalConnection | ServerBlockActions.AttachHead, subtype.EntityId);
BvServer.SendEntityActionToServer(BvServerActions.MyMechanicalConnection | BvServerActions.AttachHead, subtype.EntityId);
}

/// <summary>
Expand All @@ -52,7 +52,7 @@ public void AttachHead()
public void DetachHead()
{
//subtype.Detach(); -- Bug: This isn't being synchronized with the DS, yet Attach() is.
BvServer.SendEntityActionToServer(ServerBlockActions.MyMechanicalConnection | ServerBlockActions.DetachHead, subtype.EntityId);
BvServer.SendEntityActionToServer(BvServerActions.MyMechanicalConnection | BvServerActions.DetachHead, subtype.EntityId);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public float Angle
{
BvServer.SendEntityActionToServer
(
ServerBlockActions.MotorStator | ServerBlockActions.GetAngle,
BvServerActions.MotorStator | BvServerActions.GetAngle,
subtype.EntityId,
RotorAngleCallback
);
Expand Down Expand Up @@ -68,10 +68,10 @@ public override void GetSummary(RichText builder, GlyphFormat nameFormat, GlyphF
builder.Add(RotorLock ? MyTexts.GetString(MySpaceTexts.HudInfoOn) : MyTexts.GetString(MySpaceTexts.HudInfoOff), valueFormat);
builder.Add("\n", valueFormat);

#if !PLUGIN_LOADER
if (BvServer.IsAlive) {
builder.Add(MyTexts.GetString(MySpaceTexts.BlockPropertiesText_MotorCurrentAngle), nameFormat);
builder.Add($"{Angle.RadiansToDegrees():F2}°\n", valueFormat);
#endif
}
}
}

Expand Down
Loading

0 comments on commit e1b9eaa

Please sign in to comment.