mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 20:20:06 +00:00
Refactor ScitForecastDataPacket to ScitDataPacket
This commit is contained in:
parent
3b110516ed
commit
d92c1e50f2
6 changed files with 128 additions and 137 deletions
80
wxdata/source/scwx/wsr88d/rpg/scit_data_packet.cpp
Normal file
80
wxdata/source/scwx/wsr88d/rpg/scit_data_packet.cpp
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
#include <scwx/wsr88d/rpg/scit_data_packet.hpp>
|
||||
#include <scwx/util/logger.hpp>
|
||||
|
||||
#include <istream>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
namespace wsr88d
|
||||
{
|
||||
namespace rpg
|
||||
{
|
||||
|
||||
static const std::string logPrefix_ = "scwx::wsr88d::rpg::scit_data_packet";
|
||||
static const auto logger_ = util::Logger::Create(logPrefix_);
|
||||
|
||||
static const std::set<std::uint16_t> packetCodes_ = {23, 24};
|
||||
|
||||
class ScitDataPacket::Impl
|
||||
{
|
||||
public:
|
||||
explicit Impl() : data_ {}, recordCount_ {0} {}
|
||||
~Impl() = default;
|
||||
|
||||
std::vector<std::uint8_t> data_;
|
||||
size_t recordCount_;
|
||||
};
|
||||
|
||||
ScitDataPacket::ScitDataPacket() : p(std::make_unique<Impl>()) {}
|
||||
ScitDataPacket::~ScitDataPacket() = default;
|
||||
|
||||
ScitDataPacket::ScitDataPacket(ScitDataPacket&&) noexcept = default;
|
||||
ScitDataPacket& ScitDataPacket::operator=(ScitDataPacket&&) noexcept = default;
|
||||
|
||||
const std::vector<std::uint8_t>& ScitDataPacket::data() const
|
||||
{
|
||||
return p->data_;
|
||||
}
|
||||
|
||||
size_t ScitDataPacket::RecordCount() const
|
||||
{
|
||||
return p->recordCount_;
|
||||
}
|
||||
|
||||
bool ScitDataPacket::ParseData(std::istream& is)
|
||||
{
|
||||
bool blockValid = true;
|
||||
|
||||
if (!packetCodes_.contains(packet_code()))
|
||||
{
|
||||
logger_->warn("Invalid packet code: {}", packet_code());
|
||||
blockValid = false;
|
||||
}
|
||||
|
||||
if (blockValid)
|
||||
{
|
||||
p->recordCount_ = length_of_block();
|
||||
p->data_.resize(p->recordCount_);
|
||||
is.read(reinterpret_cast<char*>(p->data_.data()), p->recordCount_);
|
||||
}
|
||||
|
||||
return blockValid;
|
||||
}
|
||||
|
||||
std::shared_ptr<ScitDataPacket> ScitDataPacket::Create(std::istream& is)
|
||||
{
|
||||
std::shared_ptr<ScitDataPacket> packet = std::make_shared<ScitDataPacket>();
|
||||
|
||||
if (!packet->Parse(is))
|
||||
{
|
||||
packet.reset();
|
||||
}
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
} // namespace rpg
|
||||
} // namespace wsr88d
|
||||
} // namespace scwx
|
||||
Loading…
Add table
Add a link
Reference in a new issue