Skip to content

Commit ae2ef7d

Browse files
committed
Make wiki button on protoflux nodes togglable by category and add more localization
1 parent f11d2a4 commit ae2ef7d

5 files changed

Lines changed: 157 additions & 9 deletions

File tree

WikiIntegration/Locale/de.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99
"WikiIntegration.WikiHyperlink.ProtoFlux": "Öffnet den Resonite Wiki Artikel über diese ProtoFlux Node.",
1010

1111
"WikiIntegration.OpenWikiArticleButton.Name": "Öffne Wiki Artikel Link",
12-
"WikiIntegration.OpenWikiArticleButton.Description": "Fügt den Resonite Wiki Artikel Link zu Komponenten in Worker Inspektoren und ProtoFlux Nodes hinzu."
12+
"WikiIntegration.OpenWikiArticleButton.Description": "Fügt den Resonite Wiki Artikel Link zu Komponenten in Worker Inspektoren und ProtoFlux Nodes hinzu.",
13+
14+
"WikiIntegration.Config.Buttons.Name": "Links",
15+
"WikiIntegration.Config.Buttons.ProtoFlux.Description": "Ob der Resonite Wiki Artikel Link auf ProtoFlux Nodes angezeigt werden soll.",
16+
"WikiIntegration.Config.Buttons.Components.Name": "Komponenten",
17+
"WikiIntegration.Config.Buttons.Components.Description": "Ob der Resonite Wiki Artikel Link auf Komponenten in Worker Inspektoren angezeigt werden soll.",
18+
"WikiIntegration.Config.Buttons.ComponentOffset.Name": "Komponenten-Offset",
19+
"WikiIntegration.Config.Buttons.ComponentOffset.Description": "Das Order Offset des Resonite Wiki Artikel Links bei Inspektor-Kopfzeilen. Erlaubter Wert: 0-16 - Höher ist weiter rechts.",
20+
21+
"WikiIntegration.Config.ProtoFluxCategories.Name": "ProtoFlux Kategorien",
22+
"WikiIntegration.ProtoFluxCategoryToggles.Description": "Ob der Resonite Wiki Artikel Link auf ProtoFlux Nodes in der Kategorie {0} angezeigt werden soll."
1323
}
1424
}

WikiIntegration/Locale/en.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
"WikiIntegration.WikiHyperlink.ProtoFlux": "Opens the Resonite Wiki article about this ProtoFlux node.",
1010

1111
"WikiIntegration.OpenWikiArticleButton.Name": "Open Wiki Article Button",
12-
"WikiIntegration.OpenWikiArticleButton.Description": "Adds the Resonite Wiki button to Components in Worker Inspectors, and ProtoFlux nodes."
12+
"WikiIntegration.OpenWikiArticleButton.Description": "Adds the Resonite Wiki button to Components in Worker Inspectors, and ProtoFlux nodes.",
13+
14+
"WikiIntegration.Config.ProtoFluxCategories.Name": "ProtoFlux Categories",
15+
"WikiIntegration.ProtoFluxCategoryToggles.Description": "Whether to show the Resonite Wiki button on ProtoFlux nodes in the category {0}."
1316
}
1417
}

WikiIntegration/OpenWikiArticleButton.cs

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,51 @@
33
using FrooxEngine.ProtoFlux;
44
using FrooxEngine.UIX;
55
using HarmonyLib;
6+
using MonkeyLoader.Components;
7+
using MonkeyLoader.Configuration;
8+
using MonkeyLoader.Events;
69
using MonkeyLoader.Resonite;
10+
using MonkeyLoader.Resonite.Configuration;
11+
using MonkeyLoader.Resonite.Locale;
712
using MonkeyLoader.Resonite.UI;
813
using MonkeyLoader.Resonite.UI.Inspectors;
914
using ProtoFlux.Core;
1015
using System;
1116
using System.Collections.Generic;
17+
using System.IO;
18+
using System.Linq;
1219
using System.Text;
20+
using System.Threading.Tasks;
1321

