Page 1 of 2

shaiya - Using multiple window instances, and keyboard input

Posted: Sat Jun 28, 2008 9:52 am
by zer0
@Elverion

Hello,
I've been coding a script that uses multiple instances of Shaiya windows, problem is that when I set focus to one, and press Enter, the other window also receives the message. I've noticed this occurs with shaiya even if you use any other application.
If their any way of preventing both windows from receiving the Enter keypress?

Here is a code sample to demonstrate what I mean.. You need the Shaiya bomber.exe multiclient.

Code: Select all

function get_windows(win_name)
  local l_windows = findWindowList(win_name);
  local l_new_win_name;
  for i, win in pairs(l_windows) do
    printf("Found window %x [%s]\n", win, getWindowName(win));
    l_new_win_name = win_name .. "_" .. i;
    setWindowName(win, l_new_win_name);
  end
  return l_windows;
end

function enter_msg(win, msg)
  showWindow(win, sw.shownormal);
  yrest("2000");
  attach(win)
  keyboardPress(key.VK_ENTER);
  keyboardPress(key.VK_BACK);
  keyboardPress(key.VK_BACK);
  keyboardType(msg);
  keyboardPress(key.VK_ENTER);
  detach();
end

function main()
  local wins = get_windows("Shaiya");

  while(true) do
    enter_msg(wins[1], "@ test 1");
    enter_msg(wins[2], "@ test 2");
    coroutine.yield();
  end
end

startMacro(main);

Re: shaiya - Using multiple window instances, and keyboard input

Posted: Sat Jun 28, 2008 12:43 pm
by Administrator
As far as I know, there's nothing that can be done about it. It should be considered a bug in Shaiya and fixed. Like you said, it happens even if you press enter in a whole other window. I'm guessing that this is because they used GetKeyState()/GetAsyncKeyState() where they shouldn't have.

I suppose you could hook GetKeyState()/GetAsyncKeyState() and have it return false if the correct window is not on top. If you can find a way to get the correct HWND of the main window of the current thread, then this will be pretty easy.

Re: shaiya - Using multiple window instances, and keyboard input

Posted: Sat Jun 28, 2008 7:45 pm
by zer0
@elverion
I thought so.

SonoV r next to useless when it comes to finding/fixing real bugs.

I think the only real solution would be to modify the executable ourselves.

Re: shaiya - Using multiple window instances, and keyboard input

Posted: Sat Jun 28, 2008 9:14 pm
by zer0
I'm looking at Bomber.exe, I can't get very far. I've managed to locate the set on instructions that handle in-game keypresses, that's about it.

Re: shaiya - Using multiple window instances, and keyboard input

Posted: Sun Jun 29, 2008 1:27 am
by Administrator
Rather than manipulate the binary (which can be difficult -- especially in this case), I've started writing a DLL which can be injected, and overrides GetAsyncKeyState().

It is now officially working. Download hook_GetAsyncKeyState.dll and put it in micromacro/data folder. You will then use the shaiya_launcher.lua to launch the game and inject the DLL. You will need the injector plugin to use this (just place injector.dll into the plugins folder).

Make note that the window will be locked onto after ~30 seconds of starting the game. This is to give ample time for the game windows to gain focus, and selected properly. By the time you get your first client in game, it should be ready to start the second client. You must not switch windows until after it has locked on, otherwise it may target the wrong window. Yeah, it's a hackish little trick, but it works.

Re: shaiya - Using multiple window instances, and keyboard input

Posted: Sun Jun 29, 2008 4:59 pm
by zer0
It doesn't work for me. It crashes as it's being launched. Here is the details of the error.

Using Vista 64-bit:
Image

And here is my sample lua file.

Code: Select all

-- game executable
g_game_path = "D:/Program Files (x86)/Shaiya/";
g_game_exe = "Bomber.exe";
g_game_args = "start game";

startWithDll(getPath() .. "/data/hook_GetAsyncKeyState.dll", g_game_path .. g_game_exe, g_game_exe .. " " .. g_game_args);
os.exit();

Re: shaiya - Using multiple window instances, and keyboard input

Posted: Sun Jun 29, 2008 6:59 pm
by Administrator
Ok, I think I've made the necessary fixes. It's strange that it crashed for you, but not for me under WinXP 32bit.

Re: shaiya - Using multiple window instances, and keyboard input

Posted: Sun Jun 29, 2008 9:53 pm
by zer0
Still crashing.
As their a start function I can use that won't inject any DLLs just to see if microMacro can launch programs on the Vista platform?

edit:
I tried it opening Notepad and it worked fine.
I tried Battle Realms (A directX 3D Strategy game), and it crashed during loading the menu.

Re: shaiya - Using multiple window instances, and keyboard input

Posted: Sun Jun 29, 2008 11:00 pm
by Administrator
You can use os.execute(path .. exe_name .. " game start"); to launch it without injecting.

Re: shaiya - Using multiple window instances, and keyboard input

