Repository navigation
Embed versioned app maps and install the compatible GPS/signal provider #14
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ac62ab7
feat: embed versioned maps and integrate compatible game provider
ATAC-Helicopter 198688b
fix: read map resources through bounded no-follow file handles
ATAC-Helicopter 108089e
test: stream full scene fixtures without browser-driver serialization
ATAC-Helicopter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| 'use strict'; | ||
| // Local game-derived packs stay in build/resources, outside source and public archives. | ||
| const fs=require('node:fs/promises'),path=require('node:path'),zlib=require('node:zlib'); | ||
| const {libraries,identify,digest,regular,readResource}=require('./maps.cjs'); | ||
| async function embed(target,home){ | ||
| const root=path.resolve(__dirname,'../..'),packs=[]; | ||
| for(const game of ['ats','ets2']){ | ||
| const source=path.join(root,'build',game+'-scene.json');let map;try{map=JSON.parse(await regular(source));}catch(error){if(error.code==='ENOENT')continue;throw error;} | ||
| if(map.game!==game||map.version!==2||!/^\d+\.\d+\.\d+\.\d+$/.test(map.gameVersion||''))throw Error('Map must contain its extracted gameVersion: '+source); | ||
| let directory;for(const library of await libraries(home)){const candidate=path.join(library,'steamapps','common',game==='ats'?'American Truck Simulator':'Euro Truck Simulator 2');try{await identify(candidate);directory=candidate;break;}catch{}} | ||
| if(!directory)throw Error('Cannot verify the game installation for '+game); | ||
| // Check extracted version against the current game's log before binding archive identity. | ||
| const id=game==='ats'?'270880':'227300';const steamapps=path.dirname(path.dirname(directory));const log=path.join(steamapps,'compatdata',id,'pfx','drive_c','users','steamuser','Documents',path.basename(directory),'game.log.txt'); | ||
| const versionResource=await readResource(path.join(directory,'version.scs'),1024*1024),logResource=await readResource(log,16*1024*1024);if(versionResource.stat.mtimeMs>logResource.stat.mtimeMs)throw Error('Run the updated game once before embedding its map'); | ||
| const text=logResource.data.toString('utf8');const match=text.match(/Loaded pack set version (\d+\.\d+\.\d+\.\d+)/);if(match?.[1]!==map.gameVersion)throw Error('Extracted map does not match the installed game log'); | ||
| const identity=await identify(directory),data=Buffer.from(JSON.stringify(map)),compressed=zlib.gzipSync(data,{level:9}),file=game+'-'+map.gameVersion+'.json.gz'; | ||
| await fs.mkdir(path.join(target,'maps'),{recursive:true});await fs.writeFile(path.join(target,'maps',file),compressed); | ||
| packs.push({game,gameVersion:map.gameVersion,file,bytes:data.length,sha256:digest(compressed),...identity}); | ||
| } | ||
| await fs.mkdir(path.join(target,'maps'),{recursive:true});await fs.writeFile(path.join(target,'maps','manifest.json'),JSON.stringify({format:1,packs},null,2));return packs; | ||
| } | ||
| module.exports={embed}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| 'use strict'; | ||
| // Trusted application resources only. Renderer input never supplies filesystem paths. | ||
| const fs=require('node:fs/promises'),path=require('node:path'),crypto=require('node:crypto'),zlib=require('node:zlib'); | ||
| const GAMES={ats:{id:'270880',directory:'American Truck Simulator'},ets2:{id:'227300',directory:'Euro Truck Simulator 2'}}; | ||
| const MAX=96*1024*1024; | ||
| const digest=data=>crypto.createHash('sha256').update(data).digest('hex'); | ||
| async function readResource(file,max=MAX){ | ||
| const handle=await fs.open(file,fs.constants.O_RDONLY|fs.constants.O_NOFOLLOW); | ||
| try{const stat=await handle.stat();if(!stat.isFile()||stat.size>max)throw Error('Invalid application resource');const data=Buffer.alloc(stat.size);let offset=0;while(offset<data.length){const {bytesRead}=await handle.read(data,offset,data.length-offset,offset);if(!bytesRead)throw Error('Resource changed during read');offset+=bytesRead;}const after=await handle.stat();if(after.size!==stat.size||after.mtimeMs!==stat.mtimeMs)throw Error('Resource changed during read');return {data,stat};}finally{await handle.close();} | ||
| } | ||
| async function regular(file,max=MAX){return (await readResource(file,max)).data;} | ||
| async function libraries(home){ | ||
| const roots=[path.join(home,'.steam','steam'),path.join(home,'.local','share','Steam'),path.join(home,'.var','app','com.valvesoftware.Steam','data','Steam')];const result=new Set(); | ||
| for(const root of roots){try{const vdf=await fs.readFile(path.join(root,'steamapps','libraryfolders.vdf'),'utf8');result.add(root);for(const m of vdf.matchAll(/"path"\s+"([^"\r\n]+)"/g))if(path.isAbsolute(m[1]))result.add(m[1].replace(/\\\\/g,'\\'));}catch{}} | ||
| return [...result]; | ||
| } | ||
| async function identify(directory){ | ||
| const versionSha256=digest(await regular(path.join(directory,'version.scs'),1024*1024)); | ||
| const archives=[];for(const name of (await fs.readdir(directory)).filter(n=>/^[a-z0-9_]+\.scs$/i.test(n)).sort()){const s=await fs.lstat(path.join(directory,name));if(!s.isFile())throw Error('Invalid game archive');archives.push([name,s.size]);} | ||
| return {versionSha256,archiveSignature:digest(JSON.stringify(archives))}; | ||
| } | ||
| function compatible(pack,identity){return pack.versionSha256===identity.versionSha256&&pack.archiveSignature===identity.archiveSignature;} | ||
| class EmbeddedMaps{ | ||
| constructor(resources,home){this.resources=resources;this.home=home;this.checked=new Map();} | ||
| async init(){try{this.manifest=JSON.parse(await regular(path.join(this.resources,'maps','manifest.json'),1024*1024));if(this.manifest.format!==1||!Array.isArray(this.manifest.packs))throw Error('Invalid map catalog');}catch{this.manifest={format:1,packs:[]};}this.roots=await libraries(this.home);} | ||
| async status(game='ats'){ | ||
| if(!GAMES[game])game='ats';const packs=this.manifest.packs.filter(p=>p.game===game);let installation; | ||
| for(const root of this.roots){const directory=path.join(root,'steamapps','common',GAMES[game].directory);try{const identity=await identify(directory);installation={directory,identity};break;}catch{}} | ||
| const pack=installation&&packs.find(p=>compatible(p,installation.identity)); | ||
| const state={game,ready:!!pack,version:pack?.gameVersion||null,reason:!installation?'game-not-installed':!packs.length?'map-not-bundled':!pack?'version-mismatch':'ready',provider:'waiting',message:!installation?'Steam game installation not found.':!packs.length?'This app has no embedded map for '+game.toUpperCase()+'.':!pack?'Game content changed. A matching app map pack is required; incompatible roads are disabled.':'Embedded '+game.toUpperCase()+' '+pack.gameVersion+' map · verified against installed game archives'}; | ||
| if(pack&&pack.gameVersion.startsWith('1.61.')){try{state.provider=await this.provision(installation.directory);}catch{state.provider='install-failed';}} | ||
| if(pack&&!pack.gameVersion.startsWith('1.61.'))state.provider='unsupported-version'; | ||
| return {state,pack}; | ||
| } | ||
| async provision(directory){ | ||
| const source=path.join(this.resources,'providers','ets2la_plugin.dll');const expected='0e1893719f84f28079857451b82a22a263401d98b8fc299cd561eb7695bfa88f'; | ||
| const data=await regular(source,1024*1024);if(digest(data)!==expected)throw Error('Invalid provider checksum'); | ||
| const plugins=path.join(directory,'bin','win_x64','plugins');await fs.mkdir(plugins,{recursive:true});if(!(await fs.lstat(plugins)).isDirectory())throw Error('Invalid plugins directory');const target=path.join(plugins,'ets2la_plugin.dll'); | ||
| try{if(digest(await regular(target,1024*1024))===expected){const status=this.checked.get(directory)||'installed';this.checked.set(directory,status);return status;}await fs.copyFile(target,target+'.backup-'+Date.now(),fs.constants.COPYFILE_EXCL);}catch(error){if(error.code!=='ENOENT')throw error;} | ||
| const temporary=path.join(plugins,'.haulsense-provider-'+crypto.randomBytes(8).toString('hex'));try{await fs.writeFile(temporary,data,{mode:0o644,flag:'wx'});await fs.rename(temporary,target);}finally{await fs.unlink(temporary).catch(()=>{});} | ||
| this.checked.set(directory,'restart-required');return 'restart-required'; | ||
| } | ||
| async read(game){const {state,pack}=await this.status(game);if(!state.ready)throw Error(state.message);if(!/^(ats|ets2)-[0-9.]+\.json\.gz$/.test(pack.file))throw Error('Invalid map pack filename');const bytes=await regular(path.join(this.resources,'maps',pack.file));if(digest(bytes)!==pack.sha256)throw Error('Map pack checksum mismatch');const data=zlib.gunzipSync(bytes,{maxOutputLength:MAX});if(data.length!==pack.bytes)throw Error('Map pack size mismatch');return data;} | ||
| } | ||
| module.exports={EmbeddedMaps,libraries,identify,compatible,digest,regular,readResource}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,19 @@ | ||
| 'use strict'; | ||
| const path=require('node:path'),fs=require('node:fs/promises'); | ||
| const path=require('node:path'),fs=require('node:fs/promises'),os=require('node:os'); | ||
| const localMaps=process.argv.includes('--local-maps'); | ||
| (async()=>{ | ||
| const provider=path.resolve(__dirname,'../../build/ets2la-provider/ets2la_plugin.dll'); | ||
| const expected='0e1893719f84f28079857451b82a22a263401d98b8fc299cd561eb7695bfa88f';let providerBytes; | ||
| try{providerBytes=await require('./maps.cjs').regular(provider,1024*1024);}catch(error){if(error.code!=='ENOENT')throw error;await require('node:util').promisify(require('node:child_process').execFile)('bash',[path.resolve(__dirname,'../scripts/download-signal-provider.sh')],{timeout:100000});providerBytes=await require('./maps.cjs').regular(provider,1024*1024);} | ||
| if(providerBytes.length>1024*1024||require('./maps.cjs').digest(providerBytes)!==expected)throw Error('Provider checksum mismatch'); | ||
| const {packager}=await import('@electron/packager'); | ||
| const output=path.resolve(__dirname,'../../build/desktop'); | ||
| const {flipFuses,FuseVersion,FuseV1Options}=await import('@electron/fuses'); | ||
| const paths=await packager({dir:__dirname,out:output,name:'HaulSense',executableName:'haulsense-app',platform:'linux',arch:'x64',electronVersion:require('./package.json').devDependencies.electron,overwrite:true,asar:true,prune:true,ignore:[/^\/test(?:\/|$)/,/^\/package\.cjs$/,/^\/node_modules$/]}); | ||
| for(const directory of paths){await flipFuses(path.join(directory,'haulsense-app'),{version:FuseVersion.V1,[FuseV1Options.RunAsNode]:false,[FuseV1Options.EnableNodeOptionsEnvironmentVariable]:false,[FuseV1Options.EnableNodeCliInspectArguments]:false,[FuseV1Options.OnlyLoadAppFromAsar]:true});const target=path.join(directory,'resources','daemon');await fs.mkdir(target,{recursive:true});await fs.copyFile(path.resolve(__dirname,'../../build/ats-dualsense/daemon/haulsense'),path.join(target,'haulsense'));await fs.chmod(path.join(target,'haulsense'),0o755);await fs.copyFile(path.resolve(__dirname,'../../LICENSE'),path.join(directory,'HAULSENSE-LICENSE'));} | ||
| const paths=await packager({dir:__dirname,out:output,name:'HaulSense',executableName:'haulsense-app',platform:'linux',arch:'x64',electronVersion:require('./package.json').devDependencies.electron,overwrite:true,asar:true,prune:true,ignore:[/^\/test(?:\/|$)/,/^\/(?:package|embed-maps)\.cjs$/,/^\/node_modules$/]}); | ||
| for(const directory of paths){await flipFuses(path.join(directory,'haulsense-app'),{version:FuseVersion.V1,[FuseV1Options.RunAsNode]:false,[FuseV1Options.EnableNodeOptionsEnvironmentVariable]:false,[FuseV1Options.EnableNodeCliInspectArguments]:false,[FuseV1Options.OnlyLoadAppFromAsar]:true});const target=path.join(directory,'resources','daemon');await fs.mkdir(target,{recursive:true});await fs.copyFile(path.resolve(__dirname,'../../build/ats-dualsense/daemon/haulsense'),path.join(target,'haulsense'));await fs.chmod(path.join(target,'haulsense'),0o755);await fs.copyFile(path.resolve(__dirname,'../../LICENSE'),path.join(directory,'HAULSENSE-LICENSE')); | ||
| if(localMaps)await require('./embed-maps.cjs').embed(path.join(directory,'resources'),os.homedir()); | ||
| const destination=path.join(directory,'resources','providers');await fs.mkdir(destination,{recursive:true});await fs.writeFile(path.join(destination,'ets2la_plugin.dll'),providerBytes);await fs.copyFile(path.resolve(__dirname,'../third_party/ets2la-provider/LICENSE'),path.join(destination,'LICENSE')); | ||
| } | ||
|
|
||
| console.log(paths.join('\n')); | ||
| })().catch(e=>{console.error(e.message);process.exitCode=1;}); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.