From fb7b878b829b37d4e37aacc5285b5eb254213a65 Mon Sep 17 00:00:00 2001 From: Joey Parrish Date: Thu, 16 Jun 2022 08:42:00 -0700 Subject: [PATCH] Overhaul asset location and packaging R2modman installs should now be fixed. Hopefully also works in Thunderstore and Vortex, which I cannot yet test. Closes #1 --- Pokeheim/BallItem.cs | 16 ++++++++-------- Pokeheim/InventoryMods.cs | 4 ++-- Pokeheim/MonsterMetadata.cs | 12 +++++------- Pokeheim/MusicMods.cs | 4 ++-- Pokeheim/PlayerMods.cs | 4 ++-- Pokeheim/Pokeheim.cs | 6 ++---- Pokeheim/Utils.cs | 31 +++++++++++++++++++++++++++++++ docs/Install.md | 4 +++- scripts/build-and-launch.sh | 4 +++- scripts/install-mod.sh | 17 ++++++++++++----- scripts/package.sh | 12 ++++++------ 11 files changed, 76 insertions(+), 38 deletions(-) diff --git a/Pokeheim/BallItem.cs b/Pokeheim/BallItem.cs index f81c69a..39c1872 100644 --- a/Pokeheim/BallItem.cs +++ b/Pokeheim/BallItem.cs @@ -48,12 +48,12 @@ private struct BallConfig { private static Dictionary BallConfigs = new Dictionary { { "Pokeball", new BallConfig { - TexturePath = "Pokeheim/Assets/Monster ball texture.png", + TexturePath = "Monster ball texture.png", BallFactor = 1.0f, ItemConfig = new ItemConfig { Name = "$item_pokeball", Description = "$item_pokeball_description", - Icons = new []{ AssetUtils.LoadSpriteFromFile("Pokeheim/Assets/Monster ball.png") }, + Icons = new []{ Utils.LoadSprite("Monster ball.png") }, Amount = 10, Requirements = new[] { new RequirementConfig { @@ -68,12 +68,12 @@ private struct BallConfig { }, } }, { "Greatball", new BallConfig { - TexturePath = "Pokeheim/Assets/Super monster ball texture.png", + TexturePath = "Super monster ball texture.png", BallFactor = 1.5f, ItemConfig = new ItemConfig { Name = "$item_greatball", Description = "$item_greatball_description", - Icons = new []{ AssetUtils.LoadSpriteFromFile("Pokeheim/Assets/Super monster ball.png") }, + Icons = new []{ Utils.LoadSprite("Super monster ball.png") }, Amount = 10, Requirements = new[] { new RequirementConfig { @@ -88,12 +88,12 @@ private struct BallConfig { }, } }, { "Ultraball", new BallConfig { - TexturePath = "Pokeheim/Assets/Ultra monster ball texture.png", + TexturePath = "Ultra monster ball texture.png", BallFactor = 2.0f, ItemConfig = new ItemConfig { Name = "$item_ultraball", Description = "$item_ultraball_description", - Icons = new []{ AssetUtils.LoadSpriteFromFile("Pokeheim/Assets/Ultra monster ball.png") }, + Icons = new []{ Utils.LoadSprite("Ultra monster ball.png") }, Amount = 10, Requirements = new[] { new RequirementConfig { @@ -156,7 +156,7 @@ private static void AddBalls() { Identifier = "training", Name = "$skill_monster_training", Description = "$skill_monster_training_description", - Icon = AssetUtils.LoadSpriteFromFile("Pokeheim/Assets/Skill icon.png"), + Icon = Utils.LoadSprite("Skill icon.png"), IncreaseStep = 1f, }); @@ -202,7 +202,7 @@ private static void ApplyBallStyle(GameObject prefab, string texturePath) { ReplaceMesh(prefab, BallMesh, scale, rotation, translation); // Replace the texture, as well. - var newTexture = AssetUtils.LoadTexture(texturePath); + var newTexture = Utils.LoadTexture(texturePath); ReplaceTexture(prefab, newTexture); } diff --git a/Pokeheim/InventoryMods.cs b/Pokeheim/InventoryMods.cs index 796e7f7..efae653 100644 --- a/Pokeheim/InventoryMods.cs +++ b/Pokeheim/InventoryMods.cs @@ -26,7 +26,7 @@ namespace Pokeheim { public class InventoryMods { - private const string SortIconPath = "Pokeheim/Assets/Sort icon.png"; + private const string SortIconPath = "Sort icon.png"; [HarmonyPatch(typeof(Humanoid), nameof(Humanoid.Awake))] class LargeInventory_Patch { @@ -125,7 +125,7 @@ static void Postfix(InventoryGui __instance) { armorText.gameObject.SetActive(false); // Replace the icon. - armorIcon.sprite = AssetUtils.LoadSpriteFromFile(SortIconPath); + armorIcon.sprite = Utils.LoadSprite(SortIconPath); // Center the icon. armorIcon.rectTransform.anchoredPosition = new Vector2(0.5f, 0.5f); diff --git a/Pokeheim/MonsterMetadata.cs b/Pokeheim/MonsterMetadata.cs index 6952b4c..45c0f41 100644 --- a/Pokeheim/MonsterMetadata.cs +++ b/Pokeheim/MonsterMetadata.cs @@ -202,10 +202,8 @@ public static class MonsterMetadata { private static Dictionary MonsterMap; - private static readonly string OverlayPath = - "Pokeheim/Assets/Inhabited-overlay.png"; - private static readonly string PremadeIconPrefix = - "Pokeheim/Assets/PremadeIcon-"; + private static readonly string OverlayPath = "Inhabited-overlay.png"; + private static readonly string PremadeIconPrefix = "PremadeIcon-"; private static Sprite Overlay; private const float OverlayAlpha = 0.6f; @@ -214,7 +212,7 @@ public static class MonsterMetadata { [PokeheimInit] public static void Init() { - Overlay = AssetUtils.LoadSpriteFromFile(OverlayPath, centerPivot); + Overlay = Utils.LoadSprite(OverlayPath, centerPivot); #if DEBUG CommandManager.Instance.AddConsoleCommand(new SpawnAll()); @@ -443,11 +441,11 @@ public void Load() { if (this.trophyName == null) { // Load a premade icon from disk. var path = $"{PremadeIconPrefix}{this.prefabName}.png"; - this.trophyIcon = AssetUtils.LoadSpriteFromFile(path, centerPivot); + this.trophyIcon = Utils.LoadSprite(path, centerPivot); if (trophyIcon == null) { path = $"{PremadeIconPrefix}fallback.png"; - this.trophyIcon = AssetUtils.LoadSpriteFromFile(path, centerPivot); + this.trophyIcon = Utils.LoadSprite(path, centerPivot); } if (this.prefabCharacter != null && this.trophyIcon != null) { diff --git a/Pokeheim/MusicMods.cs b/Pokeheim/MusicMods.cs index 161c605..867e38e 100644 --- a/Pokeheim/MusicMods.cs +++ b/Pokeheim/MusicMods.cs @@ -30,7 +30,7 @@ namespace Pokeheim { public static class MusicMods { private static readonly Dictionary OverrideMusic = new Dictionary { - {"menu", "Pokeheim/Assets/Main-Menu.mp3"}, + {"menu", "Main-Menu.mp3"}, // TODO: morning music //{"morning", ""}, }; @@ -43,7 +43,7 @@ public static void Init() { } static AudioClip LoadAudioClip(string relativePath) { - var absolutePath = Path.Combine(BepInEx.Paths.PluginPath, relativePath); + var absolutePath = Utils.GetAssetPath(relativePath); var pathUrl = "file:///" + absolutePath.Replace("\\", "/"); var request = UnityWebRequestMultimedia.GetAudioClip( pathUrl, AudioType.MPEG); diff --git a/Pokeheim/PlayerMods.cs b/Pokeheim/PlayerMods.cs index 52760a5..3f11135 100644 --- a/Pokeheim/PlayerMods.cs +++ b/Pokeheim/PlayerMods.cs @@ -32,7 +32,7 @@ namespace Pokeheim { public static class PlayerMods { private const string PokedexFakeStationPrefix = "com.pokeheim.pokedex."; - private const string PokedexIconPath = "Pokeheim/Assets/Pokedex icon.png"; + private const string PokedexIconPath = "Pokedex icon.png"; [PokeheimInit] public static void Init() { @@ -192,7 +192,7 @@ static void FindPokedexComponents(InventoryGui __instance) { if (component.name == "Trophies") { foreach (var child in component.GetComponentsInChildren()) { if (child.name == "Image") { - child.sprite = AssetUtils.LoadSpriteFromFile(PokedexIconPath); + child.sprite = Utils.LoadSprite(PokedexIconPath); } } } diff --git a/Pokeheim/Pokeheim.cs b/Pokeheim/Pokeheim.cs index 8461aa8..6b0542e 100644 --- a/Pokeheim/Pokeheim.cs +++ b/Pokeheim/Pokeheim.cs @@ -85,8 +85,7 @@ static void Postfix(FejdStartup __instance) { var logo = startup.m_mainMenu.transform.Find("LOGO").gameObject; var hh = startup.m_mainMenu.transform.Find("H&H").gameObject; - logo.GetComponent().sprite = - AssetUtils.LoadSpriteFromFile("Pokeheim/Assets/Logo.png"); + logo.GetComponent().sprite = Utils.LoadSprite("Logo.png"); hh.GetComponent().enabled = false; } } @@ -135,8 +134,7 @@ static void Postfix(Hud __instance) { // Update the logo. var image = __instance.m_loadingProgress.transform.Find("text_darken/Logotype"); - image.GetComponent().sprite = - AssetUtils.LoadSpriteFromFile("Pokeheim/Assets/Logo.png"); + image.GetComponent().sprite = Utils.LoadSprite("Logo.png"); } } diff --git a/Pokeheim/Utils.cs b/Pokeheim/Utils.cs index b57146b..b23b128 100644 --- a/Pokeheim/Utils.cs +++ b/Pokeheim/Utils.cs @@ -18,11 +18,14 @@ using HarmonyLib; using Jotunn.Managers; +using Jotunn.Utils; using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Linq; +using System.Reflection; using UnityEngine; using UnityEngine.UI; @@ -346,5 +349,33 @@ public static GameObject GetSpawnedLocationOrPrefab(string name) { Logger.LogDebug($"Found no existing instances of location {name}."); return zoneLocation.m_prefab; } + + private static string cachedAssetRootPath = null; + + private static void FindAssetRootPath() { + if (cachedAssetRootPath == null) { + string assemblyFolder = System.IO.Path.GetDirectoryName( + Assembly.GetExecutingAssembly().Location); + cachedAssetRootPath = Path.Combine( + assemblyFolder, "Pokeheim", "Assets"); + } + } + + public static string GetAssetPath(string asset) { + FindAssetRootPath(); + return Path.Combine(cachedAssetRootPath, asset); + } + + public static Sprite LoadSprite(string asset) { + return LoadSprite(asset, Vector2.zero); + } + + public static Sprite LoadSprite(string asset, Vector2 pivot) { + return AssetUtils.LoadSpriteFromFile(GetAssetPath(asset), pivot); + } + + public static Texture2D LoadTexture(string asset) { + return AssetUtils.LoadTexture(GetAssetPath(asset)); + } } } diff --git a/docs/Install.md b/docs/Install.md index 3512c9c..ae747a5 100644 --- a/docs/Install.md +++ b/docs/Install.md @@ -74,7 +74,8 @@ experienced Linux user). 5. Move `Jotunn.dll` to the `BepInEx/plugins/` folder inside the Valheim install folder. 6. Unpack the MountUp zip into the `BepInEx/plugins/` folder. -7. Unpack the Pokéheim zip into the `BepInEx/plugins/` folder. +7. Unpack the Pokéheim zip and move the contents of the zip's `plugins` folder + into the `BepInEx/plugins/` folder. 8. _(If **not using** Steam)_ Run `start_game_bepinex.sh` to launch Pokéheim. 9. _(If **using** Steam)_ See [Steam.md](https://github.com/joeyparrish/pokeheim/blob/main/docs/Steam.md) @@ -84,6 +85,7 @@ experienced Linux user). 1. _(Linux only)_ Remove the mods: ```sh + rm ~/.local/share/Steam/steamapps/common/Valheim/BepInEx/plugins/Pokeheim.dll rm -rf ~/.local/share/Steam/steamapps/common/Valheim/BepInEx/plugins/Pokeheim/ rm ~/.local/share/Steam/steamapps/common/Valheim/BepInEx/plugins/MountUp.dll rm ~/.local/share/Steam/steamapps/common/Valheim/BepInEx/plugins/Jotunn.dll diff --git a/scripts/build-and-launch.sh b/scripts/build-and-launch.sh index 2e417f0..6c78267 100755 --- a/scripts/build-and-launch.sh +++ b/scripts/build-and-launch.sh @@ -18,4 +18,6 @@ cd "$(dirname "$0")"/.. ./scripts/install-mod.sh "$BUILD_TYPE" cd ~/.local/share/Steam/steamapps/common/Valheim -./start_game_bepinex.sh ~/.local/share/Steam/steamapps/common/Valheim/valheim.x86_64 -force-glcore -console +./start_game_bepinex.sh \ + ~/.local/share/Steam/steamapps/common/Valheim/valheim.x86_64 \ + -force-glcore -console diff --git a/scripts/install-mod.sh b/scripts/install-mod.sh index d5d34b2..6836e30 100755 --- a/scripts/install-mod.sh +++ b/scripts/install-mod.sh @@ -22,8 +22,14 @@ PLUGINS_PATH=~/.local/share/Steam/steamapps/common/"$GAME_NAME"/BepInEx/plugins cd "$(dirname "$0")"/.. +# Old location (<= v1.0.3) rm -rf "$PLUGINS_PATH"/Pokeheim/ -mkdir -p "$PLUGINS_PATH"/Pokeheim/Assets/ + +# New location (>= v1.0.4), simulates r2modman install pattern +POKEHEIM_FOLDER="$PLUGINS_PATH"/Pokeheim-Pokeheim +POKEHEIM_ASSETS="$PLUGINS_PATH"/Pokeheim-Pokeheim/Pokeheim/Assets +rm -rf "$POKEHEIM_FOLDER" +mkdir -p "$POKEHEIM_ASSETS" # NOTE: What nuget downloads is always a Debug build of Jotunn. # To install a true Release build, we need to fetch that from some place like @@ -33,9 +39,10 @@ if [ "$BUILD_TYPE" == "Release" ]; then else cp Pokeheim/bin/$BUILD_TYPE/Jotunn.dll "$PLUGINS_PATH"/ fi -cp Pokeheim/bin/$BUILD_TYPE/Pokeheim.dll "$PLUGINS_PATH"/Pokeheim/ -cp Pokeheim/Assets/*.png "$PLUGINS_PATH"/Pokeheim/Assets/ -cp Pokeheim/Assets/*.mp3 "$PLUGINS_PATH"/Pokeheim/Assets/ -cp -a Pokeheim/Assets/Translations "$PLUGINS_PATH"/Pokeheim/Assets/ + +cp Pokeheim/bin/$BUILD_TYPE/Pokeheim.dll "$POKEHEIM_FOLDER"/ +cp Pokeheim/Assets/*.png "$POKEHEIM_ASSETS"/ +cp Pokeheim/Assets/*.mp3 "$POKEHEIM_ASSETS"/ +cp -a Pokeheim/Assets/Translations "$POKEHEIM_ASSETS"/ echo "Installed $BUILD_TYPE build." diff --git a/scripts/package.sh b/scripts/package.sh index 33b39c6..e126b34 100755 --- a/scripts/package.sh +++ b/scripts/package.sh @@ -21,12 +21,12 @@ rm -rf Pokeheim/bin/ # Make a generic zip. rm -rf staging Pokeheim.zip -mkdir -p staging/Pokeheim/Assets +mkdir -p staging/plugins/Pokeheim/Assets # Stage mod & assets. -cp Pokeheim/bin/Release/Pokeheim.dll staging/Pokeheim/ -cp Pokeheim/Assets/*.png staging/Pokeheim/Assets/ -cp Pokeheim/Assets/*.mp3 staging/Pokeheim/Assets/ -cp -a Pokeheim/Assets/Translations staging/Pokeheim/Assets/ +cp Pokeheim/bin/Release/Pokeheim.dll staging/plugins/ +cp Pokeheim/Assets/*.png staging/plugins/Pokeheim/Assets/ +cp Pokeheim/Assets/*.mp3 staging/plugins/Pokeheim/Assets/ +cp -a Pokeheim/Assets/Translations staging/plugins/Pokeheim/Assets/ # Stage mod metadata. cp publish/icon.png staging/ cp README.md staging/ @@ -41,7 +41,7 @@ set +x # Double-check versioning. manifest_version=$(cat staging/manifest.json | jq -r .version_number) -dll_version=$(monodis --assembly staging/Pokeheim/Pokeheim.dll \ +dll_version=$(monodis --assembly staging/plugins/Pokeheim.dll \ | grep Version | cut -f 2 -d : | tr -d ' ') if [[ "$manifest_version.0" != "$dll_version" ]]; then echo "Version mismatch!"