Skip to content

Commit 976bc6e

Browse files
committed
Merge pull request Pathoschild#67 from Zoryn4163/master
logging things. not sure if ready for release build, testing for a bit.
2 parents 1123051 + 12bf4fd commit 976bc6e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1636
-988
lines changed

StardewModdingAPI/App.config

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
2+
23
<configuration>
3-
<startup>
4-
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
5-
</startup>
6-
<runtime>
7-
<loadFromRemoteSources enabled="true"/>
8-
</runtime>
9-
</configuration>
4+
<startup>
5+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
6+
</startup>
7+
<runtime>
8+
<loadFromRemoteSources enabled="true" />
9+
</runtime>
10+
</configuration>

StardewModdingAPI/Command.cs

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,43 @@ namespace StardewModdingAPI
77
public class Command
88
{
99
internal static List<Command> RegisteredCommands = new List<Command>();
10+
public string[] CalledArgs;
11+
public string[] CommandArgs;
12+
public string CommandDesc;
13+
14+
public string CommandName;
15+
16+
/// <summary>
17+
/// Creates a Command from a Name, Description, and Arguments
18+
/// </summary>
19+
/// <param name="cname">Name</param>
20+
/// <param name="cdesc">Description</param>
21+
/// <param name="args">Arguments</param>
22+
public Command(string cname, string cdesc, string[] args = null)
23+
{
24+
CommandName = cname;
25+
CommandDesc = cdesc;
26+
if (args == null)
27+
args = new string[0];
28+
CommandArgs = args;
29+
}
1030

11-
public String CommandName;
12-
public String CommandDesc;
13-
public String[] CommandArgs;
14-
public String[] CalledArgs;
1531
public event EventHandler<EventArgsCommand> CommandFired;
1632

1733
/// <summary>
18-
/// Calls the specified command. (It runs the command)
34+
/// Calls the specified command. (It runs the command)
1935
/// </summary>
2036
/// <param name="input">The command to run</param>
2137
public static void CallCommand(string input)
2238
{
2339
input = input.TrimEnd(' ');
24-
string[] args = new string[0];
40+
var args = new string[0];
2541
Command fnd;
2642
if (input.Contains(" "))
2743
{
2844
args = input.Split(new[] {" "}, 2, StringSplitOptions.RemoveEmptyEntries);
2945
fnd = FindCommand(args[0]);
30-
args = args[1].Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
46+
args = args[1].Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries);
3147
}
3248
else
3349
{
@@ -41,34 +57,34 @@ public static void CallCommand(string input)
4157
}
4258
else
4359
{
44-
Log.Error("Unknown Command");
60+
Log.AsyncR("Unknown Command");
4561
}
4662
}
4763

4864
/// <summary>
49-
/// Registers a command to the list of commands properly
65+
/// Registers a command to the list of commands properly
5066
/// </summary>
5167
/// <param name="command">Name of the command to register</param>
5268
/// <param name="cdesc">Description</param>
5369
/// <param name="args">Arguments (these are purely for viewing so that a user can see what an argument needs to be)</param>
5470
/// <returns></returns>
5571
public static Command RegisterCommand(string command, string cdesc, string[] args = null)
5672
{
57-
Command c = new Command(command, cdesc, args);
73+
var c = new Command(command, cdesc, args);
5874
if (RegisteredCommands.Contains(c))
5975
{
60-
Log.Error("Command already registered! [{0}]", c.CommandName);
76+
Log.AsyncR($"Command already registered! [{c.CommandName}]");
6177
return RegisteredCommands.Find(x => x.Equals(c));
6278
}
6379

6480
RegisteredCommands.Add(c);
65-
Log.Verbose("Registered command: " + command);
81+
Log.AsyncY("Registered command: " + command);
6682

6783
return c;
6884
}
6985

