Last active
September 10, 2020 20:46
-
-
Save Alf21/2ec5f08d99d34db3135fbfa5c7d3c6c7 to your computer and use it in GitHub Desktop.
An example how to dynamically change (role's) language strings and the roles' & teams' icons. If you just wanna change the role's language strings, the code can be shrinked a lot! This code snippet also includes ConVar support (toggling this mode) as well as full ULX integration.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
-- @author Alf21 | |
-- @author Mineotopia | |
local iconTableNew = { | |
PIRATE_CAPTAIN = "vgui/ttt/dynamic/roles/icon_pirate2", | |
INFECTED = "vgui/ttt/dynamic/roles/icon_infected2" | |
} | |
-- if putting a new image to that not included in iconTableNew, you need to add this to FastDL (resource.AddFile(...)) | |
local teamTableNew = { | |
TEAM_PIRATE = "vgui/ttt/dynamic/roles/icon_pirate2", | |
TEAM_INFECTED = "vgui/ttt/dynamic/roles/icon_infected2", | |
} | |
if SERVER then | |
AddCSLuaFile() | |
-- FastDL | |
for _, val in pairs(iconTableNew) do | |
resource.AddFile("materials/" .. val .. ".vmt") | |
end | |
-- Networking | |
util.AddNetworkString("TTT2LangRoleMod") | |
-- CVar | |
local langCVar = CreateConVar("ttt2_lang_rolemod", "0", {FCVAR_NOTIFY, FCVAR_ARCHIVE}, "Toggles Language Role Modification Modus") | |
-- ConVar Replicating | |
hook.Add("TTTUlxInitCustomCVar", "TTTLangRoleModInitRWCVar", function(name) | |
local tmp = langCVar:GetName() | |
ULib.replicatedWritableCvar(tmp, "rep_" .. tmp, langCVar:GetBool(), true, true, "xgui_gmsettings") | |
end) | |
hook.Add("TTT2SyncGlobals", "AddLangRoleModCVar", function() | |
SetGlobalBool(langCVar:GetName(), langCVar:GetBool()) | |
end) | |
cvars.AddChangeCallback(langCVar:GetName(), function(name, _, new) | |
local b = tonumber(new) == 1 | |
SetGlobalBool(name, b) | |
net.Start("TTT2LangRoleMod") | |
net.WriteBit(b) | |
net.Broadcast() | |
end, "TTT2LangRoleModCVar") | |
else -- CLIENT | |
-- ULX thing | |
hook.Add("TTTUlxModifyAddonSettings", "TTTLangRoleModModifySettings", function(name) | |
local clspnl = xlib.makelistlayout{w = 415, h = 318, parent = xgui.null} | |
local clsclp = vgui.Create("DCollapsibleCategory", clspnl) | |
clsclp:SetSize(390, 75) | |
clsclp:SetExpanded(1) | |
clsclp:SetLabel("Basic Settings") | |
local clslst = vgui.Create("DPanelList", clsclp) | |
clslst:SetPos(5, 25) | |
clslst:SetSize(390, 75) | |
clslst:SetSpacing(5) | |
local clslim = xlib.makecheckbox{label = "Enable Lang Role Mod? (ttt2_lang_rolemod) (def. 1)", repconvar = "rep_ttt2_lang_rolemod", parent = clslst} | |
clslst:AddItem(clslim) | |
xlib.makelabel{x = 0, y = 20, w = 415, wordwrap = true, label = "This modifies the roles and teams icons as well as the role names.", parent = clslst} | |
xgui.hookEvent("onProcessModules", nil, clspnl.processModules) | |
xgui.addSubModule("TTT2 LangRole Mod", clspnl, nil, name) | |
end) | |
local langTableNew = {} | |
local langTableOld = {} | |
local function InitializeCustomLanguageTable() | |
if PIRATE_CAPTAIN then | |
langTableNew.PIRATE_CAPTAIN = { | |
english = { | |
[PIRATE_CAPTAIN.name] = "Captain2", | |
["info_popup_" .. PIRATE_CAPTAIN.name] = [[You are a Captain2! Search someone to fight for - earn gold and points.]], | |
["body_found_" .. PIRATE_CAPTAIN.abbr] = "They were a Captain2...", | |
["search_role_" .. PIRATE_CAPTAIN.abbr] = "This person was a Captain2!", | |
["target_" .. PIRATE_CAPTAIN.name] = "Captain2", | |
["ttt2_desc_" .. PIRATE_CAPTAIN.name] = [[The Captain2 is a neutral role. He doesn’t really care about what’s good and what’s evil… | |
all that matters is, that there’s money involved. As long as another person owns the Captain2’s contract, all Pirates are on the same team as them.]], | |
}, | |
deutsch = { | |
[PIRATE_CAPTAIN.name] = "Captain2", | |
["info_popup_" .. PIRATE_CAPTAIN.name] = [[Du bist ein Captain2! Tu dich mit jemandem zusammen und kämpfe für Gold und Punkte.]], | |
["body_found_" .. PIRATE_CAPTAIN.abbr] = "Er war ein Captain2...", | |
["search_role_" .. PIRATE_CAPTAIN.abbr] = "Diese Person war ein Captain2!", | |
["target_" .. PIRATE_CAPTAIN.name] = "Captain2", | |
["ttt2_desc_" .. PIRATE_CAPTAIN.name] = [[Der Captain2 ist neutral. Er kümmert sich nicht um gut und böse... das Geld muss stimmen. | |
So lange eine andere Person einen Vertrag mit dem Captain2 geschlossen hat, kämpfen alle Piraten für sein Team.]], | |
} | |
} | |
end | |
if INFECTED then | |
langTableNew.INFECTED = { | |
english = { | |
[INFECTED.name] = "Infected2", | |
[TEAM_INFECTED] = "TEAM Infected2", | |
["hilite_win_" .. TEAM_INFECTED] = "THE INFECTED2 WON", | |
["win_" .. TEAM_INFECTED] = "The Infected2 has won!", | |
["info_popup_" .. INFECTED.name] = [[Now its your turn! Infect them ALL.]], | |
["body_found_" .. INFECTED.abbr] = "They were a Infected2.", | |
["search_role_" .. INFECTED.abbr] = "This person was a Infected2!", | |
["ev_win_" .. TEAM_INFECTED] = "The crazy Infected2 won the round!", | |
["target_" .. INFECTED.name] = "Infected2", | |
["ttt2_desc_" .. INFECTED.name] = [[The Infected2 needs to infect every player to win. He will infect other players by killing them. | |
If a player gets infected, the player looks like a zombie and is also able to infect other players. So you can build up a whole army. | |
But there is one thing you need to get in mind: If the host (the main infected player with a normal model) will die or disconnect, each player that the host infected will die. | |
If there is a Jester, feel free to infect him ]], | |
}, | |
deutsch = { | |
[INFECTED.name] = "Infected2", | |
[TEAM_INFECTED] = "TEAM Infected2", | |
["hilite_win_" .. TEAM_INFECTED] = "INFECTED2 GEWINNEN", | |
["win_" .. TEAM_INFECTED] = "Der Infected2 hat gewonnen!", | |
["info_popup_" .. INFECTED.name] = [[Jetzt bist du dran! Infiziere sie ALLE...]], | |
["body_found_" .. INFECTED.abbr] = "Er war ein Infected2...", | |
["search_role_" .. INFECTED.abbr] = "Diese Person war ein Infected2!", | |
["ev_win_" .. TEAM_INFECTED] = "Der verrückte Infected2 hat die Runde gewonnen!", | |
["target_" .. INFECTED.name] = "Infected2", | |
["ttt2_desc_" .. INFECTED.name] = [[Der Infected2 muss alle anderen Spieler infizieren, um zu gewinnen. Dies tut er, indem er die Spieler tötet. | |
Wenn ein Spieler infiziert wird, wird er wie ein Zombie aussehen und wird ebenfalls andere Spieler infizieren können. Also erbaue Deine Armee! | |
Doch es gibt eine Sache, an die Du denken solltest: Stirbt/Disconnected der Host (der erste Infizierte mit dem normalen Playermodel), stirbt auch jeder Infizierte, der von ihm infiziert wurde. | |
Falls es einen Jester gibt, zögere nicht und infiziere ihn!]], | |
} | |
} | |
end | |
end | |
local function BackupDefaultLanguageTable() | |
local GetTransFromLang = LANG.GetTranslationFromLanguage | |
for index, tbl in pairs(langTableNew) do | |
for lang, tbl2 in pairs(tbl) do | |
for key in pairs(tbl2) do | |
langTableOld[index] = langTableOld[index] or {} | |
langTableOld[index][lang] = langTableOld[index][lang] or {} | |
langTableOld[index][lang][key] = GetTransFromLang(key, lang) | |
end | |
end | |
end | |
end | |
local iconTableOld = {} | |
local function BackupDefaultIconTable() | |
for roleIndex in pairs(iconTableNew) do | |
local rd = _G[roleIndex] | |
if not rd or not rd.name then continue end | |
for _, roleData in ipairs(roles.GetList()) do | |
if rd.name == roleData.name then | |
iconTableOld[roleIndex] = roleData.icon | |
break | |
end | |
end | |
end | |
end | |
local teamTableOld = {} | |
local function BackupDefaultTeamTable() | |
for teamName in pairs(teamTableNew) do | |
local tm = _G[teamName] | |
if not tm or not TEAMS or not TEAMS[tm] then continue end | |
teamTableOld[teamName] = TEAMS[tm].icon | |
end | |
end | |
local function ModifyLanguage(langTable) | |
local AddToLang = LANG.AddToLanguage | |
for _, tbl in pairs(langTable) do | |
for lang, tbl2 in pairs(tbl) do | |
for key, val in pairs(tbl2) do | |
AddToLang(lang, key, val) | |
end | |
end | |
end | |
end | |
local function ModifyIcons(iconTable) | |
for roleIndex, val in pairs(iconTable) do | |
local rd = _G[roleIndex] | |
if not rd or not rd.name then continue end | |
rd.icon = val | |
rd.iconMaterial = Material(val) | |
end | |
end | |
local function ModifyTeams(teamTable) | |
for teamName, val in pairs(teamTable) do | |
local tm = _G[teamName] | |
if not tm or not TEAMS or not TEAMS[tm] then continue end | |
TEAMS[tm].icon = val | |
TEAMS[tm].iconMaterial = Material(val) | |
end | |
end | |
-- Icon updates | |
hook.Add("InitPostEntity", "ModifyTTT2Imgs4LangRoleMod", function() | |
local b = GetGlobalBool("ttt2_lang_rolemod", false) | |
ModifyLanguage(b and langTableNew or langTableOld) | |
ModifyIcons(b and iconTableNew or iconTableOld) | |
ModifyTeams(b and teamTableNew or teamTableOld) | |
end) | |
hook.Add("PostInitialize", "TTT2LangRoleOverride", function() | |
InitializeCustomLanguageTable() | |
BackupDefaultLanguageTable() | |
BackupDefaultIconTable() | |
BackupDefaultTeamTable() | |
end) | |
net.Receive("TTT2LangRoleMod", function() | |
local b = net.ReadBool() | |
ModifyLanguage(b and langTableNew or langTableOld) | |
ModifyIcons(b and iconTableNew or iconTableOld) | |
ModifyTeams(b and teamTableNew or teamTableOld) | |
end) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment