2023-03-11 22:10:18 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2013 cocos2d-x.org
|
|
|
|
Copyright (c) Microsoft Open Technologies, Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2024-03-07 08:47:00 +08:00
|
|
|
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
|
2023-03-11 22:10:18 +08:00
|
|
|
|
2023-03-12 01:44:55 +08:00
|
|
|
https://axmolengine.github.io/
|
2023-03-11 22:10:18 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "platform/winrt/WinRTUtils.h"
|
|
|
|
#include "base/Macros.h"
|
|
|
|
#include "platform/PlatformMacros.h"
|
|
|
|
#include "platform/FileUtils.h"
|
|
|
|
#include "base/UserDefault.h"
|
2023-03-11 22:10:18 +08:00
|
|
|
#include "ntcvt/ntcvt.hpp"
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
#include <winrt/Windows.Networking.Connectivity.h>
|
|
|
|
#include <winrt/Windows.Foundation.Metadata.h>
|
|
|
|
#include <winrt/Windows.Foundation.Collections.h>
|
|
|
|
#include <winrt/Windows.UI.Xaml.Controls.h>
|
2023-03-11 22:10:18 +08:00
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
using namespace winrt;
|
2023-03-11 22:10:18 +08:00
|
|
|
using namespace Windows::Graphics::Display;
|
|
|
|
using namespace concurrency;
|
|
|
|
using namespace Windows::Storage;
|
|
|
|
using namespace Windows::Storage::Pickers;
|
|
|
|
using namespace Windows::Storage::Streams;
|
|
|
|
using namespace Windows::Networking::Connectivity;
|
2023-09-26 00:11:33 +08:00
|
|
|
using namespace Windows::UI::Xaml;
|
|
|
|
using namespace Windows::UI::Xaml::Controls;
|
|
|
|
|
|
|
|
NS_AX_BEGIN
|
2023-03-11 22:10:18 +08:00
|
|
|
|
|
|
|
bool isWindowsPhone()
|
|
|
|
{
|
|
|
|
#if _MSC_VER >= 1900
|
2023-09-26 00:11:33 +08:00
|
|
|
if (Windows::Foundation::Metadata::ApiInformation::IsTypePresent(L"Windows.Phone.UI.Input.HardwareButtons"))
|
2023-03-11 22:10:18 +08:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
#elif (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
std::string PlatformStringToString(const winrt::hstring& s) {
|
|
|
|
return ntcvt::from_chars(std::wstring_view(s.data(), s.size()));
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
winrt::hstring PlatformStringFromString(std::string_view s)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
|
|
|
std::wstring ws = ntcvt::from_chars(s);
|
2023-09-26 00:11:33 +08:00
|
|
|
return winrt::hstring(ws.data(), static_cast<unsigned int>(ws.length()));
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// Method to convert a length in device-independent pixels (DIPs) to a length in physical pixels.
|
|
|
|
float ConvertDipsToPixels(float dips)
|
|
|
|
{
|
|
|
|
static const float dipsPerInch = 96.0f;
|
|
|
|
return floor(dips * DisplayProperties::LogicalDpi / dipsPerInch + 0.5f); // Round to nearest integer.
|
|
|
|
}
|
|
|
|
|
|
|
|
float getScaledDPIValue(float v) {
|
|
|
|
auto dipFactor = DisplayProperties::LogicalDpi / 96.0f;
|
|
|
|
return v * dipFactor;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void AX_DLL printIPAddresses()
|
|
|
|
{
|
|
|
|
auto hostnames = NetworkInformation::GetHostNames();
|
2023-09-26 00:11:33 +08:00
|
|
|
int length = hostnames.Size();
|
2023-03-11 22:10:18 +08:00
|
|
|
|
|
|
|
for(int i = 0; i < length; i++)
|
|
|
|
{
|
2023-09-26 00:11:33 +08:00
|
|
|
auto hn = hostnames.GetAt(i);
|
|
|
|
if (hn.IPInformation() != nullptr)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
2023-09-26 00:11:33 +08:00
|
|
|
std::string s = PlatformStringToString(hn.DisplayName());
|
2024-03-07 08:47:00 +08:00
|
|
|
AXLOGI("IP Address: {}:", s);
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string AX_DLL getDeviceIPAddresses()
|
|
|
|
{
|
|
|
|
std::stringstream result;
|
|
|
|
|
|
|
|
auto hostnames = NetworkInformation::GetHostNames();
|
2023-09-26 00:11:33 +08:00
|
|
|
int length = hostnames.Size();
|
2023-03-11 22:10:18 +08:00
|
|
|
|
|
|
|
for(int i = 0; i < length; i++)
|
|
|
|
{
|
2023-09-26 00:11:33 +08:00
|
|
|
auto hn = hostnames.GetAt(i);
|
|
|
|
if (hn.IPInformation() != nullptr)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
2023-09-26 00:11:33 +08:00
|
|
|
result << PlatformStringToString(hn.DisplayName()) << std::endl;
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.str();
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
Windows::Foundation::IInspectable findXamlElement(Windows::Foundation::IInspectable const& parent, winrt::hstring const& name)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
2023-09-26 00:11:33 +08:00
|
|
|
if (parent == nullptr || name.empty())
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
FrameworkElement element = parent.try_as<FrameworkElement>();
|
|
|
|
if (!element)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
if (element.Name() == name)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
Panel panel = element.try_as <Panel>();
|
|
|
|
if (!panel)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
int count = panel.Children().Size();
|
2023-03-11 22:10:18 +08:00
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
2023-09-26 00:11:33 +08:00
|
|
|
auto result = findXamlElement(panel.Children().GetAt(i), name);
|
2023-03-11 22:10:18 +08:00
|
|
|
if (result != nullptr)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
bool removeXamlElement(Windows::Foundation::IInspectable const& parent,
|
|
|
|
Windows::Foundation::IInspectable const& element)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
2023-09-26 00:11:33 +08:00
|
|
|
Panel panel = parent.try_as<Panel>();
|
2023-03-11 22:10:18 +08:00
|
|
|
if (panel == nullptr)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
auto uiElement = element.try_as<UIElement>();
|
2023-03-11 22:10:18 +08:00
|
|
|
if (uiElement == nullptr)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int index;
|
2023-09-26 00:11:33 +08:00
|
|
|
if (!panel.Children().IndexOf(uiElement, index))
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
panel.Children().RemoveAt(index);
|
2023-03-11 22:10:18 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
bool replaceXamlElement(Windows::Foundation::IInspectable const& parent,
|
|
|
|
Windows::Foundation::IInspectable const& add,
|
|
|
|
Windows::Foundation::IInspectable const& remove)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
2023-09-26 00:11:33 +08:00
|
|
|
Panel panel = parent.try_as<Panel>();
|
2023-03-11 22:10:18 +08:00
|
|
|
if (panel == nullptr)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
UIElement addElement = add.try_as<UIElement>();
|
2023-03-11 22:10:18 +08:00
|
|
|
if (addElement == nullptr)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
UIElement removeElement = remove.try_as<UIElement>();
|
2023-03-11 22:10:18 +08:00
|
|
|
if (removeElement == nullptr)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int index;
|
2023-09-26 00:11:33 +08:00
|
|
|
if (!panel.Children().IndexOf(removeElement, index))
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
panel.Children().RemoveAt(index);
|
|
|
|
panel.Children().InsertAt(index, addElement);
|
2023-03-11 22:10:18 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string computeHashForFile(const std::string& filePath)
|
|
|
|
{
|
|
|
|
std::string ret = filePath;
|
|
|
|
size_t pos = ret.find_last_of('/');
|
|
|
|
|
|
|
|
if (pos != std::string::npos) {
|
|
|
|
ret = ret.substr(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = ret.find_last_of('.');
|
|
|
|
|
|
|
|
if (pos != std::string::npos) {
|
|
|
|
ret = ret.substr(0, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
CREATEFILE2_EXTENDED_PARAMETERS extParams = { 0 };
|
|
|
|
extParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
|
|
|
|
extParams.dwFileFlags = FILE_FLAG_RANDOM_ACCESS;
|
|
|
|
extParams.dwSecurityQosFlags = SECURITY_ANONYMOUS;
|
|
|
|
extParams.dwSize = sizeof(extParams);
|
|
|
|
extParams.hTemplateFile = nullptr;
|
|
|
|
extParams.lpSecurityAttributes = nullptr;
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
winrt::file_handle handle{CreateFile2(std::wstring(filePath.begin(), filePath.end()).c_str(), GENERIC_READ,
|
|
|
|
FILE_SHARE_READ, OPEN_EXISTING, &extParams)};
|
|
|
|
if (handle)
|
|
|
|
{
|
2023-03-11 22:10:18 +08:00
|
|
|
FILE_BASIC_INFO fInfo = { 0 };
|
2023-09-26 00:11:33 +08:00
|
|
|
if (GetFileInformationByHandleEx(handle.get(), FileBasicInfo, &fInfo, sizeof(FILE_BASIC_INFO)))
|
|
|
|
{
|
2023-03-11 22:10:18 +08:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << ret << "_";
|
|
|
|
ss << fInfo.CreationTime.QuadPart;
|
|
|
|
ss << fInfo.ChangeTime.QuadPart;
|
|
|
|
ret = ss.str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool createMappedCacheFile(const std::string& srcFilePath, std::string& cacheFilePath, const std::string& ext /* = "" */)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
auto folderPath = FileUtils::getInstance()->getWritablePath();
|
|
|
|
cacheFilePath = folderPath + computeHashForFile(srcFilePath) + ext;
|
|
|
|
auto prevFile = UserDefault::getInstance()->getStringForKey(srcFilePath.c_str());
|
|
|
|
|
|
|
|
if (prevFile == cacheFilePath) {
|
|
|
|
ret = FileUtils::getInstance()->isFileExist(cacheFilePath);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
FileUtils::getInstance()->removeFile(prevFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
UserDefault::getInstance()->setStringForKey(srcFilePath.c_str(), cacheFilePath);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroyMappedCacheFile(const std::string& key)
|
|
|
|
{
|
|
|
|
auto value = UserDefault::getInstance()->getStringForKey(key.c_str());
|
|
|
|
|
|
|
|
if (!value.empty()) {
|
|
|
|
FileUtils::getInstance()->removeFile(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
UserDefault::getInstance()->setStringForKey(key.c_str(), "");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_AX_END
|