Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
6.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "tf_notification.h"
  9. #include "gcsdk/enumutils.h"
  10. #include "schemainitutils.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #ifdef GC
  13. IMPLEMENT_CLASS_MEMPOOL( CTFNotification, 1000, UTLMEMORYPOOL_GROW_SLOW );
  14. #endif
  15. #include "tier0/memdbgon.h"
  16. using namespace GCSDK;
  17. #ifdef GC
  18. // See LOCALIZED NOTIFICATIONS note in header
  19. static bool BIsLocalizedNotificationType( CMsgGCNotification_NotificationType eType )
  20. {
  21. switch ( eType )
  22. {
  23. case CMsgGCNotification_NotificationType_NOTIFICATION_SUPPORT_MESSAGE:
  24. return true;
  25. case CMsgGCNotification_NotificationType_NOTIFICATION_REPORTED_PLAYER_BANNED:
  26. case CMsgGCNotification_NotificationType_NOTIFICATION_CUSTOM_STRING:
  27. case CMsgGCNotification_NotificationType_NOTIFICATION_MM_BAN_DUE_TO_EXCESSIVE_REPORTS:
  28. case CMsgGCNotification_NotificationType_NOTIFICATION_REPORTED_PLAYER_WAS_BANNED:
  29. case CMsgGCNotification_NotificationType_NOTIFICATION_NUM_TYPES:
  30. return false;
  31. default:
  32. Assert( !"Unknown notification type" );
  33. return false;
  34. }
  35. }
  36. CTFNotification::CTFNotification()
  37. : m_eLocalizedToLanguage( k_Lang_None )
  38. {
  39. Obj().set_notification_id( 0 );
  40. Obj().set_account_id( 0 );
  41. Obj().set_expiration_time( 0 );
  42. Obj().set_type( CMsgGCNotification_NotificationType_NOTIFICATION_NUM_TYPES ); // Invalid
  43. Obj().set_notification_string( "" );
  44. }
  45. CTFNotification::CTFNotification( CMsgGCNotification msg, const char *pUnlocalizedString, ELanguage eLang )
  46. : m_eLocalizedToLanguage( k_Lang_None )
  47. {
  48. Obj() = msg;
  49. if ( BIsLocalizedNotificationType( Obj().type() ) )
  50. {
  51. m_strUnlocalizedString = pUnlocalizedString;
  52. BLocalize( eLang );
  53. }
  54. else
  55. {
  56. Obj().set_notification_string( pUnlocalizedString );
  57. }
  58. }
  59. bool CTFNotification::BLocalize( ELanguage eLang )
  60. {
  61. if ( !BIsLocalizedNotificationType( Obj().type() ) || eLang == m_eLocalizedToLanguage )
  62. {
  63. return false;
  64. }
  65. m_eLocalizedToLanguage = eLang;
  66. const char *pLocalized = GGCGameBase()->LocalizeToken( m_strUnlocalizedString.Get(), eLang, false );
  67. // Try english fallback
  68. const ELanguage eFallback = k_Lang_English;
  69. if ( !pLocalized && eLang != eFallback )
  70. {
  71. pLocalized = GGCGameBase()->LocalizeToken( m_strUnlocalizedString.Get(), eFallback, false );
  72. if ( pLocalized )
  73. {
  74. EmitError( SPEW_GC,
  75. "Notification has localized token \"%s\" that is not available in language %s, falling back to %s.\n",
  76. m_strUnlocalizedString.Get(), GetLanguageShortName( eLang ), GetLanguageShortName( eFallback ) );
  77. }
  78. }
  79. if ( !pLocalized )
  80. {
  81. EmitError( SPEW_GC, "Notification has localized token \"%s\" that was not found in primary language %s *or* fallback to %s.\n",
  82. m_strUnlocalizedString.Get(), GetLanguageShortName( eLang ), GetLanguageShortName( eFallback ) );
  83. pLocalized = m_strUnlocalizedString.Get();
  84. }
  85. Obj().set_notification_string( pLocalized ? pLocalized : m_strUnlocalizedString.Get() );
  86. return true;
  87. }
  88. bool CTFNotification::BYieldingAddInsertToTransaction( GCSDK::CSQLAccess & sqlAccess )
  89. {
  90. CSchNotification schNotification;
  91. WriteToRecord( &schNotification );
  92. return CSchemaSharedObjectHelper::BYieldingAddInsertToTransaction( sqlAccess, &schNotification );
  93. }
  94. bool CTFNotification::BYieldingAddWriteToTransaction( GCSDK::CSQLAccess & sqlAccess, const CUtlVector< int > &fields )
  95. {
  96. CSchNotification schNotification;
  97. WriteToRecord( &schNotification );
  98. CColumnSet csDatabaseDirty( schNotification.GetPSchema()->GetRecordInfo() );
  99. csDatabaseDirty.MakeEmpty();
  100. FOR_EACH_VEC( fields, nField )
  101. {
  102. switch ( fields[nField] )
  103. {
  104. case CMsgGCNotification::kNotificationIdFieldNumber : csDatabaseDirty.BAddColumn( CSchNotification::k_iField_ulNotificationID ); break;
  105. case CMsgGCNotification::kAccountIdFieldNumber : csDatabaseDirty.BAddColumn( CSchNotification::k_iField_unAccountID ); break;
  106. case CMsgGCNotification::kExpirationTimeFieldNumber : csDatabaseDirty.BAddColumn( CSchNotification::k_iField_RTime32Expiration ); break;
  107. case CMsgGCNotification::kTypeFieldNumber : csDatabaseDirty.BAddColumn( CSchNotification::k_iField_unNotificationType ); break;
  108. case CMsgGCNotification::kNotificationStringFieldNumber : csDatabaseDirty.BAddColumn( CSchNotification::k_iField_VarCharData ); break;
  109. default:
  110. Assert( false );
  111. }
  112. }
  113. return CSchemaSharedObjectHelper::BYieldingAddWriteToTransaction( sqlAccess, &schNotification, csDatabaseDirty );
  114. }
  115. bool CTFNotification::BYieldingAddRemoveToTransaction( GCSDK::CSQLAccess & sqlAccess )
  116. {
  117. CSchNotification schNotification;
  118. WriteToRecord( &schNotification );
  119. return CSchemaSharedObjectHelper::BYieldingAddRemoveToTransaction( sqlAccess, &schNotification );
  120. }
  121. void CTFNotification::WriteToRecord( CSchNotification *pNotification ) const
  122. {
  123. // Important: For localized notifications, the DB value is the localization key (#TF_Some_Thing), but the in-memory
  124. // shared object value is localized. See LOCALIZED NOTIFICATIONS note in header.
  125. pNotification->m_ulNotificationID = Obj().notification_id();
  126. pNotification->m_unAccountID = Obj().account_id();
  127. pNotification->m_RTime32Expiration = Obj().expiration_time();
  128. pNotification->m_unNotificationType = Obj().type();
  129. if ( BIsLocalizedNotificationType( Obj().type() ) )
  130. {
  131. WRITE_VAR_CHAR_FIELD_TRUNC( *pNotification, VarCharData, m_strUnlocalizedString.Get() );
  132. }
  133. else
  134. {
  135. WRITE_VAR_CHAR_FIELD_TRUNC( *pNotification, VarCharData, Obj().notification_string().c_str() );
  136. }
  137. }
  138. void CTFNotification::ReadFromRecord( const CSchNotification & schNotification )
  139. {
  140. // Important: For localized notifications, the DB value is the localization key (#TF_Some_Thing), but the in-memory
  141. // shared object value is localized. See LOCALIZED NOTIFICATIONS note in header.
  142. m_strUnlocalizedString.Clear();
  143. m_eLocalizedToLanguage = k_Lang_None;
  144. Obj().set_notification_id( schNotification.m_ulNotificationID );
  145. Obj().set_account_id( schNotification.m_unAccountID );
  146. Obj().set_expiration_time( schNotification.m_RTime32Expiration );
  147. Obj().set_type( ( CMsgGCNotification::NotificationType ) schNotification.m_unNotificationType );
  148. const char *pchNotificationString = READ_VAR_CHAR_FIELD( schNotification, m_VarCharData );
  149. Obj().set_notification_string( pchNotificationString );
  150. if ( BIsLocalizedNotificationType( Obj().type() ) )
  151. {
  152. m_strUnlocalizedString = pchNotificationString;
  153. }
  154. }
  155. #endif