#include "stdafx.h" #include "CosmeticModels.h" #include "HumanoidModel.h" #include "ModelPart.h" #include "SkinBox.h" #include "../Minecraft.World/Player.h" #include "../Minecraft.World/Inventory.h" #include #include #include namespace CosmeticModels { namespace { struct Entry { HumanoidModel *model = nullptr; std::vector boxes; }; std::map s_models; const struct { const char *name; eBodyPart part; } kParts[] = { { "HEAD", eBodyPart_Head }, { "BODY", eBodyPart_Body }, { "ARM0", eBodyPart_Arm0 }, { "ARM1", eBodyPart_Arm1 }, { "LEG0", eBodyPart_Leg0 }, { "LEG1", eBodyPart_Leg1 }, { "HEADWEAR", eBodyPart_Headwear }, { "JACKET", eBodyPart_Jacket }, { "SLEEVE0", eBodyPart_Sleeve0 }, { "SLEEVE1", eBodyPart_Sleeve1 }, { "PANTS0", eBodyPart_Pants0 }, { "PANTS1", eBodyPart_Pants1 }, { "WAIST", eBodyPart_Waist }, { "LEGGING0", eBodyPart_Legging0 }, { "LEGGING1", eBodyPart_Legging1 }, { "SOCK0", eBodyPart_Sock0 }, { "SOCK1", eBodyPart_Sock1 }, { "BOOT0", eBodyPart_Boot0 }, { "BOOT1", eBodyPart_Boot1 }, { "ARMARMOR0", eBodyPart_ArmArmor0 }, { "ARMARMOR1", eBodyPart_ArmArmor1 }, { "BODYARMOR", eBodyPart_BodyArmor }, { "BELT", eBodyPart_Belt }, }; bool TextureSize(const std::wstring &path, int &width, int &height) { PBYTE data = nullptr; DWORD bytes = 0; app.GetMemFileDetails(path, &data, &bytes); if (data == nullptr || bytes < 24 || data[0] != 0x89 || data[1] != 'P' || data[2] != 'N' || data[3] != 'G') return false; width = (data[16] << 24) | (data[17] << 16) | (data[18] << 8) | data[19]; height = (data[20] << 24) | (data[21] << 16) | (data[22] << 8) | data[23]; return width > 0 && height > 0 && width <= 4096 && height <= 4096; } bool ParseLine(const std::string &line, SKIN_BOX &box) { std::istringstream in(line); std::string name; if (!(in >> name)) return false; box = SKIN_BOX(); box.ePart = eBodyPart_Unknown; for (const auto &entry : kParts) { if (name == entry.name) box.ePart = entry.part; } if (box.ePart == eBodyPart_Unknown) return false; float *values[] = { &box.fX, &box.fY, &box.fZ, &box.fW, &box.fH, &box.fD, &box.fU, &box.fV, &box.fA, &box.fM, &box.fS }; int read = 0; for (float *value : values) { if (!(in >> *value)) break; ++read; } return read >= 8; } Entry *Build(unsigned int index, const std::string &boxes, const std::wstring &texturePath) { int width = 0; int height = 0; if (!TextureSize(texturePath, width, height)) return nullptr; Entry entry; entry.model = new HumanoidModel(0.0f, 0.0f, width, height); std::istringstream lines(boxes); std::string line; while (std::getline(lines, line) && entry.boxes.size() < 64) { SKIN_BOX box; if (!ParseLine(line, box)) continue; ModelPart *part = entry.model->AddOrRetrievePart(&box); if (part != nullptr) { part->visible = true; entry.boxes.push_back(part); } } s_models[index] = entry; return &s_models[index]; } void CopyPose(ModelPart *to, const ModelPart *from) { if (to == nullptr || from == nullptr) return; to->x = from->x; to->y = from->y; to->z = from->z; to->xRot = from->xRot; to->yRot = from->yRot; to->zRot = from->zRot; to->translateX = from->translateX; to->translateY = from->translateY; to->translateZ = from->translateZ; } } void Render(HumanoidModel *pose, Player *player, unsigned int index, const std::string &boxes, const std::wstring &texturePath) { if (pose == nullptr || player == nullptr || boxes.empty()) return; auto it = s_models.find(index); Entry *entry = it != s_models.end() ? &it->second : Build(index, boxes, texturePath); if (entry == nullptr || entry->model == nullptr) return; bool helmet = player->inventory->getArmor(3) != nullptr; bool chestplate = player->inventory->getArmor(2) != nullptr; bool leggings = player->inventory->getArmor(1) != nullptr; bool boots = player->inventory->getArmor(0) != nullptr; for (ModelPart *part : entry->boxes) { unsigned int hide = part->hideWithArmor; part->visible = !((helmet && (hide & 1)) || (chestplate && (hide & 2)) || (leggings && (hide & 4)) || (boots && (hide & 8))); } HumanoidModel *model = entry->model; ModelPart *const targets[] = { model->head, model->hair, model->body, model->jacket, model->arm0, model->sleeve0, model->arm1, model->sleeve1, model->leg0, model->pants0, model->leg1, model->pants1, model->waist, model->belt, model->bodyArmor, model->armArmor0, model->armArmor1, model->legging0, model->legging1, model->sock0, model->sock1, model->boot0, model->boot1, }; ModelPart *const sources[] = { pose->head, pose->hair, pose->body, pose->jacket, pose->arm0, pose->sleeve0, pose->arm1, pose->sleeve1, pose->leg0, pose->pants0, pose->leg1, pose->pants1, pose->waist, pose->belt, pose->bodyArmor, pose->armArmor0, pose->armArmor1, pose->legging0, pose->legging1, pose->sock0, pose->sock1, pose->boot0, pose->boot1, }; const float scale = 1.0f / 16.0f; for (size_t i = 0; i < sizeof(targets) / sizeof(targets[0]); ++i) { ModelPart *target = targets[i]; if (target == nullptr || target->children.empty()) continue; CopyPose(target, sources[i]); bool wasVisible = target->visible; bool wasNever = target->neverRender; target->visible = true; target->neverRender = false; target->render(scale, false, true); target->visible = wasVisible; target->neverRender = wasNever; } } }