neptools

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

gbnl_lua.hpp (2766B)


      1 #ifndef UUID_907F2DFB_AA53_4295_B41F_9AAA950AEEB4
      2 #define UUID_907F2DFB_AA53_4295_B41F_9AAA950AEEB4
      3 #pragma once
      4 
      5 #include "gbnl.hpp"
      6 #include "../dynamic_struct.lua.hpp"
      7 
      8 #if LIBSHIT_WITH_LUA
      9 
     10 namespace Neptools
     11 {
     12 
     13   template <>
     14   struct DynamicStructTypeTraits<Gbnl::OffsetString>
     15   {
     16     static void Push(Libshit::Lua::StateRef vm, const void* ptr, size_t size)
     17     {
     18       LIBSHIT_ASSERT(size == sizeof(Gbnl::OffsetString)); (void) size;
     19       auto ofs = static_cast<const Gbnl::OffsetString*>(ptr);
     20       if (ofs->offset == static_cast<uint32_t>(-1))
     21         lua_pushnil(vm);
     22       else
     23         vm.Push(ofs->str);
     24     }
     25 
     26     static void Get(Libshit::Lua::StateRef vm, int idx, void* ptr, size_t size)
     27     {
     28       LIBSHIT_ASSERT(size == sizeof(Gbnl::OffsetString)); (void) size;
     29       auto ofs = static_cast<Gbnl::OffsetString*>(ptr);
     30 
     31       if (Libshit::Lua::IsNoneOrNil(lua_type(vm, idx)))
     32       {
     33         ofs->offset = static_cast<uint32_t>(-1);
     34         ofs->str.clear();
     35       }
     36       else
     37       {
     38         ofs->str = vm.Check<std::string>(idx);
     39         ofs->offset = 0; // no longer null
     40       }
     41     }
     42 
     43     static constexpr bool SIZABLE = false;
     44     static constexpr const char* NAME = "string";
     45   };
     46 
     47   // FixString is zero terminated, Padding is not
     48   template <>
     49   struct DynamicStructTypeTraits<Gbnl::FixStringTag>
     50   {
     51     static void Push(Libshit::Lua::StateRef vm, const void* ptr, size_t size)
     52     {
     53       auto str = static_cast<const char*>(ptr);
     54       lua_pushlstring(vm, str, strnlen(str, size));
     55     }
     56 
     57     static void Get(Libshit::Lua::StateRef vm, int idx, void* ptr, size_t size)
     58     {
     59       auto str = vm.Check<Libshit::StringView>(idx);
     60       auto dst = static_cast<char*>(ptr);
     61 
     62       auto n = std::min(size-1, str.length());
     63       memcpy(dst, str.data(), n);
     64       memset(dst+n, 0, size-n);
     65     }
     66 
     67     static constexpr bool SIZABLE = true;
     68     static constexpr const char* NAME = "fix_string";
     69   };
     70 
     71   template<>
     72   struct DynamicStructTypeTraits<Gbnl::PaddingTag>
     73   {
     74     static void Push(Libshit::Lua::StateRef vm, const void* ptr, size_t size)
     75     {
     76       auto str = static_cast<const char*>(ptr);
     77       lua_pushlstring(vm, str, size);
     78     }
     79 
     80     static void Get(Libshit::Lua::StateRef vm, int idx, void* ptr, size_t size)
     81     {
     82       auto str = vm.Check<Libshit::StringView>(idx);
     83       auto dst = static_cast<char*>(ptr);
     84 
     85       auto n = std::min(size, str.length());
     86       memcpy(dst, str.data(), n);
     87       memset(dst+n, 0, size-n);
     88     }
     89 
     90     static constexpr bool SIZABLE = true;
     91     static constexpr const char* NAME = "padding";
     92   };
     93 
     94 }
     95 
     96 NEPTOOLS_DYNAMIC_STRUCT_TABLECTOR(
     97   int8_t, int16_t, int32_t, int64_t, float,
     98   ::Neptools::Gbnl::OffsetString, ::Neptools::Gbnl::FixStringTag,
     99   ::Neptools::Gbnl::PaddingTag);
    100 
    101 #endif
    102 #endif