Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Asset Path Conflict Check #345

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions script/package.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ param (
[switch]$Dev = $false
)

$name = node -pe 'p = require(\"./package.json\"); `${p[\"name\"]}`'
$version = node -pe 'p = require(\"./package.json\"); `${p[\"version\"]}`'
$name = node -pe 'p = require(''./package.json''); `${p[''name'']}`'
$version = node -pe 'p = require(''./package.json''); `${p[''version'']}`'
$hash = git log --oneline | Select-Object -first 1 | ForEach-Object { $_.split(' ')[0] }
$date = [datetime]::Now.ToUniversalTime().ToString("yyyyMMdd-hhmm")

Expand Down
8 changes: 6 additions & 2 deletions src/installers.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import {
fromNullable,
getOrElse as getOrElseO,
} from "fp-ts/lib/Option";
import { FeatureSet } from "./features";
import {
FeatureSet,
} from "./features";
import {
FileTree,
Path,
Expand All @@ -28,7 +30,9 @@ import {
VortexInstallResult,
VortexMod,
} from "./vortex-wrapper";
import { S } from "./util.functions";
import {
S,
} from "./util.functions";

export enum InstallerType {
// Meta-installer, won't be in the pipeline itself
Expand Down
85 changes: 85 additions & 0 deletions src/validation.assetconflicts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import util from "util";
import * as fs from "fs";
import * as fsAsync from "fs/promises";
import {
Buffer,
} from "buffer";
import {
flow,
pipe,
} from "fp-ts/lib/function";
import {
TaskEither,
tryCatch,
map as mapTE,
} from "fp-ts/lib/TaskEither";
import {
map,
} from "fp-ts/lib/ReadonlyArray";


// Original implementation courtesy of Seberoth
class ArchiveReader {
_read: any = util.promisify(fs.read);

ReadFileList = async (path: string): Promise<bigint[]> => {
const handle = await fsAsync.open(path, `r`);

const indexPosition = await this.ReadInt64(handle, 8);
const fileCount = await this.ReadInt32(handle, indexPosition + BigInt(16));

const result:bigint[] = [];
for (let i = 0; i < fileCount; i += 1) {
// can't validate right now, should be right
const position = indexPosition + (BigInt(28) + (BigInt(i) * BigInt(56)));
// eslint-disable-next-line no-await-in-loop
const hash = await this.ReadUInt64(handle, position);

result.push(hash);
}

handle.close();

return result;
};

ReadInt32 = async (handle: fsAsync.FileHandle, position: number | bigint): Promise<number> => {
const data = await this.ReadBytes(handle, position, 8);

return data.readInt32LE();
};

ReadInt64 = async (handle: fsAsync.FileHandle, position: number | bigint): Promise<bigint> => {
const data = await this.ReadBytes(handle, position, 8);

return data.readBigInt64LE();
};

ReadUInt64 = async (handle: fsAsync.FileHandle, position: number | bigint): Promise<bigint> => {
const data = await this.ReadBytes(handle, position, 8);

return data.readBigUInt64LE();
};

ReadBytes = async (handle: fsAsync.FileHandle, position: number | bigint, length: number): Promise<Buffer> => {
const buffer = Buffer.alloc(length);

// fs accepts bigint for position, fs/promises does not -.-
// eslint-disable-next-line no-underscore-dangle
const ret = await this._read(handle.fd, buffer, 0, length, position);

return ret.buffer;
};
}


export const extractAssetPathHashesUsedByArchive = (archivePath: string): TaskEither<Error, readonly string[]> =>
pipe(
tryCatch(
() => new ArchiveReader().ReadFileList(archivePath),
(err) => new Error(`Error extracting asset paths from ${archivePath}: ${err}`),
),
mapTE(flow(
map((hash) => hash.toString(16)),
)),
);
Binary file added test/fixtures/testarchive.archive
Binary file not shown.
57 changes: 57 additions & 0 deletions test/unit/validation.assetconflicts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
pipe,
} from 'fp-ts/lib/function';
import {
getOrElseW as getOrElseWTE,
} from 'fp-ts/lib/TaskEither';
import path from 'path';
import {
constant,
} from '../../src/util.functions';
import {
extractAssetPathHashesUsedByArchive,
} from '../../src/validation.assetconflicts';

const testArchivePath = path.join(__dirname, `..\\fixtures\\testarchive.archive`);

const expectedAssetHashes = [
`e6112aba4dd569b`,
`f3b68c22724d568`,
`1192cdb2838f1a0b`,
`1cd0c7e6f59069cb`,
`2ff8aa43d13f10dc`,
`36114a9d1c7529dc`,
`3818ace8d1d3d789`,
`434ce82d135a8d7b`,
`583ab8a5670995a2`,
`67c26ea21cb0f9b1`,
`6c07c44bff7cbdc6`,
`8e7afdc9957dce37`,
`a55bfda25d42e6f5`,
`af3f42cdbda5049d`,
`b084b893f5fd56aa`,
`b2248fd941ffc596`,
`ba73a5c2b596ca48`,
`bc0f21007664b1ef`,
`c0f646a7ef26da61`,
`d4290ccae1f5ff82`,
`e7ed42220fd78214`,
`f01b872e7d9a0c5c`,
`f14cd4f6eaea939f`,
`f6a502a9dac98a46`,
];

describe(`Archive asset paths`, () => {

describe(`extracting asset path hashes`, () => {

test(`Produces list of asset hashes defined in the .archive`, async () => {
const hashes = await pipe(
extractAssetPathHashesUsedByArchive(testArchivePath),
getOrElseWTE((error) => constant(Promise.reject(error))),
)();

expect(hashes).toEqual(expectedAssetHashes);
});
});
});