示例#1
0
        public static NBTFolder ToNBT <T>(this T src, string TagName = "_ROOT_")
        {
            NBTFolder root  = new NBTFolder(TagName);
            NBTString _type = new NBTString("_TYPE", typeof(T).FullName); // We only need to store the root type so we have a way to compare

            root.Add(_type);
            Type X = typeof(T);

            FieldInfo[] fields = X.GetFields();


            ProcessFields(fields, src, root);



            return(root);
        }
示例#2
0
        private static void ProcessFields(FieldInfo[] fields, object src, NBTTag parent)
        {
            NBTTag currentParent = parent;

            foreach (FieldInfo fi in fields)
            {
                //Console.WriteLine($"Name: {fi.Name}\nType: {fi.FieldType.Name}\nValue: {fi.GetValue(src)}\n\n");
                NBTTag theTag = null;
                switch (fi.FieldType.Name)
                {
                case "String":
                    theTag = new NBTString(fi.Name, (string)fi.GetValue(src));

                    break;

                case "Int32":
                    theTag = new NBTInt(fi.Name, (int)fi.GetValue(src));
                    break;

                case "Double":
                    theTag = new NBTDouble(fi.Name, (double)fi.GetValue(src));
                    break;

                case "String[]":
                    theTag = new NBTStringArray(fi.Name, (string[])fi.GetValue(src));
                    break;

                case "Int32[]":
                    theTag = new NBTIntArray(fi.Name, (int[])fi.GetValue(src));
                    break;

                case "Single":
                    theTag = new NBTFloat(fi.Name, (float)fi.GetValue(src));
                    break;

                case "Byte":
                    theTag = new NBTByte(fi.Name, (byte)fi.GetValue(src));
                    break;

                case "List`1":
                    if (fi.GetValue(src).ToString().Contains("System.String"))
                    {
                        theTag = new NBTStringArray(fi.Name, (List <string>)fi.GetValue(src));
                    }
                    else if (fi.GetValue(src).ToString().Contains("System.Int32"))
                    {
                        theTag = new NBTIntArray(fi.Name, (List <int>)fi.GetValue(src));
                    }
                    else if (fi.GetValue(src).ToString().Contains("System.Byte"))
                    {
                        theTag = new NBTByteArray(fi.Name, (List <byte>)fi.GetValue(src));
                    }
                    else if (fi.GetValue(src).ToString().Contains("System.Single"))
                    {
                        theTag = new NBTFloatArray(fi.Name, (List <float>)fi.GetValue(src));
                    }
                    else if (fi.GetValue(src).ToString().Contains("System.Double"))
                    {
                        theTag = new NBTDoubleArray(fi.Name, (List <double>)fi.GetValue(src));
                    }
                    else
                    {
                        // Treat as a compound tag
                        NBTFolder tags = new NBTFolder(fi.Name);
                        Type      _typ = Type.GetType(fi.GetValue(src).ToString());
                        tags.Add(new NBTString("_TYPE", _typ.FullName));
                    }


                    if (theTag == null)
                    {
                        continue;
                    }
                    else
                    {
                        break;
                    }

                case "Byte[]":
                    theTag = new NBTByteArray(fi.Name, (byte[])fi.GetValue(src));
                    break;

                case "Single[]":
                    theTag = new NBTFloatArray(fi.Name, (float[])fi.GetValue(src));
                    break;

                case "Double[]":
                    theTag = new NBTDoubleArray(fi.Name, (double[])fi.GetValue(src));
                    break;

                default:
                    // Try to run as a folder
                    var       valx      = fi.GetValue(src);
                    NBTFolder thefolder = new NBTFolder(fi.Name);
                    ProcessFields(fi.FieldType.GetFields(), valx, thefolder);
                    theTag = thefolder;
                    break;
                }
                switch (currentParent.TagType)
                {
                case NBTTagType.COMPOUND:
                    NBTFolder parFold = currentParent as NBTFolder;
                    parFold.Add(theTag);
                    break;

                default:
                    continue;
                }
            }
        }