XPath Help... inserting into a Vanilla Def file

Started by QuantumX, November 29, 2017, 02:18:12 PM

Previous topic - Next topic

QuantumX

So trying to do what i assume is a very simple insert of text i for life of me cannot get it to work.

Could someone tell me (and explain) how to do the following with XPath?

Original Def Code is here.

<ThingDef ParentName="BenchBase">
    <defName>HiTechResearchBench</defName>
    <label>hi-tech research bench</label>
    <description>A high-tech bench with computers and electronic measurement equipment. Research is sped up by the high-tech tools. Requires power.</description>
    <thingClass>Building_ResearchBench</thingClass>
    <size>(5,2)</size>
    <stuffCategories>
      <li>Metallic</li>
    </stuffCategories>
    <costStuffCount>150</costStuffCount>
    <costList>
      <Steel>100</Steel>
      <Component>10</Component>
    </costList>
    <graphicData>
      <texPath>Things/Building/Production/ResearchBenchHiTech</texPath>
      <graphicClass>Graphic_Multi</graphicClass>
      <shaderType>CutoutComplex</shaderType>
      <drawSize>(7,4)</drawSize>
      <damageData>
        <cornerTL>Damage/Corner</cornerTL>
        <cornerTR>Damage/Corner</cornerTR>
        <cornerBL>Damage/Corner</cornerBL>
        <cornerBR>Damage/Corner</cornerBR>
      </damageData>
    </graphicData>
    <altitudeLayer>Building</altitudeLayer>
    <passability>PassThroughOnly</passability>
    <pathCost>70</pathCost>
    <statBases>
      <MaxHitPoints>250</MaxHitPoints>
      <WorkToBuild>5000</WorkToBuild>
      <Flammability>1.0</Flammability>
      <ResearchSpeedFactor>1.0</ResearchSpeedFactor>
    </statBases>
    <comps>
      <li Class="CompProperties_Power">
        <compClass>CompPowerTrader</compClass>
        <basePowerConsumption>250</basePowerConsumption>
        <startElectricalFires>true</startElectricalFires>
      </li>
      <li Class="CompProperties_Flickable"/>
      <li Class="CompProperties_AffectedByFacilities">
        <linkableFacilities>
          <li>MultiAnalyzer</li>
        </linkableFacilities>
      </li>
    </comps>
    <fillPercent>0.5</fillPercent>
    <interactionCellOffset>(0,0,-1)</interactionCellOffset>
    <hasInteractionCell>true</hasInteractionCell>
    <terrainAffordanceNeeded>Heavy</terrainAffordanceNeeded>
    <designationCategory>Production</designationCategory>
    <surfaceType>Item</surfaceType>
    <designationHotKey>Misc4</designationHotKey>
<placeWorkers>
<li>PlaceWorker_ShowFacilitiesConnections</li>
</placeWorkers>
    <researchPrerequisites>
      <li>MicroelectronicsBasics</li>
    </researchPrerequisites>
  </ThingDef>


I want to make the following addition to the above

<li Class="CompProperties_AffectedByFacilities">
        <linkableFacilities>
          <li>MultiAnalyzer</li>
          <li>ResCogitator</li>
        </linkableFacilities>
      </li>


But i cannot work out how to get the "<li>ResCogitator</li>" part inserted?

Help please?


Nightinggale

<Patch>
<Operation Class="PatchOperationAdd">
<xpath>Defs/ThingDef[ defName= "HiTechResearchBench"]/CompProperties_AffectedByFacilities/linkableFacilities</xpath>
<value>
<li>ResCogitator</li>
</value>
</Operation>
</Patch>

You want to add something to a list, which is why you have to use PatchOperationAdd.
The path in xml you want to change is Defs/ThingDef/CompProperties_AffectedByFacilities/linkableFacilities, where ThingDef has DefName= "HiTechResearchBench".
For each location it finds, which matches the path (in this case just one), it will insert everything listed in value in the patch operation.
ModCheck - boost your patch loading times and include patchmods in your main mod.

QuantumX

Thanks Nightinggale,

Just created a patch.xml file using the above, creating an xml file with the below;

<?xml version="1.0" encoding="utf-8" ?>
<Patch>

<Operation Class="PatchOperationAdd">
<xpath>/Defs/ThingDef[ defName= "HiTechResearchBench"]/CompProperties_AffectedByFacilities/linkableFacilities</xpath>
<value>
<li>ResCogitator</li>
</value>
</Operation>

</Patch>


But upon loading i get error failed to patch.



TeflonJim

The comp property you're attempting to filter on is the value of a class attribute.

If you make your expression this, it should work:



/Defs/ThingDef[defName="HiTechResearchBench"]/comps/li[@Class="CompProperties_AffectedByFacilities"]/linkableFacilities



Where @ (in @Class) is used to indicate the expression should evaluate an attribute rather than an element name.

Nightinggale

Oops, I should have paid more attention to the actual path and not skip the middle section  :-[

It's actually a decent example of how xpath works because there is basically 3 building blocks.

Quote/Defs/ThingDef[defName="HiTechResearchBench"]/comps/li[@Class="CompProperties_AffectedByFacilities"]/linkableFacilities

  • Defs is used unconditionally and will match all paths with the chosen search pattern
  • ThingDef[defName="HiTechResearchBench"] is used when it looks up the content of a child element of a specific name, in this case defName
  • li[@Class="CompProperties_AffectedByFacilities"] is used when the keyword you search for is a property for the item you search for, that is they are written in the same line
2+3 are used in lists where multiple children have the same tag name and you want specific ones.

You should be able to match every single path you want using a combo of those 3.
ModCheck - boost your patch loading times and include patchmods in your main mod.

QuantumX

Ok, that makes alot more sense (i think), thanks for the responses guys, learned a lot.. going to try to make a few of the patches on my own now and see how i get on.

CannibarRechter

> li[@Class="CompProperties_AffectedByFacilities"]

As an observation, this method of patching a list item ("li") is superior to using the numbered method, as the number relies on the list item always being in a fixed position, whereas the quoted method is order independent.
CR All Mods and Tools Download Link
CR Total Texture Overhaul : Gives RimWorld a Natural Feel
CR Moddable: make RimWorld more moddable.
CR CompFX: display dynamic effects over RimWorld objects