Giter Site home page Giter Site logo

myitian.nbtserdes's Introduction

[Deprecated] Myitian.NbtSerDes

Deprecated because I'm writing an NBT serialization/deserialization library that supports SNBT and Bedrock Edition NBT formats, and also supports AOT.

一个NBT标签序列化/反序列化器

兼容以下任一运行时

.NET Framework 4.5及以上

.NET Core 2.0及以上

.NET 5.0及以上

示例

序列化/反序列化一个结构方块文件格式

(如果字段/属性名与目标NBT标签名一致,可不使用NbtProperty属性)

先定义如下结构方块文件类:

public class StructureNBTRoot
{
    [NbtProperty("DataVersion")]
    public int DataVersion { get; set; }
    [NbtProperty("author")]
    public string Author { get; set; }
    [NbtProperty("size"), NbtType(NbtType.TAG_List)]
    public int[] Size { get; set; }
    [NbtProperty("palette")]
    public StructureNBTPaletteItem[] Palette { get; set; }
    [NbtProperty("palettes")]
    public StructureNBTPaletteItem[][] Palettes { get; set; }
    [NbtProperty("blocks")]
    public StructureNBTBlock[] Blocks { get; set; }
    [NbtProperty("entities")]
    public StructureNBTEntity[] Entities { get; set; }
}
public class StructureNBTPaletteItem
{
    [NbtProperty("Name")]
    public string Name { get; set; }
    [NbtProperty("Properties")]
    public Dictionary<string, string> Properties { get; set; }
}
public class StructureNBTBlock
{
    [NbtProperty("state")]
    public int State { get; set; }
    [NbtProperty("pos"), NbtType(NbtType.TAG_List)]
    public int[] Pos { get; set; }
    [NbtProperty("nbt")]
    public dynamic Nbt { get; set; }
}
public class StructureNBTEntity
{
    [NbtProperty("pos"), NbtType(NbtType.TAG_List)]
    public double[] Pos { get; set; }
    [NbtProperty("bolckPos"), NbtType(NbtType.TAG_List)]
    public int[] BolckPos { get; set; }
    [NbtProperty("nbt"), NbtRequired]
    public dynamic Nbt { get; set; }
}

再使用如下代码(假设nbt文件在程序启动路径):

// 反序列化
byte[] nbtbytes = File.ReadAllBytes(@"with_mast.nbt");
StructureNBTRoot nbt = NbtConverter.DeserializeObject<StructureNBTRoot>(nbtbytes);

// 序列化
byte[] nbtbytes2 = NbtConverter.SerializeObject(nbt);

myitian.nbtserdes's People

Contributors

myitian avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.