Editing Modding Tutorials/PatchOperations

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
{{DISPLAYTITLE:PatchOperations}}
 
 
{{BackToTutorials}}
 
{{BackToTutorials}}
  
Line 21: Line 20:
 
</source>
 
</source>
  
Just as with XML Defs, folder and file names do not matter and you can freely name and organize your patch files in whatever manner you wish. Individual patches themselves are a standard XML file with <tt><Patch></tt> as the root tag:
+
Just as with XML Defs, folder and file names do not matter and you can freely name and organize your patche files in whatever manner you wish. Individual patches themselves are a standard XML file with <tt><Patch></tt> as the root tag:
  
 
<source lang="xml">
 
<source lang="xml">
Line 41: Line 40:
 
* The first segment of any XPath targeting an XML Def will be <code>Defs/</code> as all XML Defs use the <code><Defs></code> root tag.
 
* The first segment of any XPath targeting an XML Def will be <code>Defs/</code> as all XML Defs use the <code><Defs></code> root tag.
 
* The square brackets denote a predicate, or conditional match. In this case, we are looking for <code>ThingDef</code>s that have a child tag <code>defName</code> equal to "Wall".
 
* The square brackets denote a predicate, or conditional match. In this case, we are looking for <code>ThingDef</code>s that have a child tag <code>defName</code> equal to "Wall".
 
=== Targeting Attributes ===
 
  
 
You can target Defs that do not have a <code>defName</code> (such as abstract bases) by targeting their identifying attributes. For example, if you wanted to add another Stuff category to the abstract base Def for all shelves, you might use the following xpath:
 
You can target Defs that do not have a <code>defName</code> (such as abstract bases) by targeting their identifying attributes. For example, if you wanted to add another Stuff category to the abstract base Def for all shelves, you might use the following xpath:
Line 48: Line 45:
 
<code>Defs/ThingDef[@Name="ShelfBase"]/stuffCategories</code>
 
<code>Defs/ThingDef[@Name="ShelfBase"]/stuffCategories</code>
  
You can use the same technique to target all Defs that inherit from a common base. For example, you could use the following xpath to target all <code>ThingDef</code>s that inherit from <code>ApparelBase</code>:
+
Additional XPath resources:
 
 
<code>Defs/ThingDef[@ParentName="ApparelBase"]</code>
 
 
 
