Skip to content
This repository has been archived by the owner on Mar 5, 2023. It is now read-only.

Commit

Permalink
feat: pseudo-support to RPC localization (support names localized but…
Browse files Browse the repository at this point in the history
… not messages)
  • Loading branch information
renanrcp committed Feb 19, 2023
1 parent b241efc commit 7880309
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@cliqz/adblocker-electron": "^1.25.2",
"cross-fetch": "^3.1.5",
"discord-rpc": "^4.0.1",
"electron-fetch": "^1.9.1",
"electron-is-dev": "^2.0.0",
"electron-log": "^5.0.0-beta.16",
"electron-prompt": "^1.7.0",
Expand Down
21 changes: 17 additions & 4 deletions src/discord/parsers/roomParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import { BrowserWindow } from "electron";
import { setLocationsInStore } from "../../discord";
import { Store } from "../../store";
import { CPLocation, CPLocationType } from "../../store/DiscordState";
import { ROOMS_JSONP_NAME } from "../constants";
import { getJsonFromParams } from "../requestHandler";
import { getRoomsJsonFromParams } from "../requestHandler";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const parseAndUpdateRooms = async (store: Store, mainWindow: BrowserWindow, params: any) => {
const json = await getJsonFromParams(mainWindow, params, ROOMS_JSONP_NAME);
const result = await getRoomsJsonFromParams(mainWindow, params);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const rooms = Object.values(json) as any[];
const rooms = Object.values(result.roomsJson) as any[];

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const localizedRooms = result.localizedJson ? Object.values(result.localizedJson) as any[] : [];

const cpLocations: CPLocation[] = [];

Expand Down Expand Up @@ -129,6 +131,17 @@ export const parseAndUpdateRooms = async (store: Store, mainWindow: BrowserWindo
match = 'igloo';
}

// Sets the localized name if has
if (localizedRooms) {
const localizedName = localizedRooms.filter(localizedRoom => {
return room.display_name === localizedRoom.display_name;
})[0].name;

if (localizedName) {
name = localizedName;
}
}

const cpLocation: CPLocation = {
id: id,
name: name,
Expand Down
51 changes: 45 additions & 6 deletions src/discord/requestHandler.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,67 @@
import { BrowserWindow } from "electron";
import { Store } from "../store";
import { ROOMS_PATH, SWF_MIME_FILE } from "./constants";
import { ROOMS_JSONP_NAME, ROOMS_PATH, SWF_MIME_FILE } from "./constants";
import { parseAndUpdateLocation } from "./parsers/locationParser";
import { parseAndUpdateRooms } from "./parsers/roomParser";
import fetch from 'electron-fetch';

const parseJSONP = (jsonp: string, name: string) => {
const nameLength = name.length;

return JSON.parse(jsonp.slice(nameLength + 1, jsonp.length - 2));
const json = jsonp.slice(nameLength + 1, jsonp.length - 2);

return JSON.parse(json);
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const getJsonFromParams = async (mainWindow: BrowserWindow, params: any, jsonpName: string) => {
const response = await mainWindow.webContents.debugger.sendCommand('Network.getResponseBody', { requestId: params.requestId });
type RoomsResponse = {
roomsJson: string,
localizedJson?: string,
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const getRoomsJsonFromParams = async (mainWindow: BrowserWindow, params: any): Promise<RoomsResponse> => {
let plainResponseBody;
let localizedResponseBody;

const url = params.response.url as string;

const response = await mainWindow.webContents.debugger.sendCommand('Network.getResponseBody', { requestId: params.requestId });

if (response.base64Encoded) {
plainResponseBody = Buffer.from(response.body, 'base64').toString('utf8');
} else {
plainResponseBody = response.body;
}

return parseJSONP(plainResponseBody, jsonpName);
if (!url.includes('/en/')) {
localizedResponseBody = plainResponseBody;

// https://media1.cpbrasil.pw/play/en/web_service/game_configs/rooms.jsonp?v=1.3.64&callback=cp_rooms
// https://play.newcp.net//pt/web_service/game_configs/rooms.jsonp?v=1.3.63&callback=cp_rooms

const webServiceIndex = url.indexOf('/web_service/');

const urlStart = url.substring(0, webServiceIndex);

const langIndex = urlStart.lastIndexOf('/');

const lang = urlStart.substring(langIndex + 1);

const enUrl = url.replace(lang, 'en');

const enResponse = await fetch(enUrl);

const enResponseBuffer = await enResponse.buffer();

plainResponseBody = enResponseBuffer.toString();


}

return {
roomsJson: parseJSONP(plainResponseBody, ROOMS_JSONP_NAME),
localizedJson: parseJSONP(localizedResponseBody, ROOMS_JSONP_NAME),
};
};

export const startRequestListener = (store: Store, mainWindow: BrowserWindow) => {
Expand Down
14 changes: 14 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,13 @@ electron-builder@^22.2.0:
update-notifier "^5.1.0"
yargs "^17.0.1"

electron-fetch@^1.9.1:
version "1.9.1"
resolved "https://registry.yarnpkg.com/electron-fetch/-/electron-fetch-1.9.1.tgz#e28bfe78d467de3f2dec884b1d72b8b05322f30f"
integrity sha512-M9qw6oUILGVrcENMSRRefE1MbHPIz0h79EKIeJWK9v563aT9Qkh8aEHPO1H5vi970wPirNY+jO9OpFoLiMsMGA==
dependencies:
encoding "^0.1.13"

electron-is-dev@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/electron-is-dev/-/electron-is-dev-2.0.0.tgz#833487a069b8dad21425c67a19847d9064ab19bd"
Expand Down Expand Up @@ -1315,6 +1322,13 @@ encodeurl@^1.0.2:
resolved "https://registry.yarnpkg.com/encodeurl/-/encodeurl-1.0.2.tgz#ad3ff4c86ec2d029322f5a02c3a9a606c95b3f59"
integrity sha512-TPJXq8JqFaVYm2CWmPvnP2Iyo4ZSM7/QKcSmuMLDObfpH5fi7RUGmd/rTDf+rut/saiDiQEeVTNgAmJEdAOx0w==

encoding@^0.1.13:
version "0.1.13"
resolved "https://registry.yarnpkg.com/encoding/-/encoding-0.1.13.tgz#56574afdd791f54a8e9b2785c0582a2d26210fa9"
integrity sha512-ETBauow1T35Y/WZMkio9jiM0Z5xjHHmJ4XmjZOq1l/dXz3lr2sRn87nJy20RupqSh1F2m3HHPSp8ShIPQJrJ3A==
dependencies:
iconv-lite "^0.6.2"

end-of-stream@^1.1.0:
version "1.4.4"
resolved "https://registry.yarnpkg.com/end-of-stream/-/end-of-stream-1.4.4.tgz#5ae64a5f45057baf3626ec14da0ca5e4b2431eb0"
Expand Down

0 comments on commit 7880309

Please sign in to comment.