• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include "StageContext.h"
16 #include <sstream>
17 #include <fstream>
18 #include <cctype>
19 #include <algorithm>
20 #include "JsonReader.h"
21 #include "FileSystem.h"
22 #include "TraceTool.h"
23 #include "PreviewerEngineLog.h"
24 #include "CommandParser.h"
25 #include "zlib.h"
26 #include "contrib/minizip/unzip.h"
27 
28 namespace OHOS::Ide {
GetInstance()29 StageContext& StageContext::GetInstance()
30 {
31     static StageContext instance;
32     return instance;
33 }
34 
ReadFileContents(const std::string & filePath) const35 const std::optional<std::vector<uint8_t>> StageContext::ReadFileContents(const std::string& filePath) const
36 {
37     if (!FileSystem::IsFileExists(filePath)) {
38         ELOG("file %s is not exist.", filePath.c_str());
39         return std::nullopt;
40     }
41     std::ifstream file(filePath, std::ios::binary | std::ios::ate);
42     if (!file) {
43         ELOG("open file %s failed.", filePath.c_str());
44         return std::nullopt;
45     }
46     std::streamsize fileSize = file.tellg();
47     file.seekg(0, std::ios::beg);
48     std::vector<uint8_t> data(fileSize);
49     if (file.read(reinterpret_cast<char*>(data.data()), fileSize)) {
50         return data;
51     } else {
52         ELOG("read file %s failed.", filePath.c_str());
53         return std::nullopt;
54     }
55 }
56 
SetLoaderJsonPath(const std::string & assetPath)57 void StageContext::SetLoaderJsonPath(const std::string& assetPath)
58 {
59     loaderJsonPath = FileSystem::NormalizePath(assetPath);
60     if (loaderJsonPath.empty() || !FileSystem::IsFileExists(loaderJsonPath)) {
61         ELOG("the loaderJsonPath %s is not exist.", loaderJsonPath.c_str());
62         return;
63     }
64     ILOG("set loaderJsonPath: %s successed.", loaderJsonPath.c_str());
65 }
66 
SetHosSdkPath(const std::string & hosSdkPathValue)67 void StageContext::SetHosSdkPath(const std::string& hosSdkPathValue)
68 {
69     this->hosSdkPath = hosSdkPathValue;
70 }
71 
GetModulePathMapFromLoaderJson()72 void StageContext::GetModulePathMapFromLoaderJson()
73 {
74     if (!FileSystem::IsFileExists(loaderJsonPath)) {
75         ELOG("the loaderJsonPath is not exist.");
76         return;
77     }
78     std::string jsonStr = JsonReader::ReadFile(loaderJsonPath);
79     Json2::Value rootJson = JsonReader::ParseJsonData2(jsonStr);
80     if (rootJson.IsNull() || !rootJson.IsValid()) {
81         ELOG("Get loader.json content failed.");
82         return;
83     }
84     if (!rootJson.IsMember("modulePathMap") || !rootJson.IsMember("harNameOhmMap") ||
85         !rootJson.IsMember("projectRootPath")) {
86         ELOG("Don't find some necessary node in loader.json.");
87         return;
88     }
89     Json2::Value jsonObj = rootJson["modulePathMap"];
90     for (const auto& key : jsonObj.GetMemberNames()) {
91         modulePathMap[key] = jsonObj[key].AsString();
92     }
93     Json2::Value jsonObjOhm = rootJson["harNameOhmMap"];
94     if (rootJson.IsMember("hspNameOhmMap")) {
95         if (!rootJson["hspNameOhmMap"].IsNull() && rootJson["hspNameOhmMap"].IsValid()) {
96             ILOG("hspNameOhmMap is valid");
97             jsonObjOhm = rootJson["hspNameOhmMap"];
98         }
99     }
100     for (const auto& key : jsonObjOhm.GetMemberNames()) {
101         hspNameOhmMap[key] = jsonObjOhm[key].AsString();
102     }
103     projectRootPath = rootJson["projectRootPath"].AsString();
104     if (rootJson.IsMember("buildConfigPath")) {
105         buildConfigPath = rootJson["buildConfigPath"].AsString();
106     }
107 }
108 
GetHspAceModuleBuild(const std::string & hspConfigPath)109 std::string StageContext::GetHspAceModuleBuild(const std::string& hspConfigPath)
110 {
111     if (!FileSystem::IsFileExists(hspConfigPath)) {
112         ELOG("hspConfigPath: %s is not exist.", hspConfigPath.c_str());
113         return "";
114     }
115     std::string jsonStr = JsonReader::ReadFile(hspConfigPath);
116     Json2::Value rootJson = JsonReader::ParseJsonData2(jsonStr);
117     if (rootJson.IsNull() || !rootJson.IsValid()) {
118         ELOG("Get hsp buildConfig.json content failed.");
119         return "";
120     }
121     if (!rootJson.IsMember("aceModuleBuild")) {
122         ELOG("Don't find aceModuleBuild node in hsp buildConfig.json.");
123         return "";
124     }
125     return rootJson["aceModuleBuild"].AsString();
126 }
127 
ReleaseHspBuffers()128 void StageContext::ReleaseHspBuffers()
129 {
130     for (std::vector<uint8_t>* ptr : hspBufferPtrsVec) {
131         if (ptr) {
132             delete ptr;
133         }
134     }
135     hspBufferPtrsVec.clear();
136     ILOG("ReleaseHspBuffers finished.");
137 }
138 
GetModulePathMap() const139 std::map<std::string, std::string> StageContext::GetModulePathMap() const
140 {
141     return modulePathMap;
142 }
143 
GetModuleBuffer(const std::string & inputPath)144 std::vector<uint8_t>* StageContext::GetModuleBuffer(const std::string& inputPath)
145 {
146     ILOG("inputPath is:%s.", inputPath.c_str());
147     TraceTool::GetInstance().HandleTrace("HSP is loaded");
148     std::string spliter = "/";
149     size_t pos = inputPath.rfind(spliter);
150     if (pos == std::string::npos) {
151         ELOG("inputPath: %s format error.", inputPath.c_str());
152         return nullptr;
153     }
154     std::string bundleName = inputPath.substr(0, pos);
155     ILOG("bundleName is:%s.", bundleName.c_str());
156     if (bundleName.empty()) {
157         ELOG("bundleName is empty.");
158         return nullptr;
159     }
160     std::string moduleName = inputPath.substr(pos + spliter.size());
161     ILOG("moduleName is:%s.", moduleName.c_str());
162     if (modulePathMap.empty()) {
163         ELOG("modulePathMap is empty.");
164         return nullptr;
165     }
166     if (bundleName == localBundleName) { // local hsp
167         if (modulePathMap.count(moduleName) > 0) { // exist local hsp
168             return GetLocalModuleBuffer(moduleName);
169         } else { // local hsp not exist, load cloud hsp
170             ILOG("cloud hsp bundleName is same as the local project.");
171             return GetCloudModuleBuffer(moduleName);
172         }
173     } else {
174         // 先找三方hsp,再找系统hsp
175         std::vector<uint8_t>* buf = GetCloudModuleBuffer(moduleName);
176         if (buf) { // cloud hsp
177             return buf;
178         } else { // system hsp
179             std::vector<uint8_t>* buf = GetSystemModuleBuffer(inputPath, moduleName);
180             ILOG("system hsp buf size is %d", buf->size());
181             return buf;
182         }
183     }
184 }
185 
GetLocalModuleBuffer(const std::string & moduleName)186 std::vector<uint8_t>* StageContext::GetLocalModuleBuffer(const std::string& moduleName)
187 {
188     std::string modulePath = StageContext::GetInstance().modulePathMap[moduleName];
189     if (modulePath.empty()) {
190         ELOG("modulePath is empty.");
191         return nullptr;
192     }
193     ILOG("get modulePath: %s successed.", modulePath.c_str());
194     if (!FileSystem::IsDirectoryExists(modulePath)) {
195         ELOG("don't find moduleName: %s in modulePathMap from loader.json.", moduleName.c_str());
196         return nullptr;
197     }
198     if (ContainsRelativePath(modulePath)) {
199         ELOG("modulePath format error: %s.", modulePath.c_str());
200         return nullptr;
201     }
202     std::string separator = FileSystem::GetSeparator();
203     // 读取hsp的.preview/config/buildConfig.json获取aceModuleBuild值就是hsp的modules.abc所在文件夹
204     std::string hspConfigPath = modulePath + separator + ".preview" + separator + "config" +
205         separator + "buildConfig.json";
206     if (!buildConfigPath.empty()) {
207         ILOG("buildConfigPath is not empty.");
208         hspConfigPath = modulePath + separator + buildConfigPath;
209     }
210     std::string abcDir = GetHspAceModuleBuild(hspConfigPath);
211     if (!FileSystem::IsDirectoryExists(abcDir)) {
212         ELOG("the abcDir:%s is not exist.", abcDir.c_str());
213         return nullptr;
214     }
215     std::string abcPath = abcDir + separator + "modules.abc";
216     if (!FileSystem::IsFileExists(abcPath)) {
217         ELOG("the abcPath:%s is not exist.", abcPath.c_str());
218         return nullptr;
219     }
220     ILOG("get modules.abc path: %s successed.", abcPath.c_str());
221     std::optional<std::vector<uint8_t>> opt = ReadFileContents(abcPath);
222     if (!opt.has_value()) {
223         ELOG("read modules.abc buffer failed.");
224         return nullptr;
225     }
226     std::vector<uint8_t> *buf = new(std::nothrow) std::vector<uint8_t>(opt.value());
227     if (!buf) {
228         ELOG("Memory allocation failed: buf.");
229     }
230     hspBufferPtrsVec.push_back(buf);
231     return buf;
232 }
233 
GetCloudHspVersion(const std::string & hspPath,const std::string & actualName)234 std::string StageContext::GetCloudHspVersion(const std::string& hspPath, const std::string& actualName)
235 {
236     std::string flag = "@";
237     std::string spliter = actualName + flag;
238     // 以partName字符串拆分出版本号
239     size_t pos = hspPath.rfind(spliter);
240     if (pos == std::string::npos) {
241         ELOG("hspPath: %s format error. no spliter:%s exist", hspPath.c_str(), spliter.c_str());
242         return "";
243     }
244     int idx = pos + spliter.size();
245     return hspPath.substr(idx);
246 }
247 
SplitHspVersion(const std::string & version)248 std::vector<int> StageContext::SplitHspVersion(const std::string& version)
249 {
250     std::vector<int> segments;
251     std::istringstream iss(version);
252     std::string segment;
253     while (getline(iss, segment, '.')) {
254         segments.push_back(std::stoi(segment));
255     }
256     return segments;
257 }
258 
CompareHspVersion(const std::string & version1,const std::string & version2)259 int StageContext::CompareHspVersion(const std::string& version1, const std::string& version2)
260 {
261     ILOG("module hsp version:%s, project hsp version:%s", version1.c_str(), version2.c_str());
262     std::vector<int> ver1 = SplitHspVersion(version1);
263     std::vector<int> ver2 = SplitHspVersion(version2);
264     // 将两个版本号的分段个数补齐
265     while (ver1.size() < ver2.size()) {
266         ver1.push_back(0);
267     }
268     while (ver2.size() < ver1.size()) {
269         ver2.push_back(0);
270     }
271     // 逐段比较版本号
272     for (size_t i = 0; i < ver1.size(); ++i) {
273         if (ver1[i] < ver2[i]) {
274             return -1;
275         } else if (ver1[i] > ver2[i]) {
276             return 1;
277         }
278     }
279     return 0;
280 }
281 
GetActualCloudHspDir(const std::string & actualName)282 std::string StageContext::GetActualCloudHspDir(const std::string& actualName)
283 {
284     std::string moduleHspPath = GetCloudModuleHspPath(actualName);
285     std::string projectHspPath = GetCloudProjectHspPath(actualName);
286     ILOG("moduleHspPath:%s, projectHspPath:%s", moduleHspPath.c_str(), projectHspPath.c_str());
287     if (moduleHspPath.empty() || !FileSystem::IsDirectoryExists(moduleHspPath)) {
288         return projectHspPath; // 模块级不存在,加载项目级
289     }
290     if (projectHspPath.empty() || !FileSystem::IsDirectoryExists(projectHspPath)) {
291         return moduleHspPath; // 模块级存在,项目级不存在,加载模块级
292     }
293     // 模块级和项目级都存在,加载版本号高的
294     std::string moduleHspVersion = GetCloudHspVersion(moduleHspPath, actualName);
295     std::string projectHspVersion = GetCloudHspVersion(projectHspPath, actualName);
296     if (moduleHspVersion.empty()) {
297         return projectHspPath; // 模块级版本号不存在,加载项目级
298     }
299     if (projectHspVersion.empty()) {
300         return moduleHspPath; // 模块级版本号存在,项目级版本号不存在,加载模块级
301     }
302     int ret = CompareHspVersion(moduleHspVersion, projectHspVersion);
303     ILOG("CompareHspVersion result is:%d", ret);
304     return ret >= 0 ? moduleHspPath : projectHspPath; // 优先加载版本号高的,版本号相同则优先加载模块级的
305 }
306 
GetCloudProjectHspPath(const std::string & actualName)307 std::string StageContext::GetCloudProjectHspPath(const std::string& actualName)
308 {
309     ILOG("get projectRootPath:%s", projectRootPath.c_str());
310     std::string hspDir = projectRootPath + "/oh_modules/.hsp";
311     if (!FileSystem::IsDirectoryExists(hspDir)) {
312         ELOG("hspDir: %s in project is not exist.", hspDir.c_str());
313         return "";
314     }
315     return GetCloudHspPath(hspDir, actualName);
316 }
317 
GetCloudModuleHspPath(const std::string & actualName)318 std::string StageContext::GetCloudModuleHspPath(const std::string& actualName)
319 {
320     int upwardLevel = 5;
321     int pos = GetUpwardDirIndex(loaderJsonPath, upwardLevel);
322     if (pos < 0) {
323         ILOG("GetUpwardDirIndex:%d failed.", pos);
324         return "";
325     }
326     std::string moduleRootPath = loaderJsonPath.substr(0, pos);
327     ILOG("get moduleRootPath:%s", moduleRootPath.c_str());
328     std::string hspDir = moduleRootPath + "/oh_modules/.hsp";
329     if (!FileSystem::IsDirectoryExists(hspDir)) {
330         ELOG("hspDir: %s in module is not exist.", hspDir.c_str());
331         return "";
332     }
333     return GetCloudHspPath(hspDir, actualName);
334 }
335 
GetCloudModuleBuffer(const std::string & moduleName)336 std::vector<uint8_t>* StageContext::GetCloudModuleBuffer(const std::string& moduleName)
337 {
338     std::string actualName;
339     int ret = GetHspActualName(moduleName, actualName);
340     if (ret > 1) {
341         WLOG("have more same module name hsp in the project, load the first as default.");
342     }
343     if (actualName.empty()) {
344         ELOG("get hsp actual name failed.");
345         return nullptr;
346     }
347     // 1.以entry(指代模块根目录或项目根目录)拆分,拼接oh_modules/.hsp,在这个拼接目录下查找以actualName@开头的文件夹
348     // 2.获取拼接目录下的actualName.hsp文件
349     // 3.使用zlib获取hsp压缩包下的ets/modules.abc内容
350     std::string hspPath = GetActualCloudHspDir(actualName);
351     ILOG("get hspPath:%s actualName:%s", hspPath.c_str(), actualName.c_str());
352     if (!FileSystem::IsDirectoryExists(hspPath)) {
353         ELOG("hspPath: %s is not exist.", hspPath.c_str());
354         return nullptr;
355     }
356     std::string moduleHspFile = hspPath + "/" + actualName + ".hsp";
357     ILOG("get moduleHspFile:%s.", moduleHspFile.c_str());
358     if (!FileSystem::IsFileExists(moduleHspFile)) {
359         ELOG("the moduleHspFile:%s is not exist.", moduleHspFile.c_str());
360         return nullptr;
361     }
362     // unzip and get ets/moudles.abc buffer
363     std::vector<uint8_t>* buf = GetModuleBufferFromHsp(moduleHspFile, "ets/modules.abc");
364     if (!buf) {
365         ELOG("read modules.abc buffer failed.");
366     }
367     return buf;
368 }
369 
GetCloudHspPath(const std::string & hspDir,const std::string & moduleName)370 std::string StageContext::GetCloudHspPath(const std::string& hspDir, const std::string& moduleName)
371 {
372     std::string flag = "@";
373     std::string partName = moduleName + flag;
374     return FileSystem::FindSubfolderByName(hspDir, partName);
375 }
376 
GetModuleBufferFromHsp(const std::string & hspFilePath,const std::string & fileName)377 std::vector<uint8_t>* StageContext::GetModuleBufferFromHsp(const std::string& hspFilePath,
378     const std::string& fileName)
379 {
380     unzFile zipfile = unzOpen2(hspFilePath.c_str(), nullptr);
381     if (zipfile == NULL) {
382         printf("Failed to open the zip file: %s\n", hspFilePath.c_str());
383         return nullptr;
384     }
385 
386     if (unzLocateFile(zipfile, fileName.c_str(), 1) != UNZ_OK) {
387         printf("Failed to locate the file: %s\n", fileName.c_str());
388         unzClose(zipfile);
389         return nullptr;
390     }
391 
392     unz_file_info file_info;
393     if (unzGetCurrentFileInfo(zipfile, &file_info, NULL, 0, NULL, 0, NULL, 0) != UNZ_OK) {
394         printf("Failed to get the file info: %s\n", fileName.c_str());
395         unzClose(zipfile);
396         return nullptr;
397     }
398 
399     if (unzOpenCurrentFile(zipfile) != UNZ_OK) {
400         printf("Failed to open the file: %s\n", fileName.c_str());
401         unzClose(zipfile);
402         return nullptr;
403     }
404 
405     char buffer[1024];
406     int bytesRead;
407     std::vector<uint8_t>* fileContent = new(std::nothrow) std::vector<uint8_t>();
408     if (!fileContent) {
409         ELOG("Memory allocation failed: fileContent.");
410         return fileContent;
411     }
412     while ((bytesRead = unzReadCurrentFile(zipfile, buffer, sizeof(buffer))) > 0) {
413         fileContent->insert(fileContent->end(), buffer, buffer + bytesRead);
414     }
415     hspBufferPtrsVec.push_back(fileContent);
416     unzCloseCurrentFile(zipfile);
417     unzClose(zipfile);
418 
419     printf("File extracted and content saved: %s\n", fileName.c_str());
420     return fileContent;
421 }
422 
ContainsRelativePath(const std::string & path) const423 bool StageContext::ContainsRelativePath(const std::string& path) const
424 {
425     std::string flg1 = ".." + FileSystem::GetSeparator();
426     std::string flg2 = "." + FileSystem::GetSeparator();
427     return (path.find(flg1) != std::string::npos || path.find(flg2) != std::string::npos);
428 }
429 
ParseMockJsonFile(const std::string & mockJsonFilePath)430 std::map<std::string, std::string> StageContext::ParseMockJsonFile(const std::string& mockJsonFilePath)
431 {
432     std::map<std::string, std::string> mapInfo;
433     if (!FileSystem::IsFileExists(mockJsonFilePath)) {
434         ELOG("the mockJsonFilePath:%s is not exist.", mockJsonFilePath.c_str());
435         return mapInfo;
436     }
437     std::string jsonStr = JsonReader::ReadFile(mockJsonFilePath);
438     Json2::Value rootJson = JsonReader::ParseJsonData2(jsonStr);
439     if (rootJson.IsNull() || !rootJson.IsValid()) {
440         ELOG("get mock-config.json content failed.");
441         return mapInfo;
442     }
443     for (const auto& key : rootJson.GetMemberNames()) {
444         if (!rootJson[key].IsNull() && rootJson[key].IsMember("source") && rootJson[key]["source"].IsString()) {
445             mapInfo[key] = rootJson[key]["source"].AsString();
446         }
447     }
448     return mapInfo;
449 }
450 
GetUpwardDirIndex(const std::string & path,const int upwardLevel) const451 int StageContext::GetUpwardDirIndex(const std::string& path, const int upwardLevel) const
452 {
453     std::string::size_type pos = path.find_last_of(FileSystem::GetSeparator().c_str());
454     std::string::size_type count = 0;
455     while (count < upwardLevel) {
456         if (pos == std::string::npos) {
457             ELOG("GetUpwardDir:%s failed.");
458             int errCode = -1;
459             return errCode;
460         }
461         pos = path.find_last_of(FileSystem::GetSeparator().c_str(), pos - 1);
462         ++count;
463     }
464     ILOG("GetUpwardDir path:%s pos:%d", path.c_str(), pos);
465     return pos;
466 }
467 
ReplaceLastStr(const std::string & str,const std::string & find,const std::string & replace)468 std::string StageContext::ReplaceLastStr(const std::string& str, const std::string& find, const std::string& replace)
469 {
470     std::string ret = str;
471     size_t pos = ret.rfind(find);
472     if (pos != std::string::npos) {
473         ret.replace(pos, find.size(), replace);
474     }
475     return ret;
476 }
477 
GetHspActualName(const std::string & input,std::string & ret)478 int StageContext::GetHspActualName(const std::string& input, std::string& ret)
479 {
480     int num = 0;
481     std::string flag = "";
482     std::string bundleFlag = "/" + input + "/";
483     std::string normalizedFlag = "N&" + input + "&";
484     for (const auto& pair : hspNameOhmMap) {
485         if (pair.second.find("@normalized") == 0) {
486             flag = normalizedFlag;
487         } else {
488             flag = bundleFlag;
489         }
490         if (pair.second.find(flag) != std::string::npos) {
491             std::string actualName = pair.first;
492             if (actualName.find('/') != std::string::npos) { // 以组织名开头的包
493                 std::replace(actualName.begin(), actualName.end(), '/', '+');
494             }
495             if (num == 0) {
496                 ret = actualName;
497             }
498             num++;
499             WLOG("find hsp actual name:%s", actualName.c_str());
500         }
501     }
502     return num;
503 }
504 
GetSystemModuleBuffer(const std::string & inputPath,const std::string & moduleName)505 std::vector<uint8_t>* StageContext::GetSystemModuleBuffer(const std::string& inputPath,
506     const std::string& moduleName)
507 {
508     std::string head = "com.huawei";
509     std::string tail = moduleName;
510     size_t pos1 = inputPath.find(head) + head.size();
511     size_t pos2 = inputPath.find(tail);
512     std::string relativePath = inputPath.substr(pos1, pos2 - pos1);
513     size_t found = relativePath.find(".");
514     int len = 1;
515     while (found != std::string::npos) {
516         relativePath.replace(found, len, "/");
517         found = relativePath.find(".", found + len);
518     }
519     std::string moduleHspFile = hosSdkPath + "/systemHsp" + relativePath + moduleName + ".hsp";
520     ILOG("get system moduleHspFile:%s.", moduleHspFile.c_str());
521     if (!FileSystem::IsFileExists(moduleHspFile)) {
522         ELOG("the system moduleHspFile:%s is not exist.", moduleHspFile.c_str());
523         return nullptr;
524     }
525     // unzip and get ets/moudles.abc buffer
526     std::vector<uint8_t>* buf = GetModuleBufferFromHsp(moduleHspFile, "ets/modules.abc");
527     if (!buf) {
528         ELOG("read modules.abc buffer failed.");
529     }
530     return buf;
531 }
532 
SetPkgContextInfo(std::map<std::string,std::string> & pkgContextInfoJsonStringMap,std::map<std::string,std::string> & packageNameList)533 void StageContext::SetPkgContextInfo(std::map<std::string, std::string>& pkgContextInfoJsonStringMap,
534     std::map<std::string, std::string>& packageNameList)
535 {
536     const std::string path = CommandParser::GetInstance().GetAppResourcePath() +
537         FileSystem::GetSeparator() + "module.json";
538     std::string moduleJsonStr = JsonReader::ReadFile(path);
539     if (moduleJsonStr.empty()) {
540         ELOG("Get module.json content empty.");
541     }
542     Json2::Value rootJson1 = JsonReader::ParseJsonData2(moduleJsonStr);
543     if (rootJson1.IsNull() || !rootJson1.IsValid() || !rootJson1.IsMember("module")) {
544         ELOG("Get module.json content failed.");
545         return;
546     }
547     if (!rootJson1["module"].IsMember("name") || !rootJson1["module"]["name"].IsString()) {
548         return;
549     }
550     std::string moduleName = rootJson1["module"]["name"].AsString();
551     if (rootJson1["module"].IsMember("packageName") && rootJson1["module"]["packageName"].IsString()) {
552         std::string pkgName = rootJson1["module"]["packageName"].AsString();
553         packageNameList = {{moduleName, pkgName}};
554     }
555     std::string jsonPath = CommandParser::GetInstance().GetLoaderJsonPath();
556     std::string flag = "loader.json";
557     int idx = jsonPath.find_last_of(flag);
558     std::string dirPath = jsonPath.substr(0, idx - flag.size() + 1); // 1 is for \ or /
559     std::string ctxPath = dirPath + "pkgContextInfo.json";
560     std::string ctxInfoJsonStr = JsonReader::ReadFile(ctxPath);
561     if (ctxInfoJsonStr.empty()) {
562         ELOG("Get pkgContextInfo.json content empty.");
563         return;
564     }
565     pkgContextInfoJsonStringMap = {{moduleName, ctxInfoJsonStr}};
566 }
567 }