using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using LC.Newtonsoft.Json;
using TapTap.Common;
namespace TapTap.TapDB.Standalone.Internal {
///
/// 迁移旧版数据
///
public class Migration {
private static readonly string PERSISTENT_FILE_NAME = "tapdb_storage";
public void TryMigrate() {
string persistentFilePath = Path.Combine(Application.persistentDataPath, PERSISTENT_FILE_NAME);
if (!File.Exists(persistentFilePath)) {
return;
}
try {
string json = File.ReadAllText(persistentFilePath);
// 数据类型转化
Dictionary jsonData = JsonConvert.DeserializeObject>(json);
Dictionary newJsonData = new Dictionary();
foreach (KeyValuePair kv in jsonData) {
if(kv.Value == null)
{
continue;
}
string value = Codec.Decode(kv.Value.ToString());
if (kv.Key == PlayRecorder.PLAYED_DURATION_KEY && long.TryParse(value, out long duration)) {
newJsonData[kv.Key] = duration;
} else {
newJsonData[kv.Key] = value;
}
}
// 写回新版数据文件
json = JsonConvert.SerializeObject(newJsonData);
string persistentFilePath2 = Path.Combine(Application.persistentDataPath, Prefs.OLD_PERSISTENT_FILE_NAME);
File.WriteAllText(persistentFilePath2, Codec.Encode(json));
} catch (Exception e) {
TapLogger.Warn(e.Message);
} finally {
File.Delete(persistentFilePath);
}
}
}
}