// PORT: adds or replaces whole mob_proto records (TMobTable, 40250 layout); untouched records stay byte-identical. // Row: vnum type rank battle_type level size gold_min gold_max exp max_hp regen_cycle regen_pct def // ai_flag race_flag immune_flag str dex con int dam_min dam_max attack_speed move_speed aggressive_hp_pct // aggressive_sight attack_range enchant0..5 resist0..10 resurrection_vnum drop_item_vnum mount_capacity // on_click empire dam_multiply summon drain_sp color polymorph_item (skill_vnum skill_level)x5 // berserk stoneskin godspeed deathblow revive \t folder \t name #include "GameLib/StdAfx.h" #include "UserInterface/StdAfx.h" #include "UserInterface/PythonNonPlayer.h" #include "EterBase/lzo.h" #include #include #include #include #include #include #include #include using Mob=CPythonNonPlayer::TMobTable; static constexpr std::array mob_key{{4813894,18955,552631,6822045}}; static constexpr size_t kFields=69; static std::vector read_file(const char* path){ std::ifstream stream(path,std::ios::binary); if(!stream)throw std::runtime_error("cannot read proto"); return {std::istreambuf_iterator(stream),std::istreambuf_iterator()}; } static DWORD word(const std::vector& data,size_t offset){ if(offset+4>data.size())throw std::runtime_error("truncated proto"); DWORD value;std::memcpy(&value,data.data()+offset,4);return value; } static void put_word(std::vector& data,size_t offset,DWORD value){std::memcpy(data.data()+offset,&value,4);} static void dump(const Mob& r){ std::printf("%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %d %d %u %u %u",r.dwVnum,r.bType,r.bRank,r.bBattleType,r.bLevel,r.bSize, r.dwGoldMin,r.dwGoldMax,r.dwExp,r.dwMaxHP,r.bRegenCycle,r.bRegenPercent,r.wDef,r.dwAIFlag,r.dwRaceFlag,r.dwImmuneFlag, r.bStr,r.bDex,r.bCon,r.bInt,r.dwDamageRange[0],r.dwDamageRange[1],r.sAttackSpeed,r.sMovingSpeed,r.bAggresiveHPPct,r.wAggressiveSight,r.wAttackRange); for(auto value:r.cEnchants)std::printf(" %d",int(value)); for(auto value:r.cResists)std::printf(" %d",int(value)); std::printf(" %u %u %u %u %u %g %u %u %u %u",r.dwResurrectionVnum,r.dwDropItemVnum,r.bMountCapacity,r.bOnClickType,r.bEmpire, r.fDamMultiply,r.dwSummonVnum,r.dwDrainSP,r.dwMonsterColor,r.dwPolymorphItemVnum); for(const auto& skill:r.Skills)std::printf(" %u %u",skill.dwVnum,skill.bLevel); std::printf(" %u %u %u %u %u\t%.*s\t%.*s\n",r.bBerserkPoint,r.bStoneSkinPoint,r.bGodSpeedPoint,r.bDeathBlowPoint,r.bRevivePoint, int(strnlen(r.szFolder,sizeof(r.szFolder))),r.szFolder,int(strnlen(r.szLocaleName,sizeof(r.szLocaleName))),r.szLocaleName); } static Mob parse(const std::string& line){ auto second=line.rfind('\t');if(second==std::string::npos||second==0)throw std::runtime_error("mob row without name"); auto first=line.rfind('\t',second-1);if(first==std::string::npos)throw std::runtime_error("mob row without folder"); const std::string name=line.substr(second+1),folder=line.substr(first+1,second-first-1); std::istringstream stream(line.substr(0,first));std::array f{}; for(auto& value:f)if(!(stream>>value))throw std::runtime_error("mob row: "+line.substr(0,40)); double extra;if(stream>>extra)throw std::runtime_error("mob row has extra columns"); if(name.size()>CHARACTER_NAME_MAX_LEN)throw std::runtime_error("mob name too long"); if(folder.size()>64)throw std::runtime_error("mob folder too long"); Mob r{};size_t i=0;auto n=[&]{return static_cast(f[i++]);}; r.dwVnum=n();r.bType=n();r.bRank=n();r.bBattleType=n();r.bLevel=n();r.bSize=n(); r.dwGoldMin=n();r.dwGoldMax=n();r.dwExp=n();r.dwMaxHP=n();r.bRegenCycle=n();r.bRegenPercent=n();r.wDef=n(); r.dwAIFlag=n();r.dwRaceFlag=n();r.dwImmuneFlag=n();r.bStr=n();r.bDex=n();r.bCon=n();r.bInt=n(); r.dwDamageRange[0]=n();r.dwDamageRange[1]=n();r.sAttackSpeed=n();r.sMovingSpeed=n();r.bAggresiveHPPct=n(); r.wAggressiveSight=n();r.wAttackRange=n(); for(auto& value:r.cEnchants)value=n(); for(auto& value:r.cResists)value=n(); r.dwResurrectionVnum=n();r.dwDropItemVnum=n();r.bMountCapacity=n();r.bOnClickType=n();r.bEmpire=n(); r.fDamMultiply=float(f[i++]);r.dwSummonVnum=n();r.dwDrainSP=n();r.dwMonsterColor=n();r.dwPolymorphItemVnum=n(); for(auto& skill:r.Skills){skill.dwVnum=n();skill.bLevel=n();} r.bBerserkPoint=n();r.bStoneSkinPoint=n();r.bGodSpeedPoint=n();r.bDeathBlowPoint=n();r.bRevivePoint=n(); std::memcpy(r.szName,name.data(),name.size());std::memcpy(r.szLocaleName,name.data(),name.size()); std::memcpy(r.szFolder,folder.data(),folder.size()); return r; } int main(int argc,char** argv) { const bool dumping=argc==3&&!std::strcmp(argv[1],"--dump"); if(argc!=4&&!dumping){std::fprintf(stderr,"usage: mob_proto_patch in_proto rows.tsv out_proto | --dump proto\n");return 2;} if(dumping)argv[1]=argv[2]; try { static CLZO lzo; auto key=mob_key; auto input=read_file(argv[1]); if(input.size()<12 || std::memcmp(input.data(),"MMPT",4))throw std::runtime_error("mob proto signature"); CLZObject decoded; if(!CLZO::Instance().Decompress(decoded,input.data()+12,key.data()))throw std::runtime_error("mob proto decode"); if(decoded.GetSize()!=size_t(word(input,4))*sizeof(Mob))throw std::runtime_error("proto count/size mismatch"); std::map rows; for(DWORD i=0;i plain;for(const auto& pair:rows){const BYTE* bytes=reinterpret_cast(&pair.second);plain.insert(plain.end(),bytes,bytes+sizeof(Mob));} CLZObject encoded;if(!CLZO::Instance().CompressEncryptedMemory(encoded,plain.data(),plain.size(),key.data()))throw std::runtime_error("encode"); std::vector result(input.begin(),input.begin()+12);put_word(result,4,rows.size());put_word(result,8,encoded.GetSize()); result.insert(result.end(),encoded.GetBuffer(),encoded.GetBuffer()+encoded.GetSize()); CLZObject verified;if(!CLZO::Instance().Decompress(verified,result.data()+12,key.data()) || verified.GetSize()!=plain.size() || std::memcmp(verified.GetBuffer(),plain.data(),plain.size()))throw std::runtime_error("round-trip"); std::ofstream out(argv[3],std::ios::binary|std::ios::trunc); if(!out.write(reinterpret_cast(result.data()),result.size()))throw std::runtime_error("cannot write proto"); std::printf("mobs added=%zu replaced=%zu total=%zu; verified round-trip\n",added,replaced,rows.size());return 0; }catch(const std::exception& e){std::fprintf(stderr,"%s\n",e.what());return 1;} }