Skip to content

Commit

Permalink
Added custom sprite icon.
Browse files Browse the repository at this point in the history
  • Loading branch information
Digitalroot committed Mar 12, 2023
1 parent d1aab3e commit e06dd53
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,20 @@
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
</PropertyGroup>

<ItemGroup>
<None Remove="Assets\Lightning_arrow.png" />
</ItemGroup>

<ItemGroup>
<Content Include=".nx\description.bbcode.txt" />
<Content Include=".ts\icon.png" />
<Content Include=".ts\manifest.json" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Assets\Lightning_arrow.png" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\docs\LICENSE" Link="LICENSE">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
45 changes: 45 additions & 0 deletions src/Digitalroot.Valheim.LightningArrowsJVL/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
using Jotunn.Utils;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using UnityEngine;

namespace Digitalroot.Valheim.LightningArrowsJVL
{
Expand Down Expand Up @@ -115,6 +117,7 @@ private void AddLightningArrow()
itemDrop.m_itemData.m_shared.m_damages.m_pierce = 20f;
itemDrop.m_itemData.m_shared.m_damages.m_lightning = 40f;
itemDrop.m_itemData.m_shared.m_damages.m_spirit = 0f;
itemDrop.m_itemData.m_shared.m_icons[0] = LoadResourceIcon("Lightning_arrow");

ItemManager.Instance.AddItem(customItem);
}
Expand All @@ -124,6 +127,48 @@ private void AddLightningArrow()
}
}

private static Sprite LoadResourceIcon(string name)
{
Log.Trace(Instance, $"{Namespace}.{MethodBase.GetCurrentMethod()?.DeclaringType?.Name}.{MethodBase.GetCurrentMethod()?.Name}");
return LoadSpriteFromTexture(LoadTextureRaw(GetResource(Assembly.GetCallingAssembly(), $"Digitalroot.Valheim.LightningArrowsJVL.Assets.{name}.png")));
}

private static Texture2D LoadTextureRaw(byte[] file)
{
if (file.Any())
{
Texture2D texture2D = new Texture2D(2, 2);
bool flag2 = texture2D.LoadImage(file);
if (flag2)
{
return texture2D;
}
}

return null;
}

private static Sprite LoadSpriteFromTexture(Texture2D spriteTexture, float pixelsPerUnit = 100f)
{
return spriteTexture ? Sprite.Create(spriteTexture, new Rect(0f, 0f, spriteTexture.width, spriteTexture.height), new Vector2(0f, 0f), pixelsPerUnit) : null;
}

private static byte[] GetResource(Assembly asm, string resourceName)
{
Log.Trace(Instance, $"{Namespace}.{MethodBase.GetCurrentMethod()?.DeclaringType?.Name}.{MethodBase.GetCurrentMethod()?.Name}");
var manifestResourceStream = asm.GetManifestResourceStream(resourceName);
Log.Trace(Instance, $"[{MethodBase.GetCurrentMethod()?.DeclaringType?.Name}.{MethodBase.GetCurrentMethod()?.Name}] manifestResourceStream == null : {manifestResourceStream == null}");
if (manifestResourceStream == null)
{
throw new Exception($"Unable to load the manifestResourceStream from {asm.FullName} for {resourceName}");
}
Log.Trace(Instance, $"[{MethodBase.GetCurrentMethod()?.DeclaringType?.Name}.{MethodBase.GetCurrentMethod()?.Name}] manifestResourceStream.Length : {manifestResourceStream.Length}");
var array = new byte[manifestResourceStream.Length];
var _ = manifestResourceStream.Read(array, 0, (int) manifestResourceStream.Length);
Log.Trace(Instance, $"[{MethodBase.GetCurrentMethod()?.DeclaringType?.Name}.{MethodBase.GetCurrentMethod()?.Name}] array.Length : {array.Length}");
return array;
}

#region Implementation of ITraceableLogging

/// <inheritdoc />
Expand Down

0 comments on commit e06dd53

Please sign in to comment.