Skip to content

Commit 045ae36

Browse files
committed
Fix mod loading problem (signature is not correct)
* Use `AssemblyRef` of module * Remove instance field of mod * Make `SetTranslation` instance instead of static * Implement `SetTranslation` call * IL changes * Format codes
1 parent 1d89f04 commit 045ae36

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

ModLocalizer/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414

1515
[assembly: Guid("613f981d-c5dd-43d1-b0ff-057169a647c4")]
1616

17-
[assembly: AssemblyVersion("0.8.0.0")]
18-
[assembly: AssemblyFileVersion("0.8.0.0")]
17+
[assembly: AssemblyVersion("0.9.0.0")]
18+
[assembly: AssemblyFileVersion("0.9.0.0")]

ModLocalizer/TranslationEmitter.cs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ public TranslationEmitter(ModuleDef module, string gameCulture, string modName)
2121

2222
var importer = new Importer(Module);
2323

24-
_modTranslationType = new TypeRefUser(Module, "Terraria.ModLoader", "ModTranslation", _terraria);
24+
var terraria = module.GetAssemblyRef("Terraria") ?? new AssemblyRefUser("Terraria", new Version(1, 3, 5, 1));
25+
_modTranslationType = new TypeRefUser(Module, "Terraria.ModLoader", "ModTranslation", terraria);
2526

2627
_modTranslationSetDefaultMethod = new MemberRefUser(Module, "SetDefault",
2728
MethodSig.CreateInstance(Module.CorLibTypes.Void, Module.CorLibTypes.String),
2829
_modTranslationType);
2930

30-
var gameCultureType = new TypeRefUser(Module, "Terraria.Localization", "GameCulture", _terraria);
31+
var gameCultureType = new TypeRefUser(Module, "Terraria.Localization", "GameCulture", terraria);
3132
_modTranslationAddTranslationMethod = new MemberRefUser(Module, "AddTranslation",
3233
MethodSig.CreateInstance(Module.CorLibTypes.Void, new ClassSig(gameCultureType), Module.CorLibTypes.String),
3334
_modTranslationType);
@@ -36,22 +37,19 @@ public TranslationEmitter(ModuleDef module, string gameCulture, string modName)
3637
new FieldSig(new ClassSig(gameCultureType)),
3738
gameCultureType);
3839

39-
var languageType = new TypeRefUser(Module, "Terraria.Localization", "Language", _terraria);
40+
var languageType = new TypeRefUser(Module, "Terraria.Localization", "Language", terraria);
4041
_getTextValueMethod = new MemberRefUser(Module, "GetTextValue", MethodSig.CreateStatic(Module.CorLibTypes.String, Module.CorLibTypes.String), languageType);
4142

42-
var modType = new TypeRefUser(Module, "Terraria.ModLoader", "Mod", _terraria);
43+
var modType = new TypeRefUser(Module, "Terraria.ModLoader", "Mod", terraria);
4344

4445
_modAddTranslationMethod = new MemberRefUser(Module, "AddTranslation", MethodSig.CreateInstance(Module.CorLibTypes.Void, new ClassSig(_modTranslationType)), modType);
4546
_modCreateTranslationMethod = new MemberRefUser(Module, "CreateTranslation", MethodSig.CreateInstance(new ClassSig(_modTranslationType), Module.CorLibTypes.String), modType);
4647

4748
var type = Module.Types.Single(x => string.Equals(x.BaseType?.FullName, "Terraria.ModLoader.Mod",
4849
StringComparison.Ordinal));
49-
_modInstanceField = new FieldDefUser(ModInstanceFieldName, new FieldSig(new ClassSig(type)), FieldAttributes.Assembly | FieldAttributes.Static);
50-
type.Fields.Add(_modInstanceField);
5150
var ctor = importer.Import(typeof(CompilerGeneratedAttribute).GetConstructor(new Type[0])) as IMethodDefOrRef;
52-
_modInstanceField.CustomAttributes.Add(new CustomAttribute(ctor));
5351

