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-02-02 11:06:32 +00:00
|
|
|
internal static readonly string SourceConfigFileName = "guru-service";
|
|
|
|
|
internal const string LocalServicesConfigPath = "Guru/Resources";
|
|
|
|
|
internal const string SourceConfigExtension = ".json";
|
|
|
|
|
internal const string LocalConfigExtension = ".txt";
|
2023-12-26 03:40:48 +00:00
|
|
|
|
2024-02-02 11:06:32 +00:00
|
|
|
internal 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
|
|
|
{
|
2024-01-08 02:22:56 +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)
|
|
|
|
|
{
|
2024-05-07 12:37:57 +00:00
|
|
|
UnityEngine.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);
|
2024-05-07 12:37:57 +00:00
|
|
|
// UnityEngine.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>
|
2024-02-02 11:06:32 +00:00
|
|
|
internal static void SaveConfig(GuruServicesConfig config = null, string savePath = "")
|
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-02-02 11:06:32 +00:00
|
|
|
if (string.IsNullOrEmpty(savePath)) savePath = SourceServiceFilePath;
|
|
|
|
|
if (string.IsNullOrEmpty(savePath)) savePath = DefaultFilePath;
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(savePath, json);
|
2024-05-07 12:37:57 +00:00
|
|
|
UnityEngine.Debug.Log($"Save config to {savePath}");
|
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();
|
2024-03-01 05:13:26 +00:00
|
|
|
cfg.parameters = new GuruParameters();
|
2023-12-26 03:40:48 +00:00
|
|
|
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);
|
2024-05-07 12:37:57 +00:00
|
|
|
UnityEngine.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
|
|
|
}
|
|
|
|
|
}
|