Guest User

y_commands.inc

a guest
May 13th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 81.44 KB | None | 0 0
  1. /**--------------------------------------------------------------------------**\
  2. ================================
  3. Y Sever Includes - Commands Core
  4. ================================
  5. Description:
  6. Runs commands registered with the system and calls the required functions.
  7. Also handles alternate names and prefixes. Based very loosely on dcmd.
  8. Legal:
  9. Version: MPL 1.1
  10.  
  11. The contents of this file are subject to the Mozilla Public License Version
  12. 1.1 (the "License"); you may not use this file except in compliance with
  13. the License. You may obtain a copy of the License at
  14. http://www.mozilla.org/MPL/
  15.  
  16. Software distributed under the License is distributed on an "AS IS" basis,
  17. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  18. for the specific language governing rights and limitations under the
  19. License.
  20.  
  21. The Original Code is the YSI commands include.
  22.  
  23. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  24. Portions created by the Initial Developer are Copyright (C) 2011
  25. the Initial Developer. All Rights Reserved.
  26.  
  27. Contributors:
  28. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  29.  
  30. Thanks:
  31. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  32. ZeeX - Very productive conversations.
  33. koolk - IsPlayerinAreaEx code.
  34. TheAlpha - Danish translation.
  35. breadfish - German translation.
  36. Fireburn - Dutch translation.
  37. yom - French translation.
  38. 50p - Polish translation.
  39. Zamaroht - Spanish translation.
  40. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  41. for me to strive to better.
  42. Pixels^ - Running XScripters where the idea was born.
  43. Matite - Pestering me to release it and using it.
  44.  
  45. Very special thanks to:
  46. Thiadmer - PAWN, whose limits continue to amaze me!
  47. Kye/Kalcor - SA:MP.
  48. SA:MP Team past, present and future - SA:MP.
  49.  
  50. Version:
  51. 0.1.4
  52. Changelog:
  53. 20/10/10:
  54. Fixed a bug with insensitive commands - my fault for not testing.
  55. 06/01/08:
  56. Improved master and /help support.
  57. 04/01/08:
  58. Fixed bad element in Command_SetDeniedReturn.
  59. 12/08/07:
  60. Added master support.
  61. 24/06/07:
  62. Modifed a few functions to use Bit_GetBit for speed.
  63. 04/05/07:
  64. Completed command use support.
  65. Added invalid character protection.
  66. 02/05/07:
  67. Added YSI_ prefix to all globals.
  68. 14/04/07:
  69. Updated header documentation with more than changelog/functions.
  70. Added function name requesting.
  71. 13/04/07:
  72. Added function documentation.
  73. Added wrapped functions for e_COMM_FLAG values missing them.
  74. Added header function list.
  75. 12/04/07:
  76. Added command removal.
  77. 11/04/07:
  78. Changed system slightly to handle names and alt names separately. Still
  79. need a better way of ignoring names when alt names are used.
  80. 10/04/07:
  81. First version.
  82. Functions:
  83. Public:
  84. Command_Add - Adds a command to the array for processing.
  85. Command_Remove - Removes a command.
  86. Command_Name - Gets the name of a command in a property.
  87. Core:
  88. Command_Process - Called from OnPlayerCommandText to process entered commands.
  89. Command_Parse - Sorts added commands into a binary tree.
  90. Command_Hash - Hashes a word for command hashing.
  91. Command_ProcRem - Processes a help command in the master script.
  92. Stock:
  93. Command_SetDisconnectReturn - Sets the return value for unconnected players.
  94. Command_UseShortCuts - Toggles use of per-player command shortcuts.
  95. Command_SetDeniedReturn - Sets the return value for denied use commands.
  96. Command_UseDeniedMessage - Toggles the use of an error message for denied.
  97. Command_SetIllegalReturn - Sets the return value for illegal characters.
  98. Command_UseAltNames - Toggles the use of ini defined alternate names.
  99. Command_UsePrefix - Toggles the use of a global prefix.
  100. Command_UseSpace - Toggles the use of a space between prefix and command.
  101. Command_SetAltName - Sets the alternate name of a function.
  102. Command_SetPrefix - Sets the pfexix to be typed.
  103. Comamnd_SetPlayerUse - Sets wether or not a player can use a command.
  104. Comamnd_SetPlayerUseByID - Sets wether or not a player can use a command.
  105. Command_FindByName - Finds a command in a possibly sorted list.
  106. Static:
  107. Command_FindFast - Finds a function by binary search of function name.
  108. Command_FindAlt - Finds a function by binary search of alternate name.
  109. Command_SetSorted - Marks the binary tree as sorted.
  110. Command_FindSlow - Finds a function by linear search of function name.
  111. Command_Find - Finds a function from entered text.
  112. Command_Prefix - Checks the entered prefix.
  113. Command_ReturnName - Returns the name of a function.
  114. Command_GetPlayerCount - Gets the number of commands a player can use.
  115. Inline:
  116. Command_Command - Not used, constructor.
  117. Command_IsCleared - Checks a player is cleared to use a command.
  118. Command_DisconnectReturn - Gets the return value for disconnected players.
  119. Command_UsingShortCuts - Checks if short cuts are being used.
  120. Command_DeniedReturn - Gets the return value for prohibited commands.
  121. Command_IllegalReturn - Gets the return value for invalid characters.
  122. Command_DeniedMessage - Checks if a level error message should be sent.
  123. Command_IsSorted - Checks if the binary tree has been initialised.
  124. Command_UsingAltNames - Checks if alt names are being used.
  125. Command_UsingPrefix - Checks if the prefix is being used.
  126. Command_UsingSpace - Checks if the space option is being used.
  127. Command_CallFunction - Calls the required function.
  128. ycmd - Adds a command to the system.
  129. API:
  130. -
  131. Callbacks:
  132. -
  133. Definitions:
  134. MAX_COMMAND_LENGTH - The maximum length of a command string.
  135. COMMAND_NOT_FOUND - Indicates that a searched for string is not a function.
  136. Enums:
  137. e_COMM_FLAG - Bit mappings for command options.
  138. E_COMMANDS - Structure of the array holding the string data.
  139. Macros:
  140. Command_(%1) - Forwards and declares a standard command for calling.
  141. ycmd(%1) - Adds a command to the array (wrapper for Command_Add).
  142. Tags:
  143. e_COMM_FLAG - Flag type.
  144. Variables:
  145. Global:
  146. -
  147. Static:
  148. YSI_g_sCommands - Holds all the textual data of the commands.
  149. YSI_g_sSearchTree - Tree of hashes for function names.
  150. YSI_g_sAltTree - Tree of hashes for alternate names.
  151. YSI_g_sPrefix - The command prefix.
  152. YSI_g_sPrefixLength - Length of the prefix.
  153. YSI_g_sCommandIndex - Pointer to the next free index in the function array.
  154. YSI_g_sAltCount - The number of commands with altnames.
  155. YSI_g_sCommandFlags - Bit array of command options.
  156. Commands:
  157. commands - Lists all commands available to you.
  158. Compile options:
  159. COMMAND_SENSITIVE - Make commands case sensitive.
  160. COMMAND_ACCURATE - Can use '@' in command names.
  161. MAX_COMMANDS - The maximum number of commands which can be used.
  162. </remarks>
  163. \**--------------------------------------------------------------------------**/
  164.  
  165. #if defined _INC_y_text
  166. #error y_commands must be included before y_text
  167. #endif
  168.  
  169. #include "internal\y_version"
  170.  
  171. #if !defined MAX_COMMANDS
  172. #define MAX_COMMANDS (1024)
  173. #endif
  174.  
  175. #define _GROUP_MAKE_NAME_CMD<%0...%1> %0Command%1
  176. #define _GROUP_MAKE_LIMIT_CMD MAX_COMMANDS
  177.  
  178. #define YSIM_U_DISABLE
  179. #include "y_master"
  180. #include "y_groups"
  181.  
  182. #include "y_debug"
  183. #include "y_hooks"
  184.  
  185. #include "y_bintree"
  186. #include "y_amx"
  187. #include "y_playerarray"
  188. #include "y_utils"
  189.  
  190. #include "internal\y_natives"
  191.  
  192. // Define the CMD: macro. This uses the prefix "@yC_" as it's 32 bits so allows
  193. // for fast command string searching in the publics table and makes the commands
  194. // public without the need for the public keyword.
  195. /*#if YSIM_HAS_MASTER
  196. #if _YSIM_IS_CLIENT
  197. #define YCMD:%1(%2) static stock @_yC%1(%2)
  198. #else
  199. #if _YSIM_IS_SERVER
  200. #define YCMD:%1(%2) @yC_%1(a,b[],c);@yC_%1(a,b[],c)U@(8,YSIM_RETURN,@_yC%1(a,b,c));static @_yC%1(%2)
  201. #else
  202. #define YCMD:%1(%2) @yC_%1(a,b[],c);@yC_%1(a,b[],c)<>{}@yC_%1(a,b[],c)<_YCM:y>U@(8,YSIM_RETURN,@_yC%1(a,b,c));static @_yC%1(%2)
  203. #endif
  204. #endif
  205. #else
  206. #define YCMD:%1(%2) @yC_%1(%2);@yC_%1(%2)
  207. #endif*/
  208.  
  209. //#define YCMD:%0(%1) RC:%0(%1)
  210.  
  211. #define _YCMD_0:_YCMD_1:_YCMD_2:%0(%1[]%2) RC:%0(%1[]%2)
  212. #define _YCMD_1:_YCMD_2:%0, Command_GetID(#%0),
  213. #define _YCMD_2:%0) Command_GetID(#%0))
  214. #define @YCMD:%0; Command_TouchNamed(#%0);
  215.  
  216. #define YCMD: _YCMD_0:_YCMD_1:_YCMD_2:
  217.  
  218. // ZCMD compatibility.
  219. #define CMD:%0(%1) RC:%0(%1,__help)if(__help)return 0;else
  220. #define COMMAND CMD
  221.  
  222. // This is opposite AMX_FastString as that's in C, not packed, order.
  223. #define Command_FastString(%1,%2,%3,%4) \
  224. (((%1)<<24)|((%2)<<16)|((%3)<<8)|((%4)<<0))
  225.  
  226. #define MAX_COMMAND_LENGTH (32)
  227.  
  228. #define COMMAND_NOT_FOUND (-1)
  229.  
  230. #if defined COMMAND_SENSITIVE
  231. #define TOLOWER(%0) (%0)
  232. #define COMMAND_END_CMP (0)
  233. #else
  234. //#if defined COMMAND_ACCURATE
  235. // #define TOLOWER(%0) ((%0) | 0x20)
  236. // #define COMMAND_END_CMP (0x20)
  237. //#else
  238. #define TOLOWER(%0) tolower(%0)
  239. #define COMMAND_END_CMP (0)
  240. //#endif
  241. #endif
  242.  
  243. // Don't forget the AMX_FastString below if you change this.
  244. #define COMMAND_FUNCTION_PREFIX (Command_FastString('@', 'y', 'C', '_'))
  245.  
  246. // Reset both of these when you remove a command.
  247. #define _Command_IsValid(%0) ((Command_GetPointer(%0)==(%0))||(YSI_g_sCommands[(%0)][E_COMMANDS_MASTERS]==-1))
  248. #define Command_IsValid(%0) ((0<=(%0)<MAX_COMMANDS)&&_Command_IsValid(%0))
  249.  
  250. enum e_COMM_FLAG (<<= 1)
  251. {
  252. e_COMM_FLAG_PROVIDER = 0x000000FF,
  253. e_COMM_FLAG_SORTED = 0x00000100,
  254. e_COMM_FLAG_PERMISSION_WARN,
  255. e_COMM_FLAG_USE_ALTNAMES,
  256. e_COMM_FLAG_PERMISSION_RET,
  257. e_COMM_FLAG_USE_PREFIX,
  258. e_COMM_FLAG_USE_SPACE,
  259. e_COMM_FLAG_USE_SHORTCUTS,
  260. e_COMM_FLAG_DISCONNECT,
  261. e_COMM_FLAG_ILLEGAL,
  262. e_COMM_FLAG_COLLISION,
  263. e_COMM_FLAG_UNKNOWN,
  264. e_COMM_FLAG_MULPRO,
  265. // Can't easilly add more flags now...
  266. e_COMM_FLAG_OPCP = 0x07C00000, // 0b00000111110..0
  267. e_COMM_FLAG_OPCP_ADD = 0x00400000, // Low bit of above flag.
  268. e_COMM_FLAG_OPCR = 0xF8000000, // 0b11111000000..0
  269. e_COMM_FLAG_OPCR_ADD = 0x08000000 // Low bit of above flag.
  270. }
  271.  
  272. enum E_COMMANDS
  273. {
  274. E_COMMANDS_FUNCTION[MAX_COMMAND_LENGTH char],
  275. PlayerArray:E_COMMANDS_PLAYERS<MAX_PLAYERS>,
  276. #if YSIM_HAS_MASTER
  277. E_COMMANDS_MASTERS,
  278. #endif
  279. E_COMMANDS_FUNC_POINTER
  280. //E_COMMANDS_AMX_ADDRESS,
  281. }
  282.  
  283. static stock
  284. #if YSIM_HAS_MASTER
  285. YSI_g_sPlayerProvider[MAX_PLAYERS char] = {-1, ...},
  286. #endif
  287. YSI_g_sCommands[MAX_COMMANDS][E_COMMANDS],
  288. BinaryTree:YSI_g_sSearchTree<MAX_COMMANDS>,
  289. e_COMM_FLAG:YSI_g_sCommandFlags = e_COMM_FLAG:0xFF,
  290. YSI_g_sCommandIndex,
  291. YSI_g_sCommandCount;
  292.  
  293. /**--------------------------------------------------------------------------**\
  294. <summary>Command_Name</summary>
  295. <param name="index">Index of the command to operate on.</param>
  296. <returns>
  297. -
  298. </returns>
  299. <remarks>
  300. Gets the name of a function from the array.
  301. </remarks>
  302. \**--------------------------------------------------------------------------**/
  303.  
  304. #define Command_Name(%1) \
  305. (YSI_g_sCommands[(%1)][E_COMMANDS_FUNCTION][1])
  306.  
  307. /**--------------------------------------------------------------------------**\
  308. <summary>Command_GetFuncName</summary>
  309. <param name="index">Index of the command to operate on.</param>
  310. <returns>
  311. -
  312. </returns>
  313. <remarks>
  314. Gets the full function name for a slot - note that this may not be the right
  315. function if this slot points to another one.
  316. </remarks>
  317. \**--------------------------------------------------------------------------**/
  318.  
  319. #define Command_GetFuncName(%1) \
  320. (YSI_g_sCommands[(%1)][E_COMMANDS_FUNCTION])
  321.  
  322. /**--------------------------------------------------------------------------**\
  323. <summary>Command_GetPointer</summary>
  324. <param name="index">Index of the command to operate on.</param>
  325. <returns>
  326. -
  327. </returns>
  328. <remarks>
  329. Gets the pointer for a function from the array.
  330. </remarks>
  331. \**--------------------------------------------------------------------------**/
  332.  
  333. #if YSIM_HAS_MASTER
  334. #define Command_GetPointer(%1) \
  335. (YSI_g_sCommands[(%1)][E_COMMANDS_FUNC_POINTER]&0x00FFFFFF)
  336. #else
  337. #define Command_GetPointer(%1) \
  338. (YSI_g_sCommands[(%1)][E_COMMANDS_FUNC_POINTER])
  339. #endif
  340. /**--------------------------------------------------------------------------**\
  341. <summary>Command_GetProvider</summary>
  342. <param name="index">Index of the command to operate on.</param>
  343. <returns>
  344. -
  345. </returns>
  346. <remarks>
  347. Gets the unique script in which this version of the command is.
  348. </remarks>
  349. \**--------------------------------------------------------------------------**/
  350.  
  351. #if YSIM_HAS_MASTER
  352. #define Command_Provider(%1) \
  353. (YSI_g_sCommands[(%1)][E_COMMANDS_FUNC_POINTER]>>>24)
  354. #endif
  355.  
  356. /**--------------------------------------------------------------------------**\
  357. <summary>Command_GetProvider</summary>
  358. <param name="index">Index of the command to operate on.</param>
  359. <returns>
  360. -
  361. </returns>
  362. <remarks>
  363. Gets the unique script in which this version of the command is.
  364. </remarks>
  365. \**--------------------------------------------------------------------------**/
  366.  
  367. #if YSIM_HAS_MASTER
  368. #define Command_DefaultProvider() \
  369. (YSI_g_sCommandFlags & e_COMM_FLAG_PROVIDER)
  370. #endif
  371.  
  372. /**--------------------------------------------------------------------------**\
  373. <summary>Command_GetFunction</summary>
  374. <param name="index">Index of the command to operate on.</param>
  375. <returns>
  376. -
  377. </returns>
  378. <remarks>
  379. Gets the real function for this slot.
  380. </remarks>
  381. \**--------------------------------------------------------------------------**/
  382.  
  383. #define Command_GetFunction(%1) \
  384. (Command_GetFuncName(Command_GetPointer((%1))))
  385.  
  386. /**--------------------------------------------------------------------------**\
  387. <summary>Command_CheckPlayer</summary>
  388. <param name="index">Index of the command to operate on.</param>
  389. <param name="playerid">The player to check for.</param>
  390. <returns>
  391. -
  392. </returns>
  393. <remarks>
  394. Gets wether a player can use a command.
  395. </remarks>
  396. \**--------------------------------------------------------------------------**/
  397.  
  398. #define Command_CheckPlayer(%1,%2) \
  399. (YSI_g_sCommands[(%1)][E_COMMANDS_PLAYERS][Bit_Slot(%2)+1]&Bit_Mask(%2))
  400. //(PA=([E_COMMANDS_PLAYERS], (%2)))
  401. //(Bit_Get(YSI_g_sCommands[(%1)][E_COMMANDS_PLAYERS], (%2)))
  402.  
  403. /**--------------------------------------------------------------------------**\
  404. <summary>Command_DeniedReturn</summary>
  405. <returns>
  406. e_COMM_FLAG_PERMISSION_RET
  407. </returns>
  408. <remarks>
  409. -
  410. </remarks>
  411. \**--------------------------------------------------------------------------**/
  412.  
  413. #define Command_DeniedReturn() \
  414. (YSI_g_sCommandFlags & e_COMM_FLAG_PERMISSION_RET)
  415.  
  416. /**--------------------------------------------------------------------------**\
  417. <summary>Command_SetDeniedReturn</summary>
  418. <returns>
  419. -
  420. </returns>
  421. <remarks>
  422. -
  423. </remarks>
  424. \**--------------------------------------------------------------------------**/
  425.  
  426. foreign void:Command_SetDeniedReturn(bool:set);
  427.  
  428. global void:Command_SetDeniedReturn(bool:set)
  429. {
  430. P:2("Command_SetDeniedReturn called: %i", bool:set);
  431. if (set)
  432. {
  433. YSI_g_sCommandFlags |= e_COMM_FLAG_PERMISSION_RET;
  434. }
  435. else
  436. {
  437. YSI_g_sCommandFlags &= ~e_COMM_FLAG_PERMISSION_RET;
  438. }
  439. //return 1;
  440. }
  441.  
  442. /**--------------------------------------------------------------------------**\
  443. <summary>Command_SetProvider</summary>
  444. <param name="p">Script.</param>
  445. <returns>
  446. -
  447. </returns>
  448. <remarks>
  449. Set the current provider to use for all commands being added. Basically
  450. allows us to have the same command name in different scripts and have them
  451. do different things, with different players targetting different ones.
  452. </remarks>
  453. \**--------------------------------------------------------------------------**/
  454.  
  455. #if YSIM_HAS_MASTER
  456. foreign void:Command_SetProvider(p);
  457.  
  458. global void:Command_SetProvider(p)
  459. {
  460. P:2("Command_SetProvider called: %i", p);
  461. YSI_g_sCommandFlags = (YSI_g_sCommandFlags & ~e_COMM_FLAG_PROVIDER) | (e_COMM_FLAG:p & e_COMM_FLAG_PROVIDER) | e_COMM_FLAG_MULPRO;
  462. //return 1;
  463. }
  464.  
  465. foreign Command_GetProvider();
  466.  
  467. global Command_GetProvider()
  468. {
  469. P:2("Command_GetProvider called");
  470. /*YSI_g_sCommandFlags = (YSI_g_sCommandFlags & ~e_COMM_FLAG_PROVIDER) | (e_COMM_FLAG:p & e_COMM_FLAG_PROVIDER)
  471. if (YSI_g_sCommandFlags & e_COMM_FLAG_MULPRO)
  472. {
  473. return _:(YSI_g_sCommandFlags & (YSI_g_sCommandFlags & ~e_COMM_FLAG_PROVIDER));
  474. }
  475. else
  476. {
  477. }*/
  478. // Poorely named, gets the current provider.
  479. return _:Command_DefaultProvider();
  480. }
  481.  
  482. foreign void:Command_SetPlayerProvider(playerid,p);
  483.  
  484. global void:Command_SetPlayerProvider(playerid,p)
  485. {
  486. P:2("Command_SetPlayerProvider called: %i, %i", playerid, p);
  487. if (0 <= playerid < MAX_PLAYERS)
  488. {
  489. YSI_g_sPlayerProvider{playerid} = p & 0xFF;
  490. }
  491. //return 1;
  492. }
  493.  
  494. foreign Command_GetPlayerProvider(playerid);
  495.  
  496. global Command_GetPlayerProvider(playerid)
  497. {
  498. P:2("Command_GetPlayerProvider called: %i", playerid);
  499. return YSI_g_sPlayerProvider{playerid};
  500. }
  501. #endif
  502.  
  503. /**--------------------------------------------------------------------------**\
  504. <summary>Command_GetDeniedReturn</summary>
  505. <returns>
  506. -
  507. </returns>
  508. <remarks>
  509. -
  510. </remarks>
  511. \**--------------------------------------------------------------------------**/
  512.  
  513. foreign bool:Command_GetDeniedReturn();
  514.  
  515. global bool:Command_GetDeniedReturn()
  516. {
  517. P:2("bool:Command_GetDeniedReturn called");
  518. return bool:Command_DeniedReturn();
  519. }
  520.  
  521. /**--------------------------------------------------------------------------**\
  522. <summary>Command_HasCollisions</summary>
  523. <returns>
  524. e_COMM_FLAG_COLLISION
  525. </returns>
  526. <remarks>
  527. -
  528. </remarks>
  529. \**--------------------------------------------------------------------------**/
  530.  
  531. #define Command_HasCollisions() \
  532. (YSI_g_sCommandFlags & e_COMM_FLAG_COLLISION)
  533.  
  534. /**--------------------------------------------------------------------------**\
  535. <summary>Command_HasCollisions</summary>
  536. <returns>
  537. e_COMM_FLAG_MULPRO
  538. </returns>
  539. <remarks>
  540. -
  541. </remarks>
  542. \**--------------------------------------------------------------------------**/
  543.  
  544. #if YSIM_HAS_MASTER
  545. #define Command_HasMultipleProviders() \
  546. (YSI_g_sCommandFlags & e_COMM_FLAG_MULPRO)
  547. #endif
  548.  
  549. /**--------------------------------------------------------------------------**\
  550. <summary>Command_SetCollisions</summary>
  551. <returns>
  552. -
  553. </returns>
  554. <remarks>
  555. -
  556. </remarks>
  557. \**--------------------------------------------------------------------------**/
  558.  
  559. #define Command_SetCollisions() \
  560. YSI_g_sCommandFlags |= e_COMM_FLAG_COLLISION
  561.  
  562. /**--------------------------------------------------------------------------**\
  563. <summary>Command_IllegalReturn</summary>
  564. <returns>
  565. e_COMM_FLAG_ILLEGAL
  566. </returns>
  567. <remarks>
  568. -
  569. </remarks>
  570. \**--------------------------------------------------------------------------**/
  571.  
  572. #define Command_IllegalReturn() \
  573. (YSI_g_sCommandFlags & e_COMM_FLAG_ILLEGAL)
  574.  
  575. /**--------------------------------------------------------------------------**\
  576. <summary>Command_SetIllegalReturn</summary>
  577. <returns>
  578. -
  579. </returns>
  580. <remarks>
  581. -
  582. </remarks>
  583. \**--------------------------------------------------------------------------**/
  584.  
  585. foreign void:Command_SetIllegalReturn(bool:set);
  586.  
  587. global void:Command_SetIllegalReturn(bool:set)
  588. {
  589. P:2("Command_SetIllegalReturn called: %i", bool:set);
  590. if (set)
  591. {
  592. YSI_g_sCommandFlags |= e_COMM_FLAG_ILLEGAL;
  593. }
  594. else
  595. {
  596. YSI_g_sCommandFlags &= ~e_COMM_FLAG_ILLEGAL;
  597. }
  598. //return 1;
  599. }
  600.  
  601. /**--------------------------------------------------------------------------**\
  602. <summary>Command_GetIllegalReturn</summary>
  603. <returns>
  604. -
  605. </returns>
  606. <remarks>
  607. -
  608. </remarks>
  609. \**--------------------------------------------------------------------------**/
  610.  
  611. foreign bool:Command_GetIllegalReturn();
  612.  
  613. global bool:Command_GetIllegalReturn()
  614. {
  615. P:2("bool:Command_GetIllegalReturn called");
  616. return bool:Command_IllegalReturn();
  617. }
  618.  
  619. /**--------------------------------------------------------------------------**\
  620. <summary>Command_IllegalReturn</summary>
  621. <returns>
  622. e_COMM_FLAG_ILLEGAL
  623. </returns>
  624. <remarks>
  625. -
  626. </remarks>
  627. \**--------------------------------------------------------------------------**/
  628.  
  629. #define Command_UnknownReturn() \
  630. (YSI_g_sCommandFlags & e_COMM_FLAG_UNKNOWN)
  631.  
  632. /**--------------------------------------------------------------------------**\
  633. <summary>Command_SetIllegalReturn</summary>
  634. <returns>
  635. -
  636. </returns>
  637. <remarks>
  638. -
  639. </remarks>
  640. \**--------------------------------------------------------------------------**/
  641.  
  642. foreign void:Command_SetUnknownReturn(bool:set);
  643.  
  644. global void:Command_SetUnknownReturn(bool:set)
  645. {
  646. P:2("Command_SetUnknownReturn called: %i", bool:set);
  647. if (set)
  648. {
  649. YSI_g_sCommandFlags |= e_COMM_FLAG_UNKNOWN;
  650. }
  651. else
  652. {
  653. YSI_g_sCommandFlags &= ~e_COMM_FLAG_UNKNOWN;
  654. }
  655. //return 1;
  656. }
  657.  
  658. /**--------------------------------------------------------------------------**\
  659. <summary>Command_GetIllegalReturn</summary>
  660. <returns>
  661. -
  662. </returns>
  663. <remarks>
  664. -
  665. </remarks>
  666. \**--------------------------------------------------------------------------**/
  667.  
  668. foreign bool:Command_GetUnknownReturn();
  669.  
  670. global bool:Command_GetUnknownReturn()
  671. {
  672. P:2("bool:Command_GetUnknownReturn called");
  673. return bool:Command_UnknownReturn();
  674. }
  675.  
  676. /**--------------------------------------------------------------------------**\
  677. <summary>Command_DisconnectReturn</summary>
  678. <returns>
  679. e_COMM_FLAG_DISCONNECT
  680. </returns>
  681. <remarks>
  682. -
  683. </remarks>
  684. \**--------------------------------------------------------------------------**/
  685.  
  686. #define Command_DisconnectReturn() \
  687. (YSI_g_sCommandFlags & e_COMM_FLAG_DISCONNECT)
  688.  
  689. /**--------------------------------------------------------------------------**\
  690. <summary>Command_SetDisconnectReturn</summary>
  691. <returns>
  692. -
  693. </returns>
  694. <remarks>
  695. -
  696. </remarks>
  697. \**--------------------------------------------------------------------------**/
  698.  
  699. foreign void:Command_SetDisconnectReturn(bool:set);
  700.  
  701. global void:Command_SetDisconnectReturn(bool:set)
  702. {
  703. P:2("Command_SetDisconnectReturn called: %i", bool:set);
  704. if (set)
  705. {
  706. YSI_g_sCommandFlags |= e_COMM_FLAG_DISCONNECT;
  707. }
  708. else
  709. {
  710. YSI_g_sCommandFlags &= ~e_COMM_FLAG_DISCONNECT;
  711. }
  712. //return 1;
  713. }
  714.  
  715. /**--------------------------------------------------------------------------**\
  716. <summary>Command_GetDisconnectReturn</summary>
  717. <returns>
  718. -
  719. </returns>
  720. <remarks>
  721. -
  722. </remarks>
  723. \**--------------------------------------------------------------------------**/
  724.  
  725. foreign bool:Command_GetDisconnectReturn();
  726.  
  727. global bool:Command_GetDisconnectReturn()
  728. {
  729. P:2("bool:Command_GetDisconnectReturn called");
  730. return bool:Command_DisconnectReturn();
  731. }
  732.  
  733. /**--------------------------------------------------------------------------**\
  734. <summary>Command_DeniedDisplay</summary>
  735. <returns>
  736. e_COMM_FLAG_PERMISSION_WARN
  737. </returns>
  738. <remarks>
  739. -
  740. </remarks>
  741. \**--------------------------------------------------------------------------**/
  742.  
  743. #define Command_DeniedDisplay() \
  744. (YSI_g_sCommandFlags & e_COMM_FLAG_PERMISSION_WARN)
  745.  
  746. /**--------------------------------------------------------------------------**\
  747. <summary>Command_SetDeniedDisplay</summary>
  748. <returns>
  749. -
  750. </returns>
  751. <remarks>
  752. -
  753. </remarks>
  754. \**--------------------------------------------------------------------------**/
  755.  
  756. foreign void:Command_SetDeniedDisplay(bool:set);
  757.  
  758. global void:Command_SetDeniedDisplay(bool:set)
  759. {
  760. P:2("Command_SetDeniedDisplay called: %i", bool:set);
  761. if (set)
  762. {
  763. YSI_g_sCommandFlags |= e_COMM_FLAG_PERMISSION_WARN;
  764. }
  765. else
  766. {
  767. YSI_g_sCommandFlags &= ~e_COMM_FLAG_PERMISSION_WARN;
  768. }
  769. //return 1;
  770. }
  771.  
  772. /**--------------------------------------------------------------------------**\
  773. <summary>Command_GetDeniedDisplay</summary>
  774. <returns>
  775. -
  776. </returns>
  777. <remarks>
  778. -
  779. </remarks>
  780. \**--------------------------------------------------------------------------**/
  781.  
  782. foreign bool:Command_GetDeniedDisplay();
  783.  
  784. global bool:Command_GetDeniedDisplay()
  785. {
  786. P:2("bool:Command_GetDeniedDisplay called");
  787. return bool:Command_DeniedDisplay();
  788. }
  789.  
  790. /**--------------------------------------------------------------------------**\
  791. <summary>Command_GetNameInt</summary>
  792. <param name="f">Command to get the name of.</param>
  793. <returns>
  794. -
  795. </returns>
  796. <remarks>
  797. -
  798. </remarks>
  799. \**--------------------------------------------------------------------------**/
  800.  
  801. /*RF:Command_GetNameInt[i](f)
  802. {
  803. P:2("Command_GetNameInt called: %i", f);
  804. if (f >= 0 && f < YSI_g_sCommandIndex)
  805. {
  806. setproperty(8, "", YSIM_STRING, Command_Name(f));
  807. return 1;
  808. }
  809. return 0;
  810. }*/
  811.  
  812. /**--------------------------------------------------------------------------**\
  813. <summary>Command_GetName</summary>
  814. <param name="funcid">Command to get the name of.</param>
  815. <returns>
  816. -
  817. </returns>
  818. <remarks>
  819.  
  820. native Command_GetName(funcid);
  821.  
  822. </remarks>
  823. \**--------------------------------------------------------------------------**/
  824.  
  825. /*stock Command_GetName(funcid)
  826. {
  827. P:3("Command_GetName called: %i", funcid);
  828. new
  829. buffer[32] = "";
  830. if (Command_GetNameInt(funcid))
  831. {
  832. getproperty(8, "", YSIM_STRING, buffer);
  833. strunpack(buffer, buffer);
  834. }
  835. return buffer;
  836. }*/
  837.  
  838. /**--------------------------------------------------------------------------**\
  839. <summary>Command_IsSorted</summary>
  840. <returns>
  841. e_COMM_FLAG_SORTED
  842. </returns>
  843. <remarks>
  844. -
  845. </remarks>
  846. \**--------------------------------------------------------------------------**/
  847.  
  848. #define Command_IsSorted() \
  849. (YSI_g_sCommandFlags & e_COMM_FLAG_SORTED)
  850.  
  851. /**--------------------------------------------------------------------------**\
  852. <summary>Command_Generate</summary>
  853. <returns>
  854. -
  855. </returns>
  856. <remarks>
  857. -
  858. </remarks>
  859. \**--------------------------------------------------------------------------**/
  860.  
  861. foreign void:Command_Generate();
  862.  
  863. global void:Command_Generate()
  864. {
  865. P:2("Command_Generate called");
  866. P:2("Command_Generate called");
  867. if (!Command_IsSorted())
  868. {
  869. P:2("Command_Generate: Count = %d", YSI_g_sCommandCount);
  870. new
  871. data[MAX_COMMANDS][E_BINTREE_INPUT];
  872. // This is only called once, so we know YSI_g_sCommandCount will be
  873. // accurate WRT the locations of commands.
  874. for (new i = 0; i != YSI_g_sCommandCount; ++i)
  875. {
  876. data[i][E_BINTREE_INPUT_POINTER] = i;
  877. new
  878. hash = Command_PackHash(Command_Name(i));
  879. // Check for an existing command with this hash.
  880. if (!Command_HasCollisions())
  881. {
  882. for (new j = 0; j != i; ++j)
  883. {
  884. if (hash == data[j][E_BINTREE_INPUT_VALUE])
  885. {
  886. Command_SetCollisions();
  887. break;
  888. }
  889. }
  890. }
  891. C:3(else printf("Command_Generate: Hash = %d", hash););
  892. P:2("Command_Generate: Hash = %d", hash);
  893. data[i][E_BINTREE_INPUT_VALUE] = hash;
  894. }
  895. // WHY THE HECK did I change "Bintree_Generate" to "Bintree_Fill"? That
  896. // skips the whole sorting code before adding things to the binary tree!
  897. if (YSI_g_sCommandCount)
  898. {
  899. //Bintree_Fill(YSI_g_sSearchTree, data, YSI_g_sCommandCount);
  900. Bintree_Generate(YSI_g_sSearchTree, data, YSI_g_sCommandCount);
  901. //Bintree_Fill(YSI_g_sSearchTree, data, YSI_g_sCommandCount);
  902. }
  903. P:4("Command_Generate: %d %d %d %d %d", YSI_g_sSearchTree[0][E_BINTREE_TREE_VALUE], YSI_g_sSearchTree[0][E_BINTREE_TREE_LEFT], YSI_g_sSearchTree[0][E_BINTREE_TREE_RIGHT], YSI_g_sSearchTree[0][E_BINTREE_TREE_PARENT], YSI_g_sSearchTree[0][E_BINTREE_TREE_POINTER]);
  904. // Set sorted to true.
  905. YSI_g_sCommandFlags |= e_COMM_FLAG_SORTED;
  906. }
  907. //return 1;
  908. }
  909.  
  910. foreign void:Command_IncOPCR();
  911.  
  912. global void:Command_IncOPCR()
  913. {
  914. P:2("Command_IncOPCR called");
  915. YSI_g_sCommandFlags += e_COMM_FLAG_OPCR_ADD;
  916. //return 1;
  917. }
  918.  
  919. foreign void:Command_DecOPCR();
  920.  
  921. global void:Command_DecOPCR()
  922. {
  923. P:2("Command_DecOPCR called");
  924. YSI_g_sCommandFlags -= e_COMM_FLAG_OPCR_ADD;
  925. //return 1;
  926. }
  927.  
  928. foreign void:Command_IncOPCP();
  929.  
  930. global void:Command_IncOPCP()
  931. {
  932. P:2("Command_IncOPCP called");
  933. YSI_g_sCommandFlags += e_COMM_FLAG_OPCP_ADD;
  934. //return 1;
  935. }
  936.  
  937. foreign void:Command_DecOPCP();
  938.  
  939. global void:Command_DecOPCP()
  940. {
  941. P:2("Command_DecOPCP called");
  942. YSI_g_sCommandFlags -= e_COMM_FLAG_OPCP_ADD;
  943. //return 1;
  944. }
  945.  
  946. /**--------------------------------------------------------------------------**\
  947. <summary>OnScriptInit</summary>
  948. <returns>
  949. -
  950. </returns>
  951. <remarks>
  952. -
  953. </remarks>
  954. \**--------------------------------------------------------------------------**/
  955.  
  956. hook OnScriptInit()
  957. {
  958. // Set the default provider as any script.
  959. //YSI_g_sCommandFlags = 0xFF;
  960. P:1("Command_OnScriptInit called");
  961. // Initialise the tree.
  962. #if YSIM_NOT_CLIENT
  963. Bintree_Reset(YSI_g_sSearchTree);
  964. //YSI_g_sMaster23 = getproperty(8, "x@");
  965. #endif
  966. // Make a list of unused commands.
  967. for (new i = 0; i != MAX_COMMANDS - 1; ++i)
  968. {
  969. YSI_g_sCommands[i][E_COMMANDS_FUNC_POINTER] = i + 1;
  970. }
  971. YSI_g_sCommands[MAX_COMMANDS - 1][E_COMMANDS_FUNC_POINTER] = -1;
  972. // Loop through all the possible commands. Note that this may need to add
  973. // commands to the remote command system if this is not the master system.
  974. // The way the master system is designed means that we will know if we are
  975. // master or not by the time this function is called.
  976. new
  977. buffer[32],
  978. idx;
  979. // This is the only place where AMX_FastString is used instead of
  980. // Commands_FastString as the strings in the AMX are not the same as packed
  981. // strings - they are in different memory orders.
  982. while ((idx = AMX_GetPublicNamePrefix(idx, buffer, _A<@yC_>)))
  983. {
  984. Command_Add(buffer, _@);
  985. //Command_Add(unpack(buffer), _@);
  986. //Command_Add(unpack(buffer), _@);
  987. P:2("Command_OnScriptInit: Adding %s", unpack(buffer));
  988. }
  989. Command_Generate();
  990. // Now that all commands have been added to the array, sort it.
  991. // Now call the next constructor.
  992. if (funcidx("OnPlayerCommandPerformed") != -1)
  993. {
  994. Command_IncOPCP();
  995. }
  996. if (funcidx("OnPlayerCommandReceived") != -1)
  997. {
  998. Command_IncOPCR();
  999. }
  1000. }
  1001.  
  1002. // Forwards for initial command callbacks.
  1003. forward OnPlayerCommandReceived(playerid, cmdtext[]);
  1004. forward OnPlayerCommandPerformed(playerid, cmdtext[], success);
  1005.  
  1006. hook OnScriptExit()
  1007. {
  1008. P:1("Commands_OnScriptExit called");
  1009. if (funcidx("OnPlayerCommandPerformed") != -1)
  1010. {
  1011. Command_DecOPCP();
  1012. }
  1013. if (funcidx("OnPlayerCommandReceived") != -1)
  1014. {
  1015. Command_DecOPCR();
  1016. }
  1017. return 1;
  1018. }
  1019.  
  1020. /**--------------------------------------------------------------------------**\
  1021. <summary>OnScriptClose</summary>
  1022. <param name="script">ID of the closing script.</param>
  1023. <returns>
  1024. -
  1025. </returns>
  1026. <remarks>
  1027. Called when a script under the control of the master system ends.
  1028. </remarks>
  1029. \**--------------------------------------------------------------------------**/
  1030.  
  1031. #if YSIM_HAS_MASTER && YSIM_NOT_CLIENT
  1032. #if _YSIM_IS_CLOUD
  1033. public OnScriptClose(script) <>
  1034. {
  1035. #if defined Command_OnScriptClose
  1036. Command_OnScriptClose(script);
  1037. #endif
  1038. }
  1039.  
  1040. public OnScriptClose(script) <_YCM:y>
  1041. #elseif _YSIM_IS_STUB
  1042. #error y_commands called with _YSIM_IS_STUB.
  1043. #else
  1044. public OnScriptClose(script)
  1045. #endif
  1046. {
  1047. new
  1048. mask = ~(1 << script);
  1049. for (new i = 0; i != MAX_COMMANDS; ++i)
  1050. {
  1051. if (Command_GetPointer(i) == i && (YSI_g_sCommands[i][E_COMMANDS_MASTERS] &= mask) == 0)
  1052. {
  1053. for (new j = 0; j != MAX_COMMANDS; ++j)
  1054. {
  1055. // Remove all alternate names.
  1056. if (Command_GetPointer(j) == i)
  1057. {
  1058. YSI_g_sCommands[j][E_COMMANDS_FUNC_POINTER] = YSI_g_sCommandIndex;
  1059. YSI_g_sCommandIndex = j;
  1060. YSI_g_sCommands[j][E_COMMANDS_MASTERS] = 0;
  1061. //Bintree_Delete(YSI_g_sSearchTree, j, YSI_g_sCommandCount);
  1062. Command_RemoveFromBintree(j);
  1063. --YSI_g_sCommandCount;
  1064. //Bintree_Add(YSI_g_sSearchTree, YSI_g_sCommandIndex, hash, YSI_g_sCommandIndex);
  1065. }
  1066. }
  1067. }
  1068. /*new
  1069. p = i;
  1070. while (YSI_g_sCommands[p][E_COMMANDS_MASTERS] == -1)
  1071. {
  1072. p = Command_GetPointer(p);
  1073. }
  1074. if (YSI_g_sCommands[p][E_COMMANDS_FUNC_POINTER] == p && (YSI_g_sCommands[p][E_COMMANDS_MASTERS] &= mask) == 0)
  1075. {
  1076. // Remove all alternate versions of commands - done backwards by
  1077. // an alternate command checking its parent. Can also handle
  1078. // alternate versions of alternate commands (note this is the
  1079. // only code that can currently).
  1080. YSI_g_sCommands[i][E_COMMANDS_MASTERS] = 0;
  1081. YSI_g_sCommands[i][E_COMMANDS_FUNC_POINTER] = YSI_g_sCommandIndex;
  1082. YSI_g_sCommandIndex = i;
  1083. // Mark it as invalid in this instance.
  1084. YSI_g_sCommands[i][E_COMMANDS_MASTERS] = 0;
  1085. // Reduce the number of commands.
  1086. --YSI_g_sCommandCount;
  1087. }*/
  1088. }
  1089. //YSI_g_sMaster23 = getproperty(8, "x@");
  1090. // I can add better removal code later. Done ^
  1091. #if defined Command_OnScriptClose
  1092. Command_OnScriptClose(script);
  1093. #endif
  1094. }
  1095.  
  1096. // Don't need ALS here as we did it supporting it at the start.
  1097. #undef OnScriptClose
  1098. #define OnScriptClose Command_OnScriptClose
  1099. #if defined Command_OnScriptClose
  1100. forward Command_OnScriptClose(script);
  1101. #endif
  1102. #endif
  1103.  
  1104. /**--------------------------------------------------------------------------**\
  1105. <summary>Command_TouchNamed</summary>
  1106. <param name="string:command[]">Command to "touch".</param>
  1107. <returns>
  1108. -
  1109. </returns>
  1110. <remarks>
  1111. Used within "GROUP_ADD" to quickly assign a load of commands to just one
  1112. group.
  1113. </remarks>
  1114. \**--------------------------------------------------------------------------**/
  1115.  
  1116. foreign void:Command_TouchNamed(string:command[]);
  1117.  
  1118. global void:Command_TouchNamed(string:command[])
  1119. {
  1120. new
  1121. id = Command_Find(command);
  1122. if (id != COMMAND_NOT_FOUND)
  1123. {
  1124. NO_GROUPS(id)
  1125. {
  1126. return; //0;
  1127. }
  1128. }
  1129. }
  1130.  
  1131. /**--------------------------------------------------------------------------**\
  1132. <summary>Command_Touch</summary>
  1133. <param name="string:command[]">Command to "touch".</param>
  1134. <returns>
  1135. -
  1136. </returns>
  1137. <remarks>
  1138. Used within "GROUP_ADD" to quickly assign a load of commands to just one
  1139. group.
  1140. </remarks>
  1141. \**--------------------------------------------------------------------------**/
  1142.  
  1143. foreign void:Command_Touch(command);
  1144.  
  1145. global void:Command_Touch(command)
  1146. {
  1147. if (Command_IsValid(command))
  1148. {
  1149. NO_GROUPS(command)
  1150. {
  1151. return; //0;
  1152. }
  1153. //return 1;
  1154. }
  1155. //return 0;
  1156. }
  1157.  
  1158. /**--------------------------------------------------------------------------**\
  1159. <summary>OnPlayerConnect</summary>
  1160. <returns>
  1161. -
  1162. </returns>
  1163. <remarks>
  1164. -
  1165. </remarks>
  1166. \**--------------------------------------------------------------------------**/
  1167.  
  1168. // TODO: Rewrite!
  1169. //RA:Command_OnPlayerConnect(playerid)
  1170.  
  1171. #if YSIM_NOT_CLIENT
  1172. mhook OnPlayerConnect(playerid)
  1173. {
  1174. //#if YSIM_NOT_CLIENT
  1175. //YSI_g_sCommandDialog[playerid] = -1;
  1176. #if YSIM_HAS_MASTER
  1177. YSI_g_sPlayerProvider{playerid} = 0xFF;
  1178. #endif
  1179. NO_GROUPS()
  1180. {
  1181. new
  1182. slot = Bit_Slot(playerid) + 1,
  1183. Bit:mask = Bit_Mask(playerid); //Bit:(1 << (playerid & (cellbits - 1)));
  1184. for (new i = 0; i != MAX_COMMANDS; ++i)
  1185. {
  1186. YSI_g_sCommands[i][E_COMMANDS_PLAYERS][slot] |= mask;
  1187. //PA+(YSI_g_sCommands[i][E_COMMANDS_PLAYERS], mask);
  1188. }
  1189. }
  1190. //#endif
  1191. // Groups will ALWAYS be called after this function - so this can reset
  1192. // player permissions however it likes with the group system then being
  1193. // able to override anything set in here.
  1194. return 1;
  1195. //ALS_CALL<PlayerConnect, i>(playerid)
  1196. }
  1197. #endif
  1198.  
  1199. /**--------------------------------------------------------------------------**\
  1200. <summary>Command_</summary>
  1201. <param name="command">Command to declare.</param>
  1202. <returns>
  1203. -
  1204. </returns>
  1205. <remarks>
  1206. Deprecated!
  1207. </remarks>
  1208. \**--------------------------------------------------------------------------**/
  1209.  
  1210. #define Command_(%1) \
  1211. CMD:%1(playerid,params[],help)
  1212.  
  1213. /**--------------------------------------------------------------------------**\
  1214. <summary>ycmd</summary>
  1215. <param name="command[]">Command to register.</param>
  1216. <returns>
  1217. -
  1218. </returns>
  1219. <remarks>
  1220. Deprecated!
  1221. </remarks>
  1222. \**--------------------------------------------------------------------------**/
  1223.  
  1224. #define ycmd(%1);
  1225.  
  1226. /**--------------------------------------------------------------------------**\
  1227. <summary>Command_FindFast</summary>
  1228. <param name="data[]">Function name to find.</param>
  1229. <param name="value">Hash of function name.</param>
  1230. <returns>
  1231. Position in functions array or COMMAND_NOT_FOUND.
  1232. </returns>
  1233. <remarks>
  1234. -
  1235. </remarks>
  1236. \**--------------------------------------------------------------------------**/
  1237.  
  1238. #if YSIM_HAS_MASTER
  1239. static stock Command_FindFast(data[], value, provider = 0xFF)
  1240. #else
  1241. static stock Command_FindFast(data[], value)
  1242. #endif
  1243. {
  1244. new
  1245. leaf,
  1246. pointer;
  1247. #if YSIM_HAS_MASTER
  1248. P:4("Command_FindFast called: \"%s\", %i, %i", data, value, provider);
  1249. provider <<= 24;
  1250. P:5("Command_FindFast: Searching for %s", data);
  1251. if (Command_HasMultipleProviders())
  1252. {
  1253. /*if (provider == 0xFF)
  1254. {
  1255. while ((pointer = Bintree_FindValue(YSI_g_sSearchTree, value, leaf)) != BINTREE_NOT_FOUND)
  1256. {
  1257. //new
  1258. // p = YSI_g_sCommands[pointer][E_COMMANDS_FUNC_POINTER] & 0xFF000000;
  1259. //P:7("Command_FindFast: providers %d %d %d", Command_HasMultipleProviders(), (p & 0xFF000000 != 0xFF000000), (p >>> 24 != provider));
  1260. // Any script will do.
  1261. //if ((YSI_g_sCommands[pointer][E_COMMANDS_FUNC_POINTER] & 0xFF000000) != 0xFF000000) continue;
  1262. //if (p != 0xFF000000 && p != provider) continue;
  1263. // Don't do an strcmp if there are no collisions.
  1264. P:7("Command_FindFast: collisions %d", !Command_HasCollisions());
  1265. if (!Command_HasCollisions()) return pointer;
  1266. //if (!strcmp(YSI_g_sCommands[i][E_COMMANDS_FUNCTION][1], funcname) &&
  1267. P:7("Command_FindFast: strcmp %d", !strcmp(Command_Name(pointer), data));
  1268. if (!strcmp(Command_Name(pointer), data)) return pointer;
  1269. //if (!strcmp(Command_Name(pointer), data) && (Command_GetProvider(pointer) == 0xFF || Command_GetProvider(pointer) == provider)) return pointer;
  1270. }
  1271. }
  1272. else
  1273. {*/
  1274. while ((pointer = Bintree_FindValue(YSI_g_sSearchTree, value, leaf)) != BINTREE_NOT_FOUND)
  1275. {
  1276. //new
  1277. // p = YSI_g_sCommands[pointer][E_COMMANDS_FUNC_POINTER] & 0xFF000000;
  1278. //P:7("Command_FindFast: providers %d %d %d", Command_HasMultipleProviders(), (p & 0xFF000000 != 0xFF000000), (p >>> 24 != provider));
  1279. //if (p != provider && p != 0xFF000000) continue;
  1280. // Only one provider will do.
  1281. if ((YSI_g_sCommands[pointer][E_COMMANDS_FUNC_POINTER] & 0xFF000000) != provider) continue;
  1282. // Don't do an strcmp if there are no collisions.
  1283. P:7("Command_FindFast: collisions %d", Command_HasCollisions());
  1284. if (!Command_HasCollisions()) return pointer;
  1285. //if (!strcmp(YSI_g_sCommands[i][E_COMMANDS_FUNCTION][1], funcname) &&
  1286. P:7("Command_FindFast: strcmp %d", !strcmp(Command_Name(pointer), data));
  1287. if (!strcmp(Command_Name(pointer), data)) return pointer;
  1288. //if (!strcmp(Command_Name(pointer), data) && (Command_GetProvider(pointer) == 0xFF || Command_GetProvider(pointer) == provider)) return pointer;
  1289. }
  1290. //}
  1291. }
  1292. else
  1293. {
  1294. #endif
  1295. while ((pointer = Bintree_FindValue(YSI_g_sSearchTree, value, leaf)) != BINTREE_NOT_FOUND)
  1296. {
  1297. //new
  1298. // p = YSI_g_sCommands[pointer][E_COMMANDS_FUNC_POINTER] & 0xFF000000;
  1299. //P:7("Command_FindFast: providers %d %d %d", Command_HasMultipleProviders(), (p & 0xFF000000 != 0xFF000000), (p >>> 24 != provider));
  1300. //if (Command_HasMultipleProviders() && (p != 0xFF000000) && (p != provider)) continue;
  1301. // Don't do an strcmp if there are no collisions.
  1302. P:7("Command_FindFast: collisions %d", Command_HasCollisions());
  1303. if (!Command_HasCollisions()) return pointer;
  1304. //if (!strcmp(YSI_g_sCommands[i][E_COMMANDS_FUNCTION][1], funcname) &&
  1305. P:7("Command_FindFast: strcmp %d", !strcmp(Command_Name(pointer), data));
  1306. if (!strcmp(Command_Name(pointer), data)) return pointer;
  1307. //if (!strcmp(Command_Name(pointer), data) && (Command_GetProvider(pointer) == 0xFF || Command_GetProvider(pointer) == provider)) return pointer;
  1308. }
  1309. #if YSIM_HAS_MASTER
  1310. }
  1311. #endif
  1312. P:5("Command_FindFast: Not found");
  1313. return COMMAND_NOT_FOUND;
  1314. }
  1315.  
  1316. static stock Command_FindFastStrict(data[], value, provider)
  1317. {
  1318. #if YSIM_HAS_MASTER
  1319. P:4("Command_FindFastStrict called: \"%s\", %i, %i", data, value, provider);
  1320. #else
  1321. #pragma unused provider
  1322. #endif
  1323. new
  1324. leaf,
  1325. pointer;
  1326. P:5("Command_FindFast: Searching for %s", data);
  1327. while ((pointer = Bintree_FindValue(YSI_g_sSearchTree, value, leaf)) != BINTREE_NOT_FOUND)
  1328. {
  1329. #if YSIM_HAS_MASTER
  1330. if (Command_HasMultipleProviders() && (YSI_g_sCommands[pointer][E_COMMANDS_FUNC_POINTER] >>> 24 != provider)) continue;
  1331. #endif
  1332. // Don't do an strcmp if there are no collisions.
  1333. if (!Command_HasCollisions()) return pointer;
  1334. if (!strcmp(Command_Name(pointer), data)) return pointer;
  1335. }
  1336. P:5("Command_FindFast: Not found");
  1337. return COMMAND_NOT_FOUND;
  1338. }
  1339.  
  1340. /**--------------------------------------------------------------------------**\
  1341. <summary>Command_FindSlow</summary>
  1342. <param name="funcname[]">Function to find.</param>
  1343. <returns>
  1344. -
  1345. </returns>
  1346. <remarks>
  1347. Searches through the array for function linearly - used to set altnames
  1348. before the data has been sorted.
  1349. </remarks>
  1350. \**--------------------------------------------------------------------------**/
  1351.  
  1352. #if YSIM_HAS_MASTER
  1353. static stock Command_FindSlow(funcname[], provider = 0xFF)
  1354. #else
  1355. static stock Command_FindSlow(funcname[])
  1356. #endif
  1357. {
  1358. #if YSIM_HAS_MASTER
  1359. P:4("Command_FindSlow called: \"%s\", %i", funcname, provider);
  1360. #endif
  1361. for (new i = 0; i != MAX_COMMANDS; ++i)
  1362. {
  1363. #if YSIM_HAS_MASTER
  1364. new
  1365. p = YSI_g_sCommands[i][E_COMMANDS_FUNC_POINTER];
  1366. if (((p & 0x00FFFFFF == i) || (YSI_g_sCommands[i][E_COMMANDS_MASTERS] == -1)) &&
  1367. (!Command_HasMultipleProviders() || (p & 0xFF000000 == 0xFF000000) || (p >>> 24 == provider)) &&
  1368. !strcmp(Command_Name(i), funcname))
  1369. #else
  1370. if (YSI_g_sCommands[i][E_COMMANDS_FUNC_POINTER] == i && !strcmp(Command_Name(i), funcname))
  1371. #endif
  1372. {
  1373. return i;
  1374. }
  1375. }
  1376. return COMMAND_NOT_FOUND;
  1377. }
  1378.  
  1379. /**--------------------------------------------------------------------------**\
  1380. <summary>Command_FindSlowStrict</summary>
  1381. <param name="funcname[]">Function to find.</param>
  1382. <returns>
  1383. -
  1384. </returns>
  1385. <remarks>
  1386. Searches through the array for function linearly - used to set altnames
  1387. before the data has been sorted.
  1388. </remarks>
  1389. \**--------------------------------------------------------------------------**/
  1390.  
  1391. static stock Command_FindSlowStrict(funcname[], provider)
  1392. {
  1393. #if YSIM_HAS_MASTER
  1394. P:4("Command_FindSlowStrict called: \"%s\", %i", funcname, provider);
  1395. #else
  1396. #pragma unused provider
  1397. #endif
  1398. P:2("Command_FindSlowStrict: Searching for %s", unpack(funcname));
  1399. for (new i = 0; i != MAX_COMMANDS; ++i)
  1400. {
  1401. #if YSIM_HAS_MASTER
  1402. new
  1403. p = YSI_g_sCommands[i][E_COMMANDS_FUNC_POINTER];
  1404. P:6("Command_FindSlowStrict: %s %08x %d %d", unpack(funcname), Command_Name(i), p & 0xFFFFFF, i);
  1405. // This needs additional checks that the item is valid.
  1406. if (((p & 0x00FFFFFF == i) || (YSI_g_sCommands[i][E_COMMANDS_MASTERS] == -1)) &&
  1407. (!Command_HasMultipleProviders() || (p >>> 24 == provider)) &&
  1408. !strcmp(Command_Name(i), funcname))
  1409. #else
  1410. if (YSI_g_sCommands[i][E_COMMANDS_FUNC_POINTER] == i && !strcmp(Command_Name(i), funcname))
  1411. #endif
  1412. {
  1413. return i;
  1414. }
  1415. }
  1416. return COMMAND_NOT_FOUND;
  1417. }
  1418.  
  1419. /**--------------------------------------------------------------------------**\
  1420. <summary>Command_AddHash</summary>
  1421. <param name="command[]">Command text to hash.</param>
  1422. <param name="dest[]">Array to copy to.</param>
  1423. <param name="idx">Point to start copying from.</param>
  1424. <returns>
  1425. hash value.
  1426. </returns>
  1427. <remarks>
  1428. Hashes a string and copies it to a destination at the same time.
  1429. </remarks>
  1430. \**--------------------------------------------------------------------------**/
  1431.  
  1432. static stock Command_AddHash(command[], dest[], idx)
  1433. {
  1434. P:4("Command_AddHash called: \"%s\", %i, %i", command, dest, idx);
  1435. // Skip the function name prefix.
  1436. new
  1437. hash = -1,
  1438. ch,
  1439. dx = 1,
  1440. end = idx + 28;
  1441. // Copy and hash at the same time.
  1442. do
  1443. {
  1444. /*ch = TOLOWER(command[idx++]);
  1445. // Always NULL terminate.
  1446. if ((dest[dx] = ch << 24) == COMMAND_END_CMP << 24)
  1447. {
  1448. // Fixes a bug with commands multiples of 4 chars long.
  1449. dest[dx] = 0;
  1450. break;
  1451. }*/
  1452. ch = TOLOWER(command[idx++]);
  1453. if (ch == COMMAND_END_CMP) break;
  1454. dest[dx] = ch << 24;
  1455. hash = hash * 33 + ch; //Command_ToUpper(ch);
  1456. ch = TOLOWER(command[idx++]);
  1457. if (ch == COMMAND_END_CMP) break;
  1458. dest[dx] |= ch << 16;
  1459. hash = hash * 33 + ch; //Command_ToUpper(ch);
  1460. ch = TOLOWER(command[idx++]);
  1461. if (ch == COMMAND_END_CMP) break;
  1462. dest[dx] |= ch << 8;
  1463. hash = hash * 33 + ch; //Command_ToUpper(ch);
  1464. ch = TOLOWER(command[idx++]);
  1465. if (ch == COMMAND_END_CMP) break;
  1466. dest[dx] |= ch << 0;
  1467. hash = hash * 33 + ch; //Command_ToUpper(ch);
  1468. ++dx;
  1469. }
  1470. while (idx < end);
  1471. return hash;
  1472. }
  1473.  
  1474. /**--------------------------------------------------------------------------**\
  1475. <summary>Command_AddHashPacked</summary>
  1476. <param name="command[]">Packed command text to hash.</param>
  1477. <param name="dest[]">Array to copy to.</param>
  1478. <param name="idx">Point to start copying from.</param>
  1479. <returns>
  1480. hash value.
  1481. </returns>
  1482. <remarks>
  1483. Hashes a string and copies it to a destination at the same time.
  1484. </remarks>
  1485. \**--------------------------------------------------------------------------**/
  1486.  
  1487. static stock Command_AddHashPacked(command[], dest[], idx)
  1488. {
  1489. P:4("Command_AddHashPacked called: \"%s\", %i, %i", command, dest, idx);
  1490. // Skip the function name prefix.
  1491. new
  1492. hash = -1,
  1493. ch,
  1494. dx = 1,
  1495. end = idx + 28;
  1496. // Copy and hash at the same time.
  1497. do
  1498. {
  1499. /*ch = TOLOWER(command[idx++]);
  1500. // Always NULL terminate.
  1501. if ((dest[dx] = ch << 24) == COMMAND_END_CMP << 24)
  1502. {
  1503. // Fixes a bug with commands multiples of 4 chars long.
  1504. dest[dx] = 0;
  1505. break;
  1506. }*/
  1507. ch = TOLOWER(command{idx++});
  1508. if (ch == COMMAND_END_CMP) break;
  1509. dest[dx] = ch << 24;
  1510. hash = hash * 33 + ch; //Command_ToUpper(ch);
  1511. ch = TOLOWER(command{idx++});
  1512. if (ch == COMMAND_END_CMP) break;
  1513. dest[dx] |= ch << 16;
  1514. hash = hash * 33 + ch; //Command_ToUpper(ch);
  1515. ch = TOLOWER(command{idx++});
  1516. if (ch == COMMAND_END_CMP) break;
  1517. dest[dx] |= ch << 8;
  1518. hash = hash * 33 + ch; //Command_ToUpper(ch);
  1519. ch = TOLOWER(command{idx++});
  1520. if (ch == COMMAND_END_CMP) break;
  1521. dest[dx] |= ch << 0;
  1522. hash = hash * 33 + ch; //Command_ToUpper(ch);
  1523. ++dx;
  1524. }
  1525. while (idx < end);
  1526. return hash;
  1527. }
  1528.  
  1529. /**--------------------------------------------------------------------------**\
  1530. <summary>Command_FastHash</summary>
  1531. <param name="command[]">Command text to hash.</param>
  1532. <returns>
  1533. hash value.
  1534. </returns>
  1535. <remarks>
  1536. Just hashes the passed string.
  1537. </remarks>
  1538. \**--------------------------------------------------------------------------**/
  1539.  
  1540. static stock Command_FastHash(command[])
  1541. {
  1542. P:4("Command_FastHash called: \"%s\"", command);
  1543. new
  1544. index = 0,
  1545. hash = -1,
  1546. ch;
  1547. while ((ch = command[index++])) hash = hash * 33 + TOLOWER(ch);
  1548. return hash;
  1549. }
  1550.  
  1551. /**--------------------------------------------------------------------------**\
  1552. <summary>Command_PackHash</summary>
  1553. <param name="command[]">Command text to hash.</param>
  1554. <returns>
  1555. hash value.
  1556. </returns>
  1557. <remarks>
  1558. Hashes packed strings.
  1559. </remarks>
  1560. \**--------------------------------------------------------------------------**/
  1561.  
  1562. static stock Command_PackHash(command[])
  1563. {
  1564. P:4("Command_PackHash called: \"%s\"", command);
  1565. new
  1566. index = 0,
  1567. hash = -1,
  1568. ch;
  1569. while ((ch = command[index++]))
  1570. {
  1571. P:4("Commands_PackHash: ch = 0x%04x%04x", ch >>> 16, ch & 0xFFFF);
  1572. if (ch & 0xFF000000)
  1573. {
  1574. hash = hash * 33 + TOLOWER(ch >>> 24);
  1575. P:5("Command_PackHash: Hash1 = %d", hash);
  1576. }
  1577. else
  1578. {
  1579. break;
  1580. }
  1581. if (ch & 0x00FF0000)
  1582. {
  1583. hash = hash * 33 + TOLOWER(ch >> 16 & 0xFF);
  1584. P:5("Command_PackHash: Hash2 = %d", hash);
  1585. }
  1586. else
  1587. {
  1588. break;
  1589. }
  1590. if (ch & 0x0000FF00)
  1591. {
  1592. hash = hash * 33 + TOLOWER(ch >> 8 & 0xFF);
  1593. P:5("Command_PackHash: Hash3 = %d", hash);
  1594. }
  1595. else
  1596. {
  1597. break;
  1598. }
  1599. if (ch & 0x000000FF)
  1600. {
  1601. hash = hash * 33 + TOLOWER(ch & 0xFF);
  1602. P:5("Command_PackHash: Hash4 = %d", hash);
  1603. }
  1604. else
  1605. {
  1606. break;
  1607. }
  1608. }
  1609. return hash;
  1610. }
  1611.  
  1612. /**--------------------------------------------------------------------------**\
  1613. <summary>Command_Hash</summary>
  1614. <param name="command[]">Command text to hash.</param>
  1615. <param name="&index">Start point and variable to store end point to.</param>
  1616. <param name="&length">Length of the hashed word.</param>
  1617. <returns>
  1618. hash value.
  1619. </returns>
  1620. <remarks>
  1621. Hashes a string using space delimiters and returns information such as the
  1622. length of the string hased and the start point of the next word.
  1623. </remarks>
  1624. \**--------------------------------------------------------------------------**/
  1625.  
  1626. static stock Command_Hash(command[], &index, &length)
  1627. {
  1628. P:4("Command_Hash called: \"%s\", %i, %i", command, index, length);
  1629. new
  1630. hash = -1,
  1631. ch;
  1632. length = index;
  1633. while ((ch = command[index++]) > ' ') hash = hash * 33 + TOLOWER(ch);
  1634. length = index - length - 1;
  1635. while (ch)
  1636. {
  1637. if (ch > ' ')
  1638. {
  1639. break;
  1640. }
  1641. ch = command[index++];
  1642. }
  1643. --index;
  1644. return hash;
  1645. }
  1646.  
  1647. /**--------------------------------------------------------------------------**\
  1648. <summary>Command_Find</summary>
  1649. <param name="function[]">Function name to find.</param>
  1650. <returns>
  1651. Position in functions array or COMMAND_NOT_FOUND.
  1652. </returns>
  1653. <remarks>
  1654. Used by API functions to avoid repeated sorting checks.
  1655. </remarks>
  1656. \**--------------------------------------------------------------------------**/
  1657.  
  1658. #if YSIM_HAS_MASTER
  1659. static stock Command_Find(function[], provider = -1)
  1660. #else
  1661. static stock Command_Find(function[])
  1662. #endif
  1663. {
  1664. #if YSIM_HAS_MASTER
  1665. P:4("Command_Find called: \"%s\", %i", function, provider);
  1666. if (provider == -1)
  1667. {
  1668. provider = _:Command_DefaultProvider();
  1669. // Find the ID of the command.
  1670. if (Command_IsSorted())
  1671. {
  1672. return Command_FindFast(function, Command_FastHash(function), provider);
  1673. }
  1674. else
  1675. {
  1676. return Command_FindSlow(function, provider);
  1677. }
  1678. }
  1679. else
  1680. {
  1681. provider &= 0xFF;
  1682. // Find the ID of the command.
  1683. if (Command_IsSorted())
  1684. {
  1685. return Command_FindFastStrict(function, Command_FastHash(function), provider);
  1686. }
  1687. else
  1688. {
  1689. return Command_FindSlowStrict(function, provider);
  1690. }
  1691. }
  1692. #else
  1693. if (Command_IsSorted())
  1694. {
  1695. return Command_FindFast(function, Command_FastHash(function));
  1696. }
  1697. else
  1698. {
  1699. return Command_FindSlow(function);
  1700. }
  1701. #endif
  1702. }
  1703.  
  1704. /**--------------------------------------------------------------------------**\
  1705. <summary>Command_GetID</summary>
  1706. <param name="function[]">Function name to find.</param>
  1707. <returns>
  1708. The ID of the passed function.
  1709. </returns>
  1710. <remarks>
  1711. -
  1712.  
  1713. native Command_GetID(function[])
  1714.  
  1715. </remarks>
  1716. \**--------------------------------------------------------------------------**/
  1717.  
  1718. foreign Command_GetID(string:function[]);
  1719.  
  1720. global Command_GetID(string:function[])
  1721. {
  1722. P:2("Command_GetID called: \"%s\"", function);
  1723. return Command_Find(function);
  1724. }
  1725.  
  1726. /**--------------------------------------------------------------------------**\
  1727. <summary>Command_SetPlayer</summary>
  1728. <param name="command">Command to set for.</param>
  1729. <param name="playerid">Player to set.</param>
  1730. <param name="bool:set">Wether or not this player can use this command.</param>
  1731. <returns>
  1732. -
  1733. </returns>
  1734. <remarks>
  1735. -
  1736.  
  1737. native bool:Command_SetPlayer(command, playerid, bool:set);
  1738.  
  1739. </remarks>
  1740. \**--------------------------------------------------------------------------**/
  1741.  
  1742. foreign void:Command_SetPlayer(c,p,bool:s);
  1743.  
  1744. global void:Command_SetPlayer(c,p,bool:s)
  1745. {
  1746. P:2("Command_SetPlayer called: %i, %i, %i", c, p, s);
  1747. // if (c < 0 || c >= YSI_g_sCommandIndex)
  1748. if (0 <= c < MAX_COMMANDS)
  1749. {
  1750. //Bit_Set(YSI_g_sCommands[c][E_COMMANDS_PLAYERS], p, s, bits<MAX_PLAYERS>);
  1751. //if (s) PA+(YSI_g_sCommands[c][E_COMMANDS_PLAYERS], p);
  1752. //else PA-(YSI_g_sCommands[c][E_COMMANDS_PLAYERS], p);
  1753. PA_Set(YSI_g_sCommands[c][E_COMMANDS_PLAYERS], p, s);
  1754. // Not in range,
  1755. // return false;
  1756. }
  1757. //return 1;
  1758. // return s;
  1759. }
  1760.  
  1761. /**--------------------------------------------------------------------------**\
  1762. <summary>Command_SetPlayerNamed</summary>
  1763. <param name="funcname[]">Command to set for.</param>
  1764. <param name="playerid">Player to set.</param>
  1765. <param name="set">Wether or not this player can use this command.</param>
  1766. <returns>
  1767. -
  1768. </returns>
  1769. <remarks>
  1770. Like Command_SetPlayer but for a function name.
  1771.  
  1772. native bool:Command_SetPlayerNamed(funcname[], playerid, bool:set);
  1773.  
  1774. </remarks>
  1775. \**--------------------------------------------------------------------------**/
  1776.  
  1777. foreign void:Command_SetPlayerNamed(string:f[],p,bool:s);
  1778.  
  1779. global void:Command_SetPlayerNamed(string:f[],p,bool:s)
  1780. {
  1781. P:2("Command_SetPlayerNamed called: \"%s\", %i, %i", f, p, s);
  1782. Command_SetPlayer(Command_Find(f), p, s);
  1783. //return 1;
  1784. }
  1785.  
  1786. /**--------------------------------------------------------------------------**\
  1787. <summary>Command_GetPlayer</summary>
  1788. <param name="command">Command to get for.</param>
  1789. <param name="playerid">Player to get.</param>
  1790. <returns>
  1791. Wether this player can use this command.
  1792. </returns>
  1793. <remarks>
  1794. -
  1795.  
  1796. native bool:Command_GetPlayer(command, playerid);
  1797.  
  1798. </remarks>
  1799. \**--------------------------------------------------------------------------**/
  1800.  
  1801. foreign bool:Command_GetPlayer(command, playerid);
  1802.  
  1803. global bool:Command_GetPlayer(command, playerid)
  1804. {
  1805. P:2("bool:Command_GetPlayer called: %i, %i", command, playerid);
  1806. if (0 <= command < MAX_COMMANDS)
  1807. {
  1808. return bool:Command_CheckPlayer(command, playerid);
  1809. }
  1810. // Not in range,
  1811. return false;
  1812. }
  1813.  
  1814. /**--------------------------------------------------------------------------**\
  1815. <summary>Command_GetPlayerNamed</summary>
  1816. <param name="funcname[]">Command to get for.</param>
  1817. <param name="playerid">Player to get.</param>
  1818. <returns>
  1819. -
  1820. </returns>
  1821. <remarks>
  1822. Like Command_GetPlayer but for a function name.
  1823.  
  1824. native bool:Command_GetPlayerNamed(funcname[], playerid);
  1825.  
  1826. </remarks>
  1827. \**--------------------------------------------------------------------------**/
  1828.  
  1829. foreign bool:Command_GetPlayerNamed(string:func[], playerid);
  1830.  
  1831. global bool:Command_GetPlayerNamed(string:func[], playerid)
  1832. {
  1833. P:2("bool:Command_GetPlayerNamed called: \"%s\", %i", func, playerid);
  1834. return Command_GetPlayer(Command_Find(func), playerid);
  1835. }
  1836.  
  1837. /**--------------------------------------------------------------------------**\
  1838. <summary>Command_Remove</summary>
  1839. <param name="func">The slot of the command to remove.</param>
  1840. <returns>
  1841. -
  1842. </returns>
  1843. <remarks>
  1844.  
  1845. native Command_Remove(func);
  1846.  
  1847. </remarks>
  1848. \**--------------------------------------------------------------------------**/
  1849.  
  1850. static stock Command_RemoveFromBintree(func)
  1851. {
  1852. P:4("Command_RemoveFromBintree called: %i", func);
  1853. if (!Command_IsSorted()) return;
  1854. // This function has to find the right index in the binary tree, as that's
  1855. // not in the same order at all.
  1856. new
  1857. leaf,
  1858. hash = Command_PackHash(Command_Name(func));
  1859. // Find where in the binary tree this is referenced from.
  1860. while (Bintree_FindValue(YSI_g_sSearchTree, hash, _, leaf) != BINTREE_NOT_FOUND)
  1861. {
  1862. if (YSI_g_sSearchTree[leaf][E_BINTREE_TREE_POINTER] == func)
  1863. {
  1864. P:2("Command_RemoveFromBintree: Delete branch");
  1865. Bintree_Delete(YSI_g_sSearchTree, leaf, YSI_g_sCommandCount);
  1866. return;
  1867. }
  1868. }
  1869. }
  1870.  
  1871. foreign void:Command_Remove(func);
  1872.  
  1873. global void:Command_Remove(func)
  1874. {
  1875. P:2("Command_Remove called: %i", func);
  1876. if (0 <= func < MAX_COMMANDS)
  1877. {
  1878. if (Command_GetPointer(func) == func)
  1879. {
  1880. for (new i = 0; i != MAX_COMMANDS; ++i)
  1881. {
  1882. // Remove all alternate names.
  1883. if (Command_GetPointer(i) == func)
  1884. {
  1885. // Add this to the list of unused functions.
  1886. YSI_g_sCommands[i][E_COMMANDS_FUNC_POINTER] = YSI_g_sCommandIndex;
  1887. YSI_g_sCommandIndex = i;
  1888. // Mark it as invalid in this instance.
  1889. #if YSIM_HAS_MASTER
  1890. YSI_g_sCommands[i][E_COMMANDS_MASTERS] = 0;
  1891. #endif
  1892. // Reduce the number of commands.
  1893. //Bintree_Delete(YSI_g_sSearchTree, i, YSI_g_sCommandCount);
  1894. Command_RemoveFromBintree(i);
  1895. --YSI_g_sCommandCount;
  1896. }
  1897. }
  1898. }
  1899. // Remove a single alternate name.
  1900. YSI_g_sCommands[func][E_COMMANDS_FUNC_POINTER] = YSI_g_sCommandIndex;
  1901. YSI_g_sCommandIndex = func;
  1902. #if YSIM_HAS_MASTER
  1903. YSI_g_sCommands[func][E_COMMANDS_MASTERS] = 0;
  1904. #endif
  1905. //Bintree_Delete(YSI_g_sSearchTree, func, YSI_g_sCommandCount);
  1906. Command_RemoveFromBintree(func);
  1907. --YSI_g_sCommandCount;
  1908. //return 1;
  1909. }
  1910. //return 0;
  1911. }
  1912.  
  1913. foreign void:Command_RemoveNamed(string:func[]);
  1914.  
  1915. global void:Command_RemoveNamed(string:func[])
  1916. {
  1917. P:2("Command_RemoveNamed called: \"%s\"", func);
  1918. Command_Remove(Command_Find(func));
  1919. }
  1920.  
  1921. /**--------------------------------------------------------------------------**\
  1922. <summary>Command_Add</summary>
  1923. <param name="funcname[]">The function to add to the array.</param>
  1924. <param name="script">The script ID with the function.</param>
  1925. <returns>
  1926. -
  1927. </returns>
  1928. <remarks>
  1929. If the list of commands have already been sorted into the binary tree the
  1930. new commands will be appended, otherwise they will just be added to the
  1931. array.
  1932.  
  1933. native Command_Add(funcname[], script);
  1934.  
  1935. </remarks>
  1936. \**--------------------------------------------------------------------------**/
  1937.  
  1938. foreign void:Command_Add(string:f[],s);
  1939.  
  1940. global void:Command_Add(string:f[],s)
  1941. {
  1942. #if !YSIM_HAS_MASTER
  1943. #pragma unused s
  1944. #endif
  1945. P:2("Command_Add called: \"%s\", %i", f, s);
  1946. if (YSI_g_sCommandCount < MAX_COMMANDS && YSI_g_sCommandIndex != -1)
  1947. {
  1948. #if !YSIM_HAS_MASTER
  1949. static
  1950. provider = -1;
  1951. #endif
  1952. new
  1953. #if YSIM_HAS_MASTER
  1954. provider = _:Command_DefaultProvider(),
  1955. #endif
  1956. //hash = Command_AddHashPacked(f, YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION], 4),
  1957. // Because of the way the master system works, packed strings are
  1958. // unpacked when they are passed remotely, but not when they are
  1959. // passed locally. The unpacking is done by "CallRemoteFunction",
  1960. // not anything we outselves do...
  1961. offset = f[0] == '@' ? 4 : 1,
  1962. hash = f[0] == '@' ?
  1963. Command_AddHash(f, YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION], 4) :
  1964. Command_AddHashPacked(f, YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION], 4);
  1965. //printf("%s", unpack(YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION][1]));
  1966. if (Command_IsSorted())
  1967. {
  1968. new
  1969. idx = Command_FindFastStrict(f[offset], hash, provider);
  1970. if (idx != COMMAND_NOT_FOUND)
  1971. {
  1972. P:4("Command_Add: Command exists");
  1973. #if YSIM_HAS_MASTER
  1974. YSI_g_sCommands[idx][E_COMMANDS_MASTERS] |= 1 << s;
  1975. #endif
  1976. return; //0;
  1977. }
  1978. if (!Command_HasCollisions())
  1979. {
  1980. // Check for an existing command with this hash.
  1981. if (Bintree_FindValue(YSI_g_sSearchTree, hash) != BINTREE_NOT_FOUND)
  1982. {
  1983. Command_SetCollisions();
  1984. }
  1985. }
  1986. // Command doesn't exist already - good!
  1987. Bintree_Add(YSI_g_sSearchTree, YSI_g_sCommandIndex, hash, YSI_g_sCommandIndex);
  1988. }
  1989. else
  1990. {
  1991. new
  1992. idx = Command_FindSlowStrict(f[offset], provider);
  1993. if (idx != COMMAND_NOT_FOUND)
  1994. {
  1995. P:4("Command_Add: Command exists");
  1996. #if YSIM_HAS_MASTER
  1997. YSI_g_sCommands[idx][E_COMMANDS_MASTERS] |= 1 << s;
  1998. #endif
  1999. return; //0;
  2000. }
  2001. }
  2002. YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION][0] = COMMAND_FUNCTION_PREFIX;
  2003. //Bit_SetAll(YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_PLAYERS], true, bits<MAX_PLAYERS>);
  2004. //Command_InitialiseFromGroups(YSI_g_sCommandIndex);
  2005. PA_FastInit(YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_PLAYERS]);
  2006. NO_GROUPS(YSI_g_sCommandIndex)
  2007. {
  2008. PA_Init(YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_PLAYERS], true);
  2009. }
  2010. // Swap these.
  2011. #if YSIM_HAS_MASTER
  2012. YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_MASTERS] = 1 << s;
  2013. #endif
  2014. P:2("Command_Add: Command added in %d (%d)", YSI_g_sCommandIndex, hash);
  2015. hash = YSI_g_sCommandIndex;
  2016. // Set this command as usable in any script.
  2017. P:4("Command_Add: %08x%08x%08x%08x", YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION][0], YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION][1], YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION][2], YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION][3]);
  2018. YSI_g_sCommandIndex = YSI_g_sCommands[hash][E_COMMANDS_FUNC_POINTER];
  2019. #if YSIM_HAS_MASTER
  2020. YSI_g_sCommands[hash][E_COMMANDS_FUNC_POINTER] = hash | (provider << 24);
  2021. #else
  2022. YSI_g_sCommands[hash][E_COMMANDS_FUNC_POINTER] = hash;
  2023. #endif
  2024. /*#if YSIM_HAS_MASTER
  2025. if (s == _@)
  2026. #endif
  2027. {
  2028. new
  2029. addr = funcidx(f) * 8 + AMX_HEADER_PUBLICS;
  2030. #emit LREF.S.pri addr
  2031. #emit STOR.S.pri addr
  2032. YSI_g_sCommands[hash][E_COMMANDS_AMX_ADDRESS] = addr;
  2033. }
  2034. #pragma tabsize 4*/
  2035. // Now some complex debug rubbish.
  2036. C:4(new str[32];strpack(str, f);printf("Command_Add: %08x%08x%08x%08x", str[0], str[1], str[2], str[3]););
  2037. ++YSI_g_sCommandCount;
  2038. }
  2039. //return 1;
  2040. /*else
  2041. {
  2042. // Not all hope is lost - check if this command name already exists.
  2043. if (Command_IsSorted())
  2044. {
  2045. new
  2046. pos = Command_FindFast(funcname[4], Command_FastHash(funcname[4]));
  2047. if (pos != COMMAND_NOT_FOUND)
  2048. {
  2049. // Found it already in the array.
  2050. return pos;
  2051. }
  2052. }
  2053. else
  2054. {
  2055. new
  2056. pos = Command_FindSlow(funcname[4]);
  2057. if (pos != COMMAND_NOT_FOUND)
  2058. {
  2059. // Command already exists.
  2060. return pos;
  2061. }
  2062. }
  2063. }
  2064. return COMMAND_NOT_FOUND;*/
  2065. }
  2066.  
  2067. /**--------------------------------------------------------------------------**\
  2068. <summary>Command_AddAlt</summary>
  2069. <param name="funcidx">The function this is an alternate to.</param>
  2070. <param name="altname[]">The new name.</param>
  2071. <returns>
  2072. -
  2073. </returns>
  2074. <remarks>
  2075. If the list of commands have already been sorted into the binary tree the
  2076. new commands will be appended, otherwise they will just be added to the
  2077. array.
  2078.  
  2079. native Command_AddAlt(funcidx, altname[]);
  2080.  
  2081. </remarks>
  2082. \**--------------------------------------------------------------------------**/
  2083.  
  2084. foreign Command_AddAlt(oidx, string:altname[]);
  2085.  
  2086. global Command_AddAlt(oidx, string:altname[])
  2087. {
  2088. P:2("Command_AddAlt called: %i, \"%s\"", oidx, altname);
  2089. if (!Command_IsValid(oidx))
  2090. {
  2091. return COMMAND_NOT_FOUND;
  2092. }
  2093. #if YSIM_HAS_MASTER
  2094. new
  2095. provider = _:Command_DefaultProvider();
  2096. #else
  2097. static
  2098. provider = -1;
  2099. #endif
  2100. if (YSI_g_sCommandCount < MAX_COMMANDS && YSI_g_sCommandIndex != -1)
  2101. {
  2102. new
  2103. hash = Command_AddHash(altname, YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION], 0);
  2104. if (Command_IsSorted())
  2105. {
  2106. // Find the function this mirrors.
  2107. //oidx = Command_FindFast(function, Command_FastHash(function));
  2108. new
  2109. pos = Command_FindFastStrict(altname, hash, provider);
  2110. if (pos != COMMAND_NOT_FOUND)
  2111. {
  2112. if (Command_GetPointer(pos) != oidx)
  2113. {
  2114. // Same altname, different function.
  2115. return COMMAND_NOT_FOUND;
  2116. }
  2117. return pos;
  2118. }
  2119. if (!Command_HasCollisions())
  2120. {
  2121. // Check for an existing command with this hash.
  2122. //if (Bintree_FindValue(YSI_g_sSearchTree, hash) != BINTREE_NOT_FOUND)
  2123. //{
  2124. // Command_SetCollisions();
  2125. //}
  2126. new
  2127. leaf;
  2128. while ((pos = Bintree_FindValue(YSI_g_sSearchTree, hash, leaf)) != BINTREE_NOT_FOUND)
  2129. {
  2130. // Don't have collisions if the providers are different.
  2131. if ((YSI_g_sCommands[pos][E_COMMANDS_FUNC_POINTER] >>> 24) != provider) continue;
  2132. Command_SetCollisions();
  2133. }
  2134. }
  2135. // Command doesn't exist already - good!
  2136. Bintree_Add(YSI_g_sSearchTree, YSI_g_sCommandIndex, hash, YSI_g_sCommandIndex);
  2137. }
  2138. else
  2139. {
  2140. new
  2141. pos = Command_FindSlowStrict(altname, provider);
  2142. if (pos != COMMAND_NOT_FOUND)
  2143. {
  2144. if (Command_GetPointer(pos) != oidx)
  2145. {
  2146. // Same altname, different function.
  2147. return COMMAND_NOT_FOUND;
  2148. }
  2149. // Command already exists.
  2150. return pos;
  2151. }
  2152. }
  2153. YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_FUNCTION][0] = COMMAND_FUNCTION_PREFIX;
  2154. //Bit_SetAll(YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_PLAYERS], true, bits<MAX_PLAYERS>);
  2155. //Command_InitialiseFromGroups(YSI_g_sCommandIndex);
  2156. PA_FastInit(YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_PLAYERS]);
  2157. NO_GROUPS(YSI_g_sCommandIndex)
  2158. {
  2159. PA_Init(YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_PLAYERS], true);
  2160. }
  2161. ++YSI_g_sCommandCount;
  2162. #if YSIM_HAS_MASTER
  2163. // This doesn't have real masters.
  2164. YSI_g_sCommands[YSI_g_sCommandIndex][E_COMMANDS_MASTERS] = -1;
  2165. #endif
  2166. P:2("Command_AddAlt: Command added in %d as %d", YSI_g_sCommandIndex, oidx);
  2167. hash = YSI_g_sCommandIndex;
  2168. YSI_g_sCommandIndex = YSI_g_sCommands[hash][E_COMMANDS_FUNC_POINTER];
  2169. #if YSIM_HAS_MASTER
  2170. YSI_g_sCommands[hash][E_COMMANDS_FUNC_POINTER] = oidx | (provider << 24);
  2171. #else
  2172. YSI_g_sCommands[hash][E_COMMANDS_FUNC_POINTER] = oidx;
  2173. #endif
  2174. return hash;
  2175. }
  2176. else
  2177. {
  2178. // Not all hope is lost - check if this command name already exists.
  2179. new
  2180. pos;
  2181. if (Command_IsSorted())
  2182. {
  2183. pos = Command_FindFastStrict(altname, Command_FastHash(altname), provider);
  2184. }
  2185. else
  2186. {
  2187. pos = Command_FindSlowStrict(altname, provider);
  2188. }
  2189. if (pos != COMMAND_NOT_FOUND)
  2190. {
  2191. // Found it already in the array. Check if the element it points to
  2192. // has the correct name so we know this is the correct pair, not
  2193. // just the correct single element. I.e. check that this is the
  2194. // correct original/altname combo, not the right altname on a
  2195. // different original name.
  2196. if (oidx == Command_GetPointer(pos))
  2197. {
  2198. return pos;
  2199. }
  2200. }
  2201. }
  2202. return COMMAND_NOT_FOUND;
  2203. }
  2204.  
  2205. /**--------------------------------------------------------------------------**\
  2206. <summary>Command_AddAltNamed</summary>
  2207. <param name="function[]">The function this is an alternate to.</param>
  2208. <param name="altname[]">The new name.</param>
  2209. <returns>
  2210. -
  2211. </returns>
  2212. <remarks>
  2213. Add an alternate command for an existing command.
  2214.  
  2215. native Command_AddAltNamed(function[], altname[]);
  2216.  
  2217. </remarks>
  2218. \**--------------------------------------------------------------------------**/
  2219.  
  2220. foreign Command_AddAltNamed(string:function[], string:altname[]);
  2221.  
  2222. global Command_AddAltNamed(string:function[], string:altname[])
  2223. {
  2224. P:2("Command_AddAltNamed called: \"%s\", \"%s\"", function, altname);
  2225. return Command_AddAlt(Command_Find(function), altname);
  2226. }
  2227.  
  2228. /**--------------------------------------------------------------------------**\
  2229. <summary>Command_Debug</summary>
  2230. <returns>
  2231. -
  2232. </returns>
  2233. <remarks>
  2234. Print some random information about commands if _DEBUG is set.
  2235. </remarks>
  2236. \**--------------------------------------------------------------------------**/
  2237.  
  2238. stock Command_Debug()
  2239. {
  2240. P:3("Command_Debug called: %i");
  2241. #if _DEBUG > 0
  2242. printf("Command_Debug: Start");
  2243. for (new i = 0; i != MAX_COMMANDS; ++i)
  2244. {
  2245. if (Command_IsValid(i))
  2246. {
  2247. printf("Command_Debug: Loop start %d", i);
  2248. new buffer[MAX_COMMAND_LENGTH];
  2249. strunpack(buffer, Command_Name(i));
  2250. printf("%08x%08x%08x", YSI_g_sCommands[i][E_COMMANDS_FUNCTION][0], YSI_g_sCommands[i][E_COMMANDS_FUNCTION][1], YSI_g_sCommands[i][E_COMMANDS_FUNCTION][2]);
  2251. new pointer = Command_GetPointer(i);
  2252. printf("Command %d:", i);
  2253. printf("\t%s", buffer);
  2254. printf("\t%d", pointer);
  2255. printf("\t%d %d %d", YSI_g_sSearchTree[i][E_BINTREE_TREE_LEFT], YSI_g_sSearchTree[i][E_BINTREE_TREE_RIGHT], YSI_g_sSearchTree[i][E_BINTREE_TREE_VALUE]);
  2256. CallLocalFunction(Command_GetFunction(Command_GetPointer(i)), "isi", 0, "hi", 0);
  2257. printf("Command_Debug: Loop end");
  2258. }
  2259. }
  2260. printf("Command_Debug: End");
  2261. #endif
  2262. }
  2263.  
  2264. /**--------------------------------------------------------------------------**\
  2265. <summary>OnPlayerCommandText</summary>
  2266. <param name="playerid">Player who entered the command.</param>
  2267. <param name="cmdtext[]">Text entered.</param>
  2268. <returns>
  2269. true - success or hidden fail.
  2270. false - fail.
  2271. </returns>
  2272. <remarks>
  2273. Calls the Command_Process function if this is not a client.
  2274. </remarks>
  2275. \**--------------------------------------------------------------------------**/
  2276.  
  2277. #if YSIM_NOT_CLIENT
  2278. mhook OnPlayerCommandText(playerid, cmdtext[])
  2279. {
  2280. P:1("Commands_OnPlayerCommandText");
  2281. if (isnull(cmdtext)) return Command_UnknownReturn();
  2282. if (YSI_g_sCommandFlags & e_COMM_FLAG_OPCR)
  2283. {
  2284. switch (CallRemoteFunction("OnPlayerCommandReceived", "is", playerid, cmdtext))
  2285. {
  2286. case 0:
  2287. // Handle normally, as in ZCMD.
  2288. return 1;
  2289. case -1:
  2290. // Allow them to stop processing but return 0.
  2291. return 0;
  2292. // Do nothing on 1.
  2293. }
  2294. }
  2295. if (YSI_g_sCommandFlags & e_COMM_FLAG_OPCP)
  2296. {
  2297. new
  2298. ret = Command_Process(playerid, cmdtext, 0);
  2299. switch (CallRemoteFunction("OnPlayerCommandPerformed", "isi", playerid, cmdtext, ret))
  2300. {
  2301. case 0:
  2302. return 0;
  2303. case 1:
  2304. return 1;
  2305. //default:
  2306. // Return the original return on -1.
  2307. }
  2308. return ret;
  2309. }
  2310. else
  2311. {
  2312. /*switch (Command_Process(playerid, cmdtext, 0))
  2313. {
  2314. case 1:
  2315. // Found the command, processed.
  2316. return 1;
  2317. case -1:
  2318. // Found the command, but want to return 0 anyway.
  2319. return 0;
  2320. // Do nothing on 0.
  2321. }*/
  2322. return Command_Process(playerid, cmdtext, 0);
  2323. }
  2324. // This can never actually be reached!
  2325. //return 0;
  2326. }
  2327. #endif
  2328.  
  2329. /**--------------------------------------------------------------------------**\
  2330. <summary>Command_ReProcess</summary>
  2331. <param name="playerid">Player who entered the command.</param>
  2332. <param name="cmdtext[]">Text entered.</param>
  2333. <param name="help">Called from the help commmand or OnPlayerCommandText.</param>
  2334. <returns>
  2335. true - success or hidden fail.
  2336. false - fail.
  2337. </returns>
  2338. <remarks>
  2339. -
  2340. </remarks>
  2341. \**--------------------------------------------------------------------------**/
  2342.  
  2343. foreign Command_ReProcess(p,string:c[],h);
  2344.  
  2345. global Command_ReProcess(p,string:c[],h)
  2346. {
  2347. P:2("Command_ReProcess called: %i, \"%s\", %i", p, c, h);
  2348. return Command_Process(p,c,h);
  2349. }
  2350.  
  2351. /**--------------------------------------------------------------------------**\
  2352. <summary>Command_Process</summary>
  2353. <param name="playerid">Player who entered the command.</param>
  2354. <param name="cmdtext[]">Text entered.</param>
  2355. <param name="help">Called from the help commmand or OnPlayerCommandText.</param>
  2356. <returns>
  2357. true - success or hidden fail.
  2358. false - fail.
  2359. </returns>
  2360. <remarks>
  2361. -
  2362. </remarks>
  2363. \**--------------------------------------------------------------------------**/
  2364.  
  2365. #define Command_CallR(%1,%2) \
  2366. CallRemoteFunction(Command_GetFuncName((%1)), "isii", playerid, %2, help, script)
  2367.  
  2368. #define Command_CallL(%1,%2) \
  2369. CallLocalFunction(Command_GetFuncName((%1)), "isi", playerid, %2, help)
  2370.  
  2371. static stock Command_Process(playerid, cmdtext[], help)
  2372. {
  2373. P:4("Command_Process FS called: %i, \"%s\", %i", playerid, cmdtext, help);
  2374. // Support for very old problems!
  2375. // TODO: Add back.
  2376. P:2("Command_Process called: %d %s", playerid, cmdtext);
  2377. /*#if !_DEBUG && !defined _YSI_SPECIAL_DEBUG
  2378. // For testing purposes.
  2379. if (!IsPlayerConnected(playerid))
  2380. {
  2381. return Command_DisconnectReturn();
  2382. }
  2383. #endif*/
  2384. P:4("Command_Process: Connected");
  2385. new
  2386. idx,
  2387. prelen = help ? 0 : 1,
  2388. index = prelen;
  2389. /*// Shortcuts.
  2390. if (cmdtext[2] <= ' ')
  2391. {
  2392. // Get a player's shortcut information for this letter.
  2393. }
  2394. else*/
  2395. {
  2396. // No more faffing about with random alternate code - it's all done in
  2397. // one nice function instead of having to handle both separately.
  2398. new
  2399. length,
  2400. hash = Command_Hash(cmdtext, index, length);
  2401. P:2("Command_Process: Hash = %d, Length = %d", hash, length);
  2402. // NOTE: No prefix support here.
  2403. cmdtext[length + prelen] = '\0';
  2404. #if YSIM_HAS_MASTER
  2405. length = YSI_g_sPlayerProvider{playerid};
  2406. P:2("Command_Process: Provider: %d", length);
  2407. idx = Command_FindFast(cmdtext[prelen], hash, length);
  2408. if (idx == COMMAND_NOT_FOUND && length != 0xFF)
  2409. {
  2410. // Check default commands if no specialised commands were found.
  2411. idx = Command_FindFast(cmdtext[prelen], hash);
  2412. }
  2413. #else
  2414. idx = Command_FindFast(cmdtext[prelen], hash);
  2415. #endif
  2416. // TODO: Replace!
  2417. //idx = Command_FindSlow(cmdtext[prelen]);//, hash);
  2418. }
  2419. P:2("Command_Process: Index = %d", idx);
  2420. if (idx != COMMAND_NOT_FOUND)
  2421. {
  2422. // Get the master data for the underlying command, not the possibly
  2423. // changed name.
  2424. new
  2425. pointer = Command_GetPointer(idx);
  2426. P:4("Command_Process: Found %d, %d", idx, pointer);
  2427. // Found a command with this name - check the permissions.
  2428. //if (Bit_Get(YSI_g_sCommands[idx][E_COMMANDS_PLAYERS], playerid))
  2429. if (Command_CheckPlayer(idx, playerid))
  2430. {
  2431. P:4("Command_Process: Allowed");
  2432. // Allowed to use the command, get the real function. Note that
  2433. // this may well be the same as "idx", but we loose no time.
  2434. #if YSIM_HAS_MASTER
  2435. P:4("Command_Process: %08x%08x%08x%08x", YSI_g_sCommands[idx][E_COMMANDS_FUNCTION][0], YSI_g_sCommands[idx][E_COMMANDS_FUNCTION][1], YSI_g_sCommands[idx][E_COMMANDS_FUNCTION][2], YSI_g_sCommands[idx][E_COMMANDS_FUNCTION][3]);
  2436. //if (master & 1 << YSI_g_sMaster23)
  2437. //{
  2438. // master = YSI_g_sMaster23;
  2439. //}
  2440. //else
  2441. //{
  2442. new
  2443. script = Command_Provider(idx);
  2444. if (script == 0xFF)
  2445. {
  2446. script = YSI_g_sCommands[pointer][E_COMMANDS_MASTERS];
  2447. if (!script)
  2448. {
  2449. // No scripts can serve the code.
  2450. return Command_UnknownReturn();
  2451. }
  2452. // Find the lowest set bit. We use this to broadcastfunc the
  2453. // command. If it uses MASTER 23, this will select only
  2454. // one script to use. If it doesn't use MASTER 23, this is
  2455. // ignored as a parameter and _YCM is used instead.
  2456. static const
  2457. scDeBruijn[] =
  2458. {
  2459. 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
  2460. 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
  2461. };
  2462. // http://supertech.csail.mit.edu/papers/debruijn.pdf
  2463. script = scDeBruijn[((script & -script) * 0x077CB531) >>> 27];
  2464. }
  2465. //}
  2466. P:2("Command_Process: script = %d", script);
  2467. //if (master == _@)
  2468. //{
  2469. /*#endif
  2470. // This script has it!
  2471. addr = YSI_g_sCommands[pointer][E_COMMANDS_AMX_ADDRESS];
  2472. P:4("Command_Process: %04x%04x", addr >>> 16, addr & 0xFFFF);
  2473. // Make sure all the "#emits" are together as they don't
  2474. // seem to be affected by pre-processor directives.
  2475. #emit PUSH.S master
  2476. #emit PUSH.S help
  2477. #emit LOAD.S.alt cmdtext
  2478. #emit LOAD.S.pri index
  2479. #emit SMUL.C 4
  2480. #emit ADD
  2481. #emit PUSH.pri
  2482. #emit PUSH.S playerid
  2483. #emit PUSH.C 16
  2484. #emit LCTRL 6
  2485. #emit ADD.C 28
  2486. #emit PUSH.pri
  2487. #emit LOAD.S.pri addr
  2488. #emit SCTRL 6
  2489. #emit STOR.S.pri addr
  2490. #if YSIM_HAS_MASTER*/
  2491. //}
  2492. //else
  2493. //{
  2494. #pragma tabsize 4
  2495. P:2("Command_Process: call %s (%d, %d)", unpack(Command_GetFuncName(pointer)), pointer, script);
  2496. if (cmdtext[index])
  2497. {
  2498. // Call it!
  2499. Command_CallR(pointer, cmdtext[index]);
  2500. }
  2501. else
  2502. {
  2503. Command_CallR(pointer, NULL);
  2504. }
  2505. #pragma tabsize 4
  2506. //}
  2507. // Get the real return.
  2508. P:1("Command_Process: return: %d", getproperty(8, YSIM_RETURN));
  2509. return getproperty(8, YSIM_RETURN);
  2510. #else
  2511. if (cmdtext[index])
  2512. {
  2513. // Call it!
  2514. return Command_CallL(pointer, cmdtext[index]);
  2515. }
  2516. else
  2517. {
  2518. return Command_CallL(pointer, NULL);
  2519. }
  2520. //return addr;
  2521. #endif
  2522. }
  2523. else
  2524. {
  2525. return Command_DeniedReturn();
  2526. }
  2527. }
  2528. P:5("Command_Process: Not found");
  2529. #pragma tabsize 4
  2530. return Command_UnknownReturn();
  2531. }
  2532.  
  2533. /**--------------------------------------------------------------------------**\
  2534. <summary>Command_GetName</summary>
  2535. <param name="funcid">Command to get the name of.</param>
  2536. <returns>
  2537. -
  2538. </returns>
  2539. <remarks>
  2540. -
  2541.  
  2542. native Command_GetName(funcid);
  2543.  
  2544. </remarks>
  2545. \**--------------------------------------------------------------------------**/
  2546.  
  2547. foreign string:Command_GetName(funcid);
  2548.  
  2549. global string:Command_GetName(funcid)
  2550. {
  2551. P:2("Command_GetName called: %i", funcid);
  2552. new
  2553. buffer[YSI_MAX_STRING] = "";
  2554. if (Command_IsValid(funcid))
  2555. {
  2556. strunpack(buffer, Command_Name(funcid));
  2557. }
  2558. return buffer;
  2559. }
  2560.  
  2561. /**--------------------------------------------------------------------------**\
  2562. <summary>Command_GetDisplay</summary>
  2563. <param name="f">Command to get the real name of.</param>
  2564. <param name="p">Player to get the name for.</param>
  2565. <returns>
  2566. The name of a command for a single player.
  2567. </returns>
  2568. <remarks>
  2569. -
  2570.  
  2571. native Command_GetDisplay(funcid, playerid);
  2572.  
  2573. </remarks>
  2574. \**--------------------------------------------------------------------------**/
  2575.  
  2576. foreign string:Command_GetDisplay(funcid, playerid);
  2577.  
  2578. global string:Command_GetDisplay(funcid, playerid)
  2579. {
  2580. P:2("Command_GetDisplay called: %i, %i", funcid, playerid);
  2581. new
  2582. buffer[YSI_MAX_STRING] = "";
  2583. if (Command_IsValid(funcid))
  2584. {
  2585. // Don't recalculate this every loop.
  2586. new
  2587. slot = Bit_Slot(playerid) + 1,
  2588. Bit:mask = Bit_Mask(playerid);
  2589. // Check if they can use the original version.
  2590. if (YSI_g_sCommands[funcid][E_COMMANDS_PLAYERS][slot] & mask)
  2591. {
  2592. //setproperty(8, "", YSIM_STRING, Command_Name(f));
  2593. strunpack(buffer, Command_Name(funcid));
  2594. return buffer;
  2595. //return 1;
  2596. }
  2597. // Search for a command pointing to that command which the player can use.
  2598. for (new i = 0; i != MAX_COMMANDS; ++i)
  2599. {
  2600. if (Command_GetPointer(i) == funcid && (YSI_g_sCommands[i][E_COMMANDS_PLAYERS][slot] & mask))
  2601. {
  2602. //setproperty(8, "", YSIM_STRING, Command_Name(i));
  2603. strunpack(buffer, Command_Name(i));
  2604. return buffer;
  2605. }
  2606. }
  2607. }
  2608. return buffer;
  2609. }
  2610.  
  2611. /**--------------------------------------------------------------------------**\
  2612. <summary>Command_GetDisplayNamed</summary>
  2613. <param name="f[]">Command to get the real name of.</param>
  2614. <param name="p">Player to get the name for.</param>
  2615. <returns>
  2616. The name of a named function for one player.
  2617. </returns>
  2618. <remarks>
  2619. Remote function call for Command_GetDisplayNameNamed - avoids needing to
  2620. expose users to the master system's odd way of returning strings. This is
  2621. the only part I've not yet fixed up to be nice and hidden.
  2622.  
  2623. native string:Command_GetDisplayNamed(string:funcid[], playerid);
  2624.  
  2625. </remarks>
  2626. \**--------------------------------------------------------------------------**/
  2627.  
  2628. foreign string:Command_GetDisplayNamed(string:func[], playerid);
  2629.  
  2630. global string:Command_GetDisplayNamed(string:func[], playerid)
  2631. {
  2632. P:1("Command_GetDisplayNamed called: \"%s\", %i", func, playerid);
  2633. new
  2634. pointer = Command_Find(func),
  2635. buffer[YSI_MAX_STRING] = "";
  2636. if (pointer != COMMAND_NOT_FOUND)
  2637. {
  2638. // Don't recalculate this every loop.
  2639. new
  2640. slot = Bit_Slot(playerid) + 1, //playerid >>> CELLSHIFT) + 1,
  2641. Bit:mask = Bit_Mask(playerid); //Bit:(1 << (playerid & (cellbits - 1)));
  2642. // Check if they can use the original version.
  2643. if (YSI_g_sCommands[pointer][E_COMMANDS_PLAYERS][slot] & mask)
  2644. {
  2645. //setproperty(8, "", YSIM_STRING, Command_Name(pointer));
  2646. strunpack(buffer, Command_Name(pointer));
  2647. return buffer;
  2648. }
  2649. // Search for a command pointing to that command which the player can use.
  2650. for (new i = 0; i != MAX_COMMANDS; ++i)
  2651. {
  2652. if (Command_GetPointer(i) == pointer && (YSI_g_sCommands[i][E_COMMANDS_PLAYERS][slot] & mask))
  2653. {
  2654. //setproperty(8, "", YSIM_STRING, Command_Name(i));
  2655. strunpack(buffer, Command_Name(i));
  2656. return buffer;
  2657. }
  2658. }
  2659. }
  2660. return buffer;
  2661. }
  2662.  
  2663. /**--------------------------------------------------------------------------**\
  2664. <summary>Command_GetPlayerCommandCount</summary>
  2665. <param name="playerid">Player to count for.</param>
  2666. <returns>
  2667. -
  2668. </returns>
  2669. <remarks>
  2670. Gets the number of comamnds this player can use.
  2671.  
  2672. native Command_GetPlayerCommandCount(playerid);
  2673.  
  2674. </remarks>
  2675. \**--------------------------------------------------------------------------**/
  2676.  
  2677. foreign Command_GetPlayerCommandCount(playerid);
  2678.  
  2679. global Command_GetPlayerCommandCount(playerid)
  2680. {
  2681. P:2("Command_GetPlayerCommandCount called: %i", playerid);
  2682. new
  2683. slot = Bit_Slot(playerid) + 1,
  2684. Bit:mask = Bit_Mask(playerid),
  2685. count = 0;
  2686. for (new i = 0; i != MAX_COMMANDS; ++i)
  2687. {
  2688. if (_Command_IsValid(i) && YSI_g_sCommands[i][E_COMMANDS_PLAYERS][slot] & mask)
  2689. {
  2690. ++count;
  2691. }
  2692. }
  2693. return count;
  2694. }
  2695.  
  2696. /**--------------------------------------------------------------------------**\
  2697. <summary>Command_GetNext</summary>
  2698. <param name="index">Index of the next command for this player.</param>
  2699. <param name="playerid">Player to get the name for.</param>
  2700. <returns>
  2701. The name of a command for a single player.
  2702. </returns>
  2703. <remarks>
  2704. -
  2705.  
  2706. native Command_GetNext(index, playerid);
  2707.  
  2708. </remarks>
  2709. \**--------------------------------------------------------------------------**/
  2710.  
  2711. foreign string:Command_GetNext(index, playerid);
  2712.  
  2713. global string:Command_GetNext(index, playerid)
  2714. {
  2715. P:2("Command_GetNext called: %i, %i", index, playerid);
  2716. new
  2717. buffer[YSI_MAX_STRING] = "";
  2718. if (0 <= index < MAX_COMMANDS)
  2719. {
  2720. // Don't recalculate this every loop.
  2721. new
  2722. slot = Bit_Slot(playerid) + 1,
  2723. Bit:mask = Bit_Mask(playerid);
  2724. for (new i = 0; i != MAX_COMMANDS; ++i)
  2725. {
  2726. if (_Command_IsValid(i) && YSI_g_sCommands[i][E_COMMANDS_PLAYERS][slot] & mask)
  2727. {
  2728. // Skip already displayed ones.
  2729. if (index)
  2730. {
  2731. --index;
  2732. }
  2733. else
  2734. {
  2735. strunpack(buffer, Command_Name(i));
  2736. return buffer;
  2737. }
  2738. }
  2739. }
  2740. }
  2741. return buffer;
  2742. }
  2743.  
  2744. //#tryinclude <YSI\y_groups>
  2745.  
  2746. //#undef _YCM
  2747.  
  2748. // This is to allow callback chaining. When the user includes y_groups and
  2749. // other libraries, the function names will be reset to their custom ones after
  2750. // every inclusion, however if you then include another YSI library you need to
  2751. // revert to the previous library names to get the chaining to work. However I
  2752. // think this is completely pointless now thanks to "y_hooks".
  2753.  
  2754. //#define YSI_SET_LAST_GROUP 25
  2755. #include "internal\y_grouprevert"
  2756.  
  2757. // So we can use custom syntax on these two functions and allow command names to
  2758. // be sort of used...
  2759. //#define Group_SetCommand(%0,YCMD:%2,%3) Group_SetCommand(%0,Command_GetID(#%2),%3)
  2760. //#define Group_SetGlobalCommand(YCMD:%2,%3) Group_SetGlobalCommand(Command_GetID(#%2),%3)
Advertisement
Add Comment
Please, Sign In to add comment