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