Advertisement
WeltEnSTurm

Dota 2 Camera

Nov 21st, 2013
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 3.59 KB | None | 0 0
  1.  
  2. import
  3.     std.utf,
  4.     std.conv,
  5.     ws.io,
  6.     ws.decode,
  7.     ws.time,
  8.     ws.exception,
  9.     ws.gui.input,
  10.     ws.gui.osWindow;
  11.  
  12.  
  13. version(Windows)
  14.     import core.sys.windows.windows;
  15.  
  16. // settings
  17. long mouseBox = 50;
  18. double tick = 0.01;
  19. double inputDelay = 0.009;
  20. bool borderReset = true;
  21. ushort button = Keyboard.space;
  22. ushort buttonEmulate = Mouse.buttonMiddle;
  23.  
  24. // internal
  25. osWindow window;
  26. long[2] screenSize;
  27. long[2] lastMouse;
  28. long[2] internalCursor;
  29. bool dragging;
  30.  
  31.  
  32. void main(){
  33.     Decode.file("settings.cfg", (name, value, b){
  34.         switch(name){
  35.             case "button":
  36.             case "emulate":
  37.                 if(value !in Keys)
  38.                     exception("\"%\" is not a valid button", value);
  39.                 if(name == "button")
  40.                     button = cast(ushort)Keys[value];
  41.                 else
  42.                     buttonEmulate = cast(ushort)Keys[value];
  43.                 break;
  44.             case "border":
  45.                 mouseBox = to!long(value);
  46.                 break;
  47.             case "borderReset":
  48.                 borderReset = (value == "false" ? false : true);
  49.                 break;
  50.             case "tick":
  51.                 tick = to!double(value);
  52.                 break;
  53.             case "inputDelay":
  54.                 inputDelay = to!double(value);
  55.                 break;
  56.             default:
  57.                 exception("\"%\" is not a setting", name);
  58.         }
  59.     });
  60.     while(true){
  61.         try {
  62.             if(window && wm.findWindow("Dota 2")){
  63.                 if(wm.isKeyDown(button) != dragging){
  64.                     dragging = !dragging;
  65.                     sendButton(buttonEmulate, dragging);
  66.                     internalCursor = wm.getCursorPos();
  67.                     lastMouse = internalCursor;
  68.                     window.setCursor(dragging ? Mouse.cursor.none : Mouse.cursor.inherit);
  69.                 }
  70.                 time.sleep(tick);
  71.                 if(dragging){
  72.                     auto pos = wm.getCursorPos();
  73.                     onMouseMove(pos.x, pos.y);
  74.                 }
  75.             }else{
  76.                 window = wm.findWindow("Dota 2");
  77.                 screenSize = window.getScreenSize();
  78.             }
  79.         }
  80.         catch(WindowNotFound e){
  81.             writeln("Could not find Dota 2 window");
  82.             time.sleep(1);
  83.         }
  84.         catch(Exception e)
  85.             writeln(e);
  86.     }
  87. }
  88.  
  89.  
  90. void resetCursor(){
  91.     sendButton(buttonEmulate, false);
  92.     time.sleep(inputDelay);
  93.     window.setCursorPos(cast(int)internalCursor.x, cast(int)internalCursor.y);
  94.     time.sleep(inputDelay);
  95.     sendButton(buttonEmulate, true);
  96.     lastMouse = internalCursor;
  97. }
  98.  
  99.  
  100. void onMouseMove(long x, long y){
  101.     long[2] diff = [x - lastMouse.x, y - lastMouse.y];
  102.     lastMouse = [x, y];
  103.     internalCursor = [
  104.         clamp(internalCursor.x-diff.x, screenSize.x/2-mouseBox, screenSize.x/2+mouseBox),
  105.         clamp(internalCursor.y-diff.y, screenSize.y/2-mouseBox+50, screenSize.y/2+mouseBox)
  106.     ];
  107.     resetCursor();
  108. }
  109.  
  110.  
  111. void sendButton(ushort b, bool pressed){
  112.     window.sendMessage(pressed ? WM_KEYDOWN : WM_KEYUP, cast(WPARAM)b, 0);
  113.    
  114.     INPUT ip;
  115.     ip.type = INPUT_KEYBOARD;
  116.     ip.ki.wScan = 0;
  117.     ip.ki.time = 0;
  118.     ip.ki.dwExtraInfo = 0;
  119.     ip.ki.wVk = b;
  120.     ip.ki.dwFlags = (pressed ? 0 : KEYEVENTF_KEYUP);
  121.     SendInput(1, &ip, INPUT.sizeof);
  122. }
  123.  
  124.  
  125. T clamp(T, C: T)(T a, C min, C max){
  126.     return (a < min ? min : (a > max ? max : a));
  127. }
  128.  
  129. ref T x(T)(ref T[2] a){
  130.     return a[0];
  131. }
  132.  
  133. ref T y(T)(ref T[2] a){
  134.     return a[1];
  135. }
  136.  
  137. extern(Windows):
  138.  
  139.     LPARAM MAKELPARAM(WPARAM a, WPARAM b){
  140.         return (a & 0xffff) | ((b & 0xffff) << 16);
  141.     }
  142.  
  143.     const int INPUT_KEYBOARD = 1;
  144.     const int KEYEVENTF_KEYUP = 2;
  145.  
  146.     uint SendInput(uint cInputs, INPUT* pInputs, int cbSize);
  147.  
  148.     struct INPUT {
  149.         DWORD type;
  150.         union {
  151.             MOUSEINPUT    mi;
  152.             KEYBDINPUT    ki;
  153.             HARDWAREINPUT   hi;
  154.         };
  155.     }
  156.  
  157.     struct MOUSEINPUT {
  158.         LONG    dx;
  159.         LONG    dy;
  160.         DWORD   mouseData;
  161.         DWORD   dwFlags;
  162.         DWORD   time;
  163.         ULONG_PTR dwExtraInfo;
  164.     }
  165.  
  166.     struct KEYBDINPUT {
  167.         WORD    wVk;
  168.         WORD    wScan;
  169.         DWORD   dwFlags;
  170.         DWORD   time;
  171.         ULONG_PTR dwExtraInfo;
  172.     }
  173.  
  174.     struct HARDWAREINPUT {
  175.         DWORD   uMsg;
  176.         WORD    wParamL;
  177.         WORD    wParamH;
  178.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement