Skip to content

Commit

Permalink
Add removed hook messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Chicken-Bones committed Jun 9, 2022
1 parent 6d3a2b2 commit 67d30b9
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ void Method() {
#endif
Console.WriteLine(Item);
}

public override void FrameEffects(Player player, EquipType type) { /* Empty */ }

#if COMPILE_ERROR
public override bool DrawHead()/* tModPorter Suggestion: Removed. After registering this as EquipType.Head, use ArmorIDs.Head.Sets.DrawHead[slot] = false if you returned false */ { return true; /* Empty */ }
public override bool DrawHead()/* tModPorter Note: Removed. After registering this as EquipType.Head, use ArmorIDs.Head.Sets.DrawHead[slot] = false if you returned false */ { return true; /* Empty */ }

public override bool DrawBody()/* tModPorter Suggestion: Removed. After registering this as EquipType.Body, use ArmorIDs.Body.Sets.HidesTopSkin[Item.bodySlot] = true if you returned false */ { return true; /* Empty */ }
public override bool DrawBody()/* tModPorter Note: Removed. After registering this as EquipType.Body, use ArmorIDs.Body.Sets.HidesTopSkin[Item.bodySlot] = true if you returned false */ { return true; /* Empty */ }

public override bool DrawLegs()/* tModPorter Suggestion: Removed. After registering this as EquipType.Legs or Shoes, use ArmorIDs.Legs.Sets.HidesBottomSkin[slot] = true if you returned false for EquipType.Legs, and ArmorIDs.Shoe.Sets.OverridesLegs[slot] = true if you returned false for EquipType.Shoes */ { return true; /* Empty */ }
public override bool DrawLegs()/* tModPorter Note: Removed. After registering this as EquipType.Legs or Shoes, use ArmorIDs.Legs.Sets.HidesBottomSkin[slot] = true if you returned false for EquipType.Legs, and ArmorIDs.Shoe.Sets.OverridesLegs[slot] = true if you returned false for EquipType.Shoes */ { return true; /* Empty */ }

public override void DrawHands(ref bool drawHands, ref bool drawArms)/* tModPorter Suggestion: Removed. After registering this as EquipType.Body, use ArmorIDs.Body.Sets.HidesHands[slot] = false if you had drawHands set to true. If you had drawArms set to true, you don't need to do anything */ { /* Empty */ }
public override void DrawHands(ref bool drawHands, ref bool drawArms)/* tModPorter Note: Removed. After registering this as EquipType.Body, use ArmorIDs.Body.Sets.HidesHands[slot] = false if you had drawHands set to true. If you had drawArms set to true, you don't need to do anything */ { /* Empty */ }

public override void DrawHair(ref bool drawHair, ref bool drawAltHair)/* tModPorter Suggestion: Removed. After registering this as EquipType.Head, use ArmorIDs.Body.Sets.DrawFullHair[slot] = true if you had drawHair set to true, and ArmorIDs.Body.Sets.DrawHatHair[slot] = true if you had drawAltHair set to true */ { /* Empty */ }
public override void DrawHair(ref bool drawHair, ref bool drawAltHair)/* tModPorter Note: Removed. After registering this as EquipType.Head, use ArmorIDs.Body.Sets.DrawFullHair[slot] = true if you had drawHair set to true, and ArmorIDs.Body.Sets.DrawHatHair[slot] = true if you had drawAltHair set to true */ { /* Empty */ }
#endif
}

Expand Down
10 changes: 5 additions & 5 deletions tModPorter/tModPorter.Tests/TestData/GlobalItemTest.Expected.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public override void ModifyWeaponDamage(Item item, Player player, ref StatModifi
}

#if COMPILE_ERROR
public override bool DrawHead(int head)/* tModPorter Suggestion: Removed. In SetStaticDefaults, use ArmorIDs.Head.Sets.DrawHead[head] = false if you returned false. */ { return true; /* Empty */ }
public override bool DrawHead(int head)/* tModPorter Note: Removed. In SetStaticDefaults, use ArmorIDs.Head.Sets.DrawHead[head] = false if you returned false */ { return true; /* Empty */ }

public override bool DrawBody(int body)/* tModPorter Suggestion: Removed. In SetStaticDefaults, use ArmorIDs.Body.Sets.HidesTopSkin[body] = true if you returned false */ { return true; /* Empty */ }
public override bool DrawBody(int body)/* tModPorter Note: Removed. In SetStaticDefaults, use ArmorIDs.Body.Sets.HidesTopSkin[body] = true if you returned false */ { return true; /* Empty */ }

