y_commands.inc 76 KB

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