GameplayTags icon indicating copy to clipboard operation
GameplayTags copied to clipboard

Need fix for Android builds

Open mjolner36 opened this issue 3 months ago • 4 comments

2025.09.26 11:28:19.772 8257 8411 Info SwappyDisplayManager Terminating looper thread 2025.09.26 11:28:19.780 8257 8306 Error SwappyDisplayManager dalvik.system.InMemoryDexClassLoader[DexPathList[[dex file "InMemoryDexFile[cookie=[-5476376606068064000, -5476376606068851328]]"],nativeLibraryDirectories=[/system/lib64, /system_ext/lib64]]] couldn't find "libgame.so" 2025.09.26 11:28:19.927 8257 8477 Error Unity Failed to load gameplay tags from StreamingAssets: Could not find a part of the path "/jar:file:/data/app/~~VfVdO7Zr6pjQxZNcT6Gk9g==/com.UnityTechnologies.UniversalMobile3DTemplate-CAMgyFsbqThb3aR-WQhY1g==/base.apk!/assets/GameplayTags". 2025.09.26 11:28:19.927 8257 8477 Error Unity UnityEngine.DebugLogHandler:Internal_Log(LogType, LogOption, String, Object) 2025.09.26 11:28:19.927 8257 8477 Error Unity BandoWare.GameplayTags.BuildGameplayTagSource:RegisterTags(GameplayTagRegistrationContext) 2025.09.26 11:28:19.927 8257 8477 Error Unity BandoWare.GameplayTags.GameplayTagManager:InitializeIfNeeded() 2025.09.26 11:28:19.927 8257 8477 Error Unity BandoWare.GameplayTags.GameplayTagManager:TryGetDefinition(String, GameplayTagDefinition&) 2025.09.26 11:28:19.927 8257 8477 Error Unity BandoWare.GameplayTags.GameplayTagManager:RequestTag(String, Boolean) 2025.09.26 11:28:19.927 8257 8477 Error Unity BandoWare.GameplayTags.GameplayTagContainer:UnityEngine.ISerializationCallbackReceiver.OnAfterDeserialize()

My solution

using System;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;

namespace BandoWare.GameplayTags
{
    internal class BuildGameplayTagSource : IGameplayTagSource
    {
        public string Name => "Build";

        public void RegisterTags(GameplayTagRegistrationContext context)
        {
            try
            {
                string src = Path.Combine(Application.streamingAssetsPath, "GameplayTags");

                byte[] data = LoadStreamingAssetBytes(src);

                MemoryStream ms = new MemoryStream(data);
                BinaryReader reader = new BinaryReader(ms, Encoding.UTF8, false);

                while (ms.Position < ms.Length)
                {
                    string tagName = reader.ReadString();
                    context.RegisterTag(tagName, string.Empty, GameplayTagFlags.None, this);
                }

                reader.Dispose();
                ms.Dispose();
            }
            catch (Exception e)
            {
                Debug.LogError($"Failed to load gameplay tags from StreamingAssets: {e.Message}");
            }
        }

        private static byte[] LoadStreamingAssetBytes(string absolutePath)
        {
            if (absolutePath.Contains("://") || absolutePath.StartsWith("jar:", StringComparison.OrdinalIgnoreCase))
            {
                UnityWebRequest req = UnityWebRequest.Get(absolutePath);
                UnityWebRequestAsyncOperation op = req.SendWebRequest();
                while (!op.isDone) { }

#if UNITY_2020_2_OR_NEWER
                if (req.result != UnityWebRequest.Result.Success)
#else
                if (req.isNetworkError || req.isHttpError)
#endif
                {
                    string err = req.error;
                    req.Dispose();
                    throw new IOException($"StreamingAssets load failed: {err} | {absolutePath}");
                }

                byte[] bytes = req.downloadHandler.data;
                req.Dispose();

                if (bytes == null || bytes.Length == 0)
                    throw new IOException($"Empty data from StreamingAssets: {absolutePath}");

                return bytes;
            }
            else
            {
                if (!File.Exists(absolutePath))
                    throw new FileNotFoundException($"File not found in StreamingAssets: {absolutePath}");
                return File.ReadAllBytes(absolutePath);
            }
        }
    }
}

mjolner36 avatar Sep 26 '25 09:09 mjolner36

Thanks for reporting

I fixed this in https://github.com/BandoWare/GameplayTags/commit/cc4178580b11634a0b426aeab487e437e5ef284e, can you test it for me?

felipemcoliveira avatar Sep 26 '25 13:09 felipemcoliveira

@mjolner36 can you test it for me so I can add it to the next release?

felipemcoliveira avatar Oct 01 '25 20:10 felipemcoliveira

I was tested my code in my commerial project. It fine

mjolner36 avatar Oct 01 '25 20:10 mjolner36

The error on android is that directories work differently on operating systems such as android and PC or iOS. On android, this is a url type, but on PC or iOS, as usual

mjolner36 avatar Oct 01 '25 20:10 mjolner36