com.guru.unity.sdk/Editor/GuruManager/Config/EditorGuruServiceIO.cs

112 lines
3.5 KiB
C#
Raw Normal View History

2023-12-26 03:40:48 +00:00
using UnityEngine;
using Guru;
using NUnit.Framework;
namespace Guru.Editor
{
using System.IO;
using UnityEditor;
using Guru.LitJson;
public class EditorGuruServiceIO
{
private static readonly string DefaultFileName = "guru-service";
private static string DefaultFilePath =
Path.GetFullPath(Path.Combine(Application.dataPath, $"{DefaultFileName}.json"));
2023-12-27 12:24:16 +00:00
internal static string EmbededServiceFilePath = "";
2023-12-26 03:40:48 +00:00
/// <summary>
/// 加载配置
/// </summary>
/// <returns></returns>
2023-12-27 12:24:16 +00:00
public static GuruServicesConfig LoadConfig()
2023-12-26 03:40:48 +00:00
{
var a = AssetDatabase.FindAssets($"*{DefaultFileName}* t:TextAsset");
if (a == null || a.Length == 0)
{
Debug.Log($"<color=orange>--- Can't ind guru-services file</color>");
}
else
{
var p = AssetDatabase.GUIDToAssetPath(a[0]);
var fp = Path.GetFullPath(p);
2023-12-27 12:24:16 +00:00
if (File.Exists(fp)) EmbededServiceFilePath = fp;
2023-12-26 03:40:48 +00:00
var t = AssetDatabase.LoadAssetAtPath<TextAsset>(p);
// Debug.Log($"<color=#88ff00>--- find services file:{p} \n{t.text}</color>");
2023-12-27 12:24:16 +00:00
return JsonMapper.ToObject<GuruServicesConfig>(t.text);
2023-12-26 03:40:48 +00:00
}
return null;
}
/// <summary>
/// 保存配置
/// </summary>
/// <param name="config"></param>
2023-12-27 12:24:16 +00:00
internal static void SaveConfig(GuruServicesConfig config = null)
2023-12-26 03:40:48 +00:00
{
if (config == null)
{
2023-12-27 12:24:16 +00:00
config = new GuruServicesConfig();
2023-12-26 03:40:48 +00:00
}
var jw = new JsonWriter()
{
PrettyPrint = true,
};
JsonMapper.ToJson(config, jw);
var json = jw.ToString();
2023-12-27 12:24:16 +00:00
if (string.IsNullOrEmpty(EmbededServiceFilePath)) EmbededServiceFilePath = DefaultFilePath;
File.WriteAllText(EmbededServiceFilePath, json);
Debug.Log($"Save config to {EmbededServiceFilePath}");
2023-12-26 03:40:48 +00:00
}
/// <summary>
/// 创建空配置
/// </summary>
/// <returns></returns>
2023-12-27 12:24:16 +00:00
internal static GuruServicesConfig CreateEmpty()
2023-12-26 03:40:48 +00:00
{
2023-12-27 12:24:16 +00:00
var cfg = new GuruServicesConfig();
cfg.version = 0;
2023-12-26 03:40:48 +00:00
cfg.app_settings = new GuruAppSettings();
cfg.ad_settings = new GuruAdSettings();
cfg.adjust_settings = new GuruAdjustSettings();
cfg.fb_settings = new GuruFbSettings();
return cfg;
}
[Test]
public static void Test_SaveConfig()
{
var cfg = CreateEmpty();
SaveConfig(cfg);
}
2023-12-27 12:24:16 +00:00
public static void DeployAppServiceFile()
{
var streamingPath = Application.streamingAssetsPath;
if(!Directory.Exists(streamingPath)) Directory.CreateDirectory(streamingPath);
var path = Path.Combine(streamingPath, $"{GuruSDK.ServicesConfigKey}.{GuruSDK.ServicesConfigExtension}");
var config = LoadConfig();
var from = EmbededServiceFilePath;
if (string.IsNullOrEmpty(from) || !File.Exists(from)) // 文件不存在
{
return;
}
2023-12-26 03:40:48 +00:00
2023-12-27 12:24:16 +00:00
if (null != config)
{
if (File.Exists(path)) File.Delete(path);
Debug.Log($"<color=#88ff00> --- setup {GuruSDK.ServicesConfigKey} file on streamingPath</color>");
File.Copy(from, path);
}
}
2023-12-26 03:40:48 +00:00
}
}