-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdvancedStarmap.cs
More file actions
122 lines (101 loc) · 4.52 KB
/
AdvancedStarmap.cs
File metadata and controls
122 lines (101 loc) · 4.52 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
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
namespace AdvancedStarmap
{
[BepInPlugin("com.brokenmass.plugin.DSP.AdvancedStarmap", "AdvancedStarmap", "1.0.0")]
public class AdvancedStarmap : BaseUnityPlugin
{
internal class StarInfoEntry
{
public GameObject gameObject;
public Text label;
public Text value;
}
Harmony harmony;
static Dictionary<string, StarInfoEntry> starInfos = new Dictionary<string, StarInfoEntry>();
static readonly string BG_PATH = "UI Root/Overlay Canvas/In Game/Planet & Star Details/star-detail-ui/black-bg";
static readonly string LAST_LABEL_PATH = "UI Root/Overlay Canvas/In Game/Planet & Star Details/star-detail-ui/param-group/label (6)";
void Start()
{
harmony = new Harmony("com.brokenmass.plugin.DSP.AdvancedStarmap");
try
{
harmony.PatchAll(typeof(AdvancedStarmap));
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
internal void OnDestroy()
{
// For ScriptEngine hot-reloading
harmony.UnpatchSelf();
foreach (var item in starInfos.Values)
{
Destroy(item.gameObject);
}
var blackBg = GameObject.Find(BG_PATH).GetComponent<RectTransform>();
blackBg.offsetMin -= new Vector2(0f, -20f * starInfos.Count);
}
[HarmonyPrefix, HarmonyPatch(typeof(UIStarDetail), "_OnOpen")]
public static void UIStarDetail__OnOpen_Prefix(UIStarDetail __instance)
{
if (!starInfos.ContainsKey("star-details-min-radius"))
{
AddStarInfo("star-details-min-radius", "Dyson sphere min radius");
}
if (!starInfos.ContainsKey("star-details-max-radius"))
{
AddStarInfo("star-details-max-radius", "Dyson sphere max radius");
}
var minSphereRadius = Mathf.Max(4000f, __instance.star.physicsRadius * 1.5f);
if (__instance.star.type == EStarType.GiantStar)
{
minSphereRadius *= 0.6f;
}
minSphereRadius = Mathf.Ceil(minSphereRadius / 100f) * 100f;
starInfos["star-details-min-radius"].value.text = minSphereRadius.ToString("0");
var maxSphereRadius = Mathf.Round((float)((double)__instance.star.dysonRadius * 40000.0) * 2f / 100f) * 100f;
starInfos["star-details-max-radius"].value.text = maxSphereRadius.ToString("0");
}
public static void AddStarInfo(string id, string labelText)
{
var originalDetailLabel = GameObject.Find(LAST_LABEL_PATH);
if (originalDetailLabel == null)
{
throw new InvalidOperationException("Star detail info base entry is not present");
}
GameObject gameObject = null;
var originalDetailLabelText = originalDetailLabel.GetComponent<Text>();
gameObject = Instantiate(originalDetailLabel, originalDetailLabel.transform.position, Quaternion.identity);
Destroy(gameObject.GetComponentInChildren<Localizer>());
gameObject.name = id;
gameObject.transform.SetParent(originalDetailLabel.transform.parent);
var textComponents = gameObject.GetComponentsInChildren<Text>();
var label = textComponents[0];
var value = textComponents[1];
label.text = labelText;
label.rectTransform.offsetMax = originalDetailLabelText.rectTransform.offsetMax;
label.rectTransform.offsetMin = originalDetailLabelText.rectTransform.offsetMin;
gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
gameObject.transform.localPosition = originalDetailLabel.transform.localPosition + new Vector3(0f, -20f * (1 + starInfos.Count), 0f);
gameObject.transform.right = originalDetailLabel.transform.right;
var blackBg = GameObject.Find(BG_PATH).GetComponent<RectTransform>();
blackBg.offsetMin += new Vector2(0f, -20f);
StarInfoEntry newStarInfoEntry = new StarInfoEntry()
{
gameObject = gameObject,
label = label,
value = value
};
starInfos.Add(id, newStarInfoEntry);
}
}
}