class Mmi::Modloader::Fabric

Attributes

install_dir[R]
install_type[R]
mcversion[R]
options[R]
version[R]

Public Class Methods

new(options) click to toggle source
# File lib/mmi/modloader/fabric.rb, line 11
def initialize(options)
        @options = options
        
        @version      = options['version'          ]
        @install_type = options['install_type'     ]
        @mcversion    = options['minecraft_version']
        @install_dir  = options['install_dir'      ] || Mmi.minecraft_dir
        
        if self.version
                if self.install_type
                        if ['client', 'server'].include?(self.install_type)
                                if self.mcversion
                                        # Pass.
                                else
                                        raise Mmi::MissingAttributeError, 'Missing "modloader.minecraft_version".'
                                end
                        else
                                raise Mmi::InvalidAttributeError, %Q{Invalid "modloader.install_type". Expecting "client" or "server", got #{self.install_type.inspect}.}
                        end
                else
                        raise Mmi::MissingAttributeError, 'Missing "modloader.install_type".'
                end
        else
                raise Mmi::MissingAttributeError, 'Missing "modloader.version".'
        end
end

Public Instance Methods

absolute_install_dir() click to toggle source
# File lib/mmi/modloader/fabric.rb, line 50
def absolute_install_dir
        File.expand_path(self.install_dir)
end
download_installer() click to toggle source
# File lib/mmi/modloader/fabric.rb, line 54
def download_installer
        Mmi.info "Downloading fabric-installer version #{self.version.inspect}."
        
        begin
                FileUtils.mkdir_p(Mmi.cache_dir)
                
                expected_hexdigest = URI.open(installer_sha512sum_uri).read
                
                if !File.exists?(installer_path) || expected_hexdigest != Digest::SHA512.hexdigest(File.read(installer_path))
                        stream = URI.open(installer_uri)
                        
                        IO.copy_stream(stream, installer_path)
                        
                        actual_hexdigest = Digest::SHA512.hexdigest(File.read(installer_path))
                        
                        if expected_hexdigest == actual_hexdigest
                                # Pass.
                        else
                                Mmi.fail! "Expected fabric installer to have SHA512 sum #{expected_hexdigest.inspect} but received #{actual_hexdigest.inspect}."
                        end
                else
                        Mmi.info 'Using cached fabric-installer.'
                end
        rescue OpenURI::HTTPError => e
                Mmi.fail! %Q{Error when requesting fabric installer. Maybe "modloader.version" == #{version.inspect} is invalid.\n#{e.inspect}}
        end
end
install() click to toggle source
# File lib/mmi/modloader/fabric.rb, line 92
def install
        download_installer
        run_installer
end
installer_path() click to toggle source
# File lib/mmi/modloader/fabric.rb, line 46
def installer_path
        File.join(Mmi.cache_dir, "fabric-installer-#{self.version}.jar")
end
installer_sha512sum_uri() click to toggle source
# File lib/mmi/modloader/fabric.rb, line 42
def installer_sha512sum_uri
        "#{installer_uri}.sha512"
end
installer_uri() click to toggle source
# File lib/mmi/modloader/fabric.rb, line 38
def installer_uri
        "https://maven.fabricmc.net/net/fabricmc/fabric-installer/#{self.version}/fabric-installer-#{self.version}.jar"
end
run_installer() click to toggle source
# File lib/mmi/modloader/fabric.rb, line 82
def run_installer
        FileUtils.mkdir_p(absolute_install_dir)
        
        if system('java', '-jar', installer_path, self.install_type, '-dir', absolute_install_dir, '-noprofile', '-mcversion', self.mcversion)
                # Pass.
        else
                Mmi.fail! 'Failed to install Fabric modloader.'
        end
end