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

125 lines
3.4 KiB
Lua
Raw Normal View History

2022-03-19 19:57:07 -04:00
game:addlocalizedstring("MENU_MODS", "MODS")
game:addlocalizedstring("MENU_MODS_DESC", "Load installed mods.")
game:addlocalizedstring("LUA_MENU_MOD_DESC_DEFAULT", "Load &&1.")
game:addlocalizedstring("LUA_MENU_MOD_DESC", "&&1\nAuthor: &&2\nVersion: &&3")
game:addlocalizedstring("LUA_MENU_OPEN_STORE", "Open store")
game:addlocalizedstring("LUA_MENU_OPEN_STORE_DESC", "Download and install mods.")
game:addlocalizedstring("LUA_MENU_LOADED_MOD", "Loaded mod: ^3&&1")
game:addlocalizedstring("LUA_MENU_AVAILABLE_MODS", "Available mods")
game:addlocalizedstring("LUA_MENU_UNLOAD", "Unload")
game:addlocalizedstring("LUA_MENU_UNLOAD_DESC", "Unload the currently loaded mod.")
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", {
2022-03-19 19:57:07 -04:00
title_bar_text = Engine.ToUpperCase(text)
2022-01-29 20:04:58 -05:00
}))
menu.list:addElement(element)
end
function string:truncate(length)
2022-03-19 19:57:07 -04:00
if (#self <= length) then
return self
end
2022-01-29 20:04:58 -05:00
2022-03-19 19:57:07 -04:00
return self:sub(1, length - 3) .. "..."
2022-01-29 20:04:58 -05:00
end
2022-01-30 16:55:21 -05:00
LUI.addmenubutton("main_campaign", {
2022-03-19 19:57:07 -04:00
index = 6,
text = "@MENU_MODS",
description = Engine.Localize("@MENU_MODS_DESC"),
callback = function()
LUI.FlowManager.RequestAddMenu(nil, "mods_menu")
end
2022-01-30 16:55:21 -05:00
})
2022-01-29 20:04:58 -05:00
2022-01-30 18:13:55 -05:00
function getmodname(path)
2022-03-19 19:57:07 -04:00
local name = path
local desc = Engine.Localize("@LUA_MENU_MOD_DESC_DEFAULT", name)
local infofile = path .. "/info.json"
if (io.fileexists(infofile)) then
pcall(function()
local data = json.decode(io.readfile(infofile))
desc = Engine.Localize("@LUA_MENU_MOD_DESC",
data.description, data.author, data.version)
name = data.name
end)
end
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)
2022-03-19 19:57:07 -04:00
local menu = LUI.MenuTemplate.new(a1, {
menu_title = "@MENU_MODS",
exclusiveController = 0,
menu_width = 400,
menu_top_indent = LUI.MenuTemplate.spMenuOffset,
showTopRightSmallBar = true
})
2022-01-29 20:04:58 -05:00
2022-03-19 19:57:07 -04:00
menu:AddButton("@LUA_MENU_OPEN_STORE", function()
if (LUI.MenuBuilder.m_types_build["mod_store_menu"]) then
LUI.FlowManager.RequestAddMenu(nil, "mod_store_menu")
end
end, nil, true, nil, {
desc_text = Engine.Localize("@LUA_MENU_OPEN_STORE_DESC")
})
2022-01-29 20:04:58 -05:00
2022-03-19 19:57:07 -04:00
local modfolder = game:getloadedmod()
if (modfolder ~= "") then
createdivider(menu, Engine.Localize("@LUA_MENU_LOADED_MOD", getmodname(modfolder):truncate(24)))
menu:AddButton("@LUA_MENU_UNLOAD", function()
game:executecommand("unloadmod")
end, nil, true, nil, {
desc_text = Engine.Localize("@LUA_MENU_UNLOAD_DESC")
})
end
createdivider(menu, Engine.Localize("@LUA_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
game:addlocalizedstring(name, name)
menu:AddButton(name, function()
game:executecommand("loadmod " .. mods[i])
end, nil, true, nil, {
desc_text = desc
})
end
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