RemoteCommandEvent_ScriptBinding_o.h 7.1 KB

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