Posted: Mon Jun 30, 2008 1:14 am
by zer0
have to modify the command so the start path was added, but otherwise os.execute() works fine.

For battle realms the intro would display but during loading it crashes.

Shaiya has a few initial calls to GetAsyncKeyState(), would that make any diff?

Here is my test code, can you modify the paths to yours and test with this script? Maybe I've left out something.

Code: Select all

-- game executable
g_game_path = "D:/Program Files (x86)/Shaiya/";
g_game_exe = "Bomber.exe";
g_game_args = "start game";

--local l_cmd = "start \"\" /D \""..g_game_path.."\" \"".. g_game_path .. g_game_exe .. "\"";
--os.execute(l_cmd);

--startWithDll(getPath() .. "/data/hook_GetAsyncKeyState.dll", g_game_path .. g_game_exe, g_game_exe .. " " .. g_game_args);

Re: shaiya - Using multiple window instances, and keyboard input

Posted: Mon Jun 30, 2008 1:16 am
by Administrator
I'm thinking the crashes have more to do with how it hooks. Try running Shaiya under Windows XP compatability mode. Let me know if that works.

Re: shaiya - Using multiple window instances, and keyboard input

Posted: Mon Jun 30, 2008 2:03 am
by zer0
I tried that didn't work, I'll try again.

I tested on a Win XP SP2 Machine just then, and your code worked fine.

So it just leaves a issue with Vista, being shit. :lol:

edit: Nope, Win XP SP2 compatibilty doesn't work either.

Can the DLL be attached after the game has been launched with os.execute()?

Re: shaiya - Using multiple window instances, and keyboard input

Posted: Mon Jun 30, 2008 7:20 am
by Administrator
No, not using os.execute(). It can be attached after the game has started by doing this:

Code: Select all

  local success = inject(findWindow("Shaiya"), getPath() .. "/data/hook_GetAsyncKeyState.dll");
  if( success == false ) then
    printf("Injecting has failed.\n");
  end

Re: shaiya - Using multiple window instances, and keyboard input

Posted: Tue Jul 01, 2008 11:18 am
by Administrator
zerosignal: Try injecting this version of the DLL, then post your hooklog.txt here. You will find this file in the Shaiya folder after injecting (or, attempting to, rather).

Re: shaiya - Using multiple window instances, and keyboard input

Posted: Wed Jul 02, 2008 2:02 am
by zer0
Hey, Shaiya is now running and it looks like from the log that it's hooking. But it's not working on Vista. I tried it both in native Vista, and win xp compatibility. :(

Code: Select all

traget_hModule: 0x758a0000
GetProcAddress(target_hModule, "GetAsyncKeyState"):1972097652
Hooking... Installed
Locking onto window: 0x10088
Don't worry about it, if it's too much hassle.

edit: in another test. the "hooklog.txt" said "Locking onto window: 0x1f0096" but when I use the findWindow("Shaiya") function it returns decimal: 2229416 or 0x2204A8. Should they be the same values?
Is it hooking into the wrong process?
I've disabled window previewing so it shouldn't be that annoying issue.

Re: shaiya - Using multiple window instances, and keyboard input

Posted: Wed Jul 02, 2008 2:59 am
by Administrator
Can you define "not working"? Do you mean it starts, but does not prevent outside input from effecting Shaiya? Or do you mean that once it locks onto the window, you cannot input any method? And yes, it should give the same HWND. I might've created a bug somehow, so it may not be on your end.

Re: shaiya - Using multiple window instances, and keyboard input

Posted: Wed Jul 02, 2008 11:29 am
by zer0
It starts, but does not prevent outside input from affecting Shaiya. And if it's locking onto the wrong window, that is why.

Re: shaiya - Using multiple window instances, and keyboard input

Posted: Wed Jul 02, 2008 6:29 pm
by Administrator
Ok, lets try this again. This time I tested to make sure it worked (at least under XP) before I uploaded it. In the last version, it was not hooking the address (hence, not effecting the game in any way).

This is what I've got logged:
traget_hModule: 0x7e410000
GetProcAddress(target_hModule, "GetAsyncKeyState"):2118247994
Hooking... Installed
Locking onto window: 0x70670
Post yours for comparison.

Re: shaiya - Using multiple window instances, and keyboard input

Posted: Mon Jul 07, 2008 9:13 am
by zer0
@elverion
Now it's crashing again. Don't worry about it any more, It's not that big of a deal I guess. ;)

Re: shaiya - Using multiple window instances, and keyboard input

Posted: Wed Jul 09, 2008 12:14 pm
by Sgraffite
I was able to successfully accomplish this using autoit script, and I've never run into any problems with inactive game windows receiving keystrokes.

I can switch between up to 3 characters on the same computer (if I run 4 clients things get unstable for some reason), and sending keystrokes to any single clients does not affect the inactive clients.

Why would I not have the same issue that you guys do if it is a game bug?