ToCppHook.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. --
  2. -- Copyright (c) 2008-2015 the Urho3D project.
  3. --
  4. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  5. -- of this software and associated documentation files (the "Software"), to deal
  6. -- in the Software without restriction, including without limitation the rights
  7. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. -- copies of the Software, and to permit persons to whom the Software is
  9. -- furnished to do so, subject to the following conditions:
  10. --
  11. -- The above copyright notice and this permission notice shall be included in
  12. -- all copies or substantial portions of the Software.
  13. --
  14. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. -- THE SOFTWARE.
  21. --
  22. local toWrite = {}
  23. local currentString = ''
  24. local out
  25. local WRITE, OUTPUT = write, output
  26. function output(s)
  27. out = _OUTPUT
  28. output = OUTPUT -- restore
  29. output(s)
  30. end
  31. function write(a)
  32. if out == _OUTPUT then
  33. currentString = currentString .. a
  34. if string.sub(currentString,-1) == '\n' then
  35. toWrite[#toWrite+1] = currentString
  36. currentString = ''
  37. end
  38. else
  39. WRITE(a)
  40. end
  41. end
  42. function post_output_hook(package)
  43. local result = table.concat(toWrite)
  44. local function replace(pattern, replacement)
  45. local k = 0
  46. local nxt, currentString = 1, ''
  47. repeat
  48. local s, e = string.find(result, pattern, nxt, true)
  49. if e then
  50. currentString = currentString .. string.sub(result, nxt, s-1) .. replacement
  51. nxt = e + 1
  52. k = k + 1
  53. end
  54. until not e
  55. result = currentString..string.sub(result, nxt)
  56. --if k == 0 then print('Pattern not replaced', pattern) end
  57. end
  58. replace("\t", " ")
  59. replace([[#ifndef __cplusplus
  60. #include "stdlib.h"
  61. #endif
  62. #include "string.h"
  63. #include "tolua++.h"]], [[//
  64. // Copyright (c) 2008-2015 the Urho3D project.
  65. //
  66. // Permission is hereby granted, free of charge, to any person obtaining a copy
  67. // of this software and associated documentation files (the "Software"), to deal
  68. // in the Software without restriction, including without limitation the rights
  69. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  70. // copies of the Software, and to permit persons to whom the Software is
  71. // furnished to do so, subject to the following conditions:
  72. //
  73. // The above copyright notice and this permission notice shall be included in
  74. // all copies or substantial portions of the Software.
  75. //
  76. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  77. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  78. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  79. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  80. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  81. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  82. // THE SOFTWARE.
  83. //
  84. #include <toluapp/tolua++.h>
  85. #include "LuaScript/ToluaUtils.h"
  86. #if __clang__
  87. #pragma clang diagnostic push
  88. #pragma clang diagnostic ignored "-Wunused-function"
  89. #endif]])
  90. WRITE(result)
  91. WRITE([[
  92. #if __clang__
  93. #pragma clang diagnostic pop
  94. #endif]])
  95. end
  96. _push_functions['Component'] = "ToluaPushObject"
  97. _push_functions['Resource'] = "ToluaPushObject"
  98. _push_functions['UIElement'] = "ToluaPushObject"
  99. -- Is Urho3D Vector type.
  100. function urho3d_is_vector(t)
  101. return t:find("Vector<") ~= nil
  102. end
  103. -- Is Urho3D PODVector type.
  104. function urho3d_is_podvector(t)
  105. return t:find("PODVector<") ~= nil
  106. end
  107. local old_get_push_function = get_push_function
  108. local old_get_to_function = get_to_function
  109. local old_get_is_function = get_is_function
  110. function get_push_function(t)
  111. if not urho3d_is_vector(t) then
  112. return old_get_push_function(t)
  113. end
  114. if not urho3d_is_podvector(t) then
  115. return "ToluaPushVector" .. t:match("<.*>")
  116. else
  117. return "ToluaPushPODVector" .. t:match("<.*>")
  118. end
  119. end
  120. function get_to_function(t)
  121. if not urho3d_is_vector(t) then
  122. return old_get_to_function(t)
  123. end
  124. if not urho3d_is_podvector(t) then
  125. return "ToluaToVector" .. t:match("<.*>")
  126. else
  127. return "ToluaToPODVector" .. t:match("<.*>")
  128. end
  129. end
  130. function get_is_function(t)
  131. if not urho3d_is_vector(t) then
  132. return old_get_is_function(t)
  133. end
  134. if not urho3d_is_podvector(t) then
  135. return "ToluaIsVector" .. t:match("<.*>")
  136. else
  137. return "ToluaIsPODVector" .. t:match("<.*>")
  138. end
  139. end
  140. function get_property_methods_hook(ptype, name)
  141. if ptype == "get_set" then
  142. local Name = string.upper(string.sub(name, 1, 1))..string.sub(name, 2)
  143. return "Get"..Name, "Set"..Name
  144. end
  145. if ptype == "is_set" then
  146. local Name = string.upper(string.sub(name, 1, 1))..string.sub(name, 2)
  147. return "Is"..Name, "Set"..Name
  148. end
  149. if ptype == "has_set" then
  150. local Name = string.upper(string.sub(name, 1, 1))..string.sub(name, 2)
  151. return "Has"..Name, "Set"..Name
  152. end
  153. if ptype == "no_prefix" then
  154. local Name = string.upper(string.sub(name, 1, 1))..string.sub(name, 2)
  155. return Name, "Set"..Name
  156. end
  157. end