ToCppHook.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. --
  2. -- Copyright (c) 2008-2014 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-2014 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 "Precompiled.h"
  85. #include "tolua++.h"
  86. #include "ToluaUtils.h"
  87. #if __clang__
  88. #pragma clang diagnostic push
  89. #pragma clang diagnostic ignored "-Wunused-function"
  90. #endif]])
  91. WRITE(result)
  92. WRITE([[
  93. #if __clang__
  94. #pragma clang diagnostic pop
  95. #endif]])
  96. end
  97. _push_functions['Component'] = "ToluaPushObject"
  98. _push_functions['Resource'] = "ToluaPushObject"
  99. _push_functions['UIElement'] = "ToluaPushObject"
  100. -- Is Urho3D Vector type.
  101. function urho3d_is_vector(t)
  102. return t:find("Vector<") ~= nil
  103. end
  104. -- Is Urho3D PODVector type.
  105. function urho3d_is_podvector(t)
  106. return t:find("PODVector<") ~= nil
  107. end
  108. local old_get_push_function = get_push_function
  109. local old_get_to_function = get_to_function
  110. local old_get_is_function = get_is_function
  111. function get_push_function(t)
  112. if not urho3d_is_vector(t) then
  113. return old_get_push_function(t)
  114. end
  115. if not urho3d_is_podvector(t) then
  116. return "ToluaPushVector" .. t:match("<.*>")
  117. else
  118. return "ToluaPushPODVector" .. t:match("<.*>")
  119. end
  120. end
  121. function get_to_function(t)
  122. if not urho3d_is_vector(t) then
  123. return old_get_to_function(t)
  124. end
  125. if not urho3d_is_podvector(t) then
  126. return "ToluaToVector" .. t:match("<.*>")
  127. else
  128. return "ToluaToPODVector" .. t:match("<.*>")
  129. end
  130. end
  131. function get_is_function(t)
  132. if not urho3d_is_vector(t) then
  133. return old_get_is_function(t)
  134. end
  135. if not urho3d_is_podvector(t) then
  136. return "ToluaIsVector" .. t:match("<.*>")
  137. else
  138. return "ToluaIsPODVector" .. t:match("<.*>")
  139. end
  140. end
  141. function get_property_methods_hook(ptype, name)
  142. if ptype == "get_set" then
  143. local Name = string.upper(string.sub(name, 1, 1))..string.sub(name, 2)
  144. return "Get"..Name, "Set"..Name
  145. end
  146. if ptype == "is_set" then
  147. local Name = string.upper(string.sub(name, 1, 1))..string.sub(name, 2)
  148. return "Is"..Name, "Set"..Name
  149. end
  150. if ptype == "has_set" then
  151. local Name = string.upper(string.sub(name, 1, 1))..string.sub(name, 2)
  152. return "Has"..Name, "Set"..Name
  153. end
  154. if ptype == "no_prefix" then
  155. local Name = string.upper(string.sub(name, 1, 1))..string.sub(name, 2)
  156. return Name, "Set"..Name
  157. end
  158. end