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

115 lines
3.8 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
{
2024-01-07 14:59:02 +00:00
private static readonly string SourceConfigFileName = "guru-service";
2024-01-08 03:45:48 +00:00
private const string LocalServicesConfigPath = "Guru/Resources";
2024-01-07 14:59:02 +00:00
private const string SourceConfigExtension = ".json";
private const string LocalConfigExtension = ".txt";
2023-12-26 03:40:48 +00:00
private static string DefaultFilePath =
2024-01-07 14:59:02 +00:00
Path.GetFullPath(Path.Combine(Application.dataPath, $"{SourceConfigFileName}{SourceConfigExtension}"));
2023-12-26 03:40:48 +00:00
2024-01-07 14:59:02 +00:00
internal static string SourceServiceFilePath = "";
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($"*{SourceConfigFileName}* t:TextAsset", new []{"Assets"});
2023-12-26 03:40:48 +00:00
if (a == null || a.Length == 0)
{
Debug.Log($"<color=orange>--- Can't find guru-services file</color>");
2023-12-26 03:40:48 +00:00
}
else
{
var p = AssetDatabase.GUIDToAssetPath(a[0]);
var fp = Path.GetFullPath(p);
2024-01-07 14:59:02 +00:00
if (File.Exists(fp)) SourceServiceFilePath = 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();
2024-01-07 14:59:02 +00:00
if (string.IsNullOrEmpty(SourceServiceFilePath)) SourceServiceFilePath = DefaultFilePath;
File.WriteAllText(SourceServiceFilePath, json);
Debug.Log($"Save config to {SourceServiceFilePath}");
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);
}
2024-01-07 14:59:02 +00:00
public static void DeployLocalServiceFile()
2023-12-27 12:24:16 +00:00
{
2024-01-07 14:59:02 +00:00
var deployPath = Path.Combine(Application.dataPath, LocalServicesConfigPath);
if(!Directory.Exists(deployPath)) Directory.CreateDirectory(deployPath);
2024-01-08 03:47:35 +00:00
var path = Path.Combine(deployPath, $"{GuruSDK.ServicesConfigKey}{LocalConfigExtension}");
2023-12-27 12:24:16 +00:00
var config = LoadConfig();
2024-01-07 14:59:02 +00:00
var from = SourceServiceFilePath;
2023-12-27 12:24:16 +00:00
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} to local resources.</color>");
2023-12-27 12:24:16 +00:00
File.Copy(from, path);
}
}
2023-12-26 03:40:48 +00:00
}
}