2023-01-22 04:02:33 +00:00
|
|
|
using System;
|
2017-10-18 06:59:14 +00:00
|
|
|
using System.Diagnostics;
|
2022-01-03 05:35:59 +00:00
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2017-10-18 06:59:14 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
public static class BVRequestUtil
|
2017-10-18 06:59:14 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
public static string GetSMBattleVideoURL(string code)
|
2017-10-18 06:59:14 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
code = code.Replace("-", string.Empty);
|
|
|
|
Debug.Assert(code.Length == 16);
|
|
|
|
var video_id = StrToU64(code, out bool valid);
|
|
|
|
if (!valid)
|
|
|
|
return string.Empty;
|
|
|
|
return $"https://ctr-bnda-live.s3.amazonaws.com/10.CTR_BNDA_datastore/ds/1/data/{video_id:D11}-00001"; // Sun datastore
|
|
|
|
}
|
2017-10-18 06:59:14 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public static ulong StrToU64(ReadOnlySpan<char> input, out bool valid)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
var chk = Pull(input[..4]) >> 4; // first four chars are checksum bits
|
|
|
|
var result = Pull(input[4..]); // next 12 chars are the 70 value bits
|
2022-01-03 05:35:59 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
Span<byte> temp = stackalloc byte[8];
|
|
|
|
WriteUInt64LittleEndian(temp, result);
|
|
|
|
var actual = Checksums.CRC16_CCITT(temp);
|
|
|
|
valid = chk == actual;
|
|
|
|
return result;
|
2018-08-07 23:32:31 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
static ulong Pull(ReadOnlySpan<char> input)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
ulong val = 0;
|
2023-01-22 04:02:33 +00:00
|
|
|
foreach (char c in input)
|
2018-08-07 23:32:31 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (c == '-')
|
|
|
|
continue;
|
2018-08-07 23:32:31 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
val <<= 5;
|
|
|
|
val |= Get5BitFromChar(c) & 0b11111;
|
2018-08-07 23:32:31 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return val;
|
2018-08-07 23:32:31 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static string U64ToStr(ulong input, bool insertDash)
|
|
|
|
{
|
|
|
|
Span<byte> temp = stackalloc byte[8];
|
|
|
|
WriteUInt64LittleEndian(temp, input);
|
|
|
|
uint chk = Checksums.CRC16_CCITT(temp);
|
2023-01-22 04:02:33 +00:00
|
|
|
Span<char> buff = stackalloc char[16];
|
2022-06-18 18:04:24 +00:00
|
|
|
int ctr = 15;
|
2023-01-22 04:02:33 +00:00
|
|
|
Push(buff, ref ctr, 12, input); // store value bits
|
|
|
|
Push(buff, ref ctr, 04, chk << 4); // store checksum bits
|
|
|
|
return !insertDash ? new string(buff) : GetStringWithDashesEvery(buff, 4);
|
2017-10-18 06:59:14 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
static void Push(Span<char> buff, ref int ctr, int bit5Chunks, ulong value)
|
2017-10-18 06:59:14 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
for (int i = 0; i < bit5Chunks; i++)
|
2017-10-18 06:59:14 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
buff[ctr--] = Set5BitToChar((char)(value & 0x1F));
|
|
|
|
value >>= 5;
|
2017-10-18 06:59:14 +00:00
|
|
|
}
|
2018-08-07 23:32:31 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2017-10-18 06:59:14 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
private static string GetStringWithDashesEvery(ReadOnlySpan<char> buff, int spacer)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
Span<char> buff2 = stackalloc char[buff.Length + ((buff.Length / spacer) - 1)];
|
2022-06-18 18:04:24 +00:00
|
|
|
for (int i = 0, ctr = 0; i < buff.Length; i++)
|
2018-08-07 23:32:31 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
buff2[ctr++] = buff[i];
|
|
|
|
if (i % spacer == 3 && ctr < buff2.Length)
|
|
|
|
buff2[ctr++] = '-'; // add dash between every chunk of size {spacer}
|
2017-10-18 06:59:14 +00:00
|
|
|
}
|
2023-01-22 04:02:33 +00:00
|
|
|
return new string(buff2);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2020-12-25 18:58:33 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static char Set5BitToChar(char c)
|
|
|
|
{
|
|
|
|
var shift = c > 9 ? '7' : '0';
|
|
|
|
c += shift;
|
|
|
|
return MapToChar(c);
|
|
|
|
}
|
2020-12-25 18:58:33 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static uint Get5BitFromChar(char c)
|
|
|
|
{
|
|
|
|
c = MapFromChar(c);
|
|
|
|
var shift = c >= 'A' ? '7' : '0';
|
|
|
|
return (uint)(c - shift);
|
2017-10-18 06:59:14 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
private static char MapToChar(char c) => c switch
|
|
|
|
{
|
|
|
|
'0' => 'W',
|
|
|
|
'1' => 'X',
|
|
|
|
'I' => 'Y',
|
|
|
|
'O' => 'Z',
|
|
|
|
_ => c,
|
|
|
|
};
|
|
|
|
|
|
|
|
private static char MapFromChar(char c) => c switch
|
|
|
|
{
|
|
|
|
'W' => '0',
|
|
|
|
'X' => '1',
|
|
|
|
'Y' => 'I',
|
|
|
|
'Z' => 'O',
|
|
|
|
_ => c,
|
|
|
|
};
|
2017-10-18 06:59:14 +00:00
|
|
|
}
|