新存储系统
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using AibisDream.Framework;
|
||||
using AibisDream.Utility;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using UnityEngine;
|
||||
using Yarn.Unity;
|
||||
|
||||
@@ -11,6 +15,8 @@ namespace AibisDream
|
||||
private const string VariablePrefix = "$";
|
||||
private const string GlobalPrefix = "$global.";
|
||||
private const string DataPrefix = "$data.";
|
||||
|
||||
private static readonly string SaveFilePath = Application.streamingAssetsPath + "/SaveFiles";
|
||||
|
||||
#region 存储部分
|
||||
|
||||
@@ -77,7 +83,7 @@ namespace AibisDream
|
||||
};
|
||||
EnumEventSystem.Global.Send(StorageEvent.VariableSet, variableItem);
|
||||
}
|
||||
|
||||
|
||||
public override void Clear()
|
||||
{
|
||||
_variableDict.Clear();
|
||||
@@ -100,9 +106,15 @@ namespace AibisDream
|
||||
|
||||
public override bool Contains(string variableName)
|
||||
{
|
||||
return _variableDict.ContainsKey(variableName);
|
||||
var variableType = ValidateVariableName(variableName);
|
||||
if (variableType == VariableType.Illegal)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return variableType == VariableType.DataField ?
|
||||
_dataContainer.TryGetFieldValue<string>(variableName, out _) : _variableDict.ContainsKey(variableName);
|
||||
}
|
||||
|
||||
|
||||
public override bool TryGetValue<T>(string variableName, out T result)
|
||||
{
|
||||
var variableType = ValidateVariableName(variableName);
|
||||
@@ -115,9 +127,7 @@ namespace AibisDream
|
||||
// 在系统数据中查找
|
||||
if (variableType == VariableType.DataField)
|
||||
{
|
||||
Debug.LogWarning("查找Data数据功能待实现");
|
||||
result = default;
|
||||
return true;
|
||||
return _dataContainer.TryGetFieldValue(variableName, out result);
|
||||
}
|
||||
// 直接查找数据
|
||||
if (_variableDict.TryGetValue(variableName, out var value))
|
||||
@@ -128,13 +138,13 @@ namespace AibisDream
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
#region 序列化
|
||||
|
||||
public override void SetAllVariables(Dictionary<string, float> floats, Dictionary<string, string> strings,
|
||||
@@ -165,29 +175,29 @@ namespace AibisDream
|
||||
var floatDict = new Dictionary<string, float>();
|
||||
var stringDict = new Dictionary<string, string>();
|
||||
var boolDict = new Dictionary<string, bool>();
|
||||
|
||||
|
||||
foreach (var variablePair in _variableDict)
|
||||
{
|
||||
switch (variablePair.Value)
|
||||
{
|
||||
case float:
|
||||
{
|
||||
var value = System.Convert.ToSingle(variablePair.Value);
|
||||
floatDict.Add(variablePair.Key, value);
|
||||
break;
|
||||
}
|
||||
{
|
||||
var value = System.Convert.ToSingle(variablePair.Value);
|
||||
floatDict.Add(variablePair.Key, value);
|
||||
break;
|
||||
}
|
||||
case string:
|
||||
{
|
||||
var value = System.Convert.ToString(variablePair.Value);
|
||||
stringDict.Add(variablePair.Key, value);
|
||||
break;
|
||||
}
|
||||
{
|
||||
var value = System.Convert.ToString(variablePair.Value);
|
||||
stringDict.Add(variablePair.Key, value);
|
||||
break;
|
||||
}
|
||||
case bool:
|
||||
{
|
||||
var value = System.Convert.ToBoolean(variablePair.Value);
|
||||
boolDict.Add(variablePair.Key, value);
|
||||
break;
|
||||
}
|
||||
{
|
||||
var value = System.Convert.ToBoolean(variablePair.Value);
|
||||
boolDict.Add(variablePair.Key, value);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
Debug.Log($"{variablePair.Key} is not a valid type");
|
||||
break;
|
||||
@@ -197,6 +207,54 @@ namespace AibisDream
|
||||
return (floatDict, stringDict, boolDict);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 保存/加载
|
||||
|
||||
public void Save()
|
||||
{
|
||||
// 先序列化Yarn变量
|
||||
var saveFile = new JObject();
|
||||
|
||||
// 保存系统数据
|
||||
saveFile["systemData"] = _dataContainer.SaveAsJson();
|
||||
|
||||
// 保存对话变量
|
||||
var (floatDict, stringDict, boolDict) = GetAllVariables();
|
||||
saveFile["floatDict"] = JObject.FromObject(floatDict);
|
||||
saveFile["stringDict"] = JObject.FromObject(stringDict);
|
||||
saveFile["boolDict"] = JObject.FromObject(boolDict);
|
||||
|
||||
// 存为Json
|
||||
JsonUtil.SaveJObject(saveFile, GetSavePath());
|
||||
#if !UNITY_EDITOR
|
||||
DeleteOldSave();
|
||||
#endif
|
||||
}
|
||||
|
||||
public void Load()
|
||||
{
|
||||
// 先将存档文件转为JObject
|
||||
}
|
||||
|
||||
private string GetSavePath()
|
||||
{
|
||||
string gameStageStr;
|
||||
try
|
||||
{
|
||||
TryGetValue("$gameStage", out float gameStage);
|
||||
gameStageStr = gameStage.ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
TryGetValue("$gameStage", out string gameStage);
|
||||
gameStageStr = gameStage;
|
||||
}
|
||||
|
||||
return $"{SaveFilePath}/{DateTime.Now:yyyyMMdd_HHmmss}_SOName_{gameStageStr}";
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
private static VariableType ValidateVariableName(string variableName)
|
||||
|
||||
Reference in New Issue
Block a user