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
147 lines (124 loc) · 5.21 KB
/
Copy pathTextAsset.cs
File metadata and controls
147 lines (124 loc) · 5.21 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.Text;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
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
{
var localBytes = bytes;
return localBytes.Length == 0 ? string.Empty : DecodeString(localBytes);
}
}
public long dataSize => GetDataSize();
public override string ToString() { return text; }
public TextAsset() : this(CreateOptions.CreateNativeObject, (string)null)
{
}
public TextAsset(string text) : this(CreateOptions.CreateNativeObject, text)
{
}
public TextAsset(ReadOnlySpan<byte> bytes) : this(CreateOptions.CreateNativeObject, bytes)
{
}
internal TextAsset(CreateOptions options, string text)
{
if (options == CreateOptions.CreateNativeObject)
{
Internal_CreateInstance(this, text);
}
}
internal TextAsset(CreateOptions options, ReadOnlySpan<byte> bytes)
{
if (options == CreateOptions.CreateNativeObject)
{
Internal_CreateInstanceFromBytes(this, bytes);
}
}
public unsafe NativeArray<T> GetData<T>() where T : struct
{
long size = GetDataSize();
long stride = UnsafeUtility.SizeOf<T>();
if (size % stride != 0)
throw new ArgumentException($"Type passed to {nameof(GetData)} can't capture the asset data. Data size is {size} which is not a multiple of type size {stride}");
var arrSize = size / stride;
var array = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<T>((void*)GetDataPtr(), (int)arrSize, Allocator.None);
NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref array, GetSafetyHandle(this));
return array;
}
internal string GetPreview(int maxChars)
{
// Take 4 times as much bytes because our widest character could be 4 bytes long
return DecodeString(GetPreviewBytes(maxChars * 4));
}
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)
{
int encodingLookupLength = EncodingUtility.encodingLookup.Length;
int preambleLength;
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;
return tempEncoding.GetString(bytes, preambleLength, bytes.Length - preambleLength);
}
catch {};
}
}
preambleLength = 0;
Encoding encoding = EncodingUtility.targetEncoding;
return encoding.GetString(bytes, preambleLength, bytes.Length - preambleLength);
}
}
}