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