Jump to content

[Solved] Saving a changelog destroys internal structure?


Recommended Posts

I have some code below that loads cfg files, reads them, and then sets one value inside. 

		private void LoadChangelogs()
		{
			Debug.Log("[KCL] Loading changelogs...");
			ConfigNode[] changelogNodes = GameDatabase.Instance.GetConfigNodes("KERBALCHANGELOG"); //searches for any instances of files with a changelog node
			UrlDir.UrlConfig[] cfgDirs = GameDatabase.Instance.GetConfigs("KERBALCHANGELOG");
			bool firstVersionNumber = true;
			foreach (UrlDir.UrlConfig cfgDir in cfgDirs)//loops through all of the files found
			{
				bool show = true;
				ConfigNode cln = cfgDir.config;
				cln.TryGetValue("showChangelog", ref show);
				if (!show)
				{
					continue;
				}
				string mod = cln.GetValue("modName");
				string modChangelog = "";
				List<string> version = new List<string>();
				foreach(ConfigNode vn in cln.GetNodes("VERSION"))//adds version nodes to the string
				{
					if(!firstVersionNumber)
					{
						modChangelog += "\n";
					}
					firstVersionNumber = false;
					version.Add(vn.GetValue("version"));
					modChangelog += (vn.GetValue("version") + ":\n");
					foreach(string change in vn.GetValues("change"))
					{
						modChangelog += ("* " + change + "\n");
					}
				}
				modChangelogs.Add(new Tuple<string, string, string>(mod, modChangelog, HighestVersion(version)));
				if (!cln.SetValue("showChangelog", false))
				{
					Debug.Log("[KCL] Unable to set value 'showChangelog'.");
				}
				Debug.Log($"[KCL] {cfgDir.parent.fullPath}"); //Returns something like "C:/Users/BenjaminK/Desktop/KSP/1.5.1/GameData/CoolMod/Coolmodchangelog.cfg"
				cfgDir.config.Save(cfgDir.parent.fullPath); //Saves the file with the one field edited
				firstVersionNumber = true;
			}
			changesLoaded = true;
		}

Unfortunately, this changes a nicely formatted changelog like the one described here into this

Any suggestions as to what I can do to fix this?

Edited by Benjamin Kerman
Link to comment
Share on other sites

The structure is destroyed because you're not really saving the original contents of the file your ConfigNode comes from. ConfigNodes loaded by the game are all wrapped in a root ConfigNode that looks like this:

root
{
	KERBALCHANGELOG
	{
		...
	}
}

This is so you can have multiple "inner nodes" in one cfg. You want to save the file itself to keep this structure, using cfgDir.parent.SaveFile()

 

Link to comment
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.

×
×
  • Create New...