data.cpp (1873B)
1 #include "data.hpp" 2 #include "../context.hpp" 3 #include "../raw_item.hpp" 4 #include "../../sink.hpp" 5 #include <iostream> 6 7 namespace Neptools::Stcm 8 { 9 10 void DataItem::Header::Validate(FilePosition chunk_size) const 11 { 12 #define VALIDATE(x) LIBSHIT_VALIDATE_FIELD("Stcm::DataItem::Header", x) 13 VALIDATE(type < 0xff); 14 VALIDATE(length <= chunk_size); 15 #undef VALIDATE 16 } 17 18 DataItem::DataItem(Key k, Context& ctx, const Header& raw, size_t chunk_size) 19 : ItemWithChildren{k, ctx} 20 { 21 raw.Validate(chunk_size); 22 23 type = raw.type; 24 offset_unit = raw.offset_unit; 25 field_8 = raw.field_8; 26 } 27 28 DataItem& DataItem::CreateAndInsert(ItemPointer ptr) 29 { 30 auto x = RawItem::Get<Header>(ptr); 31 32 auto& ret = x.ritem.SplitCreate<DataItem>( 33 ptr.offset, x.t, x.ritem.GetSize() - ptr.offset - sizeof(Header)); 34 if (x.t.length > 0) 35 ret.MoveNextToChild(x.t.length); 36 37 LIBSHIT_ASSERT(ret.GetSize() == sizeof(Header) + x.t.length); 38 39 // check heuristics 40 if (!ret.GetChildren().empty()) 41 DataFactory::Check(ret); 42 43 return ret; 44 } 45 46 void DataItem::Dump_(Sink& sink) const 47 { 48 Header hdr; 49 hdr.type = type; 50 hdr.offset_unit = offset_unit; 51 hdr.field_8 = field_8; 52 hdr.length = GetSize() - sizeof(Header); 53 sink.WriteGen(hdr); 54 55 ItemWithChildren::Dump_(sink); 56 } 57 58 void DataItem::Inspect_(std::ostream& os, unsigned indent) const 59 { 60 Item::Inspect_(os, indent); 61 os << "data(" << type << ", " << offset_unit << ", " << field_8 << ')'; 62 InspectChildren(os, indent); 63 } 64 65 FilePosition DataItem::GetSize() const noexcept 66 { 67 return ItemWithChildren::GetSize() + sizeof(Header); 68 } 69 70 void DataItem::Fixup() 71 { 72 ItemWithChildren::Fixup_(sizeof(Header)); 73 } 74 75 void DataFactory::Check(DataItem& it) 76 { 77 for (auto f : GetStore()) 78 if (f(it)) return; 79 } 80 81 } 82 83 #include "data.binding.hpp"