-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTranslateHelper.cs
87 lines (77 loc) · 2.85 KB
/
TranslateHelper.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace UnitaleFontMaker
{
public class TranslateHelper
{
public delegate void GetProgress(float progress);
public static char[] GetAllCharactersFile(string filePath)
{
string contents = File.ReadAllText(filePath);
HashSet<char> chars = new HashSet<char>();
for (int i = 0; i < contents.Length; i++)
{
chars.Add(contents[i]);
}
return chars.ToArray();
}
/// <summary>
/// 搜索文件夹下的所有 *.lua 并找出所有字符
/// </summary>
/// <param name="dirPath">目录</param>
/// <param name="callback">回调。用于显示进度</param>
/// <returns></returns>
public static char[] GetAllCharactersDir(string dirPath)
{
HashSet<char> chars = new HashSet<char>();
DirectoryInfo dir = new DirectoryInfo(dirPath);
DirectoryInfo[] ds = dir.GetDirectories();
foreach (DirectoryInfo d in ds) //一层足矣
{
chars.UnionWith(GetCharsFromDir(d));
}
FileInfo[] files = dir.GetFiles("*.lua");
foreach (FileInfo f in files)
{
Console.WriteLine(f.FullName);
FileStream fs = f.OpenRead();
byte[] buff = new byte[fs.Length];
fs.Read(buff, 0, (int)fs.Length); //TODO 潜在的溢出问题
fs.Close();
string contents = System.Text.Encoding.UTF8.GetString(buff);
for (int i = 0; i < contents.Length; i++)
{
chars.Add(contents[i]);
Console.WriteLine(contents[i] == '\n');
}
}
//这两个会造成显示错误
chars.Remove('\n');
chars.Remove('\r');
chars.UnionWith(new HashSet<char>(Character.ENGLISH_CHARS)); //确保有全部的英文字符
return chars.ToArray();
}
private static HashSet<char> GetCharsFromDir(DirectoryInfo dir)
{
FileInfo[] files = dir.GetFiles("*.lua");
HashSet<char> chars = new HashSet<char>();
foreach (FileInfo f in files)
{
FileStream fs = f.OpenRead();
byte[] buff = new byte[fs.Length];
fs.Read(buff, 0, (int)fs.Length); //TODO 潜在的溢出问题
fs.Close();
string contents = System.Text.Encoding.UTF8.GetString(buff);
for (int i = 0; i < contents.Length; i++)
{
chars.Add(contents[i]);
}
}
return chars;
}
}
}