forked from Michal78900/ProjectMER
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectMER.cs
More file actions
139 lines (109 loc) · 3.67 KB
/
ProjectMER.cs
File metadata and controls
139 lines (109 loc) · 3.67 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
global using Logger = LabApi.Features.Console.Logger;
using ApiLib.Library;
using HarmonyLib;
using LabApi.Events.CustomHandlers;
using LabApi.Loader.Features.Paths;
using LabApi.Loader.Features.Plugins;
using MEC;
using ProjectMER.Configs;
using ProjectMER.Events.Handlers.Internal;
using ProjectMER.Features;
namespace ProjectMER;
public class ProjectMER : Plugin<Config>
{
private Harmony _harmony;
private FileSystemWatcher _mapFileSystemWatcher;
public const int MerSettingId = int.MaxValue;
public static ProjectMER Singleton { get; private set; }
/// <summary>
/// Gets the MapEditorReborn parent folder path.
/// </summary>
public static string PluginDir { get; private set; }
/// <summary>
/// Gets the folder path in which the maps are stored.
/// </summary>
public static string MapsDir { get; private set; }
/// <summary>
/// Gets the folder path in which the schematics are stored.
/// </summary>
public static string SchematicsDir { get; private set; }
public GenericEventsHandler GenericEventsHandler { get; } = new();
public ToolGunEventsHandler ToolGunEventsHandler { get; } = new();
public ActionOnEventHandlers AcionOnEventHandlers { get; } = new();
public PickupEventsHandler PickupEventsHandler { get; } = new();
public override void Enable()
{
Singleton = this;
PluginDir = Path.Combine(PathManager.Configs.FullName, "ProjectMER");
MapsDir = Path.Combine(PluginDir, "Maps");
SchematicsDir = Path.Combine(PluginDir, "Schematics");
if (!Directory.Exists(PluginDir))
{
Logger.Warn("Plugin directory does not exist. Creating...");
Directory.CreateDirectory(PluginDir);
}
if (!Directory.Exists(MapsDir))
{
Logger.Warn("Maps directory does not exist. Creating...");
Directory.CreateDirectory(MapsDir);
}
if (!Directory.Exists(SchematicsDir))
{
Logger.Warn("Schematics directory does not exist. Creating...");
Directory.CreateDirectory(SchematicsDir);
}
CustomHandlersManager.RegisterEventsHandler(GenericEventsHandler);
CustomHandlersManager.RegisterEventsHandler(ToolGunEventsHandler);
CustomHandlersManager.RegisterEventsHandler(AcionOnEventHandlers);
CustomHandlersManager.RegisterEventsHandler(PickupEventsHandler);
_harmony = new Harmony($"michal78900.mapEditorReborn-{DateTime.Now.Ticks}");
_harmony.PatchAll();
if (Config!.EnableFileSystemWatcher)
{
_mapFileSystemWatcher = new FileSystemWatcher(MapsDir)
{
NotifyFilter = NotifyFilters.LastWrite,
Filter = "*.yml",
EnableRaisingEvents = true,
};
_mapFileSystemWatcher.Changed += OnMapFileChanged;
Logger.Debug("FileSystemWatcher enabled!");
}
}
private void OnMapFileChanged(object _, FileSystemEventArgs ev)
{
string mapName = ev.Name.Split('.')[0];
if (!MapUtils.LoadedMaps.ContainsKey(mapName))
return;
Timing.CallDelayed(0.01f, () =>
{
try
{
MapUtils.LoadMap(mapName);
}
catch (Exception e)
{
Logger.Error(e);
}
});
}
public override void Disable()
{
Singleton = null!;
CustomHandlersManager.UnregisterEventsHandler(GenericEventsHandler);
CustomHandlersManager.UnregisterEventsHandler(ToolGunEventsHandler);
CustomHandlersManager.UnregisterEventsHandler(AcionOnEventHandlers);
CustomHandlersManager.UnregisterEventsHandler(PickupEventsHandler);
_harmony.UnpatchAll();
_mapFileSystemWatcher?.Dispose();
}
public override string Name => "ProjectMER";
public override string Description => "MER LabAPI";
public override string Author => "Michal78900";
public override Version Version => new Version(2025, 8, 4, 2);
public override Version RequiredApiVersion => new Version(1, 0, 0, 0);
public static bool EventUsage()
{
return ServerInfo.EventOngoing;
}
}