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.

181 lines
5.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "gameeventeditdoc.h"
  9. #include "tier1/KeyValues.h"
  10. #include "tier1/utlbuffer.h"
  11. #include "toolutils/enginetools_int.h"
  12. #include "filesystem.h"
  13. #include "toolframework/ienginetool.h"
  14. #include "datamodel/idatamodel.h"
  15. #include "toolutils/attributeelementchoicelist.h"
  16. #include "vgui_controls/messagebox.h"
  17. // FIXME: This document currently stores a whole lot of nothing.
  18. //-----------------------------------------------------------------------------
  19. // Constructor
  20. //-----------------------------------------------------------------------------
  21. CGameEventEditDoc::CGameEventEditDoc()
  22. {
  23. m_hRoot = NULL;
  24. m_pTXTFileName[0] = 0;
  25. m_bDirty = false;
  26. g_pDataModel->InstallNotificationCallback( this );
  27. }
  28. CGameEventEditDoc::~CGameEventEditDoc()
  29. {
  30. g_pDataModel->RemoveNotificationCallback( this );
  31. }
  32. //-----------------------------------------------------------------------------
  33. // Inherited from INotifyUI
  34. //-----------------------------------------------------------------------------
  35. void CGameEventEditDoc::NotifyDataChanged( const char *pReason, int nNotifySource, int nNotifyFlags )
  36. {
  37. //OnDataChanged( pReason, nNotifySource, nNotifyFlags );
  38. }
  39. //-----------------------------------------------------------------------------
  40. // Gets the file name
  41. //-----------------------------------------------------------------------------
  42. const char *CGameEventEditDoc::GetTXTFileName()
  43. {
  44. return m_pTXTFileName;
  45. }
  46. void CGameEventEditDoc::SetTXTFileName( const char *pFileName )
  47. {
  48. Q_strncpy( m_pTXTFileName, pFileName, sizeof( m_pTXTFileName ) );
  49. Q_FixSlashes( m_pTXTFileName );
  50. SetDirty( true );
  51. }
  52. //-----------------------------------------------------------------------------
  53. // Dirty bits
  54. //-----------------------------------------------------------------------------
  55. void CGameEventEditDoc::SetDirty( bool bDirty )
  56. {
  57. m_bDirty = bDirty;
  58. }
  59. bool CGameEventEditDoc::IsDirty() const
  60. {
  61. return m_bDirty;
  62. }
  63. //-----------------------------------------------------------------------------
  64. // Saves/loads from file
  65. //-----------------------------------------------------------------------------
  66. bool CGameEventEditDoc::LoadFromFile( const char *pFileName )
  67. {
  68. /*
  69. Assert( !m_hRoot.Get() );
  70. CAppDisableUndoScopeGuard guard( "CCommEditDoc::LoadFromFile", 0 );
  71. SetDirty( false );
  72. if ( !pFileName[0] )
  73. return false;
  74. char mapname[ 256 ];
  75. // Compute the map name
  76. const char *pMaps = Q_stristr( pFileName, "\\maps\\" );
  77. if ( !pMaps )
  78. return false;
  79. // Build map name
  80. //int nNameLen = (int)( (size_t)pComm - (size_t)pMaps ) - 5;
  81. Q_StripExtension( pFileName, mapname, sizeof(mapname) );
  82. char *pszFileName = (char*)Q_UnqualifiedFileName(mapname);
  83. // Set the txt file name.
  84. // If we loaded an existing commentary file, keep the same filename.
  85. // If we loaded a .bsp, change the name & the extension.
  86. if ( !V_stricmp( Q_GetFileExtension( pFileName ), "bsp" ) )
  87. {
  88. const char *pCommentaryAppend = "_commentary.txt";
  89. Q_StripExtension( pFileName, m_pTXTFileName, sizeof(m_pTXTFileName)- strlen(pCommentaryAppend) - 1 );
  90. Q_strcat( m_pTXTFileName, pCommentaryAppend, sizeof( m_pTXTFileName ) );
  91. if ( g_pFileSystem->FileExists( m_pTXTFileName ) )
  92. {
  93. char pBuf[1024];
  94. Q_snprintf( pBuf, sizeof(pBuf), "File %s already exists!\n", m_pTXTFileName );
  95. m_pTXTFileName[0] = 0;
  96. vgui::MessageBox *pMessageBox = new vgui::MessageBox( "Unable to overwrite file!\n", pBuf, g_pCommEditTool );
  97. pMessageBox->DoModal( );
  98. return false;
  99. }
  100. DmFileId_t fileid = g_pDataModel->FindOrCreateFileId( m_pTXTFileName );
  101. m_hRoot = CreateElement<CDmElement>( "root", fileid );
  102. CDmrElementArray<> subkeys( m_hRoot->AddAttribute( "subkeys", AT_ELEMENT_ARRAY ) );
  103. CDmElement *pRoot2 = CreateElement<CDmElement>( "Entities", fileid );
  104. pRoot2->AddAttribute( "subkeys", AT_ELEMENT_ARRAY );
  105. subkeys.AddToTail( pRoot2 );
  106. g_pDataModel->SetFileRoot( fileid, m_hRoot );
  107. }
  108. else
  109. {
  110. char *pComm = Q_stristr( pszFileName, "_commentary" );
  111. if ( !pComm )
  112. {
  113. char pBuf[1024];
  114. Q_snprintf( pBuf, sizeof(pBuf), "File %s is not a commentary file!\nThe file name must end in _commentary.txt.\n", m_pTXTFileName );
  115. m_pTXTFileName[0] = 0;
  116. vgui::MessageBox *pMessageBox = new vgui::MessageBox( "Bad file name!\n", pBuf, g_pCommEditTool );
  117. pMessageBox->DoModal( );
  118. return false;
  119. }
  120. // Clip off the "_commentary" at the end of the filename
  121. *pComm = '\0';
  122. // This is not undoable
  123. CDisableUndoScopeGuard guard;
  124. CDmElement *pTXT = NULL;
  125. CElementForKeyValueCallback KeyValuesCallback;
  126. g_pDataModel->SetKeyValuesElementCallback( &KeyValuesCallback );
  127. DmFileId_t fileid = g_pDataModel->RestoreFromFile( pFileName, NULL, "keyvalues", &pTXT );
  128. g_pDataModel->SetKeyValuesElementCallback( NULL );
  129. if ( fileid == DMFILEID_INVALID )
  130. {
  131. m_pTXTFileName[0] = 0;
  132. return false;
  133. }
  134. SetTXTFileName( pFileName );
  135. m_hRoot = pTXT;
  136. }
  137. guard.Release();
  138. SetDirty( false );
  139. char cmd[ 256 ];
  140. Q_snprintf( cmd, sizeof( cmd ), "disconnect; map %s\n", pszFileName );
  141. enginetools->Command( cmd );
  142. enginetools->Execute( );*/
  143. return true;
  144. }
  145. void CGameEventEditDoc::SaveToFile( )
  146. {
  147. if ( m_hRoot.Get() && m_pTXTFileName && m_pTXTFileName[0] )
  148. {
  149. g_pDataModel->SaveToFile( m_pTXTFileName, NULL, "keyvalues", "keyvalues", m_hRoot );
  150. }
  151. SetDirty( false );
  152. }