using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine.Gonbest.MagicCube;

namespace Thousandto.Core.Base
{
    /// <summary>
    /// 全局数据的一个代理
    /// </summary>
    public static class GonbestGMemoryAccessor
    {
        //读取Float内存块的方法
        private static MethodInfo _allocFloatBlockMethod = null;
        //读取String内存块的方法
        private static MethodInfo _allocStringBlockMethod = null;
        //判断当前是否已经有某个块
        private static MethodInfo _hasBlockMethod = null;

        static GonbestGMemoryAccessor()
        {
            var t = AssemblyUtils.FindType("Thousandto.Launcher.ExternalLibs.GonbestGMemory");
            if (t != null)
            {
                _allocFloatBlockMethod = AssemblyUtils.GetPublicStaticMethod(t,"AllocFloatBlock");
                _allocStringBlockMethod = AssemblyUtils.GetPublicStaticMethod(t, "AllocStringBlock");
                _hasBlockMethod = AssemblyUtils.GetPublicStaticMethod(t, "HasBlock");
            }
        }

        //分配Float的内存块
        public static float[] AllocFloatBlock(string key, int maxSize = -1)
        {
            if (_allocFloatBlockMethod != null)
            {
                return _allocFloatBlockMethod.Invoke(null, new object[] { key, maxSize }) as float[];
            }
            return null;
        }

        //分配字符串的内存块,如果是只读,那么maxSize最好为-1
        public static string[] AllocStringBlock(string key, int maxSize = -1)
        {
            if (_allocStringBlockMethod != null)
            {
                return _allocStringBlockMethod.Invoke(null, new object[] { key, maxSize }) as string[];
            }
            return null;
        }


        //判断当前内存块是否有效
        public static bool HasBlock(string key)
        {
            if (_hasBlockMethod != null)
            {
                return (bool)_hasBlockMethod.Invoke(null, new object[] {}) ;
            }
            return false;
        }

    }
}