h2-mod/data/ui_scripts/mods/loading.lua

123 lines
3.3 KiB
Lua
Raw Normal View History

2022-01-29 20:04:58 -05:00
function createdivider(menu, text)
local element = LUI.UIElement.new( {
leftAnchor = true,
rightAnchor = true,
left = 0,
right = 0,
topAnchor = true,
bottomAnchor = false,
top = 0,
bottom = 33.33
})
element.scrollingToNext = true
element:addElement(LUI.MenuBuilder.BuildRegisteredType("h1_option_menu_titlebar", {
title_bar_text = Engine.ToUpperCase(Engine.Localize(text))
}))
menu.list:addElement(element)
end
function string:truncate(length)
if (#self <= length) then
return self
end
return self:sub(1, length - 3) .. "..."
end
2022-01-30 16:55:21 -05:00
LUI.addmenubutton("main_campaign", {
index = 6,
text = "$_MODS",
description = "Load installed mods.",
callback = function()
2022-01-29 20:04:58 -05:00
LUI.FlowManager.RequestAddMenu(nil, "mods_menu")
2022-01-30 16:55:21 -05:00
end
})
2022-01-29 20:04:58 -05:00
2022-01-30 18:13:55 -05:00
function getmodname(path)
local name = path
2022-02-02 19:22:50 -05:00
local desc = "Load " .. name
2022-01-30 18:13:55 -05:00
local infofile = path .. "/info.json"
if (io.fileexists(infofile)) then
pcall(function()
local data = json.decode(io.readfile(infofile))
desc = string.format("%s\nAuthor: %s\nVersion: %s",
data.description, data.author, data.version)
name = data.name
end)
end
2022-02-02 19:22:50 -05:00
return name, desc
2022-01-30 18:13:55 -05:00
end
2022-01-29 20:04:58 -05:00
LUI.MenuBuilder.m_types_build["mods_menu"] = function(a1)
local menu = LUI.MenuTemplate.new(a1, {
menu_title = "$_MODS",
exclusiveController = 0,
menu_width = 400,
menu_top_indent = LUI.MenuTemplate.spMenuOffset,
showTopRightSmallBar = true
})
menu:AddButton("$_OPEN STORE", function()
2022-01-30 16:55:21 -05:00
if (LUI.MenuBuilder.m_types_build["mod_store_menu"]) then
LUI.FlowManager.RequestAddMenu(nil, "mod_store_menu")
end
2022-01-29 20:04:58 -05:00
end, nil, true, nil, {
2022-01-30 16:55:21 -05:00
desc_text = "Download and install mods."
2022-01-29 20:04:58 -05:00
})
local modfolder = game:getloadedmod()
if (modfolder ~= "") then
2022-01-30 18:13:55 -05:00
createdivider(menu, "$_Loaded mod: ^3" .. getmodname(modfolder):truncate(20))
2022-01-29 20:04:58 -05:00
menu:AddButton("$_UNLOAD", function()
game:executecommand("unloadmod")
end, nil, true, nil, {
2022-02-03 14:23:46 -05:00
desc_text = "Unload the currently loaded mod."
2022-01-29 20:04:58 -05:00
})
end
createdivider(menu, "$_Available mods")
if (io.directoryexists("mods")) then
local mods = io.listfiles("mods/")
for i = 1, #mods do
if (io.directoryexists(mods[i]) and not io.directoryisempty(mods[i])) then
local name, desc = getmodname(mods[i])
if (mods[i] ~= modfolder) then
menu:AddButton("$_" .. name, function()
game:executecommand("loadmod " .. mods[i])
end, nil, true, nil, {
desc_text = desc
})
end
2022-01-29 20:04:58 -05:00
end
end
end
menu:AddBackButton(function(a1)
Engine.PlaySound(CoD.SFX.MenuBack)
LUI.FlowManager.RequestLeaveMenu(a1)
end)
LUI.Options.InitScrollingList(menu.list, nil)
menu:CreateBottomDivider()
menu.optionTextInfo = LUI.Options.AddOptionTextInfo(menu)
return menu
end
local localize = Engine.Localize
Engine.Localize = function(...)
local args = {...}
if (type(args[1]) == "string" and args[1]:sub(1, 2) == "$_") then
return args[1]:sub(3, -1)
end
return localize(table.unpack(args))
end