Example #1
0
        private static void LoadItemsFromScene(TreasureChest currentStash, SaveInstance instance, string areaName)
        {
            // Load the save data for the area
            var save = new EnvironmentSave();

            save.AreaName = areaName;
            if (save.LoadFromFile(instance.SavePath))
            {
                var otherStashUID = StashAreaToStashUID[areaName];
                // the expected hierarchy data for items in the stashes
                string oldStashHierarchy = $"Hierarchy>1{otherStashUID}";
                string newStashHierarchy = $"Hierarchy>1{currentStash.UID}";

                // get items in the stash, remove from old save and add to our list
                bool needSaveOtherScene = false;
                var  dataToSync         = new List <BasicSaveData>();
                for (int i = save.ItemList.Count - 1; i >= 0; i--)
                {
                    var data = save.ItemList[i];
                    // if item is in the stash
                    if (data.SyncData.Contains(oldStashHierarchy))
                    {
                        needSaveOtherScene = true;
                        // remove from the old scene save
                        save.ItemList.RemoveAt(i);
                        // change the parent from the old stash to the new one
                        data.SyncData = data.SyncData.Replace(oldStashHierarchy, newStashHierarchy);
                        // add to our data to sync
                        dataToSync.Add(data);
                    }
                    // if item is the stash itself
                    else if ((string)data.Identifier == otherStashUID)
                    {
                        // get the contained silver. could serialize it all and split, but this is a bit more optimized.
                        var sb = new StringBuilder();
                        // index of the silver value (string is 28 chars, plus the '/' separator)
                        int idx = data.SyncData.IndexOf("TreasureChestContainedSilver") + 29;
                        while (idx < data.SyncData.Length)
                        {
                            char c = data.SyncData[idx];
                            if (!char.IsDigit(c))
                            {
                                break;
                            }
                            sb.Append(c);
                            idx++;
                        }
                        int silver = int.Parse(sb.ToString());
                        if (silver > 0)
                        {
                            needSaveOtherScene = true;
                            // remove the silver
                            data.SyncData = data.SyncData.Replace($"TreasureChestContainedSilver/{silver};", "TreasureChestContainedSilver/0;");
                            // add to this scene
                            currentStash.AddSilver(silver);
                        }
                    }
                }

                if (needSaveOtherScene)
                {
                    // save the other scene to finalize removing the items and silver
                    save.ProcessSave();

                    // load the item data into this scene's stash
                    if (dataToSync.Any())
                    {
                        ItemManager.Instance.LoadItems(dataToSync, false);
                    }

                    Log.LogMessage($"Loaded stashed items from {areaName}");
                }
            }
        }