From caa087624e6924de35d21dca2f8ba70a134e1776 Mon Sep 17 00:00:00 2001 From: Jim Kroon Date: Wed, 25 Sep 2024 22:44:32 +0200 Subject: [PATCH] Make platform Gamepass api typed --- app/main/main.ts | 117 +++++++++--------- packages/platform/src/controllers/gamepass.ts | 22 ++-- packages/platform/src/index.ts | 4 +- packages/storeapi/src/product.ts | 88 +++++++------ packages/xcloudapi/src/index.ts | 2 +- 5 files changed, 113 insertions(+), 120 deletions(-) diff --git a/app/main/main.ts b/app/main/main.ts index 0e2820a6..b013916e 100644 --- a/app/main/main.ts +++ b/app/main/main.ts @@ -4,79 +4,74 @@ import serve from 'electron-serve' import { createWindow } from './helpers' import Platform from '@greenlight/platform' import Logger from '@greenlight/logger' -// const Platform = require('@greenlight/platform') -const Logger2 = require('@greenlight/logger') import pkg from '../package.json' -console.log('Fucking logger kanker shit:', Logger, Logger2.default) +export default class Application { -// export default class Application { + public isProduction: boolean = process.env.NODE_ENV === 'production' -// public isProduction: boolean = process.env.NODE_ENV === 'production' + private _platform?:Platform + public logger:Logger = new Logger('GreenlightApp') -// private _platform:Platform -// public logger:Logger = new Logger('GreenlightApp') + constructor() { + this.logger.log('constructor() Application booting... Greenlight App version', pkg.version) -// constructor() { -// this.logger.log('constructor() Application booting... Greenlight App version', pkg.version) + if(this.isProduction === true){ + serve({ directory: 'app' }) + } else { + app.setPath('userData', `${app.getPath('userData')} (development)`) + } + } -// if(this.isProduction === true){ -// serve({ directory: 'app' }) -// } else { -// app.setPath('userData', `${app.getPath('userData')} (development)`) -// } -// } + async isReady() { + await app.whenReady() + } -// async isReady() { -// await app.whenReady() -// } + loadPlatform(){ + return new Promise((resolve, reject) => { + this._platform = new Platform() -// loadPlatform(){ -// return new Promise((resolve, reject) => { -// // const Platform = require('@greenlight/platform'); -// this._platform = new Platform() + this._platform.loadWorker('./app/worker.js').then((authenticated:boolean) => { + this.logger.log('loadPlatform() Platform loaded and authenticated') + resolve(authenticated) + }).catch((error:any) => { + this.logger.error('loadPlatform() Platform failed to load. Critical error: '+error) + reject(error) + }) + }) + } -// this._platform.loadWorker('./app/worker.js').then((authenticated:boolean) => { -// this.logger.log('loadPlatform() Platform loaded and authenticated') -// resolve(authenticated) -// }).catch((error:any) => { -// this.logger.error('loadPlatform() Platform failed to load. Critical error: '+error) -// reject(error) -// }) -// }) -// } - -// async spawnMainWindow() { -// const mainWindow = createWindow('main', { -// width: 1280, -// height: (this.isProduction) ? 800 : 1200, -// title: 'Greenlight', -// backgroundColor: 'rgb(26, 27, 30)', -// webPreferences: { -// preload: path.join(__dirname, 'preload.js'), -// }, -// }) + async spawnMainWindow() { + const mainWindow = createWindow('main', { + width: 1280, + height: (this.isProduction) ? 800 : 1200, + title: 'Greenlight', + backgroundColor: 'rgb(26, 27, 30)', + webPreferences: { + preload: path.join(__dirname, 'preload.js'), + }, + }) -// if (this.isProduction) { -// await mainWindow.loadURL('app://./boot') -// } else { -// const port = process.argv[2] -// await mainWindow.loadURL(`http://localhost:${port}/boot`) -// mainWindow.webContents.openDevTools({ -// mode: 'bottom' -// }) -// } -// this.logger.log('spawnMainWindow() Main application windows drawn') + if (this.isProduction) { + await mainWindow.loadURL('app://./boot') + } else { + const port = process.argv[2] + await mainWindow.loadURL(`http://localhost:${port}/boot`) + mainWindow.webContents.openDevTools({ + mode: 'bottom' + }) + } + this.logger.log('spawnMainWindow() Main application windows drawn') -// return mainWindow -// } -// } + return mainWindow + } +} -// const main = new Application() -// ;(async () => { -// await main.isReady() +const main = new Application() +;(async () => { + await main.isReady() -// await main.spawnMainWindow() -// await main.loadPlatform() -// })() \ No newline at end of file + await main.spawnMainWindow() + await main.loadPlatform() +})() \ No newline at end of file diff --git a/packages/platform/src/controllers/gamepass.ts b/packages/platform/src/controllers/gamepass.ts index 211cb3b2..0ad4f4ee 100644 --- a/packages/platform/src/controllers/gamepass.ts +++ b/packages/platform/src/controllers/gamepass.ts @@ -1,4 +1,6 @@ import GreenlightPlatform from '..' +import { GetTitlesResponse } from '@greenlight/xcloudapi/src/response' +import Product from '@greenlight/storeapi/src/product' export interface SiglResponse { properties: { @@ -19,15 +21,15 @@ export default class Gamepass { this._platform = platform } - async getTitles() { + async getTitles():Promise { return await this._platform.sendMessage({ controller: 'xCloudApi', action: 'getTitles' }) } - async getList(type:string) { - const titles = await (this._platform.sendMessage({ + async getList(type:string):Promise { + const sigl = await (this._platform.sendMessage({ controller: 'xCloudApi', action: 'getList', params: [ @@ -35,10 +37,10 @@ export default class Gamepass { ] }) as Promise) - return await this.getProductIds(titles.productIds) + return await this.getProductIds(sigl.productIds) } - async getSigl(siglId:string) { + async getSigl(siglId:string):Promise { return await this._platform.sendMessage({ controller: 'xCloudApi', action: 'getSigl', @@ -48,7 +50,7 @@ export default class Gamepass { }) } - async getProductIds(productIds:string[]) { + async getProductIds(productIds:string[]):Promise { const products = await this._platform.sendMessage({ controller: 'storeApi', action: 'getProductIds', @@ -66,21 +68,19 @@ export default class Gamepass { ] }) - const retProducts = await this._platform.sendMessage({ + return await this._platform.sendMessage({ controller: 'storeApi', action: 'getProductIds', params: [ productIds ] }) - - return await this.parseItems(retProducts) } else { - return await this.parseItems(products) + return products } } - async parseItems(items:any[]){ + async parseItems(items:any[]):Promise { const parsedItems = [] for(const item in items){ const parsedItem = await this._platform.sendMessage({ diff --git a/packages/platform/src/index.ts b/packages/platform/src/index.ts index d32338a4..1b0f6527 100644 --- a/packages/platform/src/index.ts +++ b/packages/platform/src/index.ts @@ -25,7 +25,7 @@ export default class GreenlightPlatform { this.logger.log('constructor() Creating new GreenlightPlatform instance') } - loadWorker(file = __filename) { + loadWorker(file = __filename):Promise { return new Promise((resolve, reject) => { if(! worker.isMainThread) throw new Error('Cannot load worker in worker thread, need to be loaded in the main thread.') @@ -39,7 +39,7 @@ export default class GreenlightPlatform { }) }) this._worker.on('error', (error:any) => { - console.log('Worker error:', error) + this.logger.error('Worker error:', error) reject(error) }) this._worker.on('exit', (exit:any) => { diff --git a/packages/storeapi/src/product.ts b/packages/storeapi/src/product.ts index 1042e026..7259cbd3 100644 --- a/packages/storeapi/src/product.ts +++ b/packages/storeapi/src/product.ts @@ -61,7 +61,7 @@ export default class Product { public name:string; public description:string; - public description_short:string; + public descriptionShort:string; public developer:string; public publisher:string; @@ -80,83 +80,81 @@ export default class Product { this.id = item.StoreId this.name = item.ProductTitle - const storeData = item - - this.description = storeData.ProductDescription - this.description_short = storeData.ProductDescriptionShort - this.developer = storeData.DeveloperName - this.publisher = storeData.PublisherName + this.description = item.ProductDescription + this.descriptionShort = item.ProductDescriptionShort + this.developer = item.DeveloperName + this.publisher = item.PublisherName this.images = { - tile: 'https:'+storeData.Image_Tile.URL, - poster: 'https:'+storeData.Image_Poster.URL, - hero: (storeData.Image_Hero !== null) ? 'https:'+storeData.Image_Hero.URL : undefined, - titledHero: (storeData.Image_TitledHero !== null) ? 'https:'+storeData.Image_TitledHero.URL : undefined + tile: 'https:'+item.Image_Tile?.URL, + poster: 'https:'+item.Image_Poster?.URL, + hero: (item.Image_Hero !== null) ? 'https:'+item.Image_Hero?.URL : undefined, + titledHero: (item.Image_TitledHero !== null) ? 'https:'+item.Image_TitledHero?.URL : undefined } - for(const screenshot in storeData.Screenshots){ + for(const screenshot in item.Screenshots){ this.screenshots.push({ - url: 'https:'+storeData.Screenshots[screenshot].URL, - caption: storeData.Screenshots[screenshot].Caption + url: 'https:'+item.Screenshots[screenshot].URL, + caption: item.Screenshots[screenshot].Caption }) } - for(const trailer in storeData.Trailers){ + for(const trailer in item.Trailers){ this.trailers.push({ - image: storeData.Trailers[trailer].PreviewImageURL, - url: storeData.Trailers[trailer].URL, - caption: storeData.Trailers[trailer].Caption + image: item.Trailers[trailer].PreviewImageURL, + url: item.Trailers[trailer].URL, + caption: item.Trailers[trailer].Caption }) } - for(const category in storeData.Categories){ + for(const category in item.Categories){ this.categories.push({ - name: storeData.Categories[category], - localizedName: storeData.LocalizedCategories[category] + name: item.Categories[category], + localizedName: item.LocalizedCategories[category] }) } - for(const capability in storeData.Attributes){ + for(const capability in item.Attributes){ this.capabilities.push({ - name: storeData.Attributes[capability].Name, - localizedName: storeData.Attributes[capability].LocalizedName + name: item.Attributes[capability].Name, + localizedName: item.Attributes[capability].LocalizedName }) } - for(const language in storeData.LanguageSupport){ + for(const language in item.LanguageSupport){ this.languageSupport[language] = { - audio: storeData.LanguageSupport[language].GamePlayAudioLanguageSupport == 1 ? true : false, - interface: storeData.LanguageSupport[language].InterfaceLanguageSupport == 1 ? true : false, - subtitles: storeData.LanguageSupport[language].SubtitlesLanguageSupport == 1 ? true : false + audio: item.LanguageSupport[language].GamePlayAudioLanguageSupport == 1 ? true : false, + interface: item.LanguageSupport[language].InterfaceLanguageSupport == 1 ? true : false, + subtitles: item.LanguageSupport[language].SubtitlesLanguageSupport == 1 ? true : false } } - this.defaultPrice = (storeData.AnonymousPrice.MSRP !== null) ? { - currency: storeData.AnonymousPrice.MSRP.CurrencyCode, - value: storeData.AnonymousPrice.MSRP.Value, - formatted: storeData.AnonymousPrice.MSRP.FormattedValue + this.defaultPrice = (item.AnonymousPrice.MSRP !== null) ? { + currency: item.AnonymousPrice.MSRP?.CurrencyCode, + value: item.AnonymousPrice.MSRP?.Value, + formatted: item.AnonymousPrice.MSRP?.FormattedValue } : { currency: 'USD', value: 0.00, formatted: '$0.00' } - this.price = (storeData.AnonymousPrice.SalePrice !== null) ? { - currency: storeData.AnonymousPrice.SalePrice.CurrencyCode, - value: storeData.AnonymousPrice.SalePrice.Value, - formatted: storeData.AnonymousPrice.SalePrice.FormattedValue + this.price = (item.AnonymousPrice.SalePrice !== null) ? { + currency: item.AnonymousPrice.SalePrice.CurrencyCode, + value: item.AnonymousPrice.SalePrice.Value, + formatted: item.AnonymousPrice.SalePrice.FormattedValue } : this.defaultPrice this.rating = { - name: storeData.ContentRating.LongName, - description: storeData.ContentRating.Description, - age: storeData.ContentRating.Age, - logo: storeData.ContentRating.LogoUrl, - system: storeData.ContentRating.RatingSystem, - systemUrl: storeData.ContentRating.RatingSystemUrl, - localizedDescriptors: storeData.ContentRating.LocalizedDescriptors, - localizedDisclaimers: storeData.ContentRating.LocalizedDisclaimers, - localizedInteractiveElements: storeData.ContentRating.LocalizedInteractiveElements + name: item.ContentRating.LongName, + description: item.ContentRating.Description, + age: item.ContentRating.Age, + logo: item.ContentRating.LogoUrl, + system: item.ContentRating.RatingSystem, + systemUrl: item.ContentRating.RatingSystemUrl, + localizedDescriptors: item.ContentRating.LocalizedDescriptors, + localizedDisclaimers: item.ContentRating.LocalizedDisclaimers, + localizedInteractiveElements: item.ContentRating.LocalizedInteractiveElements } } diff --git a/packages/xcloudapi/src/index.ts b/packages/xcloudapi/src/index.ts index 83a063ac..275e5baa 100644 --- a/packages/xcloudapi/src/index.ts +++ b/packages/xcloudapi/src/index.ts @@ -17,7 +17,7 @@ export interface xCloudApiConfig { language?:string } -interface SiglResponse { +export interface SiglResponse { properties: { siglId: string, title: string,