• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_RESTOOL_RESOURCE_DATA_H
17 #define OHOS_RESTOOL_RESOURCE_DATA_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <set>
22 #include <stdint.h>
23 #include <string>
24 #include <vector>
25 
26 namespace OHOS {
27 namespace Global {
28 namespace Restool {
29 const static std::string TOOL_NAME = "restool";
30 const static std::string RESOURCES_DIR = "resources";
31 const static std::string CACHES_DIR = ".caches";
32 const static std::string CONFIG_JSON = "config.json";
33 const static std::string MODULE_JSON = "module.json";
34 const static std::string RAW_FILE_DIR = "rawfile";
35 const static std::string RES_FILE_DIR = "resfile";
36 const static std::string ID_DEFINED_FILE = "id_defined.json";
37 const static std::string RESOURCE_INDEX_FILE = "resources.index";
38 const static std::string JSON_EXTENSION = ".json";
39 #ifdef __WIN32
40 const static std::string SEPARATOR_FILE = "\\";
41 #else
42 const static std::string SEPARATOR_FILE = "/";
43 #endif
44 const static std::string SEPARATOR = "/";
45 const static std::string WIN_SEPARATOR = "\\";
46 const static std::string NEW_LINE_PATH = "\nat ";
47 const static std::string SOLUTIONS = "Solutions:";
48 const static std::string SOLUTIONS_ARROW = "> ";
49 const static std::string LONG_PATH_HEAD = "\\\\?\\";
50 const static int32_t VERSION_MAX_LEN = 128;
51 const static int32_t INT_TO_BYTES = sizeof(uint32_t);
52 static const int8_t RESTOOL_VERSION[VERSION_MAX_LEN] = { "Restool 5.1.0.007" };
53 const static int32_t TAG_LEN = 4;
54 constexpr static int DEFAULT_POOL_SIZE = 8;
55 static std::set<std::string> g_resourceSet;
56 static std::set<std::string> g_hapResourceSet;
57 const static int8_t INVALID_ID = -1;
58 
59 enum class KeyType {
60     LANGUAGE = 0,
61     REGION = 1,
62     RESOLUTION = 2,
63     ORIENTATION = 3,
64     DEVICETYPE = 4,
65     SCRIPT = 5,
66     NIGHTMODE = 6,
67     MCC = 7,
68     MNC = 8,
69     // RESERVER 9
70     INPUTDEVICE = 10,
71     KEY_TYPE_MAX,
72     OTHER,
73 };
74 
75 enum class ResType {
76     ELEMENT = 0,
77     RAW = 6,
78     INTEGER = 8,
79     STRING = 9,
80     STRARRAY = 10,
81     INTARRAY = 11,
82     BOOLEAN = 12,
83     COLOR = 14,
84     ID = 15,
85     THEME = 16,
86     PLURAL = 17,
87     FLOAT = 18,
88     MEDIA = 19,
89     PROF = 20,
90     PATTERN = 22,
91     SYMBOL = 23,
92     RES = 24,
93     INVALID_RES_TYPE = -1,
94 };
95 
96 enum class PackType {
97     NORMAL = 1,
98     OVERLAP
99 };
100 
101 enum class OrientationType {
102     VERTICAL = 0,
103     HORIZONTAL = 1,
104 };
105 
106 enum class DeviceType {
107     PHONE = 0,
108     TABLET = 1,
109     CAR = 2,
110     // RESERVER 3
111     TV = 4,
112     WEARABLE = 6,
113     TWOINONE = 7,
114 };
115 
116 enum class ResolutionType {
117     SDPI = 120,
118     MDPI = 160,
119     LDPI = 240,
120     XLDPI = 320,
121     XXLDPI = 480,
122     XXXLDPI = 640,
123 };
124 
125 enum class NightMode {
126     DARK = 0,
127     LIGHT = 1,
128 };
129 
130 enum class InputDevice {
131     INPUTDEVICE_NOT_SET = -1,
132     INPUTDEVICE_POINTINGDEVICE = 0,
133 };
134 
135 enum class ResourceIdCluster {
136     RES_ID_APP = 0,
137     RES_ID_SYS,
138     RES_ID_TYPE_MAX,
139 };
140 
141 enum Option {
142     END = -1,
143     IDS = 1,
144     DEFINED_IDS = 2,
145     DEPENDENTRY = 3,
146     ICON_CHECK = 4,
147     TARGET_CONFIG = 5,
148     DEFINED_SYSIDS = 6,
149     COMPRESSED_CONFIG = 7,
150     THREAD = 8,
151     STARTID = 'e',
152     FORCEWRITE = 'f',
153     HELP = 'h',
154     INPUTPATH = 'i',
155     JSON = 'j',
156     FILELIST = 'l',
157     MODULES = 'm',
158     OUTPUTPATH = 'o',
159     PACKAGENAME = 'p',
160     RESHEADER = 'r',
161     VERSION = 'v',
162     APPEND = 'x',
163     COMBINE = 'z',
164     UNKNOWN = '?',
165     NO_ARGUMENT = ':',
166 };
167 
168 const std::map<std::string, OrientationType> g_orientaionMap = {
169     { "vertical", OrientationType::VERTICAL },
170     { "horizontal", OrientationType::HORIZONTAL },
171 };
172 
173 const std::map<std::string, DeviceType> g_deviceMap = {
174     { "phone", DeviceType::PHONE },
175     { "tablet", DeviceType::TABLET },
176     { "car", DeviceType::CAR },
177     { "tv", DeviceType::TV },
178     { "wearable", DeviceType::WEARABLE },
179     { "2in1", DeviceType::TWOINONE },
180 };
181 
182 const std::map<std::string, ResolutionType> g_resolutionMap = {
183     { "sdpi", ResolutionType::SDPI },
184     { "mdpi",  ResolutionType::MDPI },
185     { "ldpi",  ResolutionType::LDPI },
186     { "xldpi", ResolutionType::XLDPI },
187     { "xxldpi", ResolutionType::XXLDPI },
188     { "xxxldpi", ResolutionType::XXXLDPI },
189 };
190 
191 const std::map<std::string, NightMode> g_nightModeMap = {
192     { "dark", NightMode::DARK },
193     { "light", NightMode::LIGHT },
194 };
195 
196 const std::map<std::string, InputDevice> g_inputDeviceMap = {
197     { "pointingdevice", InputDevice::INPUTDEVICE_POINTINGDEVICE },
198 };
199 
200 struct KeyParam {
201     KeyType keyType;
202     uint32_t value;
203     bool operator == (const KeyParam &other)
204     {
205         return keyType == other.keyType && value == other.value;
206     }
207 };
208 
209 struct IdData {
210     uint32_t id;
211     uint32_t dataOffset;
212 };
213 
214 struct ResourceId {
215     int64_t id;
216     int64_t seq;
217     std::string type;
218     std::string name;
219 };
220 
221 struct CompressFilter {
222     std::vector<std::string> path;
223     std::vector<std::string> excludePath;
224     std::string rules;
225     std::string excludeRules;
226     std::string method;
227 };
228 
229 const std::map<std::string, ResType> g_copyFileMap = {
230     { RAW_FILE_DIR, ResType::RAW },
231     { RES_FILE_DIR, ResType::RES },
232 };
233 
234 const std::map<std::string, ResType> g_fileClusterMap = {
235     { "element", ResType::ELEMENT },
236     { "media", ResType::MEDIA },
237     { "profile", ResType::PROF },
238 };
239 
240 const std::map<std::string, ResType> g_contentClusterMap = {
241     { "id", ResType::ID },
242     { "integer", ResType::INTEGER },
243     { "string", ResType::STRING },
244     { "strarray", ResType::STRARRAY },
245     { "intarray", ResType::INTARRAY },
246     { "color", ResType::COLOR },
247     { "plural", ResType::PLURAL },
248     { "boolean", ResType::BOOLEAN },
249     { "pattern", ResType::PATTERN },
250     { "theme", ResType::THEME },
251     { "float", ResType::FLOAT },
252     { "symbol", ResType::SYMBOL }
253 };
254 
255 const std::map<KeyType, std::string> g_keyTypeToStrMap = {
256     {KeyType::MCC, "mcc"},
257     {KeyType::MNC, "mnc"},
258     {KeyType::LANGUAGE, "language"},
259     {KeyType::SCRIPT, "script"},
260     {KeyType::REGION, "region"},
261     {KeyType::ORIENTATION, "orientation"},
262     {KeyType::RESOLUTION, "density"},
263     {KeyType::DEVICETYPE, "device"},
264     {KeyType::NIGHTMODE, "colorMode"},
265     {KeyType::INPUTDEVICE, "inputDevice"},
266 };
267 
268 const std::map<int32_t, ResType> g_resTypeMap = {
269     { static_cast<int32_t>(ResType::ELEMENT), ResType::ELEMENT},
270     { static_cast<int32_t>(ResType::RAW), ResType::RAW},
271     { static_cast<int32_t>(ResType::INTEGER), ResType::INTEGER},
272     { static_cast<int32_t>(ResType::STRING), ResType::STRING},
273     { static_cast<int32_t>(ResType::STRARRAY), ResType::STRARRAY},
274     { static_cast<int32_t>(ResType::INTARRAY), ResType::INTARRAY},
275     { static_cast<int32_t>(ResType::BOOLEAN), ResType::BOOLEAN},
276     { static_cast<int32_t>(ResType::COLOR), ResType::COLOR},
277     { static_cast<int32_t>(ResType::ID), ResType::ID},
278     { static_cast<int32_t>(ResType::THEME), ResType::THEME},
279     { static_cast<int32_t>(ResType::PLURAL), ResType::PLURAL},
280     { static_cast<int32_t>(ResType::FLOAT), ResType::FLOAT},
281     { static_cast<int32_t>(ResType::MEDIA), ResType::MEDIA},
282     { static_cast<int32_t>(ResType::PROF), ResType::PROF},
283     { static_cast<int32_t>(ResType::PATTERN), ResType::PATTERN},
284     { static_cast<int32_t>(ResType::SYMBOL), ResType::SYMBOL},
285     { static_cast<int32_t>(ResType::RES), ResType::RES},
286     { static_cast<int32_t>(ResType::INVALID_RES_TYPE), ResType::INVALID_RES_TYPE},
287 };
288 
289 const std::map<std::string, std::vector<uint32_t>> g_normalIconMap = {
290     { "sdpi-phone", {41, 144} },
291     { "sdpi-tablet", {51, 192} },
292     { "mdpi-phone", {54, 192} },
293     { "mdpi-tablet", {68, 256} },
294     { "ldpi-phone", {81, 288} },
295     { "ldpi-tablet", {102, 384} },
296     { "xldpi-phone", {108, 384} },
297     { "xldpi-tablet", {136, 512} },
298     { "xxldpi-phone", {162, 576} },
299     { "xxldpi-tablet", {204, 768} },
300     { "xxxldpi-phone", {216, 768} },
301     { "xxxldpi-tablet", {272, 1024} },
302 };
303 
304 const std::map<std::string, uint32_t> g_keyNodeIndexs = {
305     { "icon", 0 },
306     { "startWindowIcon", 1 },
307 };
308 
309 struct DirectoryInfo {
310     std::string limitKey;
311     std::string fileCluster;
312     std::string dirPath;
313     std::vector<KeyParam> keyParams;
314     ResType dirType;
315 };
316 
317 struct FileInfo : DirectoryInfo {
318     std::string filePath;
319     std::string filename;
320     ResType fileType;
321 };
322 
323 struct TargetConfig {
324     std::vector<KeyParam> mccmnc;
325     std::vector<KeyParam> locale;
326     std::vector<KeyParam> orientation;
327     std::vector<KeyParam> device;
328     std::vector<KeyParam> colormode;
329     std::vector<KeyParam> density;
330 };
331 
332 struct Mccmnc {
333     KeyParam mcc;
334     KeyParam mnc;
335     bool operator == (const Mccmnc &other)
336     {
337         if (mcc.value != other.mcc.value) {
338             return false;
339         }
340         if (mnc.keyType != KeyType::OTHER && other.mnc.keyType != KeyType::OTHER &&
341             mnc.value != other.mnc.value) {
342             return false;
343         }
344         return true;
345     }
346 };
347 
348 struct Locale {
349     KeyParam language;
350     KeyParam script;
351     KeyParam region;
352     bool operator == (const Locale &other)
353     {
354         if (language.value != other.language.value) {
355             return false;
356         }
357         if (script.keyType != KeyType::OTHER && other.script.keyType != KeyType::OTHER &&
358             script.value != other.script.value) {
359             return false;
360         }
361         if (region.keyType != KeyType::OTHER && other.region.keyType != KeyType::OTHER &&
362             region.value != other.region.value) {
363             return false;
364         }
365         return true;
366     }
367 };
368 
369 }
370 }
371 }
372 #endif
373