Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Source/Client/ModCompatibilityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Multiplayer.Common;
using RestSharp;
using Steamworks;
using UnityEngine;
using Verse;

namespace Multiplayer.Client
Expand Down Expand Up @@ -165,6 +166,9 @@ private static void SetupFrom(List<ModCompatibility> mods)
.ToDictionary(grouping => grouping.Key, grouping => grouping.First());
}

public static ModCompatibility? LookupByMod(ModMetaData meta) =>
LookupByWorkshopId(meta.GetPublishedFileId()) ?? LookupByName(meta.Name);

public static ModCompatibility? LookupByWorkshopId(PublishedFileId_t workshopId) =>
LookupByWorkshopId(workshopId.m_PublishedFileId);

Expand All @@ -188,5 +192,23 @@ public class ModCompatibility
public string name { get; set; }
public long workshopId { get; set; }
public string notes { get; set; } = "";

public static Color ScoreColor(int score) => score switch
{
1 => ColorLibrary.Red,
2 => ColorLibrary.Orange,
3 => ColorLibrary.Yellow,
4 => ColorLibrary.Green,
_ => ColorLibrary.Grey
};

public static string ScoreDescription(int score) => score switch
{
1 => "MpModCompatScore1",
2 => "MpModCompatScore2",
3 => "MpModCompatScore3",
4 => "MpModCompatScore4",
_ => "MpModCompatScoreUnk"
};
}
}
102 changes: 81 additions & 21 deletions Source/Client/Windows/ModCompatWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ModCompatWindow(Window parent, bool popup, bool forceNameSort, Func<strin
resizeable = true;
draggable = true;
layer = WindowLayer.SubSuper;
resizer = new WindowResizer() { minWindowSize = InitialSize };
resizer = new WindowResizer { minWindowSize = InitialSize };
}
else
{
Expand All @@ -62,6 +62,7 @@ public override void SetInitialSizeAndPosition()
private int? modsHash;
private (SortType t, SortDirection d) sort;
private bool nameFieldChanged;
private bool scrollToModName;

private Dictionary<string, string> modNameCache = new();
private Dictionary<string, string> notesCache = new();
Expand All @@ -75,6 +76,12 @@ public override void SetInitialSizeAndPosition()
const float ScrollbarWidth = 20f;
const float ListInset = 15f;

public void ScrollToModName(string modName)
{
nameFieldStr = modName;
scrollToModName = true;
}

public override void DoWindowContents(Rect inRect)
{
if (modsHash != ModLister.InstalledModsListHash(true))
Expand Down Expand Up @@ -128,6 +135,11 @@ ref Multiplayer.settings.hideTranslationMods
GUI.SetNextControlName("mod_search");
nameFieldStr = Widgets.TextField(nameField, nameFieldStr);
nameFieldChanged = nameFieldStr != prevNameField;
if (scrollToModName)
{
nameFieldChanged = true;
scrollToModName = false;
}
inRect.yMin += CheckboxesHeight + 10f;

GUI.BeginGroup(inRect);
Expand Down Expand Up @@ -249,7 +261,7 @@ private void DoHeaders(float width)

// Notes header
var notesHeader = headerRow.MaxX(headerRow.width - ScrollbarWidth);
Widgets.Label(notesHeader, $"MpModCompatHeaderNotes".Translate());
Widgets.Label(notesHeader, "MpModCompatHeaderNotes".Translate());
Widgets.DrawHighlightIfMouseover(notesHeader);
}

Expand All @@ -275,23 +287,10 @@ private void DoModRow(ModMetaData mod, bool alt, Rect row)
{
bool xml = MultiplayerData.IsXmlMod(mod);

var scoreColor = xml ? ColorLibrary.Green : info?.status switch
{
1 => ColorLibrary.Red,
2 => ColorLibrary.Orange,
3 => ColorLibrary.Yellow,
4 => ColorLibrary.Green,
_ => ColorLibrary.Grey
};

var scoreDescKey = xml ? "MpModCompatXmlOnlyDesc" : info?.status switch
{
1 => "MpModCompatScore1",
2 => "MpModCompatScore2",
3 => "MpModCompatScore3",
4 => "MpModCompatScore4",
_ => "MpModCompatScoreUnk"
};
var scoreColor = xml ? ColorLibrary.Green : ModCompatibility.ScoreColor(info?.status ?? 0);

var scoreDescKey =
xml ? "MpModCompatXmlOnlyDesc" : ModCompatibility.ScoreDescription(info?.status ?? 0);

var scoreText = xml
? "XML"
Expand Down Expand Up @@ -368,8 +367,7 @@ private static ModCompatibility TryGetCompatInfo(ModMetaData mod)
if (!Multiplayer.settings.showModCompatibility)
return null;

return ModCompatibilityManager.LookupByWorkshopId(mod.publishedFileIdInt) ??
ModCompatibilityManager.LookupByName(mod.Name);
return ModCompatibilityManager.LookupByMod(mod);
}

private static string SortChar(SortDirection dir) => dir switch
Expand Down Expand Up @@ -402,6 +400,68 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> inst
}
}

[HarmonyPatch(typeof(Page_ModsConfig), nameof(Page_ModsConfig.DoModRow))]
static class PageModsConfigShowModCompat
{
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> insts)
{
var labelMethod =
AccessTools.Method(typeof(Widgets), nameof(Widgets.Label), [typeof(Rect), typeof(string)]);
foreach (var inst in insts)
{
if (inst.Calls(labelMethod))
{
yield return CodeInstruction.LoadArgument(2); // ModMetaData
yield return CodeInstruction.LoadArgument(0); // Page_ModsConfig
inst.operand = AccessTools.Method(typeof(PageModsConfigShowModCompat), nameof(DrawModLabel));
}

yield return inst;
}
}

public static void DrawModLabel(Rect r, string label, ModMetaData mod, Page_ModsConfig parent)
{
if (!Multiplayer.settings.showModCompatibility)
{
Widgets.Label(r, label);
return;
}

var compat = ModCompatibilityManager.LookupByMod(mod);
var rect = new Rect(r.x, (float) (r.y + r.height / 2.0 - 12.0), 20f, 24f);
Text.Anchor = TextAnchor.MiddleCenter;

bool xml = MultiplayerData.IsXmlMod(mod);
var score = xml ? 4 : compat?.status ?? 0;
var scoreColor = ModCompatibility.ScoreColor(score);
Widgets.Label(rect, "[" + score.ToString().Colorize(scoreColor) + "]");

if (Mouse.IsOver(rect))
{
var scoreText = (xml ? "MpModCompatXmlOnlyDesc" : ModCompatibility.ScoreDescription(score)).Translate();
if (compat?.notes is { Length: > 0 })
{
scoreText += $"\n{compat.notes}";
}

TooltipHandler.TipRegion(rect, () => scoreText,
(int)(r.x + r.y * 56167.0));
Widgets.DrawHighlight(rect);
}

if (Widgets.ButtonInvisible(rect))
{
var modCompatWindow = new ModCompatWindow(parent, true, false, null);
modCompatWindow.ScrollToModName(mod.Name);
Find.WindowStack.Add(modCompatWindow);
}

Text.Anchor = TextAnchor.MiddleLeft;
Widgets.Label(r.Right(rect.width + 4f), label);
}
}


[HarmonyPatch(typeof(Page_ModsConfig), nameof(Page_ModsConfig.DoBottomButtons))]
static class PageModsConfigAddButton
Expand Down
Loading