7086
/// <summary>
71-
/// Looks up a command in the list of registered commands. Returns null if it doesn't exist (I think)
87+
/// Looks up a command in the list of registered commands. Returns null if it doesn't exist (I think)
7288
/// </summary>
7389
/// <param name="name">Name of command to find</param>
7490
/// <returns></returns>
@@ -78,31 +94,16 @@ public static Command FindCommand(string name)
7894
}
7995

8096
/// <summary>
81-
/// Creates a Command from a Name, Description, and Arguments
82-
/// </summary>
83-
/// <param name="cname">Name</param>
84-
/// <param name="cdesc">Description</param>
85-
/// <param name="args">Arguments</param>
86-
public Command(String cname, String cdesc, String[] args = null)
87-
{
88-
CommandName = cname;
89-
CommandDesc = cdesc;
90-
if (args == null)
91-
args = new string[0];
92-
CommandArgs = args;
93-
}
94-
95-
/// <summary>
96-
/// Runs a command. Fires it. Calls it. Any of those.
97+
/// Runs a command. Fires it. Calls it. Any of those.
9798
/// </summary>
9899
public void Fire()
99100
{
100101
if (CommandFired == null)
101102
{
102-
Log.Error("Command failed to fire because it's fire event is null: " + CommandName);
103+
Log.AsyncR("Command failed to fire because it's fire event is null: " + CommandName);
103104
return;
104105
}
105106
CommandFired.Invoke(this, new EventArgsCommand(this));
106107
}
107108
}
108-
}
109+
}

StardewModdingAPI/Config.cs

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ Copyright 2016 Zoey (Zoryn)
55
using System;
66
using System.IO;
77
using System.Linq;
8-
using System.Reflection;
98
using Newtonsoft.Json;
109
using Newtonsoft.Json.Linq;
1110

@@ -22,23 +21,15 @@ public class Config
2221
public virtual Config Instance<T>() where T : Config => Activator.CreateInstance<T>();
2322

2423
/// <summary>
25-
/// Should never be used for anything.
26-
/// </summary>
27-
public Config()
28-
{
29-
30-
}
31-
32-
/// <summary>
33-
/// Loads the config from the json blob on disk, updating and re-writing to the disk if needed.
24+
/// Loads the config from the json blob on disk, updating and re-writing to the disk if needed.
3425
/// </summary>
3526
/// <typeparam name="T"></typeparam>
3627
/// <returns></returns>
3728
public virtual T LoadConfig<T>() where T : Config
3829
{
3930
if (string.IsNullOrEmpty(ConfigLocation))
4031
{
41-
Log.Error("A config tried to load without specifying a location on the disk.");
32+
Log.AsyncR("A config tried to load without specifying a location on the disk.");
4233
return null;
4334
}
4435

@@ -47,7 +38,7 @@ public virtual T LoadConfig<T>() where T : Config
4738
if (!File.Exists(ConfigLocation))
4839
{
4940
//no config exists, generate default values
50-
var c = this.GenerateDefaultConfig<T>();
41+
var c = GenerateDefaultConfig<T>();
5142
c.ConfigLocation = ConfigLocation;
5243
ret = c;
5344
}
@@ -56,7 +47,7 @@ public virtual T LoadConfig<T>() where T : Config
5647
try
5748
{
5849
//try to load the config from a json blob on disk
59-
T c = JsonConvert.DeserializeObject<T>(File.ReadAllText(ConfigLocation));
50+
var c = JsonConvert.DeserializeObject<T>(File.ReadAllText(ConfigLocation), new JsonSerializerSettings {ContractResolver = new JsonResolver()});
6051

6152
c.ConfigLocation = ConfigLocation;
6253

@@ -67,7 +58,7 @@ public virtual T LoadConfig<T>() where T : Config
6758
}
6859
catch (Exception ex)
6960
{
70-
Log.Error("Invalid JSON ({0}): {1} \n{2}", GetType().Name, ConfigLocation, ex);
61+
Log.AsyncR($"Invalid JSON ({GetType().Name}): {ConfigLocation} \n{ex}");
7162
return GenerateDefaultConfig<T>();
7263
}
7364
}
@@ -77,15 +68,15 @@ public virtual T LoadConfig<T>() where T : Config
7768
}
7869

