Flexi Archive 提供了多种存档读写的操作方案,包括:
1.持有DataObject操作存档
2.对象字段与数据绑定 对象字段与数据绑定 - 简化存档流程
其中DataObject是核心,但对象字段与数据绑定的操作也极大地简化了存档流程。
对象字段与数据绑定 #
对象字段与数据绑定是Flexi Archive提供的另一种简化存档流程的操作方案。这种方式利用了数据绑定的特性,使得对象字段的变化能够自动反映到存档数据中,从而简化了存档操作。
代码示例:( 更多绑定方式见文档 : 对象字段与数据绑定 - 简化存档流程 )
public class DataContainerForSave_Sample : FlexiDataContainer
{
public string author
{
get => GetMemoryValue<DataString, string>("FlexiDataSave","author");
set => ModifyValue<DataString, string>("FlexiDataSave","author", value);
}
public int age
{
get => GetMemoryValue<DataInteger, int>("FlexiDataSave","age");
set => ModifyValue<DataInteger, int>("FlexiDataSave","age", value);
}
public DataContainerForSave_Sample(IFlexiDataArchiveManager dataArchiveManager)
{
SetDataArchiveManager(dataArchiveManager);
}
}
持有DataObject操作存档 #
通过 GroupKey + DataKey 从存档系统中得到一个DataObject实例,示例:archiveManager.GetDataObject("group1", "key1"); 。
DataObject相当一个数据管理容器,我们可以从DataObject中获取到具体的数据对象。TDataWrapper idataWrapper = dataObject.GetData<TDataWrapper>();
写入数据:idataWrapper.Write(new TData());
从磁盘中读取数据:idataWrapper.DiskData
从内存中读取数据:idataWrapper.Data
目前系统已支持的数据类型:
float、double、int、long、bool、string、vector2 、vector3、vector4
object、list、array、dictionary、custom(所有自定义类型) ·····
下面重点介绍几种常用的数据类型
String #
DataWrapper:DataString
写入数据:
private string WriteStr()
{
DataObject dataObject = archiveManager.GetDataObject("group1", "key1");
DataString dataString = dataObject.GetData<DataString>();
dataString.Write(char.ConvertFromUtf32(Random.Range(65, 123)));
Debug.Log(string.Format($"[{Path.Combine("group1", "key1")}]: write a str :{dataString.data}"));
return dataString.data;
}
读取数据:
private string ReadStrFromDisk()
{
DataObject dataObject = archiveManager.GetDataObject("group1", "key1");
DataString dataString = dataObject.GetData<DataString>();
string str = dataString.DiskData;
Debug.Log(string.Format($"[{Path.Combine("group1", "key1")}]: read a str from disk: {str}"));
return str;
}
Int32 #
DataWrapper:DataInteger
写入数据:
private string WriteInt()
{
DataObject dataObject = archiveManager.GetDataObject("group2", "key1");
DataInteger dataInteger = dataObject.GetData<DataInteger>();
dataInteger.Write(Random.Range(0, 5));
Debug.Log(string.Format(
$"[{Path.Combine("group2", "key1")}]: write a number of type int {dataInteger.data}"));
return dataInteger.data.ToString();
}
读取数据:
private string ReadIntFromDisk()
{
DataObject dataObject = archiveManager.GetDataObject("group2", "key1");
DataInteger dataInteger = dataObject.GetData<DataInteger>();
Debug.Log(string.Format(
$"[{Path.Combine("group2", "key1")}]: read a int from disk : {dataInteger.DiskData}"));
return dataInteger.DiskData.ToString();
}
Vector3 #
DataWrapper:DataVector3
写入数据:
private string WriteVector3()
{
DataObject dataObject = archiveManager.GetDataObject("group2", "key2");
DataVector3 dataVector3 = dataObject.GetData<DataVector3>();
dataVector3.Write(new Vector3(Random.Range(0, 100), Random.Range(0, 100), Random.Range(0, 100)));
Debug.Log(string.Format($"[{Path.Combine("group2", "key2")}]: write a vector3 :{dataVector3.data}"));
return dataVector3.data.ToString();
}
读取数据:
private string ReadVector3()
{
DataObject dataObject = archiveManager.GetDataObject("group2", "key2");
DataVector3 dataVector3 = dataObject.GetData<DataVector3>();
Debug.Log(string.Format(
$"[{Path.Combine("group2", "key2")}]: read a vector3 from disk:{dataVector3.DiskData}"));
return dataVector3.DiskData.ToString();
}
Object #
DataWrapper:DataTypeObject
写入数据:
private string WriteObj()
{
DataObject dataObject = archiveManager.GetDataObject("group3", "key2");
DataTypeObject dataTypeObject = dataObject.GetData<DataTypeObject>();
dataTypeObject.Write(new Vector3Wrapper(new Vector3(6,6,6)));
Debug.Log(string.Format($"[{Path.Combine("group3", "key2")}]: write a object :{dataTypeObject.data}"));
return dataTypeObject.data.ToString();
}
读取数据:
private string ReadObj()
{
DataObject dataObject = archiveManager.GetDataObject("group3", "key2");
DataTypeObject dataTypeObject = dataObject.GetData<DataTypeObject>();
string str = "null";
if (dataTypeObject.DiskData != null)
{
Vector3 vector3 = (Vector3Wrapper)dataTypeObject.DiskData;
str = vector3.ToString();
}
Debug.Log(string.Format($"[{Path.Combine("group3", "key2")}]: read a object :{str}"));
return str;
}
List #
DataWrapper:DataStructList<T>
写入数据:
private void Demo9_WriteList()
{
DataObject dataObject = archiveManager.GetDataObject("group3", "key1");
DataStructList<float> dataStructList = dataObject.GetData<DataStructList<float>>();
List<float> list = new List<float>();
list.Add(Random.Range(0, 5));
list.Add(Random.Range(0, 5));
list.Add(Random.Range(0, 5));
list.Add(Random.Range(0, 5));
dataStructList.Write(list);
}
读取数据:
private void Demo10_ReadList()
{
DataObject dataObject = archiveManager.GetDataObject("group3", "key1");
DataStructList<float> dataStructList = dataObject.GetData<DataStructList<float>>();
List<float> list = dataStructList.DiskData;
}
Dictionary #
DataWrapper:DataDictionary
<T>
写入数据:
private string Demo11_WriteDic()
{
DataObject dataObject = archiveManager.GetDataObject("group3", "key3");
DataDictionary<Vector3Wrapper> dataDictionary = dataObject.GetData<DataDictionary<Vector3Wrapper>>();
Dictionary<string, Vector3Wrapper> dictionary = new Dictionary<string, Vector3Wrapper>();
dictionary.Add(1.ToString(),new Vector3(1,1,1));
dictionary.Add(2.ToString(),new Vector3(2,2,2));
dictionary.Add(3.ToString(),new Vector3(3,4,4));
dataDictionary.Write(dictionary);
string log = dataDictionary.data.ToString() + " - " + dataDictionary.ToString();
Debug.Log(($"[{Path.Combine("group3", "key1")}]: write a dictionary :{log}"));
return log;
}
读取数据:
private string Demo12_ReadDic()
{
DataObject dataObject = archiveManager.GetDataObject("group3", "key3");
DataDictionary<Vector3Wrapper> dataDictionary = dataObject.GetData<DataDictionary<Vector3Wrapper>>();
string log = "null";
if (dataDictionary.DiskData != null)
{
log = dataDictionary.DiskData.ToString() + " - " + dataDictionary.ToString();
}
Debug.Log(($"[{Path.Combine("group3", "key1")}]: write a dictionary :{log}"));
return log;
}
自定义类型 #
定义数据类型,详细见 创建自定义数据类型 - 温文的小屋
/// <summary>
/// 1.create construct method
/// </summary>
public class CustomDataType : AbstractDataType<CustomData>
{
public CustomDataType(string dataStr) : base(dataStr){}
}
/// <summary>
/// 2.override "ToString(CustomData)" function if would know detail data
/// </summary>
public class CustomData
{
public string author = "温文";
public int code = 1;
public double luckly = 1.0f;
public override string ToString()
{
return "author: " + author +
" code: " + code +
" luckly: " + luckly;
}
}
获取持有CustomData
值的数据对象
DataObject dataObject = archiveManager.GetDataObject("group3", "key2");
CustomDataType customDataType= dataObject.GetData<CustomDataType>();
写入:
customDataType.Write(new CustomData("温文",1,1.0f));
CustomData customData = customDataType.DiskData;