Ejemplo n.º 1
0
        public Villager(int offset, int idx, Save save)
        {
            _saveData = save;
            Index     = idx;
            Offset    = offset;
            Offsets   = VillagerInfo.GetVillagerInfo(save.SaveType);

            var structType = typeof(VillagerDataStruct);
            var offsetType = typeof(VillagerOffsets);

            if (save.SaveType == SaveType.WildWorld)
            {
                Exists = _saveData.ReadByte(offset + Offsets.VillagerId) != 0 && _saveData.ReadByte(offset + Offsets.VillagerId) != 0xFF;
            }
            else
            {
                Exists = _saveData.ReadUInt16(offset + Offsets.VillagerId, save.IsBigEndian) != 0 &&
                         _saveData.ReadUInt16(offset + Offsets.VillagerId, save.IsBigEndian) != 0xFFFF;
            }
            object boxedData = new VillagerDataStruct();

            foreach (var field in offsetType.GetFields(BindingFlags.Public | BindingFlags.Instance))
            {
                if (field.GetValue(Offsets) == null || field.Name.Contains("Count") ||
                    field.Name.Contains("Size"))
                {
                    continue;
                }
                if (structType.GetField(field.Name) == null)
                {
                    continue;
                }
                if (field.FieldType != typeof(int) || (int)field.GetValue(Offsets) == -1)
                {
                    continue;
                }

                var fieldType  = structType.GetField(field.Name).FieldType;
                var dataOffset = Offset + (int)field.GetValue(Offsets);

                if (field.Name == "VillagerId" && save.SaveType == SaveType.WildWorld) // Villager IDs are only a byte in WW
                {
                    structType.GetField(field.Name).SetValue(boxedData, _saveData.ReadByte(dataOffset));
                }
                else if (fieldType == typeof(byte))
                {
                    structType.GetField(field.Name).SetValue(boxedData, _saveData.ReadByte(dataOffset));
                }
                else if (fieldType == typeof(byte[]) && offsetType.GetField(field.Name + "Count") != null)
                {
                    structType.GetField(field.Name).SetValue(boxedData, _saveData.ReadByteArray(dataOffset,
                                                                                                (int)offsetType.GetField(field.Name + "Count").GetValue(Offsets)));
                }
                else if (fieldType == typeof(ushort))
                {
                    structType.GetField(field.Name).SetValue(boxedData,
                                                             _saveData.ReadUInt16(dataOffset, _saveData.IsBigEndian));
                }
                else if (fieldType == typeof(ushort[]))
                {
                    structType.GetField(field.Name).SetValue(boxedData, _saveData.ReadUInt16Array(dataOffset,
                                                                                                  (int)offsetType.GetField(field.Name + "Count").GetValue(Offsets),
                                                                                                  _saveData.IsBigEndian));
                }
                else if (fieldType == typeof(uint))
                {
                    structType.GetField(field.Name).SetValue(boxedData,
                                                             _saveData.ReadUInt32(dataOffset, _saveData.IsBigEndian));
                }
                else if (fieldType == typeof(string))
                {
                    structType.GetField(field.Name).SetValue(boxedData, new AcString(_saveData.ReadByteArray(
                                                                                         dataOffset,
                                                                                         (int)offsetType.GetField(field.Name + "Size").GetValue(Offsets)),
                                                                                     _saveData.SaveType)
                                                             .Trim());
                }
                else if (fieldType == typeof(Item))
                {
                    structType.GetField(field.Name).SetValue(boxedData,
                                                             save.SaveGeneration == SaveGeneration.N3DS
                            ? new Item(_saveData.ReadUInt32(dataOffset))
                            : new Item(_saveData.ReadUInt16(dataOffset, _saveData.IsBigEndian)));
                }
                else if (fieldType == typeof(Item[]))
                {
                    var collection =
                        new Item[(int)offsetType.GetField(field.Name + "Count").GetValue(Offsets)];
                    for (var i = 0; i < collection.Length; i++)
                    {
                        if (save.SaveGeneration == SaveGeneration.N3DS)
                        {
                            collection[i] = new Item(_saveData.ReadUInt32(dataOffset + i * 4));
                        }
                        else
                        {
                            collection[i] =
                                new Item(_saveData.ReadUInt16(dataOffset + i * 2, _saveData.IsBigEndian));
                        }
                    }

                    structType.GetField(field.Name).SetValue(boxedData, collection);
                }
            }

            Data = (VillagerDataStruct)boxedData;

            // Set Villager Name for e+ TODO: Separate translated e+ & untranslated e+.
            if (_saveData.SaveType == SaveType.DoubutsuNoMoriEPlus)
            {
                Name = _saveData.ReadString(Offset + 0xC, 6);
            }
            else if (_saveData.SaveType == SaveType.AnimalForestEPlus)
            {
                Name = _saveData.ReadString(Offset + 0xC, 8);
            }

            // Create Player Relations;
            if (save.SaveType != SaveType.AnimalCrossing)
            {
                return;
            }
            {
                PlayerRelations = new PlayerRelation[7];
                for (var i = 0; i < 7; i++)
                {
                    PlayerRelations[i] = new AcPlayerRelation(save, this, Offset + 0x10 + i * 0x138);
                }
            }
        }
