Files
2025-01-25 04:38:09 +08:00

151 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
namespace Thousandto.Launcher.ExternalLibs
{
/// <summary>
/// Launcher的全局数据信息
/// </summary>
public static class GonbestGMemory
{
//通过字符串作为Key
private static Dictionary<string, GData> _allData = new Dictionary<string, GData>();
//分配Float的内存块
public static float[] AllocFloatBlock(string key, int maxSize = -1)
{
GData ret = null;
_allData.TryGetValue(key, out ret);
if (maxSize > 0)
{
if (ret == null)
{
ret = new GData(maxSize, true);
_allData[key] = ret;
return ret.Fdata;
}
else
{
if (ret.FloatType)
{
ret.Reset(maxSize);
return ret.Fdata;
}
else
{
UnityEngine.Debug.LogError("全局变量G["+key+"]不是Float型的数据!");
return null;
}
}
}
else
{
return ret != null ? ret.Fdata : null;
}
}
//分配字符串的内存块
public static string[] AllocStringBlock(string key, int maxSize = -1)
{
GData ret = null;
_allData.TryGetValue(key, out ret);
if (maxSize > 0)
{
if (ret == null)
{
ret = new GData(maxSize, false);
_allData[key] = ret;
return ret.SData;
}
else
{
if (ret.FloatType)
{
ret.Reset(maxSize);
return ret.SData;
}
else
{
UnityEngine.Debug.LogError("全局变量G[" + key + "]不是String型的数据!");
return null;
}
}
}
else
{
return ret != null ? ret.SData : null;
}
}
//判断当前内存块是否有效
public static bool HasBlock(string key)
{
return _allData.ContainsKey(key);
}
/// <summary>
/// 全局数据信息
/// </summary>
private class GData
{
//当前全局数据是否为Float型
public bool FloatType { get; private set; }
//Float的数据内存块
public float[] Fdata = null;
//字符串的数据内存块
public string[] SData = null;
//构造函数
public GData(int size, bool floatType)
{
FloatType = floatType;
if (floatType)
{
Fdata = new float[size];
}
else
{
SData = new string[size];
}
}
//重新设置大小
public void Reset(int maxSize = -1)
{
if (FloatType)
{
Fdata = NewBlock(Fdata, maxSize);
}
else
{
SData = NewBlock(SData, maxSize);
}
}
public T[] NewBlock<T>(T[] old,int maxSize)
{
if (maxSize <= 0) return old;
if (old != null)
{
if (old.Length < maxSize)
{
T[] newData = new T[maxSize];
Array.Copy(old, newData, Fdata.Length);
return newData;
}
}
else
{
return new T[maxSize];
}
return old;
}
}
}
}