2021-06-11 19:24:52 +02:00
|
|
|
// MassBuilderSaveTool
|
|
|
|
// Copyright (C) 2021 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 <cstring>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <Corrade/Containers/Array.h>
|
|
|
|
#include <Corrade/Utility/Directory.h>
|
|
|
|
|
2021-08-19 20:33:59 +02:00
|
|
|
#include "Locators.h"
|
|
|
|
|
2021-06-11 19:24:52 +02:00
|
|
|
#include "Mass.h"
|
|
|
|
|
|
|
|
std::string Mass::_lastError;
|
|
|
|
|
2021-08-19 20:33:59 +02:00
|
|
|
Mass::Mass(const std::string& path) {
|
|
|
|
_folder = Utility::Directory::path(path);
|
|
|
|
_filename = Utility::Directory::filename(path);
|
2021-06-11 19:24:52 +02:00
|
|
|
|
2021-08-28 20:20:09 +02:00
|
|
|
refreshValues();
|
2021-06-11 19:24:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto Mass::lastError() -> std::string const& {
|
|
|
|
return _lastError;
|
|
|
|
}
|
|
|
|
|
2021-08-19 20:33:59 +02:00
|
|
|
auto Mass::getNameFromFile(const std::string& path) -> std::string {
|
|
|
|
if(!Utility::Directory::exists(path)) {
|
|
|
|
_lastError = path + " couldn't be found.";
|
2021-06-11 19:24:52 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2021-08-19 20:33:59 +02:00
|
|
|
std::string name;
|
2021-06-11 19:24:52 +02:00
|
|
|
|
2021-08-19 20:33:59 +02:00
|
|
|
auto mmap = Utility::Directory::mapRead(path);
|
2021-06-11 19:24:52 +02:00
|
|
|
|
|
|
|
auto iter = std::search(mmap.begin(), mmap.end(), &mass_name_locator[0], &mass_name_locator[56]);
|
|
|
|
|
|
|
|
if(iter != mmap.end()) {
|
|
|
|
name = std::string{iter + 70};
|
|
|
|
}
|
|
|
|
else {
|
2021-08-19 20:33:59 +02:00
|
|
|
_lastError = "The name couldn't be found in " + path;
|
2021-06-11 19:24:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2021-08-28 20:20:09 +02:00
|
|
|
void Mass::refreshValues() {
|
|
|
|
getName();
|
2021-08-28 20:21:13 +02:00
|
|
|
|
|
|
|
if(_state != State::Valid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
getFrameStyles();
|
2021-08-28 20:20:09 +02:00
|
|
|
}
|
|
|
|
|
2021-06-11 19:24:52 +02:00
|
|
|
auto Mass::filename() -> std::string const&{
|
|
|
|
return _filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Mass::name() -> std::string const&{
|
|
|
|
return _name;
|
|
|
|
}
|
|
|
|
|
2021-08-28 20:20:09 +02:00
|
|
|
auto Mass::state() -> State {
|
2021-06-11 19:24:52 +02:00
|
|
|
return _state;
|
|
|
|
}
|
|
|
|
|
2021-08-28 20:21:13 +02:00
|
|
|
auto Mass::frameStyles() -> Containers::StaticArrayView<4, Int> {
|
|
|
|
return _frameStyles;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Mass::setFrameStyle(Int index, Int style_id) -> bool {
|
|
|
|
if(index < 0 || index > 3) {
|
|
|
|
_lastError = "Index is out of range in Mass::setFrameStyle().";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string path = Utility::Directory::join(_folder, _filename);
|
|
|
|
|
|
|
|
if(!Utility::Directory::exists(path)) {
|
|
|
|
_lastError = path + " couldn't be found.";
|
|
|
|
_state = State::Empty;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto mmap = Utility::Directory::map(path);
|
|
|
|
|
|
|
|
auto iter = std::search(mmap.begin(), mmap.end(), &frame_styles_locator[0], &frame_styles_locator[90]);
|
|
|
|
|
|
|
|
if(iter != mmap.end()) {
|
|
|
|
iter += 0x5A;
|
|
|
|
*(reinterpret_cast<Int*>(iter) + index) = style_id;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_lastError = "Frame styles couldn't be found in " + path;
|
|
|
|
_state = State::Invalid;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-11 19:24:52 +02:00
|
|
|
auto Mass::updateSteamId(const std::string& steam_id) -> bool {
|
2021-08-19 20:33:59 +02:00
|
|
|
std::string path = Utility::Directory::join(_folder, _filename);
|
|
|
|
|
|
|
|
if(!Utility::Directory::exists(path)) {
|
|
|
|
_lastError = path + " couldn't be found.";
|
2021-08-28 20:20:09 +02:00
|
|
|
_state = State::Empty;
|
2021-06-11 19:24:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-19 20:33:59 +02:00
|
|
|
Utility::Directory::copy(path, path + ".tmp");
|
2021-06-11 19:24:52 +02:00
|
|
|
|
|
|
|
{
|
2021-08-19 20:33:59 +02:00
|
|
|
auto mmap = Utility::Directory::map(path + ".tmp");
|
2021-06-11 19:24:52 +02:00
|
|
|
|
|
|
|
auto iter = std::search(mmap.begin(), mmap.end(), &steamid_locator[0], &steamid_locator[23]);
|
|
|
|
|
|
|
|
if(iter == mmap.end()) {
|
2021-08-19 20:33:59 +02:00
|
|
|
_lastError = "The M.A.S.S. file at " + path + " seems to be corrupt.";
|
|
|
|
Utility::Directory::rm(path + ".tmp");
|
2021-06-11 19:24:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
iter += 37;
|
|
|
|
|
|
|
|
if(std::strncmp(iter, steam_id.c_str(), steam_id.length()) != 0) {
|
|
|
|
for(int i = 0; i < 17; ++i) {
|
|
|
|
*(iter + i) = steam_id[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 20:33:59 +02:00
|
|
|
if(Utility::Directory::exists(path)) {
|
|
|
|
Utility::Directory::rm(path);
|
2021-06-11 19:24:52 +02:00
|
|
|
}
|
|
|
|
|
2021-08-19 20:33:59 +02:00
|
|
|
Utility::Directory::move(path + ".tmp", path);
|
2021-06-11 19:24:52 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-08-28 20:20:09 +02:00
|
|
|
|
|
|
|
void Mass::getName() {
|
|
|
|
std::string path = Utility::Directory::join(_folder, _filename);
|
|
|
|
|
|
|
|
if(!Utility::Directory::exists(path)) {
|
|
|
|
_lastError = path + " couldn't be found.";
|
|
|
|
_state = State::Empty;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto mmap = Utility::Directory::mapRead(path);
|
|
|
|
|
|
|
|
auto iter = std::search(mmap.begin(), mmap.end(), &mass_name_locator[0], &mass_name_locator[56]);
|
|
|
|
|
|
|
|
if(iter != mmap.end()) {
|
|
|
|
_name = std::string{iter + 70};
|
|
|
|
_state = State::Valid;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_lastError = "The name couldn't be found in " + _filename;
|
|
|
|
_state = State::Invalid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mass::getFrameStyles() {
|
|
|
|
std::string path = Utility::Directory::join(_folder, _filename);
|
|
|
|
|
|
|
|
if(!Utility::Directory::exists(path)) {
|
|
|
|
_lastError = path + " couldn't be found.";
|
|
|
|
_state = State::Empty;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto mmap = Utility::Directory::mapRead(path);
|
|
|
|
|
|
|
|
auto iter = std::search(mmap.begin(), mmap.end(), &frame_styles_locator[0], &frame_styles_locator[90]);
|
|
|
|
|
|
|
|
if(iter != mmap.end()) {
|
|
|
|
iter += 0x5A;
|
|
|
|
std::copy(reinterpret_cast<const Int*>(iter), reinterpret_cast<const Int*>(iter) + 4, _frameStyles.data());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_lastError = "Frame styles couldn't be found in " + path;
|
|
|
|
_state = State::Invalid;
|
|
|
|
}
|
|
|
|
}
|