Skip to content

Commit

Permalink
Compile client with default read and write access
Browse files Browse the repository at this point in the history
This makes it more convenient to start the client without asking for
permissions to the Steam's common folder. Read-write access is only
applied for supported games. The permission prompt will only appear
if the game is not located where it is expected to be. This only works
for Windows since on Linux all games are located in the users home
directory.
  • Loading branch information
NeKzor committed Sep 15, 2023
1 parent eb9de08 commit af06adf
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 33 deletions.
54 changes: 38 additions & 16 deletions compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,40 @@
* SPDX-License-Identifier: MIT
*/

import { join } from 'https://deno.land/std@0.192.0/path/mod.ts';
import { Command } from 'https://deno.land/x/cliffy@v1.0.0-rc.2/command/mod.ts';
import { colors } from 'https://deno.land/x/cliffy@v1.0.0-rc.2/ansi/colors.ts';

const devHostname = 'autorender.portal2.local';

const supportedTargets: Record<string, { target: string; binaryName: string }> = {
type SupportedTarget = {
target: string;
binaryName: string;
steamappsDir?: string;
};

const supportedTargets: Record<string, SupportedTarget> = {
linux: {
target: 'x86_64-unknown-linux-gnu',
binaryName: 'autorenderclient',
},
windows: {
target: 'x86_64-pc-windows-msvc',
binaryName: 'autorenderclient.exe',
steamappsDir: 'C:\\\\Program Files (x86)\\\\Steam\\\\steamapps',
},
};

export const supportedGames: Record<string, { sourcemod: boolean }> = {
'Portal 2': { sourcemod: false },
'Aperture Tag': { sourcemod: false },
'Thinking with Time Machine': { sourcemod: false },
'Portal Stories Mel': { sourcemod: false },
// 'Portal 2 Community Edition': { sourcemod: false },
'Portal Reloaded': { sourcemod: false },
'Portal 2 Speedrun Mod': { sourcemod: true },
};

const getCompilationOptions = (os: string) => {
const target = supportedTargets[os];
if (!target) {
Expand All @@ -41,24 +59,28 @@ const main = async () => {
const systems = all ? ['linux', 'windows'] : [os ?? Deno.build.os];

for (const os of systems) {
const { target, binaryName } = getCompilationOptions(os);
const { target, binaryName, steamappsDir } = getCompilationOptions(os);
const developerFlags = release ? '' : `--unsafely-ignore-certificate-errors=${devHostname}`;

const command = new Deno.Command('deno', {
env: {
COMPILATION_TARGET: target,
COMPILATION_BINARY_NAME: binaryName,
COMPILATION_DEVELOPER_FLAGS: developerFlags,
},
args: [
'task',
'--cwd',
'src/client',
'compile',
],
});
const env = {
COMPILATION_TARGET: target,
COMPILATION_BINARY_NAME: binaryName,
COMPILATION_DEVELOPER_FLAGS: developerFlags,
COMPILATION_READ_WRITE_GAME_PATHS: steamappsDir
? ',' + Object.entries(supportedGames)
.map(([gameName, { sourcemod }]) => join(steamappsDir, sourcemod ? 'sourcemods' : 'common', gameName!))
.join(',')
: '',
};

const args = [
'task',
'--cwd',
'src/client',
'compile',
];

await command
await new Deno.Command('deno', { env, args })
.spawn()
.output();
}
Expand Down
2 changes: 1 addition & 1 deletion src/client/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"tasks": {
"dev": "deno run --no-prompt --allow-hrtime --allow-env --allow-read=\"autorender.yaml,autorender.cfg,log,worker.ts\" --allow-write=\"autorender.yaml,log\" --allow-run --allow-net=deno.land,autorender.portal2.local,steamusercontent-a.akamaihd.net,api.github.com,github.com,objects.githubusercontent.com --unsafely-ignore-certificate-errors=autorender.portal2.local --watch main.ts --dev",
"test": "deno test --allow-net --allow-read --allow-env",
"compile": "deno compile --output bin/$COMPILATION_BINARY_NAME --target $COMPILATION_TARGET --include ./worker.ts --include ./upload.ts --no-prompt --allow-hrtime --allow-env --allow-read=\"autorender.yaml,autorender.cfg,log,worker.ts\" --allow-write=\"autorender.yaml,log\" --allow-run --allow-net=deno.land,autorender.portal2.local,autorender.nekz.me,steamusercontent-a.akamaihd.net,api.github.com,github.com,objects.githubusercontent.com $COMPILATION_DEVELOPER_FLAGS main.ts"
"compile": "deno compile --output bin/$COMPILATION_BINARY_NAME --target $COMPILATION_TARGET --include ./worker.ts --include ./upload.ts --no-prompt --allow-hrtime --allow-env --allow-read=\"autorender.yaml,autorender.cfg,log,worker.ts$COMPILATION_READ_WRITE_GAME_PATHS\" --allow-write=\"autorender.yaml,log$COMPILATION_READ_WRITE_GAME_PATHS\" --allow-run --allow-net=deno.land,autorender.portal2.local,autorender.nekz.me,steamusercontent-a.akamaihd.net,api.github.com,github.com,objects.githubusercontent.com $COMPILATION_DEVELOPER_FLAGS main.ts"
},
"fmt": {
"useTabs": false,
Expand Down
37 changes: 21 additions & 16 deletions src/client/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,29 @@ export const createFolders = async (config: Config | null) => {
for (const game of config.games) {
const commonDir = dirname(game.dir);

const { state: readAccess } = await Deno.permissions.request({
name: 'read',
path: commonDir,
});

if (readAccess !== 'granted') {
logger.error(`Unable to get read access for path ${commonDir}`);
Deno.exit(1);
}
const gameDirReadAccess = await Deno.permissions.query({ name: 'read', path: game.dir });
const gameDirWiteAccess = await Deno.permissions.query({ name: 'write', path: game.dir });

if (gameDirReadAccess.state !== 'granted' || gameDirWiteAccess.state !== 'granted') {
const { state: readAccess } = await Deno.permissions.request({
name: 'read',
path: commonDir,
});

if (readAccess !== 'granted') {
logger.error(`Unable to get read access for path ${commonDir}`);
Deno.exit(1);
}

const { state: writeAccess } = await Deno.permissions.request({
name: 'write',
path: commonDir,
});
const { state: writeAccess } = await Deno.permissions.request({
name: 'write',
path: commonDir,
});

if (writeAccess !== 'granted') {
logger.error(`Unable to get write access for path ${commonDir}`);
Deno.exit(1);
if (writeAccess !== 'granted') {
logger.error(`Unable to get write access for path ${commonDir}`);
Deno.exit(1);
}
}

try {
Expand Down

0 comments on commit af06adf

Please sign in to comment.