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); _folder = Utility::Directory::path(path);
_filename = Utility::Directory::filename(path); _filename = Utility::Directory::filename(path);
if(!Utility::Directory::exists(path)) { refreshValues();
_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;
}
} }
auto Mass::lastError() -> std::string const& { auto Mass::lastError() -> std::string const& {
@ -78,6 +62,10 @@ auto Mass::getNameFromFile(const std::string& path) -> std::string {
return name; return name;
} }
void Mass::refreshValues() {
getName();
}
auto Mass::filename() -> std::string const&{ auto Mass::filename() -> std::string const&{
return _filename; return _filename;
} }
@ -86,7 +74,7 @@ auto Mass::name() -> std::string const&{
return _name; return _name;
} }
auto Mass::state() -> MassState { auto Mass::state() -> State {
return _state; return _state;
} }
@ -95,7 +83,7 @@ auto Mass::updateSteamId(const std::string& steam_id) -> bool {
if(!Utility::Directory::exists(path)) { if(!Utility::Directory::exists(path)) {
_lastError = path + " couldn't be found."; _lastError = path + " couldn't be found.";
_state = MassState::Empty; _state = State::Empty;
return false; return false;
} }
@ -129,3 +117,49 @@ auto Mass::updateSteamId(const std::string& steam_id) -> bool {
return true; 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; using namespace Magnum;
enum class MassState : UnsignedByte {
Empty, Invalid, Valid
};
class Mass { class Mass {
public: public:
enum class State : UnsignedByte {
Empty, Invalid, Valid
};
explicit Mass(const std::string& path); explicit Mass(const std::string& path);
Mass(const Mass&) = delete; Mass(const Mass&) = delete;
@ -40,19 +41,23 @@ class Mass {
static auto getNameFromFile(const std::string& path) -> std::string; static auto getNameFromFile(const std::string& path) -> std::string;
void refreshValues();
auto filename() -> std::string const&; auto filename() -> std::string const&;
auto name() -> std::string const&; auto name() -> std::string const&;
auto state() -> MassState; auto state() -> State;
auto updateSteamId(const std::string& steam_id) -> bool; auto updateSteamId(const std::string& steam_id) -> bool;
private: private:
void getName();
static std::string _lastError; static std::string _lastError;
std::string _folder; std::string _folder;
std::string _filename; std::string _filename;
std::string _name; 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; 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); _lastError = Utility::formatString("There is no valid data to export in hangar {:.2d}", hangar + 1);
return false; return false;
} }
@ -134,22 +134,22 @@ auto MassManager::moveMass(int source, int destination) -> bool {
std::string source_file = _hangars[source].filename(); std::string source_file = _hangars[source].filename();
std::string dest_file = _hangars[destination].filename(); std::string dest_file = _hangars[destination].filename();
MassState dest_state = _hangars[destination].state(); Mass::State dest_state = _hangars[destination].state();
switch(dest_state) { switch(dest_state) {
case MassState::Empty: case Mass::State::Empty:
break; break;
case MassState::Invalid: case Mass::State::Invalid:
Utility::Directory::rm(dest_file); Utility::Directory::rm(dest_file);
break; break;
case MassState::Valid: case Mass::State::Valid:
Utility::Directory::move(dest_file, dest_file + ".tmp"); Utility::Directory::move(dest_file, dest_file + ".tmp");
break; break;
} }
Utility::Directory::move(source_file, dest_file); 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); Utility::Directory::move(dest_file + ".tmp", source_file);
} }