-
Notifications
You must be signed in to change notification settings - Fork 16
/
ModEntry.cs
135 lines (105 loc) · 4.79 KB
/
ModEntry.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
#region global using directives
#pragma warning disable SA1200 // Using directives should be placed correctly
global using static DaLion.Overhaul.ModEntry;
global using static DaLion.Overhaul.Modules.OverhaulModule;
#pragma warning restore SA1200 // Using directives should be placed correctly
#endregion global using directives
namespace DaLion.Overhaul;
#region using directives
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using DaLion.Shared.Events;
using DaLion.Shared.Extensions.Collections;
using DaLion.Shared.Extensions.Reflection;
using DaLion.Shared.Extensions.SMAPI;
using DaLion.Shared.ModData;
using DaLion.Shared.Networking;
using DaLion.Shared.Reflection;
using StardewModdingAPI.Utilities;
#endregion using directives
/// <summary>The mod entry point.</summary>
public sealed class ModEntry : Mod
{
/// <inheritdoc cref="Stopwatch"/>
private readonly Stopwatch _sw = new();
/// <summary>Gets the static <see cref="ModEntry"/> instance.</summary>
internal static ModEntry Instance { get; private set; } = null!; // set in Entry
/// <summary>Gets or sets the <see cref="ModConfig"/> instance.</summary>
internal static ModConfig Config { get; set; } = null!; // set in Entry
/// <summary>Gets or sets the <see cref="ModData"/> instance.</summary>
internal static ModData Data { get; set; } = null!; // set in Entry
/// <summary>Gets the <see cref="PerScreen{T}"/> <see cref="ModState"/>.</summary>
internal static PerScreen<ModState> PerScreenState { get; private set; } = null!; // set in Entry
/// <summary>Gets or sets the <see cref="ModState"/> of the local player.</summary>
internal static ModState State
{
get => PerScreenState.Value;
set => PerScreenState.Value = value;
}
/// <summary>Gets the <see cref="Shared.Events.EventManager"/> instance.</summary>
internal static EventManager EventManager { get; private set; } = null!; // set in Entry
/// <summary>Gets the <see cref="Reflector"/> instance.</summary>
internal static Reflector Reflector { get; private set; } = null!; // set in Entry
/// <summary>Gets the <see cref="Broadcaster"/> instance.</summary>
internal static Broadcaster Broadcaster { get; private set; } = null!; // set in Entry
/// <summary>Gets the <see cref="IModHelper"/> API.</summary>
internal static IModHelper ModHelper => Instance.Helper;
/// <summary>Gets the <see cref="IManifest"/> for this mod.</summary>
internal static IManifest Manifest => Instance.ModManifest;
/// <summary>Gets the <see cref="ITranslationHelper"/> API.</summary>
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1300:Element should begin with upper-case letter", Justification = "Conflicts with Pathoschild.TranslationBuilder")]
// ReSharper disable once InconsistentNaming
internal static ITranslationHelper _I18n => ModHelper.Translation;
/// <summary>The mod entry point, called after the mod is first loaded.</summary>
/// <param name="helper">Provides simplified APIs for writing mods.</param>
public override void Entry(IModHelper helper)
{
this.StartWatch();
Instance = this;
Log.Init(this.Monitor);
// check SpaceCore build first of all
var spaceCoreAssembly = Assembly.Load("SpaceCore");
if (spaceCoreAssembly.IsDebugBuild())
{
Log.E(
"The installed version of SpaceCore was built in Debug mode, which is not compatible with this mood. Please ask the author to build in Release mode.");
return;
}
ModDataIO.Init(this.ModManifest.UniqueID);
I18n.Init(helper.Translation);
Data = helper.Data.ReadJsonFile<ModData>("data.json") ?? new ModData();
Config = helper.ReadConfig<ModConfig>();
Config.Validate(helper);
Config.Log();
PerScreenState = new PerScreen<ModState>(() => new ModState());
EventManager = new EventManager(helper.Events, helper.ModRegistry);
Reflector = new Reflector();
Broadcaster = new Broadcaster(helper.Multiplayer, this.ModManifest.UniqueID);
EnumerateModules().ForEach(module => module.Activate(helper));
this.ValidateMultiplayer();
this.StopWatch();
this.LogStats();
Log.I("[Entry]: Version checksum: " + this.GetType().Assembly.CalculateMd5());
}
/// <inheritdoc />
public override object GetApi()
{
return new ModApi();
}
[Conditional("DEBUG")]
private void StartWatch()
{
this._sw.Start();
}
[Conditional("DEBUG")]
private void StopWatch()
{
this._sw.Stop();
}
[Conditional("DEBUG")]
private void LogStats()
{
Log.A($"[Entry]: Initialization completed in {this._sw.ElapsedMilliseconds}ms.");
}
}