diff --git a/meson.build b/meson.build index 3d17d663b..46945ee0b 100644 --- a/meson.build +++ b/meson.build @@ -40,7 +40,6 @@ endif add_project_arguments(global_cpp_defs, language: ['cpp', 'c']) global_cpp_args = [ - '-fpermissive', '-Wshift-count-overflow', '-pipe', ] diff --git a/minecraft/Minecraft.World/net/minecraft/world/entity/item/FallingTile.cpp b/minecraft/Minecraft.World/net/minecraft/world/entity/item/FallingTile.cpp index aa088e0e8..12414a0b6 100644 --- a/minecraft/Minecraft.World/net/minecraft/world/entity/item/FallingTile.cpp +++ b/minecraft/Minecraft.World/net/minecraft/world/entity/item/FallingTile.cpp @@ -122,9 +122,9 @@ void FallingTile::tick() { if (tileEntity != nullptr) { CompoundTag* swap = new CompoundTag(); tileEntity->save(swap); - std::vector* allTags = tileData->getAllTags(); - for (auto it = allTags->begin(); - it != allTags->end(); ++it) { + std::vector allTags = tileData->getAllTags(); + for (auto it = allTags.begin(); + it != allTags.end(); ++it) { Tag* tag = *it; if (tag->getName().compare(L"x") == 0 || tag->getName().compare(L"y") == 0 || @@ -132,7 +132,6 @@ void FallingTile::tick() { continue; swap->put(tag->getName(), tag->copy()); } - delete allTags; tileEntity->load(swap); tileEntity->setChanged(); } diff --git a/minecraft/Minecraft.World/net/minecraft/world/level/BaseMobSpawner.cpp b/minecraft/Minecraft.World/net/minecraft/world/level/BaseMobSpawner.cpp index 1ebdb0348..8f7656571 100644 --- a/minecraft/Minecraft.World/net/minecraft/world/level/BaseMobSpawner.cpp +++ b/minecraft/Minecraft.World/net/minecraft/world/level/BaseMobSpawner.cpp @@ -133,12 +133,11 @@ std::shared_ptr BaseMobSpawner::loadDataAndAddEntity( CompoundTag* data = new CompoundTag(); entity->save(data); - std::vector* tags = getNextSpawnData()->tag->getAllTags(); - for (auto it = tags->begin(); it != tags->end(); ++it) { + std::vector tags = getNextSpawnData()->tag->getAllTags(); + for (auto it = tags.begin(); it != tags.end(); ++it) { Tag* tag = *it; data->put(tag->getName(), tag->copy()); } - delete tags; entity->load(data); if (entity->level != nullptr) entity->level->addEntity(entity); @@ -153,13 +152,12 @@ std::shared_ptr BaseMobSpawner::loadDataAndAddEntity( CompoundTag* mountData = new CompoundTag(); mount->save(mountData); - std::vector* ridingTags = ridingTag->getAllTags(); - for (auto it = ridingTags->begin(); it != ridingTags->end(); + std::vector ridingTags = ridingTag->getAllTags(); + for (auto it = ridingTags.begin(); it != ridingTags.end(); ++it) { Tag* tag = *it; mountData->put(tag->getName(), tag->copy()); } - delete ridingTags; mount->load(mountData); mount->moveTo(rider->x, rider->y, rider->z, rider->yRot, rider->xRot); diff --git a/minecraft/Minecraft.World/net/minecraft/world/level/GameRules.cpp b/minecraft/Minecraft.World/net/minecraft/world/level/GameRules.cpp index 7c40547a2..381af0602 100644 --- a/minecraft/Minecraft.World/net/minecraft/world/level/GameRules.cpp +++ b/minecraft/Minecraft.World/net/minecraft/world/level/GameRules.cpp @@ -127,8 +127,8 @@ CompoundTag *GameRules::createTag() void GameRules::loadFromTag(CompoundTag *tag) { - vector *allTags = tag->getAllTags(); - for (auto it = allTags->begin(); it != allTags->end(); ++it) + vector allTags = tag->getAllTags(); + for (auto it = allTags.begin(); it != allTags.end(); ++it) { Tag *ruleTag = *it; std::wstring ruleName = ruleTag->getName(); @@ -136,7 +136,6 @@ void GameRules::loadFromTag(CompoundTag *tag) set(ruleName, value); } - delete allTags; } // Need to delete returned vector. diff --git a/minecraft/Minecraft.World/net/minecraft/world/level/levelgen/structure/StructureFeature.cpp b/minecraft/Minecraft.World/net/minecraft/world/level/levelgen/structure/StructureFeature.cpp index db724ddea..d68807c07 100644 --- a/minecraft/Minecraft.World/net/minecraft/world/level/levelgen/structure/StructureFeature.cpp +++ b/minecraft/Minecraft.World/net/minecraft/world/level/levelgen/structure/StructureFeature.cpp @@ -252,8 +252,8 @@ void StructureFeature::restoreSavedData(Level* level) { } else { CompoundTag* fullTag = savedData->getFullTag(); - std::vector* allTags = fullTag->getAllTags(); - for (auto it = allTags->begin(); it != allTags->end(); ++it) { + std::vector allTags = fullTag->getAllTags(); + for (auto it = allTags.begin(); it != allTags.end(); ++it) { Tag* featureTag = *it; if (featureTag->getId() == Tag::TAG_Compound) { CompoundTag* ct = (CompoundTag*)featureTag; diff --git a/minecraft/Minecraft.World/net/minecraft/world/level/storage/SavedDataStorage.cpp b/minecraft/Minecraft.World/net/minecraft/world/level/storage/SavedDataStorage.cpp index 58ddfc21f..395906dd4 100644 --- a/minecraft/Minecraft.World/net/minecraft/world/level/storage/SavedDataStorage.cpp +++ b/minecraft/Minecraft.World/net/minecraft/world/level/storage/SavedDataStorage.cpp @@ -134,10 +134,10 @@ void SavedDataStorage::loadAuxValues() { dis.close(); Tag* tag; - std::vector* allTags = tags->getAllTags(); - auto itEnd = allTags->end(); - for (auto it = allTags->begin(); it != itEnd; it++) { - tag = *it; // tags->getAllTags()->at(i); + std::vector allTags = tags->getAllTags(); + auto itEnd = allTags.end(); + for (auto it = allTags.begin(); it != itEnd; it++) { + tag = *it; if (dynamic_cast(tag) != nullptr) { ShortTag* sTag = (ShortTag*)tag; @@ -146,7 +146,6 @@ void SavedDataStorage::loadAuxValues() { usedAuxIds.insert(uaiMapType::value_type(id, val)); } } - delete allTags; } } diff --git a/minecraft/nbt/include/nbt/CompoundTag.h b/minecraft/nbt/include/nbt/CompoundTag.h index 919e4c7a6..e9df4f61a 100644 --- a/minecraft/nbt/include/nbt/CompoundTag.h +++ b/minecraft/nbt/include/nbt/CompoundTag.h @@ -11,21 +11,20 @@ #include "ByteArrayTag.h" #include "IntArrayTag.h" +#include #include class CompoundTag : public Tag { private: - std::unordered_map tags; + std::unordered_map> tags; public: CompoundTag() : Tag(L"") {} CompoundTag(const std::wstring& name) : Tag(name) {} void write(DataOutput* dos) { - auto itEnd = tags.end(); - for (std::unordered_map::iterator it = tags.begin(); - it != itEnd; it++) { - Tag::writeNamedTag(it->second, dos); + for (auto& [key, value] : tags) { + Tag::writeNamedTag(value.get(), dos); } dos->writeByte(Tag::TAG_End); } @@ -39,22 +38,19 @@ public: return; } tags.clear(); - Tag* tag; - while ((tag = Tag::readNamedTag(dis))->getId() != Tag::TAG_End) { - tags[tag->getName()] = tag; + for (;;) { + std::unique_ptr tag(Tag::readNamedTag(dis)); + if (tag->getId() == Tag::TAG_End) break; + auto name = tag->getName(); + tags[name] = std::move(tag); } - delete tag; } - std::vector* getAllTags() // 4J - was collection - { - // 4J - was return tags.values(); - std::vector* ret = new std::vector; - - auto itEnd = tags.end(); - for (std::unordered_map::iterator it = tags.begin(); - it != itEnd; it++) { - ret->push_back(it->second); + std::vector getAllTags() { + std::vector ret; + ret.reserve(tags.size()); + for (auto& [key, value] : tags) { + ret.push_back(value.get()); } return ret; } @@ -62,47 +58,49 @@ public: uint8_t getId() { return TAG_Compound; } void put(const std::wstring& name, Tag* tag) { - tags[name] = tag->setName(name); + tag->setName(name); + tags[name] = std::unique_ptr(tag); } void putByte(const std::wstring& name, uint8_t value) { - tags[name] = (new ByteTag(name, value)); + tags[name] = std::make_unique(name, value); } void putShort(const std::wstring& name, short value) { - tags[name] = (new ShortTag(name, value)); + tags[name] = std::make_unique(name, value); } void putInt(const std::wstring& name, int value) { - tags[name] = (new IntTag(name, value)); + tags[name] = std::make_unique(name, value); } void putLong(const std::wstring& name, int64_t value) { - tags[name] = (new LongTag(name, value)); + tags[name] = std::make_unique(name, value); } void putFloat(const std::wstring& name, float value) { - tags[name] = (new FloatTag(name, value)); + tags[name] = std::make_unique(name, value); } void putDouble(const std::wstring& name, double value) { - tags[name] = (new DoubleTag(name, value)); + tags[name] = std::make_unique(name, value); } void putString(const std::wstring& name, const std::wstring& value) { - tags[name] = (new StringTag(name, value)); + tags[name] = std::make_unique(name, value); } void putByteArray(const std::wstring& name, std::vector& value) { - tags[name] = (new ByteArrayTag(name, value)); + tags[name] = std::make_unique(name, value); } void putIntArray(const std::wstring& name, std::vector& value) { - tags[name] = (new IntArrayTag(name, value)); + tags[name] = std::make_unique(name, value); } void putCompound(const std::wstring& name, CompoundTag* value) { - tags[name] = value->setName(std::wstring(name)); + value->setName(name); + tags[name] = std::unique_ptr(value); } void putBoolean(const std::wstring& name, bool val) { @@ -111,7 +109,7 @@ public: Tag* get(const std::wstring& name) { auto it = tags.find(name); - if (it != tags.end()) return it->second; + if (it != tags.end()) return it->second.get(); return nullptr; } @@ -120,72 +118,87 @@ public: } uint8_t getByte(const std::wstring& name) { - if (tags.find(name) == tags.end()) return (uint8_t)0; - return ((ByteTag*)tags[name])->data; + auto it = tags.find(name); + if (it == tags.end()) return 0; + return static_cast(it->second.get())->data; } short getShort(const std::wstring& name) { - if (tags.find(name) == tags.end()) return (short)0; - return ((ShortTag*)tags[name])->data; + auto it = tags.find(name); + if (it == tags.end()) return 0; + return static_cast(it->second.get())->data; } int getInt(const std::wstring& name) { - if (tags.find(name) == tags.end()) return (int)0; - return ((IntTag*)tags[name])->data; + auto it = tags.find(name); + if (it == tags.end()) return 0; + return static_cast(it->second.get())->data; } int64_t getLong(const std::wstring& name) { - if (tags.find(name) == tags.end()) return (int64_t)0; - return ((LongTag*)tags[name])->data; + auto it = tags.find(name); + if (it == tags.end()) return 0; + return static_cast(it->second.get())->data; } float getFloat(const std::wstring& name) { - if (tags.find(name) == tags.end()) return (float)0; - return ((FloatTag*)tags[name])->data; + auto it = tags.find(name); + if (it == tags.end()) return 0; + return static_cast(it->second.get())->data; } double getDouble(const std::wstring& name) { - if (tags.find(name) == tags.end()) return (double)0; - return ((DoubleTag*)tags[name])->data; + auto it = tags.find(name); + if (it == tags.end()) return 0; + return static_cast(it->second.get())->data; } std::wstring getString(const std::wstring& name) { - if (tags.find(name) == tags.end()) return std::wstring(L""); - return ((StringTag*)tags[name])->data; + auto it = tags.find(name); + if (it == tags.end()) return std::wstring(L""); + return static_cast(it->second.get())->data; } std::vector getByteArray(const std::wstring& name) { - if (tags.find(name) == tags.end()) return std::vector(); - return ((ByteArrayTag*)tags[name])->data; + auto it = tags.find(name); + if (it == tags.end()) return std::vector(); + return static_cast(it->second.get())->data; } std::vector getIntArray(const std::wstring& name) { - if (tags.find(name) == tags.end()) return std::vector(); - return ((IntArrayTag*)tags[name])->data; + auto it = tags.find(name); + if (it == tags.end()) return std::vector(); + return static_cast(it->second.get())->data; } CompoundTag* getCompound(const std::wstring& name) { - if (tags.find(name) == tags.end()) return new CompoundTag(name); - return (CompoundTag*)tags[name]; + auto it = tags.find(name); + if (it == tags.end()) { + auto [it2, inserted] = tags.emplace(name, std::make_unique(name)); + return static_cast(it2->second.get()); + } + return static_cast(it->second.get()); } ListTag* getList(const std::wstring& name) { - if (tags.find(name) == tags.end()) return new ListTag(name); - return (ListTag*)tags[name]; + auto it = tags.find(name); + if (it == tags.end()) { + auto [it2, inserted] = tags.emplace(name, std::make_unique>(name)); + return static_cast*>(it2->second.get()); + } + return static_cast*>(it->second.get()); } bool getBoolean(const std::wstring& string) { return getByte(string) != 0; } void remove(const std::wstring& name) { - auto it = tags.find(name); - if (it != tags.end()) tags.erase(it); - // tags.remove(name); + tags.erase(name); } std::wstring toString() { static const int bufSize = 32; static wchar_t buf[bufSize]; - swprintf(buf, bufSize, L"%d entries", tags.size()); + swprintf(buf, bufSize, L"%zu entries", tags.size()); return std::wstring(buf); } @@ -211,19 +224,12 @@ public: bool isEmpty() { return tags.empty(); } - virtual ~CompoundTag() { - auto itEnd = tags.end(); - for (auto it = tags.begin(); it != itEnd; it++) { - delete it->second; - } - } + virtual ~CompoundTag() = default; Tag* copy() { CompoundTag* tag = new CompoundTag(getName()); - - auto itEnd = tags.end(); - for (auto it = tags.begin(); it != itEnd; it++) { - tag->put((wchar_t*)it->first.c_str(), it->second->copy()); + for (auto& [key, value] : tags) { + tag->put(key, value->copy()); } return tag; } @@ -233,18 +239,14 @@ public: CompoundTag* o = (CompoundTag*)obj; if (tags.size() == o->tags.size()) { - bool equal = true; - auto itEnd = tags.end(); - for (auto it = tags.begin(); it != itEnd; it++) { - auto itFind = o->tags.find(it->first); + for (auto& [key, value] : tags) { + auto itFind = o->tags.find(key); if (itFind == o->tags.end() || - !it->second->equals(itFind->second)) { - equal = false; - break; + !value->equals(itFind->second.get())) { + return false; } } - return equal; - // return tags.entrySet().equals(o.tags.entrySet()); + return true; } } return false; diff --git a/minecraft/nbt/include/nbt/ListTag.h b/minecraft/nbt/include/nbt/ListTag.h index c6b76d7ae..62a2e193e 100644 --- a/minecraft/nbt/include/nbt/ListTag.h +++ b/minecraft/nbt/include/nbt/ListTag.h @@ -1,12 +1,13 @@ #pragma once #include "Tag.h" +#include #include template class ListTag : public Tag { private: - std::vector list; + std::vector> list; uint8_t type; public: @@ -15,15 +16,14 @@ public: void write(DataOutput* dos) { if (list.size() > 0) - type = (list[0])->getId(); + type = list[0]->getId(); else type = static_cast(1); dos->writeByte(type); dos->writeInt((int)list.size()); - auto itEnd = list.end(); - for (auto it = list.begin(); it != itEnd; it++) (*it)->write(dos); + for (auto& tag : list) tag->write(dos); } void load(DataInput* dis, int tagDepth) { @@ -39,9 +39,9 @@ public: list.clear(); for (int i = 0; i < size; i++) { - Tag* tag = Tag::newTag(type, L""); + std::unique_ptr tag(Tag::newTag(type, L"")); tag->load(dis, tagDepth); - list.push_back(tag); + list.push_back(std::move(tag)); } } @@ -49,7 +49,7 @@ public: std::wstring toString() { static wchar_t buf[64]; - swprintf(buf, 64, L"%d entries of type %ls", list.size(), + swprintf(buf, 64, L"%zu entries of type %ls", list.size(), Tag::getTagName(type)); return std::wstring(buf); } @@ -62,9 +62,8 @@ public: char* newPrefix = new char[strlen(prefix) + 4]; strcpy(newPrefix, prefix); strcat(newPrefix, " "); - auto itEnd = list.end(); - for (auto it = list.begin(); it != itEnd; it++) { - (*it)->print(newPrefix, out); + for (auto& tag : list) { + tag->print(newPrefix, out); } delete[] newPrefix; out << prefix << "}" << std::endl; @@ -78,27 +77,20 @@ public: // other items that also use list tags and require equality checks to // work) considering we can't change the write/load functions. tag->setName(L""); - list.push_back(tag); + list.push_back(std::unique_ptr(tag)); } - T* get(int index) { return (T*)list[index]; } + T* get(int index) { return static_cast(list[index].get()); } int size() { return (int)list.size(); } - virtual ~ListTag() { - auto itEnd = list.end(); - for (auto it = list.begin(); it != itEnd; it++) { - delete *it; - } - } + virtual ~ListTag() = default; virtual Tag* copy() { ListTag* res = new ListTag(getName()); res->type = type; - auto itEnd = list.end(); - for (auto it = list.begin(); it != itEnd; it++) { - T* copy = (T*)(*it)->copy(); - res->list.push_back(copy); + for (auto& tag : list) { + res->list.push_back(std::unique_ptr(tag->copy())); } return res; } @@ -110,15 +102,13 @@ public: bool equal = false; if (list.size() == o->list.size()) { equal = true; - auto itEnd = list.end(); // 4J Stu - Pretty inefficient method, but I think we can // live with it give how often it will happen, and the small // sizes of the data sets - for (auto it = list.begin(); it != itEnd; ++it) { + for (auto& tag : list) { bool thisMatches = false; - for (auto it2 = o->list.begin(); it2 != o->list.end(); - ++it2) { - if ((*it)->equals(*it2)) { + for (auto& otherTag : o->list) { + if (tag->equals(otherTag.get())) { thisMatches = true; break; } @@ -135,4 +125,4 @@ public: } return false; } -}; \ No newline at end of file +};