MojangUtils.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { Rule, Natives } from '../asset/model/mojang/VersionJson'
  2. export function getMojangOS(): string {
  3. const opSys = process.platform
  4. switch(opSys) {
  5. case 'darwin':
  6. return 'osx'
  7. case 'win32':
  8. return 'windows'
  9. case 'linux':
  10. return 'linux'
  11. default:
  12. return opSys
  13. }
  14. }
  15. export function validateLibraryRules(rules?: Rule[]): boolean {
  16. if(rules == null) {
  17. return false
  18. }
  19. for(const rule of rules){
  20. if(rule.action != null && rule.os != null){
  21. const osName = rule.os.name
  22. const osMoj = getMojangOS()
  23. if(rule.action === 'allow'){
  24. return osName === osMoj
  25. } else if(rule.action === 'disallow'){
  26. return osName !== osMoj
  27. }
  28. }
  29. }
  30. return true
  31. }
  32. export function validateLibraryNatives(natives?: Natives): boolean {
  33. return natives == null ? true : Object.hasOwnProperty.call(natives, getMojangOS())
  34. }
  35. export function isLibraryCompatible(rules?: Rule[], natives?: Natives): boolean {
  36. return rules == null ? validateLibraryNatives(natives) : validateLibraryRules(rules)
  37. }
  38. /**
  39. * Returns true if the actual version is greater than
  40. * or equal to the desired version.
  41. *
  42. * @param {string} desired The desired version.
  43. * @param {string} actual The actual version.
  44. */
  45. export function mcVersionAtLeast(desired: string, actual: string): boolean {
  46. const des = desired.split('.')
  47. const act = actual.split('.')
  48. for(let i=0; i<des.length; i++){
  49. if(!(parseInt(act[i]) >= parseInt(des[i]))){
  50. return false
  51. }
  52. }
  53. return true
  54. }