-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.cs
158 lines (144 loc) · 6.52 KB
/
Main.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using LethalLib.Modules;
using UnityEngine.SceneManagement;
using BepInEx.Configuration;
using Dissonance;
using static UnityEngine.UI.Image;
using Unity.Netcode;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System;
using Steamworks.Ugc;
using NetworkPrefabs = LethalLib.Modules.NetworkPrefabs;
namespace BuyableShotgun
{
[BepInDependency("evaisa.lethallib", "0.13.2")]
[BepInPlugin(modGUID, modName, modVersion)]
public class BuyableShotgun : BaseUnityPlugin
{
private const string modGUID = "MegaPiggy.BuyableShotgun";
private const string modName = "Buyable Shotgun";
private const string modVersion = "1.1.0";
private readonly Harmony harmony = new Harmony(modGUID);
private static BuyableShotgun Instance;
private static ManualLogSource LoggerInstance => Instance.Logger;
public static List<Item> AllItems => Resources.FindObjectsOfTypeAll<Item>().Concat(UnityEngine.Object.FindObjectsByType<Item>(FindObjectsInactive.Include, FindObjectsSortMode.InstanceID)).ToList();
public static Item Shotgun => AllItems.FirstOrDefault(item => item.name.Equals("Shotgun"));
public static Item ShotgunClone { get; private set; }
public static GameObject ShotgunObjectClone { get; private set; }
private static ConfigEntry<int> ShotgunPriceConfig;
public static int ShotgunPrice => ShotgunPriceConfig.Value;
private void Awake()
{
if (Instance == null)
{
DontDestroyOnLoad(this);
Instance = this;
}
harmony.PatchAll();
ShotgunPriceConfig = Config.Bind("Prices", "ShotgunPrice", 700, "Credits needed to buy shotgun");
SceneManager.sceneLoaded += OnSceneLoaded;
ShotgunClone = MakeNonScrap(ShotgunPrice);
AddToShop();
Logger.LogInfo($"Plugin {modName} is loaded with version {modVersion}!");
}
private static Item MakeNonScrap(int price)
{
Item nonScrap = ScriptableObject.CreateInstance<Item>();
DontDestroyOnLoad(nonScrap);
nonScrap.name = "Error";
nonScrap.itemName = "Error";
nonScrap.itemId = 6624;
nonScrap.isScrap = false;
nonScrap.creditsWorth = price;
nonScrap.canBeGrabbedBeforeGameStart = true;
nonScrap.automaticallySetUsingPower = false;
nonScrap.batteryUsage = 300;
nonScrap.canBeInspected = false;
nonScrap.isDefensiveWeapon = true;
nonScrap.saveItemVariable = true;
nonScrap.syncGrabFunction = false;
nonScrap.twoHandedAnimation = true;
nonScrap.verticalOffset = 0.25f;
var prefab = LethalLib.Modules.NetworkPrefabs.CreateNetworkPrefab("Cube");
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.SetParent(prefab.transform, false);
cube.GetComponent<MeshRenderer>().sharedMaterial.shader = Shader.Find("HDRP/Lit");
prefab.AddComponent<BoxCollider>().size = Vector3.one * 2;
prefab.AddComponent<AudioSource>();
var prop = prefab.AddComponent<PhysicsProp>();
prop.itemProperties = nonScrap;
prop.grabbable = true;
nonScrap.spawnPrefab = prefab;
prefab.tag = "PhysicsProp";
prefab.layer = LayerMask.NameToLayer("Props");
cube.layer = LayerMask.NameToLayer("Props");
try
{
GameObject scanNode = GameObject.Instantiate<GameObject>(Items.scanNodePrefab, prefab.transform);
scanNode.name = "ScanNode";
scanNode.transform.localPosition = new Vector3(0f, 0f, 0f);
scanNode.transform.localScale *= 2;
ScanNodeProperties properties = scanNode.GetComponent<ScanNodeProperties>();
properties.nodeType = 1;
properties.headerText = "Error";
properties.subText = $"A mod is incompatible with {modName}";
}
catch (Exception e)
{
LoggerInstance.LogError(e.ToString());
}
prefab.transform.localScale = Vector3.one / 2;
return nonScrap;
}
private static GameObject CloneNonScrap(Item original, Item clone, int price)
{
var prefab = NetworkPrefabs.CloneNetworkPrefab(original.spawnPrefab);
DontDestroyOnLoad(prefab);
CopyFields(original, clone);
prefab.GetComponent<GrabbableObject>().itemProperties = clone;
clone.spawnPrefab = prefab;
clone.name = "Buyable" + original.name;
clone.creditsWorth = price;
return prefab;
}
public static void CopyFields(Item source, Item destination)
{
FieldInfo[] fields = typeof(Item).GetFields();
foreach (FieldInfo field in fields)
{
field.SetValue(destination, field.GetValue(source));
}
}
private static Dictionary<string, TerminalNode> infoNodes = new Dictionary<string, TerminalNode>();
private static TerminalNode CreateInfoNode(string name, string description)
{
if (infoNodes.ContainsKey(name)) return infoNodes[name];
TerminalNode node = ScriptableObject.CreateInstance<TerminalNode>();
DontDestroyOnLoad(node);
node.clearPreviousText = true;
node.name = name + "InfoNode";
node.displayText = description + "\n\n";
infoNodes.Add(name, node);
return node;
}
private static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
LoggerInstance.LogInfo("Scene \"" + scene.name + "\" loaded with " + mode + " mode.");
if (Shotgun == null) return;
if (ShotgunObjectClone != null) return;
ShotgunObjectClone = CloneNonScrap(Shotgun, ShotgunClone, ShotgunPrice);
}
private static void AddToShop()
{
Items.RegisterShopItem(ShotgunClone, price: ShotgunPrice, itemInfo: CreateInfoNode("Shotgun", "Nutcracker's shotgun. Can hold 2 shells. Recommended to keep safety on while not using or it might shoot randomly."));
LoggerInstance.LogInfo($"Shotgun added to Shop for {ShotgunPrice} credits");
}
}
}