public override bool DrawLegs(int legs, int shoes)/* tModPorter Suggestion: Removed. In SetStaticDefaults, use ArmorIDs.Legs.Sets.HidesBottomSkin[legs] = true, and ArmorIDs.Shoe.Sets.OverridesLegs[shoes] = true */ { return true; /* Empty */ }
public override bool DrawLegs(int legs, int shoes)/* tModPorter Note: Removed. In SetStaticDefaults, use ArmorIDs.Legs.Sets.HidesBottomSkin[legs] = true, and ArmorIDs.Shoe.Sets.OverridesLegs[shoes] = true */ { return true; /* Empty */ }

public override void DrawHands(int body, ref bool drawHands, ref bool drawArms)/* tModPorter Suggestion: Removed. In SetStaticDefaults, use ArmorIDs.Body.Sets.HidesHands[body] = false if you had drawHands set to true. If you had drawArms set to true, you don't need to do anything */ { /* Empty */ }
public override void DrawHands(int body, ref bool drawHands, ref bool drawArms)/* tModPorter Note: Removed. In SetStaticDefaults, use ArmorIDs.Body.Sets.HidesHands[body] = false if you had drawHands set to true. If you had drawArms set to true, you don't need to do anything */ { /* Empty */ }

public override void DrawHair(int head, ref bool drawHair, ref bool drawAltHair)/* tModPorter Suggestion: Removed. In SetStaticDefaults, use ArmorIDs.Body.Sets.DrawFullHair[head] = true if you had drawHair set to true, and ArmorIDs.Body.Sets.DrawHatHair[head] = true if you had drawAltHair set to true */ { /* Empty */ }
public override void DrawHair(int head, ref bool drawHair, ref bool drawAltHair)/* tModPorter Note: Removed. In SetStaticDefaults, use ArmorIDs.Body.Sets.DrawFullHair[head] = true if you had drawHair set to true, and ArmorIDs.Body.Sets.DrawHatHair[head] = true if you had drawAltHair set to true */ { /* Empty */ }
#endif

public override void LoadData(Item item, TagCompound tag) { /* Empty */ }
Expand Down
10 changes: 5 additions & 5 deletions tModPorter/tModPorter.Tests/TestData/ModItemTest.Expected.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ public override void ModifyWeaponDamage(Player player, ref StatModifier damage)
}

#if COMPILE_ERROR
public override bool DrawHead()/* tModPorter Suggestion: Removed. In SetStaticDefaults, use ArmorIDs.Head.Sets.DrawHead[Item.headSlot] = false if you returned false */ { return true; /* Empty */ }
public override bool DrawHead()/* tModPorter Note: Removed. In SetStaticDefaults, use ArmorIDs.Head.Sets.DrawHead[Item.headSlot] = false if you returned false */ { return true; /* Empty */ }

public override bool DrawBody()/* tModPorter Suggestion: Removed. In SetStaticDefaults, use ArmorIDs.Body.Sets.HidesTopSkin[Item.bodySlot] = true if you returned false */ { return true; /* Empty */ }
public override bool DrawBody()/* tModPorter Note: Removed. In SetStaticDefaults, use ArmorIDs.Body.Sets.HidesTopSkin[Item.bodySlot] = true if you returned false */ { return true; /* Empty */ }

public override bool DrawLegs()/* tModPorter Suggestion: Removed. In SetStaticDefaults, use ArmorIDs.Legs.Sets.HidesBottomSkin[Item.legSlot] = true if you returned false for an accessory of EquipType.Legs, and ArmorIDs.Shoe.Sets.OverridesLegs[Item.shoeSlot] = true if you returned false for an accessory of EquipType.Shoes */ { return true; /* Empty */ }
public override bool DrawLegs()/* tModPorter Note: Removed. In SetStaticDefaults, use ArmorIDs.Legs.Sets.HidesBottomSkin[Item.legSlot] = true if you returned false for an accessory of EquipType.Legs, and ArmorIDs.Shoe.Sets.OverridesLegs[Item.shoeSlot] = true if you returned false for an accessory of EquipType.Shoes */ { return true; /* Empty */ }

