Ticket #2951: template_subsets_fix.diff

File template_subsets_fix.diff, 6.9 KB (added by leper, 8 years ago)

Incremental patch on top of the earlier one. Fixes issues found in the review.

  • binaries/data/mods/public/simulation/templates/special_filter/foundation.xml

    From 838c57f3a17b1befc246b577bd1b174ce3823c2a Mon Sep 17 00:00:00 2001
    From: leper <leper@wildfiregames.com>
    Date: Mon, 11 Apr 2016 03:45:15 +0200
    Subject: [PATCH] Fix some issues.
    
     * Do not specify Health twice for foundations.
     * Change merging behaviour slightly to fix the Identity case.
       Previously using merge="" would overwrite the value of that tag with the new value,
       this would lead to all elements below Identity having empty/undef values, causing
       the GUI to complain about the specific name not being defined.
     * Only keep elements that are actually defined in the parent if we are filtering. (merge fix #2)
     * Remove unused function.
     * Add tests for some of the above issues.
       TODO: Check if some of the tests are (partially) duplicated, and possibly fix the tags of the last test.
    ---
     .../templates/special_filter/foundation.xml        |  9 ++++-----
     source/simulation2/system/ParamNode.cpp            | 22 +++++-----------------
     source/simulation2/system/ParamNode.h              |  8 --------
     source/simulation2/tests/test_ParamNode.h          | 13 +++++++++++++
     4 files changed, 22 insertions(+), 30 deletions(-)
    
    diff --git a/binaries/data/mods/public/simulation/templates/special_filter/foundation.xml b/binaries/data/mods/public/simulation/templates/special_filter/foundation.xml
    index e294934..633ccdc 100644
    a b  
    88    <PopulationBonus>0</PopulationBonus>
    99  </Cost>
    1010  <Decay merge=""/>
    11   <!-- Initialise health to 1 -->
    12   <Health>
    13     <Initial>1</Initial>
    14   </Health>
    1511  <Fogging merge=""/>
    1612  <Footprint merge=""/>
    1713  <!-- Add the Foundation component, to deal with the construction process -->
    1814  <Foundation replace=""/>
    19   <Health merge=""/>
     15  <!-- Initialise health to 1 -->
     16  <Health>
     17    <Initial>1</Initial>
     18  </Health>
    2019  <Identity merge=""/>
    2120  <!-- Foundations shouldn't initially block unit movement -->
    2221  <Obstruction merge="">
  • source/simulation2/system/ParamNode.cpp

    diff --git a/source/simulation2/system/ParamNode.cpp b/source/simulation2/system/ParamNode.cpp
    index 74ca203..e41df76 100644
    a b void CParamNode::ApplyLayer(const XMBFile& xmb, const XMBElement& element, const  
    8787    } op = INVALID;
    8888    bool replacing = false;
    8989    bool filtering = false;
     90    bool merging = false;
    9091    {
    9192        XERO_ITER_ATTR(element, attr)
    9293        {
    void CParamNode::ApplyLayer(const XMBFile& xmb, const XMBElement& element, const  
    108109            {
    109110                if (m_Childs.find(name) == m_Childs.end())
    110111                    return;
     112                merging = true;
    111113            }
    112114            else if (attr.Name == at_op)
    113115            {
    void CParamNode::ApplyLayer(const XMBFile& xmb, const XMBElement& element, const  
    183185        }
    184186        hasSetValue = true;
    185187    }
    186     if (!hasSetValue)
     188    if (!hasSetValue && !merging)
    187189        node.m_Value = value;
    188190
    189191    // Recurse through the element's children
    void CParamNode::ApplyLayer(const XMBFile& xmb, const XMBElement& element, const  
    193195        if (filtering)
    194196        {
    195197            std::string childname = xmb.GetElementString(child.GetNodeName()); // TODO: is GetElementString inefficient? (especially if we call it twice)
    196             childs[childname] = std::move(node.m_Childs[childname]);
     198            if (node.m_Childs.find(childname) != node.m_Childs.end())
     199                childs[childname] = std::move(node.m_Childs[childname]);
    197200        }
    198201    }
    199202
    void CParamNode::ApplyLayer(const XMBFile& xmb, const XMBElement& element, const  
    212215    }
    213216}
    214217
    215 void CParamNode::CopyFilteredChildrenOfChild(const CParamNode& src, const char* name, const std::set<std::string>& permitted)
    216 {
    217     ResetScriptVal();
    218 
    219     ChildrenMap::iterator dstChild = m_Childs.find(name);
    220     ChildrenMap::const_iterator srcChild = src.m_Childs.find(name);
    221     if (dstChild == m_Childs.end() || srcChild == src.m_Childs.end())
    222         return; // error
    223 
    224     ChildrenMap::const_iterator it = srcChild->second.m_Childs.begin();
    225     for (; it != srcChild->second.m_Childs.end(); ++it)
    226         if (permitted.count(it->first))
    227             dstChild->second.m_Childs[it->first] = it->second;
    228 }
    229 
    230218const CParamNode& CParamNode::GetChild(const char* name) const
    231219{
    232220    ChildrenMap::const_iterator it = m_Childs.find(name);
  • source/simulation2/system/ParamNode.h

    diff --git a/source/simulation2/system/ParamNode.h b/source/simulation2/system/ParamNode.h
    index 9ce530e..0b18e5e 100644
    a b public:  
    182182    static PSRETURN LoadXMLString(CParamNode& ret, const char* xml, const wchar_t* sourceIdentifier = NULL);
    183183
    184184    /**
    185      * Finds the childs named @a name from @a src and from @a this, and copies the source child's children
    186      * which are in the @a permitted set into this node's child.
    187      * Intended for use as a filtered clone of XML files.
    188      * @a this and @a src must have childs named @a name.
    189      */
    190     void CopyFilteredChildrenOfChild(const CParamNode& src, const char* name, const std::set<std::string>& permitted);
    191 
    192     /**
    193185     * Returns the (unique) child node with the given name, or a node with IsOk() == false if there is none.
    194186     */
    195187    const CParamNode& GetChild(const char* name) const;
  • source/simulation2/tests/test_ParamNode.h

    diff --git a/source/simulation2/tests/test_ParamNode.h b/source/simulation2/tests/test_ParamNode.h
    index d329424..fc9a1ed 100644
    a b public:  
    141141        TS_ASSERT_EQUALS(CParamNode::LoadXMLString(node, "<test> <a><b/></a> <c>toberemoved</c> <d><e/></d> </test>"), PSRETURN_OK);
    142142        TS_ASSERT_EQUALS(CParamNode::LoadXMLString(node, "<test filtered=\"\"> <a/> <d><f/></d> <g/> </test>"), PSRETURN_OK);
    143143        TS_ASSERT_WSTR_EQUALS(node.ToXML(), L"<test><a><b></b></a><d><e></e><f></f></d><g></g></test>");
     144
     145        CParamNode node2;
     146        TS_ASSERT_EQUALS(CParamNode::LoadXMLString(node2, "<test> <a><b>b</b><c>c</c><d>d</d><e>e</e></a> <f/> </test>"), PSRETURN_OK);
     147        TS_ASSERT_EQUALS(CParamNode::LoadXMLString(node2, "<test filtered=\"\"> <a filtered=\"\"><b merge=\"\"/><c>c2</c><d/></a> </test>"), PSRETURN_OK);
     148        TS_ASSERT_WSTR_EQUALS(node2.ToXML(), L"<test><a><b>b</b><c>c2</c><d></d></a></test>");
    144149    }
    145150
    146151    void test_overlay_merge()
    public:  
    151156        TS_ASSERT_WSTR_EQUALS(node.ToXML(), L"<test><a><b>test</b><c>bar</c><d>baz</d></a><x><w>more text</w><y><v>text</v><z>foo</z></y></x></test>");
    152157    }
    153158
     159    void test_overlay_filtered_merge()
     160    {
     161        CParamNode node;
     162        TS_ASSERT_EQUALS(CParamNode::LoadXMLString(node, "<test> <a><b/></a> <c><x/></c> <Health><Max>1200</Max></Health> </test>"), PSRETURN_OK);
     163        TS_ASSERT_EQUALS(CParamNode::LoadXMLString(node, "<test filtered=\"\"> <c merge=\"\"/> <d>bar</d> <e merge=\"\"/> <Health><Initial>1</Initial></Health> </test>"), PSRETURN_OK);
     164        TS_ASSERT_WSTR_EQUALS(node.ToXML(), L"<test><Health><Initial>1</Initial><Max>1200</Max></Health><c><x></x></c><d>bar</d></test>");
     165    }
     166
    154167    void test_types()
    155168    {
    156169        CParamNode node;