Module:Utility

From Primal Fear Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Utility/doc

local p = {}


function p.merge(dst, src)
	for k, v in pairs(src) do
		if v == nil and dst[k] ~= nil then
			dst[k] = nil
		elseif type(dst[k]) == 'table' and type(v) == 'table' then
			dst[k] = p.merge(dst[k], v)
		else
			dst[k] = v
		end
	end
	return dst
end


function p.deepcopy(orig)
	if type(orig) == 'table' then
		local copy = {}
		for k, v in pairs(orig) do
			copy[p.deepcopy(k)] = p.deepcopy(v)
		end
		return copy
	end
	return orig
end


function p.getUnqualifiedBlueprintPath(descriptor)
	-- Return nothing if descriptor is empty.
	if not descriptor then
		return nil
	end
	-- Remove double quotes from beginning and the end.
	if descriptor:sub(1, 1) == '"' then
		descriptor = descriptor:sub(2, -2)
	end
	-- Check if descriptor is a likely clean blueprint path, and exit early.
	local descriptorStart = 'Blueprint\''
	local descriptorEnd = '\''
	if descriptor:sub(1, #descriptorStart) ~= descriptorStart then
		return descriptor
	end
	-- Remove descriptor.
	return descriptor:sub(#descriptorStart+1, -#descriptorEnd-1)
end


function p.getUnqualifiedBlueprintPathW(f)
	return p.getUnqualifiedBlueprintPath(f.args[1]) or ''
end


function p.getBlueprintClassName(blueprintPath, withSuffix)
	-- Return nothing if blueprint path is empty.
	if not blueprintPath then
		return nil
	end
	local className = blueprintPath:match('%.([%w_]+)$')
	if withSuffix and className:sub(-2) ~= '_C' then
		return className .. '_C'
	elseif not withSuffix and className:sub(-2) == '_C' then
		className, _ = className:gsub('_C$', '', 1)
	end
	return className
end


function p.getBlueprintClassNameW(f)
	return p.getBlueprintClassName(f.args[1], not (f.args.withSuffix == '' or f.args.withSuffix == 'no')) or ''
end


return p