MassBuilderSaveTool/src/Mass/Mass_Armour.cpp

434 lines
17 KiB
C++

// MassBuilderSaveTool
// Copyright (C) 2021-2022 Guillaume Jacquemin
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <algorithm>
#include "PropertyNames.h"
#include "../Logger/Logger.h"
#include "../UESaveFile/Types/ArrayProperty.h"
#include "../UESaveFile/Types/ByteProperty.h"
#include "../UESaveFile/Types/GenericStructProperty.h"
#include "../UESaveFile/Types/IntProperty.h"
#include "../UESaveFile/Types/StringProperty.h"
#include "../UESaveFile/Types/VectorStructProperty.h"
#include "Mass.h"
using namespace Containers::Literals;
auto Mass::armourParts() -> Containers::ArrayView<ArmourPart> {
return _armour.parts;
}
void Mass::getArmourParts() {
LOG_INFO("Getting armour parts.");
auto unit_data = _mass->at<GenericStructProperty>(MASS_UNIT_DATA);
if(!unit_data) {
LOG_ERROR_FORMAT("Couldn't find {} in {}.", MASS_UNIT_DATA, _filename);
_state = State::Invalid;
return;
}
auto armour_array = unit_data->at<ArrayProperty>(MASS_ARMOUR_PARTS);
if(!armour_array) {
LOG_ERROR_FORMAT("Couldn't find {} in {}.", MASS_ARMOUR_PARTS, _filename);
_state = State::Invalid;
return;
}
if(armour_array->items.size() != _armour.parts.size()) {
LOG_ERROR_FORMAT("Armour part arrays are not of the same size. Expected {}, got {} instead.",
_armour.parts.size(), armour_array->items.size());
_state = State::Invalid;
return;
}
for(UnsignedInt i = 0; i < armour_array->items.size(); i++) {
auto part_prop = armour_array->at<GenericStructProperty>(i);
auto& part = _armour.parts[i];
auto& armour_slot = part_prop->at<ByteProperty>(MASS_ARMOUR_SLOT)->enumValue;
#define c(enumerator, strenum, name) if(armour_slot == (strenum)) { part.slot = ArmourSlot::enumerator; } else
#include "../Maps/ArmourSlots.hpp"
#undef c
{
LOG_ERROR_FORMAT("Invalid armour slot enumerator {}.", armour_slot);
_state = State::Invalid;
return;
}
part.id = part_prop->at<IntProperty>(MASS_ARMOUR_ID)->value;
auto part_styles = part_prop->at<ArrayProperty>(MASS_ARMOUR_STYLES);
if(!part_styles) {
LOG_ERROR_FORMAT("Part styles not found for part number {}.", i);
_state = State::Invalid;
return;
}
if(part_styles->items.size() != part.styles.size()) {
LOG_ERROR_FORMAT("Armour part style arrays are not of the same size. Expected {}, got {} instead.",
part.styles.size(), part_styles->items.size());
_state = State::Invalid;
return;
}
for(UnsignedInt j = 0; j < part_styles->items.size(); j++) {
part.styles[j] = part_styles->at<IntProperty>(j)->value;
}
auto decals_array = part_prop->at<ArrayProperty>(MASS_ARMOUR_DECALS);
if(!decals_array) {
LOG_ERROR_FORMAT("Part decals not found for part number {}.", i);
_state = State::Invalid;
return;
}
part.decals = Containers::Array<Decal>{decals_array->items.size()};
getDecals(part.decals, decals_array);
auto accs_array = part_prop->at<ArrayProperty>(MASS_ARMOUR_ACCESSORIES);
if(!accs_array) {
LOG_WARNING_FORMAT("Part accessories not found for part number {}.", i);
part.accessories = Containers::Array<Accessory>{};
continue;
}
if(part.accessories.size() != accs_array->items.size()) {
part.accessories = Containers::Array<Accessory>{accs_array->items.size()};
}
getAccessories(part.accessories, accs_array);
}
}
auto Mass::writeArmourPart(ArmourSlot slot) -> bool {
LOG_INFO_FORMAT("Writing armour part in slot {}.", static_cast<int>(slot));
auto& part = *std::find_if(_armour.parts.begin(), _armour.parts.end(), [&slot](const ArmourPart& part){ return slot == part.slot; });
auto unit_data = _mass->at<GenericStructProperty>(MASS_UNIT_DATA);
if(!unit_data) {
_lastError = "Couldn't find the unit data in " + _filename + ".";
LOG_ERROR(_lastError);
_state = State::Invalid;
return false;
}
auto armour_array = unit_data->at<ArrayProperty>(MASS_ARMOUR_PARTS);
if(!armour_array) {
_lastError = "Couldn't find the armour part array in " + _filename + ".";
LOG_ERROR(_lastError);
_state = State::Invalid;
return false;
}
Containers::StringView slot_str = nullptr;
switch(slot) {
#define c(enumerator, strenum, name) case ArmourSlot::enumerator: \
slot_str = strenum; \
break;
#include "../Maps/ArmourSlots.hpp"
#undef c
}
GenericStructProperty* part_prop = nullptr;
for(UnsignedInt i = 0; i < armour_array->items.size(); i++) {
part_prop = armour_array->at<GenericStructProperty>(i);
if(slot_str == part_prop->at<ByteProperty>(MASS_ARMOUR_SLOT)->enumValue) {
break;
}
else {
part_prop = nullptr;
}
}
if(!part_prop) {
auto prefix = "Couldn't find the armour part for slot "_s;
switch(slot) {
#define c(enumerator, strenum, name) case ArmourSlot::enumerator: \
_lastError = prefix + "ArmourSlot::" #enumerator "."_s; \
break;
#include "../Maps/ArmourSlots.hpp"
#undef c
}
LOG_ERROR(_lastError);
return false;
}
part_prop->at<IntProperty>(MASS_ARMOUR_ID)->value = part.id;
auto part_styles = part_prop->at<ArrayProperty>(MASS_ARMOUR_STYLES);
for(UnsignedInt i = 0; i < part.styles.size(); i++) {
part_styles->at<IntProperty>(i)->value = part.styles[i];
}
auto decals_array = part_prop->at<ArrayProperty>(MASS_ARMOUR_DECALS);
writeDecals(part.decals, decals_array);
if(part.accessories.size() != 0) {
auto accs_array = part_prop->at<ArrayProperty>(MASS_ARMOUR_ACCESSORIES);
writeAccessories(part.accessories, accs_array);
}
if(!_mass->saveToFile()) {
_lastError = _mass->lastError();
return false;
}
return true;
}
auto Mass::bulletLauncherAttachmentStyle() -> BulletLauncherAttachmentStyle& {
return _armour.blAttachmentStyle;
}
auto Mass::bulletLauncherAttachments() -> Containers::ArrayView<BulletLauncherAttachment> {
return _armour.blAttachment;
}
void Mass::getBulletLauncherAttachments() {
LOG_INFO("Getting the bullet launcher attachment data.");
auto unit_data = _mass->at<GenericStructProperty>(MASS_UNIT_DATA);
if(!unit_data) {
LOG_ERROR_FORMAT("Couldn't find {} in {}.", MASS_UNIT_DATA, _filename);
_state = State::Invalid;
return;
}
auto attach_style_prop = unit_data->at<ByteProperty>(MASS_BL_ATTACHMENT_STYLE);
auto attach_array = unit_data->at<ArrayProperty>(MASS_BL_ATTACHMENTS);
if(!attach_style_prop && !attach_array) {
LOG_WARNING_FORMAT("No bullet launcher attachment data found in {}.", _filename);
_armour.blAttachmentStyle = BulletLauncherAttachmentStyle::NotFound;
return;
}
if(attach_style_prop && !attach_array) {
LOG_WARNING_FORMAT("No bullet launcher attachments found in {}.", _filename);
_armour.blAttachmentStyle = BulletLauncherAttachmentStyle::NotFound;
_state = State::Invalid;
return;
}
if(attach_array->items.size() == _weapons.bulletLaunchers.size() &&
attach_array->items.size() == _armour.blAttachment.size())
{
for(UnsignedInt i = 0; i < attach_array->items.size(); i++) {
auto attachment_prop = attach_array->at<GenericStructProperty>(i);
auto& attachment = _armour.blAttachment[i];
Containers::StringView socket = attachment_prop->at<StringProperty>(MASS_BL_ATTACHMENT_SOCKET)->value;
#define c(enumerator, strenum, name) if(socket == (strenum)) { attachment.socket = BulletLauncherSocket::enumerator; } else
#include "../Maps/BulletLauncherSockets.hpp"
#undef c
{
LOG_ERROR_FORMAT("Invalid attachment socket {}.", socket);
_state = State::Invalid;
return;
}
auto rel_loc_prop = attachment_prop->at<VectorStructProperty>(MASS_BL_ATTACHMENT_RELLOC);
attachment.relativeLocation = Vector3{rel_loc_prop->x, rel_loc_prop->y, rel_loc_prop->z};
auto off_loc_prop = attachment_prop->at<VectorStructProperty>(MASS_BL_ATTACHMENT_OFFLOC);
attachment.offsetLocation = Vector3{off_loc_prop->x, off_loc_prop->y, off_loc_prop->z};
auto rel_rot_prop = attachment_prop->at<VectorStructProperty>(MASS_BL_ATTACHMENT_RELROT);
attachment.relativeRotation = Vector3{rel_rot_prop->x, rel_rot_prop->y, rel_rot_prop->z};
auto off_rot_prop = attachment_prop->at<VectorStructProperty>(MASS_BL_ATTACHMENT_OFFROT);
attachment.offsetRotation = Vector3{off_rot_prop->x, off_rot_prop->y, off_rot_prop->z};
auto rel_scale_prop = attachment_prop->at<VectorStructProperty>(MASS_BL_ATTACHMENT_RELSCALE);
attachment.relativeScale = Vector3{rel_scale_prop->x, rel_scale_prop->y, rel_scale_prop->z};
}
}
if(attach_style_prop) {
Containers::StringView attach_style = attach_style_prop->enumValue;
#define c(enumerator, strenum) if(attach_style == (strenum)) { _armour.blAttachmentStyle = BulletLauncherAttachmentStyle::enumerator; } else
#include "../Maps/BulletLauncherAttachmentStyles.hpp"
#undef c
{
LOG_ERROR_FORMAT("Invalid attachment style {}.", attach_style);
_state = State::Invalid;
}
}
else {
_armour.blAttachmentStyle = BulletLauncherAttachmentStyle::ActiveOne;
}
}
auto Mass::writeBulletLauncherAttachments() -> bool {
LOG_INFO("Writing bullet launcher attachments.");
auto unit_data = _mass->at<GenericStructProperty>(MASS_UNIT_DATA);
if(!unit_data) {
_lastError = "No unit data in " + _filename;
LOG_ERROR(_lastError);
_state = State::Invalid;
return false;
}
auto attach_style_prop = unit_data->at<ByteProperty>(MASS_BL_ATTACHMENT_STYLE);
auto attach_array = unit_data->at<ArrayProperty>(MASS_BL_ATTACHMENTS);
if(!attach_style_prop && !attach_array) {
_lastError = "No attachment properties to write to in " + _filename;
LOG_ERROR(_lastError);
_armour.blAttachmentStyle = BulletLauncherAttachmentStyle::NotFound;
return false;
}
if(attach_style_prop && !attach_array) {
_lastError = "Couldn't find the attachments in " + _filename;
LOG_ERROR(_lastError);
_armour.blAttachmentStyle = BulletLauncherAttachmentStyle::NotFound;
_state = State::Invalid;
return false;
}
if(attach_array->items.size() == _weapons.bulletLaunchers.size() &&
attach_array->items.size() == _armour.blAttachment.size())
{
for(UnsignedInt i = 0; i < attach_array->items.size(); i++) {
auto attachment_prop = attach_array->at<GenericStructProperty>(i);
auto& attachment = _armour.blAttachment[i];
auto& socket = attachment_prop->at<StringProperty>(MASS_BL_ATTACHMENT_SOCKET)->value;
switch(attachment.socket) {
#define c(enumerator, strenum, name) case BulletLauncherSocket::enumerator: socket = strenum; break;
#include "../Maps/BulletLauncherSockets.hpp"
#undef c
default:
_lastError = "Invalid socket type."_s;
LOG_ERROR(_lastError);
return false;
}
auto rel_loc_prop = attachment_prop->at<VectorStructProperty>(MASS_BL_ATTACHMENT_RELLOC);
rel_loc_prop->x = attachment.relativeLocation.x();
rel_loc_prop->y = attachment.relativeLocation.y();
rel_loc_prop->z = attachment.relativeLocation.z();
auto off_loc_prop = attachment_prop->at<VectorStructProperty>(MASS_BL_ATTACHMENT_OFFLOC);
off_loc_prop->x = attachment.offsetLocation.x();
off_loc_prop->y = attachment.offsetLocation.y();
off_loc_prop->z = attachment.offsetLocation.z();
auto rel_rot_prop = attachment_prop->at<VectorStructProperty>(MASS_BL_ATTACHMENT_RELROT);
rel_rot_prop->x = attachment.relativeRotation.x();
rel_rot_prop->y = attachment.relativeRotation.y();
rel_rot_prop->z = attachment.relativeRotation.z();
auto off_rot_prop = attachment_prop->at<VectorStructProperty>(MASS_BL_ATTACHMENT_OFFROT);
off_rot_prop->x = attachment.offsetRotation.x();
off_rot_prop->y = attachment.offsetRotation.y();
off_rot_prop->z = attachment.offsetRotation.z();
auto rel_scale_prop = attachment_prop->at<VectorStructProperty>(MASS_BL_ATTACHMENT_RELSCALE);
rel_scale_prop->x = attachment.relativeScale.x();
rel_scale_prop->y = attachment.relativeScale.y();
rel_scale_prop->z = attachment.relativeScale.z();
}
}
if(!attach_style_prop) {
attach_style_prop = new ByteProperty;
attach_style_prop->name.emplace(MASS_BL_ATTACHMENT_STYLE);
attach_style_prop->enumType = "enuBLAttachmentStyle"_s;
ByteProperty::ptr prop{attach_style_prop};
arrayAppend(unit_data->properties, std::move(prop));
}
auto& attach_style = attach_style_prop->enumValue;
switch(_armour.blAttachmentStyle) {
#define c(enumerator, strenum) case BulletLauncherAttachmentStyle::enumerator: \
attach_style = strenum; \
break;
#include "../Maps/BulletLauncherAttachmentStyles.hpp"
#undef c
default:
_lastError = "Unknown BL attachment style.";
LOG_ERROR(_lastError);
return false;
}
if(!_mass->saveToFile()) {
_lastError = _mass->lastError();
return false;
}
return true;
}
auto Mass::armourCustomStyles() -> Containers::ArrayView<CustomStyle> {
return _armour.customStyles;
}
void Mass::getArmourCustomStyles() {
LOG_INFO("Getting the custom armour styles.");
auto unit_data = _mass->at<GenericStructProperty>(MASS_UNIT_DATA);
if(!unit_data) {
LOG_ERROR_FORMAT("Couldn't find {} in {}.", MASS_UNIT_DATA, _filename);
_state = State::Invalid;
return;
}
auto armour_styles = unit_data->at<ArrayProperty>(MASS_CUSTOM_ARMOUR_STYLES);
if(!armour_styles) {
LOG_ERROR_FORMAT("Couldn't find {} in {}.", MASS_CUSTOM_ARMOUR_STYLES, _filename);
_state = State::Invalid;
return;
}
if(armour_styles->items.size() != _armour.customStyles.size()) {
LOG_ERROR_FORMAT("Custom armour style arrays are not of the same size. Expected {}, got {} instead.",
_armour.customStyles.size(), armour_styles->items.size());
_state = State::Invalid;
return;
}
getCustomStyles(_armour.customStyles, armour_styles);
}
auto Mass::writeArmourCustomStyle(UnsignedLong index) -> bool {
LOG_INFO_FORMAT("Writing custom armour style {}.", index);
if(index > _armour.customStyles.size()) {
_lastError = "Style index out of range."_s;
LOG_ERROR(_lastError);
return false;
}
auto unit_data = _mass->at<GenericStructProperty>(MASS_UNIT_DATA);
if(!unit_data) {
_lastError = "Couldn't find unit data in "_s + _filename;
LOG_ERROR(_lastError);
_state = State::Invalid;
return false;
}
auto armour_styles = unit_data->at<ArrayProperty>(MASS_CUSTOM_ARMOUR_STYLES);
if(!armour_styles) {
_lastError = "Couldn't find armour custom styles in "_s + _filename;
LOG_ERROR(_lastError);
_state = State::Invalid;
return false;
}
return writeCustomStyle(_armour.customStyles[index], index, armour_styles);
}