2020-08-08 06:43:41 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
|
|
|
* Copyright (C) 2002-2020 PCSX2 Dev Team
|
|
|
|
*
|
|
|
|
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU Lesser General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with PCSX2.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "PrecompiledHeader.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <thread>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#if _WIN32
|
|
|
|
#define read_portable(a, b, c) (recv(a, b, c, 0))
|
|
|
|
#define write_portable(a, b, c) (send(a, b, c, 0))
|
2020-08-21 20:13:03 +00:00
|
|
|
#define close_portable(a) (closesocket(a))
|
2020-08-08 06:43:41 +00:00
|
|
|
#define bzero(b, len) (memset((b), '\0', (len)), (void)0)
|
2020-10-03 19:03:04 +00:00
|
|
|
#include <WinSock2.h>
|
2020-08-08 06:43:41 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#define read_portable(a, b, c) (read(a, b, c))
|
|
|
|
#define write_portable(a, b, c) (write(a, b, c))
|
2020-08-21 20:13:03 +00:00
|
|
|
#define close_portable(a) (close(a))
|
2020-08-08 06:43:41 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Common.h"
|
|
|
|
#include "Memory.h"
|
2021-03-01 10:15:59 +00:00
|
|
|
#include "gui/AppSaveStates.h"
|
2021-03-01 17:30:07 +00:00
|
|
|
#include "gui/AppCoreThread.h"
|
2020-08-08 06:43:41 +00:00
|
|
|
#include "System/SysThreads.h"
|
2020-09-19 23:46:19 +00:00
|
|
|
#include "svnrev.h"
|
2021-05-29 16:01:27 +00:00
|
|
|
#include "PINE.h"
|
2020-08-08 06:43:41 +00:00
|
|
|
|
2021-05-29 16:01:27 +00:00
|
|
|
PINEServer::PINEServer(SysCoreThread* vm, unsigned int slot)
|
|
|
|
: pxThread("PINE_Server")
|
2020-08-08 06:43:41 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSADATA wsa;
|
2020-09-25 23:09:59 +00:00
|
|
|
struct sockaddr_in server;
|
2020-08-08 06:43:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
|
|
|
|
{
|
2021-05-29 16:01:27 +00:00
|
|
|
Console.WriteLn(Color_Red, "PINE: Cannot initialize winsock! Shutting down...");
|
2020-08-08 06:43:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-03 23:11:11 +00:00
|
|
|
m_sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if ((m_sock == INVALID_SOCKET) || slot > 65536)
|
2020-08-08 06:43:41 +00:00
|
|
|
{
|
2021-05-29 16:01:27 +00:00
|
|
|
Console.WriteLn(Color_Red, "PINE: Cannot open socket! Shutting down...");
|
2020-08-08 06:43:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// yes very good windows s/sun/sin/g sure is fine
|
|
|
|
server.sin_family = AF_INET;
|
|
|
|
// localhost only
|
|
|
|
server.sin_addr.s_addr = inet_addr("127.0.0.1");
|
2021-03-03 20:59:32 +00:00
|
|
|
server.sin_port = htons(slot);
|
2020-08-08 06:43:41 +00:00
|
|
|
|
|
|
|
if (bind(m_sock, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
|
|
|
|
{
|
2021-05-29 16:01:27 +00:00
|
|
|
Console.WriteLn(Color_Red, "PINE: Error while binding to socket! Shutting down...");
|
2020-08-08 06:43:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2021-03-29 12:58:45 +00:00
|
|
|
char* runtime_dir = nullptr;
|
2020-08-30 21:19:30 +00:00
|
|
|
#ifdef __APPLE__
|
2021-03-29 12:58:45 +00:00
|
|
|
runtime_dir = std::getenv("TMPDIR");
|
2020-08-30 21:19:30 +00:00
|
|
|
#else
|
2021-03-29 12:58:45 +00:00
|
|
|
runtime_dir = std::getenv("XDG_RUNTIME_DIR");
|
2020-08-30 21:19:30 +00:00
|
|
|
#endif
|
2020-08-30 12:41:10 +00:00
|
|
|
// fallback in case macOS or other OSes don't implement the XDG base
|
|
|
|
// spec
|
2021-03-29 12:58:45 +00:00
|
|
|
if (runtime_dir == nullptr)
|
2021-05-29 16:01:27 +00:00
|
|
|
m_socket_name = "/tmp/" PINE_EMULATOR_NAME ".sock";
|
2020-08-30 12:41:10 +00:00
|
|
|
else
|
2021-03-29 12:58:45 +00:00
|
|
|
{
|
2021-03-29 17:58:09 +00:00
|
|
|
m_socket_name = runtime_dir;
|
2021-05-29 16:01:27 +00:00
|
|
|
m_socket_name += "/" PINE_EMULATOR_NAME ".sock";
|
2021-03-29 12:58:45 +00:00
|
|
|
}
|
2021-03-03 20:59:32 +00:00
|
|
|
|
2021-05-29 16:01:27 +00:00
|
|
|
if (slot != PINE_DEFAULT_SLOT)
|
2021-03-29 18:15:11 +00:00
|
|
|
m_socket_name += "." + std::to_string(slot);
|
2020-08-30 12:41:10 +00:00
|
|
|
|
2020-08-08 06:43:41 +00:00
|
|
|
struct sockaddr_un server;
|
|
|
|
|
|
|
|
m_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (m_sock < 0)
|
|
|
|
{
|
2021-05-29 16:01:27 +00:00
|
|
|
Console.WriteLn(Color_Red, "PINE: Cannot open socket! Shutting down...");
|
2020-08-08 06:43:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
server.sun_family = AF_UNIX;
|
2021-03-29 17:58:09 +00:00
|
|
|
strcpy(server.sun_path, m_socket_name.c_str());
|
2020-08-08 06:43:41 +00:00
|
|
|
|
|
|
|
// we unlink the socket so that when releasing this thread the socket gets
|
|
|
|
// freed even if we didn't close correctly the loop
|
2021-03-29 17:58:09 +00:00
|
|
|
unlink(m_socket_name.c_str());
|
2020-08-08 06:43:41 +00:00
|
|
|
if (bind(m_sock, (struct sockaddr*)&server, sizeof(struct sockaddr_un)))
|
|
|
|
{
|
2021-05-29 16:01:27 +00:00
|
|
|
Console.WriteLn(Color_Red, "PINE: Error while binding to socket! Shutting down...");
|
2020-08-08 06:43:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-08-21 20:13:03 +00:00
|
|
|
// maximum queue of 4096 commands before refusing, approximated to the
|
|
|
|
// nearest legal value. We do not use SOMAXCONN as windows have this idea
|
|
|
|
// that a "reasonable" value is 5, which is not.
|
|
|
|
listen(m_sock, 4096);
|
2020-08-08 06:43:41 +00:00
|
|
|
|
|
|
|
// we save a handle of the main vm object
|
|
|
|
m_vm = vm;
|
|
|
|
|
|
|
|
// we start the thread
|
|
|
|
Start();
|
|
|
|
}
|
|
|
|
|
2021-05-29 16:01:27 +00:00
|
|
|
char* PINEServer::MakeOkIPC(char* ret_buffer, uint32_t size = 5)
|
2020-08-31 03:53:40 +00:00
|
|
|
{
|
|
|
|
ToArray<uint32_t>(ret_buffer, size, 0);
|
|
|
|
ret_buffer[4] = IPC_OK;
|
|
|
|
return ret_buffer;
|
|
|
|
}
|
|
|
|
|
2021-05-29 16:01:27 +00:00
|
|
|
char* PINEServer::MakeFailIPC(char* ret_buffer, uint32_t size = 5)
|
2020-08-31 03:53:40 +00:00
|
|
|
{
|
|
|
|
ToArray<uint32_t>(ret_buffer, size, 0);
|
|
|
|
ret_buffer[4] = IPC_FAIL;
|
|
|
|
return ret_buffer;
|
|
|
|
}
|
|
|
|
|
2021-05-29 16:01:27 +00:00
|
|
|
int PINEServer::StartSocket()
|
2021-02-28 21:32:29 +00:00
|
|
|
{
|
|
|
|
m_msgsock = accept(m_sock, 0, 0);
|
|
|
|
|
|
|
|
if (m_msgsock == -1)
|
|
|
|
{
|
|
|
|
// everything else is non recoverable in our scope
|
|
|
|
// we also mark as recoverable socket errors where it would block a
|
|
|
|
// non blocking socket, even though our socket is blocking, in case
|
|
|
|
// we ever have to implement a non blocking socket.
|
|
|
|
#ifdef _WIN32
|
|
|
|
int errno_w = WSAGetLastError();
|
|
|
|
if (!(errno_w == WSAECONNRESET || errno_w == WSAEINTR || errno_w == WSAEINPROGRESS || errno_w == WSAEMFILE || errno_w == WSAEWOULDBLOCK))
|
|
|
|
{
|
|
|
|
#else
|
|
|
|
if (!(errno == ECONNABORTED || errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
|
|
|
|
{
|
|
|
|
#endif
|
2021-05-29 16:01:27 +00:00
|
|
|
fprintf(stderr, "PINE: An unrecoverable error happened! Shutting down...\n");
|
2021-02-28 21:32:29 +00:00
|
|
|
m_end = true;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-29 16:01:27 +00:00
|
|
|
void PINEServer::ExecuteTaskInThread()
|
2020-08-08 06:43:41 +00:00
|
|
|
{
|
2020-08-21 20:13:03 +00:00
|
|
|
m_end = false;
|
2020-08-17 14:29:16 +00:00
|
|
|
|
|
|
|
// we allocate once buffers to not have to do mallocs for each IPC
|
|
|
|
// request, as malloc is expansive when we optimize for µs.
|
|
|
|
m_ret_buffer = new char[MAX_IPC_RETURN_SIZE];
|
|
|
|
m_ipc_buffer = new char[MAX_IPC_SIZE];
|
2021-02-28 21:32:29 +00:00
|
|
|
|
|
|
|
if (StartSocket() < 0)
|
|
|
|
return;
|
|
|
|
|
2020-08-08 06:43:41 +00:00
|
|
|
while (true)
|
|
|
|
{
|
2021-02-28 21:32:29 +00:00
|
|
|
// either int or ssize_t depending on the platform, so we have to
|
|
|
|
// use a bunch of auto
|
|
|
|
auto receive_length = 0;
|
|
|
|
auto end_length = 4;
|
|
|
|
|
|
|
|
// while we haven't received the entire packet, maybe due to
|
|
|
|
// socket datagram splittage, we continue to read
|
|
|
|
while (receive_length < end_length)
|
2020-08-08 06:43:41 +00:00
|
|
|
{
|
2021-02-28 21:32:29 +00:00
|
|
|
auto tmp_length = read_portable(m_msgsock, &m_ipc_buffer[receive_length], MAX_IPC_SIZE - receive_length);
|
|
|
|
|
|
|
|
// we recreate the socket if an error happens
|
|
|
|
if (tmp_length <= 0)
|
2020-08-21 20:13:03 +00:00
|
|
|
{
|
2021-02-28 21:32:29 +00:00
|
|
|
receive_length = 0;
|
|
|
|
if (StartSocket() < 0)
|
|
|
|
return;
|
2020-08-21 20:13:03 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-08-30 08:37:11 +00:00
|
|
|
|
2021-02-28 21:32:29 +00:00
|
|
|
receive_length += tmp_length;
|
2020-08-31 03:53:40 +00:00
|
|
|
|
2021-02-28 21:32:29 +00:00
|
|
|
// if we got at least the final size then update
|
|
|
|
if (end_length == 4 && receive_length >= 4)
|
2020-08-08 06:43:41 +00:00
|
|
|
{
|
2021-02-28 21:32:29 +00:00
|
|
|
end_length = FromArray<u32>(m_ipc_buffer, 0);
|
|
|
|
// we'd like to avoid a client trying to do OOB
|
|
|
|
if (end_length > MAX_IPC_SIZE || end_length < 4)
|
2020-08-29 23:57:08 +00:00
|
|
|
{
|
2020-08-31 03:53:40 +00:00
|
|
|
receive_length = 0;
|
|
|
|
break;
|
2020-08-29 23:57:08 +00:00
|
|
|
}
|
2021-02-28 21:32:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-29 16:01:27 +00:00
|
|
|
PINEServer::IPCBuffer res;
|
2020-08-29 23:57:08 +00:00
|
|
|
|
2021-02-28 21:32:29 +00:00
|
|
|
// we remove 4 bytes to get the message size out of the IPC command
|
2021-03-01 21:41:43 +00:00
|
|
|
// size in ParseCommand.
|
|
|
|
// also, if we got a failed command, let's reset the state so we don't
|
|
|
|
// end up deadlocking by getting out of sync, eg when a client
|
|
|
|
// disconnects
|
|
|
|
if (receive_length != 0)
|
|
|
|
{
|
2021-02-28 21:32:29 +00:00
|
|
|
res = ParseCommand(&m_ipc_buffer[4], m_ret_buffer, (u32)end_length - 4);
|
2020-08-29 23:57:08 +00:00
|
|
|
|
2021-03-01 21:41:43 +00:00
|
|
|
// if we cannot send back our answer restart the socket
|
|
|
|
if (write_portable(m_msgsock, res.buffer, res.size) < 0)
|
|
|
|
{
|
|
|
|
if (StartSocket() < 0)
|
|
|
|
return;
|
|
|
|
}
|
2020-08-08 06:43:41 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-21 20:13:03 +00:00
|
|
|
return;
|
2020-08-08 06:43:41 +00:00
|
|
|
}
|
|
|
|
|
2021-05-29 16:01:27 +00:00
|
|
|
PINEServer::~PINEServer()
|
2020-08-08 06:43:41 +00:00
|
|
|
{
|
2020-08-21 20:13:03 +00:00
|
|
|
m_end = true;
|
2020-08-08 06:43:41 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
WSACleanup();
|
|
|
|
#else
|
2021-03-29 17:58:09 +00:00
|
|
|
unlink(m_socket_name.c_str());
|
2020-08-08 06:43:41 +00:00
|
|
|
#endif
|
2020-08-21 20:13:03 +00:00
|
|
|
close_portable(m_sock);
|
|
|
|
close_portable(m_msgsock);
|
2020-08-17 14:29:16 +00:00
|
|
|
delete[] m_ret_buffer;
|
|
|
|
delete[] m_ipc_buffer;
|
2020-08-08 06:43:41 +00:00
|
|
|
// destroy the thread
|
|
|
|
try
|
|
|
|
{
|
|
|
|
pxThread::Cancel();
|
|
|
|
}
|
|
|
|
DESTRUCTOR_CATCHALL
|
|
|
|
}
|
|
|
|
|
2021-05-29 16:01:27 +00:00
|
|
|
PINEServer::IPCBuffer PINEServer::ParseCommand(char* buf, char* ret_buffer, u32 buf_size)
|
2020-08-08 06:43:41 +00:00
|
|
|
{
|
2020-08-30 09:46:29 +00:00
|
|
|
u32 ret_cnt = 5;
|
2020-08-20 05:33:27 +00:00
|
|
|
u32 buf_cnt = 0;
|
2020-08-08 06:43:41 +00:00
|
|
|
|
2020-08-29 23:57:08 +00:00
|
|
|
while (buf_cnt < buf_size)
|
2020-08-08 06:43:41 +00:00
|
|
|
{
|
2020-09-19 23:46:19 +00:00
|
|
|
if (!SafetyChecks(buf_cnt, 1, ret_cnt, 0, buf_size))
|
2020-08-30 09:46:29 +00:00
|
|
|
return IPCBuffer{5, MakeFailIPC(ret_buffer)};
|
2020-09-20 12:37:12 +00:00
|
|
|
buf_cnt++;
|
|
|
|
// example IPC messages: MsgRead/Write
|
|
|
|
// refer to the client doc for more info on the format
|
2020-08-17 14:29:16 +00:00
|
|
|
// IPC Message event (1 byte)
|
|
|
|
// | Memory address (4 byte)
|
|
|
|
// | | argument (VLE)
|
|
|
|
// | | |
|
|
|
|
// format: XX YY YY YY YY ZZ ZZ ZZ ZZ
|
|
|
|
// reply code: 00 = OK, FF = NOT OK
|
|
|
|
// | return value (VLE)
|
|
|
|
// | |
|
|
|
|
// reply: XX ZZ ZZ ZZ ZZ
|
2020-09-20 12:37:12 +00:00
|
|
|
switch ((IPCCommand)buf[buf_cnt - 1])
|
2020-08-08 06:43:41 +00:00
|
|
|
{
|
2020-08-17 14:29:16 +00:00
|
|
|
case MsgRead8:
|
|
|
|
{
|
2020-09-20 12:37:12 +00:00
|
|
|
if (!m_vm->HasActiveMachine())
|
|
|
|
goto error;
|
|
|
|
if (!SafetyChecks(buf_cnt, 4, ret_cnt, 1, buf_size))
|
2020-08-20 05:33:27 +00:00
|
|
|
goto error;
|
2020-09-20 12:37:12 +00:00
|
|
|
const u32 a = FromArray<u32>(&buf[buf_cnt], 0);
|
2020-08-31 03:53:40 +00:00
|
|
|
const u8 res = memRead8(a);
|
2020-08-17 14:29:16 +00:00
|
|
|
ToArray(ret_buffer, res, ret_cnt);
|
|
|
|
ret_cnt += 1;
|
2020-09-20 12:37:12 +00:00
|
|
|
buf_cnt += 4;
|
2020-08-17 14:29:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MsgRead16:
|
|
|
|
{
|
2020-09-20 12:37:12 +00:00
|
|
|
if (!m_vm->HasActiveMachine())
|
|
|
|
goto error;
|
|
|
|
if (!SafetyChecks(buf_cnt, 4, ret_cnt, 2, buf_size))
|
2020-08-20 05:33:27 +00:00
|
|
|
goto error;
|
2020-09-20 12:37:12 +00:00
|
|
|
const u32 a = FromArray<u32>(&buf[buf_cnt], 0);
|
2020-08-31 03:53:40 +00:00
|
|
|
const u16 res = memRead16(a);
|
2020-08-17 14:29:16 +00:00
|
|
|
ToArray(ret_buffer, res, ret_cnt);
|
|
|
|
ret_cnt += 2;
|
2020-09-20 12:37:12 +00:00
|
|
|
buf_cnt += 4;
|
2020-08-17 14:29:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MsgRead32:
|
|
|
|
{
|
2020-09-20 12:37:12 +00:00
|
|
|
if (!m_vm->HasActiveMachine())
|
2020-08-20 05:33:27 +00:00
|
|
|
goto error;
|
2020-09-20 12:37:12 +00:00
|
|
|
if (!SafetyChecks(buf_cnt, 4, ret_cnt, 4, buf_size))
|
|
|
|
goto error;
|
|
|
|
const u32 a = FromArray<u32>(&buf[buf_cnt], 0);
|
2020-08-31 03:53:40 +00:00
|
|
|
const u32 res = memRead32(a);
|
2020-08-17 14:29:16 +00:00
|
|
|
ToArray(ret_buffer, res, ret_cnt);
|
|
|
|
ret_cnt += 4;
|
2020-09-20 12:37:12 +00:00
|
|
|
buf_cnt += 4;
|
2020-08-17 14:29:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MsgRead64:
|
|
|
|
{
|
2020-09-20 12:37:12 +00:00
|
|
|
if (!m_vm->HasActiveMachine())
|
|
|
|
goto error;
|
|
|
|
if (!SafetyChecks(buf_cnt, 4, ret_cnt, 8, buf_size))
|
2020-08-20 05:33:27 +00:00
|
|
|
goto error;
|
2020-09-20 12:37:12 +00:00
|
|
|
const u32 a = FromArray<u32>(&buf[buf_cnt], 0);
|
2020-08-31 03:53:40 +00:00
|
|
|
u64 res = 0;
|
2020-08-17 14:29:16 +00:00
|
|
|
memRead64(a, &res);
|
|
|
|
ToArray(ret_buffer, res, ret_cnt);
|
|
|
|
ret_cnt += 8;
|
2020-09-20 12:37:12 +00:00
|
|
|
buf_cnt += 4;
|
2020-08-17 14:29:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MsgWrite8:
|
|
|
|
{
|
2020-09-20 12:37:12 +00:00
|
|
|
if (!m_vm->HasActiveMachine())
|
2020-08-20 05:33:27 +00:00
|
|
|
goto error;
|
2020-09-20 12:37:12 +00:00
|
|
|
if (!SafetyChecks(buf_cnt, 1 + 4, ret_cnt, 0, buf_size))
|
|
|
|
goto error;
|
|
|
|
const u32 a = FromArray<u32>(&buf[buf_cnt], 0);
|
|
|
|
memWrite8(a, FromArray<u8>(&buf[buf_cnt], 4));
|
|
|
|
buf_cnt += 5;
|
2020-08-17 14:29:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MsgWrite16:
|
|
|
|
{
|
2020-09-20 12:37:12 +00:00
|
|
|
if (!m_vm->HasActiveMachine())
|
|
|
|
goto error;
|
|
|
|
if (!SafetyChecks(buf_cnt, 2 + 4, ret_cnt, 0, buf_size))
|
2020-08-20 05:33:27 +00:00
|
|
|
goto error;
|
2020-09-20 12:37:12 +00:00
|
|
|
const u32 a = FromArray<u32>(&buf[buf_cnt], 0);
|
|
|
|
memWrite16(a, FromArray<u16>(&buf[buf_cnt], 4));
|
|
|
|
buf_cnt += 6;
|
2020-08-17 14:29:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MsgWrite32:
|
|
|
|
{
|
2020-09-20 12:37:12 +00:00
|
|
|
if (!m_vm->HasActiveMachine())
|
|
|
|
goto error;
|
|
|
|
if (!SafetyChecks(buf_cnt, 4 + 4, ret_cnt, 0, buf_size))
|
2020-08-20 05:33:27 +00:00
|
|
|
goto error;
|
2020-09-20 12:37:12 +00:00
|
|
|
const u32 a = FromArray<u32>(&buf[buf_cnt], 0);
|
|
|
|
memWrite32(a, FromArray<u32>(&buf[buf_cnt], 4));
|
|
|
|
buf_cnt += 8;
|
2020-08-17 14:29:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MsgWrite64:
|
|
|
|
{
|
2020-09-20 12:37:12 +00:00
|
|
|
if (!m_vm->HasActiveMachine())
|
2020-08-20 05:33:27 +00:00
|
|
|
goto error;
|
2020-09-20 12:37:12 +00:00
|
|
|
if (!SafetyChecks(buf_cnt, 8 + 4, ret_cnt, 0, buf_size))
|
|
|
|
goto error;
|
|
|
|
const u32 a = FromArray<u32>(&buf[buf_cnt], 0);
|
|
|
|
memWrite64(a, FromArray<u64>(&buf[buf_cnt], 4));
|
|
|
|
buf_cnt += 12;
|
2020-08-17 14:29:16 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-09-20 12:37:12 +00:00
|
|
|
case MsgVersion:
|
|
|
|
{
|
2021-05-31 13:37:32 +00:00
|
|
|
if (!m_vm->HasActiveMachine())
|
|
|
|
goto error;
|
2020-09-20 12:37:12 +00:00
|
|
|
char version[256] = {};
|
2021-10-18 04:09:42 +00:00
|
|
|
if (GIT_TAGGED_COMMIT) // Nightly builds
|
|
|
|
{
|
|
|
|
// tagged commit - more modern implementation of dev build versioning
|
|
|
|
// - there is no need to include the commit - that is associated with the tag, git is implied
|
|
|
|
sprintf(version, "PCSX2 Nightly - %s", GIT_TAG);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprintf(version, "PCSX2 %u.%u.%u-%lld", PCSX2_VersionHi, PCSX2_VersionMid, PCSX2_VersionLo, SVN_REV);
|
|
|
|
}
|
2021-05-31 13:37:32 +00:00
|
|
|
const u32 size = strlen(version) + 1;
|
|
|
|
version[size] = 0x00;
|
|
|
|
if (!SafetyChecks(buf_cnt, 0, ret_cnt, size + 4, buf_size))
|
2020-09-19 23:46:19 +00:00
|
|
|
goto error;
|
2021-05-31 13:37:32 +00:00
|
|
|
ToArray(ret_buffer, size, ret_cnt);
|
|
|
|
ret_cnt += 4;
|
|
|
|
memcpy(&ret_buffer[ret_cnt], version, size);
|
|
|
|
ret_cnt += size;
|
2020-09-20 12:37:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-03-01 10:15:59 +00:00
|
|
|
case MsgSaveState:
|
|
|
|
{
|
|
|
|
if (!m_vm->HasActiveMachine())
|
|
|
|
goto error;
|
|
|
|
if (!SafetyChecks(buf_cnt, 1, ret_cnt, 0, buf_size))
|
|
|
|
goto error;
|
|
|
|
StateCopy_SaveToSlot(FromArray<u8>(&buf[buf_cnt], 0));
|
|
|
|
buf_cnt += 1;
|
|
|
|
break;
|
|
|
|
}
|
2021-03-01 17:01:01 +00:00
|
|
|
case MsgLoadState:
|
|
|
|
{
|
|
|
|
if (!m_vm->HasActiveMachine())
|
|
|
|
goto error;
|
|
|
|
if (!SafetyChecks(buf_cnt, 1, ret_cnt, 0, buf_size))
|
|
|
|
goto error;
|
|
|
|
StateCopy_LoadFromSlot(FromArray<u8>(&buf[buf_cnt], 0), false);
|
|
|
|
buf_cnt += 1;
|
|
|
|
break;
|
|
|
|
}
|
2021-03-01 17:30:07 +00:00
|
|
|
case MsgTitle:
|
|
|
|
{
|
|
|
|
if (!m_vm->HasActiveMachine())
|
|
|
|
goto error;
|
2021-05-31 13:53:18 +00:00
|
|
|
char* title = new char[GameInfo::gameName.size() + 1];
|
2021-03-01 18:02:38 +00:00
|
|
|
sprintf(title, "%s", GameInfo::gameName.ToUTF8().data());
|
2021-05-31 13:37:32 +00:00
|
|
|
const u32 size = strlen(title) + 1;
|
|
|
|
title[size] = 0x00;
|
|
|
|
if (!SafetyChecks(buf_cnt, 0, ret_cnt, size + 4, buf_size))
|
|
|
|
goto error;
|
|
|
|
ToArray(ret_buffer, size, ret_cnt);
|
|
|
|
ret_cnt += 4;
|
|
|
|
memcpy(&ret_buffer[ret_cnt], title, size);
|
|
|
|
ret_cnt += size;
|
2021-05-31 13:53:18 +00:00
|
|
|
delete[] title;
|
2021-03-01 17:30:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-03-01 17:37:47 +00:00
|
|
|
case MsgID:
|
|
|
|
{
|
|
|
|
if (!m_vm->HasActiveMachine())
|
|
|
|
goto error;
|
2021-05-31 13:53:18 +00:00
|
|
|
char* title = new char[GameInfo::gameSerial.size() + 1];
|
2021-05-31 13:37:32 +00:00
|
|
|
sprintf(title, "%s", GameInfo::gameSerial.ToUTF8().data());
|
|
|
|
const u32 size = strlen(title) + 1;
|
|
|
|
title[size] = 0x00;
|
|
|
|
if (!SafetyChecks(buf_cnt, 0, ret_cnt, size + 4, buf_size))
|
2021-03-01 17:37:47 +00:00
|
|
|
goto error;
|
2021-05-31 13:37:32 +00:00
|
|
|
ToArray(ret_buffer, size, ret_cnt);
|
|
|
|
ret_cnt += 4;
|
|
|
|
memcpy(&ret_buffer[ret_cnt], title, size);
|
|
|
|
ret_cnt += size;
|
2021-05-31 13:53:18 +00:00
|
|
|
delete[] title;
|
2021-03-01 17:37:47 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-03-01 17:43:21 +00:00
|
|
|
case MsgUUID:
|
|
|
|
{
|
|
|
|
if (!m_vm->HasActiveMachine())
|
|
|
|
goto error;
|
2021-05-31 13:53:18 +00:00
|
|
|
char* title = new char[GameInfo::gameCRC.size() + 1];
|
2021-05-31 13:37:32 +00:00
|
|
|
sprintf(title, "%s", GameInfo::gameCRC.ToUTF8().data());
|
|
|
|
const u32 size = strlen(title) + 1;
|
|
|
|
title[size] = 0x00;
|
|
|
|
if (!SafetyChecks(buf_cnt, 0, ret_cnt, size + 4, buf_size))
|
2021-03-01 17:43:21 +00:00
|
|
|
goto error;
|
2021-05-31 13:37:32 +00:00
|
|
|
ToArray(ret_buffer, size, ret_cnt);
|
|
|
|
ret_cnt += 4;
|
|
|
|
memcpy(&ret_buffer[ret_cnt], title, size);
|
|
|
|
ret_cnt += size;
|
2021-05-31 13:53:18 +00:00
|
|
|
delete[] title;
|
2021-03-01 17:43:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-03-30 07:04:33 +00:00
|
|
|
case MsgGameVersion:
|
|
|
|
{
|
|
|
|
if (!m_vm->HasActiveMachine())
|
|
|
|
goto error;
|
2021-05-31 13:53:18 +00:00
|
|
|
char* title = new char[GameInfo::gameVersion.size() + 1];
|
2021-05-31 13:37:32 +00:00
|
|
|
sprintf(title, "%s", GameInfo::gameVersion.ToUTF8().data());
|
|
|
|
const u32 size = strlen(title) + 1;
|
|
|
|
title[size] = 0x00;
|
|
|
|
if (!SafetyChecks(buf_cnt, 0, ret_cnt, size + 4, buf_size))
|
2021-03-30 07:04:33 +00:00
|
|
|
goto error;
|
2021-05-31 13:37:32 +00:00
|
|
|
ToArray(ret_buffer, size, ret_cnt);
|
|
|
|
ret_cnt += 4;
|
|
|
|
memcpy(&ret_buffer[ret_cnt], title, size);
|
|
|
|
ret_cnt += size;
|
2021-05-31 13:53:18 +00:00
|
|
|
delete[] title;
|
2021-03-30 07:04:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-03-30 07:37:29 +00:00
|
|
|
case MsgStatus:
|
|
|
|
{
|
|
|
|
if (!SafetyChecks(buf_cnt, 0, ret_cnt, 4, buf_size))
|
|
|
|
goto error;
|
|
|
|
EmuStatus status;
|
|
|
|
switch (m_vm->HasActiveMachine())
|
|
|
|
{
|
|
|
|
case true:
|
2021-08-19 07:59:02 +00:00
|
|
|
if (GetCoreThread().IsClosing())
|
2021-03-30 07:37:29 +00:00
|
|
|
status = Paused;
|
|
|
|
else
|
|
|
|
status = Running;
|
|
|
|
break;
|
|
|
|
case false:
|
|
|
|
status = Shutdown;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ToArray(ret_buffer, status, ret_cnt);
|
|
|
|
ret_cnt += 4;
|
|
|
|
break;
|
|
|
|
}
|
2020-08-17 14:29:16 +00:00
|
|
|
default:
|
|
|
|
{
|
2020-08-20 05:33:27 +00:00
|
|
|
error:
|
2020-08-30 09:46:29 +00:00
|
|
|
return IPCBuffer{5, MakeFailIPC(ret_buffer)};
|
2020-08-17 14:29:16 +00:00
|
|
|
}
|
2020-08-08 06:43:41 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-30 09:46:29 +00:00
|
|
|
return IPCBuffer{(int)ret_cnt, MakeOkIPC(ret_buffer, ret_cnt)};
|
2020-08-08 06:43:41 +00:00
|
|
|
}
|