OpenClonk
C4Network2ResChunk Class Reference

#include <C4Network2Res.h>

Inheritance diagram for C4Network2ResChunk:
[legend]
Collaboration diagram for C4Network2ResChunk:
[legend]

Public Member Functions

 C4Network2ResChunk ()
 
 ~C4Network2ResChunk () override
 
int32_t getResID () const
 
uint32_t getChunkNr () const
 
bool Set (C4Network2Res *pRes, uint32_t iChunk)
 
bool AddTo (C4Network2Res *pRes, C4Network2IO *pIO) const
 
void CompileFunc (StdCompiler *pComp) override
 
C4NetIOPacket pack (const C4NetIO::addr_t &addr=C4NetIO::addr_t()) const
 
C4NetIOPacket pack (uint8_t cStatus, const C4NetIO::addr_t &addr=C4NetIO::addr_t()) const
 
void unpack (const C4NetIOPacket &Pkt, char *pStatus=nullptr)
 

Protected Attributes

int32_t iResID
 
uint32_t iChunk
 
StdBuf Data
 

Detailed Description

Definition at line 305 of file C4Network2Res.h.

Constructor & Destructor Documentation

◆ C4Network2ResChunk()

C4Network2ResChunk::C4Network2ResChunk ( )
default

◆ ~C4Network2ResChunk()

C4Network2ResChunk::~C4Network2ResChunk ( )
overridedefault

Member Function Documentation

◆ AddTo()

bool C4Network2ResChunk::AddTo ( C4Network2Res pRes,
C4Network2IO pIO 
) const

Definition at line 1207 of file C4Network2Res.cpp.

