1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . IO ;
3
4
using System . Linq ;
4
5
using System . Reflection ;
5
6
using dnlib . DotNet ;
6
7
using Mod . Localizer . ContentFramework ;
8
+ using Mod . Localizer . Extensions ;
9
+ using Mod . Localizer . Resources ;
7
10
using Newtonsoft . Json ;
8
11
using Newtonsoft . Json . Serialization ;
9
12
using Terraria ;
@@ -20,19 +23,32 @@ public BuildPropertyProcessor(TmodFileWrapper.ITmodFile modFile, ModuleDef modMo
20
23
21
24
public override IReadOnlyList < Content > DumpContents ( )
22
25
{
26
+ var path = this . GetExtraDataPath ( ) ;
23
27
var json = _helper . Load ( ModFile ) ;
24
28
25
- _helper . Write ( ModFile , json ) ;
29
+ File . WriteAllText ( path , json ) ;
30
+ Logger . Info ( Strings . BuildPropWrittenToPath , path ) ;
26
31
27
32
return new List < Content > ( ) ;
28
33
}
29
34
30
35
public override void PatchContents ( IReadOnlyList < Content > contents )
31
36
{
37
+ var path = this . GetExtraDataPath ( ) ;
38
+ if ( ! File . Exists ( path ) )
39
+ {
40
+ return ;
41
+ }
32
42
43
+ using ( var sr = new StreamReader ( File . OpenRead ( path ) ) )
44
+ {
45
+ var json = sr . ReadToEnd ( ) ;
46
+
47
+ _helper . Write ( ModFile , json ) ;
48
+ }
33
49
}
34
50
35
- private class BuildPropHelper
51
+ private sealed class BuildPropHelper
36
52
{
37
53
public string Load ( TmodFileWrapper . ITmodFile modFile )
38
54
{
@@ -54,19 +70,23 @@ private object LoadRaw(TmodFileWrapper.ITmodFile modFile)
54
70
55
71
public void Write ( TmodFileWrapper . ITmodFile modFile , string json )
56
72
{
73
+ // Creates a new blank information object from json data
57
74
var obj = JsonConvert . DeserializeObject ( json , _readBuildFile . DeclaringType , new JsonSerializerSettings
58
75
{
59
76
ContractResolver = new StringFieldContractResolver ( )
60
77
} ) ;
61
78
79
+ // Loads the property object within current mod
62
80
var info = LoadRaw ( modFile ) ;
63
81
82
+ // Replaces target fields
64
83
foreach ( var field in obj . GetType ( ) . GetFields ( BindingFlags . NonPublic | BindingFlags . Public | BindingFlags . Instance ) . Where ( FieldSelector ) )
65
84
{
66
85
var replace = field . GetValue ( obj ) ;
67
86
field . SetValue ( info , replace ) ;
68
87
}
69
88
89
+ // Serializes the property object and store it inside mod file
70
90
var data = ( byte [ ] ) _toBytes . Invoke ( info , new object [ 0 ] ) ;
71
91
modFile . Files [ "Info" ] = data ;
72
92
}
@@ -84,6 +104,9 @@ public BuildPropHelper()
84
104
private readonly MethodInfo _readBuildFile ;
85
105
private readonly MethodInfo _toBytes ;
86
106
107
+ /// <summary>
108
+ /// Resolver to include all private fields in <see cref="Terraria.ModLoader.BuildProperties"/>.
109
+ /// </summary>
87
110
private sealed class StringFieldContractResolver : DefaultContractResolver
88
111
{
89
112
protected override IList < JsonProperty > CreateProperties ( Type type , MemberSerialization memberSerialization )
@@ -97,6 +120,9 @@ protected override IList<JsonProperty> CreateProperties(Type type, MemberSeriali
97
120
}
98
121
}
99
122
123
+ /// <summary>
124
+ /// Selects all fields of <see cref="Terraria.ModLoader.BuildProperties"/> that should be included in serialization.
125
+ /// </summary>
100
126
private static bool FieldSelector ( FieldInfo f ) => f . FieldType == typeof ( string ) ;
101
127
}
102
128
}
0 commit comments