forked from XDelta/CustomColorPicker
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCustomProtofluxBrowser.cs
More file actions
124 lines (110 loc) · 5.99 KB
/
CustomProtofluxBrowser.cs
File metadata and controls
124 lines (110 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.Reflection;
using Elements.Core;
using FrooxEngine;
using FrooxEngine.ProtoFlux;
using HarmonyLib;
using ResoniteModLoader;
using SpecialItemsLib;
using static OfficialAssets.Graphics.Badges.MMC;
namespace CustomProtofluxBrowser
{
public class CustomProtofluxBrowser : ResoniteMod
{
public override string Name => "CustomProtofluxBrowser";
public override string Author => "AlexW-578";
public override string Version => "2.1.3";
public override string Link => "https://github.com/AlexW-578/CustomProtofluxBrowser";
private static ModConfiguration Config;
[AutoRegisterConfigKey] private static readonly ModConfigurationKey<bool> Enabled = new ModConfigurationKey<bool>("Enabled", "Enables the mod", () => true);
[AutoRegisterConfigKey] private static readonly ModConfigurationKey<bool> UserScale = new ModConfigurationKey<bool>("User scale", "Adjust browser scale to user scale", () => true);
[AutoRegisterConfigKey] private static readonly ModConfigurationKey<float> Scale = new ModConfigurationKey<float>("Scale", "Browser size or scale relative to the user when user scale is on", () => 1f);
[AutoRegisterConfigKey] private static readonly ModConfigurationKey<bool> CherryPick = new ModConfigurationKey<bool>("CherryPick", "Enable CherryPick or ComponentSelectorAdditions compatibility", () => false);
private static string PROTOFLUX_BROWSER_TAG
{
get { return "custom_protoflux_browser"; }
}
private static CustomSpecialItem protofluxBrowserObject;
public override void OnEngineInit()
{
Config = GetConfiguration();
Config.Save(true);
Harmony harmony = new Harmony("co.uk.AlexW-578.CustomProtofluxBrowser");
protofluxBrowserObject = SpecialItemsLib.SpecialItemsLib.RegisterItem(PROTOFLUX_BROWSER_TAG, "Protoflux Browser");
harmony.PatchAll();
}
[HarmonyPatch(typeof(SlotHelper), nameof(SlotHelper.GenerateTags), new Type[] { typeof(Slot), typeof(HashSet<string>) })]
class SlotHelper_GenerateTags_Patch
{
static MethodInfo NodeTypeSelectedMethod = AccessTools.Method(typeof(ProtoFluxTool), "OnNodeTypeSelected");
static void Postfix(Slot slot, HashSet<string> tags)
{
var comp = slot.GetComponent<ComponentSelector>();
if (comp != null && comp.ComponentSelected.Target?.Method == NodeTypeSelectedMethod)
{
tags.Add(PROTOFLUX_BROWSER_TAG);
}
}
}
[HarmonyPatch(typeof(ProtoFluxTool), "OpenNodeBrowser")]
class ResoniteProtofluxBrowser_Patch
{
static bool Prefix(ProtoFluxTool __instance)
{
if (!Config.GetValue(Enabled)) return true;
if (protofluxBrowserObject.Uri == null) return true;
Slot slot = __instance.LocalUserSpace.AddSlot("NodeMenu");
slot.StartTask(async delegate ()
{
await slot.LoadObjectAsync(protofluxBrowserObject.Uri);
InventoryItem component = slot.GetComponent<InventoryItem>();
Slot slot_two = ((component != null) ? component.Unpack() : null) ?? slot;
slot_two.PositionInFrontOfUser(float3.Backward);
if (Config.GetValue(UserScale))
{
slot_two.GlobalScale = slot_two.World.LocalUser.Root.Slot.GlobalScale * Config.GetValue(Scale);
}
else
{
slot_two.GlobalScale = float3.One * Config.GetValue(Scale);
}
if (Config.GetValue(CherryPick))
{
try
{
var prsSlot = slot_two.FindChild("Cherry Node Browser - Parent");
var cherrySlot = prsSlot != null ? prsSlot.AddSlot("Cherry Node Browser") :
slot_two.AddSlot("Cherry Node Browser - Parent").AddSlot("Cherry Node Browser");
var currentSelecter = slot_two.GetComponentInChildren<ComponentSelector>();
ComponentSelector componentSelector = cherrySlot.AttachComponent<ComponentSelector>();
componentSelector.SetupUI("ProtoFlux.UI.NodeBrowser.Title".AsLocaleKey(), ComponentSelector.DEFAULT_SIZE);
componentSelector.BuildUI(ProtoFluxHelper.PROTOFLUX_ROOT, doNotGenerateBack: true);
componentSelector.ComponentSelected.Target = currentSelecter.ComponentSelected.Target;
componentSelector.ComponentFilter.Target = currentSelecter.ComponentFilter.Target;
componentSelector.GenericArgumentPrefiller.Target = currentSelecter.GenericArgumentPrefiller.Target;
cherrySlot.PersistentSelf = false;
List<Grabbable> components = new List<Grabbable>();
cherrySlot.GetComponents(components);
foreach (Grabbable grabbable in components)
{
grabbable.Enabled = false;
}
if(cherrySlot.GetComponent<Grabbable>() == null)
{
cherrySlot.AttachComponent<Grabbable>();
}
}
catch (Exception e)
{
Debug("CherryPick compatibility failed: ");
UniLog.Error(e.ToString());
}
}
});
__instance.ActiveHandler?.CloseContextMenu();
return false;
}
}
}
}