sprotoloader.lua 576 B

12345678910111213141516171819202122232425262728
  1. local parser = require "sprotoparser"
  2. local core = require "sproto.core"
  3. local sproto = require "sproto"
  4. local loader = {}
  5. function loader.register(filename, index)
  6. local f = assert(io.open(filename), "Can't open sproto file")
  7. local data = f:read "a"
  8. f:close()
  9. local sp = core.newproto(parser.parse(data))
  10. core.saveproto(sp, index)
  11. end
  12. function loader.save(bin, index)
  13. local sp = core.newproto(bin)
  14. core.saveproto(sp, index)
  15. end
  16. function loader.load(index)
  17. local sp = core.loadproto(index)
  18. -- no __gc in metatable
  19. return sproto.sharenew(sp)
  20. end
  21. return loader