Skip to content

Resolved bug #557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion native~/Shared/src/UnityAssetAccessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <DotNet/UnityEngine/Networking/UploadHandlerRaw.h>

#include <algorithm>
#include <regex>
#include <filesystem>

using namespace CesiumAsync;
using namespace CesiumUtility;
Expand Down Expand Up @@ -113,6 +115,28 @@ std::string replaceInvalidChars(const std::string& input) {
return result;
}

std::string extractFilePath(const std::string& url) {
// 正则表达式:匹配 file:// 后的路径部分,忽略查询参数
std::string strregex;
if (url.find("file:///") == 0)
{
strregex = R"(file:///([^?]+))";
}
else
{
strregex = R"(file://([^?]+))";
}
std::regex re(strregex);
std::smatch match;

// 查找匹配的部分
if (std::regex_search(url, match, re)) {
return match[1]; // 返回匹配到的文件路径
}
else {
return ""; // 如果没有匹配到,返回空字符串
}
}
} // namespace

namespace CesiumForUnityNative {
Expand Down Expand Up @@ -169,6 +193,19 @@ UnityAssetAccessor::get(
.createPromise<std::shared_ptr<CesiumAsync::IAssetRequest>>();

auto future = promise.getFuture();
bool isOffline = url.find("file") == 0;
if (isOffline)
{
std::string filePath = extractFilePath(url);
if (!std::filesystem::exists(filePath))
{
promise.reject(std::runtime_error(fmt::format(
"Request for `{}` failed: {}",
request.url().ToStlString(),
request.error().ToStlString())));
return future;
}
}

UnityEngine::Networking::UnityWebRequestAsyncOperation op =
request.SendWebRequest();
Expand Down Expand Up @@ -202,7 +239,7 @@ UnityAssetAccessor::request(
const std::string& verb,
const std::string& url,
const std::vector<THeader>& headers,
const std::span<const std::byte>& contentPayload) {
const gsl::span<const std::byte>& contentPayload) {
if (contentPayload.size() >
size_t(std::numeric_limits<std::int32_t>::max())) {
// This implementation cannot be used to send more than 2 gigabytes - just
Expand Down