Minecraft Wiki
k (v1.2.25)
kGeen bewerkingssamenvatting
Regel 76: Regel 76:
 
-- Check if variant also outputs a hash as 'json_package' (only works when force_download is NOT set)
 
-- Check if variant also outputs a hash as 'json_package' (only works when force_download is NOT set)
 
if variant == 'json' and not args.force_download then
 
if variant == 'json' and not args.force_download then
hash = version_data[version]['json_package']
+
variant = 'json_package'
if hash ~= nil then
+
if hash == nil then
variant = 'json_package'
 
else
 
 
variant = 'json'
 
variant = 'json'
hash = version_data[version]['json']
 
 
end
 
end
 
end
 
end

Versie van 20 nov 2018 14:41

Description

This module creates download links based on hashes stored in subpage /hashes. It looks up hashes there based on template calls.

It is highly recommended to also have some JavaScript which removes empty p elements. Those are apparantly also generated by the module, yet there doesn't seem to be any good reason to do so.

Individual elements

Table i18n

local i18n = {
    process_args = 'Module:ProcessArgs',
    hashes = 'Module:Downloadlinks/hashes',
    category = 'Categorie:Ongeldige downloadlink',

    version_error = '[[Sjabloon:Downloadlink|Geen of incorrecte versie gegeven]]',
    variant_error = '[[Sjabloon:Downloadlink|Geen of incorrecte variant gegeven]]',
    hash_error = '[[Sjabloon:Downloadlink|Geen hash gevonden]]',

    client = 'Client',
    server = 'Server',
    windows_server = 'Windowsserver',
    json = '.json',
    json_package = '.json',
    
    nowiki_template = 'nw',
}

In here:

  • process_args: Module where process arguments are located.
  • hashes: Module where hashes are stored.
  • category: (including namespace) Category where pages should be added to when no download link could be constructed.
  • version_error: What to show when no version could be found in the hash module.
  • variant_error: What to show when no variant could be found (e.g. client, server, etc.).
  • hash_error: What to show when no hash could be found.
  • client: Default text for a client download link.
  • server: Default text for a server download link.
  • windows_server: Default text for a Windows server download link.
  • json: Default text for a JSON download link.
  • json_package: Default text for JSON shown in-browser.
  • nowiki_template: Name of template where a self-closing nowiki element is located. This is necessary to prevent the module from generating illogical pres.

Getting and checking arguments from template call

Getting

Using

-- Checks before constructing
if args[1] then
	version = mw.text.trim(tostring(args[1])):lower()
elseif args.v then -- args.v is deprecated, yet still here for backwards compatibility
	version = mw.text.trim(tostring(args.v)):lower()
end
	
if args[2] then
	variant = mw.text.trim(tostring(args[2])):lower()
elseif args.s then -- args.s is deprecated, yet still here for backwards compatibility
	variant = mw.text.trim(tostring(args.s)):lower()
end

the module gets the mandatory arguments from the template call.

NOTICE: If a previous version of this module has not been used ever before, the deprecated argument checks may be safely removed.

Checking

After the module got the arguments, a further check is done to find out if all arguments are valid, that is, if the given mandatory parameters (version and variant) in the template are in the hashes module.

-- Check if version or variant outputs nil
if version_data[version] == nil then
    text = i18n['version_error']
    return text .. category
elseif version_data[version][variant] == nil then
    text = i18n['variant_error']
    return text .. category
end

If version or variant do not pass this test, the module stops, returns an error message and adds the page to the given category (category = if the page is in the main namespace).

If the hash is somehow nil (either set to that or unintended) in the hashes module, the module returns a 'download link' with text explaining no hash could be found (this might be redundant after the version and variant checks, but better safe than sorry).

Downloaded and in-browser JSON

By utilising

-- Start constructing
-- Check if variant also outputs a hash as 'json_package' (only works when force_download is NOT set)
if variant == 'json' and not args.force_download then
    hash = version_data[version]['json_package']
    if hash ~= nil then
    	variant = 'json_package'
    else
    	variant = 'json'
    	hash = version_data[version]['json']
    end
end

the module checks if there's also a json_package hash in the hashes module for the specified version. If there's such a hash found, it'll construct a link based on json_package rather than a download link. To prevent this from happening, set parameter {{{force_download}}}.

windows shorthand

If, rather than windows_server, windows is used for a Windows server download link, the module converts that to windows_server internally by utilising

-- Check if variant is 'windows' and then change it to 'windows_server'
if variant == 'windows' then
	variant = 'windows_server'
end

Custom link text

If default text is unwanted, the module can add custom text using

-- Check if text parameter exists, if not, use default
if args[3] then
	text = args[3]
elseif args.beschrijving then -- args.beschrijving is deprecated, yet still here for backwards compatibility
	text = args.beschrijving
else
	text = i18n[variant]
end

NOTICE: If a previous version of this module has not been used ever before, the deprecated argument check may be safely removed.

Other

When a link is returned by the module, it will be an external link, plus the nowiki template put behind it. This will prevent unwanted pres from happening.

[bekijken | bewerken | geschiedenis | cache legen]Bovenstaande documentatie is getranscludeerd uit Module:Downloadlinks/doc.

local p = {}

local i18n = {
    process_args = 'Module:ProcessArgs',
    hashes = 'Module:Downloadlinks/hashes',
    category = 'Categorie:Ongeldige downloadlink',

    version_error = '[[Sjabloon:Downloadlink|Geen of incorrecte versie gegeven]]',
    variant_error = '[[Sjabloon:Downloadlink|Geen of incorrecte variant gegeven]]',
    hash_error = '[[Sjabloon:Downloadlink|Geen hash gevonden]]',

    client = 'Client',
    server = 'Server',
    windows_server = '.exe',
    json = '.json',
    json_package = '.json',
    
    nowiki_template = 'nw',
}

function p.downloadlink_constructor(f)

    local args = f
    if f == mw.getCurrentFrame() then
        args = require(i18n['process_args']).merge(true)
    end
    
    -- Construct category
    local title = mw.title.getCurrentTitle()
    local category = ''
    
    if not args.nocat and title.namespace == 0 and not title.isSubpage then
    	category = '[[' .. i18n['category'] .. '|' .. title.text .. ']]'
    end

	local version
	local variant
	
	-- Checks before constructing
	if args[1] then
		version = mw.text.trim(tostring(args[1])):lower()
	elseif args.v then -- args.v is deprecated, yet still here for backwards compatibility
		version = mw.text.trim(tostring(args.v)):lower()
	end
	
	if args[2] then
		variant = mw.text.trim(tostring(args[2])):lower()
	elseif args.s then -- args.s is deprecated, yet still here for backwards compatibility
		variant = mw.text.trim(tostring(args.s)):lower()
	end
    
    -- change 'windows' to 'windows_server'
    if variant == 'windows' then
    	variant = 'windows_server'
    end
	
	local nw = f:expandTemplate{title = i18n['nowiki_template']}

    local version_data = mw.loadData(i18n['hashes'])
    
    -- Check if version or variant outputs nil
    if version_data[version] == nil then
    	text = i18n['version_error']
    	return text .. category
    elseif version_data[version][variant] == nil then
    	text = i18n['variant_error']
    	return text .. category
    end
    
    local hash = version_data[version][variant]
    local link
    local download_link
    local text

    -- Start constructing
    -- Check if variant also outputs a hash as 'json_package' (only works when force_download is NOT set)
    if variant == 'json' and not args.force_download then
    	variant = 'json_package'
    	if hash == nil then
    		variant = 'json'
    	end
	end
	
	-- Check if text parameter exists, if not, use default
	if args[3] then
		text = args[3]
	elseif args.beschrijving then -- args.beschrijving is deprecated, yet still here for backwards compatibility
		text = args.beschrijving
	else
		text = i18n[variant]
	end
    
    -- And now finally actually start contructing links
    -- Client
    if variant == 'client' then
        -- Check if hash exists
        if hash then
            download_link = 'https://launcher.mojang.com/v1/objects/' .. hash .. '/client.jar'
	        
            link = '[' .. download_link .. ' ' .. text .. ']' .. nw
    		return link
        else
            download_link = i18n['hash_error']
            return download_link .. category
        end
        
    -- Server
    elseif variant == 'server' then
        -- Check if hash exists
        if hash then
            download_link = 'https://launcher.mojang.com/v1/objects/' .. hash .. '/server.jar'
	        
            link = '[' .. download_link .. ' ' .. text .. ']' .. nw
    		return link
        else
            download_link = i18n['hash_error']
            return download_link .. category
        end

    -- Windows server
	elseif variant == 'windows_server' then
        -- Check if hash exists
        if hash then
            download_link = 'https://launcher.mojang.com/v1/objects/' .. hash .. '/windows_server.exe'
	        
            link = '[' .. download_link .. ' ' .. text .. ']' .. nw
    		return link
        else
            download_link = i18n['hash_error']
            return download_link .. category .. hash
        end
        
    -- JSON
    elseif variant == 'json' then
        -- Check if hash exists
        if hash then
            download_link = 'https://launchermeta.mojang.com/mc/game/' .. hash .. '/' .. version .. '.json'
	        
            link = '[' .. download_link .. ' ' .. text .. ']' .. nw
    		return link
        else
            download_link = i18n['hash_error']
            return download_link .. category
        end

    -- JSON in browser
    elseif variant == 'json_package' then
        -- Check if hash exists
        if hash ~= nil then
            download_link = 'https://launchermeta.mojang.com/v1/packages/' .. hash .. '/' .. version .. '.json'
	        
            link = '[' .. download_link .. ' ' .. text .. ']' .. nw
    		return link
        else
            download_link = i18n['hash_error']
            return download_link .. category
        end
    end
end

return p