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.

228 lines
9.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Implements an interface to the map editor for the execution of
  4. // editor shell commands from another application. Commands allow the
  5. // creation and deletion of entities, AI nodes, and AI node connections.
  6. //
  7. // $NoKeywords: $
  8. //=============================================================================//
  9. #if !defined(_STATIC_LINKED) || defined(_SHARED_LIB)
  10. #if !defined(_X360) && defined(_WIN32)
  11. #include <windows.h>
  12. #endif
  13. #include <stdio.h>
  14. #include "editor_sendcommand.h"
  15. #include "tier1/strtools.h"
  16. #include "mathlib/vector.h"
  17. #if defined( _X360 )
  18. #include "xbox/xbox_win32stubs.h"
  19. #endif
  20. // memdbgon must be the last include file in a .cpp file!!!
  21. #include "tier0/memdbgon.h"
  22. const int MAX_COMMAND_BUFFER = 2048;
  23. //-----------------------------------------------------------------------------
  24. // Purpose: Sends a command to the editor (if running) to begin an editing session
  25. // of a given map.
  26. // Input : pszMapName - Name of the bsp, without the path or extension: "c1a3a_port"
  27. // nMapVersion - Map version number, from the BSP file.
  28. // Output : Returns Editor_OK on success, an error code if the session was rejected.
  29. //-----------------------------------------------------------------------------
  30. EditorSendResult_t Editor_BeginSession(const char *pszMapName, int nMapVersion, bool bShowUI)
  31. {
  32. char szCommand[MAX_COMMAND_BUFFER];
  33. Q_snprintf(szCommand,sizeof(szCommand), "session_begin %s %d", pszMapName, nMapVersion);
  34. return(Editor_SendCommand(szCommand, bShowUI));
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Purpose: Sends a command to the editor (if running) to verify the version and
  38. // map name being edited.
  39. // Input : pszMapName - Name of the bsp, without the path or extension: "c1a3a_port"
  40. // nMapVersion - Map version number, from the BSP file.
  41. // Output : Returns Editor_OK on success, an error code if the session was rejected.
  42. //-----------------------------------------------------------------------------
  43. EditorSendResult_t Editor_CheckVersion(const char *pszMapName, int nMapVersion, bool bShowUI)
  44. {
  45. char szCommand[MAX_COMMAND_BUFFER];
  46. Q_snprintf(szCommand,sizeof(szCommand), "map_check_version %s %d", pszMapName, nMapVersion);
  47. return(Editor_SendCommand(szCommand, bShowUI));
  48. }
  49. //-----------------------------------------------------------------------------
  50. // Purpose: Sends a command to the editor (if running) to create an entity at
  51. // a given (x, y, z) coordinate.
  52. // Input : pszEntity - Class name of entity to create, ie "info_player_start".
  53. // x, y, z - World coordinates at which to create entity.
  54. // Output : Returns Editor_OK on success, an error code on failure.
  55. //-----------------------------------------------------------------------------
  56. EditorSendResult_t Editor_CreateEntity(const char *pszEntity, float x, float y, float z, bool bShowUI)
  57. {
  58. char szCommand[MAX_COMMAND_BUFFER];
  59. Q_snprintf(szCommand,sizeof(szCommand), "entity_create %s %g %g %g", pszEntity, x, y, z);
  60. return(Editor_SendCommand(szCommand, bShowUI));
  61. }
  62. //-----------------------------------------------------------------------------
  63. // Purpose: Sends a command to the editor (if running) to create an entity at
  64. // a given (x, y, z) coordinate.
  65. // Input : pszNodeClass - Class name of node to create, ie "info_node".
  66. // nID - Unique ID to assign the node.
  67. // x, y, z - World coordinates at which to create node.
  68. // Output : Returns Editor_OK on success, an error code on failure.
  69. //-----------------------------------------------------------------------------
  70. EditorSendResult_t Editor_CreateNode(const char *pszNodeClass, int nID, float x, float y, float z, bool bShowUI)
  71. {
  72. char szCommand[MAX_COMMAND_BUFFER];
  73. Q_snprintf(szCommand,sizeof(szCommand), "node_create %s %d %g %g %g", pszNodeClass, nID, x, y, z);
  74. return(Editor_SendCommand(szCommand, bShowUI));
  75. }
  76. //-----------------------------------------------------------------------------
  77. // Purpose: Sends a command to the editor (if running) to create an entity at
  78. // a given (x, y, z) coordinate.
  79. // Input : pszNodeClass - Class name of node to create, ie "info_node".
  80. // nID - Unique ID to assign the node.
  81. // x, y, z - World coordinates at which to create node.
  82. // Output : Returns Editor_OK on success, an error code on failure.
  83. //-----------------------------------------------------------------------------
  84. EditorSendResult_t Editor_CreateNodeLink(int nStartID, int nEndID, bool bShowUI)
  85. {
  86. char szCommand[MAX_COMMAND_BUFFER];
  87. Q_snprintf(szCommand,sizeof(szCommand), "nodelink_create %d %d", nStartID, nEndID);
  88. return(Editor_SendCommand(szCommand, bShowUI));
  89. }
  90. //-----------------------------------------------------------------------------
  91. // Purpose: Sends a command to the editor (if running) to delete an entity at
  92. // a given (x, y, z) coordinate.
  93. // Input : pszEntity - Class name of entity to delete, ie "info_player_start".
  94. // x, y, z - World coordinates of entity to delete.
  95. // Output : Returns Editor_OK on success, an error code on failure.
  96. //-----------------------------------------------------------------------------
  97. EditorSendResult_t Editor_DeleteEntity(const char *pszEntity, float x, float y, float z, bool bShowUI)
  98. {
  99. char szCommand[MAX_COMMAND_BUFFER];
  100. Q_snprintf(szCommand,sizeof(szCommand), "entity_delete %s %g %g %g", pszEntity, x, y, z);
  101. return(Editor_SendCommand(szCommand, bShowUI));
  102. }
  103. // sets an arbitrary key/value pair in the entity
  104. EditorSendResult_t Editor_SetKeyValue(const char *pszEntity, float x, float y, float z, const char *pKey, const char *pValue, bool bShowUI)
  105. {
  106. char szCommand[MAX_COMMAND_BUFFER];
  107. Q_snprintf(szCommand,sizeof(szCommand), "entity_set_keyvalue %s %f %f %f \"%s\" \"%s\"", pszEntity, x, y, z, pKey, pValue);
  108. return(Editor_SendCommand(szCommand, bShowUI));
  109. }
  110. // applies an incremental rotation to an entity
  111. EditorSendResult_t Editor_RotateEntity(const char *pszEntity, float x, float y, float z, const QAngle &incrementalRotation, bool bShowUI)
  112. {
  113. char szCommand[MAX_COMMAND_BUFFER];
  114. Q_snprintf(szCommand,sizeof(szCommand), "entity_rotate_incremental %s %f %f %f %f %f %f", pszEntity, x, y, z, incrementalRotation.x, incrementalRotation.y, incrementalRotation.z );
  115. return(Editor_SendCommand(szCommand, bShowUI));
  116. }
  117. //-----------------------------------------------------------------------------
  118. // Purpose: Sends a command to the editor (if running) to delete an entity at
  119. // a given (x, y, z) coordinate.
  120. // Input : nID - unique ID of node to delete.
  121. // Output : Returns Editor_OK on success, an error code on failure.
  122. //-----------------------------------------------------------------------------
  123. EditorSendResult_t Editor_DeleteNode(int nID, bool bShowUI)
  124. {
  125. char szCommand[MAX_COMMAND_BUFFER];
  126. Q_snprintf(szCommand,sizeof(szCommand), "node_delete %d", nID);
  127. return(Editor_SendCommand(szCommand, bShowUI));
  128. }
  129. //-----------------------------------------------------------------------------
  130. // Purpose: Sends a command to the editor (if running) to delete an entity at
  131. // a given (x, y, z) coordinate.
  132. // Input : nStartID - unique ID of one node that the link is connected to.
  133. // nEndID - unique ID of the other node that the link is connected to.
  134. // Output : Returns Editor_OK on success, an error code on failure.
  135. //-----------------------------------------------------------------------------
  136. EditorSendResult_t Editor_DeleteNodeLink(int nStartID, int nEndID, bool bShowUI)
  137. {
  138. char szCommand[MAX_COMMAND_BUFFER];
  139. Q_snprintf(szCommand,sizeof(szCommand), "nodelink_delete %d %d", nStartID, nEndID);
  140. return(Editor_SendCommand(szCommand, bShowUI));
  141. }
  142. //-----------------------------------------------------------------------------
  143. // Purpose: Sends a command to the editor (if running) to end the current remote
  144. // editing session.
  145. // Output : Returns Editor_OK on success, an error code if the session was rejected.
  146. //-----------------------------------------------------------------------------
  147. EditorSendResult_t Editor_EndSession(bool bShowUI)
  148. {
  149. return(Editor_SendCommand("session_end", bShowUI));
  150. }
  151. //-----------------------------------------------------------------------------
  152. // Purpose: Attempts to sends a shell command to the editor.
  153. // Input : pszCommand - Shell command to send.
  154. // bShowUI - Whether to display mesage boxes on failure.
  155. // Output : Returns one of the following values:
  156. // Editor_OK - The command was executed successfully.
  157. // Editor_NotRunning - Unable to establish a communications channel with the editor.
  158. // Editor_BadCommand - The editor did not accept the command.
  159. //-----------------------------------------------------------------------------
  160. EditorSendResult_t Editor_SendCommand(const char *pszCommand, bool bShowUI)
  161. {
  162. #ifdef _WIN32
  163. HWND hwnd = FindWindow("Worldcraft_ShellMessageWnd", "Worldcraft_ShellMessageWnd");
  164. if (hwnd != NULL)
  165. {
  166. //
  167. // Fill out the data structure to send to the editor.
  168. //
  169. COPYDATASTRUCT CopyData;
  170. CopyData.cbData = strlen(pszCommand) + 1;
  171. CopyData.dwData = 0;
  172. CopyData.lpData = (void *)pszCommand;
  173. if (!SendMessage(hwnd, WM_COPYDATA, 0, (LPARAM)&CopyData))
  174. {
  175. if (bShowUI)
  176. {
  177. char szError[1024];
  178. Q_snprintf(szError,sizeof(szError), "Worldcraft did not accept the command: \n\n\"%s\"\n\n Make sure the command is valid and that Worldcraft is still running properly.", pszCommand);
  179. MessageBox(NULL, szError, "Editor_SendCommand Error", MB_OK);
  180. }
  181. return(Editor_BadCommand);
  182. }
  183. }
  184. else
  185. {
  186. if (bShowUI)
  187. {
  188. char szError[1024];
  189. Q_snprintf(szError,sizeof(szError), "Could not contact Worldcraft to send the command: \n\n\"%s\"\n\n Worldcraft does not appear to be running.", pszCommand);
  190. MessageBox(NULL, szError, "Editor_SendCommand Error", MB_OK);
  191. }
  192. return(Editor_NotRunning);
  193. }
  194. #endif
  195. return(Editor_OK);
  196. }
  197. #endif // !_STATIC_LINKED || _SHARED_LIB