Commit 6c80c8f9 authored by gerstrong@gmail.com's avatar gerstrong@gmail.com

Code cleanups

parent 3f9c08ca
......@@ -7,8 +7,8 @@
*
*/
#ifndef __TYPEDEFINITIONS_H__
#define __TYPEDEFINITIONS_H__
#ifndef TYPEDEFINITIONS_H
#define TYPEDEFINITIONS_H
// Some good basic types
......@@ -94,4 +94,4 @@ static inline auto MERGERLOFFSET(const unsigned long levelLongWord) -> unsigned
};
#endif /* __TYPEDEFINITIONS_H__ */
#endif /* TYPEDEFINITIONS_H */
......@@ -14,7 +14,6 @@ add_library(fileio CExeFile.cpp
CExeFile.h
CPatcher.h
crc.h
CSavedGameCoder.h
CSaveGameController.h
CTileLoader.h
CTileProperties.h
......
......@@ -66,7 +66,7 @@ public:
template <class T>
void encodeVariable(T value);
template <class S>
void encodeData(S structure);
void encodeData(const S structure);
template <class T>
void decodeVariable(T &variable);
template <class S>
......@@ -93,9 +93,6 @@ public:
private:
// bool loadSaveGameVersion5(const std::string &fname, OldSaveGameFormatV5& old);
// bool loadSaveGameVersion4(const std::string &fname, OldSaveGameFormatV4& old);
bool IsOldSGVersion5(const std::string& fname);
bool IsOldSGVersion4(const std::string& fname);
int getOldSGVersion(const std::string& fname);
......@@ -119,6 +116,69 @@ struct LoadGameEvent : public CEvent
{};
#include "CSavedGameCoder.h"
template <class S>
void CSaveGameController::encodeData(const S structure)
{
const size_t size = sizeof(S);
byte sizebuf[sizeof(size_t)];
byte databuf[sizeof(S)];
memcpy(sizebuf, &size, sizeof(size_t));
for( Uint32 i=0 ; i<sizeof(size_t) ; i++ )
{
m_datablock.push_back( sizebuf[i] );
}
memcpy(databuf, &structure, size);
for( Uint32 i=0 ; i<size ; i++ )
{
m_datablock.push_back( databuf[i] );
}
}
template <class S>
bool CSaveGameController::decodeData(S &structure)
{
size_t det_size = 0;
size_t req_size = sizeof(S);
const auto datablockSize = m_datablock.size();
if(m_offset+sizeof(size_t) > datablockSize)
return false;
memcpy(&det_size, &m_datablock.at(m_offset), sizeof(size_t));
m_offset += sizeof(size_t);
size_t copySize = 0;
if( det_size > req_size ) // This implementation is used for avoiding possible errors
{
if(m_offset+req_size > datablockSize)
{
return false;
}
copySize = req_size;
}
else
{
if(m_offset+det_size > datablockSize)
{
return false;
}
copySize = det_size;
}
void *dataPtr = &structure;
memcpy(dataPtr, &m_datablock.at(m_offset), copySize);
m_offset += det_size;
return true;
}
#endif /* CSAVEDGAME_H_ */
/*
* CSaveGameControllerCoder.h
*
* Created on: 24.11.2009
* Author: gerstrong
*
* Definition of special template functions
*
* They are part of the main header, because otherwise the compiler
* would not know how to apply the templates to those special
* methods
*/
#ifndef CPlayGameCoder_H_
#define CPlayGameCoder_H_
#include <base/TypeDefinitions.h>
#include <cstring>
// This functions are used for enconding/decoding a variable to the game data format.
// It makes everything platform independent
template <class S>
void CSaveGameController::encodeData(S structure)
{
size_t size = sizeof(S);
byte sizebuf[sizeof(size_t)];
byte databuf[size];
memcpy(sizebuf, &size, sizeof(size_t));
for( Uint32 i=0 ; i<sizeof(size_t) ; i++ )
m_datablock.push_back( sizebuf[i] );
memcpy(databuf, &structure, size);
for( Uint32 i=0 ; i<size ; i++ )
m_datablock.push_back( databuf[i] );
}
template <class S>
bool CSaveGameController::decodeData(S &structure)
{
size_t det_size = 0;
size_t req_size = sizeof(S);
const Uint32 datablockSize = m_datablock.size();
if(m_offset+sizeof(size_t) > datablockSize)
return false;
memcpy(&det_size, &m_datablock.at(m_offset), sizeof(size_t));
m_offset += sizeof(size_t);
if( det_size > req_size ) // This implementation is used for avoiding possible errors
{
if(m_offset+req_size > datablockSize)
return false;
memcpy(&structure, &m_datablock.at(m_offset), req_size);
}
else
{
if(m_offset+det_size > datablockSize)
return false;
memcpy(&structure, &m_datablock.at(m_offset), det_size);
}
m_offset += det_size;
return true;
}
#endif /* CSAVEDGAMECODER_H_ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment