mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-02-03 20:45:31 +00:00
[Server] Pass stdin to a Lua event, fix Ctrl+C on Windows
This commit is contained in:
parent
3476bd7d04
commit
4e6bcf02d2
4 changed files with 50 additions and 7 deletions
|
@ -25,6 +25,8 @@
|
|||
#include "processors/ObjectProcessor.hpp"
|
||||
#include "processors/WorldstateProcessor.hpp"
|
||||
|
||||
#include "handleInput.hpp"
|
||||
|
||||
using namespace mwmp;
|
||||
using namespace std;
|
||||
|
||||
|
@ -496,6 +498,20 @@ void signalHandler(int signum)
|
|||
}
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
BOOL WINAPI sigIntHandler(_In_ DWORD dwCtrlType) {
|
||||
switch (dwCtrlType)
|
||||
{
|
||||
case CTRL_C_EVENT:
|
||||
signalHandler(15);
|
||||
return TRUE;
|
||||
default:
|
||||
// Pass signal on to the next handler
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
int Networking::mainLoop()
|
||||
{
|
||||
RakNet::Packet *packet;
|
||||
|
@ -506,16 +522,15 @@ int Networking::mainLoop()
|
|||
sigIntHandler.sa_handler = signalHandler;
|
||||
sigemptyset(&sigIntHandler.sa_mask);
|
||||
sigIntHandler.sa_flags = 0;
|
||||
sigaction(SIGTERM, &sigIntHandler, NULL);
|
||||
sigaction(SIGINT, &sigIntHandler, NULL);
|
||||
#else
|
||||
SetConsoleCtrlHandler(sigIntHandler, TRUE);
|
||||
#endif
|
||||
|
||||
while (running and !killLoop)
|
||||
{
|
||||
#ifndef _WIN32
|
||||
sigaction(SIGTERM, &sigIntHandler, NULL);
|
||||
sigaction(SIGINT, &sigIntHandler, NULL);
|
||||
#endif
|
||||
if (kbhit() && getch() == '\n')
|
||||
break;
|
||||
mwmp_input::handler();
|
||||
for (packet=peer->Receive(); packet; peer->DeallocatePacket(packet), packet=peer->Receive())
|
||||
{
|
||||
if (getMasterClient()->Process(packet))
|
||||
|
|
|
@ -214,7 +214,8 @@ public:
|
|||
{"OnWorldWeather", Callback<unsigned short>()},
|
||||
{"OnClientScriptGlobal", Callback<unsigned short>()},
|
||||
{"OnMpNumIncrement", Callback<int>()},
|
||||
{"OnRequestDataFileList", Callback<>()}
|
||||
{"OnRequestDataFileList", Callback<>()},
|
||||
{"OnServerWindowInput", Callback<const char *>()}
|
||||
};
|
||||
};
|
||||
|
||||
|
|
24
apps/openmw-mp/handleInput.cpp
Normal file
24
apps/openmw-mp/handleInput.cpp
Normal file
|
@ -0,0 +1,24 @@
|
|||
#include <Script/Script.hpp>
|
||||
#include <Kbhit.h>
|
||||
using namespace std;
|
||||
namespace mwmp_input {
|
||||
string windowInputBuffer;
|
||||
void handler() {
|
||||
char c;
|
||||
#ifndef WIN32 // on WIndows conio.h getch() and kbhit() are deprecated, use _getch() and _kbhit() instead
|
||||
while (kbhit()) {
|
||||
c = getch();
|
||||
#else
|
||||
while (_kbhit()) {
|
||||
c = _getch();
|
||||
#endif
|
||||
cout << c << flush;
|
||||
if (c == '\n' || c == '\r') { // handle carriage return as new line on Windows
|
||||
cout << endl;
|
||||
Script::Call<Script::CallbackIdentity("OnServerWindowInput")>(windowInputBuffer.c_str());
|
||||
windowInputBuffer.assign("");
|
||||
}
|
||||
else windowInputBuffer += c;
|
||||
}
|
||||
}
|
||||
}
|
3
apps/openmw-mp/handleInput.hpp
Normal file
3
apps/openmw-mp/handleInput.hpp
Normal file
|
@ -0,0 +1,3 @@
|
|||
namespace mwmp_input {
|
||||
void handler();
|
||||
}
|
Loading…
Reference in a new issue