-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMapLoaderConfigurator.java
143 lines (128 loc) · 4.44 KB
/
MapLoaderConfigurator.java
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
/*
* This file is part of Swf Map Loader.
*
* Swf Map Loader is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Swf Map Loader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Swf Map Loader. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (c) 2020-2020 Vincent Quatrevieux
*/
package fr.arakne.swfmaploader;
import fr.arakne.swfmaploader.cache.MapStructureCache;
import fr.arakne.swfmaploader.cache.NullMapStructureCache;
import fr.arakne.swfmaploader.cache.SqlMapStructureCache;
import fr.arakne.swfmaploader.map.MapFactory;
import fr.arakne.swfmaploader.map.SimpleMapFactory;
import fr.arakne.swfmaploader.swf.SwfFileLoader;
import fr.arakne.utils.maps.DofusMap;
import fr.arakne.utils.maps.MapCell;
import fr.arakne.utils.maps.serializer.DefaultMapDataSerializer;
import java.sql.SQLException;
/**
* Utility class for configure and create a {@link MapLoader}
*
* <code>
* MapLoaderConfigurator configurator = new MapLoaderConfigurator();
*
* MapLoader loader = configurator
* .baseUrl("http://my-server.com/dofus/swf/maps")
* .tempDir("/tmp/my-client")
* .cacheFile("map-cache.sqlite")
* .factory(new MyCustomMapFactory())
* .create()
* ;
*
* MyMap map = loader.load(gmd.id(), gmd.version(), gmd.key());
* </code>
*
* @param <C> The cell type
* @param <M> The map type
*/
final public class MapLoaderConfigurator<C extends MapCell, M extends DofusMap<C>> {
private MapStructureCache cache;
private DefaultMapDataSerializer serializer;
private MapFactory<C, M> factory;
private SwfFileLoader loader;
private String tempDir = "./tmp";
private String baseUrl = "file:./data/maps";
/**
* Define the cache system to use
*/
public MapLoaderConfigurator<C, M> cache(MapStructureCache cache) {
this.cache = cache;
return this;
}
/**
* Define the serializer to use
*/
public MapLoaderConfigurator<C, M> serializer(DefaultMapDataSerializer serializer) {
this.serializer = serializer;
return this;
}
/**
* Enable the cell cache for the inner map data serializer
*/
public MapLoaderConfigurator<C, M> enableCellCache() {
DefaultMapDataSerializer serializer = new DefaultMapDataSerializer();
serializer.enableCache();
return serializer(serializer);
}
/**
* Define the factory to use
*/
public MapLoaderConfigurator<C, M> factory(MapFactory<C, M> factory) {
this.factory = factory;
return this;
}
/**
* Define the SWF loader to use
*
* @see MapLoaderConfigurator#baseUrl(String) To configure loader with the given base url
* @see MapLoaderConfigurator#tempDir(String) To configure loader with the given temporary directory
*/
public MapLoaderConfigurator<C, M> loader(SwfFileLoader loader) {
this.loader = loader;
return this;
}
/**
* Define the used temporary directory for load SWF files
*/
public MapLoaderConfigurator<C, M> tempDir(String tempDir) {
this.tempDir = tempDir;
return this;
}
/**
* Define the base URL of the maps CDN
*/
public MapLoaderConfigurator<C, M> baseUrl(String baseUrl) {
this.baseUrl = baseUrl;
return this;
}
/**
* Define the SQLite database file for the map cache
*/
public MapLoaderConfigurator<C, M> cacheFile(String filename) throws SQLException {
return cache(SqlMapStructureCache.createBySqliteFile(filename));
}
/**
* Create the {@link MapLoader} instance
*/
@SuppressWarnings("unchecked")
public MapLoader<C, M> create() {
return new MapLoader<>(
loader == null ? new SwfFileLoader(baseUrl, tempDir) : loader,
serializer == null ? new DefaultMapDataSerializer() : serializer,
cache == null ? new NullMapStructureCache() : cache,
factory == null ? (MapFactory<C, M>) new SimpleMapFactory() : factory
);
}
}