2026-06-19 13:18:04 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient.Scripting
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class DataPath
|
|
|
|
|
|
{
|
|
|
|
|
|
private static string? relativePath;
|
|
|
|
|
|
private static string? absolutePath;
|
|
|
|
|
|
|
|
|
|
|
|
public enum PathType
|
|
|
|
|
|
{
|
|
|
|
|
|
Relative,
|
|
|
|
|
|
Absolute
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 初始化数据目录(修正版)
|
2026-06-21 10:53:11 +08:00
|
|
|
|
/// Initialize the data directory (Revised version)
|
2026-06-19 13:18:04 +08:00
|
|
|
|
/// 需要手动传入脚本文件名(不含.cs扩展名)
|
2026-06-21 10:53:11 +08:00
|
|
|
|
/// Requires manually passing the script file name (without the .cs extension)
|
2026-06-19 13:18:04 +08:00
|
|
|
|
/// </summary>
|
2026-06-21 10:53:11 +08:00
|
|
|
|
/// <param name="scriptName">脚本文件名(不含扩展名)Script file name (without extension)</param>
|
2026-06-19 13:18:04 +08:00
|
|
|
|
public static void Init(string scriptName)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(scriptName))
|
2026-06-21 10:53:11 +08:00
|
|
|
|
throw new ArgumentNullException(nameof(scriptName), "Script name cannot be null or empty(脚本名称不能为空)");
|
2026-06-19 13:18:04 +08:00
|
|
|
|
|
2026-06-21 10:53:11 +08:00
|
|
|
|
// 计算路径 Calculate the path
|
2026-06-19 13:18:04 +08:00
|
|
|
|
relativePath = scriptName;
|
|
|
|
|
|
absolutePath = Path.Combine(AppContext.BaseDirectory, scriptName);
|
|
|
|
|
|
|
2026-06-21 10:53:11 +08:00
|
|
|
|
// 创建目录 Create the directory
|
2026-06-19 13:18:04 +08:00
|
|
|
|
if (!Directory.Exists(absolutePath))
|
|
|
|
|
|
Directory.CreateDirectory(absolutePath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取路径
|
2026-06-21 10:53:11 +08:00
|
|
|
|
/// Get the path
|
2026-06-19 13:18:04 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static string Get(PathType pathType = PathType.Relative)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(relativePath) || string.IsNullOrEmpty(absolutePath))
|
2026-06-21 10:53:11 +08:00
|
|
|
|
throw new InvalidOperationException("Please call DataPath.Init() first to initialize(请先调用DataPath.Init()初始化)");
|
2026-06-19 13:18:04 +08:00
|
|
|
|
|
|
|
|
|
|
return pathType switch
|
|
|
|
|
|
{
|
|
|
|
|
|
PathType.Relative => relativePath,
|
|
|
|
|
|
PathType.Absolute => absolutePath,
|
|
|
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(pathType))
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-06-21 10:53:11 +08:00
|
|
|
|
}
|