1422
namespace WikiIntegration
1523
{
1624
/// <summary>
17-
/// This monkey must not be disabled to generate wiki buttons for anyone else in the session.
25+
/// This monkey must not be disabled to generate wiki buttons for everyone else in the session.
1826
/// </summary>
1927
[HarmonyPatchCategory(nameof(OpenWikiArticleButton))]
2028
[HarmonyPatch(typeof(ProtoFluxNodeVisual), nameof(ProtoFluxNodeVisual.GenerateVisual))]
21-
internal sealed class OpenWikiArticleButton : ConfiguredResoniteInspectorMonkey<OpenWikiArticleButton, WikiButtonConfig, BuildInspectorHeaderEvent, Worker>
29+
internal sealed class OpenWikiArticleButton : ConfiguredResoniteInspectorMonkey<OpenWikiArticleButton, WikiButtonConfig, BuildInspectorHeaderEvent, Worker>,
30+
IAsyncEventHandler<FallbackLocaleGenerationEvent>
2231
{
2332
private static readonly Lazy<LocaleString> _componentLocale = new(() => Mod.GetLocaleString("WikiHyperlink.Component"));
2433
private static readonly Lazy<LocaleString> _protoFluxLocale = new(() => Mod.GetLocaleString("WikiHyperlink.ProtoFlux"));
25-
34+
private static ProtoFluxCategoryConfig _categoryConfig = null!;
2635
public override int Priority => HarmonyLib.Priority.HigherThanNormal;
36+
2737
private static LocaleString ComponentLocale => _componentLocale.Value;
38+
2839
private static LocaleString ProtoFluxLocale => _protoFluxLocale.Value;
2940

41+
public Task Handle(FallbackLocaleGenerationEvent eventData)
42+
{
43+
var toggleDescription = eventData.GetMessage(Mod.GetLocaleKey("ProtoFluxCategoryToggles.Description"));
44+
45+
var protoFluxNodesRoot = WorkerInitializer.ComponentLibrary.GetSubcategory(ProtoFluxCategoryConfig.ProtoFluxPath);
46+
CreateConfigKeyNames(eventData, toggleDescription, protoFluxNodesRoot, ProtoFluxCategoryConfig.ProtoFluxPath);
47+
48+
return Task.CompletedTask;
49+
}
50+
3051
protected override void Handle(BuildInspectorHeaderEvent eventData)
3152
{
3253
var ui = eventData.UI;
@@ -46,6 +67,24 @@ protected override void Handle(BuildInspectorHeaderEvent eventData)
4667
ui.PopStyle();
4768
}
4869

70+
protected override bool OnEngineReady()
71+
{
72+
_categoryConfig = Config.LoadSection<ProtoFluxCategoryConfig>();
73+
_categoryConfig.Initialize();
74+
75+
Mod.RegisterEventHandler<FallbackLocaleGenerationEvent>(this);
76+
77+
return base.OnEngineReady();
78+
}
79+
80+
protected override bool OnShutdown(bool applicationExiting)
81+
{
82+
if (!applicationExiting)
83+
Mod.UnregisterEventHandler<FallbackLocaleGenerationEvent>(this);
84+
85+
return base.OnShutdown(applicationExiting);
86+
}
87+
4988
private static void AddHyperlink(Slot slot, Worker worker)
5089
{
5190
string wikiPage;
@@ -72,7 +111,7 @@ private static void AddHyperlink(Slot slot, Worker worker)
72111
reason = ComponentLocale;
73112
var workerName = worker.WorkerType.Name;
74113

75-
// Don't need to remove the `1 on generics - they redirect
114+
// Don't need to remove the `1 on generics - they redirect and may actually be different
76115
wikiPage = $"Component:{workerName}";
77116
}
78117

@@ -90,17 +129,38 @@ private static void Postfix(ProtoFluxNodeVisual __instance, ProtoFluxNode node)
90129

91130
var buttonArea = ui.Panel();
92131
ui.IgnoreLayout();
132+
ConfigSection.ProtoFlux.DriveFromVariable(buttonArea.Slot.ActiveSelf_Field);
133+
93134
buttonArea.AnchorMin.Value = new(1, 0);
94135
buttonArea.AnchorMax.Value = new(1, 0);
95136
buttonArea.OffsetMin.Value = new(-12, 2);
96137
buttonArea.OffsetMax.Value = new(-2, 12);
97138

139+
// creates texture for every button
98140
var button = ui.Image(OfficialAssets.Graphics.Badges.Mentor);
99141
button.Slot.AttachComponent<Button>().WithTooltip(ProtoFluxLocale);
100142

101143
AddHyperlink(button.Slot, node);
102144

103-
ConfigSection.ProtoFlux.DriveFromVariable(button.Slot.ActiveSelf_Field);
145+
if (_categoryConfig[node.GetType()] is ConfigKeySessionShare<bool> categoryShare)
146+
categoryShare.DriveFromVariable(button.Slot.ActiveSelf_Field);
147+
}
148+
149+
private void CreateConfigKeyNames(FallbackLocaleGenerationEvent eventData, string descriptionFormat, CategoryNode<Type> category, string path)
150+
{
151+
if (category.Elements.Any())
152+
{
153+
var trimmedPath = path.Replace(ProtoFluxCategoryConfig.ProtoFluxPath, "");
154+
155+
var id = $"{_categoryConfig.FullId}.{ProtoFluxCategoryConfig.GetToggleId(path)}";
156+
var description = string.Format(descriptionFormat, trimmedPath);
157+
158+
eventData.AddMessage($"{id}.Name", trimmedPath);
159+
eventData.AddMessage($"{id}.Description", description);
160+
}
161+
162+
foreach (var subcategory in category.Subcategories)
163+
CreateConfigKeyNames(eventData, descriptionFormat, subcategory, $"{path}/{subcategory.Name}");
104164
}
105165
}
106166
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using FrooxEngine;
2+
using MonkeyLoader.Components;
3+
using MonkeyLoader.Configuration;
4+
using MonkeyLoader.Resonite.Configuration;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.IO;
8+
using System.Linq;
9+
using System.Reflection;
10+
using System.Text;
11+
12+
namespace WikiIntegration
13+
{
14+
internal sealed class ProtoFluxCategoryConfig : ExpandoConfigSection
15+
{
16+
internal const string ProtoFluxPath = "ProtoFlux/Runtimes/Execution/Nodes";
17+
18+
private static readonly Dictionary<string, ConfigKeySessionShare<bool>> _sessionSharesByCategory = new();
19+
20+
public override string Description => "Contains settings for the Resonite Wiki buttons on specific categories of ProtoFlux nodes.";
21+
public override string Id => "ProtoFluxCategories";
22+
23+
public ConfigKeySessionShare<bool>? this[string category]
24+
{
25+
get
26+
{
27+
_sessionSharesByCategory.TryGetValue(category, out var sessionShare);
28+
return sessionShare;
29+
}
30+
}
31+
32+
public ConfigKeySessionShare<bool>? this[Type worker]
33+
{
34+
get
35+
{
36+
if (worker.GetCustomAttribute<CategoryAttribute>() is not CategoryAttribute category)
37+
return null;
38+
39+
foreach (var path in category.Paths)
40+
{
41+
if (this[path] is ConfigKeySessionShare<bool> sessionShare)
42+
return sessionShare;
43+
}
44+
45+
return null;
46+
}
47+
}
48+
49+
public override Version Version { get; } = new(1, 0, 0);
50+
51+
public static string GetToggleId(string categoryPath)
52+
=> $"Category-{categoryPath.Replace('/', '_')}";
53+
54+
internal void Initialize()
55+
{
56+
var protoFluxNodesRoot = WorkerInitializer.ComponentLibrary.GetSubcategory(ProtoFluxPath);
57+
CreateConfigKeys(protoFluxNodesRoot, ProtoFluxPath);
58+
}
59+
60+
private void CreateConfigKeys(CategoryNode<Type> category, string path)
61+
{
62+
if (category.Elements.Any())
63+
{
64+
IEntity<IDefiningConfigKey<bool>> categoryKey = CreateDefiningKey(new ConfigKey<bool>(GetToggleId(path)), $"Whether to show the Resonite Wiki button on ProtoFlux nodes in the category {path}.", () => true);
65+
var sessionShare = new ConfigKeySessionShare<bool>(true);
66+
categoryKey.Components.Add(sessionShare);
67+
_sessionSharesByCategory.Add(path, sessionShare);
68+
}
69+
70+
foreach (var subcategory in category.Subcategories)
71+
CreateConfigKeys(subcategory, $"{path}/{subcategory.Name}");
72+
}
73+
}
74+
}