7970
/// <summary>
80-
/// MUST be implemented in inheriting class!
71+
/// MUST be implemented in inheriting class!
8172
/// </summary>
8273
public virtual T GenerateDefaultConfig<T>() where T : Config
8374
{
8475
return null;
8576
}
8677

8778
/// <summary>
88-
/// Merges a default-value config with the user-config on disk.
79+
/// Merges a default-value config with the user-config on disk.
8980
/// </summary>
9081
/// <typeparam name="T"></typeparam>
9182
/// <returns></returns>
@@ -94,16 +85,16 @@ public virtual T UpdateConfig<T>() where T : Config
9485
try
9586
{
9687
//default config
97-
var b = JObject.FromObject(Instance<T>().GenerateDefaultConfig<T>());
88+
var b = JObject.FromObject(Instance<T>().GenerateDefaultConfig<T>(), new JsonSerializer {ContractResolver = new JsonResolver()});
9889

9990
//user config
100-
var u = JObject.FromObject(this);
91+
var u = JObject.FromObject(this, new JsonSerializer {ContractResolver = new JsonResolver()});
10192

10293
//overwrite default values with user values
10394
b.Merge(u, new JsonMergeSettings {MergeArrayHandling = MergeArrayHandling.Replace});
10495

10596
//cast json object to config
106-
T c = b.ToObject<T>();
97+
var c = b.ToObject<T>();
10798

10899
//re-write the location on disk to the object
109100
c.ConfigLocation = ConfigLocation;
@@ -112,7 +103,7 @@ public virtual T UpdateConfig<T>() where T : Config
112103
}
113104
catch (Exception ex)
114105
{
115-
Log.Error("An error occured when updating a config: " + ex);
106+
Log.AsyncR("An error occured when updating a config: " + ex);
116107
return this as T;
117108
}
118109
}
@@ -121,10 +112,10 @@ public virtual T UpdateConfig<T>() where T : Config
121112
public static class ConfigExtensions
122113
{
123114
/// <summary>
124-
/// Initializes an instance of any class that inherits from Config.
125-
/// This method performs the loading, saving, and merging of the config on the disk and in memory at a default state.
126-
/// This method should not be used to re-load or to re-save a config.
127-
/// NOTE: You MUST set your config EQUAL to the return of this method!
115+
/// Initializes an instance of any class that inherits from Config.
116+
/// This method performs the loading, saving, and merging of the config on the disk and in memory at a default state.
117+
/// This method should not be used to re-load or to re-save a config.
118+
/// NOTE: You MUST set your config EQUAL to the return of this method!
128119
/// </summary>
129120
/// <typeparam name="T"></typeparam>
130121
/// <param name="baseConfig"></param>
@@ -136,35 +127,35 @@ public static T InitializeConfig<T>(this T baseConfig, string configLocation) wh
136127
{
137128
baseConfig = Activator.CreateInstance<T>();
138129
/*
139-
Log.Error("A config tried to initialize whilst being null.");
130+
Log.AsyncR("A config tried to initialize whilst being null.");
140131
return null;
141132
*/
142133
}
143134

144135
if (string.IsNullOrEmpty(configLocation))
145136
{
146-
Log.Error("A config tried to initialize without specifying a location on the disk.");
137+
Log.AsyncR("A config tried to initialize without specifying a location on the disk.");
147138
return null;
148139
}
149140

150141
baseConfig.ConfigLocation = configLocation;
151-
T c = baseConfig.LoadConfig<T>();
142+
var c = baseConfig.LoadConfig<T>();
152143

153144
return c;
154145
}
155146

