Skip to content

Commit

Permalink
Add missing data file CRC checks
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due committed Jul 14, 2018
1 parent 126956d commit 23b6efa
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 68 deletions.
11 changes: 11 additions & 0 deletions lib/framework/wzconfig.cpp
Expand Up @@ -141,6 +141,17 @@ WzConfig::WzConfig(const WzString &name, WzConfig::warning warning)
pCurrentObj = &mRoot;
}

bool WzConfig::isAtDocumentRoot() const
{
return pCurrentObj == &mRoot;
}

std::string WzConfig::compactStringRepresentation(const bool ensure_ascii) const
{
// Use the most compact representation of the JSON
return mRoot.dump(-1, ' ', ensure_ascii);
}

std::vector<WzString> WzConfig::childGroups() const
{
std::vector<WzString> keys;
Expand Down
4 changes: 4 additions & 0 deletions lib/framework/wzconfig.h
Expand Up @@ -129,6 +129,8 @@ class WzConfig
bool beginGroup(const WzString &name);
void endGroup();

bool isAtDocumentRoot() const;

WzString fileName() const
{
return mFilename;
Expand All @@ -151,6 +153,8 @@ class WzConfig
{
return mStatus;
}

std::string compactStringRepresentation(const bool ensure_ascii = false) const;
};

// Enable JSON support for custom types
Expand Down
88 changes: 68 additions & 20 deletions src/data.cpp
Expand Up @@ -66,7 +66,7 @@ uint32_t DataHash[DATA_MAXDATA] = {0};
* This is almost the same routine that Pumpkin had, minus the ugly bug :)
* And minus the old algorithm and debugging trace, replaced with a simple CRC...
*/
static UDWORD hashBuffer(uint8_t *pData, uint32_t size)
static UDWORD hashBuffer(const uint8_t *pData, uint32_t size)
{
char nl = '\n';
uint32_t crc = 0;
Expand Down Expand Up @@ -94,7 +94,7 @@ static UDWORD hashBuffer(uint8_t *pData, uint32_t size)

// create the hash for that data block.
// Data should be converted to Network byte order
static void calcDataHash(uint8_t *pBuffer, uint32_t size, uint32_t index)
static void calcDataHash(const uint8_t *pBuffer, uint32_t size, uint32_t index)
{
const uint32_t oldHash = DataHash[index];

Expand All @@ -115,6 +115,12 @@ static void calcDataHash(uint8_t *pBuffer, uint32_t size, uint32_t index)
return;
}

static void calcDataHash(const WzConfig &ini, uint32_t index)
{
std::string jsonDump = ini.compactStringRepresentation();
calcDataHash(reinterpret_cast<const uint8_t *>(jsonDump.data()), jsonDump.size(), index);
}

void resetDataHash()
{
UDWORD i;
Expand All @@ -140,7 +146,10 @@ void dataClearSaveFlag()
/* Load the body stats */
static bool bufferSBODYLoad(const char *fileName, void **ppData)
{
if (!loadBodyStats(fileName) || !allocComponentList(COMP_BODY, numBodyStats))
WzConfig ini(fileName, WzConfig::ReadOnlyAndRequired);
calcDataHash(ini, DATA_SBODY);

if (!loadBodyStats(ini) || !allocComponentList(COMP_BODY, numBodyStats))
{
return false;
}
Expand All @@ -159,7 +168,10 @@ static void dataReleaseStats(WZ_DECL_UNUSED void *pData)
/* Load the weapon stats */
static bool bufferSWEAPONLoad(const char *fileName, void **ppData)
{
if (!loadWeaponStats(fileName)
WzConfig ini(fileName, WzConfig::ReadOnlyAndRequired);
calcDataHash(ini, DATA_SWEAPON);

if (!loadWeaponStats(ini)
|| !allocComponentList(COMP_WEAPON, numWeaponStats))
{
return false;
Expand All @@ -173,7 +185,10 @@ static bool bufferSWEAPONLoad(const char *fileName, void **ppData)
/* Load the constructor stats */
static bool bufferSCONSTRLoad(const char *fileName, void **ppData)
{
if (!loadConstructStats(fileName)
WzConfig ini(fileName, WzConfig::ReadOnlyAndRequired);
calcDataHash(ini, DATA_SCONSTR);

if (!loadConstructStats(ini)
|| !allocComponentList(COMP_CONSTRUCT, numConstructStats))
{
return false;
Expand All @@ -187,7 +202,10 @@ static bool bufferSCONSTRLoad(const char *fileName, void **ppData)
/* Load the ECM stats */
static bool bufferSECMLoad(const char *fileName, void **ppData)
{
if (!loadECMStats(fileName)
WzConfig ini(fileName, WzConfig::ReadOnlyAndRequired);
calcDataHash(ini, DATA_SECM);

if (!loadECMStats(ini)
|| !allocComponentList(COMP_ECM, numECMStats))
{
return false;
Expand All @@ -201,7 +219,10 @@ static bool bufferSECMLoad(const char *fileName, void **ppData)
/* Load the Propulsion stats */
static bool bufferSPROPLoad(const char *fileName, void **ppData)
{
if (!loadPropulsionStats(fileName) || !allocComponentList(COMP_PROPULSION, numPropulsionStats))
WzConfig ini(fileName, WzConfig::ReadOnlyAndRequired);
calcDataHash(ini, DATA_SPROP);

if (!loadPropulsionStats(ini) || !allocComponentList(COMP_PROPULSION, numPropulsionStats))
{
return false;
}
Expand All @@ -213,7 +234,10 @@ static bool bufferSPROPLoad(const char *fileName, void **ppData)

static bool bufferSSENSORLoad(const char *fileName, void **ppData)
{
if (!loadSensorStats(fileName)
WzConfig ini(fileName, WzConfig::ReadOnlyAndRequired);
calcDataHash(ini, DATA_SSENSOR);

if (!loadSensorStats(ini)
|| !allocComponentList(COMP_SENSOR, numSensorStats))
{
return false;
Expand All @@ -227,7 +251,10 @@ static bool bufferSSENSORLoad(const char *fileName, void **ppData)
/* Load the Repair stats */
static bool bufferSREPAIRLoad(const char *fileName, void **ppData)
{
if (!loadRepairStats(fileName) || !allocComponentList(COMP_REPAIRUNIT, numRepairStats))
WzConfig ini(fileName, WzConfig::ReadOnlyAndRequired);
calcDataHash(ini, DATA_SREPAIR);

if (!loadRepairStats(ini) || !allocComponentList(COMP_REPAIRUNIT, numRepairStats))
{
return false;
}
Expand All @@ -240,7 +267,10 @@ static bool bufferSREPAIRLoad(const char *fileName, void **ppData)
/* Load the Brain stats */
static bool bufferSBRAINLoad(const char *fileName, void **ppData)
{
if (!loadBrainStats(fileName) || !allocComponentList(COMP_BRAIN, numBrainStats))
WzConfig ini(fileName, WzConfig::ReadOnlyAndRequired);
calcDataHash(ini, DATA_SBRAIN);

if (!loadBrainStats(ini) || !allocComponentList(COMP_BRAIN, numBrainStats))
{
return false;
}
Expand All @@ -252,12 +282,14 @@ static bool bufferSBRAINLoad(const char *fileName, void **ppData)
/* Load the PropulsionType stats */
static bool bufferSPROPTYPESLoad(const char *fileName, void **ppData)
{
if (!loadPropulsionTypes(fileName))
WzConfig ini(fileName, WzConfig::ReadOnlyAndRequired);
calcDataHash(ini, DATA_SPROPTY);

if (!loadPropulsionTypes(ini))
{
return false;
}


//not interested in this value
*ppData = nullptr;
return true;
Expand All @@ -279,7 +311,10 @@ static bool bufferSPROPSNDLoad(const char *fileName, void **ppData)
/* Load the STERRTABLE stats */
static bool bufferSTERRTABLELoad(const char *fileName, void **ppData)
{
if (!loadTerrainTable(fileName))
WzConfig ini(fileName, WzConfig::ReadOnlyAndRequired);
calcDataHash(ini, DATA_STERRT);

if (!loadTerrainTable(ini))
{
return false;
}
Expand All @@ -300,7 +335,10 @@ static bool bufferSBPIMDLoad(const char *fileName, void **ppData)
/* Load the Weapon Effect modifier stats */
static bool bufferSWEAPMODLoad(const char *fileName, void **ppData)
{
if (!loadWeaponModifiers(fileName))
WzConfig ini(fileName, WzConfig::ReadOnlyAndRequired);
calcDataHash(ini, DATA_SWEAPMOD);

if (!loadWeaponModifiers(ini))
{
return false;
}
Expand Down Expand Up @@ -334,7 +372,10 @@ static void dataSTEMPLRelease(WZ_DECL_UNUSED void *pData)
/* Load the Structure stats */
static bool bufferSSTRUCTLoad(const char *fileName, void **ppData)
{
if (!loadStructureStats(WzString::fromUtf8(fileName)))
WzConfig ini(fileName, WzConfig::ReadOnlyAndRequired);
calcDataHash(ini, DATA_SSTRUCT);

if (!loadStructureStats(ini))
{
return false;
}
Expand All @@ -359,7 +400,10 @@ static void dataSSTRUCTRelease(WZ_DECL_UNUSED void *pData)
/* Load the Structure strength modifier stats */
static bool bufferSSTRMODLoad(const char *fileName, void **ppData)
{
if (!loadStructureStrengthModifiers(fileName))
WzConfig ini(fileName, WzConfig::ReadOnlyAndRequired);
calcDataHash(ini, DATA_SSTRMOD);

if (!loadStructureStrengthModifiers(ini))
{
return false;
}
Expand All @@ -372,7 +416,10 @@ static bool bufferSSTRMODLoad(const char *fileName, void **ppData)
/* Load the Feature stats */
static bool bufferSFEATLoad(const char *fileName, void **ppData)
{
if (!loadFeatureStats(fileName))
WzConfig ini(fileName, WzConfig::ReadOnlyAndRequired);
calcDataHash(ini, DATA_SFEAT);

if (!loadFeatureStats(ini))
{
return false;
}
Expand All @@ -397,16 +444,17 @@ static void dataRESCHRelease(WZ_DECL_UNUSED void *pData)
/* Load the Research stats */
static bool bufferRESCHLoad(const char *fileName, void **ppData)
{
//calcDataHash((uint8_t *)pBuffer, size, DATA_RESCH);

//check to see if already loaded
if (!asResearch.empty())
{
//release previous data before loading in the new
dataRESCHRelease(nullptr);
}

if (!loadResearch(WzString::fromUtf8(fileName)))
WzConfig ini(fileName, WzConfig::ReadOnlyAndRequired);
calcDataHash(ini, DATA_RESCH);

if (!loadResearch(ini))
{
return false;
}
Expand Down
5 changes: 2 additions & 3 deletions src/feature.cpp
Expand Up @@ -23,7 +23,6 @@
* Load feature stats
*/
#include "lib/framework/frame.h"
#include "lib/framework/wzconfig.h"

#include "lib/gamelib/gtime.h"
#include "lib/sound/audio.h"
Expand Down Expand Up @@ -66,9 +65,9 @@ void featureInitVars()
}

/* Load the feature stats */
bool loadFeatureStats(const char *pFileName)
bool loadFeatureStats(WzConfig &ini)
{
WzConfig ini(WzString::fromUtf8(pFileName), WzConfig::ReadOnlyAndRequired);
ASSERT(ini.isAtDocumentRoot(), "WzConfig instance is in the middle of traversal");
std::vector<WzString> list = ini.childGroups();
asFeatureStats = new FEATURE_STATS[list.size()];
numFeatureStats = list.size();
Expand Down
3 changes: 2 additions & 1 deletion src/feature.h
Expand Up @@ -25,6 +25,7 @@
#define __INCLUDED_SRC_FEATURE_H__

#include "objectdef.h"
#include "lib/framework/wzconfig.h"

/* The statistics for the features */
extern FEATURE_STATS *asFeatureStats;
Expand All @@ -34,7 +35,7 @@ extern UDWORD numFeatureStats;
extern FEATURE_STATS *oilResFeature;

/* Load the feature stats */
bool loadFeatureStats(const char *pFileName);
bool loadFeatureStats(WzConfig &ini);

/* Release the feature stats memory */
void featureStatsShutDown();
Expand Down
5 changes: 2 additions & 3 deletions src/research.cpp
Expand Up @@ -27,7 +27,6 @@
#include <map>

#include "lib/framework/frame.h"
#include "lib/framework/wzconfig.h"
#include "lib/netplay/netplay.h"
#include "lib/ivis_opengl/imd.h"
#include "objects.h"
Expand Down Expand Up @@ -102,9 +101,9 @@ bool researchInitVars()
}

/** Load the research stats */
bool loadResearch(const WzString& filename)
bool loadResearch(WzConfig &ini)
{
WzConfig ini(filename, WzConfig::ReadOnlyAndRequired);
ASSERT(ini.isAtDocumentRoot(), "WzConfig instance is in the middle of traversal");
std::vector<WzString> list = ini.childGroups();
PLAYER_RESEARCH dummy;
memset(&dummy, 0, sizeof(dummy));
Expand Down
4 changes: 3 additions & 1 deletion src/research.h
Expand Up @@ -24,6 +24,8 @@
#ifndef __INCLUDED_SRC_RESEARCH_H__
#define __INCLUDED_SRC_RESEARCH_H__

#include "lib/framework/wzconfig.h"

#include "objectdef.h"

struct VIEWDATA;
Expand Down Expand Up @@ -81,7 +83,7 @@ extern UDWORD aDefaultSensor[MAX_PLAYERS];
extern UDWORD aDefaultECM[MAX_PLAYERS];
extern UDWORD aDefaultRepair[MAX_PLAYERS];

bool loadResearch(const WzString& filename);
bool loadResearch(WzConfig &ini);

/*function to check what can be researched for a particular player at any one
instant. Returns the number to research*/
Expand Down

0 comments on commit 23b6efa

Please sign in to comment.