1
|
|
|
/* |
|
|
|
|
2
|
|
|
* This file is part of Swf Map Loader. |
3
|
|
|
* |
4
|
|
|
* Swf Map Loader is free software: you can redistribute it and/or modify |
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
* (at your option) any later version. |
8
|
|
|
* |
9
|
|
|
* Swf Map Loader is distributed in the hope that it will be useful, |
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
* GNU Lesser General Public License for more details. |
13
|
|
|
* |
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
15
|
|
|
* along with Swf Map Loader. If not, see <https://www.gnu.org/licenses/>. |
16
|
|
|
* |
17
|
|
|
* Copyright (c) 2020-2020 Vincent Quatrevieux |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
package fr.arakne.swfmaploader; |
21
|
|
|
|
22
|
|
|
import fr.arakne.swfmaploader.cache.MapStructureCache; |
23
|
|
|
import fr.arakne.swfmaploader.map.MapFactory; |
24
|
|
|
import fr.arakne.swfmaploader.swf.SwfFileLoader; |
25
|
|
|
import fr.arakne.swfmaploader.swf.SwfMapStructure; |
26
|
|
|
import fr.arakne.utils.maps.DofusMap; |
27
|
|
|
import fr.arakne.utils.maps.MapCell; |
28
|
|
|
import fr.arakne.utils.maps.serializer.DefaultMapDataSerializer; |
29
|
|
|
import fr.arakne.utils.maps.serializer.MapDataSerializer; |
30
|
|
|
|
31
|
|
|
import java.io.IOException; |
32
|
|
|
import java.net.URL; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Loader for SWF Dofus maps |
36
|
|
|
* |
37
|
|
|
* @param <C> The cell type |
38
|
|
|
* @param <M> The map type |
39
|
|
|
*/ |
40
|
|
|
final public class MapLoader<C extends MapCell, M extends DofusMap<C>> { |
41
|
|
|
final private SwfFileLoader loader; |
42
|
|
|
final private DefaultMapDataSerializer serializer; |
43
|
|
|
final private MapStructureCache cache; |
44
|
|
|
final private MapFactory<C, M> factory; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param loader The swf file loader |
48
|
|
|
* @param serializer Map date serializer |
49
|
|
|
* @param cache Cache system |
50
|
|
|
* @param factory The map factory |
51
|
|
|
*/ |
52
|
1 |
|
public MapLoader(SwfFileLoader loader, DefaultMapDataSerializer serializer, MapStructureCache cache, MapFactory<C, M> factory) { |
53
|
1 |
|
this.loader = loader; |
54
|
1 |
|
this.serializer = serializer; |
55
|
1 |
|
this.cache = cache; |
56
|
1 |
|
this.factory = factory; |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Load a map from CDN |
61
|
|
|
* |
62
|
|
|
* @param id The map id |
63
|
|
|
* @param version The map version string |
64
|
|
|
* @param key The encryption key. null is the map is not encrypted |
65
|
|
|
* |
66
|
|
|
* @return The map instance |
67
|
|
|
* |
68
|
|
|
* @throws RuntimeException If the map cannot be loaded or is not found |
69
|
|
|
*/ |
70
|
|
|
public M load(int id, String version, String key) { |
71
|
1 |
|
final SwfMapStructure structure = cache.retrieve(id) |
72
|
1 |
|
.filter(s -> s.version().equals(version)) |
|
|
|
|
73
|
1 |
|
.orElseGet(() -> { |
74
|
|
|
try { |
75
|
1 |
|
SwfMapStructure loaded = loader.load(id, version, key); |
76
|
1 |
|
cache.store(loaded); |
77
|
1 |
|
return loaded; |
78
|
|
|
} catch (InterruptedException e) { |
79
|
|
|
Thread.currentThread().interrupt(); |
80
|
|
|
|
81
|
|
|
throw new RuntimeException(e); |
|
|
|
|
82
|
1 |
|
} catch (IOException e) { |
83
|
1 |
|
throw new RuntimeException("Cannot load map " + id, e); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
}) |
86
|
|
|
; |
87
|
|
|
|
88
|
1 |
|
return createMap(structure, key); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Load a map from SWF file, by URL |
93
|
|
|
* The cache is not used by the method : the file is always downloaded (is needed) and parsed |
94
|
|
|
* |
95
|
|
|
* @param url The file URL |
96
|
|
|
* @param key The encryption key. null is the map is not encrypted |
97
|
|
|
* |
98
|
|
|
* @return The map instance |
99
|
|
|
* |
100
|
|
|
* @throws IOException When cannot load the map from the URL |
101
|
|
|
* @throws InterruptedException When an interruption occurs during loading the SWF file |
102
|
|
|
* @throws IllegalArgumentException When an invalid map file is loaded |
103
|
|
|
*/ |
104
|
|
|
public M loadFromUrl(URL url, String key) throws IOException, InterruptedException { |
|
|
|
|
105
|
1 |
|
final SwfMapStructure structure = loader.load(url); |
106
|
|
|
|
107
|
1 |
|
return createMap(structure, key); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the serializer for the given key |
112
|
|
|
* |
113
|
|
|
* @param key Key to use |
114
|
|
|
* |
115
|
|
|
* @return The serializer |
116
|
|
|
*/ |
117
|
|
|
private MapDataSerializer serializer(String key) { |
118
|
1 |
|
return key == null || key.isEmpty() ? serializer : serializer.withKey(key); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Create the map instance |
123
|
|
|
* |
124
|
|
|
* @param structure The structure |
125
|
|
|
* @param key The encryption key |
126
|
|
|
* |
127
|
|
|
* @return The map instance |
128
|
|
|
*/ |
129
|
|
|
private M createMap(SwfMapStructure structure, String key) { |
130
|
1 |
|
return factory.createMap(structure, serializer(key).deserialize(structure.mapData())); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|