2022-02-20 05:50:17 +08:00
import { log , LogStyle } from './logging' ;
2022-04-01 04:26:13 +08:00
import { RGB , TOOLS_DIR } from '../src/util' ;
2021-10-14 06:26:26 +08:00
2022-02-20 05:50:17 +08:00
import fs from 'fs' ;
import path from 'path' ;
import { PNG } from 'pngjs' ;
2022-02-20 08:42:30 +08:00
import prompt from 'prompt' ;
2022-02-20 05:50:17 +08:00
export const ASSERT = ( condition : boolean , onFailMessage : string ) = > {
if ( ! condition ) {
log ( LogStyle . Failure , onFailMessage ) ;
process . exit ( 0 ) ;
}
} ;
2021-10-14 06:26:26 +08:00
export function isDirSetup ( relativePath : string , jarAssetDir : string ) {
2022-04-01 04:26:13 +08:00
const dir = path . join ( TOOLS_DIR , relativePath ) ;
2021-10-14 06:26:26 +08:00
if ( fs . existsSync ( dir ) ) {
if ( fs . readdirSync ( dir ) . length > 0 ) {
return true ;
}
} else {
fs . mkdirSync ( dir ) ;
}
2022-01-16 06:39:52 +08:00
log ( LogStyle . Warning , ` Copy the contents of .minecraft/versions/<version>/<version>.jar/ ${ jarAssetDir } from a Minecraft game files into ${ relativePath } or fetch them automatically ` ) ;
2021-10-14 06:26:26 +08:00
return false ;
}
2021-10-22 22:52:03 +08:00
export function getAverageColour ( image : PNG ) {
2022-02-20 05:50:17 +08:00
let r = 0 ;
let g = 0 ;
let b = 0 ;
2021-10-14 06:26:26 +08:00
for ( let x = 0 ; x < image . width ; ++ x ) {
for ( let y = 0 ; y < image . height ; ++ y ) {
const index = 4 * ( image . width * y + x ) ;
2022-02-20 05:50:17 +08:00
const rgba = image . data . slice ( index , index + 4 ) ;
2021-10-14 06:26:26 +08:00
r += rgba [ 0 ] ;
g += rgba [ 1 ] ;
b += rgba [ 2 ] ;
}
}
const numPixels = image . width * image . height ;
2022-02-20 05:50:17 +08:00
return new RGB ( r / ( 255 * numPixels ) , g / ( 255 * numPixels ) , b / ( 255 * numPixels ) ) ;
}
2022-02-20 08:42:30 +08:00
export async function getPermission() {
2022-04-17 02:29:20 +08:00
const directory = getMinecraftDir ( ) ;
2022-02-20 08:42:30 +08:00
log ( LogStyle . Info , ` This script requires files inside of ${ directory } ` ) ;
const { permission } = await prompt . get ( {
properties : {
permission : {
pattern : /^[YyNn]$/ ,
description : 'Do you give permission to access these files? (Y/n)' ,
message : 'Response must be Y or N' ,
required : true ,
} ,
} ,
} ) ;
const responseYes = [ 'Y' , 'y' ] . includes ( permission as string ) ;
if ( ! responseYes ) {
process . exit ( 0 ) ;
}
}
2022-04-17 02:29:20 +08:00
export function getMinecraftDir ( ) : string {
switch ( process . platform ) {
case 'darwin' : // MacOS
return path . join ( process . env . HOME ! , './Library/Application Support/minecraft' ) ;
case 'win32' : // Windows
2022-04-17 03:03:23 +08:00
return path . join ( process . env . APPDATA ! , './.minecraft' ) ;
2022-04-17 02:29:20 +08:00
default :
return path . join ( require ( 'os' ) . homedir ( ) , '/.minecraft' ) ;
}
}