Ludeon Forums

Ludeon Forums

  • June 19, 2021, 05:55:40 AM
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 5 6 [7]

Author Topic: A quick tutorial of xpathing and patching  (Read 68555 times)

Garwel

  • Muffalo
  • *
  • Posts: 9
  • Refugee
    • View Profile
Re: A quick tutorial of xpathing and patching
« Reply #90 on: March 10, 2021, 03:12:57 PM »

I'm trying to write a patch to add a comp to all items (i.e. ThingDefs with category set to "Item"). I use what seems to be an obvious solution:
Code: [Select]
<Operation Class="PatchOperationSequence">
<success>Always</success>
<operations>
<li Class="PatchOperationTest">
<xpath>/Defs/ThingDef[category="Item"]/comps</xpath>
<success>Invert</success>
</li>
<li Class="PatchOperationAdd">
<xpath>/Defs/ThingDef[category="Item"]</xpath>
<value>
<comps/>
</value>
</li>
</operations>
</Operation>
<Operation Class="PatchOperationAdd">
<success>Always</success>
<xpath>/Defs/ThingDef[category="Item"]/comps</xpath>
<value>
<li>
<compClass>DestroyItem.CompDestructible</compClass>
</li>
</value>
</Operation>

However, it mysteriously only applies to few items such as Apparel_ShieldBelt. I also tried to target abstract ThingDefs (e.g. with @Name="ApparelBase"), but the result is the same. Any ideas?

PS: I did some research and found that this code actually kind of works. But there are some ThingDefs it won't affect no matter what you do, because they are apparently not present in any XML files and are generated dynamically by the game. These include various types of meat, corpses, skill trainers etc. So I had to use C# instead to add the comp in code:
Code: [Select]
foreach (ThingDef def in DefDatabase<ThingDef>.AllDefs.Where(def => typeof(ThingWithComps).IsAssignableFrom(def.thingClass) && def.category == ThingCategory.Item).ToList())
    def.comps.Add(new CompProperties(typeof(CompDestructible)));
It seems to work ok.
« Last Edit: March 12, 2021, 01:21:51 AM by Garwel »
Logged

LWM

  • Planetologist
  • ****
  • Posts: 1081
  • Little White Mouse
    • View Profile
Re: A quick tutorial of xpathing and patching
« Reply #91 on: April 18, 2021, 07:39:10 PM »

A couple of notes on your xpaths:

First, the PatchOperationTest is mostly superfluous - the PatchOperationAdd will only affect things that match the xpath (and if you set <success>Always</success> you won't get an error message if nothing matches)

Second...what happens when something already has a set of comps?  You might want to do something like
Code: [Select]
  <PatchOperation Class="PatchOperationAdd">
    <xpath>/Defs/ThingDef[category="Item" and not(comps)]</xpath><!--only add comps if not there-->
    <value><comps /></value>
  </PatchOperation>
and then add your comp.

...I have often found that just going through the DefDatabase in C# is the easier approach :shrug:
Logged
Pages: 1 ... 5 6 [7]