|
| 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 | + } |
0 commit comments