mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 02:40:05 +00:00 
			
		
		
		
	Linked vector packet
This commit is contained in:
		
							parent
							
								
									cb86ab4b9b
								
							
						
					
					
						commit
						ab702e9927
					
				
					 6 changed files with 351 additions and 0 deletions
				
			
		
							
								
								
									
										121
									
								
								wxdata/source/scwx/wsr88d/rpg/linked_vector_packet.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										121
									
								
								wxdata/source/scwx/wsr88d/rpg/linked_vector_packet.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,121 @@ | |||
| #include <scwx/wsr88d/rpg/linked_vector_packet.hpp> | ||||
| #include <scwx/wsr88d/rpg/vector2d.hpp> | ||||
| 
 | ||||
| #include <array> | ||||
| #include <istream> | ||||
| #include <set> | ||||
| #include <string> | ||||
| 
 | ||||
| #include <boost/log/trivial.hpp> | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
| namespace wsr88d | ||||
| { | ||||
| namespace rpg | ||||
| { | ||||
| 
 | ||||
| static const std::string logPrefix_ = | ||||
|    "[scwx::wsr88d::rpg::linked_vector_packet] "; | ||||
| 
 | ||||
| class LinkedVectorPacketImpl | ||||
| { | ||||
| public: | ||||
|    explicit LinkedVectorPacketImpl() : | ||||
|        packetCode_ {}, lengthOfBlock_ {}, valueOfVector_ {}, vectorList_ {} {}; | ||||
|    ~LinkedVectorPacketImpl() = default; | ||||
| 
 | ||||
|    uint16_t packetCode_; | ||||
|    uint16_t lengthOfBlock_; | ||||
|    uint16_t valueOfVector_; | ||||
| 
 | ||||
|    std::vector<Vector2D> vectorList_; | ||||
| }; | ||||
| 
 | ||||
| LinkedVectorPacket::LinkedVectorPacket() : | ||||
|     p(std::make_unique<LinkedVectorPacketImpl>()) | ||||
| { | ||||
| } | ||||
| LinkedVectorPacket::~LinkedVectorPacket() = default; | ||||
| 
 | ||||
| LinkedVectorPacket::LinkedVectorPacket(LinkedVectorPacket&&) noexcept = default; | ||||
| LinkedVectorPacket& | ||||
| LinkedVectorPacket::operator=(LinkedVectorPacket&&) noexcept = default; | ||||
| 
 | ||||
| uint16_t LinkedVectorPacket::packet_code() const | ||||
| { | ||||
|    return p->packetCode_; | ||||
| } | ||||
| 
 | ||||
| uint16_t LinkedVectorPacket::length_of_block() const | ||||
| { | ||||
|    return p->lengthOfBlock_; | ||||
| } | ||||
| 
 | ||||
| uint16_t LinkedVectorPacket::value_of_vector() const | ||||
| { | ||||
|    return p->valueOfVector_; | ||||
| } | ||||
| 
 | ||||
| size_t LinkedVectorPacket::data_size() const | ||||
| { | ||||
|    return p->lengthOfBlock_ + 4u; | ||||
| } | ||||
| 
 | ||||
| bool LinkedVectorPacket::Parse(std::istream& is) | ||||
| { | ||||
|    bool blockValid = true; | ||||
| 
 | ||||
|    is.read(reinterpret_cast<char*>(&p->packetCode_), 2); | ||||
|    is.read(reinterpret_cast<char*>(&p->lengthOfBlock_), 2); | ||||
| 
 | ||||
|    p->packetCode_    = ntohs(p->packetCode_); | ||||
|    p->lengthOfBlock_ = ntohs(p->lengthOfBlock_); | ||||
| 
 | ||||
|    size_t vectorSize = p->lengthOfBlock_; | ||||
| 
 | ||||
|    if (is.eof()) | ||||
|    { | ||||
|       BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "Reached end of file"; | ||||
|       blockValid = false; | ||||
|    } | ||||
|    else if (p->packetCode_ == 9) | ||||
|    { | ||||
|       is.read(reinterpret_cast<char*>(&p->valueOfVector_), 2); | ||||
|       p->valueOfVector_ = ntohs(p->valueOfVector_); | ||||
| 
 | ||||
|       vectorSize -= 2; | ||||
|    } | ||||
| 
 | ||||
|    // The number of vectors is equal to the size divided by the number of bytes
 | ||||
|    // in a vector
 | ||||
|    size_t vectorCount = Vector2D::SIZE / 8; | ||||
| 
 | ||||
|    for (size_t v = 0; v < vectorCount && !is.eof(); v++) | ||||
|    { | ||||
|       Vector2D vector; | ||||
|       vector.Parse(is); | ||||
|       p->vectorList_.push_back(std::move(vector)); | ||||
|    } | ||||
| 
 | ||||
|    if (is.eof()) | ||||
|    { | ||||
|       BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "Reached end of file"; | ||||
|       blockValid = false; | ||||
|    } | ||||
|    else | ||||
|    { | ||||
|       if (p->packetCode_ != 6 && p->packetCode_ != 9) | ||||
|       { | ||||
|          BOOST_LOG_TRIVIAL(warning) | ||||
|             << logPrefix_ << "Invalid packet code: " << p->packetCode_; | ||||
|          blockValid = false; | ||||
|       } | ||||
|    } | ||||
| 
 | ||||
|    return blockValid; | ||||
| } | ||||
| 
 | ||||
| } // namespace rpg
 | ||||
| } // namespace wsr88d
 | ||||
