neptools

Modding tools to Neptunia games
git clone https://git.neptards.moe/neptards/neptools.git
Log | Files | Refs | Submodules | README | LICENSE

primitive_item.hpp (1993B)


      1 #ifndef UUID_2223739B_EEF5_4E62_B610_F34A86369503
      2 #define UUID_2223739B_EEF5_4E62_B610_F34A86369503
      3 #pragma once
      4 
      5 #include "item.hpp"
      6 #include "raw_item.hpp"
      7 #include "../sink.hpp"
      8 
      9 namespace Neptools
     10 {
     11 
     12   template <typename T, typename DumpT, typename Endian, char... Name>
     13   class PrimitiveItem final : public Item
     14   {
     15     static_assert(sizeof(T) == sizeof(DumpT));
     16     static_assert(sizeof(T) == sizeof(Endian));
     17     LIBSHIT_DYNAMIC_OBJECT;
     18   public:
     19     using Type = T;
     20 
     21     PrimitiveItem(Key k, Context& ctx, T val)
     22       : Item{k, ctx}, value{val} {}
     23     PrimitiveItem(Key k, Context& ctx, Source src)
     24       : Item{k, ctx}
     25     {
     26       Union u;
     27       src.PreadGen<Libshit::Check::Throw>(0, u.dump);
     28       value = u.native;
     29     }
     30     static PrimitiveItem& CreateAndInsert(ItemPointer ptr)
     31     {
     32       auto x = RawItem::GetSource(ptr, -1);
     33       return x.ritem.SplitCreate<PrimitiveItem>(ptr.offset, x.src);
     34     }
     35 
     36     FilePosition GetSize() const noexcept override { return sizeof(T); }
     37 
     38     T value;
     39 
     40   private:
     41     union Union
     42     {
     43       T native;
     44       DumpT dump;
     45     };
     46 
     47     void Dump_(Sink& sink) const override
     48     {
     49       Union u{value};
     50       sink.WriteGen(Endian{u.dump});
     51     }
     52     void Inspect_(std::ostream& os, unsigned indent) const override
     53     {
     54       Item::Inspect_(os, indent);
     55       static constexpr const char name[] = { Name..., 0 };
     56       os << name << '(' << value << ')';
     57     }
     58   };
     59 
     60 #define NEPTOOLS_PRIMITIVE_ITEMS(x)                                         \
     61   x(Int32Item, int32, PrimitiveItem<                                        \
     62     int32_t, int32_t, boost::endian::little_int32_t, 'i','n','t','3','2'>); \
     63   x(FloatItem, float, PrimitiveItem<                                        \
     64     float, int32_t, boost::endian::little_int32_t, 'f','l','o','a','t'>)
     65 #define NEPTOOLS_GEN(cname, lname, ...) \
     66   using cname LIBSHIT_LUAGEN(fullname: "neptools."#lname"_item") = __VA_ARGS__
     67   NEPTOOLS_PRIMITIVE_ITEMS(NEPTOOLS_GEN);
     68 #undef NEPTOOLS_GEN
     69 
     70 }
     71 
     72 #endif