summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2016-11-14 22:25:36 -0500
committerJesse Plamondon-Willard <github@jplamondonw.com>2016-11-14 22:25:36 -0500
commit54c0f26d5d6faaf71da4eb91bf28203390d79e71 (patch)
treef34ebb17da6182c7e44e4e1949b48e8df9484fc5
parentf54b4647921b04ce88b45bf90a33ae12e38fb77e (diff)
downloadSMAPI-54c0f26d5d6faaf71da4eb91bf28203390d79e71.tar.gz
SMAPI-54c0f26d5d6faaf71da4eb91bf28203390d79e71.tar.bz2
SMAPI-54c0f26d5d6faaf71da4eb91bf28203390d79e71.zip
migrate game loop, events, and commands to new logging framework (#168)
-rw-r--r--src/StardewModdingAPI/Command.cs16
-rw-r--r--src/StardewModdingAPI/Config.cs10
-rw-r--r--src/StardewModdingAPI/Events/GameEvents.cs15
-rw-r--r--src/StardewModdingAPI/Events/GraphicsEvents.cs5
-rw-r--r--src/StardewModdingAPI/Inheritance/SGame.cs21
-rw-r--r--src/StardewModdingAPI/Inheritance/SObject.cs9
-rw-r--r--src/StardewModdingAPI/Mod.cs2
-rw-r--r--src/StardewModdingAPI/Program.cs4
8 files changed, 38 insertions, 44 deletions
diff --git a/src/StardewModdingAPI/Command.cs b/src/StardewModdingAPI/Command.cs
index 65207f0e..ef4ca2d8 100644
--- a/src/StardewModdingAPI/Command.cs
+++ b/src/StardewModdingAPI/Command.cs
@@ -59,10 +59,7 @@ namespace StardewModdingAPI
public void Fire()
{
if (this.CommandFired == null)
- {
- Log.AsyncR($"Command failed to fire because it's fire event is null: {this.CommandName}");
- return;
- }
+ throw new InvalidOperationException($"Can't run command '{this.CommandName}' because it has no registered handler.");
this.CommandFired.Invoke(this, new EventArgsCommand(this));
}
@@ -71,7 +68,8 @@ namespace StardewModdingAPI
****/
/// <summary>Parse a command string and invoke it if valid.</summary>
/// <param name="input">The command to run, including the command name and any arguments.</param>
- public static void CallCommand(string input)
+ /// <param name="monitor">Encapsulates monitoring and logging.</param>
+ public static void CallCommand(string input, IMonitor monitor)
{
// normalise input
input = input?.Trim();
@@ -87,7 +85,7 @@ namespace StardewModdingAPI
Command command = Command.FindCommand(commandName);
if (command == null)
{
- Log.AsyncR("Unknown command");
+ monitor.Log("Unknown command", LogLevel.Error);
return;
}
@@ -104,13 +102,9 @@ namespace StardewModdingAPI
{
var command = new Command(name, description, args);
if (Command.RegisteredCommands.Contains(command))
- {
- Log.AsyncR($"Command already registered! [{command.CommandName}]");
- return Command.RegisteredCommands.Find(x => x.Equals(command));
- }
+ throw new InvalidOperationException($"The '{command.CommandName}' command is already registered!");
Command.RegisteredCommands.Add(command);
-
return command;
}
diff --git a/src/StardewModdingAPI/Config.cs b/src/StardewModdingAPI/Config.cs
index 326685c1..0d94ba76 100644
--- a/src/StardewModdingAPI/Config.cs
+++ b/src/StardewModdingAPI/Config.cs
@@ -39,7 +39,7 @@ namespace StardewModdingAPI
// validate
if (string.IsNullOrEmpty(this.ConfigLocation))
{
- Log.AsyncR("A config tried to load without specifying a location on the disk.");
+ Log.Error("A config tried to load without specifying a location on the disk.");
return null;
}
@@ -61,7 +61,7 @@ namespace StardewModdingAPI
}
catch (Exception ex)
{
- Log.AsyncR($"Invalid JSON ({this.GetType().Name}): {this.ConfigLocation} \n{ex}");
+ Log.Error($"Invalid JSON ({this.GetType().Name}): {this.ConfigLocation} \n{ex}");
return this.GenerateDefaultConfig<T>();
}
}
@@ -96,7 +96,7 @@ namespace StardewModdingAPI
}
catch (Exception ex)
{
- Log.AsyncR($"An error occured when updating a config: {ex}");
+ Log.Error($"An error occured when updating a config: {ex}");
return this as T;
}
}
@@ -129,7 +129,7 @@ namespace StardewModdingAPI
if (string.IsNullOrEmpty(configLocation))
{
- Log.AsyncR("A config tried to initialize without specifying a location on the disk.");
+ Log.Error("A config tried to initialize without specifying a location on the disk.");
return null;
}
@@ -145,7 +145,7 @@ namespace StardewModdingAPI
{
if (string.IsNullOrEmpty(baseConfig?.ConfigLocation) || string.IsNullOrEmpty(baseConfig.ConfigDir))
{
- Log.AsyncR("A config attempted to save when it itself or it's location were null.");
+ Log.Error("A config attempted to save when it itself or it's location were null.");
return;
}
diff --git a/src/StardewModdingAPI/Events/GameEvents.cs b/src/StardewModdingAPI/Events/GameEvents.cs
index e1bfc924..46505d14 100644
--- a/src/StardewModdingAPI/Events/GameEvents.cs
+++ b/src/StardewModdingAPI/Events/GameEvents.cs
@@ -52,7 +52,8 @@ namespace StardewModdingAPI.Events
}
/// <summary>Raise an <see cref="Initialize"/> event.</summary>
- internal static void InvokeInitialize()
+ /// <param name="monitor">Encapsulates logging and monitoring.</param>
+ internal static void InvokeInitialize(IMonitor monitor)
{
try
{
@@ -60,12 +61,13 @@ namespace StardewModdingAPI.Events
}
catch (Exception ex)
{
- Log.Error($"A mod crashed handling an event.\n{ex}");
+ monitor.Log($"A mod crashed handling an event.\n{ex}", LogLevel.Error);
}
}
/// <summary>Raise a <see cref="LoadContent"/> event.</summary>
- internal static void InvokeLoadContent()
+ /// <param name="monitor">Encapsulates logging and monitoring.</param>
+ internal static void InvokeLoadContent(IMonitor monitor)
{
try
{
@@ -73,12 +75,13 @@ namespace StardewModdingAPI.Events
}
catch (Exception ex)
{
- Log.Error($"A mod crashed handling an event.\n{ex}");
+ monitor.Log($"A mod crashed handling an event.\n{ex}", LogLevel.Error);
}
}
/// <summary>Raise an <see cref="UpdateTick"/> event.</summary>
- internal static void InvokeUpdateTick()
+ /// <param name="monitor">Encapsulates logging and monitoring.</param>
+ internal static void InvokeUpdateTick(IMonitor monitor)
{
try
{
@@ -86,7 +89,7 @@ namespace StardewModdingAPI.Events
}
catch (Exception ex)
{
- Log.Error($"A mod crashed handling an event.\n{ex}");
+ monitor.Log($"A mod crashed handling an event.\n{ex}", LogLevel.Error);
}
}
diff --git a/src/StardewModdingAPI/Events/GraphicsEvents.cs b/src/StardewModdingAPI/Events/GraphicsEvents.cs
index a6112807..3f52212b 100644
--- a/src/StardewModdingAPI/Events/GraphicsEvents.cs
+++ b/src/StardewModdingAPI/Events/GraphicsEvents.cs
@@ -88,8 +88,9 @@ namespace StardewModdingAPI.Events
}
/// <summary>Raise a <see cref="DrawTick"/> event.</summary>
+ /// <param name="monitor">Encapsulates logging and monitoring.</param>
[Obsolete("Should not be used.")]
- public static void InvokeDrawTick()
+ public static void InvokeDrawTick(IMonitor monitor)
{
try
{
@@ -97,7 +98,7 @@ namespace StardewModdingAPI.Events
}
catch (Exception ex)
{
- Log.AsyncR("An exception occured in a Mod's DrawTick: " + ex);
+ monitor.Log($"A mod crashed handling an event.\n{ex}", LogLevel.Error);
}
}
diff --git a/src/StardewModdingAPI/Inheritance/SGame.cs b/src/StardewModdingAPI/Inheritance/SGame.cs
index 3c79c779..5f562ea9 100644
--- a/src/StardewModdingAPI/Inheritance/SGame.cs
+++ b/src/StardewModdingAPI/Inheritance/SGame.cs
@@ -32,6 +32,9 @@ namespace StardewModdingAPI.Inheritance
/// <summary>Whether the game's zoom level is at 100% (i.e. nothing should be scaled).</summary>
public bool ZoomLevelIsOne => Game1.options.zoomLevel.Equals(1.0f);
+ /// <summary>Encapsulates monitoring and logging.</summary>
+ private readonly IMonitor Monitor;
+
/*********
** Accessors
@@ -278,10 +281,12 @@ namespace StardewModdingAPI.Inheritance
** Protected methods
*********/
/// <summary>Construct an instance.</summary>
- internal SGame()
+ /// <param name="monitor">Encapsulates monitoring and logging.</param>
+ internal SGame(IMonitor monitor)
{
- SGame.Instance = this;
+ this.Monitor = monitor;
this.FirstUpdate = true;
+ SGame.Instance = this;
}
/// <summary>The method called during game launch after configuring XNA or MonoGame. The game window hasn't been opened by this point.</summary>
@@ -294,14 +299,14 @@ namespace StardewModdingAPI.Inheritance
this.PreviouslyPressedButtons[i] = new Buttons[0];
base.Initialize();
- GameEvents.InvokeInitialize();
+ GameEvents.InvokeInitialize(this.Monitor);
}
/// <summary>The method called before XNA or MonoGame loads or reloads graphics resources.</summary>
protected override void LoadContent()
{
base.LoadContent();
- GameEvents.InvokeLoadContent();
+ GameEvents.InvokeLoadContent(this.Monitor);
}
/// <summary>The method called when the game is updating its state. This happens roughly 60 times per second.</summary>
@@ -325,12 +330,12 @@ namespace StardewModdingAPI.Inheritance
}
catch (Exception ex)
{
- Log.AsyncR($"An error occured in the base update loop: {ex}");
+ this.Monitor.Log($"An error occured in the base update loop: {ex}", LogLevel.Error);
Console.ReadKey();
}
// raise update events
- GameEvents.InvokeUpdateTick();
+ GameEvents.InvokeUpdateTick(this.Monitor);
if (this.FirstUpdate)
{
GameEvents.InvokeFirstUpdateTick();
@@ -752,11 +757,11 @@ namespace StardewModdingAPI.Inheritance
Game1.spriteBatch.End();
}
- GraphicsEvents.InvokeDrawTick();
+ GraphicsEvents.InvokeDrawTick(this.Monitor);
}
catch (Exception ex)
{
- Log.Error("An error occured in the overridden draw loop: " + ex);
+ this.Monitor.Log($"An error occured in the overridden draw loop: {ex}", LogLevel.Error);
}
if (SGame.Debug)
diff --git a/src/StardewModdingAPI/Inheritance/SObject.cs b/src/StardewModdingAPI/Inheritance/SObject.cs
index ebdf6d56..a90ed624 100644
--- a/src/StardewModdingAPI/Inheritance/SObject.cs
+++ b/src/StardewModdingAPI/Inheritance/SObject.cs
@@ -87,11 +87,6 @@ namespace StardewModdingAPI.Inheritance
{
}
- public override void draw(SpriteBatch spriteBatch, int xNonTile, int yNonTile, float layerDepth, float alpha = 1)
- {
- Log.Debug("THIS DRAW FUNCTION IS NOT IMPLEMENTED I WANT TO KNOW WHERE IT IS CALLED");
- }
-
public override void drawInMenu(SpriteBatch spriteBatch, Vector2 location, float scaleSize, float transparency, float layerDepth, bool drawStackNumber)
{
if (isRecipe)
@@ -219,9 +214,6 @@ namespace StardewModdingAPI.Inheritance
x = x / Game1.tileSize;
y = y / Game1.tileSize;
- //Program.LogDebug(x + " - " + y);
- //Console.ReadKey();
-
var key = new Vector2(x, y);
if (!canBePlacedHere(location, key))
@@ -233,7 +225,6 @@ namespace StardewModdingAPI.Inheritance
s.boundingBox = new Rectangle(x / Game1.tileSize * Game1.tileSize, y / Game1.tileSize * Game1.tileSize, boundingBox.Width, boundingBox.Height);
location.objects.Add(key, s);
- Log.Async($"{GetHashCode()} - {s.GetHashCode()}");
return true;
}
diff --git a/src/StardewModdingAPI/Mod.cs b/src/StardewModdingAPI/Mod.cs
index ce7c8e31..e3011a19 100644
--- a/src/StardewModdingAPI/Mod.cs
+++ b/src/StardewModdingAPI/Mod.cs
@@ -89,7 +89,7 @@ namespace StardewModdingAPI
if (!this.Manifest.PerSaveConfigs)
{
- Log.Error($"The mod [{this.Manifest.Name}] is not configured to use per-save configs.");
+ this.Monitor.Log("Tried to fetch the per-save config folder, but this mod isn't configured to use per-save config files.", LogLevel.Error);
return "";
}
return Path.Combine(this.PathOnDisk, "psconfigs");
diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs
index ae61bc4d..0e1a2527 100644
--- a/src/StardewModdingAPI/Program.cs
+++ b/src/StardewModdingAPI/Program.cs
@@ -191,7 +191,7 @@ namespace StardewModdingAPI
try
{
Program.Monitor.Log("Patching game...");
- Program.gamePtr = new SGame();
+ Program.gamePtr = new SGame(Program.Monitor);
// hook events
Program.gamePtr.Exiting += (sender, e) => Program.ready = false;
@@ -396,7 +396,7 @@ namespace StardewModdingAPI
private static void ConsoleInputLoop()
{
while (true)
- Command.CallCommand(Console.ReadLine());
+ Command.CallCommand(Console.ReadLine(), Program.Monitor);
}
/// <summary>The method called when the user submits the help command in the console.</summary>