Tz2/Assets/Scripts/ExamFileConfig.cs

245 lines
6.8 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;
using System.Collections.Generic;
using UnityEngine;
namespace DefaultNamespace
{
/// <summary>
/// 单个文件的下载信息
/// </summary>
[Serializable]
public class FileDownloadInfo
{
/// <summary>
/// 文件显示名称
/// </summary>
public string name;
/// <summary>
/// 文件下载URL
/// </summary>
public string url;
/// <summary>
/// 保存的文件名
/// </summary>
public string fileName;
/// <summary>
/// 本地文件路径(下载后设置)
/// </summary>
[NonSerialized]
public string localFilePath;
/// <summary>
/// 是否已下载
/// </summary>
[NonSerialized]
public bool isDownloaded;
/// <summary>
/// 下载状态
/// </summary>
[NonSerialized]
public DownloadStatus downloadStatus;
/// <summary>
/// 错误信息(如果下载失败)
/// </summary>
[NonSerialized]
public string errorMessage;
}
/// <summary>
/// 下载状态枚举
/// </summary>
public enum DownloadStatus
{
/// <summary>
/// 未开始
/// </summary>
NotStarted,
/// <summary>
/// 下载中
/// </summary>
Downloading,
/// <summary>
/// 下载完成
/// </summary>
Completed,
/// <summary>
/// 下载失败
/// </summary>
Failed
}
/// <summary>
/// 单个考试类型的文件配置
/// </summary>
[Serializable]
public class ExamFileConfig
{
/// <summary>
/// 考试类型名称
/// </summary>
public string examType;
/// <summary>
/// 该考试类型需要下载的文件列表
/// </summary>
public List<FileDownloadInfo> files = new List<FileDownloadInfo>();
/// <summary>
/// 获取指定名称的文件信息
/// </summary>
/// <param name="fileName">文件名称</param>
/// <returns>文件信息如果不存在则返回null</returns>
public FileDownloadInfo GetFileByName(string fileName)
{
return files.Find(f => f.fileName == fileName);
}
/// <summary>
/// 获取已下载的文件列表
/// </summary>
/// <returns>已下载的文件列表</returns>
public List<FileDownloadInfo> GetDownloadedFiles()
{
return files.FindAll(f => f.isDownloaded);
}
/// <summary>
/// 获取所有文件的本地路径(仅已下载的文件)
/// </summary>
/// <returns>已下载文件的本地路径列表</returns>
public List<string> GetDownloadedFilePaths()
{
var downloadedFiles = GetDownloadedFiles();
var paths = new List<string>();
foreach (var file in downloadedFiles)
{
if (!string.IsNullOrEmpty(file.localFilePath) && System.IO.File.Exists(file.localFilePath))
{
paths.Add(file.localFilePath);
}
}
return paths;
}
/// <summary>
/// 检查是否所有文件都已下载
/// </summary>
/// <returns>是否所有文件都已下载</returns>
public bool AreAllFilesDownloaded()
{
return files.TrueForAll(f => f.isDownloaded);
}
// /// <summary>
// /// 获取下载进度0-1之间
// /// </summary>
// /// <returns>下载进度</returns>
// public float GetDownloadProgress()
// {
// if (files.Count == 0) return 1f;
//
// int downloadedCount = files.Count(f => f.isDownloaded);
// return (float)downloadedCount / files.Count;
// }
}
/// <summary>
/// 考试文件配置管理器
/// </summary>
[Serializable]
public class ExamFileConfigManager
{
/// <summary>
/// 所有考试类型的文件配置
/// </summary>
public List<ExamFileConfig> examFileConfigs = new List<ExamFileConfig>();
/// <summary>
/// 根据考试类型获取配置
/// </summary>
/// <param name="examType">考试类型名称</param>
/// <returns>考试文件配置如果不存在则返回null</returns>
public ExamFileConfig GetConfigByExamType(string examType)
{
return examFileConfigs.Find(c => c.examType == examType);
}
/// <summary>
/// 添加或更新考试配置
/// </summary>
/// <param name="config">考试文件配置</param>
public void AddOrUpdateConfig(ExamFileConfig config)
{
var existingConfig = GetConfigByExamType(config.examType);
if (existingConfig != null)
{
// 更新现有配置
int index = examFileConfigs.IndexOf(existingConfig);
examFileConfigs[index] = config;
}
else
{
// 添加新配置
examFileConfigs.Add(config);
}
}
/// <summary>
/// 验证配置完整性
/// </summary>
/// <returns>验证结果</returns>
public bool Validate()
{
foreach (var config in examFileConfigs)
{
if (string.IsNullOrEmpty(config.examType))
{
Debug.LogError($"考试类型名称不能为空");
return false;
}
if (config.files == null || config.files.Count == 0)
{
Debug.LogWarning($"考试类型 '{config.examType}' 没有配置任何文件");
continue;
}
foreach (var file in config.files)
{
if (string.IsNullOrEmpty(file.name))
{
Debug.LogError($"考试类型 '{config.examType}' 存在文件名称为空的情况");
return false;
}
if (string.IsNullOrEmpty(file.url))
{
Debug.LogError($"考试类型 '{config.examType}' 文件 '{file.name}' 的URL为空");
return false;
}
if (string.IsNullOrEmpty(file.fileName))
{
Debug.LogError($"考试类型 '{config.examType}' 文件 '{file.name}' 的文件名为空");
return false;
}
}
}
return true;
}
}
}