Files
WT-Offline/apps/server/src/profile/profile-entitlements.ts
T
N0VAN0M4LY d20b7f4249 feat(saves): edit local save nicknames and premium days
Let the launcher update a save's account nickname and replace or clear its remaining premium time. Reuse registration validation and preserve the existing login and password hash.
2026-11-29 17:41:46 +08:00

65 lines
3.1 KiB
TypeScript

/**
* Entitlements and Premium Account in the profile (read by aces.exe
* 2.61.0.37 0x91daf0..0x91e474, has_entitlement 0x179ec70, entitlement_expires_in 0x179ed60):
*
* entitlementsInfo{
* PremiumAccount{
* count:i=1
* expiredTime:i64=<Windows FILETIME>
* }
* wto_pack_f-5c{
* count:i=1
* }
* }
*/
import { ensureBlock, findBlock, setParam, type BlkBlock } from '@wto/blk';
import { EMPTY_BLK, int64Field, intField } from './blk-fields.ts';
const ENTITLEMENTS_INFO = 'entitlementsInfo';
export const PREMIUM_ACCOUNT_ENTITLEMENT = 'PremiumAccount';
const TICKS_PER_SECOND = 10_000_000n;
const TICKS_PER_DAY = 86_400n * TICKS_PER_SECOND;
export function hasEntitlement(profile: BlkBlock, name: string, nowFiletime: bigint): boolean {
const entry = findBlock(findBlock(profile, ENTITLEMENTS_INFO) ?? EMPTY_BLK, name);
if (!entry) return false;
const count = intField(entry, 'count');
if (count <= 0) return false;
const expires = int64Field(entry, 'expiredTime');
return expires === 0n || expires > nowFiletime;
}
export function recordEntitlementPurchase(profile: BlkBlock, name: string): void {
const entry = ensureBlock(ensureBlock(profile, ENTITLEMENTS_INFO), name);
const previous = intField(entry, 'count');
setParam(entry, 'count', { type: 'int', value: previous + 1 });
}
export function extendPremiumAccount(profile: BlkBlock, days: number, nowFiletime: bigint): void {
if (!Number.isSafeInteger(days) || days <= 0) throw new RangeError(`invalid premium days: ${days}`);
const entry = ensureBlock(ensureBlock(profile, ENTITLEMENTS_INFO), PREMIUM_ACCOUNT_ENTITLEMENT);
const previousCount = intField(entry, 'count');
const previousExpiry = int64Field(entry, 'expiredTime');
const base = previousExpiry > nowFiletime ? previousExpiry : nowFiletime;
setParam(entry, 'count', { type: 'int', value: previousCount + 1 });
setParam(entry, 'expiredTime', { type: 'int64', value: base + BigInt(days) * TICKS_PER_DAY });
}
export function setPremiumDays(profile: BlkBlock, days: number, nowFiletime: bigint): void {
if (!Number.isSafeInteger(days) || days < 0) throw new RangeError('Premium days must be a whole non-negative number.');
const expires = nowFiletime + BigInt(days) * TICKS_PER_DAY;
if (expires >= (1n << 63n)) throw new RangeError('Premium expiration exceeds the saved format.');
const entry = ensureBlock(ensureBlock(profile, ENTITLEMENTS_INFO), PREMIUM_ACCOUNT_ENTITLEMENT);
setParam(entry, 'count', { type: 'int', value: days > 0 ? Math.max(1, intField(entry, 'count')) : 0 });
setParam(entry, 'expiredTime', { type: 'int64', value: expires });
}
/** Remaining whole days (rounded up) of Premium Account, or 0 when inactive. */
export function remainingPremiumDays(profile: BlkBlock, nowFiletime: bigint): number {
const entry = findBlock(findBlock(profile, ENTITLEMENTS_INFO) ?? EMPTY_BLK, PREMIUM_ACCOUNT_ENTITLEMENT);
if (!entry || intField(entry, 'count') <= 0) return 0;
const expires = int64Field(entry, 'expiredTime');
if (expires <= nowFiletime) return 0;
return Number((expires - nowFiletime + TICKS_PER_DAY - 1n) / TICKS_PER_DAY);
}