Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 1457fb4

Browse files
增加UTF8转无BOM格式功能
1 parent c312434 commit 1457fb4

File tree

3 files changed

+180
-1
lines changed

3 files changed

+180
-1
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEditor;
4+
using UnityEngine;
5+
using System;
6+
using System.IO;
7+
using System.Text;
8+
9+
namespace Wanderer.GameFramework
10+
{
11+
public class SetUTF8WithoutBOM
12+
{
13+
[MenuItem("Assets/Set the text to UTF-8 without BOM encoding")]
14+
private static void SetTextEncoding2UTF8withoutBOM()
15+
{
16+
if (Selection.objects == null)
17+
return;
18+
19+
20+
if (Selection.assetGUIDs.Length == 1)
21+
{
22+
string selectPath = AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[0]);
23+
if (AssetDatabase.IsValidFolder(selectPath))
24+
{
25+
var assets = AssetDatabase.FindAssets("t:TextAsset", new string[] { selectPath });
26+
if (assets != null)
27+
{
28+
bool reEncoding = false;
29+
foreach (var item in assets)
30+
{
31+
string assetPath = AssetDatabase.GUIDToAssetPath(item);
32+
TextAsset asset = AssetDatabase.LoadAssetAtPath<TextAsset>(assetPath);
33+
if (EncodingType.GetType(assetPath) == Encoding.UTF8)
34+
{
35+
if (EncodingType.HasBom(asset.bytes))
36+
{
37+
byte[] newData = new byte[asset.bytes.Length - 3];
38+
Array.Copy(asset.bytes, 3, newData, 0, newData.Length);
39+
File.WriteAllBytes(assetPath, newData);
40+
Debug.Log($"UTF-8 重新编码,设置为Without BOM: {assetPath}");
41+
reEncoding = true;
42+
}
43+
}
44+
else
45+
{
46+
Debug.Log($"非UTF-8编码,请检查编码格式(暂不做自动处理) : {assetPath}");
47+
}
48+
}
49+
if (reEncoding)
50+
{
51+
AssetDatabase.Refresh();
52+
}
53+
}
54+
55+
}
56+
}
57+
}
58+
}
59+
60+
61+
62+
/// <summary>
63+
/// 获取文件的编码格式
64+
/// </summary>
65+
public class EncodingType
66+
{
67+
/// <summary>
68+
/// 给定文件的路径,读取文件的二进制数据,判断文件的编码类型
69+
/// </summary>
70+
/// <param name="FILE_NAME">文件路径</param>
71+
/// <returns>文件的编码类型</returns>
72+
public static System.Text.Encoding GetType(string FILE_NAME)
73+
{
74+
FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
75+
Encoding r = GetType(fs);
76+
fs.Close();
77+
return r;
78+
}
79+
80+
/// <summary>
81+
/// 通过给定的文件流,判断文件的编码类型
82+
/// </summary>
83+
/// <param name="fs">文件流</param>
84+
/// <returns>文件的编码类型</returns>
85+
public static System.Text.Encoding GetType(FileStream fs)
86+
{
87+
byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 };
88+
byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 };
89+
byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM
90+
Encoding reVal = Encoding.Default;
91+
92+
BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default);
93+
int i;
94+
int.TryParse(fs.Length.ToString(), out i);
95+
byte[] ss = r.ReadBytes(i);
96+
if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF))
97+
{
98+
reVal = Encoding.UTF8;
99+
}
100+
else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00)
101+
{
102+
reVal = Encoding.BigEndianUnicode;
103+
}
104+
else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41)
105+
{
106+
reVal = Encoding.Unicode;
107+
}
108+
r.Close();
109+
return reVal;
110+
111+
}
112+
113+
/// <summary>
114+
/// 判断是否是不带 BOM 的 UTF8 格式
115+
/// </summary>
116+
/// <param name="data"></param>
117+
/// <returns></returns>
118+
public static bool IsUTF8Bytes(byte[] data)
119+
{
120+
int charByteCounter = 1; //计算当前正分析的字符应还有的字节数
121+
byte curByte; //当前分析的字节.
122+
for (int i = 0; i < data.Length; i++)
123+
{
124+
curByte = data[i];
125+
if (charByteCounter == 1)
126+
{
127+
if (curByte >= 0x80)
128+
{
129+
//判断当前
130+
while (((curByte <<= 1) & 0x80) != 0)
131+
{
132+
charByteCounter++;
133+
}
134+
//标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X
135+
if (charByteCounter == 1 || charByteCounter > 6)
136+
{
137+
return false;
138+
}
139+
}
140+
}
141+
else
142+
{
143+
//若是UTF-8 此时第一位必须为1
144+
if ((curByte & 0xC0) != 0x80)
145+
{
146+
return false;
147+
}
148+
charByteCounter--;
149+
}
150+
}
151+
if (charByteCounter > 1)
152+
{
153+
throw new Exception("非预期的byte格式");
154+
}
155+
return true;
156+
}
157+
158+
159+
public static bool HasBom(byte[] data)
160+
{
161+
bool hasBom = (data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF);
162+
return hasBom;
163+
}
164+
}
165+
}

‎GameFramework/Editor/Other/SetUTF8WithoutBOM.cs.meta‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎GameFramework/Runtime/Resource/AddressablesAssetsHelper.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ public void UnloadAsset(string assetName)
102102
{
103103
var @object = _objectAsync[assetName];
104104
_objectAsync.Remove(assetName);
105-
Addressables.Release(@object);
105+
if (@object != null)
106+
{
107+
Addressables.Release(@object);
108+
}
106109
}
107110
}
108111

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /