MassBuilderSaveTool/src/Mass/Mass.cpp

337 lines
7.8 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 <Corrade/Containers/Array.h>
#include <Corrade/Containers/Pair.h>
#include <Corrade/Utility/Path.h>
#include "../UESaveFile/Types/ArrayProperty.h"
#include "../UESaveFile/Types/BoolProperty.h"
#include "../UESaveFile/Types/ByteProperty.h"
#include "../UESaveFile/Types/ColourStructProperty.h"
#include "../UESaveFile/Types/FloatProperty.h"
#include "../UESaveFile/Types/GenericStructProperty.h"
#include "../UESaveFile/Types/IntProperty.h"
#include "../UESaveFile/Types/RotatorStructProperty.h"
#include "../UESaveFile/Types/StringProperty.h"
#include "../UESaveFile/Types/VectorStructProperty.h"
#include "../UESaveFile/Types/Vector2DStructProperty.h"
#include "Mass.h"
using namespace Containers::Literals;
Mass::Mass(Containers::StringView path) {
auto split = Utility::Path::split(path);
_folder = split.first();
_filename = split.second();
refreshValues();
}
auto Mass::lastError() -> Containers::StringView {
return _lastError;
}
auto Mass::getNameFromFile(Containers::StringView path) -> Containers::Optional<Containers::String> {
if(!Utility::Path::exists(path)) {
Utility::Error{} << path << "couldn't be found."_s;
return Containers::NullOpt;
}
UESaveFile mass{path};
if(!mass.valid()) {
Utility::Error{} << "The unit file seems to be corrupt."_s;
return Containers::NullOpt;
}
auto unit_data = mass.at<GenericStructProperty>("UnitData"_s);
if(!unit_data) {
Utility::Error{} << "Couldn't find unit data in the file."_s;
return Containers::NullOpt;
}
auto name_prop = unit_data->at<StringProperty>("Name_45_A037C5D54E53456407BDF091344529BB"_s);
if(!name_prop) {
Utility::Error{} << "Couldn't find the name in the file."_s;
return Containers::NullOpt;
}
return {name_prop->value};
}
void Mass::refreshValues() {
if(!Utility::Path::exists(Utility::Path::join(_folder, _filename))) {
_state = State::Empty;
return;
}
if(!_mass) {
_mass.emplace(Utility::Path::join(_folder, _filename));
if(!_mass->valid()) {
_state = State::Invalid;
return;
}
}
else {
if(!_mass->reloadData()) {
_state = State::Invalid;
return;
}
}
auto unit_data = _mass->at<GenericStructProperty>("UnitData");
if(!unit_data) {
_state = State::Invalid;
return;
}
auto name_prop = unit_data->at<StringProperty>("Name_45_A037C5D54E53456407BDF091344529BB");
if(!name_prop) {
_name = Containers::NullOpt;
_state = State::Invalid;
return;
}
_name = {name_prop->value};
getJointSliders();
if(_state == State::Invalid) {
return;
}
getFrameStyles();
if(_state == State::Invalid) {
return;
}
getEyeFlareColour();
if(_state == State::Invalid) {
return;
}
getFrameCustomStyles();
if(_state == State::Invalid) {
return;
}
getArmourParts();
if(_state == State::Invalid) {
return;
}
getArmourCustomStyles();
if(_state == State::Invalid) {
return;
}
getMeleeWeapons();
if(_state == State::Invalid) {
return;
}
getShields();
if(_state == State::Invalid) {
return;
}
getBulletShooters();
if(_state == State::Invalid) {
return;
}
getEnergyShooters();
if(_state == State::Invalid) {
return;
}
getBulletLaunchers();
if(_state == State::Invalid) {
return;
}
getEnergyLaunchers();
if(_state == State::Invalid) {
return;
}
getGlobalStyles();
if(_state == State::Invalid) {
return;
}
getTuning();
if(_state == State::Invalid) {
return;
}
auto account_prop = _mass->at<StringProperty>("Account");
if(!account_prop) {
_state = State::Invalid;
return;
}
_account = account_prop->value;
_state = State::Valid;
}
auto Mass::filename() -> Containers::StringView {
return _filename;
}
auto Mass::name() -> Containers::StringView {
return *_name;
}
auto Mass::setName(Containers::StringView new_name) -> bool {
_name = {new_name};
auto unit_data = _mass->at<GenericStructProperty>("UnitData"_s);
if(!unit_data) {
_state = State::Invalid;
return false;
}
auto name_prop = unit_data->at<StringProperty>("Name_45_A037C5D54E53456407BDF091344529BB"_s);
if(!name_prop) {
_state = State::Invalid;
return false;
}
name_prop->value = new_name;
if(!_mass->saveToFile()) {
_lastError = _mass->lastError();
return false;
}
return true;
}
auto Mass::state() -> State {
return _state;
}
auto Mass::dirty() const -> bool {
return _dirty;
}
void Mass::setDirty(bool dirty) {
_dirty = dirty;
}
void Mass::getTuning() {
getTuningCategory("Engine"_s, _tuning.engineId, "Gears"_s, _tuning.gearIds);
if(_state == State::Invalid) {
return;
}
getTuningCategory("OS"_s, _tuning.osId, "Modules"_s, _tuning.moduleIds);
if(_state == State::Invalid) {
return;
}
getTuningCategory("Architect"_s, _tuning.archId, "Techs"_s, _tuning.techIds);
if(_state == State::Invalid) {
return;
}
}
auto Mass::engine() -> Int& {
return _tuning.engineId;
}
auto Mass::gears() -> Containers::ArrayView<Int> {
return _tuning.gearIds;
}
auto Mass::os() -> Int& {
return _tuning.osId;
}
auto Mass::modules() -> Containers::ArrayView<Int> {
return _tuning.moduleIds;
}
auto Mass::architecture() -> Int& {
return _tuning.archId;
}
auto Mass::techs() -> Containers::ArrayView<Int> {
return _tuning.techIds;
}
auto Mass::account() -> Containers::StringView {
return _account;
}
auto Mass::updateAccount(Containers::StringView new_account) -> bool {
_account = new_account;
auto account = _mass->at<StringProperty>("Account"_s);
if(!account) {
_state = State::Invalid;
_lastError = "Couldn't find the account property."_s;
return false;
}
account->value = new_account;
if(!_mass->saveToFile()) {
_lastError = _mass->lastError();
return false;
}
return true;
}
void Mass::getTuningCategory(Containers::StringView big_node_prop_name, Int& big_node_id,
Containers::StringView small_nodes_prop_name, Containers::ArrayView<Int> small_nodes_ids)
{
auto node_id = _mass->at<IntProperty>(big_node_prop_name);
if(!node_id) {
_state = State::Invalid;
return;
}
big_node_id = node_id->value;
auto node_ids = _mass->at<ArrayProperty>(small_nodes_prop_name);
if(!node_ids) {
_state = State::Invalid;
return;
}
if(node_ids->items.size() != small_nodes_ids.size()) {
_state = State::Invalid;
return;
}
for(UnsignedInt i = 0; i < small_nodes_ids.size(); i++) {
auto small_node_id = node_ids->at<IntProperty>(i);
CORRADE_INTERNAL_ASSERT(small_node_id);
small_nodes_ids[i] = small_node_id->value;
}
}