From 4e4b3fd0b5bfcf30abf8076efcba7eea7a13de24 Mon Sep 17 00:00:00 2001 From: Gamrguy Date: Sun, 2 Apr 2017 21:57:52 -0400 Subject: [PATCH] Big, uh, updatey-things --- Dyes/ModDyePlayer.cs | 55 +++++++++++++++++++++++++++++++++-- ShaderLib-WINDOWS.csproj | 36 +++++++++++------------ ShaderLib-WINDOWS.userprefs | 15 ++++++---- ShaderLib.userprefs | 33 ++++++--------------- ShaderLibMod.cs | 43 ++++++++++++++++++++++++++- Shaders/ItemShader.cs | 4 +-- Shaders/ModArmorShaderData.cs | 33 ++++++++++++--------- Shaders/NPCShader.cs | 2 +- Shaders/ProjectileShader.cs | 2 +- Shaders/ShaderIO.cs | 4 +-- 10 files changed, 156 insertions(+), 71 deletions(-) mode change 100755 => 100644 ShaderLib-WINDOWS.csproj diff --git a/Dyes/ModDyePlayer.cs b/Dyes/ModDyePlayer.cs index 7833028..9602b66 100755 --- a/Dyes/ModDyePlayer.cs +++ b/Dyes/ModDyePlayer.cs @@ -1,13 +1,18 @@ using System; +using System.Collections.Generic; +using System.Linq; using Terraria; +using Terraria.DataStructures; using Terraria.Graphics.Shaders; using Terraria.ModLoader; -using ShaderLib.Dyes; namespace ShaderLib.Dyes { public class ModDyePlayer : ModPlayer - { + { + internal static List> heldItemHooks; + internal static List> heldItemFlameHooks; + public override void ModifyDrawInfo(ref PlayerDrawInfo drawInfo) { //Don't ask. This is the vanilla code that applies dye to the appropriate equip types. @@ -189,6 +194,52 @@ public override void PreUpdateBuffs() player.cYorai = player.cPet; } + public PlayerLayer heldItemLayer = new PlayerLayer("ShaderLib", "HeldItemShader", (PlayerDrawInfo drawInfo) => { + Player player = drawInfo.drawPlayer; + + //Selects the held mouse item if it's the client-side player AND is holding one; selected inventory item otherwise + Item heldItem = (Main.myPlayer == player.whoAmI) && Main.mouseItem.active && !Main.mouseItem.IsAir && Main.mouseItem.type > 0 ? Main.mouseItem : player.inventory[player.selectedItem]; + + //Don't cause crashes :-) + if(heldItem == null || !heldItem.active || heldItem.IsAir) return; + int index = Main.playerDrawData.Count - 1; + + //Don't bother with items that aren't showing (e.g. Arkhalis) + if(heldItem.noUseGraphic) return; + + //Handling for applying shaders to flame textures + if(heldItem.flame) { + int flameShaderID = 0; + heldItemHooks.ForEach((hook) => { flameShaderID = hook(flameShaderID, heldItem, player); }); + + if(player.itemAnimation > 0 && !player.pulley && flameShaderID > 0) { + DrawData data = Main.playerDrawData[index]; + data.shader = flameShaderID; + Main.playerDrawData[index] = data; + } + + index -= 1; + } + + //Handling for applying shaders to the held item itself + int shaderID = 0; + heldItemHooks.ForEach((hook) => { shaderID = hook(shaderID, heldItem, player); }); + + //Only use shader when player is using/holding an item + if(player.itemAnimation > 0 && !player.pulley && shaderID > 0) { + DrawData data = Main.playerDrawData[index]; + data.shader = shaderID; + Main.playerDrawData[index] = data; + } + }); + + public override void ModifyDrawLayers(List layers) + { + if(!Main.gameMenu) { + layers.Insert(layers.IndexOf(PlayerLayer.HeldItem) + 1, heldItemLayer); + } + } + private int GetShaderID(Item i){ /*if(i.GetModInfo(mod).fakeItemID < 0) { return GameShaders.Armor.GetShaderIdFromItemId(i.GetModInfo(mod).fakeItemID); diff --git a/ShaderLib-WINDOWS.csproj b/ShaderLib-WINDOWS.csproj old mode 100755 new mode 100644 index ae58fac..92570fd --- a/ShaderLib-WINDOWS.csproj +++ b/ShaderLib-WINDOWS.csproj @@ -56,28 +56,9 @@ - - - ..\..\..\..\Steam\steamapps\common\Terraria\ModCompile\Microsoft.Xna.Framework.dll - - - ..\..\..\..\Steam\steamapps\common\Terraria\ModCompile\Microsoft.Xna.Framework.Game.dll - - - ..\..\..\..\Steam\steamapps\common\Terraria\ModCompile\Microsoft.Xna.Framework.Graphics.dll - - - ..\..\..\..\Steam\steamapps\common\Terraria\ModCompile\Microsoft.Xna.Framework.Xact.dll - - - ..\..\..\..\..\..\Downloads\tModLoader-1.3.4\ModCompile\tModLoaderWindows.exe - - - - @@ -90,4 +71,21 @@ + + + ..\..\..\..\Steam\steamapps\common\Terraria\Terraria.app\Contents\MacOS\ModCompile\tModLoaderWindows.exe + + + ..\..\..\..\Steam\steamapps\common\Terraria\Terraria.app\Contents\MacOS\ModCompile\Microsoft.Xna.Framework.dll + + + ..\..\..\..\Steam\steamapps\common\Terraria\Terraria.app\Contents\MacOS\ModCompile\Microsoft.Xna.Framework.Game.dll + + + ..\..\..\..\Steam\steamapps\common\Terraria\Terraria.app\Contents\MacOS\ModCompile\Microsoft.Xna.Framework.Graphics.dll + + + ..\..\..\..\Steam\steamapps\common\Terraria\Terraria.app\Contents\MacOS\ModCompile\Microsoft.Xna.Framework.Xact.dll + + \ No newline at end of file diff --git a/ShaderLib-WINDOWS.userprefs b/ShaderLib-WINDOWS.userprefs index 3027294..dc4acca 100755 --- a/ShaderLib-WINDOWS.userprefs +++ b/ShaderLib-WINDOWS.userprefs @@ -1,15 +1,18 @@  - - + - - - - + + + + + + + + \ No newline at end of file diff --git a/ShaderLib.userprefs b/ShaderLib.userprefs index 4c16052..dc4acca 100755 --- a/ShaderLib.userprefs +++ b/ShaderLib.userprefs @@ -1,28 +1,13 @@  - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + diff --git a/ShaderLibMod.cs b/ShaderLibMod.cs index fc2bb27..c372c61 100755 --- a/ShaderLibMod.cs +++ b/ShaderLibMod.cs @@ -49,6 +49,7 @@ public override void Load() ItemShader.preDrawInv = new List>(); ItemShader.preDrawWorld = new List>(); NPCShader.hooks = new List>(); + Dyes.ModDyePlayer.heldItemHooks = new List>(); instance = this; } @@ -96,7 +97,7 @@ public ModArmorShaderData GetModShaderByNames(Tuple data) { /// Shader name. /// ModArmorShaderData object. /// Item ID to associate with this shader (use for dyes). - public int RegisterArmorShader(string modName, string shaderName, ModArmorShaderData data, int itemID = -1) { + public int RegisterArmorShader(string modName, string shaderName, T data, int itemID = -1) where T : ModArmorShaderData { int shaderID; if(itemID > 0) { ShaderReflections.BindArmorShaderWithID(itemID, data); @@ -127,5 +128,45 @@ public int RegisterMetaArmorShader(MetaArmorShaderData data) { metaShaderRegistry.Add(shaderID, data); return shaderID; } + + //Hook setter methods, ignores server so mods don't have to :-) + //Also acts as security; no hacking the hook lists! + public static void AddProjectileShaderHook(Func hook) { + if(Main.netMode != NetmodeID.Server) { + ProjectileShader.hooks.Add(hook); + } + } + + public static void AddNPCShaderHook(Func hook) { + if(Main.netMode != NetmodeID.Server) { + NPCShader.hooks.Add(hook); + } + } + + public static void AddItemShaderInventoryHook(Func hook) { + if(Main.netMode != NetmodeID.Server) { + ItemShader.preDrawInv.Add(hook); + } + } + + public static void AddItemShaderWorldHook(Func hook) { + if(Main.netMode != NetmodeID.Server) { + ItemShader.preDrawWorld.Add(hook); + } + } + + public static void AddHeldItemShaderHook(Func hook) + { + if(Main.netMode != NetmodeID.Server) { + Dyes.ModDyePlayer.heldItemHooks.Add(hook); + } + } + + public static void AddHeldItemShaderFlameHook(Func hook) + { + if(Main.netMode != NetmodeID.Server) { + Dyes.ModDyePlayer.heldItemFlameHooks.Add(hook); + } + } } } \ No newline at end of file diff --git a/Shaders/ItemShader.cs b/Shaders/ItemShader.cs index f4155bc..f0665c5 100755 --- a/Shaders/ItemShader.cs +++ b/Shaders/ItemShader.cs @@ -11,8 +11,8 @@ namespace ShaderLib.Shaders { public class ItemShader : GlobalItem { - public static List> preDrawInv; - public static List> preDrawWorld; + internal static List> preDrawInv; + internal static List> preDrawWorld; public override bool PreDrawInInventory(Item item, SpriteBatch spriteBatch, Vector2 position, Rectangle frame, Color drawColor, Color itemColor, Vector2 origin, float scale) { diff --git a/Shaders/ModArmorShaderData.cs b/Shaders/ModArmorShaderData.cs index 64bb733..15f4c06 100755 --- a/Shaders/ModArmorShaderData.cs +++ b/Shaders/ModArmorShaderData.cs @@ -47,17 +47,7 @@ public class ModArmorShaderData : ArmorShaderData public Func SecondaryShader; public ModArmorShaderData(Ref shader, string passName) : base(shader, passName){ - //Set default delegates if not set by user - if(UpdatePrimary == null) { - UpdatePrimary = new Func(delegate(Entity e, DrawData? drawData) { - return primary; - }); - } - if(UpdateSecondary == null) { - UpdateSecondary = new Func(delegate(Entity e, DrawData? drawData) { - return secondary; - }); - } + //Set default delegate if not set by user if(SecondaryShader == null) { SecondaryShader = new Func(delegate(Entity e) { return this; @@ -67,8 +57,8 @@ public ModArmorShaderData(Ref shader, string passName) : base(shader, pa public override void Apply(Entity e, DrawData? drawData) { - UseColor(UpdatePrimary(e, drawData)); - UseSecondaryColor(UpdateSecondary(e, drawData)); + if(UpdatePrimary != null) UseColor(UpdatePrimary(e, drawData)); + if(UpdateSecondary != null) UseSecondaryColor(UpdateSecondary(e, drawData)); UseSaturation(saturation); if(image != null) ShaderReflections.SetImage(this as ArmorShaderData, new Ref(image)); SwapProgram(_passName); @@ -86,5 +76,22 @@ public override ArmorShaderData GetSecondaryShader(Entity entity) public string GetPassName() { return _passName; } + + //New methods to replace the old ones, no more "as ModArmorShaderData" being appended all the time + new public ModArmorShaderData UseColor(Color color) + { + return base.UseColor(color) as ModArmorShaderData; + } + + new public ModArmorShaderData UseSecondaryColor(Color color) + { + return base.UseSecondaryColor(color) as ModArmorShaderData; + } + + new public ModArmorShaderData UseSaturation(float sat) + { + return base.UseSaturation(sat) as ModArmorShaderData; + } + } } \ No newline at end of file diff --git a/Shaders/NPCShader.cs b/Shaders/NPCShader.cs index fefe79e..014b7cd 100755 --- a/Shaders/NPCShader.cs +++ b/Shaders/NPCShader.cs @@ -12,7 +12,7 @@ namespace ShaderLib.Shaders { public class NPCShader : GlobalNPC { - public static List> hooks; + internal static List> hooks; public override bool PreDraw(NPC npc, SpriteBatch spriteBatch, Color drawColor) { int shaderID = 0; diff --git a/Shaders/ProjectileShader.cs b/Shaders/ProjectileShader.cs index 68a562c..0379ef1 100755 --- a/Shaders/ProjectileShader.cs +++ b/Shaders/ProjectileShader.cs @@ -18,7 +18,7 @@ public class ProjectileShader : GlobalProjectile /// /// List of hooks for changing shader on projectiles. /// - public static List> hooks; + internal static List> hooks; public override bool PreDraw(Projectile projectile, SpriteBatch spriteBatch, Color lightColor) { diff --git a/Shaders/ShaderIO.cs b/Shaders/ShaderIO.cs index d6223dc..d3357a2 100755 --- a/Shaders/ShaderIO.cs +++ b/Shaders/ShaderIO.cs @@ -110,8 +110,7 @@ public static int ReadArmorShader(BinaryReader reader){ /// /// The extracted shader ID. /// TagCompound to extract the shader from. - public static int LoadShader(TagCompound compound) { - var savedShader = compound.GetCompound("SavedShader"); + public static int LoadShader(TagCompound savedShader) { string shaderType = savedShader.GetString("Type"); //Load vanilla shader; easily identified by unchanging shader ID @@ -179,6 +178,7 @@ public static TagCompound SaveShader(int shaderID) { if(shaderID <= ShaderLibMod.maxVanillaID) { compound.Set("Type", "Vanilla"); compound.Set("ShaderID", shaderID); + ErrorLogger.Log("Saving shader of type Vanilla"); } //Save mod name and shader name if modded shader