Пример #1
0
        private static void ProcessFieldData(FieldInfo[] fields, NBTFolder src, object dest)
        {
            NBTTag currentParent = src;

            foreach (FieldInfo fi in fields)
            {
                //Console.WriteLine($"Name: {fi.Name}\nType: {fi.FieldType.Name}\nValue: {fi.GetValue(dest)}\n\n");
                try
                {
                    switch (fi.FieldType.Name)
                    {
                    case "String":

                        fi.SetValue(dest, currentParent[fi.Name].StringValue);
                        break;

                    case "Int32":
                        fi.SetValue(dest, currentParent[fi.Name].IntValue);
                        break;

                    case "Double":
                        fi.SetValue(dest, currentParent[fi.Name].DoubleValue);
                        break;

                    case "String[]":
                        fi.SetValue(dest, currentParent[fi.Name].StringArrayValue);
                        break;

                    case "Int32[]":
                        fi.SetValue(dest, currentParent[fi.Name].IntArrayValue);
                        break;

                    case "Single":
                        fi.SetValue(dest, currentParent[fi.Name].FloatValue);
                        break;

                    case "Byte":
                        fi.SetValue(dest, currentParent[fi.Name].ByteValue);
                        break;

                    case "List`1":
                        string TypeStr = fi.GetValue(dest).ToString();
                        if (TypeStr.Contains("System.String"))
                        {
                            fi.SetValue(dest, currentParent[fi.Name].StringArrayValue.ToList());
                        }
                        else if (TypeStr.Contains("System.Int32"))
                        {
                            fi.SetValue(dest, currentParent[fi.Name].IntArrayValue.ToList());
                        }
                        else if (TypeStr.Contains("System.Byte"))
                        {
                            fi.SetValue(dest, currentParent[fi.Name].ByteArrayValue.ToList());
                        }
                        else if (TypeStr.Contains("System.Single"))
                        {
                            fi.SetValue(dest, currentParent[fi.Name].FloatArrayValue.ToList());
                        }
                        else if (TypeStr.Contains("System.Double"))
                        {
                            fi.SetValue(dest, currentParent[fi.Name].DoubleArrayValue.ToList());
                        }
                        break;

                    case "Byte[]":
                        fi.SetValue(dest, currentParent[fi.Name].ByteArrayValue);
                        break;

                    case "Single[]":
                        fi.SetValue(dest, currentParent[fi.Name].FloatArrayValue);
                        break;

                    case "Double[]":
                        fi.SetValue(dest, currentParent[fi.Name].DoubleArrayValue);
                        break;

                    default:
                        var       VALX      = fi.GetValue(dest);
                        NBTFolder theFolder = currentParent[fi.Name] as NBTFolder;
                        ProcessFieldData(fi.FieldType.GetFields(), theFolder, VALX);
                        break;
                    }
                } catch (Exception e) { } // Silently catch.. If it does not 100% conform, do not crash the program.
            }
        }
Пример #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;
                }
            }
        }