353 lines
11 KiB
C#
353 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using UnityEngine.Gonbest.MagicCube;
|
|
|
|
namespace Thousandto.Core.Base
|
|
{
|
|
|
|
/// <summary>
|
|
/// 文件的工具函数库
|
|
/// </summary>
|
|
public static class FileUtils
|
|
{
|
|
#region//公共常量
|
|
public const string ROOT_NAME = "FileRoot";
|
|
#endregion
|
|
|
|
#region //资源路径,以及配置文件所在路径,这些路径都有可能会被编辑器使用 ---都是读取AssetRoot的路径
|
|
//资源所在目录
|
|
public static string AssetsPath
|
|
{
|
|
get
|
|
{
|
|
return PathUtils.GetResourcePath(string.Empty);
|
|
}
|
|
}
|
|
|
|
//本地版本路径
|
|
public static string LocalVersionPath
|
|
{
|
|
get
|
|
{
|
|
return PathUtils.GetConfigFilePath("LocalVersion.xml");
|
|
}
|
|
}
|
|
|
|
//类型资源索引文件路径
|
|
public static string PoolSettingsPath
|
|
{
|
|
get
|
|
{
|
|
return PathUtils.GetConfigFilePath("PoolSettings.xml");
|
|
}
|
|
}
|
|
|
|
//技能配置文件路径
|
|
public static string SkillConfigPath
|
|
{
|
|
get
|
|
{
|
|
return PathUtils.GetConfigFilePath("SkillCfg.xml");
|
|
}
|
|
}
|
|
//技能配置文件路径
|
|
public static string SkillConfigBinaryPath
|
|
{
|
|
get
|
|
{
|
|
return PathUtils.GetConfigFilePath("SkillCfgBinary.bytes");
|
|
}
|
|
}
|
|
|
|
//地图飞行传送数据
|
|
public static string FlyTeleportConfigBinaryPath
|
|
{
|
|
get
|
|
{
|
|
return PathUtils.GetConfigFilePath("FlyTeleportCfg.bytes");
|
|
}
|
|
}
|
|
//类型资源索引文件路径
|
|
public static string AllCharConfigPath
|
|
{
|
|
get
|
|
{
|
|
return PathUtils.GetConfigFilePath("AllChar_{0}.bytes");
|
|
}
|
|
}
|
|
|
|
//配置文件路径
|
|
public static string Config_GlobalConfigPath {
|
|
get {
|
|
return PathUtils.GetConfigFilePath("Config");
|
|
}
|
|
}
|
|
|
|
//地图目录路径
|
|
public static string MapInfoDirPath
|
|
{
|
|
get
|
|
{
|
|
return PathUtils.GetResourcePath("/MapInfo");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
//创建目录
|
|
public static bool CreateDirectory(string dirName)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
String[] dirs = dirName.Split('/');
|
|
if (dirs.Length > 0)
|
|
{
|
|
if (dirName[0] == '/')
|
|
{
|
|
// abs path tag on Linux OS
|
|
dirs[0] = "/" + dirs[0];
|
|
}
|
|
}
|
|
for (int i = 0; i < dirs.Length; ++i)
|
|
{
|
|
if (dirs[i].Length == 0)
|
|
{
|
|
continue;
|
|
}
|
|
if (sb.Length != 0)
|
|
{
|
|
sb.Append('/');
|
|
}
|
|
sb.Append(dirs[i]);
|
|
string cur = sb.ToString();
|
|
if (String.IsNullOrEmpty(cur))
|
|
{
|
|
continue;
|
|
}
|
|
if (!Directory.Exists(cur))
|
|
{
|
|
var info = Directory.CreateDirectory(cur);
|
|
if (null == info)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
//移除目录
|
|
public static bool RemoveTree(string dirName, bool delSelf = false)
|
|
{
|
|
int count = 0;
|
|
using (var td = new TreeDeleter())
|
|
{
|
|
WalkTree(dirName, td);
|
|
count = td.DirList.Count + td.FileList.Count;
|
|
}
|
|
if (delSelf)
|
|
{
|
|
if (Directory.Exists(dirName))
|
|
{
|
|
Directory.Delete(dirName);
|
|
}
|
|
}
|
|
return count != 0;
|
|
}
|
|
|
|
//复制树结构
|
|
public static bool CopyTree(string from, string to)
|
|
{
|
|
int count = 0;
|
|
return count != 0;
|
|
}
|
|
|
|
//遍历目录
|
|
public static void WalkTree(string dirName, ITreeWalker walker)
|
|
{
|
|
dirName = StringUtils.StandardisePath(dirName);
|
|
LinkedList<String> dirStack = new LinkedList<String>();
|
|
dirStack.AddLast(dirName);
|
|
while (dirStack.Count > 0)
|
|
{
|
|
string lastPath = dirStack.Last.Value;
|
|
dirStack.RemoveLast();
|
|
DirectoryInfo di = new DirectoryInfo(lastPath);
|
|
if (!di.Exists || (di.Attributes & FileAttributes.Hidden) != 0)
|
|
{
|
|
continue;
|
|
}
|
|
foreach (FileInfo fileInfo in di.GetFiles(walker.FileSearchPattern()))
|
|
{
|
|
// compose full file name from dirName
|
|
string f = lastPath;
|
|
if (f[f.Length - 1] == '/')
|
|
{
|
|
f += fileInfo.Name;
|
|
}
|
|
else
|
|
{
|
|
f = f + "/" + fileInfo.Name;
|
|
}
|
|
if (!walker.DoFile(f))
|
|
{
|
|
goto EXIT;
|
|
}
|
|
}
|
|
if (walker.IsRecursive())
|
|
{
|
|
foreach (DirectoryInfo dirInfo in di.GetDirectories(walker.DirectorySearchPattern()))
|
|
{
|
|
// compose full path name from dirName
|
|
string p = lastPath;
|
|
if (p[p.Length - 1] == '/')
|
|
{
|
|
p += dirInfo.Name;
|
|
}
|
|
else
|
|
{
|
|
p = p + "/" + dirInfo.Name;
|
|
}
|
|
dirStack.AddLast(p);
|
|
FileAttributes fa = File.GetAttributes(p);
|
|
if ((fa & FileAttributes.Hidden) == 0)
|
|
{
|
|
if (!walker.DoDirectory(p))
|
|
{
|
|
goto EXIT;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
EXIT:
|
|
;
|
|
}
|
|
|
|
|
|
//获取文件列表
|
|
public static List<String> GetFileList(string path, MyFunc<String, Boolean> filter, bool recursive = true)
|
|
{
|
|
var ret = new List<String>();
|
|
var fs = new FileScanner(ret, filter, recursive);
|
|
WalkTree(path, fs);
|
|
return ret;
|
|
}
|
|
|
|
//获取目录列表
|
|
public static List<String> GetDirectoryList(string path, MyFunc<String, Boolean> filter, bool recursive = true)
|
|
{
|
|
var ret = new List<String>();
|
|
var fs = new DirectoryScanner(ret, filter, recursive);
|
|
WalkTree(path, fs);
|
|
return ret;
|
|
}
|
|
|
|
#region//私有子类定义
|
|
//树结构的遍历接口
|
|
public interface ITreeWalker
|
|
{
|
|
bool IsRecursive();
|
|
// will be called for each file while WalkTree is running
|
|
bool DoFile(string name);
|
|
// will be called for each directory while WalkTree is running
|
|
bool DoDirectory(string name);
|
|
// wildmatch pattern
|
|
string FileSearchPattern();
|
|
string DirectorySearchPattern();
|
|
}
|
|
//树结构的遍历基类
|
|
public class BaseTreeWalker : ITreeWalker
|
|
{
|
|
public virtual bool IsRecursive() { return true; }
|
|
public virtual bool DoFile(string name) { return true; }
|
|
public virtual bool DoDirectory(string name) { return true; }
|
|
public virtual string FileSearchPattern() { return "*"; }
|
|
public virtual string DirectorySearchPattern() { return "*"; }
|
|
}
|
|
|
|
//树结构删除者
|
|
class TreeDeleter : BaseTreeWalker, IDisposable
|
|
{
|
|
public List<String> FileList = new List<String>();
|
|
public List<String> DirList = new List<String>();
|
|
public override bool IsRecursive() { return true; }
|
|
public override bool DoFile(string name)
|
|
{
|
|
FileList.Add(name);
|
|
return true;
|
|
}
|
|
public override bool DoDirectory(string name)
|
|
{
|
|
DirList.Add(name);
|
|
return true;
|
|
}
|
|
public void Dispose()
|
|
{
|
|
for (int i = 0; i < FileList.Count; ++i)
|
|
{
|
|
File.Delete(FileList[i]);
|
|
}
|
|
for (int i = DirList.Count - 1; i >= 0; --i)
|
|
{
|
|
Directory.Delete(DirList[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
//目录扫描类
|
|
class DirectoryScanner : BaseTreeWalker
|
|
{
|
|
List<String> _allDirs;
|
|
MyFunc<String, Boolean> _filter;
|
|
bool _recursive;
|
|
public DirectoryScanner(List<String> fs, MyFunc<String, Boolean> theFilter, bool theRecursive)
|
|
{
|
|
_allDirs = fs;
|
|
_filter = theFilter;
|
|
_recursive = theRecursive;
|
|
}
|
|
public override bool IsRecursive()
|
|
{
|
|
return _recursive;
|
|
}
|
|
public override bool DoDirectory(string name)
|
|
{
|
|
if (_filter == null || !_filter(name))
|
|
{
|
|
_allDirs.Add(name);
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
//文件的扫描类
|
|
class FileScanner : BaseTreeWalker
|
|
{
|
|
List<String> _allFiles;
|
|
MyFunc<String, Boolean> _filter;
|
|
bool _recursive;
|
|
public FileScanner(List<String> fs, MyFunc<String, Boolean> aFilter, bool aRecursive)
|
|
{
|
|
_allFiles = fs;
|
|
_filter = aFilter;
|
|
_recursive = aRecursive;
|
|
}
|
|
public override bool IsRecursive()
|
|
{
|
|
return _recursive;
|
|
}
|
|
public override bool DoFile(string name)
|
|
{
|
|
if (_filter == null || !_filter(name))
|
|
{
|
|
_allFiles.Add(name);
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|