Commit 3717e48e authored by nanahira's avatar nanahira

add deck code

base64 lib

temp

finish
parent 34253cef
#ifndef BASE64_H
#define BASE64_H
#include <string>
const char kBase64Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
class Base64 {
public:
static bool Encode(const std::string &in, std::string *out) {
int i = 0, j = 0;
size_t enc_len = 0;
unsigned char a3[3];
unsigned char a4[4];
out->resize(EncodedLength(in));
int input_len = in.size();
std::string::const_iterator input = in.begin();
while (input_len--) {
a3[i++] = *(input++);
if (i == 3) {
a3_to_a4(a4, a3);
for (i = 0; i < 4; i++) {
(*out)[enc_len++] = kBase64Alphabet[a4[i]];
}
i = 0;
}
}
if (i) {
for (j = i; j < 3; j++) {
a3[j] = '\0';
}
a3_to_a4(a4, a3);
for (j = 0; j < i + 1; j++) {
(*out)[enc_len++] = kBase64Alphabet[a4[j]];
}
while ((i++ < 3)) {
(*out)[enc_len++] = '=';
}
}
return (enc_len == out->size());
}
static bool Encode(const char *input, size_t input_length, char *out, size_t out_length) {
int i = 0, j = 0;
char *out_begin = out;
unsigned char a3[3];
unsigned char a4[4];
size_t encoded_length = EncodedLength(input_length);
if (out_length < encoded_length) return false;
while (input_length--) {
a3[i++] = *input++;
if (i == 3) {
a3_to_a4(a4, a3);
for (i = 0; i < 4; i++) {
*out++ = kBase64Alphabet[a4[i]];
}
i = 0;
}
}
if (i) {
for (j = i; j < 3; j++) {
a3[j] = '\0';
}
a3_to_a4(a4, a3);
for (j = 0; j < i + 1; j++) {
*out++ = kBase64Alphabet[a4[j]];
}
while ((i++ < 3)) {
*out++ = '=';
}
}
return (out == (out_begin + encoded_length));
}
static bool Decode(const std::string &in, std::string *out) {
int i = 0, j = 0;
size_t dec_len = 0;
unsigned char a3[3];
unsigned char a4[4];
int input_len = in.size();
std::string::const_iterator input = in.begin();
out->resize(DecodedLength(in));
while (input_len--) {
if (*input == '=') {
break;
}
a4[i++] = *(input++);
if (i == 4) {
for (i = 0; i <4; i++) {
a4[i] = b64_lookup(a4[i]);
}
a4_to_a3(a3,a4);
for (i = 0; i < 3; i++) {
(*out)[dec_len++] = a3[i];
}
i = 0;
}
}
if (i) {
for (j = i; j < 4; j++) {
a4[j] = '\0';
}
for (j = 0; j < 4; j++) {
a4[j] = b64_lookup(a4[j]);
}
a4_to_a3(a3,a4);
for (j = 0; j < i - 1; j++) {
(*out)[dec_len++] = a3[j];
}
}
return (dec_len == out->size());
}
static bool Decode(const char *input, size_t input_length, char *out, size_t out_length) {
int i = 0, j = 0;
char *out_begin = out;
unsigned char a3[3];
unsigned char a4[4];
size_t decoded_length = DecodedLength(input, input_length);
if (out_length < decoded_length) return false;
while (input_length--) {
if (*input == '=') {
break;
}
a4[i++] = *(input++);
if (i == 4) {
for (i = 0; i <4; i++) {
a4[i] = b64_lookup(a4[i]);
}
a4_to_a3(a3,a4);
for (i = 0; i < 3; i++) {
*out++ = a3[i];
}
i = 0;
}
}
if (i) {
for (j = i; j < 4; j++) {
a4[j] = '\0';
}
for (j = 0; j < 4; j++) {
a4[j] = b64_lookup(a4[j]);
}
a4_to_a3(a3,a4);
for (j = 0; j < i - 1; j++) {
*out++ = a3[j];
}
}
return (out == (out_begin + decoded_length));
}
static int DecodedLength(const char *in, size_t in_length) {
int numEq = 0;
const char *in_end = in + in_length;
while (*--in_end == '=') ++numEq;
return ((6 * in_length) / 8) - numEq;
}
static int DecodedLength(const std::string &in) {
int numEq = 0;
int n = in.size();
for (std::string::const_reverse_iterator it = in.rbegin(); *it == '='; ++it) {
++numEq;
}
return ((6 * n) / 8) - numEq;
}
inline static int EncodedLength(size_t length) {
return (length + 2 - ((length + 2) % 3)) / 3 * 4;
}
inline static int EncodedLength(const std::string &in) {
return EncodedLength(in.length());
}
inline static void StripPadding(std::string *in) {
while (!in->empty() && *(in->rbegin()) == '=') in->resize(in->size() - 1);
}
private:
static inline void a3_to_a4(unsigned char * a4, unsigned char * a3) {
a4[0] = (a3[0] & 0xfc) >> 2;
a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4);
a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6);
a4[3] = (a3[2] & 0x3f);
}
static inline void a4_to_a3(unsigned char * a3, unsigned char * a4) {
a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);
a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2);
a3[2] = ((a4[2] & 0x3) << 6) + a4[3];
}
static inline unsigned char b64_lookup(unsigned char c) {
if(c >='A' && c <='Z') return c - 'A';
if(c >='a' && c <='z') return c - 71;
if(c >='0' && c <='9') return c + 4;
if(c == '+') return 62;
if(c == '/') return 63;
return 255;
}
};
#endif // BASE64_H
......@@ -179,6 +179,44 @@ bool DeckBuilder::OnEvent(const irr::SEvent& event) {
}
break;
}
case BUTTON_DECK_CODE: {
int sel = mainGame->cbDBDecks->getSelected();
if(sel == -1)
break;
mainGame->gMutex.Lock();
mainGame->wDeckCode->setText(dataManager.GetSysString(1387));
wchar_t deck_code[2048];
char deck_code_utf8[1024];
deckManager.SaveDeckToCode(deckManager.current_deck, deck_code_utf8);
BufferIO::DecodeUTF8(deck_code_utf8, deck_code);
mainGame->ebDeckCode->setText(deck_code);
mainGame->PopupElement(mainGame->wDeckCode);
mainGame->gMutex.Unlock();
prev_operation = id;
prev_sel = sel;
break;
}
case BUTTON_DECK_CODE_SAVE: {
mainGame->HideElement(mainGame->wDeckCode);
if(prev_operation == BUTTON_DECK_CODE) {
Deck new_deck;
char deck_code[1024];
BufferIO::EncodeUTF8(mainGame->ebDeckCode->getText(), deck_code);
if(deckManager.LoadDeckFromCode(new_deck, deck_code, strlen(deck_code)))
deckManager.current_deck = new_deck;
else
mainGame->env->addMessageBox(L"", dataManager.GetSysString(1389));
}
prev_operation = 0;
prev_sel = -1;
break;
}
case BUTTON_DECK_CODE_CANCEL: {
mainGame->HideElement(mainGame->wDeckCode);
prev_operation = 0;
prev_sel = -1;
break;
}
case BUTTON_DELETE_DECK: {
int sel = mainGame->cbDBDecks->getSelected();
if(sel == -1)
......
......@@ -2,6 +2,7 @@
#include "data_manager.h"
#include "network.h"
#include "game.h"
#include "base64.h"
#include <algorithm>
namespace ygo {
......@@ -266,4 +267,29 @@ bool DeckManager::DeleteDeck(Deck& deck, const wchar_t* name) {
return result == 0;
#endif
}
bool DeckManager::LoadDeckFromCode(Deck& deck, const char *code, int len) {
char data[1024], *pdeck = data, *data_ = data;
int decoded_len = Base64::DecodedLength(code, len);
if(decoded_len < 8 || !Base64::Decode(code, len, data_, decoded_len))
return false;
int mainc = BufferIO::ReadInt32(pdeck);
int sidec = BufferIO::ReadInt32(pdeck);
int errorcode = LoadDeck(deck, (int*)pdeck, mainc, sidec);
return (errorcode == 0);
}
int DeckManager::SaveDeckToCode(Deck& deck, char* code) {
char deckbuf[1024], *pdeck = deckbuf;
BufferIO::WriteInt32(pdeck, deck.main.size() + deck.extra.size());
BufferIO::WriteInt32(pdeck, deck.side.size());
for(size_t i = 0; i < deck.main.size(); ++i)
BufferIO::WriteInt32(pdeck, deck.main[i]->first);
for(size_t i = 0; i < deck.extra.size(); ++i)
BufferIO::WriteInt32(pdeck, deck.extra[i]->first);
for(size_t i = 0; i < deck.side.size(); ++i)
BufferIO::WriteInt32(pdeck, deck.side[i]->first);
int len = pdeck - deckbuf;
int encoded_len = Base64::EncodedLength(len);
Base64::Encode(deckbuf, len, code, encoded_len);
return encoded_len;
}
}
......@@ -45,6 +45,8 @@ public:
bool LoadDeck(const wchar_t* file);
bool SaveDeck(Deck& deck, const wchar_t* name);
bool DeleteDeck(Deck& deck, const wchar_t* name);
bool LoadDeckFromCode(Deck& deck, const char *code, int len);
int SaveDeckToCode(Deck &deck, char *code);
};
extern DeckManager deckManager;
......
......@@ -515,11 +515,25 @@ bool Game::Initialize() {
btnSideSort->setVisible(false);
btnSideReload = env->addButton(rect<s32>(440, 100, 500, 130), 0, BUTTON_SIDE_RELOAD, dataManager.GetSysString(1309));
btnSideReload->setVisible(false);
<<<<<<< HEAD
=======
btnRenameDeck = env->addButton(rect<s32>(170, 99, 220, 120), wDeckEdit, BUTTON_RENAME_DECK, dataManager.GetSysString(1362));
btnDeckCode = env->addButton(rect<s32>(225, 5, 290, 30), wDeckEdit, BUTTON_DECK_CODE, dataManager.GetSysString(1387));
>>>>>>> df2434cc... finish
//
scrFilter = env->addScrollBar(false, recti(999, 161, 1019, 629), 0, SCROLL_FILTER);
scrFilter->setLargeStep(10);
scrFilter->setSmallStep(1);
scrFilter->setVisible(false);
//deck code
wDeckCode = env->addWindow(rect<s32>(510, 200, 820, 320), false, dataManager.GetSysString(1387));
wDeckCode->getCloseButton()->setVisible(false);
wDeckCode->setVisible(false);
env->addStaticText(dataManager.GetSysString(1388), rect<s32>(20, 25, 290, 45), false, false, wDeckCode);
ebDeckCode = env->addEditBox(L"", rect<s32>(20, 50, 290, 70), true, wDeckCode, -1);
ebREName->setTextAlignment(irr::gui::EGUIA_CENTER, irr::gui::EGUIA_CENTER);
btnDeckCodeYes = env->addButton(rect<s32>(70, 80, 140, 105), wDeckCode, BUTTON_DECK_CODE_SAVE, dataManager.GetSysString(1341));
btnDeckCodeNo = env->addButton(rect<s32>(170, 80, 240, 105), wDeckCode, BUTTON_DECK_CODE_CANCEL, dataManager.GetSysString(1212));
//sort type
wSort = env->addStaticText(L"", rect<s32>(930, 132, 1020, 156), true, false, 0, -1, true);
cbSortType = env->addComboBox(rect<s32>(10, 2, 85, 22), wSort, COMBOBOX_SORTTYPE);
......@@ -1565,6 +1579,11 @@ void Game::OnResize() {
btnSideSort->setRelativePosition(Resize(375, 100, 435, 130));
btnSideReload->setRelativePosition(Resize(440, 100, 500, 130));
btnDeleteDeck->setRelativePosition(Resize(225, 95, 290, 120));
<<<<<<< HEAD
=======
btnRenameDeck->setRelativePosition(Resize(170, 99, 220, 120));
btnDeckCode->setRelativePosition(Resize(225, 5, 290, 30));
>>>>>>> df2434cc... finish
wLanWindow->setRelativePosition(ResizeWin(220, 100, 800, 520));
wCreateHost->setRelativePosition(ResizeWin(320, 100, 700, 520));
......
......@@ -461,6 +461,13 @@ public:
irr::gui::IGUIButton* btnSideSort;
irr::gui::IGUIButton* btnSideReload;
irr::gui::IGUIEditBox* ebDeckname;
irr::gui::IGUIButton* btnDeckCode;
//deck code
irr::gui::IGUIWindow* wDeckCode;
irr::gui::IGUIEditBox* ebDeckCode;
irr::gui::IGUIButton* btnDeckCodeYes;
irr::gui::IGUIButton* btnDeckCodeNo;
//
irr::gui::IGUIStaticText* stBanlist;
irr::gui::IGUIStaticText* stDeck;
irr::gui::IGUIStaticText* stCategory;
......@@ -693,6 +700,10 @@ extern Game* mainGame;
#define BUTTON_MARKS_FILTER 380
#define BUTTON_MARKERS_OK 381
#define BUTTON_DECK_CODE 389
#define BUTTON_DECK_CODE_SAVE 390
#define BUTTON_DECK_CODE_CANCEL 391
#define DEFAULT_DUEL_RULE 4
#define CARD_ARTWORK_VERSIONS_OFFSET 10
......
Subproject commit 3cbdbf5a970b3ac5d8dcf0c6550898c41f5274e0
Subproject commit e8544068c0ce285d9a2ba5b002cd544e7754444e
Subproject commit 68a3de27cbd22e6cf74157feeaa2b8e5ee455636
Subproject commit 3b2030eeaff8558f81a8c635451b5be95ccacb33
......@@ -413,6 +413,9 @@
!system 1383 使用旧规则(大师规则3)
!system 1384 电脑锁定出剪刀
!system 1385 列表为空,可能未安装合适的人机
!system 1387 卡组代码
!system 1388 卡组代码:
!system 1389 卡组代码无效。
!system 1390 等待行动中...
!system 1391 等待行动中....
!system 1392 等待行动中.....
......
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