| } // namespace scwx
 | ||||
							
								
								
									
										20
									
								
								wxdata/source/scwx/wsr88d/rpg/packet.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								wxdata/source/scwx/wsr88d/rpg/packet.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,20 @@ | |||
| #include <scwx/wsr88d/rpg/packet.hpp> | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
| namespace wsr88d | ||||
| { | ||||
| namespace rpg | ||||
| { | ||||
| 
 | ||||
| static const std::string logPrefix_ = "[scwx::wsr88d::rpg::packet] "; | ||||
| 
 | ||||
| Packet::Packet()  = default; | ||||
| Packet::~Packet() = default; | ||||
| 
 | ||||
| Packet::Packet(Packet&&) noexcept = default; | ||||
| Packet& Packet::operator=(Packet&&) noexcept = default; | ||||
| 
 | ||||
| } // namespace rpg
 | ||||
| } // namespace wsr88d
 | ||||
| } // namespace scwx
 | ||||
							
								
								
									
										87
									
								
								wxdata/source/scwx/wsr88d/rpg/vector2d.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										87
									
								
								wxdata/source/scwx/wsr88d/rpg/vector2d.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,87 @@ | |||
| #include <scwx/wsr88d/rpg/vector2d.hpp> | ||||
| 
 | ||||
| #include <array> | ||||
| #include <istream> | ||||
| #include <set> | ||||
| #include <string> | ||||
| 
 | ||||
| #include <boost/log/trivial.hpp> | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
| namespace wsr88d | ||||
| { | ||||
| namespace rpg | ||||
| { | ||||
| 
 | ||||
| static const std::string logPrefix_ = "[scwx::wsr88d::rpg::vector2d] "; | ||||
| 
 | ||||
| class Vector2DImpl | ||||
| { | ||||
| public: | ||||
|    explicit Vector2DImpl() : startI_ {}, startJ_ {}, endI_ {}, endJ_ {} {}; | ||||
|    ~Vector2DImpl() = default; | ||||
| 
 | ||||
|    int16_t startI_; | ||||
|    int16_t startJ_; | ||||
|    int16_t endI_; | ||||
|    int16_t endJ_; | ||||
| }; | ||||
| 
 | ||||
| Vector2D::Vector2D() : p(std::make_unique<Vector2DImpl>()) {} | ||||
| Vector2D::~Vector2D() = default; | ||||
| 
 | ||||
| Vector2D::Vector2D(Vector2D&&) noexcept = default; | ||||
| Vector2D& Vector2D::operator=(Vector2D&&) noexcept = default; | ||||
| 
 | ||||
| int16_t Vector2D::start_i() const | ||||
| { | ||||
|    return p->startI_; | ||||
| } | ||||
| 
 | ||||
| int16_t Vector2D::start_j() const | ||||
| { | ||||
|    return p->startJ_; | ||||
| } | ||||
| 
 | ||||
| int16_t Vector2D::end_i() const | ||||
| { | ||||
|    return p->endI_; | ||||
| } | ||||
| 
 | ||||
| int16_t Vector2D::end_j() const | ||||
| { | ||||
|    return p->endJ_; | ||||
| } | ||||
| 
 | ||||
| size_t Vector2D::data_size() const | ||||
| { | ||||
|    return SIZE; | ||||
| } | ||||
| 
 | ||||
| bool Vector2D::Parse(std::istream& is) | ||||
| { | ||||
|    bool blockValid = true; | ||||
| 
 | ||||
|    is.read(reinterpret_cast<char*>(&p->startI_), 2); | ||||
|    is.read(reinterpret_cast<char*>(&p->startJ_), 2); | ||||
|    is.read(reinterpret_cast<char*>(&p->endI_), 2); | ||||
|    is.read(reinterpret_cast<char*>(&p->endJ_), 2); | ||||
| 
 | ||||
|    p->startI_ = ntohs(p->startI_); | ||||
|    p->startJ_ = ntohs(p->startJ_); | ||||
|    p->endI_   = ntohs(p->endI_); | ||||
|    p->endJ_   = ntohs(p->endJ_); | ||||
| 
 | ||||
|    if (is.eof()) | ||||
|    { | ||||
|       BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "Reached end of file"; | ||||
|       blockValid = false; | ||||
|    } | ||||
| 
 | ||||
|    return blockValid; | ||||
| } | ||||
| 
 | ||||
| } // namespace rpg
 | ||||
| } // namespace wsr88d
 | ||||
| } // namespace scwx
 | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat