forked from ServUO/ServUO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileMatrixPatch.cs
More file actions
188 lines (147 loc) · 4.82 KB
/
TileMatrixPatch.cs
File metadata and controls
188 lines (147 loc) · 4.82 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#region References
using System.IO;
using System.Runtime.CompilerServices;
#endregion
namespace Server
{
public class TileMatrixPatch
{
private readonly int m_LandBlocks;
private readonly int m_StaticBlocks;
private static bool m_Enabled = true;
public static bool Enabled { get { return m_Enabled; } set { m_Enabled = value; } }
public int LandBlocks
{
get
{
lock (this)
return m_LandBlocks;
}
}
public int StaticBlocks
{
get
{
lock (this)
return m_StaticBlocks;
}
}
public TileMatrixPatch(TileMatrix matrix, int index)
{
if (!m_Enabled)
{
return;
}
string mapDataPath = Core.FindDataFile("mapdif{0}.mul", index);
string mapIndexPath = Core.FindDataFile("mapdifl{0}.mul", index);
if (File.Exists(mapDataPath) && File.Exists(mapIndexPath))
{
m_LandBlocks = PatchLand(matrix, mapDataPath, mapIndexPath);
}
string staDataPath = Core.FindDataFile("stadif{0}.mul", index);
string staIndexPath = Core.FindDataFile("stadifl{0}.mul", index);
string staLookupPath = Core.FindDataFile("stadifi{0}.mul", index);
if (File.Exists(staDataPath) && File.Exists(staIndexPath) && File.Exists(staLookupPath))
{
m_StaticBlocks = PatchStatics(matrix, staDataPath, staIndexPath, staLookupPath);
}
}
[MethodImpl(MethodImplOptions.Synchronized)]
private unsafe int PatchLand(TileMatrix matrix, string dataPath, string indexPath)
{
using (FileStream fsData = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (FileStream fsIndex = new FileStream(indexPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
BinaryReader indexReader = new BinaryReader(fsIndex);
int count = (int)(indexReader.BaseStream.Length / 4);
for (int i = 0; i < count; ++i)
{
int blockID = indexReader.ReadInt32();
int x = blockID / matrix.BlockHeight;
int y = blockID % matrix.BlockHeight;
fsData.Seek(4, SeekOrigin.Current);
var tiles = new LandTile[64];
fixed (LandTile* pTiles = tiles)
{
NativeReader.Read(fsData.SafeFileHandle.DangerousGetHandle(), pTiles, 192);
}
matrix.SetLandBlock(x, y, tiles);
}
indexReader.Close();
return count;
}
}
}
private StaticTile[] m_TileBuffer = new StaticTile[128];
[MethodImpl(MethodImplOptions.Synchronized)]
private unsafe int PatchStatics(TileMatrix matrix, string dataPath, string indexPath, string lookupPath)
{
using (FileStream fsData = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (FileStream fsIndex = new FileStream(indexPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (FileStream fsLookup = new FileStream(lookupPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
BinaryReader indexReader = new BinaryReader(fsIndex);
BinaryReader lookupReader = new BinaryReader(fsLookup);
int count = (int)(indexReader.BaseStream.Length / 4);
var lists = new TileList[8][];
for (int x = 0; x < 8; ++x)
{
lists[x] = new TileList[8];
for (int y = 0; y < 8; ++y)
{
lists[x][y] = new TileList();
}
}
for (int i = 0; i < count; ++i)
{
int blockID = indexReader.ReadInt32();
int blockX = blockID / matrix.BlockHeight;
int blockY = blockID % matrix.BlockHeight;
int offset = lookupReader.ReadInt32();
int length = lookupReader.ReadInt32();
lookupReader.ReadInt32(); // Extra
if (offset < 0 || length <= 0)
{
matrix.SetStaticBlock(blockX, blockY, matrix.EmptyStaticBlock);
continue;
}
fsData.Seek(offset, SeekOrigin.Begin);
int tileCount = length / 7;
if (m_TileBuffer.Length < tileCount)
{
m_TileBuffer = new StaticTile[tileCount];
}
var staTiles = m_TileBuffer;
fixed (StaticTile* pTiles = staTiles)
{
NativeReader.Read(fsData.SafeFileHandle.DangerousGetHandle(), pTiles, length);
StaticTile* pCur = pTiles, pEnd = pTiles + tileCount;
while (pCur < pEnd)
{
lists[pCur->m_X & 0x7][pCur->m_Y & 0x7].Add(pCur->m_ID, pCur->m_Z);
pCur = pCur + 1;
}
var tiles = new StaticTile[8][][];
for (int x = 0; x < 8; ++x)
{
tiles[x] = new StaticTile[8][];
for (int y = 0; y < 8; ++y)
{
tiles[x][y] = lists[x][y].ToArray();
}
}
matrix.SetStaticBlock(blockX, blockY, tiles);
}
}
indexReader.Close();
lookupReader.Close();
return count;
}
}
}
}
}
}