public override void DrawHands(ref bool drawHands, ref bool drawArms)/* tModPorter Suggestion: Removed. In SetStaticDefaults, use ArmorIDs.Body.Sets.HidesHands[Item.bodySlot] = false if you had drawHands set to true. If you had drawArms set to true, you don't need to do anything */ { /* Empty */ }
public override void DrawHands(ref bool drawHands, ref bool drawArms)/* tModPorter Note: Removed. In SetStaticDefaults, use ArmorIDs.Body.Sets.HidesHands[Item.bodySlot] = false if you had drawHands set to true. If you had drawArms set to true, you don't need to do anything */ { /* Empty */ }

public override void DrawHair(ref bool drawHair, ref bool drawAltHair)/* tModPorter Suggestion: Removed. In SetStaticDefaults, use ArmorIDs.Body.Sets.DrawFullHair[Item.headSlot] = true if you had drawHair set to true, and ArmorIDs.Body.Sets.DrawHatHair[Item.headSlot] = true if you had drawAltHair set to true */ { /* Empty */ }
public override void DrawHair(ref bool drawHair, ref bool drawAltHair)/* tModPorter Note: Removed. In SetStaticDefaults, use ArmorIDs.Body.Sets.DrawFullHair[Item.headSlot] = true if you had drawHair set to true, and ArmorIDs.Body.Sets.DrawHatHair[Item.headSlot] = true if you had drawAltHair set to true */ { /* Empty */ }
#endif

public override void HorizontalWingSpeeds(Player player, ref float speed, ref float acceleration)/* tModPorter Suggestion: In SetStaticDefaults, needs to mirror values to ArmorIDs.Wing.Sets.Stats[Item.wingSlot] = new WingStats(...), otherwise wings won't work. Only use this hook for dynamic adjustments moving forward */ { /* Empty */ } // ModItem recommendation only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void IdentifierTest() {
Console.Write(BannerItem);

#if COMPILE_ERROR
Console.Write(bossBag/* tModPorter Suggestion: Removed. Spawn the treasure bag alongside other loot via npcLoot.Add(ItemDropRule.BossBag(type)) */);
Console.Write(bossBag/* tModPorter Note: Removed. Spawn the treasure bag alongside other loot via npcLoot.Add(ItemDropRule.BossBag(type)) */);
#endif
}

Expand Down
6 changes: 3 additions & 3 deletions tModPorter/tModPorter.Tests/TestData/ModTileTest.Expected.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ void Method() {
AnimationFrameHeight = 0;
AdjTiles = new int[0];

SetModTree(new ExampleTree())/* tModPorter Suggestion: Removed, assign GrowsOnTileId to this tile type in ModTree.SetStaticDefaults */;
SetModCactus(new ExampleCactus())/* tModPorter Suggestion: Removed, assign GrowsOnTileId to this tile type in ModCactus.SetStaticDefaults */;
SetModPalmTree(new ExamplePalmTree())/* tModPorter Suggestion: Removed, assign GrowsOnTileId to this tile type in ModPalmTree.SetStaticDefaults */;
SetModTree(new ExampleTree())/* tModPorter Note: Removed, assign GrowsOnTileId to this tile type in ModTree.SetStaticDefaults */;
SetModCactus(new ExampleCactus())/* tModPorter Note: Removed, assign GrowsOnTileId to this tile type in ModCactus.SetStaticDefaults */;
SetModPalmTree(new ExamplePalmTree())/* tModPorter Note: Removed, assign GrowsOnTileId to this tile type in ModPalmTree.SetStaticDefaults */;
#endif
}

Expand Down
19 changes: 18 additions & 1 deletion tModPorter/tModPorter/Config.ModLoader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using static tModPorter.Rewriters.RenameRewriter;
using static tModPorter.Rewriters.InvokeRewriter;
using static tModPorter.Rewriters.HookSignatureRewriter;
using static tModPorter.Rewriters.HookRewriter;

namespace tModPorter;

