-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModEntry.cs
More file actions
162 lines (139 loc) · 5.27 KB
/
Copy pathModEntry.cs
File metadata and controls
162 lines (139 loc) · 5.27 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System.Reflection.Emit;
using HarmonyLib;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
namespace ConsoleMacros;
// ReSharper disable FieldCanBeMadeReadOnly.Global
// ReSharper disable InconsistentNaming
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable ClassNeverInstantiated.Global
public class ModEntry : Mod
{
public static void Log(string v, LogLevel logLevel = LogLevel.Debug)
{
_log.Log(v, logLevel);
}
public static IMonitor _log = null!;
public static IManifest Manifest = null!;
public static string assetName = null!;
public static Dictionary<string, string> macros = new();
public override void Entry(IModHelper helper)
{
_log = Monitor;
Manifest = ModManifest;
assetName = Manifest.UniqueID + "/Macros";
Helper.Events.Content.AssetRequested += OnAssetRequested;
Helper.Events.GameLoop.GameLaunched += OnGameLaunched;
Helper.Events.GameLoop.SaveLoaded += OnSaveLoaded;
helper.ConsoleCommands.Add("macro",
"Base command for SMAPI macro execution and debugging. See: \"macro run\", \"macro list\", \"macro reload\".",
Macro);
}
private static void RefreshMacros()
{
string[] files = Directory.GetFiles("Mods/ConsoleMacros/Macros", "*.*");
foreach (var file in files)
{
string fileName = new FileInfo(file).Name.Split(".").SkipLast(1).Join(delimiter: ".");
macros[fileName] = File.ReadAllText(file);
}
Dictionary<string, string> contentSource = Game1.content.Load<Dictionary<string, string>>(assetName);
// https://stackoverflow.com/a/10559415
macros = macros.Concat(contentSource)
.GroupBy(v => v.Key)
.ToDictionary(v => v.Key, v => v.First().Value);
Log($"Loaded {macros.Count} macros:" +
$"\n {contentSource.Count} added via Content Patcher" +
$"\n {macros.Count - contentSource.Count} added via Macros folder."
);
}
public void Macro(string command, string[] args)
{
switch (args.Length != 0 ? args[0] : "")
{
case "run":
MacroRun(args);
break;
case "list":
MacroList();
break;
case "reload":
RefreshMacros();
break;
default:
Log(
"Base command for SMAPI macro execution and debugging.\n\nCommands:\n macro run <macro>\n macro list\n");
break;
}
}
public static void MacroRun(string[] args)
{
List<string> commands = new();
foreach (var macro in args)
{
string? thisMacro = macros.GetValueOrDefault(macro);
if (thisMacro is null)
{
thisMacro = macros.GetValueOrDefault(macro.ToLower());
}
if (thisMacro is null) continue;
commands.AddRange(thisMacro.Split("\n"));
}
foreach (var cmd in commands)
{
try
{
ExecuteCommand(cmd);
}
catch
{
// Should never happen. Just in case.
Log($"Failed to execute command: \"{cmd}\"", LogLevel.Error);
}
}
}
public static void MacroList()
{
Log($"Available macros:\n\n {macros.Keys.Join(delimiter:", ")}");
}
// https://gist.github.com/Shockah/ec111245868ee9b7dbf2ca2928dd2896
private static readonly Lazy<Action<string>> AddToRawCommandQueue = new(() =>
{
var scoreType = AccessTools.TypeByName("StardewModdingAPI.Framework.SCore, StardewModdingAPI")!;
var commandQueueType = AccessTools.TypeByName("StardewModdingAPI.Framework.CommandQueue, StardewModdingAPI")!;
var scoreGetter = AccessTools.PropertyGetter(scoreType, "Instance")!;
var rawCommandQueueField = AccessTools.Field(scoreType, "RawCommandQueue")!;
var commandQueueAddMethod = AccessTools.Method(commandQueueType, "Add");
var dynamicMethod = new DynamicMethod("AddToRawCommandQueue", null, new Type[] { typeof(string) });
var il = dynamicMethod.GetILGenerator();
il.Emit(OpCodes.Call, scoreGetter);
il.Emit(OpCodes.Ldfld, rawCommandQueueField);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Call, commandQueueAddMethod);
il.Emit(OpCodes.Ret);
return dynamicMethod.CreateDelegate<Action<string>>();
});
// https://gist.github.com/Shockah/ec111245868ee9b7dbf2ca2928dd2896
private static void ExecuteCommand(string command)
{
if (string.IsNullOrEmpty(command))
return;
AddToRawCommandQueue.Value(command);
}
private static void OnGameLaunched(object? sender, GameLaunchedEventArgs e)
{
RefreshMacros();
}
private static void OnSaveLoaded(object? sender, SaveLoadedEventArgs e)
{
RefreshMacros();
}
private static void OnAssetRequested(object? sender, AssetRequestedEventArgs e)
{
if (e.Name.IsEquivalentTo(assetName))
{
e.LoadFrom(() => new Dictionary<string, string>(), AssetLoadPriority.Exclusive);
}
}
}