ningshuxia
2023-02-06 1fc0b4ed96d4c4b1ff7142926239998de32b2ada
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
namespace IStation.DAL.LocalFile
{
    /// <summary>
    /// 文件辅助类
    /// </summary>
    public class FileHelper
    {
        private const string _extension = ".txt";//文件扩展名
 
        /// <summary>
        /// 获取数据文件夹路径
        /// </summary>
        public static string GetDataPath()
        {
            var path = Path.Combine(LocalFileConfig.RootDirectory, LocalFileConfig.DataFolder);
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            return path;
        }
 
        /// <summary>
        /// 获取工作文件夹路径
        /// </summary>
        public static string GetWorkPath()
        {
            var dataPath = GetDataPath();
            var workPath = Path.Combine(dataPath, LocalFileConfig.WorkFolder);
            if (!Directory.Exists(workPath))
                Directory.CreateDirectory(workPath);
            return workPath;
        }
 
        /// <summary>
        /// 获取项目文件夹路径
        /// </summary>
        public static string GetProjectPath(long projectId)
        {
            var workPath = GetWorkPath();
            var projectPath = Path.Combine(workPath, projectId.ToString());
            if (!Directory.Exists(projectPath))
                Directory.CreateDirectory(projectPath);
            return projectPath;
        }
 
        /// <summary>
        /// 获取场景文件夹路径
        /// </summary>
        public static string GetSceneFolder(long projectId, long sceneId)
        {
            var projectPath = GetProjectPath(projectId);
            var scenePath = Path.Combine(projectPath, sceneId.ToString());
            if (!Directory.Exists(scenePath))
                Directory.CreateDirectory(scenePath);
            return scenePath;
        }
 
        /// <summary>
        /// 获取文件路径
        /// </summary>
        public static string GetFilePath<T>(string folderPath) where T : class
        {
            if (!Directory.Exists(folderPath))
                Directory.CreateDirectory(folderPath);
            var objType = typeof(T);
            var fileName = Path.Combine(folderPath, objType.Name + _extension);
            return fileName;
        }
 
        /// <summary>
        /// 保存对象到文件中
        /// </summary>
        public static void SaveObjectFile<T>(string fileName, T t) where T : class
        {
            var str = JsonHelper.Object2FormatJson(t);
            File.WriteAllText(fileName, str);
        }
 
        /// <summary>
        /// 获取文件对象
        /// </summary>
        public static T GetFileObject<T>(string fileName) where T : class
        {
            if (!File.Exists(fileName))
                return default;
            var str = File.ReadAllText(fileName);
            if (string.IsNullOrEmpty(str))
                return default;
            return JsonHelper.Json2Object<T>(str);
        }
 
 
        /// <summary>
        /// 获取Id
        /// </summary> 
        public static long NextId()
        {
            return SnowflakeIdHelper.NextId();
        }
 
    }
}