patch operation add or replace

This commit is contained in:
c0ffee 2022-01-16 16:42:31 -08:00
parent c3a93a448f
commit d04ad49393
4 changed files with 75 additions and 8 deletions

View file

@ -9,20 +9,27 @@
<success>Always</success>
<operations>
<li Class="PatchOperationConditional">
<xpath>/Defs/Verse.ThingDef/comps</xpath>
<xpath>/Defs/ThingDef/comps</xpath>
<success>Always</success>
<nomatch Class="PatchOperationAdd">
<xpath>/Defs/Verse.ThingDef</xpath>
<value>
<comps />
</value>
<xpath>/Defs/ThingDef</xpath>
<value>
<comps />
</value>
</nomatch>
</li>
<li Class="PatchOperationAdd">
<xpath>/Defs/ThingDef[@Name="SK_BasePawn"]/comps</xpath>
<value>
<li Class="Rimworld_Animations.CompProperties_BodyAnimator" />
</value>
</li>
<li Class="PatchOperationAdd">
<xpath>/Defs/Verse.ThingDef/comps</xpath>
<xpath>/Defs/ThingDef[@Name="BaseAnimalPawn"]/comps</xpath>
<value>
<li Class="Rimworld_Animations.CompProperties_BodyAnimator" />
<li Class="Rimworld_Animations.CompProperties_BodyAnimator" />
</value>
</li>
</operations>

View file

@ -0,0 +1,59 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Verse;
namespace Rimworld_Animations
{
public class PatchOperationAddOrReplace : PatchOperationPathed
{
protected string key;
private XmlContainer value;
protected override bool ApplyWorker(XmlDocument xml)
{
XmlNode valNode = value.node;
bool result = false;
IEnumerator enumerator = xml.SelectNodes(xpath).GetEnumerator();
try
{
while (enumerator.MoveNext())
{
object obj = enumerator.Current;
result = true;
XmlNode parentNode = obj as XmlNode;
XmlNode xmlNode = parentNode.SelectSingleNode(key);
if (xmlNode == null)
{
// Add - Add node if not existing
xmlNode = parentNode.OwnerDocument.CreateElement(key);
parentNode.AppendChild(xmlNode);
}
else
{
// Replace - Clear existing children
xmlNode.RemoveAll();
}
// (Re)add value
xmlNode.AppendChild(parentNode.OwnerDocument.ImportNode(valNode.FirstChild, true));
}
}
finally
{
IDisposable disposable = enumerator as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
return result;
}
}
}

View file

@ -65,6 +65,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="1.3\Source\Utilities\PatchOperationAddOrReplace.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="1.3\Source\Actors\Actor.cs" />
<Compile Include="1.3\Source\Actors\AlienRaceOffset.cs" />
@ -76,7 +77,7 @@
<Compile Include="1.3\Source\Animations\Keyframes\Keyframe.cs" />
<Compile Include="1.3\Source\Animations\Keyframes\PawnKeyframe.cs" />
<Compile Include="1.3\Source\Animations\Keyframes\ThingKeyframe.cs" />
<Compile Include="1.3\Source\AnimationUtility.cs" />
<Compile Include="1.3\Source\Utilities\AnimationUtility.cs" />
<Compile Include="1.3\Source\Comps\CompBodyAnimator.cs" />
<Compile Include="1.3\Source\Comps\CompProperties_BodyAnimator.cs" />
<Compile Include="1.3\Source\Comps\CompProperties_ThingAnimator.cs" />