Skip to content

Commit 032091b

Browse files
committed
Implement build prop processing
1 parent ede9abd commit 032091b

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

Mod.Localizer/ContentProcessor/BuildPropertyProcessor.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using System.Reflection;
56
using dnlib.DotNet;
67
using Mod.Localizer.ContentFramework;
8+
using Mod.Localizer.Extensions;
9+
using Mod.Localizer.Resources;
710
using Newtonsoft.Json;
811
using Newtonsoft.Json.Serialization;
912
using Terraria;
@@ -20,19 +23,32 @@ public BuildPropertyProcessor(TmodFileWrapper.ITmodFile modFile, ModuleDef modMo
2023

2124
public override IReadOnlyList<Content> DumpContents()
2225
{
26+
var path = this.GetExtraDataPath();
2327
var json = _helper.Load(ModFile);
2428

25-
_helper.Write(ModFile, json);
29+
File.WriteAllText(path, json);
30+
Logger.Info(Strings.BuildPropWrittenToPath, path);
2631

2732
return new List<Content>();
2833
}
2934

3035
public override void PatchContents(IReadOnlyList<Content> contents)
3136
{
37+
var path = this.GetExtraDataPath();
38+
if (!File.Exists(path))
39+
{
40+
return;
41+
}
3242

43+
using (var sr = new StreamReader(File.OpenRead(path)))
44+
{
45+
var json = sr.ReadToEnd();
46+
47+
_helper.Write(ModFile, json);
48+
}
3349
}
3450

35-
private class BuildPropHelper
51+
private sealed class BuildPropHelper
3652
{
3753
public string Load(TmodFileWrapper.ITmodFile modFile)
3854
{
@@ -54,19 +70,23 @@ private object LoadRaw(TmodFileWrapper.ITmodFile modFile)
5470

5571
public void Write(TmodFileWrapper.ITmodFile modFile, string json)
5672
{
73+
// Creates a new blank information object from json data
5774
var obj = JsonConvert.DeserializeObject(json, _readBuildFile.DeclaringType, new JsonSerializerSettings
5875
{
5976
ContractResolver = new StringFieldContractResolver()
6077
});
6178

79+
// Loads the property object within current mod
6280
var info = LoadRaw(modFile);
6381

82+
// Replaces target fields
6483
foreach (var field in obj.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).Where(FieldSelector))
6584
{
6685
var replace = field.GetValue(obj);
6786
field.SetValue(info, replace);
6887
}
6988

89+
// Serializes the property object and store it inside mod file
7090
var data = (byte[])_toBytes.Invoke(info, new object[0]);
7191
modFile.Files["Info"] = data;
7292
}
@@ -84,6 +104,9 @@ public BuildPropHelper()
84104
private readonly MethodInfo _readBuildFile;
85105
private readonly MethodInfo _toBytes;
86106

107+
/// <summary>
108+
/// Resolver to include all private fields in <see cref="Terraria.ModLoader.BuildProperties"/>.
109+
/// </summary>
87110
private sealed class StringFieldContractResolver : DefaultContractResolver
88111
{
89112
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
@@ -97,6 +120,9 @@ protected override IList<JsonProperty> CreateProperties(Type type, MemberSeriali
97120
}
98121
}
99122

123+
/// <summary>
124+
/// Selects all fields of <see cref="Terraria.ModLoader.BuildProperties"/> that should be included in serialization.
125+
/// </summary>
100126
private static bool FieldSelector(FieldInfo f) => f.FieldType == typeof(string);
101127
}
102128
}

0 commit comments

Comments
 (0)