Skip to content

Commit dd9a810

Browse files
RagathRagath
authored andcommitted
uint tile data, resolves #13
1 parent 3af08b8 commit dd9a810

File tree

10 files changed

+43
-47
lines changed

10 files changed

+43
-47
lines changed

TiledLib.Pipeline/TiledMapImporter.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ public override Map Import(string filename, ContentImporterContext context)
1111
{
1212
string GetPath(string path) => Path.IsPathRooted(path) ? path : Path.Combine(Path.GetDirectoryName(filename), path);
1313

14-
using (var stream = File.OpenRead(filename))
15-
{
16-
var map = Map.FromStream(stream, ts => File.OpenRead(GetPath(ts.Source)));
14+
using var stream = File.OpenRead(filename);
15+
var map = Map.FromStream(stream, ts => File.OpenRead(GetPath(ts.Source)));
1716

18-
foreach (var ts in map.Tilesets.OfType<ExternalTileset>())
19-
ts.LoadTileset();
17+
foreach (var ts in map.Tilesets.OfType<ExternalTileset>())
18+
ts.LoadTileset();
2019

21-
return map;
22-
}
20+
return map;
2321
}
2422
}

TiledLib.Tests/External_tilesetTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Tileset LoadTileset(ExternalTileset t)
2323
ts.LoadTileset(LoadTileset);
2424

