ParseEvents.pl 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # These contains the pre-mapped events
  2. $events{"ReloadStarted"} = "Urho.Resources";
  3. $events{"ReloadFinished"} = "Urho.Resources";
  4. $events{"ReloadFailed"} = "Urho.Resources";
  5. $events{"FileChanged"} = "Urho.Resources";
  6. $events{"LoadFailed"} = "Urho.Resources";
  7. $events{"ResourceNotFound"} = "Urho.Resources";
  8. $events{"UnknownResourceType"} = "Urho.Resources";
  9. $events{"ResourceBackgroundLoaded"} = "Urho.Resources";
  10. $events{"ChangeLanguage"} = "Urho.Resources";
  11. $events{"LogMessage"} = "Urho.IO";
  12. $events{"AsyncExecFinished"} = "Urho.IO";
  13. $events{"UIMouseClick"} = "Urho.Gui";
  14. $events{"UIMouseClickEnd"} = "Urho.Gui";
  15. $events{"UIMouseDoubleClick"} = "Urho.Gui";
  16. $events{"DragDropTest"} = "Urho.Gui";
  17. $events{"DragDropFinish"} = "Urho.Gui";
  18. $events{"FocusChanged"} = "Urho.Gui";
  19. $events{"NameChanged"} = "Urho.Gui";
  20. $events{"Resized"} = "Urho.Gui";
  21. $events{"Positioned"} = "Urho.Gui";
  22. $events{"VisibleChanged"} = "Urho.Gui";
  23. $events{"Focused"} = "Urho.Gui";
  24. $events{"Defocused"} = "Urho.Gui";
  25. $events{"LayoutUpdated"} = "Urho.Gui";
  26. $events{"Pressed"} = "Urho.Gui";
  27. $events{"Released"} = "Urho.Gui";
  28. $events{"Toggled"} = "Urho.Gui";
  29. $events{"SliderChanged"} = "Urho.Gui";
  30. $events{"SliderPaged"} = "Urho.Gui";
  31. $events{"ScrollBarChanged"} = "Urho.Gui";
  32. $events{"ViewChanged"} = "Urho.Gui";
  33. $events{"ModalChanged"} = "Urho.Gui";
  34. $events{"CharEntry"} = "Urho.Gui";
  35. $events{"TextChanged"} = "Urho.Gui";
  36. $events{"TextFinished"} = "Urho.Gui";
  37. $events{"MenuSelected"} = "Urho.Gui";
  38. $events{"ItemSelected"} = "Urho.Gui";
  39. $events{"ItemDeselected"} = "Urho.Gui";
  40. $events{"SelectionChanged"} = "Urho.Gui";
  41. $events{"ItemClicked"} = "Urho.Gui";
  42. $events{"ItemDoubleClicked"} = "Urho.Gui";
  43. $events{"UnhandledKey"} = "Urho.Gui";
  44. $events{"FileSelected"} = "Urho.Gui";
  45. $events{"MessageACK"} = "Urho.Gui";
  46. $events{"ElementAdded"} = "Urho.Gui";
  47. $events{"ElementRemoved"} = "Urho.Gui";
  48. $events{"HoverBegin"} = "Urho.Gui";
  49. $events{"HoverEnd"} = "Urho.Gui";
  50. $events{"DragBegin"} = "Urho.Gui";
  51. $events{"DragMove"} = "Urho.Gui";
  52. $events{"DragEnd"} = "Urho.Gui";
  53. $events{"DragCancel"} = "Urho.Gui";
  54. $events{"UIDropFile"} = "Urho.Gui";
  55. $events{"TextEntry"} = "Urho.Gui";
  56. $events{"PhysicsPreStep2D"} = "Urho.Urho2D";
  57. $events{"PhysicsPostStep2D"} = "Urho.Urho2D";
  58. $events{"PhysicsBeginContact2D"} = "Urho.Urho2D";
  59. $events{"PhysicsEndContact2D"} = "Urho.Urho2D";
  60. $events{"PhysicsPreStep"} = "Urho.Physics";
  61. $events{"PhysicsPostStep"} = "Urho.Physics";
  62. $events{"PhysicsCollisionStart"} = "Urho.Physics";
  63. $events{"PhysicsCollision"} = "Urho.Physics";
  64. $events{"PhysicsCollisionEnd"} = "Urho.Physics";
  65. $events{"NavigationMeshRebuilt"} = "Urho.Navigation";
  66. $events{"NavigationAreaRebuilt"} = "Urho.Navigation";
  67. $events{"CrowdAgentFormation"} = "Urho.Navigation";
  68. $events{"CrowdAgentReposition"} = "Urho.Navigation";
  69. $events{"CrowdAgentFailure"} = "Urho.Navigation";
  70. $events{"CrowdAgentStateChanged"} = "Urho.Navigation";
  71. $events{"NavigationObstacleAdded"} = "Urho.Navigation";
  72. $events{"NavigationObstacleRemoved"} = "Urho.Navigation";
  73. $events{"ServerConnected"} = "Urho.Network";
  74. $events{"ServerDisconnected"} = "Urho.Network";
  75. $events{"ConnectFailed"} = "Urho.Network";
  76. $events{"ClientConnected"} = "Urho.Network";
  77. $events{"ClientDisconnected"} = "Urho.Network";
  78. $events{"ClientIdentity"} = "Urho.Network";
  79. $events{"ClientSceneLoaded"} = "Urho.Network";
  80. $events{"NetworkMessage"} = "Urho.Network";
  81. $events{"NetworkUpdate"} = "Urho.Network";
  82. $events{"NetworkUpdateSent"} = "Urho.Network";
  83. $events{"NetworkSceneLoadFailed"} = "Urho.Network";
  84. $events{"RemoteEventData"} = "Urho.Network";
  85. open CS,">Portable/Generated/Object.Events.cs" || die;
  86. open CPP,">Portable/Generated/events.cpp" || die;
  87. print CS "using System;\n";
  88. print CS "using System.Runtime.InteropServices;\n";
  89. print CS "using Urho.Physics;\n";
  90. print CS "using Urho.Navigation;\n";
  91. print CS "using Urho.Network;\n";
  92. print CS "using Urho.Urho2D;\n";
  93. print CS "using Urho.Gui;\n";
  94. print CS "using Urho.Actions;\n";
  95. print CS "using Urho.Audio;\n";
  96. print CS "using Urho.Resources;\n";
  97. print CS "using Urho.IO;\n";
  98. print CS "#pragma warning disable CS0618, CS0649\n";
  99. print CS "namespace Urho {\n\n";
  100. print CS "\t[UnmanagedFunctionPointer(CallingConvention.Cdecl)]";
  101. print CS "\tpublic delegate void ObjectCallbackSignature (IntPtr data, int stringhash, IntPtr variantMap);\n";
  102. print CS "}\n\n";
  103. print CPP "#include \"../../Native/AllUrho.h\"\n";
  104. print CPP "#include \"../../Native/glue.h\"\n";
  105. print CPP "extern \"C\" {\n";
  106. print CPP "void urho_unsubscribe (NotificationProxy *proxy);\n";
  107. sub mapType {
  108. my ($pt) = @_;
  109. ($t,$st) = split (/ /, $pt);
  110. if ($st eq "pointer"){
  111. return "IntPtr" if $t eq "b2Contact";
  112. return "$t";
  113. }
  114. if ($st eq "ptr"){
  115. return "$t";
  116. }
  117. return "uint" if $t eq "unsigned";
  118. return "bool" if $t eq "Bool";
  119. return "float" if $t eq "Float";
  120. return "IntPtr" if $t eq "User-defined";
  121. return "IntPtr" if $t eq "SDL_Event";
  122. return "CollisionData []" if $pt eq "Buffer containing";
  123. return "byte []" if $t eq "Buffer";
  124. return $t;
  125. }
  126. sub stringHash {
  127. my ($str) = @_;
  128. $str = lc $str;
  129. $hash = 0;
  130. foreach $char (split //, $str) {
  131. $hash = ord($char) + ($hash << 6) + ($hash << 16) - $hash;
  132. $hash = $hash & 0xffffffff;
  133. }
  134. return $hash;
  135. }
  136. open EVENTS, "events.txt" || die "Need the file events.txt to figure out where to generate events";
  137. while (<EVENTS>){
  138. ($event, $classes) = split;
  139. @events{$event} = $classes;
  140. }
  141. $hasNamespace = 0;
  142. RESTART:
  143. while (<>){
  144. chop;
  145. next if (/#define/);
  146. if (/EVENT\(/){
  147. ($ec,$en) = $_ =~ /EVENT\((\w+), ?(\w+)/;
  148. if ($en ne "DbCursor"){
  149. $eventName = $en;
  150. $eventName = "MouseMoved" if ($en eq "MouseMove");
  151. $eventName = "FrameStarted" if ($en eq "BeginFrame");
  152. $eventName = "FrameEnded" if ($en eq "EndFrame");
  153. if (exists $events{$en}){
  154. print CS "namespace ${events{$en}} {\n"
  155. } else {
  156. print CS "namespace Urho {\n";
  157. }
  158. print CS " public partial struct ${eventName}EventArgs {\n";
  159. print CS " public EventDataContainer EventData;\n";
  160. while (<>){
  161. chop;
  162. $cast = "";
  163. if (/PARAM/){
  164. ($pc,$pn,$pt) = $_ =~ /PARAM\((\w+), ?(\w+).*\/\/\W*(\w+(\W+\w+)?)/;
  165. $cspt = $plain = &mapType ($pt);
  166. if (/P_KEY.*Key/){
  167. $cspt = "Key";
  168. $cast = "(Key)";
  169. }
  170. $plain =~ s/ .*//;
  171. if ($plain eq "byte"){
  172. $plain = "Buffer";
  173. }
  174. $hashgetters{$pc} = $en;
  175. $pccode = &stringHash($pn);
  176. print CS " public $cspt $pn =>$cast EventData.get_$plain (unchecked((int)$pccode) /* $pn ($pc) */);\n";
  177. }
  178. if (/}/){
  179. print CS " } /* struct ${eventName}EventArgs */\n\n";
  180. for $type (split /,/,$events{$ec}){
  181. $eccode = &stringHash($en);
  182. print CS " public partial class $type {\n";
  183. print CS " [Obsolete(\"SubscribeTo API may lead to unxpected behaviour and will be removed in a future version. Use C# event '.${eventName} += ...' instead.\")]\n";
  184. print CS " @{[$en eq 'Update' ? 'internal' : 'public']} Subscription SubscribeTo${eventName} (Action<${eventName}EventArgs> handler)\n";
  185. print CS " {\n";
  186. print CS " Action<IntPtr> proxy = (x)=> { var d = new ${eventName}EventArgs () { EventData = new EventDataContainer(x) }; handler (d); };\n";
  187. print CS " var s = new Subscription (proxy);\n";
  188. print CS " s.UnmanagedProxy = UrhoObject.urho_subscribe_event (handle, UrhoObject.ObjectCallbackInstance, GCHandle.ToIntPtr (s.gch), unchecked((int)$eccode) /* $en ($ec) */);\n";
  189. print CS " return s;\n";
  190. print CS " }\n\n";
  191. print CS " static UrhoEventAdapter<${eventName}EventArgs> eventAdapterFor${eventName};\n";
  192. print CS " public event Action<${eventName}EventArgs> ${eventName}\n";
  193. print CS " {\n";
  194. print CS " add\n";
  195. print CS " {\n";
  196. print CS " if (eventAdapterFor${eventName} == null)\n";
  197. print CS " eventAdapterFor${eventName} = new UrhoEventAdapter<${eventName}EventArgs>(typeof($type));\n";
  198. print CS " eventAdapterFor${eventName}.AddManagedSubscriber(handle, value, SubscribeTo${eventName});\n";
  199. print CS " }\n";
  200. print CS " remove { eventAdapterFor${eventName}.RemoveManagedSubscriber(handle, value); }\n";
  201. print CS " }\n";
  202. print CS " } /* class $type */ \n\n";
  203. }
  204. }
  205. if (/}/){
  206. print CS "} /* namespace */\n\n";
  207. }
  208. next RESTART if (/}/);
  209. }
  210. }
  211. }
  212. }
  213. print CPP "// TODO: remove this file.\n";
  214. print CPP "}\n";
  215. print CS "#pragma warning restore CS0618, CS0649";
  216. close CS;
  217. close CPP;