- speed improvement for anti-hack checking

This commit is contained in:
Mark Vejvoda 2011-01-15 04:13:15 +00:00
parent 3dac5df814
commit a7ca23d18e
7 changed files with 35 additions and 79 deletions

View File

@ -70,42 +70,29 @@ string Resource::getDescription() const {
int Resource::getAmount() const { int Resource::getAmount() const {
checkItemInVault(&this->amount,this->amount); checkItemInVault(&this->amount,this->amount);
checkItemInVault(&this->balance,this->balance);
return amount; return amount;
} }
int Resource::getBalance() const { int Resource::getBalance() const {
checkItemInVault(&this->amount,this->amount);
checkItemInVault(&this->balance,this->balance); checkItemInVault(&this->balance,this->balance);
return balance; return balance;
} }
void Resource::setAmount(int amount) { void Resource::setAmount(int amount) {
checkItemInVault(&this->amount,this->amount); checkItemInVault(&this->amount,this->amount);
checkItemInVault(&this->balance,this->balance);
this->amount= amount; this->amount= amount;
addItemToVault(&this->amount,this->amount); addItemToVault(&this->amount,this->amount);
} }
void Resource::setBalance(int balance) { void Resource::setBalance(int balance) {
checkItemInVault(&this->amount,this->amount);
checkItemInVault(&this->balance,this->balance); checkItemInVault(&this->balance,this->balance);
this->balance= balance; this->balance= balance;
addItemToVault(&this->balance,this->balance); addItemToVault(&this->balance,this->balance);
} }
bool Resource::decAmount(int i) { bool Resource::decAmount(int i) {
checkItemInVault(&this->amount,this->amount); checkItemInVault(&this->amount,this->amount);
checkItemInVault(&this->balance,this->balance);
amount -= i; amount -= i;
addItemToVault(&this->amount,this->amount); addItemToVault(&this->amount,this->amount);
if(amount > 0) { if(amount > 0) {

View File

@ -912,9 +912,7 @@ void Unit::born() {
setCurrSkill(scStop); setCurrSkill(scStop);
checkItemInVault(&this->hp,this->hp); checkItemInVault(&this->hp,this->hp);
hp= type->getMaxHp(); hp= type->getMaxHp();
addItemToVault(&this->hp,this->hp); addItemToVault(&this->hp,this->hp);
} }
@ -1122,13 +1120,10 @@ void Unit::tick() {
//regenerate hp //regenerate hp
if(type->getHpRegeneration() >= 0) { if(type->getHpRegeneration() >= 0) {
checkItemInVault(&this->hp,this->hp); checkItemInVault(&this->hp,this->hp);
hp += type->getHpRegeneration();
hp+= type->getHpRegeneration();
if(hp > type->getTotalMaxHp(&totalUpgrade)) { if(hp > type->getTotalMaxHp(&totalUpgrade)) {
hp = type->getTotalMaxHp(&totalUpgrade); hp = type->getTotalMaxHp(&totalUpgrade);
} }
addItemToVault(&this->hp,this->hp); addItemToVault(&this->hp,this->hp);
} }
// If we have negative regeneration then check if the unit should die // If we have negative regeneration then check if the unit should die
@ -1150,14 +1145,11 @@ void Unit::tick() {
} }
checkItemInVault(&this->ep,this->ep); checkItemInVault(&this->ep,this->ep);
//regenerate ep //regenerate ep
ep += type->getEpRegeneration(); ep += type->getEpRegeneration();
if(ep > type->getTotalMaxEp(&totalUpgrade)){
if(ep>type->getTotalMaxEp(&totalUpgrade)){ ep = type->getTotalMaxEp(&totalUpgrade);
ep= type->getTotalMaxEp(&totalUpgrade);
} }
addItemToVault(&this->ep,this->ep); addItemToVault(&this->ep,this->ep);
} }
} }
@ -1181,10 +1173,8 @@ bool Unit::computeEp() {
} }
checkItemInVault(&this->ep,this->ep); checkItemInVault(&this->ep,this->ep);
//decrease ep //decrease ep
ep -= currSkill->getEpCost(); ep -= currSkill->getEpCost();
addItemToVault(&this->ep,this->ep); addItemToVault(&this->ep,this->ep);
if(getType() == NULL) { if(getType() == NULL) {
@ -1196,7 +1186,6 @@ bool Unit::computeEp() {
if(ep > getType()->getTotalMaxEp(&totalUpgrade)){ if(ep > getType()->getTotalMaxEp(&totalUpgrade)){
ep = getType()->getTotalMaxEp(&totalUpgrade); ep = getType()->getTotalMaxEp(&totalUpgrade);
} }
addItemToVault(&this->ep,this->ep); addItemToVault(&this->ep,this->ep);
return false; return false;
@ -1212,13 +1201,10 @@ bool Unit::repair(){
//increase hp //increase hp
checkItemInVault(&this->hp,this->hp); checkItemInVault(&this->hp,this->hp);
hp += getType()->getMaxHp()/type->getProductionTime() + 1; hp += getType()->getMaxHp()/type->getProductionTime() + 1;
if(hp > (getType()->getTotalMaxHp(&totalUpgrade))) { if(hp > (getType()->getTotalMaxHp(&totalUpgrade))) {
hp = getType()->getTotalMaxHp(&totalUpgrade); hp = getType()->getTotalMaxHp(&totalUpgrade);
addItemToVault(&this->hp,this->hp); addItemToVault(&this->hp,this->hp);
return true; return true;
} }
addItemToVault(&this->hp,this->hp); addItemToVault(&this->hp,this->hp);
@ -1237,9 +1223,7 @@ bool Unit::decHp(int i) {
} }
checkItemInVault(&this->hp,this->hp); checkItemInVault(&this->hp,this->hp);
hp -= i; hp -= i;
addItemToVault(&this->hp,this->hp); addItemToVault(&this->hp,this->hp);
if(type == NULL) { if(type == NULL) {
@ -1257,7 +1241,6 @@ bool Unit::decHp(int i) {
if(hp <= 0) { if(hp <= 0) {
alive= false; alive= false;
hp=0; hp=0;
addItemToVault(&this->hp,this->hp); addItemToVault(&this->hp,this->hp);
stopDamageParticles(); stopDamageParticles();
@ -1365,9 +1348,7 @@ void Unit::applyUpgrade(const UpgradeType *upgradeType){
totalUpgrade.sum(upgradeType); totalUpgrade.sum(upgradeType);
checkItemInVault(&this->hp,this->hp); checkItemInVault(&this->hp,this->hp);
hp += upgradeType->getMaxHp(); hp += upgradeType->getMaxHp();
addItemToVault(&this->hp,this->hp); addItemToVault(&this->hp,this->hp);
} }
} }
@ -1386,9 +1367,7 @@ void Unit::incKills(){
totalUpgrade.incLevel(type); totalUpgrade.incLevel(type);
checkItemInVault(&this->hp,this->hp); checkItemInVault(&this->hp,this->hp);
hp += totalUpgrade.getMaxHp()-maxHp; hp += totalUpgrade.getMaxHp()-maxHp;
addItemToVault(&this->hp,this->hp); addItemToVault(&this->hp,this->hp);
} }
} }
@ -1417,9 +1396,7 @@ bool Unit::morph(const MorphCommandType *mct){
faction->deApplyStaticCosts(type); faction->deApplyStaticCosts(type);
checkItemInVault(&this->hp,this->hp); checkItemInVault(&this->hp,this->hp);
hp += morphUnitType->getMaxHp() - type->getMaxHp(); hp += morphUnitType->getMaxHp() - type->getMaxHp();
addItemToVault(&this->hp,this->hp); addItemToVault(&this->hp,this->hp);
type= morphUnitType; type= morphUnitType;

View File

@ -141,43 +141,32 @@ void UnitType::load(int id,const string &dir, const TechTree *techTree, const Fa
//size //size
//checkItemInVault(&(this->size),this->size); //checkItemInVault(&(this->size),this->size);
size= parametersNode->getChild("size")->getAttribute("value")->getIntValue(); size= parametersNode->getChild("size")->getAttribute("value")->getIntValue();
addItemToVault(&(this->size),this->size); addItemToVault(&(this->size),this->size);
//height //height
//checkItemInVault(&(this->height),this->height); //checkItemInVault(&(this->height),this->height);
height= parametersNode->getChild("height")->getAttribute("value")->getIntValue(); height= parametersNode->getChild("height")->getAttribute("value")->getIntValue();
addItemToVault(&(this->height),this->height); addItemToVault(&(this->height),this->height);
//maxHp //maxHp
//checkItemInVault(&(this->maxHp),this->maxHp); //checkItemInVault(&(this->maxHp),this->maxHp);
maxHp = parametersNode->getChild("max-hp")->getAttribute("value")->getIntValue(); maxHp = parametersNode->getChild("max-hp")->getAttribute("value")->getIntValue();
addItemToVault(&(this->maxHp),this->maxHp); addItemToVault(&(this->maxHp),this->maxHp);
//hpRegeneration //hpRegeneration
//checkItemInVault(&(this->hpRegeneration),this->hpRegeneration); //checkItemInVault(&(this->hpRegeneration),this->hpRegeneration);
hpRegeneration= parametersNode->getChild("max-hp")->getAttribute("regeneration")->getIntValue(); hpRegeneration= parametersNode->getChild("max-hp")->getAttribute("regeneration")->getIntValue();
addItemToVault(&(this->hpRegeneration),this->hpRegeneration); addItemToVault(&(this->hpRegeneration),this->hpRegeneration);
//maxEp //maxEp
//checkItemInVault(&(this->maxEp),this->maxEp); //checkItemInVault(&(this->maxEp),this->maxEp);
maxEp= parametersNode->getChild("max-ep")->getAttribute("value")->getIntValue(); maxEp= parametersNode->getChild("max-ep")->getAttribute("value")->getIntValue();
addItemToVault(&(this->maxEp),this->maxEp); addItemToVault(&(this->maxEp),this->maxEp);
if(maxEp != 0) { if(maxEp != 0) {
//epRegeneration //epRegeneration
//checkItemInVault(&(this->epRegeneration),this->epRegeneration); //checkItemInVault(&(this->epRegeneration),this->epRegeneration);
epRegeneration= parametersNode->getChild("max-ep")->getAttribute("regeneration")->getIntValue(); epRegeneration= parametersNode->getChild("max-ep")->getAttribute("regeneration")->getIntValue();
} }
addItemToVault(&(this->epRegeneration),this->epRegeneration); addItemToVault(&(this->epRegeneration),this->epRegeneration);
@ -185,7 +174,6 @@ void UnitType::load(int id,const string &dir, const TechTree *techTree, const Fa
//maxUnitCount //maxUnitCount
if(parametersNode->hasChild("max-unit-count")) { if(parametersNode->hasChild("max-unit-count")) {
//checkItemInVault(&(this->maxUnitCount),this->maxUnitCount); //checkItemInVault(&(this->maxUnitCount),this->maxUnitCount);
maxUnitCount= parametersNode->getChild("max-unit-count")->getAttribute("value")->getIntValue(); maxUnitCount= parametersNode->getChild("max-unit-count")->getAttribute("value")->getIntValue();
} }
addItemToVault(&(this->maxUnitCount),this->maxUnitCount); addItemToVault(&(this->maxUnitCount),this->maxUnitCount);
@ -532,10 +520,8 @@ const RepairCommandType *UnitType::getFirstRepairCommand(const UnitType *repaire
} }
bool UnitType::hasEmptyCellMap() const { bool UnitType::hasEmptyCellMap() const {
checkItemInVault(&(this->size),this->size); //checkItemInVault(&(this->size),this->size);
bool result = (size > 0); bool result = (size > 0);
for(int i = 0; result == true && i < size; ++i) { for(int i = 0; result == true && i < size; ++i) {
for(int j = 0; j < size; ++j){ for(int j = 0; j < size; ++j){
if(cellMap[i*size+j] == true) { if(cellMap[i*size+j] == true) {
@ -554,8 +540,7 @@ bool UnitType::getCellMapCell(int x, int y, CardinalDir facing) const {
throw runtime_error("cellMap == NULL"); throw runtime_error("cellMap == NULL");
} }
checkItemInVault(&(this->size),this->size); //checkItemInVault(&(this->size),this->size);
int tmp=0; int tmp=0;
switch (facing) { switch (facing) {
case CardinalDir::EAST: case CardinalDir::EAST:
@ -603,27 +588,23 @@ const SkillType *UnitType::getSkillType(const string &skillName, SkillClass skil
// ==================== totals ==================== // ==================== totals ====================
int UnitType::getTotalMaxHp(const TotalUpgrade *totalUpgrade) const{ int UnitType::getTotalMaxHp(const TotalUpgrade *totalUpgrade) const {
checkItemInVault(&(this->maxHp),this->maxHp); checkItemInVault(&(this->maxHp),this->maxHp);
return maxHp + totalUpgrade->getMaxHp(); return maxHp + totalUpgrade->getMaxHp();
} }
int UnitType::getTotalMaxEp(const TotalUpgrade *totalUpgrade) const{ int UnitType::getTotalMaxEp(const TotalUpgrade *totalUpgrade) const {
checkItemInVault(&(this->maxEp),this->maxEp); checkItemInVault(&(this->maxEp),this->maxEp);
return maxEp + totalUpgrade->getMaxEp(); return maxEp + totalUpgrade->getMaxEp();
} }
int UnitType::getTotalArmor(const TotalUpgrade *totalUpgrade) const { int UnitType::getTotalArmor(const TotalUpgrade *totalUpgrade) const {
checkItemInVault(&(this->armor),this->armor); checkItemInVault(&(this->armor),this->armor);
return armor + totalUpgrade->getArmor(); return armor + totalUpgrade->getArmor();
} }
int UnitType::getTotalSight(const TotalUpgrade *totalUpgrade) const{ int UnitType::getTotalSight(const TotalUpgrade *totalUpgrade) const {
checkItemInVault(&(this->sight),this->sight); checkItemInVault(&(this->sight),this->sight);
return sight + totalUpgrade->getSight(); return sight + totalUpgrade->getSight();
} }
@ -734,11 +715,10 @@ string UnitType::getReqDesc() const{
string resultTxt=""; string resultTxt="";
checkItemInVault(&(this->maxUnitCount),this->maxUnitCount); checkItemInVault(&(this->maxUnitCount),this->maxUnitCount);
if(getMaxUnitCount() > 0) { if(getMaxUnitCount() > 0) {
resultTxt+="\n"+lang.get("MaxUnitCount")+" "+intToStr(getMaxUnitCount()); resultTxt += "\n" + lang.get("MaxUnitCount") + " " + intToStr(getMaxUnitCount());
} }
if(resultTxt=="") if(resultTxt == "")
return ProducibleType::getReqDesc(); return ProducibleType::getReqDesc();
else else
return ProducibleType::getReqDesc()+"\nLimits: "+resultTxt; return ProducibleType::getReqDesc()+"\nLimits: "+resultTxt;

View File

@ -169,7 +169,7 @@ inline string trim (const string & s, const string & t = SPACES) {
class ValueCheckerVault { class ValueCheckerVault {
protected: protected:
std::map<const void *,string> vaultList; std::map<const void *,int32> vaultList;
void addItemToVault(const void *ptr,int value); void addItemToVault(const void *ptr,int value);
void checkItemInVault(const void *ptr,int value) const; void checkItemInVault(const void *ptr,int value) const;

View File

@ -27,7 +27,7 @@ namespace Shared{ namespace Util{
// class Checksum // class Checksum
// ===================================================== // =====================================================
class Checksum{ class Checksum {
private: private:
int32 sum; int32 sum;
int32 r; int32 r;
@ -48,6 +48,7 @@ public:
void addByte(int8 value); void addByte(int8 value);
void addString(const string &value); void addString(const string &value);
int32 addInt(const int32 &value);
void addFile(const string &path); void addFile(const string &path);
static void removeFileFromCache(const string file); static void removeFileFromCache(const string file);

View File

@ -992,14 +992,13 @@ string ModeInfo::getString() const{
void ValueCheckerVault::addItemToVault(const void *ptr,int value) { void ValueCheckerVault::addItemToVault(const void *ptr,int value) {
Checksum checksum; Checksum checksum;
checksum.addString(intToStr(value)); vaultList[ptr] = checksum.addInt(value);
vaultList[ptr] = intToStr(checksum.getSum());
// if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] add vault key [%p] value [%s] [%d]\n",__FILE__,__FUNCTION__,__LINE__,ptr,intToStr(checksum.getSum()).c_str(),value); // if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] add vault key [%p] value [%s] [%d]\n",__FILE__,__FUNCTION__,__LINE__,ptr,intToStr(checksum.getSum()).c_str(),value);
} }
void ValueCheckerVault::checkItemInVault(const void *ptr,int value) const { void ValueCheckerVault::checkItemInVault(const void *ptr,int value) const {
map<const void *,string>::const_iterator iterFind = vaultList.find(ptr); map<const void *,int32>::const_iterator iterFind = vaultList.find(ptr);
if(iterFind == vaultList.end()) { if(iterFind == vaultList.end()) {
if(SystemFlags::VERBOSE_MODE_ENABLED) { if(SystemFlags::VERBOSE_MODE_ENABLED) {
// printf("In [%s::%s Line: %d] check vault key [%p] value [%d]\n",__FILE__,__FUNCTION__,__LINE__,ptr,value); // printf("In [%s::%s Line: %d] check vault key [%p] value [%d]\n",__FILE__,__FUNCTION__,__LINE__,ptr,value);
@ -1011,8 +1010,7 @@ void ValueCheckerVault::checkItemInVault(const void *ptr,int value) const {
throw std::runtime_error("memory value has been unexpectedly modified (not found)!"); throw std::runtime_error("memory value has been unexpectedly modified (not found)!");
} }
Checksum checksum; Checksum checksum;
checksum.addString(intToStr(value)); if(iterFind->second != checksum.addInt(value)) {
if(iterFind->second != intToStr(checksum.getSum())) {
// if(SystemFlags::VERBOSE_MODE_ENABLED) { // if(SystemFlags::VERBOSE_MODE_ENABLED) {
// printf("In [%s::%s Line: %d] check vault key [%p] value [%s] [%d]\n",__FILE__,__FUNCTION__,__LINE__,ptr,intToStr(checksum.getSum()).c_str(),value); // printf("In [%s::%s Line: %d] check vault key [%p] value [%s] [%d]\n",__FILE__,__FUNCTION__,__LINE__,ptr,intToStr(checksum.getSum()).c_str(),value);
// for(map<const void *,string>::const_iterator iterFind = vaultList.begin(); // for(map<const void *,string>::const_iterator iterFind = vaultList.begin();

View File

@ -47,26 +47,39 @@ void Checksum::addByte(int8 value) {
int32 cipher= (value ^ (r >> 8)); int32 cipher= (value ^ (r >> 8));
r= (cipher + r) * c1 + c2; r= (cipher + r) * c1 + c2;
sum+= cipher; sum += cipher;
} }
void Checksum::addSum(int32 value) { void Checksum::addSum(int32 value) {
sum+= value; sum += value;
} }
void Checksum::addString(const string &value){ int32 Checksum::addInt(const int32 &value) {
for(unsigned int i= 0; i<value.size(); ++i){ int8 byte = (value >> 0) & 0xFF;
addByte(byte);
byte = (value >> 8) & 0xFF;
addByte(byte);
byte = (value >> 16) & 0xFF;
addByte(byte);
byte = (value >> 24) & 0xFF;
addByte(byte);
return sum;
}
void Checksum::addString(const string &value) {
for(unsigned int i = 0; i < value.size(); ++i) {
addByte(value[i]); addByte(value[i]);
} }
} }
void Checksum::addFile(const string &path){ void Checksum::addFile(const string &path) {
if(path != "") { if(path != "") {
fileList[path] = 0; fileList[path] = 0;
} }
} }
bool Checksum::addFileToSum(const string &path){ bool Checksum::addFileToSum(const string &path) {
// OLD SLOW FILE I/O // OLD SLOW FILE I/O
/* /*