=== Additional XPath Resources ===
 
 
 
 
* [https://developer.mozilla.org/en-US/docs/Web/XPath Mozilla Developer Network]
 
* [https://developer.mozilla.org/en-US/docs/Web/XPath Mozilla Developer Network]
 
* [https://www.w3schools.com/xml/xml_xpath.asp W3Schools XPath Tutorial]
 
* [https://www.w3schools.com/xml/xml_xpath.asp W3Schools XPath Tutorial]
Line 100: Line 92:
  
 
==PatchOperationAdd==
 
==PatchOperationAdd==
Inserts the specified <code>value</code>s as a child node of the XML nodes targeted by the operation's <code>xpath</code>. By default, the new nodes will be inserted after any existing child nodes (<code>Append</code>). You can use <code><order>Prepend</order></code> in the patch operation to insert them before existing child nodes instead.
+
Inserts the specified <code>value</code>s as a child node of the XML nodes targeted by the operation's <code>xpath</code>. By default, the new nodes will be inserted after (appended to) any existing child nodes. You can use <code><order>Prepend</order></code> in the patch operation (order is Append by default).
  
 
Note: PatchOperationAdd will not overwrite any existing tags. If one of your <code>value</code>s overlaps with an existing node and you are not targeting a list node, then it will cause an error on game load.
 
Note: PatchOperationAdd will not overwrite any existing tags. If one of your <code>value</code>s overlaps with an existing node and you are not targeting a list node, then it will cause an error on game load.
Line 423: Line 415:
 
   <value>
 
   <value>
 
     <li Class="MyNamespace.MyModExtension">
 
     <li Class="MyNamespace.MyModExtension">
       <key>Value</key>
+
       <key>Value</exampleValue>
 
     </li>
 
     </li>
 
   </value>
 
   </value>
Line 436: Line 428:
 
   <modExtensions>
 
   <modExtensions>
 
     <li Class="MyNamespace.MyModExtension">
 
     <li Class="MyNamespace.MyModExtension">
       <key>Value</key>
+
       <key>Value</exampleValue>
 
     </li>
 
     </li>
 
   </modExtensions>
 
   </modExtensions>
Line 528: Line 520:
 
</source>
 
</source>
  
As mentioned, you can use [[Modding_Tutorials/MayRequire|MayRequire]] on child operations of a PatchOperationSequence, but you should test them individually before adding them to said Sequence:
+
As mentioned, you can use {{Modding_Tutorials/MayRequire}} on child operations of a PatchOperationSequence, but you should test them individually before adding them to said Sequence:
  
 
<source lang="xml">
 
<source lang="xml">
Line 534: Line 526:
 
   <operations>
 
   <operations>
 
     <li Class="PatchOperationAdd" MayRequire="Ludeon.Rimworld.Biotech"> <!-- Only runs if Biotech is active -->
 
     <li Class="PatchOperationAdd" MayRequire="Ludeon.Rimworld.Biotech"> <!-- Only runs if Biotech is active -->
       <xpath>Defs/ThingDef[defName="MechGestator"]/recipes</xpath>
+
       <xpath>Defs/ThingDef[defName="MechGestator"]/recipes<xpath>
 
       <value>
 
       <value>
 
         <li>MyCustomMech</li>
 
         <li>MyCustomMech</li>
Line 554: Line 546:
  
 
'''WARNING''': Unlike all other mod-compatibility features in RimWorld, PatchOperationFindMod uses the mod ''name'' and not its <code>packageId</code>.
 
'''WARNING''': Unlike all other mod-compatibility features in RimWorld, PatchOperationFindMod uses the mod ''name'' and not its <code>packageId</code>.
 
'''NOTE''': For expansions, PatchOperationFindMod uses the <code>label</code> value defined in the <code>ExpansionDef</code> for the DLC.  At present, this means they are identified by just their single word names: Royalty, Ideology, Biotech, Anomaly.
 
 
'''NOTE''': PatchOperationFindMod should only be used if you are implementing ''optional'' compatibility for the target mod, i.e. if your mod can work with or without the mod in question. If you are creating a compatibility mod specifically for a target mod that would be meaningless without that mod present, then it's better to simply specify that mod in your About.xml as a dependency and forgo the use of PatchOperationFindMod and avoid the potential issues introduced by using PatchOperationSequence.
 
  
 
The following example adds a mod extension to the targeted Defs if RimQuest is loaded ([https://github.com/Mehni/MoreFactionInteraction/blob/ab3ab2a22ee529ee4e38a471fe150fd48fd07ce9/Patches/MoreFactionInteraction.xml#L64 See original]):
 
The following example adds a mod extension to the targeted Defs if RimQuest is loaded ([https://github.com/Mehni/MoreFactionInteraction/blob/ab3ab2a22ee529ee4e38a471fe150fd48fd07ce9/Patches/MoreFactionInteraction.xml#L64 See original]):
Line 661: Line 649:
  
 
A tutorial for this process does not exist yet, but you can check out the following references of custom PatchOperations:
 
A tutorial for this process does not exist yet, but you can check out the following references of custom PatchOperations:
* [https://github.com/15adhami/XmlExtensions XML Extensions] - An entire framework mod containing many useful custom PatchOperations.
+
9 [https://github.com/15adhami/XmlExtensions XML Extensions] - An entire framework mod containing many useful custom PatchOperations.
* [https://github.com/CombatExtended-Continued/CombatExtended/blob/Development/Source/CombatExtended/CombatExtended/PatchOperationMakeGunCECompatible.cs PatchOperationMakeGunCECompatible] - Used by Combat Extended to automatically apply multiple changes to a single firearm for compatibility.
+
- [https://github.com/CombatExtended-Continued/CombatExtended/blob/Development/Source/CombatExtended/CombatExtended/PatchOperationMakeGunCECompatible.cs PatchOperationMakeGunCECompatible] - Used by Combat Extended to automatically apply multiple changes to a single firearm for compatibility.
* [https://gist.github.com/Lanilor/e326e33e268e78f68a2f5cd3cdbbe8c0 PatchOperationAddOrReplace] - An example of a custom variant of PatchOperationAdd that replaces an existing value.</br>
+
- [https://gist.github.com/Lanilor/e326e33e268e78f68a2f5cd3cdbbe8c0 PatchOperationAddOrReplace] - An example of a custom variant of PatchOperationAdd that replaces an existing value.</br>
  
 
== "Success" Option ==
 
== "Success" Option ==

Please note that all contributions to RimWorld Wiki are considered to be released under the CC BY-SA 3.0 (see RimWorld Wiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)