Skip to content

Commit

Permalink
refactor: model display & credential persist
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Apr 14, 2021
1 parent 03cf878 commit 30f84f0
Show file tree
Hide file tree
Showing 37 changed files with 1,211 additions and 1,110 deletions.
966 changes: 903 additions & 63 deletions package-lock.json

Large diffs are not rendered by default.

21 changes: 14 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,27 @@
"build:locale": "node scripts/build-locale.js",
"build:schema": "node scripts/build-schema.js",
"build:production": "cross-env BUILD_TARGET=production node scripts/build.js",
"build:production:full": "cross-env BUILD_TARGET=production FULL_RELEASE=true node scripts/build.js",
"build:debug": "cross-env BUILD_TARGET=debug node scripts/build.js",
"build:dir": "cross-env BUILD_TARGET=dir node scripts/build.js",
"build:electron": "cross-env BUILD_TARGET=dir ONLY=electron node scripts/build.js",
"build:main": "cross-env BUILD_TARGET=dir ONLY=main node scripts/build.js",
"build:renderer": "cross-env BUILD_TARGET=dir ONLY=renderer node scripts/build.js",
"dev": "cross-env NODE_ENV=development node scripts/dev.js",
"lint": "eslint --ext .ts,.vue src",
"lint:fix": "eslint --ext .ts,.vue --fix src",
"test": "jest"
},
"dependencies": {
"@azure/msal-node": "^1.0.0-beta.2",
"@azure/msal-node": "^1.0.1",
"@types/three": "^0.127.0",
"@vue/composition-api": "^1.0.0-rc.2",
"@xmcl/client": "2.0.6",
"@xmcl/core": "2.6.0",
"@xmcl/curseforge": "^0.1.2",
"@xmcl/forge-site-parser": "^2.0.7",
"@xmcl/forge-site-parser": "^2.0.8",
"@xmcl/gamesetting": "^2.0.2",
"@xmcl/installer": "^3.2.0",
"@xmcl/installer": "^3.2.1",
"@xmcl/mod-parser": "^3.1.0",
"@xmcl/model": "^1.0.16",
"@xmcl/model": "^1.0.17",
"@xmcl/nbt": "^2.0.3",
"@xmcl/resourcepack": "^1.1.0",
"@xmcl/server-info": "^2.0.3",
Expand All @@ -64,7 +67,7 @@
"node-powershell": "^4.0.0",
"node-watch": "^0.6.4",
"original-fs": "^1.1.0",
"three": "^0.110.0",
"three": "^0.127.0",
"uuid": "^7.0.3",
"vue": "^2.6.11",
"vue-i18n": "^7.8.1",
Expand All @@ -79,6 +82,7 @@
"atomically",
"semver",
"electron-updater",
"electron-devtools-installer",
"fs-extra",
"ajv",
"7zip-min",
Expand All @@ -87,6 +91,7 @@
"filenamify",
"iconv-lite",
"jschardet",
"keytar",
"node-powershell",
"@xmcl/client",
"@xmcl/core",
Expand Down Expand Up @@ -121,6 +126,7 @@
"@rollup/plugin-typescript": "^8.2.0",
"@types/7zip-min": "^1.1.0",
"@types/bytebuffer": "^5.0.40",
"@types/electron-devtools-installer": "^2.2.0",
"@types/fs-extra": "^8.1.0",
"@types/jest": "^24.9.1",
"@types/lodash.debounce": "^4.0.6",
Expand All @@ -141,6 +147,7 @@
"cross-env": "^5.2.1",
"electron": "^11.3.0",
"electron-builder": "^22.9.1",
"electron-devtools-installer": "^3.1.1",
"esbuild": "^0.9.2",
"eslint": "^7.13.0",
"eslint-config-standard": "^14.1.1",
Expand Down
6 changes: 2 additions & 4 deletions scripts/build.base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ const config = {
repo: "x-minecraft-launcher"
}],
files: [
// don't include node_modules as all js modules are bundled into production js by rollup
// unless you want to prevent some module to bundle up
// list them below
'**/*'
],
asarUnpack: [
"dist/static/Acrylic.cs",
"static/Acrylic.cs",
"node_modules/7zip-bin/**/*"
],
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/build.full.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const config = {
],
files: [
"node_modules/7zip-bin/**/*",
"!dist/electron/static/Acrylic.cs",
"!dist/static/Acrylic.cs",
"!node_modules/7zip-bin/linux/**",
"!node_modules/7zip-bin/win/**"
]
Expand Down Expand Up @@ -92,7 +92,7 @@ const config = {
],
files: [
"node_modules/7zip-bin/**/*",
"!dist/electron/static/Acrylic.cs",
"!dist/static/Acrylic.cs",
"!node_modules/7zip-bin/win/**",
"!node_modules/7zip-bin/mac/**"
]
Expand Down
42 changes: 26 additions & 16 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,35 @@ async function start() {
return require('./build.lite.config')
}
}
const onlyRenderer = process.env.ONLY === 'renderer'
const onlyMain = process.env.ONLY === 'main'
const onlyElectron = process.env.ONLY === 'electron'