2525
var q = from t in map.Tilesets
26-
from i in Enumerable.Range(t.FirstGid, t.Rows * t.Columns)
26+
from i in Enumerable.Range(t.FirstGid, t.Rows * t.Columns).Select(i => (uint)i)
2727
select new
2828
{
2929
index = i,

TiledLib/ExternalTileset.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public class ExternalTileset : ITileset
3232
public Dictionary<int, Dictionary<string, string>> TileProperties => Tileset.TileProperties;
3333
public Dictionary<int, Frame[]> TileAnimations => Tileset.TileAnimations;
3434

35-
public Tile this[int gid] => Tileset[gid];
36-
public string this[int gid, string property] => Tileset[gid, property];
35+
public Tile this[uint gid] => Tileset[gid];
36+
public string this[uint gid, string property] => Tileset[gid, property];
3737

3838
public void LoadTileset()
3939
{
@@ -44,7 +44,7 @@ public void LoadTileset(Func<ExternalTileset, Tileset> loader)
4444
_Tileset = new Lazy<Tileset>(() =>
4545
{
4646
var tileset = loader(this);
47-
tileset.FirstGid = this.FirstGid;
47+
tileset.FirstGid = this.FirstGid & (int)TileOrientation.MaskID;
4848
return tileset;
4949
});
5050
LoadTileset();

TiledLib/ITileset.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
[JsonConverter(typeof(TilesetConverter))]
44
public interface ITileset
55
{
6-
Tile this[int gid] { get; }
7-
string this[int gid, string property] { get; }
6+
Tile this[uint gid] { get; }
7+
string this[uint gid, string property] { get; }
88

99
int Columns { get; }
1010
int FirstGid { get; }

TiledLib/Layer/tilelayer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class TileLayer : BaseLayer, IXmlSerializable
1616

1717
[Required]
1818
[JsonPropertyName("data")]
19-
public int[] Data { get; set; }
19+
public uint[] Data { get; set; }
2020

2121
public XmlSchema GetSchema() => null;
2222

TiledLib/LayerConverter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@ public override BaseLayer Read(ref Utf8JsonReader reader, Type typeToConvert, Js
3535
switch (tl.Compression)
3636
{
3737
case null:
38-
tl.Data = new int[buffer.Length / sizeof(int)];
38+
tl.Data = new uint[buffer.Length / sizeof(uint)];
3939
Buffer.BlockCopy(buffer, 0, tl.Data, 0, buffer.Length);
4040
break;
4141
case "zlib":
4242
using (var mStream = new MemoryStream(buffer))
4343
{
4444
using var stream = new Zlib.ZlibStream(mStream, Zlib.CompressionMode.Decompress);
45-
var bufferSize = result.Width * result.Height * sizeof(int);
45+
var bufferSize = result.Width * result.Height * sizeof(uint);
4646
Array.Resize(ref buffer, bufferSize);
4747
stream.Read(buffer, 0, bufferSize);
4848

4949
if (stream.ReadByte() != -1)
5050
throw new JsonException();
5151

52-
tl.Data = new int[result.Width * result.Height];
52+
tl.Data = new uint[result.Width * result.Height];
5353
Buffer.BlockCopy(buffer, 0, tl.Data, 0, buffer.Length);
5454
}
5555
break;
@@ -64,7 +64,7 @@ public override BaseLayer Read(ref Utf8JsonReader reader, Type typeToConvert, Js
6464
if (stream.ReadByte() != -1)
6565
throw new JsonException();
6666

67-
tl.Data = new int[result.Width * result.Height];
67+
tl.Data = new uint[result.Width * result.Height];
6868
Buffer.BlockCopy(buffer, 0, tl.Data, 0, buffer.Length);
6969
}
7070
break;

TiledLib/TileOrientation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
namespace TiledLib;
22

3-
public enum TileOrientation : int
3+
public enum TileOrientation : uint
44
{
55
Default = 0b00000000_00000000_00000000_00000000,
6-
FlippedH = unchecked((int)0b10000000_00000000_00000000_00000000),
6+
FlippedH = 0b10000000_00000000_00000000_00000000,
77
FlippedV = 0b01000000_00000000_00000000_00000000,
88
FlippedAD = 0b00100000_00000000_00000000_00000000,
99
Rotate90CW = FlippedAD | FlippedH,

TiledLib/Tileset.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class Tileset : ITileset, IXmlSerializable
4646
[JsonIgnore] //TODO: Add json support
4747
public Dictionary<int, Frame[]> TileAnimations { get; init; } = new Dictionary<int, Frame[]>();
4848

49-
public Tile this[int gid]
49+
public Tile this[uint gid]
5050
{
5151
get
5252
{
@@ -57,7 +57,7 @@ public Tile this[int gid]
5757

5858
var columns = Columns;
5959
var rows = Rows;
60-
var index = Utils.GetId(gid) - FirstGid;
60+
var index = Utils.GetId(gid) - (int)FirstGid;
6161
if (index < 0 || index >= rows * columns)
6262
throw new ArgumentOutOfRangeException();
6363

@@ -74,13 +74,13 @@ public Tile this[int gid]
7474
}
7575
}
7676

77-
public string this[int gid, string property]
77+
public string this[uint gid, string property]
7878
{
7979
get
8080
{
81-
gid = Utils.GetId(gid);
82-
return gid != 0
83-
&& TileProperties.TryGetValue(gid - FirstGid, out var tile)
81+
var id = Utils.GetId(gid);
82+
return id != 0
83+
&& TileProperties.TryGetValue(id - FirstGid, out var tile)
8484
&& tile.TryGetValue(property, out var value) ? value : default;
8585
}
8686
}

TiledLib/TmxMisc.cs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ static Frame[] ReadAnimation(this XmlReader reader)
136136
.ToArray();
137137
}
138138

139-
static int[] ReadCSV(this XmlReader reader, int size)
139+
static uint[] ReadCSV(this XmlReader reader, int size)
140140
{
141141
var data = reader.ReadElementContentAsString()
142142
.Split(new[] { '\r', '\n', ',' }, StringSplitOptions.RemoveEmptyEntries)
143-
.Select(int.Parse)
143+
.Select(uint.Parse)
144144
.ToArray();
145145

146146
if (data.Length == size)
@@ -149,41 +149,39 @@ static int[] ReadCSV(this XmlReader reader, int size)
149149
throw new XmlException();
150150
}
151151

152-
static int[] ReadBase64(this XmlReader reader, int count)
152+
static uint[] ReadBase64(this XmlReader reader, int count)
153153
{
154-
var buffer = new byte[count * sizeof(int)];
154+
var buffer = new byte[count * sizeof(uint)];
155155
var size = reader.ReadElementContentAsBase64(buffer, 0, buffer.Length);
156156
if (reader.ReadElementContentAsBase64(buffer, 0, buffer.Length) != 0)
157157
throw new InvalidDataException();
158-
var data = new int[size / sizeof(int)];
158+
var data = new uint[size / sizeof(uint)];
159159
Buffer.BlockCopy(buffer, 0, data, 0, size);
160160
return data;
161161
}
162-
static int[] ReadBase64Decompress<T>(this XmlReader reader, Func<Stream, Zlib.CompressionMode, T> streamFactory, int size)
162+
static uint[] ReadBase64Decompress<T>(this XmlReader reader, Func<Stream, Zlib.CompressionMode, T> streamFactory, int size)
163163
where T : Stream
164164
{
165-
var buffer = new byte[size * sizeof(int)];
165+
var buffer = new byte[size * sizeof(uint)];
166166

167167
var total = reader.ReadElementContentAsBase64(buffer, 0, buffer.Length);
168168
if (reader.ReadElementContentAsBase64(buffer, 0, buffer.Length) != 0)
169169
throw new InvalidDataException();
170170

171-
using (var mstream = new MemoryStream(buffer, 0, total))
172-
using (var stream = streamFactory(mstream, Zlib.CompressionMode.Decompress))
171+
using var mstream = new MemoryStream(buffer, 0, total);
172+
using var stream = streamFactory(mstream, Zlib.CompressionMode.Decompress);
173+
var data = new uint[size];
174+
var pos = 0;
175+
int count;
176+
while ((count = stream.Read(buffer, 0, buffer.Length)) > 0)
173177
{
174-
var data = new int[size];
175-
var pos = 0;
176-
int count;
177-
while ((count = stream.Read(buffer, 0, buffer.Length)) > 0)
178-
{
179-
Buffer.BlockCopy(buffer, 0, data, pos, count);
180-
pos += count;
181-
}
182-
return data;
178+
Buffer.BlockCopy(buffer, 0, data, pos, count);
179+
pos += count;
183180
}
181+
return data;
184182
}
185183

186-
public static int[] ReadData(this XmlReader reader, int count, out string encoding, out string compression)
184+
public static uint[] ReadData(this XmlReader reader, int count, out string encoding, out string compression)
187185
{
188186
encoding = reader["encoding"];
189187
compression = reader["compression"];

TiledLib/Utils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ internal static bool ContainsJson(this StreamReader reader)
3636
return true;
3737
}
3838

39-
public static int GetId(int gid) => gid & (int)TileOrientation.MaskID;
40-
public static TileOrientation GetOrientation(int gid) => (TileOrientation)gid & TileOrientation.MaskFlip;
39+
public static int GetId(uint gid) => (int)(gid & (uint)TileOrientation.MaskID);
40+
public static TileOrientation GetOrientation(uint gid) => (TileOrientation)gid & TileOrientation.MaskFlip;
4141
}

0 commit comments

Comments
 (0)