vPOD impersonates a Tesla game client (rpl4opt.exe / btl4.exe) so the Red Planet and BattleTech operator consoles can be exercised without real cockpit hardware. New net48 WinForms project under Console\vPOD: - MungaPodServer: the server half of the Munga control protocol (TCP 1501). The vendored MungaSocket is client-only, so this reimplements the identical framing ([16-byte header][12-byte base + body], dispatched by ClientID+MessageID) for the listening side, reusing the vendored message classes' WriteTo/BinaryReader serialization. - PodSimulator: the ApplicationState machine driven by the console's messages - answers StateQuery, reassembles the streamed egg and acknowledges it, and walks WaitingForEgg -> LoadingMission -> WaitingForLaunch -> RunningMission and back on Run/Stop/Abort/Suspend/Resume. - VPodForm: live display of listening/connection status, the colour-coded ApplicationState, an egg viewer (fields + summary), and a newest-first protocol log. A Red Planet / BattleTech toggle changes which ApplicationID the pod reports, live, so one vPOD stands in for either game. - PodArguments: parses the real client's launch flags (-net/-app/-lc/-mr/ -host/-res). Deployable from Manage Site -> Install Product: a catalog product in RedPlanet\Apps.xml (Game Client / Live Camera / Mission Review entries) plus pack.ps1, which builds dist\vPOD.zip laying out vPOD\vPOD.exe for the launcher to extract to C:\Games\vPOD. CatalogTests updated to 5 products / 11 entries with the four vPOD entry assertions (88/88 pass). TeslaConsole.csproj excludes vPOD\** from its **/*.cs glob; the project is added to the solution. Verified end-to-end over real TCP: a console-role client using the vendored MungaSocket drives connect -> WaitingForEgg -> stream egg -> (ack) -> WaitingForLaunch -> Run -> RunningMission -> Stop -> WaitingForEgg, with vPOD reporting the correct state at each step. MungaGame (what the console's game windows use) is a thin wrapper over MungaSocket, so this exercises the exact wire behaviour. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
308 lines
7.8 KiB
C#
308 lines
7.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using Munga.Net;
|
|
|
|
namespace VPod;
|
|
|
|
/// <summary>
|
|
/// Emulates a game client's ApplicationState machine over the Munga control
|
|
/// channel, driving it from the messages the console sends. This is the brain of
|
|
/// vPOD: it answers state queries, reassembles the streamed egg, and walks the
|
|
/// pod through the load/run/stop lifecycle the console's game window expects.
|
|
///
|
|
/// The console (RPGame/BTGame NetworkScan) gates purely on the reported
|
|
/// ApplicationState:
|
|
/// WaitingForEgg -- ready; console streams the egg here
|
|
/// (egg complete) -> LoadingMission -> WaitingForLaunch (console's "Loaded")
|
|
/// RunMission -> LaunchingMission -> RunningMission (console's "Running")
|
|
/// StopMission -> EndingMission -> WaitingForEgg (mission over/reset)
|
|
/// AbortMission -> AbortingMission -> WaitingForEgg
|
|
/// Intermediate states are shown for realism; the console only waits on the gate
|
|
/// states, so a short delay between them is harmless.
|
|
/// </summary>
|
|
internal sealed class PodSimulator
|
|
{
|
|
private const int NetworkManagerFromHost = 0;
|
|
|
|
private readonly MungaPodServer mServer;
|
|
private readonly object mLock = new object();
|
|
|
|
private ApplicationState mState = ApplicationState.InitializingState;
|
|
private ApplicationID mApplicationId;
|
|
private int mHostId;
|
|
private int mTransitionDelayMs = 400;
|
|
|
|
// Egg reassembly
|
|
private byte[] mEggBuffer;
|
|
private int mEggTotal;
|
|
private int mEggReceived;
|
|
private readonly HashSet<int> mEggChunks = new HashSet<int>();
|
|
private string mEggText;
|
|
|
|
// Cancels a pending delayed transition when a newer one supersedes it.
|
|
private int mTransitionToken;
|
|
private Timer mTransitionTimer;
|
|
|
|
public event Action<ApplicationState> StateChanged;
|
|
public event Action<string> EggReceived; // full egg text (NUL-normalized)
|
|
public event Action EggProgress; // a chunk arrived (mid-transfer)
|
|
public event Action<string> Log;
|
|
|
|
public PodSimulator(MungaPodServer server, ApplicationID applicationId, int hostId)
|
|
{
|
|
mServer = server;
|
|
mApplicationId = applicationId;
|
|
mHostId = hostId;
|
|
}
|
|
|
|
public ApplicationState State { get { lock (mLock) { return mState; } } }
|
|
|
|
public ApplicationID ApplicationId
|
|
{
|
|
get { lock (mLock) { return mApplicationId; } }
|
|
set
|
|
{
|
|
lock (mLock) { mApplicationId = value; }
|
|
Log?.Invoke("Reported application changed to " + value + ".");
|
|
}
|
|
}
|
|
|
|
public int HostId { get { lock (mLock) { return mHostId; } } }
|
|
|
|
public int TransitionDelayMs
|
|
{
|
|
get { lock (mLock) { return mTransitionDelayMs; } }
|
|
set { lock (mLock) { mTransitionDelayMs = Math.Max(0, value); } }
|
|
}
|
|
|
|
public string EggText { get { lock (mLock) { return mEggText; } } }
|
|
|
|
public int EggPercent
|
|
{
|
|
get
|
|
{
|
|
lock (mLock)
|
|
{
|
|
if (mEggTotal <= 0) return 0;
|
|
return (int)(100L * mEggReceived / mEggTotal);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Bring the pod online: Initializing -> WaitingForEgg.</summary>
|
|
public void PowerOn()
|
|
{
|
|
SetState(ApplicationState.InitializingState);
|
|
ScheduleTransition(ApplicationState.WaitingForEgg, ResetEggOnArrive: true);
|
|
}
|
|
|
|
/// <summary>Operator reset: drop any mission and return to WaitingForEgg immediately.</summary>
|
|
public void Reset()
|
|
{
|
|
lock (mLock)
|
|
{
|
|
ClearEggLocked();
|
|
CancelPendingLocked();
|
|
}
|
|
SetState(ApplicationState.WaitingForEgg);
|
|
Log?.Invoke("Reset to WaitingForEgg.");
|
|
}
|
|
|
|
public void HandleMessage(MungaPodServer.Incoming incoming)
|
|
{
|
|
MungaMessage msg = incoming?.Message;
|
|
if (msg == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
switch (msg)
|
|
{
|
|
case StateQueryMessage _:
|
|
RespondState();
|
|
break;
|
|
|
|
case EggFileMessage egg:
|
|
HandleEggChunk(egg);
|
|
break;
|
|
|
|
case RunMissionMessage _:
|
|
Log?.Invoke("<- RunMissionMessage");
|
|
if (State == ApplicationState.WaitingForLaunch)
|
|
{
|
|
SetState(ApplicationState.LaunchingMission);
|
|
mServer.Send(new ReadyToRunMessage(HostId), 0, HostId);
|
|
ScheduleTransition(ApplicationState.RunningMission);
|
|
}
|
|
break;
|
|
|
|
case StopMissionMessage _:
|
|
Log?.Invoke("<- StopMissionMessage");
|
|
if (State == ApplicationState.RunningMission || State == ApplicationState.SuspendingMission)
|
|
{
|
|
SetState(ApplicationState.EndingMission);
|
|
ScheduleTransition(ApplicationState.WaitingForEgg, ResetEggOnArrive: true);
|
|
}
|
|
break;
|
|
|
|
case AbortMissionMessage _:
|
|
Log?.Invoke("<- AbortMissionMessage");
|
|
SetState(ApplicationState.AbortingMission);
|
|
mServer.Send(new AbortMissionResponseMessage(HostId), 0, HostId);
|
|
ScheduleTransition(ApplicationState.WaitingForEgg, ResetEggOnArrive: true);
|
|
break;
|
|
|
|
case SuspendMissionMessage _:
|
|
Log?.Invoke("<- SuspendMissionMessage");
|
|
if (State == ApplicationState.RunningMission)
|
|
{
|
|
SetState(ApplicationState.SuspendingMission);
|
|
}
|
|
break;
|
|
|
|
case ResumeMissionMessage _:
|
|
Log?.Invoke("<- ResumeMissionMessage");
|
|
if (State == ApplicationState.SuspendingMission)
|
|
{
|
|
SetState(ApplicationState.ResumingMission);
|
|
ScheduleTransition(ApplicationState.RunningMission);
|
|
}
|
|
break;
|
|
|
|
case LoadMissionMessage _:
|
|
Log?.Invoke("<- LoadMissionMessage");
|
|
if (State == ApplicationState.WaitingForEgg)
|
|
{
|
|
SetState(ApplicationState.LoadingMission);
|
|
ScheduleTransition(ApplicationState.WaitingForLaunch);
|
|
}
|
|
break;
|
|
|
|
case CheckLoadMessage _:
|
|
Log?.Invoke("<- CheckLoadMessage");
|
|
break;
|
|
|
|
case LightsOutMissionMessage _:
|
|
Log?.Invoke("<- LightsOutMissionMessage");
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void RespondState()
|
|
{
|
|
ApplicationState state;
|
|
ApplicationID app;
|
|
int host;
|
|
lock (mLock)
|
|
{
|
|
state = mState;
|
|
app = mApplicationId;
|
|
host = mHostId;
|
|
}
|
|
mServer.Send(new StateResponseMessage(host, state, app), 0, host);
|
|
}
|
|
|
|
private void HandleEggChunk(EggFileMessage egg)
|
|
{
|
|
bool complete = false;
|
|
string text = null;
|
|
lock (mLock)
|
|
{
|
|
if (mEggBuffer == null || mEggTotal != egg.NotationFileLength)
|
|
{
|
|
mEggTotal = egg.NotationFileLength;
|
|
mEggBuffer = new byte[mEggTotal];
|
|
mEggReceived = 0;
|
|
mEggChunks.Clear();
|
|
}
|
|
int offset = egg.SequenceNumber * 1000;
|
|
int len = egg.ThisMessageLength;
|
|
if (offset >= 0 && offset + len <= mEggBuffer.Length && mEggChunks.Add(egg.SequenceNumber))
|
|
{
|
|
Buffer.BlockCopy(egg.NotationData, 0, mEggBuffer, offset, len);
|
|
mEggReceived += len;
|
|
}
|
|
if (mEggReceived >= mEggTotal && mEggTotal > 0)
|
|
{
|
|
// On the wire the egg is NUL-delimited ASCII; show it line-per-field.
|
|
text = Encoding.ASCII.GetString(mEggBuffer).Replace('\0', '\n');
|
|
mEggText = text;
|
|
complete = true;
|
|
}
|
|
}
|
|
|
|
EggProgress?.Invoke();
|
|
|
|
if (complete)
|
|
{
|
|
Log?.Invoke($"Egg received ({mEggTotal} bytes, {mEggChunks.Count} chunks). Acknowledging.");
|
|
mServer.Send(new AcknowledgeEggFileMessage(), NetworkManagerFromHost, HostId);
|
|
EggReceived?.Invoke(text);
|
|
// WaitingForEgg -> LoadingMission -> WaitingForLaunch
|
|
SetState(ApplicationState.LoadingMission);
|
|
ScheduleTransition(ApplicationState.WaitingForLaunch);
|
|
}
|
|
}
|
|
|
|
private void SetState(ApplicationState state)
|
|
{
|
|
bool changed;
|
|
lock (mLock)
|
|
{
|
|
changed = mState != state;
|
|
mState = state;
|
|
}
|
|
if (changed)
|
|
{
|
|
Log?.Invoke("State -> " + state);
|
|
StateChanged?.Invoke(state);
|
|
}
|
|
}
|
|
|
|
private void ScheduleTransition(ApplicationState target, bool ResetEggOnArrive = false)
|
|
{
|
|
int token;
|
|
int delay;
|
|
lock (mLock)
|
|
{
|
|
token = ++mTransitionToken;
|
|
delay = mTransitionDelayMs;
|
|
mTransitionTimer?.Dispose();
|
|
mTransitionTimer = new Timer(delegate
|
|
{
|
|
bool proceed;
|
|
lock (mLock)
|
|
{
|
|
proceed = token == mTransitionToken;
|
|
if (proceed && ResetEggOnArrive)
|
|
{
|
|
ClearEggLocked();
|
|
}
|
|
}
|
|
if (proceed)
|
|
{
|
|
SetState(target);
|
|
}
|
|
}, null, delay, Timeout.Infinite);
|
|
}
|
|
}
|
|
|
|
private void CancelPendingLocked()
|
|
{
|
|
mTransitionToken++;
|
|
mTransitionTimer?.Dispose();
|
|
mTransitionTimer = null;
|
|
}
|
|
|
|
private void ClearEggLocked()
|
|
{
|
|
mEggBuffer = null;
|
|
mEggTotal = 0;
|
|
mEggReceived = 0;
|
|
mEggChunks.Clear();
|
|
mEggText = null;
|
|
}
|
|
}
|