-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.cs
70 lines (58 loc) · 2.33 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
using BepInEx;
using HarmonyLib;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using UnityEngine;
namespace Vehicle_Map_Marker
{
[BepInPlugin("de.schlangguru.valheim.vehicle_map_marker", "Vehicle Map Marker", "1.2.0")]
[BepInProcess("valheim.exe")]
public class Main : BaseUnityPlugin
{
private Harmony _harmony = null;
public static Configuration ModConfig;
public static List<MarkerType> MarkerTypes = new List<MarkerType>();
public static Dictionary<ZDOID, MarkerData> Markers = new Dictionary<ZDOID, MarkerData>();
void Awake()
{
ModConfig = new Configuration(Config);
Sprite shipMarkerSprite = LoadSprite("mapicon_anchor.png");
Sprite cartMarkerSprite = LoadSprite("mapicon_cart.png");
MarkerTypes.Add(new MarkerType("Raft", shipMarkerSprite, "$ship_raft", ModConfig.ShowShips));
MarkerTypes.Add(new MarkerType("Karve", shipMarkerSprite, "$ship_karve", ModConfig.ShowShips));
MarkerTypes.Add(new MarkerType("VikingShip", shipMarkerSprite, "$ship_longship", ModConfig.ShowShips));
MarkerTypes.Add(new MarkerType("Cart", cartMarkerSprite, "$tool_cart", ModConfig.ShowCarts));
_harmony = new Harmony("Vehicle_Map_Marker");
_harmony.PatchAll(typeof(ZDOMan_Patch));
_harmony.PatchAll(typeof(Minimap_Patch));
}
public static Sprite LoadSprite(string name)
{
Stream manifestResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Vehicle_Map_Marker.Resources." + name);
byte[] buffer = new byte[manifestResourceStream.Length];
manifestResourceStream.Read(buffer, 0, (int)manifestResourceStream.Length);
// Create a texture. Texture size does not matter, since LoadImage will replace with with incoming image size.
Texture2D texture = new Texture2D(2, 2);
texture.LoadImage(buffer);
texture.Apply();
return Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.0f, 0.0f), 50f);
}
public static void DestroyMarkers()
{
foreach (MarkerData data in Markers.Values)
{
if (data.Marker != null)
{
UnityEngine.Object.Destroy(data.Marker);
}
}
Markers.Clear();
}
private void OnDestroy()
{
this._harmony?.UnpatchSelf();
DestroyMarkers();
}
}
}