1208 {
1209  assert(pRes); assert(pIO);
1210  const C4Network2ResCore &Core = pRes->getCore();
1211  // check
1212  if (iResID != pRes->getResID())
1213  {
1214 #ifdef C4NET2RES_DEBUG_LOG
1215  Application.InteractiveThread.ThreadLogS("C4Network2ResChunk(%d)::AddTo(%s [%d]): Resource ID mismatch!", (int) iResID, (const char *) Core.getFileName(), (int) pRes->getResID());
1216 #endif
1217  return false;
1218  }
1219  // calculate offset and size
1220  int32_t iOffset = iChunk * Core.getChunkSize();
1221  if (iOffset + Data.getSize() > Core.getFileSize())
1222  {
1223 #ifdef C4NET2RES_DEBUG_LOG
1224  Application.InteractiveThread.ThreadLogS("C4Network2ResChunk(%d)::AddTo(%s [%d]): Adding %d bytes at offset %d exceeds expected file size of %d!", (int) iResID, (const char *) Core.getFileName(), (int) pRes->getResID(), (int) Data.getSize(), (int) iOffset, (int) Core.getFileSize());
1225 #endif
1226  return false;
1227  }
1228  // open file
1229  int32_t f = pRes->OpenFileWrite();
1230  if (f == -1)
1231  {
1232 #ifdef C4NET2RES_DEBUG_LOG
1233  Application.InteractiveThread.ThreadLogS("C4Network2ResChunk(%d)::AddTo(%s [%d]): Open write file error: %s!", (int) iResID, (const char *) Core.getFileName(), (int) pRes->getResID(), strerror(errno));
1234 #endif
1235  return false;
1236  }
1237  // seek
1238  if (iOffset)
1239  if (lseek(f, iOffset, SEEK_SET) != iOffset)
1240  {
1241 #ifdef C4NET2RES_DEBUG_LOG
1242  Application.InteractiveThread.ThreadLogS("C4Network2ResChunk(%d)::AddTo(%s [%d]): lseek file error: %s!", (int) iResID, (const char *) Core.getFileName(), (int) pRes->getResID(), strerror(errno));
1243 #endif
1244  close(f);
1245  return false;
1246  }
1247  // write
1248  if (write(f, Data.getData(), Data.getSize()) != int32_t(Data.getSize()))
1249  {
1250 #ifdef C4NET2RES_DEBUG_LOG
1251  Application.InteractiveThread.ThreadLogS("C4Network2ResChunk(%d)::AddTo(%s [%d]): write error: %s!", (int) iResID, (const char *) Core.getFileName(), (int) pRes->getResID(), strerror(errno));
1252 #endif
1253  close(f);
1254  return false;
1255  }
1256  // ok, add chunks
1257  close(f);
1258  pRes->Chunks.AddChunk(iChunk);
1259  return true;
1260 }
C4Application Application
Definition: C4Globals.cpp:44
C4InteractiveThread InteractiveThread
Definition: C4Application.h:45
bool ThreadLogS(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
void AddChunk(int32_t iChunk)
uint32_t getChunkSize() const
Definition: C4Network2Res.h:95
const char * getFileName() const
Definition: C4Network2Res.h:94
uint32_t getFileSize() const
Definition: C4Network2Res.h:89
int32_t getResID() const
const C4Network2ResCore & getCore() const
C4Network2ResChunkData Chunks
int32_t OpenFileWrite()
size_t getSize() const
Definition: StdBuf.h:101
const void * getData() const
Definition: StdBuf.h:99

References C4Network2ResChunkData::AddChunk(), Application, C4Network2Res::Chunks, Data, C4Network2ResCore::getChunkSize(), C4Network2Res::getCore(), StdBuf::getData(), C4Network2ResCore::getFileName(), C4Network2ResCore::getFileSize(), C4Network2Res::getResID(), StdBuf::getSize(), iChunk, C4Application::InteractiveThread, iResID, C4Network2Res::OpenFileWrite(), and C4InteractiveThread::ThreadLogS().

Referenced by C4Network2Res::OnChunk().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CompileFunc()

void C4Network2ResChunk::CompileFunc ( StdCompiler pComp)
overridevirtual

Implements C4PacketBase.

Definition at line 1262 of file C4Network2Res.cpp.

1263 {
1264  // pack header
1265  pComp->Value(mkNamingAdapt(iResID, "ResID", -1));
1266  pComp->Value(mkNamingAdapt(iChunk, "Chunk", ~0U));
1267  // Data
1268  pComp->Value(mkNamingAdapt(Data, "Data"));
1269 }
StdNamingAdapt< T > mkNamingAdapt(T &&rValue, const char *szName)
Definition: StdAdaptors.h:92
void Value(const T &rStruct)
Definition: StdCompiler.h:161

References Data, iChunk, iResID, mkNamingAdapt(), and StdCompiler::Value().

Here is the call graph for this function:

◆ getChunkNr()

uint32_t C4Network2ResChunk::getChunkNr ( ) const
inline

Definition at line 318 of file C4Network2Res.h.

318 { return iChunk; }

References iChunk.

Referenced by C4Network2Res::OnChunk().

Here is the caller graph for this function:

◆ getResID()

int32_t C4Network2ResChunk::getResID ( ) const
inline

Definition at line 317 of file C4Network2Res.h.

317 { return iResID; }

References iResID.

Referenced by C4Network2Res::OnChunk().

Here is the caller graph for this function:

◆ pack() [1/2]

C4NetIOPacket C4PacketBase::pack ( const C4NetIO::addr_t addr = C4NetIO::addr_t()) const
inherited

Definition at line 150 of file C4Packet2.cpp.

151 {
152  return C4NetIOPacket(DecompileToBuf<StdCompilerBinWrite>(*this), addr);
153 }

Referenced by MkC4NetIOPacket().

Here is the caller graph for this function:

◆ pack() [2/2]

C4NetIOPacket C4PacketBase::pack ( uint8_t  cStatus,
const C4NetIO::addr_t addr = C4NetIO::addr_t() 
) const
inherited

Definition at line 155 of file C4Packet2.cpp.

156 {
157  return C4NetIOPacket(DecompileToBuf<StdCompilerBinWrite>(mkInsertAdapt(mkDecompileAdapt(*this), cStatus)), addr);
158 }
StdInsertAdapt< T, I > mkInsertAdapt(T &&rObj, I &&rIns, bool fBefore=true)
Definition: StdAdaptors.h:469
StdDecompileAdapt< T > mkDecompileAdapt(const T &rValue)
Definition: StdAdaptors.h:153

References mkDecompileAdapt(), and mkInsertAdapt().

Here is the call graph for this function:

◆ Set()

bool C4Network2ResChunk::Set ( C4Network2Res pRes,
uint32_t  iChunk 
)

Definition at line 1179 of file C4Network2Res.cpp.

1180 {
1181  const C4Network2ResCore &Core = pRes->getCore();
1182  iResID = pRes->getResID();
1183  iChunk = inChunk;
1184  // calculate offset and size
1185  int32_t iOffset = iChunk * Core.getChunkSize(),
1186  iSize = std::min<int32_t>(Core.getFileSize() - iOffset, C4NetResChunkSize);
1187  if (iSize < 0) { LogF("Network: could not get chunk from offset %d from resource file %s: File size is only %d!", iOffset, pRes->getFile(), Core.getFileSize()); return false; }
1188  // open file
1189  int32_t f = pRes->OpenFileRead();
1190  if (f == -1) { LogF("Network: could not open resource file %s!", pRes->getFile()); return false; }
1191  // seek
1192  if (iOffset)
1193  if (lseek(f, iOffset, SEEK_SET) != iOffset)
1194  { close(f); LogF("Network: could not read resource file %s!", pRes->getFile()); return false; }
1195  // read chunk of data
1196  char *pBuf = (char *) malloc(iSize);
1197  if (read(f, pBuf, iSize) != iSize)
1198  { free(pBuf); close(f); LogF("Network: could not read resource file %s!", pRes->getFile()); return false; }
1199  // set
1200  Data.Take(pBuf, iSize);
1201  // close
1202  close(f);
1203  // ok
1204  return true;
1205 }
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:262
const uint32_t C4NetResChunkSize
Definition: C4Network2Res.h:28
int iSize
Definition: TstC4NetIO.cpp:32
const char * getFile() const
int32_t OpenFileRead()
void Take(void *pnData, size_t inSize)
Definition: StdBuf.h:124

References C4NetResChunkSize, Data, C4Network2ResCore::getChunkSize(), C4Network2Res::getCore(), C4Network2Res::getFile(), C4Network2ResCore::getFileSize(), C4Network2Res::getResID(), iChunk, iResID, iSize, LogF(), C4Network2Res::OpenFileRead(), and StdBuf::Take().

Referenced by C4Network2Res::SendChunk().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ unpack()

void C4PacketBase::unpack ( const C4NetIOPacket Pkt,
char *  pStatus = nullptr 
)
inherited

Definition at line 160 of file C4Packet2.cpp.

161 {
162  if (pStatus) *pStatus = Pkt.getStatus();
163  CompileFromBuf<StdCompilerBinRead>(*this, pStatus ? Pkt.getPBuf() : Pkt.getRef());
164 }
uint8_t getStatus() const
Definition: C4NetIO.h:319
StdBuf getPBuf() const
Definition: C4NetIO.h:322
C4NetIOPacket getRef() const
Definition: C4NetIO.h:325

References C4NetIOPacket::getPBuf(), C4NetIOPacket::getRef(), and C4NetIOPacket::getStatus().

Referenced by C4IDPacket::C4IDPacket(), and C4Network2IO::HandlePacket().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ Data

StdBuf C4Network2ResChunk::Data
protected

Definition at line 314 of file C4Network2Res.h.

Referenced by AddTo(), CompileFunc(), and Set().

◆ iChunk

uint32_t C4Network2ResChunk::iChunk
protected

Definition at line 313 of file C4Network2Res.h.

Referenced by AddTo(), CompileFunc(), getChunkNr(), and Set().

◆ iResID

int32_t C4Network2ResChunk::iResID
protected

Definition at line 312 of file C4Network2Res.h.

Referenced by AddTo(), CompileFunc(), getResID(), and Set().


The documentation for this class was generated from the following files: