Terraria Wiki
Terraria Wiki
Aucun résumé des modifications
Aucun résumé des modifications
 
Ligne 79 : Ligne 79 :
   
 
---Override the exclusivity information in `info`
 
---Override the exclusivity information in `info`
---with the content of `dcom3j`.
+
---with the content of `jdcom3st`.
 
---@param info number
 
---@param info number
---@param dcom3j string Expected format: `<d>:<c>:<o>:<m>:<3>:<j>`, with each platform either a Boolean string ("y", "0", etc.) or an empty string
+
---@param jdcom3st string Expected format: `<j>:<d>:<c>:<o>:<m>:<3>:<s>:<t>`, with each platform either a Boolean string ("y", "0", etc.) or an empty string
 
---@return number
 
---@return number
local function override(info, dcom3j)
+
local function override(info, jdcom3st)
for k, v in pairs(explode(':', dcom3j)) do
+
for k, v in pairs(explode(':', jdcom3st)) do
 
if v ~= '' then
 
if v ~= '' then
 
if v == '1' or v == 'y' or v == 'yes' then
 
if v == '1' or v == 'y' or v == 'yes' then
Ligne 97 : Ligne 97 :
 
-- Example to demonstrate the behavior of this function:
 
-- Example to demonstrate the behavior of this function:
 
-- Goal: Change "dcom" to "dco3".
 
-- Goal: Change "dcom" to "dco3".
-- info = 15 ("dcom"), dcom3j = ":::no:yes:"
+
-- info = 30 ("dcom"), jdcom3st = "::::no:yes::"
-- decimal 15 = binary 001111
+
-- decimal 30 = binary 00011110
-- The first "no" is at index 3 in the dcom3j string, so
+
-- The first "no" is at index 4 in the jdcom3st string, so
-- replace the corresponding bit with a 0: 001111 -> 000111.
+
-- replace the corresponding bit with a 0: 00011110 -> 00001110.
-- The "yes" is at index 4, so replace the bit at index 4
+
-- The "yes" is at index 5, so replace the bit at index 5
-- with a 1: 000111 -> 010111.
+
-- with a 1: 00001110 -> 00101110.
-- The result is info = 23 ("dco3").
+
-- The result is info = 46 ("dco3").
 
end
 
end
   
Ligne 110 : Ligne 110 :
 
---@param invert boolean Whether to invert the exclusivity info.
 
---@param invert boolean Whether to invert the exclusivity info.
 
---@param pagenot string The entity whose exclusivity info to subtract from the main one's.
 
---@param pagenot string The entity whose exclusivity info to subtract from the main one's.
---@param dcom3j string Manual exclusivity info to override the fetched one with.
+
---@param jdcom3st string Manual exclusivity info to override the fetched one with.
 
---@return number info An integer that holds the exclusivity information.
 
---@return number info An integer that holds the exclusivity information.
local function getInfo(page, invert, pagenot, dcom3j)
+
local function getInfo(page, invert, pagenot, jdcom3st)
 
local info = 0
 
local info = 0
   
 
-- A piece of exclusivity information is a set of Boolean values, one
 
-- A piece of exclusivity information is a set of Boolean values, one
 
-- for each platform. This is represented as bits of the `info` integer.
 
-- for each platform. This is represented as bits of the `info` integer.
-- Each platform (Desktop, Console, Old-gen console, Mobile, 3DS,
+
-- Each platform (Japanese console, Desktop, Console, Old-gen console, Mobile, 3DS,
-- and Japanese console – "dcom3j") is assigned one bit, in this order.
+
-- Switch, and TModLoader – "jdcom3st") is assigned one bit, in this order.
-- This means that, for instance, an `info` value of 1 would represent
+
-- This means that, for instance, an `info` value of 2 would represent
 
-- Desktop-only exclusivity ("d"):
 
-- Desktop-only exclusivity ("d"):
-- decimal 1 = binary 000001
+
-- decimal 2 = binary 00000010
-- j3mocd -> "d"
+
-- ts3mocdj -> "d"
-- Similarly, an `info` value of 20 would represent Old-gen and 3DS exclusivity ("o3"):
+
-- Similarly, an `info` value of 40 would represent Old-gen and 3DS exclusivity ("o3"):
-- decimal 20 = binary 010100
+
-- decimal 40 = binary 00101000
-- j3mocd -> "o3"
+
-- ts3mocdj -> "o3"
 
-- See Module:Exclusive/data for a quick overview of all values.
 
-- See Module:Exclusive/data for a quick overview of all values.
   
 
-- This system allows using bitwise operations (https://en.wikipedia.org/wiki/Bitwise_operation)
 
-- This system allows using bitwise operations (https://en.wikipedia.org/wiki/Bitwise_operation)
 
-- instead of the formerly used string processing, resulting in much lower script execution times.
 
-- instead of the formerly used string processing, resulting in much lower script execution times.
 
   
 
-- get info about page
 
-- get info about page
Ligne 136 : Ligne 135 :
 
info = readFromDb(page)
 
info = readFromDb(page)
 
if invert then
 
if invert then
-- invert dcom3 and set j=0 (always force-off Japanese console when inverting)
+
-- invert jdcom3st and set j=0 (always force-off Japanese console when inverting)
  +
-- (0xFE is 11111110 in binary, i.e. "dcom3st")
info = bit32.band(bit32.bnot(info), 31)
+
info = bit32.band(bit32.bnot(info), 0xFE)
 
end
 
end
 
if pagenot then
 
if pagenot then
Ligne 148 : Ligne 148 :
 
-- bit masking (https://en.wikipedia.org/wiki/Mask_(computing)).
 
-- bit masking (https://en.wikipedia.org/wiki/Mask_(computing)).
 
-- The following example demonstrates the operations:
 
-- The following example demonstrates the operations:
-- 1. assume info=11 ("dcm", binary 001011)
+
-- 1. assume info=214 ("dcmst", binary 11010110)
 
-- 2. invert:
 
-- 2. invert:
-- 2a. not(001011) = 110100 ("o3j", the inverse of "dcm")
+
-- 2a. not(11010110) = 00101001 ("jo3", the inverse of "dcmst")
-- 2b. and(110100, 011111) = 010100 ("o3", forced-off "j")
+
-- 2b. and(00101001, 11111110) = 00101000 ("o3", forced-off "j")
-- 3. assume info_not=4 ("o", binary 000100)
+
-- 3. assume info_not=8 ("o", binary 00001000)
-- 3a. not(000100) = 111011
+
-- 3a. not(00001000) = 11110111
-- 3b. and(010100, 111011) = 010000 ("3")
+
-- 3b. and(00101000, 11110111) = 00100000 ("3")
-- An initial exclusivity info of "dcm" was inverted to "o3",
+
-- An initial exclusivity info of "dcmst" was inverted to "o3",
 
-- then "o" was subtracted from it, resulting in the final
 
-- then "o" was subtracted from it, resulting in the final
 
-- exclusivity information of "3".
 
-- exclusivity information of "3".
Ligne 161 : Ligne 161 :
   
 
-- override if needed
 
-- override if needed
if dcom3j == nil or dcom3j == ':::::' then
+
if jdcom3st == nil or jdcom3st == ':::::::' then
 
return info
 
return info
 
else
 
else
return override(info, dcom3j)
+
return override(info, jdcom3st)
 
end
 
end
 
end
 
end
Ligne 174 : Ligne 174 :
 
---@return string
 
---@return string
 
local function eicons(info, _small)
 
local function eicons(info, _small)
local class = 'eico' .. (_small and ' s' or '')
+
local class = _small and 'eico s' or 'eico'
 
local hovertext
   
if bit32.btest(info, 32) then
+
if bit32.btest(info, 0x01) then
 
-- Japanese console is set, so simply display that
 
-- Japanese console is set, so simply display that
 
-- ("j" is always alone or not set at all – "dcj", for instance, doesn't exist)
 
-- ("j" is always alone or not set at all – "dcj", for instance, doesn't exist)
  +
return '<span class="'..class..' j" title="'..l10n('text_j')..'"></span>';
class = class .. ' j'
 
local hovertext = l10n('text_j')
 
return mw.text.tag('span', {class=class}, mw.text.tag('span', {title=hovertext}, ''))
 
 
end
 
end
   
 
-- for each platform of dcom3, add the class and load the hovertext if the platform if set
 
-- for each platform of dcom3, add the class and load the hovertext if the platform if set
-- (e.g. for info=25 ("dm3"), append "i0 i3 i4" to the class and load the
+
-- (e.g. for info=50 ("dm3"), append "i1 i4 i5" to the class and load the
-- "text_0", "text_3", and "text_4" l10n strings)
+
-- "text_1", "text_4", and "text_5" l10n strings)
 
local v = {}
 
local v = {}
for i = 0, 4 do
+
for i = 1, 7 do -- 0 is "j"
 
if bit32.btest(info, 2^i) then
 
if bit32.btest(info, 2^i) then
class = class .. ' i' .. i
+
class = class .. " i" .. i
 
v[#v+1] = l10n('text_' .. i)
 
v[#v+1] = l10n('text_' .. i)
 
end
 
end
 
end
 
end
 
local hovertext = mw.text.listToText(v, l10n('list_separator'), l10n('list_conjunction'))
 
local hovertext = mw.text.listToText(v, l10n('list_separator'), l10n('list_conjunction'))
hovertext = hovertext .. contentLanguage:convertPlural(#v, l10n('version_plural_forms'))
+
hovertext = string.format(contentLanguage:convertPlural(#v, l10n('version_plural_forms')), hovertext)
return mw.text.tag('span', {class=class}, mw.text.tag('span', {title=hovertext}, ''))
+
return '<span class="'..class..'" title="'..hovertext..'"><b></b><i></i></span>';
 
end
 
end
   
 
---Check if the `infoToCheck` integer is a valid number for output.
 
---Check if the `infoToCheck` integer is a valid number for output.
 
---It is considered invalid if it represents an empty exclusivity ("")
 
---It is considered invalid if it represents an empty exclusivity ("")
---or a "full" exclusivity, i.e. all platforms being set ("dcom3" or "dcom3j").
+
---or a "full" exclusivity, i.e. all platforms being set ("dcom3st" or "jdcom3st").
 
---@param infoToCheck number
 
---@param infoToCheck number
 
---@return boolean
 
---@return boolean
 
local function infoIsInvalid(infoToCheck)
 
local function infoIsInvalid(infoToCheck)
return infoToCheck == 0 or infoToCheck == 31 or infoToCheck == 63
+
return infoToCheck == 0x00 or infoToCheck == 0xFE or infoToCheck == 0xFF
 
end
 
end
   
Ligne 220 : Ligne 219 :
 
if infoIsInvalid(info) then
 
if infoIsInvalid(info) then
 
frame:callParserFunction{ name = '#dplvar:set', args = {
 
frame:callParserFunction{ name = '#dplvar:set', args = {
  +
'ex_j', '',
 
'ex_d', '',
 
'ex_d', '',
 
'ex_c', '',
 
'ex_c', '',
Ligne 225 : Ligne 225 :
 
'ex_m', '',
 
'ex_m', '',
 
'ex_3', '',
 
'ex_3', '',
'ex_j', '',
+
'ex_s', '',
  +
'ex_t', '',
 
'ex_cached', 'y'
 
'ex_cached', 'y'
 
} }
 
} }
 
else
 
else
 
frame:callParserFunction{ name = '#dplvar:set', args = {
 
frame:callParserFunction{ name = '#dplvar:set', args = {
'ex_d', bit32.btest(info, 2^0) and 'y' or '',
+
'ex_j', bit32.btest(info, 2^0) and 'y' or '',
'ex_c', bit32.btest(info, 2^1) and 'y' or '',
+
'ex_d', bit32.btest(info, 2^1) and 'y' or '',
'ex_o', bit32.btest(info, 2^2) and 'y' or '',
+
'ex_c', bit32.btest(info, 2^2) and 'y' or '',
'ex_m', bit32.btest(info, 2^3) and 'y' or '',
+
'ex_o', bit32.btest(info, 2^3) and 'y' or '',
'ex_3', bit32.btest(info, 2^4) and 'y' or '',
+
'ex_m', bit32.btest(info, 2^4) and 'y' or '',
'ex_j', bit32.btest(info, 2^5) and 'y' or '',
+
'ex_3', bit32.btest(info, 2^5) and 'y' or '',
  +
'ex_s', bit32.btest(info, 2^6) and 'y' or '',
  +
'ex_t', bit32.btest(info, 2^7) and 'y' or '',
 
'ex_cached', 'y'
 
'ex_cached', 'y'
 
} }
 
} }
Ligne 245 : Ligne 248 :
 
args_table = parse(frame.args[1])-- cache
 
args_table = parse(frame.args[1])-- cache
   
local info = getInfo(getArg('page'), getArg('invert'), getArg('pagenot'), getArg('dcom3j'))
+
local info = getInfo(getArg('page'), getArg('invert'), getArg('pagenot'), getArg('jdcom3st'))
 
if infoIsInvalid(info) then
 
if infoIsInvalid(info) then
 
return frame:expandTemplate{ title = 'error', args = { l10n('eicons_error_text'), l10n('eicons_error_cate'), from = 'Eicons' } }
 
return frame:expandTemplate{ title = 'error', args = { l10n('eicons_error_text'), l10n('eicons_error_cate'), from = 'Eicons' } }

Dernière version du 10 octobre 2021 à 22:58

Voir aussi la page anglaise du module : Module:Exclusive. Elle pourra contenir des informations plus complètes et actuelles.

Aucune sous-page de documentation n'existe déjà pour ce module. En créer une maintenant.


---Holds the tables with the l10n information for the different languages, taken from the l10n submodule.
local l10n_data = mw.loadData('Module:Exclusive/l10n')

---Database with exclusivity info.
local exclusive_info = mw.loadData('Module:Exclusive/data')

local bit32 = require('bit32')
local trim = mw.text.trim

---Default content language of the wiki (i.e. `$wgLanguageCode`, not the value of the
---`uselang` URL parameter, and not the user's language preference setting).
local contentLanguage = mw.getContentLanguage()

---Holds the arguments from the template call.
local args_table

---The current language. Determines which l10n table to use.
local lang

---Return the l10n string associated with the `key`.
---@param key string
---@return string
local function l10n(key)
	if l10n_data[lang] then
		return l10n_data[lang][key] or l10n_data['en'][key]
	else
		return l10n_data['en'][key]
	end
end

---Return a trimmed version of the value of the template parameter with the specified `key`.
---Return `nil` if the parameter is empty or unset.
---@param key string|number
---@return string|nil
local function getArg(key)
	local value = trim(args_table[key] or '')
	return (value ~= '') and value or nil
end

---Convert a string of parameters in a `@param1:value^@param2:value^` format to a table.
---@param paramstr string
---@return table
local function parse(paramstr)
	local args = {}
	for s in string.gmatch(paramstr, '%b@^') do
		local k,v = string.match(s, '^@(.-):(.*)^$')
		args[k] = v
	end
	return args
end

---Split the `str` on each `div` in it and return the result as a table.
---This is much much faster then `mw.text.split`.
---Credit: http://richard.warburton.it.
---@param div string
---@param str string
---@return table|boolean
local function explode(div,str)
	if (div=='') then return false end
	local pos,arr = 0,{}
	-- for each divider found
	for st,sp in function() return string.find(str,div,pos,true) end do
		arr[#arr + 1] = string.sub(str,pos,st-1) -- Attach chars left of current divider
		pos = sp + 1 -- Jump past current divider
	end
	arr[#arr + 1] = string.sub(str,pos) -- Attach chars right of last divider
	return arr
end

---Return the integer defined in the database for the specified `page`.
---Perform some standardization on the `page` for that first.
---@param page string
---@return number
local function readFromDb(page)
	-- standardize pagename: remove section parts ('x#section' -> 'x') and replace underscores with spaces
	page = contentLanguage:ucfirst(string.gsub(string.gsub(page or '', '#.*', ''), '_', ' '))
	return exclusive_info[page] or 0
end

---Override the exclusivity information in `info`
---with the content of `jdcom3st`.
---@param info number
---@param jdcom3st string Expected format: `<j>:<d>:<c>:<o>:<m>:<3>:<s>:<t>`, with each platform either a Boolean string ("y", "0", etc.) or an empty string
---@return number
local function override(info, jdcom3st)
	for k, v in pairs(explode(':', jdcom3st)) do
		if v ~= '' then
			if v == '1' or v == 'y' or v == 'yes' then
				info = bit32.replace(info, 1, k-1)
			elseif v == '0' or v == 'n' or v == 'no' then
				info = bit32.replace(info, 0, k-1)
			end
		end
	end
	return info

	-- Example to demonstrate the behavior of this function:
	-- Goal: Change "dcom" to "dco3".
	-- info = 30 ("dcom"), jdcom3st = "::::no:yes::"
	-- decimal 30 = binary 00011110
	-- The first "no" is at index 4 in the jdcom3st string, so
	-- replace the corresponding bit with a 0: 00011110 -> 00001110.
	-- The "yes" is at index 5, so replace the bit at index 5
	-- with a 1: 00001110 -> 00101110.
	-- The result is info = 46 ("dco3").
end

---Main function to retrieve exclusivity information.
---@param page string The entity to get the info about.
---@param invert boolean Whether to invert the exclusivity info.
---@param pagenot string The entity whose exclusivity info to subtract from the main one's.
---@param jdcom3st string Manual exclusivity info to override the fetched one with.
---@return number info An integer that holds the exclusivity information.
local function getInfo(page, invert, pagenot, jdcom3st)
	local info = 0

	-- A piece of exclusivity information is a set of Boolean values, one
	-- for each platform. This is represented as bits of the `info` integer.
	-- Each platform (Japanese console, Desktop, Console, Old-gen console, Mobile, 3DS,
	-- Switch, and TModLoader – "jdcom3st") is assigned one bit, in this order.
	-- This means that, for instance, an `info` value of 2 would represent
	-- Desktop-only exclusivity ("d"):
	-- decimal  2 = binary 00000010
	--                     ts3mocdj  -> "d"
	-- Similarly, an `info` value of 40 would represent Old-gen and 3DS exclusivity ("o3"):
	-- decimal 40 = binary 00101000
	--                     ts3mocdj -> "o3"
	-- See Module:Exclusive/data for a quick overview of all values.

	-- This system allows using bitwise operations (https://en.wikipedia.org/wiki/Bitwise_operation)
	-- instead of the formerly used string processing, resulting in much lower script execution times.

	-- get info about page
	if page then
		info = readFromDb(page)
		if invert then
			-- invert jdcom3st and set j=0 (always force-off Japanese console when inverting)
			-- (0xFE is 11111110 in binary, i.e. "dcom3st")
			info = bit32.band(bit32.bnot(info), 0xFE)
		end
		if pagenot then
			-- exclude some versions, depending on pagenot
			local info_not = readFromDb(pagenot)
			info = bit32.band(info, bit32.bnot(info_not))
		end

		-- The "invert" and "pagenot" functionalities above utilize
		-- bit masking (https://en.wikipedia.org/wiki/Mask_(computing)).
		-- The following example demonstrates the operations:
		-- 1. assume info=214 ("dcmst", binary 11010110)
		-- 2. invert:
		--    2a. not(11010110) = 00101001 ("jo3", the inverse of "dcmst")
		--    2b. and(00101001, 11111110) = 00101000 ("o3", forced-off "j")
		-- 3. assume info_not=8 ("o", binary 00001000)
		--    3a. not(00001000) = 11110111
		--    3b. and(00101000, 11110111) = 00100000 ("3")
		-- An initial exclusivity info of "dcmst" was inverted to "o3",
		-- then "o" was subtracted from it, resulting in the final
		-- exclusivity information of "3".
	end

	-- override if needed
	if jdcom3st == nil or jdcom3st == ':::::::' then
		return info
	else
		return override(info, jdcom3st)
	end
end

---Return an HTML span tag whose `class` attribute is set
---according to the exclusivity `info`.
---@param info number
---@param _small boolean Whether to add the "s" class, for small icons
---@return string
local function eicons(info, _small)
	local class = _small and 'eico s' or 'eico'
	local hovertext

	if bit32.btest(info, 0x01) then
		-- Japanese console is set, so simply display that
		-- ("j" is always alone or not set at all – "dcj", for instance, doesn't exist)
		return '<span class="'..class..' j" title="'..l10n('text_j')..'"></span>';
	end

	-- for each platform of dcom3, add the class and load the hovertext if the platform if set
	-- (e.g. for info=50 ("dm3"), append "i1 i4 i5" to the class and load the
	-- "text_1", "text_4", and "text_5" l10n strings)
	local v = {}
	for i = 1, 7 do -- 0 is "j"
		if bit32.btest(info, 2^i) then
			class = class .. " i" .. i
			v[#v+1] = l10n('text_' .. i)
		end
	end
	local hovertext = mw.text.listToText(v, l10n('list_separator'), l10n('list_conjunction'))
	hovertext = string.format(contentLanguage:convertPlural(#v, l10n('version_plural_forms')), hovertext)
	return '<span class="'..class..'" title="'..hovertext..'"><b></b><i></i></span>';
end

---Check if the `infoToCheck` integer is a valid number for output.
---It is considered invalid if it represents an empty exclusivity ("")
---or a "full" exclusivity, i.e. all platforms being set ("dcom3st" or "jdcom3st").
---@param infoToCheck number
---@return boolean
local function infoIsInvalid(infoToCheck)
	return infoToCheck == 0x00 or infoToCheck == 0xFE or infoToCheck == 0xFF
end

-----------------------------------------------------------------
-- main return object
return {

-- for templates; get all exclusive info and set it in dplvars.
-- parameters: $1 = pagename
getInfo = function(frame)
	args_table = frame.args -- cache

	local info = getInfo(getArg(1), getArg('invert'), getArg('pagenot'))
	if infoIsInvalid(info) then
		frame:callParserFunction{ name = '#dplvar:set', args = {
			'ex_j', '',
			'ex_d', '',
			'ex_c', '',
			'ex_o', '',
			'ex_m', '',
			'ex_3', '',
			'ex_s', '',
			'ex_t', '',
			'ex_cached', 'y'
		} }
	else
		frame:callParserFunction{ name = '#dplvar:set', args = {
			'ex_j', bit32.btest(info, 2^0) and 'y' or '',
			'ex_d', bit32.btest(info, 2^1) and 'y' or '',
			'ex_c', bit32.btest(info, 2^2) and 'y' or '',
			'ex_o', bit32.btest(info, 2^3) and 'y' or '',
			'ex_m', bit32.btest(info, 2^4) and 'y' or '',
			'ex_3', bit32.btest(info, 2^5) and 'y' or '',
			'ex_s', bit32.btest(info, 2^6) and 'y' or '',
			'ex_t', bit32.btest(info, 2^7) and 'y' or '',
			'ex_cached', 'y'
		} }
	end
end,

-- for {{eicons}}
eicons = function(frame)
	args_table = parse(frame.args[1])-- cache

	local info = getInfo(getArg('page'), getArg('invert'), getArg('pagenot'), getArg('jdcom3st'))
	if infoIsInvalid(info) then
		return frame:expandTemplate{ title = 'error', args = { l10n('eicons_error_text'), l10n('eicons_error_cate'), from = 'Eicons' } }
	end
	lang = getArg('lang') -- set lang for l10n
	return eicons(info, getArg('small'))
end,

-- simplified version of the eicons function above, for other modules such as Module:Item
simpleEicons = function(page, language, small)
	local info = getInfo(page)
	if infoIsInvalid(info) then
		return ''
	end
	lang = language -- set lang for l10n
	return eicons(info, small)
end,

}