Expand All @@ -9,6 +9,7 @@ public static partial class Config
private static void AddModLoaderRefactors() {
RenameInstanceField("Terraria.ModLoader.ModType", from: "mod", to: "Mod");
RenameInstanceField("Terraria.ModLoader.ModItem", from: "item", to: "Item");
RenameInstanceField("Terraria.ModLoader.EquipTexture", from: "item", to: "Item");
RenameInstanceField("Terraria.ModLoader.ModNPC", from: "npc", to: "NPC");
RenameInstanceField("Terraria.ModLoader.ModPlayer", from: "player", to: "Player");
RenameInstanceField("Terraria.ModLoader.ModProjectile", from: "projectile", to: "Projectile");
Expand Down Expand Up @@ -109,6 +110,22 @@ private static void AddModLoaderRefactors() {
ChangeHookSignature("Terraria.ModLoader.GlobalTile", "PlaceInWorld");
ChangeHookSignature("Terraria.ModLoader.ModNPC", "SetNPCNameList", comment: "Suggestion: Return a list of names");

HookRemoved("Terraria.ModLoader.EquipTexture", "DrawHead", "After registering this as EquipType.Head, use ArmorIDs.Head.Sets.DrawHead[slot] = false if you returned false");
HookRemoved("Terraria.ModLoader.ModItem", "DrawHead", "In SetStaticDefaults, use ArmorIDs.Head.Sets.DrawHead[Item.headSlot] = false if you returned false");
HookRemoved("Terraria.ModLoader.GlobalItem", "DrawHead", "In SetStaticDefaults, use ArmorIDs.Head.Sets.DrawHead[head] = false if you returned false");
HookRemoved("Terraria.ModLoader.EquipTexture", "DrawBody", "After registering this as EquipType.Body, use ArmorIDs.Body.Sets.HidesTopSkin[Item.bodySlot] = true if you returned false");
HookRemoved("Terraria.ModLoader.ModItem", "DrawBody", "In SetStaticDefaults, use ArmorIDs.Body.Sets.HidesTopSkin[Item.bodySlot] = true if you returned false");
HookRemoved("Terraria.ModLoader.GlobalItem", "DrawBody", "In SetStaticDefaults, use ArmorIDs.Body.Sets.HidesTopSkin[body] = true if you returned false");
HookRemoved("Terraria.ModLoader.EquipTexture", "DrawLegs", "After registering this as EquipType.Legs or Shoes, use ArmorIDs.Legs.Sets.HidesBottomSkin[slot] = true if you returned false for EquipType.Legs, and ArmorIDs.Shoe.Sets.OverridesLegs[slot] = true if you returned false for EquipType.Shoes");
HookRemoved("Terraria.ModLoader.ModItem", "DrawLegs", "In SetStaticDefaults, use ArmorIDs.Legs.Sets.HidesBottomSkin[Item.legSlot] = true if you returned false for an accessory of EquipType.Legs, and ArmorIDs.Shoe.Sets.OverridesLegs[Item.shoeSlot] = true if you returned false for an accessory of EquipType.Shoes");
HookRemoved("Terraria.ModLoader.GlobalItem", "DrawLegs", "In SetStaticDefaults, use ArmorIDs.Legs.Sets.HidesBottomSkin[legs] = true, and ArmorIDs.Shoe.Sets.OverridesLegs[shoes] = true");
HookRemoved("Terraria.ModLoader.EquipTexture", "DrawHands", "After registering this as EquipType.Body, use ArmorIDs.Body.Sets.HidesHands[slot] = false if you had drawHands set to true. If you had drawArms set to true, you don't need to do anything");
HookRemoved("Terraria.ModLoader.ModItem", "DrawHands", "In SetStaticDefaults, use ArmorIDs.Body.Sets.HidesHands[Item.bodySlot] = false if you had drawHands set to true. If you had drawArms set to true, you don't need to do anything");
HookRemoved("Terraria.ModLoader.GlobalItem", "DrawHands", "In SetStaticDefaults, use ArmorIDs.Body.Sets.HidesHands[body] = false if you had drawHands set to true. If you had drawArms set to true, you don't need to do anything");
HookRemoved("Terraria.ModLoader.EquipTexture", "DrawHair", "After registering this as EquipType.Head, use ArmorIDs.Body.Sets.DrawFullHair[slot] = true if you had drawHair set to true, and ArmorIDs.Body.Sets.DrawHatHair[slot] = true if you had drawAltHair set to true");
HookRemoved("Terraria.ModLoader.ModItem", "DrawHair", "In SetStaticDefaults, use ArmorIDs.Body.Sets.DrawFullHair[Item.headSlot] = true if you had drawHair set to true, and ArmorIDs.Body.Sets.DrawHatHair[Item.headSlot] = true if you had drawAltHair set to true");
HookRemoved("Terraria.ModLoader.GlobalItem", "DrawHair", "In SetStaticDefaults, use ArmorIDs.Body.Sets.DrawFullHair[head] = true if you had drawHair set to true, and ArmorIDs.Body.Sets.DrawHatHair[head] = true if you had drawAltHair set to true");

RenameMethod("Terraria.ModLoader.ModItem", from: "Load", to: "LoadData");
RenameMethod("Terraria.ModLoader.ModItem", from: "Save", to: "SaveData");
RenameMethod("Terraria.ModLoader.GlobalItem", from: "Load", to: "LoadData");
Expand Down
2 changes: 1 addition & 1 deletion tModPorter/tModPorter/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static partial class Config
public static List<BaseRewriter> CreateRewriters() => new() {
new RenameRewriter(),
new InvokeRewriter(),
new HookSignatureRewriter(),
new HookRewriter(),
new RecipeRewriter(),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@

namespace tModPorter.Rewriters;

public class HookSignatureRewriter : BaseRewriter
public class HookRewriter : BaseRewriter
{
public class SignatureChange
internal delegate bool RewriteHook(ref MethodDeclarationSyntax node, IMethodSymbol sym, IMethodSymbol baseSym);

public class RefactorEntry
{
public string type { get; init; }
public string method { get; init; }
public string comment { get; init; }

internal Func<HookRewriter, RewriteHook> Rewrite { get; init; }
}

private static List<SignatureChange> refactors = new();
private static List<RefactorEntry> refactors = new();

public static void ChangeHookSignature(string type, string method, string comment = null) => refactors.Add(new() { type = type, method = method, comment = comment });
private static void AddRefactor(string type, string method, string comment, Func<HookRewriter, RewriteHook> rewrite) => refactors.Add(new() { type = type, method = method, comment = comment, Rewrite = rewrite });
public static void ChangeHookSignature(string type, string method, string comment = null) => AddRefactor(type, method, comment, rw => rw.RewriteSignature);
public static void HookRemoved(string type, string method, string comment) => AddRefactor(type, method, "Note: Removed. " + comment, rw => rw.NoteRemoved);

public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node) {
var sym = model.GetDeclaredSymbol(node);
Expand All @@ -33,8 +39,22 @@ public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node)
return node;

var baseSym = sym.OverriddenMethod ?? sym.ContainingType.BaseType.LookupMethod(refactor.method);
if (refactor.Rewrite(this)(ref node, sym, baseSym) && refactor.comment != null)
node = node.WithParameterList(node.ParameterList.WithBlockComment(refactor.comment));

return node;
}

private bool ReturnTypeMismatch(IMethodSymbol sym, IMethodSymbol baseSym) {
return !model.Compilation.ClassifyConversion(sym.ReturnType, baseSym.ReturnType).IsIdentity;
}

private static bool ParametersEqual(IMethodSymbol sym1, IMethodSymbol sym2) =>
sym1.Parameters.SequenceEqual(sym2.Parameters, (p1, p2) => SymbolEqualityComparer.Default.Equals(p1.Type, p2.Type) && p1.RefKind == p2.RefKind);

private bool RewriteSignature(ref MethodDeclarationSyntax node, IMethodSymbol sym, IMethodSymbol baseSym) {
if (baseSym == null)
return node;
return false;

var origNode = node;
if (!ParametersEqual(sym, baseSym)) {
Expand All @@ -45,17 +65,8 @@ public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node)
node = node.WithReturnType(UseType(baseSym.ReturnType).WithTriviaFrom(node.ReturnType));
}

if (node != origNode && refactor.comment != null && !node.ParameterList.GetTrailingTrivia().Any(SyntaxKind.MultiLineCommentTrivia)) {
node = node.WithParameterList(node.ParameterList.WithBlockComment(refactor.comment));
}

return node;
}

private bool ReturnTypeMismatch(IMethodSymbol sym, IMethodSymbol baseSym) {
return !model.Compilation.ClassifyConversion(sym.ReturnType, baseSym.ReturnType).IsIdentity;
return node != origNode;
}

private static bool ParametersEqual(IMethodSymbol sym1, IMethodSymbol sym2) =>
sym1.Parameters.SequenceEqual(sym2.Parameters, (p1, p2) => SymbolEqualityComparer.Default.Equals(p1.Type, p2.Type) && p1.RefKind == p2.RefKind);
private bool NoteRemoved(ref MethodDeclarationSyntax node, IMethodSymbol sym, IMethodSymbol baseSym) => baseSym == null;
}

0 comments on commit 67d30b9

Please sign in to comment.