- 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 {
checkItemInVault(&this->amount,this->amount);
checkItemInVault(&this->balance,this->balance);
return amount;
}
int Resource::getBalance() const {
checkItemInVault(&this->amount,this->amount);
checkItemInVault(&this->balance,this->balance);
return balance;
}
void Resource::setAmount(int amount) {
checkItemInVault(&this->amount,this->amount);
checkItemInVault(&this->balance,this->balance);
this->amount= amount;
addItemToVault(&this->amount,this->amount);
}
void Resource::setBalance(int balance) {
checkItemInVault(&this->amount,this->amount);
checkItemInVault(&this->balance,this->balance);
this->balance= balance;
addItemToVault(&this->balance,this->balance);
}
bool Resource::decAmount(int i) {
checkItemInVault(&this->amount,this->amount);
checkItemInVault(&this->balance,this->balance);
amount -= i;
addItemToVault(&this->amount,this->amount);
if(amount > 0) {

View File

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

View File

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

View File

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

View File

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

View File

@ -992,14 +992,13 @@ string ModeInfo::getString() const{
void ValueCheckerVault::addItemToVault(const void *ptr,int value) {
Checksum checksum;
checksum.addString(intToStr(value));
vaultList[ptr] = intToStr(checksum.getSum());
vaultList[ptr] = checksum.addInt(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 {
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(SystemFlags::VERBOSE_MODE_ENABLED) {
// 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)!");
}
Checksum checksum;
checksum.addString(intToStr(value));
if(iterFind->second != intToStr(checksum.getSum())) {
if(iterFind->second != checksum.addInt(value)) {
// 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);
// 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));
r= (cipher + r) * c1 + c2;
sum+= cipher;
sum += cipher;
}
void Checksum::addSum(int32 value) {
sum+= value;
sum += value;
}
void Checksum::addString(const string &value){
for(unsigned int i= 0; i<value.size(); ++i){
int32 Checksum::addInt(const int32 &value) {
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]);
}
}
void Checksum::addFile(const string &path){
void Checksum::addFile(const string &path) {
if(path != "") {
fileList[path] = 0;
}
}
bool Checksum::addFileToSum(const string &path){
bool Checksum::addFileToSum(const string &path) {
// OLD SLOW FILE I/O
/*