-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.cs
212 lines (176 loc) · 10.1 KB
/
Plugin.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System;
using System.IO;
using System.Linq;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using JetBrains.Annotations;
using LocalizationManager;
using LocationManager;
using ServerSync;
using UnityEngine;
namespace LocationManagerModTemplate
{
[BepInPlugin(ModGUID, ModName, ModVersion)]
public class LocationManagerModTemplatePlugin : BaseUnityPlugin
{
internal const string ModName = "LocationManagerModTemplate";
internal const string ModVersion = "1.0.0";
internal const string Author = "{azumatt}";
private const string ModGUID = Author + "." + ModName;
private static string ConfigFileName = ModGUID + ".cfg";
private static string ConfigFileFullPath = Paths.ConfigPath + Path.DirectorySeparatorChar + ConfigFileName;
internal static string ConnectionError = "";
private readonly Harmony _harmony = new(ModGUID);
public static readonly ManualLogSource LocationManagerModTemplateLogger = BepInEx.Logging.Logger.CreateLogSource(ModName);
private static readonly ConfigSync ConfigSync = new(ModGUID) { DisplayName = ModName, CurrentVersion = ModVersion, MinimumRequiredVersion = ModVersion };
public Texture2D tex = null!;
// Use only if you need them
//private Sprite mySprite = null!;
//private SpriteRenderer sr = null!;
public enum Toggle
{
On = 1,
Off = 0
}
private void Awake()
{
// Uncomment the line below to use the LocalizationManager for localizing your mod.
//Localizer.Load(); // Use this to initialize the LocalizationManager (for more information on LocalizationManager, see the LocalizationManager documentation https://github.com/blaxxun-boop/LocalizationManager#example-project).
_serverConfigLocked = config("1 - General", "Lock Configuration", Toggle.On, "If on, the configuration is locked and can be changed by server admins only.");
_ = ConfigSync.AddLockingConfigEntry(_serverConfigLocked);
#region Location Notes
// MapIcon Sets the map icon for the location.
// ShowMapIcon When to show the map icon of the location. Requires an icon to be set. Use "Never" to not show a map icon for the location. Use "Always" to always show a map icon for the location. Use "Explored" to start showing a map icon for the location as soon as a player has explored the area.
// MapIconSprite Sets the map icon for the location.
// CanSpawn Can the location spawn at all.
// SpawnArea If the location should spawn more towards the edge of the biome or towards the center. Use "Edge" to make it spawn towards the edge. Use "Median" to make it spawn towards the center. Use "Everything" if it doesn't matter.</para>
// Prioritize If set to true, this location will be prioritized over other locations, if they would spawn in the same area.
// PreferCenter If set to true, Valheim will try to spawn your location as close to the center of the map as possible.
// Rotation How to rotate the location. Use "Fixed" to use the rotation of the prefab. Use "Random" to randomize the rotation. Use "Slope" to rotate the location along a possible slope.
// HeightDelta The minimum and maximum height difference of the terrain below the location.
// SnapToWater If the location should spawn near water.
// ForestThreshold If the location should spawn in a forest. Everything above 1.15 is considered a forest by Valheim. 2.19 is considered a thick forest by Valheim.
// Biome
// SpawnDistance Minimum and maximum range from the center of the map for the location.
// SpawnAltitude Minimum and maximum altitude for the location.
// MinimumDistanceFromGroup Locations in the same group will keep at least this much distance between each other.
// GroupName The name of the group of the location, used by the minimum distance from group setting.
// Count Maximum number of locations to spawn in. Does not mean that this many locations will spawn. But Valheim will try its best to spawn this many, if there is space.
// Unique If set to true, all other locations will be deleted, once the first one has been discovered by a player.
#endregion
_ = new LocationManager.Location("guildfabs", "GuildAltarSceneFab")
{
MapIcon = "portalicon.png",
ShowMapIcon = ShowIcon.Explored,
MapIconSprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f),
100.0f),
CanSpawn = true,
SpawnArea = Heightmap.BiomeArea.Everything,
Prioritize = true,
PreferCenter = true,
Rotation = Rotation.Slope,
HeightDelta = new Range(0, 2),
SnapToWater = false,
ForestThreshold = new Range(0, 2.19f),
Biome = Heightmap.Biome.Meadows,
SpawnDistance = new Range(500, 1500),
SpawnAltitude = new Range(10, 100),
MinimumDistanceFromGroup = 100,
GroupName = "groupName",
Count = 15,
Unique = true
};
LocationManager.Location location = new("krumpaclocations", "WaterPit1")
{
MapIcon = "K_Church_Ruin01.png",
ShowMapIcon = ShowIcon.Always,
Biome = Heightmap.Biome.Meadows,
SpawnDistance = new Range(100, 1500),
SpawnAltitude = new Range(5, 150),
MinimumDistanceFromGroup = 100,
Count = 15
};
// If your location has creature spawners, you can configure the creature they spawn like this.
location.CreatureSpawner.Add("Spawner_1", "Neck");
location.CreatureSpawner.Add("Spawner_2", "Troll");
location.CreatureSpawner.Add("Spawner_3", "Greydwarf");
location.CreatureSpawner.Add("Spawner_4", "Neck");
location.CreatureSpawner.Add("Spawner_5", "Troll");
location.CreatureSpawner.Add("Spawner_6", "Greydwarf");
SetupWatcher();
}
private void OnDestroy()
{
Config.Save();
}
private void SetupWatcher()
{
FileSystemWatcher watcher = new(Paths.ConfigPath, ConfigFileName);
watcher.Changed += ReadConfigValues;
watcher.Created += ReadConfigValues;
watcher.Renamed += ReadConfigValues;
watcher.IncludeSubdirectories = true;
watcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
watcher.EnableRaisingEvents = true;
}
private void ReadConfigValues(object sender, FileSystemEventArgs e)
{
if (!File.Exists(ConfigFileFullPath)) return;
try
{
LocationManagerModTemplateLogger.LogDebug("ReadConfigValues called");
Config.Reload();
}
catch
{
LocationManagerModTemplateLogger.LogError($"There was an issue loading your {ConfigFileName}");
LocationManagerModTemplateLogger.LogError("Please check your config entries for spelling and format!");
}
}
#region ConfigOptions
private static ConfigEntry<Toggle> _serverConfigLocked = null!;
private ConfigEntry<T> config<T>(string group, string name, T value, ConfigDescription description, bool synchronizedSetting = true)
{
ConfigDescription extendedDescription = new(description.Description + (synchronizedSetting ? " [Synced with Server]" : " [Not Synced with Server]"), description.AcceptableValues, description.Tags);
ConfigEntry<T> configEntry = Config.Bind(group, name, value, extendedDescription);
//var configEntry = Config.Bind(group, name, value, description);
SyncedConfigEntry<T> syncedConfigEntry = ConfigSync.AddConfigEntry(configEntry);
syncedConfigEntry.SynchronizedConfig = synchronizedSetting;
return configEntry;
}
private ConfigEntry<T> config<T>(string group, string name, T value, string description, bool synchronizedSetting = true)
{
return config(group, name, value, new ConfigDescription(description), synchronizedSetting);
}
private class ConfigurationManagerAttributes
{
[UsedImplicitly] public int? Order = null!;
[UsedImplicitly] public bool? Browsable = null!;
[UsedImplicitly] public string? Category = null!;
[UsedImplicitly] public Action<ConfigEntryBase>? CustomDrawer = null!;
}
class AcceptableShortcuts : AcceptableValueBase // Used for KeyboardShortcut Configs
{
public AcceptableShortcuts() : base(typeof(KeyboardShortcut))
{
}
public override object Clamp(object value) => value;
public override bool IsValid(object value) => true;
public override string ToDescriptionString() => "# Acceptable values: " + string.Join(", ", UnityInput.Current.SupportedKeyCodes);
}
#endregion
}
public static class KeyboardExtensions
{
public static bool IsKeyDown(this KeyboardShortcut shortcut)
{
return shortcut.MainKey != KeyCode.None && Input.GetKeyDown(shortcut.MainKey) && shortcut.Modifiers.All(Input.GetKey);
}
public static bool IsKeyHeld(this KeyboardShortcut shortcut)
{
return shortcut.MainKey != KeyCode.None && Input.GetKey(shortcut.MainKey) && shortcut.Modifiers.All(Input.GetKey);
}
}
}