TH1/Unity/Assets/Scripts/TH1_Logic/MatchConfig/MatchConfigManager.cs
2026-01-22 17:29:58 +08:00

98 lines
3.0 KiB
C#

/*
* @Author: 白哉
* @Description:
* @Date: 2025年09月05日 星期五 16:09:49
* @Modify:
*/
using System.Collections.Generic;
using System.IO;
using Logic.CrashSight;
using MemoryPack;
using RuntimeData;
using UnityEngine;
namespace TH1_Logic.MatchConfig
{
public class MatchConfigManager
{
public static MatchConfigManager Instance = new MatchConfigManager();
public MatchLevelData LevelData;
public void Init()
{
LevelData = LoadMatchLevelData();
if (LevelData == null) LevelData = new MatchLevelData();
}
public MatchLevelData GetMatchLevelData()
{
if (LevelData == null) LevelData = new MatchLevelData();
return LevelData;
}
public MapConfig GetMatchConfig(uint id)
{
return LevelData.LevelConfigs.GetValueOrDefault(id);
}
public void AddMatchConfig(uint id)
{
if (LevelData.LevelConfigs.ContainsKey(id)) return;
var mapConfig = new MapConfig();
mapConfig.Id = id;
LevelData.LevelConfigs[id] = mapConfig;
}
public MatchLevelData LoadMatchLevelData()
{
try
{
string resourcePath = $"MatchLevelData/LevelData";
TextAsset textAsset = Resources.Load<TextAsset>(resourcePath);
if (textAsset == null)
{
LogSystem.LogWarning($"LoadMatchLevelData: 未找到资源 {resourcePath}");
return null;
}
MatchLevelData matchConfig = MemoryPackSerializer.Deserialize<MatchLevelData>(textAsset.bytes);
LogSystem.LogInfo($"LoadMatchLevelData: 关卡配置加载成功");
return matchConfig;
}
catch (System.Exception e)
{
LogSystem.LogWarning($"LoadMatchLevelData: 加载失败 - {e.Message}");
return null;
}
}
public void SaveMatchLevelData()
{
#if UNITY_EDITOR
if (LevelData == null) return;
try
{
// 确保文件夹存在
string folderPath = Path.Combine(Application.dataPath, "Resources", "MatchLevelData");
if (!Directory.Exists(folderPath)) Directory.CreateDirectory(folderPath);
string filePath = Path.Combine(folderPath, $"LevelData.bytes");
byte[] bytes = MemoryPackSerializer.Serialize(LevelData);
File.WriteAllBytes(filePath, bytes);
// 刷新 AssetDatabase
UnityEditor.AssetDatabase.Refresh();
LogSystem.LogInfo($"SaveMatchLevelData: MatchLevelData 保存成功,路径: {filePath}");
}
catch (System.Exception e)
{
LogSystem.LogWarning($"SaveMatchLevelData: 保存失败 - {e.Message}");
}
#endif
}
}
}