Ejemplo n.º 1
0
        public void OnProtoSerialize(ProtobufSerializer serializer)
        {
            foreach (string slot in SlotHelper.SlotNames)
            {
                EmModuleSaveData savedModule = SaveData.GetModuleInSlot(slot);
                InventoryItem    item        = this.Modules.GetItemInSlot(slot);

                if (item == null)
                {
                    savedModule.ItemID          = (int)TechType.None;
                    savedModule.RemainingCharge = -1f;
                }
                else
                {
                    savedModule.ItemID = (int)item.item.GetTechType();

                    Battery battery = item.item.GetComponent <Battery>();

                    if (battery == null)
                    {
                        savedModule.RemainingCharge = -1f;
                    }
                    else
                    {
                        savedModule.RemainingCharge = battery._charge;
                    }
                }
            }

            SaveData.Save();
        }
Ejemplo n.º 2
0
        public void OnProtoDeserializeObjectTree(ProtobufSerializer serializer)
        {
            if (SaveData.Load())
            {
                // Because the items here aren't being serialized with everything else normally,
                // I've used custom save data to handle whatever gets left in these slots.

                QuickLogger.Debug("Loading save data");
                // The following is a recreation of the essential parts of the Equipment.ResponseEquipment method.
                foreach (string slot in SlotHelper.SlotNames)
                {
                    // These slots need to be added before we can add items to them
                    this.Modules.AddSlot(slot);

                    EmModuleSaveData savedModule = SaveData.GetModuleInSlot(slot);

                    if (savedModule.ItemID == 0) // (int)TechType.None
                    {
                        continue;                // Nothing here
                    }
                    InventoryItem spanwedItem = CyclopsModule.SpawnCyclopsModule((TechType)savedModule.ItemID);
                    QuickLogger.Debug($"Spawned in {savedModule.ItemID} from save data");

                    if (spanwedItem is null)
                    {
                        continue;
                    }

                    if (savedModule.RemainingCharge > 0f) // Modules without batteries are stored with a -1 value for charge
                    {
                        spanwedItem.item.GetComponent <Battery>().charge = savedModule.RemainingCharge;
                    }

                    this.Modules.AddItem(slot, spanwedItem, true);
                }
            }
            else
            {
                UnlockDefaultModuleSlots();
            }
        }
        /// <summary>
        /// Called when the game is being saved.
        /// </summary>
        /// <param name="serializer">The serializer.</param>
        /// <see cref="IProtoEventListener"/>
        void IProtoEventListener.OnProtoSerialize(ProtobufSerializer serializer)
        {
            if (saveData == null)
            {
                PrepareSaveData();
            }

            for (int s = 0; s < TotalSlots; s++)
            {
                UpgradeSlot upgradeSlot = UpgradeSlotArray[s];

                EmModuleSaveData savedModule = saveData.GetModuleInSlot(upgradeSlot.slotName);
                InventoryItem    item        = upgradeSlot.GetItemInSlot();

                if (item == null)
                {
                    savedModule.ItemID          = (int)TechType.None;
                    savedModule.RemainingCharge = -1f;
                }
                else
                {
                    savedModule.ItemID = (int)item.item.GetTechType();

                    Battery battery = item.item.GetComponent <Battery>();

                    if (battery == null)
                    {
                        savedModule.RemainingCharge = -1f;
                    }
                    else
                    {
                        savedModule.RemainingCharge = battery._charge;
                    }
                }
            }

            saveData.Save();
        }
        /// <summary>
        /// Called when loading the game from a save file.
        /// </summary>
        /// <param name="serializer">The serializer.</param>
        /// <see cref="IProtoEventListener"/>
        void IProtoEventListener.OnProtoDeserialize(ProtobufSerializer serializer)
        {
            if (saveData == null)
            {
                PrepareSaveData();
            }

            if (this.Modules == null)
            {
                InitializeModules();
            }

            this.Modules.Clear();

            QuickLogger.Debug("Checking save data");

            if (saveData != null && saveData.Load())
            {
                // Because the items here aren't being serialized with everything else normally,
                // I've used custom save data to handle whatever gets left in these slots.

                QuickLogger.Debug("Loading save data");
                // The following is a recreation of the essential parts of the Equipment.ResponseEquipment method.
                for (int s = 0; s < TotalSlots; s++)
                {
                    string slot = UpgradeSlotArray[s].slotName;
                    // These slots need to be added before we can add items to them
                    this.Modules.AddSlot(slot);

                    EmModuleSaveData savedModule = saveData.GetModuleInSlot(slot);

                    if (savedModule.ItemID == 0) // (int)TechType.None
                    {
                        continue;                // Nothing here
                    }
                    var    techtype = (TechType)savedModule.ItemID;
                    string itemName = techtype.AsString();

                    QuickLogger.Debug($"Spawning '{itemName}' from save data");
                    InventoryItem spanwedItem = CyclopsUpgrade.SpawnCyclopsModule(techtype);

                    if (spanwedItem == null)
                    {
                        QuickLogger.Warning($"Unknown upgrade module '{itemName}' could not be spamned in from save data");
                        continue;
                    }

                    QuickLogger.Debug($"Spawned in {itemName} from save data");

                    if (savedModule.RemainingCharge > 0f) // Modules without batteries are stored with a -1 value for charge
                    {
                        spanwedItem.item.GetComponent <Battery>().charge = savedModule.RemainingCharge;
                    }

                    this.Modules.AddItem(slot, spanwedItem, true);
                    OnSlotEquipped(slot, spanwedItem);
                }
            }
            else
            {
                QuickLogger.Debug("No save data found.");
                this.Modules.AddSlots(SlotNames);
            }
        }