ToCppHook.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. )
  88. WRITE(result)
  89. end
  90. -- Is Urho3D Vector type.
  91. function urho3d_is_vector(t)
  92. return t:find("Vector<") ~= nil
  93. end
  94. -- Is Urho3D PODVector type.
  95. function urho3d_is_podvector(t)
  96. return t:find("PODVector<") ~= nil
  97. end
  98. local old_get_push_function = get_push_function
  99. local old_get_to_function = get_to_function
  100. local old_get_is_function = get_is_function
  101. function get_push_function(t)
  102. if not urho3d_is_vector(t) then
  103. return old_get_push_function(t)
  104. end
  105. if not urho3d_is_podvector(t) then
  106. return "ToluaPushVector" .. t:match("<.*>")
  107. else
  108. return "ToluaPushPODVector" .. t:match("<.*>")
  109. end
  110. end
  111. function get_to_function(t)
  112. if not urho3d_is_vector(t) then
  113. return old_get_to_function(t)
  114. end
  115. if not urho3d_is_podvector(t) then
  116. return "ToluaToVector" .. t:match("<.*>")
  117. else
  118. return "ToluaToPODVector" .. t:match("<.*>")
  119. end
  120. end
  121. function get_is_function(t)
  122. if not urho3d_is_vector(t) then
  123. return old_get_is_function(t)
  124. end
  125. if not urho3d_is_podvector(t) then
  126. return "ToluaIsVector" .. t:match("<.*>")
  127. else
  128. return "ToluaIsPODVector" .. t:match("<.*>")
  129. end
  130. end
  131. function get_property_methods_hook(ptype, name)
  132. if ptype == "get_set" then
  133. local Name = string.upper(string.sub(name, 1, 1))..string.sub(name, 2)
  134. return "Get"..Name, "Set"..Name
  135. end
  136. if ptype == "is_set" then
  137. local Name = string.upper(string.sub(name, 1, 1))..string.sub(name, 2)
  138. return "Is"..Name, "Set"..Name
  139. end
  140. if ptype == "has_set" then
  141. local Name = string.upper(string.sub(name, 1, 1))..string.sub(name, 2)
  142. return "Has"..Name, "Set"..Name
  143. end
  144. if ptype == "no_prefix" then
  145. local Name = string.upper(string.sub(name, 1, 1))..string.sub(name, 2)
  146. return Name, "Set"..Name
  147. end
  148. end