Skip to content

Commit

Permalink
Merge pull request #1203 from hydralauncher/fix/replace-nbsp-with-space
Browse files Browse the repository at this point in the history
fix: replace nbsp with space
  • Loading branch information
zamitto authored Nov 5, 2024
2 parents 0745f5b + bdaf4d6 commit ab5024e
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 142 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
MAIN_VITE_API_URL: ${{ vars.MAIN_VITE_STAGING_API_URL }}
MAIN_VITE_AUTH_URL: ${{ vars.MAIN_VITE_STAGING_AUTH_URL }}
MAIN_VITE_CHECKOUT_URL: ${{ vars.MAIN_VITE_STAGING_CHECKOUT_URL }}
MAIN_VITE_ANALYTICS_API_URL: ${{ vars.MAIN_VITE_ANALYTICS_API_URL }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Build Windows
Expand All @@ -52,6 +53,7 @@ jobs:
MAIN_VITE_API_URL: ${{ vars.MAIN_VITE_STAGING_API_URL }}
MAIN_VITE_AUTH_URL: ${{ vars.MAIN_VITE_STAGING_AUTH_URL }}
MAIN_VITE_CHECKOUT_URL: ${{ vars.MAIN_VITE_STAGING_CHECKOUT_URL }}
MAIN_VITE_ANALYTICS_API_URL: ${{ vars.MAIN_VITE_ANALYTICS_API_URL }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Create artifact
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
MAIN_VITE_API_URL: ${{ vars.MAIN_VITE_API_URL }}
MAIN_VITE_AUTH_URL: ${{ vars.MAIN_VITE_AUTH_URL }}
MAIN_VITE_CHECKOUT_URL: ${{ vars.MAIN_VITE_CHECKOUT_URL }}
MAIN_VITE_ANALYTICS_API_URL: ${{ vars.MAIN_VITE_ANALYTICS_API_URL }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Build Windows
Expand All @@ -54,6 +55,7 @@ jobs:
MAIN_VITE_API_URL: ${{ vars.MAIN_VITE_API_URL }}
MAIN_VITE_AUTH_URL: ${{ vars.MAIN_VITE_AUTH_URL }}
MAIN_VITE_CHECKOUT_URL: ${{ vars.MAIN_VITE_CHECKOUT_URL }}
MAIN_VITE_ANALYTICS_API_URL: ${{ vars.MAIN_VITE_ANALYTICS_API_URL }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Create artifact
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
"dexie": "^4.0.9",
"electron-log": "^5.2.0",
"electron-updater": "^6.3.9",
"file-type": "^19.6.0",
"flexsearch": "^0.7.43",
"i18next": "^23.11.2",
"i18next-browser-languagedetector": "^7.2.1",
"icojs": "^0.19.4",
"jsdom": "^24.0.0",
"jsonwebtoken": "^9.0.2",
"knex": "^3.1.0",
Expand Down
8 changes: 7 additions & 1 deletion src/main/events/torrenting/start-game-download.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { registerEvent } from "../register-event";

import parseTorrent from "parse-torrent";
import type { StartGameDownloadPayload } from "@types";
import { DownloadManager, HydraApi, logger } from "@main/services";

Expand All @@ -9,6 +9,7 @@ import { createGame } from "@main/services/library-sync";
import { steamUrlBuilder } from "@shared";
import { dataSource } from "@main/data-source";
import { DownloadQueue, Game } from "@main/entity";
import { HydraAnalytics } from "@main/services/hydra-analytics";

const startGameDownload = async (
_event: Electron.IpcMainInvokeEvent,
Expand Down Expand Up @@ -90,6 +91,11 @@ const startGameDownload = async (
logger.error("Failed to create game download", err);
});

const { infoHash } = await parseTorrent(payload.uri);
if (infoHash) {
HydraAnalytics.postDownload(infoHash).catch(() => {});
}

await DownloadManager.cancelDownload(updatedGame!.id);
await DownloadManager.startDownload(updatedGame!);

Expand Down
34 changes: 34 additions & 0 deletions src/main/services/hydra-analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { userSubscriptionRepository } from "@main/repository";
import axios from "axios";
import { appVersion } from "@main/constants";

export class HydraAnalytics {
private static instance = axios.create({
baseURL: import.meta.env.MAIN_VITE_ANALYTICS_API_URL,
headers: { "User-Agent": `Hydra Launcher v${appVersion}` },
});

private static async hasActiveSubscription() {
const userSubscription = await userSubscriptionRepository.findOne({
where: { id: 1 },
});

return (
userSubscription?.expiresAt && userSubscription.expiresAt > new Date()
);
}

static async postDownload(hash: string) {
const hasSubscription = await this.hasActiveSubscription();

return this.instance
.post("/track", {
event: "download",
attributes: {
hash,
hasSubscription,
},
})
.then((response) => response.data);
}
}
70 changes: 24 additions & 46 deletions src/main/services/notifications/index.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
import { Notification, app, nativeImage } from "electron";
import { Notification, app } from "electron";
import { t } from "i18next";
import { parseICO } from "icojs";
import trayIcon from "@resources/tray-icon.png?asset";
import { Game } from "@main/entity";
import { gameRepository, userPreferencesRepository } from "@main/repository";
import { userPreferencesRepository } from "@main/repository";
import fs from "node:fs";
import axios from "axios";
import path from "node:path";
import sound from "sound-play";
import { achievementSoundPath } from "@main/constants";
import icon from "@resources/icon.png?asset";
import { NotificationOptions, toXmlString } from "./xml";
import { logger } from "../logger";

const getGameIconNativeImage = async (gameId: number) => {
try {
const game = await gameRepository.findOne({
where: {
id: gameId,
},
});
async function downloadImage(url: string | null) {
if (!url) return undefined;
if (!url.startsWith("http")) return undefined;

if (!game?.iconUrl) return undefined;
const fileName = url.split("/").pop()!;
const outputPath = path.join(app.getPath("temp"), fileName);
const writer = fs.createWriteStream(outputPath);

const images = await parseICO(
Buffer.from(game.iconUrl.split("base64,")[1], "base64")
);
const response = await axios.get(url, {
responseType: "stream",
});

const highResIcon = images.find((image) => image.width >= 128);
if (!highResIcon) return undefined;
response.data.pipe(writer);

return nativeImage.createFromBuffer(Buffer.from(highResIcon.buffer));
} catch (err) {
return undefined;
}
};
return new Promise<string | undefined>((resolve) => {
writer.on("finish", () => {
resolve(outputPath);
});
writer.on("error", () => {
logger.error("Failed to download image", { url });
resolve(undefined);
});
});
}

export const publishDownloadCompleteNotification = async (game: Game) => {
const userPreferences = await userPreferencesRepository.findOne({
where: { id: 1 },
});

const icon = await getGameIconNativeImage(game.id);

if (userPreferences?.downloadNotificationsEnabled) {
new Notification({
title: t("download_complete", {
Expand All @@ -51,7 +51,7 @@ export const publishDownloadCompleteNotification = async (game: Game) => {
ns: "notifications",
title: game.title,
}),
icon,
icon: await downloadImage(game.iconUrl),
}).show();
}
};
Expand All @@ -73,28 +73,6 @@ export const publishNotificationUpdateReadyToInstall = async (

export const publishNewFriendRequestNotification = async () => {};

async function downloadImage(url: string | null) {
if (!url) return null;
if (!url.startsWith("http")) return null;

const fileName = url.split("/").pop()!;
const outputPath = path.join(app.getPath("temp"), fileName);
const writer = fs.createWriteStream(outputPath);

const response = await axios.get(url, {
responseType: "stream",
});

response.data.pipe(writer);

return new Promise<string>((resolve, reject) => {
writer.on("finish", () => {
resolve(outputPath);
});
writer.on("error", reject);
});
}

export const publishCombinedNewAchievementNotification = async (
achievementCount,
gameCount
Expand Down
1 change: 1 addition & 0 deletions src/main/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
interface ImportMetaEnv {
readonly MAIN_VITE_STEAMGRIDDB_API_KEY: string;
readonly MAIN_VITE_API_URL: string;
readonly MAIN_VITE_ANALYTICS_API_URL: string;
readonly MAIN_VITE_AUTH_URL: string;
readonly MAIN_VITE_CHECKOUT_URL: string;
}
Expand Down
4 changes: 4 additions & 0 deletions src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export const removeDuplicateSpaces = (name: string) =>

export const replaceDotsWithSpace = (name: string) => name.replace(/\./g, " ");

export const replaceNbspWithSpace = (name: string) =>
name.replace(new RegExp(String.fromCharCode(160), "g"), " ");

export const replaceUnderscoreWithSpace = (name: string) =>
name.replace(/_/g, " ");

Expand All @@ -69,6 +72,7 @@ export const formatName = pipe<string>(
removeSpecialEditionFromName,
replaceUnderscoreWithSpace,
replaceDotsWithSpace,
replaceNbspWithSpace,
(str) => str.replace(/DIRECTOR'S CUT/g, ""),
removeSymbolsFromName,
removeDuplicateSpaces,
Expand Down
Loading

0 comments on commit ab5024e

Please sign in to comment.