• 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 
16 #include "platform_windows.h"
17 #include "os/platform.h"
18 
19 #include <core/namespace.h>
20 #include <core/io/intf_file_manager.h>
21 
22 #include "io/path_tools.h"
23 #include "util/string_util.h"
24 
25 
26 CORE_BEGIN_NAMESPACE()
27 using BASE_NS::string;
28 using BASE_NS::string_view;
29 
PlatformWindows(PlatformCreateInfo const & createInfo)30 PlatformWindows::PlatformWindows(PlatformCreateInfo const& createInfo)
31 {
32     // Convert the input paths to absolute.
33     auto cwd = GetCurrentDirectory();
34     string_view curDrive, curPath, curFilename, curExt;
35     SplitPath(cwd, curDrive, curPath, curFilename, curExt);
36 
37     auto fixPath = [&](string_view pathRaw) -> string {
38         if (pathRaw.empty()) {
39             return {};
40         }
41         // fix slashes. (just change \\ to /)
42         string_view pathIn = pathRaw;
43         string tmp;
44         if (pathIn.find("\\") != string_view::npos) {
45             tmp = pathIn;
46             StringUtil::FindAndReplaceAll(tmp, "\\", "/");
47             pathIn = tmp;
48         }
49 
50         string_view drive, path, filename, ext;
51         SplitPath(pathIn, drive, path, filename, ext);
52         string res = "/";
53         if (drive.empty()) {
54             // relative to current drive then
55             res += curDrive;
56         } else {
57             res += drive;
58         }
59         res += ":";
60         string normalizedPath;
61         if (path.empty()) {
62             normalizedPath = curPath;
63         } else {
64             if (path[0] != '/') {
65                 // relative path.
66                 normalizedPath = NormalizePath(curPath + path);
67             } else {
68                 normalizedPath = NormalizePath(path);
69             }
70         }
71         if (normalizedPath.empty()) {
72             // Invalid path? how to handle this?
73             // just fallback on current path for now. (hoping that it's somewhat safe)
74             normalizedPath = curPath;
75         }
76         res += normalizedPath;
77         return string(res.substr(1));
78     };
79 
80     plat_.coreRootPath = fixPath(createInfo.coreRootPath);
81     plat_.appRootPath = fixPath(createInfo.appRootPath);
82     plat_.appPluginPath = fixPath(createInfo.appPluginPath);
83 }
84 
GetPlatformData() const85 const PlatformData& PlatformWindows::GetPlatformData() const
86 {
87     return plat_;
88 }
89 
RegisterDefaultPaths(IFileManager & fileManager)90 string PlatformWindows::RegisterDefaultPaths(IFileManager& fileManager)
91 {
92     // register path to system plugins (this does not actually do anything anymore, pluginregistry has it's one
93     // filemanager instance etc..) Root path is the location where system plugins , non-rofs assets etc could be held.
94     const string coreDirectory = "file://" + plat_.coreRootPath;
95 
96     // Create plugins:// protocol that points to plugin files under coredirectory.
97     fileManager.RegisterPath("plugins", coreDirectory + "plugins/", false);
98 
99 #if (CORE_EMBEDDED_ASSETS_ENABLED == 0) || (CORE_DEV_ENABLED == 1)
100     const string assetRoot = coreDirectory + "assets/";
101 
102     // Create engine:// protocol that points to core asset files on the filesystem.
103     CORE_LOG_I("Registered core asset path: '%score/'", assetRoot.c_str());
104     fileManager.RegisterPath("engine", assetRoot + "core/", false);
105 #endif
106     return coreDirectory;
107 }
108 
RegisterPluginLocations(IPluginRegister & registry)109 void PlatformWindows::RegisterPluginLocations(IPluginRegister& registry)
110 {
111     constexpr string_view fileproto("file://");
112     registry.RegisterPluginPath(fileproto + GetPlatformData().coreRootPath + "plugins/");
113     if (!GetPlatformData().appPluginPath.empty()) {
114         registry.RegisterPluginPath(fileproto + GetPlatformData().appPluginPath);
115     }
116 }
117 
Create(PlatformCreateInfo const & createInfo)118 CORE_NS::IPlatform::Ptr Platform::Create(PlatformCreateInfo const& createInfo)
119 {
120     return CORE_NS::IPlatform::Ptr(new PlatformWindows(createInfo));
121 }
122 
123 CORE_END_NAMESPACE()
124 
125