forked from mcedit/pymclevel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
java.py
190 lines (155 loc) · 5.84 KB
/
java.py
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
189
190
'''
Created on Jul 22, 2011
@author: Rio
'''
__all__ = ["MCJavaLevel"]
from cStringIO import StringIO
import gzip
from level import MCLevel
from logging import getLogger
from numpy import fromstring
import os
import re
log = getLogger(__name__)
warn, error, info, debug = log.warn, log.error, log.info, log.debug
class MCJavaLevel(MCLevel):
def setBlockDataAt(self, *args):
pass
def blockDataAt(self, *args):
return 0
@property
def Height(self):
return self.Blocks.shape[2]
@property
def Length(self):
return self.Blocks.shape[1]
@property
def Width(self):
return self.Blocks.shape[0]
def guessSize(self, data):
if data.shape[0] <= (32 * 32 * 64) * 2:
warn(u"Can't guess the size of a {0} byte level".format(data.shape[0]))
raise IOError("MCJavaLevel attempted for smaller than 64 blocks cubed")
if data.shape[0] > (32 * 32 * 64) * 2:
Width = 64
Length = 64
Height = 64
if data.shape[0] > (64 * 64 * 64) * 2:
Width = 128
Length = 128
Height = 64
if data.shape[0] > (128 * 128 * 64) * 2:
Width = 256
Length = 256
Height = 64
if data.shape[0] > (256 * 256 * 64) * 2: # could also be 256*256*256
Width = 512
Length = 512
Height = 64
if data.shape[0] > 512 * 512 * 64 * 2: # just to load shadowmarch castle
Width = 512
Length = 512
Height = 256
return Width, Length, Height
@classmethod
def _isDataLevel(cls, data):
return (data[0] == 0x27 and
data[1] == 0x1B and
data[2] == 0xb7 and
data[3] == 0x88)
def __init__(self, filename, data):
self.filename = filename
if isinstance(data, basestring):
data = fromstring(data, dtype='uint8')
self.filedata = data
# try to take x,z,y from the filename
r = re.findall("\d+", os.path.basename(filename))
if r and len(r) >= 3:
(w, l, h) = map(int, r[-3:])
if w * l * h > data.shape[0]:
info("Not enough blocks for size " + str((w, l, h)))
w, l, h = self.guessSize(data)
else:
w, l, h = self.guessSize(data)
info(u"MCJavaLevel created for potential level of size " + str((w, l, h)))
blockCount = h * l * w
if blockCount > data.shape[0]:
raise ValueError("Level file does not contain enough blocks! (size {s}) Try putting the size into the filename, e.g. server_level_{w}_{l}_{h}.dat".format(w=w, l=l, h=h, s=data.shape))
blockOffset = data.shape[0] - blockCount
blocks = data[blockOffset:blockOffset + blockCount]
maxBlockType = 64 # maximum allowed in classic
while max(blocks[-4096:]) > maxBlockType:
# guess the block array by starting at the end of the file
# and sliding the blockCount-sized window back until it
# looks like every block has a valid blockNumber
blockOffset -= 1
blocks = data[blockOffset:blockOffset + blockCount]
if blockOffset <= -data.shape[0]:
raise IOError("Can't find a valid array of blocks <= #%d" % maxBlockType)
self.Blocks = blocks
self.blockOffset = blockOffset
blocks.shape = (w, l, h)
blocks.strides = (1, w, w * l)
def saveInPlace(self):
s = StringIO()
if self.compressed:
g = gzip.GzipFile(fileobj=s, mode='wb')
else:
g = s
g.write(self.filedata.tostring())
g.flush()
g.close()
try:
os.rename(self.filename, self.filename + ".old")
except Exception, e:
pass
try:
with open(self.filename, 'wb') as f:
f.write(s.getvalue())
except Exception, e:
info(u"Error while saving java level in place: {0}".format(e))
try:
os.remove(self.filename)
except:
pass
os.rename(self.filename + ".old", self.filename)
try:
os.remove(self.filename + ".old")
except Exception, e:
pass
class MCSharpLevel(MCLevel):
""" int magic = convert(data.readShort())
logger.trace("Magic number: {}", magic)
if (magic != 1874)
throw new IOException("Only version 1 MCSharp levels supported (magic number was "+magic+")")
int width = convert(data.readShort())
int height = convert(data.readShort())
int depth = convert(data.readShort())
logger.trace("Width: {}", width)
logger.trace("Depth: {}", depth)
logger.trace("Height: {}", height)
int spawnX = convert(data.readShort())
int spawnY = convert(data.readShort())
int spawnZ = convert(data.readShort())
int spawnRotation = data.readUnsignedByte()
int spawnPitch = data.readUnsignedByte()
int visitRanks = data.readUnsignedByte()
int buildRanks = data.readUnsignedByte()
byte[][][] blocks = new byte[width][height][depth]
int i = 0
BlockManager manager = BlockManager.getBlockManager()
for(int z = 0;z<depth;z++) {
for(int y = 0;y<height;y++) {
byte[] row = new byte[height]
data.readFully(row)
for(int x = 0;x<width;x++) {
blocks[x][y][z] = translateBlock(row[x])
}
}
}
lvl.setBlocks(blocks, new byte[width][height][depth], width, height, depth)
lvl.setSpawnPosition(new Position(spawnX, spawnY, spawnZ))
lvl.setSpawnRotation(new Rotation(spawnRotation, spawnPitch))
lvl.setEnvironment(new Environment())
return lvl
}"""