Nils277

Using UpgradeScript from the SaveUpgradePipeline

Recommended Posts

Hi,

has anyone experience with the UpgradeScript from the SaveUpgradePipeline?
I want to replace one part in a save or craft file with another part of the same dimension and properties and therefore try to use the following script:

using System;
using SaveUpgradePipeline;
using UnityEngine;

namespace PlanetarySurfaceStructures
{
    [UpgradeModule(LoadContext.SFS | LoadContext.Craft, sfsNodeUrl = "GAME/FLIGHTSTATE", craftNodeUrl = "PART")]
    public class StorageUpgrader : UpgradeScript
    {
        ///FLIGHTSTATE/VESSEL/PART"

        //visible name of the upgrader
        public override string Name
        {
            get
            {
                return "Upgrader 2.0.0";
            }
        }

        //description for the upgrade process 
        public override string Description
        {
            get
            {
                return "Upgraded to BLA";
            }
        }

        //check the maximal version
        protected override bool CheckMaxVersion(Version v)
        {
            return true;// v <= TargetVersion;
        }

        //get the earliest version that can be upgraded
        public override Version EarliestCompatibleVersion
        {
            get
            {
                return new Version(1, 0, 4);
            }
        }

        //get the target version of the save file
        public override Version TargetVersion
        {
            get
            {
                return new Version(1, 3, 1);
            }
        }

        //test the save file for upgrades
        public override TestResult OnTest(ConfigNode node, LoadContext loadContext, ref string nodeName)
        {
           

            //when the parts of a craft should be checked
            if (loadContext == LoadContext.Craft)
            {
                Debug.Log("[STORAGEUPGRADE] OnTest - Craft");
                string[] values = node.GetValues();
                return checkPart(node, loadContext);
            }
            //when the savefile should be updated
            else if (loadContext == LoadContext.SFS)
            {
                Debug.Log("[STORAGEUPGRADE] OnTest - SFS");
                //iterate over all vessels in the savefile
                ConfigNode[] vessels = node.GetNodes("VESSEL");
                for (int i = 0; i < vessels.Length; i++)
                {
                    //iterate of all parts in the vessel
                    ConfigNode[] parts = vessels[i].GetNodes("PART");
                    for (int j = 0; j < parts.Length; j++)
                    {
                        if (checkPart(parts[j], loadContext) == TestResult.Upgradeable)
                        {
                            Debug.Log("[STORAGEUPGRADE] OnTest - Vessel \"" + vessels[i].GetValue("name") + "\" needs to be upgraded");
                            return TestResult.Upgradeable;
                        }
                    }
                }
            }
            Debug.Log("[STORAGEUPGRADE] OnTest - Save file is up to date");
            return TestResult.Pass;
        }

        //upgrade the save file
        public override void OnUpgrade(ConfigNode node, LoadContext loadContext)
        {
            //when the part of a craft should be upgraded
            if (loadContext == LoadContext.Craft)
            {
                upgradePart(node, loadContext);
            }
            else if (loadContext == LoadContext.SFS)
            {
                Debug.Log("[STORAGEUPGRADE] OnUpgrade - SFS");
                //string partName = node.GetValue("name");
                
                //iterate over all vessels in the savefile
                ConfigNode[] vessels = node.GetNodes("VESSEL");
                for (int i = 0; i < vessels.Length; i++)
                {
                    //iterate of all parts in the vessel
                    ConfigNode[] parts = vessels[i].GetNodes("PART");
                    for (int j = 0; j < parts.Length; j++)
                    {
                        upgradePart(parts[j], loadContext);
                    }
                }
            }
        }

        //Check of the parts has to be upgraded
        private TestResult checkPart(ConfigNode part, LoadContext loadContext)
        {
            string partName = part.GetValue("name");
            switch (partName)
            {
                case "KKAOSS.RCS.Tank":
                    return TestResult.Upgradeable;
            }
            return TestResult.Pass;
        }


        //Upgrade the part
        private void upgradePart(ConfigNode part, LoadContext loadContext)
        {
            string partName = part.GetValue("name");

            switch (partName)
            {
                case "KKAOSS.RCS.Tank":
                    Debug.Log("[STORAGEUPGRADE] upgrade Part:" + partName);
                    part.SetValue("name", "KKAOSS.Switchable.Tank");
                    part.AddValue("testValue", "BLA");
                    break;
            }
        }
    }
}

Judging from the debug information from the logs, all works as intended. First it is checked if something has to be upgraded (OnTest). This is the case an therefore the upgrade is done (onUpgrade). After the update the OnTest method is run again to check if everything is okay.

[LOG 22:14:01.028] [STORAGEUPGRADE] OnTest - SFS
[LOG 22:14:01.029] [STORAGEUPGRADE] OnTest - Vessel "Untitled Space Craft" needs to be upgraded
[LOG 22:14:01.037] [STORAGEUPGRADE] OnTest - SFS
[LOG 22:14:01.037] [STORAGEUPGRADE] OnTest - Vessel "Untitled Space Craft" needs to be upgraded
[LOG 22:14:01.038] [STORAGEUPGRADE] OnUpgrade - SFS
[LOG 22:14:01.039] [STORAGEUPGRADE] upgrade Part:KKAOSS.RCS.Tank
[LOG 22:14:01.039] [STORAGEUPGRADE] upgrade Part:KKAOSS.RCS.Tank
[LOG 22:14:01.040] [STORAGEUPGRADE] OnTest - SFS
[LOG 22:14:01.040] [STORAGEUPGRADE] OnTest - Save file is up to date

The thing is, although it seems to work on the paper (in the log) there are no changes made at all. Neither in the loaded game nor in the SFS file. Even the test value i added via AddValue("testValue, "BLA") to the node does not appear anywhere in the savefile.

It was stated that with this upgrade pipeline you don't have to take care of file operations and so on and one simply needs to change the config nodes.

Am i completely missing something here?  

Share this post


Link to post
Share on other sites

@JPLRepo Thanks for the hint to your mod using this update script.

I have to use NodeUtil.GetPartNodeName(node, loadContext) to get the name of the part.
Also it seems i need to add nodeName = NodeUtil.GetPartNodeName(node, loadContext); to the OnTest() method.
With this changes the Script is able to replace parts of crafts (loadContext == LoadContext.Craft) that are loaded.

Unfortunately it still does not work for the SFS files. I changed the script is to have:
[UpgradeModule(LoadContext.SFS | LoadContext.Craft, sfsNodeUrl = "GAME/FLIGHTSTATE/VESSEL/PART", craftNodeUrl = "PART")]
so one already gets the nodes for the config nodes for the parts in the OnTest and OnUpgrade methods.

Judging from the log file nothing goes wrong and the logs clearly state that the names of the parts were changed but no changes seem to be applied or saved.

Edited by Nils277

Share this post


Link to post
Share on other sites
This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.