blob: fc6f96f27235e2cf865228e18fddd88611b3e2bc (
plain)
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
|
package net.tylermurphy.Minecraft.Util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
import net.tylermurphy.Minecraft.Main;
public class ResourceManager {
static String saveName;
protected static void init(String saveName) {
ResourceManager.saveName = saveName;
}
private static byte[] compress(byte[] data){
ByteArrayOutputStream baos = null;
Deflater dfl = new Deflater();
dfl.setLevel(Deflater.BEST_COMPRESSION);
dfl.setInput(data);
dfl.finish();
baos = new ByteArrayOutputStream();
byte[] tmp = new byte[4*1024];
try {
while(!dfl.finished()) {
int size = dfl.deflate(tmp);
baos.write(tmp, 0, size);
}
} catch(Exception e) {
} finally {
try {
if(baos != null) baos.close();
} catch(Exception e) {}
}
return baos.toByteArray();
}
protected static byte[] decompress(byte[] data){
ByteArrayOutputStream baos = null;
Inflater ifl = new Inflater();
ifl.setInput(data);
baos = new ByteArrayOutputStream();
byte[] tmp = new byte[4*1024];
try {
while(!ifl.finished()) {
int size = ifl.inflate(tmp);
baos.write(tmp, 0, size);
}
} catch(Exception e) {
} finally {
try {
if(baos != null) baos.close();
} catch(Exception e) {}
}
return baos.toByteArray();
}
public static byte[][][] expandArray(byte[] data) {
byte[] result = decompress(data);
byte[][][] cubes = new byte[16][256][16];
for (int x = 0 ; x != 16 ; x++) {
for (int y = 0 ; y != 256 ; y++) {
for (int z = 0 ; z != 16 ; z++) {
cubes[x][y][z] = result[256 * 16 * x + 16 * y + z];
}
}
}
return cubes;
}
public static byte[] flattenArray(byte[][][] cubes) {
byte[] data = new byte[16*256*16];
for (int x = 0 ; x != 16 ; x++) {
for (int y = 0 ; y != 256 ; y++) {
for (int z = 0 ; z != 16 ; z++) {
data[256 * 16 * x + 16 * y + z] = cubes[x][y][z];
}
}
}
byte[] result = compress(data);
return result;
}
public static Object loadObject(String pass_path, String fileName) {
String path = Constants.SAVES_LOCATION+"/"+Main.currentWorld+"/"+pass_path+"/";
ObjectInputStream is = null;
try {
is = new ObjectInputStream(new FileInputStream(new File(path+fileName)));
Object object = is.readObject();
is.close();
return object;
} catch(Exception e) {
} finally {
try {
if(is!=null) is.close();
} catch(Exception e) {
}
}
return null;
}
public static void saveObject(String pass_path, String fileName, Object o) {
String path = Constants.SAVES_LOCATION+"/"+Main.currentWorld+"/"+pass_path+"/";
ObjectOutputStream os = null;
try {
File folder = new File(path);
folder.mkdirs();
File file = new File(path+fileName);
if(!file.exists()) file.createNewFile();
os = new ObjectOutputStream(new FileOutputStream(file));
os.writeObject(o);
os.close();
} catch(Exception e) {
} finally {
try {
if(os!=null) os.close();
} catch(Exception e) {}
}
}
}
|