Files
Continentis/Assets/Scripts/Mod/Manifests/ModManifest.cs
SoulliesOfficial 1bca620966 AttackResult修改
2025-11-08 22:22:43 -05:00

129 lines
5.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Continentis.MainGame;
using Continentis.MainGame.Card;
using Continentis.MainGame.Character;
using Continentis.MainGame.Equipment;
using Continentis.MainGame.UI;
using NaughtyAttributes;
using SLSFramework.UModAssistance;
using UMod;
using UnityEngine;
using UnityEngine.Serialization;
namespace Continentis.Mods
{
[CreateAssetMenu(fileName = "ModManifest", menuName = "Continentis/Mod/Manifest", order = 1)]
public partial class ModManifest : ScriptableObject
{
public string inEditorModFolder;
public List<string> keywordDataIDList = new List<string>();
public List<string> cardDataIDList = new List<string>();
public List<string> characterDataIDList = new List<string>();
public List<string> equipmentDataIDList = new List<string>();
public List<string> hudDataIDList = new List<string>();
public List<TextAsset> localizationFiles = new List<TextAsset>();
public void SaveToDatabase(ModHost host)
{
SaveIDListToDatabase<KeywordData>(host, keywordDataIDList);
SaveIDListToDatabase<CardData>(host, cardDataIDList);
SaveIDListToDatabase<CharacterData>(host, characterDataIDList);
SaveIDListToDatabase<EquipmentData>(host, equipmentDataIDList);
SaveIDListToDatabase<HUDData>(host, hudDataIDList);
}
private void SaveIDListToDatabase<T>(ModHost host, List<string> idList) where T : ScriptableObject
{
ModManager.Database.TryAdd(typeof(T), new Dictionary<string, ScriptableObject>());
foreach (var assetName in idList)
{
T data = host.Assets.Load<T>(assetName);
if (!ModManager.Database[typeof(T)].TryAdd(assetName, data))
{
Debug.LogWarning($"Asset '{assetName}' already exists in the database. Skipping addition.");
}
}
}
}
#if UNITY_EDITOR
public partial class ModManifest
{
[Button]
private void CollectAllKeywordData() => keywordDataIDList = CollectData<KeywordData>();
[Button]
private void CollectAllCardData() => cardDataIDList = CollectData<CardData>();
[Button]
private void CollectAllCharacterData() => characterDataIDList = CollectData<CharacterData>();
[Button]
private void CollectAllEquipmentData() => equipmentDataIDList = CollectData<EquipmentData>();
[Button]
private void CollectAllHUDData() => hudDataIDList = CollectData<HUDData>();
[Button]
private void CollectAllLocalizations() => localizationFiles = CollectLocalizations();
}
public partial class ModManifest
{
private List<string> CollectData<T>() where T : ScriptableObject
{
string inEditorModPath = "Assets/Mods/" + inEditorModFolder;
string dataTypeName = typeof(T).Name;
string[] guids = UnityEditor.AssetDatabase.FindAssets($"t:{dataTypeName}", new[] { inEditorModPath });
List<string> collectedDataIDList = new List<string>();
foreach (string guid in guids)
{
string path = UnityEditor.AssetDatabase.GUIDToAssetPath(guid);
T data = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(path);
string assetFileName = System.IO.Path.GetFileNameWithoutExtension(path);
if (!Regex.IsMatch(assetFileName, @"^\w+_\w+_.+$"))
{
Debug.LogWarning("Asset name does not follow the 'Type_ModName_AssetName' format: " + assetFileName);
}
if (data != null)
{
collectedDataIDList.Add(assetFileName);
}
}
Debug.Log($"Collected {collectedDataIDList.Count} CardData assets.");
return collectedDataIDList;
}
private List<TextAsset> CollectLocalizations()
{
string inEditorModPath = "Assets/Mods/" + inEditorModFolder;
string dataTypeName = nameof(TextAsset);
string[] guids = UnityEditor.AssetDatabase.FindAssets($"t:{dataTypeName}", new[] { inEditorModPath });
List<TextAsset> collectedTextAssets = new List<TextAsset>();
Debug.Log($"Found {guids.Length} TextAsset assets.");
foreach (string guid in guids)
{
string path = UnityEditor.AssetDatabase.GUIDToAssetPath(guid);
string assetFileName = System.IO.Path.GetFileNameWithoutExtension(path);
TextAsset data = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>(path);
if (assetFileName.Contains("Localization"))
{
collectedTextAssets.Add(data);
Debug.Log($"Collected localization TextAsset: {assetFileName}");
}
}
Debug.Log($"Collected {collectedTextAssets.Count} localization TextAssets.");
return collectedTextAssets;
}
}
#endif
}