Skip to content

Commit 9368007

Browse files
authored
fix(android): Incorrect sample decoding and missing assets (#2017)
2 parents b518e33 + 561a98d commit 9368007

File tree

15 files changed

+205
-263
lines changed

15 files changed

+205
-263
lines changed

src.csharp/AlphaTab.Test/TestPlatform.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static async Task SaveFile(string name, Uint8Array data)
3737
var path = Path.Combine(RepositoryRoot.Value, name);
3838
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
3939
await using var fs = new FileStream(path, FileMode.Create);
40-
await fs.WriteAsync(data.Data.Array!, data.Data.Offset, data.Data.Count);
40+
await fs.WriteAsync(data.Buffer.Raw, (int)data.ByteOffset, (int)data.Length);
4141
}
4242

4343
public static Task<IList<string>> ListDirectory(string path)

src.csharp/AlphaTab/Core/Dom/TextDecoder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ public TextDecoder(string encoding)
1414

1515
public string Decode(ArrayBuffer data)
1616
{
17-
return _encoding.GetString(data.Raw.Array, data.Raw.Offset, data.Raw.Count);
17+
return _encoding.GetString(data.Raw, 0, (int)data.ByteLength);
1818
}
19-
}
19+
}
Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1-
using System;
2-
3-
namespace AlphaTab.Core.EcmaScript;
1+
namespace AlphaTab.Core.EcmaScript;
42

53
public class ArrayBuffer
64
{
7-
public ArraySegment<byte> Raw { get; }
8-
public double ByteLength => Raw.Count;
5+
public byte[] Raw { get; }
6+
public double ByteLength => Raw.Length;
97

108
public ArrayBuffer(double size)
119
{
12-
Raw = new ArraySegment<byte>(new byte[(int) size]);
10+
Raw = new byte[(int) size];
1311
}
1412
public ArrayBuffer(byte[] raw)
15-
{
16-
Raw = new ArraySegment<byte>(raw, 0, raw.Length);
17-
}
18-
public ArrayBuffer(ArraySegment<byte> raw)
1913
{
2014
Raw = raw;
2115
}
22-
}
16+
}

src.csharp/AlphaTab/Core/EcmaScript/DataView.cs

