RemoteCommandEvent_ScriptBinding.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. static void sendRemoteCommand(NetConnection *conn, S32 argc, const char **argv);
  23. ConsoleFunctionGroupBegin( Net, "Functions for use with the network; tagged strings and remote commands.");
  24. /*! @addtogroup Network Network
  25. @ingroup TorqueScriptFunctions
  26. @{
  27. */
  28. /*! Use the commandToServer function to issue a remote procedure call the server.
  29. All arguments may be in tagged or non-tagged format. See 'Remote Procedure Call Samples' below
  30. @param func The suffix of the remote procedure name to be executed on the client.
  31. @param arg1 ... argn - Optional arguments to be passed to the remote procedure.
  32. @return No return value.
  33. */
  34. ConsoleFunctionWithDocs( commandToServer, ConsoleVoid, 2, RemoteCommandEvent::MaxRemoteCommandArgs + 1, ( func, [arg1, ... , argn]? ))
  35. {
  36. NetConnection *conn = NetConnection::getConnectionToServer();
  37. if(!conn)
  38. return;
  39. sendRemoteCommand(conn, argc - 1, argv + 1);
  40. }
  41. // Send command to other server by given address
  42. // Useful when you connected to multi-servers
  43. ConsoleFunction( commandToNamedServer, void, 3, RemoteCommandEvent::MaxRemoteCommandArgs + 2, "(NetConnection server, string func, ...)"
  44. "Send a command to the server.")
  45. {
  46. NetConnection *conn;
  47. if(!Sim::findObject(argv[1], conn))
  48. return;
  49. sendRemoteCommand(conn, argc - 2, argv + 2);
  50. }
  51. /*! Use the commandToClient function to issue a remote procedure call on a client.
  52. All arguments (excluding client) may be in tagged or non-tagged format. See 'Remote Procedure Call Samples' below
  53. @param client The numeric ID of a client gameConnection.
  54. @param func The suffix of the remote procedure name to be executed on the client.
  55. @param arg1 ... argn - Optional arguments to be passed to the remote procedure.
  56. @return No return value.
  57. */
  58. ConsoleFunctionWithDocs( commandToClient, ConsoleVoid, 3, RemoteCommandEvent::MaxRemoteCommandArgs + 2, ( client, func, [arg1, ... , argn]? ))
  59. {
  60. NetConnection *conn;
  61. if(!Sim::findObject(argv[1], conn))
  62. return;
  63. sendRemoteCommand(conn, argc - 2, argv + 2);
  64. }
  65. /*! Use the removeTaggedSTring function to remove a previously tagged string from the NetStringTable.
  66. @param tag A number tag ID.
  67. @return No return value
  68. */
  69. ConsoleFunctionWithDocs(removeTaggedString, ConsoleVoid, 2, 2, ( tag ))
  70. {
  71. gNetStringTable->removeString(dAtoi(argv[1]+1), true);
  72. }
  73. /*! Use the addTaggedString function to tag a new string and add it to the NetStringTable.
  74. @param string The string to tagged and placed in the NetStringTable. Tagging ignores case, so tagging the same string (excluding case differences) will be ignored as a duplicated tag.
  75. @return Returns a string (containing a numeric value) equivalent to the string ID for the newly tagged string
  76. */
  77. ConsoleFunctionWithDocs( addTaggedString, ConsoleString, 2, 2, ( string ))
  78. {
  79. NetStringHandle s(argv[1]);
  80. gNetStringTable->incStringRefScript(s.getIndex());
  81. char *ret = Con::getReturnBuffer(10);
  82. ret[0] = StringTagPrefixByte;
  83. dSprintf(ret + 1, 9, "%d", s.getIndex());
  84. return ret;
  85. }
  86. /*! Use the getTaggedString function to convert a tag to a string. This is not the same a detag() which can only be used within the context of a function that receives a tag. This function can be used any time and anywhere to convert a tag to a string.
  87. @param tag A numeric tag ID.
  88. @return Returns the string corresponding to the tag ID
  89. */
  90. ConsoleFunctionWithDocs( getTaggedString, ConsoleString, 2, 2, ( tag ))
  91. {
  92. const char *indexPtr = argv[1];
  93. if (*indexPtr == StringTagPrefixByte)
  94. indexPtr++;
  95. return gNetStringTable->lookupString(dAtoi(indexPtr));
  96. }
  97. /*! Use the buildTaggedString function to build a tagged string using the specified format.
  98. @param enable A boolean value. If set to true, network packet logging is enabled, otherwise it is disabled.
  99. @return No return value
  100. */
  101. ConsoleFunctionWithDocs( buildTaggedString, ConsoleString, 2, 11, ( format, [arg1, ...arg9]? ))
  102. {
  103. const char *indexPtr = argv[1];
  104. if (*indexPtr == StringTagPrefixByte)
  105. indexPtr++;
  106. const char *fmtString = gNetStringTable->lookupString(dAtoi(indexPtr));
  107. char *strBuffer = Con::getReturnBuffer(512);
  108. const char *fmtStrPtr = fmtString;
  109. char *strBufPtr = strBuffer;
  110. S32 strMaxLength = 511;
  111. if (!fmtString)
  112. goto done;
  113. //build the string
  114. while (*fmtStrPtr)
  115. {
  116. //look for an argument tag
  117. if (*fmtStrPtr == '%')
  118. {
  119. if (fmtStrPtr[1] >= '1' && fmtStrPtr[1] <= '9')
  120. {
  121. S32 argIndex = S32(fmtStrPtr[1] - '0') + 1;
  122. if (argIndex >= argc)
  123. goto done;
  124. const char *argStr = argv[argIndex];
  125. if (!argStr)
  126. goto done;
  127. S32 strLength = dStrlen(argStr);
  128. if (strLength > strMaxLength)
  129. goto done;
  130. dStrcpy(strBufPtr, argStr);
  131. strBufPtr += strLength;
  132. strMaxLength -= strLength;
  133. fmtStrPtr += 2;
  134. continue;
  135. }
  136. }
  137. //if we don't continue, just copy the character
  138. if (strMaxLength <= 0)
  139. goto done;
  140. *strBufPtr++ = *fmtStrPtr++;
  141. strMaxLength--;
  142. }
  143. done:
  144. *strBufPtr = '\0';
  145. return strBuffer;
  146. }
  147. ConsoleFunctionGroupEnd( Net );
  148. /*! @} */ // group NetworkFunctions