hook.bmx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. Strict
  2. Rem
  3. bbdoc: Events/Hook functions
  4. End Rem
  5. Module BRL.Hook
  6. ModuleInfo "Version: 1.01"
  7. ModuleInfo "Author: Mark Sibly"
  8. ModuleInfo "License: zlib/libpng"
  9. ModuleInfo "Copyright: Blitz Research Ltd"
  10. ModuleInfo "Modserver: BRL"
  11. ModuleInfo "History: 1.01 Release"
  12. ModuleInfo "History: Added Context parameter to RemoveHook function"
  13. Private
  14. Type THook
  15. Field succ:THook
  16. Field priority
  17. Field func:Object( id,data:Object,context:Object )
  18. Field context:Object
  19. End Type
  20. Global hooks:THook[256]
  21. Public
  22. Rem
  23. bbdoc: Allocate a hook id
  24. returns: An integer hook id
  25. about:
  26. The returned hook id can be used with #AddHook, #RunHooks and #RemoveHook.
  27. end rem
  28. Function AllocHookId()
  29. Global id=-1
  30. id:+1
  31. If id>255 Throw "Too many hook ids"
  32. Return id
  33. End Function
  34. Rem
  35. bbdoc: Add a hook function
  36. returns: A hook object that can be used with the #RemoveHook command.
  37. about:
  38. Add a hook function to be executed when #RunHooks is called with the specified hook @id.
  39. End Rem
  40. Function AddHook( id,func:Object( id,data:Object,context:Object ),context:Object=Null,priority=0 )
  41. Local t:THook=New THook
  42. t.priority=priority
  43. t.func=func
  44. t.context=context
  45. Local pred:THook
  46. Local hook:THook=hooks[id]
  47. While hook
  48. If priority>hook.priority Exit
  49. pred=hook
  50. hook=hook.succ
  51. Wend
  52. If pred
  53. t.succ=pred.succ
  54. pred.succ=t
  55. Else
  56. t.succ=hooks[id]
  57. hooks[id]=t
  58. EndIf
  59. End Function
  60. Rem
  61. bbdoc: Run hook functions
  62. returns: The data produced by the last hook function
  63. about:
  64. #RunHooks runs all hook functions that have been added for the specified hook @id.
  65. The first hook function is called with the provided @data object. The object returned by
  66. this function is then passed as the @data parameter to the next hook function and so on.
  67. Therefore, hook functions should generally return the @data parameter when finished.
  68. End Rem
  69. Function RunHooks:Object( id,data:Object )
  70. Local hook:THook=hooks[id]
  71. While hook
  72. data=hook.Func( id,data,hook.context )
  73. hook=hook.succ
  74. Wend
  75. Return data
  76. End Function
  77. Rem
  78. bbdoc:Remove a hook function
  79. about:
  80. Removes the hook function specified by @id, @func and @context.
  81. End Rem
  82. Function RemoveHook( id,func:Object( id,data:Object,context:Object ),context:Object=Null )
  83. Local pred:THook
  84. Local hook:THook=hooks[id]
  85. While hook And (hook.func<>func Or hook.context<>context)
  86. pred=hook
  87. hook=hook.succ
  88. Wend
  89. If Not hook Return
  90. If pred
  91. pred.succ=hook.succ
  92. Else
  93. hooks[id]=hook.succ
  94. EndIf
  95. End Function