Ejemplo n.º 2
0
        public NewVillager(int offset, int idx, Save save)
        {
            SaveData = save;
            Index    = idx;
            Offset   = offset;
            Offsets  = VillagerInfo.GetVillagerInfo(save.Save_Type);
            if (save.Save_Type == SaveType.Wild_World)
            {
                Exists = SaveData.ReadByte(offset + Offsets.Villager_ID) != 0 && SaveData.ReadByte(offset + Offsets.Villager_ID) != 0xFF;
            }
            else
            {
                Exists = SaveData.ReadUInt16(offset + Offsets.Villager_ID, save.Is_Big_Endian) != 0 && SaveData.ReadUInt16(offset + Offsets.Villager_ID, save.Is_Big_Endian) != 0xFFFF;
            }
            object BoxedData = new VillagerDataStruct();

            foreach (var Field in typeof(VillagerOffsets).GetFields(BindingFlags.Public | BindingFlags.Instance))
            {
                if (Field.GetValue(Offsets) != null && !Field.Name.Contains("Count") && !Field.Name.Contains("Size"))
                {
                    if (typeof(VillagerDataStruct).GetField(Field.Name) != null)
                    {
                        if (Field.FieldType == typeof(int) && (int)Field.GetValue(Offsets) != -1)
                        {
                            Type FieldType  = typeof(VillagerDataStruct).GetField(Field.Name).FieldType;
                            int  DataOffset = Offset + (int)Field.GetValue(Offsets);

                            if (Field.Name == "Villager_ID" && save.Save_Type == SaveType.Wild_World) //Villager IDs are only a byte in WW
                            {
                                typeof(VillagerDataStruct).GetField(Field.Name).SetValue(BoxedData, SaveData.ReadByte(DataOffset));
                            }
                            else if (FieldType == typeof(byte))
                            {
                                typeof(VillagerDataStruct).GetField(Field.Name).SetValue(BoxedData, SaveData.ReadByte(DataOffset));
                            }
                            else if (FieldType == typeof(byte[]) && typeof(VillagerOffsets).GetField(Field.Name + "Count") != null)
                            {
                                typeof(VillagerDataStruct).GetField(Field.Name).SetValue(BoxedData, SaveData.ReadByteArray(DataOffset,
                                                                                                                           (int)typeof(VillagerOffsets).GetField(Field.Name + "Count").GetValue(Offsets)));
                            }
                            else if (FieldType == typeof(ushort))
                            {
                                typeof(VillagerDataStruct).GetField(Field.Name).SetValue(BoxedData, SaveData.ReadUInt16(DataOffset, SaveData.Is_Big_Endian));
                            }
                            else if (FieldType == typeof(ushort[]))
                            {
                                typeof(VillagerDataStruct).GetField(Field.Name).SetValue(BoxedData, SaveData.ReadUInt16Array(DataOffset,
                                                                                                                             (int)typeof(VillagerOffsets).GetField(Field.Name + "Count").GetValue(Offsets), SaveData.Is_Big_Endian));
                            }
                            else if (FieldType == typeof(uint))
                            {
                                typeof(VillagerDataStruct).GetField(Field.Name).SetValue(BoxedData, SaveData.ReadUInt32(DataOffset, SaveData.Is_Big_Endian));
                            }
                            else if (FieldType == typeof(string))
                            {
                                typeof(VillagerDataStruct).GetField(Field.Name).SetValue(BoxedData, new ACString(SaveData.ReadByteArray(DataOffset,
                                                                                                                                        (int)typeof(VillagerOffsets).GetField(Field.Name + "Size").GetValue(Offsets)), SaveData.Save_Type).Trim());
                            }
                            else if (FieldType == typeof(string[]))
                            {
                            }
                            //Add logic
                            else if (FieldType == typeof(Item))
                            {
                                if (save.Save_Type == SaveType.New_Leaf || save.Save_Type == SaveType.Welcome_Amiibo)
                                {
                                    typeof(VillagerDataStruct).GetField(Field.Name).SetValue(BoxedData, new Item(SaveData.ReadUInt32(DataOffset, false)));
                                }
                                else
                                {
                                    typeof(VillagerDataStruct).GetField(Field.Name).SetValue(BoxedData, new Item(SaveData.ReadUInt16(DataOffset, SaveData.Is_Big_Endian)));
                                }
                            }
                            else if (FieldType == typeof(Item[]))
                            {
                                Item[] Collection = new Item[(int)typeof(VillagerOffsets).GetField(Field.Name + "Count").GetValue(Offsets)];
                                for (int i = 0; i < Collection.Length; i++)
                                {
                                    if (save.Save_Type == SaveType.New_Leaf || save.Save_Type == SaveType.Welcome_Amiibo)
                                    {
                                        Collection[i] = new Item(SaveData.ReadUInt32(DataOffset + i * 4, false));
                                    }
                                    else
                                    {
                                        Collection[i] = new Item(SaveData.ReadUInt16(DataOffset + i * 2, SaveData.Is_Big_Endian));
                                    }
                                }
                                typeof(VillagerDataStruct).GetField(Field.Name).SetValue(BoxedData, Collection);
                            }
                        }
                    }
                }
            }
            Data = (VillagerDataStruct)BoxedData;
        }