Skip to content

Commit

Permalink
Improved types
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed Oct 13, 2023
1 parent 15fd988 commit 6e400e4
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 85 deletions.
14 changes: 11 additions & 3 deletions src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ export abstract class Bundle {
*/
public async createResourceFile(
destination: string,
data: Readonly<Buffer> | string,
data: Readonly<Uint8Array> | string,
options: Readonly<IBundleResourceOptions> | null = null
) {
this._assertIsOpen();
Expand Down Expand Up @@ -398,14 +398,22 @@ export abstract class Bundle {
*/
public async createResourceSymlink(
destination: string,
target: Readonly<Buffer> | string,
target: Readonly<Uint8Array> | string,
options: Readonly<IBundleResourceOptions> | null = null
) {
this._assertIsOpen();

const dest = await this._assertNotResourceExists(destination);
await mkdir(dirname(dest), {recursive: true});
await symlink(target, dest);
const t =
typeof target === 'string'
? target
: Buffer.from(
target.buffer,
target.byteOffset,
target.byteLength
);
await symlink(t, dest);

if (options) {
await this._setResourceAttributes(dest, options);
Expand Down
77 changes: 57 additions & 20 deletions src/projector/otto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,35 @@ export abstract class ProjectorOtto extends Projector {
*/
public shockwave = false;

/**
* Splash image data.
*/
public splashImageData:
| Readonly<Uint8Array>
| (() => Readonly<Uint8Array>)
| null = null;

/**
* Splash image file.
*/
public splashImageFile: string | null = null;

/**
* Splash image data.
* Lingo data.
*/
public splashImageData: Readonly<Buffer> | null = null;
public lingoData:
| Readonly<string[]>
| string
| Readonly<Uint8Array>
| (() => Readonly<string[]> | string | Readonly<Uint8Array>)
| (() => Promise<Readonly<string[]> | string | Readonly<Uint8Array>>)
| null = null;

/**
* Lingo file.
*/
public lingoFile: string | null = null;

/**
* Lingo data.
*/
public lingoData: Readonly<string[]> | string | Readonly<Buffer> | null =
null;

/**
* Xtras include map.
*/
Expand Down Expand Up @@ -241,10 +249,16 @@ export abstract class ProjectorOtto extends Projector {
*/
public async getSplashImageData() {
const {splashImageData, splashImageFile} = this;
return (
splashImageData ||
(splashImageFile ? readFile(splashImageFile) : null)
);
if (splashImageData) {
return typeof splashImageData === 'function'
? splashImageData()
: splashImageData;
}
if (splashImageFile) {
const d = await readFile(splashImageFile);
return new Uint8Array(d.buffer, d.byteOffset, d.byteLength);
}
return null;
}

/**
Expand All @@ -254,16 +268,39 @@ export abstract class ProjectorOtto extends Projector {
*/
public async getLingoData() {
const {lingoData, lingoFile} = this;
if (typeof lingoData === 'string') {
return Buffer.from(lingoData);
}
if (Array.isArray(lingoData)) {
return Buffer.from(lingoData.join(this.lingoNewline));
}
if (lingoData) {
return lingoData as Readonly<Buffer>;
switch (typeof lingoData) {
case 'function': {
const d = await lingoData();
if (typeof d === 'string') {
return new TextEncoder().encode(d);
}
if (Array.isArray(d)) {
return new TextEncoder().encode(
d.join(this.lingoNewline)
);
}
return d as Readonly<Uint8Array>;
}
case 'string': {
return new TextEncoder().encode(lingoData);
}
default: {
// Fall through.
}
}
if (Array.isArray(lingoData)) {
return new TextEncoder().encode(
lingoData.join(this.lingoNewline)
);
}
return lingoData as Readonly<Uint8Array>;
}
return lingoFile ? readFile(lingoFile) : null;
if (lingoFile) {
const d = await readFile(lingoFile);
return new Uint8Array(d.buffer, d.byteOffset, d.byteLength);
}
return null;
}

/**
Expand Down
83 changes: 65 additions & 18 deletions src/projector/otto/mac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,30 @@ export class ProjectorOttoMac extends ProjectorOtto {
*/
public intel = false;

/**
* Icon data.
*/
public iconData:
| Readonly<Uint8Array>
| (() => Readonly<Uint8Array>)
| (() => Promise<Readonly<Uint8Array>>)
| null = null;

/**
* Icon file.
*/
public iconFile: string | null = null;

/**
* Icon data.
* Info.plist data.
* Currently only supports XML plist.
*/
public iconData: Readonly<Buffer> | null = null;
public infoPlistData:
| string
| Readonly<Uint8Array>
| (() => string | Readonly<Uint8Array>)
| (() => Promise<string | Readonly<Uint8Array>>)
| null = null;

/**
* Info.plist file.
Expand All @@ -46,21 +61,20 @@ export class ProjectorOttoMac extends ProjectorOtto {
public infoPlistFile: string | null = null;

/**
* Info.plist data.
* Currently only supports XML plist.
* PkgInfo data.
*/
public infoPlistData: string | Readonly<Buffer> | null = null;
public pkgInfoData:
| string
| Readonly<Uint8Array>
| (() => Readonly<Uint8Array>)
| (() => Promise<Readonly<Uint8Array>>)
| null = null;

/**
* PkgInfo file.
*/
public pkgInfoFile: string | null = null;

/**
* PkgInfo data.
*/
public pkgInfoData: string | Readonly<Buffer> | null = null;

/**
* Update the bundle name in Info.plist.
* Possible values:
Expand Down Expand Up @@ -407,7 +421,14 @@ export class ProjectorOttoMac extends ProjectorOtto {
*/
public async getIconData() {
const {iconData, iconFile} = this;
return iconData || (iconFile ? readFile(iconFile) : null);
if (iconData) {
return typeof iconData === 'function' ? iconData() : iconData;
}
if (iconFile) {
const d = await readFile(iconFile);
return new Uint8Array(d.buffer, d.byteOffset, d.byteLength);
}
return null;
}

/**
Expand All @@ -417,11 +438,22 @@ export class ProjectorOttoMac extends ProjectorOtto {
*/
public async getInfoPlistData() {
const {infoPlistData, infoPlistFile} = this;
if (typeof infoPlistData === 'string') {
return infoPlistData;
}
if (infoPlistData) {
return infoPlistData.toString('utf8');
switch (typeof infoPlistData) {
case 'function': {
const d = await infoPlistData();
return typeof d === 'string'
? d
: new TextDecoder().decode(d);
}
case 'string': {
return infoPlistData;
}
default: {
// Fall through.
}
}
return new TextDecoder().decode(infoPlistData);
}
if (infoPlistFile) {
return readFile(infoPlistFile, 'utf8');
Expand All @@ -436,10 +468,25 @@ export class ProjectorOttoMac extends ProjectorOtto {
*/
public async getPkgInfoData() {
const {pkgInfoData, pkgInfoFile} = this;
if (typeof pkgInfoData === 'string') {
return Buffer.from(pkgInfoData, 'ascii');
if (pkgInfoData) {
switch (typeof pkgInfoData) {
case 'function': {
return pkgInfoData();
}
case 'string': {
return new TextEncoder().encode(pkgInfoData);
}
default: {
// Fall through.
}
}
return pkgInfoData;
}
if (pkgInfoFile) {
const d = await readFile(pkgInfoFile);
return new Uint8Array(d.buffer, d.byteOffset, d.byteLength);
}
return pkgInfoData || (pkgInfoFile ? readFile(pkgInfoFile) : null);
return null;
}

/**
Expand Down
15 changes: 13 additions & 2 deletions src/projector/otto/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ export class ProjectorOttoWindows extends ProjectorOtto {
/**
* Icon data.
*/
public iconData: Readonly<Buffer> | null = null;
public iconData:
| Readonly<Uint8Array>
| (() => Readonly<Uint8Array>)
| (() => Promise<Readonly<Uint8Array>>)
| null = null;

/**
* Version strings.
Expand Down Expand Up @@ -98,7 +102,14 @@ export class ProjectorOttoWindows extends ProjectorOtto {
*/
public async getIconData() {
const {iconData, iconFile} = this;
return iconData || (iconFile ? readFile(iconFile) : null);
if (iconData) {
return typeof iconData === 'function' ? iconData() : iconData;
}
if (iconFile) {
const d = await readFile(iconFile);
return new Uint8Array(d.buffer, d.byteOffset, d.byteLength);
}
return null;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@ export async function launcher(id: string) {
throw new Error(`Invalid launcher id: ${id}`);
}

return new Promise<Buffer>((resolve, reject) => {
return new Promise<Uint8Array>((resolve, reject) => {
inflateRaw(Buffer.from(b64, 'base64'), (err, data) => {
if (err) {
reject(err);
return;
}
resolve(data);
resolve(
new Uint8Array(data.buffer, data.byteOffset, data.byteLength)
);
});
});
}
2 changes: 1 addition & 1 deletion src/util/mac.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {fixtureFile} from '../util.spec';

import {machoAppLauncher, machoTypesData, machoTypesFile} from './mac';

function sha256(data: Buffer) {
function sha256(data: Uint8Array) {
return createHash('sha256').update(data).digest('hex');
}

Expand Down
Loading

0 comments on commit 6e400e4

Please sign in to comment.