-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configuration loading is now separated from the actual configuration data, which should make it easier to eventually reuse. Also cleaned up some formatting in EntityAttachment.cs.
- Loading branch information
Showing
5 changed files
with
80 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,25 @@ | ||
using Newtonsoft.Json; | ||
using System.ComponentModel; | ||
using System.ComponentModel; | ||
using System.IO; | ||
using Terraria.ID; | ||
using TShockAPI; | ||
|
||
namespace BloodTithe | ||
{ | ||
class BloodTitheConfig | ||
struct BloodTitheConfig : IPluginConfiguration | ||
{ | ||
private static readonly string FilePath = Path.Combine(TShock.SavePath, "BloodTithe.json"); | ||
private static readonly BloodTitheConfig Default = JsonConvert.DeserializeObject<BloodTitheConfig>("{}"); | ||
public string FilePath => Path.Combine(TShock.SavePath, "BloodTithe.json"); | ||
|
||
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)] | ||
[DefaultValue(ItemID.LifeCrystal)] | ||
public int Item { get; private set; } | ||
|
||
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)] | ||
[DefaultValue(3)] | ||
public int ItemsRequired { get; private set; } | ||
|
||
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)] | ||
[DefaultValue(false)] | ||
public bool PreventCorruption { get; private set; } | ||
|
||
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)] | ||
[DefaultValue(true)] | ||
public bool SpawnWarpFairy { get; private set; } | ||
|
||
public static BloodTitheConfig Load() | ||
{ | ||
TShock.Log.Info("Loading configuration from {0}", FilePath); | ||
|
||
BloodTitheConfig config; | ||
|
||
if (!File.Exists(FilePath)) | ||
{ | ||
config = Default; | ||
} | ||
else | ||
{ | ||
try | ||
{ | ||
config = JsonConvert.DeserializeObject<BloodTitheConfig>(File.ReadAllText(FilePath)); | ||
} | ||
catch (JsonReaderException e) | ||
{ | ||
TShock.Log.Error("Error loading {0}: {1}", FilePath, e.Message); | ||
config = Default; | ||
} | ||
} | ||
|
||
File.WriteAllText(FilePath, JsonConvert.SerializeObject(config, Formatting.Indented)); | ||
return config; | ||
} | ||
|
||
public override string ToString() => JsonConvert.SerializeObject(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Serialization; | ||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using TShockAPI; | ||
|
||
namespace BloodTithe | ||
{ | ||
interface IPluginConfiguration { | ||
[JsonIgnore] | ||
string FilePath { get; } | ||
} | ||
|
||
class DefaultValueContractResolver : DefaultContractResolver | ||
{ | ||
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) | ||
{ | ||
var prop = base.CreateProperty(member, memberSerialization); | ||
bool tagged = member.GetCustomAttribute<System.ComponentModel.DefaultValueAttribute>() != null && (member as PropertyInfo)?.SetMethod != null; | ||
prop.Writable = tagged; | ||
prop.ShouldSerialize = _ => tagged; | ||
prop.ShouldDeserialize = _ => tagged; | ||
return prop; | ||
} | ||
} | ||
|
||
static class PluginConfiguration | ||
{ | ||
private readonly static JsonSerializerSettings settings = new JsonSerializerSettings() { | ||
DefaultValueHandling = DefaultValueHandling.Populate, | ||
ContractResolver = new DefaultValueContractResolver() | ||
}; | ||
|
||
public static T LoadDefault<T>() => JsonConvert.DeserializeObject<T>("{}", settings); | ||
|
||
public static T Load<T>() where T : IPluginConfiguration | ||
{ | ||
string filePath = default(T).FilePath; | ||
TShock.Log.Info("Loading configuration from {0}", filePath); | ||
|
||
T config; | ||
if (!File.Exists(filePath)) | ||
{ | ||
config = LoadDefault<T>(); | ||
} | ||
else | ||
{ | ||
try | ||
{ | ||
config = JsonConvert.DeserializeObject<T>(File.ReadAllText(filePath), settings); | ||
} | ||
catch (JsonReaderException e) | ||
{ | ||
TShock.Log.Error("Error loading {0}: {1}", filePath, e.Message); | ||
config = LoadDefault<T>(); | ||
} | ||
} | ||
|
||
File.WriteAllText(filePath, JsonConvert.SerializeObject(config, Formatting.Indented)); | ||
return config; | ||
} | ||
|
||
public static string Stringify<T>(T config) where T : IPluginConfiguration => JsonConvert.SerializeObject(config); | ||
} | ||
|
||
} |