Lines changed: 17 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -5,138 +5,47 @@ namespace AlphaTab.Core.EcmaScript;
55
internal class DataView
66
{
77
public ArrayBuffer Buffer { get; }
8+
public double ByteOffset { get; }
9+
public double ByteLength { get; }
10+
811

912
public DataView(ArrayBuffer buffer)
1013
{
1114
Buffer = buffer;
1215
}
1316

14-
public double GetUint8(double offset)
17+
public DataView(ArrayBuffer buffer, double byteOffset, double byteLength)
1518
{
16-
return Buffer.Raw.Array![Buffer.Raw.Offset + (int) offset];
19+
Buffer = buffer;
20+
ByteOffset = byteOffset;
21+
ByteLength = byteLength;
1722
}
1823

19-
public void SetUint16(double offset, double value, bool littleEndian)
24+
public double GetInt16(double offset, bool littleEndian)
2025
{
21-
var bytes = BitConverter.GetBytes((ushort) value);
22-
if (littleEndian != BitConverter.IsLittleEndian)
26+
if (littleEndian == BitConverter.IsLittleEndian)
2327
{
24-
System.Array.Reverse(bytes);
28+
return BitConverter.ToInt16(Buffer.Raw, (int)(ByteOffset + offset));
2529
}
2630

27-
System.Buffer.BlockCopy(bytes, 0, Buffer.Raw.Array!, Buffer.Raw.Offset + (int) offset,
28-
bytes.Length);
29-
}
30-
31-
public double GetInt16(double offset, bool littleEndian)
32-
{
3331
var bytes = new byte[sizeof(short)];
34-
System.Buffer.BlockCopy(Buffer.Raw.Array!, Buffer.Raw.Offset + (int) offset, bytes, 0,
32+
System.Buffer.BlockCopy(Buffer.Raw, (int)(ByteOffset + offset), bytes, 0,
3533
bytes.Length);
36-
if (littleEndian != BitConverter.IsLittleEndian)
37-
{
38-
System.Array.Reverse(bytes);
39-
}
40-
34+
System.Array.Reverse(bytes);
4135
return BitConverter.ToInt16(bytes, 0);
4236
}
4337

44-
public void SetInt16(double offset, double value, bool littleEndian)
45-
{
46-
var bytes = BitConverter.GetBytes((short) value);
47-
if (littleEndian != BitConverter.IsLittleEndian)
48-
{
49-
System.Array.Reverse(bytes);
50-
}
51-
52-
System.Buffer.BlockCopy(bytes, 0, Buffer.Raw.Array!, Buffer.Raw.Offset + (int) offset,
53-
bytes.Length);
54-
}
55-
56-
public double GetUint32(double offset, bool littleEndian)
57-
{
58-
var bytes = new byte[sizeof(uint)];
59-
System.Buffer.BlockCopy(Buffer.Raw.Array!, Buffer.Raw.Offset + (int) offset, bytes, 0,
60-
bytes.Length);
61-
if (littleEndian != BitConverter.IsLittleEndian)
62-
{
63-
System.Array.Reverse(bytes);
64-
}
65-
66-
return BitConverter.ToUInt32(bytes, 0);
67-
}
68-
69-
public double GetInt32(double offset, bool littleEndian)
70-
{
71-
var bytes = new byte[sizeof(uint)];
72-
System.Buffer.BlockCopy(Buffer.Raw.Array!, Buffer.Raw.Offset + (int) offset, bytes, 0,
73-
bytes.Length);
74-
if (littleEndian != BitConverter.IsLittleEndian)
75-
{
76-
System.Array.Reverse(bytes);
77-
}
78-
79-
return BitConverter.ToInt32(bytes, 0);
80-
}
81-
82-
public void SetInt32(double offset, double value, bool littleEndian)
83-
{
84-
var bytes = BitConverter.GetBytes((int) value);
85-
if (littleEndian != BitConverter.IsLittleEndian)
86-
{
87-
System.Array.Reverse(bytes);
88-
}
89-
90-
System.Buffer.BlockCopy(bytes, 0, Buffer.Raw.Array!, Buffer.Raw.Offset + (int) offset, bytes
91-
.Length);
92-
}
93-
94-
public double GetUint16(double offset, bool littleEndian)
38+
public double GetFloat32(double offset, bool littleEndian = true)
9539
{
96-
var bytes = new byte[sizeof(ushort)];
97-
System.Buffer.BlockCopy(Buffer.Raw.Array!, Buffer.Raw.Offset + (int) offset, bytes, 0,
98-
bytes.Length);
99-
if (littleEndian != BitConverter.IsLittleEndian)
40+
if (littleEndian == BitConverter.IsLittleEndian)
10041
{
101-
System.Array.Reverse(bytes);
42+
return BitConverter.ToSingle(Buffer.Raw, (int)(ByteOffset + offset));
10243
}
10344

104-
return BitConverter.ToUInt16(bytes, 0);
105-
}
106-
107-
public double GetFloat32(double offset, bool littleEndian = true)
108-
{
10945
var bytes = new byte[sizeof(float)];
110-
System.Buffer.BlockCopy(Buffer.Raw.Array!, Buffer.Raw.Offset + (int) offset, bytes, 0,
46+
System.Buffer.BlockCopy(Buffer.Raw, (int)(ByteOffset + offset), bytes, 0,
11147
bytes.Length);
112-
if (littleEndian != BitConverter.IsLittleEndian)
113-
{
114-
System.Array.Reverse(bytes);
115-
}
116-
48+
System.Array.Reverse(bytes);
11749
return BitConverter.ToSingle(bytes, 0);
11850
}
119-
120-
public void SetUint8(double offset, double value)
121-
{
122-
Buffer.Raw.Array![Buffer.Raw.Offset + (int) offset] = (byte) value;
123-
}
124-
125-
public double GetInt8(double offset)
126-
{
127-
return (sbyte) Buffer.Raw.Array![Buffer.Raw.Offset + (int) offset];
128-
}
129-
130-
public double SetUint32(double offset, double value, bool littleEndian)
131-
{
132-
var bytes = BitConverter.GetBytes((uint)value);
133-
if (littleEndian != BitConverter.IsLittleEndian)
134-
{
135-
System.Array.Reverse(bytes);
136-
}
137-
138-
System.Buffer.BlockCopy(bytes, 0, Buffer.Raw.Array!, Buffer.Raw.Offset + (int) offset, bytes
139-
.Length);
140-
return value;
141-
}
14251
}
Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,35 @@
1-
using System;
2-
using System.Collections;
1+
using System.Collections;
32
using System.Collections.Generic;
4-
using System.Globalization;
5-
using System.Linq;
63
using System.Runtime.CompilerServices;
74

85
namespace AlphaTab.Core.EcmaScript;
96

107
internal class Int16Array : IEnumerable<short>
118
{
12-
private readonly short[]? _data;
13-
private readonly ArrayBuffer? _buffer;
9+
private readonly short[] _data;
1410

15-
public double Length => _data?.Length ?? _buffer.ByteLength / 2;
11+
public double Length => _data.Length;
1612

1713
public Int16Array(double size)
1814
{
1915
_data = new short[(int)size];
2016
}
2117

22-
internal Int16Array(ArrayBuffer buffer)
23-
{
24-
_buffer = buffer;
25-
}
26-
2718
public double this[double index]
2819
{
2920
[MethodImpl(MethodImplOptions.AggressiveInlining)]
30-
get
31-
{
32-
return _data != null
33-
? _data[(int)index]
34-
: GetInt16FromBuffer(index);
35-
}
21+
get => _data[(int)index];
3622
[MethodImpl(MethodImplOptions.AggressiveInlining)]
37-
set
38-
{
39-
if (_data != null)
40-
{
41-
_data[(int)index] = (short)value;
42-
}
43-
else
44-
{
45-
var bytes = BitConverter.GetBytes(value);
46-
47-
Buffer.BlockCopy(bytes,
48-
0, _buffer.Raw.Array!,
49-
(_buffer.Raw.Offset + ((int)index * sizeof(short))),
50-
bytes.Length * sizeof(short)
51-
);
52-
}
53-
}
54-
}
55-
56-
private short GetInt16FromBuffer(double index)
57-
{
58-
return BitConverter.ToInt16(_buffer.Raw.Array!,
59-
_buffer.Raw.Offset + ((int)index * sizeof(short)));
23+
set => _data[(int)index] = (short)value;
6024
}
6125

6226
public IEnumerator<short> GetEnumerator()
6327
{
64-
if (_data == null)
65-
{
66-
return Enumerable.Range(0, (int)Length)
67-
.Select(i => GetInt16FromBuffer(i))
68-
.GetEnumerator();
69-
}
70-
7128
return ((IEnumerable<short>)_data).GetEnumerator();
7229
}
7330

7431
IEnumerator IEnumerable.GetEnumerator()
7532
{
7633
return GetEnumerator();
7734
}
78-
}
35+
}

0 commit comments

Comments
 (0)