MassBuilderSaveTool/src/Mass/Mass_Weapons.cpp

361 lines
14 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 "PropertyNames.h"
#include "../Logger/Logger.h"
#include "../UESaveFile/Types/ArrayProperty.h"
#include "../UESaveFile/Types/BoolProperty.h"
#include "../UESaveFile/Types/ByteProperty.h"
#include "../UESaveFile/Types/ColourStructProperty.h"
#include "../UESaveFile/Types/GenericStructProperty.h"
#include "../UESaveFile/Types/IntProperty.h"
#include "../UESaveFile/Types/StringProperty.h"
#include "Mass.h"
using namespace Containers::Literals;
auto Mass::meleeWeapons() -> Containers::ArrayView<Weapon> {
return _weapons.melee;
}
void Mass::getMeleeWeapons() {
LOG_INFO("Getting melee weapons.");
getWeaponType(MASS_WEAPONS_MELEE, _weapons.melee);
}
auto Mass::writeMeleeWeapons() -> bool {
LOG_INFO("Writing melee weapons.");
return writeWeaponType(MASS_WEAPONS_MELEE, _weapons.melee);
}
auto Mass::shields() -> Containers::ArrayView<Weapon> {
return _weapons.shields;
}
void Mass::getShields() {
LOG_INFO("Getting shields.");
getWeaponType(MASS_WEAPONS_SHIELD, _weapons.shields);
}
auto Mass::writeShields() -> bool {
LOG_INFO("Writing shields.");
return writeWeaponType(MASS_WEAPONS_SHIELD, _weapons.shields);
}
auto Mass::bulletShooters() -> Containers::ArrayView<Weapon> {
return _weapons.bulletShooters;
}
void Mass::getBulletShooters() {
LOG_INFO("Getting bullet shooters.");
getWeaponType(MASS_WEAPONS_BSHOOTER, _weapons.bulletShooters);
}
auto Mass::writeBulletShooters() -> bool {
LOG_INFO("Writing bullet shooters.");
return writeWeaponType(MASS_WEAPONS_BSHOOTER, _weapons.bulletShooters);
}
auto Mass::energyShooters() -> Containers::ArrayView<Weapon> {
return _weapons.energyShooters;
}
void Mass::getEnergyShooters() {
LOG_INFO("Getting energy shooters.");
getWeaponType(MASS_WEAPONS_ESHOOTER, _weapons.energyShooters);
}
auto Mass::writeEnergyShooters() -> bool {
LOG_INFO("Writing energy shooters.");
return writeWeaponType(MASS_WEAPONS_ESHOOTER, _weapons.energyShooters);
}
auto Mass::bulletLaunchers() -> Containers::ArrayView<Weapon> {
return _weapons.bulletLaunchers;
}
void Mass::getBulletLaunchers() {
LOG_INFO("Getting bullet launchers.");
getWeaponType(MASS_WEAPONS_BLAUNCHER, _weapons.bulletLaunchers);
}
auto Mass::writeBulletLaunchers() -> bool {
LOG_INFO("Writing bullet launchers.");
return writeWeaponType(MASS_WEAPONS_BLAUNCHER, _weapons.bulletLaunchers);
}
auto Mass::energyLaunchers() -> Containers::ArrayView<Weapon> {
return _weapons.energyLaunchers;
}
void Mass::getEnergyLaunchers() {
LOG_INFO("Getting energy launchers.");
getWeaponType(MASS_WEAPONS_ELAUNCHER, _weapons.energyLaunchers);
}
auto Mass::writeEnergyLaunchers() -> bool {
LOG_INFO("Writing energy launchers.");
return writeWeaponType(MASS_WEAPONS_ELAUNCHER, _weapons.energyLaunchers);
}
void Mass::getWeaponType(Containers::StringView prop_name, Containers::ArrayView<Weapon> weapon_array) {
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 prop = unit_data->at<ArrayProperty>(prop_name);
if(!prop) {
LOG_ERROR_FORMAT("Couldn't find {} in {}.", prop_name, _filename);
_state = State::Invalid;
return;
}
if(prop->items.size() != weapon_array.size()) {
LOG_ERROR_FORMAT("Weapon arrays are not of the same size. Expected {}, got {} instead.",
weapon_array.size(), prop->items.size());
_state = State::Invalid;
return;
}
for(UnsignedInt i = 0; i < weapon_array.size(); i++) {
auto weapon_prop = prop->at<GenericStructProperty>(i);
auto& weapon = weapon_array[i];
weapon.name = weapon_prop->at<StringProperty>(MASS_WEAPON_NAME)->value;
auto& weapon_type = weapon_prop->at<ByteProperty>(MASS_WEAPON_TYPE)->enumValue;
#define c(enumerator, strenum, name) if(weapon_type == (strenum)) { weapon.type = WeaponType::enumerator; } else
#include "../Maps/WeaponTypes.hpp"
#undef c
{
LOG_ERROR_FORMAT("Invalid weapon type {} in {}.", weapon_type, _filename);
_state = State::Invalid;
return;
}
auto parts_prop = weapon_prop->at<ArrayProperty>(MASS_WEAPON_ELEMENT);
weapon.parts = Containers::Array<WeaponPart>{ValueInit, parts_prop->items.size()};
for(UnsignedInt j = 0; j < parts_prop->items.size(); j++) {
auto part_prop = parts_prop->at<GenericStructProperty>(j);
auto& part = weapon.parts[j];
part.id = part_prop->at<IntProperty>(MASS_WEAPON_PART_ID)->value;
auto part_styles = part_prop->at<ArrayProperty>(MASS_WEAPON_PART_STYLES);
for(UnsignedInt k = 0; k < part_styles->items.size(); k++) {
part.styles[k] = part_styles->at<IntProperty>(k)->value;
}
auto part_decals = part_prop->at<ArrayProperty>(MASS_WEAPON_PART_DECALS);
if(part_decals->items.size() != part.decals.size()) {
part.decals = Containers::Array<Decal>{part_decals->items.size()};
}
getDecals(part.decals, part_decals);
auto part_accs = part_prop->at<ArrayProperty>(MASS_WEAPON_PART_ACCESSORIES);
if(!part_accs) {
part.accessories = Containers::Array<Accessory>{0};
continue;
}
if(part_accs->items.size() != part.accessories.size()) {
part.accessories = Containers::Array<Accessory>{part_accs->items.size()};
}
getAccessories(part.accessories, part_accs);
}
auto custom_styles = weapon_prop->at<ArrayProperty>(MASS_CUSTOM_WEAPON_STYLES);
if(!custom_styles) {
LOG_ERROR_FORMAT("Can't find weapon custom styles in {}", _filename);
_state = State::Invalid;
return;
}
if(custom_styles->items.size() != weapon.customStyles.size()) {
LOG_ERROR_FORMAT("Custom weapon style arrays are not of the same size. Expected {}, got {} instead.",
weapon.customStyles.size(), custom_styles->items.size());
_state = State::Invalid;
return;
}
getCustomStyles(weapon.customStyles, custom_styles);
weapon.attached = weapon_prop->at<BoolProperty>(MASS_WEAPON_ATTACH)->value;
auto& damage_type = weapon_prop->at<ByteProperty>(MASS_WEAPON_DAMAGE_TYPE)->enumValue;
#define c(enumerator, strenum) if(damage_type == (strenum)) { weapon.damageType = DamageType::enumerator; } else
#include "../Maps/DamageTypes.hpp"
#undef c
{
LOG_ERROR_FORMAT("Invalid damage type {} in {}.", damage_type, _filename);
_state = State::Invalid;
return;
}
weapon.dualWield = weapon_prop->at<BoolProperty>(MASS_WEAPON_DUAL_WIELD)->value;
auto& effect_colour_mode = weapon_prop->at<ByteProperty>(MASS_WEAPON_COLOUR_EFX_MODE)->enumValue;
#define c(enumerator, strenum) if(effect_colour_mode == (strenum)) { weapon.effectColourMode = EffectColourMode::enumerator; } else
#include "../Maps/EffectColourModes.hpp"
#undef c
{
LOG_ERROR_FORMAT("Invalid effect colour mode {} in {}.", effect_colour_mode, _filename);
_state = State::Invalid;
return;
}
auto effect_colour = weapon_prop->at<ColourStructProperty>(MASS_WEAPON_COLOUR_EFX);
weapon.effectColour = Color4{effect_colour->r, effect_colour->g, effect_colour->b, effect_colour->a};
}
}
auto Mass::writeWeaponType(Containers::StringView prop_name, Containers::ArrayView<Weapon> weapon_array) -> bool {
auto unit_data = _mass->at<GenericStructProperty>(MASS_UNIT_DATA);
if(!unit_data) {
_lastError = "No unit data in "_s + _filename;
LOG_ERROR(_lastError);
_state = State::Invalid;
return false;
}
auto prop = unit_data->at<ArrayProperty>(prop_name);
if(!prop) {
_lastError = prop_name + " not found in "_s + _filename;
LOG_ERROR(_lastError);
_state = State::Invalid;
return false;
}
if(prop->items.size() != weapon_array.size()) {
_lastError = Utility::format("Weapon arrays are not of the same size. Expected {}, got {} instead.",
weapon_array.size(), prop->items.size());
LOG_ERROR(_lastError);
_state = State::Invalid;
return false;
}
for(UnsignedInt i = 0; i < weapon_array.size(); i++) {
auto weapon_prop = prop->at<GenericStructProperty>(i);
auto& weapon = weapon_array[i];
weapon_prop->at<StringProperty>(MASS_WEAPON_NAME)->value = weapon.name;
switch(weapon.type) {
#define c(enumerator, strenum, name) case WeaponType::enumerator: weapon_prop->at<ByteProperty>(MASS_WEAPON_TYPE)->enumValue = strenum; break;
#include "../Maps/WeaponTypes.hpp"
#undef c
default:
_lastError = Utility::format("Invalid weapon type at index {}.", i);
LOG_ERROR(_lastError);
return false;
}
auto parts_prop = weapon_prop->at<ArrayProperty>(MASS_WEAPON_ELEMENT);
if(parts_prop->items.size() != weapon.parts.size()) {
_lastError = Utility::format("Weapon part arrays are not of the same size. Expected {}, got {} instead.",
weapon.parts.size(), parts_prop->items.size());
LOG_ERROR(_lastError);
_state = State::Invalid;
return false;
}
for(UnsignedInt j = 0; j < parts_prop->items.size(); j++) {
auto part_prop = parts_prop->at<GenericStructProperty>(j);
auto& part = weapon.parts[j];
part_prop->at<IntProperty>(MASS_WEAPON_PART_ID)->value = part.id;
auto part_styles = part_prop->at<ArrayProperty>(MASS_WEAPON_PART_STYLES);
for(UnsignedInt k = 0; k < part_styles->items.size(); k++) {
part_styles->at<IntProperty>(k)->value = part.styles[k];
}
auto part_decals = part_prop->at<ArrayProperty>(MASS_WEAPON_PART_DECALS);
writeDecals(part.decals, part_decals);
auto part_accs = part_prop->at<ArrayProperty>(MASS_WEAPON_PART_ACCESSORIES);
if(!part_accs) {
continue;
}
if(part_accs->items.size() != part.accessories.size()) {
_lastError = Utility::format("Part accessory arrays are not of the same size. Expected {}, got {} instead.",
part.accessories.size(), part_accs->items.size());
LOG_ERROR(_lastError);
_state = State::Invalid;
return false;
}
writeAccessories(part.accessories, part_accs);
}
auto custom_styles = weapon_prop->at<ArrayProperty>(MASS_CUSTOM_WEAPON_STYLES);
if(!custom_styles) {
_lastError = "No custom styles found for weapon."_s;
LOG_ERROR(_lastError);
_state = State::Invalid;
return false;
}
if(custom_styles->items.size() != weapon.customStyles.size()) {
_lastError = Utility::format("Custom style arrays are not of the same size. Expected {}, got {} instead.",
weapon.customStyles.size(), custom_styles->items.size());
LOG_ERROR(_lastError);
_state = State::Invalid;
return false;
}
for(UnsignedInt j = 0; j < weapon.customStyles.size(); j++) {
writeCustomStyle(weapon.customStyles[j], j, custom_styles);
}
weapon_prop->at<BoolProperty>(MASS_WEAPON_ATTACH)->value = weapon.attached;
switch(weapon.damageType) {
#define c(enumerator, strenum) case DamageType::enumerator: weapon_prop->at<ByteProperty>(MASS_WEAPON_DAMAGE_TYPE)->enumValue = strenum; break;
#include "../Maps/DamageTypes.hpp"
#undef c
default:
_lastError = Utility::format("Invalid damage type at index {}.", i);
LOG_ERROR(_lastError);
return false;
}
weapon_prop->at<BoolProperty>(MASS_WEAPON_DUAL_WIELD)->value = weapon.dualWield;
switch(weapon.effectColourMode) {
#define c(enumerator, enumstr) case EffectColourMode::enumerator: \
weapon_prop->at<ByteProperty>(MASS_WEAPON_COLOUR_EFX_MODE)->enumValue = enumstr; \
break;
#include "../Maps/EffectColourModes.hpp"
#undef c
default:
_lastError = Utility::format("Invalid damage type at index {}.", i);
LOG_ERROR(_lastError);
return false;
}
auto effect_colour = weapon_prop->at<ColourStructProperty>(MASS_WEAPON_COLOUR_EFX);
effect_colour->r = weapon.effectColour.r();
effect_colour->g = weapon.effectColour.g();
effect_colour->b = weapon.effectColour.b();
effect_colour->a = weapon.effectColour.a();
}
if(!_mass->saveToFile()) {
_lastError = _mass->lastError();
return false;
}
return true;
}