fr.arakne.swflangloader.LangLoader   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 62
c 1
b 0
f 0
dl 0
loc 142
ccs 50
cts 50
cp 1
rs 10
wmc 17

10 Methods

Rating   Name   Duplication   Size   Complexity  
A clear() 0 8 1
A classes() 0 6 2
A version(String) 0 12 3
A lang() 0 6 2
A maps() 0 6 2
A hints() 0 6 2
A LangLoader(URL,String) 0 2 1
A url(String) 0 6 1
A LangLoader(URL,String,VersionsLoader,SwfFileLoader) 0 5 1
A load(String) 0 11 2
1
/*
2
 * This file is part of ArakneLangLoader.
3
 *
4
 * ArakneLangLoader 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
 * ArakneLangLoader 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 ArakneLangLoader.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2020 Vincent Quatrevieux
18
 */
19
20
package fr.arakne.swflangloader;
21
22
import fr.arakne.swflangloader.lang.BaseLangFile;
23
import fr.arakne.swflangloader.lang.classes.ClassesFile;
24
import fr.arakne.swflangloader.lang.hints.HintsFile;
25
import fr.arakne.swflangloader.lang.lang.LangFile;
26
import fr.arakne.swflangloader.lang.maps.MapsFile;
27
import fr.arakne.swflangloader.loader.SwfFileLoader;
28
import fr.arakne.swflangloader.loader.TxtVersionsLoader;
29
import fr.arakne.swflangloader.loader.VersionsLoader;
30
31
import java.io.IOException;
32
import java.net.URL;
33
import java.util.HashMap;
34
import java.util.Map;
35
import java.util.NoSuchElementException;
36
37
/**
38
 * Loader for Dofus swf lang files
39
 */
40
final public class LangLoader {
41
    final private URL baseUrl;
42
    final private String language;
43
    final private VersionsLoader versionsLoader;
44
    final private SwfFileLoader swfFileLoader;
45
46 1
    final private Map<String, BaseLangFile> langs = new HashMap<>();
47 1
    private MapsFile maps = null;
48 1
    private ClassesFile classes = null;
49 1
    private HintsFile hints = null;
50 1
    private LangFile lang = null;
51 1
    private Map<String, Integer> versions = null;
52
53
    public LangLoader(URL baseUrl, String language) {
54 1
        this(baseUrl, language, new TxtVersionsLoader(), new SwfFileLoader());
55 1
    }
56
57 1
    public LangLoader(URL baseUrl, String language, VersionsLoader versionsLoader, SwfFileLoader swfFileLoader) {
58 1
        this.baseUrl = baseUrl;
59 1
        this.language = language;
60 1
        this.versionsLoader = versionsLoader;
61 1
        this.swfFileLoader = swfFileLoader;
62 1
    }
63
64
    /**
65
     * Load an SWF lang File
66
     *
67
     * @param name The file name
68
     *
69
     * @return The loaded file
70
     *
71
     * @throws IOException When cannot load the file
72
     */
73
    public BaseLangFile load(String name) throws IOException, InterruptedException {
74
        BaseLangFile file;
75
76 1
        if ((file = langs.get(name)) != null) {
77 1
            return file;
78
        }
79
80 1
        file = BaseLangFile.load(url(name), swfFileLoader);
81 1
        langs.put(name, file);
82
83 1
        return file;
84
    }
85
86
    /**
87
     * Load maps file
88
     * Note: Keep the loaded instance in memory
89
     *
90
     * @return MapsFile instance
91
     * @throws IOException When cannot load the file
92
     */
93
    public MapsFile maps() throws IOException, InterruptedException {
94 1
        if (maps != null) {
95 1
            return maps;
96
        }
97
98 1
        return maps = new MapsFile(url("maps"), swfFileLoader);
99
    }
100
101
    /**
102
     * Load classes file
103
     * Note: Keep the loaded instance in memory
104
     *
105
     * @return ClassesFile instance
106
     * @throws IOException When cannot load the file
107
     */
108
    public ClassesFile classes() throws IOException, InterruptedException {
109 1
        if (classes != null) {
110 1
            return classes;
111
        }
112
113 1
        return classes = new ClassesFile(url("classes"), swfFileLoader);
114
    }
115
116
    /**
117
     * Load hints file
118
     * Note: Keep the loaded instance in memory
119
     *
120
     * @return HintsFile instance
121
     * @throws IOException When cannot load the file
122
     */
123
    public HintsFile hints() throws IOException, InterruptedException {
124 1
        if (hints != null) {
125 1
            return hints;
126
        }
127
128 1
        return hints = new HintsFile(url("hints"), swfFileLoader);
129
    }
130
131
    /**
132
     * Load lang file
133
     * Note: Keep the loaded instance in memory
134
     *
135
     * @return LangFile instance
136
     * @throws IOException When cannot load the file
137
     */
138
    public LangFile lang() throws IOException, InterruptedException {
139 1
        if (lang != null) {
140 1
            return lang;
141
        }
142
143 1
        return lang = new LangFile(url("lang"), swfFileLoader);
144
    }
145
146
    /**
147
     * Clear loaded files and cache
148
     * Permit to refresh langs versions
149
     *
150
     * @throws IOException When cannot clear cache
151
     */
152
    public void clear() throws IOException {
153 1
        versions = null;
154 1
        langs.clear();
155 1
        maps = null;
156 1
        classes = null;
157 1
        hints = null;
158 1
        lang = null;
159 1
        swfFileLoader.clear();
160 1
    }
161
162
    private int version(String file) throws IOException {
163 1
        if (versions == null) {
164 1
            versions = versionsLoader.forLanguage(baseUrl, language);
165
        }
166
167 1
        Integer version = versions.get(file);
168
169 1
        if (version != null) {
170 1
            return version;
171
        }
172
173 1
        throw new NoSuchElementException("The swf file " + file + " is not found");
174
    }
175
176
    private URL url(String file) throws IOException {
177 1
        return new URL(
178 1
            baseUrl.getProtocol(),
179 1
            baseUrl.getHost(),
180 1
            baseUrl.getPort(),
181 1
            baseUrl.getFile() + "/swf/" + file + "_" + language + "_" + version(file) + ".swf"
182
        );
183
    }
184
}
185