Commit ae3da991 authored by mercury233's avatar mercury233 Committed by GitHub

use one ScriptReaderEx (#2165)

parent c13ed7b6
...@@ -5,6 +5,7 @@ namespace ygo { ...@@ -5,6 +5,7 @@ namespace ygo {
const wchar_t* DataManager::unknown_string = L"???"; const wchar_t* DataManager::unknown_string = L"???";
wchar_t DataManager::strBuffer[4096]; wchar_t DataManager::strBuffer[4096];
byte DataManager::scriptBuffer[0x20000];
DataManager dataManager; DataManager dataManager;
bool DataManager::LoadDB(const char* file) { bool DataManager::LoadDB(const char* file) {
...@@ -309,5 +310,31 @@ int DataManager::CardReader(int code, void* pData) { ...@@ -309,5 +310,31 @@ int DataManager::CardReader(int code, void* pData) {
memset(pData, 0, sizeof(CardData)); memset(pData, 0, sizeof(CardData));
return 0; return 0;
} }
byte* DataManager::ScriptReaderEx(const char* script_name, int* slen) {
char exname[256] = "./expansions";
strcat(exname, script_name + 1);//default script name: ./script/c%d.lua
if(ScriptReader(exname, slen))
return scriptBuffer;
else
return ScriptReader(script_name, slen);
}
byte* DataManager::ScriptReader(const char* script_name, int* slen) {
FILE *fp;
#ifdef _WIN32
wchar_t fname[256];
BufferIO::DecodeUTF8(script_name, fname);
fp = _wfopen(fname, L"rb");
#else
fp = fopen(script_name, "rb");
#endif
if(!fp)
return 0;
int len = fread(scriptBuffer, 1, sizeof(scriptBuffer), fp);
fclose(fp);
if(len >= sizeof(scriptBuffer))
return 0;
*slen = len;
return scriptBuffer;
}
} }
...@@ -49,8 +49,11 @@ public: ...@@ -49,8 +49,11 @@ public:
wchar_t lmBuffer[32]; wchar_t lmBuffer[32];
static wchar_t strBuffer[4096]; static wchar_t strBuffer[4096];
static byte scriptBuffer[0x20000];
static const wchar_t* unknown_string; static const wchar_t* unknown_string;
static int CardReader(int, void*); static int CardReader(int, void*);
static byte* ScriptReaderEx(const char* script_name, int* slen);
static byte* ScriptReader(const char* script_name, int* slen);
}; };
......
...@@ -63,15 +63,9 @@ int ReplayMode::ReplayThread(void* param) { ...@@ -63,15 +63,9 @@ int ReplayMode::ReplayThread(void* param) {
mainGame->dInfo.isSingleMode = !!(rh.flag & REPLAY_SINGLE_MODE); mainGame->dInfo.isSingleMode = !!(rh.flag & REPLAY_SINGLE_MODE);
mainGame->dInfo.tag_player[0] = false; mainGame->dInfo.tag_player[0] = false;
mainGame->dInfo.tag_player[1] = false; mainGame->dInfo.tag_player[1] = false;
if(mainGame->dInfo.isSingleMode) { set_script_reader((script_reader)DataManager::ScriptReaderEx);
set_script_reader((script_reader)SingleMode::ScriptReaderEx); set_card_reader((card_reader)DataManager::CardReader);
set_card_reader((card_reader)DataManager::CardReader); set_message_handler((message_handler)MessageHandler);
set_message_handler((message_handler)MessageHandler);
} else {
set_script_reader((script_reader)ScriptReaderEx);
set_card_reader((card_reader)DataManager::CardReader);
set_message_handler((message_handler)MessageHandler);
}
if(!StartDuel()) { if(!StartDuel()) {
EndDuel(); EndDuel();
return 0; return 0;
...@@ -935,15 +929,6 @@ void ReplayMode::ReplayReload() { ...@@ -935,15 +929,6 @@ void ReplayMode::ReplayReload() {
/*len = */query_field_card(pduel, 1, LOCATION_REMOVED, flag, queryBuffer, 0); /*len = */query_field_card(pduel, 1, LOCATION_REMOVED, flag, queryBuffer, 0);
mainGame->dField.UpdateFieldCard(mainGame->LocalPlayer(1), LOCATION_REMOVED, (char*)queryBuffer); mainGame->dField.UpdateFieldCard(mainGame->LocalPlayer(1), LOCATION_REMOVED, (char*)queryBuffer);
} }
byte* ReplayMode::ScriptReaderEx(const char* script_name, int* slen) {
char sname[256] = "./expansions";
strcat(sname, script_name + 1);//default script name: ./script/c%d.lua
byte* buffer = default_script_reader(sname, slen);
if(buffer)
return buffer;
else
return default_script_reader(script_name, slen);
}
int ReplayMode::MessageHandler(long fduel, int type) { int ReplayMode::MessageHandler(long fduel, int type) {
if(!enable_log) if(!enable_log)
return 0; return 0;
......
...@@ -47,7 +47,6 @@ public: ...@@ -47,7 +47,6 @@ public:
static void ReplayRefreshSingle(int player, int location, int sequence, int flag = 0xf81fff); static void ReplayRefreshSingle(int player, int location, int sequence, int flag = 0xf81fff);
static void ReplayReload(); static void ReplayReload();
static byte* ScriptReaderEx(const char* script_name, int* slen);
static int MessageHandler(long fduel, int type); static int MessageHandler(long fduel, int type);
}; };
......
...@@ -406,7 +406,7 @@ void SingleDuel::TPResult(DuelPlayer* dp, unsigned char tp) { ...@@ -406,7 +406,7 @@ void SingleDuel::TPResult(DuelPlayer* dp, unsigned char tp) {
} }
time_limit[0] = host_info.time_limit; time_limit[0] = host_info.time_limit;
time_limit[1] = host_info.time_limit; time_limit[1] = host_info.time_limit;
set_script_reader((script_reader)ScriptReaderEx); set_script_reader((script_reader)DataManager::ScriptReaderEx);
set_card_reader((card_reader)DataManager::CardReader); set_card_reader((card_reader)DataManager::CardReader);
set_message_handler((message_handler)SingleDuel::MessageHandler); set_message_handler((message_handler)SingleDuel::MessageHandler);
rnd.reset(seed); rnd.reset(seed);
...@@ -1544,15 +1544,6 @@ void SingleDuel::RefreshSingle(int player, int location, int sequence, int flag) ...@@ -1544,15 +1544,6 @@ void SingleDuel::RefreshSingle(int player, int location, int sequence, int flag)
NetServer::ReSendToPlayer(*pit); NetServer::ReSendToPlayer(*pit);
} }
} }
byte* SingleDuel::ScriptReaderEx(const char* script_name, int* slen) {
char sname[256] = "./expansions";
strcat(sname, script_name + 1);//default script name: ./script/c%d.lua
byte* buffer = default_script_reader(sname, slen);
if(buffer)
return buffer;
else
return default_script_reader(script_name, slen);
}
int SingleDuel::MessageHandler(long fduel, int type) { int SingleDuel::MessageHandler(long fduel, int type) {
if(!enable_log) if(!enable_log)
return 0; return 0;
......
...@@ -38,7 +38,6 @@ public: ...@@ -38,7 +38,6 @@ public:
void RefreshExtra(int player, int flag = 0x81fff, int use_cache = 1); void RefreshExtra(int player, int flag = 0x81fff, int use_cache = 1);
void RefreshSingle(int player, int location, int sequence, int flag = 0xf81fff); void RefreshSingle(int player, int location, int sequence, int flag = 0xf81fff);
static byte* ScriptReaderEx(const char* script_name, int* slen);
static int MessageHandler(long fduel, int type); static int MessageHandler(long fduel, int type);
static void SingleTimer(evutil_socket_t fd, short events, void* arg); static void SingleTimer(evutil_socket_t fd, short events, void* arg);
......
...@@ -11,8 +11,6 @@ bool SingleMode::is_closing = false; ...@@ -11,8 +11,6 @@ bool SingleMode::is_closing = false;
bool SingleMode::is_continuing = false; bool SingleMode::is_continuing = false;
Replay SingleMode::last_replay; Replay SingleMode::last_replay;
static byte buffer[0x20000];
bool SingleMode::StartPlay() { bool SingleMode::StartPlay() {
Thread::NewThread(SinglePlayThread, 0); Thread::NewThread(SinglePlayThread, 0);
return true; return true;
...@@ -38,7 +36,7 @@ int SingleMode::SinglePlayThread(void* param) { ...@@ -38,7 +36,7 @@ int SingleMode::SinglePlayThread(void* param) {
mtrandom rnd; mtrandom rnd;
time_t seed = time(0); time_t seed = time(0);
rnd.reset(seed); rnd.reset(seed);
set_script_reader((script_reader)ScriptReaderEx); set_script_reader((script_reader)DataManager::ScriptReaderEx);
set_card_reader((card_reader)DataManager::CardReader); set_card_reader((card_reader)DataManager::CardReader);
set_message_handler((message_handler)MessageHandler); set_message_handler((message_handler)MessageHandler);
pduel = create_duel(rnd.rand()); pduel = create_duel(rnd.rand());
...@@ -852,32 +850,6 @@ void SingleMode::SinglePlayReload() { ...@@ -852,32 +850,6 @@ void SingleMode::SinglePlayReload() {
/*len = */query_field_card(pduel, 1, LOCATION_REMOVED, flag, queryBuffer, 0); /*len = */query_field_card(pduel, 1, LOCATION_REMOVED, flag, queryBuffer, 0);
mainGame->dField.UpdateFieldCard(mainGame->LocalPlayer(1), LOCATION_REMOVED, (char*)queryBuffer); mainGame->dField.UpdateFieldCard(mainGame->LocalPlayer(1), LOCATION_REMOVED, (char*)queryBuffer);
} }
byte* SingleMode::ScriptReaderEx(const char* script_name, int* slen) {
char sname[256] = "./expansions";
strcat(sname, script_name + 1);//default script name: ./script/c%d.lua
if(ScriptReader(sname, slen))
return buffer;
else
return ScriptReader(script_name, slen);
}
byte* SingleMode::ScriptReader(const char* script_name, int* slen) {
FILE *fp;
#ifdef _WIN32
wchar_t fname[256];
BufferIO::DecodeUTF8(script_name, fname);
fp = _wfopen(fname, L"rb");
#else
fp = fopen(script_name, "rb");
#endif
if(!fp)
return 0;
int len = fread(buffer, 1, sizeof(buffer), fp);
fclose(fp);
if(len >= sizeof(buffer))
return 0;
*slen = len;
return buffer;
}
int SingleMode::MessageHandler(long fduel, int type) { int SingleMode::MessageHandler(long fduel, int type) {
if(!enable_log) if(!enable_log)
return 0; return 0;
......
...@@ -26,8 +26,6 @@ public: ...@@ -26,8 +26,6 @@ public:
static void SinglePlayRefreshSingle(int player, int location, int sequence, int flag = 0xf81fff); static void SinglePlayRefreshSingle(int player, int location, int sequence, int flag = 0xf81fff);
static void SinglePlayReload(); static void SinglePlayReload();
static byte* ScriptReaderEx(const char* script_name, int* slen);
static byte* ScriptReader(const char* script_name, int* slen);
static int MessageHandler(long fduel, int type); static int MessageHandler(long fduel, int type);
protected: protected:
......
...@@ -381,7 +381,7 @@ void TagDuel::TPResult(DuelPlayer* dp, unsigned char tp) { ...@@ -381,7 +381,7 @@ void TagDuel::TPResult(DuelPlayer* dp, unsigned char tp) {
} }
time_limit[0] = host_info.time_limit; time_limit[0] = host_info.time_limit;
time_limit[1] = host_info.time_limit; time_limit[1] = host_info.time_limit;
set_script_reader((script_reader)ScriptReaderEx); set_script_reader((script_reader)DataManager::ScriptReaderEx);
set_card_reader((card_reader)DataManager::CardReader); set_card_reader((card_reader)DataManager::CardReader);
set_message_handler((message_handler)TagDuel::MessageHandler); set_message_handler((message_handler)TagDuel::MessageHandler);
rnd.reset(seed); rnd.reset(seed);
...@@ -1661,15 +1661,6 @@ void TagDuel::RefreshSingle(int player, int location, int sequence, int flag) { ...@@ -1661,15 +1661,6 @@ void TagDuel::RefreshSingle(int player, int location, int sequence, int flag) {
} }
} }
} }
byte* TagDuel::ScriptReaderEx(const char* script_name, int* slen) {
char sname[256] = "./expansions";
strcat(sname, script_name + 1);//default script name: ./script/c%d.lua
byte* buffer = default_script_reader(sname, slen);
if(buffer)
return buffer;
else
return default_script_reader(script_name, slen);
}
int TagDuel::MessageHandler(long fduel, int type) { int TagDuel::MessageHandler(long fduel, int type) {
if(!enable_log) if(!enable_log)
return 0; return 0;
......
...@@ -38,7 +38,6 @@ public: ...@@ -38,7 +38,6 @@ public:
void RefreshExtra(int player, int flag = 0x81fff, int use_cache = 1); void RefreshExtra(int player, int flag = 0x81fff, int use_cache = 1);
void RefreshSingle(int player, int location, int sequence, int flag = 0xf81fff); void RefreshSingle(int player, int location, int sequence, int flag = 0xf81fff);
static byte* ScriptReaderEx(const char* script_name, int* slen);
static int MessageHandler(long fduel, int type); static int MessageHandler(long fduel, int type);
static void TagTimer(evutil_socket_t fd, short events, void* arg); static void TagTimer(evutil_socket_t fd, short events, void* arg);
......
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