54-
_modSetTranslationMethod = new MethodDefUser(ModSetTranslationMethod, MethodSig.CreateStatic(Module.CorLibTypes.Void));
52+
_modSetTranslationMethod = new MethodDefUser(ModSetTranslationMethod, MethodSig.CreateInstance(Module.CorLibTypes.Void), MethodAttributes.Private);
5553
type.Methods.Add(_modSetTranslationMethod);
5654

5755
_modSetTranslationMethod.CustomAttributes.Add(new CustomAttribute(ctor));
@@ -60,6 +58,20 @@ public TranslationEmitter(ModuleDef module, string gameCulture, string modName)
6058
Instructions = { OpCodes.Ret.ToInstruction() },
6159
Variables = { new Local(new ClassSig(_modTranslationType), "translation", 0) }
6260
};
61+
62+
var loadMethod = type.FindMethod("Load", MethodSig.CreateInstance(Module.CorLibTypes.Void));
63+
if (loadMethod?.HasBody != true)
64+
{
65+
Console.WriteLine("Could not find Mod.Load() method; miscs translations will not be added.");
66+
return;
67+
}
68+
69+
var instructions = loadMethod.Body.Instructions;
70+
instructions.Insert(instructions.Count - 1, new[]
71+
{
72+
OpCodes.Ldarg_0.ToInstruction(),
73+
OpCodes.Call.ToInstruction(_modSetTranslationMethod)
74+
});
6375
}
6476

6577
public void Emit(MethodDef method, string propertyName, string content)
@@ -126,9 +138,9 @@ private string AddTranslation(string key, string @default, string content)
126138

127139
instructions.Insert(instructions.Count - 1, new[]
128140
{
129-
OpCodes.Ldsfld.ToInstruction(_modInstanceField),
141+
OpCodes.Ldarg_0.ToInstruction(),
130142
OpCodes.Ldstr.ToInstruction(key),
131-
OpCodes.Callvirt.ToInstruction(_modCreateTranslationMethod),
143+
OpCodes.Call.ToInstruction(_modCreateTranslationMethod),
132144
OpCodes.Stloc_0.ToInstruction(),
133145

134146
OpCodes.Ldloc_0.ToInstruction(),
@@ -140,9 +152,9 @@ private string AddTranslation(string key, string @default, string content)
140152
OpCodes.Ldstr.ToInstruction(content),
141153
OpCodes.Callvirt.ToInstruction(_modTranslationAddTranslationMethod),
142154

143-
OpCodes.Ldsfld.ToInstruction(_modInstanceField),
155+
OpCodes.Ldarg_0.ToInstruction(),
144156
OpCodes.Ldloc_0.ToInstruction(),
145-
OpCodes.Callvirt.ToInstruction(_modAddTranslationMethod)
157+
OpCodes.Call.ToInstruction(_modAddTranslationMethod)
146158
});
147159

148160
return string.Format("Mods.{0}.{1}", _modName, key);
@@ -161,19 +173,16 @@ private int GetIndex(string typeName)
161173
return _index[typeName]++;
162174
}
163175

164-
private readonly AssemblyRefUser _terraria = new AssemblyRefUser("Terraria", new Version(1, 3, 5, 1));
165176
private readonly TypeRefUser _modTranslationType;
166177

167178
private readonly MemberRefUser _modTranslationAddTranslationMethod, _modTranslationSetDefaultMethod;
168179
private readonly MemberRefUser _gameCultureField, _getTextValueMethod;
169180
private readonly MemberRefUser _modAddTranslationMethod, _modCreateTranslationMethod;
170181

171-
private readonly FieldDef _modInstanceField;
172182
private readonly MethodDef _modSetTranslationMethod;
173183

174184
private readonly string _modName;
175185

176-
private const string ModInstanceFieldName = "Localizer_instance<>";
177186
private const string ModSetTranslationMethod = "Localizer_setTranslation<>";
178187
}
179188
}

0 commit comments

Comments
 (0)