156147
/// <summary>
157-
/// Writes a config to a json blob on the disk specified in the config's properties.
148+
/// Writes a config to a json blob on the disk specified in the config's properties.
158149
/// </summary>
159150
public static void WriteConfig<T>(this T baseConfig) where T : Config
160151
{
161152
if (string.IsNullOrEmpty(baseConfig?.ConfigLocation) || string.IsNullOrEmpty(baseConfig.ConfigDir))
162153
{
163-
Log.Error("A config attempted to save when it itself or it's location were null.");
154+
Log.AsyncR("A config attempted to save when it itself or it's location were null.");
164155
return;
165156
}
166157

167-
string s = JsonConvert.SerializeObject(baseConfig, typeof (T), Formatting.Indented, new JsonSerializerSettings());
158+
var s = JsonConvert.SerializeObject(baseConfig, typeof (T), Formatting.Indented, new JsonSerializerSettings {ContractResolver = new JsonResolver()});
168159

169160
if (!Directory.Exists(baseConfig.ConfigDir))
170161
Directory.CreateDirectory(baseConfig.ConfigDir);
@@ -174,7 +165,8 @@ public static void WriteConfig<T>(this T baseConfig) where T : Config
174165
}
175166

176167
/// <summary>
177-
/// Re-reads the json blob on the disk and merges its values with a default config
168+
/// Re-reads the json blob on the disk and merges its values with a default config
169+
/// NOTE: You MUST set your config EQUAL to the return of this method!
178170
/// </summary>
179171
public static T ReloadConfig<T>(this T baseConfig) where T : Config
180172
{

StardewModdingAPI/Constants.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@
66
namespace StardewModdingAPI
77
{
88
/// <summary>
9-
/// Static class containing readonly values.
9+
/// Static class containing readonly values.
1010
/// </summary>
1111
public static class Constants
1212
{
13+
public static readonly Version Version = new Version(0, 39, 3, "Alpha");
14+
15+
/// <summary>
16+
/// Not quite "constant", but it makes more sense for it to be here, at least for now
17+
/// </summary>
18+
public static int ModsLoaded = 0;
19+
1320
/// <summary>
14-
/// Stardew Valley's roaming app data location.
15-
/// %AppData%//StardewValley
21+
/// Stardew Valley's roaming app data location.
22+
/// %AppData%//StardewValley
1623
/// </summary>
1724
public static string DataPath => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StardewValley");
1825

@@ -29,26 +36,27 @@ public static class Constants
2936
public static bool PlayerNull => !Game1.hasLoadedGame || Game1.player == null || string.IsNullOrEmpty(Game1.player.name);
3037

3138
/// <summary>
32-
/// Execution path to execute the code.
39+
/// Execution path to execute the code.
3340
/// </summary>
3441
public static string ExecutionPath => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
3542

3643
/// <summary>
37-
/// Title for the API console
44+
/// Title for the API console
3845
/// </summary>
3946
public static string ConsoleTitle => $"Stardew Modding API Console - Version {Version.VersionString} - Mods Loaded: {ModsLoaded}";
4047

4148
/// <summary>
42-
/// Path for log files to be output to.
43-
/// %LocalAppData%//StardewValley//ErrorLogs
49+
/// Path for log files to be output to.
50+
/// %LocalAppData%//StardewValley//ErrorLogs
4451
/// </summary>
45-
public static string LogPath => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StardewValley", "ErrorLogs");
52+
public static string LogDir => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StardewValley", "ErrorLogs");
4653

47-
public static readonly Version Version = new Version(0, 39, 2, "Alpha");
54+
public static string LogPath => Path.Combine(LogDir, "MODDED_ProgramLog.Log_LATEST.txt");
4855

4956
/// <summary>
50-
/// Not quite "constant", but it makes more sense for it to be here, at least for now
57+
/// Whether or not to enable the Render Target drawing code offered by ClxS
58+
/// Do not mark as 'const' or else 'if' checks will complain that the expression is always true in ReSharper
5159
/// </summary>
52-
public static int ModsLoaded = 0;
60+
public static bool EnableDrawingIntoRenderTarget => true;
5361
}
54-
}
62+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace StardewModdingAPI.Entities
22
{
3-
class SCharacter
3+
internal class SCharacter
44
{
55
}
6-
}
6+
}

0 commit comments

Comments
 (0)