Commit 08c178c4 authored by Nanahira's avatar Nanahira Committed by GitHub

Adapt new replay seed thing (#32)

* refa pre_seed to base64

* refa pre_seed to base64

* fix all zero seed

---------
Co-authored-by: mercury233's avatarmercury233 <me@mercury233.me>
parent 25de2c54
#ifndef BASE64_H
#define BASE64_H
#include <string>
#include <cstring>
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 unsigned char *input, size_t input_length, unsigned char *out, size_t out_length) {
int i = 0, j = 0;
unsigned char *out_begin = out;
unsigned char a3[3];
unsigned char a4[4];
size_t encoded_length = EncodedLength(input_length);
if (out_length < encoded_length + 1) return false;
std::memset(out, 0, out_length);
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 unsigned char *input, size_t input_length, unsigned char *out, size_t out_length) {
int i = 0, j = 0;
unsigned 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 unsigned char *in, size_t in_length) {
int numEq = 0;
const unsigned 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
......@@ -71,7 +71,8 @@ void DuelInfo::Clear() {
#ifdef YGOPRO_SERVER_MODE
unsigned short server_port;
unsigned short replay_mode;
unsigned int pre_seed[3];
uint32_t pre_seed[MAX_MATCH_COUNT][SEED_COUNT];
uint8_t pre_seed_specified[MAX_MATCH_COUNT];
HostInfo game_info;
void Game::MainServerLoop() {
......
......@@ -660,10 +660,13 @@ public:
extern Game* mainGame;
#ifdef YGOPRO_SERVER_MODE
#define MAX_MATCH_COUNT 3
extern unsigned short server_port;
extern unsigned short replay_mode;
extern HostInfo game_info;
extern unsigned int pre_seed[3];
extern uint32_t pre_seed[MAX_MATCH_COUNT][SEED_COUNT];
extern uint8_t pre_seed_specified[MAX_MATCH_COUNT];
#endif
}
......
......@@ -7,6 +7,9 @@
#ifdef __APPLE__
#import <CoreFoundation/CoreFoundation.h>
#endif
#ifdef YGOPRO_SERVER_MODE
#include "base64.h"
#endif
unsigned int enable_log = 0x3;
#ifndef YGOPRO_SERVER_MODE
......@@ -82,8 +85,8 @@ int main(int argc, char* argv[]) {
ygo::game_info.no_shuffle_deck = false;
ygo::game_info.duel_rule = DEFAULT_DUEL_RULE;
ygo::game_info.time_limit = 180;
for (int i = 0; i < 3; ++i)
ygo::pre_seed[i] = (unsigned int)0;
std::memset(ygo::pre_seed, 0, sizeof(ygo::pre_seed));
std::memset(ygo::pre_seed_specified, 0, sizeof(ygo::pre_seed_specified));
if (argc > 1) {
ygo::server_port = atoi(argv[1]);
int lflist = atoi(argv[2]);
......@@ -119,9 +122,28 @@ int main(int argc, char* argv[]) {
ygo::game_info.draw_count = atoi(argv[10]);
ygo::game_info.time_limit = atoi(argv[11]);
ygo::replay_mode = atoi(argv[12]);
for (int i = 13; (i < argc && i <= 15) ; ++i)
for (int i = 13; (i < argc && i < (13 + MAX_MATCH_COUNT)) ; ++i)
{
ygo::pre_seed[i - 13] = (unsigned int)atol(argv[i]);
auto ok = Base64::Decode(
reinterpret_cast<const unsigned char*>(argv[i]),
strlen(argv[i]),
reinterpret_cast<unsigned char*>(ygo::pre_seed[i - 13]),
SEED_COUNT * sizeof(uint32_t)
);
if(ok) {
// check if it isn't all zero
bool all_zero = true;
for (int j = 0; j < SEED_COUNT; ++j) {
if (ygo::pre_seed[i - 13][j] != 0) {
all_zero = false;
break;
}
}
if (!all_zero)
ygo::pre_seed_specified[i - 13] = 1;
}
else
std::fprintf(stderr, "Failed to decode seed %d: %s\n", i - 13, argv[i]);
}
}
ygo::mainGame = &_game;
......
......@@ -546,6 +546,11 @@ void SingleDuel::TPResult(DuelPlayer* dp, unsigned char tp) {
rh.base.version = PRO_VERSION;
rh.base.flag = REPLAY_UNIFORM;
rh.base.start_time = (uint32_t)std::time(nullptr);
#ifdef YGOPRO_SERVER_MODE
if (pre_seed_specified[duel_count])
memcpy(rh.seed_sequence, pre_seed[duel_count], SEED_COUNT * sizeof(uint32_t));
else
#endif
for (auto& x : rh.seed_sequence)
x = rd();
mtrandom rnd(rh.seed_sequence, SEED_COUNT);
......
......@@ -520,6 +520,11 @@ void TagDuel::TPResult(DuelPlayer* dp, unsigned char tp) {
rh.base.version = PRO_VERSION;
rh.base.flag = REPLAY_UNIFORM | REPLAY_TAG;
rh.base.start_time = (uint32_t)std::time(nullptr);
#ifdef YGOPRO_SERVER_MODE
if (pre_seed_specified[0])
memcpy(rh.seed_sequence, pre_seed[0], SEED_COUNT * sizeof(uint32_t));
else
#endif
for (auto& x : rh.seed_sequence)
x = rd();
mtrandom rnd(rh.seed_sequence, SEED_COUNT);
......
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