Ejemplo n.º 1
0
        /// <summary>
        /// 保存玩家数据到硬盘中
        /// </summary>
        private static void Save(SerializableWeaponBundle bundle)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream      saveFile  = File.Create(Application.persistentDataPath + "/.wps.bdl");

            formatter.Serialize(saveFile, bundle);

            saveFile.Close();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 从硬盘中读取数据
        /// </summary>
        private static SerializableWeaponBundle Load()
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream      saveFile;

            try
            {
                saveFile = File.Open(Application.persistentDataPath + "/.wps.bdl", FileMode.Open);
            }
            catch (FileNotFoundException)
            {
                return(null);
            }

            object deserialized             = null;
            SerializableWeaponBundle bundle = null;

            try
            {
                deserialized = formatter.Deserialize(saveFile);
                bundle       = (SerializableWeaponBundle)deserialized;
            }
            catch (SerializationException)
            {
                if (deserialized == null)
                {
                    Debug.LogWarning("无法打开存档");
                }
                else
                {
                    Debug.LogWarning("读取到旧版本存档,尝试解析……");
                    var weapons = (PreserializedWeapon[])deserialized.GetType().GetField("weapons").GetValue(deserialized);
                    var partIDs = (string[])deserialized.GetType().GetField("partIDs").GetValue(deserialized);

                    if (weapons == null || partIDs == null)
                    {
                        Debug.LogWarning("无法解析出武器与部件,文件可能已经损坏");
                    }
                    else
                    {
                        Debug.LogWarning("文件解析成功,读取武器和部件信息……");
                        bundle.weapons = weapons;
                        bundle.partIDs = partIDs;
                    }
                }
            }

            saveFile.Close();
            return(bundle);
        }
Ejemplo n.º 3
0
        public static void SaveToFile()
        {
            SerializableWeaponBundle bundle = new SerializableWeaponBundle();

            bundle.weapons = new PreserializedWeapon[Instance.weapons.Count];
            bundle.partIDs = new string[Instance.spareParts.Count];

            for (int i = 0; i < Instance.weapons.Count; i++)
            {
                bundle.weapons[i] = WeaponPreserializer.Preserializate(Instance.weapons[i]);
            }

            for (int i = 0; i < Instance.spareParts.Count; i++)
            {
                bundle.partIDs[i] = Instance.spareParts[i].PrefabID;
            }

            Save(bundle);
        }
Ejemplo n.º 4
0
        public static void LoadFromFile()
        {
            //var bundle = Load();
            SerializableWeaponBundle bundle = null;

            if (bundle == null)
            {
                print("Bundle is null");
                if ((Instance.weapons?.Count > 0) ||
                    (Instance.spareParts?.Count > 0))
                {
                    return;
                }

                print("Have a set of default parts.");
                bundle         = new SerializableWeaponBundle();
                bundle.weapons = new PreserializedWeapon[0];
                bundle.partIDs = new string[] {
                    "AK47_R_V1",
                    "AK47_B_V1",
                    "A2_M_V1",
                    "9mm",
                    "AK47_St_V1",
                };
            }

            Instance.weapons.Clear();
            for (int i = 0; i < bundle.weapons.Length; i++)
            {
                var weapon = WeaponPreserializer.DeserializeWeapon(bundle.weapons[i]);
                Instance.weapons.Add(weapon);
                ReturnWeapon(weapon);
            }

            Instance.spareParts.Clear();
            for (int i = 0; i < bundle.partIDs.Length; i++)
            {
                var part = GameObject.Instantiate(WAPrefabStore.GetPartPrefab(bundle.partIDs[i]).gameObject).GetComponent <MonoPart>();
                Instance.spareParts.Add(part);
                ReturnPart(part);
            }
        }