Skip to content

Commit

Permalink
fix: Should load skin right after login
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Feb 24, 2024
1 parent f6df0ed commit 62f743f
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions xmcl-runtime/user/accountSystems/YggdrasilAccountSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@ export class YggdrasilAccountSystem implements UserAccountSystem {
return client
}

async #updateSkins(client: YggdrasilThirdPartyClient, userProfile: UserProfile, signal?: AbortSignal) {
for (const p of Object.values(userProfile.profiles)) {
const profile = await client.lookup(p.id, true, signal)
const texturesBase64 = profile.properties.textures
const textures = JSON.parse(Buffer.from(texturesBase64, 'base64').toString())
const skin = textures?.textures.SKIN
const uploadable = profile.properties.uploadableTextures

// mark skin already refreshed
if (skin) {
this.logger.log(`Update the skin for profile ${p.name}`)

userProfile.profiles[p.id] = {
...profile,
textures: {
...textures.textures,
SKIN: skin,
},
uploadable: uploadable ? uploadable.split(',') as any : undefined,
}
}
}
}

async login({ username, password, authority }: LoginOptions, signal?: AbortSignal): Promise<UserProfile> {
const client = this.getClient(authority)
if (!client) throw new UserException({ type: 'loginServiceNotSupported', authority }, `Service ${authority} is not supported`)
Expand All @@ -61,6 +85,8 @@ export class YggdrasilAccountSystem implements UserAccountSystem {
}
await this.storage.put(userProfile, auth.accessToken)

await this.#updateSkins(client, userProfile, signal)

return userProfile
} catch (e: any) {
if (e.message && e.message.startsWith('getaddrinfo ENOTFOUND')) {
Expand Down Expand Up @@ -123,27 +149,7 @@ export class YggdrasilAccountSystem implements UserAccountSystem {
userProfile.invalidated = false
}

for (const p of Object.values(userProfile.profiles)) {
const profile = await client.lookup(p.id, true, signal)
const texturesBase64 = profile.properties.textures
const textures = JSON.parse(Buffer.from(texturesBase64, 'base64').toString())
const skin = textures?.textures.SKIN
const uploadable = profile.properties.uploadableTextures

// mark skin already refreshed
if (skin) {
this.logger.log(`Update the skin for profile ${p.name}`)

userProfile.profiles[p.id] = {
...profile,
textures: {
...textures.textures,
SKIN: skin,
},
uploadable: uploadable ? uploadable.split(',') as any : undefined,
}
}
}
await this.#updateSkins(client, userProfile, signal)

return userProfile
}
Expand Down

0 comments on commit 62f743f

Please sign in to comment.