forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextAsset.cs
More file actions
118 lines (101 loc) · 3.97 KB
/
Copy pathTextAsset.cs
File metadata and controls
118 lines (101 loc) · 3.97 KB
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
109
110
111
112
113
114
115
116
117
118
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UnityEngine
{
public partial class TextAsset : Object
{
// Used by MonoScript constructor to avoid creating native TextAsset object.
internal enum CreateOptions
{
None = 0,
CreateNativeObject = 1
}
// The text contents of the .txt file as a string. (RO)
public string text
{
get
{
return DecodeString(bytes);
}
}
public override string ToString() { return text; }
public TextAsset() : this(CreateOptions.CreateNativeObject, null)
{
}
public TextAsset(string text) : this(CreateOptions.CreateNativeObject, text)
{
}
internal TextAsset(CreateOptions options, string text)
{
if (options == CreateOptions.CreateNativeObject)
{
Internal_CreateInstance(this, text);
}
}
static class EncodingUtility
{
internal static readonly KeyValuePair<byte[], Encoding>[] encodingLookup;
internal static readonly Encoding targetEncoding =
Encoding.GetEncoding(Encoding.UTF8.CodePage,
new EncoderReplacementFallback("�"),
new DecoderReplacementFallback("�"));
static EncodingUtility()
{
Encoding utf32BE = new UTF32Encoding(true, true, true);
Encoding utf32LE = new UTF32Encoding(false, true, true);
Encoding utf16BE = new UnicodeEncoding(true, true, true);
Encoding utf16LE = new UnicodeEncoding(false, true, true);
Encoding utf8BOM = new UTF8Encoding(true, true);
encodingLookup = new KeyValuePair<byte[], Encoding>[]
{
new KeyValuePair<byte[], Encoding>(utf32BE.GetPreamble(), utf32BE),
new KeyValuePair<byte[], Encoding>(utf32LE.GetPreamble(), utf32LE),
new KeyValuePair<byte[], Encoding>(utf16BE.GetPreamble(), utf16BE),
new KeyValuePair<byte[], Encoding>(utf16LE.GetPreamble(), utf16LE),
new KeyValuePair<byte[], Encoding>(utf8BOM.GetPreamble(), utf8BOM),
};
}
}
internal static string DecodeString(byte[] bytes)
{
Encoding encoding = null;
int preambleLength = 0;
int encodingLookupLength = EncodingUtility.encodingLookup.Length;
for (int i = 0; i < encodingLookupLength; i++)
{
byte[] preamble = EncodingUtility.encodingLookup[i].Key;
preambleLength = preamble.Length;
if (bytes.Length >= preambleLength)
{
for (int j = 0; j < preambleLength; j++)
{
if (preamble[j] != bytes[j])
{
preambleLength = -1;
}
}
if (preambleLength < 0) continue;
try
{
Encoding tempEncoding = EncodingUtility.encodingLookup[i].Value;
string text = tempEncoding.GetString(bytes, preambleLength, bytes.Length - preambleLength);
encoding = tempEncoding;
break;
}
catch {};
}
}
if (encoding == null)
{
encoding = EncodingUtility.targetEncoding;
preambleLength = 0;
}
return encoding.GetString(bytes, preambleLength, bytes.Length - preambleLength);
}
}
}