@@ -885,9 +885,8 @@ class IScriptVM
885
885
// --------------------------------------------------------
886
886
// Hooks
887
887
// --------------------------------------------------------
888
- virtual bool ScopeIsHooked ( HSCRIPT hScope, const char *pszEventName ) = 0;
889
- virtual HSCRIPT LookupHookFunction ( const char *pszEventName, HSCRIPT hScope, bool &bLegacy ) = 0;
890
- virtual ScriptStatus_t ExecuteHookFunction ( const char *pszEventName, HSCRIPT hFunction, ScriptVariant_t *pArgs, int nArgs, ScriptVariant_t *pReturn, HSCRIPT hScope, bool bWait ) = 0;
888
+ virtual HSCRIPT LookupHookFunction ( const char *pszEventName, HSCRIPT hScope ) = 0;
889
+ virtual ScriptStatus_t ExecuteHookFunction ( HSCRIPT hFunction, const char *pszEventName, ScriptVariant_t *pArgs, int nArgs, ScriptVariant_t *pReturn, HSCRIPT hScope, bool bWait ) = 0;
891
890
#endif
892
891
893
892
// --------------------------------------------------------
@@ -1593,17 +1592,6 @@ typedef CScriptScopeT<> CScriptScope;
1593
1592
//
1594
1593
// This was previously done with raw function lookups, but Mapbase adds more and
1595
1594
// it's hard to keep track of them without proper standards or documentation.
1596
- //
1597
- // At the moment, this simply plugs hook documentation into VScript and maintains
1598
- // the same function lookup method on the inside, but it's intended to be open for
1599
- // more complex hook mechanisms with proper parameters in the future.
1600
- //
1601
- // For example:
1602
- //
1603
- // if (m_hFunc)
1604
- // {
1605
- // g_pScriptVM->ExecuteFunction( m_Func, pArgs, m_desc.m_Parameters.Count(), pReturn, m_ScriptScope, true );
1606
- // }
1607
1595
// -----------------------------------------------------------------------------
1608
1596
struct ScriptHook_t
1609
1597
{
@@ -1620,71 +1608,92 @@ struct ScriptHook_t
1620
1608
1621
1609
// -----------------------------------------------------------------
1622
1610
1623
- // Cached for when CanRunInScope() is called before Call()
1611
+ // Only valid between CanRunInScope() and Call()
1624
1612
HSCRIPT m_hFunc;
1625
1613
bool m_bLegacy;
1626
1614
1627
- // Checks if there's a function of this name which would run in this scope
1628
- HSCRIPT CanRunInScope ( HSCRIPT hScope )
1615
+ ScriptHook_t () :
1616
+ m_bLegacy (false ),
1617
+ m_hFunc (NULL )
1629
1618
{
1630
- extern IScriptVM *g_pScriptVM;
1631
- m_hFunc = g_pScriptVM->LookupHookFunction ( m_desc.m_pszScriptName , hScope, m_bLegacy );
1632
- return m_hFunc;
1633
1619
}
1634
1620
1635
- // Checks if an existing func can be used
1636
- bool CheckFuncValid ( HSCRIPT hFunc )
1621
+ #ifdef _DEBUG
1622
+ //
1623
+ // An uninitialised script scope will pass as null scope which is considered a valid hook scope (global hook)
1624
+ // This should catch CanRunInScope() calls without CScriptScope::IsInitalised() checks first.
1625
+ //
1626
+ bool CanRunInScope ( CScriptScope &hScope )
1637
1627
{
1638
- // TODO: Better crtieria for this?
1639
- if (hFunc)
1628
+ Assert ( hScope.IsInitialized () );
1629
+ return hScope.IsInitialized () && CanRunInScope ( (HSCRIPT)hScope );
1630
+ }
1631
+ #endif
1632
+
1633
+ // Checks if there's a function of this name which would run in this scope
1634
+ bool CanRunInScope ( HSCRIPT hScope )
1635
+ {
1636
+ extern IScriptVM *g_pScriptVM;
1637
+
1638
+ // Check the hook system first to make sure an unintended function in the script scope does not cancel out all script hooks.
1639
+ m_hFunc = g_pScriptVM->LookupHookFunction ( m_desc.m_pszScriptName , hScope );
1640
+ if ( !m_hFunc )
1640
1641
{
1641
- m_hFunc = hFunc;
1642
- return true ;
1642
+ // Legacy support if the new system is not being used
1643
+ m_hFunc = g_pScriptVM->LookupFunction ( m_desc.m_pszScriptName , hScope );
1644
+ m_bLegacy = true ;
1643
1645
}
1644
- return false ;
1646
+ else
1647
+ {
1648
+ m_bLegacy = false ;
1649
+ }
1650
+
1651
+ return !!m_hFunc;
1645
1652
}
1646
1653
1647
1654
// Call the function
1655
+ // NOTE: `bRelease` only exists for weapon_custom_scripted legacy script func caching
1648
1656
bool Call ( HSCRIPT hScope, ScriptVariant_t *pReturn, ScriptVariant_t *pArgs, bool bRelease = true )
1649
1657
{
1650
1658
extern IScriptVM *g_pScriptVM;
1651
1659
1652
- // Make sure we have a function in this scope
1653
- if (!m_hFunc && ! CanRunInScope (hScope))
1654
- return false ;
1660
+ // Call() should not be called without CanRunInScope() check first, it caches m_hFunc for legacy support
1661
+ Assert ( CanRunInScope ( hScope ) );
1662
+
1655
1663
// Legacy
1656
- else if (m_bLegacy)
1664
+ if ( m_bLegacy )
1657
1665
{
1666
+ Assert ( m_hFunc );
1667
+
1658
1668
for (int i = 0 ; i < m_desc.m_Parameters .Count (); i++)
1659
1669
{
1660
1670
g_pScriptVM->SetValue ( m_pszParameterNames[i], pArgs[i] );
1661
1671
}
1662
1672
1663
- g_pScriptVM->ExecuteFunction ( m_hFunc, NULL , 0 , pReturn, hScope, true );
1673
+ ScriptStatus_t status = g_pScriptVM->ExecuteFunction ( m_hFunc, NULL , 0 , pReturn, hScope, true );
1664
1674
1665
- if (bRelease)
1675
+ if ( bRelease )
1666
1676
g_pScriptVM->ReleaseFunction ( m_hFunc );
1667
-
1668
1677
m_hFunc = NULL ;
1669
1678
1670
1679
for (int i = 0 ; i < m_desc.m_Parameters .Count (); i++)
1671
1680
{
1672
1681
g_pScriptVM->ClearValue ( m_pszParameterNames[i] );
1673
1682
}
1674
1683
1675
- return true ;
1684
+ return status == SCRIPT_DONE ;
1676
1685
}
1677
1686
// New Hook System
1678
1687
else
1679
1688
{
1680
- g_pScriptVM->ExecuteHookFunction ( m_desc.m_pszScriptName , m_hFunc, pArgs, m_desc.m_Parameters .Count (), pReturn, hScope, true );
1681
- if (bRelease)
1689
+ ScriptStatus_t status = g_pScriptVM->ExecuteHookFunction ( m_hFunc, m_desc.m_pszScriptName , pArgs, m_desc.m_Parameters .Count (), pReturn, hScope, true );
1690
+
1691
+ if ( bRelease )
1682
1692
g_pScriptVM->ReleaseFunction ( m_hFunc );
1683
1693
m_hFunc = NULL ;
1684
- return true ;
1685
- }
1686
1694
1687
- return false ;
1695
+ return status == SCRIPT_DONE;
1696
+ }
1688
1697
}
1689
1698
};
1690
1699
#endif
0 commit comments