-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMapsFile.cs
More file actions
148 lines (134 loc) · 4.94 KB
/
MapsFile.cs
File metadata and controls
148 lines (134 loc) · 4.94 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
148
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static MapsDatEditor.Structs;
namespace MapsDatEditor
{
public static class MapsFile
{
public static MapsData Load(string path)
{
using var reader = new BinaryReader(File.OpenRead(path));
var data = new MapsData { Header = reader.ReadInt32() };
short wmCount = reader.ReadInt16();
for (int i = 0; i < wmCount; i++)
{
var wm = new WorldMap { Name = reader.ReadString() };
byte nodeCount = reader.ReadByte();
for (int j = 0; j < nodeCount; j++)
{
wm.Nodes.Add(new WorldMapNode
{
SourceX = reader.ReadInt16(),
SourceY = reader.ReadInt16(),
Name = reader.ReadString(),
MapId = reader.ReadInt16(),
TargetX = reader.ReadByte(),
TargetY = reader.ReadByte()
});
}
data.WorldMaps.Add(wm);
}
short mapCount = reader.ReadInt16();
for (int i = 0; i < mapCount; i++)
{
var map = new MapEntry
{
Id = reader.ReadInt16(),
SizeX = reader.ReadByte(),
SizeY = reader.ReadByte(),
Name = reader.ReadString(),
Flags = reader.ReadByte(),
Music = reader.ReadSByte()
};
short warpCount = reader.ReadInt16();
for (int j = 0; j < warpCount; j++)
{
map.Warps.Add(new Warp
{
SourceX = reader.ReadByte(),
SourceY = reader.ReadByte(),
TargetMapId = reader.ReadInt16(),
TargetX = reader.ReadByte(),
TargetY = reader.ReadByte()
});
}
byte numWM = reader.ReadByte();
for (int j = 0; j < numWM; j++)
{
map.WorldMapLinks.Add(new WorldMapLink
{
X = reader.ReadByte(),
Y = reader.ReadByte(),
Key = reader.ReadUInt32()
});
}
data.Maps.Add(map);
}
return data;
}
public static byte[] Serialize(MapsData data)
{
using var ms = new MemoryStream();
using var writer = new BinaryWriter(ms, Encoding.UTF8, leaveOpen: true);
writer.Write(data.Header);
writer.Write((short)data.WorldMaps.Count);
foreach (var wm in data.WorldMaps)
{
writer.Write(wm.Name);
writer.Write((byte)wm.Nodes.Count);
foreach (var node in wm.Nodes)
{
writer.Write(node.SourceX);
writer.Write(node.SourceY);
writer.Write(node.Name);
writer.Write(node.MapId);
writer.Write(node.TargetX);
writer.Write(node.TargetY);
}
}
writer.Write((short)data.Maps.Count);
foreach (var map in data.Maps)
{
writer.Write(map.Id);
writer.Write(map.SizeX);
writer.Write(map.SizeY);
writer.Write(map.Name);
writer.Write(map.Flags);
writer.Write(map.Music);
writer.Write((short)map.Warps.Count);
foreach (var warp in map.Warps)
{
writer.Write(warp.SourceX);
writer.Write(warp.SourceY);
writer.Write(warp.TargetMapId);
writer.Write(warp.TargetX);
writer.Write(warp.TargetY);
}
writer.Write((byte)map.WorldMapLinks.Count);
foreach (var wml in map.WorldMapLinks)
{
writer.Write(wml.X);
writer.Write(wml.Y);
writer.Write(wml.Key);
}
}
writer.Flush();
return ms.ToArray();
}
public static void Save(string path, MapsData data)
{
File.WriteAllBytes(path, Serialize(data));
}
public static bool Verify(byte[] original, MapsData data)
{
byte[] reserialized = Serialize(data);
if (original.Length != reserialized.Length) return false;
for (int i = 0; i < original.Length; i++)
if (original[i] != reserialized[i]) return false;
return true;
}
}
}