-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCourierPanelAddRouteMod.cs
116 lines (100 loc) · 4.33 KB
/
CourierPanelAddRouteMod.cs
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
using GoodCompany;
using GoodCompany.GUI;
using UnityEngine;
using HarmonyLib;
using System;
using System.Linq;
using UnityEngine.UI;
using TMPro;
namespace BetterCouriers
{
public static class CourierPanelAddRouteMod
{
public static void AddRouteButton(CourierPalletPanel panel)
{
var session = panel.GetField<SessionManager>("_session");
GameObject configure = panel.gameObject.transform.GetChild(2).gameObject;
GameObject configure_button = configure.transform.GetChild(0).gameObject;
configure_button.transform.localPosition = new Vector3(-74, 0, 0);
GameObject add_route = GameObject.Instantiate(configure_button);
add_route.transform.parent = configure.transform;
add_route.transform.localPosition = new Vector3(130, 0, 0);
add_route.transform.localScale = Vector3.one;
Image img = add_route.GetComponent<Image>();
img.rectTransform.sizeDelta = new Vector2(140, img.rectTransform.sizeDelta.y);
GUIButton button = add_route.GetComponent<GUIButton>();
button.OnClick.AddListener(() => CreateRouteWithItems(panel, session));
GameObject label = add_route.transform.GetChild(0).gameObject;
label.transform.DestroyAllChildren();
label.transform.localPosition = new Vector3(-16, 0, 0);
TextMeshProUGUI text = label.GetComponent<TextMeshProUGUI>();
text.DefferedSetText("Add route");
}
private static void CreateRouteWithItems(CourierPalletPanel panel, SessionManager session)
{
var palletControl = panel.GetField<CourierPalletPanelControl>("_control");
var itemRules = panel.GetField<GUIList<ItemRuleListItem>>("_rulesList");
uint palletId = palletControl.GetField<CourierPalletModel>("_currentPallet").ID;
ItemType[] itemTypes = itemRules
.ToArray()
.Select(ruleListItem => ruleListItem.ItemType)
.ToArray()
.SliceArray(0, 5);
var routesControl = OpenRouteOverview(session);
var reqId = session.MsgSender.Requests.SendAddRouteRequest(
new ResponseReceiver.Receiver(
(ushort requestId, byte[] content, int offset) =>
{
uint routeId = BitConverter.ToUInt32(content, content.Length - 4);
routesControl.InvokePrivateMethod(
"OnAddRouteRequestResponse",
new object[] { requestId, content, offset }
);
routesControl.AddPalletToRoute(routeId, palletId);
routesControl.AddItemsToRoute(routeId, itemTypes);
}
)
);
routesControl.SetField("_lastAddRouteRequest", reqId);
}
private static CourierRoutesPanelControl OpenRouteOverview(SessionManager session)
{
var routesPanel = session.WindowManager.OpenFullscreenPanel(
session.GUIElements.Panels.Get<CourierRoutesPanel>(0)
);
return new CourierRoutesPanelControl(routesPanel, session);
}
private static void AddPalletToRoute(
this CourierRoutesPanelControl control,
uint routeId,
uint palletId
)
{
control.InvokePrivateMethod(
"GoodCompany.GUI.CourierRoutesPanel.IControl.AddPalletToRoute",
new object[] { routeId, palletId }
);
}
private static void AddItemsToRoute(
this CourierRoutesPanelControl control,
uint routeId,
ItemType[] itemTypes
)
{
var addItemMethod = control.GetPrivateMethod(
"GoodCompany.GUI.CourierRoutesPanel.IControl.AddItemToRoute"
);
itemTypes.Do(
itemType => addItemMethod.Invoke(control, new object[] { routeId, itemType })
);
}
}
[HarmonyPatch(typeof(CourierPalletPanel), "Initialize")]
static class CourierPalletPanel_Initialize_Patch
{
static void Postfix(ref CourierPalletPanel __instance)
{
CourierPanelAddRouteMod.AddRouteButton(__instance);
}
}
}