RemoteCommandEvent_ScriptBinding.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. /*! Use the commandToClient function to issue a remote procedure call on a client.
  42. All arguments (excluding client) may be in tagged or non-tagged format. See 'Remote Procedure Call Samples' below
  43. @param client The numeric ID of a client gameConnection.
  44. @param func The suffix of the remote procedure name to be executed on the client.
  45. @param arg1 ... argn - Optional arguments to be passed to the remote procedure.
  46. @return No return value.
  47. */
  48. ConsoleFunctionWithDocs( commandToClient, ConsoleVoid, 3, RemoteCommandEvent::MaxRemoteCommandArgs + 2, ( client, func, [arg1, ... , argn]? ))
  49. {
  50. NetConnection *conn;
  51. if(!Sim::findObject(argv[1], conn))
  52. return;
  53. sendRemoteCommand(conn, argc - 2, argv + 2);
  54. }
  55. /*! Use the removeTaggedSTring function to remove a previously tagged string from the NetStringTable.
  56. @param tag A number tag ID.
  57. @return No return value
  58. */
  59. ConsoleFunctionWithDocs(removeTaggedString, ConsoleVoid, 2, 2, ( tag ))
  60. {
  61. gNetStringTable->removeString(dAtoi(argv[1]+1), true);
  62. }
  63. /*! Use the addTaggedString function to tag a new string and add it to the NetStringTable.
  64. @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.
  65. @return Returns a string (containing a numeric value) equivalent to the string ID for the newly tagged string
  66. */
  67. ConsoleFunctionWithDocs( addTaggedString, ConsoleString, 2, 2, ( string ))
  68. {
  69. NetStringHandle s(argv[1]);
  70. gNetStringTable->incStringRefScript(s.getIndex());
  71. char *ret = Con::getReturnBuffer(10);
  72. ret[0] = StringTagPrefixByte;
  73. dSprintf(ret + 1, 9, "%d", s.getIndex());
  74. return ret;
  75. }
  76. /*! 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.
  77. @param tag A numeric tag ID.
  78. @return Returns the string corresponding to the tag ID
  79. */
  80. ConsoleFunctionWithDocs( getTaggedString, ConsoleString, 2, 2, ( tag ))
  81. {
  82. const char *indexPtr = argv[1];
  83. if (*indexPtr == StringTagPrefixByte)
  84. indexPtr++;
  85. return gNetStringTable->lookupString(dAtoi(indexPtr));
  86. }
  87. /*! Use the buildTaggedString function to build a tagged string using the specified format.
  88. @param enable A boolean value. If set to true, network packet logging is enabled, otherwise it is disabled.
  89. @return No return value
  90. */
  91. ConsoleFunctionWithDocs( buildTaggedString, ConsoleString, 2, 11, ( format, [arg1, ...arg9]? ))
  92. {
  93. const char *indexPtr = argv[1];
  94. if (*indexPtr == StringTagPrefixByte)
  95. indexPtr++;
  96. const char *fmtString = gNetStringTable->lookupString(dAtoi(indexPtr));
  97. char *strBuffer = Con::getReturnBuffer(512);
  98. const char *fmtStrPtr = fmtString;
  99. char *strBufPtr = strBuffer;
  100. S32 strMaxLength = 511;
  101. if (!fmtString)
  102. goto done;
  103. //build the string
  104. while (*fmtStrPtr)
  105. {
  106. //look for an argument tag
  107. if (*fmtStrPtr == '%')
  108. {
  109. if (fmtStrPtr[1] >= '1' && fmtStrPtr[1] <= '9')
  110. {
  111. S32 argIndex = S32(fmtStrPtr[1] - '0') + 1;
  112. if (argIndex >= argc)
  113. goto done;
  114. const char *argStr = argv[argIndex];
  115. if (!argStr)
  116. goto done;
  117. S32 strLength = dStrlen(argStr);
  118. if (strLength > strMaxLength)
  119. goto done;
  120. dStrcpy(strBufPtr, argStr);
  121. strBufPtr += strLength;
  122. strMaxLength -= strLength;
  123. fmtStrPtr += 2;
  124. continue;
  125. }
  126. }
  127. //if we don't continue, just copy the character
  128. if (strMaxLength <= 0)
  129. goto done;
  130. *strBufPtr++ = *fmtStrPtr++;
  131. strMaxLength--;
  132. }
  133. done:
  134. *strBufPtr = '\0';
  135. return strBuffer;
  136. }
  137. ConsoleFunctionGroupEnd( Net );
  138. /*! @} */ // group NetworkFunctions