Files
TeslaRel410/CODE/RP/MUNGA/SPOOLER.CPP
T
CydandClaude Fable 5 fdd9ac9d97 Initial import: Tesla Release 4.10 (Tesla:BattleTech & Tesla:Red Planet)
Archival snapshot of the Virtual World Entertainment Tesla cockpit
software, 1994-1996: MUNGA engine and L4 pod layer source (Borland
C++ 5.0), BT/RP game code, and game content (models, audio, maps,
gauges, Division renderer data). Includes third-party libraries:
Division dVS/DPL graphics, HMI SOS audio, WATTCP networking.

Files are preserved byte-for-byte (.gitattributes disables all
line-ending conversion). README.md documents the layout, target
hardware, and toolchain.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-02 13:21:58 -05:00

298 lines
7.4 KiB
C++

#include <munga.hpp>
#pragma hdrstop
#if !defined(SPOOLER_HPP)
# include <spooler.hpp>
#endif
#if !defined(FILESTRM_HPP)
# include <filestrm.hpp>
#endif
//#############################################################################
//############################## SpoolFile ################################
//#############################################################################
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
SpoolFile::SpoolFile(
void *stream_start,
size_t stream_size,
size_t initial_offset
):
MemoryStream(stream_start, stream_size, initial_offset)
{
spoolState = Empty;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
SpoolFile::SpoolFile(SpoolFile& spool):
MemoryStream(
spool.streamStart,
spool.streamSize,
spool.GetBytesUsed()
)
{
spoolState = spool.spoolState;
Verify(spoolState == Playing);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
SpoolFile::~SpoolFile()
{
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
SpoolFile::SaveAs(const char *file_name)
{
FileStream output(file_name, True);
size_t length = GetBytesUsed();
output << length;
Rewind();
output.WriteBytes(GetPointer(), length);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
SpoolFile::Read(const char *file_name)
{
FileStream input(file_name);
if (!input.IsFileOpened())
{
spoolState = Empty;
return;
}
//
//-------------------------------
// Figure out how big the file is
//-------------------------------
//
size_t spool_size = 0;
input >> spool_size;
if (spool_size > GetBytesRemaining() || !spool_size)
{
spoolState = Empty;
}
//
//-----------
// Read it in
//-----------
//
else
{
input.ReadBytes(streamStart, spool_size);
streamSize = spool_size;
Rewind();
spoolState = Stored;
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
SpoolFile::SpoolPacket(NetworkPacket *packet)
{
Check(this);
Check_Pointer(packet);
//
//-------------------------------------------------------------------------
// Check to see if we are out of spool space. For now, if it happens, just
// die
//-------------------------------------------------------------------------
//
int length =
packet->messageData.messageLength + sizeof(NetworkPacketHeader);
int remaining = GetBytesRemaining();
if (remaining < length)
{
DEBUG_STREAM << "Error: Spool file ran out of memory!\n";
Exit(AbortExitCodeID);
}
//
//-----------------------------------------------------
// Copy into the memory stream, and advance the pointer
//-----------------------------------------------------
//
Mem_Copy(GetPointer(), packet, length, remaining);
AdvancePointer(length);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
NetworkPacket*
SpoolFile::NextPacket()
{
Check(this);
//
//--------------------------------------------------------------------------
// Check to see if anything is left in the spool. If only a partial message
// remains, die from an error
//-------------------------------------------------------------------------
//
if (!GetBytesRemaining())
{
return NULL;
}
NetworkPacket *packet = (NetworkPacket*)GetPointer();
int length =
packet->messageData.messageLength + sizeof(NetworkPacketHeader);
if (GetBytesRemaining() < length)
{
DEBUG_STREAM << "Error: Partial packet in spool file!\n";
Exit(AbortExitCodeID);
return NULL;
}
//
//-----------------------------------------------------
// Copy into the memory stream, and advance the pointer
//-----------------------------------------------------
//
AdvancePointer(length);
return (NetworkPacket*)GetPointer();
}
//#############################################################################
//################# MissionReviewApplicationManager #####################
//#############################################################################
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
MissionReviewApplicationManager::MissionReviewApplicationManager(
Scalar frame_rate,
int spool_count,
size_t spool_size
):
ApplicationManager(frame_rate)
{
//
//--------------------------------------------------------
// Allocate the space for the spool files and buffer table
//--------------------------------------------------------
//
spoolCount = spool_count;
spoolSize = spool_size;
spoolBuffers = new char* [spoolCount];
Register_Pointer(spoolBuffers);
spoolFiles = new SpoolFile* [spoolCount];
Register_Pointer(spoolFiles);
//
//--------------------------------------------------------
// Allocate each spool buffer, and initial the spool files to none
//--------------------------------------------------------
//
for (int i=0; i<spoolCount; ++i)
{
spoolBuffers[i] = new char[spoolSize];
Register_Pointer(spoolBuffers[i]);
spoolFiles[i] = new SpoolFile(spoolBuffers[i], spoolSize);
Register_Object(spoolFiles[i]);
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
MissionReviewApplicationManager::~MissionReviewApplicationManager()
{
for (int i=0; i<spoolCount; ++i)
{
if (spoolFiles[i])
{
Unregister_Object(spoolFiles[i]);
delete spoolFiles[i];
}
Unregister_Pointer(spoolBuffers[i]);
delete spoolBuffers[i];
}
Unregister_Pointer(spoolFiles);
delete spoolFiles;
Unregister_Pointer(spoolBuffers);
delete spoolBuffers;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
SpoolFile*
MissionReviewApplicationManager::GetEmptySpoolFile()
{
for (int i=0; i<spoolCount; ++i)
{
if (spoolFiles[i]->spoolState == SpoolFile::Empty)
{
spoolFiles[i]->spoolState = SpoolFile::Spooling;
return spoolFiles[i];
}
}
Fail("No spool files available!");
return NULL;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
SpoolFile*
MissionReviewApplicationManager::GetStoredSpoolFile()
{
for (int i=0; i<spoolCount; ++i)
{
if (spoolFiles[i]->spoolState == SpoolFile::Empty)
{
spoolFiles[i]->Read("last.spl");
if (spoolFiles[i]->spoolState == SpoolFile::Stored)
{
spoolFiles[i]->spoolState = SpoolFile::Playing;
return spoolFiles[i];
}
return NULL;
}
}
return NULL;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
MissionReviewApplicationManager::StoreSpoolFile(SpoolFile *spool_file)
{
for (int i=0; i<spoolCount; ++i)
{
if (spoolFiles[i] == spool_file)
{
Verify(spoolFiles[i]->spoolState == SpoolFile::Spooling);
spoolFiles[i]->spoolState = SpoolFile::Stored;
spoolFiles[i]->SaveAs("last.spl");
spoolFiles[i]->Rewind();
return;
}
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
MissionReviewApplicationManager::ReleaseSpoolFile(SpoolFile *spool_file)
{
for (int i=0; i<spoolCount; ++i)
{
if (spoolFiles[i] == spool_file)
{
Verify(spoolFiles[i]->spoolState != SpoolFile::Spooling);
spoolFiles[i]->spoolState = SpoolFile::Empty;
spoolFiles[i]->Rewind();
return;
}
}
}