WikiIntegration/WikiButtonConfig.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ namespace WikiIntegration
99
{
1010
internal sealed class WikiButtonConfig : ConfigSection
1111
{
12-
private static readonly DefiningConfigKey<bool> _components = new("Components", "Whether to add the Wiki button to Components in Worker Inspectors.", () => true)
12+
private static readonly DefiningConfigKey<bool> _components = new("Components", "Whether to show the Wiki button on Components in Worker Inspectors.", () => true)
1313
{
1414
new ConfigKeySessionShare<bool>(true)
1515
};
1616

17-
private static readonly DefiningConfigKey<bool> _protoFlux = new("ProtoFlux", "Whether to add the Wiki button to ProtoFlux nodes.", () => true)
17+
private static readonly DefiningConfigKey<bool> _protoFlux = new("ProtoFlux", "Whether to show the Wiki button on ProtoFlux nodes.", () => true)
1818
{
1919
new ConfigKeySessionShare<bool>(true)
2020
};
@@ -33,6 +33,7 @@ internal sealed class WikiButtonConfig : ConfigSection
3333
public ConfigKeySessionShare<bool> Components => _components.Components.Get<ConfigKeySessionShare<bool>>();
3434

3535
public override string Description => "Contains settings for the Resonite Wiki buttons on components and ProtoFlux nodes.";
36+
3637
public override string Id => "Buttons";
3738

3839
public ConfigKeySessionShare<bool> ProtoFlux => _protoFlux.Components.Get<ConfigKeySessionShare<bool>>();

0 commit comments

Comments
 (0)