Repository navigation
Expand file tree
/
Copy pathCompanionMod.cs
More file actions
125 lines (98 loc) · 4.24 KB
/
Copy pathCompanionMod.cs
File metadata and controls
125 lines (98 loc) · 4.24 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
125
using MelonLoader;
using StellarModdingToolkit.StellarDriveIntegration;
using Ship.Interface.Settings;
using UnityEngine;
using Ship.Interface.Model.Parts;
using Ship.Parts.Common;
using System.Linq;
using StellarModdingToolkit.Assets;
using System;
using Object = UnityEngine.Object;
using Harmony;
namespace Companion;
public class CompanionMod : MelonMod
{
public static MelonPreferences_Category PreferenceCategory { get; set; } = MelonPreferences.CreateCategory("CompanionMod");
public static MelonPreferences_Entry<ushort> PartID { get; set; } = PreferenceCategory.CreateEntry<ushort>("PartID", 0b_10000000_00000000);
public static AssetLoader? AssetLoader { get; set; }
public static EventHandler? OnCreatedAssetLoader;
struct Keys
{
public const string CompanionCubeModel = "cc-model";
public const string CompanionCubeAlbedo = "cc-albedo";
public const string CompanionCubeNormals = "cc-normal";
public const string CompanionCubeEmission = "cc-emission";
}
public override void OnLateInitializeMelon()
{
AssetLoader = new(MelonAssembly.Assembly, LoggerInstance,
[
Keys.CompanionCubeModel,
Keys.CompanionCubeAlbedo,
Keys.CompanionCubeNormals,
Keys.CompanionCubeEmission
]);
OnCreatedAssetLoader?.Invoke(this, EventArgs.Empty);
}
public override void OnSceneWasLoaded(int buildIndex, string sceneName)
{
if (buildIndex != 1) return;
if (IntegrationUtilities.IsPartIDTaken(PartID.Value))
{
LoggerInstance.Warning($"Part ID({PartID.Value}) is already taken!");
return;
}
PartSettings part = ScriptableObject.CreateInstance<PartSettings>();
part.fullLabel = "Comapnion";
part.name = "Companion Cube";
part.description = "Woman's best friend! (sorry dogs)";
part.internalStateType = Ship.Interface.Model.Parts.StateTypes.PartInternalStateType.None;
part.size = Vector3.one;
part.thumbnailTex = Texture2D.blackTexture;
part.snappingStyle = Ship.Interface.Model.SnappingStyle.PreciseOnFloor;
part.id = PartID.Value;
part.buildingCost = [];
part.part = CreateCube();
IntegrationUtilities.AddPart(part);
}
public override void OnSceneWasUnloaded(int buildIndex, string sceneName)
{
if (buildIndex != 1) return;
foreach (var item in Object.FindObjectsOfType<PartSettings>().Where(p => p.id == PartID.Value).ToArray())
{
Object.Destroy(item);
}
}
private GameObject CreateCube()
{
float size = 0.8f;
Vector3 scale = Vector3.one * size;
Vector3 offset = Vector3.up * (scale.y / 2);
GameObject partObject = new("Cube");
var bounds = partObject.AddComponent<ShipPartBounds>();
bounds.bounds = scale;
bounds.center = offset;
GameObject visualContainer = new("Visuals");
visualContainer.AddComponent<DefaultShipPartVisuals>();
visualContainer.transform.parent = partObject.transform;
GameObject cube = new();
Shader shader = Shader.Find("Shader Graphs/PlanetObjectDefaultLighting");
Material material = new(shader);
for (int i = 0; i <= 6; i++)
{
foreach (var p in material.GetPropertyNames((MaterialPropertyType)i)) LoggerInstance.Msg(p);
}
cube.AddComponent<MeshFilter>().mesh = AssetLoader.GetAsset<Mesh>(Keys.CompanionCubeModel);
cube.AddComponent<MeshRenderer>().material = material;
material.SetTexture("_AlbedoTex", AssetLoader.GetAsset<Texture2D>(Keys.CompanionCubeAlbedo));
material.SetTexture("_EmissionTex", AssetLoader.GetAsset<Texture2D>(Keys.CompanionCubeEmission));
material.SetTexture("_NormalMap", AssetLoader.GetAsset<Texture2D>(Keys.CompanionCubeNormals));
material.SetColor("_EmissionColor", new Color(174 / 255f, 77/255f, 129/255f) * 1.5f);
cube.transform.parent = visualContainer.transform;
cube.transform.localScale = scale * 50;
cube.transform.localPosition = offset;
cube.transform.localRotation = Quaternion.Euler(-90, 0, 0);
partObject.SetActive(false);
return partObject;
}
}