22using System ;
33using System . Collections . Generic ;
44using System . IO ;
5- using System . Text ;
65using TSMapEditor . GameMath ;
76using TSMapEditor . Models ;
87using TSMapEditor . UI ;
@@ -40,8 +39,6 @@ public abstract class CopiedMapEntry
4039 public Point2D Offset { get ; protected set ; }
4140 public abstract CopiedEntryType EntryType { get ; }
4241
43- private byte [ ] buffer ;
44-
4542 protected CopiedMapEntry ( )
4643 {
4744 }
@@ -51,55 +48,16 @@ protected CopiedMapEntry(Point2D offset)
5148 Offset = offset ;
5249 }
5350
54- protected int ReadInt ( Stream stream )
55- {
56- if ( stream . Read ( buffer , 0 , 4 ) != 4 )
57- throw new CopiedMapDataSerializationException ( "Failed to read integer from stream: end of stream" ) ;
58-
59- return BitConverter . ToInt32 ( buffer , 0 ) ;
60- }
61-
62- protected long ReadLong ( Stream stream )
63- {
64- if ( stream . Read ( buffer , 0 , 8 ) != 8 )
65- throw new CopiedMapDataSerializationException ( "Failed to read integer from stream: end of stream" ) ;
66-
67- return BitConverter . ToInt64 ( buffer , 0 ) ;
68- }
69-
70- protected string ReadASCIIString ( Stream stream )
71- {
72- int length = ReadInt ( stream ) ;
73- byte [ ] stringBuffer = new byte [ length ] ;
74- if ( stream . Read ( stringBuffer , 0 , length ) != length )
75- throw new CopiedMapDataSerializationException ( "Failed to read string from stream: end of stream" ) ;
76-
77- string result = Encoding . ASCII . GetString ( stringBuffer ) ;
78- return result ;
79- }
80-
81- protected byte [ ] ASCIIStringToBytes ( string str )
82- {
83- byte [ ] buffer = new byte [ sizeof ( int ) + str . Length ] ;
84- Array . Copy ( BitConverter . GetBytes ( str . Length ) , buffer , sizeof ( int ) ) ;
85- byte [ ] stringBytes = Encoding . ASCII . GetBytes ( str ) ;
86- Array . Copy ( stringBytes , 0 , buffer , sizeof ( int ) , stringBytes . Length ) ;
87- return buffer ;
88- }
89-
9051 /// <summary>
9152 /// Reads all of the map entry's data from a stream.
9253 /// </summary>
9354 /// <param name="stream">The stream to read the data from.</param>
9455 public void ReadData ( Stream stream )
9556 {
96- buffer = new byte [ 8 ] ;
97-
98- int x = ReadInt ( stream ) ;
99- int y = ReadInt ( stream ) ;
57+ int x = StreamHelpers . ReadInt ( stream ) ;
58+ int y = StreamHelpers . ReadInt ( stream ) ;
10059 Offset = new Point2D ( x , y ) ;
10160 ReadCustomData ( stream ) ;
102- buffer = null ; // Free memory
10361 }
10462
10563 /// <summary>
@@ -159,7 +117,7 @@ protected override byte[] GetCustomData()
159117
160118 protected override void ReadCustomData ( Stream stream )
161119 {
162- TileIndex = ReadInt ( stream ) ;
120+ TileIndex = StreamHelpers . ReadInt ( stream ) ;
163121 SubTileIndex = ( byte ) stream . ReadByte ( ) ;
164122 HeightOffset = ( byte ) stream . ReadByte ( ) ;
165123 }
@@ -184,7 +142,7 @@ public CopiedOverlayEntry(Point2D offset, string overlayTypeName, int frameIndex
184142
185143 protected override byte [ ] GetCustomData ( )
186144 {
187- byte [ ] nameBytes = ASCIIStringToBytes ( OverlayTypeName ) ;
145+ byte [ ] nameBytes = StreamHelpers . ASCIIStringToBytes ( OverlayTypeName ) ;
188146 byte [ ] buffer = new byte [ nameBytes . Length + sizeof ( int ) ] ;
189147 Array . Copy ( nameBytes , buffer , nameBytes . Length ) ;
190148 Array . Copy ( BitConverter . GetBytes ( FrameIndex ) , 0 , buffer , nameBytes . Length , sizeof ( int ) ) ;
@@ -193,8 +151,8 @@ protected override byte[] GetCustomData()
193151
194152 protected override void ReadCustomData ( Stream stream )
195153 {
196- OverlayTypeName = ReadASCIIString ( stream ) ;
197- FrameIndex = ReadInt ( stream ) ;
154+ OverlayTypeName = StreamHelpers . ReadASCIIString ( stream ) ;
155+ FrameIndex = StreamHelpers . ReadInt ( stream ) ;
198156 }
199157 }
200158
@@ -215,13 +173,13 @@ public CopiedSmudgeEntry(Point2D offset, string smudgeTypeName) : base(offset)
215173
216174 protected override byte [ ] GetCustomData ( )
217175 {
218- byte [ ] nameBytes = ASCIIStringToBytes ( SmudgeTypeName ) ;
176+ byte [ ] nameBytes = StreamHelpers . ASCIIStringToBytes ( SmudgeTypeName ) ;
219177 return nameBytes ;
220178 }
221179
222180 protected override void ReadCustomData ( Stream stream )
223181 {
224- SmudgeTypeName = ReadASCIIString ( stream ) ;
182+ SmudgeTypeName = StreamHelpers . ReadASCIIString ( stream ) ;
225183 }
226184 }
227185
@@ -240,12 +198,12 @@ public CopiedObjectEntry(Point2D offset, string objectTypeName) : base(offset)
240198
241199 protected override byte [ ] GetCustomData ( )
242200 {
243- return ASCIIStringToBytes ( ObjectTypeName ) ;
201+ return StreamHelpers . ASCIIStringToBytes ( ObjectTypeName ) ;
244202 }
245203
246204 protected override void ReadCustomData ( Stream stream )
247205 {
248- ObjectTypeName = ReadASCIIString ( stream ) ;
206+ ObjectTypeName = StreamHelpers . ReadASCIIString ( stream ) ;
249207 }
250208 }
251209
@@ -285,9 +243,9 @@ public CopiedTechnoEntry(Point2D offset, string objectTypeName, string ownerName
285243
286244 protected override byte [ ] GetCustomData ( )
287245 {
288- byte [ ] objectTypeBuffer = ASCIIStringToBytes ( ObjectTypeName ) ;
289- byte [ ] ownerBuffer = ASCIIStringToBytes ( OwnerHouseName ) ;
290- byte [ ] missionBuffer = ASCIIStringToBytes ( Mission ) ;
246+ byte [ ] objectTypeBuffer = StreamHelpers . ASCIIStringToBytes ( ObjectTypeName ) ;
247+ byte [ ] ownerBuffer = StreamHelpers . ASCIIStringToBytes ( OwnerHouseName ) ;
248+ byte [ ] missionBuffer = StreamHelpers . ASCIIStringToBytes ( Mission ) ;
291249 byte [ ] result = new byte [ sizeof ( int ) + sizeof ( int ) + 1 + objectTypeBuffer . Length + ownerBuffer . Length + missionBuffer . Length ] ;
292250 Array . Copy ( BitConverter . GetBytes ( HP ) , 0 , result , 0 , sizeof ( int ) ) ;
293251 Array . Copy ( BitConverter . GetBytes ( Veterancy ) , 0 , result , sizeof ( int ) , sizeof ( int ) ) ;
@@ -300,12 +258,12 @@ protected override byte[] GetCustomData()
300258
301259 protected override void ReadCustomData ( Stream stream )
302260 {
303- HP = ReadInt ( stream ) ;
304- Veterancy = ReadInt ( stream ) ;
261+ HP = StreamHelpers . ReadInt ( stream ) ;
262+ Veterancy = StreamHelpers . ReadInt ( stream ) ;
305263 Facing = ( byte ) stream . ReadByte ( ) ;
306- ObjectTypeName = ReadASCIIString ( stream ) ;
307- OwnerHouseName = ReadASCIIString ( stream ) ;
308- Mission = ReadASCIIString ( stream ) ;
264+ ObjectTypeName = StreamHelpers . ReadASCIIString ( stream ) ;
265+ OwnerHouseName = StreamHelpers . ReadASCIIString ( stream ) ;
266+ Mission = StreamHelpers . ReadASCIIString ( stream ) ;
309267 }
310268 }
311269
@@ -362,7 +320,7 @@ protected override byte[] GetCustomData()
362320 protected override void ReadCustomData ( Stream stream )
363321 {
364322 base . ReadCustomData ( stream ) ;
365- SubCell = ( SubCell ) ReadInt ( stream ) ;
323+ SubCell = ( SubCell ) StreamHelpers . ReadInt ( stream ) ;
366324 }
367325 }
368326
@@ -379,11 +337,11 @@ public byte[] Serialize()
379337
380338 using ( var memoryStream = new MemoryStream ( ) )
381339 {
382- memoryStream . Write ( BitConverter . GetBytes ( Width ) ) ;
383- memoryStream . Write ( BitConverter . GetBytes ( Height ) ) ;
340+ StreamHelpers . WriteUShort ( memoryStream , Width ) ;
341+ StreamHelpers . WriteUShort ( memoryStream , Height ) ;
384342
385343 // Write entry count
386- memoryStream . Write ( BitConverter . GetBytes ( CopiedMapEntries . Count ) ) ;
344+ StreamHelpers . WriteInt ( memoryStream , CopiedMapEntries . Count ) ;
387345
388346 // Write entries
389347 foreach ( var entry in CopiedMapEntries )
0 commit comments