const [mainConfig] = await loadRollupConfig()
if (!onlyRenderer && !onlyElectron) {
const [mainConfig] = await loadRollupConfig()

await remove(join(__dirname, '../dist'))
await remove(join(__dirname, '../dist'))

console.log(chalk.bold.underline('Build main process & preload'))
const startTime = Date.now()
await buildMain(mainConfig)
console.log(
`Build completed in ${((Date.now() - startTime) / 1000).toFixed(2)}s.\n`
)
await buildRenderer()

console.log()
if (process.env.BUILD_TARGET) {
const config = loadElectronBuilderConfig()
const dir = process.env.BUILD_TARGET === 'dir'
await generatePackageJson()
await buildElectron(config, dir)
console.log(chalk.bold.underline('Build main process & preload'))
const startTime = Date.now()
await buildMain(mainConfig)
console.log(
`Build completed in ${((Date.now() - startTime) / 1000).toFixed(2)}s.\n`
)
}

if (!onlyMain && !onlyElectron) {
await buildRenderer()
console.log()
}

if (!onlyMain && !onlyRenderer) {
if (process.env.BUILD_TARGET) {
const config = loadElectronBuilderConfig()
const dir = process.env.BUILD_TARGET === 'dir'
await generatePackageJson()
await buildElectron(config, dir)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions scripts/build.lite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const config = {
],
files: [
"node_modules/7zip-bin/**/*",
"!dist/electron/static/Acrylic.cs",
"!dist/static/Acrylic.cs",
"!node_modules/7zip-bin/linux/**",
"!node_modules/7zip-bin/win/**"
]
Expand Down Expand Up @@ -45,7 +45,7 @@ const config = {
],
files: [
"node_modules/7zip-bin/**/*",
"!dist/electron/static/Acrylic.cs",
"!dist/static/Acrylic.cs",
"!node_modules/7zip-bin/win/**",
"!node_modules/7zip-bin/mac/**"
]
Expand Down
5 changes: 3 additions & 2 deletions src/main/app/LauncherApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,10 @@ export abstract class LauncherApp extends EventEmitter {
}

protected async setup() {
await ensureDir(this.appDataPath)
try {
(this.gameDataPath as any) = await readFile(join(this.appDataPath, 'root')).then((b) => b.toString().trim())
await ensureDir(this.appDataPath)
const self = this as any
self.gameDataPath = await readFile(join(this.appDataPath, 'root')).then((b) => b.toString().trim())
} catch (e) {
if (e.code === 'ENOENT') {
// first launch
Expand Down
10 changes: 9 additions & 1 deletion src/main/electron/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default class Controller implements LauncherAppController {

async createMainWindow() {
const configPath = join(this.app.appDataPath, 'main-window-config.json')
this.app.log(`[Controller] Creating main window by config ${configPath}`)
const configData = await readJSON(configPath).catch(() => ({
width: -1,
height: -1,
Expand All @@ -82,6 +83,10 @@ export default class Controller implements LauncherAppController {

const sess = session.fromPartition('persist:main')

// sess.protocol.interceptHttpProtocol('https', (req, cb) => {
// eslint-disable-next-line standard/no-callback-literal
// cb({ headers: { 'Access-Control-Allow-Origin': '*' } })
// })
sess.protocol.registerFileProtocol('dataroot', (req, callback) => {
const pathname = decodeURIComponent(req.url.replace('dataroot:///', ''))
callback(join(this.app.appDataPath, pathname))
Expand All @@ -105,14 +110,15 @@ export default class Controller implements LauncherAppController {
vibrancy: 'sidebar', // or popover
icon: iconPath,
webPreferences: {
// webSecurity: !IS_DEV, // disable security for loading local image
webSecurity: !IS_DEV, // disable security for loading local image
nodeIntegration: IS_DEV, // enable node for webpack in dev
preload: indexPreload,
session: sess,
webviewTag: true,
},
})

this.app.log(`[Controller] Created main window by config ${configPath}`)
browser.on('ready-to-show', () => { this.app.log('Main Window is ready to show!') })
browser.on('close', () => { })

Expand All @@ -124,6 +130,8 @@ export default class Controller implements LauncherAppController {
browser.loadURL(mainWinUrl)
browser.show()

this.app.log(`[Controller] Load main window url ${mainWinUrl}`)

this.mainWin = browser
}

Expand Down
1 change: 1 addition & 0 deletions src/main/electron/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export function checkUpdateTask (this: ElectronLauncherApp): Task<_UpdateInfo> {
this.log('Update available and set status to pending')
this.storeManager.store.commit('updateStatus', 'pending')
})
console.log(`check update task`)
const info = await autoUpdater.checkForUpdates()

if (this.networkManager.isInGFW && !injectedUpdate) {
Expand Down
24 changes: 12 additions & 12 deletions src/main/entities/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ export interface XBoxGameProfileResponse {
}, {
'id': 'PublicGamerpic'
'value': string
} ]
}]
isSponsoredUser: boolean
} ]
}]
}

export interface MinecraftAuthResponse {
Expand All @@ -66,12 +66,12 @@ export interface MinecraftProfileResponse {
url: string
variant: 'CLASSIC' | string
alias: 'STEVE' | string
} ]
}]
capes: [{
id: string
state: 'ACTIVE' | string
url: string
} ]
}]
}
export interface MinecraftProfileErrorResponse {
path: '/minecraft/profile'
Expand All @@ -98,7 +98,7 @@ export interface MinecraftOwnershipResponse {
keyId: string
}

export async function getOAuthToken (request: Got, code: string) {
export async function getOAuthToken(request: Got, code: string) {
const oauthResponse: OAuthTokenResponse = await request.post('https://login.live.com/oauth20_token.srf', {
form: {
client_id: CLIENT_ID,
Expand All @@ -118,7 +118,7 @@ export async function getOAuthToken (request: Got, code: string) {
* authenticate with xbox live by ms oauth access token
* @param oauthAccessToken The oauth access token
*/
export async function authenticateXboxLive (request: Got, oauthAccessToken: string) {
export async function authenticateXboxLive(request: Got, oauthAccessToken: string) {
const xblResponse: XBoxResponse = await request.post('https://user.auth.xboxlive.com/user/authenticate', {
body: JSON.stringify({
Properties: {
Expand All @@ -142,7 +142,7 @@ export async function authenticateXboxLive (request: Got, oauthAccessToken: stri
* Authorize the xbox live. It will get the xsts token in response.
* @param xblResponseToken
*/
export async function authorizeXboxLive (request: Got, xblResponseToken: string, relyingParty: 'rp://api.minecraftservices.com/' | 'http://xboxlive.com' = 'rp://api.minecraftservices.com/') {
export async function authorizeXboxLive(request: Got, xblResponseToken: string, relyingParty: 'rp://api.minecraftservices.com/' | 'http://xboxlive.com' = 'rp://api.minecraftservices.com/') {
const xstsResponse: XBoxResponse = await request.post('https://xsts.auth.xboxlive.com/xsts/authorize', {
headers: {
'Content-Type': 'application/json',
Expand All @@ -160,7 +160,7 @@ export async function authorizeXboxLive (request: Got, xblResponseToken: string,
return xstsResponse
}

export async function aquireXBoxToken (request: Got, oauthAccessToken: string) {
export async function aquireXBoxToken(request: Got, oauthAccessToken: string) {
const req = request.extend({
headers: {
'Content-Type': 'application/json',
Expand All @@ -175,7 +175,7 @@ export async function aquireXBoxToken (request: Got, oauthAccessToken: string) {
return { xstsResponse: minecraftXstsResponse, xboxGameProfile }
}

export async function getXboxGameProfile (request: Got, xuid: string, uhs: string, xstsToken: string) {
export async function getXboxGameProfile(request: Got, xuid: string, uhs: string, xstsToken: string) {
const response: XBoxGameProfileResponse = await request.get(`https://profile.xboxlive.com/users/xuid(${xuid})/profile/settings`, {
searchParams: {
settings: ['PublicGamerpic', 'Gamertag'].join(','),
Expand All @@ -197,7 +197,7 @@ export async function getXboxGameProfile (request: Got, xuid: string, uhs: strin
* @param uhs uhs from {@link XBoxResponse}
* @param xstsToken
*/
export async function loginMinecraftWithXBox (request: Got, uhs: string, xstsToken: string) {
export async function loginMinecraftWithXBox(request: Got, uhs: string, xstsToken: string) {
const mcResponse: MinecraftAuthResponse = await request.post('https://api.minecraftservices.com/authentication/login_with_xbox', {
json: {
identityToken: `XBL3.0 x=${uhs};${xstsToken}`,
Expand All @@ -211,7 +211,7 @@ export async function loginMinecraftWithXBox (request: Got, uhs: string, xstsTok
* Return the owner ship list of the player with those token.
* @param accessToken The Minecraft access token
*/
export async function checkGameOwnership (request: Got, accessToken: string) {
export async function checkGameOwnership(request: Got, accessToken: string) {
const mcResponse: MinecraftOwnershipResponse = await request.get('https://api.minecraftservices.com/entitlements/mcstore', {
headers: {
Authorization: `Bearer ${accessToken}`,
Expand All @@ -225,7 +225,7 @@ export async function checkGameOwnership (request: Got, accessToken: string) {
* The new way to get game profile by the access token
* @param accessToken The minecraft access token
*/
export async function getGameProfile (request: Got, accessToken: string) {
export async function getGameProfile(request: Got, accessToken: string) {
const profileResponse: MinecraftProfileResponse | MinecraftProfileErrorResponse = await request.get('https://api.minecraftservices.com/minecraft/profile', {
headers: {
Authorization: `Bearer ${accessToken}`,
Expand Down
14 changes: 8 additions & 6 deletions src/main/index.dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { autoUpdater } from 'electron-updater';
import { existsSync } from 'fs-extra';
import { Socket } from 'net';
import { join } from 'path';
import install, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
import './main';

app.on('web-contents-created', (event, contents) => {
Expand All @@ -32,12 +33,13 @@ autoUpdater.setFeedURL({
});
autoUpdater.logger = null;

// Require `main` process to boot app

app.on('browser-window-created', (event, window) => {
// if (!window.webContents.isDevToolsOpened()) {
// window.webContents.openDevTools()
// }
app.whenReady().then(() => {
install(VUEJS_DEVTOOLS).then((v) => {
console.log(`Installed vue devtool ${v}`)
}, (e) => {
console.error(`Fail to install vue devtool`)
console.error(e)
})
})

const devServer = new Socket({}).connect(25555, '127.0.0.1')
Expand Down
9 changes: 9 additions & 0 deletions src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import { app } from 'electron'
import ElectronLauncherApp from './electron/ElectronLauncherApp'

new ElectronLauncherApp().start()

app.on('web-contents-created', (event, contents) => {
// const extensionDir = join(__dirname, '../extensions')
// if (existsSync(extensionDir)) {
// contents.session.loadExtension(extensionDir)
// }
contents.openDevTools({ mode: 'detach' })
})
Loading

0 comments on commit 30f84f0

Please sign in to comment.