Is it possible to write logic in C# to decide if an XML patch will be applied or not?
I want to conditionally execute an XML patch based on a value in mod settings.
Looks like GetSettings<Settings>() is available when Verse.Mod is allocated, which is before running the patches. In other words it can be used. The approach to do so would be:
public class CheckSetting : PatchOperation
{
public string someInfo;
protected override bool ApplyWorker(XmlDocument xml)
{
try
{
// some code with GetSettings<Settings>(), which might return true
}
catch
{
}
return false;
}
}
If you return true, then the next operation in a sequence will be reached. If you return false, the rest of the operations will be ignored. Using try-catch prevents any possible issues with settings not existing and such. Better be safe than sorry.
The properties, like in this case someInfo is the tags in xml and they will be null if not filled out. You can skip those if you have nothing to use them for.
The PatchOperation can then be called from xml by using the class namespace.classname.
All files are patched by all patches, meaning you will get a performance hit if your code is slow. Putting your operation after ModCheck.FindFile will avoid that issue.