2017-10-18 06:59:14 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
|
|
|
|
public static class BVRequestUtil
|
|
|
|
|
{
|
|
|
|
|
public static string GetSMBattleVideoURL(string code)
|
|
|
|
|
{
|
|
|
|
|
code = code.Replace("-", string.Empty);
|
|
|
|
|
Debug.Assert(code.Length == 16);
|
2018-08-07 23:32:31 +00:00
|
|
|
|
var video_id = StrToU64(code, out bool valid);
|
|
|
|
|
if (!valid)
|
PKHeX.Core Nullable cleanup (#2401)
* Handle some nullable cases
Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data)
Make some classes have explicit constructors instead of { } initialization
* Handle bits more obviously without null
* Make SaveFile.BAK explicitly readonly again
* merge constructor methods to have readonly fields
* Inline some properties
* More nullable handling
* Rearrange box actions
define straightforward classes to not have any null properties
* Make extrabyte reference array immutable
* Move tooltip creation to designer
* Rearrange some logic to reduce nesting
* Cache generated fonts
* Split mystery gift album purpose
* Handle more tooltips
* Disallow null setters
* Don't capture RNG object, only type enum
* Unify learnset objects
Now have readonly properties which are never null
don't new() empty learnsets (>800 Learnset objects no longer created,
total of 2400 objects since we also new() a move & level array)
optimize g1/2 reader for early abort case
* Access rewrite
Initialize blocks in a separate object, and get via that object
removes a couple hundred "might be null" warnings since blocks are now readonly getters
some block references have been relocated, but interfaces should expose all that's needed
put HoF6 controls in a groupbox, and disable
* Readonly personal data
* IVs non nullable for mystery gift
* Explicitly initialize forced encounter moves
* Make shadow objects readonly & non-null
Put murkrow fix in binary data resource, instead of on startup
* Assign dex form fetch on constructor
Fixes legality parsing edge cases
also handle cxd parse for valid; exit before exception is thrown in FrameGenerator
* Remove unnecessary null checks
* Keep empty value until init
SetPouch sets the value to an actual one during load, but whatever
* Readonly team lock data
* Readonly locks
Put locked encounters at bottom (favor unlocked)
* Mail readonly data / offset
Rearrange some call flow and pass defaults
Add fake classes for SaveDataEditor mocking
Always party size, no need to check twice in stat editor
use a fake save file as initial data for savedata editor, and for
gamedata (wow i found a usage)
constrain eventwork editor to struct variable types (uint, int, etc),
thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
|
|
|
|
return string.Empty;
|
2017-10-18 06:59:14 +00:00
|
|
|
|
return $"https://ctr-bnda-live.s3.amazonaws.com/10.CTR_BNDA_datastore/ds/1/data/{video_id:D11}-00001"; // Sun datastore
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-07 23:32:31 +00:00
|
|
|
|
public static ulong StrToU64(string input, out bool valid)
|
|
|
|
|
{
|
|
|
|
|
var chk = Pull(0, 4) >> 4; // first four chars are checksum bits
|
|
|
|
|
var result = Pull(4, input.Length); // next 12 chars are the 70 value bits
|
2019-03-16 19:01:21 +00:00
|
|
|
|
var actual = Checksums.CRC16_CCITT(BitConverter.GetBytes(result));
|
2018-08-07 23:32:31 +00:00
|
|
|
|
valid = chk == actual;
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
ulong Pull(int start, int count)
|
|
|
|
|
{
|
|
|
|
|
ulong val = 0;
|
|
|
|
|
for (int i = start; i < count; i++)
|
|
|
|
|
{
|
|
|
|
|
var c = input[i];
|
|
|
|
|
if (c == '-')
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
val <<= 5;
|
|
|
|
|
val |= Get5BitFromChar(c) & 0b11111;
|
|
|
|
|
}
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
|
2018-08-07 23:32:31 +00:00
|
|
|
|
public static string U64ToStr(ulong input, bool insertDash)
|
2017-10-18 06:59:14 +00:00
|
|
|
|
{
|
2019-03-16 19:01:21 +00:00
|
|
|
|
uint chk = Checksums.CRC16_CCITT(BitConverter.GetBytes(input));
|
2018-08-07 23:32:31 +00:00
|
|
|
|
var buff = new char[16];
|
|
|
|
|
int ctr = 15;
|
|
|
|
|
Push(input, 12); // store value bits
|
|
|
|
|
Push(chk << 4, 4); // store checksum bits
|
|
|
|
|
return !insertDash ? string.Concat(buff) : GetStringWithDashesEvery(buff, 4);
|
|
|
|
|
|
|
|
|
|
void Push(ulong v, int count)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
|
{
|
|
|
|
|
buff[ctr--] = Set5BitToChar((char)(v & 0b11111));
|
|
|
|
|
v >>= 5;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-18 06:59:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-07 23:32:31 +00:00
|
|
|
|
private static string GetStringWithDashesEvery(char[] buff, int spacer)
|
2017-10-18 06:59:14 +00:00
|
|
|
|
{
|
2018-08-07 23:32:31 +00:00
|
|
|
|
var buff2 = new char[buff.Length + ((buff.Length / spacer) - 1)];
|
|
|
|
|
for (int i = 0, ctr = 0; i < buff.Length; i++)
|
2017-10-18 06:59:14 +00:00
|
|
|
|
{
|
2018-08-07 23:32:31 +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
|
|
|
|
}
|
2018-08-07 23:32:31 +00:00
|
|
|
|
return string.Concat(buff2);
|
|
|
|
|
}
|
2017-10-18 06:59:14 +00:00
|
|
|
|
|
2018-08-07 23:32:31 +00:00
|
|
|
|
private static char Set5BitToChar(char c)
|
|
|
|
|
{
|
2019-10-08 01:40:09 +00:00
|
|
|
|
var shift = c > 9 ? '7' : '0';
|
|
|
|
|
c += shift;
|
2020-12-25 18:58:33 +00:00
|
|
|
|
return MapToChar(c);
|
2018-08-07 23:32:31 +00:00
|
|
|
|
}
|
2017-10-18 06:59:14 +00:00
|
|
|
|
|
2018-08-07 23:32:31 +00:00
|
|
|
|
private static uint Get5BitFromChar(char c)
|
|
|
|
|
{
|
2020-12-25 18:58:33 +00:00
|
|
|
|
c = MapFromChar(c);
|
2019-10-08 01:40:09 +00:00
|
|
|
|
var shift = c >= 'A' ? '7' : '0';
|
|
|
|
|
return (uint)(c - shift);
|
2017-10-18 06:59:14 +00:00
|
|
|
|
}
|
2020-12-25 18:58:33 +00:00
|
|
|
|
|
|
|
|
|
private static char MapToChar(char c) => c switch
|
|
|
|
|
{
|
|
|
|
|
'0' => 'W',
|
|
|
|
|
'1' => 'X',
|
|
|
|
|
'I' => 'Y',
|
|
|
|
|
'O' => 'Z',
|
2021-08-20 20:49:20 +00:00
|
|
|
|
_ => c,
|
2020-12-25 18:58:33 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private static char MapFromChar(char c) => c switch
|
|
|
|
|
{
|
|
|
|
|
'W' => '0',
|
|
|
|
|
'X' => '1',
|
|
|
|
|
'Y' => 'I',
|
|
|
|
|
'Z' => 'O',
|
2021-08-20 20:49:20 +00:00
|
|
|
|
_ => c,
|
2020-12-25 18:58:33 +00:00
|
|
|
|
};
|
2017-10-18 06:59:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|