Skip to content

Commit

Permalink
Variable name unification
Browse files Browse the repository at this point in the history
  • Loading branch information
Karthik99999 committed Feb 25, 2024
1 parent 291640d commit 4e5e8e5
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 44 deletions.
6 changes: 3 additions & 3 deletions src/lib/generators/rt/sos/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { deserialize } from './read';
import Data from '../data';

function shuffle(password: string[]): string[] {
const newPassword: string[] = [];
const shuffledIndexes = [
23, 16, 37, 45, 4, 41, 52, 1, 8, 39, 25, 36, 47, 0, 12, 3, 33, 20, 28, 9, 49, 53, 51, 31, 11, 2, 13, 14, 34, 5, 46,
27, 17, 18, 19, 29, 38, 48, 22, 32, 42, 15, 6, 26, 30, 10, 44, 50, 35, 7, 40, 21, 43, 24,
];
const shuffled = [];
for (let i = 0; i < shuffledIndexes.length; i++) {
newPassword[i] = password[shuffledIndexes[i]];
shuffled[i] = password[shuffledIndexes[i]];
}
return newPassword;
return shuffled;
}

interface PasswordData {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/generators/rt/sos/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { bitpack, BitstreamReader } from '../../bitstream';
import { sanitizePassword, charsToBits, checksum } from '../utils';

function unshuffle(password: string[]): string[] {
const newPassword: string[] = [];
const shuffledIndexes = [
23, 16, 37, 45, 4, 41, 52, 1, 8, 39, 25, 36, 47, 0, 12, 3, 33, 20, 28, 9, 49, 53, 51, 31, 11, 2, 13, 14, 34, 5, 46,
27, 17, 18, 19, 29, 38, 48, 22, 32, 42, 15, 6, 26, 30, 10, 44, 50, 35, 7, 40, 21, 43, 24,
];
const unshuffled = [];
for (let i = 0; i < shuffledIndexes.length; i++) {
newPassword[shuffledIndexes[i]] = password[i];
unshuffled[shuffledIndexes[i]] = password[i];
}
return newPassword;
return unshuffled;
}

interface PasswordData {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/generators/rt/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const characters = [
*/
export function sanitizePassword(password: string, length: number): string[] {
password = password.replace(/\s/g, '');
if (password.length !== length) throw new Error(`Password must be exatly ${length} characters long`);
if (password.length !== length) throw new Error(`Password must be exactly ${length} characters long`);
const sanitized: string[] = [];
for (let char of password) {
char = char.replace(/\./g, '…').replace(/m|#/g, '♂').replace(/f|%/g, '♀');
Expand Down
6 changes: 3 additions & 3 deletions src/lib/generators/rt/wm/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { bitsToChars, checksum } from '../utils';
import Data from '../data';

function shuffle(password: string[]): string[] {
const newPassword: string[] = [];
const shuffledIndexes = [12, 20, 9, 17, 4, 15, 1, 23, 3, 7, 19, 14, 0, 5, 21, 6, 8, 18, 11, 2, 10, 13, 22, 16];
const shuffled = [];
for (let i = 0; i < shuffledIndexes.length; i++) {
newPassword[i] = password[shuffledIndexes[i]];
shuffled[i] = password[shuffledIndexes[i]];
}
return newPassword;
return shuffled;
}

interface WonderMailData {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/generators/rt/wm/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { bitpack, BitstreamReader } from '../../bitstream';
import { sanitizePassword, charsToBits, checksum } from '../utils';

function unshuffle(password: string[]): string[] {
const newPassword: string[] = [];
const shuffledIndexes = [12, 20, 9, 17, 4, 15, 1, 23, 3, 7, 19, 14, 0, 5, 21, 6, 8, 18, 11, 2, 10, 13, 22, 16];
const unshuffled = [];
for (let i = 0; i < shuffledIndexes.length; i++) {
newPassword[shuffledIndexes[i]] = password[i];
unshuffled[shuffledIndexes[i]] = password[i];
}
return newPassword;
return unshuffled;
}

interface WonderMailData {
Expand Down
22 changes: 11 additions & 11 deletions src/lib/generators/rtdx/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import { deserialize } from './read';
import Data from './data';

function shuffle(code: string[]): string[] {
const unshuffledIndex = [
const shuffledIndexes = [
3, 27, 13, 21, 12, 9, 7, 4, 6, 17, 19, 16, 28, 29, 23, 20, 11, 0, 1, 22, 24, 14, 8, 2, 15, 25, 10, 5, 18, 26,
];
const shuffled: string[] = [];
const shuffled = [];
for (let i = 0; i < 30; i++) {
shuffled[unshuffledIndex[i]] = code[i];
shuffled[shuffledIndexes[i]] = code[i];
}
return shuffled;
}

function toSymbols(indexes: number[]): string[] {
return indexes.map((i) => symbols[i]);
function bitsToSymbols(bits: number[]): string[] {
return bits.map((i) => symbols[i]);
}

function encrypt(code: number[]): number[] {
Expand Down Expand Up @@ -72,13 +72,13 @@ function serialize(data: RescueData | RevivalData): string {
writer.write(data.revive, 30);
}

const indexes = writer.finish();
indexes.unshift(checksum(indexes));
const encrypted = encrypt(indexes);
const code = writer.finish();
code.unshift(checksum(code));
const encrypted = encrypt(code);
const bitstream = bitpack(encrypted, 8, 6);
const symbols = toSymbols(bitstream);
const code = shuffle(symbols);
return code.join('');
const bits = bitsToSymbols(bitstream);
const shuffled = shuffle(bits);
return shuffled.join('');
}

export function generateRescue(data: { team: string; dungeon: number; floor: number; pokemon: number }): string {
Expand Down
39 changes: 19 additions & 20 deletions src/lib/generators/rtdx/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@ import { bitpack, BitstreamReader } from '../bitstream';
import { checksum, crc32, RNG, symbols } from './utils';
import Data from './data';

function splitCode(code: string): string[] {
code = code.replace(/\s/g, '').toLowerCase();
let codeToSplit = '';
for (let i = 0; i < code.length; i += 2) {
codeToSplit += code.slice(i, i + 2) + ' ';
function sanitizePassword(password: string): string[] {
const split = password.replace(/\s/g, '').match(/.{2}/g);
if (split?.length !== 30) throw new Error('Password must be exactly 30 symbols long');
for (let symbol of split) {
if (!symbols.includes(symbol)) {
throw new Error(`Invalid symbol: ${symbol}`);
}
}
return codeToSplit.trim().split(' ');
return split;
}

function unshuffle(code: string[]): string[] {
const unshuffledIndex = [
const shuffledIndexes = [
3, 27, 13, 21, 12, 9, 7, 4, 6, 17, 19, 16, 28, 29, 23, 20, 11, 0, 1, 22, 24, 14, 8, 2, 15, 25, 10, 5, 18, 26,
];
const unshuffled: string[] = [];
const unshuffled = [];
for (let i = 0; i < 30; i++) {
unshuffled[i] = code[unshuffledIndex[i]];
unshuffled[i] = code[shuffledIndexes[i]];
}
return unshuffled;
}

function toIndexes(code: string[]): number[] {
return code.map((c) => symbols.indexOf(c));
function symbolsToBits(password: string[]): number[] {
return password.map((symbol) => symbols.indexOf(symbol));
}

function decrypt(code: number[]): number[] {
Expand Down Expand Up @@ -59,10 +61,10 @@ type RescueData = PasswordData & { type: 0 };
type RevivalData = Omit<PasswordData, 'dungeon' | 'floor' | 'pokemon' | 'gender' | 'reward' | 'unknown2'> & { type: 1 };

export function deserialize(password: string): RescueData | RevivalData {
const passwordArr = splitCode(password);
const unshuffled = unshuffle(passwordArr);
const indexes = toIndexes(unshuffled);
const bitpacked = bitpack(indexes, 6, 8);
const sanitized = sanitizePassword(password);
const unshuffled = unshuffle(sanitized);
const bits = symbolsToBits(unshuffled);
const bitpacked = bitpack(bits, 6, 8);
const code = decrypt(bitpacked);

const inclChecksum = code[0];
Expand All @@ -84,11 +86,8 @@ export function deserialize(password: string): RescueData | RevivalData {
const reward = reader.read(2);
const unknown2 = reader.read(1);

const indexes = toIndexes(passwordArr);
let charcode = '';
for (const i of indexes) {
charcode += Data.charmap[i];
}
const bits = symbolsToBits(sanitized);
const charcode = bits.map((b) => Data.charmap[b]).join('');
const revive = crc32(charcode) & 0x3fffffff;

const data: RescueData = {
Expand Down

0 comments on commit 4e5e8e5

Please sign in to comment.