Commit 080df2c7 authored by fallenstardust's avatar fallenstardust

update Gframe

parent d65b2e45
......@@ -390,36 +390,26 @@ uint32_t DataManager::CardReader(uint32_t code, card_data* pData) {
}
unsigned char* DataManager::ScriptReaderEx(const char* script_name, int* slen) {
// default script name: ./script/c%d.lua
char first[256]{};
char second[256]{};
if (std::strncmp(script_name, "./script", 8) != 0)
return DefaultScriptReader(script_name, slen);
char expansions_path[1024]{};
std::snprintf(expansions_path, sizeof expansions_path, "./expansions/%s", script_name + 2);
if(mainGame->gameConf.prefer_expansion_script) {
snprintf(first, sizeof first, "expansions/%s", script_name + 2);
snprintf(second, sizeof second, "%s", script_name + 2);
} else {
snprintf(first, sizeof first, "%s", script_name + 2);
snprintf(second, sizeof second, "expansions/%s", script_name + 2);
}
if(mainGame->gameConf.prefer_expansion_script) {
if(ScriptReader(first, slen))
if (DefaultScriptReader(expansions_path, slen))
return scriptBuffer;
if(ScriptReader(second, slen))
else if (ScriptReaderZip(script_name + 2, slen))
return scriptBuffer;
}
if(ScriptReaderZip(first, slen))
else if (DefaultScriptReader(script_name, slen))
return scriptBuffer;
else
return ScriptReaderZip(second, slen);
}
unsigned char* DataManager::ScriptReader(const char* script_name, int* slen) {
FILE *fp = fopen(script_name, "rb");
if(!fp)
return 0;
int len = fread(scriptBuffer, 1, sizeof(scriptBuffer), fp);
fclose(fp);
if(len >= sizeof(scriptBuffer))
return 0;
*slen = len;
} else {
if (DefaultScriptReader(script_name, slen))
return scriptBuffer;
else if (DefaultScriptReader(expansions_path, slen))
return scriptBuffer;
else if (ScriptReaderZip(script_name + 2, slen))
return scriptBuffer;
}
return nullptr;
}
unsigned char* DataManager::ScriptReaderZip(const char* script_name, int* slen) {
IReadFile* reader = FileSystem->createAndOpenFile(script_name);
......@@ -435,5 +425,18 @@ unsigned char* DataManager::ScriptReaderZip(const char* script_name, int* slen)
*slen = (int)size;
return scriptBuffer;
}
unsigned char* DataManager::DefaultScriptReader(const char* script_name, int* slen) {
wchar_t fname[256]{};
BufferIO::DecodeUTF8(script_name, fname);
FILE* fp = myfopen(fname, "rb");
if (!fp)
return nullptr;
size_t len = std::fread(scriptBuffer, 1, sizeof scriptBuffer, fp);
std::fclose(fp);
if (len >= sizeof scriptBuffer)
return nullptr;
*slen = (int)len;
return scriptBuffer;
}
}
......@@ -54,7 +54,11 @@ public:
static const wchar_t* unknown_string;
static uint32_t CardReader(uint32_t, card_data*);
static unsigned char* ScriptReaderEx(const char* script_name, int* slen);
//read by IFileSystem
static unsigned char* ScriptReader(const char* script_name, int* slen);
//read by fread
static unsigned char* DefaultScriptReader(const char* script_name, int* slen);
static unsigned char* ScriptReaderZip(const char* script_name, int* slen);
static IFileSystem* FileSystem;
......
#include "config.h"
#include "duelclient.h"
#include "client_card.h"
#include "materials.h"
#include "image_manager.h"
#include "sound_manager.h"
#include "single_mode.h"
#include "../ocgcore/common.h"
#include "game.h"
#include "deck_manager.h"
#include "replay.h"
#include "replay_mode.h"
#include <thread>
#ifdef _IRR_ANDROID_PLATFORM_
#include <android/android_tools.h>
......@@ -16,7 +17,7 @@ namespace ygo {
unsigned DuelClient::connect_state = 0;
unsigned char DuelClient::response_buf[SIZE_RETURN_VALUE];
unsigned int DuelClient::response_len = 0;
size_t DuelClient::response_len = 0;
unsigned int DuelClient::watching = 0;
unsigned char DuelClient::selftype = 0;
bool DuelClient::is_host = false;
......@@ -29,7 +30,7 @@ int DuelClient::select_hint = 0;
int DuelClient::select_unselect_hint = 0;
int DuelClient::last_select_hint = 0;
unsigned char DuelClient::last_successful_msg[0x2000];
unsigned int DuelClient::last_successful_msg_length = 0;
size_t DuelClient::last_successful_msg_length = 0;
wchar_t DuelClient::event_string[256];
mt19937 DuelClient::rnd;
......@@ -51,7 +52,6 @@ bool DuelClient::StartClient(unsigned int ip, unsigned short port, bool create_g
sin.sin_addr.s_addr = htonl(ip);
sin.sin_port = htons(port);
client_bev = bufferevent_socket_new(client_base, -1, BEV_OPT_CLOSE_ON_FREE);
bufferevent_setwatermark(client_bev, EV_READ, 3, 0);
bufferevent_setcb(client_bev, ClientRead, nullptr, ClientEvent, (void*)create_game);
if (bufferevent_socket_connect(client_bev, (sockaddr*)&sin, sizeof(sin)) < 0) {
bufferevent_free(client_bev);
......@@ -102,19 +102,16 @@ void DuelClient::StopClient(bool is_exiting) {
void DuelClient::ClientRead(bufferevent* bev, void* ctx) {
evbuffer* input = bufferevent_get_input(bev);
int len = evbuffer_get_length(input);
if (len < 2)
return;
unsigned char* duel_client_read = new unsigned char[SIZE_NETWORK_BUFFER];
unsigned short packet_len;
uint16_t packet_len = 0;
while (len >= 2) {
evbuffer_copyout(input, &packet_len, sizeof packet_len);
if (packet_len + 2 > SIZE_NETWORK_BUFFER) {
delete[] duel_client_read;
ClientEvent(bev, BEV_EVENT_ERROR, 0);
return;
}
if (len < packet_len + 2)
break;
int read_len = evbuffer_remove(input, duel_client_read, packet_len + 2);
if (read_len >= 3)
if (read_len > 2)
HandleSTOCPacketLan(&duel_client_read[2], read_len - 2);
len -= packet_len + 2;
}
......@@ -4110,11 +4107,11 @@ int DuelClient::ClientAnalyze(unsigned char* msg, unsigned int len) {
void DuelClient::SwapField() {
is_swapping = true;
}
void DuelClient::SetResponseI(int respI) {
void DuelClient::SetResponseI(int32_t respI) {
std::memcpy(response_buf, &respI, sizeof respI);
response_len = 4;
response_len = sizeof respI;
}
void DuelClient::SetResponseB(void* respB, unsigned int len) {
void DuelClient::SetResponseB(void* respB, size_t len) {
if (len > SIZE_RETURN_VALUE)
len = SIZE_RETURN_VALUE;
std::memcpy(response_buf, respB, len);
......@@ -4286,7 +4283,7 @@ void DuelClient::BroadcastReply(evutil_socket_t fd, short events, void * arg) {
else hoststr.append(dataManager.GetSysString(1248));
hoststr.append(L"]");
wchar_t gamename[20];
BufferIO::CopyWStr(pHP->name, gamename, 20);
BufferIO::CopyCharArray(pHP->name, gamename);
hoststr.append(gamename);
mainGame->lstHostList->addItem(hoststr.c_str());
mainGame->gMutex.unlock();
......
#ifndef DUELCLIENT_H
#define DUELCLIENT_H
#include "config.h"
#include <vector>
#include <set>
#include <utility>
#include "network.h"
#include "data_manager.h"
#include "deck_manager.h"
#include "../ocgcore/mtrandom.h"
namespace ygo {
......@@ -16,7 +12,7 @@ class DuelClient {
private:
static unsigned int connect_state;
static unsigned char response_buf[SIZE_RETURN_VALUE];
static unsigned int response_len;
static size_t response_len;
static unsigned int watching;
static bool is_host;
static event_base* client_base;
......@@ -28,7 +24,7 @@ private:
static int select_unselect_hint;
static int last_select_hint;
static unsigned char last_successful_msg[0x2000];
static unsigned int last_successful_msg_length;
static size_t last_successful_msg_length;
static wchar_t event_string[256];
static mt19937 rnd;
static bool is_refreshing;
......@@ -47,8 +43,8 @@ public:
static void HandleSTOCPacketLan(unsigned char* data, int len);
static int ClientAnalyze(unsigned char* msg, unsigned int len);
static void SwapField();
static void SetResponseI(int respI);
static void SetResponseB(void* respB, unsigned int len);
static void SetResponseI(int32_t respI);
static void SetResponseB(void* respB, size_t len);
static void SendResponse();
static void SendPacketToServer(unsigned char proto) {
auto p = duel_client_write;
......
......@@ -6,6 +6,8 @@
#include "duelclient.h"
#include "data_manager.h"
#include "image_manager.h"
#include "sound_manager.h"
#include "deck_manager.h"
#include "replay_mode.h"
#include "single_mode.h"
#include "materials.h"
......
#include "config.h"
#include "netserver.h"
#include "single_duel.h"
#include "tag_duel.h"
#include "deck_manager.h"
#include <thread>
namespace ygo {
......@@ -108,7 +110,6 @@ void NetServer::ServerAccept(evconnlistener* listener, evutil_socket_t fd, socka
dp.type = 0xff;
dp.bev = bev;
users[bev] = dp;
bufferevent_setwatermark(bev, EV_READ, 3, 0);
bufferevent_setcb(bev, ServerEchoRead, nullptr, ServerEchoEvent, nullptr);
bufferevent_enable(bev, EV_READ);
}
......@@ -123,19 +124,16 @@ void NetServer::ServerAcceptError(evconnlistener* listener, void* ctx) {
void NetServer::ServerEchoRead(bufferevent *bev, void *ctx) {
evbuffer* input = bufferevent_get_input(bev);
int len = evbuffer_get_length(input);
if (len < 2)
return;
unsigned char* net_server_read = new unsigned char[SIZE_NETWORK_BUFFER];
unsigned short packet_len;
uint16_t packet_len = 0;
while (len >= 2) {
evbuffer_copyout(input, &packet_len, sizeof packet_len);
if (packet_len + 2 > SIZE_NETWORK_BUFFER) {
delete[] net_server_read;
ServerEchoEvent(bev, BEV_EVENT_ERROR, 0);
return;
}
if (len < packet_len + 2)
break;
int read_len = evbuffer_remove(input, net_server_read, packet_len + 2);
if (read_len >= 3)
if (read_len > 2)
HandleCTOSPacket(&users[bev], &net_server_read[2], read_len - 2);
len -= packet_len + 2;
}
......
#ifndef NETSERVER_H
#define NETSERVER_H
#include "config.h"
#include "network.h"
#include "data_manager.h"
#include "deck_manager.h"
#include <set>
#include <unordered_map>
namespace ygo {
......
#ifndef NETWORK_H
#define NETWORK_H
#include "config.h"
#include "deck_manager.h"
#include <cstdint>
#include <cstring>
#include <event2/event.h>
#include <event2/listener.h>
#include <event2/bufferevent.h>
......@@ -13,7 +13,7 @@
#define check_trivially_copyable(T) static_assert(std::is_trivially_copyable<T>::value == true && std::is_standard_layout<T>::value == true, "not trivially copyable")
namespace ygo {
constexpr int SIZE_NETWORK_BUFFER = 0x2000;
constexpr int SIZE_NETWORK_BUFFER = 0x20000;
constexpr int MAX_DATA_SIZE = SIZE_NETWORK_BUFFER - 3;
constexpr int MAINC_MAX = 250; // the limit of card_state
constexpr int SIDEC_MAX = MAINC_MAX;
......@@ -191,7 +191,7 @@ struct DuelPlayer {
inline bool check_msg_size(int size) {
// empty string is not allowed
if (size < 2* sizeof(uint16_t))
if (size < 2 * sizeof(uint16_t))
return false;
if (size > LEN_CHAT_MSG * sizeof(uint16_t))
return false;
......
#include "replay_mode.h"
#include "duelclient.h"
#include "game.h"
#include "../ocgcore/common.h"
#include "data_manager.h"
#include "../ocgcore/mtrandom.h"
#include <thread>
......
#include "config.h"
#include "single_duel.h"
#include "netserver.h"
#include "game.h"
#include "../ocgcore/ocgapi.h"
#include "../ocgcore/common.h"
#include "data_manager.h"
#include "../ocgcore/mtrandom.h"
namespace ygo {
......
#ifndef SINGLE_DUEL_H
#define SINGLE_DUEL_H
#include "config.h"
#include "network.h"
#include "deck_manager.h"
#include "replay.h"
namespace ygo {
......
#include "single_mode.h"
#include "duelclient.h"
#include "game.h"
#include "../ocgcore/common.h"
#include "data_manager.h"
#include "../ocgcore/mtrandom.h"
#include <thread>
......
#include "config.h"
#include "tag_duel.h"
#include "netserver.h"
#include "game.h"
#include "../ocgcore/ocgapi.h"
#include "../ocgcore/common.h"
#include "data_manager.h"
#include "../ocgcore/mtrandom.h"
namespace ygo {
......
#ifndef TAG_DUEL_H
#define TAG_DUEL_H
#include "config.h"
#include "network.h"
#include "deck_manager.h"
#include "replay.h"
namespace ygo {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment