-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
barak
committed
Aug 10, 2024
1 parent
6018c8c
commit a8a491d
Showing
31 changed files
with
604 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ dist | |
out | ||
.DS_Store | ||
*.log* | ||
map-* |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
singleQuote: true | ||
semi: false | ||
printWidth: 100 | ||
printWidth: 220 | ||
trailingComma: none |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import * as pimage from 'pureimage' | ||
import * as fs from 'fs' | ||
import { BrowserWindow, shell } from 'electron' | ||
import crypto from 'crypto' | ||
import { DownloadData } from '../common/download' | ||
import { MapData, maps, UrlResult, UrlUsageType } from '../common/maps/map.data' | ||
import { getBackendData } from './maps/map-backend' | ||
|
||
export const downloadOptions = { | ||
canceled: false | ||
} | ||
|
||
export const downloadMap = async (win: BrowserWindow, request: DownloadData) => { | ||
if (!request) { | ||
return | ||
} | ||
|
||
downloadOptions.canceled = false | ||
|
||
const maxX = request.endCol - request.startCol + 1 | ||
const maxY = request.endRow - request.startRow + 1 | ||
|
||
console.log(`Starting download of ${maxX * maxY} tiles`) | ||
|
||
const map = maps.find((it) => it.name === request.mapName)! | ||
|
||
const croppedWidth = maxX * 256 - request.startX - (256 - request.endX) | ||
const croppedHeight = maxY * 256 - request.startY - (256 - request.endY) | ||
const overallImg = pimage.make(croppedWidth, croppedHeight) | ||
const overallCtx = overallImg.getContext('2d') | ||
|
||
const backendData = getBackendData(map.name) | ||
|
||
for (let y = 0; y < maxY; y++) { | ||
for (let x = 0; x < maxX; x++) { | ||
if (downloadOptions.canceled) { | ||
console.log('Download canceled') | ||
return | ||
} | ||
|
||
const progress = (x + y * maxX) / (maxX * maxY) | ||
win.webContents.send('download-progress', progress) | ||
|
||
console.log(`Downloading images: ${(progress * 100).toFixed(2)}%, x=${x}/${maxX}, y=${y}/${maxY}`) | ||
|
||
const { url, unsupported } = await getTileUrl(map, request.zoomLevel, request.startRow + y, request.startCol + x, request.mapType) | ||
try { | ||
if (!unsupported) { | ||
const headers = map.getDownloaderHeaders ? map.getDownloaderHeaders() : {} | ||
|
||
const response = await fetch(url, headers) //, { responseType: "arraybuffer" }); | ||
const arrayBuffer = await response.arrayBuffer() | ||
const buffer = Buffer.from(arrayBuffer) | ||
const img1 = await backendData.decode(request.mapType, buffer) | ||
const ctx = img1.getContext('2d') | ||
const imageData = ctx.getImageData(0, 0, 256, 256) | ||
for (let j = 0; j < 256; j++) { | ||
const posY = j + y * 256 - request.startY | ||
if (posY >= 0) { | ||
for (let i = 0; i < 256; i++) { | ||
const posX = i + x * 256 - request.startX | ||
if (posX >= 0) { | ||
overallCtx.fillPixelWithColor(posX, posY, imageData.getPixelRGBA(i, j)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
} | ||
} | ||
|
||
console.log(`Downloading done`) | ||
|
||
const fileName = `map-${crypto.randomUUID()}.png` | ||
pimage | ||
.encodePNGToStream(overallImg, fs.createWriteStream(fileName)) | ||
.then(() => { | ||
win.webContents.send('download-done', true) | ||
shell.openPath(fileName) | ||
}) | ||
.catch((e) => { | ||
console.log('there was an error writing', e) | ||
win.webContents.send('download-done', false) | ||
}) | ||
} | ||
|
||
const getTileUrl = async (map: MapData, zoomLevel: number, row: number, col: number, mapType: string): Promise<UrlResult> => { | ||
return await map.urlProvider(UrlUsageType.DOWNLOAD, mapType, zoomLevel, row, col) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { App, BrowserWindow, ipcMain } from 'electron' | ||
import { downloadMap, downloadOptions } from './downloader' | ||
import { handleHttpRequest } from './http-request.service' | ||
import { setReferrer } from './main-store' | ||
|
||
export const eventRegistrar = { | ||
registerEvents: (win: BrowserWindow, app: App) => { | ||
ipcMain.on('download-map', async (_, arg) => { | ||
await downloadMap(win, arg) | ||
}) | ||
|
||
ipcMain.on('cancel-download', () => { | ||
downloadOptions.canceled = true | ||
}) | ||
|
||
ipcMain.on('get-app-version', () => { | ||
win.webContents.send('app-version', app.getVersion()) | ||
}) | ||
|
||
ipcMain.on('set-referer', (_, arg) => { | ||
setReferrer(arg) | ||
}) | ||
|
||
ipcMain.handle('http:request', handleHttpRequest) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { IpcMainInvokeEvent } from 'electron' | ||
import fetch from 'electron-fetch' | ||
|
||
export async function handleHttpRequest(_: IpcMainInvokeEvent, url: string): Promise<ArrayBuffer> { | ||
console.log('Getting', url) | ||
const response = await fetch(url, { | ||
headers: { | ||
referer: 'https://en.mapy.cz/' | ||
} | ||
}) | ||
return await response.arrayBuffer() | ||
} |
Oops, something went wrong.