Compare commits

...
5 Commits
8 changed files with 106 additions and 48 deletions
+75 -41
View File
@@ -50,6 +50,13 @@ static uint8_t l_plugin[4];
static uint8_t l_buffer_target;
static uint8_t l_player_lag[4];
//UDP packets
static UDPpacket *l_request_input_packet;
static UDPpacket *l_send_input_packet;
static UDPpacket *l_process_packet;
static UDPpacket *l_check_sync_packet;
static const int32_t l_check_sync_packet_size = (CP0_REGS_COUNT * 4) + 5;
//UDP packet formats
#define UDP_SEND_KEY_INFO 0
#define UDP_RECEIVE_KEY_INFO 1
@@ -115,6 +122,31 @@ m64p_error netplay_start(const char* host, int port)
return M64ERR_SYSTEM_FAIL;
}
l_request_input_packet = SDLNet_AllocPacket(12);
l_send_input_packet = SDLNet_AllocPacket(11);
l_process_packet = SDLNet_AllocPacket(512);
l_check_sync_packet = SDLNet_AllocPacket(l_check_sync_packet_size);
if (l_request_input_packet == NULL ||
l_send_input_packet == NULL ||
l_process_packet == NULL ||
l_check_sync_packet == NULL)
{
DebugMessage(M64MSG_ERROR, "Netplay: could not allocate UDP packets");
SDLNet_UDP_Close(l_udpSocket);
l_udpSocket = NULL;
SDLNet_TCP_Close(l_tcpSocket);
l_tcpSocket = NULL;
SDLNet_FreePacket(l_request_input_packet);
l_request_input_packet = NULL;
SDLNet_FreePacket(l_send_input_packet);
l_send_input_packet = NULL;
SDLNet_FreePacket(l_process_packet);
l_process_packet = NULL;
SDLNet_FreePacket(l_check_sync_packet);
l_check_sync_packet = NULL;
return M64ERR_NO_MEMORY;
}
for (int i = 0; i < 4; ++i)
{
l_netplay_control[i] = -1;
@@ -165,6 +197,16 @@ m64p_error netplay_stop()
l_tcpSocket = NULL;
l_udpSocket = NULL;
l_udpChannel = -1;
SDLNet_FreePacket(l_request_input_packet);
SDLNet_FreePacket(l_send_input_packet);
SDLNet_FreePacket(l_process_packet);
SDLNet_FreePacket(l_check_sync_packet);
l_request_input_packet = NULL;
l_send_input_packet = NULL;
l_process_packet = NULL;
l_check_sync_packet = NULL;
l_netplay_is_init = 0;
SDLNet_Quit();
return M64ERR_SUCCESS;
@@ -191,16 +233,14 @@ static uint8_t buffer_size(uint8_t control_id)
static void netplay_request_input(uint8_t control_id)
{
UDPpacket *packet = SDLNet_AllocPacket(12);
packet->data[0] = UDP_REQUEST_KEY_INFO;
packet->data[1] = control_id; //The player we need input for
SDLNet_Write32(l_reg_id, &packet->data[2]); //our registration ID
SDLNet_Write32(l_cin_compats[control_id].netplay_count, &packet->data[6]); //the current event count
packet->data[10] = l_spectator; //whether we are a spectator
packet->data[11] = buffer_size(control_id); //our local buffer size
packet->len = 12;
SDLNet_UDP_Send(l_udpSocket, l_udpChannel, packet);
SDLNet_FreePacket(packet);
l_request_input_packet->data[0] = UDP_REQUEST_KEY_INFO;
l_request_input_packet->data[1] = control_id; //The player we need input for
SDLNet_Write32(l_reg_id, &l_request_input_packet->data[2]); //our registration ID
SDLNet_Write32(l_cin_compats[control_id].netplay_count, &l_request_input_packet->data[6]); //the current event count
l_request_input_packet->data[10] = l_spectator; //whether we are a spectator
l_request_input_packet->data[11] = buffer_size(control_id); //our local buffer size
l_request_input_packet->len = 12;
SDLNet_UDP_Send(l_udpSocket, l_udpChannel, l_request_input_packet);
}
static int check_valid(uint8_t control_id, uint32_t count)
@@ -240,21 +280,20 @@ static int netplay_require_response(void* opaque)
static void netplay_process()
{
//In this function we process data we have received from the server
UDPpacket *packet = SDLNet_AllocPacket(512);
uint32_t curr, count, keys;
uint8_t plugin, player, current_status;
while (SDLNet_UDP_Recv(l_udpSocket, packet) == 1)
while (SDLNet_UDP_Recv(l_udpSocket, l_process_packet) == 1)
{
switch (packet->data[0])
switch (l_process_packet->data[0])
{
case UDP_RECEIVE_KEY_INFO:
case UDP_RECEIVE_KEY_INFO_GRATUITOUS:
player = packet->data[1];
player = l_process_packet->data[1];
//current_status is a status update from the server
//it will let us know if another player has disconnected, or the games have desynced
current_status = packet->data[2];
if (packet->data[0] == UDP_RECEIVE_KEY_INFO)
l_player_lag[player] = packet->data[3];
current_status = l_process_packet->data[2];
if (l_process_packet->data[0] == UDP_RECEIVE_KEY_INFO)
l_player_lag[player] = l_process_packet->data[3];
if (current_status != l_status)
{
if (((current_status & 0x1) ^ (l_status & 0x1)) != 0)
@@ -269,9 +308,9 @@ static void netplay_process()
curr = 5;
//this loop processes input data from the server, inserting new events into the linked list for each player
//it skips events that we have already recorded, or if we receive data for an event that has already happened
for (uint8_t i = 0; i < packet->data[4]; ++i)
for (uint8_t i = 0; i < l_process_packet->data[4]; ++i)
{
count = SDLNet_Read32(&packet->data[curr]);
count = SDLNet_Read32(&l_process_packet->data[curr]);
curr += 4;
if (((count - l_cin_compats[player].netplay_count) > (UINT32_MAX / 2)) || (check_valid(player, count))) //event doesn't need to be recorded
@@ -280,9 +319,9 @@ static void netplay_process()
continue;
}
keys = SDLNet_Read32(&packet->data[curr]);
keys = SDLNet_Read32(&l_process_packet->data[curr]);
curr += 4;
plugin = packet->data[curr];
plugin = l_process_packet->data[curr];
curr += 1;
//insert new event at beginning of linked list
@@ -299,7 +338,6 @@ static void netplay_process()
break;
}
}
SDLNet_FreePacket(packet);
}
static int netplay_ensure_valid(uint8_t control_id)
@@ -383,15 +421,13 @@ static uint32_t netplay_get_input(uint8_t control_id)
static void netplay_send_input(uint8_t control_id, uint32_t keys)
{
UDPpacket *packet = SDLNet_AllocPacket(11);
packet->data[0] = UDP_SEND_KEY_INFO;
packet->data[1] = control_id; //player number
SDLNet_Write32(l_cin_compats[control_id].netplay_count, &packet->data[2]); // current event count
SDLNet_Write32(keys, &packet->data[6]); //key data
packet->data[10] = l_plugin[control_id]; //current plugin
packet->len = 11;
SDLNet_UDP_Send(l_udpSocket, l_udpChannel, packet);
SDLNet_FreePacket(packet);
l_send_input_packet->data[0] = UDP_SEND_KEY_INFO;
l_send_input_packet->data[1] = control_id; //player number
SDLNet_Write32(l_cin_compats[control_id].netplay_count, &l_send_input_packet->data[2]); // current event count
SDLNet_Write32(keys, &l_send_input_packet->data[6]); //key data
l_send_input_packet->data[10] = l_plugin[control_id]; //current plugin
l_send_input_packet->len = 11;
SDLNet_UDP_Send(l_udpSocket, l_udpChannel, l_send_input_packet);
}
uint8_t netplay_register_player(uint8_t player, uint8_t plugin, uint8_t rawdata, uint32_t reg_id)
@@ -541,22 +577,20 @@ void netplay_check_sync(struct cp0* cp0)
if (!netplay_is_init())
return;
const uint32_t* cp0_regs = r4300_cp0_regs(cp0);
if (l_vi_counter % 600 == 0)
{
uint32_t packet_len = (CP0_REGS_COUNT * 4) + 5;
UDPpacket *packet = SDLNet_AllocPacket(packet_len);
packet->data[0] = UDP_SYNC_DATA;
SDLNet_Write32(l_vi_counter, &packet->data[1]); //current VI count
const uint32_t* cp0_regs = r4300_cp0_regs(cp0);
l_check_sync_packet->data[0] = UDP_SYNC_DATA;
SDLNet_Write32(l_vi_counter, &l_check_sync_packet->data[1]); //current VI count
for (int i = 0; i < CP0_REGS_COUNT; ++i)
{
SDLNet_Write32(cp0_regs[i], &packet->data[(i * 4) + 5]);
SDLNet_Write32(cp0_regs[i], &l_check_sync_packet->data[(i * 4) + 5]);
}
packet->len = packet_len;
SDLNet_UDP_Send(l_udpSocket, l_udpChannel, packet);
SDLNet_FreePacket(packet);
l_check_sync_packet->len = l_check_sync_packet_size;
SDLNet_UDP_Send(l_udpSocket, l_udpChannel, l_check_sync_packet);
}
++l_vi_counter;
}
+6 -1
View File
@@ -76,12 +76,16 @@ static const uint8_t Z64_SIGNATURE[4] = { 0x80, 0x37, 0x12, 0x40 };
static const uint8_t V64_SIGNATURE[4] = { 0x37, 0x80, 0x40, 0x12 };
static const uint8_t N64_SIGNATURE[4] = { 0x40, 0x12, 0x37, 0x80 };
static const uint8_t ALT_Z64_SIGNATURE[4] = { 0x31, 0x37, 0x12, 0x40 };
/* Tests if a file is a valid N64 rom by checking the first 4 bytes and size */
static int is_valid_rom(const unsigned char *buffer, unsigned int size)
{
printf("rom signature: 0x%02X, 0x%02X, 0x%02X, 0x%02X\n", buffer[0], buffer[1], buffer[2], buffer[3]);
if ((memcmp(buffer, Z64_SIGNATURE, sizeof(Z64_SIGNATURE)) == 0)
|| (memcmp(buffer, V64_SIGNATURE, sizeof(V64_SIGNATURE)) == 0 && size % 2 == 0)
|| (memcmp(buffer, N64_SIGNATURE, sizeof(N64_SIGNATURE)) == 0 && size % 4 == 0))
|| (memcmp(buffer, N64_SIGNATURE, sizeof(N64_SIGNATURE)) == 0 && size % 4 == 0)
|| (memcmp(buffer, ALT_Z64_SIGNATURE, sizeof(ALT_Z64_SIGNATURE)) == 0))
return 1;
else
return 0;
@@ -149,6 +153,7 @@ m64p_error open_rom(const unsigned char* romimage, unsigned int size)
if (romimage == NULL || !is_valid_rom(romimage, size))
{
DebugMessage(M64MSG_ERROR, "open_rom(): not a valid ROM image");
printf("fail\n");
return M64ERR_INPUT_INVALID;
}
+2 -2
View File
@@ -6,7 +6,7 @@
[subrepo]
remote = git@github.com:/gonetz/GLideN64.git
branch = master
commit = 42129aadb16e0fb0e9c265ee134dbc7f9c4022e6
parent = 2c32dfd552dbfa5c2293b00fa22c4a08dd8c4eaf
commit = 9f8fc1e4c59c4cdb37112ef0c100158b25c2c1cb
parent = 3c615c84599a0ea8624c2deea4c8ba438ce988d6
method = merge
cmdver = 0.4.6
@@ -17,8 +17,9 @@ namespace graphics {
struct CachedTexture;
struct FrameBuffer;
#define VERTBUFF_SIZE 256U
#define ELEMBUFF_SIZE 1024U
constexpr u32 VERTBUFF_SIZE = 256U;
constexpr u32 VERTBUFF_MASK = VERTBUFF_SIZE - 1;
constexpr u32 ELEMBUFF_SIZE = 1024U;
constexpr f32 SCREEN_SIZE_DIM = 640.0f;
constexpr u32 MIPMAP_TILE_WIDTH = 256u;
@@ -136,7 +137,7 @@ public:
bool isRejected(u32 _v0, u32 _v1, u32 _v2) const;
SPVertex & getVertex(u32 _v) { return triangles.vertices[_v]; }
SPVertex & getVertex(u32 _v) { return triangles.vertices[_v&VERTBUFF_MASK]; }
SPVertex * getVertexPtr(u32 _v) { return triangles.vertices.data() + _v; }
@@ -12,6 +12,7 @@
#include <iomanip>
#include <fstream>
#include <ctime>
#include <chrono>
#ifndef MUPENPLUSAPI // zilmar spec
@@ -1566,6 +1566,11 @@ bool gSPCullVertices( u32 v0, u32 vn )
// Aidyn Chronicles - The First Mage seems to pass parameters in reverse order.
std::swap(v0, vn);
}
if (vn >= VERTBUFF_SIZE)
// Wrong input. DL is wrong, let's cull it.
return true;
u32 clip = 0;
GraphicsDrawer & drawer = dwnd().getDrawer();
for (u32 i = v0; i <= vn; ++i) {
+5
View File
@@ -29,7 +29,12 @@
// minizip includes
#include <unzip.h>
#ifndef ZNGLIB_H_
/* sadly older minizip-ng versions didn't include zlib.h automatically,
* so i.e voidpf would be undefined, in newer minizip-ng versions,
* including zlib.h will cause an error */
#include <zlib.h>
#endif
//
// Local Defines
+8 -1
View File
@@ -12,6 +12,7 @@
#else // Unix
#include <arpa/inet.h>
#endif // _WIN32
#include <cstring>
#define CORE_INTERNAL
#include "ConvertStringEncoding.hpp"
@@ -168,13 +169,19 @@ CORE_EXPORT bool CoreGetCurrentRomHeader(CoreRomHeader& header)
return false;
}
printf("header name len: %i\n", strlen((char*)m64p_header.Name));
header.CRC1 = ntohl(m64p_header.CRC1);
header.CRC2 = ntohl(m64p_header.CRC2);
header.CountryCode = m64p_header.Country_code;
header.Name = CoreConvertStringEncoding(std::string(reinterpret_cast<char*>(m64p_header.Name), 20), CoreStringEncoding::Shift_JIS);
header.Name = CoreConvertStringEncoding(std::string(reinterpret_cast<char*>(m64p_header.Name),
std::min(std::strlen(reinterpret_cast<char*>(m64p_header.Name)), static_cast<size_t>(20))),
CoreStringEncoding::Shift_JIS);
header.GameID = get_gameid_from_header(m64p_header);
header.Region = get_region_from_countrycode(static_cast<char>(header.CountryCode));
header.SystemType = get_systemtype_from_countrycode(header.CountryCode);
printf("header name len after: %i\n", header.Name.size());
return true;
}