Mass: change how the name is obtained, and move the state enum.

This commit is contained in:
Guillaume Jacquemin 2021-08-28 20:20:09 +02:00
parent bd6e55826d
commit 69021eacdf
3 changed files with 69 additions and 30 deletions

View file

@ -33,23 +33,7 @@ Mass::Mass(const std::string& path) {
_folder = Utility::Directory::path(path);
_filename = Utility::Directory::filename(path);
if(!Utility::Directory::exists(path)) {
_lastError = path + " couldn't be found.";
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 = MassState::Valid;
}
else {
_lastError = "The name couldn't be found in " + _filename;
_state = MassState::Invalid;
}
refreshValues();
}
auto Mass::lastError() -> std::string const& {
@ -78,6 +62,10 @@ auto Mass::getNameFromFile(const std::string& path) -> std::string {
return name;
}
void Mass::refreshValues() {
getName();
}
auto Mass::filename() -> std::string const&{
return _filename;
}
@ -86,7 +74,7 @@ auto Mass::name() -> std::string const&{
return _name;
}
auto Mass::state() -> MassState {
auto Mass::state() -> State {
return _state;
}
@ -95,7 +83,7 @@ auto Mass::updateSteamId(const std::string& steam_id) -> bool {
if(!Utility::Directory::exists(path)) {
_lastError = path + " couldn't be found.";
_state = MassState::Empty;
_state = State::Empty;
return false;
}
@ -129,3 +117,49 @@ auto Mass::updateSteamId(const std::string& steam_id) -> bool {
return true;
}
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;
}
}

View file

@ -22,12 +22,13 @@
using namespace Magnum;
enum class MassState : UnsignedByte {
Empty, Invalid, Valid
};
class Mass {
public:
enum class State : UnsignedByte {
Empty, Invalid, Valid
};
explicit Mass(const std::string& path);
Mass(const Mass&) = delete;
@ -40,19 +41,23 @@ class Mass {
static auto getNameFromFile(const std::string& path) -> std::string;
void refreshValues();
auto filename() -> std::string const&;
auto name() -> std::string const&;
auto state() -> MassState;
auto state() -> State;
auto updateSteamId(const std::string& steam_id) -> bool;
private:
void getName();
static std::string _lastError;
std::string _folder;
std::string _filename;
std::string _name;
MassState _state = MassState::Empty;
State _state = State::Empty;
};

View file

@ -104,7 +104,7 @@ auto MassManager::exportMass(int hangar) -> bool {
return false;
}
if(_hangars[hangar].state() != MassState::Valid) {
if(_hangars[hangar].state() != Mass::State::Valid) {
_lastError = Utility::formatString("There is no valid data to export in hangar {:.2d}", hangar + 1);
return false;
}
@ -134,22 +134,22 @@ auto MassManager::moveMass(int source, int destination) -> bool {
std::string source_file = _hangars[source].filename();
std::string dest_file = _hangars[destination].filename();
MassState dest_state = _hangars[destination].state();
Mass::State dest_state = _hangars[destination].state();
switch(dest_state) {
case MassState::Empty:
case Mass::State::Empty:
break;
case MassState::Invalid:
case Mass::State::Invalid:
Utility::Directory::rm(dest_file);
break;
case MassState::Valid:
case Mass::State::Valid:
Utility::Directory::move(dest_file, dest_file + ".tmp");
break;
}
Utility::Directory::move(source_file, dest_file);
if(dest_state == MassState::Valid) {
if(dest_state == Mass::State::Valid) {
Utility::Directory::move(dest_file + ".tmp", source_file);
}