Files
Continentis/Assets/Scripts/Mod/ModLoader.cs
SoulliesOfficial 9b1b5ca93f initial
2025-10-03 00:02:43 -04:00

110 lines
4.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.ResourceManagement.AsyncOperations;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.AddressableAssets;
#endif
namespace Continentis
{
/// <summary>
/// 负责在游戏启动时发现并加载所有Mod的资源目录 (catalog.json)
/// </summary>
public static class ModLoader
{
public const string ModCatalogVersion = "1.0.0";
public static async Task LoadAllModsAsync()
{
#if UNITY_EDITOR
var settings = AddressableAssetSettingsDefaultObject.Settings;
if (settings == null)
{
Debug.LogWarning("[ModLoader] Addressable Asset Settings not found. Skipping mod loading.");
return;
}
// 修正后的索引检查0 = Use Asset Database, 1 = Use Existing Build
// 只有在 "Use Existing Build" (Index 1) 模式下才加载外部Mod
if (settings.ActivePlayModeDataBuilderIndex == 0)
{
Debug.Log($"[ModLoader] Play Mode is 'Use Asset Database'. No external mods will be loaded. Skipping.");
return;
}
Debug.Log($"[ModLoader] Play Mode is 'Use Existing Build'. Proceeding to load external mods...");
#endif
string serverDataPath = Path.Combine(Application.dataPath, "../ServerData");
// 修正后的平台路径构建方式
string platformName = GetPlatformName();
string platformSpecificPath = Path.Combine(serverDataPath, platformName);
Debug.Log($"[ModLoader] Searching for mods in: {platformSpecificPath}");
if (!Directory.Exists(platformSpecificPath))
{
Debug.LogWarning(
$"[ModLoader] Mod directory not found at: {platformSpecificPath}. No mods will be loaded. Did you build Addressables?");
return;
}
string catalogFileName = $"catalog_{ModCatalogVersion}.json";
string catalogPath = Path.Combine(platformSpecificPath, catalogFileName);
if (File.Exists(catalogPath))
{
Debug.Log($"[ModLoader] Found catalog at: {catalogPath}");
AsyncOperationHandle<IResourceLocator> handle = Addressables.LoadContentCatalogAsync(catalogPath);
await handle.Task;
if (handle.Status == AsyncOperationStatus.Succeeded)
{
Debug.Log($"[ModLoader] Successfully loaded mod catalog.");
}
else
{
Debug.LogError($"[ModLoader] Failed to load mod catalog at: {catalogPath}");
}
}
else
{
Debug.LogWarning($"[ModLoader] No catalog.json found in the mod directory: {platformSpecificPath}");
}
}
private static string GetPlatformName()
{
#if UNITY_EDITOR
// 在Editor中我们可以直接从构建设置获取准确的平台名称字符串
return EditorUserBuildSettings.activeBuildTarget.ToString();
#else
// 在实际发布的游戏中根据Application.platform来判断
switch (Application.platform)
{
case RuntimePlatform.WindowsPlayer:
case RuntimePlatform.WindowsServer:
return "StandaloneWindows64"; // 假设我们主要构建64位
case RuntimePlatform.OSXPlayer:
case RuntimePlatform.OSXServer:
return "StandaloneOSX";
case RuntimePlatform.LinuxPlayer:
case RuntimePlatform.LinuxServer:
return "StandaloneLinux64";
// ... 在这里添加对其他平台的支持
default:
return Application.platform.ToString(); // 作为一个备用选项
}
#endif
}
}
}