diff --git a/src/YMhut.Box.Core/Api/ApiEndpoints.cs b/src/YMhut.Box.Core/Api/ApiEndpoints.cs new file mode 100644 index 0000000..67ba7c7 --- /dev/null +++ b/src/YMhut.Box.Core/Api/ApiEndpoints.cs @@ -0,0 +1,270 @@ +namespace YMhut.Box.Core.Api; + +public enum ApiSourceVisibility +{ + PublicOfficial, + PublicTrusted, + SensitivePrivate +} + +public sealed record ApiEndpoint( + string Id, + string Name, + string UriTemplate, + string SourceName, + string SourceUrl, + bool IsOfficial = true, + ApiSourceVisibility? VisibilityOverride = null) +{ + public ApiSourceVisibility SourceVisibility => + VisibilityOverride ?? (IsOfficial ? ApiSourceVisibility.PublicOfficial : ApiSourceVisibility.PublicTrusted); + + public bool ShouldHideEndpoint => SourceVisibility == ApiSourceVisibility.SensitivePrivate || + Id.Equals("http_diagnostic", StringComparison.OrdinalIgnoreCase) || + UriTemplate.Contains("update.ymhut.cn", StringComparison.OrdinalIgnoreCase) || + UriTemplate.Contains("api.pearapi.ai", StringComparison.OrdinalIgnoreCase); +} + +public static class ApiEndpoints +{ + private const string PearApiBase = "https://api.pearapi.ai"; + private const string DailyHotBase = PearApiBase + "/api/dailyhot/"; + private const string AiLatestNewsBase = PearApiBase + "/api/latest_ai_consultative"; + private const string HistoryTodayBase = PearApiBase + "/api/lsjt/?type=json"; + private const string CityRouteBase = PearApiBase + "/api/citytravelroutes/"; + private const string MaoyanBase = PearApiBase + "/api/maoyan/"; + + private static readonly IReadOnlyDictionary DailyHotShortcuts = new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["baidu_hot"] = "百度", + ["bili_hot"] = "哔哩哔哩", + ["zhihu_hot"] = "知乎" + }; + + private static readonly IReadOnlyDictionary Endpoints = new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["baidu_hot"] = new("baidu_hot", "百度热搜", DailyHotBase, "PearAPI DailyHot", PearApiBase, true, ApiSourceVisibility.SensitivePrivate), + ["bili_hot"] = new("bili_hot", "B 站热榜", DailyHotBase, "PearAPI DailyHot", PearApiBase, true, ApiSourceVisibility.SensitivePrivate), + ["zhihu_hot"] = new("zhihu_hot", "知乎热榜", DailyHotBase, "PearAPI DailyHot", PearApiBase, true, ApiSourceVisibility.SensitivePrivate), + ["hotboard"] = new("hotboard", "今日热榜", DailyHotBase, "PearAPI DailyHot", PearApiBase, true, ApiSourceVisibility.SensitivePrivate), + ["football_news"] = new("football_news", "体育新闻", "https://www.fifa.com/en", "FIFA", "https://www.fifa.com/en"), + ["tech_news"] = new("tech_news", "科技新闻", "https://36kr.com/feed", "36Kr RSS", "https://36kr.com/feed"), + ["cctv_news"] = new("cctv_news", "央视新闻", "https://news.cctv.com/", "CCTV News", "https://news.cctv.com/"), + ["earthquake_info"] = new("earthquake_info", "地震信息", "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson", "USGS Earthquake Hazards Program", "https://earthquake.usgs.gov/earthquakes/feed/v1.0/geojson.php"), + ["ip_info"] = new("ip_info", "IP / 域名详情", "https://rdap.org/ip/{value}", "RDAP", "https://www.iana.org/rdap"), + ["ip_lookup"] = new("ip_lookup", "IP RDAP 查询", "https://rdap.org/ip/{value}", "RDAP", "https://www.iana.org/rdap"), + ["dns_query"] = new("dns_query", "DNS 解析", "https://dns.google/resolve?name={value}&type=A", "Google Public DNS", "https://developers.google.com/speed/public-dns/docs/doh/json"), + ["weather"] = new("weather", "天气查询", "https://geocoding-api.open-meteo.com/v1/search?name={value}&count=1&language=zh&format=json", "Open-Meteo", "https://open-meteo.com/", false), + ["history_today"] = new("history_today", "历史上的今天", HistoryTodayBase, "PearAPI History Today", PearApiBase, true, ApiSourceVisibility.SensitivePrivate), + ["domain_price"] = new("domain_price", "域名官方信息", "https://rdap.org/domain/{value}", "RDAP", "https://www.iana.org/rdap"), + ["wx_domain_check"] = new("wx_domain_check", "微信域名检查说明", "https://developers.weixin.qq.com/doc/", "微信公众平台文档", "https://developers.weixin.qq.com/doc/"), + ["qq_avatar"] = new("qq_avatar", "QQ 信息", "https://uapis.cn/api/v1/social/qq/userinfo?qq={value}", "Uapis QQ User Info", "https://uapis.cn/docs/api-reference/get-social-qq-userinfo", false, ApiSourceVisibility.SensitivePrivate), + ["qq_profile"] = new("qq_profile", "QQ 公开资料", "https://r.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?uins={value}", "QQ 公开资料源", "https://qzone.qq.com/", false), + ["gold_price"] = new("gold_price", "黄金价格", "https://prices.lbma.org.uk/json/gold_pm.json", "LBMA Precious Metal Prices", "https://www.lbma.org.uk/prices-and-data/precious-metal-prices"), + ["oil_price"] = new("oil_price", "成品油价格政策", "https://www.ndrc.gov.cn/", "国家发展改革委", "https://www.ndrc.gov.cn/", true), + ["movie_box_office"] = new("movie_box_office", "电影票房", MaoyanBase, "PearAPI Maoyan", PearApiBase, true, ApiSourceVisibility.SensitivePrivate), + ["media_types"] = new("media_types", "随机放映室远程配置", "https://update.ymhut.cn/media-types.json", "YMhut Remote Config", "https://update.ymhut.cn/media-types.json", false, ApiSourceVisibility.SensitivePrivate), + ["train_query"] = new("train_query", "铁路车站数据", "https://kyfw.12306.cn/otn/resources/js/framework/station_name.js", "中国铁路 12306", "https://www.12306.cn/"), + ["rdap_domain_lookup"] = new("rdap_domain_lookup", "域名 RDAP", "https://rdap.org/domain/{value}", "RDAP", "https://www.iana.org/rdap"), + ["rdap_ip_lookup"] = new("rdap_ip_lookup", "IP/ASN RDAP", "https://rdap.org/ip/{value}", "RDAP", "https://www.iana.org/rdap"), + ["http_diagnostic"] = new("http_diagnostic", "HTTP 诊断", "{value}", "目标站点", "about:blank", false), + ["ai_latest_news"] = new("ai_latest_news", "AI 全网最新资讯", AiLatestNewsBase, "PearAPI AI News", PearApiBase, true, ApiSourceVisibility.SensitivePrivate), + ["city_route_query"] = new("city_route_query", "城际路线查询", CityRouteBase, "PearAPI City Travel Routes", PearApiBase, true, ApiSourceVisibility.SensitivePrivate) + }; + + public static bool TryResolve(string id, string input, out ApiEndpoint endpoint, out Uri uri) + { + endpoint = default!; + uri = default!; + if (!Endpoints.TryGetValue(id, out var match)) + { + return false; + } + + switch (id.ToLowerInvariant()) + { + case "hotboard": + return ResolveDailyHot(match, input, out endpoint, out uri); + case "baidu_hot": + case "bili_hot": + case "zhihu_hot": + return ResolveDailyHot(match, string.IsNullOrWhiteSpace(input) ? DailyHotShortcuts[id] : input, out endpoint, out uri); + case "history_today": + return ResolveHistoryToday(match, input, out endpoint, out uri); + case "ai_latest_news": + return ResolveAiLatestNews(match, input, out endpoint, out uri); + case "city_route_query": + return ResolveCityRoute(match, input, out endpoint, out uri); + case "movie_box_office": + endpoint = match; + uri = new Uri(match.UriTemplate); + return true; + case "dns_query": + return ResolveDnsQuery(match, input, out endpoint, out uri); + } + + var fallback = id switch + { + "weather" => "北京", + "history_today" => DateTime.Today.ToString("MM-dd"), + "dns_query" => "example.com", + "domain_price" or "rdap_domain_lookup" => "example.com", + "rdap_ip_lookup" or "ip_info" or "ip_lookup" => "8.8.8.8", + "wx_domain_check" or "http_diagnostic" => "https://example.com", + "qq_avatar" => "10000", + "qq_profile" => "10000", + _ => string.Empty + }; + var value = string.IsNullOrWhiteSpace(input) ? fallback : input.Trim(); + if (id.Equals("history_today", StringComparison.OrdinalIgnoreCase)) + { + value = NormalizeOnThisDay(value); + } + + if (string.IsNullOrWhiteSpace(value) && match.UriTemplate.Contains("{value}", StringComparison.Ordinal)) + { + value = "example.com"; + } + + var encodedValue = id.Equals("http_diagnostic", StringComparison.OrdinalIgnoreCase) || + id.Equals("history_today", StringComparison.OrdinalIgnoreCase) + ? value + : Uri.EscapeDataString(value); + var rawUri = match.UriTemplate.Replace("{value}", encodedValue, StringComparison.Ordinal); + if (id.Equals("http_diagnostic", StringComparison.OrdinalIgnoreCase) && + !rawUri.StartsWith("http://", StringComparison.OrdinalIgnoreCase) && + !rawUri.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) + { + rawUri = "https://" + rawUri; + } + + if (!Uri.TryCreate(rawUri, UriKind.Absolute, out var parsedUri)) + { + return false; + } + + endpoint = match; + uri = parsedUri; + return true; + } + + public static bool TryGet(string id, out ApiEndpoint endpoint) + { + return Endpoints.TryGetValue(id, out endpoint!); + } + + public static IReadOnlyList All => Endpoints.Values.ToList(); + + private static bool ResolveDailyHot(ApiEndpoint match, string input, out ApiEndpoint endpoint, out Uri uri) + { + endpoint = match; + var title = string.IsNullOrWhiteSpace(input) ? string.Empty : input.Trim(); + if (title.StartsWith("title=", StringComparison.OrdinalIgnoreCase)) + { + title = title[6..]; + } + + if (string.IsNullOrWhiteSpace(title)) + { + uri = new Uri(DailyHotBase); + return true; + } + + var encoded = Uri.EscapeDataString(title); + uri = new Uri($"{DailyHotBase}?title={encoded}"); + return true; + } + + private static bool ResolveHistoryToday(ApiEndpoint match, string input, out ApiEndpoint endpoint, out Uri uri) + { + endpoint = match; + var value = string.IsNullOrWhiteSpace(input) ? DateTime.Today.ToString("MM-dd") : NormalizeOnThisDay(input); + var encoded = Uri.EscapeDataString(value); + uri = new Uri($"{HistoryTodayBase}&date={encoded}"); + return true; + } + + private static bool ResolveAiLatestNews(ApiEndpoint match, string input, out ApiEndpoint endpoint, out Uri uri) + { + endpoint = match; + if (string.IsNullOrWhiteSpace(input)) + { + uri = new Uri(AiLatestNewsBase + "?key="); + return true; + } + + var key = input.Trim(); + if (key.StartsWith("key=", StringComparison.OrdinalIgnoreCase)) + { + key = key[4..]; + } + + uri = new Uri($"{AiLatestNewsBase}?key={Uri.EscapeDataString(key)}"); + return true; + } + + private static bool ResolveCityRoute(ApiEndpoint match, string input, out ApiEndpoint endpoint, out Uri uri) + { + endpoint = match; + var (from, to) = ParseCityRouteInput(input); + var fromPart = Uri.EscapeDataString(from); + var toPart = Uri.EscapeDataString(to); + uri = new Uri($"{CityRouteBase}?from={fromPart}&to={toPart}"); + return true; + } + + private static bool ResolveDnsQuery(ApiEndpoint match, string input, out ApiEndpoint endpoint, out Uri uri) + { + var parts = input.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + var name = parts.ElementAtOrDefault(0); + var type = parts.ElementAtOrDefault(1); + var value = string.IsNullOrWhiteSpace(name) ? "example.com" : name; + type = string.IsNullOrWhiteSpace(type) ? "A" : type.ToUpperInvariant(); + endpoint = match; + uri = new Uri($"https://dns.google/resolve?name={Uri.EscapeDataString(value)}&type={Uri.EscapeDataString(type)}"); + return true; + } + + private static (string From, string To) ParseCityRouteInput(string input) + { + var value = input.Replace("\r\n", "\n").Trim(); + if (value.Contains("->", StringComparison.Ordinal)) + { + var parts = value.Split("->", 2, StringSplitOptions.TrimEntries); + return (parts.ElementAtOrDefault(0) ?? "广州", parts.ElementAtOrDefault(1) ?? "深圳"); + } + + var lines = value.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + if (lines.Length >= 2) + { + return (lines[0], lines[1]); + } + + var tokens = value.Split(new[] { ' ', ',', ';', '|', '\t' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + if (tokens.Length >= 2) + { + return (tokens[0], tokens[1]); + } + + return ("广州", "深圳"); + } + + private static string NormalizeOnThisDay(string input) + { + if (DateTime.TryParse(input, out var date)) + { + return $"{date:MM-dd}"; + } + + var trimmed = input.Trim().Replace('/', '-'); + var parts = trimmed.Split('-', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + if (parts.Length >= 2 && + int.TryParse(parts[^2], out var month) && + int.TryParse(parts[^1], out var day)) + { + return $"{month:00}-{day:00}"; + } + + return $"{DateTime.Today:MM-dd}"; + } +} diff --git a/src/YMhut.Box.Core/Api/ApiManager.cs b/src/YMhut.Box.Core/Api/ApiManager.cs new file mode 100644 index 0000000..ffef8e5 --- /dev/null +++ b/src/YMhut.Box.Core/Api/ApiManager.cs @@ -0,0 +1,149 @@ +using YMhut.Box.Core; +using YMhut.Box.Core.Logging; +using YMhut.Box.Core.Net; + +namespace YMhut.Box.Core.Api; + +public enum ApiHealthStatus +{ + Unknown, + Healthy, + Degraded, + Unhealthy +} + +public sealed record ApiResponse( + string EndpointId, + Uri Uri, + bool Success, + string Content, + string? Error, + DateTimeOffset FetchedAt, + int StatusCode = 0); + +public interface IApiManager +{ + Task FetchAsync(string endpointId, string input = "", CancellationToken cancellationToken = default); + + Task FetchUriAsync(string endpointId, Uri uri, string input = "", CancellationToken cancellationToken = default); + + Task CheckHealthAsync(string endpointId, string input = "", CancellationToken cancellationToken = default); +} + +public sealed class ApiManager(IHttpService httpService, ILogService? logService = null) : IApiManager +{ + private readonly Dictionary _cache = new(StringComparer.OrdinalIgnoreCase); + private readonly TimeSpan _cacheDuration = TimeSpan.FromMinutes(5); + + public async Task FetchAsync(string endpointId, string input = "", CancellationToken cancellationToken = default) + { + if (!ApiEndpoints.TryResolve(endpointId, input, out var endpoint, out var uri)) + { + return new ApiResponse(endpointId, new Uri("about:blank"), false, string.Empty, "No upstream endpoint is configured for this tool.", DateTimeOffset.Now); + } + + return await FetchUriAsync(endpoint.Id, uri, input, cancellationToken).ConfigureAwait(false); + } + + public async Task FetchUriAsync(string endpointId, Uri uri, string input = "", CancellationToken cancellationToken = default) + { + var cacheKey = $"{endpointId}|{uri}|{input.Trim()}"; + if (_cache.TryGetValue(cacheKey, out var cached) && DateTimeOffset.Now - cached.fetchedAt < _cacheDuration) + { + return cached.response; + } + + var endpoint = ApiEndpoints.TryGet(endpointId, out var knownEndpoint) ? knownEndpoint : null; + var endpointName = endpoint?.Name ?? endpointId; + try + { + var result = await httpService.SendAsync( + uri, + ensureSuccess: false, + policy: ResolvePolicy(endpointId), + cancellationToken: cancellationToken).ConfigureAwait(false); + if ((int)result.StatusCode < 200 || (int)result.StatusCode >= 300) + { + return new ApiResponse( + endpointId, + uri, + false, + result.Content, + $"HTTP {(int)result.StatusCode} {result.StatusCode}", + DateTimeOffset.Now, + (int)result.StatusCode); + } + + var response = new ApiResponse(endpointId, uri, true, result.Content, null, DateTimeOffset.Now, (int)result.StatusCode); + _cache[cacheKey] = (DateTimeOffset.Now, response); + return response; + } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + return new ApiResponse(endpointId, uri, false, string.Empty, "Request canceled.", DateTimeOffset.Now); + } + catch (HttpRequestTimeoutException exception) + { + var error = FriendlyTimeoutMessage(exception.Timeout); + await WriteFailureLogAsync(endpointName, endpoint, error, cancellationToken).ConfigureAwait(false); + return new ApiResponse(endpointId, uri, false, string.Empty, error, DateTimeOffset.Now); + } + catch (TimeoutException) + { + var error = FriendlyTimeoutMessage(ResolvePolicy(endpointId).Timeout); + await WriteFailureLogAsync(endpointName, endpoint, error, cancellationToken).ConfigureAwait(false); + return new ApiResponse(endpointId, uri, false, string.Empty, error, DateTimeOffset.Now); + } + catch (HttpRequestException exception) + { + const string error = "Network request failed. Check the network connection or proxy settings, then retry."; + var detail = endpoint is { ShouldHideEndpoint: true } ? "Sensitive endpoint hidden." : SensitiveText.Sanitize(exception.Message); + await (logService?.WriteAsync("Error", "api", $"{endpointName} request failed", detail, cancellationToken) ?? Task.CompletedTask) + .ConfigureAwait(false); + return new ApiResponse(endpointId, uri, false, string.Empty, error, DateTimeOffset.Now); + } + catch (Exception exception) + { + var error = endpoint is { ShouldHideEndpoint: true } + ? "Remote data is temporarily unavailable. Please retry later." + : SensitiveText.Sanitize(exception.Message); + await WriteFailureLogAsync(endpointName, endpoint, error, cancellationToken).ConfigureAwait(false); + return new ApiResponse(endpointId, uri, false, string.Empty, error, DateTimeOffset.Now); + } + } + + public async Task CheckHealthAsync(string endpointId, string input = "", CancellationToken cancellationToken = default) + { + var response = await FetchAsync(endpointId, input, cancellationToken).ConfigureAwait(false); + if (!response.Success) + { + return ApiHealthStatus.Unhealthy; + } + + return string.IsNullOrWhiteSpace(response.Content) + ? ApiHealthStatus.Degraded + : ApiHealthStatus.Healthy; + } + + public static ApiManager CreateDefault() + { + return new ApiManager(new HttpService()); + } + + private static HttpRequestPolicy ResolvePolicy(string endpointId) + { + return endpointId.Equals("http_diagnostic", StringComparison.OrdinalIgnoreCase) || + endpointId.Contains("diagnostic", StringComparison.OrdinalIgnoreCase) + ? HttpRequestPolicy.Diagnostics + : HttpRequestPolicy.RemoteTool; + } + + private static string FriendlyTimeoutMessage(TimeSpan timeout) + => $"Remote service response timed out after {timeout.TotalSeconds:0} seconds. Please retry later or check your network/proxy settings."; + + private Task WriteFailureLogAsync(string endpointName, ApiEndpoint? endpoint, string error, CancellationToken cancellationToken) + { + var detail = endpoint is { ShouldHideEndpoint: true } ? "Sensitive endpoint hidden." : SensitiveText.Sanitize(error); + return logService?.WriteAsync("Error", "api", $"{endpointName} request failed", detail, cancellationToken) ?? Task.CompletedTask; + } +} diff --git a/src/YMhut.Box.Core/App/AppDatabasePaths.cs b/src/YMhut.Box.Core/App/AppDatabasePaths.cs new file mode 100644 index 0000000..6edb0c0 --- /dev/null +++ b/src/YMhut.Box.Core/App/AppDatabasePaths.cs @@ -0,0 +1,9 @@ +namespace YMhut.Box.Core.App; + +public static class AppDatabasePaths +{ + public const string MainDatabaseFileName = "app-log.db"; + + public static string ResolveMainDatabasePath(AppPaths paths) + => Path.Combine(paths.Logs, MainDatabaseFileName); +} diff --git a/src/YMhut.Box.Core/App/AppPaths.cs b/src/YMhut.Box.Core/App/AppPaths.cs new file mode 100644 index 0000000..baa27ec --- /dev/null +++ b/src/YMhut.Box.Core/App/AppPaths.cs @@ -0,0 +1,151 @@ +namespace YMhut.Box.Core.App; + +public sealed class AppPaths +{ + public static IReadOnlyList RuntimePayloadDirectoryNames { get; } = + [ + "Runtime", + "runtime", + "Runtimes", + "runtimes" + ]; + + public static IReadOnlyList UserPayloadDirectoryNames { get; } = + [ + "Runtime", + "runtime", + "Runtimes", + "runtimes", + "Tools", + "Metadata" + ]; + + public AppPaths(string root, string? assetsRoot = null) + { + Root = root; + Logs = Path.Combine(root, "Logs"); + Cache = Path.Combine(root, "Cache"); + Data = Path.Combine(root, "Data"); + Assets = assetsRoot ?? Path.Combine(AppContext.BaseDirectory, "Assets"); + } + + public string Root { get; } + + public string Logs { get; } + + public string Cache { get; } + + public string Data { get; } + + public string Assets { get; } + + public void EnsureCreated() + { + Directory.CreateDirectory(Root); + Directory.CreateDirectory(Logs); + Directory.CreateDirectory(Cache); + Directory.CreateDirectory(Data); + CleanupUserPayloadDirectories(Root); + } + + public IReadOnlyList CleanupRuntimePayloadDirectories() + => CleanupRuntimePayloadDirectories(Root); + + public static IReadOnlyList CleanupRuntimePayloadDirectories(string root) + => CleanupPayloadDirectories(root, RuntimePayloadDirectoryNames); + + public IReadOnlyList CleanupUserPayloadDirectories() + => CleanupUserPayloadDirectories(Root); + + public static IReadOnlyList CleanupUserPayloadDirectories(string root) + => CleanupPayloadDirectories(root, UserPayloadDirectoryNames); + + private static IReadOnlyList CleanupPayloadDirectories(string root, IEnumerable names) + { + if (string.IsNullOrWhiteSpace(root)) + { + return []; + } + + var removed = new List(); + string rootFullPath; + try + { + rootFullPath = Path.GetFullPath(root).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + } + catch + { + return removed; + } + + foreach (var name in names) + { + var candidate = Path.Combine(rootFullPath, name); + if (!IsChildPath(rootFullPath, candidate)) + { + continue; + } + + if (TryDeleteDirectory(candidate)) + { + removed.Add(candidate); + } + } + + return removed; + } + + public static AppPaths ForCurrentUser(string? root = null, string? assetsRoot = null) + { + var appRoot = root ?? Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), + "YMhut Box", + "WinUI"); + var paths = new AppPaths(appRoot, assetsRoot); + paths.EnsureCreated(); + return paths; + } + + private static bool TryDeleteDirectory(string directory) + { + try + { + if (!Directory.Exists(directory)) + { + return false; + } + + NormalizeAttributes(directory); + Directory.Delete(directory, recursive: true); + return true; + } + catch + { + return false; + } + } + + private static void NormalizeAttributes(string directory) + { + foreach (var path in Directory.EnumerateFileSystemEntries(directory, "*", SearchOption.AllDirectories)) + { + File.SetAttributes(path, FileAttributes.Normal); + } + + File.SetAttributes(directory, FileAttributes.Normal); + } + + private static bool IsChildPath(string rootFullPath, string candidate) + { + try + { + var childFullPath = Path.GetFullPath(candidate).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + return childFullPath.StartsWith(rootFullPath + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase) || + childFullPath.StartsWith(rootFullPath + Path.AltDirectorySeparatorChar, StringComparison.OrdinalIgnoreCase); + } + catch + { + return false; + } + } +} diff --git a/src/YMhut.Box.Core/App/IndependentWindowHostOptions.cs b/src/YMhut.Box.Core/App/IndependentWindowHostOptions.cs new file mode 100644 index 0000000..92ce3dd --- /dev/null +++ b/src/YMhut.Box.Core/App/IndependentWindowHostOptions.cs @@ -0,0 +1,120 @@ +using System.Collections.ObjectModel; +using System.Diagnostics.CodeAnalysis; + +namespace YMhut.Box.Core.App; + +public enum IndependentWindowKind +{ + Browser, + Media +} + +public sealed record IndependentWindowHostOptions( + IndependentWindowKind Kind, + string Title, + string InitialUrl = "", + string Root = "", + string AssetsRoot = "") +{ + public const string HostSwitch = "--ymhut-window-host"; + public const string KindSwitch = "--kind"; + public const string TitleSwitch = "--title"; + public const string UrlSwitch = "--url"; + public const string RootSwitch = "--root"; + public const string AssetsRootSwitch = "--assets-root"; + + public static IndependentWindowHostOptions Browser(string title, string initialUrl, AppPaths paths) + => new(IndependentWindowKind.Browser, title, initialUrl, paths.Root, paths.Assets); + + public static IndependentWindowHostOptions Media(string title, AppPaths paths) + => new(IndependentWindowKind.Media, title, string.Empty, paths.Root, paths.Assets); + + public IReadOnlyList ToArguments() + { + var args = new List + { + HostSwitch, + KindSwitch, + Kind.ToString().ToLowerInvariant(), + TitleSwitch, + Title + }; + + AddOptional(args, UrlSwitch, InitialUrl); + AddOptional(args, RootSwitch, Root); + AddOptional(args, AssetsRootSwitch, AssetsRoot); + return new ReadOnlyCollection(args); + } + + public static bool TryParse(IEnumerable args, [NotNullWhen(true)] out IndependentWindowHostOptions? options) + { + options = null; + var tokens = args.Where(token => !string.IsNullOrWhiteSpace(token)).ToArray(); + if (!tokens.Any(token => string.Equals(token, HostSwitch, StringComparison.OrdinalIgnoreCase))) + { + return false; + } + + var map = new Dictionary(StringComparer.OrdinalIgnoreCase); + for (var index = 0; index < tokens.Length; index++) + { + var token = tokens[index]; + if (!token.StartsWith("--", StringComparison.Ordinal) || string.Equals(token, HostSwitch, StringComparison.OrdinalIgnoreCase)) + { + continue; + } + + if (index + 1 < tokens.Length && !tokens[index + 1].StartsWith("--", StringComparison.Ordinal)) + { + map[token] = tokens[++index]; + } + } + + var kindText = Value(map, KindSwitch); + if (!TryParseKind(kindText, out var kind)) + { + return false; + } + + var title = Value(map, TitleSwitch); + if (string.IsNullOrWhiteSpace(title)) + { + title = kind == IndependentWindowKind.Browser ? "Safe Browser" : "Media Player"; + } + + options = new IndependentWindowHostOptions( + kind, + title, + Value(map, UrlSwitch), + Value(map, RootSwitch), + Value(map, AssetsRootSwitch)); + return true; + } + + private static bool TryParseKind(string value, out IndependentWindowKind kind) + { + var normalized = value.Trim().ToLowerInvariant(); + kind = normalized switch + { + "browser" or "safe_browser" or "safe-browser" => IndependentWindowKind.Browser, + "media" or "media_player" or "media-player" => IndependentWindowKind.Media, + _ => default + }; + + return normalized is "browser" or "safe_browser" or "safe-browser" or "media" or "media_player" or "media-player"; + } + + private static string Value(IReadOnlyDictionary map, string key) + => map.TryGetValue(key, out var value) ? value : string.Empty; + + private static void AddOptional(ICollection args, string key, string value) + { + if (string.IsNullOrWhiteSpace(value)) + { + return; + } + + args.Add(key); + args.Add(value); + } +} diff --git a/src/YMhut.Box.Core/App/InstallLayoutPaths.cs b/src/YMhut.Box.Core/App/InstallLayoutPaths.cs new file mode 100644 index 0000000..fbef4ab --- /dev/null +++ b/src/YMhut.Box.Core/App/InstallLayoutPaths.cs @@ -0,0 +1,160 @@ +namespace YMhut.Box.Core.App; + +public static class InstallLayoutPaths +{ + public const string InstallRootEnvironmentVariable = "YMHUT_BOX_INSTALL_ROOT"; + public const string ArchivedLayoutEnvironmentVariable = "YMHUT_BOX_ARCHIVED_LAYOUT"; + + public static string ResolveInstallRoot(string? baseDirectory = null) + { + return ResolveInstallRootContext(baseDirectory).InstallRoot; + } + + public static InstallRootContext ResolveInstallRootContext(string? baseDirectory = null) + { + var selected = SelectInstallRoot(baseDirectory); + var installRoot = selected.Path; + var assetsRoot = Path.Combine(installRoot, "Assets"); + if (!Directory.Exists(assetsRoot)) + { + assetsRoot = CandidateRoots(baseDirectory) + .Select(candidate => Path.Combine(candidate, "Assets")) + .FirstOrDefault(Directory.Exists) + ?? Path.Combine(installRoot, "Assets"); + } + + var manifestPath = Path.Combine(installRoot, InstallManifest.RelativePath.Replace('/', Path.DirectorySeparatorChar)); + var manifestIdentity = InstallRootContext.BuildManifestIdentity(installRoot); + return new InstallRootContext( + installRoot, + assetsRoot, + manifestPath, + manifestIdentity, + IsPackagedInstallPath(installRoot), + selected.Source); + } + + public static string ResolveInstalledExecutablePath(string? baseDirectory = null) + { + var processPath = Environment.ProcessPath; + if (!string.IsNullOrWhiteSpace(processPath) && + string.Equals(Path.GetFileName(processPath), "YMhutBox.exe", StringComparison.OrdinalIgnoreCase) && + File.Exists(processPath)) + { + return processPath; + } + + foreach (var root in CandidateRoots(baseDirectory)) + { + var executable = Path.Combine(root, "YMhutBox.exe"); + if (File.Exists(executable)) + { + return executable; + } + } + + return Environment.ProcessPath ?? Path.Combine(Path.GetFullPath(baseDirectory ?? AppContext.BaseDirectory), "YMhutBox.exe"); + } + + public static string ResolveAssetsRoot(string? baseDirectory = null) + { + return ResolveInstallRootContext(baseDirectory).AssetsRoot; + } + + public static string? ResolveArchivedLayoutRoot() + => NormalizeCandidate(Environment.GetEnvironmentVariable(ArchivedLayoutEnvironmentVariable)); + + public static IEnumerable CandidateRoots(string? baseDirectory = null) + { + var seen = new HashSet(StringComparer.OrdinalIgnoreCase); + foreach (var (candidate, _) in RawCandidates(baseDirectory)) + { + var fullPath = NormalizeCandidate(candidate); + if (fullPath is not null && seen.Add(fullPath)) + { + yield return fullPath; + } + } + } + + private static (string Path, string Source) SelectInstallRoot(string? baseDirectory) + { + var candidates = RawCandidates(baseDirectory) + .Select(candidate => (Path: NormalizeCandidate(candidate.Path), candidate.Source)) + .Where(candidate => candidate.Path is not null) + .Select(candidate => (Path: candidate.Path!, candidate.Source)) + .ToArray(); + + foreach (var candidate in candidates) + { + if (LooksLikeInstallRoot(candidate.Path)) + { + return candidate; + } + } + + var explicitRoot = NormalizeCandidate(Environment.GetEnvironmentVariable(InstallRootEnvironmentVariable)); + if (explicitRoot is not null && Directory.Exists(explicitRoot)) + { + return (explicitRoot, "install-root-env"); + } + + var firstExisting = candidates.FirstOrDefault(candidate => Directory.Exists(candidate.Path)); + if (!string.IsNullOrWhiteSpace(firstExisting.Path)) + { + return firstExisting; + } + + return (Path.GetFullPath(baseDirectory ?? AppContext.BaseDirectory), "fallback"); + } + + private static IEnumerable<(string? Path, string Source)> RawCandidates(string? baseDirectory) + { + var processPath = Environment.ProcessPath; + if (!string.IsNullOrWhiteSpace(processPath) && + string.Equals(Path.GetFileName(processPath), "YMhutBox.exe", StringComparison.OrdinalIgnoreCase)) + { + yield return (Path.GetDirectoryName(processPath), "process"); + } + + yield return (Environment.GetEnvironmentVariable(InstallRootEnvironmentVariable), "install-root-env"); + yield return (baseDirectory, "base-directory"); + yield return (AppContext.BaseDirectory, "app-context"); + } + + private static string? NormalizeCandidate(string? candidate) + { + if (string.IsNullOrWhiteSpace(candidate)) + { + return null; + } + + try + { + return Path.GetFullPath(candidate.Trim()) + .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + } + catch + { + return null; + } + } + + private static bool LooksLikeInstallRoot(string root) + { + if (!Directory.Exists(root)) + { + return false; + } + + return File.Exists(Path.Combine(root, "YMhutBox.exe")) || + File.Exists(Path.Combine(root, "YMhutBox.dll")) || + File.Exists(Path.Combine(root, InstallManifest.RelativePath.Replace('/', Path.DirectorySeparatorChar))); + } + + private static bool IsPackagedInstallPath(string root) + { + var normalized = root.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); + return normalized.Contains($"{Path.DirectorySeparatorChar}WindowsApps{Path.DirectorySeparatorChar}", StringComparison.OrdinalIgnoreCase); + } +} diff --git a/src/YMhut.Box.Core/App/InstallManifest.cs b/src/YMhut.Box.Core/App/InstallManifest.cs new file mode 100644 index 0000000..6397ac6 --- /dev/null +++ b/src/YMhut.Box.Core/App/InstallManifest.cs @@ -0,0 +1,94 @@ +namespace YMhut.Box.Core.App; + +public sealed record InstallManifestRelease( + string Version, + string Build, + string Channel, + string PackageVersion); + +public sealed record InstallManifest( + InstallManifestRelease? Release, + IReadOnlyList RequiredFiles, + IReadOnlyList Files) +{ + public const string RelativePath = "config/install-manifest.ini"; + + public static InstallManifest Parse(string text) + { + var release = new Dictionary(StringComparer.OrdinalIgnoreCase); + var requiredFiles = new List(); + var files = new List(); + var section = string.Empty; + + using var reader = new StringReader(text); + while (reader.ReadLine() is { } line) + { + var value = line.Trim(); + if (string.IsNullOrWhiteSpace(value) || value.StartsWith(';') || value.StartsWith('#')) + { + continue; + } + + if (value.StartsWith('[') && value.EndsWith(']')) + { + section = value[1..^1].Trim(); + continue; + } + + if (section.Equals("Release", StringComparison.OrdinalIgnoreCase)) + { + var separator = value.IndexOf('='); + if (separator <= 0) + { + continue; + } + + release[value[..separator].Trim()] = value[(separator + 1)..].Trim(); + continue; + } + + if (section.Equals("RequiredFiles", StringComparison.OrdinalIgnoreCase)) + { + requiredFiles.Add(NormalizeManifestPath(value)); + continue; + } + + if (section.Equals("Files", StringComparison.OrdinalIgnoreCase)) + { + files.Add(NormalizeManifestPath(value)); + } + } + + InstallManifestRelease? releaseInfo = null; + if (release.Count > 0) + { + releaseInfo = new InstallManifestRelease( + GetReleaseValue(release, "Version"), + GetReleaseValue(release, "Build"), + GetReleaseValue(release, "Channel"), + GetReleaseValue(release, "PackageVersion")); + } + + return new InstallManifest(releaseInfo, requiredFiles, files); + } + + public static async Task ReadAsync(string installRoot, CancellationToken cancellationToken = default) + { + var path = Path.Combine(installRoot, RelativePath.Replace('/', Path.DirectorySeparatorChar)); + var text = await File.ReadAllTextAsync(path, cancellationToken).ConfigureAwait(false); + return Parse(text); + } + + public bool ContainsFile(string relativePath) + { + var normalized = NormalizeManifestPath(relativePath); + return Files.Contains(normalized, StringComparer.OrdinalIgnoreCase) || + RequiredFiles.Contains(normalized, StringComparer.OrdinalIgnoreCase); + } + + private static string GetReleaseValue(IReadOnlyDictionary release, string key) + => release.TryGetValue(key, out var value) ? value : string.Empty; + + private static string NormalizeManifestPath(string path) + => path.Trim().Replace('\\', '/').TrimStart('/'); +} diff --git a/src/YMhut.Box.Core/App/InstallRootContext.cs b/src/YMhut.Box.Core/App/InstallRootContext.cs new file mode 100644 index 0000000..9e638cb --- /dev/null +++ b/src/YMhut.Box.Core/App/InstallRootContext.cs @@ -0,0 +1,54 @@ +using System.Globalization; + +namespace YMhut.Box.Core.App; + +public sealed record InstallRootContext( + string InstallRoot, + string AssetsRoot, + string ManifestPath, + string ManifestIdentity, + bool IsPackaged, + string ResolvedFrom) +{ + public string InstallIdentity { get; init; } = BuildInstallIdentity(InstallRoot, ManifestIdentity); + + public string CheckBasis => $"installRoot={InstallRoot}; manifest={ManifestIdentity}; source={ResolvedFrom}"; + + public static string BuildInstallIdentity(string installRoot, string manifestIdentity) + => $"{NormalizePath(installRoot)}|{manifestIdentity}"; + + public static string BuildManifestIdentity(string installRoot) + { + var manifestPath = Path.Combine(installRoot, InstallManifest.RelativePath.Replace('/', Path.DirectorySeparatorChar)); + if (!File.Exists(manifestPath)) + { + return $"no-manifest:{NormalizePath(installRoot)}"; + } + + try + { + var info = new FileInfo(manifestPath); + var manifest = InstallManifest.Parse(File.ReadAllText(manifestPath)); + var release = manifest.Release is null + ? "unknown" + : $"{manifest.Release.Version}/{manifest.Release.Build}/{manifest.Release.Channel}/{manifest.Release.PackageVersion}"; + return $"{release}:{info.Length}:{info.LastWriteTimeUtc.Ticks.ToString(CultureInfo.InvariantCulture)}"; + } + catch + { + return $"invalid-manifest:{NormalizePath(manifestPath)}"; + } + } + + private static string NormalizePath(string path) + { + try + { + return Path.GetFullPath(path).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + } + catch + { + return path.Trim(); + } + } +} diff --git a/src/YMhut.Box.Core/App/OpenSourceReferenceService.cs b/src/YMhut.Box.Core/App/OpenSourceReferenceService.cs new file mode 100644 index 0000000..b615d53 --- /dev/null +++ b/src/YMhut.Box.Core/App/OpenSourceReferenceService.cs @@ -0,0 +1,315 @@ +using System.Xml.Linq; +using YMhut.Box.Core.Logging; + +namespace YMhut.Box.Core.App; + +public sealed record OpenSourceReferenceItem( + string Name, + string Kind, + string Version, + string Usage, + string Attribution, + string Path = ""); + +public interface IOpenSourceReferenceService +{ + Task> GetReferencesAsync(CancellationToken cancellationToken = default); +} + +public sealed class OpenSourceReferenceService(AppPaths paths, ILogService? logService = null) : IOpenSourceReferenceService +{ + private static readonly StringComparer PathComparer = StringComparer.OrdinalIgnoreCase; + + public async Task> GetReferencesAsync(CancellationToken cancellationToken = default) + { + var references = new List + { + new( + "YMhut Box", + "Application", + string.Empty, + "Primary desktop toolbox application and integration shell.", + "Copyright and branding belong to YMhut / YMhut Box."), + new( + "tubatool reference project", + "Reference project attribution", + string.Empty, + "Toolbox layout, built-in tool behavior, settings interaction, and bundled Tools directory were used as reference material with author permission.", + "Original reference project author attribution is retained here; migrated user-facing branding is YMhut Box."), + new( + ".NET", + "Runtime", + "net10.0", + "Application runtime, libraries, file IO, process, JSON, XML, and platform interop.", + "Microsoft and .NET contributors retain their original rights.") + }; + + try + { + AddPackageReferences(references); + AddBundledToolNotices(references, cancellationToken); + var toolNoticeCount = references.Count(item => item.Kind == "Third-party tool notice"); + await WriteLogAsync( + "Information", + "about", + "Open source references loaded", + $"count={references.Count}; toolNotices={toolNoticeCount}", + cancellationToken).ConfigureAwait(false); + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException or global::System.Xml.XmlException) + { + await WriteLogAsync( + "Warning", + "about", + "Open source reference scan degraded", + Sanitize(exception.Message), + cancellationToken).ConfigureAwait(false); + } + + return references + .OrderBy(item => ReferenceOrder(item.Kind)) + .ThenBy(item => item.Name, StringComparer.CurrentCultureIgnoreCase) + .ToArray(); + } + + private void AddPackageReferences(List references) + { + var packages = new Dictionary(StringComparer.OrdinalIgnoreCase); + foreach (var projectFile in FindProjectFiles()) + { + var document = XDocument.Load(projectFile); + foreach (var package in document.Descendants("PackageReference")) + { + var name = package.Attribute("Include")?.Value ?? package.Attribute("Update")?.Value; + if (string.IsNullOrWhiteSpace(name)) + { + continue; + } + + var version = package.Attribute("Version")?.Value + ?? package.Element("Version")?.Value + ?? string.Empty; + packages[name] = version; + } + } + + if (packages.Count == 0) + { + foreach (var package in StaticPackageFallback()) + { + packages[package.Name] = package.Version; + } + } + + foreach (var package in packages.OrderBy(item => item.Key, StringComparer.OrdinalIgnoreCase)) + { + references.Add(new OpenSourceReferenceItem( + package.Key, + "NuGet package", + package.Value, + PackageUsage(package.Key), + "Original package license and ownership remain with its maintainers.")); + } + } + + private void AddBundledToolNotices(List references, CancellationToken cancellationToken) + { + var toolsRoot = FindToolsRoot(); + if (string.IsNullOrWhiteSpace(toolsRoot) || !Directory.Exists(toolsRoot)) + { + references.Add(new OpenSourceReferenceItem( + "Bundled third-party Tools", + "Third-party tool notice", + string.Empty, + "Tools directory was not found in the current runtime layout.", + "Third-party tool declarations are preserved when the Tools directory is present.")); + return; + } + + var files = Directory.EnumerateFiles(toolsRoot, "*.*", SearchOption.AllDirectories) + .Where(IsNoticeFile) + .Take(120) + .ToArray(); + + foreach (var file in files) + { + cancellationToken.ThrowIfCancellationRequested(); + references.Add(new OpenSourceReferenceItem( + ToolNoticeName(toolsRoot, file), + "Third-party tool notice", + string.Empty, + "Bundled external tool notice. Open the original file for full license, EULA, README, or copyright text.", + "Original third-party declaration file is kept unchanged on disk.", + file)); + } + } + + private IEnumerable FindProjectFiles() + { + var seen = new HashSet(PathComparer); + foreach (var root in CandidateRoots()) + { + if (!Directory.Exists(root)) + { + continue; + } + + foreach (var project in Directory.EnumerateFiles(root, "*.csproj", SearchOption.TopDirectoryOnly).Take(12)) + { + if (seen.Add(project)) + { + yield return project; + } + } + + var src = Path.Combine(root, "src"); + if (!Directory.Exists(src)) + { + continue; + } + + foreach (var project in Directory.EnumerateFiles(src, "*.csproj", SearchOption.AllDirectories).Take(24)) + { + if (seen.Add(project)) + { + yield return project; + } + } + } + } + + private string FindToolsRoot() + { + foreach (var root in CandidateRoots()) + { + var direct = Path.Combine(root, "Tools"); + if (Directory.Exists(direct)) + { + return direct; + } + + foreach (var child in SafeEnumerateDirectories(root)) + { + if (!Path.GetFileName(child).Contains("tubatool", StringComparison.OrdinalIgnoreCase)) + { + continue; + } + + var tools = Path.Combine(child, "Tools"); + if (Directory.Exists(tools)) + { + return tools; + } + } + } + + return string.Empty; + } + + private IEnumerable CandidateRoots() + { + foreach (var root in InstallLayoutPaths.CandidateRoots()) + { + yield return root; + } + + yield return paths.Root; + yield return paths.Assets; + + foreach (var root in InstallLayoutPaths.CandidateRoots()) + { + var directory = new DirectoryInfo(root); + for (var depth = 0; depth < 8 && directory is not null; depth++, directory = directory.Parent) + { + yield return directory.FullName; + } + } + } + + private static IEnumerable SafeEnumerateDirectories(string root) + { + try + { + return Directory.Exists(root) ? Directory.EnumerateDirectories(root).Take(40).ToArray() : []; + } + catch + { + return []; + } + } + + private static bool IsNoticeFile(string path) + { + var name = Path.GetFileName(path); + return name.Contains("license", StringComparison.OrdinalIgnoreCase) + || name.Contains("readme", StringComparison.OrdinalIgnoreCase) + || name.Contains("eula", StringComparison.OrdinalIgnoreCase) + || name.Contains("copyright", StringComparison.OrdinalIgnoreCase) + || name.Contains("copying", StringComparison.OrdinalIgnoreCase) + || name.Contains("notice", StringComparison.OrdinalIgnoreCase); + } + + private static string ToolNoticeName(string toolsRoot, string file) + { + var relative = Path.GetRelativePath(toolsRoot, file); + var parts = relative.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + if (parts.Length >= 3) + { + return $"{parts[1]} / {parts[^1]}"; + } + + return relative; + } + + private static IEnumerable<(string Name, string Version)> StaticPackageFallback() + { + yield return ("Microsoft.Data.Sqlite", "10.0.0"); + yield return ("Microsoft.Extensions.DependencyInjection", "10.0.0"); + yield return ("Microsoft.Web.WebView2", "1.0.3967.48"); + yield return ("Microsoft.WindowsAppSDK", "1.8.260416003"); + yield return ("QRCoder", "1.8.0"); + yield return ("System.Drawing.Common", "10.0.0"); + yield return ("System.Management", "10.0.0"); + yield return ("ZXing.Net", "0.16.11"); + } + + private static string PackageUsage(string packageName) + { + return packageName switch + { + "Microsoft.WindowsAppSDK" => "WinUI 3 desktop shell, windows, controls, app lifecycle, and packaging support.", + "Microsoft.Web.WebView2" => "Embedded web content for browser, plugin, and preview surfaces.", + "Microsoft.Extensions.DependencyInjection" => "Application service registration and dependency injection.", + "Microsoft.Data.Sqlite" => "Local SQLite logging and app data persistence.", + "QRCoder" => "QR code generation tools.", + "ZXing.Net" => "Barcode and QR decoding helpers.", + "System.Drawing.Common" => "EXE and shortcut icon extraction and image conversion for external Tools.", + "System.Management" => "WMI hardware and system information queries.", + _ => "Application dependency used by YMhut Box." + }; + } + + private static int ReferenceOrder(string kind) + { + return kind switch + { + "Application" => 0, + "Reference project attribution" => 1, + "Runtime" => 2, + "NuGet package" => 3, + _ => 4 + }; + } + + private static string Sanitize(string value) + { + return value + .Replace(Environment.UserName, "", StringComparison.OrdinalIgnoreCase) + .Replace(AppContext.BaseDirectory, "", StringComparison.OrdinalIgnoreCase); + } + + private Task WriteLogAsync(string level, string category, string message, string? detail, CancellationToken cancellationToken) + { + return logService?.WriteAsync(level, category, message, detail, cancellationToken) ?? Task.CompletedTask; + } +} diff --git a/src/YMhut.Box.Core/App/RuntimeLayoutPolicy.cs b/src/YMhut.Box.Core/App/RuntimeLayoutPolicy.cs new file mode 100644 index 0000000..9a1f460 --- /dev/null +++ b/src/YMhut.Box.Core/App/RuntimeLayoutPolicy.cs @@ -0,0 +1,50 @@ +namespace YMhut.Box.Core.App; + +public static class RuntimeLayoutPolicy +{ + public const string ReadyMarker = ".ymhut-runtime-layout"; + + private static readonly string[] ExcludedRootDirectories = + [ + "Tools", + "Metadata", + "data", + "FeedbackPackages", + "Assets", + "config", + "download-host", + "plugin-host", + "prereqs", + "updater", + "worker" + ]; + + public static bool ShouldSkipRelativePath(string relativePath) + { + var normalized = relativePath.Replace('\\', '/').Trim('/'); + var firstSegment = normalized.Split('/', 2, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(); + return firstSegment is not null && + ExcludedRootDirectories.Any(excluded => string.Equals(firstSegment, excluded, StringComparison.OrdinalIgnoreCase)); + } + + public static bool ShouldCopyFile(string relativePath) + { + return !ShouldSkipRelativePath(relativePath) && !ShouldSkipFile(relativePath); + } + + public static bool ShouldSkipFile(string relativePath) + { + var normalized = relativePath.Replace('\\', '/').Trim('/'); + var fileName = Path.GetFileName(relativePath); + return fileName.Equals(ReadyMarker, StringComparison.OrdinalIgnoreCase) || + fileName.StartsWith("unins", StringComparison.OrdinalIgnoreCase) || + fileName.Equals("YMhutBox.exe", StringComparison.OrdinalIgnoreCase) || + fileName.Equals("YMhutBox.dll", StringComparison.OrdinalIgnoreCase) || + fileName.Equals("resources.pri", StringComparison.OrdinalIgnoreCase) || + fileName.EndsWith(".mui", StringComparison.OrdinalIgnoreCase) || + fileName.EndsWith(".resources.dll", StringComparison.OrdinalIgnoreCase) || + normalized.StartsWith("lang/", StringComparison.OrdinalIgnoreCase) || + normalized.StartsWith("Microsoft.UI.Xaml/", StringComparison.OrdinalIgnoreCase) || + normalized.StartsWith("runtimes/", StringComparison.OrdinalIgnoreCase); + } +} diff --git a/src/YMhut.Box.Core/AssemblyInfo.cs b/src/YMhut.Box.Core/AssemblyInfo.cs new file mode 100644 index 0000000..712e6a9 --- /dev/null +++ b/src/YMhut.Box.Core/AssemblyInfo.cs @@ -0,0 +1,4 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("YMhut.Box.Tests")] +[assembly: InternalsVisibleTo("YMhutBox")] diff --git a/src/YMhut.Box.Core/Data/ReferenceDataService.cs b/src/YMhut.Box.Core/Data/ReferenceDataService.cs new file mode 100644 index 0000000..1ac69a6 --- /dev/null +++ b/src/YMhut.Box.Core/Data/ReferenceDataService.cs @@ -0,0 +1,225 @@ +using System.Text.Json; +using System.IO.Compression; +using System.Security.Cryptography; +using System.Text; +using YMhut.Box.Core.App; + +namespace YMhut.Box.Core.Data; + +public sealed record ReferenceDataValidationResult( + IReadOnlyList PresentPaths, + IReadOnlyList MissingPaths) +{ + public bool IsHealthy => MissingPaths.Count == 0; +} + +public interface IReferenceDataService +{ + Task ReadTextAsync(string relativePath, CancellationToken cancellationToken = default); + + Task ReadJsonAsync(string relativePath, CancellationToken cancellationToken = default); + + bool Exists(string relativePath); + + IReadOnlyList EnumerateFiles(string relativeDirectory, string searchPattern = "*.*"); + + ReferenceDataValidationResult ValidateRequiredAssets(params string[] relativePaths); +} + +public sealed class ReferenceDataService(AppPaths paths) : IReferenceDataService +{ + private static readonly byte[] YbinMagic = Encoding.ASCII.GetBytes("YMHUTYBIN1"); + private static readonly byte[] YbinKey = SHA256.HashData(Encoding.UTF8.GetBytes("YMhut.Box.ReferenceData.YBin.v1")); + + public async Task ReadTextAsync(string relativePath, CancellationToken cancellationToken = default) + { + var fullPath = ResolveAssetPath(relativePath); + if (File.Exists(fullPath)) + { + return await File.ReadAllTextAsync(fullPath, cancellationToken).ConfigureAwait(false); + } + + return await ReadFromDatAsync(relativePath, cancellationToken).ConfigureAwait(false); + } + + public async Task ReadJsonAsync(string relativePath, CancellationToken cancellationToken = default) + { + var text = await ReadTextAsync(relativePath, cancellationToken).ConfigureAwait(false); + return string.IsNullOrWhiteSpace(text) + ? default + : JsonSerializer.Deserialize(text, new JsonSerializerOptions(JsonSerializerDefaults.Web)); + } + + public bool Exists(string relativePath) + { + return File.Exists(ResolveAssetPath(relativePath)) || DatContains(relativePath); + } + + public IReadOnlyList EnumerateFiles(string relativeDirectory, string searchPattern = "*.*") + { + var directory = ResolveAssetDirectory(relativeDirectory); + var physicalFiles = Directory.Exists(directory) + ? Directory.EnumerateFiles(directory, searchPattern, SearchOption.AllDirectories) + .Select(path => Path.GetRelativePath(directory, path)) + .Order(StringComparer.OrdinalIgnoreCase) + .ToArray() + : []; + var datFiles = EnumerateDatFiles(relativeDirectory, searchPattern); + return physicalFiles + .Concat(datFiles) + .Distinct(StringComparer.OrdinalIgnoreCase) + .Order(StringComparer.OrdinalIgnoreCase) + .ToArray(); + } + + public ReferenceDataValidationResult ValidateRequiredAssets(params string[] relativePaths) + { + var present = new List(); + var missing = new List(); + foreach (var path in relativePaths) + { + if (Exists(path)) + { + present.Add(path); + } + else + { + missing.Add(path); + } + } + + return new ReferenceDataValidationResult(present, missing); + } + + private string ResolveAssetPath(string relativePath) + { + var normalized = relativePath.Replace('/', Path.DirectorySeparatorChar).TrimStart(Path.DirectorySeparatorChar); + var assetPath = Path.Combine(paths.Assets, normalized); + if (File.Exists(assetPath)) + { + return assetPath; + } + + return Path.Combine(AppContext.BaseDirectory, normalized); + } + + private string ResolveAssetDirectory(string relativeDirectory) + { + var normalized = relativeDirectory.Replace('/', Path.DirectorySeparatorChar).TrimStart(Path.DirectorySeparatorChar); + var assetDirectory = Path.Combine(paths.Assets, normalized); + if (Directory.Exists(assetDirectory)) + { + return assetDirectory; + } + + return Path.Combine(AppContext.BaseDirectory, normalized); + } + + private async Task ReadFromDatAsync(string relativePath, CancellationToken cancellationToken) + { + var entryName = NormalizeEntryName(relativePath); + foreach (var package in DatCandidates()) + { + if (!File.Exists(package)) + { + continue; + } + + using var archive = OpenPackage(package); + var entry = archive.GetEntry(entryName); + if (entry is null) + { + continue; + } + + await using var stream = entry.Open(); + using var reader = new StreamReader(stream); + return await reader.ReadToEndAsync(cancellationToken).ConfigureAwait(false); + } + + return null; + } + + private bool DatContains(string relativePath) + { + var entryName = NormalizeEntryName(relativePath); + foreach (var package in DatCandidates()) + { + if (!File.Exists(package)) + { + continue; + } + + using var archive = OpenPackage(package); + if (archive.GetEntry(entryName) is not null) + { + return true; + } + } + + return false; + } + + private IReadOnlyList EnumerateDatFiles(string relativeDirectory, string searchPattern) + { + var prefix = NormalizeEntryName(relativeDirectory).TrimEnd('/') + "/"; + var extension = searchPattern.StartsWith("*.") ? searchPattern[1..] : string.Empty; + var results = new List(); + foreach (var package in DatCandidates()) + { + if (!File.Exists(package)) + { + continue; + } + + using var archive = OpenPackage(package); + results.AddRange(archive.Entries + .Where(entry => entry.FullName.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) + .Where(entry => string.IsNullOrWhiteSpace(extension) || entry.FullName.EndsWith(extension, StringComparison.OrdinalIgnoreCase)) + .Select(entry => entry.FullName[prefix.Length..])); + } + + return results; + } + + private IEnumerable DatCandidates() + { + yield return Path.Combine(paths.Assets, "data", "ymhut-data.ybin"); + yield return Path.Combine(AppContext.BaseDirectory, "Assets", "data", "ymhut-data.ybin"); + yield return Path.Combine(AppContext.BaseDirectory, "resources", "ymhut-data.ybin"); + yield return Path.Combine(paths.Assets, "data", "reference-data.dat"); + yield return Path.Combine(AppContext.BaseDirectory, "Assets", "data", "reference-data.dat"); + yield return Path.Combine(AppContext.BaseDirectory, "resources", "reference-data.dat"); + } + + private static string NormalizeEntryName(string relativePath) + { + return relativePath.Replace('\\', '/').TrimStart('/'); + } + + private static ZipArchive OpenPackage(string package) + { + if (package.EndsWith(".ybin", StringComparison.OrdinalIgnoreCase)) + { + var encrypted = File.ReadAllBytes(package); + if (encrypted.Length <= YbinMagic.Length + 16 || + !encrypted.AsSpan(0, YbinMagic.Length).SequenceEqual(YbinMagic)) + { + throw new InvalidDataException("Reference data package has an invalid header."); + } + + var iv = encrypted.AsSpan(YbinMagic.Length, 16).ToArray(); + var cipherText = encrypted.AsSpan(YbinMagic.Length + 16).ToArray(); + using var aes = Aes.Create(); + aes.Key = YbinKey; + aes.IV = iv; + aes.Mode = CipherMode.CBC; + aes.Padding = PaddingMode.PKCS7; + using var decryptor = aes.CreateDecryptor(); + var payload = decryptor.TransformFinalBlock(cipherText, 0, cipherText.Length); + return new ZipArchive(new MemoryStream(payload), ZipArchiveMode.Read, leaveOpen: false); + } + + return ZipFile.OpenRead(package); + } +} diff --git a/src/YMhut.Box.Core/DevEnvironments/DevEnvironmentModels.cs b/src/YMhut.Box.Core/DevEnvironments/DevEnvironmentModels.cs new file mode 100644 index 0000000..8c7dfe8 --- /dev/null +++ b/src/YMhut.Box.Core/DevEnvironments/DevEnvironmentModels.cs @@ -0,0 +1,727 @@ +using System.Diagnostics; +using System.Text.Json; +using System.Text.RegularExpressions; +using YMhut.Box.Core.App; +using YMhut.Box.Core.Downloads; +using YMhut.Box.Core.Logging; +using YMhut.Box.Core.Net; + +namespace YMhut.Box.Core.DevEnvironments; + +public enum DevEnvironmentInstallMode +{ + QuickInstall, + SourceBuild +} + +public sealed record DevEnvironmentDefinition( + string Id, + string Name, + string CommandName, + string VersionArgument, + string OfficialHome, + string VersionSource, + string WindowsAssetHint, + string SourceAssetHint = "source") +{ + public static IReadOnlyList Defaults { get; } = + [ + new("go", "Go", "go", "version", "https://go.dev/dl/", "https://go.dev/dl/?mode=json", "windows-amd64.msi", "src.tar.gz"), + new("python", "Python", "python", "--version", "https://www.python.org/downloads/windows/", "https://www.python.org/ftp/python/", "amd64.exe", "tgz"), + new("java", "Java / Temurin JDK", "java", "-version", "https://adoptium.net/temurin/releases/", "https://api.adoptium.net/v3/assets/latest/21/hotspot?os=windows&architecture=x64&image_type=jdk", "jdk_x64_windows_hotspot", "sources"), + new("docker", "Docker Desktop", "docker", "--version", "https://www.docker.com/products/docker-desktop/", "https://desktop.docker.com/win/main/amd64/appcast.xml", "Docker Desktop Installer.exe"), + new("mysql", "MySQL", "mysql", "--version", "https://dev.mysql.com/downloads/mysql/", "https://dev.mysql.com/downloads/mysql/", "mysql-installer"), + new("node", "Node.js", "node", "--version", "https://nodejs.org/en/download", "https://nodejs.org/dist/index.json", "win-x64.msi", "tar.gz"), + new("dotnet", ".NET SDK", "dotnet", "--list-sdks", "https://dotnet.microsoft.com/download", "https://dotnetcli.blob.core.windows.net/dotnet/release-metadata/releases-index.json", "win-x64.exe", "source"), + new("git", "Git", "git", "--version", "https://git-scm.com/download/win", "https://api.github.com/repos/git-for-windows/git/releases", "64-bit.exe", "tar.gz"), + new("rust", "Rust", "rustc", "--version", "https://www.rust-lang.org/tools/install", "https://static.rust-lang.org/dist/channel-rust-stable.toml", "rustup-init.exe", "tar.gz"), + new("cmake", "CMake", "cmake", "--version", "https://cmake.org/download/", "https://api.github.com/repos/Kitware/CMake/releases", "windows-x86_64.msi", "tar.gz") + ]; +} + +public sealed record DetectedDevEnvironment( + string Id, + string Name, + bool IsInstalled, + string Version = "", + string Path = "", + string Source = "", + string Error = ""); + +public sealed record DevEnvironmentDownloadCandidate( + string Id, + string DisplayName, + DownloadSource Source, + DevEnvironmentInstallMode Mode, + string SourceType = "Official", + string Region = "", + bool IsDefault = false); + +public sealed record DevEnvironmentVersion( + string Version, + DownloadSource? Installer, + DownloadSource? SourceArchive, + DateTimeOffset? PublishedAt = null, + string ReleaseNotesUrl = "", + IReadOnlyList? Candidates = null) +{ + public IReadOnlyList AllCandidates + { + get + { + if (Candidates is { Count: > 0 }) + { + return Candidates; + } + + var list = new List(); + if (Installer is not null) + { + list.Add(new DevEnvironmentDownloadCandidate("installer", Installer.DisplayName, Installer, DevEnvironmentInstallMode.QuickInstall, Installer.SourceKind, Installer.MirrorRegion, true)); + } + if (SourceArchive is not null) + { + list.Add(new DevEnvironmentDownloadCandidate("source", SourceArchive.DisplayName, SourceArchive, DevEnvironmentInstallMode.SourceBuild, SourceArchive.SourceKind, SourceArchive.MirrorRegion)); + } + + return list; + } + } +} + +public sealed record DevEnvironmentBuildRecipe( + string EnvironmentId, + string Summary, + IReadOnlyList Prerequisites, + IReadOnlyList Commands, + bool IsSupported = true); + +public sealed record DevEnvironmentBuildSession( + string Id, + string EnvironmentId, + string WorkingDirectory, + string SourceArchivePath, + DevEnvironmentBuildRecipe Recipe, + DateTimeOffset CreatedAt); + +public sealed record DevEnvironmentInstallPlan( + string EnvironmentId, + string EnvironmentName, + DevEnvironmentVersion Version, + DevEnvironmentInstallMode Mode, + string BuildRecipe = "", + string InstallCommand = "", + string InstallArguments = ""); + +public interface IDevEnvironmentDetectionService +{ + Task> DetectAsync(CancellationToken cancellationToken = default); +} + +public interface IDevEnvironmentCatalogService +{ + IReadOnlyList Definitions { get; } + + Task> GetVersionsAsync(string environmentId, CancellationToken cancellationToken = default); + + DevEnvironmentInstallPlan CreateInstallPlan(string environmentId, DevEnvironmentVersion version, DevEnvironmentInstallMode mode); + + DevEnvironmentInstallPlan CreateInstallPlan(string environmentId, DevEnvironmentVersion version, DevEnvironmentDownloadCandidate candidate); +} + +public sealed class DevEnvironmentDetectionService(ILogService? logService = null) : IDevEnvironmentDetectionService +{ + public async Task> DetectAsync(CancellationToken cancellationToken = default) + { + var tasks = DevEnvironmentDefinition.Defaults.Select(definition => DetectOneAsync(definition, cancellationToken)); + return await Task.WhenAll(tasks).ConfigureAwait(false); + } + + private async Task DetectOneAsync(DevEnvironmentDefinition definition, CancellationToken cancellationToken) + { + try + { + var result = await RunProcessAsync(definition.CommandName, definition.VersionArgument, cancellationToken).ConfigureAwait(false); + if (result.ExitCode != 0 && string.IsNullOrWhiteSpace(result.Output)) + { + return new DetectedDevEnvironment(definition.Id, definition.Name, false, Error: result.Error); + } + + var version = DevEnvironmentVersionParser.Parse(definition.Id, result.Output); + var path = await ResolveCommandPathAsync(definition.CommandName, cancellationToken).ConfigureAwait(false); + return new DetectedDevEnvironment(definition.Id, definition.Name, true, version, path, "PATH"); + } + catch (OperationCanceledException) + { + throw; + } + catch (Exception exception) + { + await (logService?.WriteAsync("Debug", "dev-env", $"Detect {definition.Id} failed", exception.Message, cancellationToken) ?? Task.CompletedTask) + .ConfigureAwait(false); + return new DetectedDevEnvironment(definition.Id, definition.Name, false, Error: exception.Message); + } + } + + private static async Task<(int ExitCode, string Output, string Error)> RunProcessAsync(string fileName, string arguments, CancellationToken cancellationToken) + { + using var process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = fileName, + Arguments = arguments, + UseShellExecute = false, + CreateNoWindow = true, + RedirectStandardOutput = true, + RedirectStandardError = true + }, + EnableRaisingEvents = true + }; + + process.Start(); + var outputTask = process.StandardOutput.ReadToEndAsync(cancellationToken); + var errorTask = process.StandardError.ReadToEndAsync(cancellationToken); + await process.WaitForExitAsync(cancellationToken).ConfigureAwait(false); + var output = (await outputTask.ConfigureAwait(false) + Environment.NewLine + await errorTask.ConfigureAwait(false)).Trim(); + return (process.ExitCode, output, string.Empty); + } + + private static async Task ResolveCommandPathAsync(string command, CancellationToken cancellationToken) + { + try + { + var result = await RunProcessAsync("where.exe", command, cancellationToken).ConfigureAwait(false); + return result.Output + .Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .FirstOrDefault() ?? string.Empty; + } + catch + { + return string.Empty; + } + } +} + +public static partial class DevEnvironmentVersionParser +{ + public static string Parse(string id, string output) + { + if (string.IsNullOrWhiteSpace(output)) + { + return string.Empty; + } + + return id switch + { + "go" => Match(output, @"go(?:\s+version)?\s+go(?[0-9][^\s]+)"), + "python" => Match(output, @"Python\s+(?[0-9][^\s]+)"), + "java" => Match(output, @"(?:openjdk|java)\s+version\s+""(?[^""]+)""", RegexOptions.IgnoreCase), + "docker" => Match(output, @"Docker version\s+(?[0-9][^,\s]+)"), + "mysql" => Match(output, @"Distrib\s+(?[0-9][^,\s]+)|Ver\s+(?[0-9][^\s]+)"), + "node" => Match(output, @"v(?[0-9][^\s]+)"), + "dotnet" => output.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).FirstOrDefault() ?? string.Empty, + "git" => Match(output, @"git version\s+(?[0-9][^\s]+)"), + "rust" => Match(output, @"rustc\s+(?[0-9][^\s]+)"), + "cmake" => Match(output, @"cmake version\s+(?[0-9][^\s]+)"), + _ => output.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).FirstOrDefault() ?? string.Empty + }; + } + + private static string Match(string output, string pattern, RegexOptions options = RegexOptions.None) + { + var match = Regex.Match(output, pattern, options | RegexOptions.CultureInvariant); + if (!match.Success) + { + return output.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).FirstOrDefault() ?? string.Empty; + } + + return FirstGroup(match, "v", "v2"); + } + + private static string FirstGroup(Match match, params string[] names) + { + foreach (var name in names) + { + if (match.Groups[name] is { Success: true } group) + { + return group.Value; + } + } + + return string.Empty; + } +} + +public sealed class DevEnvironmentCatalogService( + AppPaths paths, + IHttpService? httpService = null, + ILogService? logService = null) : IDevEnvironmentCatalogService +{ + public IReadOnlyList Definitions => DevEnvironmentDefinition.Defaults; + + public async Task> GetVersionsAsync(string environmentId, CancellationToken cancellationToken = default) + { + var definition = Definitions.FirstOrDefault(item => string.Equals(item.Id, environmentId, StringComparison.OrdinalIgnoreCase)); + if (definition is null) + { + return []; + } + + var cached = await TryReadCacheAsync(environmentId, cancellationToken).ConfigureAwait(false); + if (cached.Count > 0) + { + return cached; + } + + try + { + var versions = await FetchVersionsAsync(definition, cancellationToken).ConfigureAwait(false); + if (versions.Count > 0) + { + await WriteCacheAsync(environmentId, versions, cancellationToken).ConfigureAwait(false); + return versions; + } + } + catch (Exception exception) + { + await (logService?.WriteAsync("Warning", "dev-env", $"Fetch versions failed: {environmentId}", exception.Message, cancellationToken) ?? Task.CompletedTask) + .ConfigureAwait(false); + } + + return OfflineFallback(definition); + } + + public DevEnvironmentInstallPlan CreateInstallPlan(string environmentId, DevEnvironmentVersion version, DevEnvironmentInstallMode mode) + { + var candidate = version.AllCandidates.FirstOrDefault(item => item.Mode == mode) ?? + version.AllCandidates.FirstOrDefault(); + if (candidate is not null) + { + return CreateInstallPlan(environmentId, version, candidate); + } + + var source = mode == DevEnvironmentInstallMode.SourceBuild + ? version.SourceArchive ?? version.Installer + : version.Installer ?? version.SourceArchive; + candidate = source is null + ? new DevEnvironmentDownloadCandidate("manual", version.Version, new DownloadSource(string.Empty, version.Version, "download.url"), mode) + : new DevEnvironmentDownloadCandidate(mode == DevEnvironmentInstallMode.SourceBuild ? "source" : "installer", source.DisplayName, source, mode); + return CreateInstallPlan(environmentId, version, candidate); + } + + public DevEnvironmentInstallPlan CreateInstallPlan(string environmentId, DevEnvironmentVersion version, DevEnvironmentDownloadCandidate candidate) + { + var definition = Definitions.FirstOrDefault(item => string.Equals(item.Id, environmentId, StringComparison.OrdinalIgnoreCase)) + ?? new DevEnvironmentDefinition(environmentId, environmentId, environmentId, "--version", string.Empty, string.Empty, string.Empty); + + if (candidate.Mode == DevEnvironmentInstallMode.SourceBuild) + { + var recipe = BuildRecipe(definition); + return new DevEnvironmentInstallPlan( + definition.Id, + definition.Name, + version, + candidate.Mode, + string.Join(Environment.NewLine, recipe.Commands.Prepend(recipe.Summary)), + "cmd.exe", + "/k echo Build recipe is ready. Download the source archive first, then follow the listed commands."); + } + + return new DevEnvironmentInstallPlan( + definition.Id, + definition.Name, + version, + candidate.Mode, + InstallCommand: "installer", + InstallArguments: string.Empty); + } + + private async Task> FetchVersionsAsync(DevEnvironmentDefinition definition, CancellationToken cancellationToken) + { + if (httpService is null) + { + return OfflineFallback(definition); + } + + var response = await httpService.GetAsync(new Uri(definition.VersionSource), cancellationToken).ConfigureAwait(false); + return definition.Id switch + { + "go" => ParseGo(definition, response.Content), + "node" => ParseNode(definition, response.Content), + "dotnet" => ParseDotNet(definition, response.Content), + "git" => ParseGitHub(definition, response.Content, "Git for Windows", static name => + name.EndsWith("64-bit.exe", StringComparison.OrdinalIgnoreCase) || + name.Contains("64-bit.exe", StringComparison.OrdinalIgnoreCase)), + "cmake" => ParseGitHub(definition, response.Content, "CMake", static name => + name.Contains("windows-x86_64.msi", StringComparison.OrdinalIgnoreCase) || + name.Contains("windows-x86_64.zip", StringComparison.OrdinalIgnoreCase)), + "rust" => ParseRust(definition, response.Content), + "python" => ParsePython(definition, response.Content), + "java" => ParseAdoptium(definition, response.Content), + _ => OfflineFallback(definition) + }; + } + + private async Task> TryReadCacheAsync(string environmentId, CancellationToken cancellationToken) + { + try + { + var path = CachePath(environmentId); + if (!File.Exists(path) || DateTimeOffset.UtcNow - File.GetLastWriteTimeUtc(path) > TimeSpan.FromHours(6)) + { + return []; + } + + await using var stream = File.OpenRead(path); + return await JsonSerializer.DeserializeAsync>(stream, cancellationToken: cancellationToken).ConfigureAwait(false) ?? []; + } + catch + { + return []; + } + } + + private async Task WriteCacheAsync(string environmentId, IReadOnlyList versions, CancellationToken cancellationToken) + { + try + { + Directory.CreateDirectory(Path.GetDirectoryName(CachePath(environmentId))!); + await using var stream = File.Create(CachePath(environmentId)); + await JsonSerializer.SerializeAsync(stream, versions.Take(30).ToArray(), cancellationToken: cancellationToken).ConfigureAwait(false); + } + catch + { + } + } + + private string CachePath(string environmentId) + { + return Path.Combine(paths.Cache, "DevEnvironments", $"{environmentId}.json"); + } + + private static IReadOnlyList ParseGo(DevEnvironmentDefinition definition, string json) + { + using var document = JsonDocument.Parse(json); + var results = new List(); + foreach (var release in document.RootElement.EnumerateArray().Take(12)) + { + var version = release.GetProperty("version").GetString() ?? string.Empty; + if (string.IsNullOrWhiteSpace(version) || !release.TryGetProperty("files", out var files)) + { + continue; + } + + DownloadSource? installer = null; + DownloadSource? source = null; + foreach (var file in files.EnumerateArray()) + { + var name = file.GetProperty("filename").GetString() ?? string.Empty; + var url = $"https://go.dev/dl/{name}"; + if (name.Contains("windows-amd64.msi", StringComparison.OrdinalIgnoreCase)) + { + installer = OfficialSource(url, $"{definition.Name} {version}", name, Sha256: GetString(file, "sha256"), sizeBytes: GetLong(file, "size")); + } + else if (name.Contains("src.tar.gz", StringComparison.OrdinalIgnoreCase)) + { + source = OfficialSource(url, $"{definition.Name} {version} source", name, Sha256: GetString(file, "sha256"), sizeBytes: GetLong(file, "size")); + } + } + + var cleanVersion = version.TrimStart('g', 'o'); + results.Add(WithCandidates(definition.Id, new DevEnvironmentVersion(cleanVersion, installer, source))); + } + + return results; + } + + private static IReadOnlyList ParseNode(DevEnvironmentDefinition definition, string json) + { + using var document = JsonDocument.Parse(json); + return document.RootElement.EnumerateArray() + .Take(20) + .Select(item => + { + var version = (item.GetProperty("version").GetString() ?? string.Empty).TrimStart('v'); + var installerName = $"node-v{version}-x64.msi"; + var sourceName = $"node-v{version}.tar.gz"; + return new DevEnvironmentVersion( + version, + OfficialSource($"https://nodejs.org/dist/v{version}/{installerName}", $"{definition.Name} {version}", installerName), + OfficialSource($"https://nodejs.org/dist/v{version}/{sourceName}", $"{definition.Name} {version} source", sourceName), + TryDate(GetString(item, "date"))); + }) + .Select(version => WithCandidates(definition.Id, version)) + .ToArray(); + } + + private static IReadOnlyList ParseDotNet(DevEnvironmentDefinition definition, string json) + { + using var document = JsonDocument.Parse(json); + if (!document.RootElement.TryGetProperty("releases-index", out var releases)) + { + return []; + } + + return releases.EnumerateArray() + .Take(10) + .Select(item => + { + var version = GetString(item, "latest-sdk"); + var channel = GetString(item, "channel-version"); + var installerName = $"dotnet-sdk-{version}-win-x64.exe"; + var url = $"https://dotnet.microsoft.com/download/dotnet/thank-you/sdk-{version}-windows-x64-installer"; + return new DevEnvironmentVersion( + string.IsNullOrWhiteSpace(version) ? channel : version, + OfficialSource(url, $"{definition.Name} {version}", installerName), + null, + TryDate(GetString(item, "release-date"))); + }) + .Where(item => !string.IsNullOrWhiteSpace(item.Version)) + .Select(version => WithCandidates(definition.Id, version)) + .ToArray(); + } + + private static IReadOnlyList ParseGitHub( + DevEnvironmentDefinition definition, + string json, + string displayPrefix, + Func installerPredicate) + { + using var document = JsonDocument.Parse(json); + var releases = document.RootElement.ValueKind == JsonValueKind.Array + ? document.RootElement.EnumerateArray() + : []; + return releases + .Take(20) + .Select(release => + { + var tag = GetString(release, "tag_name"); + var version = tag.TrimStart('v'); + var published = TryDate(GetString(release, "published_at")); + DownloadSource? installer = null; + DownloadSource? source = null; + if (release.TryGetProperty("assets", out var assets) && assets.ValueKind == JsonValueKind.Array) + { + foreach (var asset in assets.EnumerateArray()) + { + var name = GetString(asset, "name"); + var url = GetString(asset, "browser_download_url"); + if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(url)) + { + continue; + } + + if (installer is null && installerPredicate(name)) + { + installer = new DownloadSource(url, $"{displayPrefix} {version}", name, "GitHub", SourceLabel: "GitHub", SizeBytes: GetLong(asset, "size")); + } + else if (source is null && (name.EndsWith(".tar.gz", StringComparison.OrdinalIgnoreCase) || name.EndsWith(".zip", StringComparison.OrdinalIgnoreCase))) + { + source = new DownloadSource(url, $"{displayPrefix} {version} source", name, "GitHub", SourceLabel: "GitHub", SizeBytes: GetLong(asset, "size")); + } + } + } + + var notes = GetString(release, "html_url"); + return WithCandidates(definition.Id, new DevEnvironmentVersion(version, installer, source, published, notes)); + }) + .Where(item => !string.IsNullOrWhiteSpace(item.Version) && item.AllCandidates.Count > 0) + .ToArray(); + } + + private static IReadOnlyList ParseRust(DevEnvironmentDefinition definition, string _) + { + var installer = OfficialSource( + "https://win.rustup.rs/x86_64", + "Rust stable rustup-init", + "rustup-init.exe"); + return [WithCandidates(definition.Id, new DevEnvironmentVersion("stable", installer, null, ReleaseNotesUrl: definition.OfficialHome))]; + } + + private static IReadOnlyList ParsePython(DevEnvironmentDefinition definition, string _) + { + var versions = new[] { "3.13.1", "3.12.8", "3.11.11", "3.10.16" }; + return versions.Select(version => + { + var installerName = $"python-{version}-amd64.exe"; + var installer = OfficialSource( + $"https://www.python.org/ftp/python/{version}/{installerName}", + $"Python {version}", + installerName); + var sourceName = $"Python-{version}.tgz"; + var source = OfficialSource( + $"https://www.python.org/ftp/python/{version}/{sourceName}", + $"Python {version} source", + sourceName); + return WithCandidates(definition.Id, new DevEnvironmentVersion(version, installer, source, ReleaseNotesUrl: definition.OfficialHome)); + }).ToArray(); + } + + private static IReadOnlyList ParseAdoptium(DevEnvironmentDefinition definition, string json) + { + try + { + using var document = JsonDocument.Parse(json); + var versions = new List(); + foreach (var asset in document.RootElement.EnumerateArray().Take(12)) + { + var version = asset.TryGetProperty("version", out var versionElement) + ? GetString(versionElement, "semver") + : string.Empty; + if (!asset.TryGetProperty("binary", out var binary) || + !binary.TryGetProperty("package", out var package)) + { + continue; + } + + var link = GetString(package, "link"); + var name = Path.GetFileName(new Uri(link).LocalPath); + if (string.IsNullOrWhiteSpace(version) || string.IsNullOrWhiteSpace(link) || string.IsNullOrWhiteSpace(name)) + { + continue; + } + + var installer = OfficialSource(link, $"{definition.Name} {version}", name, Sha256: GetString(package, "checksum"), sizeBytes: GetLong(package, "size")); + versions.Add(WithCandidates(definition.Id, new DevEnvironmentVersion(version, installer, null))); + } + + return versions.Count > 0 ? versions : OfflineFallback(definition); + } + catch + { + return OfflineFallback(definition); + } + } + + private static IReadOnlyList OfflineFallback(DevEnvironmentDefinition definition) + { + var source = new DownloadSource(definition.OfficialHome, $"{definition.Name} official downloads", $"{definition.Id}-official-download.url", "Manual", SourceLabel: "Manual"); + return [WithCandidates(definition.Id, new DevEnvironmentVersion("Latest", source, null, ReleaseNotesUrl: definition.OfficialHome))]; + } + + private static DevEnvironmentBuildRecipe BuildRecipe(DevEnvironmentDefinition definition) + { + return definition.Id switch + { + "go" => new(definition.Id, "Build Go from source after installing a bootstrap Go toolchain.", ["Go bootstrap toolchain", "Git", "Visual Studio Build Tools"], ["tar -xf ", "cd go\\src", "make.bat"]), + "python" => new(definition.Id, "Build CPython from source.", ["Visual Studio Build Tools", "Python dependencies"], ["tar -xf ", "cd Python-*", "PCbuild\\build.bat -p x64"]), + "java" => new(definition.Id, "Build OpenJDK/Temurin from source.", ["Boot JDK", "Visual Studio Build Tools", "Cygwin or MSYS2 where required"], ["tar -xf ", "bash configure", "make images"]), + "node" => new(definition.Id, "Build Node.js from source.", ["Python", "Visual Studio Build Tools", "Git"], ["tar -xf ", "cd node-*", "vcbuild.bat release x64"]), + "mysql" => new(definition.Id, "Build MySQL from source.", ["CMake", "Visual Studio Build Tools", "Bison", "Boost"], ["tar -xf ", "cmake -S . -B build -G \"Visual Studio 17 2022\" -A x64", "cmake --build build --config Release"]), + "cmake" => new(definition.Id, "Build CMake from source.", ["Visual Studio Build Tools"], ["tar -xf ", "cmake -S . -B build -A x64", "cmake --build build --config Release"]), + "docker" => new(definition.Id, "Docker Desktop source build is not exposed in this tool.", [], [], IsSupported: false), + _ => new(definition.Id, "Download the source archive, verify prerequisites from the official documentation, then run the vendor build commands.", [], ["tar -xf "]) + }; + } + + private static DevEnvironmentVersion WithCandidates(string environmentId, DevEnvironmentVersion version) + { + var candidates = new List(); + if (version.Installer is not null) + { + candidates.Add(new DevEnvironmentDownloadCandidate( + "installer-official", + version.Installer.DisplayName, + version.Installer, + DevEnvironmentInstallMode.QuickInstall, + version.Installer.SourceKind, + version.Installer.MirrorRegion, + true)); + } + if (version.SourceArchive is not null) + { + candidates.Add(new DevEnvironmentDownloadCandidate( + "source-official", + version.SourceArchive.DisplayName, + version.SourceArchive, + DevEnvironmentInstallMode.SourceBuild, + version.SourceArchive.SourceKind, + version.SourceArchive.MirrorRegion)); + } + + foreach (var mirror in MirrorCandidates(environmentId, version)) + { + if (candidates.All(item => !string.Equals(item.Source.Url, mirror.Source.Url, StringComparison.OrdinalIgnoreCase))) + { + candidates.Add(mirror); + } + } + + return version with { Candidates = candidates }; + } + + private static IEnumerable MirrorCandidates(string environmentId, DevEnvironmentVersion version) + { + var v = version.Version.TrimStart('v'); + if (string.IsNullOrWhiteSpace(v)) + { + yield break; + } + + if (environmentId == "node") + { + var installerName = $"node-v{v}-x64.msi"; + yield return Mirror("mirror-cn", "Node.js CN mirror", $"https://npmmirror.com/mirrors/node/v{v}/{installerName}", installerName, DevEnvironmentInstallMode.QuickInstall); + var sourceName = $"node-v{v}.tar.gz"; + yield return Mirror("mirror-cn-source", "Node.js CN source mirror", $"https://npmmirror.com/mirrors/node/v{v}/{sourceName}", sourceName, DevEnvironmentInstallMode.SourceBuild); + } + else if (environmentId == "go") + { + var installerName = $"go{v}.windows-amd64.msi"; + yield return Mirror("mirror-cn", "Go CN mirror", $"https://mirrors.aliyun.com/golang/{installerName}", installerName, DevEnvironmentInstallMode.QuickInstall); + var sourceName = $"go{v}.src.tar.gz"; + yield return Mirror("mirror-cn-source", "Go CN source mirror", $"https://mirrors.aliyun.com/golang/{sourceName}", sourceName, DevEnvironmentInstallMode.SourceBuild); + } + else if (environmentId == "python") + { + var installerName = $"python-{v}-amd64.exe"; + yield return Mirror("mirror-cn", "Python CN mirror", $"https://mirrors.huaweicloud.com/python/{v}/{installerName}", installerName, DevEnvironmentInstallMode.QuickInstall); + } + } + + private static DevEnvironmentDownloadCandidate Mirror(string id, string displayName, string url, string fileName, DevEnvironmentInstallMode mode) + { + return new DevEnvironmentDownloadCandidate( + id, + displayName, + new DownloadSource(url, displayName, fileName, "MirrorCN", SourceLabel: "MirrorCN", MirrorRegion: "CN"), + mode, + "MirrorCN", + "CN"); + } + + private static DownloadSource OfficialSource(string url, string displayName, string fileName, string? Sha256 = null, long? sizeBytes = null) + { + return new DownloadSource(url, displayName, fileName, "Official", Sha256, "Official", SizeBytes: sizeBytes); + } + + private static string GetString(JsonElement element, string name) + { + return element.ValueKind == JsonValueKind.Object && + element.TryGetProperty(name, out var value) + ? value.ValueKind == JsonValueKind.String ? value.GetString() ?? string.Empty : value.ToString() + : string.Empty; + } + + private static long? GetLong(JsonElement element, string name) + { + if (element.ValueKind != JsonValueKind.Object || + !element.TryGetProperty(name, out var value)) + { + return null; + } + + if (value.ValueKind == JsonValueKind.Number && value.TryGetInt64(out var number)) + { + return number; + } + + return long.TryParse(value.ToString(), out number) ? number : null; + } + + private static DateTimeOffset? TryDate(string value) + { + return DateTimeOffset.TryParse(value, out var parsed) ? parsed : null; + } +} diff --git a/src/YMhut.Box.Core/Downloads/DirectDownloadValidator.cs b/src/YMhut.Box.Core/Downloads/DirectDownloadValidator.cs new file mode 100644 index 0000000..7fc9c0a --- /dev/null +++ b/src/YMhut.Box.Core/Downloads/DirectDownloadValidator.cs @@ -0,0 +1,293 @@ +using System.Net.Http.Headers; +using System.Text; + +namespace YMhut.Box.Core.Downloads; + +public interface IDirectDownloadValidator +{ + Task ValidateAsync(string input, CancellationToken cancellationToken = default); +} + +public sealed record DirectDownloadValidationResult( + bool IsDirectDownload, + string Url, + string OriginalUrl = "", + string ResolvedUrl = "", + string ContentType = "", + string SuggestedFileName = "", + string Message = "", + bool WasInternetShortcut = false) +{ + public string EffectiveUrl => string.IsNullOrWhiteSpace(ResolvedUrl) ? Url : ResolvedUrl; +} + +public sealed class DirectDownloadValidator(HttpClient? client = null) : IDirectDownloadValidator, IDisposable +{ + private const int ProbeBytes = 4096; + private readonly HttpClient _client = client ?? CreateDefaultClient(); + private readonly bool _disposeClient = client is null; + + public async Task ValidateAsync(string input, CancellationToken cancellationToken = default) + { + var value = input.Trim(); + if (TryParseInternetShortcut(value, out var shortcutUrl)) + { + var nested = await ValidateAsync(shortcutUrl, cancellationToken).ConfigureAwait(false); + return nested with + { + OriginalUrl = value, + WasInternetShortcut = true, + Message = nested.IsDirectDownload + ? "Internet shortcut resolved to a direct download URL." + : nested.Message + }; + } + + if (!Uri.TryCreate(value, UriKind.Absolute, out var uri) || + uri.Scheme is not ("http" or "https")) + { + return new DirectDownloadValidationResult(false, value, Message: "Enter a valid HTTP/HTTPS download URL."); + } + + if (LooksLikeInternetShortcutName(uri)) + { + var shortcut = await TryReadShortcutAsync(uri, cancellationToken).ConfigureAwait(false); + if (!string.IsNullOrWhiteSpace(shortcut) && TryParseInternetShortcut(shortcut, out var resolved)) + { + var nested = await ValidateAsync(resolved, cancellationToken).ConfigureAwait(false); + return nested with + { + OriginalUrl = uri.ToString(), + WasInternetShortcut = true, + Message = nested.IsDirectDownload + ? "Internet shortcut resolved to a direct download URL." + : nested.Message + }; + } + + return new DirectDownloadValidationResult( + false, + uri.ToString(), + Message: "This link points to an Internet shortcut instead of a downloadable file.", + WasInternetShortcut: true); + } + + try + { + using var request = new HttpRequestMessage(HttpMethod.Get, uri); + request.Headers.Range = new RangeHeaderValue(0, ProbeBytes - 1); + using var response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken) + .ConfigureAwait(false); + response.EnsureSuccessStatusCode(); + + var resolvedUri = response.RequestMessage?.RequestUri?.ToString() ?? uri.ToString(); + var contentType = response.Content.Headers.ContentType?.MediaType ?? string.Empty; + var contentDispositionName = response.Content.Headers.ContentDisposition?.FileNameStar ?? + response.Content.Headers.ContentDisposition?.FileName; + var suggestedName = CleanHeaderFileName(contentDispositionName); + if (string.IsNullOrWhiteSpace(suggestedName)) + { + suggestedName = Path.GetFileName(new Uri(resolvedUri).LocalPath); + } + + var probe = await ReadProbeAsync(response, cancellationToken).ConfigureAwait(false); + var textProbe = LooksTextual(contentType, probe) + ? Encoding.UTF8.GetString(probe) + : string.Empty; + + if (IsBlockedContent(contentType, suggestedName, textProbe, out var message)) + { + if (TryParseInternetShortcut(textProbe, out var nestedUrl)) + { + var nested = await ValidateAsync(nestedUrl, cancellationToken).ConfigureAwait(false); + return nested with + { + OriginalUrl = uri.ToString(), + WasInternetShortcut = true, + Message = nested.IsDirectDownload + ? "Internet shortcut resolved to a direct download URL." + : nested.Message + }; + } + + return new DirectDownloadValidationResult( + false, + uri.ToString(), + ResolvedUrl: resolvedUri, + ContentType: contentType, + SuggestedFileName: suggestedName, + Message: message); + } + + return new DirectDownloadValidationResult( + true, + uri.ToString(), + ResolvedUrl: resolvedUri, + ContentType: contentType, + SuggestedFileName: suggestedName, + Message: "Direct download URL verified."); + } + catch (Exception exception) + { + return new DirectDownloadValidationResult( + false, + uri.ToString(), + Message: $"Could not verify the download link: {exception.Message}"); + } + } + + public void Dispose() + { + if (_disposeClient) + { + _client.Dispose(); + } + } + + public static bool TryParseInternetShortcut(string content, out string url) + { + url = string.Empty; + foreach (var rawLine in content.Replace("\r\n", "\n", StringComparison.Ordinal).Split('\n')) + { + var line = rawLine.Trim(); + if (!line.StartsWith("URL=", StringComparison.OrdinalIgnoreCase)) + { + continue; + } + + var value = line[4..].Trim(); + if (Uri.TryCreate(value, UriKind.Absolute, out var uri) && + uri.Scheme is "http" or "https") + { + url = uri.ToString(); + return true; + } + } + + return false; + } + + private static HttpClient CreateDefaultClient() + { + var client = new HttpClient(new HttpClientHandler + { + AllowAutoRedirect = true, + MaxAutomaticRedirections = 8 + }) + { + Timeout = TimeSpan.FromSeconds(20) + }; + client.DefaultRequestHeaders.UserAgent.ParseAdd("YMhutBox/2.0 DownloadValidator"); + return client; + } + + private static async Task TryReadShortcutAsync(Uri uri, CancellationToken cancellationToken) + { + try + { + using var client = CreateDefaultClient(); + using var request = new HttpRequestMessage(HttpMethod.Get, uri); + request.Headers.Range = new RangeHeaderValue(0, ProbeBytes - 1); + using var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken) + .ConfigureAwait(false); + if (!response.IsSuccessStatusCode) + { + return string.Empty; + } + + var bytes = await ReadProbeAsync(response, cancellationToken).ConfigureAwait(false); + return Encoding.UTF8.GetString(bytes); + } + catch + { + return string.Empty; + } + } + + private static async Task ReadProbeAsync(HttpResponseMessage response, CancellationToken cancellationToken) + { + await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false); + using var memory = new MemoryStream(); + var buffer = new byte[ProbeBytes]; + var read = await stream.ReadAsync(buffer.AsMemory(0, buffer.Length), cancellationToken).ConfigureAwait(false); + if (read > 0) + { + memory.Write(buffer, 0, read); + } + + return memory.ToArray(); + } + + private static bool IsBlockedContent(string contentType, string fileName, string probe, out string message) + { + var lowerType = contentType.ToLowerInvariant(); + var lowerName = fileName.ToLowerInvariant(); + var text = probe.TrimStart('\uFEFF', ' ', '\t', '\r', '\n'); + + if (lowerName.EndsWith(".url", StringComparison.OrdinalIgnoreCase) || + lowerType.Contains("internet-shortcut", StringComparison.OrdinalIgnoreCase)) + { + message = "This is an Internet shortcut, not the actual downloadable file."; + return true; + } + + if (lowerType.Contains("text/html", StringComparison.OrdinalIgnoreCase) || + text.StartsWith(" 0 && bytes.Take(Math.Min(bytes.Length, 128)).All(value => + value is 9 or 10 or 13 || value >= 32); + } + + private static bool LooksLikeInternetShortcutName(Uri uri) + { + return uri.LocalPath.EndsWith(".url", StringComparison.OrdinalIgnoreCase); + } + + private static string CleanHeaderFileName(string? value) + { + if (string.IsNullOrWhiteSpace(value)) + { + return string.Empty; + } + + return value.Trim().Trim('"'); + } +} diff --git a/src/YMhut.Box.Core/Downloads/DownloadModels.cs b/src/YMhut.Box.Core/Downloads/DownloadModels.cs new file mode 100644 index 0000000..7939955 --- /dev/null +++ b/src/YMhut.Box.Core/Downloads/DownloadModels.cs @@ -0,0 +1,258 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace YMhut.Box.Core.Downloads; + +public enum DownloadState +{ + Queued, + Running, + Paused, + Completed, + Failed, + Canceled +} + +public sealed record DownloadSource( + string Url, + string DisplayName, + string FileName, + string SourceKind = "Official", + string? Sha256 = null, + string SourceLabel = "", + string MirrorRegion = "", + long? SizeBytes = null, + string OriginalUrl = "", + string ResolvedUrl = "", + string ValidatedContentType = "", + string PackageId = "") +{ + public string EffectiveLabel => string.IsNullOrWhiteSpace(SourceLabel) ? SourceKind : SourceLabel; + + public string EffectiveUrl => string.IsNullOrWhiteSpace(ResolvedUrl) ? Url : ResolvedUrl; +} + +public sealed record DownloadOptions( + string? TargetDirectory = null, + string? TargetPath = null, + string? InstallCommand = null, + string? InstallArguments = null, + bool IsInstaller = false, + bool DeleteAfterInstall = false); + +public sealed record DownloadSettings( + string DefaultDirectory, + int MaxConcurrentDownloads = 5) +{ + public int EffectiveMaxConcurrentDownloads => Math.Clamp(MaxConcurrentDownloads, 1, 5); +} + +public sealed record DownloadItem( + string Id, + DownloadSource Source, + string TargetPath, + DownloadState State, + long ReceivedBytes = 0, + long? TotalBytes = null, + double BytesPerSecond = 0, + DateTimeOffset CreatedAt = default, + DateTimeOffset UpdatedAt = default, + string? Error = null, + string? InstallCommand = null, + string? InstallArguments = null, + bool IsInstaller = false, + bool DeleteAfterInstall = false, + bool InstallLaunched = false, + bool InstallCleanupCompleted = false, + string PartialPath = "", + string ETag = "", + string LastModified = "", + string AcceptRanges = "", + long? ContentLength = null, + string FinalUrl = "", + bool ResumeSupported = false) +{ + [JsonIgnore] + public string EffectivePartialPath => string.IsNullOrWhiteSpace(PartialPath) ? TargetPath + ".partial" : PartialPath; + + public static DownloadItem Create( + DownloadSource source, + string targetPath, + string? installCommand = null, + string? installArguments = null, + bool isInstaller = false, + bool deleteAfterInstall = false) + { + var now = DateTimeOffset.UtcNow; + return new DownloadItem( + Guid.NewGuid().ToString("N"), + source, + targetPath, + DownloadState.Queued, + CreatedAt: now, + UpdatedAt: now, + InstallCommand: installCommand, + InstallArguments: installArguments, + IsInstaller: isInstaller, + DeleteAfterInstall: deleteAfterInstall, + PartialPath: targetPath + ".partial"); + } + + public DownloadItem WithProgress(DownloadState state, long receivedBytes, long? totalBytes, double bytesPerSecond, string? error = null) + { + return this with + { + State = state, + ReceivedBytes = Math.Max(0, receivedBytes), + TotalBytes = totalBytes is > 0 ? totalBytes : null, + BytesPerSecond = Math.Max(0, bytesPerSecond), + UpdatedAt = DateTimeOffset.UtcNow, + Error = error + }; + } + + public DownloadItem WithResumeMetadata( + string? eTag = null, + string? lastModified = null, + string? acceptRanges = null, + long? contentLength = null, + string? finalUrl = null, + bool? resumeSupported = null) + { + return this with + { + PartialPath = string.IsNullOrWhiteSpace(PartialPath) ? TargetPath + ".partial" : PartialPath, + ETag = string.IsNullOrWhiteSpace(eTag) ? ETag : eTag, + LastModified = string.IsNullOrWhiteSpace(lastModified) ? LastModified : lastModified, + AcceptRanges = string.IsNullOrWhiteSpace(acceptRanges) ? AcceptRanges : acceptRanges, + ContentLength = contentLength is > 0 ? contentLength : ContentLength, + FinalUrl = string.IsNullOrWhiteSpace(finalUrl) ? FinalUrl : finalUrl, + ResumeSupported = resumeSupported ?? ResumeSupported, + UpdatedAt = DateTimeOffset.UtcNow + }; + } + + public DownloadItem WithInstallState(bool launched, bool cleanupCompleted = false) + { + return this with + { + InstallLaunched = launched, + InstallCleanupCompleted = cleanupCompleted, + UpdatedAt = DateTimeOffset.UtcNow + }; + } +} + +public sealed record DownloadProgressSnapshot( + string Id, + DownloadState State, + long ReceivedBytes, + long? TotalBytes, + double BytesPerSecond, + string? Error = null, + string ETag = "", + string LastModified = "", + string AcceptRanges = "", + long? ContentLength = null, + string FinalUrl = "", + bool RestartedFromZero = false) +{ + public double Progress + { + get + { + if (TotalBytes is not > 0) + { + return 0; + } + + return Math.Clamp(ReceivedBytes / (double)TotalBytes.Value, 0, 1); + } + } +} + +public static class DownloadFormat +{ + public static string FormatBytes(long bytes) + { + string[] units = ["B", "KB", "MB", "GB", "TB"]; + var value = Math.Max(0, bytes); + var unit = 0; + var scaled = (double)value; + while (scaled >= 1024 && unit < units.Length - 1) + { + scaled /= 1024; + unit++; + } + + return unit == 0 ? $"{value} {units[unit]}" : $"{scaled:0.#} {units[unit]}"; + } + + public static string FormatSpeed(double bytesPerSecond) + { + return $"{FormatBytes((long)Math.Max(0, bytesPerSecond))}/s"; + } + + public static string FormatEta(long receivedBytes, long? totalBytes, double bytesPerSecond) + { + if (totalBytes is not > 0 || + receivedBytes <= 0 || + receivedBytes >= totalBytes.Value || + bytesPerSecond <= 1) + { + return "-"; + } + + var remaining = Math.Max(0, totalBytes.Value - receivedBytes); + var eta = TimeSpan.FromSeconds(remaining / bytesPerSecond); + if (eta.TotalHours >= 1) + { + return $"{(int)eta.TotalHours:0}h {eta.Minutes:00}m"; + } + + return eta.TotalMinutes >= 1 + ? $"{eta.Minutes:0}m {eta.Seconds:00}s" + : $"{Math.Max(1, eta.Seconds):0}s"; + } +} + +public static class DownloadHostProtocol +{ + public const string Version = "1"; + public const string Ready = "ready"; + public const string Start = "start"; + public const string Pause = "pause"; + public const string Resume = "resume"; + public const string Cancel = "cancel"; + public const string Progress = "progress"; + public const string Completed = "completed"; + public const string Failed = "failed"; + public const string Paused = "paused"; + public const string Canceled = "canceled"; + public const string Shutdown = "shutdown"; + public const string Ping = "ping"; + public const string Pong = "pong"; + + private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web) + { + Converters = { new JsonStringEnumConverter() } + }; + + public static string Serialize(DownloadHostMessage message) + { + return JsonSerializer.Serialize(message, JsonOptions); + } + + public static DownloadHostMessage? Deserialize(string line) + { + return JsonSerializer.Deserialize(line, JsonOptions); + } +} + +public sealed record DownloadHostMessage( + string Type, + string RequestId = "", + string Version = "", + DownloadItem? Item = null, + DownloadProgressSnapshot? Progress = null, + string? Error = null); diff --git a/src/YMhut.Box.Core/Downloads/DownloadQueueStore.cs b/src/YMhut.Box.Core/Downloads/DownloadQueueStore.cs new file mode 100644 index 0000000..1877e96 --- /dev/null +++ b/src/YMhut.Box.Core/Downloads/DownloadQueueStore.cs @@ -0,0 +1,189 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using YMhut.Box.Core.App; + +namespace YMhut.Box.Core.Downloads; + +public interface IDownloadQueueStore +{ + Task> LoadAsync(CancellationToken cancellationToken = default); + + Task SaveAsync(IReadOnlyList items, CancellationToken cancellationToken = default); + + Task LoadSettingsAsync(CancellationToken cancellationToken = default); + + Task SaveSettingsAsync(DownloadSettings settings, CancellationToken cancellationToken = default); +} + +public sealed class DownloadQueueStore(AppPaths paths) : IDownloadQueueStore +{ + private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web) + { + WriteIndented = true, + Converters = { new JsonStringEnumConverter() } + }; + + private readonly string _path = Path.Combine(paths.Data, "downloads.json"); + private readonly string _settingsPath = Path.Combine(paths.Data, "download-settings.json"); + private readonly SemaphoreSlim _gate = new(1, 1); + + public async Task> LoadAsync(CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + if (!File.Exists(_path)) + { + return []; + } + + await using var stream = File.OpenRead(_path); + return await JsonSerializer.DeserializeAsync>(stream, JsonOptions, cancellationToken) + .ConfigureAwait(false) + ?? []; + } + catch + { + return []; + } + finally + { + _gate.Release(); + } + } + + public async Task SaveAsync(IReadOnlyList items, CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + Directory.CreateDirectory(Path.GetDirectoryName(_path)!); + var temp = _path + ".tmp"; + await using (var stream = File.Create(temp)) + { + await JsonSerializer.SerializeAsync(stream, items, JsonOptions, cancellationToken).ConfigureAwait(false); + } + + File.Copy(temp, _path, overwrite: true); + File.Delete(temp); + } + finally + { + _gate.Release(); + } + } + + public async Task LoadSettingsAsync(CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + var fallback = DefaultSettings(); + if (!File.Exists(_settingsPath)) + { + return fallback; + } + + await using var stream = File.OpenRead(_settingsPath); + var settings = await JsonSerializer.DeserializeAsync(stream, JsonOptions, cancellationToken) + .ConfigureAwait(false) + ?? fallback; + + var directory = string.IsNullOrWhiteSpace(settings.DefaultDirectory) + ? fallback.DefaultDirectory + : settings.DefaultDirectory; + return settings with + { + DefaultDirectory = directory, + MaxConcurrentDownloads = settings.EffectiveMaxConcurrentDownloads + }; + } + catch + { + return DefaultSettings(); + } + finally + { + _gate.Release(); + } + } + + public async Task SaveSettingsAsync(DownloadSettings settings, CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + var normalized = settings with + { + DefaultDirectory = string.IsNullOrWhiteSpace(settings.DefaultDirectory) + ? DefaultSettings().DefaultDirectory + : settings.DefaultDirectory, + MaxConcurrentDownloads = settings.EffectiveMaxConcurrentDownloads + }; + Directory.CreateDirectory(Path.GetDirectoryName(_settingsPath)!); + var temp = _settingsPath + ".tmp"; + await using (var stream = File.Create(temp)) + { + await JsonSerializer.SerializeAsync(stream, normalized, JsonOptions, cancellationToken).ConfigureAwait(false); + } + + File.Copy(temp, _settingsPath, overwrite: true); + File.Delete(temp); + } + finally + { + _gate.Release(); + } + } + + private DownloadSettings DefaultSettings() + { + return new DownloadSettings(Path.Combine(paths.Data, "Downloads"), 5); + } +} + +public interface IDownloadHostProcessService : IDisposable +{ + event EventHandler? ProgressChanged; + + Task StartAsync(DownloadItem item, CancellationToken cancellationToken = default); + + Task PauseAsync(string id, CancellationToken cancellationToken = default); + + Task ResumeAsync(DownloadItem item, CancellationToken cancellationToken = default); + + Task CancelAsync(string id, CancellationToken cancellationToken = default); +} + +public interface IDownloadManagerService +{ + event EventHandler? ItemsChanged; + + IReadOnlyList Items { get; } + + DownloadSettings Settings { get; } + + Task InitializeAsync(CancellationToken cancellationToken = default); + + Task EnqueueAsync(DownloadSource source, string? targetDirectory = null, string? installCommand = null, string? installArguments = null, CancellationToken cancellationToken = default); + + Task EnqueueAsync(DownloadSource source, DownloadOptions options, CancellationToken cancellationToken = default); + + Task UpdateSettingsAsync(DownloadSettings settings, CancellationToken cancellationToken = default); + + Task StartAsync(string id, CancellationToken cancellationToken = default); + + Task PauseAsync(string id, CancellationToken cancellationToken = default); + + Task ResumeAsync(string id, CancellationToken cancellationToken = default); + + Task CancelAsync(string id, CancellationToken cancellationToken = default); + + Task RetryAsync(string id, CancellationToken cancellationToken = default); + + Task ClearCompletedAsync(CancellationToken cancellationToken = default); + + Task MarkInstallLaunchedAsync(string id, bool cleanupCompleted = false, CancellationToken cancellationToken = default); + + Task TryCleanupAfterInstallAsync(string id, CancellationToken cancellationToken = default); +} diff --git a/src/YMhut.Box.Core/Feedback/FeedbackCode.cs b/src/YMhut.Box.Core/Feedback/FeedbackCode.cs new file mode 100644 index 0000000..a875608 --- /dev/null +++ b/src/YMhut.Box.Core/Feedback/FeedbackCode.cs @@ -0,0 +1,32 @@ +using System.Globalization; +using System.Security.Cryptography; +using System.Text.RegularExpressions; + +namespace YMhut.Box.Core.Feedback; + +public static class FeedbackCode +{ + private static readonly Regex Pattern = new( + "^FB-[0-9]{8}-[A-F0-9]{6}$", + RegexOptions.Compiled | RegexOptions.CultureInvariant); + + public static string Create(DateTimeOffset? now = null) + { + Span bytes = stackalloc byte[3]; + RandomNumberGenerator.Fill(bytes); + var stamp = (now ?? DateTimeOffset.Now).ToString("yyyyMMdd", CultureInfo.InvariantCulture); + return $"FB-{stamp}-{Convert.ToHexString(bytes)}"; + } + + public static bool IsValid(string? value) + { + return Pattern.IsMatch(Normalize(value)); + } + + public static string Normalize(string? value) + { + return string.IsNullOrWhiteSpace(value) + ? string.Empty + : value.Trim().ToUpperInvariant(); + } +} diff --git a/src/YMhut.Box.Core/Feedback/FeedbackModels.cs b/src/YMhut.Box.Core/Feedback/FeedbackModels.cs new file mode 100644 index 0000000..a10a78c --- /dev/null +++ b/src/YMhut.Box.Core/Feedback/FeedbackModels.cs @@ -0,0 +1,121 @@ +namespace YMhut.Box.Core.Feedback; + +public sealed record FeedbackRequest( + string Title, + string Type, + string Severity, + string Contact, + string Body, + bool IncludeTodayLogs, + bool IncludeToolStatus, + bool IncludeSystemSummary, + string? ScreenshotPath = null, + string Language = "zh-CN", + string? FeedbackCode = null); + +public sealed record FeedbackPackageResult( + string PackagePath, + string PackageSha256, + long PackageBytes, + DateTimeOffset CreatedAt, + string SummaryText, + IReadOnlyDictionary IncludedFiles); + +public sealed record FeedbackSubmissionResponse( + bool Ok, + string? Code, + string? ErrorCode, + string? Message, + bool Duplicate = false); + +public sealed record FeedbackStatusResponse( + bool Ok, + string? Code, + string? Status, + string? StatusLabel, + bool HasReply, + string? Reply, + string? ReceivedAt, + string? UpdatedAt, + string? ErrorCode, + string? Message, + string? StatusDetail = null, + string? Category = null, + string? Priority = null, + bool MailSent = false); + +public enum FeedbackRecordState +{ + Draft, + PackageCreated, + PackageFailed, + Sending, + Sent, + SendFailed, + Querying, + StatusUpdated, + QueryFailed, + Archived +} + +public sealed record FeedbackRecord( + string Code, + string Title, + string Type, + string Severity, + DateTimeOffset CreatedAtUtc, + DateTimeOffset UpdatedAtUtc, + FeedbackRecordState State, + string StatusLabel, + string LastMessage, + string? PackagePath = null, + string? PackageSha256 = null, + long PackageBytes = 0, + DateTimeOffset? SubmittedAtUtc = null, + DateTimeOffset? LastQueryAtUtc = null, + DateTimeOffset? ArchivedAtUtc = null, + bool IsArchived = false, + int FailureCount = 0, + string? ServiceStatus = null, + string? PublicReply = null, + string? ServiceStatusDetail = null, + string? ServiceCategory = null, + string? ServicePriority = null, + bool? ServiceMailSent = null, + string? ServiceReceivedAt = null, + string? ServiceUpdatedAt = null); + +public interface IFeedbackPackageService +{ + Task BuildPackageAsync( + FeedbackRequest request, + CancellationToken cancellationToken = default); +} + +public interface IFeedbackSubmissionService +{ + Task SubmitAsync( + FeedbackRequest request, + FeedbackPackageResult package, + CancellationToken cancellationToken = default); + + Task GetStatusAsync( + string feedbackCode, + CancellationToken cancellationToken = default); +} + +public interface IFeedbackRecordStore +{ + Task> ReadAsync(CancellationToken cancellationToken = default); + + Task FindAsync(string feedbackCode, CancellationToken cancellationToken = default); + + Task UpsertAsync(FeedbackRecord record, CancellationToken cancellationToken = default); + + Task RemoveAsync(string feedbackCode, CancellationToken cancellationToken = default); + + Task> ArchiveOlderThanAsync( + TimeSpan activeWindow, + DateTimeOffset? now = null, + CancellationToken cancellationToken = default); +} diff --git a/src/YMhut.Box.Core/Feedback/FeedbackPackageCrypto.cs b/src/YMhut.Box.Core/Feedback/FeedbackPackageCrypto.cs new file mode 100644 index 0000000..b00da39 --- /dev/null +++ b/src/YMhut.Box.Core/Feedback/FeedbackPackageCrypto.cs @@ -0,0 +1,89 @@ +using System.Security.Cryptography; +using System.Text; + +namespace YMhut.Box.Core.Feedback; + +public sealed record EncryptedFeedbackPackage( + string PackagePath, + string PackageSha256, + long PackageBytes); + +public static class FeedbackPackageCrypto +{ + public const string MagicText = "YMHUTFB1"; + public const string EncryptionKeyMaterial = "ymhut-box-feedback-package-v1"; + + private const int NonceSize = 12; + private const int TagSize = 16; + private static readonly byte[] Magic = Encoding.ASCII.GetBytes(MagicText); + private static readonly byte[] Key = SHA256.HashData(Encoding.UTF8.GetBytes(EncryptionKeyMaterial)); + + public static async Task EncryptPackageAsync( + string packagePath, + string? encryptedPath = null, + CancellationToken cancellationToken = default) + { + if (!File.Exists(packagePath)) + { + throw new FileNotFoundException("反馈包不存在。", packagePath); + } + + encryptedPath ??= Path.ChangeExtension(packagePath, ".ymfb"); + var plaintext = await File.ReadAllBytesAsync(packagePath, cancellationToken).ConfigureAwait(false); + var nonce = RandomNumberGenerator.GetBytes(NonceSize); + var tag = new byte[TagSize]; + var ciphertext = new byte[plaintext.Length]; + + using (var aes = new AesGcm(Key, TagSize)) + { + aes.Encrypt(nonce, plaintext, ciphertext, tag, Magic); + } + + await using (var stream = File.Create(encryptedPath)) + { + await stream.WriteAsync(Magic, cancellationToken).ConfigureAwait(false); + await stream.WriteAsync(nonce, cancellationToken).ConfigureAwait(false); + await stream.WriteAsync(tag, cancellationToken).ConfigureAwait(false); + await stream.WriteAsync(ciphertext, cancellationToken).ConfigureAwait(false); + } + + var hash = await HashFileAsync(encryptedPath, cancellationToken).ConfigureAwait(false); + var info = new FileInfo(encryptedPath); + return new EncryptedFeedbackPackage(info.FullName, hash, info.Length); + } + + public static async Task DecryptPackageAsync( + string encryptedPath, + string outputPath, + CancellationToken cancellationToken = default) + { + var payload = await File.ReadAllBytesAsync(encryptedPath, cancellationToken).ConfigureAwait(false); + if (payload.Length < Magic.Length + NonceSize + TagSize || + !payload.AsSpan(0, Magic.Length).SequenceEqual(Magic)) + { + throw new InvalidDataException("反馈包加密格式无效。"); + } + + var offset = Magic.Length; + var nonce = payload.AsSpan(offset, NonceSize).ToArray(); + offset += NonceSize; + var tag = payload.AsSpan(offset, TagSize).ToArray(); + offset += TagSize; + var ciphertext = payload.AsSpan(offset).ToArray(); + var plaintext = new byte[ciphertext.Length]; + + using (var aes = new AesGcm(Key, TagSize)) + { + aes.Decrypt(nonce, ciphertext, tag, plaintext, Magic); + } + + await File.WriteAllBytesAsync(outputPath, plaintext, cancellationToken).ConfigureAwait(false); + } + + private static async Task HashFileAsync(string path, CancellationToken cancellationToken) + { + await using var stream = File.OpenRead(path); + var hash = await SHA256.HashDataAsync(stream, cancellationToken).ConfigureAwait(false); + return Convert.ToHexString(hash).ToLowerInvariant(); + } +} diff --git a/src/YMhut.Box.Core/Feedback/FeedbackPackageService.cs b/src/YMhut.Box.Core/Feedback/FeedbackPackageService.cs new file mode 100644 index 0000000..6040fab --- /dev/null +++ b/src/YMhut.Box.Core/Feedback/FeedbackPackageService.cs @@ -0,0 +1,268 @@ +using System.IO.Compression; +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using YMhut.Box.Core; +using YMhut.Box.Core.Logging; +using YMhut.Box.Core.System; +using YMhut.Box.Core.Tools; + +namespace YMhut.Box.Core.Feedback; + +public sealed class FeedbackPackageService( + ILogService logService, + ISystemMetricsService? systemMetricsService = null, + ToolCatalog? toolCatalog = null) : IFeedbackPackageService +{ + public const long MaxScreenshotBytes = 5L * 1024L * 1024L; + private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web) { WriteIndented = true }; + private static readonly string[] AllowedScreenshotExtensions = [".png", ".jpg", ".jpeg", ".webp", ".bmp"]; + + public async Task BuildPackageAsync( + FeedbackRequest request, + CancellationToken cancellationToken = default) + { + ValidateRequest(request); + var feedbackRoot = FeedbackPackagesRoot(); + Directory.CreateDirectory(feedbackRoot); + var createdAt = DateTimeOffset.Now; + var packagePath = NextAvailablePath(Path.Combine(feedbackRoot, $"feedback-{createdAt:yyyyMMdd-HHmmss}-{RandomSuffix()}.zip")); + var included = new Dictionary(StringComparer.OrdinalIgnoreCase); + + await using (var fileStream = File.Create(packagePath)) + using (var archive = new ZipArchive(fileStream, ZipArchiveMode.Create, leaveOpen: false)) + { + var manifest = new + { + createdAt, + feedbackCode = FeedbackCode.Normalize(request.FeedbackCode), + request = new + { + feedbackCode = FeedbackCode.Normalize(request.FeedbackCode), + title = SensitiveText.Sanitize(request.Title, 160), + type = request.Type, + severity = request.Severity, + contact = SensitiveText.Sanitize(request.Contact, 180), + body = SensitiveText.Sanitize(request.Body, 4000), + includeTodayLogs = request.IncludeTodayLogs, + includeToolStatus = request.IncludeToolStatus, + includeSystemSummary = request.IncludeSystemSummary, + hasScreenshot = !string.IsNullOrWhiteSpace(request.ScreenshotPath) + }, + client = new + { + app = "YMhut Box" + } + }; + await WriteJsonEntryAsync(archive, "feedback.json", manifest, cancellationToken).ConfigureAwait(false); + included["feedback.json"] = "反馈正文和勾选项"; + + if (request.IncludeSystemSummary) + { + var system = CaptureSystemSummary(); + await WriteJsonEntryAsync(archive, "system-summary.json", system, cancellationToken).ConfigureAwait(false); + included["system-summary.json"] = "系统摘要"; + } + + if (request.IncludeToolStatus) + { + var status = CaptureToolStatus(); + await WriteJsonEntryAsync(archive, "tool-status.json", status, cancellationToken).ConfigureAwait(false); + included["tool-status.json"] = "工具运行状态"; + } + + if (request.IncludeTodayLogs) + { + var entries = await logService.ReadByDateAsync( + DateOnly.FromDateTime(DateTime.Now), + take: 1000, + cancellationToken: cancellationToken).ConfigureAwait(false); + var logs = entries.Select(entry => new + { + raw = new + { + timestamp = entry.Timestamp, + level = entry.Level, + category = entry.Category, + message = SensitiveText.Sanitize(entry.Message, 360), + detail = SensitiveText.Sanitize(entry.Detail, 800) + }, + display = new + { + level = LogDisplayLocalizer.Level(entry.Level, request.Language), + category = LogDisplayLocalizer.Category(entry.Category, request.Language), + message = LogDisplayLocalizer.Message(entry.Message, request.Language), + detail = LogDisplayLocalizer.Detail(entry.Detail, request.Language, 800) + } + }).ToArray(); + await WriteJsonEntryAsync(archive, $"logs-{createdAt:yyyyMMdd}.json", logs, cancellationToken).ConfigureAwait(false); + included[$"logs-{createdAt:yyyyMMdd}.json"] = $"当天日志 {logs.Length} 条"; + } + + if (!string.IsNullOrWhiteSpace(request.ScreenshotPath)) + { + var screenshot = ValidateScreenshot(request.ScreenshotPath); + var entryName = "screenshot" + screenshot.Extension.ToLowerInvariant(); + var entry = archive.CreateEntry(entryName, CompressionLevel.Optimal); + await using var input = File.OpenRead(screenshot.FullName); + await using var output = entry.Open(); + await input.CopyToAsync(output, cancellationToken).ConfigureAwait(false); + included[entryName] = $"截图 {screenshot.Length} bytes"; + } + + var summary = BuildSummary(request, included, createdAt); + await WriteTextEntryAsync(archive, "summary.txt", summary, cancellationToken).ConfigureAwait(false); + included["summary.txt"] = "反馈包摘要"; + } + + var sha256 = await HashFileAsync(packagePath, cancellationToken).ConfigureAwait(false); + var fileInfo = new FileInfo(packagePath); + return new FeedbackPackageResult( + packagePath, + sha256, + fileInfo.Length, + createdAt, + BuildSummary(request, included, createdAt), + included); + } + + private static void ValidateRequest(FeedbackRequest request) + { + if (string.IsNullOrWhiteSpace(request.Body)) + { + throw new InvalidOperationException("反馈正文不能为空。"); + } + } + + public static string FeedbackPackagesRoot() + { + return Path.Combine(AppContext.BaseDirectory, "FeedbackPackages"); + } + + private static FileInfo ValidateScreenshot(string path) + { + var info = new FileInfo(path.Trim().Trim('"')); + if (!info.Exists) + { + throw new FileNotFoundException("截图文件不存在。", info.FullName); + } + + if (!AllowedScreenshotExtensions.Contains(info.Extension, StringComparer.OrdinalIgnoreCase)) + { + throw new InvalidOperationException("截图仅支持 png、jpg、jpeg、webp、bmp。"); + } + + if (info.Length > MaxScreenshotBytes) + { + throw new InvalidOperationException("截图不能超过 5 MB。"); + } + + return info; + } + + private object CaptureSystemSummary() + { + var snapshot = systemMetricsService?.Capture(); + return new + { + machineName = SensitiveText.Sanitize(snapshot?.MachineName, 120), + osDescription = SensitiveText.Sanitize(snapshot?.OsDescription, 200), + processorCount = snapshot?.ProcessorCount ?? Environment.ProcessorCount, + workingSetBytes = snapshot?.WorkingSetBytes ?? Environment.WorkingSet, + managedMemoryBytes = snapshot?.ManagedMemoryBytes ?? GC.GetTotalMemory(false), + capturedAt = snapshot?.CapturedAt ?? DateTimeOffset.Now + }; + } + + private object CaptureToolStatus() + { + var catalog = toolCatalog ?? new ToolCatalog(); + return new + { + capturedAt = DateTimeOffset.Now, + totalTools = catalog.Modules.Count, + byCategory = catalog.Modules + .GroupBy(module => module.Metadata.Category.ToString()) + .OrderBy(group => group.Key, StringComparer.OrdinalIgnoreCase) + .Select(group => new { category = group.Key, count = group.Count() }) + .ToArray(), + newTools = new[] + { + "compression_codec", + "totp_generator", + "color_contrast_checker", + "url_redirect_trace", + "image_metadata_inspector", + "markdown_preview" + }.Select(id => new { id, exists = catalog.GetById(id) is not null }).ToArray() + }; + } + + private static string BuildSummary(FeedbackRequest request, IReadOnlyDictionary included, DateTimeOffset createdAt) + { + var english = string.Equals(request.Language, "en-US", StringComparison.OrdinalIgnoreCase); + var lines = new List + { + english ? "YMhut Box Feedback Package" : "YMhut Box 反馈包", + $"{(english ? "Created" : "创建时间")}: {createdAt:O}", + $"{(english ? "Feedback code" : "反馈编号")}: {FeedbackCode.Normalize(request.FeedbackCode)}", + $"{(english ? "Title" : "标题")}: {SensitiveText.Sanitize(request.Title, 160)}", + $"{(english ? "Type" : "类型")}: {request.Type}", + $"{(english ? "Severity" : "严重程度")}: {request.Severity}", + $"{(english ? "Contact" : "联系方式")}: {SensitiveText.Sanitize(request.Contact, 180)}", + $"{(english ? "Body" : "正文")}: {SensitiveText.Sanitize(request.Body, 1200)}", + string.Empty, + english ? "Included files:" : "包含文件:" + }; + lines.AddRange(included.Select(pair => $"- {pair.Key}: {pair.Value}")); + return string.Join(Environment.NewLine, lines); + } + + private static async Task WriteJsonEntryAsync(ZipArchive archive, string name, object value, CancellationToken cancellationToken) + { + var json = JsonSerializer.Serialize(value, JsonOptions); + await WriteTextEntryAsync(archive, name, json, cancellationToken).ConfigureAwait(false); + } + + private static async Task WriteTextEntryAsync(ZipArchive archive, string name, string text, CancellationToken cancellationToken) + { + var entry = archive.CreateEntry(name, CompressionLevel.Optimal); + await using var stream = entry.Open(); + await using var writer = new StreamWriter(stream, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)); + await writer.WriteAsync(text.AsMemory(), cancellationToken).ConfigureAwait(false); + } + + private static async Task HashFileAsync(string path, CancellationToken cancellationToken) + { + await using var stream = File.OpenRead(path); + var hash = await SHA256.HashDataAsync(stream, cancellationToken).ConfigureAwait(false); + return Convert.ToHexString(hash).ToLowerInvariant(); + } + + private static string RandomSuffix() + { + Span bytes = stackalloc byte[3]; + RandomNumberGenerator.Fill(bytes); + return Convert.ToHexString(bytes).ToLowerInvariant(); + } + + private static string NextAvailablePath(string path) + { + if (!File.Exists(path)) + { + return path; + } + + var directory = Path.GetDirectoryName(path) ?? Environment.CurrentDirectory; + var name = Path.GetFileNameWithoutExtension(path); + var extension = Path.GetExtension(path); + for (var index = 2; ; index++) + { + var candidate = Path.Combine(directory, $"{name}-{index}{extension}"); + if (!File.Exists(candidate)) + { + return candidate; + } + } + } +} diff --git a/src/YMhut.Box.Core/Feedback/FeedbackRecordStore.cs b/src/YMhut.Box.Core/Feedback/FeedbackRecordStore.cs new file mode 100644 index 0000000..8d04bfa --- /dev/null +++ b/src/YMhut.Box.Core/Feedback/FeedbackRecordStore.cs @@ -0,0 +1,287 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace YMhut.Box.Core.Feedback; + +public sealed class FeedbackRecordStore(string? storagePath = null) : IFeedbackRecordStore +{ + private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web) + { + WriteIndented = true, + Converters = { new JsonStringEnumConverter() } + }; + + private readonly SemaphoreSlim _gate = new(1, 1); + private readonly string _storagePath = string.IsNullOrWhiteSpace(storagePath) + ? DefaultStoragePath() + : storagePath; + + public static string DefaultStoragePath() + { + return Path.Combine(FeedbackPackageService.FeedbackPackagesRoot(), "feedback-records.json"); + } + + public async Task> ReadAsync(CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + return await ReadUnsafeAsync(cancellationToken).ConfigureAwait(false); + } + finally + { + _gate.Release(); + } + } + + public async Task FindAsync(string feedbackCode, CancellationToken cancellationToken = default) + { + var normalized = FeedbackCode.Normalize(feedbackCode); + if (!FeedbackCode.IsValid(normalized)) + { + return null; + } + + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + var records = await ReadUnsafeAsync(cancellationToken).ConfigureAwait(false); + return records.FirstOrDefault(record => string.Equals(record.Code, normalized, StringComparison.OrdinalIgnoreCase)); + } + finally + { + _gate.Release(); + } + } + + public async Task UpsertAsync(FeedbackRecord record, CancellationToken cancellationToken = default) + { + var normalized = FeedbackCode.Normalize(record.Code); + if (!FeedbackCode.IsValid(normalized)) + { + throw new InvalidOperationException("反馈编号无效。"); + } + + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + var records = (await ReadUnsafeAsync(cancellationToken).ConfigureAwait(false)).ToList(); + var index = records.FindIndex(item => string.Equals(item.Code, normalized, StringComparison.OrdinalIgnoreCase)); + var existing = index >= 0 ? records[index] : null; + var merged = Merge(existing, record, normalized); + if (index >= 0) + { + records[index] = merged; + } + else + { + records.Add(merged); + } + + await WriteUnsafeAsync(records, cancellationToken).ConfigureAwait(false); + return merged; + } + finally + { + _gate.Release(); + } + } + + public async Task RemoveAsync(string feedbackCode, CancellationToken cancellationToken = default) + { + var normalized = FeedbackCode.Normalize(feedbackCode); + if (!FeedbackCode.IsValid(normalized)) + { + return; + } + + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + var records = (await ReadUnsafeAsync(cancellationToken).ConfigureAwait(false)).ToList(); + var removed = records.RemoveAll(record => string.Equals(record.Code, normalized, StringComparison.OrdinalIgnoreCase)); + if (removed > 0) + { + await WriteUnsafeAsync(records, cancellationToken).ConfigureAwait(false); + } + } + finally + { + _gate.Release(); + } + } + + public async Task> ArchiveOlderThanAsync( + TimeSpan activeWindow, + DateTimeOffset? now = null, + CancellationToken cancellationToken = default) + { + var current = (now ?? DateTimeOffset.UtcNow).ToUniversalTime(); + var threshold = current - activeWindow; + + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + var changed = false; + var records = (await ReadUnsafeAsync(cancellationToken).ConfigureAwait(false)).ToList(); + for (var index = 0; index < records.Count; index++) + { + var record = Normalize(records[index]); + if (!record.IsArchived && record.CreatedAtUtc.ToUniversalTime() <= threshold) + { + record = record with + { + State = FeedbackRecordState.Archived, + StatusLabel = "已归档", + LastMessage = "该反馈记录已超过 10 天,已自动归档到本地历史。", + UpdatedAtUtc = current, + ArchivedAtUtc = current, + IsArchived = true + }; + changed = true; + } + + records[index] = record; + } + + if (changed) + { + await WriteUnsafeAsync(records, cancellationToken).ConfigureAwait(false); + } + + return Sort(records); + } + finally + { + _gate.Release(); + } + } + + private static FeedbackRecord Merge(FeedbackRecord? existing, FeedbackRecord record, string normalizedCode) + { + var normalized = Normalize(record with { Code = normalizedCode }); + if (existing is null) + { + return normalized; + } + + var archived = existing.IsArchived || normalized.IsArchived; + var archivedAt = normalized.ArchivedAtUtc ?? existing.ArchivedAtUtc; + return normalized with + { + CreatedAtUtc = existing.CreatedAtUtc <= normalized.CreatedAtUtc ? existing.CreatedAtUtc : normalized.CreatedAtUtc, + ArchivedAtUtc = archivedAt, + IsArchived = archived + }; + } + + private static FeedbackRecord Normalize(FeedbackRecord record) + { + var now = DateTimeOffset.UtcNow; + var created = record.CreatedAtUtc == default ? now : record.CreatedAtUtc.ToUniversalTime(); + var updated = record.UpdatedAtUtc == default ? created : record.UpdatedAtUtc.ToUniversalTime(); + var title = string.IsNullOrWhiteSpace(record.Title) ? "未命名反馈" : record.Title.Trim(); + var label = string.IsNullOrWhiteSpace(record.StatusLabel) ? StateLabel(record.State) : record.StatusLabel.Trim(); + var message = string.IsNullOrWhiteSpace(record.LastMessage) ? "暂无更多状态信息。" : record.LastMessage.Trim(); + + return record with + { + Code = FeedbackCode.Normalize(record.Code), + Title = title.Length > 160 ? title[..160] : title, + Type = string.IsNullOrWhiteSpace(record.Type) ? "issue" : record.Type.Trim(), + Severity = string.IsNullOrWhiteSpace(record.Severity) ? "normal" : record.Severity.Trim(), + CreatedAtUtc = created, + UpdatedAtUtc = updated, + StatusLabel = label.Length > 80 ? label[..80] : label, + LastMessage = message.Length > 500 ? message[..500] : message, + FailureCount = Math.Max(0, record.FailureCount), + PackageBytes = Math.Max(0, record.PackageBytes), + ServiceStatus = TrimOrNull(record.ServiceStatus, 80), + PublicReply = TrimOrNull(record.PublicReply, 800), + ServiceStatusDetail = TrimOrNull(record.ServiceStatusDetail, 500), + ServiceCategory = TrimOrNull(record.ServiceCategory, 80), + ServicePriority = TrimOrNull(record.ServicePriority, 80), + ServiceReceivedAt = TrimOrNull(record.ServiceReceivedAt, 80), + ServiceUpdatedAt = TrimOrNull(record.ServiceUpdatedAt, 80) + }; + } + + private static string? TrimOrNull(string? value, int maxLength) + { + if (string.IsNullOrWhiteSpace(value)) + { + return null; + } + + var trimmed = value.Trim(); + return trimmed.Length > maxLength ? trimmed[..maxLength] : trimmed; + } + + private static string StateLabel(FeedbackRecordState state) + { + return state switch + { + FeedbackRecordState.PackageCreated => "反馈包已生成", + FeedbackRecordState.PackageFailed => "生成失败", + FeedbackRecordState.Sending => "发送中", + FeedbackRecordState.Sent => "发送成功", + FeedbackRecordState.SendFailed => "发送失败", + FeedbackRecordState.Querying => "正在获取状态", + FeedbackRecordState.StatusUpdated => "状态已更新", + FeedbackRecordState.QueryFailed => "获取失败", + FeedbackRecordState.Archived => "已归档", + _ => "未发送" + }; + } + + private async Task> ReadUnsafeAsync(CancellationToken cancellationToken) + { + if (!File.Exists(_storagePath)) + { + return []; + } + + try + { + await using var stream = File.OpenRead(_storagePath); + var records = await JsonSerializer.DeserializeAsync>(stream, JsonOptions, cancellationToken) + .ConfigureAwait(false); + return Sort(records?.Select(Normalize) ?? []); + } + catch (JsonException) + { + return []; + } + catch (IOException) + { + return []; + } + } + + private async Task WriteUnsafeAsync(IReadOnlyList records, CancellationToken cancellationToken) + { + var folder = Path.GetDirectoryName(_storagePath); + if (!string.IsNullOrWhiteSpace(folder)) + { + Directory.CreateDirectory(folder); + } + + var ordered = Sort(records.Select(Normalize)); + var tempPath = _storagePath + ".tmp"; + await using (var stream = File.Create(tempPath)) + { + await JsonSerializer.SerializeAsync(stream, ordered, JsonOptions, cancellationToken).ConfigureAwait(false); + } + + File.Move(tempPath, _storagePath, overwrite: true); + } + + private static IReadOnlyList Sort(IEnumerable records) + { + return records + .OrderBy(record => record.IsArchived) + .ThenByDescending(record => record.UpdatedAtUtc) + .ThenByDescending(record => record.CreatedAtUtc) + .ToArray(); + } +} diff --git a/src/YMhut.Box.Core/Feedback/FeedbackSubmissionService.cs b/src/YMhut.Box.Core/Feedback/FeedbackSubmissionService.cs new file mode 100644 index 0000000..332ff5b --- /dev/null +++ b/src/YMhut.Box.Core/Feedback/FeedbackSubmissionService.cs @@ -0,0 +1,172 @@ +using System.Net.Http.Headers; +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; + +namespace YMhut.Box.Core.Feedback; + +public sealed class FeedbackSubmissionService(HttpClient? httpClient = null) : IFeedbackSubmissionService +{ + public const string Endpoint = "https://mail-smtp.ymhut.cn/"; + public const string ClientSignatureKey = "ymhut-box-feedback-client-v1"; + private readonly HttpClient _httpClient = httpClient ?? new HttpClient { Timeout = TimeSpan.FromSeconds(30) }; + + public async Task SubmitAsync( + FeedbackRequest request, + FeedbackPackageResult package, + CancellationToken cancellationToken = default) + { + try + { + if (!File.Exists(package.PackagePath)) + { + return new FeedbackSubmissionResponse(false, null, "PACKAGE_MISSING", "本地反馈包不存在。"); + } + + var encrypted = await FeedbackPackageCrypto.EncryptPackageAsync(package.PackagePath, cancellationToken: cancellationToken) + .ConfigureAwait(false); + var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(global::System.Globalization.CultureInfo.InvariantCulture); + var nonce = Guid.NewGuid().ToString("N"); + var feedbackCode = FeedbackCode.Normalize(request.FeedbackCode); + var payload = JsonSerializer.Serialize(new + { + feedbackCode, + title = request.Title, + type = request.Type, + severity = request.Severity, + contact = request.Contact, + bodyLength = request.Body?.Length ?? 0, + packageEncrypted = true, + encryption = FeedbackPackageCrypto.MagicText, + packageBytes = encrypted.PackageBytes, + packageSha256 = encrypted.PackageSha256, + plainPackageBytes = package.PackageBytes, + plainPackageSha256 = package.PackageSha256, + createdAt = package.CreatedAt + }, new JsonSerializerOptions(JsonSerializerDefaults.Web)); + var signature = Sign(timestamp, nonce, encrypted.PackageSha256, payload); + + using var form = new MultipartFormDataContent(); + form.Add(new StringContent(payload, Encoding.UTF8, "application/json"), "payload"); + form.Add(new StringContent(timestamp, Encoding.UTF8), "timestamp"); + form.Add(new StringContent(nonce, Encoding.UTF8), "nonce"); + form.Add(new StringContent(encrypted.PackageSha256, Encoding.UTF8), "packageSha256"); + form.Add(new StringContent(signature, Encoding.UTF8), "signature"); + await using var stream = File.OpenRead(encrypted.PackagePath); + using var fileContent = new StreamContent(stream); + fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); + form.Add(fileContent, "package", Path.GetFileName(encrypted.PackagePath)); + + using var response = await _httpClient.PostAsync(Endpoint, form, cancellationToken).ConfigureAwait(false); + var text = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); + var parsed = ParseResponse(text); + if (response.IsSuccessStatusCode) + { + return parsed; + } + + return parsed with + { + Ok = false, + ErrorCode = parsed.ErrorCode ?? $"HTTP_{(int)response.StatusCode}", + Message = parsed.Message ?? response.ReasonPhrase + }; + } + catch (Exception exception) when (exception is HttpRequestException or TaskCanceledException or OperationCanceledException) + { + return new FeedbackSubmissionResponse(false, null, exception is TaskCanceledException ? "TIMEOUT" : "NETWORK_ERROR", exception.Message); + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException or InvalidDataException or CryptographicException) + { + return new FeedbackSubmissionResponse(false, null, "PACKAGE_ENCRYPT_FAILED", exception.Message); + } + } + + public async Task GetStatusAsync( + string feedbackCode, + CancellationToken cancellationToken = default) + { + var normalized = FeedbackCode.Normalize(feedbackCode); + if (!FeedbackCode.IsValid(normalized)) + { + return new FeedbackStatusResponse(false, normalized, null, null, false, null, null, null, "INVALID_CODE", "Invalid feedback code."); + } + + try + { + var uri = $"{Endpoint}?api=status&code={Uri.EscapeDataString(normalized)}"; + using var response = await _httpClient.GetAsync(uri, cancellationToken).ConfigureAwait(false); + var text = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); + var parsed = ParseStatusResponse(text); + if (response.IsSuccessStatusCode) + { + return parsed; + } + + return parsed with + { + Ok = false, + ErrorCode = parsed.ErrorCode ?? $"HTTP_{(int)response.StatusCode}", + Message = parsed.Message ?? response.ReasonPhrase + }; + } + catch (Exception exception) when (exception is HttpRequestException or TaskCanceledException or OperationCanceledException) + { + return new FeedbackStatusResponse(false, normalized, null, null, false, null, null, null, exception is TaskCanceledException ? "TIMEOUT" : "NETWORK_ERROR", exception.Message); + } + } + + public static string Sign(string timestamp, string nonce, string packageSha256, string payload) + { + var material = $"{timestamp}\n{nonce}\n{packageSha256}\n{payload}"; + using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(ClientSignatureKey)); + return Convert.ToHexString(hmac.ComputeHash(Encoding.UTF8.GetBytes(material))).ToLowerInvariant(); + } + + private static FeedbackSubmissionResponse ParseResponse(string text) + { + try + { + using var document = JsonDocument.Parse(text); + var root = document.RootElement; + var ok = root.TryGetProperty("ok", out var okElement) && okElement.ValueKind == JsonValueKind.True; + var code = root.TryGetProperty("code", out var codeElement) ? codeElement.GetString() : null; + var error = root.TryGetProperty("error", out var errorElement) ? errorElement.GetString() : null; + var message = root.TryGetProperty("message", out var messageElement) ? messageElement.GetString() : null; + var duplicate = root.TryGetProperty("duplicate", out var duplicateElement) && duplicateElement.ValueKind == JsonValueKind.True; + return new FeedbackSubmissionResponse(ok, code, error, message, duplicate); + } + catch (JsonException) + { + return new FeedbackSubmissionResponse(false, null, "INVALID_RESPONSE", text); + } + } + + private static FeedbackStatusResponse ParseStatusResponse(string text) + { + try + { + using var document = JsonDocument.Parse(text); + var root = document.RootElement; + var ok = root.TryGetProperty("ok", out var okElement) && okElement.ValueKind == JsonValueKind.True; + var code = root.TryGetProperty("code", out var codeElement) ? codeElement.GetString() : null; + var status = root.TryGetProperty("status", out var statusElement) ? statusElement.GetString() : null; + var statusLabel = root.TryGetProperty("statusLabel", out var statusLabelElement) ? statusLabelElement.GetString() : null; + var statusDetail = root.TryGetProperty("statusDetail", out var statusDetailElement) ? statusDetailElement.GetString() : null; + var category = root.TryGetProperty("category", out var categoryElement) ? categoryElement.GetString() : null; + var priority = root.TryGetProperty("priority", out var priorityElement) ? priorityElement.GetString() : null; + var hasReply = root.TryGetProperty("hasReply", out var hasReplyElement) && hasReplyElement.ValueKind == JsonValueKind.True; + var reply = root.TryGetProperty("reply", out var replyElement) ? replyElement.GetString() : null; + var receivedAt = root.TryGetProperty("receivedAt", out var receivedAtElement) ? receivedAtElement.GetString() : null; + var updatedAt = root.TryGetProperty("updatedAt", out var updatedAtElement) ? updatedAtElement.GetString() : null; + var mailSent = root.TryGetProperty("mailSent", out var mailSentElement) && mailSentElement.ValueKind == JsonValueKind.True; + var error = root.TryGetProperty("error", out var errorElement) ? errorElement.GetString() : null; + var message = root.TryGetProperty("message", out var messageElement) ? messageElement.GetString() : null; + return new FeedbackStatusResponse(ok, code, status, statusLabel, hasReply, reply, receivedAt, updatedAt, error, message, statusDetail, category, priority, mailSent); + } + catch (JsonException) + { + return new FeedbackStatusResponse(false, null, null, null, false, null, null, null, "INVALID_RESPONSE", text); + } + } +} diff --git a/src/YMhut.Box.Core/Logging/ILogService.cs b/src/YMhut.Box.Core/Logging/ILogService.cs new file mode 100644 index 0000000..f678790 --- /dev/null +++ b/src/YMhut.Box.Core/Logging/ILogService.cs @@ -0,0 +1,47 @@ +namespace YMhut.Box.Core.Logging; + +public interface ILogService +{ + event EventHandler? EntryWritten; + + string LogPath { get; } + + Task WriteAsync( + string level, + string category, + string message, + string? detail = null, + CancellationToken cancellationToken = default); + + Task> ReadAsync( + string? level = null, + string? category = null, + int take = 500, + CancellationToken cancellationToken = default); + + Task> ReadByDateAsync( + DateOnly date, + string? level = null, + string? query = null, + int take = 0, + CancellationToken cancellationToken = default); + + Task> ReadByDatePageAsync( + DateOnly date, + string? level = null, + string? query = null, + int skip = 0, + int take = 100, + CancellationToken cancellationToken = default); + + Task CleanupAsync(int retentionCount, CancellationToken cancellationToken = default); + + Task ClearAllAsync(CancellationToken cancellationToken = default); + + Task ClearTodayAsync(CancellationToken cancellationToken = default); + + Task ClearFilteredTodayAsync( + string? level = null, + string? query = null, + CancellationToken cancellationToken = default); +} diff --git a/src/YMhut.Box.Core/Logging/JsonFileLogService.cs b/src/YMhut.Box.Core/Logging/JsonFileLogService.cs new file mode 100644 index 0000000..4a317f5 --- /dev/null +++ b/src/YMhut.Box.Core/Logging/JsonFileLogService.cs @@ -0,0 +1,307 @@ +using System.Text.Json; +using YMhut.Box.Core.App; + +namespace YMhut.Box.Core.Logging; + +public sealed class JsonFileLogService : ILogService +{ + private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web); + private readonly SemaphoreSlim _gate = new(1, 1); + + public JsonFileLogService(AppPaths paths) + { + paths.EnsureCreated(); + LogPath = Path.Combine(paths.Logs, "app-log.jsonl"); + } + + public event EventHandler? EntryWritten; + + public string LogPath { get; } + + public async Task WriteAsync( + string level, + string category, + string message, + string? detail = null, + CancellationToken cancellationToken = default) + { + var entry = new LogEntry(DateTimeOffset.Now, level, category, message, detail); + var line = JsonSerializer.Serialize(entry, JsonOptions); + + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + Directory.CreateDirectory(Path.GetDirectoryName(LogPath)!); + await File.AppendAllTextAsync(LogPath, line + Environment.NewLine, cancellationToken).ConfigureAwait(false); + } + finally + { + _gate.Release(); + } + + EntryWritten?.Invoke(this, entry); + } + + public async Task> ReadAsync( + string? level = null, + string? category = null, + int take = 500, + CancellationToken cancellationToken = default) + { + if (!File.Exists(LogPath)) + { + return []; + } + + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + var entries = new List(); + foreach (var line in await File.ReadAllLinesAsync(LogPath, cancellationToken).ConfigureAwait(false)) + { + if (string.IsNullOrWhiteSpace(line)) + { + continue; + } + + try + { + var entry = JsonSerializer.Deserialize(line, JsonOptions); + if (entry is not null) + { + entries.Add(entry); + } + } + catch (JsonException) + { + } + } + + return entries + .Where(entry => string.IsNullOrWhiteSpace(level) || string.Equals(entry.Level, level, StringComparison.OrdinalIgnoreCase)) + .Where(entry => string.IsNullOrWhiteSpace(category) || string.Equals(entry.Category, category, StringComparison.OrdinalIgnoreCase)) + .OrderByDescending(entry => entry.Timestamp) + .Take(Math.Clamp(take, 1, 5000)) + .ToList(); + } + finally + { + _gate.Release(); + } + } + + public async Task> ReadByDateAsync( + DateOnly date, + string? level = null, + string? query = null, + int take = 0, + CancellationToken cancellationToken = default) + { + var filtered = await ReadByDateFilteredAsync(date, level, query, cancellationToken).ConfigureAwait(false); + return take > 0 ? filtered.Take(take).ToList() : filtered.ToList(); + } + + public async Task> ReadByDatePageAsync( + DateOnly date, + string? level = null, + string? query = null, + int skip = 0, + int take = 100, + CancellationToken cancellationToken = default) + { + var filtered = await ReadByDateFilteredAsync(date, level, query, cancellationToken).ConfigureAwait(false); + return filtered + .Skip(Math.Max(0, skip)) + .Take(Math.Clamp(take, 1, 5000)) + .ToList(); + } + + private async Task> ReadByDateFilteredAsync( + DateOnly date, + string? level, + string? query, + CancellationToken cancellationToken) + { + var entries = await ReadAllEntriesAsync(cancellationToken).ConfigureAwait(false); + var normalizedQuery = query?.Trim() ?? string.Empty; + return entries + .Where(entry => DateOnly.FromDateTime(entry.Timestamp.LocalDateTime) == date) + .Where(entry => string.IsNullOrWhiteSpace(level) || string.Equals(entry.Level, level, StringComparison.OrdinalIgnoreCase)) + .Where(entry => string.IsNullOrWhiteSpace(normalizedQuery) + || entry.Message.Contains(normalizedQuery, StringComparison.OrdinalIgnoreCase) + || entry.Category.Contains(normalizedQuery, StringComparison.OrdinalIgnoreCase) + || (entry.Detail?.Contains(normalizedQuery, StringComparison.OrdinalIgnoreCase) ?? false)) + .OrderByDescending(entry => entry.Timestamp) + .ToList(); + } + + private async Task> ReadAllEntriesAsync(CancellationToken cancellationToken) + { + if (!File.Exists(LogPath)) + { + return []; + } + + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + var entries = new List(); + foreach (var line in await File.ReadAllLinesAsync(LogPath, cancellationToken).ConfigureAwait(false)) + { + if (string.IsNullOrWhiteSpace(line)) + { + continue; + } + + try + { + var entry = JsonSerializer.Deserialize(line, JsonOptions); + if (entry is not null) + { + entries.Add(entry); + } + } + catch (JsonException) + { + } + } + + return entries; + } + finally + { + _gate.Release(); + } + } + + public async Task CleanupAsync(int retentionCount, CancellationToken cancellationToken = default) + { + if (!File.Exists(LogPath)) + { + return; + } + + var keep = Math.Max(0, retentionCount); + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + var lines = await File.ReadAllLinesAsync(LogPath, cancellationToken).ConfigureAwait(false); + var retained = keep == 0 ? [] : lines.TakeLast(keep).ToArray(); + await File.WriteAllLinesAsync(LogPath, retained, cancellationToken).ConfigureAwait(false); + } + finally + { + _gate.Release(); + } + } + + public async Task ClearAllAsync(CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + if (File.Exists(LogPath)) + { + File.Delete(LogPath); + } + } + finally + { + _gate.Release(); + } + } + + public async Task ClearTodayAsync(CancellationToken cancellationToken = default) + { + if (!File.Exists(LogPath)) + { + return; + } + + var today = DateTimeOffset.Now.Date; + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + var retained = new List(); + foreach (var line in await File.ReadAllLinesAsync(LogPath, cancellationToken).ConfigureAwait(false)) + { + try + { + var entry = JsonSerializer.Deserialize(line, JsonOptions); + if (entry is null || entry.Timestamp.Date != today) + { + retained.Add(line); + } + } + catch (JsonException) + { + retained.Add(line); + } + } + + await File.WriteAllLinesAsync(LogPath, retained, cancellationToken).ConfigureAwait(false); + } + finally + { + _gate.Release(); + } + } + + public async Task ClearFilteredTodayAsync( + string? level = null, + string? query = null, + CancellationToken cancellationToken = default) + { + if (!File.Exists(LogPath)) + { + return; + } + + var today = DateTimeOffset.Now.Date; + var normalizedQuery = query?.Trim() ?? string.Empty; + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + var retained = new List(); + foreach (var line in await File.ReadAllLinesAsync(LogPath, cancellationToken).ConfigureAwait(false)) + { + try + { + var entry = JsonSerializer.Deserialize(line, JsonOptions); + if (entry is null || !Matches(entry)) + { + retained.Add(line); + } + } + catch (JsonException) + { + retained.Add(line); + } + } + + await File.WriteAllLinesAsync(LogPath, retained, cancellationToken).ConfigureAwait(false); + } + finally + { + _gate.Release(); + } + + bool Matches(LogEntry entry) + { + if (entry.Timestamp.Date != today) + { + return false; + } + + if (!string.IsNullOrWhiteSpace(level) && !string.Equals(entry.Level, level, StringComparison.OrdinalIgnoreCase)) + { + return false; + } + + return string.IsNullOrWhiteSpace(normalizedQuery) + || entry.Message.Contains(normalizedQuery, StringComparison.OrdinalIgnoreCase) + || entry.Category.Contains(normalizedQuery, StringComparison.OrdinalIgnoreCase) + || (entry.Detail?.Contains(normalizedQuery, StringComparison.OrdinalIgnoreCase) ?? false); + } + } +} diff --git a/src/YMhut.Box.Core/Logging/LogDisplayLocalizer.cs b/src/YMhut.Box.Core/Logging/LogDisplayLocalizer.cs new file mode 100644 index 0000000..015ca72 --- /dev/null +++ b/src/YMhut.Box.Core/Logging/LogDisplayLocalizer.cs @@ -0,0 +1,370 @@ +using System.Text.RegularExpressions; +using YMhut.Box.Core; + +namespace YMhut.Box.Core.Logging; + +public static class LogDisplayLocalizer +{ + public static string Level(string? level, string language = "zh-CN") + { + var english = English(language); + return level?.Trim() switch + { + "Trace" => english ? "Trace" : "跟踪", + "Debug" => english ? "Debug" : "调试", + "Information" => english ? "Info" : "信息", + "Warning" => english ? "Warning" : "警告", + "Error" => english ? "Error" : "错误", + "Critical" => english ? "Critical" : "严重", + _ => english ? "Info" : "信息" + }; + } + + public static string Category(string? category, string language = "zh-CN") + { + var english = English(language); + return (category ?? string.Empty).Trim().ToLowerInvariant() switch + { + "navigation" => english ? "Navigation" : "导航", + "tool" => english ? "Tool" : "工具", + "network" => english ? "Network" : "网络", + "api" => english ? "API" : "接口", + "solar" => english ? "Solar system" : "太阳系", + "service" => english ? "Service" : "服务", + "about" => english ? "About" : "关于", + "download" => english ? "Download" : "下载", + "risk" => english ? "Risk" : "风险确认", + "dev-env" => english ? "Development environment" : "开发环境", + "tool-run" => english ? "Tool run" : "工具运行", + "builtin-tool" => english ? "Built-in tool" : "内置工具", + "tool-result" => english ? "Tool result" : "工具结果", + "tool-metadata" => english ? "Tool metadata" : "工具元数据", + "plugin" => english ? "Plugin" : "插件", + "plugin-host" => english ? "Plugin host" : "插件宿主", + "update" => english ? "Update" : "更新", + "settings" => english ? "Settings" : "设置", + "feedback" => english ? "Feedback" : "反馈", + "safe_browser" => english ? "Safe browser" : "安全浏览器", + "media_player" => english ? "Media player" : "媒体播放器", + "window" => english ? "Window" : "窗口", + "system" => english ? "System" : "系统", + "startup" => english ? "Startup" : "启动", + "startup-check" => english ? "Startup check" : "启动自检", + "version" => english ? "Version" : "版本", + "worker" => english ? "Worker" : "工具进程", + "" => english ? "General" : "通用", + var value => Humanize(value) + }; + } + + public static string Message(string? message, string language = "zh-CN") + { + var text = SensitiveText.Sanitize(message, 260); + var english = English(language); + var normalized = text.Trim(); + var exact = normalized.ToLowerInvariant() switch + { + "open home page" => english ? "Open home page" : "打开首页", + "open toolbox" => english ? "Open toolbox" : "打开工具箱", + "open logs" => english ? "Open logs" : "打开日志", + "open service status" => english ? "Open service status" : "打开服务状态", + "open startup check results" => english ? "Open startup check results" : "打开启动自检结果", + "open download manager" => english ? "Open download manager" : "打开下载管理", + "open plugin docs" => english ? "Open plugin docs" : "打开插件文档", + "open settings" => english ? "Open settings" : "打开设置", + "open about" => english ? "Open about" : "打开关于", + "open plugins" => english ? "Open plugins" : "打开插件页", + "open feedback" => english ? "Open feedback" : "打开反馈", + "tool catalog rebuilt" => english ? "Tool catalog rebuilt" : "工具目录已重建", + "external tools scanned" => english ? "External tools scanned" : "外部工具已扫描", + "user launched downloaded installer" => english ? "User launched downloaded installer" : "用户打开已下载的安装包", + "external tool icon extracted" => english ? "External tool icon extracted" : "已提取外部工具图标", + "tool worker process started" => english ? "Tool worker process started" : "工具工作进程已启动", + "tool worker ready" => english ? "Tool worker ready" : "工具工作进程已就绪", + "tool worker unavailable; using in-process fallback" => english ? "Tool worker unavailable; using in-process fallback" : "工具工作进程不可用,已改用进程内执行", + "tool worker execution failed" => english ? "Tool worker execution failed" : "工具工作进程执行失败", + "download host process started" => english ? "Download host process started" : "下载宿主进程已启动", + "download host read loop stopped" => english ? "Download host read loop stopped" : "下载宿主读取循环已停止", + "download started" => english ? "Download started" : "下载已开始", + "download completed" => english ? "Download completed" : "下载已完成", + "download completed and verified" => english ? "Download completed and verified" : "下载已完成并通过校验", + "installer package cleaned" => english ? "Installer package cleaned" : "安装包已清理", + "installer cleanup failed" => english ? "Installer cleanup failed" : "安装包清理失败", + "open source references loaded" => english ? "Open source references loaded" : "开源引用已加载", + "open source reference scan degraded" => english ? "Open source reference scan degraded" : "开源引用扫描已降级", + "external tool metadata load failed" => english ? "External tool metadata load failed" : "外部工具元数据加载失败", + "startup check found missing critical files or language resources. please reinstall with the latest setup package." => english ? "Startup check found missing critical files or language resources. Please reinstall with the latest setup package." : "启动自检发现关键文件或语言资源缺失,请使用最新安装包重新安装。", + "toolbox layout changed" => english ? "Toolbox layout changed" : "工具箱布局已更改", + "toolbox default scope changed" => english ? "Toolbox default scope changed" : "工具箱默认范围已更改", + "toolbox compact cards changed" => english ? "Toolbox compact cards changed" : "工具箱紧凑卡片设置已更改", + "toolbox recent-first changed" => english ? "Toolbox recent-first changed" : "工具箱最近优先设置已更改", + "hardware brand logo setting changed" => english ? "Hardware brand logo setting changed" : "硬件品牌标识设置已更改", + "risk confirmation setting changed" => english ? "Risk confirmation setting changed" : "风险确认设置已更改", + "risk confirmation reset cancelled" => english ? "Risk confirmation reset cancelled" : "风险确认重置已取消", + "risk confirmations reset from settings" => english ? "Risk confirmations reset from settings" : "已从设置重置风险确认", + "builtin tool execution cancelled" => english ? "Built-in tool execution cancelled" : "已取消内置工具执行", + "external tool launch cancelled" => english ? "External tool launch cancelled" : "已取消外部工具启动", + "builtin tool risk confirmed" => english ? "Built-in tool risk confirmed" : "已确认内置工具风险", + "external tool risk confirmed" => english ? "External tool risk confirmed" : "已确认外部工具风险", + "plugin validation failed" => english ? "Plugin validation failed" : "插件校验失败", + "plugin surface opened" => english ? "Plugin surface opened" : "插件页面已打开", + "plugin surface opened in window" => english ? "Plugin surface opened in window" : "插件窗口已打开", + "plugin host failed" => english ? "Plugin host failed" : "插件宿主异常", + "plugin window failed" => english ? "Plugin window failed" : "插件窗口异常", + "load plugin snapshot failed" => english ? "Failed to load plugin snapshot" : "插件快照加载失败", + "reload plugin catalog failed" => english ? "Failed to reload plugin catalog" : "插件目录刷新失败", + "plugin host ready" => english ? "Plugin host ready" : "插件宿主已就绪", + "plugin host process started" => english ? "Plugin host process started" : "插件宿主进程已启动", + "plugin host request timed out" => english ? "Plugin host request timed out" : "插件宿主请求超时", + "plugin host request failed" => english ? "Plugin host request failed" : "插件宿主请求失败", + "plugin host pipe failed" => english ? "Plugin host pipe failed" : "插件宿主管道异常", + "plugin background refresh failed" => english ? "Plugin background refresh failed" : "插件后台刷新失败", + "plugin watcher failed" => english ? "Plugin watcher failed" : "插件目录监听失败", + "plugin hot reload" => english ? "Plugin hot reload" : "插件热刷新", + "plugin hot reload failed" => english ? "Plugin hot reload failed" : "插件热刷新失败", + "plugin scan failed" => english ? "Plugin scan failed" : "插件扫描失败", + "plugin host failed to start" => english ? "Plugin host failed to start" : "插件宿主启动失败", + "plugin http fetch" => english ? "Plugin HTTP fetch" : "插件 HTTP 请求", + "plugin ran built-in tool" => english ? "Plugin ran built-in tool" : "插件调用了内置工具", + "feedback sent" => english ? "Feedback sent" : "反馈已发送", + "feedback send failed" => english ? "Feedback send failed" : "反馈发送失败", + "update check failed" => english ? "Update check failed" : "检查更新失败", + "update notice check failed" => english ? "Update notice check failed" : "\u66f4\u65b0\u901a\u77e5\u68c0\u67e5\u5931\u8d25", + "service status check completed" => english ? "Service status check completed" : "\u670d\u52a1\u72b6\u6001\u68c0\u67e5\u5b8c\u6210", + "install integrity check completed" => english ? "Install integrity check completed" : "\u5b89\u88c5\u5b8c\u6574\u6027\u68c0\u67e5\u5b8c\u6210", + "install integrity check failed" => english ? "Install integrity check failed" : "\u5b89\u88c5\u5b8c\u6574\u6027\u68c0\u67e5\u5931\u8d25", + "monthly install integrity check failed" => english ? "Monthly install integrity check failed" : "\u6708\u5ea6\u5b89\u88c5\u5b8c\u6574\u6027\u68c0\u67e5\u5931\u8d25", + "startup check result preload failed" => english ? "Startup check result preload failed" : "\u542f\u52a8\u81ea\u68c0\u7ed3\u679c\u9884\u52a0\u8f7d\u5931\u8d25", + "startup check report load failed" => english ? "Startup check report load failed" : "\u542f\u52a8\u81ea\u68c0\u62a5\u544a\u8bfb\u53d6\u5931\u8d25", + "settings page initialized" => english ? "Settings page initialized" : "\u8bbe\u7f6e\u9875\u5df2\u521d\u59cb\u5316", + "settings page load failed" => english ? "Settings page load failed" : "\u8bbe\u7f6e\u9875\u52a0\u8f7d\u5931\u8d25", + "close confirmation remembered" => english ? "Close confirmation remembered" : "\u5df2\u8bb0\u4f4f\u5173\u95ed\u786e\u8ba4\u9009\u62e9", + "window close requested" => english ? "Window close requested" : "\u7528\u6237\u8bf7\u6c42\u5173\u95ed\u7a97\u53e3", + "window close cancelled" => english ? "Window close cancelled" : "\u5df2\u53d6\u6d88\u5173\u95ed\u7a97\u53e3", + "window minimized to tray" => english ? "Window minimized to tray" : "\u7a97\u53e3\u5df2\u6700\u5c0f\u5316\u5230\u6258\u76d8", + "proxy diagnostic failed" => english ? "Proxy diagnostic failed" : "代理诊断失败", + _ => string.Empty + }; + if (!string.IsNullOrEmpty(exact)) + { + return exact; + } + + var toolMatch = Regex.Match(normalized, @"^Open tool:\s*(?.+)$", RegexOptions.IgnoreCase); + if (toolMatch.Success) + { + return english ? $"Open tool: {toolMatch.Groups["tool"].Value}" : $"打开工具:{toolMatch.Groups["tool"].Value}"; + } + + var externalToolMatch = Regex.Match(normalized, @"^Open external tool:\s*(?.+)$", RegexOptions.IgnoreCase); + if (externalToolMatch.Success) + { + return english ? $"Open external tool: {externalToolMatch.Groups["tool"].Value}" : $"打开外部工具:{externalToolMatch.Groups["tool"].Value}"; + } + + var builtinToolMatch = Regex.Match(normalized, @"^Open built-in reference tool:\s*(?.+)$", RegexOptions.IgnoreCase); + if (builtinToolMatch.Success) + { + return english ? $"Open built-in reference tool: {builtinToolMatch.Groups["tool"].Value}" : $"打开内置工具:{builtinToolMatch.Groups["tool"].Value}"; + } + + var workerQueueMatch = Regex.Match(normalized, @"^Queued tool in worker:\s*(?.+)$", RegexOptions.IgnoreCase); + if (workerQueueMatch.Success) + { + return english ? $"Queued tool in worker: {workerQueueMatch.Groups["tool"].Value}" : $"工具已加入工作进程队列:{workerQueueMatch.Groups["tool"].Value}"; + } + + var workerExecuteMatch = Regex.Match(normalized, @"^Executing tool:\s*(?.+)$", RegexOptions.IgnoreCase); + if (workerExecuteMatch.Success) + { + return english ? $"Executing tool: {workerExecuteMatch.Groups["tool"].Value}" : $"正在执行工具:{workerExecuteMatch.Groups["tool"].Value}"; + } + + var pluginSurfaceMatch = Regex.Match(normalized, @"^Open plugin surface:\s*(?.+)$", RegexOptions.IgnoreCase); + if (pluginSurfaceMatch.Success) + { + return english ? $"Open plugin surface: {pluginSurfaceMatch.Groups["surface"].Value}" : $"打开插件页面:{pluginSurfaceMatch.Groups["surface"].Value}"; + } + + var externalRunMatch = Regex.Match(normalized, @"^External tool (?success|failed|canceled|cancelled|timeout)(?::\s*(?.+))?$", RegexOptions.IgnoreCase); + if (externalRunMatch.Success) + { + var result = ResultText(externalRunMatch.Groups["result"].Value, english); + var operationValue = externalRunMatch.Groups["operation"].Value; + if (string.IsNullOrWhiteSpace(operationValue)) + { + return english ? $"External tool {result}" : $"外部工具{result}"; + } + + var operation = OperationText(operationValue, english); + return english ? $"External tool {result}: {operation}" : $"外部工具{result}:{operation}"; + } + + var builtinRunMatch = Regex.Match(normalized, @"^Builtin tool (?success|failed|canceled|cancelled|timeout)(?::\s*(?.+))?$", RegexOptions.IgnoreCase); + if (builtinRunMatch.Success) + { + var result = ResultText(builtinRunMatch.Groups["result"].Value, english); + if (string.IsNullOrWhiteSpace(builtinRunMatch.Groups["tool"].Value)) + { + return english ? $"Built-in tool {result}" : $"内置工具{result}"; + } + + return english ? $"Built-in tool {result}: {builtinRunMatch.Groups["tool"].Value}" : $"内置工具{result}:{builtinRunMatch.Groups["tool"].Value}"; + } + + var toolResultMatch = Regex.Match(normalized, @"^Tool result (?[^:]+):\s*(?.+)$", RegexOptions.IgnoreCase); + if (toolResultMatch.Success) + { + var result = ResultText(toolResultMatch.Groups["result"].Value, english); + var operation = OperationText(toolResultMatch.Groups["operation"].Value, english); + return english ? $"Tool result {result}: {operation}" : $"工具结果{result}:{operation}"; + } + + var detectMatch = Regex.Match(normalized, @"^Detect (?.+) failed$", RegexOptions.IgnoreCase); + if (detectMatch.Success) + { + return english ? $"Detect {detectMatch.Groups["id"].Value} failed" : $"检测失败:{detectMatch.Groups["id"].Value}"; + } + + var fetchVersionsMatch = Regex.Match(normalized, @"^Fetch versions failed:\s*(?.+)$", RegexOptions.IgnoreCase); + if (fetchVersionsMatch.Success) + { + return english ? $"Fetch versions failed: {fetchVersionsMatch.Groups["id"].Value}" : $"获取版本失败:{fetchVersionsMatch.Groups["id"].Value}"; + } + + var startupMissingMatch = Regex.Match(normalized, @"^Startup check found (?\d+) critical missing items\. Open Service status for details\.$", RegexOptions.IgnoreCase); + if (startupMissingMatch.Success) + { + return english + ? $"Startup check found {startupMissingMatch.Groups["count"].Value} critical missing items. Open Service status for details." + : $"启动自检发现 {startupMissingMatch.Groups["count"].Value} 个关键缺失项,请在服务状态中查看详情。"; + } + + var navigationMatch = Regex.Match(normalized, @"^Navigation failed:\s*(?.+)$", RegexOptions.IgnoreCase); + if (navigationMatch.Success) + { + return english ? $"Navigation failed: {navigationMatch.Groups["page"].Value}" : $"页面打开失败:{navigationMatch.Groups["page"].Value}"; + } + + var genericOpenMatch = Regex.Match(normalized, @"^Open\s+(?.+)$", RegexOptions.IgnoreCase); + if (genericOpenMatch.Success) + { + return english ? $"Open {genericOpenMatch.Groups["target"].Value}" : $"打开 {genericOpenMatch.Groups["target"].Value}"; + } + + return text; + } + + public static string Detail(string? detail, string language = "zh-CN", int maxLength = 600) + { + if (string.IsNullOrWhiteSpace(detail)) + { + return English(language) ? "No details" : "暂无详情"; + } + + var safe = SensitiveText.Sanitize(detail, maxLength); + if (English(language)) + { + return safe; + } + + return LocalizeDetailPayload(safe); + } + + private static bool English(string? language) => string.Equals(language, "en-US", StringComparison.OrdinalIgnoreCase); + + private static string LocalizeDetailPayload(string detail) + { + var result = detail; + foreach (var (source, target) in new (string Source, string Target)[] + { + ("result=success", "结果=成功"), + ("result=failed", "结果=失败"), + ("result=canceled", "结果=已取消"), + ("operation=launch-admin", "操作=管理员启动"), + ("operation=launch", "操作=启动"), + ("operation=open-directory", "操作=打开目录"), + ("operation=execute", "操作=执行"), + ("operation=render", "操作=渲染"), + ("operation=export", "操作=导出"), + ("elapsedMs=", "耗时毫秒="), + ("durationMs=", "耗时毫秒="), + ("chars=", "字符数="), + ("blocks=", "结果块="), + ("error=", "错误="), + ("detail=", "详情="), + ("toolId=", "工具="), + ("root=", "根目录="), + ("count=", "数量="), + ("toolNotices=", "工具声明数="), + ("source=tray", "来源=托盘"), + ("behavior=exit_directly", "行为=直接退出"), + ("behavior=minimize_to_tray", "行为=最小化到托盘"), + ("behavior=ask", "行为=询问"), + ("dialog=com_exception", "弹窗=COM 异常"), + ("result=primary", "结果=主按钮"), + ("result=secondary", "结果=次按钮"), + ("result=cancel", "结果=取消"), + ("mode=", "模式="), + ("scope=", "范围="), + ("source=", "来源="), + ("behavior=", "行为="), + ("dialog=", "弹窗="), + ("native=", "原生工具="), + ("builtin=", "内置工具="), + ("external=", "外部工具="), + ("plugin=", "插件="), + ("language=", "语言="), + ("enabled=True", "已启用=是"), + ("enabled=False", "已启用=否"), + ("enabled=true", "已启用=是"), + ("enabled=false", "已启用=否"), + ("remembered=True", "已记住=是"), + ("remembered=False", "已记住=否"), + ("remembered=true", "已记住=是"), + ("remembered=false", "已记住=否") + }) + { + result = result.Replace(source, target, StringComparison.OrdinalIgnoreCase); + } + + return result; + } + + private static string ResultText(string value, bool english) + { + return value.Trim().ToLowerInvariant() switch + { + "success" => english ? "success" : "成功", + "failed" => english ? "failed" : "失败", + "canceled" or "cancelled" => english ? "canceled" : "已取消", + "timeout" => english ? "timeout" : "超时", + var other => english ? other : other + }; + } + + private static string OperationText(string value, bool english) + { + return value.Trim().ToLowerInvariant() switch + { + "launch" => english ? "launch" : "启动", + "launch-admin" => english ? "launch as administrator" : "管理员启动", + "open-directory" => english ? "open directory" : "打开目录", + "execute" => english ? "execute" : "执行", + "render" => english ? "render" : "渲染", + "export" => english ? "export" : "导出", + var other => english ? other : other + }; + } + + private static string Humanize(string value) + { + if (string.IsNullOrWhiteSpace(value)) + { + return value; + } + + var words = value.Split(['_', '-', ' '], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + return string.Join(" ", words.Select(word => char.ToUpperInvariant(word[0]) + word[1..])); + } +} diff --git a/src/YMhut.Box.Core/Logging/LogEntry.cs b/src/YMhut.Box.Core/Logging/LogEntry.cs new file mode 100644 index 0000000..373be4f --- /dev/null +++ b/src/YMhut.Box.Core/Logging/LogEntry.cs @@ -0,0 +1,8 @@ +namespace YMhut.Box.Core.Logging; + +public sealed record LogEntry( + DateTimeOffset Timestamp, + string Level, + string Category, + string Message, + string? Detail = null); diff --git a/src/YMhut.Box.Core/Logging/ResilientLogService.cs b/src/YMhut.Box.Core/Logging/ResilientLogService.cs new file mode 100644 index 0000000..6d4e6d3 --- /dev/null +++ b/src/YMhut.Box.Core/Logging/ResilientLogService.cs @@ -0,0 +1,270 @@ +using System.Text.Json; +using YMhut.Box.Core.App; + +namespace YMhut.Box.Core.Logging; + +public sealed class ResilientLogService(ILogService primary, AppPaths paths) : ILogService +{ + private readonly SemaphoreSlim _fallbackGate = new(1, 1); + + public event EventHandler? EntryWritten; + + public string LogPath => primary.LogPath; + + private string FallbackPath => Path.Combine(paths.Logs, "app-log-fallback.jsonl"); + + public async Task WriteAsync( + string level, + string category, + string message, + string? detail = null, + CancellationToken cancellationToken = default) + { + var entry = new LogEntry(DateTimeOffset.Now, level, category, message, detail); + try + { + await primary.WriteAsync(level, category, message, detail, cancellationToken).ConfigureAwait(false); + EntryWritten?.Invoke(this, entry); + } + catch (Exception exception) + { + var fallbackEntry = entry with + { + Detail = string.Join(Environment.NewLine, new[] + { + detail, + $"SQLite logging degraded: {exception.GetType().Name}: {Sanitize(exception.Message)}" + }.Where(value => !string.IsNullOrWhiteSpace(value))) + }; + await WriteFallbackAsync(fallbackEntry, cancellationToken).ConfigureAwait(false); + EntryWritten?.Invoke(this, fallbackEntry); + } + } + + public async Task> ReadAsync( + string? level = null, + string? category = null, + int take = 500, + CancellationToken cancellationToken = default) + { + try + { + return await primary.ReadAsync(level, category, take, cancellationToken).ConfigureAwait(false); + } + catch + { + return (await ReadFallbackAsync(cancellationToken).ConfigureAwait(false)) + .Where(entry => string.IsNullOrWhiteSpace(level) || entry.Level == level) + .Where(entry => string.IsNullOrWhiteSpace(category) || entry.Category == category) + .OrderByDescending(entry => entry.Timestamp) + .Take(Math.Clamp(take, 1, 5000)) + .ToArray(); + } + } + + public async Task> ReadByDateAsync( + DateOnly date, + string? level = null, + string? query = null, + int take = 0, + CancellationToken cancellationToken = default) + { + try + { + return await primary.ReadByDateAsync(date, level, query, take, cancellationToken).ConfigureAwait(false); + } + catch + { + var entries = FilterFallbackByDate(await ReadFallbackAsync(cancellationToken).ConfigureAwait(false), date, level, query); + return take > 0 ? entries.Take(take).ToArray() : entries.ToArray(); + } + } + + public async Task> ReadByDatePageAsync( + DateOnly date, + string? level = null, + string? query = null, + int skip = 0, + int take = 100, + CancellationToken cancellationToken = default) + { + try + { + return await primary.ReadByDatePageAsync(date, level, query, skip, take, cancellationToken).ConfigureAwait(false); + } + catch + { + return FilterFallbackByDate(await ReadFallbackAsync(cancellationToken).ConfigureAwait(false), date, level, query) + .Skip(Math.Max(0, skip)) + .Take(Math.Clamp(take, 1, 5000)) + .ToArray(); + } + } + + public async Task CleanupAsync(int retentionCount, CancellationToken cancellationToken = default) + { + try + { + await primary.CleanupAsync(retentionCount, cancellationToken).ConfigureAwait(false); + } + catch + { + await TrimFallbackAsync(retentionCount, cancellationToken).ConfigureAwait(false); + } + } + + public async Task ClearAllAsync(CancellationToken cancellationToken = default) + { + try + { + await primary.ClearAllAsync(cancellationToken).ConfigureAwait(false); + } + catch + { + } + + await ClearFallbackAsync(_ => true, cancellationToken).ConfigureAwait(false); + } + + public async Task ClearTodayAsync(CancellationToken cancellationToken = default) + { + try + { + await primary.ClearTodayAsync(cancellationToken).ConfigureAwait(false); + } + catch + { + } + + var today = DateOnly.FromDateTime(DateTime.Now); + await ClearFallbackAsync(entry => DateOnly.FromDateTime(entry.Timestamp.LocalDateTime) == today, cancellationToken).ConfigureAwait(false); + } + + public async Task ClearFilteredTodayAsync( + string? level = null, + string? query = null, + CancellationToken cancellationToken = default) + { + try + { + await primary.ClearFilteredTodayAsync(level, query, cancellationToken).ConfigureAwait(false); + } + catch + { + } + + var today = DateOnly.FromDateTime(DateTime.Now); + await ClearFallbackAsync(entry => + DateOnly.FromDateTime(entry.Timestamp.LocalDateTime) == today && + (string.IsNullOrWhiteSpace(level) || entry.Level == level) && + Matches(entry, query), cancellationToken).ConfigureAwait(false); + } + + private async Task WriteFallbackAsync(LogEntry entry, CancellationToken cancellationToken) + { + await _fallbackGate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + Directory.CreateDirectory(paths.Logs); + var json = JsonSerializer.Serialize(entry); + await File.AppendAllTextAsync(FallbackPath, json + Environment.NewLine, cancellationToken).ConfigureAwait(false); + } + finally + { + _fallbackGate.Release(); + } + } + + private async Task> ReadFallbackAsync(CancellationToken cancellationToken) + { + await _fallbackGate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + if (!File.Exists(FallbackPath)) + { + return []; + } + + var entries = new List(); + foreach (var line in await File.ReadAllLinesAsync(FallbackPath, cancellationToken).ConfigureAwait(false)) + { + try + { + var entry = JsonSerializer.Deserialize(line); + if (entry is not null) + { + entries.Add(entry); + } + } + catch (JsonException) + { + } + } + + return entries; + } + finally + { + _fallbackGate.Release(); + } + } + + private async Task TrimFallbackAsync(int retentionCount, CancellationToken cancellationToken) + { + var keep = Math.Max(0, retentionCount); + var entries = (await ReadFallbackAsync(cancellationToken).ConfigureAwait(false)) + .OrderByDescending(entry => entry.Timestamp) + .Take(keep) + .Reverse() + .ToArray(); + await RewriteFallbackAsync(entries, cancellationToken).ConfigureAwait(false); + } + + private async Task ClearFallbackAsync(Func predicate, CancellationToken cancellationToken) + { + var entries = (await ReadFallbackAsync(cancellationToken).ConfigureAwait(false)) + .Where(entry => !predicate(entry)) + .ToArray(); + await RewriteFallbackAsync(entries, cancellationToken).ConfigureAwait(false); + } + + private async Task RewriteFallbackAsync(IReadOnlyList entries, CancellationToken cancellationToken) + { + await _fallbackGate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + Directory.CreateDirectory(paths.Logs); + var lines = entries.Select(entry => JsonSerializer.Serialize(entry)).ToArray(); + await File.WriteAllLinesAsync(FallbackPath, lines, cancellationToken).ConfigureAwait(false); + } + finally + { + _fallbackGate.Release(); + } + } + + private static IReadOnlyList FilterFallbackByDate(IReadOnlyList entries, DateOnly date, string? level, string? query) + { + return entries + .Where(entry => DateOnly.FromDateTime(entry.Timestamp.LocalDateTime) == date) + .Where(entry => string.IsNullOrWhiteSpace(level) || entry.Level == level) + .Where(entry => Matches(entry, query)) + .OrderByDescending(entry => entry.Timestamp) + .ToArray(); + } + + private static bool Matches(LogEntry entry, string? query) + { + if (string.IsNullOrWhiteSpace(query)) + { + return true; + } + + return entry.Message.Contains(query, StringComparison.OrdinalIgnoreCase) || + entry.Category.Contains(query, StringComparison.OrdinalIgnoreCase) || + (entry.Detail?.Contains(query, StringComparison.OrdinalIgnoreCase) ?? false); + } + + private static string Sanitize(string value) + => string.IsNullOrWhiteSpace(value) ? string.Empty : value.Length > 240 ? value[..240] : value; +} diff --git a/src/YMhut.Box.Core/Logging/SqliteLogService.cs b/src/YMhut.Box.Core/Logging/SqliteLogService.cs new file mode 100644 index 0000000..99db456 --- /dev/null +++ b/src/YMhut.Box.Core/Logging/SqliteLogService.cs @@ -0,0 +1,378 @@ +using Microsoft.Data.Sqlite; +using YMhut.Box.Core.App; + +namespace YMhut.Box.Core.Logging; + +public sealed class SqliteLogService : ILogService +{ + private readonly SemaphoreSlim _gate = new(1, 1); + private bool _initialized; + + public SqliteLogService(AppPaths paths) + { + paths.EnsureCreated(); + LogPath = AppDatabasePaths.ResolveMainDatabasePath(paths); + } + + public event EventHandler? EntryWritten; + + public string LogPath { get; } + + public async Task WriteAsync( + string level, + string category, + string message, + string? detail = null, + CancellationToken cancellationToken = default) + { + var entry = new LogEntry(DateTimeOffset.Now, level, category, message, detail); + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = """ + INSERT INTO logs(timestamp, level, category, message, detail) + VALUES ($timestamp, $level, $category, $message, $detail); + """; + command.Parameters.AddWithValue("$timestamp", entry.Timestamp.ToString("O")); + command.Parameters.AddWithValue("$level", entry.Level); + command.Parameters.AddWithValue("$category", entry.Category); + command.Parameters.AddWithValue("$message", entry.Message); + command.Parameters.AddWithValue("$detail", (object?)entry.Detail ?? DBNull.Value); + await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + } + finally + { + _gate.Release(); + } + + EntryWritten?.Invoke(this, entry); + } + + public async Task> ReadAsync( + string? level = null, + string? category = null, + int take = 500, + CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = """ + SELECT timestamp, level, category, message, detail + FROM logs + WHERE ($level = '' OR level = $level) + AND ($category = '' OR category = $category) + ORDER BY timestamp DESC + LIMIT $take; + """; + command.Parameters.AddWithValue("$level", level ?? string.Empty); + command.Parameters.AddWithValue("$category", category ?? string.Empty); + command.Parameters.AddWithValue("$take", Math.Clamp(take, 1, 5000)); + + var entries = new List(); + await using var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false); + while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false)) + { + entries.Add(new LogEntry( + DateTimeOffset.TryParse(reader.GetString(0), out var timestamp) ? timestamp : DateTimeOffset.Now, + reader.GetString(1), + reader.GetString(2), + reader.GetString(3), + reader.IsDBNull(4) ? null : reader.GetString(4))); + } + + return entries; + } + finally + { + _gate.Release(); + } + } + + public async Task> ReadByDateAsync( + DateOnly date, + string? level = null, + string? query = null, + int take = 0, + CancellationToken cancellationToken = default) + { + return await ReadByDatePageCoreAsync( + date, + level, + query, + skip: 0, + take, + usePaging: take > 0, + cancellationToken).ConfigureAwait(false); + } + + public async Task> ReadByDatePageAsync( + DateOnly date, + string? level = null, + string? query = null, + int skip = 0, + int take = 100, + CancellationToken cancellationToken = default) + { + return await ReadByDatePageCoreAsync( + date, + level, + query, + Math.Max(0, skip), + Math.Clamp(take, 1, 5000), + usePaging: true, + cancellationToken).ConfigureAwait(false); + } + + private async Task> ReadByDatePageCoreAsync( + DateOnly date, + string? level, + string? query, + int skip, + int take, + bool usePaging, + CancellationToken cancellationToken) + { + var start = date.ToDateTime(TimeOnly.MinValue); + var end = start.AddDays(1); + var normalizedQuery = query?.Trim() ?? string.Empty; + + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = usePaging + ? """ + SELECT timestamp, level, category, message, detail + FROM logs + WHERE timestamp >= $start + AND timestamp < $end + AND ($level = '' OR level = $level) + AND ( + $query = '' + OR message LIKE $like + OR category LIKE $like + OR IFNULL(detail, '') LIKE $like + ) + ORDER BY timestamp DESC + LIMIT $take OFFSET $skip; + """ + : """ + SELECT timestamp, level, category, message, detail + FROM logs + WHERE timestamp >= $start + AND timestamp < $end + AND ($level = '' OR level = $level) + AND ( + $query = '' + OR message LIKE $like + OR category LIKE $like + OR IFNULL(detail, '') LIKE $like + ) + ORDER BY timestamp DESC; + """; + command.Parameters.AddWithValue("$start", start.ToString("O")); + command.Parameters.AddWithValue("$end", end.ToString("O")); + command.Parameters.AddWithValue("$level", level ?? string.Empty); + command.Parameters.AddWithValue("$query", normalizedQuery); + command.Parameters.AddWithValue("$like", $"%{normalizedQuery}%"); + if (usePaging) + { + command.Parameters.AddWithValue("$take", Math.Clamp(take, 1, 5000)); + command.Parameters.AddWithValue("$skip", Math.Max(0, skip)); + } + + var entries = new List(); + await using var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false); + while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false)) + { + entries.Add(new LogEntry( + DateTimeOffset.TryParse(reader.GetString(0), out var timestamp) ? timestamp : DateTimeOffset.Now, + reader.GetString(1), + reader.GetString(2), + reader.GetString(3), + reader.IsDBNull(4) ? null : reader.GetString(4))); + } + + return entries; + } + finally + { + _gate.Release(); + } + } + + public async Task CleanupAsync(int retentionCount, CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + var keep = Math.Max(0, retentionCount); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = keep == 0 + ? "DELETE FROM logs;" + : """ + DELETE FROM logs + WHERE id NOT IN ( + SELECT id FROM logs ORDER BY timestamp DESC LIMIT $keep + ); + """; + if (keep > 0) + { + command.Parameters.AddWithValue("$keep", keep); + } + + await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + await VacuumAsync(connection, cancellationToken).ConfigureAwait(false); + } + finally + { + _gate.Release(); + } + } + + public async Task ClearAllAsync(CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = "DELETE FROM logs;"; + await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + await VacuumAsync(connection, cancellationToken).ConfigureAwait(false); + } + finally + { + _gate.Release(); + } + } + + public async Task ClearTodayAsync(CancellationToken cancellationToken = default) + { + var today = DateTimeOffset.Now.Date; + var tomorrow = today.AddDays(1); + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = """ + DELETE FROM logs + WHERE timestamp >= $today + AND timestamp < $tomorrow; + """; + command.Parameters.AddWithValue("$today", today.ToString("O")); + command.Parameters.AddWithValue("$tomorrow", tomorrow.ToString("O")); + await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + await VacuumAsync(connection, cancellationToken).ConfigureAwait(false); + } + finally + { + _gate.Release(); + } + } + + public async Task ClearFilteredTodayAsync( + string? level = null, + string? query = null, + CancellationToken cancellationToken = default) + { + var today = DateTimeOffset.Now.Date; + var tomorrow = today.AddDays(1); + var normalizedQuery = query?.Trim() ?? string.Empty; + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = """ + DELETE FROM logs + WHERE timestamp >= $today + AND timestamp < $tomorrow + AND ($level = '' OR level = $level) + AND ( + $query = '' + OR message LIKE $like + OR category LIKE $like + OR IFNULL(detail, '') LIKE $like + ); + """; + command.Parameters.AddWithValue("$today", today.ToString("O")); + command.Parameters.AddWithValue("$tomorrow", tomorrow.ToString("O")); + command.Parameters.AddWithValue("$level", level ?? string.Empty); + command.Parameters.AddWithValue("$query", normalizedQuery); + command.Parameters.AddWithValue("$like", $"%{normalizedQuery}%"); + await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + await VacuumAsync(connection, cancellationToken).ConfigureAwait(false); + } + finally + { + _gate.Release(); + } + } + + private async Task EnsureInitializedAsync(CancellationToken cancellationToken) + { + if (_initialized) + { + return; + } + + Directory.CreateDirectory(Path.GetDirectoryName(LogPath)!); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = """ + PRAGMA journal_mode = WAL; + PRAGMA synchronous = NORMAL; + PRAGMA busy_timeout = 5000; + CREATE TABLE IF NOT EXISTS logs ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + timestamp TEXT NOT NULL, + level TEXT NOT NULL, + category TEXT NOT NULL, + message TEXT NOT NULL, + detail TEXT NULL + ); + CREATE INDEX IF NOT EXISTS idx_logs_timestamp ON logs(timestamp); + CREATE INDEX IF NOT EXISTS idx_logs_timestamp_level ON logs(timestamp DESC, level); + CREATE INDEX IF NOT EXISTS idx_logs_level ON logs(level); + CREATE INDEX IF NOT EXISTS idx_logs_category ON logs(category); + """; + await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + _initialized = true; + } + + private SqliteConnection OpenConnection() + { + var connection = new SqliteConnection($"Data Source={LogPath};Pooling=True"); + connection.Open(); + using var command = connection.CreateCommand(); + command.CommandText = """ + PRAGMA busy_timeout = 5000; + PRAGMA synchronous = NORMAL; + """; + command.ExecuteNonQuery(); + return connection; + } + + private static async Task VacuumAsync(SqliteConnection connection, CancellationToken cancellationToken) + { + await using var command = connection.CreateCommand(); + command.CommandText = "VACUUM;"; + await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + } +} diff --git a/src/YMhut.Box.Core/Media/MediaVolumeModel.cs b/src/YMhut.Box.Core/Media/MediaVolumeModel.cs new file mode 100644 index 0000000..a81b1c4 --- /dev/null +++ b/src/YMhut.Box.Core/Media/MediaVolumeModel.cs @@ -0,0 +1,52 @@ +namespace YMhut.Box.Core.Media; + +public enum MediaVolumeOutputMode +{ + Silent, + Normal, + Boosted +} + +public readonly record struct MediaVolumeState( + int Percent, + double PlatformVolume, + double AudioGain, + MediaVolumeOutputMode Mode) +{ + public bool IsMuted => Percent <= 0; + + public bool IsBoosted => Mode == MediaVolumeOutputMode.Boosted; +} + +public static class MediaVolumeModel +{ + public const int MinPercent = 0; + public const int MaxPercent = 120; + public const int NormalMaxPercent = 100; + + public static MediaVolumeState FromPercent(double percent) + { + var normalized = (int)Math.Round(Math.Clamp(percent, MinPercent, MaxPercent), MidpointRounding.AwayFromZero); + return new MediaVolumeState( + normalized, + normalized <= 0 ? 0 : Math.Min(normalized, NormalMaxPercent) / 100d, + normalized / 100d, + normalized <= 0 + ? MediaVolumeOutputMode.Silent + : normalized > NormalMaxPercent + ? MediaVolumeOutputMode.Boosted + : MediaVolumeOutputMode.Normal); + } + + public static string FormatPercent(int percent, string language = "zh-CN") + { + var state = FromPercent(percent); + var english = string.Equals(language, "en-US", StringComparison.OrdinalIgnoreCase); + return state.Mode switch + { + MediaVolumeOutputMode.Silent => english ? "0% (Muted)" : "0%(静音)", + MediaVolumeOutputMode.Boosted => english ? $"{state.Percent}% (Boosted)" : $"{state.Percent}%(增强音量)", + _ => $"{state.Percent}%" + }; + } +} diff --git a/src/YMhut.Box.Core/Media/RemoteMediaCatalog.cs b/src/YMhut.Box.Core/Media/RemoteMediaCatalog.cs new file mode 100644 index 0000000..dde8da1 --- /dev/null +++ b/src/YMhut.Box.Core/Media/RemoteMediaCatalog.cs @@ -0,0 +1,673 @@ +using System.Globalization; +using System.Text.Json; +using System.Text.RegularExpressions; + +namespace YMhut.Box.Core.Media; + +public enum RemoteMediaKind +{ + Unknown, + Image, + Audio, + Video +} + +public enum RemoteMediaCatalogLoadSource +{ + Remote, + Cache +} + +public sealed record RemoteMediaCatalog( + IReadOnlyList Categories, + RemoteMediaUiConfig UiConfig, + string LastUpdated, + string LayoutVersion, + DateTimeOffset LoadedAt) +{ + public IEnumerable EnabledCategories => + Categories.Where(category => category.Enabled); + + public IEnumerable Entries(bool enabledOnly = true) + { + foreach (var category in enabledOnly ? EnabledCategories : Categories) + { + foreach (var source in category.Sources) + { + yield return new RemoteMediaEntry(category, source); + } + } + } +} + +public sealed record RemoteMediaEntry(RemoteMediaCategory Category, RemoteMediaSource Source); + +public sealed record RemoteMediaCategory( + string Id, + string Name, + string Icon, + bool Enabled, + RemoteMediaKind Kind, + RemoteMediaLayout Layout, + IReadOnlyList Sources) +{ + public string DisplayName => RemoteMediaCatalogNames.CategoryName(Id, Name, Kind); + + public IReadOnlyList Subcategories => Sources; +} + +public sealed record RemoteMediaSource( + string Id, + string Name, + string Description, + string ApiUrl, + string ThumbnailUrl, + bool Downloadable, + int RefreshIntervalSeconds, + IReadOnlyList SupportedFormats, + RemoteMediaKind Kind) +{ + public bool IsAvailable => Uri.TryCreate(ApiUrl, UriKind.Absolute, out _); + + public string DisplayName => RemoteMediaCatalogNames.SourceName(Id, Name); + + public string DisplayDescription => RemoteMediaCatalogNames.Description(Description); + + public int RefreshInterval => RefreshIntervalSeconds; +} + +public sealed record RemoteMediaLayout( + int Columns, + string AspectRatio, + bool ShowPreview, + bool AutoPlay, + string TransitionEffect); + +public sealed record RemoteMediaUiConfig( + bool DarkMode, + bool ShowThumbnails, + string DefaultView, + RemoteMediaAnimationConfig Animations); + +public sealed record RemoteMediaAnimationConfig( + string TransitionEffect, + int DurationMilliseconds); + +public sealed record RemoteMediaCatalogLoadResult( + RemoteMediaCatalog Catalog, + RemoteMediaCatalogLoadSource Source, + string? Warning = null); + +public static class RemoteMediaCatalogParser +{ + private static readonly JsonDocumentOptions DocumentOptions = new() + { + AllowTrailingCommas = true, + CommentHandling = JsonCommentHandling.Skip + }; + + public static RemoteMediaCatalog Parse(string content, DateTimeOffset? loadedAt = null) + { + if (string.IsNullOrWhiteSpace(content)) + { + throw new InvalidDataException("Remote media configuration is empty."); + } + + using var document = ParseDocument(content); + var root = document.RootElement; + if (root.ValueKind != JsonValueKind.Object || + !TryGet(root, out var categoriesElement, "categories") || + categoriesElement.ValueKind != JsonValueKind.Array) + { + throw new InvalidDataException("Remote media configuration does not contain categories."); + } + + var uiConfig = ParseUiConfig(root); + var categories = new List(); + foreach (var categoryElement in categoriesElement.EnumerateArray()) + { + if (categoryElement.ValueKind != JsonValueKind.Object) + { + continue; + } + + var id = JsonString(categoryElement, "id"); + var categoryKind = InferKind(id, []); + var sources = ParseSources(categoryElement, categoryKind); + if (categoryKind == RemoteMediaKind.Unknown) + { + categoryKind = InferKind(id, sources.SelectMany(source => source.SupportedFormats)); + } + + var normalizedSources = sources + .Select(source => source.Kind == RemoteMediaKind.Unknown ? source with { Kind = categoryKind } : source) + .ToArray(); + + categories.Add(new RemoteMediaCategory( + Id: string.IsNullOrWhiteSpace(id) ? $"category-{categories.Count + 1}" : id, + Name: JsonString(categoryElement, "name"), + Icon: JsonString(categoryElement, "icon"), + Enabled: JsonBool(categoryElement, true, "enabled"), + Kind: categoryKind, + Layout: ParseLayout(categoryElement, categoryKind, uiConfig), + Sources: normalizedSources)); + } + + if (categories.Count == 0) + { + throw new InvalidDataException("Remote media configuration contains no usable categories."); + } + + return new RemoteMediaCatalog( + Categories: categories, + UiConfig: uiConfig, + LastUpdated: JsonString(root, "last_updated", "lastUpdated"), + LayoutVersion: JsonString(root, "layout_version", "layoutVersion"), + LoadedAt: loadedAt ?? DateTimeOffset.Now); + } + + public static bool TryParse(string content, out RemoteMediaCatalog catalog) + { + try + { + catalog = Parse(content); + return true; + } + catch + { + catalog = new RemoteMediaCatalog([], DefaultUiConfig(), string.Empty, string.Empty, DateTimeOffset.Now); + return false; + } + } + + private static JsonDocument ParseDocument(string content) + { + try + { + return JsonDocument.Parse(content, DocumentOptions); + } + catch (JsonException) + { + var repaired = RepairBrokenQuotedLines(content); + return JsonDocument.Parse(repaired, DocumentOptions); + } + } + + private static string RepairBrokenQuotedLines(string content) + { + var normalized = content.Replace("\r\n", "\n", StringComparison.Ordinal).Replace('\r', '\n'); + var lines = normalized.Split('\n'); + for (var index = 0; index < lines.Length; index++) + { + lines[index] = RepairBrokenQuotedLine(lines[index]); + } + + return string.Join('\n', lines); + } + + private static string RepairBrokenQuotedLine(string line) + { + var colon = line.IndexOf(':'); + if (colon < 0) + { + return line; + } + + var valueStart = line.IndexOf('"', colon + 1); + if (valueStart < 0) + { + return line; + } + + var between = line[(colon + 1)..valueStart]; + if (!string.IsNullOrWhiteSpace(between)) + { + return line; + } + + var tail = line[(valueStart + 1)..]; + if (ContainsUnescapedQuote(tail)) + { + return line; + } + + var trimmed = line.TrimEnd(); + var endPadding = line.Length - trimmed.Length; + var insertAt = trimmed.EndsWith(",", StringComparison.Ordinal) + ? line.LastIndexOf(',', line.Length - endPadding - 1) + : line.Length - endPadding; + return insertAt > valueStart + ? line.Insert(insertAt, "\"") + : line; + } + + private static bool ContainsUnescapedQuote(string value) + { + var escaped = false; + foreach (var ch in value) + { + if (escaped) + { + escaped = false; + continue; + } + + if (ch == '\\') + { + escaped = true; + continue; + } + + if (ch == '"') + { + return true; + } + } + + return false; + } + + private static RemoteMediaUiConfig ParseUiConfig(JsonElement root) + { + if (!TryGet(root, out var ui, "ui_config", "uiConfig") || ui.ValueKind != JsonValueKind.Object) + { + return DefaultUiConfig(); + } + + var animation = DefaultUiConfig().Animations; + if (TryGet(ui, out var animations, "animations") && animations.ValueKind == JsonValueKind.Object) + { + animation = new RemoteMediaAnimationConfig( + TransitionEffect: NonEmpty(JsonString(animations, "transition_effect", "transitionEffect"), animation.TransitionEffect), + DurationMilliseconds: Math.Clamp(JsonInt(animations, animation.DurationMilliseconds, "duration", "duration_ms", "durationMilliseconds"), 50, 3000)); + } + + return new RemoteMediaUiConfig( + DarkMode: JsonBool(ui, false, "dark_mode", "darkMode"), + ShowThumbnails: JsonBool(ui, true, "show_thumbnails", "showThumbnails"), + DefaultView: NonEmpty(JsonString(ui, "default_view", "defaultView"), "grid"), + Animations: animation); + } + + private static RemoteMediaUiConfig DefaultUiConfig() + { + return new RemoteMediaUiConfig( + DarkMode: false, + ShowThumbnails: true, + DefaultView: "grid", + Animations: new RemoteMediaAnimationConfig("fade", 300)); + } + + private static IReadOnlyList ParseSources(JsonElement category, RemoteMediaKind categoryKind) + { + if (!TryGet(category, out var subcategories, "subcategories", "sources") || + subcategories.ValueKind != JsonValueKind.Array) + { + return []; + } + + var sources = new List(); + foreach (var sourceElement in subcategories.EnumerateArray()) + { + if (sourceElement.ValueKind != JsonValueKind.Object) + { + continue; + } + + var id = JsonString(sourceElement, "id"); + var formats = JsonStringArray(sourceElement, "supported_formats", "supportedFormats"); + var kind = InferKind(id, formats); + if (kind == RemoteMediaKind.Unknown) + { + kind = categoryKind; + } + + if (formats.Count == 0) + { + formats = DefaultFormats(kind); + } + + var apiUrl = JsonString(sourceElement, "api_url", "apiUrl", "url"); + var thumbnailUrl = JsonString(sourceElement, "thumbnail_url", "thumbnailUrl", "thumbnail", "cover"); + sources.Add(new RemoteMediaSource( + Id: string.IsNullOrWhiteSpace(id) ? $"source-{sources.Count + 1}" : id, + Name: JsonString(sourceElement, "name"), + Description: JsonString(sourceElement, "description"), + ApiUrl: apiUrl, + ThumbnailUrl: string.IsNullOrWhiteSpace(thumbnailUrl) ? apiUrl : thumbnailUrl, + Downloadable: JsonBool(sourceElement, true, "downloadable"), + RefreshIntervalSeconds: NormalizedRefreshInterval(sourceElement, kind), + SupportedFormats: formats, + Kind: kind)); + } + + return sources; + } + + private static RemoteMediaLayout ParseLayout(JsonElement category, RemoteMediaKind kind, RemoteMediaUiConfig uiConfig) + { + var columns = 1; + var aspectRatio = "16:9"; + var showPreview = uiConfig.ShowThumbnails; + var autoPlay = false; + var transition = uiConfig.Animations.TransitionEffect; + + if (TryGet(category, out var layout, "layout") && layout.ValueKind == JsonValueKind.Object) + { + columns = JsonInt(layout, columns, "columns"); + aspectRatio = NonEmpty(JsonString(layout, "aspect_ratio", "aspectRatio"), aspectRatio); + showPreview = JsonBool(layout, showPreview, "show_preview", "showPreview"); + autoPlay = JsonBool(layout, false, "auto_play", "autoPlay"); + transition = NonEmpty(JsonString(layout, "transition_effect", "transitionEffect"), transition); + } + + if (kind == RemoteMediaKind.Audio) + { + showPreview = false; + } + + return new RemoteMediaLayout( + Columns: Math.Clamp(columns, 1, 4), + AspectRatio: aspectRatio, + ShowPreview: showPreview, + AutoPlay: autoPlay, + TransitionEffect: transition); + } + + private static int NormalizedRefreshInterval(JsonElement source, RemoteMediaKind kind) + { + var fallback = kind == RemoteMediaKind.Video ? 60 : 30; + var value = JsonInt(source, fallback, "refresh_interval", "refreshInterval"); + return Math.Clamp(value <= 0 ? fallback : value, 5, 3600); + } + + private static List DefaultFormats(RemoteMediaKind kind) + { + return kind switch + { + RemoteMediaKind.Video => ["mp4", "webm"], + RemoteMediaKind.Audio => ["mp3", "wav", "m4a", "ogg"], + _ => ["jpg", "jpeg", "png", "webp"] + }; + } + + public static RemoteMediaKind InferKind(string id, IEnumerable formats) + { + var normalizedId = id.Trim().ToLowerInvariant(); + if (normalizedId.Contains("video", StringComparison.Ordinal) || + normalizedId.Contains("sp", StringComparison.Ordinal) || + normalizedId.Contains("mv", StringComparison.Ordinal)) + { + return RemoteMediaKind.Video; + } + + if (normalizedId.Contains("audio", StringComparison.Ordinal) || + normalizedId.Contains("music", StringComparison.Ordinal)) + { + return RemoteMediaKind.Audio; + } + + if (normalizedId.Contains("image", StringComparison.Ordinal) || + normalizedId.Contains("img", StringComparison.Ordinal) || + normalizedId.Contains("pic", StringComparison.Ordinal)) + { + return RemoteMediaKind.Image; + } + + var kind = RemoteMediaKind.Unknown; + foreach (var format in formats.Select(format => format.Trim().TrimStart('.').ToLowerInvariant())) + { + if (format is "mp4" or "mkv" or "webm" or "avi" or "mov" or "wmv" or "m4v") + { + return RemoteMediaKind.Video; + } + + if (format is "mp3" or "wav" or "flac" or "aac" or "m4a" or "ogg" or "wma") + { + kind = RemoteMediaKind.Audio; + } + + if (kind == RemoteMediaKind.Unknown && + format is "png" or "jpg" or "jpeg" or "bmp" or "gif" or "webp" or "tif" or "tiff") + { + kind = RemoteMediaKind.Image; + } + } + + return kind; + } + + private static string JsonString(JsonElement root, params string[] names) + { + if (!TryGet(root, out var value, names)) + { + return string.Empty; + } + + return value.ValueKind switch + { + JsonValueKind.String => value.GetString() ?? string.Empty, + JsonValueKind.Number or JsonValueKind.True or JsonValueKind.False => value.ToString(), + _ => string.Empty + }; + } + + private static bool JsonBool(JsonElement root, bool fallback, params string[] names) + { + if (!TryGet(root, out var value, names)) + { + return fallback; + } + + return value.ValueKind switch + { + JsonValueKind.True => true, + JsonValueKind.False => false, + JsonValueKind.String when bool.TryParse(value.GetString(), out var parsed) => parsed, + _ => fallback + }; + } + + private static int JsonInt(JsonElement root, int fallback, params string[] names) + { + if (!TryGet(root, out var value, names)) + { + return fallback; + } + + return value.ValueKind switch + { + JsonValueKind.Number when value.TryGetInt32(out var parsed) => parsed, + JsonValueKind.String when int.TryParse(value.GetString(), NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsed) => parsed, + _ => fallback + }; + } + + private static List JsonStringArray(JsonElement root, params string[] names) + { + if (!TryGet(root, out var value, names)) + { + return []; + } + + if (value.ValueKind == JsonValueKind.Array) + { + return value + .EnumerateArray() + .Select(item => item.ValueKind == JsonValueKind.String ? item.GetString() ?? string.Empty : item.ToString()) + .Select(item => item.Trim().TrimStart('.').ToLowerInvariant()) + .Where(item => item.Length > 0) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToList(); + } + + if (value.ValueKind == JsonValueKind.String) + { + return Regex.Split(value.GetString() ?? string.Empty, @"[\s,;/|]+") + .Select(item => item.Trim().TrimStart('.').ToLowerInvariant()) + .Where(item => item.Length > 0) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToList(); + } + + return []; + } + + private static bool TryGet(JsonElement root, out JsonElement value, params string[] names) + { + value = default; + if (root.ValueKind != JsonValueKind.Object) + { + return false; + } + + foreach (var name in names) + { + if (root.TryGetProperty(name, out value)) + { + return true; + } + } + + foreach (var property in root.EnumerateObject()) + { + if (names.Any(name => string.Equals(name, property.Name, StringComparison.OrdinalIgnoreCase))) + { + value = property.Value; + return true; + } + } + + return false; + } + + private static string NonEmpty(string value, string fallback) + => string.IsNullOrWhiteSpace(value) ? fallback : value.Trim(); +} + +public static class RemoteMediaCatalogFormatter +{ + public static string FormatRandomSource(RemoteMediaCatalog catalog, string input, string language = "zh-CN") + { + var query = (input ?? string.Empty).Trim(); + var entries = catalog + .Entries() + .Where(entry => entry.Source.IsAvailable) + .ToList(); + + if (!string.IsNullOrWhiteSpace(query)) + { + entries = entries + .Where(entry => + Contains(entry.Category.Id, query) || + Contains(entry.Category.DisplayName, query) || + Contains(entry.Source.Id, query) || + Contains(entry.Source.DisplayName, query) || + Contains(entry.Source.DisplayDescription, query)) + .ToList(); + } + + if (entries.Count == 0) + { + throw new InvalidOperationException(T(language, "没有可用的远程媒体源。", "No available remote media sources.")); + } + + var selected = entries[Random.Shared.Next(entries.Count)]; + var source = selected.Source; + var category = selected.Category; + var lines = new[] + { + T(language, "随机放映室远程源", "Random Cinema remote source"), + T(language, $"配置:远程媒体目录 {VersionText(catalog)}", $"Configuration: remote media catalog {VersionText(catalog)}"), + T(language, $"分类:{category.DisplayName} ({category.Id})", $"Category: {category.DisplayName} ({category.Id})"), + T(language, $"媒体:{source.DisplayName} ({source.Id})", $"Media: {source.DisplayName} ({source.Id})"), + T(language, $"说明:{source.DisplayDescription}", $"Description: {source.DisplayDescription}"), + T(language, "媒体源:已隐藏远程地址", "Media source: remote address hidden"), + T(language, "缩略图:已隐藏远程地址", "Thumbnail: remote address hidden"), + T(language, $"格式:{string.Join(" / ", source.SupportedFormats)}", $"Formats: {string.Join(" / ", source.SupportedFormats)}"), + T(language, $"建议刷新间隔:{source.RefreshIntervalSeconds} 秒", $"Refresh interval: {source.RefreshIntervalSeconds} seconds"), + T(language, source.Downloadable ? "下载:允许" : "下载:禁用", source.Downloadable ? "Download: allowed" : "Download: disabled"), + T(language, + $"布局:{category.Layout.Columns} 列 / {category.Layout.AspectRatio} / {(category.Layout.ShowPreview ? "显示预览" : "隐藏预览")} / {(category.Layout.AutoPlay ? "自动播放" : "手动播放")}", + $"Layout: {category.Layout.Columns} columns / {category.Layout.AspectRatio} / {(category.Layout.ShowPreview ? "preview on" : "preview off")} / {(category.Layout.AutoPlay ? "autoplay" : "manual play")}") + }; + return string.Join(Environment.NewLine, lines.Where(line => !string.IsNullOrWhiteSpace(line))); + } + + private static bool Contains(string value, string query) + => value.Contains(query, StringComparison.OrdinalIgnoreCase); + + private static string VersionText(RemoteMediaCatalog catalog) + => string.IsNullOrWhiteSpace(catalog.LayoutVersion) ? string.Empty : $"v{catalog.LayoutVersion}"; + + private static string T(string language, string zh, string en) + => string.Equals(language, "en-US", StringComparison.OrdinalIgnoreCase) ? en : zh; +} + +internal static class RemoteMediaCatalogNames +{ + private static readonly IReadOnlyDictionary SourceFallbacks = new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["xjj"] = "Portrait", + ["baisi"] = "Light style", + ["heisi"] = "Dark style", + ["acg"] = "ACG 4K", + ["miku"] = "Miku", + ["wappller"] = "HD wallpaper", + ["radom_xjj_leixing"] = "Style videos", + ["radom_xjj_short"] = "Short videos", + ["radom_xjj_mv"] = "JK video", + ["radom_xjj_menv"] = "Random video" + }; + + public static string CategoryName(string id, string name, RemoteMediaKind kind) + { + if (!LooksBroken(name)) + { + return name; + } + + return kind switch + { + RemoteMediaKind.Video => "Random Videos", + RemoteMediaKind.Audio => "Random Audio", + RemoteMediaKind.Image => "Random Images", + _ => string.IsNullOrWhiteSpace(id) ? "Remote Media" : id + }; + } + + public static string SourceName(string id, string name) + { + if (!LooksBroken(name)) + { + return name; + } + + return SourceFallbacks.TryGetValue(id, out var fallback) + ? fallback + : string.IsNullOrWhiteSpace(id) ? "Remote source" : id; + } + + public static string Description(string description) + { + return LooksBroken(description) + ? "Remote random media source with reload, preview, and save support." + : description; + } + + private static bool LooksBroken(string value) + { + if (string.IsNullOrWhiteSpace(value)) + { + return true; + } + + return value.Contains('\uFFFD') || + value.Contains("锟", StringComparison.Ordinal) || + value.Contains("闅", StringComparison.Ordinal) || + value.Contains("濮", StringComparison.Ordinal) || + value.Contains("绮", StringComparison.Ordinal) || + value.Contains("瑙", StringComparison.Ordinal) || + value.Contains("鐭", StringComparison.Ordinal); + } +} diff --git a/src/YMhut.Box.Core/Media/RemoteMediaCatalogService.cs b/src/YMhut.Box.Core/Media/RemoteMediaCatalogService.cs new file mode 100644 index 0000000..cf82809 --- /dev/null +++ b/src/YMhut.Box.Core/Media/RemoteMediaCatalogService.cs @@ -0,0 +1,130 @@ +using System.Text.Json; +using YMhut.Box.Core.Api; +using YMhut.Box.Core.App; +using YMhut.Box.Core.Logging; + +namespace YMhut.Box.Core.Media; + +public interface IRemoteMediaCatalogService +{ + string CacheDirectory { get; } + + Task LoadAsync(bool forceRefresh = false, CancellationToken cancellationToken = default); + + Task TryReadCacheAsync(CancellationToken cancellationToken = default); + + Task ClearCacheAsync(CancellationToken cancellationToken = default); +} + +public sealed class RemoteMediaCatalogService( + AppPaths paths, + IApiManager apiManager, + ILogService? logService = null) : IRemoteMediaCatalogService +{ + public static readonly Uri PrimaryConfigUri = new("https://update.ymhut.cn/media-types.json"); + + private const string EndpointId = "media_types"; + private const string SnapshotFileName = "media-types.json"; + + public string CacheDirectory => Path.Combine(paths.Cache, "remote-media"); + + private string SnapshotPath => Path.Combine(CacheDirectory, SnapshotFileName); + + public async Task LoadAsync(bool forceRefresh = false, CancellationToken cancellationToken = default) + { + string? warning = null; + try + { + var response = forceRefresh + ? await apiManager.FetchUriAsync(EndpointId, AddCacheBuster(PrimaryConfigUri), string.Empty, cancellationToken).ConfigureAwait(false) + : await apiManager.FetchAsync(EndpointId, string.Empty, cancellationToken).ConfigureAwait(false); + + if (!response.Success) + { + throw new InvalidOperationException(response.Error ?? "Remote media configuration request failed."); + } + + var catalog = RemoteMediaCatalogParser.Parse(response.Content, response.FetchedAt); + await WriteSnapshotAsync(response.Content, cancellationToken).ConfigureAwait(false); + return new RemoteMediaCatalogLoadResult(catalog, RemoteMediaCatalogLoadSource.Remote); + } + catch (Exception exception) when (exception is HttpRequestException or IOException or JsonException or InvalidDataException or TaskCanceledException or InvalidOperationException) + { + warning = exception.Message; + await WriteLogAsync("Warning", "remote-media", "Remote media configuration failed; trying local snapshot.", warning, cancellationToken).ConfigureAwait(false); + } + + var cached = await TryReadCacheAsync(cancellationToken).ConfigureAwait(false); + if (cached is not null) + { + return cached with { Warning = warning }; + } + + throw new InvalidOperationException(warning ?? "Remote media configuration is unavailable and no local snapshot exists."); + } + + public async Task TryReadCacheAsync(CancellationToken cancellationToken = default) + { + if (!File.Exists(SnapshotPath)) + { + return null; + } + + try + { + var content = await File.ReadAllTextAsync(SnapshotPath, cancellationToken).ConfigureAwait(false); + var loadedAt = File.GetLastWriteTimeUtc(SnapshotPath); + var catalog = RemoteMediaCatalogParser.Parse(content, new DateTimeOffset(loadedAt, TimeSpan.Zero)); + return new RemoteMediaCatalogLoadResult(catalog, RemoteMediaCatalogLoadSource.Cache); + } + catch (Exception exception) when (exception is IOException or JsonException or InvalidDataException) + { + await WriteLogAsync("Warning", "remote-media", "Remote media cache snapshot is unreadable.", exception.Message, cancellationToken).ConfigureAwait(false); + return null; + } + } + + public Task ClearCacheAsync(CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + if (!Directory.Exists(CacheDirectory)) + { + return Task.CompletedTask; + } + + NormalizeAttributes(CacheDirectory); + Directory.Delete(CacheDirectory, recursive: true); + return Task.CompletedTask; + } + + private async Task WriteSnapshotAsync(string content, CancellationToken cancellationToken) + { + Directory.CreateDirectory(CacheDirectory); + await File.WriteAllTextAsync(SnapshotPath, content, cancellationToken).ConfigureAwait(false); + } + + private static Uri AddCacheBuster(Uri uri) + { + var builder = new UriBuilder(uri); + var token = $"_={DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}"; + builder.Query = string.IsNullOrWhiteSpace(builder.Query) + ? token + : builder.Query.TrimStart('?') + "&" + token; + return builder.Uri; + } + + private static void NormalizeAttributes(string directory) + { + foreach (var path in Directory.EnumerateFileSystemEntries(directory, "*", SearchOption.AllDirectories)) + { + File.SetAttributes(path, FileAttributes.Normal); + } + + File.SetAttributes(directory, FileAttributes.Normal); + } + + private Task WriteLogAsync(string level, string category, string message, string detail, CancellationToken cancellationToken) + { + return logService?.WriteAsync(level, category, message, detail, cancellationToken) ?? Task.CompletedTask; + } +} diff --git a/src/YMhut.Box.Core/Media/RemoteMediaResolver.cs b/src/YMhut.Box.Core/Media/RemoteMediaResolver.cs new file mode 100644 index 0000000..bbd102f --- /dev/null +++ b/src/YMhut.Box.Core/Media/RemoteMediaResolver.cs @@ -0,0 +1,502 @@ +using System.Net.Http.Headers; +using System.Text.Json; +using System.Text.RegularExpressions; +using YMhut.Box.Core.Net; + +namespace YMhut.Box.Core.Media; + +public interface IRemoteMediaResolver +{ + Task ResolveMediaAsync( + string apiUrl, + RemoteMediaKind expectedKind = RemoteMediaKind.Unknown, + bool cacheBust = true, + IProgress? progress = null, + CancellationToken cancellationToken = default); + + Task ResolveMediaUriAsync(string apiUrl, bool cacheBust = true, IProgress? progress = null, CancellationToken cancellationToken = default); + + Task DownloadBytesAsync(Uri uri, IProgress? progress = null, CancellationToken cancellationToken = default); +} + +public sealed record RemoteMediaResolution( + Uri Uri, + string ContentType, + long? ContentLength, + bool IsDirectMedia, + string SuggestedExtension); + +public sealed class RemoteMediaResolver : IRemoteMediaResolver +{ + private const int MaxMediaRedirects = 8; + private const long MaxTextProbeLength = 2 * 1024 * 1024; + private static readonly Regex AbsoluteUrlRegex = new(@"https?://[^\s""'<>\\]+", RegexOptions.IgnoreCase | RegexOptions.Compiled); + + private readonly Func? _handlerFactory; + + public RemoteMediaResolver(Func? handlerFactory = null) + { + _handlerFactory = handlerFactory; + } + + public async Task ResolveMediaAsync( + string apiUrl, + RemoteMediaKind expectedKind = RemoteMediaKind.Unknown, + bool cacheBust = true, + IProgress? progress = null, + CancellationToken cancellationToken = default) + { + if (!Uri.TryCreate(cacheBust ? AddCacheBuster(apiUrl) : apiUrl, UriKind.Absolute, out var uri)) + { + throw new InvalidOperationException("The remote media source URL is invalid."); + } + + return await ResolveMediaAsync(uri, expectedKind, progress, cancellationToken).ConfigureAwait(false); + } + + public async Task ResolveMediaUriAsync(string apiUrl, bool cacheBust = true, IProgress? progress = null, CancellationToken cancellationToken = default) + { + var resolution = await ResolveMediaAsync(apiUrl, RemoteMediaKind.Unknown, cacheBust, progress, cancellationToken).ConfigureAwait(false); + return resolution.Uri; + } + + public async Task DownloadBytesAsync(Uri uri, IProgress? progress = null, CancellationToken cancellationToken = default) + { + using var client = CreateMediaHttpClient(); + using var response = await client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); + response.EnsureSuccessStatusCode(); + var total = response.Content.Headers.ContentLength; + await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false); + using var buffer = new MemoryStream(); + var bytes = new byte[64 * 1024]; + long received = 0; + int read; + while ((read = await stream.ReadAsync(bytes.AsMemory(0, bytes.Length), cancellationToken).ConfigureAwait(false)) > 0) + { + buffer.Write(bytes, 0, read); + received += read; + if (total is > 0) + { + progress?.Report(45 + Math.Min(50, received * 50d / total.Value)); + } + } + + progress?.Report(98); + return buffer.ToArray(); + } + + private async Task ResolveMediaAsync( + Uri uri, + RemoteMediaKind expectedKind, + IProgress? progress, + CancellationToken cancellationToken) + { + var current = uri; + RemoteMediaResolution? lastResolution = null; + + for (var depth = 0; depth < MaxMediaRedirects; depth++) + { + progress?.Report(Math.Min(30, 6 + depth * 4)); + try + { + using var redirectClient = CreateRedirectHttpClient(); + current = await HttpRedirectResolver.ResolveAsync(current, redirectClient, MaxMediaRedirects, cancellationToken).ConfigureAwait(false); + + using var probeClient = CreateMediaHttpClient(timeout: TimeSpan.FromSeconds(20)); + using var response = await probeClient.GetAsync(current, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); + response.EnsureSuccessStatusCode(); + + var finalUri = response.RequestMessage?.RequestUri ?? current; + var contentType = NormalizeContentType(response.Content.Headers.ContentType?.MediaType); + var length = response.Content.Headers.ContentLength; + var direct = IsDirectMedia(finalUri, contentType, length, expectedKind); + var suggestedExtension = SuggestedExtension(finalUri, contentType, expectedKind); + lastResolution = new RemoteMediaResolution(finalUri, contentType, length, direct, suggestedExtension); + if (direct) + { + return lastResolution; + } + + if (!CanReadAsText(contentType, length)) + { + return lastResolution; + } + + var text = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); + if (TryExtractMediaUri(text, finalUri, expectedKind, out var next) && !UriEquals(finalUri, next)) + { + current = next; + continue; + } + + return lastResolution; + } + catch when (!cancellationToken.IsCancellationRequested) + { + return lastResolution ?? FromUriOnly(current, expectedKind); + } + } + + return lastResolution ?? FromUriOnly(current, expectedKind); + } + + private HttpClient CreateRedirectHttpClient() + { + if (_handlerFactory is not null) + { + return CreateMediaHttpClient(_handlerFactory(), TimeSpan.FromSeconds(20)); + } + + return CreateMediaHttpClient(new HttpClientHandler { AllowAutoRedirect = false }, TimeSpan.FromSeconds(20)); + } + + private HttpClient CreateMediaHttpClient(HttpMessageHandler? handler = null, TimeSpan? timeout = null) + { + var effectiveHandler = handler ?? _handlerFactory?.Invoke(); + var client = effectiveHandler is null + ? new HttpClient() + : new HttpClient(effectiveHandler, disposeHandler: true); + client.Timeout = timeout ?? TimeSpan.FromMinutes(5); + AddHeader(client.DefaultRequestHeaders.UserAgent, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"); + AddHeader(client.DefaultRequestHeaders.UserAgent, "YMhutBox/2.0"); + client.DefaultRequestHeaders.Accept.ParseAdd("image/*, video/*, audio/*, application/json, text/plain, text/html, application/octet-stream, */*"); + client.DefaultRequestHeaders.AcceptLanguage.ParseAdd("zh-CN,zh;q=0.9,en;q=0.8"); + return client; + } + + private static void AddHeader(HttpHeaderValueCollection headers, string value) + { + if (!headers.Any(header => string.Equals(header.ToString(), value, StringComparison.OrdinalIgnoreCase))) + { + headers.ParseAdd(value); + } + } + + private static string AddCacheBuster(string url) + { + if (string.IsNullOrWhiteSpace(url)) + { + return url; + } + + var separator = url.Contains('?', StringComparison.Ordinal) ? '&' : '?'; + return $"{url}{separator}_={DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}"; + } + + private static RemoteMediaResolution FromUriOnly(Uri uri, RemoteMediaKind expectedKind) + { + var extension = SuggestedExtension(uri, string.Empty, expectedKind); + return new RemoteMediaResolution( + uri, + string.Empty, + null, + LooksLikeDirectMediaUri(uri, expectedKind), + extension); + } + + private static bool IsDirectMedia(Uri uri, string contentType, long? length, RemoteMediaKind expectedKind) + { + if (IsMediaContentType(contentType) || LooksLikeDirectMediaUri(uri, expectedKind)) + { + return true; + } + + if (contentType.Equals("application/octet-stream", StringComparison.OrdinalIgnoreCase)) + { + return expectedKind != RemoteMediaKind.Unknown || KnownMediaExtension(Path.GetExtension(uri.AbsolutePath)); + } + + return expectedKind != RemoteMediaKind.Unknown && + string.IsNullOrWhiteSpace(contentType) && + length is > MaxTextProbeLength; + } + + private static bool LooksLikeDirectMediaUri(Uri uri, RemoteMediaKind expectedKind) + { + var extension = Path.GetExtension(uri.AbsolutePath).TrimStart('.').ToLowerInvariant(); + if (string.IsNullOrWhiteSpace(extension)) + { + return false; + } + + if (expectedKind == RemoteMediaKind.Image && IsImageExtension(extension)) + { + return true; + } + + if (expectedKind == RemoteMediaKind.Video && IsVideoExtension(extension)) + { + return true; + } + + if (expectedKind == RemoteMediaKind.Audio && IsAudioExtension(extension)) + { + return true; + } + + return expectedKind == RemoteMediaKind.Unknown && KnownMediaExtension(extension); + } + + private static bool KnownMediaExtension(string extension) + { + var normalized = extension.TrimStart('.').ToLowerInvariant(); + return IsVideoExtension(normalized) || IsAudioExtension(normalized) || IsImageExtension(normalized); + } + + private static bool IsVideoExtension(string extension) + => extension is "mp4" or "mkv" or "webm" or "avi" or "mov" or "wmv" or "m4v"; + + private static bool IsAudioExtension(string extension) + => extension is "mp3" or "wav" or "flac" or "aac" or "m4a" or "ogg" or "wma"; + + private static bool IsImageExtension(string extension) + => extension is "png" or "jpg" or "jpeg" or "bmp" or "gif" or "webp" or "tif" or "tiff"; + + private static bool IsMediaContentType(string contentType) + { + return contentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase) || + contentType.StartsWith("video/", StringComparison.OrdinalIgnoreCase) || + contentType.StartsWith("audio/", StringComparison.OrdinalIgnoreCase); + } + + private static bool CanReadAsText(string contentType, long? length) + { + if (length is > MaxTextProbeLength) + { + return false; + } + + return string.IsNullOrWhiteSpace(contentType) || + contentType.StartsWith("text/", StringComparison.OrdinalIgnoreCase) || + contentType.Contains("json", StringComparison.OrdinalIgnoreCase) || + contentType.Contains("xml", StringComparison.OrdinalIgnoreCase) || + contentType.Contains("javascript", StringComparison.OrdinalIgnoreCase); + } + + internal static bool TryExtractMediaUri(string text, Uri baseUri, RemoteMediaKind expectedKind, out Uri uri) + { + if (TryExtractUriFromJson(text, baseUri, expectedKind, out uri)) + { + return true; + } + + var trimmed = (text ?? string.Empty).Trim(); + if (TryCreateCandidateUri(trimmed, baseUri, expectedKind, out uri)) + { + return true; + } + + Uri? best = null; + var bestScore = int.MinValue; + var index = 0; + foreach (Match match in AbsoluteUrlRegex.Matches(text ?? string.Empty)) + { + if (!TryCreateCandidateUri(match.Value.TrimEnd(',', ';', ')', ']', '}'), baseUri, expectedKind, out var candidate)) + { + continue; + } + + var score = ScoreUriCandidate(candidate, string.Empty, expectedKind, index++); + if (score > bestScore) + { + best = candidate; + bestScore = score; + } + } + + if (best is not null) + { + uri = best; + return true; + } + + uri = baseUri; + return false; + } + + private static bool TryExtractUriFromJson(string text, Uri baseUri, RemoteMediaKind expectedKind, out Uri uri) + { + uri = baseUri; + if (string.IsNullOrWhiteSpace(text)) + { + return false; + } + + try + { + using var document = JsonDocument.Parse(text, new JsonDocumentOptions + { + AllowTrailingCommas = true, + CommentHandling = JsonCommentHandling.Skip + }); + return TryFindBestUri(document.RootElement, baseUri, expectedKind, out uri); + } + catch + { + return false; + } + } + + private static bool TryFindBestUri(JsonElement element, Uri baseUri, RemoteMediaKind expectedKind, out Uri uri) + { + var candidates = new List<(Uri Uri, string Field, int Order)>(); + CollectJsonUriCandidates(element, baseUri, candidates, string.Empty); + var best = candidates + .Select(candidate => (candidate.Uri, Score: ScoreUriCandidate(candidate.Uri, candidate.Field, expectedKind, candidate.Order))) + .OrderByDescending(candidate => candidate.Score) + .FirstOrDefault(); + + if (best.Uri is not null) + { + uri = best.Uri; + return true; + } + + uri = baseUri; + return false; + } + + private static void CollectJsonUriCandidates( + JsonElement element, + Uri baseUri, + List<(Uri Uri, string Field, int Order)> candidates, + string fieldName) + { + if (element.ValueKind == JsonValueKind.String) + { + var value = element.GetString(); + if (TryCreateCandidateUri(value, baseUri, RemoteMediaKind.Unknown, out var candidate)) + { + candidates.Add((candidate, fieldName, candidates.Count)); + } + return; + } + + if (element.ValueKind == JsonValueKind.Object) + { + foreach (var property in element.EnumerateObject()) + { + CollectJsonUriCandidates(property.Value, baseUri, candidates, property.Name); + } + } + else if (element.ValueKind == JsonValueKind.Array) + { + foreach (var item in element.EnumerateArray()) + { + CollectJsonUriCandidates(item, baseUri, candidates, fieldName); + } + } + } + + private static bool TryCreateCandidateUri(string? value, Uri baseUri, RemoteMediaKind expectedKind, out Uri uri) + { + uri = baseUri; + if (string.IsNullOrWhiteSpace(value)) + { + return false; + } + + var trimmed = value.Trim().Trim('"', '\''); + if (!trimmed.StartsWith("http://", StringComparison.OrdinalIgnoreCase) && + !trimmed.StartsWith("https://", StringComparison.OrdinalIgnoreCase) && + !trimmed.StartsWith("//", StringComparison.Ordinal) && + !trimmed.StartsWith("/", StringComparison.Ordinal) && + !trimmed.StartsWith("./", StringComparison.Ordinal) && + !trimmed.StartsWith("../", StringComparison.Ordinal)) + { + return false; + } + + if (trimmed.StartsWith("//", StringComparison.Ordinal)) + { + trimmed = $"{baseUri.Scheme}:{trimmed}"; + } + + if (!Uri.TryCreate(trimmed, UriKind.Absolute, out var absoluteUri) && + !Uri.TryCreate(baseUri, trimmed, out absoluteUri)) + { + return false; + } + + uri = absoluteUri; + return true; + } + + private static int ScoreUriCandidate(Uri candidate, string fieldName, RemoteMediaKind expectedKind, int order) + { + var field = fieldName.ToLowerInvariant(); + var score = Math.Max(0, 1000 - order); + if (field is "url" or "src" or "media" or "file" or "uri") + { + score += 500; + } + + if (field is "video" or "mp4" or "image" or "img" or "audio") + { + score += 420; + } + + if (expectedKind != RemoteMediaKind.Unknown && LooksLikeDirectMediaUri(candidate, expectedKind)) + { + score += 900; + } + else if (LooksLikeDirectMediaUri(candidate, RemoteMediaKind.Unknown)) + { + score += 240; + } + + return score; + } + + private static string SuggestedExtension(Uri uri, string contentType, RemoteMediaKind expectedKind) + { + var fromContentType = ExtensionFromContentType(contentType); + if (!string.IsNullOrWhiteSpace(fromContentType)) + { + return fromContentType; + } + + var fromPath = Path.GetExtension(uri.AbsolutePath); + if (!string.IsNullOrWhiteSpace(fromPath) && fromPath.Length <= 8) + { + return fromPath.StartsWith('.') ? fromPath : "." + fromPath; + } + + return expectedKind switch + { + RemoteMediaKind.Video => ".mp4", + RemoteMediaKind.Audio => ".mp3", + RemoteMediaKind.Image => ".jpg", + _ => ".bin" + }; + } + + private static string ExtensionFromContentType(string contentType) + { + return NormalizeContentType(contentType) switch + { + "image/jpeg" => ".jpg", + "image/png" => ".png", + "image/webp" => ".webp", + "image/gif" => ".gif", + "image/bmp" => ".bmp", + "video/mp4" => ".mp4", + "video/webm" => ".webm", + "video/quicktime" => ".mov", + "audio/mpeg" => ".mp3", + "audio/mp3" => ".mp3", + "audio/wav" => ".wav", + "audio/x-wav" => ".wav", + "audio/mp4" => ".m4a", + "audio/aac" => ".aac", + "audio/ogg" => ".ogg", + _ => string.Empty + }; + } + + private static string NormalizeContentType(string? contentType) + => (contentType ?? string.Empty).Trim().ToLowerInvariant(); + + private static bool UriEquals(Uri left, Uri right) + => string.Equals(left.AbsoluteUri, right.AbsoluteUri, StringComparison.OrdinalIgnoreCase); +} diff --git a/src/YMhut.Box.Core/Net/HttpRedirectResolver.cs b/src/YMhut.Box.Core/Net/HttpRedirectResolver.cs new file mode 100644 index 0000000..1b56ed6 --- /dev/null +++ b/src/YMhut.Box.Core/Net/HttpRedirectResolver.cs @@ -0,0 +1,44 @@ +using System.Net; + +namespace YMhut.Box.Core.Net; + +public static class HttpRedirectResolver +{ + public static async Task ResolveAsync( + Uri uri, + HttpMessageInvoker client, + int maxRedirects = 8, + CancellationToken cancellationToken = default) + { + var current = uri; + for (var redirect = 0; redirect < Math.Max(1, maxRedirects); redirect++) + { + using var request = new HttpRequestMessage(HttpMethod.Get, current); + using var response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false); + if (!IsRedirect(response.StatusCode)) + { + response.EnsureSuccessStatusCode(); + return current; + } + + var location = response.Headers.Location; + if (location is null) + { + return current; + } + + current = location.IsAbsoluteUri ? location : new Uri(current, location); + } + + return current; + } + + private static bool IsRedirect(HttpStatusCode statusCode) + { + return statusCode is HttpStatusCode.Moved or + HttpStatusCode.Redirect or + HttpStatusCode.RedirectMethod or + HttpStatusCode.TemporaryRedirect or + HttpStatusCode.PermanentRedirect; + } +} diff --git a/src/YMhut.Box.Core/Net/HttpService.cs b/src/YMhut.Box.Core/Net/HttpService.cs new file mode 100644 index 0000000..cfa4f40 --- /dev/null +++ b/src/YMhut.Box.Core/Net/HttpService.cs @@ -0,0 +1,356 @@ +using System.Net; +using System.Text; +using YMhut.Box.Core.Logging; +using YMhut.Box.Core.Settings; + +namespace YMhut.Box.Core.Net; + +public sealed record HttpServiceResult( + HttpStatusCode StatusCode, + string Content, + IReadOnlyDictionary Headers, + TimeSpan Elapsed); + +public sealed record HttpRequestPolicy( + TimeSpan Timeout, + int MaxRetries = 0, + TimeSpan? RetryDelay = null) +{ + public static HttpRequestPolicy Default { get; } = new(TimeSpan.FromSeconds(30)); + + public static HttpRequestPolicy RemoteTool { get; } = new(TimeSpan.FromSeconds(45), 1, TimeSpan.FromMilliseconds(650)); + + public static HttpRequestPolicy Diagnostics { get; } = new(TimeSpan.FromSeconds(15)); + + public static HttpRequestPolicy LongDownload { get; } = new(TimeSpan.FromMinutes(5)); +} + +public sealed class HttpRequestTimeoutException(Uri uri, TimeSpan timeout, Exception? innerException = null) + : TimeoutException($"HTTP request timed out after {timeout.TotalSeconds:0.#} seconds.", innerException) +{ + public Uri Uri { get; } = uri; + + public TimeSpan Timeout { get; } = timeout; +} + +public interface IHttpService +{ + Task GetAsync(Uri uri, CancellationToken cancellationToken = default); + + Task GetStringAsync(Uri uri, CancellationToken cancellationToken = default); + + Task SendAsync( + Uri uri, + string method = "GET", + string? body = null, + IReadOnlyDictionary? headers = null, + bool ensureSuccess = true, + HttpRequestPolicy? policy = null, + CancellationToken cancellationToken = default); +} + +public sealed class HttpService : IHttpService, IDisposable +{ + private readonly HttpClient _client; + private readonly ILogService? _logService; + private readonly ISettingsService? _settingsService; + private readonly bool _disposeClient; + + public HttpService(HttpClient? client = null, ILogService? logService = null, ISettingsService? settingsService = null) + { + _client = client ?? new HttpClient(); + _client.Timeout = Timeout.InfiniteTimeSpan; + ConfigureDefaultHeaders(_client); + _logService = logService; + _settingsService = settingsService; + _disposeClient = client is null; + } + + public async Task GetAsync(Uri uri, CancellationToken cancellationToken = default) + => await SendAsync(uri, cancellationToken: cancellationToken).ConfigureAwait(false); + + public async Task SendAsync( + Uri uri, + string method = "GET", + string? body = null, + IReadOnlyDictionary? headers = null, + bool ensureSuccess = true, + HttpRequestPolicy? policy = null, + CancellationToken cancellationToken = default) + { + policy ??= HttpRequestPolicy.Default; + var started = DateTimeOffset.UtcNow; + using var proxyClient = CreateProxyClientIfNeeded(); + var client = proxyClient ?? _client; + var normalizedMethod = string.IsNullOrWhiteSpace(method) ? "GET" : method.ToUpperInvariant(); + Exception? lastException = null; + + for (var attempt = 0; attempt <= Math.Max(0, policy.MaxRetries); attempt++) + { + using var timeoutCts = policy.Timeout == Timeout.InfiniteTimeSpan + ? null + : new CancellationTokenSource(policy.Timeout); + using var linkedCts = timeoutCts is null + ? CancellationTokenSource.CreateLinkedTokenSource(cancellationToken) + : CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token); + using var request = CreateRequest(uri, normalizedMethod, body, headers); + + try + { + using var response = await client.SendAsync(request, linkedCts.Token).ConfigureAwait(false); + var content = await response.Content.ReadAsStringAsync(linkedCts.Token).ConfigureAwait(false); + var elapsed = DateTimeOffset.UtcNow - started; + var responseHeaders = response.Headers + .Concat(response.Content.Headers) + .GroupBy(header => header.Key, StringComparer.OrdinalIgnoreCase) + .ToDictionary(group => group.Key, group => group.SelectMany(header => header.Value).ToArray(), StringComparer.OrdinalIgnoreCase); + + await WriteLogAsync( + response.IsSuccessStatusCode ? "Information" : "Warning", + "http", + $"HTTP request completed: {(int)response.StatusCode} {response.ReasonPhrase}", + cancellationToken).ConfigureAwait(false); + + if (ensureSuccess) + { + response.EnsureSuccessStatusCode(); + } + + return new HttpServiceResult(response.StatusCode, content, responseHeaders, elapsed); + } + catch (OperationCanceledException exception) when (!cancellationToken.IsCancellationRequested) + { + lastException = new HttpRequestTimeoutException(uri, policy.Timeout, exception); + if (!ShouldRetry(normalizedMethod, lastException, attempt, policy)) + { + throw lastException; + } + } + catch (Exception exception) when (ShouldRetry(normalizedMethod, exception, attempt, policy)) + { + lastException = exception; + } + + if (attempt < policy.MaxRetries) + { + await Task.Delay(policy.RetryDelay ?? TimeSpan.FromMilliseconds(500), cancellationToken).ConfigureAwait(false); + } + } + + throw lastException ?? new InvalidOperationException("HTTP request failed before a response was available."); + } + + public async Task GetStringAsync(Uri uri, CancellationToken cancellationToken = default) + => (await GetAsync(uri, cancellationToken).ConfigureAwait(false)).Content; + + public void Dispose() + { + if (_disposeClient) + { + _client.Dispose(); + } + } + + private Task WriteLogAsync(string level, string category, string message, CancellationToken cancellationToken) + { + return _logService?.WriteAsync(level, category, message, cancellationToken: cancellationToken) ?? Task.CompletedTask; + } + + private HttpClient? CreateProxyClientIfNeeded() + { + var settings = _settingsService?.Current; + if (settings is not { ProxyEnabled: true } || + string.Equals(settings.ProxyMode, "system", StringComparison.OrdinalIgnoreCase)) + { + return null; + } + + if (string.Equals(settings.ProxyMode, "direct", StringComparison.OrdinalIgnoreCase)) + { + var directClient = new HttpClient(new HttpClientHandler { UseProxy = false }, disposeHandler: true) + { + Timeout = Timeout.InfiniteTimeSpan + }; + ConfigureDefaultHeaders(directClient); + return directClient; + } + + if (string.IsNullOrWhiteSpace(settings.ProxyHost)) + { + return null; + } + + var builder = UriBuilderCache.Create(settings.ProxyHost, settings.ProxyPort); + var handler = new HttpClientHandler + { + Proxy = new WebProxy(builder.Uri), + UseProxy = true + }; + var client = new HttpClient(handler, disposeHandler: true) + { + Timeout = Timeout.InfiniteTimeSpan + }; + ConfigureDefaultHeaders(client); + return client; + } + + private static HttpRequestMessage CreateRequest( + Uri uri, + string method, + string? body, + IReadOnlyDictionary? headers) + { + var request = new HttpRequestMessage(new HttpMethod(method), uri); + if (body is not null && request.Method != HttpMethod.Get && request.Method != HttpMethod.Head) + { + request.Content = new StringContent(body, Encoding.UTF8, "text/plain"); + } + + ApplyRequestHeaders(request); + ApplyCustomHeaders(request, headers); + return request; + } + + private static bool ShouldRetry(string method, Exception exception, int attempt, HttpRequestPolicy policy) + { + if (attempt >= policy.MaxRetries || method is not ("GET" or "HEAD")) + { + return false; + } + + return exception is HttpRequestTimeoutException || + exception is TimeoutException || + exception is IOException || + exception is HttpRequestException { StatusCode: null }; + } + + private static void ConfigureDefaultHeaders(HttpClient client) + { + if (!client.DefaultRequestHeaders.UserAgent.Any()) + { + client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"); + client.DefaultRequestHeaders.UserAgent.ParseAdd("YMhutBox/2.0"); + } + + if (!client.DefaultRequestHeaders.Accept.Any()) + { + client.DefaultRequestHeaders.Accept.ParseAdd("application/json, text/plain, application/xml, text/xml, */*"); + } + + if (!client.DefaultRequestHeaders.AcceptLanguage.Any()) + { + client.DefaultRequestHeaders.AcceptLanguage.ParseAdd("zh-CN,zh;q=0.9,en;q=0.8"); + } + } + + private static void ApplyRequestHeaders(HttpRequestMessage request) + { + if (request.RequestUri is null) + { + return; + } + + var host = request.RequestUri.Host; + request.Headers.TryAddWithoutValidation("Cache-Control", "no-cache"); + request.Headers.TryAddWithoutValidation("Pragma", "no-cache"); + request.Headers.TryAddWithoutValidation("Upgrade-Insecure-Requests", "1"); + + if (host.Contains("12306.cn", StringComparison.OrdinalIgnoreCase)) + { + request.Headers.Referrer = new Uri("https://kyfw.12306.cn/otn/leftTicket/init"); + request.Headers.TryAddWithoutValidation("X-Requested-With", "XMLHttpRequest"); + } + + if (host.Contains("zhihu.com", StringComparison.OrdinalIgnoreCase)) + { + request.Headers.Referrer = new Uri("https://www.zhihu.com/hot"); + request.Headers.TryAddWithoutValidation("X-Requested-With", "fetch"); + request.Headers.TryAddWithoutValidation("Sec-Fetch-Site", "same-origin"); + } + + if (host.Contains("bilibili.com", StringComparison.OrdinalIgnoreCase)) + { + request.Headers.Referrer = new Uri("https://www.bilibili.com/v/popular/all"); + request.Headers.TryAddWithoutValidation("Origin", "https://www.bilibili.com"); + request.Headers.TryAddWithoutValidation("X-Requested-With", "XMLHttpRequest"); + request.Headers.TryAddWithoutValidation("Sec-Fetch-Site", "same-site"); + } + + if (host.Contains("baidu.com", StringComparison.OrdinalIgnoreCase)) + { + request.Headers.Referrer = new Uri("https://top.baidu.com/board?tab=realtime"); + request.Headers.TryAddWithoutValidation("Sec-Fetch-Site", "same-origin"); + } + + if (host.Contains("ndrc.gov.cn", StringComparison.OrdinalIgnoreCase)) + { + request.Headers.Referrer = new Uri("https://www.ndrc.gov.cn/"); + request.Headers.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); + } + + if (host.Contains("cbooo.cn", StringComparison.OrdinalIgnoreCase)) + { + request.Headers.Referrer = new Uri("https://www.cbooo.cn/"); + request.Headers.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); + } + + if (host.Contains("36kr.com", StringComparison.OrdinalIgnoreCase)) + { + request.Headers.Referrer = new Uri("https://36kr.com/"); + } + + if (host.Contains("cctv.com", StringComparison.OrdinalIgnoreCase)) + { + request.Headers.Referrer = new Uri("https://news.cctv.com/"); + } + } + + private static void ApplyCustomHeaders(HttpRequestMessage request, IReadOnlyDictionary? headers) + { + if (headers is null || headers.Count == 0) + { + return; + } + + foreach (var (name, value) in headers) + { + if (string.IsNullOrWhiteSpace(name) || IsBlockedHeader(name)) + { + continue; + } + + if (request.Content is not null && request.Content.Headers.TryAddWithoutValidation(name, value)) + { + continue; + } + + request.Headers.TryAddWithoutValidation(name, value); + } + } + + private static bool IsBlockedHeader(string name) + { + return name.Equals("host", StringComparison.OrdinalIgnoreCase) || + name.Equals("content-length", StringComparison.OrdinalIgnoreCase) || + name.Equals("connection", StringComparison.OrdinalIgnoreCase) || + name.Equals("transfer-encoding", StringComparison.OrdinalIgnoreCase); + } + + private static class UriBuilderCache + { + public static UriBuilder Create(string host, int port) + { + var normalized = host.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || + host.StartsWith("https://", StringComparison.OrdinalIgnoreCase) + ? host + : "http://" + host; + var builder = new UriBuilder(normalized); + if (port > 0) + { + builder.Port = port; + } + + return builder; + } + } +} diff --git a/src/YMhut.Box.Core/Plugins/BuiltIn/ipcheck-demo/README.md b/src/YMhut.Box.Core/Plugins/BuiltIn/ipcheck-demo/README.md new file mode 100644 index 0000000..3c81521 --- /dev/null +++ b/src/YMhut.Box.Core/Plugins/BuiltIn/ipcheck-demo/README.md @@ -0,0 +1,32 @@ +# IPCheck 网络工具箱内置示例插件 + +这是 YMhut Box 随程序发布的内置示例插件。插件资源嵌入在 `YMhut.Box.Core.dll` 中,插件系统首次启用或扫描时会复制到用户插件目录;如果用户已经修改同 ID 插件,程序会保留用户版本。 + +## 文件说明 + +- `ymhut.plugin.json`:插件声明文件,定义插件 ID、权限、工具入口和工具箱分类。 +- `index.html`:插件页面入口,适配主窗口内嵌和独立窗口内容区。 +- `style.css`:原创黑白极简点阵界面样式,卡片圆角控制在 8px。 +- `main.js`:插件主脚本,负责公网 IP、IPv4/IPv6、Cloudflare Trace、DNS 泄漏、WebRTC、测速、Ping、MTR、Whois/RDAP、MAC 厂商、ASN 连通性、规则测试、可达性检查、本机接口和浏览器指纹检测。 + +## Bridge 能力示例 + +页面底部的“Bridge 示例”卡片演示了三个常用能力: + +- 写入输出区:`window.ymhut.output.set(report)`,适合报告、日志摘要、可复制结果。 +- 保存私有状态:`window.ymhut.storage.set("lastSnapshot", value)`,只写入当前插件命名空间。 +- 打开安全链接:`window.ymhut.openExternal(url)`,默认进入 YMhut Box 安全浏览器;如需系统浏览器,必须显式传入 `{ target: "system" }` 并获得权限。 + +## 权限说明 + +插件声明 `Http`、`NetworkDiagnostics`、`Log`、`Output`、`Storage`、`Clipboard`、`OpenExternal`。用户启用并授权后,插件可通过 YMhut Bridge 执行必要的公网观测请求、本机网络诊断、日志记录、输出区写入、状态保存、报告复制和安全链接打开。 + +## 安全与边界 + +本示例只复刻 IPCheck 类工具的功能覆盖、内容结构和黑白极简风格,不复制受保护页面源码、品牌资产或私有接口。页面本体与工具交互均为内置原创实现;公网 IP、DNS 泄漏、RDAP、测速等必须由远端观测点才能完成的指标,会在插件授权后通过公开端点探测,并在失败时显示清晰降级状态。 + +插件 UI 不应覆盖宿主标题栏、输出区或系统窗口按钮。需要展示长报告时写入宿主输出区;主操作界面应保留在插件内容区内,避免 fixed 全屏遮罩和超高 z-index 点击层。 + +## AI 实现提示 + +给其他 AI 生成插件时,可以把本示例作为最小可运行模板:保留 `ymhut.plugin.json`、`README.md`、`index.html`、`style.css`、`main.js` 五个核心文件,按需减少权限,并在 README 中解释每个权限的用途。不要依赖远程脚本或修改 YMhut Box 内置资源。 diff --git a/src/YMhut.Box.Core/Plugins/BuiltIn/ipcheck-demo/index.html b/src/YMhut.Box.Core/Plugins/BuiltIn/ipcheck-demo/index.html new file mode 100644 index 0000000..3b51c2d --- /dev/null +++ b/src/YMhut.Box.Core/Plugins/BuiltIn/ipcheck-demo/index.html @@ -0,0 +1,307 @@ + + + + + + IPCheck 网络工具箱 + + + + +
+
+ +
+

All in one IP Toolbox

+

正在检测...

+

正在并行检测公网视角、本机网络、DNS、WebRTC、浏览器指纹和链路质量。

+
+
+ + +
+
+ +
+ + + +
+
+
+
+

网络身份

+

IP 地址与地理位置

+
+ 检测中 +
+
+
+ +
+
+
+

协议栈

+

IPv4 / IPv6

+
+ 检测中 +
+
+
+ +
+
+
+

边缘网络

+

Cloudflare Trace

+
+ 检测中 +
+
+
+ +
+
+
+

质量判断

+

IP 质量与风险

+
+ 检测中 +
+
+
+
+ +
+
+
+
+

隐私

+

WebRTC 泄漏

+
+ 检测中 +
+
+
+ +
+
+
+

解析

+

DNS 泄漏

+
+ 检测中 +
+
+
+ +
+
+
+

可见性

+

IP 泄漏对照

+
+ 检测中 +
+
+
+
+ +
+
+
+
+

连通性

+

全球延迟

+
+ 检测中 +
+
+
+ +
+
+
+

吞吐

+

网络测速

+
+ 检测中 +
+
--Mbps
+

下载与上传测速将在授权 HTTP 后执行;无网络时显示本机链路速率。

+
+ +
+
+
+

规则

+

安全检查清单

+
+ 检测中 +
+
+
+
+ +
+
+
+

Advanced Tools

+

高级网络工具

+
+ 待输入 +
+ +
+
+

IP 查询

+
+ + +
+
等待查询。
+
+ +
+

DNS Resolver

+
+ + +
+
等待解析。
+
+ +
+

Ping / Global Latency

+
+ + +
+
等待测试。
+
+ +
+

MTR / Trace Route

+
+ + +
+
等待追踪。
+
+ +
+

Whois / RDAP

+
+ + +
+
等待查询。
+
+ +
+

MAC 厂商查询

+
+ + +
+
等待识别。
+
+ +
+

ASN Connectivity

+
+ + +
+
等待分析。
+
+ +
+

Rule Test

+
+ + +
+ +
等待测试。
+
+ +
+

Censorship Check

+
+ + +
+
等待检查。
+
+ +
+

Invisibility Test

+
+ + +
+
等待评估。
+
+
+
+ +
+
+
+
+

本机环境

+

浏览器指纹

+
+ 本地 +
+
+
+ +
+
+
+

接口

+

本机网络接口

+
+ 检测中 +
+
+
+
+ +
+
+
+
+

Bridge 示例

+

输出、存储与安全链接

+
+ 待操作 +
+

这些按钮演示插件如何写入宿主输出区、保存插件私有状态,以及默认用安全浏览器打开外链。

+
+ + + +
+
等待 Bridge 操作。
+
+
+ +
+ 报告未复制 +
+
+ + + + diff --git a/src/YMhut.Box.Core/Plugins/BuiltIn/ipcheck-demo/main.js b/src/YMhut.Box.Core/Plugins/BuiltIn/ipcheck-demo/main.js new file mode 100644 index 0000000..840086f --- /dev/null +++ b/src/YMhut.Box.Core/Plugins/BuiltIn/ipcheck-demo/main.js @@ -0,0 +1,907 @@ +// 插件主脚本:所有页面、工具逻辑和降级策略均内置;公网 IP/测速/DNS 泄漏等必须依赖远端观测点的项目才通过授权 HTTP 探测。 +const $ = (id) => document.getElementById(id); +const state = { + diagnostics: null, + publicIp: null, + trace: null, + browser: {}, + webrtc: [], + dnsProbe: null, + latency: {}, + speed: null, + lookup: null, + checks: {}, + startedAt: null +}; + +const endpoints = { + ipApis: [ + { name: "ipapi.co", url: "https://ipapi.co/json/" }, + { name: "ipwho.is", url: "https://ipwho.is/" }, + { name: "ip.sb", url: "https://api.ip.sb/geoip" } + ], + ipv4: "https://api.ipify.org?format=json", + ipv6: "https://api64.ipify.org?format=json", + trace: "https://speed.cloudflare.com/cdn-cgi/trace", + speedDown: "https://speed.cloudflare.com/__down?bytes=1000000", + speedUp: "https://speed.cloudflare.com/__up", + doh: "https://cloudflare-dns.com/dns-query", + rdapIp: "https://rdap.org/ip/", + rdapDomain: "https://rdap.org/domain/", + mac: "https://api.macvendors.com/" +}; + +const macVendors = { + "001A2B": "Ayecom Technology", + "001B63": "Apple", + "001C42": "Parallels", + "002248": "Microsoft", + "005056": "VMware", + "080027": "PCS Systemtechnik / VirtualBox", + "3C5A37": "Google", + "F4F5D8": "Google", + "D850E6": "ASUSTek", + "FCFBFB": "Cisco", + "B827EB": "Raspberry Pi" +}; + +function escapeHtml(value) { + return String(value ?? "") + .replaceAll("&", "&") + .replaceAll("<", "<") + .replaceAll(">", ">") + .replaceAll('"', """) + .replaceAll("'", "'"); +} + +function setBadge(id, status, text) { + const el = $(id); + if (!el) return; + el.className = `badge ${status}`; + el.textContent = text; +} + +function setFacts(id, rows) { + $(id).innerHTML = rows + .map(([key, value]) => `
${escapeHtml(key)}
${escapeHtml(value || "--")}
`) + .join(""); +} + +function setMetrics(id, rows) { + $(id).innerHTML = rows + .map(([key, value, tone = ""]) => ` +
+ ${escapeHtml(key)} + ${escapeHtml(value || "--")} +
`) + .join(""); +} + +function setChecks(id, rows) { + $(id).innerHTML = rows + .map(([key, value, tone = ""]) => ` +
+ ${escapeHtml(key)} + ${escapeHtml(value || "--")} +
`) + .join(""); +} + +function setOutput(id, value) { + $(id).textContent = typeof value === "string" ? value : JSON.stringify(value, null, 2); +} + +function flatten(values) { + return [...new Set((values || []).flat().filter(Boolean))]; +} + +function activeInterfaces() { + return (state.diagnostics?.interfaces || []).filter((item) => item.status === "Up"); +} + +function shortJson(value) { + return JSON.stringify(value, null, 2) + .replaceAll("\\u0022", "\"") + .slice(0, 6000); +} + +function parseJson(content) { + try { + return JSON.parse(content); + } catch { + return null; + } +} + +async function bridgeFetch(request) { + const response = await window.ymhut.http.fetch(request); + if (!response.ok) { + throw new Error(`HTTP ${response.status}`); + } + return response; +} + +async function fetchJson(url, options = {}) { + const response = await bridgeFetch({ url, headers: options.headers, method: options.method, body: options.body }); + return { data: parseJson(response.content), response }; +} + +async function firstSuccessful(tasks) { + const errors = []; + for (const task of tasks) { + try { + const value = await task(); + return value; + } catch (error) { + errors.push(error.message); + } + } + throw new Error(errors.join("; ")); +} + +function normalizeIpInfo(source, data) { + if (!data) return null; + if (source === "ipapi.co") { + return { + source, + ip: data.ip, + version: data.version, + city: data.city, + region: data.region, + country: data.country_name || data.country, + countryCode: data.country_code, + timezone: data.timezone, + latitude: data.latitude, + longitude: data.longitude, + asn: data.asn, + isp: data.org, + postal: data.postal, + raw: data + }; + } + if (source === "ipwho.is") { + return { + source, + ip: data.ip, + version: data.type, + city: data.city, + region: data.region, + country: data.country, + countryCode: data.country_code, + timezone: data.timezone?.id, + latitude: data.latitude, + longitude: data.longitude, + asn: data.connection?.asn ? `AS${data.connection.asn}` : "", + isp: data.connection?.isp || data.connection?.org, + postal: data.postal, + raw: data + }; + } + return { + source, + ip: data.ip || data.address, + version: data.version, + city: data.city, + region: data.region, + country: data.country, + countryCode: data.country_code, + timezone: data.timezone, + latitude: data.latitude, + longitude: data.longitude, + asn: data.asn, + isp: data.organization || data.isp, + postal: data.postal_code, + raw: data + }; +} + +function parseTrace(text) { + const rows = {}; + String(text || "").split(/\r?\n/).forEach((line) => { + const index = line.indexOf("="); + if (index > 0) rows[line.slice(0, index)] = line.slice(index + 1); + }); + return rows; +} + +async function loadDiagnostics() { + try { + const diagnostics = await window.ymhut.network.diagnostics(); + state.diagnostics = diagnostics; + state.checks.host = true; + setBadge("hostStatus", "ok", "本地"); + } catch (error) { + state.checks.host = false; + state.diagnostics = { interfaces: [], summary: {}, proxy: {}, note: error.message }; + setBadge("hostStatus", "bad", "失败"); + } +} + +async function loadPublicIp() { + try { + const info = await firstSuccessful(endpoints.ipApis.map((item) => async () => { + const { data } = await fetchJson(item.url); + const normalized = normalizeIpInfo(item.name, data); + if (!normalized?.ip) throw new Error(`${item.name} 未返回 IP`); + return normalized; + })); + state.publicIp = info; + state.checks.publicIp = true; + } catch (error) { + state.publicIp = { error: error.message }; + state.checks.publicIp = false; + } +} + +async function loadTrace() { + try { + const response = await bridgeFetch({ url: endpoints.trace }); + state.trace = parseTrace(response.content); + state.checks.trace = true; + } catch (error) { + state.trace = { error: error.message }; + state.checks.trace = false; + } +} + +async function loadIpVersions() { + const result = { ipv4: null, ipv6: null }; + try { + const { data } = await fetchJson(endpoints.ipv4); + result.ipv4 = data?.ip || null; + } catch (error) { + result.ipv4Error = error.message; + } + try { + const { data } = await fetchJson(endpoints.ipv6); + result.ipv6 = data?.ip || null; + } catch (error) { + result.ipv6Error = error.message; + } + state.ipVersions = result; +} + +function renderIdentity() { + const summary = state.diagnostics?.summary || {}; + const active = activeInterfaces(); + const localIpv4 = flatten(active.map((item) => item.ipv4)); + const localIpv6 = flatten(active.map((item) => item.ipv6)); + const publicIp = state.publicIp?.ip; + + $("primaryIp").textContent = publicIp || localIpv4[0] || localIpv6[0] || "网络未就绪"; + $("primarySummary").textContent = publicIp + ? `${state.publicIp.isp || "未知 ISP"} · ${[state.publicIp.city, state.publicIp.region, state.publicIp.country].filter(Boolean).join(" / ") || "未知位置"} · ${state.publicIp.asn || "未知 ASN"}` + : "未获得公网观测结果,已展示本机可见网络信息。"; + + setFacts("ipDetails", [ + ["公网 IP", publicIp || "未获得"], + ["ASN / ISP", [state.publicIp?.asn, state.publicIp?.isp].filter(Boolean).join(" / ")], + ["国家地区", [state.publicIp?.city, state.publicIp?.region, state.publicIp?.country].filter(Boolean).join(" / ")], + ["经纬度", state.publicIp?.latitude ? `${state.publicIp.latitude}, ${state.publicIp.longitude}` : ""], + ["时区", state.publicIp?.timezone || state.diagnostics?.localTimeZone], + ["活动接口", active.map((item) => item.name).join(" / ")], + ["本机 IPv4", localIpv4.join(" / ")], + ["本机 IPv6", localIpv6.join(" / ")], + ["默认网关", (summary.defaultGateways || []).join(" / ")], + ["DNS 服务器", (summary.dnsServers || []).join(" / ")], + ["观测来源", state.publicIp?.source || state.publicIp?.error || "本地"] + ]); + setBadge("ipStatus", publicIp ? "ok" : "warn", publicIp ? "完成" : "降级"); +} + +function renderStack() { + const active = activeInterfaces(); + const localIpv4 = flatten(active.map((item) => item.ipv4)); + const localIpv6 = flatten(active.map((item) => item.ipv6)); + const publicV4 = state.ipVersions?.ipv4; + const publicV6 = state.ipVersions?.ipv6; + setMetrics("stackDetails", [ + ["公网 IPv4", publicV4 || state.ipVersions?.ipv4Error || "未检测到", publicV4 ? "ok" : "warn"], + ["公网 IPv6", publicV6 || state.ipVersions?.ipv6Error || "未检测到", publicV6 ? "ok" : "warn"], + ["本机 IPv4", localIpv4.length ? `${localIpv4.length} 个地址` : "未发现", localIpv4.length ? "ok" : "bad"], + ["本机 IPv6", localIpv6.length ? `${localIpv6.length} 个地址` : "未发现", localIpv6.length ? "ok" : "warn"], + ["双栈状态", (publicV4 || localIpv4.length) && (publicV6 || localIpv6.length) ? "双栈可见" : "非完整双栈", (publicV4 || localIpv4.length) && (publicV6 || localIpv6.length) ? "ok" : "warn"] + ]); + setBadge("stackStatus", publicV4 || publicV6 || localIpv4.length || localIpv6.length ? "ok" : "bad", "完成"); +} + +function renderTrace() { + const trace = state.trace || {}; + setFacts("traceDetails", [ + ["Colo", trace.colo || "--"], + ["HTTP", trace.http || "--"], + ["TLS", trace.tls || "--"], + ["WARP", trace.warp || "--"], + ["Gateway", trace.gateway || "--"], + ["SNI", trace.sni || "--"], + ["IP", trace.ip || "--"], + ["错误", trace.error || ""] + ]); + setBadge("traceStatus", trace.colo ? "ok" : "warn", trace.colo ? "完成" : "降级"); +} + +function renderQuality() { + const publicIp = state.publicIp || {}; + const proxy = state.diagnostics?.proxy || {}; + const trace = state.trace || {}; + const active = activeInterfaces(); + const localIps = flatten(active.map((item) => [...(item.ipv4 || []), ...(item.ipv6 || [])])); + const hints = []; + if (proxy.enabled) hints.push("系统代理已启用"); + if (trace.warp === "on" || trace.warp === "plus") hints.push("检测到 WARP"); + if (publicIp.error) hints.push("公网观测失败"); + if (localIps.some((ip) => ip.startsWith("10.") || ip.startsWith("192.168.") || ip.startsWith("172."))) hints.push("本机存在私网地址"); + const risk = publicIp.error ? "中" : proxy.enabled ? "需复核" : "低"; + setMetrics("qualityDetails", [ + ["质量评级", risk, risk === "低" ? "ok" : "warn"], + ["代理/VPN 线索", hints.join(" / ") || "未发现明显线索", hints.length ? "warn" : "ok"], + ["ASN 信息", publicIp.asn || "未知", publicIp.asn ? "ok" : "warn"], + ["运营商", publicIp.isp || "未知", publicIp.isp ? "ok" : "warn"], + ["观测一致性", state.ipVersions?.ipv4 && publicIp.ip && state.ipVersions.ipv4 !== publicIp.ip ? "IPv4 观测不一致" : "未发现冲突", "ok"] + ]); + setBadge("qualityStatus", risk === "低" ? "ok" : "warn", risk); +} + +function renderBrowser() { + const canvas = document.createElement("canvas"); + const gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl"); + const debug = gl?.getExtension("WEBGL_debug_renderer_info"); + state.browser = { + language: navigator.language, + languages: navigator.languages?.join(" / "), + platform: navigator.platform, + userAgent: navigator.userAgent, + timezone: Intl.DateTimeFormat().resolvedOptions().timeZone, + screen: `${screen.width}x${screen.height} / DPR ${window.devicePixelRatio}`, + online: navigator.onLine ? "在线" : "离线", + cookies: navigator.cookieEnabled ? "启用" : "禁用", + hardwareConcurrency: navigator.hardwareConcurrency, + memory: navigator.deviceMemory ? `${navigator.deviceMemory} GB` : "未暴露", + touch: navigator.maxTouchPoints || 0, + webglVendor: debug ? gl.getParameter(debug.UNMASKED_VENDOR_WEBGL) : "未暴露", + webglRenderer: debug ? gl.getParameter(debug.UNMASKED_RENDERER_WEBGL) : "未暴露" + }; + setFacts("browserDetails", [ + ["语言", state.browser.language], + ["语言列表", state.browser.languages], + ["平台", state.browser.platform], + ["时区", state.browser.timezone], + ["屏幕", state.browser.screen], + ["在线状态", state.browser.online], + ["Cookie", state.browser.cookies], + ["CPU 线程", state.browser.hardwareConcurrency], + ["内存", state.browser.memory], + ["触控点", state.browser.touch], + ["WebGL Vendor", state.browser.webglVendor], + ["WebGL Renderer", state.browser.webglRenderer], + ["UA", state.browser.userAgent] + ]); +} + +async function renderWebRtc() { + if (!window.RTCPeerConnection) { + $("webrtcDetails").textContent = "当前 WebView2 环境不支持 RTCPeerConnection。"; + setBadge("webrtcStatus", "warn", "不可用"); + return; + } + + const candidates = new Set(); + try { + const pc = new RTCPeerConnection({ iceServers: [{ urls: "stun:stun.l.google.com:19302" }] }); + pc.createDataChannel("probe"); + pc.onicecandidate = (event) => { + if (event.candidate?.candidate) candidates.add(event.candidate.candidate); + }; + await pc.setLocalDescription(await pc.createOffer()); + await new Promise((resolve) => setTimeout(resolve, 1800)); + pc.close(); + state.webrtc = Array.from(candidates); + $("webrtcDetails").textContent = state.webrtc.length + ? state.webrtc.join("\n") + : "未暴露候选地址,或当前 WebView2 策略阻止采集。"; + const leaksPublic = state.webrtc.some((line) => /(srflx|relay)/i.test(line)); + setBadge("webrtcStatus", leaksPublic ? "warn" : "ok", leaksPublic ? "有候选" : "未发现"); + } catch (error) { + $("webrtcDetails").textContent = `WebRTC 检测失败:${error.message}`; + setBadge("webrtcStatus", "bad", "失败"); + } +} + +async function renderDns() { + const active = activeInterfaces(); + const rows = active.map((item) => [ + item.name, + item.dnsServers?.length ? item.dnsServers.join(" / ") : "未配置", + item.dnsServers?.length ? "ok" : "warn" + ]); + if (rows.length === 0) rows.push(["DNS", "未发现活动接口", "bad"]); + + try { + const probeName = `ymhut-${Date.now()}.cloudflare.com`; + const query = `${endpoints.doh}?name=${encodeURIComponent(probeName)}&type=A`; + const response = await bridgeFetch({ url: query, headers: { accept: "application/dns-json" } }); + state.dnsProbe = parseJson(response.content) || {}; + rows.push(["DoH 探测", `${response.status} / ${Math.round(response.elapsedMs)} ms`, "ok"]); + rows.push(["泄漏判断", "已列出本机 DNS;远端递归出口需专用回显域名才能精确归因", "warn"]); + } catch (error) { + rows.push(["DoH 探测", error.message, "warn"]); + } + + setMetrics("dnsDetails", rows); + setBadge("dnsStatus", rows.some((row) => row[2] === "ok") ? "ok" : "warn", "完成"); +} + +function renderLeakComparison() { + const local = flatten(activeInterfaces().map((item) => [...(item.ipv4 || []), ...(item.ipv6 || [])])); + const publicValues = [state.publicIp?.ip, state.ipVersions?.ipv4, state.ipVersions?.ipv6, state.trace?.ip].filter(Boolean); + const webrtcValues = state.webrtc.join("\n"); + const leakedLocal = local.filter((ip) => webrtcValues.includes(ip)); + setMetrics("leakDetails", [ + ["公网观测", publicValues.join(" / ") || "未获得", publicValues.length ? "ok" : "warn"], + ["WebRTC 本机地址", leakedLocal.join(" / ") || "未暴露完整本机地址", leakedLocal.length ? "warn" : "ok"], + ["代理一致性", state.diagnostics?.proxy?.enabled ? "系统代理已启用,建议复核浏览器出口" : "未启用系统代理", state.diagnostics?.proxy?.enabled ? "warn" : "ok"], + ["Trace 对照", state.trace?.ip && state.publicIp?.ip && state.trace.ip !== state.publicIp.ip ? "不同观测点结果不一致" : "未发现明显冲突", "ok"] + ]); + setBadge("leakStatus", leakedLocal.length ? "warn" : "ok", leakedLocal.length ? "需注意" : "正常"); +} + +async function renderLatency() { + const targets = [ + ["Cloudflare", "1.1.1.1"], + ["Google DNS", "8.8.8.8"], + ["Quad9", "9.9.9.9"] + ]; + const rows = []; + for (const [name, host] of targets) { + try { + const result = await window.ymhut.network.ping({ host, count: 3, timeoutMs: 1800 }); + state.latency[name] = result; + rows.push([name, result.avgMs >= 0 ? `${result.avgMs} ms / 丢包 ${result.lossPercent}%` : "无响应", result.avgMs >= 0 ? "ok" : "warn"]); + } catch (error) { + rows.push([name, error.message, "warn"]); + } + } + rows.push(["DOM 响应", `${Math.round(performance.now() - state.startedAt)} ms`, "ok"]); + setMetrics("latencyDetails", rows); + setBadge("latencyStatus", rows.some((row) => row[2] === "ok") ? "ok" : "warn", "完成"); +} + +async function renderSpeed() { + const active = activeInterfaces(); + const maxLink = Math.max(0, ...active.map((item) => Number(item.speedMbps || 0))); + try { + const downStart = performance.now(); + const down = await bridgeFetch({ url: endpoints.speedDown }); + const downSeconds = Math.max(0.001, (performance.now() - downStart) / 1000); + const downMbps = (Number(down.content.length || 1000000) * 8 / downSeconds / 1000000); + + const upPayload = "0".repeat(250000); + const upStart = performance.now(); + await bridgeFetch({ url: endpoints.speedUp, method: "POST", body: upPayload, headers: { "content-type": "text/plain" } }); + const upSeconds = Math.max(0.001, (performance.now() - upStart) / 1000); + const upMbps = (upPayload.length * 8 / upSeconds / 1000000); + + state.speed = { downloadMbps: downMbps, uploadMbps: upMbps }; + $("speedValue").textContent = downMbps.toFixed(1); + $("speedUnit").textContent = "Mbps down"; + $("speedNote").textContent = `上传 ${upMbps.toFixed(1)} Mbps;本机最大链路 ${maxLink ? `${maxLink.toLocaleString()} Mbps` : "未知"}。`; + setBadge("speedStatus", "ok", "完成"); + } catch (error) { + $("speedValue").textContent = maxLink ? maxLink.toLocaleString() : "--"; + $("speedUnit").textContent = "Mbps link"; + $("speedNote").textContent = maxLink + ? `测速失败:${error.message}。当前显示本机网卡报告链路速率。` + : `测速失败:${error.message},且未发现可用链路速率。`; + setBadge("speedStatus", maxLink ? "warn" : "bad", maxLink ? "链路" : "失败"); + } +} + +function renderSecurity() { + const rows = [ + ["HTTPS / TLS", state.trace?.tls ? `TLS ${state.trace.tls}` : "未获得 Trace", state.trace?.tls ? "ok" : "warn"], + ["Cloudflare WARP", state.trace?.warp || "未知", state.trace?.warp === "off" ? "ok" : "warn"], + ["系统代理", state.diagnostics?.proxy?.enabled ? `${state.diagnostics.proxy.mode} ${state.diagnostics.proxy.host || ""}` : "未启用", state.diagnostics?.proxy?.enabled ? "warn" : "ok"], + ["WebRTC 泄漏", state.webrtc.length ? "存在候选地址,需检查是否暴露真实地址" : "未发现候选地址", state.webrtc.length ? "warn" : "ok"], + ["DNS 配置", (state.diagnostics?.summary?.dnsServers || []).length ? "已发现 DNS 服务器" : "未发现 DNS", (state.diagnostics?.summary?.dnsServers || []).length ? "ok" : "warn"], + ["浏览器指纹", "已采集 UA、语言、屏幕、WebGL、硬件线程等本地指标", "info"] + ]; + setChecks("securityDetails", rows); + setBadge("securityStatus", rows.some((row) => row[2] === "bad") ? "bad" : rows.some((row) => row[2] === "warn") ? "warn" : "ok", "完成"); +} + +function renderInterfaces() { + const items = state.diagnostics?.interfaces || []; + if (items.length === 0) { + $("interfaceDetails").innerHTML = '
接口未发现
'; + return; + } + $("interfaceDetails").innerHTML = items.map((item) => ` +
+
+ ${escapeHtml(item.name)} + ${escapeHtml(item.description)} +
+
+
状态
${escapeHtml(item.status)} · ${escapeHtml(item.type)}
+
速率
${escapeHtml(item.speedMbps ? `${item.speedMbps} Mbps` : "--")}
+
IPv4
${escapeHtml((item.ipv4 || []).join(" / ") || "--")}
+
IPv6
${escapeHtml((item.ipv6 || []).join(" / ") || "--")}
+
网关
${escapeHtml((item.gateways || []).join(" / ") || "--")}
+
DNS
${escapeHtml((item.dnsServers || []).join(" / ") || "--")}
+
+
`).join(""); +} + +function renderStatusStrip() { + const summary = state.diagnostics?.summary || {}; + const items = [ + ["公网 IP", state.publicIp?.ip || "未知"], + ["IPv4 / IPv6", `${state.ipVersions?.ipv4 ? "4" : "-"} / ${state.ipVersions?.ipv6 ? "6" : "-"}`], + ["活动接口", summary.activeInterfaceCount ?? 0], + ["DNS", (summary.dnsServers || []).length] + ]; + $("statusStrip").innerHTML = items.map(([label, value]) => ` +
+ ${escapeHtml(label)} + ${escapeHtml(value)} +
`).join(""); +} + +function buildReport() { + const summary = state.diagnostics?.summary || {}; + return [ + "YMhut Box IPCheck 网络诊断报告", + `生成时间:${new Date().toLocaleString()}`, + `公网 IP:${state.publicIp?.ip || "--"}`, + `ASN/ISP:${[state.publicIp?.asn, state.publicIp?.isp].filter(Boolean).join(" / ") || "--"}`, + `位置:${[state.publicIp?.city, state.publicIp?.region, state.publicIp?.country].filter(Boolean).join(" / ") || "--"}`, + `IPv4:${state.ipVersions?.ipv4 || "--"}`, + `IPv6:${state.ipVersions?.ipv6 || "--"}`, + `Cloudflare Trace:${state.trace?.colo || "--"} / WARP ${state.trace?.warp || "--"}`, + `活动接口:${summary.activeInterfaceCount ?? 0}`, + `DNS:${(summary.dnsServers || []).join(" / ") || "--"}`, + `网关:${(summary.defaultGateways || []).join(" / ") || "--"}`, + `WebRTC:${state.webrtc.join(" | ") || "未发现候选地址"}`, + `测速:${state.speed ? `${state.speed.downloadMbps.toFixed(1)} down / ${state.speed.uploadMbps.toFixed(1)} up Mbps` : "--"}`, + `浏览器:${state.browser.userAgent || "--"}` + ].join("\n"); +} + +async function copyReport() { + try { + const report = buildReport(); + await window.ymhut.clipboard.writeText(report); + await window.ymhut.output.set(report); + setBadge("copyStatus", "ok", "已复制"); + } catch (error) { + setBadge("copyStatus", "bad", "复制失败"); + await window.ymhut.log.warn("IPCheck copy failed", error.message); + } +} + +async function writeOutputDemo() { + try { + const report = buildReport(); + await window.ymhut.output.set(report); + $("bridgeOutput").textContent = "已把当前诊断报告写入宿主输出区。"; + setBadge("bridgeStatus", "ok", "输出已写入"); + } catch (error) { + $("bridgeOutput").textContent = `输出失败:${error.message}`; + setBadge("bridgeStatus", "bad", "输出失败"); + } +} + +async function saveSnapshotDemo() { + try { + const snapshot = { + savedAt: new Date().toISOString(), + ip: state.publicIp?.ip || "", + ipv4: state.ipVersions?.ipv4 || "", + ipv6: state.ipVersions?.ipv6 || "", + colo: state.trace?.colo || "" + }; + await window.ymhut.storage.set("lastSnapshot", JSON.stringify(snapshot)); + const stored = await window.ymhut.storage.get("lastSnapshot"); + $("bridgeOutput").textContent = `已保存插件私有状态:\n${stored}`; + setBadge("bridgeStatus", "ok", "快照已保存"); + } catch (error) { + $("bridgeOutput").textContent = `保存失败:${error.message}`; + setBadge("bridgeStatus", "bad", "保存失败"); + } +} + +async function openGuideDemo() { + try { + await window.ymhut.openExternal("https://github.com/YMhut/box-winUI3#plugins"); + $("bridgeOutput").textContent = "已请求使用安全浏览器打开插件规范链接。"; + setBadge("bridgeStatus", "ok", "链接已打开"); + } catch (error) { + $("bridgeOutput").textContent = `打开链接失败:${error.message}`; + setBadge("bridgeStatus", "bad", "链接失败"); + } +} + +function cleanHost(value) { + return String(value || "") + .trim() + .replace(/^https?:\/\//i, "") + .split(/[/?#]/)[0]; +} + +function isIp(value) { + return /^(\d{1,3}\.){3}\d{1,3}$/.test(value) || value.includes(":"); +} + +function ipToNumber(ip) { + const parts = String(ip).split(".").map(Number); + if (parts.length !== 4 || parts.some((part) => Number.isNaN(part) || part < 0 || part > 255)) return null; + return (((parts[0] << 24) >>> 0) + (parts[1] << 16) + (parts[2] << 8) + parts[3]) >>> 0; +} + +function domainMatches(target, domain) { + const host = cleanHost(target).toLowerCase(); + const needle = String(domain || "").toLowerCase(); + return host === needle || host.endsWith(`.${needle}`); +} + +function cidrMatches(ip, cidr) { + const [base, bitsText] = String(cidr || "").split("/"); + const bits = Number(bitsText); + const ipNumber = ipToNumber(ip); + const baseNumber = ipToNumber(base); + if (ipNumber === null || baseNumber === null || Number.isNaN(bits) || bits < 0 || bits > 32) return false; + const mask = bits === 0 ? 0 : (0xffffffff << (32 - bits)) >>> 0; + return (ipNumber & mask) === (baseNumber & mask); +} + +async function runLookup() { + const value = cleanHost($("lookupInput").value || $("primaryIp").textContent); + if (!value) return; + setBadge("toolStatus", "pending", "查询中"); + setOutput("lookupOutput", "正在查询 IP 信息..."); + try { + const url = isIp(value) ? `https://ipapi.co/${encodeURIComponent(value)}/json/` : `https://ipapi.co/${encodeURIComponent(value)}/json/`; + const { data } = await fetchJson(url); + state.lookup = normalizeIpInfo("ipapi.co", data); + setOutput("lookupOutput", shortJson(state.lookup || data)); + setBadge("toolStatus", "ok", "完成"); + } catch (error) { + setOutput("lookupOutput", `查询失败:${error.message}`); + setBadge("toolStatus", "bad", "失败"); + } +} + +async function runDnsLookup() { + const value = cleanHost($("dnsInput").value || "example.com"); + setOutput("dnsOutput", "正在解析..."); + try { + const bridge = await window.ymhut.network.dnsLookup({ host: value }); + let doh = null; + try { + const { data } = await fetchJson(`${endpoints.doh}?name=${encodeURIComponent(value)}&type=A`, { headers: { accept: "application/dns-json" } }); + doh = data; + } catch { + doh = null; + } + setOutput("dnsOutput", shortJson({ systemResolver: bridge, cloudflareDoh: doh })); + } catch (error) { + setOutput("dnsOutput", `解析失败:${error.message}`); + } +} + +async function runPing() { + const value = cleanHost($("pingInput").value || "1.1.1.1"); + setOutput("pingOutput", "正在 Ping..."); + try { + const result = await window.ymhut.network.ping({ host: value, count: 5, timeoutMs: 2500 }); + setOutput("pingOutput", shortJson(result)); + } catch (error) { + setOutput("pingOutput", `Ping 失败:${error.message}`); + } +} + +async function runTraceRoute() { + const value = cleanHost($("traceInput").value || "1.1.1.1"); + setOutput("traceOutput", "正在追踪路由..."); + try { + const result = await window.ymhut.network.traceRoute({ host: value, maxHops: 16, timeoutMs: 2200 }); + setOutput("traceOutput", shortJson(result)); + } catch (error) { + setOutput("traceOutput", `追踪失败:${error.message}`); + } +} + +async function runWhois() { + const value = cleanHost($("whoisInput").value || $("primaryIp").textContent); + setOutput("whoisOutput", "正在查询 RDAP..."); + try { + const url = `${isIp(value) ? endpoints.rdapIp : endpoints.rdapDomain}${encodeURIComponent(value)}`; + const { data } = await fetchJson(url); + setOutput("whoisOutput", shortJson(data)); + } catch (error) { + setOutput("whoisOutput", `RDAP 查询失败:${error.message}`); + } +} + +async function runMacLookup() { + const raw = $("macInput").value.trim(); + const oui = raw.replace(/[^0-9a-f]/gi, "").slice(0, 6).toUpperCase(); + if (oui.length < 6) { + setOutput("macOutput", "请输入至少 6 位十六进制 OUI。"); + return; + } + if (macVendors[oui]) { + setOutput("macOutput", `${raw}\nOUI: ${oui}\n厂商: ${macVendors[oui]}\n来源: 内置常用 OUI 表`); + return; + } + setOutput("macOutput", "正在查询厂商..."); + try { + const response = await bridgeFetch({ url: `${endpoints.mac}${encodeURIComponent(raw)}` }); + setOutput("macOutput", `${raw}\nOUI: ${oui}\n厂商: ${response.content.trim()}`); + } catch (error) { + setOutput("macOutput", `${raw}\nOUI: ${oui}\n未在内置表命中,在线查询失败:${error.message}`); + } +} + +async function runAsnConnectivity() { + const value = cleanHost($("asnInput").value || state.publicIp?.ip || $("primaryIp").textContent); + setOutput("asnOutput", "正在分析 ASN 连通性..."); + try { + const ipValue = value.toUpperCase().startsWith("AS") ? state.publicIp?.ip : value; + const rdap = ipValue ? (await fetchJson(`${endpoints.rdapIp}${encodeURIComponent(ipValue)}`)).data : null; + const cidrs = (rdap?.cidr0_cidrs || []).map((item) => `${item.v4prefix || item.v6prefix}/${item.length}`); + const pings = []; + for (const target of ["1.1.1.1", "8.8.8.8", "9.9.9.9"]) { + try { + const ping = await window.ymhut.network.ping({ host: target, count: 2, timeoutMs: 1800 }); + pings.push({ target, avgMs: ping.avgMs, lossPercent: ping.lossPercent }); + } catch (error) { + pings.push({ target, error: error.message }); + } + } + setOutput("asnOutput", shortJson({ + input: value, + publicIp: state.publicIp?.ip, + asn: state.publicIp?.asn, + isp: state.publicIp?.isp, + rdapName: rdap?.name, + country: rdap?.country, + cidrs, + reachability: pings + })); + } catch (error) { + setOutput("asnOutput", `ASN 分析失败:${error.message}`); + } +} + +async function runRuleTest() { + const target = cleanHost($("ruleTargetInput").value || state.publicIp?.ip || "example.com"); + const ip = isIp(target) ? target : state.publicIp?.ip; + const country = (state.publicIp?.countryCode || "").toUpperCase(); + const rules = ($("ruleInput").value || "DOMAIN-SUFFIX,example.com\nIP-CIDR,1.1.1.0/24\nGEOIP,CN") + .split(/\r?\n/) + .map((line) => line.trim()) + .filter(Boolean); + const results = rules.map((line) => { + const [typeRaw, valueRaw] = line.split(",").map((part) => part?.trim()); + const type = (typeRaw || "").toUpperCase(); + const value = valueRaw || ""; + let matched = false; + if (type === "DOMAIN" || type === "DOMAIN-SUFFIX") matched = domainMatches(target, value); + if (type === "IP-CIDR") matched = ip ? cidrMatches(ip, value) : false; + if (type === "GEOIP") matched = country === value.toUpperCase(); + if (type === "KEYWORD") matched = target.toLowerCase().includes(value.toLowerCase()); + return { rule: line, matched }; + }); + setOutput("ruleOutput", shortJson({ target, ip, country, results, firstMatch: results.find((item) => item.matched) || null })); +} + +async function runCensorshipCheck() { + const custom = $("censorInput").value.trim(); + const targets = custom ? [custom] : [ + "https://www.cloudflare.com/cdn-cgi/trace", + "https://www.google.com/generate_204", + "https://www.wikipedia.org/", + "https://www.github.com/" + ]; + setOutput("censorOutput", "正在检查可达性..."); + const rows = []; + for (const target of targets) { + const url = /^https?:\/\//i.test(target) ? target : `https://${target}`; + try { + const started = performance.now(); + const response = await window.ymhut.http.fetch({ url }); + rows.push({ url, ok: response.ok, status: response.status, elapsedMs: Math.round(performance.now() - started) }); + } catch (error) { + rows.push({ url, ok: false, error: error.message }); + } + } + setOutput("censorOutput", shortJson(rows)); +} + +async function runInvisibilityTest() { + const expected = $("invisibleInput").value.trim().toUpperCase(); + const local = activeInterfaces(); + const dns = flatten(local.map((item) => item.dnsServers)); + const localIps = flatten(local.map((item) => [...(item.ipv4 || []), ...(item.ipv6 || [])])); + const publicIp = state.publicIp?.ip; + const country = (state.publicIp?.countryCode || "").toUpperCase(); + const findings = [ + { item: "公网 IP", value: publicIp || "未知", status: publicIp ? "ok" : "warn" }, + { item: "期望国家", value: expected || "未指定", status: expected && country && expected !== country ? "warn" : "ok" }, + { item: "WebRTC 候选", value: state.webrtc.length ? `${state.webrtc.length} 条` : "未发现", status: state.webrtc.length ? "warn" : "ok" }, + { item: "本机私网地址", value: localIps.filter((ip) => ip.startsWith("10.") || ip.startsWith("192.168.") || ip.startsWith("172.")).join(" / ") || "未发现", status: "ok" }, + { item: "DNS 服务器", value: dns.join(" / ") || "未知", status: dns.length ? "ok" : "warn" }, + { item: "系统代理", value: state.diagnostics?.proxy?.enabled ? state.diagnostics.proxy.mode : "未启用", status: state.diagnostics?.proxy?.enabled ? "warn" : "ok" }, + { item: "Trace WARP", value: state.trace?.warp || "未知", status: state.trace?.warp === "off" ? "ok" : "warn" } + ]; + setOutput("invisibleOutput", shortJson({ + score: findings.filter((item) => item.status === "warn").length === 0 ? "隐私暴露线索较少" : "存在需要复核的暴露线索", + country, + expectedCountry: expected || null, + findings + })); +} + +async function runAll() { + state.startedAt = performance.now(); + ["ipStatus", "stackStatus", "traceStatus", "hostStatus", "webrtcStatus", "dnsStatus", "latencyStatus", "speedStatus", "qualityStatus", "securityStatus", "leakStatus"].forEach((id) => setBadge(id, "pending", "检测中")); + $("primaryIp").textContent = "正在检测..."; + $("primarySummary").textContent = "正在并行检测公网视角、本机网络、DNS、WebRTC、浏览器指纹和链路质量。"; + await loadDiagnostics(); + await Promise.allSettled([loadPublicIp(), loadTrace(), loadIpVersions()]); + renderBrowser(); + renderIdentity(); + renderStack(); + renderTrace(); + renderQuality(); + await renderWebRtc(); + await renderDns(); + renderLeakComparison(); + await renderLatency(); + await renderSpeed(); + renderSecurity(); + renderInterfaces(); + renderStatusStrip(); + await window.ymhut.log.info("IPCheck diagnostics completed", JSON.stringify({ + publicIp: state.publicIp?.ip, + ipv4: state.ipVersions?.ipv4, + ipv6: state.ipVersions?.ipv6, + colo: state.trace?.colo + })); +} + +function wireAdvancedTools() { + $("lookupBtn").addEventListener("click", runLookup); + $("dnsBtn").addEventListener("click", runDnsLookup); + $("pingBtn").addEventListener("click", runPing); + $("traceBtn").addEventListener("click", runTraceRoute); + $("whoisBtn").addEventListener("click", runWhois); + $("macBtn").addEventListener("click", runMacLookup); + $("asnBtn").addEventListener("click", runAsnConnectivity); + $("ruleBtn").addEventListener("click", runRuleTest); + $("censorBtn").addEventListener("click", runCensorshipCheck); + $("invisibleBtn").addEventListener("click", runInvisibilityTest); + ["lookupInput", "dnsInput", "pingInput", "traceInput", "whoisInput", "macInput", "asnInput", "ruleTargetInput", "censorInput", "invisibleInput"].forEach((id) => { + $(id).addEventListener("keydown", (event) => { + if (event.key !== "Enter") return; + event.preventDefault(); + const buttonId = { + ruleTargetInput: "ruleBtn", + censorInput: "censorBtn", + invisibleInput: "invisibleBtn" + }[id] || id.replace("Input", "Btn"); + $(buttonId)?.click(); + }); + }); +} + +document.addEventListener("DOMContentLoaded", () => { + $("rerunBtn").addEventListener("click", runAll); + $("copyBtn").addEventListener("click", copyReport); + $("outputDemoBtn").addEventListener("click", writeOutputDemo); + $("storageDemoBtn").addEventListener("click", saveSnapshotDemo); + $("guideDemoBtn").addEventListener("click", openGuideDemo); + wireAdvancedTools(); + runAll(); +}); diff --git a/src/YMhut.Box.Core/Plugins/BuiltIn/ipcheck-demo/style.css b/src/YMhut.Box.Core/Plugins/BuiltIn/ipcheck-demo/style.css new file mode 100644 index 0000000..0d4e64f --- /dev/null +++ b/src/YMhut.Box.Core/Plugins/BuiltIn/ipcheck-demo/style.css @@ -0,0 +1,519 @@ +/* 原创黑白极简网络仪表盘:点阵背景、紧凑卡片和状态徽章,适配独立插件窗口。 */ +:root { + color-scheme: light; + --bg: #f7f7f4; + --ink: #101010; + --muted: #606064; + --line: #deded8; + --panel: rgba(255, 255, 255, 0.92); + --panel-strong: #ffffff; + --panel-soft: #efefeb; + --ok: #0b7a3b; + --warn: #946100; + --bad: #b42318; + --info: #245aa5; +} + +* { + box-sizing: border-box; +} + +html { + scroll-behavior: smooth; +} + +body { + margin: 0; + min-height: 100vh; + color: var(--ink); + font-family: "Segoe UI", "Microsoft YaHei", system-ui, sans-serif; + background: + radial-gradient(#d0d0ca 1px, transparent 1px) 0 0 / 22px 22px, + linear-gradient(180deg, #fbfbf8 0%, var(--bg) 100%); +} + +button, +input { + font: inherit; +} + +button { + border: 1px solid var(--ink); + background: #ffffff; + color: var(--ink); + min-height: 38px; + padding: 0 14px; + border-radius: 6px; + font-weight: 700; + cursor: pointer; +} + +button.primary, +button:hover { + background: var(--ink); + color: #ffffff; +} + +button:disabled { + cursor: progress; + opacity: 0.58; +} + +input { + width: 100%; + min-height: 38px; + border: 1px solid var(--line); + border-radius: 6px; + padding: 0 11px; + background: #ffffff; + color: var(--ink); +} + +textarea { + width: 100%; + min-height: 84px; + margin-top: 10px; + border: 1px solid var(--line); + border-radius: 6px; + padding: 10px 11px; + resize: vertical; + background: #ffffff; + color: var(--ink); + font: 12px/1.5 "Cascadia Mono", Consolas, monospace; +} + +input:focus, +textarea:focus, +button:focus-visible, +a:focus-visible { + outline: 2px solid #111111; + outline-offset: 2px; +} + +a { + color: inherit; +} + +.shell { + width: min(1220px, calc(100vw - 36px)); + margin: 0 auto; + padding: 28px 0 34px; +} + +.hero { + min-height: 172px; + display: grid; + grid-template-columns: auto minmax(0, 1fr) auto; + gap: 20px; + align-items: end; + padding: 26px; + border: 1px solid var(--line); + border-radius: 8px; + background: var(--panel); +} + +.brandMark { + width: 58px; + height: 58px; + position: relative; + align-self: start; + display: grid; + place-items: center; +} + +.brandMark::before, +.brandMark::after { + content: ""; + position: absolute; + inset: 0; + border: 1px solid var(--ink); + border-radius: 50%; + opacity: 0.16; +} + +.brandMark::after { + inset: 11px; + opacity: 0.36; +} + +.brandMark span { + width: 18px; + height: 18px; + border: 2px solid var(--ink); + border-radius: 50%; + background: #ffffff; +} + +.heroText { + min-width: 0; +} + +.eyebrow, +.label { + margin: 0 0 6px; + color: var(--muted); + font-size: 12px; + text-transform: uppercase; + letter-spacing: 0; + font-weight: 800; +} + +h1, +h2, +h3 { + margin: 0; + letter-spacing: 0; +} + +h1 { + font-size: clamp(34px, 5vw, 66px); + line-height: 1.02; + word-break: break-all; +} + +h2 { + font-size: 18px; +} + +h3 { + font-size: 16px; +} + +.summary, +.muted { + margin: 10px 0 0; + color: var(--muted); + line-height: 1.58; +} + +.heroActions, +.formRow { + display: flex; + gap: 10px; +} + +.bridgeActions { + margin-top: 12px; + flex-wrap: wrap; +} + +.heroActions { + flex-wrap: wrap; + justify-content: flex-end; +} + +.quickStats { + display: grid; + grid-template-columns: repeat(4, minmax(0, 1fr)); + gap: 10px; + margin: 12px 0; +} + +.statusItem { + border: 1px solid var(--line); + border-radius: 8px; + padding: 12px; + background: rgba(255, 255, 255, 0.78); +} + +.statusItem span { + color: var(--muted); + font-size: 12px; + font-weight: 700; +} + +.statusItem strong { + display: block; + font-size: 22px; + margin-top: 4px; + overflow-wrap: anywhere; +} + +.toolNav { + position: sticky; + top: 0; + z-index: 3; + display: flex; + gap: 8px; + flex-wrap: wrap; + padding: 10px 0 12px; + backdrop-filter: blur(12px); +} + +.toolNav a { + min-height: 32px; + display: inline-flex; + align-items: center; + border: 1px solid var(--line); + border-radius: 999px; + padding: 0 12px; + background: rgba(255, 255, 255, 0.86); + color: var(--muted); + text-decoration: none; + font-size: 13px; + font-weight: 750; +} + +.toolNav a:hover { + color: var(--ink); + border-color: var(--ink); +} + +.grid, +.toolGrid { + display: grid; + grid-template-columns: repeat(3, minmax(0, 1fr)); + gap: 12px; + margin-bottom: 12px; +} + +.toolGrid { + grid-template-columns: repeat(2, minmax(0, 1fr)); + margin-bottom: 0; +} + +.card, +.toolPanel { + border: 1px solid var(--line); + border-radius: 8px; + background: var(--panel); +} + +.card { + min-height: 248px; + padding: 18px; +} + +.toolCard { + min-height: 260px; +} + +.toolPanel { + padding: 18px; + margin-bottom: 12px; +} + +.span2 { + grid-column: span 2; +} + +.cardHeader, +.sectionHeader { + display: flex; + align-items: start; + justify-content: space-between; + gap: 12px; + margin-bottom: 14px; +} + +.badge { + display: inline-flex; + align-items: center; + min-height: 26px; + border-radius: 999px; + border: 1px solid var(--line); + padding: 0 10px; + white-space: nowrap; + font-size: 12px; + font-weight: 800; +} + +.badge.ok { + color: var(--ok); + border-color: rgba(11, 122, 59, 0.35); + background: rgba(11, 122, 59, 0.08); +} + +.badge.warn, +.badge.pending { + color: var(--warn); + border-color: rgba(148, 97, 0, 0.35); + background: rgba(148, 97, 0, 0.08); +} + +.badge.bad { + color: var(--bad); + border-color: rgba(180, 35, 24, 0.35); + background: rgba(180, 35, 24, 0.08); +} + +.badge.info { + color: var(--info); + border-color: rgba(36, 90, 165, 0.35); + background: rgba(36, 90, 165, 0.08); +} + +.facts { + display: grid; + grid-template-columns: 134px minmax(0, 1fr); + gap: 10px 14px; + margin: 0; +} + +.facts.compact { + grid-template-columns: 96px minmax(0, 1fr); +} + +dt { + color: var(--muted); + font-weight: 700; +} + +dd { + margin: 0; + min-width: 0; + overflow-wrap: anywhere; +} + +.metricList, +.checkList { + display: grid; + gap: 10px; +} + +.metric, +.checkItem { + display: grid; + grid-template-columns: minmax(0, 1fr) auto; + gap: 10px; + align-items: center; + padding: 10px 0; + border-bottom: 1px solid var(--line); +} + +.metric:last-child, +.checkItem:last-child { + border-bottom: 0; +} + +.metric strong, +.checkItem strong { + overflow-wrap: anywhere; + text-align: right; +} + +.okText { + color: var(--ok); +} + +.warnText { + color: var(--warn); +} + +.badText { + color: var(--bad); +} + +.monoBlock { + min-height: 148px; + padding: 12px; + border-radius: 6px; + background: var(--panel-soft); + color: #222222; + font-family: "Cascadia Mono", Consolas, monospace; + font-size: 12px; + line-height: 1.55; + overflow: auto; + overflow-wrap: anywhere; + white-space: pre-wrap; +} + +.monoBlock.small { + min-height: 156px; + max-height: 260px; + margin-top: 12px; +} + +.speedValue { + display: flex; + align-items: baseline; + gap: 8px; + font-weight: 850; + font-size: 52px; +} + +.speedValue small { + color: var(--muted); + font-size: 18px; +} + +.interfaceList { + display: grid; + gap: 12px; +} + +.interfaceItem { + display: grid; + gap: 10px; + padding: 12px; + border: 1px solid var(--line); + border-radius: 8px; + background: var(--panel-soft); +} + +.interfaceItem strong, +.interfaceItem span { + display: block; +} + +.interfaceItem span { + margin-top: 3px; + color: var(--muted); + overflow-wrap: anywhere; +} + +.copyState { + display: flex; + justify-content: flex-end; + margin-top: 12px; +} + +@media (max-width: 980px) { + .hero, + .quickStats, + .grid, + .toolGrid { + grid-template-columns: 1fr; + } + + .span2 { + grid-column: auto; + } + + .heroActions { + justify-content: flex-start; + } +} + +@media (max-width: 620px) { + .shell { + width: min(100vw - 22px, 1220px); + padding-top: 16px; + } + + .hero { + grid-template-columns: 1fr; + padding: 18px; + } + + .brandMark { + width: 44px; + height: 44px; + } + + .formRow { + flex-direction: column; + } + + .facts, + .facts.compact, + .metric, + .checkItem { + grid-template-columns: 1fr; + } + + .metric strong, + .checkItem strong { + text-align: left; + } +} diff --git a/src/YMhut.Box.Core/Plugins/BuiltIn/ipcheck-demo/ymhut.plugin.json b/src/YMhut.Box.Core/Plugins/BuiltIn/ipcheck-demo/ymhut.plugin.json new file mode 100644 index 0000000..8c1c008 --- /dev/null +++ b/src/YMhut.Box.Core/Plugins/BuiltIn/ipcheck-demo/ymhut.plugin.json @@ -0,0 +1,52 @@ +{ + "id": "ipcheck-demo", + "name": "IPCheck 网络工具箱", + "version": "1.2.0", + "author": "YMhut Box", + "builtIn": true, + "description": "内置示例插件:以独立窗口运行的 IP、地理位置、DNS、WebRTC、浏览器指纹、测速、Ping、MTR、Whois/RDAP 和网络质量检测工具。", + "entry": "index.html", + "permissions": [ + "Http", + "Log", + "Output", + "Storage", + "Clipboard", + "OpenExternal", + "NetworkDiagnostics" + ], + "surfaces": [ + { + "kind": "ToolboxTool", + "id": "ipcheck", + "name": "IPCheck 网络检测", + "description": "内置 IP 工具箱:公网 IP、ASN/ISP、地理位置、IPv4/IPv6、DNS/WebRTC 泄漏、测速、Ping、MTR、Whois/RDAP、MAC 厂商、规则测试、可达性与浏览器指纹。", + "entry": "index.html", + "category": "plugin", + "keywords": [ + "ip", + "network", + "dns", + "webrtc", + "ipv4", + "ipv6", + "latency", + "speed", + "whois", + "rdap", + "mtr", + "rule", + "censorship", + "asn", + "fingerprint" + ], + "iconGlyph": "\uE968" + } + ], + "resources": [ + "index.html", + "style.css", + "main.js", + "README.md" + ] +} diff --git a/src/YMhut.Box.Core/Plugins/BuiltInPluginInstallerService.cs b/src/YMhut.Box.Core/Plugins/BuiltInPluginInstallerService.cs new file mode 100644 index 0000000..e5ccf0e --- /dev/null +++ b/src/YMhut.Box.Core/Plugins/BuiltInPluginInstallerService.cs @@ -0,0 +1,206 @@ +using YMhut.Box.Core.App; +using YMhut.Box.Core.Logging; +using YMhut.Box.Core.Settings; +using System.Security.Cryptography; +using System.Text; + +namespace YMhut.Box.Core.Plugins; + +public interface IBuiltInPluginInstallerService +{ + Task EnsureInstalledAsync(CancellationToken cancellationToken = default); +} + +public sealed class BuiltInPluginInstallerService( + AppPaths paths, + ILogService? logService = null, + ISettingsService? settingsService = null) : IBuiltInPluginInstallerService +{ + private const string ResourcePrefix = "YMhut.Box.Core.Plugins.BuiltIn."; + private const string FingerprintFileName = ".ymhut-built-in.sha256"; + private static readonly string[] KnownRootFiles = + [ + "README.md", + "ymhut.plugin.json", + "index.html", + "style.css", + "main.js" + ]; + private readonly SemaphoreSlim _gate = new(1, 1); + + public string PluginsRoot => PluginRegistryService.ResolvePluginsRoot(paths, settingsService?.Current.PluginRootPath); + + public async Task EnsureInstalledAsync(CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + Directory.CreateDirectory(PluginsRoot); + var plugins = DiscoverEmbeddedPlugins(); + if (plugins.Count == 0) + { + await WriteLogAsync("Warning", "No built-in plugin resources found", PluginsRoot, cancellationToken).ConfigureAwait(false); + return; + } + + foreach (var plugin in plugins) + { + var targetRoot = Path.Combine(PluginsRoot, plugin.FolderName); + if (Directory.Exists(targetRoot)) + { + if (await IsUnmodifiedBuiltInPluginAsync(targetRoot, cancellationToken).ConfigureAwait(false)) + { + Directory.Delete(targetRoot, recursive: true); + await ExtractEmbeddedPluginAsync(plugin, targetRoot, cancellationToken).ConfigureAwait(false); + await WriteLogAsync("Information", "Built-in plugin refreshed", targetRoot, cancellationToken).ConfigureAwait(false); + } + + continue; + } + + await ExtractEmbeddedPluginAsync(plugin, targetRoot, cancellationToken).ConfigureAwait(false); + await WriteLogAsync("Information", "Built-in plugin installed", targetRoot, cancellationToken).ConfigureAwait(false); + } + } + finally + { + _gate.Release(); + } + } + + private static async Task IsUnmodifiedBuiltInPluginAsync(string targetRoot, CancellationToken cancellationToken) + { + var fingerprintPath = Path.Combine(targetRoot, FingerprintFileName); + if (!File.Exists(fingerprintPath)) + { + return false; + } + + try + { + var saved = (await File.ReadAllTextAsync(fingerprintPath, cancellationToken).ConfigureAwait(false)).Trim(); + if (string.IsNullOrWhiteSpace(saved)) + { + return false; + } + + var current = await ComputeDirectoryFingerprintAsync(targetRoot, cancellationToken).ConfigureAwait(false); + return string.Equals(saved, current, StringComparison.OrdinalIgnoreCase); + } + catch (IOException) + { + return false; + } + catch (UnauthorizedAccessException) + { + return false; + } + } + + private static async Task ExtractEmbeddedPluginAsync(EmbeddedBuiltInPlugin plugin, string targetRoot, CancellationToken cancellationToken) + { + var assembly = typeof(BuiltInPluginInstallerService).Assembly; + Directory.CreateDirectory(targetRoot); + foreach (var resource in plugin.Resources) + { + cancellationToken.ThrowIfCancellationRequested(); + var target = Path.GetFullPath(Path.Combine(targetRoot, resource.RelativePath)); + if (!PluginRegistryService.IsInside(targetRoot, target)) + { + throw new InvalidDataException($"Built-in plugin resource escapes target directory: {resource.ResourceName}"); + } + + Directory.CreateDirectory(Path.GetDirectoryName(target)!); + await using var input = assembly.GetManifestResourceStream(resource.ResourceName) + ?? throw new InvalidOperationException($"Built-in plugin resource cannot be opened: {resource.ResourceName}"); + await using var output = File.Create(target); + await input.CopyToAsync(output, cancellationToken).ConfigureAwait(false); + } + + var fingerprint = await ComputeDirectoryFingerprintAsync(targetRoot, cancellationToken).ConfigureAwait(false); + await File.WriteAllTextAsync(Path.Combine(targetRoot, FingerprintFileName), fingerprint, Encoding.UTF8, cancellationToken).ConfigureAwait(false); + } + + private static async Task ComputeDirectoryFingerprintAsync(string targetRoot, CancellationToken cancellationToken) + { + using var sha = SHA256.Create(); + var files = Directory.EnumerateFiles(targetRoot, "*", SearchOption.AllDirectories) + .Where(path => !string.Equals(Path.GetFileName(path), FingerprintFileName, StringComparison.OrdinalIgnoreCase)) + .Select(path => Path.GetFullPath(path)) + .Order(StringComparer.OrdinalIgnoreCase); + foreach (var file in files) + { + cancellationToken.ThrowIfCancellationRequested(); + var relative = file.Substring(Path.GetFullPath(targetRoot).Length).TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar).Replace('\\', '/'); + var nameBytes = Encoding.UTF8.GetBytes(relative); + sha.TransformBlock(nameBytes, 0, nameBytes.Length, null, 0); + sha.TransformBlock([0], 0, 1, null, 0); + var content = await File.ReadAllBytesAsync(file, cancellationToken).ConfigureAwait(false); + sha.TransformBlock(content, 0, content.Length, null, 0); + sha.TransformBlock([0], 0, 1, null, 0); + } + + sha.TransformFinalBlock([], 0, 0); + return Convert.ToHexString(sha.Hash ?? []); + } + + private Task WriteLogAsync(string level, string message, string detail, CancellationToken cancellationToken) + { + return logService?.WriteAsync(level, "plugin", message, detail, cancellationToken) ?? Task.CompletedTask; + } + + private static IReadOnlyList DiscoverEmbeddedPlugins() + { + var assembly = typeof(BuiltInPluginInstallerService).Assembly; + var resources = assembly.GetManifestResourceNames() + .Where(name => name.StartsWith(ResourcePrefix, StringComparison.Ordinal)) + .Select(ParseResource) + .OfType() + .GroupBy(resource => resource.PluginToken, StringComparer.OrdinalIgnoreCase) + .Select(group => new EmbeddedBuiltInPlugin( + DecodePluginFolder(group.Key), + group.OrderBy(resource => resource.RelativePath, StringComparer.OrdinalIgnoreCase).ToArray())) + .Where(plugin => plugin.Resources.Any(resource => string.Equals(resource.RelativePath, PluginManifest.FileName, StringComparison.OrdinalIgnoreCase))) + .OrderBy(plugin => plugin.FolderName, StringComparer.OrdinalIgnoreCase) + .ToArray(); + + return resources; + } + + private static EmbeddedBuiltInResource? ParseResource(string resourceName) + { + var relativeName = resourceName[ResourcePrefix.Length..]; + foreach (var fileName in KnownRootFiles) + { + var suffix = "." + fileName; + if (!relativeName.EndsWith(suffix, StringComparison.OrdinalIgnoreCase)) + { + continue; + } + + var token = relativeName[..^suffix.Length]; + if (string.IsNullOrWhiteSpace(token)) + { + return null; + } + + return new EmbeddedBuiltInResource(token, fileName, resourceName); + } + + return null; + } + + private static string DecodePluginFolder(string token) + { + return token.Replace('_', '-'); + } + + private sealed record EmbeddedBuiltInPlugin( + string FolderName, + IReadOnlyList Resources); + + private sealed record EmbeddedBuiltInResource( + string PluginToken, + string RelativePath, + string ResourceName); +} diff --git a/src/YMhut.Box.Core/Plugins/PluginHostProtocol.cs b/src/YMhut.Box.Core/Plugins/PluginHostProtocol.cs new file mode 100644 index 0000000..a242088 --- /dev/null +++ b/src/YMhut.Box.Core/Plugins/PluginHostProtocol.cs @@ -0,0 +1,163 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace YMhut.Box.Core.Plugins; + +public static class PluginHostProtocol +{ + public const string Version = "1"; + public const string Ready = "ready"; + public const string Ping = "ping"; + public const string Pong = "pong"; + public const string GetSnapshot = "getSnapshot"; + public const string Reload = "reload"; + public const string SetPluginEnabled = "setPluginEnabled"; + public const string SetPermission = "setPermission"; + public const string SetSurfaceMounted = "setSurfaceMounted"; + public const string BridgeCall = "bridgeCall"; + public const string SnapshotChanged = "snapshotChanged"; + public const string Shutdown = "shutdown"; + public const string Error = "error"; + + private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web) + { + WriteIndented = false, + PropertyNameCaseInsensitive = true, + Converters = { new JsonStringEnumConverter(), new JsonStringEnumConverter() } + }; + + public static string Serialize(PluginHostMessage message) + { + return JsonSerializer.Serialize(message, JsonOptions); + } + + public static PluginHostMessage? Deserialize(string line) + { + return JsonSerializer.Deserialize(line, JsonOptions); + } +} + +public enum PluginHostStatus +{ + Stopped, + Starting, + Ready, + Failed +} + +public sealed record PluginHostMessage( + string Type, + string RequestId = "", + string? Version = null, + string? PluginId = null, + string? SurfaceId = null, + PluginPermission? Permission = null, + bool? Enabled = null, + bool? Granted = null, + bool? Mounted = null, + PluginSnapshot? Snapshot = null, + PluginBridgeRequest? BridgeRequest = null, + PluginBridgeResponse? BridgeResponse = null, + string? Error = null); + +public sealed record PluginSnapshot( + bool PluginsEnabled, + string PluginsRoot, + IReadOnlyList Plugins, + IReadOnlyList EnabledTools, + DateTimeOffset LoadedAt); + +public sealed record LoadedPluginDto( + PluginManifest Manifest, + string RootPath, + PluginRuntimeStateDto State, + IReadOnlyList Errors) +{ + public bool IsValid => Errors.Count == 0; + + public LoadedPlugin ToLoadedPlugin() + { + return new LoadedPlugin( + Manifest, + RootPath, + State.ToRuntimeState(), + Errors); + } + + public static LoadedPluginDto FromLoadedPlugin(LoadedPlugin plugin) + { + return new LoadedPluginDto( + plugin.Manifest, + plugin.RootPath, + PluginRuntimeStateDto.FromRuntimeState(plugin.State), + plugin.Errors.ToArray()); + } +} + +public sealed record PluginRuntimeStateDto( + string PluginId, + bool Enabled, + IReadOnlyList GrantedPermissions, + IReadOnlyList MountedSurfaceIds, + DateTimeOffset? LastRunAt) +{ + public PluginRuntimeState ToRuntimeState() + { + return new PluginRuntimeState( + PluginId, + Enabled, + GrantedPermissions.ToHashSet(), + MountedSurfaceIds.ToHashSet(StringComparer.OrdinalIgnoreCase), + LastRunAt); + } + + public static PluginRuntimeStateDto FromRuntimeState(PluginRuntimeState state) + { + return new PluginRuntimeStateDto( + state.PluginId, + state.Enabled, + state.GrantedPermissions.ToArray(), + state.MountedSurfaceIds.ToArray(), + state.LastRunAt); + } +} + +public sealed record PluginToolDto( + string PluginId, + string SurfaceId, + string ToolId, + PluginManifest Manifest, + PluginSurface Surface, + string RootPath, + PluginRuntimeStateDto State) +{ + public PluginToolModule ToToolModule() + { + var plugin = new LoadedPlugin(Manifest, RootPath, State.ToRuntimeState(), []); + return new PluginToolModule(plugin, Surface); + } + + public static PluginToolDto FromPluginToolModule(PluginToolModule module) + { + return new PluginToolDto( + module.Plugin.Manifest.Id, + module.Surface.Id, + module.Id, + module.Plugin.Manifest, + module.Surface, + module.Plugin.RootPath, + PluginRuntimeStateDto.FromRuntimeState(module.Plugin.State)); + } +} + +public sealed record PluginBridgeRequest( + string PluginId, + string SurfaceId, + string Method, + string PayloadJson); + +public sealed record PluginBridgeResponse( + bool Ok, + string? ValueJson = null, + string? Error = null, + string? UiAction = null); diff --git a/src/YMhut.Box.Core/Plugins/PluginLogService.cs b/src/YMhut.Box.Core/Plugins/PluginLogService.cs new file mode 100644 index 0000000..920a02c --- /dev/null +++ b/src/YMhut.Box.Core/Plugins/PluginLogService.cs @@ -0,0 +1,24 @@ +using YMhut.Box.Core.Logging; + +namespace YMhut.Box.Core.Plugins; + +public sealed class PluginLogService(ILogService logService) +{ + public Task WriteAsync( + string pluginId, + string level, + string message, + string? detail = null, + CancellationToken cancellationToken = default) + { + return logService.WriteAsync(level, $"plugin:{pluginId}", message, detail, cancellationToken); + } + + public Task> ReadAsync( + string pluginId, + int take = 200, + CancellationToken cancellationToken = default) + { + return logService.ReadAsync(category: $"plugin:{pluginId}", take: take, cancellationToken: cancellationToken); + } +} diff --git a/src/YMhut.Box.Core/Plugins/PluginModels.cs b/src/YMhut.Box.Core/Plugins/PluginModels.cs new file mode 100644 index 0000000..3146b2a --- /dev/null +++ b/src/YMhut.Box.Core/Plugins/PluginModels.cs @@ -0,0 +1,127 @@ +using System.Text.Json.Serialization; +using YMhut.Box.Core.Tools; + +namespace YMhut.Box.Core.Plugins; + +public enum PluginPermission +{ + Input, + Output, + Log, + Storage, + Http, + Clipboard, + FilePicker, + RunTool, + OpenExternal, + NetworkDiagnostics +} + +public enum PluginSurfaceKind +{ + ToolboxTool, + NavPage +} + +public sealed record PluginManifest( + string Id, + string Name, + string Version, + string Author, + string Description, + string Entry, + IReadOnlyList Permissions, + IReadOnlyList Surfaces, + IReadOnlyList Resources) +{ + public static readonly string FileName = "ymhut.plugin.json"; +} + +public sealed record PluginSurface( + PluginSurfaceKind Kind, + string Id, + string Name, + string Description, + string Entry = "", + string Category = "dev", + IReadOnlyList? Keywords = null, + string IconGlyph = "\uECAA") +{ + public string EffectiveEntry(string pluginEntry) => string.IsNullOrWhiteSpace(Entry) ? pluginEntry : Entry; +} + +public sealed record PluginRuntimeState( + string PluginId, + bool Enabled, + IReadOnlySet GrantedPermissions, + IReadOnlySet MountedSurfaceIds, + DateTimeOffset? LastRunAt); + +public sealed record LoadedPlugin( + PluginManifest Manifest, + string RootPath, + PluginRuntimeState State, + IReadOnlyList Errors) +{ + public bool IsValid => Errors.Count == 0; +} + +public sealed class PluginToolModule(LoadedPlugin plugin, PluginSurface surface) : IToolModule +{ + public LoadedPlugin Plugin { get; } = plugin; + + public PluginSurface Surface { get; } = surface; + + public string Id { get; } = PluginIds.ToolId(plugin.Manifest.Id, surface.Id); + + public ToolMetadata Metadata { get; } = new( + PluginIds.ToolId(plugin.Manifest.Id, surface.Id), + surface.Name, + surface.Description, + ToolCategory.Plugin, + (surface.Keywords ?? []).Concat(["plugin", plugin.Manifest.Id, plugin.Manifest.Name]).Distinct(StringComparer.OrdinalIgnoreCase).ToArray(), + false, + string.IsNullOrWhiteSpace(surface.IconGlyph) ? "\uECAA" : surface.IconGlyph); + + public object CreateViewModel() => new ToolModuleViewModel(Metadata); +} + +public static class PluginIds +{ + public const string Prefix = "plugin:"; + + public static bool IsPluginToolId(string id) => id.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase); + + public static string ToolId(string pluginId, string surfaceId) => $"{Prefix}{Normalize(pluginId)}:{Normalize(surfaceId)}"; + + public static string Normalize(string id) => id.Trim().ToLowerInvariant(); + + public static bool IsSafeId(string id) + { + return !string.IsNullOrWhiteSpace(id) && + id.Length <= 64 && + id.All(ch => char.IsAsciiLetterOrDigit(ch) || ch is '-' or '_' or '.'); + } + + public static ToolCategory ParseCategory(string? category) + { + return category?.Trim().ToLowerInvariant() switch + { + "plugin" => ToolCategory.Plugin, + "network" => ToolCategory.Network, + "security" => ToolCategory.Security, + "data" => ToolCategory.Data, + "calculator" => ToolCategory.Calculator, + "text" => ToolCategory.Text, + "image" => ToolCategory.Image, + "design" => ToolCategory.Design, + "life" => ToolCategory.Life, + "system" => ToolCategory.System, + _ => ToolCategory.Dev + }; + } +} + +[JsonSerializable(typeof(PluginManifest))] +[JsonSerializable(typeof(PluginSurface))] +internal sealed partial class PluginJsonContext : JsonSerializerContext; diff --git a/src/YMhut.Box.Core/Plugins/PluginRegistryService.cs b/src/YMhut.Box.Core/Plugins/PluginRegistryService.cs new file mode 100644 index 0000000..332e59f --- /dev/null +++ b/src/YMhut.Box.Core/Plugins/PluginRegistryService.cs @@ -0,0 +1,223 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using YMhut.Box.Core.App; +using YMhut.Box.Core.Logging; +using YMhut.Box.Core.Settings; +using YMhut.Box.Core.Tools; + +namespace YMhut.Box.Core.Plugins; + +public interface IPluginRegistryService +{ + string PluginsRoot { get; } + + Task> LoadPluginsAsync(CancellationToken cancellationToken = default); + + Task> LoadEnabledToolModulesAsync(CancellationToken cancellationToken = default); +} + +public sealed class PluginRegistryService( + AppPaths paths, + IPluginStateStore stateStore, + ILogService? logService = null, + ISettingsService? settingsService = null, + IBuiltInPluginInstallerService? builtInInstaller = null) : IPluginRegistryService +{ + private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web) + { + PropertyNameCaseInsensitive = true, + Converters = { new JsonStringEnumConverter(), new JsonStringEnumConverter() } + }; + + public string PluginsRoot => ResolvePluginsRoot(paths, settingsService?.Current.PluginRootPath); + + public static string DefaultPluginsRoot(AppPaths paths) => Path.Combine(paths.Root, "Plugins"); + + public static string ResolvePluginsRoot(AppPaths paths, string? configuredPath) + { + return string.IsNullOrWhiteSpace(configuredPath) + ? DefaultPluginsRoot(paths) + : Path.GetFullPath(Environment.ExpandEnvironmentVariables(configuredPath.Trim())); + } + + public async Task> LoadPluginsAsync(CancellationToken cancellationToken = default) + { + if (settingsService is not null && !settingsService.Current.PluginsEnabled) + { + return []; + } + + if (builtInInstaller is not null) + { + await builtInInstaller.EnsureInstalledAsync(cancellationToken).ConfigureAwait(false); + } + + Directory.CreateDirectory(PluginsRoot); + var plugins = new List(); + var seen = new HashSet(StringComparer.OrdinalIgnoreCase); + var builtInIds = ToolCatalog.DefaultModules().Select(module => module.Id).ToHashSet(StringComparer.OrdinalIgnoreCase); + + foreach (var directory in Directory.EnumerateDirectories(PluginsRoot).Order(StringComparer.OrdinalIgnoreCase)) + { + var manifestPath = Path.Combine(directory, PluginManifest.FileName); + var errors = new List(); + PluginManifest? manifest = null; + try + { + if (!File.Exists(manifestPath)) + { + errors.Add("Missing ymhut.plugin.json."); + } + else + { + await using var stream = File.OpenRead(manifestPath); + manifest = await JsonSerializer.DeserializeAsync(stream, JsonOptions, cancellationToken).ConfigureAwait(false); + if (manifest is null) + { + errors.Add("Manifest is empty or invalid."); + } + } + } + catch (Exception exception) when (exception is JsonException or IOException or UnauthorizedAccessException) + { + errors.Add(exception.Message); + } + + manifest ??= new PluginManifest(Path.GetFileName(directory), Path.GetFileName(directory), "0.0.0", string.Empty, string.Empty, string.Empty, [], [], []); + ValidateManifest(manifest, directory, seen, builtInIds, errors); + seen.Add(manifest.Id); + + var state = await stateStore.GetStateAsync(manifest.Id, cancellationToken).ConfigureAwait(false); + var loaded = new LoadedPlugin(manifest, directory, state, errors); + plugins.Add(loaded); + if (errors.Count > 0) + { + await (logService?.WriteAsync("Warning", $"plugin:{manifest.Id}", "Plugin validation failed", string.Join(Environment.NewLine, errors), cancellationToken) ?? Task.CompletedTask) + .ConfigureAwait(false); + } + } + + return plugins; + } + + public async Task> LoadEnabledToolModulesAsync(CancellationToken cancellationToken = default) + { + if (settingsService is not null && !settingsService.Current.PluginsEnabled) + { + return []; + } + + var plugins = await LoadPluginsAsync(cancellationToken).ConfigureAwait(false); + return plugins + .Where(plugin => plugin.IsValid && plugin.State.Enabled) + .SelectMany(plugin => plugin.Manifest.Surfaces + .Where(surface => surface.Kind == PluginSurfaceKind.ToolboxTool && + (plugin.State.MountedSurfaceIds.Count == 0 || plugin.State.MountedSurfaceIds.Contains(surface.Id))) + .Select(surface => new PluginToolModule(plugin, surface))) + .ToArray(); + } + + private static void ValidateManifest( + PluginManifest manifest, + string root, + ISet seen, + ISet builtInIds, + IList errors) + { + if (!PluginIds.IsSafeId(manifest.Id) || PluginIds.IsPluginToolId(manifest.Id)) + { + errors.Add("Plugin id must be a safe local id and cannot start with plugin:."); + } + if (seen.Contains(manifest.Id)) + { + errors.Add("Duplicate plugin id."); + } + if (string.IsNullOrWhiteSpace(manifest.Name)) + { + errors.Add("Plugin name is required."); + } + if (string.IsNullOrWhiteSpace(manifest.Version)) + { + errors.Add("Plugin version is required."); + } + if (!IsSafeRelativeFile(root, manifest.Entry)) + { + errors.Add("Plugin entry must point to a file inside the plugin directory."); + } + if (!HasReadme(root)) + { + errors.Add("Plugin package must include README.md, README.txt, or 说明.md."); + } + + var surfaceIds = new HashSet(StringComparer.OrdinalIgnoreCase); + foreach (var surface in manifest.Surfaces) + { + if (!PluginIds.IsSafeId(surface.Id)) + { + errors.Add($"Surface id is invalid: {surface.Id}"); + } + if (!surfaceIds.Add(surface.Id)) + { + errors.Add($"Duplicate surface id: {surface.Id}"); + } + if (builtInIds.Contains(surface.Id) || builtInIds.Contains(PluginIds.ToolId(manifest.Id, surface.Id))) + { + errors.Add($"Surface id conflicts with a built-in tool: {surface.Id}"); + } + if (!IsSafeRelativeFile(root, surface.EffectiveEntry(manifest.Entry))) + { + errors.Add($"Surface entry must point to a file inside the plugin directory: {surface.Id}"); + } + } + + foreach (var resource in manifest.Resources) + { + if (!IsSafeRelativePath(root, resource)) + { + errors.Add($"Resource path escapes plugin directory: {resource}"); + } + if (resource.Contains("Assets/icons", StringComparison.OrdinalIgnoreCase) || + resource.Contains("developer", StringComparison.OrdinalIgnoreCase) || + resource.Contains("about", StringComparison.OrdinalIgnoreCase)) + { + errors.Add($"Core asset override is not allowed: {resource}"); + } + } + } + + public static bool IsSafeRelativeFile(string root, string relativePath) + { + if (string.IsNullOrWhiteSpace(relativePath)) + { + return false; + } + + var fullPath = Path.GetFullPath(Path.Combine(root, relativePath)); + return IsInside(root, fullPath) && File.Exists(fullPath); + } + + public static bool IsSafeRelativePath(string root, string relativePath) + { + if (string.IsNullOrWhiteSpace(relativePath)) + { + return false; + } + + var fullPath = Path.GetFullPath(Path.Combine(root, relativePath)); + return IsInside(root, fullPath); + } + + public static bool IsInside(string root, string path) + { + var normalizedRoot = Path.GetFullPath(root).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) + Path.DirectorySeparatorChar; + var normalizedPath = Path.GetFullPath(path); + return normalizedPath.StartsWith(normalizedRoot, StringComparison.OrdinalIgnoreCase); + } + + private static bool HasReadme(string root) + { + return File.Exists(Path.Combine(root, "README.md")) || + File.Exists(Path.Combine(root, "README.txt")) || + File.Exists(Path.Combine(root, "说明.md")); + } +} diff --git a/src/YMhut.Box.Core/Plugins/PluginStateStore.cs b/src/YMhut.Box.Core/Plugins/PluginStateStore.cs new file mode 100644 index 0000000..64e540a --- /dev/null +++ b/src/YMhut.Box.Core/Plugins/PluginStateStore.cs @@ -0,0 +1,290 @@ +using Microsoft.Data.Sqlite; +using YMhut.Box.Core.App; + +namespace YMhut.Box.Core.Plugins; + +public interface IPluginStateStore +{ + string DatabasePath { get; } + + Task GetStateAsync(string pluginId, CancellationToken cancellationToken = default); + + Task SetEnabledAsync(string pluginId, bool enabled, CancellationToken cancellationToken = default); + + Task SetPermissionAsync(string pluginId, PluginPermission permission, bool granted, CancellationToken cancellationToken = default); + + Task SetSurfaceMountedAsync(string pluginId, string surfaceId, bool mounted, CancellationToken cancellationToken = default); + + Task MarkRunAsync(string pluginId, CancellationToken cancellationToken = default); + + Task GetValueAsync(string pluginId, string key, CancellationToken cancellationToken = default); + + Task SetValueAsync(string pluginId, string key, string value, CancellationToken cancellationToken = default); + + Task RemoveValueAsync(string pluginId, string key, CancellationToken cancellationToken = default); + + Task> ListValuesAsync(string pluginId, CancellationToken cancellationToken = default); +} + +public sealed class PluginStateStore : IPluginStateStore +{ + private readonly SemaphoreSlim _gate = new(1, 1); + private bool _initialized; + + public PluginStateStore(AppPaths paths) + { + paths.EnsureCreated(); + DatabasePath = Path.Combine(paths.Data, "plugins.db"); + } + + public string DatabasePath { get; } + + public async Task GetStateAsync(string pluginId, CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + await using var connection = OpenConnection(); + + var enabled = false; + DateTimeOffset? lastRun = null; + await using (var command = connection.CreateCommand()) + { + command.CommandText = "SELECT enabled, last_run_at FROM plugin_states WHERE plugin_id = $plugin_id;"; + command.Parameters.AddWithValue("$plugin_id", pluginId); + await using var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false); + if (await reader.ReadAsync(cancellationToken).ConfigureAwait(false)) + { + enabled = reader.GetInt32(0) != 0; + lastRun = reader.IsDBNull(1) ? null : DateTimeOffset.Parse(reader.GetString(1)); + } + } + + var permissions = await ReadStringSetAsync(connection, "plugin_permissions", "permission", pluginId, cancellationToken).ConfigureAwait(false); + var surfaces = await ReadStringSetAsync(connection, "plugin_surfaces", "surface_id", pluginId, cancellationToken).ConfigureAwait(false); + return new PluginRuntimeState( + pluginId, + enabled, + permissions.Select(Enum.Parse).ToHashSet(), + surfaces.ToHashSet(StringComparer.OrdinalIgnoreCase), + lastRun); + } + finally + { + _gate.Release(); + } + } + + public Task SetEnabledAsync(string pluginId, bool enabled, CancellationToken cancellationToken = default) + => UpsertStateAsync(pluginId, enabled: enabled, markRun: false, cancellationToken); + + public async Task SetPermissionAsync(string pluginId, PluginPermission permission, bool granted, CancellationToken cancellationToken = default) + { + await SetStringFlagAsync("plugin_permissions", "permission", pluginId, permission.ToString(), granted, cancellationToken).ConfigureAwait(false); + } + + public async Task SetSurfaceMountedAsync(string pluginId, string surfaceId, bool mounted, CancellationToken cancellationToken = default) + { + await SetStringFlagAsync("plugin_surfaces", "surface_id", pluginId, surfaceId, mounted, cancellationToken).ConfigureAwait(false); + } + + public Task MarkRunAsync(string pluginId, CancellationToken cancellationToken = default) + => UpsertStateAsync(pluginId, enabled: null, markRun: true, cancellationToken); + + public async Task GetValueAsync(string pluginId, string key, CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = "SELECT value FROM plugin_kv WHERE plugin_id = $plugin_id AND key = $key;"; + command.Parameters.AddWithValue("$plugin_id", pluginId); + command.Parameters.AddWithValue("$key", key); + var value = await command.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false); + return value as string; + } + finally + { + _gate.Release(); + } + } + + public async Task SetValueAsync(string pluginId, string key, string value, CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = """ + INSERT INTO plugin_kv(plugin_id, key, value) + VALUES ($plugin_id, $key, $value) + ON CONFLICT(plugin_id, key) DO UPDATE SET value = excluded.value; + """; + command.Parameters.AddWithValue("$plugin_id", pluginId); + command.Parameters.AddWithValue("$key", key); + command.Parameters.AddWithValue("$value", value); + await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + } + finally + { + _gate.Release(); + } + } + + public async Task RemoveValueAsync(string pluginId, string key, CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = "DELETE FROM plugin_kv WHERE plugin_id = $plugin_id AND key = $key;"; + command.Parameters.AddWithValue("$plugin_id", pluginId); + command.Parameters.AddWithValue("$key", key); + await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + } + finally + { + _gate.Release(); + } + } + + public async Task> ListValuesAsync(string pluginId, CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = "SELECT key, value FROM plugin_kv WHERE plugin_id = $plugin_id ORDER BY key;"; + command.Parameters.AddWithValue("$plugin_id", pluginId); + var values = new Dictionary(StringComparer.OrdinalIgnoreCase); + await using var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false); + while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false)) + { + values[reader.GetString(0)] = reader.GetString(1); + } + + return values; + } + finally + { + _gate.Release(); + } + } + + private async Task UpsertStateAsync(string pluginId, bool? enabled, bool markRun, CancellationToken cancellationToken) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = """ + INSERT INTO plugin_states(plugin_id, enabled, last_run_at) + VALUES ($plugin_id, $enabled, $last_run_at) + ON CONFLICT(plugin_id) DO UPDATE SET + enabled = CASE WHEN $enabled_set = 1 THEN excluded.enabled ELSE plugin_states.enabled END, + last_run_at = CASE WHEN $mark_run = 1 THEN excluded.last_run_at ELSE plugin_states.last_run_at END; + """; + command.Parameters.AddWithValue("$plugin_id", pluginId); + command.Parameters.AddWithValue("$enabled", enabled == true ? 1 : 0); + command.Parameters.AddWithValue("$enabled_set", enabled is null ? 0 : 1); + command.Parameters.AddWithValue("$mark_run", markRun ? 1 : 0); + command.Parameters.AddWithValue("$last_run_at", DateTimeOffset.Now.ToString("O")); + await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + } + finally + { + _gate.Release(); + } + } + + private async Task SetStringFlagAsync(string table, string column, string pluginId, string value, bool enabled, CancellationToken cancellationToken) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = enabled + ? $"INSERT OR IGNORE INTO {table}(plugin_id, {column}) VALUES ($plugin_id, $value);" + : $"DELETE FROM {table} WHERE plugin_id = $plugin_id AND {column} = $value;"; + command.Parameters.AddWithValue("$plugin_id", pluginId); + command.Parameters.AddWithValue("$value", value); + await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + } + finally + { + _gate.Release(); + } + } + + private static async Task> ReadStringSetAsync(SqliteConnection connection, string table, string column, string pluginId, CancellationToken cancellationToken) + { + await using var command = connection.CreateCommand(); + command.CommandText = $"SELECT {column} FROM {table} WHERE plugin_id = $plugin_id;"; + command.Parameters.AddWithValue("$plugin_id", pluginId); + var values = new List(); + await using var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false); + while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false)) + { + values.Add(reader.GetString(0)); + } + + return values; + } + + private async Task EnsureInitializedAsync(CancellationToken cancellationToken) + { + if (_initialized) + { + return; + } + + Directory.CreateDirectory(Path.GetDirectoryName(DatabasePath)!); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = """ + CREATE TABLE IF NOT EXISTS plugin_states ( + plugin_id TEXT PRIMARY KEY, + enabled INTEGER NOT NULL DEFAULT 0, + last_run_at TEXT NULL + ); + CREATE TABLE IF NOT EXISTS plugin_permissions ( + plugin_id TEXT NOT NULL, + permission TEXT NOT NULL, + PRIMARY KEY(plugin_id, permission) + ); + CREATE TABLE IF NOT EXISTS plugin_surfaces ( + plugin_id TEXT NOT NULL, + surface_id TEXT NOT NULL, + PRIMARY KEY(plugin_id, surface_id) + ); + CREATE TABLE IF NOT EXISTS plugin_kv ( + plugin_id TEXT NOT NULL, + key TEXT NOT NULL, + value TEXT NOT NULL, + PRIMARY KEY(plugin_id, key) + ); + """; + await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + _initialized = true; + } + + private SqliteConnection OpenConnection() + { + var connection = new SqliteConnection($"Data Source={DatabasePath}"); + connection.Open(); + return connection; + } +} diff --git a/src/YMhut.Box.Core/SensitiveText.cs b/src/YMhut.Box.Core/SensitiveText.cs new file mode 100644 index 0000000..d622f58 --- /dev/null +++ b/src/YMhut.Box.Core/SensitiveText.cs @@ -0,0 +1,36 @@ +using System.Text.RegularExpressions; + +namespace YMhut.Box.Core; + +public static class SensitiveText +{ + public static string Sanitize(string? message, int maxLength = 160) + { + if (string.IsNullOrWhiteSpace(message)) + { + return "未知错误"; + } + + var sanitized = Regex.Replace( + message, + @"https?://[^\s\]\)""'<>]+", + "远程服务", + RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); + + sanitized = Regex.Replace( + sanitized, + @"update\.ymhut\.cn[^\s\]\)""'<>]*", + "远程服务", + RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); + + sanitized = sanitized + .Replace("update-info.json", "发布服务", StringComparison.OrdinalIgnoreCase) + .Replace("media-types.json", "媒体配置", StringComparison.OrdinalIgnoreCase) + .Replace("api_url", "媒体源", StringComparison.OrdinalIgnoreCase) + .Replace("download_url", "下载源", StringComparison.OrdinalIgnoreCase); + + return sanitized.Length <= maxLength + ? sanitized + : sanitized[..Math.Max(0, maxLength - 3)] + "..."; + } +} diff --git a/src/YMhut.Box.Core/Settings/AgreementDocument.cs b/src/YMhut.Box.Core/Settings/AgreementDocument.cs new file mode 100644 index 0000000..138c56d --- /dev/null +++ b/src/YMhut.Box.Core/Settings/AgreementDocument.cs @@ -0,0 +1,169 @@ +using System.Globalization; +using Microsoft.Data.Sqlite; +using YMhut.Box.Core.App; + +namespace YMhut.Box.Core.Settings; + +public static class AgreementDocument +{ + public const string CurrentVersion = "2.0.6.2"; + + public const int CurrentRevision = 1; + + public static bool SettingsMatchCurrent(AppSettings settings) + => settings.UserAgreementAccepted && + string.Equals(settings.UserAgreementVersion, CurrentVersion, StringComparison.OrdinalIgnoreCase); +} + +public sealed record AgreementAcceptance( + string Version, + int Revision, + string AppVersion, + string Language, + DateTimeOffset AcceptedAt); + +public interface IAgreementAcceptanceStore +{ + Task GetLatestAsync(CancellationToken cancellationToken = default); + + Task RecordAcceptedAsync( + string version, + int revision, + string appVersion, + string language, + DateTimeOffset acceptedAt, + CancellationToken cancellationToken = default); +} + +public sealed class AgreementAcceptanceStore : IAgreementAcceptanceStore +{ + private readonly SemaphoreSlim _gate = new(1, 1); + private bool _initialized; + + public AgreementAcceptanceStore(AppPaths paths) + { + paths.EnsureCreated(); + DatabasePath = AppDatabasePaths.ResolveMainDatabasePath(paths); + } + + public string DatabasePath { get; } + + public async Task GetLatestAsync(CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = """ + SELECT agreement_version, agreement_revision, app_version, language, accepted_at + FROM agreement_acceptances + ORDER BY accepted_at DESC, id DESC + LIMIT 1; + """; + + await using var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false); + if (!await reader.ReadAsync(cancellationToken).ConfigureAwait(false)) + { + return null; + } + + return new AgreementAcceptance( + reader.GetString(0), + reader.GetInt32(1), + reader.GetString(2), + reader.GetString(3), + DateTimeOffset.TryParse(reader.GetString(4), CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out var acceptedAt) + ? acceptedAt + : DateTimeOffset.MinValue); + } + finally + { + _gate.Release(); + } + } + + public async Task RecordAcceptedAsync( + string version, + int revision, + string appVersion, + string language, + DateTimeOffset acceptedAt, + CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = """ + INSERT INTO agreement_acceptances( + agreement_version, + agreement_revision, + app_version, + language, + accepted_at) + VALUES ( + $agreement_version, + $agreement_revision, + $app_version, + $language, + $accepted_at); + """; + command.Parameters.AddWithValue("$agreement_version", version); + command.Parameters.AddWithValue("$agreement_revision", revision); + command.Parameters.AddWithValue("$app_version", appVersion); + command.Parameters.AddWithValue("$language", language); + command.Parameters.AddWithValue("$accepted_at", acceptedAt.ToString("O", CultureInfo.InvariantCulture)); + await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + } + finally + { + _gate.Release(); + } + } + + private async Task EnsureInitializedAsync(CancellationToken cancellationToken) + { + if (_initialized) + { + return; + } + + Directory.CreateDirectory(Path.GetDirectoryName(DatabasePath)!); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = """ + PRAGMA journal_mode = WAL; + PRAGMA synchronous = NORMAL; + PRAGMA busy_timeout = 5000; + CREATE TABLE IF NOT EXISTS agreement_acceptances ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + agreement_version TEXT NOT NULL, + agreement_revision INTEGER NOT NULL, + app_version TEXT NOT NULL, + language TEXT NOT NULL, + accepted_at TEXT NOT NULL + ); + CREATE INDEX IF NOT EXISTS idx_agreement_acceptances_latest + ON agreement_acceptances(accepted_at DESC, id DESC); + """; + await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + _initialized = true; + } + + private SqliteConnection OpenConnection() + { + var connection = new SqliteConnection($"Data Source={DatabasePath};Pooling=True"); + connection.Open(); + using var command = connection.CreateCommand(); + command.CommandText = """ + PRAGMA busy_timeout = 5000; + PRAGMA synchronous = NORMAL; + """; + command.ExecuteNonQuery(); + return connection; + } +} diff --git a/src/YMhut.Box.Core/Settings/AppSettings.cs b/src/YMhut.Box.Core/Settings/AppSettings.cs new file mode 100644 index 0000000..b903c76 --- /dev/null +++ b/src/YMhut.Box.Core/Settings/AppSettings.cs @@ -0,0 +1,118 @@ +namespace YMhut.Box.Core.Settings; + +public sealed class AppSettings +{ + public string Theme { get; set; } = "Light"; + + public string Language { get; set; } = "zh-CN"; + + public bool PitchBlack { get; set; } + + public string WindowBackdrop { get; set; } = "mica"; + + public string SettingsPanelMaterial { get; set; } = "solid"; + + public string TopBarMaterial { get; set; } = "inherit"; + + public int SeedColor { get; set; } = unchecked((int)0xFF5E6D44); + + public double CardOpacity { get; set; } = 0.7; + + public string BackgroundImage { get; set; } = string.Empty; + + public double BackgroundOpacity { get; set; } = 1.0; + + public bool UserAgreementAccepted { get; set; } + + public string UserAgreementVersion { get; set; } = string.Empty; + + public DateTimeOffset? UserAgreementAcceptedAt { get; set; } + + public bool SidebarCollapsed { get; set; } + + public bool DesktopTitlebar { get; set; } = true; + + public List RecentToolIds { get; set; } = []; + + public List PinnedToolIds { get; set; } = []; + + public double? WindowX { get; set; } + + public double? WindowY { get; set; } + + public double? WindowWidth { get; set; } + + public double? WindowHeight { get; set; } + + public bool WindowMaximized { get; set; } + + public bool ProxyEnabled { get; set; } + + public string ProxyMode { get; set; } = "system"; + + public string ProxyHost { get; set; } = string.Empty; + + public int ProxyPort { get; set; } = 7890; + + public bool AnimationsEnabled { get; set; } = true; + + public double FontSize { get; set; } = 1.0; + + public string ToolDisplayMode { get; set; } = "grid"; + + public string ToolboxDefaultScope { get; set; } = "all"; + + public bool ToolboxCompactCards { get; set; } + + public bool ToolboxShowRecentFirst { get; set; } = true; + + public bool ShowHardwareBrandLogo { get; set; } = true; + + public bool RequireRiskConfirmation { get; set; } = true; + + public string HomePageMode { get; set; } = "dashboard"; + + public string CloseBehavior { get; set; } = "ask"; + + public bool RestoreWindowPosition { get; set; } = true; + + public bool AutoStart { get; set; } + + public int LogRetentionCount { get; set; } + + public int DataRefreshInterval { get; set; } = 30; + + public bool UpdateNotification { get; set; } = true; + + public bool HardwareAccelerationEnabled { get; set; } = true; + + public int ProxyTestTimeoutSeconds { get; set; } = 6; + + public bool PluginsEnabled { get; set; } + + public string PluginRootPath { get; set; } = string.Empty; + + public string FeedbackDefaultContact { get; set; } = string.Empty; + + public string FeedbackDefaultType { get; set; } = "issue"; + + public string FeedbackDefaultSeverity { get; set; } = "normal"; + + public bool FeedbackIncludeTodayLogsByDefault { get; set; } + + public bool FeedbackIncludeToolStatusByDefault { get; set; } + + public bool FeedbackIncludeSystemSummaryByDefault { get; set; } + + public bool FeedbackRememberDefaults { get; set; } = true; + + public string FeedbackDailySubmissionDate { get; set; } = string.Empty; + + public int FeedbackDailySubmissionCount { get; set; } + + public bool LegacyImportCompleted { get; set; } + + public bool LegacyDatabaseImportCompleted { get; set; } + + public string? LegacyDatabaseSnapshotPath { get; set; } +} diff --git a/src/YMhut.Box.Core/Settings/AppSettingsService.cs b/src/YMhut.Box.Core/Settings/AppSettingsService.cs new file mode 100644 index 0000000..47af02f --- /dev/null +++ b/src/YMhut.Box.Core/Settings/AppSettingsService.cs @@ -0,0 +1,106 @@ +using System.ComponentModel; +using System.Runtime.CompilerServices; + +namespace YMhut.Box.Core.Settings; + +public sealed class AppSettingsService(AppSettingsStore store) : ISettingsService +{ + private readonly SemaphoreSlim _gate = new(1, 1); + private AppSettings _current = new(); + + public event PropertyChangedEventHandler? PropertyChanged; + + public AppSettings Current => _current; + + public string SettingsPath => store.SettingsPath; + + public async Task LoadAsync(CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + _current = await store.LoadAsync(cancellationToken).ConfigureAwait(false); + OnPropertyChanged(nameof(Current)); + return _current; + } + finally + { + _gate.Release(); + } + } + + public async Task SaveAsync(CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await store.SaveAsync(_current, cancellationToken).ConfigureAwait(false); + OnPropertyChanged(nameof(Current)); + } + finally + { + _gate.Release(); + } + } + + public async Task UpdateAsync(Action update, CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + if (_current is null) + { + _current = await store.LoadAsync(cancellationToken).ConfigureAwait(false); + } + + update(_current); + await store.SaveAsync(_current, cancellationToken).ConfigureAwait(false); + OnPropertyChanged(nameof(Current)); + } + finally + { + _gate.Release(); + } + } + + public async Task RecordRecentToolAsync(string toolId, CancellationToken cancellationToken = default) + { + if (string.IsNullOrWhiteSpace(toolId)) + { + return; + } + + await UpdateAsync(settings => + { + settings.RecentToolIds.RemoveAll(id => string.Equals(id, toolId, StringComparison.OrdinalIgnoreCase)); + settings.RecentToolIds.Insert(0, toolId); + if (settings.RecentToolIds.Count > 12) + { + settings.RecentToolIds.RemoveRange(12, settings.RecentToolIds.Count - 12); + } + }, cancellationToken).ConfigureAwait(false); + } + + public Task SaveWindowBoundsAsync( + double x, + double y, + double width, + double height, + bool maximized, + CancellationToken cancellationToken = default) + { + return UpdateAsync(settings => + { + settings.WindowX = x; + settings.WindowY = y; + settings.WindowWidth = Math.Max(640, width); + settings.WindowHeight = Math.Max(420, height); + settings.WindowMaximized = maximized; + }, cancellationToken); + } + + private void OnPropertyChanged([CallerMemberName] string? propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } +} diff --git a/src/YMhut.Box.Core/Settings/AppSettingsStore.cs b/src/YMhut.Box.Core/Settings/AppSettingsStore.cs new file mode 100644 index 0000000..bb0c5b1 --- /dev/null +++ b/src/YMhut.Box.Core/Settings/AppSettingsStore.cs @@ -0,0 +1,566 @@ +using System.Text.Json; + +namespace YMhut.Box.Core.Settings; + +public sealed class AppSettingsStore +{ + private static readonly string LegacyPreferencePrefix = string.Concat("flut", "ter."); + private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web) + { + WriteIndented = true + }; + + private readonly string _settingsPath; + + public AppSettingsStore(string? root = null) + { + var appData = root ?? Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), + "YMhut Box", + "WinUI"); + Directory.CreateDirectory(appData); + _settingsPath = Path.Combine(appData, "settings.json"); + } + + public string SettingsPath => _settingsPath; + + public async Task LoadAsync(CancellationToken cancellationToken = default) + { + if (!File.Exists(_settingsPath)) + { + var legacy = await TryImportLegacySettingsAsync(cancellationToken); + if (legacy is not null) + { + await TryImportLegacyDatabaseSnapshotAsync(legacy, cancellationToken); + return legacy; + } + + var fresh = new AppSettings { LegacyImportCompleted = true }; + await TryImportLegacyDatabaseSnapshotAsync(fresh, cancellationToken); + await SaveAsync(fresh, cancellationToken); + return fresh; + } + + AppSettings settings; + await using (var stream = File.OpenRead(_settingsPath)) + { + settings = await JsonSerializer.DeserializeAsync(stream, JsonOptions, cancellationToken) + ?? new AppSettings(); + } + + if (!settings.LegacyImportCompleted) + { + settings.LegacyImportCompleted = true; + } + + settings.HomePageMode = NormalizeHomePageMode(settings.HomePageMode); + settings.Language = LanguagePreference.Normalize(settings.Language); + settings.ToolDisplayMode = NormalizeToolDisplayMode(settings.ToolDisplayMode); + settings.ToolboxDefaultScope = NormalizeToolboxScope(settings.ToolboxDefaultScope); + settings.WindowBackdrop = NormalizeWindowBackdrop(settings.WindowBackdrop); + settings.SettingsPanelMaterial = NormalizeSettingsPanelMaterial(settings.SettingsPanelMaterial); + settings.TopBarMaterial = NormalizeTopBarMaterial(settings.TopBarMaterial); + settings.FeedbackDefaultType = NormalizeFeedbackType(settings.FeedbackDefaultType); + settings.FeedbackDefaultSeverity = NormalizeFeedbackSeverity(settings.FeedbackDefaultSeverity); + + if (!settings.LegacyDatabaseImportCompleted) + { + await TryImportLegacyDatabaseSnapshotAsync(settings, cancellationToken); + } + + await SaveAsync(settings, cancellationToken); + + return settings; + } + + public async Task SaveAsync(AppSettings settings, CancellationToken cancellationToken = default) + { + settings.Language = LanguagePreference.Normalize(settings.Language); + Directory.CreateDirectory(Path.GetDirectoryName(_settingsPath)!); + await using var stream = File.Create(_settingsPath); + await JsonSerializer.SerializeAsync(stream, settings, JsonOptions, cancellationToken); + } + + public async Task RecordRecentToolAsync(string toolId, CancellationToken cancellationToken = default) + { + var settings = await LoadAsync(cancellationToken); + settings.RecentToolIds.RemoveAll(id => string.Equals(id, toolId, StringComparison.OrdinalIgnoreCase)); + settings.RecentToolIds.Insert(0, toolId); + if (settings.RecentToolIds.Count > 12) + { + settings.RecentToolIds.RemoveRange(12, settings.RecentToolIds.Count - 12); + } + + await SaveAsync(settings, cancellationToken); + } + + private async Task TryImportLegacySettingsAsync(CancellationToken cancellationToken) + { + var settingsRoot = Path.GetDirectoryName(_settingsPath); + if (string.IsNullOrWhiteSpace(settingsRoot)) + { + return null; + } + + foreach (var legacyPath in ResolveLegacySettingsCandidates(settingsRoot)) + { + if (!File.Exists(legacyPath)) + { + continue; + } + + try + { + await using var stream = File.OpenRead(legacyPath); + using var document = await JsonDocument.ParseAsync(stream, cancellationToken: cancellationToken); + var legacy = ParseLegacySettings(document.RootElement); + if (legacy is null) + { + continue; + } + + legacy.LegacyImportCompleted = true; + await SaveAsync(legacy, cancellationToken); + return legacy; + } + catch (JsonException) + { + continue; + } + } + + return null; + } + + private static IEnumerable ResolveLegacySettingsCandidates(string settingsRoot) + { + var legacyBase = Path.GetFullPath(Path.Combine(settingsRoot, "..")); + foreach (var basePath in new[] + { + legacyBase, + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + }.Where(path => !string.IsNullOrWhiteSpace(path)).Distinct(StringComparer.OrdinalIgnoreCase)) + { + foreach (var appFolder in new[] { "YMhut Box", "ymhut_box" }) + { + yield return Path.Combine(basePath, appFolder, "settings.json"); + yield return Path.Combine(basePath, appFolder, "shared_preferences.json"); + } + } + } + + private static AppSettings? ParseLegacySettings(JsonElement root) + { + if (root.ValueKind != JsonValueKind.Object) + { + return null; + } + + var found = false; + var settings = new AppSettings(); + + if (TryGetString(root, out var theme, "theme")) + { + settings.Theme = NormalizeTheme(theme); + found = true; + } + + AssignString(root, value => settings.Language = LanguagePreference.Normalize(value), ref found, "language", "locale", "ui_language", "uiLanguage", "app_language", "appLanguage"); + AssignBool(root, value => settings.PitchBlack = value, ref found, "pitch_black", "pitchBlack"); + AssignInt(root, value => settings.SeedColor = value, ref found, "seed_color", "seedColor"); + AssignDouble(root, value => settings.CardOpacity = value, ref found, "card_opacity", "cardOpacity"); + AssignString(root, value => settings.WindowBackdrop = NormalizeWindowBackdrop(value), ref found, "window_backdrop", "windowBackdrop"); + AssignString(root, value => settings.SettingsPanelMaterial = NormalizeSettingsPanelMaterial(value), ref found, "settings_panel_material", "settingsPanelMaterial"); + AssignString(root, value => settings.TopBarMaterial = NormalizeTopBarMaterial(value), ref found, "top_bar_material", "topBarMaterial", "title_bar_material", "titleBarMaterial"); + AssignString(root, value => settings.BackgroundImage = value, ref found, "background_image", "backgroundImage"); + AssignDouble(root, value => settings.BackgroundOpacity = value, ref found, "background_opacity", "backgroundOpacity"); + AssignBool(root, value => settings.UserAgreementAccepted = value, ref found, "user_agreement_accepted", "userAgreementAccepted"); + AssignString(root, value => settings.UserAgreementVersion = value, ref found, "user_agreement_version", "userAgreementVersion"); + AssignDateTimeOffset(root, value => settings.UserAgreementAcceptedAt = value, ref found, "user_agreement_accepted_at", "userAgreementAcceptedAt"); + AssignBool(root, value => settings.SidebarCollapsed = value, ref found, "sidebar_collapsed", "sidebarCollapsed"); + AssignBool(root, value => settings.DesktopTitlebar = value, ref found, "desktop_titlebar", "desktopTitlebar"); + AssignStringList(root, value => settings.RecentToolIds = value, ref found, "recent_tool_ids", "recentToolIds"); + AssignStringList(root, value => settings.PinnedToolIds = value, ref found, "pinned_tool_ids", "pinnedToolIds"); + AssignNullableDouble(root, value => settings.WindowX = value, ref found, "window_x", "windowX"); + AssignNullableDouble(root, value => settings.WindowY = value, ref found, "window_y", "windowY"); + AssignNullableDouble(root, value => settings.WindowWidth = value, ref found, "window_width", "windowWidth"); + AssignNullableDouble(root, value => settings.WindowHeight = value, ref found, "window_height", "windowHeight"); + AssignBool(root, value => settings.WindowMaximized = value, ref found, "window_maximized", "windowMaximized"); + AssignBool(root, value => settings.ProxyEnabled = value, ref found, "proxy_enabled", "proxyEnabled"); + AssignString(root, value => settings.ProxyMode = value, ref found, "proxy_mode", "proxyMode"); + AssignString(root, value => settings.ProxyHost = value, ref found, "proxy_host", "proxyHost"); + AssignInt(root, value => settings.ProxyPort = value, ref found, "proxy_port", "proxyPort"); + AssignBool(root, value => settings.AnimationsEnabled = value, ref found, "animations_enabled", "animationsEnabled"); + AssignDouble(root, value => settings.FontSize = value, ref found, "font_size", "fontSize"); + AssignString(root, value => settings.ToolDisplayMode = NormalizeToolDisplayMode(value), ref found, "tool_display_mode", "toolDisplayMode"); + AssignString(root, value => settings.ToolboxDefaultScope = NormalizeToolboxScope(value), ref found, "toolbox_default_scope", "toolboxDefaultScope"); + AssignBool(root, value => settings.ToolboxCompactCards = value, ref found, "toolbox_compact_cards", "toolboxCompactCards"); + AssignBool(root, value => settings.ToolboxShowRecentFirst = value, ref found, "toolbox_show_recent_first", "toolboxShowRecentFirst"); + AssignBool(root, value => settings.ShowHardwareBrandLogo = value, ref found, "show_hardware_brand_logo", "showHardwareBrandLogo", "brand_logo", "brandLogo"); + AssignBool(root, value => settings.RequireRiskConfirmation = value, ref found, "require_risk_confirmation", "requireRiskConfirmation"); + AssignString(root, value => settings.HomePageMode = NormalizeHomePageMode(value), ref found, "home_page_mode", "homePageMode"); + AssignString(root, value => settings.CloseBehavior = value, ref found, "close_behavior", "closeBehavior"); + AssignBool(root, value => settings.RestoreWindowPosition = value, ref found, "restore_window_position", "restoreWindowPosition"); + AssignBool(root, value => settings.AutoStart = value, ref found, "auto_start", "autoStart"); + AssignInt(root, value => settings.LogRetentionCount = value, ref found, "log_retention_count", "logRetentionCount"); + AssignInt(root, value => settings.DataRefreshInterval = value, ref found, "data_refresh_interval", "dataRefreshInterval"); + AssignBool(root, value => settings.UpdateNotification = value, ref found, "update_notification", "updateNotification"); + AssignBool(root, value => settings.HardwareAccelerationEnabled = value, ref found, "hardware_acceleration_enabled", "hardwareAccelerationEnabled"); + AssignInt(root, value => settings.ProxyTestTimeoutSeconds = value, ref found, "proxy_test_timeout_seconds", "proxyTestTimeoutSeconds"); + AssignBool(root, value => settings.PluginsEnabled = value, ref found, "plugins_enabled", "pluginsEnabled"); + AssignString(root, value => settings.PluginRootPath = value, ref found, "plugin_root_path", "pluginRootPath"); + AssignString(root, value => settings.FeedbackDefaultContact = value, ref found, "feedback_default_contact", "feedbackDefaultContact"); + AssignString(root, value => settings.FeedbackDefaultType = NormalizeFeedbackType(value), ref found, "feedback_default_type", "feedbackDefaultType"); + AssignString(root, value => settings.FeedbackDefaultSeverity = NormalizeFeedbackSeverity(value), ref found, "feedback_default_severity", "feedbackDefaultSeverity"); + AssignBool(root, value => settings.FeedbackIncludeTodayLogsByDefault = value, ref found, "feedback_include_today_logs_by_default", "feedbackIncludeTodayLogsByDefault"); + AssignBool(root, value => settings.FeedbackIncludeToolStatusByDefault = value, ref found, "feedback_include_tool_status_by_default", "feedbackIncludeToolStatusByDefault"); + AssignBool(root, value => settings.FeedbackIncludeSystemSummaryByDefault = value, ref found, "feedback_include_system_summary_by_default", "feedbackIncludeSystemSummaryByDefault"); + AssignBool(root, value => settings.FeedbackRememberDefaults = value, ref found, "feedback_remember_defaults", "feedbackRememberDefaults"); + + if (!TryGetString(root, out _, "close_behavior", "closeBehavior") && + TryGetBool(root, out var minimizeToTray, "minimize_to_tray", "minimizeToTray")) + { + settings.CloseBehavior = minimizeToTray ? "minimize_then_exit" : "exit_directly"; + found = true; + } + + settings.LegacyImportCompleted = true; + return found ? settings : null; + } + + private static string NormalizeTheme(string theme) + { + return theme.Trim().ToLowerInvariant() switch + { + "dark" => "Dark", + "system" => "System", + _ => "Light" + }; + } + + private static string NormalizeHomePageMode(string mode) + { + return (mode ?? string.Empty).Trim().ToLowerInvariant() switch + { + "solar" or "planets" or "planet" => "solar", + "dashboard" or "overview" or "tools" => "dashboard", + _ => "dashboard" + }; + } + + private static string NormalizeToolDisplayMode(string mode) + { + return (mode ?? string.Empty).Trim().ToLowerInvariant() switch + { + "list" or "compact" => "list", + _ => "grid" + }; + } + + private static string NormalizeToolboxScope(string scope) + { + return (scope ?? string.Empty).Trim().ToLowerInvariant() switch + { + "favorite" or "favorites" or "pinned" => "favorites", + "recent" or "history" => "recent", + "external" or "tools" => "external", + "builtin" or "built-in" or "reference" => "builtin", + "plugin" or "plugins" => "plugin", + "risk" or "risky" => "risk", + _ => "all" + }; + } + + private static string NormalizeWindowBackdrop(string value) + { + return (value ?? string.Empty).Trim().ToLowerInvariant() switch + { + "none" or "solid" => "solid", + "micaalt" or "mica_alt" or "mica-alt" => "micaAlt", + "acrylic" or "desktopacrylic" or "desktop_acrylic" => "acrylic", + _ => "mica" + }; + } + + private static string NormalizeSettingsPanelMaterial(string value) + { + return (value ?? string.Empty).Trim().ToLowerInvariant() switch + { + "acrylic" => "acrylic", + "glass" or "frosted" or "frosted_glass" => "glass", + _ => "solid" + }; + } + + private static string NormalizeTopBarMaterial(string value) + { + return (value ?? string.Empty).Trim().ToLowerInvariant() switch + { + "solid" or "none" => "solid", + "mica" => "mica", + "acrylic" => "acrylic", + "glass" or "frosted" or "frosted_glass" => "glass", + _ => "inherit" + }; + } + + private static string NormalizeFeedbackType(string value) + { + return (value ?? string.Empty).Trim().ToLowerInvariant() switch + { + "suggestion" => "suggestion", + "ui" => "ui", + "other" => "other", + _ => "issue" + }; + } + + private static string NormalizeFeedbackSeverity(string value) + { + return (value ?? string.Empty).Trim().ToLowerInvariant() switch + { + "major" => "major", + "blocking" => "blocking", + _ => "normal" + }; + } + + private static void AssignString(JsonElement root, Action assign, ref bool found, params string[] aliases) + { + if (TryGetString(root, out var value, aliases)) + { + assign(value); + found = true; + } + } + + private static void AssignBool(JsonElement root, Action assign, ref bool found, params string[] aliases) + { + if (TryGetBool(root, out var value, aliases)) + { + assign(value); + found = true; + } + } + + private static void AssignInt(JsonElement root, Action assign, ref bool found, params string[] aliases) + { + if (TryGetInt(root, out var value, aliases)) + { + assign(value); + found = true; + } + } + + private static void AssignDouble(JsonElement root, Action assign, ref bool found, params string[] aliases) + { + if (TryGetDouble(root, out var value, aliases)) + { + assign(value); + found = true; + } + } + + private static void AssignNullableDouble(JsonElement root, Action assign, ref bool found, params string[] aliases) + { + if (TryGetDouble(root, out var value, aliases)) + { + assign(value); + found = true; + } + } + + private static void AssignStringList(JsonElement root, Action> assign, ref bool found, params string[] aliases) + { + if (TryGetStringList(root, out var value, aliases)) + { + assign(value); + found = true; + } + } + + private static void AssignDateTimeOffset(JsonElement root, Action assign, ref bool found, params string[] aliases) + { + if (TryGetString(root, out var value, aliases) && + DateTimeOffset.TryParse(value, out var parsed)) + { + assign(parsed); + found = true; + } + } + + private static bool TryGetString(JsonElement root, out string value, params string[] aliases) + { + if (TryGet(root, out var element, aliases)) + { + if (element.ValueKind == JsonValueKind.String) + { + value = element.GetString() ?? string.Empty; + return true; + } + + if (element.ValueKind is JsonValueKind.Number or JsonValueKind.True or JsonValueKind.False) + { + value = element.ToString(); + return true; + } + } + + value = string.Empty; + return false; + } + + private static bool TryGetBool(JsonElement root, out bool value, params string[] aliases) + { + if (TryGet(root, out var element, aliases)) + { + if (element.ValueKind is JsonValueKind.True or JsonValueKind.False) + { + value = element.GetBoolean(); + return true; + } + + if (element.ValueKind == JsonValueKind.String && bool.TryParse(element.GetString(), out var parsed)) + { + value = parsed; + return true; + } + } + + value = false; + return false; + } + + private static bool TryGetInt(JsonElement root, out int value, params string[] aliases) + { + if (TryGet(root, out var element, aliases)) + { + if (element.ValueKind == JsonValueKind.Number && element.TryGetInt32(out value)) + { + return true; + } + + if (element.ValueKind == JsonValueKind.String && int.TryParse(element.GetString(), out value)) + { + return true; + } + } + + value = 0; + return false; + } + + private static bool TryGetDouble(JsonElement root, out double value, params string[] aliases) + { + if (TryGet(root, out var element, aliases)) + { + if (element.ValueKind == JsonValueKind.Number && element.TryGetDouble(out value)) + { + return true; + } + + if (element.ValueKind == JsonValueKind.String && double.TryParse(element.GetString(), out value)) + { + return true; + } + } + + value = 0; + return false; + } + + private static bool TryGetStringList(JsonElement root, out List value, params string[] aliases) + { + if (TryGet(root, out var element, aliases)) + { + if (element.ValueKind == JsonValueKind.Array) + { + value = element.EnumerateArray() + .Where(item => item.ValueKind == JsonValueKind.String) + .Select(item => item.GetString() ?? string.Empty) + .Where(item => !string.IsNullOrWhiteSpace(item)) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToList(); + return true; + } + + if (element.ValueKind == JsonValueKind.String) + { + value = element.GetString()? + .Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToList() ?? []; + return true; + } + } + + value = []; + return false; + } + + private static bool TryGet(JsonElement root, out JsonElement value, params string[] aliases) + { + foreach (var alias in aliases) + { + foreach (var key in new[] { alias, LegacyPreferencePrefix + alias }) + { + if (root.TryGetProperty(key, out value)) + { + return true; + } + } + } + + value = default; + return false; + } + + private async Task TryImportLegacyDatabaseSnapshotAsync(AppSettings settings, CancellationToken cancellationToken) + { + if (settings.LegacyDatabaseImportCompleted) + { + return; + } + + var settingsRoot = Path.GetDirectoryName(_settingsPath); + if (string.IsNullOrWhiteSpace(settingsRoot)) + { + return; + } + + var snapshotPath = Path.Combine(settingsRoot, "legacy_app_state.db"); + foreach (var legacyPath in ResolveLegacyDatabaseCandidates(settingsRoot)) + { + if (!File.Exists(legacyPath)) + { + continue; + } + + Directory.CreateDirectory(settingsRoot); + await using var source = File.Open(legacyPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + await using var target = File.Create(snapshotPath); + await source.CopyToAsync(target, cancellationToken); + settings.LegacyDatabaseSnapshotPath = snapshotPath; + settings.LegacyDatabaseImportCompleted = true; + return; + } + + settings.LegacyDatabaseImportCompleted = true; + } + + private static IEnumerable ResolveLegacyDatabaseCandidates(string settingsRoot) + { + var legacyBase = Path.GetFullPath(Path.Combine(settingsRoot, "..")); + yield return Path.Combine(legacyBase, "YMhut Box", "app_state.db"); + yield return Path.Combine(legacyBase, "ymhut_box", "app_state.db"); + yield return Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), + "ymhut_box", + "app_state.db"); + yield return Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), + "YMhut Box", + "app_state.db"); + } +} diff --git a/src/YMhut.Box.Core/Settings/ISettingsService.cs b/src/YMhut.Box.Core/Settings/ISettingsService.cs new file mode 100644 index 0000000..fedf0ee --- /dev/null +++ b/src/YMhut.Box.Core/Settings/ISettingsService.cs @@ -0,0 +1,26 @@ +using System.ComponentModel; + +namespace YMhut.Box.Core.Settings; + +public interface ISettingsService : INotifyPropertyChanged +{ + AppSettings Current { get; } + + string SettingsPath { get; } + + Task LoadAsync(CancellationToken cancellationToken = default); + + Task SaveAsync(CancellationToken cancellationToken = default); + + Task UpdateAsync(Action update, CancellationToken cancellationToken = default); + + Task RecordRecentToolAsync(string toolId, CancellationToken cancellationToken = default); + + Task SaveWindowBoundsAsync( + double x, + double y, + double width, + double height, + bool maximized, + CancellationToken cancellationToken = default); +} diff --git a/src/YMhut.Box.Core/Settings/LanguagePreference.cs b/src/YMhut.Box.Core/Settings/LanguagePreference.cs new file mode 100644 index 0000000..aced0b6 --- /dev/null +++ b/src/YMhut.Box.Core/Settings/LanguagePreference.cs @@ -0,0 +1,40 @@ +namespace YMhut.Box.Core.Settings; + +public static class LanguagePreference +{ + public const string Chinese = "zh-CN"; + public const string English = "en-US"; + + public static string Normalize(string? language) + { + var value = (language ?? string.Empty).Trim(); + if (value.Length == 0) + { + return Chinese; + } + + var normalized = value + .Replace('_', '-') + .Trim() + .ToLowerInvariant(); + + if (normalized is "en" or "english" || + normalized.StartsWith("en-", StringComparison.Ordinal)) + { + return English; + } + + if (normalized is "zh" or "cn" or "chinese" or "simplified-chinese" or "zh-hans" || + normalized.StartsWith("zh-", StringComparison.Ordinal) || + normalized.Contains("中文", StringComparison.Ordinal) || + normalized.Contains("简体", StringComparison.Ordinal)) + { + return Chinese; + } + + return Chinese; + } + + public static bool IsEnglish(string? language) + => string.Equals(Normalize(language), English, StringComparison.OrdinalIgnoreCase); +} diff --git a/src/YMhut.Box.Core/Settings/PinnedToolSettings.cs b/src/YMhut.Box.Core/Settings/PinnedToolSettings.cs new file mode 100644 index 0000000..2523c72 --- /dev/null +++ b/src/YMhut.Box.Core/Settings/PinnedToolSettings.cs @@ -0,0 +1,42 @@ +namespace YMhut.Box.Core.Settings; + +internal static class PinnedToolSettings +{ + public const int MaxPinnedTools = 64; + + public static void Toggle(IList pinnedToolIds, string toolId, bool wasPinned) + { + if (string.IsNullOrWhiteSpace(toolId)) + { + return; + } + + for (var index = pinnedToolIds.Count - 1; index >= 0; index--) + { + if (string.Equals(pinnedToolIds[index], toolId, StringComparison.OrdinalIgnoreCase)) + { + pinnedToolIds.RemoveAt(index); + } + } + + if (!wasPinned) + { + pinnedToolIds.Insert(0, toolId); + } + + Trim(pinnedToolIds); + } + + public static void Trim(IList pinnedToolIds) + { + if (pinnedToolIds.Count <= MaxPinnedTools) + { + return; + } + + for (var index = pinnedToolIds.Count - 1; index >= MaxPinnedTools; index--) + { + pinnedToolIds.RemoveAt(index); + } + } +} diff --git a/src/YMhut.Box.Core/SolarSystem/SolarEphemerisModels.cs b/src/YMhut.Box.Core/SolarSystem/SolarEphemerisModels.cs new file mode 100644 index 0000000..0f27df6 --- /dev/null +++ b/src/YMhut.Box.Core/SolarSystem/SolarEphemerisModels.cs @@ -0,0 +1,23 @@ +namespace YMhut.Box.Core.SolarSystem; + +public sealed record SolarEphemerisPayload( + string Type, + string Source, + DateTimeOffset EpochUtc, + IReadOnlyList Planets); + +public sealed record SolarEphemerisPlanet( + string Id, + HeliocentricAu HeliocentricAu); + +public sealed record HeliocentricAu( + double X, + double Y, + double Z); + +public interface ISolarEphemerisService +{ + Task GetCurrentAsync(CancellationToken cancellationToken = default); + + Task GetPreciseCurrentAsync(CancellationToken cancellationToken = default); +} diff --git a/src/YMhut.Box.Core/SolarSystem/SolarEphemerisService.cs b/src/YMhut.Box.Core/SolarSystem/SolarEphemerisService.cs new file mode 100644 index 0000000..2f3c606 --- /dev/null +++ b/src/YMhut.Box.Core/SolarSystem/SolarEphemerisService.cs @@ -0,0 +1,569 @@ +using System.Globalization; +using System.Text.Json; +using YMhut.Box.Core.App; +using YMhut.Box.Core.Logging; +using YMhut.Box.Core.Net; + +namespace YMhut.Box.Core.SolarSystem; + +public sealed partial class SolarEphemerisService( + AppPaths paths, + IHttpService httpService, + ILogService? logService = null) : ISolarEphemerisService +{ + private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web) + { + WriteIndented = true + }; + + private static readonly string[] PlanetIds = + [ + "mercury", + "venus", + "earth", + "mars", + "jupiter", + "saturn", + "uranus", + "neptune" + ]; + + private static readonly Dictionary HorizonsIds = new(StringComparer.OrdinalIgnoreCase) + { + ["mercury"] = "199", + ["venus"] = "299", + ["earth"] = "399", + ["mars"] = "499", + ["jupiter"] = "599", + ["saturn"] = "699", + ["uranus"] = "799", + ["neptune"] = "899" + }; + + private static readonly Dictionary MiriadeNames = new(StringComparer.OrdinalIgnoreCase) + { + ["mercury"] = "Mercury", + ["venus"] = "Venus", + ["earth"] = "Earth", + ["mars"] = "Mars", + ["jupiter"] = "Jupiter", + ["saturn"] = "Saturn", + ["uranus"] = "Uranus", + ["neptune"] = "Neptune" + }; + + public async Task GetCurrentAsync(CancellationToken cancellationToken = default) + { + var epoch = DateTimeOffset.UtcNow; + if (TryReadCache(epoch, out var cached, preciseOnly: false)) + { + return cached; + } + + _ = cancellationToken; + return CreateChinaApproximatePayload(epoch); + } + + public static bool TryParseMiriadePayload(string json, out HeliocentricAu vector) + { + return TryParseMiriadeVector(json, out vector); + } + + public async Task GetPreciseCurrentAsync(CancellationToken cancellationToken = default) + { + var epoch = DateTimeOffset.UtcNow; + if (TryReadCache(epoch, out var cached, preciseOnly: true)) + { + return cached; + } + + SolarEphemerisPayload? payload = null; + try + { + payload = await FetchChinaRemoteAsync(epoch, cancellationToken).ConfigureAwait(false); + } + catch (Exception exception) when (exception is not OperationCanceledException) + { + await WriteLogAsync("Warning", "solar", "China ephemeris sync failed", exception.Message, cancellationToken).ConfigureAwait(false); + } + + if (payload is null) + { + try + { + payload = await FetchMiriadeAsync(epoch, cancellationToken).ConfigureAwait(false); + } + catch (Exception exception) when (exception is not OperationCanceledException) + { + await WriteLogAsync("Warning", "solar", "IMCCE Miriade sync failed", exception.Message, cancellationToken).ConfigureAwait(false); + } + } + + if (payload is null) + { + try + { + payload = await FetchHorizonsAsync(epoch, cancellationToken).ConfigureAwait(false); + } + catch (Exception exception) when (exception is not OperationCanceledException) + { + await WriteLogAsync("Warning", "solar", "JPL Horizons sync failed", exception.Message, cancellationToken).ConfigureAwait(false); + } + } + + if (payload is not null) + { + WriteCache(payload); + } + + return payload; + } + + private static Task FetchChinaRemoteAsync(DateTimeOffset epoch, CancellationToken cancellationToken) + { + _ = epoch; + _ = cancellationToken; + return Task.FromResult(null); + } + + private async Task FetchHorizonsAsync(DateTimeOffset epoch, CancellationToken cancellationToken) + { + using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + timeout.CancelAfter(TimeSpan.FromSeconds(10)); + + var tasks = PlanetIds.Select(id => FetchHorizonsPlanetAsync(id, epoch, timeout.Token)).ToArray(); + var planets = await Task.WhenAll(tasks).ConfigureAwait(false); + + return planets.All(planet => planet is not null) + ? new SolarEphemerisPayload("ephemeris:sync", "JPL Horizons", epoch, planets!) + : null; + } + + private async Task FetchMiriadeAsync(DateTimeOffset epoch, CancellationToken cancellationToken) + { + using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + timeout.CancelAfter(TimeSpan.FromSeconds(8)); + + var tasks = PlanetIds.Select(id => FetchMiriadePlanetAsync(id, epoch, timeout.Token)).ToArray(); + var planets = await Task.WhenAll(tasks).ConfigureAwait(false); + + return planets.All(planet => planet is not null) + ? new SolarEphemerisPayload("ephemeris:sync", "IMCCE Miriade", epoch, planets!) + : null; + } + + private async Task FetchHorizonsPlanetAsync(string id, DateTimeOffset epoch, CancellationToken cancellationToken) + { + var command = Uri.EscapeDataString($"'{HorizonsIds[id]}'"); + var start = Uri.EscapeDataString(epoch.UtcDateTime.ToString("yyyy-MMM-dd HH:mm", CultureInfo.InvariantCulture)); + var stop = Uri.EscapeDataString(epoch.UtcDateTime.AddHours(1).ToString("yyyy-MMM-dd HH:mm", CultureInfo.InvariantCulture)); + var uri = new Uri($"https://ssd.jpl.nasa.gov/api/horizons.api?format=json&COMMAND={command}&OBJ_DATA=NO&MAKE_EPHEM=YES&EPHEM_TYPE=VECTORS&CENTER=@sun&START_TIME={start}&STOP_TIME={stop}&STEP_SIZE=1h&OUT_UNITS=AU-D"); + var response = await httpService.GetStringAsync(uri, cancellationToken).ConfigureAwait(false); + return TryParseHorizonsVector(response, out var vector) + ? new SolarEphemerisPlanet(id, vector) + : null; + } + + private async Task FetchMiriadePlanetAsync(string id, DateTimeOffset epoch, CancellationToken cancellationToken) + { + var julianDate = ToJulianDate(epoch.UtcDateTime).ToString("F6", CultureInfo.InvariantCulture); + var name = Uri.EscapeDataString($"p:{MiriadeNames[id]}"); + var uri = new Uri($"https://ssp.imcce.fr/webservices/miriade/api/ephemcc.php?-name={name}&-type=Planet&-ep={julianDate}&-nbd=1&-step=1d&-observer=@sun&-tcoor=2&-rplane=2&-mime=json"); + var response = await httpService.GetStringAsync(uri, cancellationToken).ConfigureAwait(false); + return TryParseMiriadeVector(response, out var vector) + ? new SolarEphemerisPlanet(id, vector) + : null; + } + + private static bool TryParseHorizonsVector(string json, out HeliocentricAu vector) + { + vector = new HeliocentricAu(0, 0, 0); + using var document = JsonDocument.Parse(json); + if (!document.RootElement.TryGetProperty("result", out var result) || + result.ValueKind != JsonValueKind.String) + { + return false; + } + + var text = result.GetString() ?? string.Empty; + var start = text.IndexOf("$$SOE", StringComparison.Ordinal); + var end = text.IndexOf("$$EOE", StringComparison.Ordinal); + if (start < 0 || end <= start) + { + return false; + } + + var block = text[(start + 5)..end]; + var x = ExtractHorizonsNumber(block, "X"); + var y = ExtractHorizonsNumber(block, "Y"); + var z = ExtractHorizonsNumber(block, "Z"); + if (x is null || y is null || z is null) + { + return false; + } + + vector = new HeliocentricAu(x.Value, y.Value, z.Value); + return true; + } + + private static double? ExtractHorizonsNumber(string text, string key) + { + var index = text.IndexOf(key + " =", StringComparison.Ordinal); + if (index < 0) + { + index = text.IndexOf(key + "=", StringComparison.Ordinal); + } + + if (index < 0) + { + return null; + } + + var equals = text.IndexOf('=', index); + if (equals < 0) + { + return null; + } + + var span = text[(equals + 1)..]; + var token = span.Split([' ', '\r', '\n', '\t'], StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(); + return double.TryParse(token, NumberStyles.Float, CultureInfo.InvariantCulture, out var value) ? value : null; + } + + private static bool TryParseMiriadeVector(string json, out HeliocentricAu vector) + { + vector = new HeliocentricAu(0, 0, 0); + using var document = JsonDocument.Parse(json); + var root = document.RootElement; + if (root.ValueKind == JsonValueKind.Array && + root.GetArrayLength() > 0 && + TryGetVectorFromObject(root[0], out vector)) + { + return true; + } + + if (root.ValueKind == JsonValueKind.Object) + { + if (TryGetVectorFromObject(root, out vector)) + { + return true; + } + + if (root.TryGetProperty("data", out var data) && + data.ValueKind == JsonValueKind.Array && + data.GetArrayLength() > 0 && + (TryGetVectorFromObject(data[0], out vector) || TryGetSphericalVector(data[0], out vector))) + { + return true; + } + + foreach (var property in root.EnumerateObject()) + { + if (property.Value.ValueKind == JsonValueKind.Array && property.Value.GetArrayLength() > 0 && + (TryGetVectorFromObject(property.Value[0], out vector) || TryGetSphericalVector(property.Value[0], out vector))) + { + return true; + } + } + } + + return false; + } + + private static bool TryGetVectorFromObject(JsonElement element, out HeliocentricAu vector) + { + vector = new HeliocentricAu(0, 0, 0); + if (element.ValueKind != JsonValueKind.Object) + { + return false; + } + + var x = TryGetDouble(element, "x", "X", "px", "PX"); + var y = TryGetDouble(element, "y", "Y", "py", "PY"); + var z = TryGetDouble(element, "z", "Z", "pz", "PZ"); + if (x is null || y is null || z is null) + { + return false; + } + + vector = new HeliocentricAu(x.Value, y.Value, z.Value); + return true; + } + + private static double? TryGetDouble(JsonElement element, params string[] names) + { + foreach (var name in names) + { + if (!element.TryGetProperty(name, out var value)) + { + continue; + } + + if (value.ValueKind == JsonValueKind.Number && value.TryGetDouble(out var number)) + { + return number; + } + + if (value.ValueKind == JsonValueKind.String && + double.TryParse(value.GetString(), NumberStyles.Float, CultureInfo.InvariantCulture, out number)) + { + return number; + } + } + + return null; + } + + private static bool TryGetSphericalVector(JsonElement element, out HeliocentricAu vector) + { + vector = new HeliocentricAu(0, 0, 0); + if (element.ValueKind != JsonValueKind.Object) + { + return false; + } + + if (!element.TryGetProperty("RA", out var raElement) || + !element.TryGetProperty("DEC", out var decElement)) + { + return false; + } + + var distance = TryGetDouble(element, "Dobs", "delta", "distance"); + if (distance is null || + !TryParseRightAscension(raElement.GetString(), out var raRadians) || + !TryParseDeclination(decElement.GetString(), out var decRadians)) + { + return false; + } + + var cosDec = Math.Cos(decRadians); + vector = new HeliocentricAu( + distance.Value * cosDec * Math.Cos(raRadians), + distance.Value * cosDec * Math.Sin(raRadians), + distance.Value * Math.Sin(decRadians)); + return true; + } + + private static bool TryParseRightAscension(string? value, out double radians) + { + radians = 0; + if (string.IsNullOrWhiteSpace(value)) + { + return false; + } + + var parts = value.Trim().Split(':', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + if (parts.Length == 3 && + double.TryParse(parts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out var hours) && + double.TryParse(parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out var minutes) && + double.TryParse(parts[2], NumberStyles.Float, CultureInfo.InvariantCulture, out var seconds)) + { + var sign = hours < 0 ? -1 : 1; + var totalHours = sign * (Math.Abs(hours) + minutes / 60d + seconds / 3600d); + radians = ToRadians(totalHours * 15d); + return true; + } + + if (double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var decimalHours)) + { + radians = ToRadians(decimalHours * 15d); + return true; + } + + return false; + } + + private static bool TryParseDeclination(string? value, out double radians) + { + radians = 0; + if (string.IsNullOrWhiteSpace(value)) + { + return false; + } + + var trimmed = value.Trim(); + var sign = trimmed.StartsWith("-", StringComparison.Ordinal) ? -1d : 1d; + trimmed = trimmed.TrimStart('+', '-'); + var parts = trimmed.Split(':', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + if (parts.Length == 3 && + double.TryParse(parts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out var degrees) && + double.TryParse(parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out var minutes) && + double.TryParse(parts[2], NumberStyles.Float, CultureInfo.InvariantCulture, out var seconds)) + { + radians = ToRadians(sign * (Math.Abs(degrees) + minutes / 60d + seconds / 3600d)); + return true; + } + + if (double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var decimalDegrees)) + { + radians = ToRadians(decimalDegrees); + return true; + } + + return false; + } + + private SolarEphemerisPayload CreateLocalPayload(DateTimeOffset epoch) + { + var planets = PlanetIds + .Select(id => new SolarEphemerisPlanet(id, ApproximatePosition(id, epoch))) + .ToArray(); + return new SolarEphemerisPayload("ephemeris:sync", "local", epoch, planets); + } + + private SolarEphemerisPayload CreateChinaApproximatePayload(DateTimeOffset epoch) + { + var planets = PlanetIds + .Select(id => new SolarEphemerisPlanet(id, ApproximatePosition(id, epoch))) + .ToArray(); + return new SolarEphemerisPayload("ephemeris:sync", "China Ephemeris Approx", epoch, planets); + } + + private static HeliocentricAu ApproximatePosition(string id, DateTimeOffset epoch) + { + var element = ApproximateElements[id]; + var jd = ToJulianDate(epoch.UtcDateTime); + var t = (jd - 2451545.0) / 36525.0; + var a = element.A + element.ARate * t; + var e = element.E + element.ERate * t; + var inclination = ToRadians(NormalizeDegrees(element.I + element.IRate * t)); + var meanLongitude = NormalizeDegrees(element.L + element.LRate * t); + var perihelion = NormalizeDegrees(element.Perihelion + element.PerihelionRate * t); + var node = ToRadians(NormalizeDegrees(element.Node + element.NodeRate * t)); + var argumentOfPerihelion = ToRadians(perihelion) - node; + var meanAnomaly = ToRadians(NormalizeDegrees(meanLongitude - perihelion)); + var eccentricAnomaly = SolveKepler(meanAnomaly, e); + var xv = a * (Math.Cos(eccentricAnomaly) - e); + var yv = a * Math.Sqrt(1 - e * e) * Math.Sin(eccentricAnomaly); + var trueAnomaly = Math.Atan2(yv, xv); + var radius = Math.Sqrt(xv * xv + yv * yv); + var angle = argumentOfPerihelion + trueAnomaly; + + return new HeliocentricAu( + radius * (Math.Cos(node) * Math.Cos(angle) - Math.Sin(node) * Math.Sin(angle) * Math.Cos(inclination)), + radius * (Math.Sin(node) * Math.Cos(angle) + Math.Cos(node) * Math.Sin(angle) * Math.Cos(inclination)), + radius * (Math.Sin(angle) * Math.Sin(inclination))); + } + + private bool TryReadCache(DateTimeOffset epoch, out SolarEphemerisPayload payload, bool preciseOnly) + { + payload = null!; + var path = CachePath(); + if (!File.Exists(path)) + { + return false; + } + + try + { + var text = File.ReadAllText(path); + var cached = JsonSerializer.Deserialize(text, JsonOptions); + if (cached is null || cached.Planets.Count == 0) + { + return false; + } + + if (preciseOnly && IsLocalApproximateSource(cached.Source)) + { + return false; + } + + if (epoch - cached.EpochUtc > TimeSpan.FromHours(6)) + { + return false; + } + + payload = cached; + return true; + } + catch + { + return false; + } + } + + private static bool IsLocalApproximateSource(string source) + { + return string.Equals(source, "local", StringComparison.OrdinalIgnoreCase) || + source.Contains("Approx", StringComparison.OrdinalIgnoreCase); + } + + private void WriteCache(SolarEphemerisPayload payload) + { + try + { + var path = CachePath(); + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + File.WriteAllText(path, JsonSerializer.Serialize(payload, JsonOptions)); + } + catch + { + } + } + + private string CachePath() + { + return Path.Combine(paths.Cache, "SolarEphemeris", "current.json"); + } + + private Task WriteLogAsync(string level, string category, string message, string detail, CancellationToken cancellationToken) + { + return logService?.WriteAsync(level, category, message, detail, cancellationToken) ?? Task.CompletedTask; + } + + private static double ToJulianDate(DateTime utc) + { + return new DateTimeOffset(DateTime.SpecifyKind(utc, DateTimeKind.Utc)).ToUnixTimeMilliseconds() / 86400000d + 2440587.5d; + } + + private static double SolveKepler(double meanAnomaly, double eccentricity) + { + var eccentricAnomaly = eccentricity < 0.8 ? meanAnomaly : Math.PI; + for (var index = 0; index < 12; index++) + { + var delta = (eccentricAnomaly - eccentricity * Math.Sin(eccentricAnomaly) - meanAnomaly) / + (1 - eccentricity * Math.Cos(eccentricAnomaly)); + eccentricAnomaly -= delta; + if (Math.Abs(delta) < 1e-10) + { + break; + } + } + + return eccentricAnomaly; + } + + private static double NormalizeDegrees(double value) + { + var normalized = value % 360; + return normalized < 0 ? normalized + 360 : normalized; + } + + private static double ToRadians(double degrees) => degrees * Math.PI / 180d; + + private sealed record Elements( + double A, + double ARate, + double E, + double ERate, + double I, + double IRate, + double L, + double LRate, + double Perihelion, + double PerihelionRate, + double Node, + double NodeRate); + + private static readonly Dictionary ApproximateElements = new(StringComparer.OrdinalIgnoreCase) + { + ["mercury"] = new(0.38709927, 0.00000037, 0.20563593, 0.00001906, 7.00497902, -0.00594749, 252.25032350, 149472.67411175, 77.45779628, 0.16047689, 48.33076593, -0.12534081), + ["venus"] = new(0.72333566, 0.00000390, 0.00677672, -0.00004107, 3.39467605, -0.00078890, 181.97909950, 58517.81538729, 131.60246718, 0.00268329, 76.67984255, -0.27769418), + ["earth"] = new(1.00000261, 0.00000562, 0.01671123, -0.00004392, -0.00001531, -0.01294668, 100.46457166, 35999.37244981, 102.93768193, 0.32327364, 0, 0), + ["mars"] = new(1.52371034, 0.00001847, 0.09339410, 0.00007882, 1.84969142, -0.00813131, -4.55343205, 19140.30268499, -23.94362959, 0.44441088, 49.55953891, -0.29257343), + ["jupiter"] = new(5.20288700, -0.00011607, 0.04838624, -0.00013253, 1.30439695, -0.00183714, 34.39644051, 3034.74612775, 14.72847983, 0.21252668, 100.47390909, 0.20469106), + ["saturn"] = new(9.53667594, -0.00125060, 0.05386179, -0.00050991, 2.48599187, 0.00193609, 49.95424423, 1222.49362201, 92.59887831, -0.41897216, 113.66242448, -0.28867794), + ["uranus"] = new(19.18916464, -0.00196176, 0.04725744, -0.00004397, 0.77263783, -0.00242939, 313.23810451, 428.48202785, 170.95427630, 0.40805281, 74.01692503, 0.04240589), + ["neptune"] = new(30.06992276, 0.00026291, 0.00859048, 0.00005105, 1.77004347, 0.00035372, -55.12002969, 218.45945325, 44.96476227, -0.32241464, 131.78422574, -0.00508664) + }; +} diff --git a/src/YMhut.Box.Core/Startup/InstallIntegrityCheckService.cs b/src/YMhut.Box.Core/Startup/InstallIntegrityCheckService.cs new file mode 100644 index 0000000..8b85525 --- /dev/null +++ b/src/YMhut.Box.Core/Startup/InstallIntegrityCheckService.cs @@ -0,0 +1,830 @@ +using System.Globalization; +using System.Text.Json; +using YMhut.Box.Core.App; +using YMhut.Box.Core.Logging; + +namespace YMhut.Box.Core.Startup; + +public interface IInstallIntegrityCheckService +{ + Task RunFastPreflightAsync(CancellationToken cancellationToken = default); + + Task RunMonthlyIntegrityCheckAsync(CancellationToken cancellationToken = default); + + Task RunManualIntegrityCheckAsync(CancellationToken cancellationToken = default); + + Task GetLatestReportAsync(CancellationToken cancellationToken = default); + + Task GetLatestReportAsync(StartupCheckKind kind, CancellationToken cancellationToken = default); + + Task GetLatestCurrentReportAsync(CancellationToken cancellationToken = default); + + Task GetLatestCurrentReportAsync(StartupCheckKind kind, CancellationToken cancellationToken = default); + + Task GetReportAsync(Guid id, CancellationToken cancellationToken = default); + + Task> GetHistorySummariesAsync(int take = 30, CancellationToken cancellationToken = default); + + Task> GetHistoryAsync(int take = 30, CancellationToken cancellationToken = default); + + Task> GetCurrentHistoryAsync(int take = 30, CancellationToken cancellationToken = default); + + Task RecheckItemAsync(Guid reportId, string itemId, CancellationToken cancellationToken = default); +} + +public sealed class InstallIntegrityCheckService( + AppPaths paths, + IStartupCheckStore store, + ILogService? logService = null, + string? baseDirectory = null, + Func? clock = null) : IInstallIntegrityCheckService +{ + private static readonly TimeSpan MonthlyInterval = TimeSpan.FromDays(30); + private static readonly string[] SupportedCultures = ["zh-CN", "en-US"]; + private static readonly string[] LaunchableExtensions = [".exe", ".bat", ".cmd", ".lnk", ".msc", ".ps1", ".vbs"]; + private static readonly string[] CriticalRelativeFiles = + [ + "YMhutBox.exe", + "YMhutBox.dll", + "resources.pri" + ]; + + private readonly string? _baseDirectory = baseDirectory; + private readonly Func _clock = clock ?? (() => DateTimeOffset.Now); + + public Task RunFastPreflightAsync(CancellationToken cancellationToken = default) + => RunIntegrityCoreAsync(StartupCheckKind.FastPreflight, includeFullPayloadChecks: false, cancellationToken); + + public async Task RunMonthlyIntegrityCheckAsync(CancellationToken cancellationToken = default) + { + var context = ResolveCurrentContext(); + var latest = await GetLatestCurrentReportAsync(StartupCheckKind.MonthlyIntegrity, cancellationToken).ConfigureAwait(false); + if (latest is not null && + latest.CompletedAt + MonthlyInterval > _clock() && + string.Equals(latest.InstallIdentity, context.InstallIdentity, StringComparison.OrdinalIgnoreCase)) + { + return latest; + } + + return await RunIntegrityCoreAsync(StartupCheckKind.MonthlyIntegrity, includeFullPayloadChecks: true, cancellationToken).ConfigureAwait(false); + } + + public Task RunManualIntegrityCheckAsync(CancellationToken cancellationToken = default) + => RunIntegrityCoreAsync(StartupCheckKind.ManualIntegrity, includeFullPayloadChecks: true, cancellationToken); + + public Task GetLatestReportAsync(CancellationToken cancellationToken = default) + => GetLatestReportCoreAsync(null, cancellationToken); + + public Task GetLatestReportAsync(StartupCheckKind kind, CancellationToken cancellationToken = default) + => GetLatestReportCoreAsync(kind, cancellationToken); + + public Task GetLatestCurrentReportAsync(CancellationToken cancellationToken = default) + => GetLatestCurrentReportCoreAsync(null, cancellationToken); + + public Task GetLatestCurrentReportAsync(StartupCheckKind kind, CancellationToken cancellationToken = default) + => GetLatestCurrentReportCoreAsync(kind, cancellationToken); + + public async Task GetReportAsync(Guid id, CancellationToken cancellationToken = default) + { + var report = await store.GetReportAsync(id, cancellationToken).ConfigureAwait(false); + return report is null ? null : MarkCurrentInstall(report, ResolveCurrentContext()); + } + + public async Task> GetHistorySummariesAsync(int take = 30, CancellationToken cancellationToken = default) + { + var context = ResolveCurrentContext(); + var summaries = await store.GetHistorySummariesAsync(take, cancellationToken).ConfigureAwait(false); + return summaries.Select(summary => MarkCurrentInstall(summary, context)).ToArray(); + } + + public async Task> GetHistoryAsync(int take = 30, CancellationToken cancellationToken = default) + { + var context = ResolveCurrentContext(); + var reports = await store.GetHistoryAsync(take, cancellationToken).ConfigureAwait(false); + return reports.Select(report => MarkCurrentInstall(report, context)).ToArray(); + } + + public async Task> GetCurrentHistoryAsync(int take = 30, CancellationToken cancellationToken = default) + { + var context = ResolveCurrentContext(); + var history = await store.GetHistoryAsync(Math.Max(take, 200), cancellationToken).ConfigureAwait(false); + return history + .Select(report => MarkCurrentInstall(report, context)) + .Where(report => report.IsCurrentInstall) + .Take(Math.Clamp(take, 1, 200)) + .Select(StartupCheckReportSummary.FromReport) + .ToArray(); + } + + public async Task RecheckItemAsync(Guid reportId, string itemId, CancellationToken cancellationToken = default) + { + var report = await GetReportAsync(reportId, cancellationToken).ConfigureAwait(false); + var item = report?.Items.FirstOrDefault(candidate => string.Equals(candidate.Id, itemId, StringComparison.OrdinalIgnoreCase)); + return item is null ? null : RecheckItem(item, ResolveCurrentContext()); + } + + private async Task RunIntegrityCoreAsync( + StartupCheckKind kind, + bool includeFullPayloadChecks, + CancellationToken cancellationToken) + { + var startedAt = _clock(); + var context = ResolveCurrentContext(); + var installRoot = context.InstallRoot; + var manifest = TryReadManifest(installRoot, out var manifestPath, out var manifestError); + var rootCultureFallback = HasAnySupportedRootCultureResources(installRoot); + var items = new List(); + + CheckWritableDirectory(items, "user-root", "用户数据目录", paths.Root, StartupCheckSeverity.Critical); + CheckWritableDirectory(items, "logs", "日志目录", paths.Logs, StartupCheckSeverity.Critical); + CheckWritableDirectory(items, "data", "数据目录", paths.Data, StartupCheckSeverity.Critical); + CheckWritableDirectory(items, "sqlite-parent", "主 SQLite 存储目录", Path.GetDirectoryName(AppDatabasePaths.ResolveMainDatabasePath(paths)) ?? paths.Logs, StartupCheckSeverity.Critical); + AddDirectoryCheck(items, "install-root", "安装目录", installRoot, StartupCheckSeverity.Critical); + if (manifest is null) + { + items.Add(Skipped( + "manifest", + "安装清单", + manifestError ?? "当前安装目录未包含安装清单;按当前目录实际文件执行降噪检查。", + manifestPath)); + } + else + { + AddFileCheck(items, "manifest", "安装清单", manifestPath, StartupCheckSeverity.Info); + } + + foreach (var relativePath in CriticalRelativeFiles) + { + AddRelativeFileCheck(items, installRoot, relativePath, CriticalFileSeverity(relativePath, manifest, rootCultureFallback)); + } + + AddPureLanguageChecks(items, installRoot, strictPureLang: manifest is not null, allowRootCultureFallback: rootCultureFallback); + AddAppDataPayloadChecks(items); + AddToolsChecks(items, installRoot, includeFullPayloadChecks ? manifest : null); + AddMetadataChecks(items, installRoot); + + if (includeFullPayloadChecks) + { + if (manifest is not null) + { + AddManifestFileChecks(items, installRoot, manifest); + } + } + + var report = StartupCheckReport.Create( + kind, + startedAt, + installRoot, + context.ManifestIdentity, + context.InstallIdentity, + context.CheckBasis, + items); + await SaveAndLogAsync(report, cancellationToken).ConfigureAwait(false); + return report; + } + + private static InstallManifest? TryReadManifest(string installRoot, out string manifestPath, out string? error) + { + manifestPath = Path.Combine(installRoot, InstallManifest.RelativePath.Replace('/', Path.DirectorySeparatorChar)); + error = null; + try + { + if (!File.Exists(manifestPath)) + { + error = "未找到安装清单。开发构建可继续使用,正式安装包应包含该文件。"; + return null; + } + + return InstallManifest.Parse(File.ReadAllText(manifestPath)); + } + catch (Exception exception) + { + error = $"安装清单读取失败:{Sanitize(exception.Message)}"; + return null; + } + } + + private static void CheckWritableDirectory( + ICollection items, + string id, + string name, + string directory, + StartupCheckSeverity severity) + { + try + { + Directory.CreateDirectory(directory); + var probe = Path.Combine(directory, $".ymhut-probe-{Guid.NewGuid():N}.tmp"); + File.WriteAllText(probe, "ok"); + File.Delete(probe); + items.Add(Passed(id, name, $"可写:{directory}", directory)); + } + catch (Exception exception) + { + items.Add(Failed(id, name, severity, $"目录不可写:{Sanitize(exception.Message)}", directory)); + } + } + + private static void AddDirectoryCheck( + ICollection items, + string id, + string name, + string directory, + StartupCheckSeverity severity) + { + items.Add(Directory.Exists(directory) + ? Passed(id, name, $"目录存在:{directory}", directory) + : Missing(id, name, severity, "目录缺失", directory)); + } + + private static void AddFileCheck( + ICollection items, + string id, + string name, + string path, + StartupCheckSeverity severity, + string? missingDetail = null) + { + items.Add(File.Exists(path) + ? Passed(id, name, $"文件存在:{path}", path) + : Missing(id, name, severity, missingDetail ?? "文件缺失", path)); + } + + private static void AddRelativeFileCheck( + ICollection items, + string installRoot, + string relativePath, + StartupCheckSeverity severity) + { + var normalized = Normalize(relativePath); + var path = Path.Combine(installRoot, relativePath.Replace('/', Path.DirectorySeparatorChar)); + if (normalized.Equals("resources.pri", StringComparison.OrdinalIgnoreCase) && !File.Exists(path)) + { + var appPri = Path.Combine(installRoot, "YMhutBox.pri"); + if (File.Exists(appPri)) + { + items.Add(Passed( + $"file:{normalized}", + "resources.pri / YMhutBox.pri", + "检测到当前 WinUI 开发/运行输出使用 YMhutBox.pri,资源索引可用。", + appPri)); + return; + } + } + + AddFileCheck( + items, + $"file:{normalized}", + relativePath, + path, + severity); + } + + private static StartupCheckSeverity CriticalFileSeverity(string relativePath, InstallManifest? manifest, bool allowRootCultureFallback) + { + if (manifest is null && + allowRootCultureFallback && + string.Equals(Normalize(relativePath), "resources.pri", StringComparison.OrdinalIgnoreCase)) + { + return StartupCheckSeverity.Warning; + } + + return StartupCheckSeverity.Critical; + } + + private static void AddPureLanguageChecks( + ICollection items, + string installRoot, + bool strictPureLang, + bool allowRootCultureFallback) + { + var resourcesLang = Path.Combine(installRoot, "resources", "lang"); + if (Directory.Exists(resourcesLang)) + { + items.Add(Failed( + "lang:legacy-resources", + "旧语言压缩目录", + StartupCheckSeverity.Critical, + "检测到 resources\\lang。正式发布布局应只使用 lang\\zh-CN 和 lang\\en-US。", + resourcesLang)); + } + else + { + items.Add(Passed("lang:legacy-resources", "旧语言压缩目录", "未检测到 resources\\lang。", resourcesLang)); + } + + foreach (var culture in SupportedCultures) + { + var cultureRoot = Path.Combine(installRoot, "lang", culture); + var hasRootCultureFallback = allowRootCultureFallback && HasRootCultureResources(installRoot, culture); + var missingSeverity = strictPureLang || !hasRootCultureFallback + ? StartupCheckSeverity.Critical + : StartupCheckSeverity.Warning; + if (!Directory.Exists(cultureRoot)) + { + items.Add(hasRootCultureFallback + ? Passed( + $"lang:{culture}", + $"语言资源 {culture}", + "检测到当前开发/运行输出的根目录语言资源,语言资源可用。", + Path.Combine(installRoot, culture)) + : Missing( + $"lang:{culture}", + $"语言资源 {culture}", + missingSeverity, + "语言目录缺失", + cultureRoot)); + continue; + } + + var hasSatellite = Directory.EnumerateFiles(cultureRoot, "*.mui", SearchOption.AllDirectories).Any() || + Directory.EnumerateFiles(cultureRoot, "*.resources.dll", SearchOption.AllDirectories).Any(); + items.Add(hasSatellite + ? Passed($"lang:{culture}", $"语言资源 {culture}", "语言资源已展开。", cultureRoot) + : hasRootCultureFallback + ? Passed( + $"lang:{culture}", + $"语言资源 {culture}", + "lang 目录未展开 MUI 或 resources.dll,但根目录语言资源可用。", + Path.Combine(installRoot, culture)) + : Missing( + $"lang:{culture}", + $"语言资源 {culture}", + missingSeverity, + "语言资源目录中没有 MUI 或 resources.dll。", + cultureRoot)); + } + + var rootCultures = (Directory.Exists(installRoot) + ? Directory.EnumerateDirectories(installRoot) + .Select(Path.GetFileName) + .Where(name => !string.IsNullOrWhiteSpace(name) && LooksLikeCultureName(name!)) + .OrderBy(name => name, StringComparer.OrdinalIgnoreCase) + .ToArray() + : Array.Empty()); + if (rootCultures.Length > 0) + { + items.Add(Skipped( + "lang:root-culture-summary", + "根目录语言资源", + $"检测到 {rootCultures.Length} 个运行时/框架语言资源目录,已识别为当前安装目录的正常卫星资源,不计入问题。", + installRoot)); + } + } + + private static bool HasAnySupportedRootCultureResources(string installRoot) + => SupportedCultures.Any(culture => HasRootCultureResources(installRoot, culture)); + + private static bool HasRootCultureResources(string installRoot, string culture) + { + try + { + var cultureRoot = Path.Combine(installRoot, culture); + if (!Directory.Exists(cultureRoot)) + { + return false; + } + + return Directory.EnumerateFiles(cultureRoot, "*.mui", SearchOption.AllDirectories).Any() || + Directory.EnumerateFiles(cultureRoot, "*.resources.dll", SearchOption.AllDirectories).Any(); + } + catch + { + return false; + } + } + + private void AddAppDataPayloadChecks(ICollection items) + { + var removed = paths.CleanupUserPayloadDirectories() + .ToHashSet(StringComparer.OrdinalIgnoreCase); + + foreach (var relative in AppPaths.UserPayloadDirectoryNames.Distinct(StringComparer.OrdinalIgnoreCase)) + { + var path = Path.Combine(paths.Root, relative); + if (Directory.Exists(path)) + { + items.Add(new StartupCheckItem( + $"userdata:{relative.ToLowerInvariant()}", + $"User data {relative}", + StartupCheckSeverity.Info, + StartupCheckStatus.Skipped, + "Legacy user payload remains, but current install health is based only on the active install directory.", + path)); + continue; + } + + items.Add(Passed( + $"userdata:{relative.ToLowerInvariant()}", + $"User data {relative}", + removed.Contains(path) + ? "Removed a legacy payload copy from the user data folder." + : "No legacy payload copy found in the user data folder.", + path)); + } + } + + private static void AddManifestFileChecks(ICollection items, string installRoot, InstallManifest manifest) + { + foreach (var relativePath in manifest.RequiredFiles.Distinct(StringComparer.OrdinalIgnoreCase)) + { + AddRelativeFileCheck(items, installRoot, relativePath, StartupCheckSeverity.Critical); + } + + foreach (var relativePath in manifest.Files + .Where(ShouldVerifyPayloadFile) + .Distinct(StringComparer.OrdinalIgnoreCase)) + { + var severity = relativePath.StartsWith("Tools/", StringComparison.OrdinalIgnoreCase) || + relativePath.StartsWith("Metadata/", StringComparison.OrdinalIgnoreCase) + ? StartupCheckSeverity.Warning + : StartupCheckSeverity.Critical; + AddRelativeFileCheck(items, installRoot, relativePath, severity); + } + } + + private static bool ShouldVerifyPayloadFile(string relativePath) + { + var normalized = Normalize(relativePath); + return normalized.StartsWith("lang/", StringComparison.OrdinalIgnoreCase) || + normalized.StartsWith("Tools/", StringComparison.OrdinalIgnoreCase) || + normalized.StartsWith("Metadata/", StringComparison.OrdinalIgnoreCase) || + normalized.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || + normalized.EndsWith(".exe", StringComparison.OrdinalIgnoreCase) || + normalized.EndsWith(".mui", StringComparison.OrdinalIgnoreCase) || + normalized.Equals("resources.pri", StringComparison.OrdinalIgnoreCase); + } + + private static void AddToolsChecks(ICollection items, string installRoot, InstallManifest? manifest) + { + var toolsRoot = Path.Combine(installRoot, "Tools"); + if (!Directory.Exists(toolsRoot)) + { + items.Add(Missing("tools:root", "随包工具目录", StartupCheckSeverity.Warning, "Tools 目录缺失,外部工具将不可用。", toolsRoot)); + return; + } + + var launchables = Directory.EnumerateFiles(toolsRoot, "*", SearchOption.AllDirectories) + .Where(path => LaunchableExtensions.Contains(Path.GetExtension(path), StringComparer.OrdinalIgnoreCase)) + .Take(5000) + .ToArray(); + items.Add(launchables.Length > 0 + ? Passed("tools:launchables", "外部工具启动项", $"检测到 {launchables.Length} 个启动项。", toolsRoot) + : Missing("tools:launchables", "外部工具启动项", StartupCheckSeverity.Warning, "Tools 目录中没有可启动文件。", toolsRoot)); + + if (manifest is null) + { + return; + } + + foreach (var relativePath in manifest.Files + .Where(path => Normalize(path).StartsWith("Tools/", StringComparison.OrdinalIgnoreCase) && + LaunchableExtensions.Contains(Path.GetExtension(path), StringComparer.OrdinalIgnoreCase)) + .Distinct(StringComparer.OrdinalIgnoreCase)) + { + AddRelativeFileCheck(items, installRoot, relativePath, StartupCheckSeverity.Warning); + } + } + + private static void AddMetadataChecks(ICollection items, string installRoot) + { + var metadataRoot = Path.Combine(installRoot, "Metadata"); + if (!Directory.Exists(metadataRoot)) + { + items.Add(Missing("metadata:root", "工具元数据目录", StartupCheckSeverity.Warning, "Metadata 目录缺失,外部工具说明会降级。", metadataRoot)); + return; + } + + var jsonFiles = Directory.EnumerateFiles(metadataRoot, "*.json", SearchOption.AllDirectories).Take(500).ToArray(); + if (jsonFiles.Length == 0) + { + items.Add(Missing("metadata:json", "工具元数据 JSON", StartupCheckSeverity.Warning, "Metadata 目录中没有 JSON 元数据。", metadataRoot)); + return; + } + + var invalid = new List(); + foreach (var file in jsonFiles) + { + try + { + using var stream = File.OpenRead(file); + using var _ = JsonDocument.Parse(stream); + } + catch + { + invalid.Add(file); + if (invalid.Count >= 5) + { + break; + } + } + } + + items.Add(invalid.Count == 0 + ? Passed("metadata:json", "工具元数据 JSON", $"检测到 {jsonFiles.Length} 个可解析 JSON。", metadataRoot) + : Failed("metadata:json", "工具元数据 JSON", StartupCheckSeverity.Warning, $"发现无法解析的 JSON:{string.Join("; ", invalid)}", metadataRoot)); + } + + private async Task SaveAndLogAsync(StartupCheckReport report, CancellationToken cancellationToken) + { + await store.SaveAsync(report, cancellationToken).ConfigureAwait(false); + if (logService is not null) + { + var level = report.HasCriticalIssues ? "Error" : report.HasVisibleIssues ? "Warning" : "Information"; + await logService.WriteAsync( + level, + "startup-check", + "启动自检完成", + $"kind={report.Kind}; issues={report.IssueCount}; visible={report.VisibleIssueCount}; critical={report.CriticalIssueCount}; root={report.InstallRoot}", + cancellationToken).ConfigureAwait(false); + } + } + + private async Task GetLatestReportCoreAsync(StartupCheckKind? kind, CancellationToken cancellationToken) + { + var report = kind is null + ? await store.GetLatestReportAsync(cancellationToken).ConfigureAwait(false) + : await store.GetLatestReportAsync(kind.Value, cancellationToken).ConfigureAwait(false); + return report is null ? null : MarkCurrentInstall(report, ResolveCurrentContext()); + } + + private async Task GetLatestCurrentReportCoreAsync(StartupCheckKind? kind, CancellationToken cancellationToken) + { + var context = ResolveCurrentContext(); + var history = await store.GetHistoryAsync(200, cancellationToken).ConfigureAwait(false); + return history + .Select(report => MarkCurrentInstall(report, context)) + .Where(report => report.IsCurrentInstall && (kind is null || report.Kind == kind.Value)) + .OrderByDescending(report => report.CompletedAt) + .FirstOrDefault(); + } + + private InstallRootContext ResolveCurrentContext() + => InstallLayoutPaths.ResolveInstallRootContext(_baseDirectory); + + private static StartupCheckReport MarkCurrentInstall(StartupCheckReport report, InstallRootContext context) + { + var installIdentity = string.IsNullOrWhiteSpace(report.InstallIdentity) + ? InstallRootContext.BuildInstallIdentity(report.InstallRoot, report.ManifestIdentity) + : report.InstallIdentity; + var isCurrent = string.Equals(installIdentity, context.InstallIdentity, StringComparison.OrdinalIgnoreCase); + var items = isCurrent ? NormalizeCurrentInstallLegacyItems(report.Items, context) : report.Items; + return report with + { + Items = items, + InstallIdentity = installIdentity, + IsCurrentInstall = isCurrent, + SupersededByCurrentInstall = !isCurrent, + CheckBasis = string.IsNullOrWhiteSpace(report.CheckBasis) + ? $"installRoot={report.InstallRoot}; manifest={report.ManifestIdentity}" + : report.CheckBasis + }; + } + + private static IReadOnlyList NormalizeCurrentInstallLegacyItems( + IReadOnlyList items, + InstallRootContext context) + { + if (items.Count == 0) + { + return items; + } + + return items.Select(item => NormalizeCurrentInstallLegacyItem(item, context)).ToArray(); + } + + private static StartupCheckItem NormalizeCurrentInstallLegacyItem(StartupCheckItem item, InstallRootContext context) + { + if (!item.IsIssue) + { + return item; + } + + if (item.Id.StartsWith("lang:root:", StringComparison.OrdinalIgnoreCase)) + { + var culture = item.Id["lang:root:".Length..]; + var culturePath = Path.Combine(context.InstallRoot, culture); + if (HasRootCultureResources(context.InstallRoot, culture)) + { + return item with + { + Severity = StartupCheckSeverity.Info, + Status = StartupCheckStatus.Skipped, + Detail = "旧版报告将根目录运行时语言资源记录为异常;当前版本已识别为正常卫星资源,旧记录仅供参考。", + Path = culturePath + }; + } + } + + if (item.Id is "lang:zh-CN" or "lang:en-US") + { + var culture = item.Id["lang:".Length..]; + if (HasRootCultureResources(context.InstallRoot, culture)) + { + return item with + { + Severity = StartupCheckSeverity.Info, + Status = StartupCheckStatus.Passed, + Detail = "检测到当前开发/运行输出的根目录语言资源,语言资源可用;旧记录仅供参考。", + Path = Path.Combine(context.InstallRoot, culture) + }; + } + } + + if (item.Id.Equals("file:resources.pri", StringComparison.OrdinalIgnoreCase)) + { + var appPri = Path.Combine(context.InstallRoot, "YMhutBox.pri"); + if (File.Exists(appPri)) + { + return item with + { + Severity = StartupCheckSeverity.Info, + Status = StartupCheckStatus.Passed, + Name = "resources.pri / YMhutBox.pri", + Detail = "检测到当前 WinUI 开发/运行输出使用 YMhutBox.pri,资源索引可用;旧记录仅供参考。", + Path = appPri + }; + } + } + + if (item.Id.Equals("manifest", StringComparison.OrdinalIgnoreCase) && + !File.Exists(context.ManifestPath) && + context.ManifestIdentity.StartsWith("no-manifest:", StringComparison.OrdinalIgnoreCase)) + { + return item with + { + Severity = StartupCheckSeverity.Info, + Status = StartupCheckStatus.Skipped, + Detail = "当前安装目录未包含安装清单;按当前目录实际文件执行降噪检查,旧记录仅供参考。", + Path = context.ManifestPath + }; + } + + return item; + } + + private static StartupCheckReportSummary MarkCurrentInstall(StartupCheckReportSummary summary, InstallRootContext context) + { + var installIdentity = string.IsNullOrWhiteSpace(summary.InstallIdentity) + ? InstallRootContext.BuildInstallIdentity(summary.InstallRoot, summary.ManifestIdentity) + : summary.InstallIdentity; + var isCurrent = string.Equals(installIdentity, context.InstallIdentity, StringComparison.OrdinalIgnoreCase); + return summary with + { + InstallIdentity = installIdentity, + IsCurrentInstall = isCurrent, + SupersededByCurrentInstall = !isCurrent, + VisibleIssueCount = isCurrent ? summary.VisibleIssueCount : 0, + CheckBasis = string.IsNullOrWhiteSpace(summary.CheckBasis) + ? $"installRoot={summary.InstallRoot}; manifest={summary.ManifestIdentity}" + : summary.CheckBasis + }; + } + + private static StartupCheckItem RecheckItem(StartupCheckItem item, InstallRootContext context) + { + var path = ResolveCurrentItemPath(item, context); + if (string.IsNullOrWhiteSpace(path)) + { + return item with + { + Severity = StartupCheckSeverity.Info, + Status = StartupCheckStatus.Skipped, + Detail = "No concrete path is attached to this item." + }; + } + + var passed = RecheckPathForItem(item.Id, path); + return item with + { + Severity = passed ? StartupCheckSeverity.Info : item.Severity, + Status = passed ? StartupCheckStatus.Passed : item.Status, + Detail = passed + ? "Current check passed. The old record is kept for reference only." + : "Current check still does not pass for this path.", + Path = path + }; + } + + private static string? ResolveCurrentItemPath(StartupCheckItem item, InstallRootContext context) + { + if (item.Id.StartsWith("file:", StringComparison.OrdinalIgnoreCase)) + { + var relative = item.Id["file:".Length..].Replace('/', Path.DirectorySeparatorChar); + return Path.Combine(context.InstallRoot, relative); + } + + if (item.Id == "manifest") + { + return context.ManifestPath; + } + + if (item.Id == "install-root") + { + return context.InstallRoot; + } + + if (item.Id.StartsWith("lang:", StringComparison.OrdinalIgnoreCase) && + !item.Id.StartsWith("lang:legacy", StringComparison.OrdinalIgnoreCase) && + !item.Id.StartsWith("lang:root:", StringComparison.OrdinalIgnoreCase)) + { + return Path.Combine(context.InstallRoot, "lang", item.Id["lang:".Length..]); + } + + if (item.Id == "lang:legacy-resources") + { + return Path.Combine(context.InstallRoot, "resources", "lang"); + } + + if (item.Id.StartsWith("lang:root:", StringComparison.OrdinalIgnoreCase)) + { + return Path.Combine(context.InstallRoot, item.Id["lang:root:".Length..]); + } + + if (item.Id is "tools:root" or "tools:launchables") + { + return Path.Combine(context.InstallRoot, "Tools"); + } + + if (item.Id is "metadata:root" or "metadata:json") + { + return Path.Combine(context.InstallRoot, "Metadata"); + } + + return item.Path; + } + + private static bool RecheckPathForItem(string itemId, string path) + { + if (itemId == "lang:legacy-resources" || + itemId.StartsWith("lang:root:", StringComparison.OrdinalIgnoreCase) || + itemId.StartsWith("userdata:", StringComparison.OrdinalIgnoreCase)) + { + return !Directory.Exists(path); + } + + if (itemId.StartsWith("lang:", StringComparison.OrdinalIgnoreCase)) + { + return Directory.Exists(path) && + (Directory.EnumerateFiles(path, "*.mui", SearchOption.AllDirectories).Any() || + Directory.EnumerateFiles(path, "*.resources.dll", SearchOption.AllDirectories).Any()); + } + + if (itemId is "install-root" or "tools:root" or "metadata:root") + { + return Directory.Exists(path); + } + + if (itemId == "tools:launchables") + { + return Directory.Exists(path) && + Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories) + .Any(file => LaunchableExtensions.Contains(Path.GetExtension(file), StringComparer.OrdinalIgnoreCase)); + } + + if (itemId == "metadata:json") + { + return Directory.Exists(path) && + Directory.EnumerateFiles(path, "*.json", SearchOption.AllDirectories).Any(); + } + + if (itemId.StartsWith("file:", StringComparison.OrdinalIgnoreCase) || itemId == "manifest") + { + return File.Exists(path); + } + + return File.Exists(path) || Directory.Exists(path); + } + + private static StartupCheckItem Passed(string id, string name, string detail, string? path = null) + => new(id, name, StartupCheckSeverity.Info, StartupCheckStatus.Passed, detail, path); + + private static StartupCheckItem Missing(string id, string name, StartupCheckSeverity severity, string detail, string? path = null) + => new(id, name, severity, StartupCheckStatus.Missing, detail, path); + + private static StartupCheckItem Failed(string id, string name, StartupCheckSeverity severity, string detail, string? path = null) + => new(id, name, severity, StartupCheckStatus.Failed, detail, path); + + private static StartupCheckItem Skipped(string id, string name, string detail, string? path = null) + => new(id, name, StartupCheckSeverity.Info, StartupCheckStatus.Skipped, detail, path); + + private static string Normalize(string relativePath) + => relativePath.Trim().Replace('\\', '/').TrimStart('/'); + + private static bool LooksLikeCultureName(string name) + { + try + { + var culture = CultureInfo.GetCultureInfo(name); + return !string.IsNullOrWhiteSpace(culture.Name) && + string.Equals(culture.Name, name, StringComparison.OrdinalIgnoreCase); + } + catch (CultureNotFoundException) + { + return false; + } + } + + private static string Sanitize(string value) + => value.ReplaceLineEndings(" ").Trim(); +} diff --git a/src/YMhut.Box.Core/Startup/StartupCheckModels.cs b/src/YMhut.Box.Core/Startup/StartupCheckModels.cs new file mode 100644 index 0000000..95d265c --- /dev/null +++ b/src/YMhut.Box.Core/Startup/StartupCheckModels.cs @@ -0,0 +1,164 @@ +using System.Text.Json; + +namespace YMhut.Box.Core.Startup; + +public enum StartupCheckKind +{ + FastPreflight, + MonthlyIntegrity, + ManualIntegrity +} + +public enum StartupCheckSeverity +{ + Info, + Warning, + Critical +} + +public enum StartupCheckStatus +{ + Passed, + Missing, + Failed, + Skipped +} + +public sealed record StartupCheckItem( + string Id, + string Name, + StartupCheckSeverity Severity, + StartupCheckStatus Status, + string Detail, + string? Path = null) +{ + public bool IsIssue => Status is StartupCheckStatus.Missing or StartupCheckStatus.Failed; +} + +public sealed record StartupCheckReport( + Guid Id, + StartupCheckKind Kind, + DateTimeOffset StartedAt, + DateTimeOffset CompletedAt, + string InstallRoot, + string ManifestIdentity, + IReadOnlyList Items, + string InstallIdentity = "", + bool IsCurrentInstall = true, + bool SupersededByCurrentInstall = false, + string CheckBasis = "") +{ + public int IssueCount => Items.Count(item => item.IsIssue); + + public int CriticalIssueCount => Items.Count(item => item.IsIssue && item.Severity == StartupCheckSeverity.Critical); + + public int WarningIssueCount => Items.Count(item => item.IsIssue && item.Severity == StartupCheckSeverity.Warning); + + public int VisibleIssueCount => IsCurrentInstall + ? Items.Count(item => item.IsIssue && !item.Id.StartsWith("repaired:", StringComparison.OrdinalIgnoreCase)) + : 0; + + public int VisibleCriticalIssueCount => IsCurrentInstall + ? Items.Count(item => item.IsIssue && + item.Severity == StartupCheckSeverity.Critical && + !item.Id.StartsWith("repaired:", StringComparison.OrdinalIgnoreCase)) + : 0; + + public int VisibleWarningIssueCount => IsCurrentInstall + ? Items.Count(item => item.IsIssue && + item.Severity == StartupCheckSeverity.Warning && + !item.Id.StartsWith("repaired:", StringComparison.OrdinalIgnoreCase)) + : 0; + + public bool HasVisibleIssues => VisibleIssueCount > 0; + + public bool HasCriticalIssues => CriticalIssueCount > 0; + + public static StartupCheckReport Create( + StartupCheckKind kind, + DateTimeOffset startedAt, + string installRoot, + string manifestIdentity, + string installIdentity, + string checkBasis, + IEnumerable items) + { + return new StartupCheckReport( + Guid.NewGuid(), + kind, + startedAt, + DateTimeOffset.Now, + installRoot, + manifestIdentity, + items.ToArray(), + installIdentity, + IsCurrentInstall: true, + SupersededByCurrentInstall: false, + checkBasis); + } + + public string ToJson() + => JsonSerializer.Serialize(this, StartupCheckJson.Options); +} + +public sealed record StartupCheckReportSummary( + Guid Id, + StartupCheckKind Kind, + DateTimeOffset StartedAt, + DateTimeOffset CompletedAt, + string InstallRoot, + string ManifestIdentity, + int ItemCount, + int IssueCount, + int CriticalIssueCount, + int WarningIssueCount, + int VisibleIssueCount, + string InstallIdentity = "", + bool IsCurrentInstall = true, + bool SupersededByCurrentInstall = false, + string CheckBasis = "") +{ + public bool HasCriticalIssues => CriticalIssueCount > 0; + + public bool HasVisibleIssues => VisibleIssueCount > 0; + + public static StartupCheckReportSummary FromReport(StartupCheckReport report) + => new( + report.Id, + report.Kind, + report.StartedAt, + report.CompletedAt, + report.InstallRoot, + report.ManifestIdentity, + report.Items.Count, + report.IssueCount, + report.CriticalIssueCount, + report.WarningIssueCount, + report.VisibleIssueCount, + report.InstallIdentity, + report.IsCurrentInstall, + report.SupersededByCurrentInstall, + report.CheckBasis); +} + +public sealed record StartupInitializationSnapshot( + DateTimeOffset StartedAt, + DateTimeOffset CompletedAt, + StartupCheckReport FastPreflightReport, + int ToolCount, + string InstallRoot) +{ + public bool HasStartupIssue => FastPreflightReport.IssueCount > 0; + + public bool HasVisibleStartupIssue => FastPreflightReport.HasVisibleIssues; + + public bool HasCriticalStartupIssue => FastPreflightReport.HasCriticalIssues; +} + +public static class StartupCheckJson +{ + public static JsonSerializerOptions Options { get; } = new(JsonSerializerDefaults.Web) + { + WriteIndented = true + }; +} diff --git a/src/YMhut.Box.Core/Startup/StartupCheckStore.cs b/src/YMhut.Box.Core/Startup/StartupCheckStore.cs new file mode 100644 index 0000000..0017b5e --- /dev/null +++ b/src/YMhut.Box.Core/Startup/StartupCheckStore.cs @@ -0,0 +1,639 @@ +using System.Globalization; +using Microsoft.Data.Sqlite; +using YMhut.Box.Core.App; + +namespace YMhut.Box.Core.Startup; + +public interface IStartupCheckStore +{ + string DatabasePath { get; } + + Task SaveAsync(StartupCheckReport report, CancellationToken cancellationToken = default); + + Task GetLatestReportAsync(CancellationToken cancellationToken = default); + + Task GetLatestReportAsync(StartupCheckKind kind, CancellationToken cancellationToken = default); + + Task GetReportAsync(Guid id, CancellationToken cancellationToken = default); + + Task> GetHistorySummariesAsync(int take = 30, CancellationToken cancellationToken = default); + + Task> GetHistoryAsync(int take = 30, CancellationToken cancellationToken = default); +} + +public sealed class StartupCheckStore : IStartupCheckStore +{ + private readonly SemaphoreSlim _gate = new(1, 1); + private readonly string _legacyDatabasePath; + private bool _initialized; + + public StartupCheckStore(AppPaths paths) + { + paths.EnsureCreated(); + DatabasePath = AppDatabasePaths.ResolveMainDatabasePath(paths); + _legacyDatabasePath = Path.Combine(paths.Data, "startup-checks.db"); + } + + public string DatabasePath { get; } + + public async Task SaveAsync(StartupCheckReport report, CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + await using var connection = OpenConnection(); + await using var transaction = connection.BeginTransaction(); + + await using (var command = connection.CreateCommand()) + { + command.Transaction = transaction; + command.CommandText = """ + INSERT INTO startup_check_reports( + id, + kind, + started_at, + completed_at, + install_root, + manifest_identity, + install_identity, + is_current_install, + superseded_by_current_install, + check_basis, + issue_count, + critical_issue_count, + warning_issue_count, + visible_issue_count) + VALUES ( + $id, + $kind, + $started_at, + $completed_at, + $install_root, + $manifest_identity, + $install_identity, + $is_current_install, + $superseded_by_current_install, + $check_basis, + $issue_count, + $critical_issue_count, + $warning_issue_count, + $visible_issue_count); + """; + command.Parameters.AddWithValue("$id", report.Id.ToString("N")); + command.Parameters.AddWithValue("$kind", report.Kind.ToString()); + command.Parameters.AddWithValue("$started_at", report.StartedAt.ToString("O", CultureInfo.InvariantCulture)); + command.Parameters.AddWithValue("$completed_at", report.CompletedAt.ToString("O", CultureInfo.InvariantCulture)); + command.Parameters.AddWithValue("$install_root", report.InstallRoot); + command.Parameters.AddWithValue("$manifest_identity", report.ManifestIdentity); + command.Parameters.AddWithValue("$install_identity", report.InstallIdentity); + command.Parameters.AddWithValue("$is_current_install", report.IsCurrentInstall ? 1 : 0); + command.Parameters.AddWithValue("$superseded_by_current_install", report.SupersededByCurrentInstall ? 1 : 0); + command.Parameters.AddWithValue("$check_basis", report.CheckBasis); + command.Parameters.AddWithValue("$issue_count", report.IssueCount); + command.Parameters.AddWithValue("$critical_issue_count", report.CriticalIssueCount); + command.Parameters.AddWithValue("$warning_issue_count", report.WarningIssueCount); + command.Parameters.AddWithValue("$visible_issue_count", report.VisibleIssueCount); + await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + } + + foreach (var item in report.Items) + { + await using var command = connection.CreateCommand(); + command.Transaction = transaction; + command.CommandText = """ + INSERT INTO startup_check_items( + report_id, + item_id, + name, + severity, + status, + detail, + path) + VALUES ( + $report_id, + $item_id, + $name, + $severity, + $status, + $detail, + $path); + """; + command.Parameters.AddWithValue("$report_id", report.Id.ToString("N")); + command.Parameters.AddWithValue("$item_id", item.Id); + command.Parameters.AddWithValue("$name", item.Name); + command.Parameters.AddWithValue("$severity", item.Severity.ToString()); + command.Parameters.AddWithValue("$status", item.Status.ToString()); + command.Parameters.AddWithValue("$detail", item.Detail); + command.Parameters.AddWithValue("$path", (object?)item.Path ?? DBNull.Value); + await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + } + + await transaction.CommitAsync(cancellationToken).ConfigureAwait(false); + } + finally + { + _gate.Release(); + } + } + + public Task GetLatestReportAsync(CancellationToken cancellationToken = default) + => GetLatestCoreAsync(kind: null, cancellationToken); + + public Task GetLatestReportAsync(StartupCheckKind kind, CancellationToken cancellationToken = default) + => GetLatestCoreAsync(kind, cancellationToken); + + public async Task GetReportAsync(Guid id, CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = """ + SELECT id, kind, started_at, completed_at, install_root, manifest_identity, install_identity, is_current_install, superseded_by_current_install, check_basis, issue_count, critical_issue_count, warning_issue_count, visible_issue_count + FROM startup_check_reports + WHERE id = $id + LIMIT 1; + """; + command.Parameters.AddWithValue("$id", id.ToString("N")); + + await using var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false); + if (!await reader.ReadAsync(cancellationToken).ConfigureAwait(false)) + { + return null; + } + + var header = ReadHeader(reader); + return await ReadReportAsync(connection, header, cancellationToken).ConfigureAwait(false); + } + finally + { + _gate.Release(); + } + } + + public async Task> GetHistorySummariesAsync(int take = 30, CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = """ + SELECT reports.id, + reports.kind, + reports.started_at, + reports.completed_at, + reports.install_root, + reports.manifest_identity, + reports.install_identity, + reports.is_current_install, + reports.superseded_by_current_install, + reports.check_basis, + COUNT(items.id) AS item_count, + reports.issue_count, + reports.critical_issue_count, + reports.warning_issue_count, + reports.visible_issue_count + FROM startup_check_reports reports + LEFT JOIN startup_check_items items ON items.report_id = reports.id + GROUP BY reports.id + ORDER BY reports.completed_at DESC + LIMIT $take; + """; + command.Parameters.AddWithValue("$take", Math.Clamp(take, 1, 200)); + + var summaries = new List(); + await using var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false); + while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false)) + { + summaries.Add(ReadSummary(reader)); + } + + return summaries; + } + finally + { + _gate.Release(); + } + } + + public async Task> GetHistoryAsync(int take = 30, CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = """ + SELECT id, kind, started_at, completed_at, install_root, manifest_identity, install_identity, is_current_install, superseded_by_current_install, check_basis, issue_count, critical_issue_count, warning_issue_count, visible_issue_count + FROM startup_check_reports + ORDER BY completed_at DESC + LIMIT $take; + """; + command.Parameters.AddWithValue("$take", Math.Clamp(take, 1, 200)); + + var headers = new List(); + await using var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false); + while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false)) + { + headers.Add(ReadHeader(reader)); + } + + var reports = new List(headers.Count); + foreach (var header in headers) + { + reports.Add(await ReadReportAsync(connection, header, cancellationToken).ConfigureAwait(false)); + } + + return reports; + } + finally + { + _gate.Release(); + } + } + + private async Task GetLatestCoreAsync(StartupCheckKind? kind, CancellationToken cancellationToken) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = kind is null + ? """ + SELECT id, kind, started_at, completed_at, install_root, manifest_identity, install_identity, is_current_install, superseded_by_current_install, check_basis, issue_count, critical_issue_count, warning_issue_count, visible_issue_count + FROM startup_check_reports + ORDER BY completed_at DESC + LIMIT 1; + """ + : """ + SELECT id, kind, started_at, completed_at, install_root, manifest_identity, install_identity, is_current_install, superseded_by_current_install, check_basis, issue_count, critical_issue_count, warning_issue_count, visible_issue_count + FROM startup_check_reports + WHERE kind = $kind + ORDER BY completed_at DESC + LIMIT 1; + """; + if (kind is not null) + { + command.Parameters.AddWithValue("$kind", kind.Value.ToString()); + } + + await using var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false); + if (!await reader.ReadAsync(cancellationToken).ConfigureAwait(false)) + { + return null; + } + + var header = ReadHeader(reader); + return await ReadReportAsync(connection, header, cancellationToken).ConfigureAwait(false); + } + finally + { + _gate.Release(); + } + } + + private static async Task ReadReportAsync(SqliteConnection connection, ReportHeader header, CancellationToken cancellationToken) + { + await using var command = connection.CreateCommand(); + command.CommandText = """ + SELECT item_id, name, severity, status, detail, path + FROM startup_check_items + WHERE report_id = $report_id + ORDER BY id; + """; + command.Parameters.AddWithValue("$report_id", header.Id.ToString("N")); + + var items = new List(); + await using var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false); + while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false)) + { + items.Add(new StartupCheckItem( + reader.GetString(0), + reader.GetString(1), + ParseEnum(reader.GetString(2), StartupCheckSeverity.Info), + ParseEnum(reader.GetString(3), StartupCheckStatus.Skipped), + reader.GetString(4), + reader.IsDBNull(5) ? null : reader.GetString(5))); + } + + return new StartupCheckReport( + header.Id, + header.Kind, + header.StartedAt, + header.CompletedAt, + header.InstallRoot, + header.ManifestIdentity, + items, + header.InstallIdentity, + header.IsCurrentInstall, + header.SupersededByCurrentInstall, + header.CheckBasis); + } + + private static ReportHeader ReadHeader(SqliteDataReader reader) + { + return new ReportHeader( + Guid.TryParseExact(reader.GetString(0), "N", out var id) ? id : Guid.NewGuid(), + ParseEnum(reader.GetString(1), StartupCheckKind.FastPreflight), + ParseDate(reader.GetString(2)), + ParseDate(reader.GetString(3)), + reader.GetString(4), + reader.GetString(5), + reader.IsDBNull(6) ? string.Empty : reader.GetString(6), + ReadBoolean(reader, 7), + ReadBoolean(reader, 8), + reader.IsDBNull(9) ? string.Empty : reader.GetString(9)); + } + + private static StartupCheckReportSummary ReadSummary(SqliteDataReader reader) + { + return new StartupCheckReportSummary( + Guid.TryParseExact(reader.GetString(0), "N", out var id) ? id : Guid.NewGuid(), + ParseEnum(reader.GetString(1), StartupCheckKind.FastPreflight), + ParseDate(reader.GetString(2)), + ParseDate(reader.GetString(3)), + reader.GetString(4), + reader.GetString(5), + ReadInt32(reader, 10), + ReadInt32(reader, 11), + ReadInt32(reader, 12), + ReadInt32(reader, 13), + ReadInt32(reader, 14), + reader.IsDBNull(6) ? string.Empty : reader.GetString(6), + ReadBoolean(reader, 7), + ReadBoolean(reader, 8), + reader.IsDBNull(9) ? string.Empty : reader.GetString(9)); + } + + private static int ReadInt32(SqliteDataReader reader, int ordinal) + => Convert.ToInt32(reader.GetValue(ordinal), CultureInfo.InvariantCulture); + + private static bool ReadBoolean(SqliteDataReader reader, int ordinal) + => !reader.IsDBNull(ordinal) && Convert.ToInt32(reader.GetValue(ordinal), CultureInfo.InvariantCulture) != 0; + + private async Task EnsureInitializedAsync(CancellationToken cancellationToken) + { + if (_initialized) + { + return; + } + + Directory.CreateDirectory(Path.GetDirectoryName(DatabasePath)!); + await using var connection = OpenConnection(); + await using var command = connection.CreateCommand(); + command.CommandText = """ + PRAGMA journal_mode = WAL; + PRAGMA synchronous = NORMAL; + PRAGMA busy_timeout = 5000; + CREATE TABLE IF NOT EXISTS startup_check_reports ( + id TEXT PRIMARY KEY, + kind TEXT NOT NULL, + started_at TEXT NOT NULL, + completed_at TEXT NOT NULL, + install_root TEXT NOT NULL, + manifest_identity TEXT NOT NULL, + install_identity TEXT NOT NULL DEFAULT '', + is_current_install INTEGER NOT NULL DEFAULT 1, + superseded_by_current_install INTEGER NOT NULL DEFAULT 0, + check_basis TEXT NOT NULL DEFAULT '', + issue_count INTEGER NOT NULL, + critical_issue_count INTEGER NOT NULL, + warning_issue_count INTEGER NOT NULL, + visible_issue_count INTEGER NOT NULL DEFAULT 0 + ); + CREATE TABLE IF NOT EXISTS startup_check_items ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + report_id TEXT NOT NULL, + item_id TEXT NOT NULL, + name TEXT NOT NULL, + severity TEXT NOT NULL, + status TEXT NOT NULL, + detail TEXT NOT NULL, + path TEXT NULL, + FOREIGN KEY(report_id) REFERENCES startup_check_reports(id) ON DELETE CASCADE + ); + CREATE INDEX IF NOT EXISTS idx_startup_check_reports_completed ON startup_check_reports(completed_at DESC); + CREATE INDEX IF NOT EXISTS idx_startup_check_reports_kind_completed ON startup_check_reports(kind, completed_at DESC); + CREATE INDEX IF NOT EXISTS idx_startup_check_items_report ON startup_check_items(report_id); + """; + await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + await EnsureColumnAsync(connection, "startup_check_reports", "install_identity", "TEXT NOT NULL DEFAULT ''", cancellationToken).ConfigureAwait(false); + await EnsureColumnAsync(connection, "startup_check_reports", "is_current_install", "INTEGER NOT NULL DEFAULT 1", cancellationToken).ConfigureAwait(false); + await EnsureColumnAsync(connection, "startup_check_reports", "superseded_by_current_install", "INTEGER NOT NULL DEFAULT 0", cancellationToken).ConfigureAwait(false); + await EnsureColumnAsync(connection, "startup_check_reports", "check_basis", "TEXT NOT NULL DEFAULT ''", cancellationToken).ConfigureAwait(false); + await EnsureColumnAsync(connection, "startup_check_reports", "visible_issue_count", "INTEGER NOT NULL DEFAULT 0", cancellationToken).ConfigureAwait(false); + await ImportLegacyDatabaseAsync(connection, cancellationToken).ConfigureAwait(false); + _initialized = true; + } + + private async Task ImportLegacyDatabaseAsync(SqliteConnection connection, CancellationToken cancellationToken) + { + if (!File.Exists(_legacyDatabasePath) || + string.Equals(Path.GetFullPath(_legacyDatabasePath), Path.GetFullPath(DatabasePath), StringComparison.OrdinalIgnoreCase)) + { + return; + } + + try + { + await using (var attach = connection.CreateCommand()) + { + attach.CommandText = "ATTACH DATABASE $path AS legacy_startup;"; + attach.Parameters.AddWithValue("$path", _legacyDatabasePath); + await attach.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + } + + var legacyHasVisibleIssueCount = await LegacyTableHasColumnAsync( + connection, + "startup_check_reports", + "visible_issue_count", + cancellationToken).ConfigureAwait(false); + + await using (var command = connection.CreateCommand()) + { + var visibleIssueExpression = legacyHasVisibleIssueCount ? "visible_issue_count" : "issue_count"; + command.CommandText = $$""" + INSERT OR IGNORE INTO startup_check_reports( + id, + kind, + started_at, + completed_at, + install_root, + manifest_identity, + install_identity, + is_current_install, + superseded_by_current_install, + check_basis, + issue_count, + critical_issue_count, + warning_issue_count, + visible_issue_count) + SELECT id, + kind, + started_at, + completed_at, + install_root, + manifest_identity, + '', + 0, + 1, + 'legacy-import', + issue_count, + critical_issue_count, + warning_issue_count, + {{visibleIssueExpression}} + FROM legacy_startup.startup_check_reports; + + INSERT OR IGNORE INTO startup_check_items( + report_id, + item_id, + name, + severity, + status, + detail, + path) + SELECT report_id, + item_id, + name, + severity, + status, + detail, + path + FROM legacy_startup.startup_check_items; + """; + await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + } + + await using (var detach = connection.CreateCommand()) + { + detach.CommandText = "DETACH DATABASE legacy_startup;"; + await detach.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + } + + DeleteLegacyDatabaseFiles(); + } + catch + { + try + { + await using var detach = connection.CreateCommand(); + detach.CommandText = "DETACH DATABASE legacy_startup;"; + await detach.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + } + catch + { + } + } + } + + private static async Task LegacyTableHasColumnAsync( + SqliteConnection connection, + string table, + string column, + CancellationToken cancellationToken) + { + await using var command = connection.CreateCommand(); + command.CommandText = $"PRAGMA legacy_startup.table_info({table});"; + await using var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false); + while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false)) + { + if (string.Equals(reader.GetString(1), column, StringComparison.OrdinalIgnoreCase)) + { + return true; + } + } + + return false; + } + + private void DeleteLegacyDatabaseFiles() + { + foreach (var path in new[] + { + _legacyDatabasePath, + _legacyDatabasePath + "-wal", + _legacyDatabasePath + "-shm" + }) + { + try + { + if (File.Exists(path)) + { + File.Delete(path); + } + } + catch + { + } + } + } + + private static async Task EnsureColumnAsync( + SqliteConnection connection, + string table, + string column, + string definition, + CancellationToken cancellationToken) + { + await using (var command = connection.CreateCommand()) + { + command.CommandText = $"PRAGMA table_info({table});"; + await using var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false); + while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false)) + { + if (string.Equals(reader.GetString(1), column, StringComparison.OrdinalIgnoreCase)) + { + return; + } + } + } + + await using (var command = connection.CreateCommand()) + { + command.CommandText = $"ALTER TABLE {table} ADD COLUMN {column} {definition};"; + await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + } + } + + private SqliteConnection OpenConnection() + { + var connection = new SqliteConnection($"Data Source={DatabasePath};Pooling=False"); + connection.Open(); + using var command = connection.CreateCommand(); + command.CommandText = """ + PRAGMA busy_timeout = 5000; + PRAGMA synchronous = NORMAL; + """; + command.ExecuteNonQuery(); + return connection; + } + + private static DateTimeOffset ParseDate(string value) + => DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out var parsed) + ? parsed + : DateTimeOffset.Now; + + private static TEnum ParseEnum(string value, TEnum fallback) + where TEnum : struct + => Enum.TryParse(value, ignoreCase: true, out var parsed) ? parsed : fallback; + + private sealed record ReportHeader( + Guid Id, + StartupCheckKind Kind, + DateTimeOffset StartedAt, + DateTimeOffset CompletedAt, + string InstallRoot, + string ManifestIdentity, + string InstallIdentity, + bool IsCurrentInstall, + bool SupersededByCurrentInstall, + string CheckBasis); +} diff --git a/src/YMhut.Box.Core/Startup/StartupInitializationPipeline.cs b/src/YMhut.Box.Core/Startup/StartupInitializationPipeline.cs new file mode 100644 index 0000000..b24e409 --- /dev/null +++ b/src/YMhut.Box.Core/Startup/StartupInitializationPipeline.cs @@ -0,0 +1,152 @@ +namespace YMhut.Box.Core.Startup; + +public sealed record StartupInitializationStage( + string Id, + string DisplayName, + double Weight, + bool Critical, + Func ExecuteAsync); + +public sealed record StartupInitializationStageProgress( + string StageId, + string StageName, + int StepIndex, + int StepCount, + double Progress, + string Detail, + StartupCheckSeverity Severity = StartupCheckSeverity.Info, + bool IsRepairStep = false, + bool IsIndeterminate = false); + +public sealed class StartupInitializationStageContext +{ + private readonly StartupInitializationPipeline _pipeline; + private readonly StartupInitializationStage _stage; + private readonly int _stepIndex; + private readonly int _stepCount; + private readonly double _completedWeight; + private readonly double _totalWeight; + private readonly IProgress? _progress; + + internal StartupInitializationStageContext( + StartupInitializationPipeline pipeline, + StartupInitializationStage stage, + int stepIndex, + int stepCount, + double completedWeight, + double totalWeight, + IProgress? progress) + { + _pipeline = pipeline; + _stage = stage; + _stepIndex = stepIndex; + _stepCount = stepCount; + _completedWeight = completedWeight; + _totalWeight = totalWeight; + _progress = progress; + } + + public void Report( + double stageProgress, + string detail, + StartupCheckSeverity severity = StartupCheckSeverity.Info, + bool isRepairStep = false, + bool isIndeterminate = false) + { + var clampedStageProgress = Math.Clamp(stageProgress, 0, 1); + var absolute = _totalWeight <= 0 + ? 100 + : ((_completedWeight + (_stage.Weight * clampedStageProgress)) / _totalWeight) * 100; + _pipeline.Report( + _progress, + new StartupInitializationStageProgress( + _stage.Id, + _stage.DisplayName, + _stepIndex, + _stepCount, + absolute, + detail, + severity, + isRepairStep, + isIndeterminate)); + } +} + +public sealed class StartupInitializationPipeline +{ + private readonly IReadOnlyList _stages; + private double _lastProgress; + + public StartupInitializationPipeline(IEnumerable stages) + { + _stages = stages + .Where(stage => !string.IsNullOrWhiteSpace(stage.Id)) + .ToArray(); + + if (_stages.Count == 0) + { + throw new ArgumentException("Startup initialization pipeline requires at least one stage.", nameof(stages)); + } + } + + public IReadOnlyList Stages => _stages; + + public async Task RunAsync( + IProgress? progress = null, + CancellationToken cancellationToken = default) + { + _lastProgress = 0; + var totalWeight = _stages.Sum(stage => Math.Max(0.1, stage.Weight)); + var completedWeight = 0d; + + for (var index = 0; index < _stages.Count; index++) + { + cancellationToken.ThrowIfCancellationRequested(); + var stage = _stages[index] with { Weight = Math.Max(0.1, _stages[index].Weight) }; + var context = new StartupInitializationStageContext( + this, + stage, + index + 1, + _stages.Count, + completedWeight, + totalWeight, + progress); + + context.Report(0, stage.DisplayName, isIndeterminate: false); + try + { + await stage.ExecuteAsync(context, cancellationToken).ConfigureAwait(false); + } + catch when (!stage.Critical) + { + context.Report(1, stage.DisplayName, StartupCheckSeverity.Warning); + } + + completedWeight += stage.Weight; + context.Report(1, stage.DisplayName); + } + + if (_lastProgress < 100) + { + var lastStage = _stages[^1]; + Report( + progress, + new StartupInitializationStageProgress( + lastStage.Id, + lastStage.DisplayName, + _stages.Count, + _stages.Count, + 100, + lastStage.DisplayName)); + } + } + + internal void Report( + IProgress? progress, + StartupInitializationStageProgress value) + { + var monotonicProgress = Math.Max(_lastProgress, Math.Clamp(value.Progress, 0, 100)); + _lastProgress = monotonicProgress; + progress?.Report(value with { Progress = monotonicProgress }); + } +} diff --git a/src/YMhut.Box.Core/Startup/StartupSplashMessage.cs b/src/YMhut.Box.Core/Startup/StartupSplashMessage.cs new file mode 100644 index 0000000..42367ff --- /dev/null +++ b/src/YMhut.Box.Core/Startup/StartupSplashMessage.cs @@ -0,0 +1,25 @@ +using System.Text.Json; + +namespace YMhut.Box.Core.Startup; + +public sealed record StartupSplashMessage( + string Type, + string Status, + double Progress = 0, + string StageId = "", + string StageName = "", + int StepIndex = 0, + int StepCount = 0, + string? Detail = null, + string Severity = "info", + bool IsRepairStep = false, + bool IsIndeterminate = false, + bool HasIssues = false, + int VisibleIssueCount = 0, + long DurationMs = 0, + string? Theme = null, + bool ReducedMotion = false) +{ + public string ToJson() + => JsonSerializer.Serialize(this, StartupCheckJson.Options); +} diff --git a/src/YMhut.Box.Core/System/HardwareInfoService.cs b/src/YMhut.Box.Core/System/HardwareInfoService.cs new file mode 100644 index 0000000..d142ce0 --- /dev/null +++ b/src/YMhut.Box.Core/System/HardwareInfoService.cs @@ -0,0 +1,174 @@ +using System.Runtime.Versioning; +using YMhut.Box.Core.Logging; + +namespace YMhut.Box.Core.System; + +public sealed record HardwareInfoSummary( + string OsDescription, + string CpuName, + string GpuName, + string TotalMemory, + string PrimaryDisk, + string BoardName, + string WmiStatus, + DateTimeOffset CapturedAt); + +public interface IHardwareInfoService +{ + Task CaptureAsync(CancellationToken cancellationToken = default); +} + +public sealed class HardwareInfoService(ILogService? logService = null) : IHardwareInfoService +{ + public async Task CaptureAsync(CancellationToken cancellationToken = default) + { + if (!OperatingSystem.IsWindows()) + { + return new HardwareInfoSummary( + global::System.Runtime.InteropServices.RuntimeInformation.OSDescription, + $"{Environment.ProcessorCount} logical processors", + "Unavailable", + "Unavailable", + "Unavailable", + "Unavailable", + "Degraded: WMI is only available on Windows.", + DateTimeOffset.Now); + } + + return await CaptureWindowsAsync(cancellationToken).ConfigureAwait(false); + } + + [SupportedOSPlatform("windows")] + private async Task CaptureWindowsAsync(CancellationToken cancellationToken) + { + return await Task.Run(async () => + { + try + { + var summary = new HardwareInfoSummary( + global::System.Runtime.InteropServices.RuntimeInformation.OSDescription, + FirstWmiValue("Win32_Processor", "Name") ?? $"{Environment.ProcessorCount} logical processors", + FirstWmiValue("Win32_VideoController", "Name") ?? "Unknown GPU", + FormatMemory(FirstWmiUlong("Win32_ComputerSystem", "TotalPhysicalMemory")), + BuildDiskSummary(), + BuildBoardSummary(), + "OK", + DateTimeOffset.Now); + await WriteLogAsync("Information", "wmi", "Hardware WMI query succeeded", $"cpu={summary.CpuName}; gpu={summary.GpuName}", cancellationToken).ConfigureAwait(false); + return summary; + } + catch (Exception exception) + { + await WriteLogAsync("Warning", "wmi", "Hardware WMI query degraded", Sanitize(exception.Message), cancellationToken).ConfigureAwait(false); + return new HardwareInfoSummary( + global::System.Runtime.InteropServices.RuntimeInformation.OSDescription, + $"{Environment.ProcessorCount} logical processors", + "Unavailable", + "Unavailable", + "Unavailable", + "Unavailable", + $"Degraded: {Sanitize(exception.Message)}", + DateTimeOffset.Now); + } + }, cancellationToken).ConfigureAwait(false); + } + + [SupportedOSPlatform("windows")] + private static string? FirstWmiValue(string className, string propertyName) + { + using var searcher = new global::System.Management.ManagementObjectSearcher($"SELECT {propertyName} FROM {className}"); + foreach (global::System.Management.ManagementObject item in searcher.Get()) + { + var value = item[propertyName]?.ToString()?.Trim(); + if (!string.IsNullOrWhiteSpace(value)) + { + return value; + } + } + + return null; + } + + [SupportedOSPlatform("windows")] + private static ulong? FirstWmiUlong(string className, string propertyName) + { + using var searcher = new global::System.Management.ManagementObjectSearcher($"SELECT {propertyName} FROM {className}"); + foreach (global::System.Management.ManagementObject item in searcher.Get()) + { + if (ulong.TryParse(item[propertyName]?.ToString(), out var value)) + { + return value; + } + } + + return null; + } + + [SupportedOSPlatform("windows")] + private static string BuildDiskSummary() + { + using var searcher = new global::System.Management.ManagementObjectSearcher("SELECT Model, Size FROM Win32_DiskDrive"); + var parts = new List(); + foreach (global::System.Management.ManagementObject item in searcher.Get()) + { + var model = item["Model"]?.ToString()?.Trim(); + var size = ulong.TryParse(item["Size"]?.ToString(), out var bytes) ? FormatMemory(bytes) : "Unknown size"; + if (!string.IsNullOrWhiteSpace(model)) + { + parts.Add($"{model} ({size})"); + } + } + + return parts.Count == 0 ? "Unknown disk" : string.Join("; ", parts.Take(3)); + } + + [SupportedOSPlatform("windows")] + private static string BuildBoardSummary() + { + using var searcher = new global::System.Management.ManagementObjectSearcher("SELECT Manufacturer, Product FROM Win32_BaseBoard"); + foreach (global::System.Management.ManagementObject item in searcher.Get()) + { + var manufacturer = item["Manufacturer"]?.ToString()?.Trim(); + var product = item["Product"]?.ToString()?.Trim(); + var text = string.Join(" ", new[] { manufacturer, product }.Where(value => !string.IsNullOrWhiteSpace(value))); + if (!string.IsNullOrWhiteSpace(text)) + { + return text; + } + } + + return "Unknown board"; + } + + private static string FormatMemory(ulong? bytes) + { + if (bytes is null or 0) + { + return "Unknown"; + } + + var value = bytes.Value; + var units = new[] { "B", "KB", "MB", "GB", "TB" }; + var index = 0; + var size = (double)value; + while (size >= 1024 && index < units.Length - 1) + { + size /= 1024; + index++; + } + + return $"{size:0.##} {units[index]}"; + } + + private Task WriteLogAsync(string level, string category, string message, string? detail, CancellationToken cancellationToken) + { + return logService?.WriteAsync(level, category, message, detail, cancellationToken) ?? Task.CompletedTask; + } + + private static string Sanitize(string message) + { + return string.IsNullOrWhiteSpace(message) + ? string.Empty + : message.Length > 260 ? message[..260] : message; + } +} diff --git a/src/YMhut.Box.Core/System/SystemMetricsService.cs b/src/YMhut.Box.Core/System/SystemMetricsService.cs new file mode 100644 index 0000000..4d27d5a --- /dev/null +++ b/src/YMhut.Box.Core/System/SystemMetricsService.cs @@ -0,0 +1,257 @@ +using System.Diagnostics; +using System.IO; +using System.Runtime.InteropServices; + +namespace YMhut.Box.Core.System; + +public sealed record SystemMetricsSnapshot( + string MachineName, + string OsDescription, + int ProcessorCount, + long WorkingSetBytes, + long ManagedMemoryBytes, + double? CpuUsagePercent, + ulong? TotalMemoryBytes, + ulong? AvailableMemoryBytes, + ulong? UsedMemoryBytes, + double? MemoryUsagePercent, + double? ProcessCpuUsagePercent, + TimeSpan ProcessUptime, + int ProcessThreadCount, + int ProcessHandleCount, + ulong? SystemDriveTotalBytes, + ulong? SystemDriveAvailableBytes, + ulong? SystemDriveUsedBytes, + double? SystemDriveUsagePercent, + bool Is64BitProcess, + string RuntimeDescription, + DateTimeOffset CapturedAt); + +public interface ISystemMetricsService +{ + SystemMetricsSnapshot Capture(); +} + +public sealed class SystemMetricsService : ISystemMetricsService +{ + private readonly object _cpuLock = new(); + private CpuSample? _lastCpuSample; + private readonly object _processCpuLock = new(); + private ProcessCpuSample? _lastProcessCpuSample; + + public SystemMetricsSnapshot Capture() + { + using var process = Process.GetCurrentProcess(); + var capturedAt = DateTimeOffset.Now; + var cpuUsage = CaptureCpuUsage(); + var processCpuUsage = CaptureProcessCpuUsage(process, capturedAt); + var memory = CaptureMemoryStatus(); + var drive = CaptureSystemDriveStatus(); + var processUptime = SafeProcessUptime(process, capturedAt); + return new SystemMetricsSnapshot( + Environment.MachineName, + global::System.Runtime.InteropServices.RuntimeInformation.OSDescription, + Environment.ProcessorCount, + Environment.WorkingSet, + GC.GetTotalMemory(false), + cpuUsage, + memory.Total, + memory.Available, + memory.Used, + memory.UsagePercent, + processCpuUsage, + processUptime, + SafeThreadCount(process), + SafeHandleCount(process), + drive.Total, + drive.Available, + drive.Used, + drive.UsagePercent, + Environment.Is64BitProcess, + global::System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription, + capturedAt); + } + + private double? CaptureCpuUsage() + { + if (!GetSystemTimes(out var idleTime, out var kernelTime, out var userTime)) + { + return null; + } + + var sample = new CpuSample(ToUInt64(idleTime), ToUInt64(kernelTime), ToUInt64(userTime)); + lock (_cpuLock) + { + if (_lastCpuSample is not { } previous) + { + _lastCpuSample = sample; + return null; + } + + _lastCpuSample = sample; + var idleDelta = sample.Idle - previous.Idle; + var kernelDelta = sample.Kernel - previous.Kernel; + var userDelta = sample.User - previous.User; + var totalDelta = kernelDelta + userDelta; + if (totalDelta == 0 || idleDelta > totalDelta) + { + return null; + } + + return Math.Clamp((double)(totalDelta - idleDelta) / totalDelta * 100, 0, 100); + } + } + + private static MemoryStatus CaptureMemoryStatus() + { + var status = new MemoryStatusEx(); + status.dwLength = (uint)Marshal.SizeOf(); + if (!GlobalMemoryStatusEx(ref status) || status.ullTotalPhys == 0) + { + return new MemoryStatus(null, null, null, null); + } + + var total = status.ullTotalPhys; + var available = Math.Min(status.ullAvailPhys, total); + var used = total - available; + var percent = Math.Clamp((double)used / total * 100, 0, 100); + return new MemoryStatus(total, available, used, percent); + } + + private double? CaptureProcessCpuUsage(Process process, DateTimeOffset capturedAt) + { + TimeSpan totalProcessorTime; + try + { + totalProcessorTime = process.TotalProcessorTime; + } + catch + { + return null; + } + + var sample = new ProcessCpuSample(totalProcessorTime, capturedAt); + lock (_processCpuLock) + { + if (_lastProcessCpuSample is not { } previous) + { + _lastProcessCpuSample = sample; + return null; + } + + _lastProcessCpuSample = sample; + var elapsed = sample.CapturedAt - previous.CapturedAt; + var cpuDelta = sample.TotalProcessorTime - previous.TotalProcessorTime; + if (elapsed.TotalMilliseconds <= 0 || cpuDelta.TotalMilliseconds < 0) + { + return null; + } + + return Math.Clamp(cpuDelta.TotalMilliseconds / (elapsed.TotalMilliseconds * Math.Max(1, Environment.ProcessorCount)) * 100, 0, 100); + } + } + + private static DriveStatus CaptureSystemDriveStatus() + { + try + { + var root = Path.GetPathRoot(Environment.SystemDirectory); + if (string.IsNullOrWhiteSpace(root)) + { + return new DriveStatus(null, null, null, null); + } + + var drive = new DriveInfo(root); + if (!drive.IsReady || drive.TotalSize <= 0) + { + return new DriveStatus(null, null, null, null); + } + + var total = (ulong)drive.TotalSize; + var available = (ulong)Math.Clamp(drive.AvailableFreeSpace, 0, drive.TotalSize); + var used = total - available; + var percent = Math.Clamp((double)used / total * 100, 0, 100); + return new DriveStatus(total, available, used, percent); + } + catch + { + return new DriveStatus(null, null, null, null); + } + } + + private static TimeSpan SafeProcessUptime(Process process, DateTimeOffset capturedAt) + { + try + { + return capturedAt - new DateTimeOffset(process.StartTime); + } + catch + { + return TimeSpan.Zero; + } + } + + private static int SafeThreadCount(Process process) + { + try + { + return process.Threads.Count; + } + catch + { + return 0; + } + } + + private static int SafeHandleCount(Process process) + { + try + { + return process.HandleCount; + } + catch + { + return 0; + } + } + + private static ulong ToUInt64(FileTime value) + { + return ((ulong)(uint)value.HighDateTime << 32) | (uint)value.LowDateTime; + } + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool GetSystemTimes(out FileTime idleTime, out FileTime kernelTime, out FileTime userTime); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool GlobalMemoryStatusEx(ref MemoryStatusEx buffer); + + [StructLayout(LayoutKind.Sequential)] + private struct FileTime + { + public int LowDateTime; + public int HighDateTime; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] + private struct MemoryStatusEx + { + public uint dwLength; + public uint dwMemoryLoad; + public ulong ullTotalPhys; + public ulong ullAvailPhys; + public ulong ullTotalPageFile; + public ulong ullAvailPageFile; + public ulong ullTotalVirtual; + public ulong ullAvailVirtual; + public ulong ullAvailExtendedVirtual; + } + + private sealed record CpuSample(ulong Idle, ulong Kernel, ulong User); + + private sealed record MemoryStatus(ulong? Total, ulong? Available, ulong? Used, double? UsagePercent); + + private sealed record ProcessCpuSample(TimeSpan TotalProcessorTime, DateTimeOffset CapturedAt); + + private sealed record DriveStatus(ulong? Total, ulong? Available, ulong? Used, double? UsagePercent); +} diff --git a/src/YMhut.Box.Core/Tools/BuiltinReferenceToolService.cs b/src/YMhut.Box.Core/Tools/BuiltinReferenceToolService.cs new file mode 100644 index 0000000..0e4adef --- /dev/null +++ b/src/YMhut.Box.Core/Tools/BuiltinReferenceToolService.cs @@ -0,0 +1,309 @@ +using System.Diagnostics; +using System.Runtime.InteropServices; +using System.Text; +using YMhut.Box.Core.Logging; +using YMhut.Box.Core.Settings; +using YMhut.Box.Core.System; + +namespace YMhut.Box.Core.Tools; + +public interface IBuiltinReferenceToolCatalog +{ + IReadOnlyList GetModules(); + + BuiltinReferenceToolDefinition? GetByModuleId(string moduleId); +} + +public interface IBuiltinReferenceToolService +{ + Task ExecuteAsync( + BuiltinReferenceToolDefinition tool, + bool confirmed = false, + CancellationToken cancellationToken = default); +} + +public sealed class BuiltinReferenceToolCatalog : IBuiltinReferenceToolCatalog +{ + private readonly Lazy> _modules = new(() => Definitions + .Select(definition => new BuiltinReferenceToolModule(definition)) + .OrderBy(module => module.Metadata.Category) + .ThenBy(module => module.Metadata.Name, StringComparer.CurrentCulture) + .ToArray()); + + public IReadOnlyList GetModules() => _modules.Value; + + public BuiltinReferenceToolDefinition? GetByModuleId(string moduleId) + { + var id = moduleId.StartsWith(BuiltinReferenceToolModule.IdPrefix, StringComparison.OrdinalIgnoreCase) + ? moduleId[BuiltinReferenceToolModule.IdPrefix.Length..] + : moduleId; + return Definitions.FirstOrDefault(definition => string.Equals(definition.Id, id, StringComparison.OrdinalIgnoreCase)); + } + + private static readonly BuiltinReferenceToolDefinition[] Definitions = + [ + Tool("cert-block", "证书拦截", "管理证书拦截相关入口,执行前要求确认证书写入或删除风险。", ToolCategory.Security, "\uE72E", BuiltinReferenceToolKind.Command, ReferenceToolRiskLevel.High, "打开证书管理", ["cert", "certificate", "block"]), + Tool("port-viewer", "端口占用", "查看本机 TCP/UDP 端口占用与进程提示。", ToolCategory.Network, "\uE774", BuiltinReferenceToolKind.Command, ReferenceToolRiskLevel.Low, "查看端口占用", ["port", "netstat", "process"]), + Tool("hosts-editor", "Hosts 编辑", "打开系统 Hosts 文件位置,修改前需要管理员权限和二次确认。", ToolCategory.Network, "\uE779", BuiltinReferenceToolKind.SystemEntry, ReferenceToolRiskLevel.High, "打开 Hosts 文件", ["hosts", "dns", "network"]), + Tool("keyboard-test", "键盘测试", "打开 YMhut Box 键盘按键测试入口。", ToolCategory.System, "\uE765", BuiltinReferenceToolKind.Information, ReferenceToolRiskLevel.None, "打开测试说明", ["keyboard", "test"]), + Tool("junk-cleaner", "垃圾清理", "扫描并清理临时文件入口,删除文件前必须再次确认。", ToolCategory.System, "\uE74D", BuiltinReferenceToolKind.Command, ReferenceToolRiskLevel.High, "打开临时目录", ["cleanup", "temp", "delete"]), + Tool("bsod-analysis", "蓝屏分析", "打开 Minidump 目录并提示使用 WinDbg 或事件查看器分析。", ToolCategory.System, "\uE7BA", BuiltinReferenceToolKind.SystemEntry, ReferenceToolRiskLevel.Low, "打开蓝屏转储目录", ["bsod", "dump", "windbg"]), + Tool("winget-installer", "Winget 安装", "打开 Winget 包管理入口,安装软件前写入日志并提示确认。", ToolCategory.System, "\uE896", BuiltinReferenceToolKind.Command, ReferenceToolRiskLevel.Medium, "检查 Winget", ["winget", "install", "package"]), + Tool("battery-report", "电池报告", "生成 Windows battery-report.html 并打开报告。", ToolCategory.System, "\uEBAA", BuiltinReferenceToolKind.Command, ReferenceToolRiskLevel.Low, "生成电池报告", ["battery", "powercfg", "report"]), + Tool("speed-test", "网速测试", "打开系统网络状态与测速说明。", ToolCategory.Network, "\uE968", BuiltinReferenceToolKind.SystemEntry, ReferenceToolRiskLevel.None, "打开网络状态", ["speed", "network", "test"]), + Tool("wifi-password", "WiFi 密码", "查看已保存 WiFi 配置需用户确认,敏感输出不会写入日志。", ToolCategory.Security, "\uE701", BuiltinReferenceToolKind.Command, ReferenceToolRiskLevel.High, "列出 WiFi 配置", ["wifi", "password", "wlan"]), + Tool("disk-space-analyzer", "磁盘分析", "打开 Windows 存储设置并展示磁盘空间分析入口。", ToolCategory.System, "\uEDA2", BuiltinReferenceToolKind.SystemEntry, ReferenceToolRiskLevel.None, "打开存储设置", ["disk", "storage", "space"]), + Tool("lite-monitor", "硬件监控", "通过 WMI/System.Management 读取硬件摘要,失败时降级展示基础信息。", ToolCategory.System, "\uE9D9", BuiltinReferenceToolKind.Information, ReferenceToolRiskLevel.None, "重新加载硬件摘要", ["hardware", "monitor", "wmi"]), + Tool("windows-activation", "Windows 激活状态", "仅提供合规入口:查看激活状态、打开系统激活设置和企业 KMS 配置说明。", ToolCategory.System, "\uE895", BuiltinReferenceToolKind.SystemEntry, ReferenceToolRiskLevel.None, "查看激活状态", ["activation", "license", "kms"]), + Tool("defender-control", "Defender 控制", "打开 Windows 安全中心;修改 Defender 前必须由用户在系统界面确认。", ToolCategory.Security, "\uE72E", BuiltinReferenceToolKind.SystemEntry, ReferenceToolRiskLevel.High, "打开 Windows 安全中心", ["defender", "security"]), + Tool("cpu-ranking", "CPU 天梯图", "打开随包 Metadata/cpu-ranking.json 数据位置和说明。", ToolCategory.System, "\uEEA1", BuiltinReferenceToolKind.Information, ReferenceToolRiskLevel.None, "查看 CPU 数据", ["cpu", "ranking", "hardware"]), + Tool("gpu-ranking", "GPU 天梯图", "打开随包 Metadata/gpu-ranking.json 数据位置和说明。", ToolCategory.System, "\uE9D5", BuiltinReferenceToolKind.Information, ReferenceToolRiskLevel.None, "查看 GPU 数据", ["gpu", "ranking", "hardware"]), + Tool("context-menu-mgr", "右键菜单管理", "打开注册表相关入口,修改右键菜单前必须二次确认。", ToolCategory.System, "\uE8B7", BuiltinReferenceToolKind.SystemEntry, ReferenceToolRiskLevel.High, "打开注册表编辑器", ["context", "menu", "registry"]) + ]; + + private static BuiltinReferenceToolDefinition Tool( + string id, + string name, + string description, + ToolCategory category, + string glyph, + BuiltinReferenceToolKind kind, + ReferenceToolRiskLevel risk, + string primaryAction, + IReadOnlyList keywords) + { + return new BuiltinReferenceToolDefinition(id, name, description, category, glyph, kind, risk, keywords, primaryAction, []); + } +} + +public sealed class BuiltinReferenceToolService( + ILogService? logService = null, + ISettingsService? settingsService = null, + IHardwareInfoService? hardwareInfoService = null) : IBuiltinReferenceToolService +{ + public async Task ExecuteAsync( + BuiltinReferenceToolDefinition tool, + bool confirmed = false, + CancellationToken cancellationToken = default) + { + var start = Stopwatch.StartNew(); + if (tool.IsRisky && !confirmed) + { + await WriteLogAsync("Warning", tool.Id, "execute", "confirmation-required", start.ElapsedMilliseconds, null, null, cancellationToken).ConfigureAwait(false); + return ReferenceToolExecutionResult.ConfirmationRequired("此功能需要二次确认后才会执行。", tool.Description); + } + + try + { + var result = tool.Id switch + { + "battery-report" => await GenerateBatteryReportAsync(cancellationToken).ConfigureAwait(false), + "port-viewer" => await RunCommandCaptureAsync(tool.Id, "cmd.exe", "/c netstat -ano | more", sensitive: false, cancellationToken).ConfigureAwait(false), + "winget-installer" => await RunCommandCaptureAsync(tool.Id, "winget.exe", "--version", sensitive: false, cancellationToken).ConfigureAwait(false), + "wifi-password" => await RunCommandCaptureAsync(tool.Id, "netsh.exe", "wlan show profiles", sensitive: true, cancellationToken).ConfigureAwait(false), + "lite-monitor" => await CaptureHardwareSummaryAsync(cancellationToken).ConfigureAwait(false), + "windows-activation" => await RunCommandCaptureAsync(tool.Id, "cscript.exe", "//Nologo %windir%\\system32\\slmgr.vbs /xpr", sensitive: false, cancellationToken).ConfigureAwait(false), + "hosts-editor" => OpenPath(GetHostsPath()), + "junk-cleaner" => OpenPath(Path.GetTempPath()), + "bsod-analysis" => OpenPath(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Minidump")), + "speed-test" => OpenUri("ms-settings:network-status"), + "disk-space-analyzer" => OpenUri("ms-settings:storagesense"), + "defender-control" => OpenUri("windowsdefender:"), + "context-menu-mgr" => OpenProcess("regedit.exe", string.Empty, true), + "cert-block" => OpenProcess("certmgr.msc", string.Empty, false), + "cpu-ranking" => OpenMetadata("cpu-ranking.json"), + "gpu-ranking" => OpenMetadata("gpu-ranking.json"), + "keyboard-test" => ReferenceToolExecutionResult.Ok("键盘测试入口已接入。", "请在 YMhut Box 后续键盘测试子页中按键验证;本次打开已记录日志。"), + _ => ReferenceToolExecutionResult.Fail("暂不支持该内置工具。", tool.Id) + }; + + if (settingsService is not null && result.Success) + { + await settingsService.RecordRecentToolAsync(BuiltinReferenceToolModule.IdPrefix + tool.Id, cancellationToken).ConfigureAwait(false); + } + + await WriteLogAsync(result.Success ? "Information" : result.Cancelled ? "Warning" : "Error", tool.Id, "execute", result.Success ? "success" : result.Cancelled ? "cancelled" : "failed", start.ElapsedMilliseconds, result.Success ? null : result.Message, result.Detail, cancellationToken).ConfigureAwait(false); + return result; + } + catch (Exception exception) + { + await WriteLogAsync("Error", tool.Id, "execute", "failed", start.ElapsedMilliseconds, Sanitize(exception.Message), null, cancellationToken).ConfigureAwait(false); + return ReferenceToolExecutionResult.Fail("内置工具执行失败。", Sanitize(exception.Message)); + } + } + + private async Task GenerateBatteryReportAsync(CancellationToken cancellationToken) + { + var output = Path.Combine(Path.GetTempPath(), $"ymhut-battery-report-{DateTime.Now:yyyyMMdd-HHmmss}.html"); + var result = await RunProcessAsync("powercfg.exe", $"/batteryreport /output \"{output}\"", sensitive: false, cancellationToken).ConfigureAwait(false); + if (result.ExitCode == 0 && File.Exists(output)) + { + Process.Start(new ProcessStartInfo { FileName = output, UseShellExecute = true }); + return ReferenceToolExecutionResult.Ok("电池报告已生成并打开。", "报告路径已脱敏记录。", output, result.ExitCode); + } + + return ReferenceToolExecutionResult.Fail("生成电池报告失败。", result.Output, result.ExitCode); + } + + private async Task RunCommandCaptureAsync(string toolId, string fileName, string arguments, bool sensitive, CancellationToken cancellationToken) + { + var result = await RunProcessAsync(fileName, Environment.ExpandEnvironmentVariables(arguments), sensitive, cancellationToken).ConfigureAwait(false); + if (result.ExitCode == 0) + { + var detail = sensitive ? "命令执行成功;敏感输出未写入日志。" : Trim(result.Output, 2000); + return ReferenceToolExecutionResult.Ok("命令执行完成。", detail, exitCode: result.ExitCode); + } + + return ReferenceToolExecutionResult.Fail("命令执行失败。", sensitive ? "敏感输出未写入日志。" : Trim(result.Output, 2000), result.ExitCode); + } + + private async Task CaptureHardwareSummaryAsync(CancellationToken cancellationToken) + { + if (hardwareInfoService is null) + { + return ReferenceToolExecutionResult.Fail("硬件信息服务不可用。"); + } + + var summary = await hardwareInfoService.CaptureAsync(cancellationToken).ConfigureAwait(false); + var text = new StringBuilder() + .AppendLine($"OS: {summary.OsDescription}") + .AppendLine($"CPU: {summary.CpuName}") + .AppendLine($"GPU: {summary.GpuName}") + .AppendLine($"Memory: {summary.TotalMemory}") + .AppendLine($"Disk: {summary.PrimaryDisk}") + .AppendLine($"WMI: {summary.WmiStatus}") + .ToString(); + return ReferenceToolExecutionResult.Ok("硬件摘要已重新加载。", text); + } + + private static ReferenceToolExecutionResult OpenMetadata(string fileName) + { + var path = ResolveMetadataPath(fileName); + if (File.Exists(path)) + { + Process.Start(new ProcessStartInfo { FileName = path, UseShellExecute = true }); + return ReferenceToolExecutionResult.Ok("数据文件已打开。", Path.GetFileName(path), path); + } + + return ReferenceToolExecutionResult.Fail("未找到随包数据文件。", fileName); + } + + private static ReferenceToolExecutionResult OpenPath(string path) + { + if (!File.Exists(path) && !Directory.Exists(path)) + { + return ReferenceToolExecutionResult.Fail("目标路径不存在。", path); + } + + Process.Start(new ProcessStartInfo { FileName = path, UseShellExecute = true }); + return ReferenceToolExecutionResult.Ok("系统入口已打开。", path); + } + + private static ReferenceToolExecutionResult OpenUri(string uri) + { + Process.Start(new ProcessStartInfo { FileName = uri, UseShellExecute = true }); + return ReferenceToolExecutionResult.Ok("系统入口已打开。", uri); + } + + private static ReferenceToolExecutionResult OpenProcess(string fileName, string arguments, bool runAsAdmin) + { + Process.Start(new ProcessStartInfo + { + FileName = fileName, + Arguments = arguments, + UseShellExecute = true, + Verb = runAsAdmin ? "runas" : string.Empty + }); + return ReferenceToolExecutionResult.Ok("系统工具已打开。", fileName); + } + + private static async Task<(int ExitCode, string Output)> RunProcessAsync(string fileName, string arguments, bool sensitive, CancellationToken cancellationToken) + { + var process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = fileName, + Arguments = arguments, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + CreateNoWindow = true, + StandardOutputEncoding = Encoding.UTF8, + StandardErrorEncoding = Encoding.UTF8 + } + }; + + process.Start(); + var stdout = await process.StandardOutput.ReadToEndAsync(cancellationToken).ConfigureAwait(false); + var stderr = await process.StandardError.ReadToEndAsync(cancellationToken).ConfigureAwait(false); + await process.WaitForExitAsync(cancellationToken).ConfigureAwait(false); + var output = sensitive ? string.Empty : string.Join(Environment.NewLine, [stdout, stderr]).Trim(); + return (process.ExitCode, output); + } + + private static string GetHostsPath() + { + return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers", "etc", "hosts"); + } + + private static string ResolveMetadataPath(string fileName) + { + var directory = new DirectoryInfo(AppContext.BaseDirectory); + while (directory is not null) + { + var candidate = Path.Combine(directory.FullName, "Metadata", fileName); + if (File.Exists(candidate)) + { + return candidate; + } + + candidate = Path.Combine(directory.FullName, "tubatool-参考补充项目", "Metadata", fileName); + if (File.Exists(candidate)) + { + return candidate; + } + + directory = directory.Parent; + } + + return Path.Combine(AppContext.BaseDirectory, "Metadata", fileName); + } + + private Task WriteLogAsync( + string level, + string toolId, + string operation, + string result, + long elapsedMs, + string? error, + string? detail, + CancellationToken cancellationToken) + { + var payload = $"toolId={BuiltinReferenceToolModule.IdPrefix}{toolId}; operation={operation}; result={result}; elapsedMs={elapsedMs}; error={error ?? ""}; detail={Sanitize(detail ?? "")}"; + return logService?.WriteAsync(level, "builtin-tool", $"Builtin tool {result}: {toolId}", payload, cancellationToken) ?? Task.CompletedTask; + } + + private static string Trim(string? value, int length) + { + if (string.IsNullOrWhiteSpace(value)) + { + return string.Empty; + } + + return value.Length > length ? value[..length] : value; + } + + private static string Sanitize(string value) + { + if (string.IsNullOrWhiteSpace(value)) + { + return string.Empty; + } + + var sanitized = value + .Replace(Environment.UserName, "%USER%", StringComparison.OrdinalIgnoreCase) + .Replace(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "%USERPROFILE%", StringComparison.OrdinalIgnoreCase); + return sanitized.Length > 1200 ? sanitized[..1200] : sanitized; + } +} diff --git a/src/YMhut.Box.Core/Tools/ExternalToolCatalogService.cs b/src/YMhut.Box.Core/Tools/ExternalToolCatalogService.cs new file mode 100644 index 0000000..82124a2 --- /dev/null +++ b/src/YMhut.Box.Core/Tools/ExternalToolCatalogService.cs @@ -0,0 +1,757 @@ +using System.Diagnostics; +using System.Drawing; +using System.Runtime.Versioning; +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using YMhut.Box.Core.App; +using YMhut.Box.Core.Logging; + +namespace YMhut.Box.Core.Tools; + +public interface IExternalToolCatalogService +{ + Task> GetModulesAsync(CancellationToken cancellationToken = default); + + string ResolveToolsRoot(); +} + +public sealed class ExternalToolCatalogService(AppPaths paths, ILogService? logService = null) : IExternalToolCatalogService +{ + private static readonly string[] LaunchableExtensions = [".exe", ".bat", ".cmd", ".lnk", ".msc", ".ps1", ".vbs"]; + private static readonly string[] ArchSuffixes = ["64", "32", "x64", "x86", "_x64", "_x86", "_64", "_32", "w64", "w32", "_Win64", "_Win32", "ARM64", "_ARM64", "_arm64"]; + private static readonly string[] X64Patterns = ["x64", "_x64", "w64", "_Win64"]; + private static readonly string[] X86Patterns = ["x86", "_x86", "32", "_32", "w32", "_Win32"]; + private static readonly string[] Arm64Patterns = ["ARM64", "_ARM64", "arm64", "_arm64"]; + private readonly SemaphoreSlim _gate = new(1, 1); + private IReadOnlyList? _cached; + + public async Task> GetModulesAsync(CancellationToken cancellationToken = default) + { + if (_cached is not null) + { + return _cached; + } + + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + if (_cached is not null) + { + return _cached; + } + + var start = Stopwatch.StartNew(); + var root = ResolveToolsRoot(); + if (!Directory.Exists(root)) + { + await WriteLogAsync("Warning", "tool-scan", "External Tools directory not found", root, cancellationToken).ConfigureAwait(false); + _cached = []; + return _cached; + } + + var metadata = ToolMetadataDatabase.Load(ResolveMetadataRoot(root), root, logService); + var modules = Directory.EnumerateDirectories(root) + .SelectMany(categoryRoot => ScanCategory(root, categoryRoot, metadata, cancellationToken)) + .GroupBy(module => module.Id, StringComparer.OrdinalIgnoreCase) + .Select(group => group.First()) + .OrderBy(module => CategorySortIndex(module.Metadata.Category)) + .ThenBy(module => module.Tool.CategoryName, StringComparer.CurrentCultureIgnoreCase) + .ThenBy(module => module.Tool.Name, StringComparer.CurrentCultureIgnoreCase) + .ToArray(); + + _cached = modules; + start.Stop(); + await WriteLogAsync("Information", "tool-scan", "External Tools scanned", $"root={root}; count={modules.Length}; elapsedMs={start.ElapsedMilliseconds}", cancellationToken).ConfigureAwait(false); + return _cached; + } + catch (Exception exception) + { + await WriteLogAsync("Error", "tool-scan", "External Tools scan failed", Sanitize(exception.Message), cancellationToken).ConfigureAwait(false); + _cached = []; + return _cached; + } + finally + { + _gate.Release(); + } + } + + public string ResolveToolsRoot() + { + foreach (var candidate in CandidateRoots("Tools")) + { + if (Directory.Exists(candidate)) + { + return candidate; + } + } + + return Path.Combine(AppContext.BaseDirectory, "Tools"); + } + + private IEnumerable ScanCategory(string toolsRoot, string categoryRoot, ToolMetadataDatabase metadata, CancellationToken cancellationToken) + { + cancellationToken.ThrowIfCancellationRequested(); + var categoryName = Path.GetFileName(categoryRoot); + if (string.IsNullOrWhiteSpace(categoryName)) + { + yield break; + } + + var toolDirs = Directory.EnumerateDirectories(categoryRoot).ToList(); + foreach (var toolDir in MergeArchDirectories(toolDirs)) + { + cancellationToken.ThrowIfCancellationRequested(); + ExternalToolModule? module = null; + try + { + module = CreateModule(toolsRoot, categoryName, categoryRoot, toolDir, metadata); + } + catch (Exception exception) + { + _ = WriteLogAsync("Warning", "tool-scan", "External tool skipped", $"{toolDir}: {Sanitize(exception.Message)}", CancellationToken.None); + } + + if (module is not null) + { + yield return module; + } + } + } + + private ExternalToolModule? CreateModule(string toolsRoot, string categoryName, string categoryRoot, string toolDir, ToolMetadataDatabase metadata) + { + var launchable = FindPrimaryLaunchable(toolDir, metadata.GetLaunchTarget(toolsRoot, toolDir)); + if (launchable is null) + { + return null; + } + + var relative = Path.GetRelativePath(toolsRoot, launchable); + var extension = Path.GetExtension(launchable).TrimStart('.').ToUpperInvariant(); + var info = metadata.GetMetadata(toolsRoot, launchable, toolDir); + var rawName = GetDisplayName(launchable); + var name = CleanupName(StripArchSuffix(rawName)); + if (string.IsNullOrWhiteSpace(name)) + { + name = CleanupName(rawName); + } + + var arch = FormatArchDisplay(DetectArch(Path.GetFileNameWithoutExtension(launchable))); + var variants = FindAllArchVariants(categoryRoot, toolDir, launchable, metadata); + var risk = DetermineRisk(launchable); + var description = FirstUseful( + info.Description, + ReadFolderDescription(toolDir), + $"{categoryName} 外部工具,来自 YMhut Box 随包 Tools 目录。") + ?? $"{categoryName} 外部工具,来自 YMhut Box 随包 Tools 目录。"; + var item = new ExternalToolItem( + ExternalToolModule.IdPrefix + Hash(relative), + name, + description, + categoryName, + MapCategory(categoryName), + toolDir, + launchable, + relative, + string.IsNullOrWhiteSpace(extension) ? "FILE" : extension, + info.Publisher, + info.Version, + GetIconGlyph(launchable), + GetCachedOrExtractIcon(launchable), + string.IsNullOrWhiteSpace(arch) ? null : arch, + (info.Tags ?? []).Concat([categoryName, extension]).Where(tag => !string.IsNullOrWhiteSpace(tag)).Distinct(StringComparer.OrdinalIgnoreCase).ToArray(), + variants, + risk, + risk == ReferenceToolRiskLevel.High || LooksAdministrative(launchable)); + + return new ExternalToolModule(item); + } + + private List FindAllArchVariants(string categoryRoot, string toolDir, string primaryPath, ToolMetadataDatabase metadata) + { + var variants = new List(); + var allLaunchables = Directory.EnumerateFiles(toolDir, "*", SearchOption.AllDirectories) + .Where(IsLaunchable) + .ToArray(); + foreach (var file in allLaunchables) + { + if (string.Equals(file, primaryPath, StringComparison.OrdinalIgnoreCase)) + { + continue; + } + + var arch = FormatArchDisplay(DetectArch(Path.GetFileNameWithoutExtension(file))); + if (string.IsNullOrWhiteSpace(arch)) + { + continue; + } + + variants.Add(new ExternalToolVariant(CleanupName(StripArchSuffix(Path.GetFileNameWithoutExtension(file))), file, arch)); + } + + foreach (var variant in metadata.GetArchVariants(toolDir)) + { + var variantPath = ResolveVariantPath(categoryRoot, toolDir, variant); + if (variantPath is null || + string.Equals(variantPath, primaryPath, StringComparison.OrdinalIgnoreCase) || + variants.Any(existing => string.Equals(existing.Path, variantPath, StringComparison.OrdinalIgnoreCase))) + { + continue; + } + + variants.Add(new ExternalToolVariant( + CleanupName(StripArchSuffix(Path.GetFileNameWithoutExtension(variantPath))), + variantPath, + variant.Arch)); + } + + return variants + .OrderBy(variant => variant.Architecture, StringComparer.OrdinalIgnoreCase) + .ThenBy(variant => variant.Name, StringComparer.CurrentCultureIgnoreCase) + .ToList(); + } + + private static string? ResolveVariantPath(string categoryRoot, string toolDir, JsonArchVariantResult variant) + { + if (!string.IsNullOrWhiteSpace(variant.File)) + { + var candidate = Path.Combine(toolDir, variant.File); + if (File.Exists(candidate) && IsLaunchable(candidate)) + { + return candidate; + } + } + + if (!string.IsNullOrWhiteSpace(variant.Dir)) + { + var candidateDir = Path.Combine(categoryRoot, variant.Dir); + if (Directory.Exists(candidateDir)) + { + return FindPrimaryLaunchable(candidateDir, null); + } + } + + return null; + } + + private static string? FindPrimaryLaunchable(string toolDir, string? launchTarget) + { + if (!string.IsNullOrWhiteSpace(launchTarget)) + { + var directTarget = Path.Combine(toolDir, launchTarget); + if (File.Exists(directTarget) && IsLaunchable(directTarget)) + { + return directTarget; + } + + var deepTarget = Directory.EnumerateFiles(toolDir, launchTarget, SearchOption.AllDirectories) + .FirstOrDefault(IsLaunchable); + if (deepTarget is not null) + { + return deepTarget; + } + } + + var allLaunchables = Directory.EnumerateFiles(toolDir, "*", SearchOption.AllDirectories) + .Where(IsLaunchable) + .ToList(); + if (allLaunchables.Count == 0) + { + return null; + } + + if (allLaunchables.Count == 1) + { + return allLaunchables[0]; + } + + var dirName = Path.GetFileName(toolDir); + var directLaunchables = Directory.EnumerateFiles(toolDir).Where(IsLaunchable).ToList(); + var exact = directLaunchables.FirstOrDefault(file => + Path.GetFileNameWithoutExtension(file).Equals(dirName, StringComparison.OrdinalIgnoreCase)); + if (exact is not null) + { + return exact; + } + + var directArch = directLaunchables + .Where(file => StripArchSuffix(Path.GetFileNameWithoutExtension(file)).Equals(StripArchSuffix(dirName), StringComparison.OrdinalIgnoreCase)) + .ToList(); + if (directArch.Count > 0) + { + return PickPreferredArch(directArch); + } + + var deepArch = allLaunchables + .Where(file => StripArchSuffix(Path.GetFileNameWithoutExtension(file)).Equals(StripArchSuffix(dirName), StringComparison.OrdinalIgnoreCase)) + .ToList(); + if (deepArch.Count > 0) + { + return PickPreferredArch(deepArch); + } + + return directLaunchables.FirstOrDefault() ?? allLaunchables[0]; + } + + private static List MergeArchDirectories(IReadOnlyList toolDirs) + { + var consumed = new HashSet(); + var result = new List(); + for (var i = 0; i < toolDirs.Count; i++) + { + if (consumed.Contains(i)) + { + continue; + } + + var stripped = StripArchSuffix(Path.GetFileName(toolDirs[i])); + result.Add(toolDirs[i]); + for (var j = i + 1; j < toolDirs.Count; j++) + { + if (StripArchSuffix(Path.GetFileName(toolDirs[j])).Equals(stripped, StringComparison.OrdinalIgnoreCase)) + { + consumed.Add(j); + } + } + } + + return result; + } + + private static string PickPreferredArch(IReadOnlyList candidates) + { + if (global::System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture == global::System.Runtime.InteropServices.Architecture.Arm64) + { + var arm64 = candidates.FirstOrDefault(path => Arm64Patterns.Any(pattern => Path.GetFileNameWithoutExtension(path).EndsWith(pattern, StringComparison.OrdinalIgnoreCase))); + if (arm64 is not null) + { + return arm64; + } + } + + if (Environment.Is64BitOperatingSystem) + { + var x64 = candidates.FirstOrDefault(path => X64Patterns.Any(pattern => Path.GetFileNameWithoutExtension(path).EndsWith(pattern, StringComparison.OrdinalIgnoreCase))); + if (x64 is not null) + { + return x64; + } + } + + var x86 = candidates.FirstOrDefault(path => X86Patterns.Any(pattern => Path.GetFileNameWithoutExtension(path).EndsWith(pattern, StringComparison.OrdinalIgnoreCase))); + return x86 ?? candidates[0]; + } + + private static ReferenceToolRiskLevel DetermineRisk(string path) + { + var extension = Path.GetExtension(path).ToLowerInvariant(); + if (extension is ".bat" or ".cmd" or ".ps1" or ".vbs" or ".msc") + { + return ReferenceToolRiskLevel.High; + } + + return extension == ".exe" ? ReferenceToolRiskLevel.Medium : ReferenceToolRiskLevel.Low; + } + + private static bool LooksAdministrative(string path) + { + var text = $"{Path.GetFileNameWithoutExtension(path)} {Path.GetDirectoryName(path)}"; + return text.Contains("defender", StringComparison.OrdinalIgnoreCase) || + text.Contains("registry", StringComparison.OrdinalIgnoreCase) || + text.Contains("driver", StringComparison.OrdinalIgnoreCase) || + text.Contains("clean", StringComparison.OrdinalIgnoreCase) || + text.Contains("admin", StringComparison.OrdinalIgnoreCase); + } + + private string? GetCachedOrExtractIcon(string toolPath) + { + var extension = Path.GetExtension(toolPath); + if (!OperatingSystem.IsWindowsVersionAtLeast(6, 1) || + (!extension.Equals(".exe", StringComparison.OrdinalIgnoreCase) && !extension.Equals(".lnk", StringComparison.OrdinalIgnoreCase)) || + !File.Exists(toolPath)) + { + return null; + } + + var cacheRoot = Path.Combine(paths.Cache, "tool-icons"); + Directory.CreateDirectory(cacheRoot); + var iconPath = Path.Combine(cacheRoot, $"{Hash(toolPath)}.png"); + if (File.Exists(iconPath) && DateTime.UtcNow - File.GetLastWriteTimeUtc(iconPath) < TimeSpan.FromDays(90)) + { + return iconPath; + } + + try + { + return ExtractIconToCache(toolPath, iconPath); + } + catch (Exception exception) + { + _ = WriteLogAsync("Warning", "icon", "External tool icon extraction failed", $"{Path.GetFileName(toolPath)}: {Sanitize(exception.Message)}", CancellationToken.None); + return null; + } + } + + [SupportedOSPlatform("windows6.1")] + private string? ExtractIconToCache(string toolPath, string iconPath) + { + using var icon = Icon.ExtractAssociatedIcon(toolPath); + if (icon is null) + { + return null; + } + + using var bitmap = icon.ToBitmap(); + bitmap.Save(iconPath, global::System.Drawing.Imaging.ImageFormat.Png); + _ = WriteLogAsync("Information", "icon", "External tool icon extracted", Path.GetFileName(toolPath), CancellationToken.None); + return iconPath; + } + + private static string? GetIconGlyph(string toolPath) + { + return Path.GetExtension(toolPath).ToLowerInvariant() switch + { + ".bat" or ".cmd" => "\uE756", + ".ps1" or ".vbs" => "\uE943", + ".msc" => "\uEC7A", + ".lnk" => "\uE8A7", + ".exe" => null, + _ => "\uE8B7" + }; + } + + private static ToolCategory MapCategory(string categoryName) + { + if (categoryName.Contains("综合", StringComparison.OrdinalIgnoreCase) || + categoryName.Contains("其他", StringComparison.OrdinalIgnoreCase)) + { + return ToolCategory.System; + } + + if (categoryName.Contains("烤鸡", StringComparison.OrdinalIgnoreCase)) + { + return ToolCategory.System; + } + + if (categoryName.Contains("处理器", StringComparison.OrdinalIgnoreCase) || + categoryName.Contains("显卡", StringComparison.OrdinalIgnoreCase) || + categoryName.Contains("硬盘", StringComparison.OrdinalIgnoreCase) || + categoryName.Contains("内存", StringComparison.OrdinalIgnoreCase) || + categoryName.Contains("显示器", StringComparison.OrdinalIgnoreCase) || + categoryName.Contains("外设", StringComparison.OrdinalIgnoreCase)) + { + return ToolCategory.System; + } + + return ToolCategory.Dev; + } + + private IEnumerable CandidateRoots(string folderName) + { + foreach (var root in InstallLayoutPaths.CandidateRoots()) + { + yield return Path.Combine(root, folderName); + + var directory = new DirectoryInfo(root); + while (directory is not null) + { + yield return Path.Combine(directory.FullName, folderName); + yield return Path.Combine(directory.FullName, "tubatool-参考补充项目", folderName); + directory = directory.Parent; + } + } + } + + private string ResolveMetadataRoot(string toolsRoot) + { + foreach (var candidate in CandidateRoots("Metadata")) + { + if (Directory.Exists(candidate)) + { + return candidate; + } + } + + var parent = Directory.GetParent(toolsRoot)?.Parent?.FullName; + return parent is null ? Path.Combine(AppContext.BaseDirectory, "Metadata") : Path.Combine(parent, "Metadata"); + } + + private static bool IsLaunchable(string path) => LaunchableExtensions.Contains(Path.GetExtension(path), StringComparer.OrdinalIgnoreCase); + + private static string GetDisplayName(string path) + { + var name = Path.GetFileNameWithoutExtension(path); + return name.Equals("start", StringComparison.OrdinalIgnoreCase) + ? Directory.GetParent(path)?.Name ?? name + : name; + } + + private static string CleanupName(string name) + { + return name + .Replace("_x64", " x64", StringComparison.OrdinalIgnoreCase) + .Replace("_x86", " x86", StringComparison.OrdinalIgnoreCase) + .Replace("_ARM64", " ARM64", StringComparison.OrdinalIgnoreCase) + .Replace("_arm64", " ARM64", StringComparison.OrdinalIgnoreCase) + .Replace("_", " ", StringComparison.Ordinal) + .Trim(); + } + + private static string StripArchSuffix(string name) + { + foreach (var suffix in ArchSuffixes) + { + if (name.EndsWith(suffix, StringComparison.OrdinalIgnoreCase)) + { + return name[..^suffix.Length].TrimEnd('_', '-', ' '); + } + } + + return name; + } + + private static string? DetectArch(string name) + { + if (Arm64Patterns.Any(pattern => name.EndsWith(pattern, StringComparison.OrdinalIgnoreCase))) + { + return "ARM64"; + } + + if (X64Patterns.Any(pattern => name.EndsWith(pattern, StringComparison.OrdinalIgnoreCase))) + { + return "x64"; + } + + if (X86Patterns.Any(pattern => name.EndsWith(pattern, StringComparison.OrdinalIgnoreCase))) + { + return "x86"; + } + + return null; + } + + private static string FormatArchDisplay(string? arch) + { + return arch switch + { + "ARM64" => "ARM64", + "x64" or "Win64" => "x64", + "x86" or "Win32" => "x86", + _ => arch ?? string.Empty + }; + } + + private static string? ReadFolderDescription(string toolDir) + { + try + { + var readme = Directory.EnumerateFiles(toolDir, "*.*", SearchOption.TopDirectoryOnly) + .FirstOrDefault(path => + Path.GetFileName(path).Contains("readme", StringComparison.OrdinalIgnoreCase) || + Path.GetFileName(path).Contains("说明", StringComparison.CurrentCultureIgnoreCase)); + return readme is null + ? null + : File.ReadLines(readme).FirstOrDefault(line => !string.IsNullOrWhiteSpace(line))?.Trim() is { } line + ? line.Length > 180 ? line[..180] : line + : null; + } + catch + { + return null; + } + } + + private static string? FirstUseful(params string?[] values) + => values.FirstOrDefault(value => !string.IsNullOrWhiteSpace(value))?.Trim(); + + private static string Hash(string value) + { + var bytes = SHA256.HashData(Encoding.UTF8.GetBytes(value)); + return Convert.ToHexString(bytes)[..16].ToLowerInvariant(); + } + + private static int CategorySortIndex(ToolCategory category) + { + return category switch + { + ToolCategory.Plugin => 1, + ToolCategory.Dev => 2, + ToolCategory.Network => 3, + ToolCategory.Security => 4, + ToolCategory.Data => 5, + ToolCategory.Calculator => 6, + ToolCategory.Text => 7, + ToolCategory.Image => 8, + ToolCategory.Design => 9, + ToolCategory.Life => 10, + ToolCategory.System => 11, + _ => 0 + }; + } + + private static string Sanitize(string message) + => message.Length > 240 ? message[..240] : message; + + private Task WriteLogAsync(string level, string category, string message, string? detail, CancellationToken cancellationToken) + { + return logService?.WriteAsync(level, category, message, detail, cancellationToken) ?? Task.CompletedTask; + } + + private sealed record ToolInfo( + string? Description, + string? Publisher, + string? Version, + IReadOnlyList? Tags); + + private sealed record JsonArchVariantResult(string? File, string? Dir, string? Arch); + + private sealed class ToolMetadataDatabase + { + private readonly IReadOnlyList _items; + + private ToolMetadataDatabase(IReadOnlyList items) + { + _items = items; + } + + public static ToolMetadataDatabase Load(string metadataRoot, string toolsRoot, ILogService? logService) + { + var path = Path.Combine(metadataRoot, "tools.json"); + if (!File.Exists(path)) + { + return new ToolMetadataDatabase([]); + } + + try + { + using var stream = File.OpenRead(path); + var database = JsonSerializer.Deserialize(stream, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); + return new ToolMetadataDatabase(database?.Tools ?? []); + } + catch (Exception exception) + { + _ = logService?.WriteAsync("Warning", "tool-metadata", "External tool metadata load failed", $"{path}: {exception.Message}"); + return new ToolMetadataDatabase([]); + } + } + + public ToolInfo GetMetadata(string toolsRoot, string launchPath, string toolDir) + { + FileVersionInfo? versionInfo = null; + try + { + if (File.Exists(launchPath)) + { + versionInfo = FileVersionInfo.GetVersionInfo(launchPath); + } + } + catch + { + } + + var json = FindJsonMetadata(toolsRoot, launchPath, toolDir); + return new ToolInfo( + FirstUseful(json?.Description, versionInfo?.FileDescription, versionInfo?.ProductName), + FirstUseful(json?.Publisher, versionInfo?.CompanyName, versionInfo?.LegalCopyright), + FirstUseful(versionInfo?.ProductVersion, versionInfo?.FileVersion), + json?.Tags); + } + + public string? GetLaunchTarget(string toolsRoot, string toolDir) + => FindJsonMetadataByDir(toolsRoot, toolDir)?.LaunchTarget; + + public IReadOnlyList GetArchVariants(string toolDir) + { + var json = FindJsonMetadataByDir(string.Empty, toolDir); + return json?.ArchVariants? + .Select(variant => new JsonArchVariantResult(variant.File, variant.Dir, variant.Arch)) + .ToArray() ?? []; + } + + private JsonToolMetadata? FindJsonMetadata(string toolsRoot, string launchPath, string toolDir) + { + var fileName = Path.GetFileNameWithoutExtension(launchPath); + var relative = SafeRelative(toolsRoot, launchPath); + var dirName = Path.GetFileName(toolDir); + return _items + .Where(item => !string.IsNullOrWhiteSpace(item.Match) && + (fileName.Contains(item.Match, StringComparison.CurrentCultureIgnoreCase) || + relative.Contains(item.Match, StringComparison.CurrentCultureIgnoreCase) || + MatchesFlexible(dirName, item.Match))) + .OrderByDescending(item => item.Match!.Length) + .FirstOrDefault(); + } + + private JsonToolMetadata? FindJsonMetadataByDir(string toolsRoot, string toolDir) + { + var dirName = Path.GetFileName(toolDir); + var relative = SafeRelative(toolsRoot, toolDir); + return _items + .Where(item => !string.IsNullOrWhiteSpace(item.Match) && + (relative.Contains(item.Match, StringComparison.CurrentCultureIgnoreCase) || + MatchesFlexible(dirName, item.Match))) + .OrderByDescending(item => item.Match!.Length) + .FirstOrDefault(); + } + + private static string SafeRelative(string root, string path) + { + try + { + return string.IsNullOrWhiteSpace(root) ? path : Path.GetRelativePath(root, path); + } + catch + { + return path; + } + } + + private static bool MatchesFlexible(string? source, string match) + { + if (string.IsNullOrWhiteSpace(source)) + { + return false; + } + + if (source.Contains(match, StringComparison.CurrentCultureIgnoreCase)) + { + return true; + } + + var normalizedSource = source.Replace(" ", "", StringComparison.Ordinal).Replace("-", "", StringComparison.Ordinal).Replace("_", "", StringComparison.Ordinal); + var normalizedMatch = match.Replace(" ", "", StringComparison.Ordinal).Replace("-", "", StringComparison.Ordinal).Replace("_", "", StringComparison.Ordinal); + return normalizedSource.Contains(normalizedMatch, StringComparison.CurrentCultureIgnoreCase); + } + + private sealed class JsonToolDatabase + { + public List Tools { get; set; } = []; + } + + private sealed class JsonToolMetadata + { + public string? Match { get; set; } + + public string? Description { get; set; } + + public string? Publisher { get; set; } + + public string? LaunchTarget { get; set; } + + public List? Tags { get; set; } + + public List? ArchVariants { get; set; } + } + + private sealed class JsonArchVariant + { + public string? File { get; set; } + + public string? Dir { get; set; } + + public string? Arch { get; set; } + } + } +} diff --git a/src/YMhut.Box.Core/Tools/ExternalToolLaunchService.cs b/src/YMhut.Box.Core/Tools/ExternalToolLaunchService.cs new file mode 100644 index 0000000..0291e60 --- /dev/null +++ b/src/YMhut.Box.Core/Tools/ExternalToolLaunchService.cs @@ -0,0 +1,143 @@ +using System.Diagnostics; +using YMhut.Box.Core.Logging; +using YMhut.Box.Core.Settings; + +namespace YMhut.Box.Core.Tools; + +public interface IExternalToolLaunchService +{ + Task LaunchAsync( + ExternalToolItem tool, + string? launchPath = null, + bool runAsAdministrator = false, + bool confirmed = false, + CancellationToken cancellationToken = default); + + Task OpenDirectoryAsync(ExternalToolItem tool, CancellationToken cancellationToken = default); +} + +public sealed class ExternalToolLaunchService( + ILogService? logService = null, + ISettingsService? settingsService = null) : IExternalToolLaunchService +{ + public async Task LaunchAsync( + ExternalToolItem tool, + string? launchPath = null, + bool runAsAdministrator = false, + bool confirmed = false, + CancellationToken cancellationToken = default) + { + var start = Stopwatch.StartNew(); + var target = string.IsNullOrWhiteSpace(launchPath) ? tool.LaunchPath : launchPath; + var operation = runAsAdministrator ? "launch-admin" : "launch"; + if (tool.IsRisky && !confirmed) + { + await WriteToolLogAsync("Warning", tool.Id, operation, "confirmation-required", start.ElapsedMilliseconds, null, SafePath(target), cancellationToken).ConfigureAwait(false); + return ReferenceToolExecutionResult.ConfirmationRequired("此工具需要二次确认后才能启动。", $"工具:{tool.Name}\n路径:{SafePath(target)}"); + } + + try + { + if (!File.Exists(target)) + { + await WriteToolLogAsync("Error", tool.Id, operation, "failed", start.ElapsedMilliseconds, "file-not-found", SafePath(target), cancellationToken).ConfigureAwait(false); + return ReferenceToolExecutionResult.Fail("启动文件不存在。", SafePath(target)); + } + + var extension = Path.GetExtension(target).ToLowerInvariant(); + var startInfo = BuildStartInfo(target, extension, runAsAdministrator || tool.RequiresAdministrator); + Process.Start(startInfo); + if (settingsService is not null) + { + await settingsService.RecordRecentToolAsync(tool.Id, cancellationToken).ConfigureAwait(false); + } + + await WriteToolLogAsync("Information", tool.Id, operation, "success", start.ElapsedMilliseconds, null, SafePath(target), cancellationToken).ConfigureAwait(false); + return ReferenceToolExecutionResult.Ok("工具已启动。", SafePath(target)); + } + catch (Exception exception) + { + await WriteToolLogAsync("Error", tool.Id, operation, "failed", start.ElapsedMilliseconds, Sanitize(exception.Message), SafePath(target), cancellationToken).ConfigureAwait(false); + return ReferenceToolExecutionResult.Fail("工具启动失败。", Sanitize(exception.Message)); + } + } + + public async Task OpenDirectoryAsync(ExternalToolItem tool, CancellationToken cancellationToken = default) + { + var start = Stopwatch.StartNew(); + try + { + if (!Directory.Exists(tool.ToolDirectory)) + { + await WriteToolLogAsync("Error", tool.Id, "open-directory", "failed", start.ElapsedMilliseconds, "directory-not-found", SafePath(tool.ToolDirectory), cancellationToken).ConfigureAwait(false); + return ReferenceToolExecutionResult.Fail("工具目录不存在。", SafePath(tool.ToolDirectory)); + } + + Process.Start(new ProcessStartInfo + { + FileName = tool.ToolDirectory, + UseShellExecute = true + }); + await WriteToolLogAsync("Information", tool.Id, "open-directory", "success", start.ElapsedMilliseconds, null, SafePath(tool.ToolDirectory), cancellationToken).ConfigureAwait(false); + return ReferenceToolExecutionResult.Ok("目录已打开。", SafePath(tool.ToolDirectory)); + } + catch (Exception exception) + { + await WriteToolLogAsync("Error", tool.Id, "open-directory", "failed", start.ElapsedMilliseconds, Sanitize(exception.Message), SafePath(tool.ToolDirectory), cancellationToken).ConfigureAwait(false); + return ReferenceToolExecutionResult.Fail("打开目录失败。", Sanitize(exception.Message)); + } + } + + private static ProcessStartInfo BuildStartInfo(string path, string extension, bool runAsAdministrator) + { + if (extension == ".ps1") + { + return new ProcessStartInfo + { + FileName = "powershell.exe", + Arguments = $"-NoProfile -ExecutionPolicy Bypass -File \"{path}\"", + WorkingDirectory = Path.GetDirectoryName(path) ?? Environment.CurrentDirectory, + UseShellExecute = true, + Verb = runAsAdministrator ? "runas" : string.Empty + }; + } + + return new ProcessStartInfo + { + FileName = path, + WorkingDirectory = Path.GetDirectoryName(path) ?? Environment.CurrentDirectory, + UseShellExecute = true, + Verb = runAsAdministrator ? "runas" : string.Empty + }; + } + + private Task WriteToolLogAsync( + string level, + string toolId, + string operation, + string result, + long elapsedMs, + string? error, + string? detail, + CancellationToken cancellationToken) + { + var payload = $"toolId={toolId}; operation={operation}; result={result}; elapsedMs={elapsedMs}; error={error ?? ""}; detail={detail ?? ""}"; + return logService?.WriteAsync(level, "tool-run", $"External tool {result}: {operation}", payload, cancellationToken) + ?? Task.CompletedTask; + } + + private static string SafePath(string? path) + { + if (string.IsNullOrWhiteSpace(path)) + { + return string.Empty; + } + + var file = Path.GetFileName(path); + var parent = Path.GetFileName(Path.GetDirectoryName(path) ?? string.Empty); + return string.IsNullOrWhiteSpace(parent) ? file : Path.Combine("...", parent, file); + } + + private static string Sanitize(string value) + => value.Length > 260 ? value[..260] : value; +} diff --git a/src/YMhut.Box.Core/Tools/IToolModule.cs b/src/YMhut.Box.Core/Tools/IToolModule.cs new file mode 100644 index 0000000..9794c9c --- /dev/null +++ b/src/YMhut.Box.Core/Tools/IToolModule.cs @@ -0,0 +1,10 @@ +namespace YMhut.Box.Core.Tools; + +public interface IToolModule +{ + string Id { get; } + + ToolMetadata Metadata { get; } + + object CreateViewModel(); +} diff --git a/src/YMhut.Box.Core/Tools/QqProfileProvider.cs b/src/YMhut.Box.Core/Tools/QqProfileProvider.cs new file mode 100644 index 0000000..d8dd044 --- /dev/null +++ b/src/YMhut.Box.Core/Tools/QqProfileProvider.cs @@ -0,0 +1,238 @@ +using System.Text.Json; +using System.Text.RegularExpressions; +using YMhut.Box.Core.Api; + +namespace YMhut.Box.Core.Tools; + +public sealed record QqProfileResult( + string Qq, + string AvatarUrl, + IReadOnlyDictionary Fields, + string SourceName, + bool ProfileAvailable, + string StatusMessage); + +public static class QqProfileProvider +{ + private const string SourceName = "Uapis QQ User Info"; + + public static async Task QueryAsync( + string input, + IApiManager? apiManager, + CancellationToken cancellationToken, + string language = "zh-CN") + { + var qq = Regex.Match(input, @"\d{5,12}").Value; + if (string.IsNullOrWhiteSpace(qq)) + { + qq = "10000"; + } + + var avatarUrl = AvatarUrl(qq); + var fields = new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["QQ"] = qq + }; + + if (apiManager is null) + { + return new QqProfileResult( + qq, + avatarUrl, + fields, + "qlogo.cn", + false, + T(language, "资料源未配置,已展示默认 QQ 头像。", "Profile source is not configured; default QQ avatar is shown.")); + } + + try + { + var response = await apiManager.FetchAsync("qq_avatar", qq, cancellationToken).ConfigureAwait(false); + if (!response.Success || string.IsNullOrWhiteSpace(response.Content)) + { + return new QqProfileResult( + qq, + avatarUrl, + fields, + SourceName, + false, + response.Error ?? T(language, "资料源未返回可用内容。", "Profile source returned no usable content.")); + } + + var parsed = ParseProfile(response.Content, qq); + foreach (var pair in parsed.Fields) + { + fields[pair.Key] = pair.Value; + } + + if (IsAbsoluteHttpUrl(parsed.AvatarUrl)) + { + avatarUrl = parsed.AvatarUrl; + } + + if (!fields.ContainsKey("头像") && IsAbsoluteHttpUrl(avatarUrl)) + { + fields["头像"] = avatarUrl; + } + + var available = fields.Count > 1; + return new QqProfileResult( + qq, + avatarUrl, + fields, + SourceName, + available, + available + ? T(language, "资料字段来自新版 QQ 信息源;请求地址已隐藏,响应中的头像链接可展示。", "Profile fields came from the new QQ information source; request URL is hidden and returned avatar links can be displayed.") + : T(language, "资料源未返回昵称、签名、等级或会员信息。", "Profile source did not return nickname, signature, level, or membership fields.")); + } + catch (Exception exception) + { + return new QqProfileResult(qq, avatarUrl, fields, SourceName, false, SensitiveText.Sanitize(exception.Message)); + } + } + + public static string AvatarUrl(string qq) => $"https://q1.qlogo.cn/g?b=qq&nk={Uri.EscapeDataString(qq)}&s=640"; + + private static ParsedProfile ParseProfile(string content, string qq) + { + var fields = new Dictionary(StringComparer.OrdinalIgnoreCase); + var avatarUrl = string.Empty; + var text = content.Trim(); + if (text.Contains("portraitCallBack", StringComparison.OrdinalIgnoreCase)) + { + var nicknameMatch = Regex.Match(text, @$"""{Regex.Escape(qq)}""\s*:\s*\[[^\]]*?""(?[^""]+)"""); + if (nicknameMatch.Success) + { + fields["昵称"] = nicknameMatch.Groups["nickname"].Value; + } + + return new ParsedProfile(fields, avatarUrl); + } + + try + { + using var document = JsonDocument.Parse(text); + var root = document.RootElement; + if (root.ValueKind == JsonValueKind.Object && + root.TryGetProperty("data", out var data) && + data.ValueKind == JsonValueKind.Object) + { + root = data; + } + + AddIfPresent(fields, root, "QQ", "qq", "uin"); + AddIfPresent(fields, root, "用户 ID", "user_id", "userId"); + AddIfPresent(fields, root, "昵称", "nickname", "nick", "name"); + AddIfPresent(fields, root, "个性签名", "long_nick", "longNick", "signature", "sign"); + AddIfPresent(fields, root, "头像", "avatar_url", "avatarUrl", "avatar", "headimgurl", "figureurl"); + AddIfPresent(fields, root, "年龄", "age"); + AddIfPresent(fields, root, "性别", "sex", "gender"); + AddIfPresent(fields, root, "QID", "qid"); + AddIfPresent(fields, root, "QQ 等级", "qq_level", "qqLevel", "level"); + AddIfPresent(fields, root, "QQ 等级图标", "qq_level_icons", "qqLevelIcons"); + AddIfPresent(fields, root, "位置", "location", "region", "area", "province", "city"); + AddIfPresent(fields, root, "邮箱", "email"); + AddIfPresent(fields, root, "VIP", "is_vip", "vip"); + AddIfPresent(fields, root, "年费 VIP", "is_years_vip", "years_vip", "isYearsVip"); + AddIfPresent(fields, root, "SVIP", "is_svip", "svip", "isSvip"); + AddIfPresent(fields, root, "QQ 大会员", "is_big_club", "big_club", "isBigClub"); + AddIfPresent(fields, root, "会员状态", "vip_status", "vipStatus"); + AddIfPresent(fields, root, "会员类型", "vip_type", "vipType"); + AddIfPresent(fields, root, "VIP 等级", "vip_level", "vipLevel"); + AddIfPresent(fields, root, "QQ 大会员等级", "big_club_level", "bigClubLevel"); + AddIfPresent(fields, root, "黄钻等级", "yellow_diamond_level", "yellowDiamondLevel"); + AddIfPresent(fields, root, "绿钻等级", "green_diamond_level", "greenDiamondLevel"); + AddIfPresent(fields, root, "腾讯视频会员等级", "video_vip_level", "videoVipLevel"); + AddIfPresent(fields, root, "情侣会员等级", "lover_vip_level", "loverVipLevel"); + AddIfPresent(fields, root, "注册时间", "reg_time", "regTime", "registerTime", "registrationTime", "createdAt"); + AddIfPresent(fields, root, "最后更新时间", "last_updated", "lastUpdated", "updatedAt"); + + if (TryFind(root, "privilege_icons", out var icons) && !string.IsNullOrWhiteSpace(icons)) + { + fields["特权图标"] = icons; + } + + if (fields.TryGetValue("头像", out var parsedAvatar) && IsAbsoluteHttpUrl(parsedAvatar)) + { + avatarUrl = parsedAvatar; + } + } + catch + { + } + + return new ParsedProfile(fields, avatarUrl); + } + + private static void AddIfPresent(Dictionary fields, JsonElement root, string label, params string[] names) + { + foreach (var name in names) + { + if (TryFind(root, name, out var value) && !string.IsNullOrWhiteSpace(value)) + { + fields[label] = value; + return; + } + } + } + + private static bool TryFind(JsonElement element, string name, out string value) + { + value = string.Empty; + if (element.ValueKind == JsonValueKind.Object) + { + foreach (var property in element.EnumerateObject()) + { + if (property.NameEquals(name)) + { + value = StringValue(property.Value); + return true; + } + + if (TryFind(property.Value, name, out value)) + { + return true; + } + } + } + else if (element.ValueKind == JsonValueKind.Array) + { + foreach (var item in element.EnumerateArray()) + { + if (TryFind(item, name, out value)) + { + return true; + } + } + } + + return false; + } + + private static string StringValue(JsonElement value) + { + return value.ValueKind switch + { + JsonValueKind.String => value.GetString() ?? string.Empty, + JsonValueKind.Number => value.GetRawText(), + JsonValueKind.True => "是", + JsonValueKind.False => "否", + JsonValueKind.Null => string.Empty, + JsonValueKind.Object or JsonValueKind.Array => JsonSerializer.Serialize(value), + _ => value.GetRawText() + }; + } + + private static bool IsAbsoluteHttpUrl(string value) + { + return Uri.TryCreate(value, UriKind.Absolute, out var uri) && + uri.Scheme is "http" or "https"; + } + + private static bool English(string? language) => string.Equals(language, "en-US", StringComparison.OrdinalIgnoreCase); + + private static string T(string? language, string zh, string en) => English(language) ? en : zh; + + private sealed record ParsedProfile(IReadOnlyDictionary Fields, string AvatarUrl); +} diff --git a/src/YMhut.Box.Core/Tools/ReferenceToolModels.cs b/src/YMhut.Box.Core/Tools/ReferenceToolModels.cs new file mode 100644 index 0000000..fb9b029 --- /dev/null +++ b/src/YMhut.Box.Core/Tools/ReferenceToolModels.cs @@ -0,0 +1,135 @@ +namespace YMhut.Box.Core.Tools; + +public enum ReferenceToolRiskLevel +{ + None, + Low, + Medium, + High +} + +public enum BuiltinReferenceToolKind +{ + Information, + Dialog, + Command, + SystemEntry, + ExternalHelper +} + +public sealed record ExternalToolVariant( + string Name, + string Path, + string? Architecture); + +public sealed record ExternalToolItem( + string Id, + string Name, + string Description, + string CategoryName, + ToolCategory Category, + string ToolDirectory, + string LaunchPath, + string RelativePath, + string Extension, + string? Publisher, + string? Version, + string? IconGlyph, + string? IconPath, + string? PrimaryArchitecture, + IReadOnlyList Tags, + IReadOnlyList Variants, + ReferenceToolRiskLevel RiskLevel, + bool RequiresAdministrator) +{ + public bool IsRisky => RiskLevel != ReferenceToolRiskLevel.None || RequiresAdministrator; +} + +public sealed class ExternalToolModule(ExternalToolItem tool) : IToolModule +{ + public const string IdPrefix = "external:"; + + public ExternalToolItem Tool { get; } = tool; + + public string Id => Tool.Id; + + public ToolMetadata Metadata { get; } = new( + tool.Id, + tool.Name, + tool.Description, + tool.Category, + tool.Tags.Concat(["external", "tools", tool.CategoryName, tool.Extension]).Distinct(StringComparer.OrdinalIgnoreCase).ToArray(), + true, + string.IsNullOrWhiteSpace(tool.IconGlyph) ? "\uE8B7" : tool.IconGlyph); + + public object CreateViewModel() => new ToolModuleViewModel(Metadata); + + public static bool IsExternalToolId(string id) => id.StartsWith(IdPrefix, StringComparison.OrdinalIgnoreCase); +} + +public sealed record BuiltinReferenceToolDefinition( + string Id, + string Name, + string Description, + ToolCategory Category, + string Glyph, + BuiltinReferenceToolKind Kind, + ReferenceToolRiskLevel RiskLevel, + IReadOnlyList Keywords, + string PrimaryAction, + IReadOnlyList SecondaryActions) +{ + public bool IsRisky => RiskLevel != ReferenceToolRiskLevel.None; +} + +public sealed class BuiltinReferenceToolModule(BuiltinReferenceToolDefinition definition) : IToolModule +{ + public const string IdPrefix = "builtin:"; + + public BuiltinReferenceToolDefinition Definition { get; } = definition; + + public string Id { get; } = $"{IdPrefix}{definition.Id}"; + + public ToolMetadata Metadata { get; } = new( + $"{IdPrefix}{definition.Id}", + definition.Name, + definition.Description, + definition.Category, + definition.Keywords.Concat(["builtin", "reference", "ymhut", definition.Id]).Distinct(StringComparer.OrdinalIgnoreCase).ToArray(), + true, + definition.Glyph); + + public object CreateViewModel() => new ToolModuleViewModel(Metadata); + + public static bool IsBuiltinToolId(string id) => id.StartsWith(IdPrefix, StringComparison.OrdinalIgnoreCase); +} + +public sealed record ToolExecutionLogDetail( + string ToolId, + string Operation, + string Result, + long ElapsedMilliseconds, + string? ErrorSummary = null, + string? Detail = null); + +public sealed record ReferenceToolExecutionResult( + bool Success, + bool Cancelled, + bool RequiresConfirmation, + string Message, + string? Detail = null, + string? OutputPath = null, + int? ExitCode = null) +{ + public static ReferenceToolExecutionResult Ok(string message, string? detail = null, string? outputPath = null, int? exitCode = null) + => new(true, false, false, message, detail, outputPath, exitCode); + + public static ReferenceToolExecutionResult Fail(string message, string? detail = null, int? exitCode = null) + => new(false, false, false, message, detail, null, exitCode); + + public static ReferenceToolExecutionResult Cancel(string message, string? detail = null) + => new(false, true, false, message, detail); + + public static ReferenceToolExecutionResult ConfirmationRequired(string message, string? detail = null) + => new(false, false, true, message, detail); +} diff --git a/src/YMhut.Box.Core/Tools/RemoteToolFormatter.cs b/src/YMhut.Box.Core/Tools/RemoteToolFormatter.cs new file mode 100644 index 0000000..99e46d8 --- /dev/null +++ b/src/YMhut.Box.Core/Tools/RemoteToolFormatter.cs @@ -0,0 +1,1159 @@ +using System.Globalization; +using System.Net; +using System.Text; +using System.Text.Json; +using System.Text.RegularExpressions; +using System.Xml.Linq; +using YMhut.Box.Core.Api; + +namespace YMhut.Box.Core.Tools; + +internal static class RemoteToolFormatter +{ + private static bool English(string? language) => string.Equals(language, "en-US", StringComparison.OrdinalIgnoreCase); + + private static string T(string? language, string zh, string en) => English(language) ? en : zh; + + public static ToolExecutionResult Format(ApiEndpoint endpoint, ApiResponse response, string input, string language = "zh-CN") + { + var header = Header(endpoint, response, language); + var body = endpoint.Id switch + { + "baidu_hot" or "hotboard" or "bili_hot" or "zhihu_hot" => FormatDailyHot(response.Content, endpoint.Id), + "tech_news" => FormatRss(response.Content), + "earthquake_info" => FormatEarthquakes(response.Content), + "dns_query" => FormatDns(response.Content), + "ip_info" or "ip_lookup" or "rdap_ip_lookup" => FormatRdap(response.Content), + "rdap_domain_lookup" or "domain_price" => FormatRdap(response.Content, endpoint.Id == "domain_price"), + "history_today" => FormatPearHistoryToday(response.Content), + "gold_price" => FormatLbmaGold(response.Content), + "train_query" => FormatTrainStations(response.Content, input), + "ai_latest_news" => FormatAiLatestNews(response.Content, language), + "city_route_query" => FormatCityRoute(response.Content, language), + "movie_box_office" => FormatMaoyanBoxOffice(response.Content, language), + "football_news" or "cctv_news" or "oil_price" or "wx_domain_check" or "http_diagnostic" => FormatHtmlSummary(response.Content, endpoint), + "qq_avatar" => response.Uri.ToString(), + _ => Trim(response.Content) + }; + body = LocalizeCommonBody(body, language); + + var output = $"{header}{Environment.NewLine}{Environment.NewLine}{body}".Trim(); + return ToolExecutionResult.Success(output, ToolResultBuilder.FromRemote(endpoint, response, output, language)); + } + + public static ToolExecutionResult FormatFailure(ApiEndpoint endpoint, ApiResponse response, string input, string language = "zh-CN") + { + var statusCode = response.StatusCode > 0 ? response.StatusCode.ToString(CultureInfo.InvariantCulture) : T(language, "未知", "unknown"); + var reason = string.IsNullOrWhiteSpace(response.Error) + ? T(language, "上游接口没有返回成功状态。", "The upstream source did not return a success status.") + : response.Error; + var isForbidden = response.StatusCode is 401 or 403; + var body = string.Join(Environment.NewLine, new[] + { + isForbidden + ? T(language, "源站拒绝了自动请求。", "The source rejected the automated request.") + : T(language, "远程数据暂时不可用。", "Remote data is temporarily unavailable."), + T(language, $"状态码:{statusCode}", $"Status code: {statusCode}"), + T(language, $"原因:{reason}", $"Reason: {reason}"), + T(language, "建议:可以稍后重试,或打开公开来源页面查看。", "Suggestion: try again later, or open the public source page."), + T(language, $"来源:{endpoint.SourceName}", $"Source: {endpoint.SourceName}") + }); + var output = $"{Header(endpoint, response, language)}{Environment.NewLine}{Environment.NewLine}{body}".Trim(); + return ToolExecutionResult.Success(output, ToolResultBuilder.FromRemote(endpoint, response, output, language)); + } + + public static string Header(ApiEndpoint endpoint, ApiResponse response, string language = "zh-CN") + { + var official = endpoint.IsOfficial ? T(language, "官方/权威源", "official / authoritative source") : T(language, "公开可信源", "public trusted source"); + return string.Join(Environment.NewLine, new[] + { + T(language, $"数据源:{endpoint.SourceName}({official})", $"Data source: {endpoint.SourceName} ({official})"), + T(language, "来源说明:已隐藏远程地址,仅展示脱敏来源名称。", "Source note: remote address is hidden; only sanitized source name is shown."), + T(language, $"获取时间:{response.FetchedAt:yyyy-MM-dd HH:mm:ss zzz}", $"Fetched at: {response.FetchedAt:yyyy-MM-dd HH:mm:ss zzz}") + }); + } + + public static string FormatWeather(ApiEndpoint endpoint, ApiResponse geocode, ApiResponse forecast, string language = "zh-CN") + { + var header = Header(endpoint, forecast, language); + using var geocodeJson = JsonDocument.Parse(geocode.Content); + var location = geocodeJson.RootElement.TryGetProperty("results", out var results) && results.ValueKind == JsonValueKind.Array && results.GetArrayLength() > 0 + ? results[0] + : default; + var locationName = location.ValueKind == JsonValueKind.Object + ? $"{GetString(location, "name")} {GetString(location, "admin1")} {GetString(location, "country")}".Trim() + : T(language, "未知位置", "Unknown location"); + + using var forecastJson = JsonDocument.Parse(forecast.Content); + var root = forecastJson.RootElement; + var current = root.TryGetProperty("current", out var currentElement) ? currentElement : default; + var daily = root.TryGetProperty("daily", out var dailyElement) ? dailyElement : default; + + var lines = new List + { + T(language, $"位置:{locationName}", $"Location: {locationName}"), + T(language, $"当前温度:{GetDouble(current, "temperature_2m"):0.#} °C", $"Current temperature: {GetDouble(current, "temperature_2m"):0.#} °C"), + T(language, $"体感温度:{GetDouble(current, "apparent_temperature"):0.#} °C", $"Apparent temperature: {GetDouble(current, "apparent_temperature"):0.#} °C"), + T(language, $"相对湿度:{GetDouble(current, "relative_humidity_2m"):0.#}%", $"Relative humidity: {GetDouble(current, "relative_humidity_2m"):0.#}%"), + T(language, $"风速:{GetDouble(current, "wind_speed_10m"):0.#} km/h", $"Wind speed: {GetDouble(current, "wind_speed_10m"):0.#} km/h") + }; + + if (daily.ValueKind == JsonValueKind.Object && + daily.TryGetProperty("time", out var dates) && + daily.TryGetProperty("temperature_2m_max", out var max) && + daily.TryGetProperty("temperature_2m_min", out var min)) + { + lines.Add(string.Empty); + lines.Add(T(language, "未来三天:", "Next three days:")); + var count = Math.Min(3, dates.GetArrayLength()); + for (var index = 0; index < count; index++) + { + lines.Add($"{dates[index].GetString()}: {min[index].GetDouble():0.#} - {max[index].GetDouble():0.#} °C"); + } + } + + return $"{header}{Environment.NewLine}{Environment.NewLine}{string.Join(Environment.NewLine, lines)}"; + } + + public static string FormatTrainLeftTickets(ApiEndpoint endpoint, ApiResponse stationNames, ApiResponse leftTickets, string from, string to, string date) + { + var header = Header(endpoint, leftTickets); + if (!TryJson(leftTickets.Content, out var document)) + { + return $"{header}{Environment.NewLine}{Environment.NewLine}12306 返回了非 JSON 响应。已确认车站数据来自 12306,余票查询请稍后重试或检查日期是否在预售期内。"; + } + + using (document) + { + if (!document.RootElement.TryGetProperty("data", out var data) || + !data.TryGetProperty("result", out var result) || + result.ValueKind != JsonValueKind.Array) + { + return $"{header}{Environment.NewLine}{Environment.NewLine}未查询到 {date} {from} -> {to} 的车次。"; + } + + var lines = new List + { + $"查询:{date} {from} -> {to}" + }; + + foreach (var row in result.EnumerateArray().Take(20)) + { + var text = row.GetString() ?? string.Empty; + var fields = text.Split('|'); + if (fields.Length < 33) + { + continue; + } + + lines.Add($"{fields[3]} 出发 {fields[8]} 到达 {fields[9]} 历时 {fields[10]} 二等座 {Blank(fields[30])} 一等座 {Blank(fields[31])} 商务座 {Blank(fields[32])}"); + } + + return $"{header}{Environment.NewLine}{Environment.NewLine}{string.Join(Environment.NewLine, lines)}"; + } + } + + private static string LocalizeCommonBody(string body, string language) + { + if (!English(language)) + { + return body; + } + + var replacements = new Dictionary + { + ["热度:"] = "Heat: ", + ["播放 "] = "Views ", + ["发布时间:"] = "Published: ", + ["标题:"] = "Title: ", + ["摘要:"] = "Summary: ", + ["位置:"] = "Location: ", + ["震级:"] = "Magnitude: ", + ["深度:"] = "Depth: ", + ["时间:"] = "Time: ", + ["查询:"] = "Query: ", + ["出发 "] = "Depart ", + ["到达 "] = "Arrive ", + ["历时 "] = "Duration ", + ["二等座 "] = "Second class ", + ["一等座 "] = "First class ", + ["商务座 "] = "Business class ", + ["未查询到"] = "No result found for", + ["返回了非 JSON 响应"] = "returned a non-JSON response", + ["请稍后重试"] = "try again later" + }; + + foreach (var replacement in replacements) + { + body = body.Replace(replacement.Key, replacement.Value, StringComparison.Ordinal); + } + + return body; + } + + private static string FormatBaiduHot(string content) + { + var lines = new List(); + if (TryJson(content, out var document)) + { + using (document) + { + if (document.RootElement.TryGetProperty("data", out var data) && + data.TryGetProperty("cards", out var cards)) + { + foreach (var card in cards.EnumerateArray()) + { + if (!card.TryGetProperty("content", out var contentArray)) + { + continue; + } + + foreach (var item in contentArray.EnumerateArray().Take(20)) + { + var title = GetString(item, "word"); + if (string.IsNullOrWhiteSpace(title)) + { + title = GetString(item, "title"); + } + if (string.IsNullOrWhiteSpace(title)) + { + continue; + } + + var index = lines.Count + 1; + var desc = GetString(item, "desc"); + var hotScore = FirstNonEmpty(GetString(item, "hotScore"), GetString(item, "score"), GetString(item, "heat")); + lines.Add(string.IsNullOrWhiteSpace(hotScore) + ? $"{index}. {title}" + : $"{index}. {title} 热度:{hotScore}"); + if (!string.IsNullOrWhiteSpace(desc)) + { + lines.Add($" {desc}"); + } + } + } + } + + if (lines.Count == 0) + { + lines.AddRange(CollectRankedJsonLines(document.RootElement, "热度")); + } + } + } + + if (lines.Count == 0) + { + lines.AddRange(ExtractEmbeddedRankedLines(content)); + } + + return lines.Count == 0 ? Trim(content) : string.Join(Environment.NewLine, lines); + } + + private static string FormatBiliHot(string content) + { + var lines = new List(); + if (TryJson(content, out var document)) + { + using (document) + { + if (document.RootElement.TryGetProperty("data", out var data) && + data.TryGetProperty("list", out var list) && + list.ValueKind == JsonValueKind.Array) + { + foreach (var item in list.EnumerateArray().Take(20)) + { + var title = GetString(item, "title"); + if (string.IsNullOrWhiteSpace(title)) + { + continue; + } + + var owner = item.TryGetProperty("owner", out var ownerElement) ? GetString(ownerElement, "name") : string.Empty; + var views = item.TryGetProperty("stat", out var stat) ? GetInt64(stat, "view") : 0; + var meta = new List(); + if (!string.IsNullOrWhiteSpace(owner)) + { + meta.Add(owner); + } + if (views > 0) + { + meta.Add($"播放 {views:N0}"); + } + lines.Add(meta.Count == 0 + ? $"{lines.Count + 1}. {title}" + : $"{lines.Count + 1}. {title} / {string.Join(" / ", meta)}"); + } + } + + if (lines.Count == 0) + { + lines.AddRange(CollectRankedJsonLines(document.RootElement, "播放")); + } + } + } + + if (lines.Count == 0) + { + lines.AddRange(ExtractEmbeddedRankedLines(content)); + } + + return lines.Count == 0 ? Trim(content) : string.Join(Environment.NewLine, lines); + } + + private static string FormatZhihuHot(string content) + { + if (!TryJson(content, out var document)) + { + var extracted = ExtractEmbeddedRankedLines(content); + return extracted.Count == 0 ? FormatHtmlAnchors(content) : string.Join(Environment.NewLine, extracted); + } + + using (document) + { + if (!document.RootElement.TryGetProperty("data", out var data)) + { + var fallback = CollectRankedJsonLines(document.RootElement, "热度"); + return fallback.Count == 0 ? Trim(content) : string.Join(Environment.NewLine, fallback); + } + + var lines = new List(); + foreach (var item in data.EnumerateArray().Take(20)) + { + var target = item.TryGetProperty("target", out var targetElement) ? targetElement : item; + var title = GetString(target, "title"); + if (string.IsNullOrWhiteSpace(title)) + { + continue; + } + + var metric = FirstNonEmpty(GetString(item, "detail_text"), GetString(item, "metrics"), GetString(target, "excerpt")); + lines.Add(string.IsNullOrWhiteSpace(metric) + ? $"{lines.Count + 1}. {title}" + : $"{lines.Count + 1}. {title} / {metric}"); + } + + if (lines.Count == 0) + { + lines.AddRange(CollectRankedJsonLines(document.RootElement, "热度")); + } + + return lines.Count == 0 ? Trim(content) : string.Join(Environment.NewLine, lines); + } + } + + private static string FormatDailyHot(string content, string endpointId) + { + if (!TryJson(content, out var document)) + { + var extracted = ExtractEmbeddedRankedLines(content); + return extracted.Count == 0 ? Trim(content) : string.Join(Environment.NewLine, extracted); + } + + using (document) + { + var root = document.RootElement; + var lines = new List(); + + if (TryEnumerateDailyHotPlatforms(root, out var platforms)) + { + lines.Add("平台列表"); + foreach (var (platform, index) in platforms.Take(40).Select((item, index) => (item, index))) + { + var name = FirstNonEmpty(GetString(platform, "title"), GetString(platform, "name"), GetString(platform, "type")); + if (string.IsNullOrWhiteSpace(name)) + { + continue; + } + + var summary = FirstNonEmpty(GetString(platform, "description"), GetString(platform, "desc"), GetString(platform, "updateTime")); + var link = FirstNonEmpty(GetString(platform, "link"), GetString(platform, "url"), GetString(platform, "mobileUrl")); + var line = $"{index + 1}. {name}"; + if (!string.IsNullOrWhiteSpace(summary)) + { + line += $" / {summary}"; + } + if (!string.IsNullOrWhiteSpace(link)) + { + line += $" / {link}"; + } + lines.Add(line); + } + + if (lines.Count > 1) + { + return string.Join(Environment.NewLine, lines); + } + } + + var items = TryEnumerateDailyHotItems(root, endpointId); + foreach (var (item, index) in items.Take(40).Select((value, itemIndex) => (value, itemIndex))) + { + var rank = FirstNonEmpty(GetString(item, "id"), GetString(item, "top"), GetString(item, "rank")); + var title = FirstNonEmpty(GetString(item, "title"), GetString(item, "name"), GetString(item, "word")); + if (string.IsNullOrWhiteSpace(title)) + { + continue; + } + + var meta = new List(); + var ownerName = item.TryGetProperty("owner", out var owner) && owner.ValueKind == JsonValueKind.Object + ? GetString(owner, "name") + : string.Empty; + if (!string.IsNullOrWhiteSpace(ownerName)) + { + meta.Add(ownerName); + } + if (item.TryGetProperty("stat", out var stat) && stat.ValueKind == JsonValueKind.Object) + { + var views = GetInt64(stat, "view"); + if (views > 0) + { + meta.Add($"播放 {views:N0}"); + } + } + + var summary = FirstNonEmpty(GetString(item, "desc"), GetString(item, "summary"), GetString(item, "hot"), GetString(item, "timestamp")); + if (!string.IsNullOrWhiteSpace(summary)) + { + meta.Add(summary); + } + var link = FirstNonEmpty(GetString(item, "url"), GetString(item, "mobileUrl"), GetString(item, "link")); + var line = string.IsNullOrWhiteSpace(rank) ? $"{index + 1}. {title}" : $"{rank}. {title}"; + if (meta.Count > 0) + { + line += $" / {string.Join(" / ", meta)}"; + } + if (!string.IsNullOrWhiteSpace(link)) + { + line += $" / {link}"; + } + lines.Add(line); + } + + if (lines.Count == 0) + { + lines.AddRange(CollectRankedJsonLines(root, "热度")); + } + + return lines.Count == 0 ? Trim(content) : string.Join(Environment.NewLine, lines); + } + } + + private static bool TryEnumerateDailyHotPlatforms(JsonElement root, out IEnumerable platforms) + { + platforms = []; + if (root.ValueKind == JsonValueKind.Object) + { + if (root.TryGetProperty("data", out var data) && data.ValueKind == JsonValueKind.Array) + { + platforms = data.EnumerateArray(); + return true; + } + + if (root.TryGetProperty("formCache", out var formCache) && formCache.ValueKind == JsonValueKind.Array) + { + platforms = formCache.EnumerateArray(); + return true; + } + + if (root.TryGetProperty("data", out data) && data.ValueKind == JsonValueKind.Object && data.TryGetProperty("data", out var nested) && nested.ValueKind == JsonValueKind.Array) + { + platforms = nested.EnumerateArray(); + return true; + } + } + + return false; + } + + private static IEnumerable TryEnumerateDailyHotItems(JsonElement root, string endpointId) + { + if (root.ValueKind != JsonValueKind.Object) + { + return []; + } + + if (root.TryGetProperty("data", out var data)) + { + if (data.ValueKind == JsonValueKind.Array) + { + return data.EnumerateArray(); + } + + if (data.ValueKind == JsonValueKind.Object) + { + foreach (var candidateName in new[] { "list", "cards", "items", "data", "result" }) + { + if (data.TryGetProperty(candidateName, out var nested) && nested.ValueKind == JsonValueKind.Array) + { + return nested.EnumerateArray(); + } + } + } + } + + if (root.TryGetProperty("cards", out var cards) && cards.ValueKind == JsonValueKind.Array) + { + return cards.EnumerateArray(); + } + + if (endpointId.Equals("hotboard", StringComparison.OrdinalIgnoreCase) && root.TryGetProperty("formCache", out var formCache) && formCache.ValueKind == JsonValueKind.Array) + { + return formCache.EnumerateArray(); + } + + return []; + } + + private static string FormatPearHistoryToday(string content) + { + if (TryJson(content, out var document)) + { + using (document) + { + var root = document.RootElement; + if (root.TryGetProperty("data", out var data) && data.ValueKind == JsonValueKind.String) + { + content = data.GetString() ?? content; + } + else if (root.TryGetProperty("data", out data) && data.ValueKind == JsonValueKind.Object) + { + content = JsonSerializer.Serialize(data); + } + else if (root.TryGetProperty("data", out data) && data.ValueKind == JsonValueKind.Array) + { + content = JsonSerializer.Serialize(data); + } + } + } + + if (TryJson(content, out var historyDocument)) + { + using (historyDocument) + { + var root = historyDocument.RootElement; + var lines = new List(); + foreach (var section in new[] { "events", "births", "deaths", "holidays" }) + { + if (!root.TryGetProperty(section, out var items) || items.ValueKind != JsonValueKind.Array) + { + continue; + } + + lines.Add(section.ToUpperInvariant()); + foreach (var item in items.EnumerateArray().Take(12)) + { + var year = FirstNonEmpty(GetString(item, "year"), GetString(item, "date")); + var text = StripHtml(FirstNonEmpty(GetString(item, "text"), GetString(item, "title"), GetString(item, "summary"))); + if (!string.IsNullOrWhiteSpace(text)) + { + lines.Add(string.IsNullOrWhiteSpace(year) ? $"- {text}" : $"- {year}: {text}"); + } + } + + lines.Add(string.Empty); + } + + return lines.Count == 0 ? Trim(content) : string.Join(Environment.NewLine, lines).Trim(); + } + } + + return Trim(content); + } + + private static string FormatAiLatestNews(string content, string language) + { + if (!TryJson(content, out var document)) + { + return Trim(content); + } + + using (document) + { + var root = document.RootElement; + var lines = new List(); + var categories = new[] + { + "models_and_releases", + "tools_and_frameworks", + "research_and_papers", + "industry_and_business", + "applications_and_use_cases", + "technical_discussions", + "ethics_and_safety" + }; + + foreach (var category in categories) + { + if (!root.TryGetProperty("data", out var data) || data.ValueKind != JsonValueKind.Object || !data.TryGetProperty(category, out var items) || items.ValueKind != JsonValueKind.Array) + { + continue; + } + + var label = category.Replace('_', ' '); + lines.Add(label); + foreach (var item in items.EnumerateArray().Take(8)) + { + var created = FirstNonEmpty(GetString(item, "created_at"), GetString(item, "published_at"), GetString(item, "time")); + var title = FirstNonEmpty(GetString(item, "title"), GetString(item, "name")); + var summary = FirstNonEmpty(GetString(item, "summary"), GetString(item, "description")); + var source = FirstNonEmpty(GetString(item, "source"), GetString(item, "site")); + var url = FirstNonEmpty(GetString(item, "url"), GetString(item, "link")); + if (string.IsNullOrWhiteSpace(title)) + { + continue; + } + + var line = string.IsNullOrWhiteSpace(created) ? $"{lines.Count + 1}. {title}" : $"{lines.Count + 1}. {title} / {created}"; + if (!string.IsNullOrWhiteSpace(source)) + { + line += $" / {source}"; + } + if (!string.IsNullOrWhiteSpace(summary)) + { + line += $" / {summary}"; + } + if (!string.IsNullOrWhiteSpace(url)) + { + line += $" / {url}"; + } + lines.Add(line); + } + lines.Add(string.Empty); + } + + return lines.Count == 0 ? Trim(content) : string.Join(Environment.NewLine, lines).Trim(); + } + } + + private static string FormatCityRoute(string content, string language) + { + if (!TryJson(content, out var document)) + { + return Trim(content); + } + + using (document) + { + var root = document.RootElement; + var data = root.TryGetProperty("data", out var dataElement) ? dataElement : root; + var lines = new List(); + foreach (var pair in new[] { + ("from", "出发"), + ("to", "到达"), + ("distance", "距离"), + ("time", "耗时"), + ("fuelcosts", "油费"), + ("bridgetoll", "过路费"), + ("totalcost", "总成本"), + ("roadconditions", "路况") + }) + { + var value = GetString(data, pair.Item1); + if (!string.IsNullOrWhiteSpace(value)) + { + lines.Add($"{pair.Item2}: {value}"); + } + } + + if (lines.Count == 0) + { + foreach (var property in data.EnumerateObject()) + { + if (property.Value.ValueKind is JsonValueKind.String or JsonValueKind.Number) + { + lines.Add($"{property.Name}: {property.Value.GetRawText().Trim('"')}"); + } + } + } + + return lines.Count == 0 ? Trim(content) : string.Join(Environment.NewLine, lines); + } + } + + private static string FormatMaoyanBoxOffice(string content, string language) + { + if (!TryJson(content, out var document)) + { + return Trim(content); + } + + using (document) + { + var root = document.RootElement; + var items = root.ValueKind == JsonValueKind.Array ? root.EnumerateArray() : + root.TryGetProperty("data", out var data) && data.ValueKind == JsonValueKind.Array ? data.EnumerateArray() : + root.TryGetProperty("data", out data) && data.ValueKind == JsonValueKind.Object && data.TryGetProperty("data", out var nested) && nested.ValueKind == JsonValueKind.Array ? nested.EnumerateArray() : + root.TryGetProperty("data", out data) && data.ValueKind == JsonValueKind.Object && data.TryGetProperty("list", out nested) && nested.ValueKind == JsonValueKind.Array ? nested.EnumerateArray() : + []; + + var lines = new List(); + foreach (var item in items.Take(20)) + { + var rank = FirstNonEmpty(GetString(item, "top"), GetString(item, "rank")); + var title = FirstNonEmpty(GetString(item, "movieName"), GetString(item, "title"), GetString(item, "name")); + if (string.IsNullOrWhiteSpace(title)) + { + continue; + } + + var summary = FirstNonEmpty(GetString(item, "releaseInfo"), GetString(item, "sumBoxDesc"), GetString(item, "boxRate"), GetString(item, "showCount"), GetString(item, "showCountRate"), GetString(item, "avgShowView"), GetString(item, "avgSeatView")); + var line = string.IsNullOrWhiteSpace(rank) ? title : $"{rank}. {title}"; + if (!string.IsNullOrWhiteSpace(summary)) + { + line += $" / {summary}"; + } + lines.Add(line); + } + + return lines.Count == 0 ? Trim(content) : string.Join(Environment.NewLine, lines); + } + } + + private static IReadOnlyList CollectRankedJsonLines(JsonElement root, string metricLabel) + { + var lines = new List(); + var seen = new HashSet(StringComparer.OrdinalIgnoreCase); + + void Visit(JsonElement element, int depth) + { + if (lines.Count >= 40 || depth > 10) + { + return; + } + + if (element.ValueKind == JsonValueKind.Object) + { + var title = FirstNonEmpty( + GetString(element, "title"), + GetString(element, "word"), + GetString(element, "name_text"), + GetString(element, "display_title")); + if (IsUsefulRankTitle(title) && seen.Add(title)) + { + var meta = new List(); + if (element.TryGetProperty("owner", out var owner) && owner.ValueKind == JsonValueKind.Object) + { + var ownerName = GetString(owner, "name"); + if (!string.IsNullOrWhiteSpace(ownerName)) + { + meta.Add(ownerName); + } + } + + if (element.TryGetProperty("stat", out var stat) && stat.ValueKind == JsonValueKind.Object) + { + var views = GetInt64(stat, "view"); + if (views > 0) + { + meta.Add($"播放 {views:N0}"); + } + } + + var heat = FirstNonEmpty( + GetString(element, "hotScore"), + GetString(element, "hot_score"), + GetString(element, "heat"), + GetString(element, "score"), + GetString(element, "detail_text"), + GetString(element, "metrics")); + if (!string.IsNullOrWhiteSpace(heat)) + { + meta.Add($"{metricLabel}:{heat}"); + } + + var desc = FirstNonEmpty(GetString(element, "desc"), GetString(element, "excerpt"), GetString(element, "summary")); + lines.Add(meta.Count == 0 + ? $"{lines.Count + 1}. {title}" + : $"{lines.Count + 1}. {title} / {string.Join(" / ", meta)}"); + if (!string.IsNullOrWhiteSpace(desc) && + !desc.Equals(title, StringComparison.OrdinalIgnoreCase) && + desc.Length <= 180) + { + lines.Add($" {desc}"); + } + } + + foreach (var property in element.EnumerateObject()) + { + Visit(property.Value, depth + 1); + } + return; + } + + if (element.ValueKind == JsonValueKind.Array) + { + foreach (var item in element.EnumerateArray()) + { + Visit(item, depth + 1); + } + } + } + + Visit(root, 0); + return lines; + } + + private static IReadOnlyList ExtractEmbeddedRankedLines(string content) + { + var titles = new List(); + var seen = new HashSet(StringComparer.OrdinalIgnoreCase); + + foreach (Match match in Regex.Matches(content, @"""(?:title|word)""\s*:\s*""(?(?:\\.|[^""\\])+)""", RegexOptions.IgnoreCase)) + { + var value = DecodeJsonString(match.Groups["value"].Value); + if (IsUsefulRankTitle(value) && seen.Add(value)) + { + titles.Add(value); + if (titles.Count >= 30) + { + break; + } + } + } + + if (titles.Count == 0) + { + foreach (var anchor in HtmlAnchorTexts(content)) + { + if (IsUsefulRankTitle(anchor) && seen.Add(anchor)) + { + titles.Add(anchor); + if (titles.Count >= 30) + { + break; + } + } + } + } + + return titles + .Take(20) + .Select((title, index) => $"{index + 1}. {title}") + .ToArray(); + } + + private static IEnumerable HtmlAnchorTexts(string content) + { + foreach (Match match in Regex.Matches(content, @"]*>(.*?)", RegexOptions.IgnoreCase | RegexOptions.Singleline)) + { + var text = StripHtml(match.Groups[1].Value); + if (!string.IsNullOrWhiteSpace(text)) + { + yield return text; + } + } + } + + private static string DecodeJsonString(string value) + { + try + { + return JsonSerializer.Deserialize($"\"{value}\"") ?? string.Empty; + } + catch + { + return WebUtility.HtmlDecode(value.Replace("\\/", "/", StringComparison.Ordinal)).Trim(); + } + } + + private static bool IsUsefulRankTitle(string? value) + { + if (string.IsNullOrWhiteSpace(value)) + { + return false; + } + + var text = value.Trim(); + if (text.Length < 2 || text.Length > 160) + { + return false; + } + + return !text.StartsWith("http://", StringComparison.OrdinalIgnoreCase) && + !text.StartsWith("https://", StringComparison.OrdinalIgnoreCase) && + !text.Contains("<", StringComparison.Ordinal) && + !text.Contains(">", StringComparison.Ordinal) && + !text.Equals("更多", StringComparison.OrdinalIgnoreCase) && + !text.Equals("首页", StringComparison.OrdinalIgnoreCase); + } + + private static string FirstNonEmpty(params string[] values) + => values.FirstOrDefault(value => !string.IsNullOrWhiteSpace(value)) ?? string.Empty; + + private static string FormatRss(string content) + { + var document = XDocument.Parse(content); + var items = document.Descendants("item").Take(20); + var lines = items + .Select((item, index) => $"{index + 1}. {Value(item, "title")}{Environment.NewLine} {Value(item, "link")}") + .ToList(); + return lines.Count == 0 ? Trim(content) : string.Join(Environment.NewLine, lines); + } + + private static string FormatEarthquakes(string content) + { + using var document = JsonDocument.Parse(content); + var features = document.RootElement.GetProperty("features").EnumerateArray(); + var lines = new List(); + foreach (var feature in features.Take(20)) + { + var properties = feature.GetProperty("properties"); + var time = DateTimeOffset.FromUnixTimeMilliseconds(GetInt64(properties, "time")).ToLocalTime(); + lines.Add($"{lines.Count + 1}. M{GetDouble(properties, "mag"):0.#} {GetString(properties, "place")} / {time:yyyy-MM-dd HH:mm}"); + } + + return string.Join(Environment.NewLine, lines); + } + + private static string FormatDns(string content) + { + using var document = JsonDocument.Parse(content); + var root = document.RootElement; + var lines = new List + { + $"Status: {GetInt64(root, "Status")}", + $"Question: {GetString(root.GetProperty("Question")[0], "name")}" + }; + if (root.TryGetProperty("Answer", out var answers)) + { + foreach (var answer in answers.EnumerateArray()) + { + lines.Add($"{GetString(answer, "name")} TYPE {GetInt64(answer, "type")} TTL {GetInt64(answer, "TTL")} {GetString(answer, "data")}"); + } + } + + return string.Join(Environment.NewLine, lines); + } + + private static string FormatRdap(string content, bool domainPriceMode = false) + { + using var document = JsonDocument.Parse(content); + var root = document.RootElement; + var lines = new List(); + if (domainPriceMode) + { + lines.Add("说明:域名零售价格由各注册商制定,IANA/RDAP 不提供官方零售价;这里展示官方注册与 RDAP 信息。"); + lines.Add(string.Empty); + } + + foreach (var key in new[] { "objectClassName", "handle", "ldhName", "name", "country", "startAddress", "endAddress" }) + { + var value = GetString(root, key); + if (!string.IsNullOrWhiteSpace(value)) + { + lines.Add($"{key}: {value}"); + } + } + + if (root.TryGetProperty("events", out var events)) + { + foreach (var item in events.EnumerateArray().Take(8)) + { + lines.Add($"{GetString(item, "eventAction")}: {GetString(item, "eventDate")}"); + } + } + + if (root.TryGetProperty("notices", out var notices)) + { + foreach (var notice in notices.EnumerateArray().Take(3)) + { + lines.Add($"Notice: {GetString(notice, "title")}"); + } + } + + return lines.Count == 0 ? Trim(content) : string.Join(Environment.NewLine, lines); + } + + private static string FormatOnThisDay(string content) + { + using var document = JsonDocument.Parse(content); + var lines = new List(); + foreach (var section in new[] { "events", "births", "deaths", "holidays" }) + { + if (!document.RootElement.TryGetProperty(section, out var items)) + { + continue; + } + + lines.Add(section.ToUpperInvariant()); + foreach (var item in items.EnumerateArray().Take(8)) + { + var year = GetInt64(item, "year"); + var text = StripHtml(GetString(item, "text")); + lines.Add($"- {year}: {text}"); + } + lines.Add(string.Empty); + } + + return lines.Count == 0 ? Trim(content) : string.Join(Environment.NewLine, lines).Trim(); + } + + private static string FormatLbmaGold(string content) + { + using var document = JsonDocument.Parse(content); + var items = document.RootElement.EnumerateArray() + .Where(item => item.TryGetProperty("v", out var values) && values.ValueKind == JsonValueKind.Array && values.GetArrayLength() >= 2 && values[0].ValueKind == JsonValueKind.Number) + .ToArray(); + var latest = items.LastOrDefault(); + if (latest.ValueKind != JsonValueKind.Object) + { + return Trim(content); + } + + var date = GetString(latest, "d"); + var v = latest.GetProperty("v"); + var builder = new StringBuilder() + .AppendLine("LBMA Gold PM Fixing") + .AppendLine($"日期:{date}") + .AppendLine($"USD/oz:{v[0].GetDouble():0.##}") + .AppendLine($"GBP/oz:{v[1].GetDouble():0.##}") + .AppendLine() + .AppendLine("Date | USD/oz | GBP/oz"); + + foreach (var item in items.TakeLast(30)) + { + var values = item.GetProperty("v"); + builder.AppendLine($"{GetString(item, "d")} | {values[0].GetDouble():0.##} | {values[1].GetDouble():0.##}"); + } + + return builder.ToString().Trim(); + } + + private static string FormatTrainStations(string content, string input) + { + var stations = ParseStations(content); + if (string.IsNullOrWhiteSpace(input)) + { + return string.Join(Environment.NewLine, stations.Take(20).Select(station => $"{station.Name} / {station.Code} / {station.Pinyin}")); + } + + var matches = stations + .Where(station => station.Name.Contains(input, StringComparison.OrdinalIgnoreCase) || + station.Code.Contains(input, StringComparison.OrdinalIgnoreCase) || + station.Pinyin.Contains(input, StringComparison.OrdinalIgnoreCase)) + .Take(30) + .Select(station => $"{station.Name} / {station.Code} / {station.Pinyin}") + .ToList(); + return matches.Count == 0 ? "未命中车站;可输入中文站名、拼音或 12306 车站代码。" : string.Join(Environment.NewLine, matches); + } + + public static IReadOnlyList ParseStations(string content) + { + var payload = content.Split('\'').Length >= 2 ? content.Split('\'')[1] : content; + return payload.Split('@', StringSplitOptions.RemoveEmptyEntries) + .Select(row => row.Split('|')) + .Where(parts => parts.Length >= 5) + .Select(parts => new TrainStation(parts[1], parts[2], parts[3])) + .ToArray(); + } + + private static string FormatHtmlSummary(string content, ApiEndpoint endpoint) + { + var title = Regex.Match(content, @"]*>(.*?)", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value; + title = StripHtml(title); + var anchors = Regex.Matches(content, @"]*>(.*?)", RegexOptions.IgnoreCase | RegexOptions.Singleline) + .Select(match => StripHtml(match.Groups[1].Value)) + .Where(text => text.Length >= 6) + .Distinct() + .Take(10) + .ToArray(); + + var lines = new List + { + string.IsNullOrWhiteSpace(title) ? endpoint.Name : title + }; + lines.AddRange(anchors.Select((anchor, index) => $"{index + 1}. {anchor}")); + + if (endpoint.Id == "wx_domain_check") + { + lines.Add(string.Empty); + lines.Add("说明:微信没有开放匿名域名状态查询官方接口;请在微信公众平台、安全中心或已授权开放平台能力中进行最终校验。"); + } + + return string.Join(Environment.NewLine, lines); + } + + private static string FormatHtmlAnchors(string content) + { + var anchors = Regex.Matches(content, @"]*>(.*?)", RegexOptions.IgnoreCase | RegexOptions.Singleline) + .Select(match => StripHtml(match.Groups[1].Value)) + .Where(text => text.Length >= 4 && text.Length <= 120) + .Distinct() + .Take(20) + .Select((text, index) => $"{index + 1}. {text}") + .ToArray(); + return anchors.Length == 0 ? Trim(content) : string.Join(Environment.NewLine, anchors); + } + + private static bool TryJson(string content, out JsonDocument document) + { + try + { + document = JsonDocument.Parse(content); + return true; + } + catch + { + document = default!; + return false; + } + } + + private static string Trim(string content) + { + return content.Length > 6000 ? content[..6000] + Environment.NewLine + "...(truncated)" : content.Trim(); + } + + private static string GetString(JsonElement element, string name) + { + if (!element.TryGetProperty(name, out var value)) + { + return string.Empty; + } + + return value.ValueKind switch + { + JsonValueKind.String => value.GetString() ?? string.Empty, + JsonValueKind.Number => value.GetRawText(), + JsonValueKind.True => "true", + JsonValueKind.False => "false", + _ => string.Empty + }; + } + + private static double GetDouble(JsonElement element, string name) + { + return element.ValueKind == JsonValueKind.Object && + element.TryGetProperty(name, out var value) && + value.ValueKind == JsonValueKind.Number + ? value.GetDouble() + : 0; + } + + private static long GetInt64(JsonElement element, string name) + { + return element.ValueKind == JsonValueKind.Object && + element.TryGetProperty(name, out var value) && + value.ValueKind == JsonValueKind.Number + ? value.GetInt64() + : 0; + } + + private static string Value(XElement item, string name) + { + return item.Elements().FirstOrDefault(element => element.Name.LocalName.Equals(name, StringComparison.OrdinalIgnoreCase))?.Value.Trim() ?? string.Empty; + } + + private static string StripHtml(string value) + { + return WebUtility.HtmlDecode(Regex.Replace(value, "<.*?>", string.Empty, RegexOptions.Singleline)).Trim(); + } + + private static string Blank(string value) + { + return string.IsNullOrWhiteSpace(value) || value == "--" ? "无" : value; + } +} + +internal sealed record TrainStation(string Name, string Code, string Pinyin); diff --git a/src/YMhut.Box.Core/Tools/RiskConfirmationStore.cs b/src/YMhut.Box.Core/Tools/RiskConfirmationStore.cs new file mode 100644 index 0000000..a03a7ae --- /dev/null +++ b/src/YMhut.Box.Core/Tools/RiskConfirmationStore.cs @@ -0,0 +1,138 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using YMhut.Box.Core.App; +using YMhut.Box.Core.Logging; + +namespace YMhut.Box.Core.Tools; + +public interface IRiskConfirmationStore +{ + Task IsRememberedAsync(string toolId, string operation, CancellationToken cancellationToken = default); + + Task RememberAsync(string toolId, string operation, CancellationToken cancellationToken = default); + + Task ResetAsync(CancellationToken cancellationToken = default); +} + +public sealed class RiskConfirmationStore(AppPaths paths, ILogService? logService = null) : IRiskConfirmationStore +{ + private readonly SemaphoreSlim _gate = new(1, 1); + private readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web) { WriteIndented = true }; + + private string StorePath => Path.Combine(paths.Data, "risk-confirmations.json"); + + public async Task IsRememberedAsync(string toolId, string operation, CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + var store = await LoadAsync(cancellationToken).ConfigureAwait(false); + return store.Items.Any(item => + string.Equals(item.ToolId, toolId, StringComparison.OrdinalIgnoreCase) && + string.Equals(item.Operation, operation, StringComparison.OrdinalIgnoreCase)); + } + finally + { + _gate.Release(); + } + } + + public async Task RememberAsync(string toolId, string operation, CancellationToken cancellationToken = default) + { + if (string.IsNullOrWhiteSpace(toolId) || string.IsNullOrWhiteSpace(operation)) + { + return; + } + + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + var store = await LoadAsync(cancellationToken).ConfigureAwait(false); + store.Items.RemoveAll(item => + string.Equals(item.ToolId, toolId, StringComparison.OrdinalIgnoreCase) && + string.Equals(item.Operation, operation, StringComparison.OrdinalIgnoreCase)); + store.Items.Add(new RiskConfirmationItem(toolId, operation, DateTimeOffset.Now)); + await SaveAsync(store, cancellationToken).ConfigureAwait(false); + await WriteLogAsync("Information", "risk", "Risk confirmation remembered", $"{toolId} / {operation}", cancellationToken).ConfigureAwait(false); + } + finally + { + _gate.Release(); + } + } + + public async Task ResetAsync(CancellationToken cancellationToken = default) + { + await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await SaveAsync(new RiskConfirmationFile(), cancellationToken).ConfigureAwait(false); + await WriteLogAsync("Information", "risk", "Risk confirmations reset", null, cancellationToken).ConfigureAwait(false); + } + finally + { + _gate.Release(); + } + } + + private async Task LoadAsync(CancellationToken cancellationToken) + { + try + { + if (!File.Exists(StorePath)) + { + return new RiskConfirmationFile(); + } + + await using var stream = File.OpenRead(StorePath); + return await JsonSerializer.DeserializeAsync(stream, _jsonOptions, cancellationToken).ConfigureAwait(false) + ?? new RiskConfirmationFile(); + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException or JsonException) + { + await WriteLogAsync("Warning", "risk", "Risk confirmation store degraded", exception.Message, cancellationToken).ConfigureAwait(false); + return new RiskConfirmationFile(); + } + } + + private async Task SaveAsync(RiskConfirmationFile store, CancellationToken cancellationToken) + { + Directory.CreateDirectory(paths.Data); + var tempPath = StorePath + ".tmp"; + await using (var stream = File.Create(tempPath)) + { + await JsonSerializer.SerializeAsync(stream, store, _jsonOptions, cancellationToken).ConfigureAwait(false); + } + + if (File.Exists(StorePath)) + { + try + { + File.Replace(tempPath, StorePath, null); + return; + } + catch (Exception exception) when (exception is IOException or UnauthorizedAccessException or PlatformNotSupportedException) + { + await WriteLogAsync("Warning", "risk", "Atomic risk confirmation save degraded", exception.Message, cancellationToken).ConfigureAwait(false); + File.Delete(StorePath); + } + } + + File.Move(tempPath, StorePath); + } + + private Task WriteLogAsync(string level, string category, string message, string? detail, CancellationToken cancellationToken) + { + return logService?.WriteAsync(level, category, message, detail, cancellationToken) ?? Task.CompletedTask; + } + + private sealed class RiskConfirmationFile + { + public List Items { get; set; } = []; + } + + private sealed record RiskConfirmationItem( + string ToolId, + string Operation, + DateTimeOffset RememberedAt); +} diff --git a/src/YMhut.Box.Core/Tools/ToolCatalog.cs b/src/YMhut.Box.Core/Tools/ToolCatalog.cs new file mode 100644 index 0000000..2c2af0f --- /dev/null +++ b/src/YMhut.Box.Core/Tools/ToolCatalog.cs @@ -0,0 +1,302 @@ +namespace YMhut.Box.Core.Tools; + +public sealed class ToolCatalog +{ + private readonly List _modules; + + public ToolCatalog(IEnumerable? modules = null) + { + var provided = modules?.ToList(); + _modules = provided is { Count: > 0 } ? provided : DefaultModules().ToList(); + } + + public IReadOnlyList Modules => _modules; + + public IEnumerable Search(string query, ToolCategory category = ToolCategory.All) + { + var normalized = query.Trim(); + var source = category == ToolCategory.All + ? _modules + : _modules.Where(module => module.Metadata.Category == category); + + if (string.IsNullOrWhiteSpace(normalized)) + { + return source; + } + + return source.Where(module => + module.Id.Contains(normalized, StringComparison.OrdinalIgnoreCase) || + module.Metadata.Name.Contains(normalized, StringComparison.OrdinalIgnoreCase) || + module.Metadata.Description.Contains(normalized, StringComparison.OrdinalIgnoreCase) || + module.Metadata.Keywords.Any(keyword => keyword.Contains(normalized, StringComparison.OrdinalIgnoreCase))); + } + + public IToolModule? GetById(string id) + { + return _modules.FirstOrDefault(module => string.Equals(module.Id, id, StringComparison.OrdinalIgnoreCase)); + } + + public static IEnumerable DefaultModules() + { + return RawToolData + .Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .Select(ParseModule) + .Concat([DevEnvironmentConfigModule()]) + .OrderBy(module => CategorySortIndex(module.Metadata.Category)) + .ThenBy(module => module.Metadata.Name, StringComparer.CurrentCulture); + } + + private static IToolModule DevEnvironmentConfigModule() + { + return new ToolModule(new ToolMetadata( + "dev_environment_config", + "开发环境配置", + "检测本机开发环境,并从官方来源下载、安装或按源码配方构建开发工具。", + ToolCategory.Dev, + ["go", "python", "java", "jdk", "docker", "mysql", "node", "dotnet", "git", "rust", "cmake", "download", "install"], + false, + IconForCategory(ToolCategory.Dev))); + } + + private static IToolModule ParseModule(string line) + { + var parts = line.Split('\t'); + var id = parts.ElementAtOrDefault(0) ?? string.Empty; + var name = parts.ElementAtOrDefault(1) ?? id; + var description = parts.ElementAtOrDefault(2) ?? string.Empty; + var category = ParseCategory(parts.ElementAtOrDefault(3)); + var offline = bool.TryParse(parts.ElementAtOrDefault(4), out var parsedOffline) && parsedOffline; + var keywords = (parts.ElementAtOrDefault(5) ?? string.Empty) + .Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToArray(); + + return new ToolModule(new ToolMetadata(id, name, description, category, keywords, offline, IconForCategory(category))); + } + + private static ToolCategory ParseCategory(string? value) + { + return value?.Trim().ToLowerInvariant() switch + { + "dev" => ToolCategory.Dev, + "plugin" => ToolCategory.Plugin, + "network" => ToolCategory.Network, + "security" => ToolCategory.Security, + "data" => ToolCategory.Data, + "calculator" => ToolCategory.Calculator, + "text" => ToolCategory.Text, + "image" => ToolCategory.Image, + "design" => ToolCategory.Design, + "life" => ToolCategory.Life, + "system" => ToolCategory.System, + _ => ToolCategory.Dev + }; + } + + private static int CategorySortIndex(ToolCategory category) + { + return category switch + { + ToolCategory.Plugin => 1, + ToolCategory.Dev => 2, + ToolCategory.Network => 3, + ToolCategory.Security => 4, + ToolCategory.Data => 5, + ToolCategory.Calculator => 6, + ToolCategory.Text => 7, + ToolCategory.Image => 8, + ToolCategory.Design => 9, + ToolCategory.Life => 10, + ToolCategory.System => 11, + _ => 0, + }; + } + + private static string IconForCategory(ToolCategory category) + { + return category switch + { + ToolCategory.Plugin => "\uECAA", + ToolCategory.Dev => "\uE943", + ToolCategory.Network => "\uE774", + ToolCategory.Security => "\uE72E", + ToolCategory.Data => "\uE9D9", + ToolCategory.Calculator => "\uE8EF", + ToolCategory.Text => "\uE8D2", + ToolCategory.Image => "\uEB9F", + ToolCategory.Design => "\uE790", + ToolCategory.Life => "\uE753", + ToolCategory.System => "\uE950", + _ => "\uE8FD", + }; + } + + private const string RawToolData = """ +html_js_playground HTML/JS 渲染器 运行 HTML、CSS、JS、引用脚本并查看输出日志 dev false html;js;webview;plugin;playground +json_formatter JSON 格式化 美化、压缩并校验 JSON 内容 dev true json;format;pretty;minify +base64_codec Base64 编解码 在 Base64 与 UTF-8 文本之间转换 dev true base64;encode;decode +compression_codec 压缩编解码 GZip、Deflate、Brotli 文本与 Base64 压缩解压 dev true compress;gzip;deflate;brotli;base64 +url_codec URL 编解码 URL 百分号编码、解码与参数检查 dev true url;encode;decode +html_entity HTML 实体转换 HTML 实体编码和解码 dev true html;entity;escape +html_minifier HTML/代码压缩 压缩 HTML、CSS 或 JavaScript 片段 dev true html;css;js;minify +regex_tool 正则测试 实时测试正则匹配结果 dev true regex;regular expression +js_obfuscator JS 代码混淆 轻量混淆 JavaScript 代码 dev true javascript;obfuscate +uuid_generator UUID 生成 生成 UUID/GUID 标识符 dev true uuid;guid +ulid_generator ULID 生成 生成唯一且可排序的 ULID 标识符 dev true ulid;id +timestamp_converter 时间戳转换 Unix 时间戳与本地时间互转 dev true timestamp;date;time +number_base 进制转换 二进制、八进制、十进制和十六进制互转 dev true base;binary;hex +number_base_converter 进制转换器 常用数字进制互转 dev true base;converter;binary;hex +csv_json_converter CSV/JSON 转换 CSV 表格与 JSON 结构互转 dev true csv;json;table +column_extractor 列提取器 按列号提取多列文本或表格字段 dev true column;csv;extract +csv_tsv_converter CSV/TSV 转换 CSV 与 TSV 快速互转 dev true csv;tsv +yaml_json_converter YAML/JSON 转换 轻量键值结构与 JSON 互转 dev true yaml;json +xml_formatter XML 格式化 校验并美化 XML 文本 dev true xml;format +sql_formatter SQL 格式化 整理常见 SQL 查询语句 dev true sql;format +unicode_codec Unicode 转义 文本与 Unicode 转义序列互转 dev true unicode;escape +punycode_codec Punycode/IDN 转换 国际化域名与 ASCII 域名互转 dev true punycode;idn +jwt_decoder JWT 解码 本地解析 JWT 头部与载荷 dev true jwt;token +query_builder Query 参数构建 URL Query 字符串与键值对互转 dev true query;url;params +form_urlencoded_builder 表单编码构建 x-www-form-urlencoded 内容生成和解析 dev true form;urlencoded +key_value_converter 键值对转换 JSON、ENV、Query 等键值格式互转 dev true kv;json;env +cron_helper Cron 辅助 校验并解释五段 Cron 表达式 dev true cron;schedule +unicode_block_lookup Unicode 区段速查 查询常见 Unicode 区段范围 dev true unicode;block +charset_lookup 字符集速查 查询常见字符集编码说明 dev true charset;encoding +regex_preset_lookup 正则预设速查 查询常见正则表达式模板 dev true regex;preset +magic_number_lookup 文件头速查 查询文件魔数和对应格式 dev true magic number;file header +html_text_extractor HTML 正文提取 移除标签并提取纯文本 dev true html;text;extract +slug_generator Slug 生成 把标题转换为 URL 友好的 slug dev true slug;url +markdown_table_normalizer Markdown 表格整理 补齐、对齐并规范化 Markdown 表格 text true markdown;table +markdown_preview Markdown 预览整理 生成 Markdown 结构摘要、纯文本预览和轻量规范化 text true markdown;preview;commonmark +json_path_helper JSON 路径辅助 从 JSON 结构中提取常见路径表达式 dev true json;path +hash_generator 哈希计算 计算 MD5、SHA1、SHA256 和 SHA512 security true hash;md5;sha +hmac_generator HMAC 生成 用密钥生成 HMAC 摘要 security true hmac;hash +totp_generator TOTP 验证码 根据 Base32 密钥本地生成一次性验证码 security true totp;otp;2fa;mfa;base32 +password_generator 密码生成 生成随机强密码 security true password;random +profanity_check 敏感词检查 检查文本中可能的敏感词 security true profanity;check +hash_manifest_builder 哈希清单生成 为文件列表生成 SHA256 清单 security true hash;manifest +hash_manifest_verify 哈希清单校验 根据清单校验文件哈希 security true hash;verify +safe_browser 安全浏览器 在应用内打开隔离 WebView2 浏览器 network false browser;webview +wx_domain_check 微信域名检测说明 打开微信官方文档入口并说明匿名检测限制 security false wechat;domain +ip_lookup IP 归属地查询 查询 IP 归属地信息 network false ip;location +ip_info IP/域名详情 查询 IP 或域名网络详情 network false ip;domain +dns_query DNS 解析 查询 A、MX、TXT 等 DNS 记录 network false dns;record +domain_price 域名官方信息 查询域名 RDAP、注册状态与官方公开信息 network false domain;rdap;whois +smart_search 聚合搜索 聚合搜索工具和常用入口 network false search;ai +rdap_domain_lookup 域名 RDAP 查询 通过 RDAP 查询域名注册信息 network false rdap;domain +rdap_ip_lookup IP/ASN RDAP 查询 通过 RDAP 查询 IP 或 ASN 信息 network false rdap;ip;asn +http_diagnostic HTTP 诊断 检查 URL 状态码、响应头和耗时 network false http;diagnostic +url_redirect_trace URL 跳转追踪 追踪 URL 重定向链、状态码和最终地址 network false url;redirect;trace;http +url_inspector URL 结构解析 解析 URL 协议、主机、路径和参数 network true url;inspect +domain_extractor 域名提取 从 URL 或文本中提取唯一域名 network true domain;extract +cidr_subnet_calculator CIDR 子网计算 计算 IPv4 CIDR 掩码与地址范围 network true cidr;subnet +mime_lookup MIME 类型速查 查询常见 MIME 类型 network true mime +http_status_lookup HTTP 状态码速查 查询常见 HTTP 状态码含义 network true http;status +port_lookup 常见端口速查 查询常见端口与服务名称 network true port;service +dns_record_lookup DNS 记录速查 查询 DNS 记录类型用途 network true dns;record +weather 天气查询 查询城市天气与空气质量 network false weather;city +hotboard 综合热榜 聚合热门榜单、新闻和实时资讯 data false hot;news +bili_hot B 站热榜 获取哔哩哔哩热门内容 data false bilibili;hot +baidu_hot 百度热搜 获取百度热搜榜 data false baidu;hot +zhihu_hot 知乎热榜 获取知乎热榜 data false zhihu;hot +cctv_news 央视新闻 获取央视新闻摘要 data false news;cctv +tech_news 科技新闻 获取科技资讯 data false news;tech +football_news 体育新闻 获取足球和体育新闻 data false football;sports +movie_box_office 电影票房 查询电影票房数据 data false movie;box office +ai_latest_news AI 全网最新资讯 按分类查看 AI 模型、工具、研究、产业和安全资讯 data false ai;news;latest +earthquake_info 地震信息 获取近期全球地震信息 data false earthquake +gold_price 黄金价格 获取黄金价格与市场摘要 data false gold;price +oil_price 成品油价格政策 查看国家发展改革委成品油价格政策发布入口 data false oil;price +train_query 列车查询 查询列车、站点和时刻信息 data false train;railway +history_today 历史上的今天 查询当天历史事件 data false history;today +city_route_query 城际路线查询 查询两个城市之间的距离、耗时、油费、过路费和路况 data false city;route;travel;cost +car_info 车辆信息 查询车辆和地区参考数据 data true car;reference +sanguosha_skin 三国杀皮肤 查询三国杀皮肤配置 data true sanguosha;skin +calculator_tool 计算器 基础四则运算和表达式计算 calculator true calculator;math +unit_converter 单位换算 常用单位换算 calculator true unit;convert +bmi_calculator BMI 计算 根据体重和身高计算 BMI calculator true bmi;health +percentage_calculator 百分比计算 计算部分值占整体的百分比 calculator true percent +percentage_change_calculator 涨跌幅计算 计算新旧数值之间的百分比变化 calculator true percent;change +discount_calculator 折扣计算 计算折后价与优惠金额 calculator true discount;price +tax_calculator 税费计算 根据税率计算含税价、税额与未税价 calculator true tax;vat +ratio_simplifier 比例化简 把整数比例化简为最简形式 calculator true ratio;gcd +average_calculator 平均值计算 计算平均值、总和与极值 calculator true average;mean +median_calculator 中位数计算 计算中位数和四分位信息 calculator true median;statistics +gcd_lcm_calculator 最大公约数/最小公倍数 计算多个整数的 GCD 与 LCM calculator true gcd;lcm +permutation_calculator 排列数计算 计算 P(n,r) 排列数量 calculator true permutation +combination_calculator 组合数计算 计算 C(n,r) 组合数量 calculator true combination +date_difference_calculator 日期差计算 计算两个日期之间的时间差 life true date;difference +workday_calculator 工作日统计 统计两个日期之间的工作日 life true workday;calendar +age_calculator 年龄计算 根据出生日期计算年龄 life true age;birthday +simple_interest_calculator 单利计算 根据本金、利率和期限计算单利 calculator true interest;simple +compound_interest_calculator 复利计算 计算复利终值 calculator true interest;compound +loan_emi_calculator 等额本息月供 计算贷款月供、总还款和利息 calculator true loan;emi +length_converter 长度换算 米、千米、英尺等长度单位换算 calculator true length;unit +weight_converter 重量换算 千克、克、磅等重量单位换算 calculator true weight;mass +temperature_converter 温度换算 摄氏度、华氏度和开尔文互转 calculator true temperature +speed_converter 速度换算 米每秒、千米每小时等速度换算 calculator true speed +area_converter 面积换算 平方米、公顷、亩等面积换算 calculator true area +volume_converter 体积换算 立方米、升、加仑等体积换算 calculator true volume +data_size_converter 数据容量换算 B、KB、MB、GB、TB 互转 calculator true data size +time_duration_converter 时长换算 秒、分钟、小时、天和周互转 calculator true duration +text_statistics 文本统计 统计字数、行数和字符分布 text true text;statistics +case_converter 大小写转换 转换大小写、标题和命名风格 text true case +chinese_converter 简繁转换 简体和繁体中文基础互转 text true chinese +text_diff 文本对比 对比两段文本差异 text true diff +random_generator 随机生成器 生成随机字符串、数字或颜色 text true random +ai_translation AI 翻译 保留 AI 翻译入口和配置位 text false translate;ai +ymhut_uutool_suite UU 工具合集 聚合常用小工具入口 text true uutool;suite +text_deduplicator 文本去重 按行去重并保留顺序 text true dedupe +text_sorter 文本排序 按字典序排序多行文本 text true sort +text_filter 文本过滤 按关键字或正则筛选文本 text true filter +delimiter_converter 分隔符转换 在逗号、制表符和换行之间转换 text true delimiter +filename_formatter 文件名格式化 批量清洗文件名和路径名 text true filename +text_splitter 文本拆分 按分隔符批量拆分文本 text true split +text_merger 文本合并 合并多行文本 text true merge +text_numbering 文本编号 为每行添加递增序号 text true numbering +blank_line_cleaner 空行清理 移除空白行 text true blank line +whitespace_normalizer 空白规范化 清理多余空格和连续空白 text true whitespace +prefix_suffix_batch 前后缀批处理 按行追加统一前缀或后缀 text true prefix;suffix +line_column_transformer 行列互转 多行文本与分隔列表互转 text true line;column +list_sampler 列表抽样 从列表中随机抽取项目 text true sample +list_chunker 列表分块 按固定大小切分长列表 text true chunk +list_intersection 列表交集 输出两个列表的交集 text true intersection +list_union 列表并集 合并两个列表并去重 text true union +list_difference 列表差集 输出左侧列表独有项目 text true difference +list_group_statistics 列表分组统计 统计列表项目出现次数 text true group;count +duplicate_detector 重复项检测 输出重复出现的内容和次数 text true duplicate +line_length_analyzer 行长度分析 分析每行长度与整体分布 text true line length +keyword_counter 关键词计数 统计关键词命中次数 text true keyword;count +template_filler 模板填充 用键值对替换模板占位符 text true template +path_normalizer 路径规范化 批量标准化文件或目录路径 text true path;normalize +path_breakdown 路径拆解 拆解目录、文件名和扩展名 text true path +extension_extractor 扩展名提取 从路径列表提取扩展名 text true extension +extension_statistics 扩展名统计 统计扩展名分布 text true extension;statistics +file_manifest_builder 文件清单生成 生成路径、大小和时间清单 text true manifest +batch_rename_preview 批量重命名预览 预览批量重命名结果 text true rename +indexed_rename_preview 序号重命名预览 按前缀和序号预览命名方案 text true rename;index +manifest_deduplicator 清单去重 清单或路径列表去重 text true manifest;dedupe +manifest_sorter 清单排序 清单或路径列表排序 text true manifest;sort +manifest_template_export 清单模板导出 输出 JSON、CSV、TSV 清单模板 text true manifest;template +text_trimmer 文本裁剪 按行去除前后空白 text true trim +text_reverser 文本反转 反转字符、单词或整行顺序 text true reverse +markdown_list_cleaner Markdown 列表清理 规范 Markdown 列表项目 text true markdown;list +line_pair_zipper 双列合并 将两组逐行数据按顺序配对 text true zip;pair +ascii_art ASCII 艺术 生成简单 ASCII 艺术文本 design true ascii;art +color_picker 颜色工具 解析、转换并展示颜色值 design true color;hex;rgb +color_contrast_checker 颜色对比度 计算前景与背景色的 WCAG 对比度 design true color;contrast;wcag;accessibility +qr_generator 二维码生成 根据文本生成二维码 image true qr;qrcode +qrcode_scanner 二维码扫描 识别图片中的二维码 image true qr;scan +archive_tool 归档工具 创建、查看或解包 zip/tar 归档 image true archive;zip;tar +image_processor 图片处理 查看图片信息并进行基础处理 image true image;resize +image_metadata_inspector 图片元数据 读取本地图片格式、尺寸、体积和基础元数据 image true image;metadata;exif;dimensions +media_player 媒体播放器 沉浸式本地影音播放 image false media;player +qq_avatar QQ 资料查询 查询 QQ 头像与公开资料 life false qq;avatar;profile +random_cinema 随机播放室 远程随机图片和随机视频播放 image false movie;random;image;video +shelf_life 保质期计算 按生产日期和保质期计算到期时间 life true shelf life +timezone_abbr_lookup 时区缩写速查 查询时区缩写和 UTC 偏移 life true timezone;utc +system_info 系统信息 查看 CPU、内存、系统和运行状态 system true system;info +system_tool 系统工具 常用系统操作和维护入口 system true system;tool +serial_terminal 串口终端 枚举 COM 口并支持文本、HEX、定时发送和接收日志 system false serial;com;uart;terminal +pc_benchmark 性能跑分 执行轻量性能测试 system true benchmark +"""; +} diff --git a/src/YMhut.Box.Core/Tools/ToolCategory.cs b/src/YMhut.Box.Core/Tools/ToolCategory.cs new file mode 100644 index 0000000..c6702a4 --- /dev/null +++ b/src/YMhut.Box.Core/Tools/ToolCategory.cs @@ -0,0 +1,17 @@ +namespace YMhut.Box.Core.Tools; + +public enum ToolCategory +{ + All, + Plugin, + Dev, + Network, + Security, + Data, + Calculator, + Text, + Image, + Design, + Life, + System +} diff --git a/src/YMhut.Box.Core/Tools/ToolExecutor.cs b/src/YMhut.Box.Core/Tools/ToolExecutor.cs new file mode 100644 index 0000000..3548006 --- /dev/null +++ b/src/YMhut.Box.Core/Tools/ToolExecutor.cs @@ -0,0 +1,3395 @@ +using System.Globalization; +using System.Drawing; +using System.Drawing.Imaging; +using System.Formats.Tar; +using System.IO.Compression; +using System.Net; +using System.Runtime.InteropServices; +using System.Runtime.Versioning; +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using System.Text.RegularExpressions; +using System.Xml.Linq; +using Microsoft.VisualBasic; +using QRCoder; +using YMhut.Box.Core; +using YMhut.Box.Core.Api; +using YMhut.Box.Core.Data; +using YMhut.Box.Core.Media; +using ZXing; +using ZXing.Common; +using RuntimeInformation = global::System.Runtime.InteropServices.RuntimeInformation; + +namespace YMhut.Box.Core.Tools; + +public sealed record ToolExecutionResult(bool Ok, string Output, string? Error = null, ToolResultDocument? Document = null) +{ + public static ToolExecutionResult Success(string output) => new(true, output, null); + + public static ToolExecutionResult Success(string output, ToolResultDocument document) => new(true, output, null, document); + + public static ToolExecutionResult Fail(string error) => new(false, string.Empty, error); +} + +public static class ToolExecutor +{ + private static readonly IReadOnlyDictionary SimplifiedToTraditional = BuildChineseMap(); + private static readonly IReadOnlyDictionary TraditionalToSimplified = SimplifiedToTraditional.ToDictionary(pair => pair.Value, pair => pair.Key); + + private static bool English(string? language) => string.Equals(language, "en-US", StringComparison.OrdinalIgnoreCase); + + private static string T(string? language, string zh, string en) => English(language) ? en : zh; + + public static async Task ExecuteAsync( + IToolModule module, + string input, + CancellationToken cancellationToken = default, + IApiManager? apiManager = null, + IReferenceDataService? referenceDataService = null, + string language = "zh-CN") + { + try + { + var result = module.Id switch + { + "json_formatter" => Json(input), + "base64_codec" => Base64(input), + "compression_codec" => CompressionCodec(input), + "url_codec" or "query_builder" => Url(input), + "html_entity" => ToolExecutionResult.Success(WebUtility.HtmlEncode(input) + Environment.NewLine + Environment.NewLine + WebUtility.HtmlDecode(input)), + "html_minifier" or "js_obfuscator" => Minify(input), + "xml_formatter" => Xml(input), + "hash_generator" => Hashes(input), + "hmac_generator" => Hmac(input), + "totp_generator" => Totp(input), + "password_generator" => Password(input), + "uuid_generator" => GenerateIds(input, ulid: false), + "ulid_generator" => GenerateIds(input, ulid: true), + "random_generator" or "random_picker" => RandomText(input), + "timestamp_converter" or "date_time_calculator" => Timestamp(input), + "number_base" or "number_base_converter" => NumberBase(input), + "unit_converter" or "length_converter" or "weight_converter" or "temperature_converter" or "speed_converter" or "area_converter" or "volume_converter" or "data_size_converter" or "time_duration_converter" => Unit(module.Id, input), + "calculator_tool" or "percentage_calculator" or "percentage_change_calculator" or "discount_calculator" or "tax_calculator" or "ratio_simplifier" or "average_calculator" or "median_calculator" or "gcd_lcm_calculator" or "permutation_calculator" or "combination_calculator" or "simple_interest_calculator" or "loan_emi_calculator" or "compound_interest_calculator" => Calculator(module.Id, input), + "bmi_calculator" => Bmi(input), + "regex_tool" => RegexMatch(input), + "case_converter" or "slug_generator" => Case(input), + "chinese_converter" => Chinese(input), + "text_diff" => TextDiff(input), + "text_deduplicator" or "duplicate_detector" => Deduplicate(input), + "text_sorter" => SortLines(input), + "text_filter" => FilterLines(input), + "text_statistics" or "line_length_analyzer" or "keyword_counter" or "list_group_statistics" => TextStatistics(input), + "delimiter_converter" or "line_column_transformer" or "csv_tsv_converter" => Delimiters(input), + "column_extractor" => Column(input), + "csv_json_converter" => CsvJson(input), + "yaml_json_converter" => KeyValueToJson(input), + "markdown_table_normalizer" => MarkdownTable(input), + "markdown_preview" => MarkdownPreview(input), + "json_path_helper" => JsonPaths(input), + "key_value_converter" or "form_urlencoded_builder" => KeyValueToJson(input), + "sql_formatter" => Sql(input), + "hash_manifest_builder" => HashManifest(input), + "hash_manifest_verify" => VerifyHashManifest(input), + "batch_text_replace" => ReplaceText(input), + "filename_formatter" => FileNames(input), + "color_picker" => ColorTool(input), + "color_contrast_checker" => ColorContrast(input), + "qr_generator" => QrGenerate(input), + "qrcode_scanner" => QrScan(input), + "archive_tool" => Archive(input), + "image_processor" => ImageTool(input), + "image_metadata_inspector" => ImageMetadata(input), + "ascii_art" => AsciiArt(input), + "profanity_check" => Profanity(input), + "unicode_codec" => Unicode(input), + "punycode_codec" => Punycode(input), + "jwt_decoder" => Jwt(input), + "text_splitter" => ToolExecutionResult.Success(string.Join(Environment.NewLine, SplitTokens(input))), + "text_merger" => ToolExecutionResult.Success(string.Join(" ", Lines(input))), + "text_numbering" => ToolExecutionResult.Success(string.Join(Environment.NewLine, Lines(input).Select((line, index) => $"{index + 1}. {line}"))), + "blank_line_cleaner" => ToolExecutionResult.Success(string.Join(Environment.NewLine, Lines(input))), + "whitespace_normalizer" => ToolExecutionResult.Success(Regex.Replace(input.Trim(), @"\s+", " ")), + "prefix_suffix_batch" => ToolExecutionResult.Success(string.Join(Environment.NewLine, Lines(input).Select(line => $"prefix_{line}_suffix"))), + "list_sampler" => ToolExecutionResult.Success(string.Join(Environment.NewLine, Lines(input).OrderBy(_ => Random.Shared.Next()).Take(5))), + "list_chunker" => Chunk(input), + "list_intersection" => SetOperation(input, "intersection"), + "list_union" => SetOperation(input, "union"), + "list_difference" => SetOperation(input, "difference"), + "template_filler" => Template(input), + "path_normalizer" => PathNormalize(input), + "path_breakdown" => PathBreakdown(input), + "extension_extractor" => ExtensionExtract(input), + "extension_statistics" => ExtensionStats(input), + "file_manifest_builder" or "manifest_template_export" => Manifest(input), + "manifest_deduplicator" => Deduplicate(input), + "manifest_sorter" => SortLines(input), + "batch_rename_preview" => RenamePreview(input, indexed: false), + "indexed_rename_preview" => RenamePreview(input, indexed: true), + "text_trimmer" => ToolExecutionResult.Success(string.Join(Environment.NewLine, input.Replace("\r\n", "\n").Split('\n').Select(line => line.Trim()))), + "text_reverser" => ToolExecutionResult.Success(new string(input.Reverse().ToArray())), + "markdown_list_cleaner" => MarkdownList(input), + "html_text_extractor" => ToolExecutionResult.Success(Regex.Replace(input, "<.*?>", string.Empty, RegexOptions.Singleline).Trim()), + "url_inspector" => UrlInspect(input), + "url_redirect_trace" => await UrlRedirectTraceAsync(input, cancellationToken, language).ConfigureAwait(false), + "domain_extractor" => DomainExtract(input), + "cidr_subnet_calculator" => Cidr(input), + "line_pair_zipper" => PairZip(input), + "date_difference_calculator" or "workday_calculator" or "age_calculator" => DateTool(module.Id, input), + "cron_helper" => Cron(input), + "mime_lookup" or "http_status_lookup" or "port_lookup" or "dns_record_lookup" or "unicode_block_lookup" or "charset_lookup" or "timezone_abbr_lookup" or "regex_preset_lookup" or "magic_number_lookup" => await ReferenceLookupAsync(referenceDataService, module.Id, input, cancellationToken, language).ConfigureAwait(false), + "car_info" => await CarInfoAsync(referenceDataService, input, cancellationToken).ConfigureAwait(false), + "sanguosha_skin" => await SanguoshaSkinAsync(referenceDataService, input, cancellationToken).ConfigureAwait(false), + "qq_avatar" => await QqProfileAsync(input, cancellationToken, apiManager, language).ConfigureAwait(false), + "shelf_life" => ShelfLife(input), + "random_cinema" => await RandomCinemaAsync(input, cancellationToken, apiManager, language).ConfigureAwait(false), + "smart_search" => SmartSearch(input, language), + "ymhut_uutool_suite" => UuToolSuite(input), + "system_info" => SystemInfo(), + "pc_benchmark" => Benchmark(), + "system_tool" => SystemTool(input, language), + "safe_browser" or "media_player" or "html_js_playground" or "serial_terminal" => NativeTool(module), + "ai_translation" => AiTranslation(input), + "ai_latest_news" => await AiLatestNewsAsync(input, cancellationToken, apiManager, language).ConfigureAwait(false), + "city_route_query" => await CityRouteQueryAsync(input, cancellationToken, apiManager, language).ConfigureAwait(false), + "weather" => await WeatherAsync(input, cancellationToken, apiManager, language).ConfigureAwait(false), + "train_query" => await TrainQueryAsync(input, cancellationToken, apiManager, language).ConfigureAwait(false), + _ => await ExecuteKnownRemoteOrDescribeAsync(module, input, cancellationToken, apiManager, language).ConfigureAwait(false) + }; + return result.Ok && result.Document is null + ? result with { Document = ToolResultBuilder.FromOutput(module, result.Output, language) } + : result; + } + catch (Exception exception) + { + return ToolExecutionResult.Fail(SensitiveText.Sanitize(exception.Message)); + } + } + + private static ToolExecutionResult Json(string input) + { + using var document = JsonDocument.Parse(input); + return ToolExecutionResult.Success(JsonSerializer.Serialize(document.RootElement, new JsonSerializerOptions { WriteIndented = true })); + } + + private static ToolExecutionResult Base64(string input) + { + var encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(input)); + var output = $"Encode:{Environment.NewLine}{encoded}"; + try + { + var decoded = Encoding.UTF8.GetString(Convert.FromBase64String(input.Trim())); + output += $"{Environment.NewLine}{Environment.NewLine}Decode:{Environment.NewLine}{decoded}"; + } + catch (global::System.FormatException) + { + } + + return ToolExecutionResult.Success(output); + } + + private static ToolExecutionResult CompressionCodec(string input) + { + var normalized = input.Replace("\r\n", "\n"); + var split = normalized.Split('\n', 2); + var command = split[0].Trim().ToLowerInvariant(); + var hasCommand = command.Contains("encode") || command.Contains("decode") || + command.Contains("compress") || command.Contains("decompress") || + command.Contains("gzip") || command.Contains("deflate") || command.Contains("brotli"); + var payload = hasCommand && split.Length > 1 ? split[1] : input; + var algorithm = command.Contains("brotli") ? "brotli" : command.Contains("deflate") ? "deflate" : "gzip"; + var decode = command.Contains("decode") || command.Contains("decompress"); + + if (decode) + { + byte[] compressed; + try + { + compressed = Convert.FromBase64String(payload.Trim()); + } + catch (global::System.FormatException) + { + return ToolExecutionResult.Fail("请输入 Base64 压缩数据。"); + } + + using var inputStream = new MemoryStream(compressed); + using var codec = OpenDecompressionStream(inputStream, algorithm); + using var output = new MemoryStream(); + codec.CopyTo(output); + return ToolExecutionResult.Success(Encoding.UTF8.GetString(output.ToArray())); + } + + var bytes = Encoding.UTF8.GetBytes(payload); + using var target = new MemoryStream(); + using (var codec = OpenCompressionStream(target, algorithm)) + { + codec.Write(bytes, 0, bytes.Length); + } + + var compressedBytes = target.ToArray(); + var ratio = bytes.Length == 0 ? 0 : (double)compressedBytes.Length / bytes.Length; + return ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + $"Algorithm: {algorithm}", + $"Original bytes: {bytes.Length}", + $"Compressed bytes: {compressedBytes.Length}", + $"Ratio: {ratio:0.###}", + $"Base64: {Convert.ToBase64String(compressedBytes)}" + })); + } + + private static Stream OpenCompressionStream(Stream target, string algorithm) + { + return algorithm switch + { + "brotli" => new BrotliStream(target, CompressionLevel.Optimal, leaveOpen: true), + "deflate" => new DeflateStream(target, CompressionLevel.Optimal, leaveOpen: true), + _ => new GZipStream(target, CompressionLevel.Optimal, leaveOpen: true) + }; + } + + private static Stream OpenDecompressionStream(Stream source, string algorithm) + { + return algorithm switch + { + "brotli" => new BrotliStream(source, CompressionMode.Decompress, leaveOpen: true), + "deflate" => new DeflateStream(source, CompressionMode.Decompress, leaveOpen: true), + _ => new GZipStream(source, CompressionMode.Decompress, leaveOpen: true) + }; + } + + private static ToolExecutionResult Url(string input) + { + return ToolExecutionResult.Success( + $"Encode:{Environment.NewLine}{Uri.EscapeDataString(input)}{Environment.NewLine}{Environment.NewLine}Decode:{Environment.NewLine}{Uri.UnescapeDataString(input)}"); + } + + private static ToolExecutionResult Minify(string input) + { + var withoutBlockComments = Regex.Replace(input, @"/\*.*?\*/", string.Empty, RegexOptions.Singleline); + var withoutLineComments = Regex.Replace(withoutBlockComments, @"^\s*//.*$", string.Empty, RegexOptions.Multiline); + return ToolExecutionResult.Success(Regex.Replace(withoutLineComments, @">\s+<", "><").Trim()); + } + + private static ToolExecutionResult Xml(string input) + { + var document = XDocument.Parse(input); + return ToolExecutionResult.Success(document.ToString()); + } + + private static ToolExecutionResult Hashes(string input) + { + var bytes = Encoding.UTF8.GetBytes(input); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + $"MD5: {Hex(MD5.HashData(bytes))}", + $"SHA1: {Hex(SHA1.HashData(bytes))}", + $"SHA256: {Hex(SHA256.HashData(bytes))}", + $"SHA512: {Hex(SHA512.HashData(bytes))}" + })); + } + + private static ToolExecutionResult Hmac(string input) + { + var lines = input.Replace("\r\n", "\n").Split('\n', 2); + var key = lines.Length > 1 ? lines[0] : "ymhut"; + var payload = lines.Length > 1 ? lines[1] : input; + using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key)); + return ToolExecutionResult.Success(Hex(hmac.ComputeHash(Encoding.UTF8.GetBytes(payload)))); + } + + private static ToolExecutionResult Totp(string input) + { + var lines = Lines(input).ToArray(); + var secret = lines.FirstOrDefault(line => !line.Contains('=')) ?? "JBSWY3DPEHPK3PXP"; + var options = lines + .Select(line => line.Split('=', 2)) + .Where(parts => parts.Length == 2) + .ToDictionary(parts => parts[0].Trim(), parts => parts[1].Trim(), StringComparer.OrdinalIgnoreCase); + var period = options.TryGetValue("period", out var periodText) && int.TryParse(periodText, out var parsedPeriod) + ? Math.Clamp(parsedPeriod, 15, 120) + : 30; + var digits = options.TryGetValue("digits", out var digitsText) && int.TryParse(digitsText, out var parsedDigits) + ? Math.Clamp(parsedDigits, 6, 8) + : 6; + var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); + if (options.TryGetValue("time", out var timeText) && long.TryParse(timeText, out var parsedTime)) + { + timestamp = parsedTime; + } + + byte[] keyBytes; + try + { + keyBytes = DecodeBase32(secret); + } + catch (global::System.FormatException exception) + { + return ToolExecutionResult.Fail(exception.Message); + } + + var counter = timestamp / period; + var counterBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(counter)); + using var hmac = new HMACSHA1(keyBytes); + var hash = hmac.ComputeHash(counterBytes); + var offset = hash[^1] & 0x0f; + var binary = + ((hash[offset] & 0x7f) << 24) | + ((hash[offset + 1] & 0xff) << 16) | + ((hash[offset + 2] & 0xff) << 8) | + (hash[offset + 3] & 0xff); + var modulo = (int)Math.Pow(10, digits); + var code = (binary % modulo).ToString(new string('0', digits), CultureInfo.InvariantCulture); + var remaining = period - (timestamp % period); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + $"Code: {code}", + $"Digits: {digits}", + $"Period: {period}s", + $"Remaining: {remaining}s", + $"Counter: {counter}" + })); + } + + private static byte[] DecodeBase32(string secret) + { + const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; + var clean = Regex.Replace(secret.ToUpperInvariant(), @"[\s=\-]", string.Empty); + if (clean.Length == 0) + { + throw new global::System.FormatException("请输入 Base32 密钥。"); + } + + var bytes = new List(); + var buffer = 0; + var bitsLeft = 0; + foreach (var ch in clean) + { + var value = alphabet.IndexOf(ch); + if (value < 0) + { + throw new global::System.FormatException("Base32 密钥包含无效字符。"); + } + + buffer = (buffer << 5) | value; + bitsLeft += 5; + if (bitsLeft >= 8) + { + bytes.Add((byte)((buffer >> (bitsLeft - 8)) & 0xff)); + bitsLeft -= 8; + } + } + + return bytes.ToArray(); + } + + private static ToolExecutionResult Password(string input) + { + var length = int.TryParse(input.Trim(), out var requested) ? Math.Clamp(requested, 8, 128) : 24; + const string alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789!@#$%^&*"; + return ToolExecutionResult.Success(RandomNumberGenerator.GetString(alphabet, length)); + } + + private static string Ulid() + { + var time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + Span bytes = stackalloc byte[16]; + RandomNumberGenerator.Fill(bytes); + bytes[0] = (byte)(time >> 40); + bytes[1] = (byte)(time >> 32); + bytes[2] = (byte)(time >> 24); + bytes[3] = (byte)(time >> 16); + bytes[4] = (byte)(time >> 8); + bytes[5] = (byte)time; + const string crockford = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; + var value = global::System.Numerics.BigInteger.Abs(new global::System.Numerics.BigInteger(bytes, isUnsigned: true, isBigEndian: true)); + var chars = new char[26]; + for (var i = 25; i >= 0; i--) + { + chars[i] = crockford[(int)(value % 32)]; + value /= 32; + } + return new string(chars); + } + + private static ToolExecutionResult GenerateIds(string input, bool ulid) + { + var count = int.TryParse(input.Trim(), out var requested) + ? Math.Clamp(requested, 1, 100) + : 1; + var values = Enumerable.Range(0, count) + .Select(_ => ulid ? Ulid() : $"{Guid.NewGuid():D}"); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, values)); + } + + private static ToolExecutionResult RandomText(string input) + { + var length = int.TryParse(input.Trim(), out var requested) ? Math.Clamp(requested, 1, 4096) : 32; + return ToolExecutionResult.Success(RandomNumberGenerator.GetString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", length)); + } + + private static ToolExecutionResult Timestamp(string input) + { + if (long.TryParse(input.Trim(), out var unix)) + { + var seconds = unix > 9_999_999_999 ? unix / 1000 : unix; + return ToolExecutionResult.Success(DateTimeOffset.FromUnixTimeSeconds(seconds).ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss zzz", CultureInfo.InvariantCulture)); + } + + return ToolExecutionResult.Success($"{DateTimeOffset.Now:yyyy-MM-dd HH:mm:ss zzz}{Environment.NewLine}Unix seconds: {DateTimeOffset.Now.ToUnixTimeSeconds()}"); + } + + private static ToolExecutionResult NumberBase(string input) + { + var text = input.Trim(); + var value = text.StartsWith("0x", StringComparison.OrdinalIgnoreCase) + ? Convert.ToInt64(text[2..], 16) + : Convert.ToInt64(text); + return ToolExecutionResult.Success($"DEC: {value}{Environment.NewLine}HEX: 0x{value:X}{Environment.NewLine}BIN: {Convert.ToString(value, 2)}{Environment.NewLine}OCT: {Convert.ToString(value, 8)}"); + } + + private static ToolExecutionResult Unit(string id, string input) + { + var value = double.TryParse(input.Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, out var parsed) ? parsed : 1d; + return id switch + { + "length_converter" => ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + $"m: {value:0.####}", + $"km: {value / 1000:0.####}", + $"cm: {value * 100:0.####}", + $"mm: {value * 1000:0.####}", + $"inch: {value / 0.0254:0.####}", + $"ft: {value / 0.3048:0.####}", + $"mile: {value / 1609.344:0.####}" + })), + "weight_converter" => ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + $"kg: {value:0.####}", + $"g: {value * 1000:0.####}", + $"t: {value / 1000:0.####}", + $"lb: {value * 2.2046226218:0.####}", + $"oz: {value * 35.27396195:0.####}" + })), + "temperature_converter" => ToolExecutionResult.Success($"C: {value:0.###}{Environment.NewLine}F: {(value * 9 / 5) + 32:0.###}{Environment.NewLine}K: {value + 273.15:0.###}"), + "speed_converter" => ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + $"m/s: {value:0.####}", + $"km/h: {value * 3.6:0.####}", + $"mph: {value * 2.2369362921:0.####}", + $"kn: {value * 1.9438444924:0.####}" + })), + "area_converter" => ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + $"m²: {value:0.####}", + $"km²: {value / 1_000_000:0.####}", + $"ha: {value / 10_000:0.####}", + $"亩: {value / 666.6666667:0.####}", + $"ft²: {value * 10.763910417:0.####}" + })), + "volume_converter" => ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + $"m³: {value:0.####}", + $"L: {value * 1000:0.####}", + $"mL: {value * 1_000_000:0.####}", + $"US gal: {value * 264.17205236:0.####}" + })), + "data_size_converter" => ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + $"B: {value:0.###}", + $"KiB: {value / 1024:0.###}", + $"MiB: {value / 1024 / 1024:0.###}", + $"GiB: {value / 1024 / 1024 / 1024:0.###}", + $"KB: {value / 1000:0.###}", + $"MB: {value / 1000 / 1000:0.###}", + $"GB: {value / 1000 / 1000 / 1000:0.###}" + })), + "time_duration_converter" => ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + $"seconds: {value:0.####}", + $"minutes: {value / 60:0.####}", + $"hours: {value / 3600:0.####}", + $"days: {value / 86400:0.####}", + $"weeks: {value / 604800:0.####}" + })), + _ => ToolExecutionResult.Success($"Input: {value:0.###}{Environment.NewLine}Metric x1000: {value * 1000:0.###}{Environment.NewLine}Metric /1000: {value / 1000:0.###}") + }; + } + + private static ToolExecutionResult Calculator(string id, string input) + { + var numbers = Numbers(input).ToArray(); + if (id == "percentage_calculator") + { + var part = NumberAt(numbers, 0, 25); + var whole = NumberAt(numbers, 1, 100); + return ToolExecutionResult.Success($"{part / whole * 100:0.####}%"); + } + + if (id == "percentage_change_calculator") + { + var oldValue = NumberAt(numbers, 0, 100); + var newValue = NumberAt(numbers, 1, 120); + return ToolExecutionResult.Success($"{(newValue - oldValue) / oldValue * 100:0.####}%"); + } + + if (id == "discount_calculator") + { + var price = NumberAt(numbers, 0, 100); + var discount = NumberAt(numbers, 1, 80); + var final = price * discount / 100; + return ToolExecutionResult.Success($"Final: {final:0.00}{Environment.NewLine}Saved: {price - final:0.00}"); + } + + if (id == "tax_calculator") + { + var price = NumberAt(numbers, 0, 100); + var rate = NumberAt(numbers, 1, 13) / 100; + return ToolExecutionResult.Success($"Tax: {price * rate:0.00}{Environment.NewLine}With tax: {price * (1 + rate):0.00}{Environment.NewLine}Before tax: {price / (1 + rate):0.00}"); + } + + if (id == "ratio_simplifier") + { + var a = (long)NumberAt(numbers, 0, 16); + var b = (long)NumberAt(numbers, 1, 9); + var gcd = Gcd(Math.Abs(a), Math.Abs(b)); + return ToolExecutionResult.Success($"{a / gcd}:{b / gcd}"); + } + + if (id is "average_calculator" or "median_calculator") + { + var values = numbers.Length == 0 ? [0d] : numbers.Order().ToArray(); + var average = values.Average(); + var median = values.Length % 2 == 1 + ? values[values.Length / 2] + : (values[(values.Length / 2) - 1] + values[values.Length / 2]) / 2; + return ToolExecutionResult.Success($"Count: {values.Length}{Environment.NewLine}Sum: {values.Sum():0.####}{Environment.NewLine}Average: {average:0.####}{Environment.NewLine}Median: {median:0.####}{Environment.NewLine}Min: {values.First():0.####}{Environment.NewLine}Max: {values.Last():0.####}"); + } + + if (id == "gcd_lcm_calculator") + { + var values = numbers.Select(value => Math.Abs((long)value)).Where(value => value > 0).DefaultIfEmpty(1).ToArray(); + var gcd = values.Aggregate(Gcd); + var lcm = values.Aggregate((left, right) => left / Gcd(left, right) * right); + return ToolExecutionResult.Success($"GCD: {gcd}{Environment.NewLine}LCM: {lcm}"); + } + + if (id is "permutation_calculator" or "combination_calculator") + { + var n = (int)NumberAt(numbers, 0, 10); + var r = (int)NumberAt(numbers, 1, 3); + var permutation = Factorial(n) / Factorial(Math.Max(0, n - r)); + var combination = permutation / Factorial(r); + return ToolExecutionResult.Success(id == "permutation_calculator" ? $"P({n},{r}) = {permutation}" : $"C({n},{r}) = {combination}"); + } + + if (id == "simple_interest_calculator") + { + var principal = NumberAt(numbers, 0, 10000); + var rate = NumberAt(numbers, 1, 3.5) / 100; + var years = NumberAt(numbers, 2, 1); + var interest = principal * rate * years; + return ToolExecutionResult.Success($"Interest: {interest:0.00}{Environment.NewLine}Total: {principal + interest:0.00}"); + } + + if (id == "compound_interest_calculator") + { + var principal = NumberAt(numbers, 0, 10000); + var rate = NumberAt(numbers, 1, 3.5) / 100; + var years = NumberAt(numbers, 2, 3); + var times = NumberAt(numbers, 3, 12); + var total = principal * Math.Pow(1 + rate / times, times * years); + return ToolExecutionResult.Success($"Future value: {total:0.00}{Environment.NewLine}Interest: {total - principal:0.00}"); + } + + if (id == "loan_emi_calculator") + { + var principal = NumberAt(numbers, 0, 100000); + var annualRate = NumberAt(numbers, 1, 4.5) / 100 / 12; + var months = (int)NumberAt(numbers, 2, 360); + var emi = principal * annualRate * Math.Pow(1 + annualRate, months) / (Math.Pow(1 + annualRate, months) - 1); + return ToolExecutionResult.Success($"Monthly payment: {emi:0.00}{Environment.NewLine}Total: {emi * months:0.00}{Environment.NewLine}Interest: {emi * months - principal:0.00}"); + } + + var expression = input.Trim(); + var allowed = Regex.Replace(expression, @"[^0-9+\-*/(). ]", string.Empty); + var result = new global::System.Data.DataTable().Compute(allowed, null); + return ToolExecutionResult.Success(Convert.ToString(result, CultureInfo.InvariantCulture) ?? string.Empty); + } + + private static ToolExecutionResult Bmi(string input) + { + var numbers = Numbers(input).ToArray(); + var weight = NumberAt(numbers, 0, 70); + var height = NumberAt(numbers, 1, 1.75); + if (height > 3) height /= 100; + var bmi = weight / (height * height); + return ToolExecutionResult.Success($"BMI: {bmi:0.0}"); + } + + private static ToolExecutionResult RegexMatch(string input) + { + var parts = input.Replace("\r\n", "\n").Split('\n', 2); + var pattern = parts.ElementAtOrDefault(0) ?? string.Empty; + var text = parts.ElementAtOrDefault(1) ?? string.Empty; + return ToolExecutionResult.Success(string.Join(Environment.NewLine, Regex.Matches(text, pattern).Select(match => match.Value))); + } + + private static ToolExecutionResult Case(string input) + { + var words = Regex.Matches(input, @"[A-Za-z0-9]+").Select(match => match.Value).ToArray(); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + input.ToUpperInvariant(), + input.ToLowerInvariant(), + string.Join("-", words).ToLowerInvariant(), + string.Join("_", words).ToLowerInvariant(), + string.Concat(words.Select(word => CultureInfo.InvariantCulture.TextInfo.ToTitleCase(word.ToLowerInvariant()))) + })); + } + + private static ToolExecutionResult Chinese(string input) + { + var text = input.Trim(); + if (string.IsNullOrWhiteSpace(text)) + { + return ToolExecutionResult.Fail("请输入需要转换的中文文本。"); + } + + var simplified = ConvertChinese(text, toTraditional: false); + var traditional = ConvertChinese(text, toTraditional: true); + return ToolExecutionResult.Success($"简体:{simplified}{Environment.NewLine}{Environment.NewLine}繁体:{traditional}"); + } + + private static ToolExecutionResult TextDiff(string input) + { + var parts = input.Replace("\r\n", "\n").Split("\n---\n", 2, StringSplitOptions.None); + if (parts.Length < 2) + { + return ToolExecutionResult.Fail("请用单独一行 --- 分隔左右两段文本。"); + } + + var left = parts[0].Split('\n'); + var right = parts[1].Split('\n'); + var dp = new int[left.Length + 1, right.Length + 1]; + for (var i = left.Length - 1; i >= 0; i--) + { + for (var j = right.Length - 1; j >= 0; j--) + { + dp[i, j] = string.Equals(left[i], right[j], StringComparison.Ordinal) + ? dp[i + 1, j + 1] + 1 + : Math.Max(dp[i + 1, j], dp[i, j + 1]); + } + } + + var output = new List(); + var x = 0; + var y = 0; + while (x < left.Length && y < right.Length) + { + if (string.Equals(left[x], right[y], StringComparison.Ordinal)) + { + output.Add(" " + left[x]); + x++; + y++; + } + else if (dp[x + 1, y] >= dp[x, y + 1]) + { + output.Add("- " + left[x++]); + } + else + { + output.Add("+ " + right[y++]); + } + } + + while (x < left.Length) + { + output.Add("- " + left[x++]); + } + + while (y < right.Length) + { + output.Add("+ " + right[y++]); + } + + return ToolExecutionResult.Success(string.Join(Environment.NewLine, output)); + } + + private static ToolExecutionResult Deduplicate(string input) + { + return ToolExecutionResult.Success(string.Join(Environment.NewLine, Lines(input).Distinct(StringComparer.OrdinalIgnoreCase))); + } + + private static ToolExecutionResult SortLines(string input) + { + return ToolExecutionResult.Success(string.Join(Environment.NewLine, Lines(input).Order(StringComparer.CurrentCulture))); + } + + private static ToolExecutionResult FilterLines(string input) + { + var lines = Lines(input).ToArray(); + var keyword = lines.FirstOrDefault() ?? string.Empty; + return ToolExecutionResult.Success(string.Join(Environment.NewLine, lines.Skip(1).Where(line => line.Contains(keyword, StringComparison.OrdinalIgnoreCase)))); + } + + private static ToolExecutionResult TextStatistics(string input) + { + var lines = Lines(input).ToArray(); + var words = Regex.Matches(input, @"\p{L}+|\p{N}+").Count; + return ToolExecutionResult.Success($"Characters: {input.Length}{Environment.NewLine}Lines: {lines.Length}{Environment.NewLine}Words/Tokens: {words}{Environment.NewLine}Non-empty lines: {lines.Count(line => !string.IsNullOrWhiteSpace(line))}"); + } + + private static ToolExecutionResult Delimiters(string input) + { + return ToolExecutionResult.Success(string.Join(Environment.NewLine, SplitTokens(input))); + } + + private static ToolExecutionResult Column(string input) + { + return ToolExecutionResult.Success(string.Join(Environment.NewLine, Lines(input).Select(line => SplitTokens(line).FirstOrDefault() ?? string.Empty))); + } + + private static ToolExecutionResult CsvJson(string input) + { + var lines = Lines(input).ToArray(); + if (lines.Length < 2) return ToolExecutionResult.Success("[]"); + var headers = SplitCsv(lines[0]).ToArray(); + var rows = lines.Skip(1).Select(line => + { + var cells = SplitCsv(line).ToArray(); + return headers.Select((header, index) => new KeyValuePair(header, cells.ElementAtOrDefault(index) ?? string.Empty)) + .ToDictionary(pair => pair.Key, pair => pair.Value); + }); + return ToolExecutionResult.Success(JsonSerializer.Serialize(rows, new JsonSerializerOptions { WriteIndented = true })); + } + + private static ToolExecutionResult KeyValueToJson(string input) + { + var pairs = Lines(input) + .Select(line => line.Split(['=', ':'], 2)) + .Where(parts => parts.Length == 2) + .ToDictionary(parts => parts[0].Trim(), parts => parts[1].Trim(), StringComparer.OrdinalIgnoreCase); + if (pairs.Count == 0) + { + pairs = SplitTokens(input) + .Select(token => token.Split('=', 2)) + .Where(parts => parts.Length == 2) + .ToDictionary(parts => parts[0].Trim(), parts => Uri.UnescapeDataString(parts[1].Trim()), StringComparer.OrdinalIgnoreCase); + } + + return ToolExecutionResult.Success(JsonSerializer.Serialize(pairs, new JsonSerializerOptions { WriteIndented = true })); + } + + private static ToolExecutionResult MarkdownTable(string input) + { + var rows = Lines(input) + .Where(line => line.Contains('|')) + .Select(ParseMarkdownRow) + .Where(row => row.Length > 0) + .ToList(); + if (rows.Count == 0) + { + return ToolExecutionResult.Fail("请输入 Markdown 表格内容。"); + } + + if (rows.Count > 1 && IsMarkdownSeparatorRow(rows[1])) + { + rows.RemoveAt(1); + } + + var columnCount = rows.Max(row => row.Length); + var normalized = rows + .Select(row => row.Concat(Enumerable.Repeat(string.Empty, columnCount - row.Length)).ToArray()) + .ToList(); + var widths = Enumerable.Range(0, columnCount) + .Select(index => Math.Max(3, normalized.Max(row => row[index].Length))) + .ToArray(); + + string FormatRow(IReadOnlyList row) => + "| " + string.Join(" | ", row.Select((cell, index) => cell.PadRight(widths[index]))) + " |"; + + var output = new List { FormatRow(normalized[0]) }; + output.Add("| " + string.Join(" | ", widths.Select(width => new string('-', width))) + " |"); + output.AddRange(normalized.Skip(1).Select(FormatRow)); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, output)); + } + + private static string[] ParseMarkdownRow(string line) + { + var trimmed = line.Trim(); + if (trimmed.StartsWith('|')) + { + trimmed = trimmed[1..]; + } + + if (trimmed.EndsWith('|')) + { + trimmed = trimmed[..^1]; + } + + return trimmed.Split('|').Select(cell => cell.Trim()).ToArray(); + } + + private static bool IsMarkdownSeparatorRow(IEnumerable row) + { + return row.All(cell => Regex.IsMatch(cell.Trim(), "^:?-{3,}:?$")); + } + + private static ToolExecutionResult MarkdownPreview(string input) + { + var lines = input.Replace("\r\n", "\n").Split('\n'); + var headings = lines + .Where(line => Regex.IsMatch(line, @"^\s{0,3}#{1,6}\s+")) + .Select(line => line.Trim()) + .ToArray(); + var links = Regex.Matches(input, @"\[[^\]]+\]\((?[^)]+)\)") + .Select(match => match.Groups["url"].Value) + .Distinct(StringComparer.OrdinalIgnoreCase) + .Take(20) + .ToArray(); + var plain = Regex.Replace(input, @"!\[[^\]]*\]\([^)]+\)", string.Empty); + plain = Regex.Replace(plain, @"\[[^\]]+\]\(([^)]+)\)", "$1"); + plain = Regex.Replace(plain, @"[`*_>#~\-]+", " "); + plain = Regex.Replace(plain, @"\s+", " ").Trim(); + var normalized = string.Join(Environment.NewLine, lines.Select(line => + { + var trimmedEnd = line.TrimEnd(); + return Regex.Replace(trimmedEnd, @"^\s*[-*+]\s+", "- "); + })); + + return ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + $"Headings: {headings.Length}", + $"Links: {links.Length}", + $"Characters: {input.Length}", + $"Words: {Regex.Matches(plain, @"[\p{L}\p{N}_]+").Count}", + string.Empty, + "Headings:", + headings.Length == 0 ? "(none)" : string.Join(Environment.NewLine, headings), + string.Empty, + "Links:", + links.Length == 0 ? "(none)" : string.Join(Environment.NewLine, links), + string.Empty, + "Plain preview:", + plain.Length > 600 ? plain[..600] + "..." : plain, + string.Empty, + "Normalized:", + normalized + })); + } + + private static ToolExecutionResult JsonPaths(string input) + { + using var document = JsonDocument.Parse(input); + var paths = new List(); + VisitJson(document.RootElement, "$", paths); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, paths.Take(300))); + } + + private static void VisitJson(JsonElement element, string path, ICollection paths) + { + switch (element.ValueKind) + { + case JsonValueKind.Object: + var propertyCount = 0; + foreach (var property in element.EnumerateObject()) + { + propertyCount++; + VisitJson(property.Value, $"{path}.{property.Name}", paths); + } + + if (propertyCount == 0) + { + paths.Add($"{path} = {{}}"); + } + break; + case JsonValueKind.Array: + var index = 0; + foreach (var item in element.EnumerateArray()) + { + VisitJson(item, $"{path}[{index++}]", paths); + } + + if (index == 0) + { + paths.Add($"{path} = []"); + } + break; + case JsonValueKind.String: + paths.Add($"{path} = \"{element.GetString()}\""); + break; + case JsonValueKind.Number: + case JsonValueKind.True: + case JsonValueKind.False: + case JsonValueKind.Null: + paths.Add($"{path} = {element.GetRawText()}"); + break; + default: + paths.Add(path); + break; + } + } + + private static ToolExecutionResult HashManifest(string input) + { + var targets = Lines(input).ToArray(); + if (targets.Length == 0) + { + return ToolExecutionResult.Fail("请输入文件路径列表,或输入要生成清单的文本行。"); + } + + var rows = targets.Select(target => + { + var normalized = target.Trim().Trim('"'); + var hash = File.Exists(normalized) + ? HashFile(normalized) + : Hex(SHA256.HashData(Encoding.UTF8.GetBytes(target))); + return $"{hash} {target}"; + }); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, rows)); + } + + private static ToolExecutionResult VerifyHashManifest(string input) + { + var rows = Lines(input).ToArray(); + if (rows.Length == 0) + { + return ToolExecutionResult.Fail("请输入 SHA256 清单,每行格式为:hash path。"); + } + + var output = new List(); + foreach (var row in rows) + { + var match = Regex.Match(row, @"^(?[a-fA-F0-9]{64})\s+\*?(?.+)$"); + if (!match.Success) + { + output.Add($"SKIP {row}"); + continue; + } + + var expected = match.Groups["hash"].Value.ToLowerInvariant(); + var target = match.Groups["target"].Value.Trim().Trim('"'); + if (!File.Exists(target) && LooksLikePath(target)) + { + output.Add($"MISSING {target}"); + continue; + } + + var actual = File.Exists(target) + ? HashFile(target) + : Hex(SHA256.HashData(Encoding.UTF8.GetBytes(target))); + output.Add(string.Equals(expected, actual, StringComparison.OrdinalIgnoreCase) + ? $"OK {target}" + : $"MISMATCH {target}"); + } + + return ToolExecutionResult.Success(string.Join(Environment.NewLine, output)); + } + + private static ToolExecutionResult Sql(string input) + { + var keywords = new[] { "select", "from", "where", "group by", "order by", "having", "left join", "right join", "inner join", "join", "limit" }; + var formatted = input.Trim(); + foreach (var keyword in keywords) + { + formatted = Regex.Replace(formatted, $@"\s+{Regex.Escape(keyword)}\s+", $"{Environment.NewLine}{keyword.ToUpperInvariant()} ", RegexOptions.IgnoreCase); + } + + return ToolExecutionResult.Success(formatted.Trim()); + } + + private static ToolExecutionResult ReplaceText(string input) + { + var parts = input.Replace("\r\n", "\n").Split('\n', 3); + return parts.Length < 3 + ? ToolExecutionResult.Fail("第一行写查找文本,第二行写替换文本,第三行开始写正文。") + : ToolExecutionResult.Success(parts[2].Replace(parts[0], parts[1], StringComparison.Ordinal)); + } + + private static ToolExecutionResult FileNames(string input) + { + return ToolExecutionResult.Success(string.Join(Environment.NewLine, Lines(input).Select(line => Regex.Replace(line.Trim(), @"[\\/:*?""<>|]+", "_")))); + } + + private static ToolExecutionResult ColorTool(string input) + { + if (!TryParseColor(input, out var r, out var g, out var b)) + { + return ToolExecutionResult.Fail("请输入 #RRGGBB、#RGB 或 R G B 格式的颜色值。"); + } + + var (h, s, l) = RgbToHsl(r, g, b); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + $"HEX: #{r:X2}{g:X2}{b:X2}", + $"RGB: {r}, {g}, {b}", + $"HSL: {h:0.#}, {s:0.#}%, {l:0.#}%" + })); + } + + private static ToolExecutionResult ColorContrast(string input) + { + var parts = Lines(input).SelectMany(line => Regex.Split(line, @"\s+")).Where(part => !string.IsNullOrWhiteSpace(part)).ToArray(); + if (parts.Length < 2 || + !TryParseColor(parts[0], out var fr, out var fg, out var fb) || + !TryParseColor(parts[1], out var br, out var bg, out var bb)) + { + return ToolExecutionResult.Fail("请输入两个颜色值,例如:#111827 #ffffff。"); + } + + var foreground = RelativeLuminance(fr, fg, fb); + var background = RelativeLuminance(br, bg, bb); + var lighter = Math.Max(foreground, background); + var darker = Math.Min(foreground, background); + var ratio = (lighter + 0.05) / (darker + 0.05); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + $"Foreground: #{fr:X2}{fg:X2}{fb:X2}", + $"Background: #{br:X2}{bg:X2}{bb:X2}", + $"Contrast: {ratio:0.00}:1", + $"AA normal text: {(ratio >= 4.5 ? "Pass" : "Fail")}", + $"AA large text: {(ratio >= 3 ? "Pass" : "Fail")}", + $"AAA normal text: {(ratio >= 7 ? "Pass" : "Fail")}", + $"AAA large text: {(ratio >= 4.5 ? "Pass" : "Fail")}" + })); + } + + private static double RelativeLuminance(byte r, byte g, byte b) + { + static double Channel(byte value) + { + var normalized = value / 255d; + return normalized <= 0.04045 + ? normalized / 12.92 + : Math.Pow((normalized + 0.055) / 1.055, 2.4); + } + + return 0.2126 * Channel(r) + 0.7152 * Channel(g) + 0.0722 * Channel(b); + } + + private static ToolExecutionResult QrGenerate(string input) + { + var payload = string.IsNullOrWhiteSpace(input) ? "YMhut Box" : input.Trim(); + using var generator = new QRCodeGenerator(); + using var data = generator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q); + var svg = new SvgQRCode(data).GetGraphic(4); + var pngBase64 = Convert.ToBase64String(new PngByteQRCode(data).GetGraphic(12)); + var block = ToolResultBlock.Media( + ToolResultBlockKind.Image, + T(null, "二维码预览", "QR code preview"), + svg, + string.Empty, + new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["contentType"] = "image/svg+xml", + ["displayMode"] = "preview", + ["rawSvg"] = svg, + ["pngBase64"] = pngBase64, + ["payload"] = payload, + ["suggestedFileName"] = "ymhut-qrcode" + }); + var document = new ToolResultDocument( + "qr_generator", + ToolResultKind.ImagePreview, + svg, + [block], + new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["toolId"] = "qr_generator", + ["resultKind"] = ToolResultKind.ImagePreview.ToString(), + ["contentType"] = "image/svg+xml", + ["generatedAt"] = DateTimeOffset.Now.ToString("O") + }, + svg, + string.Empty, + "ok"); + return ToolExecutionResult.Success(svg, document); + } + + private static ToolExecutionResult QrScan(string input) + { + if (!OperatingSystem.IsWindowsVersionAtLeast(6, 1)) + { + return ToolExecutionResult.Fail("二维码扫描依赖 Windows 图像解码能力。"); + } + + var path = input.Trim().Trim('"'); + if (!File.Exists(path)) + { + return ToolExecutionResult.Fail("请输入要扫描的本地图片文件路径。"); + } + + using var bitmap = new Bitmap(path); + var source = CreateLuminanceSource(bitmap); + var reader = new MultiFormatReader(); + var result = reader.decode( + new BinaryBitmap(new HybridBinarizer(source)), + new Dictionary + { + [DecodeHintType.TRY_HARDER] = true, + [DecodeHintType.POSSIBLE_FORMATS] = new[] { BarcodeFormat.QR_CODE } + }); + return result is null + ? ToolExecutionResult.Fail("未识别到二维码。") + : ToolExecutionResult.Success(result.Text); + } + + private static ToolExecutionResult Archive(string input) + { + var commandLine = Lines(input).FirstOrDefault() ?? input; + var extractMatch = Regex.Match(commandLine, "^extract\\s+(?:\"(?[^\"]+)\"|(?\\S+))(?:\\s+(?:\"(?[^\"]+)\"|(?\\S+)))?$", RegexOptions.IgnoreCase); + if (extractMatch.Success) + { + var archivePath = extractMatch.Groups["path"].Value; + var destination = extractMatch.Groups["dest"].Success + ? extractMatch.Groups["dest"].Value + : NextAvailablePath(Path.Combine(Path.GetDirectoryName(archivePath) ?? Environment.CurrentDirectory, Path.GetFileNameWithoutExtension(archivePath))); + if (!File.Exists(archivePath)) + { + return ToolExecutionResult.Fail("请输入要解压的 .zip 或 .tar 文件路径。"); + } + + Directory.CreateDirectory(destination); + if (Path.GetExtension(archivePath).Equals(".zip", StringComparison.OrdinalIgnoreCase)) + { + ZipFile.ExtractToDirectory(archivePath, destination, overwriteFiles: false); + return ToolExecutionResult.Success($"已解压到:{destination}"); + } + + if (Path.GetExtension(archivePath).Equals(".tar", StringComparison.OrdinalIgnoreCase)) + { + TarFile.ExtractToDirectory(archivePath, destination, overwriteFiles: false); + return ToolExecutionResult.Success($"已解压到:{destination}"); + } + + return ToolExecutionResult.Fail("目前支持解压 .zip 和 .tar。"); + } + + var tarMatch = Regex.Match(commandLine, "^tar\\s+(?:\"(?[^\"]+)\"|(?\\S+))$", RegexOptions.IgnoreCase); + var path = (tarMatch.Success ? tarMatch.Groups["path"].Value : commandLine).Trim().Trim('"'); + if (Directory.Exists(path)) + { + var targetPath = NextAvailablePath(Path.Combine(Path.GetDirectoryName(path) ?? Environment.CurrentDirectory, Path.GetFileName(path) + (tarMatch.Success ? ".tar" : ".zip"))); + if (tarMatch.Success) + { + TarFile.CreateFromDirectory(path, targetPath, includeBaseDirectory: false); + } + else + { + ZipFile.CreateFromDirectory(path, targetPath, CompressionLevel.Optimal, includeBaseDirectory: false); + } + + return ToolExecutionResult.Success($"已创建归档:{targetPath}"); + } + + if (File.Exists(path) && Path.GetExtension(path).Equals(".zip", StringComparison.OrdinalIgnoreCase)) + { + using var archive = ZipFile.OpenRead(path); + var entries = archive.Entries + .Take(80) + .Select(entry => $"{entry.FullName}\t{entry.Length} bytes"); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, entries)); + } + + if (File.Exists(path) && Path.GetExtension(path).Equals(".tar", StringComparison.OrdinalIgnoreCase)) + { + using var stream = File.OpenRead(path); + using var reader = new TarReader(stream); + var entries = new List(); + TarEntry? entry; + while ((entry = reader.GetNextEntry()) is not null && entries.Count < 80) + { + entries.Add($"{entry.Name}\t{entry.Length} bytes"); + } + + return ToolExecutionResult.Success(string.Join(Environment.NewLine, entries)); + } + + if (File.Exists(path)) + { + var targetPath = NextAvailablePath(Path.ChangeExtension(path, tarMatch.Success ? ".tar" : ".zip")); + if (tarMatch.Success) + { + using var stream = File.Create(targetPath); + using var writer = new TarWriter(stream, leaveOpen: false); + writer.WriteEntry(fileName: path, entryName: Path.GetFileName(path)); + } + else + { + using var archive = ZipFile.Open(targetPath, ZipArchiveMode.Create); + archive.CreateEntryFromFile(path, Path.GetFileName(path), CompressionLevel.Optimal); + } + + return ToolExecutionResult.Success($"已创建归档:{targetPath}"); + } + + return ToolExecutionResult.Fail("请输入文件、目录、.zip/.tar 归档路径,或使用 extract/tar 命令。"); + } + + private static ToolExecutionResult ImageTool(string input) + { + if (!OperatingSystem.IsWindowsVersionAtLeast(6, 1)) + { + return ToolExecutionResult.Fail("图片处理依赖 Windows 图像解码能力。"); + } + + var commandLine = Lines(input).FirstOrDefault() ?? input; + var resizeMatch = Regex.Match(commandLine, "^resize\\s+(?:\"(?[^\"]+)\"|(?\\S+))\\s+(?\\d+)\\s+(?\\d+)$", RegexOptions.IgnoreCase); + if (resizeMatch.Success) + { + var resizePath = resizeMatch.Groups["path"].Value; + var width = int.Parse(resizeMatch.Groups["width"].Value, CultureInfo.InvariantCulture); + var height = int.Parse(resizeMatch.Groups["height"].Value, CultureInfo.InvariantCulture); + return ResizeImage(resizePath, width, height); + } + + var cropMatch = Regex.Match(commandLine, "^crop\\s+(?:\"(?[^\"]+)\"|(?\\S+))\\s+(?\\d+)\\s+(?\\d+)\\s+(?\\d+)\\s+(?\\d+)$", RegexOptions.IgnoreCase); + if (cropMatch.Success) + { + var cropPath = cropMatch.Groups["path"].Value; + var rect = new Rectangle( + int.Parse(cropMatch.Groups["x"].Value, CultureInfo.InvariantCulture), + int.Parse(cropMatch.Groups["y"].Value, CultureInfo.InvariantCulture), + int.Parse(cropMatch.Groups["width"].Value, CultureInfo.InvariantCulture), + int.Parse(cropMatch.Groups["height"].Value, CultureInfo.InvariantCulture)); + return CropImage(cropPath, rect); + } + + var convertMatch = Regex.Match(commandLine, "^convert\\s+(?:\"(?[^\"]+)\"|(?\\S+))\\s+(?png|jpg|jpeg|bmp)$", RegexOptions.IgnoreCase); + if (convertMatch.Success) + { + return ConvertImage(convertMatch.Groups["path"].Value, convertMatch.Groups["format"].Value); + } + + var imagePath = commandLine.Trim().Trim('"'); + if (!File.Exists(imagePath)) + { + return ToolExecutionResult.Fail("请输入本地图片路径,或使用 resize / crop / convert 命令。"); + } + + using var image = Image.FromFile(imagePath); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + $"Path: {imagePath}", + $"Width: {image.Width}", + $"Height: {image.Height}", + $"Pixel format: {image.PixelFormat}", + $"Raw format: {image.RawFormat}" + })); + } + + private static ToolExecutionResult ImageMetadata(string input) + { + if (!OperatingSystem.IsWindowsVersionAtLeast(6, 1)) + { + return ToolExecutionResult.Fail("图片元数据读取依赖 Windows 图像解码能力。"); + } + + var imagePath = (Lines(input).FirstOrDefault() ?? input).Trim().Trim('"'); + if (!File.Exists(imagePath)) + { + return ToolExecutionResult.Fail("请输入本地图片路径,或使用文件选择器。"); + } + + var info = new FileInfo(imagePath); + using var image = Image.FromFile(imagePath); + var rows = new List + { + $"Path: {imagePath}", + $"File name: {info.Name}", + $"File size: {info.Length} bytes", + $"Width: {image.Width}", + $"Height: {image.Height}", + $"Pixel format: {image.PixelFormat}", + $"Raw format: {ImageFormatName(image.RawFormat)}", + $"Horizontal resolution: {image.HorizontalResolution:0.##} dpi", + $"Vertical resolution: {image.VerticalResolution:0.##} dpi", + $"Metadata tags: {image.PropertyIdList.Length}" + }; + foreach (var property in image.PropertyItems.Take(16)) + { + rows.Add($"EXIF 0x{property.Id:X4}: type {property.Type}, {property.Len} bytes"); + } + + return ToolExecutionResult.Success(string.Join(Environment.NewLine, rows)); + } + + [SupportedOSPlatform("windows6.1")] + private static string ImageFormatName(ImageFormat format) + { + if (format.Guid == ImageFormat.Png.Guid) return "PNG"; + if (format.Guid == ImageFormat.Jpeg.Guid) return "JPEG"; + if (format.Guid == ImageFormat.Bmp.Guid) return "BMP"; + if (format.Guid == ImageFormat.Gif.Guid) return "GIF"; + if (format.Guid == ImageFormat.Tiff.Guid) return "TIFF"; + if (format.Guid == ImageFormat.Icon.Guid) return "ICO"; + return format.ToString(); + } + + private static ToolExecutionResult ResizeImage(string path, int width, int height) + { + if (!OperatingSystem.IsWindowsVersionAtLeast(6, 1)) + { + return ToolExecutionResult.Fail("图片处理依赖 Windows 图像解码能力。"); + } + + if (!File.Exists(path)) + { + return ToolExecutionResult.Fail("请输入本地图片文件路径。"); + } + + using var source = Image.FromFile(path); + using var target = new Bitmap(width, height); + using (var graphics = Graphics.FromImage(target)) + { + graphics.InterpolationMode = global::System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; + graphics.DrawImage(source, 0, 0, width, height); + } + + var output = NextAvailablePath(Path.Combine(Path.GetDirectoryName(path) ?? Environment.CurrentDirectory, $"{Path.GetFileNameWithoutExtension(path)}_{width}x{height}.png")); + target.Save(output, ImageFormat.Png); + return ToolExecutionResult.Success($"已输出:{output}"); + } + + private static ToolExecutionResult CropImage(string path, Rectangle rectangle) + { + if (!OperatingSystem.IsWindowsVersionAtLeast(6, 1)) + { + return ToolExecutionResult.Fail("图片处理依赖 Windows 图像解码能力。"); + } + + if (!File.Exists(path)) + { + return ToolExecutionResult.Fail("请输入本地图片文件路径。"); + } + + using var source = new Bitmap(path); + var safeRectangle = Rectangle.Intersect(rectangle, new Rectangle(0, 0, source.Width, source.Height)); + if (safeRectangle.Width <= 0 || safeRectangle.Height <= 0) + { + return ToolExecutionResult.Fail("裁剪区域超出图片范围。"); + } + + using var target = source.Clone(safeRectangle, source.PixelFormat); + var output = NextAvailablePath(Path.Combine(Path.GetDirectoryName(path) ?? Environment.CurrentDirectory, $"{Path.GetFileNameWithoutExtension(path)}_crop.png")); + target.Save(output, ImageFormat.Png); + return ToolExecutionResult.Success($"已输出:{output}"); + } + + private static ToolExecutionResult ConvertImage(string path, string format) + { + if (!OperatingSystem.IsWindowsVersionAtLeast(6, 1)) + { + return ToolExecutionResult.Fail("图片处理依赖 Windows 图像解码能力。"); + } + + if (!File.Exists(path)) + { + return ToolExecutionResult.Fail("请输入本地图片文件路径。"); + } + + var normalized = format.Equals("jpeg", StringComparison.OrdinalIgnoreCase) ? "jpg" : format.ToLowerInvariant(); + var imageFormat = normalized switch + { + "jpg" => ImageFormat.Jpeg, + "bmp" => ImageFormat.Bmp, + _ => ImageFormat.Png + }; + var output = NextAvailablePath(Path.ChangeExtension(path, normalized)); + using var source = Image.FromFile(path); + source.Save(output, imageFormat); + return ToolExecutionResult.Success($"已输出:{output}"); + } + + private static ToolExecutionResult AsciiArt(string input) + { + if (!OperatingSystem.IsWindowsVersionAtLeast(6, 1)) + { + return ToolExecutionResult.Fail("ASCII Art 渲染依赖 Windows 字体栅格化能力。"); + } + + var text = Lines(input).FirstOrDefault() ?? input.Trim(); + if (string.IsNullOrWhiteSpace(text)) + { + return ToolExecutionResult.Fail("请输入要转换的文本。"); + } + + text = text.Length > 48 ? text[..48] : text; + using var measureBitmap = new Bitmap(1, 1); + using var measureGraphics = Graphics.FromImage(measureBitmap); + using var font = new Font("Consolas", 24, FontStyle.Bold, GraphicsUnit.Pixel); + var size = measureGraphics.MeasureString(text, font); + var width = Math.Max(1, (int)Math.Ceiling(size.Width) + 8); + var height = Math.Max(1, (int)Math.Ceiling(size.Height) + 8); + using var bitmap = new Bitmap(width, height); + using (var graphics = Graphics.FromImage(bitmap)) + { + graphics.Clear(Color.Black); + graphics.TextRenderingHint = global::System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit; + graphics.DrawString(text, font, Brushes.White, new PointF(4, 2)); + } + + var rows = new List(); + for (var y = 0; y < bitmap.Height; y += 2) + { + var builder = new StringBuilder(); + for (var x = 0; x < bitmap.Width; x += 2) + { + var lit = false; + for (var yy = y; yy < Math.Min(y + 2, bitmap.Height) && !lit; yy++) + { + for (var xx = x; xx < Math.Min(x + 2, bitmap.Width); xx++) + { + if (bitmap.GetPixel(xx, yy).R > 96) + { + lit = true; + break; + } + } + } + + builder.Append(lit ? '#' : ' '); + } + + var row = builder.ToString().TrimEnd(); + if (row.Length > 0) + { + rows.Add(row); + } + } + + return ToolExecutionResult.Success(string.Join(Environment.NewLine, rows)); + } + + private static ToolExecutionResult Profanity(string input) + { + var parts = input.Replace("\r\n", "\n").Split("\n---\n", 2, StringSplitOptions.None); + var defaultTerms = new[] { "spam", "scam", "fraud", "广告", "诈骗", "钓鱼", "违禁", "敏感词" }; + var terms = defaultTerms; + var text = input; + if (parts.Length == 2) + { + terms = Lines(parts[0]).Where(term => !string.IsNullOrWhiteSpace(term)).ToArray(); + text = parts[1]; + } + + var hits = terms + .Where(term => text.Contains(term, StringComparison.OrdinalIgnoreCase)) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToArray(); + var masked = text; + foreach (var term in hits) + { + masked = Regex.Replace(masked, Regex.Escape(term), new string('*', Math.Max(2, term.Length)), RegexOptions.IgnoreCase); + } + + return hits.Length == 0 + ? ToolExecutionResult.Success("未发现内置敏感词命中。") + : ToolExecutionResult.Success($"Potential matches: {string.Join(", ", hits)}{Environment.NewLine}{Environment.NewLine}{masked}"); + } + + private static ToolExecutionResult Unicode(string input) + { + var escaped = string.Concat(input.Select(ch => ch > 127 ? $"\\u{(int)ch:X4}" : ch.ToString())); + return ToolExecutionResult.Success($"{escaped}{Environment.NewLine}{Regex.Unescape(input)}"); + } + + private static ToolExecutionResult Punycode(string input) + { + var idn = new IdnMapping(); + return ToolExecutionResult.Success(input.Contains("xn--", StringComparison.OrdinalIgnoreCase) + ? idn.GetUnicode(input) + : idn.GetAscii(input)); + } + + private static ToolExecutionResult Jwt(string input) + { + var parts = input.Trim().Split('.'); + if (parts.Length < 2) return ToolExecutionResult.Fail("JWT 至少需要 header.payload 两段。"); + return ToolExecutionResult.Success($"{DecodeBase64Url(parts[0])}{Environment.NewLine}{Environment.NewLine}{DecodeBase64Url(parts[1])}"); + } + + private static ToolExecutionResult Chunk(string input) + { + var items = Lines(input).ToArray(); + return ToolExecutionResult.Success(string.Join(Environment.NewLine + "---" + Environment.NewLine, items.Chunk(5).Select(chunk => string.Join(Environment.NewLine, chunk)))); + } + + private static ToolExecutionResult SetOperation(string input, string operation) + { + var parts = input.Replace("\r\n", "\n").Split("\n---\n", 2, StringSplitOptions.None); + var left = Lines(parts.ElementAtOrDefault(0) ?? string.Empty).ToHashSet(StringComparer.OrdinalIgnoreCase); + var right = Lines(parts.ElementAtOrDefault(1) ?? string.Empty).ToHashSet(StringComparer.OrdinalIgnoreCase); + IEnumerable result = operation switch + { + "intersection" => left.Intersect(right, StringComparer.OrdinalIgnoreCase), + "union" => left.Union(right, StringComparer.OrdinalIgnoreCase), + _ => left.Except(right, StringComparer.OrdinalIgnoreCase) + }; + return ToolExecutionResult.Success(string.Join(Environment.NewLine, result)); + } + + private static ToolExecutionResult Template(string input) + { + var parts = input.Replace("\r\n", "\n").Split("\n---\n", 2, StringSplitOptions.None); + var template = parts.ElementAtOrDefault(0) ?? string.Empty; + foreach (var line in Lines(parts.ElementAtOrDefault(1) ?? string.Empty)) + { + var pair = line.Split('=', 2); + if (pair.Length == 2) + { + template = template.Replace("{{" + pair[0].Trim() + "}}", pair[1].Trim(), StringComparison.OrdinalIgnoreCase); + } + } + return ToolExecutionResult.Success(template); + } + + private static ToolExecutionResult PathNormalize(string input) + { + return ToolExecutionResult.Success(string.Join(Environment.NewLine, Lines(input).Select(path => Path.GetFullPath(path)))); + } + + private static ToolExecutionResult PathBreakdown(string input) + { + var lines = Lines(input).Select(path => $"{Path.GetDirectoryName(path) ?? string.Empty} | {Path.GetFileNameWithoutExtension(path)} | {Path.GetExtension(path)}"); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, lines)); + } + + private static ToolExecutionResult ExtensionExtract(string input) + { + return ToolExecutionResult.Success(string.Join(Environment.NewLine, Lines(input).Select(path => Path.GetExtension(path)))); + } + + private static ToolExecutionResult ExtensionStats(string input) + { + var counts = Lines(input) + .Select(path => string.IsNullOrWhiteSpace(Path.GetExtension(path)) ? "(none)" : Path.GetExtension(path).ToLowerInvariant()) + .GroupBy(ext => ext) + .OrderByDescending(group => group.Count()) + .Select(group => $"{group.Key}: {group.Count()}"); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, counts)); + } + + private static ToolExecutionResult Manifest(string input) + { + var rows = Lines(input).Select(line => new + { + Value = line, + Length = line.Length, + Hash = Hex(SHA256.HashData(Encoding.UTF8.GetBytes(line))) + }); + return ToolExecutionResult.Success(JsonSerializer.Serialize(rows, new JsonSerializerOptions { WriteIndented = true })); + } + + private static ToolExecutionResult RenamePreview(string input, bool indexed) + { + var lines = Lines(input).ToList(); + if (lines.Count == 0) + { + return ToolExecutionResult.Fail("请输入要预览重命名的文件名或路径列表。"); + } + + var prefix = "item_"; + if (lines[0].StartsWith("prefix=", StringComparison.OrdinalIgnoreCase)) + { + prefix = lines[0][7..].Trim(); + lines.RemoveAt(0); + } + + var find = string.Empty; + var replace = string.Empty; + if (!indexed && lines.Count > 1 && lines[0].Contains("=>", StringComparison.Ordinal)) + { + var rule = lines[0].Split("=>", 2, StringSplitOptions.TrimEntries); + find = rule.ElementAtOrDefault(0) ?? string.Empty; + replace = rule.ElementAtOrDefault(1) ?? string.Empty; + lines.RemoveAt(0); + } + + var used = new HashSet(StringComparer.OrdinalIgnoreCase); + var output = new List(); + for (var index = 0; index < lines.Count; index++) + { + var source = lines[index].Trim().Trim('"'); + var fileName = Path.GetFileName(source); + if (string.IsNullOrWhiteSpace(fileName)) + { + fileName = source; + } + + var extension = Path.GetExtension(fileName); + var nextName = indexed + ? $"{prefix}{index + 1:000}{extension}" + : string.IsNullOrEmpty(find) + ? SanitizeFileName(fileName) + : SanitizeFileName(fileName.Replace(find, replace, StringComparison.OrdinalIgnoreCase)); + nextName = EnsureUniqueFileName(nextName, used); + output.Add($"{fileName} -> {nextName}"); + } + + return ToolExecutionResult.Success(string.Join(Environment.NewLine, output)); + } + + private static ToolExecutionResult MarkdownList(string input) + { + var lines = input.Replace("\r\n", "\n") + .Split('\n') + .Select(line => line.Trim()) + .Where(line => line.Length > 0) + .Select(line => Regex.Replace(line, @"^(?:[-*+]|\d+[.)]|[A-Za-z][.)])\s+", string.Empty).Trim()) + .Where(line => line.Length > 0) + .Select(line => "- " + line); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, lines)); + } + + private static ToolExecutionResult UrlInspect(string input) + { + if (!Uri.TryCreate(input.Trim(), UriKind.Absolute, out var uri)) + { + return ToolExecutionResult.Fail("请输入完整 URL。"); + } + + return ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + $"Scheme: {uri.Scheme}", + $"Host: {uri.Host}", + $"Port: {uri.Port}", + $"Path: {uri.AbsolutePath}", + $"Query: {uri.Query}", + $"Fragment: {uri.Fragment}" + })); + } + + private static async Task UrlRedirectTraceAsync(string input, CancellationToken cancellationToken, string language) + { + if (!Uri.TryCreate(input.Trim(), UriKind.Absolute, out var uri) || + uri.Scheme is not ("http" or "https")) + { + return ToolExecutionResult.Fail(T(language, "请输入 http 或 https URL。", "Enter an http or https URL.")); + } + + using var handler = new HttpClientHandler { AllowAutoRedirect = false }; + using var client = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(10) }; + var rows = new List(); + var current = uri; + for (var index = 0; index < 8; index++) + { + using var request = new HttpRequestMessage(HttpMethod.Head, current); + HttpResponseMessage response; + try + { + response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); + } + catch (HttpRequestException) + { + using var fallback = new HttpRequestMessage(HttpMethod.Get, current); + response = await client.SendAsync(fallback, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); + } + + using (response) + { + var location = response.Headers.Location; + rows.Add($"{index + 1} | {(int)response.StatusCode} {response.ReasonPhrase} | {current}"); + if ((int)response.StatusCode < 300 || (int)response.StatusCode >= 400 || location is null) + { + rows.Add($"Final URL: {current}"); + rows.Add($"Final status: {(int)response.StatusCode}"); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, rows)); + } + + current = location.IsAbsoluteUri ? location : new Uri(current, location); + rows.Add($"Redirect to: {current}"); + } + } + + rows.Add($"Stopped: redirect limit reached"); + rows.Add($"Last URL: {current}"); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, rows)); + } + + private static ToolExecutionResult DomainExtract(string input) + { + var domains = Lines(input) + .Select(line => Uri.TryCreate(line, UriKind.Absolute, out var uri) ? uri.Host : line) + .Where(line => line.Contains('.')) + .Distinct(StringComparer.OrdinalIgnoreCase); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, domains)); + } + + private static ToolExecutionResult Cidr(string input) + { + var parts = input.Split(['/', ' '], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + if (parts.Length == 0 || !IPAddress.TryParse(parts[0], out var ip)) + { + return ToolExecutionResult.Fail("请输入 IPv4 地址,例如 192.168.1.0/24。"); + } + + var prefix = parts.Length > 1 && int.TryParse(parts[1], out var parsed) ? parsed : 24; + var bytes = ip.GetAddressBytes(); + if (bytes.Length != 4) + { + return ToolExecutionResult.Fail("仅支持 IPv4。"); + } + + var mask = prefix == 0 ? 0u : uint.MaxValue << (32 - prefix); + var value = BitConverter.ToUInt32(bytes.Reverse().ToArray(), 0); + var network = value & mask; + var broadcast = network | ~mask; + return ToolExecutionResult.Success($"Network: {ToIPv4(network)}{Environment.NewLine}Broadcast: {ToIPv4(broadcast)}{Environment.NewLine}Mask: {ToIPv4(mask)}{Environment.NewLine}Hosts: {Math.Max(0, (long)Math.Pow(2, 32 - prefix) - 2)}"); + } + + private static ToolExecutionResult PairZip(string input) + { + var parts = input.Replace("\r\n", "\n").Split("\n---\n", 2, StringSplitOptions.None); + var left = Lines(parts.ElementAtOrDefault(0) ?? string.Empty).ToArray(); + var right = Lines(parts.ElementAtOrDefault(1) ?? string.Empty).ToArray(); + var zipped = left.Zip(right, (a, b) => $"{a}\t{b}"); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, zipped)); + } + + private static ToolExecutionResult DateTool(string id, string input) + { + var dates = Regex.Matches(input, @"\d{4}[-/]\d{1,2}[-/]\d{1,2}") + .Select(match => DateOnly.TryParse(match.Value.Replace('/', '-'), out var parsed) ? parsed : (DateOnly?)null) + .Where(date => date.HasValue) + .Select(date => date!.Value) + .ToArray(); + var first = dates.ElementAtOrDefault(0); + if (first == default) + { + first = DateOnly.FromDateTime(DateTime.Today); + } + var second = dates.ElementAtOrDefault(1); + if (second == default) + { + second = DateOnly.FromDateTime(DateTime.Today); + } + + return id switch + { + "date_difference_calculator" => ToolExecutionResult.Success($"{Math.Abs(second.DayNumber - first.DayNumber)} days{Environment.NewLine}{PeriodBetween(first < second ? first : second, first < second ? second : first)}"), + "workday_calculator" => ToolExecutionResult.Success($"{CountWorkdays(first < second ? first : second, first < second ? second : first)} workdays"), + "age_calculator" => ToolExecutionResult.Success($"{PeriodBetween(first, DateOnly.FromDateTime(DateTime.Today))}"), + _ => ToolExecutionResult.Success(input) + }; + } + + private static ToolExecutionResult Cron(string input) + { + var parts = input.Split([' ', '\t'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + return parts.Length >= 5 + ? ToolExecutionResult.Success($"Cron OK: {string.Join(" ", parts.Take(5))}") + : ToolExecutionResult.Fail("请输入五段 Cron 表达式。"); + } + + private static ToolExecutionResult Lookup(string id, string input) + { + var table = id switch + { + "mime_lookup" => new[] + { + "text/plain - Plain text", + "text/html - HTML document", + "text/css - Cascading Style Sheets", + "application/json - JSON document", + "application/xml - XML document", + "application/pdf - PDF document", + "image/png - PNG image", + "image/jpeg - JPEG image", + "image/svg+xml - SVG image", + "application/zip - ZIP archive" + }, + "http_status_lookup" => new[] + { + "100 Continue", + "200 OK", + "201 Created", + "204 No Content", + "301 Moved Permanently", + "302 Found", + "304 Not Modified", + "400 Bad Request", + "401 Unauthorized", + "403 Forbidden", + "404 Not Found", + "429 Too Many Requests", + "500 Internal Server Error", + "502 Bad Gateway", + "503 Service Unavailable" + }, + "port_lookup" => new[] + { + "20/21 - FTP 文件传输服务", + "22 - SSH 安全远程登录", + "25 - SMTP 邮件发送", + "53 - DNS 域名解析", + "80 - HTTP 网页服务", + "110 - POP3 邮件接收", + "143 - IMAP 邮件同步", + "443 - HTTPS 加密网页服务", + "3306 - MySQL 数据库", + "3389 - RDP 远程桌面", + "5432 - PostgreSQL 数据库", + "6379 - Redis 缓存服务" + }, + "dns_record_lookup" => new[] + { + "A - IPv4 address", + "AAAA - IPv6 address", + "CNAME - Canonical name alias", + "MX - Mail exchanger", + "TXT - Text metadata", + "NS - Authoritative name server", + "SOA - Zone authority", + "CAA - Certificate authority authorization", + "SRV - Service locator" + }, + "unicode_block_lookup" => new[] + { + "U+0000..U+007F Basic Latin", + "U+0080..U+00FF Latin-1 Supplement", + "U+2000..U+206F General Punctuation", + "U+3040..U+309F Hiragana", + "U+30A0..U+30FF Katakana", + "U+4E00..U+9FFF CJK Unified Ideographs", + "U+1F300..U+1FAFF Symbols and Pictographs" + }, + "charset_lookup" => new[] + { + "UTF-8 - Unicode variable-width encoding", + "UTF-16LE - Unicode little-endian", + "GB18030 - Chinese national standard", + "GBK - Simplified Chinese legacy encoding", + "Big5 - Traditional Chinese legacy encoding", + "Shift_JIS - Japanese legacy encoding", + "ISO-8859-1 - Western European legacy encoding" + }, + "timezone_abbr_lookup" => new[] + { + "UTC +00:00 Coordinated Universal Time", + "CST +08:00 China Standard Time", + "JST +09:00 Japan Standard Time", + "EST -05:00 Eastern Standard Time", + "EDT -04:00 Eastern Daylight Time", + "PST -08:00 Pacific Standard Time", + "PDT -07:00 Pacific Daylight Time" + }, + "regex_preset_lookup" => new[] + { + @"整数: ^-?\d+$", + @"邮箱: ^[^\s@]+@[^\s@]+\.[^\s@]+$", + @"IPv4: ^(?:\d{1,3}\.){3}\d{1,3}$", + @"URL: ^https?://\S+$", + @"中文: [\u4e00-\u9fff]+", + @"空白行: ^\s*$" + }, + "magic_number_lookup" => new[] + { + "PNG: 89 50 4E 47 0D 0A 1A 0A", + "JPEG: FF D8 FF", + "GIF: 47 49 46 38", + "PDF: 25 50 44 46", + "ZIP: 50 4B 03 04", + "7z: 37 7A BC AF 27 1C", + "RAR: 52 61 72 21 1A 07" + }, + _ => [input] + }; + + var query = input.Trim(); + var rows = string.IsNullOrWhiteSpace(query) + ? table + : table.Where(row => row.Contains(query, StringComparison.OrdinalIgnoreCase)).ToArray(); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, rows.Length == 0 ? table : rows)); + } + + private static async Task ReferenceLookupAsync( + IReferenceDataService? referenceDataService, + string id, + string input, + CancellationToken cancellationToken, + string language) + { + if (referenceDataService is null) + { + return Lookup(id, input); + } + + var text = await referenceDataService.ReadTextAsync("data/reference/ymhut_reference_data.json", cancellationToken).ConfigureAwait(false); + if (string.IsNullOrWhiteSpace(text)) + { + return Lookup(id, input); + } + + var sectionName = id switch + { + "mime_lookup" => "mime_types", + "http_status_lookup" => "http_status", + "port_lookup" => "ports", + "dns_record_lookup" => "dns_records", + "unicode_block_lookup" => "unicode_blocks", + "charset_lookup" => "charsets", + "timezone_abbr_lookup" => "timezone_abbreviations", + "regex_preset_lookup" => "regex_presets", + "magic_number_lookup" => "magic_numbers", + _ => string.Empty + }; + + if (string.IsNullOrWhiteSpace(sectionName)) + { + return Lookup(id, input); + } + + try + { + using var document = JsonDocument.Parse(text); + if (!document.RootElement.TryGetProperty(sectionName, out var section) || section.ValueKind != JsonValueKind.Array) + { + return Lookup(id, input); + } + + var query = input.Trim(); + var rows = section.EnumerateArray() + .Where(item => string.IsNullOrWhiteSpace(query) || ReferenceItemMatches(item, query)) + .Take(80) + .Select(item => FormatReferenceItem(item, id, language)) + .ToList(); + if (rows.Count == 0) + { + rows = section.EnumerateArray().Take(80).Select(item => FormatReferenceItem(item, id, language)).ToList(); + } + + var sourceKey = sectionName switch + { + "timezone_abbreviations" => "timezones", + "regex_presets" => "unicode", + "magic_numbers" => "iana", + _ => sectionName + }; + var source = ReferenceSourceName(sourceKey, language); + var header = T(language, $"数据源:YMhut Reference Data / {source}", $"Data source: YMhut Reference Data / {source}"); + if (id == "port_lookup") + { + rows.Add(T(language, "翻译入口:未知英文参考内容可使用 Edge 打开官方参考页并启用网页翻译。", "Translation entry: for unknown English references, open the official reference page in Edge and use webpage translation.")); + } + return ToolExecutionResult.Success($"{header}{Environment.NewLine}{string.Join(Environment.NewLine, rows)}"); + } + catch + { + return Lookup(id, input); + } + } + + private static bool ReferenceItemMatches(JsonElement item, string query) + { + foreach (var property in item.EnumerateObject()) + { + if (property.Value.ValueKind == JsonValueKind.String && + property.Value.GetString()?.Contains(query, StringComparison.OrdinalIgnoreCase) == true) + { + return true; + } + + if (property.Value.ValueKind == JsonValueKind.Array && + property.Value.EnumerateArray().Any(value => value.ValueKind == JsonValueKind.String && + value.GetString()?.Contains(query, StringComparison.OrdinalIgnoreCase) == true)) + { + return true; + } + } + + return false; + } + + private static string FormatReferenceItem(JsonElement item, string id, string language) + { + var code = JsonString(item, "code"); + var label = JsonString(item, "label"); + var description = id == "port_lookup" + ? LocalizedPortDescription(code, JsonString(item, "description"), language) + : JsonString(item, "description"); + var value = JsonString(item, "value"); + var aliases = JsonArrayString(item, "aliases"); + + var line = string.IsNullOrWhiteSpace(value) + ? $"{code} - {label}: {description}".Trim(' ', '-', ':') + : $"{code} - {label}: {value} ({description})".Trim(); + return string.IsNullOrWhiteSpace(aliases) + ? line + : $"{line} / {T(language, "别名", "aliases")}: {aliases}"; + } + + private static string ReferenceSourceName(string key, string language) + { + return key switch + { + "mime_types" or "http_status" or "ports" or "dns_records" or "iana" => "IANA", + "unicode" => "Unicode Consortium", + "timezones" => "IANA Time Zone Database", + _ => T(language, "公开参考数据", "Public reference data") + }; + } + + private static string LocalizedPortDescription(string code, string fallback, string language) + { + if (English(language)) + { + return fallback; + } + + return code switch + { + "20" => "FTP 文件传输数据通道", + "21" => "FTP 文件传输控制通道", + "22" => "SSH 安全远程登录,也常用于 SFTP", + "25" => "SMTP 邮件传输服务", + "53" => "DNS 域名解析服务", + "67" => "DHCP 服务端地址分配", + "68" => "DHCP 客户端地址分配", + "80" => "HTTP 网页服务", + "110" => "POP3 邮件收取服务", + "123" => "NTP 网络时间同步", + "143" => "IMAP 邮件访问服务", + "161" => "SNMP 网络设备管理", + "389" => "LDAP 目录访问服务", + "443" => "HTTPS 加密网页服务", + "465" => "SMTPS 加密邮件发送", + "587" => "邮件提交服务,常配合 STARTTLS", + "993" => "IMAPS 加密邮件访问", + "995" => "POP3S 加密邮件收取", + "1433" => "Microsoft SQL Server 数据库服务", + "3306" => "MySQL 数据库服务", + "3389" => "RDP 远程桌面协议", + "5432" => "PostgreSQL 数据库服务", + "6379" => "Redis 键值存储服务", + "8080" => "常见 HTTP 备用端口或代理端口", + _ => fallback + }; + } + + private static string JsonString(JsonElement item, string name) + { + return item.TryGetProperty(name, out var value) && value.ValueKind == JsonValueKind.String + ? value.GetString() ?? string.Empty + : string.Empty; + } + + private static string JsonArrayString(JsonElement item, string name) + { + return item.TryGetProperty(name, out var value) && value.ValueKind == JsonValueKind.Array + ? string.Join(", ", value.EnumerateArray().Where(element => element.ValueKind == JsonValueKind.String).Select(element => element.GetString())) + : string.Empty; + } + + private static ToolExecutionResult SystemInfo() + { + return ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + $"OS: {RuntimeInformation.OSDescription}", + $"Framework: {RuntimeInformation.FrameworkDescription}", + $"Machine: {Environment.MachineName}", + $"Processors: {Environment.ProcessorCount}", + $"Memory: {Environment.WorkingSet / 1024 / 1024} MB" + })); + } + + private static ToolExecutionResult Benchmark() + { + var started = DateTime.UtcNow; + var data = Enumerable.Range(1, 10000).Select(value => SHA256.HashData(BitConverter.GetBytes(value))).ToArray(); + var elapsed = DateTime.UtcNow - started; + return ToolExecutionResult.Success($"Samples: {data.Length}{Environment.NewLine}Elapsed: {elapsed.TotalMilliseconds:0.00} ms"); + } + + private static ToolExecutionResult SystemTool(string input, string language) + { + var command = input.Trim().ToLowerInvariant(); + if (string.IsNullOrWhiteSpace(command) || command is "help" or "status") + { + return ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + T(language, "系统工具", "System tools"), + T(language, "可用命令:status、temp、path、env、process", "Available commands: status, temp, path, env, process"), + T(language, "可打开:calculator、paint、rdp、control_panel、registry_editor、task_manager、services、device_manager、disk_management、event_viewer、windows_settings、explorer、notepad、cmd、powershell", "Launchable: calculator, paint, rdp, control_panel, registry_editor, task_manager, services, device_manager, disk_management, event_viewer, windows_settings, explorer, notepad, cmd, powershell"), + string.Empty, + $"OS: {RuntimeInformation.OSDescription}", + $"Machine: {Environment.MachineName}", + $"User: {Environment.UserName}", + $"Processors: {Environment.ProcessorCount}", + $".NET: {RuntimeInformation.FrameworkDescription}", + $"Temp: {Path.GetTempPath()}" + })); + } + + if (TryResolveSystemLauncher(command, out var launcher)) + { + try + { + var startInfo = new global::System.Diagnostics.ProcessStartInfo + { + FileName = launcher.FileName, + Arguments = launcher.Arguments, + UseShellExecute = true + }; + global::System.Diagnostics.Process.Start(startInfo); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + T(language, $"已打开:{launcher.DisplayName}", $"Opened: {launcher.DisplayName}"), + T(language, $"入口:{launcher.Command}", $"Entry: {launcher.Command}"), + T(language, "提示:系统工具由 Windows 直接启动,YMhut Box 不会读取或保存其窗口内容。", "Tip: Windows launches this system tool directly. YMhut Box does not read or store its window content.") + })); + } + catch (Exception exception) + { + return ToolExecutionResult.Fail(T(language, $"无法打开 {launcher.DisplayName}:{exception.Message}", $"Could not open {launcher.DisplayName}: {exception.Message}")); + } + } + + if (command.StartsWith("temp", StringComparison.Ordinal)) + { + return TempPreview(); + } + + if (command.StartsWith("path", StringComparison.Ordinal)) + { + return ToolExecutionResult.Success(string.Join(Environment.NewLine, (Environment.GetEnvironmentVariable("PATH") ?? string.Empty) + .Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .Distinct(StringComparer.OrdinalIgnoreCase))); + } + + if (command.StartsWith("env", StringComparison.Ordinal)) + { + var names = new[] { "OS", "PROCESSOR_ARCHITECTURE", "NUMBER_OF_PROCESSORS", "USERNAME", "USERPROFILE", "TEMP", "TMP" }; + return ToolExecutionResult.Success(string.Join(Environment.NewLine, names.Select(name => $"{name}={Environment.GetEnvironmentVariable(name)}"))); + } + + if (command.StartsWith("process", StringComparison.Ordinal)) + { + var rows = global::System.Diagnostics.Process.GetProcesses() + .OrderByDescending(process => + { + try { return process.WorkingSet64; } + catch { return 0; } + }) + .Take(20) + .Select(process => + { + try { return $"{process.ProcessName}\tPID {process.Id}\t{process.WorkingSet64 / 1024 / 1024:N0} MB"; } + catch { return $"{process.ProcessName}\tPID {process.Id}"; } + }); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, rows)); + } + + return ToolExecutionResult.Fail(T(language, "未知系统工具命令。可输入:status、temp、path、env、process,或选择 calculator、paint、rdp、control_panel、registry_editor 等 Windows 工具。", "Unknown system tool command. Enter status, temp, path, env, process, or choose Windows tools such as calculator, paint, rdp, control_panel, registry_editor.")); + } + + private static bool TryResolveSystemLauncher(string command, out SystemLauncher launcher) + { + var token = command.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).FirstOrDefault() ?? command; + var normalized = token.Replace("-", "_", StringComparison.OrdinalIgnoreCase); + launcher = normalized switch + { + "calc" or "calculator" => new("计算器", "calculator", "calc.exe"), + "paint" or "mspaint" => new("画图", "paint", "mspaint.exe"), + "rdp" or "mstsc" or "remote_desktop" => new("远程桌面连接", "rdp", "mstsc.exe"), + "control" or "control_panel" => new("控制面板", "control_panel", "control.exe"), + "regedit" or "registry" or "registry_editor" => new("注册表编辑器", "registry_editor", "regedit.exe"), + "taskmgr" or "task_manager" => new("任务管理器", "task_manager", "taskmgr.exe"), + "services" or "services_msc" => new("服务", "services", "services.msc"), + "devmgmt" or "device_manager" => new("设备管理器", "device_manager", "devmgmt.msc"), + "diskmgmt" or "disk_management" => new("磁盘管理", "disk_management", "diskmgmt.msc"), + "eventvwr" or "event_viewer" => new("事件查看器", "event_viewer", "eventvwr.msc"), + "firewall" or "windows_firewall" => new("高级安全 Windows Defender 防火墙", "firewall", "wf.msc"), + "msconfig" or "system_configuration" => new("系统配置", "msconfig", "msconfig.exe"), + "appwiz" or "programs" => new("程序和功能", "programs", "appwiz.cpl"), + "network_connections" or "ncpa" => new("网络连接", "network_connections", "ncpa.cpl"), + "system_properties" or "sysdm" => new("系统属性", "system_properties", "sysdm.cpl"), + "settings" or "windows_settings" => new("Windows 设置", "windows_settings", "ms-settings:"), + "explorer" or "file_explorer" => new("文件资源管理器", "explorer", "explorer.exe"), + "notepad" => new("记事本", "notepad", "notepad.exe"), + "cmd" or "command_prompt" => new("命令提示符", "cmd", "cmd.exe"), + "powershell" or "pwsh" => new("PowerShell", "powershell", "powershell.exe"), + _ => default + }; + + return launcher.FileName is not null; + } + + private readonly record struct SystemLauncher(string DisplayName, string Command, string FileName, string Arguments = ""); + + private static ToolExecutionResult TempPreview() + { + var temp = Path.GetTempPath(); + if (!Directory.Exists(temp)) + { + return ToolExecutionResult.Fail("未找到系统临时目录。"); + } + + var cutoff = DateTime.UtcNow.AddDays(-1); + long bytes = 0; + var count = 0; + foreach (var file in Directory.EnumerateFiles(temp, "*", SearchOption.TopDirectoryOnly)) + { + try + { + var info = new FileInfo(file); + if (info.LastWriteTimeUtc <= cutoff) + { + bytes += info.Length; + count++; + } + } + catch + { + } + } + + return ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + $"Temp: {temp}", + $"Files older than 24h: {count}", + $"Estimated size: {bytes / 1024d / 1024d:0.##} MB", + "当前版本只做清理预览,避免误删正在使用的临时文件。" + })); + } + + private static ToolExecutionResult NativeTool(IToolModule module) + { + return ToolExecutionResult.Success($"{module.Metadata.Name} 已迁移为 WinUI 3 原生页面,请从工具详情页直接打开。"); + } + + private static ToolExecutionResult AiTranslation(string input) + { + var text = input.Trim(); + if (string.IsNullOrWhiteSpace(text)) + { + return ToolExecutionResult.Fail("请输入要翻译的文本。"); + } + + return ToolExecutionResult.Fail("AI 翻译入口已迁移到 WinUI 3,但 Provider 尚未配置;请在后续设置 OpenAI、Azure OpenAI 或自定义 endpoint 后启用。"); + } + + private static ToolExecutionResult ShelfLife(string input) + { + var text = input.Trim(); + if (string.IsNullOrWhiteSpace(text)) + { + return ToolExecutionResult.Fail("请输入食品名称或关键词。"); + } + + var days = Regex.Match(text, @"(?\d+)\s*(?天|日|day|days|d|周|week|weeks|w|月|months|month|m|年|year|years|y)", RegexOptions.IgnoreCase); + var date = Regex.Match(text, @"\d{4}[-/]\d{1,2}[-/]\d{1,2}"); + if (days.Success && date.Success && DateOnly.TryParse(date.Value.Replace('/', '-'), out var productionDate)) + { + var value = int.Parse(days.Groups["value"].Value, CultureInfo.InvariantCulture); + var unit = days.Groups["unit"].Value.ToLowerInvariant(); + var expiry = unit switch + { + "周" or "week" or "weeks" or "w" => productionDate.AddDays(value * 7), + "月" or "month" or "months" or "m" => productionDate.AddMonths(value), + "年" or "year" or "years" or "y" => productionDate.AddYears(value), + _ => productionDate.AddDays(value) + }; + var today = DateOnly.FromDateTime(DateTime.Today); + var remaining = expiry.DayNumber - today.DayNumber; + var status = remaining < 0 ? $"已过期 {-remaining} 天" : remaining == 0 ? "今天到期" : $"剩余 {remaining} 天"; + return ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + "保质期计算", + $"生产日期:{productionDate:yyyy-MM-dd}", + $"到期日期:{expiry:yyyy-MM-dd}", + $"状态:{status}", + "提示:食品保质期应以包装标识和实际储存条件为准。" + })); + } + + var table = new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["milk"] = ["牛奶:冷藏鲜奶通常 3-7 天;UHT 常温奶未开封常见 6-12 个月。"], + ["牛奶"] = ["牛奶:冷藏鲜奶通常 3-7 天;UHT 常温奶未开封常见 6-12 个月。"], + ["egg"] = ["鸡蛋:冷藏通常 3-5 周,破壳或熟制后应尽快食用。"], + ["鸡蛋"] = ["鸡蛋:冷藏通常 3-5 周,破壳或熟制后应尽快食用。"], + ["rice"] = ["大米:阴凉干燥密封通常 6-12 个月。"], + ["大米"] = ["大米:阴凉干燥密封通常 6-12 个月。"], + ["noodles"] = ["挂面/方便面:未开封常见 6-24 个月,见包装标识。"], + ["面"] = ["挂面/方便面:未开封常见 6-24 个月,见包装标识。"], + ["bread"] = ["面包:常温通常 2-4 天,冷冻可明显延长。"], + ["面包"] = ["面包:常温通常 2-4 天,冷冻可明显延长。"], + ["meat"] = ["生鲜肉:冷藏通常 1-2 天;冷冻可按品类保存数月。"], + ["肉"] = ["生鲜肉:冷藏通常 1-2 天;冷冻可按品类保存数月。"], + ["fish"] = ["鱼类/海鲜:冷藏通常 1-2 天,建议尽快食用。"], + ["鱼"] = ["鱼类/海鲜:冷藏通常 1-2 天,建议尽快食用。"] + }; + + var hits = table + .Where(pair => text.Contains(pair.Key, StringComparison.OrdinalIgnoreCase)) + .SelectMany(pair => pair.Value.Select(value => $"{pair.Key}: {value}")) + .Distinct() + .ToArray(); + + return hits.Length > 0 + ? ToolExecutionResult.Success($"参考范围(非替代包装标识):{Environment.NewLine}{string.Join(Environment.NewLine, hits)}") + : ToolExecutionResult.Success("未命中内置常见食品数据。可输入“2026-05-01 180天”直接计算到期日;实际保质期以包装标识和储存条件为准。"); + } + + private static async Task QqProfileAsync(string input, CancellationToken cancellationToken, IApiManager? apiManager, string language) + { + var profile = await QqProfileProvider.QueryAsync(input, apiManager, cancellationToken, language).ConfigureAwait(false); + var outputLines = new List + { + $"QQ: {profile.Qq}", + $"头像: {profile.AvatarUrl}", + $"资料状态: {profile.StatusMessage}" + }; + outputLines.AddRange(profile.Fields.Where(pair => pair.Key != "QQ").Select(pair => $"{pair.Key}: {pair.Value}")); + var output = string.Join(Environment.NewLine, outputLines); + var blocks = new List + { + ToolResultBlock.Metric( + T(language, "QQ 摘要", "QQ summary"), + [ + new("QQ", profile.Qq), + new(T(language, "资料状态", "Profile status"), profile.ProfileAvailable ? T(language, "可用", "Available") : T(language, "部分可用", "Partially available")) + ], + new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["contentType"] = "metric", + ["displayMode"] = "summary" + }), + ToolResultBlock.Media( + ToolResultBlockKind.Image, + T(language, "QQ 头像", "QQ avatar"), + profile.AvatarUrl, + profile.AvatarUrl, + new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["contentType"] = "image/png", + ["displayMode"] = "preview", + ["sourceVisibility"] = "publicTrusted" + }), + ToolResultBlock.KeyValue( + T(language, "公开资料", "Public profile"), + profile.Fields.Select(pair => new KeyValuePair(pair.Key, pair.Value)).ToArray(), + new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["contentType"] = "text/plain", + ["displayMode"] = "details", + ["sourceVisibility"] = profile.ProfileAvailable ? "publicTrusted" : "sensitivePrivate", + ["sourceSensitivity"] = profile.ProfileAvailable ? "public" : "partial" + }), + ToolResultBlock.List( + ToolResultBlockKind.Status, + T(language, "来源状态", "Source status"), + [new(profile.ProfileAvailable ? "ok" : "info", profile.StatusMessage, profile.SourceName, profile.ProfileAvailable ? "ok" : "info", string.Empty)]) + }; + var document = new ToolResultDocument( + "qq_avatar", + ToolResultKind.ImagePreview, + output, + blocks, + new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["toolId"] = "qq_avatar", + ["resultKind"] = ToolResultKind.ImagePreview.ToString(), + ["displayProfile"] = "profile-image", + ["sourceVisibility"] = profile.ProfileAvailable ? "publicTrusted" : "sensitivePrivate", + ["generatedAt"] = DateTimeOffset.Now.ToString("O") + }, + output, + profile.SourceName, + "ok"); + return ToolExecutionResult.Success(output, document); + } + + private static async Task RandomCinemaAsync(string input, CancellationToken cancellationToken, IApiManager? apiManager, string language) + { + var manager = apiManager ?? ApiManager.CreateDefault(); + var response = await manager.FetchAsync("media_types", string.Empty, cancellationToken).ConfigureAwait(false); + if (response.Success) + { + try + { + var catalog = RemoteMediaCatalogParser.Parse(response.Content, response.FetchedAt); + var output = RemoteMediaCatalogFormatter.FormatRandomSource(catalog, input, language); + return ToolExecutionResult.Success(output); + } + catch + { + } + } + + return RandomCinemaFallback(input); + } + + private static ToolExecutionResult RandomCinemaFallback(string input) + { + var sources = new[] + { + "https://picsum.photos/seed/ymhut-box/1280/720", + "https://images.unsplash.com/photo-1497032628192-86f99bcd76bc?w=1280", + "https://images.unsplash.com/photo-1489599849927-2ee91cede3ba?w=1280" + }; + + var index = Math.Abs((input ?? string.Empty).GetHashCode()) % sources.Length; + return ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + "RandomCinema source:", + sources[index] + })); + } + + private static ToolExecutionResult SmartSearch(string input, string language) + { + var parts = input.Replace("\r\n", "\n").Split('\n', 2, StringSplitOptions.TrimEntries); + var requestedChannel = parts.Length > 1 ? parts[0] : "baidu"; + var rawQuery = parts.Length > 1 ? parts[1] : input; + var query = Uri.EscapeDataString(rawQuery.Trim()); + if (string.IsNullOrWhiteSpace(query)) + { + return ToolExecutionResult.Fail(T(language, "请输入搜索关键词。", "Enter search keywords.")); + } + + var channels = SearchChannels(); + var channel = channels.FirstOrDefault(item => string.Equals(item.Id, requestedChannel, StringComparison.OrdinalIgnoreCase)) + ?? channels[0]; + var url = string.Format(CultureInfo.InvariantCulture, channel.UrlTemplate, query); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, new[] + { + T(language, $"搜索渠道:{channel.ZhName}", $"Search channel: {channel.EnName}"), + T(language, $"关键词:{rawQuery.Trim()}", $"Keywords: {rawQuery.Trim()}"), + $"{channel.EnName} - {url}" + })); + } + + private static IReadOnlyList SearchChannels() => + [ + new("baidu", "百度", "Baidu", "https://www.baidu.com/s?wd={0}"), + new("bing", "Bing", "Bing", "https://www.bing.com/search?q={0}"), + new("google", "Google", "Google", "https://www.google.com/search?q={0}"), + new("duckduckgo", "DuckDuckGo", "DuckDuckGo", "https://duckduckgo.com/?q={0}"), + new("sogou", "搜狗", "Sogou", "https://www.sogou.com/web?query={0}"), + new("so360", "360 搜索", "360 Search", "https://www.so.com/s?q={0}"), + new("zhihu", "知乎", "Zhihu", "https://www.zhihu.com/search?q={0}"), + new("bilibili", "B 站", "Bilibili", "https://search.bilibili.com/all?keyword={0}"), + new("weibo", "微博", "Weibo", "https://s.weibo.com/weibo?q={0}"), + new("github", "GitHub", "GitHub", "https://github.com/search?q={0}"), + new("stackoverflow", "Stack Overflow", "Stack Overflow", "https://stackoverflow.com/search?q={0}"), + new("mdn", "MDN", "MDN", "https://developer.mozilla.org/search?q={0}") + ]; + + private sealed record SearchChannel(string Id, string ZhName, string EnName, string UrlTemplate); + + private static ToolExecutionResult UuToolSuite(string input) + { + var catalog = new ToolCatalog(); + var query = input.Trim(); + var matches = string.IsNullOrWhiteSpace(query) + ? catalog.Modules + .Where(module => module.Metadata.Category is ToolCategory.Text or ToolCategory.Calculator or ToolCategory.Dev or ToolCategory.Network) + .Take(20) + : catalog.Search(query).Take(20); + + var lines = new List + { + "YMhut UU Tool Suite", + "Aggregated native WinUI tools", + string.Empty + }; + + foreach (var module in matches) + { + lines.Add($"{module.Id}\t{module.Metadata.Name}\t{module.Metadata.Description}"); + } + + if (lines.Count == 3) + { + lines.Add("No matching tools found."); + } + + return ToolExecutionResult.Success(string.Join(Environment.NewLine, lines)); + } + + private static async Task CarInfoAsync( + IReferenceDataService? referenceDataService, + string input, + CancellationToken cancellationToken) + { + var query = input.Trim(); + var plateMap = new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["京"] = "北京", + ["津"] = "天津", + ["沪"] = "上海", + ["渝"] = "重庆", + ["冀"] = "河北", + ["豫"] = "河南", + ["云"] = "云南", + ["辽"] = "辽宁", + ["黑"] = "黑龙江", + ["湘"] = "湖南", + ["皖"] = "安徽", + ["鲁"] = "山东", + ["新"] = "新疆", + ["苏"] = "江苏", + ["浙"] = "浙江", + ["赣"] = "江西", + ["鄂"] = "湖北", + ["桂"] = "广西", + ["甘"] = "甘肃", + ["晋"] = "山西", + ["蒙"] = "内蒙古", + ["陕"] = "陕西", + ["吉"] = "吉林", + ["闽"] = "福建", + ["贵"] = "贵州", + ["粤"] = "广东", + ["青"] = "青海", + ["藏"] = "西藏", + ["川"] = "四川", + ["宁"] = "宁夏", + ["琼"] = "海南" + }; + + var lines = new List + { + "数据源:本地规则 + AMap 行政区划 adcode/citycode 数据", + "说明:车牌省级简称为公开交通规则;城市编码来自随包资源,精确号段仍以公安交管平台为准。", + string.Empty + }; + + var plateMatch = Regex.Match(query, @"[\u4e00-\u9fff][A-Z0-9]?[A-Z0-9]{4,6}", RegexOptions.IgnoreCase); + if (plateMatch.Success) + { + var prefix = plateMatch.Value[..1]; + if (plateMap.TryGetValue(prefix, out var province)) + { + lines.Add($"车牌:{plateMatch.Value.ToUpperInvariant()}"); + lines.Add($"省级简称:{prefix} / {province}"); + lines.Add(string.Empty); + } + } + else if (query.Length == 1 && plateMap.TryGetValue(query, out var province)) + { + lines.Add($"省级简称:{query} / {province}"); + lines.Add(string.Empty); + } + + if (referenceDataService is not null) + { + var csv = await referenceDataService.ReadTextAsync("data/AMap_adcode_citycode.csv", cancellationToken).ConfigureAwait(false); + if (!string.IsNullOrWhiteSpace(csv)) + { + var matches = csv.Replace("\r\n", "\n") + .Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .Skip(1) + .Select(line => line.Split(',')) + .Where(parts => parts.Length >= 3) + .Where(parts => string.IsNullOrWhiteSpace(query) || + parts[0].Contains(query, StringComparison.OrdinalIgnoreCase) || + parts[1].Contains(query, StringComparison.OrdinalIgnoreCase) || + parts[2].Contains(query, StringComparison.OrdinalIgnoreCase) || + (plateMap.TryGetValue(query[..Math.Min(query.Length, 1)], out var mappedProvince) && parts[0].Contains(mappedProvince, StringComparison.OrdinalIgnoreCase))) + .Take(40) + .Select(parts => $"{parts[0]} / adcode {parts[1]} / citycode {(string.IsNullOrWhiteSpace(parts[2]) ? "-" : parts[2])}") + .ToList(); + + if (matches.Count > 0) + { + lines.Add("行政区划命中:"); + lines.AddRange(matches); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, lines)); + } + } + } + + lines.Add("常用省级简称:"); + lines.Add(string.Join(" ", plateMap.Select(pair => $"{pair.Key}-{pair.Value}"))); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, lines)); + } + + private static async Task SanguoshaSkinAsync( + IReferenceDataService? referenceDataService, + string input, + CancellationToken cancellationToken) + { + if (referenceDataService is null) + { + return ToolExecutionResult.Fail("参考数据服务不可用。"); + } + + var text = await referenceDataService.ReadTextAsync("data/sanguosha/skin_config.json", cancellationToken).ConfigureAwait(false); + if (string.IsNullOrWhiteSpace(text)) + { + return ToolExecutionResult.Fail("未找到三国杀皮肤配置:data/sanguosha/skin_config.json"); + } + + using var document = JsonDocument.Parse(text); + if (document.RootElement.ValueKind != JsonValueKind.Array) + { + return ToolExecutionResult.Fail("三国杀皮肤配置格式无效。"); + } + + var query = input.Trim(); + var rows = document.RootElement.EnumerateArray() + .Where(item => string.IsNullOrWhiteSpace(query) || + JsonString(item, "skinId").Contains(query, StringComparison.OrdinalIgnoreCase) || + JsonString(item, "itemName").Contains(query, StringComparison.OrdinalIgnoreCase) || + JsonString(item, "generalName").Contains(query, StringComparison.OrdinalIgnoreCase) || + JsonString(item, "skinName").Contains(query, StringComparison.OrdinalIgnoreCase)) + .Take(80) + .Select(item => + { + var skinId = JsonString(item, "skinId"); + var itemName = JsonString(item, "itemName"); + var generalName = JsonString(item, "generalName"); + var skinName = JsonString(item, "skinName"); + var gender = JsonString(item, "gender"); + return $"{skinId} / {itemName} / 武将:{generalName} / 皮肤:{skinName} / {gender}"; + }) + .ToList(); + + if (rows.Count == 0) + { + rows.Add("未命中皮肤,可输入武将名、皮肤名或 skinId。"); + } + + var header = string.IsNullOrWhiteSpace(query) + ? "数据源:assets/data/sanguosha/skin_config.json(本地随包数据,显示前 80 条)" + : $"数据源:assets/data/sanguosha/skin_config.json / 查询:{query}"; + return ToolExecutionResult.Success($"{header}{Environment.NewLine}{string.Join(Environment.NewLine, rows)}"); + } + + private static async Task ReferenceSearchAsync( + IReferenceDataService? referenceDataService, + string relativePath, + string input, + CancellationToken cancellationToken) + { + if (referenceDataService is null) + { + return ToolExecutionResult.Fail("参考数据服务不可用。"); + } + + var text = await referenceDataService.ReadTextAsync(relativePath, cancellationToken).ConfigureAwait(false); + if (string.IsNullOrWhiteSpace(text)) + { + return ToolExecutionResult.Fail($"未找到参考数据:{relativePath}"); + } + + var lines = text.Replace("\r\n", "\n").Split('\n'); + var query = input.Trim(); + var matches = string.IsNullOrWhiteSpace(query) + ? lines.Take(40) + : lines.Where(line => line.Contains(query, StringComparison.OrdinalIgnoreCase)).Take(40); + return ToolExecutionResult.Success(string.Join(Environment.NewLine, matches)); + } + + private static async Task WeatherAsync(string input, CancellationToken cancellationToken, IApiManager? apiManager, string language) + { + var manager = apiManager ?? ApiManager.CreateDefault(); + var cityCode = ChinaWeatherCityCode(input); + if (!string.IsNullOrWhiteSpace(cityCode)) + { + var chinaUri = new Uri($"https://www.weather.com.cn/data/cityinfo/{cityCode}.html"); + var chinaWeather = await manager.FetchUriAsync("weather_china", chinaUri, input, cancellationToken).ConfigureAwait(false); + if (chinaWeather.Success && !string.IsNullOrWhiteSpace(chinaWeather.Content)) + { + return ToolExecutionResult.Success(FormatChinaWeather(chinaWeather.Content, language)); + } + } + + var geocode = await manager.FetchAsync("weather", input, cancellationToken).ConfigureAwait(false); + if (!geocode.Success) + { + return ToolExecutionResult.Fail(geocode.Error ?? T(language, "天气位置解析失败。", "Weather location lookup failed.")); + } + + if (!ApiEndpoints.TryGet("weather", out var endpoint)) + { + return ToolExecutionResult.Fail(T(language, "天气数据源未配置。", "Weather source is not configured.")); + } + + using var document = JsonDocument.Parse(geocode.Content); + if (!document.RootElement.TryGetProperty("results", out var results) || + results.ValueKind != JsonValueKind.Array || + results.GetArrayLength() == 0) + { + return ToolExecutionResult.Fail(T(language, "未找到匹配城市。请尝试输入城市中文名、英文名或拼音。", "No matching city found. Try a Chinese name, English name, or pinyin.")); + } + + var location = results[0]; + var latitude = location.GetProperty("latitude").GetDouble().ToString(CultureInfo.InvariantCulture); + var longitude = location.GetProperty("longitude").GetDouble().ToString(CultureInfo.InvariantCulture); + var uri = new Uri($"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}¤t=temperature_2m,relative_humidity_2m,apparent_temperature,weather_code,wind_speed_10m&daily=weather_code,temperature_2m_max,temperature_2m_min,precipitation_sum&timezone=auto&forecast_days=3"); + var forecast = await manager.FetchUriAsync("weather_forecast", uri, input, cancellationToken).ConfigureAwait(false); + if (!forecast.Success) + { + return ToolExecutionResult.Fail(forecast.Error ?? T(language, "天气预报获取失败。", "Weather forecast request failed.")); + } + + var output = RemoteToolFormatter.FormatWeather(endpoint, geocode, forecast, language); + return ToolExecutionResult.Success(output, ToolResultBuilder.FromRemote(endpoint, forecast, output, language)); + } + + private static string ChinaWeatherCityCode(string input) + { + var city = string.IsNullOrWhiteSpace(input) ? "北京" : input.Trim(); + var map = new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["北京"] = "101010100", ["beijing"] = "101010100", + ["上海"] = "101020100", ["shanghai"] = "101020100", + ["广州"] = "101280101", ["guangzhou"] = "101280101", + ["深圳"] = "101280601", ["shenzhen"] = "101280601", + ["杭州"] = "101210101", ["hangzhou"] = "101210101", + ["南京"] = "101190101", ["nanjing"] = "101190101", + ["成都"] = "101270101", ["chengdu"] = "101270101", + ["重庆"] = "101040100", ["chongqing"] = "101040100", + ["武汉"] = "101200101", ["wuhan"] = "101200101", + ["西安"] = "101110101", ["xian"] = "101110101", ["xi'an"] = "101110101", + ["天津"] = "101030100", ["tianjin"] = "101030100", + ["苏州"] = "101190401", ["suzhou"] = "101190401", + ["长沙"] = "101250101", ["changsha"] = "101250101", + ["郑州"] = "101180101", ["zhengzhou"] = "101180101", + ["青岛"] = "101120201", ["qingdao"] = "101120201", + ["厦门"] = "101230201", ["xiamen"] = "101230201" + }; + return map.TryGetValue(city, out var code) ? code : string.Empty; + } + + private static string FormatChinaWeather(string content, string language) + { + using var document = JsonDocument.Parse(content); + var info = document.RootElement.TryGetProperty("weatherinfo", out var weatherInfo) + ? weatherInfo + : document.RootElement; + var rows = new[] + { + T(language, "数据源:中国天气", "Data source: China Weather"), + T(language, $"城市:{JsonString(info, "city")}", $"City: {JsonString(info, "city")}"), + T(language, $"天气:{JsonString(info, "weather")}", $"Weather: {JsonString(info, "weather")}"), + T(language, $"温度:{JsonString(info, "temp1")} / {JsonString(info, "temp2")}", $"Temperature: {JsonString(info, "temp1")} / {JsonString(info, "temp2")}"), + T(language, $"发布时间:{JsonString(info, "ptime")}", $"Published: {JsonString(info, "ptime")}") + }; + return string.Join(Environment.NewLine, rows.Where(row => !row.EndsWith(":", StringComparison.Ordinal) && !row.EndsWith(": ", StringComparison.Ordinal))); + } + + + private static async Task TrainQueryAsync(string input, CancellationToken cancellationToken, IApiManager? apiManager, string language) + { + var manager = apiManager ?? ApiManager.CreateDefault(); + var stationResponse = await manager.FetchAsync("train_query", string.Empty, cancellationToken).ConfigureAwait(false); + if (!stationResponse.Success) + { + return ToolExecutionResult.Fail(stationResponse.Error ?? "12306 车站数据获取失败。"); + } + + if (!ApiEndpoints.TryGet("train_query", out var endpoint)) + { + return ToolExecutionResult.Fail("铁路数据源未配置。"); + } + + var query = ParseTrainQuery(input); + if (query is null) + { + return RemoteToolFormatter.Format(endpoint, stationResponse, input, language); + } + + var stations = RemoteToolFormatter.ParseStations(stationResponse.Content); + var from = FindStation(stations, query.Value.From); + var to = FindStation(stations, query.Value.To); + if (from is null || to is null) + { + return RemoteToolFormatter.Format(endpoint, stationResponse, $"{query.Value.From} {query.Value.To}", language); + } + + await manager.FetchUriAsync("train_init", new Uri("https://kyfw.12306.cn/otn/leftTicket/init"), input, cancellationToken).ConfigureAwait(false); + var uri = new Uri($"https://kyfw.12306.cn/otn/leftTicket/queryG?leftTicketDTO.train_date={query.Value.Date:yyyy-MM-dd}&leftTicketDTO.from_station={from.Code}&leftTicketDTO.to_station={to.Code}&purpose_codes=ADULT"); + var ticketResponse = await manager.FetchUriAsync("train_left_ticket", uri, input, cancellationToken).ConfigureAwait(false); + if (!ticketResponse.Success) + { + return ToolExecutionResult.Fail(ticketResponse.Error ?? "12306 余票查询失败。"); + } + + var output = RemoteToolFormatter.FormatTrainLeftTickets( + endpoint, + stationResponse, + ticketResponse, + from.Name, + to.Name, + query.Value.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)); + return ToolExecutionResult.Success(output, ToolResultBuilder.FromRemote(endpoint, ticketResponse, output, language)); + } + + private static async Task AiLatestNewsAsync(string input, CancellationToken cancellationToken, IApiManager? apiManager, string language) + { + if (string.IsNullOrWhiteSpace(input) || input.Trim().Equals("key=", StringComparison.OrdinalIgnoreCase)) + { + return ToolExecutionResult.Success(T(language, + "状态: 需要输入接口 key 后再查询。\n说明: AI 全网最新资讯不会在缺少 key 时自动请求远程服务。", + "Status: enter an API key before querying.\nNote: AI latest news will not request the remote service without a key.")); + } + + return await ExecuteKnownRemoteOrDescribeAsync(new ToolModule(new ToolMetadata( + "ai_latest_news", + "AI 全网最新资讯", + "按分类查看 AI 新闻", + ToolCategory.Data, + ["ai", "news"], + false, + "\uE9D9")), + input, + cancellationToken, + apiManager, + language).ConfigureAwait(false); + } + + private static async Task CityRouteQueryAsync(string input, CancellationToken cancellationToken, IApiManager? apiManager, string language) + { + if (!HasCityRoutePair(input)) + { + return ToolExecutionResult.Success(T(language, + "状态: 请输入两个城市后再查询。\n示例: 广州 深圳\n示例: 广州 -> 深圳", + "Status: enter two cities before querying.\nExample: Guangzhou Shenzhen\nExample: Guangzhou -> Shenzhen")); + } + + return await ExecuteKnownRemoteOrDescribeAsync(new ToolModule(new ToolMetadata( + "city_route_query", + "城际路线查询", + "查询两个城市之间的路线成本", + ToolCategory.Data, + ["city", "route"], + false, + "\uE9D9")), + input, + cancellationToken, + apiManager, + language).ConfigureAwait(false); + } + + private static bool HasCityRoutePair(string input) + { + var value = input.Replace("\r\n", "\n").Trim(); + if (string.IsNullOrWhiteSpace(value)) + { + return false; + } + + if (value.Contains("->", StringComparison.Ordinal)) + { + var parts = value.Split("->", 2, StringSplitOptions.TrimEntries); + return parts.All(part => !string.IsNullOrWhiteSpace(part)); + } + + var lines = value.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + if (lines.Length >= 2) + { + return true; + } + + return SplitTokens(value).Take(2).Count() >= 2; + } + + private static async Task ExecuteKnownRemoteOrDescribeAsync(IToolModule module, string input, CancellationToken cancellationToken, IApiManager? apiManager, string language) + { + if (ApiEndpoints.TryResolve(module.Id, input, out var endpoint, out var uri)) + { + var manager = apiManager ?? ApiManager.CreateDefault(); + var response = await manager.FetchAsync(endpoint.Id, input, cancellationToken).ConfigureAwait(false); + if (response.Success) + { + return RemoteToolFormatter.Format(endpoint, response, input, language); + } + + return RemoteToolFormatter.FormatFailure(endpoint, response, input, language); + } + + return ToolExecutionResult.Success( + T(language, $"工具:{module.Metadata.Name}", $"Tool: {module.Metadata.Name}") + Environment.NewLine + + T(language, "状态:已迁移到 WinUI 3 原生工具入口。", "Status: migrated to the native WinUI 3 tool entry.") + Environment.NewLine + + T(language, $"说明:此工具的元数据、搜索、最近使用和页面承载均已迁移为本地实现。输入内容如下:{Environment.NewLine}{input}", $"Note: metadata, search, recent usage, and page hosting are now local. Input:{Environment.NewLine}{input}")); + } + + private static (DateOnly Date, string From, string To)? ParseTrainQuery(string input) + { + var tokens = SplitTokens(input).ToArray(); + if (tokens.Length < 2) + { + return null; + } + + var date = DateOnly.FromDateTime(DateTime.Today.AddDays(1)); + var fromIndex = 0; + if (DateOnly.TryParse(tokens[0], out var firstDate)) + { + date = firstDate; + fromIndex = 1; + } + else if (DateOnly.TryParse(tokens[^1], out var lastDate)) + { + date = lastDate; + } + + if (fromIndex + 1 >= tokens.Length) + { + return null; + } + + return (date, tokens[fromIndex], tokens[fromIndex + 1]); + } + + private static TrainStation? FindStation(IEnumerable stations, string value) + { + return stations.FirstOrDefault(station => + station.Name.Equals(value, StringComparison.OrdinalIgnoreCase) || + station.Code.Equals(value, StringComparison.OrdinalIgnoreCase) || + station.Pinyin.Equals(value, StringComparison.OrdinalIgnoreCase)) ?? + stations.FirstOrDefault(station => + station.Name.Contains(value, StringComparison.OrdinalIgnoreCase) || + station.Pinyin.Contains(value, StringComparison.OrdinalIgnoreCase)); + } + + private static string TranslateChars(string input, IReadOnlyDictionary map) + { + return string.Concat(input.Select(ch => map.TryGetValue(ch, out var replacement) ? replacement : ch)); + } + + private static string ConvertChinese(string input, bool toTraditional) + { + try + { + if (OperatingSystem.IsWindows()) + { + return Strings.StrConv(input, toTraditional ? VbStrConv.TraditionalChinese : VbStrConv.SimplifiedChinese, 0x0804) ?? input; + } + } + catch + { + } + + return TranslateChars(input, toTraditional ? SimplifiedToTraditional : TraditionalToSimplified); + } + + private static IReadOnlyDictionary BuildChineseMap() + { + return new Dictionary + { + ['汉'] = '漢', + ['马'] = '馬', + ['门'] = '門', + ['风'] = '風', + ['云'] = '雲', + ['车'] = '車', + ['电'] = '電', + ['国'] = '國', + ['学'] = '學', + ['术'] = '術', + ['简'] = '簡', + ['广'] = '廣', + ['东'] = '東', + ['网'] = '網', + ['页'] = '頁', + ['后'] = '後', + ['发'] = '發', + ['台'] = '臺', + ['万'] = '萬', + ['与'] = '與', + ['乐'] = '樂', + ['书'] = '書', + ['买'] = '買', + ['卖'] = '賣', + ['亲'] = '親', + ['会'] = '會', + ['传'] = '傳', + ['优'] = '優', + ['写'] = '寫', + ['军'] = '軍', + ['农'] = '農', + ['凤'] = '鳳', + ['动'] = '動', + ['区'] = '區', + ['医'] = '醫', + ['华'] = '華', + ['单'] = '單', + ['卫'] = '衛', + ['历'] = '歷', + ['县'] = '縣', + ['双'] = '雙', + ['听'] = '聽', + ['问'] = '問', + ['团'] = '團', + ['园'] = '園', + ['圆'] = '圓', + ['场'] = '場', + ['头'] = '頭', + ['实'] = '實', + ['对'] = '對', + ['导'] = '導', + ['将'] = '將', + ['层'] = '層', + ['岁'] = '歲', + ['岛'] = '島', + ['师'] = '師', + ['开'] = '開', + ['张'] = '張', + ['强'] = '強', + ['归'] = '歸', + ['当'] = '當', + ['录'] = '錄', + ['忆'] = '憶', + ['总'] = '總', + ['护'] = '護', + ['报'] = '報', + ['损'] = '損', + ['换'] = '換', + ['摄'] = '攝', + ['数'] = '數', + ['无'] = '無', + ['时'] = '時', + ['机'] = '機', + ['权'] = '權', + ['条'] = '條', + ['来'] = '來', + ['构'] = '構', + ['标'] = '標', + ['样'] = '樣', + ['档'] = '檔', + ['桥'] = '橋', + ['检'] = '檢', + ['楼'] = '樓', + ['气'] = '氣', + ['汇'] = '匯', + ['测'] = '測', + ['济'] = '濟', + ['湾'] = '灣', + ['点'] = '點', + ['热'] = '熱', + ['爱'] = '愛', + ['现'] = '現', + ['画'] = '畫', + ['监'] = '監', + ['种'] = '種', + ['积'] = '積', + ['称'] = '稱', + ['签'] = '簽', + ['线'] = '線', + ['组'] = '組', + ['经'] = '經', + ['结'] = '結', + ['给'] = '給', + ['统'] = '統', + ['维'] = '維', + ['职'] = '職', + ['联'] = '聯', + ['肤'] = '膚', + ['脑'] = '腦', + ['艺'] = '藝', + ['节'] = '節', + ['获'] = '獲', + ['蓝'] = '藍', + ['见'] = '見', + ['观'] = '觀', + ['视'] = '視', + ['订'] = '訂', + ['认'] = '認', + ['计'] = '計', + ['讯'] = '訊', + ['记'] = '記', + ['讲'] = '講', + ['证'] = '證', + ['评'] = '評', + ['识'] = '識', + ['译'] = '譯', + ['词'] = '詞', + ['试'] = '試', + ['话'] = '話', + ['该'] = '該', + ['语'] = '語', + ['说'] = '說', + ['请'] = '請', + ['读'] = '讀', + ['调'] = '調', + ['谢'] = '謝', + ['账'] = '賬', + ['货'] = '貨', + ['费'] = '費', + ['资'] = '資', + ['购'] = '購', + ['责'] = '責', + ['贵'] = '貴', + ['赛'] = '賽', + ['赞'] = '讚', + ['赠'] = '贈', + ['轮'] = '輪', + ['软'] = '軟', + ['轻'] = '輕', + ['输'] = '輸', + ['边'] = '邊', + ['远'] = '遠', + ['连'] = '連', + ['迟'] = '遲', + ['选'] = '選', + ['邮'] = '郵', + ['里'] = '裏', + ['针'] = '針', + ['钟'] = '鐘', + ['钢'] = '鋼', + ['钱'] = '錢', + ['铁'] = '鐵', + ['铜'] = '銅', + ['银'] = '銀', + ['链'] = '鏈', + ['锁'] = '鎖', + ['镜'] = '鏡', + ['长'] = '長', + ['闭'] = '閉', + ['间'] = '間', + ['队'] = '隊', + ['际'] = '際', + ['阳'] = '陽', + ['难'] = '難', + ['预'] = '預', + ['领'] = '領', + ['题'] = '題', + ['飞'] = '飛', + ['饭'] = '飯', + ['饮'] = '飲', + ['馆'] = '館', + ['驱'] = '驅', + ['驶'] = '駛', + ['验'] = '驗', + ['骑'] = '騎', + ['鱼'] = '魚', + ['鸟'] = '鳥', + ['鸣'] = '鳴', + ['鸡'] = '雞', + ['麦'] = '麥', + ['齐'] = '齊', + ['龄'] = '齡', + ['龙'] = '龍' + }; + } + + private static bool TryParseColor(string input, out byte r, out byte g, out byte b) + { + r = g = b = 0; + var text = input.Trim(); + if (text.StartsWith('#')) + { + text = text[1..]; + if (text.Length == 3) + { + r = Convert.ToByte(new string(text[0], 2), 16); + g = Convert.ToByte(new string(text[1], 2), 16); + b = Convert.ToByte(new string(text[2], 2), 16); + return true; + } + + return text.Length == 6 && + byte.TryParse(text[..2], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out r) && + byte.TryParse(text[2..4], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out g) && + byte.TryParse(text[4..6], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out b); + } + + var parts = Regex.Split(text, @"[\s,]+").Where(part => part.Length > 0).ToArray(); + return parts.Length >= 3 && + byte.TryParse(parts[0], out r) && + byte.TryParse(parts[1], out g) && + byte.TryParse(parts[2], out b); + } + + private static (double H, double S, double L) RgbToHsl(byte r, byte g, byte b) + { + var red = r / 255d; + var green = g / 255d; + var blue = b / 255d; + var max = Math.Max(red, Math.Max(green, blue)); + var min = Math.Min(red, Math.Min(green, blue)); + var l = (max + min) / 2; + if (Math.Abs(max - min) < 0.0001) + { + return (0, 0, l * 100); + } + + var d = max - min; + var s = l > 0.5 ? d / (2 - max - min) : d / (max + min); + var h = max switch + { + var value when Math.Abs(value - red) < 0.0001 => ((green - blue) / d + (green < blue ? 6 : 0)) / 6, + var value when Math.Abs(value - green) < 0.0001 => ((blue - red) / d + 2) / 6, + _ => ((red - green) / d + 4) / 6 + }; + return (h * 360, s * 100, l * 100); + } + + [SupportedOSPlatform("windows6.1")] + private static RGBLuminanceSource CreateLuminanceSource(Bitmap bitmap) + { + using var clone = bitmap.Clone( + new Rectangle(0, 0, bitmap.Width, bitmap.Height), + PixelFormat.Format32bppArgb); + var data = clone.LockBits( + new Rectangle(0, 0, clone.Width, clone.Height), + ImageLockMode.ReadOnly, + PixelFormat.Format32bppArgb); + try + { + var length = Math.Abs(data.Stride) * data.Height; + var bytes = new byte[length]; + Marshal.Copy(data.Scan0, bytes, 0, length); + return new RGBLuminanceSource(bytes, clone.Width, clone.Height, RGBLuminanceSource.BitmapFormat.BGRA32); + } + finally + { + clone.UnlockBits(data); + } + } + + private static string NextAvailablePath(string path) + { + if (!File.Exists(path)) + { + return path; + } + + var directory = Path.GetDirectoryName(path) ?? Environment.CurrentDirectory; + var name = Path.GetFileNameWithoutExtension(path); + var extension = Path.GetExtension(path); + for (var index = 1; index < 1000; index++) + { + var candidate = Path.Combine(directory, $"{name}-{index}{extension}"); + if (!File.Exists(candidate)) + { + return candidate; + } + } + + return Path.Combine(directory, $"{name}-{Guid.NewGuid():N}{extension}"); + } + + private static string HashFile(string path) + { + using var stream = File.OpenRead(path); + return Hex(SHA256.HashData(stream)); + } + + private static bool LooksLikePath(string value) + { + return value.Contains(Path.DirectorySeparatorChar) || + value.Contains(Path.AltDirectorySeparatorChar) || + value.Contains(':') || + Path.HasExtension(value); + } + + private static string SanitizeFileName(string value) + { + var invalid = Path.GetInvalidFileNameChars().ToHashSet(); + var sanitized = new string(value.Select(ch => invalid.Contains(ch) ? '_' : ch).ToArray()).Trim(); + return string.IsNullOrWhiteSpace(sanitized) ? "untitled" : sanitized; + } + + private static string EnsureUniqueFileName(string value, ISet used) + { + var candidate = value; + var name = Path.GetFileNameWithoutExtension(value); + var extension = Path.GetExtension(value); + var index = 1; + while (!used.Add(candidate)) + { + candidate = $"{name}-{index++}{extension}"; + } + + return candidate; + } + + private static string Hex(byte[] bytes) => Convert.ToHexString(bytes).ToLowerInvariant(); + + private static long Factorial(int value) + { + return Enumerable.Range(1, Math.Max(1, value)).Aggregate(1L, (acc, next) => acc * next); + } + + private static long Gcd(long left, long right) + { + while (right != 0) + { + (left, right) = (right, left % right); + } + return Math.Abs(left); + } + + private static string ToIPv4(uint value) + { + var bytes = BitConverter.GetBytes(value); + if (BitConverter.IsLittleEndian) + { + Array.Reverse(bytes); + } + return new IPAddress(bytes).ToString(); + } + + private static string PeriodBetween(DateOnly start, DateOnly end) + { + var years = end.Year - start.Year; + var months = end.Month - start.Month; + var days = end.Day - start.Day; + if (days < 0) + { + months--; + days += DateTime.DaysInMonth(end.AddMonths(-1).Year, end.AddMonths(-1).Month); + } + if (months < 0) + { + years--; + months += 12; + } + return $"{years} years {months} months {days} days"; + } + + private static int CountWorkdays(DateOnly start, DateOnly end) + { + var count = 0; + for (var date = start; date <= end; date = date.AddDays(1)) + { + if (date.DayOfWeek is not DayOfWeek.Saturday and not DayOfWeek.Sunday) + { + count++; + } + } + + return count; + } + + private static IEnumerable Lines(string input) + { + return input.Replace("\r\n", "\n").Split('\n').Select(line => line.Trim()).Where(line => line.Length > 0); + } + + private static IEnumerable SplitTokens(string input) + { + return Regex.Split(input.Trim(), @"[\s,;|\t]+").Where(token => token.Length > 0); + } + + private static IEnumerable SplitCsv(string input) + { + return input.Split(',').Select(cell => cell.Trim().Trim('"')); + } + + private static IEnumerable Numbers(string input) + { + return Regex.Matches(input, @"-?\d+(?:\.\d+)?") + .Select(match => double.Parse(match.Value, CultureInfo.InvariantCulture)); + } + + private static double NumberAt(IReadOnlyList values, int index, double fallback) + { + return index >= 0 && index < values.Count ? values[index] : fallback; + } + + private static string DecodeBase64Url(string value) + { + var padded = value.Replace('-', '+').Replace('_', '/'); + padded = padded.PadRight(padded.Length + (4 - padded.Length % 4) % 4, '='); + return Encoding.UTF8.GetString(Convert.FromBase64String(padded)); + } +} diff --git a/src/YMhut.Box.Core/Tools/ToolMarkdownDocument.cs b/src/YMhut.Box.Core/Tools/ToolMarkdownDocument.cs new file mode 100644 index 0000000..058137d --- /dev/null +++ b/src/YMhut.Box.Core/Tools/ToolMarkdownDocument.cs @@ -0,0 +1,309 @@ +using System.Text; +using System.Text.Json; + +namespace YMhut.Box.Core.Tools; + +public sealed record ToolMarkdownDocument( + string ToolId, + ToolResultKind ResultKind, + string MarkdownText, + string RawText, + string Language, + IReadOnlyDictionary Metadata); + +public static class ToolMarkdownConverter +{ + public static ToolMarkdownDocument FromResult(IToolModule module, ToolExecutionResult result, string language) + { + var raw = result.Ok ? result.Output : result.Error ?? string.Empty; + var document = result.Document ?? ToolResultDocument.FromOutput(module, raw); + var metadata = new Dictionary(document.Metadata, StringComparer.OrdinalIgnoreCase) + { + ["ok"] = result.Ok.ToString(), + ["language"] = language + }; + + return new ToolMarkdownDocument( + module.Id, + document.ResultKind, + BuildMarkdown(module, document, raw, language), + raw, + language, + metadata); + } + + private static string BuildMarkdown(IToolModule module, ToolResultDocument document, string raw, string language) + { + if (string.IsNullOrWhiteSpace(raw)) + { + return T(language, "_暂无可展示的结果。_", "_There is no result to display._"); + } + + if (document.ResultKind == ToolResultKind.JsonTree || LooksLikeJson(raw)) + { + return JsonToMarkdown(raw, language); + } + + if (document.Blocks.Count == 0) + { + return PlainTextToMarkdown(raw); + } + + var builder = new StringBuilder(); + builder.AppendLine($"## {EscapeInline(module.Metadata.Name)}"); + builder.AppendLine(); + + foreach (var block in document.Blocks) + { + AppendBlock(builder, block, language); + } + + return builder.ToString().Trim(); + } + + private static void AppendBlock(StringBuilder builder, ToolResultBlock block, string language) + { + switch (block.Kind) + { + case ToolResultBlockKind.KeyValue: + case ToolResultBlockKind.Metric: + AppendHeading(builder, block.Title, T(language, "详情", "Details")); + AppendTable(builder, [T(language, "字段", "Field"), T(language, "值", "Value")], block.Pairs.Select(pair => new[] { pair.Key, pair.Value })); + break; + case ToolResultBlockKind.Table: + case ToolResultBlockKind.LineChart: + AppendHeading(builder, block.Title, T(language, "表格", "Table")); + AppendRowsAsTable(builder, block.Rows, language); + break; + case ToolResultBlockKind.CardList: + case ToolResultBlockKind.RankedList: + case ToolResultBlockKind.NewsList: + AppendHeading(builder, block.Title, T(language, "列表", "List")); + foreach (var item in block.Items) + { + builder.AppendLine($"{NormalizeOrdinal(item.Leading)}. {EscapeInline(item.Title)}{Subtitle(item.Subtitle)}"); + } + builder.AppendLine(); + break; + case ToolResultBlockKind.Timeline: + AppendHeading(builder, block.Title, T(language, "时间线", "Timeline")); + foreach (var item in block.Items) + { + builder.AppendLine($"- **{EscapeInline(item.Title)}**{Subtitle(item.Subtitle)}"); + } + builder.AppendLine(); + break; + case ToolResultBlockKind.Diff: + AppendCode(builder, block.Title, string.Join(Environment.NewLine, block.Items.Select(item => item.Title)), "diff"); + break; + case ToolResultBlockKind.Status: + case ToolResultBlockKind.Text: + AppendHeading(builder, block.Title, T(language, "内容", "Content")); + foreach (var item in block.Items) + { + builder.AppendLine($"- {EscapeInline(item.Title)}{Subtitle(item.Subtitle)}"); + } + builder.AppendLine(); + break; + case ToolResultBlockKind.Code: + case ToolResultBlockKind.Json: + case ToolResultBlockKind.JsonTree: + case ToolResultBlockKind.Raw: + AppendCode(builder, block.Title, block.Text, block.Language); + break; + case ToolResultBlockKind.Link: + builder.AppendLine($"- [{EscapeInline(string.IsNullOrWhiteSpace(block.Text) ? block.Uri : block.Text)}]({block.Uri})"); + break; + case ToolResultBlockKind.File: + builder.AppendLine($"- **{T(language, "文件", "File")}**: `{EscapeCode(block.Path)}`"); + break; + case ToolResultBlockKind.Image: + case ToolResultBlockKind.Media: + builder.AppendLine($"- **{T(language, "媒体", "Media")}**: [{EscapeInline(block.Text)}]({block.Uri})"); + break; + case ToolResultBlockKind.Color: + builder.AppendLine($"- **{T(language, "颜色", "Color")}**: `{EscapeCode(block.Text)}`"); + break; + default: + builder.AppendLine(EscapeInline(block.Text)); + break; + } + } + + private static string JsonToMarkdown(string raw, string language) + { + try + { + using var json = JsonDocument.Parse(raw); + var root = json.RootElement; + var builder = new StringBuilder(); + builder.AppendLine($"## {T(language, "JSON 结果", "JSON Result")}"); + builder.AppendLine(); + + if (root.ValueKind == JsonValueKind.Array) + { + builder.AppendLine($"- **{T(language, "项目数", "Items")}**: {root.GetArrayLength()}"); + builder.AppendLine(); + var rows = root.EnumerateArray().Take(20).ToArray(); + AppendJsonArrayTable(builder, rows, language); + } + else if (root.ValueKind == JsonValueKind.Object) + { + builder.AppendLine($"- **{T(language, "字段数", "Fields")}**: {root.EnumerateObject().Count()}"); + builder.AppendLine(); + AppendTable( + builder, + [T(language, "字段", "Field"), T(language, "值", "Value")], + root.EnumerateObject().Take(80).Select(property => new[] { property.Name, JsonCell(property.Value) })); + } + + AppendCode(builder, T(language, "原始 JSON", "Raw JSON"), JsonSerializer.Serialize(root, new JsonSerializerOptions { WriteIndented = true }), "json"); + return builder.ToString().Trim(); + } + catch (JsonException exception) + { + var builder = new StringBuilder(); + builder.AppendLine($"## {T(language, "JSON 解析失败", "JSON Parse Failed")}"); + builder.AppendLine(); + builder.AppendLine($"> {EscapeInline(exception.Message)}"); + builder.AppendLine(); + AppendCode(builder, T(language, "原始内容", "Raw Content"), raw, "json"); + return builder.ToString().Trim(); + } + } + + private static void AppendJsonArrayTable(StringBuilder builder, IReadOnlyList rows, string language) + { + if (rows.Count == 0) + { + builder.AppendLine(T(language, "_数组为空。_", "_The array is empty._")); + builder.AppendLine(); + return; + } + + if (rows.Any(row => row.ValueKind != JsonValueKind.Object)) + { + AppendTable(builder, [T(language, "序号", "Index"), T(language, "值", "Value")], rows.Select((row, index) => new[] { (index + 1).ToString(), JsonCell(row) })); + return; + } + + var headers = rows + .SelectMany(row => row.EnumerateObject().Select(property => property.Name)) + .Distinct(StringComparer.Ordinal) + .Take(8) + .ToArray(); + AppendTable(builder, headers, rows.Select(row => headers.Select(header => + row.TryGetProperty(header, out var value) ? JsonCell(value) : string.Empty).ToArray())); + } + + private static void AppendRowsAsTable(StringBuilder builder, IReadOnlyList rows, string language) + { + if (rows.Count == 0) + { + builder.AppendLine(T(language, "_暂无表格行。_", "_No table rows._")); + builder.AppendLine(); + return; + } + + var headers = rows[0].Length > 0 + ? rows[0] + : [T(language, "字段", "Field"), T(language, "值", "Value")]; + var body = rows.Skip(1); + AppendTable(builder, headers, body); + } + + private static void AppendTable(StringBuilder builder, IReadOnlyList headers, IEnumerable> rows) + { + var normalizedHeaders = headers.Count == 0 ? ["Value"] : headers.Take(8).Select(EscapeTableCell).ToArray(); + builder.AppendLine($"| {string.Join(" | ", normalizedHeaders)} |"); + builder.AppendLine($"| {string.Join(" | ", normalizedHeaders.Select(_ => "---"))} |"); + + foreach (var row in rows.Take(80)) + { + var cells = normalizedHeaders.Select((_, index) => index < row.Count ? EscapeTableCell(row[index]) : string.Empty); + builder.AppendLine($"| {string.Join(" | ", cells)} |"); + } + + builder.AppendLine(); + } + + private static void AppendHeading(StringBuilder builder, string title, string fallback) + { + builder.AppendLine($"### {EscapeInline(string.IsNullOrWhiteSpace(title) ? fallback : title)}"); + builder.AppendLine(); + } + + private static void AppendCode(StringBuilder builder, string title, string code, string language) + { + AppendHeading(builder, title, "Code"); + builder.AppendLine($"```{SafeFenceLanguage(language)}"); + builder.AppendLine(code.Replace("```", "``\\`", StringComparison.Ordinal)); + builder.AppendLine("```"); + builder.AppendLine(); + } + + private static string PlainTextToMarkdown(string raw) + { + if (raw.Contains('\n')) + { + return string.Join(Environment.NewLine, raw.Replace("\r\n", "\n").Split('\n').Select(line => string.IsNullOrWhiteSpace(line) ? string.Empty : EscapeInline(line))); + } + + return EscapeInline(raw); + } + + private static bool LooksLikeJson(string raw) + { + var trimmed = raw.TrimStart(); + return trimmed.StartsWith('{') || trimmed.StartsWith('['); + } + + private static string JsonCell(JsonElement value) + { + return value.ValueKind switch + { + JsonValueKind.String => value.GetString() ?? string.Empty, + JsonValueKind.Number or JsonValueKind.True or JsonValueKind.False => value.GetRawText(), + JsonValueKind.Null => "null", + _ => JsonSerializer.Serialize(value) + }; + } + + private static string Subtitle(string subtitle) + { + return string.IsNullOrWhiteSpace(subtitle) ? string.Empty : $" \n {EscapeInline(subtitle)}"; + } + + private static int NormalizeOrdinal(string value) + { + return int.TryParse(value, out var number) && number > 0 ? number : 1; + } + + private static string T(string language, string zh, string en) + { + return string.Equals(language, "en-US", StringComparison.OrdinalIgnoreCase) ? en : zh; + } + + private static string EscapeInline(string value) + { + return value.Replace("|", "\\|", StringComparison.Ordinal); + } + + private static string EscapeTableCell(string value) + { + return EscapeInline(value) + .Replace("\r", " ", StringComparison.Ordinal) + .Replace("\n", " ", StringComparison.Ordinal) + .Trim(); + } + + private static string EscapeCode(string value) + { + return value.Replace("`", "\\`", StringComparison.Ordinal); + } + + private static string SafeFenceLanguage(string language) + { + return string.IsNullOrWhiteSpace(language) ? string.Empty : new string(language.Where(char.IsLetterOrDigit).ToArray()).ToLowerInvariant(); + } +} diff --git a/src/YMhut.Box.Core/Tools/ToolMetadata.cs b/src/YMhut.Box.Core/Tools/ToolMetadata.cs new file mode 100644 index 0000000..884f868 --- /dev/null +++ b/src/YMhut.Box.Core/Tools/ToolMetadata.cs @@ -0,0 +1,10 @@ +namespace YMhut.Box.Core.Tools; + +public sealed record ToolMetadata( + string Id, + string Name, + string Description, + ToolCategory Category, + IReadOnlyList Keywords, + bool OfflineCapable, + string IconGlyph); diff --git a/src/YMhut.Box.Core/Tools/ToolModule.cs b/src/YMhut.Box.Core/Tools/ToolModule.cs new file mode 100644 index 0000000..c7bd344 --- /dev/null +++ b/src/YMhut.Box.Core/Tools/ToolModule.cs @@ -0,0 +1,13 @@ +namespace YMhut.Box.Core.Tools; + +public sealed class ToolModule(ToolMetadata metadata) : IToolModule +{ + public string Id => Metadata.Id; + + public ToolMetadata Metadata { get; } = metadata; + + public object CreateViewModel() + { + return new ToolModuleViewModel(Metadata); + } +} diff --git a/src/YMhut.Box.Core/Tools/ToolModuleViewModel.cs b/src/YMhut.Box.Core/Tools/ToolModuleViewModel.cs new file mode 100644 index 0000000..0521b15 --- /dev/null +++ b/src/YMhut.Box.Core/Tools/ToolModuleViewModel.cs @@ -0,0 +1,8 @@ +namespace YMhut.Box.Core.Tools; + +public sealed class ToolModuleViewModel(ToolMetadata metadata) +{ + public ToolMetadata Metadata { get; } = metadata; + + public bool InlinePreviewLoaded { get; set; } +} diff --git a/src/YMhut.Box.Core/Tools/ToolPageSpec.cs b/src/YMhut.Box.Core/Tools/ToolPageSpec.cs new file mode 100644 index 0000000..77e0dc0 --- /dev/null +++ b/src/YMhut.Box.Core/Tools/ToolPageSpec.cs @@ -0,0 +1,790 @@ +namespace YMhut.Box.Core.Tools; + +using YMhut.Box.Core.Settings; + +public enum ToolPrimaryInputKind +{ + None, + Text, + MultilineText, + Query, + Url, + Token, + Color, + FixedOptions, + FilePath, + DateRange, + Number, + NumberPair, + NumberTriple, + NumberQuad +} + +public enum ToolParameterControlKind +{ + None, + ComboBox, + RadioButtons, + ToggleSwitch, + NumberBox, + Slider, + CalendarDatePicker, + FilePicker, + TabView +} + +public enum ToolRulePresentationKind +{ + SelectionCards, + ComboBox, + RadioButtons, + ToggleSwitch, + Expander +} + +public enum ToolLayoutKind +{ + TextWorkbench, + FormatterWorkbench, + CodecWorkbench, + QueryDashboard, + Dashboard, + CalculatorForm, + UnitConverter, + ReferenceBrowser, + FileWorkflow, + MediaNative, + BrowserNative, + RandomCinema, + SystemLauncher, + SecurityWorkbench +} + +public enum ToolAssistPanelMode +{ + Hidden, + RulesOnly, + SourceOnly, + ExamplesOnly, + RulesAndSource, + Custom +} + +public enum ToolInteractionMode +{ + ManualRun, + AutoLoad, + LivePreview, + NativeLauncher, + MediaViewer, + ReferenceBrowser +} + +public enum ToolPageExperienceKind +{ + LivePreview, + OneShotTransform, + FormCalculator, + LookupBrowser, + RemoteDashboard, + FileWorkflow, + NativeWindow, + SystemLauncher, + MediaViewer +} + +public enum ToolPageDensity +{ + Compact, + Expanded +} + +public enum ToolResultPriority +{ + Value, + List, + Table, + Preview, + Document, + Status, + Media +} + +public enum ToolResultKind +{ + PlainText, + CodePreview, + JsonTree, + KeyValueCards, + Table, + RankedList, + NewsCards, + ImagePreview, + LinkCards, + FileCards, + Diff, + StatusList, + ColorSwatch, + CalculatorTable, + SystemCards, + Media, + ReferenceRows +} + +public sealed record ToolTextPair(string Zh, string En) +{ + public string ForLanguage(string? language) + { + return LanguagePreference.IsEnglish(language) ? En : Zh; + } +} + +public sealed record ToolRuleSpec( + string Key, + string Label, + ToolRulePresentationKind Presentation, + IReadOnlyList Options, + bool DefaultCollapsed = true, + bool IsRequired = false, + ToolTextPair? LocalizedLabel = null); + +public sealed record ToolRuleOptionSpec( + string Label, + string Value, + string Description = "", + ToolTextPair? LocalizedLabel = null, + ToolTextPair? LocalizedDescription = null); + +public sealed record ToolParameterSpec( + string Key, + string Label, + ToolParameterControlKind Control, + string Placeholder, + IReadOnlyList Options, + string DefaultValue = "", + bool IsRequired = false, + ToolTextPair? LocalizedLabel = null, + ToolTextPair? LocalizedPlaceholder = null); + +public sealed record ToolPageSpec( + string ToolId, + ToolPrimaryInputKind PrimaryInput, + ToolLayoutKind Layout, + ToolResultKind Result, + IReadOnlyList ParameterControls, + bool AutoRunOnOpen, + bool RequiresFilePicker, + bool UseVirtualizedResults, + IReadOnlyList Rules, + IReadOnlyList Parameters, + string EmptyState, + string ErrorHint, + ToolAssistPanelMode AssistPanelMode, + ToolInteractionMode InteractionMode, + ToolPageExperienceKind PageExperience, + ToolPageDensity PageDensity, + string PrimaryActionLabel, + IReadOnlyList SecondaryActions, + ToolResultPriority ResultPriority, + bool ShowInputHeader, + bool ShowDescription, + string DefaultFocusTarget) +{ + public bool UsesComboBox => ParameterControls.Contains(ToolParameterControlKind.ComboBox); + + public bool UsesRadioButtons => ParameterControls.Contains(ToolParameterControlKind.RadioButtons); + + public bool UsesDatePicker => ParameterControls.Contains(ToolParameterControlKind.CalendarDatePicker); + + public bool UsesNumberBox => ParameterControls.Contains(ToolParameterControlKind.NumberBox); +} + +public static class ToolPageSpecCatalog +{ + public static ToolPageSpec For(IToolModule module) + { + var id = module.Id; + return id switch + { + "safe_browser" => Spec(id, ToolPrimaryInputKind.Url, ToolLayoutKind.BrowserNative, ToolResultKind.LinkCards, controls: [ToolParameterControlKind.ComboBox]), + "media_player" => Spec(id, ToolPrimaryInputKind.FilePath, ToolLayoutKind.MediaNative, ToolResultKind.Media, controls: [ToolParameterControlKind.FilePicker], filePicker: true), + "random_cinema" => Spec(id, ToolPrimaryInputKind.None, ToolLayoutKind.RandomCinema, ToolResultKind.Media, controls: [ToolParameterControlKind.TabView], autoRun: true, virtualized: true), + "system_tool" => Spec(id, ToolPrimaryInputKind.FixedOptions, ToolLayoutKind.SystemLauncher, ToolResultKind.SystemCards, controls: [ToolParameterControlKind.ComboBox], autoRun: false, virtualized: true), + "system_info" or "pc_benchmark" => Spec(id, ToolPrimaryInputKind.None, ToolLayoutKind.Dashboard, ToolResultKind.SystemCards, controls: [ToolParameterControlKind.RadioButtons], autoRun: true), + "serial_terminal" => Spec(id, ToolPrimaryInputKind.None, ToolLayoutKind.SystemLauncher, ToolResultKind.StatusList, controls: [ToolParameterControlKind.None], autoRun: false), + "timestamp_converter" => Spec(id, ToolPrimaryInputKind.Text, ToolLayoutKind.CalculatorForm, ToolResultKind.CalculatorTable, controls: [ToolParameterControlKind.RadioButtons]), + "number_base" or "number_base_converter" => Spec(id, ToolPrimaryInputKind.Text, ToolLayoutKind.UnitConverter, ToolResultKind.CalculatorTable, controls: [ToolParameterControlKind.ComboBox]), + + "json_formatter" or "csv_json_converter" or "yaml_json_converter" or "json_path_helper" or "jwt_decoder" => + Spec(id, TextInput(id), ToolLayoutKind.FormatterWorkbench, ToolResultKind.JsonTree, controls: [ToolParameterControlKind.RadioButtons]), + "xml_formatter" or "sql_formatter" or "html_minifier" or "js_obfuscator" or "html_text_extractor" => + Spec(id, ToolPrimaryInputKind.MultilineText, ToolLayoutKind.FormatterWorkbench, ToolResultKind.CodePreview, controls: [ToolParameterControlKind.RadioButtons]), + "base64_codec" or "compression_codec" or "url_codec" or "html_entity" or "unicode_codec" or "punycode_codec" or "query_builder" or "form_urlencoded_builder" or "key_value_converter" => + Spec(id, TextInput(id), ToolLayoutKind.CodecWorkbench, ToolResultKind.KeyValueCards, controls: [ToolParameterControlKind.RadioButtons]), + "regex_tool" or "cron_helper" => + Spec(id, ToolPrimaryInputKind.MultilineText, ToolLayoutKind.FormatterWorkbench, ToolResultKind.Table, controls: [ToolParameterControlKind.RadioButtons, ToolParameterControlKind.ToggleSwitch]), + + "weather" or "train_query" or "dns_query" or "ip_lookup" or "ip_info" or "rdap_domain_lookup" or "rdap_ip_lookup" or "domain_price" or "http_diagnostic" or "url_redirect_trace" or "url_inspector" or "domain_extractor" or "cidr_subnet_calculator" or "wx_domain_check" => + Spec(id, QueryInput(id), ToolLayoutKind.QueryDashboard, ToolResultKind.KeyValueCards, controls: [ToolParameterControlKind.ComboBox], autoRun: !module.Metadata.OfflineCapable, virtualized: true), + "ai_latest_news" => + Spec(id, ToolPrimaryInputKind.Token, ToolLayoutKind.QueryDashboard, ToolResultKind.NewsCards, controls: [ToolParameterControlKind.None], autoRun: false, virtualized: true), + "city_route_query" => + Spec(id, ToolPrimaryInputKind.Query, ToolLayoutKind.QueryDashboard, ToolResultKind.KeyValueCards, controls: [ToolParameterControlKind.None], autoRun: false, virtualized: true), + "hotboard" => + Spec(id, ToolPrimaryInputKind.FixedOptions, ToolLayoutKind.Dashboard, ToolResultKind.RankedList, controls: [ToolParameterControlKind.ComboBox], autoRun: false, virtualized: true), + "history_today" => + Spec(id, ToolPrimaryInputKind.Query, ToolLayoutKind.Dashboard, ToolResultKind.RankedList, controls: [ToolParameterControlKind.None], autoRun: true, virtualized: true), + "baidu_hot" or "bili_hot" or "zhihu_hot" or "cctv_news" or "tech_news" or "football_news" or "movie_box_office" or "earthquake_info" or "gold_price" or "oil_price" => + Spec(id, ToolPrimaryInputKind.None, ToolLayoutKind.Dashboard, DashboardResult(id), controls: [ToolParameterControlKind.ComboBox], autoRun: true, virtualized: true), + "mime_lookup" or "http_status_lookup" or "port_lookup" or "dns_record_lookup" or "unicode_block_lookup" or "charset_lookup" or "timezone_abbr_lookup" or "regex_preset_lookup" or "magic_number_lookup" or "car_info" or "sanguosha_skin" => + Spec(id, ToolPrimaryInputKind.FixedOptions, ToolLayoutKind.ReferenceBrowser, ToolResultKind.ReferenceRows, controls: [ToolParameterControlKind.ComboBox], autoRun: true, virtualized: true), + + "calculator_tool" => Spec(id, ToolPrimaryInputKind.Text, ToolLayoutKind.CalculatorForm, ToolResultKind.CalculatorTable, controls: [ToolParameterControlKind.RadioButtons]), + "unit_converter" or "length_converter" or "weight_converter" or "temperature_converter" or "speed_converter" or "area_converter" or "volume_converter" or "data_size_converter" or "time_duration_converter" => + Spec(id, ToolPrimaryInputKind.Number, ToolLayoutKind.UnitConverter, ToolResultKind.CalculatorTable, controls: [ToolParameterControlKind.NumberBox, ToolParameterControlKind.ComboBox]), + "bmi_calculator" or "percentage_calculator" or "percentage_change_calculator" or "discount_calculator" or "tax_calculator" or "ratio_simplifier" or "average_calculator" or "median_calculator" or "gcd_lcm_calculator" or "permutation_calculator" or "combination_calculator" or "simple_interest_calculator" or "compound_interest_calculator" or "loan_emi_calculator" => + Spec(id, NumericInput(id), ToolLayoutKind.CalculatorForm, ToolResultKind.CalculatorTable, controls: [ToolParameterControlKind.NumberBox]), + "date_difference_calculator" or "workday_calculator" or "age_calculator" => + Spec(id, ToolPrimaryInputKind.DateRange, ToolLayoutKind.CalculatorForm, ToolResultKind.CalculatorTable, controls: [ToolParameterControlKind.CalendarDatePicker]), + "shelf_life" => Spec(id, ToolPrimaryInputKind.DateRange, ToolLayoutKind.CalculatorForm, ToolResultKind.KeyValueCards, controls: [ToolParameterControlKind.CalendarDatePicker, ToolParameterControlKind.NumberBox]), + + "qrcode_scanner" or "image_processor" or "image_metadata_inspector" or "archive_tool" or "hash_manifest_builder" or "hash_manifest_verify" => + Spec(id, ToolPrimaryInputKind.FilePath, ToolLayoutKind.FileWorkflow, FileResult(id), controls: [ToolParameterControlKind.FilePicker, ToolParameterControlKind.RadioButtons], filePicker: true, virtualized: true), + "qr_generator" => Spec(id, ToolPrimaryInputKind.MultilineText, ToolLayoutKind.FileWorkflow, ToolResultKind.ImagePreview, controls: [ToolParameterControlKind.RadioButtons]), + "color_picker" => Spec(id, ToolPrimaryInputKind.Color, ToolLayoutKind.TextWorkbench, ToolResultKind.ColorSwatch, controls: [ToolParameterControlKind.ComboBox]), + "color_contrast_checker" => Spec(id, ToolPrimaryInputKind.Text, ToolLayoutKind.TextWorkbench, ToolResultKind.KeyValueCards, controls: [ToolParameterControlKind.RadioButtons]), + "totp_generator" => Spec(id, ToolPrimaryInputKind.Token, ToolLayoutKind.SecurityWorkbench, ToolResultKind.KeyValueCards, controls: [ToolParameterControlKind.RadioButtons]), + "markdown_preview" => Spec(id, ToolPrimaryInputKind.MultilineText, ToolLayoutKind.FormatterWorkbench, ToolResultKind.CodePreview, controls: [ToolParameterControlKind.RadioButtons]), + "qq_avatar" => Spec(id, ToolPrimaryInputKind.Query, ToolLayoutKind.QueryDashboard, ToolResultKind.ImagePreview, controls: [ToolParameterControlKind.None], autoRun: false), + "html_js_playground" => Spec(id, ToolPrimaryInputKind.MultilineText, ToolLayoutKind.BrowserNative, ToolResultKind.CodePreview, controls: [ToolParameterControlKind.None], autoRun: false), + "ascii_art" or "password_generator" or "uuid_generator" or "ulid_generator" or "random_generator" or "random_picker" => + Spec(id, GeneratorInput(id), ToolLayoutKind.TextWorkbench, ToolResultKind.PlainText, controls: [ToolParameterControlKind.NumberBox, ToolParameterControlKind.RadioButtons]), + + _ when module.Metadata.Category == ToolCategory.Security => + Spec(id, ToolPrimaryInputKind.MultilineText, ToolLayoutKind.SecurityWorkbench, SecurityResult(id), controls: [ToolParameterControlKind.RadioButtons, ToolParameterControlKind.ToggleSwitch]), + _ when module.Metadata.Category == ToolCategory.Text => + Spec(id, ToolPrimaryInputKind.MultilineText, ToolLayoutKind.TextWorkbench, TextResult(id), controls: [ToolParameterControlKind.RadioButtons, ToolParameterControlKind.ToggleSwitch], virtualized: true), + _ when module.Metadata.Category == ToolCategory.Image => + Spec(id, ToolPrimaryInputKind.FilePath, ToolLayoutKind.FileWorkflow, ToolResultKind.FileCards, controls: [ToolParameterControlKind.FilePicker, ToolParameterControlKind.RadioButtons], filePicker: true), + _ when module.Metadata.Category == ToolCategory.Network => + Spec(id, ToolPrimaryInputKind.Query, ToolLayoutKind.QueryDashboard, ToolResultKind.LinkCards, controls: [ToolParameterControlKind.ComboBox], autoRun: !module.Metadata.OfflineCapable), + _ when module.Metadata.Category == ToolCategory.Data => + Spec(id, ToolPrimaryInputKind.None, ToolLayoutKind.Dashboard, ToolResultKind.RankedList, controls: [ToolParameterControlKind.ComboBox], autoRun: true, virtualized: true), + _ when module.Metadata.Category == ToolCategory.Calculator => + Spec(id, ToolPrimaryInputKind.Number, ToolLayoutKind.CalculatorForm, ToolResultKind.CalculatorTable, controls: [ToolParameterControlKind.NumberBox]), + _ when module.Metadata.Category == ToolCategory.Design => + Spec(id, ToolPrimaryInputKind.Text, ToolLayoutKind.TextWorkbench, ToolResultKind.ImagePreview, controls: [ToolParameterControlKind.RadioButtons]), + _ when module.Metadata.Category == ToolCategory.Life => + Spec(id, ToolPrimaryInputKind.Query, ToolLayoutKind.QueryDashboard, ToolResultKind.KeyValueCards, controls: [ToolParameterControlKind.ComboBox]), + _ when module.Metadata.Category == ToolCategory.System => + Spec(id, ToolPrimaryInputKind.FixedOptions, ToolLayoutKind.SystemLauncher, ToolResultKind.SystemCards, controls: [ToolParameterControlKind.ComboBox]), + _ => + Spec(id, ToolPrimaryInputKind.MultilineText, ToolLayoutKind.TextWorkbench, ToolResultKind.PlainText, controls: [ToolParameterControlKind.RadioButtons]) + }; + } + + public static IReadOnlyDictionary CreateFor(ToolCatalog catalog) + { + return catalog.Modules.ToDictionary(module => module.Id, For, StringComparer.OrdinalIgnoreCase); + } + + private static ToolPageSpec Spec( + string id, + ToolPrimaryInputKind input, + ToolLayoutKind layout, + ToolResultKind result, + IReadOnlyList? controls = null, + bool autoRun = false, + bool filePicker = false, + bool virtualized = false) + { + return new ToolPageSpec( + id, + input, + layout, + result, + controls is { Count: > 0 } ? controls : [ToolParameterControlKind.None], + autoRun, + filePicker, + virtualized, + BuildRules(id, input, controls ?? [ToolParameterControlKind.None]), + BuildParameters(id, input, controls ?? [ToolParameterControlKind.None]), + input == ToolPrimaryInputKind.None ? "Open this tool to refresh and render live data." : "Enter values or choose options, then run the tool.", + "The tool could not finish. Check the input and try again.", + ResolveAssistPanelMode(id, input, layout, controls ?? [ToolParameterControlKind.None], autoRun, filePicker), + ResolveInteractionMode(id, input, layout, autoRun), + ResolvePageExperience(id, input, layout, autoRun, filePicker), + ResolvePageDensity(result, layout, virtualized), + ResolvePrimaryActionLabel(id, input, layout, autoRun, filePicker), + ResolveSecondaryActions(input, filePicker, autoRun), + ResolveResultPriority(result), + ShouldShowInputHeader(input, layout, autoRun), + ShouldShowDescription(input, layout, autoRun), + ResolveDefaultFocusTarget(input, layout)); + } + + private static ToolPageExperienceKind ResolvePageExperience( + string id, + ToolPrimaryInputKind input, + ToolLayoutKind layout, + bool autoRun, + bool filePicker) + { + if (id == "color_picker") + { + return ToolPageExperienceKind.LivePreview; + } + + if (id == "hotboard") + { + return ToolPageExperienceKind.RemoteDashboard; + } + + return layout switch + { + ToolLayoutKind.BrowserNative => ToolPageExperienceKind.NativeWindow, + ToolLayoutKind.MediaNative or ToolLayoutKind.RandomCinema => ToolPageExperienceKind.MediaViewer, + ToolLayoutKind.SystemLauncher => ToolPageExperienceKind.SystemLauncher, + ToolLayoutKind.FileWorkflow => ToolPageExperienceKind.FileWorkflow, + ToolLayoutKind.ReferenceBrowser => ToolPageExperienceKind.LookupBrowser, + ToolLayoutKind.Dashboard or ToolLayoutKind.QueryDashboard when autoRun || input == ToolPrimaryInputKind.None => ToolPageExperienceKind.RemoteDashboard, + ToolLayoutKind.CalculatorForm or ToolLayoutKind.UnitConverter => ToolPageExperienceKind.FormCalculator, + _ when filePicker => ToolPageExperienceKind.FileWorkflow, + _ => ToolPageExperienceKind.OneShotTransform + }; + } + + private static ToolPageDensity ResolvePageDensity(ToolResultKind result, ToolLayoutKind layout, bool virtualized) + { + return virtualized || + layout is ToolLayoutKind.MediaNative or ToolLayoutKind.RandomCinema or ToolLayoutKind.FileWorkflow || + result is ToolResultKind.Table or ToolResultKind.RankedList or ToolResultKind.NewsCards or ToolResultKind.ReferenceRows or ToolResultKind.Media + ? ToolPageDensity.Expanded + : ToolPageDensity.Compact; + } + + private static string ResolvePrimaryActionLabel( + string id, + ToolPrimaryInputKind input, + ToolLayoutKind layout, + bool autoRun, + bool filePicker) + { + if (id == "color_picker") + { + return string.Empty; + } + + if (id == "hotboard") + { + return "查询"; + } + + if (filePicker || layout == ToolLayoutKind.FileWorkflow) + { + return id is "qrcode_scanner" ? "扫描" : id is "qr_generator" ? "生成" : "处理"; + } + + if (layout == ToolLayoutKind.SystemLauncher) + { + return "打开"; + } + + if (autoRun || input == ToolPrimaryInputKind.None) + { + return "重新加载"; + } + + if (layout is ToolLayoutKind.FormatterWorkbench) + { + return "格式化"; + } + + if (layout is ToolLayoutKind.CodecWorkbench or ToolLayoutKind.UnitConverter) + { + return "转换"; + } + + if (layout is ToolLayoutKind.CalculatorForm) + { + return "计算"; + } + + if (layout is ToolLayoutKind.QueryDashboard) + { + return "查询"; + } + + if (id.Contains("generator", StringComparison.OrdinalIgnoreCase)) + { + return "生成"; + } + + return "处理"; + } + + private static IReadOnlyList ResolveSecondaryActions(ToolPrimaryInputKind input, bool filePicker, bool autoRun) + { + var actions = new List(); + if (filePicker) + { + actions.Add("chooseFile"); + } + if (input != ToolPrimaryInputKind.None) + { + actions.Add("clear"); + } + actions.Add("copyResult"); + return actions; + } + + private static ToolResultPriority ResolveResultPriority(ToolResultKind result) + { + return result switch + { + ToolResultKind.JsonTree or ToolResultKind.CodePreview or ToolResultKind.Diff => ToolResultPriority.Document, + ToolResultKind.Table or ToolResultKind.CalculatorTable or ToolResultKind.ReferenceRows => ToolResultPriority.Table, + ToolResultKind.RankedList or ToolResultKind.NewsCards => ToolResultPriority.List, + ToolResultKind.ImagePreview or ToolResultKind.ColorSwatch => ToolResultPriority.Preview, + ToolResultKind.FileCards or ToolResultKind.StatusList or ToolResultKind.SystemCards => ToolResultPriority.Status, + ToolResultKind.Media => ToolResultPriority.Media, + _ => ToolResultPriority.Value + }; + } + + private static bool ShouldShowInputHeader(ToolPrimaryInputKind input, ToolLayoutKind layout, bool autoRun) + { + return input != ToolPrimaryInputKind.None && layout is not ToolLayoutKind.ReferenceBrowser && !autoRun; + } + + private static bool ShouldShowDescription(ToolPrimaryInputKind input, ToolLayoutKind layout, bool autoRun) + { + return input is ToolPrimaryInputKind.MultilineText or ToolPrimaryInputKind.FilePath or ToolPrimaryInputKind.DateRange || + layout is ToolLayoutKind.FileWorkflow or ToolLayoutKind.SecurityWorkbench || + (!autoRun && input != ToolPrimaryInputKind.None); + } + + private static string ResolveDefaultFocusTarget(ToolPrimaryInputKind input, ToolLayoutKind layout) + { + return input switch + { + ToolPrimaryInputKind.Query or ToolPrimaryInputKind.Url => "query", + ToolPrimaryInputKind.Number or ToolPrimaryInputKind.NumberPair or ToolPrimaryInputKind.NumberTriple or ToolPrimaryInputKind.NumberQuad => "number", + ToolPrimaryInputKind.DateRange => "date", + ToolPrimaryInputKind.FilePath => "file", + ToolPrimaryInputKind.FixedOptions => "parameter", + _ when layout == ToolLayoutKind.ReferenceBrowser => "parameter", + _ => "input" + }; + } + + private static ToolAssistPanelMode ResolveAssistPanelMode( + string id, + ToolPrimaryInputKind input, + ToolLayoutKind layout, + IReadOnlyList controls, + bool autoRun, + bool filePicker) + { + if (id == "color_picker") + { + return ToolAssistPanelMode.Custom; + } + + if (layout is ToolLayoutKind.BrowserNative or ToolLayoutKind.MediaNative or ToolLayoutKind.RandomCinema || + id is "system_info" or "pc_benchmark" or "system_tool") + { + return ToolAssistPanelMode.Hidden; + } + + if (input == ToolPrimaryInputKind.None && autoRun) + { + return ToolAssistPanelMode.Hidden; + } + + if (!filePicker && layout == ToolLayoutKind.QueryDashboard && autoRun) + { + return ToolAssistPanelMode.SourceOnly; + } + + if (layout == ToolLayoutKind.ReferenceBrowser || id == "smart_search") + { + return ToolAssistPanelMode.Hidden; + } + + if (id is "regex_tool" or "template_filler" or "batch_text_replace" or "qr_generator" or "path_breakdown" or "path_normalizer") + { + return ToolAssistPanelMode.ExamplesOnly; + } + + if (filePicker || + id is "json_formatter" or "xml_formatter" or "sql_formatter" or "html_minifier" or "js_obfuscator" + or "base64_codec" or "url_codec" or "html_entity" or "unicode_codec" or "punycode_codec" + or "text_sorter" or "text_deduplicator" or "duplicate_detector" or "manifest_deduplicator" + or "text_filter" or "keyword_counter" or "hash_generator" or "hmac_generator") + { + return ToolAssistPanelMode.RulesOnly; + } + + return ToolAssistPanelMode.Hidden; + } + + private static ToolInteractionMode ResolveInteractionMode( + string id, + ToolPrimaryInputKind input, + ToolLayoutKind layout, + bool autoRun) + { + if (id == "color_picker") + { + return ToolInteractionMode.LivePreview; + } + + return layout switch + { + ToolLayoutKind.BrowserNative => ToolInteractionMode.NativeLauncher, + ToolLayoutKind.MediaNative => ToolInteractionMode.MediaViewer, + ToolLayoutKind.RandomCinema => ToolInteractionMode.MediaViewer, + ToolLayoutKind.ReferenceBrowser => ToolInteractionMode.ReferenceBrowser, + _ when autoRun || input == ToolPrimaryInputKind.None => ToolInteractionMode.AutoLoad, + _ => ToolInteractionMode.ManualRun + }; + } + + private static IReadOnlyList BuildRules( + string id, + ToolPrimaryInputKind input, + IReadOnlyList controls) + { + static ToolTextPair Text(string zh, string en) => new(zh, en); + + if (id is "json_formatter" or "xml_formatter" or "sql_formatter" or "html_minifier" or "js_obfuscator") + { + return + [ + new ToolRuleSpec("mode", "Mode", ToolRulePresentationKind.RadioButtons, + [ + new("Format", "format", LocalizedLabel: Text("格式化", "Format")), + new("Minify", "minify", LocalizedLabel: Text("压缩", "Minify")), + new("Validate", "validate", LocalizedLabel: Text("校验", "Validate")) + ], LocalizedLabel: Text("模式", "Mode")) + ]; + } + + if (id is "base64_codec" or "url_codec" or "html_entity" or "unicode_codec" or "punycode_codec" or "query_builder" or "form_urlencoded_builder" or "key_value_converter") + { + return + [ + new ToolRuleSpec("direction", "Direction", ToolRulePresentationKind.SelectionCards, + [ + new("Encode", "encode", LocalizedLabel: Text("编码", "Encode")), + new("Decode", "decode", LocalizedLabel: Text("解码", "Decode")), + new("Both", "both", LocalizedLabel: Text("双向", "Both")) + ], LocalizedLabel: Text("方向", "Direction")) + ]; + } + + if (id is "text_sorter" or "manifest_sorter") + { + return + [ + new ToolRuleSpec("sort", "Sort order", ToolRulePresentationKind.RadioButtons, + [ + new("Ascending", "ascending", LocalizedLabel: Text("升序", "Ascending")), + new("Descending", "descending", LocalizedLabel: Text("降序", "Descending")), + new("Natural", "natural", LocalizedLabel: Text("自然排序", "Natural")) + ], LocalizedLabel: Text("排序方式", "Sort order")) + ]; + } + + if (id is "text_deduplicator" or "duplicate_detector" or "manifest_deduplicator") + { + return + [ + new ToolRuleSpec("dedupe", "Deduplication", ToolRulePresentationKind.SelectionCards, + [ + new("Keep first", "first", LocalizedLabel: Text("保留首次出现", "Keep first")), + new("Keep last", "last", LocalizedLabel: Text("保留最后出现", "Keep last")), + new("Ignore case", "ignoreCase", LocalizedLabel: Text("忽略大小写", "Ignore case")) + ], LocalizedLabel: Text("去重规则", "Deduplication")), + new ToolRuleSpec("preserveOrder", "Keep original order", ToolRulePresentationKind.ToggleSwitch, [], LocalizedLabel: Text("保留原始顺序", "Keep original order")) + ]; + } + + if (id is "text_filter" or "keyword_counter") + { + return + [ + new ToolRuleSpec("match", "Match mode", ToolRulePresentationKind.RadioButtons, + [ + new("Contains", "contains", LocalizedLabel: Text("包含", "Contains")), + new("Regex", "regex", LocalizedLabel: Text("正则", "Regex")), + new("Exclude", "exclude", LocalizedLabel: Text("排除", "Exclude")) + ], LocalizedLabel: Text("匹配模式", "Match mode")) + ]; + } + + if (id is "hash_generator" or "hmac_generator" or "hash_manifest_builder" or "hash_manifest_verify") + { + return + [ + new ToolRuleSpec("algorithm", "Algorithm", ToolRulePresentationKind.ComboBox, + [ + new("SHA256", "sha256"), + new("SHA512", "sha512"), + new("MD5", "md5") + ], LocalizedLabel: Text("算法", "Algorithm")) + ]; + } + + if (input == ToolPrimaryInputKind.FixedOptions || controls.Contains(ToolParameterControlKind.ComboBox)) + { + return + [ + new ToolRuleSpec("source", "Source", ToolRulePresentationKind.ComboBox, + [ + new("Default", "default", LocalizedLabel: Text("默认", "Default")), + new("All", "all", LocalizedLabel: Text("全部", "All")) + ], LocalizedLabel: Text("来源", "Source")) + ]; + } + + if (controls.Contains(ToolParameterControlKind.ToggleSwitch)) + { + return [new ToolRuleSpec("strict", "Strict mode", ToolRulePresentationKind.ToggleSwitch, [], LocalizedLabel: Text("严格模式", "Strict mode"))]; + } + + return []; + } + + private static IReadOnlyList BuildParameters( + string id, + ToolPrimaryInputKind input, + IReadOnlyList controls) + { + static ToolTextPair Text(string zh, string en) => new(zh, en); + + if (input == ToolPrimaryInputKind.None) + { + return []; + } + + if (input == ToolPrimaryInputKind.FixedOptions || controls.Contains(ToolParameterControlKind.ComboBox)) + { + return + [ + new ToolParameterSpec( + "preset", + "Preset", + ToolParameterControlKind.ComboBox, + "Choose a preset", + BuildPresetOptions(id), + BuildPresetOptions(id).FirstOrDefault()?.Value ?? "", + LocalizedLabel: Text("预设", "Preset"), + LocalizedPlaceholder: Text("选择预设", "Choose a preset")) + ]; + } + + if (input is ToolPrimaryInputKind.Number or ToolPrimaryInputKind.NumberPair or ToolPrimaryInputKind.NumberTriple or ToolPrimaryInputKind.NumberQuad) + { + return [new ToolParameterSpec("value", "Value", ToolParameterControlKind.NumberBox, "Enter a number", [], "1", true, Text("数值", "Value"), Text("输入数字", "Enter a number"))]; + } + + if (input == ToolPrimaryInputKind.DateRange) + { + return [new ToolParameterSpec("date", "Date", ToolParameterControlKind.CalendarDatePicker, "Choose date", [], string.Empty, true, Text("日期", "Date"), Text("选择日期", "Choose date"))]; + } + + if (input == ToolPrimaryInputKind.FilePath) + { + return [new ToolParameterSpec("path", "File path", ToolParameterControlKind.FilePicker, "Choose or paste a local file", [], string.Empty, true, Text("文件路径", "File path"), Text("选择或粘贴本地文件", "Choose or paste a local file"))]; + } + + return [new ToolParameterSpec("input", "Input", ToolParameterControlKind.None, "Enter input", [], string.Empty, false, Text("输入", "Input"), Text("输入内容", "Enter input"))]; + } + + private static IReadOnlyList BuildPresetOptions(string id) + { + static ToolTextPair Text(string zh, string en) => new(zh, en); + + return id switch + { + "system_tool" => + [ + new("System status", "status", LocalizedLabel: Text("系统状态", "System status")), + new("Temporary folder", "temp", LocalizedLabel: Text("临时目录", "Temporary folder")), + new("Processes", "process", LocalizedLabel: Text("进程", "Processes")), + new("Environment variables", "env", LocalizedLabel: Text("环境变量", "Environment variables")), + new("Remote Desktop", "rdp", LocalizedLabel: Text("远程桌面", "Remote Desktop")), + new("Paint", "paint", LocalizedLabel: Text("画图", "Paint")), + new("Calculator", "calculator", LocalizedLabel: Text("计算器", "Calculator")), + new("Control Panel", "control_panel", LocalizedLabel: Text("控制面板", "Control Panel")), + new("Registry Editor", "registry_editor", LocalizedLabel: Text("注册表编辑器", "Registry Editor")), + new("Task Manager", "task_manager", LocalizedLabel: Text("任务管理器", "Task Manager")), + new("Services", "services", LocalizedLabel: Text("服务", "Services")) + ], + "http_status_lookup" => [new("All status codes", "", LocalizedLabel: Text("全部状态码", "All status codes")), new("2xx", "2"), new("3xx", "3"), new("4xx", "4"), new("5xx", "5"), new("404", "404")], + "port_lookup" => [new("All ports", "", LocalizedLabel: Text("全部端口", "All ports")), new("Web service", "http", LocalizedLabel: Text("网页服务", "Web service")), new("Mail service", "mail", LocalizedLabel: Text("邮件服务", "Mail service")), new("Database", "sql", LocalizedLabel: Text("数据库", "Database")), new("Remote access", "remote", LocalizedLabel: Text("远程连接", "Remote access"))], + "mime_lookup" => [new("All MIME", "", LocalizedLabel: Text("全部 MIME", "All MIME")), new("Image", "image", LocalizedLabel: Text("图片", "Image")), new("Audio", "audio", LocalizedLabel: Text("音频", "Audio")), new("Video", "video", LocalizedLabel: Text("视频", "Video")), new("Text", "text", LocalizedLabel: Text("文本", "Text")), new("JSON", "json")], + "hotboard" => [new("百度热搜", "百度"), new("哔哩哔哩", "哔哩哔哩"), new("知乎", "知乎"), new("微博", "微博"), new("抖音", "抖音"), new("今日头条", "今日头条"), new("36氪", "36氪"), new("少数派", "少数派"), new("GitHub", "GitHub"), new("V2EX", "V2EX")], + "dns_record_lookup" => [new("All records", "", LocalizedLabel: Text("全部记录", "All records")), new("A", "A"), new("AAAA", "AAAA"), new("CNAME", "CNAME"), new("MX", "MX"), new("TXT", "TXT")], + _ => [new("Default", "", LocalizedLabel: Text("默认", "Default"))] + }; + } + + private static ToolPrimaryInputKind TextInput(string id) + { + return id is "base64_codec" or "url_codec" or "html_entity" or "unicode_codec" or "punycode_codec" + ? ToolPrimaryInputKind.Text + : ToolPrimaryInputKind.MultilineText; + } + + private static ToolPrimaryInputKind QueryInput(string id) + { + return id is "http_diagnostic" or "url_inspector" ? ToolPrimaryInputKind.Url : ToolPrimaryInputKind.Query; + } + + private static ToolPrimaryInputKind NumericInput(string id) + { + return id is "compound_interest_calculator" + ? ToolPrimaryInputKind.NumberQuad + : id is "loan_emi_calculator" or "simple_interest_calculator" + ? ToolPrimaryInputKind.NumberTriple + : id is "bmi_calculator" or "percentage_calculator" or "percentage_change_calculator" or "discount_calculator" or "tax_calculator" or "ratio_simplifier" or "permutation_calculator" or "combination_calculator" + ? ToolPrimaryInputKind.NumberPair + : ToolPrimaryInputKind.MultilineText; + } + + private static ToolPrimaryInputKind GeneratorInput(string id) + { + return id is "uuid_generator" or "ulid_generator" or "password_generator" or "random_generator" or "random_picker" + ? ToolPrimaryInputKind.Number + : ToolPrimaryInputKind.Text; + } + + private static ToolResultKind DashboardResult(string id) + { + return id is "cctv_news" or "tech_news" or "football_news" ? ToolResultKind.NewsCards : ToolResultKind.RankedList; + } + + private static ToolResultKind FileResult(string id) + { + return id is "qrcode_scanner" or "image_processor" ? ToolResultKind.ImagePreview : + id is "hash_manifest_verify" ? ToolResultKind.StatusList : + ToolResultKind.FileCards; + } + + private static ToolResultKind SecurityResult(string id) + { + return id is "hash_manifest_verify" ? ToolResultKind.StatusList : ToolResultKind.KeyValueCards; + } + + private static ToolResultKind TextResult(string id) + { + return id is "text_diff" ? ToolResultKind.Diff : + id is "markdown_table_normalizer" ? ToolResultKind.Table : + id.Contains("statistics", StringComparison.OrdinalIgnoreCase) || id.Contains("counter", StringComparison.OrdinalIgnoreCase) || id.Contains("analyzer", StringComparison.OrdinalIgnoreCase) + ? ToolResultKind.KeyValueCards + : ToolResultKind.PlainText; + } +} diff --git a/src/YMhut.Box.Core/Tools/ToolResultBuilder.cs b/src/YMhut.Box.Core/Tools/ToolResultBuilder.cs new file mode 100644 index 0000000..d9c4f43 --- /dev/null +++ b/src/YMhut.Box.Core/Tools/ToolResultBuilder.cs @@ -0,0 +1,1013 @@ +using System.Text.Json; +using System.Text.RegularExpressions; +using System.Globalization; +using YMhut.Box.Core.Api; + +namespace YMhut.Box.Core.Tools; + +public static class ToolResultBuilder +{ + public static ToolResultDocument FromOutput(IToolModule module, string output, string language = "zh-CN") + { + var spec = ToolPageSpecCatalog.For(module); + var lines = Lines(output); + var blocks = BuildBlocks(module, spec, output, lines, language); + if (blocks.Count == 0) + { + blocks = ToolResultDocument.FromOutput(module, output).Blocks; + } + + return Document(module.Id, spec.Result, output, blocks, language: language); + } + + public static ToolResultDocument FromRemote(ApiEndpoint endpoint, ApiResponse response, string output, string language = "zh-CN") + { + var lines = Lines(output); + var displayLines = RemoveSourceHeader(lines); + var blocks = new List(); + blocks.AddRange(BuildRemoteBlocks(endpoint.Id, displayLines, language)); + if (blocks.Count == 0) + { + blocks.AddRange(GenericTextBlocks(displayLines.Count > 0 ? displayLines : lines, language)); + } + + blocks.Add(ToolResultBlock.KeyValue( + T(language, "来源与隐私", "Source and privacy"), + [ + Pair(T(language, "数据源", "Data source"), endpoint.SourceName), + Pair(T(language, "来源类型", "Source type"), SourceTypeLabel(endpoint, language)), + Pair(T(language, "获取时间", "Fetched at"), response.FetchedAt.ToString("yyyy-MM-dd HH:mm:ss zzz")) + ], + SourceMetadata(endpoint))); + + return new ToolResultDocument( + endpoint.Id, + GuessRemoteResultKind(endpoint.Id), + output, + blocks, + RemoteMetadata(endpoint, GuessRemoteResultKind(endpoint.Id), language), + output, + endpoint.SourceName, + response.Success ? "ok" : "error"); + } + + private static IReadOnlyList BuildBlocks(IToolModule module, ToolPageSpec spec, string output, IReadOnlyList lines, string language) + { + if (string.IsNullOrWhiteSpace(output)) + { + return [ToolResultBlock.List(ToolResultBlockKind.Status, T(language, "空结果", "Empty result"), [new("info", T(language, "暂无可展示的结果。", "There is no result to display."), string.Empty, "info", string.Empty)])]; + } + + if (module.Id is "qr_generator" && LooksLikeSvg(output)) + { + return + [ + ToolResultBlock.Media( + ToolResultBlockKind.Image, + T(language, "二维码预览", "QR code preview"), + output, + string.Empty, + new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["contentType"] = "image/svg+xml", + ["displayMode"] = "preview", + ["rawSvg"] = output + }) + ]; + } + + if (module.Id is "weather") + { + return [ToolResultBlock.Metric(T(language, "天气摘要", "Weather summary"), KeyValues(lines).ToArray())]; + } + + if (module.Id is "train_query") + { + return BuildTrainBlocks(lines, language); + } + + if (module.Id is "dns_query") + { + return BuildDnsBlocks(lines, language); + } + + if (module.Id is "ip_lookup" or "ip_info" or "rdap_ip_lookup" or "rdap_domain_lookup" or "domain_price") + { + return BuildRdapBlocks(lines, language); + } + + if (IsReferenceTool(module.Id)) + { + return BuildReferenceBlocksV2(lines, language); + } + + if (module.Id is "smart_search") + { + return BuildSearchBlocks(lines, language); + } + + if (module.Id is "system_tool" or "system_info" or "pc_benchmark") + { + return BuildSystemBlocks(lines, language); + } + + if (spec.Result == ToolResultKind.JsonTree || LooksLikeJson(output)) + { + return BuildJsonBlocks(output, language); + } + + if (module.Id is "jwt_decoder") + { + return BuildJwtBlocks(output, language); + } + + if (spec.Result == ToolResultKind.Diff || module.Id is "text_diff") + { + return [ToolResultBlock.List(ToolResultBlockKind.Diff, T(language, "差异结果", "Diff result"), lines.Select(line => new ToolResultListItem(DiffLeading(line), line, string.Empty, DiffStatus(line), string.Empty)).ToArray())]; + } + + if (module.Id is "base64_codec" or "url_codec" or "html_entity" or "unicode_codec" or "punycode_codec") + { + return BuildCodecBlocks(lines, language); + } + + if (module.Id is "hash_generator" or "hmac_generator") + { + return [ToolResultBlock.Table(T(language, "摘要值", "Digest values"), DigestRows(lines, language))]; + } + + if (spec.Result == ToolResultKind.CodePreview || module.Id.Contains("formatter", StringComparison.OrdinalIgnoreCase) || module.Id.Contains("minifier", StringComparison.OrdinalIgnoreCase)) + { + return [ToolResultBlock.Code(T(language, "代码预览", "Code preview"), output, CodeLanguage(module.Id))]; + } + + if (spec.Result == ToolResultKind.CalculatorTable || module.Metadata.Category == ToolCategory.Calculator || spec.Layout is ToolLayoutKind.CalculatorForm or ToolLayoutKind.UnitConverter) + { + return [ToolResultBlock.Metric(T(language, "计算结果", "Calculation result"), MetricPairs(lines).ToArray())]; + } + + if (spec.Result is ToolResultKind.FileCards or ToolResultKind.ImagePreview or ToolResultKind.Media or ToolResultKind.ColorSwatch) + { + return ToolResultDocument.FromOutput(module, output).Blocks; + } + + if (spec.Result == ToolResultKind.Table) + { + var rows = ParseTableRows(lines); + return rows.Count == 0 ? GenericTextBlocks(lines, language) : [ToolResultBlock.Table(T(language, "表格结果", "Table result"), rows)]; + } + + if (spec.Result is ToolResultKind.RankedList or ToolResultKind.NewsCards) + { + return spec.Result == ToolResultKind.NewsCards + ? BuildNewsCards(lines, T(language, "新闻列表", "News list")) + : BuildRankedCards(lines, T(language, "榜单", "Ranked list")); + } + + if (spec.Result == ToolResultKind.StatusList) + { + return [ToolResultBlock.List(ToolResultBlockKind.Status, T(language, "状态", "Status"), lines.Select(line => new ToolResultListItem(StatusLeading(line), line, string.Empty, StatusOf(line), string.Empty)).ToArray())]; + } + + var keyValues = KeyValues(lines).ToArray(); + if (keyValues.Length >= 2) + { + return [ToolResultBlock.KeyValue(T(language, "详情", "Details"), keyValues)]; + } + + return GenericTextBlocks(lines, language); + } + + private static IReadOnlyList BuildRemoteBlocks(string id, IReadOnlyList lines, string language) + { + if (id.Equals("weather", StringComparison.OrdinalIgnoreCase)) + { + return BuildWeatherBlocks(lines, language); + } + + return id switch + { + "dns_query" => BuildDnsBlocks(lines, language), + "weather" => [ToolResultBlock.Metric(T(language, "天气摘要", "Weather summary"), KeyValues(lines).ToArray())], + "ip_info" or "ip_lookup" or "rdap_ip_lookup" or "rdap_domain_lookup" or "domain_price" => BuildRdapBlocks(lines, language), + "baidu_hot" or "hotboard" or "bili_hot" or "zhihu_hot" or "earthquake_info" or "movie_box_office" => BuildRankedCards(lines, T(language, "数据列表", "Data list")), + "history_today" => BuildHistoryBlocks(lines, language), + "ai_latest_news" or "tech_news" or "football_news" or "cctv_news" => BuildNewsCards(lines, T(language, "新闻列表", "News list")), + "gold_price" => BuildGoldPriceBlocks(lines, language), + "oil_price" or "wx_domain_check" or "http_diagnostic" => BuildHtmlSummaryBlocks(lines, language), + "city_route_query" => BuildMetricTextBlocks(lines, T(language, "城际路线", "City route"), language), + "train_query" => BuildTrainBlocks(lines, language), + _ => [] + }; + } + + private static IReadOnlyList BuildHistoryBlocks(IReadOnlyList lines, string language) + { + var blocks = new List(); + var currentSection = string.Empty; + var items = new List(); + + void Flush() + { + if (items.Count > 0) + { + blocks.Add(ToolResultBlock.Timeline( + string.IsNullOrWhiteSpace(currentSection) ? T(language, "历史事件", "History") : currentSection, + items.ToArray(), + BlockMeta("text/plain", "timeline", "normal"))); + items.Clear(); + } + } + + foreach (var line in lines) + { + var trimmed = line.Trim(); + if (IsRemoteHeaderOrPrivacyLine(trimmed)) + { + continue; + } + if (string.IsNullOrWhiteSpace(trimmed)) + { + continue; + } + + if (!trimmed.StartsWith("-", StringComparison.Ordinal) && trimmed == trimmed.ToUpperInvariant() && trimmed.Length <= 24) + { + Flush(); + currentSection = trimmed; + continue; + } + + var match = Regex.Match(trimmed, @"^-?\s*(?-?\d{1,4})\s*:\s*(?.+)$"); + if (match.Success) + { + items.Add(new ToolResultListItem(match.Groups["year"].Value, match.Groups["text"].Value, currentSection, "info", ExtractLinks([line]).FirstOrDefault() ?? string.Empty)); + } + else + { + items.Add(new ToolResultListItem("•", trimmed.TrimStart('-', ' '), currentSection, "info", ExtractLinks([line]).FirstOrDefault() ?? string.Empty)); + } + } + + Flush(); + return blocks.Count == 0 ? GenericTextBlocks(lines, language) : blocks; + } + + private static IReadOnlyList BuildMetricTextBlocks(IReadOnlyList lines, string title, string language) + { + var pairs = KeyValues(lines).ToArray(); + if (pairs.Length > 0) + { + return [ToolResultBlock.Metric(title, pairs, BlockMeta("metric", "summary", "high"))]; + } + + return BuildHtmlSummaryBlocks(lines, language); + } + + private static IReadOnlyList BuildGoldPriceBlocks(IReadOnlyList lines, string language) + { + var blocks = new List(); + var pairs = KeyValues(lines).ToArray(); + if (pairs.Length > 0) + { + blocks.Add(ToolResultBlock.Metric(T(language, "黄金价格", "Gold price"), pairs, BlockMeta("metric", "summary", "high"))); + } + + var rows = ParseTableRows(lines); + if (rows.Count > 1) + { + blocks.Add(ToolResultBlock.Table(T(language, "近期定盘价", "Recent fixes"), rows, BlockMeta("table", "details", "normal"))); + + var chartRows = rows + .Skip(1) + .Where(row => row.Length >= 2 && TryParseDecimal(row[1], out _)) + .Select(row => new[] { row[0], row[1] }) + .Take(60) + .ToArray(); + if (chartRows.Length >= 2) + { + blocks.Add(ToolResultBlock.LineChart( + T(language, "USD/oz 趋势", "USD/oz trend"), + new[] { new[] { T(language, "日期", "Date"), "USD/oz" } }.Concat(chartRows).ToArray(), + new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["contentType"] = "chart/line", + ["displayMode"] = "chart", + ["priority"] = "high", + ["chartUnit"] = "USD/oz" + })); + } + } + + return blocks.Count == 0 + ? BuildMetricTextBlocks(lines, T(language, "黄金价格", "Gold price"), language) + : blocks; + } + + private static IReadOnlyList BuildHtmlSummaryBlocks(IReadOnlyList lines, string language) + { + var blocks = new List(); + var title = lines.FirstOrDefault(line => !string.IsNullOrWhiteSpace(line) && !Regex.IsMatch(line, @"^\d+[\.\)]")) ?? string.Empty; + if (!string.IsNullOrWhiteSpace(title)) + { + blocks.Add(ToolResultBlock.Metric( + T(language, "查询摘要", "Query summary"), + [Pair(T(language, "标题", "Title"), title)], + BlockMeta("text/plain", "summary", "high"))); + } + + var cards = BuildRankedCards(lines.Where(line => Regex.IsMatch(line.Trim(), @"^\d+[\.\)]")).ToArray(), T(language, "详情", "Details")); + blocks.AddRange(cards); + + if (blocks.Count == 0) + { + blocks.AddRange(GenericTextBlocks(lines, language)); + } + + return blocks; + } + + private static IReadOnlyList BuildJsonBlocks(string output, string language) + { + try + { + using var json = JsonDocument.Parse(output); + var root = json.RootElement; + var blocks = new List + { + ToolResultBlock.JsonTree(T(language, "JSON 树", "JSON tree"), JsonSerializer.Serialize(root, new JsonSerializerOptions { WriteIndented = true })) + }; + + if (root.ValueKind == JsonValueKind.Object) + { + blocks.Insert(0, ToolResultBlock.KeyValue( + T(language, "字段摘要", "Field summary"), + root.EnumerateObject().Take(40).Select(property => Pair(property.Name, JsonPreview(property.Value))).ToArray())); + } + else if (root.ValueKind == JsonValueKind.Array) + { + blocks.Insert(0, ToolResultBlock.Metric(T(language, "数组摘要", "Array summary"), [Pair(T(language, "项目数", "Items"), root.GetArrayLength().ToString())])); + var rows = JsonArrayRows(root).ToArray(); + if (rows.Length > 0) + { + blocks.Insert(1, ToolResultBlock.Table(T(language, "数组预览", "Array preview"), rows)); + } + } + + return blocks; + } + catch (JsonException exception) + { + return + [ + ToolResultBlock.List(ToolResultBlockKind.Status, T(language, "JSON 解析失败", "JSON parse failed"), [new("error", exception.Message, string.Empty, "error", string.Empty)]), + ToolResultBlock.Raw(T(language, "原始内容", "Raw content"), output, "json") + ]; + } + } + + private static IReadOnlyList BuildJwtBlocks(string output, string language) + { + var blocks = new List(); + var sections = output.Replace("\r\n", "\n") + .Split("\n\n", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .Where(section => section.TrimStart().StartsWith('{') || section.TrimStart().StartsWith('[')) + .Take(3) + .ToArray(); + foreach (var section in sections) + { + blocks.AddRange(BuildJsonBlocks(section, language)); + } + + return blocks.Count == 0 ? [ToolResultBlock.Code("JWT", output, "json")] : blocks; + } + + private static IReadOnlyList BuildDnsBlocks(IReadOnlyList lines, string language) + { + var records = new List + { + new[] { T(language, "名称", "Name"), "TYPE", "TTL", T(language, "数据", "Data") } + }; + var details = new List>(); + foreach (var line in lines) + { + var match = Regex.Match(line, @"^(?.*?)\s+TYPE\s+(?\S+)\s+TTL\s+(?\S+)\s+(?.*)$", RegexOptions.IgnoreCase); + if (match.Success) + { + records.Add([match.Groups["name"].Value, match.Groups["type"].Value, match.Groups["ttl"].Value, match.Groups["data"].Value]); + continue; + } + + details.AddRange(KeyValues([line])); + } + + var blocks = new List(); + if (details.Count > 0) + { + blocks.Add(ToolResultBlock.KeyValue(T(language, "查询摘要", "Query summary"), details)); + } + if (records.Count > 1) + { + blocks.Add(ToolResultBlock.Table("DNS", records)); + } + return blocks; + } + + private static IReadOnlyList BuildWeatherBlocks(IReadOnlyList lines, string language) + { + var pairs = KeyValues(lines).ToList(); + var blocks = new List(); + if (pairs.Count > 0) + { + blocks.Add(ToolResultBlock.Metric( + T(language, "天气摘要", "Weather summary"), + pairs.Take(8).ToArray(), + BlockMeta("metric", "summary", "high"))); + } + + var forecastRows = new List { new[] { T(language, "日期", "Date"), T(language, "最低", "Low"), T(language, "最高", "High"), T(language, "说明", "Note") } }; + foreach (var line in lines) + { + var match = Regex.Match(line, @"^(?\d{4}-\d{2}-\d{2})\s*:\s*(?-?\d+(?:\.\d+)?)\s*-\s*(?-?\d+(?:\.\d+)?)(?.*)$"); + if (match.Success) + { + forecastRows.Add([ + match.Groups["date"].Value, + match.Groups["low"].Value, + match.Groups["high"].Value, + match.Groups["unit"].Value.Trim() + ]); + } + } + + if (forecastRows.Count > 1) + { + blocks.Add(ToolResultBlock.Table( + T(language, "未来预报", "Forecast"), + forecastRows, + BlockMeta("table", "details", "normal"))); + } + + return blocks.Count > 0 ? blocks : GenericTextBlocks(lines, language); + } + + private static IReadOnlyList BuildRdapBlocks(IReadOnlyList lines, string language) + { + var events = new List(); + var pairs = new List>(); + foreach (var pair in KeyValues(lines)) + { + if (pair.Key.Contains("event", StringComparison.OrdinalIgnoreCase) || pair.Key.Contains("Notice", StringComparison.OrdinalIgnoreCase)) + { + events.Add(new ToolResultListItem("•", pair.Key, pair.Value, "info", string.Empty)); + } + else + { + pairs.Add(pair); + } + } + + var blocks = new List(); + if (pairs.Count > 0) + { + blocks.Add(ToolResultBlock.KeyValue("RDAP", pairs)); + } + if (events.Count > 0) + { + blocks.Add(ToolResultBlock.Timeline(T(language, "事件/公告", "Events / notices"), events)); + } + return blocks; + } + + private static IReadOnlyList BuildReferenceBlocks(IReadOnlyList lines, string language) + { + var rows = new List { new[] { T(language, "代码", "Code"), T(language, "名称", "Name"), T(language, "说明", "Description"), T(language, "备注", "Note") } }; + var sourcePairs = new List>(); + foreach (var line in lines) + { + if (line.StartsWith("数据源:", StringComparison.Ordinal) || line.StartsWith("Data source:", StringComparison.OrdinalIgnoreCase)) + { + sourcePairs.Add(Pair(T(language, "数据源", "Data source"), SplitValue(line))); + continue; + } + + var match = Regex.Match(line, @"^(?.*?)\s+-\s+(?.*?)(?:[::]\s*(?.*?))?(?:\s+/\s+(?.*))?$"); + if (match.Success) + { + rows.Add([match.Groups["code"].Value, match.Groups["name"].Value, match.Groups["desc"].Value, match.Groups["note"].Value]); + } + } + + var blocks = new List(); + if (sourcePairs.Count > 0) + { + blocks.Add(ToolResultBlock.KeyValue(T(language, "来源", "Source"), sourcePairs)); + } + if (rows.Count > 1) + { + blocks.Add(ToolResultBlock.Table(T(language, "参考数据", "Reference data"), rows)); + } + return blocks.Count == 0 ? GenericTextBlocks(lines, language) : blocks; + } + + private static IReadOnlyList BuildSearchBlocks(IReadOnlyList lines, string language) + { + var pairs = KeyValues(lines).ToList(); + var links = ExtractLinks(lines).Distinct(StringComparer.OrdinalIgnoreCase).Select(link => ToolResultBlock.Link(T(language, "打开入口", "Open entry"), link, link)).ToList(); + var blocks = new List(); + if (pairs.Count > 0) + { + blocks.Add(ToolResultBlock.KeyValue(T(language, "搜索信息", "Search info"), pairs)); + } + blocks.AddRange(links); + return blocks.Count == 0 ? GenericTextBlocks(lines, language) : blocks; + } + + private static IReadOnlyList BuildReferenceBlocksV2(IReadOnlyList lines, string language) + { + var rows = new List { new[] { T(language, "代码", "Code"), T(language, "名称", "Name"), T(language, "说明", "Description"), T(language, "备注", "Note") } }; + var sourcePairs = new List>(); + foreach (var line in lines) + { + if (IsSourceLine(line)) + { + sourcePairs.Add(Pair(T(language, "数据源", "Data source"), SplitValue(line))); + continue; + } + + var match = Regex.Match(line, @"^(?.*?)\s+-\s+(?.*?)(?:[::]\s*(?.*?))?(?:\s+/\s+(?.*))?$"); + if (match.Success) + { + rows.Add([match.Groups["code"].Value, match.Groups["name"].Value, match.Groups["desc"].Value, match.Groups["note"].Value]); + } + } + + var blocks = new List(); + if (rows.Count > 1) + { + blocks.Add(ToolResultBlock.Metric( + T(language, "参考摘要", "Reference summary"), + [ + Pair(T(language, "命中数", "Matches"), (rows.Count - 1).ToString()), + Pair(T(language, "展示上限", "Display limit"), "80") + ], + BlockMeta("metric", "summary", "high"))); + blocks.Add(ToolResultBlock.Table(T(language, "参考数据", "Reference data"), rows, BlockMeta("table", "details", "normal"))); + } + if (sourcePairs.Count > 0) + { + blocks.Add(ToolResultBlock.KeyValue(T(language, "来源", "Source"), sourcePairs, BlockMeta("text/plain", "source", "normal"))); + } + + return blocks.Count == 0 ? GenericTextBlocks(lines, language) : blocks; + } + + private static IReadOnlyList BuildCodecBlocks(IReadOnlyList lines, string language) + { + var blocks = new List(); + var sections = new List>(); + var currentTitle = string.Empty; + var currentText = new List(); + + void Flush() + { + if (!string.IsNullOrWhiteSpace(currentTitle)) + { + sections.Add(Pair(currentTitle.TrimEnd(':'), string.Join(Environment.NewLine, currentText).Trim())); + } + currentText.Clear(); + } + + foreach (var line in lines) + { + if (line.EndsWith(":", StringComparison.Ordinal) && line.Length < 32) + { + Flush(); + currentTitle = line; + } + else + { + currentText.Add(line); + } + } + Flush(); + + if (sections.Count > 0) + { + blocks.Add(ToolResultBlock.KeyValue(T(language, "转换结果", "Conversion result"), sections)); + } + else + { + blocks.Add(ToolResultBlock.Code(T(language, "转换文本", "Converted text"), string.Join(Environment.NewLine, lines))); + } + return blocks; + } + + private static IReadOnlyList DigestRows(IReadOnlyList lines, string language) + { + var rows = new List { new[] { T(language, "算法", "Algorithm"), T(language, "摘要", "Digest") } }; + foreach (var pair in KeyValues(lines)) + { + rows.Add([pair.Key, pair.Value]); + } + if (rows.Count == 1) + { + rows.AddRange(lines.Select((line, index) => new[] { (index + 1).ToString(), line })); + } + return rows; + } + + private static IReadOnlyList BuildSystemBlocks(IReadOnlyList lines, string language) + { + var metrics = KeyValues(lines).ToArray(); + return metrics.Length > 0 + ? [ToolResultBlock.Metric(T(language, "系统摘要", "System summary"), metrics)] + : GenericTextBlocks(lines, language); + } + + private static IReadOnlyList BuildTrainBlocks(IReadOnlyList lines, string language) + { + var rows = new List { new[] { T(language, "车次/车站", "Train / station"), T(language, "信息", "Info"), T(language, "备注", "Note") } }; + foreach (var line in lines.Where(line => !line.Contains("数据源", StringComparison.Ordinal) && !line.StartsWith("Fetched", StringComparison.OrdinalIgnoreCase))) + { + var parts = line.Split(" ", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + if (parts.Length >= 2) + { + rows.Add([parts[0], string.Join(" / ", parts.Skip(1)), string.Empty]); + } + else if (line.Contains(" / ", StringComparison.Ordinal)) + { + var station = line.Split(" / ", StringSplitOptions.TrimEntries); + rows.Add([station.ElementAtOrDefault(0) ?? string.Empty, station.ElementAtOrDefault(1) ?? string.Empty, station.ElementAtOrDefault(2) ?? string.Empty]); + } + } + return rows.Count > 1 ? [ToolResultBlock.Table(T(language, "列车/车站", "Train / station"), rows)] : GenericTextBlocks(lines, language); + } + + private static IReadOnlyList BuildRankedCards(IReadOnlyList lines, string title) + { + var items = new List(); + foreach (var line in lines) + { + var trimmed = line.Trim(); + if (IsRemoteHeaderOrPrivacyLine(trimmed)) + { + continue; + } + var match = Regex.Match(trimmed, @"^(?\d+)[\.\)、]\s*(?.*)$"); + if (match.Success) + { + var visibleTitle = CleanVisibleText(match.Groups["title"].Value); + var uri = ExtractLinks([line]).FirstOrDefault() ?? string.Empty; + items.Add(new ToolResultListItem(match.Groups["rank"].Value, visibleTitle, string.Empty, "info", uri)); + } + else if (items.Count > 0 && trimmed.Length > 0 && !trimmed.Contains("数据源", StringComparison.Ordinal)) + { + var previous = items[^1]; + var visible = CleanVisibleText(trimmed); + items[^1] = previous with { Subtitle = string.IsNullOrWhiteSpace(previous.Subtitle) ? visible : $"{previous.Subtitle}\n{visible}" }; + } + } + return items.Count == 0 ? [] : [ToolResultBlock.List(ToolResultBlockKind.RankedList, title, items)]; + } + + private static IReadOnlyList<ToolResultBlock> BuildNewsCards(IReadOnlyList<string> lines, string title) + { + var items = new List<ToolResultListItem>(); + foreach (var line in lines) + { + var trimmed = line.Trim(); + if (IsRemoteHeaderOrPrivacyLine(trimmed) || string.IsNullOrWhiteSpace(trimmed)) + { + continue; + } + + var match = Regex.Match(trimmed, @"^(?<rank>\d+)[\.\)\u3001\uff09\s]+(?<title>.*)$"); + if (match.Success) + { + var visibleTitle = CleanVisibleText(match.Groups["title"].Value); + var uri = ExtractLinks([line]).FirstOrDefault() ?? string.Empty; + items.Add(new ToolResultListItem(match.Groups["rank"].Value, visibleTitle, string.Empty, "info", uri)); + continue; + } + + if (items.Count > 0) + { + var previous = items[^1]; + var visible = CleanVisibleText(trimmed); + items[^1] = previous with { Subtitle = string.IsNullOrWhiteSpace(previous.Subtitle) ? visible : $"{previous.Subtitle}\n{visible}" }; + } + } + + return items.Count == 0 ? [] : [ToolResultBlock.List(ToolResultBlockKind.NewsList, title, items)]; + } + + private static IReadOnlyList<ToolResultBlock> GenericTextBlocks(IReadOnlyList<string> lines, string language) + { + return [ToolResultBlock.List(ToolResultBlockKind.Text, T(language, "结果", "Result"), lines.Take(160).Select((line, index) => new ToolResultListItem((index + 1).ToString(), CleanVisibleText(line), string.Empty, "info", ExtractLinks([line]).FirstOrDefault() ?? string.Empty)).ToArray())]; + } + + private static IReadOnlyList<KeyValuePair<string, string>> KeyValues(IReadOnlyList<string> lines) + { + var pairs = new List<KeyValuePair<string, string>>(); + foreach (var line in lines) + { + var normalized = line.Trim(); + var separators = new[] { ":", ":", "=", "\t" }; + foreach (var separator in separators) + { + var index = normalized.IndexOf(separator, StringComparison.Ordinal); + if (index > 0 && index < normalized.Length - separator.Length) + { + var key = normalized[..index].Trim().Trim('-', '*', ' '); + var value = normalized[(index + separator.Length)..].Trim(); + if (key.Length is > 0 and < 64 && value.Length > 0) + { + pairs.Add(Pair(key, value)); + } + break; + } + } + } + return pairs; + } + + private static IEnumerable<KeyValuePair<string, string>> MetricPairs(IReadOnlyList<string> lines) + { + var pairs = KeyValues(lines).ToArray(); + if (pairs.Length > 0) + { + return pairs; + } + return lines.Take(12).Select((line, index) => Pair((index + 1).ToString(), line)); + } + + private static IReadOnlyList<string[]> ParseTableRows(IReadOnlyList<string> lines) + { + return lines + .Where(line => line.Contains('|') || line.Contains('\t')) + .Select(line => line.Contains('|') ? line.Trim().Trim('|').Split('|', StringSplitOptions.TrimEntries) : line.Split('\t', StringSplitOptions.TrimEntries)) + .Where(row => row.Length >= 2) + .Take(80) + .ToArray(); + } + + private static bool TryParseDecimal(string value, out decimal number) + { + var cleaned = Regex.Replace(value ?? string.Empty, @"[^\d\.\,\-]", string.Empty).Replace(",", string.Empty, StringComparison.Ordinal); + return decimal.TryParse(cleaned, NumberStyles.Float, CultureInfo.InvariantCulture, out number) || + decimal.TryParse(cleaned, NumberStyles.Float, CultureInfo.CurrentCulture, out number); + } + + private static IEnumerable<string[]> JsonArrayRows(JsonElement root) + { + if (root.ValueKind != JsonValueKind.Array) + { + yield break; + } + var items = root.EnumerateArray().Take(20).ToArray(); + if (items.Length == 0) + { + yield break; + } + if (items.Any(item => item.ValueKind != JsonValueKind.Object)) + { + yield return ["#", "Value"]; + for (var index = 0; index < items.Length; index++) + { + yield return [(index + 1).ToString(), JsonPreview(items[index])]; + } + yield break; + } + var headers = items.SelectMany(item => item.EnumerateObject().Select(property => property.Name)).Distinct(StringComparer.Ordinal).Take(8).ToArray(); + yield return headers; + foreach (var item in items) + { + yield return headers.Select(header => item.TryGetProperty(header, out var value) ? JsonPreview(value) : string.Empty).ToArray(); + } + } + + private static string JsonPreview(JsonElement value) + { + return value.ValueKind switch + { + JsonValueKind.String => value.GetString() ?? string.Empty, + JsonValueKind.Number or JsonValueKind.True or JsonValueKind.False => value.GetRawText(), + JsonValueKind.Null => "null", + JsonValueKind.Array => $"Array[{value.GetArrayLength()}]", + JsonValueKind.Object => JsonSerializer.Serialize(value), + _ => value.GetRawText() + }; + } + + private static bool IsReferenceTool(string id) + { + return id is "mime_lookup" or "http_status_lookup" or "port_lookup" or "dns_record_lookup" or "unicode_block_lookup" or "charset_lookup" or "timezone_abbr_lookup" or "regex_preset_lookup" or "magic_number_lookup" or "car_info" or "sanguosha_skin"; + } + + private static ToolResultKind GuessRemoteResultKind(string id) + { + return id is "baidu_hot" or "hotboard" or "bili_hot" or "zhihu_hot" or "movie_box_office" or "history_today" + ? ToolResultKind.RankedList + : id is "tech_news" or "football_news" or "cctv_news" or "ai_latest_news" + ? ToolResultKind.NewsCards + : id is "city_route_query" + ? ToolResultKind.KeyValueCards + : ToolResultKind.KeyValueCards; + } + + private static ToolResultDocument Document(string toolId, ToolResultKind kind, string output, IReadOnlyList<ToolResultBlock> blocks, string language) + { + return new ToolResultDocument(toolId, kind, output, blocks, Metadata(toolId, kind, language, DisplayProfile(kind)), output, string.Empty, "ok"); + } + + private static Dictionary<string, string> Metadata(string toolId, ToolResultKind kind, string language, string displayProfile) + { + return new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) + { + ["toolId"] = toolId, + ["resultKind"] = kind.ToString(), + ["language"] = language, + ["generatedAt"] = DateTimeOffset.Now.ToString("O"), + ["loadedAt"] = DateTimeOffset.Now.ToString("O"), + ["dataFreshness"] = "live", + ["displayProfile"] = displayProfile + }; + } + + private static Dictionary<string, string> RemoteMetadata(ApiEndpoint endpoint, ToolResultKind kind, string language) + { + var metadata = Metadata(endpoint.Id, kind, language, "remote-dashboard"); + metadata["sourceName"] = endpoint.SourceName; + metadata["sourceVisibility"] = endpoint.SourceVisibility.ToString(); + metadata["sourceSensitivity"] = endpoint.SourceVisibility == ApiSourceVisibility.SensitivePrivate ? "sensitive" : "public"; + metadata["dataFreshness"] = "remote-live"; + + return metadata; + } + + private static string DisplayProfile(ToolResultKind kind) + { + return kind switch + { + ToolResultKind.RankedList or ToolResultKind.NewsCards => "cards", + ToolResultKind.Table or ToolResultKind.CalculatorTable or ToolResultKind.ReferenceRows => "table", + ToolResultKind.ImagePreview or ToolResultKind.Media => "preview", + ToolResultKind.JsonTree or ToolResultKind.CodePreview => "document", + _ => "summary-details" + }; + } + + private static IReadOnlyDictionary<string, string> BlockMeta(string contentType, string displayMode, string priority) + { + return new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) + { + ["contentType"] = contentType, + ["displayMode"] = displayMode, + ["priority"] = priority + }; + } + + private static KeyValuePair<string, string> Pair(string key, string value) => new(key, value); + + private static IReadOnlyList<string> Lines(string output) + { + return output.Replace("\r\n", "\n").Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + } + + private static IReadOnlyList<string> RemoveSourceHeader(IReadOnlyList<string> lines) + { + return lines + .Where(line => !IsRemoteHeaderOrPrivacyLine(line)) + .ToArray(); + } + + private static bool IsRemoteHeaderOrPrivacyLine(string line) + { + var normalized = line.Trim(); + return IsSourceLine(normalized) || + normalized.StartsWith("\u6570\u636e\u6e90", StringComparison.OrdinalIgnoreCase) || + normalized.StartsWith("\u6765\u6e90", StringComparison.OrdinalIgnoreCase) || + normalized.StartsWith("\u83b7\u53d6\u65f6\u95f4", StringComparison.OrdinalIgnoreCase) || + normalized.Contains("Source note:", StringComparison.OrdinalIgnoreCase) || + normalized.Contains("remote address is hidden", StringComparison.OrdinalIgnoreCase) || + normalized.Contains("sanitized source name", StringComparison.OrdinalIgnoreCase) || + normalized.Contains("\u6765\u6e90\u8bf4\u660e", StringComparison.OrdinalIgnoreCase) || + normalized.Contains("\u5df2\u9690\u85cf\u8fdc\u7a0b\u5730\u5740", StringComparison.OrdinalIgnoreCase) || + normalized.Contains("\u8131\u654f\u6765\u6e90", StringComparison.OrdinalIgnoreCase) || + normalized.Contains("璇存槑", StringComparison.OrdinalIgnoreCase) && normalized.Contains("闅愯棌", StringComparison.OrdinalIgnoreCase) || + normalized.Contains("璇存槑", StringComparison.OrdinalIgnoreCase) && normalized.Contains("宸查殣", StringComparison.OrdinalIgnoreCase) || + normalized.Contains("鑴辨晱", StringComparison.OrdinalIgnoreCase) || + normalized.Contains("杩滅▼鍦板潃", StringComparison.OrdinalIgnoreCase) || + normalized.Contains("繙绋嬪湴鍧", StringComparison.OrdinalIgnoreCase); + } + + private static bool IsSourceLine(string line) + { + var normalized = line.Trim(); + return normalized.StartsWith("Data source:", StringComparison.OrdinalIgnoreCase) || + normalized.StartsWith("Fetched at:", StringComparison.OrdinalIgnoreCase) || + normalized.StartsWith("Source:", StringComparison.OrdinalIgnoreCase) || + normalized.StartsWith("来源", StringComparison.OrdinalIgnoreCase) || + normalized.StartsWith("数据源", StringComparison.OrdinalIgnoreCase) || + normalized.StartsWith("获取时间", StringComparison.OrdinalIgnoreCase) || + normalized.StartsWith("鏉ユ簮", StringComparison.OrdinalIgnoreCase) || + normalized.StartsWith("鏁版嵁", StringComparison.OrdinalIgnoreCase) || + normalized.StartsWith("鑾峰彇", StringComparison.OrdinalIgnoreCase); + } + + private static string SplitValue(string line) + { + var index = line.IndexOfAny([':', ':']); + return index >= 0 && index < line.Length - 1 ? line[(index + 1)..].Trim() : line; + } + + private static IEnumerable<string> ExtractLinks(IEnumerable<string> lines) + { + foreach (var line in lines) + { + foreach (Match match in Regex.Matches(line, @"https?://[^\s\]\)>'""]+", RegexOptions.IgnoreCase)) + { + yield return match.Value.TrimEnd('.', ',', ';'); + } + } + } + + private static string CleanVisibleText(string value) + { + var text = value ?? string.Empty; + text = Regex.Replace(text, @"\s*https?://[^\s\]\)>'""]+", string.Empty, RegexOptions.IgnoreCase); + text = Regex.Replace(text, @"\s*/\s*$", string.Empty); + text = Regex.Replace(text, @"\s{2,}", " "); + return text.Trim(); + } + + private static bool LooksLikeJson(string output) + { + var trimmed = output.TrimStart(); + return trimmed.StartsWith('{') || trimmed.StartsWith('['); + } + + private static bool LooksLikeSvg(string output) + { + var trimmed = output.TrimStart(); + return trimmed.StartsWith("<svg", StringComparison.OrdinalIgnoreCase) || + (trimmed.StartsWith("<?xml", StringComparison.OrdinalIgnoreCase) && + trimmed.Contains("<svg", StringComparison.OrdinalIgnoreCase)); + } + + private static string SourceTypeLabel(ApiEndpoint endpoint, string language) + { + return endpoint.SourceVisibility switch + { + ApiSourceVisibility.PublicOfficial => T(language, "官方/权威源", "Official / authoritative"), + ApiSourceVisibility.PublicTrusted => T(language, "公开可信源", "Public trusted"), + ApiSourceVisibility.SensitivePrivate => T(language, "私有/敏感源", "Private / sensitive"), + _ => endpoint.IsOfficial ? T(language, "官方/权威源", "Official / authoritative") : T(language, "公开可信源", "Public trusted") + }; + } + + private static IReadOnlyDictionary<string, string> SourceMetadata(ApiEndpoint endpoint) + { + var metadata = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) + { + ["contentType"] = "text/plain", + ["displayMode"] = "source", + ["priority"] = "low", + ["sourceVisibility"] = endpoint.SourceVisibility.ToString(), + ["sourceSensitivity"] = endpoint.SourceVisibility == ApiSourceVisibility.SensitivePrivate ? "sensitive" : "public", + ["sourceName"] = endpoint.SourceName + }; + + return metadata; + } + + private static string CodeLanguage(string id) + { + return id switch + { + var value when value.Contains("json", StringComparison.OrdinalIgnoreCase) => "json", + var value when value.Contains("xml", StringComparison.OrdinalIgnoreCase) => "xml", + var value when value.Contains("sql", StringComparison.OrdinalIgnoreCase) => "sql", + var value when value.Contains("html", StringComparison.OrdinalIgnoreCase) => "html", + _ => string.Empty + }; + } + + private static string DiffLeading(string line) => line.StartsWith('+') ? "+" : line.StartsWith('-') ? "-" : " "; + + private static string DiffStatus(string line) => line.StartsWith('+') ? "added" : line.StartsWith('-') ? "removed" : "context"; + + private static string StatusLeading(string line) => StatusOf(line) switch { "ok" => "OK", "error" => "!", _ => "i" }; + + private static string StatusOf(string line) + { + return line.Contains("OK", StringComparison.OrdinalIgnoreCase) || line.Contains("SUCCESS", StringComparison.OrdinalIgnoreCase) + ? "ok" + : line.Contains("FAIL", StringComparison.OrdinalIgnoreCase) || line.Contains("MISMATCH", StringComparison.OrdinalIgnoreCase) || line.Contains("ERROR", StringComparison.OrdinalIgnoreCase) + ? "error" + : "info"; + } + + private static string T(string language, string zh, string en) => string.Equals(language, "en-US", StringComparison.OrdinalIgnoreCase) ? en : zh; +} diff --git a/src/YMhut.Box.Core/Tools/ToolResultDocument.cs b/src/YMhut.Box.Core/Tools/ToolResultDocument.cs new file mode 100644 index 0000000..f1bcb0f --- /dev/null +++ b/src/YMhut.Box.Core/Tools/ToolResultDocument.cs @@ -0,0 +1,419 @@ +using System.Text.Json; +using System.Text.RegularExpressions; + +namespace YMhut.Box.Core.Tools; + +public enum ToolResultBlockKind +{ + Text, + KeyValue, + Table, + RankedList, + NewsList, + Image, + Link, + File, + Diff, + Status, + Code, + Json, + JsonTree, + Metric, + LineChart, + CardList, + Timeline, + Media, + Color, + Raw +} + +public sealed record ToolResultDocument( + string ToolId, + ToolResultKind ResultKind, + string RawText, + IReadOnlyList<ToolResultBlock> Blocks, + IReadOnlyDictionary<string, string> Metadata, + string RawPayload = "", + string SourceName = "", + string Status = "") +{ + public static ToolResultDocument FromOutput(IToolModule module, string output) + { + var spec = ToolPageSpecCatalog.For(module); + var lines = Lines(output); + var blocks = spec.Result switch + { + ToolResultKind.JsonTree => JsonBlocks(output), + ToolResultKind.CodePreview => [ToolResultBlock.Code("Code", output)], + ToolResultKind.Table or ToolResultKind.CalculatorTable => TableBlocks(lines), + ToolResultKind.RankedList => RankedBlocks(lines), + ToolResultKind.NewsCards => NewsBlocks(lines), + ToolResultKind.ImagePreview => MediaBlocks(lines, imageOnly: true), + ToolResultKind.LinkCards => LinkBlocks(lines), + ToolResultKind.FileCards => FileBlocks(lines), + ToolResultKind.Diff => DiffBlocks(lines), + ToolResultKind.StatusList => StatusBlocks(lines), + ToolResultKind.ColorSwatch => ColorBlocks(lines), + ToolResultKind.SystemCards or ToolResultKind.KeyValueCards or ToolResultKind.ReferenceRows => KeyValueBlocks(lines), + ToolResultKind.Media => MediaBlocks(lines, imageOnly: false), + _ => TextBlocks(lines) + }; + + if (blocks.Count == 0) + { + blocks = TextBlocks(lines); + } + + return new ToolResultDocument( + module.Id, + spec.Result, + output, + blocks, + new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) + { + ["toolId"] = module.Id, + ["resultKind"] = spec.Result.ToString(), + ["layout"] = spec.Layout.ToString(), + ["generatedAt"] = DateTimeOffset.Now.ToString("O") + }, + output, + string.Empty, + "ok"); + } + + private static IReadOnlyList<ToolResultBlock> JsonBlocks(string output) + { + try + { + using var document = JsonDocument.Parse(output); + return [ToolResultBlock.Json("JSON", JsonSerializer.Serialize(document.RootElement, new JsonSerializerOptions { WriteIndented = true }))]; + } + catch + { + return []; + } + } + + private static IReadOnlyList<ToolResultBlock> TableBlocks(IReadOnlyList<string> lines) + { + var rows = lines + .Select(line => line.Contains('|') ? line.Split('|').Select(cell => cell.Trim()).Where(cell => cell.Length > 0).ToArray() : SplitKeyValue(line)) + .Where(row => row.Length >= 2) + .Take(80) + .ToArray(); + return rows.Length == 0 ? [] : [ToolResultBlock.Table("Table", rows)]; + } + + private static IReadOnlyList<ToolResultBlock> RankedBlocks(IReadOnlyList<string> lines) + { + var items = lines + .Select(ParseRankedItem) + .Where(item => item is not null) + .Select(item => item!) + .Take(80) + .ToArray(); + return items.Length == 0 ? TextBlocks(lines) : [ToolResultBlock.List(ToolResultBlockKind.RankedList, "Ranked list", items)]; + } + + private static IReadOnlyList<ToolResultBlock> NewsBlocks(IReadOnlyList<string> lines) + { + var items = lines + .Where(line => !string.IsNullOrWhiteSpace(line)) + .Take(60) + .Select((line, index) => new ToolResultListItem((index + 1).ToString(), StripOrdinal(line), string.Empty, string.Empty, string.Empty)) + .ToArray(); + return items.Length == 0 ? [] : [ToolResultBlock.List(ToolResultBlockKind.NewsList, "News", items)]; + } + + private static IReadOnlyList<ToolResultBlock> LinkBlocks(IReadOnlyList<string> lines) + { + return ExtractLinks(lines) + .Distinct(StringComparer.OrdinalIgnoreCase) + .Take(30) + .Select(link => ToolResultBlock.Link("Link", link, link)) + .ToArray(); + } + + private static IReadOnlyList<ToolResultBlock> MediaBlocks(IReadOnlyList<string> lines, bool imageOnly) + { + var blocks = new List<ToolResultBlock>(); + foreach (var link in ExtractLinks(lines).Distinct(StringComparer.OrdinalIgnoreCase).Take(30)) + { + if (IsImageLink(link)) + { + blocks.Add(ToolResultBlock.Media(ToolResultBlockKind.Image, "Image", link, link)); + } + else if (!imageOnly) + { + blocks.Add(ToolResultBlock.Media(ToolResultBlockKind.Media, "Media", link, link)); + } + } + + blocks.AddRange(FileBlocks(lines)); + return blocks; + } + + private static IReadOnlyList<ToolResultBlock> FileBlocks(IReadOnlyList<string> lines) + { + return lines + .Select(ExtractPath) + .Where(path => !string.IsNullOrWhiteSpace(path)) + .Distinct(StringComparer.OrdinalIgnoreCase) + .Take(30) + .Select(path => ToolResultBlock.File("File", path!, path!)) + .ToArray(); + } + + private static IReadOnlyList<ToolResultBlock> DiffBlocks(IReadOnlyList<string> lines) + { + var items = lines + .Take(160) + .Select(line => new ToolResultListItem( + line.StartsWith('+') ? "+" : line.StartsWith('-') ? "-" : string.Empty, + line, + string.Empty, + line.StartsWith('+') ? "added" : line.StartsWith('-') ? "removed" : "context", + string.Empty)) + .ToArray(); + return items.Length == 0 ? [] : [ToolResultBlock.List(ToolResultBlockKind.Diff, "Diff", items)]; + } + + private static IReadOnlyList<ToolResultBlock> StatusBlocks(IReadOnlyList<string> lines) + { + var items = lines + .Take(80) + .Select(line => + { + var status = line.Contains("OK", StringComparison.OrdinalIgnoreCase) || line.Contains("SUCCESS", StringComparison.OrdinalIgnoreCase) + ? "ok" + : line.Contains("FAIL", StringComparison.OrdinalIgnoreCase) || line.Contains("MISMATCH", StringComparison.OrdinalIgnoreCase) + ? "error" + : "info"; + return new ToolResultListItem(status, line, string.Empty, status, string.Empty); + }) + .ToArray(); + return items.Length == 0 ? [] : [ToolResultBlock.List(ToolResultBlockKind.Status, "Status", items)]; + } + + private static IReadOnlyList<ToolResultBlock> ColorBlocks(IReadOnlyList<string> lines) + { + var colors = lines.SelectMany(line => Regex.Matches(line, @"#[0-9a-fA-F]{6}|#[0-9a-fA-F]{3}").Select(match => match.Value)) + .Distinct(StringComparer.OrdinalIgnoreCase) + .Take(20) + .Select(color => ToolResultBlock.Media(ToolResultBlockKind.Color, color, color, color)) + .ToArray(); + return colors.Length == 0 ? KeyValueBlocks(lines) : colors; + } + + private static IReadOnlyList<ToolResultBlock> KeyValueBlocks(IReadOnlyList<string> lines) + { + var rows = lines + .Select(SplitKeyValue) + .Where(row => row.Length >= 2) + .Select(row => new KeyValuePair<string, string>(row[0], row[1])) + .Take(80) + .ToArray(); + return rows.Length == 0 ? [] : [ToolResultBlock.KeyValue("Details", rows)]; + } + + private static IReadOnlyList<ToolResultBlock> TextBlocks(IReadOnlyList<string> lines) + { + var items = lines + .Take(120) + .Select((line, index) => new ToolResultListItem((index + 1).ToString(), line, string.Empty, string.Empty, string.Empty)) + .ToArray(); + return items.Length == 0 ? [] : [ToolResultBlock.List(ToolResultBlockKind.Text, "Lines", items)]; + } + + private static IReadOnlyList<string> Lines(string output) + { + return output.Replace("\r\n", "\n").Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + } + + private static string[] SplitKeyValue(string line) + { + var separators = new[] { ":", ":", "=", "\t" }; + foreach (var separator in separators) + { + var index = line.IndexOf(separator, StringComparison.Ordinal); + if (index > 0 && index < line.Length - separator.Length) + { + return [line[..index].Trim().Trim('-', '*', ' '), line[(index + separator.Length)..].Trim()]; + } + } + + return []; + } + + private static ToolResultListItem? ParseRankedItem(string line) + { + var match = Regex.Match(line, @"^\s*(?<rank>\d+)[\.\)\u3001\uff09\s]+(?<title>.+)$"); + return match.Success + ? new ToolResultListItem(match.Groups["rank"].Value, CleanVisibleText(match.Groups["title"].Value), string.Empty, string.Empty, ExtractLinks([line]).FirstOrDefault() ?? string.Empty) + : null; + } + + private static string CleanVisibleText(string value) + { + var text = value ?? string.Empty; + text = Regex.Replace(text, @"\s*https?://[^\s\]\)>'""]+", string.Empty, RegexOptions.IgnoreCase); + text = Regex.Replace(text, @"\s*/\s*$", string.Empty); + text = Regex.Replace(text, @"\s{2,}", " "); + return text.Trim(); + } + + private static string StripOrdinal(string value) + { + return Regex.Replace(value, @"^\s*\d+[\.\)\u3001\uff09\s]+", string.Empty).Trim(); + } + + private static IEnumerable<string> ExtractLinks(IEnumerable<string> lines) + { + foreach (var line in lines) + { + foreach (Match match in Regex.Matches(line, @"https?://[^\s\]\)>'""]+", RegexOptions.IgnoreCase)) + { + yield return match.Value.TrimEnd('.', ',', ';'); + } + } + } + + private static bool IsImageLink(string link) + { + if (!Uri.TryCreate(link, UriKind.Absolute, out var uri)) + { + return false; + } + + var path = uri.AbsolutePath.ToLowerInvariant(); + return path.EndsWith(".png", StringComparison.OrdinalIgnoreCase) + || path.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) + || path.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase) + || path.EndsWith(".webp", StringComparison.OrdinalIgnoreCase) + || path.EndsWith(".gif", StringComparison.OrdinalIgnoreCase) + || uri.Host.Contains("qlogo.cn", StringComparison.OrdinalIgnoreCase); + } + + private static string? ExtractPath(string line) + { + var candidate = line + .Replace("Path:", string.Empty, StringComparison.OrdinalIgnoreCase) + .Replace("Output:", string.Empty, StringComparison.OrdinalIgnoreCase) + .Replace("已输出:", string.Empty, StringComparison.OrdinalIgnoreCase) + .Replace("已创建归档:", string.Empty, StringComparison.OrdinalIgnoreCase) + .Replace("已解压到:", string.Empty, StringComparison.OrdinalIgnoreCase) + .Trim() + .Trim('"'); + + return File.Exists(candidate) || Directory.Exists(candidate) ? candidate : null; + } +} + +public sealed record ToolResultBlock( + ToolResultBlockKind Kind, + string Title, + string Text, + IReadOnlyList<KeyValuePair<string, string>> Pairs, + IReadOnlyList<string[]> Rows, + IReadOnlyList<ToolResultListItem> Items, + string Uri, + string Path, + string Language, + string Status, + string Subtitle = "", + string Description = "", + string Severity = "", + IReadOnlyDictionary<string, string>? Metadata = null) +{ + public static ToolResultBlock KeyValue( + string title, + IReadOnlyList<KeyValuePair<string, string>> pairs, + IReadOnlyDictionary<string, string>? metadata = null) + => new(ToolResultBlockKind.KeyValue, title, string.Empty, pairs, [], [], string.Empty, string.Empty, string.Empty, string.Empty, Metadata: metadata); + + public static ToolResultBlock Table( + string title, + IReadOnlyList<string[]> rows, + IReadOnlyDictionary<string, string>? metadata = null) + => new(ToolResultBlockKind.Table, title, string.Empty, [], rows, [], string.Empty, string.Empty, string.Empty, string.Empty, Metadata: metadata); + + public static ToolResultBlock List( + ToolResultBlockKind kind, + string title, + IReadOnlyList<ToolResultListItem> items, + IReadOnlyDictionary<string, string>? metadata = null) + => new(kind, title, string.Empty, [], [], items, string.Empty, string.Empty, string.Empty, string.Empty, Metadata: metadata); + + public static ToolResultBlock Link( + string title, + string text, + string uri, + IReadOnlyDictionary<string, string>? metadata = null) + => new(ToolResultBlockKind.Link, title, text, [], [], [], uri, string.Empty, string.Empty, string.Empty, Metadata: metadata); + + public static ToolResultBlock File( + string title, + string text, + string path, + IReadOnlyDictionary<string, string>? metadata = null) + => new(ToolResultBlockKind.File, title, text, [], [], [], string.Empty, path, string.Empty, string.Empty, Metadata: metadata); + + public static ToolResultBlock Code( + string title, + string text, + string language = "", + IReadOnlyDictionary<string, string>? metadata = null) + => new(ToolResultBlockKind.Code, title, text, [], [], [], string.Empty, string.Empty, language, string.Empty, Metadata: metadata); + + public static ToolResultBlock Json(string title, string text) + => new(ToolResultBlockKind.Json, title, text, [], [], [], string.Empty, string.Empty, "json", string.Empty); + + public static ToolResultBlock JsonTree(string title, string text) + => new(ToolResultBlockKind.JsonTree, title, text, [], [], [], string.Empty, string.Empty, "json", string.Empty); + + public static ToolResultBlock Metric( + string title, + IReadOnlyList<KeyValuePair<string, string>> pairs, + IReadOnlyDictionary<string, string>? metadata = null) + => new(ToolResultBlockKind.Metric, title, string.Empty, pairs, [], [], string.Empty, string.Empty, string.Empty, string.Empty, Metadata: metadata); + + public static ToolResultBlock LineChart( + string title, + IReadOnlyList<string[]> rows, + IReadOnlyDictionary<string, string>? metadata = null) + => new(ToolResultBlockKind.LineChart, title, string.Empty, [], rows, [], string.Empty, string.Empty, string.Empty, string.Empty, Metadata: metadata); + + public static ToolResultBlock Cards( + string title, + IReadOnlyList<ToolResultListItem> items, + IReadOnlyDictionary<string, string>? metadata = null) + => new(ToolResultBlockKind.CardList, title, string.Empty, [], [], items, string.Empty, string.Empty, string.Empty, string.Empty, Metadata: metadata); + + public static ToolResultBlock Timeline( + string title, + IReadOnlyList<ToolResultListItem> items, + IReadOnlyDictionary<string, string>? metadata = null) + => new(ToolResultBlockKind.Timeline, title, string.Empty, [], [], items, string.Empty, string.Empty, string.Empty, string.Empty, Metadata: metadata); + + public static ToolResultBlock Media( + ToolResultBlockKind kind, + string title, + string text, + string uri, + IReadOnlyDictionary<string, string>? metadata = null) + => new(kind, title, text, [], [], [], uri, string.Empty, string.Empty, string.Empty, Metadata: metadata); + + public static ToolResultBlock Raw( + string title, + string text, + string language = "", + IReadOnlyDictionary<string, string>? metadata = null) + => new(ToolResultBlockKind.Raw, title, text, [], [], [], string.Empty, string.Empty, language, string.Empty, Metadata: metadata); +} + +public sealed record ToolResultListItem( + string Leading, + string Title, + string Subtitle, + string Status, + string Uri); diff --git a/src/YMhut.Box.Core/Tools/ToolResultExperienceCatalog.cs b/src/YMhut.Box.Core/Tools/ToolResultExperienceCatalog.cs new file mode 100644 index 0000000..32eb60d --- /dev/null +++ b/src/YMhut.Box.Core/Tools/ToolResultExperienceCatalog.cs @@ -0,0 +1,359 @@ +namespace YMhut.Box.Core.Tools; + +public sealed record ToolResultExperience( + string ToolId, + string ExperienceId, + string Layout, + string Accent, + string Tone, + string PrimaryPrimitive, + IReadOnlyList<string> SecondaryPrimitives); + +public sealed record ToolPageExperience( + string ToolId, + string ExperienceId, + string InputLayout, + string ResultLayout, + string Accent, + string Tone, + IReadOnlyList<string> InputPrimitives, + IReadOnlyList<string> ResultPrimitives, + IReadOnlyList<string> Actions); + +public interface IToolResultExperienceCatalog +{ + ToolResultExperience GetRequired(string toolId); + + ToolPageExperience GetRequiredPage(string toolId); + + IReadOnlyDictionary<string, ToolResultExperience> Experiences { get; } + + IReadOnlyDictionary<string, ToolPageExperience> PageExperiences { get; } +} + +public sealed class ToolResultExperienceCatalog : IToolResultExperienceCatalog +{ + private readonly IReadOnlyDictionary<string, ToolResultExperience> _experiences; + private readonly IReadOnlyDictionary<string, ToolPageExperience> _pageExperiences; + + public ToolResultExperienceCatalog(ToolCatalog catalog) + { + _experiences = catalog.Modules.ToDictionary(module => module.Id, CreateExperience, StringComparer.OrdinalIgnoreCase); + _pageExperiences = catalog.Modules.ToDictionary(module => module.Id, CreatePageExperience, StringComparer.OrdinalIgnoreCase); + } + + public IReadOnlyDictionary<string, ToolResultExperience> Experiences => _experiences; + + public IReadOnlyDictionary<string, ToolPageExperience> PageExperiences => _pageExperiences; + + public ToolResultExperience GetRequired(string toolId) + { + if (_experiences.TryGetValue(toolId, out var experience)) + { + return experience; + } + + throw new InvalidOperationException($"Tool result experience is missing for '{toolId}'."); + } + + public ToolPageExperience GetRequiredPage(string toolId) + { + if (_pageExperiences.TryGetValue(toolId, out var experience)) + { + return experience; + } + + throw new InvalidOperationException($"Tool page experience is missing for '{toolId}'."); + } + + public static ToolResultExperience CreateExperience(IToolModule module) + { + var spec = ToolPageSpecCatalog.For(module); + var slug = module.Id.Replace('_', '-').ToLowerInvariant(); + var family = ResolveFamily(module, spec); + var primitive = ResolvePrimaryPrimitive(module, spec); + return new ToolResultExperience( + module.Id, + $"{slug}-{family}-{primitive}", + $"{family}-{spec.Result.ToString().ToLowerInvariant()}", + AccentFor(module.Metadata.Category), + ToneFor(module, spec), + primitive, + SecondaryPrimitives(spec)); + } + + public static ToolPageExperience CreatePageExperience(IToolModule module) + { + var spec = ToolPageSpecCatalog.For(module); + var result = CreateExperience(module); + var inputLayout = InputLayoutFor(spec); + var resultLayout = $"{ResolveFamily(module, spec)}-{result.PrimaryPrimitive}"; + var slug = module.Id.Replace('_', '-').ToLowerInvariant(); + return new ToolPageExperience( + module.Id, + $"{slug}-{inputLayout}-{result.PrimaryPrimitive}", + inputLayout, + resultLayout, + result.Accent, + result.Tone, + InputPrimitives(spec), + [result.PrimaryPrimitive, ..result.SecondaryPrimitives], + PageActions(spec)); + } + + private static string ResolveFamily(IToolModule module, ToolPageSpec spec) + { + if (module.Id.Contains("json", StringComparison.OrdinalIgnoreCase) || module.Id is "jwt_decoder") + { + return "structured"; + } + + if (spec.Layout is ToolLayoutKind.CalculatorForm or ToolLayoutKind.UnitConverter) + { + return "calculation"; + } + + if (spec.Layout is ToolLayoutKind.QueryDashboard or ToolLayoutKind.Dashboard) + { + return module.Metadata.OfflineCapable ? "reference" : "live"; + } + + if (spec.Layout is ToolLayoutKind.FileWorkflow) + { + return "fileflow"; + } + + if (spec.Layout is ToolLayoutKind.SecurityWorkbench) + { + return "security"; + } + + if (spec.Result is ToolResultKind.CodePreview or ToolResultKind.JsonTree) + { + return "code"; + } + + return module.Metadata.Category.ToString().ToLowerInvariant(); + } + + private static string ResolvePrimaryPrimitive(IToolModule module, ToolPageSpec spec) + { + return spec.Result switch + { + ToolResultKind.JsonTree => "json-tree", + ToolResultKind.CodePreview => "code-panel", + ToolResultKind.Table or ToolResultKind.CalculatorTable or ToolResultKind.ReferenceRows => "data-table", + ToolResultKind.RankedList => "ranked-lane", + ToolResultKind.NewsCards => "news-board", + ToolResultKind.ImagePreview => "media-stage", + ToolResultKind.LinkCards => "link-dock", + ToolResultKind.FileCards => "file-rail", + ToolResultKind.Diff => "diff-stack", + ToolResultKind.StatusList => "status-timeline", + ToolResultKind.ColorSwatch => "color-lab", + ToolResultKind.SystemCards => "metric-grid", + ToolResultKind.Media => "media-wall", + _ when module.Id.Contains("generator", StringComparison.OrdinalIgnoreCase) => "generator-strip", + _ => "text-stream" + }; + } + + private static IReadOnlyList<string> SecondaryPrimitives(ToolPageSpec spec) + { + var primitives = new List<string> { "summary-metrics", "raw-drawer" }; + if (spec.UseVirtualizedResults) + { + primitives.Add("virtual-list"); + } + if (spec.ResultPriority is ToolResultPriority.Table) + { + primitives.Add("column-scan"); + } + if (spec.ResultPriority is ToolResultPriority.Media or ToolResultPriority.Preview) + { + primitives.Add("preview-actions"); + } + return primitives; + } + + private static string InputLayoutFor(ToolPageSpec spec) + { + return spec.PrimaryInput switch + { + ToolPrimaryInputKind.None => spec.AutoRunOnOpen ? "auto-dashboard" : "action-panel", + ToolPrimaryInputKind.MultilineText => "editor-workbench", + ToolPrimaryInputKind.Query or ToolPrimaryInputKind.Url => "search-console", + ToolPrimaryInputKind.Token => "secret-entry", + ToolPrimaryInputKind.Color => "color-studio", + ToolPrimaryInputKind.FixedOptions => "preset-browser", + ToolPrimaryInputKind.FilePath => "file-dropzone", + ToolPrimaryInputKind.DateRange => "date-range-form", + ToolPrimaryInputKind.Number or ToolPrimaryInputKind.NumberPair or ToolPrimaryInputKind.NumberTriple or ToolPrimaryInputKind.NumberQuad => "numeric-calculator", + _ => "text-console" + }; + } + + private static IReadOnlyList<string> InputPrimitives(ToolPageSpec spec) + { + var primitives = new List<string>(); + if (spec.PrimaryInput is ToolPrimaryInputKind.Text or ToolPrimaryInputKind.MultilineText or ToolPrimaryInputKind.Query or ToolPrimaryInputKind.Url or ToolPrimaryInputKind.Token) + { + primitives.Add("text-input"); + } + if (spec.PrimaryInput == ToolPrimaryInputKind.MultilineText) + { + primitives.Add("monospace-editor"); + } + if (spec.PrimaryInput == ToolPrimaryInputKind.FilePath || spec.RequiresFilePicker) + { + primitives.Add("file-picker"); + } + if (spec.UsesNumberBox || spec.PrimaryInput is ToolPrimaryInputKind.Number or ToolPrimaryInputKind.NumberPair or ToolPrimaryInputKind.NumberTriple or ToolPrimaryInputKind.NumberQuad) + { + primitives.Add("number-grid"); + } + if (spec.UsesDatePicker || spec.PrimaryInput == ToolPrimaryInputKind.DateRange) + { + primitives.Add("date-fields"); + } + if (spec.Rules.Count > 0) + { + primitives.Add("rule-strip"); + } + if (spec.Parameters.Count > 0 || spec.UsesComboBox) + { + primitives.Add("preset-select"); + } + if (primitives.Count == 0) + { + primitives.Add("action-surface"); + } + + return primitives; + } + + private static IReadOnlyList<string> PageActions(ToolPageSpec spec) + { + var actions = new List<string>(); + if (!string.IsNullOrWhiteSpace(spec.PrimaryActionLabel)) + { + actions.Add("run"); + } + if (spec.RequiresFilePicker || spec.SecondaryActions.Contains("chooseFile")) + { + actions.Add("chooseFile"); + } + if (spec.SecondaryActions.Contains("clear")) + { + actions.Add("clear"); + } + actions.Add("copyResult"); + actions.Add("showRaw"); + return actions.Distinct(StringComparer.OrdinalIgnoreCase).ToArray(); + } + + private static string AccentFor(ToolCategory category) + { + return category switch + { + ToolCategory.Network => "#38BDF8", + ToolCategory.Security => "#F97316", + ToolCategory.Data => "#A78BFA", + ToolCategory.Calculator => "#22C55E", + ToolCategory.Text => "#FACC15", + ToolCategory.Image => "#EC4899", + ToolCategory.Design => "#14B8A6", + ToolCategory.Life => "#84CC16", + ToolCategory.System => "#60A5FA", + ToolCategory.Plugin => "#C084FC", + _ => "#22C55E" + }; + } + + private static string ToneFor(IToolModule module, ToolPageSpec spec) + { + if (!module.Metadata.OfflineCapable) + { + return "live-network"; + } + + return spec.PageExperience switch + { + ToolPageExperienceKind.FormCalculator => "precision", + ToolPageExperienceKind.FileWorkflow => "workflow", + ToolPageExperienceKind.LookupBrowser => "reference", + ToolPageExperienceKind.LivePreview => "studio", + ToolPageExperienceKind.SystemLauncher => "system", + _ => "local" + }; + } +} + +public sealed record ToolResultWebPayload( + string ToolId, + string ToolName, + ToolResultDocument ResultDocument, + string ExperienceId, + string Theme, + string Language, + ToolResultPrivacyPolicy PrivacyPolicy, + ToolResultRuntimeMetadata RuntimeMetadata, + ToolResultExperience Experience); + +public sealed record ToolPageWebPayload( + string ToolId, + string ToolName, + string ToolDescription, + ToolPageSpec Spec, + ToolPageExperience Experience, + ToolInputState Input, + string Theme, + string Language, + ToolResultPrivacyPolicy PrivacyPolicy, + ToolPageRuntimeMetadata RuntimeMetadata, + IReadOnlyList<string> AvailableActions, + ToolResultDocument? Result = null, + ToolResultRunState? RunState = null); + +public sealed record ToolInputState( + string Text, + IReadOnlyList<ToolInputField> Fields, + IReadOnlyDictionary<string, string> Rules, + IReadOnlyDictionary<string, string> Metadata); + +public sealed record ToolInputField( + string Key, + string Label, + string Kind, + string Value, + string Placeholder = "", + double? Minimum = null, + double? Maximum = null, + IReadOnlyList<ToolInputOption>? Options = null); + +public sealed record ToolInputOption(string Label, string Value, string Description = ""); + +public sealed record ToolResultRunState( + string State, + string Message, + string? Error = null, + long DurationMs = 0); + +public sealed record ToolPageRuntimeMetadata( + DateTimeOffset CreatedAt, + bool AutoRun, + bool OfflineCapable, + string Category, + string Icon, + string Source = "tool-page"); + +public sealed record ToolResultPrivacyPolicy( + IReadOnlyList<string> RedactedHostHints, + string Mode = "ymhut-api-only"); + +public sealed record ToolResultRuntimeMetadata( + long DurationMs, + DateTimeOffset CompletedAt, + bool Cached, + string Source, + IReadOnlyDictionary<string, string> ToolMetadata); diff --git a/src/YMhut.Box.Core/Tools/ToolResultPrivacySanitizer.cs b/src/YMhut.Box.Core/Tools/ToolResultPrivacySanitizer.cs new file mode 100644 index 0000000..4e0f4fd --- /dev/null +++ b/src/YMhut.Box.Core/Tools/ToolResultPrivacySanitizer.cs @@ -0,0 +1,50 @@ +using System.Text.RegularExpressions; + +namespace YMhut.Box.Core.Tools; + +public static class ToolResultPrivacySanitizer +{ + private static readonly Regex UrlPattern = new( + @"https?://[^\s\]\)""'<>]+", + RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled); + + private static readonly Regex RequestUrlContextPattern = new( + @"(?:api[_\-\s]*url|request[_\-\s]*url|api\s+request|endpoint|接口地址|请求地址)\s*[:=:]?\s*$", + RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled); + + public static IReadOnlyList<string> DefaultRedactedHostHints { get; } = + ["ymhut.cn", "ymhut.com", "api.ymhut", "update.ymhut"]; + + public static string Redact(string? value, string language, IReadOnlyList<string>? hostHints = null) + { + if (string.IsNullOrEmpty(value)) + { + return string.Empty; + } + + var hints = hostHints is { Count: > 0 } ? hostHints : DefaultRedactedHostHints; + var placeholder = IsEnglish(language) ? "[YMhut endpoint hidden]" : "[已隐藏 YMhut 接口]"; + return UrlPattern.Replace(value, match => + ShouldRedact(match.Value, value, match.Index, hints) ? placeholder : match.Value); + } + + private static bool ShouldRedact(string candidate, string fullText, int index, IReadOnlyList<string> hostHints) + { + if (Uri.TryCreate(candidate, UriKind.Absolute, out var uri)) + { + var host = uri.Host; + if (hostHints.Any(hint => host.Contains(hint, StringComparison.OrdinalIgnoreCase))) + { + return true; + } + } + + var contextStart = Math.Max(0, index - 64); + var context = fullText[contextStart..index]; + return RequestUrlContextPattern.IsMatch(context); + } + + private static bool IsEnglish(string? language) + => string.Equals(language, "en-US", StringComparison.OrdinalIgnoreCase) || + string.Equals(language, "en", StringComparison.OrdinalIgnoreCase); +} diff --git a/src/YMhut.Box.Core/Tools/ToolWorkerProtocol.cs b/src/YMhut.Box.Core/Tools/ToolWorkerProtocol.cs new file mode 100644 index 0000000..5792ca0 --- /dev/null +++ b/src/YMhut.Box.Core/Tools/ToolWorkerProtocol.cs @@ -0,0 +1,45 @@ +using System.Text.Json; + +namespace YMhut.Box.Core.Tools; + +public static class ToolWorkerProtocol +{ + public const string Version = "1"; + public const string Ready = "ready"; + public const string Ping = "ping"; + public const string Pong = "pong"; + public const string ExecuteTool = "executeTool"; + public const string Cancel = "cancel"; + public const string Progress = "progress"; + public const string Result = "result"; + public const string Error = "error"; + public const string Shutdown = "shutdown"; + + private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web) + { + WriteIndented = false + }; + + public static string Serialize(ToolWorkerMessage message) + { + return JsonSerializer.Serialize(message, JsonOptions); + } + + public static ToolWorkerMessage? Deserialize(string line) + { + return JsonSerializer.Deserialize<ToolWorkerMessage>(line, JsonOptions); + } +} + +public sealed record ToolWorkerMessage( + string Type, + string RequestId = "", + string? ToolId = null, + string? Input = null, + bool Ok = false, + string? Output = null, + string? Error = null, + ToolResultDocument? Document = null, + string? Version = null, + int TimeoutMs = 0, + string? Language = null); diff --git a/src/YMhut.Box.Core/Tools/ToolWorkerService.cs b/src/YMhut.Box.Core/Tools/ToolWorkerService.cs new file mode 100644 index 0000000..0909d56 --- /dev/null +++ b/src/YMhut.Box.Core/Tools/ToolWorkerService.cs @@ -0,0 +1,38 @@ +using YMhut.Box.Core.Api; +using YMhut.Box.Core.Data; + +namespace YMhut.Box.Core.Tools; + +public interface IToolWorkerService +{ + Task<T> RunAsync<T>(Func<CancellationToken, Task<T>> work, CancellationToken cancellationToken = default); + + Task<ToolExecutionResult> ExecuteToolAsync( + IToolModule module, + string input, + IApiManager? apiManager = null, + IReferenceDataService? referenceDataService = null, + CancellationToken cancellationToken = default, + string language = "zh-CN"); +} + +public sealed class ToolWorkerService : IToolWorkerService +{ + public Task<T> RunAsync<T>(Func<CancellationToken, Task<T>> work, CancellationToken cancellationToken = default) + { + return Task.Run(() => work(cancellationToken), cancellationToken); + } + + public Task<ToolExecutionResult> ExecuteToolAsync( + IToolModule module, + string input, + IApiManager? apiManager = null, + IReferenceDataService? referenceDataService = null, + CancellationToken cancellationToken = default, + string language = "zh-CN") + { + return RunAsync( + token => ToolExecutor.ExecuteAsync(module, input, token, apiManager, referenceDataService, language), + cancellationToken); + } +} diff --git a/src/YMhut.Box.Core/Tools/ToolboxLayoutCalculator.cs b/src/YMhut.Box.Core/Tools/ToolboxLayoutCalculator.cs new file mode 100644 index 0000000..7813e09 --- /dev/null +++ b/src/YMhut.Box.Core/Tools/ToolboxLayoutCalculator.cs @@ -0,0 +1,66 @@ +namespace YMhut.Box.Core.Tools; + +public readonly record struct ToolboxGridLayout( + int Columns, + double CardWidth, + double ItemWidth, + bool ShowCategoryRail, + bool IsCompact); + +public static class ToolboxLayoutCalculator +{ + public const double MinCardWidth = 264; + public const double MaxCardWidth = 360; + public const double ItemGap = 14; + + public static ToolboxGridLayout Calculate(double availableWidth) + { + if (double.IsNaN(availableWidth) || double.IsInfinity(availableWidth) || availableWidth <= 0) + { + return new ToolboxGridLayout(3, 318, 332, ShowCategoryRail: true, IsCompact: false); + } + + var showCategoryRail = availableWidth >= 900; + var usable = Math.Max(MinCardWidth, availableWidth - 28); + var columns = ColumnsFor(usable); + var cardWidth = Math.Floor((usable - (columns - 1) * ItemGap) / columns); + cardWidth = Math.Clamp(cardWidth, MinCardWidth, MaxCardWidth); + + return new ToolboxGridLayout( + columns, + cardWidth, + cardWidth + ItemGap, + showCategoryRail, + IsCompact: availableWidth < 720); + } + + private static int ColumnsFor(double usableWidth) + { + if (usableWidth < 620) + { + return 1; + } + + if (usableWidth < 900) + { + return 2; + } + + if (usableWidth < 1220) + { + return 3; + } + + if (usableWidth < 1540) + { + return 4; + } + + if (usableWidth < 1880) + { + return 5; + } + + return 6; + } +} diff --git a/src/YMhut.Box.Core/Updates/UpdateInfoEndpointPolicy.cs b/src/YMhut.Box.Core/Updates/UpdateInfoEndpointPolicy.cs new file mode 100644 index 0000000..17a90b8 --- /dev/null +++ b/src/YMhut.Box.Core/Updates/UpdateInfoEndpointPolicy.cs @@ -0,0 +1,20 @@ +namespace YMhut.Box.Core.Updates; + +public static class UpdateInfoEndpointPolicy +{ + public static readonly IReadOnlyList<string> DefaultRelativePaths = + [ + "api/client/bootstrap", + "update-info.json", + "update-info", + "api/update-info" + ]; + + public static IReadOnlyList<Uri> BuildDefaultUris(string baseUri = "https://update.ymhut.cn/") + { + var root = new Uri(baseUri.TrimEnd('/') + "/"); + return DefaultRelativePaths + .Select(path => new Uri(root, path)) + .ToArray(); + } +} diff --git a/src/YMhut.Box.Core/Updates/UpdateModels.cs b/src/YMhut.Box.Core/Updates/UpdateModels.cs new file mode 100644 index 0000000..d4a04d7 --- /dev/null +++ b/src/YMhut.Box.Core/Updates/UpdateModels.cs @@ -0,0 +1,48 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace YMhut.Box.Core.Updates; + +public sealed record ReleaseManifest( + string Version, + string PackageVersion, + string Channel, + DateTimeOffset CreatedAt, + IReadOnlyList<ReleaseFileEntry> Files, + IReadOnlyList<string> DeletedFiles, + string BaseDownloadUrl = "", + string Flavor = ""); + +public sealed record ReleaseFileEntry( + string Path, + long Size, + string Sha256, + bool Required = false); + +public interface IAppUpdateService +{ + Task<ReleaseManifest?> GetLatestManifestAsync(CancellationToken cancellationToken = default); +} + +public static class ReleaseManifestSerializer +{ + public static readonly JsonSerializerOptions Options = new(JsonSerializerDefaults.Web) + { + WriteIndented = true, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull + }; + + public static async Task<ReleaseManifest> ReadManifestAsync(string path, CancellationToken cancellationToken = default) + { + await using var stream = File.OpenRead(path); + return await JsonSerializer.DeserializeAsync<ReleaseManifest>(stream, Options, cancellationToken).ConfigureAwait(false) + ?? throw new InvalidDataException("Release manifest is empty or invalid."); + } + + public static async Task WriteManifestAsync(string path, ReleaseManifest manifest, CancellationToken cancellationToken = default) + { + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + await using var stream = File.Create(path); + await JsonSerializer.SerializeAsync(stream, manifest, Options, cancellationToken).ConfigureAwait(false); + } +} diff --git a/src/YMhut.Box.Core/Updates/UpdateVersionComparer.cs b/src/YMhut.Box.Core/Updates/UpdateVersionComparer.cs new file mode 100644 index 0000000..c0896bc --- /dev/null +++ b/src/YMhut.Box.Core/Updates/UpdateVersionComparer.cs @@ -0,0 +1,82 @@ +namespace YMhut.Box.Core.Updates; + +public static class UpdateVersionComparer +{ + public static string NormalizeVersion(string? version, string? build = null) + { + var baseParts = VersionParts(version).ToArray(); + if (baseParts.Length == 0) + { + return string.Empty; + } + + if (!string.IsNullOrWhiteSpace(build) && + baseParts.Length < 4 && + TryReadFirstNumber(build, out var buildNumber)) + { + baseParts = [.. baseParts, buildNumber]; + } + + return string.Join('.', baseParts); + } + + public static int Compare(string? remoteVersion, string? remoteBuild, string? currentVersion) + { + return CompareNormalized(NormalizeVersion(remoteVersion, remoteBuild), NormalizeVersion(currentVersion)); + } + + public static int CompareNormalized(string? remoteVersion, string? currentVersion) + { + var remoteParts = VersionParts(remoteVersion).ToArray(); + var currentParts = VersionParts(currentVersion).ToArray(); + var length = Math.Max(remoteParts.Length, currentParts.Length); + for (var index = 0; index < length; index++) + { + var left = index < remoteParts.Length ? remoteParts[index] : 0; + var right = index < currentParts.Length ? currentParts[index] : 0; + if (left > right) + { + return 1; + } + + if (left < right) + { + return -1; + } + } + + return 0; + } + + public static bool IsRemoteNewer(string? remoteVersion, string? remoteBuild, string? currentVersion) + { + return Compare(remoteVersion, remoteBuild, currentVersion) > 0; + } + + private static IEnumerable<int> VersionParts(string? value) + { + return (value ?? string.Empty) + .Split(['.', '+', '-', ' ', '_'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .Select(ReadLeadingNumber) + .Where(part => part is not null) + .Select(part => part!.Value); + } + + private static int? ReadLeadingNumber(string part) + { + var digits = new string(part.TakeWhile(char.IsDigit).ToArray()); + return int.TryParse(digits, out var parsed) ? parsed : null; + } + + private static bool TryReadFirstNumber(string value, out int number) + { + number = 0; + foreach (var part in VersionParts(value)) + { + number = part; + return true; + } + + return false; + } +} diff --git a/src/YMhut.Box.Core/YMhut.Box.Core.csproj b/src/YMhut.Box.Core/YMhut.Box.Core.csproj new file mode 100644 index 0000000..09782d5 --- /dev/null +++ b/src/YMhut.Box.Core/YMhut.Box.Core.csproj @@ -0,0 +1,21 @@ +<Project Sdk="Microsoft.NET.Sdk"> + <PropertyGroup> + <TargetFramework>net10.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)' == 'Release'"> + <DebugType>none</DebugType> + <DebugSymbols>false</DebugSymbols> + </PropertyGroup> + <ItemGroup> + <PackageReference Include="Microsoft.Data.Sqlite" Version="10.0.0" /> + <PackageReference Include="QRCoder" Version="1.8.0" /> + <PackageReference Include="System.Drawing.Common" Version="10.0.0" /> + <PackageReference Include="System.Management" Version="10.0.0" /> + <PackageReference Include="ZXing.Net" Version="0.16.11" /> + </ItemGroup> + <ItemGroup> + <EmbeddedResource Include="Plugins\BuiltIn\**\*.*" /> + </ItemGroup> +</Project> diff --git a/src/YMhut.Box.DownloadHost/Program.cs b/src/YMhut.Box.DownloadHost/Program.cs new file mode 100644 index 0000000..583c8e8 --- /dev/null +++ b/src/YMhut.Box.DownloadHost/Program.cs @@ -0,0 +1,409 @@ +using System.Collections.Concurrent; +using System.Diagnostics; +using System.IO.Pipes; +using System.Net; +using System.Net.Http.Headers; +using System.Text; +using YMhut.Box.Core.Downloads; + +Console.OutputEncoding = Encoding.UTF8; + +var pipeName = ReadPipeName(args); +if (string.IsNullOrWhiteSpace(pipeName)) +{ + Console.Error.WriteLine("Missing --pipe argument."); + return 2; +} + +var running = new ConcurrentDictionary<string, CancellationTokenSource>(StringComparer.Ordinal); +var paused = new ConcurrentDictionary<string, bool>(StringComparer.Ordinal); +var canceled = new ConcurrentDictionary<string, bool>(StringComparer.Ordinal); +var writeGate = new SemaphoreSlim(1, 1); + +await using var pipe = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous); +await pipe.ConnectAsync(8000).ConfigureAwait(false); +using var reader = new StreamReader(pipe, Encoding.UTF8, detectEncodingFromByteOrderMarks: false, bufferSize: 4096, leaveOpen: true); +await using var writer = new StreamWriter(pipe, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false), bufferSize: 4096, leaveOpen: true) +{ + AutoFlush = true +}; + +await WriteAsync(new DownloadHostMessage(DownloadHostProtocol.Ready, Version: DownloadHostProtocol.Version), CancellationToken.None) + .ConfigureAwait(false); + +while (await reader.ReadLineAsync().ConfigureAwait(false) is { } line) +{ + var message = DownloadHostProtocol.Deserialize(line); + if (message is null) + { + continue; + } + + if (string.Equals(message.Type, DownloadHostProtocol.Shutdown, StringComparison.Ordinal)) + { + foreach (var request in running.Values) + { + request.Cancel(); + } + + break; + } + + if (string.Equals(message.Type, DownloadHostProtocol.Ping, StringComparison.Ordinal)) + { + await WriteAsync(new DownloadHostMessage(DownloadHostProtocol.Pong, message.RequestId), CancellationToken.None) + .ConfigureAwait(false); + continue; + } + + if (string.Equals(message.Type, DownloadHostProtocol.Start, StringComparison.Ordinal) && message.Item is not null) + { + StartDownload(message.Item, resume: FileLength(message.Item.EffectivePartialPath) > 0); + continue; + } + + if (string.Equals(message.Type, DownloadHostProtocol.Resume, StringComparison.Ordinal) && message.Item is not null) + { + StartDownload(message.Item, resume: true); + continue; + } + + if (string.Equals(message.Type, DownloadHostProtocol.Pause, StringComparison.Ordinal)) + { + if (running.TryRemove(message.RequestId, out var source)) + { + paused[message.RequestId] = true; + source.Cancel(); + source.Dispose(); + } + + continue; + } + + if (string.Equals(message.Type, DownloadHostProtocol.Cancel, StringComparison.Ordinal)) + { + if (running.TryRemove(message.RequestId, out var source)) + { + source.Cancel(); + source.Dispose(); + } + + paused.TryRemove(message.RequestId, out _); + canceled[message.RequestId] = true; + } +} + +foreach (var request in running.Values) +{ + request.Cancel(); + request.Dispose(); +} + +return 0; + +void StartDownload(DownloadItem item, bool resume) +{ + if (running.ContainsKey(item.Id)) + { + return; + } + + paused.TryRemove(item.Id, out _); + var cancelSource = new CancellationTokenSource(); + running[item.Id] = cancelSource; + + _ = Task.Run(async () => + { + try + { + await DownloadAsync(item, resume, cancelSource.Token).ConfigureAwait(false); + } + catch (OperationCanceledException) + { + if (paused.ContainsKey(item.Id)) + { + var partialLength = FileLength(item.EffectivePartialPath); + await WriteAsync( + new DownloadHostMessage( + DownloadHostProtocol.Paused, + item.Id, + Progress: new DownloadProgressSnapshot(item.Id, DownloadState.Paused, partialLength, item.TotalBytes, 0)), + CancellationToken.None).ConfigureAwait(false); + } + else + { + var wasCanceled = canceled.ContainsKey(item.Id); + if (wasCanceled) + { + TryDeleteFile(item.EffectivePartialPath); + } + + var partialLength = wasCanceled ? 0 : FileLength(item.EffectivePartialPath); + await WriteAsync( + new DownloadHostMessage( + DownloadHostProtocol.Canceled, + item.Id, + Progress: new DownloadProgressSnapshot(item.Id, DownloadState.Canceled, partialLength, item.TotalBytes, 0)), + CancellationToken.None).ConfigureAwait(false); + } + } + catch (Exception exception) + { + await WriteAsync( + new DownloadHostMessage( + DownloadHostProtocol.Failed, + item.Id, + Progress: new DownloadProgressSnapshot(item.Id, DownloadState.Failed, FileLength(item.EffectivePartialPath), item.TotalBytes, 0, exception.Message), + Error: exception.Message), + CancellationToken.None).ConfigureAwait(false); + } + finally + { + paused.TryRemove(item.Id, out _); + canceled.TryRemove(item.Id, out _); + if (running.TryRemove(item.Id, out var source)) + { + source.Dispose(); + } + } + }); +} + +async Task DownloadAsync(DownloadItem item, bool resume, CancellationToken cancellationToken) +{ + Directory.CreateDirectory(Path.GetDirectoryName(item.TargetPath) ?? AppContext.BaseDirectory); + Directory.CreateDirectory(Path.GetDirectoryName(item.EffectivePartialPath) ?? AppContext.BaseDirectory); + + using var client = new HttpClient { Timeout = TimeSpan.FromHours(6) }; + client.DefaultRequestHeaders.UserAgent.ParseAdd("YMhutBox/2.0 DownloadHost"); + + var allowResume = resume; + for (var attempt = 0; attempt < 2; attempt++) + { + var existingBytes = allowResume ? FileLength(item.EffectivePartialPath) : 0; + using var request = new HttpRequestMessage(HttpMethod.Get, item.Source.EffectiveUrl); + if (existingBytes > 0) + { + request.Headers.Range = new RangeHeaderValue(existingBytes, null); + } + + using var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); + var metadata = ResponseMetadata.From(response); + if (ShouldRestartFromZero(item, metadata, response.StatusCode, existingBytes)) + { + TryDeleteFile(item.EffectivePartialPath); + allowResume = false; + if (attempt == 0) + { + continue; + } + + existingBytes = 0; + } + + if (existingBytes > 0 && response.StatusCode == HttpStatusCode.OK) + { + TryDeleteFile(item.EffectivePartialPath); + existingBytes = 0; + } + + response.EnsureSuccessStatusCode(); + var remoteLength = response.Content.Headers.ContentLength; + var totalBytes = response.Content.Headers.ContentRange?.Length ?? + metadata.ContentLength ?? + (remoteLength is > 0 ? remoteLength + existingBytes : item.TotalBytes); + + await using var source = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false); + await using var target = new FileStream( + item.EffectivePartialPath, + existingBytes > 0 ? FileMode.Append : FileMode.Create, + FileAccess.Write, + FileShare.Read, + 128 * 1024, + useAsync: true); + + var buffer = new byte[128 * 1024]; + var received = existingBytes; + var started = Stopwatch.StartNew(); + var lastReport = Stopwatch.StartNew(); + var restartedFromZero = attempt > 0 || (resume && existingBytes == 0); + + await ReportAsync(item.Id, DownloadState.Running, received, totalBytes, 0, metadata, restartedFromZero, cancellationToken).ConfigureAwait(false); + while (true) + { + var read = await source.ReadAsync(buffer.AsMemory(0, buffer.Length), cancellationToken).ConfigureAwait(false); + if (read <= 0) + { + break; + } + + await target.WriteAsync(buffer.AsMemory(0, read), cancellationToken).ConfigureAwait(false); + received += read; + if (lastReport.ElapsedMilliseconds >= 250) + { + var speed = (received - existingBytes) / Math.Max(0.001, started.Elapsed.TotalSeconds); + await ReportAsync(item.Id, DownloadState.Running, received, totalBytes, speed, metadata, restartedFromZero, cancellationToken).ConfigureAwait(false); + lastReport.Restart(); + } + } + + await target.FlushAsync(cancellationToken).ConfigureAwait(false); + target.Close(); + + MovePartialToFinal(item.EffectivePartialPath, item.TargetPath); + await WriteAsync( + new DownloadHostMessage( + DownloadHostProtocol.Completed, + item.Id, + Progress: new DownloadProgressSnapshot( + item.Id, + DownloadState.Completed, + received, + totalBytes, + 0, + ETag: metadata.ETag, + LastModified: metadata.LastModified, + AcceptRanges: metadata.AcceptRanges, + ContentLength: metadata.ContentLength, + FinalUrl: metadata.FinalUrl, + RestartedFromZero: restartedFromZero)), + cancellationToken).ConfigureAwait(false); + return; + } + + throw new IOException("Download restart failed."); +} + +async Task ReportAsync(string id, DownloadState state, long received, long? total, double speed, ResponseMetadata metadata, bool restartedFromZero, CancellationToken cancellationToken) +{ + await WriteAsync( + new DownloadHostMessage( + DownloadHostProtocol.Progress, + id, + Progress: new DownloadProgressSnapshot( + id, + state, + received, + total, + speed, + ETag: metadata.ETag, + LastModified: metadata.LastModified, + AcceptRanges: metadata.AcceptRanges, + ContentLength: metadata.ContentLength, + FinalUrl: metadata.FinalUrl, + RestartedFromZero: restartedFromZero)), + cancellationToken).ConfigureAwait(false); +} + +static bool ShouldRestartFromZero(DownloadItem item, ResponseMetadata metadata, HttpStatusCode statusCode, long existingBytes) +{ + if (existingBytes <= 0) + { + return false; + } + + if (statusCode != HttpStatusCode.PartialContent) + { + return true; + } + + if (!string.IsNullOrWhiteSpace(item.ETag) && + !string.IsNullOrWhiteSpace(metadata.ETag) && + !string.Equals(item.ETag, metadata.ETag, StringComparison.Ordinal)) + { + return true; + } + + return !string.IsNullOrWhiteSpace(item.LastModified) && + !string.IsNullOrWhiteSpace(metadata.LastModified) && + !string.Equals(item.LastModified, metadata.LastModified, StringComparison.OrdinalIgnoreCase); +} + +static void MovePartialToFinal(string partialPath, string targetPath) +{ + Directory.CreateDirectory(Path.GetDirectoryName(targetPath) ?? AppContext.BaseDirectory); + if (File.Exists(targetPath)) + { + File.Delete(targetPath); + } + + File.Move(partialPath, targetPath); +} + +static void TryDeleteFile(string path) +{ + try + { + if (File.Exists(path)) + { + File.Delete(path); + } + } + catch + { + } +} + +async Task WriteAsync(DownloadHostMessage message, CancellationToken cancellationToken) +{ + await writeGate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await writer.WriteLineAsync(DownloadHostProtocol.Serialize(message).AsMemory(), cancellationToken).ConfigureAwait(false); + await writer.FlushAsync(cancellationToken).ConfigureAwait(false); + } + finally + { + writeGate.Release(); + } +} + +static long FileLength(string path) +{ + try + { + return File.Exists(path) ? new FileInfo(path).Length : 0; + } + catch + { + return 0; + } +} + +static string? ReadPipeName(string[] args) +{ + for (var index = 0; index < args.Length; index++) + { + if (string.Equals(args[index], "--pipe", StringComparison.OrdinalIgnoreCase) && + index + 1 < args.Length) + { + return args[index + 1]; + } + } + + return null; +} + +sealed record ResponseMetadata( + string ETag, + string LastModified, + string AcceptRanges, + long? ContentLength, + string FinalUrl) +{ + public bool ResumeSupported => + AcceptRanges.Contains("bytes", StringComparison.OrdinalIgnoreCase) || + !string.IsNullOrWhiteSpace(ETag) || + !string.IsNullOrWhiteSpace(LastModified); + + public static ResponseMetadata From(HttpResponseMessage response) + { + var requestUri = response.RequestMessage?.RequestUri?.ToString() ?? string.Empty; + return new ResponseMetadata( + response.Headers.ETag?.ToString() ?? string.Empty, + response.Content.Headers.LastModified?.ToString() ?? response.Headers.Date?.ToString() ?? string.Empty, + string.Join(",", response.Headers.AcceptRanges.Select(value => value.ToString())), + response.Content.Headers.ContentRange?.Length ?? response.Content.Headers.ContentLength, + requestUri); + } +} diff --git a/src/YMhut.Box.DownloadHost/YMhut.Box.DownloadHost.csproj b/src/YMhut.Box.DownloadHost/YMhut.Box.DownloadHost.csproj new file mode 100644 index 0000000..1b26d00 --- /dev/null +++ b/src/YMhut.Box.DownloadHost/YMhut.Box.DownloadHost.csproj @@ -0,0 +1,17 @@ +<Project Sdk="Microsoft.NET.Sdk"> + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net10.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + <AssemblyName>YMhut.Box.DownloadHost</AssemblyName> + <RootNamespace>YMhut.Box.DownloadHost</RootNamespace> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)' == 'Release'"> + <DebugType>none</DebugType> + <DebugSymbols>false</DebugSymbols> + </PropertyGroup> + <ItemGroup> + <ProjectReference Include="..\YMhut.Box.Core\YMhut.Box.Core.csproj" /> + </ItemGroup> +</Project> diff --git a/src/YMhut.Box.PluginHost/Program.cs b/src/YMhut.Box.PluginHost/Program.cs new file mode 100644 index 0000000..9face23 --- /dev/null +++ b/src/YMhut.Box.PluginHost/Program.cs @@ -0,0 +1,749 @@ +using System.Collections.Concurrent; +using System.Globalization; +using System.IO.Pipes; +using System.Net; +using System.Net.NetworkInformation; +using System.Net.Sockets; +using System.Runtime.InteropServices; +using System.Text; +using System.Text.Json; +using YMhut.Box.Core.Api; +using YMhut.Box.Core.App; +using YMhut.Box.Core.Data; +using YMhut.Box.Core.Logging; +using YMhut.Box.Core.Net; +using YMhut.Box.Core.Plugins; +using YMhut.Box.Core.Settings; +using YMhut.Box.Core.Tools; + +Console.OutputEncoding = Encoding.UTF8; + +var pipeName = ReadPipeName(args); +if (string.IsNullOrWhiteSpace(pipeName)) +{ + Console.Error.WriteLine("Missing --pipe argument."); + return 2; +} + +var appPaths = AppPaths.ForCurrentUser(); +var logService = new SqliteLogService(appPaths); +var settingsService = new AppSettingsService(new AppSettingsStore(appPaths.Root)); +await settingsService.LoadAsync().ConfigureAwait(false); + +using var httpService = new HttpService(logService: logService, settingsService: settingsService); +var apiManager = new ApiManager(httpService, logService); +var referenceDataService = new ReferenceDataService(appPaths); +var stateStore = new PluginStateStore(appPaths); +var builtInPluginInstaller = new BuiltInPluginInstallerService(appPaths, logService, settingsService); +var registry = new PluginRegistryService(appPaths, stateStore, logService, settingsService, builtInPluginInstaller); +var writeGate = new SemaphoreSlim(1, 1); +var snapshotGate = new SemaphoreSlim(1, 1); +var runtimeValues = new ConcurrentDictionary<string, ConcurrentDictionary<string, string>>(StringComparer.OrdinalIgnoreCase); +FileSystemWatcher? watcher = null; +CancellationTokenSource? reloadDebounce = null; +PluginSnapshot? currentSnapshot = null; +var backgroundRefreshActive = 0; + +await using var pipe = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous); +await pipe.ConnectAsync(8000).ConfigureAwait(false); +using var reader = new StreamReader(pipe, Encoding.UTF8, detectEncodingFromByteOrderMarks: false, bufferSize: 4096, leaveOpen: true); +await using var writer = new StreamWriter(pipe, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false), bufferSize: 4096, leaveOpen: true) +{ + AutoFlush = true +}; + +await WriteAsync(new PluginHostMessage(PluginHostProtocol.Ready, Version: PluginHostProtocol.Version), CancellationToken.None) + .ConfigureAwait(false); +await logService.WriteAsync("Information", "plugin-host", "Plugin host ready").ConfigureAwait(false); +ConfigureWatcher(); + +while (await reader.ReadLineAsync().ConfigureAwait(false) is { } line) +{ + var message = PluginHostProtocol.Deserialize(line); + if (message is null) + { + continue; + } + + if (string.Equals(message.Type, PluginHostProtocol.Shutdown, StringComparison.Ordinal)) + { + break; + } + + _ = Task.Run(async () => await HandleAsync(message).ConfigureAwait(false)); +} + +watcher?.Dispose(); +reloadDebounce?.Cancel(); +reloadDebounce?.Dispose(); +return 0; + +async Task HandleAsync(PluginHostMessage message) +{ + try + { + switch (message.Type) + { + case PluginHostProtocol.Ping: + await WriteAsync(new PluginHostMessage(PluginHostProtocol.Pong, message.RequestId), CancellationToken.None).ConfigureAwait(false); + break; + case PluginHostProtocol.GetSnapshot: + var hadSnapshot = currentSnapshot is not null; + await EnsureSnapshotAvailableAsync(CancellationToken.None).ConfigureAwait(false); + await WriteAsync(new PluginHostMessage(PluginHostProtocol.GetSnapshot, message.RequestId, Snapshot: currentSnapshot), CancellationToken.None).ConfigureAwait(false); + if (hadSnapshot) + { + QueueSnapshotRefresh(); + } + + break; + case PluginHostProtocol.Reload: + await settingsService.LoadAsync().ConfigureAwait(false); + await ReloadSnapshotAsync(broadcast: true, CancellationToken.None).ConfigureAwait(false); + await WriteAsync(new PluginHostMessage(PluginHostProtocol.Reload, message.RequestId, Snapshot: currentSnapshot), CancellationToken.None).ConfigureAwait(false); + break; + case PluginHostProtocol.SetPluginEnabled: + await stateStore.SetEnabledAsync(Required(message.PluginId), message.Enabled == true).ConfigureAwait(false); + await ReloadSnapshotAsync(broadcast: true, CancellationToken.None).ConfigureAwait(false); + await WriteAsync(new PluginHostMessage(PluginHostProtocol.SetPluginEnabled, message.RequestId, Snapshot: currentSnapshot), CancellationToken.None).ConfigureAwait(false); + break; + case PluginHostProtocol.SetPermission: + await stateStore.SetPermissionAsync(Required(message.PluginId), message.Permission ?? throw new InvalidOperationException("Missing permission."), message.Granted == true).ConfigureAwait(false); + await ReloadSnapshotAsync(broadcast: true, CancellationToken.None).ConfigureAwait(false); + await WriteAsync(new PluginHostMessage(PluginHostProtocol.SetPermission, message.RequestId, Snapshot: currentSnapshot), CancellationToken.None).ConfigureAwait(false); + break; + case PluginHostProtocol.SetSurfaceMounted: + await stateStore.SetSurfaceMountedAsync(Required(message.PluginId), Required(message.SurfaceId), message.Mounted == true).ConfigureAwait(false); + await ReloadSnapshotAsync(broadcast: true, CancellationToken.None).ConfigureAwait(false); + await WriteAsync(new PluginHostMessage(PluginHostProtocol.SetSurfaceMounted, message.RequestId, Snapshot: currentSnapshot), CancellationToken.None).ConfigureAwait(false); + break; + case PluginHostProtocol.BridgeCall: + var response = await HandleBridgeAsync(message.BridgeRequest ?? throw new InvalidOperationException("Missing bridge request.")).ConfigureAwait(false); + await WriteAsync(new PluginHostMessage(PluginHostProtocol.BridgeCall, message.RequestId, BridgeResponse: response), CancellationToken.None).ConfigureAwait(false); + break; + default: + await WriteAsync(new PluginHostMessage(PluginHostProtocol.Error, message.RequestId, Error: $"Unknown message type: {message.Type}"), CancellationToken.None).ConfigureAwait(false); + break; + } + } + catch (Exception exception) + { + await logService.WriteAsync("Error", "plugin-host", "Plugin host request failed", exception.Message).ConfigureAwait(false); + await WriteAsync(new PluginHostMessage(PluginHostProtocol.Error, message.RequestId, Error: exception.Message), CancellationToken.None).ConfigureAwait(false); + } +} + +async Task EnsureSnapshotAvailableAsync(CancellationToken cancellationToken) +{ + if (currentSnapshot is not null) + { + return; + } + + await settingsService.LoadAsync(cancellationToken).ConfigureAwait(false); + await ReloadSnapshotAsync(broadcast: false, cancellationToken).ConfigureAwait(false); +} + +void QueueSnapshotRefresh() +{ + if (Interlocked.Exchange(ref backgroundRefreshActive, 1) == 1) + { + return; + } + + _ = Task.Run(async () => + { + try + { + await settingsService.LoadAsync().ConfigureAwait(false); + await ReloadSnapshotAsync(broadcast: true, CancellationToken.None).ConfigureAwait(false); + } + catch (Exception exception) + { + await logService.WriteAsync("Warning", "plugin-host", "Plugin background refresh failed", exception.Message).ConfigureAwait(false); + } + finally + { + Interlocked.Exchange(ref backgroundRefreshActive, 0); + } + }); +} + +async Task ReloadSnapshotAsync(bool broadcast, CancellationToken cancellationToken) +{ + await snapshotGate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + var plugins = await registry.LoadPluginsAsync(cancellationToken).ConfigureAwait(false); + var tools = settingsService.Current.PluginsEnabled + ? plugins + .Where(plugin => plugin.IsValid && plugin.State.Enabled) + .SelectMany(plugin => plugin.Manifest.Surfaces + .Where(surface => surface.Kind == PluginSurfaceKind.ToolboxTool && + (plugin.State.MountedSurfaceIds.Count == 0 || plugin.State.MountedSurfaceIds.Contains(surface.Id))) + .Select(surface => new PluginToolModule(plugin, surface))) + .ToArray() + : []; + currentSnapshot = new PluginSnapshot( + settingsService.Current.PluginsEnabled, + registry.PluginsRoot, + plugins.Select(LoadedPluginDto.FromLoadedPlugin).ToArray(), + tools.Select(PluginToolDto.FromPluginToolModule).ToArray(), + DateTimeOffset.Now); + ConfigureWatcher(); + } + finally + { + snapshotGate.Release(); + } + + if (broadcast && currentSnapshot is not null) + { + await WriteAsync(new PluginHostMessage(PluginHostProtocol.SnapshotChanged, Snapshot: currentSnapshot), CancellationToken.None).ConfigureAwait(false); + } +} + +void ConfigureWatcher() +{ + if (!settingsService.Current.PluginsEnabled) + { + watcher?.Dispose(); + watcher = null; + return; + } + + Directory.CreateDirectory(registry.PluginsRoot); + if (watcher is not null && string.Equals(watcher.Path, registry.PluginsRoot, StringComparison.OrdinalIgnoreCase)) + { + return; + } + + watcher?.Dispose(); + watcher = new FileSystemWatcher(registry.PluginsRoot) + { + IncludeSubdirectories = true, + NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.CreationTime | NotifyFilters.Size, + EnableRaisingEvents = true + }; + watcher.Created += PluginFilesChanged; + watcher.Changed += PluginFilesChanged; + watcher.Deleted += PluginFilesChanged; + watcher.Renamed += PluginFilesChanged; + watcher.Error += (_, e) => _ = logService.WriteAsync("Warning", "plugin-host", "Plugin watcher failed", e.GetException().Message); +} + +void PluginFilesChanged(object sender, FileSystemEventArgs e) +{ + reloadDebounce?.Cancel(); + reloadDebounce?.Dispose(); + var cts = new CancellationTokenSource(); + reloadDebounce = cts; + _ = Task.Run(async () => + { + try + { + await Task.Delay(500, cts.Token).ConfigureAwait(false); + await settingsService.LoadAsync(cts.Token).ConfigureAwait(false); + await ReloadSnapshotAsync(broadcast: true, cts.Token).ConfigureAwait(false); + await logService.WriteAsync("Information", "plugin-host", "Plugin hot reload", e.Name).ConfigureAwait(false); + } + catch (OperationCanceledException) + { + } + catch (Exception exception) + { + await logService.WriteAsync("Error", "plugin-host", "Plugin hot reload failed", exception.Message).ConfigureAwait(false); + } + }); +} + +async Task<PluginBridgeResponse> HandleBridgeAsync(PluginBridgeRequest request) +{ + var plugin = await FindPluginAsync(request.PluginId).ConfigureAwait(false); + if (plugin is null || !plugin.IsValid || !plugin.State.Enabled) + { + return Fail("Plugin is not enabled or valid."); + } + + try + { + using var document = string.IsNullOrWhiteSpace(request.PayloadJson) + ? JsonDocument.Parse("null") + : JsonDocument.Parse(request.PayloadJson); + var payload = document.RootElement; + return request.Method switch + { + "input.get" => JsonOk(GetRuntime(plugin.Manifest.Id, "input", "{}")), + "input.set" => SetRuntime(plugin.Manifest.Id, "input", JsonValue(payload), PluginPermission.Input), + "output.set" => AuthorizeUi(plugin, PluginPermission.Output), + "output.append" => AuthorizeUi(plugin, PluginPermission.Output), + "output.clear" => AuthorizeUi(plugin, PluginPermission.Output), + "log.info" => await LogAsync(plugin, "Information", payload).ConfigureAwait(false), + "log.warn" => await LogAsync(plugin, "Warning", payload).ConfigureAwait(false), + "log.error" => await LogAsync(plugin, "Error", payload).ConfigureAwait(false), + "storage.get" => await GetStorageAsync(plugin, payload).ConfigureAwait(false), + "storage.set" => await SetStorageAsync(plugin, payload).ConfigureAwait(false), + "storage.remove" => await RemoveStorageAsync(plugin, payload).ConfigureAwait(false), + "storage.list" => await ListStorageAsync(plugin).ConfigureAwait(false), + "http.fetch" => await FetchAsync(plugin, payload).ConfigureAwait(false), + "network.ping" => await PingAsync(plugin, payload).ConfigureAwait(false), + "network.dnsLookup" => await DnsLookupAsync(plugin, payload).ConfigureAwait(false), + "network.diagnostics" => NetworkDiagnostics(plugin), + "network.traceRoute" => await TraceRouteAsync(plugin, payload).ConfigureAwait(false), + "tool.run" => await RunToolAsync(plugin, payload).ConfigureAwait(false), + "clipboard.readText" => AuthorizeUi(plugin, PluginPermission.Clipboard, "clipboard.readText"), + "clipboard.writeText" => AuthorizeUi(plugin, PluginPermission.Clipboard, "clipboard.writeText"), + "file.openPicker" => AuthorizeUi(plugin, PluginPermission.FilePicker, "file.openPicker"), + "file.savePicker" => AuthorizeUi(plugin, PluginPermission.FilePicker, "file.savePicker"), + "openExternal" => ValidateExternal(plugin, payload), + _ => Fail($"Unknown plugin bridge method: {request.Method}") + }; + } + catch (Exception exception) + { + return Fail(exception.Message); + } +} + +async Task<LoadedPlugin?> FindPluginAsync(string pluginId) +{ + var snapshot = currentSnapshot; + if (snapshot is null) + { + await ReloadSnapshotAsync(broadcast: false, CancellationToken.None).ConfigureAwait(false); + snapshot = currentSnapshot; + } + + return snapshot?.Plugins + .FirstOrDefault(plugin => string.Equals(plugin.Manifest.Id, pluginId, StringComparison.OrdinalIgnoreCase)) + ?.ToLoadedPlugin(); +} + +PluginBridgeResponse SetRuntime(string pluginId, string key, string value, PluginPermission permission) +{ + EnsurePermissionById(pluginId, permission); + Runtime(pluginId)[key] = value; + return JsonOk(true); +} + +PluginBridgeResponse AuthorizeUi(LoadedPlugin plugin, PluginPermission permission, string? uiAction = null) +{ + EnsurePluginPermission(plugin, permission); + return new PluginBridgeResponse(true, JsonSerializer.Serialize(true), UiAction: uiAction); +} + +PluginBridgeResponse ValidateExternal(LoadedPlugin plugin, JsonElement payload) +{ + EnsurePluginPermission(plugin, PluginPermission.OpenExternal); + var value = payload.ValueKind == JsonValueKind.Object + ? ReadString(payload, "url") ?? ReadString(payload, "uri") ?? string.Empty + : JsonValue(payload); + if (!Uri.TryCreate(value, UriKind.Absolute, out var uri) || + (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps)) + { + return Fail("Only absolute http/https URLs can be opened externally."); + } + + return new PluginBridgeResponse(true, JsonSerializer.Serialize(true), UiAction: "openExternal"); +} + +async Task<PluginBridgeResponse> LogAsync(LoadedPlugin plugin, string level, JsonElement payload) +{ + EnsurePluginPermission(plugin, PluginPermission.Log); + var message = ReadString(payload, "message") ?? JsonValue(payload); + var detail = ReadString(payload, "detail"); + await logService.WriteAsync(level, $"plugin:{plugin.Manifest.Id}", message, detail).ConfigureAwait(false); + return JsonOk(true); +} + +async Task<PluginBridgeResponse> GetStorageAsync(LoadedPlugin plugin, JsonElement payload) +{ + EnsurePluginPermission(plugin, PluginPermission.Storage); + var value = await stateStore.GetValueAsync(plugin.Manifest.Id, JsonValue(payload)).ConfigureAwait(false); + return JsonOk(value); +} + +async Task<PluginBridgeResponse> SetStorageAsync(LoadedPlugin plugin, JsonElement payload) +{ + EnsurePluginPermission(plugin, PluginPermission.Storage); + var key = ReadString(payload, "key") ?? throw new InvalidOperationException("storage.set requires key."); + var value = ReadString(payload, "value") ?? JsonValue(payload.GetProperty("value")); + await stateStore.SetValueAsync(plugin.Manifest.Id, key, value).ConfigureAwait(false); + return JsonOk(true); +} + +async Task<PluginBridgeResponse> RemoveStorageAsync(LoadedPlugin plugin, JsonElement payload) +{ + EnsurePluginPermission(plugin, PluginPermission.Storage); + await stateStore.RemoveValueAsync(plugin.Manifest.Id, JsonValue(payload)).ConfigureAwait(false); + return JsonOk(true); +} + +async Task<PluginBridgeResponse> ListStorageAsync(LoadedPlugin plugin) +{ + EnsurePluginPermission(plugin, PluginPermission.Storage); + return JsonOk(await stateStore.ListValuesAsync(plugin.Manifest.Id).ConfigureAwait(false)); +} + +async Task<PluginBridgeResponse> FetchAsync(LoadedPlugin plugin, JsonElement payload) +{ + EnsurePluginPermission(plugin, PluginPermission.Http); + var rawUrl = payload.ValueKind == JsonValueKind.Object ? ReadString(payload, "url") ?? ReadString(payload, "uri") : JsonValue(payload); + if (!Uri.TryCreate(rawUrl, UriKind.Absolute, out var uri) || + (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps)) + { + return Fail("ymhut.http.fetch only accepts absolute http/https URLs."); + } + + var method = payload.ValueKind == JsonValueKind.Object ? ReadString(payload, "method") ?? "GET" : "GET"; + var body = payload.ValueKind == JsonValueKind.Object ? ReadString(payload, "body") : null; + var headers = ReadHeaders(payload); + var result = await httpService.SendAsync(uri, method, body, headers, ensureSuccess: false).ConfigureAwait(false); + await logService.WriteAsync("Information", $"plugin:{plugin.Manifest.Id}", "Plugin HTTP fetch", uri.Host).ConfigureAwait(false); + return JsonOk(new + { + status = (int)result.StatusCode, + ok = (int)result.StatusCode is >= 200 and < 300, + content = result.Content, + headers = result.Headers, + elapsedMs = (long)result.Elapsed.TotalMilliseconds + }); +} + +async Task<PluginBridgeResponse> PingAsync(LoadedPlugin plugin, JsonElement payload) +{ + EnsurePluginPermission(plugin, PluginPermission.NetworkDiagnostics); + var host = payload.ValueKind == JsonValueKind.Object ? ReadString(payload, "host") : JsonValue(payload); + if (string.IsNullOrWhiteSpace(host)) + { + return Fail("network.ping requires host."); + } + + var count = payload.ValueKind == JsonValueKind.Object ? ReadInt(payload, "count", 4, 1, 12) : 4; + var timeout = payload.ValueKind == JsonValueKind.Object ? ReadInt(payload, "timeoutMs", 2500, 500, 10000) : 2500; + var rows = new List<object>(); + using var ping = new Ping(); + for (var index = 0; index < count; index++) + { + try + { + var reply = await ping.SendPingAsync(host.Trim(), timeout).ConfigureAwait(false); + rows.Add(new + { + seq = index + 1, + status = reply.Status.ToString(), + address = reply.Address?.ToString() ?? string.Empty, + roundtripMs = reply.Status == IPStatus.Success ? reply.RoundtripTime : -1 + }); + } + catch (Exception exception) + { + rows.Add(new { seq = index + 1, status = "Error", address = string.Empty, roundtripMs = -1, error = exception.Message }); + } + } + + var success = rows + .Select(item => JsonSerializer.SerializeToElement(item)) + .Where(item => string.Equals(ReadString(item, "status"), IPStatus.Success.ToString(), StringComparison.OrdinalIgnoreCase)) + .Select(item => (double)ReadLong(item, "roundtripMs", -1)) + .Where(value => value >= 0) + .ToArray(); + return JsonOk(new + { + host = host.Trim(), + count, + sent = count, + received = success.Length, + lossPercent = Math.Round((count - success.Length) * 100d / count, 1), + minMs = success.Length == 0 ? -1 : Math.Round(success.Min(), 1), + avgMs = success.Length == 0 ? -1 : Math.Round(success.Average(), 1), + maxMs = success.Length == 0 ? -1 : Math.Round(success.Max(), 1), + replies = rows + }); +} + +async Task<PluginBridgeResponse> DnsLookupAsync(LoadedPlugin plugin, JsonElement payload) +{ + EnsurePluginPermission(plugin, PluginPermission.NetworkDiagnostics); + var host = payload.ValueKind == JsonValueKind.Object ? ReadString(payload, "host") : JsonValue(payload); + if (string.IsNullOrWhiteSpace(host)) + { + return Fail("network.dnsLookup requires host."); + } + + try + { + var started = DateTimeOffset.UtcNow; + var entries = await Dns.GetHostAddressesAsync(host.Trim()).ConfigureAwait(false); + return JsonOk(new + { + host = host.Trim(), + elapsedMs = (long)(DateTimeOffset.UtcNow - started).TotalMilliseconds, + addresses = entries.Select(address => new + { + value = address.ToString(), + family = address.AddressFamily.ToString() + }).ToArray() + }); + } + catch (Exception exception) + { + return Fail(exception.Message); + } +} + +async Task<PluginBridgeResponse> TraceRouteAsync(LoadedPlugin plugin, JsonElement payload) +{ + EnsurePluginPermission(plugin, PluginPermission.NetworkDiagnostics); + var host = payload.ValueKind == JsonValueKind.Object ? ReadString(payload, "host") : JsonValue(payload); + if (string.IsNullOrWhiteSpace(host)) + { + return Fail("network.traceRoute requires host."); + } + + var maxHops = payload.ValueKind == JsonValueKind.Object ? ReadInt(payload, "maxHops", 12, 1, 30) : 12; + var timeout = payload.ValueKind == JsonValueKind.Object ? ReadInt(payload, "timeoutMs", 2200, 500, 8000) : 2200; + var hops = new List<object>(); + var buffer = Encoding.ASCII.GetBytes("ymhut"); + using var ping = new Ping(); + for (var ttl = 1; ttl <= maxHops; ttl++) + { + try + { + var options = new PingOptions(ttl, dontFragment: true); + var reply = await ping.SendPingAsync(host.Trim(), timeout, buffer, options).ConfigureAwait(false); + hops.Add(new + { + ttl, + status = reply.Status.ToString(), + address = reply.Address?.ToString() ?? "*", + roundtripMs = reply.Status is IPStatus.Success or IPStatus.TtlExpired ? reply.RoundtripTime : -1 + }); + if (reply.Status == IPStatus.Success) + { + break; + } + } + catch (Exception exception) + { + hops.Add(new { ttl, status = "Error", address = "*", roundtripMs = -1, error = exception.Message }); + } + } + + return JsonOk(new { host = host.Trim(), maxHops, hops }); +} + +PluginBridgeResponse NetworkDiagnostics(LoadedPlugin plugin) +{ + EnsurePluginPermission(plugin, PluginPermission.NetworkDiagnostics); + var settings = settingsService.Current; + var interfaces = NetworkInterface.GetAllNetworkInterfaces() + .Where(item => item.NetworkInterfaceType != NetworkInterfaceType.Loopback) + .Select(item => + { + var properties = item.GetIPProperties(); + var statistics = item.GetIPv4Statistics(); + return new + { + id = item.Id, + name = item.Name, + description = item.Description, + type = item.NetworkInterfaceType.ToString(), + status = item.OperationalStatus.ToString(), + speedMbps = item.Speed > 0 ? Math.Round(item.Speed / 1000d / 1000d, 1) : 0, + dnsSuffix = properties.DnsSuffix, + ipv4 = properties.UnicastAddresses + .Where(address => address.Address.AddressFamily == AddressFamily.InterNetwork) + .Select(address => address.Address.ToString()) + .ToArray(), + ipv6 = properties.UnicastAddresses + .Where(address => address.Address.AddressFamily == AddressFamily.InterNetworkV6) + .Select(address => address.Address.ToString()) + .ToArray(), + gateways = properties.GatewayAddresses.Select(address => address.Address.ToString()).ToArray(), + dnsServers = properties.DnsAddresses.Select(address => address.ToString()).Distinct(StringComparer.OrdinalIgnoreCase).ToArray(), + bytesReceived = statistics.BytesReceived, + bytesSent = statistics.BytesSent + }; + }) + .ToArray(); + + var active = interfaces.Where(item => string.Equals(item.status, "Up", StringComparison.OrdinalIgnoreCase)).ToArray(); + var dnsServers = active.SelectMany(item => item.dnsServers).Distinct(StringComparer.OrdinalIgnoreCase).ToArray(); + return JsonOk(new + { + generatedAt = DateTimeOffset.Now, + machine = Environment.MachineName, + os = Environment.OSVersion.VersionString, + processArchitecture = RuntimeInformation.ProcessArchitecture.ToString(), + uiCulture = CultureInfo.CurrentUICulture.Name, + localTimeZone = TimeZoneInfo.Local.Id, + proxy = new + { + enabled = settings.ProxyEnabled, + mode = settings.ProxyMode, + host = settings.ProxyEnabled ? settings.ProxyHost : string.Empty, + port = settings.ProxyEnabled ? settings.ProxyPort : 0 + }, + interfaces, + summary = new + { + interfaceCount = interfaces.Length, + activeInterfaceCount = active.Length, + ipv4Count = active.Sum(item => item.ipv4.Length), + ipv6Count = active.Sum(item => item.ipv6.Length), + dnsServers, + defaultGateways = active.SelectMany(item => item.gateways).Distinct(StringComparer.OrdinalIgnoreCase).ToArray() + }, + offlineMode = true, + note = "Local diagnostics only. Public IP, ASN, external DNS leak, and internet speed require a remote observer and are intentionally not fetched by the built-in sample." + }); +} + +async Task<PluginBridgeResponse> RunToolAsync(LoadedPlugin plugin, JsonElement payload) +{ + EnsurePluginPermission(plugin, PluginPermission.RunTool); + var toolId = ReadString(payload, "toolId") ?? throw new InvalidOperationException("ymhut.tool.run requires toolId."); + var input = ReadString(payload, "input") ?? string.Empty; + if (PluginIds.IsPluginToolId(toolId)) + { + return Fail("Plugins cannot call plugin tools through ymhut.tool.run."); + } + + var catalog = new ToolCatalog(); + var module = catalog.GetById(toolId) ?? throw new InvalidOperationException($"Tool was not found: {toolId}"); + if (!module.Metadata.OfflineCapable) + { + return Fail("ymhut.tool.run v1 only allows offline built-in tools."); + } + + var result = await ToolExecutor.ExecuteAsync(module, input, apiManager: apiManager, referenceDataService: referenceDataService).ConfigureAwait(false); + await logService.WriteAsync("Information", $"plugin:{plugin.Manifest.Id}", "Plugin ran built-in tool", toolId).ConfigureAwait(false); + return JsonOk(new { ok = result.Ok, output = result.Output, error = result.Error }); +} + +void EnsurePermissionById(string pluginId, PluginPermission permission) +{ + var plugin = currentSnapshot?.Plugins.FirstOrDefault(item => string.Equals(item.Manifest.Id, pluginId, StringComparison.OrdinalIgnoreCase))?.ToLoadedPlugin() + ?? throw new UnauthorizedAccessException("Plugin is not loaded."); + EnsurePluginPermission(plugin, permission); +} + +static void EnsurePluginPermission(LoadedPlugin plugin, PluginPermission permission) +{ + if (!plugin.State.GrantedPermissions.Contains(permission)) + { + throw new UnauthorizedAccessException($"Plugin permission is not granted: {permission}"); + } +} + +ConcurrentDictionary<string, string> Runtime(string pluginId) +{ + return runtimeValues.GetOrAdd(pluginId, _ => new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase)); +} + +string GetRuntime(string pluginId, string key, string fallback) +{ + return Runtime(pluginId).TryGetValue(key, out var value) ? value : fallback; +} + +static PluginBridgeResponse JsonOk(object? value) +{ + return new PluginBridgeResponse(true, JsonSerializer.Serialize(value)); +} + +static PluginBridgeResponse Fail(string error) +{ + return new PluginBridgeResponse(false, Error: error); +} + +async Task WriteAsync(PluginHostMessage message, CancellationToken cancellationToken) +{ + await writeGate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await writer.WriteLineAsync(PluginHostProtocol.Serialize(message).AsMemory(), cancellationToken).ConfigureAwait(false); + await writer.FlushAsync(cancellationToken).ConfigureAwait(false); + } + finally + { + writeGate.Release(); + } +} + +static string Required(string? value) +{ + return string.IsNullOrWhiteSpace(value) ? throw new InvalidOperationException("Missing required value.") : value; +} + +static string JsonValue(JsonElement element) +{ + return element.ValueKind == JsonValueKind.String ? element.GetString() ?? string.Empty : element.GetRawText(); +} + +static string? ReadString(JsonElement element, string property) +{ + return element.ValueKind == JsonValueKind.Object && element.TryGetProperty(property, out var value) ? JsonValue(value) : null; +} + +static IReadOnlyDictionary<string, string>? ReadHeaders(JsonElement payload) +{ + if (payload.ValueKind != JsonValueKind.Object || + !payload.TryGetProperty("headers", out var headersElement) || + headersElement.ValueKind != JsonValueKind.Object) + { + return null; + } + + var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); + foreach (var property in headersElement.EnumerateObject()) + { + headers[property.Name] = property.Value.ValueKind == JsonValueKind.String + ? property.Value.GetString() ?? string.Empty + : property.Value.GetRawText(); + } + + return headers; +} + +static int ReadInt(JsonElement element, string property, int fallback, int min, int max) +{ + if (element.ValueKind != JsonValueKind.Object || !element.TryGetProperty(property, out var value)) + { + return fallback; + } + + var parsed = value.ValueKind == JsonValueKind.Number && value.TryGetInt32(out var number) + ? number + : int.TryParse(JsonValue(value), out var textNumber) + ? textNumber + : fallback; + return Math.Clamp(parsed, min, max); +} + +static long ReadLong(JsonElement element, string property, long fallback) +{ + if (element.ValueKind != JsonValueKind.Object || !element.TryGetProperty(property, out var value)) + { + return fallback; + } + + return value.ValueKind == JsonValueKind.Number && value.TryGetInt64(out var number) + ? number + : long.TryParse(JsonValue(value), out var textNumber) + ? textNumber + : fallback; +} + +static string? ReadPipeName(string[] args) +{ + for (var index = 0; index < args.Length; index++) + { + if (string.Equals(args[index], "--pipe", StringComparison.OrdinalIgnoreCase) && index + 1 < args.Length) + { + return args[index + 1]; + } + } + + return null; +} diff --git a/src/YMhut.Box.PluginHost/YMhut.Box.PluginHost.csproj b/src/YMhut.Box.PluginHost/YMhut.Box.PluginHost.csproj new file mode 100644 index 0000000..f8ea68a --- /dev/null +++ b/src/YMhut.Box.PluginHost/YMhut.Box.PluginHost.csproj @@ -0,0 +1,15 @@ +<Project Sdk="Microsoft.NET.Sdk"> + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net10.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)' == 'Release'"> + <DebugType>none</DebugType> + <DebugSymbols>false</DebugSymbols> + </PropertyGroup> + <ItemGroup> + <ProjectReference Include="..\YMhut.Box.Core\YMhut.Box.Core.csproj" /> + </ItemGroup> +</Project> diff --git a/src/YMhut.Box.Tests/AgreementAcceptanceStoreTests.cs b/src/YMhut.Box.Tests/AgreementAcceptanceStoreTests.cs new file mode 100644 index 0000000..5ca4943 --- /dev/null +++ b/src/YMhut.Box.Tests/AgreementAcceptanceStoreTests.cs @@ -0,0 +1,81 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.Data.Sqlite; +using YMhut.Box.Core.App; +using YMhut.Box.Core.Logging; +using YMhut.Box.Core.Settings; + +namespace YMhut.Box.Tests; + +[TestClass] +public sealed class AgreementAcceptanceStoreTests +{ + [TestMethod] + public void OldBooleanAgreementSettingRequiresResign() + { + var settings = new AppSettings + { + UserAgreementAccepted = true + }; + + Assert.IsFalse(AgreementDocument.SettingsMatchCurrent(settings)); + + settings.UserAgreementVersion = AgreementDocument.CurrentVersion; + + Assert.IsTrue(AgreementDocument.SettingsMatchCurrent(settings)); + } + + [TestMethod] + public async Task AgreementAcceptanceUsesMainSqliteAndSurvivesLogCleanup() + { + using var workspace = TestWorkspace.Create("ymhut-agreement-store"); + var paths = AppPaths.ForCurrentUser(workspace.Root); + var store = new AgreementAcceptanceStore(paths); + var logService = new SqliteLogService(paths); + var acceptedAt = new DateTimeOffset(2026, 6, 15, 9, 30, 0, TimeSpan.Zero); + + await store.RecordAcceptedAsync( + AgreementDocument.CurrentVersion, + AgreementDocument.CurrentRevision, + "2.0.6.2", + "zh-CN", + acceptedAt); + await logService.WriteAsync("Information", "test", "log before cleanup"); + await logService.ClearAllAsync(); + + var latest = await store.GetLatestAsync(); + + Assert.AreEqual(Path.Combine(paths.Logs, AppDatabasePaths.MainDatabaseFileName), store.DatabasePath); + Assert.IsNotNull(latest); + Assert.AreEqual(AgreementDocument.CurrentVersion, latest.Version); + Assert.AreEqual(AgreementDocument.CurrentRevision, latest.Revision); + Assert.AreEqual("2.0.6.2", latest.AppVersion); + Assert.AreEqual("zh-CN", latest.Language); + Assert.AreEqual(acceptedAt, latest.AcceptedAt); + } + + private sealed class TestWorkspace : IDisposable + { + private TestWorkspace(string root) + { + Root = root; + } + + public string Root { get; } + + public static TestWorkspace Create(string prefix) + { + var root = Path.Combine(Path.GetTempPath(), prefix, Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(root); + return new TestWorkspace(root); + } + + public void Dispose() + { + SqliteConnection.ClearAllPools(); + if (Directory.Exists(Root)) + { + Directory.Delete(Root, recursive: true); + } + } + } +} diff --git a/src/YMhut.Box.Tests/AppSettingsStoreTests.cs b/src/YMhut.Box.Tests/AppSettingsStoreTests.cs new file mode 100644 index 0000000..2e6b0a1 --- /dev/null +++ b/src/YMhut.Box.Tests/AppSettingsStoreTests.cs @@ -0,0 +1,185 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using YMhut.Box.Core.Settings; + +namespace YMhut.Box.Tests; + +[TestClass] +public sealed class AppSettingsStoreTests +{ + [TestMethod] + public async Task NewSettingsDefaultToDashboardHome() + { + var root = Path.Combine(Path.GetTempPath(), "ymhut-box-tests", Guid.NewGuid().ToString("N")); + var store = new AppSettingsStore(root); + + var settings = await store.LoadAsync(); + + Assert.AreEqual("dashboard", settings.HomePageMode); + } + + [TestMethod] + public async Task RecordRecentToolKeepsNewestFirstAndUnique() + { + var root = Path.Combine(Path.GetTempPath(), "ymhut-box-tests", Guid.NewGuid().ToString("N")); + var store = new AppSettingsStore(root); + + await store.RecordRecentToolAsync("json_formatter"); + await store.RecordRecentToolAsync("safe_browser"); + await store.RecordRecentToolAsync("json_formatter"); + + var settings = await store.LoadAsync(); + CollectionAssert.AreEqual( + new[] { "json_formatter", "safe_browser" }, + settings.RecentToolIds); + } + + [TestMethod] + public async Task LoadAsyncImportsLegacySettingsOnce() + { + var root = Path.Combine(Path.GetTempPath(), "ymhut-box-tests", Guid.NewGuid().ToString("N"), "WinUI"); + var legacyPath = Path.Combine(Path.GetDirectoryName(root)!, "YMhut Box", "settings.json"); + Directory.CreateDirectory(Path.GetDirectoryName(legacyPath)!); + await File.WriteAllTextAsync(legacyPath, """ + { + "theme": "Dark", + "recentToolIds": [ + "safe_browser" + ], + "legacyImportCompleted": false + } + """); + + var store = new AppSettingsStore(root); + var settings = await store.LoadAsync(); + + Assert.IsTrue(settings.LegacyImportCompleted); + Assert.AreEqual("Dark", settings.Theme); + CollectionAssert.AreEqual(new[] { "safe_browser" }, settings.RecentToolIds); + } + + [TestMethod] + public async Task LoadAsyncImportsLegacySharedPreferences() + { + var root = Path.Combine(Path.GetTempPath(), "ymhut-box-tests", Guid.NewGuid().ToString("N"), "WinUI"); + var legacyPath = Path.Combine(Path.GetDirectoryName(root)!, "ymhut_box", "shared_preferences.json"); + Directory.CreateDirectory(Path.GetDirectoryName(legacyPath)!); + var legacyPrefix = string.Concat("flut", "ter."); + await File.WriteAllTextAsync(legacyPath, """ + { + "__PREFIX__theme": "dark", + "__PREFIX__pitch_black": true, + "__PREFIX__seed_color": -10458044, + "__PREFIX__card_opacity": 0.64, + "__PREFIX__background_image": "D:\\Images\\wallpaper.png", + "__PREFIX__background_opacity": 0.42, + "__PREFIX__user_agreement_accepted": true, + "__PREFIX__sidebar_collapsed": true, + "__PREFIX__desktop_titlebar": false, + "__PREFIX__recent_tool_ids": [ + "media_player", + "safe_browser" + ], + "__PREFIX__pinned_tool_ids": [ + "json_formatter" + ], + "__PREFIX__window_x": 12.5, + "__PREFIX__window_y": 24.5, + "__PREFIX__window_width": 1280, + "__PREFIX__window_height": 760, + "__PREFIX__window_maximized": true, + "__PREFIX__proxy_enabled": true, + "__PREFIX__proxy_mode": "manual", + "__PREFIX__proxy_host": "127.0.0.1", + "__PREFIX__proxy_port": 7891, + "__PREFIX__animations_enabled": false, + "__PREFIX__font_size": 1.15, + "__PREFIX__tool_display_mode": "list", + "__PREFIX__close_behavior": "minimize_then_exit", + "__PREFIX__restore_window_position": false, + "__PREFIX__auto_start": true, + "__PREFIX__log_retention_count": 300, + "__PREFIX__data_refresh_interval": 45, + "__PREFIX__update_notification": false, + "__PREFIX__hardware_acceleration_enabled": false, + "__PREFIX__proxy_test_timeout_seconds": 9 + } + """.Replace("__PREFIX__", legacyPrefix)); + + var store = new AppSettingsStore(root); + var settings = await store.LoadAsync(); + + Assert.AreEqual("Dark", settings.Theme); + Assert.IsTrue(settings.PitchBlack); + Assert.AreEqual(-10458044, settings.SeedColor); + Assert.AreEqual(0.64, settings.CardOpacity, 0.001); + Assert.AreEqual("D:\\Images\\wallpaper.png", settings.BackgroundImage); + Assert.AreEqual(0.42, settings.BackgroundOpacity, 0.001); + Assert.IsTrue(settings.UserAgreementAccepted); + Assert.IsTrue(settings.SidebarCollapsed); + Assert.IsFalse(settings.DesktopTitlebar); + CollectionAssert.AreEqual(new[] { "media_player", "safe_browser" }, settings.RecentToolIds); + CollectionAssert.AreEqual(new[] { "json_formatter" }, settings.PinnedToolIds); + Assert.AreEqual(12.5, settings.WindowX); + Assert.AreEqual(24.5, settings.WindowY); + Assert.AreEqual(1280, settings.WindowWidth); + Assert.AreEqual(760, settings.WindowHeight); + Assert.IsTrue(settings.WindowMaximized); + Assert.IsTrue(settings.ProxyEnabled); + Assert.AreEqual("manual", settings.ProxyMode); + Assert.AreEqual("127.0.0.1", settings.ProxyHost); + Assert.AreEqual(7891, settings.ProxyPort); + Assert.IsFalse(settings.AnimationsEnabled); + Assert.AreEqual(1.15, settings.FontSize, 0.001); + Assert.AreEqual("list", settings.ToolDisplayMode); + Assert.AreEqual("minimize_then_exit", settings.CloseBehavior); + Assert.IsFalse(settings.RestoreWindowPosition); + Assert.IsTrue(settings.AutoStart); + Assert.AreEqual(300, settings.LogRetentionCount); + Assert.AreEqual(45, settings.DataRefreshInterval); + Assert.IsFalse(settings.UpdateNotification); + Assert.IsFalse(settings.HardwareAccelerationEnabled); + Assert.AreEqual(9, settings.ProxyTestTimeoutSeconds); + } + + [TestMethod] + public void PinnedToolSettingsTogglesUniquelyAndTrims() + { + var pinned = Enumerable.Range(0, 70) + .Select(index => $"tool_{index:00}") + .ToList(); + pinned.Insert(10, "json_formatter"); + + PinnedToolSettings.Toggle(pinned, "json_formatter", wasPinned: false); + + Assert.HasCount(64, pinned); + Assert.AreEqual("json_formatter", pinned[0]); + Assert.AreEqual(1, pinned.Count(id => string.Equals(id, "json_formatter", StringComparison.OrdinalIgnoreCase))); + + PinnedToolSettings.Toggle(pinned, "JSON_FORMATTER", wasPinned: true); + + Assert.HasCount(63, pinned); + Assert.IsFalse(pinned.Any(id => string.Equals(id, "json_formatter", StringComparison.OrdinalIgnoreCase))); + } + + [TestMethod] + public async Task LoadAsyncNormalizesGlobalMaterialSettings() + { + var root = Path.Combine(Path.GetTempPath(), "ymhut-box-tests", Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(root); + await File.WriteAllTextAsync(Path.Combine(root, "settings.json"), """ + { + "legacyImportCompleted": true, + "windowBackdrop": "desktop_acrylic", + "settingsPanelMaterial": "frosted_glass", + "topBarMaterial": "frosted_glass" + } + """); + + var store = new AppSettingsStore(root); + var settings = await store.LoadAsync(); + + Assert.AreEqual("acrylic", settings.WindowBackdrop); + Assert.AreEqual("glass", settings.SettingsPanelMaterial); + Assert.AreEqual("glass", settings.TopBarMaterial); + } +} diff --git a/src/YMhut.Box.Tests/AssemblyInfo.cs b/src/YMhut.Box.Tests/AssemblyInfo.cs new file mode 100644 index 0000000..ae411c7 --- /dev/null +++ b/src/YMhut.Box.Tests/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; + +[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)] diff --git a/src/YMhut.Box.Tests/DownloadAndDevEnvironmentTests.cs b/src/YMhut.Box.Tests/DownloadAndDevEnvironmentTests.cs new file mode 100644 index 0000000..cf2f37b --- /dev/null +++ b/src/YMhut.Box.Tests/DownloadAndDevEnvironmentTests.cs @@ -0,0 +1,377 @@ +using System.Net; +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using YMhut.Box.Core.App; +using YMhut.Box.Core.DevEnvironments; +using YMhut.Box.Core.Downloads; +using YMhut.Box.Core.Net; +using YMhut.Box.Core.Updates; + +namespace YMhut.Box.Tests; + +[TestClass] +public sealed class DownloadAndDevEnvironmentTests +{ + [TestMethod] + public void DevEnvironmentVersionParserRecognizesSupportedTools() + { + Assert.AreEqual("1.23.4", DevEnvironmentVersionParser.Parse("go", "go version go1.23.4 windows/amd64")); + Assert.AreEqual("3.12.7", DevEnvironmentVersionParser.Parse("python", "Python 3.12.7")); + Assert.AreEqual("21.0.2", DevEnvironmentVersionParser.Parse("java", "openjdk version \"21.0.2\" 2024-01-16")); + Assert.AreEqual("27.3.1", DevEnvironmentVersionParser.Parse("docker", "Docker version 27.3.1, build ce12230")); + Assert.AreEqual("8.0.36", DevEnvironmentVersionParser.Parse("mysql", "mysql Ver 8.0.36 for Win64 on x86_64")); + Assert.AreEqual("22.11.0", DevEnvironmentVersionParser.Parse("node", "v22.11.0")); + Assert.AreEqual("10.0.100", DevEnvironmentVersionParser.Parse("dotnet", "10.0.100\r\n9.0.308")); + Assert.AreEqual("2.47.1.windows.2", DevEnvironmentVersionParser.Parse("git", "git version 2.47.1.windows.2")); + Assert.AreEqual("1.82.0", DevEnvironmentVersionParser.Parse("rust", "rustc 1.82.0 (f6e511eec 2024-10-15)")); + Assert.AreEqual("3.31.0", DevEnvironmentVersionParser.Parse("cmake", "cmake version 3.31.0")); + } + + [TestMethod] + public async Task DevEnvironmentCatalogReadsOfficialVersionMetadata() + { + var workspace = TempWorkspace(); + var paths = AppPaths.ForCurrentUser(workspace); + var service = new DevEnvironmentCatalogService( + paths, + new FakeHttpService(uri => uri.AbsoluteUri switch + { + "https://go.dev/dl/?mode=json" => """ + [ + { + "version": "go1.23.4", + "stable": true, + "files": [ + { "filename": "go1.23.4.windows-amd64.msi", "sha256": "abc" }, + { "filename": "go1.23.4.src.tar.gz", "sha256": "def" } + ] + } + ] + """, + "https://nodejs.org/dist/index.json" => """ + [ + { "version": "v22.11.0", "date": "2024-10-29" } + ] + """, + "https://api.github.com/repos/git-for-windows/git/releases" => """ + [ + { + "tag_name": "v2.47.1.windows.2", + "published_at": "2024-10-01T00:00:00Z", + "html_url": "https://github.com/git-for-windows/git/releases/tag/v2.47.1.windows.2", + "assets": [ + { + "name": "Git-2.47.1-64-bit.exe", + "browser_download_url": "https://github.com/git-for-windows/git/releases/download/v2.47.1.windows.2/Git-2.47.1-64-bit.exe", + "size": 1024 + } + ] + } + ] + """, + _ => """ + { + "releases-index": [ + { + "channel-version": "10.0", + "latest-sdk": "10.0.100", + "release-date": "2025-11-11" + } + ] + } + """ + })); + + var go = await service.GetVersionsAsync("go"); + var node = await service.GetVersionsAsync("node"); + var dotnet = await service.GetVersionsAsync("dotnet"); + + Assert.AreEqual("1.23.4", go[0].Version); + Assert.AreEqual("go1.23.4.windows-amd64.msi", go[0].Installer?.FileName); + Assert.AreEqual("go1.23.4.src.tar.gz", go[0].SourceArchive?.FileName); + Assert.AreEqual("22.11.0", node[0].Version); + Assert.AreEqual("node-v22.11.0-x64.msi", node[0].Installer?.FileName); + Assert.IsTrue(node[0].AllCandidates.Any(candidate => candidate.SourceType == "MirrorCN")); + Assert.AreEqual("10.0.100", dotnet[0].Version); + Assert.AreEqual("dotnet-sdk-10.0.100-win-x64.exe", dotnet[0].Installer?.FileName); + } + + [TestMethod] + public async Task DevEnvironmentCatalogReadsGithubReleaseAssets() + { + var workspace = TempWorkspace(); + var service = new DevEnvironmentCatalogService( + AppPaths.ForCurrentUser(workspace), + new FakeHttpService(_ => """ + [ + { + "tag_name": "v3.31.0", + "published_at": "2024-11-01T00:00:00Z", + "html_url": "https://github.com/Kitware/CMake/releases/tag/v3.31.0", + "assets": [ + { + "name": "cmake-3.31.0-windows-x86_64.msi", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.31.0/cmake-3.31.0-windows-x86_64.msi", + "size": 2048 + }, + { + "name": "cmake-3.31.0.tar.gz", + "browser_download_url": "https://github.com/Kitware/CMake/releases/download/v3.31.0/cmake-3.31.0.tar.gz", + "size": 4096 + } + ] + } + ] + """)); + + var cmake = await service.GetVersionsAsync("cmake"); + + Assert.HasCount(1, cmake); + Assert.AreEqual("3.31.0", cmake[0].Version); + Assert.IsTrue(cmake[0].AllCandidates.Any(candidate => candidate.Mode == DevEnvironmentInstallMode.QuickInstall && candidate.SourceType == "GitHub")); + Assert.IsTrue(cmake[0].AllCandidates.Any(candidate => candidate.Mode == DevEnvironmentInstallMode.SourceBuild && candidate.Source.SizeBytes == 4096)); + } + + [TestMethod] + public async Task DownloadQueueStorePersistsAndRestoresItems() + { + var workspace = TempWorkspace(); + var store = new DownloadQueueStore(AppPaths.ForCurrentUser(workspace)); + var item = DownloadItem.Create( + new DownloadSource("https://example.com/tool.exe", "Tool", "tool.exe"), + Path.Combine(workspace, "tool.exe"), + "installer") + .WithResumeMetadata( + "\"abc\"", + "Wed, 10 Jun 2026 08:00:00 GMT", + "bytes", + 2048, + "https://cdn.example.com/tool.exe", + true) + .WithProgress(DownloadState.Completed, 1024, 2048, 0); + + await store.SaveAsync([item]); + var loaded = await store.LoadAsync(); + + Assert.HasCount(1, loaded); + Assert.AreEqual(item.Id, loaded[0].Id); + Assert.AreEqual(DownloadState.Completed, loaded[0].State); + Assert.AreEqual("tool.exe", loaded[0].Source.FileName); + Assert.AreEqual("installer", loaded[0].InstallCommand); + Assert.AreEqual(Path.Combine(workspace, "tool.exe.partial"), loaded[0].EffectivePartialPath); + Assert.AreEqual("\"abc\"", loaded[0].ETag); + Assert.AreEqual("Wed, 10 Jun 2026 08:00:00 GMT", loaded[0].LastModified); + Assert.AreEqual("bytes", loaded[0].AcceptRanges); + Assert.AreEqual(2048, loaded[0].ContentLength); + Assert.AreEqual("https://cdn.example.com/tool.exe", loaded[0].FinalUrl); + Assert.IsTrue(loaded[0].ResumeSupported); + } + + [TestMethod] + public async Task DownloadQueueStorePersistsSettings() + { + var workspace = TempWorkspace(); + var store = new DownloadQueueStore(AppPaths.ForCurrentUser(workspace)); + var directory = Path.Combine(workspace, "custom"); + + await store.SaveSettingsAsync(new DownloadSettings(directory, 99)); + var loaded = await store.LoadSettingsAsync(); + + Assert.AreEqual(directory, loaded.DefaultDirectory); + Assert.AreEqual(5, loaded.MaxConcurrentDownloads); + } + + [TestMethod] + public void DownloadProgressAndProtocolRoundTrip() + { + var progress = new DownloadProgressSnapshot( + "a", + DownloadState.Running, + 512, + 1024, + 2048, + ETag: "\"abc\"", + LastModified: "Wed, 10 Jun 2026 08:00:00 GMT", + AcceptRanges: "bytes", + ContentLength: 1024, + FinalUrl: "https://cdn.example.com/a.zip"); + var message = new DownloadHostMessage(DownloadHostProtocol.Progress, progress.Id, DownloadHostProtocol.Version, Progress: progress); + + var serialized = DownloadHostProtocol.Serialize(message); + var roundTrip = DownloadHostProtocol.Deserialize(serialized); + + Assert.AreEqual(0.5, progress.Progress, 0.001); + Assert.IsNotNull(roundTrip); + Assert.AreEqual(DownloadHostProtocol.Progress, roundTrip.Type); + Assert.AreEqual(DownloadState.Running, roundTrip.Progress?.State); + Assert.AreEqual(2048, roundTrip.Progress?.BytesPerSecond); + Assert.AreEqual("\"abc\"", roundTrip.Progress?.ETag); + Assert.AreEqual("bytes", roundTrip.Progress?.AcceptRanges); + Assert.AreEqual("https://cdn.example.com/a.zip", roundTrip.Progress?.FinalUrl); + } + + [TestMethod] + public async Task DirectDownloadValidatorParsesInternetShortcut() + { + using var validator = new DirectDownloadValidator(new HttpClient(new StubHttpHandler(request => + { + var content = new ByteArrayContent(Encoding.UTF8.GetBytes("package")); + content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/zip"); + return new HttpResponseMessage(HttpStatusCode.OK) + { + RequestMessage = request, + Content = content + }; + }))); + + var result = await validator.ValidateAsync(""" + [InternetShortcut] + URL=https://example.com/package.zip + """); + + Assert.IsTrue(result.IsDirectDownload); + Assert.IsTrue(result.WasInternetShortcut); + Assert.AreEqual("https://example.com/package.zip", result.EffectiveUrl); + } + + [TestMethod] + public async Task DirectDownloadValidatorRejectsHtmlDownloadPage() + { + using var validator = new DirectDownloadValidator(new HttpClient(new StubHttpHandler(request => + { + var content = new ByteArrayContent(Encoding.UTF8.GetBytes("<!doctype html><html></html>")); + content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("text/html"); + return new HttpResponseMessage(HttpStatusCode.OK) + { + RequestMessage = request, + Content = content + }; + }))); + + var result = await validator.ValidateAsync("https://example.com/download"); + + Assert.IsFalse(result.IsDirectDownload); + StringAssert.Contains(result.Message, "web page"); + } + + [TestMethod] + public async Task ReleaseManifestSerializerRoundTripsFullInstallerManifest() + { + var workspace = TempWorkspace(); + var path = Path.Combine(workspace, "manifest.json"); + var manifest = new ReleaseManifest( + "2.0.7.0", + "2.0.7.0", + "stable", + DateTimeOffset.Parse("2026-06-13T00:00:00Z"), + [ + new ReleaseFileEntry("downloads/YMhut_Box_WinUI_Setup_2.0.7.0.exe", 445_000_000, "installer"), + new ReleaseFileEntry("downloads/YMhut_Box_WinUI_2.0.7.0.msix", 571_000_000, "msix") + ], + [], + "https://update.ymhut.cn/downloads/", + "Full"); + + await ReleaseManifestSerializer.WriteManifestAsync(path, manifest); + var loaded = await ReleaseManifestSerializer.ReadManifestAsync(path); + + Assert.AreEqual("2.0.7.0", loaded.Version); + Assert.AreEqual("stable", loaded.Channel); + Assert.AreEqual("Full", loaded.Flavor); + Assert.HasCount(2, loaded.Files); + Assert.IsTrue(loaded.Files.All(file => + file.Path.EndsWith(".exe", StringComparison.OrdinalIgnoreCase) || + file.Path.EndsWith(".msix", StringComparison.OrdinalIgnoreCase))); + } + + [TestMethod] + public void UpdateInfoEndpointPolicyPrefersCanonicalUpdateInfo() + { + var endpoints = UpdateInfoEndpointPolicy.BuildDefaultUris("https://update.ymhut.cn/"); + + CollectionAssert.AreEqual( + new[] + { + "https://update.ymhut.cn/api/client/bootstrap", + "https://update.ymhut.cn/update-info.json", + "https://update.ymhut.cn/update-info", + "https://update.ymhut.cn/api/update-info" + }, + endpoints.Select(endpoint => endpoint.ToString()).ToArray()); + } + + [TestMethod] + public async Task HttpRedirectResolverFollowsMultiHopAndRelativeLocations() + { + using var client = new HttpClient(new StubHttpHandler(request => + { + return request.RequestUri!.AbsoluteUri switch + { + "https://media.example/start" => new HttpResponseMessage(HttpStatusCode.Redirect) + { + Headers = { Location = new Uri("/hop-1", UriKind.Relative) }, + RequestMessage = request + }, + "https://media.example/hop-1" => new HttpResponseMessage(HttpStatusCode.TemporaryRedirect) + { + Headers = { Location = new Uri("https://cdn.example/final.mp4") }, + RequestMessage = request + }, + "https://cdn.example/final.mp4" => new HttpResponseMessage(HttpStatusCode.OK) + { + RequestMessage = request, + Content = new ByteArrayContent(Array.Empty<byte>()) + }, + _ => new HttpResponseMessage(HttpStatusCode.NotFound) + { + RequestMessage = request, + Content = new StringContent("missing") + } + }; + })); + + var resolved = await HttpRedirectResolver.ResolveAsync(new Uri("https://media.example/start"), client); + + Assert.AreEqual("https://cdn.example/final.mp4", resolved.ToString()); + } + + private static string TempWorkspace() + { + var path = Path.Combine(Path.GetTempPath(), "ymhut-box-download-tests", Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(path); + return path; + } + + private sealed class FakeHttpService(Func<Uri, string> handler) : IHttpService + { + public Task<HttpServiceResult> GetAsync(Uri uri, CancellationToken cancellationToken = default) + { + return Task.FromResult(new HttpServiceResult(HttpStatusCode.OK, handler(uri), new Dictionary<string, string[]>(), TimeSpan.Zero)); + } + + public Task<string> GetStringAsync(Uri uri, CancellationToken cancellationToken = default) + { + return Task.FromResult(handler(uri)); + } + + public Task<HttpServiceResult> SendAsync( + Uri uri, + string method = "GET", + string? body = null, + IReadOnlyDictionary<string, string>? headers = null, + bool ensureSuccess = true, + HttpRequestPolicy? policy = null, + CancellationToken cancellationToken = default) + { + return GetAsync(uri, cancellationToken); + } + } + + private sealed class StubHttpHandler(Func<HttpRequestMessage, HttpResponseMessage> handler) : HttpMessageHandler + { + protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + return Task.FromResult(handler(request)); + } + } +} diff --git a/src/YMhut.Box.Tests/FeedbackServiceTests.cs b/src/YMhut.Box.Tests/FeedbackServiceTests.cs new file mode 100644 index 0000000..3362a71 --- /dev/null +++ b/src/YMhut.Box.Tests/FeedbackServiceTests.cs @@ -0,0 +1,409 @@ +using System.IO.Compression; +using System.Reflection; +using System.Text.Json; +using System.Text.RegularExpressions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using YMhut.Box.Core.App; +using YMhut.Box.Core.Feedback; +using YMhut.Box.Core.Logging; +using YMhut.Box.Core.System; +using YMhut.Box.Core.Tools; + +namespace YMhut.Box.Tests; + +[TestClass] +public sealed class FeedbackServiceTests +{ + [TestMethod] + public void FeedbackCodeCreatesServerCompatibleCode() + { + var code = FeedbackCode.Create(DateTimeOffset.Parse("2026-06-04T10:00:00+08:00")); + + Assert.IsTrue(Regex.IsMatch(code, "^FB-20260604-[A-F0-9]{6}$")); + Assert.AreEqual("FB-20260604-ABC123", FeedbackCode.Normalize(" fb-20260604-abc123 ")); + Assert.IsTrue(FeedbackCode.IsValid("fb-20260604-abc123")); + Assert.IsFalse(FeedbackCode.IsValid("FB-20260604-XYZ123")); + } + + [TestMethod] + public async Task FeedbackPackageIncludesSelectedAttachmentsAndLocalizedLogs() + { + var tempRoot = Path.Combine(Path.GetTempPath(), "ymhut-box-feedback-tests", Guid.NewGuid().ToString("N")); + var paths = AppPaths.ForCurrentUser(tempRoot); + var logs = new JsonFileLogService(paths); + var service = new FeedbackPackageService(logs, new FakeSystemMetricsService(), new ToolCatalog()); + + await logs.WriteAsync("Information", "navigation", "Open toolbox", "https://example.com/private?token=secret"); + + var package = await service.BuildPackageAsync(new FeedbackRequest( + "同步状态卡住", + "issue", + "major", + "dev@example.com", + "首页一直显示后台校验中。", + IncludeTodayLogs: true, + IncludeToolStatus: true, + IncludeSystemSummary: true, + Language: "zh-CN", + FeedbackCode: "FB-20260604-ABC123")); + + try + { + Assert.IsTrue(File.Exists(package.PackagePath)); + Assert.IsGreaterThan(0, package.PackageBytes); + Assert.AreEqual( + Path.GetFullPath(FeedbackPackageService.FeedbackPackagesRoot()), + Path.GetFullPath(Path.GetDirectoryName(package.PackagePath)!)); + Assert.IsTrue(package.IncludedFiles.ContainsKey("feedback.json")); + Assert.IsTrue(package.IncludedFiles.ContainsKey("tool-status.json")); + Assert.IsTrue(package.IncludedFiles.ContainsKey("system-summary.json")); + Assert.IsTrue(package.IncludedFiles.Keys.Any(name => name.StartsWith("logs-", StringComparison.OrdinalIgnoreCase))); + Assert.IsTrue(package.SummaryText.Contains("首页一直显示后台校验中。", StringComparison.Ordinal)); + Assert.IsTrue(package.SummaryText.Contains("FB-20260604-ABC123", StringComparison.Ordinal)); + + using var archive = ZipFile.OpenRead(package.PackagePath); + Assert.IsNotNull(archive.GetEntry("feedback.json")); + Assert.IsNotNull(archive.GetEntry("summary.txt")); + var feedbackJson = ReadEntry(archive.GetEntry("feedback.json")!); + using var feedbackDocument = JsonDocument.Parse(feedbackJson); + Assert.AreEqual("FB-20260604-ABC123", feedbackDocument.RootElement.GetProperty("feedbackCode").GetString()); + Assert.AreEqual("FB-20260604-ABC123", feedbackDocument.RootElement.GetProperty("request").GetProperty("feedbackCode").GetString()); + + var logsEntry = archive.Entries.Single(entry => entry.FullName.StartsWith("logs-", StringComparison.OrdinalIgnoreCase)); + var logsJson = ReadEntry(logsEntry); + using var logsDocument = JsonDocument.Parse(logsJson); + var firstLog = logsDocument.RootElement[0]; + Assert.AreEqual("导航", firstLog.GetProperty("display").GetProperty("category").GetString()); + Assert.AreEqual("打开工具箱", firstLog.GetProperty("display").GetProperty("message").GetString()); + Assert.AreEqual("远程服务", firstLog.GetProperty("display").GetProperty("detail").GetString()); + Assert.AreEqual("远程服务", firstLog.GetProperty("raw").GetProperty("detail").GetString()); + Assert.IsFalse(logsJson.Contains("example.com/private", StringComparison.OrdinalIgnoreCase)); + + var toolStatus = ReadEntry(archive.GetEntry("tool-status.json")!); + StringAssert.Contains(toolStatus, "\"id\": \"compression_codec\""); + StringAssert.Contains(toolStatus, "\"exists\": true"); + } + finally + { + DeleteIfExists(package.PackagePath); + Directory.Delete(tempRoot, recursive: true); + } + } + + [TestMethod] + public async Task FeedbackPackageCryptoRoundTripsLocalZip() + { + var tempRoot = Path.Combine(Path.GetTempPath(), "ymhut-box-feedback-tests", Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(tempRoot); + var sourceZip = Path.Combine(tempRoot, "feedback.zip"); + var decryptedZip = Path.Combine(tempRoot, "feedback-decrypted.zip"); + + try + { + using (var archive = ZipFile.Open(sourceZip, ZipArchiveMode.Create)) + { + var entry = archive.CreateEntry("summary.txt"); + await using var stream = entry.Open(); + await using var writer = new StreamWriter(stream); + await writer.WriteAsync("反馈包加密测试"); + } + + var encrypted = await FeedbackPackageCrypto.EncryptPackageAsync(sourceZip); + Assert.IsTrue(File.Exists(encrypted.PackagePath)); + Assert.AreEqual(".ymfb", Path.GetExtension(encrypted.PackagePath)); + Assert.IsGreaterThan(new FileInfo(sourceZip).Length, encrypted.PackageBytes); + + await FeedbackPackageCrypto.DecryptPackageAsync(encrypted.PackagePath, decryptedZip); + + CollectionAssert.AreEqual( + await File.ReadAllBytesAsync(sourceZip), + await File.ReadAllBytesAsync(decryptedZip)); + } + finally + { + if (Directory.Exists(tempRoot)) + { + Directory.Delete(tempRoot, recursive: true); + } + } + } + + [TestMethod] + public async Task FeedbackPackageRejectsScreenshotOverFiveMegabytes() + { + var tempRoot = Path.Combine(Path.GetTempPath(), "ymhut-box-feedback-tests", Guid.NewGuid().ToString("N")); + var paths = AppPaths.ForCurrentUser(tempRoot); + var logs = new JsonFileLogService(paths); + var service = new FeedbackPackageService(logs, new FakeSystemMetricsService(), new ToolCatalog()); + var screenshot = Path.Combine(tempRoot, "large.png"); + await File.WriteAllBytesAsync(screenshot, new byte[FeedbackPackageService.MaxScreenshotBytes + 1]); + + try + { + await Assert.ThrowsExactlyAsync<InvalidOperationException>(() => + service.BuildPackageAsync(new FeedbackRequest( + "截图过大", + "issue", + "normal", + string.Empty, + "测试截图限制。", + IncludeTodayLogs: false, + IncludeToolStatus: false, + IncludeSystemSummary: false, + ScreenshotPath: screenshot))); + } + finally + { + Directory.Delete(tempRoot, recursive: true); + } + } + + [TestMethod] + public void FeedbackSubmissionSignatureIsStable() + { + var signature = FeedbackSubmissionService.Sign("1760000000", "abc123", new string('a', 64), "{\"ok\":true}"); + + Assert.AreEqual(64, signature.Length); + Assert.IsTrue(signature.All(character => Uri.IsHexDigit(character) && !char.IsUpper(character))); + Assert.AreEqual(signature, FeedbackSubmissionService.Sign("1760000000", "abc123", new string('a', 64), "{\"ok\":true}")); + } + + [TestMethod] + public void FeedbackStatusResponseParsesExtendedServerFields() + { + var response = ParseStatusResponse(""" + { + "ok": true, + "code": "FB-20260604-ABC123", + "status": "investigating", + "statusLabel": "处理中", + "statusDetail": "已收到日志,正在定位。", + "category": "issue", + "priority": "major", + "hasReply": true, + "reply": "请先保留现场文件。", + "receivedAt": "2026-06-04T10:00:00+08:00", + "updatedAt": "2026-06-04T10:30:00+08:00", + "mailSent": true + } + """); + + Assert.IsTrue(response.Ok); + Assert.AreEqual("FB-20260604-ABC123", response.Code); + Assert.AreEqual("investigating", response.Status); + Assert.AreEqual("处理中", response.StatusLabel); + Assert.AreEqual("已收到日志,正在定位。", response.StatusDetail); + Assert.AreEqual("issue", response.Category); + Assert.AreEqual("major", response.Priority); + Assert.IsTrue(response.HasReply); + Assert.AreEqual("请先保留现场文件。", response.Reply); + Assert.AreEqual("2026-06-04T10:30:00+08:00", response.UpdatedAt); + Assert.IsTrue(response.MailSent); + } + + [TestMethod] + public async Task FeedbackRecordStoreReadsOldRecordsWithoutExtendedFields() + { + var tempRoot = Path.Combine(Path.GetTempPath(), "ymhut-box-feedback-record-tests", Guid.NewGuid().ToString("N")); + var storagePath = Path.Combine(tempRoot, "feedback-records.json"); + Directory.CreateDirectory(tempRoot); + await File.WriteAllTextAsync(storagePath, """ + [ + { + "code": "FB-20260604-ABC123", + "title": "Old record", + "type": "issue", + "severity": "normal", + "createdAtUtc": "2026-06-04T02:00:00+00:00", + "updatedAtUtc": "2026-06-04T02:10:00+00:00", + "state": "Sent", + "statusLabel": "Sent", + "lastMessage": "The service received it." + } + ] + """); + + try + { + var record = await new FeedbackRecordStore(storagePath).FindAsync("fb-20260604-abc123"); + + Assert.IsNotNull(record); + Assert.AreEqual("FB-20260604-ABC123", record.Code); + Assert.AreEqual("Sent", record.StatusLabel); + Assert.IsNull(record.ServiceStatusDetail); + Assert.IsNull(record.ServiceCategory); + Assert.IsNull(record.ServicePriority); + Assert.IsNull(record.ServiceMailSent); + } + finally + { + if (Directory.Exists(tempRoot)) + { + Directory.Delete(tempRoot, recursive: true); + } + } + } + + [TestMethod] + public async Task FeedbackRecordStoreKeepsStatusMessageAndPublicReplySeparate() + { + var tempRoot = Path.Combine(Path.GetTempPath(), "ymhut-box-feedback-record-tests", Guid.NewGuid().ToString("N")); + var storagePath = Path.Combine(tempRoot, "feedback-records.json"); + var store = new FeedbackRecordStore(storagePath); + var now = DateTimeOffset.Parse("2026-06-04T03:00:00Z"); + + try + { + await store.UpsertAsync(new FeedbackRecord( + "FB-20260604-ABC123", + "Status query", + "issue", + "major", + now, + now, + FeedbackRecordState.StatusUpdated, + "处理中", + "已收到日志,正在定位。", + LastQueryAtUtc: now, + ServiceStatus: "investigating", + PublicReply: "请先保留现场文件。", + ServiceStatusDetail: "已收到日志,正在定位。", + ServiceCategory: "issue", + ServicePriority: "major", + ServiceMailSent: true, + ServiceReceivedAt: "2026-06-04T10:00:00+08:00", + ServiceUpdatedAt: "2026-06-04T10:30:00+08:00")); + + var record = await store.FindAsync("FB-20260604-ABC123"); + + Assert.IsNotNull(record); + Assert.AreEqual("已收到日志,正在定位。", record.LastMessage); + Assert.IsFalse(record.LastMessage.Contains("请先保留现场文件。", StringComparison.Ordinal)); + Assert.AreEqual("请先保留现场文件。", record.PublicReply); + Assert.AreEqual("investigating", record.ServiceStatus); + Assert.AreEqual("issue", record.ServiceCategory); + Assert.AreEqual("major", record.ServicePriority); + Assert.IsTrue(record.ServiceMailSent); + Assert.AreEqual("2026-06-04T10:30:00+08:00", record.ServiceUpdatedAt); + } + finally + { + if (Directory.Exists(tempRoot)) + { + Directory.Delete(tempRoot, recursive: true); + } + } + } + + [TestMethod] + public async Task FeedbackRecordStoreArchivesOldRecordsAndKeepsThemQueryable() + { + var tempRoot = Path.Combine(Path.GetTempPath(), "ymhut-box-feedback-record-tests", Guid.NewGuid().ToString("N")); + var storagePath = Path.Combine(tempRoot, "feedback-records.json"); + var store = new FeedbackRecordStore(storagePath); + var now = DateTimeOffset.Parse("2026-06-05T12:00:00Z"); + var oldCreated = now.AddDays(-11); + + try + { + await store.UpsertAsync(new FeedbackRecord( + "FB-20260525-ABC123", + "旧反馈", + "issue", + "normal", + oldCreated, + oldCreated, + FeedbackRecordState.Sent, + "发送成功", + "服务端已接收。", + PackagePath: Path.Combine(tempRoot, "feedback.zip"), + PackageSha256: new string('a', 64), + PackageBytes: 128, + SubmittedAtUtc: oldCreated)); + + var archived = await store.ArchiveOlderThanAsync(TimeSpan.FromDays(10), now); + var record = archived.Single(); + Assert.IsTrue(record.IsArchived); + Assert.AreEqual(FeedbackRecordState.Archived, record.State); + Assert.AreEqual("已归档", record.StatusLabel); + Assert.IsNotNull(record.ArchivedAtUtc); + + var updated = await store.UpsertAsync(record with + { + State = FeedbackRecordState.StatusUpdated, + StatusLabel = "状态已更新", + LastMessage = "当前状态:已处理。", + UpdatedAtUtc = now.AddMinutes(5), + LastQueryAtUtc = now.AddMinutes(5) + }); + + Assert.IsTrue(updated.IsArchived); + Assert.AreEqual(FeedbackRecordState.StatusUpdated, updated.State); + Assert.AreEqual("状态已更新", updated.StatusLabel); + Assert.AreEqual("当前状态:已处理。", updated.LastMessage); + + var reloaded = await new FeedbackRecordStore(storagePath).FindAsync("fb-20260525-abc123"); + Assert.IsNotNull(reloaded); + Assert.IsTrue(reloaded.IsArchived); + Assert.AreEqual("FB-20260525-ABC123", reloaded.Code); + } + finally + { + if (Directory.Exists(tempRoot)) + { + Directory.Delete(tempRoot, recursive: true); + } + } + } + + private static string ReadEntry(ZipArchiveEntry entry) + { + using var stream = entry.Open(); + using var reader = new StreamReader(stream); + return reader.ReadToEnd(); + } + + private static FeedbackStatusResponse ParseStatusResponse(string json) + { + var method = typeof(FeedbackSubmissionService).GetMethod("ParseStatusResponse", BindingFlags.NonPublic | BindingFlags.Static); + Assert.IsNotNull(method); + return (FeedbackStatusResponse)method.Invoke(null, new object?[] { json })!; + } + + private static void DeleteIfExists(string path) + { + if (File.Exists(path)) + { + File.Delete(path); + } + } + + private sealed class FakeSystemMetricsService : ISystemMetricsService + { + public SystemMetricsSnapshot Capture() + { + return new SystemMetricsSnapshot( + "TEST-PC", + "Windows Test", + 8, + 1024, + 2048, + 42.5, + 16UL * 1024 * 1024 * 1024, + 8UL * 1024 * 1024 * 1024, + 8UL * 1024 * 1024 * 1024, + 50, + 1.2, + TimeSpan.FromMinutes(12), + 24, + 512, + 256UL * 1024 * 1024 * 1024, + 128UL * 1024 * 1024 * 1024, + 128UL * 1024 * 1024 * 1024, + 50, + true, + ".NET 10.0", + DateTimeOffset.Parse("2026-06-04T10:00:00+08:00")); + } + } +} diff --git a/src/YMhut.Box.Tests/InstallLayoutPathsTests.cs b/src/YMhut.Box.Tests/InstallLayoutPathsTests.cs new file mode 100644 index 0000000..6fd174b --- /dev/null +++ b/src/YMhut.Box.Tests/InstallLayoutPathsTests.cs @@ -0,0 +1,266 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using YMhut.Box.Core.App; + +namespace YMhut.Box.Tests; + +[TestClass] +[DoNotParallelize] +public sealed class InstallLayoutPathsTests +{ + [TestMethod] + public void ResolveInstallRootPrefersCurrentInstallRootEnvironment() + { + using var workspace = TestWorkspace.Create("ymhut-install-root-tests"); + var installRoot = workspace.CreateDirectory("install"); + var baseRoot = workspace.CreateDirectory("runtime"); + + using var environment = EnvironmentScope.Capture( + InstallLayoutPaths.InstallRootEnvironmentVariable, + InstallLayoutPaths.ArchivedLayoutEnvironmentVariable); + environment.Set(InstallLayoutPaths.InstallRootEnvironmentVariable, installRoot); + environment.Set(InstallLayoutPaths.ArchivedLayoutEnvironmentVariable, null); + + Assert.AreEqual(installRoot, InstallLayoutPaths.ResolveInstallRoot(baseRoot)); + Assert.AreEqual(installRoot, InstallLayoutPaths.CandidateRoots(baseRoot).First()); + } + + [TestMethod] + public void ResolveInstallRootIgnoresLegacyArchivedLayoutForHealth() + { + using var workspace = TestWorkspace.Create("ymhut-archived-layout-tests"); + var archivedRoot = workspace.CreateDirectory("archived"); + var baseRoot = workspace.CreateDirectory("runtime"); + workspace.CreateFile(["runtime", "YMhutBox.exe"], string.Empty); + + using var environment = EnvironmentScope.Capture( + InstallLayoutPaths.InstallRootEnvironmentVariable, + InstallLayoutPaths.ArchivedLayoutEnvironmentVariable); + environment.Set(InstallLayoutPaths.InstallRootEnvironmentVariable, null); + environment.Set(InstallLayoutPaths.ArchivedLayoutEnvironmentVariable, archivedRoot); + + Assert.AreEqual(baseRoot, InstallLayoutPaths.ResolveInstallRoot(baseRoot)); + Assert.IsFalse(InstallLayoutPaths.CandidateRoots(baseRoot).Contains(archivedRoot)); + Assert.AreEqual(archivedRoot, InstallLayoutPaths.ResolveArchivedLayoutRoot()); + } + + [TestMethod] + public void ResolveAssetsRootAndExecutableUseInstallRoot() + { + using var workspace = TestWorkspace.Create("ymhut-install-assets-tests"); + var installRoot = workspace.CreateDirectory("install"); + var assetsRoot = workspace.CreateDirectory("install", "Assets"); + var executable = workspace.CreateFile(["install", "YMhutBox.exe"], string.Empty); + var runtimeRoot = workspace.CreateDirectory("runtime"); + + using var environment = EnvironmentScope.Capture( + InstallLayoutPaths.InstallRootEnvironmentVariable, + InstallLayoutPaths.ArchivedLayoutEnvironmentVariable); + environment.Set(InstallLayoutPaths.InstallRootEnvironmentVariable, installRoot); + environment.Set(InstallLayoutPaths.ArchivedLayoutEnvironmentVariable, null); + + Assert.AreEqual(assetsRoot, InstallLayoutPaths.ResolveAssetsRoot(runtimeRoot)); + Assert.AreEqual(executable, InstallLayoutPaths.ResolveInstalledExecutablePath(runtimeRoot)); + } + + [TestMethod] + public void RuntimeLayoutPolicySkipsLargeToolsAndUserDataRoots() + { + foreach (var relativePath in new[] + { + "Tools/vendor/tool.exe", + "Metadata/tools.json", + "data/settings.json", + "FeedbackPackages/feedback.zip" + }) + { + Assert.IsTrue(RuntimeLayoutPolicy.ShouldSkipRelativePath(relativePath), relativePath); + Assert.IsFalse(RuntimeLayoutPolicy.ShouldCopyFile(relativePath), relativePath); + } + + Assert.IsFalse(RuntimeLayoutPolicy.ShouldCopyFile("unins000.exe")); + Assert.IsFalse(RuntimeLayoutPolicy.ShouldCopyFile("YMhutBox.exe")); + Assert.IsFalse(RuntimeLayoutPolicy.ShouldCopyFile("YMhutBox.dll")); + Assert.IsFalse(RuntimeLayoutPolicy.ShouldCopyFile("resources.pri")); + Assert.IsFalse(RuntimeLayoutPolicy.ShouldCopyFile("zh-CN/Microsoft.ui.xaml.dll.mui")); + Assert.IsFalse(RuntimeLayoutPolicy.ShouldCopyFile("lang/zh-CN/Microsoft.ui.xaml.dll.mui")); + Assert.IsFalse(RuntimeLayoutPolicy.ShouldCopyFile("runtimes/win-x64/native/Microsoft.ui.xaml.dll")); + } + + [TestMethod] + public void InstallManifestParserReadsReleaseRequiredFilesAndPayloadFiles() + { + var manifest = InstallManifest.Parse( + """ + [Release] + Version=2.0.6 + Build=2 + Channel=stable + PackageVersion=2.0.6.2 + + [RequiredFiles] + YMhutBox.exe + YMhutBox.dll + + [Files] + YMhutBox.exe + lang/zh-CN/Microsoft.ui.xaml.dll.mui + Tools/SampleTool/tool.exe + """); + + Assert.IsNotNull(manifest.Release); + Assert.AreEqual("2.0.6", manifest.Release.Version); + Assert.AreEqual("2", manifest.Release.Build); + Assert.AreEqual("stable", manifest.Release.Channel); + Assert.AreEqual("2.0.6.2", manifest.Release.PackageVersion); + Assert.IsTrue(manifest.RequiredFiles.SequenceEqual(["YMhutBox.exe", "YMhutBox.dll"])); + Assert.IsTrue(manifest.ContainsFile(@"lang\zh-CN\Microsoft.ui.xaml.dll.mui")); + Assert.IsTrue(manifest.ContainsFile("Tools/SampleTool/tool.exe")); + } + + [TestMethod] + public void RuntimeLayoutPolicySkipsRuntimePayloadRoots() + { + foreach (var relativePath in new[] + { + "lang/en-US/Microsoft.UI.Xaml.Phone.dll.mui", + "Microsoft.UI.Xaml/Microsoft.UI.Xaml.dll", + "runtimes/win-x64/native/WebView2Loader.dll", + "worker/YMhut.Box.Worker.exe" + }) + { + Assert.IsFalse(RuntimeLayoutPolicy.ShouldCopyFile(relativePath), relativePath); + } + } + + [TestMethod] + public void SourceFilesDoNotContainCommonMojibakeSequences() + { + var root = FindRepositoryRoot(); + var scanRoots = new[] + { + Path.Combine(root, "src", "YMhut.Box.Core"), + Path.Combine(root, "src", "box-winUI"), + Path.Combine(root, "installer") + }; + var extensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { ".cs", ".xaml", ".iss" }; + var mojibakeMarkers = new[] + { + "Ã", + "Â", + "â", + "鈥", + "锛", + "銆", + "鐑", + "澶", + "璁", + "鏍", + "棰", + "缃" + }; + + var offenders = scanRoots + .Where(Directory.Exists) + .SelectMany(scanRoot => Directory.EnumerateFiles(scanRoot, "*.*", SearchOption.AllDirectories)) + .Where(path => extensions.Contains(Path.GetExtension(path))) + .Where(path => !path.Contains($"{Path.DirectorySeparatorChar}bin{Path.DirectorySeparatorChar}", StringComparison.OrdinalIgnoreCase)) + .Where(path => !path.Contains($"{Path.DirectorySeparatorChar}obj{Path.DirectorySeparatorChar}", StringComparison.OrdinalIgnoreCase)) + .SelectMany(path => FindMojibakeMarkers(path, mojibakeMarkers)) + .Take(12) + .ToArray(); + + Assert.IsEmpty(offenders, string.Join(Environment.NewLine, offenders)); + } + + private static IEnumerable<string> FindMojibakeMarkers(string path, IReadOnlyList<string> markers) + { + var text = File.ReadAllText(path); + foreach (var marker in markers) + { + if (text.Contains(marker, StringComparison.Ordinal)) + { + yield return $"{Path.GetFileName(path)} contains '{marker}'"; + } + } + } + + private static string FindRepositoryRoot() + { + var directory = new DirectoryInfo(AppContext.BaseDirectory); + while (directory is not null) + { + if (Directory.Exists(Path.Combine(directory.FullName, "src")) && + Directory.Exists(Path.Combine(directory.FullName, "installer"))) + { + return directory.FullName; + } + + directory = directory.Parent; + } + + Assert.Fail("Repository root could not be found from test output directory."); + return AppContext.BaseDirectory; + } + + private sealed class EnvironmentScope : IDisposable + { + private readonly Dictionary<string, string?> _snapshot; + + private EnvironmentScope(IEnumerable<string> names) + { + _snapshot = names.ToDictionary(name => name, Environment.GetEnvironmentVariable, StringComparer.Ordinal); + } + + public static EnvironmentScope Capture(params string[] names) => new(names); + + public void Set(string name, string? value) => Environment.SetEnvironmentVariable(name, value); + + public void Dispose() + { + foreach (var item in _snapshot) + { + Environment.SetEnvironmentVariable(item.Key, item.Value); + } + } + } + + private sealed class TestWorkspace : IDisposable + { + private TestWorkspace(string root) + { + Root = root; + } + + public string Root { get; } + + public static TestWorkspace Create(string prefix) + { + var root = Path.Combine(Path.GetTempPath(), prefix, Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(root); + return new TestWorkspace(root); + } + + public string CreateDirectory(params string[] parts) + { + var directory = Path.Combine([Root, .. parts]); + Directory.CreateDirectory(directory); + return directory; + } + + public string CreateFile(string[] parts, string contents) + { + var path = Path.Combine([Root, .. parts]); + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + File.WriteAllText(path, contents); + return path; + } + + public void Dispose() + { + if (Directory.Exists(Root)) + { + Directory.Delete(Root, recursive: true); + } + } + } +} diff --git a/src/YMhut.Box.Tests/LogServiceTests.cs b/src/YMhut.Box.Tests/LogServiceTests.cs new file mode 100644 index 0000000..65b1bc0 --- /dev/null +++ b/src/YMhut.Box.Tests/LogServiceTests.cs @@ -0,0 +1,131 @@ +using Microsoft.Data.Sqlite; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using YMhut.Box.Core.App; +using YMhut.Box.Core.Logging; + +namespace YMhut.Box.Tests; + +[TestClass] +public sealed class LogServiceTests +{ + [TestMethod] + public void LogDisplayLocalizerUsesEmptyDetailTextInsteadOfUnknownError() + { + Assert.AreEqual("暂无详情", LogDisplayLocalizer.Detail(null, "zh-CN")); + Assert.AreEqual("No details", LogDisplayLocalizer.Detail(" ", "en-US")); + Assert.AreEqual("真实详情", LogDisplayLocalizer.Detail("真实详情", "zh-CN")); + } + + [TestMethod] + public void LogDisplayLocalizerTranslatesCommonEnglishMessagesInChineseMode() + { + Assert.AreEqual("打开启动自检结果", LogDisplayLocalizer.Message("Open startup check results", "zh-CN")); + Assert.AreEqual("工具目录已重建", LogDisplayLocalizer.Message("Tool catalog rebuilt", "zh-CN")); + StringAssert.Contains(LogDisplayLocalizer.Message("External tool success: launch", "zh-CN"), "外部工具成功"); + Assert.AreEqual("外部工具成功", LogDisplayLocalizer.Message("External tool success", "zh-CN")); + StringAssert.Contains(LogDisplayLocalizer.Message("Tool result canceled: export", "zh-CN"), "工具结果已取消"); + Assert.AreEqual("工具工作进程已就绪", LogDisplayLocalizer.Message("Tool worker ready", "zh-CN")); + Assert.AreEqual("下载宿主进程已启动", LogDisplayLocalizer.Message("Download host process started", "zh-CN")); + Assert.AreEqual("打开插件页面:sample/main", LogDisplayLocalizer.Message("Open plugin surface: sample/main", "zh-CN")); + Assert.AreEqual("打开 JSON formatter", LogDisplayLocalizer.Message("Open JSON formatter", "zh-CN")); + Assert.AreEqual("\u66f4\u65b0\u901a\u77e5\u68c0\u67e5\u5931\u8d25", LogDisplayLocalizer.Message("Update notice check failed", "zh-CN")); + Assert.AreEqual("\u542f\u52a8\u81ea\u68c0\u7ed3\u679c\u9884\u52a0\u8f7d\u5931\u8d25", LogDisplayLocalizer.Message("Startup check result preload failed", "zh-CN")); + Assert.AreEqual("\u8bbe\u7f6e\u9875\u52a0\u8f7d\u5931\u8d25", LogDisplayLocalizer.Message("Settings page load failed", "zh-CN")); + Assert.AreEqual("\u5df2\u8bb0\u4f4f\u5173\u95ed\u786e\u8ba4\u9009\u62e9", LogDisplayLocalizer.Message("Close confirmation remembered", "zh-CN")); + } + + [TestMethod] + public void LogDisplayLocalizerTranslatesCommonDetailKeysInChineseMode() + { + var detail = LogDisplayLocalizer.Detail("result=success; operation=launch; elapsedMs=12; root=C:\\Tools; count=8", "zh-CN"); + + StringAssert.Contains(detail, "结果=成功"); + StringAssert.Contains(detail, "操作=启动"); + StringAssert.Contains(detail, "耗时毫秒=12"); + StringAssert.Contains(detail, "根目录=C:\\Tools"); + StringAssert.Contains(detail, "数量=8"); + + var windowDetail = LogDisplayLocalizer.Detail("source=tray; behavior=minimize_to_tray; result=primary; dialog=com_exception", "zh-CN"); + + StringAssert.Contains(windowDetail, "来源=托盘"); + StringAssert.Contains(windowDetail, "行为=最小化到托盘"); + StringAssert.Contains(windowDetail, "结果=主按钮"); + StringAssert.Contains(windowDetail, "弹窗=COM 异常"); + } + + [TestMethod] + public async Task ClearFilteredTodayDeletesOnlyMatchingRows() + { + var tempRoot = Path.Combine(Path.GetTempPath(), "ymhut-box-tests", Guid.NewGuid().ToString("N")); + try + { + var service = new SqliteLogService(AppPaths.ForCurrentUser(tempRoot)); + await service.WriteAsync("Information", "tool", "Open tool: JSON formatter"); + await service.WriteAsync("Warning", "network", "Proxy diagnostic failed"); + + await service.ClearFilteredTodayAsync("Information", "JSON"); + + var entries = await service.ReadAsync(take: 10); + Assert.HasCount(1, entries); + Assert.AreEqual("network", entries[0].Category); + } + finally + { + SqliteConnection.ClearAllPools(); + Directory.Delete(tempRoot, recursive: true); + } + } + + [TestMethod] + public async Task ReadByDateReturnsAllMatchingRowsForSelectedDay() + { + var tempRoot = Path.Combine(Path.GetTempPath(), "ymhut-box-tests", Guid.NewGuid().ToString("N")); + try + { + var service = new SqliteLogService(AppPaths.ForCurrentUser(tempRoot)); + await service.WriteAsync("Information", "tool", "Open JSON formatter"); + await service.WriteAsync("Information", "tool", "Open URL codec"); + await service.WriteAsync("Warning", "network", "Proxy diagnostic failed"); + + var allToday = await service.ReadByDateAsync(DateOnly.FromDateTime(DateTime.Now)); + var filtered = await service.ReadByDateAsync(DateOnly.FromDateTime(DateTime.Now), "Information", "Open"); + + Assert.HasCount(3, allToday); + Assert.HasCount(2, filtered); + } + finally + { + SqliteConnection.ClearAllPools(); + Directory.Delete(tempRoot, recursive: true); + } + } + + [TestMethod] + public async Task ReadByDatePageReturnsStablePages() + { + var tempRoot = Path.Combine(Path.GetTempPath(), "ymhut-box-tests", Guid.NewGuid().ToString("N")); + try + { + var service = new SqliteLogService(AppPaths.ForCurrentUser(tempRoot)); + for (var index = 0; index < 25; index++) + { + await service.WriteAsync("Information", "stream", $"Row {index:00}"); + } + + var today = DateOnly.FromDateTime(DateTime.Now); + var first = await service.ReadByDatePageAsync(today, "Information", "Row", skip: 0, take: 20); + var second = await service.ReadByDatePageAsync(today, "Information", "Row", skip: 20, take: 20); + + Assert.HasCount(20, first); + Assert.HasCount(5, second); + CollectionAssert.AreNotEquivalent( + first.Select(entry => entry.Message).ToArray(), + second.Select(entry => entry.Message).ToArray()); + } + finally + { + SqliteConnection.ClearAllPools(); + Directory.Delete(tempRoot, recursive: true); + } + } +} diff --git a/src/YMhut.Box.Tests/MediaVolumeModelTests.cs b/src/YMhut.Box.Tests/MediaVolumeModelTests.cs new file mode 100644 index 0000000..5f910ea --- /dev/null +++ b/src/YMhut.Box.Tests/MediaVolumeModelTests.cs @@ -0,0 +1,39 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using YMhut.Box.Core.Media; + +namespace YMhut.Box.Tests; + +[TestClass] +public sealed class MediaVolumeModelTests +{ + [TestMethod] + public void FromPercentClampsAndMapsOutputModes() + { + var silent = MediaVolumeModel.FromPercent(-5); + Assert.AreEqual(0, silent.Percent); + Assert.AreEqual(0, silent.PlatformVolume, 0.001); + Assert.AreEqual(0, silent.AudioGain, 0.001); + Assert.AreEqual(MediaVolumeOutputMode.Silent, silent.Mode); + + var normal = MediaVolumeModel.FromPercent(80); + Assert.AreEqual(80, normal.Percent); + Assert.AreEqual(0.8, normal.PlatformVolume, 0.001); + Assert.AreEqual(0.8, normal.AudioGain, 0.001); + Assert.AreEqual(MediaVolumeOutputMode.Normal, normal.Mode); + + var boosted = MediaVolumeModel.FromPercent(120); + Assert.AreEqual(120, boosted.Percent); + Assert.AreEqual(1.0, boosted.PlatformVolume, 0.001); + Assert.AreEqual(1.2, boosted.AudioGain, 0.001); + Assert.AreEqual(MediaVolumeOutputMode.Boosted, boosted.Mode); + } + + [TestMethod] + public void FormatPercentNamesMutedAndBoostedStates() + { + Assert.AreEqual("0%(静音)", MediaVolumeModel.FormatPercent(0, "zh-CN")); + Assert.AreEqual("120%(增强音量)", MediaVolumeModel.FormatPercent(120, "zh-CN")); + Assert.AreEqual("0% (Muted)", MediaVolumeModel.FormatPercent(0, "en-US")); + Assert.AreEqual("120% (Boosted)", MediaVolumeModel.FormatPercent(120, "en-US")); + } +} diff --git a/src/YMhut.Box.Tests/PluginTests.cs b/src/YMhut.Box.Tests/PluginTests.cs new file mode 100644 index 0000000..e135c1f --- /dev/null +++ b/src/YMhut.Box.Tests/PluginTests.cs @@ -0,0 +1,359 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.Data.Sqlite; +using YMhut.Box.Core.App; +using YMhut.Box.Core.Plugins; +using YMhut.Box.Core.Settings; +using YMhut.Box.Core.Tools; + +namespace YMhut.Box.Tests; + +[TestClass] +public sealed class PluginTests +{ + [TestMethod] + public async Task PluginRegistryLoadsValidManifestAndToolModule() + { + using var workspace = TempWorkspace(); + var pluginRoot = CreatePlugin(workspace.Path, "hello-tools", """ +{ + "id": "hello-tools", + "name": "Hello Tools", + "version": "1.0.0", + "author": "tester", + "description": "Test plugin", + "entry": "index.html", + "permissions": ["Input", "Output", "Log", "Storage"], + "surfaces": [ + { "kind": "ToolboxTool", "id": "hello", "name": "Hello", "description": "Hello tool", "entry": "index.html", "category": "dev" }, + { "kind": "NavPage", "id": "home", "name": "Home", "description": "Home page", "entry": "index.html" } + ], + "resources": ["index.html"] +} +"""); + File.WriteAllText(Path.Combine(pluginRoot, "index.html"), "<h1>Hello</h1>"); + var paths = AppPaths.ForCurrentUser(workspace.Path); + var stateStore = new PluginStateStore(paths); + await stateStore.SetEnabledAsync("hello-tools", true); + + var registry = new PluginRegistryService(paths, stateStore); + var plugins = await registry.LoadPluginsAsync(); + var tools = await registry.LoadEnabledToolModulesAsync(); + + Assert.HasCount(1, plugins); + Assert.IsTrue(plugins[0].IsValid, string.Join(", ", plugins[0].Errors)); + Assert.HasCount(1, tools); + Assert.AreEqual("plugin:hello-tools:hello", tools[0].Id); + } + + [TestMethod] + public async Task PluginRegistryRejectsInvalidAndCoreAssetOverride() + { + using var workspace = TempWorkspace(); + var pluginRoot = CreatePlugin(workspace.Path, "bad", """ +{ + "id": "plugin:bad", + "name": "Bad", + "version": "1.0.0", + "author": "tester", + "description": "Bad plugin", + "entry": "index.html", + "permissions": [], + "surfaces": [ + { "kind": "ToolboxTool", "id": "json_formatter", "name": "Override", "description": "conflict", "entry": "index.html" } + ], + "resources": ["Assets/icons/app_icon.ico"] +} +"""); + File.WriteAllText(Path.Combine(pluginRoot, "index.html"), "<h1>Bad</h1>"); + var paths = AppPaths.ForCurrentUser(workspace.Path); + var registry = new PluginRegistryService(paths, new PluginStateStore(paths)); + + var plugin = (await registry.LoadPluginsAsync()).Single(); + + Assert.IsFalse(plugin.IsValid); + Assert.IsTrue(plugin.Errors.Any(error => error.Contains("plugin:", StringComparison.OrdinalIgnoreCase))); + Assert.IsTrue(plugin.Errors.Any(error => error.Contains("built-in", StringComparison.OrdinalIgnoreCase))); + Assert.IsTrue(plugin.Errors.Any(error => error.Contains("Core asset", StringComparison.OrdinalIgnoreCase))); + } + + [TestMethod] + public async Task PluginStateStorePersistsStatePermissionsSurfacesAndKv() + { + using var workspace = TempWorkspace(); + var paths = AppPaths.ForCurrentUser(workspace.Path); + var store = new PluginStateStore(paths); + + await store.SetEnabledAsync("demo", true); + await store.SetPermissionAsync("demo", PluginPermission.Log, true); + await store.SetSurfaceMountedAsync("demo", "tool", true); + await store.SetValueAsync("demo", "name", "value"); + + var secondStore = new PluginStateStore(paths); + var state = await secondStore.GetStateAsync("demo"); + var values = await secondStore.ListValuesAsync("demo"); + + Assert.IsTrue(state.Enabled); + CollectionAssert.Contains(state.GrantedPermissions.ToList(), PluginPermission.Log); + CollectionAssert.Contains(state.MountedSurfaceIds.ToList(), "tool"); + Assert.AreEqual("value", values["name"]); + } + + [TestMethod] + public async Task PluginToolsMergeIntoToolCatalogWhenEnabled() + { + using var workspace = TempWorkspace(); + var pluginRoot = CreatePlugin(workspace.Path, "merge-demo", """ +{ + "id": "merge-demo", + "name": "Merge Demo", + "version": "1.0.0", + "author": "tester", + "description": "Merge plugin", + "entry": "index.html", + "permissions": [], + "surfaces": [ + { "kind": "ToolboxTool", "id": "tool", "name": "Merged Tool", "description": "Merged", "entry": "index.html" } + ], + "resources": ["index.html"] +} +"""); + File.WriteAllText(Path.Combine(pluginRoot, "index.html"), "<h1>Merged</h1>"); + var paths = AppPaths.ForCurrentUser(workspace.Path); + var store = new PluginStateStore(paths); + await store.SetEnabledAsync("merge-demo", true); + var registry = new PluginRegistryService(paths, store); + + var catalog = new ToolCatalog(ToolCatalog.DefaultModules().Concat(await registry.LoadEnabledToolModulesAsync())); + + Assert.IsNotNull(catalog.GetById("plugin:merge-demo:tool")); + } + + [TestMethod] + public async Task PluginRegistryHonorsSettingsEnabledAndCustomRoot() + { + using var workspace = TempWorkspace(); + var paths = AppPaths.ForCurrentUser(workspace.Path); + var settings = new AppSettingsService(new AppSettingsStore(paths.Root)); + await settings.LoadAsync(); + var store = new PluginStateStore(paths); + var defaultPluginRoot = CreatePlugin(workspace.Path, "default-demo", BasicManifest("default-demo")); + File.WriteAllText(Path.Combine(defaultPluginRoot, "index.html"), "<h1>Default</h1>"); + await store.SetEnabledAsync("default-demo", true); + var registry = new PluginRegistryService(paths, store, settingsService: settings); + + Assert.IsFalse(settings.Current.PluginsEnabled); + Assert.HasCount(0, await registry.LoadEnabledToolModulesAsync()); + + var customPluginsRoot = Path.Combine(workspace.Path, "ExternalPlugins"); + var customPluginRoot = CreatePluginAtRoot(customPluginsRoot, "custom-demo", BasicManifest("custom-demo")); + File.WriteAllText(Path.Combine(customPluginRoot, "index.html"), "<h1>Custom</h1>"); + await store.SetEnabledAsync("custom-demo", true); + await settings.UpdateAsync(value => + { + value.PluginsEnabled = true; + value.PluginRootPath = customPluginsRoot; + }); + + Assert.AreEqual(Path.GetFullPath(customPluginsRoot), registry.PluginsRoot); + var tools = await registry.LoadEnabledToolModulesAsync(); + Assert.HasCount(1, tools); + Assert.AreEqual("plugin:custom-demo:tool", tools[0].Id); + } + + [TestMethod] + public async Task PluginRegistryRequiresReadmeFile() + { + using var workspace = TempWorkspace(); + var pluginRoot = CreatePlugin(workspace.Path, "no-readme", BasicManifest("no-readme")); + File.WriteAllText(Path.Combine(pluginRoot, "index.html"), "<h1>No readme</h1>"); + File.Delete(Path.Combine(pluginRoot, "README.md")); + var paths = AppPaths.ForCurrentUser(workspace.Path); + var registry = new PluginRegistryService(paths, new PluginStateStore(paths)); + + var plugin = (await registry.LoadPluginsAsync()).Single(); + + Assert.IsFalse(plugin.IsValid); + Assert.IsTrue(plugin.Errors.Any(error => error.Contains("README", StringComparison.OrdinalIgnoreCase))); + } + + [TestMethod] + public async Task PluginSnapshotRoundTripsEnabledToolModules() + { + using var workspace = TempWorkspace(); + var pluginRoot = CreatePlugin(workspace.Path, "snapshot-demo", BasicManifest("snapshot-demo")); + File.WriteAllText(Path.Combine(pluginRoot, "index.html"), "<h1>Snapshot</h1>"); + var paths = AppPaths.ForCurrentUser(workspace.Path); + var store = new PluginStateStore(paths); + await store.SetEnabledAsync("snapshot-demo", true); + var registry = new PluginRegistryService(paths, store); + var plugin = (await registry.LoadPluginsAsync()).Single(); + var tool = (await registry.LoadEnabledToolModulesAsync()).Single(); + + var snapshot = new PluginSnapshot( + true, + registry.PluginsRoot, + [LoadedPluginDto.FromLoadedPlugin(plugin)], + [PluginToolDto.FromPluginToolModule(tool)], + DateTimeOffset.Now); + var message = new PluginHostMessage(PluginHostProtocol.SnapshotChanged, Snapshot: snapshot); + var roundTrip = PluginHostProtocol.Deserialize(PluginHostProtocol.Serialize(message)); + + Assert.IsNotNull(roundTrip?.Snapshot); + Assert.HasCount(1, roundTrip.Snapshot.EnabledTools); + Assert.AreEqual("plugin:snapshot-demo:tool", roundTrip.Snapshot.EnabledTools[0].ToToolModule().Id); + } + + [TestMethod] + public async Task BuiltInPluginInstallerCopiesMissingPluginOnly() + { + using var workspace = TempWorkspace(); + var paths = AppPaths.ForCurrentUser(Path.Combine(workspace.Path, "App")); + var installer = new BuiltInPluginInstallerService(paths); + await installer.EnsureInstalledAsync(); + + var installed = Path.Combine(paths.Root, "Plugins", "ipcheck-demo", "index.html"); + var installedReadme = Path.Combine(paths.Root, "Plugins", "ipcheck-demo", "README.md"); + var installedManifest = Path.Combine(paths.Root, "Plugins", "ipcheck-demo", PluginManifest.FileName); + Assert.IsTrue(File.Exists(installed)); + Assert.IsTrue(File.Exists(installedReadme)); + Assert.IsTrue(File.Exists(installedManifest)); + Assert.IsTrue(File.ReadAllText(installed).Contains("IPCheck 网络工具箱", StringComparison.OrdinalIgnoreCase)); + var manifestText = File.ReadAllText(installedManifest); + Assert.IsTrue(manifestText.Contains("\"Http\"", StringComparison.OrdinalIgnoreCase)); + Assert.IsTrue(manifestText.Contains("\"OpenExternal\"", StringComparison.OrdinalIgnoreCase)); + Assert.IsTrue(manifestText.Contains("\"README.md\"", StringComparison.OrdinalIgnoreCase)); + + File.WriteAllText(installed, "<h1>User version</h1>"); + await installer.EnsureInstalledAsync(); + + Assert.AreEqual("<h1>User version</h1>", File.ReadAllText(installed)); + } + + [TestMethod] + public async Task BuiltInSamplePluginIsValidAndDocumentsBridgeCapabilities() + { + using var workspace = TempWorkspace(); + var paths = AppPaths.ForCurrentUser(Path.Combine(workspace.Path, "App")); + var installer = new BuiltInPluginInstallerService(paths); + await installer.EnsureInstalledAsync(); + + var stateStore = new PluginStateStore(paths); + var registry = new PluginRegistryService(paths, stateStore); + var plugin = (await registry.LoadPluginsAsync()).Single(item => item.Manifest.Id == "ipcheck-demo"); + + Assert.IsTrue(plugin.IsValid, string.Join("; ", plugin.Errors)); + CollectionAssert.Contains(plugin.Manifest.Permissions.ToList(), PluginPermission.Storage); + CollectionAssert.Contains(plugin.Manifest.Permissions.ToList(), PluginPermission.Output); + CollectionAssert.Contains(plugin.Manifest.Permissions.ToList(), PluginPermission.OpenExternal); + CollectionAssert.Contains(plugin.Manifest.Resources.ToList(), "README.md"); + + var readme = File.ReadAllText(Path.Combine(plugin.RootPath, "README.md")); + StringAssert.Contains(readme, "Bridge 能力示例"); + StringAssert.Contains(readme, "安全浏览器"); + StringAssert.Contains(readme, "AI 实现提示"); + } + + [TestMethod] + public void PluginDocsAndToolResultAssetsExposeRequiredSections() + { + var root = FindRepositoryRoot(); + var pluginDocs = File.ReadAllText(Path.Combine(root, "docs", "plugins", "README.md")); + var aiDocs = File.ReadAllText(Path.Combine(root, "docs", "plugins", "AI-INTEGRATION.md")); + var helpPage = File.ReadAllText(Path.Combine(root, "src", "box-winUI", "Views", "PluginDocsPage.cs")); + var resultJs = File.ReadAllText(Path.Combine(root, "src", "box-winUI", "Assets", "tool-results", "result.js")); + var resultCss = File.ReadAllText(Path.Combine(root, "src", "box-winUI", "Assets", "tool-results", "result.css")); + var pageJs = File.ReadAllText(Path.Combine(root, "src", "box-winUI", "Assets", "tool-pages", "tool-page.js")); + var pageCss = File.ReadAllText(Path.Combine(root, "src", "box-winUI", "Assets", "tool-pages", "tool-page.css")); + + StringAssert.Contains(pluginDocs, "权限"); + StringAssert.Contains(pluginDocs, "安全边界"); + StringAssert.Contains(pluginDocs, "独立窗口"); + StringAssert.Contains(aiDocs, "Acceptance Checklist"); + StringAssert.Contains(helpPage, "输出区"); + StringAssert.Contains(helpPage, "常见问题"); + + StringAssert.Contains(resultJs, "openSystemBrowser"); + StringAssert.Contains(pageJs, "openSystemBrowser"); + StringAssert.Contains(resultCss, "rank-1"); + StringAssert.Contains(pageCss, "rank-1"); + StringAssert.Contains(resultCss, "prefers-reduced-motion"); + StringAssert.Contains(pageCss, "prefers-reduced-motion"); + } + + private static string CreatePlugin(string root, string folder, string manifest) + { + var pluginRoot = Path.Combine(root, "Plugins", folder); + Directory.CreateDirectory(pluginRoot); + File.WriteAllText(Path.Combine(pluginRoot, PluginManifest.FileName), manifest); + File.WriteAllText(Path.Combine(pluginRoot, "README.md"), "# Test plugin"); + return pluginRoot; + } + + private static string CreatePluginAtRoot(string pluginsRoot, string folder, string manifest) + { + var pluginRoot = Path.Combine(pluginsRoot, folder); + Directory.CreateDirectory(pluginRoot); + File.WriteAllText(Path.Combine(pluginRoot, PluginManifest.FileName), manifest); + File.WriteAllText(Path.Combine(pluginRoot, "README.md"), "# Test plugin"); + return pluginRoot; + } + + private static string BasicManifest(string id) => $$""" +{ + "id": "{{id}}", + "name": "{{id}}", + "version": "1.0.0", + "author": "tester", + "description": "Test plugin", + "entry": "index.html", + "permissions": [], + "surfaces": [ + { "kind": "ToolboxTool", "id": "tool", "name": "Tool", "description": "Tool", "entry": "index.html" } + ], + "resources": ["index.html"] +} +"""; + + private static TempDirectory TempWorkspace() + { + return new TempDirectory(Path.Combine(Path.GetTempPath(), "ymhut-plugin-tests", Guid.NewGuid().ToString("N"))); + } + + private static string FindRepositoryRoot() + { + var directory = new DirectoryInfo(AppContext.BaseDirectory); + while (directory is not null) + { + if (Directory.Exists(Path.Combine(directory.FullName, "src")) && + Directory.Exists(Path.Combine(directory.FullName, "docs")) && + File.Exists(Path.Combine(directory.FullName, "YMhut.Box.Native.sln"))) + { + return directory.FullName; + } + + directory = directory.Parent; + } + + throw new DirectoryNotFoundException("Could not locate repository root from test output path."); + } + + private sealed class TempDirectory : IDisposable + { + public TempDirectory(string path) + { + Path = path; + Directory.CreateDirectory(path); + } + + public string Path { get; } + + public void Dispose() + { + SqliteConnection.ClearAllPools(); + if (Directory.Exists(Path)) + { + Directory.Delete(Path, recursive: true); + } + } + } +} diff --git a/src/YMhut.Box.Tests/ReferenceToolsTests.cs b/src/YMhut.Box.Tests/ReferenceToolsTests.cs new file mode 100644 index 0000000..fe5b69f --- /dev/null +++ b/src/YMhut.Box.Tests/ReferenceToolsTests.cs @@ -0,0 +1,314 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using YMhut.Box.Core.App; +using YMhut.Box.Core.Logging; +using YMhut.Box.Core.Tools; + +namespace YMhut.Box.Tests; + +[TestClass] +[DoNotParallelize] +public sealed class ReferenceToolsTests +{ + [TestMethod] + public async Task ExternalToolCatalogScansLaunchablesFromToolsDirectory() + { + var category = "YMhutTestTools_" + Guid.NewGuid().ToString("N"); + var toolsRoot = Path.Combine(AppContext.BaseDirectory, "Tools"); + var categoryRoot = Path.Combine(toolsRoot, category); + var toolRoot = Path.Combine(categoryRoot, "SampleTool"); + Directory.CreateDirectory(toolRoot); + var launchPath = Path.Combine(toolRoot, "SampleTool.cmd"); + await File.WriteAllTextAsync(launchPath, "@echo off\r\necho YMhut"); + + var tempRoot = Path.Combine(Path.GetTempPath(), "ymhut-reference-tests", Guid.NewGuid().ToString("N")); + try + { + var service = new ExternalToolCatalogService(AppPaths.ForCurrentUser(tempRoot)); + + var modules = await service.GetModulesAsync(); + var module = modules.FirstOrDefault(item => item.Tool.CategoryName == category); + + Assert.IsNotNull(module); + Assert.AreEqual("SampleTool", module.Tool.Name); + Assert.AreEqual("CMD", module.Tool.Extension); + Assert.AreEqual(ReferenceToolRiskLevel.High, module.Tool.RiskLevel); + Assert.IsTrue(module.Id.StartsWith(ExternalToolModule.IdPrefix, StringComparison.OrdinalIgnoreCase)); + } + finally + { + if (Directory.Exists(categoryRoot)) + { + Directory.Delete(categoryRoot, recursive: true); + } + + if (Directory.Exists(tempRoot)) + { + Directory.Delete(tempRoot, recursive: true); + } + } + } + + [TestMethod] + public async Task ExternalToolCatalogUsesInstallRootForToolsAndMetadata() + { + var category = "InstallRootCategory_" + Guid.NewGuid().ToString("N"); + var tempRoot = Path.Combine(Path.GetTempPath(), "ymhut-install-tool-tests", Guid.NewGuid().ToString("N")); + var installRoot = Path.Combine(tempRoot, "install"); + var toolRoot = Path.Combine(installRoot, "Tools", category, "MetaTool"); + var metadataRoot = Path.Combine(installRoot, "Metadata"); + var appRoot = Path.Combine(tempRoot, "appdata"); + var launchPath = Path.Combine(toolRoot, "MetaTool.cmd"); + + var oldInstallRoot = Environment.GetEnvironmentVariable(InstallLayoutPaths.InstallRootEnvironmentVariable); + var oldArchivedRoot = Environment.GetEnvironmentVariable(InstallLayoutPaths.ArchivedLayoutEnvironmentVariable); + try + { + Directory.CreateDirectory(toolRoot); + Directory.CreateDirectory(metadataRoot); + await File.WriteAllTextAsync(launchPath, "@echo off\r\necho install-root"); + await File.WriteAllTextAsync( + Path.Combine(metadataRoot, "tools.json"), + """ + { + "tools": [ + { + "match": "MetaTool", + "description": "Install root metadata description", + "publisher": "YMhut Tests", + "launchTarget": "MetaTool.cmd", + "tags": [ "install-root" ] + } + ] + } + """); + + Environment.SetEnvironmentVariable(InstallLayoutPaths.InstallRootEnvironmentVariable, installRoot); + Environment.SetEnvironmentVariable(InstallLayoutPaths.ArchivedLayoutEnvironmentVariable, null); + + var service = new ExternalToolCatalogService(AppPaths.ForCurrentUser(appRoot)); + var modules = await service.GetModulesAsync(); + var module = modules.FirstOrDefault(item => item.Tool.CategoryName == category); + + Assert.AreEqual(Path.Combine(installRoot, "Tools"), service.ResolveToolsRoot()); + Assert.IsNotNull(module); + Assert.AreEqual("MetaTool", module.Tool.Name); + Assert.AreEqual("Install root metadata description", module.Tool.Description); + Assert.AreEqual("YMhut Tests", module.Tool.Publisher); + Assert.Contains("install-root", module.Tool.Tags); + Assert.IsTrue(module.Tool.LaunchPath.StartsWith(installRoot, StringComparison.OrdinalIgnoreCase)); + } + finally + { + Environment.SetEnvironmentVariable(InstallLayoutPaths.InstallRootEnvironmentVariable, oldInstallRoot); + Environment.SetEnvironmentVariable(InstallLayoutPaths.ArchivedLayoutEnvironmentVariable, oldArchivedRoot); + + if (Directory.Exists(tempRoot)) + { + Directory.Delete(tempRoot, recursive: true); + } + } + } + + [TestMethod] + public async Task RiskConfirmationStorePersistsAndResetsChoices() + { + var tempRoot = Path.Combine(Path.GetTempPath(), "ymhut-risk-tests", Guid.NewGuid().ToString("N")); + try + { + var paths = AppPaths.ForCurrentUser(tempRoot); + var store = new RiskConfirmationStore(paths); + + await store.RememberAsync("builtin:hosts-editor", "execute"); + + var secondStore = new RiskConfirmationStore(paths); + Assert.IsTrue(await secondStore.IsRememberedAsync("builtin:hosts-editor", "execute")); + + await secondStore.ResetAsync(); + Assert.IsFalse(await secondStore.IsRememberedAsync("builtin:hosts-editor", "execute")); + } + finally + { + if (Directory.Exists(tempRoot)) + { + Directory.Delete(tempRoot, recursive: true); + } + } + } + + [TestMethod] + public async Task OpenSourceReferenceServiceLoadsBundledToolNoticesFromInstallRoot() + { + var tempRoot = Path.Combine(Path.GetTempPath(), "ymhut-install-reference-tests", Guid.NewGuid().ToString("N")); + var installRoot = Path.Combine(tempRoot, "install"); + var noticePath = Path.Combine(installRoot, "Tools", "Category", "VendorTool", "LICENSE.txt"); + var appRoot = Path.Combine(tempRoot, "appdata"); + + var oldInstallRoot = Environment.GetEnvironmentVariable(InstallLayoutPaths.InstallRootEnvironmentVariable); + var oldArchivedRoot = Environment.GetEnvironmentVariable(InstallLayoutPaths.ArchivedLayoutEnvironmentVariable); + try + { + Directory.CreateDirectory(Path.GetDirectoryName(noticePath)!); + await File.WriteAllTextAsync(noticePath, "Vendor license text"); + + Environment.SetEnvironmentVariable(InstallLayoutPaths.InstallRootEnvironmentVariable, installRoot); + Environment.SetEnvironmentVariable(InstallLayoutPaths.ArchivedLayoutEnvironmentVariable, null); + + var service = new OpenSourceReferenceService(AppPaths.ForCurrentUser(appRoot)); + var references = await service.GetReferencesAsync(); + + Assert.IsTrue(references.Any(item => + item.Kind == "Third-party tool notice" && + string.Equals(item.Path, noticePath, StringComparison.OrdinalIgnoreCase))); + } + finally + { + Environment.SetEnvironmentVariable(InstallLayoutPaths.InstallRootEnvironmentVariable, oldInstallRoot); + Environment.SetEnvironmentVariable(InstallLayoutPaths.ArchivedLayoutEnvironmentVariable, oldArchivedRoot); + + if (Directory.Exists(tempRoot)) + { + Directory.Delete(tempRoot, recursive: true); + } + } + } + + [TestMethod] + public void BuiltinReferenceCatalogRegistersExpectedToolsAndRiskLevels() + { + var catalog = new BuiltinReferenceToolCatalog(); + var modules = catalog.GetModules(); + var ids = modules.Select(module => module.Definition.Id).ToHashSet(StringComparer.OrdinalIgnoreCase); + + foreach (var id in new[] + { + "cert-block", + "port-viewer", + "hosts-editor", + "keyboard-test", + "junk-cleaner", + "bsod-analysis", + "winget-installer", + "battery-report", + "speed-test", + "wifi-password", + "disk-space-analyzer", + "lite-monitor", + "windows-activation", + "defender-control", + "cpu-ranking", + "gpu-ranking", + "context-menu-mgr" + }) + { + Assert.Contains(id, ids); + } + + Assert.AreEqual(ReferenceToolRiskLevel.High, catalog.GetByModuleId("builtin:hosts-editor")?.RiskLevel); + Assert.AreEqual(ReferenceToolRiskLevel.None, catalog.GetByModuleId("builtin:windows-activation")?.RiskLevel); + Assert.HasCount(17, modules); + } + + [TestMethod] + public async Task ResilientLogServiceFallsBackToJsonlWhenPrimaryFails() + { + var tempRoot = Path.Combine(Path.GetTempPath(), "ymhut-log-fallback-tests", Guid.NewGuid().ToString("N")); + try + { + var service = new ResilientLogService(new ThrowingLogService(), AppPaths.ForCurrentUser(tempRoot)); + + await service.WriteAsync("Information", "tool-run", "External tool success", "toolId=external:test"); + var entries = await service.ReadAsync(take: 10); + + Assert.HasCount(1, entries); + Assert.AreEqual("tool-run", entries[0].Category); + Assert.IsTrue(File.Exists(Path.Combine(tempRoot, "Logs", "app-log-fallback.jsonl"))); + } + finally + { + if (Directory.Exists(tempRoot)) + { + Directory.Delete(tempRoot, recursive: true); + } + } + } + + [TestMethod] + public async Task OpenSourceReferenceServiceLoadsPackagesAndBundledToolNotices() + { + var tempRoot = Path.Combine(Path.GetTempPath(), "ymhut-reference-attribution-tests", Guid.NewGuid().ToString("N")); + var toolsRoot = Path.Combine(AppContext.BaseDirectory, "Tools"); + var noticeCategory = "YMhutTestNotice_" + Guid.NewGuid().ToString("N"); + var noticeCategoryRoot = Path.Combine(toolsRoot, noticeCategory); + var noticePath = Path.Combine(noticeCategoryRoot, "SampleVendor", "LICENSE.txt"); + try + { + var projectRoot = Path.Combine(tempRoot, "src", "FakeProject"); + Directory.CreateDirectory(projectRoot); + await File.WriteAllTextAsync( + Path.Combine(projectRoot, "FakeProject.csproj"), + """ + <Project Sdk="Microsoft.NET.Sdk"> + <ItemGroup> + <PackageReference Include="Sample.Reference.Package" Version="1.2.3" /> + </ItemGroup> + </Project> + """); + + Directory.CreateDirectory(Path.GetDirectoryName(noticePath)!); + await File.WriteAllTextAsync(noticePath, "Sample vendor license"); + + var service = new OpenSourceReferenceService(AppPaths.ForCurrentUser(tempRoot)); + var references = await service.GetReferencesAsync(); + + Assert.IsTrue(references.Any(item => item.Kind == "Reference project attribution" && item.Name.Contains("tubatool", StringComparison.OrdinalIgnoreCase))); + Assert.IsTrue(references.Any(item => item.Kind == "NuGet package" && item.Name == "Sample.Reference.Package" && item.Version == "1.2.3")); + Assert.IsTrue(references.Any(item => item.Kind == "Third-party tool notice" && item.Path == noticePath)); + } + finally + { + if (Directory.Exists(noticeCategoryRoot)) + { + Directory.Delete(noticeCategoryRoot, recursive: true); + } + + if (Directory.Exists(tempRoot)) + { + Directory.Delete(tempRoot, recursive: true); + } + } + } + + private sealed class ThrowingLogService : ILogService + { + public event EventHandler<LogEntry>? EntryWritten; + + public string LogPath => "throwing"; + + public Task WriteAsync(string level, string category, string message, string? detail = null, CancellationToken cancellationToken = default) + { + EntryWritten?.Invoke(this, new LogEntry(DateTimeOffset.Now, level, category, message, detail)); + throw new IOException("primary failed"); + } + + public Task<IReadOnlyList<LogEntry>> ReadAsync(string? level = null, string? category = null, int take = 500, CancellationToken cancellationToken = default) + => throw new IOException("primary failed"); + + public Task<IReadOnlyList<LogEntry>> ReadByDateAsync(DateOnly date, string? level = null, string? query = null, int take = 0, CancellationToken cancellationToken = default) + => throw new IOException("primary failed"); + + public Task<IReadOnlyList<LogEntry>> ReadByDatePageAsync(DateOnly date, string? level = null, string? query = null, int skip = 0, int take = 100, CancellationToken cancellationToken = default) + => throw new IOException("primary failed"); + + public Task CleanupAsync(int retentionCount, CancellationToken cancellationToken = default) + => throw new IOException("primary failed"); + + public Task ClearAllAsync(CancellationToken cancellationToken = default) + => throw new IOException("primary failed"); + + public Task ClearTodayAsync(CancellationToken cancellationToken = default) + => throw new IOException("primary failed"); + + public Task ClearFilteredTodayAsync(string? level = null, string? query = null, CancellationToken cancellationToken = default) + => throw new IOException("primary failed"); + } +} diff --git a/src/YMhut.Box.Tests/RemoteMediaCatalogTests.cs b/src/YMhut.Box.Tests/RemoteMediaCatalogTests.cs new file mode 100644 index 0000000..1fdbd0e --- /dev/null +++ b/src/YMhut.Box.Tests/RemoteMediaCatalogTests.cs @@ -0,0 +1,167 @@ +using YMhut.Box.Core.Api; +using YMhut.Box.Core.App; +using YMhut.Box.Core.Media; + +namespace YMhut.Box.Tests; + +[TestClass] +public sealed class RemoteMediaCatalogTests +{ + [TestMethod] + public void ParsesCurrentMediaTypesSnapshot() + { + var catalog = RemoteMediaCatalogParser.Parse(ReadRepoFile("server", "update", "public", "media-types.json")); + + Assert.AreEqual("1.0.6", catalog.LayoutVersion); + Assert.AreEqual("grid", catalog.UiConfig.DefaultView); + Assert.IsTrue(catalog.Categories.Count >= 2); + + var image = catalog.Categories.Single(category => category.Id == "image"); + Assert.IsTrue(image.Enabled); + Assert.AreEqual(RemoteMediaKind.Image, image.Kind); + Assert.IsTrue(image.Layout.ShowPreview); + CollectionAssert.Contains(image.Sources.First(source => source.Id == "xjj").SupportedFormats.ToArray(), "jpg"); + Assert.AreEqual(30, image.Sources.First(source => source.Id == "xjj").RefreshIntervalSeconds); + + var video = catalog.Categories.Single(category => category.Id == "video"); + Assert.AreEqual(RemoteMediaKind.Video, video.Kind); + Assert.IsFalse(video.Layout.AutoPlay); + CollectionAssert.Contains(video.Sources.First().SupportedFormats.ToArray(), "mp4"); + } + + [TestMethod] + public void ParsesLegacyMediaTypesSnapshot() + { + var catalog = RemoteMediaCatalogParser.Parse(ReadRepoFile("box-old", "server", "media-types.json")); + + Assert.AreEqual("1.0.8", catalog.LayoutVersion); + Assert.IsTrue(catalog.Categories.Count >= 2); + Assert.IsTrue(catalog.Categories.Any(category => category.Id == "image" && category.Sources.Count >= 7)); + Assert.IsTrue(catalog.Categories.Any(category => category.Id == "video" && category.Sources.Any(source => source.Id == "radom_xjj_mv"))); + } + + [TestMethod] + public void AppliesDefaultsWhenOptionalFieldsAreMissing() + { + const string minimal = """ + { + "categories": [ + { + "id": "video", + "subcategories": [ + { + "id": "demo", + "api_url": "https://example.test/media" + } + ] + } + ] + } + """; + + var catalog = RemoteMediaCatalogParser.Parse(minimal); + var category = catalog.Categories.Single(); + var source = category.Sources.Single(); + + Assert.IsTrue(category.Enabled); + Assert.AreEqual(RemoteMediaKind.Video, category.Kind); + Assert.AreEqual(1, category.Layout.Columns); + Assert.AreEqual("16:9", category.Layout.AspectRatio); + Assert.IsTrue(category.Layout.ShowPreview); + Assert.IsFalse(category.Layout.AutoPlay); + Assert.IsTrue(source.Downloadable); + Assert.AreEqual(60, source.RefreshIntervalSeconds); + CollectionAssert.AreEqual(new[] { "mp4", "webm" }, source.SupportedFormats.ToArray()); + Assert.AreEqual("https://example.test/media", source.ThumbnailUrl); + } + + [TestMethod] + public async Task ServiceWritesReadsFallsBackAndClearsCache() + { + var root = Path.Combine(Path.GetTempPath(), "ymhut-remote-media-" + Guid.NewGuid().ToString("N")); + try + { + var paths = new AppPaths(root); + paths.EnsureCreated(); + var content = ReadRepoFile("server", "update", "public", "media-types.json"); + var api = new FakeApiManager(content); + var service = new RemoteMediaCatalogService(paths, api); + + var remote = await service.LoadAsync(forceRefresh: true); + + Assert.AreEqual(RemoteMediaCatalogLoadSource.Remote, remote.Source); + Assert.IsTrue(api.LastUri?.Query.Contains("_=", StringComparison.Ordinal) == true); + Assert.IsTrue(File.Exists(Path.Combine(paths.Cache, "remote-media", "media-types.json"))); + + var fallback = new RemoteMediaCatalogService(paths, new FakeApiManager(string.Empty, success: false)); + var cached = await fallback.LoadAsync(); + + Assert.AreEqual(RemoteMediaCatalogLoadSource.Cache, cached.Source); + Assert.IsFalse(string.IsNullOrWhiteSpace(cached.Warning)); + + await fallback.ClearCacheAsync(); + Assert.IsFalse(Directory.Exists(Path.Combine(paths.Cache, "remote-media"))); + Assert.IsTrue(Directory.Exists(paths.Cache)); + } + finally + { + if (Directory.Exists(root)) + { + Directory.Delete(root, recursive: true); + } + } + } + + private static string ReadRepoFile(params string[] segments) + { + var directory = new DirectoryInfo(Directory.GetCurrentDirectory()); + while (directory is not null) + { + var candidate = Path.Combine(new[] { directory.FullName }.Concat(segments).ToArray()); + if (File.Exists(candidate)) + { + return File.ReadAllText(candidate); + } + + directory = directory.Parent; + } + + throw new DirectoryNotFoundException("Unable to locate repository sample file."); + } + + private sealed class FakeApiManager(string content, bool success = true) : IApiManager + { + public Uri? LastUri { get; private set; } + + public Task<ApiResponse> FetchAsync(string endpointId, string input = "", CancellationToken cancellationToken = default) + { + LastUri = RemoteMediaCatalogService.PrimaryConfigUri; + return Task.FromResult(new ApiResponse( + endpointId, + LastUri, + success, + success ? content : string.Empty, + success ? null : "offline", + DateTimeOffset.Now, + success ? 200 : 0)); + } + + public Task<ApiResponse> FetchUriAsync(string endpointId, Uri uri, string input = "", CancellationToken cancellationToken = default) + { + LastUri = uri; + return Task.FromResult(new ApiResponse( + endpointId, + uri, + success, + success ? content : string.Empty, + success ? null : "offline", + DateTimeOffset.Now, + success ? 200 : 0)); + } + + public Task<ApiHealthStatus> CheckHealthAsync(string endpointId, string input = "", CancellationToken cancellationToken = default) + { + return Task.FromResult(success ? ApiHealthStatus.Healthy : ApiHealthStatus.Unhealthy); + } + } +} diff --git a/src/YMhut.Box.Tests/RemoteMediaResolverTests.cs b/src/YMhut.Box.Tests/RemoteMediaResolverTests.cs new file mode 100644 index 0000000..a0a625c --- /dev/null +++ b/src/YMhut.Box.Tests/RemoteMediaResolverTests.cs @@ -0,0 +1,129 @@ +using System.Net; +using YMhut.Box.Core.Media; + +namespace YMhut.Box.Tests; + +[TestClass] +public sealed class RemoteMediaResolverTests +{ + [TestMethod] + public async Task ResolvesRedirectToDirectMedia() + { + var resolver = CreateResolver(request => + { + if (request.RequestUri?.AbsolutePath == "/api") + { + return Redirect("/media/demo.mp4"); + } + + return Text(HttpStatusCode.OK, string.Empty, "video/mp4"); + }); + + var result = await resolver.ResolveMediaAsync("https://example.test/api", RemoteMediaKind.Video, cacheBust: false); + + Assert.AreEqual("https://example.test/media/demo.mp4", result.Uri.AbsoluteUri); + Assert.IsTrue(result.IsDirectMedia); + Assert.AreEqual("video/mp4", result.ContentType); + Assert.AreEqual(".mp4", result.SuggestedExtension); + } + + [TestMethod] + public async Task ExtractsPlainTextUrl() + { + var resolver = CreateResolver(request => + { + if (request.RequestUri?.Host == "cdn.test") + { + return Text(HttpStatusCode.OK, string.Empty, "video/mp4"); + } + + return Text(HttpStatusCode.OK, "https://cdn.test/random/clip.mp4", "text/plain"); + }); + + var result = await resolver.ResolveMediaAsync("https://example.test/api", RemoteMediaKind.Video, cacheBust: false); + + Assert.AreEqual("https://cdn.test/random/clip.mp4", result.Uri.AbsoluteUri); + Assert.IsTrue(result.IsDirectMedia); + Assert.AreEqual(".mp4", result.SuggestedExtension); + } + + [TestMethod] + public async Task ExtractsNestedJsonUrl() + { + var resolver = CreateResolver(request => + { + if (request.RequestUri?.Host == "cdn.test") + { + return Text(HttpStatusCode.OK, string.Empty, "image/webp"); + } + + return Text(HttpStatusCode.OK, """ + { + "data": { + "url": "https://cdn.test/images/pic.webp" + } + } + """, "application/json"); + }); + + var result = await resolver.ResolveMediaAsync("https://example.test/api", RemoteMediaKind.Image, cacheBust: false); + + Assert.AreEqual("https://cdn.test/images/pic.webp", result.Uri.AbsoluteUri); + Assert.IsTrue(result.IsDirectMedia); + Assert.AreEqual(".webp", result.SuggestedExtension); + } + + [TestMethod] + public async Task KeepsHtmlWithoutMediaUrlAsNonDirect() + { + var resolver = CreateResolver(_ => Text(HttpStatusCode.OK, "<html><body>No media here</body></html>", "text/html")); + + var result = await resolver.ResolveMediaAsync("https://example.test/page", RemoteMediaKind.Video, cacheBust: false); + + Assert.AreEqual("https://example.test/page", result.Uri.AbsoluteUri); + Assert.IsFalse(result.IsDirectMedia); + Assert.AreEqual("text/html", result.ContentType); + } + + [TestMethod] + public async Task TreatsDirectMediaContentTypeAsPlayable() + { + var resolver = CreateResolver(_ => Text(HttpStatusCode.OK, string.Empty, "audio/mpeg")); + + var result = await resolver.ResolveMediaAsync("https://example.test/random", RemoteMediaKind.Audio, cacheBust: false); + + Assert.AreEqual("https://example.test/random", result.Uri.AbsoluteUri); + Assert.IsTrue(result.IsDirectMedia); + Assert.AreEqual(".mp3", result.SuggestedExtension); + } + + private static RemoteMediaResolver CreateResolver(Func<HttpRequestMessage, HttpResponseMessage> responseFactory) + { + return new RemoteMediaResolver(() => new StubHttpHandler(responseFactory)); + } + + private static HttpResponseMessage Redirect(string location) + { + var response = new HttpResponseMessage(HttpStatusCode.Redirect); + response.Headers.Location = new Uri(location, UriKind.RelativeOrAbsolute); + return response; + } + + private static HttpResponseMessage Text(HttpStatusCode statusCode, string content, string contentType) + { + return new HttpResponseMessage(statusCode) + { + Content = new StringContent(content, System.Text.Encoding.UTF8, contentType) + }; + } + + private sealed class StubHttpHandler(Func<HttpRequestMessage, HttpResponseMessage> responseFactory) : HttpMessageHandler + { + protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + var response = responseFactory(request); + response.RequestMessage = request; + return Task.FromResult(response); + } + } +} diff --git a/src/YMhut.Box.Tests/SensitiveTextTests.cs b/src/YMhut.Box.Tests/SensitiveTextTests.cs new file mode 100644 index 0000000..9fdffcf --- /dev/null +++ b/src/YMhut.Box.Tests/SensitiveTextTests.cs @@ -0,0 +1,22 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using YMhut.Box.Core; + +namespace YMhut.Box.Tests; + +[TestClass] +public sealed class SensitiveTextTests +{ + [TestMethod] + public void SanitizeRemovesInternalRemoteDetails() + { + var sanitized = SensitiveText.Sanitize("failed https://update.ymhut.cn/update-info.json?token=abc media-types.json download_url api_url"); + + Assert.DoesNotContain("https://", sanitized); + Assert.DoesNotContain("update.ymhut.cn", sanitized); + Assert.DoesNotContain("update-info.json", sanitized); + Assert.DoesNotContain("media-types.json", sanitized); + Assert.DoesNotContain("download_url", sanitized); + Assert.DoesNotContain("api_url", sanitized); + Assert.Contains("远程服务", sanitized); + } +} diff --git a/src/YMhut.Box.Tests/ServiceLayerTests.cs b/src/YMhut.Box.Tests/ServiceLayerTests.cs new file mode 100644 index 0000000..f129757 --- /dev/null +++ b/src/YMhut.Box.Tests/ServiceLayerTests.cs @@ -0,0 +1,129 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using YMhut.Box.Core.App; +using YMhut.Box.Core.Api; +using YMhut.Box.Core.Data; +using YMhut.Box.Core.Logging; +using YMhut.Box.Core.Settings; + +namespace YMhut.Box.Tests; + +[TestClass] +public sealed class ServiceLayerTests +{ + [TestMethod] + public async Task SettingsServiceBroadcastsAndPersistsChanges() + { + var root = Path.Combine(Path.GetTempPath(), "ymhut-box-tests", Guid.NewGuid().ToString("N")); + var service = new AppSettingsService(new AppSettingsStore(root)); + var changed = false; + service.PropertyChanged += (_, args) => changed |= args.PropertyName == nameof(ISettingsService.Current); + + await service.LoadAsync(); + await service.UpdateAsync(settings => settings.Theme = "Dark"); + + var reloaded = await new AppSettingsStore(root).LoadAsync(); + Assert.IsTrue(changed); + Assert.AreEqual("Dark", reloaded.Theme); + } + + [TestMethod] + public async Task SettingsStorePersistsAndNormalizesFeedbackDefaults() + { + var root = Path.Combine(Path.GetTempPath(), "ymhut-box-tests", Guid.NewGuid().ToString("N")); + var store = new AppSettingsStore(root); + await store.SaveAsync(new AppSettings + { + FeedbackDefaultContact = "dev@example.com", + FeedbackDefaultType = "bad-type", + FeedbackDefaultSeverity = "bad-severity", + FeedbackIncludeTodayLogsByDefault = true, + FeedbackIncludeToolStatusByDefault = true, + FeedbackIncludeSystemSummaryByDefault = true, + FeedbackRememberDefaults = false + }); + + var settings = await store.LoadAsync(); + + Assert.AreEqual("dev@example.com", settings.FeedbackDefaultContact); + Assert.AreEqual("issue", settings.FeedbackDefaultType); + Assert.AreEqual("normal", settings.FeedbackDefaultSeverity); + Assert.IsTrue(settings.FeedbackIncludeTodayLogsByDefault); + Assert.IsTrue(settings.FeedbackIncludeToolStatusByDefault); + Assert.IsTrue(settings.FeedbackIncludeSystemSummaryByDefault); + Assert.IsFalse(settings.FeedbackRememberDefaults); + } + + [TestMethod] + public async Task SettingsStorePersistsToolboxAndRiskSettings() + { + var root = Path.Combine(Path.GetTempPath(), "ymhut-box-tests", Guid.NewGuid().ToString("N")); + var store = new AppSettingsStore(root); + await store.SaveAsync(new AppSettings + { + ToolDisplayMode = "compact", + ToolboxDefaultScope = "plugins", + ToolboxCompactCards = true, + ToolboxShowRecentFirst = false, + ShowHardwareBrandLogo = false, + RequireRiskConfirmation = false, + LogRetentionCount = 90, + PluginsEnabled = true + }); + + var settings = await store.LoadAsync(); + + Assert.AreEqual("list", settings.ToolDisplayMode); + Assert.AreEqual("plugin", settings.ToolboxDefaultScope); + Assert.IsTrue(settings.ToolboxCompactCards); + Assert.IsFalse(settings.ToolboxShowRecentFirst); + Assert.IsFalse(settings.ShowHardwareBrandLogo); + Assert.IsFalse(settings.RequireRiskConfirmation); + Assert.AreEqual(90, settings.LogRetentionCount); + Assert.IsTrue(settings.PluginsEnabled); + } + + [TestMethod] + public async Task LogServiceWritesReadsAndCleansUp() + { + var root = Path.Combine(Path.GetTempPath(), "ymhut-box-tests", Guid.NewGuid().ToString("N")); + var service = new JsonFileLogService(AppPaths.ForCurrentUser(root)); + + await service.WriteAsync("Information", "test", "first"); + await service.WriteAsync("Error", "test", "second"); + var errors = await service.ReadAsync("Error"); + + Assert.HasCount(1, errors); + Assert.AreEqual("second", errors[0].Message); + + await service.CleanupAsync(1); + var all = await service.ReadAsync(); + Assert.HasCount(1, all); + } + + [TestMethod] + public void ApiEndpointsResolveWeather() + { + var ok = ApiEndpoints.TryResolve("weather", "Shanghai", out var endpoint, out var uri); + + Assert.IsTrue(ok); + Assert.AreEqual("weather", endpoint.Id); + StringAssert.Contains(endpoint.SourceName, "Open-Meteo"); + StringAssert.Contains(uri.Host, "open-meteo.com"); + } + + [TestMethod] + public void ReferenceDataServiceValidatesRequiredAssets() + { + var root = Path.Combine(Path.GetTempPath(), "ymhut-box-tests", Guid.NewGuid().ToString("N")); + var assets = Path.Combine(root, "Assets"); + Directory.CreateDirectory(Path.Combine(assets, "data", "reference")); + File.WriteAllText(Path.Combine(assets, "data", "reference", "ymhut_reference_data.json"), "{}"); + + var service = new ReferenceDataService(AppPaths.ForCurrentUser(root, assets)); + var result = service.ValidateRequiredAssets("data/reference/ymhut_reference_data.json", "data/missing.json"); + + Assert.IsFalse(result.IsHealthy); + Assert.HasCount(1, result.PresentPaths); + Assert.HasCount(1, result.MissingPaths); + } +} diff --git a/src/YMhut.Box.Tests/SolarEphemerisServiceTests.cs b/src/YMhut.Box.Tests/SolarEphemerisServiceTests.cs new file mode 100644 index 0000000..c34b00e --- /dev/null +++ b/src/YMhut.Box.Tests/SolarEphemerisServiceTests.cs @@ -0,0 +1,134 @@ +using System.Net; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using YMhut.Box.Core.App; +using YMhut.Box.Core.Net; +using YMhut.Box.Core.SolarSystem; + +namespace YMhut.Box.Tests; + +[TestClass] +public sealed class SolarEphemerisServiceTests +{ + [TestMethod] + public void MiriadeParserReadsRectangularPxPyPzPayload() + { + var json = """ + { + "data": [ + { + "Date": 0.24611925E+7, + "px": -0.32062933285171752E+0, + "py": 0.14714396284300654E+0, + "pz": 0.41432383444950593E-1 + } + ] + } + """; + + var parsed = SolarEphemerisService.TryParseMiriadePayload(json, out var vector); + + Assert.IsTrue(parsed); + Assert.AreEqual(-0.32062933285171752, vector.X, 1e-12); + Assert.AreEqual(0.14714396284300654, vector.Y, 1e-12); + Assert.AreEqual(0.041432383445, vector.Z, 1e-12); + } + + [TestMethod] + public async Task CurrentEphemerisReturnsApproximatePayloadWhenPreciseCacheMissing() + { + var root = Path.Combine(Path.GetTempPath(), "ymhut-solar-tests", Guid.NewGuid().ToString("N")); + try + { + var service = new SolarEphemerisService( + AppPaths.ForCurrentUser(root), + new FakeHttpService(_ => Task.FromException<string>(new InvalidOperationException()))); + + var payload = await service.GetCurrentAsync(); + + Assert.AreEqual("ephemeris:sync", payload.Type); + Assert.AreEqual("China Ephemeris Approx", payload.Source); + Assert.HasCount(8, payload.Planets); + } + finally + { + if (Directory.Exists(root)) + { + Directory.Delete(root, recursive: true); + } + } + } + + [TestMethod] + public async Task PreciseEphemerisRequestsMiriadePlanetsConcurrently() + { + var root = Path.Combine(Path.GetTempPath(), "ymhut-solar-tests", Guid.NewGuid().ToString("N")); + var requestTimes = new List<DateTimeOffset>(); + try + { + var service = new SolarEphemerisService( + AppPaths.ForCurrentUser(root), + new FakeHttpService(async uri => + { + lock (requestTimes) + { + requestTimes.Add(DateTimeOffset.UtcNow); + } + + await Task.Delay(80); + if (uri.Host.Contains("ssp.imcce.fr", StringComparison.OrdinalIgnoreCase)) + { + return """ + { + "data": [ + { "px": 1.0, "py": 2.0, "pz": 3.0 } + ] + } + """; + } + + throw new InvalidOperationException("Unexpected endpoint"); + })); + + var payload = await service.GetPreciseCurrentAsync(); + + Assert.IsNotNull(payload); + Assert.AreEqual("IMCCE Miriade", payload.Source); + Assert.HasCount(8, payload.Planets); + Assert.HasCount(8, requestTimes); + Assert.IsLessThan(70, (requestTimes.Max() - requestTimes.Min()).TotalMilliseconds); + } + finally + { + if (Directory.Exists(root)) + { + Directory.Delete(root, recursive: true); + } + } + } + + private sealed class FakeHttpService(Func<Uri, Task<string>> handler) : IHttpService + { + public async Task<HttpServiceResult> GetAsync(Uri uri, CancellationToken cancellationToken = default) + { + var content = await GetStringAsync(uri, cancellationToken); + return new HttpServiceResult(HttpStatusCode.OK, content, new Dictionary<string, string[]>(), TimeSpan.FromMilliseconds(1)); + } + + public Task<string> GetStringAsync(Uri uri, CancellationToken cancellationToken = default) + { + return handler(uri); + } + + public Task<HttpServiceResult> SendAsync( + Uri uri, + string method = "GET", + string? body = null, + IReadOnlyDictionary<string, string>? headers = null, + bool ensureSuccess = true, + HttpRequestPolicy? policy = null, + CancellationToken cancellationToken = default) + { + return GetAsync(uri, cancellationToken); + } + } +} diff --git a/src/YMhut.Box.Tests/StartupCheckTests.cs b/src/YMhut.Box.Tests/StartupCheckTests.cs new file mode 100644 index 0000000..674e710 --- /dev/null +++ b/src/YMhut.Box.Tests/StartupCheckTests.cs @@ -0,0 +1,647 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.Data.Sqlite; +using YMhut.Box.Core.App; +using YMhut.Box.Core.Startup; + +namespace YMhut.Box.Tests; + +[TestClass] +[DoNotParallelize] +public sealed class StartupCheckTests +{ + [TestMethod] + public async Task FastPreflightPassesForPureLangInstallLayout() + { + using var workspace = TestWorkspace.Create("ymhut-startup-fast"); + var installRoot = CreateHealthyInstall(workspace); + var appRoot = workspace.CreateDirectory("user"); + + using var environment = EnvironmentScope.Capture(InstallLayoutPaths.InstallRootEnvironmentVariable); + environment.Set(InstallLayoutPaths.InstallRootEnvironmentVariable, installRoot); + + var paths = AppPaths.ForCurrentUser(appRoot); + var store = new StartupCheckStore(paths); + var service = new InstallIntegrityCheckService(paths, store); + + var report = await service.RunFastPreflightAsync(); + + Assert.AreEqual(StartupCheckKind.FastPreflight, report.Kind); + Assert.AreEqual(0, report.CriticalIssueCount); + Assert.IsTrue(report.Items.Any(item => item.Id == "lang:zh-CN" && item.Status == StartupCheckStatus.Passed)); + Assert.IsTrue(report.Items.Any(item => item.Id == "lang:en-US" && item.Status == StartupCheckStatus.Passed)); + Assert.IsFalse(Directory.Exists(Path.Combine(appRoot, "Runtime"))); + StringAssert.StartsWith(store.DatabasePath, Path.Combine(appRoot, "Logs")); + } + + [TestMethod] + public async Task FastPreflightDoesNotBlockDevelopmentRootCultureLayout() + { + using var workspace = TestWorkspace.Create("ymhut-startup-dev-layout"); + var installRoot = CreateDevelopmentInstall(workspace); + var appRoot = workspace.CreateDirectory("user"); + + using var environment = EnvironmentScope.Capture(InstallLayoutPaths.InstallRootEnvironmentVariable); + environment.Set(InstallLayoutPaths.InstallRootEnvironmentVariable, installRoot); + + var paths = AppPaths.ForCurrentUser(appRoot); + var service = new InstallIntegrityCheckService(paths, new StartupCheckStore(paths)); + + var report = await service.RunFastPreflightAsync(); + var resourcesPri = report.Items.FirstOrDefault(item => item.Id == "file:resources.pri"); + var zhCn = report.Items.FirstOrDefault(item => item.Id == "lang:zh-CN"); + var enUs = report.Items.FirstOrDefault(item => item.Id == "lang:en-US"); + + Assert.AreEqual(0, report.CriticalIssueCount); + Assert.IsNotNull(resourcesPri); + Assert.AreEqual(StartupCheckStatus.Missing, resourcesPri.Status); + Assert.AreEqual(StartupCheckSeverity.Warning, resourcesPri.Severity); + Assert.IsNotNull(zhCn); + Assert.AreEqual(StartupCheckStatus.Passed, zhCn.Status); + Assert.IsNotNull(enUs); + Assert.AreEqual(StartupCheckStatus.Passed, enUs.Status); + Assert.IsFalse(report.Items.Any(item => item.Id.StartsWith("lang:root:", StringComparison.OrdinalIgnoreCase))); + } + + [TestMethod] + public async Task FastPreflightAcceptsWinUiDevelopmentPriAndRootCultureSatellites() + { + using var workspace = TestWorkspace.Create("ymhut-startup-dev-pri"); + var installRoot = CreateDevelopmentInstall(workspace); + var appRoot = workspace.CreateDirectory("user"); + workspace.CreateFile(["install", "YMhutBox.pri"], string.Empty); + workspace.CreateFile(["install", "fr-FR", "Microsoft.ui.xaml.dll.mui"], string.Empty); + workspace.CreateFile(["install", "ja-JP", "Microsoft.ui.xaml.dll.mui"], string.Empty); + workspace.CreateFile(["install", "Tools", "Sample", "tool.exe"], string.Empty); + workspace.CreateFile(["install", "Metadata", "tools.json"], """{ "tools": [] }"""); + + using var environment = EnvironmentScope.Capture(InstallLayoutPaths.InstallRootEnvironmentVariable); + environment.Set(InstallLayoutPaths.InstallRootEnvironmentVariable, installRoot); + + var paths = AppPaths.ForCurrentUser(appRoot); + var service = new InstallIntegrityCheckService(paths, new StartupCheckStore(paths)); + + var report = await service.RunFastPreflightAsync(); + + Assert.AreEqual(0, report.VisibleIssueCount); + Assert.IsTrue(report.Items.Any(item => item.Id == "manifest" && item.Status == StartupCheckStatus.Skipped)); + Assert.IsTrue(report.Items.Any(item => item.Id == "file:resources.pri" && item.Status == StartupCheckStatus.Passed)); + Assert.IsTrue(report.Items.Any(item => item.Id == "lang:root-culture-summary" && item.Status == StartupCheckStatus.Skipped)); + Assert.IsFalse(report.Items.Any(item => item.Id.StartsWith("lang:root:", StringComparison.OrdinalIgnoreCase))); + } + + [TestMethod] + public async Task CurrentHistoryNormalizesLegacyDevelopmentLayoutFalsePositives() + { + using var workspace = TestWorkspace.Create("ymhut-startup-legacy-false-positive"); + var installRoot = CreateDevelopmentInstall(workspace); + var appRoot = workspace.CreateDirectory("user"); + workspace.CreateFile(["install", "YMhutBox.pri"], string.Empty); + workspace.CreateFile(["install", "fr-FR", "Microsoft.ui.xaml.dll.mui"], string.Empty); + workspace.CreateFile(["install", "Tools", "Sample", "tool.exe"], string.Empty); + workspace.CreateFile(["install", "Metadata", "tools.json"], """{ "tools": [] }"""); + + using var environment = EnvironmentScope.Capture(InstallLayoutPaths.InstallRootEnvironmentVariable); + environment.Set(InstallLayoutPaths.InstallRootEnvironmentVariable, installRoot); + + var paths = AppPaths.ForCurrentUser(appRoot); + var store = new StartupCheckStore(paths); + var manifestIdentity = InstallRootContext.BuildManifestIdentity(installRoot); + var oldReport = StartupCheckReport.Create( + StartupCheckKind.FastPreflight, + new DateTimeOffset(2026, 6, 1, 8, 0, 0, TimeSpan.Zero), + installRoot, + manifestIdentity, + InstallRootContext.BuildInstallIdentity(installRoot, manifestIdentity), + $"installRoot={installRoot}; manifest={manifestIdentity}", + [ + new StartupCheckItem("manifest", "安装清单", StartupCheckSeverity.Warning, StartupCheckStatus.Missing, "文件缺失", Path.Combine(installRoot, "config", "install-manifest.ini")), + new StartupCheckItem("file:resources.pri", "resources.pri", StartupCheckSeverity.Warning, StartupCheckStatus.Missing, "文件缺失", Path.Combine(installRoot, "resources.pri")), + new StartupCheckItem("lang:zh-CN", "语言资源 zh-CN", StartupCheckSeverity.Warning, StartupCheckStatus.Missing, "语言目录缺失", Path.Combine(installRoot, "lang", "zh-CN")), + new StartupCheckItem("lang:root:fr-FR", "根目录语言资源 fr-FR", StartupCheckSeverity.Warning, StartupCheckStatus.Failed, "旧版误判", Path.Combine(installRoot, "fr-FR")) + ]); + await store.SaveAsync(oldReport); + + var service = new InstallIntegrityCheckService(paths, store); + + var latest = await service.GetLatestCurrentReportAsync(StartupCheckKind.FastPreflight); + var history = await service.GetCurrentHistoryAsync(10); + + Assert.IsNotNull(latest); + Assert.AreEqual(0, latest.VisibleIssueCount); + Assert.AreEqual(StartupCheckStatus.Skipped, latest.Items.First(item => item.Id == "manifest").Status); + Assert.AreEqual(StartupCheckStatus.Passed, latest.Items.First(item => item.Id == "file:resources.pri").Status); + Assert.AreEqual(StartupCheckStatus.Passed, latest.Items.First(item => item.Id == "lang:zh-CN").Status); + Assert.AreEqual(StartupCheckStatus.Skipped, latest.Items.First(item => item.Id == "lang:root:fr-FR").Status); + Assert.AreEqual(0, history[0].VisibleIssueCount); + } + + [TestMethod] + public async Task MonthlyIntegrityUsesRecentReportUntilManifestIdentityChanges() + { + using var workspace = TestWorkspace.Create("ymhut-startup-monthly"); + var installRoot = CreateHealthyInstall(workspace); + var appRoot = workspace.CreateDirectory("user"); + var now = new DateTimeOffset(2026, 6, 14, 9, 0, 0, TimeSpan.Zero); + + using var environment = EnvironmentScope.Capture(InstallLayoutPaths.InstallRootEnvironmentVariable); + environment.Set(InstallLayoutPaths.InstallRootEnvironmentVariable, installRoot); + + var paths = AppPaths.ForCurrentUser(appRoot); + var store = new StartupCheckStore(paths); + var service = new InstallIntegrityCheckService(paths, store, clock: () => now); + + var first = await service.RunMonthlyIntegrityCheckAsync(); + now = now.AddDays(5); + var cached = await service.RunMonthlyIntegrityCheckAsync(); + + Assert.AreEqual(first.Id, cached.Id); + + File.AppendAllText(Path.Combine(installRoot, InstallManifest.RelativePath.Replace('/', Path.DirectorySeparatorChar)), Environment.NewLine + "; changed"); + now = now.AddDays(1); + var changed = await service.RunMonthlyIntegrityCheckAsync(); + + Assert.AreNotEqual(first.Id, changed.Id); + } + + [TestMethod] + public async Task MonthlyIntegrityDoesNotReuseReportFromDifferentInstallRoot() + { + using var workspace = TestWorkspace.Create("ymhut-startup-monthly-root"); + var installRoot = CreateHealthyInstall(workspace); + var secondRoot = workspace.CreateDirectory("install2"); + CopyDirectory(installRoot, secondRoot); + var appRoot = workspace.CreateDirectory("user"); + var now = new DateTimeOffset(2026, 6, 14, 9, 0, 0, TimeSpan.Zero); + + using var environment = EnvironmentScope.Capture(InstallLayoutPaths.InstallRootEnvironmentVariable); + environment.Set(InstallLayoutPaths.InstallRootEnvironmentVariable, installRoot); + + var paths = AppPaths.ForCurrentUser(appRoot); + var store = new StartupCheckStore(paths); + var service = new InstallIntegrityCheckService(paths, store, clock: () => now); + var first = await service.RunMonthlyIntegrityCheckAsync(); + + environment.Set(InstallLayoutPaths.InstallRootEnvironmentVariable, secondRoot); + now = now.AddDays(1); + var second = await service.RunMonthlyIntegrityCheckAsync(); + + Assert.AreNotEqual(first.Id, second.Id); + Assert.AreNotEqual(first.InstallIdentity, second.InstallIdentity); + } + + [TestMethod] + public async Task IntegrityReportsMissingExternalToolLaunchableAsWarning() + { + using var workspace = TestWorkspace.Create("ymhut-startup-tool-missing"); + var installRoot = CreateHealthyInstall(workspace); + var appRoot = workspace.CreateDirectory("user"); + Directory.CreateDirectory(Path.Combine(installRoot, "Tools", "Sample")); + File.WriteAllText(Path.Combine(installRoot, "Tools", "Sample", "present.exe"), string.Empty); + File.AppendAllText( + Path.Combine(installRoot, InstallManifest.RelativePath.Replace('/', Path.DirectorySeparatorChar)), + Environment.NewLine + "Tools/Sample/missing.exe"); + + using var environment = EnvironmentScope.Capture(InstallLayoutPaths.InstallRootEnvironmentVariable); + environment.Set(InstallLayoutPaths.InstallRootEnvironmentVariable, installRoot); + + var paths = AppPaths.ForCurrentUser(appRoot); + var service = new InstallIntegrityCheckService(paths, new StartupCheckStore(paths)); + + var report = await service.RunManualIntegrityCheckAsync(); + var missing = report.Items.FirstOrDefault(item => item.Id == "file:Tools/Sample/missing.exe"); + + Assert.IsNotNull(missing); + Assert.AreEqual(StartupCheckStatus.Missing, missing.Status); + Assert.AreEqual(StartupCheckSeverity.Warning, missing.Severity); + } + + [TestMethod] + public async Task UserDataRuntimePayloadIsRemovedBeforePreflight() + { + using var workspace = TestWorkspace.Create("ymhut-startup-userdata"); + var installRoot = CreateHealthyInstall(workspace); + var appRoot = workspace.CreateDirectory("user"); + var runtimeRoot = Path.Combine(appRoot, "Runtime"); + var runtimesRoot = Path.Combine(appRoot, "runtimes"); + Directory.CreateDirectory(runtimeRoot); + Directory.CreateDirectory(runtimesRoot); + File.WriteAllText(Path.Combine(runtimeRoot, "YMhutBox.exe"), string.Empty); + File.WriteAllText(Path.Combine(runtimesRoot, "Microsoft.ui.xaml.dll"), string.Empty); + + using var environment = EnvironmentScope.Capture(InstallLayoutPaths.InstallRootEnvironmentVariable); + environment.Set(InstallLayoutPaths.InstallRootEnvironmentVariable, installRoot); + + var paths = AppPaths.ForCurrentUser(appRoot); + Assert.IsFalse(Directory.Exists(runtimeRoot)); + Assert.IsFalse(Directory.Exists(runtimesRoot)); + + var service = new InstallIntegrityCheckService(paths, new StartupCheckStore(paths)); + + var report = await service.RunFastPreflightAsync(); + var runtime = report.Items.FirstOrDefault(item => item.Id == "userdata:runtime"); + + Assert.IsNotNull(runtime); + Assert.AreEqual(StartupCheckStatus.Passed, runtime.Status); + Assert.IsFalse(Directory.Exists(runtimeRoot)); + Assert.IsFalse(Directory.Exists(runtimesRoot)); + } + + [TestMethod] + public async Task UserDataToolsAndMetadataPayloadsAreRemovedBeforePreflight() + { + using var workspace = TestWorkspace.Create("ymhut-startup-userdata-tools"); + var installRoot = CreateHealthyInstall(workspace); + var appRoot = workspace.CreateDirectory("user"); + + using var environment = EnvironmentScope.Capture(InstallLayoutPaths.InstallRootEnvironmentVariable); + environment.Set(InstallLayoutPaths.InstallRootEnvironmentVariable, installRoot); + + var paths = AppPaths.ForCurrentUser(appRoot); + var toolsRoot = Path.Combine(appRoot, "Tools"); + var metadataRoot = Path.Combine(appRoot, "Metadata"); + Directory.CreateDirectory(toolsRoot); + Directory.CreateDirectory(metadataRoot); + File.WriteAllText(Path.Combine(toolsRoot, "YMhutBox.exe"), string.Empty); + File.WriteAllText(Path.Combine(metadataRoot, "tools.json"), "{}"); + + var service = new InstallIntegrityCheckService(paths, new StartupCheckStore(paths)); + + var report = await service.RunFastPreflightAsync(); + var tools = report.Items.FirstOrDefault(item => item.Id == "userdata:tools"); + var metadata = report.Items.FirstOrDefault(item => item.Id == "userdata:metadata"); + + Assert.IsNotNull(tools); + Assert.IsNotNull(metadata); + Assert.AreEqual(StartupCheckStatus.Passed, tools.Status); + Assert.AreEqual(StartupCheckStatus.Passed, metadata.Status); + Assert.IsFalse(Directory.Exists(toolsRoot)); + Assert.IsFalse(Directory.Exists(metadataRoot)); + Assert.AreEqual(0, report.VisibleIssueCount); + } + + [TestMethod] + public async Task CurrentHistoryFiltersOutHistoricalInstallReports() + { + using var workspace = TestWorkspace.Create("ymhut-startup-current-history"); + var installRoot = CreateHealthyInstall(workspace); + var secondRoot = workspace.CreateDirectory("install2"); + CopyDirectory(installRoot, secondRoot); + var appRoot = workspace.CreateDirectory("user"); + + using var environment = EnvironmentScope.Capture(InstallLayoutPaths.InstallRootEnvironmentVariable); + environment.Set(InstallLayoutPaths.InstallRootEnvironmentVariable, installRoot); + + var paths = AppPaths.ForCurrentUser(appRoot); + var store = new StartupCheckStore(paths); + var service = new InstallIntegrityCheckService(paths, store); + var oldReport = await service.RunManualIntegrityCheckAsync(); + + environment.Set(InstallLayoutPaths.InstallRootEnvironmentVariable, secondRoot); + var currentReport = await service.RunManualIntegrityCheckAsync(); + var all = await service.GetHistorySummariesAsync(); + var current = await service.GetCurrentHistoryAsync(); + + Assert.IsTrue(all.Any(summary => summary.Id == oldReport.Id && !summary.IsCurrentInstall)); + Assert.IsTrue(current.All(summary => summary.IsCurrentInstall)); + Assert.IsTrue(current.Any(summary => summary.Id == currentReport.Id)); + Assert.IsFalse(current.Any(summary => summary.Id == oldReport.Id)); + } + + [TestMethod] + public async Task RecheckItemReportsCurrentPathPassesAfterFileRestored() + { + using var workspace = TestWorkspace.Create("ymhut-startup-recheck"); + var installRoot = CreateHealthyInstall(workspace); + var appRoot = workspace.CreateDirectory("user"); + var executable = Path.Combine(installRoot, "YMhutBox.exe"); + File.Delete(executable); + + using var environment = EnvironmentScope.Capture(InstallLayoutPaths.InstallRootEnvironmentVariable); + environment.Set(InstallLayoutPaths.InstallRootEnvironmentVariable, installRoot); + + var paths = AppPaths.ForCurrentUser(appRoot); + var service = new InstallIntegrityCheckService(paths, new StartupCheckStore(paths)); + var report = await service.RunManualIntegrityCheckAsync(); + var missing = report.Items.First(item => item.Id == "file:YMhutBox.exe"); + + File.WriteAllText(executable, string.Empty); + var rechecked = await service.RecheckItemAsync(report.Id, missing.Id); + + Assert.IsNotNull(rechecked); + Assert.AreEqual(StartupCheckStatus.Passed, rechecked.Status); + StringAssert.Contains(rechecked.Detail, "old record"); + } + + [TestMethod] + public async Task IntegrityDoesNotTreatNormalRootFoldersAsCultureFolders() + { + using var workspace = TestWorkspace.Create("ymhut-startup-root-folders"); + var installRoot = CreateHealthyInstall(workspace); + var appRoot = workspace.CreateDirectory("user"); + Directory.CreateDirectory(Path.Combine(installRoot, "Assets")); + + using var environment = EnvironmentScope.Capture(InstallLayoutPaths.InstallRootEnvironmentVariable); + environment.Set(InstallLayoutPaths.InstallRootEnvironmentVariable, installRoot); + + var paths = AppPaths.ForCurrentUser(appRoot); + var service = new InstallIntegrityCheckService(paths, new StartupCheckStore(paths)); + + var report = await service.RunManualIntegrityCheckAsync(); + + Assert.IsFalse(report.Items.Any(item => string.Equals(item.Id, "lang:root:Assets", StringComparison.OrdinalIgnoreCase))); + Assert.IsFalse(report.Items.Any(item => string.Equals(item.Id, "lang:root:Tools", StringComparison.OrdinalIgnoreCase))); + } + + [TestMethod] + public async Task StartupCheckStorePersistsHistoryAndLatestReport() + { + using var workspace = TestWorkspace.Create("ymhut-startup-store"); + var installRoot = CreateHealthyInstall(workspace); + var appRoot = workspace.CreateDirectory("user"); + + using var environment = EnvironmentScope.Capture(InstallLayoutPaths.InstallRootEnvironmentVariable); + environment.Set(InstallLayoutPaths.InstallRootEnvironmentVariable, installRoot); + + var paths = AppPaths.ForCurrentUser(appRoot); + var store = new StartupCheckStore(paths); + var service = new InstallIntegrityCheckService(paths, store); + + var report = await service.RunManualIntegrityCheckAsync(); + var latest = await store.GetLatestReportAsync(); + var history = await store.GetHistoryAsync(); + var summaries = await store.GetHistorySummariesAsync(); + var loaded = await store.GetReportAsync(report.Id); + + Assert.IsNotNull(latest); + Assert.AreEqual(report.Id, latest.Id); + Assert.IsGreaterThanOrEqualTo(history.Count, 1); + Assert.IsNotEmpty(history[0].Items); + Assert.IsGreaterThanOrEqualTo(summaries.Count, 1); + Assert.AreEqual(report.Id, summaries[0].Id); + Assert.AreEqual(report.Items.Count, summaries[0].ItemCount); + Assert.AreEqual(report.IssueCount, summaries[0].IssueCount); + Assert.IsNotNull(loaded); + Assert.AreEqual(report.Id, loaded.Id); + Assert.IsNotEmpty(loaded.Items); + } + + [TestMethod] + public async Task StartupCheckStoreMigratesLegacyDatabaseIntoMainSqlite() + { + using var workspace = TestWorkspace.Create("ymhut-startup-store-migrate"); + var installRoot = CreateHealthyInstall(workspace); + var appRoot = workspace.CreateDirectory("user"); + + using var environment = EnvironmentScope.Capture(InstallLayoutPaths.InstallRootEnvironmentVariable); + environment.Set(InstallLayoutPaths.InstallRootEnvironmentVariable, installRoot); + + var paths = AppPaths.ForCurrentUser(appRoot); + var legacyPath = Path.Combine(paths.Data, "startup-checks.db"); + Directory.CreateDirectory(Path.GetDirectoryName(legacyPath)!); + await using (var connection = new SqliteConnection($"Data Source={legacyPath};Pooling=False")) + { + connection.Open(); + await using var command = connection.CreateCommand(); + command.CommandText = """ + CREATE TABLE startup_check_reports ( + id TEXT PRIMARY KEY, + kind TEXT NOT NULL, + started_at TEXT NOT NULL, + completed_at TEXT NOT NULL, + install_root TEXT NOT NULL, + manifest_identity TEXT NOT NULL, + issue_count INTEGER NOT NULL, + critical_issue_count INTEGER NOT NULL, + warning_issue_count INTEGER NOT NULL + ); + CREATE TABLE startup_check_items ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + report_id TEXT NOT NULL, + item_id TEXT NOT NULL, + name TEXT NOT NULL, + severity TEXT NOT NULL, + status TEXT NOT NULL, + detail TEXT NOT NULL, + path TEXT NULL + ); + INSERT INTO startup_check_reports( + id, kind, started_at, completed_at, install_root, manifest_identity, + issue_count, critical_issue_count, warning_issue_count) + VALUES( + '11111111111111111111111111111111', + 'FastPreflight', + '2026-06-01T00:00:00.0000000+00:00', + '2026-06-01T00:00:01.0000000+00:00', + 'C:\Legacy', + 'legacy', + 1, + 0, + 1); + INSERT INTO startup_check_items(report_id, item_id, name, severity, status, detail, path) + VALUES( + '11111111111111111111111111111111', + 'legacy:item', + '旧记录', + 'Warning', + 'Failed', + '从旧库导入', + NULL); + """; + await command.ExecuteNonQueryAsync(); + } + + var store = new StartupCheckStore(paths); + var latest = await store.GetLatestReportAsync(); + + Assert.IsNotNull(latest); + Assert.AreEqual("legacy", latest.ManifestIdentity); + Assert.AreEqual(1, latest.Items.Count); + Assert.IsFalse(File.Exists(legacyPath)); + Assert.AreEqual(Path.Combine(paths.Logs, AppDatabasePaths.MainDatabaseFileName), store.DatabasePath); + } + + [TestMethod] + public async Task StartupInitializationPipelineReportsMonotonicWeightedProgress() + { + var progress = new List<StartupInitializationStageProgress>(); + var pipeline = new StartupInitializationPipeline([ + new StartupInitializationStage("first", "First", 1, true, (context, token) => + { + context.Report(0.5, "Half"); + return Task.CompletedTask; + }), + new StartupInitializationStage("second", "Second", 3, true, (context, token) => + { + context.Report(0.25, "Quarter"); + context.Report(0.75, "Almost"); + return Task.CompletedTask; + }) + ]); + + await pipeline.RunAsync(new InlineProgress<StartupInitializationStageProgress>(progress.Add)); + + Assert.IsTrue(progress.Count > 0); + Assert.AreEqual(100, progress[^1].Progress); + for (var index = 1; index < progress.Count; index++) + { + Assert.IsTrue( + progress[index].Progress >= progress[index - 1].Progress, + $"Progress moved backwards at {index}: {progress[index - 1].Progress} -> {progress[index].Progress}"); + } + + CollectionAssert.AreEqual(new[] { "first", "second" }, pipeline.Stages.Select(stage => stage.Id).ToArray()); + } + + [TestMethod] + public void StartupSplashMessageSerializesStableContract() + { + var message = new StartupSplashMessage( + "progress", + "正在检查", + 42, + "fast-preflight", + "快速预检", + 8, + 10, + "检查关键文件", + "warning", + IsRepairStep: true, + Theme: "dark", + ReducedMotion: true); + + var json = message.ToJson(); + + StringAssert.Contains(json, "\"type\": \"progress\""); + StringAssert.Contains(json, "\"stageId\": \"fast-preflight\""); + StringAssert.Contains(json, "\"isRepairStep\": true"); + StringAssert.Contains(json, "\"reducedMotion\": true"); + } + + private static string CreateHealthyInstall(TestWorkspace workspace) + { + var installRoot = workspace.CreateDirectory("install"); + workspace.CreateFile(["install", "YMhutBox.exe"], string.Empty); + workspace.CreateFile(["install", "YMhutBox.dll"], string.Empty); + workspace.CreateFile(["install", "resources.pri"], string.Empty); + workspace.CreateFile(["install", "lang", "zh-CN", "Microsoft.ui.xaml.dll.mui"], string.Empty); + workspace.CreateFile(["install", "lang", "en-US", "Microsoft.ui.xaml.dll.mui"], string.Empty); + workspace.CreateFile(["install", "Tools", "Sample", "tool.exe"], string.Empty); + workspace.CreateFile(["install", "Metadata", "tools.json"], """{ "tools": [] }"""); + workspace.CreateFile(["install", "config", "install-manifest.ini"], + """ + [Release] + Version=2.0.6 + Build=1 + Channel=stable + PackageVersion=2.0.6.1 + + [RequiredFiles] + YMhutBox.exe + YMhutBox.dll + resources.pri + + [Files] + YMhutBox.exe + YMhutBox.dll + resources.pri + lang/zh-CN/Microsoft.ui.xaml.dll.mui + lang/en-US/Microsoft.ui.xaml.dll.mui + Tools/Sample/tool.exe + Metadata/tools.json + """); + return installRoot; + } + + private static string CreateDevelopmentInstall(TestWorkspace workspace) + { + var installRoot = workspace.CreateDirectory("install"); + workspace.CreateFile(["install", "YMhutBox.exe"], string.Empty); + workspace.CreateFile(["install", "YMhutBox.dll"], string.Empty); + workspace.CreateFile(["install", "zh-CN", "Microsoft.ui.xaml.dll.mui"], string.Empty); + workspace.CreateFile(["install", "en-US", "Microsoft.ui.xaml.dll.mui"], string.Empty); + return installRoot; + } + + private static void CopyDirectory(string source, string destination) + { + Directory.CreateDirectory(destination); + foreach (var directory in Directory.EnumerateDirectories(source, "*", SearchOption.AllDirectories)) + { + Directory.CreateDirectory(Path.Combine(destination, Path.GetRelativePath(source, directory))); + } + + foreach (var file in Directory.EnumerateFiles(source, "*", SearchOption.AllDirectories)) + { + var target = Path.Combine(destination, Path.GetRelativePath(source, file)); + Directory.CreateDirectory(Path.GetDirectoryName(target)!); + File.Copy(file, target, overwrite: true); + } + } + + private sealed class EnvironmentScope : IDisposable + { + private readonly Dictionary<string, string?> _snapshot; + + private EnvironmentScope(IEnumerable<string> names) + { + _snapshot = names.ToDictionary(name => name, Environment.GetEnvironmentVariable, StringComparer.Ordinal); + } + + public static EnvironmentScope Capture(params string[] names) => new(names); + + public void Set(string name, string? value) => Environment.SetEnvironmentVariable(name, value); + + public void Dispose() + { + foreach (var item in _snapshot) + { + Environment.SetEnvironmentVariable(item.Key, item.Value); + } + } + } + + private sealed class InlineProgress<T>(Action<T> report) : IProgress<T> + { + public void Report(T value) => report(value); + } + + private sealed class TestWorkspace : IDisposable + { + private TestWorkspace(string root) + { + Root = root; + } + + public string Root { get; } + + public static TestWorkspace Create(string prefix) + { + var root = Path.Combine(Path.GetTempPath(), prefix, Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(root); + return new TestWorkspace(root); + } + + public string CreateDirectory(params string[] parts) + { + var directory = Path.Combine([Root, .. parts]); + Directory.CreateDirectory(directory); + return directory; + } + + public string CreateFile(string[] parts, string contents) + { + var path = Path.Combine([Root, .. parts]); + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + File.WriteAllText(path, contents); + return path; + } + + public void Dispose() + { + if (Directory.Exists(Root)) + { + Directory.Delete(Root, recursive: true); + } + } + } +} diff --git a/src/YMhut.Box.Tests/ToolCatalogTests.cs b/src/YMhut.Box.Tests/ToolCatalogTests.cs new file mode 100644 index 0000000..960a0bb --- /dev/null +++ b/src/YMhut.Box.Tests/ToolCatalogTests.cs @@ -0,0 +1,298 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using YMhut.Box.Core.Tools; + +namespace YMhut.Box.Tests; + +[TestClass] +public sealed class ToolCatalogTests +{ + [TestMethod] + public void DefaultCatalogContainsRequiredNativePages() + { + var catalog = new ToolCatalog(); + + Assert.IsNotNull(catalog.GetById("safe_browser")); + Assert.IsNotNull(catalog.GetById("media_player")); + Assert.IsNotNull(catalog.GetById("text_splitter")); + Assert.IsNotNull(catalog.GetById("loan_emi_calculator")); + Assert.IsNotNull(catalog.GetById("number_base_converter")); + Assert.IsNotNull(catalog.GetById("ymhut_uutool_suite")); + Assert.IsNotNull(catalog.GetById("html_js_playground")); + Assert.IsNotNull(catalog.GetById("timezone_abbr_lookup")); + Assert.IsNotNull(catalog.GetById("percentage_change_calculator")); + Assert.IsNotNull(catalog.GetById("dev_environment_config")); + Assert.IsNull(catalog.GetById("mmd_model_studio")); + foreach (var id in new[] + { + "compression_codec", + "totp_generator", + "color_contrast_checker", + "url_redirect_trace", + "image_metadata_inspector", + "markdown_preview" + }) + { + Assert.IsNotNull(catalog.GetById(id), id); + } + + Assert.IsGreaterThanOrEqualTo(160, catalog.Modules.Count); + } + + [TestMethod] + public void SearchFiltersByQueryAndCategory() + { + var catalog = new ToolCatalog(); + + var results = catalog.Search("browser", ToolCategory.Network).ToList(); + + Assert.HasCount(1, results); + Assert.AreEqual("safe_browser", results[0].Id); + } + + [TestMethod] + public void ToolViewModelStartsWithInlinePreviewHidden() + { + var module = new ToolCatalog().GetById("json_formatter")!; + + var viewModel = (ToolModuleViewModel)module.CreateViewModel(); + + Assert.IsFalse(viewModel.InlinePreviewLoaded); + } + + [TestMethod] + public void EveryCatalogToolHasNativeWinUiPageSpec() + { + var catalog = new ToolCatalog(); + var specs = ToolPageSpecCatalog.CreateFor(catalog); + + Assert.HasCount(catalog.Modules.Count, specs); + foreach (var module in catalog.Modules) + { + Assert.IsTrue(specs.ContainsKey(module.Id), module.Id); + var spec = specs[module.Id]; + Assert.AreEqual(module.Id, spec.ToolId); + Assert.IsTrue(Enum.IsDefined(spec.Layout), module.Id); + Assert.IsNotEmpty(spec.ParameterControls, module.Id); + Assert.IsTrue(Enum.IsDefined(spec.AssistPanelMode), module.Id); + Assert.IsTrue(Enum.IsDefined(spec.InteractionMode), module.Id); + Assert.IsTrue(Enum.IsDefined(spec.PageExperience), module.Id); + Assert.IsTrue(Enum.IsDefined(spec.PageDensity), module.Id); + Assert.IsTrue(Enum.IsDefined(spec.ResultPriority), module.Id); + Assert.IsFalse(string.IsNullOrWhiteSpace(spec.DefaultFocusTarget), module.Id); + if (spec.PageExperience != ToolPageExperienceKind.LivePreview) + { + Assert.IsFalse(string.IsNullOrWhiteSpace(spec.PrimaryActionLabel), module.Id); + } + if (spec.PrimaryInput != ToolPrimaryInputKind.None) + { + Assert.IsNotEmpty(spec.Parameters, module.Id); + } + Assert.IsFalse(string.IsNullOrWhiteSpace(spec.EmptyState), module.Id); + Assert.IsFalse(string.IsNullOrWhiteSpace(spec.ErrorHint), module.Id); + } + } + + [TestMethod] + public void RepresentativeToolSpecsUseGalleryNativeControls() + { + var catalog = new ToolCatalog(); + + Assert.AreEqual(ToolPrimaryInputKind.MultilineText, ToolPageSpecCatalog.For(catalog.GetById("json_formatter")!).PrimaryInput); + Assert.AreEqual(ToolResultKind.JsonTree, ToolPageSpecCatalog.For(catalog.GetById("json_formatter")!).Result); + Assert.AreEqual(ToolPrimaryInputKind.FixedOptions, ToolPageSpecCatalog.For(catalog.GetById("system_tool")!).PrimaryInput); + Assert.AreEqual(ToolLayoutKind.SystemLauncher, ToolPageSpecCatalog.For(catalog.GetById("system_tool")!).Layout); + Assert.AreEqual(ToolPrimaryInputKind.None, ToolPageSpecCatalog.For(catalog.GetById("baidu_hot")!).PrimaryInput); + Assert.IsTrue(ToolPageSpecCatalog.For(catalog.GetById("archive_tool")!).RequiresFilePicker); + Assert.IsTrue(ToolPageSpecCatalog.For(catalog.GetById("image_metadata_inspector")!).RequiresFilePicker); + Assert.AreEqual(ToolLayoutKind.CodecWorkbench, ToolPageSpecCatalog.For(catalog.GetById("compression_codec")!).Layout); + Assert.AreEqual(ToolLayoutKind.SecurityWorkbench, ToolPageSpecCatalog.For(catalog.GetById("totp_generator")!).Layout); + Assert.AreEqual(ToolLayoutKind.TextWorkbench, ToolPageSpecCatalog.For(catalog.GetById("color_contrast_checker")!).Layout); + Assert.AreEqual(ToolLayoutKind.QueryDashboard, ToolPageSpecCatalog.For(catalog.GetById("url_redirect_trace")!).Layout); + Assert.AreEqual(ToolLayoutKind.FormatterWorkbench, ToolPageSpecCatalog.For(catalog.GetById("markdown_preview")!).Layout); + Assert.AreEqual(ToolLayoutKind.RandomCinema, ToolPageSpecCatalog.For(catalog.GetById("random_cinema")!).Layout); + Assert.AreEqual(ToolCategory.Image, catalog.GetById("random_cinema")!.Metadata.Category); + } + + [TestMethod] + public void ModelStudioToolIsRemovedFromCatalogAndSpecs() + { + var catalog = new ToolCatalog(); + + Assert.IsNull(catalog.GetById("mmd_model_studio")); + Assert.IsFalse(ToolPageSpecCatalog.CreateFor(catalog).ContainsKey("mmd_model_studio")); + } + + [TestMethod] + public void ToolAssistAndInteractionModesMatchRepresentativeExperience() + { + var catalog = new ToolCatalog(); + + Assert.AreEqual(ToolAssistPanelMode.Custom, ToolPageSpecCatalog.For(catalog.GetById("color_picker")!).AssistPanelMode); + Assert.AreEqual(ToolInteractionMode.LivePreview, ToolPageSpecCatalog.For(catalog.GetById("color_picker")!).InteractionMode); + Assert.AreEqual(ToolPageExperienceKind.LivePreview, ToolPageSpecCatalog.For(catalog.GetById("color_picker")!).PageExperience); + Assert.AreEqual(ToolAssistPanelMode.Hidden, ToolPageSpecCatalog.For(catalog.GetById("system_info")!).AssistPanelMode); + Assert.AreEqual(ToolAssistPanelMode.Hidden, ToolPageSpecCatalog.For(catalog.GetById("baidu_hot")!).AssistPanelMode); + Assert.AreEqual(ToolAssistPanelMode.RulesOnly, ToolPageSpecCatalog.For(catalog.GetById("json_formatter")!).AssistPanelMode); + Assert.AreEqual(ToolAssistPanelMode.RulesOnly, ToolPageSpecCatalog.For(catalog.GetById("base64_codec")!).AssistPanelMode); + Assert.AreEqual(ToolAssistPanelMode.SourceOnly, ToolPageSpecCatalog.For(catalog.GetById("weather")!).AssistPanelMode); + Assert.AreEqual(ToolInteractionMode.ReferenceBrowser, ToolPageSpecCatalog.For(catalog.GetById("mime_lookup")!).InteractionMode); + Assert.AreEqual(ToolPageExperienceKind.RemoteDashboard, ToolPageSpecCatalog.For(catalog.GetById("baidu_hot")!).PageExperience); + Assert.AreEqual(ToolPageExperienceKind.FormCalculator, ToolPageSpecCatalog.For(catalog.GetById("bmi_calculator")!).PageExperience); + Assert.AreEqual(ToolPageExperienceKind.FileWorkflow, ToolPageSpecCatalog.For(catalog.GetById("archive_tool")!).PageExperience); + } + + [TestMethod] + public void RuleDrivenToolsHaveRuleSpecsAndSourceOnlyCopyIsSanitized() + { + var catalog = new ToolCatalog(); + var specs = ToolPageSpecCatalog.CreateFor(catalog); + + foreach (var spec in specs.Values.Where(spec => spec.AssistPanelMode == ToolAssistPanelMode.RulesOnly)) + { + Assert.IsTrue(spec.Rules.Count > 0 || spec.RequiresFilePicker || spec.ParameterControls.Contains(ToolParameterControlKind.NumberBox), spec.ToolId); + } + + foreach (var spec in specs.Values.Where(spec => spec.AssistPanelMode == ToolAssistPanelMode.SourceOnly)) + { + var uiCopy = $"{spec.EmptyState} {spec.ErrorHint}"; + Assert.IsFalse(uiCopy.Contains("http://", StringComparison.OrdinalIgnoreCase), spec.ToolId); + Assert.IsFalse(uiCopy.Contains("https://", StringComparison.OrdinalIgnoreCase), spec.ToolId); + Assert.IsFalse(uiCopy.Contains(".json", StringComparison.OrdinalIgnoreCase), spec.ToolId); + } + } + + [TestMethod] + public void RuleAndParameterSpecsCarryBilingualText() + { + var catalog = new ToolCatalog(); + var json = ToolPageSpecCatalog.For(catalog.GetById("json_formatter")!); + var port = ToolPageSpecCatalog.For(catalog.GetById("port_lookup")!); + + Assert.AreEqual("模式", json.Rules[0].LocalizedLabel?.ForLanguage("zh-CN")); + Assert.AreEqual("Mode", json.Rules[0].LocalizedLabel?.ForLanguage("en-US")); + Assert.AreEqual("格式化", json.Rules[0].Options[0].LocalizedLabel?.ForLanguage("zh-CN")); + Assert.AreEqual("Format", json.Rules[0].Options[0].LocalizedLabel?.ForLanguage("en-US")); + Assert.AreEqual("来源", port.Rules[0].LocalizedLabel?.ForLanguage("zh-CN")); + Assert.AreEqual("Source", port.Rules[0].LocalizedLabel?.ForLanguage("en-US")); + Assert.AreEqual("预设", port.Parameters[0].LocalizedLabel?.ForLanguage("zh-CN")); + Assert.AreEqual("Preset", port.Parameters[0].LocalizedLabel?.ForLanguage("en-US")); + } + + [TestMethod] + public void PageExperienceRulesKeepToolPagesCompact() + { + var catalog = new ToolCatalog(); + + var color = ToolPageSpecCatalog.For(catalog.GetById("color_picker")!); + Assert.AreEqual(ToolPageExperienceKind.LivePreview, color.PageExperience); + Assert.AreEqual(string.Empty, color.PrimaryActionLabel); + + var hot = ToolPageSpecCatalog.For(catalog.GetById("hotboard")!); + Assert.AreEqual(ToolPageExperienceKind.RemoteDashboard, hot.PageExperience); + Assert.IsFalse(hot.AutoRunOnOpen); + Assert.AreEqual("查询", hot.PrimaryActionLabel); + Assert.IsTrue(hot.ShowInputHeader); + + var bmi = ToolPageSpecCatalog.For(catalog.GetById("bmi_calculator")!); + Assert.AreEqual(ToolPageExperienceKind.FormCalculator, bmi.PageExperience); + Assert.AreEqual("计算", bmi.PrimaryActionLabel); + Assert.AreNotEqual(ToolPrimaryInputKind.MultilineText, bmi.PrimaryInput); + + var json = ToolPageSpecCatalog.For(catalog.GetById("json_formatter")!); + Assert.AreEqual(ToolPageExperienceKind.OneShotTransform, json.PageExperience); + Assert.AreEqual("格式化", json.PrimaryActionLabel); + } + + [TestMethod] + public void CalculatorToolSpecsUseNativeFormInputsByArity() + { + var catalog = new ToolCatalog(); + + Assert.AreEqual(ToolPrimaryInputKind.NumberPair, ToolPageSpecCatalog.For(catalog.GetById("bmi_calculator")!).PrimaryInput); + Assert.AreEqual(ToolPrimaryInputKind.NumberTriple, ToolPageSpecCatalog.For(catalog.GetById("loan_emi_calculator")!).PrimaryInput); + Assert.AreEqual(ToolPrimaryInputKind.NumberQuad, ToolPageSpecCatalog.For(catalog.GetById("compound_interest_calculator")!).PrimaryInput); + Assert.AreEqual(ToolPrimaryInputKind.DateRange, ToolPageSpecCatalog.For(catalog.GetById("shelf_life")!).PrimaryInput); + Assert.IsTrue(ToolPageSpecCatalog.For(catalog.GetById("shelf_life")!).UsesNumberBox); + } + + [TestMethod] + public void SpecsCoverEveryStructuredResultKind() + { + var catalog = new ToolCatalog(); + var resultKinds = catalog.Modules + .Select(module => ToolPageSpecCatalog.For(module).Result) + .Distinct() + .ToHashSet(); + + foreach (ToolResultKind kind in Enum.GetValues<ToolResultKind>()) + { + Assert.Contains(kind, resultKinds, kind.ToString()); + } + } + + [TestMethod] + public async Task ToolExecutorReturnsStructuredDocuments() + { + var catalog = new ToolCatalog(); + var samples = new Dictionary<string, string> + { + ["json_formatter"] = "{\"name\":\"YMhut Box\"}", + ["base64_codec"] = "YMhut Box", + ["bmi_calculator"] = "70 1.75", + ["text_diff"] = "WinUI\n---\nWinUI 3", + ["color_picker"] = "#3399ff" + }; + + foreach (var sample in samples) + { + var module = catalog.GetById(sample.Key)!; + var result = await ToolExecutor.ExecuteAsync(module, sample.Value); + + Assert.IsTrue(result.Ok, sample.Key); + Assert.IsNotNull(result.Document, sample.Key); + Assert.AreEqual(module.Id, result.Document.ToolId); + Assert.IsNotEmpty(result.Document.Blocks, sample.Key); + Assert.AreEqual(result.Output, result.Document.RawText); + } + } + + [TestMethod] + public void EveryResultKindBuildsStructuredDocumentBlocks() + { + var catalog = new ToolCatalog(); + var samples = new Dictionary<string, string> + { + ["ascii_art"] = "YMhut", + ["xml_formatter"] = "<root><name>YMhut</name></root>", + ["json_formatter"] = "{\"name\":\"YMhut\"}", + ["base64_codec"] = "Encode:\nWU1odXQ=", + ["regex_tool"] = "match | value\n1 | YMhut", + ["hotboard"] = "1. YMhut 热点", + ["cctv_news"] = "1. 新闻标题", + ["qr_generator"] = "https://example.com/qr.png", + ["smart_search"] = "Bing - https://www.bing.com/search?q=YMhut", + ["archive_tool"] = "已创建归档:sample.zip", + ["text_diff"] = "- old\n+ new", + ["hash_manifest_verify"] = "OK hello", + ["color_picker"] = "HEX: #336699\nRGB: 51, 102, 153", + ["number_base"] = "DEC: 255\nHEX: 0xFF", + ["system_tool"] = "OS: Windows\nProcessors: 8", + ["random_cinema"] = "https://example.com/media.mp4", + ["port_lookup"] = "数据源:YMhut Reference Data / IANA\n80 - HTTP: 网页服务" + }; + + foreach (ToolResultKind kind in Enum.GetValues<ToolResultKind>()) + { + var module = catalog.Modules.FirstOrDefault(module => ToolPageSpecCatalog.For(module).Result == kind); + Assert.IsNotNull(module, kind.ToString()); + var output = samples.TryGetValue(module.Id, out var sample) ? sample : "Key: Value\nItem: Result"; + + var document = ToolResultBuilder.FromOutput(module, output, "zh-CN"); + + Assert.AreEqual(module.Id, document.ToolId, kind.ToString()); + Assert.AreEqual(kind, document.ResultKind, kind.ToString()); + Assert.IsNotEmpty(document.Blocks, kind.ToString()); + Assert.AreEqual("ok", document.Status, kind.ToString()); + } + } +} diff --git a/src/YMhut.Box.Tests/ToolExecutorTests.cs b/src/YMhut.Box.Tests/ToolExecutorTests.cs new file mode 100644 index 0000000..b34e11e --- /dev/null +++ b/src/YMhut.Box.Tests/ToolExecutorTests.cs @@ -0,0 +1,966 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.IO.Compression; +using System.Text.Json; +using YMhut.Box.Core.App; +using YMhut.Box.Core.Api; +using YMhut.Box.Core.Data; +using YMhut.Box.Core.Net; +using YMhut.Box.Core.Tools; + +namespace YMhut.Box.Tests; + +[TestClass] +public sealed class ToolExecutorTests +{ + [TestMethod] + public async Task JsonFormatterFormatsInput() + { + var module = new ToolCatalog().GetById("json_formatter")!; + + var result = await ToolExecutor.ExecuteAsync(module, "{\"name\":\"YMhut\"}"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "\"name\": \"YMhut\""); + } + + [TestMethod] + public async Task NumberBaseSupportsCatalogId() + { + var module = new ToolCatalog().GetById("number_base")!; + + var result = await ToolExecutor.ExecuteAsync(module, "255"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "HEX: 0xFF"); + } + + [TestMethod] + public async Task NumberBaseConverterSupportsPlanId() + { + var module = new ToolCatalog().GetById("number_base_converter")!; + + var result = await ToolExecutor.ExecuteAsync(module, "255"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "HEX: 0xFF"); + } + + [TestMethod] + public async Task UuToolSuiteListsNativeTools() + { + var module = new ToolCatalog().GetById("ymhut_uutool_suite")!; + + var result = await ToolExecutor.ExecuteAsync(module, "json"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "YMhut UU Tool Suite"); + StringAssert.Contains(result.Output, "json_formatter"); + } + + [TestMethod] + public async Task ApiToolUsesApiManagerEndpoint() + { + var module = new ToolCatalog().GetById("dns_query")!; + var manager = new ApiManager(new FakeHttpService(""" + { + "Status": 0, + "Question": [{ "name": "example.com.", "type": 1 }], + "Answer": [{ "name": "example.com.", "type": 1, "TTL": 300, "data": "1.2.3.4" }] + } + """)); + + var result = await ToolExecutor.ExecuteAsync(module, "example.com", apiManager: manager); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "Google Public DNS"); + StringAssert.Contains(result.Output, "1.2.3.4"); + Assert.IsNotNull(result.Document); + Assert.IsTrue(result.Document.Blocks.Any(block => block.Kind == ToolResultBlockKind.Table)); + } + + [TestMethod] + public async Task QqProfileUsesNewUserInfoSourceAndDisplaysReturnedAvatar() + { + var module = new ToolCatalog().GetById("qq_avatar")!; + var manager = new ApiManager(new FakeHttpService(""" + { + "qq": "10001", + "nickname": "Xiao Ming", + "long_nick": "Good day", + "avatar_url": "http://q.qlogo.cn/g?b=qq&nk=10001&s=640", + "age": 25, + "sex": "male", + "qid": "xiaoming2024", + "qq_level": 64, + "location": "Guangdong Shenzhen", + "email": "10001@qq.com", + "is_vip": true, + "is_years_vip": true, + "is_svip": false, + "is_big_club": true, + "vip_level": 7, + "reg_time": "2008-03-15T10:30:00Z", + "last_updated": "2024-08-14T15:45:30Z" + } + """)); + + var result = await ToolExecutor.ExecuteAsync(module, "10001", apiManager: manager, language: "zh-CN"); + + Assert.IsTrue(result.Ok); + Assert.IsNotNull(result.Document); + Assert.AreEqual("Uapis QQ User Info", result.Document.SourceName); + Assert.IsTrue(ApiEndpoints.TryGet("qq_avatar", out var endpoint)); + Assert.IsTrue(endpoint.ShouldHideEndpoint); + Assert.IsFalse(result.Document.Metadata.ContainsKey("sourceUrl")); + var avatar = result.Document.Blocks.FirstOrDefault(block => block.Kind == ToolResultBlockKind.Image); + Assert.IsNotNull(avatar); + Assert.AreEqual("http://q.qlogo.cn/g?b=qq&nk=10001&s=640", avatar.Uri); + var pairs = result.Document.Blocks.SelectMany(block => block.Pairs).ToArray(); + Assert.IsTrue(pairs.Any(pair => pair.Value == "64")); + Assert.IsTrue(pairs.Any(pair => pair.Key.Contains("VIP", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrWhiteSpace(pair.Value))); + Assert.IsFalse(result.Output.Contains("uapis.cn/api/v1/social/qq/userinfo", StringComparison.OrdinalIgnoreCase)); + } + + [TestMethod] + public async Task QrGeneratorOutputsSvg() + { + var module = new ToolCatalog().GetById("qr_generator")!; + + var result = await ToolExecutor.ExecuteAsync(module, "YMhut Box"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "<svg"); + Assert.IsNotNull(result.Document); + Assert.IsTrue(result.Document.Blocks.Any(block => + block.Kind == ToolResultBlockKind.Image && + block.Metadata is not null && + block.Metadata.TryGetValue("contentType", out var contentType) && + contentType == "image/svg+xml")); + Assert.IsTrue(result.Document.Blocks.Any(block => + block.Kind == ToolResultBlockKind.Image && + block.Metadata is not null && + block.Metadata.TryGetValue("pngBase64", out var pngBase64) && + Convert.TryFromBase64String(pngBase64, new byte[pngBase64.Length], out _))); + Assert.AreEqual(ToolResultKind.ImagePreview, result.Document.ResultKind); + StringAssert.Contains(result.Document.RawText, "<svg"); + } + + [TestMethod] + public async Task TextDiffMarksAddedAndRemovedLines() + { + var module = new ToolCatalog().GetById("text_diff")!; + + var result = await ToolExecutor.ExecuteAsync(module, "alpha\nbeta\n---\nalpha\ngamma"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "- beta"); + StringAssert.Contains(result.Output, "+ gamma"); + } + + [TestMethod] + public async Task ColorPickerConvertsHexToRgb() + { + var module = new ToolCatalog().GetById("color_picker")!; + + var result = await ToolExecutor.ExecuteAsync(module, "#336699"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "RGB: 51, 102, 153"); + } + + [TestMethod] + public async Task AsciiArtRendersBlockText() + { + var module = new ToolCatalog().GetById("ascii_art")!; + + var result = await ToolExecutor.ExecuteAsync(module, "YM"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "#"); + } + + [TestMethod] + public async Task ProfanitySupportsCustomTerms() + { + var module = new ToolCatalog().GetById("profanity_check")!; + + var result = await ToolExecutor.ExecuteAsync(module, "secret\n---\nthis is a secret token"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "Potential matches: secret"); + StringAssert.Contains(result.Output, "******"); + } + + [TestMethod] + public async Task ArchiveCreatesZipForFile() + { + var tempRoot = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(tempRoot); + try + { + var file = Path.Combine(tempRoot, "sample.txt"); + File.WriteAllText(file, "hello"); + var module = new ToolCatalog().GetById("archive_tool")!; + + var result = await ToolExecutor.ExecuteAsync(module, file); + + Assert.IsTrue(result.Ok); + Assert.HasCount(1, Directory.GetFiles(tempRoot, "*.zip")); + } + finally + { + Directory.Delete(tempRoot, recursive: true); + } + } + + [TestMethod] + public async Task QqAvatarBuildsStaticUrls() + { + var module = new ToolCatalog().GetById("qq_avatar")!; + + var result = await ToolExecutor.ExecuteAsync(module, "10000"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "qlogo.cn"); + Assert.IsNotNull(result.Document); + Assert.IsTrue(result.Document.Blocks.Any(block => + block.Kind == ToolResultBlockKind.Image && + block.Metadata is not null && + block.Metadata.TryGetValue("displayMode", out var displayMode) && + displayMode == "preview")); + Assert.IsTrue(result.Document.Blocks.Any(block => + block.Kind is ToolResultBlockKind.KeyValue or ToolResultBlockKind.Metric && + block.Pairs.Any(pair => pair.Key.Equals("QQ", StringComparison.OrdinalIgnoreCase) && pair.Value == "10000"))); + } + + [TestMethod] + public async Task MarkdownTableNormalizerAlignsColumns() + { + var module = new ToolCatalog().GetById("markdown_table_normalizer")!; + + var result = await ToolExecutor.ExecuteAsync(module, "| a | long |\n|---|---|\n| 1 | 2 |"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "| a | long |"); + StringAssert.Contains(result.Output, "| --- | ---- |"); + } + + [TestMethod] + public async Task CompressionCodecRoundTripsGzipBase64() + { + var module = new ToolCatalog().GetById("compression_codec")!; + + var compressed = await ToolExecutor.ExecuteAsync(module, "gzip compress\nYMhut Box"); + var base64 = compressed.Output + .Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .Single(line => line.StartsWith("Base64:", StringComparison.OrdinalIgnoreCase)) + .Replace("Base64:", string.Empty, StringComparison.OrdinalIgnoreCase) + .Trim(); + var decoded = await ToolExecutor.ExecuteAsync(module, $"gzip decode\n{base64}"); + + Assert.IsTrue(compressed.Ok); + StringAssert.Contains(compressed.Output, "Algorithm: gzip"); + Assert.IsTrue(decoded.Ok); + Assert.AreEqual("YMhut Box", decoded.Output); + } + + [TestMethod] + public async Task TotpGeneratorMatchesRfc6238Sample() + { + var module = new ToolCatalog().GetById("totp_generator")!; + + var result = await ToolExecutor.ExecuteAsync(module, "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ\ntime=59\ndigits=8"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "Code: 94287082"); + StringAssert.Contains(result.Output, "Counter: 1"); + } + + [TestMethod] + public async Task ColorContrastCheckerReportsWcagPasses() + { + var module = new ToolCatalog().GetById("color_contrast_checker")!; + + var result = await ToolExecutor.ExecuteAsync(module, "#000000 #ffffff"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "Contrast: 21.00:1"); + StringAssert.Contains(result.Output, "AA normal text: Pass"); + StringAssert.Contains(result.Output, "AAA normal text: Pass"); + } + + [TestMethod] + public async Task MarkdownPreviewBuildsSummaryAndNormalizedText() + { + var module = new ToolCatalog().GetById("markdown_preview")!; + + var result = await ToolExecutor.ExecuteAsync(module, "# Title\n\n* item\n\n[YMhut](https://example.com)"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "Headings: 1"); + StringAssert.Contains(result.Output, "Links: 1"); + StringAssert.Contains(result.Output, "Plain preview:"); + StringAssert.Contains(result.Output, "Normalized:"); + StringAssert.Contains(result.Output, "- item"); + } + + [TestMethod] + public async Task ImageMetadataInspectorFailsForInvalidPath() + { + var module = new ToolCatalog().GetById("image_metadata_inspector")!; + + var result = await ToolExecutor.ExecuteAsync(module, @"Z:\missing\not-an-image.png"); + + Assert.IsFalse(result.Ok); + StringAssert.Contains(result.Error, "本地图片路径"); + } + + [TestMethod] + public async Task UrlRedirectTraceRejectsInvalidUrlWithoutNetwork() + { + var module = new ToolCatalog().GetById("url_redirect_trace")!; + + var result = await ToolExecutor.ExecuteAsync(module, "not a url", language: "zh-CN"); + + Assert.IsFalse(result.Ok); + StringAssert.Contains(result.Error, "http 或 https URL"); + } + + [TestMethod] + public async Task JsonPathHelperListsLeafPaths() + { + var module = new ToolCatalog().GetById("json_path_helper")!; + + var result = await ToolExecutor.ExecuteAsync(module, "{\"items\":[{\"name\":\"YMhut\"}]}"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "$.items[0].name = \"YMhut\""); + } + + [TestMethod] + public async Task HashManifestBuilderAndVerifierSupportTextRows() + { + var builder = new ToolCatalog().GetById("hash_manifest_builder")!; + var verifier = new ToolCatalog().GetById("hash_manifest_verify")!; + + var manifest = await ToolExecutor.ExecuteAsync(builder, "hello"); + var result = await ToolExecutor.ExecuteAsync(verifier, manifest.Output); + + Assert.IsTrue(manifest.Ok); + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "OK hello"); + } + + [TestMethod] + public async Task RenamePreviewSupportsIndexedNames() + { + var module = new ToolCatalog().GetById("indexed_rename_preview")!; + + var result = await ToolExecutor.ExecuteAsync(module, "prefix=photo_\na.jpg\nb.png"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "a.jpg -> photo_001.jpg"); + StringAssert.Contains(result.Output, "b.png -> photo_002.png"); + } + + [TestMethod] + public async Task ReferenceLookupUsesBundledAuthoritativeData() + { + var module = new ToolCatalog().GetById("mime_lookup")!; + var reference = new ReferenceDataService(AppPaths.ForCurrentUser( + Path.Combine(Path.GetTempPath(), "ymhut-box-tests", Guid.NewGuid().ToString("N")), + FindAssetsRoot())); + + var result = await ToolExecutor.ExecuteAsync(module, "json", referenceDataService: reference); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "YMhut Reference Data"); + StringAssert.Contains(result.Output, "application/json"); + } + + [TestMethod] + public async Task PortLookupLocalizesDescriptionsAndHidesSourceUrls() + { + var module = new ToolCatalog().GetById("port_lookup")!; + var reference = new ReferenceDataService(AppPaths.ForCurrentUser( + Path.Combine(Path.GetTempPath(), "ymhut-box-tests", Guid.NewGuid().ToString("N")), + FindAssetsRoot())); + + var result = await ToolExecutor.ExecuteAsync(module, "443", referenceDataService: reference, language: "zh-CN"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "HTTPS 加密网页服务"); + StringAssert.Contains(result.Output, "数据源:YMhut Reference Data / IANA"); + Assert.IsFalse(result.Output.Contains("https://", StringComparison.OrdinalIgnoreCase)); + } + + [TestMethod] + public async Task SmartSearchUsesOnlySelectedChannel() + { + var module = new ToolCatalog().GetById("smart_search")!; + + var result = await ToolExecutor.ExecuteAsync(module, "bing\nYMhut Box", language: "zh-CN"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "搜索渠道:Bing"); + StringAssert.Contains(result.Output, "https://www.bing.com/search"); + Assert.IsFalse(result.Output.Contains("baidu.com", StringComparison.OrdinalIgnoreCase)); + } + + [TestMethod] + public async Task WeatherPrefersChinaWeatherForKnownChineseCity() + { + var module = new ToolCatalog().GetById("weather")!; + var manager = new ApiManager(new FakeHttpService(""" + {"weatherinfo":{"city":"北京","weather":"晴","temp1":"10℃","temp2":"20℃","ptime":"11:00"}} + """)); + + var result = await ToolExecutor.ExecuteAsync(module, "北京", apiManager: manager, language: "en-US"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "Data source: China Weather"); + StringAssert.Contains(result.Output, "City: 北京"); + Assert.IsFalse(result.Output.Contains("weather.com.cn", StringComparison.OrdinalIgnoreCase)); + } + + [TestMethod] + public async Task MarkdownConverterRendersJsonObjectAndLanguageLabels() + { + var module = new ToolCatalog().GetById("json_formatter")!; + var result = await ToolExecutor.ExecuteAsync(module, "{\"name\":\"YMhut\",\"count\":2}"); + + var markdown = ToolMarkdownConverter.FromResult(module, result, "en-US"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(markdown.MarkdownText, "## JSON Result"); + StringAssert.Contains(markdown.MarkdownText, "| Field | Value |"); + StringAssert.Contains(markdown.MarkdownText, "```json"); + Assert.IsFalse(markdown.MarkdownText.Contains("字段", StringComparison.Ordinal)); + } + + [TestMethod] + public async Task MarkdownConverterRendersJsonArrayAsTable() + { + var module = new ToolCatalog().GetById("json_formatter")!; + var result = await ToolExecutor.ExecuteAsync(module, "[{\"name\":\"YMhut\",\"meta\":{\"kind\":\"tool\"}},{\"name\":\"Box\",\"meta\":{\"kind\":\"app\"}}]"); + + var markdown = ToolMarkdownConverter.FromResult(module, result, "zh-CN"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(markdown.MarkdownText, "**项目数**: 2"); + StringAssert.Contains(markdown.MarkdownText, "| name | meta |"); + StringAssert.Contains(markdown.MarkdownText, "{\"kind\":\"tool\"}"); + } + + [TestMethod] + public void MarkdownConverterKeepsInvalidJsonInCodeBlock() + { + var module = new ToolCatalog().GetById("json_formatter")!; + var result = ToolExecutionResult.Success("{not-json"); + + var markdown = ToolMarkdownConverter.FromResult(module, result, "zh-CN"); + + StringAssert.Contains(markdown.MarkdownText, "JSON 解析失败"); + StringAssert.Contains(markdown.MarkdownText, "```json"); + StringAssert.Contains(markdown.MarkdownText, "{not-json"); + } + + [TestMethod] + public async Task MarkdownConverterRendersStructuredResultKinds() + { + var catalog = new ToolCatalog(); + + var diffModule = catalog.GetById("text_diff")!; + var diff = await ToolExecutor.ExecuteAsync(diffModule, "alpha\n---\nbeta"); + var diffMarkdown = ToolMarkdownConverter.FromResult(diffModule, diff, "zh-CN"); + StringAssert.Contains(diffMarkdown.MarkdownText, "```diff"); + + var tableModule = catalog.GetById("markdown_table_normalizer")!; + var table = await ToolExecutor.ExecuteAsync(tableModule, "| a | b |\n|---|---|\n| 1 | 2 |"); + var tableMarkdown = ToolMarkdownConverter.FromResult(tableModule, table, "zh-CN"); + StringAssert.Contains(tableMarkdown.MarkdownText, "| a | b |"); + + var systemModule = catalog.GetById("system_tool")!; + var system = await ToolExecutor.ExecuteAsync(systemModule, "status"); + var systemMarkdown = ToolMarkdownConverter.FromResult(systemModule, system, "zh-CN"); + StringAssert.Contains(systemMarkdown.MarkdownText, "| 字段 | 值 |"); + } + + [TestMethod] + public async Task MarkdownConverterPreservesSanitizedReferenceSources() + { + var module = new ToolCatalog().GetById("port_lookup")!; + var reference = new ReferenceDataService(AppPaths.ForCurrentUser( + Path.Combine(Path.GetTempPath(), "ymhut-box-tests", Guid.NewGuid().ToString("N")), + FindAssetsRoot())); + var result = await ToolExecutor.ExecuteAsync(module, "443", referenceDataService: reference, language: "zh-CN"); + + var markdown = ToolMarkdownConverter.FromResult(module, result, "zh-CN"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(markdown.MarkdownText, "YMhut Reference Data / IANA"); + Assert.IsFalse(markdown.MarkdownText.Contains("https://", StringComparison.OrdinalIgnoreCase)); + Assert.IsNotNull(result.Document); + Assert.IsTrue(result.Document.Blocks.Any(block => block.Kind is ToolResultBlockKind.Metric or ToolResultBlockKind.Table)); + Assert.IsFalse(result.Document.Blocks.All(block => block.Kind == ToolResultBlockKind.Text)); + } + + [TestMethod] + public void RemoteResultBuilderAddsSanitizedSourceAndCustomBlocks() + { + Assert.IsTrue(ApiEndpoints.TryGet("baidu_hot", out var endpoint)); + var response = new ApiResponse( + endpoint.Id, + new Uri("https://top.baidu.com/api/board?secret=hidden"), + true, + "{}", + null, + DateTimeOffset.Parse("2026-05-27T10:00:00+08:00")); + var output = """ + 鏁版嵁婧愶細鐧惧害鐑悳锛堝畼鏂?鏉冨▉婧愶級 + 鏉ユ簮璇存槑锛氬凡闅愯棌杩滅▼鍦板潃锛屼粎灞曠ず鑴辨晱鏉ユ簮鍚嶇О銆? 鑾峰彇鏃堕棿锛?026-05-27 10:00:00 +08:00 + + 1. YMhut Box 鐑偣 鐑害锛?00 + 鎽樿鍐呭 + """; + + var document = ToolResultBuilder.FromRemote(endpoint, response, output, "zh-CN"); + + Assert.AreEqual("PearAPI DailyHot", document.SourceName); + Assert.AreEqual(ToolResultBlockKind.RankedList, document.Blocks[0].Kind); + Assert.IsTrue(document.Blocks.Any(block => block.Kind == ToolResultBlockKind.KeyValue)); + Assert.IsTrue(document.Blocks.Any(block => block.Kind == ToolResultBlockKind.RankedList)); + Assert.AreEqual("source", document.Blocks[^1].Metadata?["displayMode"]); + Assert.IsFalse(document.Metadata.ContainsKey("sourceUrl")); + Assert.IsFalse(document.Blocks.Any(block => + block.Metadata is not null && + block.Metadata.ContainsKey("sourceUrl"))); + Assert.IsFalse(document.Blocks.SelectMany(block => block.Pairs).Any(pair => + pair.Key.Contains("鎺ュ彛鍦板潃", StringComparison.OrdinalIgnoreCase) || + pair.Key.Contains("Endpoint", StringComparison.OrdinalIgnoreCase))); + Assert.IsFalse(document.Blocks.SelectMany(block => block.Pairs).Any(pair => pair.Value.Contains("secret=hidden", StringComparison.OrdinalIgnoreCase))); + Assert.IsFalse(document.RawText.Contains("secret=hidden", StringComparison.OrdinalIgnoreCase)); + } + + [TestMethod] + public void RemoteFailureBuildsRenderableDocumentInsteadOfEmptyFailure() + { + Assert.IsTrue(ApiEndpoints.TryGet("bili_hot", out var endpoint)); + var response = new ApiResponse( + endpoint.Id, + new Uri("https://api.bilibili.com/x/web-interface/popular"), + false, + "Forbidden", + "HTTP 403 Forbidden", + DateTimeOffset.Parse("2026-05-27T10:00:00+08:00"), + 403); + + var result = RemoteToolFormatter.FormatFailure(endpoint, response, string.Empty, "zh-CN"); + + Assert.IsTrue(result.Ok); + Assert.IsNotNull(result.Document); + Assert.AreEqual("error", result.Document.Status); + Assert.IsTrue(result.Document.Blocks.Count >= 2); + StringAssert.Contains(result.Output, "源站拒绝"); + Assert.IsFalse(result.Output.Contains(endpoint.SourceUrl, StringComparison.OrdinalIgnoreCase)); + Assert.IsFalse(result.Output.Contains("接口地址", StringComparison.OrdinalIgnoreCase)); + Assert.IsFalse(result.Output.Contains("Endpoint", StringComparison.OrdinalIgnoreCase)); + Assert.IsTrue(result.Document.Blocks.Any(block => block.Kind == ToolResultBlockKind.Text)); + Assert.AreEqual("source", result.Document.Blocks[^1].Metadata?["displayMode"]); + } + + [TestMethod] + public void RemoteRankingFormatterRecoversCardsFromAlternativeJson() + { + Assert.IsTrue(ApiEndpoints.TryGet("bili_hot", out var endpoint)); + var response = new ApiResponse( + endpoint.Id, + new Uri("https://api.bilibili.com/x/web-interface/popular"), + true, + """ + { + "data": { + "items": [ + { "title": "First video", "owner": { "name": "UP" }, "stat": { "view": 123456 } }, + { "title": "Second video", "stat": { "view": 888 } } + ] + } + } + """, + null, + DateTimeOffset.Parse("2026-05-27T10:00:00+08:00"), + 200); + + var result = RemoteToolFormatter.Format(endpoint, response, string.Empty, "zh-CN"); + + Assert.IsTrue(result.Ok); + Assert.IsNotNull(result.Document); + var cards = result.Document.Blocks.FirstOrDefault(block => block.Kind == ToolResultBlockKind.RankedList); + Assert.IsNotNull(cards); + Assert.AreEqual("1", cards.Items[0].Leading); + StringAssert.Contains(cards.Items[0].Title, "First video"); + StringAssert.Contains(cards.Items[0].Title + cards.Items[0].Subtitle, "123,456"); + } + + [TestMethod] + public void RemoteRankingFormatterRecoversCardsFromEmbeddedHtml() + { + Assert.IsTrue(ApiEndpoints.TryGet("zhihu_hot", out var endpoint)); + var response = new ApiResponse( + endpoint.Id, + new Uri("https://www.zhihu.com/hot"), + true, + """ + <html><body> + <script id="js-initialData" type="application/json"> + {"initialState":{"topstory":{"hotList":[{"target":{"title":"Hot title one"}},{"target":{"title":"Hot title two"}}]}}} + </script> + </body></html> + """, + null, + DateTimeOffset.Parse("2026-05-27T10:00:00+08:00"), + 200); + + var result = RemoteToolFormatter.Format(endpoint, response, string.Empty, "zh-CN"); + + Assert.IsTrue(result.Ok); + Assert.IsNotNull(result.Document); + var cards = result.Document.Blocks.FirstOrDefault(block => block.Kind == ToolResultBlockKind.RankedList); + Assert.IsNotNull(cards); + Assert.AreEqual(2, cards.Items.Count); + Assert.AreEqual("1", cards.Items[0].Leading); + StringAssert.Contains(cards.Items[0].Title, "Hot title one"); + } + + [TestMethod] + public void RemoteRankingResultIgnoresSanitizedSourceNoticeInCards() + { + Assert.IsTrue(ApiEndpoints.TryGet("hotboard", out var endpoint)); + var response = new ApiResponse( + endpoint.Id, + new Uri("https://top.baidu.com/api/board?secret=hidden"), + true, + "{}", + null, + DateTimeOffset.Parse("2026-05-27T10:00:00+08:00")); + var output = string.Join(Environment.NewLine, new[] + { + "\u6570\u636e\u6e90\uff1aBaidu Hot\uff08official\uff09", + "\u6765\u6e90\u8bf4\u660e\uff1a\u5df2\u9690\u85cf\u8fdc\u7a0b\u5730\u5740\uff0c\u4ec5\u5c55\u793a\u8131\u654f\u6765\u6e90\u540d\u79f0\u3002", + "\u83b7\u53d6\u65f6\u95f4\uff1a2026-05-27 10:00:00 +08:00", + "", + "1. YMhut Box Hot", + " Summary line", + "2. WinUI Tools", + " Another summary" + }); + + var document = ToolResultBuilder.FromRemote(endpoint, response, output, "zh-CN"); + var cards = document.Blocks.FirstOrDefault(block => block.Kind == ToolResultBlockKind.RankedList); + + Assert.IsNotNull(cards); + Assert.IsGreaterThanOrEqualTo(cards.Items.Count, 2); + Assert.AreEqual("1", cards.Items[0].Leading); + StringAssert.Contains(cards.Items[0].Title, "YMhut Box Hot"); + Assert.IsFalse(cards.Items.Any(item => + item.Title.Contains("\u5df2\u9690\u85cf\u8fdc\u7a0b\u5730\u5740", StringComparison.OrdinalIgnoreCase) || + item.Subtitle.Contains("\u5df2\u9690\u85cf\u8fdc\u7a0b\u5730\u5740", StringComparison.OrdinalIgnoreCase) || + item.Title.Contains("remote address is hidden", StringComparison.OrdinalIgnoreCase) || + item.Subtitle.Contains("remote address is hidden", StringComparison.OrdinalIgnoreCase))); + Assert.IsFalse(document.Metadata.ContainsKey("sourceUrl")); + } + + [TestMethod] + public void GoldPriceRemoteResultBuildsTrendChart() + { + Assert.IsTrue(ApiEndpoints.TryGet("gold_price", out var endpoint)); + var response = new ApiResponse( + endpoint.Id, + new Uri("https://prices.lbma.org.uk/json/gold_pm.json"), + true, + "{}", + null, + DateTimeOffset.Parse("2026-06-14T10:00:00+08:00")); + var output = string.Join(Environment.NewLine, new[] + { + "LBMA Gold PM Fixing", + "日期:2026-06-12", + "USD/oz:3362.15", + "GBP/oz:2491.22", + "", + "Date | USD/oz | GBP/oz", + "2026-06-10 | 3320.1 | 2460.4", + "2026-06-11 | 3340.8 | 2475.2", + "2026-06-12 | 3362.15 | 2491.22" + }); + + var document = ToolResultBuilder.FromRemote(endpoint, response, output, "zh-CN"); + + Assert.IsTrue(document.Blocks.Any(block => block.Kind == ToolResultBlockKind.Metric)); + Assert.IsTrue(document.Blocks.Any(block => block.Kind == ToolResultBlockKind.Table)); + var chart = document.Blocks.FirstOrDefault(block => block.Kind == ToolResultBlockKind.LineChart); + Assert.IsNotNull(chart); + Assert.IsGreaterThanOrEqualTo(chart.Rows.Count, 4); + Assert.AreEqual("USD/oz", chart.Metadata?["chartUnit"]); + } + + [TestMethod] + public void UpdateNoticeJsonKeepsPlainTextAndAddsMarkdown() + { + var repoRoot = Directory.GetParent(FindAssetsRoot())!.FullName; + var noticePath = Path.Combine(repoRoot, "update-notice", "2.0.6.3.json"); + var totalPath = Path.Combine(repoRoot, "update-notice", "total.json"); + + using var notice = JsonDocument.Parse(File.ReadAllText(noticePath)); + var noticeRoot = notice.RootElement; + Assert.AreEqual("2.0.6.3", noticeRoot.GetProperty("app_version").GetString()); + Assert.IsFalse(string.IsNullOrWhiteSpace(noticeRoot.GetProperty("message").GetString())); + Assert.IsFalse(string.IsNullOrWhiteSpace(noticeRoot.GetProperty("release_notes").GetString())); + StringAssert.Contains(noticeRoot.GetProperty("message_md").GetString(), "YMhut Box 2.0.6.3"); + StringAssert.Contains(noticeRoot.GetProperty("release_notes_md").GetString(), "QQ 信息"); + StringAssert.Contains(noticeRoot.GetProperty("release_notes_md").GetString(), "安全浏览器"); + + using var total = JsonDocument.Parse(File.ReadAllText(totalPath)); + var totalRoot = total.RootElement; + Assert.AreEqual("2.0.6.3", totalRoot.GetProperty("latest_version").GetString()); + var latest = totalRoot.GetProperty("latest"); + Assert.AreEqual("2.0.6.3", latest.GetProperty("version").GetString()); + StringAssert.Contains(latest.GetProperty("release_notes_md").GetString(), "QQ 信息"); + Assert.AreEqual("2.0.6.3", totalRoot.GetProperty("versions")[0].GetProperty("version").GetString()); + StringAssert.Contains(totalRoot.GetProperty("versions")[0].GetProperty("summary").GetString(), "QQ 信息"); + } + + [TestMethod] + public void WeatherRemoteResultBuildsSummaryForecastAndSourceMetadata() + { + Assert.IsTrue(ApiEndpoints.TryGet("weather", out var endpoint)); + var response = new ApiResponse(endpoint.Id, new Uri("https://api.open-meteo.com/v1/forecast"), true, "{}", null, DateTimeOffset.Now); + var output = """ + Data source: Open-Meteo (public trusted source) + Fetched at: 2026-05-28 10:00:00 +08:00 + + Location: Beijing China + Current temperature: 22.5 掳C + Apparent temperature: 23 掳C + Relative humidity: 56% + Wind speed: 12 km/h + + Next three days: + 2026-05-28: 18 - 27 掳C + 2026-05-29: 19 - 28 掳C + """; + + var document = ToolResultBuilder.FromRemote(endpoint, response, output, "zh-CN"); + + Assert.IsTrue(document.Metadata.TryGetValue("sourceVisibility", out var visibility)); + Assert.AreEqual(ApiSourceVisibility.PublicTrusted.ToString(), visibility); + Assert.IsTrue(document.Blocks.Any(block => block.Kind == ToolResultBlockKind.Metric)); + Assert.IsTrue(document.Blocks.Any(block => block.Kind == ToolResultBlockKind.Table)); + } + + [TestMethod] + public void HistoryRemoteResultBuildsTimelineInsteadOfPlainText() + { + Assert.IsTrue(ApiEndpoints.TryGet("history_today", out var endpoint)); + var response = new ApiResponse(endpoint.Id, new Uri("https://api.wikimedia.org/feed/v1/wikipedia/zh/onthisday/all/05/28"), true, "{}", null, DateTimeOffset.Now); + var output = """ + EVENTS + - 585: Historical event + - 1926: Another event + + BIRTHS + - 1908: Person born + """; + + var document = ToolResultBuilder.FromRemote(endpoint, response, output, "zh-CN"); + + Assert.IsTrue(document.Blocks.Any(block => block.Kind == ToolResultBlockKind.Timeline)); + Assert.IsFalse(document.Blocks.All(block => block.Kind == ToolResultBlockKind.Text)); + } + + [TestMethod] + public async Task LocalToolFamiliesReturnSemanticDocumentBlocks() + { + var catalog = new ToolCatalog(); + + var hash = await ToolExecutor.ExecuteAsync(catalog.GetById("hash_generator")!, "YMhut"); + Assert.IsTrue(hash.Document!.Blocks.Any(block => block.Kind == ToolResultBlockKind.Table)); + + var base64 = await ToolExecutor.ExecuteAsync(catalog.GetById("base64_codec")!, "YMhut"); + Assert.IsTrue(base64.Document!.Blocks.Any(block => block.Kind == ToolResultBlockKind.KeyValue)); + + var calculator = await ToolExecutor.ExecuteAsync(catalog.GetById("number_base")!, "255"); + Assert.IsTrue(calculator.Document!.Blocks.Any(block => block.Kind == ToolResultBlockKind.Metric)); + + var diff = await ToolExecutor.ExecuteAsync(catalog.GetById("text_diff")!, "a\n---\nb"); + Assert.IsTrue(diff.Document!.Blocks.Any(block => block.Kind == ToolResultBlockKind.Diff)); + } + + [TestMethod] + public async Task DataSetToolsReadBundledAssets() + { + var catalog = new ToolCatalog(); + var reference = new ReferenceDataService(AppPaths.ForCurrentUser( + Path.Combine(Path.GetTempPath(), "ymhut-box-tests", Guid.NewGuid().ToString("N")), + FindAssetsRoot())); + + var car = await ToolExecutor.ExecuteAsync(catalog.GetById("car_info")!, "京A12345", referenceDataService: reference); + var skin = await ToolExecutor.ExecuteAsync(catalog.GetById("sanguosha_skin")!, "刘备", referenceDataService: reference); + + Assert.IsTrue(car.Ok); + Assert.IsTrue(skin.Ok); + StringAssert.Contains(car.Output, "北京"); + StringAssert.Contains(skin.Output, "刘备"); + } + + [TestMethod] + public async Task ReferenceDataServiceReadsDatPackageWhenJsonIsPacked() + { + var tempRoot = Path.Combine(Path.GetTempPath(), "ymhut-box-tests", Guid.NewGuid().ToString("N")); + var assetsRoot = Path.Combine(tempRoot, "Assets"); + var dataRoot = Path.Combine(assetsRoot, "data"); + Directory.CreateDirectory(dataRoot); + var datPath = Path.Combine(dataRoot, "reference-data.dat"); + + using (var archive = ZipFile.Open(datPath, ZipArchiveMode.Create)) + { + var entry = archive.CreateEntry("data/reference/ymhut_reference_data.json"); + await using var stream = entry.Open(); + await using var writer = new StreamWriter(stream); + await writer.WriteAsync("{\"source\":\"packed\"}"); + } + + try + { + var service = new ReferenceDataService(AppPaths.ForCurrentUser(tempRoot, assetsRoot)); + + var text = await service.ReadTextAsync("data/reference/ymhut_reference_data.json"); + + Assert.IsTrue(service.Exists("data/reference/ymhut_reference_data.json")); + Assert.AreEqual("{\"source\":\"packed\"}", text); + } + finally + { + Directory.Delete(tempRoot, recursive: true); + } + } + + [TestMethod] + public async Task SystemToolProvidesNativeStatus() + { + var module = new ToolCatalog().GetById("system_tool")!; + + var result = await ToolExecutor.ExecuteAsync(module, "status"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, ".NET"); + StringAssert.Contains(result.Output, "calculator"); + } + + [TestMethod] + public void OfficialSourceHidesPublicSourceUrlInDocumentMetadata() + { + var endpoint = new ApiEndpoint( + "dns_query", + "DNS", + "https://dns.google/resolve?name={value}", + "Google Public DNS", + "https://developers.google.com/speed/public-dns/docs/doh/json"); + var response = new ApiResponse(endpoint.Id, new Uri("https://dns.google/resolve?name=example.com"), true, "Status: 0", null, DateTimeOffset.Now); + + var document = ToolResultBuilder.FromRemote(endpoint, response, "Status: 0", "zh-CN"); + + Assert.IsFalse(document.Metadata.ContainsKey("sourceUrl")); + Assert.IsFalse(document.Blocks.Any(block => + block.Metadata is not null && + block.Metadata.ContainsKey("sourceUrl"))); + Assert.IsTrue(document.Blocks.Any(block => + block.Metadata is not null && + block.Metadata.TryGetValue("sourceVisibility", out var visibility) && + visibility == ApiSourceVisibility.PublicOfficial.ToString())); + } + + [TestMethod] + public void SensitivePrivateSourceHidesEndpointInDocumentMetadata() + { + var endpoint = new ApiEndpoint( + "media_types", + "Media config", + "https://update.ymhut.cn/media-types.json", + "YMhut remote config", + "https://update.ymhut.cn/media-types.json", + false, + ApiSourceVisibility.SensitivePrivate); + var response = new ApiResponse(endpoint.Id, new Uri(endpoint.SourceUrl), true, "{}", null, DateTimeOffset.Now); + + var document = ToolResultBuilder.FromRemote(endpoint, response, "{}", "zh-CN"); + + Assert.IsFalse(document.Metadata.Values.Any(value => value.Contains("update.ymhut.cn", StringComparison.OrdinalIgnoreCase))); + Assert.IsTrue(document.Blocks.Any(block => + block.Metadata is not null && + block.Metadata.TryGetValue("sourceSensitivity", out var sensitivity) && + sensitivity == "sensitive")); + Assert.IsFalse(document.Blocks.Any(block => + block.Metadata is not null && + block.Metadata.Values.Any(value => value.Contains("update.ymhut.cn", StringComparison.OrdinalIgnoreCase)))); + } + + [TestMethod] + public void HttpDiagnosticHidesDynamicTargetAsEndpoint() + { + Assert.IsTrue(ApiEndpoints.TryGet("http_diagnostic", out var endpoint)); + var response = new ApiResponse(endpoint.Id, new Uri("https://example.com/private/path?token=secret"), true, "<title>Example", null, DateTimeOffset.Now); + + var document = ToolResultBuilder.FromRemote(endpoint, response, "Example\n1. Status page", "zh-CN"); + + Assert.IsTrue(endpoint.ShouldHideEndpoint); + Assert.IsFalse(document.Metadata.ContainsKey("sourceUrl")); + Assert.IsFalse(document.Blocks.Any(block => + block.Metadata is not null && + block.Metadata.TryGetValue("sourceUrl", out var sourceUrl) && + sourceUrl.Contains("example.com", StringComparison.OrdinalIgnoreCase))); + } + + private sealed class FakeHttpService(string content) : IHttpService + { + public Task GetAsync(Uri uri, CancellationToken cancellationToken = default) + { + return Task.FromResult(new HttpServiceResult(System.Net.HttpStatusCode.OK, content, new Dictionary(), TimeSpan.FromMilliseconds(1))); + } + + public Task GetStringAsync(Uri uri, CancellationToken cancellationToken = default) + { + return Task.FromResult(content); + } + + public Task SendAsync( + Uri uri, + string method = "GET", + string? body = null, + IReadOnlyDictionary? headers = null, + bool ensureSuccess = true, + HttpRequestPolicy? policy = null, + CancellationToken cancellationToken = default) + { + return GetAsync(uri, cancellationToken); + } + } + + private static string FindAssetsRoot() + { + var directory = new DirectoryInfo(Directory.GetCurrentDirectory()); + while (directory is not null) + { + var candidate = Path.Combine(directory.FullName, "assets"); + if (File.Exists(Path.Combine(candidate, "data", "reference", "ymhut_reference_data.json"))) + { + return candidate; + } + + directory = directory.Parent; + } + + throw new DirectoryNotFoundException("Unable to locate repository assets directory."); + } +} + diff --git a/src/YMhut.Box.Tests/ToolRemoteRankingTests.cs b/src/YMhut.Box.Tests/ToolRemoteRankingTests.cs new file mode 100644 index 0000000..a1bfe9a --- /dev/null +++ b/src/YMhut.Box.Tests/ToolRemoteRankingTests.cs @@ -0,0 +1,60 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using YMhut.Box.Core.Api; +using YMhut.Box.Core.Tools; + +namespace YMhut.Box.Tests; + +[TestClass] +public sealed class ToolRemoteRankingTests +{ + [TestMethod] + public void HotboardUsesPlatformSelectionBeforeRunning() + { + var module = new ToolCatalog().GetById("hotboard")!; + var spec = ToolPageSpecCatalog.For(module); + + Assert.AreEqual(ToolPrimaryInputKind.FixedOptions, spec.PrimaryInput); + Assert.IsFalse(spec.AutoRunOnOpen); + Assert.IsTrue(spec.Parameters.Any(parameter => + parameter.Key == "preset" && + parameter.Options.Any(option => option.Value == "百度"))); + } + + [TestMethod] + public void HotboardPlatformResolvesDailyHotTitleParameter() + { + var ok = ApiEndpoints.TryResolve("hotboard", "百度", out var endpoint, out var uri); + + Assert.IsTrue(ok); + Assert.AreEqual("hotboard", endpoint.Id); + StringAssert.Contains(Uri.UnescapeDataString(uri.Query), "title=百度"); + } + + [TestMethod] + public void DailyHotRankingKeepsPublicUriOutOfVisibleTitle() + { + Assert.IsTrue(ApiEndpoints.TryGet("hotboard", out var endpoint)); + var uri = new Uri("https://example.invalid/api/dailyhot?title=test"); + var response = new ApiResponse( + endpoint.Id, + uri, + Success: true, + Content: string.Empty, + Error: null, + FetchedAt: DateTimeOffset.UnixEpoch); + var document = ToolResultBuilder.FromRemote( + endpoint, + response, + """ + 1. YMhut 发布新版本 / 热度 999 / https://example.com/news?id=1 + 2. 第二条 https://example.com/news?id=2 + """); + + var ranked = document.Blocks.First(block => block.Kind == ToolResultBlockKind.RankedList); + var first = ranked.Items[0]; + + Assert.AreEqual("https://example.com/news?id=1", first.Uri); + Assert.IsFalse(first.Title.Contains("https://", StringComparison.OrdinalIgnoreCase)); + Assert.IsTrue(ranked.Items.All(item => !item.Title.Contains("https://", StringComparison.OrdinalIgnoreCase))); + } +} diff --git a/src/YMhut.Box.Tests/ToolResultExperienceCatalogTests.cs b/src/YMhut.Box.Tests/ToolResultExperienceCatalogTests.cs new file mode 100644 index 0000000..5b8c460 --- /dev/null +++ b/src/YMhut.Box.Tests/ToolResultExperienceCatalogTests.cs @@ -0,0 +1,64 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using YMhut.Box.Core.Tools; + +namespace YMhut.Box.Tests; + +[TestClass] +public sealed class ToolResultExperienceCatalogTests +{ + [TestMethod] + public void EveryDefaultToolHasUniqueResultExperience() + { + var catalog = new ToolCatalog(ToolCatalog.DefaultModules()); + var experiences = new ToolResultExperienceCatalog(catalog); + + Assert.AreEqual(catalog.Modules.Count, experiences.Experiences.Count); + + var ids = new HashSet(StringComparer.OrdinalIgnoreCase); + foreach (var module in catalog.Modules) + { + var experience = experiences.GetRequired(module.Id); + Assert.AreEqual(module.Id, experience.ToolId); + Assert.IsFalse(string.IsNullOrWhiteSpace(experience.ExperienceId), module.Id); + Assert.IsFalse(string.IsNullOrWhiteSpace(experience.Layout), module.Id); + Assert.IsFalse(string.IsNullOrWhiteSpace(experience.PrimaryPrimitive), module.Id); + Assert.IsTrue(ids.Add(experience.ExperienceId), $"Duplicate experienceId: {experience.ExperienceId}"); + } + } + + [TestMethod] + public void EveryDefaultToolHasUniquePageExperience() + { + var catalog = new ToolCatalog(ToolCatalog.DefaultModules()); + var experiences = new ToolResultExperienceCatalog(catalog); + + Assert.AreEqual(catalog.Modules.Count, experiences.PageExperiences.Count); + + var ids = new HashSet(StringComparer.OrdinalIgnoreCase); + foreach (var module in catalog.Modules) + { + var experience = experiences.GetRequiredPage(module.Id); + Assert.AreEqual(module.Id, experience.ToolId); + Assert.IsFalse(string.IsNullOrWhiteSpace(experience.ExperienceId), module.Id); + Assert.IsFalse(string.IsNullOrWhiteSpace(experience.InputLayout), module.Id); + Assert.IsFalse(string.IsNullOrWhiteSpace(experience.ResultLayout), module.Id); + Assert.IsTrue(experience.InputPrimitives.Count > 0, module.Id); + Assert.IsTrue(experience.ResultPrimitives.Count > 0, module.Id); + Assert.IsTrue(ids.Add(experience.ExperienceId), $"Duplicate page experienceId: {experience.ExperienceId}"); + } + } + + [TestMethod] + public void PrivacySanitizerOnlyRedactsYmHutAndApiRequestUrls() + { + const string input = + "public https://example.com/path?q=1; ymhut https://update.ymhut.cn/update-info.json; api_url=https://api.example.net/v1/items"; + + var sanitized = ToolResultPrivacySanitizer.Redact(input, "en-US"); + + StringAssert.Contains(sanitized, "https://example.com/path?q=1"); + Assert.IsFalse(sanitized.Contains("update.ymhut.cn", StringComparison.OrdinalIgnoreCase), sanitized); + Assert.IsFalse(sanitized.Contains("api.example.net", StringComparison.OrdinalIgnoreCase), sanitized); + StringAssert.Contains(sanitized, "[YMhut endpoint hidden]"); + } +} diff --git a/src/YMhut.Box.Tests/ToolWebPayloadSerializationTests.cs b/src/YMhut.Box.Tests/ToolWebPayloadSerializationTests.cs new file mode 100644 index 0000000..6cb9a21 --- /dev/null +++ b/src/YMhut.Box.Tests/ToolWebPayloadSerializationTests.cs @@ -0,0 +1,105 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using YMhut.Box.Core.Tools; + +namespace YMhut.Box.Tests; + +[TestClass] +public sealed class ToolWebPayloadSerializationTests +{ + private static readonly JsonSerializerOptions Options = CreateOptions(); + + [TestMethod] + public void ToolPagePayloadSerializesRulesParametersAndActions() + { + var catalog = new ToolCatalog(); + var module = catalog.GetById("json_formatter")!; + var spec = ToolPageSpecCatalog.For(module); + var experiences = new ToolResultExperienceCatalog(catalog); + + Assert.IsNotEmpty(spec.Rules); + + var payload = new ToolPageWebPayload( + module.Id, + module.Metadata.Name, + module.Metadata.Description, + spec, + experiences.GetRequiredPage(module.Id), + new ToolInputState( + """{"name":"YMhut"}""", + [new ToolInputField("input", "Input", "textarea", """{"name":"YMhut"}""")], + new Dictionary(StringComparer.OrdinalIgnoreCase) { ["mode"] = "format" }, + new Dictionary(StringComparer.OrdinalIgnoreCase) { ["hint"] = "Enter values or choose options, then run the tool." }), + "Light", + "zh-CN", + new ToolResultPrivacyPolicy(["update.ymhut.cn"]), + new ToolPageRuntimeMetadata(DateTimeOffset.UnixEpoch, AutoRun: false, OfflineCapable: true, "Dev", "\uE943"), + ["run", "copyResult", "showRaw"]); + + var json = JsonSerializer.Serialize(payload, Options); + var roundTrip = JsonSerializer.Deserialize(json, Options); + + Assert.IsNotNull(roundTrip); + Assert.AreEqual(module.Id, roundTrip.ToolId); + Assert.IsNotEmpty(roundTrip.Spec.Rules); + Assert.IsNotEmpty(roundTrip.Spec.Parameters); + Assert.AreEqual("format", roundTrip.Input.Rules["mode"]); + CollectionAssert.Contains(roundTrip.AvailableActions.ToList(), "copyResult"); + } + + [TestMethod] + public void ToolResultPayloadSerializesRepresentativeBlockKinds() + { + var catalog = new ToolCatalog(); + var module = catalog.GetById("hotboard")!; + var experiences = new ToolResultExperienceCatalog(catalog); + var blocks = new[] + { + ToolResultBlock.KeyValue("Details", [new("Key", "Value")]), + ToolResultBlock.Table("Table", [new[] { "Name", "Value" }, new[] { "YMhut", "Box" }]), + ToolResultBlock.List(ToolResultBlockKind.RankedList, "Ranking", [new("1", "First", "Trend", "ok", "https://example.com")]), + ToolResultBlock.List(ToolResultBlockKind.NewsList, "News", [new("1", "Headline", "Source", "info", "https://example.com/news")]), + ToolResultBlock.File("File", "report.txt", @"C:\Temp\report.txt"), + ToolResultBlock.Link("Link", "YMhut", "https://example.com"), + ToolResultBlock.Media(ToolResultBlockKind.Media, "Media", "video", "https://example.com/video.mp4"), + ToolResultBlock.Json("JSON", """{"ok":true}"""), + ToolResultBlock.List(ToolResultBlockKind.Status, "Status", [new("OK", "Ready", "", "ok", "")]) + }; + var document = new ToolResultDocument( + module.Id, + ToolResultKind.RankedList, + "raw output", + blocks, + new Dictionary(StringComparer.OrdinalIgnoreCase) { ["displayProfile"] = "cards" }, + "raw output", + "test", + "ok"); + var payload = new ToolResultWebPayload( + module.Id, + module.Metadata.Name, + document, + experiences.GetRequired(module.Id).ExperienceId, + "Dark", + "en-US", + new ToolResultPrivacyPolicy(["update.ymhut.cn"]), + new ToolResultRuntimeMetadata(42, DateTimeOffset.UnixEpoch, Cached: false, "test", new Dictionary()), + experiences.GetRequired(module.Id)); + + var json = JsonSerializer.Serialize(payload, Options); + var roundTrip = JsonSerializer.Deserialize(json, Options); + + Assert.IsNotNull(roundTrip); + Assert.HasCount(blocks.Length, roundTrip.ResultDocument.Blocks); + CollectionAssert.Contains(roundTrip.ResultDocument.Blocks.Select(block => block.Kind).ToList(), ToolResultBlockKind.RankedList); + CollectionAssert.Contains(roundTrip.ResultDocument.Blocks.Select(block => block.Kind).ToList(), ToolResultBlockKind.Media); + StringAssert.Contains(json, nameof(ToolResultBlockKind.RankedList)); + } + + private static JsonSerializerOptions CreateOptions() + { + var options = new JsonSerializerOptions(JsonSerializerDefaults.Web); + options.Converters.Add(new JsonStringEnumConverter()); + return options; + } +} diff --git a/src/YMhut.Box.Tests/ToolWorkerTests.cs b/src/YMhut.Box.Tests/ToolWorkerTests.cs new file mode 100644 index 0000000..8cc7600 --- /dev/null +++ b/src/YMhut.Box.Tests/ToolWorkerTests.cs @@ -0,0 +1,68 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using YMhut.Box.Core.Tools; + +namespace YMhut.Box.Tests; + +[TestClass] +public sealed class ToolWorkerTests +{ + [TestMethod] + public void WorkerProtocolRoundTripsExecuteMessage() + { + var message = new ToolWorkerMessage( + ToolWorkerProtocol.ExecuteTool, + "req-1", + ToolId: "json_formatter", + Input: "{\"name\":\"YMhut\"}", + TimeoutMs: 120000, + Language: "en-US"); + + var line = ToolWorkerProtocol.Serialize(message); + var parsed = ToolWorkerProtocol.Deserialize(line); + + Assert.IsNotNull(parsed); + Assert.AreEqual(ToolWorkerProtocol.ExecuteTool, parsed.Type); + Assert.AreEqual("req-1", parsed.RequestId); + Assert.AreEqual("json_formatter", parsed.ToolId); + Assert.AreEqual("{\"name\":\"YMhut\"}", parsed.Input); + Assert.AreEqual(120000, parsed.TimeoutMs); + Assert.AreEqual("en-US", parsed.Language); + } + + [TestMethod] + public async Task InProcessWorkerExecutesToolByModule() + { + var module = new ToolCatalog().GetById("json_formatter")!; + var worker = new ToolWorkerService(); + + var result = await worker.ExecuteToolAsync(module, "{\"name\":\"YMhut\"}"); + + Assert.IsTrue(result.Ok); + StringAssert.Contains(result.Output, "\"name\": \"YMhut\""); + } + + [TestMethod] + public async Task InProcessWorkerHonorsCancellation() + { + var worker = new ToolWorkerService(); + using var source = new CancellationTokenSource(); + await source.CancelAsync(); + + try + { + await worker.RunAsync( + async token => + { + await Task.Delay(TimeSpan.FromSeconds(5), token); + return true; + }, + source.Token); + } + catch (TaskCanceledException) + { + return; + } + + Assert.Fail("Expected TaskCanceledException."); + } +} diff --git a/src/YMhut.Box.Tests/ToolboxLayoutTests.cs b/src/YMhut.Box.Tests/ToolboxLayoutTests.cs new file mode 100644 index 0000000..e31201d --- /dev/null +++ b/src/YMhut.Box.Tests/ToolboxLayoutTests.cs @@ -0,0 +1,41 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using YMhut.Box.Core.Tools; + +namespace YMhut.Box.Tests; + +[TestClass] +public sealed class ToolboxLayoutTests +{ + [TestMethod] + [DataRow(480, 1)] + [DataRow(760, 2)] + [DataRow(1120, 3)] + [DataRow(1440, 4)] + [DataRow(1720, 5)] + [DataRow(2200, 6)] + public void ToolboxLayoutUsesStableBreakpoints(double width, int expectedColumns) + { + var layout = ToolboxLayoutCalculator.Calculate(width); + + Assert.AreEqual(expectedColumns, layout.Columns); + Assert.IsGreaterThanOrEqualTo(ToolboxLayoutCalculator.MinCardWidth, layout.CardWidth); + Assert.IsLessThanOrEqualTo(ToolboxLayoutCalculator.MaxCardWidth, layout.CardWidth); + } + + [TestMethod] + public void ToolboxLayoutKeepsCategoryRailOutOfNarrowWidths() + { + Assert.IsFalse(ToolboxLayoutCalculator.Calculate(760).ShowCategoryRail); + Assert.IsTrue(ToolboxLayoutCalculator.Calculate(1180).ShowCategoryRail); + } + + [TestMethod] + public void ToolboxLayoutHasSafeFallbackForUnknownWidth() + { + var layout = ToolboxLayoutCalculator.Calculate(0); + + Assert.AreEqual(3, layout.Columns); + Assert.AreEqual(318, layout.CardWidth); + Assert.IsTrue(layout.ShowCategoryRail); + } +} diff --git a/src/YMhut.Box.Tests/UpdateVersionComparerTests.cs b/src/YMhut.Box.Tests/UpdateVersionComparerTests.cs new file mode 100644 index 0000000..b1ec373 --- /dev/null +++ b/src/YMhut.Box.Tests/UpdateVersionComparerTests.cs @@ -0,0 +1,41 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using YMhut.Box.Core.Updates; + +namespace YMhut.Box.Tests; + +[TestClass] +public sealed class UpdateVersionComparerTests +{ + [TestMethod] + public void NormalizeVersionDoesNotAppendBuildWhenVersionAlreadyHasRevision() + { + Assert.AreEqual("2.0.6.2", UpdateVersionComparer.NormalizeVersion("2.0.6.2", "2")); + } + + [TestMethod] + public void NormalizeVersionAppendsBuildOnlyToShortVersion() + { + Assert.AreEqual("2.0.6.2", UpdateVersionComparer.NormalizeVersion("2.0.6", "2")); + } + + [TestMethod] + public void SameVersionWithBuildIsNotNewer() + { + Assert.IsFalse(UpdateVersionComparer.IsRemoteNewer("2.0.6.2", "2", "2.0.6.2")); + Assert.AreEqual(0, UpdateVersionComparer.Compare("2.0.6.2", "2", "2.0.6.2")); + } + + [TestMethod] + public void HigherRemoteVersionIsNewer() + { + Assert.IsTrue(UpdateVersionComparer.IsRemoteNewer("2.0.6.3", "", "2.0.6.2")); + Assert.IsTrue(UpdateVersionComparer.IsRemoteNewer("2.0.7", "", "2.0.6.2")); + } + + [TestMethod] + public void LowerRemoteVersionIsNotNewer() + { + Assert.IsFalse(UpdateVersionComparer.IsRemoteNewer("2.0.6.1", "", "2.0.6.2")); + Assert.IsFalse(UpdateVersionComparer.IsRemoteNewer("2.0.5", "", "2.0.6.2")); + } +} diff --git a/src/YMhut.Box.Tests/YMhut.Box.Tests.csproj b/src/YMhut.Box.Tests/YMhut.Box.Tests.csproj new file mode 100644 index 0000000..e7fbd42 --- /dev/null +++ b/src/YMhut.Box.Tests/YMhut.Box.Tests.csproj @@ -0,0 +1,16 @@ + + + net10.0 + enable + enable + false + + + + + + + + + + diff --git a/src/YMhut.Box.Worker/Program.cs b/src/YMhut.Box.Worker/Program.cs new file mode 100644 index 0000000..d2e8974 --- /dev/null +++ b/src/YMhut.Box.Worker/Program.cs @@ -0,0 +1,191 @@ +using System.Collections.Concurrent; +using System.IO.Pipes; +using System.Text; +using YMhut.Box.Core.Api; +using YMhut.Box.Core.App; +using YMhut.Box.Core.Data; +using YMhut.Box.Core.Logging; +using YMhut.Box.Core.Net; +using YMhut.Box.Core.Settings; +using YMhut.Box.Core.Tools; + +Console.OutputEncoding = Encoding.UTF8; + +var pipeName = ReadPipeName(args); +if (string.IsNullOrWhiteSpace(pipeName)) +{ + Console.Error.WriteLine("Missing --pipe argument."); + return 2; +} + +var appPaths = AppPaths.ForCurrentUser(); +var logService = new JsonFileLogService(appPaths); +var settingsService = new AppSettingsService(new AppSettingsStore(appPaths.Root)); +await settingsService.LoadAsync().ConfigureAwait(false); + +using var httpService = new HttpService(logService: logService, settingsService: settingsService); +var apiManager = new ApiManager(httpService, logService); +var referenceDataService = new ReferenceDataService(appPaths); +var catalog = new ToolCatalog(); +var running = new ConcurrentDictionary(StringComparer.Ordinal); +var writeGate = new SemaphoreSlim(1, 1); + +await using var pipe = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous); +await pipe.ConnectAsync(8000).ConfigureAwait(false); +using var reader = new StreamReader(pipe, Encoding.UTF8, detectEncodingFromByteOrderMarks: false, bufferSize: 4096, leaveOpen: true); +await using var writer = new StreamWriter(pipe, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false), bufferSize: 4096, leaveOpen: true) +{ + AutoFlush = true +}; + +await WriteAsync(new ToolWorkerMessage(ToolWorkerProtocol.Ready, Version: ToolWorkerProtocol.Version), CancellationToken.None) + .ConfigureAwait(false); +await logService.WriteAsync("Information", "worker", "Tool worker ready").ConfigureAwait(false); + +while (await reader.ReadLineAsync().ConfigureAwait(false) is { } line) +{ + var message = ToolWorkerProtocol.Deserialize(line); + if (message is null) + { + continue; + } + + if (string.Equals(message.Type, ToolWorkerProtocol.Shutdown, StringComparison.Ordinal)) + { + foreach (var request in running.Values) + { + request.Cancel(); + } + + break; + } + + if (string.Equals(message.Type, ToolWorkerProtocol.Ping, StringComparison.Ordinal)) + { + await WriteAsync(new ToolWorkerMessage(ToolWorkerProtocol.Pong, message.RequestId), CancellationToken.None) + .ConfigureAwait(false); + continue; + } + + if (string.Equals(message.Type, ToolWorkerProtocol.Cancel, StringComparison.Ordinal)) + { + if (running.TryGetValue(message.RequestId, out var cancelSource)) + { + cancelSource.Cancel(); + } + + continue; + } + + if (string.Equals(message.Type, ToolWorkerProtocol.ExecuteTool, StringComparison.Ordinal)) + { + StartToolExecution(message); + } +} + +foreach (var request in running.Values) +{ + request.Dispose(); +} + +return 0; + +void StartToolExecution(ToolWorkerMessage message) +{ + var timeout = message.TimeoutMs > 0 + ? TimeSpan.FromMilliseconds(Math.Clamp(message.TimeoutMs, 1_000, 600_000)) + : TimeSpan.FromMinutes(2); + var cancelSource = new CancellationTokenSource(timeout); + running[message.RequestId] = cancelSource; + + _ = Task.Run(async () => + { + try + { + if (string.IsNullOrWhiteSpace(message.ToolId)) + { + await WriteAsync( + new ToolWorkerMessage(ToolWorkerProtocol.Error, message.RequestId, Error: "Missing tool id."), + CancellationToken.None).ConfigureAwait(false); + return; + } + + var module = catalog.GetById(message.ToolId); + if (module is null) + { + await WriteAsync( + new ToolWorkerMessage(ToolWorkerProtocol.Error, message.RequestId, Error: $"Unknown tool id: {message.ToolId}"), + CancellationToken.None).ConfigureAwait(false); + return; + } + + await logService.WriteAsync("Information", "worker", $"Executing tool: {module.Id}", cancellationToken: cancelSource.Token) + .ConfigureAwait(false); + var result = await ToolExecutor.ExecuteAsync( + module, + message.Input ?? string.Empty, + cancelSource.Token, + apiManager, + referenceDataService, + message.Language ?? "zh-CN").ConfigureAwait(false); + + await WriteAsync( + new ToolWorkerMessage( + ToolWorkerProtocol.Result, + message.RequestId, + Ok: result.Ok, + Output: result.Output, + Error: result.Error, + Document: result.Document), + CancellationToken.None).ConfigureAwait(false); + } + catch (OperationCanceledException) + { + await WriteAsync( + new ToolWorkerMessage(ToolWorkerProtocol.Error, message.RequestId, Error: "Tool execution was canceled or timed out."), + CancellationToken.None).ConfigureAwait(false); + } + catch (Exception exception) + { + await logService.WriteAsync("Error", "worker", "Tool worker execution failed", exception.Message).ConfigureAwait(false); + await WriteAsync( + new ToolWorkerMessage(ToolWorkerProtocol.Error, message.RequestId, Error: exception.Message), + CancellationToken.None).ConfigureAwait(false); + } + finally + { + if (running.TryRemove(message.RequestId, out var source)) + { + source.Dispose(); + } + } + }); +} + +async Task WriteAsync(ToolWorkerMessage message, CancellationToken cancellationToken) +{ + await writeGate.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + await writer.WriteLineAsync(ToolWorkerProtocol.Serialize(message).AsMemory(), cancellationToken).ConfigureAwait(false); + await writer.FlushAsync(cancellationToken).ConfigureAwait(false); + } + finally + { + writeGate.Release(); + } +} + +static string? ReadPipeName(string[] args) +{ + for (var index = 0; index < args.Length; index++) + { + if (string.Equals(args[index], "--pipe", StringComparison.OrdinalIgnoreCase) && + index + 1 < args.Length) + { + return args[index + 1]; + } + } + + return null; +} diff --git a/src/YMhut.Box.Worker/YMhut.Box.Worker.csproj b/src/YMhut.Box.Worker/YMhut.Box.Worker.csproj new file mode 100644 index 0000000..bd334f4 --- /dev/null +++ b/src/YMhut.Box.Worker/YMhut.Box.Worker.csproj @@ -0,0 +1,17 @@ + + + Exe + net10.0 + enable + enable + YMhut.Box.Worker + YMhut.Box.Worker + + + none + false + + + + + diff --git a/src/box-winUI/AgreementWindow.cs b/src/box-winUI/AgreementWindow.cs new file mode 100644 index 0000000..5df9736 --- /dev/null +++ b/src/box-winUI/AgreementWindow.cs @@ -0,0 +1,462 @@ +using System.Runtime.InteropServices; +using Microsoft.UI; +using Microsoft.UI.Text; +using Microsoft.UI.Windowing; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Automation; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Media; +using Microsoft.UI.Xaml.Media.Imaging; +using Windows.Graphics; +using Windows.UI.ViewManagement; +using WinRT.Interop; +using YMhut.Box.Core.Settings; +using YMhut.Box.WinUI.Services; + +namespace YMhut.Box.WinUI; + +public sealed class AgreementWindow : Window +{ + private const double DesiredWindowWidthDip = 760; + private const double DesiredWindowHeightDip = 640; + + private readonly ISettingsService _settingsService; + private readonly IAgreementAcceptanceStore _acceptanceStore; + private readonly string _appVersion; + private readonly Func _accepted; + private readonly ElementTheme _requestedTheme; + private Button _acceptButton = null!; + private bool _acceptedAndContinuing; + + public AgreementWindow( + ISettingsService settingsService, + IAgreementAcceptanceStore acceptanceStore, + string appVersion, + Func accepted) + { + _settingsService = settingsService; + _acceptanceStore = acceptanceStore; + _appVersion = appVersion; + _accepted = accepted; + _requestedTheme = ResolveElementTheme(settingsService.Current.Theme); + Title = $"YMhut Box 使用声明 · {AgreementDocument.CurrentVersion}"; + WindowIconService.ApplyAppIcon(this); + ModernUi.SetPalette(ShouldUseDarkPalette(settingsService.Current.Theme)); + ThemeService.ApplyFontScale(settingsService.Current.FontSize); + Content = BuildContent(); + ApplyWindowTheme(); + Closed += AgreementWindow_Closed; + ResizeWindow(); + } + + private UIElement BuildContent() + { + var agreeCheck = new CheckBox + { + Content = AppLocalizer.T("我已阅读并同意以上声明", "I have read and agree to this statement"), + FontSize = 14, + Foreground = ThemeBrush("TextFillColorPrimaryBrush", ModernUi.TextPrimary), + MinHeight = 34, + Padding = new Thickness(2, 0, 0, 0), + VerticalAlignment = VerticalAlignment.Center + }; + AutomationProperties.SetName(agreeCheck, AppLocalizer.T("同意使用声明", "Accept agreement statement")); + + _acceptButton = CreateActionButton(AppLocalizer.T("同意并进入", "Agree and continue"), "\uE73E", primary: true); + SetAcceptButtonState(false); + _acceptButton.Click += async (_, _) => await AcceptAsync(); + + var decline = CreateActionButton(AppLocalizer.T("拒绝并退出", "Decline and exit"), "\uE711", primary: false); + decline.Click += (_, _) => Application.Current.Exit(); + + agreeCheck.Checked += (_, _) => SetAcceptButtonState(true); + agreeCheck.Unchecked += (_, _) => SetAcceptButtonState(false); + + var statement = ModernUi.Text(AgreementText(), 14, foreground: ThemeBrush("TextFillColorSecondaryBrush", ModernUi.TextSecondary)); + statement.LineHeight = 23; + statement.TextWrapping = TextWrapping.WrapWholeWords; + statement.TextTrimming = TextTrimming.None; + statement.HorizontalAlignment = HorizontalAlignment.Stretch; + + var statementCard = ModernUi.Card( + statement, + new Thickness(16), + radius: 8, + background: ThemeBrush("CardBackgroundFillColorDefaultBrush", ModernUi.Surface)); + statementCard.HorizontalAlignment = HorizontalAlignment.Stretch; + + var scrollContent = new StackPanel + { + Spacing = 16, + HorizontalAlignment = HorizontalAlignment.Stretch, + Children = + { + BuildHeader(), + statementCard + } + }; + + var bodyScroll = new ScrollViewer + { + VerticalScrollBarVisibility = ScrollBarVisibility.Auto, + VerticalScrollMode = ScrollMode.Enabled, + HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled, + HorizontalScrollMode = ScrollMode.Disabled, + Content = scrollContent + }; + bodyScroll.SizeChanged += (_, args) => + { + var width = Math.Max(0, args.NewSize.Width); + scrollContent.Width = width; + statementCard.Width = width; + statement.Width = Math.Max(280, width - 48); + }; + + var footer = ModernUi.Card( + BuildFooter(agreeCheck, decline, _acceptButton), + new Thickness(16, 12, 16, 14), + radius: 8, + background: ThemeBrush("CardBackgroundFillColorDefaultBrush", ModernUi.Surface)); + footer.VerticalAlignment = VerticalAlignment.Bottom; + + var root = new Grid + { + Background = ThemeBrush("ApplicationPageBackgroundThemeBrush", ModernUi.AppBackground), + Padding = new Thickness(24), + RowSpacing = 14, + RowDefinitions = + { + new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }, + new RowDefinition { Height = GridLength.Auto } + } + }; + + root.Children.Add(bodyScroll); + Grid.SetRow(footer, 1); + root.Children.Add(footer); + return root; + } + + private static Grid BuildFooter(CheckBox agreeCheck, Button decline, Button acceptButton) + { + var footer = new Grid + { + RowSpacing = 12, + RowDefinitions = + { + new RowDefinition { Height = GridLength.Auto }, + new RowDefinition { Height = GridLength.Auto } + } + }; + footer.Children.Add(agreeCheck); + + var commandRow = new Grid + { + ColumnSpacing = 10, + ColumnDefinitions = + { + new ColumnDefinition(), + new ColumnDefinition { Width = GridLength.Auto }, + new ColumnDefinition { Width = GridLength.Auto } + } + }; + + Grid.SetColumn(decline, 1); + Grid.SetColumn(acceptButton, 2); + commandRow.Children.Add(new Border()); + commandRow.Children.Add(decline); + commandRow.Children.Add(acceptButton); + + Grid.SetRow(commandRow, 1); + footer.Children.Add(commandRow); + return footer; + } + + private Button CreateActionButton(string text, string glyph, bool primary) + { + var secondaryForeground = ThemeBrush("TextFillColorPrimaryBrush", ModernUi.TextPrimary); + var foreground = primary ? ModernUi.Brush("#FFFFFFFF") : secondaryForeground; + var buttonText = ModernUi.Text(text, 14, FontWeights.SemiBold, foreground, maxLines: 1); + buttonText.TextWrapping = TextWrapping.NoWrap; + + var icon = new FontIcon + { + Glyph = glyph, + FontSize = 15, + Foreground = foreground, + VerticalAlignment = VerticalAlignment.Center + }; + + var content = new StackPanel + { + Orientation = Orientation.Horizontal, + Spacing = 8, + HorizontalAlignment = HorizontalAlignment.Center, + VerticalAlignment = VerticalAlignment.Center, + Children = { icon, buttonText } + }; + + var button = new Button + { + MinWidth = primary ? 138 : 124, + MinHeight = 38, + Padding = new Thickness(14, 8, 14, 8), + CornerRadius = new CornerRadius(6), + Background = primary ? ThemeBrush("AccentFillColorDefaultBrush", ModernUi.Accent) : ThemeBrush("ControlFillColorDefaultBrush", ModernUi.SurfaceAlt), + Foreground = foreground, + BorderBrush = primary ? ThemeBrush("AccentFillColorDefaultBrush", ModernUi.Accent) : ThemeBrush("ControlStrokeColorDefaultBrush", ModernUi.Stroke), + BorderThickness = new Thickness(1), + Content = content + }; + AutomationProperties.SetName(button, text); + return button; + } + + private void SetAcceptButtonState(bool enabled) + { + if (_acceptButton is null) + { + return; + } + + var foreground = enabled ? ModernUi.Brush("#FFFFFFFF") : ThemeBrush("TextFillColorSecondaryBrush", ModernUi.TextSecondary); + _acceptButton.IsEnabled = enabled; + _acceptButton.Background = enabled ? ThemeBrush("AccentFillColorDefaultBrush", ModernUi.Accent) : ThemeBrush("ControlFillColorDefaultBrush", ModernUi.SurfaceAlt); + _acceptButton.Foreground = foreground; + _acceptButton.BorderBrush = enabled ? ThemeBrush("AccentFillColorDefaultBrush", ModernUi.Accent) : ThemeBrush("ControlStrokeColorDefaultBrush", ModernUi.Stroke); + SetButtonContentForeground(_acceptButton, foreground); + } + + private static void SetButtonContentForeground(Button button, Brush foreground) + { + if (button.Content is not Panel panel) + { + return; + } + + foreach (var child in panel.Children) + { + switch (child) + { + case FontIcon icon: + icon.Foreground = foreground; + break; + case TextBlock text: + text.Foreground = foreground; + break; + } + } + } + + private UIElement BuildHeader() + { + var grid = new Grid { ColumnSpacing = 14 }; + grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); + grid.ColumnDefinitions.Add(new ColumnDefinition()); + grid.Children.Add(new Border + { + Width = 58, + Height = 58, + CornerRadius = new CornerRadius(8), + Background = ThemeBrush("AccentFillColorTertiaryBrush", ModernUi.AccentSoft), + BorderBrush = ThemeBrush("CardStrokeColorDefaultBrush", ModernUi.Stroke), + BorderThickness = new Thickness(1), + Child = new Image + { + Source = new BitmapImage(new Uri("ms-appx:///Assets/icons/app_icon.png")), + Width = 34, + Height = 34, + HorizontalAlignment = HorizontalAlignment.Center, + VerticalAlignment = VerticalAlignment.Center + } + }); + + var title = new StackPanel + { + Spacing = 4, + VerticalAlignment = VerticalAlignment.Center, + Children = + { + ModernUi.Text($"YMhut Box 使用声明 · {AgreementDocument.CurrentVersion}", 24, FontWeights.SemiBold, ThemeBrush("TextFillColorPrimaryBrush", ModernUi.TextPrimary), maxLines: 1), + ModernUi.Text(AppLocalizer.T("请确认你理解 YMhut Box 的使用边界、免责条款与风险提示。", "Please confirm that you understand YMhut Box usage boundaries, disclaimers, and safety notes."), 14, foreground: ThemeBrush("TextFillColorSecondaryBrush", ModernUi.TextSecondary), maxLines: 2) + } + }; + Grid.SetColumn(title, 1); + grid.Children.Add(title); + return grid; + } + + private void ApplyWindowTheme() + { + ModernUi.SetPalette(ShouldUseDarkPalette(_settingsService.Current.Theme)); + + if (Content is FrameworkElement root) + { + root.RequestedTheme = _requestedTheme; + } + + ThemeService.ApplyWindowBackdrop(this, _settingsService.Current.WindowBackdrop); + } + + private async Task AcceptAsync() + { + _acceptButton.IsEnabled = false; + var acceptedAt = DateTimeOffset.Now; + + try + { + await _acceptanceStore.RecordAcceptedAsync( + AgreementDocument.CurrentVersion, + AgreementDocument.CurrentRevision, + _appVersion, + AppLocalizer.CurrentLanguage, + acceptedAt); + } + catch (Exception exception) + { + CrashLog.Write(exception); + } + + await _settingsService.UpdateAsync(settings => + { + settings.UserAgreementAccepted = true; + settings.UserAgreementVersion = AgreementDocument.CurrentVersion; + settings.UserAgreementAcceptedAt = acceptedAt; + }); + _acceptedAndContinuing = true; + await _accepted(); + Close(); + } + + private void AgreementWindow_Closed(object sender, WindowEventArgs args) + { + if (!_acceptedAndContinuing) + { + Application.Current.Exit(); + } + } + + private void ResizeWindow() + { + try + { + var hwnd = WindowNative.GetWindowHandle(this); + var windowId = Win32Interop.GetWindowIdFromWindow(hwnd); + var appWindow = AppWindow.GetFromWindowId(windowId); + var scale = GetWindowScale(hwnd); + var width = DipToPixels(DesiredWindowWidthDip, scale); + var height = DipToPixels(DesiredWindowHeightDip, scale); + + var displayArea = DisplayArea.GetFromWindowId(windowId, DisplayAreaFallback.Nearest); + if (displayArea is not null) + { + var workArea = displayArea.WorkArea; + width = Math.Min(width, Math.Max(DipToPixels(480, scale), workArea.Width - DipToPixels(48, scale))); + height = Math.Min(height, Math.Max(DipToPixels(420, scale), workArea.Height - DipToPixels(48, scale))); + appWindow.Resize(new SizeInt32(width, height)); + appWindow.Move(new PointInt32( + workArea.X + Math.Max(0, (workArea.Width - width) / 2), + workArea.Y + Math.Max(0, (workArea.Height - height) / 2))); + return; + } + + appWindow.Resize(new SizeInt32(width, height)); + } + catch + { + } + } + + private static double GetWindowScale(IntPtr hwnd) + { + try + { + var dpi = GetDpiForWindow(hwnd); + return dpi > 0 ? dpi / 96.0 : 1.0; + } + catch + { + return 1.0; + } + } + + private static int DipToPixels(double dips, double scale) + { + return Math.Max(1, (int)Math.Round(dips * scale)); + } + + private Brush ThemeBrush(string resourceKey, Brush fallback) + { + return Application.Current.Resources.TryGetValue(resourceKey, out var value) && value is Brush brush + ? brush + : fallback; + } + + private static ElementTheme ResolveElementTheme(string? theme) + { + return (theme ?? string.Empty).Trim().ToLowerInvariant() switch + { + "light" => ElementTheme.Light, + "dark" => ElementTheme.Dark, + _ => ElementTheme.Default + }; + } + + private static bool ShouldUseDarkPalette(string? theme) + { + return (theme ?? string.Empty).Trim().ToLowerInvariant() switch + { + "light" => false, + "dark" => true, + _ => IsSystemDarkTheme() + }; + } + + private static bool IsSystemDarkTheme() + { + try + { + var background = new UISettings().GetColorValue(UIColorType.Background); + var luminance = (0.2126 * background.R) + (0.7152 * background.G) + (0.0722 * background.B); + return luminance < 128; + } + catch + { + return false; + } + } + + [DllImport("user32.dll")] + private static extern uint GetDpiForWindow(IntPtr hwnd); + + private static string AgreementText() + { + return AppLocalizer.T( + """ + YMhut Box 是面向个人效率、系统诊断和工具集合的桌面应用。部分工具可能访问网络、读取本机状态、启动外部程序、修改系统相关配置,或处理你主动提供的文件。请在确认来源可信、重要数据已备份、操作风险可接受的前提下使用。 + + 免责与风险提示: + 1. 工具输出仅供参考,你需要自行判断输出、建议、排行榜、第三方数据和检测结果的准确性、完整性与适用性。 + 2. 涉及系统设置、注册表、证书、Hosts、Defender、防火墙、清理删除、管理员运行、脚本执行或未知可执行文件启动的操作,可能影响系统稳定性和数据安全。 + 3. 网络请求、第三方接口、价格/指标数据、媒体资源和资讯内容可能变化、延迟、失效或被第三方限制;YMhut Box 不保证其持续可用。 + 4. 文件处理、图片/音视频加载、压缩解压、格式转换、批量操作和外部工具调用可能产生不可逆结果,请自行保留原始文件备份。 + 5. 更新安装和覆盖安装会替换程序文件并清理旧版程序残留,但会尽力保留用户设置、日志、下载队列、插件状态等用户数据。 + 6. 插件和外部程序由其各自作者负责。启用插件、加载外部工具或执行第三方程序前,请确认其来源与权限需求。 + 7. 应用会在本机保存必要的设置、日志、自检结果和轻量运行状态,用于恢复界面、诊断问题和改进体验;不会把 runtime、Tools、Metadata 或完整安装布局持久复制到用户数据目录。 + 8. 继续使用表示你已阅读并理解上述声明,并愿意对自己的操作、数据备份和使用结果承担责任。 + """, + """ + YMhut Box is a desktop app for personal productivity, diagnostics, and utility workflows. Some tools may access the network, read local system state, launch external programs, modify system-related settings, or process files you provide. Use them only when the source is trusted, important data is backed up, and the operation risk is acceptable. + + Disclaimer and safety notes: + 1. Tool output is for reference only. You are responsible for judging the accuracy, completeness, and applicability of output, suggestions, rankings, third-party data, and diagnostics. + 2. Operations involving system settings, registry, certificates, Hosts, Defender, firewall, cleanup/deletion, administrator execution, scripts, or unknown executables may affect system stability and data safety. + 3. Network requests, third-party APIs, price/metric data, media resources, and news content may change, be delayed, expire, or be limited by third parties. YMhut Box does not guarantee continuous availability. + 4. File processing, image/audio/video loading, archive operations, format conversion, batch operations, and external tool calls may produce irreversible results. Keep original backups. + 5. Updates and overwrite installs replace application files and clean old application remnants while preserving user settings, logs, download queues, and plugin state where possible. + 6. Plugins and external programs are maintained by their own authors. Confirm source and permission needs before enabling plugins or launching external tools. + 7. The app stores necessary local settings, logs, startup check results, and lightweight runtime state for UI restore and diagnostics. It does not persist runtime, Tools, Metadata, or a full installation payload in the user data folder. + 8. Continuing means you have read and understood this statement and accept responsibility for your operations, backups, and usage results. + """); + } +} diff --git a/src/box-winUI/App.xaml b/src/box-winUI/App.xaml new file mode 100644 index 0000000..4cce5c4 --- /dev/null +++ b/src/box-winUI/App.xaml @@ -0,0 +1,12 @@ + + + + + + + + + diff --git a/src/box-winUI/App.xaml.cs b/src/box-winUI/App.xaml.cs new file mode 100644 index 0000000..9b423d5 --- /dev/null +++ b/src/box-winUI/App.xaml.cs @@ -0,0 +1,301 @@ +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Media; +using System.Runtime.InteropServices; +using YMhut.Box.Core.Logging; +using YMhut.Box.Core.App; +using YMhut.Box.Core.Settings; +using YMhut.Box.Core.Startup; +using YMhut.Box.WinUI.Services; +using YMhut.Box.WinUI.Views; + +namespace YMhut.Box.WinUI; + +public partial class App : Application +{ + private const int MrtNoLoadedMuiItems = unchecked((int)0x80073B01); + private static int HandlingUnhandledException; + + public static Window? CurrentWindow { get; private set; } + private static readonly string StartupTracePath = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), + "YMhut Box", + "WinUI", + "Logs", + "startup-trace.log"); + + public App() + { + Trace("App.ctor"); + InitializeComponent(); + UnhandledException += App_UnhandledException; + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; + TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException; + } + + protected override async void OnLaunched(LaunchActivatedEventArgs args) + { + try + { + Trace("OnLaunched:start"); + if (RuntimeLayoutBootstrapper.TryRelaunchFromWritableLayoutIfNeeded()) + { + Trace("OnLaunched:relaunched-runtime-layout"); + Current.Exit(); + return; + } + + if (IndependentWindowHostOptions.TryParse(Environment.GetCommandLineArgs().Skip(1), out var hostOptions)) + { + Trace($"OnLaunched:independent-host:{hostOptions.Kind}"); + AppServices.Configure( + string.IsNullOrWhiteSpace(hostOptions.Root) ? null : hostOptions.Root); + await LaunchIndependentWindowHostAsync(hostOptions); + return; + } + + AppServices.Configure(); + CrashLog.Write("Launch started."); + var startupInitialization = AppServices.GetRequiredService(); + var settingsService = AppServices.GetRequiredService(); + var logService = AppServices.GetRequiredService(); + var acceptanceStore = AppServices.GetRequiredService(); + var appVersionService = AppServices.GetRequiredService(); + var startupSplash = new StartupSplashWindow(settingsService.Current.Theme); + CurrentWindow = startupSplash; + startupSplash.Activate(); + await Task.Yield(); + + StartupInitializationSnapshot startupSnapshot; + try + { + var progress = new Progress(startupSplash.ReportProgress); + startupSnapshot = await startupInitialization.InitializeAsync(progress); + startupSplash.ReportOutcome(startupSnapshot); + startupSplash.ScheduleCloseFallback(TimeSpan.FromSeconds(5)); + if (startupSnapshot.HasVisibleStartupIssue) + { + Trace($"OnLaunched:startup-check-issues:{startupSnapshot.FastPreflightReport.VisibleIssueCount}"); + CrashLog.Write($"Startup check completed with issues: issues={startupSnapshot.FastPreflightReport.IssueCount}; visible={startupSnapshot.FastPreflightReport.VisibleIssueCount}; critical={startupSnapshot.FastPreflightReport.CriticalIssueCount}"); + } + + await Task.Delay(2000); + } + catch (Exception exception) + { + startupSplash.ReportFatal(exception); + await Task.Delay(1200); + throw; + } + + var appVersion = appVersionService.GetCurrent().Version; + if (!await IsAgreementAcceptedCurrentAsync(settingsService, acceptanceStore, logService)) + { + Trace("OnLaunched:agreement-required"); + var agreementWindow = new AgreementWindow(settingsService, acceptanceStore, appVersion, async () => + { + LaunchMainWindow(); + await Task.CompletedTask; + }); + CurrentWindow = agreementWindow; + CurrentWindow.Activate(); + startupSplash.Close(); + return; + } + + LaunchMainWindow(startupSplash); + } + catch (Exception exception) + { + CrashLog.Write(exception); + ShowStartupError( + AppLocalizer.T("YMhut Box 启动失败", "YMhut Box failed to start"), + AppLocalizer.T($"启动初始化失败:{exception.Message}", $"Startup initialization failed: {exception.Message}")); + Current.Exit(); + } + } + + private static async Task IsAgreementAcceptedCurrentAsync( + ISettingsService settingsService, + IAgreementAcceptanceStore acceptanceStore, + ILogService logService) + { + var settings = settingsService.Current; + if (!AgreementDocument.SettingsMatchCurrent(settings)) + { + return false; + } + + try + { + var latest = await acceptanceStore.GetLatestAsync().ConfigureAwait(false); + return latest is not null && + string.Equals(latest.Version, AgreementDocument.CurrentVersion, StringComparison.OrdinalIgnoreCase) && + latest.Revision == AgreementDocument.CurrentRevision; + } + catch (Exception exception) + { + CrashLog.Write(exception); + await logService.WriteAsync( + "Warning", + "agreement", + "声明签署记录读取失败,暂时使用设置文件作为兜底", + AppLocalizer.SanitizeSensitiveText(exception.Message, 220)).ConfigureAwait(false); + return true; + } + } + + private static void LaunchMainWindow(Window? previousWindow = null) + { + CurrentWindow = new MainWindow(); + Trace("OnLaunched:after-window"); + CurrentWindow.Activate(); + previousWindow?.Close(); + Trace("OnLaunched:after-activate"); + CrashLog.Write("Launch completed."); + LanguagePackService.ScheduleTransientProjectionCleanup(); + } + + private static async Task LaunchIndependentWindowHostAsync(IndependentWindowHostOptions options) + { + var settingsService = AppServices.GetRequiredService(); + try + { + await settingsService.LoadAsync(); + } + catch (Exception exception) + { + CrashLog.Write(exception); + } + + Trace($"IndependentWindowHost:before-window:{options.Kind}"); + CurrentWindow = options.Kind switch + { + IndependentWindowKind.Browser => new SafeBrowserWindow(options.InitialUrl), + IndependentWindowKind.Media => new MediaPlayerWindow(), + _ => new SafeBrowserWindow(options.InitialUrl) + }; + ThemeService.ApplyTheme(CurrentWindow, settingsService.Current.Theme, settingsService.Current.WindowBackdrop); + CurrentWindow.Activate(); + Trace($"IndependentWindowHost:activated:{options.Kind}"); + CrashLog.Write($"Independent window host launched: {options.Kind}"); + } + + private static void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e) + { + if (Interlocked.Exchange(ref HandlingUnhandledException, 1) == 1) + { + e.Handled = true; + return; + } + + try + { + if (IsBenignResourceProbeException(e.Exception)) + { + e.Handled = true; + return; + } + + TryWriteCrash(e.Exception); + if (IsRecoverableUiException(e.Exception)) + { + e.Handled = true; + } + } + catch + { + e.Handled = true; + } + finally + { + Interlocked.Exchange(ref HandlingUnhandledException, 0); + } + } + + private static void CurrentDomain_UnhandledException(object sender, System.UnhandledExceptionEventArgs e) + { + TryWriteCrash(e.ExceptionObject as Exception ?? new InvalidOperationException(e.ExceptionObject?.ToString())); + } + + private static void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e) + { + TryWriteCrash(e.Exception); + e.SetObserved(); + } + + private static void TryWriteCrash(Exception? exception) + { + try + { + CrashLog.Write(exception); + } + catch + { + } + } + + private static bool IsRecoverableUiException(Exception? exception) + { + if (exception is null) + { + return false; + } + + var details = exception.ToString(); + return exception is System.Runtime.InteropServices.COMException || + details.Contains("ToolboxPage", StringComparison.OrdinalIgnoreCase) || + details.Contains("MeasureOverride", StringComparison.OrdinalIgnoreCase) || + details.Contains("Window object has already been closed", StringComparison.OrdinalIgnoreCase); + } + + private static bool IsBenignResourceProbeException(Exception? exception) + { + if (exception is null) + { + return false; + } + + var details = exception.ToString(); + return exception.HResult == MrtNoLoadedMuiItems || + details.Contains("Resource loader cache", StringComparison.OrdinalIgnoreCase) || + details.Contains("资源加载器缓存没有已加载的 MUI 项", StringComparison.Ordinal); + } + + private static string BuildCriticalStartupMessage(StartupCheckReport report) + { + var issues = report.Items + .Where(item => item.IsIssue && item.Severity == StartupCheckSeverity.Critical) + .Take(8) + .Select(item => $"- {item.Name}: {item.Detail}{(string.IsNullOrWhiteSpace(item.Path) ? string.Empty : $" ({item.Path})")}"); + return AppLocalizer.T( + "启动自检发现关键文件或语言资源缺失,请使用最新安装包覆盖安装。\n\n" + string.Join("\n", issues), + "Startup check found missing critical files or language resources. Please reinstall with the latest setup package.\n\n" + string.Join("\n", issues)); + } + + private static void ShowStartupError(string title, string message) + { + try + { + _ = MessageBoxW(IntPtr.Zero, message, title, 0x00000010); + } + catch + { + } + } + + private static void Trace(string message) + { + try + { + Directory.CreateDirectory(Path.GetDirectoryName(StartupTracePath)!); + File.AppendAllText(StartupTracePath, $"{DateTime.Now:O} {message}{Environment.NewLine}"); + } + catch + { + } + } + + [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = false)] + private static extern int MessageBoxW(IntPtr hWnd, string text, string caption, uint type); +} diff --git a/src/box-winUI/AppLocalizer.cs b/src/box-winUI/AppLocalizer.cs new file mode 100644 index 0000000..c84d9be --- /dev/null +++ b/src/box-winUI/AppLocalizer.cs @@ -0,0 +1,313 @@ +using System.Text.RegularExpressions; +using YMhut.Box.Core.Settings; +using YMhut.Box.Core.Tools; +using YMhut.Box.WinUI.Services; + +namespace YMhut.Box.WinUI; + +internal static class AppLocalizer +{ + private static readonly IReadOnlyDictionary ToolNames = + new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["dev_environment_config"] = ("开发环境配置", "Development Environment Setup"), + ["json_formatter"] = ("JSON 格式化", "JSON Formatter"), + ["base64_codec"] = ("Base64 编解码", "Base64 Encoder / Decoder"), + ["compression_codec"] = ("压缩编解码", "Compression Codec"), + ["url_codec"] = ("URL 编解码", "URL Encoder / Decoder"), + ["html_entity"] = ("HTML 实体转换", "HTML Entity Converter"), + ["html_minifier"] = ("HTML/代码压缩", "HTML / Code Minifier"), + ["regex_tool"] = ("正则测试", "Regex Tester"), + ["js_obfuscator"] = ("JS 代码混淆", "JavaScript Obfuscator"), + ["uuid_generator"] = ("UUID 生成", "UUID Generator"), + ["ulid_generator"] = ("ULID 生成", "ULID Generator"), + ["timestamp_converter"] = ("时间戳转换", "Timestamp Converter"), + ["number_base"] = ("进制转换", "Number Base Converter"), + ["number_base_converter"] = ("进制转换器", "Number Base Converter"), + ["csv_json_converter"] = ("CSV/JSON 转换", "CSV / JSON Converter"), + ["column_extractor"] = ("列提取器", "Column Extractor"), + ["csv_tsv_converter"] = ("CSV/TSV 转换", "CSV / TSV Converter"), + ["yaml_json_converter"] = ("YAML/JSON 转换", "YAML / JSON Converter"), + ["xml_formatter"] = ("XML 格式化", "XML Formatter"), + ["sql_formatter"] = ("SQL 格式化", "SQL Formatter"), + ["unicode_codec"] = ("Unicode 转义", "Unicode Escape Converter"), + ["punycode_codec"] = ("Punycode/IDN 转换", "Punycode / IDN Converter"), + ["jwt_decoder"] = ("JWT 解码", "JWT Decoder"), + ["query_builder"] = ("Query 参数构建", "Query Builder"), + ["form_urlencoded_builder"] = ("表单编码构建", "Form URL Encoded Builder"), + ["key_value_converter"] = ("键值对转换", "Key / Value Converter"), + ["cron_helper"] = ("Cron 辅助", "Cron Helper"), + ["unicode_block_lookup"] = ("Unicode 区段速查", "Unicode Block Lookup"), + ["charset_lookup"] = ("字符集速查", "Charset Lookup"), + ["regex_preset_lookup"] = ("正则预设速查", "Regex Preset Lookup"), + ["magic_number_lookup"] = ("文件头速查", "Magic Number Lookup"), + ["html_text_extractor"] = ("HTML 正文提取", "HTML Text Extractor"), + ["slug_generator"] = ("Slug 生成", "Slug Generator"), + ["markdown_table_normalizer"] = ("Markdown 表格整理", "Markdown Table Normalizer"), + ["markdown_preview"] = ("Markdown 预览整理", "Markdown Preview / Cleaner"), + ["json_path_helper"] = ("JSON 路径辅助", "JSON Path Helper"), + ["hash_generator"] = ("哈希计算", "Hash Generator"), + ["hmac_generator"] = ("HMAC 生成", "HMAC Generator"), + ["totp_generator"] = ("TOTP 验证码", "TOTP Generator"), + ["password_generator"] = ("密码生成", "Password Generator"), + ["profanity_check"] = ("敏感词检查", "Profanity Check"), + ["hash_manifest_builder"] = ("哈希清单生成", "Hash Manifest Builder"), + ["hash_manifest_verify"] = ("哈希清单校验", "Hash Manifest Verifier"), + ["safe_browser"] = ("安全浏览器", "Safe Browser"), + ["wx_domain_check"] = ("微信域名检测说明", "WeChat Domain Check"), + ["ip_lookup"] = ("IP 归属查询", "IP Lookup"), + ["ip_info"] = ("IP/域名详情", "IP / Domain Info"), + ["dns_query"] = ("DNS 解析", "DNS Query"), + ["domain_price"] = ("域名官方信息", "Domain Official Info"), + ["smart_search"] = ("聚合搜索", "Smart Search"), + ["rdap_domain_lookup"] = ("域名 RDAP 查询", "Domain RDAP Lookup"), + ["rdap_ip_lookup"] = ("IP/ASN RDAP 查询", "IP / ASN RDAP Lookup"), + ["http_diagnostic"] = ("HTTP 诊断", "HTTP Diagnostic"), + ["url_redirect_trace"] = ("URL 跳转追踪", "URL Redirect Trace"), + ["url_inspector"] = ("URL 结构解析", "URL Inspector"), + ["domain_extractor"] = ("域名提取", "Domain Extractor"), + ["cidr_subnet_calculator"] = ("CIDR 子网计算", "CIDR Subnet Calculator"), + ["mime_lookup"] = ("MIME 类型速查", "MIME Lookup"), + ["http_status_lookup"] = ("HTTP 状态码速查", "HTTP Status Lookup"), + ["port_lookup"] = ("常见端口速查", "Port Lookup"), + ["dns_record_lookup"] = ("DNS 记录速查", "DNS Record Lookup"), + ["weather"] = ("天气查询", "Weather"), + ["hotboard"] = ("综合热榜", "Hotboard"), + ["bili_hot"] = ("B 站热榜", "Bilibili Hot"), + ["baidu_hot"] = ("百度热搜", "Baidu Hot Search"), + ["zhihu_hot"] = ("知乎热榜", "Zhihu Hot"), + ["cctv_news"] = ("央视新闻", "CCTV News"), + ["tech_news"] = ("科技新闻", "Tech News"), + ["football_news"] = ("体育新闻", "Sports News"), + ["movie_box_office"] = ("电影票房", "Movie Box Office"), + ["earthquake_info"] = ("地震信息", "Earthquake Info"), + ["gold_price"] = ("黄金价格", "Gold Price"), + ["oil_price"] = ("成品油价格政策", "Oil Price Policy"), + ["train_query"] = ("列车查询", "Train Query"), + ["history_today"] = ("历史上的今天", "On This Day"), + ["car_info"] = ("车辆信息", "Vehicle Info"), + ["sanguosha_skin"] = ("三国杀皮肤", "Sanguosha Skin"), + ["calculator_tool"] = ("计算器", "Calculator"), + ["unit_converter"] = ("单位换算", "Unit Converter"), + ["bmi_calculator"] = ("BMI 计算", "BMI Calculator"), + ["percentage_calculator"] = ("百分比计算", "Percentage Calculator"), + ["percentage_change_calculator"] = ("涨跌幅计算", "Percentage Change Calculator"), + ["discount_calculator"] = ("折扣计算", "Discount Calculator"), + ["tax_calculator"] = ("税费计算", "Tax Calculator"), + ["ratio_simplifier"] = ("比例化简", "Ratio Simplifier"), + ["average_calculator"] = ("平均值计算", "Average Calculator"), + ["median_calculator"] = ("中位数计算", "Median Calculator"), + ["gcd_lcm_calculator"] = ("最大公约数/最小公倍数", "GCD / LCM Calculator"), + ["permutation_calculator"] = ("排列数计算", "Permutation Calculator"), + ["combination_calculator"] = ("组合数计算", "Combination Calculator"), + ["date_difference_calculator"] = ("日期差计算", "Date Difference Calculator"), + ["workday_calculator"] = ("工作日统计", "Workday Calculator"), + ["age_calculator"] = ("年龄计算", "Age Calculator"), + ["simple_interest_calculator"] = ("单利计算", "Simple Interest Calculator"), + ["compound_interest_calculator"] = ("复利计算", "Compound Interest Calculator"), + ["loan_emi_calculator"] = ("等额本息月供", "Loan EMI Calculator"), + ["length_converter"] = ("长度换算", "Length Converter"), + ["weight_converter"] = ("重量换算", "Weight Converter"), + ["temperature_converter"] = ("温度换算", "Temperature Converter"), + ["speed_converter"] = ("速度换算", "Speed Converter"), + ["area_converter"] = ("面积换算", "Area Converter"), + ["volume_converter"] = ("体积换算", "Volume Converter"), + ["data_size_converter"] = ("数据容量换算", "Data Size Converter"), + ["time_duration_converter"] = ("时长换算", "Time Duration Converter"), + ["text_statistics"] = ("文本统计", "Text Statistics"), + ["case_converter"] = ("大小写转换", "Case Converter"), + ["chinese_converter"] = ("简繁转换", "Simplified / Traditional Chinese"), + ["text_diff"] = ("文本对比", "Text Diff"), + ["random_generator"] = ("随机生成器", "Random Generator"), + ["ai_translation"] = ("AI 翻译", "AI Translation"), + ["ymhut_uutool_suite"] = ("UU 工具合集", "UU Tool Suite"), + ["text_deduplicator"] = ("文本去重", "Text Deduplicator"), + ["text_sorter"] = ("文本排序", "Text Sorter"), + ["text_filter"] = ("文本过滤", "Text Filter"), + ["delimiter_converter"] = ("分隔符转换", "Delimiter Converter"), + ["filename_formatter"] = ("文件名格式化", "Filename Formatter"), + ["text_splitter"] = ("文本拆分", "Text Splitter"), + ["text_merger"] = ("文本合并", "Text Merger"), + ["text_numbering"] = ("文本编号", "Text Numbering"), + ["blank_line_cleaner"] = ("空行清理", "Blank Line Cleaner"), + ["whitespace_normalizer"] = ("空白规范化", "Whitespace Normalizer"), + ["prefix_suffix_batch"] = ("前后缀批处理", "Prefix / Suffix Batch"), + ["line_column_transformer"] = ("行列互转", "Line / Column Transformer"), + ["list_sampler"] = ("列表抽样", "List Sampler"), + ["list_chunker"] = ("列表分块", "List Chunker"), + ["list_intersection"] = ("列表交集", "List Intersection"), + ["list_union"] = ("列表并集", "List Union"), + ["list_difference"] = ("列表差集", "List Difference"), + ["list_group_statistics"] = ("列表分组统计", "List Group Statistics"), + ["duplicate_detector"] = ("重复项检测", "Duplicate Detector"), + ["line_length_analyzer"] = ("行长度分析", "Line Length Analyzer"), + ["keyword_counter"] = ("关键词计数", "Keyword Counter"), + ["template_filler"] = ("模板填充", "Template Filler"), + ["path_normalizer"] = ("路径规范化", "Path Normalizer"), + ["path_breakdown"] = ("路径拆解", "Path Breakdown"), + ["extension_extractor"] = ("扩展名提取", "Extension Extractor"), + ["extension_statistics"] = ("扩展名统计", "Extension Statistics"), + ["file_manifest_builder"] = ("文件清单生成", "File Manifest Builder"), + ["batch_rename_preview"] = ("批量重命名预览", "Batch Rename Preview"), + ["indexed_rename_preview"] = ("序号重命名预览", "Indexed Rename Preview"), + ["manifest_deduplicator"] = ("清单去重", "Manifest Deduplicator"), + ["manifest_sorter"] = ("清单排序", "Manifest Sorter"), + ["manifest_template_export"] = ("清单模板导出", "Manifest Template Export"), + ["text_trimmer"] = ("文本裁剪", "Text Trimmer"), + ["text_reverser"] = ("文本反转", "Text Reverser"), + ["markdown_list_cleaner"] = ("Markdown 列表清理", "Markdown List Cleaner"), + ["line_pair_zipper"] = ("双列合并", "Line Pair Zipper"), + ["ascii_art"] = ("ASCII 艺术", "ASCII Art"), + ["color_picker"] = ("颜色工具", "Color Tool"), + ["color_contrast_checker"] = ("颜色对比度", "Color Contrast Checker"), + ["qr_generator"] = ("二维码生成", "QR Code Generator"), + ["qrcode_scanner"] = ("二维码扫描", "QR Code Scanner"), + ["archive_tool"] = ("归档工具", "Archive Tool"), + ["image_processor"] = ("图片处理", "Image Processor"), + ["image_metadata_inspector"] = ("图片元数据", "Image Metadata Inspector"), + ["media_player"] = ("媒体播放器", "Media Player"), + ["qq_avatar"] = ("QQ 资料查询", "QQ Profile"), + ["random_cinema"] = ("随机放映室", "Random Cinema"), + ["shelf_life"] = ("保质期计算", "Shelf Life Calculator"), + ["timezone_abbr_lookup"] = ("时区缩写速查", "Timezone Abbreviation Lookup"), + ["system_info"] = ("系统信息", "System Info"), + ["system_tool"] = ("系统工具", "System Tools"), + ["pc_benchmark"] = ("性能跑分", "PC Benchmark") + }; + + internal static bool IsEnglish => string.Equals(CurrentLanguage, "en-US", StringComparison.OrdinalIgnoreCase); + + internal static string CurrentLanguage + { + get + { + try + { + return AppServices.GetRequiredService().Current.Language; + } + catch + { + return "zh-CN"; + } + } + } + + internal static string T(string zh, string en) => IsEnglish ? en : zh; + + internal static string ToolName(string id, string fallback = "") + { + if (ToolNames.TryGetValue(id, out var name)) + { + return IsEnglish ? name.En : name.Zh; + } + + return string.IsNullOrWhiteSpace(fallback) || LooksBroken(fallback) ? Humanize(id) : fallback; + } + + internal static string ToolNameIn(string id, bool english) + { + if (ToolNames.TryGetValue(id, out var name)) + { + return english ? name.En : name.Zh; + } + + return Humanize(id); + } + + internal static string ToolDescription(IToolModule module) + { + var name = ToolName(module.Id); + return module.Metadata.Category switch + { + ToolCategory.Plugin => T($"{name} 由插件提供,在独立窗口中隔离运行。", $"{name} is provided by a plugin and runs in an isolated window."), + ToolCategory.Dev => T($"{name} 使用原生 WinUI 输入与结果预览,适合开发调试。", $"{name} uses native WinUI inputs and previews for development workflows."), + ToolCategory.Network => T($"{name} 通过共享网络服务获取或解析公开数据,界面不会展示敏感接口地址。", $"{name} uses the shared network service for public data without exposing sensitive endpoints."), + ToolCategory.Security => T($"{name} 默认在本地执行,适合摘要、校验和安全辅助。", $"{name} runs locally by default for hashes, validation, and security helpers."), + ToolCategory.Data => T($"{name} 进入页面后加载真实数据源,并以卡片或列表展示。", $"{name} loads live data on open and renders cards or lists."), + ToolCategory.Calculator => T($"{name} 提供表单式参数输入和结构化计算结果。", $"{name} provides form inputs and structured calculation results."), + ToolCategory.Text => T($"{name} 针对文本、列表或路径执行本地处理。", $"{name} processes text, lists, or paths locally."), + ToolCategory.Image => T($"{name} 使用系统文件选择器和原生媒体能力处理文件。", $"{name} uses native file pickers and media capabilities."), + ToolCategory.Design => T($"{name} 面向颜色、二维码和视觉辅助场景。", $"{name} supports color, QR, and visual helper workflows."), + ToolCategory.Life => T($"{name} 面向日常查询和生活场景。", $"{name} supports daily lookup and lifestyle workflows."), + ToolCategory.System => T($"{name} 调用 Windows 原生能力并展示本机摘要。", $"{name} launches Windows-native capabilities and local summaries."), + _ => T($"{name} 已迁移为 WinUI 3 原生工具页。", $"{name} has been migrated to a native WinUI 3 tool page.") + }; + } + + internal static string CategoryLabel(ToolCategory category) + { + return category switch + { + ToolCategory.Plugin => T("插件工具", "Plugin tools"), + ToolCategory.Dev => T("开发调试", "Developer"), + ToolCategory.Network => T("网络工具", "Network"), + ToolCategory.Security => T("安全校验", "Security"), + ToolCategory.Data => T("数据资讯", "Data"), + ToolCategory.Calculator => T("计算换算", "Calculator"), + ToolCategory.Text => T("文本处理", "Text"), + ToolCategory.Image => T("图像媒体", "Image & Media"), + ToolCategory.Design => T("设计辅助", "Design"), + ToolCategory.Life => T("生活查询", "Life"), + ToolCategory.System => T("系统工具", "System"), + _ => T("全部工具", "All tools") + }; + } + + internal static string CategoryHint(ToolCategory category) + { + return category switch + { + ToolCategory.All => T("全部原生与插件工具", "All native and plugin tools"), + ToolCategory.Plugin => T("已启用插件提供的工具", "Tools provided by enabled plugins"), + _ => T("按分类筛选", "Filter by category") + }; + } + + internal static string ToolCount(int count) => IsEnglish ? $"{count} tools" : $"{count} 个工具"; + + internal static string Capability(bool offline) => offline ? T("本地", "Local") : T("联网", "Online"); + + internal static string SanitizeSensitiveText(string? message, int maxLength = 120) + { + if (string.IsNullOrWhiteSpace(message)) + { + return T("未知错误", "Unknown error"); + } + + var sanitized = Regex.Replace( + message, + @"https?://[^\s\]\)""'<>]+", + T("远程服务", "remote service"), + RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); + + sanitized = Regex.Replace( + sanitized, + @"update\.ymhut\.cn[^\s\]\)""'<>]*", + T("远程服务", "remote service"), + RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); + + sanitized = sanitized + .Replace("update-info.json", T("发布服务", "release service"), StringComparison.OrdinalIgnoreCase) + .Replace("media-types.json", T("媒体配置", "media configuration"), StringComparison.OrdinalIgnoreCase) + .Replace("api_url", T("媒体源", "media source"), StringComparison.OrdinalIgnoreCase) + .Replace("download_url", T("下载源", "download source"), StringComparison.OrdinalIgnoreCase); + + if (sanitized.Length <= maxLength) + { + return sanitized; + } + + return sanitized[..Math.Max(0, maxLength - 3)] + "..."; + } + + private static bool LooksBroken(string value) + { + return value.Contains('�') || value.Contains("鐢", StringComparison.Ordinal) || value.Contains("宸", StringComparison.Ordinal); + } + + private static string Humanize(string id) + { + var words = id.Split('_', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + return string.Join(" ", words.Select(word => char.ToUpperInvariant(word[0]) + word[1..])); + } +} diff --git a/src/box-winUI/Assets/LockScreenLogo.png b/src/box-winUI/Assets/LockScreenLogo.png new file mode 100644 index 0000000..c0e57c6 Binary files /dev/null and b/src/box-winUI/Assets/LockScreenLogo.png differ diff --git a/src/box-winUI/Assets/SplashScreen.png b/src/box-winUI/Assets/SplashScreen.png new file mode 100644 index 0000000..f681b70 Binary files /dev/null and b/src/box-winUI/Assets/SplashScreen.png differ diff --git a/src/box-winUI/Assets/Square150x150Logo.png b/src/box-winUI/Assets/Square150x150Logo.png new file mode 100644 index 0000000..f482e4e Binary files /dev/null and b/src/box-winUI/Assets/Square150x150Logo.png differ diff --git a/src/box-winUI/Assets/Square44x44Logo.png b/src/box-winUI/Assets/Square44x44Logo.png new file mode 100644 index 0000000..3a37741 Binary files /dev/null and b/src/box-winUI/Assets/Square44x44Logo.png differ diff --git a/src/box-winUI/Assets/StoreLogo.png b/src/box-winUI/Assets/StoreLogo.png new file mode 100644 index 0000000..f1cdf82 Binary files /dev/null and b/src/box-winUI/Assets/StoreLogo.png differ diff --git a/src/box-winUI/Assets/Wide310x150Logo.png b/src/box-winUI/Assets/Wide310x150Logo.png new file mode 100644 index 0000000..6530c38 Binary files /dev/null and b/src/box-winUI/Assets/Wide310x150Logo.png differ diff --git a/src/box-winUI/Assets/app_icon.ico b/src/box-winUI/Assets/app_icon.ico new file mode 100644 index 0000000..08918ba Binary files /dev/null and b/src/box-winUI/Assets/app_icon.ico differ diff --git a/src/box-winUI/Assets/home-globe/draco/draco_decoder.js b/src/box-winUI/Assets/home-globe/draco/draco_decoder.js new file mode 100644 index 0000000..6629469 --- /dev/null +++ b/src/box-winUI/Assets/home-globe/draco/draco_decoder.js @@ -0,0 +1,34 @@ + +var DracoDecoderModule = (() => { + var _scriptDir = typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined; + if (typeof __filename !== 'undefined') _scriptDir = _scriptDir || __filename; + return ( +function(DracoDecoderModule = {}) { + +var Module=typeof DracoDecoderModule!="undefined"?DracoDecoderModule:{};var readyPromiseResolve,readyPromiseReject;Module["ready"]=new Promise(function(resolve,reject){readyPromiseResolve=resolve;readyPromiseReject=reject});var isRuntimeInitialized=false;var isModuleParsed=false;Module["onRuntimeInitialized"]=function(){isRuntimeInitialized=true;if(isModuleParsed){if(typeof Module["onModuleLoaded"]==="function"){Module["onModuleLoaded"](Module)}}};Module["onModuleParsed"]=function(){isModuleParsed=true;if(isRuntimeInitialized){if(typeof Module["onModuleLoaded"]==="function"){Module["onModuleLoaded"](Module)}}};function isVersionSupported(versionString){if(typeof versionString!=="string")return false;const version=versionString.split(".");if(version.length<2||version.length>3)return false;if(version[0]==1&&version[1]>=0&&version[1]<=5)return true;if(version[0]!=0||version[1]>10)return false;return true}Module["isVersionSupported"]=isVersionSupported;var moduleOverrides=Object.assign({},Module);var arguments_=[];var thisProgram="./this.program";var quit_=(status,toThrow)=>{throw toThrow};var ENVIRONMENT_IS_WEB=typeof window=="object";var ENVIRONMENT_IS_WORKER=typeof importScripts=="function";var ENVIRONMENT_IS_NODE=typeof process=="object"&&typeof process.versions=="object"&&typeof process.versions.node=="string";var scriptDirectory="";function locateFile(path){if(Module["locateFile"]){return Module["locateFile"](path,scriptDirectory)}return scriptDirectory+path}var read_,readAsync,readBinary,setWindowTitle;function logExceptionOnExit(e){if(e instanceof ExitStatus)return;let toLog=e;err("exiting due to exception: "+toLog)}if(ENVIRONMENT_IS_NODE){var fs=require("fs");var nodePath=require("path");if(ENVIRONMENT_IS_WORKER){scriptDirectory=nodePath.dirname(scriptDirectory)+"/"}else{scriptDirectory=__dirname+"/"}read_=(filename,binary)=>{var ret=tryParseAsDataURI(filename);if(ret){return binary?ret:ret.toString()}filename=isFileURI(filename)?new URL(filename):nodePath.normalize(filename);return fs.readFileSync(filename,binary?undefined:"utf8")};readBinary=filename=>{var ret=read_(filename,true);if(!ret.buffer){ret=new Uint8Array(ret)}return ret};readAsync=(filename,onload,onerror)=>{var ret=tryParseAsDataURI(filename);if(ret){onload(ret)}filename=isFileURI(filename)?new URL(filename):nodePath.normalize(filename);fs.readFile(filename,function(err,data){if(err)onerror(err);else onload(data.buffer)})};if(process["argv"].length>1){thisProgram=process["argv"][1].replace(/\\/g,"/")}arguments_=process["argv"].slice(2);quit_=(status,toThrow)=>{if(keepRuntimeAlive()){process["exitCode"]=status;throw toThrow}logExceptionOnExit(toThrow);process["exit"](status)};Module["inspect"]=function(){return"[Emscripten Module object]"}}else if(ENVIRONMENT_IS_WEB||ENVIRONMENT_IS_WORKER){if(ENVIRONMENT_IS_WORKER){scriptDirectory=self.location.href}else if(typeof document!="undefined"&&document.currentScript){scriptDirectory=document.currentScript.src}if(_scriptDir){scriptDirectory=_scriptDir}if(scriptDirectory.indexOf("blob:")!==0){scriptDirectory=scriptDirectory.substr(0,scriptDirectory.replace(/[?#].*/,"").lastIndexOf("/")+1)}else{scriptDirectory=""}{read_=url=>{try{var xhr=new XMLHttpRequest;xhr.open("GET",url,false);xhr.send(null);return xhr.responseText}catch(err){var data=tryParseAsDataURI(url);if(data){return intArrayToString(data)}throw err}};if(ENVIRONMENT_IS_WORKER){readBinary=url=>{try{var xhr=new XMLHttpRequest;xhr.open("GET",url,false);xhr.responseType="arraybuffer";xhr.send(null);return new Uint8Array(xhr.response)}catch(err){var data=tryParseAsDataURI(url);if(data){return data}throw err}}}readAsync=(url,onload,onerror)=>{var xhr=new XMLHttpRequest;xhr.open("GET",url,true);xhr.responseType="arraybuffer";xhr.onload=()=>{if(xhr.status==200||xhr.status==0&&xhr.response){onload(xhr.response);return}var data=tryParseAsDataURI(url);if(data){onload(data.buffer);return}onerror()};xhr.onerror=onerror;xhr.send(null)}}setWindowTitle=title=>document.title=title}else{}var out=Module["print"]||console.log.bind(console);var err=Module["printErr"]||console.warn.bind(console);Object.assign(Module,moduleOverrides);moduleOverrides=null;if(Module["arguments"])arguments_=Module["arguments"];if(Module["thisProgram"])thisProgram=Module["thisProgram"];if(Module["quit"])quit_=Module["quit"];var wasmBinary;if(Module["wasmBinary"])wasmBinary=Module["wasmBinary"];var noExitRuntime=Module["noExitRuntime"]||true;var WebAssembly={Memory:function(opts){this.buffer=new ArrayBuffer(opts["initial"]*65536)},Module:function(binary){},Instance:function(module,info){this.exports=( +// EMSCRIPTEN_START_ASM +function instantiate(na){function c(d){d.set=function(a,b){this[a]=b};d.get=function(a){return this[a]};return d}var e;var f=new Uint8Array(123);for(var a=25;a>=0;--a){f[48+a]=52+a;f[65+a]=a;f[97+a]=26+a}f[43]=62;f[47]=63;function l(m,n,o){var g,h,a=0,i=n,j=o.length,k=n+(j*3>>2)-(o[j-2]=="=")-(o[j-1]=="=");for(;a>4;if(i>2;if(i>2];s=H[b+12>>2];d=H[b+20>>2];e=H[b+16>>2];g=e+4|0;d=g>>>0<4?d+1|0:d;a:{b:{c:{if(g>>>0>k>>>0&(d|0)>=(s|0)|(d|0)>(s|0)){break c}d=e+H[b>>2]|0;H[a>>2]=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);d=H[b+20>>2];k=d;g=H[b+16>>2];e=g+4|0;d=e>>>0<4?d+1|0:d;H[b+16>>2]=e;H[b+20>>2]=d;if(K[a>>2]>31){break c}s=H[b+8>>2];y=H[b+12>>2];d=k;g=g+8|0;d=g>>>0<8?d+1|0:d;if(g>>>0>s>>>0&(d|0)>=(y|0)|(d|0)>(y|0)){break c}d=e+H[b>>2]|0;H[a+4>>2]=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);d=H[b+20>>2];k=d;g=H[b+16>>2];e=g+4|0;d=e>>>0<4?d+1|0:d;H[b+16>>2]=e;H[b+20>>2]=d;s=H[b+8>>2];y=H[b+12>>2];d=k;g=g+8|0;d=g>>>0<8?d+1|0:d;if(g>>>0>s>>>0&(d|0)>=(y|0)|(d|0)>(y|0)){break c}d=e+H[b>>2]|0;H[a+12>>2]=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);d=H[b+20>>2];k=d;g=H[b+16>>2];e=g+4|0;d=e>>>0<4?d+1|0:d;H[b+16>>2]=e;H[b+20>>2]=d;d=H[a+20>>2];x=H[a+12>>2];if((x|0)!=(d|0)?d:0){break c}s=H[b+8>>2];y=H[b+12>>2];d=k;g=g+8|0;d=g>>>0<8?d+1|0:d;if(g>>>0>s>>>0&(d|0)>=(y|0)|(d|0)>(y|0)){break c}d=e+H[b>>2]|0;e=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[a+16>>2]=e;g=H[b+20>>2];d=H[b+16>>2]+4|0;g=d>>>0<4?g+1|0:g;H[b+16>>2]=d;H[b+20>>2]=g;if(e>>>0>=7){H[B>>2]=e;Qd(1713,B);break c}H[B+664>>2]=c;d:{if(!x){break d}e:{k=H[c>>2];if(x>>>0<=(H[c+8>>2]-k|0)/12>>>0){break e}if(x>>>0<357913942){l=H[c+4>>2];d=N(x,12);e=pa(d);g=d+e|0;e=e+N((l-k|0)/12|0,12)|0;d=e;if((k|0)!=(l|0)){while(1){d=d-12|0;l=l-12|0;H[d>>2]=H[l>>2];H[d+4>>2]=H[l+4>>2];H[d+8>>2]=H[l+8>>2];if((k|0)!=(l|0)){continue}break}}H[c+8>>2]=g;H[c+4>>2]=e;H[c>>2]=d;if(!k){break e}oa(k);break e}break b}f:{switch(H[a+16>>2]){case 0:i=wb(B+8|0,3);z=B+664|0;k=H[b+8>>2];n=H[b+12>>2];d=H[b+20>>2];e=H[b+16>>2];g=e+4|0;d=g>>>0<4?d+1|0:d;g:{if(g>>>0>k>>>0&(d|0)>=(n|0)|(d|0)>(n|0)){break g}d=e+H[b>>2]|0;H[i>>2]=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);d=H[b+20>>2];k=d;g=H[b+16>>2];e=g+4|0;d=e>>>0<4?d+1|0:d;H[b+16>>2]=e;H[b+20>>2]=d;if(K[i>>2]>32){break g}n=H[b+8>>2];s=H[b+12>>2];d=k;g=g+8|0;d=g>>>0<8?d+1|0:d;if(g>>>0>n>>>0&(d|0)>=(s|0)|(d|0)>(s|0)){break g}d=e+H[b>>2]|0;e=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[i+4>>2]=e;g=H[b+20>>2];d=H[b+16>>2]+4|0;g=d>>>0<4?g+1|0:g;H[b+16>>2]=d;H[b+20>>2]=g;if(!e){break g}H[i+8>>2]=0;if(!ua(i+16|0,b)){break g}if(!ua(i+36|0,b)){break g}if(!ua(i+56|0,b)){break g}if(!ua(i+76|0,b)){break g}A=H[i+4>>2];d=0;g=0;f=ca-32|0;ca=f;m=H[i+12>>2];H[f+16>>2]=0;H[f+8>>2]=0;H[f+12>>2]=0;if(m){if(m>>>0>=1073741824){break b}b=m<<2;g=pa(b);H[f+8>>2]=g;d=b+g|0;H[f+16>>2]=d;ra(g,0,b);H[f+12>>2]=d}e=H[i+120>>2];b=H[e>>2];if(b){H[e+4>>2]=b;oa(b);m=H[i+12>>2];g=H[f+8>>2];d=H[f+12>>2]}H[e+4>>2]=d;H[e>>2]=g;H[e+8>>2]=H[f+16>>2];g=0;H[f+16>>2]=0;H[f+8>>2]=0;H[f+12>>2]=0;h:{if(m){if(m>>>0>=1073741824){break b}b=m<<2;w=pa(b);H[f+8>>2]=w;g=b+w|0;H[f+16>>2]=g;ra(w,0,b);H[f+12>>2]=g}d=H[i+132>>2];b=H[d>>2];if(b){H[d+4>>2]=b;oa(b);w=H[f+8>>2];g=H[f+12>>2]}H[d+4>>2]=g;H[d>>2]=w;H[d+8>>2]=H[f+16>>2];H[f+24>>2]=0;H[f+28>>2]=0;H[f+16>>2]=0;H[f+20>>2]=0;H[f+8>>2]=0;H[f+12>>2]=0;xa(f+8|0);d=H[f+24>>2]+H[f+28>>2]|0;b=(d>>>0)/341|0;b=H[H[f+12>>2]+(b<<2)>>2]+N(d-N(b,341)|0,12)|0;H[b+4>>2]=0;H[b+8>>2]=0;H[b>>2]=A;m=H[f+28>>2]+1|0;H[f+28>>2]=m;i:{if(!m){break i}y=i+96|0;while(1){n=H[f+12>>2];g=H[f+24>>2];e=m-1|0;d=g+e|0;b=(d>>>0)/341|0;b=H[n+(b<<2)>>2]+N(d-N(b,341)|0,12)|0;o=H[b+8>>2];k=H[b+4>>2];t=H[b>>2];H[f+28>>2]=e;b=H[f+16>>2];if((((b|0)!=(n|0)?N(b-n>>2,341)-1|0:0)-(g+m|0)|0)+1>>>0>=682){oa(H[b-4>>2]);H[f+16>>2]=H[f+16>>2]-4}b=0;if(t>>>0>A>>>0){break i}d=H[i+12>>2];m=(k|0)!=(d-1|0)?k+1|0:0;if(m>>>0>=d>>>0){break i}q=N(o,12);p=q+H[i+132>>2]|0;l=q+H[i+120>>2]|0;g=H[i>>2];r=m<<2;e=H[r+H[p>>2]>>2];j:{k:{if((g|0)==(e|0)){if(!t){break k}while(1){d=H[l>>2];x=H[d+8>>2];s=H[d+4>>2];n=H[d>>2];q=H[z>>2];m=H[q+4>>2];d=H[q+8>>2];l:{if(m>>>0>>0){H[m+8>>2]=x;H[m+4>>2]=s;H[m>>2]=n;H[q+4>>2]=m+12;break l}r=H[q>>2];g=(m-r|0)/12|0;k=g+1|0;if(k>>>0>=357913942){break b}e=(d-r|0)/12|0;d=e<<1;k=e>>>0>=178956970?357913941:d>>>0>k>>>0?d:k;if(k){if(k>>>0>=357913942){break a}d=pa(N(k,12))}else{d=0}w=d+N(g,12)|0;H[w+8>>2]=x;H[w+4>>2]=s;H[w>>2]=n;e=w+12|0;if((m|0)!=(r|0)){while(1){w=w-12|0;m=m-12|0;H[w>>2]=H[m>>2];H[w+4>>2]=H[m+4>>2];H[w+8>>2]=H[m+8>>2];if((m|0)!=(r|0)){continue}break}}H[q+8>>2]=d+N(k,12);H[q+4>>2]=e;H[q>>2]=w;if(!r){break l}oa(r)}H[i+8>>2]=H[i+8>>2]+1;b=b+1|0;if((t|0)!=(b|0)){continue}break}break k}m:{n:{o:{p:{if(t>>>0<=2){d=H[i+108>>2];H[d>>2]=m;w=1;g=H[i+12>>2];if(g>>>0>1){break p}break m}if(K[i+8>>2]>K[i+4>>2]){break i}b=H[i+120>>2];s=o+1|0;x=N(s,12);d=b+x|0;if((d|0)!=(l|0)){Aa(d,H[l>>2],H[l+4>>2]);b=H[i+120>>2]}b=r+H[b+x>>2]|0;H[b>>2]=H[b>>2]+(1<>2];e=32-k|0;q:{if((n|0)<=(e|0)){e=H[i+28>>2];if((e|0)==H[i+20>>2]){break o}d=H[e>>2];b=k+n|0;H[i+32>>2]=b;w=d<>>32-n|0;if((b|0)!=32){break q}H[i+32>>2]=0;H[i+28>>2]=e+4;break q}g=H[i+28>>2];b=g+4|0;if((b|0)==H[i+20>>2]){break o}d=H[g>>2];H[i+28>>2]=b;b=n-e|0;H[i+32>>2]=b;w=H[g+4>>2]>>>32-b|d<>>32-n}d=t>>>1|0;if(w>>>0>d>>>0){break i}break n}while(1){m=(g-1|0)!=(m|0)?m+1|0:0;H[d+(w<<2)>>2]=m;g=H[i+12>>2];w=w+1|0;if(g>>>0>w>>>0){continue}break}break m}d=t>>>1|0;w=0}r:{s:{e=d-w|0;b=t-e|0;t:{if((b|0)==(e|0)){b=e;break t}n=H[i+88>>2];if((n|0)==H[i+80>>2]){break s}k=H[n>>2];g=H[i+92>>2];d=g+1|0;H[i+92>>2]=d;g=k&-2147483648>>>g;u:{if((d|0)==32){H[i+92>>2]=0;H[i+88>>2]=n+4;if(g){break u}break s}if(!g){break s}}}d=b;b=e;break r}d=e}n=H[i+132>>2];k=n+q|0;g=H[k>>2];e=g+r|0;H[e>>2]=H[e>>2]+1;Aa(n+x|0,g,H[k+4>>2]);if(b){g=H[f+28>>2]+H[f+24>>2]|0;e=H[f+16>>2];w=H[f+12>>2];if((g|0)==(((e|0)!=(w|0)?N(e-w>>2,341)-1|0:0)|0)){xa(f+8|0);w=H[f+12>>2];g=H[f+24>>2]+H[f+28>>2]|0}e=(g>>>0)/341|0;e=H[(e<<2)+w>>2]+N(g-N(e,341)|0,12)|0;H[e+8>>2]=o;H[e+4>>2]=m;H[e>>2]=b;H[f+28>>2]=H[f+28>>2]+1}if(!d){break k}g=H[f+28>>2]+H[f+24>>2]|0;b=H[f+16>>2];w=H[f+12>>2];if((g|0)==(((b|0)!=(w|0)?N(b-w>>2,341)-1|0:0)|0)){xa(f+8|0);w=H[f+12>>2];g=H[f+24>>2]+H[f+28>>2]|0}b=(g>>>0)/341|0;b=H[(b<<2)+w>>2]+N(g-N(b,341)|0,12)|0;H[b+8>>2]=s;H[b+4>>2]=m;H[b>>2]=d;m=H[f+28>>2]+1|0;H[f+28>>2]=m;break j}if(!t){break k}while(1){if(H[i+12>>2]){o=H[i+40>>2];n=H[p>>2];w=H[i+96>>2];k=H[i+108>>2];m=0;while(1){q=k+(m<<2)|0;H[w+(H[q>>2]<<2)>>2]=0;g=H[i>>2];e=H[q>>2]<<2;d=H[e+n>>2];v:{if((g|0)==(d|0)){break v}r=e+w|0;u=g-d|0;x=H[i+52>>2];g=32-x|0;if((u|0)<=(g|0)){e=H[i+48>>2];if((e|0)==(o|0)){break i}H[r>>2]=H[e>>2]<>>32-u;d=u+H[i+52>>2]|0;H[i+52>>2]=d;if((d|0)!=32){break v}H[i+52>>2]=0;H[i+48>>2]=e+4;break v}s=H[i+48>>2];d=s+4|0;if((d|0)==(o|0)){break i}e=H[s>>2];H[i+48>>2]=d;d=u-g|0;H[i+52>>2]=d;H[r>>2]=H[s+4>>2]>>>32-d|e<>>32-u}e=H[q>>2]<<2;d=e+w|0;H[d>>2]=H[d>>2]|H[e+H[l>>2]>>2];m=m+1|0;if(m>>>0>2]){continue}break}}jb(z,y);H[i+8>>2]=H[i+8>>2]+1;b=b+1|0;if((t|0)!=(b|0)){continue}break}}m=H[f+28>>2]}if(m){continue}break}}H[f+28>>2]=0;w=H[f+16>>2];m=H[f+12>>2];g=w-m|0;if(g>>>0>=9){while(1){oa(H[m>>2]);m=H[f+12>>2]+4|0;H[f+12>>2]=m;w=H[f+16>>2];g=w-m|0;if(g>>>0>8){continue}break}}b=170;w:{switch((g>>>2|0)-1|0){case 1:b=341;case 0:H[f+24>>2]=b;break;default:break w}}x:{if((m|0)==(w|0)){break x}while(1){oa(H[m>>2]);m=m+4|0;if((w|0)!=(m|0)){continue}break}d=H[f+16>>2];b=H[f+12>>2];if((d|0)==(b|0)){break x}H[f+16>>2]=d+((b-d|0)+3&-4)}b=H[f+8>>2];if(b){oa(b)}ca=f+32|0;break h}}xb(i);break d;case 1:i=wb(B+8|0,3);A=B+664|0;k=H[b+8>>2];n=H[b+12>>2];d=H[b+20>>2];e=H[b+16>>2];g=e+4|0;d=g>>>0<4?d+1|0:d;y:{if(g>>>0>k>>>0&(d|0)>=(n|0)|(d|0)>(n|0)){break y}d=e+H[b>>2]|0;H[i>>2]=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);d=H[b+20>>2];k=d;g=H[b+16>>2];e=g+4|0;d=e>>>0<4?d+1|0:d;H[b+16>>2]=e;H[b+20>>2]=d;if(K[i>>2]>32){break y}n=H[b+8>>2];s=H[b+12>>2];d=k;g=g+8|0;d=g>>>0<8?d+1|0:d;if(g>>>0>n>>>0&(d|0)>=(s|0)|(d|0)>(s|0)){break y}d=e+H[b>>2]|0;e=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[i+4>>2]=e;g=H[b+20>>2];d=H[b+16>>2]+4|0;g=d>>>0<4?g+1|0:g;H[b+16>>2]=d;H[b+20>>2]=g;if(!e){break y}H[i+8>>2]=0;if(!ua(i+16|0,b)){break y}if(!ua(i+36|0,b)){break y}if(!ua(i+56|0,b)){break y}if(!ua(i+76|0,b)){break y}p=H[i+4>>2];d=0;f=ca-32|0;ca=f;m=H[i+12>>2];H[f+16>>2]=0;H[f+8>>2]=0;H[f+12>>2]=0;if(m){if(m>>>0>=1073741824){break b}b=m<<2;t=pa(b);H[f+8>>2]=t;d=b+t|0;H[f+16>>2]=d;ra(t,0,b);H[f+12>>2]=d}e=H[i+120>>2];b=H[e>>2];if(b){H[e+4>>2]=b;oa(b);m=H[i+12>>2];t=H[f+8>>2];d=H[f+12>>2]}H[e+4>>2]=d;H[e>>2]=t;H[e+8>>2]=H[f+16>>2];t=0;H[f+16>>2]=0;H[f+8>>2]=0;H[f+12>>2]=0;z:{if(m){if(m>>>0>=1073741824){break b}b=m<<2;o=pa(b);H[f+8>>2]=o;t=b+o|0;H[f+16>>2]=t;ra(o,0,b);H[f+12>>2]=t}d=H[i+132>>2];b=H[d>>2];if(b){H[d+4>>2]=b;oa(b);t=H[f+12>>2];o=H[f+8>>2]}H[d+4>>2]=t;H[d>>2]=o;H[d+8>>2]=H[f+16>>2];H[f+24>>2]=0;H[f+28>>2]=0;H[f+16>>2]=0;H[f+20>>2]=0;H[f+8>>2]=0;H[f+12>>2]=0;xa(f+8|0);d=H[f+24>>2]+H[f+28>>2]|0;b=(d>>>0)/341|0;b=H[H[f+12>>2]+(b<<2)>>2]+N(d-N(b,341)|0,12)|0;H[b+4>>2]=0;H[b+8>>2]=0;H[b>>2]=p;m=H[f+28>>2]+1|0;H[f+28>>2]=m;A:{if(!m){break A}s=i+96|0;while(1){k=H[f+12>>2];g=H[f+24>>2];e=m-1|0;d=g+e|0;b=(d>>>0)/341|0;b=H[k+(b<<2)>>2]+N(d-N(b,341)|0,12)|0;q=H[b+8>>2];d=H[b+4>>2];l=H[b>>2];H[f+28>>2]=e;b=H[f+16>>2];if((((b|0)!=(k|0)?N(b-k>>2,341)-1|0:0)-(g+m|0)|0)+1>>>0>=682){oa(H[b-4>>2]);H[f+16>>2]=H[f+16>>2]-4}if(l>>>0>p>>>0){break A}b=H[i+12>>2];m=(d|0)!=(b-1|0)?d+1|0:0;if(m>>>0>=b>>>0){break A}b=H[i+120>>2];r=N(q,12);u=b+r|0;e=H[i>>2];x=m<<2;n=r+H[i+132>>2]|0;d=H[x+H[n>>2]>>2];B:{C:{if((e|0)==(d|0)){x=0;if(!l){break C}while(1){b=H[u>>2];y=H[b+8>>2];n=H[b+4>>2];k=H[b>>2];q=H[A>>2];m=H[q+4>>2];b=H[q+8>>2];D:{if(m>>>0>>0){H[m+8>>2]=y;H[m+4>>2]=n;H[m>>2]=k;H[q+4>>2]=m+12;break D}r=H[q>>2];e=(m-r|0)/12|0;g=e+1|0;if(g>>>0>=357913942){break b}d=(b-r|0)/12|0;b=d<<1;g=d>>>0>=178956970?357913941:b>>>0>g>>>0?b:g;if(g){if(g>>>0>=357913942){break a}b=pa(N(g,12))}else{b=0}o=b+N(e,12)|0;H[o+8>>2]=y;H[o+4>>2]=n;H[o>>2]=k;d=o+12|0;if((m|0)!=(r|0)){while(1){o=o-12|0;m=m-12|0;H[o>>2]=H[m>>2];H[o+4>>2]=H[m+4>>2];H[o+8>>2]=H[m+8>>2];if((m|0)!=(r|0)){continue}break}}H[q+8>>2]=b+N(g,12);H[q+4>>2]=d;H[q>>2]=o;if(!r){break D}oa(r)}H[i+8>>2]=H[i+8>>2]+1;x=x+1|0;if((l|0)!=(x|0)){continue}break}break C}E:{F:{G:{H:{if(l>>>0<=2){b=H[i+108>>2];H[b>>2]=m;o=1;t=H[i+12>>2];if(t>>>0>1){break H}break E}if(K[i+8>>2]>K[i+4>>2]){break A}k=b;b=r+12|0;Aa(k+b|0,H[u>>2],H[u+4>>2]);b=x+H[b+H[i+120>>2]>>2]|0;H[b>>2]=H[b>>2]+(1<>2];e=32-k|0;I:{if((n|0)<=(e|0)){e=H[i+28>>2];if((e|0)==H[i+20>>2]){break G}d=H[e>>2];b=k+n|0;H[i+32>>2]=b;d=d<>>32-n|0;if((b|0)!=32){break I}H[i+32>>2]=0;H[i+28>>2]=e+4;break I}g=H[i+28>>2];b=g+4|0;if((b|0)==H[i+20>>2]){break G}d=H[g>>2];H[i+28>>2]=b;b=n-e|0;H[i+32>>2]=b;d=H[g+4>>2]>>>32-b|d<>>32-n}o=l>>>1|0;if(o>>>0>>0){break A}break F}while(1){m=(t-1|0)!=(m|0)?m+1|0:0;H[b+(o<<2)>>2]=m;o=o+1|0;t=H[i+12>>2];if(o>>>0>>0){continue}break}break E}o=l>>>1|0;d=0}y=q+1|0;J:{K:{e=o-d|0;d=l-e|0;L:{if((d|0)==(e|0)){b=e;break L}n=H[i+88>>2];if((n|0)==H[i+80>>2]){break K}k=H[n>>2];g=H[i+92>>2];b=g+1|0;H[i+92>>2]=b;g=k&-2147483648>>>g;M:{if((b|0)==32){H[i+92>>2]=0;H[i+88>>2]=n+4;if(g){break M}break K}if(!g){break K}}b=d}d=e;break J}b=e}n=H[i+132>>2];k=n+r|0;g=H[k>>2];e=g+x|0;H[e>>2]=H[e>>2]+1;Aa(n+N(y,12)|0,g,H[k+4>>2]);if(d){t=H[f+28>>2]+H[f+24>>2]|0;e=H[f+16>>2];o=H[f+12>>2];if((t|0)==(((e|0)!=(o|0)?N(e-o>>2,341)-1|0:0)|0)){xa(f+8|0);t=H[f+24>>2]+H[f+28>>2]|0;o=H[f+12>>2]}e=(t>>>0)/341|0;e=H[o+(e<<2)>>2]+N(t-N(e,341)|0,12)|0;H[e+8>>2]=q;H[e+4>>2]=m;H[e>>2]=d;H[f+28>>2]=H[f+28>>2]+1}if(!b){break C}t=H[f+28>>2]+H[f+24>>2]|0;d=H[f+16>>2];o=H[f+12>>2];if((t|0)==(((d|0)!=(o|0)?N(d-o>>2,341)-1|0:0)|0)){xa(f+8|0);t=H[f+24>>2]+H[f+28>>2]|0;o=H[f+12>>2]}d=(t>>>0)/341|0;d=H[o+(d<<2)>>2]+N(t-N(d,341)|0,12)|0;H[d+8>>2]=y;H[d+4>>2]=m;H[d>>2]=b;m=H[f+28>>2]+1|0;H[f+28>>2]=m;break B}t=0;if(!l){break C}while(1){if(H[i+12>>2]){o=H[i+40>>2];k=H[n>>2];z=H[i+96>>2];g=H[i+108>>2];m=0;while(1){q=g+(m<<2)|0;H[z+(H[q>>2]<<2)>>2]=0;e=H[i>>2];d=H[q>>2]<<2;b=H[d+k>>2];N:{if((e|0)==(b|0)){break N}r=d+z|0;w=e-b|0;x=H[i+52>>2];e=32-x|0;if((w|0)<=(e|0)){d=H[i+48>>2];if((d|0)==(o|0)){break A}H[r>>2]=H[d>>2]<>>32-w;b=w+H[i+52>>2]|0;H[i+52>>2]=b;if((b|0)!=32){break N}H[i+52>>2]=0;H[i+48>>2]=d+4;break N}y=H[i+48>>2];b=y+4|0;if((b|0)==(o|0)){break A}d=H[y>>2];H[i+48>>2]=b;b=w-e|0;H[i+52>>2]=b;H[r>>2]=H[y+4>>2]>>>32-b|d<>>32-w}d=H[q>>2]<<2;b=d+z|0;H[b>>2]=H[b>>2]|H[d+H[u>>2]>>2];m=m+1|0;if(m>>>0>2]){continue}break}}jb(A,s);H[i+8>>2]=H[i+8>>2]+1;t=t+1|0;if((l|0)!=(t|0)){continue}break}}m=H[f+28>>2]}if(m){continue}break}}H[f+28>>2]=0;o=H[f+16>>2];m=H[f+12>>2];t=o-m|0;if(t>>>0>=9){while(1){oa(H[m>>2]);m=H[f+12>>2]+4|0;H[f+12>>2]=m;o=H[f+16>>2];t=o-m|0;if(t>>>0>8){continue}break}}b=170;O:{switch((t>>>2|0)-1|0){case 1:b=341;case 0:H[f+24>>2]=b;break;default:break O}}P:{if((m|0)==(o|0)){break P}while(1){oa(H[m>>2]);m=m+4|0;if((o|0)!=(m|0)){continue}break}d=H[f+16>>2];b=H[f+12>>2];if((d|0)==(b|0)){break P}H[f+16>>2]=d+((b-d|0)+3&-4)}b=H[f+8>>2];if(b){oa(b)}ca=f+32|0;break z}}xb(i);break d;case 2:f=ub(B+8|0,3);w=B+664|0;k=H[b+8>>2];n=H[b+12>>2];d=H[b+20>>2];e=H[b+16>>2];g=e+4|0;d=g>>>0<4?d+1|0:d;Q:{if(g>>>0>k>>>0&(d|0)>=(n|0)|(d|0)>(n|0)){break Q}d=e+H[b>>2]|0;H[f>>2]=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);d=H[b+20>>2];k=d;g=H[b+16>>2];e=g+4|0;d=e>>>0<4?d+1|0:d;H[b+16>>2]=e;H[b+20>>2]=d;if(K[f>>2]>32){break Q}n=H[b+8>>2];s=H[b+12>>2];d=k;g=g+8|0;d=g>>>0<8?d+1|0:d;if(g>>>0>n>>>0&(d|0)>=(s|0)|(d|0)>(s|0)){break Q}d=e+H[b>>2]|0;e=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[f+4>>2]=e;g=H[b+20>>2];d=H[b+16>>2]+4|0;g=d>>>0<4?g+1|0:g;H[b+16>>2]=d;H[b+20>>2]=g;if(!e){break Q}H[f+8>>2]=0;if(!ta(f+16|0,b)){break Q}if(!ua(f+32|0,b)){break Q}if(!ua(f+52|0,b)){break Q}if(!ua(f+72|0,b)){break Q}z=H[f+4>>2];g=0;b=0;h=ca-32|0;ca=h;j=H[f+12>>2];H[h+16>>2]=0;H[h+8>>2]=0;H[h+12>>2]=0;if(j){if(j>>>0>=1073741824){break b}d=j<<2;g=pa(d);H[h+8>>2]=g;b=d+g|0;H[h+16>>2]=b;ra(g,0,d);H[h+12>>2]=b}e=H[f+116>>2];d=H[e>>2];if(d){H[e+4>>2]=d;oa(d);j=H[f+12>>2];g=H[h+8>>2];b=H[h+12>>2]}H[e+4>>2]=b;H[e>>2]=g;H[e+8>>2]=H[h+16>>2];g=0;H[h+16>>2]=0;H[h+8>>2]=0;H[h+12>>2]=0;R:{if(j){if(j>>>0>=1073741824){break b}b=j<<2;u=pa(b);H[h+8>>2]=u;g=b+u|0;H[h+16>>2]=g;ra(u,0,b);H[h+12>>2]=g}d=H[f+128>>2];b=H[d>>2];if(b){H[d+4>>2]=b;oa(b);u=H[h+8>>2];g=H[h+12>>2]}H[d+4>>2]=g;H[d>>2]=u;H[d+8>>2]=H[h+16>>2];H[h+24>>2]=0;H[h+28>>2]=0;H[h+16>>2]=0;H[h+20>>2]=0;H[h+8>>2]=0;H[h+12>>2]=0;xa(h+8|0);d=H[h+24>>2]+H[h+28>>2]|0;b=(d>>>0)/341|0;b=H[H[h+12>>2]+(b<<2)>>2]+N(d-N(b,341)|0,12)|0;H[b+4>>2]=0;H[b+8>>2]=0;H[b>>2]=z;j=H[h+28>>2]+1|0;H[h+28>>2]=j;S:{if(!j){break S}x=f+92|0;y=f+16|0;while(1){n=H[h+12>>2];g=H[h+24>>2];e=j-1|0;d=g+e|0;b=(d>>>0)/341|0;b=H[n+(b<<2)>>2]+N(d-N(b,341)|0,12)|0;p=H[b+8>>2];k=H[b+4>>2];i=H[b>>2];H[h+28>>2]=e;b=H[h+16>>2];if((((b|0)!=(n|0)?N(b-n>>2,341)-1|0:0)-(g+j|0)|0)+1>>>0>=682){oa(H[b-4>>2]);H[h+16>>2]=H[h+16>>2]-4}d=0;if(i>>>0>z>>>0){break S}b=H[f+12>>2];j=(k|0)!=(b-1|0)?k+1|0:0;if(j>>>0>=b>>>0){break S}o=N(p,12);A=o+H[f+128>>2]|0;t=o+H[f+116>>2]|0;g=H[f>>2];q=j<<2;e=H[q+H[A>>2]>>2];T:{if((g|0)==(e|0)){if(!i){break T}while(1){b=H[t>>2];r=H[b+8>>2];s=H[b+4>>2];n=H[b>>2];o=H[w>>2];j=H[o+4>>2];b=H[o+8>>2];U:{if(j>>>0>>0){H[j+8>>2]=r;H[j+4>>2]=s;H[j>>2]=n;H[o+4>>2]=j+12;break U}q=H[o>>2];g=(j-q|0)/12|0;k=g+1|0;if(k>>>0>=357913942){break b}e=(b-q|0)/12|0;b=e<<1;k=e>>>0>=178956970?357913941:b>>>0>k>>>0?b:k;if(k){if(k>>>0>=357913942){break a}b=pa(N(k,12))}else{b=0}u=b+N(g,12)|0;H[u+8>>2]=r;H[u+4>>2]=s;H[u>>2]=n;e=u+12|0;if((j|0)!=(q|0)){while(1){u=u-12|0;j=j-12|0;H[u>>2]=H[j>>2];H[u+4>>2]=H[j+4>>2];H[u+8>>2]=H[j+8>>2];if((j|0)!=(q|0)){continue}break}}H[o+8>>2]=b+N(k,12);H[o+4>>2]=e;H[o>>2]=u;if(!q){break U}oa(q)}H[f+8>>2]=H[f+8>>2]+1;d=d+1|0;if((i|0)!=(d|0)){continue}break}break T}V:{W:{X:{Y:{if(i>>>0<=2){b=H[f+104>>2];H[b>>2]=j;u=1;g=H[f+12>>2];if(g>>>0>1){break Y}break V}if(K[f+8>>2]>K[f+4>>2]){break S}b=H[f+116>>2];s=p+1|0;r=N(s,12);d=b+r|0;if((d|0)!=(t|0)){Aa(d,H[t>>2],H[t+4>>2]);b=H[f+116>>2]}b=q+H[b+r>>2]|0;H[b>>2]=H[b>>2]+(1<>2]=0;pc(y,Q(i)^31,h+4|0);d=i>>>1|0;b=H[h+4>>2];if(d>>>0>>0){break S}e=d-b|0;d=i-e|0;Z:{if((d|0)==(e|0)){b=e;break Z}n=H[f+84>>2];if((n|0)==H[f+76>>2]){break X}k=H[n>>2];g=H[f+88>>2];b=g+1|0;H[f+88>>2]=b;g=k&-2147483648>>>g;_:{if((b|0)==32){H[f+88>>2]=0;H[f+84>>2]=n+4;if(g){break _}break X}if(!g){break X}}b=d}d=e;break W}while(1){j=(g-1|0)!=(j|0)?j+1|0:0;H[b+(u<<2)>>2]=j;g=H[f+12>>2];u=u+1|0;if(g>>>0>u>>>0){continue}break}break V}b=e}n=H[f+128>>2];k=n+o|0;g=H[k>>2];e=g+q|0;H[e>>2]=H[e>>2]+1;Aa(n+r|0,g,H[k+4>>2]);if(d){g=H[h+28>>2]+H[h+24>>2]|0;e=H[h+16>>2];u=H[h+12>>2];if((g|0)==(((e|0)!=(u|0)?N(e-u>>2,341)-1|0:0)|0)){xa(h+8|0);u=H[h+12>>2];g=H[h+24>>2]+H[h+28>>2]|0}e=(g>>>0)/341|0;e=H[(e<<2)+u>>2]+N(g-N(e,341)|0,12)|0;H[e+8>>2]=p;H[e+4>>2]=j;H[e>>2]=d;H[h+28>>2]=H[h+28>>2]+1}if(!b){break T}g=H[h+28>>2]+H[h+24>>2]|0;d=H[h+16>>2];u=H[h+12>>2];if((g|0)==(((d|0)!=(u|0)?N(d-u>>2,341)-1|0:0)|0)){xa(h+8|0);u=H[h+12>>2];g=H[h+24>>2]+H[h+28>>2]|0}d=(g>>>0)/341|0;d=H[(d<<2)+u>>2]+N(g-N(d,341)|0,12)|0;H[d+8>>2]=s;H[d+4>>2]=j;H[d>>2]=b;H[h+28>>2]=H[h+28>>2]+1;break T}if(!i){break T}while(1){if(H[f+12>>2]){p=H[f+36>>2];n=H[A>>2];u=H[f+92>>2];k=H[f+104>>2];j=0;while(1){o=k+(j<<2)|0;H[u+(H[o>>2]<<2)>>2]=0;g=H[f>>2];e=H[o>>2]<<2;b=H[e+n>>2];$:{if((g|0)==(b|0)){break $}q=e+u|0;l=g-b|0;r=H[f+48>>2];g=32-r|0;if((l|0)<=(g|0)){e=H[f+44>>2];if((e|0)==(p|0)){break S}H[q>>2]=H[e>>2]<>>32-l;b=l+H[f+48>>2]|0;H[f+48>>2]=b;if((b|0)!=32){break $}H[f+48>>2]=0;H[f+44>>2]=e+4;break $}s=H[f+44>>2];b=s+4|0;if((b|0)==(p|0)){break S}e=H[s>>2];H[f+44>>2]=b;b=l-g|0;H[f+48>>2]=b;H[q>>2]=H[s+4>>2]>>>32-b|e<>>32-l}e=H[o>>2]<<2;b=e+u|0;H[b>>2]=H[b>>2]|H[e+H[t>>2]>>2];j=j+1|0;if(j>>>0>2]){continue}break}}jb(w,x);H[f+8>>2]=H[f+8>>2]+1;d=d+1|0;if((i|0)!=(d|0)){continue}break}}j=H[h+28>>2];if(j){continue}break}}H[h+28>>2]=0;u=H[h+16>>2];j=H[h+12>>2];g=u-j|0;if(g>>>0>=9){while(1){oa(H[j>>2]);j=H[h+12>>2]+4|0;H[h+12>>2]=j;u=H[h+16>>2];g=u-j|0;if(g>>>0>8){continue}break}}b=170;aa:{switch((g>>>2|0)-1|0){case 1:b=341;case 0:H[h+24>>2]=b;break;default:break aa}}ba:{if((j|0)==(u|0)){break ba}while(1){oa(H[j>>2]);j=j+4|0;if((u|0)!=(j|0)){continue}break}d=H[h+16>>2];b=H[h+12>>2];if((d|0)==(b|0)){break ba}H[h+16>>2]=d+((b-d|0)+3&-4)}b=H[h+8>>2];if(b){oa(b)}ca=h+32|0;break R}}vb(f);break d;case 3:i=ub(B+8|0,3);z=B+664|0;k=H[b+8>>2];n=H[b+12>>2];d=H[b+20>>2];e=H[b+16>>2];g=e+4|0;d=g>>>0<4?d+1|0:d;ca:{if(g>>>0>k>>>0&(d|0)>=(n|0)|(d|0)>(n|0)){break ca}d=e+H[b>>2]|0;H[i>>2]=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);d=H[b+20>>2];k=d;g=H[b+16>>2];e=g+4|0;d=e>>>0<4?d+1|0:d;H[b+16>>2]=e;H[b+20>>2]=d;if(K[i>>2]>32){break ca}n=H[b+8>>2];s=H[b+12>>2];d=k;g=g+8|0;d=g>>>0<8?d+1|0:d;if(g>>>0>n>>>0&(d|0)>=(s|0)|(d|0)>(s|0)){break ca}d=e+H[b>>2]|0;e=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[i+4>>2]=e;g=H[b+20>>2];d=H[b+16>>2]+4|0;g=d>>>0<4?g+1|0:g;H[b+16>>2]=d;H[b+20>>2]=g;if(!e){break ca}H[i+8>>2]=0;if(!ta(i+16|0,b)){break ca}if(!ua(i+32|0,b)){break ca}if(!ua(i+52|0,b)){break ca}if(!ua(i+72|0,b)){break ca}A=H[i+4>>2];d=0;f=ca-32|0;ca=f;j=H[i+12>>2];H[f+16>>2]=0;H[f+8>>2]=0;H[f+12>>2]=0;if(j){if(j>>>0>=1073741824){break b}b=j<<2;m=pa(b);H[f+8>>2]=m;d=b+m|0;H[f+16>>2]=d;ra(m,0,b);H[f+12>>2]=d}e=H[i+116>>2];b=H[e>>2];if(b){H[e+4>>2]=b;oa(b);j=H[i+12>>2];m=H[f+8>>2];d=H[f+12>>2]}H[e+4>>2]=d;H[e>>2]=m;H[e+8>>2]=H[f+16>>2];m=0;H[f+16>>2]=0;H[f+8>>2]=0;H[f+12>>2]=0;da:{if(j){if(j>>>0>=1073741824){break b}b=j<<2;p=pa(b);H[f+8>>2]=p;m=b+p|0;H[f+16>>2]=m;ra(p,0,b);H[f+12>>2]=m}d=H[i+128>>2];b=H[d>>2];if(b){H[d+4>>2]=b;oa(b);m=H[f+12>>2];p=H[f+8>>2]}H[d+4>>2]=m;H[d>>2]=p;H[d+8>>2]=H[f+16>>2];H[f+24>>2]=0;H[f+28>>2]=0;H[f+16>>2]=0;H[f+20>>2]=0;H[f+8>>2]=0;H[f+12>>2]=0;xa(f+8|0);d=H[f+24>>2]+H[f+28>>2]|0;b=(d>>>0)/341|0;b=H[H[f+12>>2]+(b<<2)>>2]+N(d-N(b,341)|0,12)|0;H[b+4>>2]=0;H[b+8>>2]=0;H[b>>2]=A;j=H[f+28>>2]+1|0;H[f+28>>2]=j;ea:{if(!j){break ea}y=i+92|0;s=i+16|0;while(1){k=H[f+12>>2];g=H[f+24>>2];e=j-1|0;d=g+e|0;b=(d>>>0)/341|0;b=H[k+(b<<2)>>2]+N(d-N(b,341)|0,12)|0;o=H[b+8>>2];d=H[b+4>>2];t=H[b>>2];H[f+28>>2]=e;b=H[f+16>>2];if((((b|0)!=(k|0)?N(b-k>>2,341)-1|0:0)-(g+j|0)|0)+1>>>0>=682){oa(H[b-4>>2]);H[f+16>>2]=H[f+16>>2]-4}if(t>>>0>A>>>0){break ea}b=H[i+12>>2];j=(d|0)!=(b-1|0)?d+1|0:0;if(j>>>0>=b>>>0){break ea}b=H[i+116>>2];q=N(o,12);l=b+q|0;e=H[i>>2];r=j<<2;n=q+H[i+128>>2]|0;d=H[r+H[n>>2]>>2];fa:{if((e|0)==(d|0)){r=0;if(!t){break fa}while(1){b=H[l>>2];x=H[b+8>>2];n=H[b+4>>2];k=H[b>>2];o=H[z>>2];j=H[o+4>>2];b=H[o+8>>2];ga:{if(j>>>0>>0){H[j+8>>2]=x;H[j+4>>2]=n;H[j>>2]=k;H[o+4>>2]=j+12;break ga}q=H[o>>2];e=(j-q|0)/12|0;g=e+1|0;if(g>>>0>=357913942){break b}d=(b-q|0)/12|0;b=d<<1;g=d>>>0>=178956970?357913941:b>>>0>g>>>0?b:g;if(g){if(g>>>0>=357913942){break a}b=pa(N(g,12))}else{b=0}p=b+N(e,12)|0;H[p+8>>2]=x;H[p+4>>2]=n;H[p>>2]=k;d=p+12|0;if((j|0)!=(q|0)){while(1){p=p-12|0;j=j-12|0;H[p>>2]=H[j>>2];H[p+4>>2]=H[j+4>>2];H[p+8>>2]=H[j+8>>2];if((j|0)!=(q|0)){continue}break}}H[o+8>>2]=b+N(g,12);H[o+4>>2]=d;H[o>>2]=p;if(!q){break ga}oa(q)}H[i+8>>2]=H[i+8>>2]+1;r=r+1|0;if((t|0)!=(r|0)){continue}break}break fa}ha:{ia:{ja:{ka:{if(t>>>0<=2){b=H[i+104>>2];H[b>>2]=j;p=1;m=H[i+12>>2];if(m>>>0>1){break ka}break ha}if(K[i+8>>2]>K[i+4>>2]){break ea}k=b;b=q+12|0;Aa(k+b|0,H[l>>2],H[l+4>>2]);b=r+H[b+H[i+116>>2]>>2]|0;H[b>>2]=H[b>>2]+(1<>2]=0;pc(s,Q(t)^31,f+4|0);d=t>>>1|0;b=H[f+4>>2];if(d>>>0>>0){break ea}x=o+1|0;e=d-b|0;d=t-e|0;la:{if((d|0)==(e|0)){b=e;break la}n=H[i+84>>2];if((n|0)==H[i+76>>2]){break ja}k=H[n>>2];g=H[i+88>>2];b=g+1|0;H[i+88>>2]=b;g=k&-2147483648>>>g;ma:{if((b|0)==32){H[i+88>>2]=0;H[i+84>>2]=n+4;if(g){break ma}break ja}if(!g){break ja}}b=d}d=e;break ia}while(1){j=(m-1|0)!=(j|0)?j+1|0:0;H[b+(p<<2)>>2]=j;m=H[i+12>>2];p=p+1|0;if(m>>>0>p>>>0){continue}break}break ha}b=e}n=H[i+128>>2];k=n+q|0;g=H[k>>2];e=g+r|0;H[e>>2]=H[e>>2]+1;Aa(n+N(x,12)|0,g,H[k+4>>2]);if(d){m=H[f+28>>2]+H[f+24>>2]|0;e=H[f+16>>2];p=H[f+12>>2];if((m|0)==(((e|0)!=(p|0)?N(e-p>>2,341)-1|0:0)|0)){xa(f+8|0);m=H[f+24>>2]+H[f+28>>2]|0;p=H[f+12>>2]}e=(m>>>0)/341|0;e=H[p+(e<<2)>>2]+N(m-N(e,341)|0,12)|0;H[e+8>>2]=o;H[e+4>>2]=j;H[e>>2]=d;H[f+28>>2]=H[f+28>>2]+1}if(!b){break fa}m=H[f+28>>2]+H[f+24>>2]|0;d=H[f+16>>2];p=H[f+12>>2];if((m|0)==(((d|0)!=(p|0)?N(d-p>>2,341)-1|0:0)|0)){xa(f+8|0);m=H[f+24>>2]+H[f+28>>2]|0;p=H[f+12>>2]}d=(m>>>0)/341|0;d=H[p+(d<<2)>>2]+N(m-N(d,341)|0,12)|0;H[d+8>>2]=x;H[d+4>>2]=j;H[d>>2]=b;H[f+28>>2]=H[f+28>>2]+1;break fa}m=0;if(!t){break fa}while(1){if(H[i+12>>2]){p=H[i+36>>2];k=H[n>>2];w=H[i+92>>2];g=H[i+104>>2];j=0;while(1){o=g+(j<<2)|0;H[w+(H[o>>2]<<2)>>2]=0;e=H[i>>2];d=H[o>>2]<<2;b=H[d+k>>2];na:{if((e|0)==(b|0)){break na}q=d+w|0;u=e-b|0;r=H[i+48>>2];e=32-r|0;if((u|0)<=(e|0)){d=H[i+44>>2];if((d|0)==(p|0)){break ea}H[q>>2]=H[d>>2]<>>32-u;b=u+H[i+48>>2]|0;H[i+48>>2]=b;if((b|0)!=32){break na}H[i+48>>2]=0;H[i+44>>2]=d+4;break na}x=H[i+44>>2];b=x+4|0;if((b|0)==(p|0)){break ea}d=H[x>>2];H[i+44>>2]=b;b=u-e|0;H[i+48>>2]=b;H[q>>2]=H[x+4>>2]>>>32-b|d<>>32-u}d=H[o>>2]<<2;b=d+w|0;H[b>>2]=H[b>>2]|H[d+H[l>>2]>>2];j=j+1|0;if(j>>>0>2]){continue}break}}jb(z,y);H[i+8>>2]=H[i+8>>2]+1;m=m+1|0;if((t|0)!=(m|0)){continue}break}}j=H[f+28>>2];if(j){continue}break}}H[f+28>>2]=0;p=H[f+16>>2];j=H[f+12>>2];m=p-j|0;if(m>>>0>=9){while(1){oa(H[j>>2]);j=H[f+12>>2]+4|0;H[f+12>>2]=j;p=H[f+16>>2];m=p-j|0;if(m>>>0>8){continue}break}}b=170;oa:{switch((m>>>2|0)-1|0){case 1:b=341;case 0:H[f+24>>2]=b;break;default:break oa}}pa:{if((j|0)==(p|0)){break pa}while(1){oa(H[j>>2]);j=j+4|0;if((p|0)!=(j|0)){continue}break}d=H[f+16>>2];b=H[f+12>>2];if((d|0)==(b|0)){break pa}H[f+16>>2]=d+((b-d|0)+3&-4)}b=H[f+8>>2];if(b){oa(b)}ca=f+32|0;break da}}vb(i);break d;case 4:f=$a(B+8|0,3);w=B+664|0;k=H[b+8>>2];n=H[b+12>>2];d=H[b+20>>2];e=H[b+16>>2];g=e+4|0;d=g>>>0<4?d+1|0:d;qa:{if(g>>>0>k>>>0&(d|0)>=(n|0)|(d|0)>(n|0)){break qa}d=e+H[b>>2]|0;H[f>>2]=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);d=H[b+20>>2];k=d;g=H[b+16>>2];e=g+4|0;d=e>>>0<4?d+1|0:d;H[b+16>>2]=e;H[b+20>>2]=d;if(K[f>>2]>32){break qa}n=H[b+8>>2];s=H[b+12>>2];d=k;g=g+8|0;d=g>>>0<8?d+1|0:d;if(g>>>0>n>>>0&(d|0)>=(s|0)|(d|0)>(s|0)){break qa}d=e+H[b>>2]|0;e=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[f+4>>2]=e;g=H[b+20>>2];d=H[b+16>>2]+4|0;g=d>>>0<4?g+1|0:g;H[b+16>>2]=d;H[b+20>>2]=g;if(!e){break qa}H[f+8>>2]=0;if(!sb(f+16|0,b)){break qa}if(!ua(f+544|0,b)){break qa}if(!ua(f+564|0,b)){break qa}if(!ua(f+584|0,b)){break qa}z=H[f+4>>2];l=0;b=0;h=ca-32|0;ca=h;d=H[f+12>>2];H[h+16>>2]=0;H[h+8>>2]=0;H[h+12>>2]=0;if(d){if(d>>>0>=1073741824){break b}e=d<<2;l=pa(e);H[h+8>>2]=l;b=e+l|0;H[h+16>>2]=b;ra(l,0,e);H[h+12>>2]=b}g=H[f+628>>2];e=H[g>>2];if(e){H[g+4>>2]=e;oa(e);d=H[f+12>>2];l=H[h+8>>2];b=H[h+12>>2]}H[g+4>>2]=b;H[g>>2]=l;H[g+8>>2]=H[h+16>>2];l=0;H[h+16>>2]=0;H[h+8>>2]=0;H[h+12>>2]=0;ra:{if(d){if(d>>>0>=1073741824){break b}b=d<<2;j=pa(b);H[h+8>>2]=j;l=b+j|0;H[h+16>>2]=l;ra(j,0,b);H[h+12>>2]=l}d=H[f+640>>2];b=H[d>>2];if(b){H[d+4>>2]=b;oa(b);j=H[h+8>>2];l=H[h+12>>2]}H[d+4>>2]=l;H[d>>2]=j;H[d+8>>2]=H[h+16>>2];H[h+24>>2]=0;H[h+28>>2]=0;H[h+16>>2]=0;H[h+20>>2]=0;H[h+8>>2]=0;H[h+12>>2]=0;xa(h+8|0);d=H[h+24>>2]+H[h+28>>2]|0;b=(d>>>0)/341|0;b=H[H[h+12>>2]+(b<<2)>>2]+N(d-N(b,341)|0,12)|0;H[b+4>>2]=0;H[b+8>>2]=0;H[b>>2]=z;d=H[h+28>>2]+1|0;H[h+28>>2]=d;sa:{if(!d){break sa}x=f+604|0;y=f+16|0;while(1){n=H[h+12>>2];k=H[h+24>>2];g=d-1|0;e=k+g|0;b=(e>>>0)/341|0;b=H[n+(b<<2)>>2]+N(e-N(b,341)|0,12)|0;p=H[b+8>>2];e=H[b+4>>2];i=H[b>>2];H[h+28>>2]=g;b=H[h+16>>2];if((((b|0)!=(n|0)?N(b-n>>2,341)-1|0:0)-(d+k|0)|0)+1>>>0>=682){oa(H[b-4>>2]);H[h+16>>2]=H[h+16>>2]-4}if(i>>>0>z>>>0){break sa}b=H[f+12>>2];j=(e|0)!=(b-1|0)?e+1|0:0;if(j>>>0>=b>>>0){break sa}o=N(p,12);A=o+H[f+640>>2]|0;t=o+H[f+628>>2]|0;g=H[f>>2];q=j<<2;e=H[q+H[A>>2]>>2];ta:{ua:{if((g|0)==(e|0)){o=0;if(!i){break ua}while(1){b=H[t>>2];r=H[b+8>>2];s=H[b+4>>2];n=H[b>>2];p=H[w>>2];d=H[p+4>>2];b=H[p+8>>2];va:{if(d>>>0>>0){H[d+8>>2]=r;H[d+4>>2]=s;H[d>>2]=n;H[p+4>>2]=d+12;break va}q=H[p>>2];g=(d-q|0)/12|0;k=g+1|0;if(k>>>0>=357913942){break b}e=(b-q|0)/12|0;b=e<<1;k=e>>>0>=178956970?357913941:b>>>0>k>>>0?b:k;if(k){if(k>>>0>=357913942){break a}b=pa(N(k,12))}else{b=0}j=b+N(g,12)|0;H[j+8>>2]=r;H[j+4>>2]=s;H[j>>2]=n;e=j+12|0;if((d|0)!=(q|0)){while(1){j=j-12|0;d=d-12|0;H[j>>2]=H[d>>2];H[j+4>>2]=H[d+4>>2];H[j+8>>2]=H[d+8>>2];if((d|0)!=(q|0)){continue}break}}H[p+8>>2]=b+N(k,12);H[p+4>>2]=e;H[p>>2]=j;if(!q){break va}oa(q)}H[f+8>>2]=H[f+8>>2]+1;o=o+1|0;if((i|0)!=(o|0)){continue}break}break ua}wa:{xa:{ya:{if(i>>>0<=2){b=H[f+616>>2];H[b>>2]=j;d=1;l=H[f+12>>2];if(l>>>0>1){break ya}break wa}if(K[f+8>>2]>K[f+4>>2]){break sa}b=H[f+628>>2];s=p+1|0;r=N(s,12);d=b+r|0;if((d|0)!=(t|0)){Aa(d,H[t>>2],H[t+4>>2]);b=H[f+628>>2]}b=q+H[b+r>>2]|0;H[b>>2]=H[b>>2]+(1<>>1|0;break xa}while(1){l=Ba(y+(d<<4)|0)|l<<1;d=d+1|0;if((b|0)!=(d|0)){continue}break}d=i>>>1|0;if(l>>>0<=d>>>0){break xa}break sa}while(1){j=(l-1|0)!=(j|0)?j+1|0:0;H[b+(d<<2)>>2]=j;d=d+1|0;l=H[f+12>>2];if(d>>>0>>0){continue}break}break wa}za:{Aa:{e=d-l|0;d=i-e|0;Ba:{if((d|0)==(e|0)){b=e;break Ba}n=H[f+596>>2];if((n|0)==H[f+588>>2]){break Aa}k=H[n>>2];g=H[f+600>>2];b=g+1|0;H[f+600>>2]=b;g=k&-2147483648>>>g;Ca:{if((b|0)==32){H[f+600>>2]=0;H[f+596>>2]=n+4;if(g){break Ca}break Aa}if(!g){break Aa}}b=d}d=e;break za}b=e}n=H[f+640>>2];k=n+o|0;g=H[k>>2];e=g+q|0;H[e>>2]=H[e>>2]+1;Aa(n+r|0,g,H[k+4>>2]);if(d){g=H[h+28>>2]+H[h+24>>2]|0;e=H[h+16>>2];l=H[h+12>>2];if((g|0)==(((e|0)!=(l|0)?N(e-l>>2,341)-1|0:0)|0)){xa(h+8|0);l=H[h+12>>2];g=H[h+24>>2]+H[h+28>>2]|0}e=(g>>>0)/341|0;e=H[(e<<2)+l>>2]+N(g-N(e,341)|0,12)|0;H[e+8>>2]=p;H[e+4>>2]=j;H[e>>2]=d;H[h+28>>2]=H[h+28>>2]+1}if(!b){break ua}l=H[h+28>>2]+H[h+24>>2]|0;e=H[h+16>>2];d=H[h+12>>2];if((l|0)==(((d|0)!=(e|0)?N(e-d>>2,341)-1|0:0)|0)){xa(h+8|0);l=H[h+24>>2]+H[h+28>>2]|0;e=H[h+12>>2]}else{e=d}d=(l>>>0)/341|0;d=H[e+(d<<2)>>2]+N(l-N(d,341)|0,12)|0;H[d+8>>2]=s;H[d+4>>2]=j;H[d>>2]=b;d=H[h+28>>2]+1|0;H[h+28>>2]=d;break ta}j=0;if(!i){break ua}while(1){if(H[f+12>>2]){p=H[f+548>>2];n=H[A>>2];u=H[f+604>>2];k=H[f+616>>2];d=0;while(1){o=k+(d<<2)|0;H[u+(H[o>>2]<<2)>>2]=0;g=H[f>>2];e=H[o>>2]<<2;b=H[e+n>>2];Da:{if((g|0)==(b|0)){break Da}q=e+u|0;l=g-b|0;r=H[f+560>>2];g=32-r|0;if((l|0)<=(g|0)){e=H[f+556>>2];if((e|0)==(p|0)){break sa}H[q>>2]=H[e>>2]<>>32-l;b=l+H[f+560>>2]|0;H[f+560>>2]=b;if((b|0)!=32){break Da}H[f+560>>2]=0;H[f+556>>2]=e+4;break Da}s=H[f+556>>2];b=s+4|0;if((b|0)==(p|0)){break sa}e=H[s>>2];H[f+556>>2]=b;b=l-g|0;H[f+560>>2]=b;H[q>>2]=H[s+4>>2]>>>32-b|e<>>32-l}e=H[o>>2]<<2;b=e+u|0;H[b>>2]=H[b>>2]|H[e+H[t>>2]>>2];d=d+1|0;if(d>>>0>2]){continue}break}}jb(w,x);H[f+8>>2]=H[f+8>>2]+1;j=j+1|0;if((i|0)!=(j|0)){continue}break}}d=H[h+28>>2]}if(d){continue}break}}H[h+28>>2]=0;j=H[h+16>>2];d=H[h+12>>2];l=j-d|0;if(l>>>0>=9){while(1){oa(H[d>>2]);d=H[h+12>>2]+4|0;H[h+12>>2]=d;j=H[h+16>>2];l=j-d|0;if(l>>>0>8){continue}break}}b=170;Ea:{switch((l>>>2|0)-1|0){case 1:b=341;case 0:H[h+24>>2]=b;break;default:break Ea}}Fa:{if((d|0)==(j|0)){break Fa}while(1){oa(H[d>>2]);d=d+4|0;if((j|0)!=(d|0)){continue}break}d=H[h+16>>2];b=H[h+12>>2];if((d|0)==(b|0)){break Fa}H[h+16>>2]=d+((b-d|0)+3&-4)}b=H[h+8>>2];if(b){oa(b)}ca=h+32|0;break ra}}ab(f);break d;case 5:f=$a(B+8|0,3);w=B+664|0;k=H[b+8>>2];n=H[b+12>>2];d=H[b+20>>2];e=H[b+16>>2];g=e+4|0;d=g>>>0<4?d+1|0:d;Ga:{if(g>>>0>k>>>0&(d|0)>=(n|0)|(d|0)>(n|0)){break Ga}d=e+H[b>>2]|0;H[f>>2]=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);d=H[b+20>>2];k=d;g=H[b+16>>2];e=g+4|0;d=e>>>0<4?d+1|0:d;H[b+16>>2]=e;H[b+20>>2]=d;if(K[f>>2]>32){break Ga}n=H[b+8>>2];s=H[b+12>>2];d=k;g=g+8|0;d=g>>>0<8?d+1|0:d;if(g>>>0>n>>>0&(d|0)>=(s|0)|(d|0)>(s|0)){break Ga}d=e+H[b>>2]|0;e=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[f+4>>2]=e;g=H[b+20>>2];d=H[b+16>>2]+4|0;g=d>>>0<4?g+1|0:g;H[b+16>>2]=d;H[b+20>>2]=g;if(!e){break Ga}H[f+8>>2]=0;if(!sb(f+16|0,b)){break Ga}if(!ua(f+544|0,b)){break Ga}if(!ua(f+564|0,b)){break Ga}if(!ua(f+584|0,b)){break Ga}z=H[f+4>>2];l=0;b=0;h=ca-32|0;ca=h;d=H[f+12>>2];H[h+16>>2]=0;H[h+8>>2]=0;H[h+12>>2]=0;if(d){if(d>>>0>=1073741824){break b}e=d<<2;l=pa(e);H[h+8>>2]=l;b=e+l|0;H[h+16>>2]=b;ra(l,0,e);H[h+12>>2]=b}g=H[f+628>>2];e=H[g>>2];if(e){H[g+4>>2]=e;oa(e);d=H[f+12>>2];l=H[h+8>>2];b=H[h+12>>2]}H[g+4>>2]=b;H[g>>2]=l;H[g+8>>2]=H[h+16>>2];l=0;H[h+16>>2]=0;H[h+8>>2]=0;H[h+12>>2]=0;Ha:{if(d){if(d>>>0>=1073741824){break b}b=d<<2;p=pa(b);H[h+8>>2]=p;l=b+p|0;H[h+16>>2]=l;ra(p,0,b);H[h+12>>2]=l}d=H[f+640>>2];b=H[d>>2];if(b){H[d+4>>2]=b;oa(b);l=H[h+12>>2];p=H[h+8>>2]}H[d+4>>2]=l;H[d>>2]=p;H[d+8>>2]=H[h+16>>2];H[h+24>>2]=0;H[h+28>>2]=0;H[h+16>>2]=0;H[h+20>>2]=0;H[h+8>>2]=0;H[h+12>>2]=0;xa(h+8|0);d=H[h+24>>2]+H[h+28>>2]|0;b=(d>>>0)/341|0;b=H[H[h+12>>2]+(b<<2)>>2]+N(d-N(b,341)|0,12)|0;H[b+4>>2]=0;H[b+8>>2]=0;H[b>>2]=z;d=H[h+28>>2]+1|0;H[h+28>>2]=d;Ia:{if(!d){break Ia}x=f+604|0;y=f+16|0;while(1){n=H[h+12>>2];k=H[h+24>>2];g=d-1|0;e=k+g|0;b=(e>>>0)/341|0;b=H[n+(b<<2)>>2]+N(e-N(b,341)|0,12)|0;o=H[b+8>>2];e=H[b+4>>2];i=H[b>>2];H[h+28>>2]=g;b=H[h+16>>2];if((((b|0)!=(n|0)?N(b-n>>2,341)-1|0:0)-(d+k|0)|0)+1>>>0>=682){oa(H[b-4>>2]);H[h+16>>2]=H[h+16>>2]-4}if(i>>>0>z>>>0){break Ia}m=0;b=H[f+12>>2];p=(e|0)!=(b-1|0)?e+1|0:0;if(p>>>0>=b>>>0){break Ia}b=H[f+628>>2];q=N(o,12);t=b+q|0;e=H[f>>2];r=p<<2;s=q+H[f+640>>2]|0;d=H[r+H[s>>2]>>2];Ja:{Ka:{if((e|0)==(d|0)){if(!i){break Ka}while(1){b=H[t>>2];r=H[b+8>>2];s=H[b+4>>2];n=H[b>>2];o=H[w>>2];d=H[o+4>>2];b=H[o+8>>2];La:{if(d>>>0>>0){H[d+8>>2]=r;H[d+4>>2]=s;H[d>>2]=n;H[o+4>>2]=d+12;break La}q=H[o>>2];g=(d-q|0)/12|0;k=g+1|0;if(k>>>0>=357913942){break b}e=(b-q|0)/12|0;b=e<<1;k=e>>>0>=178956970?357913941:b>>>0>k>>>0?b:k;if(k){if(k>>>0>=357913942){break a}b=pa(N(k,12))}else{b=0}p=b+N(g,12)|0;H[p+8>>2]=r;H[p+4>>2]=s;H[p>>2]=n;e=p+12|0;if((d|0)!=(q|0)){while(1){p=p-12|0;d=d-12|0;H[p>>2]=H[d>>2];H[p+4>>2]=H[d+4>>2];H[p+8>>2]=H[d+8>>2];if((d|0)!=(q|0)){continue}break}}H[o+8>>2]=b+N(k,12);H[o+4>>2]=e;H[o>>2]=p;if(!q){break La}oa(q)}H[f+8>>2]=H[f+8>>2]+1;m=m+1|0;if((i|0)!=(m|0)){continue}break}break Ka}Ma:{Na:{Oa:{if(i>>>0<=2){b=H[f+616>>2];H[b>>2]=p;d=1;l=H[f+12>>2];if(l>>>0>1){break Oa}break Ma}if(K[f+8>>2]>K[f+4>>2]){break Ia}k=b;b=q+12|0;Aa(k+b|0,H[t>>2],H[t+4>>2]);b=r+H[b+H[f+628>>2]>>2]|0;H[b>>2]=H[b>>2]+(1<>>1|0;break Na}while(1){l=Ba(y+(d<<4)|0)|l<<1;d=d+1|0;if((b|0)!=(d|0)){continue}break}d=i>>>1|0;if(l>>>0<=d>>>0){break Na}break Ia}while(1){p=(l-1|0)!=(p|0)?p+1|0:0;H[b+(d<<2)>>2]=p;d=d+1|0;l=H[f+12>>2];if(d>>>0>>0){continue}break}break Ma}s=o+1|0;Pa:{Qa:{e=d-l|0;d=i-e|0;Ra:{if((d|0)==(e|0)){b=e;break Ra}n=H[f+596>>2];if((n|0)==H[f+588>>2]){break Qa}k=H[n>>2];g=H[f+600>>2];b=g+1|0;H[f+600>>2]=b;g=k&-2147483648>>>g;Sa:{if((b|0)==32){H[f+600>>2]=0;H[f+596>>2]=n+4;if(g){break Sa}break Qa}if(!g){break Qa}}b=d}d=e;break Pa}b=e}n=H[f+640>>2];k=n+q|0;g=H[k>>2];e=g+r|0;H[e>>2]=H[e>>2]+1;Aa(n+N(s,12)|0,g,H[k+4>>2]);if(d){m=H[h+28>>2]+H[h+24>>2]|0;e=H[h+16>>2];l=H[h+12>>2];if((m|0)==(((e|0)!=(l|0)?N(e-l>>2,341)-1|0:0)|0)){xa(h+8|0);m=H[h+24>>2]+H[h+28>>2]|0;l=H[h+12>>2]}e=(m>>>0)/341|0;e=H[l+(e<<2)>>2]+N(m-N(e,341)|0,12)|0;H[e+8>>2]=o;H[e+4>>2]=p;H[e>>2]=d;H[h+28>>2]=H[h+28>>2]+1}if(!b){break Ka}l=H[h+28>>2]+H[h+24>>2]|0;e=H[h+16>>2];d=H[h+12>>2];if((l|0)==(((d|0)!=(e|0)?N(e-d>>2,341)-1|0:0)|0)){xa(h+8|0);l=H[h+24>>2]+H[h+28>>2]|0;e=H[h+12>>2]}else{e=d}d=(l>>>0)/341|0;d=H[e+(d<<2)>>2]+N(l-N(d,341)|0,12)|0;H[d+8>>2]=s;H[d+4>>2]=p;H[d>>2]=b;d=H[h+28>>2]+1|0;H[h+28>>2]=d;break Ja}if(!i){break Ka}while(1){if(H[f+12>>2]){A=H[f+548>>2];n=H[s>>2];u=H[f+604>>2];k=H[f+616>>2];d=0;while(1){p=k+(d<<2)|0;H[u+(H[p>>2]<<2)>>2]=0;g=H[f>>2];e=H[p>>2]<<2;b=H[e+n>>2];Ta:{if((g|0)==(b|0)){break Ta}o=e+u|0;l=g-b|0;q=H[f+560>>2];g=32-q|0;if((l|0)<=(g|0)){e=H[f+556>>2];if((e|0)==(A|0)){break Ia}H[o>>2]=H[e>>2]<>>32-l;b=l+H[f+560>>2]|0;H[f+560>>2]=b;if((b|0)!=32){break Ta}H[f+560>>2]=0;H[f+556>>2]=e+4;break Ta}r=H[f+556>>2];b=r+4|0;if((b|0)==(A|0)){break Ia}e=H[r>>2];H[f+556>>2]=b;b=l-g|0;H[f+560>>2]=b;H[o>>2]=H[r+4>>2]>>>32-b|e<>>32-l}e=H[p>>2]<<2;b=e+u|0;H[b>>2]=H[b>>2]|H[e+H[t>>2]>>2];d=d+1|0;if(d>>>0>2]){continue}break}}jb(w,x);H[f+8>>2]=H[f+8>>2]+1;m=m+1|0;if((i|0)!=(m|0)){continue}break}}d=H[h+28>>2]}if(d){continue}break}}H[h+28>>2]=0;p=H[h+16>>2];d=H[h+12>>2];l=p-d|0;if(l>>>0>=9){while(1){oa(H[d>>2]);d=H[h+12>>2]+4|0;H[h+12>>2]=d;p=H[h+16>>2];l=p-d|0;if(l>>>0>8){continue}break}}b=170;Ua:{switch((l>>>2|0)-1|0){case 1:b=341;case 0:H[h+24>>2]=b;break;default:break Ua}}Va:{if((d|0)==(p|0)){break Va}while(1){oa(H[d>>2]);d=d+4|0;if((p|0)!=(d|0)){continue}break}d=H[h+16>>2];b=H[h+12>>2];if((d|0)==(b|0)){break Va}H[h+16>>2]=d+((b-d|0)+3&-4)}b=H[h+8>>2];if(b){oa(b)}ca=h+32|0;break Ha}}ab(f);break d;case 6:break f;default:break c}}f=$a(B+8|0,3);w=B+664|0;k=H[b+8>>2];n=H[b+12>>2];d=H[b+20>>2];e=H[b+16>>2];g=e+4|0;d=g>>>0<4?d+1|0:d;Wa:{if(g>>>0>k>>>0&(d|0)>=(n|0)|(d|0)>(n|0)){break Wa}d=e+H[b>>2]|0;H[f>>2]=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);d=H[b+20>>2];k=d;g=H[b+16>>2];e=g+4|0;d=e>>>0<4?d+1|0:d;H[b+16>>2]=e;H[b+20>>2]=d;if(K[f>>2]>32){break Wa}n=H[b+8>>2];s=H[b+12>>2];d=k;g=g+8|0;d=g>>>0<8?d+1|0:d;if(g>>>0>n>>>0&(d|0)>=(s|0)|(d|0)>(s|0)){break Wa}d=e+H[b>>2]|0;e=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[f+4>>2]=e;g=H[b+20>>2];d=H[b+16>>2]+4|0;g=d>>>0<4?g+1|0:g;H[b+16>>2]=d;H[b+20>>2]=g;if(!e){break Wa}H[f+8>>2]=0;if(!sb(f+16|0,b)){break Wa}if(!ua(f+544|0,b)){break Wa}if(!ua(f+564|0,b)){break Wa}if(!ua(f+584|0,b)){break Wa}z=H[f+4>>2];l=0;b=0;h=ca-32|0;ca=h;d=H[f+12>>2];H[h+16>>2]=0;H[h+8>>2]=0;H[h+12>>2]=0;if(d){if(d>>>0>=1073741824){break b}e=d<<2;l=pa(e);H[h+8>>2]=l;b=e+l|0;H[h+16>>2]=b;ra(l,0,e);H[h+12>>2]=b}g=H[f+628>>2];e=H[g>>2];if(e){H[g+4>>2]=e;oa(e);d=H[f+12>>2];l=H[h+8>>2];b=H[h+12>>2]}H[g+4>>2]=b;H[g>>2]=l;H[g+8>>2]=H[h+16>>2];l=0;H[h+16>>2]=0;H[h+8>>2]=0;H[h+12>>2]=0;Xa:{if(d){if(d>>>0>=1073741824){break b}b=d<<2;j=pa(b);H[h+8>>2]=j;l=b+j|0;H[h+16>>2]=l;ra(j,0,b);H[h+12>>2]=l}d=H[f+640>>2];b=H[d>>2];if(b){H[d+4>>2]=b;oa(b);j=H[h+8>>2];l=H[h+12>>2]}H[d+4>>2]=l;H[d>>2]=j;H[d+8>>2]=H[h+16>>2];H[h+24>>2]=0;H[h+28>>2]=0;H[h+16>>2]=0;H[h+20>>2]=0;H[h+8>>2]=0;H[h+12>>2]=0;xa(h+8|0);d=H[h+24>>2]+H[h+28>>2]|0;b=(d>>>0)/341|0;b=H[H[h+12>>2]+(b<<2)>>2]+N(d-N(b,341)|0,12)|0;H[b+4>>2]=0;H[b+8>>2]=0;H[b>>2]=z;d=H[h+28>>2]+1|0;H[h+28>>2]=d;Ya:{if(!d){break Ya}x=f+604|0;y=f+16|0;while(1){n=H[h+12>>2];k=H[h+24>>2];g=d-1|0;e=k+g|0;b=(e>>>0)/341|0;b=H[n+(b<<2)>>2]+N(e-N(b,341)|0,12)|0;p=H[b+8>>2];i=H[b>>2];H[h+28>>2]=g;b=H[h+16>>2];if((((b|0)!=(n|0)?N(b-n>>2,341)-1|0:0)-(d+k|0)|0)+1>>>0>=682){oa(H[b-4>>2]);H[h+16>>2]=H[h+16>>2]-4}if(i>>>0>z>>>0){break Ya}b=H[f+628>>2];o=N(p,12);A=o+H[f+640>>2]|0;j=Vd(f,i,A);if(j>>>0>=K[f+12>>2]){break Ya}t=b+o|0;g=H[f>>2];q=j<<2;e=H[q+H[A>>2]>>2];Za:{_a:{if((g|0)==(e|0)){o=0;if(!i){break _a}while(1){b=H[t>>2];r=H[b+8>>2];s=H[b+4>>2];n=H[b>>2];p=H[w>>2];d=H[p+4>>2];b=H[p+8>>2];$a:{if(d>>>0>>0){H[d+8>>2]=r;H[d+4>>2]=s;H[d>>2]=n;H[p+4>>2]=d+12;break $a}q=H[p>>2];g=(d-q|0)/12|0;k=g+1|0;if(k>>>0>=357913942){break b}e=(b-q|0)/12|0;b=e<<1;k=e>>>0>=178956970?357913941:b>>>0>k>>>0?b:k;if(k){if(k>>>0>=357913942){break a}b=pa(N(k,12))}else{b=0}j=b+N(g,12)|0;H[j+8>>2]=r;H[j+4>>2]=s;H[j>>2]=n;e=j+12|0;if((d|0)!=(q|0)){while(1){j=j-12|0;d=d-12|0;H[j>>2]=H[d>>2];H[j+4>>2]=H[d+4>>2];H[j+8>>2]=H[d+8>>2];if((d|0)!=(q|0)){continue}break}}H[p+8>>2]=b+N(k,12);H[p+4>>2]=e;H[p>>2]=j;if(!q){break $a}oa(q)}H[f+8>>2]=H[f+8>>2]+1;o=o+1|0;if((i|0)!=(o|0)){continue}break}break _a}ab:{bb:{cb:{if(i>>>0<=2){b=H[f+616>>2];H[b>>2]=j;d=1;l=H[f+12>>2];if(l>>>0>1){break cb}break ab}if(K[f+8>>2]>K[f+4>>2]){break Ya}b=H[f+628>>2];s=p+1|0;r=N(s,12);d=b+r|0;if((d|0)!=(t|0)){Aa(d,H[t>>2],H[t+4>>2]);b=H[f+628>>2]}b=q+H[b+r>>2]|0;H[b>>2]=H[b>>2]+(1<>>1|0;break bb}while(1){l=Ba(y+(d<<4)|0)|l<<1;d=d+1|0;if((b|0)!=(d|0)){continue}break}d=i>>>1|0;if(l>>>0<=d>>>0){break bb}break Ya}while(1){j=(l-1|0)!=(j|0)?j+1|0:0;H[b+(d<<2)>>2]=j;d=d+1|0;l=H[f+12>>2];if(d>>>0>>0){continue}break}break ab}db:{eb:{e=d-l|0;d=i-e|0;fb:{if((d|0)==(e|0)){b=e;break fb}n=H[f+596>>2];if((n|0)==H[f+588>>2]){break eb}k=H[n>>2];g=H[f+600>>2];b=g+1|0;H[f+600>>2]=b;g=k&-2147483648>>>g;gb:{if((b|0)==32){H[f+600>>2]=0;H[f+596>>2]=n+4;if(g){break gb}break eb}if(!g){break eb}}b=d}d=e;break db}b=e}n=H[f+640>>2];k=n+o|0;g=H[k>>2];e=g+q|0;H[e>>2]=H[e>>2]+1;Aa(n+r|0,g,H[k+4>>2]);if(d){g=H[h+28>>2]+H[h+24>>2]|0;e=H[h+16>>2];l=H[h+12>>2];if((g|0)==(((e|0)!=(l|0)?N(e-l>>2,341)-1|0:0)|0)){xa(h+8|0);l=H[h+12>>2];g=H[h+24>>2]+H[h+28>>2]|0}e=(g>>>0)/341|0;e=H[(e<<2)+l>>2]+N(g-N(e,341)|0,12)|0;H[e+8>>2]=p;H[e+4>>2]=j;H[e>>2]=d;H[h+28>>2]=H[h+28>>2]+1}if(!b){break _a}l=H[h+28>>2]+H[h+24>>2]|0;e=H[h+16>>2];d=H[h+12>>2];if((l|0)==(((d|0)!=(e|0)?N(e-d>>2,341)-1|0:0)|0)){xa(h+8|0);l=H[h+24>>2]+H[h+28>>2]|0;e=H[h+12>>2]}else{e=d}d=(l>>>0)/341|0;d=H[e+(d<<2)>>2]+N(l-N(d,341)|0,12)|0;H[d+8>>2]=s;H[d+4>>2]=j;H[d>>2]=b;d=H[h+28>>2]+1|0;H[h+28>>2]=d;break Za}j=0;if(!i){break _a}while(1){if(H[f+12>>2]){p=H[f+548>>2];n=H[A>>2];u=H[f+604>>2];k=H[f+616>>2];d=0;while(1){o=k+(d<<2)|0;H[u+(H[o>>2]<<2)>>2]=0;g=H[f>>2];e=H[o>>2]<<2;b=H[e+n>>2];hb:{if((g|0)==(b|0)){break hb}q=e+u|0;l=g-b|0;r=H[f+560>>2];g=32-r|0;if((l|0)<=(g|0)){e=H[f+556>>2];if((e|0)==(p|0)){break Ya}H[q>>2]=H[e>>2]<>>32-l;b=l+H[f+560>>2]|0;H[f+560>>2]=b;if((b|0)!=32){break hb}H[f+560>>2]=0;H[f+556>>2]=e+4;break hb}s=H[f+556>>2];b=s+4|0;if((b|0)==(p|0)){break Ya}e=H[s>>2];H[f+556>>2]=b;b=l-g|0;H[f+560>>2]=b;H[q>>2]=H[s+4>>2]>>>32-b|e<>>32-l}e=H[o>>2]<<2;b=e+u|0;H[b>>2]=H[b>>2]|H[e+H[t>>2]>>2];d=d+1|0;if(d>>>0>2]){continue}break}}jb(w,x);H[f+8>>2]=H[f+8>>2]+1;j=j+1|0;if((i|0)!=(j|0)){continue}break}}d=H[h+28>>2]}if(d){continue}break}}H[h+28>>2]=0;j=H[h+16>>2];d=H[h+12>>2];l=j-d|0;if(l>>>0>=9){while(1){oa(H[d>>2]);d=H[h+12>>2]+4|0;H[h+12>>2]=d;j=H[h+16>>2];l=j-d|0;if(l>>>0>8){continue}break}}b=170;ib:{switch((l>>>2|0)-1|0){case 1:b=341;case 0:H[h+24>>2]=b;break;default:break ib}}jb:{if((d|0)==(j|0)){break jb}while(1){oa(H[d>>2]);d=d+4|0;if((j|0)!=(d|0)){continue}break}d=H[h+16>>2];b=H[h+12>>2];if((d|0)==(b|0)){break jb}H[h+16>>2]=d+((b-d|0)+3&-4)}b=H[h+8>>2];if(b){oa(b)}ca=h+32|0;break Xa}}ab(f)}n=H[a+12>>2]==((H[c+4>>2]-H[c>>2]|0)/12|0)}ca=B+672|0;return n}sa();v()}wa();v()}function kd(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,v=0,w=0,x=0,y=0,z=0;if(!a){return 1}e=H[c+20>>2];g=H[c+12>>2];i=H[c+16>>2];a:{if((e|0)>=(g|0)&i>>>0>=K[c+8>>2]|(e|0)>(g|0)){break a}g=I[i+H[c>>2]|0];i=i+1|0;e=i?e:e+1|0;H[c+16>>2]=i;H[c+20>>2]=e;b:{switch(g|0){case 0:e=a;f=b;i=d;a=0;d=0;m=ca+-64|0;ca=m;H[m+56>>2]=0;H[m+48>>2]=0;H[m+52>>2]=0;H[m+40>>2]=0;H[m+44>>2]=0;H[m+32>>2]=0;H[m+36>>2]=0;H[m+24>>2]=0;H[m+28>>2]=0;H[m+16>>2]=0;H[m+20>>2]=0;H[m+8>>2]=0;H[m+12>>2]=0;c:{if(!Ne(m+8|0,c)){break c}if(!Me(m+8|0,c)|(H[m+20>>2]?0:e)){break c}Db(c,0,0);if(e){s=f<<2;t=H[m+36>>2];w=H[m+48>>2];x=H[m+24>>2];l=H[m+56>>2];j=H[m+52>>2];while(1){d:{if(l>>>0>16383){break d}while(1){if((j|0)<=0){break d}j=j-1|0;H[m+52>>2]=j;l=I[j+w|0]|l<<8;H[m+56>>2]=l;if(l>>>0<16384){continue}break}}a=l&4095;r=H[(a<<2)+x>>2];b=(r<<3)+t|0;l=(N(H[b>>2],l>>>12|0)+a|0)-H[b+4>>2]|0;H[m+56>>2]=l;if((f|0)>0){a=0;if(!I[c+36|0]|r>>>0>32){break c}g=d+f|0;e:{if(!r){ra(i+(d<<2)|0,0,s);break e}y=r&-2;z=r&1;b=H[c+32>>2];h=H[c+28>>2];n=H[c+24>>2];while(1){k=0;a=b;o=0;q=0;if((r|0)!=1){while(1){p=n+(a>>>3|0)|0;f:{if(p>>>0>=h>>>0){p=0;break f}p=I[p|0];b=a+1|0;H[c+32>>2]=b;p=p>>>(a&7)&1;a=b}p=p<>>3|0)|0;if(u>>>0>>0){o=I[u|0];b=a+1|0;H[c+32>>2]=b;o=o>>>(a&7)&1;a=b}u=k|1;k=k+2|0;o=p|o<>>3|0)|0;if(p>>>0>>0){p=I[p|0];b=a+1|0;H[c+32>>2]=b;a=p>>>(a&7)&1}else{a=0}o=a<>2]=o;d=d+1|0;if((g|0)!=(d|0)){continue}break}}d=g}v=f+v|0;if(e>>>0>v>>>0){continue}break}}F[c+36|0]=0;b=H[c+20>>2];e=0;d=H[c+32>>2]+7|0;e=d>>>0<7?1:e;d=(e&7)<<29|d>>>3;a=d+H[c+16>>2]|0;e=(e>>>3|0)+b|0;H[c+16>>2]=a;H[c+20>>2]=a>>>0>>0?e+1|0:e;a=1}b=H[m+36>>2];if(b){H[m+40>>2]=b;oa(b)}b=H[m+24>>2];if(b){H[m+28>>2]=b;oa(b)}b=H[m+8>>2];if(b){H[m+12>>2]=b;oa(b)}ca=m- -64|0;return a;case 1:break b;default:break a}}b=0;e=H[c+20>>2];g=H[c+12>>2];i=H[c+16>>2];g:{if((e|0)>=(g|0)&i>>>0>=K[c+8>>2]|(e|0)>(g|0)){break g}g=I[i+H[c>>2]|0];i=i+1|0;e=i?e:e+1|0;H[c+16>>2]=i;H[c+20>>2]=e;h:{switch(g-1|0){case 8:g=a;r=d;i=ca+-64|0;ca=i;H[i+56>>2]=0;H[i+48>>2]=0;H[i+52>>2]=0;H[i+40>>2]=0;H[i+44>>2]=0;H[i+32>>2]=0;H[i+36>>2]=0;H[i+24>>2]=0;H[i+28>>2]=0;H[i+16>>2]=0;H[i+20>>2]=0;H[i+8>>2]=0;H[i+12>>2]=0;j=i+8|0;a=J[c+38>>1];i:{j:{if(!a){break j}k:{if(a>>>0<=511){d=H[c+8>>2];b=H[c+12>>2];e=H[c+20>>2];a=H[c+16>>2];f=a+4|0;e=f>>>0<4?e+1|0:e;if(d>>>0>>0&(b|0)<=(e|0)|(b|0)<(e|0)){break j}a=a+H[c>>2]|0;h=I[a|0]|I[a+1|0]<<8|(I[a+2|0]<<16|I[a+3|0]<<24);H[j+12>>2]=h;e=H[c+20>>2];f=H[c+16>>2]+4|0;e=f>>>0<4?e+1|0:e;H[c+16>>2]=f;H[c+20>>2]=e;break k}if(!hb(1,j+12|0,c)){break j}f=H[c+16>>2];e=H[c+20>>2];h=H[j+12>>2]}a=H[c+8>>2];d=a-f|0;a=H[c+12>>2]-((a>>>0>>0)+e|0)|0;if(d>>>0>>6>>>0&(a|0)<=0|(a|0)<0){break j}b=H[j>>2];a=H[j+4>>2]-b>>2;l:{if(a>>>0>>0){ya(j,h-a|0);h=H[j+12>>2];break l}if(a>>>0<=h>>>0){break l}H[j+4>>2]=b+(h<<2)}d=1;if(!h){break i}f=H[c+16>>2];e=H[c+20>>2];s=H[j>>2];m=H[c+8>>2];n=H[c+12>>2];b=0;while(1){d=0;if((e|0)>=(n|0)&f>>>0>=m>>>0|(e|0)>(n|0)){break i}d=H[c>>2];p=I[d+f|0];f=f+1|0;e=f?e:e+1|0;H[c+16>>2]=f;H[c+20>>2]=e;a=p>>>2|0;l=0;m:{n:{o:{p:{t=p&3;switch(t|0){case 0:break n;case 3:break p;default:break o}}a=a+b|0;d=0;if(a>>>0>=h>>>0){break i}ra(s+(b<<2)|0,0,(p&252)+4|0);b=a;break m}while(1){if((f|0)==(m|0)&(e|0)==(n|0)){break j}h=I[d+f|0];f=f+1|0;e=f?e:e+1|0;H[c+16>>2]=f;H[c+20>>2]=e;a=h<<(l<<3|6)|a;l=l+1|0;if((t|0)!=(l|0)){continue}break}}H[s+(b<<2)>>2]=a}b=b+1|0;h=H[j+12>>2];if(b>>>0>>0){continue}break}a=j+16|0;n=H[j>>2];d=H[j+16>>2];b=H[j+20>>2]-d|0;q:{if(b>>>0<=32767){ya(a,8192-(b>>>2|0)|0);break q}if((b|0)==32768){break q}H[j+20>>2]=d+32768}d=j+28|0;b=H[d>>2];f=H[j+32>>2]-b>>3;r:{if(f>>>0>>0){ob(d,h-f|0);b=H[d>>2];break r}if(f>>>0>h>>>0){H[j+32>>2]=(h<<3)+b}if(!h){break j}}m=H[a>>2];f=0;d=0;while(1){e=n+(f<<2)|0;j=H[e>>2];l=(f<<3)+b|0;a=d;H[l+4>>2]=a;H[l>>2]=j;e=H[e>>2];d=e+a|0;if(d>>>0>8192){break j}s:{if(a>>>0>=d>>>0){break s}l=0;j=e&7;if(j){while(1){H[m+(a<<2)>>2]=f;a=a+1|0;l=l+1|0;if((j|0)!=(l|0)){continue}break}}if(e-1>>>0<=6){break s}while(1){e=m+(a<<2)|0;H[e>>2]=f;H[e+28>>2]=f;H[e+24>>2]=f;H[e+20>>2]=f;H[e+16>>2]=f;H[e+12>>2]=f;H[e+8>>2]=f;H[e+4>>2]=f;a=a+8|0;if((d|0)!=(a|0)){continue}break}}f=f+1|0;if((h|0)!=(f|0)){continue}break}k=(d|0)==8192}d=k}t:{if(!d|(H[i+20>>2]?0:g)){break t}d=0;m=ca-16|0;ca=m;u:{v:{if(J[c+38>>1]<=511){b=H[c+8>>2];a=H[c+12>>2];h=a;e=H[c+20>>2];k=H[c+16>>2];f=k+8|0;e=f>>>0<8?e+1|0:e;if(b>>>0>>0&(a|0)<=(e|0)|(a|0)<(e|0)){break u}k=k+H[c>>2]|0;a=I[k|0]|I[k+1|0]<<8|(I[k+2|0]<<16|I[k+3|0]<<24);k=I[k+4|0]|I[k+5|0]<<8|(I[k+6|0]<<16|I[k+7|0]<<24);H[c+16>>2]=f;H[c+20>>2]=e;break v}if(!gb(1,m+8|0,c)){break u}f=H[c+16>>2];e=H[c+20>>2];b=H[c+8>>2];h=H[c+12>>2];a=H[m+8>>2];k=H[m+12>>2]}j=b-f|0;b=h-((b>>>0>>0)+e|0)|0;if((b|0)==(k|0)&a>>>0>j>>>0|b>>>0>>0){break u}e=e+k|0;b=a+f|0;e=b>>>0>>0?e+1|0:e;H[c+16>>2]=b;H[c+20>>2]=e;if((a|0)<=0){break u}b=H[c>>2]+f|0;H[i+48>>2]=b;c=a-1|0;f=c+b|0;e=I[f|0];w:{if(e>>>0<=63){H[i+52>>2]=c;a=I[f|0]&63;break w}x:{switch((e>>>6|0)-1|0){case 0:if(a>>>0<2){break u}a=a-2|0;H[i+52>>2]=a;a=a+b|0;a=I[a+1|0]<<8&16128|I[a|0];break w;case 1:if(a>>>0<3){break u}a=a-3|0;H[i+52>>2]=a;a=a+b|0;a=I[a+1|0]<<8|I[a+2|0]<<16&4128768|I[a|0];break w;default:break x}}a=a-4|0;H[i+52>>2]=a;a=a+b|0;a=(I[a|0]|I[a+1|0]<<8|(I[a+2|0]<<16|I[a+3|0]<<24))&1073741823}H[i+56>>2]=a+32768;d=a>>>0<8355840}ca=m+16|0;if(!d){break t}if(!g){o=1;break t}b=H[i+52>>2];a=H[i+56>>2];c=H[i+36>>2];d=H[i+48>>2];f=H[i+24>>2];while(1){y:{if(a>>>0>32767){break y}while(1){if((b|0)<=0){break y}b=b-1|0;H[i+52>>2]=b;a=I[b+d|0]|a<<8;H[i+56>>2]=a;if(a>>>0<32768){continue}break}}e=a&8191;o=H[f+(e<<2)>>2];k=c+(o<<3)|0;a=(N(H[k>>2],a>>>13|0)+e|0)-H[k+4>>2]|0;H[i+56>>2]=a;H[r+(q<<2)>>2]=o;o=1;q=q+1|0;if((g|0)!=(q|0)){continue}break}}a=H[i+36>>2];if(a){H[i+40>>2]=a;oa(a)}a=H[i+24>>2];if(a){H[i+28>>2]=a;oa(a)}a=H[i+8>>2];if(a){H[i+12>>2]=a;oa(a)}ca=i- -64|0;b=o;break g;case 9:m=a;r=d;g=ca+-64|0;ca=g;H[g+56>>2]=0;H[g+48>>2]=0;H[g+52>>2]=0;H[g+40>>2]=0;H[g+44>>2]=0;H[g+32>>2]=0;H[g+36>>2]=0;H[g+24>>2]=0;H[g+28>>2]=0;H[g+16>>2]=0;H[g+20>>2]=0;H[g+8>>2]=0;H[g+12>>2]=0;j=g+8|0;a=J[c+38>>1];z:{A:{if(!a){break A}B:{if(a>>>0<=511){d=H[c+8>>2];b=H[c+12>>2];e=H[c+20>>2];a=H[c+16>>2];f=a+4|0;e=f>>>0<4?e+1|0:e;if(d>>>0>>0&(b|0)<=(e|0)|(b|0)<(e|0)){break A}a=a+H[c>>2]|0;h=I[a|0]|I[a+1|0]<<8|(I[a+2|0]<<16|I[a+3|0]<<24);H[j+12>>2]=h;e=H[c+20>>2];f=H[c+16>>2]+4|0;e=f>>>0<4?e+1|0:e;H[c+16>>2]=f;H[c+20>>2]=e;break B}if(!hb(1,j+12|0,c)){break A}f=H[c+16>>2];e=H[c+20>>2];h=H[j+12>>2]}a=H[c+8>>2];d=a-f|0;a=H[c+12>>2]-((a>>>0>>0)+e|0)|0;if(d>>>0>>6>>>0&(a|0)<=0|(a|0)<0){break A}b=H[j>>2];a=H[j+4>>2]-b>>2;C:{if(a>>>0>>0){ya(j,h-a|0);h=H[j+12>>2];break C}if(a>>>0<=h>>>0){break C}H[j+4>>2]=b+(h<<2)}d=1;if(!h){break z}f=H[c+16>>2];e=H[c+20>>2];s=H[j>>2];i=H[c+8>>2];n=H[c+12>>2];b=0;while(1){d=0;if((e|0)>=(n|0)&f>>>0>=i>>>0|(e|0)>(n|0)){break z}d=H[c>>2];p=I[d+f|0];f=f+1|0;e=f?e:e+1|0;H[c+16>>2]=f;H[c+20>>2]=e;a=p>>>2|0;l=0;D:{E:{F:{G:{t=p&3;switch(t|0){case 0:break E;case 3:break G;default:break F}}a=a+b|0;d=0;if(a>>>0>=h>>>0){break z}ra(s+(b<<2)|0,0,(p&252)+4|0);b=a;break D}while(1){if((f|0)==(i|0)&(e|0)==(n|0)){break A}h=I[d+f|0];f=f+1|0;e=f?e:e+1|0;H[c+16>>2]=f;H[c+20>>2]=e;a=h<<(l<<3|6)|a;l=l+1|0;if((t|0)!=(l|0)){continue}break}}H[s+(b<<2)>>2]=a}b=b+1|0;h=H[j+12>>2];if(b>>>0>>0){continue}break}a=j+16|0;n=H[j>>2];d=H[j+16>>2];b=H[j+20>>2]-d|0;H:{if(b>>>0<=131071){ya(a,32768-(b>>>2|0)|0);break H}if((b|0)==131072){break H}H[j+20>>2]=d+131072}d=j+28|0;b=H[d>>2];f=H[j+32>>2]-b>>3;I:{if(f>>>0>>0){ob(d,h-f|0);b=H[d>>2];break I}if(f>>>0>h>>>0){H[j+32>>2]=(h<<3)+b}if(!h){break A}}i=H[a>>2];f=0;d=0;while(1){e=n+(f<<2)|0;j=H[e>>2];l=(f<<3)+b|0;a=d;H[l+4>>2]=a;H[l>>2]=j;e=H[e>>2];d=e+a|0;if(d>>>0>32768){break A}J:{if(a>>>0>=d>>>0){break J}l=0;j=e&7;if(j){while(1){H[i+(a<<2)>>2]=f;a=a+1|0;l=l+1|0;if((j|0)!=(l|0)){continue}break}}if(e-1>>>0<=6){break J}while(1){e=i+(a<<2)|0;H[e>>2]=f;H[e+28>>2]=f;H[e+24>>2]=f;H[e+20>>2]=f;H[e+16>>2]=f;H[e+12>>2]=f;H[e+8>>2]=f;H[e+4>>2]=f;a=a+8|0;if((d|0)!=(a|0)){continue}break}}f=f+1|0;if((h|0)!=(f|0)){continue}break}k=(d|0)==32768}d=k}K:{if(!d|(H[g+20>>2]?0:m)){break K}d=0;j=ca-16|0;ca=j;L:{M:{if(J[c+38>>1]<=511){b=H[c+8>>2];a=H[c+12>>2];h=a;e=H[c+20>>2];k=H[c+16>>2];f=k+8|0;e=f>>>0<8?e+1|0:e;if(b>>>0>>0&(a|0)<=(e|0)|(a|0)<(e|0)){break L}k=k+H[c>>2]|0;a=I[k|0]|I[k+1|0]<<8|(I[k+2|0]<<16|I[k+3|0]<<24);k=I[k+4|0]|I[k+5|0]<<8|(I[k+6|0]<<16|I[k+7|0]<<24);H[c+16>>2]=f;H[c+20>>2]=e;break M}if(!gb(1,j+8|0,c)){break L}f=H[c+16>>2];e=H[c+20>>2];b=H[c+8>>2];h=H[c+12>>2];a=H[j+8>>2];k=H[j+12>>2]}i=b-f|0;b=h-((b>>>0>>0)+e|0)|0;if((b|0)==(k|0)&a>>>0>i>>>0|b>>>0>>0){break L}i=e+k|0;b=a+f|0;i=b>>>0>>0?i+1|0:i;H[c+16>>2]=b;H[c+20>>2]=i;if((a|0)<=0){break L}b=H[c>>2]+f|0;H[g+48>>2]=b;c=a-1|0;f=c+b|0;e=I[f|0];N:{if(e>>>0<=63){H[g+52>>2]=c;a=I[f|0]&63;break N}O:{switch((e>>>6|0)-1|0){case 0:if(a>>>0<2){break L}a=a-2|0;H[g+52>>2]=a;a=a+b|0;a=I[a+1|0]<<8&16128|I[a|0];break N;case 1:if(a>>>0<3){break L}a=a-3|0;H[g+52>>2]=a;a=a+b|0;a=I[a+1|0]<<8|I[a+2|0]<<16&4128768|I[a|0];break N;default:break O}}a=a-4|0;H[g+52>>2]=a;a=a+b|0;a=(I[a|0]|I[a+1|0]<<8|(I[a+2|0]<<16|I[a+3|0]<<24))&1073741823}H[g+56>>2]=a+131072;d=a>>>0<33423360}ca=j+16|0;if(!d){break K}if(!m){o=1;break K}b=H[g+52>>2];a=H[g+56>>2];c=H[g+36>>2];d=H[g+48>>2];f=H[g+24>>2];while(1){P:{if(a>>>0>131071){break P}while(1){if((b|0)<=0){break P}b=b-1|0;H[g+52>>2]=b;a=I[b+d|0]|a<<8;H[g+56>>2]=a;if(a>>>0<131072){continue}break}}e=a&32767;o=H[f+(e<<2)>>2];k=c+(o<<3)|0;a=(N(H[k>>2],a>>>15|0)+e|0)-H[k+4>>2]|0;H[g+56>>2]=a;H[r+(q<<2)>>2]=o;o=1;q=q+1|0;if((m|0)!=(q|0)){continue}break}}a=H[g+36>>2];if(a){H[g+40>>2]=a;oa(a)}a=H[g+24>>2];if(a){H[g+28>>2]=a;oa(a)}a=H[g+8>>2];if(a){H[g+12>>2]=a;oa(a)}ca=g- -64|0;b=o;break g;case 10:m=a;j=d;g=ca+-64|0;ca=g;H[g+56>>2]=0;H[g+48>>2]=0;H[g+52>>2]=0;H[g+40>>2]=0;H[g+44>>2]=0;H[g+32>>2]=0;H[g+36>>2]=0;H[g+24>>2]=0;H[g+28>>2]=0;H[g+16>>2]=0;H[g+20>>2]=0;H[g+8>>2]=0;H[g+12>>2]=0;n=g+8|0;a=J[c+38>>1];Q:{R:{if(!a){break R}S:{if(a>>>0<=511){d=H[c+8>>2];b=H[c+12>>2];e=H[c+20>>2];a=H[c+16>>2];f=a+4|0;e=f>>>0<4?e+1|0:e;if(d>>>0>>0&(b|0)<=(e|0)|(b|0)<(e|0)){break R}a=a+H[c>>2]|0;h=I[a|0]|I[a+1|0]<<8|(I[a+2|0]<<16|I[a+3|0]<<24);H[n+12>>2]=h;e=H[c+20>>2];f=H[c+16>>2]+4|0;e=f>>>0<4?e+1|0:e;H[c+16>>2]=f;H[c+20>>2]=e;break S}if(!hb(1,n+12|0,c)){break R}f=H[c+16>>2];e=H[c+20>>2];h=H[n+12>>2]}a=H[c+8>>2];d=a-f|0;a=H[c+12>>2]-((a>>>0>>0)+e|0)|0;if(d>>>0>>6>>>0&(a|0)<=0|(a|0)<0){break R}b=H[n>>2];a=H[n+4>>2]-b>>2;T:{if(a>>>0>>0){ya(n,h-a|0);h=H[n+12>>2];break T}if(a>>>0<=h>>>0){break T}H[n+4>>2]=b+(h<<2)}d=1;if(!h){break Q}f=H[c+16>>2];e=H[c+20>>2];t=H[n>>2];r=H[c+8>>2];p=H[c+12>>2];b=0;while(1){d=0;if((e|0)>=(p|0)&f>>>0>=r>>>0|(e|0)>(p|0)){break Q}d=H[c>>2];s=I[d+f|0];f=f+1|0;i=f?e:e+1|0;H[c+16>>2]=f;e=i;H[c+20>>2]=e;a=s>>>2|0;l=0;U:{V:{W:{X:{i=s&3;switch(i|0){case 0:break V;case 3:break X;default:break W}}a=a+b|0;d=0;if(a>>>0>=h>>>0){break Q}ra(t+(b<<2)|0,0,(s&252)+4|0);b=a;break U}while(1){if((f|0)==(r|0)&(e|0)==(p|0)){break R}h=I[d+f|0];f=f+1|0;e=f?e:e+1|0;H[c+16>>2]=f;H[c+20>>2]=e;a=h<<(l<<3|6)|a;l=l+1|0;if((i|0)!=(l|0)){continue}break}}H[t+(b<<2)>>2]=a}b=b+1|0;h=H[n+12>>2];if(b>>>0>>0){continue}break}a=n+16|0;r=H[n>>2];d=H[n+16>>2];b=H[n+20>>2]-d|0;Y:{if(b>>>0<=262143){ya(a,65536-(b>>>2|0)|0);break Y}if((b|0)==262144){break Y}H[n+20>>2]=d+262144}d=n+28|0;b=H[d>>2];f=H[n+32>>2]-b>>3;Z:{if(f>>>0>>0){ob(d,h-f|0);b=H[d>>2];break Z}if(f>>>0>h>>>0){H[n+32>>2]=(h<<3)+b}if(!h){break R}}i=H[a>>2];f=0;d=0;while(1){e=r+(f<<2)|0;l=H[e>>2];n=(f<<3)+b|0;a=d;H[n+4>>2]=a;H[n>>2]=l;e=H[e>>2];d=e+a|0;if(d>>>0>65536){break R}_:{if(a>>>0>=d>>>0){break _}l=0;n=e&7;if(n){while(1){H[i+(a<<2)>>2]=f;a=a+1|0;l=l+1|0;if((n|0)!=(l|0)){continue}break}}if(e-1>>>0<=6){break _}while(1){e=i+(a<<2)|0;H[e>>2]=f;H[e+28>>2]=f;H[e+24>>2]=f;H[e+20>>2]=f;H[e+16>>2]=f;H[e+12>>2]=f;H[e+8>>2]=f;H[e+4>>2]=f;a=a+8|0;if((d|0)!=(a|0)){continue}break}}f=f+1|0;if((h|0)!=(f|0)){continue}break}k=(d|0)==65536}d=k}$:{if(!d|(H[g+20>>2]?0:m)){break $}d=0;i=ca-16|0;ca=i;aa:{ba:{if(J[c+38>>1]<=511){b=H[c+8>>2];a=H[c+12>>2];h=a;e=H[c+20>>2];k=H[c+16>>2];f=k+8|0;e=f>>>0<8?e+1|0:e;if(b>>>0>>0&(a|0)<=(e|0)|(a|0)<(e|0)){break aa}k=k+H[c>>2]|0;a=I[k|0]|I[k+1|0]<<8|(I[k+2|0]<<16|I[k+3|0]<<24);k=I[k+4|0]|I[k+5|0]<<8|(I[k+6|0]<<16|I[k+7|0]<<24);H[c+16>>2]=f;H[c+20>>2]=e;break ba}if(!gb(1,i+8|0,c)){break aa}f=H[c+16>>2];e=H[c+20>>2];b=H[c+8>>2];h=H[c+12>>2];a=H[i+8>>2];k=H[i+12>>2]}r=b-f|0;b=h-((b>>>0>>0)+e|0)|0;if((b|0)==(k|0)&a>>>0>r>>>0|b>>>0>>0){break aa}e=e+k|0;b=a+f|0;e=b>>>0>>0?e+1|0:e;H[c+16>>2]=b;H[c+20>>2]=e;if((a|0)<=0){break aa}b=H[c>>2]+f|0;H[g+48>>2]=b;c=a-1|0;f=c+b|0;e=I[f|0];ca:{if(e>>>0<=63){H[g+52>>2]=c;a=I[f|0]&63;break ca}da:{switch((e>>>6|0)-1|0){case 0:if(a>>>0<2){break aa}a=a-2|0;H[g+52>>2]=a;a=a+b|0;a=I[a+1|0]<<8&16128|I[a|0];break ca;case 1:if(a>>>0<3){break aa}a=a-3|0;H[g+52>>2]=a;a=a+b|0;a=I[a+1|0]<<8|I[a+2|0]<<16&4128768|I[a|0];break ca;default:break da}}a=a-4|0;H[g+52>>2]=a;a=a+b|0;a=(I[a|0]|I[a+1|0]<<8|(I[a+2|0]<<16|I[a+3|0]<<24))&1073741823}H[g+56>>2]=a+262144;d=a>>>0<66846720}ca=i+16|0;if(!d){break $}if(!m){o=1;break $}b=H[g+52>>2];a=H[g+56>>2];c=H[g+36>>2];d=H[g+48>>2];f=H[g+24>>2];while(1){ea:{if(a>>>0>262143){break ea}while(1){if((b|0)<=0){break ea}b=b-1|0;H[g+52>>2]=b;a=I[b+d|0]|a<<8;H[g+56>>2]=a;if(a>>>0<262144){continue}break}}e=a&65535;o=H[f+(e<<2)>>2];k=c+(o<<3)|0;a=(N(H[k>>2],a>>>16|0)+e|0)-H[k+4>>2]|0;H[g+56>>2]=a;H[j+(q<<2)>>2]=o;o=1;q=q+1|0;if((m|0)!=(q|0)){continue}break}}a=H[g+36>>2];if(a){H[g+40>>2]=a;oa(a)}a=H[g+24>>2];if(a){H[g+28>>2]=a;oa(a)}a=H[g+8>>2];if(a){H[g+12>>2]=a;oa(a)}ca=g- -64|0;b=o;break g;case 11:m=a;r=d;g=ca+-64|0;ca=g;H[g+56>>2]=0;H[g+48>>2]=0;H[g+52>>2]=0;H[g+40>>2]=0;H[g+44>>2]=0;H[g+32>>2]=0;H[g+36>>2]=0;H[g+24>>2]=0;H[g+28>>2]=0;H[g+16>>2]=0;H[g+20>>2]=0;H[g+8>>2]=0;H[g+12>>2]=0;j=g+8|0;a=J[c+38>>1];fa:{ga:{if(!a){break ga}ha:{if(a>>>0<=511){d=H[c+8>>2];b=H[c+12>>2];e=H[c+20>>2];a=H[c+16>>2];f=a+4|0;e=f>>>0<4?e+1|0:e;if(d>>>0>>0&(b|0)<=(e|0)|(b|0)<(e|0)){break ga}a=a+H[c>>2]|0;h=I[a|0]|I[a+1|0]<<8|(I[a+2|0]<<16|I[a+3|0]<<24);H[j+12>>2]=h;i=H[c+20>>2];f=H[c+16>>2]+4|0;i=f>>>0<4?i+1|0:i;H[c+16>>2]=f;e=i;H[c+20>>2]=e;break ha}if(!hb(1,j+12|0,c)){break ga}f=H[c+16>>2];e=H[c+20>>2];h=H[j+12>>2]}a=H[c+8>>2];d=a-f|0;a=H[c+12>>2]-((a>>>0>>0)+e|0)|0;if(d>>>0>>6>>>0&(a|0)<=0|(a|0)<0){break ga}b=H[j>>2];a=H[j+4>>2]-b>>2;ia:{if(a>>>0>>0){ya(j,h-a|0);h=H[j+12>>2];break ia}if(a>>>0<=h>>>0){break ia}H[j+4>>2]=b+(h<<2)}d=1;if(!h){break fa}f=H[c+16>>2];e=H[c+20>>2];s=H[j>>2];i=H[c+8>>2];n=H[c+12>>2];b=0;while(1){d=0;if((e|0)>=(n|0)&f>>>0>=i>>>0|(e|0)>(n|0)){break fa}d=H[c>>2];p=I[d+f|0];f=f+1|0;e=f?e:e+1|0;H[c+16>>2]=f;H[c+20>>2]=e;a=p>>>2|0;l=0;ja:{ka:{la:{ma:{t=p&3;switch(t|0){case 0:break ka;case 3:break ma;default:break la}}a=a+b|0;d=0;if(a>>>0>=h>>>0){break fa}ra(s+(b<<2)|0,0,(p&252)+4|0);b=a;break ja}while(1){if((f|0)==(i|0)&(e|0)==(n|0)){break ga}h=I[d+f|0];f=f+1|0;e=f?e:e+1|0;H[c+16>>2]=f;H[c+20>>2]=e;a=h<<(l<<3|6)|a;l=l+1|0;if((t|0)!=(l|0)){continue}break}}H[s+(b<<2)>>2]=a}b=b+1|0;h=H[j+12>>2];if(b>>>0>>0){continue}break}a=j+16|0;n=H[j>>2];d=H[j+16>>2];b=H[j+20>>2]-d|0;na:{if(b>>>0<=1048575){ya(a,262144-(b>>>2|0)|0);break na}if((b|0)==1048576){break na}H[j+20>>2]=d- -1048576}d=j+28|0;b=H[d>>2];f=H[j+32>>2]-b>>3;oa:{if(f>>>0>>0){ob(d,h-f|0);b=H[d>>2];break oa}if(f>>>0>h>>>0){H[j+32>>2]=(h<<3)+b}if(!h){break ga}}i=H[a>>2];f=0;d=0;while(1){e=n+(f<<2)|0;j=H[e>>2];l=(f<<3)+b|0;a=d;H[l+4>>2]=a;H[l>>2]=j;e=H[e>>2];d=e+a|0;if(d>>>0>262144){break ga}pa:{if(a>>>0>=d>>>0){break pa}l=0;j=e&7;if(j){while(1){H[i+(a<<2)>>2]=f;a=a+1|0;l=l+1|0;if((j|0)!=(l|0)){continue}break}}if(e-1>>>0<=6){break pa}while(1){e=i+(a<<2)|0;H[e>>2]=f;H[e+28>>2]=f;H[e+24>>2]=f;H[e+20>>2]=f;H[e+16>>2]=f;H[e+12>>2]=f;H[e+8>>2]=f;H[e+4>>2]=f;a=a+8|0;if((d|0)!=(a|0)){continue}break}}f=f+1|0;if((h|0)!=(f|0)){continue}break}k=(d|0)==262144}d=k}qa:{if(!d|(H[g+20>>2]?0:m)){break qa}d=0;j=ca-16|0;ca=j;ra:{sa:{if(J[c+38>>1]<=511){b=H[c+8>>2];a=H[c+12>>2];h=a;i=H[c+20>>2];k=H[c+16>>2];f=k+8|0;i=f>>>0<8?i+1|0:i;e=i;if(b>>>0>>0&(e|0)>=(a|0)|(a|0)<(e|0)){break ra}k=k+H[c>>2]|0;a=I[k|0]|I[k+1|0]<<8|(I[k+2|0]<<16|I[k+3|0]<<24);k=I[k+4|0]|I[k+5|0]<<8|(I[k+6|0]<<16|I[k+7|0]<<24);H[c+16>>2]=f;H[c+20>>2]=e;break sa}if(!gb(1,j+8|0,c)){break ra}f=H[c+16>>2];e=H[c+20>>2];b=H[c+8>>2];h=H[c+12>>2];a=H[j+8>>2];k=H[j+12>>2]}i=b-f|0;b=h-((b>>>0>>0)+e|0)|0;if((b|0)==(k|0)&a>>>0>i>>>0|b>>>0>>0){break ra}e=e+k|0;b=a+f|0;e=b>>>0>>0?e+1|0:e;H[c+16>>2]=b;H[c+20>>2]=e;if((a|0)<=0){break ra}b=H[c>>2]+f|0;H[g+48>>2]=b;c=a-1|0;f=c+b|0;e=I[f|0];ta:{if(e>>>0<=63){H[g+52>>2]=c;a=I[f|0]&63;break ta}ua:{switch((e>>>6|0)-1|0){case 0:if(a>>>0<2){break ra}a=a-2|0;H[g+52>>2]=a;a=a+b|0;a=I[a+1|0]<<8&16128|I[a|0];break ta;case 1:if(a>>>0<3){break ra}a=a-3|0;H[g+52>>2]=a;a=a+b|0;a=I[a+1|0]<<8|I[a+2|0]<<16&4128768|I[a|0];break ta;default:break ua}}a=a-4|0;H[g+52>>2]=a;a=a+b|0;a=(I[a|0]|I[a+1|0]<<8|(I[a+2|0]<<16|I[a+3|0]<<24))&1073741823}H[g+56>>2]=a- -1048576;d=a>>>0<267386880}ca=j+16|0;if(!d){break qa}if(!m){o=1;break qa}b=H[g+52>>2];a=H[g+56>>2];c=H[g+36>>2];d=H[g+48>>2];f=H[g+24>>2];while(1){va:{if(a>>>0>1048575){break va}while(1){if((b|0)<=0){break va}b=b-1|0;H[g+52>>2]=b;a=I[b+d|0]|a<<8;H[g+56>>2]=a;if(a>>>0<1048576){continue}break}}e=a&262143;o=H[f+(e<<2)>>2];k=c+(o<<3)|0;a=(N(H[k>>2],a>>>18|0)+e|0)-H[k+4>>2]|0;H[g+56>>2]=a;H[r+(q<<2)>>2]=o;o=1;q=q+1|0;if((m|0)!=(q|0)){continue}break}}a=H[g+36>>2];if(a){H[g+40>>2]=a;oa(a)}a=H[g+24>>2];if(a){H[g+28>>2]=a;oa(a)}a=H[g+8>>2];if(a){H[g+12>>2]=a;oa(a)}ca=g- -64|0;b=o;break g;case 12:m=a;r=d;g=ca+-64|0;ca=g;H[g+56>>2]=0;H[g+48>>2]=0;H[g+52>>2]=0;H[g+40>>2]=0;H[g+44>>2]=0;H[g+32>>2]=0;H[g+36>>2]=0;H[g+24>>2]=0;H[g+28>>2]=0;H[g+16>>2]=0;H[g+20>>2]=0;H[g+8>>2]=0;H[g+12>>2]=0;j=g+8|0;a=J[c+38>>1];wa:{xa:{if(!a){break xa}ya:{if(a>>>0<=511){d=H[c+8>>2];b=H[c+12>>2];i=H[c+20>>2];a=H[c+16>>2];f=a+4|0;i=f>>>0<4?i+1|0:i;if(d>>>0>>0&(b|0)<=(i|0)|(b|0)<(i|0)){break xa}a=a+H[c>>2]|0;h=I[a|0]|I[a+1|0]<<8|(I[a+2|0]<<16|I[a+3|0]<<24);H[j+12>>2]=h;e=H[c+20>>2];f=H[c+16>>2]+4|0;e=f>>>0<4?e+1|0:e;H[c+16>>2]=f;H[c+20>>2]=e;break ya}if(!hb(1,j+12|0,c)){break xa}f=H[c+16>>2];e=H[c+20>>2];h=H[j+12>>2]}a=H[c+8>>2];d=a-f|0;a=H[c+12>>2]-((a>>>0>>0)+e|0)|0;if(d>>>0>>6>>>0&(a|0)<=0|(a|0)<0){break xa}b=H[j>>2];a=H[j+4>>2]-b>>2;za:{if(a>>>0>>0){ya(j,h-a|0);h=H[j+12>>2];break za}if(a>>>0<=h>>>0){break za}H[j+4>>2]=b+(h<<2)}d=1;if(!h){break wa}f=H[c+16>>2];e=H[c+20>>2];s=H[j>>2];i=H[c+8>>2];n=H[c+12>>2];b=0;while(1){d=0;if((e|0)>=(n|0)&f>>>0>=i>>>0|(e|0)>(n|0)){break wa}d=H[c>>2];p=I[d+f|0];f=f+1|0;e=f?e:e+1|0;H[c+16>>2]=f;H[c+20>>2]=e;a=p>>>2|0;l=0;Aa:{Ba:{Ca:{Da:{t=p&3;switch(t|0){case 0:break Ba;case 3:break Da;default:break Ca}}a=a+b|0;d=0;if(a>>>0>=h>>>0){break wa}ra(s+(b<<2)|0,0,(p&252)+4|0);b=a;break Aa}while(1){if((f|0)==(i|0)&(e|0)==(n|0)){break xa}h=I[d+f|0];f=f+1|0;e=f?e:e+1|0;H[c+16>>2]=f;H[c+20>>2]=e;a=h<<(l<<3|6)|a;l=l+1|0;if((t|0)!=(l|0)){continue}break}}H[s+(b<<2)>>2]=a}b=b+1|0;h=H[j+12>>2];if(b>>>0>>0){continue}break}a=j+16|0;n=H[j>>2];d=H[j+16>>2];b=H[j+20>>2]-d|0;Ea:{if(b>>>0<=2097151){ya(a,524288-(b>>>2|0)|0);break Ea}if((b|0)==2097152){break Ea}H[j+20>>2]=d+2097152}d=j+28|0;b=H[d>>2];f=H[j+32>>2]-b>>3;Fa:{if(f>>>0>>0){ob(d,h-f|0);b=H[d>>2];break Fa}if(f>>>0>h>>>0){H[j+32>>2]=(h<<3)+b}if(!h){break xa}}i=H[a>>2];f=0;d=0;while(1){e=n+(f<<2)|0;j=H[e>>2];l=(f<<3)+b|0;a=d;H[l+4>>2]=a;H[l>>2]=j;e=H[e>>2];d=e+a|0;if(d>>>0>524288){break xa}Ga:{if(a>>>0>=d>>>0){break Ga}l=0;j=e&7;if(j){while(1){H[i+(a<<2)>>2]=f;a=a+1|0;l=l+1|0;if((j|0)!=(l|0)){continue}break}}if(e-1>>>0<=6){break Ga}while(1){e=i+(a<<2)|0;H[e>>2]=f;H[e+28>>2]=f;H[e+24>>2]=f;H[e+20>>2]=f;H[e+16>>2]=f;H[e+12>>2]=f;H[e+8>>2]=f;H[e+4>>2]=f;a=a+8|0;if((d|0)!=(a|0)){continue}break}}f=f+1|0;if((h|0)!=(f|0)){continue}break}k=(d|0)==524288}d=k}Ha:{if(!d|(H[g+20>>2]?0:m)){break Ha}d=0;i=ca-16|0;ca=i;Ia:{Ja:{if(J[c+38>>1]<=511){b=H[c+8>>2];a=H[c+12>>2];h=a;e=H[c+20>>2];k=H[c+16>>2];f=k+8|0;e=f>>>0<8?e+1|0:e;if(b>>>0>>0&(a|0)<=(e|0)|(a|0)<(e|0)){break Ia}k=k+H[c>>2]|0;a=I[k|0]|I[k+1|0]<<8|(I[k+2|0]<<16|I[k+3|0]<<24);k=I[k+4|0]|I[k+5|0]<<8|(I[k+6|0]<<16|I[k+7|0]<<24);H[c+16>>2]=f;H[c+20>>2]=e;break Ja}if(!gb(1,i+8|0,c)){break Ia}f=H[c+16>>2];e=H[c+20>>2];b=H[c+8>>2];h=H[c+12>>2];a=H[i+8>>2];k=H[i+12>>2]}j=b-f|0;b=h-((b>>>0>>0)+e|0)|0;if((b|0)==(k|0)&a>>>0>j>>>0|b>>>0>>0){break Ia}e=e+k|0;b=a+f|0;e=b>>>0>>0?e+1|0:e;H[c+16>>2]=b;H[c+20>>2]=e;if((a|0)<=0){break Ia}b=H[c>>2]+f|0;H[g+48>>2]=b;c=a-1|0;f=c+b|0;e=I[f|0];Ka:{if(e>>>0<=63){H[g+52>>2]=c;a=I[f|0]&63;break Ka}La:{switch((e>>>6|0)-1|0){case 0:if(a>>>0<2){break Ia}a=a-2|0;H[g+52>>2]=a;a=a+b|0;a=I[a+1|0]<<8&16128|I[a|0];break Ka;case 1:if(a>>>0<3){break Ia}a=a-3|0;H[g+52>>2]=a;a=a+b|0;a=I[a+1|0]<<8|I[a+2|0]<<16&4128768|I[a|0];break Ka;default:break La}}a=a-4|0;H[g+52>>2]=a;a=a+b|0;a=(I[a|0]|I[a+1|0]<<8|(I[a+2|0]<<16|I[a+3|0]<<24))&1073741823}H[g+56>>2]=a+2097152;d=a>>>0<534773760}ca=i+16|0;if(!d){break Ha}if(!m){o=1;break Ha}b=H[g+52>>2];a=H[g+56>>2];c=H[g+36>>2];d=H[g+48>>2];f=H[g+24>>2];while(1){Ma:{if(a>>>0>2097151){break Ma}while(1){if((b|0)<=0){break Ma}b=b-1|0;H[g+52>>2]=b;a=I[b+d|0]|a<<8;H[g+56>>2]=a;if(a>>>0<2097152){continue}break}}e=a&524287;o=H[f+(e<<2)>>2];k=c+(o<<3)|0;a=(N(H[k>>2],a>>>19|0)+e|0)-H[k+4>>2]|0;H[g+56>>2]=a;H[r+(q<<2)>>2]=o;o=1;q=q+1|0;if((m|0)!=(q|0)){continue}break}}a=H[g+36>>2];if(a){H[g+40>>2]=a;oa(a)}a=H[g+24>>2];if(a){H[g+28>>2]=a;oa(a)}a=H[g+8>>2];if(a){H[g+12>>2]=a;oa(a)}ca=g- -64|0;b=o;break g;case 17:b=Le(a,c,d);break g;case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:b=ca+-64|0;ca=b;H[b+56>>2]=0;H[b+48>>2]=0;H[b+52>>2]=0;H[b+40>>2]=0;H[b+44>>2]=0;H[b+32>>2]=0;H[b+36>>2]=0;H[b+24>>2]=0;H[b+28>>2]=0;H[b+16>>2]=0;H[b+20>>2]=0;H[b+8>>2]=0;H[b+12>>2]=0;Na:{if(!Ne(b+8|0,c)|(H[b+20>>2]?0:a)){break Na}if(!Me(b+8|0,c)){break Na}if(!a){f=1;break Na}e=H[b+52>>2];c=H[b+56>>2];k=H[b+36>>2];i=H[b+48>>2];g=H[b+24>>2];while(1){Oa:{if(c>>>0>16383){break Oa}while(1){if((e|0)<=0){break Oa}e=e-1|0;H[b+52>>2]=e;c=I[e+i|0]|c<<8;H[b+56>>2]=c;if(c>>>0<16384){continue}break}}f=c&4095;m=H[g+(f<<2)>>2];r=k+(m<<3)|0;c=(N(H[r>>2],c>>>12|0)+f|0)-H[r+4>>2]|0;H[b+56>>2]=c;H[(o<<2)+d>>2]=m;f=1;o=o+1|0;if((o|0)!=(a|0)){continue}break}}a=H[b+36>>2];if(a){H[b+40>>2]=a;oa(a)}a=H[b+24>>2];if(a){H[b+28>>2]=a;oa(a)}a=H[b+8>>2];if(a){H[b+12>>2]=a;oa(a)}ca=b- -64|0;b=f;break g;case 13:case 14:case 15:case 16:break h;default:break g}}b=Le(a,c,d)}f=b}return f}function gi(a){a=a|0;var b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0,x=0,y=0,z=0,A=0,B=0,C=0,D=0,E=0,L=0,M=0,O=0,P=0,Q=0,R=0;s=ca+-64|0;ca=s;H[a+132>>2]=0;if(H[a+148>>2]){c=H[a+144>>2];if(c){while(1){d=H[c>>2];oa(c);c=d;if(c){continue}break}}c=0;H[a+144>>2]=0;d=H[a+140>>2];a:{if(!d){break a}if(d>>>0>=4){g=d&-4;while(1){e=c<<2;H[e+H[a+136>>2]>>2]=0;H[H[a+136>>2]+(e|4)>>2]=0;H[H[a+136>>2]+(e|8)>>2]=0;H[H[a+136>>2]+(e|12)>>2]=0;c=c+4|0;b=b+4|0;if((g|0)!=(b|0)){continue}break}}b=d&3;if(!b){break a}while(1){H[H[a+136>>2]+(c<<2)>>2]=0;c=c+1|0;u=u+1|0;if((b|0)!=(u|0)){continue}break}}H[a+148>>2]=0}b:{c:{d:{c=H[a+4>>2];u=I[c+36|0];b=u<<8|I[c+37|0];if(b>>>0<=513){i=H[c+32>>2];e:{if(b>>>0<=511){b=H[i+20>>2];e=H[i+16>>2];d=e+4|0;b=d>>>0<4?b+1|0:b;g=b;h=H[i+12>>2];if(K[i+8>>2]>>0&(b|0)>=(h|0)|(b|0)>(h|0)){break d}b=e+H[i>>2]|0;b=I[b|0]|I[b+1|0]<<8|(I[b+2|0]<<16|I[b+3|0]<<24);H[i+16>>2]=d;H[i+20>>2]=g;break e}if(!Ea(1,s,i)){break d}c=H[a+4>>2];u=I[c+36|0];b=H[s>>2]}H[a+132>>2]=b}g=H[c+32>>2];f:{g:{h:{if((u&255)>>>0<=1){u=0;d=H[g+20>>2];e=H[g+16>>2];b=e+4|0;d=b>>>0<4?d+1|0:d;i=H[g+12>>2];if(K[g+8>>2]>>0&(i|0)<=(d|0)|(d|0)>(i|0)){break c}e=e+H[g>>2]|0;e=I[e|0]|I[e+1|0]<<8|(I[e+2|0]<<16|I[e+3|0]<<24);H[s+60>>2]=e;H[g+16>>2]=b;H[g+20>>2]=d;H[a+156>>2]=e;n=a+156|0;break h}u=0;if(!Ea(1,s+60|0,g)){break c}c=H[a+4>>2];b=I[c+36|0];H[a+156>>2]=H[s+60>>2];n=a+156|0;if(b>>>0>1){break g}}g=H[c+32>>2];h=H[g+8>>2];i=H[g+12>>2];c=H[g+20>>2];d=H[g+16>>2];b=d+4|0;c=b>>>0<4?c+1|0:c;e=b;if(b>>>0>h>>>0&(c|0)>=(i|0)|(c|0)>(i|0)){break c}b=d+H[g>>2]|0;b=I[b|0]|I[b+1|0]<<8|(I[b+2|0]<<16|I[b+3|0]<<24);H[s+56>>2]=b;H[g+16>>2]=e;H[g+20>>2]=c;break f}if(!Ea(1,s+56|0,H[c+32>>2])){break c}b=H[s+56>>2]}if(b>>>0>1431655765|K[n>>2]>N(b,3)>>>0){break c}f=H[a+4>>2];g=H[f+32>>2];c=g;e=H[c+8>>2];i=H[c+16>>2];j=H[c+12>>2];d=H[c+20>>2];c=d;if((j|0)<=(c|0)&e>>>0<=i>>>0|(c|0)>(j|0)){break c}n=H[g>>2];o=I[n+i|0];h=i+1|0;c=h?c:c+1|0;H[g+16>>2]=h;H[g+20>>2]=c;i:{if(I[f+36|0]<=1){f=e;c=j;e=i+5|0;d=e>>>0<5?d+1|0:d;if((c|0)<=(d|0)&e>>>0>f>>>0|(c|0)<(d|0)){break c}c=h+n|0;n=I[c|0]|I[c+1|0]<<8|(I[c+2|0]<<16|I[c+3|0]<<24);H[s+52>>2]=n;H[g+16>>2]=e;H[g+20>>2]=d;break i}if(!Ea(1,s+52|0,g)){break c}n=H[s+52>>2]}if(b>>>0>>0|((n>>>0)/3|0)+n>>>0>>0){break c}c=H[a+4>>2];i=H[c+32>>2];j:{if(I[c+36|0]<=1){c=H[i+20>>2];e=H[i+16>>2];d=e+4|0;c=d>>>0<4?c+1|0:c;g=d;f=K[i+8>>2]>>0;d=H[i+12>>2];if(f&(d|0)<=(c|0)|(c|0)>(d|0)){break c}d=e+H[i>>2]|0;d=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[s+48>>2]=d;H[i+16>>2]=g;H[i+20>>2]=c;break j}if(!Ea(1,s+48|0,i)){break c}d=H[s+48>>2]}if(d>>>0>n>>>0){break c}H[a+28>>2]=H[a+24>>2];e=$b(pa(88));c=H[a+8>>2];H[a+8>>2]=e;if(c){cb(c);if(!H[a+8>>2]){break c}}H[a+164>>2]=H[a+160>>2];Jb(a+160|0,b);H[a+176>>2]=H[a+172>>2];Jb(a+172|0,b);H[a- -64>>2]=0;H[a+92>>2]=-1;H[a+84>>2]=-1;H[a+88>>2]=-1;H[a+40>>2]=H[a+36>>2];H[a+52>>2]=H[a+48>>2];H[a+76>>2]=H[a+72>>2];B=a+216|0;ed(B);dd(B,o);if(!Lc(H[a+8>>2],b,H[a+156>>2]+d|0)){break c}c=H[a+156>>2];F[s|0]=1;Oa(a+120|0,c+d|0,s);c=H[a+4>>2];b=J[c+36>>1];b=(b<<8|b>>>8)&65535;k:{if(b>>>0<=513){i=H[c+32>>2];l:{if(b>>>0<=511){b=H[i+20>>2];e=H[i+16>>2];c=e+4|0;b=c>>>0<4?b+1|0:b;g=b;h=H[i+12>>2];if(K[i+8>>2]>>0&(b|0)>=(h|0)|(b|0)>(h|0)){break c}b=e+H[i>>2]|0;b=I[b|0]|I[b+1|0]<<8|(I[b+2|0]<<16|I[b+3|0]<<24);H[i+16>>2]=c;H[i+20>>2]=g;break l}if(!Ea(1,s+44|0,i)){break c}b=H[s+44>>2]}if(!b){break c}c=H[H[a+4>>2]+32>>2];e=H[c+8>>2];g=H[c+16>>2];i=e-g|0;c=H[c+12>>2]-(H[c+20>>2]+(e>>>0>>0)|0)|0;if((c|0)<=0&b>>>0>i>>>0|(c|0)<0){break c}c=Ha(s);e=H[H[a+4>>2]+32>>2];g=H[e+16>>2];i=(g+H[e>>2]|0)+b|0;g=H[e+8>>2]-g|0;G[c+38>>1]=J[e+38>>1];H[c>>2]=i;H[c+16>>2]=0;H[c+20>>2]=0;H[c+8>>2]=g-b;H[c+12>>2]=0;C=Ib(a,c);if((C|0)==-1){break c}M=C>>31;break k}C=-1;M=-1;if((Ib(a,H[c+32>>2])|0)==-1){break c}}e=a+232|0;Ee(e,a);H[a+372>>2]=o;H[a+384>>2]=H[a+156>>2]+d;O=Ha(s);g=O;b=0;j=ca-16|0;ca=j;m:{n:{c=H[e+144>>2];c=J[(ea[H[H[c>>2]+32>>2]](c)|0)+36>>1];if(((c<<8|c>>>8)&65535)>>>0<=513){c=H[e+4>>2];H[e+40>>2]=H[e>>2];H[e+44>>2]=c;c=H[e+36>>2];H[e+72>>2]=H[e+32>>2];H[e+76>>2]=c;d=H[e+28>>2];c=e- -64|0;H[c>>2]=H[e+24>>2];H[c+4>>2]=d;c=H[e+20>>2];H[e+56>>2]=H[e+16>>2];H[e+60>>2]=c;c=H[e+12>>2];H[e+48>>2]=H[e+8>>2];H[e+52>>2]=c;if(!Db(e+40|0,1,j+8|0)){break n}c=H[e+44>>2];H[e>>2]=H[e+40>>2];H[e+4>>2]=c;c=H[e+76>>2];H[e+32>>2]=H[e+72>>2];H[e+36>>2]=c;c=H[e+68>>2];H[e+24>>2]=H[e+64>>2];H[e+28>>2]=c;c=H[e+60>>2];h=c;d=H[e+56>>2];H[e+16>>2]=d;H[e+20>>2]=c;i=H[e+52>>2];f=i;c=H[e+48>>2];H[e+8>>2]=c;H[e+12>>2]=f;o=c-d|0;k=H[j+12>>2];c=f-((c>>>0>>0)+h|0)|0;i=H[j+8>>2];if((k|0)==(c|0)&o>>>0>>0|c>>>0>>0){break n}c=h+k|0;f=d;d=d+i|0;c=f>>>0>d>>>0?c+1|0:c;H[e+16>>2]=d;H[e+20>>2]=c}o:{if(J[e+38>>1]<=513){c=H[e+4>>2];H[e+96>>2]=H[e>>2];H[e+100>>2]=c;c=H[e+36>>2];H[e+128>>2]=H[e+32>>2];H[e+132>>2]=c;c=H[e+28>>2];H[e+120>>2]=H[e+24>>2];H[e+124>>2]=c;c=H[e+20>>2];H[e+112>>2]=H[e+16>>2];H[e+116>>2]=c;c=H[e+12>>2];H[e+104>>2]=H[e+8>>2];H[e+108>>2]=c;if(!Db(e+96|0,1,j+8|0)){break n}c=H[e+100>>2];H[e>>2]=H[e+96>>2];H[e+4>>2]=c;c=H[e+132>>2];H[e+32>>2]=H[e+128>>2];H[e+36>>2]=c;c=H[e+124>>2];H[e+24>>2]=H[e+120>>2];H[e+28>>2]=c;d=H[e+116>>2];h=d;c=H[e+112>>2];H[e+16>>2]=c;H[e+20>>2]=d;i=H[e+108>>2];f=i;d=H[e+104>>2];H[e+8>>2]=d;H[e+12>>2]=f;o=d-c|0;k=H[j+12>>2];d=f-((c>>>0>d>>>0)+h|0)|0;i=H[j+8>>2];if((k|0)==(d|0)&o>>>0>>0|d>>>0>>0){break n}d=h+k|0;f=c;c=c+i|0;d=f>>>0>c>>>0?d+1|0:d;H[e+16>>2]=c;H[e+20>>2]=d;break o}if(!ta(e+80|0,e)){break m}}if(!Fe(e)){break m}c=H[e+4>>2];H[g>>2]=H[e>>2];H[g+4>>2]=c;c=H[e+36>>2];H[g+32>>2]=H[e+32>>2];H[g+36>>2]=c;c=H[e+28>>2];H[g+24>>2]=H[e+24>>2];H[g+28>>2]=c;c=H[e+20>>2];H[g+16>>2]=H[e+16>>2];H[g+20>>2]=c;c=H[e+12>>2];H[g+8>>2]=H[e+8>>2];H[g+12>>2]=c;c=H[e+144>>2];c=J[(ea[H[H[c>>2]+32>>2]](c)|0)+36>>1];p:{if(((c<<8|c>>>8)&65535)>>>0<=513){c=H[e+144>>2];q:{if(I[(ea[H[H[c>>2]+32>>2]](c)|0)+36|0]<=1){c=H[g+20>>2];i=H[g+16>>2];d=i+4|0;c=d>>>0<4?c+1|0:c;h=d;f=K[g+8>>2]>>0;d=H[g+12>>2];if(f&(d|0)<=(c|0)|(c|0)>(d|0)){break m}d=i+H[g>>2]|0;d=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[g+16>>2]=h;H[g+20>>2]=c;break q}if(!Ea(1,j+8|0,g)){break m}d=H[j+8>>2]}c=H[e+152>>2];if(d>>>0>=c>>>0){break m}d=H[g+20>>2];h=H[g+12>>2];i=H[g+16>>2];if((d|0)>=(h|0)&i>>>0>=K[g+8>>2]|(d|0)>(h|0)){break m}h=I[i+H[g>>2]|0];i=i+1|0;d=i?d:d+1|0;H[g+16>>2]=i;H[g+20>>2]=d;if(h){break m}H[e+176>>2]=2;H[e+180>>2]=7;break p}H[e+176>>2]=2;H[e+180>>2]=7;c=H[e+152>>2]}if((c|0)<0){break m}H[j+8>>2]=0;b=2;h=H[e+156>>2];i=H[e+160>>2]-h>>2;r:{if(i>>>0>>0){Pa(e+156|0,c-i|0,j+8|0);b=H[e+176>>2];d=H[e+180>>2];break r}d=7;if(c>>>0>=i>>>0){break r}H[e+160>>2]=h+(c<<2)}i=e+184|0;b=(d-b|0)+1|0;c=H[e+188>>2];h=H[e+184>>2];d=(c-h|0)/12|0;s:{if(b>>>0>d>>>0){o=0;d=b-d|0;f=H[i+8>>2];c=H[i+4>>2];t:{if(d>>>0<=(f-c|0)/12>>>0){if(d){b=c;c=N(d,12)-12|0;c=(c-((c>>>0)%12|0)|0)+12|0;c=ra(b,0,c)+c|0}H[i+4>>2]=c;break t}u:{v:{w:{h=H[i>>2];k=(c-h|0)/12|0;b=k+d|0;if(b>>>0<357913942){f=(f-h|0)/12|0;l=f<<1;f=f>>>0>=178956970?357913941:b>>>0>>0?l:b;if(f){if(f>>>0>=357913942){break w}o=pa(N(f,12))}b=N(k,12)+o|0;d=N(d,12)-12|0;k=(d-((d>>>0)%12|0)|0)+12|0;d=ra(b,0,k);k=d+k|0;f=N(f,12)+o|0;if((c|0)==(h|0)){break v}while(1){b=b-12|0;c=c-12|0;H[b>>2]=H[c>>2];H[b+4>>2]=H[c+4>>2];H[b+8>>2]=H[c+8>>2];H[c+8>>2]=0;H[c>>2]=0;H[c+4>>2]=0;if((c|0)!=(h|0)){continue}break}H[i+8>>2]=f;d=H[i+4>>2];H[i+4>>2]=k;c=H[i>>2];H[i>>2]=b;if((c|0)==(d|0)){break u}while(1){b=d-12|0;h=H[b>>2];if(h){H[d-8>>2]=h;oa(h)}d=b;if((b|0)!=(c|0)){continue}break}break u}break b}wa();v()}H[i+8>>2]=f;H[i+4>>2]=k;H[i>>2]=d}if(c){oa(c)}}d=H[e+188>>2];break s}if(b>>>0>=d>>>0){d=c;break s}d=h+N(b,12)|0;if((d|0)!=(c|0)){while(1){b=c-12|0;h=H[b>>2];if(h){H[c-8>>2]=h;oa(h)}c=b;if((d|0)!=(b|0)){continue}break}}H[e+188>>2]=d}f=e+196|0;b=H[e+184>>2];c=(d-b|0)/12|0;o=H[e+196>>2];h=H[e+200>>2]-o>>2;x:{if(c>>>0>h>>>0){ya(f,c-h|0);b=H[e+184>>2];d=H[e+188>>2];break x}if(c>>>0>=h>>>0){break x}H[e+200>>2]=o+(c<<2)}if((b|0)==(d|0)){b=1;break m}c=0;while(1){if(!Ea(1,j+8|0,g)){break n}b=H[e+148>>2];d=(H[b+4>>2]-H[b>>2]>>2>>>0)/3|0;b=H[j+8>>2];if(d>>>0>>0){break n}if(b){k=N(c,12);h=k+H[i>>2]|0;d=H[h>>2];o=H[h+4>>2]-d>>2;y:{if(o>>>0>>0){ya(h,b-o|0);d=H[k+H[i>>2]>>2];break y}if(b>>>0>=o>>>0){break y}H[h+4>>2]=(b<<2)+d}kd(b,1,g,d);H[H[f>>2]+(c<<2)>>2]=b}b=1;c=c+1|0;if(c>>>0<(H[e+188>>2]-H[e+184>>2]|0)/12>>>0){continue}break}break m}b=0}ca=j+16|0;z:{if(!b){break z}d=0;c=0;g=0;i=0;o=0;l=ca-96|0;ca=l;H[l+72>>2]=0;H[l+64>>2]=0;H[l+68>>2]=0;H[l+48>>2]=0;H[l+52>>2]=0;H[l+40>>2]=0;H[l+44>>2]=0;H[l+56>>2]=1065353216;H[l+32>>2]=0;H[l+24>>2]=0;H[l+28>>2]=0;j=a;L=H[a+124>>2];A:{B:{C:{D:{E:{if((n|0)<=0){break E}r=j+232|0;P=H[j+216>>2]!=H[j+220>>2];D=1;while(1){h=i;i=h+1|0;a=H[r+172>>2];F:{G:{if((a|0)!=-1){b=H[r+196>>2]+(a<<2)|0;f=H[b>>2];a=f-1|0;H[b>>2]=a;b=9;if((f|0)<=0){break F}a=H[H[H[r+184>>2]+N(H[r+172>>2],12)>>2]+(a<<2)>>2];if(a>>>0>4){break F}b=H[(a<<2)+12144>>2];break G}b=7;a=H[r+144>>2];a=J[(ea[H[H[a>>2]+32>>2]](a)|0)+36>>1];if(((a<<8|a>>>8)&65535)>>>0>513|!I[r+76|0]){break G}b=0;m=H[r- -64>>2];k=H[r+72>>2];a=m+(k>>>3|0)|0;p=H[r+68>>2];if(a>>>0>=p>>>0){break G}f=I[a|0];a=k+1|0;H[r+72>>2]=a;f=f>>>(k&7)&1;if(!f){break G}q=a>>>3|0;b=m+q|0;H:{if(b>>>0>=p>>>0){b=a;a=0;break H}t=I[b|0];b=k+2|0;H[r+72>>2]=b;q=b>>>3|0;a=t>>>(a&7)&1}k=m+q|0;if(k>>>0

>>0){k=I[k|0];H[r+72>>2]=b+1;b=k>>>(b&7)<<1&2}else{b=0}b=(a|b)<<1|f}H[r+168>>2]=b}a=b;I:{J:{if(!a){if((c|0)==(g|0)){b=-1;break D}d=-1;m=H[j+8>>2];t=H[m+24>>2];D=c-4|0;f=H[D>>2];a=-1;K:{if((f|0)==-1){break K}k=f+1|0;k=(k>>>0)%3|0?k:f-2|0;a=-1;if((k|0)==-1){break K}a=H[H[m>>2]+(k<<2)>>2]}b=H[t+(a<<2)>>2];if((b|0)!=-1){d=b+1|0;d=(d>>>0)%3|0?d:b-2|0}if((d|0)==(f|0)){b=-1;break D}if((f|0)!=-1){b=-1;if(H[H[m+12>>2]+(f<<2)>>2]!=-1){break D}}k=H[m+12>>2];if((d|0)!=-1){b=-1;if(H[k+(d<<2)>>2]!=-1){break D}}p=N(h,3);b=p+1|0;H[k+(f<<2)>>2]=b;w=b<<2;H[w+k>>2]=f;q=p+2|0;H[k+(d<<2)>>2]=q;y=q<<2;H[y+k>>2]=d;k=-1;h=-1;L:{if((f|0)==-1){break L}M:{if((f>>>0)%3|0){b=f-1|0;break M}b=f+2|0;h=-1;if((b|0)==-1){break L}}h=H[H[m>>2]+(b<<2)>>2]}N:{if((d|0)==-1){break N}b=d+1|0;b=(b>>>0)%3|0?b:d-2|0;if((b|0)==-1){break N}k=H[H[m>>2]+(b<<2)>>2]}b=-1;if((a|0)==(h|0)|(a|0)==(k|0)){break D}b=H[m>>2];H[b+(p<<2)>>2]=a;H[b+w>>2]=k;H[b+y>>2]=h;if((h|0)!=-1){H[t+(h<<2)>>2]=q}b=H[j+120>>2]+(a>>>3&536870908)|0;d=H[b>>2];Q=b,R=Vj(a)&d,H[Q>>2]=R;H[D>>2]=p;k=H[c-4>>2];break J}b=-1;O:{P:{Q:{R:{S:{T:{U:{V:{W:{switch(a-1|0){case 2:case 4:if((c|0)==(g|0)){break D}t=c-4|0;d=H[t>>2];f=H[j+8>>2];m=H[f+12>>2];if((d|0)!=-1&H[m+(d<<2)>>2]!=-1){break D}k=N(h,3);p=(a|0)==5;q=k+(p?2:1)|0;w=q<<2;H[w+m>>2]=d;H[m+(d<<2)>>2]=q;Ka(f+24|0,11424);a=H[j+8>>2];m=H[a+24>>2];if(H[a+28>>2]-m>>2>(L|0)){break D}a=H[a>>2];y=a+w|0;b=H[f+28>>2];f=H[f+24>>2];w=(b-f>>2)-1|0;H[y>>2]=w;if((b|0)!=(f|0)){H[m+(w<<2)>>2]=q}b=p?k:k+2|0;q=a+(k+p<<2)|0;X:{if((d|0)==-1){H[a+(b<<2)>>2]=-1;b=-1;break X}Y:{Z:{_:{if((d>>>0)%3|0){f=d-1|0;break _}f=d+2|0;if((f|0)==-1){break Z}}f=H[a+(f<<2)>>2];H[a+(b<<2)>>2]=f;if((f|0)==-1){break Y}H[m+(f<<2)>>2]=b;break Y}H[a+(b<<2)>>2]=-1}f=d+1|0;d=(f>>>0)%3|0?f:d-2|0;b=-1;if((d|0)==-1){break X}b=H[a+(d<<2)>>2]}H[q>>2]=b;H[t>>2]=k;break V;case 0:if((c|0)==(d|0)){break D}a=c-4|0;m=H[a>>2];H[l+68>>2]=a;p=H[l+44>>2];$:{if(!p){c=a;break $}f=H[l+40>>2];q=Uj(p)>>>0>1;b=h&p+2147483647;aa:{if(!q){break aa}b=h;if(b>>>0

>>0){break aa}b=(h>>>0)%(p>>>0)|0}k=b;b=H[f+(k<<2)>>2];if(!b){c=a;break $}b=H[b>>2];if(!b){c=a;break $}ba:{if(!q){f=p-1|0;while(1){p=H[b+4>>2];ca:{if((p|0)!=(h|0)){if((k|0)==(f&p)){break ca}c=a;break $}if((h|0)==H[b+8>>2]){break ba}}b=H[b>>2];if(b){continue}break}c=a;break $}while(1){f=H[b+4>>2];da:{if((f|0)!=(h|0)){if(f>>>0>=p>>>0){f=(f>>>0)%(p>>>0)|0}if((f|0)==(k|0)){break da}c=a;break $}if((h|0)==H[b+8>>2]){break ba}}b=H[b>>2];if(b){continue}break}c=a;break $}if((a|0)!=(x|0)){H[a>>2]=H[b+12>>2];H[l+68>>2]=c;break $}a=x-d|0;g=a>>2;c=g+1|0;if(c>>>0>=1073741824){break b}f=a>>>1|0;f=a>>>0>=2147483644?1073741823:c>>>0>>0?f:c;if(f){if(f>>>0>=1073741824){break B}a=pa(f<<2)}else{a=0}g=a+(g<<2)|0;H[g>>2]=H[b+12>>2];c=g+4|0;if((d|0)!=(x|0)){while(1){g=g-4|0;x=x-4|0;H[g>>2]=H[x>>2];if((d|0)!=(x|0)){continue}break}}x=a+(f<<2)|0;H[l+72>>2]=x;H[l+68>>2]=c;H[l+64>>2]=g;if(d){oa(d)}}if((c|0)==(g|0)){break P}w=c-4|0;a=H[w>>2];if((a|0)==(m|0)){break P}b=(a|0)==-1;p=H[j+8>>2];if(!b&H[H[p+12>>2]+(a<<2)>>2]!=-1){break P}q=H[p+12>>2];if((m|0)!=-1&H[q+(m<<2)>>2]!=-1){break P}k=N(h,3);t=k+2|0;H[q+(a<<2)>>2]=t;h=t<<2;H[h+q>>2]=a;d=k+1|0;H[q+(m<<2)>>2]=d;y=d<<2;H[y+q>>2]=m;if(b){break T}if((a>>>0)%3|0){f=a-1|0;break S}f=a+2|0;if((f|0)!=-1){break S}d=H[p>>2];f=-1;break R;case 6:break W;default:break D}}k=H[j+8>>2];Ka(k+24|0,11424);f=H[j+8>>2];a=N(h,3);m=H[k+28>>2];p=H[k+24>>2];q=m-p|0;k=q>>2;t=k-1|0;H[H[f>>2]+(a<<2)>>2]=t;Ka(f+24|0,11424);w=a+1|0;H[H[f>>2]+(w<<2)>>2]=(H[f+28>>2]-H[f+24>>2]>>2)-1;f=H[j+8>>2];Ka(f+24|0,11424);y=a+2|0;H[H[f>>2]+(y<<2)>>2]=(H[f+28>>2]-H[f+24>>2]>>2)-1;E=H[j+8>>2];f=H[E+24>>2];if(H[E+28>>2]-f>>2>(L|0)){break D}ea:{fa:{if((m|0)!=(p|0)){H[f+(t<<2)>>2]=a;b=0;if((q|0)==-4){break fa}}H[f+(k<<2)>>2]=w;b=k+1|0;if((b|0)==-1){break ea}}H[f+(b<<2)>>2]=y}if((c|0)!=(x|0)){H[c>>2]=a;c=c+4|0;H[l+68>>2]=c;break U}b=c-d|0;k=b>>2;g=k+1|0;if(g>>>0>=1073741824){break b}f=b>>>1|0;b=b>>>0>=2147483644?1073741823:g>>>0>>0?f:g;if(b){if(b>>>0>=1073741824){break B}f=pa(b<<2)}else{f=0}g=f+(k<<2)|0;H[g>>2]=a;x=f+(b<<2)|0;a=g+4|0;if((c|0)!=(d|0)){while(1){g=g-4|0;c=c-4|0;H[g>>2]=H[c>>2];if((c|0)!=(d|0)){continue}break}}H[l+72>>2]=x;H[l+68>>2]=a;H[l+64>>2]=g;if(d){oa(d)}c=a}d=g}Ce(r,H[c-4>>2]);a=H[j+40>>2];if((a|0)==H[j+36>>2]){break I}b=a-12|0;f=H[b+4>>2];h=(h^-1)+n|0;if(f>>>0>h>>>0){break P}if((f|0)!=(h|0)){break I}k=I[a-4|0];f=H[b>>2];H[j+40>>2]=b;if((f|0)<0){break P}m=c-4|0;a=H[m>>2];H[l+20>>2]=(f^-1)+n;b=l+20|0;H[l+88>>2]=b;Gb(l,l+40|0,b,l+88|0);f=H[l>>2];ga:{if(k&1){b=-1;if((a|0)==-1){break ga}b=a+1|0;b=(b>>>0)%3|0?b:a-2|0;break ga}b=-1;if((a|0)==-1){break ga}b=a-1|0;if((a>>>0)%3|0){break ga}b=a+2|0}H[f+12>>2]=b;b=H[j+40>>2];if((b|0)==H[j+36>>2]){break I}while(1){a=b-12|0;f=H[a+4>>2];if(f>>>0>h>>>0){break P}if((f|0)!=(h|0)){break I}f=I[b-4|0];b=H[a>>2];H[j+40>>2]=a;if((b|0)<0){break P}a=H[m>>2];H[l+20>>2]=(b^-1)+n;b=l+20|0;H[l+88>>2]=b;Gb(l,l+40|0,b,l+88|0);k=H[l>>2];ha:{if(f&1){b=-1;if((a|0)==-1){break ha}b=a+1|0;b=(b>>>0)%3|0?b:a-2|0;break ha}b=-1;if((a|0)==-1){break ha}b=a-1|0;if((a>>>0)%3|0){break ha}b=a+2|0}H[k+12>>2]=b;b=H[j+40>>2];if((b|0)!=H[j+36>>2]){continue}break}break I}f=-1;d=H[p>>2];H[d+(k<<2)>>2]=-1;b=-1;break Q}d=H[p>>2];f=H[d+(f<<2)>>2]}H[(k<<2)+d>>2]=f;E=a+1|0;a=(E>>>0)%3|0?E:a-2|0;b=-1;if((a|0)==-1){break Q}b=H[(a<<2)+d>>2]}H[d+y>>2]=b;ia:{if((m|0)==-1){H[d+h>>2]=-1;t=-1;a=-1;break ia}ja:{ka:{la:{if((m>>>0)%3|0){b=m-1|0;break la}b=m+2|0;if((b|0)==-1){break ka}}a=H[(b<<2)+d>>2];H[d+h>>2]=a;if((a|0)==-1){break ja}H[H[p+24>>2]+(a<<2)>>2]=t;break ja}H[d+h>>2]=-1}t=-1;b=m+1|0;b=(b>>>0)%3|0?b:m-2|0;a=-1;if((b|0)==-1){break ia}t=H[(b<<2)+d>>2];a=b}b=H[j+388>>2];h=f<<2;m=b+h|0;y=b;b=t<<2;H[m>>2]=H[m>>2]+H[y+b>>2];m=b;b=H[p+24>>2];m=m+b|0;if((f|0)!=-1){H[b+h>>2]=H[m>>2]}b=a;while(1){if((b|0)==-1){break O}H[(b<<2)+d>>2]=f;p=b+1|0;b=(p>>>0)%3|0?p:b-2|0;h=-1;ma:{if((b|0)==-1){break ma}b=H[q+(b<<2)>>2];h=-1;if((b|0)==-1){break ma}h=b+1|0;h=(h>>>0)%3|0?h:b-2|0}b=h;if((a|0)!=(b|0)){continue}break}}b=-1;if(!D){break E}break D}H[m>>2]=-1;na:{if(P){break na}if((z|0)!=(A|0)){H[A>>2]=t;A=A+4|0;H[l+28>>2]=A;break na}a=z-o|0;h=a>>2;b=h+1|0;if(b>>>0>=1073741824){break b}d=a>>>1|0;d=a>>>0>=2147483644?1073741823:b>>>0>>0?d:b;if(d){if(d>>>0>=1073741824){break B}a=pa(d<<2)}else{a=0}b=a+(h<<2)|0;H[b>>2]=t;A=b+4|0;if((o|0)!=(z|0)){while(1){b=b-4|0;z=z-4|0;H[b>>2]=H[z>>2];if((o|0)!=(z|0)){continue}break}}z=a+(d<<2)|0;H[l+32>>2]=z;H[l+28>>2]=A;H[l+24>>2]=b;if(o){oa(o)}o=b}H[w>>2]=k}Ce(r,k);d=g}D=(i|0)<(n|0);if((i|0)!=(n|0)){continue}break}i=n}b=-1;d=H[j+8>>2];if(H[d+28>>2]-H[d+24>>2]>>2>(L|0)){break D}if((c|0)!=(g|0)){x=j+72|0;h=j+60|0;p=j+312|0;while(1){c=c-4|0;o=H[c>>2];H[l+68>>2]=c;oa:{pa:{qa:{if(J[j+270>>1]<=513){if(!I[j+364|0]){break pa}a=H[j+360>>2];b=H[j+352>>2]+(a>>>3|0)|0;if(b>>>0>=K[j+356>>2]){break qa}b=I[b|0];H[j+360>>2]=a+1;if(!(b>>>(a&7)&1)){break qa}break pa}if(Ba(p)){break pa}}b=H[j+64>>2];a=H[j+68>>2];if((b|0)==a<<5){if((b+1|0)<0){break b}if(b>>>0<=1073741822){a=a<<6;b=(b&-32)+32|0;a=a>>>0>b>>>0?a:b}else{a=2147483647}pb(h,a);b=H[j+64>>2]}H[j+64>>2]=b+1;a=H[j+60>>2]+(b>>>3&536870908)|0;d=H[a>>2];Q=a,R=Vj(b)&d,H[Q>>2]=R;b=H[j+76>>2];if((b|0)!=H[j+80>>2]){H[b>>2]=o;H[j+76>>2]=b+4;break oa}d=H[x>>2];a=b-d|0;k=a>>2;f=k+1|0;if(f>>>0<1073741824){n=a>>>1|0;n=a>>>0>=2147483644?1073741823:f>>>0>>0?n:f;if(n){if(n>>>0>=1073741824){break B}a=pa(n<<2)}else{a=0}f=a+(k<<2)|0;H[f>>2]=o;o=f+4|0;if((b|0)!=(d|0)){while(1){f=f-4|0;b=b-4|0;H[f>>2]=H[b>>2];if((b|0)!=(d|0)){continue}break}}H[j+80>>2]=a+(n<<2);H[j+76>>2]=o;H[j+72>>2]=f;if(!d){break oa}oa(d);break oa}break b}m=H[j+8>>2];r=H[m>>2];if(((H[m+4>>2]-r>>2>>>0)/3|0)<=(i|0)){b=-1;break D}d=-1;q=H[m+24>>2];n=-1;ra:{if((o|0)==-1){break ra}g=o+1|0;g=(g>>>0)%3|0?g:o-2|0;n=-1;if((g|0)==-1){break ra}n=H[r+(g<<2)>>2]}a=H[q+(n<<2)>>2];sa:{if((a|0)==-1){k=1;f=-1;break sa}k=1;f=-1;b=a+1|0;a=(b>>>0)%3|0?b:a-2|0;if((a|0)==-1){break sa}k=0;d=a;b=a+1|0;b=(b>>>0)%3|0?b:a-2|0;if((b|0)!=-1){f=H[r+(b<<2)>>2]}}b=-1;g=-1;a=H[q+(f<<2)>>2];if((a|0)!=-1){g=a+1|0;g=(g>>>0)%3|0?g:a-2|0}if((d|0)==(o|0)|(g|0)==(o|0)|((o|0)!=-1&H[H[m+12>>2]+(o<<2)>>2]!=-1|(d|0)==(g|0))){break D}if(!k&H[H[m+12>>2]+(d<<2)>>2]!=-1){break D}k=-1;a=H[m+12>>2];m=-1;ta:{if((g|0)==-1){break ta}if(H[a+(g<<2)>>2]!=-1){break D}b=g+1|0;b=(b>>>0)%3|0?b:g-2|0;m=-1;if((b|0)==-1){break ta}m=H[r+(b<<2)>>2]}b=N(i,3);H[l>>2]=b;H[a+(b<<2)>>2]=o;H[a+(o<<2)>>2]=b;b=H[l>>2]+1|0;H[a+(b<<2)>>2]=d;H[a+(d<<2)>>2]=b;b=H[l>>2]+2|0;H[a+(b<<2)>>2]=g;H[a+(g<<2)>>2]=b;a=H[l>>2];H[r+(a<<2)>>2]=f;b=a+1|0;d=r+(b<<2)|0;H[d>>2]=m;g=a+2|0;o=r+(g<<2)|0;H[o>>2]=n;a=H[j+120>>2];f=b?f:-1;n=a+(f>>>3&536870908)|0;r=H[n>>2];Q=n,R=Vj(f)&r,H[Q>>2]=R;k=(b|0)!=-1?H[d>>2]:k;b=a+(k>>>3&536870908)|0;d=H[b>>2];Q=b,R=Vj(k)&d,H[Q>>2]=R;b=-1;b=(g|0)!=-1?H[o>>2]:b;a=a+(b>>>3&536870908)|0;d=H[a>>2];Q=a,R=Vj(b)&d,H[Q>>2]=R;F[l+88|0]=1;_c(h,l+88|0);Ka(x,l);i=i+1|0;g=H[l+64>>2]}if((c|0)!=(g|0)){continue}break}d=H[j+8>>2]}b=-1;if(((H[d+4>>2]-H[d>>2]>>2>>>0)/3|0)!=(i|0)){break D}b=H[d+28>>2]-H[d+24>>2]>>2;i=H[l+24>>2];f=H[l+28>>2];if((i|0)==(f|0)){break C}while(1){a=H[i>>2];h=H[d+24>>2];c=b-1|0;g=h+(c<<2)|0;if(H[g>>2]==-1){while(1){c=b-2|0;b=b-1|0;g=h+(c<<2)|0;if(H[g>>2]==-1){continue}break}}if(a>>>0<=c>>>0){H[l>>2]=d;g=H[g>>2];F[l+12|0]=1;H[l+8>>2]=g;H[l+4>>2]=g;if((g|0)!=-1){while(1){d=H[H[j+8>>2]>>2]+(g<<2)|0;if(H[d>>2]!=(c|0)){b=-1;break D}H[d>>2]=a;uc(l);g=H[l+8>>2];if((g|0)!=-1){continue}break}d=H[j+8>>2]}h=H[d+24>>2];g=h+(c<<2)|0;if((a|0)!=-1){H[h+(a<<2)>>2]=H[g>>2]}H[g>>2]=-1;g=1<>2];a=h+(a>>>3&536870908)|0;h=h+(c>>>3&536870908)|0;c=1<>2]&c){g=g|H[a>>2]}else{g=H[a>>2]&(g^-1)}H[a>>2]=g;H[h>>2]=H[h>>2]&(c^-1);b=b-1|0}i=i+4|0;if((f|0)!=(i|0)){continue}break}}i=H[l+24>>2]}if(i){oa(i)}a=H[l+48>>2];if(a){while(1){c=H[a>>2];oa(a);a=c;if(a){continue}break}}a=H[l+40>>2];H[l+40>>2]=0;if(a){oa(a)}a=H[l+64>>2];if(a){H[l+68>>2]=a;oa(a)}ca=l+96|0;break A}wa();v()}if((b|0)==-1){break z}a=O;c=H[a+16>>2];d=c+H[a>>2]|0;c=H[a+8>>2]-c|0;a=H[H[j+4>>2]+32>>2];G[a+38>>1]=J[a+38>>1];H[a>>2]=d;H[a+16>>2]=0;H[a+20>>2]=0;H[a+8>>2]=c;H[a+12>>2]=0;a=H[j+4>>2];c=J[a+36>>1];g=c<<8|c>>>8;if((g&65535)>>>0<=513){a=H[a+32>>2];c=H[a+16>>2];d=M+H[a+20>>2]|0;c=c+C|0;d=c>>>0>>0?d+1|0:d;H[a+16>>2]=c;H[a+20>>2]=d}ua:{if(H[j+216>>2]==H[j+220>>2]){break ua}c=H[j+8>>2];a=H[c>>2];c=H[c+4>>2];va:{if((g&65535)>>>0>=513){if((a|0)==(c|0)){break ua}c=0;break va}if((a|0)==(c|0)){break ua}c=0;while(1){if(cd(j,c)){c=c+3|0;a=H[j+8>>2];if(c>>>0>2]-H[a>>2]>>2>>>0){continue}break ua}break}break z}while(1){if(bd(j,c)){c=c+3|0;a=H[j+8>>2];if(c>>>0>2]-H[a>>2]>>2>>>0){continue}break ua}break}break z}ad(e);c=H[j+216>>2];if((c|0)!=H[j+220>>2]){n=0;while(1){d=N(n,144);Jc((d+c|0)+4|0,H[j+8>>2]);a=H[B>>2];e=a+d|0;c=H[e+132>>2];e=H[e+136>>2];if((c|0)!=(e|0)){while(1){Hc((d+H[B>>2]|0)+4|0,H[c>>2]);c=c+4|0;if((e|0)!=(c|0)){continue}break}a=H[B>>2]}if(!Ic((a+d|0)+4|0)){break z}n=n+1|0;c=H[j+216>>2];if(n>>>0<(H[j+220>>2]-c|0)/144>>>0){continue}break}}a=H[j+8>>2];Hb(j+184|0,H[a+28>>2]-H[a+24>>2]>>2);u=H[j+216>>2];if((u|0)!=H[j+220>>2]){c=0;while(1){a=N(c,144)+u|0;d=H[a+60>>2]-H[a+56>>2]>>2;f=a+104|0;a=H[j+8>>2];a=H[a+28>>2]-H[a+24>>2]>>2;Hb(f,(a|0)<(d|0)?d:a);c=c+1|0;u=H[j+216>>2];if(c>>>0<(H[j+220>>2]-u|0)/144>>>0){continue}break}}u=$c(j,b)}break c}u=0}ca=s- -64|0;return u|0}sa();v()}function ii(a){a=a|0;var b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0,x=0,y=0,z=0,A=0,B=0,C=0,D=0,E=0,L=0,M=0,O=0,P=0,Q=0,R=0,S=0;u=ca+-64|0;ca=u;H[a+132>>2]=0;if(H[a+148>>2]){c=H[a+144>>2];if(c){while(1){b=H[c>>2];oa(c);c=b;if(b){continue}break}}c=0;H[a+144>>2]=0;l=H[a+140>>2];a:{if(!l){break a}if(l>>>0>=4){b=l&-4;while(1){e=c<<2;H[e+H[a+136>>2]>>2]=0;H[H[a+136>>2]+(e|4)>>2]=0;H[H[a+136>>2]+(e|8)>>2]=0;H[H[a+136>>2]+(e|12)>>2]=0;c=c+4|0;f=f+4|0;if((b|0)!=(f|0)){continue}break}}b=l&3;if(!b){break a}while(1){H[H[a+136>>2]+(c<<2)>>2]=0;c=c+1|0;w=w+1|0;if((b|0)!=(w|0)){continue}break}}H[a+148>>2]=0}b:{c:{c=H[a+4>>2];w=I[c+36|0];b=w<<8|I[c+37|0];if(b>>>0<=513){g=H[c+32>>2];d:{if(b>>>0<=511){f=H[g+20>>2];l=H[g+16>>2];e=l+4|0;f=e>>>0<4?f+1|0:f;b=f;d=H[g+12>>2];if(K[g+8>>2]>>0&(b|0)>=(d|0)|(b|0)>(d|0)){break c}f=l+H[g>>2]|0;f=I[f|0]|I[f+1|0]<<8|(I[f+2|0]<<16|I[f+3|0]<<24);H[g+16>>2]=e;H[g+20>>2]=b;break d}if(!Ea(1,u,g)){break c}c=H[a+4>>2];w=I[c+36|0];f=H[u>>2]}H[a+132>>2]=f}d=H[c+32>>2];e:{f:{g:{if((w&255)>>>0<=1){w=0;b=H[d+20>>2];e=H[d+16>>2];f=e+4|0;b=f>>>0<4?b+1|0:b;l=H[d+12>>2];if(K[d+8>>2]>>0&(l|0)<=(b|0)|(b|0)>(l|0)){break b}e=e+H[d>>2]|0;e=I[e|0]|I[e+1|0]<<8|(I[e+2|0]<<16|I[e+3|0]<<24);H[u+60>>2]=e;H[d+16>>2]=f;H[d+20>>2]=b;H[a+156>>2]=e;t=a+156|0;break g}w=0;if(!Ea(1,u+60|0,d)){break b}c=H[a+4>>2];b=I[c+36|0];H[a+156>>2]=H[u+60>>2];t=a+156|0;if(b>>>0>1){break f}}d=H[c+32>>2];e=H[d+8>>2];l=H[d+12>>2];c=H[d+20>>2];f=H[d+16>>2];b=f+4|0;c=b>>>0<4?c+1|0:c;if(b>>>0>e>>>0&(c|0)>=(l|0)|(c|0)>(l|0)){break b}f=f+H[d>>2]|0;f=I[f|0]|I[f+1|0]<<8|(I[f+2|0]<<16|I[f+3|0]<<24);H[u+56>>2]=f;H[d+16>>2]=b;H[d+20>>2]=c;break e}if(!Ea(1,u+56|0,H[c+32>>2])){break b}f=H[u+56>>2]}if(f>>>0>1431655765|K[t>>2]>N(f,3)>>>0){break b}E=H[a+4>>2];x=H[E+32>>2];c=H[x+8>>2];d=H[x+12>>2];b=H[x+20>>2];h=H[x+16>>2];if((d|0)<=(b|0)&h>>>0>=c>>>0|(b|0)>(d|0)){break b}j=H[x>>2];k=I[j+h|0];e=x;l=h+1|0;g=l?b:b+1|0;H[e+16>>2]=l;H[e+20>>2]=g;h:{if(I[E+36|0]<=1){e=c;c=h+5|0;b=c>>>0<5?b+1|0:b;if(c>>>0>e>>>0&(b|0)>=(d|0)|(b|0)>(d|0)){break b}e=j+l|0;t=I[e|0]|I[e+1|0]<<8|(I[e+2|0]<<16|I[e+3|0]<<24);H[u+52>>2]=t;H[x+16>>2]=c;H[x+20>>2]=b;break h}if(!Ea(1,u+52|0,x)){break b}t=H[u+52>>2]}if(f>>>0>>0|((t>>>0)/3|0)+t>>>0>>0){break b}c=H[a+4>>2];d=H[c+32>>2];i:{if(I[c+36|0]<=1){c=H[d+20>>2];b=H[d+16>>2];e=b+4|0;c=e>>>0<4?c+1|0:c;l=H[d+12>>2];if(K[d+8>>2]>>0&(l|0)<=(c|0)|(c|0)>(l|0)){break b}b=b+H[d>>2]|0;b=I[b|0]|I[b+1|0]<<8|(I[b+2|0]<<16|I[b+3|0]<<24);H[u+48>>2]=b;H[d+16>>2]=e;H[d+20>>2]=c;break i}if(!Ea(1,u+48|0,d)){break b}b=H[u+48>>2]}if(b>>>0>t>>>0){break b}H[a+28>>2]=H[a+24>>2];c=$b(pa(88));e=H[a+8>>2];H[a+8>>2]=c;if(e){cb(e);if(!H[a+8>>2]){break b}}H[a+164>>2]=H[a+160>>2];Jb(a+160|0,f);H[a+176>>2]=H[a+172>>2];Jb(a+172|0,f);H[a- -64>>2]=0;H[a+92>>2]=-1;H[a+84>>2]=-1;H[a+88>>2]=-1;H[a+40>>2]=H[a+36>>2];H[a+52>>2]=H[a+48>>2];H[a+76>>2]=H[a+72>>2];M=a+216|0;ed(M);dd(M,k);if(!Lc(H[a+8>>2],f,H[a+156>>2]+b|0)){break b}c=H[a+156>>2];F[u|0]=1;Oa(a+120|0,b+c|0,u);f=H[a+4>>2];c=J[f+36>>1];c=(c<<8|c>>>8)&65535;j:{if(c>>>0<=513){g=H[f+32>>2];k:{if(c>>>0<=511){f=H[g+20>>2];l=H[g+16>>2];e=l+4|0;f=e>>>0<4?f+1|0:f;c=f;d=H[g+12>>2];if(K[g+8>>2]>>0&(c|0)>=(d|0)|(c|0)>(d|0)){break b}f=l+H[g>>2]|0;f=I[f|0]|I[f+1|0]<<8|(I[f+2|0]<<16|I[f+3|0]<<24);H[g+16>>2]=e;H[g+20>>2]=c;break k}if(!Ea(1,u+44|0,g)){break b}f=H[u+44>>2]}if(!f){break b}d=H[H[a+4>>2]+32>>2];l=H[d+8>>2];c=H[d+16>>2];e=l-c|0;c=H[d+12>>2]-(H[d+20>>2]+(c>>>0>l>>>0)|0)|0;if((c|0)<=0&f>>>0>e>>>0|(c|0)<0){break b}g=Ha(u);d=H[H[a+4>>2]+32>>2];l=H[d+16>>2];e=(l+H[d>>2]|0)+f|0;c=H[d+8>>2]-l|0;G[g+38>>1]=J[d+38>>1];H[g>>2]=e;H[g+16>>2]=0;H[g+20>>2]=0;H[g+8>>2]=c-f;H[g+12>>2]=0;c=Ib(a,g);if((c|0)==-1){break b}E=c;P=c>>31;break j}E=-1;P=-1;if((Ib(a,H[f+32>>2])|0)==-1){break b}}B=a+232|0;Ee(B,a);H[a+372>>2]=k;H[a+384>>2]=H[a+156>>2]+b;x=Ha(u);g=x;d=0;l=ca-16|0;ca=l;l:{if(!Ge(B,g)){break l}b=H[g+20>>2];f=H[g+16>>2];c=f+4|0;b=c>>>0<4?b+1|0:b;e=H[g+12>>2];if(K[g+8>>2]>>0&(e|0)<=(b|0)|(b|0)>(e|0)){break l}f=f+H[g>>2]|0;f=I[f|0]|I[f+1|0]<<8|(I[f+2|0]<<16|I[f+3|0]<<24);H[g+16>>2]=c;H[g+20>>2]=b;if((f|0)<0){break l}b=f;f=H[B+152>>2];if((b|0)>=(f|0)){break l}H[l+12>>2]=0;c=H[B+156>>2];b=H[B+160>>2]-c>>2;m:{if(b>>>0>>0){Pa(B+156|0,f-b|0,l+12|0);break m}if(b>>>0<=f>>>0){break m}H[B+160>>2]=c+(f<<2)}d=ta(B+168|0,g)}ca=l+16|0;n:{if(!d){break n}d=0;c=0;f=0;l=0;i=ca-96|0;ca=i;H[i+72>>2]=0;H[i+64>>2]=0;H[i+68>>2]=0;H[i+48>>2]=0;H[i+52>>2]=0;H[i+40>>2]=0;H[i+44>>2]=0;H[i+56>>2]=1065353216;H[i+32>>2]=0;H[i+24>>2]=0;H[i+28>>2]=0;g=a;O=H[a+124>>2];o:{p:{q:{r:{s:{t:{if((t|0)<=0){break t}z=g+400|0;Q=g+232|0;C=H[g+216>>2]!=H[g+220>>2];y=1;while(1){e=l;l=e+1|0;u:{v:{w:{x:{y:{if(H[g+420>>2]!=-1){if(Ba(z)){break y}}if(!I[g+308|0]){break x}z:{o=H[g+296>>2];r=H[g+304>>2];a=o+(r>>>3|0)|0;k=H[g+300>>2];if(a>>>0>=k>>>0){break z}b=I[a|0];a=r+1|0;H[g+304>>2]=a;h=b>>>(r&7)&1;if(!h){break z}n=a>>>3|0;b=o+n|0;A:{if(b>>>0>=k>>>0){b=a;a=0;break A}j=I[b|0];b=r+2|0;H[g+304>>2]=b;n=b>>>3|0;a=j>>>(a&7)&1}j=n+o|0;if(j>>>0>>0){j=I[j|0];H[g+304>>2]=b+1;b=j>>>(b&7)<<1&2}else{b=0}p=(a|b)<<1|h;H[g+416>>2]=p;break w}H[g+416>>2]=0;break x}p=H[g+420>>2];H[g+416>>2]=p;if(p){break w}}if((c|0)==(f|0)){b=-1;break s}p=-1;n=H[g+8>>2];o=H[n+24>>2];j=c-4|0;m=H[j>>2];d=-1;B:{if((m|0)==-1){break B}b=m+1|0;b=(b>>>0)%3|0?b:m-2|0;d=-1;if((b|0)==-1){break B}d=H[H[n>>2]+(b<<2)>>2]}b=H[o+(d<<2)>>2];if((b|0)!=-1){a=b+1|0;p=(a>>>0)%3|0?a:b-2|0}if((m|0)==(p|0)){b=-1;break s}if((m|0)!=-1){b=-1;if(H[H[n+12>>2]+(m<<2)>>2]!=-1){break s}}k=H[n+12>>2];if((p|0)!=-1){b=-1;if(H[k+(p<<2)>>2]!=-1){break s}}q=N(e,3);a=q+1|0;H[k+(m<<2)>>2]=a;h=a<<2;H[h+k>>2]=m;r=q+2|0;H[k+(p<<2)>>2]=r;e=r<<2;H[e+k>>2]=p;k=-1;a=-1;C:{if((m|0)==-1){break C}D:{if((m>>>0)%3|0){b=m-1|0;break D}b=m+2|0;a=-1;if((b|0)==-1){break C}}a=H[H[n>>2]+(b<<2)>>2]}E:{if((p|0)==-1){break E}b=p+1|0;b=(b>>>0)%3|0?b:p-2|0;if((b|0)==-1){break E}k=H[H[n>>2]+(b<<2)>>2]}b=-1;if((a|0)==(d|0)|(d|0)==(k|0)){break s}b=H[n>>2];H[b+(q<<2)>>2]=d;H[b+h>>2]=k;H[b+e>>2]=a;if((a|0)!=-1){H[o+(a<<2)>>2]=r}b=H[g+120>>2]+(d>>>3&536870908)|0;a=H[b>>2];R=b,S=Vj(d)&a,H[R>>2]=S;H[j>>2]=q;p=H[c-4>>2];break v}b=-1;F:{G:{H:{I:{J:{K:{L:{M:{N:{O:{P:{switch(p-1|0){case 2:case 4:if((c|0)==(f|0)){break s}h=c-4|0;m=H[h>>2];r=H[g+8>>2];d=H[r+12>>2];if((m|0)!=-1&H[d+(m<<2)>>2]!=-1){break s}q=N(e,3);k=(p|0)==5;j=q+(k?2:1)|0;a=j<<2;H[a+d>>2]=m;H[d+(m<<2)>>2]=j;Ka(r+24|0,11424);d=H[g+8>>2];o=H[d+24>>2];if(H[d+28>>2]-o>>2>(O|0)){break s}n=H[d>>2];p=n+a|0;d=H[r+28>>2];b=H[r+24>>2];a=(d-b>>2)-1|0;H[p>>2]=a;if((b|0)!=(d|0)){H[o+(a<<2)>>2]=j}d=k?q:q+2|0;j=n+(k+q<<2)|0;Q:{if((m|0)==-1){H[n+(d<<2)>>2]=-1;b=-1;break Q}R:{S:{T:{if((m>>>0)%3|0){a=m-1|0;break T}a=m+2|0;if((a|0)==-1){break S}}a=H[n+(a<<2)>>2];H[n+(d<<2)>>2]=a;if((a|0)==-1){break R}H[o+(a<<2)>>2]=d;break R}H[n+(d<<2)>>2]=-1}a=m+1|0;a=(a>>>0)%3|0?a:m-2|0;b=-1;if((a|0)==-1){break Q}b=H[n+(a<<2)>>2]}H[j>>2]=b;H[h>>2]=q;break O;case 0:if((c|0)==(d|0)){break s}a=c-4|0;m=H[a>>2];H[i+68>>2]=a;k=H[i+44>>2];U:{if(!k){c=a;break U}o=H[i+40>>2];h=Uj(k)>>>0>1;b=e&k+2147483647;V:{if(!h){break V}b=e;if(b>>>0>>0){break V}b=(e>>>0)%(k>>>0)|0}j=b;b=H[o+(j<<2)>>2];if(!b){c=a;break U}b=H[b>>2];if(!b){c=a;break U}W:{if(!h){k=k-1|0;while(1){h=H[b+4>>2];X:{if((h|0)!=(e|0)){if((j|0)==(h&k)){break X}c=a;break U}if((e|0)==H[b+8>>2]){break W}}b=H[b>>2];if(b){continue}break}c=a;break U}while(1){h=H[b+4>>2];Y:{if((h|0)!=(e|0)){if(h>>>0>=k>>>0){h=(h>>>0)%(k>>>0)|0}if((h|0)==(j|0)){break Y}c=a;break U}if((e|0)==H[b+8>>2]){break W}}b=H[b>>2];if(b){continue}break}c=a;break U}if((a|0)!=(A|0)){H[a>>2]=H[b+12>>2];H[i+68>>2]=c;break U}h=A-d|0;c=h>>2;f=c+1|0;if(f>>>0>=1073741824){break M}a=h>>>1|0;h=h>>>0>=2147483644?1073741823:a>>>0>f>>>0?a:f;if(h){if(h>>>0>=1073741824){break p}a=pa(h<<2)}else{a=0}f=a+(c<<2)|0;H[f>>2]=H[b+12>>2];c=f+4|0;if((d|0)!=(A|0)){while(1){f=f-4|0;A=A-4|0;H[f>>2]=H[A>>2];if((d|0)!=(A|0)){continue}break}}A=a+(h<<2)|0;H[i+72>>2]=A;H[i+68>>2]=c;H[i+64>>2]=f;if(d){oa(d)}}if((c|0)==(f|0)){break G}j=c-4|0;n=H[j>>2];if((n|0)==(m|0)){break G}d=(n|0)==-1;q=H[g+8>>2];if(!d&H[H[q+12>>2]+(n<<2)>>2]!=-1){break G}r=H[q+12>>2];if((m|0)!=-1&H[r+(m<<2)>>2]!=-1){break G}p=N(e,3);e=p+2|0;H[r+(n<<2)>>2]=e;o=e<<2;H[o+r>>2]=n;a=p+1|0;H[r+(m<<2)>>2]=a;b=a<<2;H[b+r>>2]=m;if(d){break L}if((n>>>0)%3|0){k=n-1|0;break J}k=n+2|0;if((k|0)!=-1){break J}d=H[q>>2];a=-1;break I;case 6:break P;default:break s}}a=H[g+8>>2];Ka(a+24|0,11424);h=H[g+8>>2];p=N(e,3);q=H[a+28>>2];r=H[a+24>>2];o=q-r|0;n=o>>2;k=n-1|0;H[H[h>>2]+(p<<2)>>2]=k;Ka(h+24|0,11424);j=p+1|0;H[H[h>>2]+(j<<2)>>2]=(H[h+28>>2]-H[h+24>>2]>>2)-1;a=H[g+8>>2];Ka(a+24|0,11424);h=p+2|0;H[H[a>>2]+(h<<2)>>2]=(H[a+28>>2]-H[a+24>>2]>>2)-1;a=H[g+8>>2];m=H[a+24>>2];if(H[a+28>>2]-m>>2>(O|0)){break s}Z:{_:{if((q|0)!=(r|0)){H[m+(k<<2)>>2]=p;b=0;if((o|0)==-4){break _}}H[m+(n<<2)>>2]=j;b=n+1|0;if((b|0)==-1){break Z}}H[m+(b<<2)>>2]=h}if((c|0)!=(A|0)){H[c>>2]=p;c=c+4|0;H[i+68>>2]=c;break N}h=c-d|0;b=h>>2;f=b+1|0;if(f>>>0>=1073741824){break K}a=h>>>1|0;h=h>>>0>=2147483644?1073741823:a>>>0>f>>>0?a:f;if(h){if(h>>>0>=1073741824){break p}a=pa(h<<2)}else{a=0}f=a+(b<<2)|0;H[f>>2]=p;A=a+(h<<2)|0;a=f+4|0;if((c|0)!=(d|0)){while(1){f=f-4|0;c=c-4|0;H[f>>2]=H[c>>2];if((c|0)!=(d|0)){continue}break}}H[i+72>>2]=A;H[i+68>>2]=a;H[i+64>>2]=f;if(d){oa(d)}c=a}d=f}De(Q,H[c-4>>2]);h=H[g+40>>2];if((h|0)==H[g+36>>2]){break u}b=h-12|0;a=H[b+4>>2];k=(e^-1)+t|0;if(a>>>0>k>>>0){break G}if((a|0)!=(k|0)){break u}e=I[h-4|0];a=H[b>>2];H[g+40>>2]=b;if((a|0)<0){break G}h=c-4|0;j=H[h>>2];H[i+20>>2]=(a^-1)+t;a=i+20|0;H[i+88>>2]=a;Gb(i,i+40|0,a,i+88|0);b=H[i>>2];$:{if(e&1){a=-1;if((j|0)==-1){break $}a=j+1|0;a=(a>>>0)%3|0?a:j-2|0;break $}a=-1;if((j|0)==-1){break $}a=j-1|0;if((j>>>0)%3|0){break $}a=j+2|0}H[b+12>>2]=a;b=H[g+40>>2];if((b|0)==H[g+36>>2]){break u}while(1){j=b-12|0;a=H[j+4>>2];if(a>>>0>k>>>0){break G}if((a|0)!=(k|0)){break u}e=I[b-4|0];a=H[j>>2];H[g+40>>2]=j;if((a|0)<0){break G}j=H[h>>2];H[i+20>>2]=(a^-1)+t;a=i+20|0;H[i+88>>2]=a;Gb(i,i+40|0,a,i+88|0);b=H[i>>2];aa:{if(e&1){a=-1;if((j|0)==-1){break aa}a=j+1|0;a=(a>>>0)%3|0?a:j-2|0;break aa}a=-1;if((j|0)==-1){break aa}a=j-1|0;if((j>>>0)%3|0){break aa}a=j+2|0}H[b+12>>2]=a;b=H[g+40>>2];if((b|0)!=H[g+36>>2]){continue}break}break u}sa();v()}k=-1;d=H[q>>2];H[d+(p<<2)>>2]=-1;h=-1;break H}sa();v()}d=H[q>>2];a=H[d+(k<<2)>>2]}k=a;H[(p<<2)+d>>2]=a;a=n+1|0;a=(a>>>0)%3|0?a:n-2|0;h=-1;if((a|0)==-1){break H}h=H[(a<<2)+d>>2]}H[b+d>>2]=h;ba:{if((m|0)==-1){H[d+o>>2]=-1;n=-1;a=-1;break ba}ca:{da:{ea:{if((m>>>0)%3|0){b=m-1|0;break ea}b=m+2|0;if((b|0)==-1){break da}}a=H[(b<<2)+d>>2];H[d+o>>2]=a;if((a|0)==-1){break ca}H[H[q+24>>2]+(a<<2)>>2]=e;break ca}H[d+o>>2]=-1}n=-1;b=m+1|0;b=(b>>>0)%3|0?b:m-2|0;a=-1;if((b|0)==-1){break ba}n=H[(b<<2)+d>>2];a=b}h=H[g+388>>2];e=k<<2;b=h+e|0;o=b;m=H[b>>2];b=n<<2;H[o>>2]=m+H[b+h>>2];h=b;b=H[q+24>>2];o=h+b|0;if((k|0)!=-1){H[b+e>>2]=H[o>>2]}b=a;while(1){if((b|0)==-1){break F}H[(b<<2)+d>>2]=k;h=b+1|0;b=(h>>>0)%3|0?h:b-2|0;e=-1;fa:{if((b|0)==-1){break fa}h=H[r+(b<<2)>>2];e=-1;if((h|0)==-1){break fa}b=h+1|0;e=(b>>>0)%3|0?b:h-2|0}b=e;if((a|0)!=(b|0)){continue}break}}b=-1;if(!(y&1)){break t}break s}H[o>>2]=-1;ga:{if(C){break ga}if((D|0)!=(L|0)){H[L>>2]=n;L=L+4|0;H[i+28>>2]=L;break ga}d=D-s|0;b=d>>2;e=b+1|0;if(e>>>0>=1073741824){break q}a=d>>>1|0;e=d>>>0>=2147483644?1073741823:a>>>0>e>>>0?a:e;if(e){if(e>>>0>=1073741824){break p}a=pa(e<<2)}else{a=0}b=a+(b<<2)|0;H[b>>2]=n;L=b+4|0;if((s|0)!=(D|0)){while(1){b=b-4|0;D=D-4|0;H[b>>2]=H[D>>2];if((s|0)!=(D|0)){continue}break}}D=a+(e<<2)|0;H[i+32>>2]=D;H[i+28>>2]=L;H[i+24>>2]=b;if(s){oa(s)}s=b}H[j>>2]=p}De(Q,p);d=f}y=(l|0)<(t|0);if((l|0)!=(t|0)){continue}break}l=t}b=-1;y=H[g+8>>2];if(H[y+28>>2]-H[y+24>>2]>>2>(O|0)){break s}if((c|0)!=(f|0)){r=g+72|0;j=g+60|0;t=g+312|0;while(1){c=c-4|0;z=H[c>>2];H[i+68>>2]=c;ha:{ia:{ja:{if(J[g+270>>1]<=513){if(!I[g+364|0]){break ia}b=H[g+360>>2];a=H[g+352>>2]+(b>>>3|0)|0;if(a>>>0>=K[g+356>>2]){break ja}a=I[a|0];H[g+360>>2]=b+1;if(!(a>>>(b&7)&1)){break ja}break ia}if(Ba(t)){break ia}}ka:{la:{b=H[g+64>>2];e=H[g+68>>2];if((b|0)==e<<5){if((b+1|0)<0){break la}if(b>>>0<=1073741822){e=e<<6;b=(b&-32)+32|0;a=b>>>0>>0?e:b}else{a=2147483647}pb(j,a);b=H[g+64>>2]}H[g+64>>2]=b+1;e=H[g+60>>2]+(b>>>3&536870908)|0;a=H[e>>2];R=e,S=Vj(b)&a,H[R>>2]=S;b=H[g+76>>2];if((b|0)!=H[g+80>>2]){H[b>>2]=z;H[g+76>>2]=b+4;break ha}s=H[r>>2];h=b-s|0;e=h>>2;d=e+1|0;if(d>>>0>=1073741824){break ka}a=h>>>1|0;h=h>>>0>=2147483644?1073741823:a>>>0>d>>>0?a:d;if(h){if(h>>>0>=1073741824){break p}a=pa(h<<2)}else{a=0}d=a+(e<<2)|0;H[d>>2]=z;e=d+4|0;if((b|0)!=(s|0)){while(1){d=d-4|0;b=b-4|0;H[d>>2]=H[b>>2];if((b|0)!=(s|0)){continue}break}}H[g+80>>2]=a+(h<<2);H[g+76>>2]=e;H[g+72>>2]=d;if(!s){break ha}oa(s);break ha}sa();v()}sa();v()}q=H[g+8>>2];C=H[q>>2];if(((H[q+4>>2]-C>>2>>>0)/3|0)<=(l|0)){b=-1;break s}f=-1;b=-1;d=-1;s=H[q+24>>2];e=-1;ma:{if((z|0)==-1){break ma}a=z+1|0;a=(a>>>0)%3|0?a:z-2|0;e=-1;if((a|0)==-1){break ma}e=H[C+(a<<2)>>2]}o=H[s+(e<<2)>>2];na:{if((o|0)==-1){k=1;a=-1;break na}k=1;h=o+1|0;h=(h>>>0)%3|0?h:o-2|0;a=-1;if((h|0)==-1){break na}k=0;a=h+1|0;f=h;a=(a>>>0)%3|0?a:f-2|0;if((a|0)!=-1){a=H[C+(a<<2)>>2]}else{a=-1}}h=H[(a<<2)+s>>2];if((h|0)!=-1){d=h+1|0;d=(d>>>0)%3|0?d:h-2|0}if((f|0)==(z|0)|(d|0)==(z|0)|((z|0)!=-1&H[H[q+12>>2]+(z<<2)>>2]!=-1|(d|0)==(f|0))){break s}if(!k&H[H[q+12>>2]+(f<<2)>>2]!=-1){break s}k=-1;s=H[q+12>>2];h=-1;oa:{if((d|0)==-1){break oa}if(H[s+(d<<2)>>2]!=-1){break s}b=d+1|0;b=(b>>>0)%3|0?b:d-2|0;h=-1;if((b|0)==-1){break oa}h=H[C+(b<<2)>>2]}b=N(l,3);H[i>>2]=b;H[s+(b<<2)>>2]=z;H[s+(z<<2)>>2]=b;b=H[i>>2]+1|0;H[s+(b<<2)>>2]=f;H[s+(f<<2)>>2]=b;b=H[i>>2]+2|0;H[s+(b<<2)>>2]=d;H[s+(d<<2)>>2]=b;b=H[i>>2];H[C+(b<<2)>>2]=a;o=b+1|0;s=C+(o<<2)|0;H[s>>2]=h;h=b+2|0;d=C+(h<<2)|0;H[d>>2]=e;e=H[g+120>>2];f=o?a:-1;b=e+(f>>>3&536870908)|0;a=H[b>>2];R=b,S=Vj(f)&a,H[R>>2]=S;k=(o|0)!=-1?H[s>>2]:k;b=e+(k>>>3&536870908)|0;a=H[b>>2];R=b,S=Vj(k)&a,H[R>>2]=S;b=-1;b=(h|0)!=-1?H[d>>2]:b;f=e+(b>>>3&536870908)|0;a=H[f>>2];R=f,S=Vj(b)&a,H[R>>2]=S;F[i+88|0]=1;_c(j,i+88|0);Ka(r,i);l=l+1|0;f=H[i+64>>2]}if((c|0)!=(f|0)){continue}break}y=H[g+8>>2]}b=-1;if(((H[y+4>>2]-H[y>>2]>>2>>>0)/3|0)!=(l|0)){break s}b=H[y+28>>2]-H[y+24>>2]>>2;l=H[i+24>>2];e=H[i+28>>2];if((l|0)==(e|0)){break r}while(1){j=H[l>>2];a=H[y+24>>2];c=b-1|0;d=a+(c<<2)|0;if(H[d>>2]==-1){while(1){c=b-2|0;b=b-1|0;d=a+(c<<2)|0;if(H[d>>2]==-1){continue}break}}if(c>>>0>=j>>>0){H[i>>2]=y;d=H[d>>2];F[i+12|0]=1;H[i+8>>2]=d;H[i+4>>2]=d;if((d|0)!=-1){while(1){a=H[H[g+8>>2]>>2]+(d<<2)|0;if(H[a>>2]!=(c|0)){b=-1;break s}H[a>>2]=j;uc(i);d=H[i+8>>2];if((d|0)!=-1){continue}break}y=H[g+8>>2]}a=H[y+24>>2];f=a+(c<<2)|0;if((j|0)!=-1){H[a+(j<<2)>>2]=H[f>>2]}H[f>>2]=-1;h=1<>2];f=a+(j>>>3&536870908)|0;d=a+(c>>>3&536870908)|0;a=1<>2]&a){c=h|H[f>>2]}else{c=H[f>>2]&(h^-1)}H[f>>2]=c;H[d>>2]=H[d>>2]&(a^-1);b=b-1|0}l=l+4|0;if((e|0)!=(l|0)){continue}break}}l=H[i+24>>2]}if(l){oa(l)}a=H[i+48>>2];if(a){while(1){c=H[a>>2];oa(a);a=c;if(a){continue}break}}a=H[i+40>>2];H[i+40>>2]=0;if(a){oa(a)}a=H[i+64>>2];if(a){H[i+68>>2]=a;oa(a)}ca=i+96|0;break o}sa();v()}wa();v()}f=b;if((b|0)==-1){break n}b=H[x+16>>2];c=b+H[x>>2]|0;a=H[x+8>>2]-b|0;b=H[H[g+4>>2]+32>>2];G[b+38>>1]=J[b+38>>1];H[b>>2]=c;H[b+16>>2]=0;H[b+20>>2]=0;H[b+8>>2]=a;H[b+12>>2]=0;b=H[g+4>>2];a=J[b+36>>1];c=a<<8|a>>>8;if((c&65535)>>>0<=513){b=H[b+32>>2];e=b;a=H[b+16>>2];b=P+H[b+20>>2]|0;a=a+E|0;b=a>>>0>>0?b+1|0:b;H[e+16>>2]=a;H[e+20>>2]=b}pa:{if(H[g+216>>2]==H[g+220>>2]){break pa}a=H[g+8>>2];b=H[a>>2];a=H[a+4>>2];qa:{if((c&65535)>>>0>=513){if((a|0)==(b|0)){break pa}c=0;break qa}if((a|0)==(b|0)){break pa}c=0;while(1){if(cd(g,c)){c=c+3|0;a=H[g+8>>2];if(c>>>0>2]-H[a>>2]>>2>>>0){continue}break pa}break}break n}while(1){if(bd(g,c)){c=c+3|0;a=H[g+8>>2];if(c>>>0>2]-H[a>>2]>>2>>>0){continue}break pa}break}break n}ad(B);c=H[g+216>>2];if((c|0)!=H[g+220>>2]){t=0;while(1){e=N(t,144);Jc((e+c|0)+4|0,H[g+8>>2]);a=H[M>>2];b=a+e|0;c=H[b+132>>2];b=H[b+136>>2];if((c|0)!=(b|0)){while(1){Hc((e+H[M>>2]|0)+4|0,H[c>>2]);c=c+4|0;if((b|0)!=(c|0)){continue}break}a=H[M>>2]}if(!Ic((a+e|0)+4|0)){break n}t=t+1|0;c=H[g+216>>2];if(t>>>0<(H[g+220>>2]-c|0)/144>>>0){continue}break}}a=H[g+8>>2];Hb(g+184|0,H[a+28>>2]-H[a+24>>2]>>2);w=H[g+216>>2];if((w|0)!=H[g+220>>2]){c=0;while(1){a=N(c,144)+w|0;b=H[a+60>>2]-H[a+56>>2]>>2;e=a+104|0;a=H[g+8>>2];a=H[a+28>>2]-H[a+24>>2]>>2;Hb(e,(a|0)<(b|0)?b:a);c=c+1|0;w=H[g+216>>2];if(c>>>0<(H[g+220>>2]-w|0)/144>>>0){continue}break}}w=$c(g,f)}break b}w=0}ca=u- -64|0;return w|0}function ki(a){a=a|0;var b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0,x=0,y=0,z=0,A=0,B=0,C=0,D=0,E=0,L=0,M=0,O=0,P=0,Q=0;t=ca+-64|0;ca=t;H[a+132>>2]=0;if(H[a+148>>2]){d=H[a+144>>2];if(d){while(1){b=H[d>>2];oa(d);d=b;if(b){continue}break}}d=0;H[a+144>>2]=0;k=H[a+140>>2];a:{if(!k){break a}if(k>>>0>=4){b=k&-4;while(1){c=d<<2;H[c+H[a+136>>2]>>2]=0;H[H[a+136>>2]+(c|4)>>2]=0;H[H[a+136>>2]+(c|8)>>2]=0;H[H[a+136>>2]+(c|12)>>2]=0;d=d+4|0;e=e+4|0;if((b|0)!=(e|0)){continue}break}}b=k&3;if(!b){break a}while(1){H[H[a+136>>2]+(d<<2)>>2]=0;d=d+1|0;x=x+1|0;if((b|0)!=(x|0)){continue}break}}H[a+148>>2]=0}b:{c:{d=H[a+4>>2];x=I[d+36|0];b=x<<8|I[d+37|0];if(b>>>0<=513){f=H[d+32>>2];d:{if(b>>>0<=511){b=H[f+20>>2];e=H[f+16>>2];c=e+4|0;b=c>>>0<4?b+1|0:b;k=H[f+12>>2];if(K[f+8>>2]>>0&(k|0)<=(b|0)|(b|0)>(k|0)){break c}e=e+H[f>>2]|0;e=I[e|0]|I[e+1|0]<<8|(I[e+2|0]<<16|I[e+3|0]<<24);H[f+16>>2]=c;H[f+20>>2]=b;break d}if(!Ea(1,t,f)){break c}d=H[a+4>>2];x=I[d+36|0];e=H[t>>2]}H[a+132>>2]=e}f=H[d+32>>2];e:{f:{g:{if((x&255)>>>0<=1){x=0;b=H[f+20>>2];c=H[f+16>>2];e=c+4|0;b=e>>>0<4?b+1|0:b;k=H[f+12>>2];if(K[f+8>>2]>>0&(k|0)<=(b|0)|(b|0)>(k|0)){break b}c=c+H[f>>2]|0;c=I[c|0]|I[c+1|0]<<8|(I[c+2|0]<<16|I[c+3|0]<<24);H[t+60>>2]=c;H[f+16>>2]=e;H[f+20>>2]=b;H[a+156>>2]=c;l=a+156|0;break g}x=0;if(!Ea(1,t+60|0,f)){break b}d=H[a+4>>2];b=I[d+36|0];H[a+156>>2]=H[t+60>>2];l=a+156|0;if(b>>>0>1){break f}}f=H[d+32>>2];c=H[f+8>>2];k=H[f+12>>2];d=H[f+20>>2];e=H[f+16>>2];b=e+4|0;d=b>>>0<4?d+1|0:d;if(b>>>0>c>>>0&(d|0)>=(k|0)|(d|0)>(k|0)){break b}e=e+H[f>>2]|0;e=I[e|0]|I[e+1|0]<<8|(I[e+2|0]<<16|I[e+3|0]<<24);H[t+56>>2]=e;H[f+16>>2]=b;H[f+20>>2]=d;break e}if(!Ea(1,t+56|0,H[d+32>>2])){break b}e=H[t+56>>2]}if(e>>>0>1431655765|K[l>>2]>N(e,3)>>>0){break b}j=H[a+4>>2];y=H[j+32>>2];d=H[y+8>>2];f=H[y+12>>2];b=H[y+20>>2];m=H[y+16>>2];if((f|0)<=(b|0)&m>>>0>=d>>>0|(b|0)>(f|0)){break b}l=H[y>>2];g=I[l+m|0];c=y;k=m+1|0;C=k?b:b+1|0;H[c+16>>2]=k;H[c+20>>2]=C;h:{if(I[j+36|0]<=1){c=d;d=m+5|0;b=d>>>0<5?b+1|0:b;if(c>>>0>>0&(b|0)>=(f|0)|(b|0)>(f|0)){break b}c=k+l|0;l=I[c|0]|I[c+1|0]<<8|(I[c+2|0]<<16|I[c+3|0]<<24);H[t+52>>2]=l;H[y+16>>2]=d;H[y+20>>2]=b;break h}if(!Ea(1,t+52|0,y)){break b}l=H[t+52>>2]}if(e>>>0>>0|((l>>>0)/3|0)+l>>>0>>0){break b}d=H[a+4>>2];f=H[d+32>>2];i:{if(I[d+36|0]<=1){d=H[f+20>>2];b=H[f+16>>2];c=b+4|0;d=c>>>0<4?d+1|0:d;k=H[f+12>>2];if(K[f+8>>2]>>0&(k|0)<=(d|0)|(d|0)>(k|0)){break b}b=b+H[f>>2]|0;b=I[b|0]|I[b+1|0]<<8|(I[b+2|0]<<16|I[b+3|0]<<24);H[t+48>>2]=b;H[f+16>>2]=c;H[f+20>>2]=d;break i}if(!Ea(1,t+48|0,f)){break b}b=H[t+48>>2]}if(b>>>0>l>>>0){break b}H[a+28>>2]=H[a+24>>2];d=$b(pa(88));c=H[a+8>>2];H[a+8>>2]=d;if(c){cb(c);if(!H[a+8>>2]){break b}}H[a+164>>2]=H[a+160>>2];Jb(a+160|0,e);H[a+176>>2]=H[a+172>>2];Jb(a+172|0,e);H[a- -64>>2]=0;H[a+92>>2]=-1;H[a+84>>2]=-1;H[a+88>>2]=-1;H[a+40>>2]=H[a+36>>2];H[a+52>>2]=H[a+48>>2];H[a+76>>2]=H[a+72>>2];E=a+216|0;ed(E);dd(E,g);if(!Lc(H[a+8>>2],e,H[a+156>>2]+b|0)){break b}d=H[a+156>>2];F[t|0]=1;Oa(a+120|0,b+d|0,t);b=H[a+4>>2];d=J[b+36>>1];d=(d<<8|d>>>8)&65535;j:{if(d>>>0<=513){k=H[b+32>>2];k:{if(d>>>0<=511){b=H[k+20>>2];e=H[k+16>>2];d=e+4|0;b=d>>>0<4?b+1|0:b;c=H[k+12>>2];if(K[k+8>>2]>>0&(c|0)<=(b|0)|(b|0)>(c|0)){break b}e=e+H[k>>2]|0;e=I[e|0]|I[e+1|0]<<8|(I[e+2|0]<<16|I[e+3|0]<<24);H[k+16>>2]=d;H[k+20>>2]=b;break k}if(!Ea(1,t+44|0,k)){break b}e=H[t+44>>2]}if(!e){break b}k=H[H[a+4>>2]+32>>2];c=H[k+8>>2];d=H[k+16>>2];b=c-d|0;d=H[k+12>>2]-(H[k+20>>2]+(c>>>0>>0)|0)|0;if(b>>>0>>0&(d|0)<=0|(d|0)<0){break b}f=Ha(t);k=H[H[a+4>>2]+32>>2];c=H[k+16>>2];b=(c+H[k>>2]|0)+e|0;d=H[k+8>>2]-c|0;G[f+38>>1]=J[k+38>>1];H[f>>2]=b;H[f+16>>2]=0;H[f+20>>2]=0;H[f+8>>2]=d-e;H[f+12>>2]=0;d=Ib(a,f);if((d|0)==-1){break b}y=d;M=d>>31;break j}y=-1;M=-1;if((Ib(a,H[b+32>>2])|0)==-1){break b}}O=a+232|0;e=O;H[e+144>>2]=a;d=H[(ea[H[H[a>>2]+32>>2]](a)|0)+32>>2];b=H[d>>2]+H[d+16>>2]|0;d=H[(ea[H[H[a>>2]+32>>2]](a)|0)+32>>2];d=H[d+8>>2]-H[d+16>>2]|0;P=e,Q=J[H[(ea[H[H[a>>2]+32>>2]](a)|0)+32>>2]+38>>1],G[P+38>>1]=Q;H[e>>2]=b;H[e+16>>2]=0;H[e+20>>2]=0;H[e+8>>2]=d;H[e+12>>2]=0;H[a+372>>2]=g;C=Ha(t);l:{if(!Ge(e,C)){break l}b=0;d=0;e=0;k=0;i=ca-96|0;ca=i;H[i+72>>2]=0;H[i+64>>2]=0;H[i+68>>2]=0;H[i+48>>2]=0;H[i+52>>2]=0;H[i+40>>2]=0;H[i+44>>2]=0;H[i+56>>2]=1065353216;H[i+32>>2]=0;H[i+24>>2]=0;H[i+28>>2]=0;h=a;L=H[a+124>>2];m:{n:{o:{p:{q:{r:{if((l|0)<=0){break r}A=H[h+216>>2]!=H[h+220>>2];s=1;while(1){f=k;k=f+1|0;s:{t:{u:{v:{w:{x:{y:{z:{A:{B:{C:{D:{E:{F:{G:{if(!I[h+308|0]){break G}u=H[h+296>>2];g=H[h+304>>2];a=u+(g>>>3|0)|0;p=H[h+300>>2];if(a>>>0>=p>>>0){break G}c=I[a|0];a=g+1|0;H[h+304>>2]=a;m=c>>>(g&7)&1;if(!m){break G}n=0;j=a>>>3|0;c=u+j|0;H:{if(c>>>0>=p>>>0){g=a;a=0;break H}c=I[c|0];g=g+2|0;H[h+304>>2]=g;j=g>>>3|0;a=c>>>(a&7)&1}c=j+u|0;if(c>>>0

>>0){c=I[c|0];H[h+304>>2]=g+1;n=c>>>(g&7)<<1&2}j=-1;a=m|(a|n)<<1;switch(a-1|0){case 6:break D;case 0:break E;case 2:case 4:break F;default:break q}}if((d|0)==(e|0)){j=-1;break q}g=-1;q=H[h+8>>2];u=H[q+24>>2];p=d-4|0;s=H[p>>2];c=-1;I:{if((s|0)==-1){break I}b=s+1|0;b=(b>>>0)%3|0?b:s-2|0;c=-1;if((b|0)==-1){break I}c=H[H[q>>2]+(b<<2)>>2]}b=H[u+(c<<2)>>2];if((b|0)!=-1){a=b+1|0;g=(a>>>0)%3|0?a:b-2|0}if((g|0)==(s|0)){j=-1;break q}if((s|0)!=-1){j=-1;if(H[H[q+12>>2]+(s<<2)>>2]!=-1){break q}}b=H[q+12>>2];if((g|0)!=-1){j=-1;if(H[b+(g<<2)>>2]!=-1){break q}}n=N(f,3);a=n+1|0;H[b+(s<<2)>>2]=a;m=a<<2;H[m+b>>2]=s;r=n+2|0;H[b+(g<<2)>>2]=r;f=r<<2;H[f+b>>2]=g;o=-1;a=-1;J:{if((s|0)==-1){break J}K:{if((s>>>0)%3|0){b=s-1|0;break K}b=s+2|0;a=-1;if((b|0)==-1){break J}}a=H[H[q>>2]+(b<<2)>>2]}L:{if((g|0)==-1){break L}b=g+1|0;b=(b>>>0)%3|0?b:g-2|0;if((b|0)==-1){break L}o=H[H[q>>2]+(b<<2)>>2]}j=-1;if((a|0)==(c|0)|(c|0)==(o|0)){break q}b=H[q>>2];H[b+(n<<2)>>2]=c;H[b+m>>2]=o;H[b+f>>2]=a;if((a|0)!=-1){H[u+(a<<2)>>2]=r}b=H[h+120>>2]+(c>>>3&536870908)|0;a=H[b>>2];P=b,Q=Vj(c)&a,H[P>>2]=Q;H[p>>2]=n;b=e;break s}if((d|0)==(e|0)){break q}m=d-4|0;n=H[m>>2];r=H[h+8>>2];b=H[r+12>>2];if((n|0)!=-1&H[b+(n<<2)>>2]!=-1){break q}o=N(f,3);p=(a|0)==5;g=o+(p?2:1)|0;a=g<<2;H[a+b>>2]=n;H[b+(n<<2)>>2]=g;Ka(r+24|0,11424);b=H[h+8>>2];u=H[b+24>>2];if(H[b+28>>2]-u>>2>(L|0)){break q}j=H[b>>2];q=j+a|0;c=H[r+28>>2];b=H[r+24>>2];a=(c-b>>2)-1|0;H[q>>2]=a;if((b|0)!=(c|0)){H[u+(a<<2)>>2]=g}c=p?o:o+2|0;g=j+(o+p<<2)|0;M:{if((n|0)==-1){H[j+(c<<2)>>2]=-1;b=-1;break M}N:{O:{P:{if((n>>>0)%3|0){a=n-1|0;break P}a=n+2|0;if((a|0)==-1){break O}}a=H[j+(a<<2)>>2];H[j+(c<<2)>>2]=a;if((a|0)==-1){break N}H[u+(a<<2)>>2]=c;break N}H[j+(c<<2)>>2]=-1}a=n+1|0;a=(a>>>0)%3|0?a:n-2|0;b=-1;if((a|0)==-1){break M}b=H[j+(a<<2)>>2]}H[g>>2]=b;H[m>>2]=o;b=e;break y}if((b|0)==(d|0)){break q}a=d-4|0;q=H[a>>2];H[i+68>>2]=a;p=H[i+44>>2];Q:{if(!p){d=a;break Q}g=H[i+40>>2];j=Uj(p)>>>0>1;c=f&p+2147483647;R:{if(!j){break R}c=f;if(c>>>0

>>0){break R}c=(f>>>0)%(p>>>0)|0}m=c;c=H[g+(m<<2)>>2];if(!c){d=a;break Q}g=H[c>>2];if(!g){d=a;break Q}S:{if(!j){j=p-1|0;while(1){c=H[g+4>>2];T:{if((c|0)!=(f|0)){if((m|0)==(c&j)){break T}d=a;break Q}if((f|0)==H[g+8>>2]){break S}}g=H[g>>2];if(g){continue}break}d=a;break Q}while(1){c=H[g+4>>2];U:{if((c|0)!=(f|0)){if(c>>>0>=p>>>0){c=(c>>>0)%(p>>>0)|0}if((c|0)==(m|0)){break U}d=a;break Q}if((f|0)==H[g+8>>2]){break S}}g=H[g>>2];if(g){continue}break}d=a;break Q}if((a|0)!=(z|0)){H[a>>2]=H[g+12>>2];H[i+68>>2]=d;break Q}c=z-b|0;d=c>>2;e=d+1|0;if(e>>>0>=1073741824){break C}a=c>>>1|0;c=c>>>0>=2147483644?1073741823:a>>>0>e>>>0?a:e;if(c){if(c>>>0>=1073741824){break n}a=pa(c<<2)}else{a=0}e=a+(d<<2)|0;H[e>>2]=H[g+12>>2];d=e+4|0;if((b|0)!=(z|0)){while(1){e=e-4|0;z=z-4|0;H[e>>2]=H[z>>2];if((b|0)!=(z|0)){continue}break}}z=a+(c<<2)|0;H[i+72>>2]=z;H[i+68>>2]=d;H[i+64>>2]=e;if(b){oa(b)}}if((d|0)==(e|0)){break u}g=d-4|0;n=H[g>>2];if((n|0)==(q|0)){break u}b=(n|0)==-1;o=H[h+8>>2];if(!b&H[H[o+12>>2]+(n<<2)>>2]!=-1){break u}r=H[o+12>>2];if((q|0)!=-1&H[r+(q<<2)>>2]!=-1){break u}u=N(f,3);f=u+2|0;H[r+(n<<2)>>2]=f;p=f<<2;H[p+r>>2]=n;a=u+1|0;H[r+(q<<2)>>2]=a;c=a<<2;H[c+r>>2]=q;if(b){break B}if((n>>>0)%3|0){m=n-1|0;break x}m=n+2|0;if((m|0)!=-1){break x}a=H[o>>2];b=-1;break w}a=H[h+8>>2];Ka(a+24|0,11424);c=H[h+8>>2];q=N(f,3);r=H[a+28>>2];u=H[a+24>>2];p=r-u|0;o=p>>2;g=o-1|0;H[H[c>>2]+(q<<2)>>2]=g;Ka(c+24|0,11424);m=q+1|0;H[H[c>>2]+(m<<2)>>2]=(H[c+28>>2]-H[c+24>>2]>>2)-1;a=H[h+8>>2];Ka(a+24|0,11424);c=q+2|0;H[H[a>>2]+(c<<2)>>2]=(H[a+28>>2]-H[a+24>>2]>>2)-1;a=H[h+8>>2];n=H[a+24>>2];if(H[a+28>>2]-n>>2>(L|0)){break q}V:{W:{if((r|0)!=(u|0)){H[n+(g<<2)>>2]=q;j=0;if((p|0)==-4){break W}}H[n+(o<<2)>>2]=m;j=o+1|0;if((j|0)==-1){break V}}H[n+(j<<2)>>2]=c}if((d|0)!=(z|0)){H[d>>2]=q;d=d+4|0;H[i+68>>2]=d;break y}m=d-b|0;e=m>>2;c=e+1|0;if(c>>>0>=1073741824){break A}a=m>>>1|0;c=m>>>0>=2147483644?1073741823:a>>>0>c>>>0?a:c;if(c){if(c>>>0>=1073741824){break n}a=pa(c<<2)}else{a=0}e=a+(e<<2)|0;H[e>>2]=q;z=a+(c<<2)|0;a=e+4|0;if((b|0)!=(d|0)){while(1){e=e-4|0;d=d-4|0;H[e>>2]=H[d>>2];if((b|0)!=(d|0)){continue}break}}H[i+72>>2]=z;H[i+68>>2]=a;H[i+64>>2]=e;if(!b){break z}oa(b);break z}sa();v()}m=-1;a=H[o>>2];H[a+(u<<2)>>2]=-1;j=-1;break v}sa();v()}d=a;b=e}m=H[h+40>>2];if((m|0)==H[h+36>>2]){break s}c=m-12|0;a=H[c+4>>2];j=(f^-1)+l|0;if(a>>>0>j>>>0){break u}if((a|0)!=(j|0)){break s}f=I[m-4|0];a=H[c>>2];H[h+40>>2]=c;if((a|0)<0){break u}m=d-4|0;g=H[m>>2];H[i+20>>2]=(a^-1)+l;a=i+20|0;H[i+88>>2]=a;Gb(i,i+40|0,a,i+88|0);c=H[i>>2];X:{if(f&1){a=-1;if((g|0)==-1){break X}a=g+1|0;a=(a>>>0)%3|0?a:g-2|0;break X}a=-1;if((g|0)==-1){break X}a=g-1|0;if((g>>>0)%3|0){break X}a=g+2|0}H[c+12>>2]=a;g=H[h+40>>2];if((g|0)==H[h+36>>2]){break s}while(1){c=g-12|0;a=H[c+4>>2];if(a>>>0>j>>>0){break u}if((a|0)!=(j|0)){break s}f=I[g-4|0];a=H[c>>2];H[h+40>>2]=c;if((a|0)<0){break u}g=H[m>>2];H[i+20>>2]=(a^-1)+l;a=i+20|0;H[i+88>>2]=a;Gb(i,i+40|0,a,i+88|0);c=H[i>>2];Y:{if(f&1){a=-1;if((g|0)==-1){break Y}a=g+1|0;a=(a>>>0)%3|0?a:g-2|0;break Y}a=-1;if((g|0)==-1){break Y}a=g-1|0;if((g>>>0)%3|0){break Y}a=g+2|0}H[c+12>>2]=a;g=H[h+40>>2];if((g|0)!=H[h+36>>2]){continue}break}break s}a=H[o>>2];b=H[a+(m<<2)>>2]}m=b;H[(u<<2)+a>>2]=b;b=n+1|0;b=(b>>>0)%3|0?b:n-2|0;j=-1;if((b|0)==-1){break v}j=H[(b<<2)+a>>2]}H[a+c>>2]=j;Z:{if((q|0)==-1){H[a+p>>2]=-1;n=-1;c=-1;break Z}_:{$:{aa:{if((q>>>0)%3|0){b=q-1|0;break aa}b=q+2|0;if((b|0)==-1){break $}}b=H[(b<<2)+a>>2];H[a+p>>2]=b;if((b|0)==-1){break _}H[H[o+24>>2]+(b<<2)>>2]=f;break _}H[a+p>>2]=-1}n=-1;b=q+1|0;b=(b>>>0)%3|0?b:q-2|0;c=-1;if((b|0)==-1){break Z}n=H[(b<<2)+a>>2];c=b}b=H[o+24>>2];p=b+(n<<2)|0;if((m|0)!=-1){H[b+(m<<2)>>2]=H[p>>2]}b=c;while(1){if((b|0)==-1){break t}H[(b<<2)+a>>2]=m;j=b+1|0;b=(j>>>0)%3|0?j:b-2|0;f=-1;ba:{if((b|0)==-1){break ba}j=H[r+(b<<2)>>2];f=-1;if((j|0)==-1){break ba}b=j+1|0;f=(b>>>0)%3|0?b:j-2|0}b=f;if((c|0)!=(b|0)){continue}break}}j=-1;if(!(s&1)){break r}break q}H[p>>2]=-1;ca:{if(A){break ca}if((B|0)!=(D|0)){H[D>>2]=n;D=D+4|0;H[i+28>>2]=D;break ca}f=B-w|0;b=f>>2;c=b+1|0;if(c>>>0>=1073741824){break o}a=f>>>1|0;c=f>>>0>=2147483644?1073741823:a>>>0>c>>>0?a:c;if(c){if(c>>>0>=1073741824){break n}a=pa(c<<2)}else{a=0}b=a+(b<<2)|0;H[b>>2]=n;D=b+4|0;if((w|0)!=(B|0)){while(1){b=b-4|0;B=B-4|0;H[b>>2]=H[B>>2];if((w|0)!=(B|0)){continue}break}}B=a+(c<<2)|0;H[i+32>>2]=B;H[i+28>>2]=D;H[i+24>>2]=b;if(w){oa(w)}w=b}H[g>>2]=u;b=e}s=(k|0)<(l|0);if((k|0)!=(l|0)){continue}break}k=l}j=-1;a=H[h+8>>2];if(H[a+28>>2]-H[a+24>>2]>>2>(L|0)){break q}if((d|0)!=(e|0)){u=h+72|0;m=h+60|0;p=h+312|0;while(1){d=d-4|0;o=H[d>>2];H[i+68>>2]=d;da:{ea:{fa:{if(J[h+270>>1]<=513){if(!I[h+364|0]){break ea}b=H[h+360>>2];a=H[h+352>>2]+(b>>>3|0)|0;if(a>>>0>=K[h+356>>2]){break fa}a=I[a|0];H[h+360>>2]=b+1;if(!(a>>>(b&7)&1)){break fa}break ea}if(Ba(p)){break ea}}ga:{ha:{b=H[h+64>>2];c=H[h+68>>2];if((b|0)==c<<5){if((b+1|0)<0){break ha}if(b>>>0<=1073741822){c=c<<6;b=(b&-32)+32|0;a=b>>>0>>0?c:b}else{a=2147483647}pb(m,a);b=H[h+64>>2]}H[h+64>>2]=b+1;c=H[h+60>>2]+(b>>>3&536870908)|0;a=H[c>>2];P=c,Q=Vj(b)&a,H[P>>2]=Q;b=H[h+76>>2];if((b|0)!=H[h+80>>2]){H[b>>2]=o;H[h+76>>2]=b+4;break da}l=H[u>>2];w=b-l|0;c=w>>2;f=c+1|0;if(f>>>0>=1073741824){break ga}a=w>>>1|0;f=w>>>0>=2147483644?1073741823:a>>>0>f>>>0?a:f;if(f){if(f>>>0>=1073741824){break n}a=pa(f<<2)}else{a=0}g=a+(c<<2)|0;H[g>>2]=o;c=g+4|0;if((b|0)!=(l|0)){while(1){g=g-4|0;b=b-4|0;H[g>>2]=H[b>>2];if((b|0)!=(l|0)){continue}break}}H[h+80>>2]=a+(f<<2);H[h+76>>2]=c;H[h+72>>2]=g;if(!l){break da}oa(l);break da}sa();v()}sa();v()}r=H[h+8>>2];A=H[r>>2];if(((H[r+4>>2]-A>>2>>>0)/3|0)<=(k|0)){j=-1;break q}a=-1;j=-1;b=-1;w=H[r+24>>2];f=-1;ia:{if((o|0)==-1){break ia}e=o+1|0;e=(e>>>0)%3|0?e:o-2|0;f=-1;if((e|0)==-1){break ia}f=H[A+(e<<2)>>2]}l=H[w+(f<<2)>>2];ja:{if((l|0)==-1){g=1;e=-1;break ja}g=1;c=l+1|0;c=(c>>>0)%3|0?c:l-2|0;e=-1;if((c|0)==-1){break ja}g=0;a=c;e=a+1|0;e=(e>>>0)%3|0?e:a-2|0;if((e|0)!=-1){e=H[A+(e<<2)>>2]}else{e=-1}}c=H[(e<<2)+w>>2];if((c|0)!=-1){b=c+1|0;b=(b>>>0)%3|0?b:c-2|0}if((a|0)==(o|0)|(b|0)==(o|0)|((o|0)!=-1&H[H[r+12>>2]+(o<<2)>>2]!=-1|(a|0)==(b|0))){break q}if(!g&H[H[r+12>>2]+(a<<2)>>2]!=-1){break q}g=-1;l=H[r+12>>2];w=-1;ka:{if((b|0)==-1){break ka}if(H[l+(b<<2)>>2]!=-1){break q}c=b+1|0;c=(c>>>0)%3|0?c:b-2|0;w=-1;if((c|0)==-1){break ka}w=H[A+(c<<2)>>2]}c=N(k,3);H[i>>2]=c;H[l+(c<<2)>>2]=o;H[l+(o<<2)>>2]=c;c=H[i>>2]+1|0;H[l+(c<<2)>>2]=a;H[l+(a<<2)>>2]=c;a=H[i>>2]+2|0;H[l+(a<<2)>>2]=b;H[l+(b<<2)>>2]=a;a=H[i>>2];H[A+(a<<2)>>2]=e;j=a+1|0;l=A+(j<<2)|0;H[l>>2]=w;w=a+2|0;c=A+(w<<2)|0;H[c>>2]=f;f=H[h+120>>2];e=j?e:-1;b=f+(e>>>3&536870908)|0;a=H[b>>2];P=b,Q=Vj(e)&a,H[P>>2]=Q;g=(j|0)!=-1?H[l>>2]:g;b=f+(g>>>3&536870908)|0;a=H[b>>2];P=b,Q=Vj(g)&a,H[P>>2]=Q;b=-1;b=(w|0)!=-1?H[c>>2]:b;e=f+(b>>>3&536870908)|0;a=H[e>>2];P=e,Q=Vj(b)&a,H[P>>2]=Q;F[i+88|0]=1;_c(m,i+88|0);Ka(u,i);k=k+1|0;e=H[i+64>>2]}if((d|0)!=(e|0)){continue}break}a=H[h+8>>2]}j=-1;if(((H[a+4>>2]-H[a>>2]>>2>>>0)/3|0)!=(k|0)){break q}j=H[a+28>>2]-H[a+24>>2]>>2;s=H[i+24>>2];c=H[i+28>>2];if((s|0)==(c|0)){break p}while(1){k=H[s>>2];d=H[a+24>>2];b=j-1|0;g=d+(b<<2)|0;if(H[g>>2]==-1){while(1){b=j-2|0;j=j-1|0;g=d+(b<<2)|0;if(H[g>>2]==-1){continue}break}}if(b>>>0>=k>>>0){H[i>>2]=a;g=H[g>>2];F[i+12|0]=1;H[i+8>>2]=g;H[i+4>>2]=g;if((g|0)!=-1){while(1){a=H[H[h+8>>2]>>2]+(g<<2)|0;if(H[a>>2]!=(b|0)){j=-1;break q}H[a>>2]=k;uc(i);g=H[i+8>>2];if((g|0)!=-1){continue}break}a=H[h+8>>2]}d=H[a+24>>2];e=d+(b<<2)|0;if((k|0)!=-1){H[d+(k<<2)>>2]=H[e>>2]}H[e>>2]=-1;f=1<>2];e=d+(k>>>3&536870908)|0;k=d+(b>>>3&536870908)|0;d=1<>2]&d){b=f|H[e>>2]}else{b=H[e>>2]&(f^-1)}H[e>>2]=b;H[k>>2]=H[k>>2]&(d^-1);j=j-1|0}s=s+4|0;if((c|0)!=(s|0)){continue}break}}s=H[i+24>>2]}if(s){oa(s)}a=H[i+48>>2];if(a){while(1){d=H[a>>2];oa(a);a=d;if(a){continue}break}}a=H[i+40>>2];H[i+40>>2]=0;if(a){oa(a)}a=H[i+64>>2];if(a){H[i+68>>2]=a;oa(a)}ca=i+96|0;a=j;break m}sa();v()}wa();v()}e=a;if((a|0)==-1){break l}b=H[C+16>>2];d=b+H[C>>2]|0;a=H[C+8>>2]-b|0;b=H[H[h+4>>2]+32>>2];G[b+38>>1]=J[b+38>>1];H[b>>2]=d;H[b+16>>2]=0;H[b+20>>2]=0;H[b+8>>2]=a;H[b+12>>2]=0;b=H[h+4>>2];a=J[b+36>>1];d=a<<8|a>>>8;if((d&65535)>>>0<=513){b=H[b+32>>2];c=b;a=H[b+16>>2];b=M+H[b+20>>2]|0;a=a+y|0;b=a>>>0>>0?b+1|0:b;H[c+16>>2]=a;H[c+20>>2]=b}la:{if(H[h+216>>2]==H[h+220>>2]){break la}a=H[h+8>>2];b=H[a>>2];a=H[a+4>>2];ma:{if((d&65535)>>>0>=513){if((a|0)==(b|0)){break la}d=0;break ma}if((a|0)==(b|0)){break la}d=0;while(1){if(cd(h,d)){d=d+3|0;a=H[h+8>>2];if(d>>>0>2]-H[a>>2]>>2>>>0){continue}break la}break}break l}while(1){if(bd(h,d)){d=d+3|0;a=H[h+8>>2];if(d>>>0>2]-H[a>>2]>>2>>>0){continue}break la}break}break l}ad(O);d=H[h+216>>2];if((d|0)!=H[h+220>>2]){l=0;while(1){c=N(l,144);Jc((c+d|0)+4|0,H[h+8>>2]);a=H[E>>2];b=a+c|0;d=H[b+132>>2];b=H[b+136>>2];if((d|0)!=(b|0)){while(1){Hc((c+H[E>>2]|0)+4|0,H[d>>2]);d=d+4|0;if((b|0)!=(d|0)){continue}break}a=H[E>>2]}if(!Ic((a+c|0)+4|0)){break l}l=l+1|0;d=H[h+216>>2];if(l>>>0<(H[h+220>>2]-d|0)/144>>>0){continue}break}}a=H[h+8>>2];Hb(h+184|0,H[a+28>>2]-H[a+24>>2]>>2);x=H[h+216>>2];if((x|0)!=H[h+220>>2]){d=0;while(1){a=N(d,144)+x|0;b=H[a+60>>2]-H[a+56>>2]>>2;c=a+104|0;a=H[h+8>>2];a=H[a+28>>2]-H[a+24>>2]>>2;Hb(c,(a|0)<(b|0)?b:a);d=d+1|0;x=H[h+216>>2];if(d>>>0<(H[h+220>>2]-x|0)/144>>>0){continue}break}}x=$c(h,e)}break b}x=0}ca=t- -64|0;return x|0}function Bg(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0,x=0,y=0,z=0,A=0,B=0,C=0,D=0,E=0,G=0,J=0,K=0,L=0,M=0,O=0;B=c;c=0;m=ca-96|0;ca=m;l=m+16|0;ra(l,0,76);H[m+92>>2]=-1;H[m+8>>2]=0;H[m>>2]=0;H[m+4>>2]=0;r=ca-16|0;ca=r;H[l+68>>2]=0;H[l+72>>2]=0;H[l>>2]=b;s=ca-16|0;ca=s;u=b;a=H[b+20>>2];a:{if((H[b+24>>2]-a|0)<=0){break a}a=H[a>>2];if((a|0)==-1){break a}c=H[H[u+8>>2]+(a<<2)>>2]}b:{c:{d:{if(!c){a=0;break d}a=H[u+100>>2];e=H[u+96>>2];H[s+8>>2]=0;H[s>>2]=0;H[s+4>>2]=0;f=a-e|0;b=(f|0)/12|0;e:{if((a|0)==(e|0)){break e}if(b>>>0>=357913942){break c}d=pa(f);H[s>>2]=d;H[s+8>>2]=d+N(b,12);a=0;n=d;f=f-12|0;d=(f-((f>>>0)%12|0)|0)+12|0;f=ra(n,0,d);H[s+4>>2]=d+f;if(I[c+84|0]){c=b>>>0<=1?1:b;h=c&1;if(b>>>0>=2){g=c&-2;c=0;while(1){d=N(a,12);b=d+e|0;i=H[b+4>>2];j=H[b>>2];d=d+f|0;H[d+8>>2]=H[b+8>>2];H[d>>2]=j;H[d+4>>2]=i;d=N(a|1,12);b=d+e|0;i=H[b+4>>2];j=H[b>>2];d=d+f|0;H[d+8>>2]=H[b+8>>2];H[d>>2]=j;H[d+4>>2]=i;a=a+2|0;c=c+2|0;if((g|0)!=(c|0)){continue}break}}if(!h){break e}b=N(a,12);a=b+e|0;c=H[a+4>>2];e=H[a>>2];b=b+f|0;H[b+8>>2]=H[a+8>>2];H[b>>2]=e;H[b+4>>2]=c;break e}h=b>>>0<=1?1:b;a=H[c+68>>2];c=0;while(1){d=N(c,12);b=d+e|0;g=H[a+(H[b>>2]<<2)>>2];i=H[a+(H[b+4>>2]<<2)>>2];d=d+f|0;H[d+8>>2]=H[a+(H[b+8>>2]<<2)>>2];H[d+4>>2]=i;H[d>>2]=g;c=c+1|0;if((h|0)!=(c|0)){continue}break}}d=0;E=ca-16|0;ca=E;h=pa(88);$b(h);C=ca-16|0;ca=C;H[h+80>>2]=0;H[h+84>>2]=0;a=H[h+76>>2];H[h+76>>2]=0;if(a){oa(a)}H[h+68>>2]=0;H[h+72>>2]=0;b=h- -64|0;a=H[b>>2];H[b>>2]=0;if(a){oa(a)}g=H[s+4>>2];b=H[s>>2];c=(g-b|0)/12|0;a=N(c,3);f=H[h>>2];e=H[h+4>>2]-f>>2;f:{if(a>>>0>e>>>0){ue(h,a-e|0);g=H[s+4>>2];b=H[s>>2];c=(g-b|0)/12|0;break f}if(a>>>0>=e>>>0){break f}H[h+4>>2]=f+(a<<2)}g:{if((b|0)==(g|0)){break g}e=c>>>0<=1?1:c;g=e&1;a=H[h>>2];if(c>>>0>=2){i=e&-2;c=0;while(1){e=N(d,12);j=e+a|0;f=b+e|0;H[j>>2]=H[f>>2];H[a+(e|4)>>2]=H[f+4>>2];H[j+8>>2]=H[f+8>>2];f=N(d|1,12);e=f+a|0;f=b+f|0;H[e>>2]=H[f>>2];H[e+4>>2]=H[f+4>>2];H[e+8>>2]=H[f+8>>2];d=d+2|0;c=c+2|0;if((i|0)!=(c|0)){continue}break}}if(!g){break g}c=N(d,12);a=c+a|0;b=b+c|0;H[a>>2]=H[b>>2];H[a+4>>2]=H[b+4>>2];H[a+8>>2]=H[b+8>>2]}H[C+12>>2]=-1;a=0;e=0;g=0;f=ca-32|0;ca=f;h:{i:{w=C+12|0;j:{if(!w){break j}c=H[h+4>>2];j=H[h>>2];d=c-j|0;i=d>>2;n=H[h+12>>2];b=H[h+16>>2]-n>>2;k:{if(i>>>0>b>>>0){qb(h+12|0,i-b|0,13652);c=H[h+4>>2];j=H[h>>2];d=c-j|0;i=d>>2;break k}if(b>>>0<=i>>>0){break k}H[h+16>>2]=n+(i<<2)}H[f+24>>2]=0;H[f+16>>2]=0;H[f+20>>2]=0;b=(c|0)==(j|0);if(!b){if((d|0)<0){break i}e=pa(d);H[f+20>>2]=e;H[f+16>>2]=e;H[f+24>>2]=(i<<2)+e}l:{m:{n:{o:{p:{if(d){while(1){i=H[(a<<2)+j>>2];b=H[f+20>>2]-e>>2;q:{if(i>>>0>>0){break q}H[f>>2]=0;d=i+1|0;if(d>>>0>b>>>0){Pa(f+16|0,d-b|0,f);j=H[h>>2];c=H[h+4>>2];e=H[f+16>>2];break q}if(b>>>0<=d>>>0){break q}H[f+20>>2]=(d<<2)+e}b=(i<<2)+e|0;H[b>>2]=H[b>>2]+1;a=a+1|0;d=c-j|0;i=d>>2;if(a>>>0>>0){continue}break}break p}d=0;if(!b){break o}break n}if((c|0)==(j|0)){d=0;break n}if(d>>>0>=2147483645){break m}}d=pa(d<<1);ra(d,255,i<<3)}H[f+8>>2]=0;H[f>>2]=0;H[f+4>>2]=0;b=H[f+20>>2];a=b-e|0;t=a>>2;r:{s:{if((b|0)==(e|0)){break s}if((a|0)<0){break r}q=pa(a);H[f>>2]=q;H[f+8>>2]=(t<<2)+q;b=ra(q,0,a);H[f+4>>2]=b+a;c=t>>>0<=1?1:t;n=c&3;a=0;if(c-1>>>0>=3){o=c&-4;while(1){c=g<<2;H[c+b>>2]=a;x=c|4;a=H[c+e>>2]+a|0;H[x+b>>2]=a;y=c|8;a=a+H[e+x>>2]|0;H[y+b>>2]=a;c=c|12;a=a+H[e+y>>2]|0;H[c+b>>2]=a;a=a+H[c+e>>2]|0;g=g+4|0;p=p+4|0;if((o|0)!=(p|0)){continue}break}}if(!n){break s}while(1){c=g<<2;H[c+b>>2]=a;g=g+1|0;a=H[c+e>>2]+a|0;k=k+1|0;if((n|0)!=(k|0)){continue}break}}if(!i){break l}x=H[h+40>>2];y=H[h+12>>2];n=0;while(1){G=n<<2;a=G+j|0;k=-1;c=n+1|0;b=(c>>>0)%3|0?c:n-2|0;if((b|0)!=-1){k=H[(b<<2)+j>>2]}b=H[a>>2];t:{u:{if(!((n>>>0)%3|0)){p=-1;a=n+2|0;if((a|0)!=-1){p=H[(a<<2)+j>>2]}if(!((b|0)==(k|0)|(b|0)==(p|0))&(k|0)!=(p|0)){break u}x=x+1|0;H[h+40>>2]=x;c=n+3|0;break t}p=H[a-4>>2]}a=p<<2;A=H[a+e>>2];v:{w:{if((A|0)<=0){break w}a=H[a+q>>2];g=0;while(1){o=(a<<3)+d|0;z=H[o>>2];if((z|0)==-1){break w}x:{if((k|0)!=(z|0)){break x}o=H[o+4>>2];if((o|0)!=-1){z=H[(o<<2)+j>>2]}else{z=-1}if((z|0)==(b|0)){break x}while(1){y:{b=a;g=g+1|0;if((A|0)<=(g|0)){break y}a=b+1|0;J=(a<<3)+d|0;z=H[J>>2];K=(b<<3)+d|0;H[K+4>>2]=H[J+4>>2];H[K>>2]=z;if((z|0)!=-1){continue}}break}H[(b<<3)+d>>2]=-1;if((o|0)==-1){break w}H[y+G>>2]=o;H[y+(o<<2)>>2]=n;break v}a=a+1|0;g=g+1|0;if((A|0)!=(g|0)){continue}break}}a=k<<2;k=H[a+e>>2];if((k|0)<=0){break v}a=H[a+q>>2];g=0;while(1){b=(a<<3)+d|0;if(H[b>>2]==-1){H[b>>2]=p;H[b+4>>2]=n;break v}a=a+1|0;g=g+1|0;if((k|0)!=(g|0)){continue}break}}}n=c;if(n>>>0>>0){continue}break}break l}break i}sa();v()}H[w>>2]=t;if(q){oa(q)}if(d){oa(d)}a=H[f+16>>2];if(!a){break j}H[f+20>>2]=a;oa(a)}ca=f+32|0;x=(w|0)!=0;if(x){k=ca-32|0;ca=k;a=H[h>>2];g=H[h+4>>2];H[k+24>>2]=0;H[k+16>>2]=0;H[k+20>>2]=0;if((a|0)==(g|0)){c=g}else{a=g-a|0;if((a|0)<0){break i}a=a>>2;b=(a-1>>>5|0)+1|0;c=pa(b<<2);H[k+24>>2]=b;H[k+20>>2]=0;H[k+16>>2]=c;Mc(k+16|0,a);g=H[h>>2];c=H[h+4>>2]}H[k+8>>2]=0;H[k>>2]=0;while(1){z:{o=0;i=0;if((c|0)==(g|0)){break z}while(1){b=H[k+16>>2];A:{if(H[b+(i>>>3&536870908)>>2]>>>i&1){break A}c=H[k>>2];H[k+4>>2]=c;e=H[h+12>>2];a=i;while(1){B:{f=a+1|0;d=a;a=(f>>>0)%3|0?f:a-2|0;if((a|0)==-1){break B}a=H[e+(a<<2)>>2];if((a|0)==-1){break B}f=a+1|0;a=(f>>>0)%3|0?f:a-2|0;if((i|0)==(a|0)|(a|0)==-1){break B}if(!(H[b+(a>>>3&536870908)>>2]>>>a&1)){continue}}break}j=d;C:{D:{E:{while(1){a=H[k+16>>2]+(j>>>3&536870908)|0;H[a>>2]=H[a>>2]|1<>>0)%3|0?a:j-2|0;g=H[h>>2];y=(j>>>0)%3|0;b=(y?-1:2)+j|0;n=H[k>>2];A=(n|0)==(c|0);F:{if(A){break F}w=H[(f<<2)+g>>2];q=H[h+12>>2];a=n;if((b|0)!=-1){e=q+(b<<2)|0;while(1){G:{if((w|0)!=H[a>>2]){break G}p=H[a+4>>2];t=H[e>>2];if((p|0)==(t|0)){break G}e=b;c=-1;a=-1;if((p|0)==-1){break C}break D}a=a+8|0;if((c|0)!=(a|0)){continue}break}break F}while(1){if((w|0)==H[a>>2]){t=-1;e=-1;p=H[a+4>>2];if((p|0)!=-1){break D}}a=a+8|0;if((c|0)!=(a|0)){continue}break}}b=H[(b<<2)+g>>2];H:{if(H[k+8>>2]!=(c|0)){H[c>>2]=b;H[c+4>>2]=f;c=c+8|0;H[k+4>>2]=c;break H}a=c-n|0;p=a>>3;e=p+1|0;if(e>>>0>=536870912){break i}g=a>>>2|0;g=a>>>0>=2147483640?536870911:e>>>0>>0?g:e;if(g){if(g>>>0>=536870912){break E}e=pa(g<<3)}else{e=0}a=e+(p<<3)|0;H[a>>2]=b;H[a+4>>2]=f;b=a+8|0;if(!A){while(1){c=c-8|0;f=H[c+4>>2];a=a-8|0;H[a>>2]=H[c>>2];H[a+4>>2]=f;if((c|0)!=(n|0)){continue}break}c=H[k>>2]}H[k+8>>2]=e+(g<<3);H[k+4>>2]=b;H[k>>2]=a;if(c){oa(c)}c=b}I:{J:{if(y){a=j-1|0;break J}a=j+2|0;if((a|0)==-1){break I}}a=H[H[h+12>>2]+(a<<2)>>2];if((a|0)==-1){break I}j=a+((a>>>0)%3|0?-1:2)|0;if((d|0)==(j|0)){break I}if((j|0)!=-1){continue}}break}g=H[h>>2];break A}wa();v()}c=H[q+(p<<2)>>2];b=e;a=p}if((t|0)!=-1){H[q+(t<<2)>>2]=-1}if((c|0)!=-1){H[q+(c<<2)>>2]=-1}H[q+(b<<2)>>2]=-1;H[q+(a<<2)>>2]=-1;o=1}i=i+1|0;c=H[h+4>>2];if(i>>>0>2>>>0){continue}break}if(o){continue}}break}a=H[k>>2];if(a){oa(a)}a=H[k+16>>2];if(a){oa(a)}ca=k+32|0;n=0;g=ca-32|0;ca=g;e=H[C+12>>2];H[h+36>>2]=e;p=h+24|0;b=H[h+24>>2];a=H[h+28>>2]-b>>2;K:{L:{if(a>>>0>>0){qb(p,e-a|0,13652);H[g+24>>2]=0;H[g+16>>2]=0;H[g+20>>2]=0;break L}if(a>>>0>e>>>0){H[h+28>>2]=b+(e<<2)}H[g+24>>2]=0;H[g+16>>2]=0;H[g+20>>2]=0;if(!e){break K}}if((e|0)<0){break i}a=(e-1>>>5|0)+1|0;b=pa(a<<2);H[g+24>>2]=a;H[g+20>>2]=0;H[g+16>>2]=b;Mc(g+16|0,e)}a=H[h>>2];b=H[h+4>>2];H[g+8>>2]=0;H[g>>2]=0;H[g+4>>2]=0;M:{if((a|0)==(b|0)){a=b}else{a=b-a|0;if((a|0)<0){break i}a=a>>2;b=(a-1>>>5|0)+1|0;c=pa(b<<2);H[g+8>>2]=b;H[g+4>>2]=0;H[g>>2]=c;Mc(g,a);b=H[h>>2];a=H[h+4>>2]}if(a-b>>>0<12){break M}N:{while(1){q=N(n,3);d=(q<<2)+b|0;f=H[d>>2];c=-1;i=q+1|0;if((i|0)!=-1){c=H[(i<<2)+b>>2]}O:{if((c|0)==(f|0)){break O}i=f;f=H[d+8>>2];if((i|0)==(f|0)|(c|0)==(f|0)){break O}k=0;i=H[g>>2];while(1){f=k+q|0;if(!(H[(f>>>3&536870908)+i>>2]>>>f&1)){a=H[(f<<2)+b>>2];c=1<>2];b=a>>>5|0;i=H[d+(b<<2)>>2];t=c&i;if(t){c=H[h+28>>2];P:{if((c|0)!=H[h+32>>2]){H[c>>2]=-1;H[h+28>>2]=c+4;break P}i=H[p>>2];b=c-i|0;o=b>>2;d=o+1|0;if(d>>>0>=1073741824){break i}j=b>>>1|0;j=b>>>0>=2147483644?1073741823:d>>>0>>0?j:d;if(j){if(j>>>0>=1073741824){break N}b=pa(j<<2)}else{b=0}d=b+(o<<2)|0;H[d>>2]=-1;o=d+4|0;if((c|0)!=(i|0)){while(1){d=d-4|0;c=c-4|0;H[d>>2]=H[c>>2];if((c|0)!=(i|0)){continue}break}}H[h+32>>2]=b+(j<<2);H[h+28>>2]=o;H[h+24>>2]=d;if(!i){break P}oa(i)}c=H[h+52>>2];Q:{if((c|0)!=H[h+56>>2]){H[c>>2]=a;H[h+52>>2]=c+4;break Q}i=H[h+48>>2];b=c-i|0;o=b>>2;d=o+1|0;if(d>>>0>=1073741824){break i}j=b>>>1|0;j=b>>>0>=2147483644?1073741823:d>>>0>>0?j:d;if(j){if(j>>>0>=1073741824){break N}b=pa(j<<2)}else{b=0}d=b+(o<<2)|0;H[d>>2]=a;a=d+4|0;if((c|0)!=(i|0)){while(1){d=d-4|0;c=c-4|0;H[d>>2]=H[c>>2];if((c|0)!=(i|0)){continue}break}}H[h+56>>2]=b+(j<<2);H[h+52>>2]=a;H[h+48>>2]=d;if(!i){break Q}oa(i)}c=H[g+20>>2];a=H[g+24>>2];if((c|0)==a<<5){if((c+1|0)<0){break i}b=g+16|0;if(c>>>0<=1073741822){a=a<<6;c=(c&-32)+32|0;a=a>>>0>c>>>0?a:c}else{a=2147483647}pb(b,a);c=H[g+20>>2]}H[g+20>>2]=c+1;d=H[g+16>>2];a=d+(c>>>3&536870908)|0;b=H[a>>2];M=a,O=Vj(c)&b,H[M>>2]=O;c=1<>>5|0;i=H[(b<<2)+d>>2];a=e;e=a+1|0}H[(b<<2)+d>>2]=c|i;o=H[h+24>>2]+(a<<2)|0;j=H[h+12>>2];b=H[h>>2];i=H[g>>2];c=f;R:{S:{T:{while(1){if((c|0)==-1){break T}d=(c>>>3&536870908)+i|0;H[d>>2]=H[d>>2]|1<>2]=c;if(t){H[(c<<2)+b>>2]=a}w=c+1|0;c=(w>>>0)%3|0?w:c-2|0;d=-1;U:{if((c|0)==-1){break U}c=H[j+(c<<2)>>2];d=-1;if((c|0)==-1){break U}d=c+1|0;d=(d>>>0)%3|0?d:c-2|0}c=d;if((f|0)!=(c|0)){continue}break}if((f|0)!=-1){break R}c=1;break S}if((f>>>0)%3|0){c=f-1|0;break S}c=f+2|0;if((c|0)==-1){break R}}c=H[j+(c<<2)>>2];if((c|0)==-1){break R}V:{if((c>>>0)%3|0){c=c-1|0;break V}c=c+2|0;if((c|0)==-1){break R}}f=H[h+12>>2];b=H[h>>2];while(1){d=(c>>>3&536870908)+i|0;H[d>>2]=H[d>>2]|1<>2]=a}W:{if((c>>>0)%3|0){c=c-1|0;break W}c=c+2|0;if((c|0)==-1){break R}}c=H[f+(c<<2)>>2];if((c|0)==-1){break R}c=c+((c>>>0)%3|0?-1:2)|0;if((c|0)!=-1){continue}break}}}k=k+1|0;if((k|0)!=3){continue}break}b=H[h>>2];a=H[h+4>>2]}n=n+1|0;if(n>>>0<(a-b>>2>>>0)/3>>>0){continue}break}break M}wa();v()}c=0;H[h+44>>2]=0;a=H[g+16>>2];b=H[g+20>>2];if(b){e=b&31;b=(b>>>3&536870908)+a|0;d=a;i=0;while(1){if(!(H[d>>2]>>>c&1)){i=i+1|0;H[h+44>>2]=i}f=(c|0)==31;c=f?0:c+1|0;d=(f<<2)+d|0;if((b|0)!=(d|0)|(c|0)!=(e|0)){continue}break}}b=H[g>>2];if(b){oa(b);a=H[g+16>>2]}if(a){oa(a)}ca=g+32|0}ca=C+16|0;if(!x){H[E+8>>2]=0;cb(h);h=0}ca=E+16|0;a=h;break h}sa();v()}b=H[s>>2];if(!b){break d}H[s+4>>2]=b;oa(b)}ca=s+16|0;break b}sa();v()}c=H[l+4>>2];b=a;H[l+4>>2]=a;if(c){cb(c);b=H[l+4>>2]}X:{if(!b){break X}a=H[u+100>>2];c=H[u+96>>2];F[r+12|0]=0;Oa(l+56|0,(a-c|0)/12|0,r+12|0);a=H[u+100>>2];c=H[u+96>>2];if((a|0)==(c|0)){break X}while(1){if(!(H[H[l+56>>2]+(D>>>3&536870908)>>2]>>>D&1)){a=N(D,3);Gc(l,0,a);c=H[l+8>>2];e=H[l+12>>2];Gc(l,1,a+1|0);f=H[l+20>>2];d=H[l+24>>2];Gc(l,2,a+2|0);n=(c|0)==(e|0)?-1:0;a=d-f>>2;c=e-c>>2;e=a>>>0>c>>>0;c=H[l+36>>2]-H[l+32>>2]>>2>>>0>(e?a:c)>>>0?2:e?1:n;Y:{if(H[l+68>>2]<=0){break Y}H[r+12>>2]=H[l+76>>2];H[r+8>>2]=m;bb(r+8|0,r+12|0);a=H[((c<<2)+l|0)+44>>2];if((a|0)<0){a=-1}else{e=(a>>>0)/3|0;a=H[(H[H[l>>2]+96>>2]+N(e,12)|0)+(a-N(e,3)<<2)>>2]}H[r+12>>2]=a;H[r+8>>2]=m;bb(r+8|0,r+12|0);e=H[l+72>>2];H[l+72>>2]=e+2;if(!(e&1)){break Y}H[r+12>>2]=a;H[r+8>>2]=m;bb(r+8|0,r+12|0);H[l+72>>2]=H[l+72>>2]+1}d=0;e=ca-16|0;ca=e;H[l+68>>2]=H[l+68>>2]+1;a=N(c,12)+l|0;a=H[a+12>>2]-H[a+8>>2]|0;if((a|0)>0){a=a>>>2|0;h=a>>>0<=1?1:a;c=H[((c<<2)+l|0)+44>>2];while(1){a=c;f=(a>>>0)/3|0;c=(a|0)==-1;g=c?-1:f;i=H[l+56>>2]+(g>>>3&536870908)|0;H[i>>2]=H[i>>2]|1<>2]=H[l+72>>2]+1;Z:{_:{$:{aa:{ba:{if(!d){ca:{if((a|0)>=0){H[e+12>>2]=H[(H[H[l>>2]+96>>2]+N(f,12)|0)+((a>>>0)%3<<2)>>2];H[e+8>>2]=m;bb(e+8|0,e+12|0);break ca}H[e+12>>2]=-1;H[e+8>>2]=m;bb(e+8|0,e+12|0);if(c){break ba}}c=-1;f=a+1|0;f=(f>>>0)%3|0?f:a-2|0;if((f|0)>=0){g=(f>>>0)/3|0;f=H[(H[H[l>>2]+96>>2]+N(g,12)|0)+(f-N(g,3)<<2)>>2]}else{f=-1}H[e+12>>2]=f;H[e+8>>2]=m;bb(e+8|0,e+12|0);f=((a>>>0)%3|0?-1:2)+a|0;if((f|0)<0){break aa}c=(f>>>0)/3|0;c=H[(H[H[l>>2]+96>>2]+N(c,12)|0)+(f-N(c,3)<<2)>>2];break aa}c=(a|0)<0?-1:H[(H[H[l>>2]+96>>2]+N(f,12)|0)+((a>>>0)%3<<2)>>2];H[l+76>>2]=c;H[e+12>>2]=c;H[e+8>>2]=m;bb(e+8|0,e+12|0);if(d&1){c=-1;if((a|0)==-1){break Z}if((N(f,3)|0)!=(a|0)){a=a-1|0;break _}a=a+2|0;break $}c=-1;if((a|0)==-1){break Z}c=a+1|0;a=(c>>>0)%3|0?c:a-2|0;break $}c=-1;H[e+12>>2]=-1;H[e+8>>2]=m;bb(e+8|0,e+12|0)}H[l+76>>2]=c;H[e+12>>2]=c;H[e+8>>2]=m;bb(e+8|0,e+12|0)}c=-1;if((a|0)==-1){break Z}}c=H[H[H[l+4>>2]+12>>2]+(a<<2)>>2]}d=d+1|0;if((h|0)!=(d|0)){continue}break}}ca=e+16|0;c=H[u+96>>2];a=H[u+100>>2]}D=D+1|0;if(D>>>0<(a-c|0)/12>>>0){continue}break}}ca=r+16|0;da:{if(b){a=H[B>>2];if(a){H[B+4>>2]=a;oa(a)}H[B>>2]=H[m>>2];H[B+4>>2]=H[m+4>>2];H[B+8>>2]=H[m+8>>2];L=H[m+84>>2];break da}a=H[m>>2];if(!a){break da}H[m+4>>2]=a;oa(a)}a=H[m+72>>2];if(a){oa(a)}a=H[m+48>>2];if(a){H[m+52>>2]=a;oa(a)}a=H[m+36>>2];if(a){H[m+40>>2]=a;oa(a)}a=H[m+24>>2];if(a){H[m+28>>2]=a;oa(a)}a=H[m+20>>2];H[m+20>>2]=0;if(a){cb(a)}ca=m+96|0;return L|0}function qg(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;var g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0;i=b;a=0;b=0;a:{b:{switch(d-1|0){case 0:j=H[i+80>>2];h=I[c+24|0];c:{if((N(j,h)|0)!=(e|0)){break c}d=H[c+28>>2]!=1;b=I[c+84|0];if(!(d|!b)){qa(f,H[H[c>>2]>>2]+H[c+48>>2]|0,e);b=1;break c}if(h){a=pa(h);ra(a,0,h)}d:{if(!j){b=1;break d}if(!d){if(h){d=0;e=0;while(1){i=d+f|0;k=H[H[c>>2]>>2];m=H[c+48>>2];g=H[c+40>>2];b=Rj(g,H[c+44>>2],I[c+84|0]?e:H[H[c+68>>2]+(e<<2)>>2],0);n=b;b=b+m|0;qa(i,qa(a,b+k|0,g),h);d=d+h|0;b=1;e=e+1|0;if((j|0)!=(e|0)){continue}break}break d}if(b){b=1;h=H[c>>2];e=H[c+48>>2];f=H[c+40>>2];i=H[c+44>>2];if((j|0)!=1){g=j&-2;c=0;d=0;while(1){k=H[h>>2];m=Rj(f,i,c,0)+e|0;k=qa(a,k+m|0,f);m=H[h>>2];n=Rj(f,i,c|1,0)+e|0;qa(k,m+n|0,f);c=c+2|0;d=d+2|0;if((g|0)!=(d|0)){continue}break}g=c}if(!(j&1)){break d}c=H[h>>2];d=Rj(g,0,f,i)+e|0;qa(a,c+d|0,f);break d}b=1;h=H[c>>2];e=H[c+48>>2];g=H[c+68>>2];f=H[c+40>>2];i=H[c+44>>2];c=0;if((j|0)!=1){k=j&-2;d=0;while(1){m=H[h>>2];n=c<<2;l=Rj(f,i,H[n+g>>2],0)+e|0;m=qa(a,m+l|0,f);l=H[h>>2];n=Rj(f,i,H[g+(n|4)>>2],0)+e|0;qa(m,l+n|0,f);c=c+2|0;d=d+2|0;if((k|0)!=(d|0)){continue}break}}if(!(j&1)){break d}d=H[h>>2];c=Rj(f,i,H[g+(c<<2)>>2],0)+e|0;qa(a,c+d|0,f);break d}b=0;if(!h){d=0;while(1){if(!ic(c,I[c+84|0]?d:H[H[c+68>>2]+(d<<2)>>2],F[c+24|0],a)){break d}d=d+1|0;b=j>>>0<=d>>>0;if((d|0)!=(j|0)){continue}break}break d}d=0;e=0;while(1){if(!ic(c,I[c+84|0]?e:H[H[c+68>>2]+(e<<2)>>2],F[c+24|0],a)){break d}qa(d+f|0,a,h);d=d+h|0;e=e+1|0;b=j>>>0<=e>>>0;if((e|0)!=(j|0)){continue}break}}if(!a){break c}oa(a)}break a;case 2:n=I[c+24|0];l=n<<1;j=H[i+80>>2];e:{if((N(l,j)|0)!=(e|0)){break e}i=H[c+28>>2]!=3;d=I[c+84|0];if(!(i|!d)){qa(f,H[H[c>>2]>>2]+H[c+48>>2]|0,e);a=1;break e}f:{if(!n){e=0;break f}e=pa(l);ra(e,0,l)}g:{if(!j){a=1;break g}if(!i){o=H[c+68>>2];k=H[c>>2];b=H[c+48>>2];i=H[c+40>>2];m=H[c+44>>2];if(n){if(!d){c=0;d=0;while(1){a=1;g=H[k>>2];p=Rj(i,m,H[o+(d<<2)>>2],0)+b|0;qa((c<<1)+f|0,qa(e,g+p|0,i),l);c=c+n|0;d=d+1|0;if((j|0)!=(d|0)){continue}break}break g}c=0;while(1){a=1;o=H[k>>2];p=Rj(g,h,i,m)+b|0;qa((c<<1)+f|0,qa(e,o+p|0,i),l);c=c+n|0;d=h;g=g+1|0;d=g?d:d+1|0;h=d;if((j|0)!=(g|0)|d){continue}break}break g}if(!d){a=1;c=0;if((j|0)!=1){f=j&-2;d=0;while(1){h=H[k>>2];g=c<<2;n=Rj(i,m,H[g+o>>2],0)+b|0;h=qa(e,h+n|0,i);n=H[k>>2];g=Rj(i,m,H[o+(g|4)>>2],0)+b|0;qa(h,g+n|0,i);c=c+2|0;d=d+2|0;if((f|0)!=(d|0)){continue}break}}if(!(j&1)){break g}d=H[k>>2];b=Rj(i,m,H[o+(c<<2)>>2],0)+b|0;qa(e,b+d|0,i);break g}n=j&1;a=1;if((j|0)!=1){j=j&-2;f=0;c=0;while(1){d=H[k>>2];l=Rj(g,h,i,m)+b|0;d=qa(e,d+l|0,i);l=H[k>>2];o=Rj(i,m,g|1,h)+b|0;qa(d,l+o|0,i);g=g+2|0;h=g>>>0<2?h+1|0:h;f=f+2|0;d=f>>>0<2?c+1|0:c;c=d;if((f|0)!=(j|0)|c){continue}break}}if(!n){break g}c=H[k>>2];b=Rj(g,h,i,m)+b|0;qa(e,b+c|0,i);break g}if(!n){d=0;while(1){if(!gc(c,I[c+84|0]?d:H[H[c+68>>2]+(d<<2)>>2],F[c+24|0],e)){break g}d=d+1|0;a=j>>>0<=d>>>0;if((d|0)!=(j|0)){continue}break}break g}d=0;while(1){if(!gc(c,I[c+84|0]?d:H[H[c+68>>2]+(d<<2)>>2],F[c+24|0],e)){break g}qa((b<<1)+f|0,e,l);b=b+n|0;d=d+1|0;a=j>>>0<=d>>>0;if((d|0)!=(j|0)){continue}break}}if(!e){break e}oa(e)}b=a;break a;case 4:l=I[c+24|0];o=l<<2;j=H[i+80>>2];h:{if((N(o,j)|0)!=(e|0)){break h}i=H[c+28>>2]!=5;d=I[c+84|0];if(!(i|!d)){qa(f,H[H[c>>2]>>2]+H[c+48>>2]|0,e);b=1;break h}i:{if(!l){e=0;break i}e=pa(o);ra(e,0,o)}b=1;j:{if(!j){break j}if(!i){a=H[c+68>>2];m=H[c>>2];i=H[c+48>>2];k=H[c+40>>2];n=H[c+44>>2];if(l){if(!d){c=0;d=0;while(1){g=H[m>>2];p=Rj(k,n,H[a+(d<<2)>>2],0)+i|0;qa((c<<2)+f|0,qa(e,g+p|0,k),o);c=c+l|0;d=d+1|0;if((j|0)!=(d|0)){continue}break}break j}c=0;while(1){d=H[m>>2];p=Rj(g,h,k,n)+i|0;qa((c<<2)+f|0,qa(e,d+p|0,k),o);c=c+l|0;g=g+1|0;a=g?h:h+1|0;h=a;if((j|0)!=(g|0)|h){continue}break}break j}if(!d){c=0;if((j|0)!=1){f=j&-2;d=0;while(1){h=H[m>>2];g=c<<2;l=Rj(k,n,H[g+a>>2],0)+i|0;h=qa(e,h+l|0,k);l=H[m>>2];g=Rj(k,n,H[a+(g|4)>>2],0)+i|0;qa(h,g+l|0,k);c=c+2|0;d=d+2|0;if((f|0)!=(d|0)){continue}break}}if(!(j&1)){break j}d=H[m>>2];a=Rj(k,n,H[a+(c<<2)>>2],0)+i|0;qa(e,a+d|0,k);break j}l=j&1;if((j|0)!=1){j=j&-2;f=0;c=0;while(1){a=H[m>>2];d=Rj(g,h,k,n)+i|0;a=qa(e,a+d|0,k);d=H[m>>2];o=Rj(k,n,g|1,h)+i|0;qa(a,d+o|0,k);d=h;g=g+2|0;h=g>>>0<2?d+1|0:d;f=f+2|0;a=f>>>0<2?c+1|0:c;c=a;if((f|0)!=(j|0)|c){continue}break}}if(!l){break j}a=H[m>>2];c=Rj(g,h,k,n)+i|0;qa(e,a+c|0,k);break j}b=0;if(!l){d=0;while(1){if(!ec(c,I[c+84|0]?d:H[H[c+68>>2]+(d<<2)>>2],F[c+24|0],e)){break j}d=d+1|0;b=j>>>0<=d>>>0;if((d|0)!=(j|0)){continue}break}break j}d=0;while(1){if(!ec(c,I[c+84|0]?d:H[H[c+68>>2]+(d<<2)>>2],F[c+24|0],e)){break j}qa((a<<2)+f|0,e,o);a=a+l|0;d=d+1|0;b=j>>>0<=d>>>0;if((d|0)!=(j|0)){continue}break}}if(!e){break h}oa(e)}break a;case 1:j=H[i+80>>2];h=I[c+24|0];k:{if((N(j,h)|0)!=(e|0)){break k}d=H[c+28>>2]!=2;b=I[c+84|0];if(!(d|!b)){qa(f,H[H[c>>2]>>2]+H[c+48>>2]|0,e);b=1;break k}if(h){a=pa(h);ra(a,0,h)}l:{if(!j){b=1;break l}if(!d){if(h){d=0;e=0;while(1){i=d+f|0;k=H[H[c>>2]>>2];m=H[c+48>>2];g=H[c+40>>2];b=Rj(g,H[c+44>>2],I[c+84|0]?e:H[H[c+68>>2]+(e<<2)>>2],0);n=b;b=b+m|0;qa(i,qa(a,b+k|0,g),h);d=d+h|0;b=1;e=e+1|0;if((j|0)!=(e|0)){continue}break}break l}if(b){b=1;h=H[c>>2];e=H[c+48>>2];f=H[c+40>>2];i=H[c+44>>2];if((j|0)!=1){g=j&-2;c=0;d=0;while(1){k=H[h>>2];m=Rj(f,i,c,0)+e|0;k=qa(a,k+m|0,f);m=H[h>>2];n=Rj(f,i,c|1,0)+e|0;qa(k,m+n|0,f);c=c+2|0;d=d+2|0;if((g|0)!=(d|0)){continue}break}g=c}if(!(j&1)){break l}c=H[h>>2];d=Rj(g,0,f,i)+e|0;qa(a,c+d|0,f);break l}b=1;h=H[c>>2];e=H[c+48>>2];g=H[c+68>>2];f=H[c+40>>2];i=H[c+44>>2];c=0;if((j|0)!=1){k=j&-2;d=0;while(1){m=H[h>>2];n=c<<2;l=Rj(f,i,H[n+g>>2],0)+e|0;m=qa(a,m+l|0,f);l=H[h>>2];n=Rj(f,i,H[g+(n|4)>>2],0)+e|0;qa(m,l+n|0,f);c=c+2|0;d=d+2|0;if((k|0)!=(d|0)){continue}break}}if(!(j&1)){break l}d=H[h>>2];c=Rj(f,i,H[g+(c<<2)>>2],0)+e|0;qa(a,c+d|0,f);break l}b=0;if(!h){d=0;while(1){if(!hc(c,I[c+84|0]?d:H[H[c+68>>2]+(d<<2)>>2],F[c+24|0],a)){break l}d=d+1|0;b=j>>>0<=d>>>0;if((d|0)!=(j|0)){continue}break}break l}d=0;e=0;while(1){if(!hc(c,I[c+84|0]?e:H[H[c+68>>2]+(e<<2)>>2],F[c+24|0],a)){break l}qa(d+f|0,a,h);d=d+h|0;e=e+1|0;b=j>>>0<=e>>>0;if((e|0)!=(j|0)){continue}break}}if(!a){break k}oa(a)}break a;case 3:n=I[c+24|0];l=n<<1;j=H[i+80>>2];m:{if((N(l,j)|0)!=(e|0)){break m}i=H[c+28>>2]!=4;d=I[c+84|0];if(!(i|!d)){qa(f,H[H[c>>2]>>2]+H[c+48>>2]|0,e);a=1;break m}n:{if(!n){e=0;break n}e=pa(l);ra(e,0,l)}o:{if(!j){a=1;break o}if(!i){o=H[c+68>>2];k=H[c>>2];b=H[c+48>>2];i=H[c+40>>2];m=H[c+44>>2];if(n){if(!d){c=0;d=0;while(1){a=1;g=H[k>>2];p=Rj(i,m,H[o+(d<<2)>>2],0)+b|0;qa((c<<1)+f|0,qa(e,g+p|0,i),l);c=c+n|0;d=d+1|0;if((j|0)!=(d|0)){continue}break}break o}c=0;while(1){a=1;o=H[k>>2];p=Rj(g,h,i,m)+b|0;qa((c<<1)+f|0,qa(e,o+p|0,i),l);c=c+n|0;d=h;g=g+1|0;d=g?d:d+1|0;h=d;if((j|0)!=(g|0)|d){continue}break}break o}if(!d){a=1;c=0;if((j|0)!=1){f=j&-2;d=0;while(1){h=H[k>>2];g=c<<2;n=Rj(i,m,H[g+o>>2],0)+b|0;h=qa(e,h+n|0,i);n=H[k>>2];g=Rj(i,m,H[o+(g|4)>>2],0)+b|0;qa(h,g+n|0,i);c=c+2|0;d=d+2|0;if((f|0)!=(d|0)){continue}break}}if(!(j&1)){break o}d=H[k>>2];b=Rj(i,m,H[o+(c<<2)>>2],0)+b|0;qa(e,b+d|0,i);break o}n=j&1;a=1;if((j|0)!=1){j=j&-2;f=0;c=0;while(1){d=H[k>>2];l=Rj(g,h,i,m)+b|0;d=qa(e,d+l|0,i);l=H[k>>2];o=Rj(i,m,g|1,h)+b|0;qa(d,l+o|0,i);g=g+2|0;h=g>>>0<2?h+1|0:h;f=f+2|0;d=f>>>0<2?c+1|0:c;c=d;if((f|0)!=(j|0)|c){continue}break}}if(!n){break o}c=H[k>>2];b=Rj(g,h,i,m)+b|0;qa(e,b+c|0,i);break o}if(!n){d=0;while(1){if(!fc(c,I[c+84|0]?d:H[H[c+68>>2]+(d<<2)>>2],F[c+24|0],e)){break o}d=d+1|0;a=j>>>0<=d>>>0;if((d|0)!=(j|0)){continue}break}break o}d=0;while(1){if(!fc(c,I[c+84|0]?d:H[H[c+68>>2]+(d<<2)>>2],F[c+24|0],e)){break o}qa((b<<1)+f|0,e,l);b=b+n|0;d=d+1|0;a=j>>>0<=d>>>0;if((d|0)!=(j|0)){continue}break}}if(!e){break m}oa(e)}b=a;break a;case 5:l=I[c+24|0];o=l<<2;j=H[i+80>>2];p:{if((N(o,j)|0)!=(e|0)){break p}i=H[c+28>>2]!=6;d=I[c+84|0];if(!(i|!d)){qa(f,H[H[c>>2]>>2]+H[c+48>>2]|0,e);b=1;break p}q:{if(!l){e=0;break q}e=pa(o);ra(e,0,o)}b=1;r:{if(!j){break r}if(!i){a=H[c+68>>2];m=H[c>>2];i=H[c+48>>2];k=H[c+40>>2];n=H[c+44>>2];if(l){if(!d){c=0;d=0;while(1){g=H[m>>2];p=Rj(k,n,H[a+(d<<2)>>2],0)+i|0;qa((c<<2)+f|0,qa(e,g+p|0,k),o);c=c+l|0;d=d+1|0;if((j|0)!=(d|0)){continue}break}break r}c=0;while(1){d=H[m>>2];p=Rj(g,h,k,n)+i|0;qa((c<<2)+f|0,qa(e,d+p|0,k),o);c=c+l|0;g=g+1|0;a=g?h:h+1|0;h=a;if((j|0)!=(g|0)|h){continue}break}break r}if(!d){c=0;if((j|0)!=1){f=j&-2;d=0;while(1){h=H[m>>2];g=c<<2;l=Rj(k,n,H[g+a>>2],0)+i|0;h=qa(e,h+l|0,k);l=H[m>>2];g=Rj(k,n,H[a+(g|4)>>2],0)+i|0;qa(h,g+l|0,k);c=c+2|0;d=d+2|0;if((f|0)!=(d|0)){continue}break}}if(!(j&1)){break r}d=H[m>>2];a=Rj(k,n,H[a+(c<<2)>>2],0)+i|0;qa(e,a+d|0,k);break r}l=j&1;if((j|0)!=1){j=j&-2;f=0;c=0;while(1){a=H[m>>2];d=Rj(g,h,k,n)+i|0;a=qa(e,a+d|0,k);d=H[m>>2];o=Rj(k,n,g|1,h)+i|0;qa(a,d+o|0,k);d=h;g=g+2|0;h=g>>>0<2?d+1|0:d;f=f+2|0;a=f>>>0<2?c+1|0:c;c=a;if((f|0)!=(j|0)|c){continue}break}}if(!l){break r}a=H[m>>2];c=Rj(g,h,k,n)+i|0;qa(e,a+c|0,k);break r}b=0;if(!l){d=0;while(1){if(!dc(c,I[c+84|0]?d:H[H[c+68>>2]+(d<<2)>>2],F[c+24|0],e)){break r}d=d+1|0;b=j>>>0<=d>>>0;if((d|0)!=(j|0)){continue}break}break r}d=0;while(1){if(!dc(c,I[c+84|0]?d:H[H[c+68>>2]+(d<<2)>>2],F[c+24|0],e)){break r}qa((a<<2)+f|0,e,o);a=a+l|0;d=d+1|0;b=j>>>0<=d>>>0;if((d|0)!=(j|0)){continue}break}}if(!e){break p}oa(e)}break a;case 8:p=I[c+24|0];q=p<<2;k=H[i+80>>2];s:{if((N(q,k)|0)!=(e|0)){break s}i=H[c+28>>2];t:{if(!p){break t}a=pa(q);d=a;m=q-4|0;l=(m>>>2|0)+1&7;if(l){e=0;while(1){H[d>>2]=-1073741824;d=d+4|0;e=e+1|0;if((l|0)!=(e|0)){continue}break}}if(m>>>0<28){break t}e=(p<<2)+a|0;while(1){H[d+24>>2]=-1073741824;H[d+28>>2]=-1073741824;H[d+16>>2]=-1073741824;H[d+20>>2]=-1073741824;H[d+8>>2]=-1073741824;H[d+12>>2]=-1073741824;H[d>>2]=-1073741824;H[d+4>>2]=-1073741824;d=d+32|0;if((e|0)!=(d|0)){continue}break}}u:{if(!k){b=1;break u}if((i|0)==9){r=H[c+68>>2];l=H[c>>2];i=H[c+48>>2];s=I[c+84|0];m=H[c+44>>2];c=H[c+40>>2];o=c;if(p){e=0;d=0;while(1){h=(e<<2)+f|0;g=H[l>>2];b=Rj(c,m,s?d:H[r+(d<<2)>>2],0)+i|0;qa(h,qa(a,b+g|0,o),q);e=e+p|0;b=1;d=d+1|0;if((k|0)!=(d|0)){continue}break}break u}if(!s){b=1;d=0;if((k|0)!=1){f=k&-2;e=0;while(1){h=H[l>>2];g=d<<2;j=Rj(c,m,H[g+r>>2],0)+i|0;h=qa(a,h+j|0,o);j=H[l>>2];g=Rj(c,m,H[r+(g|4)>>2],0)+i|0;qa(h,j+g|0,o);d=d+2|0;e=e+2|0;if((f|0)!=(e|0)){continue}break}}if(!(k&1)){break u}e=H[l>>2];c=Rj(c,m,H[r+(d<<2)>>2],0)+i|0;qa(a,c+e|0,o);break u}f=k&1;b=1;if((k|0)!=1){k=k&-2;while(1){d=H[l>>2];e=Rj(g,h,c,m)+i|0;d=qa(a,d+e|0,o);e=H[l>>2];p=Rj(c,m,g|1,h)+i|0;qa(d,e+p|0,o);g=g+2|0;h=g>>>0<2?h+1|0:h;d=j;e=n+2|0;d=e>>>0<2?d+1|0:d;n=e;j=d;if((e|0)!=(k|0)|d){continue}break}}if(!f){break u}d=H[l>>2];c=Rj(g,h,c,m)+i|0;qa(a,c+d|0,o);break u}if(!p){d=0;while(1){if(!Va(c,I[c+84|0]?d:H[H[c+68>>2]+(d<<2)>>2],F[c+24|0],a)){break u}d=d+1|0;b=k>>>0<=d>>>0;if((d|0)!=(k|0)){continue}break}break u}e=0;d=0;while(1){if(!Va(c,I[c+84|0]?d:H[H[c+68>>2]+(d<<2)>>2],F[c+24|0],a)){break u}qa((e<<2)+f|0,a,q);e=e+p|0;d=d+1|0;b=k>>>0<=d>>>0;if((d|0)!=(k|0)){continue}break}}if(!a){break s}oa(a)}a=b;break;default:break b}}b=a}return b|0}function ef(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0;i=ca-48|0;ca=i;a:{b:{if((c|0)!=1){break b}c=H[a+4>>2];g=H[a+12>>2];H[i+40>>2]=0;a=i;H[a+32>>2]=0;H[a+36>>2]=0;H[a+24>>2]=0;H[a+28>>2]=0;H[a+16>>2]=0;H[a+20>>2]=0;H[a+8>>2]=0;H[a+12>>2]=0;e=a+8|0;c:{if((b|0)==-2){break c}l=H[H[H[c+4>>2]+8>>2]+(g<<2)>>2];if((ea[H[H[c>>2]+8>>2]](c)|0)==1){a=J[c+36>>1];j=(a<<8|a>>>8)&65535;a=0;h=ca-32|0;ca=h;d=H[H[H[c+4>>2]+8>>2]+(g<<2)>>2];d:{if((ea[H[H[c>>2]+8>>2]](c)|0)!=1|b-1>>>0>5){break d}k=ea[H[H[c>>2]+36>>2]](c)|0;f=ea[H[H[c>>2]+44>>2]](c,g)|0;if(!k|!f){break d}a=ea[H[H[c>>2]+40>>2]](c,g)|0;if(a){c=H[c+44>>2];H[h+12>>2]=a;H[h+8>>2]=c;H[h+20>>2]=f;H[h+16>>2]=f+12;c=h+8|0;a=0;e:{f:{switch(b-1|0){case 0:a=pa(60);H[a+4>>2]=d;H[a>>2]=3272;b=H[e+4>>2];H[a+8>>2]=H[e>>2];H[a+12>>2]=b;b=H[e+12>>2];H[a+16>>2]=H[e+8>>2];H[a+20>>2]=b;b=H[e+20>>2];H[a+24>>2]=H[e+16>>2];H[a+28>>2]=b;H[a+40>>2]=0;H[a+32>>2]=0;H[a+36>>2]=0;d=H[e+24>>2];f=H[e+28>>2];if((d|0)!=(f|0)){g=f-d|0;if((g|0)<0){break a}b=pa(g);H[a+32>>2]=b;H[a+40>>2]=(g&-4)+b;while(1){H[b>>2]=H[d>>2];b=b+4|0;d=d+4|0;if((f|0)!=(d|0)){continue}break}H[a+36>>2]=b}b=H[c+4>>2];H[a+44>>2]=H[c>>2];H[a+48>>2]=b;b=H[c+12>>2];H[a+52>>2]=H[c+8>>2];H[a+56>>2]=b;H[a>>2]=2564;break e;case 1:a=pa(60);H[a+4>>2]=d;H[a>>2]=3272;b=H[e+4>>2];H[a+8>>2]=H[e>>2];H[a+12>>2]=b;b=H[e+12>>2];H[a+16>>2]=H[e+8>>2];H[a+20>>2]=b;b=H[e+20>>2];H[a+24>>2]=H[e+16>>2];H[a+28>>2]=b;H[a+40>>2]=0;H[a+32>>2]=0;H[a+36>>2]=0;d=H[e+24>>2];f=H[e+28>>2];if((d|0)!=(f|0)){g=f-d|0;if((g|0)<0){break a}b=pa(g);H[a+32>>2]=b;H[a+40>>2]=(g&-4)+b;while(1){H[b>>2]=H[d>>2];b=b+4|0;d=d+4|0;if((f|0)!=(d|0)){continue}break}H[a+36>>2]=b}b=H[c+4>>2];H[a+44>>2]=H[c>>2];H[a+48>>2]=b;b=H[c+12>>2];H[a+52>>2]=H[c+8>>2];H[a+56>>2]=b;H[a>>2]=3328;break e;case 3:a=pa(112);H[a+4>>2]=d;H[a>>2]=3272;b=H[e+4>>2];H[a+8>>2]=H[e>>2];H[a+12>>2]=b;b=H[e+12>>2];H[a+16>>2]=H[e+8>>2];H[a+20>>2]=b;b=H[e+20>>2];H[a+24>>2]=H[e+16>>2];H[a+28>>2]=b;H[a+40>>2]=0;H[a+32>>2]=0;H[a+36>>2]=0;d=H[e+24>>2];f=H[e+28>>2];if((d|0)!=(f|0)){g=f-d|0;if((g|0)<0){break a}b=pa(g);H[a+32>>2]=b;H[a+40>>2]=(g&-4)+b;while(1){H[b>>2]=H[d>>2];b=b+4|0;d=d+4|0;if((f|0)!=(d|0)){continue}break}H[a+36>>2]=b}b=H[c+4>>2];H[a+44>>2]=H[c>>2];H[a+48>>2]=b;b=H[c+12>>2];H[a+52>>2]=H[c+8>>2];H[a+56>>2]=b;H[a+60>>2]=0;H[a+64>>2]=0;H[a>>2]=3564;H[a+68>>2]=0;H[a+72>>2]=0;H[a+76>>2]=0;H[a+80>>2]=0;H[a+84>>2]=0;H[a+88>>2]=0;H[a+92>>2]=0;H[a+96>>2]=0;H[a+100>>2]=0;H[a+104>>2]=0;H[a+108>>2]=0;break e;case 2:a=pa(92);H[a+4>>2]=d;H[a>>2]=3272;b=H[e+4>>2];H[a+8>>2]=H[e>>2];H[a+12>>2]=b;b=H[e+12>>2];H[a+16>>2]=H[e+8>>2];H[a+20>>2]=b;b=H[e+20>>2];H[a+24>>2]=H[e+16>>2];H[a+28>>2]=b;H[a+40>>2]=0;H[a+32>>2]=0;H[a+36>>2]=0;d=H[e+24>>2];f=H[e+28>>2];if((d|0)!=(f|0)){g=f-d|0;if((g|0)<0){break a}b=pa(g);H[a+32>>2]=b;H[a+40>>2]=(g&-4)+b;while(1){H[b>>2]=H[d>>2];b=b+4|0;d=d+4|0;if((f|0)!=(d|0)){continue}break}H[a+36>>2]=b}b=H[c+4>>2];H[a+44>>2]=H[c>>2];H[a+48>>2]=b;b=H[c+12>>2];H[a+52>>2]=H[c+8>>2];H[a+56>>2]=b;H[a+60>>2]=0;H[a+64>>2]=0;H[a>>2]=3812;H[a+68>>2]=0;H[a+72>>2]=0;H[a+76>>2]=0;H[a+80>>2]=0;H[a+84>>2]=0;H[a+88>>2]=j;break e;case 4:a=pa(104);H[a+4>>2]=d;H[a>>2]=3272;b=H[e+4>>2];H[a+8>>2]=H[e>>2];H[a+12>>2]=b;b=H[e+12>>2];H[a+16>>2]=H[e+8>>2];H[a+20>>2]=b;b=H[e+20>>2];H[a+24>>2]=H[e+16>>2];H[a+28>>2]=b;H[a+40>>2]=0;H[a+32>>2]=0;H[a+36>>2]=0;d=H[e+24>>2];f=H[e+28>>2];if((d|0)!=(f|0)){g=f-d|0;if((g|0)<0){break a}b=pa(g);H[a+32>>2]=b;H[a+40>>2]=(g&-4)+b;while(1){H[b>>2]=H[d>>2];b=b+4|0;d=d+4|0;if((f|0)!=(d|0)){continue}break}H[a+36>>2]=b}b=H[c+4>>2];H[a+44>>2]=H[c>>2];H[a+48>>2]=b;b=H[c+12>>2];H[a+52>>2]=H[c+8>>2];H[a+56>>2]=b;H[a+84>>2]=0;H[a+76>>2]=0;H[a+80>>2]=0;H[a+60>>2]=0;H[a+64>>2]=0;H[a>>2]=4040;b=H[c+4>>2];H[a+88>>2]=H[c>>2];H[a+92>>2]=b;b=H[c+12>>2];H[a+96>>2]=H[c+8>>2];H[a+100>>2]=b;break e;case 5:break f;default:break e}}a=pa(128);H[a+4>>2]=d;H[a>>2]=3272;b=H[e+4>>2];H[a+8>>2]=H[e>>2];H[a+12>>2]=b;b=H[e+12>>2];H[a+16>>2]=H[e+8>>2];H[a+20>>2]=b;b=H[e+20>>2];H[a+24>>2]=H[e+16>>2];H[a+28>>2]=b;H[a+40>>2]=0;H[a+32>>2]=0;H[a+36>>2]=0;g:{b=H[e+28>>2];d=H[e+24>>2];if((b|0)!=(d|0)){d=b-d|0;if((d|0)<0){break a}b=pa(d);H[a+36>>2]=b;H[a+32>>2]=b;H[a+40>>2]=(d&-4)+b;d=H[e+24>>2];f=H[e+28>>2];if((d|0)!=(f|0)){while(1){H[b>>2]=H[d>>2];b=b+4|0;d=d+4|0;if((f|0)!=(d|0)){continue}break}}H[a+36>>2]=b}H[a>>2]=3216;b=H[c+4>>2];H[a+44>>2]=H[c>>2];H[a+48>>2]=b;b=H[c+12>>2];H[a+52>>2]=H[c+8>>2];H[a+56>>2]=b;b=a- -64|0;H[b>>2]=0;H[b+4>>2]=0;H[a+60>>2]=4904;H[a>>2]=4276;b=H[c+4>>2];H[a+72>>2]=H[c>>2];H[a+76>>2]=b;b=H[c+12>>2];H[a+80>>2]=H[c+8>>2];H[a+84>>2]=b;H[a+104>>2]=1065353216;H[a+108>>2]=-1;H[a+96>>2]=-1;H[a+100>>2]=-1;H[a+88>>2]=1;H[a+92>>2]=-1;H[a+60>>2]=4512;H[a+112>>2]=0;H[a+116>>2]=0;F[a+117|0]=0;F[a+118|0]=0;F[a+119|0]=0;F[a+120|0]=0;F[a+121|0]=0;F[a+122|0]=0;F[a+123|0]=0;F[a+124|0]=0;break g}}break d}a=H[c+44>>2];H[h+12>>2]=k;H[h+8>>2]=a;H[h+20>>2]=f;H[h+16>>2]=f+12;c=h+8|0;a=0;h:{i:{switch(b-1|0){case 0:a=pa(60);H[a+4>>2]=d;H[a>>2]=3272;b=H[e+4>>2];H[a+8>>2]=H[e>>2];H[a+12>>2]=b;b=H[e+12>>2];H[a+16>>2]=H[e+8>>2];H[a+20>>2]=b;b=H[e+20>>2];H[a+24>>2]=H[e+16>>2];H[a+28>>2]=b;H[a+40>>2]=0;H[a+32>>2]=0;H[a+36>>2]=0;d=H[e+24>>2];f=H[e+28>>2];if((d|0)!=(f|0)){g=f-d|0;if((g|0)<0){break a}b=pa(g);H[a+32>>2]=b;H[a+40>>2]=(g&-4)+b;while(1){H[b>>2]=H[d>>2];b=b+4|0;d=d+4|0;if((f|0)!=(d|0)){continue}break}H[a+36>>2]=b}b=H[c+4>>2];H[a+44>>2]=H[c>>2];H[a+48>>2]=b;b=H[c+12>>2];H[a+52>>2]=H[c+8>>2];H[a+56>>2]=b;H[a>>2]=4932;break h;case 1:a=pa(60);H[a+4>>2]=d;H[a>>2]=3272;b=H[e+4>>2];H[a+8>>2]=H[e>>2];H[a+12>>2]=b;b=H[e+12>>2];H[a+16>>2]=H[e+8>>2];H[a+20>>2]=b;b=H[e+20>>2];H[a+24>>2]=H[e+16>>2];H[a+28>>2]=b;H[a+40>>2]=0;H[a+32>>2]=0;H[a+36>>2]=0;d=H[e+24>>2];f=H[e+28>>2];if((d|0)!=(f|0)){g=f-d|0;if((g|0)<0){break a}b=pa(g);H[a+32>>2]=b;H[a+40>>2]=(g&-4)+b;while(1){H[b>>2]=H[d>>2];b=b+4|0;d=d+4|0;if((f|0)!=(d|0)){continue}break}H[a+36>>2]=b}b=H[c+4>>2];H[a+44>>2]=H[c>>2];H[a+48>>2]=b;b=H[c+12>>2];H[a+52>>2]=H[c+8>>2];H[a+56>>2]=b;H[a>>2]=5356;break h;case 3:a=pa(112);H[a+4>>2]=d;H[a>>2]=3272;b=H[e+4>>2];H[a+8>>2]=H[e>>2];H[a+12>>2]=b;b=H[e+12>>2];H[a+16>>2]=H[e+8>>2];H[a+20>>2]=b;b=H[e+20>>2];H[a+24>>2]=H[e+16>>2];H[a+28>>2]=b;H[a+40>>2]=0;H[a+32>>2]=0;H[a+36>>2]=0;d=H[e+24>>2];f=H[e+28>>2];if((d|0)!=(f|0)){g=f-d|0;if((g|0)<0){break a}b=pa(g);H[a+32>>2]=b;H[a+40>>2]=(g&-4)+b;while(1){H[b>>2]=H[d>>2];b=b+4|0;d=d+4|0;if((f|0)!=(d|0)){continue}break}H[a+36>>2]=b}b=H[c+4>>2];H[a+44>>2]=H[c>>2];H[a+48>>2]=b;b=H[c+12>>2];H[a+52>>2]=H[c+8>>2];H[a+56>>2]=b;H[a+60>>2]=0;H[a+64>>2]=0;H[a>>2]=5580;H[a+68>>2]=0;H[a+72>>2]=0;H[a+76>>2]=0;H[a+80>>2]=0;H[a+84>>2]=0;H[a+88>>2]=0;H[a+92>>2]=0;H[a+96>>2]=0;H[a+100>>2]=0;H[a+104>>2]=0;H[a+108>>2]=0;break h;case 2:a=pa(92);H[a+4>>2]=d;H[a>>2]=3272;b=H[e+4>>2];H[a+8>>2]=H[e>>2];H[a+12>>2]=b;b=H[e+12>>2];H[a+16>>2]=H[e+8>>2];H[a+20>>2]=b;b=H[e+20>>2];H[a+24>>2]=H[e+16>>2];H[a+28>>2]=b;H[a+40>>2]=0;H[a+32>>2]=0;H[a+36>>2]=0;d=H[e+24>>2];f=H[e+28>>2];if((d|0)!=(f|0)){g=f-d|0;if((g|0)<0){break a}b=pa(g);H[a+32>>2]=b;H[a+40>>2]=(g&-4)+b;while(1){H[b>>2]=H[d>>2];b=b+4|0;d=d+4|0;if((f|0)!=(d|0)){continue}break}H[a+36>>2]=b}b=H[c+4>>2];H[a+44>>2]=H[c>>2];H[a+48>>2]=b;b=H[c+12>>2];H[a+52>>2]=H[c+8>>2];H[a+56>>2]=b;H[a+60>>2]=0;H[a+64>>2]=0;H[a>>2]=5816;H[a+68>>2]=0;H[a+72>>2]=0;H[a+76>>2]=0;H[a+80>>2]=0;H[a+84>>2]=0;H[a+88>>2]=j;break h;case 4:a=pa(104);H[a+4>>2]=d;H[a>>2]=3272;b=H[e+4>>2];H[a+8>>2]=H[e>>2];H[a+12>>2]=b;b=H[e+12>>2];H[a+16>>2]=H[e+8>>2];H[a+20>>2]=b;b=H[e+20>>2];H[a+24>>2]=H[e+16>>2];H[a+28>>2]=b;H[a+40>>2]=0;H[a+32>>2]=0;H[a+36>>2]=0;d=H[e+24>>2];f=H[e+28>>2];if((d|0)!=(f|0)){g=f-d|0;if((g|0)<0){break a}b=pa(g);H[a+32>>2]=b;H[a+40>>2]=(g&-4)+b;while(1){H[b>>2]=H[d>>2];b=b+4|0;d=d+4|0;if((f|0)!=(d|0)){continue}break}H[a+36>>2]=b}b=H[c+4>>2];H[a+44>>2]=H[c>>2];H[a+48>>2]=b;b=H[c+12>>2];H[a+52>>2]=H[c+8>>2];H[a+56>>2]=b;H[a+84>>2]=0;H[a+76>>2]=0;H[a+80>>2]=0;H[a+60>>2]=0;H[a+64>>2]=0;H[a>>2]=6032;b=H[c+4>>2];H[a+88>>2]=H[c>>2];H[a+92>>2]=b;b=H[c+12>>2];H[a+96>>2]=H[c+8>>2];H[a+100>>2]=b;break h;case 5:break i;default:break h}}a=pa(128);H[a+4>>2]=d;H[a>>2]=3272;b=H[e+4>>2];H[a+8>>2]=H[e>>2];H[a+12>>2]=b;b=H[e+12>>2];H[a+16>>2]=H[e+8>>2];H[a+20>>2]=b;b=H[e+20>>2];H[a+24>>2]=H[e+16>>2];H[a+28>>2]=b;H[a+40>>2]=0;H[a+32>>2]=0;H[a+36>>2]=0;j:{b=H[e+28>>2];d=H[e+24>>2];if((b|0)!=(d|0)){d=b-d|0;if((d|0)<0){break a}b=pa(d);H[a+36>>2]=b;H[a+32>>2]=b;H[a+40>>2]=(d&-4)+b;d=H[e+24>>2];f=H[e+28>>2];if((d|0)!=(f|0)){while(1){H[b>>2]=H[d>>2];b=b+4|0;d=d+4|0;if((f|0)!=(d|0)){continue}break}}H[a+36>>2]=b}H[a>>2]=5300;b=H[c+4>>2];H[a+44>>2]=H[c>>2];H[a+48>>2]=b;b=H[c+12>>2];H[a+52>>2]=H[c+8>>2];H[a+56>>2]=b;b=a- -64|0;H[b>>2]=0;H[b+4>>2]=0;H[a+60>>2]=6840;H[a>>2]=6256;b=H[c+4>>2];H[a+72>>2]=H[c>>2];H[a+76>>2]=b;b=H[c+12>>2];H[a+80>>2]=H[c+8>>2];H[a+84>>2]=b;H[a+104>>2]=1065353216;H[a+108>>2]=-1;H[a+96>>2]=-1;H[a+100>>2]=-1;H[a+88>>2]=1;H[a+92>>2]=-1;H[a+60>>2]=6476;H[a+112>>2]=0;H[a+116>>2]=0;F[a+117|0]=0;F[a+118|0]=0;F[a+119|0]=0;F[a+120|0]=0;F[a+121|0]=0;F[a+122|0]=0;F[a+123|0]=0;F[a+124|0]=0;break j}}}ca=h+32|0;d=a;if(a){break c}}d=pa(44);H[d+4>>2]=l;H[d>>2]=3272;a=H[e+4>>2];H[d+8>>2]=H[e>>2];H[d+12>>2]=a;a=H[e+12>>2];H[d+16>>2]=H[e+8>>2];H[d+20>>2]=a;a=H[e+20>>2];H[d+24>>2]=H[e+16>>2];H[d+28>>2]=a;H[d+40>>2]=0;H[d+32>>2]=0;H[d+36>>2]=0;c=H[e+24>>2];a=H[e+28>>2];if((c|0)!=(a|0)){b=a-c|0;if((b|0)<0){break a}e=pa(b);H[d+32>>2]=e;H[d+40>>2]=(b&-4)+e;while(1){H[e>>2]=H[c>>2];e=e+4|0;c=c+4|0;if((a|0)!=(c|0)){continue}break}H[d+36>>2]=e}H[d>>2]=6868;break c}e=d;a=H[i+32>>2];if(!a){break b}H[i+36>>2]=a;oa(a)}ca=i+48|0;return e|0}sa();v()}function Ec(a){a=a|0;var b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0;l=ca-16|0;ca=l;a:{b:{c:{d:{e:{f:{g:{h:{i:{if(a>>>0<=244){g=H[4298];h=a>>>0<11?16:a+11&-8;c=h>>>3|0;b=g>>>c|0;if(b&3){c=c+((b^-1)&1)|0;a=c<<3;b=a+17232|0;d=H[a+17240>>2];a=H[d+8>>2];j:{if((b|0)==(a|0)){m=17192,n=Vj(c)&g,H[m>>2]=n;break j}H[a+12>>2]=b;H[b+8>>2]=a}a=d+8|0;b=c<<3;H[d+4>>2]=b|3;b=b+d|0;H[b+4>>2]=H[b+4>>2]|1;break a}k=H[4300];if(k>>>0>=h>>>0){break i}if(b){a=2<>2];a=H[e+8>>2];k:{if((b|0)==(a|0)){g=Vj(d)&g;H[4298]=g;break k}H[a+12>>2]=b;H[b+8>>2]=a}H[e+4>>2]=h|3;c=e+h|0;a=d<<3;d=a-h|0;H[c+4>>2]=d|1;H[a+e>>2]=d;if(k){b=(k&-8)+17232|0;f=H[4303];a=1<<(k>>>3);l:{if(!(a&g)){H[4298]=a|g;a=b;break l}a=H[b+8>>2]}H[b+8>>2]=f;H[a+12>>2]=f;H[f+12>>2]=b;H[f+8>>2]=a}a=e+8|0;H[4303]=c;H[4300]=d;break a}j=H[4299];if(!j){break i}c=H[(Qj(0-j&j)<<2)+17496>>2];f=(H[c+4>>2]&-8)-h|0;b=c;while(1){m:{a=H[b+16>>2];if(!a){a=H[b+20>>2];if(!a){break m}}b=(H[a+4>>2]&-8)-h|0;d=b>>>0>>0;f=d?b:f;c=d?a:c;b=a;continue}break}i=H[c+24>>2];d=H[c+12>>2];if((d|0)!=(c|0)){a=H[c+8>>2];H[a+12>>2]=d;H[d+8>>2]=a;break b}b=c+20|0;a=H[b>>2];if(!a){a=H[c+16>>2];if(!a){break h}b=c+16|0}while(1){e=b;d=a;b=a+20|0;a=H[b>>2];if(a){continue}b=d+16|0;a=H[d+16>>2];if(a){continue}break}H[e>>2]=0;break b}h=-1;if(a>>>0>4294967231){break i}a=a+11|0;h=a&-8;j=H[4299];if(!j){break i}f=0-h|0;g=0;n:{if(h>>>0<256){break n}g=31;if(h>>>0>16777215){break n}a=Q(a>>>8|0);g=((h>>>38-a&1)-(a<<1)|0)+62|0}b=H[(g<<2)+17496>>2];o:{p:{q:{if(!b){a=0;break q}a=0;c=h<<((g|0)!=31?25-(g>>>1|0)|0:0);while(1){r:{e=(H[b+4>>2]&-8)-h|0;if(e>>>0>=f>>>0){break r}d=b;f=e;if(e){break r}f=0;a=b;break p}e=H[b+20>>2];b=H[((c>>>29&4)+b|0)+16>>2];a=e?(e|0)==(b|0)?a:e:a;c=c<<1;if(b){continue}break}}if(!(a|d)){d=0;a=2<>2]}if(!a){break o}}while(1){b=(H[a+4>>2]&-8)-h|0;c=b>>>0>>0;f=c?b:f;d=c?a:d;b=H[a+16>>2];if(b){a=b}else{a=H[a+20>>2]}if(a){continue}break}}if(!d|H[4300]-h>>>0<=f>>>0){break i}g=H[d+24>>2];c=H[d+12>>2];if((d|0)!=(c|0)){a=H[d+8>>2];H[a+12>>2]=c;H[c+8>>2]=a;break c}b=d+20|0;a=H[b>>2];if(!a){a=H[d+16>>2];if(!a){break g}b=d+16|0}while(1){e=b;c=a;b=a+20|0;a=H[b>>2];if(a){continue}b=c+16|0;a=H[c+16>>2];if(a){continue}break}H[e>>2]=0;break c}a=H[4300];if(a>>>0>=h>>>0){d=H[4303];b=a-h|0;s:{if(b>>>0>=16){c=d+h|0;H[c+4>>2]=b|1;H[a+d>>2]=b;H[d+4>>2]=h|3;break s}H[d+4>>2]=a|3;a=a+d|0;H[a+4>>2]=H[a+4>>2]|1;c=0;b=0}H[4300]=b;H[4303]=c;a=d+8|0;break a}i=H[4301];if(i>>>0>h>>>0){b=i-h|0;H[4301]=b;c=H[4304];a=c+h|0;H[4304]=a;H[a+4>>2]=b|1;H[c+4>>2]=h|3;a=c+8|0;break a}a=0;j=h+47|0;if(H[4416]){c=H[4418]}else{H[4419]=-1;H[4420]=-1;H[4417]=4096;H[4418]=4096;H[4416]=l+12&-16^1431655768;H[4421]=0;H[4409]=0;c=4096}e=j+c|0;f=0-c|0;b=e&f;if(b>>>0<=h>>>0){break a}d=H[4408];if(d){c=H[4406];g=c+b|0;if(d>>>0>>0|c>>>0>=g>>>0){break a}}t:{if(!(I[17636]&4)){u:{v:{w:{x:{d=H[4304];if(d){a=17640;while(1){c=H[a>>2];if(c>>>0<=d>>>0&d>>>0>2]>>>0){break x}a=H[a+8>>2];if(a){continue}break}}c=zb(0);if((c|0)==-1){break u}g=b;d=H[4417];a=d-1|0;if(a&c){g=(b-c|0)+(a+c&0-d)|0}if(g>>>0<=h>>>0){break u}d=H[4408];if(d){a=H[4406];f=a+g|0;if(d>>>0>>0|a>>>0>=f>>>0){break u}}a=zb(g);if((c|0)!=(a|0)){break w}break t}g=f&e-i;c=zb(g);if((c|0)==(H[a>>2]+H[a+4>>2]|0)){break v}a=c}if((a|0)==-1){break u}if(h+48>>>0<=g>>>0){c=a;break t}c=H[4418];c=c+(j-g|0)&0-c;if((zb(c)|0)==-1){break u}g=c+g|0;c=a;break t}if((c|0)!=-1){break t}}H[4409]=H[4409]|4}c=zb(b);a=zb(0);if((c|0)==-1|(a|0)==-1|a>>>0<=c>>>0){break d}g=a-c|0;if(g>>>0<=h+40>>>0){break d}}a=H[4406]+g|0;H[4406]=a;if(a>>>0>K[4407]){H[4407]=a}y:{e=H[4304];if(e){a=17640;while(1){d=H[a>>2];b=H[a+4>>2];if((d+b|0)==(c|0)){break y}a=H[a+8>>2];if(a){continue}break}break f}a=H[4302];if(!(a>>>0<=c>>>0?a:0)){H[4302]=c}a=0;H[4411]=g;H[4410]=c;H[4306]=-1;H[4307]=H[4416];H[4413]=0;while(1){d=a<<3;b=d+17232|0;H[d+17240>>2]=b;H[d+17244>>2]=b;a=a+1|0;if((a|0)!=32){continue}break}d=g-40|0;a=c+8&7?-8-c&7:0;b=d-a|0;H[4301]=b;a=a+c|0;H[4304]=a;H[a+4>>2]=b|1;H[(c+d|0)+4>>2]=40;H[4305]=H[4420];break e}if(I[a+12|0]&8|d>>>0>e>>>0|c>>>0<=e>>>0){break f}H[a+4>>2]=b+g;a=e+8&7?-8-e&7:0;c=a+e|0;H[4304]=c;b=H[4301]+g|0;a=b-a|0;H[4301]=a;H[c+4>>2]=a|1;H[(b+e|0)+4>>2]=40;H[4305]=H[4420];break e}d=0;break b}c=0;break c}if(K[4302]>c>>>0){H[4302]=c}b=c+g|0;a=17640;z:{A:{B:{C:{D:{E:{while(1){if((b|0)!=H[a>>2]){a=H[a+8>>2];if(a){continue}break E}break}if(!(I[a+12|0]&8)){break D}}a=17640;while(1){b=H[a>>2];if(b>>>0<=e>>>0){f=b+H[a+4>>2]|0;if(f>>>0>e>>>0){break C}}a=H[a+8>>2];continue}}H[a>>2]=c;H[a+4>>2]=H[a+4>>2]+g;j=(c+8&7?-8-c&7:0)+c|0;H[j+4>>2]=h|3;g=b+(b+8&7?-8-b&7:0)|0;i=h+j|0;a=g-i|0;if((e|0)==(g|0)){H[4304]=i;a=H[4301]+a|0;H[4301]=a;H[i+4>>2]=a|1;break A}if(H[4303]==(g|0)){H[4303]=i;a=H[4300]+a|0;H[4300]=a;H[i+4>>2]=a|1;H[a+i>>2]=a;break A}f=H[g+4>>2];if((f&3)==1){e=f&-8;F:{if(f>>>0<=255){d=H[g+8>>2];b=f>>>3|0;c=H[g+12>>2];if((c|0)==(d|0)){m=17192,n=H[4298]&Vj(b),H[m>>2]=n;break F}H[d+12>>2]=c;H[c+8>>2]=d;break F}h=H[g+24>>2];c=H[g+12>>2];G:{if((g|0)!=(c|0)){b=H[g+8>>2];H[b+12>>2]=c;H[c+8>>2]=b;break G}H:{f=g+20|0;b=H[f>>2];if(b){break H}f=g+16|0;b=H[f>>2];if(b){break H}c=0;break G}while(1){d=f;c=b;f=c+20|0;b=H[f>>2];if(b){continue}f=c+16|0;b=H[c+16>>2];if(b){continue}break}H[d>>2]=0}if(!h){break F}d=H[g+28>>2];b=(d<<2)+17496|0;I:{if(H[b>>2]==(g|0)){H[b>>2]=c;if(c){break I}m=17196,n=H[4299]&Vj(d),H[m>>2]=n;break F}H[h+(H[h+16>>2]==(g|0)?16:20)>>2]=c;if(!c){break F}}H[c+24>>2]=h;b=H[g+16>>2];if(b){H[c+16>>2]=b;H[b+24>>2]=c}b=H[g+20>>2];if(!b){break F}H[c+20>>2]=b;H[b+24>>2]=c}g=e+g|0;f=H[g+4>>2];a=a+e|0}H[g+4>>2]=f&-2;H[i+4>>2]=a|1;H[a+i>>2]=a;if(a>>>0<=255){b=(a&-8)+17232|0;c=H[4298];a=1<<(a>>>3);J:{if(!(c&a)){H[4298]=a|c;a=b;break J}a=H[b+8>>2]}H[b+8>>2]=i;H[a+12>>2]=i;H[i+12>>2]=b;H[i+8>>2]=a;break A}f=31;if(a>>>0<=16777215){b=Q(a>>>8|0);f=((a>>>38-b&1)-(b<<1)|0)+62|0}H[i+28>>2]=f;H[i+16>>2]=0;H[i+20>>2]=0;b=(f<<2)+17496|0;d=H[4299];c=1<>2]=i;break K}f=a<<((f|0)!=31?25-(f>>>1|0)|0:0);c=H[b>>2];while(1){b=c;if((H[c+4>>2]&-8)==(a|0)){break B}c=f>>>29|0;f=f<<1;d=(c&4)+b|0;c=H[d+16>>2];if(c){continue}break}H[d+16>>2]=i}H[i+24>>2]=b;H[i+12>>2]=i;H[i+8>>2]=i;break A}d=g-40|0;a=c+8&7?-8-c&7:0;b=d-a|0;H[4301]=b;a=a+c|0;H[4304]=a;H[a+4>>2]=b|1;H[(c+d|0)+4>>2]=40;H[4305]=H[4420];a=(f+(f-39&7?39-f&7:0)|0)-47|0;d=a>>>0>>0?e:a;H[d+4>>2]=27;a=H[4413];H[d+16>>2]=H[4412];H[d+20>>2]=a;a=H[4411];H[d+8>>2]=H[4410];H[d+12>>2]=a;H[4412]=d+8;H[4411]=g;H[4410]=c;H[4413]=0;a=d+24|0;while(1){H[a+4>>2]=7;b=a+8|0;a=a+4|0;if(b>>>0>>0){continue}break}if((d|0)==(e|0)){break e}H[d+4>>2]=H[d+4>>2]&-2;f=d-e|0;H[e+4>>2]=f|1;H[d>>2]=f;if(f>>>0<=255){b=(f&-8)+17232|0;c=H[4298];a=1<<(f>>>3);L:{if(!(c&a)){H[4298]=a|c;a=b;break L}a=H[b+8>>2]}H[b+8>>2]=e;H[a+12>>2]=e;H[e+12>>2]=b;H[e+8>>2]=a;break e}a=31;if(f>>>0<=16777215){a=Q(f>>>8|0);a=((f>>>38-a&1)-(a<<1)|0)+62|0}H[e+28>>2]=a;H[e+16>>2]=0;H[e+20>>2]=0;b=(a<<2)+17496|0;d=H[4299];c=1<>2]=e;break M}a=f<<((a|0)!=31?25-(a>>>1|0)|0:0);d=H[b>>2];while(1){b=d;if((f|0)==(H[b+4>>2]&-8)){break z}c=a>>>29|0;a=a<<1;c=(c&4)+b|0;d=H[c+16>>2];if(d){continue}break}H[c+16>>2]=e}H[e+24>>2]=b;H[e+12>>2]=e;H[e+8>>2]=e;break e}a=H[b+8>>2];H[a+12>>2]=i;H[b+8>>2]=i;H[i+24>>2]=0;H[i+12>>2]=b;H[i+8>>2]=a}a=j+8|0;break a}a=H[b+8>>2];H[a+12>>2]=e;H[b+8>>2]=e;H[e+24>>2]=0;H[e+12>>2]=b;H[e+8>>2]=a}a=H[4301];if(a>>>0<=h>>>0){break d}b=a-h|0;H[4301]=b;c=H[4304];a=c+h|0;H[4304]=a;H[a+4>>2]=b|1;H[c+4>>2]=h|3;a=c+8|0;break a}H[3992]=48;a=0;break a}N:{if(!g){break N}b=H[d+28>>2];a=(b<<2)+17496|0;O:{if(H[a>>2]==(d|0)){H[a>>2]=c;if(c){break O}j=Vj(b)&j;H[4299]=j;break N}H[g+(H[g+16>>2]==(d|0)?16:20)>>2]=c;if(!c){break N}}H[c+24>>2]=g;a=H[d+16>>2];if(a){H[c+16>>2]=a;H[a+24>>2]=c}a=H[d+20>>2];if(!a){break N}H[c+20>>2]=a;H[a+24>>2]=c}P:{if(f>>>0<=15){a=f+h|0;H[d+4>>2]=a|3;a=a+d|0;H[a+4>>2]=H[a+4>>2]|1;break P}H[d+4>>2]=h|3;e=d+h|0;H[e+4>>2]=f|1;H[e+f>>2]=f;if(f>>>0<=255){b=(f&-8)+17232|0;c=H[4298];a=1<<(f>>>3);Q:{if(!(c&a)){H[4298]=a|c;a=b;break Q}a=H[b+8>>2]}H[b+8>>2]=e;H[a+12>>2]=e;H[e+12>>2]=b;H[e+8>>2]=a;break P}a=31;if(f>>>0<=16777215){a=Q(f>>>8|0);a=((f>>>38-a&1)-(a<<1)|0)+62|0}H[e+28>>2]=a;H[e+16>>2]=0;H[e+20>>2]=0;b=(a<<2)+17496|0;R:{c=1<>2]=e;break S}a=f<<((a|0)!=31?25-(a>>>1|0)|0:0);h=H[b>>2];while(1){b=h;if((H[b+4>>2]&-8)==(f|0)){break R}c=a>>>29|0;a=a<<1;c=(c&4)+b|0;h=H[c+16>>2];if(h){continue}break}H[c+16>>2]=e}H[e+24>>2]=b;H[e+12>>2]=e;H[e+8>>2]=e;break P}a=H[b+8>>2];H[a+12>>2]=e;H[b+8>>2]=e;H[e+24>>2]=0;H[e+12>>2]=b;H[e+8>>2]=a}a=d+8|0;break a}T:{if(!i){break T}b=H[c+28>>2];a=(b<<2)+17496|0;U:{if(H[a>>2]==(c|0)){H[a>>2]=d;if(d){break U}m=17196,n=Vj(b)&j,H[m>>2]=n;break T}H[i+(H[i+16>>2]==(c|0)?16:20)>>2]=d;if(!d){break T}}H[d+24>>2]=i;a=H[c+16>>2];if(a){H[d+16>>2]=a;H[a+24>>2]=d}a=H[c+20>>2];if(!a){break T}H[d+20>>2]=a;H[a+24>>2]=d}V:{if(f>>>0<=15){a=f+h|0;H[c+4>>2]=a|3;a=a+c|0;H[a+4>>2]=H[a+4>>2]|1;break V}H[c+4>>2]=h|3;d=c+h|0;H[d+4>>2]=f|1;H[d+f>>2]=f;if(k){b=(k&-8)+17232|0;e=H[4303];a=1<<(k>>>3);W:{if(!(a&g)){H[4298]=a|g;a=b;break W}a=H[b+8>>2]}H[b+8>>2]=e;H[a+12>>2]=e;H[e+12>>2]=b;H[e+8>>2]=a}H[4303]=d;H[4300]=f}a=c+8|0}ca=l+16|0;return a|0}function ce(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0,x=0,y=0,z=0,A=0;m=ca-32|0;ca=m;o=pa(12);H[o+8>>2]=0;H[o+4>>2]=b;H[o>>2]=0;s=o+12|0;b=s;a:{b:{c:{while(1){b=b-12|0;w=H[b+8>>2];j=H[b+4>>2];t=H[b>>2];if(t){if((w|0)>1e3){break a}H[m+24>>2]=0;H[m+16>>2]=0;H[m+20>>2]=0;d=1;c=H[a>>2];e=H[c+8>>2];h=H[c+12>>2];g=H[c+20>>2];f=H[c+16>>2];d:{if((h|0)<=(g|0)&f>>>0>=e>>>0|(g|0)>(h|0)){break d}e=I[f+H[c>>2]|0];h=c;c=g;f=f+1|0;c=f?c:c+1|0;H[h+16>>2]=f;H[h+20>>2]=c;Cc(m+16|0,e);if(e){c=H[a>>2];n=Dc(m+16|0);p=H[c+8>>2];g=H[c+12>>2];h=H[c+20>>2];f=H[c+16>>2];k=f+e|0;h=k>>>0>>0?h+1|0:h;if((g|0)<=(h|0)&k>>>0>p>>>0|(g|0)<(h|0)){break d}qa(n,f+H[c>>2]|0,e);d=H[c+20>>2];f=e;e=e+H[c+16>>2]|0;d=f>>>0>e>>>0?d+1|0:d;H[c+16>>2]=e;H[c+20>>2]=d}j=pa(24);c=j;H[c+4>>2]=0;H[c+8>>2]=0;c=c+16|0;H[c>>2]=0;H[c+4>>2]=0;H[j>>2]=j+4;H[j+12>>2]=c;e=ca-32|0;ca=e;h=t+12|0;c=m+16|0;u=nb(h,c);i=t+16|0;e:{if((u|0)==(i|0)){H[e+16>>2]=c;f:{g:{d=H[h+4>>2];h:{if(!d){f=h+4|0;c=f;break h}f=I[c+11|0];g=f<<24>>24<0;n=g?H[c>>2]:c;g=g?H[c+4>>2]:f;while(1){c=d;d=I[c+27|0];f=d<<24>>24<0;d=f?H[c+20>>2]:d;p=d>>>0>>0;i:{j:{k:{l:{k=p?d:g;m:{if(k){f=f?H[c+16>>2]:c+16|0;q=Fa(n,f,k);if(!q){if(d>>>0>g>>>0){break m}break l}if((q|0)>=0){break l}break m}if(d>>>0<=g>>>0){break k}}f=c;d=H[c>>2];if(d){continue}break h}d=Fa(f,n,k);if(d){break j}}if(p){break i}break g}if((d|0)>=0){break g}}d=H[c+4>>2];if(d){continue}break}f=c+4|0}d=pa(32);n=d+16|0;g=H[e+16>>2];n:{if(F[g+11|0]>=0){p=H[g+4>>2];H[n>>2]=H[g>>2];H[n+4>>2]=p;H[n+8>>2]=H[g+8>>2];break n}za(n,H[g>>2],H[g+4>>2])}H[d+8>>2]=c;H[d>>2]=0;H[d+4>>2]=0;H[d+28>>2]=0;H[f>>2]=d;c=d;g=H[H[h>>2]>>2];if(g){H[h>>2]=g;c=H[f>>2]}Sb(H[h+4>>2],c);H[h+8>>2]=H[h+8>>2]+1;c=1;break f}d=c;c=0}F[e+28|0]=c;H[e+24>>2]=d;d=H[e+24>>2];c=H[d+28>>2];H[d+28>>2]=j;if(!c){break e}Ra(c+12|0,H[c+16>>2]);Qa(c,H[c+4>>2]);oa(c);break e}if(!j){break e}Ra(j+12|0,H[j+16>>2]);Qa(j,H[j+4>>2]);oa(j)}ca=e+32|0;d=(i|0)!=(u|0)}if(F[m+27|0]<0){oa(H[m+16>>2])}if(d){break a}}if(!j){break a}H[m+16>>2]=0;if(!Bb(1,m+16|0,H[a>>2])){break a}q=0;x=H[m+16>>2];if(x){while(1){d=0;i=ca-32|0;ca=i;H[i+24>>2]=0;H[i+16>>2]=0;H[i+20>>2]=0;c=H[a>>2];f=H[c+8>>2];o:{p:{h=H[c+12>>2];g=H[c+20>>2];e=H[c+16>>2];q:{if((h|0)<=(g|0)&e>>>0>=f>>>0|(g|0)>(h|0)){break q}f=I[e+H[c>>2]|0];h=c;c=g;e=e+1|0;c=e?c:c+1|0;H[h+16>>2]=e;H[h+20>>2]=c;Cc(i+16|0,f);if(f){e=H[a>>2];n=Dc(i+16|0);p=H[e+8>>2];g=H[e+12>>2];c=H[e+20>>2];h=H[e+16>>2];k=h+f|0;c=k>>>0>>0?c+1|0:c;if(k>>>0>p>>>0&(c|0)>=(g|0)|(c|0)>(g|0)){break q}qa(n,h+H[e>>2]|0,f);c=H[e+20>>2];g=f;f=f+H[e+16>>2]|0;c=g>>>0>f>>>0?c+1|0:c;H[e+16>>2]=f;H[e+20>>2]=c}H[i+12>>2]=0;if(!Bb(1,i+12|0,H[a>>2])){break q}f=H[i+12>>2];if(!f){break q}e=H[a>>2];c=H[e+8>>2];h=H[e+16>>2];g=c-h|0;c=H[e+12>>2]-(H[e+20>>2]+(c>>>0>>0)|0)|0;if((c|0)<=0&f>>>0>g>>>0|(c|0)<0){break q}H[i+8>>2]=0;H[i>>2]=0;H[i+4>>2]=0;if((f|0)<0){break p}d=pa(f);H[i>>2]=d;c=d+f|0;H[i+8>>2]=c;l=ra(d,0,f);H[i+4>>2]=c;h=H[e+12>>2];y=h;p=H[e+8>>2];c=H[e+20>>2];k=H[e+16>>2];g=f+k|0;c=g>>>0>>0?c+1|0:c;u=g;n=c;r:{if((c|0)<=(h|0)&g>>>0<=p>>>0|(c|0)<(h|0)){qa(l,H[e>>2]+k|0,f);d=H[e+20>>2];c=f+H[e+16>>2]|0;d=c>>>0>>0?d+1|0:d;H[e+16>>2]=c;H[e+20>>2]=d;h=ca-48|0;ca=h;e=nb(j,i+16|0);if((e|0)!=(j+4|0)){c=H[e+4>>2];s:{if(!c){c=e;while(1){d=H[c+8>>2];f=H[d>>2]!=(c|0);c=d;if(f){continue}break}break s}while(1){d=c;c=H[c>>2];if(c){continue}break}}if((e|0)==H[j>>2]){H[j>>2]=d}H[j+8>>2]=H[j+8>>2]-1;f=H[j+4>>2];t:{u:{g=e;d=e;e=H[d>>2];if(e){c=H[g+4>>2];if(!c){break u}while(1){d=c;c=H[c>>2];if(c){continue}break}}e=H[d+4>>2];if(e){break u}e=0;k=1;break t}H[e+8>>2]=H[d+8>>2];k=0}l=H[d+8>>2];c=H[l>>2];v:{if((d|0)==(c|0)){H[l>>2]=e;if((d|0)==(f|0)){c=0;f=e;break v}c=H[l+4>>2];break v}H[l+4>>2]=e}r=!I[d+12|0];if((d|0)!=(g|0)){l=H[g+8>>2];H[d+8>>2]=l;H[l+(((g|0)!=H[H[g+8>>2]>>2])<<2)>>2]=d;l=H[g>>2];H[d>>2]=l;H[l+8>>2]=d;l=H[g+4>>2];H[d+4>>2]=l;if(l){H[l+8>>2]=d}F[d+12|0]=I[g+12|0];f=(f|0)==(g|0)?d:f}w:{if(r|!f){break w}if(k){while(1){e=I[c+12|0];x:{d=H[c+8>>2];if(H[d>>2]!=(c|0)){if(!e){F[c+12|0]=1;F[d+12|0]=0;e=H[d+4>>2];k=H[e>>2];H[d+4>>2]=k;if(k){H[k+8>>2]=d}H[e+8>>2]=H[d+8>>2];k=H[d+8>>2];H[(((d|0)!=H[k>>2])<<2)+k>>2]=e;H[e>>2]=d;H[d+8>>2]=e;d=c;c=H[c>>2];f=(c|0)==(f|0)?d:f;c=H[c+4>>2]}y:{z:{d=H[c>>2];A:{if(!(I[d+12|0]?0:d)){e=H[c+4>>2];if(I[e+12|0]?0:e){break A}F[c+12|0]=0;c=H[c+8>>2];B:{if((f|0)==(c|0)){c=f;break B}if(I[c+12|0]){break x}}F[c+12|0]=1;break w}e=H[c+4>>2];if(!e){break z}}if(I[e+12|0]){break z}d=c;break y}F[d+12|0]=1;F[c+12|0]=0;e=H[d+4>>2];H[c>>2]=e;if(e){H[e+8>>2]=c}H[d+8>>2]=H[c+8>>2];e=H[c+8>>2];H[((H[e>>2]!=(c|0))<<2)+e>>2]=d;H[d+4>>2]=c;H[c+8>>2]=d;e=c}c=H[d+8>>2];F[d+12|0]=I[c+12|0];F[c+12|0]=1;F[e+12|0]=1;d=H[c+4>>2];e=H[d>>2];H[c+4>>2]=e;if(e){H[e+8>>2]=c}H[d+8>>2]=H[c+8>>2];e=H[c+8>>2];H[(((c|0)!=H[e>>2])<<2)+e>>2]=d;H[d>>2]=c;H[c+8>>2]=d;break w}if(!e){F[c+12|0]=1;F[d+12|0]=0;e=H[c+4>>2];H[d>>2]=e;if(e){H[e+8>>2]=d}H[c+8>>2]=H[d+8>>2];e=H[d+8>>2];H[(((d|0)!=H[e>>2])<<2)+e>>2]=c;H[c+4>>2]=d;H[d+8>>2]=c;f=(d|0)==(f|0)?c:f;c=H[d>>2]}e=H[c>>2];C:{if(!(!e|I[e+12|0])){d=c;break C}d=H[c+4>>2];if(!(I[d+12|0]?0:d)){F[c+12|0]=0;c=H[c+8>>2];if((c|0)!=(f|0)?I[c+12|0]:0){break x}F[c+12|0]=1;break w}if(e){if(!I[e+12|0]){d=c;break C}d=H[c+4>>2]}F[d+12|0]=1;F[c+12|0]=0;e=H[d>>2];H[c+4>>2]=e;if(e){H[e+8>>2]=c}H[d+8>>2]=H[c+8>>2];e=H[c+8>>2];H[((H[e>>2]!=(c|0))<<2)+e>>2]=d;H[d>>2]=c;H[c+8>>2]=d;e=c}c=H[d+8>>2];F[d+12|0]=I[c+12|0];F[c+12|0]=1;F[e+12|0]=1;d=H[c>>2];e=H[d+4>>2];H[c>>2]=e;if(e){H[e+8>>2]=c}H[d+8>>2]=H[c+8>>2];e=H[c+8>>2];H[(((c|0)!=H[e>>2])<<2)+e>>2]=d;H[d+4>>2]=c;H[c+8>>2]=d;break w}d=c;c=H[c+8>>2];c=H[(((d|0)==H[c>>2])<<2)+c>>2];continue}}F[e+12|0]=1}c=H[g+28>>2];if(c){H[g+32>>2]=c;oa(c)}if(F[g+27|0]<0){oa(H[g+16>>2])}oa(g)}H[h+8>>2]=0;H[h>>2]=0;H[h+4>>2]=0;c=H[i+4>>2];d=H[i>>2];f=c-d|0;e=0;D:{E:{if((c|0)!=(d|0)){if((f|0)<0){break E}e=pa(f);c=ra(e,0,f);g=c+f|0;H[h+8>>2]=g;H[h+4>>2]=g;H[h>>2]=c;c=d}qa(e,c,f);F:{if(F[i+27|0]>=0){H[h+24>>2]=H[i+24>>2];c=H[i+20>>2];H[h+16>>2]=H[i+16>>2];H[h+20>>2]=c;break F}za(h+16|0,H[i+16>>2],H[i+20>>2])}ae(h+28|0,h);f=h+16|0;c=f;G:{H:{d=H[j+4>>2];I:{if(!d){e=j+4|0;c=e;break I}e=I[c+11|0];g=e<<24>>24<0;k=g?H[c>>2]:c;g=g?H[c+4>>2]:e;while(1){c=d;d=I[c+27|0];e=d<<24>>24<0;d=e?H[c+20>>2]:d;l=d>>>0>>0;J:{K:{L:{M:{r=l?d:g;N:{if(r){e=e?H[c+16>>2]:c+16|0;z=Fa(k,e,r);if(!z){if(d>>>0>g>>>0){break N}break M}if((z|0)>=0){break M}break N}if(d>>>0<=g>>>0){break L}}e=c;d=H[c>>2];if(d){continue}break I}d=Fa(e,k,r);if(d){break K}}if(l){break J}break H}if((d|0)>=0){break H}}d=H[c+4>>2];if(d){continue}break}e=c+4|0}d=pa(40);H[d+24>>2]=H[f+8>>2];g=H[f+4>>2];H[d+16>>2]=H[f>>2];H[d+20>>2]=g;H[f>>2]=0;H[f+4>>2]=0;H[f+8>>2]=0;ae(d+28|0,f+12|0);H[d+8>>2]=c;H[d>>2]=0;H[d+4>>2]=0;H[e>>2]=d;c=d;f=H[H[j>>2]>>2];if(f){H[j>>2]=f;c=H[e>>2]}Sb(H[j+4>>2],c);H[j+8>>2]=H[j+8>>2]+1;c=1;break G}d=c;c=0}F[h+44|0]=c;H[h+40>>2]=d;c=H[h+28>>2];if(c){H[h+32>>2]=c;oa(c)}if(F[h+27|0]<0){oa(H[h+16>>2])}c=H[h>>2];if(c){H[h+4>>2]=c;oa(c)}ca=h+48|0;break D}sa();v()}d=H[i>>2];if(!d){break r}}H[i+4>>2]=d;oa(d)}d=(n|0)<=(y|0)&p>>>0>=u>>>0|(n|0)<(y|0)}if(F[i+27|0]<0){oa(H[i+16>>2])}ca=i+32|0;break o}sa();v()}if(!d){break a}q=q+1|0;if((x|0)!=(q|0)){continue}break}}H[m+12>>2]=0;if(!Bb(1,m+12|0,H[a>>2])){break a}c=H[a>>2];e=H[c+8>>2];f=H[c+16>>2];h=e-f|0;d=H[m+12>>2];c=H[c+12>>2]-(H[c+20>>2]+(e>>>0>>0)|0)|0;if(h>>>0>>0&(c|0)<=0|(c|0)<0){break a}if(d){q=0;h=((t|0)!=0)+w|0;while(1){O:{if(b>>>0>>0){H[b+8>>2]=h;H[b+4>>2]=0;H[b>>2]=j;b=b+12|0;d=H[m+12>>2];break O}c=b-o|0;g=(c|0)/12|0;b=g+1|0;if(b>>>0>=357913942){break c}e=(s-o|0)/12|0;f=e<<1;e=e>>>0>=178956970?357913941:b>>>0>>0?f:b;if(e){if(e>>>0>=357913942){break b}f=pa(N(e,12))}else{f=0}b=f+N(g,12)|0;H[b+8>>2]=h;H[b+4>>2]=0;H[b>>2]=j;c=va(b+N((c|0)/-12|0,12)|0,o,c);s=f+N(e,12)|0;b=b+12|0;if(o){oa(o)}o=c}q=q+1|0;if(q>>>0>>0){continue}break}}if((b|0)!=(o|0)){continue}break}A=1;break a}sa();v()}wa();v()}if(o){oa(o)}ca=m+32|0;return A}function Af(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=O(0),q=0,r=0;e=ca-720|0;ca=e;a:{b:{c:{d:{e:{f:{g:{h:{i:{if(J[b+38>>1]>=515){H[e+680>>2]=0;H[e+672>>2]=0;H[e+676>>2]=0;if((ea[H[H[a>>2]+24>>2]](a)|0)<=0){break d}while(1){c=ea[H[H[a>>2]+20>>2]](a,n)|0;d=H[H[H[(ea[H[H[a>>2]+28>>2]](a)|0)+4>>2]+8>>2]+(c<<2)>>2];if(H[d+28>>2]==9){f=H[e+672>>2];c=H[e+676>>2]-f>>2;k=I[d+24|0];j:{if(c>>>0>>0){ya(e+672|0,k-c|0);break j}if(c>>>0<=k>>>0){break j}H[e+676>>2]=f+(k<<2)}j=0;i=H[b+8>>2];h=H[b+12>>2];c=H[b+20>>2];d=k<<2;f=H[b+16>>2];l=f+d|0;c=d>>>0>l>>>0?c+1|0:c;if(i>>>0>>0&(c|0)>=(h|0)|(c|0)>(h|0)){break b}qa(H[e+672>>2],f+H[b>>2]|0,d);c=H[b+20>>2];f=d;d=d+H[b+16>>2]|0;c=f>>>0>d>>>0?c+1|0:c;i=d;H[b+16>>2]=d;H[b+20>>2]=c;l=H[b+12>>2];g=H[b+8>>2];h=d+4|0;f=h>>>0<4?c+1|0:c;d=f;if(g>>>0>>0&(d|0)>=(l|0)|(d|0)>(l|0)){break b}o=H[b>>2];f=o+i|0;f=I[f|0]|I[f+1|0]<<8|(I[f+2|0]<<16|I[f+3|0]<<24);H[b+16>>2]=h;H[b+20>>2]=d;if(g>>>0<=h>>>0&(d|0)>=(l|0)|(d|0)>(l|0)){break b}d=I[h+o|0];h=i+5|0;c=h>>>0<5?c+1|0:c;H[b+16>>2]=h;H[b+20>>2]=c;if(d>>>0>31){break b}p=(A(2,f),B());H[e+20>>2]=-1;H[e+16>>2]=1832;H[e+32>>2]=0;H[e+36>>2]=0;H[e+24>>2]=0;H[e+28>>2]=0;c=H[e+672>>2];o=d-1|0;if(o>>>0<=29){H[e+20>>2]=d;k:{h=c+(k<<2)|0;l=h-c|0;f=l>>2;i=H[e+32>>2];d=H[e+24>>2];if(f>>>0<=i-d>>2>>>0){i=H[e+28>>2]-d|0;l=i>>2;i=f>>>0>l>>>0?c+i|0:h;g=i-c|0;if((c|0)!=(i|0)){va(d,c,g)}if(f>>>0>l>>>0){c=h-i|0;d=H[e+28>>2];if((h|0)!=(i|0)){va(d,i,c)}H[e+28>>2]=c+d;break k}H[e+28>>2]=d+g;break k}if(d){H[e+28>>2]=d;oa(d);H[e+32>>2]=0;H[e+24>>2]=0;H[e+28>>2]=0;i=0}l:{if((l|0)<0){break l}d=i>>>1|0;d=i>>>0>=2147483644?1073741823:d>>>0>f>>>0?d:f;if(d>>>0>=1073741824){break l}i=d<<2;d=pa(i);H[e+28>>2]=d;H[e+24>>2]=d;H[e+32>>2]=d+i;if((c|0)!=(h|0)){qa(d,c,l)}H[e+28>>2]=d+(f<<2);break k}sa();v()}L[e+36>>2]=p}m:{if(o>>>0>=30){break m}if(!Xc(e+16|0,H[H[a+60>>2]+((H[a+40>>2]-H[a+36>>2]|0)/24<<2)>>2])){break m}c=H[a+40>>2];n:{if((c|0)!=H[a+44>>2]){H[c>>2]=1832;d=H[e+20>>2];H[c+16>>2]=0;H[c+8>>2]=0;H[c+12>>2]=0;H[c+4>>2]=d;d=H[e+28>>2];f=H[e+24>>2];if((d|0)!=(f|0)){d=d-f|0;if((d|0)<0){break i}g=pa(d);H[c+12>>2]=g;H[c+8>>2]=g;H[c+16>>2]=(d&-4)+g;k=H[e+24>>2];d=H[e+28>>2];if((k|0)!=(d|0)){while(1){L[g>>2]=L[k>>2];g=g+4|0;k=k+4|0;if((d|0)!=(k|0)){continue}break}}H[c+12>>2]=g}L[c+20>>2]=L[e+36>>2];H[a+40>>2]=c+24;break n}d=0;o:{p:{q:{r:{j=H[a+40>>2];f=H[a+36>>2];i=(j-f|0)/24|0;c=i+1|0;if(c>>>0<178956971){h=(H[a+44>>2]-f|0)/24|0;l=h<<1;h=h>>>0>=89478485?178956970:c>>>0>>0?l:c;if(h){if(h>>>0>=178956971){break r}d=pa(N(h,24))}g=N(i,24)+d|0;H[g>>2]=1832;c=H[e+20>>2];H[g+16>>2]=0;H[g+8>>2]=0;H[g+12>>2]=0;H[g+4>>2]=c;c=H[e+24>>2];i=H[e+28>>2];if((c|0)!=(i|0)){l=i-c|0;if((l|0)<0){break q}k=pa(l);H[g+8>>2]=k;H[g+16>>2]=(l&-4)+k;while(1){L[k>>2]=L[c>>2];k=k+4|0;c=c+4|0;if((i|0)!=(c|0)){continue}break}H[g+12>>2]=k}c=N(h,24)+d|0;L[g+20>>2]=L[e+36>>2];d=g+24|0;if((f|0)==(j|0)){break p}while(1){g=g-24|0;H[g>>2]=1832;j=j-24|0;H[g+4>>2]=H[j+4>>2];H[g+8>>2]=H[j+8>>2];H[g+12>>2]=H[j+12>>2];H[g+16>>2]=H[j+16>>2];H[j+16>>2]=0;H[j+8>>2]=0;H[j+12>>2]=0;L[g+20>>2]=L[j+20>>2];if((f|0)!=(j|0)){continue}break}H[a+44>>2]=c;k=H[a+40>>2];H[a+40>>2]=d;j=H[a+36>>2];H[a+36>>2]=g;if((j|0)==(k|0)){break o}while(1){k=k-24|0;ea[H[H[k>>2]>>2]](k)|0;if((j|0)!=(k|0)){continue}break}break o}sa();v()}wa();v()}sa();v()}H[a+44>>2]=c;H[a+40>>2]=d;H[a+36>>2]=g}if(j){oa(j)}}j=1}H[e+16>>2]=1832;c=H[e+24>>2];if(c){H[e+28>>2]=c;oa(c)}if(!j){break c}}n=n+1|0;if((ea[H[H[a>>2]+24>>2]](a)|0)>(n|0)){continue}break}break d}k=ea[H[H[a>>2]+24>>2]](a)|0;H[e+712>>2]=0;H[e+704>>2]=0;H[e+708>>2]=0;if(k){if(k>>>0>=214748365){break h}c=N(k,20);d=pa(c);H[e+704>>2]=d;H[e+712>>2]=c+d;c=c-20|0;c=(c-((c>>>0)%20|0)|0)+20|0;q=e,r=ra(d,0,c)+c|0,H[q+708>>2]=r;while(1){c=ea[H[H[a>>2]+20>>2]](a,m)|0;d=H[H[H[(ea[H[H[a>>2]+28>>2]](a)|0)+4>>2]+8>>2]+(c<<2)>>2];f=H[d+28>>2];c=f-1|0;if(c>>>0<=10){c=H[(c<<2)+13584>>2]}else{c=-1}h=(c|0)>0?c:0;if(h>>>0>4){break f}c=H[e+704>>2]+N(m,20)|0;i=I[d+24|0];H[c+16>>2]=i;H[c+12>>2]=h;H[c+8>>2]=f;H[c+4>>2]=g;H[c>>2]=d;g=g+i|0;m=m+1|0;if((k|0)!=(m|0)){continue}break}}c=ea[H[H[a>>2]+20>>2]](a,0)|0;m=H[H[H[(ea[H[H[a>>2]+28>>2]](a)|0)+4>>2]+8>>2]+(c<<2)>>2];F[m+84|0]=1;H[m+72>>2]=H[m+68>>2];h=H[b+12>>2];c=h;d=H[b+20>>2];f=H[b+8>>2];i=H[b+16>>2];if((c|0)<=(d|0)&f>>>0<=i>>>0|(c|0)<(d|0)){break f}n=H[b>>2];o=I[n+i|0];c=d;l=i+1|0;c=l?c:c+1|0;H[b+16>>2]=l;H[b+20>>2]=c;s:{switch(o|0){case 0:a=H[e+704>>2];if((H[e+708>>2]-a|0)!=20){break e}if(H[a+16>>2]!=3){break f}t:{if(f>>>0<=l>>>0&(c|0)>=(h|0)|(c|0)>(h|0)){break t}c=d;a=i+2|0;c=a>>>0<2?c+1|0:c;l=a;H[b+16>>2]=a;H[b+20>>2]=c;c=d;a=i+6|0;c=a>>>0<6?c+1|0:c;if(a>>>0>f>>>0&(c|0)>=(h|0)|(c|0)>(h|0)){break t}d=l+n|0;d=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[b+16>>2]=a;H[b+20>>2]=c;mb(m,d);j=e+672|0;H[j+20>>2]=0;H[j+12>>2]=0;H[j+16>>2]=0;H[j>>2]=0;H[j+4>>2]=0;H[j+20>>2]=d;d=Ac(e+16|0,e+704|0);k=0;g=ca-32|0;ca=g;H[g+24>>2]=0;H[g+16>>2]=0;H[g+20>>2]=0;f=H[b+12>>2];m=f;i=H[b+8>>2];c=H[b+20>>2];l=c;h=H[b+16>>2];a=h+4|0;c=a>>>0<4?c+1|0:c;u:{if(a>>>0>i>>>0&(c|0)>=(f|0)|(c|0)>(f|0)){break u}n=H[b>>2];f=n+h|0;f=I[f|0]|I[f+1|0]<<8|(I[f+2|0]<<16|I[f+3|0]<<24);H[b+16>>2]=a;H[b+20>>2]=c;v:{w:{switch(f-2|0){case 1:if((c|0)>=(m|0)&a>>>0>=i>>>0|(c|0)>(m|0)){break u}a=F[a+n|0];c=l;f=h+5|0;c=f>>>0<5?c+1|0:c;H[b+16>>2]=f;H[b+20>>2]=c;H[j+8>>2]=a;if((a|0)==1){if(Ud(j,b,g+16|0)){break v}break u}Rd(1799,23,H[3443]);break u;default:Rd(1774,24,H[3443]);break u;case 0:break w}}if(!Ud(j,b,g+16|0)){break u}}H[g+8>>2]=H[g+16>>2];H[g>>2]=H[g+20>>2];c=ca-32|0;ca=c;a=H[j>>2];p=L[j+4>>2];H[c+24>>2]=1065353216;h=-1<0){L[c+24>>2]=p/O(a|0)}m=H[g+8>>2];n=H[g>>2];if((m|0)!=(n|0)){a=H[d+28>>2];while(1){b=H[m>>2];f=H[m+4>>2];p=L[c+24>>2];L[c+16>>2]=p*O(H[m+8>>2]-h|0);L[c+12>>2]=p*O(f-h|0);L[c+8>>2]=p*O(b-h|0);b=a;i=H[d+16>>2];f=H[i>>2];if(!I[f+84|0]){b=H[H[f+68>>2]+(a<<2)>>2]}if(K[f+80>>2]>b>>>0){a=H[f+40>>2];qa(H[H[f>>2]>>2]+N(a,b)|0,(c+8|0)+(H[i+4>>2]<<2)|0,a);n=H[g>>2];a=H[d+28>>2]}a=a+1|0;H[d+28>>2]=a;m=m+12|0;if((n|0)!=(m|0)){continue}break}}ca=c+32|0;k=1}a=H[g+16>>2];if(a){H[g+20>>2]=a;oa(a)}ca=g+32|0;yc(d);j=1;if(k){break f}}j=0;break f;case 1:break s;default:break f}}if(f>>>0<=l>>>0&(c|0)>=(h|0)|(c|0)>(h|0)){break f}o=I[l+n|0];c=d;l=i+2|0;c=l>>>0<2?c+1|0:c;H[b+16>>2]=l;H[b+20>>2]=c;if(o>>>0>=7){H[e>>2]=o;Qd(1651,e);break f}c=d;d=i+6|0;c=d>>>0<6?c+1|0:c;if(d>>>0>f>>>0&(c|0)>=(h|0)|(c|0)>(h|0)){break f}f=l+n|0;f=I[f|0]|I[f+1|0]<<8|(I[f+2|0]<<16|I[f+3|0]<<24);H[b+16>>2]=d;H[b+20>>2]=c;if(k){m=0;while(1){c=ea[H[H[a>>2]+20>>2]](a,m)|0;c=H[H[H[(ea[H[H[a>>2]+28>>2]](a)|0)+4>>2]+8>>2]+(c<<2)>>2];mb(c,f);F[c+84|0]=1;H[c+72>>2]=H[c+68>>2];m=m+1|0;if((k|0)!=(m|0)){continue}break}}a=Ac(e+672|0,e+704|0);x:{y:{switch(o|0){case 1:c=wb(e+16|0,g);b=zd(c,b,a,-1);xb(c);if(!b){break g}break x;case 2:c=ub(e+16|0,g);b=yd(c,b,a,-1);vb(c);if(!b){break g}break x;case 3:c=ub(e+16|0,g);b=xd(c,b,a,-1);vb(c);if(!b){break g}break x;case 4:c=$a(e+16|0,g);b=wd(c,b,a,-1);ab(c);if(!b){break g}break x;case 5:c=$a(e+16|0,g);b=vd(c,b,a,-1);ab(c);if(!b){break g}break x;case 6:c=$a(e+16|0,g);b=ud(c,b,a,-1);ab(c);if(b){break x}break g;case 0:break y;default:break g}}c=wb(e+16|0,g);b=Bd(c,b,a,-1);xb(c);if(!b){break g}}yc(a);j=1;break f}sa();v()}sa();v()}yc(a)}a=H[e+704>>2]}if(!a){break a}H[e+708>>2]=a;oa(a);break a}j=1;if(H[a+52>>2]==H[a+48>>2]){break b}while(1){if(!td(1,e+16|0,b)){break c}c=H[a+48>>2];d=H[e+16>>2];H[c+(m<<2)>>2]=d>>>1^0-(d&1);m=m+1|0;if(m>>>0>2]-c>>2>>>0){continue}break}break b}j=0}a=H[e+672>>2];if(!a){break a}H[e+676>>2]=a;oa(a)}ca=e+720|0;return j|0}function te(a,b,c,d,e){var f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0;h=ca-32|0;ca=h;H[b+32>>2]=d;H[b+40>>2]=c;H[b+4>>2]=e;nc(a,d,h+16|0);a:{if(H[a>>2]){break a}if(F[a+15|0]<0){oa(H[a+4>>2])}d=I[h+23|0];if((ea[H[H[b>>2]+8>>2]](b)|0)!=(d|0)){b=pa(64);F[b+50|0]=0;c=I[1314]|I[1315]<<8;F[b+48|0]=c;F[b+49|0]=c>>>8;c=I[1310]|I[1311]<<8|(I[1312]<<16|I[1313]<<24);d=I[1306]|I[1307]<<8|(I[1308]<<16|I[1309]<<24);F[b+40|0]=d;F[b+41|0]=d>>>8;F[b+42|0]=d>>>16;F[b+43|0]=d>>>24;F[b+44|0]=c;F[b+45|0]=c>>>8;F[b+46|0]=c>>>16;F[b+47|0]=c>>>24;c=I[1302]|I[1303]<<8|(I[1304]<<16|I[1305]<<24);d=I[1298]|I[1299]<<8|(I[1300]<<16|I[1301]<<24);F[b+32|0]=d;F[b+33|0]=d>>>8;F[b+34|0]=d>>>16;F[b+35|0]=d>>>24;F[b+36|0]=c;F[b+37|0]=c>>>8;F[b+38|0]=c>>>16;F[b+39|0]=c>>>24;c=I[1294]|I[1295]<<8|(I[1296]<<16|I[1297]<<24);d=I[1290]|I[1291]<<8|(I[1292]<<16|I[1293]<<24);F[b+24|0]=d;F[b+25|0]=d>>>8;F[b+26|0]=d>>>16;F[b+27|0]=d>>>24;F[b+28|0]=c;F[b+29|0]=c>>>8;F[b+30|0]=c>>>16;F[b+31|0]=c>>>24;c=I[1286]|I[1287]<<8|(I[1288]<<16|I[1289]<<24);d=I[1282]|I[1283]<<8|(I[1284]<<16|I[1285]<<24);F[b+16|0]=d;F[b+17|0]=d>>>8;F[b+18|0]=d>>>16;F[b+19|0]=d>>>24;F[b+20|0]=c;F[b+21|0]=c>>>8;F[b+22|0]=c>>>16;F[b+23|0]=c>>>24;c=I[1278]|I[1279]<<8|(I[1280]<<16|I[1281]<<24);d=I[1274]|I[1275]<<8|(I[1276]<<16|I[1277]<<24);F[b+8|0]=d;F[b+9|0]=d>>>8;F[b+10|0]=d>>>16;F[b+11|0]=d>>>24;F[b+12|0]=c;F[b+13|0]=c>>>8;F[b+14|0]=c>>>16;F[b+15|0]=c>>>24;c=I[1270]|I[1271]<<8|(I[1272]<<16|I[1273]<<24);d=I[1266]|I[1267]<<8|(I[1268]<<16|I[1269]<<24);F[b|0]=d;F[b+1|0]=d>>>8;F[b+2|0]=d>>>16;F[b+3|0]=d>>>24;F[b+4|0]=c;F[b+5|0]=c>>>8;F[b+6|0]=c>>>16;F[b+7|0]=c>>>24;H[a>>2]=-1;za(a+4|0,b,50);oa(b);break a}c=I[h+21|0];F[b+36|0]=c;e=I[h+22|0];F[b+37|0]=e;if((c-3&255)>>>0<=253){b=pa(32);F[b+22|0]=0;c=I[1427]|I[1428]<<8|(I[1429]<<16|I[1430]<<24);d=I[1423]|I[1424]<<8|(I[1425]<<16|I[1426]<<24);F[b+14|0]=d;F[b+15|0]=d>>>8;F[b+16|0]=d>>>16;F[b+17|0]=d>>>24;F[b+18|0]=c;F[b+19|0]=c>>>8;F[b+20|0]=c>>>16;F[b+21|0]=c>>>24;c=I[1421]|I[1422]<<8|(I[1423]<<16|I[1424]<<24);d=I[1417]|I[1418]<<8|(I[1419]<<16|I[1420]<<24);F[b+8|0]=d;F[b+9|0]=d>>>8;F[b+10|0]=d>>>16;F[b+11|0]=d>>>24;F[b+12|0]=c;F[b+13|0]=c>>>8;F[b+14|0]=c>>>16;F[b+15|0]=c>>>24;c=I[1413]|I[1414]<<8|(I[1415]<<16|I[1416]<<24);d=I[1409]|I[1410]<<8|(I[1411]<<16|I[1412]<<24);F[b|0]=d;F[b+1|0]=d>>>8;F[b+2|0]=d>>>16;F[b+3|0]=d>>>24;F[b+4|0]=c;F[b+5|0]=c>>>8;F[b+6|0]=c>>>16;F[b+7|0]=c>>>24;H[a>>2]=-5;za(a+4|0,b,22);oa(b);break a}if(!((c|0)!=2|e>>>0<=(d?2:3)>>>0)){b=pa(32);F[b+22|0]=0;c=I[1404]|I[1405]<<8|(I[1406]<<16|I[1407]<<24);d=I[1400]|I[1401]<<8|(I[1402]<<16|I[1403]<<24);F[b+14|0]=d;F[b+15|0]=d>>>8;F[b+16|0]=d>>>16;F[b+17|0]=d>>>24;F[b+18|0]=c;F[b+19|0]=c>>>8;F[b+20|0]=c>>>16;F[b+21|0]=c>>>24;c=I[1398]|I[1399]<<8|(I[1400]<<16|I[1401]<<24);d=I[1394]|I[1395]<<8|(I[1396]<<16|I[1397]<<24);F[b+8|0]=d;F[b+9|0]=d>>>8;F[b+10|0]=d>>>16;F[b+11|0]=d>>>24;F[b+12|0]=c;F[b+13|0]=c>>>8;F[b+14|0]=c>>>16;F[b+15|0]=c>>>24;c=I[1390]|I[1391]<<8|(I[1392]<<16|I[1393]<<24);d=I[1386]|I[1387]<<8|(I[1388]<<16|I[1389]<<24);F[b|0]=d;F[b+1|0]=d>>>8;F[b+2|0]=d>>>16;F[b+3|0]=d>>>24;F[b+4|0]=c;F[b+5|0]=c>>>8;F[b+6|0]=c>>>16;F[b+7|0]=c>>>24;H[a>>2]=-5;za(a+4|0,b,22);oa(b);break a}c=e|c<<8;G[H[b+32>>2]+38>>1]=c;b:{if((c&65535)>>>0<259|G[h+26>>1]>=0){break b}i=ca-16|0;ca=i;e=pa(36);c=e;H[c+4>>2]=0;H[c+8>>2]=0;H[c+24>>2]=0;H[c+28>>2]=0;c=c+16|0;H[c>>2]=0;H[c+4>>2]=0;H[e>>2]=e+4;H[e+32>>2]=0;H[e+12>>2]=c;H[i>>2]=0;d=H[b+32>>2];j=ca-16|0;ca=j;c=0;c:{if(!e){break c}H[i>>2]=d;H[j+12>>2]=0;c=0;if(!Bb(1,j+12|0,d)){break c}m=H[j+12>>2];if(m){while(1){d:{if(Bb(1,j+8|0,H[i>>2])){c=pa(28);H[c+4>>2]=0;H[c+8>>2]=0;d=c+16|0;H[d>>2]=0;H[d+4>>2]=0;H[c>>2]=c+4;H[c+12>>2]=d;H[c+24>>2]=H[j+8>>2];if(ce(i,c)){break d}Ra(c+12|0,H[c+16>>2]);Qa(c,H[c+4>>2]);oa(c)}c=0;break c}f=ca-16|0;ca=f;H[f+8>>2]=c;e:{if(!c){break e}d=H[e+28>>2];f:{if(d>>>0>2]){H[f+8>>2]=0;H[d>>2]=c;H[e+28>>2]=d+4;break f}d=0;g:{h:{i:{g=H[e+24>>2];l=H[e+28>>2]-g>>2;c=l+1|0;if(c>>>0<1073741824){g=H[e+32>>2]-g|0;k=g>>>1|0;g=g>>>0>=2147483644?1073741823:c>>>0>>0?k:c;if(g){if(g>>>0>=1073741824){break i}d=pa(g<<2)}k=H[f+8>>2];H[f+8>>2]=0;c=(l<<2)+d|0;H[c>>2]=k;g=(g<<2)+d|0;l=c+4|0;d=H[e+28>>2];k=H[e+24>>2];if((d|0)==(k|0)){break h}while(1){d=d-4|0;o=H[d>>2];H[d>>2]=0;c=c-4|0;H[c>>2]=o;if((d|0)!=(k|0)){continue}break}H[e+32>>2]=g;g=H[e+28>>2];H[e+28>>2]=l;d=H[e+24>>2];H[e+24>>2]=c;if((d|0)==(g|0)){break g}while(1){g=g-4|0;c=H[g>>2];H[g>>2]=0;if(c){Ra(c+12|0,H[c+16>>2]);Qa(c,H[c+4>>2]);oa(c)}if((d|0)!=(g|0)){continue}break}break g}sa();v()}wa();v()}H[e+32>>2]=g;H[e+28>>2]=l;H[e+24>>2]=c}if(d){oa(d)}}c=H[f+8>>2];H[f+8>>2]=0;if(!c){break e}Ra(c+12|0,H[c+16>>2]);Qa(c,H[c+4>>2]);oa(c)}ca=f+16|0;n=n+1|0;if((m|0)!=(n|0)){continue}break}}c=ce(i,e)}ca=j+16|0;j:{if(c){d=H[b+4>>2];c=H[d+4>>2];H[d+4>>2]=e;if(c){Uc(c)}H[a>>2]=0;H[a+4>>2]=0;H[a+8>>2]=0;H[a+12>>2]=0;break j}c=pa(32);F[c+26|0]=0;d=I[1579]|I[1580]<<8;F[c+24|0]=d;F[c+25|0]=d>>>8;d=I[1575]|I[1576]<<8|(I[1577]<<16|I[1578]<<24);f=I[1571]|I[1572]<<8|(I[1573]<<16|I[1574]<<24);F[c+16|0]=f;F[c+17|0]=f>>>8;F[c+18|0]=f>>>16;F[c+19|0]=f>>>24;F[c+20|0]=d;F[c+21|0]=d>>>8;F[c+22|0]=d>>>16;F[c+23|0]=d>>>24;d=I[1567]|I[1568]<<8|(I[1569]<<16|I[1570]<<24);f=I[1563]|I[1564]<<8|(I[1565]<<16|I[1566]<<24);F[c+8|0]=f;F[c+9|0]=f>>>8;F[c+10|0]=f>>>16;F[c+11|0]=f>>>24;F[c+12|0]=d;F[c+13|0]=d>>>8;F[c+14|0]=d>>>16;F[c+15|0]=d>>>24;d=I[1559]|I[1560]<<8|(I[1561]<<16|I[1562]<<24);f=I[1555]|I[1556]<<8|(I[1557]<<16|I[1558]<<24);F[c|0]=f;F[c+1|0]=f>>>8;F[c+2|0]=f>>>16;F[c+3|0]=f>>>24;F[c+4|0]=d;F[c+5|0]=d>>>8;F[c+6|0]=d>>>16;F[c+7|0]=d>>>24;H[a>>2]=-1;za(a+4|0,c,26);oa(c);H[i+8>>2]=0;Uc(e)}ca=i+16|0;if(H[a>>2]){break a}if(F[a+15|0]>=0){break b}oa(H[a+4>>2])}if(!(ea[H[H[b>>2]+12>>2]](b)|0)){b=pa(48);F[b+33|0]=0;F[b+32|0]=I[1384];c=I[1380]|I[1381]<<8|(I[1382]<<16|I[1383]<<24);d=I[1376]|I[1377]<<8|(I[1378]<<16|I[1379]<<24);F[b+24|0]=d;F[b+25|0]=d>>>8;F[b+26|0]=d>>>16;F[b+27|0]=d>>>24;F[b+28|0]=c;F[b+29|0]=c>>>8;F[b+30|0]=c>>>16;F[b+31|0]=c>>>24;c=I[1372]|I[1373]<<8|(I[1374]<<16|I[1375]<<24);d=I[1368]|I[1369]<<8|(I[1370]<<16|I[1371]<<24);F[b+16|0]=d;F[b+17|0]=d>>>8;F[b+18|0]=d>>>16;F[b+19|0]=d>>>24;F[b+20|0]=c;F[b+21|0]=c>>>8;F[b+22|0]=c>>>16;F[b+23|0]=c>>>24;c=I[1364]|I[1365]<<8|(I[1366]<<16|I[1367]<<24);d=I[1360]|I[1361]<<8|(I[1362]<<16|I[1363]<<24);F[b+8|0]=d;F[b+9|0]=d>>>8;F[b+10|0]=d>>>16;F[b+11|0]=d>>>24;F[b+12|0]=c;F[b+13|0]=c>>>8;F[b+14|0]=c>>>16;F[b+15|0]=c>>>24;c=I[1356]|I[1357]<<8|(I[1358]<<16|I[1359]<<24);d=I[1352]|I[1353]<<8|(I[1354]<<16|I[1355]<<24);F[b|0]=d;F[b+1|0]=d>>>8;F[b+2|0]=d>>>16;F[b+3|0]=d>>>24;F[b+4|0]=c;F[b+5|0]=c>>>8;F[b+6|0]=c>>>16;F[b+7|0]=c>>>24;H[a>>2]=-1;za(a+4|0,b,33);oa(b);break a}if(!(ea[H[H[b>>2]+20>>2]](b)|0)){b=mc(h,1582);H[a>>2]=-1;a=a+4|0;if(F[b+11|0]>=0){c=H[b+4>>2];H[a>>2]=H[b>>2];H[a+4>>2]=c;H[a+8>>2]=H[b+8>>2];break a}za(a,H[b>>2],H[b+4>>2]);if(F[b+11|0]>=0){break a}oa(H[b>>2]);break a}if(!(ea[H[H[b>>2]+24>>2]](b)|0)){b=mc(h,1317);H[a>>2]=-1;a=a+4|0;if(F[b+11|0]>=0){c=H[b+4>>2];H[a>>2]=H[b>>2];H[a+4>>2]=c;H[a+8>>2]=H[b+8>>2];break a}za(a,H[b>>2],H[b+4>>2]);if(F[b+11|0]>=0){break a}oa(H[b>>2]);break a}H[a>>2]=0;H[a+4>>2]=0;H[a+8>>2]=0;H[a+12>>2]=0}ca=h+32|0}function pg(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0,x=0,y=0,z=0,A=0;m=ca-16|0;ca=m;H[m+12>>2]=b;b=pa(32);H[m>>2]=b;H[m+4>>2]=24;H[m+8>>2]=-2147483616;c=I[1206]|I[1207]<<8|(I[1208]<<16|I[1209]<<24);d=I[1202]|I[1203]<<8|(I[1204]<<16|I[1205]<<24);F[b+16|0]=d;F[b+17|0]=d>>>8;F[b+18|0]=d>>>16;F[b+19|0]=d>>>24;F[b+20|0]=c;F[b+21|0]=c>>>8;F[b+22|0]=c>>>16;F[b+23|0]=c>>>24;c=I[1198]|I[1199]<<8|(I[1200]<<16|I[1201]<<24);d=I[1194]|I[1195]<<8|(I[1196]<<16|I[1197]<<24);F[b+8|0]=d;F[b+9|0]=d>>>8;F[b+10|0]=d>>>16;F[b+11|0]=d>>>24;F[b+12|0]=c;F[b+13|0]=c>>>8;F[b+14|0]=c>>>16;F[b+15|0]=c>>>24;c=I[1190]|I[1191]<<8|(I[1192]<<16|I[1193]<<24);d=I[1186]|I[1187]<<8|(I[1188]<<16|I[1189]<<24);F[b|0]=d;F[b+1|0]=d>>>8;F[b+2|0]=d>>>16;F[b+3|0]=d>>>24;F[b+4|0]=c;F[b+5|0]=c>>>8;F[b+6|0]=c>>>16;F[b+7|0]=c>>>24;F[b+24|0]=0;l=ca-48|0;ca=l;f=H[m+12>>2];d=a;a=a+16|0;b=H[a>>2];a:{b:{if(!b){break b}c=a;while(1){e=(f|0)>H[b+16>>2];c=e?c:b;b=H[(e?b+4|0:b)>>2];if(b){continue}break}if((a|0)==(c|0)){break b}if((f|0)>=H[c+16>>2]){break a}}H[l+28>>2]=0;H[l+32>>2]=0;y=l+24|0;H[l+24>>2]=y|4;a=l+16|0;H[a>>2]=0;H[a+4>>2]=0;H[l+8>>2]=f;H[l+12>>2]=a;t=l+8|0;a=t;x=ca-16|0;ca=x;u=d+12|0;c=H[u+4>>2];c:{d:{if(!c){o=u+4|0;d=o;break d}a=H[a>>2];while(1){d=c;b=H[c+16>>2];if((b|0)>(a|0)){o=d;c=H[d>>2];if(c){continue}break d}if((a|0)<=(b|0)){g=d;a=0;break c}c=H[d+4>>2];if(c){continue}break}o=d+4|0}g=pa(32);b=H[t>>2];q=g+24|0;a=q;H[a>>2]=0;H[a+4>>2]=0;H[g+16>>2]=b;r=g+20|0;H[r>>2]=a;c=H[t+4>>2];z=t+8|0;if((c|0)!=(z|0)){while(1){p=ca-16|0;ca=p;a=p+8|0;k=c+16|0;e:{f:{g:{h:{i:{j:{k:{f=q;e=r+4|0;l:{if((f|0)==(e|0)){break l}b=I[f+27|0];h=b<<24>>24<0;i=I[k+11|0];n=i<<24>>24;j=(n|0)<0;i=j?H[k+4>>2]:i;b=h?H[f+20>>2]:b;s=i>>>0>b>>>0;w=s?b:i;if(w){j=j?H[k>>2]:k;h=h?H[f+16>>2]:f+16|0;A=Fa(j,h,w);if(!A){if(b>>>0>i>>>0){break l}break k}if((A|0)>=0){break k}break l}if(b>>>0<=i>>>0){break j}}h=H[f>>2];m:{a=f;n:{if((a|0)==H[r>>2]){break n}o:{if(!h){b=f;while(1){a=H[b+8>>2];i=H[a>>2]==(b|0);b=a;if(i){continue}break}break o}b=h;while(1){a=b;b=H[b+4>>2];if(b){continue}break}}i=I[k+11|0];s=i<<24>>24;b=(s|0)<0;j=I[a+27|0];n=j<<24>>24<0;p:{i=b?H[k+4>>2]:i;j=n?H[a+20>>2]:j;w=i>>>0>>0?i:j;if(w){b=Fa(n?H[a+16>>2]:a+16|0,b?H[k>>2]:k,w);if(b){break p}}if(i>>>0>j>>>0){break n}break m}if((b|0)>=0){break m}}if(!h){H[p+12>>2]=f;a=f;break e}H[p+12>>2]=a;a=a+4|0;break e}b=H[e>>2];if(!b){H[p+12>>2]=e;a=e;break e}h=(s|0)<0?H[k>>2]:k;f=e;while(1){a=b;b=I[b+27|0];e=b<<24>>24<0;b=e?H[a+20>>2]:b;k=b>>>0>>0;q:{r:{s:{t:{n=k?b:i;u:{if(n){e=e?H[a+16>>2]:a+16|0;j=Fa(h,e,n);if(!j){if(b>>>0>i>>>0){break u}break t}if((j|0)>=0){break t}break u}if(b>>>0<=i>>>0){break s}}f=a;b=H[a>>2];if(b){continue}break g}b=Fa(e,h,n);if(b){break r}}if(k){break q}break g}if((b|0)>=0){break g}}f=a+4|0;b=H[a+4>>2];if(b){continue}break}break g}b=Fa(h,j,w);if(b){break i}}if(s){break h}break f}if((b|0)>=0){break f}}h=H[f+4>>2];v:{if(!h){b=f;while(1){a=H[b+8>>2];j=H[a>>2]!=(b|0);b=a;if(j){continue}break}break v}b=h;while(1){a=b;b=H[b>>2];if(b){continue}break}}w:{x:{if((a|0)==(e|0)){break x}j=I[a+27|0];b=j<<24>>24<0;y:{j=b?H[a+20>>2]:j;s=i>>>0>j>>>0?j:i;if(s){b=Fa((n|0)<0?H[k>>2]:k,b?H[a+16>>2]:a+16|0,s);if(b){break y}}if(i>>>0>>0){break x}break w}if((b|0)>=0){break w}}if(!h){H[p+12>>2]=f;a=f+4|0;break e}H[p+12>>2]=a;break e}b=H[e>>2];if(!b){H[p+12>>2]=e;a=e;break e}h=(n|0)<0?H[k>>2]:k;f=e;while(1){a=b;b=I[b+27|0];e=b<<24>>24<0;b=e?H[a+20>>2]:b;k=b>>>0>>0;z:{A:{B:{C:{n=k?b:i;D:{if(n){e=e?H[a+16>>2]:a+16|0;j=Fa(h,e,n);if(!j){if(b>>>0>i>>>0){break D}break C}if((j|0)>=0){break C}break D}if(b>>>0<=i>>>0){break B}}f=a;b=H[a>>2];if(b){continue}break g}b=Fa(e,h,n);if(b){break A}}if(k){break z}break g}if((b|0)>=0){break g}}f=a+4|0;b=H[a+4>>2];if(b){continue}break}}H[p+12>>2]=a;a=f;break e}H[p+12>>2]=f;H[a>>2]=f}f=a;a=H[a>>2];if(a){b=0}else{a=pa(40);b=a+16|0;E:{if(F[c+27|0]>=0){e=H[c+20>>2];H[b>>2]=H[c+16>>2];H[b+4>>2]=e;H[b+8>>2]=H[c+24>>2];break E}za(b,H[c+16>>2],H[c+20>>2])}b=a+28|0;F:{if(F[c+39|0]>=0){e=H[c+32>>2];H[b>>2]=H[c+28>>2];H[b+4>>2]=e;H[b+8>>2]=H[c+36>>2];break F}za(b,H[c+28>>2],H[c+32>>2])}H[a+8>>2]=H[p+12>>2];H[a>>2]=0;H[a+4>>2]=0;H[f>>2]=a;b=a;e=H[H[r>>2]>>2];if(e){H[r>>2]=e;b=H[f>>2]}Sb(H[r+4>>2],b);H[r+8>>2]=H[r+8>>2]+1;b=1}F[x+12|0]=b;H[x+8>>2]=a;ca=p+16|0;b=H[c+4>>2];G:{if(b){while(1){c=b;b=H[b>>2];if(b){continue}break G}}while(1){a=c;c=H[c+8>>2];if((a|0)!=H[c>>2]){continue}break}}if((c|0)!=(z|0)){continue}break}}H[g+8>>2]=d;H[g>>2]=0;H[g+4>>2]=0;H[o>>2]=g;c=g;a=H[H[u>>2]>>2];if(a){H[u>>2]=a;c=H[o>>2]}Sb(H[u+4>>2],c);H[u+8>>2]=H[u+8>>2]+1;a=1}F[l+44|0]=a;H[l+40>>2]=g;ca=x+16|0;c=H[l+40>>2];Kb(t|4,H[l+16>>2]);Kb(y,H[l+28>>2])}f=ca-48|0;ca=f;d=f+8|0;g=ca-32|0;ca=g;o=g+32|0;b=o;a=g+21|0;H:{if((b|0)==(a|0)){break H}}e=b-a|0;I:{if((e|0)<=9){h=61;if((e|0)<(K[3660]<=1|0)){break I}}F[a|0]=49;b=a+1|0;h=0}H[g+12>>2]=h;H[g+8>>2]=b;h=ca-16|0;ca=h;e=ca-16|0;ca=e;J:{q=H[g+8>>2];g=q-a|0;if(g>>>0<=2147483631){K:{if(g>>>0<11){F[d+11|0]=g|I[d+11|0]&128;F[d+11|0]=I[d+11|0]&127;b=d;break K}t=e+8|0;if(g>>>0>=11){k=g+16&-16;b=k-1|0;b=(b|0)==11?k:b}else{b=10}Zb(t,b+1|0);b=H[e+8>>2];H[d>>2]=b;H[d+8>>2]=H[d+8>>2]&-2147483648|H[e+12>>2]&2147483647;H[d+8>>2]=H[d+8>>2]|-2147483648;H[d+4>>2]=g}while(1){if((a|0)!=(q|0)){F[b|0]=I[a|0];b=b+1|0;a=a+1|0;continue}break}F[e+7|0]=0;F[b|0]=I[e+7|0];ca=e+16|0;break J}Na();v()}ca=h+16|0;ca=o;H[f+32>>2]=m;L:{M:{a=c+20|0;d=H[a+4>>2];N:{if(!d){g=a+4|0;c=g;break N}b=I[m+11|0];c=b<<24>>24<0;e=c?H[m>>2]:m;b=c?H[m+4>>2]:b;while(1){c=d;d=I[c+27|0];g=d<<24>>24<0;d=g?H[c+20>>2]:d;o=d>>>0>>0;O:{P:{Q:{R:{h=o?d:b;S:{if(h){g=g?H[c+16>>2]:c+16|0;q=Fa(e,g,h);if(!q){if(b>>>0>>0){break S}break R}if((q|0)>=0){break R}break S}if(b>>>0>=d>>>0){break Q}}g=c;d=H[c>>2];if(d){continue}break N}d=Fa(g,e,h);if(d){break P}}if(o){break O}break M}if((d|0)>=0){break M}}d=H[c+4>>2];if(d){continue}break}g=c+4|0}d=pa(40);e=d+16|0;b=H[f+32>>2];T:{if(F[b+11|0]>=0){o=H[b+4>>2];H[e>>2]=H[b>>2];H[e+4>>2]=o;H[e+8>>2]=H[b+8>>2];break T}za(e,H[b>>2],H[b+4>>2])}H[d+8>>2]=c;H[d>>2]=0;H[d+4>>2]=0;H[d+36>>2]=0;H[d+28>>2]=0;H[d+32>>2]=0;H[g>>2]=d;c=d;b=H[H[a>>2]>>2];if(b){H[a>>2]=b;c=H[g>>2]}Sb(H[a+4>>2],c);H[a+8>>2]=H[a+8>>2]+1;a=1;break L}d=c;a=0}F[f+44|0]=a;H[f+40>>2]=d;a=H[f+40>>2];if(F[a+39|0]<0){oa(H[a+28>>2])}b=H[f+12>>2];H[a+28>>2]=H[f+8>>2];H[a+32>>2]=b;H[a+36>>2]=H[f+16>>2];ca=f+48|0;ca=l+48|0;if(F[m+11|0]<0){oa(H[m>>2])}ca=m+16|0}function Bd(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0;j=H[b+8>>2];e=H[b+12>>2];g=H[b+20>>2];h=H[b+16>>2];k=h+4|0;g=k>>>0<4?g+1|0:g;a:{if(j>>>0>>0&(e|0)<=(g|0)|(e|0)<(g|0)){break a}h=h+H[b>>2]|0;H[a>>2]=I[h|0]|I[h+1|0]<<8|(I[h+2|0]<<16|I[h+3|0]<<24);h=H[b+20>>2];e=h;j=H[b+16>>2];g=j+4|0;h=g>>>0<4?e+1|0:e;H[b+16>>2]=g;H[b+20>>2]=h;if(K[a>>2]>32){break a}l=H[b+8>>2];k=H[b+12>>2];h=e;e=j+8|0;h=e>>>0<8?h+1|0:h;if(e>>>0>l>>>0&(h|0)>=(k|0)|(h|0)>(k|0)){break a}h=H[b>>2]+g|0;g=I[h|0]|I[h+1|0]<<8|(I[h+2|0]<<16|I[h+3|0]<<24);H[a+4>>2]=g;h=H[b+20>>2];e=H[b+16>>2]+4|0;h=e>>>0<4?h+1|0:h;H[b+16>>2]=e;H[b+20>>2]=h;if(!g){return 1}if(d>>>0>>0){break a}H[a+8>>2]=0;if(!ua(a+16|0,b)){break a}if(!ua(a+36|0,b)){break a}if(!ua(a+56|0,b)){break a}if(!ua(a+76|0,b)){break a}s=H[a+4>>2];h=c;b=0;g=0;e=ca-32|0;ca=e;d=a;a=H[a+12>>2];H[e+16>>2]=0;H[e+8>>2]=0;H[e+12>>2]=0;b:{c:{if(a){if(a>>>0>=1073741824){break c}c=a<<2;b=pa(c);H[e+8>>2]=b;g=b+c|0;H[e+16>>2]=g;ra(b,0,c);H[e+12>>2]=g}c=H[d+120>>2];i=H[c>>2];if(i){H[c+4>>2]=i;oa(i);g=H[e+12>>2];b=H[e+8>>2];a=H[d+12>>2]}H[c+4>>2]=g;H[c>>2]=b;H[c+8>>2]=H[e+16>>2];b=0;H[e+16>>2]=0;H[e+8>>2]=0;H[e+12>>2]=0;d:{if(a){if(a>>>0>=1073741824){break d}a=a<<2;f=pa(a);H[e+8>>2]=f;b=a+f|0;H[e+16>>2]=b;ra(f,0,a);H[e+12>>2]=b}a=H[d+132>>2];c=H[a>>2];if(c){H[a+4>>2]=c;oa(c);f=H[e+8>>2];b=H[e+12>>2]}H[a+4>>2]=b;H[a>>2]=f;H[a+8>>2]=H[e+16>>2];H[e+24>>2]=0;H[e+28>>2]=0;H[e+16>>2]=0;H[e+20>>2]=0;H[e+8>>2]=0;H[e+12>>2]=0;xa(e+8|0);a=H[e+24>>2]+H[e+28>>2]|0;b=(a>>>0)/341|0;a=H[H[e+12>>2]+(b<<2)>>2]+N(a-N(b,341)|0,12)|0;H[a+4>>2]=0;H[a+8>>2]=0;H[a>>2]=s;c=1;a=H[e+28>>2]+1|0;H[e+28>>2]=a;e:{if(!a){break e}while(1){b=H[e+12>>2];f=H[e+24>>2];k=a-1|0;c=f+k|0;i=(c>>>0)/341|0;c=H[b+(i<<2)>>2]+N(c-N(i,341)|0,12)|0;g=H[c+8>>2];i=H[c+4>>2];j=H[c>>2];H[e+28>>2]=k;c=H[e+16>>2];if((((b|0)!=(c|0)?N(c-b>>2,341)-1|0:0)-(a+f|0)|0)+1>>>0>=682){oa(H[c-4>>2]);H[e+16>>2]=H[e+16>>2]-4}c=0;if(j>>>0>s>>>0){break e}b=H[d+12>>2];a=(b-1|0)!=(i|0)?i+1|0:0;if(a>>>0>=b>>>0){break e}f=N(g,12);o=f+H[d+132>>2]|0;k=f+H[d+120>>2]|0;i=H[d>>2];l=a<<2;m=H[l+H[o>>2]>>2];f:{g:{if((i|0)==(m|0)){if(!j){break g}o=0;b=H[h+20>>2];g=H[h+16>>2];if((b|0)==(g|0)){a=H[d+8>>2];H[h+28>>2]=j+H[h+28>>2];H[d+8>>2]=a+j;break g}while(1){c=(b|0)==(g|0);a=b;i=0;b=g;h:{if(c){break h}while(1){f=H[h+28>>2];b=a;c=N(i,20)+g|0;l=H[c>>2];if(!I[l+84|0]){f=H[H[l+68>>2]+(f<<2)>>2]}if(K[l+80>>2]<=f>>>0){break h}m=H[k>>2]+(H[c+4>>2]<<2)|0;g=H[c+12>>2];b=m;i:{if(g>>>0>3){break i}a=0;b=H[h+12>>2];if(!H[c+16>>2]){break i}while(1){b=qa(b,m+(a<<2)|0,g);g=H[c+12>>2];b=b+g|0;a=a+1|0;if(a>>>0>2]){continue}break}b=H[h+12>>2]}a=H[l+40>>2];qa(H[H[l>>2]>>2]+N(a,f)|0,b,a);i=i+1|0;a=H[h+20>>2];b=a;g=H[h+16>>2];if(i>>>0<(b-g|0)/20>>>0){continue}break}}H[h+28>>2]=H[h+28>>2]+1;H[d+8>>2]=H[d+8>>2]+1;o=o+1|0;if((j|0)!=(o|0)){continue}break}break g}j:{k:{l:{m:{if(j>>>0<=2){c=H[d+108>>2];H[c>>2]=a;f=1;b=H[d+12>>2];if(b>>>0>1){break m}break j}if(K[d+8>>2]>K[d+4>>2]){break e}b=H[d+120>>2];n=g+1|0;o=N(n,12);p=b+o|0;if((p|0)!=(k|0)){Aa(p,H[k>>2],H[k+4>>2]);b=H[d+120>>2]}b=l+H[b+o>>2]|0;H[b>>2]=H[b>>2]+(1<>2];m=32-i|0;n:{if((b|0)<=(m|0)){k=H[d+28>>2];if((k|0)==H[d+20>>2]){break l}m=H[k>>2];p=b+i|0;H[d+32>>2]=p;b=m<>>32-b|0;if((p|0)!=32){break n}H[d+32>>2]=0;H[d+28>>2]=k+4;break n}k=H[d+28>>2];p=k+4|0;if((p|0)==H[d+20>>2]){break l}r=H[k>>2];H[d+28>>2]=p;m=b-m|0;H[d+32>>2]=m;b=H[k+4>>2]>>>32-m|r<>>32-b}i=j>>>1|0;if(i>>>0>>0){break e}break k}while(1){a=(b-1|0)!=(a|0)?a+1|0:0;H[c+(f<<2)>>2]=a;b=H[d+12>>2];f=f+1|0;if(b>>>0>f>>>0){continue}break}break j}i=j>>>1|0;b=0}o:{p:{b=i-b|0;c=j-b|0;q:{if((c|0)==(b|0)){c=b;break q}i=H[d+88>>2];if((i|0)==H[d+80>>2]){break p}j=H[i>>2];k=H[d+92>>2];m=k+1|0;H[d+92>>2]=m;j=j&-2147483648>>>k;r:{if((m|0)==32){H[d+92>>2]=0;H[d+88>>2]=i+4;if(j){break r}break p}if(!j){break p}}}i=c;c=b;break o}i=b}b=H[d+132>>2];j=b+f|0;f=H[j>>2];k=f+l|0;H[k>>2]=H[k>>2]+1;Aa(b+o|0,f,H[j+4>>2]);if(c){b=H[e+28>>2]+H[e+24>>2]|0;j=H[e+16>>2];f=H[e+12>>2];if((b|0)==(((f|0)!=(j|0)?N(j-f>>2,341)-1|0:0)|0)){xa(e+8|0);f=H[e+12>>2];b=H[e+24>>2]+H[e+28>>2]|0}j=(b>>>0)/341|0;b=H[(j<<2)+f>>2]+N(b-N(j,341)|0,12)|0;H[b+8>>2]=g;H[b+4>>2]=a;H[b>>2]=c;H[e+28>>2]=H[e+28>>2]+1}if(!i){break g}b=H[e+28>>2]+H[e+24>>2]|0;c=H[e+16>>2];f=H[e+12>>2];if((b|0)==(((c|0)!=(f|0)?N(c-f>>2,341)-1|0:0)|0)){xa(e+8|0);f=H[e+12>>2];b=H[e+24>>2]+H[e+28>>2]|0}c=(b>>>0)/341|0;b=H[(c<<2)+f>>2]+N(b-N(c,341)|0,12)|0;H[b+8>>2]=n;H[b+4>>2]=a;H[b>>2]=i;a=H[e+28>>2]+1|0;H[e+28>>2]=a;break f}m=0;if(!j){break g}while(1){if(H[d+12>>2]){i=H[d+40>>2];p=H[o>>2];c=H[d+96>>2];r=H[d+108>>2];a=0;while(1){g=r+(a<<2)|0;H[c+(H[g>>2]<<2)>>2]=0;b=H[d>>2];f=H[g>>2]<<2;l=H[f+p>>2];s:{if((b|0)==(l|0)){break s}f=c+f|0;b=b-l|0;l=H[d+52>>2];q=32-l|0;if((b|0)<=(q|0)){n=H[d+48>>2];if((n|0)==(i|0)){c=0;break e}H[f>>2]=H[n>>2]<>>32-b;b=b+H[d+52>>2]|0;H[d+52>>2]=b;if((b|0)!=32){break s}H[d+52>>2]=0;H[d+48>>2]=n+4;break s}n=H[d+48>>2];t=n+4|0;if((i|0)==(t|0)){c=0;break e}u=H[n>>2];H[d+48>>2]=t;q=b-q|0;H[d+52>>2]=q;H[f>>2]=H[n+4>>2]>>>32-q|u<>>32-b}b=H[g>>2]<<2;g=b+c|0;H[g>>2]=H[g>>2]|H[b+H[k>>2]>>2];a=a+1|0;if(a>>>0>2]){continue}break}}i=0;a=H[h+16>>2];t:{if((a|0)==H[h+20>>2]){break t}while(1){f=H[h+28>>2];c=N(i,20)+a|0;l=H[c>>2];if(!I[l+84|0]){f=H[H[l+68>>2]+(f<<2)>>2]}if(K[l+80>>2]<=f>>>0){break t}n=H[d+96>>2]+(H[c+4>>2]<<2)|0;g=H[c+12>>2];b=n;u:{if(g>>>0>3){break u}a=0;b=H[h+12>>2];if(!H[c+16>>2]){break u}while(1){b=qa(b,n+(a<<2)|0,g);g=H[c+12>>2];b=b+g|0;a=a+1|0;if(a>>>0>2]){continue}break}b=H[h+12>>2]}a=H[l+40>>2];qa(H[H[l>>2]>>2]+N(a,f)|0,b,a);i=i+1|0;a=H[h+16>>2];if(i>>>0<(H[h+20>>2]-a|0)/20>>>0){continue}break}}H[h+28>>2]=H[h+28>>2]+1;H[d+8>>2]=H[d+8>>2]+1;m=m+1|0;if((j|0)!=(m|0)){continue}break}}a=H[e+28>>2]}if(a){continue}break}c=1}H[e+28>>2]=0;f=H[e+16>>2];a=H[e+12>>2];b=f-a|0;if(b>>>0>=9){while(1){oa(H[a>>2]);a=H[e+12>>2]+4|0;H[e+12>>2]=a;f=H[e+16>>2];b=f-a|0;if(b>>>0>8){continue}break}}g=170;v:{switch((b>>>2|0)-1|0){case 1:g=341;case 0:H[e+24>>2]=g;break;default:break v}}w:{if((a|0)==(f|0)){break w}while(1){oa(H[a>>2]);a=a+4|0;if((f|0)!=(a|0)){continue}break}a=H[e+16>>2];b=H[e+12>>2];if((a|0)==(b|0)){break w}H[e+16>>2]=a+((b-a|0)+3&-4)}a=H[e+8>>2];if(a){oa(a)}ca=e+32|0;break b}sa();v()}sa();v()}i=c}return i}function zd(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0;j=H[b+8>>2];l=H[b+12>>2];k=H[b+20>>2];i=H[b+16>>2];f=i+4|0;k=f>>>0<4?k+1|0:k;a:{if(f>>>0>j>>>0&(k|0)>=(l|0)|(k|0)>(l|0)){break a}i=i+H[b>>2]|0;H[a>>2]=I[i|0]|I[i+1|0]<<8|(I[i+2|0]<<16|I[i+3|0]<<24);i=H[b+20>>2];j=i;f=H[b+16>>2];i=f+4|0;l=i>>>0<4?j+1|0:j;H[b+16>>2]=i;H[b+20>>2]=l;if(K[a>>2]>32){break a}l=H[b+8>>2];k=H[b+12>>2];f=f+8|0;j=f>>>0<8?j+1|0:j;if((k|0)<=(j|0)&f>>>0>l>>>0|(k|0)<(j|0)){break a}i=H[b>>2]+i|0;f=I[i|0]|I[i+1|0]<<8|(I[i+2|0]<<16|I[i+3|0]<<24);H[a+4>>2]=f;j=H[b+20>>2];i=H[b+16>>2]+4|0;j=i>>>0<4?j+1|0:j;H[b+16>>2]=i;H[b+20>>2]=j;if(!f){return 1}if(d>>>0>>0){break a}H[a+8>>2]=0;if(!ua(a+16|0,b)){break a}if(!ua(a+36|0,b)){break a}if(!ua(a+56|0,b)){break a}if(!ua(a+76|0,b)){break a}t=H[a+4>>2];i=c;b=0;c=0;e=ca-32|0;ca=e;f=a;a=H[a+12>>2];H[e+16>>2]=0;H[e+8>>2]=0;H[e+12>>2]=0;b:{c:{if(a){if(a>>>0>=1073741824){break c}d=a<<2;b=pa(d);H[e+8>>2]=b;c=b+d|0;H[e+16>>2]=c;ra(b,0,d);H[e+12>>2]=c}g=H[f+120>>2];d=H[g>>2];if(d){H[g+4>>2]=d;oa(d);c=H[e+12>>2];b=H[e+8>>2];a=H[f+12>>2]}H[g+4>>2]=c;H[g>>2]=b;H[g+8>>2]=H[e+16>>2];b=0;H[e+16>>2]=0;H[e+8>>2]=0;H[e+12>>2]=0;d:{if(a){if(a>>>0>=1073741824){break d}a=a<<2;h=pa(a);H[e+8>>2]=h;b=a+h|0;H[e+16>>2]=b;ra(h,0,a);H[e+12>>2]=b}c=H[f+132>>2];a=H[c>>2];if(a){H[c+4>>2]=a;oa(a);h=H[e+8>>2];b=H[e+12>>2]}H[c+4>>2]=b;H[c>>2]=h;H[c+8>>2]=H[e+16>>2];H[e+24>>2]=0;H[e+28>>2]=0;H[e+16>>2]=0;H[e+20>>2]=0;H[e+8>>2]=0;H[e+12>>2]=0;xa(e+8|0);b=H[e+24>>2]+H[e+28>>2]|0;a=(b>>>0)/341|0;a=H[H[e+12>>2]+(a<<2)>>2]+N(b-N(a,341)|0,12)|0;H[a+4>>2]=0;H[a+8>>2]=0;H[a>>2]=t;d=1;a=H[e+28>>2]+1|0;H[e+28>>2]=a;e:{if(!a){break e}while(1){j=H[e+12>>2];g=H[e+24>>2];d=a-1|0;c=g+d|0;b=(c>>>0)/341|0;b=H[j+(b<<2)>>2]+N(c-N(b,341)|0,12)|0;n=H[b+8>>2];c=H[b+4>>2];m=H[b>>2];H[e+28>>2]=d;b=H[e+16>>2];if((((b|0)!=(j|0)?N(b-j>>2,341)-1|0:0)-(a+g|0)|0)+1>>>0>=682){oa(H[b-4>>2]);H[e+16>>2]=H[e+16>>2]-4}if(m>>>0>t>>>0){d=0;break e}d=0;b=H[f+12>>2];a=(c|0)!=(b-1|0)?c+1|0:0;if(a>>>0>=b>>>0){break e}b=H[f+120>>2];o=N(n,12);q=b+o|0;g=H[f>>2];h=a<<2;l=o+H[f+132>>2]|0;c=H[h+H[l>>2]>>2];f:{g:{if((g|0)==(c|0)){if(!m){break g}h=0;b=H[i+20>>2];c=H[i+16>>2];if((b|0)==(c|0)){a=H[f+8>>2];H[i+28>>2]=m+H[i+28>>2];H[f+8>>2]=a+m;break g}while(1){d=(b|0)==(c|0);a=b;g=0;b=c;h:{if(d){break h}while(1){d=H[i+28>>2];b=a;k=N(g,20)+c|0;l=H[k>>2];if(!I[l+84|0]){d=H[H[l+68>>2]+(d<<2)>>2]}if(K[l+80>>2]<=d>>>0){break h}j=H[q>>2]+(H[k+4>>2]<<2)|0;c=H[k+12>>2];b=j;i:{if(c>>>0>3){break i}a=0;b=H[i+12>>2];if(!H[k+16>>2]){break i}while(1){b=qa(b,j+(a<<2)|0,c);c=H[k+12>>2];b=b+c|0;a=a+1|0;if(a>>>0>2]){continue}break}b=H[i+12>>2]}a=H[l+40>>2];qa(H[H[l>>2]>>2]+N(a,d)|0,b,a);g=g+1|0;a=H[i+20>>2];b=a;c=H[i+16>>2];if(g>>>0<(b-c|0)/20>>>0){continue}break}}H[i+28>>2]=H[i+28>>2]+1;H[f+8>>2]=H[f+8>>2]+1;h=h+1|0;if((m|0)!=(h|0)){continue}break}break g}j:{k:{l:{m:{if(m>>>0<=2){c=H[f+108>>2];H[c>>2]=a;h=1;b=H[f+12>>2];if(b>>>0>1){break m}break j}if(K[f+8>>2]>K[f+4>>2]){break e}j=b;b=o+12|0;Aa(j+b|0,H[q>>2],H[q+4>>2]);b=h+H[b+H[f+120>>2]>>2]|0;H[b>>2]=H[b>>2]+(1<>2];g=32-l|0;n:{if((k|0)<=(g|0)){g=H[f+28>>2];if((g|0)==H[f+20>>2]){break l}c=H[g>>2];b=k+l|0;H[f+32>>2]=b;c=c<>>32-k|0;if((b|0)!=32){break n}H[f+32>>2]=0;H[f+28>>2]=g+4;break n}j=H[f+28>>2];b=j+4|0;if((b|0)==H[f+20>>2]){break l}c=H[j>>2];H[f+28>>2]=b;b=k-g|0;H[f+32>>2]=b;c=H[j+4>>2]>>>32-b|c<>>32-k}g=m>>>1|0;if(g>>>0>>0){break e}break k}while(1){a=(b-1|0)!=(a|0)?a+1|0:0;H[c+(h<<2)>>2]=a;b=H[f+12>>2];h=h+1|0;if(b>>>0>h>>>0){continue}break}break j}g=m>>>1|0;c=0}k=n+1|0;o:{p:{b=g-c|0;c=m-b|0;q:{if((c|0)==(b|0)){c=b;break q}l=H[f+88>>2];if((l|0)==H[f+80>>2]){break p}j=H[l>>2];g=H[f+92>>2];d=g+1|0;H[f+92>>2]=d;g=j&-2147483648>>>g;r:{if((d|0)==32){H[f+92>>2]=0;H[f+88>>2]=l+4;if(g){break r}break p}if(!g){break p}}}g=c;c=b;break o}g=b}l=H[f+132>>2];j=l+o|0;d=H[j>>2];b=d+h|0;H[b>>2]=H[b>>2]+1;Aa(l+N(k,12)|0,d,H[j+4>>2]);if(c){b=H[e+28>>2]+H[e+24>>2]|0;d=H[e+16>>2];h=H[e+12>>2];if((b|0)==(((d|0)!=(h|0)?N(d-h>>2,341)-1|0:0)|0)){xa(e+8|0);h=H[e+12>>2];b=H[e+24>>2]+H[e+28>>2]|0}d=(b>>>0)/341|0;b=H[(d<<2)+h>>2]+N(b-N(d,341)|0,12)|0;H[b+8>>2]=n;H[b+4>>2]=a;H[b>>2]=c;H[e+28>>2]=H[e+28>>2]+1}if(!g){break g}b=H[e+28>>2]+H[e+24>>2]|0;c=H[e+16>>2];h=H[e+12>>2];if((b|0)==(((c|0)!=(h|0)?N(c-h>>2,341)-1|0:0)|0)){xa(e+8|0);h=H[e+12>>2];b=H[e+24>>2]+H[e+28>>2]|0}c=(b>>>0)/341|0;b=H[(c<<2)+h>>2]+N(b-N(c,341)|0,12)|0;H[b+8>>2]=k;H[b+4>>2]=a;H[b>>2]=g;a=H[e+28>>2]+1|0;H[e+28>>2]=a;break f}r=0;if(!m){break g}while(1){if(H[f+12>>2]){u=H[f+40>>2];j=H[l>>2];s=H[f+96>>2];g=H[f+108>>2];a=0;while(1){n=(a<<2)+g|0;H[s+(H[n>>2]<<2)>>2]=0;d=H[f>>2];c=H[n>>2]<<2;b=H[c+j>>2];s:{if((d|0)==(b|0)){break s}o=c+s|0;p=d-b|0;h=H[f+52>>2];d=32-h|0;if((p|0)<=(d|0)){c=H[f+48>>2];if((c|0)==(u|0)){d=0;break e}H[o>>2]=H[c>>2]<>>32-p;b=p+H[f+52>>2]|0;H[f+52>>2]=b;if((b|0)!=32){break s}H[f+52>>2]=0;H[f+48>>2]=c+4;break s}k=H[f+48>>2];b=k+4|0;if((u|0)==(b|0)){d=0;break e}c=H[k>>2];H[f+48>>2]=b;b=p-d|0;H[f+52>>2]=b;H[o>>2]=H[k+4>>2]>>>32-b|c<>>32-p}c=H[n>>2]<<2;b=c+s|0;H[b>>2]=H[b>>2]|H[c+H[q>>2]>>2];a=a+1|0;if(a>>>0>2]){continue}break}}g=0;a=H[i+16>>2];t:{if((a|0)==H[i+20>>2]){break t}while(1){d=H[i+28>>2];h=N(g,20)+a|0;k=H[h>>2];if(!I[k+84|0]){d=H[H[k+68>>2]+(d<<2)>>2]}if(K[k+80>>2]<=d>>>0){break t}j=H[f+96>>2]+(H[h+4>>2]<<2)|0;c=H[h+12>>2];b=j;u:{if(c>>>0>3){break u}a=0;b=H[i+12>>2];if(!H[h+16>>2]){break u}while(1){b=qa(b,j+(a<<2)|0,c);c=H[h+12>>2];b=b+c|0;a=a+1|0;if(a>>>0>2]){continue}break}b=H[i+12>>2]}a=H[k+40>>2];qa(H[H[k>>2]>>2]+N(a,d)|0,b,a);g=g+1|0;a=H[i+16>>2];if(g>>>0<(H[i+20>>2]-a|0)/20>>>0){continue}break}}H[i+28>>2]=H[i+28>>2]+1;H[f+8>>2]=H[f+8>>2]+1;r=r+1|0;if((m|0)!=(r|0)){continue}break}}a=H[e+28>>2]}if(a){continue}break}d=1}H[e+28>>2]=0;h=H[e+16>>2];a=H[e+12>>2];b=h-a|0;if(b>>>0>=9){while(1){oa(H[a>>2]);a=H[e+12>>2]+4|0;H[e+12>>2]=a;h=H[e+16>>2];b=h-a|0;if(b>>>0>8){continue}break}}c=170;v:{switch((b>>>2|0)-1|0){case 1:c=341;case 0:H[e+24>>2]=c;break;default:break v}}w:{if((a|0)==(h|0)){break w}while(1){oa(H[a>>2]);a=a+4|0;if((h|0)!=(a|0)){continue}break}b=H[e+16>>2];a=H[e+12>>2];if((b|0)==(a|0)){break w}H[e+16>>2]=b+((a-b|0)+3&-4)}a=H[e+8>>2];if(a){oa(a)}ca=e+32|0;g=d;break b}sa();v()}sa();v()}}return g}function wd(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0;i=H[b+8>>2];j=H[b+12>>2];n=H[b+20>>2];e=H[b+16>>2];h=e+4|0;n=h>>>0<4?n+1|0:n;a:{if(i>>>0>>0&(j|0)<=(n|0)|(j|0)<(n|0)){break a}e=e+H[b>>2]|0;H[a>>2]=I[e|0]|I[e+1|0]<<8|(I[e+2|0]<<16|I[e+3|0]<<24);e=H[b+20>>2];i=e;h=H[b+16>>2];e=h+4|0;j=e>>>0<4?i+1|0:i;H[b+16>>2]=e;H[b+20>>2]=j;if(K[a>>2]>32){break a}j=H[b+8>>2];n=H[b+12>>2];h=h+8|0;i=h>>>0<8?i+1|0:i;if(h>>>0>j>>>0&(i|0)>=(n|0)|(i|0)>(n|0)){break a}e=H[b>>2]+e|0;h=I[e|0]|I[e+1|0]<<8|(I[e+2|0]<<16|I[e+3|0]<<24);H[a+4>>2]=h;i=H[b+20>>2];e=H[b+16>>2]+4|0;i=e>>>0<4?i+1|0:i;H[b+16>>2]=e;H[b+20>>2]=i;if(!h){return 1}if(d>>>0>>0){break a}H[a+8>>2]=0;if(!sb(a+16|0,b)){break a}if(!ua(a+544|0,b)){break a}if(!ua(a+564|0,b)){break a}if(!ua(a+584|0,b)){break a}u=H[a+4>>2];d=c;b=0;c=0;f=ca-32|0;ca=f;g=a;a=H[a+12>>2];H[f+16>>2]=0;H[f+8>>2]=0;H[f+12>>2]=0;b:{c:{if(a){if(a>>>0>=1073741824){break c}e=a<<2;b=pa(e);H[f+8>>2]=b;c=b+e|0;H[f+16>>2]=c;ra(b,0,e);H[f+12>>2]=c}h=H[g+628>>2];e=H[h>>2];if(e){H[h+4>>2]=e;oa(e);c=H[f+12>>2];b=H[f+8>>2];a=H[g+12>>2]}H[h+4>>2]=c;H[h>>2]=b;H[h+8>>2]=H[f+16>>2];b=0;H[f+16>>2]=0;H[f+8>>2]=0;H[f+12>>2]=0;d:{if(a){if(a>>>0>=1073741824){break d}a=a<<2;k=pa(a);H[f+8>>2]=k;b=a+k|0;H[f+16>>2]=b;ra(k,0,a);H[f+12>>2]=b}c=H[g+640>>2];a=H[c>>2];if(a){H[c+4>>2]=a;oa(a);k=H[f+8>>2];b=H[f+12>>2]}H[c+4>>2]=b;H[c>>2]=k;H[c+8>>2]=H[f+16>>2];H[f+24>>2]=0;H[f+28>>2]=0;H[f+16>>2]=0;H[f+20>>2]=0;H[f+8>>2]=0;H[f+12>>2]=0;xa(f+8|0);b=H[f+24>>2]+H[f+28>>2]|0;a=(b>>>0)/341|0;a=H[H[f+12>>2]+(a<<2)>>2]+N(b-N(a,341)|0,12)|0;H[a+4>>2]=0;H[a+8>>2]=0;H[a>>2]=u;c=1;a=H[f+28>>2]+1|0;H[f+28>>2]=a;e:{if(!a){break e}n=g+16|0;while(1){j=H[f+12>>2];h=H[f+24>>2];e=a-1|0;c=h+e|0;b=(c>>>0)/341|0;b=H[j+(b<<2)>>2]+N(c-N(b,341)|0,12)|0;q=H[b+8>>2];i=H[b+4>>2];o=H[b>>2];H[f+28>>2]=e;b=H[f+16>>2];if((((b|0)!=(j|0)?N(b-j>>2,341)-1|0:0)-(a+h|0)|0)+1>>>0>=682){oa(H[b-4>>2]);H[f+16>>2]=H[f+16>>2]-4}c=0;if(o>>>0>u>>>0){break e}a=H[g+12>>2];k=(i|0)!=(a-1|0)?i+1|0:0;if(k>>>0>=a>>>0){break e}p=N(q,12);w=p+H[g+640>>2]|0;r=p+H[g+628>>2]|0;h=H[g>>2];l=k<<2;e=H[l+H[w>>2]>>2];f:{g:{if((h|0)==(e|0)){if(!o){break g}c=H[d+16>>2];b=H[d+20>>2];m=0;while(1){e=(b|0)==(c|0);a=b;j=0;b=c;h:{if(e){break h}while(1){l=H[d+28>>2];b=a;i=N(j,20)+c|0;h=H[i>>2];if(!I[h+84|0]){l=H[H[h+68>>2]+(l<<2)>>2]}if(K[h+80>>2]<=l>>>0){break h}e=H[r>>2]+(H[i+4>>2]<<2)|0;c=H[i+12>>2];b=e;i:{if(c>>>0>3){break i}a=0;b=H[d+12>>2];if(!H[i+16>>2]){break i}while(1){b=qa(b,e+(a<<2)|0,c);c=H[i+12>>2];b=b+c|0;a=a+1|0;if(a>>>0>2]){continue}break}b=H[d+12>>2]}a=H[h+40>>2];qa(H[H[h>>2]>>2]+N(a,l)|0,b,a);a=H[d+20>>2];b=a;j=j+1|0;c=H[d+16>>2];if(j>>>0<(a-c|0)/20>>>0){continue}break}}H[d+28>>2]=H[d+28>>2]+1;H[g+8>>2]=H[g+8>>2]+1;m=m+1|0;if((o|0)!=(m|0)){continue}break}break g}j:{k:{l:{if(o>>>0<=2){c=H[g+616>>2];H[c>>2]=k;a=1;b=H[g+12>>2];if(b>>>0>1){break l}break j}if(K[g+8>>2]>K[g+4>>2]){break e}a=H[g+628>>2];j=q+1|0;m=N(j,12);b=a+m|0;if((b|0)!=(r|0)){Aa(b,H[r>>2],H[r+4>>2]);a=H[g+628>>2]}a=l+H[a+m>>2]|0;H[a>>2]=H[a>>2]+(1<>>1|0;break k}while(1){b=Ba((a<<4)+n|0)|b<<1;a=a+1|0;if((c|0)!=(a|0)){continue}break}a=o>>>1|0;if(b>>>0<=a>>>0){break k}c=0;break e}while(1){k=(b-1|0)!=(k|0)?k+1|0:0;H[c+(a<<2)>>2]=k;a=a+1|0;b=H[g+12>>2];if(a>>>0>>0){continue}break}break j}m:{n:{b=a-b|0;a=o-b|0;o:{if((a|0)==(b|0)){a=b;break o}i=H[g+596>>2];if((i|0)==H[g+588>>2]){break n}h=H[i>>2];e=H[g+600>>2];c=e+1|0;H[g+600>>2]=c;e=h&-2147483648>>>e;p:{if((c|0)==32){H[g+600>>2]=0;H[g+596>>2]=i+4;if(e){break p}break n}if(!e){break n}}}c=a;a=b;break m}c=b}i=H[g+640>>2];h=i+p|0;e=H[h>>2];b=e+l|0;H[b>>2]=H[b>>2]+1;Aa(i+m|0,e,H[h+4>>2]);if(a){m=H[f+28>>2]+H[f+24>>2]|0;e=H[f+16>>2];b=H[f+12>>2];if((m|0)==(((b|0)!=(e|0)?N(e-b>>2,341)-1|0:0)|0)){xa(f+8|0);m=H[f+24>>2]+H[f+28>>2]|0;e=H[f+12>>2]}else{e=b}b=(m>>>0)/341|0;b=H[e+(b<<2)>>2]+N(m-N(b,341)|0,12)|0;H[b+8>>2]=q;H[b+4>>2]=k;H[b>>2]=a;H[f+28>>2]=H[f+28>>2]+1}if(!c){break g}b=H[f+28>>2]+H[f+24>>2]|0;e=H[f+16>>2];a=H[f+12>>2];if((b|0)==(((a|0)!=(e|0)?N(e-a>>2,341)-1|0:0)|0)){xa(f+8|0);b=H[f+24>>2]+H[f+28>>2]|0;e=H[f+12>>2]}else{e=a}a=(b>>>0)/341|0;a=H[e+(a<<2)>>2]+N(b-N(a,341)|0,12)|0;H[a+8>>2]=j;H[a+4>>2]=k;H[a>>2]=c;a=H[f+28>>2]+1|0;H[f+28>>2]=a;break f}k=0;if(!o){break g}while(1){if(H[g+12>>2]){q=H[g+548>>2];i=H[w>>2];t=H[g+604>>2];h=H[g+616>>2];a=0;while(1){p=(a<<2)+h|0;H[t+(H[p>>2]<<2)>>2]=0;e=H[g>>2];c=H[p>>2]<<2;b=H[c+i>>2];q:{if((e|0)==(b|0)){break q}l=c+t|0;s=e-b|0;m=H[g+560>>2];e=32-m|0;if((s|0)<=(e|0)){c=H[g+556>>2];if((c|0)==(q|0)){c=0;break e}H[l>>2]=H[c>>2]<>>32-s;b=s+H[g+560>>2]|0;H[g+560>>2]=b;if((b|0)!=32){break q}H[g+560>>2]=0;H[g+556>>2]=c+4;break q}j=H[g+556>>2];b=j+4|0;if((q|0)==(b|0)){c=0;break e}c=H[j>>2];H[g+556>>2]=b;b=s-e|0;H[g+560>>2]=b;H[l>>2]=H[j+4>>2]>>>32-b|c<>>32-s}c=H[p>>2]<<2;b=c+t|0;H[b>>2]=H[b>>2]|H[c+H[r>>2]>>2];a=a+1|0;if(a>>>0>2]){continue}break}}j=0;a=H[d+16>>2];r:{if((a|0)==H[d+20>>2]){break r}while(1){l=H[d+28>>2];i=N(j,20)+a|0;h=H[i>>2];if(!I[h+84|0]){l=H[H[h+68>>2]+(l<<2)>>2]}if(K[h+80>>2]<=l>>>0){break r}e=H[g+604>>2]+(H[i+4>>2]<<2)|0;c=H[i+12>>2];b=e;s:{if(c>>>0>3){break s}a=0;b=H[d+12>>2];if(!H[i+16>>2]){break s}while(1){b=qa(b,e+(a<<2)|0,c);c=H[i+12>>2];b=b+c|0;a=a+1|0;if(a>>>0>2]){continue}break}b=H[d+12>>2]}a=H[h+40>>2];qa(H[H[h>>2]>>2]+N(a,l)|0,b,a);j=j+1|0;a=H[d+16>>2];if(j>>>0<(H[d+20>>2]-a|0)/20>>>0){continue}break}}H[d+28>>2]=H[d+28>>2]+1;H[g+8>>2]=H[g+8>>2]+1;k=k+1|0;if((o|0)!=(k|0)){continue}break}}a=H[f+28>>2]}if(a){continue}break}c=1}H[f+28>>2]=0;k=H[f+16>>2];a=H[f+12>>2];b=k-a|0;if(b>>>0>=9){while(1){oa(H[a>>2]);a=H[f+12>>2]+4|0;H[f+12>>2]=a;k=H[f+16>>2];b=k-a|0;if(b>>>0>8){continue}break}}d=170;t:{switch((b>>>2|0)-1|0){case 1:d=341;case 0:H[f+24>>2]=d;break;default:break t}}u:{if((a|0)==(k|0)){break u}while(1){oa(H[a>>2]);a=a+4|0;if((k|0)!=(a|0)){continue}break}b=H[f+16>>2];a=H[f+12>>2];if((b|0)==(a|0)){break u}H[f+16>>2]=b+((a-b|0)+3&-4)}a=H[f+8>>2];if(a){oa(a)}ca=f+32|0;break b}sa();v()}sa();v()}g=c}return g}function ud(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0;i=H[b+8>>2];k=H[b+12>>2];o=H[b+20>>2];e=H[b+16>>2];h=e+4|0;o=h>>>0<4?o+1|0:o;a:{if(i>>>0>>0&(k|0)<=(o|0)|(k|0)<(o|0)){break a}e=e+H[b>>2]|0;H[a>>2]=I[e|0]|I[e+1|0]<<8|(I[e+2|0]<<16|I[e+3|0]<<24);e=H[b+20>>2];i=e;h=H[b+16>>2];e=h+4|0;k=e>>>0<4?i+1|0:i;H[b+16>>2]=e;H[b+20>>2]=k;if(K[a>>2]>32){break a}k=H[b+8>>2];o=H[b+12>>2];h=h+8|0;i=h>>>0<8?i+1|0:i;if(h>>>0>k>>>0&(i|0)>=(o|0)|(i|0)>(o|0)){break a}e=H[b>>2]+e|0;h=I[e|0]|I[e+1|0]<<8|(I[e+2|0]<<16|I[e+3|0]<<24);H[a+4>>2]=h;i=H[b+20>>2];e=H[b+16>>2]+4|0;i=e>>>0<4?i+1|0:i;H[b+16>>2]=e;H[b+20>>2]=i;if(!h){return 1}if(d>>>0>>0){break a}H[a+8>>2]=0;if(!sb(a+16|0,b)){break a}if(!ua(a+544|0,b)){break a}if(!ua(a+564|0,b)){break a}if(!ua(a+584|0,b)){break a}w=H[a+4>>2];d=c;b=0;c=0;f=ca-32|0;ca=f;g=a;a=H[a+12>>2];H[f+16>>2]=0;H[f+8>>2]=0;H[f+12>>2]=0;b:{c:{if(a){if(a>>>0>=1073741824){break c}e=a<<2;b=pa(e);H[f+8>>2]=b;c=b+e|0;H[f+16>>2]=c;ra(b,0,e);H[f+12>>2]=c}h=H[g+628>>2];e=H[h>>2];if(e){H[h+4>>2]=e;oa(e);c=H[f+12>>2];b=H[f+8>>2];a=H[g+12>>2]}H[h+4>>2]=c;H[h>>2]=b;H[h+8>>2]=H[f+16>>2];b=0;H[f+16>>2]=0;H[f+8>>2]=0;H[f+12>>2]=0;d:{if(a){if(a>>>0>=1073741824){break d}a=a<<2;j=pa(a);H[f+8>>2]=j;b=a+j|0;H[f+16>>2]=b;ra(j,0,a);H[f+12>>2]=b}c=H[g+640>>2];a=H[c>>2];if(a){H[c+4>>2]=a;oa(a);j=H[f+8>>2];b=H[f+12>>2]}H[c+4>>2]=b;H[c>>2]=j;H[c+8>>2]=H[f+16>>2];H[f+24>>2]=0;H[f+28>>2]=0;H[f+16>>2]=0;H[f+20>>2]=0;H[f+8>>2]=0;H[f+12>>2]=0;xa(f+8|0);b=H[f+24>>2]+H[f+28>>2]|0;a=(b>>>0)/341|0;a=H[H[f+12>>2]+(a<<2)>>2]+N(b-N(a,341)|0,12)|0;H[a+4>>2]=0;H[a+8>>2]=0;H[a>>2]=w;c=1;a=H[f+28>>2]+1|0;H[f+28>>2]=a;e:{if(!a){break e}o=g+16|0;while(1){i=H[f+12>>2];h=H[f+24>>2];e=a-1|0;c=h+e|0;b=(c>>>0)/341|0;b=H[i+(b<<2)>>2]+N(c-N(b,341)|0,12)|0;q=H[b+8>>2];n=H[b>>2];H[f+28>>2]=e;b=H[f+16>>2];if((((b|0)!=(i|0)?N(b-i>>2,341)-1|0:0)-(a+h|0)|0)+1>>>0>=682){oa(H[b-4>>2]);H[f+16>>2]=H[f+16>>2]-4}c=0;if(n>>>0>w>>>0){break e}a=H[g+628>>2];p=N(q,12);t=p+H[g+640>>2]|0;j=Vd(g,n,t);if(j>>>0>=K[g+12>>2]){break e}r=a+p|0;h=H[g>>2];l=j<<2;e=H[l+H[t>>2]>>2];f:{g:{if((h|0)==(e|0)){if(!n){break g}c=H[d+16>>2];b=H[d+20>>2];m=0;while(1){e=(b|0)==(c|0);a=b;k=0;b=c;h:{if(e){break h}while(1){l=H[d+28>>2];b=a;i=N(k,20)+c|0;h=H[i>>2];if(!I[h+84|0]){l=H[H[h+68>>2]+(l<<2)>>2]}if(K[h+80>>2]<=l>>>0){break h}e=H[r>>2]+(H[i+4>>2]<<2)|0;c=H[i+12>>2];b=e;i:{if(c>>>0>3){break i}a=0;b=H[d+12>>2];if(!H[i+16>>2]){break i}while(1){b=qa(b,e+(a<<2)|0,c);c=H[i+12>>2];b=b+c|0;a=a+1|0;if(a>>>0>2]){continue}break}b=H[d+12>>2]}a=H[h+40>>2];qa(H[H[h>>2]>>2]+N(a,l)|0,b,a);a=H[d+20>>2];b=a;k=k+1|0;c=H[d+16>>2];if(k>>>0<(a-c|0)/20>>>0){continue}break}}H[d+28>>2]=H[d+28>>2]+1;H[g+8>>2]=H[g+8>>2]+1;m=m+1|0;if((n|0)!=(m|0)){continue}break}break g}j:{k:{l:{if(n>>>0<=2){c=H[g+616>>2];H[c>>2]=j;a=1;b=H[g+12>>2];if(b>>>0>1){break l}break j}if(K[g+8>>2]>K[g+4>>2]){break e}a=H[g+628>>2];k=q+1|0;m=N(k,12);b=a+m|0;if((b|0)!=(r|0)){Aa(b,H[r>>2],H[r+4>>2]);a=H[g+628>>2]}a=l+H[a+m>>2]|0;H[a>>2]=H[a>>2]+(1<>>1|0;break k}while(1){b=Ba((a<<4)+o|0)|b<<1;a=a+1|0;if((c|0)!=(a|0)){continue}break}a=n>>>1|0;if(b>>>0<=a>>>0){break k}c=0;break e}while(1){j=(b-1|0)!=(j|0)?j+1|0:0;H[c+(a<<2)>>2]=j;a=a+1|0;b=H[g+12>>2];if(a>>>0>>0){continue}break}break j}m:{n:{b=a-b|0;a=n-b|0;o:{if((a|0)==(b|0)){a=b;break o}i=H[g+596>>2];if((i|0)==H[g+588>>2]){break n}h=H[i>>2];e=H[g+600>>2];c=e+1|0;H[g+600>>2]=c;e=h&-2147483648>>>e;p:{if((c|0)==32){H[g+600>>2]=0;H[g+596>>2]=i+4;if(e){break p}break n}if(!e){break n}}}c=a;a=b;break m}c=b}i=H[g+640>>2];h=i+p|0;e=H[h>>2];b=e+l|0;H[b>>2]=H[b>>2]+1;Aa(i+m|0,e,H[h+4>>2]);if(a){m=H[f+28>>2]+H[f+24>>2]|0;e=H[f+16>>2];b=H[f+12>>2];if((m|0)==(((b|0)!=(e|0)?N(e-b>>2,341)-1|0:0)|0)){xa(f+8|0);m=H[f+24>>2]+H[f+28>>2]|0;e=H[f+12>>2]}else{e=b}b=(m>>>0)/341|0;b=H[e+(b<<2)>>2]+N(m-N(b,341)|0,12)|0;H[b+8>>2]=q;H[b+4>>2]=j;H[b>>2]=a;H[f+28>>2]=H[f+28>>2]+1}if(!c){break g}b=H[f+28>>2]+H[f+24>>2]|0;e=H[f+16>>2];a=H[f+12>>2];if((b|0)==(((a|0)!=(e|0)?N(e-a>>2,341)-1|0:0)|0)){xa(f+8|0);b=H[f+24>>2]+H[f+28>>2]|0;e=H[f+12>>2]}else{e=a}a=(b>>>0)/341|0;a=H[e+(a<<2)>>2]+N(b-N(a,341)|0,12)|0;H[a+8>>2]=k;H[a+4>>2]=j;H[a>>2]=c;a=H[f+28>>2]+1|0;H[f+28>>2]=a;break f}j=0;if(!n){break g}while(1){if(H[g+12>>2]){q=H[g+548>>2];i=H[t>>2];u=H[g+604>>2];h=H[g+616>>2];a=0;while(1){p=(a<<2)+h|0;H[u+(H[p>>2]<<2)>>2]=0;e=H[g>>2];c=H[p>>2]<<2;b=H[c+i>>2];q:{if((e|0)==(b|0)){break q}l=c+u|0;s=e-b|0;m=H[g+560>>2];e=32-m|0;if((s|0)<=(e|0)){c=H[g+556>>2];if((c|0)==(q|0)){c=0;break e}H[l>>2]=H[c>>2]<>>32-s;b=s+H[g+560>>2]|0;H[g+560>>2]=b;if((b|0)!=32){break q}H[g+560>>2]=0;H[g+556>>2]=c+4;break q}k=H[g+556>>2];b=k+4|0;if((q|0)==(b|0)){c=0;break e}c=H[k>>2];H[g+556>>2]=b;b=s-e|0;H[g+560>>2]=b;H[l>>2]=H[k+4>>2]>>>32-b|c<>>32-s}c=H[p>>2]<<2;b=c+u|0;H[b>>2]=H[b>>2]|H[c+H[r>>2]>>2];a=a+1|0;if(a>>>0>2]){continue}break}}k=0;a=H[d+16>>2];r:{if((a|0)==H[d+20>>2]){break r}while(1){l=H[d+28>>2];i=N(k,20)+a|0;h=H[i>>2];if(!I[h+84|0]){l=H[H[h+68>>2]+(l<<2)>>2]}if(K[h+80>>2]<=l>>>0){break r}e=H[g+604>>2]+(H[i+4>>2]<<2)|0;c=H[i+12>>2];b=e;s:{if(c>>>0>3){break s}a=0;b=H[d+12>>2];if(!H[i+16>>2]){break s}while(1){b=qa(b,e+(a<<2)|0,c);c=H[i+12>>2];b=b+c|0;a=a+1|0;if(a>>>0>2]){continue}break}b=H[d+12>>2]}a=H[h+40>>2];qa(H[H[h>>2]>>2]+N(a,l)|0,b,a);k=k+1|0;a=H[d+16>>2];if(k>>>0<(H[d+20>>2]-a|0)/20>>>0){continue}break}}H[d+28>>2]=H[d+28>>2]+1;H[g+8>>2]=H[g+8>>2]+1;j=j+1|0;if((n|0)!=(j|0)){continue}break}}a=H[f+28>>2]}if(a){continue}break}c=1}H[f+28>>2]=0;j=H[f+16>>2];a=H[f+12>>2];b=j-a|0;if(b>>>0>=9){while(1){oa(H[a>>2]);a=H[f+12>>2]+4|0;H[f+12>>2]=a;j=H[f+16>>2];b=j-a|0;if(b>>>0>8){continue}break}}d=170;t:{switch((b>>>2|0)-1|0){case 1:d=341;case 0:H[f+24>>2]=d;break;default:break t}}u:{if((a|0)==(j|0)){break u}while(1){oa(H[a>>2]);a=a+4|0;if((j|0)!=(a|0)){continue}break}b=H[f+16>>2];a=H[f+12>>2];if((b|0)==(a|0)){break u}H[f+16>>2]=b+((a-b|0)+3&-4)}a=H[f+8>>2];if(a){oa(a)}ca=f+32|0;break b}sa();v()}sa();v()}g=c}return g}function vd(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0;i=H[b+8>>2];k=H[b+12>>2];m=H[b+20>>2];f=H[b+16>>2];h=f+4|0;m=h>>>0<4?m+1|0:m;a:{if(i>>>0>>0&(k|0)<=(m|0)|(k|0)<(m|0)){break a}f=f+H[b>>2]|0;H[a>>2]=I[f|0]|I[f+1|0]<<8|(I[f+2|0]<<16|I[f+3|0]<<24);f=H[b+20>>2];i=f;h=H[b+16>>2];f=h+4|0;k=f>>>0<4?i+1|0:i;H[b+16>>2]=f;H[b+20>>2]=k;if(K[a>>2]>32){break a}k=H[b+8>>2];m=H[b+12>>2];h=h+8|0;i=h>>>0<8?i+1|0:i;if(h>>>0>k>>>0&(i|0)>=(m|0)|(i|0)>(m|0)){break a}f=f+H[b>>2]|0;h=I[f|0]|I[f+1|0]<<8|(I[f+2|0]<<16|I[f+3|0]<<24);H[a+4>>2]=h;i=H[b+20>>2];f=H[b+16>>2]+4|0;i=f>>>0<4?i+1|0:i;H[b+16>>2]=f;H[b+20>>2]=i;if(!h){return 1}if(d>>>0>>0){break a}H[a+8>>2]=0;if(!sb(a+16|0,b)){break a}if(!ua(a+544|0,b)){break a}if(!ua(a+564|0,b)){break a}if(!ua(a+584|0,b)){break a}u=H[a+4>>2];b=0;e=ca-32|0;ca=e;f=a;a=H[a+12>>2];H[e+16>>2]=0;H[e+8>>2]=0;H[e+12>>2]=0;b:{c:{if(a){if(a>>>0>=1073741824){break c}d=a<<2;b=pa(d);H[e+8>>2]=b;g=b+d|0;H[e+16>>2]=g;ra(b,0,d);H[e+12>>2]=g}h=H[f+628>>2];d=H[h>>2];if(d){H[h+4>>2]=d;oa(d);g=H[e+12>>2];b=H[e+8>>2];a=H[f+12>>2]}H[h+4>>2]=g;H[h>>2]=b;H[h+8>>2]=H[e+16>>2];b=0;H[e+16>>2]=0;H[e+8>>2]=0;H[e+12>>2]=0;d:{if(a){if(a>>>0>=1073741824){break d}a=a<<2;j=pa(a);H[e+8>>2]=j;b=a+j|0;H[e+16>>2]=b;ra(j,0,a);H[e+12>>2]=b}d=H[f+640>>2];a=H[d>>2];if(a){H[d+4>>2]=a;oa(a);j=H[e+8>>2];b=H[e+12>>2]}H[d+4>>2]=b;H[d>>2]=j;H[d+8>>2]=H[e+16>>2];H[e+24>>2]=0;H[e+28>>2]=0;H[e+16>>2]=0;H[e+20>>2]=0;H[e+8>>2]=0;H[e+12>>2]=0;xa(e+8|0);b=H[e+24>>2]+H[e+28>>2]|0;a=(b>>>0)/341|0;a=H[H[e+12>>2]+(a<<2)>>2]+N(b-N(a,341)|0,12)|0;H[a+4>>2]=0;H[a+8>>2]=0;H[a>>2]=u;d=1;a=H[e+28>>2]+1|0;H[e+28>>2]=a;e:{if(!a){break e}m=f+16|0;while(1){k=H[e+12>>2];h=H[e+24>>2];g=a-1|0;d=h+g|0;b=(d>>>0)/341|0;b=H[k+(b<<2)>>2]+N(d-N(b,341)|0,12)|0;q=H[b+8>>2];i=H[b+4>>2];n=H[b>>2];H[e+28>>2]=g;b=H[e+16>>2];if((((b|0)!=(k|0)?N(b-k>>2,341)-1|0:0)-(a+h|0)|0)+1>>>0>=682){oa(H[b-4>>2]);H[e+16>>2]=H[e+16>>2]-4}if(n>>>0>u>>>0){d=0;break e}d=0;a=H[f+12>>2];j=(i|0)!=(a-1|0)?i+1|0:0;if(j>>>0>=a>>>0){break e}a=H[f+628>>2];o=N(q,12);s=a+o|0;g=H[f>>2];l=j<<2;k=o+H[f+640>>2]|0;b=H[l+H[k>>2]>>2];f:{g:{if((g|0)==(b|0)){if(!n){break g}g=H[c+16>>2];b=H[c+20>>2];p=0;while(1){d=(b|0)==(g|0);a=b;j=0;b=g;h:{if(d){break h}while(1){d=H[c+28>>2];b=a;k=N(j,20)+g|0;i=H[k>>2];if(!I[i+84|0]){d=H[H[i+68>>2]+(d<<2)>>2]}if(K[i+80>>2]<=d>>>0){break h}h=H[s>>2]+(H[k+4>>2]<<2)|0;g=H[k+12>>2];b=h;i:{if(g>>>0>3){break i}a=0;b=H[c+12>>2];if(!H[k+16>>2]){break i}while(1){b=qa(b,h+(a<<2)|0,g);g=H[k+12>>2];b=b+g|0;a=a+1|0;if(a>>>0>2]){continue}break}b=H[c+12>>2]}a=H[i+40>>2];qa(H[H[i>>2]>>2]+N(a,d)|0,b,a);a=H[c+20>>2];b=a;j=j+1|0;g=H[c+16>>2];if(j>>>0<(a-g|0)/20>>>0){continue}break}}H[c+28>>2]=H[c+28>>2]+1;H[f+8>>2]=H[f+8>>2]+1;p=p+1|0;if((p|0)!=(n|0)){continue}break}break g}j:{k:{l:{if(n>>>0<=2){d=H[f+616>>2];H[d>>2]=j;a=1;b=H[f+12>>2];if(b>>>0>1){break l}break j}if(K[f+8>>2]>K[f+4>>2]){break e}d=a;a=o+12|0;Aa(d+a|0,H[s>>2],H[s+4>>2]);a=l+H[a+H[f+628>>2]>>2]|0;H[a>>2]=H[a>>2]+(1<>>1|0;break k}while(1){b=Ba((a<<4)+m|0)|b<<1;a=a+1|0;if((d|0)!=(a|0)){continue}break}a=n>>>1|0;if(b>>>0<=a>>>0){break k}d=0;break e}while(1){j=(b-1|0)!=(j|0)?j+1|0:0;H[d+(a<<2)>>2]=j;a=a+1|0;b=H[f+12>>2];if(a>>>0>>0){continue}break}break j}k=q+1|0;m:{n:{b=a-b|0;a=n-b|0;o:{if((a|0)==(b|0)){a=b;break o}i=H[f+596>>2];if((i|0)==H[f+588>>2]){break n}h=H[i>>2];g=H[f+600>>2];d=g+1|0;H[f+600>>2]=d;g=h&-2147483648>>>g;p:{if((d|0)==32){H[f+600>>2]=0;H[f+596>>2]=i+4;if(g){break p}break n}if(!g){break n}}}d=a;a=b;break m}d=b}i=H[f+640>>2];h=i+o|0;g=H[h>>2];b=g+l|0;H[b>>2]=H[b>>2]+1;Aa(i+N(k,12)|0,g,H[h+4>>2]);if(a){h=H[e+28>>2]+H[e+24>>2]|0;g=H[e+16>>2];b=H[e+12>>2];if((h|0)==(((b|0)!=(g|0)?N(g-b>>2,341)-1|0:0)|0)){xa(e+8|0);h=H[e+24>>2]+H[e+28>>2]|0;g=H[e+12>>2]}else{g=b}b=(h>>>0)/341|0;b=H[g+(b<<2)>>2]+N(h-N(b,341)|0,12)|0;H[b+8>>2]=q;H[b+4>>2]=j;H[b>>2]=a;H[e+28>>2]=H[e+28>>2]+1}if(!d){break g}b=H[e+28>>2]+H[e+24>>2]|0;g=H[e+16>>2];a=H[e+12>>2];if((b|0)==(((a|0)!=(g|0)?N(g-a>>2,341)-1|0:0)|0)){xa(e+8|0);b=H[e+24>>2]+H[e+28>>2]|0;g=H[e+12>>2]}else{g=a}a=(b>>>0)/341|0;a=H[g+(a<<2)>>2]+N(b-N(a,341)|0,12)|0;H[a+8>>2]=k;H[a+4>>2]=j;H[a>>2]=d;a=H[e+28>>2]+1|0;H[e+28>>2]=a;break f}p=0;if(!n){break g}while(1){if(H[f+12>>2]){w=H[f+548>>2];i=H[k>>2];t=H[f+604>>2];h=H[f+616>>2];a=0;while(1){j=h+(a<<2)|0;H[(H[j>>2]<<2)+t>>2]=0;g=H[f>>2];d=H[j>>2]<<2;b=H[d+i>>2];q:{if((g|0)==(b|0)){break q}q=d+t|0;r=g-b|0;o=H[f+560>>2];g=32-o|0;if((r|0)<=(g|0)){d=H[f+556>>2];if((d|0)==(w|0)){d=0;break e}H[q>>2]=H[d>>2]<>>32-r;b=H[f+560>>2]+r|0;H[f+560>>2]=b;if((b|0)!=32){break q}H[f+560>>2]=0;H[f+556>>2]=d+4;break q}l=H[f+556>>2];b=l+4|0;if((b|0)==(w|0)){d=0;break e}d=H[l>>2];H[f+556>>2]=b;b=r-g|0;H[f+560>>2]=b;H[q>>2]=H[l+4>>2]>>>32-b|d<>>32-r}d=H[j>>2]<<2;b=d+t|0;H[b>>2]=H[b>>2]|H[d+H[s>>2]>>2];a=a+1|0;if(a>>>0>2]){continue}break}}j=0;a=H[c+16>>2];r:{if((a|0)==H[c+20>>2]){break r}while(1){d=H[c+28>>2];l=N(j,20)+a|0;i=H[l>>2];if(!I[i+84|0]){d=H[H[i+68>>2]+(d<<2)>>2]}if(K[i+80>>2]<=d>>>0){break r}h=H[f+604>>2]+(H[l+4>>2]<<2)|0;g=H[l+12>>2];b=h;s:{if(g>>>0>3){break s}a=0;b=H[c+12>>2];if(!H[l+16>>2]){break s}while(1){b=qa(b,h+(a<<2)|0,g);g=H[l+12>>2];b=b+g|0;a=a+1|0;if(a>>>0>2]){continue}break}b=H[c+12>>2]}a=H[i+40>>2];qa(H[H[i>>2]>>2]+N(a,d)|0,b,a);j=j+1|0;a=H[c+16>>2];if(j>>>0<(H[c+20>>2]-a|0)/20>>>0){continue}break}}H[c+28>>2]=H[c+28>>2]+1;H[f+8>>2]=H[f+8>>2]+1;p=p+1|0;if((p|0)!=(n|0)){continue}break}}a=H[e+28>>2]}if(a){continue}break}d=1}H[e+28>>2]=0;j=H[e+16>>2];a=H[e+12>>2];b=j-a|0;if(b>>>0>=9){while(1){oa(H[a>>2]);a=H[e+12>>2]+4|0;H[e+12>>2]=a;j=H[e+16>>2];b=j-a|0;if(b>>>0>8){continue}break}}g=170;t:{switch((b>>>2|0)-1|0){case 1:g=341;case 0:H[e+24>>2]=g;break;default:break t}}u:{if((a|0)==(j|0)){break u}while(1){oa(H[a>>2]);a=a+4|0;if((j|0)!=(a|0)){continue}break}b=H[e+16>>2];a=H[e+12>>2];if((b|0)==(a|0)){break u}H[e+16>>2]=b+((a-b|0)+3&-4)}a=H[e+8>>2];if(a){oa(a)}ca=e+32|0;break b}sa();v()}sa();v()}g=d}return g}function yd(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0;j=H[b+8>>2];e=H[b+12>>2];g=H[b+20>>2];h=H[b+16>>2];l=h+4|0;g=l>>>0<4?g+1|0:g;a:{if(j>>>0>>0&(e|0)<=(g|0)|(e|0)<(g|0)){break a}h=h+H[b>>2]|0;H[a>>2]=I[h|0]|I[h+1|0]<<8|(I[h+2|0]<<16|I[h+3|0]<<24);h=H[b+20>>2];e=h;j=H[b+16>>2];g=j+4|0;h=g>>>0<4?e+1|0:e;H[b+16>>2]=g;H[b+20>>2]=h;if(K[a>>2]>32){break a}k=H[b+8>>2];l=H[b+12>>2];h=e;e=j+8|0;h=e>>>0<8?h+1|0:h;if(e>>>0>k>>>0&(h|0)>=(l|0)|(h|0)>(l|0)){break a}h=H[b>>2]+g|0;g=I[h|0]|I[h+1|0]<<8|(I[h+2|0]<<16|I[h+3|0]<<24);H[a+4>>2]=g;h=H[b+20>>2];e=H[b+16>>2]+4|0;h=e>>>0<4?h+1|0:h;H[b+16>>2]=e;H[b+20>>2]=h;if(!g){return 1}if(d>>>0>>0){break a}H[a+8>>2]=0;if(!ta(a+16|0,b)){break a}if(!ua(a+32|0,b)){break a}if(!ua(a+52|0,b)){break a}if(!ua(a+72|0,b)){break a}r=H[a+4>>2];h=c;b=0;g=0;e=ca-32|0;ca=e;d=a;a=H[a+12>>2];H[e+16>>2]=0;H[e+8>>2]=0;H[e+12>>2]=0;b:{c:{if(a){if(a>>>0>=1073741824){break c}c=a<<2;b=pa(c);H[e+8>>2]=b;g=b+c|0;H[e+16>>2]=g;ra(b,0,c);H[e+12>>2]=g}c=H[d+116>>2];i=H[c>>2];if(i){H[c+4>>2]=i;oa(i);g=H[e+12>>2];b=H[e+8>>2];a=H[d+12>>2]}H[c+4>>2]=g;H[c>>2]=b;H[c+8>>2]=H[e+16>>2];b=0;H[e+16>>2]=0;H[e+8>>2]=0;H[e+12>>2]=0;d:{if(a){if(a>>>0>=1073741824){break d}a=a<<2;f=pa(a);H[e+8>>2]=f;b=a+f|0;H[e+16>>2]=b;ra(f,0,a);H[e+12>>2]=b}a=H[d+128>>2];c=H[a>>2];if(c){H[a+4>>2]=c;oa(c);f=H[e+8>>2];b=H[e+12>>2]}H[a+4>>2]=b;H[a>>2]=f;H[a+8>>2]=H[e+16>>2];H[e+24>>2]=0;H[e+28>>2]=0;H[e+16>>2]=0;H[e+20>>2]=0;H[e+8>>2]=0;H[e+12>>2]=0;xa(e+8|0);a=H[e+24>>2]+H[e+28>>2]|0;b=(a>>>0)/341|0;a=H[H[e+12>>2]+(b<<2)>>2]+N(a-N(b,341)|0,12)|0;H[a+4>>2]=0;H[a+8>>2]=0;H[a>>2]=r;c=1;a=H[e+28>>2]+1|0;H[e+28>>2]=a;e:{if(!a){break e}t=d+16|0;while(1){b=H[e+12>>2];f=H[e+24>>2];l=a-1|0;c=f+l|0;i=(c>>>0)/341|0;c=H[b+(i<<2)>>2]+N(c-N(i,341)|0,12)|0;g=H[c+8>>2];i=H[c+4>>2];j=H[c>>2];H[e+28>>2]=l;c=H[e+16>>2];if((((b|0)!=(c|0)?N(c-b>>2,341)-1|0:0)-(a+f|0)|0)+1>>>0>=682){oa(H[c-4>>2]);H[e+16>>2]=H[e+16>>2]-4}c=0;if(j>>>0>r>>>0){break e}b=H[d+12>>2];a=(b-1|0)!=(i|0)?i+1|0:0;if(a>>>0>=b>>>0){break e}f=N(g,12);o=f+H[d+128>>2]|0;l=f+H[d+116>>2]|0;i=H[d>>2];k=a<<2;n=H[k+H[o>>2]>>2];f:{if((i|0)==(n|0)){if(!j){break f}o=0;b=H[h+20>>2];g=H[h+16>>2];if((b|0)==(g|0)){a=H[d+8>>2];H[h+28>>2]=j+H[h+28>>2];H[d+8>>2]=a+j;break f}while(1){c=(b|0)==(g|0);a=b;i=0;b=g;g:{if(c){break g}while(1){f=H[h+28>>2];b=a;c=N(i,20)+g|0;k=H[c>>2];if(!I[k+84|0]){f=H[H[k+68>>2]+(f<<2)>>2]}if(K[k+80>>2]<=f>>>0){break g}n=H[l>>2]+(H[c+4>>2]<<2)|0;g=H[c+12>>2];b=n;h:{if(g>>>0>3){break h}a=0;b=H[h+12>>2];if(!H[c+16>>2]){break h}while(1){b=qa(b,n+(a<<2)|0,g);g=H[c+12>>2];b=b+g|0;a=a+1|0;if(a>>>0>2]){continue}break}b=H[h+12>>2]}a=H[k+40>>2];qa(H[H[k>>2]>>2]+N(a,f)|0,b,a);i=i+1|0;a=H[h+20>>2];b=a;g=H[h+16>>2];if(i>>>0<(b-g|0)/20>>>0){continue}break}}H[h+28>>2]=H[h+28>>2]+1;H[d+8>>2]=H[d+8>>2]+1;o=o+1|0;if((j|0)!=(o|0)){continue}break}break f}i:{j:{k:{l:{if(j>>>0<=2){c=H[d+104>>2];H[c>>2]=a;f=1;b=H[d+12>>2];if(b>>>0>1){break l}break i}if(K[d+8>>2]>K[d+4>>2]){break e}b=H[d+116>>2];m=g+1|0;o=N(m,12);q=b+o|0;if((q|0)!=(l|0)){Aa(q,H[l>>2],H[l+4>>2]);b=H[d+116>>2]}b=k+H[b+o>>2]|0;H[b>>2]=H[b>>2]+(1<>2]=0;pc(t,Q(j)^31,e+4|0);b=j>>>1|0;i=H[e+4>>2];if(b>>>0>>0){break e}b=b-i|0;c=j-b|0;m:{if((c|0)==(b|0)){c=b;break m}i=H[d+84>>2];if((i|0)==H[d+76>>2]){break k}j=H[i>>2];l=H[d+88>>2];n=l+1|0;H[d+88>>2]=n;j=j&-2147483648>>>l;n:{if((n|0)==32){H[d+88>>2]=0;H[d+84>>2]=i+4;if(j){break n}break k}if(!j){break k}}}i=c;c=b;break j}while(1){a=(b-1|0)!=(a|0)?a+1|0:0;H[c+(f<<2)>>2]=a;b=H[d+12>>2];f=f+1|0;if(b>>>0>f>>>0){continue}break}break i}i=b}b=H[d+128>>2];j=b+f|0;f=H[j>>2];l=f+k|0;H[l>>2]=H[l>>2]+1;Aa(b+o|0,f,H[j+4>>2]);if(c){b=H[e+28>>2]+H[e+24>>2]|0;j=H[e+16>>2];f=H[e+12>>2];if((b|0)==(((f|0)!=(j|0)?N(j-f>>2,341)-1|0:0)|0)){xa(e+8|0);f=H[e+12>>2];b=H[e+24>>2]+H[e+28>>2]|0}j=(b>>>0)/341|0;b=H[(j<<2)+f>>2]+N(b-N(j,341)|0,12)|0;H[b+8>>2]=g;H[b+4>>2]=a;H[b>>2]=c;H[e+28>>2]=H[e+28>>2]+1}if(!i){break f}b=H[e+28>>2]+H[e+24>>2]|0;c=H[e+16>>2];f=H[e+12>>2];if((b|0)==(((c|0)!=(f|0)?N(c-f>>2,341)-1|0:0)|0)){xa(e+8|0);f=H[e+12>>2];b=H[e+24>>2]+H[e+28>>2]|0}c=(b>>>0)/341|0;b=H[(c<<2)+f>>2]+N(b-N(c,341)|0,12)|0;H[b+8>>2]=m;H[b+4>>2]=a;H[b>>2]=i;H[e+28>>2]=H[e+28>>2]+1;break f}n=0;if(!j){break f}while(1){if(H[d+12>>2]){i=H[d+36>>2];q=H[o>>2];c=H[d+92>>2];u=H[d+104>>2];a=0;while(1){g=(a<<2)+u|0;H[c+(H[g>>2]<<2)>>2]=0;b=H[d>>2];f=H[g>>2]<<2;k=H[f+q>>2];o:{if((b|0)==(k|0)){break o}f=c+f|0;b=b-k|0;k=H[d+48>>2];p=32-k|0;if((b|0)<=(p|0)){m=H[d+44>>2];if((m|0)==(i|0)){c=0;break e}H[f>>2]=H[m>>2]<>>32-b;b=b+H[d+48>>2]|0;H[d+48>>2]=b;if((b|0)!=32){break o}H[d+48>>2]=0;H[d+44>>2]=m+4;break o}m=H[d+44>>2];s=m+4|0;if((i|0)==(s|0)){c=0;break e}w=H[m>>2];H[d+44>>2]=s;p=b-p|0;H[d+48>>2]=p;H[f>>2]=H[m+4>>2]>>>32-p|w<>>32-b}b=H[g>>2]<<2;g=b+c|0;H[g>>2]=H[g>>2]|H[b+H[l>>2]>>2];a=a+1|0;if(a>>>0>2]){continue}break}}i=0;a=H[h+16>>2];p:{if((a|0)==H[h+20>>2]){break p}while(1){f=H[h+28>>2];c=N(i,20)+a|0;k=H[c>>2];if(!I[k+84|0]){f=H[H[k+68>>2]+(f<<2)>>2]}if(K[k+80>>2]<=f>>>0){break p}m=H[d+92>>2]+(H[c+4>>2]<<2)|0;g=H[c+12>>2];b=m;q:{if(g>>>0>3){break q}a=0;b=H[h+12>>2];if(!H[c+16>>2]){break q}while(1){b=qa(b,m+(a<<2)|0,g);g=H[c+12>>2];b=b+g|0;a=a+1|0;if(a>>>0>2]){continue}break}b=H[h+12>>2]}a=H[k+40>>2];qa(H[H[k>>2]>>2]+N(a,f)|0,b,a);i=i+1|0;a=H[h+16>>2];if(i>>>0<(H[h+20>>2]-a|0)/20>>>0){continue}break}}H[h+28>>2]=H[h+28>>2]+1;H[d+8>>2]=H[d+8>>2]+1;n=n+1|0;if((j|0)!=(n|0)){continue}break}}a=H[e+28>>2];if(a){continue}break}c=1}H[e+28>>2]=0;f=H[e+16>>2];a=H[e+12>>2];b=f-a|0;if(b>>>0>=9){while(1){oa(H[a>>2]);a=H[e+12>>2]+4|0;H[e+12>>2]=a;f=H[e+16>>2];b=f-a|0;if(b>>>0>8){continue}break}}g=170;r:{switch((b>>>2|0)-1|0){case 1:g=341;case 0:H[e+24>>2]=g;break;default:break r}}s:{if((a|0)==(f|0)){break s}while(1){oa(H[a>>2]);a=a+4|0;if((f|0)!=(a|0)){continue}break}a=H[e+16>>2];b=H[e+12>>2];if((a|0)==(b|0)){break s}H[e+16>>2]=a+((b-a|0)+3&-4)}a=H[e+8>>2];if(a){oa(a)}ca=e+32|0;break b}sa();v()}sa();v()}i=c}return i}function xd(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0;i=H[b+8>>2];k=H[b+12>>2];n=H[b+20>>2];h=H[b+16>>2];f=h+4|0;n=f>>>0<4?n+1|0:n;a:{if((k|0)<=(n|0)&f>>>0>i>>>0|(k|0)<(n|0)){break a}h=h+H[b>>2]|0;H[a>>2]=I[h|0]|I[h+1|0]<<8|(I[h+2|0]<<16|I[h+3|0]<<24);h=H[b+20>>2];i=h;f=H[b+16>>2];h=f+4|0;k=h>>>0<4?i+1|0:i;H[b+16>>2]=h;H[b+20>>2]=k;if(K[a>>2]>32){break a}k=H[b+8>>2];n=H[b+12>>2];f=f+8|0;i=f>>>0<8?i+1|0:i;if(f>>>0>k>>>0&(i|0)>=(n|0)|(i|0)>(n|0)){break a}h=H[b>>2]+h|0;f=I[h|0]|I[h+1|0]<<8|(I[h+2|0]<<16|I[h+3|0]<<24);H[a+4>>2]=f;i=H[b+20>>2];h=H[b+16>>2]+4|0;i=h>>>0<4?i+1|0:i;H[b+16>>2]=h;H[b+20>>2]=i;if(!f){return 1}if(d>>>0>>0){break a}H[a+8>>2]=0;if(!ta(a+16|0,b)){break a}if(!ua(a+32|0,b)){break a}if(!ua(a+52|0,b)){break a}if(!ua(a+72|0,b)){break a}u=H[a+4>>2];h=c;b=0;c=0;e=ca-32|0;ca=e;f=a;a=H[a+12>>2];H[e+16>>2]=0;H[e+8>>2]=0;H[e+12>>2]=0;b:{c:{if(a){if(a>>>0>=1073741824){break c}d=a<<2;b=pa(d);H[e+8>>2]=b;c=b+d|0;H[e+16>>2]=c;ra(b,0,d);H[e+12>>2]=c}j=H[f+116>>2];d=H[j>>2];if(d){H[j+4>>2]=d;oa(d);c=H[e+12>>2];b=H[e+8>>2];a=H[f+12>>2]}H[j+4>>2]=c;H[j>>2]=b;H[j+8>>2]=H[e+16>>2];b=0;H[e+16>>2]=0;H[e+8>>2]=0;H[e+12>>2]=0;d:{if(a){if(a>>>0>=1073741824){break d}a=a<<2;g=pa(a);H[e+8>>2]=g;b=a+g|0;H[e+16>>2]=b;ra(g,0,a);H[e+12>>2]=b}c=H[f+128>>2];a=H[c>>2];if(a){H[c+4>>2]=a;oa(a);g=H[e+8>>2];b=H[e+12>>2]}H[c+4>>2]=b;H[c>>2]=g;H[c+8>>2]=H[e+16>>2];H[e+24>>2]=0;H[e+28>>2]=0;H[e+16>>2]=0;H[e+20>>2]=0;H[e+8>>2]=0;H[e+12>>2]=0;xa(e+8|0);b=H[e+24>>2]+H[e+28>>2]|0;a=(b>>>0)/341|0;a=H[H[e+12>>2]+(a<<2)>>2]+N(b-N(a,341)|0,12)|0;H[a+4>>2]=0;H[a+8>>2]=0;H[a>>2]=u;d=1;a=H[e+28>>2]+1|0;H[e+28>>2]=a;e:{if(!a){break e}n=f+16|0;while(1){i=H[e+12>>2];j=H[e+24>>2];d=a-1|0;c=j+d|0;b=(c>>>0)/341|0;b=H[i+(b<<2)>>2]+N(c-N(b,341)|0,12)|0;o=H[b+8>>2];c=H[b+4>>2];m=H[b>>2];H[e+28>>2]=d;b=H[e+16>>2];if((((b|0)!=(i|0)?N(b-i>>2,341)-1|0:0)-(a+j|0)|0)+1>>>0>=682){oa(H[b-4>>2]);H[e+16>>2]=H[e+16>>2]-4}if(m>>>0>u>>>0){d=0;break e}d=0;b=H[f+12>>2];a=(c|0)!=(b-1|0)?c+1|0:0;if(a>>>0>=b>>>0){break e}b=H[f+116>>2];p=N(o,12);r=b+p|0;j=H[f>>2];g=a<<2;k=p+H[f+128>>2]|0;c=H[g+H[k>>2]>>2];f:{if((j|0)==(c|0)){if(!m){break f}b=H[h+20>>2];c=H[h+16>>2];if((b|0)==(c|0)){a=H[f+8>>2];H[h+28>>2]=m+H[h+28>>2];H[f+8>>2]=a+m;break f}while(1){i=(b|0)==(c|0);a=b;j=0;b=c;g:{if(i){break g}while(1){g=H[h+28>>2];b=a;l=N(j,20)+c|0;k=H[l>>2];if(!I[k+84|0]){g=H[H[k+68>>2]+(g<<2)>>2]}if(K[k+80>>2]<=g>>>0){break g}i=H[r>>2]+(H[l+4>>2]<<2)|0;c=H[l+12>>2];b=i;h:{if(c>>>0>3){break h}a=0;b=H[h+12>>2];if(!H[l+16>>2]){break h}while(1){b=qa(b,i+(a<<2)|0,c);c=H[l+12>>2];b=b+c|0;a=a+1|0;if(a>>>0>2]){continue}break}b=H[h+12>>2]}a=H[k+40>>2];qa(H[H[k>>2]>>2]+N(a,g)|0,b,a);j=j+1|0;a=H[h+20>>2];b=a;c=H[h+16>>2];if(j>>>0<(b-c|0)/20>>>0){continue}break}}H[h+28>>2]=H[h+28>>2]+1;H[f+8>>2]=H[f+8>>2]+1;d=d+1|0;if((m|0)!=(d|0)){continue}break}break f}i:{j:{k:{l:{if(m>>>0<=2){c=H[f+104>>2];H[c>>2]=a;g=1;b=H[f+12>>2];if(b>>>0>1){break l}break i}if(K[f+8>>2]>K[f+4>>2]){break e}i=b;b=p+12|0;Aa(i+b|0,H[r>>2],H[r+4>>2]);b=g+H[b+H[f+116>>2]>>2]|0;H[b>>2]=H[b>>2]+(1<>2]=0;pc(n,Q(m)^31,e+4|0);c=m>>>1|0;b=H[e+4>>2];if(c>>>0>>0){break e}l=o+1|0;b=c-b|0;c=m-b|0;m:{if((c|0)==(b|0)){c=b;break m}k=H[f+84>>2];if((k|0)==H[f+76>>2]){break k}i=H[k>>2];j=H[f+88>>2];d=j+1|0;H[f+88>>2]=d;j=i&-2147483648>>>j;n:{if((d|0)==32){H[f+88>>2]=0;H[f+84>>2]=k+4;if(j){break n}break k}if(!j){break k}}}j=c;c=b;break j}while(1){a=(b-1|0)!=(a|0)?a+1|0:0;H[c+(g<<2)>>2]=a;b=H[f+12>>2];g=g+1|0;if(b>>>0>g>>>0){continue}break}break i}j=b}k=H[f+128>>2];i=k+p|0;d=H[i>>2];b=d+g|0;H[b>>2]=H[b>>2]+1;Aa(k+N(l,12)|0,d,H[i+4>>2]);if(c){b=H[e+28>>2]+H[e+24>>2]|0;d=H[e+16>>2];g=H[e+12>>2];if((b|0)==(((d|0)!=(g|0)?N(d-g>>2,341)-1|0:0)|0)){xa(e+8|0);g=H[e+12>>2];b=H[e+24>>2]+H[e+28>>2]|0}d=(b>>>0)/341|0;b=H[(d<<2)+g>>2]+N(b-N(d,341)|0,12)|0;H[b+8>>2]=o;H[b+4>>2]=a;H[b>>2]=c;H[e+28>>2]=H[e+28>>2]+1}if(!j){break f}b=H[e+28>>2]+H[e+24>>2]|0;c=H[e+16>>2];g=H[e+12>>2];if((b|0)==(((c|0)!=(g|0)?N(c-g>>2,341)-1|0:0)|0)){xa(e+8|0);g=H[e+12>>2];b=H[e+24>>2]+H[e+28>>2]|0}c=(b>>>0)/341|0;b=H[(c<<2)+g>>2]+N(b-N(c,341)|0,12)|0;H[b+8>>2]=l;H[b+4>>2]=a;H[b>>2]=j;H[e+28>>2]=H[e+28>>2]+1;break f}s=0;if(!m){break f}while(1){if(H[f+12>>2]){w=H[f+36>>2];i=H[k>>2];t=H[f+92>>2];j=H[f+104>>2];a=0;while(1){o=(a<<2)+j|0;H[t+(H[o>>2]<<2)>>2]=0;d=H[f>>2];c=H[o>>2]<<2;b=H[c+i>>2];o:{if((d|0)==(b|0)){break o}p=c+t|0;q=d-b|0;g=H[f+48>>2];d=32-g|0;if((q|0)<=(d|0)){c=H[f+44>>2];if((c|0)==(w|0)){d=0;break e}H[p>>2]=H[c>>2]<>>32-q;b=q+H[f+48>>2]|0;H[f+48>>2]=b;if((b|0)!=32){break o}H[f+48>>2]=0;H[f+44>>2]=c+4;break o}l=H[f+44>>2];b=l+4|0;if((w|0)==(b|0)){d=0;break e}c=H[l>>2];H[f+44>>2]=b;b=q-d|0;H[f+48>>2]=b;H[p>>2]=H[l+4>>2]>>>32-b|c<>>32-q}c=H[o>>2]<<2;b=c+t|0;H[b>>2]=H[b>>2]|H[c+H[r>>2]>>2];a=a+1|0;if(a>>>0>2]){continue}break}}j=0;a=H[h+16>>2];p:{if((a|0)==H[h+20>>2]){break p}while(1){g=H[h+28>>2];l=N(j,20)+a|0;i=H[l>>2];if(!I[i+84|0]){g=H[H[i+68>>2]+(g<<2)>>2]}if(K[i+80>>2]<=g>>>0){break p}d=H[f+92>>2]+(H[l+4>>2]<<2)|0;c=H[l+12>>2];b=d;q:{if(c>>>0>3){break q}a=0;b=H[h+12>>2];if(!H[l+16>>2]){break q}while(1){b=qa(b,d+(a<<2)|0,c);c=H[l+12>>2];b=b+c|0;a=a+1|0;if(a>>>0>2]){continue}break}b=H[h+12>>2]}a=H[i+40>>2];qa(H[H[i>>2]>>2]+N(a,g)|0,b,a);j=j+1|0;a=H[h+16>>2];if(j>>>0<(H[h+20>>2]-a|0)/20>>>0){continue}break}}H[h+28>>2]=H[h+28>>2]+1;H[f+8>>2]=H[f+8>>2]+1;s=s+1|0;if((m|0)!=(s|0)){continue}break}}a=H[e+28>>2];if(a){continue}break}d=1}H[e+28>>2]=0;g=H[e+16>>2];a=H[e+12>>2];b=g-a|0;if(b>>>0>=9){while(1){oa(H[a>>2]);a=H[e+12>>2]+4|0;H[e+12>>2]=a;g=H[e+16>>2];b=g-a|0;if(b>>>0>8){continue}break}}c=170;r:{switch((b>>>2|0)-1|0){case 1:c=341;case 0:H[e+24>>2]=c;break;default:break r}}s:{if((a|0)==(g|0)){break s}while(1){oa(H[a>>2]);a=a+4|0;if((g|0)!=(a|0)){continue}break}b=H[e+16>>2];a=H[e+12>>2];if((b|0)==(a|0)){break s}H[e+16>>2]=b+((a-b|0)+3&-4)}a=H[e+8>>2];if(a){oa(a)}ca=e+32|0;j=d;break b}sa();v()}sa();v()}}return j}function $c(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0;h=ca-32|0;ca=h;g=H[H[a+4>>2]+44>>2];c=H[a+8>>2];d=H[c>>2];c=H[c+4>>2];H[h+24>>2]=0;H[h+16>>2]=0;H[h+20>>2]=0;d=(c-d>>2>>>0)/3|0;c=H[g+96>>2];f=(H[g+100>>2]-c|0)/12|0;a:{if(d>>>0>f>>>0){e=d-f|0;i=H[g+104>>2];c=H[g+100>>2];if(e>>>0<=(i-c|0)/12>>>0){b:{if(!e){break b}d=c;f=N(e,12)-12|0;i=((f>>>0)/12|0)+1&3;if(i){while(1){l=H[h+20>>2];H[d>>2]=H[h+16>>2];H[d+4>>2]=l;H[d+8>>2]=H[h+24>>2];d=d+12|0;j=j+1|0;if((i|0)!=(j|0)){continue}break}}c=N(e,12)+c|0;if(f>>>0<36){break b}while(1){f=H[h+20>>2];H[d>>2]=H[h+16>>2];H[d+4>>2]=f;H[d+8>>2]=H[h+24>>2];H[d+20>>2]=H[h+24>>2];f=H[h+20>>2];H[d+12>>2]=H[h+16>>2];H[d+16>>2]=f;H[d+32>>2]=H[h+24>>2];f=H[h+20>>2];H[d+24>>2]=H[h+16>>2];H[d+28>>2]=f;f=H[h+20>>2];H[d+36>>2]=H[h+16>>2];H[d+40>>2]=f;H[d+44>>2]=H[h+24>>2];d=d+48|0;if((d|0)!=(c|0)){continue}break}}H[g+100>>2]=c;break a}c:{f=H[g+96>>2];n=(c-f|0)/12|0;d=n+e|0;if(d>>>0<357913942){f=(i-f|0)/12|0;i=f<<1;i=f>>>0>=178956970?357913941:d>>>0>>0?i:d;if(i){if(i>>>0>=357913942){break c}l=pa(N(i,12))}f=N(n,12)+l|0;d=f;e=N(e,12);n=e-12|0;q=((n>>>0)/12|0)+1&3;if(q){while(1){r=H[h+20>>2];H[d>>2]=H[h+16>>2];H[d+4>>2]=r;H[d+8>>2]=H[h+24>>2];d=d+12|0;j=j+1|0;if((q|0)!=(j|0)){continue}break}}e=e+f|0;if(n>>>0>=36){while(1){j=H[h+20>>2];H[d>>2]=H[h+16>>2];H[d+4>>2]=j;H[d+8>>2]=H[h+24>>2];H[d+20>>2]=H[h+24>>2];j=H[h+20>>2];H[d+12>>2]=H[h+16>>2];H[d+16>>2]=j;H[d+32>>2]=H[h+24>>2];j=H[h+20>>2];H[d+24>>2]=H[h+16>>2];H[d+28>>2]=j;j=H[h+20>>2];H[d+36>>2]=H[h+16>>2];H[d+40>>2]=j;H[d+44>>2]=H[h+24>>2];d=d+48|0;if((e|0)!=(d|0)){continue}break}}j=H[g+96>>2];if((j|0)!=(c|0)){while(1){c=c-12|0;n=H[c+4>>2];f=f-12|0;d=f;H[d>>2]=H[c>>2];H[d+4>>2]=n;H[d+8>>2]=H[c+8>>2];if((c|0)!=(j|0)){continue}break}c=H[g+96>>2]}H[g+104>>2]=N(i,12)+l;H[g+100>>2]=e;H[g+96>>2]=f;if(c){oa(c)}break a}sa();v()}wa();v()}if(d>>>0>=f>>>0){break a}H[g+100>>2]=c+N(d,12)}d:{if(H[a+216>>2]==H[a+220>>2]){j=H[a+4>>2];c=H[j+44>>2];d=H[c+100>>2];f=H[c+96>>2];if((d|0)!=(f|0)){c=(d-f|0)/12|0;o=c>>>0<=1?1:c;c=0;while(1){d=H[a+8>>2];i=f+N(c,12)|0;g=N(c,3);e:{f:{if((g|0)==-1){e=H[(H[d>>2]+(g<<2)|0)+4>>2];k=-1;g=1;break f}e=-1;k=H[H[d>>2]+(g<<2)>>2];l=g+1|0;if((l|0)==-1){g=0;break f}e=H[H[d>>2]+(l<<2)>>2];g=g+2|0;m=-1;if((g|0)==-1){break e}}m=H[H[d>>2]+(g<<2)>>2]}H[i+8>>2]=m;H[i+4>>2]=e;H[i>>2]=k;c=c+1|0;if((o|0)!=(c|0)){continue}break}}H[H[j+4>>2]+80>>2]=b;c=1;break d}d=0;H[h+24>>2]=0;H[h+16>>2]=0;H[h+20>>2]=0;l=H[a+8>>2];c=H[l>>2];g=H[l+4>>2];H[h+8>>2]=0;H[h>>2]=0;H[h+4>>2]=0;b=0;g:{h:{i:{j:{k:{l:{if((c|0)!=(g|0)){c=g-c|0;if((c|0)<0){break l}b=pa(c);H[h>>2]=b;H[h+8>>2]=(c&-4)+b;u=h,w=ra(b,0,c)+c|0,H[u+4>>2]=w}c=H[l+24>>2];if((H[l+28>>2]-c|0)<4){break h}f=0;while(1){g=H[(p<<2)+c>>2];m:{if((g|0)==-1){break m}n:{if(H[H[a+120>>2]+(p>>>3&536870908)>>2]>>>p&1){break n}n=H[a+216>>2];c=H[a+220>>2];if((n|0)==(c|0)){break n}e=g+2|0;i=(g>>>0)%3|0;q=i?g-1|0:e;c=(c-n|0)/144|0;r=c>>>0<=1?1:c;j=0;t=(i|0)!=0|(e|0)!=-1;while(1){s=g<<2;i=N(j,144)+n|0;c=H[s+H[H[i+68>>2]>>2]>>2];o:{if(!(H[H[i+16>>2]+(c>>>3&536870908)>>2]>>>c&1)){break o}c=-1;p:{if(!t){break p}e=H[H[l+12>>2]+(q<<2)>>2];c=-1;if((e|0)==-1){break p}c=e-1|0;if((e>>>0)%3|0){break p}c=e+2|0}if((g|0)==(c|0)){break o}e=s;s=H[i+32>>2];i=H[e+s>>2];while(1){e=0;if((c|0)==-1){break g}if((i|0)!=H[s+(c<<2)>>2]){g=c;break n}q:{r:{if((c>>>0)%3|0){e=c-1|0;break r}e=c+2|0;m=-1;if((e|0)==-1){break q}}c=H[H[l+12>>2]+(e<<2)>>2];m=-1;if((c|0)==-1){break q}m=c-1|0;if((c>>>0)%3|0){break q}m=c+2|0}c=m;if((g|0)!=(c|0)){continue}break}}j=j+1|0;if((r|0)!=(j|0)){continue}break}}i=k-f|0;e=i>>2;H[(g<<2)+b>>2]=e;s:{if(k>>>0>>0){H[k>>2]=g;k=k+4|0;H[h+20>>2]=k;break s}c=e+1|0;if(c>>>0>=1073741824){break k}d=o-f|0;k=d>>>1|0;c=d>>>0>=2147483644?1073741823:c>>>0>>0?k:c;if(c){if(c>>>0>=1073741824){break j}d=pa(c<<2)}else{d=0}e=d+(e<<2)|0;H[e>>2]=g;m=c<<2;c=va(d,f,i);o=m+c|0;H[h+24>>2]=o;k=e+4|0;H[h+20>>2]=k;H[h+16>>2]=c;if(f){oa(f);l=H[a+8>>2]}f=c}if((g|0)==-1){break m}t:{if((g>>>0)%3|0){c=g-1|0;break t}c=g+2|0;if((c|0)==-1){break m}}c=H[H[l+12>>2]+(c<<2)>>2];if((c|0)==-1){break m}c=c+((c>>>0)%3|0?-1:2)|0;if((c|0)==-1){break m}e=g;if((c|0)==(g|0)){break m}while(1){i=c;u:{v:{c=H[a+220>>2];j=H[a+216>>2];if((c|0)==(j|0)){break v}c=(c-j|0)/144|0;n=c>>>0<=1?1:c;c=0;while(1){q=H[(j+N(c,144)|0)+32>>2];r=i<<2;if(H[q+r>>2]==H[q+(e<<2)>>2]){c=c+1|0;if((n|0)!=(c|0)){continue}break v}break}j=k-d|0;e=j>>2;H[b+r>>2]=e;if(k>>>0>>0){H[k>>2]=i;k=k+4|0;H[h+20>>2]=k;f=d;break u}c=e+1|0;if(c>>>0>=1073741824){break i}f=o-d|0;k=f>>>1|0;c=f>>>0>=2147483644?1073741823:c>>>0>>0?k:c;if(c){if(c>>>0>=1073741824){break j}f=pa(c<<2)}else{f=0}e=f+(e<<2)|0;H[e>>2]=i;m=c<<2;c=va(f,d,j);o=m+c|0;H[h+24>>2]=o;k=e+4|0;H[h+20>>2]=k;H[h+16>>2]=c;if(!d){d=c;break u}oa(d);l=H[a+8>>2];d=c;break u}H[(i<<2)+b>>2]=H[(e<<2)+b>>2]}if((i|0)==-1){break m}w:{if((i>>>0)%3|0){c=i-1|0;break w}c=i+2|0;if((c|0)==-1){break m}}c=H[H[l+12>>2]+(c<<2)>>2];if((c|0)==-1){break m}c=c+((c>>>0)%3|0?-1:2)|0;if((c|0)==-1){break m}e=i;if((c|0)!=(g|0)){continue}break}}p=p+1|0;c=H[l+24>>2];if((p|0)>2]-c>>2){continue}break}break h}sa();v()}sa();v()}wa();v()}sa();v()}i=H[a+4>>2];a=H[i+44>>2];c=H[a+100>>2];a=H[a+96>>2];x:{if((c|0)==(a|0)){break x}g=(c-a|0)/12|0;f=g>>>0<=1?1:g;l=f&1;c=0;if(g>>>0>=2){j=f&-2;g=0;while(1){e=N(c,12);f=e+b|0;o=H[f>>2];p=H[f+4>>2];e=a+e|0;H[e+8>>2]=H[f+8>>2];H[e>>2]=o;H[e+4>>2]=p;e=N(c|1,12);f=e+b|0;o=H[f>>2];p=H[f+4>>2];e=a+e|0;H[e+8>>2]=H[f+8>>2];H[e>>2]=o;H[e+4>>2]=p;c=c+2|0;g=g+2|0;if((j|0)!=(g|0)){continue}break}}if(!l){break x}g=N(c,12);c=g+b|0;f=H[c>>2];e=H[c+4>>2];a=a+g|0;H[a+8>>2]=H[c+8>>2];H[a>>2]=f;H[a+4>>2]=e}H[H[i+4>>2]+80>>2]=k-d>>2;e=1}c=e;if(b){oa(b)}if(!d){break d}H[h+20>>2]=d;oa(d)}ca=h+32|0;return c}function Fj(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;var g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0,x=0,y=0,z=0,A=0,B=0,C=0,D=0,E=0,F=0,G=0,I=0,J=0,K=0,L=0,M=0,O=0,P=0;g=ca+-64|0;ca=g;H[a+8>>2]=e;y=a+32|0;f=H[y>>2];d=H[a+36>>2]-f>>2;a:{b:{if(d>>>0>>0){ya(y,e-d|0);H[g+56>>2]=0;H[g+60>>2]=0;H[g+48>>2]=0;H[g+52>>2]=0;H[g+40>>2]=0;H[g+44>>2]=0;H[g+32>>2]=0;H[g+36>>2]=0;H[g+24>>2]=0;H[g+28>>2]=0;H[g+16>>2]=0;H[g+20>>2]=0;H[g>>2]=0;break b}if(d>>>0>e>>>0){H[a+36>>2]=f+(e<<2)}H[g+56>>2]=0;H[g+60>>2]=0;H[g+48>>2]=0;H[g+52>>2]=0;H[g+40>>2]=0;H[g+44>>2]=0;H[g+32>>2]=0;H[g+36>>2]=0;H[g+24>>2]=0;H[g+28>>2]=0;H[g+16>>2]=0;H[g+20>>2]=0;H[g>>2]=0;d=0;if(!e){break a}}Pa(g+16|0,e,g);h=H[g+28>>2];d=H[g+32>>2]}H[g>>2]=0;d=d-h>>2;c:{if(d>>>0>=e>>>0){if(d>>>0<=e>>>0){break c}H[g+32>>2]=(e<<2)+h;break c}Pa(g+16|12,e-d|0,g)}H[g>>2]=0;f=H[g+40>>2];d=H[g+44>>2]-f>>2;d:{if(d>>>0>=e>>>0){if(d>>>0<=e>>>0){break d}H[g+44>>2]=f+(e<<2);break d}Pa(g+40|0,e-d|0,g)}H[g>>2]=0;f=H[g+52>>2];d=H[g+56>>2]-f>>2;e:{if(d>>>0>=e>>>0){if(d>>>0<=e>>>0){break e}H[g+56>>2]=f+(e<<2);break e}Pa(g+52|0,e-d|0,g)}f:{if(H[a+8>>2]<=0){break f}i=H[g+16>>2];j=H[a+32>>2];h=0;while(1){d=h<<2;f=H[d+i>>2];m=H[a+16>>2];g:{if((f|0)>(m|0)){H[d+j>>2]=m;break g}d=d+j|0;m=H[a+12>>2];if((m|0)>(f|0)){H[d>>2]=m;break g}H[d>>2]=f}h=h+1|0;d=H[a+8>>2];if((h|0)<(d|0)){continue}break}if((d|0)<=0){break f}d=0;while(1){i=d<<2;f=i+c|0;i=H[b+i>>2]+H[j+i>>2]|0;H[f>>2]=i;h:{if((i|0)>H[a+16>>2]){i=i-H[a+20>>2]|0}else{if((i|0)>=H[a+12>>2]){break h}i=i+H[a+20>>2]|0}H[f>>2]=i}d=d+1|0;if((d|0)>2]){continue}break}}G=H[a+52>>2];t=H[a+48>>2];z=pa(16);d=z;H[d>>2]=0;H[d+4>>2]=0;H[d+8>>2]=0;H[d+12>>2]=0;H[g+8>>2]=0;H[g>>2]=0;H[g+4>>2]=0;i:{if(e){if(e>>>0>=1073741824){break i}d=e<<2;r=pa(d);H[g>>2]=r;H[g+8>>2]=d+r;ra(r,0,d)}A=1;d=H[a+56>>2];B=H[d>>2];d=H[d+4>>2]-B|0;j:{if((d|0)<8){break j}w=d>>2;I=(w|0)<=2?2:w;J=w>>>0<=1?1:w;C=e&-2;D=e&1;K=e&-4;E=e&3;F=e-1|0;L=e<<2;M=e>>>0<4;A=0;m=1;while(1){k:{l:{m:{n:{if((m|0)!=(J|0)){o:{p:{f=H[(m<<2)+B>>2];if((f|0)==-1){break p}k=1;d=f+2|0;j=(f>>>0)%3|0;x=j?f-1|0:d;s=1<>2];O=n+(x>>>3&536870908)|0;i=0;P=(j|0)!=0|(d|0)!=-1;d=f;q:{while(1){r:{if(H[n+(d>>>3&536870908)>>2]>>>d&1){break r}j=H[H[H[t+64>>2]+12>>2]+(d<<2)>>2];if((j|0)==-1){break r}l=H[G>>2];h=H[t+28>>2];p=H[l+(H[h+(j<<2)>>2]<<2)>>2];if((p|0)>=(m|0)){break r}q=j+1|0;q=H[l+(H[h+(((q>>>0)%3|0?q:j-2|0)<<2)>>2]<<2)>>2];if((q|0)>=(m|0)){break r}h=H[l+(H[h+(j+((j>>>0)%3|0?-1:2)<<2)>>2]<<2)>>2];if((h|0)>=(m|0)){break r}s:{if(!e){break s}j=H[(g+16|0)+N(i,12)>>2];l=N(e,h);q=N(e,q);p=N(e,p);h=0;o=0;if(F){while(1){H[j+(h<<2)>>2]=(H[(h+l<<2)+c>>2]+H[(h+q<<2)+c>>2]|0)-H[(h+p<<2)+c>>2];u=h|1;H[j+(u<<2)>>2]=(H[(l+u<<2)+c>>2]+H[(q+u<<2)+c>>2]|0)-H[(p+u<<2)+c>>2];h=h+2|0;o=o+2|0;if((C|0)!=(o|0)){continue}break}}if(!D){break s}H[j+(h<<2)>>2]=(H[(h+l<<2)+c>>2]+H[(h+q<<2)+c>>2]|0)-H[(h+p<<2)+c>>2]}j=4;i=i+1|0;if((i|0)==4){break q}}t:{if(k&1){h=d-2|0;j=d+1|0;d=-1;j=(j>>>0)%3|0?j:h;if((j|0)==-1|H[n+(j>>>3&536870908)>>2]>>>j&1){break t}j=H[H[H[t+64>>2]+12>>2]+(j<<2)>>2];if((j|0)==-1){break t}d=j+1|0;d=(d>>>0)%3|0?d:j-2|0;break t}u:{if((d>>>0)%3|0){h=d-1|0;break u}h=d+2|0;d=-1;if((h|0)==-1){break t}}d=-1;if(H[n+(h>>>3&536870908)>>2]>>>h&1){break t}j=H[H[H[t+64>>2]+12>>2]+(h<<2)>>2];if((j|0)==-1){break t}if((j>>>0)%3|0){d=j-1|0;break t}d=j+2|0}v:{if((d|0)==(f|0)){break v}if((d|0)==-1&k){if(!P|s&H[O>>2]){break v}d=H[H[H[t+64>>2]+12>>2]+(x<<2)>>2];if((d|0)==-1){break v}k=0;d=(d>>>0)%3|0?d-1|0:d+2|0}if((d|0)!=-1){continue}}break}j=i;if((j|0)<=0){break p}}if(e){ra(r,0,L)}d=j-1|0;q=(d<<2)+z|0;d=N(d,12)+a|0;u=d;x=H[d- -64>>2];k=0;d=H[g>>2];f=0;while(1){i=H[q>>2];H[q>>2]=i+1;if(i>>>0>=x>>>0){break j}w:{if(H[H[u+60>>2]+(i>>>3&536870908)>>2]>>>i&1){break w}f=f+1|0;if(!e){break w}n=H[(g+16|0)+N(k,12)>>2];i=0;h=0;p=0;if(!M){while(1){l=h<<2;o=l+d|0;H[o>>2]=H[l+n>>2]+H[o>>2];o=l|4;s=o+d|0;H[s>>2]=H[n+o>>2]+H[s>>2];o=l|8;s=o+d|0;H[s>>2]=H[n+o>>2]+H[s>>2];l=l|12;o=l+d|0;H[o>>2]=H[l+n>>2]+H[o>>2];h=h+4|0;p=p+4|0;if((K|0)!=(p|0)){continue}break}}if(!E){break w}while(1){l=h<<2;p=l+d|0;H[p>>2]=H[l+n>>2]+H[p>>2];h=h+1|0;i=i+1|0;if((E|0)!=(i|0)){continue}break}}k=k+1|0;if((k|0)!=(j|0)){continue}break}i=N(e,m);if(!f){break o}if(!e){break l}h=0;d=0;if(F){break n}break m}i=N(e,m)}if(H[a+8>>2]<=0){break k}k=(N(m-1|0,e)<<2)+c|0;j=H[y>>2];h=0;while(1){d=h<<2;f=H[d+k>>2];n=H[a+16>>2];x:{if((f|0)>(n|0)){H[d+j>>2]=n;break x}d=d+j|0;n=H[a+12>>2];if((n|0)>(f|0)){H[d>>2]=n;break x}H[d>>2]=f}h=h+1|0;f=H[a+8>>2];if((h|0)<(f|0)){continue}break}d=0;if((f|0)<=0){break k}f=i<<2;h=f+c|0;k=b+f|0;while(1){i=d<<2;f=i+h|0;i=H[i+k>>2]+H[j+i>>2]|0;H[f>>2]=i;y:{if((i|0)>H[a+16>>2]){i=i-H[a+20>>2]|0}else{if((i|0)>=H[a+12>>2]){break y}i=i+H[a+20>>2]|0}H[f>>2]=i}d=d+1|0;if((d|0)>2]){continue}break}break k}Ca();v()}while(1){j=h<<2;k=j+r|0;H[k>>2]=H[k>>2]/(f|0);j=(j|4)+r|0;H[j>>2]=H[j>>2]/(f|0);h=h+2|0;d=d+2|0;if((C|0)!=(d|0)){continue}break}}if(!D){break l}d=(h<<2)+r|0;H[d>>2]=H[d>>2]/(f|0)}if(H[a+8>>2]<=0){break k}j=H[y>>2];h=0;while(1){d=h<<2;f=H[d+r>>2];k=H[a+16>>2];z:{if((f|0)>(k|0)){H[d+j>>2]=k;break z}d=d+j|0;k=H[a+12>>2];if((k|0)>(f|0)){H[d>>2]=k;break z}H[d>>2]=f}h=h+1|0;f=H[a+8>>2];if((h|0)<(f|0)){continue}break}d=0;if((f|0)<=0){break k}f=i<<2;h=f+c|0;k=b+f|0;while(1){i=d<<2;f=i+h|0;i=H[i+k>>2]+H[j+i>>2]|0;H[f>>2]=i;A:{if((i|0)>H[a+16>>2]){i=i-H[a+20>>2]|0}else{if((i|0)>=H[a+12>>2]){break A}i=i+H[a+20>>2]|0}H[f>>2]=i}d=d+1|0;if((d|0)>2]){continue}break}}m=m+1|0;A=(w|0)<=(m|0);if((m|0)!=(I|0)){continue}break}}a=H[g>>2];if(a){oa(a)}oa(z);a=H[g+52>>2];if(a){H[g+56>>2]=a;oa(a)}a=H[g+40>>2];if(a){H[g+44>>2]=a;oa(a)}a=H[g+28>>2];if(a){H[g+32>>2]=a;oa(a)}a=H[g+16>>2];if(a){H[g+20>>2]=a;oa(a)}ca=g- -64|0;return A|0}sa();v()}function oj(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;var g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0,x=0,y=0,z=0,A=0,B=0,C=0,D=0,E=0,F=0,G=0,I=0,J=0,K=0,L=0,M=0;h=ca+-64|0;ca=h;H[a+8>>2]=e;x=a+32|0;f=H[x>>2];d=H[a+36>>2]-f>>2;a:{b:{if(d>>>0>>0){ya(x,e-d|0);H[h+56>>2]=0;H[h+60>>2]=0;H[h+48>>2]=0;H[h+52>>2]=0;H[h+40>>2]=0;H[h+44>>2]=0;H[h+32>>2]=0;H[h+36>>2]=0;H[h+24>>2]=0;H[h+28>>2]=0;H[h+16>>2]=0;H[h+20>>2]=0;H[h>>2]=0;break b}if(d>>>0>e>>>0){H[a+36>>2]=f+(e<<2)}H[h+56>>2]=0;H[h+60>>2]=0;H[h+48>>2]=0;H[h+52>>2]=0;H[h+40>>2]=0;H[h+44>>2]=0;H[h+32>>2]=0;H[h+36>>2]=0;H[h+24>>2]=0;H[h+28>>2]=0;H[h+16>>2]=0;H[h+20>>2]=0;H[h>>2]=0;d=0;if(!e){break a}}Pa(h+16|0,e,h);i=H[h+28>>2];d=H[h+32>>2]}H[h>>2]=0;d=d-i>>2;c:{if(d>>>0>=e>>>0){if(d>>>0<=e>>>0){break c}H[h+32>>2]=(e<<2)+i;break c}Pa(h+16|12,e-d|0,h)}H[h>>2]=0;f=H[h+40>>2];d=H[h+44>>2]-f>>2;d:{if(d>>>0>=e>>>0){if(d>>>0<=e>>>0){break d}H[h+44>>2]=f+(e<<2);break d}Pa(h+40|0,e-d|0,h)}H[h>>2]=0;f=H[h+52>>2];d=H[h+56>>2]-f>>2;e:{if(d>>>0>=e>>>0){if(d>>>0<=e>>>0){break e}H[h+56>>2]=f+(e<<2);break e}Pa(h+52|0,e-d|0,h)}f:{if(H[a+8>>2]<=0){break f}g=H[h+16>>2];j=H[a+32>>2];i=0;while(1){d=i<<2;f=H[d+g>>2];m=H[a+16>>2];g:{if((f|0)>(m|0)){H[d+j>>2]=m;break g}d=d+j|0;m=H[a+12>>2];if((m|0)>(f|0)){H[d>>2]=m;break g}H[d>>2]=f}i=i+1|0;d=H[a+8>>2];if((i|0)<(d|0)){continue}break}if((d|0)<=0){break f}d=0;while(1){g=d<<2;f=g+c|0;g=H[b+g>>2]+H[g+j>>2]|0;H[f>>2]=g;h:{if((g|0)>H[a+16>>2]){g=g-H[a+20>>2]|0}else{if((g|0)>=H[a+12>>2]){break h}g=g+H[a+20>>2]|0}H[f>>2]=g}d=d+1|0;if((d|0)>2]){continue}break}}G=H[a+52>>2];A=H[a+48>>2];y=pa(16);d=y;H[d>>2]=0;H[d+4>>2]=0;H[d+8>>2]=0;H[d+12>>2]=0;H[h+8>>2]=0;H[h>>2]=0;H[h+4>>2]=0;i:{if(e){if(e>>>0>=1073741824){break i}d=e<<2;t=pa(d);H[h>>2]=t;H[h+8>>2]=d+t;ra(t,0,d)}z=1;d=H[a+56>>2];B=H[d>>2];d=H[d+4>>2]-B|0;j:{if((d|0)<8){break j}w=d>>2;I=(w|0)<=2?2:w;J=w>>>0<=1?1:w;C=e&-2;D=e&1;K=e&-4;E=e&3;F=e-1|0;L=e<<2;M=e>>>0<4;z=0;m=1;while(1){k:{l:{m:{n:{if((m|0)!=(J|0)){o:{p:{f=H[(m<<2)+B>>2];if((f|0)==-1){break p}n=H[A+12>>2];d=f+2|0;g=(f>>>0)%3|0;q=n+((g?f-1|0:d)<<2)|0;j=0;u=(g|0)!=0|(d|0)!=-1;k=1;d=f;q:{while(1){g=H[n+(d<<2)>>2];r:{if((g|0)==-1){break r}l=-1;p=H[G>>2];r=H[A>>2];i=p+(H[r+(g<<2)>>2]<<2)|0;o=g+1|0;o=(o>>>0)%3|0?o:g-2|0;if((o|0)!=-1){l=H[r+(o<<2)>>2]}o=H[i>>2];s:{t:{if((g>>>0)%3|0){i=g-1|0;break t}i=g+2|0;s=-1;if((i|0)==-1){break s}}s=H[r+(i<<2)>>2]}if((m|0)<=(o|0)){break r}i=H[p+(l<<2)>>2];if((i|0)>=(m|0)){break r}l=H[p+(s<<2)>>2];if((l|0)>=(m|0)){break r}g=H[(h+16|0)+N(j,12)>>2];u:{if(!e){break u}l=N(e,l);r=N(e,i);p=N(e,o);i=0;s=0;if(F){while(1){H[g+(i<<2)>>2]=(H[(i+l<<2)+c>>2]+H[(i+r<<2)+c>>2]|0)-H[(i+p<<2)+c>>2];o=i|1;H[g+(o<<2)>>2]=(H[(l+o<<2)+c>>2]+H[(o+r<<2)+c>>2]|0)-H[(o+p<<2)+c>>2];i=i+2|0;s=s+2|0;if((C|0)!=(s|0)){continue}break}}if(!D){break u}H[g+(i<<2)>>2]=(H[(i+l<<2)+c>>2]+H[(i+r<<2)+c>>2]|0)-H[(i+p<<2)+c>>2]}g=4;j=j+1|0;if((j|0)==4){break q}}v:{if(k&1){i=d+1|0;d=(i>>>0)%3|0?i:d-2|0;g=-1;if((d|0)==-1){break v}d=H[n+(d<<2)>>2];g=-1;if((d|0)==-1){break v}g=d+1|0;g=(g>>>0)%3|0?g:d-2|0;break v}w:{if((d>>>0)%3|0){i=d-1|0;break w}i=d+2|0;g=-1;if((i|0)==-1){break v}}d=H[n+(i<<2)>>2];g=-1;if((d|0)==-1){break v}g=d-1|0;if((d>>>0)%3|0){break v}g=d+2|0}d=g;x:{if((f|0)==(d|0)){break x}if((d|0)==-1&k){if(!u){break x}d=H[q>>2];if((d|0)==-1){break x}k=0;d=(d>>>0)%3|0?d-1|0:d+2|0}if((d|0)!=-1){continue}}break}g=j;if((g|0)<=0){break p}}if(e){ra(t,0,L)}d=g-1|0;r=(d<<2)+y|0;d=N(d,12)+a|0;o=d;s=H[d- -64>>2];k=0;d=H[h>>2];f=0;while(1){j=H[r>>2];H[r>>2]=j+1;if(j>>>0>=s>>>0){break j}y:{if(H[H[o+60>>2]+(j>>>3&536870908)>>2]>>>j&1){break y}f=f+1|0;if(!e){break y}j=H[(h+16|0)+N(k,12)>>2];l=0;i=0;p=0;if(!M){while(1){n=i<<2;q=n+d|0;H[q>>2]=H[j+n>>2]+H[q>>2];q=n|4;u=q+d|0;H[u>>2]=H[j+q>>2]+H[u>>2];q=n|8;u=q+d|0;H[u>>2]=H[j+q>>2]+H[u>>2];n=n|12;q=n+d|0;H[q>>2]=H[j+n>>2]+H[q>>2];i=i+4|0;p=p+4|0;if((K|0)!=(p|0)){continue}break}}if(!E){break y}while(1){n=i<<2;p=n+d|0;H[p>>2]=H[j+n>>2]+H[p>>2];i=i+1|0;l=l+1|0;if((E|0)!=(l|0)){continue}break}}k=k+1|0;if((k|0)!=(g|0)){continue}break}g=N(e,m);if(!f){break o}if(!e){break l}i=0;d=0;if(F){break n}break m}g=N(e,m)}if(H[a+8>>2]<=0){break k}k=(N(m-1|0,e)<<2)+c|0;j=H[x>>2];i=0;while(1){d=i<<2;f=H[d+k>>2];l=H[a+16>>2];z:{if((f|0)>(l|0)){H[d+j>>2]=l;break z}d=d+j|0;l=H[a+12>>2];if((l|0)>(f|0)){H[d>>2]=l;break z}H[d>>2]=f}i=i+1|0;f=H[a+8>>2];if((i|0)<(f|0)){continue}break}d=0;if((f|0)<=0){break k}f=g<<2;i=f+c|0;k=b+f|0;while(1){g=d<<2;f=g+i|0;g=H[g+k>>2]+H[g+j>>2]|0;H[f>>2]=g;A:{if((g|0)>H[a+16>>2]){g=g-H[a+20>>2]|0}else{if((g|0)>=H[a+12>>2]){break A}g=g+H[a+20>>2]|0}H[f>>2]=g}d=d+1|0;if((d|0)>2]){continue}break}break k}Ca();v()}while(1){j=i<<2;k=j+t|0;H[k>>2]=H[k>>2]/(f|0);j=(j|4)+t|0;H[j>>2]=H[j>>2]/(f|0);i=i+2|0;d=d+2|0;if((C|0)!=(d|0)){continue}break}}if(!D){break l}d=(i<<2)+t|0;H[d>>2]=H[d>>2]/(f|0)}if(H[a+8>>2]<=0){break k}j=H[x>>2];i=0;while(1){d=i<<2;f=H[d+t>>2];k=H[a+16>>2];B:{if((f|0)>(k|0)){H[d+j>>2]=k;break B}d=d+j|0;k=H[a+12>>2];if((k|0)>(f|0)){H[d>>2]=k;break B}H[d>>2]=f}i=i+1|0;f=H[a+8>>2];if((i|0)<(f|0)){continue}break}d=0;if((f|0)<=0){break k}f=g<<2;i=f+c|0;k=b+f|0;while(1){g=d<<2;f=g+i|0;g=H[g+k>>2]+H[g+j>>2]|0;H[f>>2]=g;C:{if((g|0)>H[a+16>>2]){g=g-H[a+20>>2]|0}else{if((g|0)>=H[a+12>>2]){break C}g=g+H[a+20>>2]|0}H[f>>2]=g}d=d+1|0;if((d|0)>2]){continue}break}}m=m+1|0;z=(w|0)<=(m|0);if((m|0)!=(I|0)){continue}break}}a=H[h>>2];if(a){oa(a)}oa(y);a=H[h+52>>2];if(a){H[h+56>>2]=a;oa(a)}a=H[h+40>>2];if(a){H[h+44>>2]=a;oa(a)}a=H[h+28>>2];if(a){H[h+32>>2]=a;oa(a)}a=H[h+16>>2];if(a){H[h+20>>2]=a;oa(a)}ca=h- -64|0;return z|0}sa();v()}function Od(a,b,c,d,e){var f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0,x=0,y=0,z=0,A=0,B=0;i=ca-80|0;ca=i;H[i+76>>2]=b;y=i+55|0;r=i+56|0;a:{b:{c:{d:{e:while(1){h=b;if((o^2147483647)<(f|0)){break d}o=f+o|0;f:{g:{h:{f=h;g=I[f|0];if(g){while(1){i:{b=g&255;j:{if(!b){b=f;break j}if((b|0)!=37){break i}g=f;while(1){if(I[g+1|0]!=37){b=g;break j}f=f+1|0;j=I[g+2|0];b=g+2|0;g=b;if((j|0)==37){continue}break}}f=f-h|0;x=o^2147483647;if((f|0)>(x|0)){break d}if(a){Ab(a,h,f)}if(f){continue e}H[i+76>>2]=b;f=b+1|0;p=-1;if(!(I[b+2|0]!=36|F[b+1|0]-48>>>0>=10)){p=F[b+1|0]-48|0;s=1;f=b+3|0}H[i+76>>2]=f;n=0;g=F[f|0];b=g-32|0;k:{if(b>>>0>31){k=f;break k}k=f;b=1<>2]=k;n=b|n;g=F[f+1|0];b=g-32|0;if(b>>>0>=32){break k}f=k;b=1<>>0>=10)){H[((F[k+1|0]<<2)+e|0)-192>>2]=10;g=k+3|0;s=1;b=H[((F[k+1|0]<<3)+d|0)-384>>2];break m}if(s){break h}g=k+1|0;if(!a){H[i+76>>2]=g;s=0;q=0;break l}b=H[c>>2];H[c>>2]=b+4;s=0;b=H[b>>2]}H[i+76>>2]=g;q=b;if((b|0)>=0){break l}q=0-q|0;n=n|8192;break l}q=Nd(i+76|0);if((q|0)<0){break d}g=H[i+76>>2]}f=0;m=-1;n:{if(I[g|0]!=46){b=g;u=0;break n}if(I[g+1|0]==42){o:{if(!(I[g+3|0]!=36|F[g+2|0]-48>>>0>=10)){H[((F[g+2|0]<<2)+e|0)-192>>2]=10;b=g+4|0;m=H[((F[g+2|0]<<3)+d|0)-384>>2];break o}if(s){break h}b=g+2|0;m=0;if(!a){break o}j=H[c>>2];H[c>>2]=j+4;m=H[j>>2]}H[i+76>>2]=b;u=(m^-1)>>>31|0;break n}H[i+76>>2]=g+1;m=Nd(i+76|0);b=H[i+76>>2];u=1}while(1){g=f;k=28;l=b;f=F[b|0];if(f-123>>>0<4294967238){break c}b=l+1|0;f=I[(f+N(g,58)|0)+13711|0];if(f-1>>>0<8){continue}break}H[i+76>>2]=b;p:{q:{if((f|0)!=27){if(!f){break c}if((p|0)>=0){H[(p<<2)+e>>2]=f;j=(p<<3)+d|0;f=H[j+4>>2];H[i+64>>2]=H[j>>2];H[i+68>>2]=f;break q}if(!a){break f}Md(i- -64|0,f,c);break p}if((p|0)>=0){break c}}f=0;if(!a){continue e}}j=n&-65537;n=n&8192?j:n;p=0;t=1132;k=r;r:{s:{t:{u:{v:{w:{x:{y:{z:{A:{B:{C:{D:{E:{F:{G:{f=F[l|0];f=g?(f&15)==3?f&-33:f:f;switch(f-88|0){case 11:break r;case 9:case 13:case 14:case 15:break s;case 27:break x;case 12:case 17:break A;case 23:break B;case 0:case 32:break C;case 24:break D;case 22:break E;case 29:break F;case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 10:case 16:case 18:case 19:case 20:case 21:case 25:case 26:case 28:case 30:case 31:break g;default:break G}}H:{switch(f-65|0){case 0:case 4:case 5:case 6:break s;case 2:break v;case 1:case 3:break g;default:break H}}if((f|0)==83){break w}break g}l=H[i+64>>2];j=H[i+68>>2];t=1132;break z}f=0;I:{switch(g&255){case 0:H[H[i+64>>2]>>2]=o;continue e;case 1:H[H[i+64>>2]>>2]=o;continue e;case 2:h=H[i+64>>2];H[h>>2]=o;H[h+4>>2]=o>>31;continue e;case 3:G[H[i+64>>2]>>1]=o;continue e;case 4:F[H[i+64>>2]]=o;continue e;case 6:H[H[i+64>>2]>>2]=o;continue e;case 7:break I;default:continue e}}h=H[i+64>>2];H[h>>2]=o;H[h+4>>2]=o>>31;continue e}m=m>>>0<=8?8:m;n=n|8;f=120}h=r;l=H[i+64>>2];j=H[i+68>>2];if(l|j){z=f&32;while(1){h=h-1|0;F[h|0]=z|I[(l&15)+14240|0];w=!j&l>>>0>15|(j|0)!=0;g=j;j=g>>>4|0;l=(g&15)<<28|l>>>4;if(w){continue}break}}if(!(H[i+64>>2]|H[i+68>>2])|!(n&8)){break y}t=(f>>>4|0)+1132|0;p=2;break y}f=r;h=H[i+68>>2];j=h;l=H[i+64>>2];if(h|l){while(1){f=f-1|0;F[f|0]=l&7|48;g=!j&l>>>0>7|(j|0)!=0;h=j;j=h>>>3|0;l=(h&7)<<29|l>>>3;if(g){continue}break}}h=f;if(!(n&8)){break y}f=r-h|0;m=(f|0)<(m|0)?m:f+1|0;break y}l=H[i+64>>2];h=H[i+68>>2];j=h;if((h|0)<0){f=0-(((l|0)!=0)+j|0)|0;j=f;l=0-l|0;H[i+64>>2]=l;H[i+68>>2]=f;p=1;t=1132;break z}if(n&2048){p=1;t=1133;break z}p=n&1;t=p?1134:1132}g=r;if(j){while(1){g=g-1|0;f=j;w=Tj(l,f,10,0);h=da;A=g,B=l-Rj(w,h,10,0)|48,F[A|0]=B;l=w;j=h;if(f>>>0>9){continue}break}}h=l;if(h){while(1){g=g-1|0;f=(h>>>0)/10|0;F[g|0]=h-N(f,10)|48;j=h>>>0>9;h=f;if(j){continue}break}}h=g}if((m|0)<0?u:0){break d}n=u?n&-65537:n;f=H[i+64>>2];j=H[i+68>>2];if(!(m|(f|j)!=0)){h=r;m=0;break g}f=!(f|j)+(r-h|0)|0;m=(f|0)<(m|0)?m:f;break g}g=m>>>0>=2147483647?2147483647:m;k=g;n=(g|0)!=0;h=H[i+64>>2];h=h?h:1614;f=h;J:{K:{L:{M:{if(!(f&3)|!g){break M}while(1){if(!I[f|0]){break L}k=k-1|0;n=(k|0)!=0;f=f+1|0;if(!(f&3)){break M}if(k){continue}break}}if(!n){break K}if(!(!I[f|0]|k>>>0<4)){while(1){l=H[f>>2];if((l^-1)&l-16843009&-2139062144){break L}f=f+4|0;k=k-4|0;if(k>>>0>3){continue}break}}if(!k){break K}}while(1){if(!I[f|0]){break J}f=f+1|0;k=k-1|0;if(k){continue}break}}f=0}f=f?f-h|0:g;k=f+h|0;if((m|0)>=0){n=j;m=f;break g}n=j;m=f;if(I[k|0]){break d}break g}if(m){g=H[i+64>>2];break u}f=0;ib(a,32,q,0,n);break t}H[i+12>>2]=0;H[i+8>>2]=H[i+64>>2];g=i+8|0;H[i+64>>2]=g;m=-1}f=0;N:{while(1){h=H[g>>2];if(!h){break N}j=Ld(i+4|0,h);h=(j|0)<0;if(!(h|j>>>0>m-f>>>0)){g=g+4|0;f=f+j|0;if(m>>>0>f>>>0){continue}break N}break}if(h){break b}}k=61;if((f|0)<0){break c}ib(a,32,q,f,n);if(!f){f=0;break t}k=0;g=H[i+64>>2];while(1){h=H[g>>2];if(!h){break t}h=Ld(i+4|0,h);k=h+k|0;if(k>>>0>f>>>0){break t}Ab(a,i+4|0,h);g=g+4|0;if(f>>>0>k>>>0){continue}break}}ib(a,32,q,f,n^8192);f=(f|0)<(q|0)?q:f;continue e}if((m|0)<0?u:0){break d}v()}F[i+55|0]=H[i+64>>2];m=1;h=y;n=j;break g}g=I[f+1|0];f=f+1|0;continue}}if(a){break a}if(!s){break f}f=1;while(1){a=H[(f<<2)+e>>2];if(a){Md((f<<3)+d|0,a,c);o=1;f=f+1|0;if((f|0)!=10){continue}break a}break}o=1;if(f>>>0>=10){break a}while(1){if(H[(f<<2)+e>>2]){break h}f=f+1|0;if((f|0)!=10){continue}break}break a}k=28;break c}l=k-h|0;j=(m|0)>(l|0)?m:l;if((j|0)>(p^2147483647)){break d}k=61;g=j+p|0;f=(g|0)<(q|0)?q:g;if((x|0)<(f|0)){break c}ib(a,32,f,g,n);Ab(a,t,p);ib(a,48,f,g,n^65536);ib(a,48,j,l,0);Ab(a,h,l);ib(a,32,f,g,n^8192);continue}break}o=0;break a}k=61}H[3992]=k}o=-1}ca=i+80|0;return o}function hj(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;var g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0,x=0,y=0,z=0,A=0,B=0,C=0,D=0,E=0,G=0,J=0,K=0,L=0,M=0,N=0,O=0,P=0,Q=0,R=0,S=0,T=0,U=0,V=0,W=0,X=0,Y=0,Z=0,_=0;a:{b:{if((e|0)!=2){break b}H[a+8>>2]=2;H[a- -64>>2]=f;M=a+32|0;e=H[M>>2];d=H[a+36>>2]-e|0;c:{if(d>>>0<=7){ya(M,2-(d>>>2|0)|0);break c}if((d|0)==8){break c}H[a+36>>2]=e+8}i=1;d=H[a+56>>2];d=H[d+4>>2]-H[d>>2]|0;if((d|0)<=0){break b}o=a+60|0;d=d>>>2|0;X=d>>>0<=1?1:d;Y=a+68|0;d=0;while(1){f=H[a+56>>2];e=H[f>>2];if(H[f+4>>2]-e>>2>>>0<=d>>>0){break a}k=ca-80|0;ca=k;f=-1;d:{e:{e=H[e+(d<<2)>>2];if((e|0)==-1){break e}i=H[o+32>>2];g=e+1|0;g=(g>>>0)%3|0?g:e-2|0;if((g|0)!=-1){f=H[H[i>>2]+(g<<2)>>2]}p=-1;e=e+((e>>>0)%3|0?-1:2)|0;if((e|0)!=-1){p=H[H[i>>2]+(e<<2)>>2]}i=H[o+36>>2];e=H[i>>2];i=H[i+4>>2]-e>>2;if(i>>>0<=f>>>0|i>>>0<=p>>>0){break e}f:{g:{h:{i:{j:{k:{j=H[e+(p<<2)>>2];f=H[e+(f<<2)>>2];if((j|0)>=(d|0)|(f|0)>=(d|0)){break k}i=(j<<3)+c|0;w=H[i+4>>2];g=(f<<3)+c|0;e=H[g+4>>2];l=H[i>>2];i=H[g>>2];if(!((l|0)!=(i|0)|(e|0)!=(w|0))){H[o+8>>2]=i;H[o+12>>2]=e;break j}p=H[H[o+4>>2]+(d<<2)>>2];H[k+72>>2]=0;H[k+76>>2]=0;g=k- -64|0;H[g>>2]=0;H[g+4>>2]=0;H[k+56>>2]=0;H[k+60>>2]=0;g=H[o>>2];if(!I[g+84|0]){p=H[H[g+68>>2]+(p<<2)>>2]}Sa(g,p,F[g+24|0],k+56|0);p=H[H[o+4>>2]+(f<<2)>>2];H[k+48>>2]=0;H[k+52>>2]=0;H[k+40>>2]=0;H[k+44>>2]=0;H[k+32>>2]=0;H[k+36>>2]=0;g=H[o>>2];if(!I[g+84|0]){p=H[H[g+68>>2]+(p<<2)>>2]}Sa(g,p,F[g+24|0],k+32|0);p=H[H[o+4>>2]+(j<<2)>>2];H[k+24>>2]=0;H[k+28>>2]=0;H[k+16>>2]=0;H[k+20>>2]=0;H[k+8>>2]=0;H[k+12>>2]=0;g=H[o>>2];if(!I[g+84|0]){p=H[H[g+68>>2]+(p<<2)>>2]}Sa(g,p,F[g+24|0],k+8|0);g=H[k+16>>2];n=H[k+40>>2];x=g-n|0;N=H[k+44>>2];g=H[k+20>>2]-(N+(g>>>0>>0)|0)|0;E=g;j=Rj(x,g,x,g);q=da;g=H[k+8>>2];z=H[k+32>>2];A=g-z|0;O=H[k+36>>2];g=H[k+12>>2]-(O+(g>>>0>>0)|0)|0;G=g;h=j;j=Rj(A,g,A,g);g=h+j|0;h=da+q|0;h=g>>>0>>0?h+1|0:h;j=H[k+24>>2];B=H[k+48>>2];C=j-B|0;P=H[k+52>>2];j=H[k+28>>2]-(P+(j>>>0>>0)|0)|0;J=j;m=g;g=Rj(C,j,C,j);r=m+g|0;h=da+h|0;s=g>>>0>r>>>0?h+1|0:h;if(!(s|r)){break k}p=0;D=Tj(-1,2147483647,r,s);f=i>>31;R=f;h=f>>31;Q=i;g=h;q=i^g;i=q-g|0;f=(f^g)-((g>>>0>q>>>0)+g|0)|0;g=f;f=e>>31;S=f;K=e;e=f>>31;q=K^e;m=q-e|0;h=f>>31;e=(h^f)-((e>>>0>q>>>0)+h|0)|0;f=(g|0)==(e|0)&i>>>0>m>>>0|e>>>0>>0;i=f?i:m;j=da;e=f?g:e;if((j|0)==(e|0)&i>>>0>D>>>0|e>>>0>j>>>0){break f}i=H[k+64>>2];T=H[k+68>>2];e=Rj(i-n|0,T-((i>>>0>>0)+N|0)|0,x,E);f=da;g=H[k+56>>2];U=H[k+60>>2];j=Rj(g-z|0,U-((g>>>0>>0)+O|0)|0,A,G);e=j+e|0;h=da+f|0;h=e>>>0>>0?h+1|0:h;f=e;m=H[k+72>>2];V=H[k+76>>2];e=Rj(m-B|0,V-((m>>>0>>0)+P|0)|0,C,J);j=f+e|0;f=da+h|0;q=e>>>0>j>>>0?f+1|0:f;e=l;D=e-Q|0;e=(e>>31)-((e>>>0>>0)+R|0)|0;W=e;l=e>>31;y=l^D;f=y-l|0;h=e>>31;e=(h^e)-((l>>>0>y>>>0)+h|0)|0;h=e;y=w-K|0;e=(w>>31)-((w>>>0>>0)+S|0)|0;w=e;l=f;t=e>>31;u=t^y;L=u-t|0;f=e>>31;e=(f^e)-((t>>>0>u>>>0)+f|0)|0;f=(h|0)==(e|0)&l>>>0>L>>>0|e>>>0>>0;f=Tj(-1,2147483647,f?l:L,f?h:e)>>>0>>0;e=da;if(f&(e|0)<=(q|0)|(e|0)<(q|0)){break f}e=G>>31;f=e;l=e^A;e=l-e|0;f=(f^G)-((f>>>0>l>>>0)+f|0)|0;h=E>>31;t=h^x;u=t-h|0;l=(h^E)-((h>>>0>t>>>0)+h|0)|0;h=(f|0)==(l|0)&e>>>0>u>>>0|f>>>0>l>>>0;e=h?e:u;f=h?f:l;h=J>>31;L=e;t=h^C;u=t-h|0;l=(h^J)-((h>>>0>t>>>0)+h|0)|0;e=(f|0)==(l|0)&e>>>0>u>>>0|f>>>0>l>>>0;f=Tj(-1,2147483647,e?L:u,e?f:l)>>>0>>0;e=da;if(f&(e|0)<=(q|0)|(e|0)<(q|0)){break f}l=1;e=0;f=n;n=Sj(Rj(j,q,x,E),da,r,s);f=f+n|0;h=da+N|0;h=f>>>0>>0?h+1|0:h;n=i-f|0;f=T-((f>>>0>i>>>0)+h|0)|0;n=Rj(n,f,n,f);x=da;f=g;h=Sj(Rj(j,q,A,G),da,r,s);i=h+z|0;g=da+O|0;g=h>>>0>i>>>0?g+1|0:g;h=f-i|0;f=U-((f>>>0>>0)+g|0)|0;g=Rj(h,f,h,f);i=g+n|0;f=da+x|0;f=g>>>0>i>>>0?f+1|0:f;n=i;g=Sj(Rj(j,q,C,J),da,r,s);i=g+B|0;h=da+P|0;h=g>>>0>i>>>0?h+1|0:h;g=m-i|0;i=V-((i>>>0>m>>>0)+h|0)|0;m=Rj(g,i,g,i);i=m+n|0;g=da+f|0;f=Rj(i,i>>>0>>0?g+1|0:g,r,s);i=da;m=i;if(!i&f>>>0<=1){break i}h=f;while(1){g=e<<1|l>>>31;l=l<<1;e=g;n=!i&h>>>0>7|(i|0)!=0;h=(i&3)<<30|h>>>2;i=i>>>2|0;if(n){continue}break}break h}if((d|0)>(f|0)){e=f<<1}else{if((d|0)<=0){H[o+8>>2]=0;H[o+12>>2]=0;break j}e=(d<<1)-2|0}e=(e<<2)+c|0;H[o+8>>2]=H[e>>2];H[o+12>>2]=H[e+4>>2]}p=1;break f}e=m;l=f;if(f-1|0){break g}}while(1){i=Tj(f,m,l,e);h=e+da|0;e=i+l|0;h=e>>>0>>0?h+1|0:h;l=(h&1)<<31|e>>>1;e=h>>>1|0;i=Rj(l,e,l,e);g=da;if((m|0)==(g|0)&f>>>0>>0|g>>>0>m>>>0){continue}break}}f=H[o+20>>2];if(!f){break f}g=f-1|0;h=H[H[o+16>>2]+(g>>>3&536870908)>>2];H[o+20>>2]=g;p=1;f=Rj(j,q,y,w);i=da;n=Rj(r,s,K,S);m=n+f|0;f=da+i|0;f=m>>>0>>0?f+1|0:f;i=Rj(l,e,D,W);g=h>>>g&1;h=g?0-i|0:i;m=h+m|0;n=f;f=da;i=n+(g?0-(f+((i|0)!=0)|0)|0:f)|0;Z=o,_=Sj(m,h>>>0>m>>>0?i+1|0:i,r,s),H[Z+12>>2]=_;f=Rj(j,q,D,W);i=da;j=Rj(r,s,Q,R);f=j+f|0;h=da+i|0;e=Rj(l,e,y,w);i=0-e|0;l=da;h=(f>>>0>>0?h+1|0:h)+(g?l:0-(((e|0)!=0)+l|0)|0)|0;i=g?e:i;f=i+f|0;Z=o,_=Sj(f,f>>>0>>0?h+1|0:h,r,s),H[Z+8>>2]=_}ca=k+80|0;e=p;break d}Ca();v()}i=e;if(!e){return 0}l:{if(H[a+8>>2]<=0){break l}l=H[M>>2];e=0;while(1){f=e<<2;g=H[f+Y>>2];j=H[a+16>>2];m:{if((g|0)>(j|0)){H[f+l>>2]=j;break m}f=f+l|0;j=H[a+12>>2];if((j|0)>(g|0)){H[f>>2]=j;break m}H[f>>2]=g}e=e+1|0;g=H[a+8>>2];if((e|0)<(g|0)){continue}break}f=0;if((g|0)<=0){break l}e=d<<3;j=e+c|0;q=b+e|0;while(1){g=f<<2;e=g+j|0;g=H[g+q>>2]+H[g+l>>2]|0;H[e>>2]=g;n:{if((g|0)>H[a+16>>2]){g=g-H[a+20>>2]|0}else{if((g|0)>=H[a+12>>2]){break n}g=g+H[a+20>>2]|0}H[e>>2]=g}f=f+1|0;if((f|0)>2]){continue}break}}d=d+1|0;if((X|0)!=(d|0)){continue}break}}return i|0}Ca();v()}function xj(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;var g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0,x=0,y=0,z=0,A=0,B=0,C=0,D=0,E=0,G=0,J=0,K=0,L=0,M=0,N=0,O=0,P=0,Q=0,R=0,S=0,T=0,U=0,V=0,W=0,X=0,Y=0,Z=0,_=0;a:{b:{if((e|0)!=2){break b}H[a+8>>2]=2;H[a- -64>>2]=f;M=a+32|0;e=H[M>>2];d=H[a+36>>2]-e|0;c:{if(d>>>0<=7){ya(M,2-(d>>>2|0)|0);break c}if((d|0)==8){break c}H[a+36>>2]=e+8}p=1;d=H[a+56>>2];d=H[d+4>>2]-H[d>>2]|0;if((d|0)<=0){break b}o=a+60|0;d=d>>>2|0;X=d>>>0<=1?1:d;Y=a+68|0;d=0;while(1){e=H[a+56>>2];h=H[e>>2];if(H[e+4>>2]-h>>2>>>0<=d>>>0){break a}k=ca-80|0;ca=k;f=-1;h=H[h+(d<<2)>>2];e=-1;d:{if((h|0)==-1){break d}e=h+1|0;f=(e>>>0)%3|0?e:h-2|0;e=h-1|0;if((h>>>0)%3|0){break d}e=h+2|0}g=H[o+36>>2];h=H[g>>2];e:{f:{g:{h:{i:{g=H[g+4>>2]-h>>2;i=f<<2;f=H[H[o+32>>2]+28>>2];j=H[i+f>>2];if(g>>>0<=j>>>0){break i}e=H[f+(e<<2)>>2];if(e>>>0>=g>>>0){break i}j:{k:{l=H[h+(e<<2)>>2];f=H[h+(j<<2)>>2];if((l|0)>=(d|0)|(f|0)>=(d|0)){break k}h=(l<<3)+c|0;w=H[h+4>>2];g=(f<<3)+c|0;e=H[g+4>>2];j=H[h>>2];h=H[g>>2];if(!((j|0)!=(h|0)|(e|0)!=(w|0))){H[o+8>>2]=h;H[o+12>>2]=e;break j}p=H[H[o+4>>2]+(d<<2)>>2];H[k+72>>2]=0;H[k+76>>2]=0;g=k- -64|0;H[g>>2]=0;H[g+4>>2]=0;H[k+56>>2]=0;H[k+60>>2]=0;g=H[o>>2];if(!I[g+84|0]){p=H[H[g+68>>2]+(p<<2)>>2]}Sa(g,p,F[g+24|0],k+56|0);p=H[H[o+4>>2]+(f<<2)>>2];H[k+48>>2]=0;H[k+52>>2]=0;H[k+40>>2]=0;H[k+44>>2]=0;H[k+32>>2]=0;H[k+36>>2]=0;g=H[o>>2];if(!I[g+84|0]){p=H[H[g+68>>2]+(p<<2)>>2]}Sa(g,p,F[g+24|0],k+32|0);p=H[H[o+4>>2]+(l<<2)>>2];H[k+24>>2]=0;H[k+28>>2]=0;H[k+16>>2]=0;H[k+20>>2]=0;H[k+8>>2]=0;H[k+12>>2]=0;g=H[o>>2];if(!I[g+84|0]){p=H[H[g+68>>2]+(p<<2)>>2]}Sa(g,p,F[g+24|0],k+8|0);g=H[k+16>>2];n=H[k+40>>2];x=g-n|0;N=H[k+44>>2];g=H[k+20>>2]-(N+(g>>>0>>0)|0)|0;E=g;l=Rj(x,g,x,g);q=da;g=H[k+8>>2];z=H[k+32>>2];A=g-z|0;O=H[k+36>>2];g=H[k+12>>2]-(O+(g>>>0>>0)|0)|0;G=g;i=l;l=Rj(A,g,A,g);g=i+l|0;i=da+q|0;i=g>>>0>>0?i+1|0:i;l=H[k+24>>2];B=H[k+48>>2];C=l-B|0;P=H[k+52>>2];l=H[k+28>>2]-(P+(l>>>0>>0)|0)|0;J=l;m=g;g=Rj(C,l,C,l);r=m+g|0;i=da+i|0;s=g>>>0>r>>>0?i+1|0:i;if(!(s|r)){break k}p=0;D=Tj(-1,2147483647,r,s);f=h>>31;R=f;i=f>>31;Q=h;g=i;q=h^g;h=q-g|0;f=(f^g)-((g>>>0>q>>>0)+g|0)|0;g=f;f=e>>31;S=f;K=e;e=f>>31;q=K^e;m=q-e|0;i=f>>31;e=(i^f)-((e>>>0>q>>>0)+i|0)|0;f=(g|0)==(e|0)&h>>>0>m>>>0|e>>>0>>0;h=f?h:m;l=da;e=f?g:e;if((l|0)==(e|0)&h>>>0>D>>>0|e>>>0>l>>>0){break e}h=H[k+64>>2];T=H[k+68>>2];e=Rj(h-n|0,T-((h>>>0>>0)+N|0)|0,x,E);f=da;g=H[k+56>>2];U=H[k+60>>2];l=Rj(g-z|0,U-((g>>>0>>0)+O|0)|0,A,G);e=l+e|0;i=da+f|0;i=e>>>0>>0?i+1|0:i;f=e;m=H[k+72>>2];V=H[k+76>>2];e=Rj(m-B|0,V-((m>>>0>>0)+P|0)|0,C,J);l=f+e|0;f=da+i|0;q=e>>>0>l>>>0?f+1|0:f;e=j;D=e-Q|0;e=(e>>31)-((e>>>0>>0)+R|0)|0;W=e;j=e>>31;y=j^D;f=y-j|0;i=e>>31;e=(i^e)-((j>>>0>y>>>0)+i|0)|0;i=e;y=w-K|0;e=(w>>31)-((w>>>0>>0)+S|0)|0;w=e;j=f;t=e>>31;u=t^y;L=u-t|0;f=e>>31;e=(f^e)-((t>>>0>u>>>0)+f|0)|0;f=(i|0)==(e|0)&j>>>0>L>>>0|e>>>0>>0;f=Tj(-1,2147483647,f?j:L,f?i:e)>>>0>>0;e=da;if(f&(e|0)<=(q|0)|(e|0)<(q|0)){break e}e=G>>31;f=e;j=e^A;e=j-e|0;f=(f^G)-((f>>>0>j>>>0)+f|0)|0;i=E>>31;t=i^x;u=t-i|0;j=(i^E)-((i>>>0>t>>>0)+i|0)|0;i=(f|0)==(j|0)&e>>>0>u>>>0|f>>>0>j>>>0;e=i?e:u;f=i?f:j;i=J>>31;L=e;t=i^C;u=t-i|0;j=(i^J)-((i>>>0>t>>>0)+i|0)|0;e=(f|0)==(j|0)&e>>>0>u>>>0|f>>>0>j>>>0;f=Tj(-1,2147483647,e?L:u,e?f:j)>>>0>>0;e=da;if(f&(e|0)<=(q|0)|(e|0)<(q|0)){break e}j=1;e=0;f=n;n=Sj(Rj(l,q,x,E),da,r,s);f=f+n|0;i=da+N|0;i=f>>>0>>0?i+1|0:i;n=h-f|0;f=T-((f>>>0>h>>>0)+i|0)|0;n=Rj(n,f,n,f);x=da;f=g;i=Sj(Rj(l,q,A,G),da,r,s);h=i+z|0;g=da+O|0;g=h>>>0>>0?g+1|0:g;i=f-h|0;f=U-((f>>>0>>0)+g|0)|0;g=Rj(i,f,i,f);h=g+n|0;f=da+x|0;f=h>>>0>>0?f+1|0:f;n=h;g=Sj(Rj(l,q,C,J),da,r,s);h=g+B|0;i=da+P|0;i=h>>>0>>0?i+1|0:i;g=m-h|0;h=V-((h>>>0>m>>>0)+i|0)|0;m=Rj(g,h,g,h);h=m+n|0;g=da+f|0;f=Rj(h,h>>>0>>0?g+1|0:g,r,s);h=da;m=h;if(!h&f>>>0<=1){break h}i=f;while(1){g=e<<1|j>>>31;j=j<<1;e=g;n=!h&i>>>0>7|(h|0)!=0;i=(h&3)<<30|i>>>2;h=h>>>2|0;if(n){continue}break}break g}if((d|0)>(f|0)){e=f<<1}else{if((d|0)<=0){H[o+8>>2]=0;H[o+12>>2]=0;break j}e=(d<<1)-2|0}e=(e<<2)+c|0;H[o+8>>2]=H[e>>2];H[o+12>>2]=H[e+4>>2]}p=1;break e}Ca();v()}e=m;j=f;if(f-1|0){break f}}while(1){h=Tj(f,m,j,e);i=e+da|0;e=h+j|0;i=e>>>0>>0?i+1|0:i;j=(i&1)<<31|e>>>1;e=i>>>1|0;h=Rj(j,e,j,e);g=da;if((m|0)==(g|0)&f>>>0>>0|g>>>0>m>>>0){continue}break}}f=H[o+20>>2];if(!f){break e}g=f-1|0;i=H[H[o+16>>2]+(g>>>3&536870908)>>2];H[o+20>>2]=g;p=1;f=Rj(l,q,y,w);h=da;n=Rj(r,s,K,S);m=n+f|0;f=da+h|0;f=m>>>0>>0?f+1|0:f;h=Rj(j,e,D,W);g=i>>>g&1;i=g?0-h|0:h;m=i+m|0;n=f;f=da;h=n+(g?0-(f+((h|0)!=0)|0)|0:f)|0;Z=o,_=Sj(m,i>>>0>m>>>0?h+1|0:h,r,s),H[Z+12>>2]=_;f=Rj(l,q,D,W);h=da;l=Rj(r,s,Q,R);f=l+f|0;i=da+h|0;e=Rj(j,e,y,w);h=0-e|0;j=da;i=(f>>>0>>0?i+1|0:i)+(g?j:0-(((e|0)!=0)+j|0)|0)|0;h=g?e:h;f=h+f|0;Z=o,_=Sj(f,f>>>0>>0?i+1|0:i,r,s),H[Z+8>>2]=_}ca=k+80|0;if(!p){return 0}l:{if(H[a+8>>2]<=0){break l}g=H[M>>2];e=0;while(1){f=e<<2;h=H[f+Y>>2];j=H[a+16>>2];m:{if((h|0)>(j|0)){H[f+g>>2]=j;break m}f=f+g|0;j=H[a+12>>2];if((j|0)>(h|0)){H[f>>2]=j;break m}H[f>>2]=h}e=e+1|0;h=H[a+8>>2];if((e|0)<(h|0)){continue}break}f=0;if((h|0)<=0){break l}e=d<<3;j=e+c|0;l=b+e|0;while(1){h=f<<2;e=h+j|0;h=H[h+l>>2]+H[h+g>>2]|0;H[e>>2]=h;n:{if((h|0)>H[a+16>>2]){i=h-H[a+20>>2]|0}else{if((h|0)>=H[a+12>>2]){break n}i=h+H[a+20>>2]|0}H[e>>2]=i}f=f+1|0;if((f|0)>2]){continue}break}}d=d+1|0;if((X|0)!=(d|0)){continue}break}}return p|0}Ca();v()}function $a(a,b){var c=0,d=0,e=0,f=0,g=0;e=ca-16|0;ca=e;H[a+12>>2]=b;H[a+8>>2]=0;H[a>>2]=0;H[a+4>>2]=0;d=a+16|0;H[d>>2]=0;H[d+4>>2]=0;F[d+5|0]=0;F[d+6|0]=0;F[d+7|0]=0;F[d+8|0]=0;F[d+9|0]=0;F[d+10|0]=0;F[d+11|0]=0;F[d+12|0]=0;c=d+16|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+32|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+48|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d- -64|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+80|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+96|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+112|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+128|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+144|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+160|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+176|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+192|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+208|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+224|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+240|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+256|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+272|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+288|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+304|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+320|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+336|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+352|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+368|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+384|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+400|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+416|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+432|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+448|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+464|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c=d+480|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;d=d+496|0;H[d>>2]=0;H[d+4>>2]=0;F[d+5|0]=0;F[d+6|0]=0;F[d+7|0]=0;F[d+8|0]=0;F[d+9|0]=0;F[d+10|0]=0;F[d+11|0]=0;F[d+12|0]=0;H[a+528>>2]=0;H[a+532>>2]=0;F[a+533|0]=0;F[a+534|0]=0;F[a+535|0]=0;F[a+536|0]=0;F[a+537|0]=0;F[a+538|0]=0;F[a+539|0]=0;F[a+540|0]=0;H[a+544>>2]=0;H[a+548>>2]=0;H[a+560>>2]=0;H[a+552>>2]=0;H[a+556>>2]=0;H[a+564>>2]=0;H[a+568>>2]=0;H[a+580>>2]=0;H[a+572>>2]=0;H[a+576>>2]=0;H[a+584>>2]=0;H[a+588>>2]=0;H[a+600>>2]=0;H[a+592>>2]=0;H[a+596>>2]=0;H[a+612>>2]=0;H[a+604>>2]=0;H[a+608>>2]=0;g=a+628|0;a:{b:{if(b){if(b>>>0<1073741824){break b}sa();v()}H[a+616>>2]=0;H[a+620>>2]=0;H[a+624>>2]=0;H[e+8>>2]=0;H[e>>2]=0;H[e+4>>2]=0;d=1;break a}d=b<<2;c=pa(d);H[a+604>>2]=c;f=c+d|0;H[a+612>>2]=f;ra(c,0,d);H[a+624>>2]=0;H[a+616>>2]=0;H[a+620>>2]=0;H[a+608>>2]=f;c=pa(d);H[a+616>>2]=c;f=c+d|0;H[a+624>>2]=f;ra(c,0,d);H[a+620>>2]=f;c=pa(d);H[e>>2]=c;f=c+d|0;H[e+8>>2]=f;ra(c,0,d);H[e+4>>2]=f;d=b<<5|1}tb(g,d,e);c=H[e>>2];if(c){H[e+4>>2]=c;oa(c)}H[e+8>>2]=0;H[e>>2]=0;H[e+4>>2]=0;if(b){b=b<<2;c=pa(b);H[e>>2]=c;f=b+c|0;H[e+8>>2]=f;ra(c,0,b);H[e+4>>2]=f}tb(a+640|0,d,e);b=H[e>>2];if(b){H[e+4>>2]=b;oa(b)}ca=e+16|0;return a}function gc(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=O(0),n=O(0),o=0;a:{b:{if(!d){break b}c:{switch(H[a+28>>2]-1|0){case 0:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}G[(g<<1)+d>>1]=F[b|0];b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}j=1;if(e>>>0>=f>>>0){break b}break a;case 1:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}G[(g<<1)+d>>1]=I[b|0];b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}j=1;if(e>>>0>=f>>>0){break b}break a;case 2:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}G[(g<<1)+d>>1]=J[b>>1];b=b+2|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}j=1;if(e>>>0>=f>>>0){break b}break a;case 3:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){return 0}e=G[b>>1];if((e|0)<0){break b}G[(g<<1)+d>>1]=e;b=b+2|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}j=1;if(e>>>0>=f>>>0){break b}break a;case 4:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}e=H[b>>2];if(e+32768>>>0>65535){break b}G[(g<<1)+d>>1]=e;b=b+4|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}j=1;if(e>>>0>=f>>>0){break b}break a;case 5:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}e=H[b>>2];if(e>>>0>32767){break b}G[(g<<1)+d>>1]=e;b=b+4|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}j=1;if(e>>>0>=f>>>0){break b}break a;case 6:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;k=H[e+4>>2];while(1){if(b>>>0>=k>>>0){break b}h=H[b+4>>2];e=H[b>>2];i=e+32768|0;h=i>>>0<32768?h+1|0:h;if(!h&i>>>0>65535|h){break b}G[(g<<1)+d>>1]=e;b=b+8|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}j=1;if(e>>>0>=f>>>0){break b}break a;case 7:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}k=H[b+4>>2];e=H[b>>2];if(!k&e>>>0>32767|k){break b}G[(g<<1)+d>>1]=e;b=b+8|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}j=1;if(e>>>0>=f>>>0){break b}break a;case 8:d:{e:{e=I[a+24|0];c=c&255;if(!(c>>>0>e>>>0?e:c)){break e}e=H[a>>2];j=H[e>>2];g=j;f=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+f|0;g=b+g|0;f=H[e+4>>2];e=f-j|0;if(!I[a+32|0]){j=0;if((b|0)>=(e|0)){break d}b=0;while(1){m=L[g>>2];if(m>=O(32767)|m>1]=i;b=b+1|0;e=I[a+24|0];if(b>>>0>=(c>>>0>e>>>0?e:c)>>>0){break e}g=g+4|0;if(f>>>0>g>>>0){continue}break}break d}j=0;if((b|0)>=(e|0)){break d}b=0;while(1){m=L[g>>2];if(m>=O(32767)|mO(1)){break d}e=(b<<1)+d|0;l=T(+m*32767+.5);f:{if(P(l)<2147483648){i=~~l;break f}i=-2147483648}G[e>>1]=i;b=b+1|0;e=I[a+24|0];if(b>>>0>=(c>>>0>e>>>0?e:c)>>>0){break e}g=g+4|0;if(f>>>0>g>>>0){continue}break}break d}j=1;if(c>>>0<=e>>>0){break d}ra((e<<1)+d|0,0,c-e<<1)}return j;case 9:g:{h:{e=I[a+24|0];c=c&255;if(!(c>>>0>e>>>0?e:c)){break h}e=H[a>>2];j=H[e>>2];g=j;f=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+f|0;g=b+g|0;f=H[e+4>>2];e=f-j|0;if(!I[a+32|0]){j=0;if((b|0)>=(e|0)){break g}b=0;while(1){l=M[g>>3];if(l>=32767|l<-32768|l!=l){break g}o=P(l);if(o==Infinity){break g}e=(b<<1)+d|0;if(o<2147483648){i=~~l}else{i=-2147483648}G[e>>1]=i;b=b+1|0;e=I[a+24|0];if(b>>>0>=(c>>>0>e>>>0?e:c)>>>0){break h}g=g+8|0;if(f>>>0>g>>>0){continue}break}break g}j=0;if((b|0)>=(e|0)){break g}b=0;while(1){l=M[g>>3];if(l>=32767|l<-32768|(P(l)==Infinity|l!=l)){break g}if(l<0|l>1){break g}e=(b<<1)+d|0;l=T(l*32767+.5);i:{if(P(l)<2147483648){i=~~l;break i}i=-2147483648}G[e>>1]=i;b=b+1|0;e=I[a+24|0];if(b>>>0>=(c>>>0>e>>>0?e:c)>>>0){break h}g=g+8|0;if(f>>>0>g>>>0){continue}break}break g}j=1;if(c>>>0<=e>>>0){break g}ra((e<<1)+d|0,0,c-e<<1)}return j;case 10:break c;default:break b}}e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}G[(g<<1)+d>>1]=I[b|0];b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}j=1;if(e>>>0>=f>>>0){break b}ra((e<<1)+d|0,0,(c&255)-e<<1)}return j}ra((e<<1)+d|0,0,(c&255)-e<<1);return 1}function ec(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=O(0),n=O(0),o=0;a:{b:{if(!d){break b}c:{switch(H[a+28>>2]-1|0){case 0:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}H[(g<<2)+d>>2]=F[b|0];b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}j=1;if(e>>>0>=f>>>0){break b}break a;case 1:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}H[(g<<2)+d>>2]=I[b|0];b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}j=1;if(e>>>0>=f>>>0){break b}break a;case 2:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}H[(g<<2)+d>>2]=G[b>>1];b=b+2|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}j=1;if(e>>>0>=f>>>0){break b}break a;case 3:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}H[(g<<2)+d>>2]=J[b>>1];b=b+2|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}j=1;if(e>>>0>=f>>>0){break b}break a;case 4:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}H[(g<<2)+d>>2]=H[b>>2];b=b+4|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}j=1;if(e>>>0>=f>>>0){break b}break a;case 5:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){return 0}e=H[b>>2];if((e|0)<0){break b}H[(g<<2)+d>>2]=e;b=b+4|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}j=1;if(e>>>0>=f>>>0){break b}break a;case 6:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;k=H[e+4>>2];while(1){if(b>>>0>=k>>>0){break b}h=H[b+4>>2];e=H[b>>2];if(e- -2147483648>>>0<2147483648?h+1|0:h){break b}H[(g<<2)+d>>2]=e;b=b+8|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}j=1;if(e>>>0>=f>>>0){break b}break a;case 7:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}k=H[b+4>>2];e=H[b>>2];if(!k&e>>>0>2147483647|k){break b}H[(g<<2)+d>>2]=e;b=b+8|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}j=1;if(e>>>0>=f>>>0){break b}break a;case 8:d:{e:{e=I[a+24|0];c=c&255;if(!(c>>>0>e>>>0?e:c)){break e}e=H[a>>2];j=H[e>>2];g=j;f=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+f|0;g=b+g|0;f=H[e+4>>2];e=f-j|0;if(!I[a+32|0]){j=0;if((b|0)>=(e|0)){break d}b=0;while(1){m=L[g>>2];if(m>=O(2147483648)|m>2]=i;b=b+1|0;e=I[a+24|0];if(b>>>0>=(c>>>0>e>>>0?e:c)>>>0){break e}g=g+4|0;if(f>>>0>g>>>0){continue}break}break d}j=0;if((b|0)>=(e|0)){break d}b=0;while(1){m=L[g>>2];if(m>=O(2147483648)|mO(1)){break d}e=(b<<2)+d|0;l=T(+m*2147483647+.5);f:{if(P(l)<2147483648){i=~~l;break f}i=-2147483648}H[e>>2]=i;b=b+1|0;e=I[a+24|0];if(b>>>0>=(c>>>0>e>>>0?e:c)>>>0){break e}g=g+4|0;if(f>>>0>g>>>0){continue}break}break d}j=1;if(c>>>0<=e>>>0){break d}ra((e<<2)+d|0,0,c-e<<2)}return j;case 9:g:{h:{e=I[a+24|0];c=c&255;if(!(c>>>0>e>>>0?e:c)){break h}e=H[a>>2];j=H[e>>2];g=j;f=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+f|0;g=b+g|0;f=H[e+4>>2];e=f-j|0;if(!I[a+32|0]){j=0;if((b|0)>=(e|0)){break g}b=0;while(1){l=M[g>>3];if(l>=2147483647|l<-2147483648|l!=l){break g}o=P(l);if(o==Infinity){break g}e=(b<<2)+d|0;if(o<2147483648){i=~~l}else{i=-2147483648}H[e>>2]=i;b=b+1|0;e=I[a+24|0];if(b>>>0>=(c>>>0>e>>>0?e:c)>>>0){break h}g=g+8|0;if(f>>>0>g>>>0){continue}break}break g}j=0;if((b|0)>=(e|0)){break g}b=0;while(1){l=M[g>>3];if(l>=2147483647|l<-2147483648|(P(l)==Infinity|l!=l)){break g}if(l<0|l>1){break g}e=(b<<2)+d|0;l=T(l*2147483647+.5);i:{if(P(l)<2147483648){i=~~l;break i}i=-2147483648}H[e>>2]=i;b=b+1|0;e=I[a+24|0];if(b>>>0>=(c>>>0>e>>>0?e:c)>>>0){break h}g=g+8|0;if(f>>>0>g>>>0){continue}break}break g}j=1;if(c>>>0<=e>>>0){break g}ra((e<<2)+d|0,0,c-e<<2)}return j;case 10:break c;default:break b}}e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}H[(g<<2)+d>>2]=I[b|0];b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}j=1;if(e>>>0>=f>>>0){break b}ra((e<<2)+d|0,0,(c&255)-e<<2)}return j}ra((e<<2)+d|0,0,(c&255)-e<<2);return 1}function fc(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=O(0);a:{b:{if(!d){break b}c:{switch(H[a+28>>2]-1|0){case 0:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){return 0}e=F[b|0];if((e|0)<0){break b}G[(g<<1)+d>>1]=e&255;b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}l=1;if(e>>>0>=f>>>0){break b}break a;case 1:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}G[(g<<1)+d>>1]=I[b|0];b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}l=1;if(e>>>0>=f>>>0){break b}break a;case 2:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){return 0}e=G[b>>1];if((e|0)<0){break b}G[(g<<1)+d>>1]=e;b=b+2|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}l=1;if(e>>>0>=f>>>0){break b}break a;case 3:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}G[(g<<1)+d>>1]=J[b>>1];b=b+2|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}l=1;if(e>>>0>=f>>>0){break b}break a;case 4:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}e=H[b>>2];if(e>>>0>65535){break b}G[(g<<1)+d>>1]=e;b=b+4|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}l=1;if(e>>>0>=f>>>0){break b}break a;case 5:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}e=H[b>>2];if(e>>>0>65535){break b}G[(g<<1)+d>>1]=e;b=b+4|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}l=1;if(e>>>0>=f>>>0){break b}break a;case 6:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}k=H[b+4>>2];e=H[b>>2];if(!k&e>>>0>65535|k){break b}G[(g<<1)+d>>1]=e;b=b+8|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}l=1;if(e>>>0>=f>>>0){break b}break a;case 7:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}k=H[b+4>>2];e=H[b>>2];if(!k&e>>>0>65535|k){break b}G[(g<<1)+d>>1]=e;b=b+8|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}l=1;if(e>>>0>=f>>>0){break b}break a;case 8:d:{e:{e=I[a+24|0];c=c&255;if(!(c>>>0>e>>>0?e:c)){break e}e=H[a>>2];l=H[e>>2];g=l;f=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+f|0;g=b+g|0;f=H[e+4>>2];e=f-l|0;if(!I[a+32|0]){l=0;if((b|0)>=(e|0)){break d}b=0;while(1){m=L[g>>2];if(m>=O(65535)|m=O(0)){i=~~m>>>0}else{i=0}G[e>>1]=i;b=b+1|0;e=I[a+24|0];if(b>>>0>=(c>>>0>e>>>0?e:c)>>>0){break e}g=g+4|0;if(f>>>0>g>>>0){continue}break}break d}l=0;if((b|0)>=(e|0)){break d}b=0;while(1){m=L[g>>2];if(m>=O(65535)|mO(1)){break d}e=(b<<1)+d|0;j=T(+m*65535+.5);f:{if(j<4294967296&j>=0){i=~~j>>>0;break f}i=0}G[e>>1]=i;b=b+1|0;e=I[a+24|0];if(b>>>0>=(c>>>0>e>>>0?e:c)>>>0){break e}g=g+4|0;if(f>>>0>g>>>0){continue}break}break d}l=1;if(c>>>0<=e>>>0){break d}ra((e<<1)+d|0,0,c-e<<1)}return l;case 9:g:{h:{e=I[a+24|0];c=c&255;if(!(c>>>0>e>>>0?e:c)){break h}e=H[a>>2];l=H[e>>2];g=l;f=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+f|0;g=b+g|0;f=H[e+4>>2];e=f-l|0;if(!I[a+32|0]){l=0;if((b|0)>=(e|0)){break g}b=0;while(1){j=M[g>>3];if(j>=65535|j<0|(P(j)==Infinity|j!=j)){break g}e=(b<<1)+d|0;if(j<4294967296&j>=0){i=~~j>>>0}else{i=0}G[e>>1]=i;b=b+1|0;e=I[a+24|0];if(b>>>0>=(c>>>0>e>>>0?e:c)>>>0){break h}g=g+8|0;if(f>>>0>g>>>0){continue}break}break g}l=0;if((b|0)>=(e|0)){break g}b=0;while(1){j=M[g>>3];if(j>=65535|j<0|(P(j)==Infinity|j!=j)){break g}if(j>1){break g}e=(b<<1)+d|0;j=T(j*65535+.5);i:{if(j<4294967296&j>=0){i=~~j>>>0;break i}i=0}G[e>>1]=i;b=b+1|0;e=I[a+24|0];if(b>>>0>=(c>>>0>e>>>0?e:c)>>>0){break h}g=g+8|0;if(f>>>0>g>>>0){continue}break}break g}l=1;if(c>>>0<=e>>>0){break g}ra((e<<1)+d|0,0,c-e<<1)}return l;case 10:break c;default:break b}}e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];k=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+k|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}G[(g<<1)+d>>1]=I[b|0];b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}l=1;if(e>>>0>=f>>>0){break b}ra((e<<1)+d|0,0,(c&255)-e<<1)}return l}ra((e<<1)+d|0,0,(c&255)-e<<1);return 1}function Sa(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0,k=O(0),l=0,m=0,n=O(0),o=0;a:{if(!d){break a}b:{c:{switch(H[a+28>>2]-1|0){case 0:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);j=b;b=b+i|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break a}e=(g<<3)+d|0;i=F[b|0];H[e>>2]=i;H[e+4>>2]=i>>31;b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}if(e>>>0>=f>>>0){break a}d=(e<<3)+d|0;a=(c&255)-e|0;break b;case 1:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);j=b;b=b+i|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break a}e=(g<<3)+d|0;H[e>>2]=I[b|0];H[e+4>>2]=0;b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}if(e>>>0>=f>>>0){break a}d=(e<<3)+d|0;a=(c&255)-e|0;break b;case 2:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);j=b;b=b+i|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break a}e=(g<<3)+d|0;i=G[b>>1];H[e>>2]=i;H[e+4>>2]=i>>31;b=b+2|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}if(e>>>0>=f>>>0){break a}d=(e<<3)+d|0;a=(c&255)-e|0;break b;case 3:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);j=b;b=b+i|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break a}e=(g<<3)+d|0;H[e>>2]=J[b>>1];H[e+4>>2]=0;b=b+2|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}if(e>>>0>=f>>>0){break a}d=(e<<3)+d|0;a=(c&255)-e|0;break b;case 4:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);j=b;b=b+i|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break a}e=(g<<3)+d|0;i=H[b>>2];H[e>>2]=i;H[e+4>>2]=i>>31;b=b+4|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}if(e>>>0>=f>>>0){break a}d=(e<<3)+d|0;a=(c&255)-e|0;break b;case 5:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);j=b;b=b+i|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break a}e=(g<<3)+d|0;H[e>>2]=H[b>>2];H[e+4>>2]=0;b=b+4|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}if(e>>>0>=f>>>0){break a}d=(e<<3)+d|0;a=(c&255)-e|0;break b;case 6:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);j=b;b=b+i|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break a}i=H[b+4>>2];e=(g<<3)+d|0;H[e>>2]=H[b>>2];H[e+4>>2]=i;b=b+8|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}if(e>>>0>=f>>>0){break a}d=(e<<3)+d|0;a=(c&255)-e|0;break b;case 7:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);j=b;b=b+i|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break a}e=H[b>>2];i=H[b+4>>2];if((i|0)<0){break a}j=(g<<3)+d|0;H[j>>2]=e;H[j+4>>2]=i;b=b+8|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}if(e>>>0>=f>>>0){break a}d=(e<<3)+d|0;a=(c&255)-e|0;break b;case 8:d:{e=I[a+24|0];f=c&255;if(!(e>>>0>>0?e:f)){break d}if(I[a+32|0]){break a}e=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);j=b;b=b+e|0;e=H[a>>2];i=H[e+4>>2];e=H[e>>2];if((b|0)>=(i-e|0)){break a}g=b+e|0;h=c&255;b=0;while(1){k=L[g>>2];if(k>=O(0x8000000000000000)|k=O(1)?~~(k>O(0)?O(R(O(T(O(k*O(2.3283064365386963e-10)))),O(4294967296))):O(U(O(O(k-O(~~k>>>0>>>0))*O(2.3283064365386963e-10)))))>>>0:0;m=~~k>>>0;break e}j=-2147483648;m=0}H[e>>2]=m;H[e+4>>2]=j;b=b+1|0;e=I[a+24|0];if(b>>>0>=(e>>>0>>0?e:h)>>>0){break d}g=g+4|0;if(i>>>0>g>>>0){continue}break}break a}if(e>>>0>=f>>>0){break a}d=(e<<3)+d|0;a=(c&255)-e|0;break b;case 9:f:{e=I[a+24|0];f=c&255;if(!(e>>>0>>0?e:f)){break f}if(I[a+32|0]){break a}e=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);j=b;b=b+e|0;e=H[a>>2];i=H[e+4>>2];e=H[e>>2];if((b|0)>=(i-e|0)){break a}g=b+e|0;h=c&255;b=0;while(1){l=M[g>>3];if(l>=0x8000000000000000|l<-0x8000000000000000|l!=l){break a}o=P(l);if(o==Infinity){break a}e=(b<<3)+d|0;g:{if(o<0x8000000000000000){j=P(l)>=1?~~(l>0?R(T(l*2.3283064365386963e-10),4294967295):U((l-+(~~l>>>0>>>0))*2.3283064365386963e-10))>>>0:0;m=~~l>>>0;break g}j=-2147483648;m=0}H[e>>2]=m;H[e+4>>2]=j;b=b+1|0;e=I[a+24|0];if(b>>>0>=(e>>>0>>0?e:h)>>>0){break f}g=g+8|0;if(i>>>0>g>>>0){continue}break}break a}if(e>>>0>=f>>>0){break a}d=(e<<3)+d|0;a=(c&255)-e|0;break b;case 10:break c;default:break a}}e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);j=b;b=b+i|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break a}e=(g<<3)+d|0;H[e>>2]=I[b|0];H[e+4>>2]=0;b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}if(e>>>0>=f>>>0){break a}d=(e<<3)+d|0;a=(c&255)-e|0}ra(d,0,a<<3)}}function Oj(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0;j=a;a:{b:{c:{d:{e:{f:{g:{h:{a=H[a+8>>2];switch(H[a+28>>2]-1|0){case 4:break c;case 5:break d;case 2:break e;case 3:break f;case 0:break g;case 1:break h;default:break a}}f=I[a+24|0];c=pa(f);a=H[j+16>>2];if(H[a+80>>2]){g=H[H[a>>2]>>2]+H[a+48>>2]|0}else{g=0}if(!b){break b}if(f){o=f&252;l=f&3;h=f>>>0<4;while(1){a=0;e=0;if(!h){while(1){k=g+(d<<2)|0;F[a+c|0]=H[k>>2];F[(a|1)+c|0]=H[k+4>>2];F[(a|2)+c|0]=H[k+8>>2];F[(a|3)+c|0]=H[k+12>>2];a=a+4|0;d=d+4|0;e=e+4|0;if((o|0)!=(e|0)){continue}break}}e=0;if(l){while(1){F[a+c|0]=H[g+(d<<2)>>2];a=a+1|0;d=d+1|0;e=e+1|0;if((l|0)!=(e|0)){continue}break}}qa(H[H[H[j+8>>2]+64>>2]>>2]+m|0,c,f);m=f+m|0;n=n+1|0;if((n|0)!=(b|0)){continue}break}break b}a=0;if((b|0)!=1){g=b&-2;while(1){qa(H[H[H[j+8>>2]+64>>2]>>2]+a|0,c,f);a=a+f|0;qa(a+H[H[H[j+8>>2]+64>>2]>>2]|0,c,f);a=a+f|0;d=d+2|0;if((g|0)!=(d|0)){continue}break}}if(!(b&1)){break b}qa(H[H[H[j+8>>2]+64>>2]>>2]+a|0,c,f);break b}f=I[a+24|0];c=pa(f);a=H[j+16>>2];if(H[a+80>>2]){g=H[H[a>>2]>>2]+H[a+48>>2]|0}else{g=0}if(!b){break b}if(f){o=f&252;l=f&3;h=f>>>0<4;while(1){a=0;e=0;if(!h){while(1){k=g+(d<<2)|0;F[a+c|0]=H[k>>2];F[(a|1)+c|0]=H[k+4>>2];F[(a|2)+c|0]=H[k+8>>2];F[(a|3)+c|0]=H[k+12>>2];a=a+4|0;d=d+4|0;e=e+4|0;if((o|0)!=(e|0)){continue}break}}e=0;if(l){while(1){F[a+c|0]=H[g+(d<<2)>>2];a=a+1|0;d=d+1|0;e=e+1|0;if((l|0)!=(e|0)){continue}break}}qa(H[H[H[j+8>>2]+64>>2]>>2]+m|0,c,f);m=f+m|0;n=n+1|0;if((n|0)!=(b|0)){continue}break}break b}a=0;if((b|0)!=1){g=b&-2;while(1){qa(H[H[H[j+8>>2]+64>>2]>>2]+a|0,c,f);a=a+f|0;qa(a+H[H[H[j+8>>2]+64>>2]>>2]|0,c,f);a=a+f|0;d=d+2|0;if((g|0)!=(d|0)){continue}break}}if(!(b&1)){break b}qa(H[H[H[j+8>>2]+64>>2]>>2]+a|0,c,f);break b}h=I[a+24|0];i=h<<1;c=pa(i);a=H[j+16>>2];if(H[a+80>>2]){g=H[H[a>>2]>>2]+H[a+48>>2]|0}else{g=0}if(!b){break b}if(h){o=h&252;l=h&3;h=h>>>0<4;while(1){a=0;e=0;if(!h){while(1){f=a<<1;k=g+(d<<2)|0;G[f+c>>1]=H[k>>2];G[(f|2)+c>>1]=H[k+4>>2];G[(f|4)+c>>1]=H[k+8>>2];G[(f|6)+c>>1]=H[k+12>>2];a=a+4|0;d=d+4|0;e=e+4|0;if((o|0)!=(e|0)){continue}break}}e=0;if(l){while(1){G[(a<<1)+c>>1]=H[g+(d<<2)>>2];a=a+1|0;d=d+1|0;e=e+1|0;if((l|0)!=(e|0)){continue}break}}qa(H[H[H[j+8>>2]+64>>2]>>2]+n|0,c,i);n=i+n|0;m=m+1|0;if((m|0)!=(b|0)){continue}break}break b}a=0;if((b|0)!=1){g=b&-2;while(1){qa(H[H[H[j+8>>2]+64>>2]>>2]+a|0,c,i);a=a+i|0;qa(a+H[H[H[j+8>>2]+64>>2]>>2]|0,c,i);a=a+i|0;d=d+2|0;if((g|0)!=(d|0)){continue}break}}if(!(b&1)){break b}qa(H[H[H[j+8>>2]+64>>2]>>2]+a|0,c,i);break b}h=I[a+24|0];i=h<<1;c=pa(i);a=H[j+16>>2];if(H[a+80>>2]){g=H[H[a>>2]>>2]+H[a+48>>2]|0}else{g=0}if(!b){break b}if(h){o=h&252;l=h&3;h=h>>>0<4;while(1){a=0;e=0;if(!h){while(1){f=a<<1;k=g+(d<<2)|0;G[f+c>>1]=H[k>>2];G[(f|2)+c>>1]=H[k+4>>2];G[(f|4)+c>>1]=H[k+8>>2];G[(f|6)+c>>1]=H[k+12>>2];a=a+4|0;d=d+4|0;e=e+4|0;if((o|0)!=(e|0)){continue}break}}e=0;if(l){while(1){G[(a<<1)+c>>1]=H[g+(d<<2)>>2];a=a+1|0;d=d+1|0;e=e+1|0;if((l|0)!=(e|0)){continue}break}}qa(H[H[H[j+8>>2]+64>>2]>>2]+n|0,c,i);n=i+n|0;m=m+1|0;if((m|0)!=(b|0)){continue}break}break b}a=0;if((b|0)!=1){g=b&-2;while(1){qa(H[H[H[j+8>>2]+64>>2]>>2]+a|0,c,i);a=a+i|0;qa(a+H[H[H[j+8>>2]+64>>2]>>2]|0,c,i);a=a+i|0;d=d+2|0;if((g|0)!=(d|0)){continue}break}}if(!(b&1)){break b}qa(H[H[H[j+8>>2]+64>>2]>>2]+a|0,c,i);break b}h=I[a+24|0];i=h<<2;c=pa(i);a=H[j+16>>2];if(H[a+80>>2]){g=H[H[a>>2]>>2]+H[a+48>>2]|0}else{g=0}if(!b){break b}if(h){o=h&252;l=h&3;h=h>>>0<4;while(1){a=0;e=0;if(!h){while(1){f=a<<2;k=g+(d<<2)|0;H[f+c>>2]=H[k>>2];H[(f|4)+c>>2]=H[k+4>>2];H[(f|8)+c>>2]=H[k+8>>2];H[(f|12)+c>>2]=H[k+12>>2];a=a+4|0;d=d+4|0;e=e+4|0;if((o|0)!=(e|0)){continue}break}}e=0;if(l){while(1){H[(a<<2)+c>>2]=H[g+(d<<2)>>2];a=a+1|0;d=d+1|0;e=e+1|0;if((l|0)!=(e|0)){continue}break}}qa(H[H[H[j+8>>2]+64>>2]>>2]+n|0,c,i);n=i+n|0;m=m+1|0;if((m|0)!=(b|0)){continue}break}break b}a=0;if((b|0)!=1){g=b&-2;while(1){qa(H[H[H[j+8>>2]+64>>2]>>2]+a|0,c,i);a=a+i|0;qa(a+H[H[H[j+8>>2]+64>>2]>>2]|0,c,i);a=a+i|0;d=d+2|0;if((g|0)!=(d|0)){continue}break}}if(!(b&1)){break b}qa(H[H[H[j+8>>2]+64>>2]>>2]+a|0,c,i);break b}h=I[a+24|0];i=h<<2;c=pa(i);a=H[j+16>>2];if(H[a+80>>2]){g=H[H[a>>2]>>2]+H[a+48>>2]|0}else{g=0}if(!b){break b}if(h){o=h&252;l=h&3;h=h>>>0<4;while(1){a=0;e=0;if(!h){while(1){f=a<<2;k=g+(d<<2)|0;H[f+c>>2]=H[k>>2];H[(f|4)+c>>2]=H[k+4>>2];H[(f|8)+c>>2]=H[k+8>>2];H[(f|12)+c>>2]=H[k+12>>2];a=a+4|0;d=d+4|0;e=e+4|0;if((o|0)!=(e|0)){continue}break}}e=0;if(l){while(1){H[(a<<2)+c>>2]=H[g+(d<<2)>>2];a=a+1|0;d=d+1|0;e=e+1|0;if((l|0)!=(e|0)){continue}break}}qa(H[H[H[j+8>>2]+64>>2]>>2]+n|0,c,i);n=i+n|0;m=m+1|0;if((m|0)!=(b|0)){continue}break}break b}a=0;if((b|0)!=1){g=b&-2;while(1){qa(H[H[H[j+8>>2]+64>>2]>>2]+a|0,c,i);a=a+i|0;qa(a+H[H[H[j+8>>2]+64>>2]>>2]|0,c,i);a=a+i|0;d=d+2|0;if((g|0)!=(d|0)){continue}break}}if(!(b&1)){break b}qa(H[H[H[j+8>>2]+64>>2]>>2]+a|0,c,i)}oa(c);c=1}return c|0}function dc(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=O(0);a:{b:{if(!d){break b}c:{switch(H[a+28>>2]-1|0){case 0:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];l=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+l|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}H[(g<<2)+d>>2]=F[b|0];b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 1:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];l=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+l|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}H[(g<<2)+d>>2]=I[b|0];b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 2:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];l=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+l|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}H[(g<<2)+d>>2]=G[b>>1];b=b+2|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 3:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];l=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+l|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}H[(g<<2)+d>>2]=J[b>>1];b=b+2|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 4:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];l=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+l|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}H[(g<<2)+d>>2]=H[b>>2];b=b+4|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 5:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];l=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+l|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}H[(g<<2)+d>>2]=H[b>>2];b=b+4|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 6:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];l=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+l|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}e=H[b>>2];if(H[b+4>>2]){break b}H[(g<<2)+d>>2]=e;b=b+8|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 7:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];l=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+l|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}e=H[b>>2];if(H[b+4>>2]){break b}H[(g<<2)+d>>2]=e;b=b+8|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 8:d:{e:{e=I[a+24|0];c=c&255;if(!(c>>>0>e>>>0?e:c)){break e}e=H[a>>2];k=H[e>>2];g=k;f=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+f|0;g=b+g|0;f=H[e+4>>2];e=f-k|0;if(!I[a+32|0]){k=0;if((b|0)>=(e|0)){break d}b=0;while(1){m=L[g>>2];if(m>=O(4294967296)|m=O(0)){i=~~m>>>0}else{i=0}H[e>>2]=i;b=b+1|0;e=I[a+24|0];if(b>>>0>=(c>>>0>e>>>0?e:c)>>>0){break e}g=g+4|0;if(f>>>0>g>>>0){continue}break}break d}k=0;if((b|0)>=(e|0)){break d}b=0;while(1){m=L[g>>2];if(m>=O(4294967296)|mO(1)){break d}e=(b<<2)+d|0;j=T(+m*4294967295+.5);f:{if(j<4294967296&j>=0){i=~~j>>>0;break f}i=0}H[e>>2]=i;b=b+1|0;e=I[a+24|0];if(b>>>0>=(c>>>0>e>>>0?e:c)>>>0){break e}g=g+4|0;if(f>>>0>g>>>0){continue}break}break d}k=1;if(c>>>0<=e>>>0){break d}ra((e<<2)+d|0,0,c-e<<2)}return k;case 9:g:{h:{e=I[a+24|0];c=c&255;if(!(c>>>0>e>>>0?e:c)){break h}e=H[a>>2];k=H[e>>2];g=k;f=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+f|0;g=b+g|0;f=H[e+4>>2];e=f-k|0;if(!I[a+32|0]){k=0;if((b|0)>=(e|0)){break g}b=0;while(1){j=M[g>>3];if(j>=4294967295|j<0|(P(j)==Infinity|j!=j)){break g}e=(b<<2)+d|0;if(j<4294967296&j>=0){i=~~j>>>0}else{i=0}H[e>>2]=i;b=b+1|0;e=I[a+24|0];if(b>>>0>=(c>>>0>e>>>0?e:c)>>>0){break h}g=g+8|0;if(f>>>0>g>>>0){continue}break}break g}k=0;if((b|0)>=(e|0)){break g}b=0;while(1){j=M[g>>3];if(j>=4294967295|j<0|(P(j)==Infinity|j!=j)){break g}if(j>1){break g}e=(b<<2)+d|0;j=T(j*4294967295+.5);i:{if(j<4294967296&j>=0){i=~~j>>>0;break i}i=0}H[e>>2]=i;b=b+1|0;e=I[a+24|0];if(b>>>0>=(c>>>0>e>>>0?e:c)>>>0){break h}g=g+8|0;if(f>>>0>g>>>0){continue}break}break g}k=1;if(c>>>0<=e>>>0){break g}ra((e<<2)+d|0,0,c-e<<2)}return k;case 10:break c;default:break b}}e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];h=H[e>>2];l=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);i=b;b=b+l|0;b=b+h|0;h=H[e+4>>2];while(1){if(b>>>0>=h>>>0){break b}H[(g<<2)+d>>2]=I[b|0];b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}ra((e<<2)+d|0,0,(c&255)-e<<2)}return k}ra((e<<2)+d|0,0,(c&255)-e<<2);return 1}function ye(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0;a:{b:{c:{d:{e:{if(H[a+92>>2]==H[a+88>>2]){break e}c=H[a+52>>2];f:{if((c|0)!=H[a+56>>2]){H[c>>2]=b;H[a+52>>2]=c+4;break f}h=H[a+48>>2];g=c-h|0;d=g>>2;f=d+1|0;if(f>>>0>=1073741824){break a}e=g>>>1|0;g=g>>>0>=2147483644?1073741823:f>>>0>>0?e:f;if(g){if(g>>>0>=1073741824){break d}e=pa(g<<2)}else{e=0}f=e+(d<<2)|0;H[f>>2]=b;d=f+4|0;if((c|0)!=(h|0)){while(1){f=f-4|0;c=c-4|0;H[f>>2]=H[c>>2];if((c|0)!=(h|0)){continue}break}}H[a+56>>2]=e+(g<<2);H[a+52>>2]=d;H[a+48>>2]=f;if(!h){break f}oa(h)}H[a+84>>2]=0;c=-1;e=-1;g:{if((b|0)==-1){break g}d=H[a+4>>2];e=b+1|0;e=(e>>>0)%3|0?e:b-2|0;if((e|0)!=-1){c=H[H[d>>2]+(e<<2)>>2]}h:{if((b>>>0)%3|0){l=b-1|0;break h}l=b+2|0;e=-1;if((l|0)==-1){break g}}e=H[H[d>>2]+(l<<2)>>2]}i=e>>>3&536870908;d=H[a+36>>2];h=d+(c>>>3&536870908)|0;g=H[h>>2];f=1<>2]=f|g;f=a+8|0;if((b|0)!=-1){d=b+1|0;d=(d>>>0)%3|0?d:b-2|0}else{d=-1}Ua(f,c,d);d=H[a+36>>2]}f=d+i|0;d=H[f>>2];c=1<>2]=c|d;d=a+8|0;c=-1;i:{if((b|0)==-1){break i}c=b-1|0;if((b>>>0)%3|0){break i}c=b+2|0}Ua(d,e,c)}c=-1;c=(b|0)!=-1?H[H[H[a+4>>2]>>2]+(b<<2)>>2]:c;f=H[a+36>>2]+(c>>>3&536870908)|0;d=H[f>>2];e=1<>2]=d|e;Ua(a+8|0,c,b)}d=H[a+84>>2];if((d|0)>2){break e}while(1){e=N(d,12)+a|0;b=H[e+52>>2];if((b|0)==H[e+48>>2]){d=d+1|0;if((d|0)!=3){continue}break e}b=b-4|0;c=H[b>>2];H[e+52>>2]=b;H[a+84>>2]=d;if((c|0)==-1){break e}f=H[a+24>>2];b=(c>>>0)/3|0;j:{if(H[f+(b>>>3&268435452)>>2]>>>b&1){break j}k:{while(1){k=(c>>>0)/3|0;b=(k>>>3&268435452)+f|0;H[b>>2]=H[b>>2]|1<>2]>>2]+(c<<2)>>2]:d;f=H[a+36>>2]+(d>>>3&536870908)|0;e=H[f>>2];b=1<>2]=b|e;i=H[(H[H[a+16>>2]+96>>2]+N(k,12)|0)+((c>>>0)%3<<2)>>2];l=H[H[a+20>>2]+4>>2];f=H[l+4>>2];t:{if((f|0)!=H[l+8>>2]){H[f>>2]=i;H[l+4>>2]=f+4;break t}j=H[l>>2];h=f-j|0;g=h>>2;e=g+1|0;if(e>>>0>=1073741824){break s}b=h>>>1|0;h=h>>>0>=2147483644?1073741823:b>>>0>e>>>0?b:e;if(h){if(h>>>0>=1073741824){break d}e=pa(h<<2)}else{e=0}b=e+(g<<2)|0;H[b>>2]=i;g=b+4|0;if((f|0)!=(j|0)){while(1){b=b-4|0;f=f-4|0;H[b>>2]=H[f>>2];if((f|0)!=(j|0)){continue}break}}H[l+8>>2]=e+(h<<2);H[l+4>>2]=g;H[l>>2]=b;if(!j){break t}oa(j)}j=H[a+12>>2];f=H[j+4>>2];u:{if((f|0)!=H[j+8>>2]){H[f>>2]=c;H[j+4>>2]=f+4;break u}i=H[j>>2];h=f-i|0;g=h>>2;e=g+1|0;if(e>>>0>=1073741824){break r}b=h>>>1|0;h=h>>>0>=2147483644?1073741823:b>>>0>e>>>0?b:e;if(h){if(h>>>0>=1073741824){break d}e=pa(h<<2)}else{e=0}b=e+(g<<2)|0;H[b>>2]=c;g=b+4|0;if((f|0)!=(i|0)){while(1){b=b-4|0;f=f-4|0;H[b>>2]=H[f>>2];if((f|0)!=(i|0)){continue}break}}H[j+8>>2]=e+(h<<2);H[j+4>>2]=g;H[j>>2]=b;if(!i){break u}oa(i)}b=H[a+12>>2];H[H[b+12>>2]+(d<<2)>>2]=H[b+24>>2];H[b+24>>2]=H[b+24>>2]+1}if((c|0)==-1){break k}g=H[a+4>>2];f=-1;b=c+1|0;b=(b>>>0)%3|0?b:c-2|0;if((b|0)!=-1){f=H[H[g+12>>2]+(b<<2)>>2]}v:{w:{if((N(k,3)|0)!=(c|0)){d=c-1|0;break w}d=c+2|0;c=-1;if((d|0)==-1){break v}}c=H[H[g+12>>2]+(d<<2)>>2]}d=(c|0)==-1;e=(c>>>0)/3|0;if((f|0)!=-1){b=(f>>>0)/3|0;b=H[H[a+24>>2]+(b>>>3&268435452)>>2]&1<>2]+(b>>>3&536870908)>>2]>>>b&1){break x}k=0;b=H[H[g>>2]+(c<<2)>>2];if(!(H[H[a+36>>2]+(b>>>3&536870908)>>2]>>>b&1)){b=H[a+88>>2]+(b<<2)|0;e=H[b>>2];H[b>>2]=e+1;k=(e|0)<=0?2:1}if(H[a+84>>2]>=(k|0)&l){break m}j=N(k,12)+a|0;b=H[j+52>>2];y:{if((b|0)!=H[j+56>>2]){H[b>>2]=c;H[j+52>>2]=b+4;break y}i=H[j+48>>2];h=b-i|0;d=h>>2;g=d+1|0;if(g>>>0>=1073741824){break c}e=h>>>1|0;g=h>>>0>=2147483644?1073741823:e>>>0>g>>>0?e:g;if(g){if(g>>>0>=1073741824){break d}e=pa(g<<2)}else{e=0}d=e+(d<<2)|0;H[d>>2]=c;c=d+4|0;if((b|0)!=(i|0)){while(1){d=d-4|0;b=b-4|0;H[d>>2]=H[b>>2];if((b|0)!=(i|0)){continue}break}}H[j+48>>2]=d;H[j+52>>2]=c;H[j+56>>2]=e+(g<<2);if(!i){break y}oa(i)}if(H[a+84>>2]<=(k|0)){break x}H[a+84>>2]=k}if(l){break k}c=-1;if((f|0)==-1){break n}}c=H[H[H[a+4>>2]>>2]+(f<<2)>>2]}b=0;if(!(H[H[a+36>>2]+(c>>>3&536870908)>>2]>>>c&1)){b=H[a+88>>2]+(c<<2)|0;c=H[b>>2];H[b>>2]=c+1;b=(c|0)<=0?2:1}if(H[a+84>>2]<(b|0)){break l}c=f}f=H[a+24>>2];continue}break}k=N(b,12)+a|0;c=H[k+52>>2];z:{if((c|0)!=H[k+56>>2]){H[c>>2]=f;H[k+52>>2]=c+4;break z}i=H[k+48>>2];h=c-i|0;d=h>>2;g=d+1|0;if(g>>>0>=1073741824){break b}e=h>>>1|0;g=h>>>0>=2147483644?1073741823:e>>>0>g>>>0?e:g;if(g){if(g>>>0>=1073741824){break d}e=pa(g<<2)}else{e=0}d=e+(d<<2)|0;H[d>>2]=f;f=d+4|0;if((c|0)!=(i|0)){while(1){d=d-4|0;c=c-4|0;H[d>>2]=H[c>>2];if((c|0)!=(i|0)){continue}break}}H[k+48>>2]=d;H[k+52>>2]=f;H[k+56>>2]=e+(g<<2);if(!i){break z}oa(i)}d=H[a+84>>2];if((d|0)<=(b|0)){break j}H[a+84>>2]=b;d=b;break j}d=H[a+84>>2]}if((d|0)<3){continue}break}}return 1}wa();v()}sa();v()}sa();v()}sa();v()}function gd(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0;n=ca-96|0;ca=n;o=H[a+4>>2];d=H[o+32>>2];i=H[d+8>>2];j=H[d+12>>2];e=j;c=H[d+20>>2];f=H[d+16>>2];a:{if((e|0)<=(c|0)&f>>>0>=i>>>0|(c|0)>(e|0)){break a}p=H[d>>2];g=I[p+f|0];h=f+1|0;e=h?c:c+1|0;H[d+16>>2]=h;H[d+20>>2]=e;if((e|0)>=(j|0)&h>>>0>=i>>>0|(e|0)>(j|0)){break a}m=I[h+p|0];h=f+2|0;e=h>>>0<2?c+1|0:c;H[d+16>>2]=h;H[d+20>>2]=e;l=g<<24>>24;b:{if((l|0)>=0){k=H[a+216>>2];if(g>>>0>=(H[a+220>>2]-k|0)/144>>>0){break a}k=k+N(g,144)|0;if(H[k>>2]<0){break b}break a}if(H[a+212>>2]>=0){break a}k=a+212|0}H[k>>2]=b;c:{d:{e:{f:{g:{h:{k=J[o+36>>1];i:{if(((k<<8|k>>>8)&65535)>>>0>=258){if((e|0)>=(j|0)&h>>>0>=i>>>0|(e|0)>(j|0)){break a}e=I[h+p|0];f=f+3|0;c=f>>>0<3?c+1|0:c;H[d+16>>2]=f;H[d+20>>2]=c;if(e>>>0>1){break a}d=e>>>0<2?e:0;if(!m){break i}if(!d){break h}break a}if(m){break g}d=0}if((l|0)<0){e=a+184|0}else{c=H[a+216>>2]+N(g,144)|0;F[c+100|0]=0;e=c+104|0}if((d|0)!=1){break e}c=ca-112|0;ca=c;h=H[H[a+4>>2]+44>>2];d=pa(120);H[d>>2]=12172;H[d+4>>2]=0;H[d+116>>2]=0;H[d+112>>2]=e;H[d+108>>2]=h;H[d+12>>2]=0;H[d+16>>2]=0;H[d+20>>2]=0;H[d+24>>2]=0;H[d+28>>2]=0;H[d+32>>2]=0;H[d+36>>2]=0;H[d+40>>2]=0;H[d+44>>2]=0;H[d+48>>2]=0;H[d+52>>2]=0;H[d+56>>2]=0;H[d+60>>2]=0;H[d+8>>2]=12384;f=d- -64|0;H[f>>2]=0;H[f+4>>2]=0;H[d+72>>2]=0;H[d+76>>2]=0;H[d+80>>2]=0;H[d+84>>2]=0;H[d+88>>2]=0;H[d+104>>2]=0;H[d+96>>2]=0;H[d+100>>2]=0;f=H[a+8>>2];H[c+48>>2]=0;H[c+52>>2]=0;H[c+40>>2]=0;H[c+44>>2]=0;i=c+32|0;H[i>>2]=0;H[i+4>>2]=0;H[c+24>>2]=0;H[c+28>>2]=0;g=c- -64|0;H[g>>2]=0;H[g+4>>2]=0;H[c+72>>2]=0;H[c+76>>2]=0;H[c+80>>2]=0;H[c+84>>2]=0;H[c+88>>2]=0;H[c+104>>2]=0;H[c+16>>2]=0;H[c+20>>2]=0;H[c+56>>2]=0;H[c+60>>2]=0;H[c+8>>2]=12384;H[c+96>>2]=0;H[c+100>>2]=0;H[c+12>>2]=f;g=H[f>>2];j=H[f+4>>2];F[c+111|0]=0;m=i;i=c+111|0;Oa(m,(j-g>>2>>>0)/3|0,i);g=H[c+12>>2];j=H[g+28>>2];g=H[g+24>>2];F[c+111|0]=0;Oa(c+44|0,j-g>>2,i);H[c+28>>2]=d;H[c+24>>2]=h;H[c+20>>2]=e;H[c+16>>2]=f;f=d+8|0;e=c+8|0;fd(f,e);j:{if((e|0)==(f|0)){H[d+92>>2]=H[e+84>>2];break j}Cb(d+56|0,H[e+48>>2],H[e+52>>2]);Cb(d+68|0,H[e+60>>2],H[e- -64>>2]);Cb(d+80|0,H[e+72>>2],H[e+76>>2]);H[d+92>>2]=H[e+84>>2];Aa(d+96|0,H[e+88>>2],H[e+92>>2])}H[c+8>>2]=12384;e=H[c+96>>2];if(e){H[c+100>>2]=e;oa(e)}e=H[c+80>>2];if(e){H[c+84>>2]=e;oa(e)}e=H[c+68>>2];if(e){H[c+72>>2]=e;oa(e)}e=H[c+56>>2];if(e){H[c+60>>2]=e;oa(e)}H[c+8>>2]=12620;e=H[c+44>>2];if(e){oa(e)}e=H[c+32>>2];if(e){oa(e)}ca=c+112|0;break d}if((l|0)>=0){break f}break a}if((l|0)<0){break a}}e=H[a+216>>2];c=H[o+44>>2];d=pa(80);H[d>>2]=12932;H[d+4>>2]=0;H[d+76>>2]=0;H[d+68>>2]=c;H[d+8>>2]=11872;H[d+12>>2]=0;H[d+16>>2]=0;H[d+20>>2]=0;H[d+24>>2]=0;H[d+28>>2]=0;H[d+32>>2]=0;H[d+36>>2]=0;H[d+40>>2]=0;H[d+44>>2]=0;H[d+48>>2]=0;H[d+52>>2]=0;e=e+N(g,144)|0;f=e+104|0;H[d+72>>2]=f;H[d- -64>>2]=0;H[d+56>>2]=0;H[d+60>>2]=0;H[n+24>>2]=c;c=n;H[c+68>>2]=0;H[c+72>>2]=0;H[c+60>>2]=0;H[c+64>>2]=0;H[c+52>>2]=0;H[c+56>>2]=0;H[c+44>>2]=0;H[c+48>>2]=0;H[c+84>>2]=0;H[c+88>>2]=0;H[c+76>>2]=0;H[c+80>>2]=0;H[c+28>>2]=d;h=H[c+28>>2];H[c+8>>2]=H[c+24>>2];H[c+12>>2]=h;H[c+20>>2]=f;f=e+4|0;H[c+16>>2]=f;H[c+36>>2]=0;H[c+40>>2]=0;H[c+32>>2]=11872;e=H[c+20>>2];H[c>>2]=H[c+16>>2];H[c+4>>2]=e;e=c+32|0;Ie(e,f,c);c=d+8|0;fd(c,e);if((c|0)!=(e|0)){Cb(d+56|0,H[e+48>>2],H[e+52>>2])}He(e);break c}c=ca+-64|0;ca=c;h=H[H[a+4>>2]+44>>2];d=pa(80);H[d>>2]=12640;H[d+4>>2]=0;H[d+76>>2]=0;H[d+72>>2]=e;H[d+68>>2]=h;H[d+8>>2]=12804;H[d+12>>2]=0;H[d+16>>2]=0;H[d+20>>2]=0;H[d+24>>2]=0;H[d+28>>2]=0;H[d+32>>2]=0;H[d+36>>2]=0;H[d+40>>2]=0;H[d+44>>2]=0;H[d+48>>2]=0;H[d+52>>2]=0;H[d- -64>>2]=0;i=d+56|0;f=i;H[f>>2]=0;H[f+4>>2]=0;f=H[a+8>>2];H[c+40>>2]=0;H[c+44>>2]=0;H[c+32>>2]=0;H[c+36>>2]=0;g=c+24|0;H[g>>2]=0;H[g+4>>2]=0;H[c+16>>2]=0;H[c+20>>2]=0;H[c+56>>2]=0;H[c+8>>2]=0;H[c+12>>2]=0;H[c+48>>2]=0;H[c+52>>2]=0;H[c>>2]=12804;H[c+4>>2]=f;j=H[f>>2];l=H[f+4>>2];F[c+63|0]=0;m=g;g=c+63|0;Oa(m,(l-j>>2>>>0)/3|0,g);j=H[c+4>>2];l=H[j+28>>2];j=H[j+24>>2];F[c+63|0]=0;Oa(c+36|0,l-j>>2,g);H[c+20>>2]=d;H[c+16>>2]=h;H[c+12>>2]=e;H[c+8>>2]=f;fd(d+8|0,c);Cb(i,H[c+48>>2],H[c+52>>2]);H[c>>2]=12804;e=H[c+48>>2];if(e){H[c+52>>2]=e;oa(e)}H[c>>2]=12620;e=H[c+36>>2];if(e){oa(e)}e=H[c+24>>2];if(e){oa(e)}ca=c- -64|0}if(!d){break a}}d=od(pa(64),d);c=H[a+4>>2];a=d;d=b;k:{l:{if((d|0)>=0){h=c+8|0;b=H[c+12>>2];i=H[c+8>>2];e=b-i>>2;m:{if((e|0)>(d|0)){break m}f=d+1|0;if(d>>>0>=e>>>0){Vb(h,f-e|0);break m}if(e>>>0<=f>>>0){break m}f=i+(f<<2)|0;if((f|0)!=(b|0)){while(1){b=b-4|0;e=H[b>>2];H[b>>2]=0;if(e){ea[H[H[e>>2]+4>>2]](e)}if((b|0)!=(f|0)){continue}break}}H[c+12>>2]=f}c=H[h>>2]+(d<<2)|0;b=H[c>>2];H[c>>2]=a;if(b){break l}break k}b=a;if(!a){break k}}ea[H[H[b>>2]+4>>2]](b)}q=(d^-1)>>>31|0}ca=n+96|0;return q|0}function Kd(a){var b=0,c=0,d=0,e=0,f=0,g=0;e=ca-16|0;ca=e;H[e+12>>2]=a;a:{if(a>>>0<=211){d=H[Jd(14256,14448,e+12|0)>>2];break a}if(a>>>0>=4294967292){X();v()}f=(a>>>0)/210|0;d=N(f,210);H[e+8>>2]=a-d;g=Jd(14448,14640,e+8|0)-14448>>2;while(1){d=H[(g<<2)+14448>>2]+d|0;a=5;while(1){b:{if((a|0)==47){a=211;while(1){b=(d>>>0)/(a>>>0)|0;if(b>>>0>>0){break a}if((N(a,b)|0)==(d|0)){break b}b=a+10|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+12|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+16|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+18|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+22|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+28|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+30|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+36|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+40|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+42|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+46|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+52|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+58|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+60|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+66|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+70|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+72|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+78|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+82|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+88|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+96|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+100|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+102|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+106|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+108|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+112|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+120|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+126|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+130|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+136|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+138|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+142|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+148|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+150|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+156|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+162|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+166|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+168|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+172|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+178|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+180|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+186|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+190|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+192|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+196|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+198|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}if((N(b,c)|0)==(d|0)){break b}b=a+208|0;c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}a=a+210|0;if((N(b,c)|0)!=(d|0)){continue}break}break b}b=H[(a<<2)+14256>>2];c=(d>>>0)/(b>>>0)|0;if(b>>>0>c>>>0){break a}a=a+1|0;if((N(b,c)|0)!=(d|0)){continue}}break}d=g+1|0;a=(d|0)==48;g=a?0:d;f=a+f|0;d=N(f,210);continue}}ca=e+16|0;return d}function Ib(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0;j=ca-16|0;ca=j;a:{b:{c:{d:{if(I[H[a+4>>2]+36|0]<=1){k=-1;c=H[b+20>>2];d=H[b+16>>2];e=d+4|0;c=e>>>0<4?c+1|0:c;g=H[b+12>>2];if(K[b+8>>2]>>0&(g|0)<=(c|0)|(c|0)>(g|0)){break c}d=d+H[b>>2]|0;l=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[j+12>>2]=l;H[b+16>>2]=e;H[b+20>>2]=c;break d}k=-1;if(!Ea(1,j+12|0,b)){break c}l=H[j+12>>2]}e:{f:{g:{h:{i:{if(!l){break i}c=H[a+8>>2];if((H[c+4>>2]-H[c>>2]>>2>>>0)/3>>>0>>0){break c}c=J[H[a+4>>2]+36>>1];if(((c<<8|c>>>8)&65535)>>>0>=258){j:{while(1){if(!Ea(1,j+8|0,b)){break c}c=H[j+8>>2];if(!Ea(1,j+8|0,b)){break c}f=c+f|0;c=H[j+8>>2];if(f>>>0>>0){break c}g=f-c|0;c=H[a+40>>2];k:{if((c|0)!=H[a+44>>2]){H[c+4>>2]=f;H[c>>2]=g;H[a+40>>2]=c+12;l=H[j+12>>2];break k}m=H[a+36>>2];d=c-m|0;o=(d|0)/12|0;e=o+1|0;if(e>>>0>=357913942){break j}c=o<<1;h=o>>>0>=178956970?357913941:c>>>0>e>>>0?c:e;if(h){if(h>>>0>=357913942){break b}i=pa(N(h,12))}else{i=0}e=i+N(o,12)|0;H[e+4>>2]=f;H[e>>2]=g;c=va(e+N((d|0)/-12|0,12)|0,m,d);H[a+44>>2]=i+N(h,12);H[a+40>>2]=e+12;H[a+36>>2]=c;if(!m){break k}oa(m)}p=p+1|0;if(l>>>0>p>>>0){continue}break}k=0;Db(b,0,0);if(l){while(1){e=I[b+36|0];c=J[H[a+4>>2]+36>>1];l:{m:{if(((c<<8|c>>>8)&65535)>>>0<=513){if(!e){break l}p=0;c=H[b+32>>2];n=c>>>3|0;g=H[b+24>>2];e=n+g|0;d=H[b+28>>2];n:{if(e>>>0>=d>>>0){f=c;break n}e=I[e|0];f=c+1|0;H[b+32>>2]=f;n=f>>>3|0;p=e>>>(c&7)&1}if(d>>>0>g+n>>>0){break m}break l}if(!e){break l}p=0;f=H[b+32>>2];c=H[b+24>>2]+(f>>>3|0)|0;if(c>>>0>=K[b+28>>2]){break l}p=I[c|0]>>>(f&7)&1}H[b+32>>2]=f+1}c=H[a+36>>2]+N(k,12)|0;F[c+8|0]=I[c+8|0]&254|p&1;k=k+1|0;if((k|0)!=(l|0)){continue}break}}F[b+36|0]=0;f=H[b+20>>2];e=0;d=H[b+32>>2]+7|0;e=d>>>0<7?1:e;c=e>>>3|0;e=(e&7)<<29|d>>>3;d=e+H[b+16>>2]|0;c=c+f|0;H[b+16>>2]=d;H[b+20>>2]=d>>>0>>0?c+1|0:c;break i}sa();v()}while(1){d=H[b+8>>2];c=H[b+12>>2];g=c;c=H[b+20>>2];e=c;h=H[b+16>>2];f=h+4|0;c=f>>>0<4?c+1|0:c;i=f;if(f>>>0>d>>>0&(c|0)>=(g|0)|(c|0)>(g|0)){break c}m=H[b>>2];f=m+h|0;o=I[f|0]|I[f+1|0]<<8|(I[f+2|0]<<16|I[f+3|0]<<24);H[b+16>>2]=i;H[b+20>>2]=c;c=e;f=h+8|0;c=f>>>0<8?c+1|0:c;if(d>>>0>>0&(c|0)>=(g|0)|(c|0)>(g|0)){break c}i=i+m|0;i=I[i|0]|I[i+1|0]<<8|(I[i+2|0]<<16|I[i+3|0]<<24);H[b+16>>2]=f;H[b+20>>2]=c;if(d>>>0<=f>>>0&(c|0)>=(g|0)|(c|0)>(g|0)){break c}d=I[f+m|0];c=h+9|0;e=c>>>0<9?e+1|0:e;H[b+16>>2]=c;H[b+20>>2]=e;f=d&1;c=H[a+40>>2];o:{if((c|0)!=H[a+44>>2]){F[c+8|0]=f;H[c+4>>2]=i;H[c>>2]=o;H[a+40>>2]=c+12;l=H[j+12>>2];break o}m=H[a+36>>2];d=c-m|0;h=(d|0)/12|0;e=h+1|0;if(e>>>0>=357913942){break h}c=h<<1;g=h>>>0>=178956970?357913941:c>>>0>e>>>0?c:e;if(g){if(g>>>0>=357913942){break b}e=pa(N(g,12))}else{e=0}h=e+N(h,12)|0;F[h+8|0]=f;H[h+4>>2]=i;H[h>>2]=o;c=va(h+N((d|0)/-12|0,12)|0,m,d);H[a+44>>2]=e+N(g,12);H[a+40>>2]=h+12;H[a+36>>2]=c;if(!m){break o}oa(m)}n=n+1|0;if(l>>>0>n>>>0){continue}break}}H[j+8>>2]=0;c=J[H[a+4>>2]+36>>1];c=(c<<8|c>>>8)&65535;p:{if(c>>>0<=511){k=-1;c=H[b+20>>2];d=H[b+16>>2];e=d+4|0;c=e>>>0<4?c+1|0:c;f=H[b+12>>2];if(K[b+8>>2]>>0&(f|0)<=(c|0)|(c|0)>(f|0)){break c}d=d+H[b>>2]|0;f=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[b+16>>2]=e;H[b+20>>2]=c;break p}if((c|0)!=512){break e}k=-1;if(!Ea(1,j+8|0,b)){break c}f=H[j+8>>2]}if(!f){break e}c=J[H[a+4>>2]+36>>1];if(((c<<8|c>>>8)&65535)>>>0<258){break f}n=0;l=0;while(1){if(!Ea(1,j+4|0,b)){break c}l=H[j+4>>2]+l|0;c=H[a+52>>2];q:{if((c|0)!=H[a+56>>2]){H[c>>2]=l;H[a+52>>2]=c+4;break q}i=H[a+48>>2];g=c-i|0;e=g>>2;d=e+1|0;if(d>>>0>=1073741824){break g}c=g>>>1|0;d=g>>>0>=2147483644?1073741823:c>>>0>d>>>0?c:d;if(d){if(d>>>0>=1073741824){break b}c=pa(d<<2)}else{c=0}e=c+(e<<2)|0;H[e>>2]=l;c=va(c,i,g);H[a+56>>2]=c+(d<<2);H[a+52>>2]=e+4;H[a+48>>2]=c;if(!i){break q}oa(i)}n=n+1|0;if((n|0)!=(f|0)){continue}break}break e}sa();v()}sa();v()}k=0;while(1){c=H[b+20>>2];d=H[b+16>>2];e=d+4|0;c=e>>>0<4?c+1|0:c;g=H[b+12>>2];if(K[b+8>>2]>>0&(g|0)<=(c|0)|(c|0)>(g|0)){k=-1;break c}d=d+H[b>>2]|0;g=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[b+16>>2]=e;H[b+20>>2]=c;c=H[a+52>>2];r:{if((c|0)!=H[a+56>>2]){H[c>>2]=g;H[a+52>>2]=c+4;break r}h=H[a+48>>2];i=c-h|0;e=i>>2;d=e+1|0;if(d>>>0>=1073741824){break a}c=i>>>1|0;d=i>>>0>=2147483644?1073741823:c>>>0>d>>>0?c:d;if(d){if(d>>>0>=1073741824){break b}c=pa(d<<2)}else{c=0}e=c+(e<<2)|0;H[e>>2]=g;c=va(c,h,i);H[a+56>>2]=c+(d<<2);H[a+52>>2]=e+4;H[a+48>>2]=c;if(!h){break r}oa(h)}k=k+1|0;if((k|0)!=(f|0)){continue}break}}k=H[b+16>>2]}ca=j+16|0;return k}wa();v()}sa();v()}function Va(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=O(0),k=0,l=0;a:{if(!d){break a}b:{c:{switch(H[a+28>>2]-1|0){case 0:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];g=H[e>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);k=b;b=b+i|0;b=b+g|0;g=H[e+4>>2];i=I[a+32|0];while(1){if(b>>>0>=g>>>0){break a}j=O(F[b|0]);L[(h<<2)+d>>2]=i?O(j/O(127)):j;b=b+1|0;h=h+1|0;e=I[a+24|0];if(h>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}l=1;if(e>>>0>=f>>>0){break a}d=(e<<2)+d|0;a=(c&255)-e|0;break b;case 1:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];g=H[e>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);k=b;b=b+i|0;b=b+g|0;g=H[e+4>>2];i=I[a+32|0];while(1){if(b>>>0>=g>>>0){break a}j=O(I[b|0]);L[(h<<2)+d>>2]=i?O(j/O(255)):j;b=b+1|0;h=h+1|0;e=I[a+24|0];if(h>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}l=1;if(e>>>0>=f>>>0){break a}d=(e<<2)+d|0;a=(c&255)-e|0;break b;case 2:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];g=H[e>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);k=b;b=b+i|0;b=b+g|0;g=H[e+4>>2];i=I[a+32|0];while(1){if(b>>>0>=g>>>0){break a}j=O(G[b>>1]);L[(h<<2)+d>>2]=i?O(j/O(32767)):j;b=b+2|0;h=h+1|0;e=I[a+24|0];if(h>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}l=1;if(e>>>0>=f>>>0){break a}d=(e<<2)+d|0;a=(c&255)-e|0;break b;case 3:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];g=H[e>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);k=b;b=b+i|0;b=b+g|0;g=H[e+4>>2];i=I[a+32|0];while(1){if(b>>>0>=g>>>0){break a}j=O(J[b>>1]);L[(h<<2)+d>>2]=i?O(j/O(65535)):j;b=b+2|0;h=h+1|0;e=I[a+24|0];if(h>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}l=1;if(e>>>0>=f>>>0){break a}d=(e<<2)+d|0;a=(c&255)-e|0;break b;case 4:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];g=H[e>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);k=b;b=b+i|0;b=b+g|0;g=H[e+4>>2];i=I[a+32|0];while(1){if(b>>>0>=g>>>0){break a}j=O(H[b>>2]);L[(h<<2)+d>>2]=i?O(j*O(4.656612873077393e-10)):j;b=b+4|0;h=h+1|0;e=I[a+24|0];if(h>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}l=1;if(e>>>0>=f>>>0){break a}d=(e<<2)+d|0;a=(c&255)-e|0;break b;case 5:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];g=H[e>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);k=b;b=b+i|0;b=b+g|0;g=H[e+4>>2];i=I[a+32|0];while(1){if(b>>>0>=g>>>0){break a}j=O(K[b>>2]);L[(h<<2)+d>>2]=i?O(j*O(2.3283064365386963e-10)):j;b=b+4|0;h=h+1|0;e=I[a+24|0];if(h>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}l=1;if(e>>>0>=f>>>0){break a}d=(e<<2)+d|0;a=(c&255)-e|0;break b;case 6:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];g=H[e>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);k=b;b=b+i|0;b=b+g|0;g=H[e+4>>2];i=I[a+32|0];while(1){if(b>>>0>=g>>>0){break a}j=O(+K[b>>2]+ +H[b+4>>2]*4294967296);L[(h<<2)+d>>2]=i?O(j*O(10842021724855044e-35)):j;b=b+8|0;h=h+1|0;e=I[a+24|0];if(h>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}l=1;if(e>>>0>=f>>>0){break a}d=(e<<2)+d|0;a=(c&255)-e|0;break b;case 7:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];g=H[e>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);k=b;b=b+i|0;b=b+g|0;g=H[e+4>>2];i=I[a+32|0];while(1){if(b>>>0>=g>>>0){break a}j=O(+K[b>>2]+ +K[b+4>>2]*4294967296);L[(h<<2)+d>>2]=i?O(j*O(5.421010862427522e-20)):j;b=b+8|0;h=h+1|0;e=I[a+24|0];if(h>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}l=1;if(e>>>0>=f>>>0){break a}d=(e<<2)+d|0;a=(c&255)-e|0;break b;case 8:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];g=H[e>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);k=b;b=b+i|0;b=b+g|0;g=H[e+4>>2];while(1){if(b>>>0>=g>>>0){break a}L[(h<<2)+d>>2]=L[b>>2];b=b+4|0;h=h+1|0;e=I[a+24|0];if(h>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}l=1;if(e>>>0>=f>>>0){break a}d=(e<<2)+d|0;a=(c&255)-e|0;break b;case 9:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];g=H[e>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);k=b;b=b+i|0;b=b+g|0;g=H[e+4>>2];while(1){if(b>>>0>=g>>>0){break a}L[(h<<2)+d>>2]=M[b>>3];b=b+8|0;h=h+1|0;e=I[a+24|0];if(h>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}l=1;if(e>>>0>=f>>>0){break a}d=(e<<2)+d|0;a=(c&255)-e|0;break b;case 10:break c;default:break a}}e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[a>>2];g=H[e>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);k=b;b=b+i|0;b=b+g|0;g=H[e+4>>2];while(1){if(b>>>0>=g>>>0){break a}L[(h<<2)+d>>2]=I[b|0]?O(1):O(0);b=b+1|0;h=h+1|0;e=I[a+24|0];if(h>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}l=1;if(e>>>0>=f>>>0){break a}d=(e<<2)+d|0;a=(c&255)-e|0}ra(d,0,a<<2)}return l}function ic(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=O(0),m=O(0);a:{b:{if(!d){break b}c:{switch(H[a+28>>2]-1|0){case 0:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[H[a>>2]>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+i|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){break b}F[d+g|0]=I[b|0];b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 1:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[H[a>>2]>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+i|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){return 0}e=F[b|0];if((e|0)<0){break b}F[d+g|0]=e;b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 2:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[H[a>>2]>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+i|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){break b}e=J[b>>1];if((e+128&65535)>>>0>255){break b}F[d+g|0]=e;b=b+2|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 3:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[H[a>>2]>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+i|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){break b}e=J[b>>1];if(e>>>0>127){break b}F[d+g|0]=e;b=b+2|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 4:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[H[a>>2]>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+i|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){break b}e=H[b>>2];if(e+128>>>0>255){break b}F[d+g|0]=e;b=b+4|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 5:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[H[a>>2]>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+i|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){break b}e=H[b>>2];if(e>>>0>127){break b}F[d+g|0]=e;b=b+4|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 6:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[H[a>>2]>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+i|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){break b}i=H[b+4>>2];e=H[b>>2];h=e+128|0;i=h>>>0<128?i+1|0:i;if(!i&h>>>0>255|i){break b}F[d+g|0]=e;b=b+8|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 7:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[H[a>>2]>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+i|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){break b}i=H[b+4>>2];e=H[b>>2];if(!i&e>>>0>127|i){break b}F[d+g|0]=e;b=b+8|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 8:e=I[a+24|0];c=c&255;d:{if(c>>>0>e>>>0?e:c){e=H[H[a>>2]>>2];f=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+f|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){break d}l=L[b>>2];if(l>=O(127)|lO(1)){break d}j=T(+l*127+.5);if(!(P(j)<2147483648)){break f}h=~~j;break e}if(!(m>>0<(c>>>0>e>>>0?e:c)>>>0){continue}break}}k=1;if(c>>>0<=e>>>0){break d}ra(d+e|0,0,c-e|0)}return k;case 9:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[H[a>>2]>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+i|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){break b}j=M[b>>3];if(j>=127|j<-128|(P(j)==Infinity|j!=j)){break b}e=d+g|0;if(I[a+32|0]){if(j<0|j>1){break b}j=T(j*127+.5)}g:{if(P(j)<2147483648){h=~~j;break g}h=-2147483648}F[e|0]=h;b=b+8|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 10:break c;default:break b}}e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[H[a>>2]>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+i|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){break b}F[d+g|0]=I[b|0];b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}ra(d+e|0,0,(c&255)-e|0)}return k}ra(d+e|0,0,(c&255)-e|0);return 1}function hc(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=O(0);a:{b:{if(!d){break b}c:{switch(H[a+28>>2]-1|0){case 0:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[H[a>>2]>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+i|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){return 0}e=F[b|0];if((e|0)<0){break b}F[d+g|0]=e;b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 1:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[H[a>>2]>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+i|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){break b}F[d+g|0]=I[b|0];b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 2:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[H[a>>2]>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+i|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){break b}e=J[b>>1];if(e>>>0>255){break b}F[d+g|0]=e;b=b+2|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 3:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[H[a>>2]>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+i|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){break b}e=J[b>>1];if(e>>>0>255){break b}F[d+g|0]=e;b=b+2|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 4:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[H[a>>2]>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+i|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){break b}e=H[b>>2];if(e>>>0>255){break b}F[d+g|0]=e;b=b+4|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 5:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[H[a>>2]>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+i|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){break b}e=H[b>>2];if(e>>>0>255){break b}F[d+g|0]=e;b=b+4|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 6:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[H[a>>2]>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+i|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){break b}i=H[b+4>>2];e=H[b>>2];if(!i&e>>>0>255|i){break b}F[d+g|0]=e;b=b+8|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 7:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[H[a>>2]>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+i|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){break b}i=H[b+4>>2];e=H[b>>2];if(!i&e>>>0>255|i){break b}F[d+g|0]=e;b=b+8|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 8:e=I[a+24|0];c=c&255;d:{if(c>>>0>e>>>0?e:c){e=H[H[a>>2]>>2];f=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+f|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){break d}l=L[b>>2];if(l>=O(255)|lO(1)){break d}j=T(+l*255+.5);if(!(j<4294967296&j>=0)){break f}h=~~j>>>0;break e}if(!(l=O(0))){break f}h=~~l>>>0;break e}h=0}F[e|0]=h;b=b+4|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(c>>>0>e>>>0?e:c)>>>0){continue}break}}k=1;if(c>>>0<=e>>>0){break d}ra(d+e|0,0,c-e|0)}return k;case 9:e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[H[a>>2]>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+i|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){break b}j=M[b>>3];if(j>=255|j<0|(P(j)==Infinity|j!=j)){break b}e=d+g|0;if(I[a+32|0]){if(j>1){break b}j=T(j*255+.5)}g:{if(j<4294967296&j>=0){h=~~j>>>0;break g}h=0}F[e|0]=h;b=b+8|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}break a;case 10:break c;default:break b}}e=I[a+24|0];f=c&255;if(e>>>0>>0?e:f){e=H[H[a>>2]>>2];i=H[a+48>>2];b=Rj(H[a+40>>2],H[a+44>>2],b,0);h=b;b=b+i|0;b=b+e|0;while(1){if(K[H[a>>2]+4>>2]<=b>>>0){break b}F[d+g|0]=I[b|0];b=b+1|0;g=g+1|0;e=I[a+24|0];if(g>>>0<(e>>>0>>0?e:f)>>>0){continue}break}}k=1;if(e>>>0>=f>>>0){break b}ra(d+e|0,0,(c&255)-e|0)}return k}ra(d+e|0,0,(c&255)-e|0);return 1}function Hh(a){a=a|0;var b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0;g=ca-32|0;ca=g;i=H[a+32>>2];b=J[a+36>>1];a:{b:{if(((b<<8|b>>>8)&65535)>>>0<=513){b=H[i+8>>2];d=H[i+12>>2];c=b;b=H[i+20>>2];e=b;j=H[i+16>>2];f=j+4|0;b=f>>>0<4?b+1|0:b;if(c>>>0>>0&(b|0)>=(d|0)|(b|0)>(d|0)){break a}n=H[i>>2];k=n+j|0;k=I[k|0]|I[k+1|0]<<8|(I[k+2|0]<<16|I[k+3|0]<<24);H[i+16>>2]=f;H[i+20>>2]=b;h=c;c=d;b=e;d=j+8|0;b=d>>>0<8?b+1|0:b;if(d>>>0>h>>>0&(b|0)>=(c|0)|(b|0)>(c|0)){break a}c=f+n|0;n=I[c|0]|I[c+1|0]<<8|(I[c+2|0]<<16|I[c+3|0]<<24);H[i+16>>2]=d;H[i+20>>2]=b;break b}if(!Fb(1,g+28|0,i)){break a}if(!Fb(1,g+24|0,H[a+32>>2])){break a}k=H[g+28>>2];n=H[g+24>>2]}if(k>>>0>1431655765){break a}d=H[a+32>>2];b=d;j=H[b+8>>2];c=H[b+16>>2];f=H[b+12>>2];b=H[b+20>>2];e=Sj(j-c|0,f-(b+(c>>>0>j>>>0)|0)|0,3,0);if(!da&e>>>0>>0){break a}e=Rj(k,0,3,0);if(!da&e>>>0>>0|((b|0)>=(f|0)&c>>>0>=j>>>0|(b|0)>(f|0))){break a}j=I[c+H[d>>2]|0];c=c+1|0;b=c?b:b+1|0;H[d+16>>2]=c;H[d+20>>2]=b;c:{d:{if(!j){d=0;c=ca-32|0;ca=c;H[c+24>>2]=0;H[c+16>>2]=0;H[c+20>>2]=0;e:{f:{b=N(k,3);if(b){if(b>>>0>=1073741824){break f}j=N(k,12);d=pa(j);ra(d,0,j)}b=kd(b,1,H[a+32>>2],d);g:{h:{if(!(!k|!b)){j=0;while(1){i:{b=(j<<2)+d|0;f=H[b>>2];e=f>>>1|0;f=(f&1?0-e|0:e)+l|0;if((f|0)<0){break i}H[c>>2]=f;e=H[b+4>>2];h=e>>>1|0;f=f+(e&1?0-h|0:h)|0;if((f|0)<0){break i}H[c+4>>2]=f;b=H[b+8>>2];e=b>>>1|0;l=f+(b&1?0-e|0:e)|0;if((l|0)<0){break i}H[c+8>>2]=l;Rb(H[a+44>>2]+96|0,c);j=j+3|0;b=1;o=o+1|0;if((o|0)!=(k|0)){continue}break h}break}b=0;break h}if(!d){break g}}oa(d)}ca=c+32|0;break e}sa();v()}if(b){break d}break a}if(n>>>0<=255){if(!k){break d}while(1){j:{H[g+16>>2]=0;H[g+8>>2]=0;H[g+12>>2]=0;d=H[a+32>>2];b=d;j=H[b+16>>2];e=H[b+8>>2];c=H[b+20>>2];h=H[b+12>>2];b=h;if(e>>>0<=j>>>0&(c|0)>=(b|0)|(b|0)<(c|0)){break j}i=H[d>>2];l=I[i+j|0];b=c;f=j+1|0;b=f?b:b+1|0;H[d+16>>2]=f;H[d+20>>2]=b;H[g+8>>2]=l;l=e>>>0>>0&(c|0)>=(h|0)|(c|0)>(h|0);e=l?j:e;h=l?c:h;if((e|0)==(f|0)&(h|0)==(b|0)){break j}l=I[f+i|0];b=c;f=j+2|0;b=f>>>0<2?b+1|0:b;H[d+16>>2]=f;H[d+20>>2]=b;H[g+12>>2]=l;if((e|0)==(f|0)&(b|0)==(h|0)){break j}f=I[f+i|0];b=c;c=j+3|0;b=c>>>0<3?b+1|0:b;H[d+16>>2]=c;H[d+20>>2]=b;H[g+16>>2]=f;Rb(H[a+44>>2]+96|0,g+8|0);m=m+1|0;if((m|0)!=(k|0)){continue}break d}break}m=0;break a}if(n>>>0<=65535){if(!k){break d}while(1){k:{H[g+16>>2]=0;H[g+8>>2]=0;H[g+12>>2]=0;i=H[a+32>>2];b=i;c=H[b+8>>2];d=H[b+12>>2];f=H[b+16>>2];b=H[b+20>>2];j=b;e=f+2|0;b=e>>>0<2?b+1|0:b;if(c>>>0>>0&(b|0)>=(d|0)|(b|0)>(d|0)){break k}l=H[i>>2];h=l+f|0;h=I[h|0]|I[h+1|0]<<8;H[i+16>>2]=e;H[i+20>>2]=b;H[g+8>>2]=h;b=j;h=f+4|0;b=h>>>0<4?b+1|0:b;if(c>>>0>>0&(b|0)>=(d|0)|(b|0)>(d|0)){break k}e=e+l|0;e=I[e|0]|I[e+1|0]<<8;H[i+16>>2]=h;H[i+20>>2]=b;H[g+12>>2]=e;e=c;b=j;c=f+6|0;b=c>>>0<6?b+1|0:b;if(c>>>0>e>>>0&(b|0)>=(d|0)|(b|0)>(d|0)){break k}d=h+l|0;d=I[d|0]|I[d+1|0]<<8;H[i+16>>2]=c;H[i+20>>2]=b;H[g+16>>2]=d;Rb(H[a+44>>2]+96|0,g+8|0);m=m+1|0;if((m|0)!=(k|0)){continue}break d}break}m=0;break a}l:{if(n>>>0>2097151){break l}b=J[a+36>>1];if(((b<<8|b>>>8)&65535)>>>0<514){break l}if(!k){break d}while(1){m:{H[g+16>>2]=0;H[g+8>>2]=0;H[g+12>>2]=0;if(!Fb(1,g+4|0,H[a+32>>2])){break m}H[g+8>>2]=H[g+4>>2];if(!Fb(1,g+4|0,H[a+32>>2])){break m}H[g+12>>2]=H[g+4>>2];if(!Fb(1,g+4|0,H[a+32>>2])){break m}H[g+16>>2]=H[g+4>>2];Rb(H[a+44>>2]+96|0,g+8|0);m=m+1|0;if((m|0)!=(k|0)){continue}break d}break}m=0;break a}if(!k){break d}while(1){H[g+16>>2]=0;H[g+8>>2]=0;H[g+12>>2]=0;i=H[a+32>>2];b=i;c=H[b+8>>2];d=H[b+12>>2];f=H[b+16>>2];b=H[b+20>>2];j=b;e=f+4|0;b=e>>>0<4?b+1|0:b;if(c>>>0>>0&(b|0)>=(d|0)|(b|0)>(d|0)){break c}l=H[i>>2];h=l+f|0;h=I[h|0]|I[h+1|0]<<8|(I[h+2|0]<<16|I[h+3|0]<<24);H[i+16>>2]=e;H[i+20>>2]=b;H[g+8>>2]=h;b=j;h=f+8|0;b=h>>>0<8?b+1|0:b;if(c>>>0>>0&(b|0)>=(d|0)|(b|0)>(d|0)){break c}e=e+l|0;e=I[e|0]|I[e+1|0]<<8|(I[e+2|0]<<16|I[e+3|0]<<24);H[i+16>>2]=h;H[i+20>>2]=b;H[g+12>>2]=e;e=c;b=j;c=f+12|0;b=c>>>0<12?b+1|0:b;if(c>>>0>e>>>0&(b|0)>=(d|0)|(b|0)>(d|0)){break c}d=h+l|0;d=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[i+16>>2]=c;H[i+20>>2]=b;H[g+16>>2]=d;Rb(H[a+44>>2]+96|0,g+8|0);m=m+1|0;if((m|0)!=(k|0)){continue}break}}H[H[a+4>>2]+80>>2]=n;m=1;break a}m=0}ca=g+32|0;return m|0}function zf(a){a=a|0;var b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,v=O(0),w=0;p=ca-16|0;ca=p;a:{if(!(H[a+60>>2]!=H[a- -64>>2]|H[a+48>>2]!=H[a+52>>2])){j=1;break a}j=1;if((ea[H[H[a>>2]+24>>2]](a)|0)<=0){break a}while(1){b:{b=ea[H[H[a>>2]+20>>2]](a,w)|0;c:{d:{e:{f=H[H[H[(ea[H[H[a>>2]+28>>2]](a)|0)+4>>2]+8>>2]+(b<<2)>>2];switch(H[f+28>>2]-1|0){case 8:break d;case 0:case 2:case 4:break e;default:break c}}b=I[f+24|0];f:{if(!b){n=0;j=0;break f}j=0;b=b<<2;n=pa(b);ra(n,0,b);b=I[f+24|0];if(!b){break f}b=b<<2;j=pa(b);ra(j,0,b)}g:{h:{i:{switch(H[f+28>>2]-1|0){case 4:i=0;h=0;d=0;b=0;k=0;e=I[f+24|0];j:{if(!e){g=0;break j}e=e<<2;h=pa(e);ra(h,0,e);g=pa(e);ra(g,0,e)}k:{if(H[f+80>>2]){while(1){o=H[f>>2];c=H[o>>2];m=H[f+48>>2];e=H[f+40>>2];l=Rj(e,H[f+44>>2],d,b);m=m+l|0;s=c+m|0;c=e;m=qa(h,s,c);l=I[f+24|0];if(l){t=H[a+48>>2];e=0;while(1){r=e<<2;s=H[r+m>>2];if((s|0)<0){break k}H[g+r>>2]=s+H[t+(e+u<<2)>>2];e=e+1|0;if((l|0)!=(e|0)){continue}break}}qa(H[o>>2]+N(d,c)|0,g,c);d=d+1|0;b=d?b:b+1|0;if(!b&K[f+80>>2]>d>>>0){continue}break}}k=1}if(g){oa(g)}if(h){oa(h)}if(k){break h}break g;case 2:g=0;e=0;d=0;b=0;c=I[f+24|0];if(c){c=c<<1;e=pa(c);ra(e,0,c);g=pa(c);ra(g,0,c)}if(H[f+80>>2]){while(1){l=H[f>>2];h=H[l>>2];i=H[f+48>>2];c=H[f+40>>2];k=Rj(c,H[f+44>>2],d,b);i=i+k|0;k=qa(e,h+i|0,c);o=I[f+24|0];l:{if(!o){break l}m=H[a+48>>2];h=0;if((o|0)!=1){t=o&254;i=0;while(1){r=h<<1;G[r+g>>1]=J[k+r>>1]+J[m+(h+u<<2)>>1];r=h|1;s=r<<1;G[s+g>>1]=J[k+s>>1]+J[m+(r+u<<2)>>1];h=h+2|0;i=i+2|0;if((t|0)!=(i|0)){continue}break}}if(!(o&1)){break l}i=h<<1;G[i+g>>1]=J[i+k>>1]+J[m+(h+u<<2)>>1]}qa(H[l>>2]+N(d,c)|0,g,c);d=d+1|0;b=d?b:b+1|0;if(!b&K[f+80>>2]>d>>>0){continue}break}}if(g){oa(g)}if(e){oa(e)}break h;case 0:break i;default:break h}}h=0;e=0;d=0;b=0;c=I[f+24|0];if(c){e=pa(c);ra(e,0,c);h=pa(c);ra(h,0,c)}if(H[f+80>>2]){while(1){t=H[f>>2];g=H[t>>2];i=H[f+48>>2];c=H[f+40>>2];k=Rj(c,H[f+44>>2],d,b);i=i+k|0;k=qa(e,g+i|0,c);o=I[f+24|0];m:{if(!o){break m}m=H[a+48>>2];g=0;if((o|0)!=1){r=o&254;i=0;while(1){F[g+h|0]=I[g+k|0]+I[m+(g+u<<2)|0];l=g|1;F[l+h|0]=I[k+l|0]+I[m+(l+u<<2)|0];g=g+2|0;i=i+2|0;if((r|0)!=(i|0)){continue}break}}if(!(o&1)){break m}F[g+h|0]=I[g+k|0]+I[m+(g+u<<2)|0]}qa(H[t>>2]+N(d,c)|0,h,c);d=d+1|0;b=d?b:b+1|0;if(!b&K[f+80>>2]>d>>>0){continue}break}}if(h){oa(h)}if(e){oa(e)}}u=I[f+24|0]+u|0;i=1}if(j){oa(j)}if(n){oa(n)}if(i){break c}j=0;break a}e=H[H[a+60>>2]+(q<<2)>>2];h=H[a+36>>2];g=H[(ea[H[H[a>>2]+28>>2]](a)|0)+40>>2];H[p+12>>2]=H[f+56>>2];b=pa(32);H[p>>2]=b;H[p+4>>2]=24;H[p+8>>2]=-2147483616;d=I[1206]|I[1207]<<8|(I[1208]<<16|I[1209]<<24);c=I[1202]|I[1203]<<8|(I[1204]<<16|I[1205]<<24);F[b+16|0]=c;F[b+17|0]=c>>>8;F[b+18|0]=c>>>16;F[b+19|0]=c>>>24;F[b+20|0]=d;F[b+21|0]=d>>>8;F[b+22|0]=d>>>16;F[b+23|0]=d>>>24;d=I[1198]|I[1199]<<8|(I[1200]<<16|I[1201]<<24);c=I[1194]|I[1195]<<8|(I[1196]<<16|I[1197]<<24);F[b+8|0]=c;F[b+9|0]=c>>>8;F[b+10|0]=c>>>16;F[b+11|0]=c>>>24;F[b+12|0]=d;F[b+13|0]=d>>>8;F[b+14|0]=d>>>16;F[b+15|0]=d>>>24;d=I[1190]|I[1191]<<8|(I[1192]<<16|I[1193]<<24);c=I[1186]|I[1187]<<8|(I[1188]<<16|I[1189]<<24);F[b|0]=c;F[b+1|0]=c>>>8;F[b+2|0]=c>>>16;F[b+3|0]=c>>>24;F[b+4|0]=d;F[b+5|0]=d>>>8;F[b+6|0]=d>>>16;F[b+7|0]=d>>>24;F[b+24|0]=0;d=sd(g,p+12|0,p);if(F[p+11|0]<0){oa(H[p>>2])}b=q+1|0;n:{if(d){oe(f,e);break n}g=h+N(q,24)|0;q=H[g+4>>2];c=I[f+24|0];h=c<<2;d=pa(h);H[p>>2]=1065353216;v=L[g+20>>2];q=-1<0){L[p>>2]=v/O(q|0)}if((q|0)<=0){break b}o:{if(!H[e+80>>2]){break o}if(!c){n=0;j=0;while(1){qa(H[H[f+64>>2]>>2]+j|0,d,h);j=h+j|0;n=n+1|0;if(n>>>0>2]){continue}break}break o}o=H[H[e>>2]>>2]+H[e+48>>2]|0;t=c&254;r=c&1;i=0;k=0;j=0;while(1){q=H[g+8>>2];v=L[p>>2];n=0;m=0;if((c|0)!=1){while(1){l=n<<2;s=o+(j<<2)|0;L[l+d>>2]=O(v*O(H[s>>2]))+L[l+q>>2];l=l|4;L[l+d>>2]=O(v*O(H[s+4>>2]))+L[l+q>>2];n=n+2|0;j=j+2|0;m=m+2|0;if((t|0)!=(m|0)){continue}break}}if(r){n=n<<2;L[n+d>>2]=O(v*O(H[o+(j<<2)>>2]))+L[n+q>>2];j=j+1|0}qa(H[H[f+64>>2]>>2]+k|0,d,h);k=h+k|0;i=i+1|0;if(i>>>0>2]){continue}break}}oa(d)}q=b}j=1;w=w+1|0;if((ea[H[H[a>>2]+24>>2]](a)|0)>(w|0)){continue}break a}break}oa(d);j=0}ca=p+16|0;return j|0}function Le(a,b,c){var d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0;g=ca+-64|0;ca=g;H[g+56>>2]=0;H[g+48>>2]=0;H[g+52>>2]=0;H[g+40>>2]=0;H[g+44>>2]=0;H[g+32>>2]=0;H[g+36>>2]=0;H[g+24>>2]=0;H[g+28>>2]=0;H[g+16>>2]=0;H[g+20>>2]=0;H[g+8>>2]=0;H[g+12>>2]=0;j=g+8|0;d=J[b+38>>1];a:{b:{if(!d){break b}c:{if(d>>>0<=511){h=H[b+8>>2];f=H[b+12>>2];e=H[b+20>>2];d=H[b+16>>2];i=d+4|0;e=i>>>0<4?e+1|0:e;if(h>>>0>>0&(e|0)>=(f|0)|(e|0)>(f|0)){break b}d=d+H[b>>2]|0;l=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[j+12>>2]=l;e=H[b+20>>2];d=H[b+16>>2]+4|0;e=d>>>0<4?e+1|0:e;H[b+16>>2]=d;H[b+20>>2]=e;break c}if(!hb(1,j+12|0,b)){break b}d=H[b+16>>2];e=H[b+20>>2];l=H[j+12>>2]}f=H[b+8>>2];i=f-d|0;d=H[b+12>>2]-((d>>>0>f>>>0)+e|0)|0;if(i>>>0>>6>>>0&(d|0)<=0|(d|0)<0){break b}e=H[j>>2];d=H[j+4>>2]-e>>2;d:{if(d>>>0>>0){ya(j,l-d|0);l=H[j+12>>2];break d}if(d>>>0<=l>>>0){break d}H[j+4>>2]=e+(l<<2)}i=1;if(!l){break a}d=H[b+16>>2];e=H[b+20>>2];r=H[j>>2];k=H[b+8>>2];o=H[b+12>>2];h=0;while(1){i=0;if((e|0)>=(o|0)&d>>>0>=k>>>0|(e|0)>(o|0)){break a}i=H[b>>2];p=I[i+d|0];d=d+1|0;e=d?e:e+1|0;H[b+16>>2]=d;H[b+20>>2]=e;f=p>>>2|0;m=0;e:{f:{g:{h:{s=p&3;switch(s|0){case 0:break f;case 3:break h;default:break g}}f=f+h|0;i=0;if(f>>>0>=l>>>0){break a}ra(r+(h<<2)|0,0,(p&252)+4|0);h=f;break e}while(1){if((d|0)==(k|0)&(e|0)==(o|0)){break b}l=I[d+i|0];d=d+1|0;e=d?e:e+1|0;H[b+16>>2]=d;H[b+20>>2]=e;f=l<<(m<<3|6)|f;m=m+1|0;if((s|0)!=(m|0)){continue}break}}H[r+(h<<2)>>2]=f}l=H[j+12>>2];h=h+1|0;if(l>>>0>h>>>0){continue}break}d=j+16|0;o=H[j>>2];f=H[j+16>>2];e=H[j+20>>2]-f|0;i:{if(e>>>0<=4194303){ya(d,1048576-(e>>>2|0)|0);break i}if((e|0)==4194304){break i}H[j+20>>2]=f+4194304}e=j+28|0;h=H[e>>2];f=H[j+32>>2]-h>>3;j:{if(f>>>0>>0){ob(e,l-f|0);h=H[e>>2];break j}if(f>>>0>l>>>0){H[j+32>>2]=(l<<3)+h}if(!l){break b}}k=H[d>>2];d=0;i=0;while(1){e=o+(d<<2)|0;j=H[e>>2];m=(d<<3)+h|0;f=i;H[m+4>>2]=f;H[m>>2]=j;e=H[e>>2];i=e+f|0;if(i>>>0>1048576){break b}k:{if(f>>>0>=i>>>0){break k}m=0;j=e&7;if(j){while(1){H[k+(f<<2)>>2]=d;f=f+1|0;m=m+1|0;if((j|0)!=(m|0)){continue}break}}if(e-1>>>0<=6){break k}while(1){e=k+(f<<2)|0;H[e>>2]=d;H[e+28>>2]=d;H[e+24>>2]=d;H[e+20>>2]=d;H[e+16>>2]=d;H[e+12>>2]=d;H[e+8>>2]=d;H[e+4>>2]=d;f=f+8|0;if((i|0)!=(f|0)){continue}break}}d=d+1|0;if((l|0)!=(d|0)){continue}break}n=(i|0)==1048576}i=n}l:{if(!i|(H[g+20>>2]?0:a)){break l}i=0;n=ca-16|0;ca=n;m:{n:{if(J[b+38>>1]<=511){h=H[b+8>>2];f=H[b+12>>2];j=f;e=H[b+20>>2];k=H[b+16>>2];d=k+8|0;e=d>>>0<8?e+1|0:e;if(d>>>0>h>>>0&(e|0)>=(f|0)|(e|0)>(f|0)){break m}k=k+H[b>>2]|0;f=I[k|0]|I[k+1|0]<<8|(I[k+2|0]<<16|I[k+3|0]<<24);k=I[k+4|0]|I[k+5|0]<<8|(I[k+6|0]<<16|I[k+7|0]<<24);H[b+16>>2]=d;H[b+20>>2]=e;break n}if(!gb(1,n+8|0,b)){break m}d=H[b+16>>2];e=H[b+20>>2];h=H[b+8>>2];j=H[b+12>>2];f=H[n+8>>2];k=H[n+12>>2]}l=h-d|0;h=j-((d>>>0>h>>>0)+e|0)|0;if((h|0)==(k|0)&f>>>0>l>>>0|h>>>0>>0){break m}e=e+k|0;h=d+f|0;e=h>>>0>>0?e+1|0:e;H[b+16>>2]=h;H[b+20>>2]=e;if((f|0)<=0){break m}b=H[b>>2]+d|0;H[g+48>>2]=b;d=f-1|0;e=d+b|0;h=I[e|0];o:{if(h>>>0<=63){H[g+52>>2]=d;b=I[e|0]&63;break o}p:{switch((h>>>6|0)-1|0){case 0:if(f>>>0<2){break m}d=f-2|0;H[g+52>>2]=d;b=b+d|0;b=I[b+1|0]<<8&16128|I[b|0];break o;case 1:if(f>>>0<3){break m}d=f-3|0;H[g+52>>2]=d;b=b+d|0;b=I[b+1|0]<<8|I[b+2|0]<<16&4128768|I[b|0];break o;default:break p}}d=f-4|0;H[g+52>>2]=d;b=b+d|0;b=(I[b|0]|I[b+1|0]<<8|(I[b+2|0]<<16|I[b+3|0]<<24))&1073741823}H[g+56>>2]=b+4194304;i=b>>>0<1069547520}ca=n+16|0;if(!i){break l}if(!a){t=1;break l}b=H[g+52>>2];f=H[g+56>>2];d=H[g+36>>2];e=H[g+48>>2];h=H[g+24>>2];while(1){q:{if(f>>>0>4194303){break q}while(1){if((b|0)<=0){break q}b=b-1|0;H[g+52>>2]=b;f=I[b+e|0]|f<<8;H[g+56>>2]=f;if(f>>>0<4194304){continue}break}}i=f&1048575;k=H[h+(i<<2)>>2];n=d+(k<<3)|0;f=(N(H[n>>2],f>>>20|0)+i|0)-H[n+4>>2]|0;H[g+56>>2]=f;H[(q<<2)+c>>2]=k;t=1;q=q+1|0;if((q|0)!=(a|0)){continue}break}}a=H[g+36>>2];if(a){H[g+40>>2]=a;oa(a)}a=H[g+24>>2];if(a){H[g+28>>2]=a;oa(a)}a=H[g+8>>2];if(a){H[g+12>>2]=a;oa(a)}ca=g- -64|0;return t}function nc(a,b,c){var d=0,e=0,f=0,g=0,h=0,i=0;e=ca-48|0;ca=e;f=J[6677]|J[6678]<<16;d=J[6675]|J[6676]<<16;G[e+38>>1]=d;G[e+40>>1]=d>>>16;G[e+42>>1]=f;G[e+44>>1]=f>>>16;d=H[3337];H[e+32>>2]=H[3336];H[e+36>>2]=d;d=H[3335];H[e+24>>2]=H[3334];H[e+28>>2]=d;d=H[3333];H[e+16>>2]=H[3332];H[e+20>>2]=d;g=H[b+8>>2];i=H[b+12>>2];h=H[b+20>>2];d=H[b+16>>2];f=d+5|0;h=f>>>0<5?h+1|0:h;a:{b:{if(g>>>0>>0&(h|0)>=(i|0)|(h|0)>(i|0)){d=Ma(e+16|0);if(d>>>0>=2147483632){break a}c:{d:{if(d>>>0>=11){b=(d|15)+1|0;c=pa(b);H[e+8>>2]=b|-2147483648;H[e>>2]=c;H[e+4>>2]=d;b=c+d|0;break d}F[e+11|0]=d;b=d+e|0;c=e;if(!d){break c}}qa(c,e+16|0,d)}F[b|0]=0;H[a>>2]=-2;b=a+4|0;if(F[e+11|0]>=0){a=H[e+4>>2];H[b>>2]=H[e>>2];H[b+4>>2]=a;H[b+8>>2]=H[e+8>>2];break b}za(b,H[e>>2],H[e+4>>2]);if(F[e+11|0]>=0){break b}oa(H[e>>2]);break b}f=d+H[b>>2]|0;d=I[f|0]|I[f+1|0]<<8|(I[f+2|0]<<16|I[f+3|0]<<24);F[c|0]=d;F[c+1|0]=d>>>8;F[c+2|0]=d>>>16;F[c+3|0]=d>>>24;F[c+4|0]=I[f+4|0];d=H[b+20>>2];f=H[b+16>>2]+5|0;d=f>>>0<5?d+1|0:d;H[b+16>>2]=f;H[b+20>>2]=d;if(Fa(c,1260,5)){d=pa(32);F[d+17|0]=0;F[d+16|0]=I[1496];c=I[1492]|I[1493]<<8|(I[1494]<<16|I[1495]<<24);b=I[1488]|I[1489]<<8|(I[1490]<<16|I[1491]<<24);F[d+8|0]=b;F[d+9|0]=b>>>8;F[d+10|0]=b>>>16;F[d+11|0]=b>>>24;F[d+12|0]=c;F[d+13|0]=c>>>8;F[d+14|0]=c>>>16;F[d+15|0]=c>>>24;c=I[1484]|I[1485]<<8|(I[1486]<<16|I[1487]<<24);b=I[1480]|I[1481]<<8|(I[1482]<<16|I[1483]<<24);F[d|0]=b;F[d+1|0]=b>>>8;F[d+2|0]=b>>>16;F[d+3|0]=b>>>24;F[d+4|0]=c;F[d+5|0]=c>>>8;F[d+6|0]=c>>>16;F[d+7|0]=c>>>24;H[a>>2]=-1;za(a+4|0,d,17);oa(d);break b}g=H[b+12>>2];if((g|0)<=(d|0)&K[b+8>>2]<=f>>>0|(d|0)>(g|0)){d=Ma(e+16|0);if(d>>>0>=2147483632){break a}e:{f:{if(d>>>0>=11){b=(d|15)+1|0;c=pa(b);H[e+8>>2]=b|-2147483648;H[e>>2]=c;H[e+4>>2]=d;b=c+d|0;break f}F[e+11|0]=d;b=d+e|0;c=e;if(!d){break e}}qa(c,e+16|0,d)}F[b|0]=0;H[a>>2]=-2;b=a+4|0;if(F[e+11|0]>=0){a=H[e+4>>2];H[b>>2]=H[e>>2];H[b+4>>2]=a;H[b+8>>2]=H[e+8>>2];break b}za(b,H[e>>2],H[e+4>>2]);if(F[e+11|0]>=0){break b}oa(H[e>>2]);break b}F[c+5|0]=I[f+H[b>>2]|0];g=H[b+20>>2];d=H[b+16>>2]+1|0;g=d?g:g+1|0;H[b+16>>2]=d;H[b+20>>2]=g;f=H[b+12>>2];if((f|0)<=(g|0)&K[b+8>>2]<=d>>>0|(g|0)>(f|0)){d=Ma(e+16|0);if(d>>>0>=2147483632){break a}g:{h:{if(d>>>0>=11){b=(d|15)+1|0;c=pa(b);H[e+8>>2]=b|-2147483648;H[e>>2]=c;H[e+4>>2]=d;b=c+d|0;break h}F[e+11|0]=d;b=d+e|0;c=e;if(!d){break g}}qa(c,e+16|0,d)}F[b|0]=0;H[a>>2]=-2;b=a+4|0;if(F[e+11|0]>=0){a=H[e+4>>2];H[b>>2]=H[e>>2];H[b+4>>2]=a;H[b+8>>2]=H[e+8>>2];break b}za(b,H[e>>2],H[e+4>>2]);if(F[e+11|0]>=0){break b}oa(H[e>>2]);break b}F[c+6|0]=I[d+H[b>>2]|0];h=H[b+20>>2];d=H[b+16>>2]+1|0;h=d?h:h+1|0;H[b+16>>2]=d;H[b+20>>2]=h;f=H[b+12>>2];if((f|0)<=(h|0)&K[b+8>>2]<=d>>>0|(f|0)<(h|0)){d=Ma(e+16|0);if(d>>>0>=2147483632){break a}i:{j:{if(d>>>0>=11){b=(d|15)+1|0;c=pa(b);H[e+8>>2]=b|-2147483648;H[e>>2]=c;H[e+4>>2]=d;b=c+d|0;break j}F[e+11|0]=d;b=d+e|0;c=e;if(!d){break i}}qa(c,e+16|0,d)}F[b|0]=0;H[a>>2]=-2;b=a+4|0;if(F[e+11|0]>=0){a=H[e+4>>2];H[b>>2]=H[e>>2];H[b+4>>2]=a;H[b+8>>2]=H[e+8>>2];break b}za(b,H[e>>2],H[e+4>>2]);if(F[e+11|0]>=0){break b}oa(H[e>>2]);break b}F[c+7|0]=I[d+H[b>>2]|0];g=H[b+20>>2];d=H[b+16>>2]+1|0;g=d?g:g+1|0;H[b+16>>2]=d;H[b+20>>2]=g;f=H[b+12>>2];if((f|0)<=(g|0)&K[b+8>>2]<=d>>>0|(g|0)>(f|0)){c=mc(e,e+16|0);H[a>>2]=-2;b=a+4|0;if(F[c+11|0]>=0){a=H[c+4>>2];H[b>>2]=H[c>>2];H[b+4>>2]=a;H[b+8>>2]=H[c+8>>2];break b}za(b,H[c>>2],H[c+4>>2]);if(F[c+11|0]>=0){break b}oa(H[c>>2]);break b}F[c+8|0]=I[d+H[b>>2]|0];d=H[b+20>>2];g=H[b+16>>2];f=g+1|0;i=f?d:d+1|0;H[b+16>>2]=f;H[b+20>>2]=i;i=H[b+8>>2];h=H[b+12>>2];g=g+3|0;d=g>>>0<3?d+1|0:d;if(g>>>0>i>>>0&(d|0)>=(h|0)|(d|0)>(h|0)){c=mc(e,e+16|0);H[a>>2]=-2;b=a+4|0;if(F[c+11|0]>=0){a=H[c+4>>2];H[b>>2]=H[c>>2];H[b+4>>2]=a;H[b+8>>2]=H[c+8>>2];break b}za(b,H[c>>2],H[c+4>>2]);if(F[c+11|0]>=0){break b}oa(H[c>>2]);break b}d=c;c=H[b>>2]+f|0;G[d+10>>1]=I[c|0]|I[c+1|0]<<8;g=H[b+20>>2];c=H[b+16>>2]+2|0;g=c>>>0<2?g+1|0:g;H[b+16>>2]=c;H[b+20>>2]=g;H[a+8>>2]=0;H[a+12>>2]=0;H[a>>2]=0;H[a+4>>2]=0}ca=e+48|0;return}Na();v()}function Nb(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0,x=0,y=0;e=ca-96|0;ca=e;f=H[a+16>>2];F[e+92|0]=1;H[e+88>>2]=b;H[e+84>>2]=b;H[e+80>>2]=f;j=H[a+20>>2];d=H[j>>2];a:{b:{f=H[H[f+28>>2]+(b<<2)>>2];if(f>>>0>2]-d>>2>>>0){d=H[H[a+8>>2]+(H[d+(f<<2)>>2]<<2)>>2];f=H[a+4>>2];if(!I[f+84|0]){d=H[H[f+68>>2]+(d<<2)>>2]}H[e+72>>2]=0;H[e+76>>2]=0;j=e- -64|0;H[j>>2]=0;H[j+4>>2]=0;H[e+56>>2]=0;H[e+60>>2]=0;Sa(f,d,F[f+24|0],e+56|0);if((b|0)!=-1){f=b+1|0;j=(f>>>0)%3|0?f:b-2|0;m=((b>>>0)%3|0?-1:2)+b|0;while(1){d=j;f=m;c:{if(!H[a+28>>2]){break c}f=b+1|0;d=(f>>>0)%3|0?f:b-2|0;f=b-1|0;if((b>>>0)%3|0){break c}f=b+2|0}n=H[a+20>>2];b=H[n>>2];d=H[H[H[a+16>>2]+28>>2]+(d<<2)>>2];if(d>>>0>=H[n+4>>2]-b>>2>>>0){break b}d=H[H[a+8>>2]+(H[b+(d<<2)>>2]<<2)>>2];b=H[a+4>>2];if(!I[b+84|0]){d=H[H[b+68>>2]+(d<<2)>>2]}H[e+48>>2]=0;H[e+52>>2]=0;H[e+40>>2]=0;H[e+44>>2]=0;H[e+32>>2]=0;H[e+36>>2]=0;Sa(b,d,F[b+24|0],e+32|0);d=H[a+20>>2];b=H[d>>2];f=H[H[H[a+16>>2]+28>>2]+(f<<2)>>2];if(f>>>0>=H[d+4>>2]-b>>2>>>0){break a}d=H[H[a+8>>2]+(H[b+(f<<2)>>2]<<2)>>2];b=H[a+4>>2];if(!I[b+84|0]){d=H[H[b+68>>2]+(d<<2)>>2]}H[e+24>>2]=0;H[e+28>>2]=0;H[e+16>>2]=0;H[e+20>>2]=0;H[e+8>>2]=0;H[e+12>>2]=0;Sa(b,d,F[b+24|0],e+8|0);g=H[e+8>>2];b=H[e+56>>2];d=g-b|0;p=H[e+60>>2];t=H[e+12>>2]-(p+(b>>>0>g>>>0)|0)|0;h=H[e+40>>2];f=H[e+64>>2];n=h-f|0;u=H[e+68>>2];y=H[e+44>>2]-(u+(f>>>0>h>>>0)|0)|0;g=Rj(d,t,n,y);w=o-g|0;x=i-(da+(g>>>0>o>>>0)|0)|0;i=w;h=H[e+16>>2];g=h-f|0;u=H[e+20>>2]-((f>>>0>h>>>0)+u|0)|0;k=H[e+32>>2];h=k-b|0;w=H[e+36>>2]-((b>>>0>k>>>0)+p|0)|0;b=Rj(g,u,h,w);o=i+b|0;i=da+x|0;i=b>>>0>o>>>0?i+1|0:i;b=l;l=d;p=t;k=H[e+48>>2];f=H[e+72>>2];d=k-f|0;t=H[e+76>>2];x=H[e+52>>2]-(t+(f>>>0>k>>>0)|0)|0;l=Rj(l,p,d,x);k=b+l|0;b=da+q|0;b=k>>>0>>0?b+1|0:b;l=H[e+24>>2];p=l-f|0;f=H[e+28>>2]-((f>>>0>l>>>0)+t|0)|0;q=Rj(p,f,h,w);l=k-q|0;q=b-(da+(k>>>0>>0)|0)|0;b=Rj(g,u,d,x);d=r-b|0;b=s-(da+(b>>>0>r>>>0)|0)|0;s=Rj(p,f,n,y);r=s+d|0;b=da+b|0;s=r>>>0>>0?b+1|0:b;b=H[e+88>>2];f=H[e+80>>2];d:{if(I[e+92|0]){e:{f:{g:{h:{if((b|0)==-1){break h}d=b+1|0;b=(d>>>0)%3|0?d:b-2|0;if((b|0)==-1|H[H[f>>2]+(b>>>3&536870908)>>2]>>>b&1){break h}b=H[H[H[f+64>>2]+12>>2]+(b<<2)>>2];if((b|0)!=-1){break g}}H[e+88>>2]=-1;break f}d=b+1|0;b=(d>>>0)%3|0?d:b-2|0;H[e+88>>2]=b;if((b|0)!=-1){break e}}b=H[e+84>>2];d=-1;i:{if((b|0)==-1){break i}j:{if((b>>>0)%3|0){b=b-1|0;break j}b=b+2|0;d=-1;if((b|0)==-1){break i}}d=-1;if(H[H[f>>2]+(b>>>3&536870908)>>2]>>>b&1){break i}b=H[H[H[f+64>>2]+12>>2]+(b<<2)>>2];d=-1;if((b|0)==-1){break i}d=b-1|0;if((b>>>0)%3|0){break i}d=b+2|0}F[e+92|0]=0;H[e+88>>2]=d;break d}if((b|0)!=H[e+84>>2]){break d}H[e+88>>2]=-1;break d}d=-1;k:{if((b|0)==-1){break k}l:{if((b>>>0)%3|0){b=b-1|0;break l}b=b+2|0;d=-1;if((b|0)==-1){break k}}d=-1;if(H[H[f>>2]+(b>>>3&536870908)>>2]>>>b&1){break k}b=H[H[H[f+64>>2]+12>>2]+(b<<2)>>2];d=-1;if((b|0)==-1){break k}d=b-1|0;if((b>>>0)%3|0){break k}d=b+2|0}H[e+88>>2]=d}b=H[e+88>>2];if((b|0)!=-1){continue}break}}b=s>>31;f=b^r;d=f-b|0;b=(b^s)-((b>>>0>f>>>0)+b|0)|0;m=-1;f=2147483647;g=q>>31;h=g^l;j=h-g|0;n=(g^q)-((h>>>0>>0)+g|0)|0;h=n;k=j^-1;g=h^2147483647;n=i;m:{n:{if(!H[a+28>>2]){if((b|0)==(g|0)&d>>>0>k>>>0|b>>>0>g>>>0){break m}b=b+h|0;a=d+j|0;b=a>>>0>>0?b+1|0:b;f=a;g=i;a=g>>31;d=a;m=d^o;a=m-d|0;i=a;d=(d^g)-((d>>>0>m>>>0)+d|0)|0;a=a+f|0;d=d^2147483647;i=(d|0)==(b|0)&(i^-1)>>>0>>0|b>>>0>d>>>0;a=i?-1:a;if(!(i&0)&(a|0)<=536870912|(a|0)<536870912){break m}b=0;a=a>>>29|0;break n}o:{if((b|0)==(g|0)&d>>>0>k>>>0|b>>>0>g>>>0){break o}b=b+h|0;a=d+j|0;b=a>>>0>>0?b+1|0:b;k=i;d=i>>31;h=d^o;i=h-d|0;j=(d^k)-((d>>>0>h>>>0)+d|0)|0;g=j^2147483647;d=a;a=i;if((g|0)==(b|0)&d>>>0>(a^-1)>>>0|b>>>0>g>>>0){break o}b=b+j|0;m=a+d|0;b=m>>>0>>0?b+1|0:b;f=b;if(!b&m>>>0<536870913){break m}}b=f>>>29|0;a=(f&536870911)<<3|m>>>29}o=Sj(o,n,a,b);l=Sj(l,q,a,b);r=Sj(r,s,a,b)}H[c+8>>2]=o;H[c+4>>2]=l;H[c>>2]=r;ca=e+96|0;return}Ca();v()}Ca();v()}Ca();v()}function Jj(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;var g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0,x=0,y=0,z=0,A=0,B=0,C=0,D=0,E=0,F=0;H[a+8>>2]=e;r=a+32|0;g=H[r>>2];f=H[a+36>>2]-g>>2;a:{if(f>>>0>>0){ya(r,e-f|0);d=H[a+8>>2];break a}d=e;if(d>>>0>=f>>>0){break a}H[a+36>>2]=g+(e<<2);d=e}w=e<<2;f=e>>>0>1073741823?-1:w;m=ra(pa(f),0,f);p=ra(pa(f),0,f);b:{if((d|0)<=0){break b}i=H[a+32>>2];while(1){d=h<<2;f=H[d+m>>2];g=H[a+16>>2];c:{if((f|0)>(g|0)){H[d+i>>2]=g;break c}d=d+i|0;g=H[a+12>>2];if((g|0)>(f|0)){H[d>>2]=g;break c}H[d>>2]=f}d=H[a+8>>2];h=h+1|0;if((d|0)>(h|0)){continue}break}if((d|0)<=0){break b}f=0;while(1){g=f<<2;d=g+c|0;g=H[b+g>>2]+H[g+i>>2]|0;H[d>>2]=g;d:{if((g|0)>H[a+16>>2]){g=g-H[a+20>>2]|0}else{if((g|0)>=H[a+12>>2]){break d}g=g+H[a+20>>2]|0}H[d>>2]=g}d=H[a+8>>2];f=f+1|0;if((d|0)>(f|0)){continue}break}}f=H[a+56>>2];x=H[f>>2];f=H[f+4>>2]-x|0;if((f|0)>=5){D=H[a+52>>2];s=H[a+48>>2];u=f>>>2|0;E=u>>>0<=2?2:u;y=e&-2;z=e&1;F=e&-4;A=e&3;B=e-1|0;n=1;while(1){e:{f:{g:{h:{if((n|0)!=(u|0)){g=H[(n<<2)+x>>2];t=(e|0)<=0;if(!t){ra(m,0,w)}if((g|0)==-1){i=N(e,n);break f}C=H[s>>2];l=0;f=g;while(1){i:{if(H[(f>>>3&536870908)+C>>2]>>>f&1){break i}i=H[H[H[s+64>>2]+12>>2]+(f<<2)>>2];if((i|0)==-1){break i}j=H[D>>2];h=H[s+28>>2];o=H[j+(H[h+(i<<2)>>2]<<2)>>2];if((o|0)>=(n|0)){break i}k=i+1|0;k=H[j+(H[h+(((k>>>0)%3|0?k:i-2|0)<<2)>>2]<<2)>>2];if((k|0)>=(n|0)){break i}i=H[j+(H[h+(i+((i>>>0)%3|0?-1:2)<<2)>>2]<<2)>>2];if((i|0)>=(n|0)){break i}j:{if(t){break j}i=N(e,i);j=N(e,k);o=N(e,o);h=0;q=0;if(B){while(1){H[(h<<2)+p>>2]=(H[(h+i<<2)+c>>2]+H[(h+j<<2)+c>>2]|0)-H[(h+o<<2)+c>>2];k=h|1;H[(k<<2)+p>>2]=(H[(i+k<<2)+c>>2]+H[(j+k<<2)+c>>2]|0)-H[(k+o<<2)+c>>2];h=h+2|0;q=q+2|0;if((y|0)!=(q|0)){continue}break}}if(z){H[(h<<2)+p>>2]=(H[(h+i<<2)+c>>2]+H[(h+j<<2)+c>>2]|0)-H[(h+o<<2)+c>>2]}if(t){break j}o=0;h=0;i=0;if(e>>>0>3){while(1){j=h<<2;k=j+m|0;H[k>>2]=H[j+p>>2]+H[k>>2];k=j|4;q=k+m|0;H[q>>2]=H[k+p>>2]+H[q>>2];k=j|8;q=k+m|0;H[q>>2]=H[k+p>>2]+H[q>>2];j=j|12;k=j+m|0;H[k>>2]=H[j+p>>2]+H[k>>2];h=h+4|0;i=i+4|0;if((F|0)!=(i|0)){continue}break}}if(!A){break j}while(1){i=h<<2;j=i+m|0;H[j>>2]=H[i+p>>2]+H[j>>2];h=h+1|0;o=o+1|0;if((A|0)!=(o|0)){continue}break}}l=l+1|0}k:{l:{if((f>>>0)%3|0){h=f-1|0;break l}h=f+2|0;i=-1;if((h|0)==-1){break k}}i=-1;if(H[(h>>>3&536870908)+C>>2]>>>h&1){break k}f=H[H[H[s+64>>2]+12>>2]+(h<<2)>>2];i=-1;if((f|0)==-1){break k}i=f-1|0;if((f>>>0)%3|0){break k}i=f+2|0}f=i;if((g|0)!=(f|0)&(f|0)!=-1){continue}break}i=N(e,n);if(!l){break f}if(t){break g}h=0;f=0;if(!B){break h}while(1){g=h<<2;j=g+m|0;H[j>>2]=H[j>>2]/(l|0);g=(g|4)+m|0;H[g>>2]=H[g>>2]/(l|0);h=h+2|0;f=f+2|0;if((y|0)!=(f|0)){continue}break}break h}Ca();v()}if(!z){break g}f=(h<<2)+m|0;H[f>>2]=H[f>>2]/(l|0)}if((d|0)<=0){break e}l=H[r>>2];h=0;while(1){d=h<<2;f=H[d+m>>2];g=H[a+16>>2];m:{if((f|0)>(g|0)){H[d+l>>2]=g;break m}d=d+l|0;g=H[a+12>>2];if((g|0)>(f|0)){H[d>>2]=g;break m}H[d>>2]=f}d=H[a+8>>2];h=h+1|0;if((d|0)>(h|0)){continue}break}f=0;if((d|0)<=0){break e}d=i<<2;i=d+c|0;h=b+d|0;while(1){g=f<<2;d=g+i|0;g=H[h+g>>2]+H[g+l>>2]|0;H[d>>2]=g;n:{if((g|0)>H[a+16>>2]){g=g-H[a+20>>2]|0}else{if((g|0)>=H[a+12>>2]){break n}g=g+H[a+20>>2]|0}H[d>>2]=g}d=H[a+8>>2];f=f+1|0;if((d|0)>(f|0)){continue}break}break e}if((d|0)<=0){break e}g=(N(n-1|0,e)<<2)+c|0;l=H[r>>2];h=0;while(1){d=h<<2;f=H[d+g>>2];j=H[a+16>>2];o:{if((f|0)>(j|0)){H[d+l>>2]=j;break o}d=d+l|0;j=H[a+12>>2];if((j|0)>(f|0)){H[d>>2]=j;break o}H[d>>2]=f}d=H[a+8>>2];h=h+1|0;if((d|0)>(h|0)){continue}break}f=0;if((d|0)<=0){break e}d=i<<2;i=d+c|0;h=b+d|0;while(1){g=f<<2;d=g+i|0;g=H[h+g>>2]+H[g+l>>2]|0;H[d>>2]=g;p:{if((g|0)>H[a+16>>2]){g=g-H[a+20>>2]|0}else{if((g|0)>=H[a+12>>2]){break p}g=g+H[a+20>>2]|0}H[d>>2]=g}d=H[a+8>>2];f=f+1|0;if((d|0)>(f|0)){continue}break}}n=n+1|0;if((E|0)!=(n|0)){continue}break}}oa(p);oa(m);return 1}function sj(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;var g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0,x=0,y=0,z=0,A=0,B=0,C=0,D=0,E=0,F=0;H[a+8>>2]=e;r=a+32|0;f=H[r>>2];j=H[a+36>>2]-f>>2;a:{if(j>>>0>>0){ya(r,e-j|0);d=H[a+8>>2];break a}d=e;if(e>>>0>=j>>>0){break a}H[a+36>>2]=f+(e<<2);d=e}u=e<<2;f=e>>>0>1073741823?-1:u;m=ra(pa(f),0,f);p=ra(pa(f),0,f);b:{if((d|0)<=0){break b}i=H[a+32>>2];while(1){f=h<<2;j=H[f+m>>2];d=H[a+16>>2];c:{if((j|0)>(d|0)){H[f+i>>2]=d;break c}f=f+i|0;d=H[a+12>>2];if((d|0)>(j|0)){H[f>>2]=d;break c}H[f>>2]=j}d=H[a+8>>2];h=h+1|0;if((d|0)>(h|0)){continue}break}if((d|0)<=0){break b}f=0;while(1){j=f<<2;d=j+c|0;j=H[b+j>>2]+H[j+i>>2]|0;H[d>>2]=j;d:{if((j|0)>H[a+16>>2]){j=j-H[a+20>>2]|0}else{if((j|0)>=H[a+12>>2]){break d}j=j+H[a+20>>2]|0}H[d>>2]=j}d=H[a+8>>2];f=f+1|0;if((d|0)>(f|0)){continue}break}}f=H[a+56>>2];w=H[f>>2];f=H[f+4>>2]-w|0;if((f|0)>=5){D=H[a+52>>2];x=H[a+48>>2];t=f>>>2|0;E=t>>>0<=2?2:t;y=e&-2;z=e&1;F=e&-4;A=e&3;B=e-1|0;n=1;while(1){e:{f:{g:{h:{if((n|0)!=(t|0)){j=H[(n<<2)+w>>2];s=(e|0)<=0;if(!s){ra(m,0,u)}if((j|0)==-1){g=N(e,n);break f}C=H[x+12>>2];q=0;f=j;while(1){h=H[(f<<2)+C>>2];i:{if((h|0)==-1){break i}o=H[D>>2];l=H[x>>2];k=H[o+(H[l+(h<<2)>>2]<<2)>>2];i=h+1|0;i=(i>>>0)%3|0?i:h-2|0;if((i|0)!=-1){g=H[l+(i<<2)>>2]}else{g=-1}j:{k:{if((h>>>0)%3|0){h=h-1|0;break k}h=h+2|0;i=-1;if((h|0)==-1){break j}}i=H[l+(h<<2)>>2]}if((k|0)>=(n|0)){break i}g=H[(g<<2)+o>>2];if((g|0)>=(n|0)){break i}i=H[o+(i<<2)>>2];if((i|0)>=(n|0)){break i}l:{if(s){break l}l=N(e,i);o=N(e,g);k=N(e,k);h=0;i=0;if(B){while(1){H[(h<<2)+p>>2]=(H[(h+l<<2)+c>>2]+H[(h+o<<2)+c>>2]|0)-H[(h+k<<2)+c>>2];g=h|1;H[(g<<2)+p>>2]=(H[(g+l<<2)+c>>2]+H[(g+o<<2)+c>>2]|0)-H[(g+k<<2)+c>>2];h=h+2|0;i=i+2|0;if((y|0)!=(i|0)){continue}break}}if(z){H[(h<<2)+p>>2]=(H[(h+l<<2)+c>>2]+H[(h+o<<2)+c>>2]|0)-H[(h+k<<2)+c>>2]}if(s){break l}o=0;h=0;k=0;if(e>>>0>3){while(1){l=h<<2;i=l+m|0;H[i>>2]=H[l+p>>2]+H[i>>2];g=l|4;i=g+m|0;H[i>>2]=H[g+p>>2]+H[i>>2];g=l|8;i=g+m|0;H[i>>2]=H[g+p>>2]+H[i>>2];g=l|12;i=g+m|0;H[i>>2]=H[g+p>>2]+H[i>>2];h=h+4|0;k=k+4|0;if((F|0)!=(k|0)){continue}break}}if(!A){break l}while(1){g=h<<2;i=g+m|0;H[i>>2]=H[g+p>>2]+H[i>>2];h=h+1|0;o=o+1|0;if((A|0)!=(o|0)){continue}break}}q=q+1|0}m:{n:{if((f>>>0)%3|0){h=f-1|0;break n}h=f+2|0;g=-1;if((h|0)==-1){break m}}f=H[(h<<2)+C>>2];g=-1;if((f|0)==-1){break m}g=f-1|0;if((f>>>0)%3|0){break m}g=f+2|0}f=g;if((j|0)!=(f|0)&(f|0)!=-1){continue}break}g=N(e,n);if(!q){break f}if(s){break g}h=0;f=0;if(!B){break h}while(1){i=h<<2;j=i+m|0;H[j>>2]=H[j>>2]/(q|0);j=(i|4)+m|0;H[j>>2]=H[j>>2]/(q|0);h=h+2|0;f=f+2|0;if((y|0)!=(f|0)){continue}break}break h}Ca();v()}if(!z){break g}f=(h<<2)+m|0;H[f>>2]=H[f>>2]/(q|0)}if((d|0)<=0){break e}k=H[r>>2];h=0;while(1){f=h<<2;j=H[f+m>>2];d=H[a+16>>2];o:{if((j|0)>(d|0)){H[f+k>>2]=d;break o}f=f+k|0;d=H[a+12>>2];if((d|0)>(j|0)){H[f>>2]=d;break o}H[f>>2]=j}d=H[a+8>>2];h=h+1|0;if((d|0)>(h|0)){continue}break}f=0;if((d|0)<=0){break e}d=g<<2;i=d+c|0;j=b+d|0;while(1){g=f<<2;d=g+i|0;g=H[g+j>>2]+H[g+k>>2]|0;H[d>>2]=g;p:{if((g|0)>H[a+16>>2]){g=g-H[a+20>>2]|0}else{if((g|0)>=H[a+12>>2]){break p}g=g+H[a+20>>2]|0}H[d>>2]=g}d=H[a+8>>2];f=f+1|0;if((d|0)>(f|0)){continue}break}break e}if((d|0)<=0){break e}f=(N(n-1|0,e)<<2)+c|0;k=H[r>>2];h=0;while(1){j=h<<2;i=H[j+f>>2];d=H[a+16>>2];q:{if((i|0)>(d|0)){H[j+k>>2]=d;break q}j=j+k|0;d=H[a+12>>2];if((d|0)>(i|0)){H[j>>2]=d;break q}H[j>>2]=i}d=H[a+8>>2];h=h+1|0;if((d|0)>(h|0)){continue}break}f=0;if((d|0)<=0){break e}d=g<<2;i=d+c|0;j=b+d|0;while(1){g=f<<2;d=g+i|0;g=H[g+j>>2]+H[g+k>>2]|0;H[d>>2]=g;r:{if((g|0)>H[a+16>>2]){g=g-H[a+20>>2]|0}else{if((g|0)>=H[a+12>>2]){break r}g=g+H[a+20>>2]|0}H[d>>2]=g}d=H[a+8>>2];f=f+1|0;if((d|0)>(f|0)){continue}break}}n=n+1|0;if((E|0)!=(n|0)){continue}break}}oa(p);oa(m);return 1}function xa(a){var b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0;f=ca-32|0;ca=f;a:{b=H[a+16>>2];b:{if(b>>>0>=341){H[a+16>>2]=b-341;b=H[a+4>>2];j=H[b>>2];c=b+4|0;H[a+4>>2]=c;b=H[a+8>>2];c:{if((b|0)!=H[a+12>>2]){d=b;break c}k=H[a>>2];if(k>>>0>>0){e=((c-k>>2)+1|0)/-2<<2;b=b-c|0;d=va(e+c|0,c,b)+b|0;H[a+8>>2]=d;H[a+4>>2]=e+H[a+4>>2];break c}d=(b|0)==(k|0)?1:b-k>>1;if(d>>>0>=1073741824){break a}e=d<<2;h=pa(e);l=e+h|0;e=h+(d&-4)|0;d=e;d:{if((b|0)==(c|0)){break d}b=b-c|0;m=b&-4;i=b-4|0;g=(i>>>2|0)+1&7;e:{if(!g){b=e;break e}d=0;b=e;while(1){H[b>>2]=H[c>>2];c=c+4|0;b=b+4|0;d=d+1|0;if((g|0)!=(d|0)){continue}break}}d=e+m|0;if(i>>>0<28){break d}while(1){H[b>>2]=H[c>>2];H[b+4>>2]=H[c+4>>2];H[b+8>>2]=H[c+8>>2];H[b+12>>2]=H[c+12>>2];H[b+16>>2]=H[c+16>>2];H[b+20>>2]=H[c+20>>2];H[b+24>>2]=H[c+24>>2];H[b+28>>2]=H[c+28>>2];c=c+32|0;b=b+32|0;if((d|0)!=(b|0)){continue}break}}H[a+12>>2]=l;H[a+8>>2]=d;H[a+4>>2]=e;H[a>>2]=h;if(!k){break c}oa(k);d=H[a+8>>2]}H[d>>2]=j;H[a+8>>2]=H[a+8>>2]+4;break b}c=H[a+8>>2];b=H[a+4>>2];l=c-b|0;h=l>>2;g=H[a+12>>2];d=H[a>>2];e=g-d|0;if(h>>>0>2>>>0){if((c|0)!=(g|0)){n=f,o=pa(4092),H[n+8>>2]=o;d=a;f:{g:{b=H[a+8>>2];h:{if((b|0)!=H[a+12>>2]){e=b;break h}c=H[d+4>>2];h=H[d>>2];if(c>>>0>h>>>0){g=((c-h>>2)+1|0)/-2<<2;a=b-c|0;e=va(g+c|0,c,a)+a|0;H[d+8>>2]=e;H[d+4>>2]=g+H[d+4>>2];break h}e=(b|0)==(h|0)?1:b-h>>1;if(e>>>0>=1073741824){break g}a=e<<2;j=pa(a);l=a+j|0;a=j+(e&-4)|0;e=a;i:{if((b|0)==(c|0)){break i}b=b-c|0;m=b&-4;i=b-4|0;g=(i>>>2|0)+1&7;j:{if(!g){b=a;break j}e=0;b=a;while(1){H[b>>2]=H[c>>2];c=c+4|0;b=b+4|0;e=e+1|0;if((g|0)!=(e|0)){continue}break}}e=a+m|0;if(i>>>0<28){break i}while(1){H[b>>2]=H[c>>2];H[b+4>>2]=H[c+4>>2];H[b+8>>2]=H[c+8>>2];H[b+12>>2]=H[c+12>>2];H[b+16>>2]=H[c+16>>2];H[b+20>>2]=H[c+20>>2];H[b+24>>2]=H[c+24>>2];H[b+28>>2]=H[c+28>>2];c=c+32|0;b=b+32|0;if((e|0)!=(b|0)){continue}break}}H[d+12>>2]=l;H[d+8>>2]=e;H[d+4>>2]=a;H[d>>2]=j;if(!h){break h}oa(h);e=H[d+8>>2]}H[e>>2]=H[f+8>>2];H[d+8>>2]=H[d+8>>2]+4;break f}wa();v()}break b}n=f,o=pa(4092),H[n+8>>2]=o;qd(a,f+8|0);b=H[a+4>>2];j=H[b>>2];c=b+4|0;H[a+4>>2]=c;b=H[a+8>>2];k:{if((b|0)!=H[a+12>>2]){d=b;break k}k=H[a>>2];if(k>>>0>>0){e=((c-k>>2)+1|0)/-2<<2;b=b-c|0;d=va(e+c|0,c,b)+b|0;H[a+8>>2]=d;H[a+4>>2]=e+H[a+4>>2];break k}d=(b|0)==(k|0)?1:b-k>>1;if(d>>>0>=1073741824){break a}e=d<<2;h=pa(e);l=e+h|0;e=h+(d&-4)|0;d=e;l:{if((b|0)==(c|0)){break l}b=b-c|0;m=b&-4;i=b-4|0;g=(i>>>2|0)+1&7;m:{if(!g){b=e;break m}d=0;b=e;while(1){H[b>>2]=H[c>>2];c=c+4|0;b=b+4|0;d=d+1|0;if((g|0)!=(d|0)){continue}break}}d=e+m|0;if(i>>>0<28){break l}while(1){H[b>>2]=H[c>>2];H[b+4>>2]=H[c+4>>2];H[b+8>>2]=H[c+8>>2];H[b+12>>2]=H[c+12>>2];H[b+16>>2]=H[c+16>>2];H[b+20>>2]=H[c+20>>2];H[b+24>>2]=H[c+24>>2];H[b+28>>2]=H[c+28>>2];c=c+32|0;b=b+32|0;if((d|0)!=(b|0)){continue}break}}H[a+12>>2]=l;H[a+8>>2]=d;H[a+4>>2]=e;H[a>>2]=h;if(!k){break k}oa(k);d=H[a+8>>2]}H[d>>2]=j;H[a+8>>2]=H[a+8>>2]+4;break b}H[f+24>>2]=a+12;m=(d|0)==(g|0)?1:e>>1;if(m>>>0>=1073741824){break a}e=m<<2;g=pa(e);H[f+8>>2]=g;j=e+g|0;H[f+20>>2]=j;d=(h<<2)+g|0;H[f+12>>2]=d;i=pa(4092);n:{if((h|0)!=(m|0)){break n}if((l|0)>0){d=((h+1|0)/-2<<2)+d|0;H[f+12>>2]=d;break n}d=(b|0)==(c|0)?1:l>>1;if(d>>>0>=1073741824){break a}b=d<<2;e=pa(b);H[f+8>>2]=e;j=b+e|0;H[f+20>>2]=j;d=e+(d&-4)|0;H[f+12>>2]=d;oa(g);b=H[a+4>>2];c=H[a+8>>2];g=e}H[d>>2]=i;i=d+4|0;H[f+16>>2]=i;e=b;if((b|0)!=(c|0)){while(1){c=c-4|0;qd(f+8|0,c);if(H[a+4>>2]!=(c|0)){continue}break}j=H[f+20>>2];i=H[f+16>>2];d=H[f+12>>2];g=H[f+8>>2];e=c;b=H[a+8>>2]}c=H[a>>2];H[a>>2]=g;H[f+8>>2]=c;H[a+4>>2]=d;H[f+12>>2]=e;H[a+8>>2]=i;H[f+16>>2]=b;d=H[a+12>>2];H[a+12>>2]=j;H[f+20>>2]=d;if((b|0)!=(e|0)){H[f+16>>2]=((e-b|0)+3&-4)+b}if(!c){break b}oa(c)}ca=f+32|0;return}wa();v()}function Aj(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;var g=0,h=0,i=O(0),j=0,k=0,l=0,m=O(0),n=O(0),o=O(0),p=O(0),q=O(0),r=0,s=O(0),t=O(0),u=O(0),w=O(0),x=0,y=O(0),z=O(0),A=O(0),B=0;a:{b:{if((e|0)!=2){break b}H[a+64>>2]=f;H[a+72>>2]=2;e=pa(8);d=H[a+68>>2];H[a+68>>2]=e;if(d){oa(d)}H[a+8>>2]=2;x=a+32|0;e=H[x>>2];d=H[a+36>>2]-e|0;c:{if(d>>>0<=7){ya(x,2-(d>>>2|0)|0);break c}if((d|0)==8){break c}H[a+36>>2]=e+8}h=1;d=H[a+56>>2];d=H[d+4>>2]-H[d>>2]|0;if((d|0)<=0){break b}d=d>>>2|0;B=d>>>0<=1?1:d;d=0;while(1){e=H[a+56>>2];h=H[e>>2];if(H[e+4>>2]-h>>2>>>0<=d>>>0){break a}q=O(0);g=ca-48|0;ca=g;e=-1;h=H[h+(d<<2)>>2];f=-1;d:{if((h|0)==-1){break d}e=h+1|0;e=(e>>>0)%3|0?e:h-2|0;f=h-1|0;if((h>>>0)%3|0){break d}f=h+2|0}j=H[a+52>>2];h=H[j>>2];e:{f:{j=H[j+4>>2]-h>>2;l=e<<2;e=H[H[a+48>>2]+28>>2];r=H[l+e>>2];if(j>>>0<=r>>>0){break f}e=H[e+(f<<2)>>2];if(e>>>0>=j>>>0){break f}j=H[h+(e<<2)>>2];f=H[h+(r<<2)>>2];g:{if(!((j|0)>=(d|0)|(f|0)>=(d|0))){e=H[a+72>>2];h=(N(e,j)<<2)+c|0;m=O(H[h+4>>2]);e=(N(e,f)<<2)+c|0;p=O(H[e+4>>2]);y=O(H[e>>2]);n=O(H[h>>2]);if(!(y!=n|m!=p)){h=+m>2147483647;e=H[a+68>>2];if(O(P(m))>2]=m2147483647;if(O(P(n))>2]=n>2]+(d<<2)>>2];H[g+40>>2]=0;H[g+32>>2]=0;H[g+36>>2]=0;h=H[a+60>>2];if(!I[h+84|0]){e=H[H[h+68>>2]+(e<<2)>>2]}Va(h,e,F[h+24|0],g+32|0);f=H[H[a+64>>2]+(f<<2)>>2];H[g+24>>2]=0;H[g+16>>2]=0;H[g+20>>2]=0;e=H[a+60>>2];if(!I[e+84|0]){f=H[H[e+68>>2]+(f<<2)>>2]}Va(e,f,F[e+24|0],g+16|0);f=H[H[a+64>>2]+(j<<2)>>2];H[g+8>>2]=0;H[g>>2]=0;H[g+4>>2]=0;e=H[a+60>>2];if(!I[e+84|0]){f=H[H[e+68>>2]+(f<<2)>>2]}Va(e,f,F[e+24|0],g);o=L[g+24>>2];s=O(L[g+8>>2]-o);t=L[g+20>>2];u=O(L[g+4>>2]-t);A=L[g+16>>2];w=O(L[g>>2]-A);z=O(O(s*s)+O(O(u*u)+O(O(w*w)+O(0))));h:{if(H[a+88>>2]>=258){i=O(0);if(!(z>O(0))){break h}}i=O(L[g+40>>2]-o);o=O(L[g+36>>2]-t);t=O(L[g+32>>2]-A);q=O(O(O(s*i)+O(O(u*o)+O(O(w*t)+O(0))))/z);i=O(i-O(s*q));s=O(i*i);i=O(o-O(u*q));o=O(i*i);i=O(t-O(w*q));i=O(W(O(O(s+O(o+O(O(i*i)+O(0))))/z)))}f=H[a+80>>2];if(f){e=f-1|0;h=H[H[a+76>>2]+(e>>>3&536870908)>>2];H[a+80>>2]=e;m=O(m-p);o=O(O(m*q)+p);n=O(n-y);p=O(n*i);e=h>>>e&1;p=O(o+(e?p:O(-p)));i=O(i*m);k=T(+O(O(O(n*q)+y)+(e?O(-i):i))+.5);i:{if(k<-2147483648|k!=k|k>2147483647){e=H[a+68>>2];H[e>>2]=-2147483648;break i}e=H[a+68>>2];if(P(k)<2147483648){h=~~k}else{h=-2147483648}H[e>>2]=h}k=T(+p+.5);j=k>2147483647;if(P(k)<2147483648){h=~~k}else{h=-2147483648}H[e+4>>2]=k<-2147483648?-2147483648:k!=k?-2147483648:j?-2147483648:h}f=(f|0)!=0;break g}j:{if((d|0)>(f|0)){e=H[a+72>>2];h=N(f,e);break j}if((d|0)<=0){f=1;if(H[a+72>>2]<=0){break g}h=H[a+68>>2];e=0;while(1){H[h+(e<<2)>>2]=0;e=e+1|0;if((e|0)>2]){continue}break}break g}e=H[a+72>>2];h=N(e,d-1|0)}f=1;if((e|0)<=0){break g}j=H[a+68>>2];e=0;while(1){H[j+(e<<2)>>2]=H[(e+h<<2)+c>>2];e=e+1|0;if((e|0)>2]){continue}break}}ca=g+48|0;break e}Ca();v()}h=f;if(!h){return 0}k:{if(H[a+8>>2]<=0){break k}r=H[a+68>>2];j=H[x>>2];e=0;while(1){f=e<<2;g=H[f+r>>2];l=H[a+16>>2];l:{if((g|0)>(l|0)){H[f+j>>2]=l;break l}f=f+j|0;l=H[a+12>>2];if((l|0)>(g|0)){H[f>>2]=l;break l}H[f>>2]=g}e=e+1|0;g=H[a+8>>2];if((e|0)<(g|0)){continue}break}f=0;if((g|0)<=0){break k}e=d<<3;r=e+c|0;l=b+e|0;while(1){g=f<<2;e=g+r|0;g=H[g+l>>2]+H[g+j>>2]|0;H[e>>2]=g;m:{if((g|0)>H[a+16>>2]){g=g-H[a+20>>2]|0}else{if((g|0)>=H[a+12>>2]){break m}g=g+H[a+20>>2]|0}H[e>>2]=g}f=f+1|0;if((f|0)>2]){continue}break}}d=d+1|0;if((B|0)!=(d|0)){continue}break}}return h|0}Ca();v()}function kj(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;var g=0,h=0,i=O(0),j=0,k=0,l=O(0),m=O(0),n=O(0),o=O(0),p=0,q=O(0),r=O(0),s=O(0),t=O(0),u=O(0),w=0,x=O(0),y=O(0),z=0,A=O(0),B=0;a:{b:{if((e|0)!=2){break b}H[a+64>>2]=f;H[a+72>>2]=2;e=pa(8);d=H[a+68>>2];H[a+68>>2]=e;if(d){oa(d)}H[a+8>>2]=2;w=a+32|0;e=H[w>>2];d=H[a+36>>2]-e|0;c:{if(d>>>0<=7){ya(w,2-(d>>>2|0)|0);break c}if((d|0)==8){break c}H[a+36>>2]=e+8}h=1;d=H[a+56>>2];d=H[d+4>>2]-H[d>>2]|0;if((d|0)<=0){break b}d=d>>>2|0;B=d>>>0<=1?1:d;d=0;while(1){f=H[a+56>>2];e=H[f>>2];if(H[f+4>>2]-e>>2>>>0<=d>>>0){break a}q=O(0);g=ca-48|0;ca=g;h=-1;d:{e:{e=H[e+(d<<2)>>2];if((e|0)==-1){break e}j=H[a+48>>2];f=e+1|0;f=(f>>>0)%3|0?f:e-2|0;if((f|0)!=-1){h=H[H[j>>2]+(f<<2)>>2]}f=-1;e=e+((e>>>0)%3|0?-1:2)|0;if((e|0)!=-1){f=H[H[j>>2]+(e<<2)>>2]}e=H[a+52>>2];j=H[e>>2];e=H[e+4>>2]-j>>2;if(e>>>0<=h>>>0|e>>>0<=f>>>0){break e}e=H[j+(h<<2)>>2];j=H[j+(f<<2)>>2];f:{if(!((d|0)<=(e|0)|(j|0)>=(d|0))){f=H[a+72>>2];h=(N(f,j)<<2)+c|0;l=O(H[h+4>>2]);f=(N(e,f)<<2)+c|0;o=O(H[f+4>>2]);x=O(H[f>>2]);m=O(H[h>>2]);if(!(x!=m|l!=o)){h=+l>2147483647;e=H[a+68>>2];if(O(P(l))>2]=l2147483647;if(O(P(m))>2]=m>2]+(d<<2)>>2];H[g+40>>2]=0;H[g+32>>2]=0;H[g+36>>2]=0;h=H[a+60>>2];if(!I[h+84|0]){f=H[H[h+68>>2]+(f<<2)>>2]}Va(h,f,F[h+24|0],g+32|0);f=H[H[a+64>>2]+(e<<2)>>2];H[g+24>>2]=0;H[g+16>>2]=0;H[g+20>>2]=0;e=H[a+60>>2];if(!I[e+84|0]){f=H[H[e+68>>2]+(f<<2)>>2]}Va(e,f,F[e+24|0],g+16|0);h=H[H[a+64>>2]+(j<<2)>>2];H[g+8>>2]=0;H[g>>2]=0;H[g+4>>2]=0;e=H[a+60>>2];if(!I[e+84|0]){h=H[H[e+68>>2]+(h<<2)>>2]}Va(e,h,F[e+24|0],g);n=L[g+24>>2];r=O(L[g+8>>2]-n);s=L[g+20>>2];t=O(L[g+4>>2]-s);A=L[g+16>>2];u=O(L[g>>2]-A);y=O(O(r*r)+O(O(t*t)+O(O(u*u)+O(0))));g:{if(H[a+88>>2]>=258){i=O(0);if(!(y>O(0))){break g}}i=O(L[g+40>>2]-n);n=O(L[g+36>>2]-s);s=O(L[g+32>>2]-A);q=O(O(O(r*i)+O(O(t*n)+O(O(u*s)+O(0))))/y);i=O(i-O(r*q));r=O(i*i);i=O(n-O(t*q));n=O(i*i);i=O(s-O(u*q));i=O(W(O(O(r+O(n+O(O(i*i)+O(0))))/y)))}e=H[a+80>>2];if(e){f=e-1|0;h=H[H[a+76>>2]+(f>>>3&536870908)>>2];H[a+80>>2]=f;l=O(l-o);n=O(O(l*q)+o);m=O(m-x);o=O(m*i);f=h>>>f&1;o=O(n+(f?o:O(-o)));i=O(i*l);k=T(+O(O(O(m*q)+x)+(f?O(-i):i))+.5);h:{if(k<-2147483648|k!=k|k>2147483647){h=H[a+68>>2];H[h>>2]=-2147483648;break h}h=H[a+68>>2];if(P(k)<2147483648){f=~~k}else{f=-2147483648}H[h>>2]=f}k=T(+o+.5);j=k>2147483647;if(P(k)<2147483648){f=~~k}else{f=-2147483648}H[h+4>>2]=k<-2147483648?-2147483648:k!=k?-2147483648:j?-2147483648:f}h=(e|0)!=0;break f}i:{if((d|0)>(e|0)){f=H[a+72>>2];e=N(e,f);break i}if((d|0)<=0){h=1;if(H[a+72>>2]<=0){break f}e=H[a+68>>2];f=0;while(1){H[e+(f<<2)>>2]=0;f=f+1|0;if((f|0)>2]){continue}break}break f}f=H[a+72>>2];e=N(f,d-1|0)}h=1;if((f|0)<=0){break f}j=H[a+68>>2];f=0;while(1){H[j+(f<<2)>>2]=H[(e+f<<2)+c>>2];f=f+1|0;if((f|0)>2]){continue}break}}ca=g+48|0;break d}Ca();v()}if(!h){return 0}j:{if(H[a+8>>2]<=0){break j}z=H[a+68>>2];j=H[w>>2];e=0;while(1){f=e<<2;g=H[f+z>>2];p=H[a+16>>2];k:{if((g|0)>(p|0)){H[f+j>>2]=p;break k}f=f+j|0;p=H[a+12>>2];if((p|0)>(g|0)){H[f>>2]=p;break k}H[f>>2]=g}e=e+1|0;g=H[a+8>>2];if((e|0)<(g|0)){continue}break}f=0;if((g|0)<=0){break j}e=d<<3;z=e+c|0;p=b+e|0;while(1){g=f<<2;e=g+z|0;g=H[g+p>>2]+H[g+j>>2]|0;H[e>>2]=g;l:{if((g|0)>H[a+16>>2]){g=g-H[a+20>>2]|0}else{if((g|0)>=H[a+12>>2]){break l}g=g+H[a+20>>2]|0}H[e>>2]=g}f=f+1|0;if((f|0)>2]){continue}break}}d=d+1|0;if((B|0)!=(d|0)){continue}break}}return h|0}Ca();v()}function Of(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0;f=ca-704|0;ca=f;n=1;a:{b:{c:{d:{if(J[b+38>>1]<515){break d}n=0;c=H[b+20>>2];d=H[b+12>>2];g=H[b+16>>2];if((c|0)>=(d|0)&g>>>0>=K[b+8>>2]|(c|0)>(d|0)){break d}p=I[H[b>>2]+g|0];g=g+1|0;c=g?c:c+1|0;H[b+16>>2]=g;H[b+20>>2]=c;g=H[H[(ea[H[H[a>>2]+28>>2]](a)|0)+4>>2]+80>>2];c=ea[H[H[a>>2]+24>>2]](a)|0;H[f+40>>2]=0;H[f+32>>2]=0;H[f+36>>2]=0;if(c){if(c>>>0>=214748365){break c}c=N(c,20);d=pa(c);H[f+32>>2]=d;H[f+40>>2]=c+d;c=c-20|0;c=(c-((c>>>0)%20|0)|0)+20|0;q=f,r=ra(d,0,c)+c|0,H[q+36>>2]=r}e:{if((ea[H[H[a>>2]+24>>2]](a)|0)>0){while(1){c=ea[H[H[a>>2]+20>>2]](a,l)|0;c=H[H[H[(ea[H[H[a>>2]+28>>2]](a)|0)+4>>2]+8>>2]+(c<<2)>>2];mb(c,g);F[c+84|0]=1;H[c+72>>2]=H[c+68>>2];d=H[c+28>>2];if(d>>>0>9){break e}f:{g:{h:{e=1<>2],d,6,0,i,i>>31);c=jc(pa(96),e);H[f>>2]=c;F[c+84|0]=1;H[c+72>>2]=H[c+68>>2];mb(c,g);c=H[a+64>>2];if(c>>>0>=K[a+68>>2]){break h}d=H[f>>2];H[f>>2]=0;H[c>>2]=d;c=c+4|0;H[a+64>>2]=c;break g}j=0;if(!I[c+24|0]){break f}while(1){d=H[a+52>>2];i=H[a+56>>2];i:{if(d>>>0>>0){H[d>>2]=0;H[a+52>>2]=d+4;break i}e=d;d=H[a+48>>2];m=e-d|0;k=m>>2;e=k+1|0;if(e>>>0>=1073741824){break b}o=k<<2;i=i-d|0;k=i>>>1|0;e=i>>>0>=2147483644?1073741823:e>>>0>>0?k:e;if(e){if(e>>>0>=1073741824){break a}i=pa(e<<2)}else{i=0}k=o+i|0;H[k>>2]=0;o=e<<2;e=va(i,d,m);H[a+56>>2]=o+e;H[a+52>>2]=k+4;H[a+48>>2]=e;if(!d){break i}oa(d)}j=j+1|0;if(j>>>0>2];i=H[a+64>>2]-e>>2;d=i+1|0;if(d>>>0<1073741824){e=H[a+68>>2]-e|0;j=e>>>1|0;e=e>>>0>=2147483644?1073741823:d>>>0>>0?j:d;if(e){if(e>>>0>=1073741824){break l}c=pa(e<<2)}j=H[f>>2];H[f>>2]=0;d=(i<<2)+c|0;H[d>>2]=j;e=(e<<2)+c|0;i=d+4|0;c=H[a+64>>2];j=H[a+60>>2];if((c|0)==(j|0)){break k}while(1){c=c-4|0;m=H[c>>2];H[c>>2]=0;d=d-4|0;H[d>>2]=m;if((c|0)!=(j|0)){continue}break}H[a+68>>2]=e;e=H[a+64>>2];H[a+64>>2]=i;c=H[a+60>>2];H[a+60>>2]=d;if((c|0)==(e|0)){break j}while(1){e=e-4|0;d=H[e>>2];H[e>>2]=0;if(d){Ga(d)}if((c|0)!=(e|0)){continue}break}break j}sa();v()}wa();v()}H[a+68>>2]=e;H[a+64>>2]=i;H[a+60>>2]=d}if(c){oa(c)}c=H[a+64>>2]}c=H[c-4>>2];d=H[f>>2];H[f>>2]=0;if(!d){break f}Ga(d)}i=H[c+28>>2];d=i-1|0;if(d>>>0<=10){e=H[(d<<2)+13584>>2]}else{e=-1}d=H[f+32>>2]+N(l,20)|0;j=I[c+24|0];H[d+16>>2]=j;H[d+12>>2]=(e|0)>0?e:0;H[d+8>>2]=i;H[d+4>>2]=h;H[d>>2]=c;h=h+j|0;l=l+1|0;if((ea[H[H[a>>2]+24>>2]](a)|0)>(l|0)){continue}break}}a=Ac(f,f+32|0);m:{n:{o:{switch(p|0){case 0:c=wb(f+48|0,h);b=Bd(c,b,a,g);h=H[c+8>>2];xb(c);if(!b){break m}if((h|0)==(g|0)){break n}break m;case 1:c=wb(f+48|0,h);b=zd(c,b,a,g);h=H[c+8>>2];xb(c);if(!b){break m}if((h|0)==(g|0)){break n}break m;case 2:c=ub(f+48|0,h);b=yd(c,b,a,g);h=H[c+8>>2];vb(c);if(!b){break m}if((h|0)==(g|0)){break n}break m;case 3:c=ub(f+48|0,h);b=xd(c,b,a,g);h=H[c+8>>2];vb(c);if(!b){break m}if((h|0)==(g|0)){break n}break m;case 4:c=$a(f+48|0,h);b=wd(c,b,a,g);h=H[c+8>>2];ab(c);if(!b){break m}if((h|0)==(g|0)){break n}break m;case 5:c=$a(f+48|0,h);b=vd(c,b,a,g);h=H[c+8>>2];ab(c);if(!b){break m}if((h|0)==(g|0)){break n}break m;case 6:break o;default:break m}}c=$a(f+48|0,h);b=ud(c,b,a,g);h=H[c+8>>2];ab(c);if(!b|(h|0)!=(g|0)){break m}}n=1}b=H[a+16>>2];if(b){H[a+20>>2]=b;oa(b)}b=H[a>>2];if(!b){break e}H[a+4>>2]=b;oa(b)}a=H[f+32>>2];if(!a){break d}H[f+36>>2]=a;oa(a)}ca=f+704|0;return n|0}sa();v()}sa();v()}wa();v()}function Zi(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0,h=0,i=0;e=ca-32|0;ca=e;a:{b:{switch(c-2|0){case 0:c=H[a+4>>2];f=H[a+12>>2];H[e+24>>2]=-1;H[e+16>>2]=-1;H[e+20>>2]=1065353216;H[e+8>>2]=-1;H[e+12>>2]=-1;if((b|0)==-2){break a}i=H[H[H[c+4>>2]+8>>2]+(f<<2)>>2];if((ea[H[H[c>>2]+8>>2]](c)|0)==1){h=H[H[H[c+4>>2]+8>>2]+(f<<2)>>2];c:{if((ea[H[H[c>>2]+8>>2]](c)|0)!=1|b-1>>>0>5){break c}g=ea[H[H[c>>2]+36>>2]](c)|0;a=ea[H[H[c>>2]+44>>2]](c,f)|0;if(!g|!a){break c}f=ea[H[H[c>>2]+40>>2]](c,f)|0;d:{if(f){if((b|0)!=6){break c}b=H[c+44>>2];d=pa(112);H[d+4>>2]=h;c=H[e+12>>2];H[d+8>>2]=H[e+8>>2];H[d+12>>2]=c;c=H[e+20>>2];H[d+16>>2]=H[e+16>>2];H[d+20>>2]=c;H[d+24>>2]=H[e+24>>2];H[d+40>>2]=a;c=a+12|0;H[d+36>>2]=c;H[d+32>>2]=f;H[d+28>>2]=b;H[d+68>>2]=a;H[d- -64>>2]=c;H[d+60>>2]=f;H[d+56>>2]=b;H[d+48>>2]=0;H[d+52>>2]=0;H[d>>2]=7144;H[d+88>>2]=1065353216;H[d+92>>2]=-1;H[d+80>>2]=-1;H[d+84>>2]=-1;H[d+72>>2]=1;H[d+76>>2]=-1;H[d+44>>2]=7668;a=d+96|0;break d}if((b|0)!=6){break c}b=H[c+44>>2];d=pa(112);H[d+4>>2]=h;c=H[e+12>>2];H[d+8>>2]=H[e+8>>2];H[d+12>>2]=c;c=H[e+20>>2];H[d+16>>2]=H[e+16>>2];H[d+20>>2]=c;H[d+24>>2]=H[e+24>>2];H[d+40>>2]=a;c=a+12|0;H[d+36>>2]=c;H[d+32>>2]=g;H[d+28>>2]=b;H[d+68>>2]=a;H[d- -64>>2]=c;H[d+60>>2]=g;H[d+56>>2]=b;H[d+48>>2]=0;H[d+52>>2]=0;H[d>>2]=8080;H[d+88>>2]=1065353216;H[d+92>>2]=-1;H[d+80>>2]=-1;H[d+84>>2]=-1;H[d+72>>2]=1;H[d+76>>2]=-1;H[d+44>>2]=8472;a=d+96|0}H[a>>2]=0;H[a+4>>2]=0;F[a+5|0]=0;F[a+6|0]=0;F[a+7|0]=0;F[a+8|0]=0;F[a+9|0]=0;F[a+10|0]=0;F[a+11|0]=0;F[a+12|0]=0}if(d){break a}}d=pa(28);H[d+4>>2]=i;a=H[e+12>>2];H[d+8>>2]=H[e+8>>2];H[d+12>>2]=a;a=H[e+20>>2];H[d+16>>2]=H[e+16>>2];H[d+20>>2]=a;H[d+24>>2]=H[e+24>>2];H[d>>2]=8860;break a;case 1:break b;default:break a}}c=H[a+4>>2];f=H[a+12>>2];H[e+24>>2]=-1;H[e+16>>2]=-1;H[e+20>>2]=1065353216;H[e+8>>2]=-1;H[e+12>>2]=-1;if((b|0)==-2){break a}i=H[H[H[c+4>>2]+8>>2]+(f<<2)>>2];if((ea[H[H[c>>2]+8>>2]](c)|0)==1){h=H[H[H[c+4>>2]+8>>2]+(f<<2)>>2];e:{if((ea[H[H[c>>2]+8>>2]](c)|0)!=1|b-1>>>0>5){break e}g=ea[H[H[c>>2]+36>>2]](c)|0;a=ea[H[H[c>>2]+44>>2]](c,f)|0;if(!g|!a){break e}f=ea[H[H[c>>2]+40>>2]](c,f)|0;f:{if(f){if((b|0)!=6){break e}b=H[c+44>>2];d=pa(112);H[d+4>>2]=h;c=H[e+12>>2];H[d+8>>2]=H[e+8>>2];H[d+12>>2]=c;c=H[e+20>>2];H[d+16>>2]=H[e+16>>2];H[d+20>>2]=c;H[d+24>>2]=H[e+24>>2];H[d+40>>2]=a;c=a+12|0;H[d+36>>2]=c;H[d+32>>2]=f;H[d+28>>2]=b;H[d+68>>2]=a;H[d- -64>>2]=c;H[d+60>>2]=f;H[d+56>>2]=b;H[d+48>>2]=0;H[d+52>>2]=0;H[d>>2]=9028;H[d+88>>2]=1065353216;H[d+92>>2]=-1;H[d+80>>2]=-1;H[d+84>>2]=-1;H[d+72>>2]=1;H[d+76>>2]=-1;H[d+44>>2]=9592;a=d+96|0;break f}if((b|0)!=6){break e}b=H[c+44>>2];d=pa(112);H[d+4>>2]=h;c=H[e+12>>2];H[d+8>>2]=H[e+8>>2];H[d+12>>2]=c;c=H[e+20>>2];H[d+16>>2]=H[e+16>>2];H[d+20>>2]=c;H[d+24>>2]=H[e+24>>2];H[d+40>>2]=a;c=a+12|0;H[d+36>>2]=c;H[d+32>>2]=g;H[d+28>>2]=b;H[d+68>>2]=a;H[d- -64>>2]=c;H[d+60>>2]=g;H[d+56>>2]=b;H[d+48>>2]=0;H[d+52>>2]=0;H[d>>2]=10032;H[d+88>>2]=1065353216;H[d+92>>2]=-1;H[d+80>>2]=-1;H[d+84>>2]=-1;H[d+72>>2]=1;H[d+76>>2]=-1;H[d+44>>2]=10452;a=d+96|0}H[a>>2]=0;H[a+4>>2]=0;F[a+5|0]=0;F[a+6|0]=0;F[a+7|0]=0;F[a+8|0]=0;F[a+9|0]=0;F[a+10|0]=0;F[a+11|0]=0;F[a+12|0]=0}if(d){break a}}d=pa(28);H[d+4>>2]=i;a=H[e+12>>2];H[d+8>>2]=H[e+8>>2];H[d+12>>2]=a;a=H[e+20>>2];H[d+16>>2]=H[e+16>>2];H[d+20>>2]=a;H[d+24>>2]=H[e+24>>2];H[d>>2]=10864}ca=e+32|0;return d|0}function Ki(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;var e=O(0),f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=O(0),p=0,q=0,r=0,s=0,t=0,u=0,v=0,w=0,x=0;if(H[c>>2]==H[c+4>>2]){m=H[d+80>>2];u=ca-16|0;ca=u;g=H[a+4>>2];k=I[b+24|0];h=H[d+48>>2];n=H[H[d>>2]>>2];c=u+8|0;H[c>>2]=1065353216;d=c;L[c>>2]=O(-1<>2];c=pa(k<<2);a:{if(!m|!k){break a}p=h+n|0;o=L[d>>2];n=H[a+8>>2];v=H[b>>2];d=H[b+48>>2];g=H[b+40>>2];w=H[b+44>>2];if(!I[b+84|0]){f=H[b+68>>2];s=k&254;t=k&1;a=0;while(1){b=H[v>>2];l=Rj(g,w,H[f+(i<<2)>>2],0)+d|0;h=qa(c,b+l|0,g);b=0;q=0;if((k|0)!=1){while(1){l=p+(a<<2)|0;j=b<<2;e=O(T(O(O(o*O(L[j+h>>2]-L[n+j>>2]))+O(.5))));b:{if(O(P(e))>2]=r;j=j|4;e=O(T(O(O(o*O(L[j+h>>2]-L[n+j>>2]))+O(.5))));c:{if(O(P(e))>2]=j;b=b+2|0;a=a+2|0;q=q+2|0;if((s|0)!=(q|0)){continue}break}}if(t){l=p+(a<<2)|0;b=b<<2;e=O(T(O(O(o*O(L[b+h>>2]-L[b+n>>2]))+O(.5))));d:{if(O(P(e))>2]=b;a=a+1|0}i=i+1|0;if((m|0)!=(i|0)){continue}break}break a}s=k&254;t=k&1;a=0;while(1){b=H[v>>2];h=Rj(g,w,i,l)+d|0;j=qa(c,b+h|0,g);b=0;q=0;if((k|0)!=1){while(1){h=p+(a<<2)|0;f=b<<2;e=O(T(O(O(o*O(L[f+j>>2]-L[f+n>>2]))+O(.5))));e:{if(O(P(e))>2]=r;f=f|4;e=O(T(O(O(o*O(L[f+j>>2]-L[f+n>>2]))+O(.5))));f:{if(O(P(e))>2]=f;b=b+2|0;a=a+2|0;q=q+2|0;if((s|0)!=(q|0)){continue}break}}if(t){h=p+(a<<2)|0;b=b<<2;e=O(T(O(O(o*O(L[b+j>>2]-L[b+n>>2]))+O(.5))));g:{if(O(P(e))>2]=b;a=a+1|0}b=l;i=i+1|0;b=i?b:b+1|0;l=b;if((i|0)!=(m|0)|b){continue}break}}oa(c);ca=u+16|0;return 1}j=ca-16|0;ca=j;m=H[a+4>>2];i=I[b+24|0];g=H[d+48>>2];h=H[H[d>>2]>>2];d=j+8|0;H[d>>2]=1065353216;l=d;L[d>>2]=O(-1<>2];d=pa(i<<2);m=H[c+4>>2];q=H[c>>2];h:{if(!i|(m|0)==(q|0)){break h}n=h+g|0;c=m-q>>2;u=c>>>0<=1?1:c;o=L[l>>2];h=H[a+8>>2];v=H[b>>2];l=H[b+48>>2];m=H[b+40>>2];w=H[b+44>>2];if(I[b+84|0]){s=i&254;t=i&1;a=0;c=0;while(1){b=H[v>>2];g=Rj(m,w,H[q+(c<<2)>>2],0)+l|0;p=qa(d,b+g|0,m);b=0;k=0;if((i|0)!=1){while(1){g=n+(a<<2)|0;f=b<<2;e=O(T(O(O(o*O(L[f+p>>2]-L[h+f>>2]))+O(.5))));i:{if(O(P(e))>2]=r;f=f|4;e=O(T(O(O(o*O(L[f+p>>2]-L[h+f>>2]))+O(.5))));j:{if(O(P(e))>2]=f;b=b+2|0;a=a+2|0;k=k+2|0;if((s|0)!=(k|0)){continue}break}}if(t){g=n+(a<<2)|0;b=b<<2;e=O(T(O(O(o*O(L[b+p>>2]-L[b+h>>2]))+O(.5))));k:{if(O(P(e))>2]=b;a=a+1|0}c=c+1|0;if((u|0)!=(c|0)){continue}break}break h}s=H[b+68>>2];t=i&254;x=i&1;a=0;c=0;while(1){b=H[v>>2];g=Rj(m,w,H[s+(H[q+(c<<2)>>2]<<2)>>2],0)+l|0;p=qa(d,b+g|0,m);b=0;k=0;if((i|0)!=1){while(1){g=n+(a<<2)|0;f=b<<2;e=O(T(O(O(o*O(L[f+p>>2]-L[h+f>>2]))+O(.5))));l:{if(O(P(e))>2]=r;f=f|4;e=O(T(O(O(o*O(L[f+p>>2]-L[h+f>>2]))+O(.5))));m:{if(O(P(e))>2]=f;b=b+2|0;a=a+2|0;k=k+2|0;if((t|0)!=(k|0)){continue}break}}if(x){g=n+(a<<2)|0;b=b<<2;e=O(T(O(O(o*O(L[b+p>>2]-L[b+h>>2]))+O(.5))));n:{if(O(P(e))>2]=b;a=a+1|0}c=c+1|0;if((u|0)!=(c|0)){continue}break}}oa(d);ca=j+16|0;return 1}function dd(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0;c=H[a+4>>2];e=H[a>>2];f=(c-e|0)/144|0;if(f>>>0>>0){e=a;b=b-f|0;h=H[a+8>>2];c=H[a+4>>2];a:{if(b>>>0<=(h-c|0)/144>>>0){b:{if(!b){break b}a=c;f=b&7;if(f){while(1){Ia(a);a=a+144|0;d=d+1|0;if((f|0)!=(d|0)){continue}break}}c=N(b,144)+c|0;if((b-1&268435455)>>>0<7){break b}while(1){Ia(a);Ia(a+144|0);Ia(a+288|0);Ia(a+432|0);Ia(a+576|0);Ia(a+720|0);Ia(a+864|0);Ia(a+1008|0);a=a+1152|0;if((c|0)!=(a|0)){continue}break}}H[e+4>>2]=c;break a}c:{d:{e:{a=c;c=H[e>>2];i=(a-c|0)/144|0;a=i+b|0;if(a>>>0<29826162){c=(h-c|0)/144|0;f=c<<1;f=c>>>0>=14913080?29826161:a>>>0>>0?f:a;if(f){if(f>>>0>=29826162){break e}g=pa(N(f,144))}c=N(i,144)+g|0;a=c;h=b&7;if(h){while(1){Ia(a);a=a+144|0;d=d+1|0;if((h|0)!=(d|0)){continue}break}}h=N(b,144)+c|0;if((b-1&268435455)>>>0>=7){while(1){Ia(a);Ia(a+144|0);Ia(a+288|0);Ia(a+432|0);Ia(a+576|0);Ia(a+720|0);Ia(a+864|0);Ia(a+1008|0);a=a+1152|0;if((h|0)!=(a|0)){continue}break}}b=N(f,144)+g|0;d=H[e+4>>2];f=H[e>>2];if((d|0)==(f|0)){break d}while(1){c=c-144|0;d=d-144|0;a=d;H[c>>2]=H[a>>2];H[c+4>>2]=H[a+4>>2];H[c+8>>2]=H[a+8>>2];H[c+12>>2]=H[a+12>>2];H[a+12>>2]=0;H[a+4>>2]=0;H[a+8>>2]=0;H[c+16>>2]=H[a+16>>2];H[c+20>>2]=H[a+20>>2];H[c+24>>2]=H[a+24>>2];H[a+24>>2]=0;H[a+16>>2]=0;H[a+20>>2]=0;g=I[a+28|0];H[c+40>>2]=0;H[c+32>>2]=0;H[c+36>>2]=0;F[c+28|0]=g;H[c+32>>2]=H[a+32>>2];H[c+36>>2]=H[a+36>>2];H[c+40>>2]=H[a+40>>2];H[a+40>>2]=0;H[a+32>>2]=0;H[a+36>>2]=0;H[c+52>>2]=0;H[c+44>>2]=0;H[c+48>>2]=0;H[c+44>>2]=H[a+44>>2];H[c+48>>2]=H[a+48>>2];H[c+52>>2]=H[a+52>>2];H[a+52>>2]=0;H[a+44>>2]=0;H[a+48>>2]=0;g=c- -64|0;H[g>>2]=0;H[c+56>>2]=0;H[c+60>>2]=0;H[c+56>>2]=H[a+56>>2];H[c+60>>2]=H[a+60>>2];i=g;g=a- -64|0;H[i>>2]=H[g>>2];H[g>>2]=0;H[a+56>>2]=0;H[a+60>>2]=0;H[c+68>>2]=H[a+68>>2];g=H[a+72>>2];H[c+84>>2]=0;H[c+76>>2]=0;H[c+80>>2]=0;H[c+72>>2]=g;H[c+76>>2]=H[a+76>>2];H[c+80>>2]=H[a+80>>2];H[c+84>>2]=H[a+84>>2];H[a+84>>2]=0;H[a+76>>2]=0;H[a+80>>2]=0;H[c+96>>2]=0;H[c+88>>2]=0;H[c+92>>2]=0;H[c+88>>2]=H[a+88>>2];H[c+92>>2]=H[a+92>>2];H[c+96>>2]=H[a+96>>2];H[a+96>>2]=0;H[a+88>>2]=0;H[a+92>>2]=0;g=I[a+100|0];H[c+112>>2]=0;H[c+104>>2]=0;H[c+108>>2]=0;F[c+100|0]=g;H[c+104>>2]=H[a+104>>2];H[c+108>>2]=H[a+108>>2];H[c+112>>2]=H[a+112>>2];H[a+112>>2]=0;H[a+104>>2]=0;H[a+108>>2]=0;H[c+124>>2]=0;H[c+116>>2]=0;H[c+120>>2]=0;H[c+116>>2]=H[a+116>>2];H[c+120>>2]=H[a+120>>2];H[c+124>>2]=H[a+124>>2];H[a+124>>2]=0;H[a+116>>2]=0;H[a+120>>2]=0;g=H[a+128>>2];H[c+140>>2]=0;H[c+132>>2]=0;H[c+136>>2]=0;H[c+128>>2]=g;H[c+132>>2]=H[a+132>>2];H[c+136>>2]=H[a+136>>2];H[c+140>>2]=H[a+140>>2];H[a+140>>2]=0;H[a+132>>2]=0;H[a+136>>2]=0;if((a|0)!=(f|0)){continue}break}H[e+8>>2]=b;a=H[e+4>>2];H[e+4>>2]=h;d=H[e>>2];H[e>>2]=c;if((a|0)==(d|0)){break c}while(1){b=a-144|0;c=H[b+132>>2];if(c){H[a-8>>2]=c;oa(c)}c=H[a-28>>2];if(c){H[a-24>>2]=c;oa(c)}c=H[a-40>>2];if(c){H[a-36>>2]=c;oa(c)}oc(a-140|0);a=b;if((d|0)!=(a|0)){continue}break}break c}sa();v()}wa();v()}H[e+8>>2]=b;H[e+4>>2]=h;H[e>>2]=c}if(d){oa(d)}}return}if(b>>>0>>0){e=e+N(b,144)|0;if((e|0)!=(c|0)){while(1){b=c-144|0;d=H[b+132>>2];if(d){H[c-8>>2]=d;oa(d)}d=H[c-28>>2];if(d){H[c-24>>2]=d;oa(d)}d=H[c-40>>2];if(d){H[c-36>>2]=d;oa(d)}oc(c-140|0);c=b;if((e|0)!=(c|0)){continue}break}}H[a+4>>2]=e}}function Pe(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0;f=ca-80|0;ca=f;e=H[c+36>>2];H[f+72>>2]=H[c+32>>2];H[f+76>>2]=e;g=H[c+28>>2];e=f- -64|0;H[e>>2]=H[c+24>>2];H[e+4>>2]=g;e=H[c+20>>2];H[f+56>>2]=H[c+16>>2];H[f+60>>2]=e;e=H[c+12>>2];H[f+48>>2]=H[c+8>>2];H[f+52>>2]=e;e=H[c+4>>2];H[f+40>>2]=H[c>>2];H[f+44>>2]=e;nc(a,f+40|0,f+24|0);a:{if(H[a>>2]){break a}if(F[a+15|0]<0){oa(H[a+4>>2])}if(I[f+31|0]){b=pa(32);F[b+27|0]=0;c=I[1521]|I[1522]<<8|(I[1523]<<16|I[1524]<<24);F[b+23|0]=c;F[b+24|0]=c>>>8;F[b+25|0]=c>>>16;F[b+26|0]=c>>>24;c=I[1518]|I[1519]<<8|(I[1520]<<16|I[1521]<<24);d=I[1514]|I[1515]<<8|(I[1516]<<16|I[1517]<<24);F[b+16|0]=d;F[b+17|0]=d>>>8;F[b+18|0]=d>>>16;F[b+19|0]=d>>>24;F[b+20|0]=c;F[b+21|0]=c>>>8;F[b+22|0]=c>>>16;F[b+23|0]=c>>>24;c=I[1510]|I[1511]<<8|(I[1512]<<16|I[1513]<<24);d=I[1506]|I[1507]<<8|(I[1508]<<16|I[1509]<<24);F[b+8|0]=d;F[b+9|0]=d>>>8;F[b+10|0]=d>>>16;F[b+11|0]=d>>>24;F[b+12|0]=c;F[b+13|0]=c>>>8;F[b+14|0]=c>>>16;F[b+15|0]=c>>>24;c=I[1502]|I[1503]<<8|(I[1504]<<16|I[1505]<<24);d=I[1498]|I[1499]<<8|(I[1500]<<16|I[1501]<<24);F[b|0]=d;F[b+1|0]=d>>>8;F[b+2|0]=d>>>16;F[b+3|0]=d>>>24;F[b+4|0]=c;F[b+5|0]=c>>>8;F[b+6|0]=c>>>16;F[b+7|0]=c>>>24;H[a>>2]=-1;za(a+4|0,b,27);oa(b);break a}i=ca-16|0;ca=i;b:{c:{switch(F[f+32|0]){case 0:e=pa(44);H[e>>2]=0;H[e+4>>2]=0;H[e+40>>2]=0;H[e+32>>2]=0;H[e+36>>2]=0;H[e+24>>2]=0;H[e+28>>2]=0;H[e+16>>2]=0;H[e+20>>2]=0;H[e+8>>2]=0;H[e+12>>2]=0;e=Vc(e);H[e>>2]=13496;H[f+8>>2]=0;H[f+12>>2]=0;H[f>>2]=0;H[f+4>>2]=0;H[f+16>>2]=e;break b;case 1:e=pa(44);H[e>>2]=0;H[e+4>>2]=0;H[e+40>>2]=0;H[e+32>>2]=0;H[e+36>>2]=0;H[e+24>>2]=0;H[e+28>>2]=0;H[e+16>>2]=0;H[e+20>>2]=0;H[e+8>>2]=0;H[e+12>>2]=0;e=Vc(e);H[e>>2]=13404;H[f+8>>2]=0;H[f+12>>2]=0;H[f>>2]=0;H[f+4>>2]=0;H[f+16>>2]=e;break b;default:break c}}g=pa(32);F[g+28|0]=0;e=I[1550]|I[1551]<<8|(I[1552]<<16|I[1553]<<24);F[g+24|0]=e;F[g+25|0]=e>>>8;F[g+26|0]=e>>>16;F[g+27|0]=e>>>24;e=I[1546]|I[1547]<<8|(I[1548]<<16|I[1549]<<24);h=I[1542]|I[1543]<<8|(I[1544]<<16|I[1545]<<24);F[g+16|0]=h;F[g+17|0]=h>>>8;F[g+18|0]=h>>>16;F[g+19|0]=h>>>24;F[g+20|0]=e;F[g+21|0]=e>>>8;F[g+22|0]=e>>>16;F[g+23|0]=e>>>24;e=I[1538]|I[1539]<<8|(I[1540]<<16|I[1541]<<24);h=I[1534]|I[1535]<<8|(I[1536]<<16|I[1537]<<24);F[g+8|0]=h;F[g+9|0]=h>>>8;F[g+10|0]=h>>>16;F[g+11|0]=h>>>24;F[g+12|0]=e;F[g+13|0]=e>>>8;F[g+14|0]=e>>>16;F[g+15|0]=e>>>24;e=I[1530]|I[1531]<<8|(I[1532]<<16|I[1533]<<24);h=I[1526]|I[1527]<<8|(I[1528]<<16|I[1529]<<24);F[g|0]=h;F[g+1|0]=h>>>8;F[g+2|0]=h>>>16;F[g+3|0]=h>>>24;F[g+4|0]=e;F[g+5|0]=e>>>8;F[g+6|0]=e>>>16;F[g+7|0]=e>>>24;H[i>>2]=-1;e=i|4;za(e,g,28);j=F[i+15|0];H[f>>2]=H[i>>2];h=f+4|0;d:{if((j|0)>=0){j=H[e+4>>2];H[h>>2]=H[e>>2];H[h+4>>2]=j;H[h+8>>2]=H[e+8>>2];H[f+16>>2]=0;break d}za(h,H[i+4>>2],H[i+8>>2]);e=F[i+15|0];H[f+16>>2]=0;if((e|0)>=0){break d}oa(H[i+4>>2])}oa(g)}ca=i+16|0;e=H[f>>2];e:{if(e){H[a>>2]=e;a=a+4|0;if(F[f+15|0]>=0){b=f|4;c=H[b+4>>2];H[a>>2]=H[b>>2];H[a+4>>2]=c;H[a+8>>2]=H[b+8>>2];break e}za(a,H[f+4>>2],H[f+8>>2]);break e}e=H[f+16>>2];H[f+16>>2]=0;te(a,e,b,c,d);if(!H[a>>2]){if(F[a+15|0]<0){oa(H[a+4>>2])}H[a>>2]=0;H[a+4>>2]=0;H[a+8>>2]=0;H[a+12>>2]=0}ea[H[H[e>>2]+4>>2]](e)}a=H[f+16>>2];H[f+16>>2]=0;if(a){ea[H[H[a>>2]+4>>2]](a)}if(F[f+15|0]>=0){break a}oa(H[f+4>>2])}ca=f+80|0}function Ic(a){var b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0;H[a+56>>2]=H[a+52>>2];H[a+44>>2]=H[a+40>>2];b=H[a+64>>2];c=H[b+24>>2];if((c|0)==H[b+28>>2]){return 1}a:{b:{c:{while(1){g=i;i=H[(k<<2)+c>>2];d:{if((i|0)==-1){i=g;break d}b=H[a+56>>2];e:{if((b|0)!=H[a+60>>2]){H[b>>2]=g;H[a+56>>2]=b+4;break e}d=H[a+52>>2];e=b-d|0;h=e>>2;c=h+1|0;if(c>>>0>=1073741824){break c}f=e>>>1|0;f=e>>>0>=2147483644?1073741823:c>>>0>>0?f:c;if(f){if(f>>>0>=1073741824){break b}e=pa(f<<2)}else{e=0}c=e+(h<<2)|0;H[c>>2]=g;h=c+4|0;if((b|0)!=(d|0)){while(1){c=c-4|0;b=b-4|0;H[c>>2]=H[b>>2];if((b|0)!=(d|0)){continue}break}}H[a+60>>2]=e+(f<<2);H[a+56>>2]=h;H[a+52>>2]=c;if(!d){break e}oa(d)}f:{g:{if(!(H[H[a+12>>2]+(k>>>3&536870908)>>2]>>>k&1)){break g}e=i+1|0;e=(e>>>0)%3|0?e:i-2|0;if((e|0)==-1|H[H[a>>2]+(e>>>3&536870908)>>2]>>>e&1){break g}e=H[H[H[a+64>>2]+12>>2]+(e<<2)>>2];if((e|0)==-1){break g}b=e+1|0;b=(b>>>0)%3|0?b:e-2|0;if((b|0)==-1){break g}c=H[a+64>>2];f=H[a>>2];while(1){e=b;b=-1;d=e+1|0;d=(d>>>0)%3|0?d:e-2|0;h:{if((d|0)==-1|H[f+(d>>>3&536870908)>>2]>>>d&1){break h}d=H[H[c+12>>2]+(d<<2)>>2];if((d|0)==-1){break h}b=d+1|0;b=(b>>>0)%3|0?b:d-2|0}if((b|0)!=(i|0)){if((b|0)==-1){break f}continue}break}return 0}e=i}H[H[a+28>>2]+(e<<2)>>2]=g;b=H[a+44>>2];i:{if((b|0)!=H[a+48>>2]){H[b>>2]=e;H[a+44>>2]=b+4;break i}d=H[a+40>>2];i=b-d|0;h=i>>2;c=h+1|0;if(c>>>0>=1073741824){break a}f=i>>>1|0;f=i>>>0>=2147483644?1073741823:c>>>0>>0?f:c;if(f){if(f>>>0>=1073741824){break b}i=pa(f<<2)}else{i=0}c=i+(h<<2)|0;H[c>>2]=e;h=c+4|0;if((b|0)!=(d|0)){while(1){c=c-4|0;b=b-4|0;H[c>>2]=H[b>>2];if((b|0)!=(d|0)){continue}break}}H[a+48>>2]=i+(f<<2);H[a+44>>2]=h;H[a+40>>2]=c;if(!d){break i}oa(d)}i=g+1|0;b=H[a+64>>2];if((e|0)==-1){break d}j:{if((e>>>0)%3|0){c=e-1|0;break j}c=e+2|0;if((c|0)==-1){break d}}d=H[H[b+12>>2]+(c<<2)>>2];if((d|0)==-1){break d}f=d+((d>>>0)%3|0?-1:2)|0;if((f|0)==-1|(e|0)==(f|0)){break d}while(1){b=f+1|0;b=(b>>>0)%3|0?b:f-2|0;if(H[H[a>>2]+(b>>>3&536870908)>>2]>>>b&1){b=H[a+56>>2];k:{if((b|0)!=H[a+60>>2]){H[b>>2]=i;H[a+56>>2]=b+4;break k}d=H[a+52>>2];g=b-d|0;j=g>>2;c=j+1|0;if(c>>>0>=1073741824){break c}h=g>>>1|0;h=g>>>0>=2147483644?1073741823:c>>>0>>0?h:c;if(h){if(h>>>0>=1073741824){break b}g=pa(h<<2)}else{g=0}c=g+(j<<2)|0;H[c>>2]=i;j=c+4|0;if((b|0)!=(d|0)){while(1){c=c-4|0;b=b-4|0;H[c>>2]=H[b>>2];if((b|0)!=(d|0)){continue}break}}H[a+60>>2]=g+(h<<2);H[a+56>>2]=j;H[a+52>>2]=c;if(!d){break k}oa(d)}d=i+1|0;b=H[a+44>>2];l:{if((b|0)!=H[a+48>>2]){H[b>>2]=f;H[a+44>>2]=b+4;break l}h=H[a+40>>2];g=b-h|0;l=g>>2;c=l+1|0;if(c>>>0>=1073741824){break a}j=g>>>1|0;j=g>>>0>=2147483644?1073741823:c>>>0>>0?j:c;if(j){if(j>>>0>=1073741824){break b}g=pa(j<<2)}else{g=0}c=g+(l<<2)|0;H[c>>2]=f;l=c+4|0;if((b|0)!=(h|0)){while(1){c=c-4|0;b=b-4|0;H[c>>2]=H[b>>2];if((b|0)!=(h|0)){continue}break}}H[a+48>>2]=g+(j<<2);H[a+44>>2]=l;H[a+40>>2]=c;if(!h){break l}oa(h)}g=i;i=d}H[H[a+28>>2]+(f<<2)>>2]=g;b=H[a+64>>2];m:{if((f>>>0)%3|0){c=f-1|0;break m}c=f+2|0;if((c|0)==-1){break d}}d=H[H[b+12>>2]+(c<<2)>>2];if((d|0)==-1){break d}f=d+((d>>>0)%3|0?-1:2)|0;if((f|0)==-1){break d}if((e|0)!=(f|0)){continue}break}}k=k+1|0;c=H[b+24>>2];if(k>>>0>2]-c>>2>>>0){continue}break}return 1}sa();v()}wa();v()}sa();v()}function ti(a){a=a|0;var b=0,c=0,d=0,e=0;c=H[a+32>>2];d=H[c+16>>2];e=H[c+12>>2];b=H[c+20>>2];if(K[c+8>>2]>d>>>0&(e|0)>=(b|0)|(b|0)<(e|0)){e=I[H[c>>2]+d|0];d=d+1|0;b=d?b:b+1|0;H[c+16>>2]=d;H[c+20>>2]=b;b=H[a+48>>2];H[a+48>>2]=0;if(b){ea[H[H[b>>2]+4>>2]](b)}a:{b:{c:{d:{switch(e|0){case 0:b=pa(384);H[b>>2]=11384;ra(b+4|0,0,80);H[b+96>>2]=0;H[b+100>>2]=0;H[b+92>>2]=-1;H[b+84>>2]=-1;H[b+88>>2]=-1;H[b+104>>2]=0;H[b+108>>2]=0;H[b+112>>2]=0;H[b+116>>2]=0;H[b+120>>2]=0;H[b+124>>2]=0;H[b+128>>2]=0;H[b+132>>2]=0;H[b+136>>2]=0;H[b+140>>2]=0;H[b+144>>2]=0;H[b+148>>2]=0;H[b+156>>2]=0;H[b+160>>2]=0;H[b+152>>2]=1065353216;H[b+164>>2]=0;H[b+168>>2]=0;H[b+172>>2]=0;H[b+176>>2]=0;H[b+180>>2]=0;H[b+184>>2]=0;H[b+188>>2]=0;H[b+192>>2]=0;H[b+196>>2]=0;H[b+200>>2]=0;H[b+204>>2]=0;H[b+208>>2]=0;H[b+212>>2]=-1;H[b+216>>2]=0;H[b+220>>2]=0;H[b+224>>2]=0;Ha(b+232|0);Ha(b+272|0);c=b+312|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;Ha(b+328|0);H[b+376>>2]=0;H[b+368>>2]=0;H[b+372>>2]=0;break c;case 1:b=pa(424);H[b>>2]=11436;ra(b+4|0,0,80);H[b+96>>2]=0;H[b+100>>2]=0;H[b+92>>2]=-1;H[b+84>>2]=-1;H[b+88>>2]=-1;H[b+104>>2]=0;H[b+108>>2]=0;H[b+112>>2]=0;H[b+116>>2]=0;H[b+120>>2]=0;H[b+124>>2]=0;H[b+128>>2]=0;H[b+132>>2]=0;H[b+136>>2]=0;H[b+140>>2]=0;H[b+144>>2]=0;H[b+148>>2]=0;H[b+156>>2]=0;H[b+160>>2]=0;H[b+152>>2]=1065353216;H[b+164>>2]=0;H[b+168>>2]=0;H[b+172>>2]=0;H[b+176>>2]=0;H[b+180>>2]=0;H[b+184>>2]=0;H[b+188>>2]=0;H[b+192>>2]=0;H[b+196>>2]=0;H[b+200>>2]=0;H[b+204>>2]=0;H[b+208>>2]=0;H[b+212>>2]=-1;H[b+216>>2]=0;H[b+220>>2]=0;H[b+224>>2]=0;Ha(b+232|0);Ha(b+272|0);c=b+312|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;Ha(b+328|0);H[b+392>>2]=0;H[b+396>>2]=0;H[b+384>>2]=0;H[b+388>>2]=0;H[b+376>>2]=0;H[b+380>>2]=0;H[b+368>>2]=0;H[b+372>>2]=0;c=b+400|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;H[b+416>>2]=-1;H[b+420>>2]=-1;break c;case 2:break d;default:break b}}b=pa(440);H[b>>2]=11484;ra(b+4|0,0,80);H[b+96>>2]=0;H[b+100>>2]=0;H[b+92>>2]=-1;H[b+84>>2]=-1;H[b+88>>2]=-1;H[b+104>>2]=0;H[b+108>>2]=0;H[b+112>>2]=0;H[b+116>>2]=0;H[b+120>>2]=0;H[b+124>>2]=0;H[b+128>>2]=0;H[b+132>>2]=0;H[b+136>>2]=0;H[b+140>>2]=0;H[b+144>>2]=0;H[b+148>>2]=0;H[b+156>>2]=0;H[b+160>>2]=0;H[b+152>>2]=1065353216;H[b+164>>2]=0;H[b+168>>2]=0;H[b+172>>2]=0;H[b+176>>2]=0;H[b+180>>2]=0;H[b+184>>2]=0;H[b+188>>2]=0;H[b+192>>2]=0;H[b+196>>2]=0;H[b+200>>2]=0;H[b+204>>2]=0;H[b+208>>2]=0;H[b+212>>2]=-1;H[b+216>>2]=0;H[b+220>>2]=0;H[b+224>>2]=0;Ha(b+232|0);Ha(b+272|0);c=b+312|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;Ha(b+328|0);H[b+392>>2]=0;H[b+396>>2]=0;H[b+384>>2]=0;H[b+388>>2]=0;H[b+376>>2]=0;H[b+380>>2]=0;H[b+368>>2]=0;H[b+372>>2]=0;H[b+416>>2]=0;H[b+420>>2]=0;H[b+408>>2]=2;H[b+412>>2]=7;H[b+400>>2]=-1;H[b+404>>2]=-1;H[b+424>>2]=0;H[b+428>>2]=0;H[b+432>>2]=0;H[b+436>>2]=0}c=H[a+48>>2];H[a+48>>2]=b;if(!c){break a}ea[H[H[c>>2]+4>>2]](c)}b=H[a+48>>2];if(b){break a}return 0}a=ea[H[H[b>>2]+8>>2]](b,a)|0}else{a=0}return a|0}function Lb(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0,x=0,y=0;f=ca-96|0;ca=f;e=H[a+16>>2];F[f+92|0]=1;H[f+88>>2]=b;H[f+84>>2]=b;H[f+80>>2]=e;a:{if((b|0)==-1){break a}j=H[a+20>>2];d=H[j>>2];e=H[H[e>>2]+(b<<2)>>2];if(e>>>0>=H[j+4>>2]-d>>2>>>0){break a}e=H[H[a+8>>2]+(H[d+(e<<2)>>2]<<2)>>2];d=H[a+4>>2];if(!I[d+84|0]){e=H[H[d+68>>2]+(e<<2)>>2]}H[f+72>>2]=0;H[f+76>>2]=0;j=f- -64|0;H[j>>2]=0;H[j+4>>2]=0;H[f+56>>2]=0;H[f+60>>2]=0;Sa(d,e,F[d+24|0],f+56|0);e=b+1|0;j=(e>>>0)%3|0?e:b-2|0;n=((b>>>0)%3|0?-1:2)+b|0;b:{c:{while(1){d=j;e=n;d:{if(!H[a+28>>2]){break d}e=b+1|0;d=(e>>>0)%3|0?e:b-2|0;e=b-1|0;if((b>>>0)%3|0){break d}e=b+2|0}if((d|0)==-1){break b}m=H[a+20>>2];b=H[m>>2];d=H[H[H[a+16>>2]>>2]+(d<<2)>>2];if(d>>>0>=H[m+4>>2]-b>>2>>>0){break b}d=H[H[a+8>>2]+(H[(d<<2)+b>>2]<<2)>>2];b=H[a+4>>2];if(!I[b+84|0]){d=H[H[b+68>>2]+(d<<2)>>2]}H[f+48>>2]=0;H[f+52>>2]=0;H[f+40>>2]=0;H[f+44>>2]=0;H[f+32>>2]=0;H[f+36>>2]=0;Sa(b,d,F[b+24|0],f+32|0);if((e|0)==-1){break c}d=H[a+20>>2];b=H[d>>2];e=H[H[H[a+16>>2]>>2]+(e<<2)>>2];if(e>>>0>=H[d+4>>2]-b>>2>>>0){break c}d=H[H[a+8>>2]+(H[b+(e<<2)>>2]<<2)>>2];b=H[a+4>>2];if(!I[b+84|0]){d=H[H[b+68>>2]+(d<<2)>>2]}H[f+24>>2]=0;H[f+28>>2]=0;H[f+16>>2]=0;H[f+20>>2]=0;H[f+8>>2]=0;H[f+12>>2]=0;Sa(b,d,F[b+24|0],f+8|0);g=H[f+8>>2];b=H[f+56>>2];d=g-b|0;p=H[f+60>>2];t=H[f+12>>2]-(p+(b>>>0>g>>>0)|0)|0;i=H[f+40>>2];e=H[f+64>>2];m=i-e|0;u=H[f+68>>2];y=H[f+44>>2]-(u+(e>>>0>i>>>0)|0)|0;g=Rj(d,t,m,y);w=o-g|0;x=h-(da+(g>>>0>o>>>0)|0)|0;h=w;i=H[f+16>>2];g=i-e|0;u=H[f+20>>2]-((e>>>0>i>>>0)+u|0)|0;k=H[f+32>>2];i=k-b|0;w=H[f+36>>2]-((b>>>0>k>>>0)+p|0)|0;b=Rj(g,u,i,w);o=h+b|0;h=da+x|0;h=b>>>0>o>>>0?h+1|0:h;b=l;l=d;p=t;k=H[f+48>>2];e=H[f+72>>2];d=k-e|0;t=H[f+76>>2];x=H[f+52>>2]-(t+(e>>>0>k>>>0)|0)|0;l=Rj(l,p,d,x);k=b+l|0;b=da+q|0;b=k>>>0>>0?b+1|0:b;l=H[f+24>>2];p=l-e|0;e=H[f+28>>2]-((e>>>0>l>>>0)+t|0)|0;q=Rj(p,e,i,w);l=k-q|0;q=b-(da+(k>>>0>>0)|0)|0;b=Rj(g,u,d,x);d=r-b|0;b=s-(da+(b>>>0>r>>>0)|0)|0;s=Rj(p,e,m,y);r=s+d|0;b=da+b|0;s=r>>>0>>0?b+1|0:b;uc(f+80|0);b=H[f+88>>2];if((b|0)!=-1){continue}break}b=s>>31;e=b^r;d=e-b|0;b=(b^s)-((b>>>0>e>>>0)+b|0)|0;n=-1;e=2147483647;m=q>>31;g=m;i=g^l;j=i-g|0;m=(g^q)-((i>>>0>>0)+g|0)|0;i=m;k=j^-1;g=i^2147483647;m=h;e:{f:{if(!H[a+28>>2]){if((b|0)==(g|0)&d>>>0>k>>>0|b>>>0>g>>>0){break e}b=b+i|0;a=d+j|0;b=a>>>0>>0?b+1|0:b;e=a;g=h;a=g>>31;d=a;n=d^o;a=n-d|0;h=a;d=(d^g)-((d>>>0>n>>>0)+d|0)|0;a=a+e|0;d=d^2147483647;h=(d|0)==(b|0)&(h^-1)>>>0>>0|b>>>0>d>>>0;a=h?-1:a;if(!(h&0)&(a|0)<=536870912|(a|0)<536870912){break e}b=0;a=a>>>29|0;break f}g:{if((b|0)==(g|0)&d>>>0>k>>>0|b>>>0>g>>>0){break g}b=b+i|0;a=d+j|0;b=a>>>0>>0?b+1|0:b;k=h;h=h>>31;g=h;i=g^o;h=i-g|0;j=(g^k)-((g>>>0>i>>>0)+g|0)|0;g=j^2147483647;d=a;a=h;if((g|0)==(b|0)&d>>>0>(a^-1)>>>0|b>>>0>g>>>0){break g}b=b+j|0;n=a+d|0;b=n>>>0>>0?b+1|0:b;e=b;if(!b&n>>>0<536870913){break e}}b=e>>>29|0;a=(e&536870911)<<3|n>>>29}o=Sj(o,m,a,b);l=Sj(l,q,a,b);r=Sj(r,s,a,b)}H[c+8>>2]=o;H[c+4>>2]=l;H[c>>2]=r;ca=f+96|0;return}Ca();v()}Ca();v()}Ca();v()}function Wd(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0;a:{if((b|0)<0){break a}c=H[a+12>>2];d=H[a+8>>2];if(c-d>>2>>>0<=b>>>0){break a}d=d+(b<<2)|0;e=H[d>>2];i=H[e+60>>2];f=H[e+56>>2];e=d+4|0;if((e|0)!=(c|0)){while(1){h=H[e>>2];H[e>>2]=0;g=H[d>>2];H[d>>2]=h;if(g){Ga(g)}d=d+4|0;e=e+4|0;if((e|0)!=(c|0)){continue}break}c=H[a+12>>2]}if((c|0)!=(d|0)){while(1){c=c-4|0;e=H[c>>2];H[c>>2]=0;if(e){Ga(e)}if((c|0)!=(d|0)){continue}break}}H[a+12>>2]=d;g=H[a+4>>2];b:{if(!g|(i|0)<0){break b}c=H[g+24>>2];d=H[g+28>>2];if((c|0)==(d|0)){break b}while(1){if((i|0)==H[H[c>>2]+24>>2]){d=c+4|0;i=H[g+28>>2];if((d|0)!=(i|0)){while(1){h=H[d>>2];H[d>>2]=0;e=H[c>>2];H[c>>2]=h;if(e){Ra(e+12|0,H[e+16>>2]);Qa(e,H[e+4>>2]);oa(e)}c=c+4|0;d=d+4|0;if((i|0)!=(d|0)){continue}break}d=H[g+28>>2]}if((c|0)!=(d|0)){while(1){d=d-4|0;e=H[d>>2];H[d>>2]=0;if(e){Ra(e+12|0,H[e+16>>2]);Qa(e,H[e+4>>2]);oa(e)}if((c|0)!=(d|0)){continue}break}}H[g+28>>2]=c;break b}c=c+4|0;if((d|0)!=(c|0)){continue}break}}c:{if((f|0)>4){break c}d:{e=N(f,12)+a|0;c=H[e+20>>2];d=H[e+24>>2];if((c|0)==(d|0)){break d}while(1){if(H[c>>2]==(b|0)){break d}c=c+4|0;if((d|0)!=(c|0)){continue}break}break c}if((c|0)==(d|0)){break c}f=c;c=c+4|0;va(f,c,d-c|0);H[e+24>>2]=d-4}c=H[a+24>>2];d=H[a+20>>2];e:{if((c|0)==(d|0)){break e}e=c-d|0;c=e>>2;g=c>>>0<=1?1:c;i=g&1;c=0;if(e>>>0>=8){g=g&-2;e=0;while(1){f=c<<2;h=f+d|0;j=H[h>>2];if((j|0)>(b|0)){H[h>>2]=j-1}f=d+(f|4)|0;h=H[f>>2];if((h|0)>(b|0)){H[f>>2]=h-1}c=c+2|0;e=e+2|0;if((g|0)!=(e|0)){continue}break}}if(!i){break e}c=d+(c<<2)|0;d=H[c>>2];if((d|0)<=(b|0)){break e}H[c>>2]=d-1}c=H[a+36>>2];d=H[a+32>>2];f:{if((c|0)==(d|0)){break f}e=c-d|0;c=e>>2;g=c>>>0<=1?1:c;i=g&1;c=0;if(e>>>0>=8){g=g&-2;e=0;while(1){f=c<<2;h=f+d|0;j=H[h>>2];if((j|0)>(b|0)){H[h>>2]=j-1}f=d+(f|4)|0;h=H[f>>2];if((h|0)>(b|0)){H[f>>2]=h-1}c=c+2|0;e=e+2|0;if((g|0)!=(e|0)){continue}break}}if(!i){break f}c=d+(c<<2)|0;d=H[c>>2];if((d|0)<=(b|0)){break f}H[c>>2]=d-1}c=H[a+48>>2];d=H[a+44>>2];g:{if((c|0)==(d|0)){break g}e=c-d|0;c=e>>2;g=c>>>0<=1?1:c;i=g&1;c=0;if(e>>>0>=8){g=g&-2;e=0;while(1){f=c<<2;h=f+d|0;j=H[h>>2];if((j|0)>(b|0)){H[h>>2]=j-1}f=d+(f|4)|0;h=H[f>>2];if((h|0)>(b|0)){H[f>>2]=h-1}c=c+2|0;e=e+2|0;if((g|0)!=(e|0)){continue}break}}if(!i){break g}c=d+(c<<2)|0;d=H[c>>2];if((d|0)<=(b|0)){break g}H[c>>2]=d-1}c=H[a+60>>2];d=H[a+56>>2];h:{if((c|0)==(d|0)){break h}e=c-d|0;c=e>>2;g=c>>>0<=1?1:c;i=g&1;c=0;if(e>>>0>=8){g=g&-2;e=0;while(1){f=c<<2;h=f+d|0;j=H[h>>2];if((j|0)>(b|0)){H[h>>2]=j-1}f=d+(f|4)|0;h=H[f>>2];if((h|0)>(b|0)){H[f>>2]=h-1}c=c+2|0;e=e+2|0;if((g|0)!=(e|0)){continue}break}}if(!i){break h}c=d+(c<<2)|0;d=H[c>>2];if((d|0)<=(b|0)){break h}H[c>>2]=d-1}c=H[a+72>>2];a=H[a+68>>2];if((c|0)==(a|0)){break a}d=c-a|0;c=d>>2;e=c>>>0<=1?1:c;g=e&1;c=0;if(d>>>0>=8){d=e&-2;e=0;while(1){i=c<<2;f=i+a|0;h=H[f>>2];if((h|0)>(b|0)){H[f>>2]=h-1}i=a+(i|4)|0;f=H[i>>2];if((f|0)>(b|0)){H[i>>2]=f-1}c=c+2|0;e=e+2|0;if((d|0)!=(e|0)){continue}break}}if(!g){break a}f=b;a=a+(c<<2)|0;b=H[a>>2];if((f|0)>=(b|0)){break a}H[a>>2]=b-1}}function oa(a){a=a|0;var b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0;a:{if(!a){break a}d=a-8|0;b=H[a-4>>2];a=b&-8;f=d+a|0;b:{if(b&1){break b}if(!(b&3)){break a}b=H[d>>2];d=d-b|0;if(d>>>0>>0<=255){e=H[d+8>>2];b=b>>>3|0;c=H[d+12>>2];if((c|0)==(e|0)){i=17192,j=H[4298]&Vj(b),H[i>>2]=j;break b}H[e+12>>2]=c;H[c+8>>2]=e;break b}h=H[d+24>>2];b=H[d+12>>2];c:{if((d|0)!=(b|0)){c=H[d+8>>2];H[c+12>>2]=b;H[b+8>>2]=c;break c}d:{e=d+20|0;c=H[e>>2];if(c){break d}e=d+16|0;c=H[e>>2];if(c){break d}b=0;break c}while(1){g=e;b=c;e=b+20|0;c=H[e>>2];if(c){continue}e=b+16|0;c=H[b+16>>2];if(c){continue}break}H[g>>2]=0}if(!h){break b}e=H[d+28>>2];c=(e<<2)+17496|0;e:{if(H[c>>2]==(d|0)){H[c>>2]=b;if(b){break e}i=17196,j=H[4299]&Vj(e),H[i>>2]=j;break b}H[h+(H[h+16>>2]==(d|0)?16:20)>>2]=b;if(!b){break b}}H[b+24>>2]=h;c=H[d+16>>2];if(c){H[b+16>>2]=c;H[c+24>>2]=b}c=H[d+20>>2];if(!c){break b}H[b+20>>2]=c;H[c+24>>2]=b;break b}b=H[f+4>>2];if((b&3)!=3){break b}H[4300]=a;H[f+4>>2]=b&-2;H[d+4>>2]=a|1;H[a+d>>2]=a;return}if(d>>>0>=f>>>0){break a}b=H[f+4>>2];if(!(b&1)){break a}f:{if(!(b&2)){if(H[4304]==(f|0)){H[4304]=d;a=H[4301]+a|0;H[4301]=a;H[d+4>>2]=a|1;if(H[4303]!=(d|0)){break a}H[4300]=0;H[4303]=0;return}if(H[4303]==(f|0)){H[4303]=d;a=H[4300]+a|0;H[4300]=a;H[d+4>>2]=a|1;H[a+d>>2]=a;return}a=(b&-8)+a|0;g:{if(b>>>0<=255){e=H[f+8>>2];b=b>>>3|0;c=H[f+12>>2];if((c|0)==(e|0)){i=17192,j=H[4298]&Vj(b),H[i>>2]=j;break g}H[e+12>>2]=c;H[c+8>>2]=e;break g}h=H[f+24>>2];b=H[f+12>>2];h:{if((f|0)!=(b|0)){c=H[f+8>>2];H[c+12>>2]=b;H[b+8>>2]=c;break h}i:{e=f+20|0;c=H[e>>2];if(c){break i}e=f+16|0;c=H[e>>2];if(c){break i}b=0;break h}while(1){g=e;b=c;e=b+20|0;c=H[e>>2];if(c){continue}e=b+16|0;c=H[b+16>>2];if(c){continue}break}H[g>>2]=0}if(!h){break g}e=H[f+28>>2];c=(e<<2)+17496|0;j:{if(H[c>>2]==(f|0)){H[c>>2]=b;if(b){break j}i=17196,j=H[4299]&Vj(e),H[i>>2]=j;break g}H[h+(H[h+16>>2]==(f|0)?16:20)>>2]=b;if(!b){break g}}H[b+24>>2]=h;c=H[f+16>>2];if(c){H[b+16>>2]=c;H[c+24>>2]=b}c=H[f+20>>2];if(!c){break g}H[b+20>>2]=c;H[c+24>>2]=b}H[d+4>>2]=a|1;H[a+d>>2]=a;if(H[4303]!=(d|0)){break f}H[4300]=a;return}H[f+4>>2]=b&-2;H[d+4>>2]=a|1;H[a+d>>2]=a}if(a>>>0<=255){b=(a&-8)+17232|0;c=H[4298];a=1<<(a>>>3);k:{if(!(c&a)){H[4298]=a|c;a=b;break k}a=H[b+8>>2]}H[b+8>>2]=d;H[a+12>>2]=d;H[d+12>>2]=b;H[d+8>>2]=a;return}e=31;if(a>>>0<=16777215){b=Q(a>>>8|0);e=((a>>>38-b&1)-(b<<1)|0)+62|0}H[d+28>>2]=e;H[d+16>>2]=0;H[d+20>>2]=0;g=(e<<2)+17496|0;l:{m:{c=H[4299];b=1<>2]=d;H[d+24>>2]=g;break n}e=a<<((e|0)!=31?25-(e>>>1|0)|0:0);b=H[g>>2];while(1){c=b;if((H[b+4>>2]&-8)==(a|0)){break m}b=e>>>29|0;e=e<<1;g=c+(b&4)|0;b=H[g+16>>2];if(b){continue}break}H[g+16>>2]=d;H[d+24>>2]=c}H[d+12>>2]=d;H[d+8>>2]=d;break l}a=H[c+8>>2];H[a+12>>2]=d;H[c+8>>2]=d;H[d+24>>2]=0;H[d+12>>2]=c;H[d+8>>2]=a}a=H[4306]-1|0;H[4306]=a?a:-1}}function tj(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;var g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0;H[a+8>>2]=e;n=a+32|0;h=H[n>>2];f=H[a+36>>2]-h>>2;a:{if(f>>>0>>0){ya(n,e-f|0);d=H[a+8>>2];break a}d=e;if(d>>>0>=f>>>0){break a}H[a+36>>2]=h+(e<<2);d=e}s=H[a+52>>2];p=H[a+48>>2];f=0;h=e>>>0>1073741823?-1:e<<2;m=ra(pa(h),0,h);b:{if((d|0)<=0){break b}g=H[a+32>>2];while(1){d=f<<2;h=H[d+m>>2];j=H[a+16>>2];c:{if((h|0)>(j|0)){H[d+g>>2]=j;break c}d=d+g|0;j=H[a+12>>2];if((j|0)>(h|0)){H[d>>2]=j;break c}H[d>>2]=h}d=H[a+8>>2];f=f+1|0;if((d|0)>(f|0)){continue}break}if((d|0)<=0){break b}f=0;while(1){h=f<<2;d=h+c|0;h=H[b+h>>2]+H[g+h>>2]|0;H[d>>2]=h;d:{if((h|0)>H[a+16>>2]){i=h-H[a+20>>2]|0}else{if((h|0)>=H[a+12>>2]){break d}i=h+H[a+20>>2]|0}H[d>>2]=i}d=H[a+8>>2];f=f+1|0;if((d|0)>(f|0)){continue}break}}f=H[a+56>>2];q=H[f>>2];f=H[f+4>>2]-q|0;if((f|0)>=5){o=f>>>2|0;t=o>>>0<=2?2:o;u=e&-2;w=e&1;h=1;while(1){e:{f:{if((h|0)!=(o|0)){r=N(e,h);f=H[(h<<2)+q>>2];if((f|0)==-1){break f}f=H[H[p+12>>2]+(f<<2)>>2];if((f|0)==-1){break f}j=H[s>>2];g=H[p>>2];k=H[j+(H[g+(f<<2)>>2]<<2)>>2];i=f+1|0;i=(i>>>0)%3|0?i:f-2|0;if((i|0)!=-1){i=H[g+(i<<2)>>2]}else{i=-1}g:{h:{if((f>>>0)%3|0){f=f-1|0;break h}f=f+2|0;l=-1;if((f|0)==-1){break g}}l=H[g+(f<<2)>>2]}if((h|0)<=(k|0)){break f}f=H[(i<<2)+j>>2];if((f|0)>=(h|0)){break f}g=H[j+(l<<2)>>2];if((g|0)>=(h|0)){break f}i:{if((e|0)<=0){break i}g=N(e,g);j=N(e,f);k=N(e,k);f=0;l=0;if((e|0)!=1){while(1){H[(f<<2)+m>>2]=(H[(f+g<<2)+c>>2]+H[(f+j<<2)+c>>2]|0)-H[(f+k<<2)+c>>2];i=f|1;H[(i<<2)+m>>2]=(H[(g+i<<2)+c>>2]+H[(j+i<<2)+c>>2]|0)-H[(i+k<<2)+c>>2];f=f+2|0;l=l+2|0;if((u|0)!=(l|0)){continue}break}}if(!w){break i}H[(f<<2)+m>>2]=(H[(f+g<<2)+c>>2]+H[(f+j<<2)+c>>2]|0)-H[(f+k<<2)+c>>2]}if((d|0)<=0){break e}j=H[n>>2];f=0;while(1){d=f<<2;g=H[d+m>>2];k=H[a+16>>2];j:{if((g|0)>(k|0)){H[d+j>>2]=k;break j}d=d+j|0;k=H[a+12>>2];if((k|0)>(g|0)){H[d>>2]=k;break j}H[d>>2]=g}d=H[a+8>>2];f=f+1|0;if((d|0)>(f|0)){continue}break}f=0;if((d|0)<=0){break e}d=r<<2;k=d+c|0;i=b+d|0;while(1){g=f<<2;d=g+k|0;g=H[g+i>>2]+H[g+j>>2]|0;H[d>>2]=g;k:{if((g|0)>H[a+16>>2]){l=g-H[a+20>>2]|0}else{if((g|0)>=H[a+12>>2]){break k}l=g+H[a+20>>2]|0}H[d>>2]=l}d=H[a+8>>2];f=f+1|0;if((d|0)>(f|0)){continue}break}break e}Ca();v()}if((d|0)<=0){break e}k=(N(h-1|0,e)<<2)+c|0;j=H[n>>2];f=0;while(1){d=f<<2;g=H[d+k>>2];i=H[a+16>>2];l:{if((g|0)>(i|0)){H[d+j>>2]=i;break l}d=d+j|0;i=H[a+12>>2];if((i|0)>(g|0)){H[d>>2]=i;break l}H[d>>2]=g}d=H[a+8>>2];f=f+1|0;if((d|0)>(f|0)){continue}break}f=0;if((d|0)<=0){break e}d=r<<2;k=d+c|0;i=b+d|0;while(1){g=f<<2;d=g+k|0;g=H[g+i>>2]+H[g+j>>2]|0;H[d>>2]=g;m:{if((g|0)>H[a+16>>2]){l=g-H[a+20>>2]|0}else{if((g|0)>=H[a+12>>2]){break m}l=g+H[a+20>>2]|0}H[d>>2]=l}d=H[a+8>>2];f=f+1|0;if((d|0)>(f|0)){continue}break}}h=h+1|0;if((t|0)!=(h|0)){continue}break}}oa(m);return 1}function we(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0;if((b|0)==-1){return 1}g=(b>>>0)/3|0;if(!(H[H[a+24>>2]+(g>>>3&268435452)>>2]>>>g&1)){e=H[a+48>>2];H[a+52>>2]=e;a:{if((e|0)!=H[a+56>>2]){H[e>>2]=b;H[a+52>>2]=e+4;break a}d=pa(4);H[d>>2]=b;c=d+4|0;H[a+56>>2]=c;H[a+52>>2]=c;H[a+48>>2]=d;if(!e){break a}oa(e)}c=b+1|0;i=(c>>>0)%3|0?c:b-2|0;c=H[H[a+4>>2]+28>>2];k=H[(i<<2)+c>>2];if((k|0)==-1){return 0}e=(b-N(g,3)|0?-1:2)+b|0;j=H[c+(e<<2)>>2];if((j|0)==-1){return 0}b=H[a+36>>2];g=b+(k>>>3&536870908)|0;d=H[g>>2];c=1<>2]=c|d;Ua(a+8|0,k,i);b=H[a+36>>2]}d=(j>>>3&536870908)+b|0;c=H[d>>2];b=1<>2]=b|c;Ua(a+8|0,j,e)}f=H[a+52>>2];if((f|0)==H[a+48>>2]){return 1}k=a+8|0;while(1){b:{c:{f=f-4|0;b=H[f>>2];if((b|0)==-1){break c}c=(b>>>0)/3|0;g=H[a+24>>2]+(c>>>3&268435452)|0;d=H[g>>2];c=1<>2]=c|d;h=H[a+4>>2];c=H[H[h+28>>2]+(b<<2)>>2];if((c|0)==-1){return 0}while(1){d=b;d:{e:{j=H[a+36>>2]+(c>>>3&536870908)|0;i=H[j>>2];e=1<>2]+(c<<2)>>2];g:{if((g|0)==-1){break g}b=g+1|0;b=(b>>>0)%3|0?b:g-2|0;if((b|0)==-1|H[H[h>>2]+(b>>>3&536870908)>>2]>>>b&1){break g}g=H[H[H[h+64>>2]+12>>2]+(b<<2)>>2];if((g|0)!=-1){break f}}H[j>>2]=e|i;Ua(k,c,d);h=H[a+4>>2];break e}H[j>>2]=e|i;Ua(k,c,d);h=H[a+4>>2];b=g+1|0;if((((b>>>0)%3|0?b:g-2|0)|0)==-1){break e}b=-1;h:{if((d|0)==-1){break h}c=d+1|0;c=(c>>>0)%3|0?c:d-2|0;if((c|0)==-1|H[H[h>>2]+(c>>>3&536870908)>>2]>>>c&1){break h}b=H[H[H[h+64>>2]+12>>2]+(c<<2)>>2]}c=(b>>>0)/3|0;d=1<>2];e=c>>>5|0;j=H[f+(e<<2)>>2];break d}i:{j:{if((d|0)==-1){break j}c=-1;b=d+1|0;b=(b>>>0)%3|0?b:d-2|0;if(!((b|0)==-1|H[H[h>>2]+(b>>>3&536870908)>>2]>>>b&1)){c=H[H[H[h+64>>2]+12>>2]+(b<<2)>>2]}k:{l:{if((d>>>0)%3|0){f=d-1|0;break l}f=d+2|0;b=-1;if((f|0)==-1){break k}}b=-1;if(H[H[h>>2]+(f>>>3&536870908)>>2]>>>f&1){break k}b=H[H[H[h+64>>2]+12>>2]+(f<<2)>>2]}g=(b|0)==-1;i=g?-1:(b>>>0)/3|0;if((c|0)!=-1){f=H[a+24>>2];d=(c>>>0)/3|0;e=d>>>5|0;j=H[f+(e<<2)>>2];d=1<>2];e=i>>>5|0;j=H[f+(e<<2)>>2];if(!(d&j)){break d}}f=H[a+52>>2]-4|0;H[a+52>>2]=f;break b}if(g){b=c;break d}if(H[(i>>>3&536870908)+f>>2]>>>i&1){b=c;break d}h=H[a+52>>2];H[h-4>>2]=b;if(H[a+56>>2]!=(h|0)){H[h>>2]=c;f=h+4|0;break c}m:{i=H[a+48>>2];e=h-i|0;g=e>>2;d=g+1|0;if(d>>>0<1073741824){b=e>>>1|0;e=e>>>0>=2147483644?1073741823:b>>>0>d>>>0?b:d;if(e){if(e>>>0>=1073741824){break m}d=pa(e<<2)}else{d=0}b=d+(g<<2)|0;H[b>>2]=c;f=b+4|0;if((h|0)!=(i|0)){while(1){b=b-4|0;h=h-4|0;H[b>>2]=H[h>>2];if((h|0)!=(i|0)){continue}break}}H[a+56>>2]=d+(e<<2);H[a+52>>2]=f;H[a+48>>2]=b;if(!i){break b}oa(i);f=H[a+52>>2];break b}sa();v()}wa();v()}H[(e<<2)+f>>2]=d|j;c=H[H[h+28>>2]+(b<<2)>>2];if((c|0)!=-1){continue}break}return 0}H[a+52>>2]=f}if(H[a+48>>2]!=(f|0)){continue}break}}return 1}function Lj(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;var g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,w=0;H[a+8>>2]=e;m=a+32|0;h=H[m>>2];f=H[a+36>>2]-h>>2;a:{if(f>>>0>>0){ya(m,e-f|0);d=H[a+8>>2];break a}d=e;if(d>>>0>=f>>>0){break a}H[a+36>>2]=h+(e<<2);d=e}s=H[a+52>>2];n=H[a+48>>2];f=0;h=e>>>0>1073741823?-1:e<<2;l=ra(pa(h),0,h);b:{if((d|0)<=0){break b}g=H[a+32>>2];while(1){d=f<<2;h=H[d+l>>2];i=H[a+16>>2];c:{if((h|0)>(i|0)){H[d+g>>2]=i;break c}d=d+g|0;i=H[a+12>>2];if((i|0)>(h|0)){H[d>>2]=i;break c}H[d>>2]=h}d=H[a+8>>2];f=f+1|0;if((d|0)>(f|0)){continue}break}if((d|0)<=0){break b}f=0;while(1){h=f<<2;d=h+c|0;h=H[b+h>>2]+H[g+h>>2]|0;H[d>>2]=h;d:{if((h|0)>H[a+16>>2]){h=h-H[a+20>>2]|0}else{if((h|0)>=H[a+12>>2]){break d}h=h+H[a+20>>2]|0}H[d>>2]=h}d=H[a+8>>2];f=f+1|0;if((d|0)>(f|0)){continue}break}}f=H[a+56>>2];q=H[f>>2];f=H[f+4>>2]-q|0;if((f|0)>=5){o=f>>>2|0;t=o>>>0<=2?2:o;u=e&-2;w=e&1;h=1;while(1){e:{f:{if((h|0)!=(o|0)){r=N(e,h);f=H[(h<<2)+q>>2];if((f|0)==-1|H[H[n>>2]+(f>>>3&536870908)>>2]>>>f&1){break f}f=H[H[H[n+64>>2]+12>>2]+(f<<2)>>2];if((f|0)==-1){break f}i=H[s>>2];g=H[n+28>>2];k=H[i+(H[g+(f<<2)>>2]<<2)>>2];if((k|0)>=(h|0)){break f}j=f+1|0;j=H[i+(H[g+(((j>>>0)%3|0?j:f-2|0)<<2)>>2]<<2)>>2];if((j|0)>=(h|0)){break f}f=H[i+(H[g+(f+((f>>>0)%3|0?-1:2)<<2)>>2]<<2)>>2];if((f|0)>=(h|0)){break f}g:{if((e|0)<=0){break g}g=N(e,f);i=N(e,j);k=N(e,k);f=0;p=0;if((e|0)!=1){while(1){H[(f<<2)+l>>2]=(H[(f+g<<2)+c>>2]+H[(f+i<<2)+c>>2]|0)-H[(f+k<<2)+c>>2];j=f|1;H[(j<<2)+l>>2]=(H[(g+j<<2)+c>>2]+H[(i+j<<2)+c>>2]|0)-H[(k+j<<2)+c>>2];f=f+2|0;p=p+2|0;if((u|0)!=(p|0)){continue}break}}if(!w){break g}H[(f<<2)+l>>2]=(H[(f+g<<2)+c>>2]+H[(f+i<<2)+c>>2]|0)-H[(f+k<<2)+c>>2]}if((d|0)<=0){break e}i=H[m>>2];f=0;while(1){d=f<<2;g=H[d+l>>2];k=H[a+16>>2];h:{if((g|0)>(k|0)){H[d+i>>2]=k;break h}d=d+i|0;k=H[a+12>>2];if((k|0)>(g|0)){H[d>>2]=k;break h}H[d>>2]=g}d=H[a+8>>2];f=f+1|0;if((d|0)>(f|0)){continue}break}f=0;if((d|0)<=0){break e}d=r<<2;k=d+c|0;j=b+d|0;while(1){g=f<<2;d=g+k|0;g=H[g+j>>2]+H[g+i>>2]|0;H[d>>2]=g;i:{if((g|0)>H[a+16>>2]){g=g-H[a+20>>2]|0}else{if((g|0)>=H[a+12>>2]){break i}g=g+H[a+20>>2]|0}H[d>>2]=g}d=H[a+8>>2];f=f+1|0;if((d|0)>(f|0)){continue}break}break e}Ca();v()}if((d|0)<=0){break e}k=(N(h-1|0,e)<<2)+c|0;i=H[m>>2];f=0;while(1){d=f<<2;g=H[d+k>>2];j=H[a+16>>2];j:{if((g|0)>(j|0)){H[d+i>>2]=j;break j}d=d+i|0;j=H[a+12>>2];if((j|0)>(g|0)){H[d>>2]=j;break j}H[d>>2]=g}d=H[a+8>>2];f=f+1|0;if((d|0)>(f|0)){continue}break}f=0;if((d|0)<=0){break e}d=r<<2;k=d+c|0;j=b+d|0;while(1){g=f<<2;d=g+k|0;g=H[g+j>>2]+H[g+i>>2]|0;H[d>>2]=g;k:{if((g|0)>H[a+16>>2]){g=g-H[a+20>>2]|0}else{if((g|0)>=H[a+12>>2]){break k}g=g+H[a+20>>2]|0}H[d>>2]=g}d=H[a+8>>2];f=f+1|0;if((d|0)>(f|0)){continue}break}}h=h+1|0;if((t|0)!=(h|0)){continue}break}}oa(l);return 1}function Gb(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=O(0),k=0,l=0,m=O(0);i=H[c>>2];a:{b:{f=H[b+4>>2];if(!f){break b}g=Uj(f);c:{if(g>>>0>=2){e=i;if(f>>>0<=e>>>0){e=(i>>>0)%(f>>>0)|0}c=H[H[b>>2]+(e<<2)>>2];if(!c){break b}if(g>>>0<=1){break c}while(1){c=H[c>>2];if(!c){break b}g=H[c+4>>2];if((g|0)!=(i|0)){if(f>>>0<=g>>>0){g=(g>>>0)%(f>>>0)|0}if((e|0)!=(g|0)){break b}}if(H[c+8>>2]!=(i|0)){continue}break}b=0;break a}e=f-1&i;c=H[H[b>>2]+(e<<2)>>2];if(!c){break b}}h=f-1|0;while(1){c=H[c>>2];if(!c){break b}g=H[c+4>>2];if((g|0)!=(i|0)&(g&h)!=(e|0)){break b}if(H[c+8>>2]!=(i|0)){continue}break}b=0;break a}c=pa(16);d=H[H[d>>2]>>2];H[c+12>>2]=0;H[c+8>>2]=d;H[c+4>>2]=i;H[c>>2]=0;m=O(H[b+12>>2]+1>>>0);j=L[b+16>>2];d:{if(m>O(j*O(f>>>0))?0:f){break d}e=2;d=(f-1&f)!=0|f>>>0<3|f<<1;j=O(U(O(m/j)));e:{if(j=O(0)){g=~~j>>>0;break e}g=0}d=d>>>0>g>>>0?d:g;f:{if((d|0)==1){break f}if(!(d&d-1)){e=d;break f}e=Kd(d);f=H[b+4>>2]}g:{if(e>>>0<=f>>>0){if(e>>>0>=f>>>0){break g}g=f>>>0<3;j=O(U(O(O(K[b+12>>2])/L[b+16>>2])));h:{if(j=O(0)){d=~~j>>>0;break h}d=0}i:{j:{if(g){break j}if(Uj(f)>>>0>1){break j}d=d>>>0<2?d:1<<32-Q(d-1|0);break i}d=Kd(d)}e=d>>>0>>0?e:d;if(f>>>0<=e>>>0){break g}}f=0;g=0;h=e;k:{l:{m:{n:{if(e){if(h>>>0>=1073741824){break n}d=pa(h<<2);e=H[b>>2];H[b>>2]=d;if(e){oa(e)}H[b+4>>2]=h;d=0;if(h>>>0>=4){e=h&-4;while(1){k=d<<2;H[k+H[b>>2]>>2]=0;H[H[b>>2]+(k|4)>>2]=0;H[H[b>>2]+(k|8)>>2]=0;H[H[b>>2]+(k|12)>>2]=0;d=d+4|0;g=g+4|0;if((e|0)!=(g|0)){continue}break}}e=h&3;if(e){while(1){H[H[b>>2]+(d<<2)>>2]=0;d=d+1|0;f=f+1|0;if((e|0)!=(f|0)){continue}break}}e=H[b+8>>2];if(!e){break k}d=b+8|0;f=H[e+4>>2];g=Uj(h);if(g>>>0<2){break m}f=f>>>0>=h>>>0?(f>>>0)%(h>>>0)|0:f;H[H[b>>2]+(f<<2)>>2]=d;d=H[e>>2];if(!d){break k}if(g>>>0<=1){break l}while(1){g=H[d+4>>2];if(h>>>0<=g>>>0){g=(g>>>0)%(h>>>0)|0}o:{if((f|0)==(g|0)){e=d;break o}l=g<<2;k=l+H[b>>2]|0;if(!H[k>>2]){H[k>>2]=e;e=d;f=g;break o}H[e>>2]=H[d>>2];H[d>>2]=H[H[l+H[b>>2]>>2]>>2];H[H[l+H[b>>2]>>2]>>2]=d}d=H[e>>2];if(d){continue}break}break k}d=H[b>>2];H[b>>2]=0;if(d){oa(d)}H[b+4>>2]=0;break k}wa();v()}f=h-1&f;H[H[b>>2]+(f<<2)>>2]=d;d=H[e>>2];if(!d){break k}}k=h-1|0;while(1){g=k&H[d+4>>2];p:{if((g|0)==(f|0)){e=d;break p}l=g<<2;h=l+H[b>>2]|0;if(H[h>>2]){H[e>>2]=H[d>>2];H[d>>2]=H[H[l+H[b>>2]>>2]>>2];H[H[l+H[b>>2]>>2]>>2]=d;break p}H[h>>2]=e;e=d;f=g}d=H[e>>2];if(d){continue}break}}}f=H[b+4>>2];d=f-1|0;if(!(d&f)){e=d&i;break d}if(f>>>0>i>>>0){e=i;break d}e=(i>>>0)%(f>>>0)|0}e=H[b>>2]+(e<<2)|0;d=H[e>>2];q:{r:{if(!d){d=b+8|0;H[c>>2]=H[d>>2];H[b+8>>2]=c;H[e>>2]=d;d=H[c>>2];if(!d){break q}d=H[d+4>>2];e=f-1|0;s:{if(!(e&f)){d=d&e;break s}if(d>>>0>>0){break s}d=(d>>>0)%(f>>>0)|0}d=H[b>>2]+(d<<2)|0;break r}H[c>>2]=H[d>>2]}H[d>>2]=c}H[b+12>>2]=H[b+12>>2]+1;b=1}F[a+4|0]=b;H[a>>2]=c}function Oe(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0;f=ca-80|0;ca=f;e=H[c+36>>2];H[f+72>>2]=H[c+32>>2];H[f+76>>2]=e;g=H[c+28>>2];e=f- -64|0;H[e>>2]=H[c+24>>2];H[e+4>>2]=g;e=H[c+20>>2];H[f+56>>2]=H[c+16>>2];H[f+60>>2]=e;e=H[c+12>>2];H[f+48>>2]=H[c+8>>2];H[f+52>>2]=e;e=H[c+4>>2];H[f+40>>2]=H[c>>2];H[f+44>>2]=e;nc(a,f+40|0,f+24|0);a:{if(H[a>>2]){break a}if(F[a+15|0]<0){oa(H[a+4>>2])}if(I[f+31|0]!=1){b=pa(32);F[b+20|0]=0;c=I[1448]|I[1449]<<8|(I[1450]<<16|I[1451]<<24);F[b+16|0]=c;F[b+17|0]=c>>>8;F[b+18|0]=c>>>16;F[b+19|0]=c>>>24;c=I[1444]|I[1445]<<8|(I[1446]<<16|I[1447]<<24);d=I[1440]|I[1441]<<8|(I[1442]<<16|I[1443]<<24);F[b+8|0]=d;F[b+9|0]=d>>>8;F[b+10|0]=d>>>16;F[b+11|0]=d>>>24;F[b+12|0]=c;F[b+13|0]=c>>>8;F[b+14|0]=c>>>16;F[b+15|0]=c>>>24;c=I[1436]|I[1437]<<8|(I[1438]<<16|I[1439]<<24);d=I[1432]|I[1433]<<8|(I[1434]<<16|I[1435]<<24);F[b|0]=d;F[b+1|0]=d>>>8;F[b+2|0]=d>>>16;F[b+3|0]=d>>>24;F[b+4|0]=c;F[b+5|0]=c>>>8;F[b+6|0]=c>>>16;F[b+7|0]=c>>>24;H[a>>2]=-1;za(a+4|0,b,20);oa(b);break a}i=ca-16|0;ca=i;b:{c:{switch(I[f+32|0]){case 0:e=Ke(pa(48));H[e>>2]=13112;H[f+8>>2]=0;H[f+12>>2]=0;H[f>>2]=0;H[f+4>>2]=0;H[f+16>>2]=e;break b;case 1:e=Ke(pa(52));H[e+48>>2]=0;H[e>>2]=11276;H[f+8>>2]=0;H[f+12>>2]=0;H[f>>2]=0;H[f+4>>2]=0;H[f+16>>2]=e;break b;default:break c}}g=pa(32);F[g+28|0]=0;e=I[1550]|I[1551]<<8|(I[1552]<<16|I[1553]<<24);F[g+24|0]=e;F[g+25|0]=e>>>8;F[g+26|0]=e>>>16;F[g+27|0]=e>>>24;e=I[1546]|I[1547]<<8|(I[1548]<<16|I[1549]<<24);h=I[1542]|I[1543]<<8|(I[1544]<<16|I[1545]<<24);F[g+16|0]=h;F[g+17|0]=h>>>8;F[g+18|0]=h>>>16;F[g+19|0]=h>>>24;F[g+20|0]=e;F[g+21|0]=e>>>8;F[g+22|0]=e>>>16;F[g+23|0]=e>>>24;e=I[1538]|I[1539]<<8|(I[1540]<<16|I[1541]<<24);h=I[1534]|I[1535]<<8|(I[1536]<<16|I[1537]<<24);F[g+8|0]=h;F[g+9|0]=h>>>8;F[g+10|0]=h>>>16;F[g+11|0]=h>>>24;F[g+12|0]=e;F[g+13|0]=e>>>8;F[g+14|0]=e>>>16;F[g+15|0]=e>>>24;e=I[1530]|I[1531]<<8|(I[1532]<<16|I[1533]<<24);h=I[1526]|I[1527]<<8|(I[1528]<<16|I[1529]<<24);F[g|0]=h;F[g+1|0]=h>>>8;F[g+2|0]=h>>>16;F[g+3|0]=h>>>24;F[g+4|0]=e;F[g+5|0]=e>>>8;F[g+6|0]=e>>>16;F[g+7|0]=e>>>24;H[i>>2]=-1;e=i|4;za(e,g,28);j=F[i+15|0];H[f>>2]=H[i>>2];h=f+4|0;d:{if((j|0)>=0){j=H[e+4>>2];H[h>>2]=H[e>>2];H[h+4>>2]=j;H[h+8>>2]=H[e+8>>2];H[f+16>>2]=0;break d}za(h,H[i+4>>2],H[i+8>>2]);e=F[i+15|0];H[f+16>>2]=0;if((e|0)>=0){break d}oa(H[i+4>>2])}oa(g)}ca=i+16|0;e=H[f>>2];e:{if(e){H[a>>2]=e;a=a+4|0;if(F[f+15|0]>=0){b=f|4;c=H[b+4>>2];H[a>>2]=H[b>>2];H[a+4>>2]=c;H[a+8>>2]=H[b+8>>2];break e}za(a,H[f+4>>2],H[f+8>>2]);break e}e=H[f+16>>2];H[f+16>>2]=0;H[e+44>>2]=d;te(a,e,b,c,d);if(!H[a>>2]){if(F[a+15|0]<0){oa(H[a+4>>2])}H[a>>2]=0;H[a+4>>2]=0;H[a+8>>2]=0;H[a+12>>2]=0}ea[H[H[e>>2]+4>>2]](e)}a=H[f+16>>2];H[f+16>>2]=0;if(a){ea[H[H[a>>2]+4>>2]](a)}if(F[f+15|0]>=0){break a}oa(H[f+4>>2])}ca=f+80|0}function Gc(a,b,c){var d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0;j=N(b,12)+a|0;H[j+12>>2]=H[j+8>>2];m=(c|0)==-1?-1:(c>>>0)/3|0;d=1;k=c;a:{b:{c:{while(1){d:{l=d;if(!d){if((k|0)==-1){break d}if((de(a,((k>>>0)%3|0?-1:2)+k|0)|0)==-1){break a}c=k+1|0;d=(c>>>0)%3|0?c:k-2|0;if((d|0)==-1){break a}c=d+1|0;c=(c>>>0)%3|0?c:d-2|0;if((c|0)==-1){break a}d=H[H[H[a+4>>2]+12>>2]+(c<<2)>>2];if((d|0)==-1){break a}c=d+1|0;c=(c>>>0)%3|0?c:d-2|0;if((c|0)==-1){break a}m=(c>>>0)/3|0}e:{d=H[a+56>>2]+(m>>>3&536870908)|0;h=H[d>>2];e=1<>2]=e|h;d=H[j+12>>2];f:{if((d|0)!=H[j+16>>2]){H[d>>2]=m;H[j+12>>2]=d+4;break f}n=H[j+8>>2];h=d-n|0;e=h>>2;i=e+1|0;if(i>>>0>=1073741824){break c}g=h>>>1|0;i=h>>>0>=2147483644?1073741823:i>>>0>>0?g:i;if(i){if(i>>>0>=1073741824){break b}g=pa(i<<2)}else{g=0}h=g+(e<<2)|0;H[h>>2]=m;e=h+4|0;if((d|0)!=(n|0)){while(1){h=h-4|0;d=d-4|0;H[h>>2]=H[d>>2];if((d|0)!=(n|0)){continue}break}}H[j+8>>2]=h;H[j+12>>2]=e;H[j+16>>2]=g+(i<<2);if(!n){break f}oa(n)}g=f+1|0;g:{h:{i:{if(!f){break i}if(g&1){if((c|0)==-1){c=-1;break g}d=c+1|0;c=(d>>>0)%3|0?d:c-2|0;break i}k=l?k:c;if((c|0)==-1){c=-1;break g}if((c>>>0)%3|0){d=c-1|0;break h}c=c+2|0}d=c;c=-1;if((d|0)==-1){break g}}c=H[H[H[a+4>>2]+12>>2]+(d<<2)>>2];h=-1;f=-1;e=d+1|0;e=(e>>>0)%3|0?e:d-2|0;if((e|0)>=0){f=(e>>>0)/3|0;f=H[(H[H[a>>2]+96>>2]+N(f,12)|0)+(e-N(f,3)<<2)>>2]}j:{if((c|0)==-1){break j}i=((c>>>0)%3|0?-1:2)+c|0;if((i|0)<0){break j}e=(i>>>0)/3|0;h=H[(H[H[a>>2]+96>>2]+N(e,12)|0)+(i-N(e,3)<<2)>>2]}if((f|0)!=(h|0)){c=-1;break g}k:{l:{f=((d>>>0)%3|0?-1:2)+d|0;if((f|0)>=0){d=(f>>>0)/3|0;if((c|0)!=-1){break l}c=-1;break g}d=-1;if((c|0)!=-1){break k}c=-1;break g}d=H[(H[H[a>>2]+96>>2]+N(d,12)|0)+(f-N(d,3)<<2)>>2]}f=c+1|0;e=(f>>>0)%3|0?f:c-2|0;if((e|0)>=0){f=(e>>>0)/3|0;f=H[(H[H[a>>2]+96>>2]+N(f,12)|0)+(e-N(f,3)<<2)>>2]}else{f=-1}if((f|0)!=(d|0)){c=-1;break g}f=g;m=(c>>>0)/3|0;d=H[a+56>>2]+(m>>>3&268435452)|0;h=H[d>>2];e=1<>2]-4|0;g=H[l>>2];d=H[a+56>>2]+(g>>>3&536870908)|0;c=H[d>>2];o=d,p=Vj(g)&c,H[o>>2]=p;H[j+12>>2]=l;break a}d=0;if(l){continue}break a}break}k=-1;de(a,-1);break a}sa();v()}wa();v()}H[((b<<2)+a|0)+44>>2]=k;b=H[j+12>>2];i=H[j+8>>2];m:{if((b|0)==(i|0)){break m}c=b-i|0;b=c>>2;b=b>>>0<=1?1:b;k=b&1;e=H[a+56>>2];d=0;if(c>>>0>=8){f=b&-2;c=0;while(1){l=d<<2;g=H[l+i>>2];b=e+(g>>>3&536870908)|0;a=H[b>>2];o=b,p=Vj(g)&a,H[o>>2]=p;g=H[i+(l|4)>>2];b=e+(g>>>3&536870908)|0;a=H[b>>2];o=b,p=Vj(g)&a,H[o>>2]=p;d=d+2|0;c=c+2|0;if((f|0)!=(c|0)){continue}break}}if(!k){break m}c=H[i+(d<<2)>>2];b=e+(c>>>3&536870908)|0;a=H[b>>2];o=b,p=Vj(c)&a,H[o>>2]=p}}function Gj(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0;h=ca-32|0;ca=h;a:{if(J[b+38>>1]<=513){c=H[b+20>>2];f=H[b+12>>2];d=H[b+16>>2];if((c|0)>=(f|0)&d>>>0>=K[b+8>>2]|(c|0)>(f|0)){break a}f=I[d+H[b>>2]|0];d=d+1|0;c=d?c:c+1|0;H[b+16>>2]=d;H[b+20>>2]=c;if(f){break a}}b:{if(!Xa(1,h+28|0,b)){break b}d=H[h+28>>2];c=H[H[a+48>>2]+64>>2];if(d>>>0>H[c+4>>2]-H[c>>2]>>2>>>0){break b}c:{if(d){Wa(a+60|0,d);c=h+8|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;if(!ta(c,b)){break c}while(1){f=1<>2]+(e>>>3&536870908)|0;if(j){i=f|H[g>>2]}else{i=H[g>>2]&(f^-1)}H[g>>2]=i;e=e+1|0;if((d|0)!=(e|0)){continue}break}}if(!Xa(1,h+28|0,b)){break b}d=H[h+28>>2];c=H[H[a+48>>2]+64>>2];if(d>>>0>H[c+4>>2]-H[c>>2]>>2>>>0){break b}if(d){e=0;Wa(a+72|0,d);c=h+8|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;if(!ta(c,b)){break c}while(1){f=1<>2]+(e>>>3&536870908)|0;if(j){i=f|H[g>>2]}else{i=H[g>>2]&(f^-1)}H[g>>2]=i;e=e+1|0;if((d|0)!=(e|0)){continue}break}}if(!Xa(1,h+28|0,b)){break b}d=H[h+28>>2];c=H[H[a+48>>2]+64>>2];if(d>>>0>H[c+4>>2]-H[c>>2]>>2>>>0){break b}if(d){e=0;Wa(a+84|0,d);c=h+8|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;if(!ta(c,b)){break c}while(1){f=1<>2]+(e>>>3&536870908)|0;if(j){i=f|H[g>>2]}else{i=H[g>>2]&(f^-1)}H[g>>2]=i;e=e+1|0;if((d|0)!=(e|0)){continue}break}}if(!Xa(1,h+28|0,b)){break b}d=H[h+28>>2];c=H[H[a+48>>2]+64>>2];if(d>>>0>H[c+4>>2]-H[c>>2]>>2>>>0){break b}if(d){e=0;Wa(a+96|0,d);c=h+8|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;if(!ta(c,b)){break c}while(1){f=1<>2]+(e>>>3&536870908)|0;if(j){i=f|H[g>>2]}else{i=H[g>>2]&(f^-1)}H[g>>2]=i;e=e+1|0;if((d|0)!=(e|0)){continue}break}}e=0;c=H[b+8>>2];f=H[b+12>>2];d=c;c=H[b+20>>2];i=c;g=H[b+16>>2];j=g+4|0;c=j>>>0<4?c+1|0:c;if(d>>>0>>0&(c|0)>=(f|0)|(c|0)>(f|0)){break a}m=H[b>>2];k=m+g|0;l=I[k|0]|I[k+1|0]<<8|(I[k+2|0]<<16|I[k+3|0]<<24);H[b+16>>2]=j;H[b+20>>2]=c;k=d;d=f;c=i;f=g+8|0;c=f>>>0<8?c+1|0:c;if(f>>>0>k>>>0&(c|0)>=(d|0)|(c|0)>(d|0)){break a}d=j+m|0;d=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[b+16>>2]=f;H[b+20>>2]=c;if((d|0)<(l|0)){break a}H[a+16>>2]=d;H[a+12>>2]=l;c=(d>>31)-((l>>31)+(d>>>0>>0)|0)|0;b=d-l|0;if(!c&b>>>0>2147483646|c){break a}e=1;b=b+1|0;H[a+20>>2]=b;c=b>>>1|0;H[a+24>>2]=c;H[a+28>>2]=0-c;if(b&1){break a}H[a+24>>2]=c-1;break a}}e=0}ca=h+32|0;return e|0}function pj(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0;h=ca-32|0;ca=h;a:{if(J[b+38>>1]<=513){c=H[b+20>>2];f=H[b+12>>2];d=H[b+16>>2];if((c|0)>=(f|0)&d>>>0>=K[b+8>>2]|(c|0)>(f|0)){break a}f=I[d+H[b>>2]|0];d=d+1|0;c=d?c:c+1|0;H[b+16>>2]=d;H[b+20>>2]=c;if(f){break a}}b:{if(!Xa(1,h+28|0,b)){break b}d=H[h+28>>2];c=H[a+48>>2];if(d>>>0>H[c+4>>2]-H[c>>2]>>2>>>0){break b}c:{if(d){Wa(a+60|0,d);c=h+8|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;if(!ta(c,b)){break c}while(1){f=1<>2]+(e>>>3&536870908)|0;if(j){i=f|H[g>>2]}else{i=H[g>>2]&(f^-1)}H[g>>2]=i;e=e+1|0;if((d|0)!=(e|0)){continue}break}}if(!Xa(1,h+28|0,b)){break b}d=H[h+28>>2];c=H[a+48>>2];if(d>>>0>H[c+4>>2]-H[c>>2]>>2>>>0){break b}if(d){e=0;Wa(a+72|0,d);c=h+8|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;if(!ta(c,b)){break c}while(1){f=1<>2]+(e>>>3&536870908)|0;if(j){i=f|H[g>>2]}else{i=H[g>>2]&(f^-1)}H[g>>2]=i;e=e+1|0;if((d|0)!=(e|0)){continue}break}}if(!Xa(1,h+28|0,b)){break b}d=H[h+28>>2];c=H[a+48>>2];if(d>>>0>H[c+4>>2]-H[c>>2]>>2>>>0){break b}if(d){e=0;Wa(a+84|0,d);c=h+8|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;if(!ta(c,b)){break c}while(1){f=1<>2]+(e>>>3&536870908)|0;if(j){i=f|H[g>>2]}else{i=H[g>>2]&(f^-1)}H[g>>2]=i;e=e+1|0;if((d|0)!=(e|0)){continue}break}}if(!Xa(1,h+28|0,b)){break b}d=H[h+28>>2];c=H[a+48>>2];if(d>>>0>H[c+4>>2]-H[c>>2]>>2>>>0){break b}if(d){e=0;Wa(a+96|0,d);c=h+8|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;if(!ta(c,b)){break c}while(1){f=1<>2]+(e>>>3&536870908)|0;if(j){i=f|H[g>>2]}else{i=H[g>>2]&(f^-1)}H[g>>2]=i;e=e+1|0;if((d|0)!=(e|0)){continue}break}}e=0;c=H[b+8>>2];f=H[b+12>>2];d=c;c=H[b+20>>2];i=c;g=H[b+16>>2];j=g+4|0;c=j>>>0<4?c+1|0:c;if(d>>>0>>0&(c|0)>=(f|0)|(c|0)>(f|0)){break a}m=H[b>>2];k=m+g|0;l=I[k|0]|I[k+1|0]<<8|(I[k+2|0]<<16|I[k+3|0]<<24);H[b+16>>2]=j;H[b+20>>2]=c;k=d;d=f;c=i;f=g+8|0;c=f>>>0<8?c+1|0:c;if(f>>>0>k>>>0&(c|0)>=(d|0)|(c|0)>(d|0)){break a}d=j+m|0;d=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[b+16>>2]=f;H[b+20>>2]=c;if((d|0)<(l|0)){break a}H[a+16>>2]=d;H[a+12>>2]=l;c=(d>>31)-((l>>31)+(d>>>0>>0)|0)|0;b=d-l|0;if(!c&b>>>0>2147483646|c){break a}e=1;b=b+1|0;H[a+20>>2]=b;c=b>>>1|0;H[a+24>>2]=c;H[a+28>>2]=0-c;if(b&1){break a}H[a+24>>2]=c-1;break a}}e=0}ca=h+32|0;return e|0}function xe(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0;if((b|0)==-1){return 1}g=(b>>>0)/3|0;if(!(H[H[a+24>>2]+(g>>>3&268435452)>>2]>>>g&1)){f=H[a+48>>2];H[a+52>>2]=f;a:{if((f|0)!=H[a+56>>2]){H[f>>2]=b;H[a+52>>2]=f+4;break a}d=pa(4);H[d>>2]=b;c=d+4|0;H[a+56>>2]=c;H[a+52>>2]=c;H[a+48>>2]=d;if(!f){break a}oa(f)}e=-1;d=H[a+4>>2];c=b+1|0;i=(c>>>0)%3|0?c:b-2|0;if((i|0)!=-1){e=H[H[d>>2]+(i<<2)>>2]}b:{h=b-N(g,3)|0;if(h){c=b-1|0;break b}c=b+2|0;if((c|0)!=-1){break b}return 0}if((e|0)==-1){return 0}j=H[H[d>>2]+(c<<2)>>2];if((j|0)==-1){return 0}c=H[a+36>>2];f=c+(e>>>3&536870908)|0;g=H[f>>2];d=1<>2]=d|g;Ua(a+8|0,e,i);c=H[a+36>>2]}g=(j>>>3&536870908)+c|0;d=H[g>>2];c=1<>2]=c|d;Ua(a+8|0,j,(h?-1:2)+b|0)}c=H[a+52>>2];if((c|0)==H[a+48>>2]){return 1}j=a+8|0;while(1){c:{d:{c=c-4|0;b=H[c>>2];if((b|0)==-1){break d}d=(b>>>0)/3|0;f=H[a+24>>2]+(d>>>3&268435452)|0;g=H[f>>2];d=1<>2]=d|g;while(1){i=H[a+4>>2];e=H[H[i>>2]+(b<<2)>>2];if((e|0)==-1){return 0}e:{f:{h=H[a+36>>2]+(e>>>3&536870908)|0;f=H[h>>2];g=1<>2]+(e<<2)>>2];h:{if((d|0)==-1){break h}c=d+1|0;c=(c>>>0)%3|0?c:d-2|0;if((c|0)==-1){break h}d=H[H[i+12>>2]+(c<<2)>>2];if((d|0)!=-1){break g}}H[h>>2]=f|g;Ua(j,e,b);break f}H[h>>2]=f|g;Ua(j,e,b);c=d+1|0;if((((c>>>0)%3|0?c:d-2|0)|0)==-1){break f}c=b-2|0;d=b+1|0;b=-1;c=(d>>>0)%3|0?d:c;if((c|0)!=-1){b=H[H[H[a+4>>2]+12>>2]+(c<<2)>>2]}c=(b>>>0)/3|0;d=1<>2];f=c>>>5|0;i=H[e+(f<<2)>>2];break e}c=-1;g=H[a+4>>2];d=b+1|0;d=(d>>>0)%3|0?d:b-2|0;if((d|0)!=-1){c=H[H[g+12>>2]+(d<<2)>>2]}i:{j:{if((b>>>0)%3|0){e=b-1|0;break j}e=b+2|0;b=-1;if((e|0)==-1){break i}}b=H[H[g+12>>2]+(e<<2)>>2]}g=(b|0)==-1;h=g?-1:(b>>>0)/3|0;k:{if((c|0)!=-1){e=H[a+24>>2];d=(c>>>0)/3|0;f=d>>>5|0;i=H[e+(f<<2)>>2];d=1<>2];f=h>>>5|0;i=H[e+(f<<2)>>2];if(!(d&i)){break e}}c=H[a+52>>2]-4|0;H[a+52>>2]=c;break c}if(g){b=c;break e}if(H[(h>>>3&536870908)+e>>2]>>>h&1){b=c;break e}e=H[a+52>>2];H[e-4>>2]=b;if(H[a+56>>2]!=(e|0)){H[e>>2]=c;c=e+4|0;break d}l:{h=H[a+48>>2];f=e-h|0;g=f>>2;d=g+1|0;if(d>>>0<1073741824){b=f>>>1|0;f=f>>>0>=2147483644?1073741823:b>>>0>d>>>0?b:d;if(f){if(f>>>0>=1073741824){break l}d=pa(f<<2)}else{d=0}b=d+(g<<2)|0;H[b>>2]=c;c=b+4|0;if((e|0)!=(h|0)){while(1){b=b-4|0;e=e-4|0;H[b>>2]=H[e>>2];if((e|0)!=(h|0)){continue}break}}H[a+56>>2]=d+(f<<2);H[a+52>>2]=c;H[a+48>>2]=b;if(!h){break c}oa(h);c=H[a+52>>2];break c}sa();v()}wa();v()}H[(f<<2)+e>>2]=d|i;if((b|0)!=-1){continue}break}return 0}H[a+52>>2]=c}if(H[a+48>>2]!=(c|0)){continue}break}}return 1}function uj(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;var g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0;g=ca-32|0;ca=g;H[a+68>>2]=f;d=H[a+56>>2];e=H[d>>2];d=H[d+4>>2];H[g+24>>2]=0;H[g+16>>2]=0;H[g+20>>2]=0;a:{d=d-e|0;if((d|0)>0){m=a+60|0;d=d>>>2|0;n=d>>>0<=1?1:d;o=a+112|0;while(1){e=H[a+56>>2];d=H[e>>2];if(H[e+4>>2]-d>>2>>>0<=j>>>0){break a}Nb(m,H[d+(j<<2)>>2],g+16|0);i=H[g+20>>2];d=i>>31;h=H[g+16>>2];e=h>>31;f=(d^i)-d+((e^h)-e)|0;k=H[g+24>>2];d=k>>31;e=(d^k)-d|0;d=0;l=e;e=e+f|0;d=l>>>0>e>>>0?1:d;b:{if(!(d|e)){H[g+16>>2]=H[a+108>>2];break b}f=H[a+108>>2];l=f>>31;h=Sj(Rj(f,l,h,h>>31),da,e,d);H[g+16>>2]=h;d=Sj(Rj(f,l,i,i>>31),da,e,d);H[g+20>>2]=d;e=d;d=d>>31;e=(e^d)-d|0;d=h>>31;d=e+((d^h)-d|0)|0;if((k|0)>=0){H[g+24>>2]=f-d;break b}H[g+24>>2]=d-f}d=Ba(o);f=H[g+16>>2];c:{if(d){H[g+24>>2]=0-H[g+24>>2];e=0-H[g+20>>2]|0;H[g+20>>2]=e;f=0-f|0;H[g+16>>2]=f;break c}e=H[g+20>>2]}d:{if((f|0)>=0){f=H[a+108>>2];d=f+H[g+24>>2]|0;f=e+f|0;break d}e:{if((e|0)<0){d=H[g+24>>2];f=d>>31;f=(d^f)-f|0;break e}d=H[g+24>>2];f=d>>31;f=H[a+100>>2]+(f-(d^f)|0)|0}if((d|0)<0){d=e>>31;d=(d^e)-d|0;break d}d=e>>31;d=H[a+100>>2]+(d-(d^e)|0)|0}e=H[a+100>>2];f:{if(!(d|f)){d=e;f=d;break f}if(!((d|0)!=(e|0)|f)){f=d;break f}if(!((e|0)!=(f|0)|d)){d=f;break f}g:{if(f){break g}i=H[a+108>>2];if((i|0)>=(d|0)){break g}d=(i<<1)-d|0;f=0;break f}h:{if((e|0)!=(f|0)){break h}i=H[a+108>>2];if((i|0)<=(d|0)){break h}d=(i<<1)-d|0;break f}i:{if((d|0)!=(e|0)){break i}e=H[a+108>>2];if((e|0)<=(f|0)){break i}f=(e<<1)-f|0;break f}if(d){break f}d=0;e=H[a+108>>2];if((e|0)>=(f|0)){break f}f=(e<<1)-f|0}H[g+12>>2]=d;H[g+8>>2]=f;j:{if(H[a+8>>2]<=0){break j}i=H[a+32>>2];f=0;while(1){d=f<<2;e=H[d+(g+8|0)>>2];h=H[a+16>>2];k:{if((e|0)>(h|0)){H[d+i>>2]=h;break k}d=d+i|0;h=H[a+12>>2];if((h|0)>(e|0)){H[d>>2]=h;break k}H[d>>2]=e}f=f+1|0;e=H[a+8>>2];if((f|0)<(e|0)){continue}break}d=0;if((e|0)<=0){break j}e=j<<3;h=e+c|0;k=b+e|0;while(1){f=d<<2;e=f+h|0;f=H[f+k>>2]+H[f+i>>2]|0;H[e>>2]=f;l:{if((f|0)>H[a+16>>2]){f=f-H[a+20>>2]|0}else{if((f|0)>=H[a+12>>2]){break l}f=f+H[a+20>>2]|0}H[e>>2]=f}d=d+1|0;if((d|0)>2]){continue}break}}j=j+1|0;if((n|0)!=(j|0)){continue}break}}ca=g+32|0;return 1}Ca();v()}function dj(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;var g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0;g=ca-32|0;ca=g;H[a+68>>2]=f;d=H[a+56>>2];e=H[d>>2];d=H[d+4>>2];H[g+24>>2]=0;H[g+16>>2]=0;H[g+20>>2]=0;a:{d=d-e|0;if((d|0)>0){m=a+60|0;d=d>>>2|0;n=d>>>0<=1?1:d;o=a+112|0;while(1){e=H[a+56>>2];d=H[e>>2];if(H[e+4>>2]-d>>2>>>0<=j>>>0){break a}Lb(m,H[d+(j<<2)>>2],g+16|0);i=H[g+20>>2];d=i>>31;h=H[g+16>>2];e=h>>31;f=(d^i)-d+((e^h)-e)|0;k=H[g+24>>2];d=k>>31;e=(d^k)-d|0;d=0;l=e;e=e+f|0;d=l>>>0>e>>>0?1:d;b:{if(!(d|e)){H[g+16>>2]=H[a+108>>2];break b}f=H[a+108>>2];l=f>>31;h=Sj(Rj(f,l,h,h>>31),da,e,d);H[g+16>>2]=h;d=Sj(Rj(f,l,i,i>>31),da,e,d);H[g+20>>2]=d;e=d;d=d>>31;e=(e^d)-d|0;d=h>>31;d=e+((d^h)-d|0)|0;if((k|0)>=0){H[g+24>>2]=f-d;break b}H[g+24>>2]=d-f}d=Ba(o);f=H[g+16>>2];c:{if(d){H[g+24>>2]=0-H[g+24>>2];e=0-H[g+20>>2]|0;H[g+20>>2]=e;f=0-f|0;H[g+16>>2]=f;break c}e=H[g+20>>2]}d:{if((f|0)>=0){f=H[a+108>>2];d=f+H[g+24>>2]|0;f=e+f|0;break d}e:{if((e|0)<0){d=H[g+24>>2];f=d>>31;f=(d^f)-f|0;break e}d=H[g+24>>2];f=d>>31;f=H[a+100>>2]+(f-(d^f)|0)|0}if((d|0)<0){d=e>>31;d=(d^e)-d|0;break d}d=e>>31;d=H[a+100>>2]+(d-(d^e)|0)|0}e=H[a+100>>2];f:{if(!(d|f)){d=e;f=d;break f}if(!((d|0)!=(e|0)|f)){f=d;break f}if(!((e|0)!=(f|0)|d)){d=f;break f}g:{if(f){break g}i=H[a+108>>2];if((i|0)>=(d|0)){break g}d=(i<<1)-d|0;f=0;break f}h:{if((e|0)!=(f|0)){break h}i=H[a+108>>2];if((i|0)<=(d|0)){break h}d=(i<<1)-d|0;break f}i:{if((d|0)!=(e|0)){break i}e=H[a+108>>2];if((e|0)<=(f|0)){break i}f=(e<<1)-f|0;break f}if(d){break f}d=0;e=H[a+108>>2];if((e|0)>=(f|0)){break f}f=(e<<1)-f|0}H[g+12>>2]=d;H[g+8>>2]=f;j:{if(H[a+8>>2]<=0){break j}i=H[a+32>>2];f=0;while(1){d=f<<2;e=H[d+(g+8|0)>>2];h=H[a+16>>2];k:{if((e|0)>(h|0)){H[d+i>>2]=h;break k}d=d+i|0;h=H[a+12>>2];if((h|0)>(e|0)){H[d>>2]=h;break k}H[d>>2]=e}f=f+1|0;e=H[a+8>>2];if((f|0)<(e|0)){continue}break}d=0;if((e|0)<=0){break j}e=j<<3;h=e+c|0;k=b+e|0;while(1){f=d<<2;e=f+h|0;f=H[f+k>>2]+H[f+i>>2]|0;H[e>>2]=f;l:{if((f|0)>H[a+16>>2]){f=f-H[a+20>>2]|0}else{if((f|0)>=H[a+12>>2]){break l}f=f+H[a+20>>2]|0}H[e>>2]=f}d=d+1|0;if((d|0)>2]){continue}break}}j=j+1|0;if((n|0)!=(j|0)){continue}break}}ca=g+32|0;return 1}Ca();v()}function ke(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0;h=ca-80|0;ca=h;a:{b:{if(I[H[a+28>>2]+36|0]<=1){d=H[b+20>>2];f=H[b+16>>2];c=f+4|0;d=c>>>0<4?d+1|0:d;g=H[b+12>>2];if(K[b+8>>2]>>0&(g|0)<=(d|0)|(d|0)>(g|0)){break a}f=f+H[b>>2]|0;j=I[f|0]|I[f+1|0]<<8|(I[f+2|0]<<16|I[f+3|0]<<24);H[b+16>>2]=c;H[b+20>>2]=d;break b}if(!Pc(1,h+76|0,b)){break a}j=H[h+76>>2]}if(!j){break a}d=H[b+8>>2];c=H[b+16>>2];d=Rj(d-c|0,H[b+12>>2]-(H[b+20>>2]+(c>>>0>d>>>0)|0)|0,5,0);c=da;if(d>>>0>>0&(c|0)<=0|(c|0)<0){break a}c=H[a+4>>2];d=H[a+8>>2]-c>>2;c:{if(d>>>0>>0){ya(a+4|0,j-d|0);break c}if(d>>>0<=j>>>0){break c}H[a+8>>2]=c+(j<<2)}p=a+16|0;l=H[a+32>>2];while(1){i=H[b+12>>2];c=i;d=H[b+20>>2];e=H[b+8>>2];f=H[b+16>>2];if((c|0)<=(d|0)&e>>>0<=f>>>0|(c|0)<(d|0)){e=0;break a}n=H[b>>2];q=I[n+f|0];c=d;g=f+1|0;c=g?c:c+1|0;H[b+16>>2]=g;H[b+20>>2]=c;if(e>>>0<=g>>>0&(c|0)>=(i|0)|(c|0)>(i|0)){e=0;break a}g=I[g+n|0];c=d;k=f+2|0;c=k>>>0<2?c+1|0:c;H[b+16>>2]=k;H[b+20>>2]=c;if(e>>>0<=k>>>0&(c|0)>=(i|0)|(c|0)>(i|0)){e=0;break a}k=I[k+n|0];c=d;m=f+3|0;c=m>>>0<3?c+1|0:c;H[b+16>>2]=m;H[b+20>>2]=c;if(e>>>0<=m>>>0&(c|0)>=(i|0)|(c|0)>(i|0)){e=0;break a}e=I[m+n|0];c=d;d=f+4|0;c=d>>>0<4?c+1|0:c;H[b+16>>2]=d;H[b+20>>2]=c;if(q>>>0>4){e=0;break a}if((g-12&255)>>>0<245){e=0;break a}if(!k){e=0;break a}m=Eb(h+8|0);i=(e|0)!=0;d=g-1|0;if(d>>>0<=10){c=H[(d<<2)+13584>>2]}else{c=-1}d=N(c,k);lc(m,q,k,g,i,d,d>>31);d:{d=J[H[a+28>>2]+36>>1];e:{if(((d<<8|d>>>8)&65535)>>>0<=258){c=H[b+20>>2];f=H[b+16>>2];d=f+2|0;c=d>>>0<2?c+1|0:c;e=H[b+12>>2];if(K[b+8>>2]>>0&(e|0)<=(c|0)|(c|0)>(e|0)){break d}f=f+H[b>>2]|0;e=I[f|0]|I[f+1|0]<<8;H[b+16>>2]=d;H[b+20>>2]=c;break e}if(!Pc(1,h+4|0,b)){break d}e=H[h+4>>2]}H[h+68>>2]=e;d=jc(pa(96),m);ea[H[H[l>>2]+8>>2]](l,H[l+12>>2]-H[l+8>>2]>>2,d);d=(H[l+12>>2]-H[l+8>>2]>>2)-1|0;f=d<<2;H[H[f+H[l+8>>2]>>2]+60>>2]=e;H[H[a+4>>2]+(o<<2)>>2]=d;e=H[a+16>>2];c=H[a+20>>2]-e>>2;f:{if((c|0)>(d|0)){break f}H[h>>2]=-1;d=d+1|0;if(d>>>0>c>>>0){Pa(p,d-c|0,h);e=H[p>>2];break f}if(c>>>0<=d>>>0){break f}H[a+20>>2]=(d<<2)+e}H[e+f>>2]=o;e=1;o=o+1|0;if((o|0)!=(j|0)){continue}break a}break}e=0}ca=h+80|0;return e|0}function nd(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0;n=ea[H[H[a>>2]+44>>2]](a)|0;a:{if((n|0)<=0){break a}i=H[b+4>>2]-H[b>>2]>>2;e=ca+-64|0;ca=e;f=Eb(e);d=N(H[3400],n);lc(f,H[H[a+8>>2]+56>>2],n&255,5,0,d,d>>31);f=jc(pa(96),f);F[f+84|0]=1;H[f+72>>2]=H[f+68>>2];mb(f,i);H[f+60>>2]=H[H[a+8>>2]+60>>2];d=H[a+16>>2];H[a+16>>2]=f;if(d){Ga(d)}ca=e- -64|0;h=H[a+16>>2];if(!H[h+80>>2]){break a}j=H[H[h>>2]>>2];if(!j){break a}m=H[c+12>>2];e=m;d=H[c+20>>2];g=H[c+8>>2];k=H[c+16>>2];if((e|0)<=(d|0)&g>>>0<=k>>>0|(d|0)>(e|0)){break a}l=N(i,n);i=j+H[h+48>>2]|0;h=H[c>>2];j=I[h+k|0];e=k+1|0;f=e?d:d+1|0;H[c+16>>2]=e;H[c+20>>2]=f;b:{c:{if(j){if(kd(l,n,c,i)){break c}break a}if((f|0)>=(m|0)&e>>>0>=g>>>0|(f|0)>(m|0)){break a}g=I[e+h|0];f=k+2|0;d=f>>>0<2?d+1|0:d;H[c+16>>2]=f;H[c+20>>2]=d;d=H[H[a+16>>2]+64>>2];d=H[d+4>>2]-H[d>>2]|0;if((g|0)==H[3400]){e=l<<2;if(e>>>0>d>>>0){break a}g=H[c+8>>2];k=H[c+12>>2];j=H[c+20>>2];d=H[c+16>>2];f=e+d|0;j=f>>>0>>0?j+1|0:j;if(f>>>0>g>>>0&(j|0)>=(k|0)|(j|0)>(k|0)){break a}qa(i,d+H[c>>2]|0,e);f=H[c+20>>2];d=e+H[c+16>>2]|0;f=d>>>0>>0?f+1|0:f;H[c+16>>2]=d;H[c+20>>2]=f;break c}if(d>>>0>>0){break a}d=H[c+8>>2];f=H[c+16>>2];e=d-f|0;m=d>>>0>>0;d=H[c+20>>2];k=H[c+12>>2]-(m+d|0)|0;m=Rj(g,0,l,0)>>>0>e>>>0;e=da;if(m&(e|0)>=(k|0)|(e|0)>(k|0)){break a}e=1;if(!l){break b}h=0;while(1){k=H[c+8>>2];j=H[c+12>>2];e=f+g|0;d=e>>>0>>0?d+1|0:d;if(e>>>0>k>>>0&(d|0)>=(j|0)|(d|0)>(j|0)){return 0}qa(i+(h<<2)|0,H[c>>2]+f|0,g);d=H[c+20>>2];f=g+H[c+16>>2]|0;d=f>>>0>>0?d+1|0:d;H[c+16>>2]=f;H[c+20>>2]=d;h=h+1|0;if((l|0)!=(h|0)){continue}break}}e=1;if(!l){break b}d=H[a+20>>2];if(d){e=0;if(ea[H[H[d>>2]+32>>2]](d)|0){break b}}g=0;h=0;d:{if((l|0)<=0){break d}if((l|0)!=1){f=l&-2;while(1){e=g<<2;d=H[e+i>>2];H[e+i>>2]=0-(d&1)^d>>>1;d=e|4;e=H[d+i>>2];H[d+i>>2]=0-(e&1)^e>>>1;g=g+2|0;h=h+2|0;if((f|0)!=(h|0)){continue}break}}if(!(l&1)){break d}d=g<<2;f=H[d+i>>2];H[d+i>>2]=0-(f&1)^f>>>1}e=0}d=e;f=H[a+20>>2];e:{if(!f){break e}if(!(ea[H[H[f>>2]+40>>2]](f,c)|0)){break a}if(d){break e}a=H[a+20>>2];if(!(ea[H[H[a>>2]+44>>2]](a,i,i,l,n,H[b>>2])|0)){break a}}o=1}return o|0}function pb(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0;h=ca-32|0;ca=h;a:{b:{if(H[a+8>>2]<<5>>>0>=b>>>0){break b}if((b|0)<0){break a}b=(b-1>>>5|0)+1|0;c=pa(b<<2);H[h+24>>2]=b;H[h+20>>2]=0;H[h+16>>2]=c;b=H[a>>2];H[h+12>>2]=0;H[h+8>>2]=b;c=H[a+4>>2];H[h+4>>2]=c&31;H[h>>2]=b+(c>>>3&536870908);e=ca-32|0;ca=e;i=H[h+4>>2];g=H[h+12>>2];j=H[h>>2];d=H[h+8>>2];b=(i-g|0)+(j-d<<3)|0;f=H[h+20>>2];c=b+f|0;H[h+20>>2]=c;if(!((c-1^f-1)>>>0<32?f:0)){H[H[h+16>>2]+((c>>>0>=33?c-1>>>5|0:0)<<2)>>2]=0}c=H[h+16>>2]+(f>>>3&536870908)|0;f=f&31;c:{if((f|0)==(g|0)){if((b|0)<=0){break c}if(g){i=32-g|0;f=(b|0)<(i|0)?b:i;i=-1<>>i-f;H[c>>2]=H[c>>2]&(i^-1)|i&H[d>>2];d=d+4|0;c=(g+f>>>3&536870908)+c|0;b=b-f|0}g=(b|0)/32|0;if(b+31>>>0>=63){va(c,d,g<<2)}b=b-(g<<5)|0;if((b|0)<=0){break c}f=c;c=g<<2;g=f+c|0;b=-1>>>32-b|0;H[g>>2]=H[g>>2]&(b^-1)|b&H[c+d>>2];break c}H[e+28>>2]=g;H[e+24>>2]=d;H[e+20>>2]=i;H[e+16>>2]=j;H[e+12>>2]=f;H[e+8>>2]=c;b=H[e+28>>2];c=H[e+24>>2];g=(H[e+20>>2]-b|0)+(H[e+16>>2]-c<<3)|0;d:{if((g|0)<=0){b=H[e+12>>2];d=H[e+8>>2];break d}e:{if(!b){b=H[e+12>>2];break e}d=H[e+12>>2];j=32-d|0;k=32-b|0;f=(g|0)<(k|0)?g:k;i=f>>>0>j>>>0?j:f;l=H[e+8>>2];m=H[l>>2]&(-1<>>j-i^-1);j=H[c>>2]&(-1<>>k-f);H[l>>2]=m|(b>>>0>>0?j<>>b-d|0);c=d+i|0;b=c&31;H[e+12>>2]=b;d=l+(c>>>3&536870908)|0;H[e+8>>2]=d;c=f-i|0;if((c|0)>0){H[d>>2]=H[d>>2]&(-1>>>32-c^-1)|j>>>i+H[e+28>>2];H[e+12>>2]=c;b=c}g=g-f|0;c=H[e+24>>2]+4|0;H[e+24>>2]=c}i=-1<=32){j=i^-1;while(1){d=H[e+8>>2];c=H[c>>2];H[d>>2]=j&H[d>>2]|c<>2]=d+4;H[d+4>>2]=i&H[d+4>>2]|c>>>f;c=H[e+24>>2]+4|0;H[e+24>>2]=c;d=g>>>0>63;g=g-32|0;if(d){continue}break}}d=H[e+8>>2];if((g|0)<=0){break d}j=f;f=(g|0)>(f|0)?f:g;j=H[d>>2]&(i&-1>>>j-f^-1);i=H[c>>2]&-1>>>32-g;H[d>>2]=j|i<>2]=c;d=(b>>>3&536870908)+d|0;H[e+8>>2]=d;b=g-f|0;if((b|0)<=0){b=c;break d}H[d>>2]=H[d>>2]&(-1>>>32-b^-1)|i>>>f;H[e+12>>2]=b}H[e+4>>2]=b;H[e>>2]=d}ca=e+32|0;b=H[a>>2];H[a>>2]=H[h+16>>2];H[h+16>>2]=b;c=H[a+4>>2];H[a+4>>2]=H[h+20>>2];H[h+20>>2]=c;c=H[a+8>>2];H[a+8>>2]=H[h+24>>2];H[h+24>>2]=c;if(!b){break b}oa(b)}ca=h+32|0;return}sa();v()}function Ne(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0;c=J[b+38>>1];a:{if(!c){break a}b:{if(c>>>0<=511){g=H[b+8>>2];e=H[b+12>>2];d=H[b+20>>2];c=H[b+16>>2];i=c+4|0;d=i>>>0<4?d+1|0:d;if(g>>>0>>0&(d|0)>=(e|0)|(d|0)>(e|0)){break a}c=c+H[b>>2]|0;f=I[c|0]|I[c+1|0]<<8|(I[c+2|0]<<16|I[c+3|0]<<24);H[a+12>>2]=f;d=H[b+20>>2];c=H[b+16>>2]+4|0;d=c>>>0<4?d+1|0:d;H[b+16>>2]=c;H[b+20>>2]=d;break b}if(!hb(1,a+12|0,b)){break a}c=H[b+16>>2];d=H[b+20>>2];f=H[a+12>>2]}e=H[b+8>>2];i=e-c|0;c=H[b+12>>2]-(d+(c>>>0>e>>>0)|0)|0;if(i>>>0>>6>>>0&(c|0)<=0|(c|0)<0){break a}d=H[a>>2];c=H[a+4>>2]-d>>2;c:{if(c>>>0>>0){ya(a,f-c|0);f=H[a+12>>2];break c}if(c>>>0<=f>>>0){break c}H[a+4>>2]=d+(f<<2)}if(!f){return 1}c=H[b+16>>2];d=H[b+20>>2];l=H[a>>2];i=H[b+8>>2];j=H[b+12>>2];g=0;while(1){if((d|0)>=(j|0)&c>>>0>=i>>>0|(d|0)>(j|0)){return 0}m=H[b>>2];k=I[m+c|0];c=c+1|0;d=c?d:d+1|0;H[b+16>>2]=c;H[b+20>>2]=d;e=k>>>2|0;h=0;d:{e:{f:{g:{n=k&3;switch(n|0){case 0:break e;case 3:break g;default:break f}}e=e+g|0;if(e>>>0>=f>>>0){return 0}ra(l+(g<<2)|0,0,(k&252)+4|0);g=e;break d}while(1){if((c|0)==(i|0)&(d|0)==(j|0)){break a}f=I[c+m|0];c=c+1|0;d=c?d:d+1|0;H[b+16>>2]=c;H[b+20>>2]=d;e=f<<(h<<3|6)|e;h=h+1|0;if((n|0)!=(h|0)){continue}break}}H[l+(g<<2)>>2]=e}f=H[a+12>>2];g=g+1|0;if(f>>>0>g>>>0){continue}break}b=a+16|0;i=H[a>>2];d=H[a+16>>2];c=H[a+20>>2]-d|0;h:{if(c>>>0<=16383){ya(b,4096-(c>>>2|0)|0);break h}if((c|0)==16384){break h}H[a+20>>2]=d+16384}c=a+28|0;g=H[c>>2];d=H[a+32>>2]-g>>3;i:{if(d>>>0>>0){ob(c,f-d|0);g=H[c>>2];break i}if(d>>>0>f>>>0){H[a+32>>2]=(f<<3)+g}if(!f){break a}}d=H[b>>2];b=0;a=0;while(1){c=i+(b<<2)|0;h=H[c>>2];e=a;j=(b<<3)+g|0;H[j+4>>2]=a;H[j>>2]=h;c=H[c>>2];a=c+a|0;if(a>>>0>4096){break a}j:{if(a>>>0<=e>>>0){break j}h=0;j=c&7;if(j){while(1){H[d+(e<<2)>>2]=b;e=e+1|0;h=h+1|0;if((j|0)!=(h|0)){continue}break}}if(c-1>>>0<=6){break j}while(1){c=d+(e<<2)|0;H[c>>2]=b;H[c+28>>2]=b;H[c+24>>2]=b;H[c+20>>2]=b;H[c+16>>2]=b;H[c+12>>2]=b;H[c+8>>2]=b;H[c+4>>2]=b;e=e+8|0;if((e|0)!=(a|0)){continue}break}}b=b+1|0;if((f|0)!=(b|0)){continue}break}o=(a|0)==4096}return o}function Ni(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;var g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0;g=ca-48|0;ca=g;d=H[a+8>>2];if(d-2>>>0<=28){H[a+76>>2]=d;e=-1<>2]=d;H[a+80>>2]=e^-1;H[a+92>>2]=(d|0)/2;L[a+88>>2]=O(2)/O(d|0)}H[a+52>>2]=f;d=H[a+40>>2];e=H[d>>2];d=H[d+4>>2];H[g+16>>2]=0;H[g+8>>2]=0;H[g+12>>2]=0;a:{d=d-e|0;if((d|0)>0){m=a+8|0;n=a+44|0;d=d>>>2|0;o=d>>>0<=1?1:d;p=a+96|0;while(1){e=H[a+40>>2];d=H[e>>2];if(H[e+4>>2]-d>>2>>>0<=j>>>0){break a}Nb(n,H[d+(j<<2)>>2],g+8|0);h=H[g+12>>2];d=h>>31;i=H[g+8>>2];e=i>>31;f=(d^h)-d+((e^i)-e)|0;l=H[g+16>>2];d=l>>31;e=(d^l)-d|0;d=0;k=e;e=e+f|0;d=k>>>0>e>>>0?1:d;b:{if(!(d|e)){H[g+8>>2]=H[a+92>>2];break b}f=H[a+92>>2];k=f>>31;i=Sj(Rj(f,k,i,i>>31),da,e,d);H[g+8>>2]=i;d=Sj(Rj(f,k,h,h>>31),da,e,d);H[g+12>>2]=d;e=d>>31;e=(d^e)-e|0;d=i>>31;d=e+((d^i)-d|0)|0;if((l|0)>=0){H[g+16>>2]=f-d;break b}H[g+16>>2]=d-f}d=Ba(p);f=H[g+8>>2];c:{if(d){H[g+16>>2]=0-H[g+16>>2];e=0-H[g+12>>2]|0;H[g+12>>2]=e;f=0-f|0;H[g+8>>2]=f;break c}e=H[g+12>>2]}d:{if((f|0)>=0){f=H[a+92>>2];d=f+H[g+16>>2]|0;f=e+f|0;break d}e:{if((e|0)<0){d=H[g+16>>2];f=d>>31;f=(d^f)-f|0;break e}d=H[g+16>>2];f=d>>31;f=H[a+84>>2]+(f-(d^f)|0)|0}if((d|0)<0){d=e>>31;d=(d^e)-d|0;break d}d=e>>31;d=H[a+84>>2]+(d-(d^e)|0)|0}e=H[a+84>>2];f:{if(!(d|f)){d=e;f=d;break f}if(!((d|0)!=(e|0)|f)){f=d;break f}if(!((e|0)!=(f|0)|d)){d=f;break f}g:{if(f){break g}h=H[a+92>>2];if((h|0)>=(d|0)){break g}d=(h<<1)-d|0;f=0;break f}h:{if((e|0)!=(f|0)){break h}h=H[a+92>>2];if((h|0)<=(d|0)){break h}d=(h<<1)-d|0;break f}i:{if((d|0)!=(e|0)){break i}e=H[a+92>>2];if((e|0)<=(f|0)){break i}f=(e<<1)-f|0;break f}if(d){break f}d=0;e=H[a+92>>2];if((e|0)>=(f|0)){break f}f=(e<<1)-f|0}e=j<<3;h=e+b|0;i=H[h>>2];h=H[h+4>>2];H[g+36>>2]=d;H[g+32>>2]=f;H[g+24>>2]=i;H[g+28>>2]=h;qc(g+40|0,m,g+32|0,g+24|0);d=c+e|0;H[d>>2]=H[g+40>>2];H[d+4>>2]=H[g+44>>2];j=j+1|0;if((o|0)!=(j|0)){continue}break}}ca=g+48|0;return 1}Ca();v()}function Ii(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;var g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0;g=ca-48|0;ca=g;d=H[a+8>>2];if(d-2>>>0<=28){H[a+76>>2]=d;e=-1<>2]=d;H[a+80>>2]=e^-1;H[a+92>>2]=(d|0)/2;L[a+88>>2]=O(2)/O(d|0)}H[a+52>>2]=f;d=H[a+40>>2];e=H[d>>2];d=H[d+4>>2];H[g+16>>2]=0;H[g+8>>2]=0;H[g+12>>2]=0;a:{d=d-e|0;if((d|0)>0){m=a+8|0;n=a+44|0;d=d>>>2|0;o=d>>>0<=1?1:d;p=a+96|0;while(1){e=H[a+40>>2];d=H[e>>2];if(H[e+4>>2]-d>>2>>>0<=j>>>0){break a}Lb(n,H[d+(j<<2)>>2],g+8|0);h=H[g+12>>2];d=h>>31;i=H[g+8>>2];e=i>>31;f=(d^h)-d+((e^i)-e)|0;l=H[g+16>>2];d=l>>31;e=(d^l)-d|0;d=0;k=e;e=e+f|0;d=k>>>0>e>>>0?1:d;b:{if(!(d|e)){H[g+8>>2]=H[a+92>>2];break b}f=H[a+92>>2];k=f>>31;i=Sj(Rj(f,k,i,i>>31),da,e,d);H[g+8>>2]=i;d=Sj(Rj(f,k,h,h>>31),da,e,d);H[g+12>>2]=d;e=d>>31;e=(d^e)-e|0;d=i>>31;d=e+((d^i)-d|0)|0;if((l|0)>=0){H[g+16>>2]=f-d;break b}H[g+16>>2]=d-f}d=Ba(p);f=H[g+8>>2];c:{if(d){H[g+16>>2]=0-H[g+16>>2];e=0-H[g+12>>2]|0;H[g+12>>2]=e;f=0-f|0;H[g+8>>2]=f;break c}e=H[g+12>>2]}d:{if((f|0)>=0){f=H[a+92>>2];d=f+H[g+16>>2]|0;f=e+f|0;break d}e:{if((e|0)<0){d=H[g+16>>2];f=d>>31;f=(d^f)-f|0;break e}d=H[g+16>>2];f=d>>31;f=H[a+84>>2]+(f-(d^f)|0)|0}if((d|0)<0){d=e>>31;d=(d^e)-d|0;break d}d=e>>31;d=H[a+84>>2]+(d-(d^e)|0)|0}e=H[a+84>>2];f:{if(!(d|f)){d=e;f=d;break f}if(!((d|0)!=(e|0)|f)){f=d;break f}if(!((e|0)!=(f|0)|d)){d=f;break f}g:{if(f){break g}h=H[a+92>>2];if((h|0)>=(d|0)){break g}d=(h<<1)-d|0;f=0;break f}h:{if((e|0)!=(f|0)){break h}h=H[a+92>>2];if((h|0)<=(d|0)){break h}d=(h<<1)-d|0;break f}i:{if((d|0)!=(e|0)){break i}e=H[a+92>>2];if((e|0)<=(f|0)){break i}f=(e<<1)-f|0;break f}if(d){break f}d=0;e=H[a+92>>2];if((e|0)>=(f|0)){break f}f=(e<<1)-f|0}e=j<<3;h=e+b|0;i=H[h>>2];h=H[h+4>>2];H[g+36>>2]=d;H[g+32>>2]=f;H[g+24>>2]=i;H[g+28>>2]=h;qc(g+40|0,m,g+32|0,g+24|0);d=c+e|0;H[d>>2]=H[g+40>>2];H[d+4>>2]=H[g+44>>2];j=j+1|0;if((o|0)!=(j|0)){continue}break}}ca=g+48|0;return 1}Ca();v()}function Wi(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;var g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0;g=ca-48|0;ca=g;d=H[a+8>>2];if(d-2>>>0<=28){H[a+76>>2]=d;e=-1<>2]=d;H[a+80>>2]=e^-1;H[a+92>>2]=(d|0)/2;L[a+88>>2]=O(2)/O(d|0)}H[a+52>>2]=f;d=H[a+40>>2];e=H[d>>2];d=H[d+4>>2];H[g+16>>2]=0;H[g+8>>2]=0;H[g+12>>2]=0;a:{d=d-e|0;if((d|0)>0){m=a+8|0;n=a+44|0;d=d>>>2|0;o=d>>>0<=1?1:d;p=a+96|0;while(1){e=H[a+40>>2];d=H[e>>2];if(H[e+4>>2]-d>>2>>>0<=j>>>0){break a}Nb(n,H[d+(j<<2)>>2],g+8|0);h=H[g+12>>2];d=h>>31;i=H[g+8>>2];e=i>>31;f=(d^h)-d+((e^i)-e)|0;l=H[g+16>>2];d=l>>31;e=(d^l)-d|0;d=0;k=e;e=e+f|0;d=k>>>0>e>>>0?1:d;b:{if(!(d|e)){H[g+8>>2]=H[a+92>>2];break b}f=H[a+92>>2];k=f>>31;i=Sj(Rj(f,k,i,i>>31),da,e,d);H[g+8>>2]=i;d=Sj(Rj(f,k,h,h>>31),da,e,d);H[g+12>>2]=d;e=d>>31;e=(d^e)-e|0;d=i>>31;d=e+((d^i)-d|0)|0;if((l|0)>=0){H[g+16>>2]=f-d;break b}H[g+16>>2]=d-f}d=Ba(p);f=H[g+8>>2];c:{if(d){H[g+16>>2]=0-H[g+16>>2];e=0-H[g+12>>2]|0;H[g+12>>2]=e;f=0-f|0;H[g+8>>2]=f;break c}e=H[g+12>>2]}d:{if((f|0)>=0){f=H[a+92>>2];d=f+H[g+16>>2]|0;f=e+f|0;break d}e:{if((e|0)<0){d=H[g+16>>2];f=d>>31;f=(d^f)-f|0;break e}d=H[g+16>>2];f=d>>31;f=H[a+84>>2]+(f-(d^f)|0)|0}if((d|0)<0){d=e>>31;d=(d^e)-d|0;break d}d=e>>31;d=H[a+84>>2]+(d-(d^e)|0)|0}e=H[a+84>>2];f:{if(!(d|f)){d=e;f=d;break f}if(!((d|0)!=(e|0)|f)){f=d;break f}if(!((e|0)!=(f|0)|d)){d=f;break f}g:{if(f){break g}h=H[a+92>>2];if((h|0)>=(d|0)){break g}d=(h<<1)-d|0;f=0;break f}h:{if((e|0)!=(f|0)){break h}h=H[a+92>>2];if((h|0)<=(d|0)){break h}d=(h<<1)-d|0;break f}i:{if((d|0)!=(e|0)){break i}e=H[a+92>>2];if((e|0)<=(f|0)){break i}f=(e<<1)-f|0;break f}if(d){break f}d=0;e=H[a+92>>2];if((e|0)>=(f|0)){break f}f=(e<<1)-f|0}e=j<<3;h=e+b|0;i=H[h+4>>2];H[g+40>>2]=H[h>>2];H[g+44>>2]=i;H[g+28>>2]=d;H[g+24>>2]=f;rc(g+32|0,m,g+24|0,g+40|0);d=c+e|0;H[d>>2]=H[g+32>>2];H[d+4>>2]=H[g+36>>2];j=j+1|0;if((o|0)!=(j|0)){continue}break}}ca=g+48|0;return 1}Ca();v()}function Ri(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;var g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0;g=ca-48|0;ca=g;d=H[a+8>>2];if(d-2>>>0<=28){H[a+76>>2]=d;e=-1<>2]=d;H[a+80>>2]=e^-1;H[a+92>>2]=(d|0)/2;L[a+88>>2]=O(2)/O(d|0)}H[a+52>>2]=f;d=H[a+40>>2];e=H[d>>2];d=H[d+4>>2];H[g+16>>2]=0;H[g+8>>2]=0;H[g+12>>2]=0;a:{d=d-e|0;if((d|0)>0){m=a+8|0;n=a+44|0;d=d>>>2|0;o=d>>>0<=1?1:d;p=a+96|0;while(1){e=H[a+40>>2];d=H[e>>2];if(H[e+4>>2]-d>>2>>>0<=j>>>0){break a}Lb(n,H[d+(j<<2)>>2],g+8|0);h=H[g+12>>2];d=h>>31;i=H[g+8>>2];e=i>>31;f=(d^h)-d+((e^i)-e)|0;l=H[g+16>>2];d=l>>31;e=(d^l)-d|0;d=0;k=e;e=e+f|0;d=k>>>0>e>>>0?1:d;b:{if(!(d|e)){H[g+8>>2]=H[a+92>>2];break b}f=H[a+92>>2];k=f>>31;i=Sj(Rj(f,k,i,i>>31),da,e,d);H[g+8>>2]=i;d=Sj(Rj(f,k,h,h>>31),da,e,d);H[g+12>>2]=d;e=d>>31;e=(d^e)-e|0;d=i>>31;d=e+((d^i)-d|0)|0;if((l|0)>=0){H[g+16>>2]=f-d;break b}H[g+16>>2]=d-f}d=Ba(p);f=H[g+8>>2];c:{if(d){H[g+16>>2]=0-H[g+16>>2];e=0-H[g+12>>2]|0;H[g+12>>2]=e;f=0-f|0;H[g+8>>2]=f;break c}e=H[g+12>>2]}d:{if((f|0)>=0){f=H[a+92>>2];d=f+H[g+16>>2]|0;f=e+f|0;break d}e:{if((e|0)<0){d=H[g+16>>2];f=d>>31;f=(d^f)-f|0;break e}d=H[g+16>>2];f=d>>31;f=H[a+84>>2]+(f-(d^f)|0)|0}if((d|0)<0){d=e>>31;d=(d^e)-d|0;break d}d=e>>31;d=H[a+84>>2]+(d-(d^e)|0)|0}e=H[a+84>>2];f:{if(!(d|f)){d=e;f=d;break f}if(!((d|0)!=(e|0)|f)){f=d;break f}if(!((e|0)!=(f|0)|d)){d=f;break f}g:{if(f){break g}h=H[a+92>>2];if((h|0)>=(d|0)){break g}d=(h<<1)-d|0;f=0;break f}h:{if((e|0)!=(f|0)){break h}h=H[a+92>>2];if((h|0)<=(d|0)){break h}d=(h<<1)-d|0;break f}i:{if((d|0)!=(e|0)){break i}e=H[a+92>>2];if((e|0)<=(f|0)){break i}f=(e<<1)-f|0;break f}if(d){break f}d=0;e=H[a+92>>2];if((e|0)>=(f|0)){break f}f=(e<<1)-f|0}e=j<<3;h=e+b|0;i=H[h+4>>2];H[g+40>>2]=H[h>>2];H[g+44>>2]=i;H[g+28>>2]=d;H[g+24>>2]=f;rc(g+32|0,m,g+24|0,g+40|0);d=c+e|0;H[d>>2]=H[g+32>>2];H[d+4>>2]=H[g+36>>2];j=j+1|0;if((o|0)!=(j|0)){continue}break}}ca=g+48|0;return 1}Ca();v()}function Ge(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0;f=ca-16|0;ca=f;c=H[a+4>>2];H[a+40>>2]=H[a>>2];H[a+44>>2]=c;c=H[a+36>>2];H[a+72>>2]=H[a+32>>2];H[a+76>>2]=c;d=H[a+28>>2];c=a- -64|0;H[c>>2]=H[a+24>>2];H[c+4>>2]=d;c=H[a+20>>2];H[a+56>>2]=H[a+16>>2];H[a+60>>2]=c;c=H[a+12>>2];H[a+48>>2]=H[a+8>>2];H[a+52>>2]=c;a:{b:{if(Db(a+40|0,1,f+8|0)){c=H[a+44>>2];H[a>>2]=H[a+40>>2];H[a+4>>2]=c;c=H[a+76>>2];H[a+32>>2]=H[a+72>>2];H[a+36>>2]=c;c=H[a+68>>2];H[a+24>>2]=H[a+64>>2];H[a+28>>2]=c;d=H[a+60>>2];h=d;c=H[a+56>>2];H[a+16>>2]=c;H[a+20>>2]=d;e=H[a+52>>2];d=H[a+48>>2];H[a+8>>2]=d;H[a+12>>2]=e;i=d-c|0;g=H[f+12>>2];e=e-((c>>>0>d>>>0)+h|0)|0;d=H[f+8>>2];if((g|0)==(e|0)&i>>>0>=d>>>0|e>>>0>g>>>0){break b}}c=0;break a}e=h+g|0;c=c+d|0;e=c>>>0>>0?e+1|0:e;H[a+16>>2]=c;H[a+20>>2]=e;c:{if(J[a+38>>1]<=513){c=H[a+4>>2];H[a+96>>2]=H[a>>2];H[a+100>>2]=c;c=H[a+36>>2];H[a+128>>2]=H[a+32>>2];H[a+132>>2]=c;c=H[a+28>>2];H[a+120>>2]=H[a+24>>2];H[a+124>>2]=c;c=H[a+20>>2];H[a+112>>2]=H[a+16>>2];H[a+116>>2]=c;c=H[a+12>>2];H[a+104>>2]=H[a+8>>2];H[a+108>>2]=c;d:{if(Db(a+96|0,1,f+8|0)){c=H[a+100>>2];H[a>>2]=H[a+96>>2];H[a+4>>2]=c;c=H[a+132>>2];H[a+32>>2]=H[a+128>>2];H[a+36>>2]=c;c=H[a+124>>2];H[a+24>>2]=H[a+120>>2];H[a+28>>2]=c;d=H[a+116>>2];h=d;c=H[a+112>>2];H[a+16>>2]=c;H[a+20>>2]=d;e=H[a+108>>2];d=H[a+104>>2];H[a+8>>2]=d;H[a+12>>2]=e;i=d-c|0;g=H[f+12>>2];e=e-((c>>>0>d>>>0)+h|0)|0;d=H[f+8>>2];if((g|0)==(e|0)&i>>>0>=d>>>0|e>>>0>g>>>0){break d}}c=0;break a}e=h+g|0;c=c+d|0;e=c>>>0>>0?e+1|0:e;H[a+16>>2]=c;H[a+20>>2]=e;break c}c=0;if(!ta(a+80|0,a)){break a}}c=0;if(!Fe(a)){break a}c=H[a+4>>2];H[b>>2]=H[a>>2];H[b+4>>2]=c;c=H[a+36>>2];H[b+32>>2]=H[a+32>>2];H[b+36>>2]=c;c=H[a+28>>2];H[b+24>>2]=H[a+24>>2];H[b+28>>2]=c;c=H[a+20>>2];H[b+16>>2]=H[a+16>>2];H[b+20>>2]=c;c=H[a+12>>2];H[b+8>>2]=H[a+8>>2];H[b+12>>2]=c;c=1}ca=f+16|0;return c}function oe(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0;if(!H[a+64>>2]){c=pa(32);H[c+16>>2]=0;H[c+20>>2]=0;H[c+8>>2]=0;H[c>>2]=0;H[c+4>>2]=0;H[c+24>>2]=0;H[c+28>>2]=0;d=H[a+64>>2];H[a+64>>2]=c;if(d){c=H[d>>2];if(c){H[d+4>>2]=c;oa(c)}oa(d);c=H[a+64>>2]}H[a>>2]=c;d=H[c+20>>2];H[a+8>>2]=H[c+16>>2];H[a+12>>2]=d;d=H[c+24>>2];c=H[c+28>>2];H[a+48>>2]=0;H[a+52>>2]=0;H[a+40>>2]=0;H[a+44>>2]=0;H[a+16>>2]=d;H[a+20>>2]=c}a:{F[a+24|0]=I[b+24|0];H[a+28>>2]=H[b+28>>2];F[a+32|0]=I[b+32|0];c=H[b+44>>2];H[a+40>>2]=H[b+40>>2];H[a+44>>2]=c;c=H[b+52>>2];H[a+48>>2]=H[b+48>>2];H[a+52>>2]=c;H[a+56>>2]=H[b+56>>2];c=H[b+12>>2];H[a+8>>2]=H[b+8>>2];H[a+12>>2]=c;c=H[b+20>>2];H[a+16>>2]=H[b+16>>2];H[a+20>>2]=c;H[a+60>>2]=H[b+60>>2];c=H[b>>2];b:{if(!c){H[a>>2]=0;d=1;break b}g=H[a>>2];d=0;if(!g){break b}d=H[c>>2];c=H[c+4>>2]-d|0;se(g,d,c,0);d=1}c:{if(!d){break c}F[a+84|0]=I[b+84|0];H[a+80>>2]=H[b+80>>2];if((a|0)!=(b|0)){Cb(a+68|0,H[b+68>>2],H[b+72>>2])}f=H[b+88>>2];d:{if(f){e=pa(40);b=H[f>>2];H[e+16>>2]=0;H[e+8>>2]=0;H[e+12>>2]=0;H[e>>2]=b;c=H[f+12>>2];b=H[f+8>>2];if((c|0)!=(b|0)){c=c-b|0;if((c|0)<0){break a}b=pa(c);H[e+12>>2]=b;H[e+8>>2]=b;H[e+16>>2]=b+c;c=H[f+8>>2];h=H[f+12>>2];e:{if((c|0)==(h|0)){break e}g=(c^-1)+h|0;d=h-c&7;if(d){while(1){F[b|0]=I[c|0];b=b+1|0;c=c+1|0;i=i+1|0;if((d|0)!=(i|0)){continue}break}}if(g>>>0<7){break e}while(1){F[b|0]=I[c|0];F[b+1|0]=I[c+1|0];F[b+2|0]=I[c+2|0];F[b+3|0]=I[c+3|0];F[b+4|0]=I[c+4|0];F[b+5|0]=I[c+5|0];F[b+6|0]=I[c+6|0];F[b+7|0]=I[c+7|0];b=b+8|0;c=c+8|0;if((h|0)!=(c|0)){continue}break}}H[e+12>>2]=b}b=H[f+36>>2];H[e+32>>2]=H[f+32>>2];H[e+36>>2]=b;b=H[f+28>>2];H[e+24>>2]=H[f+24>>2];H[e+28>>2]=b;b=H[a+88>>2];H[a+88>>2]=e;if(b){break d}break c}b=H[a+88>>2];H[a+88>>2]=0;if(!b){break c}}a=H[b+8>>2];if(a){H[b+12>>2]=a;oa(a)}oa(b)}return}sa();v()}function og(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0;f=ca-32|0;ca=f;e=f+8|0;c=ca-80|0;ca=c;a=H[b+36>>2];H[c+72>>2]=H[b+32>>2];H[c+76>>2]=a;d=H[b+28>>2];a=c- -64|0;H[a>>2]=H[b+24>>2];H[a+4>>2]=d;a=H[b+20>>2];H[c+56>>2]=H[b+16>>2];H[c+60>>2]=a;a=H[b+12>>2];H[c+48>>2]=H[b+8>>2];H[c+52>>2]=a;a=H[b+4>>2];H[c+40>>2]=H[b>>2];H[c+44>>2]=a;nc(c+8|0,c+40|0,c+24|0);a=H[c+8>>2];a:{if(a){H[e>>2]=a;a=e+4|0;if(F[c+23|0]>=0){b=c+8|4;e=H[b+4>>2];H[a>>2]=H[b>>2];H[a+4>>2]=e;H[a+8>>2]=H[b+8>>2];break a}za(a,H[c+12>>2],H[c+16>>2]);if(F[c+23|0]>=0){break a}oa(H[c+12>>2]);break a}if(F[c+23|0]<0){oa(H[c+12>>2])}a=I[c+31|0];if(a>>>0>=2){b=pa(32);F[b+26|0]=0;a=I[1477]|I[1478]<<8;F[b+24|0]=a;F[b+25|0]=a>>>8;a=I[1473]|I[1474]<<8|(I[1475]<<16|I[1476]<<24);d=I[1469]|I[1470]<<8|(I[1471]<<16|I[1472]<<24);F[b+16|0]=d;F[b+17|0]=d>>>8;F[b+18|0]=d>>>16;F[b+19|0]=d>>>24;F[b+20|0]=a;F[b+21|0]=a>>>8;F[b+22|0]=a>>>16;F[b+23|0]=a>>>24;a=I[1465]|I[1466]<<8|(I[1467]<<16|I[1468]<<24);d=I[1461]|I[1462]<<8|(I[1463]<<16|I[1464]<<24);F[b+8|0]=d;F[b+9|0]=d>>>8;F[b+10|0]=d>>>16;F[b+11|0]=d>>>24;F[b+12|0]=a;F[b+13|0]=a>>>8;F[b+14|0]=a>>>16;F[b+15|0]=a>>>24;a=I[1457]|I[1458]<<8|(I[1459]<<16|I[1460]<<24);d=I[1453]|I[1454]<<8|(I[1455]<<16|I[1456]<<24);F[b|0]=d;F[b+1|0]=d>>>8;F[b+2|0]=d>>>16;F[b+3|0]=d>>>24;F[b+4|0]=a;F[b+5|0]=a>>>8;F[b+6|0]=a>>>16;F[b+7|0]=a>>>24;H[c+8>>2]=-1;a=c+8|4;za(a,b,26);d=F[c+23|0];H[e>>2]=H[c+8>>2];e=e+4|0;if((d|0)>=0){d=H[a+4>>2];H[e>>2]=H[a>>2];H[e+4>>2]=d;H[e+8>>2]=H[a+8>>2];oa(b);break a}za(e,H[c+12>>2],H[c+16>>2]);if(F[c+23|0]<0){oa(H[c+12>>2])}oa(b);break a}H[e>>2]=0;H[e+4>>2]=0;H[e+16>>2]=a;H[e+8>>2]=0;H[e+12>>2]=0}ca=c+80|0;a=H[f+24>>2];if(F[f+23|0]<0){oa(H[f+12>>2])}ca=f+32|0;return a|0}function Xd(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0;k=ca-16|0;ca=k;H[k+8>>2]=c;h=H[a+12>>2];d=H[a+8>>2];g=h-d>>2;a:{if((g|0)>(b|0)){break a}e=b+1|0;if(e>>>0>g>>>0){l=e-g|0;f=H[a+16>>2];d=H[a+12>>2];if(l>>>0<=f-d>>2>>>0){if(l){e=d;d=l<<2;d=ra(e,0,d)+d|0}H[a+12>>2]=d;break a}b:{c:{d:{m=H[a+8>>2];g=d-m>>2;i=g+l|0;if(i>>>0<1073741824){e=f-m|0;f=e>>>1|0;e=e>>>0>=2147483644?1073741823:f>>>0>i>>>0?f:i;if(e){if(e>>>0>=1073741824){break d}j=pa(e<<2)}h=(g<<2)+j|0;f=l<<2;i=ra(h,0,f);g=f+i|0;e=(e<<2)+j|0;if((d|0)==(m|0)){break c}while(1){d=d-4|0;f=H[d>>2];H[d>>2]=0;h=h-4|0;H[h>>2]=f;if((d|0)!=(m|0)){continue}break}H[a+16>>2]=e;e=H[a+12>>2];H[a+12>>2]=g;d=H[a+8>>2];H[a+8>>2]=h;if((d|0)==(e|0)){break b}while(1){e=e-4|0;f=H[e>>2];H[e>>2]=0;if(f){Ga(f)}if((d|0)!=(e|0)){continue}break}break b}sa();v()}wa();v()}H[a+16>>2]=e;H[a+12>>2]=g;H[a+8>>2]=i}if(d){oa(d)}break a}if(e>>>0>=g>>>0){break a}d=d+(e<<2)|0;if((d|0)!=(h|0)){while(1){h=h-4|0;c=H[h>>2];H[h>>2]=0;if(c){Ga(c)}if((d|0)!=(h|0)){continue}break}c=H[k+8>>2]}H[a+12>>2]=d}e:{f:{d=H[c+56>>2];g:{if((d|0)>4){break g}j=N(d,12)+a|0;d=H[j+24>>2];if((d|0)!=H[j+28>>2]){H[d>>2]=b;H[j+24>>2]=d+4;break g}i=H[j+20>>2];g=d-i|0;f=g>>2;e=f+1|0;if(e>>>0>=1073741824){break f}d=g>>>1|0;e=g>>>0>=2147483644?1073741823:d>>>0>e>>>0?d:e;if(e){if(e>>>0>=1073741824){break e}d=pa(e<<2)}else{d=0}f=d+(f<<2)|0;H[f>>2]=b;d=va(d,i,g);H[j+20>>2]=d;H[j+24>>2]=f+4;H[j+28>>2]=d+(e<<2);if(!i){break g}oa(i)}H[c+60>>2]=b;a=H[a+8>>2];H[k+8>>2]=0;a=a+(b<<2)|0;b=H[a>>2];H[a>>2]=c;if(b){Ga(b)}a=H[k+8>>2];H[k+8>>2]=0;if(a){Ga(a)}ca=k+16|0;return}sa();v()}wa();v()}function Og(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0;i=c;d=a;a:{if(H[a+12>>2]==(b|0)){break a}a=b;b=H[d+4>>2];e=H[d>>2];if((b|0)!=(e|0)){while(1){c=b-12|0;if(F[b-1|0]<0){oa(H[c>>2])}b=c;if((e|0)!=(b|0)){continue}break}}H[d+12>>2]=a;H[d+4>>2]=e;c=H[a>>2];j=a+4|0;if((c|0)==(j|0)){break a}while(1){a=H[d+4>>2];b:{if((a|0)!=H[d+8>>2]){c:{if(F[c+27|0]>=0){b=H[c+20>>2];H[a>>2]=H[c+16>>2];H[a+4>>2]=b;H[a+8>>2]=H[c+24>>2];break c}za(a,H[c+16>>2],H[c+20>>2])}H[d+4>>2]=a+12;break b}g=0;d:{e:{f:{a=H[d+4>>2];e=H[d>>2];f=(a-e|0)/12|0;b=f+1|0;if(b>>>0<357913942){h=(H[d+8>>2]-e|0)/12|0;k=h<<1;b=h>>>0>=178956970?357913941:b>>>0>>0?k:b;if(b){if(b>>>0>=357913942){break f}g=pa(N(b,12))}h=N(b,12);b=N(f,12)+g|0;g:{if(F[c+27|0]>=0){f=H[c+20>>2];H[b>>2]=H[c+16>>2];H[b+4>>2]=f;H[b+8>>2]=H[c+24>>2];break g}za(b,H[c+16>>2],H[c+20>>2]);e=H[d>>2];a=H[d+4>>2]}g=g+h|0;f=b+12|0;if((a|0)==(e|0)){break e}while(1){a=a-12|0;h=H[a+4>>2];b=b-12|0;H[b>>2]=H[a>>2];H[b+4>>2]=h;H[b+8>>2]=H[a+8>>2];H[a>>2]=0;H[a+4>>2]=0;H[a+8>>2]=0;if((a|0)!=(e|0)){continue}break}H[d+8>>2]=g;a=H[d+4>>2];H[d+4>>2]=f;e=H[d>>2];H[d>>2]=b;if((a|0)==(e|0)){break d}while(1){b=a-12|0;if(F[a-1|0]<0){oa(H[b>>2])}a=b;if((e|0)!=(b|0)){continue}break}break d}sa();v()}wa();v()}H[d+8>>2]=g;H[d+4>>2]=f;H[d>>2]=b}if(e){oa(e)}}b=H[c+4>>2];h:{if(b){while(1){a=b;b=H[b>>2];if(b){continue}break h}}while(1){a=H[c+8>>2];b=H[a>>2]!=(c|0);c=a;if(b){continue}break}}c=a;if((j|0)!=(a|0)){continue}break}}a=0;i:{if((i|0)<0){break i}b=H[d>>2];if((H[d+4>>2]-b|0)/12>>>0<=i>>>0){break i}a=b+N(i,12)|0;a=F[a+11|0]<0?H[a>>2]:a}return a|0}function bd(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0;i=ca-16|0;ca=i;H[i>>2]=b;f=-1;a:{if((b|0)==-1){H[i+4>>2]=-1;break a}f=b+1|0;H[i+4>>2]=(f>>>0)%3|0?f:b-2|0;if((b>>>0)%3|0){f=b-1|0;break a}f=b+2|0}H[i+8>>2]=f;n=(b>>>0)/3|0;b:{c:{d:{while(1){e:{f:{j=H[(l<<2)+i>>2];if((j|0)!=-1){f=H[H[H[a+8>>2]+12>>2]+(j<<2)>>2];if((f|0)!=-1){break f}}f=0;g=H[a+216>>2];if((g|0)==H[a+220>>2]){break e}while(1){g=N(f,144)+g|0;d=H[g+136>>2];c=H[g+140>>2];g:{if(d>>>0>>0){H[d>>2]=j;H[g+136>>2]=d+4;break g}e=d;d=H[g+132>>2];k=e-d|0;e=k>>2;h=e+1|0;if(h>>>0>=1073741824){break d}m=e<<2;c=c-d|0;e=c>>>1|0;h=c>>>0>=2147483644?1073741823:h>>>0>>0?e:h;if(h){if(h>>>0>=1073741824){break c}c=pa(h<<2)}else{c=0}e=m+c|0;H[e>>2]=j;c=va(c,d,k);H[g+132>>2]=c;H[g+136>>2]=e+4;H[g+140>>2]=c+(h<<2);if(!d){break g}oa(d)}f=f+1|0;g=H[a+216>>2];if(f>>>0<(H[a+220>>2]-g|0)/144>>>0){continue}break}break e}if((b|0)==-1|(f>>>0)/3>>>0>>0){break e}f=0;if(H[a+220>>2]==H[a+216>>2]){break e}while(1){h:{if(!Ba(H[a+368>>2]+(f<<4)|0)){break h}g=H[a+216>>2]+N(f,144)|0;d=H[g+136>>2];c=H[g+140>>2];if(d>>>0>>0){H[d>>2]=j;H[g+136>>2]=d+4;break h}e=d;d=H[g+132>>2];k=e-d|0;e=k>>2;h=e+1|0;if(h>>>0>=1073741824){break b}m=e<<2;c=c-d|0;e=c>>>1|0;h=c>>>0>=2147483644?1073741823:h>>>0>>0?e:h;if(h){if(h>>>0>=1073741824){break c}c=pa(h<<2)}else{c=0}e=m+c|0;H[e>>2]=j;c=va(c,d,k);H[g+132>>2]=c;H[g+136>>2]=e+4;H[g+140>>2]=c+(h<<2);if(!d){break h}oa(d)}f=f+1|0;if(f>>>0<(H[a+220>>2]-H[a+216>>2]|0)/144>>>0){continue}break}}l=l+1|0;if((l|0)!=3){continue}break}ca=i+16|0;return 1}sa();v()}wa();v()}sa();v()}function cd(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0;h=ca-16|0;ca=h;H[h>>2]=b;c=-1;a:{if((b|0)==-1){H[h+4>>2]=-1;break a}c=b+1|0;H[h+4>>2]=(c>>>0)%3|0?c:b-2|0;if((b>>>0)%3|0){c=b-1|0;break a}c=b+2|0}H[h+8>>2]=c;b:{c:{while(1){i=H[(k<<2)+h>>2];d:{if(!((i|0)==-1|H[H[H[a+8>>2]+12>>2]+(i<<2)>>2]==-1)){b=0;if(H[a+220>>2]==H[a+216>>2]){break d}while(1){e:{f:{if(!Ba(H[a+368>>2]+(b<<4)|0)){break f}c=H[a+216>>2]+N(b,144)|0;e=H[c+136>>2];d=H[c+140>>2];if(e>>>0>>0){H[e>>2]=i;H[c+136>>2]=e+4;break f}f=e;e=H[c+132>>2];j=f-e|0;f=j>>2;g=f+1|0;if(g>>>0>=1073741824){break e}l=f<<2;d=d-e|0;f=d>>>1|0;g=d>>>0>=2147483644?1073741823:g>>>0>>0?f:g;if(g){if(g>>>0>=1073741824){break b}d=pa(g<<2)}else{d=0}f=l+d|0;H[f>>2]=i;d=va(d,e,j);H[c+132>>2]=d;H[c+136>>2]=f+4;H[c+140>>2]=d+(g<<2);if(!e){break f}oa(e)}b=b+1|0;if(b>>>0<(H[a+220>>2]-H[a+216>>2]|0)/144>>>0){continue}break d}break}sa();v()}b=0;c=H[a+216>>2];if((c|0)==H[a+220>>2]){break d}while(1){c=N(b,144)+c|0;e=H[c+136>>2];d=H[c+140>>2];g:{if(e>>>0>>0){H[e>>2]=i;H[c+136>>2]=e+4;break g}f=e;e=H[c+132>>2];j=f-e|0;f=j>>2;g=f+1|0;if(g>>>0>=1073741824){break c}l=f<<2;d=d-e|0;f=d>>>1|0;g=d>>>0>=2147483644?1073741823:g>>>0>>0?f:g;if(g){if(g>>>0>=1073741824){break b}d=pa(g<<2)}else{d=0}f=l+d|0;H[f>>2]=i;d=va(d,e,j);H[c+132>>2]=d;H[c+136>>2]=f+4;H[c+140>>2]=d+(g<<2);if(!e){break g}oa(e)}b=b+1|0;c=H[a+216>>2];if(b>>>0<(H[a+220>>2]-c|0)/144>>>0){continue}break}}k=k+1|0;if((k|0)!=3){continue}break}ca=h+16|0;return 1}sa();v()}wa();v()}function vg(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0;m=ca-16|0;ca=m;l=H[b+80>>2];e=I[c+24|0];a=N(l,e);a:{b:{c:{d:{b=H[c+28>>2];if(!(!I[c+84|0]|(b|0)!=1&(b|0)!=2)){b=H[c+48>>2];c=H[H[c>>2]>>2];H[m+8>>2]=0;H[m>>2]=0;H[m+4>>2]=0;if(a){if((a|0)<0){break d}f=pa(a);h=qa(f,b+c|0,a)+a|0}a=H[d>>2];if(a){H[d+4>>2]=a;oa(a)}H[d+8>>2]=h;H[d+4>>2]=h;H[d>>2]=f;b=1;break a}if(e){f=pa(e);ra(f,0,e)}e:{i=H[d+4>>2];b=H[d>>2];g=i-b|0;f:{if(g>>>0>>0){k=a-g|0;j=H[d+8>>2];if(k>>>0<=j-i>>>0){n=d,o=ra(i,0,k)+k|0,H[n+4>>2]=o;break f}if((a|0)<0){break e}i=j-b|0;j=i<<1;i=i>>>0>=1073741823?2147483647:a>>>0>>0?j:a;j=pa(i);ra(j+g|0,0,k);g=va(j,b,g);H[d+8>>2]=g+i;H[d+4>>2]=a+g;H[d>>2]=g;if(!b){break f}oa(b);break f}if(a>>>0>=g>>>0){break f}H[d+4>>2]=a+b}if(!l){b=1;break c}if(!e){b=0;a=0;while(1){if(!ic(c,I[c+84|0]?a:H[H[c+68>>2]+(a<<2)>>2],F[c+24|0],f)){break c}a=a+1|0;b=l>>>0<=a>>>0;if((a|0)!=(l|0)){continue}break}break c}i=e&252;g=e&3;b=0;j=e>>>0<4;e=0;while(1){if(!ic(c,I[c+84|0]?e:H[H[c+68>>2]+(e<<2)>>2],F[c+24|0],f)){break c}b=0;a=0;k=0;if(!j){while(1){F[H[d>>2]+h|0]=I[a+f|0];F[(H[d>>2]+h|0)+1|0]=I[(a|1)+f|0];F[(H[d>>2]+h|0)+2|0]=I[(a|2)+f|0];F[(H[d>>2]+h|0)+3|0]=I[(a|3)+f|0];a=a+4|0;h=h+4|0;k=k+4|0;if((i|0)!=(k|0)){continue}break}}if(g){while(1){F[H[d>>2]+h|0]=I[a+f|0];a=a+1|0;h=h+1|0;b=b+1|0;if((g|0)!=(b|0)){continue}break}}e=e+1|0;b=l>>>0<=e>>>0;if((e|0)!=(l|0)){continue}break}break b}sa();v()}sa();v()}if(!f){break a}}oa(f)}ca=m+16|0;return b&1}function ug(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0;m=ca-16|0;ca=m;l=H[b+80>>2];e=I[c+24|0];a=N(l,e);a:{b:{c:{d:{b=H[c+28>>2];if(!(!I[c+84|0]|(b|0)!=1&(b|0)!=2)){b=H[c+48>>2];c=H[H[c>>2]>>2];H[m+8>>2]=0;H[m>>2]=0;H[m+4>>2]=0;if(a){if((a|0)<0){break d}f=pa(a);h=qa(f,b+c|0,a)+a|0}a=H[d>>2];if(a){H[d+4>>2]=a;oa(a)}H[d+8>>2]=h;H[d+4>>2]=h;H[d>>2]=f;b=1;break a}if(e){f=pa(e);ra(f,0,e)}e:{i=H[d+4>>2];b=H[d>>2];g=i-b|0;f:{if(g>>>0>>0){k=a-g|0;j=H[d+8>>2];if(k>>>0<=j-i>>>0){n=d,o=ra(i,0,k)+k|0,H[n+4>>2]=o;break f}if((a|0)<0){break e}i=j-b|0;j=i<<1;i=i>>>0>=1073741823?2147483647:a>>>0>>0?j:a;j=pa(i);ra(j+g|0,0,k);g=va(j,b,g);H[d+8>>2]=g+i;H[d+4>>2]=a+g;H[d>>2]=g;if(!b){break f}oa(b);break f}if(a>>>0>=g>>>0){break f}H[d+4>>2]=a+b}if(!l){b=1;break c}if(!e){b=0;a=0;while(1){if(!hc(c,I[c+84|0]?a:H[H[c+68>>2]+(a<<2)>>2],F[c+24|0],f)){break c}a=a+1|0;b=l>>>0<=a>>>0;if((a|0)!=(l|0)){continue}break}break c}i=e&252;g=e&3;b=0;j=e>>>0<4;e=0;while(1){if(!hc(c,I[c+84|0]?e:H[H[c+68>>2]+(e<<2)>>2],F[c+24|0],f)){break c}b=0;a=0;k=0;if(!j){while(1){F[H[d>>2]+h|0]=I[a+f|0];F[(H[d>>2]+h|0)+1|0]=I[(a|1)+f|0];F[(H[d>>2]+h|0)+2|0]=I[(a|2)+f|0];F[(H[d>>2]+h|0)+3|0]=I[(a|3)+f|0];a=a+4|0;h=h+4|0;k=k+4|0;if((i|0)!=(k|0)){continue}break}}if(g){while(1){F[H[d>>2]+h|0]=I[a+f|0];a=a+1|0;h=h+1|0;b=b+1|0;if((g|0)!=(b|0)){continue}break}}e=e+1|0;b=l>>>0<=e>>>0;if((e|0)!=(l|0)){continue}break}break b}sa();v()}sa();v()}if(!f){break a}}oa(f)}ca=m+16|0;return b&1}function qc(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0;k=H[b+16>>2];h=H[c+4>>2]-k|0;e=H[c>>2]-k|0;H[c>>2]=e;f=h;H[c+4>>2]=f;l=H[b+16>>2];f=f>>31;g=(h^f)-f|0;f=e>>31;m=l>>>0>=g+((f^e)-f|0)>>>0;a:{if(m){f=h;break a}b:{c:{if((e|0)>=0){g=1;j=1;if((h|0)>=0){break b}i=1;g=-1;j=-1;if(e){break c}break b}i=-1;g=-1;j=-1;if((h|0)<=0){break b}}g=(h|0)<=0?-1:1;j=i}n=N(j,l);f=(e<<1)-n|0;i=(N(g,j)|0)>=0;e=N(g,l);f=((i?0-f|0:f)+e|0)/2|0;H[c+4>>2]=f;e=(h<<1)-e|0;e=((i?0-e|0:e)+n|0)/2|0;H[c>>2]=e}d:{e:{f:{g:{h:{i:{j:{if(e){if((e|0)<0){break j}if((f|0)>=0){break i}break f}if(f){break h}j=1;g=0;f=0;i=0;break d}j=1;if((f|0)>0){break g}i=(f|0)>0?3:0;g=f;f=e;break d}g=0-f|0;f=0-e|0;i=2;break e}if((f|0)<=0){break f}}f=0-f|0;g=e;i=3;break e}g=0-e|0;i=1}H[c>>2]=f;H[c+4>>2]=g;j=0}e=H[d>>2]+f|0;h=H[b+16>>2];k:{if((e|0)>(h|0)){e=e-H[b+4>>2]|0;break k}if((0-h|0)<=(e|0)){break k}e=H[b+4>>2]+e|0}c=H[d+4>>2]+g|0;l:{if((h|0)<(c|0)){c=c-H[b+4>>2]|0;break l}if((0-h|0)<=(c|0)){break l}c=H[b+4>>2]+c|0}m:{if(j){b=c;break m}b=c;n:{o:{p:{d=4-i|0;switch((d>>>0<4?d:0-i|0)-1|0){case 2:break n;case 1:break o;case 0:break p;default:break m}}b=0-e|0;e=c;break m}b=0-c|0;e=0-e|0;break m}b=e;e=0-c|0}q:{if(m){c=b;break q}r:{s:{if((e|0)>=0){c=1;f=1;if((b|0)>=0){break r}d=1;c=-1;f=-1;if(e){break s}break r}d=-1;c=-1;f=-1;if((b|0)<=0){break r}}c=(b|0)<=0?-1:1;f=d}d=e<<1;e=N(f,h);d=d-e|0;f=(N(c,f)|0)>=0;g=f?0-d|0:d;d=N(c,h);c=(g+d|0)/2|0;b=(b<<1)-d|0;e=(e+(f?0-b|0:b)|0)/2|0}b=a;H[b>>2]=e+k;H[b+4>>2]=c+k}function Cj(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0;j=ca-32|0;ca=j;H[j+28>>2]=0;a:{b:{if(J[b+38>>1]<=513){c=H[b+20>>2];d=H[b+16>>2];e=d+4|0;c=e>>>0<4?c+1|0:c;h=H[b+12>>2];if(K[b+8>>2]>>0&(h|0)<=(c|0)|(c|0)>(h|0)){break a}d=d+H[b>>2]|0;f=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[b+16>>2]=e;H[b+20>>2]=c;break b}if(!Xa(1,j+28|0,b)){break a}f=H[j+28>>2]}if(!f){break a}c=H[H[a+48>>2]+64>>2];if(H[c+4>>2]-H[c>>2]>>2>>>0>>0){break a}Wa(a+76|0,f);c=j+8|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c:{if(!ta(c,b)){break c}h=1;while(1){d=1<>2]+(i>>>3&536870908)|0;e=e^h;if(e&1){d=H[g>>2]&(d^-1)}else{d=d|H[g>>2]}h=e^1;H[g>>2]=d;i=i+1|0;if((f|0)!=(i|0)){continue}break}c=H[b+8>>2];e=H[b+12>>2];g=e;e=H[b+20>>2];h=e;f=H[b+16>>2];d=f+4|0;e=d>>>0<4?e+1|0:e;i=d;if(d>>>0>c>>>0&(e|0)>=(g|0)|(e|0)>(g|0)){break c}l=H[b>>2];d=l+f|0;k=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[b+16>>2]=i;H[b+20>>2]=e;d=c;c=h;e=f+8|0;c=e>>>0<8?c+1|0:c;if(d>>>0>>0&(c|0)>=(g|0)|(c|0)>(g|0)){break c}d=i+l|0;d=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[b+16>>2]=e;H[b+20>>2]=c;if((d|0)<(k|0)){break c}H[a+16>>2]=d;H[a+12>>2]=k;c=(d>>31)-((k>>31)+(d>>>0>>0)|0)|0;b=d-k|0;if(!c&b>>>0>2147483646|c){break c}m=1;c=b+1|0;H[a+20>>2]=c;b=c>>>1|0;H[a+24>>2]=b;H[a+28>>2]=0-b;if(c&1){break c}H[a+24>>2]=b-1}}ca=j+32|0;return m|0}function lj(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0;j=ca-32|0;ca=j;H[j+28>>2]=0;a:{b:{if(J[b+38>>1]<=513){c=H[b+20>>2];d=H[b+16>>2];e=d+4|0;c=e>>>0<4?c+1|0:c;h=H[b+12>>2];if(K[b+8>>2]>>0&(h|0)<=(c|0)|(c|0)>(h|0)){break a}d=d+H[b>>2]|0;f=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[b+16>>2]=e;H[b+20>>2]=c;break b}if(!Xa(1,j+28|0,b)){break a}f=H[j+28>>2]}if(!f){break a}c=H[a+48>>2];if(H[c+4>>2]-H[c>>2]>>2>>>0>>0){break a}Wa(a+76|0,f);c=j+8|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;c:{if(!ta(c,b)){break c}h=1;while(1){d=1<>2]+(i>>>3&536870908)|0;e=e^h;if(e&1){d=H[g>>2]&(d^-1)}else{d=d|H[g>>2]}h=e^1;H[g>>2]=d;i=i+1|0;if((f|0)!=(i|0)){continue}break}c=H[b+8>>2];e=H[b+12>>2];g=e;e=H[b+20>>2];h=e;f=H[b+16>>2];d=f+4|0;e=d>>>0<4?e+1|0:e;i=d;if(d>>>0>c>>>0&(e|0)>=(g|0)|(e|0)>(g|0)){break c}l=H[b>>2];d=l+f|0;k=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[b+16>>2]=i;H[b+20>>2]=e;d=c;c=h;e=f+8|0;c=e>>>0<8?c+1|0:c;if(d>>>0>>0&(c|0)>=(g|0)|(c|0)>(g|0)){break c}d=i+l|0;d=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[b+16>>2]=e;H[b+20>>2]=c;if((d|0)<(k|0)){break c}H[a+16>>2]=d;H[a+12>>2]=k;c=(d>>31)-((k>>31)+(d>>>0>>0)|0)|0;b=d-k|0;if(!c&b>>>0>2147483646|c){break c}m=1;c=b+1|0;H[a+20>>2]=c;b=c>>>1|0;H[a+24>>2]=b;H[a+28>>2]=0-b;if(c&1){break c}H[a+24>>2]=b-1}}ca=j+32|0;return m|0}function cj(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;var g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0;H[a+8>>2]=e;m=a+32|0;h=H[m>>2];g=H[a+36>>2]-h>>2;a:{if(g>>>0>>0){ya(m,e-g|0);f=H[a+8>>2];break a}f=e;if(f>>>0>=g>>>0){break a}H[a+36>>2]=h+(e<<2);f=e}g=e>>>0>1073741823?-1:e<<2;n=ra(pa(g),0,g);b:{if((f|0)<=0){break b}h=H[a+32>>2];while(1){f=i<<2;g=H[f+n>>2];j=H[a+16>>2];c:{if((g|0)>(j|0)){H[f+h>>2]=j;break c}f=f+h|0;j=H[a+12>>2];if((j|0)>(g|0)){H[f>>2]=j;break c}H[f>>2]=g}f=H[a+8>>2];i=i+1|0;if((f|0)>(i|0)){continue}break}if((f|0)<=0){break b}i=0;while(1){g=i<<2;f=g+c|0;g=H[b+g>>2]+H[g+h>>2]|0;H[f>>2]=g;d:{if((g|0)>H[a+16>>2]){g=g-H[a+20>>2]|0}else{if((g|0)>=H[a+12>>2]){break d}g=g+H[a+20>>2]|0}H[f>>2]=g}f=H[a+8>>2];i=i+1|0;if((f|0)>(i|0)){continue}break}}if(!((d|0)<=(e|0)|(f|0)<=0)){p=0-e<<2;g=e;while(1){e:{if((f|0)<=0){break e}l=g<<2;o=l+c|0;q=o+p|0;j=H[m>>2];i=0;while(1){f=i<<2;h=H[f+q>>2];k=H[a+16>>2];f:{if((h|0)>(k|0)){H[f+j>>2]=k;break f}f=f+j|0;k=H[a+12>>2];if((k|0)>(h|0)){H[f>>2]=k;break f}H[f>>2]=h}f=H[a+8>>2];i=i+1|0;if((f|0)>(i|0)){continue}break}i=0;if((f|0)<=0){break e}l=b+l|0;while(1){h=i<<2;f=h+o|0;h=H[h+l>>2]+H[h+j>>2]|0;H[f>>2]=h;g:{if((h|0)>H[a+16>>2]){h=h-H[a+20>>2]|0}else{if((h|0)>=H[a+12>>2]){break g}h=h+H[a+20>>2]|0}H[f>>2]=h}f=H[a+8>>2];i=i+1|0;if((f|0)>(i|0)){continue}break}}g=e+g|0;if((g|0)<(d|0)){continue}break}}oa(n);return 1}function De(a,b){var c=0,d=0,e=0,f=0,g=0;d=-1;f=-1;a:{if((b|0)==-1){break a}c=b+1|0;d=(c>>>0)%3|0?c:b-2|0;f=b-1|0;if((b>>>0)%3|0){break a}f=b+2|0}b:{c:{d:{e:{f:{g:{e=H[a+184>>2];switch(e|0){case 7:break d;case 3:break e;case 5:break f;case 0:case 1:break g;default:break b}}g=H[a+148>>2];c=-1;e=1;d=((d|0)!=-1?H[H[g>>2]+(d<<2)>>2]:c)<<2;c=H[a+156>>2];d=d+c|0;H[d>>2]=H[d>>2]+1;c=(((f|0)==-1?-1:H[H[g>>2]+(f<<2)>>2])<<2)+c|0;break c}g=H[a+148>>2];c=H[a+156>>2];e=c+(((b|0)==-1?-1:H[H[g>>2]+(b<<2)>>2])<<2)|0;H[e>>2]=H[e>>2]+1;d=(((d|0)==-1?-1:H[H[g>>2]+(d<<2)>>2])<<2)+c|0;H[d>>2]=H[d>>2]+1;e=2;c=(((f|0)==-1?-1:H[H[g>>2]+(f<<2)>>2])<<2)+c|0;break c}g=H[a+148>>2];c=H[a+156>>2];e=c+(((b|0)==-1?-1:H[H[g>>2]+(b<<2)>>2])<<2)|0;H[e>>2]=H[e>>2]+1;d=(((d|0)==-1?-1:H[H[g>>2]+(d<<2)>>2])<<2)+c|0;H[d>>2]=H[d>>2]+2;e=1;c=(((f|0)==-1?-1:H[H[g>>2]+(f<<2)>>2])<<2)+c|0;break c}g=H[a+148>>2];c=H[a+156>>2];e=c+(((b|0)==-1?-1:H[H[g>>2]+(b<<2)>>2])<<2)|0;H[e>>2]=H[e>>2]+2;d=(((d|0)==-1?-1:H[H[g>>2]+(d<<2)>>2])<<2)+c|0;H[d>>2]=H[d>>2]+2;e=2;c=(((f|0)==-1?-1:H[H[g>>2]+(f<<2)>>2])<<2)+c|0}H[c>>2]=H[c>>2]+e;e=H[a+184>>2]}h:{switch(e|0){case 0:case 5:f=H[a+156>>2];c=-1;i:{if((b|0)==-1){break i}d=b+1|0;b=(d>>>0)%3|0?d:b-2|0;c=-1;if((b|0)==-1){break i}c=H[H[H[a+148>>2]>>2]+(b<<2)>>2]}if(H[f+(c<<2)>>2]<=5){H[a+188>>2]=5;return}H[a+188>>2]=0;return;default:break h}}H[a+188>>2]=-1}function xg(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0;j=H[b+80>>2];b=I[c+24|0];g=N(j,b);a:{if(!b){break a}h=b<<2;f=pa(h);a=f;k=b&7;if(k){while(1){H[a>>2]=-1073741824;a=a+4|0;e=e+1|0;if((k|0)!=(e|0)){continue}break}}if((b-1&1073741823)>>>0<7){break a}e=f+h|0;while(1){H[a+24>>2]=-1073741824;H[a+28>>2]=-1073741824;H[a+16>>2]=-1073741824;H[a+20>>2]=-1073741824;H[a+8>>2]=-1073741824;H[a+12>>2]=-1073741824;H[a>>2]=-1073741824;H[a+4>>2]=-1073741824;a=a+32|0;if((e|0)!=(a|0)){continue}break}}e=H[d>>2];a=H[d+4>>2]-e>>2;b:{if(a>>>0>>0){ya(d,g-a|0);break b}if(a>>>0<=g>>>0){break b}H[d+4>>2]=e+(g<<2)}c:{d:{e:{if(!j){i=1;break e}if(!b){a=0;while(1){if(!Va(c,I[c+84|0]?a:H[H[c+68>>2]+(a<<2)>>2],F[c+24|0],f)){break e}a=a+1|0;i=j>>>0<=a>>>0;if((a|0)!=(j|0)){continue}break}break e}n=b&252;k=b&3;o=b>>>0<4;e=0;b=0;while(1){if(!Va(c,I[c+84|0]?b:H[H[c+68>>2]+(b<<2)>>2],F[c+24|0],f)){break e}m=H[d>>2];i=0;a=0;l=0;if(!o){while(1){g=(e<<2)+m|0;h=a<<2;L[g>>2]=L[h+f>>2];L[g+4>>2]=L[(h|4)+f>>2];L[g+8>>2]=L[(h|8)+f>>2];L[g+12>>2]=L[(h|12)+f>>2];a=a+4|0;e=e+4|0;l=l+4|0;if((n|0)!=(l|0)){continue}break}}if(k){while(1){L[(e<<2)+m>>2]=L[(a<<2)+f>>2];a=a+1|0;e=e+1|0;i=i+1|0;if((k|0)!=(i|0)){continue}break}}b=b+1|0;i=j>>>0<=b>>>0;if((b|0)!=(j|0)){continue}break}break d}if(!f){break c}}oa(f)}return i|0}function mf(a){a=a|0;var b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0;e=ca-16|0;ca=e;h=1;i=ea[H[H[a>>2]+24>>2]](a)|0;a:{if((i|0)<=0){break a}l=a+48|0;h=0;while(1){b:{c:{if(!H[(ea[H[H[a>>2]+28>>2]](a)|0)+40>>2]){break c}j=f<<2;g=H[j+H[a+36>>2]>>2];b=H[g+8>>2];k=rb(g);if(!k){break c}g=H[(ea[H[H[a>>2]+28>>2]](a)|0)+40>>2];H[e+12>>2]=H[b+56>>2];b=pa(32);H[e>>2]=b;H[e+4>>2]=24;H[e+8>>2]=-2147483616;c=I[1206]|I[1207]<<8|(I[1208]<<16|I[1209]<<24);d=I[1202]|I[1203]<<8|(I[1204]<<16|I[1205]<<24);F[b+16|0]=d;F[b+17|0]=d>>>8;F[b+18|0]=d>>>16;F[b+19|0]=d>>>24;F[b+20|0]=c;F[b+21|0]=c>>>8;F[b+22|0]=c>>>16;F[b+23|0]=c>>>24;c=I[1198]|I[1199]<<8|(I[1200]<<16|I[1201]<<24);d=I[1194]|I[1195]<<8|(I[1196]<<16|I[1197]<<24);F[b+8|0]=d;F[b+9|0]=d>>>8;F[b+10|0]=d>>>16;F[b+11|0]=d>>>24;F[b+12|0]=c;F[b+13|0]=c>>>8;F[b+14|0]=c>>>16;F[b+15|0]=c>>>24;c=I[1190]|I[1191]<<8|(I[1192]<<16|I[1193]<<24);d=I[1186]|I[1187]<<8|(I[1188]<<16|I[1189]<<24);F[b|0]=d;F[b+1|0]=d>>>8;F[b+2|0]=d>>>16;F[b+3|0]=d>>>24;F[b+4|0]=c;F[b+5|0]=c>>>8;F[b+6|0]=c>>>16;F[b+7|0]=c>>>24;F[b+24|0]=0;b=sd(g,e+12|0,e);if(F[e+11|0]<0){oa(H[e>>2])}if(!b){break c}oe(H[H[H[a+36>>2]+j>>2]+8>>2],k);break b}b=H[H[a+36>>2]+(f<<2)>>2];if(!(ea[H[H[b>>2]+24>>2]](b,l)|0)){break a}}f=f+1|0;h=(i|0)<=(f|0);if((f|0)!=(i|0)){continue}break}}ca=e+16|0;return h|0}function Ye(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0;k=ca-16|0;ca=k;c=H[b+20>>2];d=H[b+16>>2];e=d+4|0;c=e>>>0<4?c+1|0:c;g=H[b+12>>2];a:{if(K[b+8>>2]>>0&(g|0)<=(c|0)|(c|0)>(g|0)){break a}d=d+H[b>>2]|0;h=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[b+16>>2]=e;H[b+20>>2]=c;if((h|0)<0){break a}Wa(a+76|0,h);c=k;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;b:{if(!ta(c,b)){break b}if(h){g=1;while(1){d=1<>2]+(i>>>3&536870908)|0;e=e^g;if(e&1){d=H[f>>2]&(d^-1)}else{d=d|H[f>>2]}g=e^1;H[f>>2]=d;i=i+1|0;if((h|0)!=(i|0)){continue}break}}i=0;c=H[b+8>>2];e=H[b+12>>2];f=e;e=H[b+20>>2];g=e;l=H[b+16>>2];d=l+4|0;e=d>>>0<4?e+1|0:e;h=d;if(d>>>0>c>>>0&(e|0)>=(f|0)|(e|0)>(f|0)){break b}m=H[b>>2];d=m+l|0;j=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[b+16>>2]=h;H[b+20>>2]=e;d=c;c=g;e=l+8|0;c=e>>>0<8?c+1|0:c;if(d>>>0>>0&(c|0)>=(f|0)|(c|0)>(f|0)){break b}d=h+m|0;d=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[b+16>>2]=e;H[b+20>>2]=c;if((d|0)<(j|0)){break b}H[a+16>>2]=d;H[a+12>>2]=j;c=(d>>31)-((j>>31)+(d>>>0>>0)|0)|0;b=d-j|0;if(!c&b>>>0>2147483646|c){break b}i=1;c=b+1|0;H[a+20>>2]=c;b=c>>>1|0;H[a+24>>2]=b;H[a+28>>2]=0-b;if(c&1){break b}H[a+24>>2]=b-1}}ca=k+16|0;return i|0}function rg(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0;a=0;k=ca-16|0;ca=k;j=H[b+80>>2];e=I[c+24|0];b=N(j,e);a:{b:{c:{d:{f=H[c+28>>2];if(!(!I[c+84|0]|(f|0)!=5&(f|0)!=6)){e=H[c+48>>2];c=H[H[c>>2]>>2];H[k+8>>2]=0;H[k>>2]=0;H[k+4>>2]=0;if(b){if((b|0)<0){break d}b=b<<2;a=pa(b);g=qa(a,c+e|0,b)+b|0}b=H[d>>2];if(b){H[d+4>>2]=b;oa(b)}H[d+8>>2]=g;H[d+4>>2]=g;H[d>>2]=a;h=1;break a}if(e){f=e<<2;a=pa(f);ra(a,0,f)}i=H[d>>2];f=H[d+4>>2]-i>>2;e:{if(f>>>0>>0){ya(d,b-f|0);break e}if(b>>>0>=f>>>0){break e}H[d+4>>2]=i+(b<<2)}if(!j){h=1;break c}if(!e){b=0;while(1){if(!dc(c,I[c+84|0]?b:H[H[c+68>>2]+(b<<2)>>2],F[c+24|0],a)){break c}b=b+1|0;h=j>>>0<=b>>>0;if((b|0)!=(j|0)){continue}break}break c}o=e&252;m=e&3;p=e>>>0<4;e=0;while(1){if(!dc(c,I[c+84|0]?e:H[H[c+68>>2]+(e<<2)>>2],F[c+24|0],a)){break c}n=H[d>>2];l=0;b=0;h=0;if(!p){while(1){f=(g<<2)+n|0;i=b<<2;H[f>>2]=H[i+a>>2];H[f+4>>2]=H[(i|4)+a>>2];H[f+8>>2]=H[(i|8)+a>>2];H[f+12>>2]=H[(i|12)+a>>2];b=b+4|0;g=g+4|0;h=h+4|0;if((o|0)!=(h|0)){continue}break}}if(m){while(1){H[(g<<2)+n>>2]=H[(b<<2)+a>>2];b=b+1|0;g=g+1|0;l=l+1|0;if((l|0)!=(m|0)){continue}break}}e=e+1|0;h=j>>>0<=e>>>0;if((e|0)!=(j|0)){continue}break}break b}sa();v()}if(!a){break a}}oa(a)}ca=k+16|0;return h|0}function ge(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0;a=0;k=ca-16|0;ca=k;j=H[b+80>>2];e=I[c+24|0];b=N(j,e);a:{b:{c:{d:{f=H[c+28>>2];if(!(!I[c+84|0]|(f|0)!=5&(f|0)!=6)){e=H[c+48>>2];c=H[H[c>>2]>>2];H[k+8>>2]=0;H[k>>2]=0;H[k+4>>2]=0;if(b){if((b|0)<0){break d}b=b<<2;a=pa(b);g=qa(a,c+e|0,b)+b|0}b=H[d>>2];if(b){H[d+4>>2]=b;oa(b)}H[d+8>>2]=g;H[d+4>>2]=g;H[d>>2]=a;h=1;break a}if(e){f=e<<2;a=pa(f);ra(a,0,f)}i=H[d>>2];f=H[d+4>>2]-i>>2;e:{if(f>>>0>>0){ya(d,b-f|0);break e}if(b>>>0>=f>>>0){break e}H[d+4>>2]=i+(b<<2)}if(!j){h=1;break c}if(!e){b=0;while(1){if(!ec(c,I[c+84|0]?b:H[H[c+68>>2]+(b<<2)>>2],F[c+24|0],a)){break c}b=b+1|0;h=j>>>0<=b>>>0;if((b|0)!=(j|0)){continue}break}break c}o=e&252;m=e&3;p=e>>>0<4;e=0;while(1){if(!ec(c,I[c+84|0]?e:H[H[c+68>>2]+(e<<2)>>2],F[c+24|0],a)){break c}n=H[d>>2];l=0;b=0;h=0;if(!p){while(1){f=(g<<2)+n|0;i=b<<2;H[f>>2]=H[i+a>>2];H[f+4>>2]=H[(i|4)+a>>2];H[f+8>>2]=H[(i|8)+a>>2];H[f+12>>2]=H[(i|12)+a>>2];b=b+4|0;g=g+4|0;h=h+4|0;if((o|0)!=(h|0)){continue}break}}if(m){while(1){H[(g<<2)+n>>2]=H[(b<<2)+a>>2];b=b+1|0;g=g+1|0;l=l+1|0;if((l|0)!=(m|0)){continue}break}}e=e+1|0;h=j>>>0<=e>>>0;if((e|0)!=(j|0)){continue}break}break b}sa();v()}if(!a){break a}}oa(a)}ca=k+16|0;return h|0}function tg(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0;a=0;k=ca-16|0;ca=k;j=H[b+80>>2];e=I[c+24|0];b=N(j,e);a:{b:{c:{d:{f=H[c+28>>2];if(!(!I[c+84|0]|(f|0)!=3&(f|0)!=4)){e=H[c+48>>2];c=H[H[c>>2]>>2];H[k+8>>2]=0;H[k>>2]=0;H[k+4>>2]=0;if(b){if((b|0)<0){break d}b=b<<1;a=pa(b);g=qa(a,c+e|0,b)+b|0}b=H[d>>2];if(b){H[d+4>>2]=b;oa(b)}H[d+8>>2]=g;H[d+4>>2]=g;H[d>>2]=a;h=1;break a}if(e){f=e<<1;a=pa(f);ra(a,0,f)}i=H[d>>2];f=H[d+4>>2]-i>>1;e:{if(f>>>0>>0){qe(d,b-f|0);break e}if(b>>>0>=f>>>0){break e}H[d+4>>2]=i+(b<<1)}if(!j){h=1;break c}if(!e){b=0;while(1){if(!gc(c,I[c+84|0]?b:H[H[c+68>>2]+(b<<2)>>2],F[c+24|0],a)){break c}b=b+1|0;h=j>>>0<=b>>>0;if((b|0)!=(j|0)){continue}break}break c}o=e&252;m=e&3;p=e>>>0<4;e=0;while(1){if(!gc(c,I[c+84|0]?e:H[H[c+68>>2]+(e<<2)>>2],F[c+24|0],a)){break c}n=H[d>>2];l=0;b=0;h=0;if(!p){while(1){f=(g<<1)+n|0;i=b<<1;G[f>>1]=J[i+a>>1];G[f+2>>1]=J[(i|2)+a>>1];G[f+4>>1]=J[(i|4)+a>>1];G[f+6>>1]=J[(i|6)+a>>1];b=b+4|0;g=g+4|0;h=h+4|0;if((o|0)!=(h|0)){continue}break}}if(m){while(1){G[(g<<1)+n>>1]=J[(b<<1)+a>>1];b=b+1|0;g=g+1|0;l=l+1|0;if((l|0)!=(m|0)){continue}break}}e=e+1|0;h=j>>>0<=e>>>0;if((e|0)!=(j|0)){continue}break}break b}sa();v()}if(!a){break a}}oa(a)}ca=k+16|0;return h|0}function sg(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0;a=0;k=ca-16|0;ca=k;j=H[b+80>>2];e=I[c+24|0];b=N(j,e);a:{b:{c:{d:{f=H[c+28>>2];if(!(!I[c+84|0]|(f|0)!=3&(f|0)!=4)){e=H[c+48>>2];c=H[H[c>>2]>>2];H[k+8>>2]=0;H[k>>2]=0;H[k+4>>2]=0;if(b){if((b|0)<0){break d}b=b<<1;a=pa(b);g=qa(a,c+e|0,b)+b|0}b=H[d>>2];if(b){H[d+4>>2]=b;oa(b)}H[d+8>>2]=g;H[d+4>>2]=g;H[d>>2]=a;h=1;break a}if(e){f=e<<1;a=pa(f);ra(a,0,f)}i=H[d>>2];f=H[d+4>>2]-i>>1;e:{if(f>>>0>>0){qe(d,b-f|0);break e}if(b>>>0>=f>>>0){break e}H[d+4>>2]=i+(b<<1)}if(!j){h=1;break c}if(!e){b=0;while(1){if(!fc(c,I[c+84|0]?b:H[H[c+68>>2]+(b<<2)>>2],F[c+24|0],a)){break c}b=b+1|0;h=j>>>0<=b>>>0;if((b|0)!=(j|0)){continue}break}break c}o=e&252;m=e&3;p=e>>>0<4;e=0;while(1){if(!fc(c,I[c+84|0]?e:H[H[c+68>>2]+(e<<2)>>2],F[c+24|0],a)){break c}n=H[d>>2];l=0;b=0;h=0;if(!p){while(1){f=(g<<1)+n|0;i=b<<1;G[f>>1]=J[i+a>>1];G[f+2>>1]=J[(i|2)+a>>1];G[f+4>>1]=J[(i|4)+a>>1];G[f+6>>1]=J[(i|6)+a>>1];b=b+4|0;g=g+4|0;h=h+4|0;if((o|0)!=(h|0)){continue}break}}if(m){while(1){G[(g<<1)+n>>1]=J[(b<<1)+a>>1];b=b+1|0;g=g+1|0;l=l+1|0;if((l|0)!=(m|0)){continue}break}}e=e+1|0;h=j>>>0<=e>>>0;if((e|0)!=(j|0)){continue}break}break b}sa();v()}if(!a){break a}}oa(a)}ca=k+16|0;return h|0}function Ce(a,b){var c=0,d=0,e=0,f=0,g=0;f=-1;d=-1;a:{if((b|0)==-1){break a}d=b+1|0;f=(d>>>0)%3|0?d:b-2|0;d=b-1|0;if((b>>>0)%3|0){break a}d=b+2|0}b:{c:{d:{switch(H[a+168>>2]){case 0:case 1:e=H[a+148>>2];c=1;b=H[a+156>>2];g=b+(((f|0)==-1?-1:H[H[e>>2]+(f<<2)>>2])<<2)|0;H[g>>2]=H[g>>2]+1;b=(((d|0)==-1?-1:H[H[e>>2]+(d<<2)>>2])<<2)+b|0;break c;case 5:e=H[a+148>>2];c=-1;c=((b|0)!=-1?H[H[e>>2]+(b<<2)>>2]:c)<<2;b=H[a+156>>2];c=c+b|0;H[c>>2]=H[c>>2]+1;c=(((f|0)==-1?-1:H[H[e>>2]+(f<<2)>>2])<<2)+b|0;H[c>>2]=H[c>>2]+1;c=2;b=(((d|0)==-1?-1:H[H[e>>2]+(d<<2)>>2])<<2)+b|0;break c;case 3:e=H[a+148>>2];c=-1;c=((b|0)!=-1?H[H[e>>2]+(b<<2)>>2]:c)<<2;b=H[a+156>>2];c=c+b|0;H[c>>2]=H[c>>2]+1;c=(((f|0)==-1?-1:H[H[e>>2]+(f<<2)>>2])<<2)+b|0;H[c>>2]=H[c>>2]+2;c=1;b=(((d|0)==-1?-1:H[H[e>>2]+(d<<2)>>2])<<2)+b|0;break c;case 7:break d;default:break b}}e=H[a+148>>2];c=-1;c=((b|0)!=-1?H[H[e>>2]+(b<<2)>>2]:c)<<2;b=H[a+156>>2];c=c+b|0;H[c>>2]=H[c>>2]+2;c=(((f|0)==-1?-1:H[H[e>>2]+(f<<2)>>2])<<2)+b|0;H[c>>2]=H[c>>2]+2;c=2;b=(((d|0)==-1?-1:H[H[e>>2]+(d<<2)>>2])<<2)+b|0}H[b>>2]=H[b>>2]+c}c=a;b=H[H[a+156>>2]+(((f|0)==-1?-1:H[H[H[a+148>>2]>>2]+(f<<2)>>2])<<2)>>2];d=H[a+180>>2];a=H[a+176>>2];H[c+172>>2]=(a|0)<=(b|0)?((b|0)<(d|0)?b:d)-a|0:0}function Ac(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0;H[a+16>>2]=0;H[a+20>>2]=0;H[a+8>>2]=0;H[a>>2]=0;H[a+4>>2]=0;H[a+24>>2]=0;f=H[b+4>>2];g=H[b>>2];e=f-g|0;c=(e|0)/20|0;a:{if((f|0)==(g|0)){break a}b:{if(c>>>0<214748365){f=pa(e);H[a+20>>2]=f;H[a+16>>2]=f;H[a+24>>2]=f+N(c,20);c=H[b>>2];g=H[b+4>>2];if((c|0)==(g|0)){break a}b=f;while(1){e=H[c+4>>2];H[b>>2]=H[c>>2];H[b+4>>2]=e;H[b+16>>2]=H[c+16>>2];e=H[c+12>>2];H[b+8>>2]=H[c+8>>2];H[b+12>>2]=e;b=b+20|0;c=c+20|0;if((g|0)!=(c|0)){continue}break}g=0;H[a+28>>2]=0;H[a+20>>2]=b;if((b|0)!=(f|0)){b=(b-f|0)/20|0;e=b>>>0<=1?1:b;h=e&3;b=0;c=0;if(e-1>>>0>=3){i=e&-4;e=0;while(1){d=f+N(b,20)|0;d=N(H[d+16>>2],H[d+12>>2]);c=c>>>0>d>>>0?c:d;d=f+N(b|1,20)|0;d=N(H[d+16>>2],H[d+12>>2]);c=c>>>0>d>>>0?c:d;d=f+N(b|2,20)|0;d=N(H[d+16>>2],H[d+12>>2]);c=c>>>0>d>>>0?c:d;d=f+N(b|3,20)|0;d=N(H[d+16>>2],H[d+12>>2]);c=c>>>0>d>>>0?c:d;b=b+4|0;e=e+4|0;if((i|0)!=(e|0)){continue}break}}if(h){while(1){e=f+N(b,20)|0;e=N(H[e+16>>2],H[e+12>>2]);c=c>>>0>e>>>0?c:e;b=b+1|0;g=g+1|0;if((h|0)!=(g|0)){continue}break}}if(!c){H[a+12>>2]=0;return a}if((c|0)<0){break b}g=pa(c);b=ra(g,0,c);f=b+c|0;H[a+8>>2]=f;H[a+4>>2]=f;H[a>>2]=b}H[a+12>>2]=g;return a}sa();v()}sa();v()}H[a+28>>2]=0;H[a+12>>2]=0;return a}function Dh(a){a=a|0;var b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0;a:{b=H[a+32>>2];f=H[b+8>>2];h=H[b+12>>2];g=H[b+20>>2];c=H[b+16>>2];e=0;b:{if((h|0)<=(g|0)&c>>>0>=f>>>0|(g|0)>(h|0)){break b}f=I[H[b>>2]+c|0];e=b;b=g;c=c+1|0;b=c?b:b+1|0;H[e+16>>2]=c;H[e+20>>2]=b;c:{if(!f){break c}while(1){if(ea[H[H[a>>2]+16>>2]](a,d)|0){d=d+1|0;if((f|0)!=(d|0)){continue}break c}break}return 0}d=H[a+8>>2];b=H[a+12>>2];if((d|0)!=(b|0)){while(1){c=H[d>>2];if(!(ea[H[H[c>>2]+8>>2]](c,a,H[a+4>>2])|0)){break a}d=d+4|0;if((b|0)!=(d|0)){continue}break}}d:{if(!f){break d}d=0;while(1){b=H[H[a+8>>2]+(d<<2)>>2];if(!(ea[H[H[b>>2]+12>>2]](b,H[a+32>>2])|0)){break a}d=d+1|0;if((f|0)!=(d|0)){continue}break}if(!f){break d}i=a+20|0;b=0;while(1){d=0;j=b<<2;c=H[j+H[a+8>>2]>>2];k=ea[H[H[c>>2]+24>>2]](c)|0;if((k|0)>0){while(1){c=H[H[a+8>>2]+j>>2];c=ea[H[H[c>>2]+20>>2]](c,d)|0;e=H[a+20>>2];g=H[a+24>>2]-e>>2;e:{if(c>>>0>>0){break e}h=c+1|0;if(h>>>0>g>>>0){ya(i,h-g|0);e=H[i>>2];break e}if(g>>>0<=h>>>0){break e}H[a+24>>2]=(h<<2)+e}H[(c<<2)+e>>2]=b;d=d+1|0;if((k|0)!=(d|0)){continue}break}}b=b+1|0;if((f|0)!=(b|0)){continue}break}}e=0;if(!(ea[H[H[a>>2]+28>>2]](a)|0)){break b}e=ea[H[H[a>>2]+32>>2]](a)|0}return e|0}return 0}function ta(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0;i=ca-16|0;ca=i;f=H[b+20>>2];d=H[b+12>>2];c=H[b+16>>2];a:{if((f|0)>=(d|0)&c>>>0>=K[b+8>>2]|(d|0)<(f|0)){break a}F[a+12|0]=I[c+H[b>>2]|0];c=H[b+20>>2];g=c;f=H[b+16>>2];e=f+1|0;c=e?c:c+1|0;H[b+16>>2]=e;H[b+20>>2]=c;b:{if(J[b+38>>1]<=513){d=H[b+8>>2];c=H[b+12>>2];h=c;c=g;f=f+5|0;c=f>>>0<5?c+1|0:c;if(d>>>0>>0&(c|0)>=(h|0)|(c|0)>(h|0)){break a}e=e+H[b>>2]|0;e=I[e|0]|I[e+1|0]<<8|(I[e+2|0]<<16|I[e+3|0]<<24);H[b+16>>2]=f;H[b+20>>2]=c;break b}if(!Qe(1,i+12|0,b)){break a}f=H[b+16>>2];c=H[b+20>>2];d=H[b+8>>2];h=H[b+12>>2];e=H[i+12>>2]}g=d-f|0;d=h-(c+(d>>>0>>0)|0)|0;if((d|0)<=0&e>>>0>g>>>0|(d|0)<0|(e|0)<=0){break a}j=H[b>>2]+f|0;H[a>>2]=j;c:{d:{h=e-1|0;g=h+j|0;d=I[g|0];e:{if(d>>>0<=63){H[a+4>>2]=h;g=I[g|0]&63;break e}f:{switch((d>>>6|0)-1|0){case 1:break d;case 0:break f;default:break a}}if(e>>>0<2){break a}d=e-2|0;H[a+4>>2]=d;d=d+j|0;g=I[d+1|0]<<8&16128|I[d|0]}H[a+8>>2]=g+4096;break c}if(e>>>0<3){break a}d=e-3|0;H[a+4>>2]=d;g=a;a=d+j|0;a=I[a+1|0]<<8|I[a+2|0]<<16&4128768|I[a|0];H[g+8>>2]=a+4096;if(a>>>0>1044479){break a}}a=e+f|0;c=a>>>0>>0?c+1|0:c;H[b+16>>2]=a;H[b+20>>2]=c;k=1}ca=i+16|0;return k}function Wf(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0,h=0,i=0,j=0;Xd(a,b,c);c=H[a+84>>2];d=H[a+88>>2]-c>>2;a:{if((d|0)>(b|0)){break a}b=b+1|0;if(b>>>0>d>>>0){b:{d=b-d|0;e=H[a+92>>2];c=H[a+88>>2];if(d>>>0<=e-c>>2>>>0){c:{if(!d){break c}b=c;e=d&7;if(e){while(1){H[b>>2]=1;b=b+4|0;f=f+1|0;if((e|0)!=(f|0)){continue}break}}c=(d<<2)+c|0;if((d-1&1073741823)>>>0<7){break c}while(1){H[b+24>>2]=1;H[b+28>>2]=1;H[b+16>>2]=1;H[b+20>>2]=1;H[b+8>>2]=1;H[b+12>>2]=1;H[b>>2]=1;H[b+4>>2]=1;b=b+32|0;if((c|0)!=(b|0)){continue}break}}H[a+88>>2]=c;break b}d:{b=c;c=H[a+84>>2];i=b-c|0;g=i>>2;b=g+d|0;if(b>>>0<1073741824){e=e-c|0;h=e>>>1|0;e=e>>>0>=2147483644?1073741823:b>>>0>>0?h:b;if(e){if(e>>>0>=1073741824){break d}j=pa(e<<2)}g=(g<<2)+j|0;b=g;h=d&7;if(h){while(1){H[b>>2]=1;b=b+4|0;f=f+1|0;if((h|0)!=(f|0)){continue}break}}f=g+(d<<2)|0;if((d-1&1073741823)>>>0>=7){while(1){H[b+24>>2]=1;H[b+28>>2]=1;H[b+16>>2]=1;H[b+20>>2]=1;H[b+8>>2]=1;H[b+12>>2]=1;H[b>>2]=1;H[b+4>>2]=1;b=b+32|0;if((f|0)!=(b|0)){continue}break}}b=va(j,c,i);H[a+88>>2]=f;H[a+84>>2]=b;H[a+92>>2]=b+(e<<2);if(c){oa(c)}break b}sa();v()}wa();v()}return}if(b>>>0>=d>>>0){break a}H[a+88>>2]=c+(b<<2)}}function qb(a,b,c){var d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0;d=H[a+8>>2];e=H[a+4>>2];if(d-e>>2>>>0>=b>>>0){a:{if(!b){break a}d=e;g=b&7;if(g){while(1){H[d>>2]=H[c>>2];d=d+4|0;f=f+1|0;if((g|0)!=(f|0)){continue}break}}e=(b<<2)+e|0;if((b-1&1073741823)>>>0<7){break a}while(1){H[d>>2]=H[c>>2];H[d+4>>2]=H[c>>2];H[d+8>>2]=H[c>>2];H[d+12>>2]=H[c>>2];H[d+16>>2]=H[c>>2];H[d+20>>2]=H[c>>2];H[d+24>>2]=H[c>>2];H[d+28>>2]=H[c>>2];d=d+32|0;if((e|0)!=(d|0)){continue}break}}H[a+4>>2]=e;return}b:{i=H[a>>2];f=e-i>>2;h=f+b|0;if(h>>>0<1073741824){j=d-i|0;d=j>>>1|0;h=j>>>0>=2147483644?1073741823:d>>>0>h>>>0?d:h;if(h){if(h>>>0>=1073741824){break b}k=pa(h<<2)}f=(f<<2)+k|0;d=f;j=b&7;if(j){while(1){H[d>>2]=H[c>>2];d=d+4|0;g=g+1|0;if((j|0)!=(g|0)){continue}break}}g=(b<<2)+f|0;if((b-1&1073741823)>>>0>=7){while(1){H[d>>2]=H[c>>2];H[d+4>>2]=H[c>>2];H[d+8>>2]=H[c>>2];H[d+12>>2]=H[c>>2];H[d+16>>2]=H[c>>2];H[d+20>>2]=H[c>>2];H[d+24>>2]=H[c>>2];H[d+28>>2]=H[c>>2];d=d+32|0;if((g|0)!=(d|0)){continue}break}}if((e|0)!=(i|0)){while(1){f=f-4|0;e=e-4|0;H[f>>2]=H[e>>2];if((e|0)!=(i|0)){continue}break}}H[a+8>>2]=(h<<2)+k;H[a+4>>2]=g;H[a>>2]=f;if(i){oa(i)}return}sa();v()}wa();v()}function Kc(a,b,c){var d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0;d=H[a+8>>2];e=H[a>>2];if(d-e>>2>>>0>=b>>>0){f=H[a+4>>2];h=f-e>>2;i=b>>>0>h>>>0?h:b;a:{if(!i){break a}d=e;g=i;j=g&7;if(j){while(1){H[d>>2]=H[c>>2];g=g-1|0;d=d+4|0;k=k+1|0;if((k|0)!=(j|0)){continue}break}}if(i>>>0<8){break a}while(1){H[d>>2]=H[c>>2];H[d+4>>2]=H[c>>2];H[d+8>>2]=H[c>>2];H[d+12>>2]=H[c>>2];H[d+16>>2]=H[c>>2];H[d+20>>2]=H[c>>2];H[d+24>>2]=H[c>>2];H[d+28>>2]=H[c>>2];d=d+32|0;g=g-8|0;if(g){continue}break}}if(b>>>0>h>>>0){b=(b-h<<2)+f|0;while(1){H[f>>2]=H[c>>2];f=f+4|0;if((b|0)!=(f|0)){continue}break}H[a+4>>2]=b;return}H[a+4>>2]=e+(b<<2);return}if(e){H[a+4>>2]=e;oa(e);H[a+8>>2]=0;H[a>>2]=0;H[a+4>>2]=0;d=0}b:{if(b>>>0>=1073741824){break b}e=d>>>1|0;d=d>>>0>=2147483644?1073741823:b>>>0>>0?e:b;if(d>>>0>=1073741824){break b}d=d<<2;e=pa(d);H[a>>2]=e;H[a+8>>2]=d+e;c=H[c>>2];d=e;g=b&7;if(g){while(1){H[d>>2]=c;d=d+4|0;f=f+1|0;if((g|0)!=(f|0)){continue}break}}e=e+(b<<2)|0;if((b-1&1073741823)>>>0>=7){while(1){H[d+28>>2]=c;H[d+24>>2]=c;H[d+20>>2]=c;H[d+16>>2]=c;H[d+12>>2]=c;H[d+8>>2]=c;H[d+4>>2]=c;H[d>>2]=c;d=d+32|0;if((e|0)!=(d|0)){continue}break}}H[a+4>>2]=e;return}sa();v()}function Me(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0;h=ca-16|0;ca=h;a:{b:{if(J[b+38>>1]<=511){e=H[b+8>>2];c=H[b+12>>2];i=c;f=H[b+20>>2];d=H[b+16>>2];g=d+8|0;f=g>>>0<8?f+1|0:f;if(e>>>0>>0&(c|0)<=(f|0)|(c|0)<(f|0)){break a}d=d+H[b>>2]|0;c=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);d=I[d+4|0]|I[d+5|0]<<8|(I[d+6|0]<<16|I[d+7|0]<<24);H[b+16>>2]=g;H[b+20>>2]=f;break b}if(!gb(1,h+8|0,b)){break a}g=H[b+16>>2];f=H[b+20>>2];e=H[b+8>>2];i=H[b+12>>2];c=H[h+8>>2];d=H[h+12>>2]}j=e-g|0;e=i-(f+(e>>>0>>0)|0)|0;if((e|0)==(d|0)&c>>>0>j>>>0|d>>>0>e>>>0){break a}e=d+f|0;f=c+g|0;e=f>>>0>>0?e+1|0:e;H[b+16>>2]=f;H[b+20>>2]=e;if((c|0)<=0){break a}b=H[b>>2]+g|0;H[a+40>>2]=b;g=c-1|0;e=b+g|0;f=I[e|0];c:{if(f>>>0<=63){H[a+44>>2]=g;b=I[e|0]&63;break c}d:{switch((f>>>6|0)-1|0){case 0:if(c>>>0<2){break a}c=c-2|0;H[a+44>>2]=c;b=b+c|0;b=I[b+1|0]<<8&16128|I[b|0];break c;case 1:if(c>>>0<3){break a}c=c-3|0;H[a+44>>2]=c;b=b+c|0;b=I[b+1|0]<<8|I[b+2|0]<<16&4128768|I[b|0];break c;default:break d}}c=c-4|0;H[a+44>>2]=c;b=b+c|0;b=(I[b|0]|I[b+1|0]<<8|(I[b+2|0]<<16|I[b+3|0]<<24))&1073741823}H[a+48>>2]=b+16384;k=b>>>0<4177920}ca=h+16|0;return k}function Ua(a,b,c){var d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0;f=(c>>>0)/3|0;j=H[(H[H[a+8>>2]+96>>2]+N(f,12)|0)+(c-N(f,3)<<2)>>2];a:{h=H[H[a+12>>2]+4>>2];e=H[h+4>>2];if((e|0)!=H[h+8>>2]){H[e>>2]=j;H[h+4>>2]=e+4;break a}b:{i=H[h>>2];f=e-i|0;g=f>>2;d=g+1|0;if(d>>>0<1073741824){k=g<<2;g=f>>>1|0;g=f>>>0>=2147483644?1073741823:d>>>0>>0?g:d;if(g){if(g>>>0>=1073741824){break b}f=pa(g<<2)}else{f=0}d=k+f|0;H[d>>2]=j;j=d+4|0;if((e|0)!=(i|0)){while(1){d=d-4|0;e=e-4|0;H[d>>2]=H[e>>2];if((e|0)!=(i|0)){continue}break}}H[h+8>>2]=f+(g<<2);H[h+4>>2]=j;H[h>>2]=d;if(i){oa(i)}break a}sa();v()}wa();v()}c:{d:{h=H[a+4>>2];e=H[h+4>>2];e:{if((e|0)!=H[h+8>>2]){H[e>>2]=c;H[h+4>>2]=e+4;break e}i=H[h>>2];f=e-i|0;j=f>>2;d=j+1|0;if(d>>>0>=1073741824){break d}g=f>>>1|0;g=f>>>0>=2147483644?1073741823:d>>>0>>0?g:d;if(g){if(g>>>0>=1073741824){break c}f=pa(g<<2)}else{f=0}d=f+(j<<2)|0;H[d>>2]=c;c=d+4|0;if((e|0)!=(i|0)){while(1){d=d-4|0;e=e-4|0;H[d>>2]=H[e>>2];if((e|0)!=(i|0)){continue}break}}H[h+8>>2]=f+(g<<2);H[h+4>>2]=c;H[h>>2]=d;if(!i){break e}oa(i)}a=H[a+4>>2];H[H[a+12>>2]+(b<<2)>>2]=H[a+24>>2];H[a+24>>2]=H[a+24>>2]+1;return}sa();v()}wa();v()}function Wb(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0,k=0;h=d-c|0;if((h|0)<=0){return}a:{e=H[a+8>>2];i=H[a+4>>2];if((e-i|0)>=(h|0)){j=i-b|0;if((j|0)>=(h|0)){f=i;g=d;break a}f=i;g=c+j|0;if((g|0)!=(d|0)){e=g;while(1){F[f|0]=I[e|0];f=f+1|0;e=e+1|0;if((e|0)!=(d|0)){continue}break}}H[a+4>>2]=f;if((j|0)>0){break a}return}k=H[a>>2];g=(i-k|0)+h|0;if((g|0)>=0){j=b-k|0;f=e-k|0;e=f<<1;f=f>>>0>=1073741823?2147483647:e>>>0>g>>>0?e:g;if(f){e=pa(f)}else{e=0}g=j+e|0;if((c|0)!=(d|0)){g=qa(g,c,h)+h|0}d=va(e,k,j);c=i-b|0;b=va(g,b,c);H[a+8>>2]=e+f;H[a+4>>2]=b+c;H[a>>2]=d;if(k){oa(k)}return}sa();v()}e=f;d=e-h|0;if(i>>>0>d>>>0){while(1){F[e|0]=I[d|0];e=e+1|0;d=d+1|0;if(i>>>0>d>>>0){continue}break}}H[a+4>>2]=e;a=b+h|0;if((a|0)!=(f|0)){a=f-a|0;va(f-a|0,b,a)}if((c|0)==(g|0)){return}f=(c^-1)+g|0;a=g-c&7;b:{if(!a){e=b;break b}d=0;e=b;while(1){F[e|0]=I[c|0];e=e+1|0;c=c+1|0;d=d+1|0;if((a|0)!=(d|0)){continue}break}}if(f>>>0<7){return}while(1){F[e|0]=I[c|0];F[e+1|0]=I[c+1|0];F[e+2|0]=I[c+2|0];F[e+3|0]=I[c+3|0];F[e+4|0]=I[c+4|0];F[e+5|0]=I[c+5|0];F[e+6|0]=I[c+6|0];F[e+7|0]=I[c+7|0];e=e+8|0;c=c+8|0;if((g|0)!=(c|0)){continue}break}}function qa(a,b,c){var d=0,e=0,f=0;if(c>>>0>=512){ba(a|0,b|0,c|0);return a}e=a+c|0;a:{if(!((a^b)&3)){b:{if(!(a&3)){c=a;break b}if(!c){c=a;break b}c=a;while(1){F[c|0]=I[b|0];b=b+1|0;c=c+1|0;if(!(c&3)){break b}if(c>>>0>>0){continue}break}}d=e&-4;c:{if(d>>>0<64){break c}f=d+-64|0;if(f>>>0>>0){break c}while(1){H[c>>2]=H[b>>2];H[c+4>>2]=H[b+4>>2];H[c+8>>2]=H[b+8>>2];H[c+12>>2]=H[b+12>>2];H[c+16>>2]=H[b+16>>2];H[c+20>>2]=H[b+20>>2];H[c+24>>2]=H[b+24>>2];H[c+28>>2]=H[b+28>>2];H[c+32>>2]=H[b+32>>2];H[c+36>>2]=H[b+36>>2];H[c+40>>2]=H[b+40>>2];H[c+44>>2]=H[b+44>>2];H[c+48>>2]=H[b+48>>2];H[c+52>>2]=H[b+52>>2];H[c+56>>2]=H[b+56>>2];H[c+60>>2]=H[b+60>>2];b=b- -64|0;c=c- -64|0;if(f>>>0>=c>>>0){continue}break}}if(c>>>0>=d>>>0){break a}while(1){H[c>>2]=H[b>>2];b=b+4|0;c=c+4|0;if(d>>>0>c>>>0){continue}break}break a}if(e>>>0<4){c=a;break a}d=e-4|0;if(d>>>0>>0){c=a;break a}c=a;while(1){F[c|0]=I[b|0];F[c+1|0]=I[b+1|0];F[c+2|0]=I[b+2|0];F[c+3|0]=I[b+3|0];b=b+4|0;c=c+4|0;if(d>>>0>=c>>>0){continue}break}}if(c>>>0>>0){while(1){F[c|0]=I[b|0];b=b+1|0;c=c+1|0;if((e|0)!=(c|0)){continue}break}}return a}function ub(a,b){var c=0,d=0,e=0,f=0,g=0;d=ca-16|0;ca=d;H[a+12>>2]=b;H[a+8>>2]=0;H[a>>2]=0;H[a+4>>2]=0;c=a+16|0;H[c>>2]=0;H[c+4>>2]=0;F[c+5|0]=0;F[c+6|0]=0;F[c+7|0]=0;F[c+8|0]=0;F[c+9|0]=0;F[c+10|0]=0;F[c+11|0]=0;F[c+12|0]=0;H[a+32>>2]=0;H[a+36>>2]=0;H[a+48>>2]=0;H[a+40>>2]=0;H[a+44>>2]=0;H[a+52>>2]=0;H[a+56>>2]=0;H[a+68>>2]=0;H[a+60>>2]=0;H[a+64>>2]=0;H[a+72>>2]=0;H[a+76>>2]=0;H[a+88>>2]=0;H[a+80>>2]=0;H[a+84>>2]=0;H[a+100>>2]=0;H[a+92>>2]=0;H[a+96>>2]=0;g=a+116|0;a:{b:{if(b){if(b>>>0<1073741824){break b}sa();v()}H[a+104>>2]=0;H[a+108>>2]=0;H[a+112>>2]=0;H[d+8>>2]=0;H[d>>2]=0;H[d+4>>2]=0;c=1;break a}c=b<<2;e=pa(c);H[a+92>>2]=e;f=c+e|0;H[a+100>>2]=f;ra(e,0,c);H[a+112>>2]=0;H[a+104>>2]=0;H[a+108>>2]=0;H[a+96>>2]=f;e=pa(c);H[a+104>>2]=e;f=c+e|0;H[a+112>>2]=f;ra(e,0,c);H[a+108>>2]=f;e=pa(c);H[d>>2]=e;f=c+e|0;H[d+8>>2]=f;ra(e,0,c);H[d+4>>2]=f;c=b<<5|1}tb(g,c,d);e=H[d>>2];if(e){H[d+4>>2]=e;oa(e)}H[d+8>>2]=0;H[d>>2]=0;H[d+4>>2]=0;if(b){b=b<<2;e=pa(b);H[d>>2]=e;f=b+e|0;H[d+8>>2]=f;ra(e,0,b);H[d+4>>2]=f}tb(a+128|0,c,d);b=H[d>>2];if(b){H[d+4>>2]=b;oa(b)}ca=d+16|0;return a}function ze(a){a=a|0;var b=0,c=0,d=0,e=0,f=0;H[a>>2]=11484;d=a+232|0;b=H[d+196>>2];if(b){H[d+200>>2]=b;oa(b)}c=H[d+184>>2];if(c){b=c;e=H[d+188>>2];if((b|0)!=(e|0)){while(1){b=e-12|0;f=H[b>>2];if(f){H[e-8>>2]=f;oa(f)}e=b;if((b|0)!=(c|0)){continue}break}b=H[d+184>>2]}H[d+188>>2]=c;oa(b)}b=H[d+156>>2];if(b){H[d+160>>2]=b;oa(b)}c=H[d+136>>2];H[d+136>>2]=0;if(c){e=c-4|0;b=H[e>>2];if(b){b=c+(b<<4)|0;while(1){b=b-16|0;if((c|0)!=(b|0)){continue}break}}oa(e)}Yc(a+216|0);b=H[a+196>>2];if(b){H[a+200>>2]=b;oa(b)}b=H[a+184>>2];if(b){H[a+188>>2]=b;oa(b)}b=H[a+172>>2];if(b){H[a+176>>2]=b;oa(b)}b=H[a+160>>2];if(b){H[a+164>>2]=b;oa(b)}b=H[a+144>>2];if(b){while(1){c=H[b>>2];oa(b);b=c;if(b){continue}break}}b=H[a+136>>2];H[a+136>>2]=0;if(b){oa(b)}b=H[a+120>>2];if(b){oa(b)}b=H[a+108>>2];if(b){oa(b)}b=H[a+96>>2];if(b){oa(b)}b=H[a+72>>2];if(b){H[a+76>>2]=b;oa(b)}b=H[a+60>>2];if(b){oa(b)}b=H[a+48>>2];if(b){H[a+52>>2]=b;oa(b)}b=H[a+36>>2];if(b){H[a+40>>2]=b;oa(b)}b=H[a+24>>2];if(b){H[a+28>>2]=b;oa(b)}b=H[a+12>>2];if(b){H[a+16>>2]=b;oa(b)}b=H[a+8>>2];H[a+8>>2]=0;if(b){cb(b)}return a|0}function Pa(a,b,c){var d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0;d=H[a+8>>2];e=H[a+4>>2];if(d-e>>2>>>0>=b>>>0){a:{if(!b){break a}d=e;f=b&7;if(f){while(1){H[d>>2]=H[c>>2];d=d+4|0;h=h+1|0;if((f|0)!=(h|0)){continue}break}}e=(b<<2)+e|0;if((b-1&1073741823)>>>0<7){break a}while(1){H[d>>2]=H[c>>2];H[d+4>>2]=H[c>>2];H[d+8>>2]=H[c>>2];H[d+12>>2]=H[c>>2];H[d+16>>2]=H[c>>2];H[d+20>>2]=H[c>>2];H[d+24>>2]=H[c>>2];H[d+28>>2]=H[c>>2];d=d+32|0;if((e|0)!=(d|0)){continue}break}}H[a+4>>2]=e;return}b:{i=H[a>>2];j=e-i|0;f=j>>2;g=f+b|0;if(g>>>0<1073741824){d=d-i|0;e=d>>>1|0;g=d>>>0>=2147483644?1073741823:e>>>0>g>>>0?e:g;if(g){if(g>>>0>=1073741824){break b}k=pa(g<<2)}f=(f<<2)+k|0;d=f;e=b&7;if(e){while(1){H[d>>2]=H[c>>2];d=d+4|0;h=h+1|0;if((e|0)!=(h|0)){continue}break}}e=f+(b<<2)|0;if((b-1&1073741823)>>>0>=7){while(1){H[d>>2]=H[c>>2];H[d+4>>2]=H[c>>2];H[d+8>>2]=H[c>>2];H[d+12>>2]=H[c>>2];H[d+16>>2]=H[c>>2];H[d+20>>2]=H[c>>2];H[d+24>>2]=H[c>>2];H[d+28>>2]=H[c>>2];d=d+32|0;if((e|0)!=(d|0)){continue}break}}b=va(k,i,j);H[a+4>>2]=e;H[a>>2]=b;H[a+8>>2]=b+(g<<2);if(i){oa(i)}return}sa();v()}wa();v()}function Cc(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0;if(I[a+11|0]>>>7|0){d=H[a+4>>2]}else{d=I[a+11|0]&127}if(d>>>0>>0){h=ca-16|0;ca=h;b=b-d|0;if(b){g=I[a+11|0]>>>7|0?(H[a+8>>2]&2147483647)-1|0:10;if(I[a+11|0]>>>7|0){d=H[a+4>>2]}else{d=I[a+11|0]&127}i=d+b|0;if(g-d>>>0>>0){a:{e=ca-16|0;ca=e;c=i-g|0;if(c>>>0<=2147483631-g>>>0){if(I[a+11|0]>>>7|0){f=H[a>>2]}else{f=a}if(g>>>0<1073741799){H[e+12>>2]=g<<1;H[e>>2]=c+g;c=ca-16|0;ca=c;ca=c+16|0;c=e+12|0;c=H[(K[e>>2]>2]?c:e)>>2];if(c>>>0>=11){j=c+16&-16;c=j-1|0;c=(c|0)==11?j:c}else{c=10}c=c+1|0}else{c=2147483631}Zb(e,c);c=H[e>>2];if(d){yb(c,f,d)}if((g|0)!=10){oa(f)}H[a>>2]=c;H[a+8>>2]=H[a+8>>2]&-2147483648|H[e+4>>2]&2147483647;H[a+8>>2]=H[a+8>>2]|-2147483648;ca=e+16|0;break a}Na();v()}}f=d;if(I[a+11|0]>>>7|0){d=H[a>>2]}else{d=a}f=f+d|0;e=ca-16|0;ca=e;F[e+15|0]=0;while(1){if(b){F[f|0]=I[e+15|0];b=b-1|0;f=f+1|0;continue}break}ca=e+16|0;Id(a,i);F[h+15|0]=0;F[d+i|0]=I[h+15|0]}ca=h+16|0;return}if(I[a+11|0]>>>7|0){d=H[a>>2]}else{d=a}f=ca-16|0;ca=f;Id(a,b);F[f+15|0]=0;F[b+d|0]=I[f+15|0];ca=f+16|0}function Jc(a,b){var c=0,d=0,e=0,f=0,g=0,h=0;g=ca-16|0;ca=g;a:{b:{if(b){H[a+88>>2]=0;H[a+92>>2]=0;d=H[a+84>>2];H[a+84>>2]=0;if(d){oa(d)}H[a+76>>2]=0;H[a+80>>2]=0;d=H[a+72>>2];H[a+72>>2]=0;if(d){oa(d)}d=H[b>>2];c=H[b+4>>2];F[g+15|0]=0;Oa(a,c-d>>2,g+15|0);d=H[b+28>>2];c=H[b+24>>2];F[g+14|0]=0;Oa(a+12|0,d-c>>2,g+14|0);Kc(a+28|0,H[b+4>>2]-H[b>>2]>>2,13708);c=H[b+28>>2]-H[b+24>>2]|0;f=c>>2;e=H[a+52>>2];c:{if(f>>>0<=H[a+60>>2]-e>>2>>>0){break c}if((c|0)<0){break b}d=H[a+56>>2];c=pa(c);f=c+(f<<2)|0;h=c+(d-e&-4)|0;c=h;if((d|0)!=(e|0)){while(1){c=c-4|0;d=d-4|0;H[c>>2]=H[d>>2];if((d|0)!=(e|0)){continue}break}}H[a+60>>2]=f;H[a+56>>2]=h;H[a+52>>2]=c;if(!e){break c}oa(e)}c=H[b+28>>2]-H[b+24>>2]|0;f=c>>2;e=H[a+40>>2];d:{if(f>>>0<=H[a+48>>2]-e>>2>>>0){break d}if((c|0)<0){break a}d=H[a+44>>2];c=pa(c);f=c+(f<<2)|0;h=c+(d-e&-4)|0;c=h;if((d|0)!=(e|0)){while(1){c=c-4|0;d=d-4|0;H[c>>2]=H[d>>2];if((d|0)!=(e|0)){continue}break}}H[a+48>>2]=f;H[a+44>>2]=h;H[a+40>>2]=c;if(!e){break d}oa(e)}F[a+24|0]=1;H[a+64>>2]=b}ca=g+16|0;return}sa();v()}sa();v()}function wb(a,b){var c=0,d=0,e=0,f=0,g=0;c=ca-16|0;ca=c;H[a+12>>2]=b;H[a+8>>2]=0;H[a>>2]=0;H[a+4>>2]=0;H[a+16>>2]=0;H[a+20>>2]=0;H[a+32>>2]=0;H[a+24>>2]=0;H[a+28>>2]=0;H[a+36>>2]=0;H[a+40>>2]=0;H[a+52>>2]=0;H[a+44>>2]=0;H[a+48>>2]=0;H[a+56>>2]=0;H[a+60>>2]=0;H[a+72>>2]=0;H[a+64>>2]=0;H[a+68>>2]=0;H[a+76>>2]=0;H[a+80>>2]=0;H[a+92>>2]=0;H[a+84>>2]=0;H[a+88>>2]=0;H[a+104>>2]=0;H[a+96>>2]=0;H[a+100>>2]=0;g=a+120|0;a:{b:{if(b){if(b>>>0<1073741824){break b}sa();v()}H[a+108>>2]=0;H[a+112>>2]=0;H[a+116>>2]=0;H[c+8>>2]=0;H[c>>2]=0;H[c+4>>2]=0;e=1;break a}e=b<<2;d=pa(e);H[a+96>>2]=d;f=d+e|0;H[a+104>>2]=f;ra(d,0,e);H[a+116>>2]=0;H[a+108>>2]=0;H[a+112>>2]=0;H[a+100>>2]=f;d=pa(e);H[a+108>>2]=d;f=d+e|0;H[a+116>>2]=f;ra(d,0,e);H[a+112>>2]=f;d=pa(e);H[c>>2]=d;f=d+e|0;H[c+8>>2]=f;ra(d,0,e);H[c+4>>2]=f;e=b<<5|1}tb(g,e,c);d=H[c>>2];if(d){H[c+4>>2]=d;oa(d)}H[c+8>>2]=0;H[c>>2]=0;H[c+4>>2]=0;if(b){b=b<<2;d=pa(b);H[c>>2]=d;f=b+d|0;H[c+8>>2]=f;ra(d,0,b);H[c+4>>2]=f}tb(a+132|0,e,c);b=H[c>>2];if(b){H[c+4>>2]=b;oa(b)}ca=c+16|0;return a}function Sb(a,b){var c=0,d=0,e=0;c=(a|0)==(b|0);F[b+12|0]=c;a:{if(c){break a}while(1){d=H[b+8>>2];if(I[d+12|0]){break a}b:{c=H[d+8>>2];e=H[c>>2];if((e|0)==(d|0)){e=H[c+4>>2];if(!(!e|I[e+12|0])){break b}c:{if(H[d>>2]==(b|0)){b=d;break c}b=H[d+4>>2];a=H[b>>2];H[d+4>>2]=a;if(a){H[a+8>>2]=d;c=H[d+8>>2]}H[b+8>>2]=c;a=H[d+8>>2];H[((H[a>>2]!=(d|0))<<2)+a>>2]=b;H[b>>2]=d;H[d+8>>2]=b;c=H[b+8>>2];d=H[c>>2]}F[b+12|0]=1;F[c+12|0]=0;a=H[d+4>>2];H[c>>2]=a;if(a){H[a+8>>2]=c}H[d+8>>2]=H[c+8>>2];a=H[c+8>>2];H[((H[a>>2]!=(c|0))<<2)+a>>2]=d;H[d+4>>2]=c;H[c+8>>2]=d;return}if(!(I[e+12|0]|!e)){break b}d:{if(H[d>>2]!=(b|0)){b=d;break d}a=H[b+4>>2];H[d>>2]=a;if(a){H[a+8>>2]=d;c=H[d+8>>2]}H[b+8>>2]=c;a=H[d+8>>2];H[((H[a>>2]!=(d|0))<<2)+a>>2]=b;H[b+4>>2]=d;H[d+8>>2]=b;c=H[b+8>>2]}F[b+12|0]=1;F[c+12|0]=0;a=H[c+4>>2];b=H[a>>2];H[c+4>>2]=b;if(b){H[b+8>>2]=c}H[a+8>>2]=H[c+8>>2];b=H[c+8>>2];H[((H[b>>2]!=(c|0))<<2)+b>>2]=a;H[a>>2]=c;H[c+8>>2]=a;break a}F[d+12|0]=1;F[c+12|0]=(a|0)==(c|0);F[e+12|0]=1;b=c;if((c|0)!=(a|0)){continue}break}}}function Tj(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0;a:{b:{c:{d:{e:{f:{g:{h:{i:{j:{k:{if(b){if(!c){break k}if(!d){break j}e=Q(d)-Q(b)|0;if(e>>>0<=31){break i}break c}if((d|0)==1|d>>>0>1){break c}da=0;a=(a>>>0)/(c>>>0)|0;break a}if(!a){break h}if(!d|d-1&d){break g}a=b>>>Qj(d)|0;da=0;break a}if(!(c-1&c)){break f}h=(Q(c)+33|0)-Q(b)|0;g=0-h|0;break d}h=e+1|0;g=63-e|0;break d}da=0;a=(b>>>0)/(d>>>0)|0;break a}e=Q(d)-Q(b)|0;if(e>>>0<31){break e}break c}if((c|0)==1){break b}d=Qj(c);c=d&31;if((d&63)>>>0>=32){a=b>>>c|0}else{e=b>>>c|0;a=((1<>>c}da=e;break a}h=e+1|0;g=63-e|0}e=h&63;f=e&31;if(e>>>0>=32){e=0;i=b>>>f|0}else{e=b>>>f|0;i=((1<>>f}g=g&63;f=g&31;if(g>>>0>=32){b=a<>>32-f|b<>>31;e=i<<1|b>>>31;f=m-(j+(e>>>0>g>>>0)|0)>>31;k=c&f;i=e-k|0;e=j-((d&f)+(e>>>0>>0)|0)|0;b=b<<1|a>>>31;a=l|a<<1;l=f&1;h=h-1|0;if(h){continue}break}}da=b<<1|a>>>31;a=l|a<<1;break a}a=0;b=0}da=b}return a}function rc(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0;k=H[b+16>>2];h=H[c+4>>2]-k|0;e=H[c>>2]-k|0;H[c>>2]=e;H[c+4>>2]=h;g=H[b+16>>2];f=h>>31;i=(f^h)-f|0;f=e>>31;l=g>>>0>=i+((f^e)-f|0)>>>0;a:{if(l){f=h;break a}b:{c:{if((e|0)>=0){f=1;i=1;if((h|0)>=0){break b}j=1;f=-1;i=-1;if(e){break c}break b}j=-1;f=-1;i=-1;if((h|0)<=0){break b}}f=(h|0)<=0?-1:1;i=j}j=N(g,i);e=(e<<1)-j|0;i=(N(f,i)|0)>=0;g=N(f,g);f=((i?0-e|0:e)+g|0)/2|0;H[c+4>>2]=f;m=c;c=(h<<1)-g|0;e=(j+(i?0-c|0:c)|0)/2|0;H[m>>2]=e;g=H[b+16>>2]}c=H[d+4>>2]+f|0;e=H[d>>2]+e|0;d:{if((g|0)<(e|0)){e=e-H[b+4>>2]|0;break d}if((0-g|0)<=(e|0)){break d}e=H[b+4>>2]+e|0}e:{if((c|0)>(g|0)){c=c-H[b+4>>2]|0;break e}if((0-g|0)<=(c|0)){break e}c=H[b+4>>2]+c|0}f:{if(l){g=c;break f}g:{h:{if((e|0)>=0){b=1;f=1;if((c|0)>=0){break g}d=1;b=-1;f=-1;if(e){break h}break g}d=-1;b=-1;f=-1;if((c|0)<=0){break g}}b=(c|0)<=0?-1:1;f=d}d=N(f,g);h=(e<<1)-d|0;f=(N(b,f)|0)>=0;b=N(b,g);g=((f?0-h|0:h)+b|0)/2|0;b=(c<<1)-b|0;e=(d+(f?0-b|0:b)|0)/2|0}c=a;H[c>>2]=e+k;H[c+4>>2]=g+k}function Wh(a){a=a|0;var b=0,c=0,d=0,e=0,f=0,g=0,h=0;g=ca-16|0;ca=g;e=H[a+4>>2];d=H[e>>2];a:{b=H[a+12>>2];c=H[b+28>>2]-H[b+24>>2]|0;f=c>>2;b:{if(f>>>0<=H[e+8>>2]-d>>2>>>0){break b}if((c|0)<0){break a}b=H[e+4>>2];c=pa(c);f=c+(f<<2)|0;h=c+(b-d&-4)|0;c=h;if((b|0)!=(d|0)){while(1){c=c-4|0;b=b-4|0;H[c>>2]=H[b>>2];if((b|0)!=(d|0)){continue}break}}H[e+8>>2]=f;H[e+4>>2]=h;H[e>>2]=c;if(!d){break b}oa(d)}b=H[a+12>>2];c=H[b+28>>2];b=H[b+24>>2];H[g+12>>2]=0;b=c-b>>2;d=a+96|0;e=H[d>>2];c=H[a+100>>2]-e>>2;c:{if(b>>>0>c>>>0){Pa(d,b-c|0,g+12|0);break c}if(b>>>0>=c>>>0){break c}H[a+100>>2]=e+(b<<2)}e=a+8|0;b=H[a+116>>2];d:{if(b){d=H[b>>2];if((d|0)==H[b+4>>2]){c=1;break d}b=0;while(1){c=ye(e,H[(b<<2)+d>>2]);if(!c){break d}f=H[a+116>>2];d=H[f>>2];b=b+1|0;if(b>>>0>2]-d>>2>>>0){continue}break}break d}c=1;a=H[a+12>>2];a=H[a+4>>2]-H[a>>2]|0;if(a>>>0<12){break d}a=(a>>2>>>0)/3|0;b=0;while(1){c=ye(e,N(b,3));if(!c){break d}b=b+1|0;if((a|0)!=(b|0)){continue}break}}ca=g+16|0;return c|0}sa();v()}function gj(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0;c=H[b+88>>2];if(!(!c|H[c>>2]!=1)){e=H[c+8>>2];H[a+4>>2]=I[e|0]|I[e+1|0]<<8|(I[e+2|0]<<16|I[e+3|0]<<24);f=a+8|0;d=I[b+24|0];h=H[a+8>>2];g=H[a+12>>2]-h>>2;a:{if(d>>>0>g>>>0){ya(f,d-g|0);d=I[b+24|0];e=H[c+8>>2];break a}if(d>>>0>=g>>>0){break a}H[a+12>>2]=h+(d<<2)}b:{if(!d){b=4;break b}h=d&3;f=H[f>>2];c:{if(d-1>>>0<3){b=4;d=0;break c}k=d&252;d=0;b=4;while(1){g=d<<2;c=b+e|0;H[g+f>>2]=I[c|0]|I[c+1|0]<<8|(I[c+2|0]<<16|I[c+3|0]<<24);H[f+(g|4)>>2]=I[c+4|0]|I[c+5|0]<<8|(I[c+6|0]<<16|I[c+7|0]<<24);H[f+(g|8)>>2]=I[c+8|0]|I[c+9|0]<<8|(I[c+10|0]<<16|I[c+11|0]<<24);H[f+(g|12)>>2]=I[c+12|0]|I[c+13|0]<<8|(I[c+14|0]<<16|I[c+15|0]<<24);d=d+4|0;b=b+16|0;i=i+4|0;if((k|0)!=(i|0)){continue}break}}if(!h){break b}while(1){c=b+e|0;H[f+(d<<2)>>2]=I[c|0]|I[c+1|0]<<8|(I[c+2|0]<<16|I[c+3|0]<<24);d=d+1|0;b=b+4|0;j=j+1|0;if((h|0)!=(j|0)){continue}break}}d=a;a=b+e|0;H[d+20>>2]=I[a|0]|I[a+1|0]<<8|(I[a+2|0]<<16|I[a+3|0]<<24);d=1}return d|0}function se(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0;a:{b:{c:{if(!b){if((d|0)<0){break a}f=H[a+4>>2];b=H[a>>2];d=f-b|0;if(c>>>0>d>>>0){g=c-d|0;e=H[a+8>>2];if(g>>>0<=e-f>>>0){i=a,j=ra(f,0,g)+g|0,H[i+4>>2]=j;break c}if((c|0)<0){break b}f=e-b|0;e=f<<1;f=f>>>0>=1073741823?2147483647:c>>>0>>0?e:c;e=pa(f);ra(e+d|0,0,g);d=va(e,b,d);H[a+8>>2]=d+f;H[a+4>>2]=c+d;H[a>>2]=d;if(!b){break c}oa(b);break c}if(c>>>0>=d>>>0){break c}H[a+4>>2]=b+c;break c}if((d|0)<0){break a}e=H[a+4>>2];f=H[a>>2];g=e-f|0;d:{if((d|0)<=0&c>>>0<=g>>>0|(d|0)<0){break d}if(c>>>0>g>>>0){d=c-g|0;h=H[a+8>>2];if(d>>>0<=h-e>>>0){i=a,j=ra(e,0,d)+d|0,H[i+4>>2]=j;break d}if((c|0)<0){break b}e=h-f|0;h=e<<1;e=e>>>0>=1073741823?2147483647:c>>>0>>0?h:c;h=pa(e);ra(h+g|0,0,d);d=va(h,f,g);H[a+8>>2]=d+e;H[a+4>>2]=c+d;H[a>>2]=d;if(!f){break d}oa(f);break d}if(c>>>0>=g>>>0){break d}H[a+4>>2]=c+f}if(!c){break c}va(H[a>>2],b,c)}b=H[a+28>>2];c=H[a+24>>2]+1|0;b=c?b:b+1|0;H[a+24>>2]=c;H[a+28>>2]=b;g=1;break a}sa();v()}return g}function Jh(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0;k=H[a+12>>2];c=H[a+68>>2];d=H[c+80>>2];F[b+84|0]=0;n=b+68|0;i=H[b+68>>2];e=H[b+72>>2]-i>>2;a:{if(e>>>0>>0){qb(n,d-e|0,12372);c=H[a+68>>2];d=H[c+80>>2];break a}if(d>>>0>=e>>>0){break a}H[b+72>>2]=i+(d<<2)}b=H[c+100>>2];e=H[c+96>>2];i=(b-e|0)/12|0;m=1;b:{if((b|0)==(e|0)){break b}k=H[k+28>>2];f=H[k>>2];if((f|0)==-1){return 0}o=i>>>0<=1?1:i;c=e;b=0;m=0;while(1){g=H[c>>2];if(g>>>0>=d>>>0){break b}j=H[H[a+72>>2]+12>>2];h=H[j+(f<<2)>>2];if(h>>>0>=d>>>0){break b}f=H[n>>2];H[f+(g<<2)>>2]=h;g=k+(l<<2)|0;h=H[g+4>>2];if((h|0)==-1){break b}l=H[c+4>>2];if(l>>>0>=d>>>0){break b}h=H[(h<<2)+j>>2];if(h>>>0>=d>>>0){break b}H[f+(l<<2)>>2]=h;g=H[g+8>>2];if((g|0)==-1){break b}c=H[c+8>>2];if(c>>>0>=d>>>0){break b}j=H[(g<<2)+j>>2];if(j>>>0>=d>>>0){break b}H[f+(c<<2)>>2]=j;b=b+1|0;m=i>>>0<=b>>>0;if((b|0)==(o|0)){break b}c=e+N(b,12)|0;l=N(b,3);f=H[k+(l<<2)>>2];if((f|0)!=-1){continue}break}}return m|0}function Gh(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0;h=H[d+80>>2];e=ca-48|0;ca=e;a=H[a+4>>2];m=a-2|0;a:{if(m>>>0>28){break a}j=H[H[d>>2]>>2]+H[d+48>>2]|0;H[e+16>>2]=a;a=-1<>2]=a^-1;a=-2-a|0;H[e+24>>2]=a;H[e+32>>2]=(a|0)/2;L[e+28>>2]=O(2)/O(a|0);f=H[c>>2];if((f|0)!=H[c+4>>2]){a=0;d=0;while(1){g=H[(d<<2)+f>>2];h=e+36|0;k=H[H[b>>2]>>2];l=H[b+48>>2];f=H[b+40>>2];i=H[b+44>>2];if(!I[b+84|0]){g=H[H[b+68>>2]+(g<<2)>>2]}g=Rj(f,i,g,0);i=g;g=g+l|0;qa(h,g+k|0,f);he(e+16|0,h,e+12|0,e+8|0);f=a<<2;H[f+j>>2]=H[e+12>>2];H[(f|4)+j>>2]=H[e+8>>2];a=a+2|0;d=d+1|0;f=H[c>>2];if(d>>>0>2]-f>>2>>>0){continue}break}break a}if(!h){break a}d=0;a=0;while(1){k=e+36|0;l=H[H[b>>2]>>2];i=H[b+48>>2];c=H[b+40>>2];f=Rj(c,H[b+44>>2],I[b+84|0]?a:H[H[b+68>>2]+(a<<2)>>2],0);g=f;f=f+i|0;qa(k,f+l|0,c);he(e+16|0,k,e+12|0,e+8|0);c=d<<2;H[c+j>>2]=H[e+12>>2];H[(c|4)+j>>2]=H[e+8>>2];d=d+2|0;a=a+1|0;if((h|0)!=(a|0)){continue}break}}ca=e+48|0;return m>>>0<29|0}function Re(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0,h=0,i=O(0),j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0;k=ca-16|0;ca=k;if(H[c+28>>2]==9){d=H[a+4>>2];h=I[c+24|0];e=h<<2;f=pa(e);l=k+8|0;H[l>>2]=1065353216;i=L[a+20>>2];d=-1<0){L[l>>2]=i/O(d|0)}o=(d|0)>0;a:{if(!o){break a}j=H[c+80>>2];if(!j){break a}if(h){p=H[H[b>>2]>>2]+H[b+48>>2]|0;t=h&254;u=h&1;b=0;while(1){m=H[a+8>>2];i=L[l>>2];d=0;n=0;if((h|0)!=1){while(1){g=d<<2;q=(b<<2)+p|0;L[g+f>>2]=O(i*O(H[q>>2]))+L[g+m>>2];g=g|4;L[g+f>>2]=O(i*O(H[q+4>>2]))+L[g+m>>2];d=d+2|0;b=b+2|0;n=n+2|0;if((t|0)!=(n|0)){continue}break}}if(u){d=d<<2;L[d+f>>2]=O(i*O(H[(b<<2)+p>>2]))+L[d+m>>2];b=b+1|0}qa(H[H[c+64>>2]>>2]+r|0,f,e);r=e+r|0;s=s+1|0;if((s|0)!=(j|0)){continue}break}break a}b=0;if((j|0)!=1){a=j&-2;d=0;while(1){qa(H[H[c+64>>2]>>2]+b|0,f,e);b=b+e|0;qa(b+H[H[c+64>>2]>>2]|0,f,e);b=b+e|0;d=d+2|0;if((a|0)!=(d|0)){continue}break}}if(!(j&1)){break a}qa(H[H[c+64>>2]>>2]+b|0,f,e)}oa(f)}ca=k+16|0;return o|0}function Xh(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0;c=H[a+12>>2];d=H[a+108>>2];e=H[d+80>>2];F[b+84|0]=0;m=b+68|0;h=H[b+68>>2];f=H[b+72>>2]-h>>2;a:{if(f>>>0>>0){qb(m,e-f|0,12372);d=H[a+108>>2];e=H[d+80>>2];break a}if(e>>>0>=f>>>0){break a}H[b+72>>2]=h+(e<<2)}b=H[d+100>>2];f=H[d+96>>2];h=(b-f|0)/12|0;k=1;b:{if((b|0)==(f|0)){break b}n=h>>>0<=1?1:h;o=H[c>>2];c=0;d=f;b=0;k=0;while(1){c=(c<<2)+o|0;i=H[c>>2];if((i|0)==-1){break b}g=H[d>>2];if(g>>>0>=e>>>0){break b}l=H[H[a+112>>2]+12>>2];j=H[l+(i<<2)>>2];if(j>>>0>=e>>>0){break b}i=H[m>>2];H[i+(g<<2)>>2]=j;g=H[c+4>>2];if((g|0)==-1){break b}j=H[d+4>>2];if(j>>>0>=e>>>0){break b}g=H[(g<<2)+l>>2];if(g>>>0>=e>>>0){break b}H[i+(j<<2)>>2]=g;c=H[c+8>>2];if((c|0)==-1){break b}d=H[d+8>>2];if(d>>>0>=e>>>0){break b}c=H[(c<<2)+l>>2];if(c>>>0>=e>>>0){break b}H[i+(d<<2)>>2]=c;b=b+1|0;k=h>>>0<=b>>>0;if((b|0)==(n|0)){break b}c=N(b,3);d=f+N(b,12)|0;if((b|0)!=1431655765){continue}break}}return k|0}function Ph(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0;c=H[a+12>>2];d=H[a+68>>2];e=H[d+80>>2];F[b+84|0]=0;m=b+68|0;h=H[b+68>>2];f=H[b+72>>2]-h>>2;a:{if(f>>>0>>0){qb(m,e-f|0,12372);d=H[a+68>>2];e=H[d+80>>2];break a}if(e>>>0>=f>>>0){break a}H[b+72>>2]=h+(e<<2)}b=H[d+100>>2];f=H[d+96>>2];h=(b-f|0)/12|0;k=1;b:{if((b|0)==(f|0)){break b}n=h>>>0<=1?1:h;o=H[c>>2];c=0;d=f;b=0;k=0;while(1){c=(c<<2)+o|0;i=H[c>>2];if((i|0)==-1){break b}g=H[d>>2];if(g>>>0>=e>>>0){break b}l=H[H[a+72>>2]+12>>2];j=H[l+(i<<2)>>2];if(j>>>0>=e>>>0){break b}i=H[m>>2];H[i+(g<<2)>>2]=j;g=H[c+4>>2];if((g|0)==-1){break b}j=H[d+4>>2];if(j>>>0>=e>>>0){break b}g=H[(g<<2)+l>>2];if(g>>>0>=e>>>0){break b}H[i+(j<<2)>>2]=g;c=H[c+8>>2];if((c|0)==-1){break b}d=H[d+8>>2];if(d>>>0>=e>>>0){break b}c=H[(c<<2)+l>>2];if(c>>>0>=e>>>0){break b}H[i+(d<<2)>>2]=c;b=b+1|0;k=h>>>0<=b>>>0;if((b|0)==(n|0)){break b}c=N(b,3);d=f+N(b,12)|0;if((b|0)!=1431655765){continue}break}}return k|0}function Wa(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0;d=ca-16|0;ca=d;a:{f=H[a+4>>2];b:{if(f>>>0>>0){e=b-f|0;c=H[a+8>>2];g=c<<5;c:{if(!(e>>>0>g>>>0|f>>>0>g-e>>>0)){H[a+4>>2]=b;h=f&31;b=H[a>>2]+(f>>>3&536870908)|0;break c}H[d+8>>2]=0;H[d>>2]=0;H[d+4>>2]=0;if((b|0)<0){break a}if(g>>>0<=1073741822){c=c<<6;b=b+31&-32;b=b>>>0>>0?c:b}else{b=2147483647}pb(d,b);f=H[a+4>>2];H[d+4>>2]=f+e;i=H[a>>2];b=H[d>>2];d:{if((f|0)<=0){break d}c=f>>>5|0;if(f>>>0>=32){va(b,i,c<<2)}g=c<<2;b=g+b|0;h=f&31;if(h){c=-1>>>32-h|0;H[b>>2]=H[b>>2]&(c^-1)|H[i+g>>2]&c}i=H[a>>2]}H[a>>2]=H[d>>2];H[d>>2]=i;c=H[a+4>>2];H[a+4>>2]=H[d+4>>2];H[d+4>>2]=c;c=H[a+8>>2];H[a+8>>2]=H[d+8>>2];H[d+8>>2]=c;if(!i){break c}oa(i)}if(!e){break b}if(h){c=32-h|0;a=c>>>0>>0?c:e;H[b>>2]=H[b>>2]&(-1<>>c-a^-1);e=e-a|0;b=b+4|0}a=e>>>5|0;if(e>>>0>=32){ra(b,0,a<<2)}if((e&-32)==(e|0)){break b}a=(a<<2)+b|0;H[a>>2]=H[a>>2]&(-1>>>32-(e&31)^-1);break b}H[a+4>>2]=b}ca=d+16|0;return}sa();v()}function Je(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0,h=0,i=0,j=0;e=H[a+12>>2];i=H[a+8>>2];d=e-i>>2;b=I[b+24|0];a:{if(d>>>0>>0){ya(a+8|0,b-d|0);i=H[a+8>>2];e=H[a+12>>2];break a}if(b>>>0>=d>>>0){break a}e=(b<<2)+i|0;H[a+12>>2]=e}b=0;f=H[c+8>>2];h=H[c+12>>2];j=H[c+20>>2];e=e-i|0;d=H[c+16>>2];g=e+d|0;j=e>>>0>g>>>0?j+1|0:j;b:{if(f>>>0>>0&(h|0)<=(j|0)|(h|0)<(j|0)){break b}qa(i,d+H[c>>2]|0,e);d=H[c+20>>2];g=e;e=e+H[c+16>>2]|0;d=g>>>0>e>>>0?d+1|0:d;H[c+16>>2]=e;H[c+20>>2]=d;f=H[c+8>>2];h=H[c+12>>2];g=e+4|0;d=g>>>0<4?d+1|0:d;if(f>>>0>>0&(d|0)>=(h|0)|(d|0)>(h|0)){break b}d=e+H[c>>2]|0;H[a+20>>2]=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);d=H[c+20>>2];g=d;f=d;e=H[c+16>>2];d=e+4|0;f=d>>>0<4?f+1|0:f;H[c+16>>2]=d;H[c+20>>2]=f;h=H[c+12>>2];if((f|0)>=(h|0)&d>>>0>=K[c+8>>2]|(f|0)>(h|0)){break b}f=I[d+H[c>>2]|0];d=g;e=e+5|0;d=e>>>0<5?d+1|0:d;H[c+16>>2]=e;H[c+20>>2]=d;if(f-1>>>0>29){break b}H[a+4>>2]=f;b=1}return b|0}function qd(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0;a:{f=H[a+4>>2];b:{if((f|0)!=H[a>>2]){c=f;break b}g=H[a+8>>2];c=H[a+12>>2];if(g>>>0>>0){e=((c-g>>2)+1|0)/2<<2;c=e+g|0;if((f|0)!=(g|0)){d=g-f|0;c=c-d|0;va(c,f,d);f=H[a+8>>2]}H[a+4>>2]=c;H[a+8>>2]=e+f;break b}d=(c|0)==(f|0)?1:c-f>>1;if(d>>>0>=1073741824){break a}c=d<<2;i=pa(c);k=i+c|0;c=(d+3&-4)+i|0;h=c;c:{if((f|0)==(g|0)){break c}g=g-f|0;l=g&-4;e=c;d=f;j=g-4|0;g=(j>>>2|0)+1&7;if(g){h=0;while(1){H[e>>2]=H[d>>2];d=d+4|0;e=e+4|0;h=h+1|0;if((g|0)!=(h|0)){continue}break}}h=c+l|0;if(j>>>0<28){break c}while(1){H[e>>2]=H[d>>2];H[e+4>>2]=H[d+4>>2];H[e+8>>2]=H[d+8>>2];H[e+12>>2]=H[d+12>>2];H[e+16>>2]=H[d+16>>2];H[e+20>>2]=H[d+20>>2];H[e+24>>2]=H[d+24>>2];H[e+28>>2]=H[d+28>>2];d=d+32|0;e=e+32|0;if((h|0)!=(e|0)){continue}break}}H[a+12>>2]=k;H[a+8>>2]=h;H[a+4>>2]=c;H[a>>2]=i;if(!f){break b}oa(f);c=H[a+4>>2]}H[c-4>>2]=H[b>>2];H[a+4>>2]=H[a+4>>2]-4;return}wa();v()}function sb(a,b){var c=0;a:{if(!ta(a,b)){break a}if(!ta(a+16|0,b)){break a}if(!ta(a+32|0,b)){break a}if(!ta(a+48|0,b)){break a}if(!ta(a- -64|0,b)){break a}if(!ta(a+80|0,b)){break a}if(!ta(a+96|0,b)){break a}if(!ta(a+112|0,b)){break a}if(!ta(a+128|0,b)){break a}if(!ta(a+144|0,b)){break a}if(!ta(a+160|0,b)){break a}if(!ta(a+176|0,b)){break a}if(!ta(a+192|0,b)){break a}if(!ta(a+208|0,b)){break a}if(!ta(a+224|0,b)){break a}if(!ta(a+240|0,b)){break a}if(!ta(a+256|0,b)){break a}if(!ta(a+272|0,b)){break a}if(!ta(a+288|0,b)){break a}if(!ta(a+304|0,b)){break a}if(!ta(a+320|0,b)){break a}if(!ta(a+336|0,b)){break a}if(!ta(a+352|0,b)){break a}if(!ta(a+368|0,b)){break a}if(!ta(a+384|0,b)){break a}if(!ta(a+400|0,b)){break a}if(!ta(a+416|0,b)){break a}if(!ta(a+432|0,b)){break a}if(!ta(a+448|0,b)){break a}if(!ta(a+464|0,b)){break a}if(!ta(a+480|0,b)){break a}if(!ta(a+496|0,b)){break a}c=ta(a+512|0,b)}return c}function qf(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0;a:{if(!ke(a,b)){break a}h=a+36|0;g=ea[H[H[a>>2]+24>>2]](a)|0;e=H[a+40>>2];d=H[a+36>>2];c=e-d>>2;b:{if(g>>>0>c>>>0){Vb(h,g-c|0);break b}if(c>>>0<=g>>>0){break b}d=d+(g<<2)|0;if((d|0)!=(e|0)){while(1){e=e-4|0;c=H[e>>2];H[e>>2]=0;if(c){ea[H[H[c>>2]+4>>2]](c)}if((d|0)!=(e|0)){continue}break}}H[a+40>>2]=d}c=1;if((g|0)<=0){break a}e=0;while(1){c:{c=H[b+20>>2];f=H[b+12>>2];d=H[b+16>>2];if((c|0)>=(f|0)&d>>>0>=K[b+8>>2]|(c|0)>(f|0)){break c}f=I[H[b>>2]+d|0];d=d+1|0;c=d?c:c+1|0;H[b+16>>2]=d;H[b+20>>2]=c;d=ea[H[H[a>>2]+48>>2]](a,f)|0;f=e<<2;i=f+H[a+36>>2]|0;c=H[i>>2];H[i>>2]=d;if(c){ea[H[H[c>>2]+4>>2]](c)}c=H[H[h>>2]+f>>2];if(!c){break c}if(!(k=c,l=ea[H[H[a>>2]+28>>2]](a)|0,m=ea[H[H[a>>2]+20>>2]](a,e)|0,j=H[H[c>>2]+8>>2],ea[j](k|0,l|0,m|0)|0)){break c}c=1;e=e+1|0;if((g|0)!=(e|0)){continue}break a}break}c=0}return c|0}function he(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0;j=+L[b>>2];k=+L[b+4>>2];l=+L[b+8>>2];g=P(j)+P(k)+P(l);a:{if(!(g>1e-6)){j=1;k=0;e=0;break a}g=1/g;k=g*k;j=g*j;e=g*l<0}h=H[a+16>>2];l=+(h|0);g=T(j*l+.5);b:{if(P(g)<2147483648){m=~~g;break b}m=-2147483648}f=m>>31;i=(f^m)-f|0;g=T(k*l+.5);c:{if(P(g)<2147483648){f=~~g;break c}f=-2147483648}b=f>>31;b=h-(i+((f^b)-b|0)|0)|0;i=(b|0)>0?b:0;e=e?0-i|0:i;f=f+(b>>31&((f|0)>0?b:0-b|0))|0;d:{if((m|0)>=0){b=e+h|0;a=H[a+8>>2];e=h+f|0;break d}b=f>>31;b=(b^f)-b|0;a=H[a+8>>2];b=(e|0)<0?b:a-b|0;e=(f|0)<0?i:a-i|0}e:{if(!(b|e)){b=a;break e}if(!((a|0)!=(b|0)|e)){b=a;break e}if(!((a|0)!=(e|0)|b)){b=a;break e}if(!((b|0)<=(h|0)|e)){b=(h<<1)-b|0;a=0;break e}if(!((a|0)!=(e|0)|(b|0)>=(h|0))){b=(h<<1)-b|0;break e}if(!((a|0)!=(b|0)|(e|0)>=(h|0))){b=a;a=(h<<1)-e|0;break e}if(b){a=e;break e}b=0;if((e|0)<=(h|0)){a=e;break e}a=(h<<1)-e|0}H[c>>2]=a;H[d>>2]=b}function Ve(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0;g=H[b+8>>2];h=H[b+12>>2];c=H[b+20>>2];i=c;k=H[b+16>>2];d=k+4|0;c=d>>>0<4?c+1|0:c;a:{if(d>>>0>g>>>0&(c|0)>=(h|0)|(c|0)>(h|0)){break a}l=H[b>>2];f=k+l|0;e=I[f|0]|I[f+1|0]<<8|(I[f+2|0]<<16|I[f+3|0]<<24);H[b+16>>2]=d;H[b+20>>2]=c;c=i;f=k+8|0;c=f>>>0<8?c+1|0:c;if(f>>>0>g>>>0&(c|0)>=(h|0)|(c|0)>(h|0)){break a}d=d+l|0;j=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[b+16>>2]=f;H[b+20>>2]=c;if((e|0)>(j|0)){break a}H[a+16>>2]=j;H[a+12>>2]=e;d=j-e|0;e=(j>>31)-((e>>31)+(e>>>0>j>>>0)|0)|0;if(!e&d>>>0>2147483646|e){break a}d=d+1|0;H[a+20>>2]=d;e=d>>>1|0;H[a+24>>2]=e;H[a+28>>2]=0-e;if(!(d&1)){H[a+24>>2]=e-1}if(J[b+38>>1]<=513){if((c|0)>=(h|0)&f>>>0>=g>>>0|(c|0)>(h|0)){break a}g=I[f+l|0];c=i;i=k+9|0;c=i>>>0<9?c+1|0:c;H[b+16>>2]=i;H[b+20>>2]=c;if(g>>>0>1){break a}H[a+88>>2]=g}m=ta(a+112|0,b)}return m|0}function Hc(a,b){var c=0,d=0,e=0,f=0,g=0,h=0;g=H[a>>2];c=g+(b>>>3&536870908)|0;H[c>>2]=H[c>>2]|1<>2];e=(b|0)==-1;d=-1;a:{if(e){break a}c=b+1|0;c=(c>>>0)%3|0?c:b-2|0;d=-1;if((c|0)==-1){break a}d=H[H[f>>2]+(c<<2)>>2]}c=H[a+12>>2];h=(d>>>3&536870908)+c|0;H[h>>2]=H[h>>2]|1<>>0)%3|0){e=b-1|0;break e}e=b+2|0;d=-1;if((e|0)==-1){break d}}d=H[H[f>>2]+(e<<2)>>2]}e=(d>>>3&536870908)+c|0;H[e>>2]=H[e>>2]|1<>2]+(b<<2)>>2];if((b|0)==-1){break b}F[a+24|0]=0;a=(b>>>3&536870908)+g|0;H[a>>2]=H[a>>2]|1<>>0)%3|0?a:b-2|0;if((a|0)!=-1){d=H[H[f>>2]+(a<<2)>>2]}a=c+(d>>>3&536870908)|0;H[a>>2]=H[a>>2]|1<>>0)%3|0){b=b-1|0;break g}b=b+2|0;a=-1;if((b|0)==-1){break f}}a=H[H[f>>2]+(b<<2)>>2]}b=1<>>3&536870908)|0;c=H[a>>2];break c}a=c+536870908|0;b=H[c+536870908>>2];c=-2147483648}H[a>>2]=b|c}}function Fd(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=O(0),f=O(0),g=O(0),h=O(0),i=O(0),j=0,k=O(0),l=O(0),m=O(0),n=O(0),o=0;a:{if(H[c+28>>2]!=9|I[c+24|0]!=3){break a}a=H[a+4>>2];if(a-2>>>0>28){break a}o=1;j=H[c+80>>2];if(!j){break a}k=O(O(2)/O((1<>2]>>2]+H[c+48>>2]|0;a=H[H[b>>2]>>2]+H[b+48>>2]|0;b=0;while(1){g=O(0);l=O(0);m=O(0);e=O(O(O(H[a>>2])*k)+O(-1));f=O(O(O(H[a+4>>2])*k)+O(-1));i=O(O(O(1)-O(P(e)))-O(P(f)));h=O(S(O(-i),O(0)));n=O(-h);f=O(f+(f>>8;F[c+10|0]=d>>>16;F[c+11|0]=d>>>24;d=(w(l),y(2));F[c+4|0]=d;F[c+5|0]=d>>>8;F[c+6|0]=d>>>16;F[c+7|0]=d>>>24;d=(w(g),y(2));F[c|0]=d;F[c+1|0]=d>>>8;F[c+2|0]=d>>>16;F[c+3|0]=d>>>24;c=c+12|0;b=b+1|0;if((j|0)!=(b|0)){continue}break}}return o|0}function Vd(a,b,c){var d=0,e=0,f=0,g=0,h=0,i=0,j=0;a:{if(b>>>0<=63){b=0;a=H[a+12>>2];if(a>>>0<2){break a}b=a-1|0;e=b&3;d=H[c>>2];c=0;b:{if(a-2>>>0<3){a=1;b=0;break b}f=b&-4;b=0;a=1;while(1){g=a+3|0;h=a+2|0;i=a+1|0;b=K[d+(b<<2)>>2]>K[d+(a<<2)>>2]?a:b;b=K[d+(b<<2)>>2]>K[d+(i<<2)>>2]?i:b;b=K[d+(b<<2)>>2]>K[d+(h<<2)>>2]?h:b;b=K[d+(b<<2)>>2]>K[d+(g<<2)>>2]?g:b;a=a+4|0;j=j+4|0;if((f|0)!=(j|0)){continue}break}}if(!e){break a}while(1){b=K[d+(b<<2)>>2]>K[d+(a<<2)>>2]?a:b;a=a+1|0;c=c+1|0;if((e|0)!=(c|0)){continue}break}break a}b=H[a+580>>2];d=32-b|0;if((d|0)>=4){c=H[a+576>>2];if((c|0)==H[a+568>>2]){return 0}d=H[c>>2];e=b+4|0;H[a+580>>2]=e;b=d<>>28|0;if((e|0)!=32){break a}H[a+580>>2]=0;H[a+576>>2]=c+4;return b}c=H[a+576>>2];e=c+4|0;if((e|0)==H[a+568>>2]){return 0}f=H[c>>2];H[a+576>>2]=e;H[a+580>>2]=b-28;a=60-b|0;b=H[c+4>>2]>>>a|f<>>a-d}return b}function Ae(a){a=a|0;var b=0,c=0,d=0,e=0;H[a>>2]=11436;b=H[a+388>>2];if(b){H[a+392>>2]=b;oa(b)}d=H[a+368>>2];H[a+368>>2]=0;if(d){e=d-4|0;b=H[e>>2];if(b){c=(b<<4)+d|0;while(1){c=c-16|0;if((d|0)!=(c|0)){continue}break}}oa(e)}Yc(a+216|0);b=H[a+196>>2];if(b){H[a+200>>2]=b;oa(b)}b=H[a+184>>2];if(b){H[a+188>>2]=b;oa(b)}b=H[a+172>>2];if(b){H[a+176>>2]=b;oa(b)}b=H[a+160>>2];if(b){H[a+164>>2]=b;oa(b)}c=H[a+144>>2];if(c){while(1){b=H[c>>2];oa(c);c=b;if(b){continue}break}}b=H[a+136>>2];H[a+136>>2]=0;if(b){oa(b)}b=H[a+120>>2];if(b){oa(b)}b=H[a+108>>2];if(b){oa(b)}b=H[a+96>>2];if(b){oa(b)}b=H[a+72>>2];if(b){H[a+76>>2]=b;oa(b)}b=H[a+60>>2];if(b){oa(b)}b=H[a+48>>2];if(b){H[a+52>>2]=b;oa(b)}b=H[a+36>>2];if(b){H[a+40>>2]=b;oa(b)}b=H[a+24>>2];if(b){H[a+28>>2]=b;oa(b)}b=H[a+12>>2];if(b){H[a+16>>2]=b;oa(b)}b=H[a+8>>2];H[a+8>>2]=0;if(b){cb(b)}return a|0}function Sg(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;var e=0,f=0,g=0;a:{a=ca-32|0;ca=a;e=Ma(c);if(e>>>0<2147483632){b:{c:{if(e>>>0>=11){g=(e|15)+1|0;f=pa(g);H[a+24>>2]=g|-2147483648;H[a+16>>2]=f;H[a+20>>2]=e;g=e+f|0;break c}F[a+27|0]=e;f=a+16|0;g=e+f|0;if(!e){break b}}qa(f,c,e)}F[g|0]=0;H[a+8>>2]=0;H[a>>2]=0;H[a+4>>2]=0;d:{c=nb(b,a+16|0);if((c|0)==(b+4|0)){break d}b=H[c+28>>2];e=H[c+32>>2];if((b|0)==(e|0)){break d}b=e-b|0;if(b&3){break d}e=b>>>2|0;f=H[a+4>>2];b=H[a>>2];g=f-b>>2;e:{if(e>>>0>g>>>0){ya(a,e-g|0);b=H[a>>2];f=H[a+4>>2];break e}if(e>>>0>=g>>>0){break e}f=(e<<2)+b|0;H[a+4>>2]=f}if((b|0)!=(f|0)){e=b;b=H[c+28>>2];qa(e,b,H[c+32>>2]-b|0);break d}Ca();v()}b=H[d>>2];if(b){H[d+4>>2]=b;oa(b)}H[d>>2]=H[a>>2];H[d+4>>2]=H[a+4>>2];H[d+8>>2]=H[a+8>>2];if(F[a+27|0]<0){oa(H[a+16>>2])}ca=a+32|0;break a}Na();v()}}function Be(a){a=a|0;var b=0,c=0,d=0,e=0;H[a>>2]=11384;d=H[a+368>>2];H[a+368>>2]=0;if(d){e=d-4|0;b=H[e>>2];if(b){c=(b<<4)+d|0;while(1){c=c-16|0;if((d|0)!=(c|0)){continue}break}}oa(e)}Yc(a+216|0);b=H[a+196>>2];if(b){H[a+200>>2]=b;oa(b)}b=H[a+184>>2];if(b){H[a+188>>2]=b;oa(b)}b=H[a+172>>2];if(b){H[a+176>>2]=b;oa(b)}b=H[a+160>>2];if(b){H[a+164>>2]=b;oa(b)}c=H[a+144>>2];if(c){while(1){b=H[c>>2];oa(c);c=b;if(b){continue}break}}b=H[a+136>>2];H[a+136>>2]=0;if(b){oa(b)}b=H[a+120>>2];if(b){oa(b)}b=H[a+108>>2];if(b){oa(b)}b=H[a+96>>2];if(b){oa(b)}b=H[a+72>>2];if(b){H[a+76>>2]=b;oa(b)}b=H[a+60>>2];if(b){oa(b)}b=H[a+48>>2];if(b){H[a+52>>2]=b;oa(b)}b=H[a+36>>2];if(b){H[a+40>>2]=b;oa(b)}b=H[a+24>>2];if(b){H[a+28>>2]=b;oa(b)}b=H[a+12>>2];if(b){H[a+16>>2]=b;oa(b)}b=H[a+8>>2];H[a+8>>2]=0;if(b){cb(b)}return a|0}function Ug(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0,h=0,i=0,j=0;d=ca-16|0;ca=d;a:{e=Ma(c);if(e>>>0<2147483632){b:{c:{if(e>>>0>=11){f=(e|15)+1|0;a=pa(f);H[d+8>>2]=f|-2147483648;H[d>>2]=a;H[d+4>>2]=e;f=a+e|0;break c}F[d+11|0]=e;f=d+e|0;a=d;if(!e){break b}}qa(a,c,e)}F[f|0]=0;c=I[d+11|0];e=c<<24>>24;b=H[b+4>>2];a=0;d:{if(!b){break d}a=c;c=(e|0)<0;a=c?H[d+4>>2]:a;f=c?H[d>>2]:d;while(1){c=I[b+27|0];g=c<<24>>24<0;c=g?H[b+20>>2]:c;i=c>>>0>>0;e:{f:{g:{h:{i:{j:{h=i?c:a;if(h){g=g?H[b+16>>2]:b+16|0;j=Fa(f,g,h);if(j){break j}if(a>>>0>=c>>>0){break i}break e}if(a>>>0>=c>>>0){break h}break e}if((j|0)<0){break e}}c=Fa(g,f,h);if(c){break g}}if(i){break f}a=1;break d}if((c|0)<0){break f}a=1;break d}b=b+4|0}b=H[b>>2];if(b){continue}break}a=0}if((e|0)<0){oa(H[d>>2])}ca=d+16|0;break a}Na();v()}return a|0}function fd(a,b){var c=0,d=0;c=H[b+8>>2];H[a+4>>2]=H[b+4>>2];H[a+8>>2]=c;H[a+20>>2]=H[b+20>>2];c=H[b+16>>2];H[a+12>>2]=H[b+12>>2];H[a+16>>2]=c;a:{b:{if((a|0)!=(b|0)){c=H[b+28>>2];if(c){d=H[a+24>>2];if(H[a+32>>2]<<5>>>0>>0){if(d){oa(d);H[a+32>>2]=0;H[a+24>>2]=0;H[a+28>>2]=0;c=H[b+28>>2]}if((c|0)<0){break b}c=(c-1>>>5|0)+1|0;d=pa(c<<2);H[a+32>>2]=c;H[a+28>>2]=0;H[a+24>>2]=d;c=H[b+28>>2]}va(d,H[b+24>>2],(c-1>>>3&536870908)+4|0);c=H[b+28>>2]}else{c=0}H[a+28>>2]=c;c=H[b+40>>2];if(c){d=H[a+36>>2];if(H[a+44>>2]<<5>>>0>>0){if(d){oa(d);H[a+44>>2]=0;H[a+36>>2]=0;H[a+40>>2]=0;c=H[b+40>>2]}if((c|0)<0){break a}c=(c-1>>>5|0)+1|0;d=pa(c<<2);H[a+44>>2]=c;H[a+40>>2]=0;H[a+36>>2]=d;c=H[b+40>>2]}va(d,H[b+36>>2],(c-1>>>3&536870908)+4|0);b=H[b+40>>2]}else{b=0}H[a+40>>2]=b}return}sa();v()}sa();v()}function uc(a){var b=0,c=0,d=0;b=H[a+8>>2];d=H[a>>2];a:{if(I[a+12|0]){b:{c:{d:{e:{if((b|0)==-1){break e}c=b+1|0;b=(c>>>0)%3|0?c:b-2|0;if((b|0)==-1){break e}b=H[H[d+12>>2]+(b<<2)>>2];if((b|0)!=-1){break d}}H[a+8>>2]=-1;break c}c=b+1|0;b=(c>>>0)%3|0?c:b-2|0;H[a+8>>2]=b;if((b|0)!=-1){break b}}c=H[a+4>>2];b=-1;f:{if((c|0)==-1){break f}g:{if((c>>>0)%3|0){c=c-1|0;break g}c=c+2|0;b=-1;if((c|0)==-1){break f}}c=H[H[d+12>>2]+(c<<2)>>2];b=-1;if((c|0)==-1){break f}b=c-1|0;if((c>>>0)%3|0){break f}b=c+2|0}F[a+12|0]=0;H[a+8>>2]=b;return}if((b|0)!=H[a+4>>2]){break a}H[a+8>>2]=-1;return}c=-1;h:{if((b|0)==-1){break h}i:{if((b>>>0)%3|0){b=b-1|0;break i}b=b+2|0;c=-1;if((b|0)==-1){break h}}b=H[H[d+12>>2]+(b<<2)>>2];c=-1;if((b|0)==-1){break h}c=b-1|0;if((b>>>0)%3|0){break h}c=b+2|0}H[a+8>>2]=c}}function Rf(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0,h=0,i=0,j=0;f=ca-32|0;ca=f;d=H[a+28>>2];H[f+16>>2]=d;g=H[a+20>>2];H[f+28>>2]=c;H[f+24>>2]=b;b=g-d|0;H[f+20>>2]=b;g=b+c|0;i=2;a:{b:{b=f+16|0;d=Z(H[a+60>>2],b|0,2,f+12|0)|0;if(d){H[3992]=d;d=-1}else{d=0}c:{d:{if(d){d=b;break d}while(1){e=H[f+12>>2];if((e|0)==(g|0)){break c}if((e|0)<0){d=b;break b}h=H[b+4>>2];j=h>>>0>>0;d=(j<<3)+b|0;h=e-(j?h:0)|0;H[d>>2]=h+H[d>>2];b=(j?12:4)+b|0;H[b>>2]=H[b>>2]-h;g=g-e|0;b=d;i=i-j|0;e=Z(H[a+60>>2],b|0,i|0,f+12|0)|0;if(e){H[3992]=e;e=-1}else{e=0}if(!e){continue}break}}if((g|0)!=-1){break b}}b=H[a+44>>2];H[a+28>>2]=b;H[a+20>>2]=b;H[a+16>>2]=b+H[a+48>>2];a=c;break a}H[a+28>>2]=0;H[a+16>>2]=0;H[a+20>>2]=0;H[a>>2]=H[a>>2]|32;a=0;if((i|0)==2){break a}a=c-H[d+4>>2]|0}ca=f+32|0;return a|0}function Ih(a){a=a|0;var b=0,c=0,d=0,e=0,f=0,g=0;e=H[a+4>>2];d=H[e>>2];a:{b=H[a+12>>2];c=H[b+56>>2]-H[b+52>>2]|0;f=c>>2;b:{if(f>>>0<=H[e+8>>2]-d>>2>>>0){break b}if((c|0)<0){break a}b=H[e+4>>2];c=pa(c);f=c+(f<<2)|0;g=c+(b-d&-4)|0;c=g;if((b|0)!=(d|0)){while(1){c=c-4|0;b=b-4|0;H[c>>2]=H[b>>2];if((b|0)!=(d|0)){continue}break}}H[e+8>>2]=f;H[e+4>>2]=g;H[e>>2]=c;if(!d){break b}oa(d)}e=a+8|0;b=H[a+76>>2];c:{if(b){d=H[b>>2];if((d|0)==H[b+4>>2]){return 1}b=0;while(1){c=we(e,H[(b<<2)+d>>2]);if(!c){break c}f=H[a+76>>2];d=H[f>>2];b=b+1|0;if(b>>>0>2]-d>>2>>>0){continue}break}break c}c=1;a=H[H[a+12>>2]+64>>2];a=H[a+4>>2]-H[a>>2]|0;if(a>>>0<12){break c}a=(a>>2>>>0)/3|0;b=0;while(1){c=we(e,N(b,3));if(!c){break c}b=b+1|0;if((a|0)!=(b|0)){continue}break}}return c|0}sa();v()}function Oh(a){a=a|0;var b=0,c=0,d=0,e=0,f=0,g=0;e=H[a+4>>2];d=H[e>>2];a:{b=H[a+12>>2];c=H[b+28>>2]-H[b+24>>2]|0;f=c>>2;b:{if(f>>>0<=H[e+8>>2]-d>>2>>>0){break b}if((c|0)<0){break a}b=H[e+4>>2];c=pa(c);f=c+(f<<2)|0;g=c+(b-d&-4)|0;c=g;if((b|0)!=(d|0)){while(1){c=c-4|0;b=b-4|0;H[c>>2]=H[b>>2];if((b|0)!=(d|0)){continue}break}}H[e+8>>2]=f;H[e+4>>2]=g;H[e>>2]=c;if(!d){break b}oa(d)}e=a+8|0;b=H[a+76>>2];c:{if(b){d=H[b>>2];if((d|0)==H[b+4>>2]){return 1}b=0;while(1){c=xe(e,H[(b<<2)+d>>2]);if(!c){break c}f=H[a+76>>2];d=H[f>>2];b=b+1|0;if(b>>>0>2]-d>>2>>>0){continue}break}break c}c=1;a=H[a+12>>2];a=H[a+4>>2]-H[a>>2]|0;if(a>>>0<12){break c}a=(a>>2>>>0)/3|0;b=0;while(1){c=xe(e,N(b,3));if(!c){break c}b=b+1|0;if((a|0)!=(b|0)){continue}break}}return c|0}sa();v()}function Te(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0;g=H[b+8>>2];h=H[b+12>>2];c=H[b+20>>2];i=c;e=H[b+16>>2];d=e+4|0;c=d>>>0<4?c+1|0:c;a:{if(d>>>0>g>>>0&(c|0)>=(h|0)|(c|0)>(h|0)){break a}j=H[b>>2];f=e+j|0;f=I[f|0]|I[f+1|0]<<8|(I[f+2|0]<<16|I[f+3|0]<<24);H[b+16>>2]=d;H[b+20>>2]=c;k=J[b+38>>1];if(k>>>0<=513){c=i;d=e+8|0;c=d>>>0<8?c+1|0:c;if(d>>>0>g>>>0&(c|0)>=(h|0)|(c|0)>(h|0)){break a}H[b+16>>2]=d;H[b+20>>2]=c}if(!(f&1)){break a}e=Q(f)^31;if(e-1>>>0>28){break a}H[a+8>>2]=e+1;i=-2<>2]=e;H[a+12>>2]=i^-1;H[a+24>>2]=e>>1;L[a+20>>2]=O(2)/O(e|0);if(k>>>0<=513){if((c|0)>=(h|0)&d>>>0>=g>>>0|(c|0)>(h|0)){break a}g=I[d+j|0];d=d+1|0;c=d?c:c+1|0;H[b+16>>2]=d;H[b+20>>2]=c;if(g>>>0>1){break a}H[a+72>>2]=g}l=ta(a+96|0,b)}return l|0}function Se(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0;f=H[b+8>>2];g=H[b+12>>2];c=H[b+20>>2];h=c;i=H[b+16>>2];e=i+4|0;c=e>>>0<4?c+1|0:c;a:{if(e>>>0>f>>>0&(c|0)>=(g|0)|(c|0)>(g|0)){break a}j=H[b>>2];d=i+j|0;d=I[d|0]|I[d+1|0]<<8|(I[d+2|0]<<16|I[d+3|0]<<24);H[b+16>>2]=e;H[b+20>>2]=c;c=h;e=i+8|0;c=e>>>0<8?c+1|0:c;if(e>>>0>f>>>0&(c|0)>=(g|0)|(c|0)>(g|0)){break a}H[b+16>>2]=e;H[b+20>>2]=c;if(!(d&1)){break a}d=Q(d)^31;if(d-1>>>0>28){break a}H[a+8>>2]=d+1;k=-2<>2]=d;H[a+12>>2]=k^-1;H[a+24>>2]=d>>1;L[a+20>>2]=O(2)/O(d|0);if(J[b+38>>1]<=513){if((c|0)>=(g|0)&e>>>0>=f>>>0|(c|0)>(g|0)){break a}c=I[e+j|0];f=i+9|0;h=f>>>0<9?h+1|0:h;H[b+16>>2]=f;H[b+20>>2]=h;if(c>>>0>1){break a}H[a+72>>2]=c}l=ta(a+96|0,b)}return l|0} +function va(a,b,c){var d=0,e=0;a:{if((a|0)==(b|0)){break a}e=a+c|0;if(b-e>>>0<=0-(c<<1)>>>0){return qa(a,b,c)}d=(a^b)&3;b:{c:{if(a>>>0>>0){if(d){d=a;break b}if(!(a&3)){d=a;break c}d=a;while(1){if(!c){break a}F[d|0]=I[b|0];b=b+1|0;c=c-1|0;d=d+1|0;if(d&3){continue}break}break c}d:{if(d){break d}if(e&3){while(1){if(!c){break a}c=c-1|0;d=c+a|0;F[d|0]=I[b+c|0];if(d&3){continue}break}}if(c>>>0<=3){break d}while(1){c=c-4|0;H[c+a>>2]=H[b+c>>2];if(c>>>0>3){continue}break}}if(!c){break a}while(1){c=c-1|0;F[c+a|0]=I[b+c|0];if(c){continue}break}break a}if(c>>>0<=3){break b}while(1){H[d>>2]=H[b>>2];b=b+4|0;d=d+4|0;c=c-4|0;if(c>>>0>3){continue}break}}if(!c){break a}while(1){F[d|0]=I[b|0];d=d+1|0;b=b+1|0;c=c-1|0;if(c){continue}break}}return a}function ff(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0;h=H[c+12>>2];f=h;e=H[c+20>>2];i=H[c+8>>2];g=H[c+16>>2];a:{if((f|0)<=(e|0)&i>>>0<=g>>>0|(e|0)>(f|0)){break a}j=H[c>>2];k=F[j+g|0];d=e;f=g+1|0;d=f?d:d+1|0;H[c+16>>2]=f;H[c+20>>2]=d;b:{if((k|0)==-2){break b}if((d|0)>=(h|0)&f>>>0>=i>>>0|(d|0)>(h|0)){break a}d=F[f+j|0];g=g+2|0;e=g>>>0<2?e+1|0:e;H[c+16>>2]=g;H[c+20>>2]=e;if((d-4&255)>>>0<251){break a}e=ea[H[H[a>>2]+40>>2]](a,k,d)|0;d=H[a+20>>2];H[a+20>>2]=e;if(!d){break b}ea[H[H[d>>2]+4>>2]](d)}d=H[a+20>>2];if(d){if(!(ea[H[H[a>>2]+28>>2]](a,d)|0)){break a}}if(!(ea[H[H[a>>2]+36>>2]](a,b,c)|0)){break a}c=H[a+4>>2];if(!(!c|I[c+36|0]>1)){if(!(ea[H[H[a>>2]+48>>2]](a,H[b+4>>2]-H[b>>2]>>2)|0)){break a}}l=1}return l|0}function Vb(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0;d=H[a+8>>2];c=H[a+4>>2];if(d-c>>2>>>0>=b>>>0){if(b){b=b<<2;c=ra(c,0,b)+b|0}H[a+4>>2]=c;return}a:{b:{c:{g=H[a>>2];f=c-g>>2;e=f+b|0;if(e>>>0<1073741824){d=d-g|0;h=d>>>1|0;e=d>>>0>=2147483644?1073741823:e>>>0>>0?h:e;if(e){if(e>>>0>=1073741824){break c}i=pa(e<<2)}d=(f<<2)+i|0;f=b<<2;b=ra(d,0,f);f=b+f|0;e=(e<<2)+i|0;if((c|0)==(g|0)){break b}while(1){c=c-4|0;b=H[c>>2];H[c>>2]=0;d=d-4|0;H[d>>2]=b;if((c|0)!=(g|0)){continue}break}H[a+8>>2]=e;b=H[a+4>>2];H[a+4>>2]=f;c=H[a>>2];H[a>>2]=d;if((b|0)==(c|0)){break a}while(1){b=b-4|0;a=H[b>>2];H[b>>2]=0;if(a){ea[H[H[a>>2]+4>>2]](a)}if((b|0)!=(c|0)){continue}break}break a}sa();v()}wa();v()}H[a+8>>2]=e;H[a+4>>2]=f;H[a>>2]=b}if(c){oa(c)}}function Md(a,b,c){a:{switch(b-9|0){case 0:b=H[c>>2];H[c>>2]=b+4;H[a>>2]=H[b>>2];return;case 6:b=H[c>>2];H[c>>2]=b+4;b=G[b>>1];H[a>>2]=b;H[a+4>>2]=b>>31;return;case 7:b=H[c>>2];H[c>>2]=b+4;H[a>>2]=J[b>>1];H[a+4>>2]=0;return;case 8:b=H[c>>2];H[c>>2]=b+4;b=F[b|0];H[a>>2]=b;H[a+4>>2]=b>>31;return;case 9:b=H[c>>2];H[c>>2]=b+4;H[a>>2]=I[b|0];H[a+4>>2]=0;return;case 16:b=H[c>>2]+7&-8;H[c>>2]=b+8;M[a>>3]=M[b>>3];return;case 17:v();default:return;case 1:case 4:case 14:b=H[c>>2];H[c>>2]=b+4;b=H[b>>2];H[a>>2]=b;H[a+4>>2]=b>>31;return;case 2:case 5:case 11:case 15:b=H[c>>2];H[c>>2]=b+4;H[a>>2]=H[b>>2];H[a+4>>2]=0;return;case 3:case 10:case 12:case 13:break a}}b=H[c>>2]+7&-8;H[c>>2]=b+8;c=H[b+4>>2];H[a>>2]=H[b>>2];H[a+4>>2]=c}function Ed(a,b){var c=0,d=0,e=0;c=ca+-64|0;ca=c;d=H[a>>2];e=H[d-4>>2];d=H[d-8>>2];H[c+32>>2]=0;H[c+36>>2]=0;H[c+40>>2]=0;H[c+44>>2]=0;H[c+48>>2]=0;H[c+52>>2]=0;F[c+55|0]=0;F[c+56|0]=0;F[c+57|0]=0;F[c+58|0]=0;F[c+59|0]=0;F[c+60|0]=0;F[c+61|0]=0;F[c+62|0]=0;H[c+24>>2]=0;H[c+28>>2]=0;H[c+20>>2]=0;H[c+16>>2]=14924;H[c+12>>2]=a;H[c+8>>2]=b;a=a+d|0;d=0;a:{if(Ya(e,b,0)){H[c+56>>2]=1;ea[H[H[e>>2]+20>>2]](e,c+8|0,a,a,1,0);d=H[c+32>>2]==1?a:0;break a}ea[H[H[e>>2]+24>>2]](e,c+8|0,a,1,0);b:{switch(H[c+44>>2]){case 0:d=H[c+48>>2]==1?H[c+36>>2]==1?H[c+40>>2]==1?H[c+28>>2]:0:0:0;break a;case 1:break b;default:break a}}if(H[c+32>>2]!=1){if(H[c+48>>2]|H[c+36>>2]!=1|H[c+40>>2]!=1){break a}}d=H[c+24>>2]}ca=c- -64|0;return d}function ua(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0;H[a+16>>2]=0;e=H[a>>2];H[a+4>>2]=e;H[a+12>>2]=e;e=H[b+8>>2];c=H[b+12>>2];h=c;d=H[b+20>>2];f=H[b+16>>2];g=f+4|0;d=g>>>0<4?d+1|0:d;a:{if(e>>>0>>0&(d|0)>=(c|0)|(d|0)>(c|0)){break a}c=f+H[b>>2]|0;c=I[c|0]|I[c+1|0]<<8|(I[c+2|0]<<16|I[c+3|0]<<24);H[b+16>>2]=g;H[b+20>>2]=d;if(!c|c&3){break a}f=h-(d+(e>>>0>>0)|0)|0;if(e-g>>>0>>0&(f|0)<=0|(f|0)<0){break a}if(c>>>0>=4){ya(a,c>>>2|0);h=H[b+12>>2];g=H[b+16>>2];d=H[b+20>>2];e=H[b+8>>2]}f=c+g|0;d=f>>>0>>0?d+1|0:d;if(e>>>0>>0&(d|0)>=(h|0)|(d|0)>(h|0)){break a}qa(H[a>>2],H[b>>2]+g|0,c);d=H[b+20>>2];e=c+H[b+16>>2]|0;d=e>>>0>>0?d+1|0:d;H[b+16>>2]=e;H[b+20>>2]=d;H[a+16>>2]=0;H[a+12>>2]=H[a>>2];i=1}return i}function de(a,b){var c=0,d=0,e=0,f=0;d=-1;e=-1;f=-1;a:{b:{if((b|0)==-1){break b}e=H[H[H[a+4>>2]+12>>2]+(b<<2)>>2];c=b+1|0;c=(c>>>0)%3|0?c:b-2|0;if((c|0)>=0){f=(c>>>0)/3|0;f=H[(H[H[a>>2]+96>>2]+N(f,12)|0)+(c-N(f,3)<<2)>>2]}c:{if((e|0)==-1){break c}c=((e>>>0)%3|0?-1:2)+e|0;if((c|0)<0){break c}d=(c>>>0)/3|0;d=H[(H[H[a>>2]+96>>2]+N(d,12)|0)+(c-N(d,3)<<2)>>2]}c=-1;if((d|0)!=(f|0)){break a}f=-1;d:{b=((b>>>0)%3|0?-1:2)+b|0;if((b|0)>=0){d=(b>>>0)/3|0;d=H[(H[H[a>>2]+96>>2]+N(d,12)|0)+(b-N(d,3)<<2)>>2];if((e|0)==-1){break b}break d}d=-1;if((e|0)!=-1){break d}break b}b=e+1|0;b=(b>>>0)%3|0?b:e-2|0;if((b|0)<0){break b}c=H[H[a>>2]+96>>2];a=(b>>>0)/3|0;f=H[(c+N(a,12)|0)+(b-N(a,3)<<2)>>2]}c=(d|0)!=(f|0)?-1:e}return c}function Ah(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0;c=pa(72);H[c+4>>2]=0;H[c+8>>2]=0;H[c>>2]=1984;H[c+12>>2]=0;H[c+16>>2]=0;H[c+20>>2]=0;H[c+24>>2]=0;H[c+28>>2]=0;H[c+32>>2]=0;H[c+36>>2]=0;H[c+40>>2]=0;H[c>>2]=2128;H[c+44>>2]=0;H[c+48>>2]=0;H[c+52>>2]=0;H[c+56>>2]=0;H[c+60>>2]=0;H[c+64>>2]=0;H[c+68>>2]=0;h=c;a:{if((b|0)>=0){g=a+8|0;c=H[a+12>>2];e=H[a+8>>2];f=c-e>>2;b:{if((f|0)>(b|0)){break b}d=b+1|0;if(b>>>0>=f>>>0){Vb(g,d-f|0);break b}if(d>>>0>=f>>>0){break b}e=(d<<2)+e|0;if((e|0)!=(c|0)){while(1){c=c-4|0;d=H[c>>2];H[c>>2]=0;if(d){ea[H[H[d>>2]+4>>2]](d)}if((c|0)!=(e|0)){continue}break}}H[a+12>>2]=e}a=H[g>>2]+(b<<2)|0;c=H[a>>2];H[a>>2]=h;if(!c){break a}}ea[H[H[c>>2]+4>>2]](c)}return(b^-1)>>>31|0}function ra(a,b,c){var d=0,e=0,f=0,g=0;a:{if(!c){break a}F[a|0]=b;d=a+c|0;F[d-1|0]=b;if(c>>>0<3){break a}F[a+2|0]=b;F[a+1|0]=b;F[d-3|0]=b;F[d-2|0]=b;if(c>>>0<7){break a}F[a+3|0]=b;F[d-4|0]=b;if(c>>>0<9){break a}d=0-a&3;e=d+a|0;b=N(b&255,16843009);H[e>>2]=b;d=c-d&-4;c=d+e|0;H[c-4>>2]=b;if(d>>>0<9){break a}H[e+8>>2]=b;H[e+4>>2]=b;H[c-8>>2]=b;H[c-12>>2]=b;if(d>>>0<25){break a}H[e+24>>2]=b;H[e+20>>2]=b;H[e+16>>2]=b;H[e+12>>2]=b;H[c-16>>2]=b;H[c-20>>2]=b;H[c-24>>2]=b;H[c-28>>2]=b;g=e&4|24;c=d-g|0;if(c>>>0<32){break a}d=Rj(b,0,1,1);f=da;b=e+g|0;while(1){H[b+24>>2]=d;H[b+28>>2]=f;H[b+16>>2]=d;H[b+20>>2]=f;H[b+8>>2]=d;H[b+12>>2]=f;H[b>>2]=d;H[b+4>>2]=f;b=b+32|0;c=c-32|0;if(c>>>0>31){continue}break}}return a}function Mj(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0;d=H[b+8>>2];e=H[b+12>>2];g=e;e=H[b+20>>2];k=e;h=H[b+16>>2];c=h+4|0;e=c>>>0<4?e+1|0:e;i=c;a:{if(c>>>0>d>>>0&(e|0)>=(g|0)|(e|0)>(g|0)){break a}j=H[b>>2];c=j+h|0;f=I[c|0]|I[c+1|0]<<8|(I[c+2|0]<<16|I[c+3|0]<<24);H[b+16>>2]=i;H[b+20>>2]=e;c=d;d=k;e=h+8|0;d=e>>>0<8?d+1|0:d;if(c>>>0>>0&(d|0)>=(g|0)|(d|0)>(g|0)){break a}c=i+j|0;c=I[c|0]|I[c+1|0]<<8|(I[c+2|0]<<16|I[c+3|0]<<24);H[b+16>>2]=e;H[b+20>>2]=d;if((c|0)<(f|0)){break a}H[a+16>>2]=c;H[a+12>>2]=f;d=(c>>31)-((f>>31)+(c>>>0>>0)|0)|0;b=c-f|0;if(!d&b>>>0>2147483646|d){break a}l=1;d=b+1|0;H[a+20>>2]=d;b=d>>>1|0;H[a+24>>2]=b;H[a+28>>2]=0-b;if(d&1){break a}H[a+24>>2]=b-1}return l|0}function sd(a,b,c){var d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0;e=a+16|0;d=H[e>>2];a:{if(!d){break a}f=H[b>>2];b=e;while(1){g=(f|0)>H[d+16>>2];b=g?b:d;d=H[(g?d+4|0:d)>>2];if(d){continue}break}if((b|0)==(e|0)|(f|0)>2]){break a}d=H[b+24>>2];if(!d){break a}f=b+20|0;b=I[c+11|0];e=b<<24>>24<0;g=e?H[c>>2]:c;b=e?H[c+4>>2]:b;while(1){e=I[d+27|0];h=e<<24>>24<0;e=h?H[d+20>>2]:e;j=e>>>0>>0;b:{c:{d:{e:{f:{g:{i=j?e:b;if(i){h=h?H[d+16>>2]:d+16|0;k=Fa(g,h,i);if(k){break g}if(b>>>0>=e>>>0){break f}break b}if(b>>>0>=e>>>0){break e}break b}if((k|0)<0){break b}}e=Fa(h,g,i);if(e){break d}}if(j){break c}return Tc(f,c)}if((e|0)<0){break c}return Tc(f,c)}d=d+4|0}d=H[d>>2];if(d){continue}break}}return Tc(a,c)}function be(a,b,c){var d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0;d=ca-16|0;ca=d;f=H[a+24>>2];k=H[a+28>>2];a:{if((f|0)!=(k|0)){while(1){H[d+8>>2]=0;H[d>>2]=0;H[d+4>>2]=0;a=$d(H[f>>2],b,d);g=I[d+11|0];h=g<<24>>24;i=3;b:{c:{d:{if(!a){break d}i=0;a=I[c+11|0];e=a<<24>>24;j=(h|0)<0?H[d+4>>2]:g;if((j|0)!=(((e|0)<0?H[c+4>>2]:a)|0)){break d}a=(e|0)<0?H[c>>2]:c;e=(h|0)<0;e:{if(!e){e=d;if(!h){break e}while(1){if(I[e|0]!=I[a|0]){break d}a=a+1|0;e=e+1|0;g=g-1|0;if(g){continue}break}break e}if(!j){break e}if(Fa(e?H[d>>2]:d,a,j)){break c}}l=H[f>>2];i=1}if((h|0)>=0){break b}}oa(H[d>>2])}f:{switch(i|0){case 0:case 3:break f;default:break a}}f=f+4|0;if((k|0)!=(f|0)){continue}break}}l=0}ca=d+16|0;return l}function Cb(a,b,c){var d=0,e=0,f=0,g=0,h=0,i=0;f=c-b|0;h=f>>2;d=H[a+8>>2];e=H[a>>2];if(h>>>0<=d-e>>2>>>0){d=H[a+4>>2];g=d-e|0;f=g+b|0;i=g>>2;g=i>>>0>>0?f:c;if((g|0)!=(b|0)){while(1){H[e>>2]=H[b>>2];e=e+4|0;b=b+4|0;if((g|0)!=(b|0)){continue}break}}if(h>>>0>i>>>0){if((c|0)!=(g|0)){while(1){H[d>>2]=H[f>>2];d=d+4|0;f=f+4|0;if((f|0)!=(c|0)){continue}break}}H[a+4>>2]=d;return}H[a+4>>2]=e;return}if(e){H[a+4>>2]=e;oa(e);H[a+8>>2]=0;H[a>>2]=0;H[a+4>>2]=0;d=0}a:{if((f|0)<0){break a}e=d>>>1|0;d=d>>>0>=2147483644?1073741823:e>>>0>h>>>0?e:h;if(d>>>0>=1073741824){break a}e=d<<2;d=pa(e);H[a>>2]=d;H[a+8>>2]=d+e;if((b|0)!=(c|0)){c=b;b=(f-4&-4)+4|0;d=qa(d,c,b)+b|0}H[a+4>>2]=d;return}sa();v()}function Oa(a,b,c){var d=0,e=0,f=0;e=ca-16|0;ca=e;H[a+4>>2]=0;a:{b:{if(!b){break b}f=H[a+8>>2];d=f<<5;c:{if(d>>>0>=b>>>0){H[a+4>>2]=b;break c}H[e+8>>2]=0;H[e>>2]=0;H[e+4>>2]=0;if((b|0)<0){break a}if(d>>>0<=1073741822){f=f<<6;d=b+31&-32;d=d>>>0>>0?f:d}else{d=2147483647}pb(e,d);f=H[a>>2];H[a>>2]=H[e>>2];H[e>>2]=f;d=H[a+4>>2];H[a+4>>2]=b;H[e+4>>2]=d;d=H[a+8>>2];H[a+8>>2]=H[e+8>>2];H[e+8>>2]=d;if(!f){break c}oa(f)}d=b>>>5|0;a=H[a>>2];if(I[c|0]){if(b>>>0>=32){ra(a,255,d<<2)}if((b&-32)==(b|0)){break b}a=a+(d<<2)|0;H[a>>2]=H[a>>2]|-1>>>32-(b&31);break b}if(b>>>0>=32){ra(a,0,d<<2)}if((b&-32)==(b|0)){break b}a=a+(d<<2)|0;H[a>>2]=H[a>>2]&(-1>>>32-(b&31)^-1)}ca=e+16|0;return}sa();v()}function Hg(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;var e=0,f=0,g=0;e=ca-32|0;ca=e;a:{b:{f=Ma(c);if(f>>>0<2147483632){c:{d:{if(f>>>0>=11){a=(f|15)+1|0;g=pa(a);H[e+24>>2]=a|-2147483648;H[e+16>>2]=g;H[e+20>>2]=f;a=f+g|0;break d}F[e+27|0]=f;g=e+16|0;a=f+g|0;if(!f){break c}}qa(g,c,f)}F[a|0]=0;c=Ma(d);if(c>>>0>=2147483632){break b}e:{f:{if(c>>>0>=11){f=(c|15)+1|0;a=pa(f);H[e+8>>2]=f|-2147483648;H[e>>2]=a;H[e+4>>2]=c;g=a+c|0;break f}F[e+11|0]=c;g=c+e|0;a=e;if(!c){break e}}qa(a,d,c)}F[g|0]=0;c=H[b+4>>2];a=-1;g:{if(!c){break g}c=be(c,e+16|0,e);a=-1;if(!c){break g}a=Yd(b,H[c+24>>2])}if(F[e+11|0]<0){oa(H[e>>2])}if(F[e+27|0]<0){oa(H[e+16>>2])}ca=e+32|0;break a}Na();v()}Na();v()}return a|0}function jb(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0;b=H[b>>2];h=H[b+8>>2];i=H[b+4>>2];j=H[b>>2];d=H[a>>2];b=H[d+4>>2];a=H[d+8>>2];if(b>>>0>>0){H[b+8>>2]=h;H[b+4>>2]=i;H[b>>2]=j;H[d+4>>2]=b+12;return}a:{e=H[d>>2];g=(b-e|0)/12|0;c=g+1|0;if(c>>>0<357913942){f=(a-e|0)/12|0;a=f<<1;c=f>>>0>=178956970?357913941:a>>>0>c>>>0?a:c;if(c){if(c>>>0>=357913942){break a}f=pa(N(c,12))}else{f=0}a=f+N(g,12)|0;H[a+8>>2]=h;H[a+4>>2]=i;H[a>>2]=j;g=a+12|0;if((b|0)!=(e|0)){while(1){a=a-12|0;b=b-12|0;H[a>>2]=H[b>>2];H[a+4>>2]=H[b+4>>2];H[a+8>>2]=H[b+8>>2];if((b|0)!=(e|0)){continue}break}}H[d+8>>2]=f+N(c,12);H[d+4>>2]=g;H[d>>2]=a;if(e){oa(e)}return}sa();v()}wa();v()}function lf(a,b){a=a|0;b=b|0;a=0;a:{switch(b|0){case 0:a=pa(20);H[a+12>>2]=-1;H[a+16>>2]=0;H[a+4>>2]=0;H[a+8>>2]=0;H[a>>2]=2232;return a|0;case 1:a=pa(24);H[a+12>>2]=-1;H[a+16>>2]=0;H[a+4>>2]=0;H[a+8>>2]=0;H[a>>2]=2232;H[a+20>>2]=0;H[a>>2]=2448;return a|0;case 2:a=pa(48);H[a+12>>2]=-1;H[a+16>>2]=0;H[a+4>>2]=0;H[a+8>>2]=0;H[a>>2]=2232;H[a+20>>2]=0;H[a>>2]=2448;H[a+24>>2]=1832;H[a>>2]=11048;H[a+32>>2]=0;H[a+36>>2]=0;H[a+28>>2]=-1;H[a+40>>2]=0;H[a+44>>2]=0;return a|0;case 3:a=pa(32);H[a+12>>2]=-1;H[a+16>>2]=0;H[a+4>>2]=0;H[a+8>>2]=0;H[a>>2]=2232;H[a+20>>2]=0;H[a>>2]=2448;H[a+24>>2]=1032;H[a>>2]=7028;H[a+28>>2]=-1;break;default:break a}}return a|0}function tf(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0;f=H[b>>2];b=H[b+4>>2];d=H[H[a+8>>2]+40>>2];j=d;m=pa((d|0)<0?-1:d);i=b-f|0;e=1;a:{if((i|0)<4){break a}b=0;g=H[c+16>>2];k=d;f=g+d|0;d=0+H[c+20>>2]|0;d=f>>>0>>0?d+1|0:d;h=H[c+12>>2];e=0;if(K[c+8>>2]>>0&(d|0)>=(h|0)|(d|0)>(h|0)){break a}e=i>>2;i=(e|0)<=1?1:e;while(1){b:{g=qa(m,H[c>>2]+g|0,j);H[c+16>>2]=f;H[c+20>>2]=d;qa(H[H[H[a+8>>2]+64>>2]>>2]+b|0,g,j);l=l+1|0;if((i|0)==(l|0)){break b}b=b+j|0;d=n+H[c+20>>2]|0;g=H[c+16>>2];f=k+g|0;d=f>>>0>>0?d+1|0:d;h=H[c+12>>2];if((d|0)<=(h|0)&K[c+8>>2]>=f>>>0|(d|0)<(h|0)){continue}}break}e=(e|0)<=(l|0)}oa(m);return e|0}function Ti(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0;H[b>>2]=1;f=b+8|0;c=H[b+8>>2];d=H[b+12>>2]-c|0;if(d>>>0<=4294967291){kc(f,d+4|0);c=H[f>>2]}c=c+d|0;d=H[a+4>>2];F[c|0]=d;F[c+1|0]=d>>>8;F[c+2|0]=d>>>16;F[c+3|0]=d>>>24;c=H[a+8>>2];if((c|0)!=H[a+12>>2]){d=0;while(1){g=(d<<2)+c|0;c=H[b+8>>2];e=H[b+12>>2]-c|0;if(e>>>0<=4294967291){kc(f,e+4|0);c=H[f>>2]}c=c+e|0;e=H[g>>2];F[c|0]=e;F[c+1|0]=e>>>8;F[c+2|0]=e>>>16;F[c+3|0]=e>>>24;d=d+1|0;c=H[a+8>>2];if(d>>>0>2]-c>>2>>>0){continue}break}}c=H[b+12>>2];b=H[b+8>>2];c=c-b|0;if(c>>>0<=4294967291){kc(f,c+4|0);b=H[f>>2]}b=b+c|0;a=H[a+20>>2];F[b|0]=a;F[b+1|0]=a>>>8;F[b+2|0]=a>>>16;F[b+3|0]=a>>>24}function Aa(a,b,c){var d=0,e=0,f=0,g=0,h=0,i=0;f=c-b|0;g=f>>2;d=H[a+8>>2];e=H[a>>2];if(g>>>0<=d-e>>2>>>0){f=H[a+4>>2]-e|0;d=f+b|0;h=f>>2;f=h>>>0>>0?d:c;i=f-b|0;if((b|0)!=(f|0)){va(e,b,i)}if(g>>>0>h>>>0){b=H[a+4>>2];if((c|0)!=(f|0)){while(1){H[b>>2]=H[d>>2];b=b+4|0;d=d+4|0;if((d|0)!=(c|0)){continue}break}}H[a+4>>2]=b;return}H[a+4>>2]=e+i;return}if(e){H[a+4>>2]=e;oa(e);H[a+8>>2]=0;H[a>>2]=0;H[a+4>>2]=0;d=0}a:{if((f|0)<0){break a}e=d>>>1|0;d=d>>>0>=2147483644?1073741823:e>>>0>g>>>0?e:g;if(d>>>0>=1073741824){break a}e=d<<2;d=pa(e);H[a>>2]=d;H[a+8>>2]=d+e;if((b|0)!=(c|0)){c=b;b=(f-4&-4)+4|0;d=qa(d,c,b)+b|0}H[a+4>>2]=d;return}sa();v()}function Rb(a,b){var c=0,d=0,e=0,f=0,g=0,h=0;c=H[a+4>>2];if((c|0)!=H[a+8>>2]){e=H[b+4>>2];H[c>>2]=H[b>>2];H[c+4>>2]=e;H[c+8>>2]=H[b+8>>2];H[a+4>>2]=c+12;return}a:{g=H[a>>2];d=(c-g|0)/12|0;e=d+1|0;if(e>>>0<357913942){f=d<<1;f=d>>>0>=178956970?357913941:e>>>0>>0?f:e;if(f){if(f>>>0>=357913942){break a}e=pa(N(f,12))}else{e=0}d=e+N(d,12)|0;h=H[b+4>>2];H[d>>2]=H[b>>2];H[d+4>>2]=h;H[d+8>>2]=H[b+8>>2];b=d+12|0;if((c|0)!=(g|0)){while(1){c=c-12|0;h=H[c+4>>2];d=d-12|0;H[d>>2]=H[c>>2];H[d+4>>2]=h;H[d+8>>2]=H[c+8>>2];if((c|0)!=(g|0)){continue}break}c=H[a>>2]}H[a+8>>2]=e+N(f,12);H[a+4>>2]=b;H[a>>2]=d;if(c){oa(c)}return}sa();v()}wa();v()}function Qi(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;var g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0;f=ca-32|0;ca=f;g=e>>>0>1073741823?-1:e<<2;l=ra(pa(g),0,g);g=l;i=H[g>>2];g=H[g+4>>2];k=H[b+4>>2];H[f+24>>2]=H[b>>2];H[f+28>>2]=k;H[f+8>>2]=i;H[f+12>>2]=g;i=a+8|0;rc(f+16|0,i,f+8|0,f+24|0);H[c>>2]=H[f+16>>2];H[c+4>>2]=H[f+20>>2];if((d|0)>(e|0)){k=0-e<<2;a=e;while(1){h=a<<2;g=h+c|0;j=g+k|0;m=H[j>>2];j=H[j+4>>2];h=b+h|0;n=H[h+4>>2];H[f+24>>2]=H[h>>2];H[f+28>>2]=n;H[f+8>>2]=m;H[f+12>>2]=j;rc(f+16|0,i,f+8|0,f+24|0);H[g>>2]=H[f+16>>2];H[g+4>>2]=H[f+20>>2];a=a+e|0;if((d|0)>(a|0)){continue}break}}oa(l);ca=f+32|0;return 1}function Hi(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;var g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0;f=ca-32|0;ca=f;h=e>>>0>1073741823?-1:e<<2;h=ra(pa(h),0,h);g=H[b>>2];i=H[b+4>>2];k=H[h+4>>2];H[f+16>>2]=H[h>>2];H[f+20>>2]=k;H[f+8>>2]=g;H[f+12>>2]=i;i=a+8|0;qc(f+24|0,i,f+16|0,f+8|0);H[c>>2]=H[f+24>>2];H[c+4>>2]=H[f+28>>2];if((d|0)>(e|0)){k=0-e<<2;a=e;while(1){g=a<<2;j=g+b|0;m=H[j>>2];j=H[j+4>>2];g=c+g|0;l=g+k|0;n=H[l+4>>2];H[f+16>>2]=H[l>>2];H[f+20>>2]=n;H[f+8>>2]=m;H[f+12>>2]=j;qc(f+24|0,i,f+16|0,f+8|0);H[g>>2]=H[f+24>>2];H[g+4>>2]=H[f+28>>2];a=a+e|0;if((d|0)>(a|0)){continue}break}}oa(h);ca=f+32|0;return 1}function Ag(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;var e=0,f=0,g=0,h=0,i=0,j=0;a:{if(K[b+80>>2]>65535){break a}a=H[b+100>>2];b=H[b+96>>2];e=(a-b|0)/12|0;f=N(e,6);g=(f|0)==(c|0);if((a|0)==(b|0)|(c|0)!=(f|0)){break a}g=1;c=e>>>0<=1?1:e;i=c&1;a=0;if(e>>>0>=2){j=c&-2;c=0;while(1){f=N(a,6);h=f+d|0;e=b+N(a,12)|0;G[h>>1]=H[e>>2];G[(f|2)+d>>1]=H[e+4>>2];G[h+4>>1]=H[e+8>>2];f=a|1;e=N(f,6)+d|0;f=b+N(f,12)|0;G[e>>1]=H[f>>2];G[e+2>>1]=H[f+4>>2];G[e+4>>1]=H[f+8>>2];a=a+2|0;c=c+2|0;if((j|0)!=(c|0)){continue}break}}if(!i){break a}c=N(a,6)+d|0;a=b+N(a,12)|0;G[c>>1]=H[a>>2];G[c+2>>1]=H[a+4>>2];G[c+4>>1]=H[a+8>>2]}return g|0}function Gd(a,b,c,d,e,f,g){var h=0,i=0,j=0;h=ca-16|0;ca=h;if((b^-1)+2147483631>>>0>=c>>>0){if(I[a+11|0]>>>7|0){i=H[a>>2]}else{i=a}if(b>>>0<1073741799){H[h+12>>2]=b<<1;H[h>>2]=b+c;c=ca-16|0;ca=c;ca=c+16|0;c=h+12|0;c=H[(K[h>>2]>2]?c:h)>>2];if(c>>>0>=11){j=c+16&-16;c=j-1|0;c=(c|0)==11?j:c}else{c=10}c=c+1|0}else{c=2147483631}Zb(h,c);c=H[h>>2];if(f){yb(c,g,f)}g=d-e|0;if((d|0)!=(e|0)){yb(c+f|0,e+i|0,g)}if((b|0)!=10){oa(i)}H[a>>2]=c;H[a+8>>2]=H[a+8>>2]&-2147483648|H[h+4>>2]&2147483647;H[a+8>>2]=H[a+8>>2]|-2147483648;b=a;a=f+g|0;H[b+4>>2]=a;F[h+12|0]=0;F[a+c|0]=I[h+12|0];ca=h+16|0;return}Na();v()}function Rg(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0;a=ca-32|0;ca=a;H[a+24>>2]=0;H[a+28>>2]=0;a:{d=Ma(c);if(d>>>0<2147483632){b:{c:{if(d>>>0>=11){e=(d|15)+1|0;f=pa(e);H[a+16>>2]=e|-2147483648;H[a+8>>2]=f;H[a+12>>2]=d;e=d+f|0;break c}F[a+19|0]=d;f=a+8|0;e=f+d|0;if(!d){break b}}qa(f,c,d)}F[e|0]=0;c=b+4|0;b=nb(b,a+8|0);d:{if((c|0)==(b|0)){break d}c=H[b+32>>2];b=H[b+28>>2];if((c-b|0)!=8){break d}c=I[b+4|0]|I[b+5|0]<<8|(I[b+6|0]<<16|I[b+7|0]<<24);H[a+24>>2]=I[b|0]|I[b+1|0]<<8|(I[b+2|0]<<16|I[b+3|0]<<24);H[a+28>>2]=c}g=M[a+24>>3];if(F[a+19|0]<0){oa(H[a+8>>2])}ca=a+32|0;break a}Na();v()}return+g}function uf(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0;f=1;a:{if((ea[H[H[b>>2]+20>>2]](b)|0)<=0){break a}while(1){f=0;c=Zd(H[H[a+4>>2]+4>>2],ea[H[H[b>>2]+24>>2]](b,g)|0);if((c|0)==-1){break a}e=H[a+4>>2];b:{if(I[e+36|0]<=1){if(ea[H[H[b>>2]+28>>2]](b,H[H[H[e+4>>2]+8>>2]+(c<<2)>>2])|0){break b}break a}d=0;c:{if((c|0)<0){break c}h=H[e+4>>2];if(H[h+12>>2]-H[h+8>>2]>>2<=(c|0)){break c}d=H[H[e+8>>2]+(H[H[e+20>>2]+(c<<2)>>2]<<2)>>2];d=ea[H[H[d>>2]+32>>2]](d,c)|0}if(!d){break a}if(!(ea[H[H[b>>2]+28>>2]](b,d)|0)){break a}}f=1;g=g+1|0;if((ea[H[H[b>>2]+20>>2]](b)|0)>(g|0)){continue}break}}return f|0}function tb(a,b,c){var d=0,e=0,f=0,g=0,h=0,i=0;H[a+8>>2]=0;H[a>>2]=0;H[a+4>>2]=0;a:{b:{if(b){if(b>>>0>=357913942){break b}b=N(b,12);d=pa(b);H[a+4>>2]=d;H[a>>2]=d;e=b+d|0;H[a+8>>2]=e;f=H[c+4>>2];g=H[c>>2];c:{if((f|0)==(g|0)){b=b-12|0;ra(d,0,(b-((b>>>0)%12|0)|0)+12|0);break c}h=f-g|0;if((h|0)<0){break a}i=h&-4;while(1){H[d+8>>2]=0;H[d>>2]=0;H[d+4>>2]=0;b=pa(h);H[d>>2]=b;H[d+8>>2]=b+i;c=g;while(1){H[b>>2]=H[c>>2];b=b+4|0;c=c+4|0;if((f|0)!=(c|0)){continue}break}H[d+4>>2]=b;d=d+12|0;if((e|0)!=(d|0)){continue}break}}H[a+4>>2]=e}return}sa();v()}H[d+8>>2]=0;H[d>>2]=0;H[d+4>>2]=0;sa();v()}function Vi(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0;c=H[b+8>>2];d=H[b+12>>2];g=d;d=H[b+20>>2];i=d;h=H[b+16>>2];f=h+4|0;d=f>>>0<4?d+1|0:d;a:{if(c>>>0>>0&(d|0)>=(g|0)|(d|0)>(g|0)){break a}e=h+H[b>>2]|0;e=I[e|0]|I[e+1|0]<<8|(I[e+2|0]<<16|I[e+3|0]<<24);H[b+16>>2]=f;H[b+20>>2]=d;if(J[b+38>>1]<=513){f=c;c=i;d=h+8|0;c=d>>>0<8?c+1|0:c;if(d>>>0>f>>>0&(c|0)>=(g|0)|(c|0)>(g|0)){break a}H[b+16>>2]=d;H[b+20>>2]=c}if(!(e&1)){break a}b=Q(e)^31;if(b-1>>>0>28){break a}j=1;H[a+8>>2]=b+1;b=-2<>2]=c;H[a+12>>2]=b^-1;H[a+24>>2]=c>>1;L[a+20>>2]=O(2)/O(c|0)}return j|0}function Lc(a,b,c){var d=0,e=0,f=0,g=0;a:{f=b>>>0<1431655766&(b|c)>=0;b:{if(!f){break b}b=N(b,3);Kc(a,b,13648);Kc(a+12|0,b,13652);d=H[a+24>>2];c:{if(H[a+32>>2]-d>>2>>>0>=c>>>0){break c}if(c>>>0>=1073741824){break a}b=H[a+28>>2];e=c<<2;c=pa(e);e=c+e|0;g=c+(b-d&-4)|0;c=g;if((b|0)!=(d|0)){while(1){c=c-4|0;b=b-4|0;H[c>>2]=H[b>>2];if((b|0)!=(d|0)){continue}break}}H[a+32>>2]=e;H[a+28>>2]=g;H[a+24>>2]=c;if(!d){break c}oa(d)}H[a+80>>2]=0;H[a+84>>2]=0;b=H[a+76>>2];H[a+76>>2]=0;if(b){oa(b)}H[a+68>>2]=0;H[a+72>>2]=0;b=a- -64|0;a=H[b>>2];H[b>>2]=0;if(!a){break b}oa(a)}return f}sa();v()}function Fe(a){var b=0,c=0,d=0,e=0,f=0;f=1;c=H[a+140>>2];a:{if((c|0)<=0){break a}b=c<<4;d=pa(c>>>0>268435455?-1:b|4);H[d>>2]=c;d=d+4|0;c=d+b|0;b=d;while(1){H[b>>2]=0;H[b+4>>2]=0;F[b+5|0]=0;F[b+6|0]=0;F[b+7|0]=0;F[b+8|0]=0;F[b+9|0]=0;F[b+10|0]=0;F[b+11|0]=0;F[b+12|0]=0;b=b+16|0;if((c|0)!=(b|0)){continue}break}e=H[a+136>>2];H[a+136>>2]=d;if(e){c=e-4|0;d=H[c>>2];if(d){b=(d<<4)+e|0;while(1){b=b-16|0;if((e|0)!=(b|0)){continue}break}}oa(c)}b=0;if(H[a+140>>2]<=0){break a}while(1){f=ta(H[a+136>>2]+(b<<4)|0,a);if(!f){break a}b=b+1|0;if((b|0)>2]){continue}break}}return f}function mb(a,b){var c=0,d=0,e=0,f=0,g=0;a:{if(H[a+64>>2]){break a}c=pa(32);H[c+16>>2]=0;H[c+20>>2]=0;H[c+8>>2]=0;H[c>>2]=0;H[c+4>>2]=0;H[c+24>>2]=0;H[c+28>>2]=0;d=H[a+64>>2];H[a+64>>2]=c;if(!d){break a}c=H[d>>2];if(c){H[d+4>>2]=c;oa(c)}oa(d)}d=H[a+64>>2];c=H[a+28>>2]-1|0;if(c>>>0<=10){c=H[(c<<2)+13584>>2]}else{c=-1}c=N(c,I[a+24|0]);f=c>>31;g=se(d,0,Rj(c,f,b,0),da);if(g){d=H[a+64>>2];H[a>>2]=d;e=H[d+20>>2];H[a+8>>2]=H[d+16>>2];H[a+12>>2]=e;e=H[d+24>>2];d=H[d+28>>2];H[a+48>>2]=0;H[a+52>>2]=0;H[a+40>>2]=c;H[a+44>>2]=f;H[a+16>>2]=e;H[a+20>>2]=d;H[a+80>>2]=b}return g}function jc(a,b){var c=0;c=H[b+4>>2];H[a>>2]=H[b>>2];H[a+4>>2]=c;c=H[b+60>>2];H[a+56>>2]=H[b+56>>2];H[a+60>>2]=c;c=H[b+52>>2];H[a+48>>2]=H[b+48>>2];H[a+52>>2]=c;c=H[b+44>>2];H[a+40>>2]=H[b+40>>2];H[a+44>>2]=c;c=H[b+36>>2];H[a+32>>2]=H[b+32>>2];H[a+36>>2]=c;c=H[b+28>>2];H[a+24>>2]=H[b+24>>2];H[a+28>>2]=c;c=H[b+20>>2];H[a+16>>2]=H[b+16>>2];H[a+20>>2]=c;c=H[b+12>>2];H[a+8>>2]=H[b+8>>2];H[a+12>>2]=c;H[a+88>>2]=0;H[a+64>>2]=0;H[a+68>>2]=0;H[a+72>>2]=0;H[a+76>>2]=0;F[a+77|0]=0;F[a+78|0]=0;F[a+79|0]=0;F[a+80|0]=0;F[a+81|0]=0;F[a+82|0]=0;F[a+83|0]=0;F[a+84|0]=0;return a}function zg(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;var e=0,f=0,g=0,h=0,i=0,j=0,k=0;a=H[b+100>>2];b=H[b+96>>2];h=a-b|0;a:{if((h|0)!=(c|0)|(a|0)==(b|0)){break a}g=(c|0)/12|0;e=g>>>0<=1?1:g;j=e&1;a=0;if(g>>>0>=2){k=e&-2;g=0;while(1){e=N(a,12);i=e+d|0;f=b+e|0;H[i>>2]=H[f>>2];H[(e|4)+d>>2]=H[f+4>>2];H[i+8>>2]=H[f+8>>2];f=N(a|1,12);e=f+d|0;f=b+f|0;H[e>>2]=H[f>>2];H[e+4>>2]=H[f+4>>2];H[e+8>>2]=H[f+8>>2];a=a+2|0;g=g+2|0;if((k|0)!=(g|0)){continue}break}}if(!j){break a}e=d;d=N(a,12);a=e+d|0;b=b+d|0;H[a>>2]=H[b>>2];H[a+4>>2]=H[b+4>>2];H[a+8>>2]=H[b+8>>2]}return(c|0)==(h|0)|0}function Mi(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0;c=H[b+8>>2];d=H[b+12>>2];g=d;d=H[b+20>>2];i=d;h=H[b+16>>2];f=h+4|0;d=f>>>0<4?d+1|0:d;a:{if(c>>>0>>0&(d|0)>=(g|0)|(d|0)>(g|0)){break a}e=h+H[b>>2]|0;e=I[e|0]|I[e+1|0]<<8|(I[e+2|0]<<16|I[e+3|0]<<24);H[b+16>>2]=f;H[b+20>>2]=d;f=c;c=i;d=h+8|0;c=d>>>0<8?c+1|0:c;if(d>>>0>f>>>0&(c|0)>=(g|0)|(c|0)>(g|0)){break a}H[b+16>>2]=d;H[b+20>>2]=c;if(!(e&1)){break a}b=Q(e)^31;if(b-1>>>0>28){break a}j=1;H[a+8>>2]=b+1;b=-2<>2]=c;H[a+12>>2]=b^-1;H[a+24>>2]=c>>1;L[a+20>>2]=O(2)/O(c|0)}return j|0}function nb(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0;f=a+4|0;a=H[a+4>>2];a:{b:{if(!a){break b}d=I[b+11|0];c=d<<24>>24<0;g=c?H[b>>2]:b;d=c?H[b+4>>2]:d;b=f;while(1){e=I[a+27|0];c=e<<24>>24<0;e=c?H[a+20>>2]:e;h=e>>>0>d>>>0;i=h?d:e;c:{if(i){c=Fa(c?H[a+16>>2]:a+16|0,g,i);if(c){break c}}c=d>>>0>e>>>0?-1:h}c=(c|0)<0;b=c?b:a;a=H[(c?a+4|0:a)>>2];if(a){continue}break}if((b|0)==(f|0)){break b}c=I[b+27|0];a=c<<24>>24<0;d:{c=a?H[b+20>>2]:c;e=c>>>0>>0?c:d;if(e){a=Fa(g,a?H[b+16>>2]:b+16|0,e);if(a){break d}}if(c>>>0>d>>>0){break b}break a}if((a|0)>=0){break a}}b=f}return b}function Jf(a,b,c,d,e){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;if(Ya(a,H[b+8>>2],e)){if(!(H[b+28>>2]==1|H[b+4>>2]!=(c|0))){H[b+28>>2]=d}return}a:{if(Ya(a,H[b>>2],e)){if(!(H[b+16>>2]!=(c|0)&H[b+20>>2]!=(c|0))){if((d|0)!=1){break a}H[b+32>>2]=1;return}H[b+32>>2]=d;b:{if(H[b+44>>2]==4){break b}G[b+52>>1]=0;a=H[a+8>>2];ea[H[H[a>>2]+20>>2]](a,b,c,c,1,e);if(I[b+53|0]){H[b+44>>2]=3;if(!I[b+52|0]){break b}break a}H[b+44>>2]=4}H[b+20>>2]=c;H[b+40>>2]=H[b+40>>2]+1;if(H[b+36>>2]!=1|H[b+24>>2]!=2){break a}F[b+54|0]=1;return}a=H[a+8>>2];ea[H[H[a>>2]+24>>2]](a,b,c,d,e)}}function Db(a,b,c){var d=0,e=0,f=0,g=0;a:{b:{if(!b){break b}if(J[a+38>>1]<=513){f=H[a+12>>2];d=H[a+20>>2];b=H[a+16>>2];g=b+8|0;d=g>>>0<8?d+1|0:d;e=0;if(K[a+8>>2]>>0&(d|0)>=(f|0)|(d|0)>(f|0)){break a}b=b+H[a>>2]|0;d=I[b+4|0]|I[b+5|0]<<8|(I[b+6|0]<<16|I[b+7|0]<<24);H[c>>2]=I[b|0]|I[b+1|0]<<8|(I[b+2|0]<<16|I[b+3|0]<<24);H[c+4>>2]=d;b=H[a+20>>2];c=H[a+16>>2]+8|0;b=c>>>0<8?b+1|0:b;H[a+16>>2]=c;H[a+20>>2]=b;break b}e=0;if(!re(1,c,a)){break a}}F[a+36|0]=1;H[a+32>>2]=0;b=H[a+16>>2];c=b+H[a>>2]|0;H[a+24>>2]=c;H[a+28>>2]=(H[a+8>>2]-b|0)+c;e=1}return e}function ve(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0,h=0;f=pa(64);c=pa(12);H[c+8>>2]=H[H[a+4>>2]+80>>2];H[c>>2]=13216;H[c+4>>2]=0;f=od(f,c);a:{b:{if((b|0)<0){c=f;break b}h=a+8|0;c=H[a+12>>2];e=H[a+8>>2];g=c-e>>2;c:{if((g|0)>(b|0)){break c}d=b+1|0;if(b>>>0>=g>>>0){Vb(h,d-g|0);break c}if(d>>>0>=g>>>0){break c}e=e+(d<<2)|0;if((e|0)!=(c|0)){while(1){c=c-4|0;d=H[c>>2];H[c>>2]=0;if(d){ea[H[H[d>>2]+4>>2]](d)}if((c|0)!=(e|0)){continue}break}}H[a+12>>2]=e}a=H[h>>2]+(b<<2)|0;c=H[a>>2];H[a>>2]=f;if(!c){break a}}ea[H[H[c>>2]+4>>2]](c)}return(b^-1)>>>31|0}function Qd(a,b){var c=0,d=0,e=0,f=0;d=ca-16|0;ca=d;H[d+12>>2]=b;c=ca-208|0;ca=c;H[c+204>>2]=b;b=c+160|0;ra(b,0,40);H[c+200>>2]=H[c+204>>2];a:{if((Od(0,a,c+200|0,c+80|0,b)|0)<0){break a}f=H[3941]>=0;b=H[3922];if(H[3940]<=0){H[3922]=b&-33}b:{c:{d:{if(!H[3934]){H[3934]=80;H[3929]=0;H[3926]=0;H[3927]=0;e=H[3933];H[3933]=c;break d}if(H[3926]){break c}}if(Sd(15688)){break b}}Od(15688,a,c+200|0,c+80|0,c+160|0)}if(e){ea[H[3931]](15688,0,0)|0;H[3934]=0;H[3933]=e;H[3929]=0;H[3926]=0;H[3927]=0}H[3922]=H[3922]|b&32;if(!f){break a}}ca=c+208|0;ca=d+16|0}function pf(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0;c=H[a+60>>2];a:{if(!c){break a}H[c+4>>2]=a+48;if(!(ea[H[H[c>>2]+12>>2]](c)|0)){break a}b:{c=ea[H[H[a>>2]+24>>2]](a)|0;if((c|0)<=0){break b}while(1){c:{f=H[(ea[H[H[a>>2]+28>>2]](a)|0)+4>>2];g=ea[H[H[a>>2]+20>>2]](a,d)|0;e=H[a+60>>2];if(!(ea[H[H[e>>2]+8>>2]](e,H[H[f+8>>2]+(g<<2)>>2])|0)){break c}d=d+1|0;if((c|0)!=(d|0)){continue}break b}break}return 0}d=0;if(!(ea[H[H[a>>2]+36>>2]](a,b)|0)){break a}if(!(ea[H[H[a>>2]+40>>2]](a,b)|0)){break a}d=ea[H[H[a>>2]+44>>2]](a)|0}return d|0}function id(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0;c=H[a+216>>2];if((c|0)!=H[a+220>>2]){while(1){a:{c=H[N(e,144)+c>>2];if((c|0)<0){break a}d=H[a+4>>2];f=H[d+8>>2];if((c|0)>=H[d+12>>2]-f>>2){break a}d=0;c=H[(c<<2)+f>>2];if((ea[H[H[c>>2]+24>>2]](c)|0)<=0){break a}while(1){if((ea[H[H[c>>2]+20>>2]](c,d)|0)!=(b|0)){d=d+1|0;if((ea[H[H[c>>2]+24>>2]](c)|0)>(d|0)){continue}break a}break}a=H[a+216>>2]+N(e,144)|0;return(I[a+100|0]?a+4|0:0)|0}e=e+1|0;c=H[a+216>>2];if(e>>>0<(H[a+220>>2]-c|0)/144>>>0){continue}break}}return 0}function xb(a){var b=0,c=0,d=0,e=0;c=H[a+132>>2];if(c){d=c;b=H[a+136>>2];if((c|0)!=(b|0)){while(1){d=b-12|0;e=H[d>>2];if(e){H[b-8>>2]=e;oa(e)}b=d;if((c|0)!=(b|0)){continue}break}d=H[a+132>>2]}H[a+136>>2]=c;oa(d)}c=H[a+120>>2];if(c){d=c;b=H[a+124>>2];if((c|0)!=(b|0)){while(1){d=b-12|0;e=H[d>>2];if(e){H[b-8>>2]=e;oa(e)}b=d;if((c|0)!=(b|0)){continue}break}d=H[a+120>>2]}H[a+124>>2]=c;oa(d)}b=H[a+108>>2];if(b){H[a+112>>2]=b;oa(b)}b=H[a+96>>2];if(b){H[a+100>>2]=b;oa(b)}Za(a+76|0);Za(a+56|0);Za(a+36|0);Za(a+16|0)}function rd(a){a=a|0;var b=0,c=0,d=0;H[a>>2]=2128;d=H[a+60>>2];if(d){b=d;c=H[a- -64>>2];if((b|0)!=(c|0)){while(1){c=c-4|0;b=H[c>>2];H[c>>2]=0;if(b){Ga(b)}if((c|0)!=(d|0)){continue}break}b=H[a+60>>2]}H[a+64>>2]=d;oa(b)}b=H[a+48>>2];if(b){H[a+52>>2]=b;oa(b)}d=H[a+36>>2];if(d){b=d;c=H[a+40>>2];if((b|0)!=(c|0)){while(1){c=c-24|0;ea[H[H[c>>2]>>2]](c)|0;if((c|0)!=(d|0)){continue}break}b=H[a+36>>2]}H[a+40>>2]=d;oa(b)}H[a>>2]=1984;b=H[a+16>>2];if(b){H[a+20>>2]=b;oa(b)}b=H[a+4>>2];if(b){H[a+8>>2]=b;oa(b)}return a|0}function ue(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0;c=H[a+8>>2];d=H[a+4>>2];if(c-d>>2>>>0>=b>>>0){if(b){b=b<<2;d=ra(d,0,b)+b|0}H[a+4>>2]=d;return}a:{f=H[a>>2];g=d-f>>2;e=g+b|0;if(e>>>0<1073741824){c=c-f|0;h=c>>>1|0;e=c>>>0>=2147483644?1073741823:e>>>0>>0?h:e;if(e){if(e>>>0>=1073741824){break a}i=pa(e<<2)}c=(g<<2)+i|0;b=b<<2;b=ra(c,0,b)+b|0;if((d|0)!=(f|0)){while(1){c=c-4|0;d=d-4|0;H[c>>2]=H[d>>2];if((d|0)!=(f|0)){continue}break}}H[a+8>>2]=(e<<2)+i;H[a+4>>2]=b;H[a>>2]=c;if(f){oa(f)}return}sa();v()}wa();v()}function rb(a){var b=0,c=0,d=0,e=0,f=0;d=H[a+8>>2];a:{if(I[d+84|0]){break a}b=H[a+16>>2];if(!b|!I[b+84|0]){break a}c=H[d+72>>2];e=H[d+68>>2];F[b+84|0]=0;c=c-e>>2;f=H[b+68>>2];e=H[b+72>>2]-f>>2;b:{if(c>>>0>e>>>0){qb(b+68|0,c-e|0,2316);d=H[a+8>>2];break b}if(c>>>0>=e>>>0){break b}H[b+72>>2]=f+(c<<2)}if(I[d+84|0]){break a}c=H[d+68>>2];if((c|0)==H[d+72>>2]){break a}e=H[H[a+16>>2]+68>>2];b=0;while(1){f=b<<2;H[f+e>>2]=H[c+f>>2];b=b+1|0;c=H[d+68>>2];if(b>>>0>2]-c>>2>>>0){continue}break}}return H[a+16>>2]}function Lg(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;var e=0,f=0,g=0;e=ca+-64|0;ca=e;f=Ha(e+8|0);H[f+16>>2]=0;H[f+20>>2]=0;H[f>>2]=b;H[f+8>>2]=c;H[f+12>>2]=0;b=e+48|0;Pe(b,a,f,d);H[a+24>>2]=H[e+48>>2];f=a+24|0;a:{if((f|0)==(b|0)){break a}b=a+28|0;c=e+48|4;g=I[e+63|0];d=g<<24>>24;if(F[a+39|0]>=0){if((d|0)>=0){a=H[c+4>>2];H[b>>2]=H[c>>2];H[b+4>>2]=a;H[b+8>>2]=H[c+8>>2];break a}Xb(b,H[e+52>>2],H[e+56>>2]);break a}a=(d|0)<0;Yb(b,a?H[e+52>>2]:c,a?H[e+56>>2]:g)}if(F[e+63|0]<0){oa(H[e+52>>2])}ca=e- -64|0;return f|0}function Kg(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;var e=0,f=0,g=0;e=ca+-64|0;ca=e;f=Ha(e+8|0);H[f+16>>2]=0;H[f+20>>2]=0;H[f>>2]=b;H[f+8>>2]=c;H[f+12>>2]=0;b=e+48|0;Oe(b,a,f,d);H[a+24>>2]=H[e+48>>2];f=a+24|0;a:{if((f|0)==(b|0)){break a}b=a+28|0;c=e+48|4;g=I[e+63|0];d=g<<24>>24;if(F[a+39|0]>=0){if((d|0)>=0){a=H[c+4>>2];H[b>>2]=H[c>>2];H[b+4>>2]=a;H[b+8>>2]=H[c+8>>2];break a}Xb(b,H[e+52>>2],H[e+56>>2]);break a}a=(d|0)<0;Yb(b,a?H[e+52>>2]:c,a?H[e+56>>2]:g)}if(F[e+63|0]<0){oa(H[e+52>>2])}ca=e- -64|0;return f|0}function Ig(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0;a=ca-32|0;ca=a;a:{d=Ma(c);if(d>>>0<2147483632){b:{c:{if(d>>>0>=11){e=(d|15)+1|0;f=pa(e);H[a+24>>2]=e|-2147483648;H[a+16>>2]=f;H[a+20>>2]=d;e=d+f|0;break c}F[a+27|0]=d;f=a+16|0;e=f+d|0;if(!d){break b}}qa(f,c,d)}F[e|0]=0;F[a+4|0]=0;H[a>>2]=1701667182;F[a+11|0]=4;d=H[b+4>>2];c=-1;d:{if(!d){break d}d=be(d,a,a+16|0);c=-1;if(!d){break d}c=Yd(b,H[d+24>>2])}b=c;if(F[a+11|0]<0){oa(H[a>>2])}if(F[a+27|0]<0){oa(H[a+16>>2])}ca=a+32|0;break a}Na();v()}return b|0}function hd(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0;c=H[a+216>>2];if((c|0)!=H[a+220>>2]){while(1){a:{c=H[N(e,144)+c>>2];if((c|0)<0){break a}d=H[a+4>>2];f=H[d+8>>2];if((c|0)>=H[d+12>>2]-f>>2){break a}d=0;c=H[(c<<2)+f>>2];if((ea[H[H[c>>2]+24>>2]](c)|0)<=0){break a}while(1){if((ea[H[H[c>>2]+20>>2]](c,d)|0)!=(b|0)){d=d+1|0;if((ea[H[H[c>>2]+24>>2]](c)|0)>(d|0)){continue}break a}break}return(H[a+216>>2]+N(e,144)|0)+104|0}e=e+1|0;c=H[a+216>>2];if(e>>>0<(H[a+220>>2]-c|0)/144>>>0){continue}break}}return a+184|0}function ab(a){var b=0,c=0,d=0,e=0;c=H[a+640>>2];if(c){d=c;b=H[a+644>>2];if((c|0)!=(b|0)){while(1){d=b-12|0;e=H[d>>2];if(e){H[b-8>>2]=e;oa(e)}b=d;if((c|0)!=(b|0)){continue}break}d=H[a+640>>2]}H[a+644>>2]=c;oa(d)}c=H[a+628>>2];if(c){d=c;b=H[a+632>>2];if((c|0)!=(b|0)){while(1){d=b-12|0;e=H[d>>2];if(e){H[b-8>>2]=e;oa(e)}b=d;if((c|0)!=(b|0)){continue}break}d=H[a+628>>2]}H[a+632>>2]=c;oa(d)}b=H[a+616>>2];if(b){H[a+620>>2]=b;oa(b)}b=H[a+604>>2];if(b){H[a+608>>2]=b;oa(b)}Za(a+584|0);Za(a+564|0);Za(a+544|0)}function Tg(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0;d=ca-16|0;ca=d;H[d+12>>2]=0;a:{e=Ma(c);if(e>>>0<2147483632){b:{c:{if(e>>>0>=11){f=(e|15)+1|0;a=pa(f);H[d+8>>2]=f|-2147483648;H[d>>2]=a;H[d+4>>2]=e;f=a+e|0;break c}F[d+11|0]=e;f=d+e|0;a=d;if(!e){break b}}qa(a,c,e)}F[f|0]=0;a=nb(b,d);d:{if((a|0)==(b+4|0)){break d}b=H[a+32>>2];a=H[a+28>>2];if((b-a|0)!=4){break d}H[d+12>>2]=I[a|0]|I[a+1|0]<<8|(I[a+2|0]<<16|I[a+3|0]<<24)}a=H[d+12>>2];if(F[d+11|0]<0){oa(H[d>>2])}ca=d+16|0;break a}Na();v()}return a|0}function vb(a){var b=0,c=0,d=0,e=0;c=H[a+128>>2];if(c){d=c;b=H[a+132>>2];if((c|0)!=(b|0)){while(1){d=b-12|0;e=H[d>>2];if(e){H[b-8>>2]=e;oa(e)}b=d;if((c|0)!=(b|0)){continue}break}d=H[a+128>>2]}H[a+132>>2]=c;oa(d)}c=H[a+116>>2];if(c){d=c;b=H[a+120>>2];if((c|0)!=(b|0)){while(1){d=b-12|0;e=H[d>>2];if(e){H[b-8>>2]=e;oa(e)}b=d;if((c|0)!=(b|0)){continue}break}d=H[a+116>>2]}H[a+120>>2]=c;oa(d)}b=H[a+104>>2];if(b){H[a+108>>2]=b;oa(b)}b=H[a+92>>2];if(b){H[a+96>>2]=b;oa(b)}Za(a+72|0);Za(a+52|0);Za(a+32|0)}function kc(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0;a:{c=H[a+4>>2];e=H[a>>2];d=c-e|0;b:{if(d>>>0>>0){g=b-d|0;f=H[a+8>>2];if(g>>>0<=f-c>>>0){h=a,i=ra(c,0,g)+g|0,H[h+4>>2]=i;break b}if((b|0)<0){break a}c=f-e|0;f=c<<1;c=c>>>0>=1073741823?2147483647:b>>>0>>0?f:b;f=pa(c);ra(f+d|0,0,g);d=va(f,e,d);H[a+8>>2]=d+c;H[a+4>>2]=b+d;H[a>>2]=d;if(!e){break b}oa(e);break b}if(b>>>0>=d>>>0){break b}H[a+4>>2]=b+e}b=H[a+28>>2];c=b;d=b+1|0;b=H[a+24>>2]+1|0;e=b?c:d;H[a+24>>2]=b;H[a+28>>2]=e;return}sa();v()}function Ka(a,b){var c=0,d=0,e=0,f=0,g=0,h=0;e=H[a+4>>2];if((e|0)!=H[a+8>>2]){H[e>>2]=H[b>>2];H[a+4>>2]=e+4;return}a:{g=H[a>>2];f=e-g|0;c=f>>2;d=c+1|0;if(d>>>0<1073741824){h=c<<2;c=f>>>1|0;c=f>>>0>=2147483644?1073741823:c>>>0>d>>>0?c:d;if(c){if(c>>>0>=1073741824){break a}f=pa(c<<2)}else{f=0}d=h+f|0;H[d>>2]=H[b>>2];b=d+4|0;if((e|0)!=(g|0)){while(1){d=d-4|0;e=e-4|0;H[d>>2]=H[e>>2];if((e|0)!=(g|0)){continue}break}}H[a+8>>2]=f+(c<<2);H[a+4>>2]=b;H[a>>2]=d;if(g){oa(g)}return}sa();v()}wa();v()}function Ia(a){H[a>>2]=-1;H[a+4>>2]=0;H[a+8>>2]=0;H[a+32>>2]=0;H[a+36>>2]=0;F[a+28|0]=1;H[a+20>>2]=0;H[a+24>>2]=0;H[a+12>>2]=0;H[a+16>>2]=0;H[a+40>>2]=0;H[a+44>>2]=0;H[a+48>>2]=0;H[a+52>>2]=0;H[a+56>>2]=0;H[a+60>>2]=0;H[a+64>>2]=0;H[a+68>>2]=0;H[a+76>>2]=0;H[a+80>>2]=0;H[a+84>>2]=0;H[a+88>>2]=0;H[a+92>>2]=0;H[a+96>>2]=0;H[a+72>>2]=a+4;H[a+104>>2]=0;H[a+108>>2]=0;F[a+100|0]=1;H[a+112>>2]=0;H[a+116>>2]=0;H[a+120>>2]=0;H[a+124>>2]=0;H[a+128>>2]=0;H[a+132>>2]=0;H[a+136>>2]=0;H[a+140>>2]=0}function Ld(a,b){if(!a){return 0}a:{b:{if(a){if(b>>>0<=127){break b}c:{if(!H[H[4292]>>2]){if((b&-128)==57216){break b}break c}if(b>>>0<=2047){F[a+1|0]=b&63|128;F[a|0]=b>>>6|192;a=2;break a}if(!((b&-8192)!=57344&b>>>0>=55296)){F[a+2|0]=b&63|128;F[a|0]=b>>>12|224;F[a+1|0]=b>>>6&63|128;a=3;break a}if(b-65536>>>0<=1048575){F[a+3|0]=b&63|128;F[a|0]=b>>>18|240;F[a+2|0]=b>>>6&63|128;F[a+1|0]=b>>>12&63|128;a=4;break a}}H[3992]=25;a=-1}else{a=1}break a}F[a|0]=b;a=1}return a}function Hb(a,b){var c=0,d=0,e=0,f=0;d=H[a+12>>2];c=H[a+16>>2]-d>>2;a:{if(c>>>0>>0){ya(a+12|0,b-c|0);break a}if(b>>>0>=c>>>0){break a}H[a+16>>2]=d+(b<<2)}b:{c=H[a>>2];c:{if(H[a+8>>2]-c>>2>>>0>=b>>>0){break c}if(b>>>0>=1073741824){break b}d=H[a+4>>2];e=b<<2;b=pa(e);e=b+e|0;f=b+(d-c&-4)|0;b=f;if((c|0)!=(d|0)){while(1){b=b-4|0;d=d-4|0;H[b>>2]=H[d>>2];if((c|0)!=(d|0)){continue}break}}H[a+8>>2]=e;H[a+4>>2]=f;H[a>>2]=b;if(!c){break c}oa(c)}return}sa();v()}function _b(a){a=a|0;var b=0,c=0,d=0;H[a>>2]=13724;b=H[a+68>>2];if(b){H[a+72>>2]=b;oa(b)}b=H[a+56>>2];if(b){H[a+60>>2]=b;oa(b)}b=H[a+44>>2];if(b){H[a+48>>2]=b;oa(b)}b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}b=H[a+20>>2];if(b){H[a+24>>2]=b;oa(b)}b=H[a+8>>2];if(b){d=b;c=H[a+12>>2];if((b|0)!=(c|0)){while(1){c=c-4|0;d=H[c>>2];H[c>>2]=0;if(d){Ga(d)}if((b|0)!=(c|0)){continue}break}d=H[a+8>>2]}H[a+12>>2]=b;oa(d)}b=H[a+4>>2];H[a+4>>2]=0;if(b){Uc(b)}return a|0}function yb(a,b,c){var d=0,e=0,f=0,g=0,h=0;f=ca-16|0;ca=f;d=ca-32|0;ca=d;e=ca-16|0;ca=e;H[e+12>>2]=b;H[e+8>>2]=b+c;H[d+24>>2]=H[e+12>>2];H[d+28>>2]=H[e+8>>2];ca=e+16|0;c=ca-16|0;ca=c;h=H[d+28>>2];e=H[d+24>>2];g=h-e|0;if((e|0)!=(h|0)){va(a,e,g)}H[c+12>>2]=e+g;H[c+8>>2]=a+g;H[d+16>>2]=H[c+12>>2];H[d+20>>2]=H[c+8>>2];ca=c+16|0;H[d+12>>2]=(H[d+16>>2]-b|0)+b;H[d+8>>2]=(H[d+20>>2]-a|0)+a;H[f+8>>2]=H[d+12>>2];H[f+12>>2]=H[d+8>>2];ca=d+32|0;ca=f+16|0}function ya(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0;e=H[a+8>>2];c=H[a+4>>2];if(e-c>>2>>>0>=b>>>0){if(b){b=b<<2;c=ra(c,0,b)+b|0}H[a+4>>2]=c;return}a:{f=c;c=H[a>>2];g=f-c|0;h=g>>2;d=h+b|0;if(d>>>0<1073741824){e=e-c|0;f=e>>>1|0;d=e>>>0>=2147483644?1073741823:d>>>0>>0?f:d;if(d){if(d>>>0>=1073741824){break a}i=pa(d<<2)}b=b<<2;e=ra((h<<2)+i|0,0,b);f=d<<2;d=va(i,c,g);H[a+8>>2]=f+d;H[a+4>>2]=b+e;H[a>>2]=d;if(c){oa(c)}return}sa();v()}wa();v()}function Tc(a,b){var c=0,d=0,e=0,f=0;c=a+4|0;a=nb(a,b);a:{if((c|0)==(a|0)){break a}b=a+28|0;b=F[a+39|0]<0?H[b>>2]:b;while(1){a=b;b=a+1|0;c=F[a|0];if((c|0)==32|c-9>>>0<5){continue}break}b:{c:{d:{c=F[a|0];switch(c-43|0){case 0:break c;case 2:break d;default:break b}}e=1}c=F[b|0];a=b}if(c-48>>>0<10){while(1){d=(N(d,10)-F[a|0]|0)+48|0;b=F[a+1|0];a=a+1|0;if(b-48>>>0<10){continue}break}}a=e?d:0-d|0;if((a|0)==-1){break a}f=(a|0)!=0}return f}function bb(a,b){var c=0,d=0,e=0,f=0,g=0,h=0;a=H[a>>2];c=H[a+4>>2];e=H[a+8>>2];if(c>>>0>>0){H[c>>2]=H[b>>2];H[a+4>>2]=c+4;return}a:{d=c;c=H[a>>2];g=d-c|0;d=g>>2;f=d+1|0;if(f>>>0<1073741824){h=d<<2;e=e-c|0;d=e>>>1|0;f=e>>>0>=2147483644?1073741823:f>>>0>>0?d:f;if(f){if(f>>>0>=1073741824){break a}e=pa(f<<2)}else{e=0}d=h+e|0;H[d>>2]=H[b>>2];b=va(e,c,g);H[a+8>>2]=b+(f<<2);H[a+4>>2]=d+4;H[a>>2]=b;if(c){oa(c)}return}sa();v()}wa();v()}function ob(a,b){var c=0,d=0,e=0,f=0,g=0,h=0,i=0;e=H[a+8>>2];c=H[a+4>>2];if(e-c>>3>>>0>=b>>>0){if(b){b=b<<3;c=ra(c,0,b)+b|0}H[a+4>>2]=c;return}a:{f=c;c=H[a>>2];g=f-c|0;h=g>>3;d=h+b|0;if(d>>>0<536870912){e=e-c|0;f=e>>>2|0;d=e>>>0>=2147483640?536870911:d>>>0>>0?f:d;if(d){if(d>>>0>=536870912){break a}i=pa(d<<3)}b=b<<3;e=ra((h<<3)+i|0,0,b);f=d<<3;d=va(i,c,g);H[a+8>>2]=f+d;H[a+4>>2]=b+e;H[a>>2]=d;if(c){oa(c)}return}sa();v()}wa();v()}function kf(a){a=a|0;var b=0,c=0,d=0;H[a>>2]=2328;b=H[a+60>>2];H[a+60>>2]=0;if(b){ea[H[H[b>>2]+4>>2]](b)}b=H[a+48>>2];if(b){H[a+52>>2]=b;oa(b)}d=H[a+36>>2];if(d){c=H[a+40>>2];b=d;if((c|0)!=(b|0)){while(1){c=c-4|0;b=H[c>>2];H[c>>2]=0;if(b){ea[H[H[b>>2]+4>>2]](b)}if((c|0)!=(d|0)){continue}break}b=H[a+36>>2]}H[a+40>>2]=d;oa(b)}H[a>>2]=1984;b=H[a+16>>2];if(b){H[a+20>>2]=b;oa(b)}b=H[a+4>>2];if(b){H[a+8>>2]=b;oa(b)}return a|0}function jf(a){a=a|0;var b=0,c=0,d=0;H[a>>2]=2328;b=H[a+60>>2];H[a+60>>2]=0;if(b){ea[H[H[b>>2]+4>>2]](b)}b=H[a+48>>2];if(b){H[a+52>>2]=b;oa(b)}d=H[a+36>>2];if(d){c=H[a+40>>2];b=d;if((c|0)!=(b|0)){while(1){c=c-4|0;b=H[c>>2];H[c>>2]=0;if(b){ea[H[H[b>>2]+4>>2]](b)}if((c|0)!=(d|0)){continue}break}b=H[a+36>>2]}H[a+40>>2]=d;oa(b)}H[a>>2]=1984;b=H[a+16>>2];if(b){H[a+20>>2]=b;oa(b)}b=H[a+4>>2];if(b){H[a+8>>2]=b;oa(b)}oa(a)}function xi(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0;d=ca-16|0;ca=d;e=H[a+4>>2];a:{if((e|0)==-1){break a}c=H[b+20>>2];if(!!H[b+16>>2]&(c|0)>=0|(c|0)>0){break a}Wb(b,H[b+4>>2],H[a+8>>2],H[a+12>>2]);c=H[b+20>>2];if(!!H[b+16>>2]&(c|0)>=0|(c|0)>0){break a}Wb(b,H[b+4>>2],a+20|0,a+24|0);c=H[b+20>>2];f=H[b+16>>2];F[d+15|0]=H[a+4>>2];if(!!f&(c|0)>=0|(c|0)>0){break a}Wb(b,H[b+4>>2],d+15|0,d+16|0)}ca=d+16|0;return(e|0)!=-1|0}function Eh(a){a=a|0;var b=0,c=0,d=0,e=0,f=0;a:{b=H[a+8>>2];b:{if((b|0)<0){break b}c=H[a+4>>2];e=H[c>>2];d=H[c+4>>2]-e>>2;c:{if(d>>>0>>0){ue(c,b-d|0);f=H[a+8>>2];break c}f=b;if(b>>>0>=d>>>0){break c}H[c+4>>2]=e+(b<<2);f=b}d=f;if((d|0)<=0){break b}a=H[a+4>>2];c=H[a>>2];e=H[a+4>>2]-c>>2;a=0;while(1){if((a|0)==(e|0)){break a}H[c+(a<<2)>>2]=a;a=a+1|0;if((d|0)!=(a|0)){continue}break}}return(b^-1)>>>31|0}Ca();v()}function qe(a,b){var c=0,d=0,e=0,f=0,g=0,h=0;e=H[a+8>>2];c=H[a+4>>2];if(e-c>>1>>>0>=b>>>0){if(b){b=b<<1;c=ra(c,0,b)+b|0}H[a+4>>2]=c;return}a:{f=c;c=H[a>>2];g=f-c|0;f=g>>1;d=f+b|0;if((d|0)>=0){e=e-c|0;d=e>>>0>=2147483646?2147483647:d>>>0>>0?e:d;if(d){if((d|0)<0){break a}h=pa(d<<1)}b=b<<1;e=ra((f<<1)+h|0,0,b);f=d<<1;d=va(h,c,g);H[a+8>>2]=f+d;H[a+4>>2]=b+e;H[a>>2]=d;if(c){oa(c)}return}sa();v()}wa();v()}function ng(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0;d=ca-16|0;ca=d;Pe(d,a,b,c);H[a+24>>2]=H[d>>2];e=a+24|0;a:{if((e|0)==(d|0)){break a}b=a+28|0;c=d|4;f=I[d+15|0];g=f<<24>>24;if(F[a+39|0]>=0){if((g|0)>=0){a=H[c+4>>2];H[b>>2]=H[c>>2];H[b+4>>2]=a;H[b+8>>2]=H[c+8>>2];break a}Xb(b,H[d+4>>2],H[d+8>>2]);break a}a=(g|0)<0;Yb(b,a?H[d+4>>2]:c,a?H[d+8>>2]:f)}if(F[d+15|0]<0){oa(H[d+4>>2])}ca=d+16|0;return e|0}function mg(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0;d=ca-16|0;ca=d;Oe(d,a,b,c);H[a+24>>2]=H[d>>2];e=a+24|0;a:{if((e|0)==(d|0)){break a}b=a+28|0;c=d|4;f=I[d+15|0];g=f<<24>>24;if(F[a+39|0]>=0){if((g|0)>=0){a=H[c+4>>2];H[b>>2]=H[c>>2];H[b+4>>2]=a;H[b+8>>2]=H[c+8>>2];break a}Xb(b,H[d+4>>2],H[d+8>>2]);break a}a=(g|0)<0;Yb(b,a?H[d+4>>2]:c,a?H[d+8>>2]:f)}if(F[d+15|0]<0){oa(H[d+4>>2])}ca=d+16|0;return e|0}function za(a,b,c){var d=0,e=0,f=0,g=0;e=ca-16|0;ca=e;a:{b:{if(c>>>0<11){d=a;F[a+11|0]=I[a+11|0]&128|c;F[a+11|0]=I[a+11|0]&127;break b}if(c>>>0>2147483631){break a}g=e+8|0;if(c>>>0>=11){f=c+16&-16;d=f-1|0;d=(d|0)==11?f:d}else{d=10}Zb(g,d+1|0);d=H[e+8>>2];H[a>>2]=d;H[a+8>>2]=H[a+8>>2]&-2147483648|H[e+12>>2]&2147483647;H[a+8>>2]=H[a+8>>2]|-2147483648;H[a+4>>2]=c}yb(d,b,c+1|0);ca=e+16|0;return}Na();v()}function Qg(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0;d=ca-16|0;ca=d;a:{e=Ma(c);if(e>>>0<2147483632){b:{c:{if(e>>>0>=11){g=(e|15)+1|0;f=pa(g);H[d+8>>2]=g|-2147483648;H[d>>2]=f;H[d+4>>2]=e;g=e+f|0;break c}F[d+11|0]=e;g=d+e|0;f=d;if(!e){break b}}qa(f,c,e)}F[g|0]=0;f=a+16|0;c=$d(b,d,f);b=H[a+16>>2];a=F[a+27|0];if(F[d+11|0]<0){oa(H[d>>2])}ca=d+16|0;a=c?(a|0)<0?b:f:0;break a}Na();v()}return a|0}function Mc(a,b){var c=0,d=0,e=0;c=H[a+4>>2];d=c+b|0;H[a+4>>2]=d;if(!((d-1^c-1)>>>0<32?c:0)){H[H[a>>2]+((d>>>0>=33?d-1>>>5|0:0)<<2)>>2]=0}a:{if(!b){break a}a=H[a>>2]+(c>>>3&536870908)|0;c=c&31;if(c){d=32-c|0;e=b>>>0>d>>>0?d:b;H[a>>2]=H[a>>2]&(-1<>>d-e^-1);b=b-e|0;a=a+4|0}c=b>>>5|0;if(b>>>0>=32){ra(a,0,c<<2)}if((b&-32)==(b|0)){break a}a=(c<<2)+a|0;H[a>>2]=H[a>>2]&(-1>>>32-(b&31)^-1)}}function Fc(a,b,c){var d=0,e=0,f=0;d=H[c+16>>2];a:{if(!d){if(Sd(c)){break a}d=H[c+16>>2]}f=H[c+20>>2];if(d-f>>>0>>0){return ea[H[c+36>>2]](c,a,b)|0}b:{if(H[c+80>>2]<0){d=0;break b}e=b;while(1){d=e;if(!d){d=0;break b}e=d-1|0;if(I[e+a|0]!=10){continue}break}e=ea[H[c+36>>2]](c,a,d)|0;if(e>>>0>>0){break a}a=a+d|0;b=b-d|0;f=H[c+20>>2]}qa(f,a,b);H[c+20>>2]=H[c+20>>2]+b;e=b+d|0}return e}function ad(a){var b=0,c=0,d=0,e=0;if(I[a+76|0]){F[a+76|0]=0;e=H[a+60>>2];c=H[a+72>>2]+7|0;b=c>>>0<7?1:b;d=b<<29|c>>>3;c=d+H[a+56>>2]|0;b=(b>>>3|0)+e|0;H[a+56>>2]=c;H[a+60>>2]=c>>>0>>0?b+1|0:b}if(J[a+38>>1]<=513){F[a+132|0]=0;e=H[a+116>>2];b=0;c=H[a+128>>2]+7|0;b=c>>>0<7?1:b;d=b<<29|c>>>3;c=d+H[a+112>>2]|0;b=(b>>>3|0)+e|0;H[a+112>>2]=c;H[a+116>>2]=c>>>0>>0?b+1|0:b}}function re(a,b,c){var d=0,e=0,f=0,g=0;a:{if(a>>>0>10){break a}d=H[c+20>>2];f=H[c+12>>2];e=H[c+16>>2];if((d|0)>=(f|0)&e>>>0>=K[c+8>>2]|(d|0)>(f|0)){break a}f=F[e+H[c>>2]|0];e=e+1|0;d=e?d:d+1|0;H[c+16>>2]=e;H[c+20>>2]=d;d=f;b:{if((d|0)<0){if(!re(a+1|0,b,c)){break a}a=H[b>>2];d=d&127|a<<7;a=H[b+4>>2]<<7|a>>>25;break b}d=d&255;a=0}H[b>>2]=d;H[b+4>>2]=a;g=1}return g}function gb(a,b,c){var d=0,e=0,f=0,g=0;a:{if(a>>>0>10){break a}d=H[c+20>>2];f=H[c+12>>2];e=H[c+16>>2];if((d|0)>=(f|0)&e>>>0>=K[c+8>>2]|(d|0)>(f|0)){break a}f=F[e+H[c>>2]|0];e=e+1|0;d=e?d:d+1|0;H[c+16>>2]=e;H[c+20>>2]=d;d=f;b:{if((d|0)<0){if(!gb(a+1|0,b,c)){break a}a=H[b>>2];d=d&127|a<<7;a=H[b+4>>2]<<7|a>>>25;break b}d=d&255;a=0}H[b>>2]=d;H[b+4>>2]=a;g=1}return g}function Nh(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0,h=0,i=0;e=ca+-64|0;ca=e;d=ea[H[H[a>>2]+44>>2]](a,b)|0;a=ea[H[H[a>>2]+40>>2]](a,b)|0;f=Eb(e);g=H[b+56>>2];h=d&255;i=a;a=a-1|0;if(a>>>0<=10){a=H[(a<<2)+13584>>2]}else{a=-1}d=N(a,d);lc(f,g,h,i,0,d,d>>31);a=jc(pa(96),f);mb(a,c);F[a+84|0]=1;H[a+72>>2]=H[a+68>>2];H[a+60>>2]=H[b+60>>2];ca=e- -64|0;return a|0}function If(a,b,c,d,e){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;if(Ya(a,H[b+8>>2],e)){if(!(H[b+28>>2]==1|H[b+4>>2]!=(c|0))){H[b+28>>2]=d}return}a:{if(!Ya(a,H[b>>2],e)){break a}if(!(H[b+16>>2]!=(c|0)&H[b+20>>2]!=(c|0))){if((d|0)!=1){break a}H[b+32>>2]=1;return}H[b+20>>2]=c;H[b+32>>2]=d;H[b+40>>2]=H[b+40>>2]+1;if(!(H[b+36>>2]!=1|H[b+24>>2]!=2)){F[b+54|0]=1}H[b+44>>2]=4}}function Bh(a){a=a|0;var b=0,c=0,d=0,e=0,f=0,g=0,h=0;e=H[a+32>>2];b=e;h=H[b+8>>2];g=H[b+12>>2];c=H[b+16>>2];b=H[b+20>>2];f=c+4|0;b=f>>>0<4?b+1|0:b;d=0;a:{if(f>>>0>h>>>0&(b|0)>=(g|0)|(b|0)>(g|0)){break a}c=H[e>>2]+c|0;c=I[c|0]|I[c+1|0]<<8|(I[c+2|0]<<16|I[c+3|0]<<24);H[e+16>>2]=f;H[e+20>>2]=b;d=0;if((c|0)<0){break a}H[H[a+4>>2]+80>>2]=c;d=1}return d|0}function qi(a){a=a|0;var b=0,c=0,d=0;H[a>>2]=11276;b=H[a+48>>2];H[a+48>>2]=0;if(b){ea[H[H[b>>2]+4>>2]](b)}H[a>>2]=13280;b=H[a+20>>2];if(b){H[a+24>>2]=b;oa(b)}d=H[a+8>>2];if(d){c=H[a+12>>2];b=d;if((c|0)!=(b|0)){while(1){c=c-4|0;b=H[c>>2];H[c>>2]=0;if(b){ea[H[H[b>>2]+4>>2]](b)}if((c|0)!=(d|0)){continue}break}b=H[a+8>>2]}H[a+12>>2]=d;oa(b)}return a|0}function Ee(a,b){var c=0,d=0,e=0,f=0;H[a+144>>2]=b;c=H[(ea[H[H[b>>2]+32>>2]](b)|0)+32>>2];c=H[c>>2]+H[c+16>>2]|0;d=H[(ea[H[H[b>>2]+32>>2]](b)|0)+32>>2];d=H[d+8>>2]-H[d+16>>2]|0;e=a,f=J[H[(ea[H[H[b>>2]+32>>2]](b)|0)+32>>2]+38>>1],G[e+38>>1]=f;H[a>>2]=c;H[a+16>>2]=0;H[a+20>>2]=0;H[a+8>>2]=d;H[a+12>>2]=0;e=a,f=ea[H[H[b>>2]+36>>2]](b)|0,H[e+148>>2]=f}function Cd(a,b,c,d){F[a+53|0]=1;a:{if(H[a+4>>2]!=(c|0)){break a}F[a+52|0]=1;c=H[a+16>>2];b:{if(!c){H[a+36>>2]=1;H[a+24>>2]=d;H[a+16>>2]=b;if((d|0)!=1){break a}if(H[a+48>>2]==1){break b}break a}if((b|0)==(c|0)){c=H[a+24>>2];if((c|0)==2){H[a+24>>2]=d;c=d}if(H[a+48>>2]!=1){break a}if((c|0)==1){break b}break a}H[a+36>>2]=H[a+36>>2]+1}F[a+54|0]=1}}function pi(a){a=a|0;var b=0,c=0,d=0;H[a>>2]=11276;b=H[a+48>>2];H[a+48>>2]=0;if(b){ea[H[H[b>>2]+4>>2]](b)}H[a>>2]=13280;b=H[a+20>>2];if(b){H[a+24>>2]=b;oa(b)}d=H[a+8>>2];if(d){c=H[a+12>>2];b=d;if((c|0)!=(b|0)){while(1){c=c-4|0;b=H[c>>2];H[c>>2]=0;if(b){ea[H[H[b>>2]+4>>2]](b)}if((c|0)!=(d|0)){continue}break}b=H[a+8>>2]}H[a+12>>2]=d;oa(b)}oa(a)}function zh(a){a=a|0;var b=0,c=0,d=0,e=0,f=0,g=0;e=H[a+32>>2];b=e;g=H[b+8>>2];d=H[b+12>>2];c=H[b+16>>2];b=H[b+20>>2];f=d;d=c+4|0;b=d>>>0<4?b+1|0:b;if((f|0)>=(b|0)&d>>>0<=g>>>0|(b|0)<(f|0)){c=H[e>>2]+c|0;c=I[c|0]|I[c+1|0]<<8|(I[c+2|0]<<16|I[c+3|0]<<24);H[e+16>>2]=d;H[e+20>>2]=b;H[H[a+4>>2]+80>>2]=c}return(b|0)<=(f|0)&d>>>0<=g>>>0|(b|0)<(f|0)}function Mf(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0;e=ca+-64|0;ca=e;d=1;a:{if(Ya(a,b,0)){break a}d=0;if(!b){break a}b=Ed(b,14972);d=0;if(!b){break a}d=e+8|0;ra(d|4,0,52);H[e+56>>2]=1;H[e+20>>2]=-1;H[e+16>>2]=a;H[e+8>>2]=b;ea[H[H[b>>2]+28>>2]](b,d,H[c>>2],1);a=H[e+32>>2];if((a|0)==1){H[c>>2]=H[e+24>>2]}d=(a|0)==1}ca=e- -64|0;return d|0}function Ie(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0;d=ca-16|0;ca=d;H[a+4>>2]=b;b=H[b+64>>2];e=H[b>>2];b=H[b+4>>2];F[d+15|0]=0;Oa(a+24|0,(b-e>>2>>>0)/3|0,d+15|0);b=H[a+4>>2];e=H[b+56>>2];b=H[b+52>>2];F[d+14|0]=0;Oa(a+36|0,e-b>>2,d+14|0);b=H[c+12>>2];H[a+16>>2]=H[c+8>>2];H[a+20>>2]=b;b=H[c+4>>2];H[a+8>>2]=H[c>>2];H[a+12>>2]=b;ca=d+16|0}function pc(a,b,c){var d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0;if(!b){H[c>>2]=0;return}h=0-I[a+12|0]&255;e=H[a+4>>2];d=H[a+8>>2];i=H[a>>2];while(1){j=f<<1;if(!((e|0)<=0|d>>>0>4095)){e=e-1|0;H[a+4>>2]=e;d=I[e+i|0]|d<<8}g=d&255;f=g>>>0>>0;k=g;g=N(d>>>8|0,h);d=f?k+g|0:d-(h+g|0)|0;H[a+8>>2]=d;f=f|j;b=b-1|0;if(b){continue}break}H[c>>2]=f}function yg(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;var e=0,f=0,g=0;a=ca-16|0;ca=a;f=F[b+24|0];e=H[3411];H[a+8>>2]=H[3410];H[a+12>>2]=e;e=H[3409];H[a>>2]=H[3408];H[a+4>>2]=e;e=Va(b,c,f,a);if(e){b=0;if(f){c=(f&255)<<2;b=pa(c);g=qa(b,a,c)+c|0}c=H[d>>2];if(c){H[d+4>>2]=c;oa(c)}H[d+8>>2]=g;H[d+4>>2]=g;H[d>>2]=b}ca=a+16|0;return e|0}function of(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0;f=ea[H[H[a>>2]+24>>2]](a)|0;c=1;a:{if((f|0)<=0){break a}d=H[H[a+36>>2]>>2];g=a+48|0;c=0;if(!(ea[H[H[d>>2]+16>>2]](d,g,b)|0)){break a}while(1){e=e+1|0;if((f|0)!=(e|0)){d=H[H[a+36>>2]+(e<<2)>>2];if(ea[H[H[d>>2]+16>>2]](d,g,b)|0){continue}}break}c=(e|0)>=(f|0)}return c|0}function nf(a,b){a=a|0;b=b|0;var c=0,d=0,e=0,f=0,g=0;f=ea[H[H[a>>2]+24>>2]](a)|0;c=1;a:{if((f|0)<=0){break a}d=H[H[a+36>>2]>>2];g=a+48|0;c=0;if(!(ea[H[H[d>>2]+20>>2]](d,g,b)|0)){break a}while(1){e=e+1|0;if((f|0)!=(e|0)){d=H[H[a+36>>2]+(e<<2)>>2];if(ea[H[H[d>>2]+20>>2]](d,g,b)|0){continue}}break}c=(e|0)>=(f|0)}return c|0}function _c(a,b){var c=0,d=0;a:{c=H[a+4>>2];d=H[a+8>>2];if((c|0)==d<<5){if((c+1|0)<0){break a}if(c>>>0<=1073741822){d=d<<6;c=(c&-32)+32|0;c=c>>>0>>0?d:c}else{c=2147483647}pb(a,c);c=H[a+4>>2]}H[a+4>>2]=c+1;d=1<>2]+(c>>>3&536870908)|0;if(I[b|0]){H[a>>2]=d|H[a>>2];return}H[a>>2]=H[a>>2]&(d^-1);return}sa();v()}function $h(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0;d=ca-16|0;ca=d;H[a+4>>2]=b;e=H[b>>2];b=H[b+4>>2];F[d+15|0]=0;Oa(a+24|0,(b-e>>2>>>0)/3|0,d+15|0);b=H[a+4>>2];e=H[b+28>>2];b=H[b+24>>2];F[d+14|0]=0;Oa(a+36|0,e-b>>2,d+14|0);b=H[c+12>>2];H[a+16>>2]=H[c+8>>2];H[a+20>>2]=b;b=H[c+4>>2];H[a+8>>2]=H[c>>2];H[a+12>>2]=b;ca=d+16|0}function $b(a){var b=0;H[a>>2]=0;H[a+4>>2]=0;H[a+56>>2]=0;H[a+48>>2]=0;H[a+52>>2]=0;H[a+40>>2]=0;H[a+44>>2]=0;H[a+32>>2]=0;H[a+36>>2]=0;H[a+24>>2]=0;H[a+28>>2]=0;H[a+16>>2]=0;H[a+20>>2]=0;H[a+8>>2]=0;H[a+12>>2]=0;b=a- -64|0;H[b>>2]=0;H[b+4>>2]=0;H[a+72>>2]=0;H[a+76>>2]=0;H[a+80>>2]=0;H[a+84>>2]=0;H[a+60>>2]=a;return a}function td(a,b,c){var d=0,e=0,f=0,g=0;a:{if(a>>>0>5){break a}d=H[c+20>>2];e=H[c+12>>2];f=H[c+16>>2];if((d|0)>=(e|0)&f>>>0>=K[c+8>>2]|(d|0)>(e|0)){break a}e=I[H[c>>2]+f|0];f=f+1|0;d=f?d:d+1|0;H[c+16>>2]=f;H[c+20>>2]=d;d=e<<24>>24;if((d|0)<0){if(!td(a+1|0,b,c)){break a}e=d&127|H[b>>2]<<7}H[b>>2]=e;g=1}return g} +function hb(a,b,c){var d=0,e=0,f=0,g=0;a:{if(a>>>0>5){break a}d=H[c+20>>2];e=H[c+12>>2];f=H[c+16>>2];if((d|0)>=(e|0)&f>>>0>=K[c+8>>2]|(d|0)>(e|0)){break a}e=I[H[c>>2]+f|0];f=f+1|0;d=f?d:d+1|0;H[c+16>>2]=f;H[c+20>>2]=d;d=e<<24>>24;if((d|0)<0){if(!hb(a+1|0,b,c)){break a}e=d&127|H[b>>2]<<7}H[b>>2]=e;g=1}return g}function Xa(a,b,c){var d=0,e=0,f=0,g=0;a:{if(a>>>0>5){break a}d=H[c+20>>2];e=H[c+12>>2];f=H[c+16>>2];if((d|0)>=(e|0)&f>>>0>=K[c+8>>2]|(d|0)>(e|0)){break a}e=I[H[c>>2]+f|0];f=f+1|0;d=f?d:d+1|0;H[c+16>>2]=f;H[c+20>>2]=d;d=e<<24>>24;if((d|0)<0){if(!Xa(a+1|0,b,c)){break a}e=d&127|H[b>>2]<<7}H[b>>2]=e;g=1}return g}function Qe(a,b,c){var d=0,e=0,f=0,g=0;a:{if(a>>>0>5){break a}d=H[c+20>>2];e=H[c+12>>2];f=H[c+16>>2];if((d|0)>=(e|0)&f>>>0>=K[c+8>>2]|(d|0)>(e|0)){break a}e=I[H[c>>2]+f|0];f=f+1|0;d=f?d:d+1|0;H[c+16>>2]=f;H[c+20>>2]=d;d=e<<24>>24;if((d|0)<0){if(!Qe(a+1|0,b,c)){break a}e=d&127|H[b>>2]<<7}H[b>>2]=e;g=1}return g}function Pc(a,b,c){var d=0,e=0,f=0,g=0;a:{if(a>>>0>5){break a}d=H[c+20>>2];e=H[c+12>>2];f=H[c+16>>2];if((d|0)>=(e|0)&f>>>0>=K[c+8>>2]|(d|0)>(e|0)){break a}e=I[H[c>>2]+f|0];f=f+1|0;d=f?d:d+1|0;H[c+16>>2]=f;H[c+20>>2]=d;d=e<<24>>24;if((d|0)<0){if(!Pc(a+1|0,b,c)){break a}e=d&127|H[b>>2]<<7}H[b>>2]=e;g=1}return g}function Fb(a,b,c){var d=0,e=0,f=0,g=0;a:{if(a>>>0>5){break a}d=H[c+20>>2];e=H[c+12>>2];f=H[c+16>>2];if((d|0)>=(e|0)&f>>>0>=K[c+8>>2]|(d|0)>(e|0)){break a}e=I[H[c>>2]+f|0];f=f+1|0;d=f?d:d+1|0;H[c+16>>2]=f;H[c+20>>2]=d;d=e<<24>>24;if((d|0)<0){if(!Fb(a+1|0,b,c)){break a}e=d&127|H[b>>2]<<7}H[b>>2]=e;g=1}return g}function Ea(a,b,c){var d=0,e=0,f=0,g=0;a:{if(a>>>0>5){break a}d=H[c+20>>2];e=H[c+12>>2];f=H[c+16>>2];if((d|0)>=(e|0)&f>>>0>=K[c+8>>2]|(d|0)>(e|0)){break a}e=I[H[c>>2]+f|0];f=f+1|0;d=f?d:d+1|0;H[c+16>>2]=f;H[c+20>>2]=d;d=e<<24>>24;if((d|0)<0){if(!Ea(a+1|0,b,c)){break a}e=d&127|H[b>>2]<<7}H[b>>2]=e;g=1}return g}function Bb(a,b,c){var d=0,e=0,f=0,g=0;a:{if(a>>>0>5){break a}d=H[c+20>>2];e=H[c+12>>2];f=H[c+16>>2];if((d|0)>=(e|0)&f>>>0>=K[c+8>>2]|(d|0)>(e|0)){break a}e=I[H[c>>2]+f|0];f=f+1|0;d=f?d:d+1|0;H[c+16>>2]=f;H[c+20>>2]=d;d=e<<24>>24;if((d|0)<0){if(!Bb(a+1|0,b,c)){break a}e=d&127|H[b>>2]<<7}H[b>>2]=e;g=1}return g}function Fa(a,b,c){var d=0,e=0;a:{b:{if(c>>>0>=4){if((a|b)&3){break b}while(1){if(H[a>>2]!=H[b>>2]){break b}b=b+4|0;a=a+4|0;c=c-4|0;if(c>>>0>3){continue}break}}if(!c){break a}}while(1){d=I[a|0];e=I[b|0];if((d|0)==(e|0)){b=b+1|0;a=a+1|0;c=c-1|0;if(c){continue}break a}break}return d-e|0}return 0}function Yc(a){var b=0,c=0,d=0,e=0;d=H[a>>2];if(d){e=d;c=H[a+4>>2];if((d|0)!=(c|0)){while(1){e=c-144|0;b=H[e+132>>2];if(b){H[c-8>>2]=b;oa(b)}b=H[c-28>>2];if(b){H[c-24>>2]=b;oa(b)}b=H[c-40>>2];if(b){H[c-36>>2]=b;oa(b)}oc(c-140|0);c=e;if((d|0)!=(c|0)){continue}break}e=H[a>>2]}H[a+4>>2]=d;oa(e)}}function Dg(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0;d=H[b+4>>2];a:{if(!d){break a}b=H[H[H[b+8>>2]+(c<<2)>>2]+60>>2];if((b|0)<0){break a}a=H[d+24>>2];c=H[d+28>>2];if((a|0)==(c|0)){break a}b:{while(1){e=H[a>>2];if((b|0)==H[e+24>>2]){break b}a=a+4|0;if((c|0)!=(a|0)){continue}break}e=0}}return e|0}function Zh(a){a=a|0;var b=0;H[a+8>>2]=12384;H[a>>2]=12172;b=H[a+96>>2];if(b){H[a+100>>2]=b;oa(b)}b=H[a+80>>2];if(b){H[a+84>>2]=b;oa(b)}b=H[a+68>>2];if(b){H[a+72>>2]=b;oa(b)}b=H[a+56>>2];if(b){H[a+60>>2]=b;oa(b)}H[a+8>>2]=12620;b=H[a+44>>2];if(b){oa(b)}b=H[a+32>>2];if(b){oa(b)}return a|0}function Uc(a){var b=0,c=0,d=0;if(a){d=H[a+24>>2];if(d){b=d;c=H[a+28>>2];if((b|0)!=(c|0)){while(1){c=c-4|0;b=H[c>>2];H[c>>2]=0;if(b){Ra(b+12|0,H[b+16>>2]);Qa(b,H[b+4>>2]);oa(b)}if((c|0)!=(d|0)){continue}break}b=H[a+24>>2]}H[a+28>>2]=d;oa(b)}Ra(a+12|0,H[a+16>>2]);Qa(a,H[a+4>>2]);oa(a)}}function Yh(a){a=a|0;var b=0;H[a+8>>2]=12384;H[a>>2]=12172;b=H[a+96>>2];if(b){H[a+100>>2]=b;oa(b)}b=H[a+80>>2];if(b){H[a+84>>2]=b;oa(b)}b=H[a+68>>2];if(b){H[a+72>>2]=b;oa(b)}b=H[a+56>>2];if(b){H[a+60>>2]=b;oa(b)}H[a+8>>2]=12620;b=H[a+44>>2];if(b){oa(b)}b=H[a+32>>2];if(b){oa(b)}oa(a)}function vi(a){a=a|0;var b=0,c=0,d=0;H[a>>2]=13280;b=H[a+20>>2];if(b){H[a+24>>2]=b;oa(b)}d=H[a+8>>2];if(d){c=H[a+12>>2];b=d;if((c|0)!=(b|0)){while(1){c=c-4|0;b=H[c>>2];H[c>>2]=0;if(b){ea[H[H[b>>2]+4>>2]](b)}if((c|0)!=(d|0)){continue}break}b=H[a+8>>2]}H[a+12>>2]=d;oa(b)}return a|0}function xc(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0,f=0,g=0,h=0,i=0;h=H[c+8>>2];e=H[c+16>>2];g=H[c+12>>2];f=g;d=H[c+20>>2];if(h>>>0>e>>>0&(f|0)>=(d|0)|(d|0)<(f|0)){b=I[H[c>>2]+e|0];i=e+1|0;f=i?d:d+1|0;H[c+16>>2]=i;H[c+20>>2]=f;H[a+4>>2]=b}return e>>>0>>0&(d|0)<=(g|0)|(d|0)<(g|0)}function Wc(a){a=a|0;var b=0,c=0,d=0;H[a>>2]=13280;b=H[a+20>>2];if(b){H[a+24>>2]=b;oa(b)}d=H[a+8>>2];if(d){c=H[a+12>>2];b=d;if((c|0)!=(b|0)){while(1){c=c-4|0;b=H[c>>2];H[c>>2]=0;if(b){ea[H[H[b>>2]+4>>2]](b)}if((c|0)!=(d|0)){continue}break}b=H[a+8>>2]}H[a+12>>2]=d;oa(b)}oa(a)}function Ya(a,b,c){var d=0;if(!c){return H[a+4>>2]==H[b+4>>2]}if((a|0)==(b|0)){return 1}d=H[a+4>>2];a=I[d|0];c=H[b+4>>2];b=I[c|0];a:{if(!a|(b|0)!=(a|0)){break a}while(1){b=I[c+1|0];a=I[d+1|0];if(!a){break a}c=c+1|0;d=d+1|0;if((a|0)==(b|0)){continue}break}}return(a|0)==(b|0)}function _h(a){a=a|0;var b=0;H[a>>2]=12384;b=H[a+88>>2];if(b){H[a+92>>2]=b;oa(b)}b=H[a+72>>2];if(b){H[a+76>>2]=b;oa(b)}b=H[a+60>>2];if(b){H[a- -64>>2]=b;oa(b)}b=H[a+48>>2];if(b){H[a+52>>2]=b;oa(b)}H[a>>2]=12620;b=H[a+36>>2];if(b){oa(b)}b=H[a+24>>2];if(b){oa(b)}return a|0}function Fg(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0;d=H[b+12>>2];b=H[b+8>>2];a=0;a:{if((d|0)==(b|0)){break a}a=d-b>>2;d=a>>>0<=1?1:a;a=0;b:{while(1){e=H[b+(a<<2)>>2];if(H[e+60>>2]==(c|0)){break b}a=a+1|0;if((d|0)!=(a|0)){continue}break}a=0;break a}a=(a|0)!=-1?e:0}return a|0}function ae(a,b){var c=0,d=0,e=0;H[a+8>>2]=0;H[a>>2]=0;H[a+4>>2]=0;a:{c=H[b+4>>2];d=H[b>>2];b:{if((c|0)==(d|0)){a=c;break b}c=c-d|0;if((c|0)<0){break a}d=c;e=pa(c);c=ra(e,0,c);d=d+c|0;H[a+8>>2]=d;H[a+4>>2]=d;H[a>>2]=c;c=H[b>>2];a=H[b+4>>2]}qa(e,c,a-c|0);return}sa();v()}function ed(a){var b=0,c=0,d=0,e=0;c=H[a+4>>2];d=H[a>>2];if((c|0)!=(d|0)){while(1){e=c-144|0;b=H[e+132>>2];if(b){H[c-8>>2]=b;oa(b)}b=H[c-28>>2];if(b){H[c-24>>2]=b;oa(b)}b=H[c-40>>2];if(b){H[c-36>>2]=b;oa(b)}oc(c-140|0);c=e;if((d|0)!=(c|0)){continue}break}}H[a+4>>2]=d}function Vh(a){a=a|0;var b=0;H[a>>2]=12384;b=H[a+88>>2];if(b){H[a+92>>2]=b;oa(b)}b=H[a+72>>2];if(b){H[a+76>>2]=b;oa(b)}b=H[a+60>>2];if(b){H[a- -64>>2]=b;oa(b)}b=H[a+48>>2];if(b){H[a+52>>2]=b;oa(b)}H[a>>2]=12620;b=H[a+36>>2];if(b){oa(b)}b=H[a+24>>2];if(b){oa(b)}oa(a)}function cb(a){var b=0;if(a){b=H[a+76>>2];if(b){H[a+80>>2]=b;oa(b)}b=H[a- -64>>2];if(b){H[a+68>>2]=b;oa(b)}b=H[a+48>>2];if(b){H[a+52>>2]=b;oa(b)}b=H[a+24>>2];if(b){H[a+28>>2]=b;oa(b)}b=H[a+12>>2];if(b){H[a+16>>2]=b;oa(b)}b=H[a>>2];if(b){H[a+4>>2]=b;oa(b)}oa(a)}}function Jd(a,b,c){var d=0,e=0,f=0,g=0;f=ca-16|0;ca=f;d=ca-16|0;ca=d;b=b-a>>2;while(1){if(b){H[d+12>>2]=a;e=b>>>1|0;H[d+12>>2]=H[d+12>>2]+(e<<2);g=(e^-1)+b|0;b=e;e=K[H[d+12>>2]>>2]>2];b=e?g:b;a=e?H[d+12>>2]+4|0:a;continue}break}ca=d+16|0;ca=f+16|0;return a}function oc(a){var b=0;b=H[a+84>>2];if(b){H[a+88>>2]=b;oa(b)}b=H[a+72>>2];if(b){H[a+76>>2]=b;oa(b)}b=H[a+52>>2];if(b){H[a+56>>2]=b;oa(b)}b=H[a+40>>2];if(b){H[a+44>>2]=b;oa(b)}b=H[a+28>>2];if(b){H[a+32>>2]=b;oa(b)}b=H[a+12>>2];if(b){oa(b)}a=H[a>>2];if(a){oa(a)}}function Xc(a,b){var c=0,d=0;d=pa(40);H[d>>2]=-1;c=d+8|0;H[c+16>>2]=0;H[c+20>>2]=0;H[c+8>>2]=0;H[c>>2]=0;H[c+4>>2]=0;H[c+24>>2]=0;H[c+28>>2]=0;ea[H[H[a>>2]+16>>2]](a,d);a=H[b+88>>2];H[b+88>>2]=d;if(a){b=H[a+8>>2];if(b){H[a+12>>2]=b;oa(b)}oa(a)}return 1}function Ma(a){var b=0,c=0,d=0;b=a;a:{if(b&3){while(1){if(!I[b|0]){break a}b=b+1|0;if(b&3){continue}break}}while(1){c=b;b=b+4|0;d=H[c>>2];if(!((d^-1)&d-16843009&-2139062144)){continue}break}while(1){b=c;c=b+1|0;if(I[b|0]){continue}break}}return b-a|0}function Ba(a){var b=0,c=0,d=0,e=0,f=0;d=I[a+12|0];c=H[a+8>>2];a:{if(c>>>0>4095){break a}b=H[a+4>>2];if((b|0)<=0){break a}b=b-1|0;H[a+4>>2]=b;c=I[b+H[a>>2]|0]|c<<8}d=0-d&255;b=N(d,c>>>8|0);e=c&255;f=e>>>0>>0;H[a+8>>2]=f?b+e|0:c-(b+d|0)|0;return f}function od(a,b){H[a+4>>2]=0;H[a+8>>2]=0;H[a>>2]=1984;H[a+12>>2]=0;H[a+16>>2]=0;H[a+20>>2]=0;H[a+24>>2]=0;H[a+28>>2]=0;H[a+32>>2]=0;H[a+36>>2]=0;H[a+40>>2]=0;H[a>>2]=2328;H[a+60>>2]=b;H[a+44>>2]=0;H[a+48>>2]=0;H[a+52>>2]=0;H[a+56>>2]=0;return a}function mc(a,b){var c=0,d=0,e=0;c=Ma(b);if(c>>>0<2147483632){a:{b:{if(c>>>0>=11){d=(c|15)+1|0;e=pa(d);H[a+8>>2]=d|-2147483648;H[a>>2]=e;H[a+4>>2]=c;d=c+e|0;break b}F[a+11|0]=c;d=a+c|0;e=a;if(!c){break a}}va(e,b,c)}F[d|0]=0;return a}Na();v()}function Ng(a){a=a|0;var b=0,c=0,d=0;if(a){if(F[a+27|0]<0){oa(H[a+16>>2])}b=H[a>>2];if(b){c=b;d=H[a+4>>2];if((b|0)!=(d|0)){while(1){c=d-12|0;if(F[d-1|0]<0){oa(H[c>>2])}d=c;if((d|0)!=(b|0)){continue}break}c=H[a>>2]}H[a+4>>2]=b;oa(c)}oa(a)}}function Jb(a,b){var c=0,d=0,e=0;a:{c=H[a>>2];b:{if(H[a+8>>2]-c>>2>>>0>=b>>>0){break b}if(b>>>0>=1073741824){break a}d=H[a+4>>2]-c|0;e=b<<2;b=va(pa(e),c,d);H[a+8>>2]=b+e;H[a+4>>2]=b+d;H[a>>2]=b;if(!c){break b}oa(c)}return}sa();v()}function Ga(a){a=a|0;var b=0,c=0;if(a){b=H[a+88>>2];H[a+88>>2]=0;if(b){c=H[b+8>>2];if(c){H[b+12>>2]=c;oa(c)}oa(b)}b=H[a+68>>2];if(b){H[a+72>>2]=b;oa(b)}b=H[a+64>>2];H[a+64>>2]=0;if(b){c=H[b>>2];if(c){H[b+4>>2]=c;oa(c)}oa(b)}oa(a)}}function Nd(a){var b=0,c=0,d=0;if(F[H[a>>2]]-48>>>0>=10){return 0}while(1){d=H[a>>2];c=-1;if(b>>>0<=214748364){c=F[d|0]-48|0;b=N(b,10);c=(c|0)>(b^2147483647)?-1:c+b|0}H[a>>2]=d+1;b=c;if(F[d+1|0]-48>>>0<10){continue}break}return b}function Cg(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;b=H[b+96>>2];a=pa(12);b=b+N(c,12)|0;c=H[b+4>>2];H[a>>2]=H[b>>2];H[a+4>>2]=c;H[a+8>>2]=H[b+8>>2];b=H[d>>2];if(b){H[d+4>>2]=b;oa(b)}H[d>>2]=a;a=a+12|0;H[d+8>>2]=a;H[d+4>>2]=a;return 1}function Ai(a){a=a|0;var b=0;H[a+24>>2]=1832;H[a>>2]=11048;b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}H[a>>2]=2448;b=H[a+20>>2];H[a+20>>2]=0;if(b){ea[H[H[b>>2]+4>>2]](b)}H[a>>2]=2232;b=H[a+16>>2];H[a+16>>2]=0;if(b){Ga(b)}return a|0}function Sj(a,b,c,d){var e=0,f=0,g=0,h=0;f=b^d;g=f>>31;e=b>>31;a=a^e;h=a-e|0;e=(b^e)-((a>>>0>>0)+e|0)|0;a=d>>31;b=c^a;f=f>>31;a=Tj(h,e,b-a|0,(a^d)-((a>>>0>b>>>0)+a|0)|0)^f;b=a-f|0;da=(g^da)-((a>>>0>>0)+g|0)|0;return b}function yi(a){a=a|0;var b=0;H[a+24>>2]=1832;H[a>>2]=11048;b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}H[a>>2]=2448;b=H[a+20>>2];H[a+20>>2]=0;if(b){ea[H[H[b>>2]+4>>2]](b)}H[a>>2]=2232;b=H[a+16>>2];H[a+16>>2]=0;if(b){Ga(b)}oa(a)}function Yb(a,b,c){var d=0,e=0,f=0;e=ca-16|0;ca=e;d=H[a+8>>2]&2147483647;a:{if(d>>>0>c>>>0){d=H[a>>2];H[a+4>>2]=c;yb(d,b,c);F[e+15|0]=0;F[c+d|0]=I[e+15|0];break a}f=a;a=H[a+4>>2];Gd(f,d-1|0,(c-d|0)+1|0,a,a,c,b)}ca=e+16|0}function Bf(a,b){a=a|0;b=b|0;var c=0,d=0;c=ca-16|0;ca=c;a=H[a+4>>2];a:{if((a|0)==-1){break a}F[c+15|0]=a;d=H[b+20>>2];if(!!H[b+16>>2]&(d|0)>=0|(d|0)>0){break a}Wb(b,H[b+4>>2],c+15|0,c+16|0)}ca=c+16|0;return(a|0)!=-1|0}function Xb(a,b,c){var d=0,e=0;d=ca-16|0;ca=d;a:{if(c>>>0<=10){F[a+11|0]=I[a+11|0]&128|c;F[a+11|0]=I[a+11|0]&127;yb(a,b,c);F[d+15|0]=0;F[a+c|0]=I[d+15|0];break a}e=a;a=I[a+11|0]&127;Gd(e,10,c-10|0,a,a,c,b)}ca=d+16|0}function Rj(a,b,c,d){var e=0,f=0,g=0,h=0,i=0,j=0;e=c>>>16|0;f=a>>>16|0;j=N(e,f);g=c&65535;h=a&65535;i=N(g,h);f=(i>>>16|0)+N(f,g)|0;e=(f&65535)+N(e,h)|0;da=(N(b,c)+j|0)+N(a,d)+(f>>>16)+(e>>>16)|0;return i&65535|e<<16}function Dd(a,b,c){var d=0;d=H[a+16>>2];if(!d){H[a+36>>2]=1;H[a+24>>2]=c;H[a+16>>2]=b;return}a:{if((b|0)==(d|0)){if(H[a+24>>2]!=2){break a}H[a+24>>2]=c;return}F[a+54|0]=1;H[a+24>>2]=2;H[a+36>>2]=H[a+36>>2]+1}}function th(){var a=0;a=Eb(pa(96));H[a+64>>2]=0;H[a+68>>2]=0;H[a+88>>2]=0;H[a+72>>2]=0;H[a+76>>2]=0;F[a+77|0]=0;F[a+78|0]=0;F[a+79|0]=0;F[a+80|0]=0;F[a+81|0]=0;F[a+82|0]=0;F[a+83|0]=0;F[a+84|0]=0;return a|0}function zi(a,b){a=a|0;b=b|0;var c=0,d=0;H[b>>2]=2;c=H[b+8>>2];d=H[b+12>>2]-c|0;if(d>>>0<=4294967291){kc(b+8|0,d+4|0);c=H[b+8>>2]}b=c+d|0;a=H[a+4>>2];F[b|0]=a;F[b+1|0]=a>>>8;F[b+2|0]=a>>>16;F[b+3|0]=a>>>24}function rj(a){a=a|0;var b=0;H[a>>2]=5580;b=H[a+96>>2];if(b){oa(b)}b=H[a+84>>2];if(b){oa(b)}b=H[a+72>>2];if(b){oa(b)}b=H[a+60>>2];if(b){oa(b)}H[a>>2]=3272;b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}return a|0}function ib(a,b,c,d,e){var f=0;f=ca-256|0;ca=f;if(!(e&73728|(c|0)<=(d|0))){d=c-d|0;c=d>>>0<256;ra(f,b&255,c?d:256);if(!c){while(1){Ab(a,f,256);d=d-256|0;if(d>>>0>255){continue}break}}Ab(a,f,d)}ca=f+256|0}function Ij(a){a=a|0;var b=0;H[a>>2]=3564;b=H[a+96>>2];if(b){oa(b)}b=H[a+84>>2];if(b){oa(b)}b=H[a+72>>2];if(b){oa(b)}b=H[a+60>>2];if(b){oa(b)}H[a>>2]=3272;b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}return a|0}function Ch(a){a=a|0;var b=0,c=0,d=0;b=H[a+8>>2];d=H[a+12>>2];if((b|0)==(d|0)){return 1}while(1){c=H[b>>2];c=ea[H[H[c>>2]+16>>2]](c,H[a+32>>2])|0;if(c){b=b+4|0;if((d|0)!=(b|0)){continue}}break}return c|0}function Yd(a,b){var c=0,d=0;c=H[a+8>>2];a=H[a+12>>2];if((c|0)!=(a|0)){a=a-c>>2;d=a>>>0<=1?1:a;a=0;while(1){if(H[H[(a<<2)+c>>2]+60>>2]==(b|0)){return a}a=a+1|0;if((d|0)!=(a|0)){continue}break}}return-1}function qj(a){a=a|0;var b=0;H[a>>2]=5580;b=H[a+96>>2];if(b){oa(b)}b=H[a+84>>2];if(b){oa(b)}b=H[a+72>>2];if(b){oa(b)}b=H[a+60>>2];if(b){oa(b)}H[a>>2]=3272;b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}oa(a)}function Hj(a){a=a|0;var b=0;H[a>>2]=3564;b=H[a+96>>2];if(b){oa(b)}b=H[a+84>>2];if(b){oa(b)}b=H[a+72>>2];if(b){oa(b)}b=H[a+60>>2];if(b){oa(b)}H[a>>2]=3272;b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}oa(a)}function $d(a,b,c){var d=0,e=0;d=a+4|0;a=nb(a,b);a:{if((d|0)==(a|0)){break a}b=H[a+32>>2];d=H[a+28>>2];if((b|0)==(d|0)){break a}Cc(c,b-d|0);c=Dc(c);b=H[a+28>>2];qa(c,b,H[a+32>>2]-b|0);e=1}return e}function Qf(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;var e=0;e=ca-16|0;ca=e;a=_(H[a+60>>2],b|0,c|0,d&255,e+8|0)|0;if(a){H[3992]=a;a=-1}else{a=0}ca=e+16|0;da=a?-1:H[e+12>>2];return(a?-1:H[e+8>>2])|0}function Sd(a){var b=0;b=H[a+72>>2];H[a+72>>2]=b-1|b;b=H[a>>2];if(b&8){H[a>>2]=b|32;return-1}H[a+4>>2]=0;H[a+8>>2]=0;b=H[a+44>>2];H[a+28>>2]=b;H[a+20>>2]=b;H[a+16>>2]=b+H[a+48>>2];return 0}function Eb(a){H[a+8>>2]=0;H[a+12>>2]=0;H[a>>2]=0;H[a+40>>2]=0;H[a+44>>2]=0;H[a+28>>2]=9;F[a+24|0]=1;H[a+56>>2]=-1;H[a+60>>2]=0;H[a+16>>2]=0;H[a+20>>2]=0;H[a+48>>2]=0;H[a+52>>2]=0;return a}function hf(a,b){a=a|0;b=b|0;var c=0,d=0;d=H[a+16>>2];c=0;a:{if(H[a+20>>2]-d>>2<=(b|0)){break a}b=H[(b<<2)+d>>2];c=0;if((b|0)<0){break a}c=rb(H[H[a+36>>2]+(b<<2)>>2])}return c|0}function Mg(){var a=0,b=0;a=pa(40);H[a+4>>2]=0;H[a+8>>2]=0;H[a+24>>2]=0;H[a+28>>2]=0;b=a+16|0;H[b>>2]=0;H[b+4>>2]=0;H[a>>2]=a+4;H[a+12>>2]=b;H[a+32>>2]=0;H[a+36>>2]=0;return a|0}function Vf(a,b){a=a|0;b=b|0;var c=0,d=0;Wd(a,b);a:{if((b|0)<0){break a}d=H[a+88>>2];c=H[a+84>>2];if(d-c>>2<=(b|0)){break a}c=(b<<2)+c|0;b=c+4|0;va(c,b,d-b|0);H[a+88>>2]=d-4}}function Rh(a){a=a|0;var b=0;H[a+8>>2]=12804;H[a>>2]=12640;b=H[a+56>>2];if(b){H[a+60>>2]=b;oa(b)}H[a+8>>2]=12620;b=H[a+44>>2];if(b){oa(b)}b=H[a+32>>2];if(b){oa(b)}return a|0}function Lh(a){a=a|0;var b=0;H[a+8>>2]=11872;H[a>>2]=12932;b=H[a+56>>2];if(b){H[a+60>>2]=b;oa(b)}H[a+8>>2]=12124;b=H[a+44>>2];if(b){oa(b)}b=H[a+32>>2];if(b){oa(b)}return a|0}function zb(a){var b=0,c=0;b=H[3958];c=a+7&-8;a=b+c|0;a:{if(a>>>0<=b>>>0?c:0){break a}if(a>>>0>fa()<<16>>>0){if(!($(a|0)|0)){break a}}H[3958]=a;return b}H[3992]=48;return-1}function bj(a,b,c){a=a|0;b=b|0;c=c|0;var d=0;H[a+4>>2]=b;b=H[H[H[b+4>>2]+8>>2]+(c<<2)>>2];H[a+12>>2]=c;H[a+8>>2]=b;a=H[a+8>>2];if(I[a+24|0]==3){d=H[a+28>>2]==9}return d|0}function wf(a,b,c){a=a|0;b=b|0;c=c|0;var d=0,e=0;d=H[a+8>>2];a:{if(!I[d+24|0]){break a}if(!mb(d,H[b+4>>2]-H[b>>2]>>2)){break a}e=ea[H[H[a>>2]+32>>2]](a,b,c)|0}return e|0}function Qh(a){a=a|0;var b=0;H[a+8>>2]=12804;H[a>>2]=12640;b=H[a+56>>2];if(b){H[a+60>>2]=b;oa(b)}H[a+8>>2]=12620;b=H[a+44>>2];if(b){oa(b)}b=H[a+32>>2];if(b){oa(b)}oa(a)}function Kh(a){a=a|0;var b=0;H[a+8>>2]=11872;H[a>>2]=12932;b=H[a+56>>2];if(b){H[a+60>>2]=b;oa(b)}H[a+8>>2]=12124;b=H[a+44>>2];if(b){oa(b)}b=H[a+32>>2];if(b){oa(b)}oa(a)}function nj(a){a=a|0;var b=0;H[a>>2]=5816;b=H[a+76>>2];if(b){oa(b)}b=H[a+68>>2];H[a+68>>2]=0;if(b){oa(b)}H[a>>2]=3272;b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}return a|0}function Ra(a,b){if(b){Ra(a,H[b>>2]);Ra(a,H[b+4>>2]);a=H[b+28>>2];H[b+28>>2]=0;if(a){Ra(a+12|0,H[a+16>>2]);Qa(a,H[a+4>>2]);oa(a)}if(F[b+27|0]<0){oa(H[b+16>>2])}oa(b)}}function Gi(a,b,c){a=a|0;b=b|0;c=c|0;var d=0;H[a+4>>2]=b;d=H[H[H[b+4>>2]+8>>2]+(c<<2)>>2];H[a+12>>2]=c;H[a+8>>2]=d;return H[H[H[H[b+4>>2]+8>>2]+(c<<2)>>2]+28>>2]==9|0}function Ej(a){a=a|0;var b=0;H[a>>2]=3812;b=H[a+76>>2];if(b){oa(b)}b=H[a+68>>2];H[a+68>>2]=0;if(b){oa(b)}H[a>>2]=3272;b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}return a|0}function Vc(a){H[a+40>>2]=0;H[a+4>>2]=0;H[a+8>>2]=0;H[a>>2]=13280;H[a+12>>2]=0;H[a+16>>2]=0;H[a+20>>2]=0;H[a+24>>2]=0;H[a+28>>2]=0;H[a+32>>2]=0;G[a+36>>1]=0;return a}function Hd(a,b){var c=0,d=0,e=0,f=0;H[a>>2]=15260;H[a>>2]=15372;c=Ma(b);d=pa(c+13|0);H[d+8>>2]=0;H[d+4>>2]=c;H[d>>2]=c;e=a,f=qa(d+12|0,b,c+1|0),H[e+4>>2]=f;return a}function jg(a,b){a=a|0;b=b|0;var c=0;a:{if(!(ea[H[H[a>>2]+36>>2]](a,b)|0)){break a}if(!(ea[H[H[a>>2]+40>>2]](a,b)|0)){break a}c=ea[H[H[a>>2]+44>>2]](a)|0}return c|0}function mj(a){a=a|0;var b=0;H[a>>2]=5816;b=H[a+76>>2];if(b){oa(b)}b=H[a+68>>2];H[a+68>>2]=0;if(b){oa(b)}H[a>>2]=3272;b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}oa(a)}function Dj(a){a=a|0;var b=0;H[a>>2]=3812;b=H[a+76>>2];if(b){oa(b)}b=H[a+68>>2];H[a+68>>2]=0;if(b){oa(b)}H[a>>2]=3272;b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}oa(a)}function Xe(a){a=a|0;var b=0;a:{if(!H[a- -64>>2]|!H[a+68>>2]|(!H[a+44>>2]|!H[a+48>>2])){break a}if(!H[a+52>>2]|!H[a+56>>2]){break a}b=H[a+92>>2]!=-1}return b|0}function cf(a){a=a|0;var b=0;H[a>>2]=2448;b=H[a+20>>2];H[a+20>>2]=0;if(b){ea[H[H[b>>2]+4>>2]](b)}H[a>>2]=2232;b=H[a+16>>2];H[a+16>>2]=0;if(b){Ga(b)}return a|0}function Pj(a,b){a=a|0;b=b|0;var c=0;b=H[b+88>>2];if(!(!b|H[b>>2]!=2)){c=a;a=H[b+8>>2];H[c+4>>2]=I[a|0]|I[a+1|0]<<8|(I[a+2|0]<<16|I[a+3|0]<<24);c=1}return c|0}function tc(a){a=a|0;var b=0;a:{if(!H[a+48>>2]|!H[a+52>>2]|(!H[a+28>>2]|!H[a+32>>2])){break a}if(!H[a+36>>2]|!H[a+40>>2]){break a}b=H[a+76>>2]!=-1}return b|0}function Sh(a){a=a|0;var b=0;H[a>>2]=12804;b=H[a+48>>2];if(b){H[a+52>>2]=b;oa(b)}H[a>>2]=12620;b=H[a+36>>2];if(b){oa(b)}b=H[a+24>>2];if(b){oa(b)}return a|0}function He(a){a=a|0;var b=0;H[a>>2]=11872;b=H[a+48>>2];if(b){H[a+52>>2]=b;oa(b)}H[a>>2]=12124;b=H[a+36>>2];if(b){oa(b)}b=H[a+24>>2];if(b){oa(b)}return a|0}function bf(a){a=a|0;var b=0;H[a>>2]=2448;b=H[a+20>>2];H[a+20>>2]=0;if(b){ea[H[H[b>>2]+4>>2]](b)}H[a>>2]=2232;b=H[a+16>>2];H[a+16>>2]=0;if(b){Ga(b)}oa(a)}function wh(){var a=0,b=0;b=pa(40);H[b>>2]=-1;a=b+8|0;H[a+16>>2]=0;H[a+20>>2]=0;H[a+8>>2]=0;H[a>>2]=0;H[a+4>>2]=0;H[a+24>>2]=0;H[a+28>>2]=0;return b|0}function gf(a,b){a=a|0;b=b|0;var c=0,d=0;d=H[a+4>>2];a:{if(d){c=1;if(I[d+36|0]<2){break a}}c=ea[H[H[a>>2]+48>>2]](a,H[b+4>>2]-H[b>>2]>>2)|0}return c|0}function ci(a){a=a|0;var b=0;H[a>>2]=11872;b=H[a+48>>2];if(b){H[a+52>>2]=b;oa(b)}H[a>>2]=12124;b=H[a+36>>2];if(b){oa(b)}b=H[a+24>>2];if(b){oa(b)}oa(a)}function Mh(a){a=a|0;var b=0;H[a>>2]=12804;b=H[a+48>>2];if(b){H[a+52>>2]=b;oa(b)}H[a>>2]=12620;b=H[a+36>>2];if(b){oa(b)}b=H[a+24>>2];if(b){oa(b)}oa(a)}function Ha(a){H[a+8>>2]=0;H[a+12>>2]=0;H[a>>2]=0;H[a+16>>2]=0;H[a+20>>2]=0;H[a+32>>2]=0;H[a+24>>2]=0;H[a+28>>2]=0;G[a+38>>1]=0;F[a+36|0]=0;return a}function Hf(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;if(Ya(a,H[b+8>>2],f)){Cd(b,c,d,e);return}a=H[a+8>>2];ea[H[H[a>>2]+20>>2]](a,b,c,d,e,f)}function Ei(a,b,c){a=a|0;b=b|0;c=c|0;a:{if(I[H[a+4>>2]+36|0]>=2){b=0;if(!(ea[H[H[a>>2]+52>>2]](a)|0)){break a}}b=Xc(a+24|0,H[a+16>>2])}return b|0}function Fi(a,b,c){a=a|0;b=b|0;c=c|0;var d=0;a:{if(I[H[a+4>>2]+36|0]<=1){d=0;if(!(ea[H[H[a>>2]+52>>2]](a)|0)){break a}}d=nd(a,b,c)}return d|0}function gh(){var a=0;a=_d(pa(108));H[a+84>>2]=0;H[a+88>>2]=0;H[a>>2]=13664;H[a+92>>2]=0;H[a+96>>2]=0;H[a+100>>2]=0;H[a+104>>2]=0;return a|0}function Zd(a,b){var c=0;c=-1;a:{if((b|0)==-1|(b|0)>4){break a}b=N(b,12)+a|0;a=H[b+20>>2];if((H[b+24>>2]-a|0)<=0){break a}c=H[a>>2]}return c}function lc(a,b,c,d,e,f,g){H[a>>2]=0;H[a+56>>2]=b;H[a+48>>2]=0;H[a+52>>2]=0;H[a+40>>2]=f;H[a+44>>2]=g;F[a+32|0]=e;H[a+28>>2]=d;F[a+24|0]=c}function aj(a,b,c){a=a|0;b=b|0;c=c|0;var d=0;a:{if(I[H[a+4>>2]+36|0]<=1){d=0;if(!xc(a+24|0,H[a+8>>2],c)){break a}}d=nd(a,b,c)}return d|0}function $i(a,b,c){a=a|0;b=b|0;c=c|0;a:{if(I[H[a+4>>2]+36|0]>=2){b=0;if(!xc(a+24|0,rb(a),c)){break a}}b=Xc(a+24|0,H[a+16>>2])}return b|0}function Yf(a){a=a|0;var b=0;H[a>>2]=13664;b=H[a+96>>2];if(b){H[a+100>>2]=b;oa(b)}b=H[a+84>>2];if(b){H[a+88>>2]=b;oa(b)}return _b(a)|0}function Dc(a){var b=0;if(I[a+11|0]>>>7|0){b=H[a+4>>2]}else{b=I[a+11|0]&127}if(!b){af(1232);v()}if(I[a+11|0]>>>7|0){a=H[a>>2]}return a}function Xf(a){a=a|0;var b=0;H[a>>2]=13664;b=H[a+96>>2];if(b){H[a+100>>2]=b;oa(b)}b=H[a+84>>2];if(b){H[a+88>>2]=b;oa(b)}oa(_b(a))}function zj(a){a=a|0;var b=0;H[a>>2]=4040;b=H[a+76>>2];if(b){oa(b)}H[a>>2]=3272;b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}return a|0}function jj(a){a=a|0;var b=0;H[a>>2]=6032;b=H[a+76>>2];if(b){oa(b)}H[a>>2]=3272;b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}return a|0}function Qa(a,b){if(b){Qa(a,H[b>>2]);Qa(a,H[b+4>>2]);a=H[b+28>>2];if(a){H[b+32>>2]=a;oa(a)}if(F[b+27|0]<0){oa(H[b+16>>2])}oa(b)}}function Vg(){var a=0;a=pa(28);H[a>>2]=0;H[a+4>>2]=0;H[a+24>>2]=0;H[a+16>>2]=0;H[a+20>>2]=0;H[a+8>>2]=0;H[a+12>>2]=0;return a|0}function wg(a){a=a|0;var b=0;H[a>>2]=1984;b=H[a+16>>2];if(b){H[a+20>>2]=b;oa(b)}b=H[a+4>>2];if(b){H[a+8>>2]=b;oa(b)}return a|0}function eh(){var a=0,b=0;a=pa(24);H[a+4>>2]=0;H[a+8>>2]=0;b=a+16|0;H[b>>2]=0;H[b+4>>2]=0;H[a>>2]=a+4;H[a+12>>2]=b;return a|0}function Kf(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;if(Ya(a,H[b+8>>2],0)){Dd(b,c,d);return}a=H[a+8>>2];ea[H[H[a>>2]+28>>2]](a,b,c,d)}function yj(a){a=a|0;var b=0;H[a>>2]=4040;b=H[a+76>>2];if(b){oa(b)}H[a>>2]=3272;b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}oa(a)}function ij(a){a=a|0;var b=0;H[a>>2]=6032;b=H[a+76>>2];if(b){oa(b)}H[a>>2]=3272;b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}oa(a)}function pa(a){var b=0;a=a?a:1;a:{while(1){b=Ec(a);if(b){break a}b=H[4422];if(b){ea[b|0]();continue}break}X();v()}return b}function Kb(a,b){if(b){Kb(a,H[b>>2]);Kb(a,H[b+4>>2]);if(F[b+39|0]<0){oa(H[b+28>>2])}if(F[b+27|0]<0){oa(H[b+16>>2])}oa(b)}}function Ad(a){a=a|0;var b=0,c=0;H[a>>2]=15372;b=H[a+4>>2]-12|0;c=H[b+8>>2]-1|0;H[b+8>>2]=c;if((c|0)<0){oa(b)}return a|0}function lh(){var a=0;a=pa(24);H[a+8>>2]=0;H[a+12>>2]=0;H[a+4>>2]=-1;H[a>>2]=1832;H[a+16>>2]=0;H[a+20>>2]=0;return a|0}function pd(a,b,c){a=a|0;b=b|0;c=c|0;H[a+4>>2]=b;b=H[H[H[b+4>>2]+8>>2]+(c<<2)>>2];H[a+12>>2]=c;H[a+8>>2]=b;return 1}function wc(a){a=a|0;var b=0;if(!(!H[a+60>>2]|!H[a+44>>2]|(!H[a+48>>2]|!H[a+52>>2]))){b=H[a+56>>2]!=0}return b|0}function Id(a,b){if(I[a+11|0]>>>7|0){H[a+4>>2]=b;return}F[a+11|0]=I[a+11|0]&128|b;F[a+11|0]=I[a+11|0]&127}function wj(a){a=a|0;var b=0;H[a>>2]=4276;H[a>>2]=3272;b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}return a|0}function fj(a){a=a|0;var b=0;H[a>>2]=6256;H[a>>2]=3272;b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}return a|0}function bi(a){a=a|0;var b=0;H[a>>2]=12124;b=H[a+36>>2];if(b){oa(b)}b=H[a+24>>2];if(b){oa(b)}return a|0}function Uh(a){a=a|0;var b=0;H[a>>2]=12620;b=H[a+36>>2];if(b){oa(b)}b=H[a+24>>2];if(b){oa(b)}return a|0}function lg(a){a=a|0;if(a){if(F[a+39|0]<0){oa(H[a+28>>2])}Oc(a+12|0,H[a+16>>2]);Kb(a,H[a+4>>2]);oa(a)}}function Pb(a){a=a|0;var b=0;if(!(!H[a+52>>2]|(!H[a+44>>2]|!H[a+48>>2]))){b=H[a+56>>2]!=0}return b|0}function vj(a){a=a|0;var b=0;H[a>>2]=4276;H[a>>2]=3272;b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}oa(a)}function vc(a,b){a=a|0;b=b|0;var c=0;if(!(H[b+56>>2]|!b|I[b+24|0]!=3)){H[a+60>>2]=b;c=1}return c|0}function ej(a){a=a|0;var b=0;H[a>>2]=6256;H[a>>2]=3272;b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}oa(a)}function ai(a){a=a|0;var b=0;H[a>>2]=12124;b=H[a+36>>2];if(b){oa(b)}b=H[a+24>>2];if(b){oa(b)}oa(a)}function Th(a){a=a|0;var b=0;H[a>>2]=12620;b=H[a+36>>2];if(b){oa(b)}b=H[a+24>>2];if(b){oa(b)}oa(a)}function xh(a,b,c){a=a|0;b=b|0;c=c|0;H[a+16>>2]=0;H[a+20>>2]=0;H[a>>2]=b;H[a+8>>2]=c;H[a+12>>2]=0}function We(a,b){a=a|0;b=b|0;var c=0;if(!(H[b+56>>2]|I[b+24|0]!=3)){H[a- -64>>2]=b;c=1}return c|0}function yc(a){var b=0;b=H[a+16>>2];if(b){H[a+20>>2]=b;oa(b)}b=H[a>>2];if(b){H[a+4>>2]=b;oa(b)}}function sc(a,b){a=a|0;b=b|0;var c=0;if(!(H[b+56>>2]|I[b+24|0]!=3)){H[a+48>>2]=b;c=1}return c|0}function Gf(a,b,c,d,e,f){a=a|0;b=b|0;c=c|0;d=d|0;e=e|0;f=f|0;if(Ya(a,H[b+8>>2],f)){Cd(b,c,d,e)}}function wa(){var a=0;a=Bc(4);H[a>>2]=15260;H[a>>2]=15220;H[a>>2]=15240;Y(a|0,15352,14);v()}function sf(a){a=a|0;var b=0;H[a>>2]=2232;b=H[a+16>>2];H[a+16>>2]=0;if(b){Ga(b)}return a|0}function Kj(a){a=a|0;var b=0;H[a>>2]=3272;b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}return a|0}function mi(a){a=a|0;var b=0;H[a>>2]=1832;b=H[a+8>>2];if(b){H[a+12>>2]=b;oa(b)}return a|0}function Ci(a){a=a|0;var b=0;b=rb(a);return Je(a+24|0,b?b:H[a+8>>2],H[H[a+4>>2]+32>>2])|0}function rf(a){a=a|0;var b=0;H[a>>2]=2232;b=H[a+16>>2];H[a+16>>2]=0;if(b){Ga(b)}oa(a)}function ji(a){a=a|0;var b=0;H[a>>2]=1832;b=H[a+8>>2];if(b){H[a+12>>2]=b;oa(b)}oa(a)} +function Ub(a){a=a|0;var b=0;H[a>>2]=3272;b=H[a+32>>2];if(b){H[a+36>>2]=b;oa(b)}oa(a)}function Za(a){var b=0;H[a+16>>2]=0;b=H[a>>2];H[a+4>>2]=b;H[a+12>>2]=b;if(b){oa(b)}}function Oc(a,b){if(b){Oc(a,H[b>>2]);Oc(a,H[b+4>>2]);Kb(b+20|0,H[b+24>>2]);oa(b)}}function wi(a){a=a|0;if(!H[a+44>>2]){return 0}return ea[H[H[a>>2]+48>>2]](a)|0}function vh(a){a=a|0;var b=0;if(a){b=H[a+8>>2];if(b){H[a+12>>2]=b;oa(b)}oa(a)}}function Uj(a){var b=0;while(1){if(a){a=a-1&a;b=b+1|0;continue}break}return b}function Lf(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;if(Ya(a,H[b+8>>2],0)){Dd(b,c,d)}}function ui(a,b){a=a|0;b=b|0;a=H[a+48>>2];return ea[H[H[a>>2]+20>>2]](a,b)|0}function ni(a,b){a=a|0;b=b|0;a=H[a+48>>2];return ea[H[H[a>>2]+12>>2]](a,b)|0}function li(a,b){a=a|0;b=b|0;a=H[a+48>>2];return ea[H[H[a>>2]+16>>2]](a,b)|0}function lb(){var a=0;a=pa(12);H[a>>2]=0;H[a+4>>2]=0;H[a+8>>2]=0;return a|0}function kb(a){a=a|0;var b=0;if(a){b=H[a>>2];if(b){H[a+4>>2]=b;oa(b)}oa(a)}}function Vj(a){var b=0;b=a&31;a=0-a&31;return(-1>>>b&-2)<>>a} +function dh(a,b,c){a=a|0;b=b|0;c=c|0;H[a+32>>2]=c;H[a+28>>2]=b;return 1}function ch(a){a=a|0;if(a){Ra(a+12|0,H[a+16>>2]);Qa(a,H[a+4>>2]);oa(a)}}function Rd(a,b,c){a:{if(H[c+76>>2]<0){a=Fc(a,b,c);break a}a=Fc(a,b,c)}}function Mb(a,b){a=a|0;b=b|0;if(b>>>0<=1){H[a+28>>2]=b}return b>>>0<2|0}function Fh(a,b){a=a|0;b=b|0;F[b+84|0]=1;H[b+72>>2]=H[b+68>>2];return 1}function si(a){a=a|0;a=H[a+48>>2];return ea[H[H[a>>2]+24>>2]](a)|0}function ri(a){a=a|0;a=H[a+48>>2];return ea[H[H[a>>2]+28>>2]](a)|0}function oi(a){a=a|0;a=H[a+48>>2];return ea[H[H[a>>2]+36>>2]](a)|0}function ih(){var a=0;a=pa(8);H[a+4>>2]=-1;H[a>>2]=1032;return a|0}function Gg(a,b,c){a=a|0;b=b|0;c=c|0;return H[H[b+8>>2]+(c<<2)>>2]}function _i(a,b){a=a|0;b=b|0;return Fd(a+24|0,rb(a),H[a+8>>2])|0}function Bi(a,b){a=a|0;b=b|0;return Re(a+24|0,rb(a),H[a+8>>2])|0}function xf(a,b){a=a|0;b=b|0;H[a+12>>2]=-1;H[a+8>>2]=b;return 1}function ne(a,b){a=a|0;b=b|0;return ea[H[H[a>>2]+12>>2]](a,b)|0}function Ff(a){a=a|0;if(!a){return 0}return(Ed(a,15068)|0)!=0|0}function Di(a,b){a=a|0;b=b|0;return ea[H[H[a>>2]+56>>2]](a,b)|0}function $g(a){a=a|0;if(a){if(F[a+15|0]<0){oa(H[a+4>>2])}oa(a)}}function kh(a,b){a=a|0;b=b|0;return O(L[H[a+8>>2]+(b<<2)>>2])}function af(a){a=Hd(Bc(8),a);H[a>>2]=15472;Y(a|0,15504,1);v()}function Ue(a){a=Hd(Bc(8),a);H[a>>2]=15420;Y(a|0,15452,1);v()}function _g(a,b){a=a|0;b=b|0;return O(L[H[a>>2]+(b<<2)>>2])}function fh(a){a=a|0;return(H[a+100>>2]-H[a+96>>2]|0)/12|0}function ah(a){a=a|0;return(F[a+15|0]<0?H[a+4>>2]:a+4|0)|0}function _f(a,b){a=a|0;b=b|0;return H[H[a+4>>2]+(b<<2)>>2]}function Pf(a,b,c,d){a=a|0;b=b|0;c=c|0;d=d|0;da=0;return 0}function Ke(a){a=Vc(a);H[a+44>>2]=0;H[a>>2]=11180;return a}function ie(a,b){a=a|0;b=b|0;return H[H[a>>2]+(b<<2)>>2]}function Xg(a,b){a=a|0;b=b|0;return G[H[a>>2]+(b<<1)>>1]}function Wg(a,b){a=a|0;b=b|0;return J[H[a>>2]+(b<<1)>>1]}function Zb(a,b){var c=0;c=pa(b);H[a+4>>2]=b;H[a>>2]=c}function Jg(a,b,c){a=a|0;b=b|0;c=c|0;return Zd(b,c)|0}function _d(a){H[a>>2]=13724;ra(a+4|0,0,80);return a}function me(a){a=a|0;return H[a+12>>2]-H[a+8>>2]>>2}function Qj(a){if(a){return 31-Q(a-1^a)|0}return 32}function cc(a){a=a|0;if(a){ea[H[H[a>>2]+4>>2]](a)}}function Zg(a,b){a=a|0;b=b|0;return F[H[a>>2]+b|0]}function Yg(a,b){a=a|0;b=b|0;return I[H[a>>2]+b|0]}function Uf(a){a=a|0;return H[a+8>>2]-H[a+4>>2]>>2}function jd(a,b){a=a|0;b=b|0;H[a+4>>2]=b;return 1}function je(a){a=a|0;return H[a+4>>2]-H[a>>2]>>1}function Qc(a){a=a|0;return H[a+4>>2]-H[a>>2]>>2}function le(a){a=a|0;return H[a+4>>2]-H[a>>2]|0}function Ab(a,b,c){if(!(I[a|0]&32)){Fc(b,c,a)}}function vf(a,b,c){a=a|0;b=b|0;c=c|0;return 1}function hi(a,b){a=a|0;b=b|0;return I[b+24|0]}function Pg(a,b){a=a|0;b=b|0;return H[b+8>>2]}function Nj(a){a=a|0;return I[H[a+8>>2]+24|0]}function Li(a){a=a|0;H[a>>2]=10032;return a|0}function Eg(a,b){a=a|0;b=b|0;return H[b+4>>2]}function Yi(a){a=a|0;H[a>>2]=7144;return a|0}function Ui(a){a=a|0;H[a>>2]=8080;return a|0}function Sf(a){a=a|0;return aa(H[a+60>>2])|0}function Pi(a){a=a|0;H[a>>2]=9028;return a|0}function jh(a){a=a|0;return O(L[a+20>>2])}function Ji(a){a=a|0;H[a>>2]=10032;oa(a)}function Xi(a){a=a|0;H[a>>2]=7144;oa(a)}function Si(a){a=a|0;H[a>>2]=8080;oa(a)}function Oi(a){a=a|0;H[a>>2]=9028;oa(a)}function sh(a){a=a|0;return H[a+88>>2]}function rh(a){a=a|0;return H[a+56>>2]}function oh(a){a=a|0;return H[a+40>>2]}function nh(a){a=a|0;return H[a+48>>2]}function mh(a){a=a|0;return H[a+60>>2]}function eb(a){a=a|0;return H[a+28>>2]}function df(){H[4292]=17048;H[4274]=42}function Rc(a){a=a|0;return H[a+80>>2]}function qh(a){a=a|0;return F[a+24|0]}function ph(a){a=a|0;return I[a+32|0]}function md(a,b){a=a|0;b=b|0;return-1}function db(a){a=a|0;return H[a+4>>2]}function bh(a){a=a|0;return!H[a>>2]|0}function _e(a,b){a=a|0;b=b|0;return 6}function Zc(a){a=a|0;return H[a+8>>2]}function Pd(a,b){a=a|0;b=b|0;return 1}function Ja(a,b){a=a|0;b=b|0;return 0}function Bj(a,b){a=a|0;b=b|0;return 2}function Bc(a){return Ec(a+80|0)+80|0}function pe(a){a=a|0;return H[a>>2]}function yh(){return Ha(pa(40))|0}function uh(){return Eb(pa(64))|0}function hh(){return _d(pa(84))|0}function Sc(a){a=a|0;if(a){oa(a)}}function zc(a){a=a|0;Ad(a);oa(a)}function Ef(a){a=a|0;return 1171}function Df(a){a=a|0;return 1245}function Cf(a){a=a|0;return 1211}function Ta(a){a=a|0;return a|0}function yf(a){a=a|0;oa(rd(a))}function fi(a){a=a|0;oa(Be(a))}function ei(a){a=a|0;oa(Ae(a))}function di(a){a=a|0;oa(ze(a))}function Tf(a){a=a|0;oa(_b(a))}function ld(a){a=a|0;return 3}function _a(a){a=a|0;return 0}function Ze(a){a=a|0;return 5}function Tb(a){a=a|0;return 2}function Ob(a){a=a|0;return 6}function Da(a){a=a|0;return 1}function $e(a){a=a|0;return 4}function sa(){Ue(1164);v()}function Na(){Ue(1232);v()}function La(a){a=a|0;oa(a)}function Ca(){af(1164);v()}function fb(a){a=a|0;v()}function eg(){return 10}function dg(){return 11}function cg(){return 12}function kg(){return 5}function ig(){return 6}function hg(){return 7}function gg(){return 8}function fg(){return 9}function fe(){return 3}function ee(){return 4}function bg(){return-2}function bc(){return-1}function ag(){return-3}function ac(){return 1}function Zf(){return-5}function Qb(){return 0}function Nc(){return 2}function $f(){return-4}function Nf(){X();v()}function Td(a){a=a|0} +// EMSCRIPTEN_END_FUNCS +e=I;p(q);var ea=c([null,Ad,Ta,La,Tb,Pj,zi,Gh,Fd,Bf,xc,Nh,_e,Bj,Ta,mi,ji,Da,gj,Ti,Ki,Re,xi,Je,_e,hi,wg,fb,dh,ke,jg,_f,Uf,eb,Ja,Nf,Pd,Da,rd,yf,Of,Af,zf,sf,rf,pd,xf,wf,vf,Pd,uf,tf,kf,jf,qf,pf,hf,of,nf,mf,lf,cf,bf,pd,gf,ff,nd,ef,Nj,Oj,Kj,Ub,Da,db,Pb,_a,md,Ja,_a,Da,Mj,Lj,fb,fb,Ub,Tb,Pb,Jj,Ij,Hj,$e,Pb,Gj,Fj,Ej,Dj,ld,wc,Da,Ja,vc,Cj,Aj,zj,yj,Ze,wc,Da,Ja,vc,Ye,xj,wj,vj,Ob,Xe,Da,Ja,We,Ve,uj,Ta,La,Mb,eb,Nb,fb,Ub,Da,Pb,tj,fb,Ub,Tb,Pb,sj,rj,qj,$e,Pb,pj,oj,nj,mj,ld,wc,Da,Ja,vc,lj,kj,jj,ij,Ze,wc,Da,Ja,vc,Ye,hj,fj,ej,Ob,Xe,Da,Ja,We,Ve,dj,Ta,La,Mb,eb,Lb,fb,Ub,_a,Da,cj,cf,bf,bj,$i,aj,Zi,Tb,_i,Yi,Xi,Ob,db,tc,Da,Ja,sc,Da,Tb,Te,Wi,Ta,La,Mb,eb,Nb,Ui,Si,Ob,tc,Da,Ja,sc,Te,Ri,Ta,La,Mb,eb,Lb,Ta,La,_a,Da,_a,md,Ja,Vi,Qi,Pi,Oi,Ob,db,tc,Da,Ja,sc,Da,ld,Se,Ni,Ta,La,Mb,eb,Nb,Li,Ji,Ob,tc,Da,Ja,sc,Se,Ii,Ta,La,Mb,eb,Lb,La,_a,Da,_a,md,Ja,Mi,Hi,Ai,yi,Gi,Ei,Fi,Di,Ci,Bi,vi,fb,Da,Da,wi,Dh,Ch,Da,_a,Ja,Ja,qi,pi,ti,ui,ri,oi,ni,li,si,Be,fi,jd,id,hd,gd,ki,Da,db,Zc,Ae,ei,jd,id,hd,gd,ii,Da,db,Zc,ze,di,jd,id,hd,gd,gi,Da,db,Zc,He,ci,Ie,bi,ai,Zh,Yh,Xh,Wh,_h,Vh,$h,Uh,Th,Rh,Qh,Ph,Oh,Sh,Mh,Lh,Kh,Jh,Ih,Wc,ve,Hh,Ta,La,Fh,Eh,fb,_a,Da,Wc,Ah,Bh,Wc,ve,zh,Yf,Xf,Wf,Vf,_b,Tf,Xd,Wd,Sf,Rf,Qf,_a,Pf,Ta,La,Td,Td,Mf,Gf,If,Lf,La,Hf,Jf,Kf,La,Df,La,Cf,La,Ef,zc,db,zc,zc]);function fa(){return E.byteLength/65536|0}function ka(la){la=la|0;var ga=fa()|0;var ha=ga+la|0;if(ga=endIdx))++endPtr;if(endPtr-idx>16&&heapOrArray.buffer&&UTF8Decoder){return UTF8Decoder.decode(heapOrArray.subarray(idx,endPtr))}var str="";while(idx>10,56320|ch&1023)}}return str}function UTF8ToString(ptr,maxBytesToRead){return ptr?UTF8ArrayToString(HEAPU8,ptr,maxBytesToRead):""}function stringToUTF8Array(str,heap,outIdx,maxBytesToWrite){if(!(maxBytesToWrite>0))return 0;var startIdx=outIdx;var endIdx=outIdx+maxBytesToWrite-1;for(var i=0;i=55296&&u<=57343){var u1=str.charCodeAt(++i);u=65536+((u&1023)<<10)|u1&1023}if(u<=127){if(outIdx>=endIdx)break;heap[outIdx++]=u}else if(u<=2047){if(outIdx+1>=endIdx)break;heap[outIdx++]=192|u>>6;heap[outIdx++]=128|u&63}else if(u<=65535){if(outIdx+2>=endIdx)break;heap[outIdx++]=224|u>>12;heap[outIdx++]=128|u>>6&63;heap[outIdx++]=128|u&63}else{if(outIdx+3>=endIdx)break;heap[outIdx++]=240|u>>18;heap[outIdx++]=128|u>>12&63;heap[outIdx++]=128|u>>6&63;heap[outIdx++]=128|u&63}}heap[outIdx]=0;return outIdx-startIdx}function lengthBytesUTF8(str){var len=0;for(var i=0;i=55296&&c<=57343){len+=4;++i}else{len+=3}}return len}var HEAP8,HEAPU8,HEAP16,HEAPU16,HEAP32,HEAPU32,HEAPF32,HEAPF64;function updateMemoryViews(){var b=wasmMemory.buffer;Module["HEAP8"]=HEAP8=new Int8Array(b);Module["HEAP16"]=HEAP16=new Int16Array(b);Module["HEAP32"]=HEAP32=new Int32Array(b);Module["HEAPU8"]=HEAPU8=new Uint8Array(b);Module["HEAPU16"]=HEAPU16=new Uint16Array(b);Module["HEAPU32"]=HEAPU32=new Uint32Array(b);Module["HEAPF32"]=HEAPF32=new Float32Array(b);Module["HEAPF64"]=HEAPF64=new Float64Array(b)}var INITIAL_MEMORY=Module["INITIAL_MEMORY"]||16777216;assert(INITIAL_MEMORY>=65536,"INITIAL_MEMORY should be larger than STACK_SIZE, was "+INITIAL_MEMORY+"! (STACK_SIZE="+65536+")");if(Module["wasmMemory"]){wasmMemory=Module["wasmMemory"]}else{wasmMemory=new WebAssembly.Memory({"initial":INITIAL_MEMORY/65536,"maximum":2147483648/65536})}updateMemoryViews();INITIAL_MEMORY=wasmMemory.buffer.byteLength;var wasmTable;var __ATPRERUN__=[];var __ATINIT__=[];var __ATPOSTRUN__=[];var runtimeInitialized=false;function keepRuntimeAlive(){return noExitRuntime}function preRun(){if(Module["preRun"]){if(typeof Module["preRun"]=="function")Module["preRun"]=[Module["preRun"]];while(Module["preRun"].length){addOnPreRun(Module["preRun"].shift())}}callRuntimeCallbacks(__ATPRERUN__)}function initRuntime(){runtimeInitialized=true;callRuntimeCallbacks(__ATINIT__)}function postRun(){if(Module["postRun"]){if(typeof Module["postRun"]=="function")Module["postRun"]=[Module["postRun"]];while(Module["postRun"].length){addOnPostRun(Module["postRun"].shift())}}callRuntimeCallbacks(__ATPOSTRUN__)}function addOnPreRun(cb){__ATPRERUN__.unshift(cb)}function addOnInit(cb){__ATINIT__.unshift(cb)}function addOnPostRun(cb){__ATPOSTRUN__.unshift(cb)}var runDependencies=0;var runDependencyWatcher=null;var dependenciesFulfilled=null;function addRunDependency(id){runDependencies++;if(Module["monitorRunDependencies"]){Module["monitorRunDependencies"](runDependencies)}}function removeRunDependency(id){runDependencies--;if(Module["monitorRunDependencies"]){Module["monitorRunDependencies"](runDependencies)}if(runDependencies==0){if(runDependencyWatcher!==null){clearInterval(runDependencyWatcher);runDependencyWatcher=null}if(dependenciesFulfilled){var callback=dependenciesFulfilled;dependenciesFulfilled=null;callback()}}}function abort(what){if(Module["onAbort"]){Module["onAbort"](what)}what="Aborted("+what+")";err(what);ABORT=true;EXITSTATUS=1;what+=". Build with -sASSERTIONS for more info.";var e=new WebAssembly.RuntimeError(what);readyPromiseReject(e);throw e}var dataURIPrefix="data:application/octet-stream;base64,";function isDataURI(filename){return filename.startsWith(dataURIPrefix)}function isFileURI(filename){return filename.startsWith("file://")}var wasmBinaryFile;wasmBinaryFile="draco_decoder.wasm";if(!isDataURI(wasmBinaryFile)){wasmBinaryFile=locateFile(wasmBinaryFile)}function getBinary(file){try{if(file==wasmBinaryFile&&wasmBinary){return new Uint8Array(wasmBinary)}var binary=tryParseAsDataURI(file);if(binary){return binary}if(readBinary){return readBinary(file)}throw"both async and sync fetching of the wasm failed"}catch(err){abort(err)}}function getBinaryPromise(){if(!wasmBinary&&(ENVIRONMENT_IS_WEB||ENVIRONMENT_IS_WORKER)){if(typeof fetch=="function"&&!isFileURI(wasmBinaryFile)){return fetch(wasmBinaryFile,{credentials:"same-origin"}).then(function(response){if(!response["ok"]){throw"failed to load wasm binary file at '"+wasmBinaryFile+"'"}return response["arrayBuffer"]()}).catch(function(){return getBinary(wasmBinaryFile)})}else{if(readAsync){return new Promise(function(resolve,reject){readAsync(wasmBinaryFile,function(response){resolve(new Uint8Array(response))},reject)})}}}return Promise.resolve().then(function(){return getBinary(wasmBinaryFile)})}function createWasm(){var info={"a":wasmImports};function receiveInstance(instance,module){var exports=instance.exports;Module["asm"]=exports;wasmTable=Module["asm"]["j"];addOnInit(Module["asm"]["i"]);removeRunDependency("wasm-instantiate")}addRunDependency("wasm-instantiate");function receiveInstantiationResult(result){receiveInstance(result["instance"])}function instantiateArrayBuffer(receiver){return getBinaryPromise().then(function(binary){return WebAssembly.instantiate(binary,info)}).then(function(instance){return instance}).then(receiver,function(reason){err("failed to asynchronously prepare wasm: "+reason);abort(reason)})}function instantiateAsync(){if(!wasmBinary&&typeof WebAssembly.instantiateStreaming=="function"&&!isDataURI(wasmBinaryFile)&&!isFileURI(wasmBinaryFile)&&!ENVIRONMENT_IS_NODE&&typeof fetch=="function"){return fetch(wasmBinaryFile,{credentials:"same-origin"}).then(function(response){var result=WebAssembly.instantiateStreaming(response,info);return result.then(receiveInstantiationResult,function(reason){err("wasm streaming compile failed: "+reason);err("falling back to ArrayBuffer instantiation");return instantiateArrayBuffer(receiveInstantiationResult)})})}else{return instantiateArrayBuffer(receiveInstantiationResult)}}if(Module["instantiateWasm"]){try{var exports=Module["instantiateWasm"](info,receiveInstance);return exports}catch(e){err("Module.instantiateWasm callback failed with error: "+e);readyPromiseReject(e)}}instantiateAsync().catch(readyPromiseReject);return{}}function ExitStatus(status){this.name="ExitStatus";this.message="Program terminated with exit("+status+")";this.status=status}function callRuntimeCallbacks(callbacks){while(callbacks.length>0){callbacks.shift()(Module)}}function intArrayToString(array){var ret=[];for(var i=0;i255){chr&=255}ret.push(String.fromCharCode(chr))}return ret.join("")}function ExceptionInfo(excPtr){this.excPtr=excPtr;this.ptr=excPtr-24;this.set_type=function(type){HEAPU32[this.ptr+4>>2]=type};this.get_type=function(){return HEAPU32[this.ptr+4>>2]};this.set_destructor=function(destructor){HEAPU32[this.ptr+8>>2]=destructor};this.get_destructor=function(){return HEAPU32[this.ptr+8>>2]};this.set_refcount=function(refcount){HEAP32[this.ptr>>2]=refcount};this.set_caught=function(caught){caught=caught?1:0;HEAP8[this.ptr+12>>0]=caught};this.get_caught=function(){return HEAP8[this.ptr+12>>0]!=0};this.set_rethrown=function(rethrown){rethrown=rethrown?1:0;HEAP8[this.ptr+13>>0]=rethrown};this.get_rethrown=function(){return HEAP8[this.ptr+13>>0]!=0};this.init=function(type,destructor){this.set_adjusted_ptr(0);this.set_type(type);this.set_destructor(destructor);this.set_refcount(0);this.set_caught(false);this.set_rethrown(false)};this.add_ref=function(){var value=HEAP32[this.ptr>>2];HEAP32[this.ptr>>2]=value+1};this.release_ref=function(){var prev=HEAP32[this.ptr>>2];HEAP32[this.ptr>>2]=prev-1;return prev===1};this.set_adjusted_ptr=function(adjustedPtr){HEAPU32[this.ptr+16>>2]=adjustedPtr};this.get_adjusted_ptr=function(){return HEAPU32[this.ptr+16>>2]};this.get_exception_ptr=function(){var isPointer=___cxa_is_pointer_type(this.get_type());if(isPointer){return HEAPU32[this.excPtr>>2]}var adjusted=this.get_adjusted_ptr();if(adjusted!==0)return adjusted;return this.excPtr}}var exceptionLast=0;var uncaughtExceptionCount=0;function ___cxa_throw(ptr,type,destructor){var info=new ExceptionInfo(ptr);info.init(type,destructor);exceptionLast=ptr;uncaughtExceptionCount++;throw ptr}function _abort(){abort("")}function _emscripten_memcpy_big(dest,src,num){HEAPU8.copyWithin(dest,src,src+num)}function getHeapMax(){return 2147483648}function emscripten_realloc_buffer(size){var b=wasmMemory.buffer;try{wasmMemory.grow(size-b.byteLength+65535>>>16);updateMemoryViews();return 1}catch(e){}}function _emscripten_resize_heap(requestedSize){var oldSize=HEAPU8.length;requestedSize=requestedSize>>>0;var maxHeapSize=getHeapMax();if(requestedSize>maxHeapSize){return false}let alignUp=(x,multiple)=>x+(multiple-x%multiple)%multiple;for(var cutDown=1;cutDown<=4;cutDown*=2){var overGrownHeapSize=oldSize*(1+.2/cutDown);overGrownHeapSize=Math.min(overGrownHeapSize,requestedSize+100663296);var newSize=Math.min(maxHeapSize,alignUp(Math.max(requestedSize,overGrownHeapSize),65536));var replacement=emscripten_realloc_buffer(newSize);if(replacement){return true}}return false}var SYSCALLS={varargs:undefined,get:function(){SYSCALLS.varargs+=4;var ret=HEAP32[SYSCALLS.varargs-4>>2];return ret},getStr:function(ptr){var ret=UTF8ToString(ptr);return ret}};function _fd_close(fd){return 52}function _fd_seek(fd,offset_low,offset_high,whence,newOffset){return 70}var printCharBuffers=[null,[],[]];function printChar(stream,curr){var buffer=printCharBuffers[stream];if(curr===0||curr===10){(stream===1?out:err)(UTF8ArrayToString(buffer,0));buffer.length=0}else{buffer.push(curr)}}function _fd_write(fd,iov,iovcnt,pnum){var num=0;for(var i=0;i>2];var len=HEAPU32[iov+4>>2];iov+=8;for(var j=0;j>2]=num;return 0}function intArrayFromString(stringy,dontAddNull,length){var len=length>0?length:lengthBytesUTF8(stringy)+1;var u8array=new Array(len);var numBytesWritten=stringToUTF8Array(stringy,u8array,0,u8array.length);if(dontAddNull)u8array.length=numBytesWritten;return u8array}var decodeBase64=typeof atob=="function"?atob:function(input){var keyStr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";var output="";var chr1,chr2,chr3;var enc1,enc2,enc3,enc4;var i=0;input=input.replace(/[^A-Za-z0-9\+\/\=]/g,"");do{enc1=keyStr.indexOf(input.charAt(i++));enc2=keyStr.indexOf(input.charAt(i++));enc3=keyStr.indexOf(input.charAt(i++));enc4=keyStr.indexOf(input.charAt(i++));chr1=enc1<<2|enc2>>4;chr2=(enc2&15)<<4|enc3>>2;chr3=(enc3&3)<<6|enc4;output=output+String.fromCharCode(chr1);if(enc3!==64){output=output+String.fromCharCode(chr2)}if(enc4!==64){output=output+String.fromCharCode(chr3)}}while(i0){return}preRun();if(runDependencies>0){return}function doRun(){if(calledRun)return;calledRun=true;Module["calledRun"]=true;if(ABORT)return;initRuntime();readyPromiseResolve(Module);if(Module["onRuntimeInitialized"])Module["onRuntimeInitialized"]();postRun()}if(Module["setStatus"]){Module["setStatus"]("Running...");setTimeout(function(){setTimeout(function(){Module["setStatus"]("")},1);doRun()},1)}else{doRun()}}if(Module["preInit"]){if(typeof Module["preInit"]=="function")Module["preInit"]=[Module["preInit"]];while(Module["preInit"].length>0){Module["preInit"].pop()()}}run();function WrapperObject(){}WrapperObject.prototype=Object.create(WrapperObject.prototype);WrapperObject.prototype.constructor=WrapperObject;WrapperObject.prototype.__class__=WrapperObject;WrapperObject.__cache__={};Module["WrapperObject"]=WrapperObject;function getCache(__class__){return(__class__||WrapperObject).__cache__}Module["getCache"]=getCache;function wrapPointer(ptr,__class__){var cache=getCache(__class__);var ret=cache[ptr];if(ret)return ret;ret=Object.create((__class__||WrapperObject).prototype);ret.ptr=ptr;return cache[ptr]=ret}Module["wrapPointer"]=wrapPointer;function castObject(obj,__class__){return wrapPointer(obj.ptr,__class__)}Module["castObject"]=castObject;Module["NULL"]=wrapPointer(0);function destroy(obj){if(!obj["__destroy__"])throw"Error: Cannot destroy object. (Did you create it yourself?)";obj["__destroy__"]();delete getCache(obj.__class__)[obj.ptr]}Module["destroy"]=destroy;function compare(obj1,obj2){return obj1.ptr===obj2.ptr}Module["compare"]=compare;function getPointer(obj){return obj.ptr}Module["getPointer"]=getPointer;function getClass(obj){return obj.__class__}Module["getClass"]=getClass;var ensureCache={buffer:0,size:0,pos:0,temps:[],needed:0,prepare:function(){if(ensureCache.needed){for(var i=0;i=ensureCache.size){assert(len>0);ensureCache.needed+=len;ret=Module["_malloc"](len);ensureCache.temps.push(ret)}else{ret=ensureCache.buffer+ensureCache.pos;ensureCache.pos+=len}return ret},copy:function(array,view,offset){offset>>>=0;var bytes=view.BYTES_PER_ELEMENT;switch(bytes){case 2:offset>>>=1;break;case 4:offset>>>=2;break;case 8:offset>>>=3;break}for(var i=0;i>>0,$jscomp.propertyToPolyfillSymbol[h]=$jscomp.IS_SYMBOL_NATIVE? +$jscomp.global.Symbol(h):$jscomp.POLYFILL_PREFIX+l+"$"+h),$jscomp.defineProperty(p,$jscomp.propertyToPolyfillSymbol[h],{configurable:!0,writable:!0,value:n})))}; +$jscomp.polyfill("Promise",function(k){function n(){this.batch_=null}function l(f){return f instanceof h?f:new h(function(q,v){q(f)})}if(k&&(!($jscomp.FORCE_POLYFILL_PROMISE||$jscomp.FORCE_POLYFILL_PROMISE_WHEN_NO_UNHANDLED_REJECTION&&"undefined"===typeof $jscomp.global.PromiseRejectionEvent)||!$jscomp.global.Promise||-1===$jscomp.global.Promise.toString().indexOf("[native code]")))return k;n.prototype.asyncExecute=function(f){if(null==this.batch_){this.batch_=[];var q=this;this.asyncExecuteFunction(function(){q.executeBatch_()})}this.batch_.push(f)}; +var p=$jscomp.global.setTimeout;n.prototype.asyncExecuteFunction=function(f){p(f,0)};n.prototype.executeBatch_=function(){for(;this.batch_&&this.batch_.length;){var f=this.batch_;this.batch_=[];for(var q=0;q=A}},"es6","es3"); +$jscomp.polyfill("Array.prototype.copyWithin",function(k){function n(l){l=Number(l);return Infinity===l||-Infinity===l?l:l|0}return k?k:function(l,p,h){var A=this.length;l=n(l);p=n(p);h=void 0===h?A:n(h);l=0>l?Math.max(A+l,0):Math.min(l,A);p=0>p?Math.max(A+p,0):Math.min(p,A);h=0>h?Math.max(A+h,0):Math.min(h,A);if(lp;)--h in this?this[--l]=this[h]:delete this[--l];return this}},"es6","es3"); +$jscomp.typedArrayCopyWithin=function(k){return k?k:Array.prototype.copyWithin};$jscomp.polyfill("Int8Array.prototype.copyWithin",$jscomp.typedArrayCopyWithin,"es6","es5");$jscomp.polyfill("Uint8Array.prototype.copyWithin",$jscomp.typedArrayCopyWithin,"es6","es5");$jscomp.polyfill("Uint8ClampedArray.prototype.copyWithin",$jscomp.typedArrayCopyWithin,"es6","es5");$jscomp.polyfill("Int16Array.prototype.copyWithin",$jscomp.typedArrayCopyWithin,"es6","es5"); +$jscomp.polyfill("Uint16Array.prototype.copyWithin",$jscomp.typedArrayCopyWithin,"es6","es5");$jscomp.polyfill("Int32Array.prototype.copyWithin",$jscomp.typedArrayCopyWithin,"es6","es5");$jscomp.polyfill("Uint32Array.prototype.copyWithin",$jscomp.typedArrayCopyWithin,"es6","es5");$jscomp.polyfill("Float32Array.prototype.copyWithin",$jscomp.typedArrayCopyWithin,"es6","es5");$jscomp.polyfill("Float64Array.prototype.copyWithin",$jscomp.typedArrayCopyWithin,"es6","es5"); +var DracoDecoderModule=function(){var k="undefined"!==typeof document&&document.currentScript?document.currentScript.src:void 0;"undefined"!==typeof __filename&&(k=k||__filename);return function(n){function l(e){return a.locateFile?a.locateFile(e,U):U+e}function p(e,b,c){var d=b+c;for(c=b;e[c]&&!(c>=d);)++c;if(16g?d+=String.fromCharCode(g):(g-=65536,d+=String.fromCharCode(55296|g>>10,56320|g&1023))}}else d+=String.fromCharCode(g)}return d}function h(e,b){return e?p(ea,e,b):""}function A(){var e=ja.buffer;a.HEAP8=Y=new Int8Array(e);a.HEAP16=new Int16Array(e);a.HEAP32=ca=new Int32Array(e);a.HEAPU8=ea=new Uint8Array(e);a.HEAPU16=new Uint16Array(e);a.HEAPU32=V=new Uint32Array(e);a.HEAPF32=new Float32Array(e);a.HEAPF64=new Float64Array(e)}function f(e){if(a.onAbort)a.onAbort(e); +e="Aborted("+e+")";da(e);wa=!0;e=new WebAssembly.RuntimeError(e+". Build with -sASSERTIONS for more info.");ka(e);throw e;}function q(e){try{if(e==P&&fa)return new Uint8Array(fa);if(ma)return ma(e);throw"both async and sync fetching of the wasm failed";}catch(b){f(b)}}function v(){if(!fa&&(xa||ha)){if("function"==typeof fetch&&!P.startsWith("file://"))return fetch(P,{credentials:"same-origin"}).then(function(e){if(!e.ok)throw"failed to load wasm binary file at '"+P+"'";return e.arrayBuffer()}).catch(function(){return q(P)}); +if(na)return new Promise(function(e,b){na(P,function(c){e(new Uint8Array(c))},b)})}return Promise.resolve().then(function(){return q(P)})}function z(e){for(;0>2]=b};this.get_type=function(){return V[this.ptr+4>>2]};this.set_destructor=function(b){V[this.ptr+8>>2]=b};this.get_destructor=function(){return V[this.ptr+8>>2]};this.set_refcount=function(b){ca[this.ptr>>2]=b};this.set_caught=function(b){Y[this.ptr+ +12>>0]=b?1:0};this.get_caught=function(){return 0!=Y[this.ptr+12>>0]};this.set_rethrown=function(b){Y[this.ptr+13>>0]=b?1:0};this.get_rethrown=function(){return 0!=Y[this.ptr+13>>0]};this.init=function(b,c){this.set_adjusted_ptr(0);this.set_type(b);this.set_destructor(c);this.set_refcount(0);this.set_caught(!1);this.set_rethrown(!1)};this.add_ref=function(){ca[this.ptr>>2]+=1};this.release_ref=function(){var b=ca[this.ptr>>2];ca[this.ptr>>2]=b-1;return 1===b};this.set_adjusted_ptr=function(b){V[this.ptr+ +16>>2]=b};this.get_adjusted_ptr=function(){return V[this.ptr+16>>2]};this.get_exception_ptr=function(){if(ya(this.get_type()))return V[this.excPtr>>2];var b=this.get_adjusted_ptr();return 0!==b?b:this.excPtr}}function ba(){function e(){if(!la&&(la=!0,a.calledRun=!0,!wa)){za=!0;z(oa);Aa(a);if(a.onRuntimeInitialized)a.onRuntimeInitialized();if(a.postRun)for("function"==typeof a.postRun&&(a.postRun=[a.postRun]);a.postRun.length;)Ba.unshift(a.postRun.shift());z(Ba)}}if(!(0=d?b++:2047>=d?b+=2:55296<=d&&57343>= +d?(b+=4,++c):b+=3}b=Array(b+1);c=0;d=b.length;if(0=u){var X=e.charCodeAt(++g);u=65536+((u&1023)<<10)|X&1023}if(127>=u){if(c>=d)break;b[c++]=u}else{if(2047>=u){if(c+1>=d)break;b[c++]=192|u>>6}else{if(65535>=u){if(c+2>=d)break;b[c++]=224|u>>12}else{if(c+3>=d)break;b[c++]=240|u>>18;b[c++]=128|u>>12&63}b[c++]=128|u>>6&63}b[c++]=128|u&63}}b[c]=0}e=r.alloc(b,Y);r.copy(b,Y,e);return e}return e}function pa(e){if("object"===typeof e){var b= +r.alloc(e,Y);r.copy(e,Y,b);return b}return e}function Z(){throw"cannot construct a VoidPtr, no constructor in IDL";}function S(){this.ptr=Da();x(S)[this.ptr]=this}function Q(){this.ptr=Ea();x(Q)[this.ptr]=this}function W(){this.ptr=Fa();x(W)[this.ptr]=this}function w(){this.ptr=Ga();x(w)[this.ptr]=this}function C(){this.ptr=Ha();x(C)[this.ptr]=this}function F(){this.ptr=Ia();x(F)[this.ptr]=this}function G(){this.ptr=Ja();x(G)[this.ptr]=this}function E(){this.ptr=Ka();x(E)[this.ptr]=this}function T(){this.ptr= +La();x(T)[this.ptr]=this}function B(){throw"cannot construct a Status, no constructor in IDL";}function H(){this.ptr=Ma();x(H)[this.ptr]=this}function I(){this.ptr=Na();x(I)[this.ptr]=this}function J(){this.ptr=Oa();x(J)[this.ptr]=this}function K(){this.ptr=Pa();x(K)[this.ptr]=this}function L(){this.ptr=Qa();x(L)[this.ptr]=this}function M(){this.ptr=Ra();x(M)[this.ptr]=this}function N(){this.ptr=Sa();x(N)[this.ptr]=this}function y(){this.ptr=Ta();x(y)[this.ptr]=this}function m(){this.ptr=Ua();x(m)[this.ptr]= +this}n=void 0===n?{}:n;var a="undefined"!=typeof n?n:{},Aa,ka;a.ready=new Promise(function(e,b){Aa=e;ka=b});var Va=!1,Wa=!1;a.onRuntimeInitialized=function(){Va=!0;if(Wa&&"function"===typeof a.onModuleLoaded)a.onModuleLoaded(a)};a.onModuleParsed=function(){Wa=!0;if(Va&&"function"===typeof a.onModuleLoaded)a.onModuleLoaded(a)};a.isVersionSupported=function(e){if("string"!==typeof e)return!1;e=e.split(".");return 2>e.length||3=e[1]?!0:0!=e[0]||10>>=0;if(2147483648=c;c*=2){var d=b*(1+.2/c);d=Math.min(d,e+100663296);var g=Math;d=Math.max(e,d);g=g.min.call(g,2147483648,d+(65536-d%65536)%65536);a:{d=ja.buffer;try{ja.grow(g-d.byteLength+65535>>>16);A();var u=1;break a}catch(X){}u=void 0}if(u)return!0}return!1},f:function(e){return 52},d:function(e,b,c,d,g){return 70},c:function(e,b,c,d){for(var g=0,u=0;u>2],ab=V[b+4>>2];b+=8;for(var sa=0;sa>2]=g;return 0}};(function(){function e(g,u){a.asm=g.exports;ja=a.asm.h;A();oa.unshift(a.asm.i);aa--;a.monitorRunDependencies&&a.monitorRunDependencies(aa);0==aa&&(null!==ra&&(clearInterval(ra),ra=null),ia&&(g=ia,ia=null,g()))}function b(g){e(g.instance)}function c(g){return v().then(function(u){return WebAssembly.instantiate(u,d)}).then(function(u){return u}).then(g,function(u){da("failed to asynchronously prepare wasm: "+u);f(u)})}var d={a:xd};aa++;a.monitorRunDependencies&&a.monitorRunDependencies(aa); +if(a.instantiateWasm)try{return a.instantiateWasm(d,e)}catch(g){da("Module.instantiateWasm callback failed with error: "+g),ka(g)}(function(){return fa||"function"!=typeof WebAssembly.instantiateStreaming||P.startsWith("data:application/octet-stream;base64,")||P.startsWith("file://")||Ya||"function"!=typeof fetch?c(b):fetch(P,{credentials:"same-origin"}).then(function(g){return WebAssembly.instantiateStreaming(g,d).then(b,function(u){da("wasm streaming compile failed: "+u);da("falling back to ArrayBuffer instantiation"); +return c(b)})})})().catch(ka);return{}})();var bb=a._emscripten_bind_VoidPtr___destroy___0=function(){return(bb=a._emscripten_bind_VoidPtr___destroy___0=a.asm.k).apply(null,arguments)},Da=a._emscripten_bind_DecoderBuffer_DecoderBuffer_0=function(){return(Da=a._emscripten_bind_DecoderBuffer_DecoderBuffer_0=a.asm.l).apply(null,arguments)},cb=a._emscripten_bind_DecoderBuffer_Init_2=function(){return(cb=a._emscripten_bind_DecoderBuffer_Init_2=a.asm.m).apply(null,arguments)},db=a._emscripten_bind_DecoderBuffer___destroy___0= +function(){return(db=a._emscripten_bind_DecoderBuffer___destroy___0=a.asm.n).apply(null,arguments)},Ea=a._emscripten_bind_AttributeTransformData_AttributeTransformData_0=function(){return(Ea=a._emscripten_bind_AttributeTransformData_AttributeTransformData_0=a.asm.o).apply(null,arguments)},eb=a._emscripten_bind_AttributeTransformData_transform_type_0=function(){return(eb=a._emscripten_bind_AttributeTransformData_transform_type_0=a.asm.p).apply(null,arguments)},fb=a._emscripten_bind_AttributeTransformData___destroy___0= +function(){return(fb=a._emscripten_bind_AttributeTransformData___destroy___0=a.asm.q).apply(null,arguments)},Fa=a._emscripten_bind_GeometryAttribute_GeometryAttribute_0=function(){return(Fa=a._emscripten_bind_GeometryAttribute_GeometryAttribute_0=a.asm.r).apply(null,arguments)},gb=a._emscripten_bind_GeometryAttribute___destroy___0=function(){return(gb=a._emscripten_bind_GeometryAttribute___destroy___0=a.asm.s).apply(null,arguments)},Ga=a._emscripten_bind_PointAttribute_PointAttribute_0=function(){return(Ga= +a._emscripten_bind_PointAttribute_PointAttribute_0=a.asm.t).apply(null,arguments)},hb=a._emscripten_bind_PointAttribute_size_0=function(){return(hb=a._emscripten_bind_PointAttribute_size_0=a.asm.u).apply(null,arguments)},ib=a._emscripten_bind_PointAttribute_GetAttributeTransformData_0=function(){return(ib=a._emscripten_bind_PointAttribute_GetAttributeTransformData_0=a.asm.v).apply(null,arguments)},jb=a._emscripten_bind_PointAttribute_attribute_type_0=function(){return(jb=a._emscripten_bind_PointAttribute_attribute_type_0= +a.asm.w).apply(null,arguments)},kb=a._emscripten_bind_PointAttribute_data_type_0=function(){return(kb=a._emscripten_bind_PointAttribute_data_type_0=a.asm.x).apply(null,arguments)},lb=a._emscripten_bind_PointAttribute_num_components_0=function(){return(lb=a._emscripten_bind_PointAttribute_num_components_0=a.asm.y).apply(null,arguments)},mb=a._emscripten_bind_PointAttribute_normalized_0=function(){return(mb=a._emscripten_bind_PointAttribute_normalized_0=a.asm.z).apply(null,arguments)},nb=a._emscripten_bind_PointAttribute_byte_stride_0= +function(){return(nb=a._emscripten_bind_PointAttribute_byte_stride_0=a.asm.A).apply(null,arguments)},ob=a._emscripten_bind_PointAttribute_byte_offset_0=function(){return(ob=a._emscripten_bind_PointAttribute_byte_offset_0=a.asm.B).apply(null,arguments)},pb=a._emscripten_bind_PointAttribute_unique_id_0=function(){return(pb=a._emscripten_bind_PointAttribute_unique_id_0=a.asm.C).apply(null,arguments)},qb=a._emscripten_bind_PointAttribute___destroy___0=function(){return(qb=a._emscripten_bind_PointAttribute___destroy___0= +a.asm.D).apply(null,arguments)},Ha=a._emscripten_bind_AttributeQuantizationTransform_AttributeQuantizationTransform_0=function(){return(Ha=a._emscripten_bind_AttributeQuantizationTransform_AttributeQuantizationTransform_0=a.asm.E).apply(null,arguments)},rb=a._emscripten_bind_AttributeQuantizationTransform_InitFromAttribute_1=function(){return(rb=a._emscripten_bind_AttributeQuantizationTransform_InitFromAttribute_1=a.asm.F).apply(null,arguments)},sb=a._emscripten_bind_AttributeQuantizationTransform_quantization_bits_0= +function(){return(sb=a._emscripten_bind_AttributeQuantizationTransform_quantization_bits_0=a.asm.G).apply(null,arguments)},tb=a._emscripten_bind_AttributeQuantizationTransform_min_value_1=function(){return(tb=a._emscripten_bind_AttributeQuantizationTransform_min_value_1=a.asm.H).apply(null,arguments)},ub=a._emscripten_bind_AttributeQuantizationTransform_range_0=function(){return(ub=a._emscripten_bind_AttributeQuantizationTransform_range_0=a.asm.I).apply(null,arguments)},vb=a._emscripten_bind_AttributeQuantizationTransform___destroy___0= +function(){return(vb=a._emscripten_bind_AttributeQuantizationTransform___destroy___0=a.asm.J).apply(null,arguments)},Ia=a._emscripten_bind_AttributeOctahedronTransform_AttributeOctahedronTransform_0=function(){return(Ia=a._emscripten_bind_AttributeOctahedronTransform_AttributeOctahedronTransform_0=a.asm.K).apply(null,arguments)},wb=a._emscripten_bind_AttributeOctahedronTransform_InitFromAttribute_1=function(){return(wb=a._emscripten_bind_AttributeOctahedronTransform_InitFromAttribute_1=a.asm.L).apply(null, +arguments)},xb=a._emscripten_bind_AttributeOctahedronTransform_quantization_bits_0=function(){return(xb=a._emscripten_bind_AttributeOctahedronTransform_quantization_bits_0=a.asm.M).apply(null,arguments)},yb=a._emscripten_bind_AttributeOctahedronTransform___destroy___0=function(){return(yb=a._emscripten_bind_AttributeOctahedronTransform___destroy___0=a.asm.N).apply(null,arguments)},Ja=a._emscripten_bind_PointCloud_PointCloud_0=function(){return(Ja=a._emscripten_bind_PointCloud_PointCloud_0=a.asm.O).apply(null, +arguments)},zb=a._emscripten_bind_PointCloud_num_attributes_0=function(){return(zb=a._emscripten_bind_PointCloud_num_attributes_0=a.asm.P).apply(null,arguments)},Ab=a._emscripten_bind_PointCloud_num_points_0=function(){return(Ab=a._emscripten_bind_PointCloud_num_points_0=a.asm.Q).apply(null,arguments)},Bb=a._emscripten_bind_PointCloud___destroy___0=function(){return(Bb=a._emscripten_bind_PointCloud___destroy___0=a.asm.R).apply(null,arguments)},Ka=a._emscripten_bind_Mesh_Mesh_0=function(){return(Ka= +a._emscripten_bind_Mesh_Mesh_0=a.asm.S).apply(null,arguments)},Cb=a._emscripten_bind_Mesh_num_faces_0=function(){return(Cb=a._emscripten_bind_Mesh_num_faces_0=a.asm.T).apply(null,arguments)},Db=a._emscripten_bind_Mesh_num_attributes_0=function(){return(Db=a._emscripten_bind_Mesh_num_attributes_0=a.asm.U).apply(null,arguments)},Eb=a._emscripten_bind_Mesh_num_points_0=function(){return(Eb=a._emscripten_bind_Mesh_num_points_0=a.asm.V).apply(null,arguments)},Fb=a._emscripten_bind_Mesh___destroy___0=function(){return(Fb= +a._emscripten_bind_Mesh___destroy___0=a.asm.W).apply(null,arguments)},La=a._emscripten_bind_Metadata_Metadata_0=function(){return(La=a._emscripten_bind_Metadata_Metadata_0=a.asm.X).apply(null,arguments)},Gb=a._emscripten_bind_Metadata___destroy___0=function(){return(Gb=a._emscripten_bind_Metadata___destroy___0=a.asm.Y).apply(null,arguments)},Hb=a._emscripten_bind_Status_code_0=function(){return(Hb=a._emscripten_bind_Status_code_0=a.asm.Z).apply(null,arguments)},Ib=a._emscripten_bind_Status_ok_0=function(){return(Ib= +a._emscripten_bind_Status_ok_0=a.asm._).apply(null,arguments)},Jb=a._emscripten_bind_Status_error_msg_0=function(){return(Jb=a._emscripten_bind_Status_error_msg_0=a.asm.$).apply(null,arguments)},Kb=a._emscripten_bind_Status___destroy___0=function(){return(Kb=a._emscripten_bind_Status___destroy___0=a.asm.aa).apply(null,arguments)},Ma=a._emscripten_bind_DracoFloat32Array_DracoFloat32Array_0=function(){return(Ma=a._emscripten_bind_DracoFloat32Array_DracoFloat32Array_0=a.asm.ba).apply(null,arguments)}, +Lb=a._emscripten_bind_DracoFloat32Array_GetValue_1=function(){return(Lb=a._emscripten_bind_DracoFloat32Array_GetValue_1=a.asm.ca).apply(null,arguments)},Mb=a._emscripten_bind_DracoFloat32Array_size_0=function(){return(Mb=a._emscripten_bind_DracoFloat32Array_size_0=a.asm.da).apply(null,arguments)},Nb=a._emscripten_bind_DracoFloat32Array___destroy___0=function(){return(Nb=a._emscripten_bind_DracoFloat32Array___destroy___0=a.asm.ea).apply(null,arguments)},Na=a._emscripten_bind_DracoInt8Array_DracoInt8Array_0= +function(){return(Na=a._emscripten_bind_DracoInt8Array_DracoInt8Array_0=a.asm.fa).apply(null,arguments)},Ob=a._emscripten_bind_DracoInt8Array_GetValue_1=function(){return(Ob=a._emscripten_bind_DracoInt8Array_GetValue_1=a.asm.ga).apply(null,arguments)},Pb=a._emscripten_bind_DracoInt8Array_size_0=function(){return(Pb=a._emscripten_bind_DracoInt8Array_size_0=a.asm.ha).apply(null,arguments)},Qb=a._emscripten_bind_DracoInt8Array___destroy___0=function(){return(Qb=a._emscripten_bind_DracoInt8Array___destroy___0= +a.asm.ia).apply(null,arguments)},Oa=a._emscripten_bind_DracoUInt8Array_DracoUInt8Array_0=function(){return(Oa=a._emscripten_bind_DracoUInt8Array_DracoUInt8Array_0=a.asm.ja).apply(null,arguments)},Rb=a._emscripten_bind_DracoUInt8Array_GetValue_1=function(){return(Rb=a._emscripten_bind_DracoUInt8Array_GetValue_1=a.asm.ka).apply(null,arguments)},Sb=a._emscripten_bind_DracoUInt8Array_size_0=function(){return(Sb=a._emscripten_bind_DracoUInt8Array_size_0=a.asm.la).apply(null,arguments)},Tb=a._emscripten_bind_DracoUInt8Array___destroy___0= +function(){return(Tb=a._emscripten_bind_DracoUInt8Array___destroy___0=a.asm.ma).apply(null,arguments)},Pa=a._emscripten_bind_DracoInt16Array_DracoInt16Array_0=function(){return(Pa=a._emscripten_bind_DracoInt16Array_DracoInt16Array_0=a.asm.na).apply(null,arguments)},Ub=a._emscripten_bind_DracoInt16Array_GetValue_1=function(){return(Ub=a._emscripten_bind_DracoInt16Array_GetValue_1=a.asm.oa).apply(null,arguments)},Vb=a._emscripten_bind_DracoInt16Array_size_0=function(){return(Vb=a._emscripten_bind_DracoInt16Array_size_0= +a.asm.pa).apply(null,arguments)},Wb=a._emscripten_bind_DracoInt16Array___destroy___0=function(){return(Wb=a._emscripten_bind_DracoInt16Array___destroy___0=a.asm.qa).apply(null,arguments)},Qa=a._emscripten_bind_DracoUInt16Array_DracoUInt16Array_0=function(){return(Qa=a._emscripten_bind_DracoUInt16Array_DracoUInt16Array_0=a.asm.ra).apply(null,arguments)},Xb=a._emscripten_bind_DracoUInt16Array_GetValue_1=function(){return(Xb=a._emscripten_bind_DracoUInt16Array_GetValue_1=a.asm.sa).apply(null,arguments)}, +Yb=a._emscripten_bind_DracoUInt16Array_size_0=function(){return(Yb=a._emscripten_bind_DracoUInt16Array_size_0=a.asm.ta).apply(null,arguments)},Zb=a._emscripten_bind_DracoUInt16Array___destroy___0=function(){return(Zb=a._emscripten_bind_DracoUInt16Array___destroy___0=a.asm.ua).apply(null,arguments)},Ra=a._emscripten_bind_DracoInt32Array_DracoInt32Array_0=function(){return(Ra=a._emscripten_bind_DracoInt32Array_DracoInt32Array_0=a.asm.va).apply(null,arguments)},$b=a._emscripten_bind_DracoInt32Array_GetValue_1= +function(){return($b=a._emscripten_bind_DracoInt32Array_GetValue_1=a.asm.wa).apply(null,arguments)},ac=a._emscripten_bind_DracoInt32Array_size_0=function(){return(ac=a._emscripten_bind_DracoInt32Array_size_0=a.asm.xa).apply(null,arguments)},bc=a._emscripten_bind_DracoInt32Array___destroy___0=function(){return(bc=a._emscripten_bind_DracoInt32Array___destroy___0=a.asm.ya).apply(null,arguments)},Sa=a._emscripten_bind_DracoUInt32Array_DracoUInt32Array_0=function(){return(Sa=a._emscripten_bind_DracoUInt32Array_DracoUInt32Array_0= +a.asm.za).apply(null,arguments)},cc=a._emscripten_bind_DracoUInt32Array_GetValue_1=function(){return(cc=a._emscripten_bind_DracoUInt32Array_GetValue_1=a.asm.Aa).apply(null,arguments)},dc=a._emscripten_bind_DracoUInt32Array_size_0=function(){return(dc=a._emscripten_bind_DracoUInt32Array_size_0=a.asm.Ba).apply(null,arguments)},ec=a._emscripten_bind_DracoUInt32Array___destroy___0=function(){return(ec=a._emscripten_bind_DracoUInt32Array___destroy___0=a.asm.Ca).apply(null,arguments)},Ta=a._emscripten_bind_MetadataQuerier_MetadataQuerier_0= +function(){return(Ta=a._emscripten_bind_MetadataQuerier_MetadataQuerier_0=a.asm.Da).apply(null,arguments)},fc=a._emscripten_bind_MetadataQuerier_HasEntry_2=function(){return(fc=a._emscripten_bind_MetadataQuerier_HasEntry_2=a.asm.Ea).apply(null,arguments)},gc=a._emscripten_bind_MetadataQuerier_GetIntEntry_2=function(){return(gc=a._emscripten_bind_MetadataQuerier_GetIntEntry_2=a.asm.Fa).apply(null,arguments)},hc=a._emscripten_bind_MetadataQuerier_GetIntEntryArray_3=function(){return(hc=a._emscripten_bind_MetadataQuerier_GetIntEntryArray_3= +a.asm.Ga).apply(null,arguments)},ic=a._emscripten_bind_MetadataQuerier_GetDoubleEntry_2=function(){return(ic=a._emscripten_bind_MetadataQuerier_GetDoubleEntry_2=a.asm.Ha).apply(null,arguments)},jc=a._emscripten_bind_MetadataQuerier_GetStringEntry_2=function(){return(jc=a._emscripten_bind_MetadataQuerier_GetStringEntry_2=a.asm.Ia).apply(null,arguments)},kc=a._emscripten_bind_MetadataQuerier_NumEntries_1=function(){return(kc=a._emscripten_bind_MetadataQuerier_NumEntries_1=a.asm.Ja).apply(null,arguments)}, +lc=a._emscripten_bind_MetadataQuerier_GetEntryName_2=function(){return(lc=a._emscripten_bind_MetadataQuerier_GetEntryName_2=a.asm.Ka).apply(null,arguments)},mc=a._emscripten_bind_MetadataQuerier___destroy___0=function(){return(mc=a._emscripten_bind_MetadataQuerier___destroy___0=a.asm.La).apply(null,arguments)},Ua=a._emscripten_bind_Decoder_Decoder_0=function(){return(Ua=a._emscripten_bind_Decoder_Decoder_0=a.asm.Ma).apply(null,arguments)},nc=a._emscripten_bind_Decoder_DecodeArrayToPointCloud_3=function(){return(nc= +a._emscripten_bind_Decoder_DecodeArrayToPointCloud_3=a.asm.Na).apply(null,arguments)},oc=a._emscripten_bind_Decoder_DecodeArrayToMesh_3=function(){return(oc=a._emscripten_bind_Decoder_DecodeArrayToMesh_3=a.asm.Oa).apply(null,arguments)},pc=a._emscripten_bind_Decoder_GetAttributeId_2=function(){return(pc=a._emscripten_bind_Decoder_GetAttributeId_2=a.asm.Pa).apply(null,arguments)},qc=a._emscripten_bind_Decoder_GetAttributeIdByName_2=function(){return(qc=a._emscripten_bind_Decoder_GetAttributeIdByName_2= +a.asm.Qa).apply(null,arguments)},rc=a._emscripten_bind_Decoder_GetAttributeIdByMetadataEntry_3=function(){return(rc=a._emscripten_bind_Decoder_GetAttributeIdByMetadataEntry_3=a.asm.Ra).apply(null,arguments)},sc=a._emscripten_bind_Decoder_GetAttribute_2=function(){return(sc=a._emscripten_bind_Decoder_GetAttribute_2=a.asm.Sa).apply(null,arguments)},tc=a._emscripten_bind_Decoder_GetAttributeByUniqueId_2=function(){return(tc=a._emscripten_bind_Decoder_GetAttributeByUniqueId_2=a.asm.Ta).apply(null,arguments)}, +uc=a._emscripten_bind_Decoder_GetMetadata_1=function(){return(uc=a._emscripten_bind_Decoder_GetMetadata_1=a.asm.Ua).apply(null,arguments)},vc=a._emscripten_bind_Decoder_GetAttributeMetadata_2=function(){return(vc=a._emscripten_bind_Decoder_GetAttributeMetadata_2=a.asm.Va).apply(null,arguments)},wc=a._emscripten_bind_Decoder_GetFaceFromMesh_3=function(){return(wc=a._emscripten_bind_Decoder_GetFaceFromMesh_3=a.asm.Wa).apply(null,arguments)},xc=a._emscripten_bind_Decoder_GetTriangleStripsFromMesh_2= +function(){return(xc=a._emscripten_bind_Decoder_GetTriangleStripsFromMesh_2=a.asm.Xa).apply(null,arguments)},yc=a._emscripten_bind_Decoder_GetTrianglesUInt16Array_3=function(){return(yc=a._emscripten_bind_Decoder_GetTrianglesUInt16Array_3=a.asm.Ya).apply(null,arguments)},zc=a._emscripten_bind_Decoder_GetTrianglesUInt32Array_3=function(){return(zc=a._emscripten_bind_Decoder_GetTrianglesUInt32Array_3=a.asm.Za).apply(null,arguments)},Ac=a._emscripten_bind_Decoder_GetAttributeFloat_3=function(){return(Ac= +a._emscripten_bind_Decoder_GetAttributeFloat_3=a.asm._a).apply(null,arguments)},Bc=a._emscripten_bind_Decoder_GetAttributeFloatForAllPoints_3=function(){return(Bc=a._emscripten_bind_Decoder_GetAttributeFloatForAllPoints_3=a.asm.$a).apply(null,arguments)},Cc=a._emscripten_bind_Decoder_GetAttributeIntForAllPoints_3=function(){return(Cc=a._emscripten_bind_Decoder_GetAttributeIntForAllPoints_3=a.asm.ab).apply(null,arguments)},Dc=a._emscripten_bind_Decoder_GetAttributeInt8ForAllPoints_3=function(){return(Dc= +a._emscripten_bind_Decoder_GetAttributeInt8ForAllPoints_3=a.asm.bb).apply(null,arguments)},Ec=a._emscripten_bind_Decoder_GetAttributeUInt8ForAllPoints_3=function(){return(Ec=a._emscripten_bind_Decoder_GetAttributeUInt8ForAllPoints_3=a.asm.cb).apply(null,arguments)},Fc=a._emscripten_bind_Decoder_GetAttributeInt16ForAllPoints_3=function(){return(Fc=a._emscripten_bind_Decoder_GetAttributeInt16ForAllPoints_3=a.asm.db).apply(null,arguments)},Gc=a._emscripten_bind_Decoder_GetAttributeUInt16ForAllPoints_3= +function(){return(Gc=a._emscripten_bind_Decoder_GetAttributeUInt16ForAllPoints_3=a.asm.eb).apply(null,arguments)},Hc=a._emscripten_bind_Decoder_GetAttributeInt32ForAllPoints_3=function(){return(Hc=a._emscripten_bind_Decoder_GetAttributeInt32ForAllPoints_3=a.asm.fb).apply(null,arguments)},Ic=a._emscripten_bind_Decoder_GetAttributeUInt32ForAllPoints_3=function(){return(Ic=a._emscripten_bind_Decoder_GetAttributeUInt32ForAllPoints_3=a.asm.gb).apply(null,arguments)},Jc=a._emscripten_bind_Decoder_GetAttributeDataArrayForAllPoints_5= +function(){return(Jc=a._emscripten_bind_Decoder_GetAttributeDataArrayForAllPoints_5=a.asm.hb).apply(null,arguments)},Kc=a._emscripten_bind_Decoder_SkipAttributeTransform_1=function(){return(Kc=a._emscripten_bind_Decoder_SkipAttributeTransform_1=a.asm.ib).apply(null,arguments)},Lc=a._emscripten_bind_Decoder_GetEncodedGeometryType_Deprecated_1=function(){return(Lc=a._emscripten_bind_Decoder_GetEncodedGeometryType_Deprecated_1=a.asm.jb).apply(null,arguments)},Mc=a._emscripten_bind_Decoder_DecodeBufferToPointCloud_2= +function(){return(Mc=a._emscripten_bind_Decoder_DecodeBufferToPointCloud_2=a.asm.kb).apply(null,arguments)},Nc=a._emscripten_bind_Decoder_DecodeBufferToMesh_2=function(){return(Nc=a._emscripten_bind_Decoder_DecodeBufferToMesh_2=a.asm.lb).apply(null,arguments)},Oc=a._emscripten_bind_Decoder___destroy___0=function(){return(Oc=a._emscripten_bind_Decoder___destroy___0=a.asm.mb).apply(null,arguments)},Pc=a._emscripten_enum_draco_AttributeTransformType_ATTRIBUTE_INVALID_TRANSFORM=function(){return(Pc=a._emscripten_enum_draco_AttributeTransformType_ATTRIBUTE_INVALID_TRANSFORM= +a.asm.nb).apply(null,arguments)},Qc=a._emscripten_enum_draco_AttributeTransformType_ATTRIBUTE_NO_TRANSFORM=function(){return(Qc=a._emscripten_enum_draco_AttributeTransformType_ATTRIBUTE_NO_TRANSFORM=a.asm.ob).apply(null,arguments)},Rc=a._emscripten_enum_draco_AttributeTransformType_ATTRIBUTE_QUANTIZATION_TRANSFORM=function(){return(Rc=a._emscripten_enum_draco_AttributeTransformType_ATTRIBUTE_QUANTIZATION_TRANSFORM=a.asm.pb).apply(null,arguments)},Sc=a._emscripten_enum_draco_AttributeTransformType_ATTRIBUTE_OCTAHEDRON_TRANSFORM= +function(){return(Sc=a._emscripten_enum_draco_AttributeTransformType_ATTRIBUTE_OCTAHEDRON_TRANSFORM=a.asm.qb).apply(null,arguments)},Tc=a._emscripten_enum_draco_GeometryAttribute_Type_INVALID=function(){return(Tc=a._emscripten_enum_draco_GeometryAttribute_Type_INVALID=a.asm.rb).apply(null,arguments)},Uc=a._emscripten_enum_draco_GeometryAttribute_Type_POSITION=function(){return(Uc=a._emscripten_enum_draco_GeometryAttribute_Type_POSITION=a.asm.sb).apply(null,arguments)},Vc=a._emscripten_enum_draco_GeometryAttribute_Type_NORMAL= +function(){return(Vc=a._emscripten_enum_draco_GeometryAttribute_Type_NORMAL=a.asm.tb).apply(null,arguments)},Wc=a._emscripten_enum_draco_GeometryAttribute_Type_COLOR=function(){return(Wc=a._emscripten_enum_draco_GeometryAttribute_Type_COLOR=a.asm.ub).apply(null,arguments)},Xc=a._emscripten_enum_draco_GeometryAttribute_Type_TEX_COORD=function(){return(Xc=a._emscripten_enum_draco_GeometryAttribute_Type_TEX_COORD=a.asm.vb).apply(null,arguments)},Yc=a._emscripten_enum_draco_GeometryAttribute_Type_GENERIC= +function(){return(Yc=a._emscripten_enum_draco_GeometryAttribute_Type_GENERIC=a.asm.wb).apply(null,arguments)},Zc=a._emscripten_enum_draco_EncodedGeometryType_INVALID_GEOMETRY_TYPE=function(){return(Zc=a._emscripten_enum_draco_EncodedGeometryType_INVALID_GEOMETRY_TYPE=a.asm.xb).apply(null,arguments)},$c=a._emscripten_enum_draco_EncodedGeometryType_POINT_CLOUD=function(){return($c=a._emscripten_enum_draco_EncodedGeometryType_POINT_CLOUD=a.asm.yb).apply(null,arguments)},ad=a._emscripten_enum_draco_EncodedGeometryType_TRIANGULAR_MESH= +function(){return(ad=a._emscripten_enum_draco_EncodedGeometryType_TRIANGULAR_MESH=a.asm.zb).apply(null,arguments)},bd=a._emscripten_enum_draco_DataType_DT_INVALID=function(){return(bd=a._emscripten_enum_draco_DataType_DT_INVALID=a.asm.Ab).apply(null,arguments)},cd=a._emscripten_enum_draco_DataType_DT_INT8=function(){return(cd=a._emscripten_enum_draco_DataType_DT_INT8=a.asm.Bb).apply(null,arguments)},dd=a._emscripten_enum_draco_DataType_DT_UINT8=function(){return(dd=a._emscripten_enum_draco_DataType_DT_UINT8= +a.asm.Cb).apply(null,arguments)},ed=a._emscripten_enum_draco_DataType_DT_INT16=function(){return(ed=a._emscripten_enum_draco_DataType_DT_INT16=a.asm.Db).apply(null,arguments)},fd=a._emscripten_enum_draco_DataType_DT_UINT16=function(){return(fd=a._emscripten_enum_draco_DataType_DT_UINT16=a.asm.Eb).apply(null,arguments)},gd=a._emscripten_enum_draco_DataType_DT_INT32=function(){return(gd=a._emscripten_enum_draco_DataType_DT_INT32=a.asm.Fb).apply(null,arguments)},hd=a._emscripten_enum_draco_DataType_DT_UINT32= +function(){return(hd=a._emscripten_enum_draco_DataType_DT_UINT32=a.asm.Gb).apply(null,arguments)},id=a._emscripten_enum_draco_DataType_DT_INT64=function(){return(id=a._emscripten_enum_draco_DataType_DT_INT64=a.asm.Hb).apply(null,arguments)},jd=a._emscripten_enum_draco_DataType_DT_UINT64=function(){return(jd=a._emscripten_enum_draco_DataType_DT_UINT64=a.asm.Ib).apply(null,arguments)},kd=a._emscripten_enum_draco_DataType_DT_FLOAT32=function(){return(kd=a._emscripten_enum_draco_DataType_DT_FLOAT32=a.asm.Jb).apply(null, +arguments)},ld=a._emscripten_enum_draco_DataType_DT_FLOAT64=function(){return(ld=a._emscripten_enum_draco_DataType_DT_FLOAT64=a.asm.Kb).apply(null,arguments)},md=a._emscripten_enum_draco_DataType_DT_BOOL=function(){return(md=a._emscripten_enum_draco_DataType_DT_BOOL=a.asm.Lb).apply(null,arguments)},nd=a._emscripten_enum_draco_DataType_DT_TYPES_COUNT=function(){return(nd=a._emscripten_enum_draco_DataType_DT_TYPES_COUNT=a.asm.Mb).apply(null,arguments)},od=a._emscripten_enum_draco_StatusCode_OK=function(){return(od= +a._emscripten_enum_draco_StatusCode_OK=a.asm.Nb).apply(null,arguments)},pd=a._emscripten_enum_draco_StatusCode_DRACO_ERROR=function(){return(pd=a._emscripten_enum_draco_StatusCode_DRACO_ERROR=a.asm.Ob).apply(null,arguments)},qd=a._emscripten_enum_draco_StatusCode_IO_ERROR=function(){return(qd=a._emscripten_enum_draco_StatusCode_IO_ERROR=a.asm.Pb).apply(null,arguments)},rd=a._emscripten_enum_draco_StatusCode_INVALID_PARAMETER=function(){return(rd=a._emscripten_enum_draco_StatusCode_INVALID_PARAMETER= +a.asm.Qb).apply(null,arguments)},sd=a._emscripten_enum_draco_StatusCode_UNSUPPORTED_VERSION=function(){return(sd=a._emscripten_enum_draco_StatusCode_UNSUPPORTED_VERSION=a.asm.Rb).apply(null,arguments)},td=a._emscripten_enum_draco_StatusCode_UNKNOWN_VERSION=function(){return(td=a._emscripten_enum_draco_StatusCode_UNKNOWN_VERSION=a.asm.Sb).apply(null,arguments)};a._malloc=function(){return(a._malloc=a.asm.Tb).apply(null,arguments)};a._free=function(){return(a._free=a.asm.Ub).apply(null,arguments)}; +var ya=function(){return(ya=a.asm.Vb).apply(null,arguments)};a.___start_em_js=15856;a.___stop_em_js=15954;var la;ia=function b(){la||ba();la||(ia=b)};if(a.preInit)for("function"==typeof a.preInit&&(a.preInit=[a.preInit]);0=r.size?(0>>=0;switch(c.BYTES_PER_ELEMENT){case 2:d>>>=1;break;case 4:d>>>=2;break;case 8:d>>>=3}for(var g=0;gb.byteLength)return a.INVALID_GEOMETRY_TYPE;switch(b[7]){case 0:return a.POINT_CLOUD;case 1:return a.TRIANGULAR_MESH;default:return a.INVALID_GEOMETRY_TYPE}};return n.ready}}(); +"object"===typeof exports&&"object"===typeof module?module.exports=DracoDecoderModule:"function"===typeof define&&define.amd?define([],function(){return DracoDecoderModule}):"object"===typeof exports&&(exports.DracoDecoderModule=DracoDecoderModule); diff --git a/src/box-winUI/Assets/home-globe/e3.glb b/src/box-winUI/Assets/home-globe/e3.glb new file mode 100644 index 0000000..9cb4edd Binary files /dev/null and b/src/box-winUI/Assets/home-globe/e3.glb differ diff --git a/src/box-winUI/Assets/home-globe/ephemeris.js b/src/box-winUI/Assets/home-globe/ephemeris.js new file mode 100644 index 0000000..2491848 --- /dev/null +++ b/src/box-winUI/Assets/home-globe/ephemeris.js @@ -0,0 +1,140 @@ +const DEG_TO_RAD = Math.PI / 180; +const J2000 = 2451545.0; + +const ELEMENTS = { + mercury: [0.38709927, 0.00000037, 0.20563593, 0.00001906, 7.00497902, -0.00594749, 252.25032350, 149472.67411175, 77.45779628, 0.16047689, 48.33076593, -0.12534081], + venus: [0.72333566, 0.00000390, 0.00677672, -0.00004107, 3.39467605, -0.00078890, 181.97909950, 58517.81538729, 131.60246718, 0.00268329, 76.67984255, -0.27769418], + earth: [1.00000261, 0.00000562, 0.01671123, -0.00004392, -0.00001531, -0.01294668, 100.46457166, 35999.37244981, 102.93768193, 0.32327364, 0, 0], + mars: [1.52371034, 0.00001847, 0.09339410, 0.00007882, 1.84969142, -0.00813131, -4.55343205, 19140.30268499, -23.94362959, 0.44441088, 49.55953891, -0.29257343], + jupiter: [5.20288700, -0.00011607, 0.04838624, -0.00013253, 1.30439695, -0.00183714, 34.39644051, 3034.74612775, 14.72847983, 0.21252668, 100.47390909, 0.20469106], + saturn: [9.53667594, -0.00125060, 0.05386179, -0.00050991, 2.48599187, 0.00193609, 49.95424423, 1222.49362201, 92.59887831, -0.41897216, 113.66242448, -0.28867794], + uranus: [19.18916464, -0.00196176, 0.04725744, -0.00004397, 0.77263783, -0.00242939, 313.23810451, 428.48202785, 170.95427630, 0.40805281, 74.01692503, 0.04240589], + neptune: [30.06992276, 0.00026291, 0.00859048, 0.00005105, 1.77004347, 0.00035372, -55.12002969, 218.45945325, 44.96476227, -0.32241464, 131.78422574, -0.00508664] +}; + +const ORBITAL_PERIOD_DAYS = { + mercury: 87.969, + venus: 224.701, + earth: 365.256, + mars: 686.98, + jupiter: 4332.589, + saturn: 10759.22, + uranus: 30685.4, + neptune: 60190 +}; + +export function julianDate(date = new Date()) { + return date.getTime() / 86400000 + 2440587.5; +} + +export function approximatePlanetPosition(bodyId, date = new Date()) { + const element = ELEMENTS[bodyId]; + if (!element) { + return { x: 0, y: 0, z: 0 }; + } + + const jd = julianDate(date); + const t = (jd - J2000) / 36525; + const [a0, aRate, e0, eRate, i0, iRate, l0, lRate, peri0, periRate, node0, nodeRate] = element; + const a = a0 + aRate * t; + const e = e0 + eRate * t; + const inclination = normalizeDegrees(i0 + iRate * t) * DEG_TO_RAD; + const meanLongitude = normalizeDegrees(l0 + lRate * t); + const longitudeOfPerihelion = normalizeDegrees(peri0 + periRate * t); + const longitudeOfAscendingNode = normalizeDegrees(node0 + nodeRate * t) * DEG_TO_RAD; + const argumentOfPerihelion = (longitudeOfPerihelion * DEG_TO_RAD) - longitudeOfAscendingNode; + const meanAnomaly = normalizeDegrees(meanLongitude - longitudeOfPerihelion) * DEG_TO_RAD; + const eccentricAnomaly = solveKepler(meanAnomaly, e); + const xv = a * (Math.cos(eccentricAnomaly) - e); + const yv = a * Math.sqrt(1 - e * e) * Math.sin(eccentricAnomaly); + const trueAnomaly = Math.atan2(yv, xv); + const radius = Math.sqrt(xv * xv + yv * yv); + const angle = argumentOfPerihelion + trueAnomaly; + + return { + x: radius * (Math.cos(longitudeOfAscendingNode) * Math.cos(angle) - Math.sin(longitudeOfAscendingNode) * Math.sin(angle) * Math.cos(inclination)), + y: radius * (Math.sin(longitudeOfAscendingNode) * Math.cos(angle) + Math.cos(longitudeOfAscendingNode) * Math.sin(angle) * Math.cos(inclination)), + z: radius * (Math.sin(angle) * Math.sin(inclination)) + }; +} + +export function approximateSolarSystem(date = new Date()) { + return Object.keys(ELEMENTS).map(id => ({ + id, + heliocentricAu: approximatePlanetPosition(id, date) + })); +} + +export function orbitSamples(bodyId, date = new Date(), count = 720) { + const element = elementsForDate(bodyId, date); + if (!element) { + return []; + } + + const samples = []; + const sampleCount = Math.max(64, count); + for (let index = 0; index <= sampleCount; index += 1) { + samples.push(positionFromEccentricAnomaly(element, (index / sampleCount) * Math.PI * 2)); + } + return samples; +} + +export function orbitalPeriodDays(bodyId) { + return ORBITAL_PERIOD_DAYS[bodyId] ?? 365.256; +} + +function elementsForDate(bodyId, date) { + const element = ELEMENTS[bodyId]; + if (!element) { + return null; + } + + const jd = julianDate(date); + const t = (jd - J2000) / 36525; + const [a0, aRate, e0, eRate, i0, iRate, , , peri0, periRate, node0, nodeRate] = element; + const node = normalizeDegrees(node0 + nodeRate * t) * DEG_TO_RAD; + const perihelion = normalizeDegrees(peri0 + periRate * t) * DEG_TO_RAD; + return { + a: a0 + aRate * t, + e: e0 + eRate * t, + inclination: normalizeDegrees(i0 + iRate * t) * DEG_TO_RAD, + node, + argumentOfPerihelion: perihelion - node + }; +} + +function positionFromEccentricAnomaly(element, eccentricAnomaly) { + const xv = element.a * (Math.cos(eccentricAnomaly) - element.e); + const yv = element.a * Math.sqrt(1 - element.e * element.e) * Math.sin(eccentricAnomaly); + const cosNode = Math.cos(element.node); + const sinNode = Math.sin(element.node); + const cosPeri = Math.cos(element.argumentOfPerihelion); + const sinPeri = Math.sin(element.argumentOfPerihelion); + const cosInclination = Math.cos(element.inclination); + const sinInclination = Math.sin(element.inclination); + + return { + x: (cosNode * cosPeri - sinNode * sinPeri * cosInclination) * xv + + (-cosNode * sinPeri - sinNode * cosPeri * cosInclination) * yv, + y: (sinNode * cosPeri + cosNode * sinPeri * cosInclination) * xv + + (-sinNode * sinPeri + cosNode * cosPeri * cosInclination) * yv, + z: (sinPeri * sinInclination) * xv + (cosPeri * sinInclination) * yv + }; +} + +function solveKepler(meanAnomaly, eccentricity) { + let eccentricAnomaly = eccentricity < 0.8 ? meanAnomaly : Math.PI; + for (let index = 0; index < 12; index += 1) { + const delta = (eccentricAnomaly - eccentricity * Math.sin(eccentricAnomaly) - meanAnomaly) / + (1 - eccentricity * Math.cos(eccentricAnomaly)); + eccentricAnomaly -= delta; + if (Math.abs(delta) < 1e-10) { + break; + } + } + return eccentricAnomaly; +} + +function normalizeDegrees(value) { + return ((value % 360) + 360) % 360; +} diff --git a/src/box-winUI/Assets/home-globe/galaxy-panorama.jpg b/src/box-winUI/Assets/home-globe/galaxy-panorama.jpg new file mode 100644 index 0000000..29d34f4 Binary files /dev/null and b/src/box-winUI/Assets/home-globe/galaxy-panorama.jpg differ diff --git a/src/box-winUI/Assets/home-globe/planet-detail.html b/src/box-winUI/Assets/home-globe/planet-detail.html new file mode 100644 index 0000000..fa7a04a --- /dev/null +++ b/src/box-winUI/Assets/home-globe/planet-detail.html @@ -0,0 +1,596 @@ + + + + + + + 行星详情 + + + + +

+
+

行星

+

+
+
正在载入高清行星材质
+
+ + + + + + diff --git a/src/box-winUI/Assets/home-globe/solar-system-data.js b/src/box-winUI/Assets/home-globe/solar-system-data.js new file mode 100644 index 0000000..617b1bf --- /dev/null +++ b/src/box-winUI/Assets/home-globe/solar-system-data.js @@ -0,0 +1,365 @@ +const TEX = './textures/solar/'; + +export const SOLAR_SYSTEM_BODIES = [ + { + id: 'sun', + name: '太阳', + englishName: 'Sun', + kind: 'G 型主序恒星', + group: '恒星核心', + composition: '氢、氦等离子体', + diameter: '1,392,700 km', + solarDistance: '太阳系中心天体', + orbitalPeriod: '约 2.3 亿年绕银河系中心一周', + rotationPeriod: '赤道约 25 天,两极约 35 天', + moons: '0', + radiusKm: 695700, + color: '#ffd36a', + emissive: '#ffb12f', + displayRadius: 4.1, + orbitRadius: 0, + axialTiltDeg: 7.25, + texture: `${TEX}sun.jpg`, + textureCredit: '程序生成高清太阳纹理,参考 NASA Solar Dynamics Observatory 观感', + visualNotes: '增强日冕、色球流纹和太阳风粒子,让中心天体在高 DPI 下保持锐利。', + observationHighlights: ['11 年太阳活动周期', '太阳黑子、耀斑和日冕物质抛射', '太阳风塑造日球层'], + missions: ['SOHO', 'Parker Solar Probe', 'Solar Dynamics Observatory'], + summary: '太阳包含太阳系绝大多数质量,其引力控制八大行星、小行星、彗星与大量小天体的轨道。它通过核聚变释放能量,是地球气候、生命和人类文明存在的基础条件。', + facts: [ + '太阳主要由氢和氦组成,核心区域持续将氢聚变为氦。', + '太阳风塑造了覆盖整个太阳系的日球层。', + '太阳活动周期约 11 年,会影响空间天气、极光和卫星通信环境。' + ], + sources: ['NASA Solar System Exploration', 'NASA Science'] + }, + { + id: 'mercury', + name: '水星', + englishName: 'Mercury', + kind: '类地行星', + group: '内太阳系 · 类地行星', + composition: '金属核心、硅酸盐外壳', + diameter: '4,879 km', + solarDistance: '平均 0.39 AU', + orbitalPeriod: '87.97 地球日', + rotationPeriod: '58.65 地球日', + moons: '0', + radiusKm: 2439.7, + semiMajorAxisAu: 0.38709927, + axialTiltDeg: 0.03, + color: '#b7ada1', + displayRadius: 0.72, + texture: `${TEX}mercury.jpg`, + detailPage: './planet-detail.html?id=mercury&embedded=1', + materialProfile: '高粗糙度岩石表面,强调撞击坑、断崖和灰褐色反照率差异。', + surfaceHighlights: ['卡洛里盆地', '极区永久阴影坑', '全球性撞击坑地貌'], + textureCredit: 'Solar System Scope 8K 源图,本地 Lanczos 空间压缩为 4096x2048 运行贴图(见 solar-texture-manifest.json)', + normalDetail: 0.46, + visualNotes: '强调撞击坑、断崖和灰褐色高反差地表,适合近距离查看。', + observationHighlights: ['昼夜温差极端', '极区永久阴影坑存在水冰证据', '轨道周期只有约 88 天'], + missions: ['Mariner 10', 'MESSENGER', 'BepiColombo'], + summary: '水星是最靠近太阳、也是八大行星中最小的一颗。它几乎没有大气,昼夜温差极端,表面布满撞击坑和巨大的断崖地貌。', + facts: [ + '水星绕太阳一周只需约 88 个地球日。', + '稀薄外逸层无法像地球大气那样有效储热。', + '极区永久阴影坑中存在水冰证据。' + ], + sources: ['Solar System Scope texture maps', 'NASA Solar System Exploration', 'NASA NSSDCA', 'NASA MESSENGER', '本地 solar-texture-manifest.json'] + }, + { + id: 'venus', + name: '金星', + englishName: 'Venus', + kind: '类地行星', + group: '内太阳系 · 类地行星', + composition: '岩石行星、厚二氧化碳大气', + diameter: '12,104 km', + solarDistance: '平均 0.72 AU', + orbitalPeriod: '224.70 地球日', + rotationPeriod: '243.02 地球日,逆向自转', + moons: '0', + radiusKm: 6051.8, + semiMajorAxisAu: 0.72333566, + axialTiltDeg: 177.36, + color: '#e7bc73', + displayRadius: 0.95, + texture: `${TEX}venus.jpg`, + detailPage: './planet-detail.html?id=venus&embedded=1', + materialProfile: '厚重大气与硫酸云层材质,表面细节以柔和金色云层表现。', + surfaceHighlights: ['全球云层', '雷达可见火山平原', '高反照率大气边缘'], + textureCredit: 'Solar System Scope 8K 金星表面源图,本地 Lanczos 空间压缩为 4096x2048 运行贴图(见 solar-texture-manifest.json)', + normalDetail: 0.22, + visualNotes: '使用柔和云层和金色大气外壳,表现厚重大气的朦胧反光。', + observationHighlights: ['表面温度高于水星', '硫酸云覆盖全球', '自转方向与多数行星相反'], + missions: ['Magellan', 'Akatsuki', 'DAVINCI'], + summary: '金星大小接近地球,但拥有浓厚二氧化碳大气和强烈温室效应,是太阳系表面温度最高的行星。其云层富含硫酸,地表压力远高于地球。', + facts: [ + '金星自转方向与多数行星相反。', + '浓密云层使可见光难以直接观测地表。', + '雷达探测显示金星表面有火山平原和大型高地。' + ], + sources: ['Solar System Scope texture maps', 'NASA Solar System Exploration', 'NASA NSSDCA', 'NASA Magellan', '本地 solar-texture-manifest.json'] + }, + { + id: 'earth', + name: '地球', + englishName: 'Earth', + kind: '类地行星', + group: '内太阳系 · 类地行星', + composition: '岩石行星、氮氧大气、液态水海洋', + diameter: '12,742 km', + solarDistance: '平均 1.00 AU', + orbitalPeriod: '365.26 地球日', + rotationPeriod: '23.93 小时', + moons: '1', + radiusKm: 6371, + semiMajorAxisAu: 1.00000261, + axialTiltDeg: 23.44, + color: '#5aa7d8', + displayRadius: 1.0, + texture: `${TEX}earth.jpg`, + detailPage: './world-civilizations-globe.html?embedded=1', + materialProfile: 'NASA Blue Marble 地表、独立云层和夜侧大气边缘。', + surfaceHighlights: ['液态水海洋', '大陆地形', '动态云层'], + cloudTexture: `${TEX}earth-clouds.png`, + textureCredit: 'Solar System Scope 8K Earth day map(基于 NASA Blue Marble),本地空间压缩为 4096x2048,云层为独立 RGBA 贴图', + normalDetail: 0.18, + visualNotes: '使用 NASA Blue Marble 地表图与独立云层,点击进入世界文明古国地球界面。', + observationHighlights: ['稳定液态水海洋', '板块构造活跃', '目前已知唯一拥有生命的行星'], + missions: ['Landsat', 'Terra', 'Aqua', 'Suomi NPP'], + summary: '地球是目前已知唯一拥有稳定液态水海洋和生命的行星。本应用中点击地球会进入世界文明古国地球界面,查看代表性古文明遗址坐标和资料。', + facts: [ + '地球拥有由氮、氧为主的大气层和活跃水循环。', + '板块构造长期改变大陆、海洋和山脉形态。', + '人类文明的发展与地理环境、气候、水源和交通通道密切相关。' + ], + sources: ['Solar System Scope texture maps', 'NASA Visible Earth / Blue Marble', 'NASA Solar System Exploration', 'NASA Science', '本地 solar-texture-manifest.json'] + }, + { + id: 'mars', + name: '火星', + englishName: 'Mars', + kind: '类地行星', + group: '内太阳系 · 类地行星', + composition: '玄武岩地壳、氧化铁尘埃、稀薄二氧化碳大气', + diameter: '6,779 km', + solarDistance: '平均 1.52 AU', + orbitalPeriod: '686.98 地球日', + rotationPeriod: '24.62 小时', + moons: '2', + radiusKm: 3389.5, + semiMajorAxisAu: 1.52371034, + axialTiltDeg: 25.19, + color: '#d77755', + displayRadius: 0.86, + texture: `${TEX}mars.jpg`, + detailPage: './planet-detail.html?id=mars&embedded=1', + materialProfile: '红色尘埃、玄武岩暗区和极冠叠加的岩石行星材质。', + surfaceHighlights: ['奥林帕斯山', '水手峡谷', '南北极冠'], + textureCredit: 'Solar System Scope 8K 火星源图,本地 Lanczos 空间压缩为 4096x2048 运行贴图(见 solar-texture-manifest.json)', + normalDetail: 0.34, + visualNotes: '加入极冠、暗色玄武岩区和红色尘埃带,近景更容易辨认火星地貌。', + observationHighlights: ['奥林帕斯山和水手峡谷', '古代河道和湖泊沉积证据', '两颗小卫星火卫一、火卫二'], + missions: ['Viking', 'Mars Reconnaissance Orbiter', 'Perseverance', 'Curiosity'], + summary: '火星是一颗寒冷干燥的类地行星,拥有铁氧化物造成的红色外观。轨道器、着陆器和巡视器发现了古代河道、湖泊沉积和水活动证据。', + facts: [ + '火星拥有太阳系最高的火山奥林帕斯山。', + '极冠由水冰和二氧化碳冰构成并随季节变化。', + '火卫一和火卫二是火星的两颗小卫星。' + ], + sources: ['Solar System Scope texture maps', 'NASA Solar System Exploration', 'NASA Mars Exploration', 'NASA MRO', '本地 solar-texture-manifest.json'] + }, + { + id: 'jupiter', + name: '木星', + englishName: 'Jupiter', + kind: '气态巨行星', + group: '外太阳系 · 气态巨行星', + composition: '氢、氦大气和深层流体', + diameter: '139,820 km', + solarDistance: '平均 5.20 AU', + orbitalPeriod: '11.86 地球年', + rotationPeriod: '约 9.93 小时', + moons: '95+', + radiusKm: 69911, + semiMajorAxisAu: 5.202887, + axialTiltDeg: 3.13, + color: '#d8b38a', + displayRadius: 2.24, + texture: `${TEX}jupiter.jpg`, + detailPage: './planet-detail.html?id=jupiter&embedded=1', + materialProfile: '气态巨行星云带材质,突出大红斑、纬向条带和极区渐变。', + surfaceHighlights: ['大红斑', '纬向云带', '暗弱尘埃环'], + textureCredit: 'Solar System Scope 高清木星源图,本地锐化并压缩为 4096x2048 运行贴图(见 solar-texture-manifest.json)', + normalDetail: 0.1, + visualNotes: '增强大红斑、浅深云带和极区渐变,并加入暗弱尘埃环。', + observationHighlights: ['大红斑是持续数百年的巨大风暴', '伽利略卫星构成小型系统', '拥有暗弱环系统'], + missions: ['Voyager', 'Galileo', 'Juno', 'Europa Clipper'], + ringSystem: { + label: '暗弱尘埃环', + tiltDeg: 3.13, + opacity: 0.16, + color: '#b8a48a', + dust: true, + segments: [ + { name: 'Halo/Main', inner: 1.42, outer: 1.78, opacity: 0.18 } + ] + }, + summary: '木星是太阳系最大的行星,主要由氢和氦组成。它强大的引力影响太阳系小天体分布,著名的大红斑是持续数百年的巨大风暴系统。', + facts: [ + '木星质量超过其他行星质量总和的两倍。', + '伽利略卫星包括木卫一、木卫二、木卫三和木卫四。', + '木卫二地下海洋是太阳系生命宜居环境研究重点之一。' + ], + sources: ['Solar System Scope texture maps', 'NASA Solar System Exploration', 'NASA Science', 'NASA Juno', '本地 solar-texture-manifest.json'] + }, + { + id: 'saturn', + name: '土星', + englishName: 'Saturn', + kind: '气态巨行星', + group: '外太阳系 · 气态巨行星', + composition: '氢、氦大气和冰岩环系统', + diameter: '116,460 km', + solarDistance: '平均 9.54 AU', + orbitalPeriod: '29.46 地球年', + rotationPeriod: '约 10.7 小时', + moons: '146+', + radiusKm: 58232, + semiMajorAxisAu: 9.53667594, + axialTiltDeg: 26.73, + color: '#ead29b', + displayRadius: 2.05, + texture: `${TEX}saturn.jpg`, + detailPage: './planet-detail.html?id=saturn&embedded=1', + materialProfile: '淡金色气态云带与分层冰粒星环,强调卡西尼缝。', + surfaceHighlights: ['C/B/A/F 环', '卡西尼缝', '淡金色云带'], + textureCredit: 'Solar System Scope 高清土星源图,本地锐化并压缩为 4096x2048 运行贴图(见 solar-texture-manifest.json)', + normalDetail: 0.08, + visualNotes: '星环按自转轴倾角校准,分层表现 C/B/A/F 环和卡西尼缝。', + observationHighlights: ['明亮宽阔的冰粒环系统', '土卫六拥有浓厚大气', '土卫二喷流显示地下海洋可能性'], + missions: ['Pioneer 11', 'Voyager', 'Cassini-Huygens'], + ringSystem: { + label: 'C/B/A/F 环', + tiltDeg: 26.73, + opacity: 0.82, + color: '#f0ddb0', + dust: true, + segments: [ + { name: 'C Ring', inner: 1.22, outer: 1.48, opacity: 0.28 }, + { name: 'B Ring', inner: 1.52, outer: 1.86, opacity: 0.78 }, + { name: 'Cassini Division', inner: 1.87, outer: 1.94, opacity: 0.08 }, + { name: 'A Ring', inner: 1.96, outer: 2.28, opacity: 0.58 }, + { name: 'F Ring', inner: 2.34, outer: 2.38, opacity: 0.46 } + ] + }, + summary: '土星以明亮宽阔的环系统闻名,是第二大行星。它主要由氢和氦构成,低平均密度和复杂环结构使其成为行星科学的重要对象。', + facts: [ + '土星环主要由冰粒、岩屑和尘埃组成。', + '土卫六拥有浓厚大气和液态烃湖泊。', + '土卫二喷流显示其地下可能存在液态水海洋。' + ], + sources: ['Solar System Scope texture maps', 'NASA Solar System Exploration', 'NASA Cassini', 'NASA Saturn Facts', '本地 solar-texture-manifest.json'] + }, + { + id: 'uranus', + name: '天王星', + englishName: 'Uranus', + kind: '冰巨行星', + group: '外太阳系 · 冰巨行星', + composition: '水、氨、甲烷等冰质成分与氢氦大气', + diameter: '50,724 km', + solarDistance: '平均 19.2 AU', + orbitalPeriod: '84.01 地球年', + rotationPeriod: '约 17.2 小时,逆向自转', + moons: '27', + radiusKm: 25362, + semiMajorAxisAu: 19.18916464, + axialTiltDeg: 97.77, + color: '#8fd5d7', + displayRadius: 1.42, + texture: `${TEX}uranus.jpg`, + detailPage: './planet-detail.html?id=uranus&embedded=1', + materialProfile: '甲烷吸收形成的蓝绿色冰巨行星材质,星环近竖直展示。', + surfaceHighlights: ['横躺自转轴', '窄暗环系统', '蓝绿色甲烷大气'], + textureCredit: '本地 4K 天王星高清源图,已重新锐化并压缩为 4096x2048 运行贴图;8K 公共直链不可用,来源记录见 solar-texture-manifest.json', + normalDetail: 0.04, + visualNotes: '自转轴近乎横躺,星环平面随之近竖直,突出冰巨行星的独特姿态。', + observationHighlights: ['轴倾角约 98 度', '暗淡窄环系统', '大气中甲烷吸收红光呈蓝绿色'], + missions: ['Voyager 2'], + ringSystem: { + label: '窄暗环', + tiltDeg: 97.77, + opacity: 0.36, + color: '#bfe9e9', + segments: [ + { name: 'Main Rings', inner: 1.46, outer: 1.86, opacity: 0.28 }, + { name: 'Epsilon Ring', inner: 1.92, outer: 2.03, opacity: 0.42 } + ] + }, + summary: '天王星是一颗冰巨行星,大气中的甲烷吸收红光,使其呈蓝绿色。它的自转轴几乎横躺在轨道平面上,导致极端季节变化。', + facts: [ + '天王星轴倾角约 98 度。', + '内部富含水、氨、甲烷等冰质成分。', + '它拥有暗淡环系统和 27 颗已知卫星。' + ], + sources: ['NASA Solar System Exploration', 'NASA NSSDCA', 'NASA Uranus Facts', '本地 solar-texture-manifest.json'] + }, + { + id: 'neptune', + name: '海王星', + englishName: 'Neptune', + kind: '冰巨行星', + group: '外太阳系 · 冰巨行星', + composition: '冰质内部、氢氦甲烷大气', + diameter: '49,244 km', + solarDistance: '平均 30.1 AU', + orbitalPeriod: '164.8 地球年', + rotationPeriod: '约 16.1 小时', + moons: '14', + radiusKm: 24622, + semiMajorAxisAu: 30.06992276, + axialTiltDeg: 28.32, + color: '#4d79d8', + displayRadius: 1.38, + texture: `${TEX}neptune.jpg`, + detailPage: './planet-detail.html?id=neptune&embedded=1', + materialProfile: '深蓝甲烷大气、亮云与细弱环弧材质。', + surfaceHighlights: ['高速风暴', '亮云带', '细环与环弧'], + textureCredit: '本地 4K 海王星高清源图,已重新锐化并压缩为 4096x2048 运行贴图;8K 公共直链不可用,来源记录见 solar-texture-manifest.json', + normalDetail: 0.06, + visualNotes: '加入深蓝大气条带、亮云和细环弧段,避免远景时只呈单色球体。', + observationHighlights: ['太阳系最高速风暴之一', '海卫一沿逆行轨道运行', '拥有细弱环和环弧结构'], + missions: ['Voyager 2'], + ringSystem: { + label: '细环与环弧', + tiltDeg: 28.32, + opacity: 0.28, + color: '#92b5ff', + arcs: true, + segments: [ + { name: 'Adams Ring', inner: 1.62, outer: 1.68, opacity: 0.26 }, + { name: 'Le Verrier Ring', inner: 1.36, outer: 1.41, opacity: 0.18 } + ] + }, + summary: '海王星是八大行星中距离太阳最远的一颗。它拥有高速大气风暴和深蓝色外观,卫星海卫一可能是被捕获的柯伊伯带天体。', + facts: [ + '海王星风速可达太阳系行星大气中的极高水平。', + '海卫一沿逆行轨道运行,地质活动迹象明显。', + '海王星在 1846 年由数学预测引导观测发现。' + ], + sources: ['NASA Solar System Exploration', 'NASA NSSDCA', 'NASA Neptune Facts', '本地 solar-texture-manifest.json'] + } +]; + +export const PLANETS = SOLAR_SYSTEM_BODIES.filter(body => body.semiMajorAxisAu); + +export const SOURCE_LINKS = [ + { label: 'NASA Solar System', url: 'https://science.nasa.gov/solar-system/' }, + { label: 'NASA Visible Earth Blue Marble', url: 'https://visibleearth.nasa.gov/collection/1484/blue-marble' }, + { label: 'JPL Texture Maps', url: 'https://maps.jpl.nasa.gov/tmaps/' }, + { label: 'JPL Approximate Positions', url: 'https://ssd.jpl.nasa.gov/planets/approx_pos.html' }, + { label: 'JPL Horizons API', url: 'https://ssd-api.jpl.nasa.gov/doc/horizons.html' }, + { label: 'IMCCE Miriade ephemcc', url: 'https://ssp.imcce.fr/webservices/miriade/api/ephemcc/' }, + { label: 'NASA SVS Galaxy Panorama', url: 'https://svs.gsfc.nasa.gov/11543/' } +]; diff --git a/src/box-winUI/Assets/home-globe/solar-system-home.html b/src/box-winUI/Assets/home-globe/solar-system-home.html new file mode 100644 index 0000000..7d610d7 --- /dev/null +++ b/src/box-winUI/Assets/home-globe/solar-system-home.html @@ -0,0 +1,2638 @@ + + + + + + + 八大行星-太阳系 + + + + +
+
+
+

八大行星-太阳系

+

根据当前系统时间推算行星轨道位置。点击行星查看权威说明,点击地球进入世界文明古国。

+
+
+
+ + + 初始化太阳系 + 准备本地模型、星云、粒子和星历同步。 + + 0% +
+
+
本地 Three.js 资源加载中
+
+
+
+
+ 选择太阳或行星 + 点击地球进入世界文明古国界面 +
+
+ + + + + +
+
+
+ 初始化太阳系场景 + 0% +
+
+
正在读取本地 Three.js 模块、生成星系背景与计算行星轨道。
+
+
+
+ + + + diff --git a/src/box-winUI/Assets/home-globe/textures/solar/earth-clouds.png b/src/box-winUI/Assets/home-globe/textures/solar/earth-clouds.png new file mode 100644 index 0000000..d39ac05 Binary files /dev/null and b/src/box-winUI/Assets/home-globe/textures/solar/earth-clouds.png differ diff --git a/src/box-winUI/Assets/home-globe/textures/solar/earth.jpg b/src/box-winUI/Assets/home-globe/textures/solar/earth.jpg new file mode 100644 index 0000000..fcd2b86 Binary files /dev/null and b/src/box-winUI/Assets/home-globe/textures/solar/earth.jpg differ diff --git a/src/box-winUI/Assets/home-globe/textures/solar/jupiter.jpg b/src/box-winUI/Assets/home-globe/textures/solar/jupiter.jpg new file mode 100644 index 0000000..8bb42b1 Binary files /dev/null and b/src/box-winUI/Assets/home-globe/textures/solar/jupiter.jpg differ diff --git a/src/box-winUI/Assets/home-globe/textures/solar/mars.jpg b/src/box-winUI/Assets/home-globe/textures/solar/mars.jpg new file mode 100644 index 0000000..8bdeece Binary files /dev/null and b/src/box-winUI/Assets/home-globe/textures/solar/mars.jpg differ diff --git a/src/box-winUI/Assets/home-globe/textures/solar/mercury.jpg b/src/box-winUI/Assets/home-globe/textures/solar/mercury.jpg new file mode 100644 index 0000000..cfc8947 Binary files /dev/null and b/src/box-winUI/Assets/home-globe/textures/solar/mercury.jpg differ diff --git a/src/box-winUI/Assets/home-globe/textures/solar/neptune.jpg b/src/box-winUI/Assets/home-globe/textures/solar/neptune.jpg new file mode 100644 index 0000000..26cccde Binary files /dev/null and b/src/box-winUI/Assets/home-globe/textures/solar/neptune.jpg differ diff --git a/src/box-winUI/Assets/home-globe/textures/solar/saturn.jpg b/src/box-winUI/Assets/home-globe/textures/solar/saturn.jpg new file mode 100644 index 0000000..6d6a4f6 Binary files /dev/null and b/src/box-winUI/Assets/home-globe/textures/solar/saturn.jpg differ diff --git a/src/box-winUI/Assets/home-globe/textures/solar/solar-texture-manifest.json b/src/box-winUI/Assets/home-globe/textures/solar/solar-texture-manifest.json new file mode 100644 index 0000000..d7bac05 --- /dev/null +++ b/src/box-winUI/Assets/home-globe/textures/solar/solar-texture-manifest.json @@ -0,0 +1,204 @@ +{ + "version": 1, + "generatedAt": "2026-06-02T09:16:46Z", + "policy": { + "sourceArchive": "Original high-resolution sources are local but outside WinUI copied assets.", + "runtimeAssets": "Runtime textures are local 4096x2048 spatially compressed images.", + "minimumSourceRawBytes": 62914560 + }, + "totals": { + "sourceBytes": 93303984, + "sourceRawBytes": 603979776, + "optimizedBytes": 41161696, + "sourceBytesMB": 88.98, + "sourceRawBytesMB": 576.0, + "optimizedBytesMB": 39.25 + }, + "sources": [ + "https://www.solarsystemscope.com/textures/", + "https://visibleearth.nasa.gov/collection/1484/blue-marble", + "https://maps.jpl.nasa.gov/tmaps/" + ], + "textures": [ + { + "id": "mercury", + "sourceUrl": "https://www.solarsystemscope.com/textures/download/8k_mercury.jpg", + "sourceCredit": "Solar System Scope 8K Mercury texture, NASA/JPL-derived public imagery.", + "sourceArchivePath": "assets/source-textures/solar/8k_mercury.jpg", + "sourceBytes": 15035740, + "sourceImageSize": { + "width": 8192, + "height": 4096, + "mode": "RGB" + }, + "sourceRawBytes": 100663296, + "optimizedPath": "src/box-winUI/Assets/home-globe/textures/solar/mercury.jpg", + "optimizedBytes": 4616632, + "optimizedImageSize": { + "width": 4096, + "height": 2048 + }, + "compression": "spatial Lanczos resize to 4096x2048 plus high-quality JPEG/PNG optimization" + }, + { + "id": "venus", + "sourceUrl": "https://www.solarsystemscope.com/textures/download/8k_venus_surface.jpg", + "sourceCredit": "Solar System Scope 8K Venus surface texture, NASA/JPL-derived public imagery.", + "sourceArchivePath": "assets/source-textures/solar/8k_venus_surface.jpg", + "sourceBytes": 12523129, + "sourceImageSize": { + "width": 8192, + "height": 4096, + "mode": "RGB" + }, + "sourceRawBytes": 100663296, + "optimizedPath": "src/box-winUI/Assets/home-globe/textures/solar/venus.jpg", + "optimizedBytes": 5132591, + "optimizedImageSize": { + "width": 4096, + "height": 2048 + }, + "compression": "spatial Lanczos resize to 4096x2048 plus high-quality JPEG/PNG optimization" + }, + { + "id": "earth", + "sourceUrl": "https://www.solarsystemscope.com/textures/download/8k_earth_daymap.jpg", + "sourceCredit": "Solar System Scope 8K Earth day map based on NASA Blue Marble imagery.", + "sourceArchivePath": "assets/source-textures/solar/8k_earth_daymap.jpg", + "sourceBytes": 4565076, + "sourceImageSize": { + "width": 8192, + "height": 4096, + "mode": "RGB" + }, + "sourceRawBytes": 100663296, + "optimizedPath": "src/box-winUI/Assets/home-globe/textures/solar/earth.jpg", + "optimizedBytes": 2039911, + "optimizedImageSize": { + "width": 4096, + "height": 2048 + }, + "compression": "spatial Lanczos resize to 4096x2048 plus high-quality JPEG/PNG optimization" + }, + { + "id": "earth-clouds", + "sourceUrl": "https://www.solarsystemscope.com/textures/download/8k_earth_clouds.jpg", + "sourceCredit": "Solar System Scope 8K Earth cloud map based on NASA imagery.", + "sourceArchivePath": "assets/source-textures/solar/8k_earth_clouds.jpg", + "sourceBytes": 11619184, + "sourceImageSize": { + "width": 8192, + "height": 4096, + "mode": "RGB" + }, + "sourceRawBytes": 100663296, + "optimizedPath": "src/box-winUI/Assets/home-globe/textures/solar/earth-clouds.png", + "optimizedBytes": 7306871, + "optimizedImageSize": { + "width": 4096, + "height": 2048 + }, + "compression": "spatial Lanczos resize to 4096x2048 plus high-quality JPEG/PNG optimization" + }, + { + "id": "mars", + "sourceUrl": "https://www.solarsystemscope.com/textures/download/8k_mars.jpg", + "sourceCredit": "Solar System Scope 8K Mars texture, NASA/JPL-derived public imagery.", + "sourceArchivePath": "assets/source-textures/solar/8k_mars.jpg", + "sourceBytes": 8396951, + "sourceImageSize": { + "width": 8192, + "height": 4096, + "mode": "RGB" + }, + "sourceRawBytes": 100663296, + "optimizedPath": "src/box-winUI/Assets/home-globe/textures/solar/mars.jpg", + "optimizedBytes": 3795640, + "optimizedImageSize": { + "width": 4096, + "height": 2048 + }, + "compression": "spatial Lanczos resize to 4096x2048 plus high-quality JPEG/PNG optimization" + }, + { + "id": "jupiter", + "sourceUrl": "https://www.solarsystemscope.com/textures/download/8k_jupiter.jpg", + "sourceCredit": "Solar System Scope high-resolution Jupiter texture, NASA/JPL-derived public imagery.", + "sourceArchivePath": "assets/source-textures/solar/8k_jupiter.jpg", + "sourceBytes": 3080980, + "sourceImageSize": { + "width": 4096, + "height": 2048, + "mode": "RGB" + }, + "sourceRawBytes": 25165824, + "optimizedPath": "src/box-winUI/Assets/home-globe/textures/solar/jupiter.jpg", + "optimizedBytes": 2659196, + "optimizedImageSize": { + "width": 4096, + "height": 2048 + }, + "compression": "spatial Lanczos resize to 4096x2048 plus high-quality JPEG/PNG optimization" + }, + { + "id": "saturn", + "sourceUrl": "https://www.solarsystemscope.com/textures/download/8k_saturn.jpg", + "sourceCredit": "Solar System Scope high-resolution Saturn texture, NASA/JPL-derived public imagery.", + "sourceArchivePath": "assets/source-textures/solar/8k_saturn.jpg", + "sourceBytes": 1098204, + "sourceImageSize": { + "width": 4096, + "height": 2048, + "mode": "RGB" + }, + "sourceRawBytes": 25165824, + "optimizedPath": "src/box-winUI/Assets/home-globe/textures/solar/saturn.jpg", + "optimizedBytes": 986245, + "optimizedImageSize": { + "width": 4096, + "height": 2048 + }, + "compression": "spatial Lanczos resize to 4096x2048 plus high-quality JPEG/PNG optimization" + }, + { + "id": "uranus", + "sourceUrl": "local-png:src/box-winUI/Assets/home-globe/textures/solar/uranus.jpg", + "sourceCredit": "Local lossless 4K Uranus fallback source, retained because the public 8K direct link is unavailable.", + "sourceArchivePath": "assets/source-textures/solar/local_uranus_4k.png", + "sourceBytes": 14977648, + "sourceImageSize": { + "width": 4096, + "height": 2048, + "mode": "RGB" + }, + "sourceRawBytes": 25165824, + "optimizedPath": "src/box-winUI/Assets/home-globe/textures/solar/uranus.jpg", + "optimizedBytes": 5396966, + "optimizedImageSize": { + "width": 4096, + "height": 2048 + }, + "compression": "spatial Lanczos resize to 4096x2048 plus high-quality JPEG/PNG optimization" + }, + { + "id": "neptune", + "sourceUrl": "local-png:src/box-winUI/Assets/home-globe/textures/solar/neptune.jpg", + "sourceCredit": "Local lossless 4K Neptune fallback source, retained because the public 8K direct link is unavailable.", + "sourceArchivePath": "assets/source-textures/solar/local_neptune_4k.png", + "sourceBytes": 22007072, + "sourceImageSize": { + "width": 4096, + "height": 2048, + "mode": "RGB" + }, + "sourceRawBytes": 25165824, + "optimizedPath": "src/box-winUI/Assets/home-globe/textures/solar/neptune.jpg", + "optimizedBytes": 9227644, + "optimizedImageSize": { + "width": 4096, + "height": 2048 + }, + "compression": "spatial Lanczos resize to 4096x2048 plus high-quality JPEG/PNG optimization" + } + ] +} \ No newline at end of file diff --git a/src/box-winUI/Assets/home-globe/textures/solar/sun.jpg b/src/box-winUI/Assets/home-globe/textures/solar/sun.jpg new file mode 100644 index 0000000..962dac9 Binary files /dev/null and b/src/box-winUI/Assets/home-globe/textures/solar/sun.jpg differ diff --git a/src/box-winUI/Assets/home-globe/textures/solar/uranus.jpg b/src/box-winUI/Assets/home-globe/textures/solar/uranus.jpg new file mode 100644 index 0000000..8496f1f Binary files /dev/null and b/src/box-winUI/Assets/home-globe/textures/solar/uranus.jpg differ diff --git a/src/box-winUI/Assets/home-globe/textures/solar/venus.jpg b/src/box-winUI/Assets/home-globe/textures/solar/venus.jpg new file mode 100644 index 0000000..12879df Binary files /dev/null and b/src/box-winUI/Assets/home-globe/textures/solar/venus.jpg differ diff --git a/src/box-winUI/Assets/home-globe/utils/BufferGeometryUtils.js b/src/box-winUI/Assets/home-globe/utils/BufferGeometryUtils.js new file mode 100644 index 0000000..f620433 --- /dev/null +++ b/src/box-winUI/Assets/home-globe/utils/BufferGeometryUtils.js @@ -0,0 +1,1371 @@ +import { + BufferAttribute, + BufferGeometry, + Float32BufferAttribute, + InstancedBufferAttribute, + InterleavedBuffer, + InterleavedBufferAttribute, + TriangleFanDrawMode, + TriangleStripDrawMode, + TrianglesDrawMode, + Vector3, +} from 'three'; + +function computeMikkTSpaceTangents( geometry, MikkTSpace, negateSign = true ) { + + if ( ! MikkTSpace || ! MikkTSpace.isReady ) { + + throw new Error( 'BufferGeometryUtils: Initialized MikkTSpace library required.' ); + + } + + if ( ! geometry.hasAttribute( 'position' ) || ! geometry.hasAttribute( 'normal' ) || ! geometry.hasAttribute( 'uv' ) ) { + + throw new Error( 'BufferGeometryUtils: Tangents require "position", "normal", and "uv" attributes.' ); + + } + + function getAttributeArray( attribute ) { + + if ( attribute.normalized || attribute.isInterleavedBufferAttribute ) { + + const dstArray = new Float32Array( attribute.count * attribute.itemSize ); + + for ( let i = 0, j = 0; i < attribute.count; i ++ ) { + + dstArray[ j ++ ] = attribute.getX( i ); + dstArray[ j ++ ] = attribute.getY( i ); + + if ( attribute.itemSize > 2 ) { + + dstArray[ j ++ ] = attribute.getZ( i ); + + } + + } + + return dstArray; + + } + + if ( attribute.array instanceof Float32Array ) { + + return attribute.array; + + } + + return new Float32Array( attribute.array ); + + } + + // MikkTSpace algorithm requires non-indexed input. + + const _geometry = geometry.index ? geometry.toNonIndexed() : geometry; + + // Compute vertex tangents. + + const tangents = MikkTSpace.generateTangents( + + getAttributeArray( _geometry.attributes.position ), + getAttributeArray( _geometry.attributes.normal ), + getAttributeArray( _geometry.attributes.uv ) + + ); + + // Texture coordinate convention of glTF differs from the apparent + // default of the MikkTSpace library; .w component must be flipped. + + if ( negateSign ) { + + for ( let i = 3; i < tangents.length; i += 4 ) { + + tangents[ i ] *= - 1; + + } + + } + + // + + _geometry.setAttribute( 'tangent', new BufferAttribute( tangents, 4 ) ); + + if ( geometry !== _geometry ) { + + geometry.copy( _geometry ); + + } + + return geometry; + +} + +/** + * @param {Array} geometries + * @param {Boolean} useGroups + * @return {BufferGeometry} + */ +function mergeGeometries( geometries, useGroups = false ) { + + const isIndexed = geometries[ 0 ].index !== null; + + const attributesUsed = new Set( Object.keys( geometries[ 0 ].attributes ) ); + const morphAttributesUsed = new Set( Object.keys( geometries[ 0 ].morphAttributes ) ); + + const attributes = {}; + const morphAttributes = {}; + + const morphTargetsRelative = geometries[ 0 ].morphTargetsRelative; + + const mergedGeometry = new BufferGeometry(); + + let offset = 0; + + for ( let i = 0; i < geometries.length; ++ i ) { + + const geometry = geometries[ i ]; + let attributesCount = 0; + + // ensure that all geometries are indexed, or none + + if ( isIndexed !== ( geometry.index !== null ) ) { + + console.error( 'THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index ' + i + '. All geometries must have compatible attributes; make sure index attribute exists among all geometries, or in none of them.' ); + return null; + + } + + // gather attributes, exit early if they're different + + for ( const name in geometry.attributes ) { + + if ( ! attributesUsed.has( name ) ) { + + console.error( 'THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index ' + i + '. All geometries must have compatible attributes; make sure "' + name + '" attribute exists among all geometries, or in none of them.' ); + return null; + + } + + if ( attributes[ name ] === undefined ) attributes[ name ] = []; + + attributes[ name ].push( geometry.attributes[ name ] ); + + attributesCount ++; + + } + + // ensure geometries have the same number of attributes + + if ( attributesCount !== attributesUsed.size ) { + + console.error( 'THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index ' + i + '. Make sure all geometries have the same number of attributes.' ); + return null; + + } + + // gather morph attributes, exit early if they're different + + if ( morphTargetsRelative !== geometry.morphTargetsRelative ) { + + console.error( 'THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index ' + i + '. .morphTargetsRelative must be consistent throughout all geometries.' ); + return null; + + } + + for ( const name in geometry.morphAttributes ) { + + if ( ! morphAttributesUsed.has( name ) ) { + + console.error( 'THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index ' + i + '. .morphAttributes must be consistent throughout all geometries.' ); + return null; + + } + + if ( morphAttributes[ name ] === undefined ) morphAttributes[ name ] = []; + + morphAttributes[ name ].push( geometry.morphAttributes[ name ] ); + + } + + if ( useGroups ) { + + let count; + + if ( isIndexed ) { + + count = geometry.index.count; + + } else if ( geometry.attributes.position !== undefined ) { + + count = geometry.attributes.position.count; + + } else { + + console.error( 'THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index ' + i + '. The geometry must have either an index or a position attribute' ); + return null; + + } + + mergedGeometry.addGroup( offset, count, i ); + + offset += count; + + } + + } + + // merge indices + + if ( isIndexed ) { + + let indexOffset = 0; + const mergedIndex = []; + + for ( let i = 0; i < geometries.length; ++ i ) { + + const index = geometries[ i ].index; + + for ( let j = 0; j < index.count; ++ j ) { + + mergedIndex.push( index.getX( j ) + indexOffset ); + + } + + indexOffset += geometries[ i ].attributes.position.count; + + } + + mergedGeometry.setIndex( mergedIndex ); + + } + + // merge attributes + + for ( const name in attributes ) { + + const mergedAttribute = mergeAttributes( attributes[ name ] ); + + if ( ! mergedAttribute ) { + + console.error( 'THREE.BufferGeometryUtils: .mergeGeometries() failed while trying to merge the ' + name + ' attribute.' ); + return null; + + } + + mergedGeometry.setAttribute( name, mergedAttribute ); + + } + + // merge morph attributes + + for ( const name in morphAttributes ) { + + const numMorphTargets = morphAttributes[ name ][ 0 ].length; + + if ( numMorphTargets === 0 ) break; + + mergedGeometry.morphAttributes = mergedGeometry.morphAttributes || {}; + mergedGeometry.morphAttributes[ name ] = []; + + for ( let i = 0; i < numMorphTargets; ++ i ) { + + const morphAttributesToMerge = []; + + for ( let j = 0; j < morphAttributes[ name ].length; ++ j ) { + + morphAttributesToMerge.push( morphAttributes[ name ][ j ][ i ] ); + + } + + const mergedMorphAttribute = mergeAttributes( morphAttributesToMerge ); + + if ( ! mergedMorphAttribute ) { + + console.error( 'THREE.BufferGeometryUtils: .mergeGeometries() failed while trying to merge the ' + name + ' morphAttribute.' ); + return null; + + } + + mergedGeometry.morphAttributes[ name ].push( mergedMorphAttribute ); + + } + + } + + return mergedGeometry; + +} + +/** + * @param {Array} attributes + * @return {BufferAttribute} + */ +function mergeAttributes( attributes ) { + + let TypedArray; + let itemSize; + let normalized; + let gpuType = - 1; + let arrayLength = 0; + + for ( let i = 0; i < attributes.length; ++ i ) { + + const attribute = attributes[ i ]; + + if ( TypedArray === undefined ) TypedArray = attribute.array.constructor; + if ( TypedArray !== attribute.array.constructor ) { + + console.error( 'THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.array must be of consistent array types across matching attributes.' ); + return null; + + } + + if ( itemSize === undefined ) itemSize = attribute.itemSize; + if ( itemSize !== attribute.itemSize ) { + + console.error( 'THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.itemSize must be consistent across matching attributes.' ); + return null; + + } + + if ( normalized === undefined ) normalized = attribute.normalized; + if ( normalized !== attribute.normalized ) { + + console.error( 'THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.normalized must be consistent across matching attributes.' ); + return null; + + } + + if ( gpuType === - 1 ) gpuType = attribute.gpuType; + if ( gpuType !== attribute.gpuType ) { + + console.error( 'THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.gpuType must be consistent across matching attributes.' ); + return null; + + } + + arrayLength += attribute.count * itemSize; + + } + + const array = new TypedArray( arrayLength ); + const result = new BufferAttribute( array, itemSize, normalized ); + let offset = 0; + + for ( let i = 0; i < attributes.length; ++ i ) { + + const attribute = attributes[ i ]; + if ( attribute.isInterleavedBufferAttribute ) { + + const tupleOffset = offset / itemSize; + for ( let j = 0, l = attribute.count; j < l; j ++ ) { + + for ( let c = 0; c < itemSize; c ++ ) { + + const value = attribute.getComponent( j, c ); + result.setComponent( j + tupleOffset, c, value ); + + } + + } + + } else { + + array.set( attribute.array, offset ); + + } + + offset += attribute.count * itemSize; + + } + + if ( gpuType !== undefined ) { + + result.gpuType = gpuType; + + } + + return result; + +} + +/** + * @param {BufferAttribute} + * @return {BufferAttribute} + */ +export function deepCloneAttribute( attribute ) { + + if ( attribute.isInstancedInterleavedBufferAttribute || attribute.isInterleavedBufferAttribute ) { + + return deinterleaveAttribute( attribute ); + + } + + if ( attribute.isInstancedBufferAttribute ) { + + return new InstancedBufferAttribute().copy( attribute ); + + } + + return new BufferAttribute().copy( attribute ); + +} + +/** + * @param {Array} attributes + * @return {Array} + */ +function interleaveAttributes( attributes ) { + + // Interleaves the provided attributes into an InterleavedBuffer and returns + // a set of InterleavedBufferAttributes for each attribute + let TypedArray; + let arrayLength = 0; + let stride = 0; + + // calculate the length and type of the interleavedBuffer + for ( let i = 0, l = attributes.length; i < l; ++ i ) { + + const attribute = attributes[ i ]; + + if ( TypedArray === undefined ) TypedArray = attribute.array.constructor; + if ( TypedArray !== attribute.array.constructor ) { + + console.error( 'AttributeBuffers of different types cannot be interleaved' ); + return null; + + } + + arrayLength += attribute.array.length; + stride += attribute.itemSize; + + } + + // Create the set of buffer attributes + const interleavedBuffer = new InterleavedBuffer( new TypedArray( arrayLength ), stride ); + let offset = 0; + const res = []; + const getters = [ 'getX', 'getY', 'getZ', 'getW' ]; + const setters = [ 'setX', 'setY', 'setZ', 'setW' ]; + + for ( let j = 0, l = attributes.length; j < l; j ++ ) { + + const attribute = attributes[ j ]; + const itemSize = attribute.itemSize; + const count = attribute.count; + const iba = new InterleavedBufferAttribute( interleavedBuffer, itemSize, offset, attribute.normalized ); + res.push( iba ); + + offset += itemSize; + + // Move the data for each attribute into the new interleavedBuffer + // at the appropriate offset + for ( let c = 0; c < count; c ++ ) { + + for ( let k = 0; k < itemSize; k ++ ) { + + iba[ setters[ k ] ]( c, attribute[ getters[ k ] ]( c ) ); + + } + + } + + } + + return res; + +} + +// returns a new, non-interleaved version of the provided attribute +export function deinterleaveAttribute( attribute ) { + + const cons = attribute.data.array.constructor; + const count = attribute.count; + const itemSize = attribute.itemSize; + const normalized = attribute.normalized; + + const array = new cons( count * itemSize ); + let newAttribute; + if ( attribute.isInstancedInterleavedBufferAttribute ) { + + newAttribute = new InstancedBufferAttribute( array, itemSize, normalized, attribute.meshPerAttribute ); + + } else { + + newAttribute = new BufferAttribute( array, itemSize, normalized ); + + } + + for ( let i = 0; i < count; i ++ ) { + + newAttribute.setX( i, attribute.getX( i ) ); + + if ( itemSize >= 2 ) { + + newAttribute.setY( i, attribute.getY( i ) ); + + } + + if ( itemSize >= 3 ) { + + newAttribute.setZ( i, attribute.getZ( i ) ); + + } + + if ( itemSize >= 4 ) { + + newAttribute.setW( i, attribute.getW( i ) ); + + } + + } + + return newAttribute; + +} + +// deinterleaves all attributes on the geometry +export function deinterleaveGeometry( geometry ) { + + const attributes = geometry.attributes; + const morphTargets = geometry.morphTargets; + const attrMap = new Map(); + + for ( const key in attributes ) { + + const attr = attributes[ key ]; + if ( attr.isInterleavedBufferAttribute ) { + + if ( ! attrMap.has( attr ) ) { + + attrMap.set( attr, deinterleaveAttribute( attr ) ); + + } + + attributes[ key ] = attrMap.get( attr ); + + } + + } + + for ( const key in morphTargets ) { + + const attr = morphTargets[ key ]; + if ( attr.isInterleavedBufferAttribute ) { + + if ( ! attrMap.has( attr ) ) { + + attrMap.set( attr, deinterleaveAttribute( attr ) ); + + } + + morphTargets[ key ] = attrMap.get( attr ); + + } + + } + +} + +/** + * @param {BufferGeometry} geometry + * @return {number} + */ +function estimateBytesUsed( geometry ) { + + // Return the estimated memory used by this geometry in bytes + // Calculate using itemSize, count, and BYTES_PER_ELEMENT to account + // for InterleavedBufferAttributes. + let mem = 0; + for ( const name in geometry.attributes ) { + + const attr = geometry.getAttribute( name ); + mem += attr.count * attr.itemSize * attr.array.BYTES_PER_ELEMENT; + + } + + const indices = geometry.getIndex(); + mem += indices ? indices.count * indices.itemSize * indices.array.BYTES_PER_ELEMENT : 0; + return mem; + +} + +/** + * @param {BufferGeometry} geometry + * @param {number} tolerance + * @return {BufferGeometry} + */ +function mergeVertices( geometry, tolerance = 1e-4 ) { + + tolerance = Math.max( tolerance, Number.EPSILON ); + + // Generate an index buffer if the geometry doesn't have one, or optimize it + // if it's already available. + const hashToIndex = {}; + const indices = geometry.getIndex(); + const positions = geometry.getAttribute( 'position' ); + const vertexCount = indices ? indices.count : positions.count; + + // next value for triangle indices + let nextIndex = 0; + + // attributes and new attribute arrays + const attributeNames = Object.keys( geometry.attributes ); + const tmpAttributes = {}; + const tmpMorphAttributes = {}; + const newIndices = []; + const getters = [ 'getX', 'getY', 'getZ', 'getW' ]; + const setters = [ 'setX', 'setY', 'setZ', 'setW' ]; + + // Initialize the arrays, allocating space conservatively. Extra + // space will be trimmed in the last step. + for ( let i = 0, l = attributeNames.length; i < l; i ++ ) { + + const name = attributeNames[ i ]; + const attr = geometry.attributes[ name ]; + + tmpAttributes[ name ] = new BufferAttribute( + new attr.array.constructor( attr.count * attr.itemSize ), + attr.itemSize, + attr.normalized + ); + + const morphAttr = geometry.morphAttributes[ name ]; + if ( morphAttr ) { + + tmpMorphAttributes[ name ] = new BufferAttribute( + new morphAttr.array.constructor( morphAttr.count * morphAttr.itemSize ), + morphAttr.itemSize, + morphAttr.normalized + ); + + } + + } + + // convert the error tolerance to an amount of decimal places to truncate to + const halfTolerance = tolerance * 0.5; + const exponent = Math.log10( 1 / tolerance ); + const hashMultiplier = Math.pow( 10, exponent ); + const hashAdditive = halfTolerance * hashMultiplier; + for ( let i = 0; i < vertexCount; i ++ ) { + + const index = indices ? indices.getX( i ) : i; + + // Generate a hash for the vertex attributes at the current index 'i' + let hash = ''; + for ( let j = 0, l = attributeNames.length; j < l; j ++ ) { + + const name = attributeNames[ j ]; + const attribute = geometry.getAttribute( name ); + const itemSize = attribute.itemSize; + + for ( let k = 0; k < itemSize; k ++ ) { + + // double tilde truncates the decimal value + hash += `${ ~ ~ ( attribute[ getters[ k ] ]( index ) * hashMultiplier + hashAdditive ) },`; + + } + + } + + // Add another reference to the vertex if it's already + // used by another index + if ( hash in hashToIndex ) { + + newIndices.push( hashToIndex[ hash ] ); + + } else { + + // copy data to the new index in the temporary attributes + for ( let j = 0, l = attributeNames.length; j < l; j ++ ) { + + const name = attributeNames[ j ]; + const attribute = geometry.getAttribute( name ); + const morphAttr = geometry.morphAttributes[ name ]; + const itemSize = attribute.itemSize; + const newarray = tmpAttributes[ name ]; + const newMorphArrays = tmpMorphAttributes[ name ]; + + for ( let k = 0; k < itemSize; k ++ ) { + + const getterFunc = getters[ k ]; + const setterFunc = setters[ k ]; + newarray[ setterFunc ]( nextIndex, attribute[ getterFunc ]( index ) ); + + if ( morphAttr ) { + + for ( let m = 0, ml = morphAttr.length; m < ml; m ++ ) { + + newMorphArrays[ m ][ setterFunc ]( nextIndex, morphAttr[ m ][ getterFunc ]( index ) ); + + } + + } + + } + + } + + hashToIndex[ hash ] = nextIndex; + newIndices.push( nextIndex ); + nextIndex ++; + + } + + } + + // generate result BufferGeometry + const result = geometry.clone(); + for ( const name in geometry.attributes ) { + + const tmpAttribute = tmpAttributes[ name ]; + + result.setAttribute( name, new BufferAttribute( + tmpAttribute.array.slice( 0, nextIndex * tmpAttribute.itemSize ), + tmpAttribute.itemSize, + tmpAttribute.normalized, + ) ); + + if ( ! ( name in tmpMorphAttributes ) ) continue; + + for ( let j = 0; j < tmpMorphAttributes[ name ].length; j ++ ) { + + const tmpMorphAttribute = tmpMorphAttributes[ name ][ j ]; + + result.morphAttributes[ name ][ j ] = new BufferAttribute( + tmpMorphAttribute.array.slice( 0, nextIndex * tmpMorphAttribute.itemSize ), + tmpMorphAttribute.itemSize, + tmpMorphAttribute.normalized, + ); + + } + + } + + // indices + + result.setIndex( newIndices ); + + return result; + +} + +/** + * @param {BufferGeometry} geometry + * @param {number} drawMode + * @return {BufferGeometry} + */ +function toTrianglesDrawMode( geometry, drawMode ) { + + if ( drawMode === TrianglesDrawMode ) { + + console.warn( 'THREE.BufferGeometryUtils.toTrianglesDrawMode(): Geometry already defined as triangles.' ); + return geometry; + + } + + if ( drawMode === TriangleFanDrawMode || drawMode === TriangleStripDrawMode ) { + + let index = geometry.getIndex(); + + // generate index if not present + + if ( index === null ) { + + const indices = []; + + const position = geometry.getAttribute( 'position' ); + + if ( position !== undefined ) { + + for ( let i = 0; i < position.count; i ++ ) { + + indices.push( i ); + + } + + geometry.setIndex( indices ); + index = geometry.getIndex(); + + } else { + + console.error( 'THREE.BufferGeometryUtils.toTrianglesDrawMode(): Undefined position attribute. Processing not possible.' ); + return geometry; + + } + + } + + // + + const numberOfTriangles = index.count - 2; + const newIndices = []; + + if ( drawMode === TriangleFanDrawMode ) { + + // gl.TRIANGLE_FAN + + for ( let i = 1; i <= numberOfTriangles; i ++ ) { + + newIndices.push( index.getX( 0 ) ); + newIndices.push( index.getX( i ) ); + newIndices.push( index.getX( i + 1 ) ); + + } + + } else { + + // gl.TRIANGLE_STRIP + + for ( let i = 0; i < numberOfTriangles; i ++ ) { + + if ( i % 2 === 0 ) { + + newIndices.push( index.getX( i ) ); + newIndices.push( index.getX( i + 1 ) ); + newIndices.push( index.getX( i + 2 ) ); + + } else { + + newIndices.push( index.getX( i + 2 ) ); + newIndices.push( index.getX( i + 1 ) ); + newIndices.push( index.getX( i ) ); + + } + + } + + } + + if ( ( newIndices.length / 3 ) !== numberOfTriangles ) { + + console.error( 'THREE.BufferGeometryUtils.toTrianglesDrawMode(): Unable to generate correct amount of triangles.' ); + + } + + // build final geometry + + const newGeometry = geometry.clone(); + newGeometry.setIndex( newIndices ); + newGeometry.clearGroups(); + + return newGeometry; + + } else { + + console.error( 'THREE.BufferGeometryUtils.toTrianglesDrawMode(): Unknown draw mode:', drawMode ); + return geometry; + + } + +} + +/** + * Calculates the morphed attributes of a morphed/skinned BufferGeometry. + * Helpful for Raytracing or Decals. + * @param {Mesh | Line | Points} object An instance of Mesh, Line or Points. + * @return {Object} An Object with original position/normal attributes and morphed ones. + */ +function computeMorphedAttributes( object ) { + + const _vA = new Vector3(); + const _vB = new Vector3(); + const _vC = new Vector3(); + + const _tempA = new Vector3(); + const _tempB = new Vector3(); + const _tempC = new Vector3(); + + const _morphA = new Vector3(); + const _morphB = new Vector3(); + const _morphC = new Vector3(); + + function _calculateMorphedAttributeData( + object, + attribute, + morphAttribute, + morphTargetsRelative, + a, + b, + c, + modifiedAttributeArray + ) { + + _vA.fromBufferAttribute( attribute, a ); + _vB.fromBufferAttribute( attribute, b ); + _vC.fromBufferAttribute( attribute, c ); + + const morphInfluences = object.morphTargetInfluences; + + if ( morphAttribute && morphInfluences ) { + + _morphA.set( 0, 0, 0 ); + _morphB.set( 0, 0, 0 ); + _morphC.set( 0, 0, 0 ); + + for ( let i = 0, il = morphAttribute.length; i < il; i ++ ) { + + const influence = morphInfluences[ i ]; + const morph = morphAttribute[ i ]; + + if ( influence === 0 ) continue; + + _tempA.fromBufferAttribute( morph, a ); + _tempB.fromBufferAttribute( morph, b ); + _tempC.fromBufferAttribute( morph, c ); + + if ( morphTargetsRelative ) { + + _morphA.addScaledVector( _tempA, influence ); + _morphB.addScaledVector( _tempB, influence ); + _morphC.addScaledVector( _tempC, influence ); + + } else { + + _morphA.addScaledVector( _tempA.sub( _vA ), influence ); + _morphB.addScaledVector( _tempB.sub( _vB ), influence ); + _morphC.addScaledVector( _tempC.sub( _vC ), influence ); + + } + + } + + _vA.add( _morphA ); + _vB.add( _morphB ); + _vC.add( _morphC ); + + } + + if ( object.isSkinnedMesh ) { + + object.applyBoneTransform( a, _vA ); + object.applyBoneTransform( b, _vB ); + object.applyBoneTransform( c, _vC ); + + } + + modifiedAttributeArray[ a * 3 + 0 ] = _vA.x; + modifiedAttributeArray[ a * 3 + 1 ] = _vA.y; + modifiedAttributeArray[ a * 3 + 2 ] = _vA.z; + modifiedAttributeArray[ b * 3 + 0 ] = _vB.x; + modifiedAttributeArray[ b * 3 + 1 ] = _vB.y; + modifiedAttributeArray[ b * 3 + 2 ] = _vB.z; + modifiedAttributeArray[ c * 3 + 0 ] = _vC.x; + modifiedAttributeArray[ c * 3 + 1 ] = _vC.y; + modifiedAttributeArray[ c * 3 + 2 ] = _vC.z; + + } + + const geometry = object.geometry; + const material = object.material; + + let a, b, c; + const index = geometry.index; + const positionAttribute = geometry.attributes.position; + const morphPosition = geometry.morphAttributes.position; + const morphTargetsRelative = geometry.morphTargetsRelative; + const normalAttribute = geometry.attributes.normal; + const morphNormal = geometry.morphAttributes.position; + + const groups = geometry.groups; + const drawRange = geometry.drawRange; + let i, j, il, jl; + let group; + let start, end; + + const modifiedPosition = new Float32Array( positionAttribute.count * positionAttribute.itemSize ); + const modifiedNormal = new Float32Array( normalAttribute.count * normalAttribute.itemSize ); + + if ( index !== null ) { + + // indexed buffer geometry + + if ( Array.isArray( material ) ) { + + for ( i = 0, il = groups.length; i < il; i ++ ) { + + group = groups[ i ]; + + start = Math.max( group.start, drawRange.start ); + end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) ); + + for ( j = start, jl = end; j < jl; j += 3 ) { + + a = index.getX( j ); + b = index.getX( j + 1 ); + c = index.getX( j + 2 ); + + _calculateMorphedAttributeData( + object, + positionAttribute, + morphPosition, + morphTargetsRelative, + a, b, c, + modifiedPosition + ); + + _calculateMorphedAttributeData( + object, + normalAttribute, + morphNormal, + morphTargetsRelative, + a, b, c, + modifiedNormal + ); + + } + + } + + } else { + + start = Math.max( 0, drawRange.start ); + end = Math.min( index.count, ( drawRange.start + drawRange.count ) ); + + for ( i = start, il = end; i < il; i += 3 ) { + + a = index.getX( i ); + b = index.getX( i + 1 ); + c = index.getX( i + 2 ); + + _calculateMorphedAttributeData( + object, + positionAttribute, + morphPosition, + morphTargetsRelative, + a, b, c, + modifiedPosition + ); + + _calculateMorphedAttributeData( + object, + normalAttribute, + morphNormal, + morphTargetsRelative, + a, b, c, + modifiedNormal + ); + + } + + } + + } else { + + // non-indexed buffer geometry + + if ( Array.isArray( material ) ) { + + for ( i = 0, il = groups.length; i < il; i ++ ) { + + group = groups[ i ]; + + start = Math.max( group.start, drawRange.start ); + end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) ); + + for ( j = start, jl = end; j < jl; j += 3 ) { + + a = j; + b = j + 1; + c = j + 2; + + _calculateMorphedAttributeData( + object, + positionAttribute, + morphPosition, + morphTargetsRelative, + a, b, c, + modifiedPosition + ); + + _calculateMorphedAttributeData( + object, + normalAttribute, + morphNormal, + morphTargetsRelative, + a, b, c, + modifiedNormal + ); + + } + + } + + } else { + + start = Math.max( 0, drawRange.start ); + end = Math.min( positionAttribute.count, ( drawRange.start + drawRange.count ) ); + + for ( i = start, il = end; i < il; i += 3 ) { + + a = i; + b = i + 1; + c = i + 2; + + _calculateMorphedAttributeData( + object, + positionAttribute, + morphPosition, + morphTargetsRelative, + a, b, c, + modifiedPosition + ); + + _calculateMorphedAttributeData( + object, + normalAttribute, + morphNormal, + morphTargetsRelative, + a, b, c, + modifiedNormal + ); + + } + + } + + } + + const morphedPositionAttribute = new Float32BufferAttribute( modifiedPosition, 3 ); + const morphedNormalAttribute = new Float32BufferAttribute( modifiedNormal, 3 ); + + return { + + positionAttribute: positionAttribute, + normalAttribute: normalAttribute, + morphedPositionAttribute: morphedPositionAttribute, + morphedNormalAttribute: morphedNormalAttribute + + }; + +} + +function mergeGroups( geometry ) { + + if ( geometry.groups.length === 0 ) { + + console.warn( 'THREE.BufferGeometryUtils.mergeGroups(): No groups are defined. Nothing to merge.' ); + return geometry; + + } + + let groups = geometry.groups; + + // sort groups by material index + + groups = groups.sort( ( a, b ) => { + + if ( a.materialIndex !== b.materialIndex ) return a.materialIndex - b.materialIndex; + + return a.start - b.start; + + } ); + + // create index for non-indexed geometries + + if ( geometry.getIndex() === null ) { + + const positionAttribute = geometry.getAttribute( 'position' ); + const indices = []; + + for ( let i = 0; i < positionAttribute.count; i += 3 ) { + + indices.push( i, i + 1, i + 2 ); + + } + + geometry.setIndex( indices ); + + } + + // sort index + + const index = geometry.getIndex(); + + const newIndices = []; + + for ( let i = 0; i < groups.length; i ++ ) { + + const group = groups[ i ]; + + const groupStart = group.start; + const groupLength = groupStart + group.count; + + for ( let j = groupStart; j < groupLength; j ++ ) { + + newIndices.push( index.getX( j ) ); + + } + + } + + geometry.dispose(); // Required to force buffer recreation + geometry.setIndex( newIndices ); + + // update groups indices + + let start = 0; + + for ( let i = 0; i < groups.length; i ++ ) { + + const group = groups[ i ]; + + group.start = start; + start += group.count; + + } + + // merge groups + + let currentGroup = groups[ 0 ]; + + geometry.groups = [ currentGroup ]; + + for ( let i = 1; i < groups.length; i ++ ) { + + const group = groups[ i ]; + + if ( currentGroup.materialIndex === group.materialIndex ) { + + currentGroup.count += group.count; + + } else { + + currentGroup = group; + geometry.groups.push( currentGroup ); + + } + + } + + return geometry; + +} + + +/** + * Modifies the supplied geometry if it is non-indexed, otherwise creates a new, + * non-indexed geometry. Returns the geometry with smooth normals everywhere except + * faces that meet at an angle greater than the crease angle. + * + * @param {BufferGeometry} geometry + * @param {number} [creaseAngle] + * @return {BufferGeometry} + */ +function toCreasedNormals( geometry, creaseAngle = Math.PI / 3 /* 60 degrees */ ) { + + const creaseDot = Math.cos( creaseAngle ); + const hashMultiplier = ( 1 + 1e-10 ) * 1e2; + + // reusable vectors + const verts = [ new Vector3(), new Vector3(), new Vector3() ]; + const tempVec1 = new Vector3(); + const tempVec2 = new Vector3(); + const tempNorm = new Vector3(); + const tempNorm2 = new Vector3(); + + // hashes a vector + function hashVertex( v ) { + + const x = ~ ~ ( v.x * hashMultiplier ); + const y = ~ ~ ( v.y * hashMultiplier ); + const z = ~ ~ ( v.z * hashMultiplier ); + return `${x},${y},${z}`; + + } + + // BufferGeometry.toNonIndexed() warns if the geometry is non-indexed + // and returns the original geometry + const resultGeometry = geometry.index ? geometry.toNonIndexed() : geometry; + const posAttr = resultGeometry.attributes.position; + const vertexMap = {}; + + // find all the normals shared by commonly located vertices + for ( let i = 0, l = posAttr.count / 3; i < l; i ++ ) { + + const i3 = 3 * i; + const a = verts[ 0 ].fromBufferAttribute( posAttr, i3 + 0 ); + const b = verts[ 1 ].fromBufferAttribute( posAttr, i3 + 1 ); + const c = verts[ 2 ].fromBufferAttribute( posAttr, i3 + 2 ); + + tempVec1.subVectors( c, b ); + tempVec2.subVectors( a, b ); + + // add the normal to the map for all vertices + const normal = new Vector3().crossVectors( tempVec1, tempVec2 ).normalize(); + for ( let n = 0; n < 3; n ++ ) { + + const vert = verts[ n ]; + const hash = hashVertex( vert ); + if ( ! ( hash in vertexMap ) ) { + + vertexMap[ hash ] = []; + + } + + vertexMap[ hash ].push( normal ); + + } + + } + + // average normals from all vertices that share a common location if they are within the + // provided crease threshold + const normalArray = new Float32Array( posAttr.count * 3 ); + const normAttr = new BufferAttribute( normalArray, 3, false ); + for ( let i = 0, l = posAttr.count / 3; i < l; i ++ ) { + + // get the face normal for this vertex + const i3 = 3 * i; + const a = verts[ 0 ].fromBufferAttribute( posAttr, i3 + 0 ); + const b = verts[ 1 ].fromBufferAttribute( posAttr, i3 + 1 ); + const c = verts[ 2 ].fromBufferAttribute( posAttr, i3 + 2 ); + + tempVec1.subVectors( c, b ); + tempVec2.subVectors( a, b ); + + tempNorm.crossVectors( tempVec1, tempVec2 ).normalize(); + + // average all normals that meet the threshold and set the normal value + for ( let n = 0; n < 3; n ++ ) { + + const vert = verts[ n ]; + const hash = hashVertex( vert ); + const otherNormals = vertexMap[ hash ]; + tempNorm2.set( 0, 0, 0 ); + + for ( let k = 0, lk = otherNormals.length; k < lk; k ++ ) { + + const otherNorm = otherNormals[ k ]; + if ( tempNorm.dot( otherNorm ) > creaseDot ) { + + tempNorm2.add( otherNorm ); + + } + + } + + tempNorm2.normalize(); + normAttr.setXYZ( i3 + n, tempNorm2.x, tempNorm2.y, tempNorm2.z ); + + } + + } + + resultGeometry.setAttribute( 'normal', normAttr ); + return resultGeometry; + +} + +export { + computeMikkTSpaceTangents, + mergeGeometries, + mergeAttributes, + interleaveAttributes, + estimateBytesUsed, + mergeVertices, + toTrianglesDrawMode, + computeMorphedAttributes, + mergeGroups, + toCreasedNormals +}; diff --git a/src/box-winUI/Assets/home-globe/vendor/DRACOLoader.js b/src/box-winUI/Assets/home-globe/vendor/DRACOLoader.js new file mode 100644 index 0000000..b176ebf --- /dev/null +++ b/src/box-winUI/Assets/home-globe/vendor/DRACOLoader.js @@ -0,0 +1,613 @@ +import { + BufferAttribute, + BufferGeometry, + Color, + FileLoader, + Loader, + LinearSRGBColorSpace, + SRGBColorSpace +} from 'three'; + +const _taskCache = new WeakMap(); + +class DRACOLoader extends Loader { + + constructor( manager ) { + + super( manager ); + + this.decoderPath = ''; + this.decoderConfig = {}; + this.decoderBinary = null; + this.decoderPending = null; + + this.workerLimit = 4; + this.workerPool = []; + this.workerNextTaskID = 1; + this.workerSourceURL = ''; + + this.defaultAttributeIDs = { + position: 'POSITION', + normal: 'NORMAL', + color: 'COLOR', + uv: 'TEX_COORD' + }; + this.defaultAttributeTypes = { + position: 'Float32Array', + normal: 'Float32Array', + color: 'Float32Array', + uv: 'Float32Array' + }; + + } + + setDecoderPath( path ) { + + this.decoderPath = path; + + return this; + + } + + setDecoderConfig( config ) { + + this.decoderConfig = config; + + return this; + + } + + setWorkerLimit( workerLimit ) { + + this.workerLimit = workerLimit; + + return this; + + } + + load( url, onLoad, onProgress, onError ) { + + const loader = new FileLoader( this.manager ); + + loader.setPath( this.path ); + loader.setResponseType( 'arraybuffer' ); + loader.setRequestHeader( this.requestHeader ); + loader.setWithCredentials( this.withCredentials ); + + loader.load( url, ( buffer ) => { + + this.parse( buffer, onLoad, onError ); + + }, onProgress, onError ); + + } + + + parse( buffer, onLoad, onError = ()=>{} ) { + + this.decodeDracoFile( buffer, onLoad, null, null, SRGBColorSpace ).catch( onError ); + + } + + decodeDracoFile( buffer, callback, attributeIDs, attributeTypes, vertexColorSpace = LinearSRGBColorSpace, onError = () => {} ) { + + const taskConfig = { + attributeIDs: attributeIDs || this.defaultAttributeIDs, + attributeTypes: attributeTypes || this.defaultAttributeTypes, + useUniqueIDs: !! attributeIDs, + vertexColorSpace: vertexColorSpace, + }; + + return this.decodeGeometry( buffer, taskConfig ).then( callback ).catch( onError ); + + } + + decodeGeometry( buffer, taskConfig ) { + + const taskKey = JSON.stringify( taskConfig ); + + // Check for an existing task using this buffer. A transferred buffer cannot be transferred + // again from this thread. + if ( _taskCache.has( buffer ) ) { + + const cachedTask = _taskCache.get( buffer ); + + if ( cachedTask.key === taskKey ) { + + return cachedTask.promise; + + } else if ( buffer.byteLength === 0 ) { + + // Technically, it would be possible to wait for the previous task to complete, + // transfer the buffer back, and decode again with the second configuration. That + // is complex, and I don't know of any reason to decode a Draco buffer twice in + // different ways, so this is left unimplemented. + throw new Error( + + 'THREE.DRACOLoader: Unable to re-decode a buffer with different ' + + 'settings. Buffer has already been transferred.' + + ); + + } + + } + + // + + let worker; + const taskID = this.workerNextTaskID ++; + const taskCost = buffer.byteLength; + + // Obtain a worker and assign a task, and construct a geometry instance + // when the task completes. + const geometryPending = this._getWorker( taskID, taskCost ) + .then( ( _worker ) => { + + worker = _worker; + + return new Promise( ( resolve, reject ) => { + + worker._callbacks[ taskID ] = { resolve, reject }; + + worker.postMessage( { type: 'decode', id: taskID, taskConfig, buffer }, [ buffer ] ); + + // this.debug(); + + } ); + + } ) + .then( ( message ) => this._createGeometry( message.geometry ) ); + + // Remove task from the task list. + // Note: replaced '.finally()' with '.catch().then()' block - iOS 11 support (#19416) + geometryPending + .catch( () => true ) + .then( () => { + + if ( worker && taskID ) { + + this._releaseTask( worker, taskID ); + + // this.debug(); + + } + + } ); + + // Cache the task result. + _taskCache.set( buffer, { + + key: taskKey, + promise: geometryPending + + } ); + + return geometryPending; + + } + + _createGeometry( geometryData ) { + + const geometry = new BufferGeometry(); + + if ( geometryData.index ) { + + geometry.setIndex( new BufferAttribute( geometryData.index.array, 1 ) ); + + } + + for ( let i = 0; i < geometryData.attributes.length; i ++ ) { + + const result = geometryData.attributes[ i ]; + const name = result.name; + const array = result.array; + const itemSize = result.itemSize; + + const attribute = new BufferAttribute( array, itemSize ); + + if ( name === 'color' ) { + + this._assignVertexColorSpace( attribute, result.vertexColorSpace ); + + attribute.normalized = ( array instanceof Float32Array ) === false; + + } + + geometry.setAttribute( name, attribute ); + + } + + return geometry; + + } + + _assignVertexColorSpace( attribute, inputColorSpace ) { + + // While .drc files do not specify colorspace, the only 'official' tooling + // is PLY and OBJ converters, which use sRGB. We'll assume sRGB when a .drc + // file is passed into .load() or .parse(). GLTFLoader uses internal APIs + // to decode geometry, and vertex colors are already Linear-sRGB in there. + + if ( inputColorSpace !== SRGBColorSpace ) return; + + const _color = new Color(); + + for ( let i = 0, il = attribute.count; i < il; i ++ ) { + + _color.fromBufferAttribute( attribute, i ).convertSRGBToLinear(); + attribute.setXYZ( i, _color.r, _color.g, _color.b ); + + } + + } + + _loadLibrary( url, responseType ) { + + const loader = new FileLoader( this.manager ); + loader.setPath( this.decoderPath ); + loader.setResponseType( responseType ); + loader.setWithCredentials( this.withCredentials ); + + return new Promise( ( resolve, reject ) => { + + loader.load( url, resolve, undefined, reject ); + + } ); + + } + + preload() { + + this._initDecoder(); + + return this; + + } + + _initDecoder() { + + if ( this.decoderPending ) return this.decoderPending; + + const useJS = typeof WebAssembly !== 'object' || this.decoderConfig.type === 'js'; + const librariesPending = []; + + if ( useJS ) { + + librariesPending.push( this._loadLibrary( 'draco_decoder.js', 'text' ) ); + + } else { + + librariesPending.push( this._loadLibrary( 'draco_wasm_wrapper.js', 'text' ) ); + librariesPending.push( this._loadLibrary( 'draco_decoder.wasm', 'arraybuffer' ) ); + + } + + this.decoderPending = Promise.all( librariesPending ) + .then( ( libraries ) => { + + const jsContent = libraries[ 0 ]; + + if ( ! useJS ) { + + this.decoderConfig.wasmBinary = libraries[ 1 ]; + + } + + const fn = DRACOWorker.toString(); + + const body = [ + '/* draco decoder */', + jsContent, + '', + '/* worker */', + fn.substring( fn.indexOf( '{' ) + 1, fn.lastIndexOf( '}' ) ) + ].join( '\n' ); + + this.workerSourceURL = URL.createObjectURL( new Blob( [ body ] ) ); + + } ); + + return this.decoderPending; + + } + + _getWorker( taskID, taskCost ) { + + return this._initDecoder().then( () => { + + if ( this.workerPool.length < this.workerLimit ) { + + const worker = new Worker( this.workerSourceURL ); + + worker._callbacks = {}; + worker._taskCosts = {}; + worker._taskLoad = 0; + + worker.postMessage( { type: 'init', decoderConfig: this.decoderConfig } ); + + worker.onmessage = function ( e ) { + + const message = e.data; + + switch ( message.type ) { + + case 'decode': + worker._callbacks[ message.id ].resolve( message ); + break; + + case 'error': + worker._callbacks[ message.id ].reject( message ); + break; + + default: + console.error( 'THREE.DRACOLoader: Unexpected message, "' + message.type + '"' ); + + } + + }; + + this.workerPool.push( worker ); + + } else { + + this.workerPool.sort( function ( a, b ) { + + return a._taskLoad > b._taskLoad ? - 1 : 1; + + } ); + + } + + const worker = this.workerPool[ this.workerPool.length - 1 ]; + worker._taskCosts[ taskID ] = taskCost; + worker._taskLoad += taskCost; + return worker; + + } ); + + } + + _releaseTask( worker, taskID ) { + + worker._taskLoad -= worker._taskCosts[ taskID ]; + delete worker._callbacks[ taskID ]; + delete worker._taskCosts[ taskID ]; + + } + + debug() { + + console.log( 'Task load: ', this.workerPool.map( ( worker ) => worker._taskLoad ) ); + + } + + dispose() { + + for ( let i = 0; i < this.workerPool.length; ++ i ) { + + this.workerPool[ i ].terminate(); + + } + + this.workerPool.length = 0; + + if ( this.workerSourceURL !== '' ) { + + URL.revokeObjectURL( this.workerSourceURL ); + + } + + return this; + + } + +} + +/* WEB WORKER */ + +function DRACOWorker() { + + let decoderConfig; + let decoderPending; + + onmessage = function ( e ) { + + const message = e.data; + + switch ( message.type ) { + + case 'init': + decoderConfig = message.decoderConfig; + decoderPending = new Promise( function ( resolve/*, reject*/ ) { + + decoderConfig.onModuleLoaded = function ( draco ) { + + // Module is Promise-like. Wrap before resolving to avoid loop. + resolve( { draco: draco } ); + + }; + + DracoDecoderModule( decoderConfig ); // eslint-disable-line no-undef + + } ); + break; + + case 'decode': + const buffer = message.buffer; + const taskConfig = message.taskConfig; + decoderPending.then( ( module ) => { + + const draco = module.draco; + const decoder = new draco.Decoder(); + + try { + + const geometry = decodeGeometry( draco, decoder, new Int8Array( buffer ), taskConfig ); + + const buffers = geometry.attributes.map( ( attr ) => attr.array.buffer ); + + if ( geometry.index ) buffers.push( geometry.index.array.buffer ); + + self.postMessage( { type: 'decode', id: message.id, geometry }, buffers ); + + } catch ( error ) { + + console.error( error ); + + self.postMessage( { type: 'error', id: message.id, error: error.message } ); + + } finally { + + draco.destroy( decoder ); + + } + + } ); + break; + + } + + }; + + function decodeGeometry( draco, decoder, array, taskConfig ) { + + const attributeIDs = taskConfig.attributeIDs; + const attributeTypes = taskConfig.attributeTypes; + + let dracoGeometry; + let decodingStatus; + + const geometryType = decoder.GetEncodedGeometryType( array ); + + if ( geometryType === draco.TRIANGULAR_MESH ) { + + dracoGeometry = new draco.Mesh(); + decodingStatus = decoder.DecodeArrayToMesh( array, array.byteLength, dracoGeometry ); + + } else if ( geometryType === draco.POINT_CLOUD ) { + + dracoGeometry = new draco.PointCloud(); + decodingStatus = decoder.DecodeArrayToPointCloud( array, array.byteLength, dracoGeometry ); + + } else { + + throw new Error( 'THREE.DRACOLoader: Unexpected geometry type.' ); + + } + + if ( ! decodingStatus.ok() || dracoGeometry.ptr === 0 ) { + + throw new Error( 'THREE.DRACOLoader: Decoding failed: ' + decodingStatus.error_msg() ); + + } + + const geometry = { index: null, attributes: [] }; + + // Gather all vertex attributes. + for ( const attributeName in attributeIDs ) { + + const attributeType = self[ attributeTypes[ attributeName ] ]; + + let attribute; + let attributeID; + + // A Draco file may be created with default vertex attributes, whose attribute IDs + // are mapped 1:1 from their semantic name (POSITION, NORMAL, ...). Alternatively, + // a Draco file may contain a custom set of attributes, identified by known unique + // IDs. glTF files always do the latter, and `.drc` files typically do the former. + if ( taskConfig.useUniqueIDs ) { + + attributeID = attributeIDs[ attributeName ]; + attribute = decoder.GetAttributeByUniqueId( dracoGeometry, attributeID ); + + } else { + + attributeID = decoder.GetAttributeId( dracoGeometry, draco[ attributeIDs[ attributeName ] ] ); + + if ( attributeID === - 1 ) continue; + + attribute = decoder.GetAttribute( dracoGeometry, attributeID ); + + } + + const attributeResult = decodeAttribute( draco, decoder, dracoGeometry, attributeName, attributeType, attribute ); + + if ( attributeName === 'color' ) { + + attributeResult.vertexColorSpace = taskConfig.vertexColorSpace; + + } + + geometry.attributes.push( attributeResult ); + + } + + // Add index. + if ( geometryType === draco.TRIANGULAR_MESH ) { + + geometry.index = decodeIndex( draco, decoder, dracoGeometry ); + + } + + draco.destroy( dracoGeometry ); + + return geometry; + + } + + function decodeIndex( draco, decoder, dracoGeometry ) { + + const numFaces = dracoGeometry.num_faces(); + const numIndices = numFaces * 3; + const byteLength = numIndices * 4; + + const ptr = draco._malloc( byteLength ); + decoder.GetTrianglesUInt32Array( dracoGeometry, byteLength, ptr ); + const index = new Uint32Array( draco.HEAPF32.buffer, ptr, numIndices ).slice(); + draco._free( ptr ); + + return { array: index, itemSize: 1 }; + + } + + function decodeAttribute( draco, decoder, dracoGeometry, attributeName, attributeType, attribute ) { + + const numComponents = attribute.num_components(); + const numPoints = dracoGeometry.num_points(); + const numValues = numPoints * numComponents; + const byteLength = numValues * attributeType.BYTES_PER_ELEMENT; + const dataType = getDracoDataType( draco, attributeType ); + + const ptr = draco._malloc( byteLength ); + decoder.GetAttributeDataArrayForAllPoints( dracoGeometry, attribute, dataType, byteLength, ptr ); + const array = new attributeType( draco.HEAPF32.buffer, ptr, numValues ).slice(); + draco._free( ptr ); + + return { + name: attributeName, + array: array, + itemSize: numComponents + }; + + } + + function getDracoDataType( draco, attributeType ) { + + switch ( attributeType ) { + + case Float32Array: return draco.DT_FLOAT32; + case Int8Array: return draco.DT_INT8; + case Int16Array: return draco.DT_INT16; + case Int32Array: return draco.DT_INT32; + case Uint8Array: return draco.DT_UINT8; + case Uint16Array: return draco.DT_UINT16; + case Uint32Array: return draco.DT_UINT32; + + } + + } + +} + +export { DRACOLoader }; diff --git a/src/box-winUI/Assets/home-globe/vendor/GLTFLoader.js b/src/box-winUI/Assets/home-globe/vendor/GLTFLoader.js new file mode 100644 index 0000000..0679071 --- /dev/null +++ b/src/box-winUI/Assets/home-globe/vendor/GLTFLoader.js @@ -0,0 +1,4722 @@ +import { + AnimationClip, + Bone, + Box3, + BufferAttribute, + BufferGeometry, + ClampToEdgeWrapping, + Color, + ColorManagement, + DirectionalLight, + DoubleSide, + FileLoader, + FrontSide, + Group, + ImageBitmapLoader, + InstancedMesh, + InterleavedBuffer, + InterleavedBufferAttribute, + Interpolant, + InterpolateDiscrete, + InterpolateLinear, + Line, + LineBasicMaterial, + LineLoop, + LineSegments, + LinearFilter, + LinearMipmapLinearFilter, + LinearMipmapNearestFilter, + LinearSRGBColorSpace, + Loader, + LoaderUtils, + Material, + MathUtils, + Matrix4, + Mesh, + MeshBasicMaterial, + MeshPhysicalMaterial, + MeshStandardMaterial, + MirroredRepeatWrapping, + NearestFilter, + NearestMipmapLinearFilter, + NearestMipmapNearestFilter, + NumberKeyframeTrack, + Object3D, + OrthographicCamera, + PerspectiveCamera, + PointLight, + Points, + PointsMaterial, + PropertyBinding, + Quaternion, + QuaternionKeyframeTrack, + RepeatWrapping, + Skeleton, + SkinnedMesh, + Sphere, + SpotLight, + Texture, + TextureLoader, + TriangleFanDrawMode, + TriangleStripDrawMode, + Vector2, + Vector3, + VectorKeyframeTrack, + SRGBColorSpace, + InstancedBufferAttribute +} from 'three'; +import { toTrianglesDrawMode } from '../utils/BufferGeometryUtils.js'; + +class GLTFLoader extends Loader { + + constructor( manager ) { + + super( manager ); + + this.dracoLoader = null; + this.ktx2Loader = null; + this.meshoptDecoder = null; + + this.pluginCallbacks = []; + + this.register( function ( parser ) { + + return new GLTFMaterialsClearcoatExtension( parser ); + + } ); + + this.register( function ( parser ) { + + return new GLTFMaterialsDispersionExtension( parser ); + + } ); + + this.register( function ( parser ) { + + return new GLTFTextureBasisUExtension( parser ); + + } ); + + this.register( function ( parser ) { + + return new GLTFTextureWebPExtension( parser ); + + } ); + + this.register( function ( parser ) { + + return new GLTFTextureAVIFExtension( parser ); + + } ); + + this.register( function ( parser ) { + + return new GLTFMaterialsSheenExtension( parser ); + + } ); + + this.register( function ( parser ) { + + return new GLTFMaterialsTransmissionExtension( parser ); + + } ); + + this.register( function ( parser ) { + + return new GLTFMaterialsVolumeExtension( parser ); + + } ); + + this.register( function ( parser ) { + + return new GLTFMaterialsIorExtension( parser ); + + } ); + + this.register( function ( parser ) { + + return new GLTFMaterialsEmissiveStrengthExtension( parser ); + + } ); + + this.register( function ( parser ) { + + return new GLTFMaterialsSpecularExtension( parser ); + + } ); + + this.register( function ( parser ) { + + return new GLTFMaterialsIridescenceExtension( parser ); + + } ); + + this.register( function ( parser ) { + + return new GLTFMaterialsAnisotropyExtension( parser ); + + } ); + + this.register( function ( parser ) { + + return new GLTFMaterialsBumpExtension( parser ); + + } ); + + this.register( function ( parser ) { + + return new GLTFLightsExtension( parser ); + + } ); + + this.register( function ( parser ) { + + return new GLTFMeshoptCompression( parser ); + + } ); + + this.register( function ( parser ) { + + return new GLTFMeshGpuInstancing( parser ); + + } ); + + } + + load( url, onLoad, onProgress, onError ) { + + const scope = this; + + let resourcePath; + + if ( this.resourcePath !== '' ) { + + resourcePath = this.resourcePath; + + } else if ( this.path !== '' ) { + + // If a base path is set, resources will be relative paths from that plus the relative path of the gltf file + // Example path = 'https://my-cnd-server.com/', url = 'assets/models/model.gltf' + // resourcePath = 'https://my-cnd-server.com/assets/models/' + // referenced resource 'model.bin' will be loaded from 'https://my-cnd-server.com/assets/models/model.bin' + // referenced resource '../textures/texture.png' will be loaded from 'https://my-cnd-server.com/assets/textures/texture.png' + const relativeUrl = LoaderUtils.extractUrlBase( url ); + resourcePath = LoaderUtils.resolveURL( relativeUrl, this.path ); + + } else { + + resourcePath = LoaderUtils.extractUrlBase( url ); + + } + + // Tells the LoadingManager to track an extra item, which resolves after + // the model is fully loaded. This means the count of items loaded will + // be incorrect, but ensures manager.onLoad() does not fire early. + this.manager.itemStart( url ); + + const _onError = function ( e ) { + + if ( onError ) { + + onError( e ); + + } else { + + console.error( e ); + + } + + scope.manager.itemError( url ); + scope.manager.itemEnd( url ); + + }; + + const loader = new FileLoader( this.manager ); + + loader.setPath( this.path ); + loader.setResponseType( 'arraybuffer' ); + loader.setRequestHeader( this.requestHeader ); + loader.setWithCredentials( this.withCredentials ); + + loader.load( url, function ( data ) { + + try { + + scope.parse( data, resourcePath, function ( gltf ) { + + onLoad( gltf ); + + scope.manager.itemEnd( url ); + + }, _onError ); + + } catch ( e ) { + + _onError( e ); + + } + + }, onProgress, _onError ); + + } + + setDRACOLoader( dracoLoader ) { + + this.dracoLoader = dracoLoader; + return this; + + } + + setDDSLoader() { + + throw new Error( + + 'THREE.GLTFLoader: "MSFT_texture_dds" no longer supported. Please update to "KHR_texture_basisu".' + + ); + + } + + setKTX2Loader( ktx2Loader ) { + + this.ktx2Loader = ktx2Loader; + return this; + + } + + setMeshoptDecoder( meshoptDecoder ) { + + this.meshoptDecoder = meshoptDecoder; + return this; + + } + + register( callback ) { + + if ( this.pluginCallbacks.indexOf( callback ) === - 1 ) { + + this.pluginCallbacks.push( callback ); + + } + + return this; + + } + + unregister( callback ) { + + if ( this.pluginCallbacks.indexOf( callback ) !== - 1 ) { + + this.pluginCallbacks.splice( this.pluginCallbacks.indexOf( callback ), 1 ); + + } + + return this; + + } + + parse( data, path, onLoad, onError ) { + + let json; + const extensions = {}; + const plugins = {}; + const textDecoder = new TextDecoder(); + + if ( typeof data === 'string' ) { + + json = JSON.parse( data ); + + } else if ( data instanceof ArrayBuffer ) { + + const magic = textDecoder.decode( new Uint8Array( data, 0, 4 ) ); + + if ( magic === BINARY_EXTENSION_HEADER_MAGIC ) { + + try { + + extensions[ EXTENSIONS.KHR_BINARY_GLTF ] = new GLTFBinaryExtension( data ); + + } catch ( error ) { + + if ( onError ) onError( error ); + return; + + } + + json = JSON.parse( extensions[ EXTENSIONS.KHR_BINARY_GLTF ].content ); + + } else { + + json = JSON.parse( textDecoder.decode( data ) ); + + } + + } else { + + json = data; + + } + + if ( json.asset === undefined || json.asset.version[ 0 ] < 2 ) { + + if ( onError ) onError( new Error( 'THREE.GLTFLoader: Unsupported asset. glTF versions >=2.0 are supported.' ) ); + return; + + } + + const parser = new GLTFParser( json, { + + path: path || this.resourcePath || '', + crossOrigin: this.crossOrigin, + requestHeader: this.requestHeader, + manager: this.manager, + ktx2Loader: this.ktx2Loader, + meshoptDecoder: this.meshoptDecoder + + } ); + + parser.fileLoader.setRequestHeader( this.requestHeader ); + + for ( let i = 0; i < this.pluginCallbacks.length; i ++ ) { + + const plugin = this.pluginCallbacks[ i ]( parser ); + + if ( ! plugin.name ) console.error( 'THREE.GLTFLoader: Invalid plugin found: missing name' ); + + plugins[ plugin.name ] = plugin; + + // Workaround to avoid determining as unknown extension + // in addUnknownExtensionsToUserData(). + // Remove this workaround if we move all the existing + // extension handlers to plugin system + extensions[ plugin.name ] = true; + + } + + if ( json.extensionsUsed ) { + + for ( let i = 0; i < json.extensionsUsed.length; ++ i ) { + + const extensionName = json.extensionsUsed[ i ]; + const extensionsRequired = json.extensionsRequired || []; + + switch ( extensionName ) { + + case EXTENSIONS.KHR_MATERIALS_UNLIT: + extensions[ extensionName ] = new GLTFMaterialsUnlitExtension(); + break; + + case EXTENSIONS.KHR_DRACO_MESH_COMPRESSION: + extensions[ extensionName ] = new GLTFDracoMeshCompressionExtension( json, this.dracoLoader ); + break; + + case EXTENSIONS.KHR_TEXTURE_TRANSFORM: + extensions[ extensionName ] = new GLTFTextureTransformExtension(); + break; + + case EXTENSIONS.KHR_MESH_QUANTIZATION: + extensions[ extensionName ] = new GLTFMeshQuantizationExtension(); + break; + + default: + + if ( extensionsRequired.indexOf( extensionName ) >= 0 && plugins[ extensionName ] === undefined ) { + + console.warn( 'THREE.GLTFLoader: Unknown extension "' + extensionName + '".' ); + + } + + } + + } + + } + + parser.setExtensions( extensions ); + parser.setPlugins( plugins ); + parser.parse( onLoad, onError ); + + } + + parseAsync( data, path ) { + + const scope = this; + + return new Promise( function ( resolve, reject ) { + + scope.parse( data, path, resolve, reject ); + + } ); + + } + +} + +/* GLTFREGISTRY */ + +function GLTFRegistry() { + + let objects = {}; + + return { + + get: function ( key ) { + + return objects[ key ]; + + }, + + add: function ( key, object ) { + + objects[ key ] = object; + + }, + + remove: function ( key ) { + + delete objects[ key ]; + + }, + + removeAll: function () { + + objects = {}; + + } + + }; + +} + +/*********************************/ +/********** EXTENSIONS ***********/ +/*********************************/ + +const EXTENSIONS = { + KHR_BINARY_GLTF: 'KHR_binary_glTF', + KHR_DRACO_MESH_COMPRESSION: 'KHR_draco_mesh_compression', + KHR_LIGHTS_PUNCTUAL: 'KHR_lights_punctual', + KHR_MATERIALS_CLEARCOAT: 'KHR_materials_clearcoat', + KHR_MATERIALS_DISPERSION: 'KHR_materials_dispersion', + KHR_MATERIALS_IOR: 'KHR_materials_ior', + KHR_MATERIALS_SHEEN: 'KHR_materials_sheen', + KHR_MATERIALS_SPECULAR: 'KHR_materials_specular', + KHR_MATERIALS_TRANSMISSION: 'KHR_materials_transmission', + KHR_MATERIALS_IRIDESCENCE: 'KHR_materials_iridescence', + KHR_MATERIALS_ANISOTROPY: 'KHR_materials_anisotropy', + KHR_MATERIALS_UNLIT: 'KHR_materials_unlit', + KHR_MATERIALS_VOLUME: 'KHR_materials_volume', + KHR_TEXTURE_BASISU: 'KHR_texture_basisu', + KHR_TEXTURE_TRANSFORM: 'KHR_texture_transform', + KHR_MESH_QUANTIZATION: 'KHR_mesh_quantization', + KHR_MATERIALS_EMISSIVE_STRENGTH: 'KHR_materials_emissive_strength', + EXT_MATERIALS_BUMP: 'EXT_materials_bump', + EXT_TEXTURE_WEBP: 'EXT_texture_webp', + EXT_TEXTURE_AVIF: 'EXT_texture_avif', + EXT_MESHOPT_COMPRESSION: 'EXT_meshopt_compression', + EXT_MESH_GPU_INSTANCING: 'EXT_mesh_gpu_instancing' +}; + +/** + * Punctual Lights Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_lights_punctual + */ +class GLTFLightsExtension { + + constructor( parser ) { + + this.parser = parser; + this.name = EXTENSIONS.KHR_LIGHTS_PUNCTUAL; + + // Object3D instance caches + this.cache = { refs: {}, uses: {} }; + + } + + _markDefs() { + + const parser = this.parser; + const nodeDefs = this.parser.json.nodes || []; + + for ( let nodeIndex = 0, nodeLength = nodeDefs.length; nodeIndex < nodeLength; nodeIndex ++ ) { + + const nodeDef = nodeDefs[ nodeIndex ]; + + if ( nodeDef.extensions + && nodeDef.extensions[ this.name ] + && nodeDef.extensions[ this.name ].light !== undefined ) { + + parser._addNodeRef( this.cache, nodeDef.extensions[ this.name ].light ); + + } + + } + + } + + _loadLight( lightIndex ) { + + const parser = this.parser; + const cacheKey = 'light:' + lightIndex; + let dependency = parser.cache.get( cacheKey ); + + if ( dependency ) return dependency; + + const json = parser.json; + const extensions = ( json.extensions && json.extensions[ this.name ] ) || {}; + const lightDefs = extensions.lights || []; + const lightDef = lightDefs[ lightIndex ]; + let lightNode; + + const color = new Color( 0xffffff ); + + if ( lightDef.color !== undefined ) color.setRGB( lightDef.color[ 0 ], lightDef.color[ 1 ], lightDef.color[ 2 ], LinearSRGBColorSpace ); + + const range = lightDef.range !== undefined ? lightDef.range : 0; + + switch ( lightDef.type ) { + + case 'directional': + lightNode = new DirectionalLight( color ); + lightNode.target.position.set( 0, 0, - 1 ); + lightNode.add( lightNode.target ); + break; + + case 'point': + lightNode = new PointLight( color ); + lightNode.distance = range; + break; + + case 'spot': + lightNode = new SpotLight( color ); + lightNode.distance = range; + // Handle spotlight properties. + lightDef.spot = lightDef.spot || {}; + lightDef.spot.innerConeAngle = lightDef.spot.innerConeAngle !== undefined ? lightDef.spot.innerConeAngle : 0; + lightDef.spot.outerConeAngle = lightDef.spot.outerConeAngle !== undefined ? lightDef.spot.outerConeAngle : Math.PI / 4.0; + lightNode.angle = lightDef.spot.outerConeAngle; + lightNode.penumbra = 1.0 - lightDef.spot.innerConeAngle / lightDef.spot.outerConeAngle; + lightNode.target.position.set( 0, 0, - 1 ); + lightNode.add( lightNode.target ); + break; + + default: + throw new Error( 'THREE.GLTFLoader: Unexpected light type: ' + lightDef.type ); + + } + + // Some lights (e.g. spot) default to a position other than the origin. Reset the position + // here, because node-level parsing will only override position if explicitly specified. + lightNode.position.set( 0, 0, 0 ); + + lightNode.decay = 2; + + assignExtrasToUserData( lightNode, lightDef ); + + if ( lightDef.intensity !== undefined ) lightNode.intensity = lightDef.intensity; + + lightNode.name = parser.createUniqueName( lightDef.name || ( 'light_' + lightIndex ) ); + + dependency = Promise.resolve( lightNode ); + + parser.cache.add( cacheKey, dependency ); + + return dependency; + + } + + getDependency( type, index ) { + + if ( type !== 'light' ) return; + + return this._loadLight( index ); + + } + + createNodeAttachment( nodeIndex ) { + + const self = this; + const parser = this.parser; + const json = parser.json; + const nodeDef = json.nodes[ nodeIndex ]; + const lightDef = ( nodeDef.extensions && nodeDef.extensions[ this.name ] ) || {}; + const lightIndex = lightDef.light; + + if ( lightIndex === undefined ) return null; + + return this._loadLight( lightIndex ).then( function ( light ) { + + return parser._getNodeRef( self.cache, lightIndex, light ); + + } ); + + } + +} + +/** + * Unlit Materials Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_unlit + */ +class GLTFMaterialsUnlitExtension { + + constructor() { + + this.name = EXTENSIONS.KHR_MATERIALS_UNLIT; + + } + + getMaterialType() { + + return MeshBasicMaterial; + + } + + extendParams( materialParams, materialDef, parser ) { + + const pending = []; + + materialParams.color = new Color( 1.0, 1.0, 1.0 ); + materialParams.opacity = 1.0; + + const metallicRoughness = materialDef.pbrMetallicRoughness; + + if ( metallicRoughness ) { + + if ( Array.isArray( metallicRoughness.baseColorFactor ) ) { + + const array = metallicRoughness.baseColorFactor; + + materialParams.color.setRGB( array[ 0 ], array[ 1 ], array[ 2 ], LinearSRGBColorSpace ); + materialParams.opacity = array[ 3 ]; + + } + + if ( metallicRoughness.baseColorTexture !== undefined ) { + + pending.push( parser.assignTexture( materialParams, 'map', metallicRoughness.baseColorTexture, SRGBColorSpace ) ); + + } + + } + + return Promise.all( pending ); + + } + +} + +/** + * Materials Emissive Strength Extension + * + * Specification: https://github.com/KhronosGroup/glTF/blob/5768b3ce0ef32bc39cdf1bef10b948586635ead3/extensions/2.0/Khronos/KHR_materials_emissive_strength/README.md + */ +class GLTFMaterialsEmissiveStrengthExtension { + + constructor( parser ) { + + this.parser = parser; + this.name = EXTENSIONS.KHR_MATERIALS_EMISSIVE_STRENGTH; + + } + + extendMaterialParams( materialIndex, materialParams ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { + + return Promise.resolve(); + + } + + const emissiveStrength = materialDef.extensions[ this.name ].emissiveStrength; + + if ( emissiveStrength !== undefined ) { + + materialParams.emissiveIntensity = emissiveStrength; + + } + + return Promise.resolve(); + + } + +} + +/** + * Clearcoat Materials Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_clearcoat + */ +class GLTFMaterialsClearcoatExtension { + + constructor( parser ) { + + this.parser = parser; + this.name = EXTENSIONS.KHR_MATERIALS_CLEARCOAT; + + } + + getMaterialType( materialIndex ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) return null; + + return MeshPhysicalMaterial; + + } + + extendMaterialParams( materialIndex, materialParams ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { + + return Promise.resolve(); + + } + + const pending = []; + + const extension = materialDef.extensions[ this.name ]; + + if ( extension.clearcoatFactor !== undefined ) { + + materialParams.clearcoat = extension.clearcoatFactor; + + } + + if ( extension.clearcoatTexture !== undefined ) { + + pending.push( parser.assignTexture( materialParams, 'clearcoatMap', extension.clearcoatTexture ) ); + + } + + if ( extension.clearcoatRoughnessFactor !== undefined ) { + + materialParams.clearcoatRoughness = extension.clearcoatRoughnessFactor; + + } + + if ( extension.clearcoatRoughnessTexture !== undefined ) { + + pending.push( parser.assignTexture( materialParams, 'clearcoatRoughnessMap', extension.clearcoatRoughnessTexture ) ); + + } + + if ( extension.clearcoatNormalTexture !== undefined ) { + + pending.push( parser.assignTexture( materialParams, 'clearcoatNormalMap', extension.clearcoatNormalTexture ) ); + + if ( extension.clearcoatNormalTexture.scale !== undefined ) { + + const scale = extension.clearcoatNormalTexture.scale; + + materialParams.clearcoatNormalScale = new Vector2( scale, scale ); + + } + + } + + return Promise.all( pending ); + + } + +} + +/** + * Materials dispersion Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_dispersion + */ +class GLTFMaterialsDispersionExtension { + + constructor( parser ) { + + this.parser = parser; + this.name = EXTENSIONS.KHR_MATERIALS_DISPERSION; + + } + + getMaterialType( materialIndex ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) return null; + + return MeshPhysicalMaterial; + + } + + extendMaterialParams( materialIndex, materialParams ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { + + return Promise.resolve(); + + } + + const extension = materialDef.extensions[ this.name ]; + + materialParams.dispersion = extension.dispersion !== undefined ? extension.dispersion : 0; + + return Promise.resolve(); + + } + +} + +/** + * Iridescence Materials Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_iridescence + */ +class GLTFMaterialsIridescenceExtension { + + constructor( parser ) { + + this.parser = parser; + this.name = EXTENSIONS.KHR_MATERIALS_IRIDESCENCE; + + } + + getMaterialType( materialIndex ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) return null; + + return MeshPhysicalMaterial; + + } + + extendMaterialParams( materialIndex, materialParams ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { + + return Promise.resolve(); + + } + + const pending = []; + + const extension = materialDef.extensions[ this.name ]; + + if ( extension.iridescenceFactor !== undefined ) { + + materialParams.iridescence = extension.iridescenceFactor; + + } + + if ( extension.iridescenceTexture !== undefined ) { + + pending.push( parser.assignTexture( materialParams, 'iridescenceMap', extension.iridescenceTexture ) ); + + } + + if ( extension.iridescenceIor !== undefined ) { + + materialParams.iridescenceIOR = extension.iridescenceIor; + + } + + if ( materialParams.iridescenceThicknessRange === undefined ) { + + materialParams.iridescenceThicknessRange = [ 100, 400 ]; + + } + + if ( extension.iridescenceThicknessMinimum !== undefined ) { + + materialParams.iridescenceThicknessRange[ 0 ] = extension.iridescenceThicknessMinimum; + + } + + if ( extension.iridescenceThicknessMaximum !== undefined ) { + + materialParams.iridescenceThicknessRange[ 1 ] = extension.iridescenceThicknessMaximum; + + } + + if ( extension.iridescenceThicknessTexture !== undefined ) { + + pending.push( parser.assignTexture( materialParams, 'iridescenceThicknessMap', extension.iridescenceThicknessTexture ) ); + + } + + return Promise.all( pending ); + + } + +} + +/** + * Sheen Materials Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_sheen + */ +class GLTFMaterialsSheenExtension { + + constructor( parser ) { + + this.parser = parser; + this.name = EXTENSIONS.KHR_MATERIALS_SHEEN; + + } + + getMaterialType( materialIndex ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) return null; + + return MeshPhysicalMaterial; + + } + + extendMaterialParams( materialIndex, materialParams ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { + + return Promise.resolve(); + + } + + const pending = []; + + materialParams.sheenColor = new Color( 0, 0, 0 ); + materialParams.sheenRoughness = 0; + materialParams.sheen = 1; + + const extension = materialDef.extensions[ this.name ]; + + if ( extension.sheenColorFactor !== undefined ) { + + const colorFactor = extension.sheenColorFactor; + materialParams.sheenColor.setRGB( colorFactor[ 0 ], colorFactor[ 1 ], colorFactor[ 2 ], LinearSRGBColorSpace ); + + } + + if ( extension.sheenRoughnessFactor !== undefined ) { + + materialParams.sheenRoughness = extension.sheenRoughnessFactor; + + } + + if ( extension.sheenColorTexture !== undefined ) { + + pending.push( parser.assignTexture( materialParams, 'sheenColorMap', extension.sheenColorTexture, SRGBColorSpace ) ); + + } + + if ( extension.sheenRoughnessTexture !== undefined ) { + + pending.push( parser.assignTexture( materialParams, 'sheenRoughnessMap', extension.sheenRoughnessTexture ) ); + + } + + return Promise.all( pending ); + + } + +} + +/** + * Transmission Materials Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_transmission + * Draft: https://github.com/KhronosGroup/glTF/pull/1698 + */ +class GLTFMaterialsTransmissionExtension { + + constructor( parser ) { + + this.parser = parser; + this.name = EXTENSIONS.KHR_MATERIALS_TRANSMISSION; + + } + + getMaterialType( materialIndex ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) return null; + + return MeshPhysicalMaterial; + + } + + extendMaterialParams( materialIndex, materialParams ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { + + return Promise.resolve(); + + } + + const pending = []; + + const extension = materialDef.extensions[ this.name ]; + + if ( extension.transmissionFactor !== undefined ) { + + materialParams.transmission = extension.transmissionFactor; + + } + + if ( extension.transmissionTexture !== undefined ) { + + pending.push( parser.assignTexture( materialParams, 'transmissionMap', extension.transmissionTexture ) ); + + } + + return Promise.all( pending ); + + } + +} + +/** + * Materials Volume Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_volume + */ +class GLTFMaterialsVolumeExtension { + + constructor( parser ) { + + this.parser = parser; + this.name = EXTENSIONS.KHR_MATERIALS_VOLUME; + + } + + getMaterialType( materialIndex ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) return null; + + return MeshPhysicalMaterial; + + } + + extendMaterialParams( materialIndex, materialParams ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { + + return Promise.resolve(); + + } + + const pending = []; + + const extension = materialDef.extensions[ this.name ]; + + materialParams.thickness = extension.thicknessFactor !== undefined ? extension.thicknessFactor : 0; + + if ( extension.thicknessTexture !== undefined ) { + + pending.push( parser.assignTexture( materialParams, 'thicknessMap', extension.thicknessTexture ) ); + + } + + materialParams.attenuationDistance = extension.attenuationDistance || Infinity; + + const colorArray = extension.attenuationColor || [ 1, 1, 1 ]; + materialParams.attenuationColor = new Color().setRGB( colorArray[ 0 ], colorArray[ 1 ], colorArray[ 2 ], LinearSRGBColorSpace ); + + return Promise.all( pending ); + + } + +} + +/** + * Materials ior Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_ior + */ +class GLTFMaterialsIorExtension { + + constructor( parser ) { + + this.parser = parser; + this.name = EXTENSIONS.KHR_MATERIALS_IOR; + + } + + getMaterialType( materialIndex ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) return null; + + return MeshPhysicalMaterial; + + } + + extendMaterialParams( materialIndex, materialParams ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { + + return Promise.resolve(); + + } + + const extension = materialDef.extensions[ this.name ]; + + materialParams.ior = extension.ior !== undefined ? extension.ior : 1.5; + + return Promise.resolve(); + + } + +} + +/** + * Materials specular Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_specular + */ +class GLTFMaterialsSpecularExtension { + + constructor( parser ) { + + this.parser = parser; + this.name = EXTENSIONS.KHR_MATERIALS_SPECULAR; + + } + + getMaterialType( materialIndex ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) return null; + + return MeshPhysicalMaterial; + + } + + extendMaterialParams( materialIndex, materialParams ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { + + return Promise.resolve(); + + } + + const pending = []; + + const extension = materialDef.extensions[ this.name ]; + + materialParams.specularIntensity = extension.specularFactor !== undefined ? extension.specularFactor : 1.0; + + if ( extension.specularTexture !== undefined ) { + + pending.push( parser.assignTexture( materialParams, 'specularIntensityMap', extension.specularTexture ) ); + + } + + const colorArray = extension.specularColorFactor || [ 1, 1, 1 ]; + materialParams.specularColor = new Color().setRGB( colorArray[ 0 ], colorArray[ 1 ], colorArray[ 2 ], LinearSRGBColorSpace ); + + if ( extension.specularColorTexture !== undefined ) { + + pending.push( parser.assignTexture( materialParams, 'specularColorMap', extension.specularColorTexture, SRGBColorSpace ) ); + + } + + return Promise.all( pending ); + + } + +} + + +/** + * Materials bump Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/EXT_materials_bump + */ +class GLTFMaterialsBumpExtension { + + constructor( parser ) { + + this.parser = parser; + this.name = EXTENSIONS.EXT_MATERIALS_BUMP; + + } + + getMaterialType( materialIndex ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) return null; + + return MeshPhysicalMaterial; + + } + + extendMaterialParams( materialIndex, materialParams ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { + + return Promise.resolve(); + + } + + const pending = []; + + const extension = materialDef.extensions[ this.name ]; + + materialParams.bumpScale = extension.bumpFactor !== undefined ? extension.bumpFactor : 1.0; + + if ( extension.bumpTexture !== undefined ) { + + pending.push( parser.assignTexture( materialParams, 'bumpMap', extension.bumpTexture ) ); + + } + + return Promise.all( pending ); + + } + +} + +/** + * Materials anisotropy Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_anisotropy + */ +class GLTFMaterialsAnisotropyExtension { + + constructor( parser ) { + + this.parser = parser; + this.name = EXTENSIONS.KHR_MATERIALS_ANISOTROPY; + + } + + getMaterialType( materialIndex ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) return null; + + return MeshPhysicalMaterial; + + } + + extendMaterialParams( materialIndex, materialParams ) { + + const parser = this.parser; + const materialDef = parser.json.materials[ materialIndex ]; + + if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { + + return Promise.resolve(); + + } + + const pending = []; + + const extension = materialDef.extensions[ this.name ]; + + if ( extension.anisotropyStrength !== undefined ) { + + materialParams.anisotropy = extension.anisotropyStrength; + + } + + if ( extension.anisotropyRotation !== undefined ) { + + materialParams.anisotropyRotation = extension.anisotropyRotation; + + } + + if ( extension.anisotropyTexture !== undefined ) { + + pending.push( parser.assignTexture( materialParams, 'anisotropyMap', extension.anisotropyTexture ) ); + + } + + return Promise.all( pending ); + + } + +} + +/** + * BasisU Texture Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_texture_basisu + */ +class GLTFTextureBasisUExtension { + + constructor( parser ) { + + this.parser = parser; + this.name = EXTENSIONS.KHR_TEXTURE_BASISU; + + } + + loadTexture( textureIndex ) { + + const parser = this.parser; + const json = parser.json; + + const textureDef = json.textures[ textureIndex ]; + + if ( ! textureDef.extensions || ! textureDef.extensions[ this.name ] ) { + + return null; + + } + + const extension = textureDef.extensions[ this.name ]; + const loader = parser.options.ktx2Loader; + + if ( ! loader ) { + + if ( json.extensionsRequired && json.extensionsRequired.indexOf( this.name ) >= 0 ) { + + throw new Error( 'THREE.GLTFLoader: setKTX2Loader must be called before loading KTX2 textures' ); + + } else { + + // Assumes that the extension is optional and that a fallback texture is present + return null; + + } + + } + + return parser.loadTextureImage( textureIndex, extension.source, loader ); + + } + +} + +/** + * WebP Texture Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/EXT_texture_webp + */ +class GLTFTextureWebPExtension { + + constructor( parser ) { + + this.parser = parser; + this.name = EXTENSIONS.EXT_TEXTURE_WEBP; + this.isSupported = null; + + } + + loadTexture( textureIndex ) { + + const name = this.name; + const parser = this.parser; + const json = parser.json; + + const textureDef = json.textures[ textureIndex ]; + + if ( ! textureDef.extensions || ! textureDef.extensions[ name ] ) { + + return null; + + } + + const extension = textureDef.extensions[ name ]; + const source = json.images[ extension.source ]; + + let loader = parser.textureLoader; + if ( source.uri ) { + + const handler = parser.options.manager.getHandler( source.uri ); + if ( handler !== null ) loader = handler; + + } + + return this.detectSupport().then( function ( isSupported ) { + + if ( isSupported ) return parser.loadTextureImage( textureIndex, extension.source, loader ); + + if ( json.extensionsRequired && json.extensionsRequired.indexOf( name ) >= 0 ) { + + throw new Error( 'THREE.GLTFLoader: WebP required by asset but unsupported.' ); + + } + + // Fall back to PNG or JPEG. + return parser.loadTexture( textureIndex ); + + } ); + + } + + detectSupport() { + + if ( ! this.isSupported ) { + + this.isSupported = new Promise( function ( resolve ) { + + const image = new Image(); + + // Lossy test image. Support for lossy images doesn't guarantee support for all + // WebP images, unfortunately. + image.src = 'data:image/webp;base64,UklGRiIAAABXRUJQVlA4IBYAAAAwAQCdASoBAAEADsD+JaQAA3AAAAAA'; + + image.onload = image.onerror = function () { + + resolve( image.height === 1 ); + + }; + + } ); + + } + + return this.isSupported; + + } + +} + +/** + * AVIF Texture Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/EXT_texture_avif + */ +class GLTFTextureAVIFExtension { + + constructor( parser ) { + + this.parser = parser; + this.name = EXTENSIONS.EXT_TEXTURE_AVIF; + this.isSupported = null; + + } + + loadTexture( textureIndex ) { + + const name = this.name; + const parser = this.parser; + const json = parser.json; + + const textureDef = json.textures[ textureIndex ]; + + if ( ! textureDef.extensions || ! textureDef.extensions[ name ] ) { + + return null; + + } + + const extension = textureDef.extensions[ name ]; + const source = json.images[ extension.source ]; + + let loader = parser.textureLoader; + if ( source.uri ) { + + const handler = parser.options.manager.getHandler( source.uri ); + if ( handler !== null ) loader = handler; + + } + + return this.detectSupport().then( function ( isSupported ) { + + if ( isSupported ) return parser.loadTextureImage( textureIndex, extension.source, loader ); + + if ( json.extensionsRequired && json.extensionsRequired.indexOf( name ) >= 0 ) { + + throw new Error( 'THREE.GLTFLoader: AVIF required by asset but unsupported.' ); + + } + + // Fall back to PNG or JPEG. + return parser.loadTexture( textureIndex ); + + } ); + + } + + detectSupport() { + + if ( ! this.isSupported ) { + + this.isSupported = new Promise( function ( resolve ) { + + const image = new Image(); + + // Lossy test image. + image.src = 'data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAABcAAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAEAAAABAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQAMAAAAABNjb2xybmNseAACAAIABoAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAAB9tZGF0EgAKCBgABogQEDQgMgkQAAAAB8dSLfI='; + image.onload = image.onerror = function () { + + resolve( image.height === 1 ); + + }; + + } ); + + } + + return this.isSupported; + + } + +} + +/** + * meshopt BufferView Compression Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/EXT_meshopt_compression + */ +class GLTFMeshoptCompression { + + constructor( parser ) { + + this.name = EXTENSIONS.EXT_MESHOPT_COMPRESSION; + this.parser = parser; + + } + + loadBufferView( index ) { + + const json = this.parser.json; + const bufferView = json.bufferViews[ index ]; + + if ( bufferView.extensions && bufferView.extensions[ this.name ] ) { + + const extensionDef = bufferView.extensions[ this.name ]; + + const buffer = this.parser.getDependency( 'buffer', extensionDef.buffer ); + const decoder = this.parser.options.meshoptDecoder; + + if ( ! decoder || ! decoder.supported ) { + + if ( json.extensionsRequired && json.extensionsRequired.indexOf( this.name ) >= 0 ) { + + throw new Error( 'THREE.GLTFLoader: setMeshoptDecoder must be called before loading compressed files' ); + + } else { + + // Assumes that the extension is optional and that fallback buffer data is present + return null; + + } + + } + + return buffer.then( function ( res ) { + + const byteOffset = extensionDef.byteOffset || 0; + const byteLength = extensionDef.byteLength || 0; + + const count = extensionDef.count; + const stride = extensionDef.byteStride; + + const source = new Uint8Array( res, byteOffset, byteLength ); + + if ( decoder.decodeGltfBufferAsync ) { + + return decoder.decodeGltfBufferAsync( count, stride, source, extensionDef.mode, extensionDef.filter ).then( function ( res ) { + + return res.buffer; + + } ); + + } else { + + // Support for MeshoptDecoder 0.18 or earlier, without decodeGltfBufferAsync + return decoder.ready.then( function () { + + const result = new ArrayBuffer( count * stride ); + decoder.decodeGltfBuffer( new Uint8Array( result ), count, stride, source, extensionDef.mode, extensionDef.filter ); + return result; + + } ); + + } + + } ); + + } else { + + return null; + + } + + } + +} + +/** + * GPU Instancing Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/EXT_mesh_gpu_instancing + * + */ +class GLTFMeshGpuInstancing { + + constructor( parser ) { + + this.name = EXTENSIONS.EXT_MESH_GPU_INSTANCING; + this.parser = parser; + + } + + createNodeMesh( nodeIndex ) { + + const json = this.parser.json; + const nodeDef = json.nodes[ nodeIndex ]; + + if ( ! nodeDef.extensions || ! nodeDef.extensions[ this.name ] || + nodeDef.mesh === undefined ) { + + return null; + + } + + const meshDef = json.meshes[ nodeDef.mesh ]; + + // No Points or Lines + Instancing support yet + + for ( const primitive of meshDef.primitives ) { + + if ( primitive.mode !== WEBGL_CONSTANTS.TRIANGLES && + primitive.mode !== WEBGL_CONSTANTS.TRIANGLE_STRIP && + primitive.mode !== WEBGL_CONSTANTS.TRIANGLE_FAN && + primitive.mode !== undefined ) { + + return null; + + } + + } + + const extensionDef = nodeDef.extensions[ this.name ]; + const attributesDef = extensionDef.attributes; + + // @TODO: Can we support InstancedMesh + SkinnedMesh? + + const pending = []; + const attributes = {}; + + for ( const key in attributesDef ) { + + pending.push( this.parser.getDependency( 'accessor', attributesDef[ key ] ).then( accessor => { + + attributes[ key ] = accessor; + return attributes[ key ]; + + } ) ); + + } + + if ( pending.length < 1 ) { + + return null; + + } + + pending.push( this.parser.createNodeMesh( nodeIndex ) ); + + return Promise.all( pending ).then( results => { + + const nodeObject = results.pop(); + const meshes = nodeObject.isGroup ? nodeObject.children : [ nodeObject ]; + const count = results[ 0 ].count; // All attribute counts should be same + const instancedMeshes = []; + + for ( const mesh of meshes ) { + + // Temporal variables + const m = new Matrix4(); + const p = new Vector3(); + const q = new Quaternion(); + const s = new Vector3( 1, 1, 1 ); + + const instancedMesh = new InstancedMesh( mesh.geometry, mesh.material, count ); + + for ( let i = 0; i < count; i ++ ) { + + if ( attributes.TRANSLATION ) { + + p.fromBufferAttribute( attributes.TRANSLATION, i ); + + } + + if ( attributes.ROTATION ) { + + q.fromBufferAttribute( attributes.ROTATION, i ); + + } + + if ( attributes.SCALE ) { + + s.fromBufferAttribute( attributes.SCALE, i ); + + } + + instancedMesh.setMatrixAt( i, m.compose( p, q, s ) ); + + } + + // Add instance attributes to the geometry, excluding TRS. + for ( const attributeName in attributes ) { + + if ( attributeName === '_COLOR_0' ) { + + const attr = attributes[ attributeName ]; + instancedMesh.instanceColor = new InstancedBufferAttribute( attr.array, attr.itemSize, attr.normalized ); + + } else if ( attributeName !== 'TRANSLATION' && + attributeName !== 'ROTATION' && + attributeName !== 'SCALE' ) { + + mesh.geometry.setAttribute( attributeName, attributes[ attributeName ] ); + + } + + } + + // Just in case + Object3D.prototype.copy.call( instancedMesh, mesh ); + + this.parser.assignFinalMaterial( instancedMesh ); + + instancedMeshes.push( instancedMesh ); + + } + + if ( nodeObject.isGroup ) { + + nodeObject.clear(); + + nodeObject.add( ... instancedMeshes ); + + return nodeObject; + + } + + return instancedMeshes[ 0 ]; + + } ); + + } + +} + +/* BINARY EXTENSION */ +const BINARY_EXTENSION_HEADER_MAGIC = 'glTF'; +const BINARY_EXTENSION_HEADER_LENGTH = 12; +const BINARY_EXTENSION_CHUNK_TYPES = { JSON: 0x4E4F534A, BIN: 0x004E4942 }; + +class GLTFBinaryExtension { + + constructor( data ) { + + this.name = EXTENSIONS.KHR_BINARY_GLTF; + this.content = null; + this.body = null; + + const headerView = new DataView( data, 0, BINARY_EXTENSION_HEADER_LENGTH ); + const textDecoder = new TextDecoder(); + + this.header = { + magic: textDecoder.decode( new Uint8Array( data.slice( 0, 4 ) ) ), + version: headerView.getUint32( 4, true ), + length: headerView.getUint32( 8, true ) + }; + + if ( this.header.magic !== BINARY_EXTENSION_HEADER_MAGIC ) { + + throw new Error( 'THREE.GLTFLoader: Unsupported glTF-Binary header.' ); + + } else if ( this.header.version < 2.0 ) { + + throw new Error( 'THREE.GLTFLoader: Legacy binary file detected.' ); + + } + + const chunkContentsLength = this.header.length - BINARY_EXTENSION_HEADER_LENGTH; + const chunkView = new DataView( data, BINARY_EXTENSION_HEADER_LENGTH ); + let chunkIndex = 0; + + while ( chunkIndex < chunkContentsLength ) { + + const chunkLength = chunkView.getUint32( chunkIndex, true ); + chunkIndex += 4; + + const chunkType = chunkView.getUint32( chunkIndex, true ); + chunkIndex += 4; + + if ( chunkType === BINARY_EXTENSION_CHUNK_TYPES.JSON ) { + + const contentArray = new Uint8Array( data, BINARY_EXTENSION_HEADER_LENGTH + chunkIndex, chunkLength ); + this.content = textDecoder.decode( contentArray ); + + } else if ( chunkType === BINARY_EXTENSION_CHUNK_TYPES.BIN ) { + + const byteOffset = BINARY_EXTENSION_HEADER_LENGTH + chunkIndex; + this.body = data.slice( byteOffset, byteOffset + chunkLength ); + + } + + // Clients must ignore chunks with unknown types. + + chunkIndex += chunkLength; + + } + + if ( this.content === null ) { + + throw new Error( 'THREE.GLTFLoader: JSON content not found.' ); + + } + + } + +} + +/** + * DRACO Mesh Compression Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_draco_mesh_compression + */ +class GLTFDracoMeshCompressionExtension { + + constructor( json, dracoLoader ) { + + if ( ! dracoLoader ) { + + throw new Error( 'THREE.GLTFLoader: No DRACOLoader instance provided.' ); + + } + + this.name = EXTENSIONS.KHR_DRACO_MESH_COMPRESSION; + this.json = json; + this.dracoLoader = dracoLoader; + this.dracoLoader.preload(); + + } + + decodePrimitive( primitive, parser ) { + + const json = this.json; + const dracoLoader = this.dracoLoader; + const bufferViewIndex = primitive.extensions[ this.name ].bufferView; + const gltfAttributeMap = primitive.extensions[ this.name ].attributes; + const threeAttributeMap = {}; + const attributeNormalizedMap = {}; + const attributeTypeMap = {}; + + for ( const attributeName in gltfAttributeMap ) { + + const threeAttributeName = ATTRIBUTES[ attributeName ] || attributeName.toLowerCase(); + + threeAttributeMap[ threeAttributeName ] = gltfAttributeMap[ attributeName ]; + + } + + for ( const attributeName in primitive.attributes ) { + + const threeAttributeName = ATTRIBUTES[ attributeName ] || attributeName.toLowerCase(); + + if ( gltfAttributeMap[ attributeName ] !== undefined ) { + + const accessorDef = json.accessors[ primitive.attributes[ attributeName ] ]; + const componentType = WEBGL_COMPONENT_TYPES[ accessorDef.componentType ]; + + attributeTypeMap[ threeAttributeName ] = componentType.name; + attributeNormalizedMap[ threeAttributeName ] = accessorDef.normalized === true; + + } + + } + + return parser.getDependency( 'bufferView', bufferViewIndex ).then( function ( bufferView ) { + + return new Promise( function ( resolve, reject ) { + + dracoLoader.decodeDracoFile( bufferView, function ( geometry ) { + + for ( const attributeName in geometry.attributes ) { + + const attribute = geometry.attributes[ attributeName ]; + const normalized = attributeNormalizedMap[ attributeName ]; + + if ( normalized !== undefined ) attribute.normalized = normalized; + + } + + resolve( geometry ); + + }, threeAttributeMap, attributeTypeMap, LinearSRGBColorSpace, reject ); + + } ); + + } ); + + } + +} + +/** + * Texture Transform Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_texture_transform + */ +class GLTFTextureTransformExtension { + + constructor() { + + this.name = EXTENSIONS.KHR_TEXTURE_TRANSFORM; + + } + + extendTexture( texture, transform ) { + + if ( ( transform.texCoord === undefined || transform.texCoord === texture.channel ) + && transform.offset === undefined + && transform.rotation === undefined + && transform.scale === undefined ) { + + // See https://github.com/mrdoob/three.js/issues/21819. + return texture; + + } + + texture = texture.clone(); + + if ( transform.texCoord !== undefined ) { + + texture.channel = transform.texCoord; + + } + + if ( transform.offset !== undefined ) { + + texture.offset.fromArray( transform.offset ); + + } + + if ( transform.rotation !== undefined ) { + + texture.rotation = transform.rotation; + + } + + if ( transform.scale !== undefined ) { + + texture.repeat.fromArray( transform.scale ); + + } + + texture.needsUpdate = true; + + return texture; + + } + +} + +/** + * Mesh Quantization Extension + * + * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_mesh_quantization + */ +class GLTFMeshQuantizationExtension { + + constructor() { + + this.name = EXTENSIONS.KHR_MESH_QUANTIZATION; + + } + +} + +/*********************************/ +/********** INTERPOLATION ********/ +/*********************************/ + +// Spline Interpolation +// Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#appendix-c-spline-interpolation +class GLTFCubicSplineInterpolant extends Interpolant { + + constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) { + + super( parameterPositions, sampleValues, sampleSize, resultBuffer ); + + } + + copySampleValue_( index ) { + + // Copies a sample value to the result buffer. See description of glTF + // CUBICSPLINE values layout in interpolate_() function below. + + const result = this.resultBuffer, + values = this.sampleValues, + valueSize = this.valueSize, + offset = index * valueSize * 3 + valueSize; + + for ( let i = 0; i !== valueSize; i ++ ) { + + result[ i ] = values[ offset + i ]; + + } + + return result; + + } + + interpolate_( i1, t0, t, t1 ) { + + const result = this.resultBuffer; + const values = this.sampleValues; + const stride = this.valueSize; + + const stride2 = stride * 2; + const stride3 = stride * 3; + + const td = t1 - t0; + + const p = ( t - t0 ) / td; + const pp = p * p; + const ppp = pp * p; + + const offset1 = i1 * stride3; + const offset0 = offset1 - stride3; + + const s2 = - 2 * ppp + 3 * pp; + const s3 = ppp - pp; + const s0 = 1 - s2; + const s1 = s3 - pp + p; + + // Layout of keyframe output values for CUBICSPLINE animations: + // [ inTangent_1, splineVertex_1, outTangent_1, inTangent_2, splineVertex_2, ... ] + for ( let i = 0; i !== stride; i ++ ) { + + const p0 = values[ offset0 + i + stride ]; // splineVertex_k + const m0 = values[ offset0 + i + stride2 ] * td; // outTangent_k * (t_k+1 - t_k) + const p1 = values[ offset1 + i + stride ]; // splineVertex_k+1 + const m1 = values[ offset1 + i ] * td; // inTangent_k+1 * (t_k+1 - t_k) + + result[ i ] = s0 * p0 + s1 * m0 + s2 * p1 + s3 * m1; + + } + + return result; + + } + +} + +const _q = new Quaternion(); + +class GLTFCubicSplineQuaternionInterpolant extends GLTFCubicSplineInterpolant { + + interpolate_( i1, t0, t, t1 ) { + + const result = super.interpolate_( i1, t0, t, t1 ); + + _q.fromArray( result ).normalize().toArray( result ); + + return result; + + } + +} + + +/*********************************/ +/********** INTERNALS ************/ +/*********************************/ + +/* CONSTANTS */ + +const WEBGL_CONSTANTS = { + FLOAT: 5126, + //FLOAT_MAT2: 35674, + FLOAT_MAT3: 35675, + FLOAT_MAT4: 35676, + FLOAT_VEC2: 35664, + FLOAT_VEC3: 35665, + FLOAT_VEC4: 35666, + LINEAR: 9729, + REPEAT: 10497, + SAMPLER_2D: 35678, + POINTS: 0, + LINES: 1, + LINE_LOOP: 2, + LINE_STRIP: 3, + TRIANGLES: 4, + TRIANGLE_STRIP: 5, + TRIANGLE_FAN: 6, + UNSIGNED_BYTE: 5121, + UNSIGNED_SHORT: 5123 +}; + +const WEBGL_COMPONENT_TYPES = { + 5120: Int8Array, + 5121: Uint8Array, + 5122: Int16Array, + 5123: Uint16Array, + 5125: Uint32Array, + 5126: Float32Array +}; + +const WEBGL_FILTERS = { + 9728: NearestFilter, + 9729: LinearFilter, + 9984: NearestMipmapNearestFilter, + 9985: LinearMipmapNearestFilter, + 9986: NearestMipmapLinearFilter, + 9987: LinearMipmapLinearFilter +}; + +const WEBGL_WRAPPINGS = { + 33071: ClampToEdgeWrapping, + 33648: MirroredRepeatWrapping, + 10497: RepeatWrapping +}; + +const WEBGL_TYPE_SIZES = { + 'SCALAR': 1, + 'VEC2': 2, + 'VEC3': 3, + 'VEC4': 4, + 'MAT2': 4, + 'MAT3': 9, + 'MAT4': 16 +}; + +const ATTRIBUTES = { + POSITION: 'position', + NORMAL: 'normal', + TANGENT: 'tangent', + TEXCOORD_0: 'uv', + TEXCOORD_1: 'uv1', + TEXCOORD_2: 'uv2', + TEXCOORD_3: 'uv3', + COLOR_0: 'color', + WEIGHTS_0: 'skinWeight', + JOINTS_0: 'skinIndex', +}; + +const PATH_PROPERTIES = { + scale: 'scale', + translation: 'position', + rotation: 'quaternion', + weights: 'morphTargetInfluences' +}; + +const INTERPOLATION = { + CUBICSPLINE: undefined, // We use a custom interpolant (GLTFCubicSplineInterpolation) for CUBICSPLINE tracks. Each + // keyframe track will be initialized with a default interpolation type, then modified. + LINEAR: InterpolateLinear, + STEP: InterpolateDiscrete +}; + +const ALPHA_MODES = { + OPAQUE: 'OPAQUE', + MASK: 'MASK', + BLEND: 'BLEND' +}; + +/** + * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#default-material + */ +function createDefaultMaterial( cache ) { + + if ( cache[ 'DefaultMaterial' ] === undefined ) { + + cache[ 'DefaultMaterial' ] = new MeshStandardMaterial( { + color: 0xFFFFFF, + emissive: 0x000000, + metalness: 1, + roughness: 1, + transparent: false, + depthTest: true, + side: FrontSide + } ); + + } + + return cache[ 'DefaultMaterial' ]; + +} + +function addUnknownExtensionsToUserData( knownExtensions, object, objectDef ) { + + // Add unknown glTF extensions to an object's userData. + + for ( const name in objectDef.extensions ) { + + if ( knownExtensions[ name ] === undefined ) { + + object.userData.gltfExtensions = object.userData.gltfExtensions || {}; + object.userData.gltfExtensions[ name ] = objectDef.extensions[ name ]; + + } + + } + +} + +/** + * @param {Object3D|Material|BufferGeometry} object + * @param {GLTF.definition} gltfDef + */ +function assignExtrasToUserData( object, gltfDef ) { + + if ( gltfDef.extras !== undefined ) { + + if ( typeof gltfDef.extras === 'object' ) { + + Object.assign( object.userData, gltfDef.extras ); + + } else { + + console.warn( 'THREE.GLTFLoader: Ignoring primitive type .extras, ' + gltfDef.extras ); + + } + + } + +} + +/** + * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#morph-targets + * + * @param {BufferGeometry} geometry + * @param {Array} targets + * @param {GLTFParser} parser + * @return {Promise} + */ +function addMorphTargets( geometry, targets, parser ) { + + let hasMorphPosition = false; + let hasMorphNormal = false; + let hasMorphColor = false; + + for ( let i = 0, il = targets.length; i < il; i ++ ) { + + const target = targets[ i ]; + + if ( target.POSITION !== undefined ) hasMorphPosition = true; + if ( target.NORMAL !== undefined ) hasMorphNormal = true; + if ( target.COLOR_0 !== undefined ) hasMorphColor = true; + + if ( hasMorphPosition && hasMorphNormal && hasMorphColor ) break; + + } + + if ( ! hasMorphPosition && ! hasMorphNormal && ! hasMorphColor ) return Promise.resolve( geometry ); + + const pendingPositionAccessors = []; + const pendingNormalAccessors = []; + const pendingColorAccessors = []; + + for ( let i = 0, il = targets.length; i < il; i ++ ) { + + const target = targets[ i ]; + + if ( hasMorphPosition ) { + + const pendingAccessor = target.POSITION !== undefined + ? parser.getDependency( 'accessor', target.POSITION ) + : geometry.attributes.position; + + pendingPositionAccessors.push( pendingAccessor ); + + } + + if ( hasMorphNormal ) { + + const pendingAccessor = target.NORMAL !== undefined + ? parser.getDependency( 'accessor', target.NORMAL ) + : geometry.attributes.normal; + + pendingNormalAccessors.push( pendingAccessor ); + + } + + if ( hasMorphColor ) { + + const pendingAccessor = target.COLOR_0 !== undefined + ? parser.getDependency( 'accessor', target.COLOR_0 ) + : geometry.attributes.color; + + pendingColorAccessors.push( pendingAccessor ); + + } + + } + + return Promise.all( [ + Promise.all( pendingPositionAccessors ), + Promise.all( pendingNormalAccessors ), + Promise.all( pendingColorAccessors ) + ] ).then( function ( accessors ) { + + const morphPositions = accessors[ 0 ]; + const morphNormals = accessors[ 1 ]; + const morphColors = accessors[ 2 ]; + + if ( hasMorphPosition ) geometry.morphAttributes.position = morphPositions; + if ( hasMorphNormal ) geometry.morphAttributes.normal = morphNormals; + if ( hasMorphColor ) geometry.morphAttributes.color = morphColors; + geometry.morphTargetsRelative = true; + + return geometry; + + } ); + +} + +/** + * @param {Mesh} mesh + * @param {GLTF.Mesh} meshDef + */ +function updateMorphTargets( mesh, meshDef ) { + + mesh.updateMorphTargets(); + + if ( meshDef.weights !== undefined ) { + + for ( let i = 0, il = meshDef.weights.length; i < il; i ++ ) { + + mesh.morphTargetInfluences[ i ] = meshDef.weights[ i ]; + + } + + } + + // .extras has user-defined data, so check that .extras.targetNames is an array. + if ( meshDef.extras && Array.isArray( meshDef.extras.targetNames ) ) { + + const targetNames = meshDef.extras.targetNames; + + if ( mesh.morphTargetInfluences.length === targetNames.length ) { + + mesh.morphTargetDictionary = {}; + + for ( let i = 0, il = targetNames.length; i < il; i ++ ) { + + mesh.morphTargetDictionary[ targetNames[ i ] ] = i; + + } + + } else { + + console.warn( 'THREE.GLTFLoader: Invalid extras.targetNames length. Ignoring names.' ); + + } + + } + +} + +function createPrimitiveKey( primitiveDef ) { + + let geometryKey; + + const dracoExtension = primitiveDef.extensions && primitiveDef.extensions[ EXTENSIONS.KHR_DRACO_MESH_COMPRESSION ]; + + if ( dracoExtension ) { + + geometryKey = 'draco:' + dracoExtension.bufferView + + ':' + dracoExtension.indices + + ':' + createAttributesKey( dracoExtension.attributes ); + + } else { + + geometryKey = primitiveDef.indices + ':' + createAttributesKey( primitiveDef.attributes ) + ':' + primitiveDef.mode; + + } + + if ( primitiveDef.targets !== undefined ) { + + for ( let i = 0, il = primitiveDef.targets.length; i < il; i ++ ) { + + geometryKey += ':' + createAttributesKey( primitiveDef.targets[ i ] ); + + } + + } + + return geometryKey; + +} + +function createAttributesKey( attributes ) { + + let attributesKey = ''; + + const keys = Object.keys( attributes ).sort(); + + for ( let i = 0, il = keys.length; i < il; i ++ ) { + + attributesKey += keys[ i ] + ':' + attributes[ keys[ i ] ] + ';'; + + } + + return attributesKey; + +} + +function getNormalizedComponentScale( constructor ) { + + // Reference: + // https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_mesh_quantization#encoding-quantized-data + + switch ( constructor ) { + + case Int8Array: + return 1 / 127; + + case Uint8Array: + return 1 / 255; + + case Int16Array: + return 1 / 32767; + + case Uint16Array: + return 1 / 65535; + + default: + throw new Error( 'THREE.GLTFLoader: Unsupported normalized accessor component type.' ); + + } + +} + +function getImageURIMimeType( uri ) { + + if ( uri.search( /\.jpe?g($|\?)/i ) > 0 || uri.search( /^data\:image\/jpeg/ ) === 0 ) return 'image/jpeg'; + if ( uri.search( /\.webp($|\?)/i ) > 0 || uri.search( /^data\:image\/webp/ ) === 0 ) return 'image/webp'; + + return 'image/png'; + +} + +const _identityMatrix = new Matrix4(); + +/* GLTF PARSER */ + +class GLTFParser { + + constructor( json = {}, options = {} ) { + + this.json = json; + this.extensions = {}; + this.plugins = {}; + this.options = options; + + // loader object cache + this.cache = new GLTFRegistry(); + + // associations between Three.js objects and glTF elements + this.associations = new Map(); + + // BufferGeometry caching + this.primitiveCache = {}; + + // Node cache + this.nodeCache = {}; + + // Object3D instance caches + this.meshCache = { refs: {}, uses: {} }; + this.cameraCache = { refs: {}, uses: {} }; + this.lightCache = { refs: {}, uses: {} }; + + this.sourceCache = {}; + this.textureCache = {}; + + // Track node names, to ensure no duplicates + this.nodeNamesUsed = {}; + + // Use an ImageBitmapLoader if imageBitmaps are supported. Moves much of the + // expensive work of uploading a texture to the GPU off the main thread. + + let isSafari = false; + let isFirefox = false; + let firefoxVersion = - 1; + + if ( typeof navigator !== 'undefined' ) { + + isSafari = /^((?!chrome|android).)*safari/i.test( navigator.userAgent ) === true; + isFirefox = navigator.userAgent.indexOf( 'Firefox' ) > - 1; + firefoxVersion = isFirefox ? navigator.userAgent.match( /Firefox\/([0-9]+)\./ )[ 1 ] : - 1; + + } + + if ( typeof createImageBitmap === 'undefined' || isSafari || ( isFirefox && firefoxVersion < 98 ) ) { + + this.textureLoader = new TextureLoader( this.options.manager ); + + } else { + + this.textureLoader = new ImageBitmapLoader( this.options.manager ); + + } + + this.textureLoader.setCrossOrigin( this.options.crossOrigin ); + this.textureLoader.setRequestHeader( this.options.requestHeader ); + + this.fileLoader = new FileLoader( this.options.manager ); + this.fileLoader.setResponseType( 'arraybuffer' ); + + if ( this.options.crossOrigin === 'use-credentials' ) { + + this.fileLoader.setWithCredentials( true ); + + } + + } + + setExtensions( extensions ) { + + this.extensions = extensions; + + } + + setPlugins( plugins ) { + + this.plugins = plugins; + + } + + parse( onLoad, onError ) { + + const parser = this; + const json = this.json; + const extensions = this.extensions; + + // Clear the loader cache + this.cache.removeAll(); + this.nodeCache = {}; + + // Mark the special nodes/meshes in json for efficient parse + this._invokeAll( function ( ext ) { + + return ext._markDefs && ext._markDefs(); + + } ); + + Promise.all( this._invokeAll( function ( ext ) { + + return ext.beforeRoot && ext.beforeRoot(); + + } ) ).then( function () { + + return Promise.all( [ + + parser.getDependencies( 'scene' ), + parser.getDependencies( 'animation' ), + parser.getDependencies( 'camera' ), + + ] ); + + } ).then( function ( dependencies ) { + + const result = { + scene: dependencies[ 0 ][ json.scene || 0 ], + scenes: dependencies[ 0 ], + animations: dependencies[ 1 ], + cameras: dependencies[ 2 ], + asset: json.asset, + parser: parser, + userData: {} + }; + + addUnknownExtensionsToUserData( extensions, result, json ); + + assignExtrasToUserData( result, json ); + + return Promise.all( parser._invokeAll( function ( ext ) { + + return ext.afterRoot && ext.afterRoot( result ); + + } ) ).then( function () { + + for ( const scene of result.scenes ) { + + scene.updateMatrixWorld(); + + } + + onLoad( result ); + + } ); + + } ).catch( onError ); + + } + + /** + * Marks the special nodes/meshes in json for efficient parse. + */ + _markDefs() { + + const nodeDefs = this.json.nodes || []; + const skinDefs = this.json.skins || []; + const meshDefs = this.json.meshes || []; + + // Nothing in the node definition indicates whether it is a Bone or an + // Object3D. Use the skins' joint references to mark bones. + for ( let skinIndex = 0, skinLength = skinDefs.length; skinIndex < skinLength; skinIndex ++ ) { + + const joints = skinDefs[ skinIndex ].joints; + + for ( let i = 0, il = joints.length; i < il; i ++ ) { + + nodeDefs[ joints[ i ] ].isBone = true; + + } + + } + + // Iterate over all nodes, marking references to shared resources, + // as well as skeleton joints. + for ( let nodeIndex = 0, nodeLength = nodeDefs.length; nodeIndex < nodeLength; nodeIndex ++ ) { + + const nodeDef = nodeDefs[ nodeIndex ]; + + if ( nodeDef.mesh !== undefined ) { + + this._addNodeRef( this.meshCache, nodeDef.mesh ); + + // Nothing in the mesh definition indicates whether it is + // a SkinnedMesh or Mesh. Use the node's mesh reference + // to mark SkinnedMesh if node has skin. + if ( nodeDef.skin !== undefined ) { + + meshDefs[ nodeDef.mesh ].isSkinnedMesh = true; + + } + + } + + if ( nodeDef.camera !== undefined ) { + + this._addNodeRef( this.cameraCache, nodeDef.camera ); + + } + + } + + } + + /** + * Counts references to shared node / Object3D resources. These resources + * can be reused, or "instantiated", at multiple nodes in the scene + * hierarchy. Mesh, Camera, and Light instances are instantiated and must + * be marked. Non-scenegraph resources (like Materials, Geometries, and + * Textures) can be reused directly and are not marked here. + * + * Example: CesiumMilkTruck sample model reuses "Wheel" meshes. + */ + _addNodeRef( cache, index ) { + + if ( index === undefined ) return; + + if ( cache.refs[ index ] === undefined ) { + + cache.refs[ index ] = cache.uses[ index ] = 0; + + } + + cache.refs[ index ] ++; + + } + + /** Returns a reference to a shared resource, cloning it if necessary. */ + _getNodeRef( cache, index, object ) { + + if ( cache.refs[ index ] <= 1 ) return object; + + const ref = object.clone(); + + // Propagates mappings to the cloned object, prevents mappings on the + // original object from being lost. + const updateMappings = ( original, clone ) => { + + const mappings = this.associations.get( original ); + if ( mappings != null ) { + + this.associations.set( clone, mappings ); + + } + + for ( const [ i, child ] of original.children.entries() ) { + + updateMappings( child, clone.children[ i ] ); + + } + + }; + + updateMappings( object, ref ); + + ref.name += '_instance_' + ( cache.uses[ index ] ++ ); + + return ref; + + } + + _invokeOne( func ) { + + const extensions = Object.values( this.plugins ); + extensions.push( this ); + + for ( let i = 0; i < extensions.length; i ++ ) { + + const result = func( extensions[ i ] ); + + if ( result ) return result; + + } + + return null; + + } + + _invokeAll( func ) { + + const extensions = Object.values( this.plugins ); + extensions.unshift( this ); + + const pending = []; + + for ( let i = 0; i < extensions.length; i ++ ) { + + const result = func( extensions[ i ] ); + + if ( result ) pending.push( result ); + + } + + return pending; + + } + + /** + * Requests the specified dependency asynchronously, with caching. + * @param {string} type + * @param {number} index + * @return {Promise} + */ + getDependency( type, index ) { + + const cacheKey = type + ':' + index; + let dependency = this.cache.get( cacheKey ); + + if ( ! dependency ) { + + switch ( type ) { + + case 'scene': + dependency = this.loadScene( index ); + break; + + case 'node': + dependency = this._invokeOne( function ( ext ) { + + return ext.loadNode && ext.loadNode( index ); + + } ); + break; + + case 'mesh': + dependency = this._invokeOne( function ( ext ) { + + return ext.loadMesh && ext.loadMesh( index ); + + } ); + break; + + case 'accessor': + dependency = this.loadAccessor( index ); + break; + + case 'bufferView': + dependency = this._invokeOne( function ( ext ) { + + return ext.loadBufferView && ext.loadBufferView( index ); + + } ); + break; + + case 'buffer': + dependency = this.loadBuffer( index ); + break; + + case 'material': + dependency = this._invokeOne( function ( ext ) { + + return ext.loadMaterial && ext.loadMaterial( index ); + + } ); + break; + + case 'texture': + dependency = this._invokeOne( function ( ext ) { + + return ext.loadTexture && ext.loadTexture( index ); + + } ); + break; + + case 'skin': + dependency = this.loadSkin( index ); + break; + + case 'animation': + dependency = this._invokeOne( function ( ext ) { + + return ext.loadAnimation && ext.loadAnimation( index ); + + } ); + break; + + case 'camera': + dependency = this.loadCamera( index ); + break; + + default: + dependency = this._invokeOne( function ( ext ) { + + return ext != this && ext.getDependency && ext.getDependency( type, index ); + + } ); + + if ( ! dependency ) { + + throw new Error( 'Unknown type: ' + type ); + + } + + break; + + } + + this.cache.add( cacheKey, dependency ); + + } + + return dependency; + + } + + /** + * Requests all dependencies of the specified type asynchronously, with caching. + * @param {string} type + * @return {Promise>} + */ + getDependencies( type ) { + + let dependencies = this.cache.get( type ); + + if ( ! dependencies ) { + + const parser = this; + const defs = this.json[ type + ( type === 'mesh' ? 'es' : 's' ) ] || []; + + dependencies = Promise.all( defs.map( function ( def, index ) { + + return parser.getDependency( type, index ); + + } ) ); + + this.cache.add( type, dependencies ); + + } + + return dependencies; + + } + + /** + * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#buffers-and-buffer-views + * @param {number} bufferIndex + * @return {Promise} + */ + loadBuffer( bufferIndex ) { + + const bufferDef = this.json.buffers[ bufferIndex ]; + const loader = this.fileLoader; + + if ( bufferDef.type && bufferDef.type !== 'arraybuffer' ) { + + throw new Error( 'THREE.GLTFLoader: ' + bufferDef.type + ' buffer type is not supported.' ); + + } + + // If present, GLB container is required to be the first buffer. + if ( bufferDef.uri === undefined && bufferIndex === 0 ) { + + return Promise.resolve( this.extensions[ EXTENSIONS.KHR_BINARY_GLTF ].body ); + + } + + const options = this.options; + + return new Promise( function ( resolve, reject ) { + + loader.load( LoaderUtils.resolveURL( bufferDef.uri, options.path ), resolve, undefined, function () { + + reject( new Error( 'THREE.GLTFLoader: Failed to load buffer "' + bufferDef.uri + '".' ) ); + + } ); + + } ); + + } + + /** + * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#buffers-and-buffer-views + * @param {number} bufferViewIndex + * @return {Promise} + */ + loadBufferView( bufferViewIndex ) { + + const bufferViewDef = this.json.bufferViews[ bufferViewIndex ]; + + return this.getDependency( 'buffer', bufferViewDef.buffer ).then( function ( buffer ) { + + const byteLength = bufferViewDef.byteLength || 0; + const byteOffset = bufferViewDef.byteOffset || 0; + return buffer.slice( byteOffset, byteOffset + byteLength ); + + } ); + + } + + /** + * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#accessors + * @param {number} accessorIndex + * @return {Promise} + */ + loadAccessor( accessorIndex ) { + + const parser = this; + const json = this.json; + + const accessorDef = this.json.accessors[ accessorIndex ]; + + if ( accessorDef.bufferView === undefined && accessorDef.sparse === undefined ) { + + const itemSize = WEBGL_TYPE_SIZES[ accessorDef.type ]; + const TypedArray = WEBGL_COMPONENT_TYPES[ accessorDef.componentType ]; + const normalized = accessorDef.normalized === true; + + const array = new TypedArray( accessorDef.count * itemSize ); + return Promise.resolve( new BufferAttribute( array, itemSize, normalized ) ); + + } + + const pendingBufferViews = []; + + if ( accessorDef.bufferView !== undefined ) { + + pendingBufferViews.push( this.getDependency( 'bufferView', accessorDef.bufferView ) ); + + } else { + + pendingBufferViews.push( null ); + + } + + if ( accessorDef.sparse !== undefined ) { + + pendingBufferViews.push( this.getDependency( 'bufferView', accessorDef.sparse.indices.bufferView ) ); + pendingBufferViews.push( this.getDependency( 'bufferView', accessorDef.sparse.values.bufferView ) ); + + } + + return Promise.all( pendingBufferViews ).then( function ( bufferViews ) { + + const bufferView = bufferViews[ 0 ]; + + const itemSize = WEBGL_TYPE_SIZES[ accessorDef.type ]; + const TypedArray = WEBGL_COMPONENT_TYPES[ accessorDef.componentType ]; + + // For VEC3: itemSize is 3, elementBytes is 4, itemBytes is 12. + const elementBytes = TypedArray.BYTES_PER_ELEMENT; + const itemBytes = elementBytes * itemSize; + const byteOffset = accessorDef.byteOffset || 0; + const byteStride = accessorDef.bufferView !== undefined ? json.bufferViews[ accessorDef.bufferView ].byteStride : undefined; + const normalized = accessorDef.normalized === true; + let array, bufferAttribute; + + // The buffer is not interleaved if the stride is the item size in bytes. + if ( byteStride && byteStride !== itemBytes ) { + + // Each "slice" of the buffer, as defined by 'count' elements of 'byteStride' bytes, gets its own InterleavedBuffer + // This makes sure that IBA.count reflects accessor.count properly + const ibSlice = Math.floor( byteOffset / byteStride ); + const ibCacheKey = 'InterleavedBuffer:' + accessorDef.bufferView + ':' + accessorDef.componentType + ':' + ibSlice + ':' + accessorDef.count; + let ib = parser.cache.get( ibCacheKey ); + + if ( ! ib ) { + + array = new TypedArray( bufferView, ibSlice * byteStride, accessorDef.count * byteStride / elementBytes ); + + // Integer parameters to IB/IBA are in array elements, not bytes. + ib = new InterleavedBuffer( array, byteStride / elementBytes ); + + parser.cache.add( ibCacheKey, ib ); + + } + + bufferAttribute = new InterleavedBufferAttribute( ib, itemSize, ( byteOffset % byteStride ) / elementBytes, normalized ); + + } else { + + if ( bufferView === null ) { + + array = new TypedArray( accessorDef.count * itemSize ); + + } else { + + array = new TypedArray( bufferView, byteOffset, accessorDef.count * itemSize ); + + } + + bufferAttribute = new BufferAttribute( array, itemSize, normalized ); + + } + + // https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#sparse-accessors + if ( accessorDef.sparse !== undefined ) { + + const itemSizeIndices = WEBGL_TYPE_SIZES.SCALAR; + const TypedArrayIndices = WEBGL_COMPONENT_TYPES[ accessorDef.sparse.indices.componentType ]; + + const byteOffsetIndices = accessorDef.sparse.indices.byteOffset || 0; + const byteOffsetValues = accessorDef.sparse.values.byteOffset || 0; + + const sparseIndices = new TypedArrayIndices( bufferViews[ 1 ], byteOffsetIndices, accessorDef.sparse.count * itemSizeIndices ); + const sparseValues = new TypedArray( bufferViews[ 2 ], byteOffsetValues, accessorDef.sparse.count * itemSize ); + + if ( bufferView !== null ) { + + // Avoid modifying the original ArrayBuffer, if the bufferView wasn't initialized with zeroes. + bufferAttribute = new BufferAttribute( bufferAttribute.array.slice(), bufferAttribute.itemSize, bufferAttribute.normalized ); + + } + + for ( let i = 0, il = sparseIndices.length; i < il; i ++ ) { + + const index = sparseIndices[ i ]; + + bufferAttribute.setX( index, sparseValues[ i * itemSize ] ); + if ( itemSize >= 2 ) bufferAttribute.setY( index, sparseValues[ i * itemSize + 1 ] ); + if ( itemSize >= 3 ) bufferAttribute.setZ( index, sparseValues[ i * itemSize + 2 ] ); + if ( itemSize >= 4 ) bufferAttribute.setW( index, sparseValues[ i * itemSize + 3 ] ); + if ( itemSize >= 5 ) throw new Error( 'THREE.GLTFLoader: Unsupported itemSize in sparse BufferAttribute.' ); + + } + + } + + return bufferAttribute; + + } ); + + } + + /** + * Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#textures + * @param {number} textureIndex + * @return {Promise} + */ + loadTexture( textureIndex ) { + + const json = this.json; + const options = this.options; + const textureDef = json.textures[ textureIndex ]; + const sourceIndex = textureDef.source; + const sourceDef = json.images[ sourceIndex ]; + + let loader = this.textureLoader; + + if ( sourceDef.uri ) { + + const handler = options.manager.getHandler( sourceDef.uri ); + if ( handler !== null ) loader = handler; + + } + + return this.loadTextureImage( textureIndex, sourceIndex, loader ); + + } + + loadTextureImage( textureIndex, sourceIndex, loader ) { + + const parser = this; + const json = this.json; + + const textureDef = json.textures[ textureIndex ]; + const sourceDef = json.images[ sourceIndex ]; + + const cacheKey = ( sourceDef.uri || sourceDef.bufferView ) + ':' + textureDef.sampler; + + if ( this.textureCache[ cacheKey ] ) { + + // See https://github.com/mrdoob/three.js/issues/21559. + return this.textureCache[ cacheKey ]; + + } + + const promise = this.loadImageSource( sourceIndex, loader ).then( function ( texture ) { + + texture.flipY = false; + + texture.name = textureDef.name || sourceDef.name || ''; + + if ( texture.name === '' && typeof sourceDef.uri === 'string' && sourceDef.uri.startsWith( 'data:image/' ) === false ) { + + texture.name = sourceDef.uri; + + } + + const samplers = json.samplers || {}; + const sampler = samplers[ textureDef.sampler ] || {}; + + texture.magFilter = WEBGL_FILTERS[ sampler.magFilter ] || LinearFilter; + texture.minFilter = WEBGL_FILTERS[ sampler.minFilter ] || LinearMipmapLinearFilter; + texture.wrapS = WEBGL_WRAPPINGS[ sampler.wrapS ] || RepeatWrapping; + texture.wrapT = WEBGL_WRAPPINGS[ sampler.wrapT ] || RepeatWrapping; + + parser.associations.set( texture, { textures: textureIndex } ); + + return texture; + + } ).catch( function () { + + return null; + + } ); + + this.textureCache[ cacheKey ] = promise; + + return promise; + + } + + loadImageSource( sourceIndex, loader ) { + + const parser = this; + const json = this.json; + const options = this.options; + + if ( this.sourceCache[ sourceIndex ] !== undefined ) { + + return this.sourceCache[ sourceIndex ].then( ( texture ) => texture.clone() ); + + } + + const sourceDef = json.images[ sourceIndex ]; + + const URL = self.URL || self.webkitURL; + + let sourceURI = sourceDef.uri || ''; + let isObjectURL = false; + + if ( sourceDef.bufferView !== undefined ) { + + // Load binary image data from bufferView, if provided. + + sourceURI = parser.getDependency( 'bufferView', sourceDef.bufferView ).then( function ( bufferView ) { + + isObjectURL = true; + const blob = new Blob( [ bufferView ], { type: sourceDef.mimeType } ); + sourceURI = URL.createObjectURL( blob ); + return sourceURI; + + } ); + + } else if ( sourceDef.uri === undefined ) { + + throw new Error( 'THREE.GLTFLoader: Image ' + sourceIndex + ' is missing URI and bufferView' ); + + } + + const promise = Promise.resolve( sourceURI ).then( function ( sourceURI ) { + + return new Promise( function ( resolve, reject ) { + + let onLoad = resolve; + + if ( loader.isImageBitmapLoader === true ) { + + onLoad = function ( imageBitmap ) { + + const texture = new Texture( imageBitmap ); + texture.needsUpdate = true; + + resolve( texture ); + + }; + + } + + loader.load( LoaderUtils.resolveURL( sourceURI, options.path ), onLoad, undefined, reject ); + + } ); + + } ).then( function ( texture ) { + + // Clean up resources and configure Texture. + + if ( isObjectURL === true ) { + + URL.revokeObjectURL( sourceURI ); + + } + + texture.userData.mimeType = sourceDef.mimeType || getImageURIMimeType( sourceDef.uri ); + + return texture; + + } ).catch( function ( error ) { + + console.error( 'THREE.GLTFLoader: Couldn\'t load texture', sourceURI ); + throw error; + + } ); + + this.sourceCache[ sourceIndex ] = promise; + return promise; + + } + + /** + * Asynchronously assigns a texture to the given material parameters. + * @param {Object} materialParams + * @param {string} mapName + * @param {Object} mapDef + * @return {Promise} + */ + assignTexture( materialParams, mapName, mapDef, colorSpace ) { + + const parser = this; + + return this.getDependency( 'texture', mapDef.index ).then( function ( texture ) { + + if ( ! texture ) return null; + + if ( mapDef.texCoord !== undefined && mapDef.texCoord > 0 ) { + + texture = texture.clone(); + texture.channel = mapDef.texCoord; + + } + + if ( parser.extensions[ EXTENSIONS.KHR_TEXTURE_TRANSFORM ] ) { + + const transform = mapDef.extensions !== undefined ? mapDef.extensions[ EXTENSIONS.KHR_TEXTURE_TRANSFORM ] : undefined; + + if ( transform ) { + + const gltfReference = parser.associations.get( texture ); + texture = parser.extensions[ EXTENSIONS.KHR_TEXTURE_TRANSFORM ].extendTexture( texture, transform ); + parser.associations.set( texture, gltfReference ); + + } + + } + + if ( colorSpace !== undefined ) { + + texture.colorSpace = colorSpace; + + } + + materialParams[ mapName ] = texture; + + return texture; + + } ); + + } + + /** + * Assigns final material to a Mesh, Line, or Points instance. The instance + * already has a material (generated from the glTF material options alone) + * but reuse of the same glTF material may require multiple threejs materials + * to accommodate different primitive types, defines, etc. New materials will + * be created if necessary, and reused from a cache. + * @param {Object3D} mesh Mesh, Line, or Points instance. + */ + assignFinalMaterial( mesh ) { + + const geometry = mesh.geometry; + let material = mesh.material; + + const useDerivativeTangents = geometry.attributes.tangent === undefined; + const useVertexColors = geometry.attributes.color !== undefined; + const useFlatShading = geometry.attributes.normal === undefined; + + if ( mesh.isPoints ) { + + const cacheKey = 'PointsMaterial:' + material.uuid; + + let pointsMaterial = this.cache.get( cacheKey ); + + if ( ! pointsMaterial ) { + + pointsMaterial = new PointsMaterial(); + Material.prototype.copy.call( pointsMaterial, material ); + pointsMaterial.color.copy( material.color ); + pointsMaterial.map = material.map; + pointsMaterial.sizeAttenuation = false; // glTF spec says points should be 1px + + this.cache.add( cacheKey, pointsMaterial ); + + } + + material = pointsMaterial; + + } else if ( mesh.isLine ) { + + const cacheKey = 'LineBasicMaterial:' + material.uuid; + + let lineMaterial = this.cache.get( cacheKey ); + + if ( ! lineMaterial ) { + + lineMaterial = new LineBasicMaterial(); + Material.prototype.copy.call( lineMaterial, material ); + lineMaterial.color.copy( material.color ); + lineMaterial.map = material.map; + + this.cache.add( cacheKey, lineMaterial ); + + } + + material = lineMaterial; + + } + + // Clone the material if it will be modified + if ( useDerivativeTangents || useVertexColors || useFlatShading ) { + + let cacheKey = 'ClonedMaterial:' + material.uuid + ':'; + + if ( useDerivativeTangents ) cacheKey += 'derivative-tangents:'; + if ( useVertexColors ) cacheKey += 'vertex-colors:'; + if ( useFlatShading ) cacheKey += 'flat-shading:'; + + let cachedMaterial = this.cache.get( cacheKey ); + + if ( ! cachedMaterial ) { + + cachedMaterial = material.clone(); + + if ( useVertexColors ) cachedMaterial.vertexColors = true; + if ( useFlatShading ) cachedMaterial.flatShading = true; + + if ( useDerivativeTangents ) { + + // https://github.com/mrdoob/three.js/issues/11438#issuecomment-507003995 + if ( cachedMaterial.normalScale ) cachedMaterial.normalScale.y *= - 1; + if ( cachedMaterial.clearcoatNormalScale ) cachedMaterial.clearcoatNormalScale.y *= - 1; + + } + + this.cache.add( cacheKey, cachedMaterial ); + + this.associations.set( cachedMaterial, this.associations.get( material ) ); + + } + + material = cachedMaterial; + + } + + mesh.material = material; + + } + + getMaterialType( /* materialIndex */ ) { + + return MeshStandardMaterial; + + } + + /** + * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#materials + * @param {number} materialIndex + * @return {Promise} + */ + loadMaterial( materialIndex ) { + + const parser = this; + const json = this.json; + const extensions = this.extensions; + const materialDef = json.materials[ materialIndex ]; + + let materialType; + const materialParams = {}; + const materialExtensions = materialDef.extensions || {}; + + const pending = []; + + if ( materialExtensions[ EXTENSIONS.KHR_MATERIALS_UNLIT ] ) { + + const kmuExtension = extensions[ EXTENSIONS.KHR_MATERIALS_UNLIT ]; + materialType = kmuExtension.getMaterialType(); + pending.push( kmuExtension.extendParams( materialParams, materialDef, parser ) ); + + } else { + + // Specification: + // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#metallic-roughness-material + + const metallicRoughness = materialDef.pbrMetallicRoughness || {}; + + materialParams.color = new Color( 1.0, 1.0, 1.0 ); + materialParams.opacity = 1.0; + + if ( Array.isArray( metallicRoughness.baseColorFactor ) ) { + + const array = metallicRoughness.baseColorFactor; + + materialParams.color.setRGB( array[ 0 ], array[ 1 ], array[ 2 ], LinearSRGBColorSpace ); + materialParams.opacity = array[ 3 ]; + + } + + if ( metallicRoughness.baseColorTexture !== undefined ) { + + pending.push( parser.assignTexture( materialParams, 'map', metallicRoughness.baseColorTexture, SRGBColorSpace ) ); + + } + + materialParams.metalness = metallicRoughness.metallicFactor !== undefined ? metallicRoughness.metallicFactor : 1.0; + materialParams.roughness = metallicRoughness.roughnessFactor !== undefined ? metallicRoughness.roughnessFactor : 1.0; + + if ( metallicRoughness.metallicRoughnessTexture !== undefined ) { + + pending.push( parser.assignTexture( materialParams, 'metalnessMap', metallicRoughness.metallicRoughnessTexture ) ); + pending.push( parser.assignTexture( materialParams, 'roughnessMap', metallicRoughness.metallicRoughnessTexture ) ); + + } + + materialType = this._invokeOne( function ( ext ) { + + return ext.getMaterialType && ext.getMaterialType( materialIndex ); + + } ); + + pending.push( Promise.all( this._invokeAll( function ( ext ) { + + return ext.extendMaterialParams && ext.extendMaterialParams( materialIndex, materialParams ); + + } ) ) ); + + } + + if ( materialDef.doubleSided === true ) { + + materialParams.side = DoubleSide; + + } + + const alphaMode = materialDef.alphaMode || ALPHA_MODES.OPAQUE; + + if ( alphaMode === ALPHA_MODES.BLEND ) { + + materialParams.transparent = true; + + // See: https://github.com/mrdoob/three.js/issues/17706 + materialParams.depthWrite = false; + + } else { + + materialParams.transparent = false; + + if ( alphaMode === ALPHA_MODES.MASK ) { + + materialParams.alphaTest = materialDef.alphaCutoff !== undefined ? materialDef.alphaCutoff : 0.5; + + } + + } + + if ( materialDef.normalTexture !== undefined && materialType !== MeshBasicMaterial ) { + + pending.push( parser.assignTexture( materialParams, 'normalMap', materialDef.normalTexture ) ); + + materialParams.normalScale = new Vector2( 1, 1 ); + + if ( materialDef.normalTexture.scale !== undefined ) { + + const scale = materialDef.normalTexture.scale; + + materialParams.normalScale.set( scale, scale ); + + } + + } + + if ( materialDef.occlusionTexture !== undefined && materialType !== MeshBasicMaterial ) { + + pending.push( parser.assignTexture( materialParams, 'aoMap', materialDef.occlusionTexture ) ); + + if ( materialDef.occlusionTexture.strength !== undefined ) { + + materialParams.aoMapIntensity = materialDef.occlusionTexture.strength; + + } + + } + + if ( materialDef.emissiveFactor !== undefined && materialType !== MeshBasicMaterial ) { + + const emissiveFactor = materialDef.emissiveFactor; + materialParams.emissive = new Color().setRGB( emissiveFactor[ 0 ], emissiveFactor[ 1 ], emissiveFactor[ 2 ], LinearSRGBColorSpace ); + + } + + if ( materialDef.emissiveTexture !== undefined && materialType !== MeshBasicMaterial ) { + + pending.push( parser.assignTexture( materialParams, 'emissiveMap', materialDef.emissiveTexture, SRGBColorSpace ) ); + + } + + return Promise.all( pending ).then( function () { + + const material = new materialType( materialParams ); + + if ( materialDef.name ) material.name = materialDef.name; + + assignExtrasToUserData( material, materialDef ); + + parser.associations.set( material, { materials: materialIndex } ); + + if ( materialDef.extensions ) addUnknownExtensionsToUserData( extensions, material, materialDef ); + + return material; + + } ); + + } + + /** When Object3D instances are targeted by animation, they need unique names. */ + createUniqueName( originalName ) { + + const sanitizedName = PropertyBinding.sanitizeNodeName( originalName || '' ); + + if ( sanitizedName in this.nodeNamesUsed ) { + + return sanitizedName + '_' + ( ++ this.nodeNamesUsed[ sanitizedName ] ); + + } else { + + this.nodeNamesUsed[ sanitizedName ] = 0; + + return sanitizedName; + + } + + } + + /** + * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#geometry + * + * Creates BufferGeometries from primitives. + * + * @param {Array} primitives + * @return {Promise>} + */ + loadGeometries( primitives ) { + + const parser = this; + const extensions = this.extensions; + const cache = this.primitiveCache; + + function createDracoPrimitive( primitive ) { + + return extensions[ EXTENSIONS.KHR_DRACO_MESH_COMPRESSION ] + .decodePrimitive( primitive, parser ) + .then( function ( geometry ) { + + return addPrimitiveAttributes( geometry, primitive, parser ); + + } ); + + } + + const pending = []; + + for ( let i = 0, il = primitives.length; i < il; i ++ ) { + + const primitive = primitives[ i ]; + const cacheKey = createPrimitiveKey( primitive ); + + // See if we've already created this geometry + const cached = cache[ cacheKey ]; + + if ( cached ) { + + // Use the cached geometry if it exists + pending.push( cached.promise ); + + } else { + + let geometryPromise; + + if ( primitive.extensions && primitive.extensions[ EXTENSIONS.KHR_DRACO_MESH_COMPRESSION ] ) { + + // Use DRACO geometry if available + geometryPromise = createDracoPrimitive( primitive ); + + } else { + + // Otherwise create a new geometry + geometryPromise = addPrimitiveAttributes( new BufferGeometry(), primitive, parser ); + + } + + // Cache this geometry + cache[ cacheKey ] = { primitive: primitive, promise: geometryPromise }; + + pending.push( geometryPromise ); + + } + + } + + return Promise.all( pending ); + + } + + /** + * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#meshes + * @param {number} meshIndex + * @return {Promise} + */ + loadMesh( meshIndex ) { + + const parser = this; + const json = this.json; + const extensions = this.extensions; + + const meshDef = json.meshes[ meshIndex ]; + const primitives = meshDef.primitives; + + const pending = []; + + for ( let i = 0, il = primitives.length; i < il; i ++ ) { + + const material = primitives[ i ].material === undefined + ? createDefaultMaterial( this.cache ) + : this.getDependency( 'material', primitives[ i ].material ); + + pending.push( material ); + + } + + pending.push( parser.loadGeometries( primitives ) ); + + return Promise.all( pending ).then( function ( results ) { + + const materials = results.slice( 0, results.length - 1 ); + const geometries = results[ results.length - 1 ]; + + const meshes = []; + + for ( let i = 0, il = geometries.length; i < il; i ++ ) { + + const geometry = geometries[ i ]; + const primitive = primitives[ i ]; + + // 1. create Mesh + + let mesh; + + const material = materials[ i ]; + + if ( primitive.mode === WEBGL_CONSTANTS.TRIANGLES || + primitive.mode === WEBGL_CONSTANTS.TRIANGLE_STRIP || + primitive.mode === WEBGL_CONSTANTS.TRIANGLE_FAN || + primitive.mode === undefined ) { + + // .isSkinnedMesh isn't in glTF spec. See ._markDefs() + mesh = meshDef.isSkinnedMesh === true + ? new SkinnedMesh( geometry, material ) + : new Mesh( geometry, material ); + + if ( mesh.isSkinnedMesh === true ) { + + // normalize skin weights to fix malformed assets (see #15319) + mesh.normalizeSkinWeights(); + + } + + if ( primitive.mode === WEBGL_CONSTANTS.TRIANGLE_STRIP ) { + + mesh.geometry = toTrianglesDrawMode( mesh.geometry, TriangleStripDrawMode ); + + } else if ( primitive.mode === WEBGL_CONSTANTS.TRIANGLE_FAN ) { + + mesh.geometry = toTrianglesDrawMode( mesh.geometry, TriangleFanDrawMode ); + + } + + } else if ( primitive.mode === WEBGL_CONSTANTS.LINES ) { + + mesh = new LineSegments( geometry, material ); + + } else if ( primitive.mode === WEBGL_CONSTANTS.LINE_STRIP ) { + + mesh = new Line( geometry, material ); + + } else if ( primitive.mode === WEBGL_CONSTANTS.LINE_LOOP ) { + + mesh = new LineLoop( geometry, material ); + + } else if ( primitive.mode === WEBGL_CONSTANTS.POINTS ) { + + mesh = new Points( geometry, material ); + + } else { + + throw new Error( 'THREE.GLTFLoader: Primitive mode unsupported: ' + primitive.mode ); + + } + + if ( Object.keys( mesh.geometry.morphAttributes ).length > 0 ) { + + updateMorphTargets( mesh, meshDef ); + + } + + mesh.name = parser.createUniqueName( meshDef.name || ( 'mesh_' + meshIndex ) ); + + assignExtrasToUserData( mesh, meshDef ); + + if ( primitive.extensions ) addUnknownExtensionsToUserData( extensions, mesh, primitive ); + + parser.assignFinalMaterial( mesh ); + + meshes.push( mesh ); + + } + + for ( let i = 0, il = meshes.length; i < il; i ++ ) { + + parser.associations.set( meshes[ i ], { + meshes: meshIndex, + primitives: i + } ); + + } + + if ( meshes.length === 1 ) { + + if ( meshDef.extensions ) addUnknownExtensionsToUserData( extensions, meshes[ 0 ], meshDef ); + + return meshes[ 0 ]; + + } + + const group = new Group(); + + if ( meshDef.extensions ) addUnknownExtensionsToUserData( extensions, group, meshDef ); + + parser.associations.set( group, { meshes: meshIndex } ); + + for ( let i = 0, il = meshes.length; i < il; i ++ ) { + + group.add( meshes[ i ] ); + + } + + return group; + + } ); + + } + + /** + * Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#cameras + * @param {number} cameraIndex + * @return {Promise} + */ + loadCamera( cameraIndex ) { + + let camera; + const cameraDef = this.json.cameras[ cameraIndex ]; + const params = cameraDef[ cameraDef.type ]; + + if ( ! params ) { + + console.warn( 'THREE.GLTFLoader: Missing camera parameters.' ); + return; + + } + + if ( cameraDef.type === 'perspective' ) { + + camera = new PerspectiveCamera( MathUtils.radToDeg( params.yfov ), params.aspectRatio || 1, params.znear || 1, params.zfar || 2e6 ); + + } else if ( cameraDef.type === 'orthographic' ) { + + camera = new OrthographicCamera( - params.xmag, params.xmag, params.ymag, - params.ymag, params.znear, params.zfar ); + + } + + if ( cameraDef.name ) camera.name = this.createUniqueName( cameraDef.name ); + + assignExtrasToUserData( camera, cameraDef ); + + return Promise.resolve( camera ); + + } + + /** + * Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins + * @param {number} skinIndex + * @return {Promise} + */ + loadSkin( skinIndex ) { + + const skinDef = this.json.skins[ skinIndex ]; + + const pending = []; + + for ( let i = 0, il = skinDef.joints.length; i < il; i ++ ) { + + pending.push( this._loadNodeShallow( skinDef.joints[ i ] ) ); + + } + + if ( skinDef.inverseBindMatrices !== undefined ) { + + pending.push( this.getDependency( 'accessor', skinDef.inverseBindMatrices ) ); + + } else { + + pending.push( null ); + + } + + return Promise.all( pending ).then( function ( results ) { + + const inverseBindMatrices = results.pop(); + const jointNodes = results; + + // Note that bones (joint nodes) may or may not be in the + // scene graph at this time. + + const bones = []; + const boneInverses = []; + + for ( let i = 0, il = jointNodes.length; i < il; i ++ ) { + + const jointNode = jointNodes[ i ]; + + if ( jointNode ) { + + bones.push( jointNode ); + + const mat = new Matrix4(); + + if ( inverseBindMatrices !== null ) { + + mat.fromArray( inverseBindMatrices.array, i * 16 ); + + } + + boneInverses.push( mat ); + + } else { + + console.warn( 'THREE.GLTFLoader: Joint "%s" could not be found.', skinDef.joints[ i ] ); + + } + + } + + return new Skeleton( bones, boneInverses ); + + } ); + + } + + /** + * Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#animations + * @param {number} animationIndex + * @return {Promise} + */ + loadAnimation( animationIndex ) { + + const json = this.json; + const parser = this; + + const animationDef = json.animations[ animationIndex ]; + const animationName = animationDef.name ? animationDef.name : 'animation_' + animationIndex; + + const pendingNodes = []; + const pendingInputAccessors = []; + const pendingOutputAccessors = []; + const pendingSamplers = []; + const pendingTargets = []; + + for ( let i = 0, il = animationDef.channels.length; i < il; i ++ ) { + + const channel = animationDef.channels[ i ]; + const sampler = animationDef.samplers[ channel.sampler ]; + const target = channel.target; + const name = target.node; + const input = animationDef.parameters !== undefined ? animationDef.parameters[ sampler.input ] : sampler.input; + const output = animationDef.parameters !== undefined ? animationDef.parameters[ sampler.output ] : sampler.output; + + if ( target.node === undefined ) continue; + + pendingNodes.push( this.getDependency( 'node', name ) ); + pendingInputAccessors.push( this.getDependency( 'accessor', input ) ); + pendingOutputAccessors.push( this.getDependency( 'accessor', output ) ); + pendingSamplers.push( sampler ); + pendingTargets.push( target ); + + } + + return Promise.all( [ + + Promise.all( pendingNodes ), + Promise.all( pendingInputAccessors ), + Promise.all( pendingOutputAccessors ), + Promise.all( pendingSamplers ), + Promise.all( pendingTargets ) + + ] ).then( function ( dependencies ) { + + const nodes = dependencies[ 0 ]; + const inputAccessors = dependencies[ 1 ]; + const outputAccessors = dependencies[ 2 ]; + const samplers = dependencies[ 3 ]; + const targets = dependencies[ 4 ]; + + const tracks = []; + + for ( let i = 0, il = nodes.length; i < il; i ++ ) { + + const node = nodes[ i ]; + const inputAccessor = inputAccessors[ i ]; + const outputAccessor = outputAccessors[ i ]; + const sampler = samplers[ i ]; + const target = targets[ i ]; + + if ( node === undefined ) continue; + + if ( node.updateMatrix ) { + + node.updateMatrix(); + + } + + const createdTracks = parser._createAnimationTracks( node, inputAccessor, outputAccessor, sampler, target ); + + if ( createdTracks ) { + + for ( let k = 0; k < createdTracks.length; k ++ ) { + + tracks.push( createdTracks[ k ] ); + + } + + } + + } + + return new AnimationClip( animationName, undefined, tracks ); + + } ); + + } + + createNodeMesh( nodeIndex ) { + + const json = this.json; + const parser = this; + const nodeDef = json.nodes[ nodeIndex ]; + + if ( nodeDef.mesh === undefined ) return null; + + return parser.getDependency( 'mesh', nodeDef.mesh ).then( function ( mesh ) { + + const node = parser._getNodeRef( parser.meshCache, nodeDef.mesh, mesh ); + + // if weights are provided on the node, override weights on the mesh. + if ( nodeDef.weights !== undefined ) { + + node.traverse( function ( o ) { + + if ( ! o.isMesh ) return; + + for ( let i = 0, il = nodeDef.weights.length; i < il; i ++ ) { + + o.morphTargetInfluences[ i ] = nodeDef.weights[ i ]; + + } + + } ); + + } + + return node; + + } ); + + } + + /** + * Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#nodes-and-hierarchy + * @param {number} nodeIndex + * @return {Promise} + */ + loadNode( nodeIndex ) { + + const json = this.json; + const parser = this; + + const nodeDef = json.nodes[ nodeIndex ]; + + const nodePending = parser._loadNodeShallow( nodeIndex ); + + const childPending = []; + const childrenDef = nodeDef.children || []; + + for ( let i = 0, il = childrenDef.length; i < il; i ++ ) { + + childPending.push( parser.getDependency( 'node', childrenDef[ i ] ) ); + + } + + const skeletonPending = nodeDef.skin === undefined + ? Promise.resolve( null ) + : parser.getDependency( 'skin', nodeDef.skin ); + + return Promise.all( [ + nodePending, + Promise.all( childPending ), + skeletonPending + ] ).then( function ( results ) { + + const node = results[ 0 ]; + const children = results[ 1 ]; + const skeleton = results[ 2 ]; + + if ( skeleton !== null ) { + + // This full traverse should be fine because + // child glTF nodes have not been added to this node yet. + node.traverse( function ( mesh ) { + + if ( ! mesh.isSkinnedMesh ) return; + + mesh.bind( skeleton, _identityMatrix ); + + } ); + + } + + for ( let i = 0, il = children.length; i < il; i ++ ) { + + node.add( children[ i ] ); + + } + + return node; + + } ); + + } + + // ._loadNodeShallow() parses a single node. + // skin and child nodes are created and added in .loadNode() (no '_' prefix). + _loadNodeShallow( nodeIndex ) { + + const json = this.json; + const extensions = this.extensions; + const parser = this; + + // This method is called from .loadNode() and .loadSkin(). + // Cache a node to avoid duplication. + + if ( this.nodeCache[ nodeIndex ] !== undefined ) { + + return this.nodeCache[ nodeIndex ]; + + } + + const nodeDef = json.nodes[ nodeIndex ]; + + // reserve node's name before its dependencies, so the root has the intended name. + const nodeName = nodeDef.name ? parser.createUniqueName( nodeDef.name ) : ''; + + const pending = []; + + const meshPromise = parser._invokeOne( function ( ext ) { + + return ext.createNodeMesh && ext.createNodeMesh( nodeIndex ); + + } ); + + if ( meshPromise ) { + + pending.push( meshPromise ); + + } + + if ( nodeDef.camera !== undefined ) { + + pending.push( parser.getDependency( 'camera', nodeDef.camera ).then( function ( camera ) { + + return parser._getNodeRef( parser.cameraCache, nodeDef.camera, camera ); + + } ) ); + + } + + parser._invokeAll( function ( ext ) { + + return ext.createNodeAttachment && ext.createNodeAttachment( nodeIndex ); + + } ).forEach( function ( promise ) { + + pending.push( promise ); + + } ); + + this.nodeCache[ nodeIndex ] = Promise.all( pending ).then( function ( objects ) { + + let node; + + // .isBone isn't in glTF spec. See ._markDefs + if ( nodeDef.isBone === true ) { + + node = new Bone(); + + } else if ( objects.length > 1 ) { + + node = new Group(); + + } else if ( objects.length === 1 ) { + + node = objects[ 0 ]; + + } else { + + node = new Object3D(); + + } + + if ( node !== objects[ 0 ] ) { + + for ( let i = 0, il = objects.length; i < il; i ++ ) { + + node.add( objects[ i ] ); + + } + + } + + if ( nodeDef.name ) { + + node.userData.name = nodeDef.name; + node.name = nodeName; + + } + + assignExtrasToUserData( node, nodeDef ); + + if ( nodeDef.extensions ) addUnknownExtensionsToUserData( extensions, node, nodeDef ); + + if ( nodeDef.matrix !== undefined ) { + + const matrix = new Matrix4(); + matrix.fromArray( nodeDef.matrix ); + node.applyMatrix4( matrix ); + + } else { + + if ( nodeDef.translation !== undefined ) { + + node.position.fromArray( nodeDef.translation ); + + } + + if ( nodeDef.rotation !== undefined ) { + + node.quaternion.fromArray( nodeDef.rotation ); + + } + + if ( nodeDef.scale !== undefined ) { + + node.scale.fromArray( nodeDef.scale ); + + } + + } + + if ( ! parser.associations.has( node ) ) { + + parser.associations.set( node, {} ); + + } + + parser.associations.get( node ).nodes = nodeIndex; + + return node; + + } ); + + return this.nodeCache[ nodeIndex ]; + + } + + /** + * Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#scenes + * @param {number} sceneIndex + * @return {Promise} + */ + loadScene( sceneIndex ) { + + const extensions = this.extensions; + const sceneDef = this.json.scenes[ sceneIndex ]; + const parser = this; + + // Loader returns Group, not Scene. + // See: https://github.com/mrdoob/three.js/issues/18342#issuecomment-578981172 + const scene = new Group(); + if ( sceneDef.name ) scene.name = parser.createUniqueName( sceneDef.name ); + + assignExtrasToUserData( scene, sceneDef ); + + if ( sceneDef.extensions ) addUnknownExtensionsToUserData( extensions, scene, sceneDef ); + + const nodeIds = sceneDef.nodes || []; + + const pending = []; + + for ( let i = 0, il = nodeIds.length; i < il; i ++ ) { + + pending.push( parser.getDependency( 'node', nodeIds[ i ] ) ); + + } + + return Promise.all( pending ).then( function ( nodes ) { + + for ( let i = 0, il = nodes.length; i < il; i ++ ) { + + scene.add( nodes[ i ] ); + + } + + // Removes dangling associations, associations that reference a node that + // didn't make it into the scene. + const reduceAssociations = ( node ) => { + + const reducedAssociations = new Map(); + + for ( const [ key, value ] of parser.associations ) { + + if ( key instanceof Material || key instanceof Texture ) { + + reducedAssociations.set( key, value ); + + } + + } + + node.traverse( ( node ) => { + + const mappings = parser.associations.get( node ); + + if ( mappings != null ) { + + reducedAssociations.set( node, mappings ); + + } + + } ); + + return reducedAssociations; + + }; + + parser.associations = reduceAssociations( scene ); + + return scene; + + } ); + + } + + _createAnimationTracks( node, inputAccessor, outputAccessor, sampler, target ) { + + const tracks = []; + + const targetName = node.name ? node.name : node.uuid; + const targetNames = []; + + if ( PATH_PROPERTIES[ target.path ] === PATH_PROPERTIES.weights ) { + + node.traverse( function ( object ) { + + if ( object.morphTargetInfluences ) { + + targetNames.push( object.name ? object.name : object.uuid ); + + } + + } ); + + } else { + + targetNames.push( targetName ); + + } + + let TypedKeyframeTrack; + + switch ( PATH_PROPERTIES[ target.path ] ) { + + case PATH_PROPERTIES.weights: + + TypedKeyframeTrack = NumberKeyframeTrack; + break; + + case PATH_PROPERTIES.rotation: + + TypedKeyframeTrack = QuaternionKeyframeTrack; + break; + + case PATH_PROPERTIES.position: + case PATH_PROPERTIES.scale: + + TypedKeyframeTrack = VectorKeyframeTrack; + break; + + default: + + switch ( outputAccessor.itemSize ) { + + case 1: + TypedKeyframeTrack = NumberKeyframeTrack; + break; + case 2: + case 3: + default: + TypedKeyframeTrack = VectorKeyframeTrack; + break; + + } + + break; + + } + + const interpolation = sampler.interpolation !== undefined ? INTERPOLATION[ sampler.interpolation ] : InterpolateLinear; + + + const outputArray = this._getArrayFromAccessor( outputAccessor ); + + for ( let j = 0, jl = targetNames.length; j < jl; j ++ ) { + + const track = new TypedKeyframeTrack( + targetNames[ j ] + '.' + PATH_PROPERTIES[ target.path ], + inputAccessor.array, + outputArray, + interpolation + ); + + // Override interpolation with custom factory method. + if ( sampler.interpolation === 'CUBICSPLINE' ) { + + this._createCubicSplineTrackInterpolant( track ); + + } + + tracks.push( track ); + + } + + return tracks; + + } + + _getArrayFromAccessor( accessor ) { + + let outputArray = accessor.array; + + if ( accessor.normalized ) { + + const scale = getNormalizedComponentScale( outputArray.constructor ); + const scaled = new Float32Array( outputArray.length ); + + for ( let j = 0, jl = outputArray.length; j < jl; j ++ ) { + + scaled[ j ] = outputArray[ j ] * scale; + + } + + outputArray = scaled; + + } + + return outputArray; + + } + + _createCubicSplineTrackInterpolant( track ) { + + track.createInterpolant = function InterpolantFactoryMethodGLTFCubicSpline( result ) { + + // A CUBICSPLINE keyframe in glTF has three output values for each input value, + // representing inTangent, splineVertex, and outTangent. As a result, track.getValueSize() + // must be divided by three to get the interpolant's sampleSize argument. + + const interpolantType = ( this instanceof QuaternionKeyframeTrack ) ? GLTFCubicSplineQuaternionInterpolant : GLTFCubicSplineInterpolant; + + return new interpolantType( this.times, this.values, this.getValueSize() / 3, result ); + + }; + + // Mark as CUBICSPLINE. `track.getInterpolation()` doesn't support custom interpolants. + track.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline = true; + + } + +} + +/** + * @param {BufferGeometry} geometry + * @param {GLTF.Primitive} primitiveDef + * @param {GLTFParser} parser + */ +function computeBounds( geometry, primitiveDef, parser ) { + + const attributes = primitiveDef.attributes; + + const box = new Box3(); + + if ( attributes.POSITION !== undefined ) { + + const accessor = parser.json.accessors[ attributes.POSITION ]; + + const min = accessor.min; + const max = accessor.max; + + // glTF requires 'min' and 'max', but VRM (which extends glTF) currently ignores that requirement. + + if ( min !== undefined && max !== undefined ) { + + box.set( + new Vector3( min[ 0 ], min[ 1 ], min[ 2 ] ), + new Vector3( max[ 0 ], max[ 1 ], max[ 2 ] ) + ); + + if ( accessor.normalized ) { + + const boxScale = getNormalizedComponentScale( WEBGL_COMPONENT_TYPES[ accessor.componentType ] ); + box.min.multiplyScalar( boxScale ); + box.max.multiplyScalar( boxScale ); + + } + + } else { + + console.warn( 'THREE.GLTFLoader: Missing min/max properties for accessor POSITION.' ); + + return; + + } + + } else { + + return; + + } + + const targets = primitiveDef.targets; + + if ( targets !== undefined ) { + + const maxDisplacement = new Vector3(); + const vector = new Vector3(); + + for ( let i = 0, il = targets.length; i < il; i ++ ) { + + const target = targets[ i ]; + + if ( target.POSITION !== undefined ) { + + const accessor = parser.json.accessors[ target.POSITION ]; + const min = accessor.min; + const max = accessor.max; + + // glTF requires 'min' and 'max', but VRM (which extends glTF) currently ignores that requirement. + + if ( min !== undefined && max !== undefined ) { + + // we need to get max of absolute components because target weight is [-1,1] + vector.setX( Math.max( Math.abs( min[ 0 ] ), Math.abs( max[ 0 ] ) ) ); + vector.setY( Math.max( Math.abs( min[ 1 ] ), Math.abs( max[ 1 ] ) ) ); + vector.setZ( Math.max( Math.abs( min[ 2 ] ), Math.abs( max[ 2 ] ) ) ); + + + if ( accessor.normalized ) { + + const boxScale = getNormalizedComponentScale( WEBGL_COMPONENT_TYPES[ accessor.componentType ] ); + vector.multiplyScalar( boxScale ); + + } + + // Note: this assumes that the sum of all weights is at most 1. This isn't quite correct - it's more conservative + // to assume that each target can have a max weight of 1. However, for some use cases - notably, when morph targets + // are used to implement key-frame animations and as such only two are active at a time - this results in very large + // boxes. So for now we make a box that's sometimes a touch too small but is hopefully mostly of reasonable size. + maxDisplacement.max( vector ); + + } else { + + console.warn( 'THREE.GLTFLoader: Missing min/max properties for accessor POSITION.' ); + + } + + } + + } + + // As per comment above this box isn't conservative, but has a reasonable size for a very large number of morph targets. + box.expandByVector( maxDisplacement ); + + } + + geometry.boundingBox = box; + + const sphere = new Sphere(); + + box.getCenter( sphere.center ); + sphere.radius = box.min.distanceTo( box.max ) / 2; + + geometry.boundingSphere = sphere; + +} + +/** + * @param {BufferGeometry} geometry + * @param {GLTF.Primitive} primitiveDef + * @param {GLTFParser} parser + * @return {Promise} + */ +function addPrimitiveAttributes( geometry, primitiveDef, parser ) { + + const attributes = primitiveDef.attributes; + + const pending = []; + + function assignAttributeAccessor( accessorIndex, attributeName ) { + + return parser.getDependency( 'accessor', accessorIndex ) + .then( function ( accessor ) { + + geometry.setAttribute( attributeName, accessor ); + + } ); + + } + + for ( const gltfAttributeName in attributes ) { + + const threeAttributeName = ATTRIBUTES[ gltfAttributeName ] || gltfAttributeName.toLowerCase(); + + // Skip attributes already provided by e.g. Draco extension. + if ( threeAttributeName in geometry.attributes ) continue; + + pending.push( assignAttributeAccessor( attributes[ gltfAttributeName ], threeAttributeName ) ); + + } + + if ( primitiveDef.indices !== undefined && ! geometry.index ) { + + const accessor = parser.getDependency( 'accessor', primitiveDef.indices ).then( function ( accessor ) { + + geometry.setIndex( accessor ); + + } ); + + pending.push( accessor ); + + } + + if ( ColorManagement.workingColorSpace !== LinearSRGBColorSpace && 'COLOR_0' in attributes ) { + + console.warn( `THREE.GLTFLoader: Converting vertex colors from "srgb-linear" to "${ColorManagement.workingColorSpace}" not supported.` ); + + } + + assignExtrasToUserData( geometry, primitiveDef ); + + computeBounds( geometry, primitiveDef, parser ); + + return Promise.all( pending ).then( function () { + + return primitiveDef.targets !== undefined + ? addMorphTargets( geometry, primitiveDef.targets, parser ) + : geometry; + + } ); + +} + +export { GLTFLoader }; diff --git a/src/box-winUI/Assets/home-globe/vendor/OrbitControls.js b/src/box-winUI/Assets/home-globe/vendor/OrbitControls.js new file mode 100644 index 0000000..d21c1eb --- /dev/null +++ b/src/box-winUI/Assets/home-globe/vendor/OrbitControls.js @@ -0,0 +1,1532 @@ +import { + EventDispatcher, + MOUSE, + Quaternion, + Spherical, + TOUCH, + Vector2, + Vector3, + Plane, + Ray, + MathUtils +} from 'three'; + +// OrbitControls performs orbiting, dollying (zooming), and panning. +// Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default). +// +// Orbit - left mouse / touch: one-finger move +// Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish +// Pan - right mouse, or left mouse + ctrl/meta/shiftKey, or arrow keys / touch: two-finger move + +const _changeEvent = { type: 'change' }; +const _startEvent = { type: 'start' }; +const _endEvent = { type: 'end' }; +const _ray = new Ray(); +const _plane = new Plane(); +const TILT_LIMIT = Math.cos( 70 * MathUtils.DEG2RAD ); + +class OrbitControls extends EventDispatcher { + + constructor( object, domElement ) { + + super(); + + this.object = object; + this.domElement = domElement; + this.domElement.style.touchAction = 'none'; // disable touch scroll + + // Set to false to disable this control + this.enabled = true; + + // "target" sets the location of focus, where the object orbits around + this.target = new Vector3(); + + // Sets the 3D cursor (similar to Blender), from which the maxTargetRadius takes effect + this.cursor = new Vector3(); + + // How far you can dolly in and out ( PerspectiveCamera only ) + this.minDistance = 0; + this.maxDistance = Infinity; + + // How far you can zoom in and out ( OrthographicCamera only ) + this.minZoom = 0; + this.maxZoom = Infinity; + + // Limit camera target within a spherical area around the cursor + this.minTargetRadius = 0; + this.maxTargetRadius = Infinity; + + // How far you can orbit vertically, upper and lower limits. + // Range is 0 to Math.PI radians. + this.minPolarAngle = 0; // radians + this.maxPolarAngle = Math.PI; // radians + + // How far you can orbit horizontally, upper and lower limits. + // If set, the interval [ min, max ] must be a sub-interval of [ - 2 PI, 2 PI ], with ( max - min < 2 PI ) + this.minAzimuthAngle = - Infinity; // radians + this.maxAzimuthAngle = Infinity; // radians + + // Set to true to enable damping (inertia) + // If damping is enabled, you must call controls.update() in your animation loop + this.enableDamping = false; + this.dampingFactor = 0.05; + + // This option actually enables dollying in and out; left as "zoom" for backwards compatibility. + // Set to false to disable zooming + this.enableZoom = true; + this.zoomSpeed = 1.0; + + // Set to false to disable rotating + this.enableRotate = true; + this.rotateSpeed = 1.0; + + // Set to false to disable panning + this.enablePan = true; + this.panSpeed = 1.0; + this.screenSpacePanning = true; // if false, pan orthogonal to world-space direction camera.up + this.keyPanSpeed = 7.0; // pixels moved per arrow key push + this.zoomToCursor = false; + + // Set to true to automatically rotate around the target + // If auto-rotate is enabled, you must call controls.update() in your animation loop + this.autoRotate = false; + this.autoRotateSpeed = 2.0; // 30 seconds per orbit when fps is 60 + + // The four arrow keys + this.keys = { LEFT: 'ArrowLeft', UP: 'ArrowUp', RIGHT: 'ArrowRight', BOTTOM: 'ArrowDown' }; + + // Mouse buttons + this.mouseButtons = { LEFT: MOUSE.ROTATE, MIDDLE: MOUSE.DOLLY, RIGHT: MOUSE.PAN }; + + // Touch fingers + this.touches = { ONE: TOUCH.ROTATE, TWO: TOUCH.DOLLY_PAN }; + + // for reset + this.target0 = this.target.clone(); + this.position0 = this.object.position.clone(); + this.zoom0 = this.object.zoom; + + // the target DOM element for key events + this._domElementKeyEvents = null; + + // + // public methods + // + + this.getPolarAngle = function () { + + return spherical.phi; + + }; + + this.getAzimuthalAngle = function () { + + return spherical.theta; + + }; + + this.getDistance = function () { + + return this.object.position.distanceTo( this.target ); + + }; + + this.listenToKeyEvents = function ( domElement ) { + + domElement.addEventListener( 'keydown', onKeyDown ); + this._domElementKeyEvents = domElement; + + }; + + this.stopListenToKeyEvents = function () { + + this._domElementKeyEvents.removeEventListener( 'keydown', onKeyDown ); + this._domElementKeyEvents = null; + + }; + + this.saveState = function () { + + scope.target0.copy( scope.target ); + scope.position0.copy( scope.object.position ); + scope.zoom0 = scope.object.zoom; + + }; + + this.reset = function () { + + scope.target.copy( scope.target0 ); + scope.object.position.copy( scope.position0 ); + scope.object.zoom = scope.zoom0; + + scope.object.updateProjectionMatrix(); + scope.dispatchEvent( _changeEvent ); + + scope.update(); + + state = STATE.NONE; + + }; + + // this method is exposed, but perhaps it would be better if we can make it private... + this.update = function () { + + const offset = new Vector3(); + + // so camera.up is the orbit axis + const quat = new Quaternion().setFromUnitVectors( object.up, new Vector3( 0, 1, 0 ) ); + const quatInverse = quat.clone().invert(); + + const lastPosition = new Vector3(); + const lastQuaternion = new Quaternion(); + const lastTargetPosition = new Vector3(); + + const twoPI = 2 * Math.PI; + + return function update( deltaTime = null ) { + + const position = scope.object.position; + + offset.copy( position ).sub( scope.target ); + + // rotate offset to "y-axis-is-up" space + offset.applyQuaternion( quat ); + + // angle from z-axis around y-axis + spherical.setFromVector3( offset ); + + if ( scope.autoRotate && state === STATE.NONE ) { + + rotateLeft( getAutoRotationAngle( deltaTime ) ); + + } + + if ( scope.enableDamping ) { + + spherical.theta += sphericalDelta.theta * scope.dampingFactor; + spherical.phi += sphericalDelta.phi * scope.dampingFactor; + + } else { + + spherical.theta += sphericalDelta.theta; + spherical.phi += sphericalDelta.phi; + + } + + // restrict theta to be between desired limits + + let min = scope.minAzimuthAngle; + let max = scope.maxAzimuthAngle; + + if ( isFinite( min ) && isFinite( max ) ) { + + if ( min < - Math.PI ) min += twoPI; else if ( min > Math.PI ) min -= twoPI; + + if ( max < - Math.PI ) max += twoPI; else if ( max > Math.PI ) max -= twoPI; + + if ( min <= max ) { + + spherical.theta = Math.max( min, Math.min( max, spherical.theta ) ); + + } else { + + spherical.theta = ( spherical.theta > ( min + max ) / 2 ) ? + Math.max( min, spherical.theta ) : + Math.min( max, spherical.theta ); + + } + + } + + // restrict phi to be between desired limits + spherical.phi = Math.max( scope.minPolarAngle, Math.min( scope.maxPolarAngle, spherical.phi ) ); + + spherical.makeSafe(); + + + // move target to panned location + + if ( scope.enableDamping === true ) { + + scope.target.addScaledVector( panOffset, scope.dampingFactor ); + + } else { + + scope.target.add( panOffset ); + + } + + // Limit the target distance from the cursor to create a sphere around the center of interest + scope.target.sub( scope.cursor ); + scope.target.clampLength( scope.minTargetRadius, scope.maxTargetRadius ); + scope.target.add( scope.cursor ); + + let zoomChanged = false; + // adjust the camera position based on zoom only if we're not zooming to the cursor or if it's an ortho camera + // we adjust zoom later in these cases + if ( scope.zoomToCursor && performCursorZoom || scope.object.isOrthographicCamera ) { + + spherical.radius = clampDistance( spherical.radius ); + + } else { + + const prevRadius = spherical.radius; + spherical.radius = clampDistance( spherical.radius * scale ); + zoomChanged = prevRadius != spherical.radius; + + } + + offset.setFromSpherical( spherical ); + + // rotate offset back to "camera-up-vector-is-up" space + offset.applyQuaternion( quatInverse ); + + position.copy( scope.target ).add( offset ); + + scope.object.lookAt( scope.target ); + + if ( scope.enableDamping === true ) { + + sphericalDelta.theta *= ( 1 - scope.dampingFactor ); + sphericalDelta.phi *= ( 1 - scope.dampingFactor ); + + panOffset.multiplyScalar( 1 - scope.dampingFactor ); + + } else { + + sphericalDelta.set( 0, 0, 0 ); + + panOffset.set( 0, 0, 0 ); + + } + + // adjust camera position + if ( scope.zoomToCursor && performCursorZoom ) { + + let newRadius = null; + if ( scope.object.isPerspectiveCamera ) { + + // move the camera down the pointer ray + // this method avoids floating point error + const prevRadius = offset.length(); + newRadius = clampDistance( prevRadius * scale ); + + const radiusDelta = prevRadius - newRadius; + scope.object.position.addScaledVector( dollyDirection, radiusDelta ); + scope.object.updateMatrixWorld(); + + zoomChanged = !! radiusDelta; + + } else if ( scope.object.isOrthographicCamera ) { + + // adjust the ortho camera position based on zoom changes + const mouseBefore = new Vector3( mouse.x, mouse.y, 0 ); + mouseBefore.unproject( scope.object ); + + const prevZoom = scope.object.zoom; + scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / scale ) ); + scope.object.updateProjectionMatrix(); + + zoomChanged = prevZoom !== scope.object.zoom; + + const mouseAfter = new Vector3( mouse.x, mouse.y, 0 ); + mouseAfter.unproject( scope.object ); + + scope.object.position.sub( mouseAfter ).add( mouseBefore ); + scope.object.updateMatrixWorld(); + + newRadius = offset.length(); + + } else { + + console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - zoom to cursor disabled.' ); + scope.zoomToCursor = false; + + } + + // handle the placement of the target + if ( newRadius !== null ) { + + if ( this.screenSpacePanning ) { + + // position the orbit target in front of the new camera position + scope.target.set( 0, 0, - 1 ) + .transformDirection( scope.object.matrix ) + .multiplyScalar( newRadius ) + .add( scope.object.position ); + + } else { + + // get the ray and translation plane to compute target + _ray.origin.copy( scope.object.position ); + _ray.direction.set( 0, 0, - 1 ).transformDirection( scope.object.matrix ); + + // if the camera is 20 degrees above the horizon then don't adjust the focus target to avoid + // extremely large values + if ( Math.abs( scope.object.up.dot( _ray.direction ) ) < TILT_LIMIT ) { + + object.lookAt( scope.target ); + + } else { + + _plane.setFromNormalAndCoplanarPoint( scope.object.up, scope.target ); + _ray.intersectPlane( _plane, scope.target ); + + } + + } + + } + + } else if ( scope.object.isOrthographicCamera ) { + + const prevZoom = scope.object.zoom; + scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / scale ) ); + + if ( prevZoom !== scope.object.zoom ) { + + scope.object.updateProjectionMatrix(); + zoomChanged = true; + + } + + } + + scale = 1; + performCursorZoom = false; + + // update condition is: + // min(camera displacement, camera rotation in radians)^2 > EPS + // using small-angle approximation cos(x/2) = 1 - x^2 / 8 + + if ( zoomChanged || + lastPosition.distanceToSquared( scope.object.position ) > EPS || + 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS || + lastTargetPosition.distanceToSquared( scope.target ) > EPS ) { + + scope.dispatchEvent( _changeEvent ); + + lastPosition.copy( scope.object.position ); + lastQuaternion.copy( scope.object.quaternion ); + lastTargetPosition.copy( scope.target ); + + return true; + + } + + return false; + + }; + + }(); + + this.dispose = function () { + + scope.domElement.removeEventListener( 'contextmenu', onContextMenu ); + + scope.domElement.removeEventListener( 'pointerdown', onPointerDown ); + scope.domElement.removeEventListener( 'pointercancel', onPointerUp ); + scope.domElement.removeEventListener( 'wheel', onMouseWheel ); + + scope.domElement.removeEventListener( 'pointermove', onPointerMove ); + scope.domElement.removeEventListener( 'pointerup', onPointerUp ); + + const document = scope.domElement.getRootNode(); // offscreen canvas compatibility + + document.removeEventListener( 'keydown', interceptControlDown, { capture: true } ); + + if ( scope._domElementKeyEvents !== null ) { + + scope._domElementKeyEvents.removeEventListener( 'keydown', onKeyDown ); + scope._domElementKeyEvents = null; + + } + + //scope.dispatchEvent( { type: 'dispose' } ); // should this be added here? + + }; + + // + // internals + // + + const scope = this; + + const STATE = { + NONE: - 1, + ROTATE: 0, + DOLLY: 1, + PAN: 2, + TOUCH_ROTATE: 3, + TOUCH_PAN: 4, + TOUCH_DOLLY_PAN: 5, + TOUCH_DOLLY_ROTATE: 6 + }; + + let state = STATE.NONE; + + const EPS = 0.000001; + + // current position in spherical coordinates + const spherical = new Spherical(); + const sphericalDelta = new Spherical(); + + let scale = 1; + const panOffset = new Vector3(); + + const rotateStart = new Vector2(); + const rotateEnd = new Vector2(); + const rotateDelta = new Vector2(); + + const panStart = new Vector2(); + const panEnd = new Vector2(); + const panDelta = new Vector2(); + + const dollyStart = new Vector2(); + const dollyEnd = new Vector2(); + const dollyDelta = new Vector2(); + + const dollyDirection = new Vector3(); + const mouse = new Vector2(); + let performCursorZoom = false; + + const pointers = []; + const pointerPositions = {}; + + let controlActive = false; + + function getAutoRotationAngle( deltaTime ) { + + if ( deltaTime !== null ) { + + return ( 2 * Math.PI / 60 * scope.autoRotateSpeed ) * deltaTime; + + } else { + + return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed; + + } + + } + + function getZoomScale( delta ) { + + const normalizedDelta = Math.abs( delta * 0.01 ); + return Math.pow( 0.95, scope.zoomSpeed * normalizedDelta ); + + } + + function rotateLeft( angle ) { + + sphericalDelta.theta -= angle; + + } + + function rotateUp( angle ) { + + sphericalDelta.phi -= angle; + + } + + const panLeft = function () { + + const v = new Vector3(); + + return function panLeft( distance, objectMatrix ) { + + v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix + v.multiplyScalar( - distance ); + + panOffset.add( v ); + + }; + + }(); + + const panUp = function () { + + const v = new Vector3(); + + return function panUp( distance, objectMatrix ) { + + if ( scope.screenSpacePanning === true ) { + + v.setFromMatrixColumn( objectMatrix, 1 ); + + } else { + + v.setFromMatrixColumn( objectMatrix, 0 ); + v.crossVectors( scope.object.up, v ); + + } + + v.multiplyScalar( distance ); + + panOffset.add( v ); + + }; + + }(); + + // deltaX and deltaY are in pixels; right and down are positive + const pan = function () { + + const offset = new Vector3(); + + return function pan( deltaX, deltaY ) { + + const element = scope.domElement; + + if ( scope.object.isPerspectiveCamera ) { + + // perspective + const position = scope.object.position; + offset.copy( position ).sub( scope.target ); + let targetDistance = offset.length(); + + // half of the fov is center to top of screen + targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 ); + + // we use only clientHeight here so aspect ratio does not distort speed + panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix ); + panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix ); + + } else if ( scope.object.isOrthographicCamera ) { + + // orthographic + panLeft( deltaX * ( scope.object.right - scope.object.left ) / scope.object.zoom / element.clientWidth, scope.object.matrix ); + panUp( deltaY * ( scope.object.top - scope.object.bottom ) / scope.object.zoom / element.clientHeight, scope.object.matrix ); + + } else { + + // camera neither orthographic nor perspective + console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' ); + scope.enablePan = false; + + } + + }; + + }(); + + function dollyOut( dollyScale ) { + + if ( scope.object.isPerspectiveCamera || scope.object.isOrthographicCamera ) { + + scale /= dollyScale; + + } else { + + console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' ); + scope.enableZoom = false; + + } + + } + + function dollyIn( dollyScale ) { + + if ( scope.object.isPerspectiveCamera || scope.object.isOrthographicCamera ) { + + scale *= dollyScale; + + } else { + + console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' ); + scope.enableZoom = false; + + } + + } + + function updateZoomParameters( x, y ) { + + if ( ! scope.zoomToCursor ) { + + return; + + } + + performCursorZoom = true; + + const rect = scope.domElement.getBoundingClientRect(); + const dx = x - rect.left; + const dy = y - rect.top; + const w = rect.width; + const h = rect.height; + + mouse.x = ( dx / w ) * 2 - 1; + mouse.y = - ( dy / h ) * 2 + 1; + + dollyDirection.set( mouse.x, mouse.y, 1 ).unproject( scope.object ).sub( scope.object.position ).normalize(); + + } + + function clampDistance( dist ) { + + return Math.max( scope.minDistance, Math.min( scope.maxDistance, dist ) ); + + } + + // + // event callbacks - update the object state + // + + function handleMouseDownRotate( event ) { + + rotateStart.set( event.clientX, event.clientY ); + + } + + function handleMouseDownDolly( event ) { + + updateZoomParameters( event.clientX, event.clientX ); + dollyStart.set( event.clientX, event.clientY ); + + } + + function handleMouseDownPan( event ) { + + panStart.set( event.clientX, event.clientY ); + + } + + function handleMouseMoveRotate( event ) { + + rotateEnd.set( event.clientX, event.clientY ); + + rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed ); + + const element = scope.domElement; + + rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height + + rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight ); + + rotateStart.copy( rotateEnd ); + + scope.update(); + + } + + function handleMouseMoveDolly( event ) { + + dollyEnd.set( event.clientX, event.clientY ); + + dollyDelta.subVectors( dollyEnd, dollyStart ); + + if ( dollyDelta.y > 0 ) { + + dollyOut( getZoomScale( dollyDelta.y ) ); + + } else if ( dollyDelta.y < 0 ) { + + dollyIn( getZoomScale( dollyDelta.y ) ); + + } + + dollyStart.copy( dollyEnd ); + + scope.update(); + + } + + function handleMouseMovePan( event ) { + + panEnd.set( event.clientX, event.clientY ); + + panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed ); + + pan( panDelta.x, panDelta.y ); + + panStart.copy( panEnd ); + + scope.update(); + + } + + function handleMouseWheel( event ) { + + updateZoomParameters( event.clientX, event.clientY ); + + if ( event.deltaY < 0 ) { + + dollyIn( getZoomScale( event.deltaY ) ); + + } else if ( event.deltaY > 0 ) { + + dollyOut( getZoomScale( event.deltaY ) ); + + } + + scope.update(); + + } + + function handleKeyDown( event ) { + + let needsUpdate = false; + + switch ( event.code ) { + + case scope.keys.UP: + + if ( event.ctrlKey || event.metaKey || event.shiftKey ) { + + rotateUp( 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight ); + + } else { + + pan( 0, scope.keyPanSpeed ); + + } + + needsUpdate = true; + break; + + case scope.keys.BOTTOM: + + if ( event.ctrlKey || event.metaKey || event.shiftKey ) { + + rotateUp( - 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight ); + + } else { + + pan( 0, - scope.keyPanSpeed ); + + } + + needsUpdate = true; + break; + + case scope.keys.LEFT: + + if ( event.ctrlKey || event.metaKey || event.shiftKey ) { + + rotateLeft( 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight ); + + } else { + + pan( scope.keyPanSpeed, 0 ); + + } + + needsUpdate = true; + break; + + case scope.keys.RIGHT: + + if ( event.ctrlKey || event.metaKey || event.shiftKey ) { + + rotateLeft( - 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight ); + + } else { + + pan( - scope.keyPanSpeed, 0 ); + + } + + needsUpdate = true; + break; + + } + + if ( needsUpdate ) { + + // prevent the browser from scrolling on cursor keys + event.preventDefault(); + + scope.update(); + + } + + + } + + function handleTouchStartRotate( event ) { + + if ( pointers.length === 1 ) { + + rotateStart.set( event.pageX, event.pageY ); + + } else { + + const position = getSecondPointerPosition( event ); + + const x = 0.5 * ( event.pageX + position.x ); + const y = 0.5 * ( event.pageY + position.y ); + + rotateStart.set( x, y ); + + } + + } + + function handleTouchStartPan( event ) { + + if ( pointers.length === 1 ) { + + panStart.set( event.pageX, event.pageY ); + + } else { + + const position = getSecondPointerPosition( event ); + + const x = 0.5 * ( event.pageX + position.x ); + const y = 0.5 * ( event.pageY + position.y ); + + panStart.set( x, y ); + + } + + } + + function handleTouchStartDolly( event ) { + + const position = getSecondPointerPosition( event ); + + const dx = event.pageX - position.x; + const dy = event.pageY - position.y; + + const distance = Math.sqrt( dx * dx + dy * dy ); + + dollyStart.set( 0, distance ); + + } + + function handleTouchStartDollyPan( event ) { + + if ( scope.enableZoom ) handleTouchStartDolly( event ); + + if ( scope.enablePan ) handleTouchStartPan( event ); + + } + + function handleTouchStartDollyRotate( event ) { + + if ( scope.enableZoom ) handleTouchStartDolly( event ); + + if ( scope.enableRotate ) handleTouchStartRotate( event ); + + } + + function handleTouchMoveRotate( event ) { + + if ( pointers.length == 1 ) { + + rotateEnd.set( event.pageX, event.pageY ); + + } else { + + const position = getSecondPointerPosition( event ); + + const x = 0.5 * ( event.pageX + position.x ); + const y = 0.5 * ( event.pageY + position.y ); + + rotateEnd.set( x, y ); + + } + + rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed ); + + const element = scope.domElement; + + rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height + + rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight ); + + rotateStart.copy( rotateEnd ); + + } + + function handleTouchMovePan( event ) { + + if ( pointers.length === 1 ) { + + panEnd.set( event.pageX, event.pageY ); + + } else { + + const position = getSecondPointerPosition( event ); + + const x = 0.5 * ( event.pageX + position.x ); + const y = 0.5 * ( event.pageY + position.y ); + + panEnd.set( x, y ); + + } + + panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed ); + + pan( panDelta.x, panDelta.y ); + + panStart.copy( panEnd ); + + } + + function handleTouchMoveDolly( event ) { + + const position = getSecondPointerPosition( event ); + + const dx = event.pageX - position.x; + const dy = event.pageY - position.y; + + const distance = Math.sqrt( dx * dx + dy * dy ); + + dollyEnd.set( 0, distance ); + + dollyDelta.set( 0, Math.pow( dollyEnd.y / dollyStart.y, scope.zoomSpeed ) ); + + dollyOut( dollyDelta.y ); + + dollyStart.copy( dollyEnd ); + + const centerX = ( event.pageX + position.x ) * 0.5; + const centerY = ( event.pageY + position.y ) * 0.5; + + updateZoomParameters( centerX, centerY ); + + } + + function handleTouchMoveDollyPan( event ) { + + if ( scope.enableZoom ) handleTouchMoveDolly( event ); + + if ( scope.enablePan ) handleTouchMovePan( event ); + + } + + function handleTouchMoveDollyRotate( event ) { + + if ( scope.enableZoom ) handleTouchMoveDolly( event ); + + if ( scope.enableRotate ) handleTouchMoveRotate( event ); + + } + + // + // event handlers - FSM: listen for events and reset state + // + + function onPointerDown( event ) { + + if ( scope.enabled === false ) return; + + if ( pointers.length === 0 ) { + + scope.domElement.setPointerCapture( event.pointerId ); + + scope.domElement.addEventListener( 'pointermove', onPointerMove ); + scope.domElement.addEventListener( 'pointerup', onPointerUp ); + + } + + // + + if ( isTrackingPointer( event ) ) return; + + // + + addPointer( event ); + + if ( event.pointerType === 'touch' ) { + + onTouchStart( event ); + + } else { + + onMouseDown( event ); + + } + + } + + function onPointerMove( event ) { + + if ( scope.enabled === false ) return; + + if ( event.pointerType === 'touch' ) { + + onTouchMove( event ); + + } else { + + onMouseMove( event ); + + } + + } + + function onPointerUp( event ) { + + removePointer( event ); + + switch ( pointers.length ) { + + case 0: + + scope.domElement.releasePointerCapture( event.pointerId ); + + scope.domElement.removeEventListener( 'pointermove', onPointerMove ); + scope.domElement.removeEventListener( 'pointerup', onPointerUp ); + + scope.dispatchEvent( _endEvent ); + + state = STATE.NONE; + + break; + + case 1: + + const pointerId = pointers[ 0 ]; + const position = pointerPositions[ pointerId ]; + + // minimal placeholder event - allows state correction on pointer-up + onTouchStart( { pointerId: pointerId, pageX: position.x, pageY: position.y } ); + + break; + + } + + } + + function onMouseDown( event ) { + + let mouseAction; + + switch ( event.button ) { + + case 0: + + mouseAction = scope.mouseButtons.LEFT; + break; + + case 1: + + mouseAction = scope.mouseButtons.MIDDLE; + break; + + case 2: + + mouseAction = scope.mouseButtons.RIGHT; + break; + + default: + + mouseAction = - 1; + + } + + switch ( mouseAction ) { + + case MOUSE.DOLLY: + + if ( scope.enableZoom === false ) return; + + handleMouseDownDolly( event ); + + state = STATE.DOLLY; + + break; + + case MOUSE.ROTATE: + + if ( event.ctrlKey || event.metaKey || event.shiftKey ) { + + if ( scope.enablePan === false ) return; + + handleMouseDownPan( event ); + + state = STATE.PAN; + + } else { + + if ( scope.enableRotate === false ) return; + + handleMouseDownRotate( event ); + + state = STATE.ROTATE; + + } + + break; + + case MOUSE.PAN: + + if ( event.ctrlKey || event.metaKey || event.shiftKey ) { + + if ( scope.enableRotate === false ) return; + + handleMouseDownRotate( event ); + + state = STATE.ROTATE; + + } else { + + if ( scope.enablePan === false ) return; + + handleMouseDownPan( event ); + + state = STATE.PAN; + + } + + break; + + default: + + state = STATE.NONE; + + } + + if ( state !== STATE.NONE ) { + + scope.dispatchEvent( _startEvent ); + + } + + } + + function onMouseMove( event ) { + + switch ( state ) { + + case STATE.ROTATE: + + if ( scope.enableRotate === false ) return; + + handleMouseMoveRotate( event ); + + break; + + case STATE.DOLLY: + + if ( scope.enableZoom === false ) return; + + handleMouseMoveDolly( event ); + + break; + + case STATE.PAN: + + if ( scope.enablePan === false ) return; + + handleMouseMovePan( event ); + + break; + + } + + } + + function onMouseWheel( event ) { + + if ( scope.enabled === false || scope.enableZoom === false || state !== STATE.NONE ) return; + + event.preventDefault(); + + scope.dispatchEvent( _startEvent ); + + handleMouseWheel( customWheelEvent( event ) ); + + scope.dispatchEvent( _endEvent ); + + } + + function customWheelEvent( event ) { + + const mode = event.deltaMode; + + // minimal wheel event altered to meet delta-zoom demand + const newEvent = { + clientX: event.clientX, + clientY: event.clientY, + deltaY: event.deltaY, + }; + + switch ( mode ) { + + case 1: // LINE_MODE + newEvent.deltaY *= 16; + break; + + case 2: // PAGE_MODE + newEvent.deltaY *= 100; + break; + + } + + // detect if event was triggered by pinching + if ( event.ctrlKey && ! controlActive ) { + + newEvent.deltaY *= 10; + + } + + return newEvent; + + } + + function interceptControlDown( event ) { + + if ( event.key === 'Control' ) { + + controlActive = true; + + + const document = scope.domElement.getRootNode(); // offscreen canvas compatibility + + document.addEventListener( 'keyup', interceptControlUp, { passive: true, capture: true } ); + + } + + } + + function interceptControlUp( event ) { + + if ( event.key === 'Control' ) { + + controlActive = false; + + + const document = scope.domElement.getRootNode(); // offscreen canvas compatibility + + document.removeEventListener( 'keyup', interceptControlUp, { passive: true, capture: true } ); + + } + + } + + function onKeyDown( event ) { + + if ( scope.enabled === false || scope.enablePan === false ) return; + + handleKeyDown( event ); + + } + + function onTouchStart( event ) { + + trackPointer( event ); + + switch ( pointers.length ) { + + case 1: + + switch ( scope.touches.ONE ) { + + case TOUCH.ROTATE: + + if ( scope.enableRotate === false ) return; + + handleTouchStartRotate( event ); + + state = STATE.TOUCH_ROTATE; + + break; + + case TOUCH.PAN: + + if ( scope.enablePan === false ) return; + + handleTouchStartPan( event ); + + state = STATE.TOUCH_PAN; + + break; + + default: + + state = STATE.NONE; + + } + + break; + + case 2: + + switch ( scope.touches.TWO ) { + + case TOUCH.DOLLY_PAN: + + if ( scope.enableZoom === false && scope.enablePan === false ) return; + + handleTouchStartDollyPan( event ); + + state = STATE.TOUCH_DOLLY_PAN; + + break; + + case TOUCH.DOLLY_ROTATE: + + if ( scope.enableZoom === false && scope.enableRotate === false ) return; + + handleTouchStartDollyRotate( event ); + + state = STATE.TOUCH_DOLLY_ROTATE; + + break; + + default: + + state = STATE.NONE; + + } + + break; + + default: + + state = STATE.NONE; + + } + + if ( state !== STATE.NONE ) { + + scope.dispatchEvent( _startEvent ); + + } + + } + + function onTouchMove( event ) { + + trackPointer( event ); + + switch ( state ) { + + case STATE.TOUCH_ROTATE: + + if ( scope.enableRotate === false ) return; + + handleTouchMoveRotate( event ); + + scope.update(); + + break; + + case STATE.TOUCH_PAN: + + if ( scope.enablePan === false ) return; + + handleTouchMovePan( event ); + + scope.update(); + + break; + + case STATE.TOUCH_DOLLY_PAN: + + if ( scope.enableZoom === false && scope.enablePan === false ) return; + + handleTouchMoveDollyPan( event ); + + scope.update(); + + break; + + case STATE.TOUCH_DOLLY_ROTATE: + + if ( scope.enableZoom === false && scope.enableRotate === false ) return; + + handleTouchMoveDollyRotate( event ); + + scope.update(); + + break; + + default: + + state = STATE.NONE; + + } + + } + + function onContextMenu( event ) { + + if ( scope.enabled === false ) return; + + event.preventDefault(); + + } + + function addPointer( event ) { + + pointers.push( event.pointerId ); + + } + + function removePointer( event ) { + + delete pointerPositions[ event.pointerId ]; + + for ( let i = 0; i < pointers.length; i ++ ) { + + if ( pointers[ i ] == event.pointerId ) { + + pointers.splice( i, 1 ); + return; + + } + + } + + } + + function isTrackingPointer( event ) { + + for ( let i = 0; i < pointers.length; i ++ ) { + + if ( pointers[ i ] == event.pointerId ) return true; + + } + + return false; + + } + + function trackPointer( event ) { + + let position = pointerPositions[ event.pointerId ]; + + if ( position === undefined ) { + + position = new Vector2(); + pointerPositions[ event.pointerId ] = position; + + } + + position.set( event.pageX, event.pageY ); + + } + + function getSecondPointerPosition( event ) { + + const pointerId = ( event.pointerId === pointers[ 0 ] ) ? pointers[ 1 ] : pointers[ 0 ]; + + return pointerPositions[ pointerId ]; + + } + + // + + scope.domElement.addEventListener( 'contextmenu', onContextMenu ); + + scope.domElement.addEventListener( 'pointerdown', onPointerDown ); + scope.domElement.addEventListener( 'pointercancel', onPointerUp ); + scope.domElement.addEventListener( 'wheel', onMouseWheel, { passive: false } ); + + const document = scope.domElement.getRootNode(); // offscreen canvas compatibility + + document.addEventListener( 'keydown', interceptControlDown, { passive: true, capture: true } ); + + // force an update at start + + this.update(); + + } + +} + +export { OrbitControls }; diff --git a/src/box-winUI/Assets/home-globe/vendor/RoomEnvironment.js b/src/box-winUI/Assets/home-globe/vendor/RoomEnvironment.js new file mode 100644 index 0000000..f51f72b --- /dev/null +++ b/src/box-winUI/Assets/home-globe/vendor/RoomEnvironment.js @@ -0,0 +1,148 @@ +/** + * https://github.com/google/model-viewer/blob/master/packages/model-viewer/src/three-components/EnvironmentScene.ts + */ + +import { + BackSide, + BoxGeometry, + Mesh, + MeshBasicMaterial, + MeshStandardMaterial, + PointLight, + Scene, +} from 'three'; + +class RoomEnvironment extends Scene { + + constructor( renderer = null ) { + + super(); + + const geometry = new BoxGeometry(); + geometry.deleteAttribute( 'uv' ); + + const roomMaterial = new MeshStandardMaterial( { side: BackSide } ); + const boxMaterial = new MeshStandardMaterial(); + + let intensity = 5; + + if ( renderer !== null && renderer._useLegacyLights === false ) intensity = 900; + + const mainLight = new PointLight( 0xffffff, intensity, 28, 2 ); + mainLight.position.set( 0.418, 16.199, 0.300 ); + this.add( mainLight ); + + const room = new Mesh( geometry, roomMaterial ); + room.position.set( - 0.757, 13.219, 0.717 ); + room.scale.set( 31.713, 28.305, 28.591 ); + this.add( room ); + + const box1 = new Mesh( geometry, boxMaterial ); + box1.position.set( - 10.906, 2.009, 1.846 ); + box1.rotation.set( 0, - 0.195, 0 ); + box1.scale.set( 2.328, 7.905, 4.651 ); + this.add( box1 ); + + const box2 = new Mesh( geometry, boxMaterial ); + box2.position.set( - 5.607, - 0.754, - 0.758 ); + box2.rotation.set( 0, 0.994, 0 ); + box2.scale.set( 1.970, 1.534, 3.955 ); + this.add( box2 ); + + const box3 = new Mesh( geometry, boxMaterial ); + box3.position.set( 6.167, 0.857, 7.803 ); + box3.rotation.set( 0, 0.561, 0 ); + box3.scale.set( 3.927, 6.285, 3.687 ); + this.add( box3 ); + + const box4 = new Mesh( geometry, boxMaterial ); + box4.position.set( - 2.017, 0.018, 6.124 ); + box4.rotation.set( 0, 0.333, 0 ); + box4.scale.set( 2.002, 4.566, 2.064 ); + this.add( box4 ); + + const box5 = new Mesh( geometry, boxMaterial ); + box5.position.set( 2.291, - 0.756, - 2.621 ); + box5.rotation.set( 0, - 0.286, 0 ); + box5.scale.set( 1.546, 1.552, 1.496 ); + this.add( box5 ); + + const box6 = new Mesh( geometry, boxMaterial ); + box6.position.set( - 2.193, - 0.369, - 5.547 ); + box6.rotation.set( 0, 0.516, 0 ); + box6.scale.set( 3.875, 3.487, 2.986 ); + this.add( box6 ); + + + // -x right + const light1 = new Mesh( geometry, createAreaLightMaterial( 50 ) ); + light1.position.set( - 16.116, 14.37, 8.208 ); + light1.scale.set( 0.1, 2.428, 2.739 ); + this.add( light1 ); + + // -x left + const light2 = new Mesh( geometry, createAreaLightMaterial( 50 ) ); + light2.position.set( - 16.109, 18.021, - 8.207 ); + light2.scale.set( 0.1, 2.425, 2.751 ); + this.add( light2 ); + + // +x + const light3 = new Mesh( geometry, createAreaLightMaterial( 17 ) ); + light3.position.set( 14.904, 12.198, - 1.832 ); + light3.scale.set( 0.15, 4.265, 6.331 ); + this.add( light3 ); + + // +z + const light4 = new Mesh( geometry, createAreaLightMaterial( 43 ) ); + light4.position.set( - 0.462, 8.89, 14.520 ); + light4.scale.set( 4.38, 5.441, 0.088 ); + this.add( light4 ); + + // -z + const light5 = new Mesh( geometry, createAreaLightMaterial( 20 ) ); + light5.position.set( 3.235, 11.486, - 12.541 ); + light5.scale.set( 2.5, 2.0, 0.1 ); + this.add( light5 ); + + // +y + const light6 = new Mesh( geometry, createAreaLightMaterial( 100 ) ); + light6.position.set( 0.0, 20.0, 0.0 ); + light6.scale.set( 1.0, 0.1, 1.0 ); + this.add( light6 ); + + } + + dispose() { + + const resources = new Set(); + + this.traverse( ( object ) => { + + if ( object.isMesh ) { + + resources.add( object.geometry ); + resources.add( object.material ); + + } + + } ); + + for ( const resource of resources ) { + + resource.dispose(); + + } + + } + +} + +function createAreaLightMaterial( intensity ) { + + const material = new MeshBasicMaterial(); + material.color.setScalar( intensity ); + return material; + +} + +export { RoomEnvironment }; diff --git a/src/box-winUI/Assets/home-globe/vendor/three.module.js b/src/box-winUI/Assets/home-globe/vendor/three.module.js new file mode 100644 index 0000000..8c431f6 --- /dev/null +++ b/src/box-winUI/Assets/home-globe/vendor/three.module.js @@ -0,0 +1,53032 @@ +/** + * @license + * Copyright 2010-2024 Three.js Authors + * SPDX-License-Identifier: MIT + */ +const REVISION = '164'; + +const MOUSE = { LEFT: 0, MIDDLE: 1, RIGHT: 2, ROTATE: 0, DOLLY: 1, PAN: 2 }; +const TOUCH = { ROTATE: 0, PAN: 1, DOLLY_PAN: 2, DOLLY_ROTATE: 3 }; +const CullFaceNone = 0; +const CullFaceBack = 1; +const CullFaceFront = 2; +const CullFaceFrontBack = 3; +const BasicShadowMap = 0; +const PCFShadowMap = 1; +const PCFSoftShadowMap = 2; +const VSMShadowMap = 3; +const FrontSide = 0; +const BackSide = 1; +const DoubleSide = 2; +const NoBlending = 0; +const NormalBlending = 1; +const AdditiveBlending = 2; +const SubtractiveBlending = 3; +const MultiplyBlending = 4; +const CustomBlending = 5; +const AddEquation = 100; +const SubtractEquation = 101; +const ReverseSubtractEquation = 102; +const MinEquation = 103; +const MaxEquation = 104; +const ZeroFactor = 200; +const OneFactor = 201; +const SrcColorFactor = 202; +const OneMinusSrcColorFactor = 203; +const SrcAlphaFactor = 204; +const OneMinusSrcAlphaFactor = 205; +const DstAlphaFactor = 206; +const OneMinusDstAlphaFactor = 207; +const DstColorFactor = 208; +const OneMinusDstColorFactor = 209; +const SrcAlphaSaturateFactor = 210; +const ConstantColorFactor = 211; +const OneMinusConstantColorFactor = 212; +const ConstantAlphaFactor = 213; +const OneMinusConstantAlphaFactor = 214; +const NeverDepth = 0; +const AlwaysDepth = 1; +const LessDepth = 2; +const LessEqualDepth = 3; +const EqualDepth = 4; +const GreaterEqualDepth = 5; +const GreaterDepth = 6; +const NotEqualDepth = 7; +const MultiplyOperation = 0; +const MixOperation = 1; +const AddOperation = 2; +const NoToneMapping = 0; +const LinearToneMapping = 1; +const ReinhardToneMapping = 2; +const CineonToneMapping = 3; +const ACESFilmicToneMapping = 4; +const CustomToneMapping = 5; +const AgXToneMapping = 6; +const NeutralToneMapping = 7; +const AttachedBindMode = 'attached'; +const DetachedBindMode = 'detached'; + +const UVMapping = 300; +const CubeReflectionMapping = 301; +const CubeRefractionMapping = 302; +const EquirectangularReflectionMapping = 303; +const EquirectangularRefractionMapping = 304; +const CubeUVReflectionMapping = 306; +const RepeatWrapping = 1000; +const ClampToEdgeWrapping = 1001; +const MirroredRepeatWrapping = 1002; +const NearestFilter = 1003; +const NearestMipmapNearestFilter = 1004; +const NearestMipMapNearestFilter = 1004; +const NearestMipmapLinearFilter = 1005; +const NearestMipMapLinearFilter = 1005; +const LinearFilter = 1006; +const LinearMipmapNearestFilter = 1007; +const LinearMipMapNearestFilter = 1007; +const LinearMipmapLinearFilter = 1008; +const LinearMipMapLinearFilter = 1008; +const UnsignedByteType = 1009; +const ByteType = 1010; +const ShortType = 1011; +const UnsignedShortType = 1012; +const IntType = 1013; +const UnsignedIntType = 1014; +const FloatType = 1015; +const HalfFloatType = 1016; +const UnsignedShort4444Type = 1017; +const UnsignedShort5551Type = 1018; +const UnsignedInt248Type = 1020; +const UnsignedInt5999Type = 35902; +const AlphaFormat = 1021; +const RGBFormat = 1022; +const RGBAFormat = 1023; +const LuminanceFormat = 1024; +const LuminanceAlphaFormat = 1025; +const DepthFormat = 1026; +const DepthStencilFormat = 1027; +const RedFormat = 1028; +const RedIntegerFormat = 1029; +const RGFormat = 1030; +const RGIntegerFormat = 1031; +const RGBAIntegerFormat = 1033; + +const RGB_S3TC_DXT1_Format = 33776; +const RGBA_S3TC_DXT1_Format = 33777; +const RGBA_S3TC_DXT3_Format = 33778; +const RGBA_S3TC_DXT5_Format = 33779; +const RGB_PVRTC_4BPPV1_Format = 35840; +const RGB_PVRTC_2BPPV1_Format = 35841; +const RGBA_PVRTC_4BPPV1_Format = 35842; +const RGBA_PVRTC_2BPPV1_Format = 35843; +const RGB_ETC1_Format = 36196; +const RGB_ETC2_Format = 37492; +const RGBA_ETC2_EAC_Format = 37496; +const RGBA_ASTC_4x4_Format = 37808; +const RGBA_ASTC_5x4_Format = 37809; +const RGBA_ASTC_5x5_Format = 37810; +const RGBA_ASTC_6x5_Format = 37811; +const RGBA_ASTC_6x6_Format = 37812; +const RGBA_ASTC_8x5_Format = 37813; +const RGBA_ASTC_8x6_Format = 37814; +const RGBA_ASTC_8x8_Format = 37815; +const RGBA_ASTC_10x5_Format = 37816; +const RGBA_ASTC_10x6_Format = 37817; +const RGBA_ASTC_10x8_Format = 37818; +const RGBA_ASTC_10x10_Format = 37819; +const RGBA_ASTC_12x10_Format = 37820; +const RGBA_ASTC_12x12_Format = 37821; +const RGBA_BPTC_Format = 36492; +const RGB_BPTC_SIGNED_Format = 36494; +const RGB_BPTC_UNSIGNED_Format = 36495; +const RED_RGTC1_Format = 36283; +const SIGNED_RED_RGTC1_Format = 36284; +const RED_GREEN_RGTC2_Format = 36285; +const SIGNED_RED_GREEN_RGTC2_Format = 36286; +const LoopOnce = 2200; +const LoopRepeat = 2201; +const LoopPingPong = 2202; +const InterpolateDiscrete = 2300; +const InterpolateLinear = 2301; +const InterpolateSmooth = 2302; +const ZeroCurvatureEnding = 2400; +const ZeroSlopeEnding = 2401; +const WrapAroundEnding = 2402; +const NormalAnimationBlendMode = 2500; +const AdditiveAnimationBlendMode = 2501; +const TrianglesDrawMode = 0; +const TriangleStripDrawMode = 1; +const TriangleFanDrawMode = 2; +const BasicDepthPacking = 3200; +const RGBADepthPacking = 3201; +const TangentSpaceNormalMap = 0; +const ObjectSpaceNormalMap = 1; + +// Color space string identifiers, matching CSS Color Module Level 4 and WebGPU names where available. +const NoColorSpace = ''; +const SRGBColorSpace = 'srgb'; +const LinearSRGBColorSpace = 'srgb-linear'; +const DisplayP3ColorSpace = 'display-p3'; +const LinearDisplayP3ColorSpace = 'display-p3-linear'; + +const LinearTransfer = 'linear'; +const SRGBTransfer = 'srgb'; + +const Rec709Primaries = 'rec709'; +const P3Primaries = 'p3'; + +const ZeroStencilOp = 0; +const KeepStencilOp = 7680; +const ReplaceStencilOp = 7681; +const IncrementStencilOp = 7682; +const DecrementStencilOp = 7683; +const IncrementWrapStencilOp = 34055; +const DecrementWrapStencilOp = 34056; +const InvertStencilOp = 5386; + +const NeverStencilFunc = 512; +const LessStencilFunc = 513; +const EqualStencilFunc = 514; +const LessEqualStencilFunc = 515; +const GreaterStencilFunc = 516; +const NotEqualStencilFunc = 517; +const GreaterEqualStencilFunc = 518; +const AlwaysStencilFunc = 519; + +const NeverCompare = 512; +const LessCompare = 513; +const EqualCompare = 514; +const LessEqualCompare = 515; +const GreaterCompare = 516; +const NotEqualCompare = 517; +const GreaterEqualCompare = 518; +const AlwaysCompare = 519; + +const StaticDrawUsage = 35044; +const DynamicDrawUsage = 35048; +const StreamDrawUsage = 35040; +const StaticReadUsage = 35045; +const DynamicReadUsage = 35049; +const StreamReadUsage = 35041; +const StaticCopyUsage = 35046; +const DynamicCopyUsage = 35050; +const StreamCopyUsage = 35042; + +const GLSL1 = '100'; +const GLSL3 = '300 es'; + +const WebGLCoordinateSystem = 2000; +const WebGPUCoordinateSystem = 2001; + +/** + * https://github.com/mrdoob/eventdispatcher.js/ + */ + +class EventDispatcher { + + addEventListener( type, listener ) { + + if ( this._listeners === undefined ) this._listeners = {}; + + const listeners = this._listeners; + + if ( listeners[ type ] === undefined ) { + + listeners[ type ] = []; + + } + + if ( listeners[ type ].indexOf( listener ) === - 1 ) { + + listeners[ type ].push( listener ); + + } + + } + + hasEventListener( type, listener ) { + + if ( this._listeners === undefined ) return false; + + const listeners = this._listeners; + + return listeners[ type ] !== undefined && listeners[ type ].indexOf( listener ) !== - 1; + + } + + removeEventListener( type, listener ) { + + if ( this._listeners === undefined ) return; + + const listeners = this._listeners; + const listenerArray = listeners[ type ]; + + if ( listenerArray !== undefined ) { + + const index = listenerArray.indexOf( listener ); + + if ( index !== - 1 ) { + + listenerArray.splice( index, 1 ); + + } + + } + + } + + dispatchEvent( event ) { + + if ( this._listeners === undefined ) return; + + const listeners = this._listeners; + const listenerArray = listeners[ event.type ]; + + if ( listenerArray !== undefined ) { + + event.target = this; + + // Make a copy, in case listeners are removed while iterating. + const array = listenerArray.slice( 0 ); + + for ( let i = 0, l = array.length; i < l; i ++ ) { + + array[ i ].call( this, event ); + + } + + event.target = null; + + } + + } + +} + +const _lut = [ '00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '0a', '0b', '0c', '0d', '0e', '0f', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '1a', '1b', '1c', '1d', '1e', '1f', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '2a', '2b', '2c', '2d', '2e', '2f', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '3a', '3b', '3c', '3d', '3e', '3f', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '4a', '4b', '4c', '4d', '4e', '4f', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '5a', '5b', '5c', '5d', '5e', '5f', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '6a', '6b', '6c', '6d', '6e', '6f', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '7a', '7b', '7c', '7d', '7e', '7f', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '8a', '8b', '8c', '8d', '8e', '8f', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '9a', '9b', '9c', '9d', '9e', '9f', 'a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'aa', 'ab', 'ac', 'ad', 'ae', 'af', 'b0', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9', 'ba', 'bb', 'bc', 'bd', 'be', 'bf', 'c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'ca', 'cb', 'cc', 'cd', 'ce', 'cf', 'd0', 'd1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8', 'd9', 'da', 'db', 'dc', 'dd', 'de', 'df', 'e0', 'e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7', 'e8', 'e9', 'ea', 'eb', 'ec', 'ed', 'ee', 'ef', 'f0', 'f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'fa', 'fb', 'fc', 'fd', 'fe', 'ff' ]; + +let _seed = 1234567; + + +const DEG2RAD = Math.PI / 180; +const RAD2DEG = 180 / Math.PI; + +// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136 +function generateUUID() { + + const d0 = Math.random() * 0xffffffff | 0; + const d1 = Math.random() * 0xffffffff | 0; + const d2 = Math.random() * 0xffffffff | 0; + const d3 = Math.random() * 0xffffffff | 0; + const uuid = _lut[ d0 & 0xff ] + _lut[ d0 >> 8 & 0xff ] + _lut[ d0 >> 16 & 0xff ] + _lut[ d0 >> 24 & 0xff ] + '-' + + _lut[ d1 & 0xff ] + _lut[ d1 >> 8 & 0xff ] + '-' + _lut[ d1 >> 16 & 0x0f | 0x40 ] + _lut[ d1 >> 24 & 0xff ] + '-' + + _lut[ d2 & 0x3f | 0x80 ] + _lut[ d2 >> 8 & 0xff ] + '-' + _lut[ d2 >> 16 & 0xff ] + _lut[ d2 >> 24 & 0xff ] + + _lut[ d3 & 0xff ] + _lut[ d3 >> 8 & 0xff ] + _lut[ d3 >> 16 & 0xff ] + _lut[ d3 >> 24 & 0xff ]; + + // .toLowerCase() here flattens concatenated strings to save heap memory space. + return uuid.toLowerCase(); + +} + +function clamp( value, min, max ) { + + return Math.max( min, Math.min( max, value ) ); + +} + +// compute euclidean modulo of m % n +// https://en.wikipedia.org/wiki/Modulo_operation +function euclideanModulo( n, m ) { + + return ( ( n % m ) + m ) % m; + +} + +// Linear mapping from range to range +function mapLinear( x, a1, a2, b1, b2 ) { + + return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 ); + +} + +// https://www.gamedev.net/tutorials/programming/general-and-gameplay-programming/inverse-lerp-a-super-useful-yet-often-overlooked-function-r5230/ +function inverseLerp( x, y, value ) { + + if ( x !== y ) { + + return ( value - x ) / ( y - x ); + + } else { + + return 0; + + } + +} + +// https://en.wikipedia.org/wiki/Linear_interpolation +function lerp( x, y, t ) { + + return ( 1 - t ) * x + t * y; + +} + +// http://www.rorydriscoll.com/2016/03/07/frame-rate-independent-damping-using-lerp/ +function damp( x, y, lambda, dt ) { + + return lerp( x, y, 1 - Math.exp( - lambda * dt ) ); + +} + +// https://www.desmos.com/calculator/vcsjnyz7x4 +function pingpong( x, length = 1 ) { + + return length - Math.abs( euclideanModulo( x, length * 2 ) - length ); + +} + +// http://en.wikipedia.org/wiki/Smoothstep +function smoothstep( x, min, max ) { + + if ( x <= min ) return 0; + if ( x >= max ) return 1; + + x = ( x - min ) / ( max - min ); + + return x * x * ( 3 - 2 * x ); + +} + +function smootherstep( x, min, max ) { + + if ( x <= min ) return 0; + if ( x >= max ) return 1; + + x = ( x - min ) / ( max - min ); + + return x * x * x * ( x * ( x * 6 - 15 ) + 10 ); + +} + +// Random integer from interval +function randInt( low, high ) { + + return low + Math.floor( Math.random() * ( high - low + 1 ) ); + +} + +// Random float from interval +function randFloat( low, high ) { + + return low + Math.random() * ( high - low ); + +} + +// Random float from <-range/2, range/2> interval +function randFloatSpread( range ) { + + return range * ( 0.5 - Math.random() ); + +} + +// Deterministic pseudo-random float in the interval [ 0, 1 ] +function seededRandom( s ) { + + if ( s !== undefined ) _seed = s; + + // Mulberry32 generator + + let t = _seed += 0x6D2B79F5; + + t = Math.imul( t ^ t >>> 15, t | 1 ); + + t ^= t + Math.imul( t ^ t >>> 7, t | 61 ); + + return ( ( t ^ t >>> 14 ) >>> 0 ) / 4294967296; + +} + +function degToRad( degrees ) { + + return degrees * DEG2RAD; + +} + +function radToDeg( radians ) { + + return radians * RAD2DEG; + +} + +function isPowerOfTwo( value ) { + + return ( value & ( value - 1 ) ) === 0 && value !== 0; + +} + +function ceilPowerOfTwo( value ) { + + return Math.pow( 2, Math.ceil( Math.log( value ) / Math.LN2 ) ); + +} + +function floorPowerOfTwo( value ) { + + return Math.pow( 2, Math.floor( Math.log( value ) / Math.LN2 ) ); + +} + +function setQuaternionFromProperEuler( q, a, b, c, order ) { + + // Intrinsic Proper Euler Angles - see https://en.wikipedia.org/wiki/Euler_angles + + // rotations are applied to the axes in the order specified by 'order' + // rotation by angle 'a' is applied first, then by angle 'b', then by angle 'c' + // angles are in radians + + const cos = Math.cos; + const sin = Math.sin; + + const c2 = cos( b / 2 ); + const s2 = sin( b / 2 ); + + const c13 = cos( ( a + c ) / 2 ); + const s13 = sin( ( a + c ) / 2 ); + + const c1_3 = cos( ( a - c ) / 2 ); + const s1_3 = sin( ( a - c ) / 2 ); + + const c3_1 = cos( ( c - a ) / 2 ); + const s3_1 = sin( ( c - a ) / 2 ); + + switch ( order ) { + + case 'XYX': + q.set( c2 * s13, s2 * c1_3, s2 * s1_3, c2 * c13 ); + break; + + case 'YZY': + q.set( s2 * s1_3, c2 * s13, s2 * c1_3, c2 * c13 ); + break; + + case 'ZXZ': + q.set( s2 * c1_3, s2 * s1_3, c2 * s13, c2 * c13 ); + break; + + case 'XZX': + q.set( c2 * s13, s2 * s3_1, s2 * c3_1, c2 * c13 ); + break; + + case 'YXY': + q.set( s2 * c3_1, c2 * s13, s2 * s3_1, c2 * c13 ); + break; + + case 'ZYZ': + q.set( s2 * s3_1, s2 * c3_1, c2 * s13, c2 * c13 ); + break; + + default: + console.warn( 'THREE.MathUtils: .setQuaternionFromProperEuler() encountered an unknown order: ' + order ); + + } + +} + +function denormalize( value, array ) { + + switch ( array.constructor ) { + + case Float32Array: + + return value; + + case Uint32Array: + + return value / 4294967295.0; + + case Uint16Array: + + return value / 65535.0; + + case Uint8Array: + + return value / 255.0; + + case Int32Array: + + return Math.max( value / 2147483647.0, - 1.0 ); + + case Int16Array: + + return Math.max( value / 32767.0, - 1.0 ); + + case Int8Array: + + return Math.max( value / 127.0, - 1.0 ); + + default: + + throw new Error( 'Invalid component type.' ); + + } + +} + +function normalize( value, array ) { + + switch ( array.constructor ) { + + case Float32Array: + + return value; + + case Uint32Array: + + return Math.round( value * 4294967295.0 ); + + case Uint16Array: + + return Math.round( value * 65535.0 ); + + case Uint8Array: + + return Math.round( value * 255.0 ); + + case Int32Array: + + return Math.round( value * 2147483647.0 ); + + case Int16Array: + + return Math.round( value * 32767.0 ); + + case Int8Array: + + return Math.round( value * 127.0 ); + + default: + + throw new Error( 'Invalid component type.' ); + + } + +} + +const MathUtils = { + DEG2RAD: DEG2RAD, + RAD2DEG: RAD2DEG, + generateUUID: generateUUID, + clamp: clamp, + euclideanModulo: euclideanModulo, + mapLinear: mapLinear, + inverseLerp: inverseLerp, + lerp: lerp, + damp: damp, + pingpong: pingpong, + smoothstep: smoothstep, + smootherstep: smootherstep, + randInt: randInt, + randFloat: randFloat, + randFloatSpread: randFloatSpread, + seededRandom: seededRandom, + degToRad: degToRad, + radToDeg: radToDeg, + isPowerOfTwo: isPowerOfTwo, + ceilPowerOfTwo: ceilPowerOfTwo, + floorPowerOfTwo: floorPowerOfTwo, + setQuaternionFromProperEuler: setQuaternionFromProperEuler, + normalize: normalize, + denormalize: denormalize +}; + +class Vector2 { + + constructor( x = 0, y = 0 ) { + + Vector2.prototype.isVector2 = true; + + this.x = x; + this.y = y; + + } + + get width() { + + return this.x; + + } + + set width( value ) { + + this.x = value; + + } + + get height() { + + return this.y; + + } + + set height( value ) { + + this.y = value; + + } + + set( x, y ) { + + this.x = x; + this.y = y; + + return this; + + } + + setScalar( scalar ) { + + this.x = scalar; + this.y = scalar; + + return this; + + } + + setX( x ) { + + this.x = x; + + return this; + + } + + setY( y ) { + + this.y = y; + + return this; + + } + + setComponent( index, value ) { + + switch ( index ) { + + case 0: this.x = value; break; + case 1: this.y = value; break; + default: throw new Error( 'index is out of range: ' + index ); + + } + + return this; + + } + + getComponent( index ) { + + switch ( index ) { + + case 0: return this.x; + case 1: return this.y; + default: throw new Error( 'index is out of range: ' + index ); + + } + + } + + clone() { + + return new this.constructor( this.x, this.y ); + + } + + copy( v ) { + + this.x = v.x; + this.y = v.y; + + return this; + + } + + add( v ) { + + this.x += v.x; + this.y += v.y; + + return this; + + } + + addScalar( s ) { + + this.x += s; + this.y += s; + + return this; + + } + + addVectors( a, b ) { + + this.x = a.x + b.x; + this.y = a.y + b.y; + + return this; + + } + + addScaledVector( v, s ) { + + this.x += v.x * s; + this.y += v.y * s; + + return this; + + } + + sub( v ) { + + this.x -= v.x; + this.y -= v.y; + + return this; + + } + + subScalar( s ) { + + this.x -= s; + this.y -= s; + + return this; + + } + + subVectors( a, b ) { + + this.x = a.x - b.x; + this.y = a.y - b.y; + + return this; + + } + + multiply( v ) { + + this.x *= v.x; + this.y *= v.y; + + return this; + + } + + multiplyScalar( scalar ) { + + this.x *= scalar; + this.y *= scalar; + + return this; + + } + + divide( v ) { + + this.x /= v.x; + this.y /= v.y; + + return this; + + } + + divideScalar( scalar ) { + + return this.multiplyScalar( 1 / scalar ); + + } + + applyMatrix3( m ) { + + const x = this.x, y = this.y; + const e = m.elements; + + this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ]; + this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ]; + + return this; + + } + + min( v ) { + + this.x = Math.min( this.x, v.x ); + this.y = Math.min( this.y, v.y ); + + return this; + + } + + max( v ) { + + this.x = Math.max( this.x, v.x ); + this.y = Math.max( this.y, v.y ); + + return this; + + } + + clamp( min, max ) { + + // assumes min < max, componentwise + + this.x = Math.max( min.x, Math.min( max.x, this.x ) ); + this.y = Math.max( min.y, Math.min( max.y, this.y ) ); + + return this; + + } + + clampScalar( minVal, maxVal ) { + + this.x = Math.max( minVal, Math.min( maxVal, this.x ) ); + this.y = Math.max( minVal, Math.min( maxVal, this.y ) ); + + return this; + + } + + clampLength( min, max ) { + + const length = this.length(); + + return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) ); + + } + + floor() { + + this.x = Math.floor( this.x ); + this.y = Math.floor( this.y ); + + return this; + + } + + ceil() { + + this.x = Math.ceil( this.x ); + this.y = Math.ceil( this.y ); + + return this; + + } + + round() { + + this.x = Math.round( this.x ); + this.y = Math.round( this.y ); + + return this; + + } + + roundToZero() { + + this.x = Math.trunc( this.x ); + this.y = Math.trunc( this.y ); + + return this; + + } + + negate() { + + this.x = - this.x; + this.y = - this.y; + + return this; + + } + + dot( v ) { + + return this.x * v.x + this.y * v.y; + + } + + cross( v ) { + + return this.x * v.y - this.y * v.x; + + } + + lengthSq() { + + return this.x * this.x + this.y * this.y; + + } + + length() { + + return Math.sqrt( this.x * this.x + this.y * this.y ); + + } + + manhattanLength() { + + return Math.abs( this.x ) + Math.abs( this.y ); + + } + + normalize() { + + return this.divideScalar( this.length() || 1 ); + + } + + angle() { + + // computes the angle in radians with respect to the positive x-axis + + const angle = Math.atan2( - this.y, - this.x ) + Math.PI; + + return angle; + + } + + angleTo( v ) { + + const denominator = Math.sqrt( this.lengthSq() * v.lengthSq() ); + + if ( denominator === 0 ) return Math.PI / 2; + + const theta = this.dot( v ) / denominator; + + // clamp, to handle numerical problems + + return Math.acos( clamp( theta, - 1, 1 ) ); + + } + + distanceTo( v ) { + + return Math.sqrt( this.distanceToSquared( v ) ); + + } + + distanceToSquared( v ) { + + const dx = this.x - v.x, dy = this.y - v.y; + return dx * dx + dy * dy; + + } + + manhattanDistanceTo( v ) { + + return Math.abs( this.x - v.x ) + Math.abs( this.y - v.y ); + + } + + setLength( length ) { + + return this.normalize().multiplyScalar( length ); + + } + + lerp( v, alpha ) { + + this.x += ( v.x - this.x ) * alpha; + this.y += ( v.y - this.y ) * alpha; + + return this; + + } + + lerpVectors( v1, v2, alpha ) { + + this.x = v1.x + ( v2.x - v1.x ) * alpha; + this.y = v1.y + ( v2.y - v1.y ) * alpha; + + return this; + + } + + equals( v ) { + + return ( ( v.x === this.x ) && ( v.y === this.y ) ); + + } + + fromArray( array, offset = 0 ) { + + this.x = array[ offset ]; + this.y = array[ offset + 1 ]; + + return this; + + } + + toArray( array = [], offset = 0 ) { + + array[ offset ] = this.x; + array[ offset + 1 ] = this.y; + + return array; + + } + + fromBufferAttribute( attribute, index ) { + + this.x = attribute.getX( index ); + this.y = attribute.getY( index ); + + return this; + + } + + rotateAround( center, angle ) { + + const c = Math.cos( angle ), s = Math.sin( angle ); + + const x = this.x - center.x; + const y = this.y - center.y; + + this.x = x * c - y * s + center.x; + this.y = x * s + y * c + center.y; + + return this; + + } + + random() { + + this.x = Math.random(); + this.y = Math.random(); + + return this; + + } + + *[ Symbol.iterator ]() { + + yield this.x; + yield this.y; + + } + +} + +class Matrix3 { + + constructor( n11, n12, n13, n21, n22, n23, n31, n32, n33 ) { + + Matrix3.prototype.isMatrix3 = true; + + this.elements = [ + + 1, 0, 0, + 0, 1, 0, + 0, 0, 1 + + ]; + + if ( n11 !== undefined ) { + + this.set( n11, n12, n13, n21, n22, n23, n31, n32, n33 ); + + } + + } + + set( n11, n12, n13, n21, n22, n23, n31, n32, n33 ) { + + const te = this.elements; + + te[ 0 ] = n11; te[ 1 ] = n21; te[ 2 ] = n31; + te[ 3 ] = n12; te[ 4 ] = n22; te[ 5 ] = n32; + te[ 6 ] = n13; te[ 7 ] = n23; te[ 8 ] = n33; + + return this; + + } + + identity() { + + this.set( + + 1, 0, 0, + 0, 1, 0, + 0, 0, 1 + + ); + + return this; + + } + + copy( m ) { + + const te = this.elements; + const me = m.elements; + + te[ 0 ] = me[ 0 ]; te[ 1 ] = me[ 1 ]; te[ 2 ] = me[ 2 ]; + te[ 3 ] = me[ 3 ]; te[ 4 ] = me[ 4 ]; te[ 5 ] = me[ 5 ]; + te[ 6 ] = me[ 6 ]; te[ 7 ] = me[ 7 ]; te[ 8 ] = me[ 8 ]; + + return this; + + } + + extractBasis( xAxis, yAxis, zAxis ) { + + xAxis.setFromMatrix3Column( this, 0 ); + yAxis.setFromMatrix3Column( this, 1 ); + zAxis.setFromMatrix3Column( this, 2 ); + + return this; + + } + + setFromMatrix4( m ) { + + const me = m.elements; + + this.set( + + me[ 0 ], me[ 4 ], me[ 8 ], + me[ 1 ], me[ 5 ], me[ 9 ], + me[ 2 ], me[ 6 ], me[ 10 ] + + ); + + return this; + + } + + multiply( m ) { + + return this.multiplyMatrices( this, m ); + + } + + premultiply( m ) { + + return this.multiplyMatrices( m, this ); + + } + + multiplyMatrices( a, b ) { + + const ae = a.elements; + const be = b.elements; + const te = this.elements; + + const a11 = ae[ 0 ], a12 = ae[ 3 ], a13 = ae[ 6 ]; + const a21 = ae[ 1 ], a22 = ae[ 4 ], a23 = ae[ 7 ]; + const a31 = ae[ 2 ], a32 = ae[ 5 ], a33 = ae[ 8 ]; + + const b11 = be[ 0 ], b12 = be[ 3 ], b13 = be[ 6 ]; + const b21 = be[ 1 ], b22 = be[ 4 ], b23 = be[ 7 ]; + const b31 = be[ 2 ], b32 = be[ 5 ], b33 = be[ 8 ]; + + te[ 0 ] = a11 * b11 + a12 * b21 + a13 * b31; + te[ 3 ] = a11 * b12 + a12 * b22 + a13 * b32; + te[ 6 ] = a11 * b13 + a12 * b23 + a13 * b33; + + te[ 1 ] = a21 * b11 + a22 * b21 + a23 * b31; + te[ 4 ] = a21 * b12 + a22 * b22 + a23 * b32; + te[ 7 ] = a21 * b13 + a22 * b23 + a23 * b33; + + te[ 2 ] = a31 * b11 + a32 * b21 + a33 * b31; + te[ 5 ] = a31 * b12 + a32 * b22 + a33 * b32; + te[ 8 ] = a31 * b13 + a32 * b23 + a33 * b33; + + return this; + + } + + multiplyScalar( s ) { + + const te = this.elements; + + te[ 0 ] *= s; te[ 3 ] *= s; te[ 6 ] *= s; + te[ 1 ] *= s; te[ 4 ] *= s; te[ 7 ] *= s; + te[ 2 ] *= s; te[ 5 ] *= s; te[ 8 ] *= s; + + return this; + + } + + determinant() { + + const te = this.elements; + + const a = te[ 0 ], b = te[ 1 ], c = te[ 2 ], + d = te[ 3 ], e = te[ 4 ], f = te[ 5 ], + g = te[ 6 ], h = te[ 7 ], i = te[ 8 ]; + + return a * e * i - a * f * h - b * d * i + b * f * g + c * d * h - c * e * g; + + } + + invert() { + + const te = this.elements, + + n11 = te[ 0 ], n21 = te[ 1 ], n31 = te[ 2 ], + n12 = te[ 3 ], n22 = te[ 4 ], n32 = te[ 5 ], + n13 = te[ 6 ], n23 = te[ 7 ], n33 = te[ 8 ], + + t11 = n33 * n22 - n32 * n23, + t12 = n32 * n13 - n33 * n12, + t13 = n23 * n12 - n22 * n13, + + det = n11 * t11 + n21 * t12 + n31 * t13; + + if ( det === 0 ) return this.set( 0, 0, 0, 0, 0, 0, 0, 0, 0 ); + + const detInv = 1 / det; + + te[ 0 ] = t11 * detInv; + te[ 1 ] = ( n31 * n23 - n33 * n21 ) * detInv; + te[ 2 ] = ( n32 * n21 - n31 * n22 ) * detInv; + + te[ 3 ] = t12 * detInv; + te[ 4 ] = ( n33 * n11 - n31 * n13 ) * detInv; + te[ 5 ] = ( n31 * n12 - n32 * n11 ) * detInv; + + te[ 6 ] = t13 * detInv; + te[ 7 ] = ( n21 * n13 - n23 * n11 ) * detInv; + te[ 8 ] = ( n22 * n11 - n21 * n12 ) * detInv; + + return this; + + } + + transpose() { + + let tmp; + const m = this.elements; + + tmp = m[ 1 ]; m[ 1 ] = m[ 3 ]; m[ 3 ] = tmp; + tmp = m[ 2 ]; m[ 2 ] = m[ 6 ]; m[ 6 ] = tmp; + tmp = m[ 5 ]; m[ 5 ] = m[ 7 ]; m[ 7 ] = tmp; + + return this; + + } + + getNormalMatrix( matrix4 ) { + + return this.setFromMatrix4( matrix4 ).invert().transpose(); + + } + + transposeIntoArray( r ) { + + const m = this.elements; + + r[ 0 ] = m[ 0 ]; + r[ 1 ] = m[ 3 ]; + r[ 2 ] = m[ 6 ]; + r[ 3 ] = m[ 1 ]; + r[ 4 ] = m[ 4 ]; + r[ 5 ] = m[ 7 ]; + r[ 6 ] = m[ 2 ]; + r[ 7 ] = m[ 5 ]; + r[ 8 ] = m[ 8 ]; + + return this; + + } + + setUvTransform( tx, ty, sx, sy, rotation, cx, cy ) { + + const c = Math.cos( rotation ); + const s = Math.sin( rotation ); + + this.set( + sx * c, sx * s, - sx * ( c * cx + s * cy ) + cx + tx, + - sy * s, sy * c, - sy * ( - s * cx + c * cy ) + cy + ty, + 0, 0, 1 + ); + + return this; + + } + + // + + scale( sx, sy ) { + + this.premultiply( _m3.makeScale( sx, sy ) ); + + return this; + + } + + rotate( theta ) { + + this.premultiply( _m3.makeRotation( - theta ) ); + + return this; + + } + + translate( tx, ty ) { + + this.premultiply( _m3.makeTranslation( tx, ty ) ); + + return this; + + } + + // for 2D Transforms + + makeTranslation( x, y ) { + + if ( x.isVector2 ) { + + this.set( + + 1, 0, x.x, + 0, 1, x.y, + 0, 0, 1 + + ); + + } else { + + this.set( + + 1, 0, x, + 0, 1, y, + 0, 0, 1 + + ); + + } + + return this; + + } + + makeRotation( theta ) { + + // counterclockwise + + const c = Math.cos( theta ); + const s = Math.sin( theta ); + + this.set( + + c, - s, 0, + s, c, 0, + 0, 0, 1 + + ); + + return this; + + } + + makeScale( x, y ) { + + this.set( + + x, 0, 0, + 0, y, 0, + 0, 0, 1 + + ); + + return this; + + } + + // + + equals( matrix ) { + + const te = this.elements; + const me = matrix.elements; + + for ( let i = 0; i < 9; i ++ ) { + + if ( te[ i ] !== me[ i ] ) return false; + + } + + return true; + + } + + fromArray( array, offset = 0 ) { + + for ( let i = 0; i < 9; i ++ ) { + + this.elements[ i ] = array[ i + offset ]; + + } + + return this; + + } + + toArray( array = [], offset = 0 ) { + + const te = this.elements; + + array[ offset ] = te[ 0 ]; + array[ offset + 1 ] = te[ 1 ]; + array[ offset + 2 ] = te[ 2 ]; + + array[ offset + 3 ] = te[ 3 ]; + array[ offset + 4 ] = te[ 4 ]; + array[ offset + 5 ] = te[ 5 ]; + + array[ offset + 6 ] = te[ 6 ]; + array[ offset + 7 ] = te[ 7 ]; + array[ offset + 8 ] = te[ 8 ]; + + return array; + + } + + clone() { + + return new this.constructor().fromArray( this.elements ); + + } + +} + +const _m3 = /*@__PURE__*/ new Matrix3(); + +function arrayNeedsUint32( array ) { + + // assumes larger values usually on last + + for ( let i = array.length - 1; i >= 0; -- i ) { + + if ( array[ i ] >= 65535 ) return true; // account for PRIMITIVE_RESTART_FIXED_INDEX, #24565 + + } + + return false; + +} + +const TYPED_ARRAYS = { + Int8Array: Int8Array, + Uint8Array: Uint8Array, + Uint8ClampedArray: Uint8ClampedArray, + Int16Array: Int16Array, + Uint16Array: Uint16Array, + Int32Array: Int32Array, + Uint32Array: Uint32Array, + Float32Array: Float32Array, + Float64Array: Float64Array +}; + +function getTypedArray( type, buffer ) { + + return new TYPED_ARRAYS[ type ]( buffer ); + +} + +function createElementNS( name ) { + + return document.createElementNS( 'http://www.w3.org/1999/xhtml', name ); + +} + +function createCanvasElement() { + + const canvas = createElementNS( 'canvas' ); + canvas.style.display = 'block'; + return canvas; + +} + +const _cache = {}; + +function warnOnce( message ) { + + if ( message in _cache ) return; + + _cache[ message ] = true; + + console.warn( message ); + +} + +/** + * Matrices converting P3 <-> Rec. 709 primaries, without gamut mapping + * or clipping. Based on W3C specifications for sRGB and Display P3, + * and ICC specifications for the D50 connection space. Values in/out + * are _linear_ sRGB and _linear_ Display P3. + * + * Note that both sRGB and Display P3 use the sRGB transfer functions. + * + * Reference: + * - http://www.russellcottrell.com/photo/matrixCalculator.htm + */ + +const LINEAR_SRGB_TO_LINEAR_DISPLAY_P3 = /*@__PURE__*/ new Matrix3().set( + 0.8224621, 0.177538, 0.0, + 0.0331941, 0.9668058, 0.0, + 0.0170827, 0.0723974, 0.9105199, +); + +const LINEAR_DISPLAY_P3_TO_LINEAR_SRGB = /*@__PURE__*/ new Matrix3().set( + 1.2249401, - 0.2249404, 0.0, + - 0.0420569, 1.0420571, 0.0, + - 0.0196376, - 0.0786361, 1.0982735 +); + +/** + * Defines supported color spaces by transfer function and primaries, + * and provides conversions to/from the Linear-sRGB reference space. + */ +const COLOR_SPACES = { + [ LinearSRGBColorSpace ]: { + transfer: LinearTransfer, + primaries: Rec709Primaries, + toReference: ( color ) => color, + fromReference: ( color ) => color, + }, + [ SRGBColorSpace ]: { + transfer: SRGBTransfer, + primaries: Rec709Primaries, + toReference: ( color ) => color.convertSRGBToLinear(), + fromReference: ( color ) => color.convertLinearToSRGB(), + }, + [ LinearDisplayP3ColorSpace ]: { + transfer: LinearTransfer, + primaries: P3Primaries, + toReference: ( color ) => color.applyMatrix3( LINEAR_DISPLAY_P3_TO_LINEAR_SRGB ), + fromReference: ( color ) => color.applyMatrix3( LINEAR_SRGB_TO_LINEAR_DISPLAY_P3 ), + }, + [ DisplayP3ColorSpace ]: { + transfer: SRGBTransfer, + primaries: P3Primaries, + toReference: ( color ) => color.convertSRGBToLinear().applyMatrix3( LINEAR_DISPLAY_P3_TO_LINEAR_SRGB ), + fromReference: ( color ) => color.applyMatrix3( LINEAR_SRGB_TO_LINEAR_DISPLAY_P3 ).convertLinearToSRGB(), + }, +}; + +const SUPPORTED_WORKING_COLOR_SPACES = new Set( [ LinearSRGBColorSpace, LinearDisplayP3ColorSpace ] ); + +const ColorManagement = { + + enabled: true, + + _workingColorSpace: LinearSRGBColorSpace, + + get workingColorSpace() { + + return this._workingColorSpace; + + }, + + set workingColorSpace( colorSpace ) { + + if ( ! SUPPORTED_WORKING_COLOR_SPACES.has( colorSpace ) ) { + + throw new Error( `Unsupported working color space, "${ colorSpace }".` ); + + } + + this._workingColorSpace = colorSpace; + + }, + + convert: function ( color, sourceColorSpace, targetColorSpace ) { + + if ( this.enabled === false || sourceColorSpace === targetColorSpace || ! sourceColorSpace || ! targetColorSpace ) { + + return color; + + } + + const sourceToReference = COLOR_SPACES[ sourceColorSpace ].toReference; + const targetFromReference = COLOR_SPACES[ targetColorSpace ].fromReference; + + return targetFromReference( sourceToReference( color ) ); + + }, + + fromWorkingColorSpace: function ( color, targetColorSpace ) { + + return this.convert( color, this._workingColorSpace, targetColorSpace ); + + }, + + toWorkingColorSpace: function ( color, sourceColorSpace ) { + + return this.convert( color, sourceColorSpace, this._workingColorSpace ); + + }, + + getPrimaries: function ( colorSpace ) { + + return COLOR_SPACES[ colorSpace ].primaries; + + }, + + getTransfer: function ( colorSpace ) { + + if ( colorSpace === NoColorSpace ) return LinearTransfer; + + return COLOR_SPACES[ colorSpace ].transfer; + + }, + +}; + + +function SRGBToLinear( c ) { + + return ( c < 0.04045 ) ? c * 0.0773993808 : Math.pow( c * 0.9478672986 + 0.0521327014, 2.4 ); + +} + +function LinearToSRGB( c ) { + + return ( c < 0.0031308 ) ? c * 12.92 : 1.055 * ( Math.pow( c, 0.41666 ) ) - 0.055; + +} + +let _canvas; + +class ImageUtils { + + static getDataURL( image ) { + + if ( /^data:/i.test( image.src ) ) { + + return image.src; + + } + + if ( typeof HTMLCanvasElement === 'undefined' ) { + + return image.src; + + } + + let canvas; + + if ( image instanceof HTMLCanvasElement ) { + + canvas = image; + + } else { + + if ( _canvas === undefined ) _canvas = createElementNS( 'canvas' ); + + _canvas.width = image.width; + _canvas.height = image.height; + + const context = _canvas.getContext( '2d' ); + + if ( image instanceof ImageData ) { + + context.putImageData( image, 0, 0 ); + + } else { + + context.drawImage( image, 0, 0, image.width, image.height ); + + } + + canvas = _canvas; + + } + + if ( canvas.width > 2048 || canvas.height > 2048 ) { + + console.warn( 'THREE.ImageUtils.getDataURL: Image converted to jpg for performance reasons', image ); + + return canvas.toDataURL( 'image/jpeg', 0.6 ); + + } else { + + return canvas.toDataURL( 'image/png' ); + + } + + } + + static sRGBToLinear( image ) { + + if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) || + ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) || + ( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap ) ) { + + const canvas = createElementNS( 'canvas' ); + + canvas.width = image.width; + canvas.height = image.height; + + const context = canvas.getContext( '2d' ); + context.drawImage( image, 0, 0, image.width, image.height ); + + const imageData = context.getImageData( 0, 0, image.width, image.height ); + const data = imageData.data; + + for ( let i = 0; i < data.length; i ++ ) { + + data[ i ] = SRGBToLinear( data[ i ] / 255 ) * 255; + + } + + context.putImageData( imageData, 0, 0 ); + + return canvas; + + } else if ( image.data ) { + + const data = image.data.slice( 0 ); + + for ( let i = 0; i < data.length; i ++ ) { + + if ( data instanceof Uint8Array || data instanceof Uint8ClampedArray ) { + + data[ i ] = Math.floor( SRGBToLinear( data[ i ] / 255 ) * 255 ); + + } else { + + // assuming float + + data[ i ] = SRGBToLinear( data[ i ] ); + + } + + } + + return { + data: data, + width: image.width, + height: image.height + }; + + } else { + + console.warn( 'THREE.ImageUtils.sRGBToLinear(): Unsupported image type. No color space conversion applied.' ); + return image; + + } + + } + +} + +let _sourceId = 0; + +class Source { + + constructor( data = null ) { + + this.isSource = true; + + Object.defineProperty( this, 'id', { value: _sourceId ++ } ); + + this.uuid = generateUUID(); + + this.data = data; + this.dataReady = true; + + this.version = 0; + + } + + set needsUpdate( value ) { + + if ( value === true ) this.version ++; + + } + + toJSON( meta ) { + + const isRootObject = ( meta === undefined || typeof meta === 'string' ); + + if ( ! isRootObject && meta.images[ this.uuid ] !== undefined ) { + + return meta.images[ this.uuid ]; + + } + + const output = { + uuid: this.uuid, + url: '' + }; + + const data = this.data; + + if ( data !== null ) { + + let url; + + if ( Array.isArray( data ) ) { + + // cube texture + + url = []; + + for ( let i = 0, l = data.length; i < l; i ++ ) { + + if ( data[ i ].isDataTexture ) { + + url.push( serializeImage( data[ i ].image ) ); + + } else { + + url.push( serializeImage( data[ i ] ) ); + + } + + } + + } else { + + // texture + + url = serializeImage( data ); + + } + + output.url = url; + + } + + if ( ! isRootObject ) { + + meta.images[ this.uuid ] = output; + + } + + return output; + + } + +} + +function serializeImage( image ) { + + if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) || + ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) || + ( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap ) ) { + + // default images + + return ImageUtils.getDataURL( image ); + + } else { + + if ( image.data ) { + + // images of DataTexture + + return { + data: Array.from( image.data ), + width: image.width, + height: image.height, + type: image.data.constructor.name + }; + + } else { + + console.warn( 'THREE.Texture: Unable to serialize Texture.' ); + return {}; + + } + + } + +} + +let _textureId = 0; + +class Texture extends EventDispatcher { + + constructor( image = Texture.DEFAULT_IMAGE, mapping = Texture.DEFAULT_MAPPING, wrapS = ClampToEdgeWrapping, wrapT = ClampToEdgeWrapping, magFilter = LinearFilter, minFilter = LinearMipmapLinearFilter, format = RGBAFormat, type = UnsignedByteType, anisotropy = Texture.DEFAULT_ANISOTROPY, colorSpace = NoColorSpace ) { + + super(); + + this.isTexture = true; + + Object.defineProperty( this, 'id', { value: _textureId ++ } ); + + this.uuid = generateUUID(); + + this.name = ''; + + this.source = new Source( image ); + this.mipmaps = []; + + this.mapping = mapping; + this.channel = 0; + + this.wrapS = wrapS; + this.wrapT = wrapT; + + this.magFilter = magFilter; + this.minFilter = minFilter; + + this.anisotropy = anisotropy; + + this.format = format; + this.internalFormat = null; + this.type = type; + + this.offset = new Vector2( 0, 0 ); + this.repeat = new Vector2( 1, 1 ); + this.center = new Vector2( 0, 0 ); + this.rotation = 0; + + this.matrixAutoUpdate = true; + this.matrix = new Matrix3(); + + this.generateMipmaps = true; + this.premultiplyAlpha = false; + this.flipY = true; + this.unpackAlignment = 4; // valid values: 1, 2, 4, 8 (see http://www.khronos.org/opengles/sdk/docs/man/xhtml/glPixelStorei.xml) + + this.colorSpace = colorSpace; + + this.userData = {}; + + this.version = 0; + this.onUpdate = null; + + this.isRenderTargetTexture = false; // indicates whether a texture belongs to a render target or not + this.pmremVersion = 0; // indicates whether this texture should be processed by PMREMGenerator or not (only relevant for render target textures) + + } + + get image() { + + return this.source.data; + + } + + set image( value = null ) { + + this.source.data = value; + + } + + updateMatrix() { + + this.matrix.setUvTransform( this.offset.x, this.offset.y, this.repeat.x, this.repeat.y, this.rotation, this.center.x, this.center.y ); + + } + + clone() { + + return new this.constructor().copy( this ); + + } + + copy( source ) { + + this.name = source.name; + + this.source = source.source; + this.mipmaps = source.mipmaps.slice( 0 ); + + this.mapping = source.mapping; + this.channel = source.channel; + + this.wrapS = source.wrapS; + this.wrapT = source.wrapT; + + this.magFilter = source.magFilter; + this.minFilter = source.minFilter; + + this.anisotropy = source.anisotropy; + + this.format = source.format; + this.internalFormat = source.internalFormat; + this.type = source.type; + + this.offset.copy( source.offset ); + this.repeat.copy( source.repeat ); + this.center.copy( source.center ); + this.rotation = source.rotation; + + this.matrixAutoUpdate = source.matrixAutoUpdate; + this.matrix.copy( source.matrix ); + + this.generateMipmaps = source.generateMipmaps; + this.premultiplyAlpha = source.premultiplyAlpha; + this.flipY = source.flipY; + this.unpackAlignment = source.unpackAlignment; + this.colorSpace = source.colorSpace; + + this.userData = JSON.parse( JSON.stringify( source.userData ) ); + + this.needsUpdate = true; + + return this; + + } + + toJSON( meta ) { + + const isRootObject = ( meta === undefined || typeof meta === 'string' ); + + if ( ! isRootObject && meta.textures[ this.uuid ] !== undefined ) { + + return meta.textures[ this.uuid ]; + + } + + const output = { + + metadata: { + version: 4.6, + type: 'Texture', + generator: 'Texture.toJSON' + }, + + uuid: this.uuid, + name: this.name, + + image: this.source.toJSON( meta ).uuid, + + mapping: this.mapping, + channel: this.channel, + + repeat: [ this.repeat.x, this.repeat.y ], + offset: [ this.offset.x, this.offset.y ], + center: [ this.center.x, this.center.y ], + rotation: this.rotation, + + wrap: [ this.wrapS, this.wrapT ], + + format: this.format, + internalFormat: this.internalFormat, + type: this.type, + colorSpace: this.colorSpace, + + minFilter: this.minFilter, + magFilter: this.magFilter, + anisotropy: this.anisotropy, + + flipY: this.flipY, + + generateMipmaps: this.generateMipmaps, + premultiplyAlpha: this.premultiplyAlpha, + unpackAlignment: this.unpackAlignment + + }; + + if ( Object.keys( this.userData ).length > 0 ) output.userData = this.userData; + + if ( ! isRootObject ) { + + meta.textures[ this.uuid ] = output; + + } + + return output; + + } + + dispose() { + + this.dispatchEvent( { type: 'dispose' } ); + + } + + transformUv( uv ) { + + if ( this.mapping !== UVMapping ) return uv; + + uv.applyMatrix3( this.matrix ); + + if ( uv.x < 0 || uv.x > 1 ) { + + switch ( this.wrapS ) { + + case RepeatWrapping: + + uv.x = uv.x - Math.floor( uv.x ); + break; + + case ClampToEdgeWrapping: + + uv.x = uv.x < 0 ? 0 : 1; + break; + + case MirroredRepeatWrapping: + + if ( Math.abs( Math.floor( uv.x ) % 2 ) === 1 ) { + + uv.x = Math.ceil( uv.x ) - uv.x; + + } else { + + uv.x = uv.x - Math.floor( uv.x ); + + } + + break; + + } + + } + + if ( uv.y < 0 || uv.y > 1 ) { + + switch ( this.wrapT ) { + + case RepeatWrapping: + + uv.y = uv.y - Math.floor( uv.y ); + break; + + case ClampToEdgeWrapping: + + uv.y = uv.y < 0 ? 0 : 1; + break; + + case MirroredRepeatWrapping: + + if ( Math.abs( Math.floor( uv.y ) % 2 ) === 1 ) { + + uv.y = Math.ceil( uv.y ) - uv.y; + + } else { + + uv.y = uv.y - Math.floor( uv.y ); + + } + + break; + + } + + } + + if ( this.flipY ) { + + uv.y = 1 - uv.y; + + } + + return uv; + + } + + set needsUpdate( value ) { + + if ( value === true ) { + + this.version ++; + this.source.needsUpdate = true; + + } + + } + + set needsPMREMUpdate( value ) { + + if ( value === true ) { + + this.pmremVersion ++; + + } + + } + +} + +Texture.DEFAULT_IMAGE = null; +Texture.DEFAULT_MAPPING = UVMapping; +Texture.DEFAULT_ANISOTROPY = 1; + +class Vector4 { + + constructor( x = 0, y = 0, z = 0, w = 1 ) { + + Vector4.prototype.isVector4 = true; + + this.x = x; + this.y = y; + this.z = z; + this.w = w; + + } + + get width() { + + return this.z; + + } + + set width( value ) { + + this.z = value; + + } + + get height() { + + return this.w; + + } + + set height( value ) { + + this.w = value; + + } + + set( x, y, z, w ) { + + this.x = x; + this.y = y; + this.z = z; + this.w = w; + + return this; + + } + + setScalar( scalar ) { + + this.x = scalar; + this.y = scalar; + this.z = scalar; + this.w = scalar; + + return this; + + } + + setX( x ) { + + this.x = x; + + return this; + + } + + setY( y ) { + + this.y = y; + + return this; + + } + + setZ( z ) { + + this.z = z; + + return this; + + } + + setW( w ) { + + this.w = w; + + return this; + + } + + setComponent( index, value ) { + + switch ( index ) { + + case 0: this.x = value; break; + case 1: this.y = value; break; + case 2: this.z = value; break; + case 3: this.w = value; break; + default: throw new Error( 'index is out of range: ' + index ); + + } + + return this; + + } + + getComponent( index ) { + + switch ( index ) { + + case 0: return this.x; + case 1: return this.y; + case 2: return this.z; + case 3: return this.w; + default: throw new Error( 'index is out of range: ' + index ); + + } + + } + + clone() { + + return new this.constructor( this.x, this.y, this.z, this.w ); + + } + + copy( v ) { + + this.x = v.x; + this.y = v.y; + this.z = v.z; + this.w = ( v.w !== undefined ) ? v.w : 1; + + return this; + + } + + add( v ) { + + this.x += v.x; + this.y += v.y; + this.z += v.z; + this.w += v.w; + + return this; + + } + + addScalar( s ) { + + this.x += s; + this.y += s; + this.z += s; + this.w += s; + + return this; + + } + + addVectors( a, b ) { + + this.x = a.x + b.x; + this.y = a.y + b.y; + this.z = a.z + b.z; + this.w = a.w + b.w; + + return this; + + } + + addScaledVector( v, s ) { + + this.x += v.x * s; + this.y += v.y * s; + this.z += v.z * s; + this.w += v.w * s; + + return this; + + } + + sub( v ) { + + this.x -= v.x; + this.y -= v.y; + this.z -= v.z; + this.w -= v.w; + + return this; + + } + + subScalar( s ) { + + this.x -= s; + this.y -= s; + this.z -= s; + this.w -= s; + + return this; + + } + + subVectors( a, b ) { + + this.x = a.x - b.x; + this.y = a.y - b.y; + this.z = a.z - b.z; + this.w = a.w - b.w; + + return this; + + } + + multiply( v ) { + + this.x *= v.x; + this.y *= v.y; + this.z *= v.z; + this.w *= v.w; + + return this; + + } + + multiplyScalar( scalar ) { + + this.x *= scalar; + this.y *= scalar; + this.z *= scalar; + this.w *= scalar; + + return this; + + } + + applyMatrix4( m ) { + + const x = this.x, y = this.y, z = this.z, w = this.w; + const e = m.elements; + + this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] * w; + this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] * w; + this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] * w; + this.w = e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] * w; + + return this; + + } + + divideScalar( scalar ) { + + return this.multiplyScalar( 1 / scalar ); + + } + + setAxisAngleFromQuaternion( q ) { + + // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm + + // q is assumed to be normalized + + this.w = 2 * Math.acos( q.w ); + + const s = Math.sqrt( 1 - q.w * q.w ); + + if ( s < 0.0001 ) { + + this.x = 1; + this.y = 0; + this.z = 0; + + } else { + + this.x = q.x / s; + this.y = q.y / s; + this.z = q.z / s; + + } + + return this; + + } + + setAxisAngleFromRotationMatrix( m ) { + + // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm + + // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled) + + let angle, x, y, z; // variables for result + const epsilon = 0.01, // margin to allow for rounding errors + epsilon2 = 0.1, // margin to distinguish between 0 and 180 degrees + + te = m.elements, + + m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ], + m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ], + m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ]; + + if ( ( Math.abs( m12 - m21 ) < epsilon ) && + ( Math.abs( m13 - m31 ) < epsilon ) && + ( Math.abs( m23 - m32 ) < epsilon ) ) { + + // singularity found + // first check for identity matrix which must have +1 for all terms + // in leading diagonal and zero in other terms + + if ( ( Math.abs( m12 + m21 ) < epsilon2 ) && + ( Math.abs( m13 + m31 ) < epsilon2 ) && + ( Math.abs( m23 + m32 ) < epsilon2 ) && + ( Math.abs( m11 + m22 + m33 - 3 ) < epsilon2 ) ) { + + // this singularity is identity matrix so angle = 0 + + this.set( 1, 0, 0, 0 ); + + return this; // zero angle, arbitrary axis + + } + + // otherwise this singularity is angle = 180 + + angle = Math.PI; + + const xx = ( m11 + 1 ) / 2; + const yy = ( m22 + 1 ) / 2; + const zz = ( m33 + 1 ) / 2; + const xy = ( m12 + m21 ) / 4; + const xz = ( m13 + m31 ) / 4; + const yz = ( m23 + m32 ) / 4; + + if ( ( xx > yy ) && ( xx > zz ) ) { + + // m11 is the largest diagonal term + + if ( xx < epsilon ) { + + x = 0; + y = 0.707106781; + z = 0.707106781; + + } else { + + x = Math.sqrt( xx ); + y = xy / x; + z = xz / x; + + } + + } else if ( yy > zz ) { + + // m22 is the largest diagonal term + + if ( yy < epsilon ) { + + x = 0.707106781; + y = 0; + z = 0.707106781; + + } else { + + y = Math.sqrt( yy ); + x = xy / y; + z = yz / y; + + } + + } else { + + // m33 is the largest diagonal term so base result on this + + if ( zz < epsilon ) { + + x = 0.707106781; + y = 0.707106781; + z = 0; + + } else { + + z = Math.sqrt( zz ); + x = xz / z; + y = yz / z; + + } + + } + + this.set( x, y, z, angle ); + + return this; // return 180 deg rotation + + } + + // as we have reached here there are no singularities so we can handle normally + + let s = Math.sqrt( ( m32 - m23 ) * ( m32 - m23 ) + + ( m13 - m31 ) * ( m13 - m31 ) + + ( m21 - m12 ) * ( m21 - m12 ) ); // used to normalize + + if ( Math.abs( s ) < 0.001 ) s = 1; + + // prevent divide by zero, should not happen if matrix is orthogonal and should be + // caught by singularity test above, but I've left it in just in case + + this.x = ( m32 - m23 ) / s; + this.y = ( m13 - m31 ) / s; + this.z = ( m21 - m12 ) / s; + this.w = Math.acos( ( m11 + m22 + m33 - 1 ) / 2 ); + + return this; + + } + + min( v ) { + + this.x = Math.min( this.x, v.x ); + this.y = Math.min( this.y, v.y ); + this.z = Math.min( this.z, v.z ); + this.w = Math.min( this.w, v.w ); + + return this; + + } + + max( v ) { + + this.x = Math.max( this.x, v.x ); + this.y = Math.max( this.y, v.y ); + this.z = Math.max( this.z, v.z ); + this.w = Math.max( this.w, v.w ); + + return this; + + } + + clamp( min, max ) { + + // assumes min < max, componentwise + + this.x = Math.max( min.x, Math.min( max.x, this.x ) ); + this.y = Math.max( min.y, Math.min( max.y, this.y ) ); + this.z = Math.max( min.z, Math.min( max.z, this.z ) ); + this.w = Math.max( min.w, Math.min( max.w, this.w ) ); + + return this; + + } + + clampScalar( minVal, maxVal ) { + + this.x = Math.max( minVal, Math.min( maxVal, this.x ) ); + this.y = Math.max( minVal, Math.min( maxVal, this.y ) ); + this.z = Math.max( minVal, Math.min( maxVal, this.z ) ); + this.w = Math.max( minVal, Math.min( maxVal, this.w ) ); + + return this; + + } + + clampLength( min, max ) { + + const length = this.length(); + + return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) ); + + } + + floor() { + + this.x = Math.floor( this.x ); + this.y = Math.floor( this.y ); + this.z = Math.floor( this.z ); + this.w = Math.floor( this.w ); + + return this; + + } + + ceil() { + + this.x = Math.ceil( this.x ); + this.y = Math.ceil( this.y ); + this.z = Math.ceil( this.z ); + this.w = Math.ceil( this.w ); + + return this; + + } + + round() { + + this.x = Math.round( this.x ); + this.y = Math.round( this.y ); + this.z = Math.round( this.z ); + this.w = Math.round( this.w ); + + return this; + + } + + roundToZero() { + + this.x = Math.trunc( this.x ); + this.y = Math.trunc( this.y ); + this.z = Math.trunc( this.z ); + this.w = Math.trunc( this.w ); + + return this; + + } + + negate() { + + this.x = - this.x; + this.y = - this.y; + this.z = - this.z; + this.w = - this.w; + + return this; + + } + + dot( v ) { + + return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w; + + } + + lengthSq() { + + return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w; + + } + + length() { + + return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w ); + + } + + manhattanLength() { + + return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ) + Math.abs( this.w ); + + } + + normalize() { + + return this.divideScalar( this.length() || 1 ); + + } + + setLength( length ) { + + return this.normalize().multiplyScalar( length ); + + } + + lerp( v, alpha ) { + + this.x += ( v.x - this.x ) * alpha; + this.y += ( v.y - this.y ) * alpha; + this.z += ( v.z - this.z ) * alpha; + this.w += ( v.w - this.w ) * alpha; + + return this; + + } + + lerpVectors( v1, v2, alpha ) { + + this.x = v1.x + ( v2.x - v1.x ) * alpha; + this.y = v1.y + ( v2.y - v1.y ) * alpha; + this.z = v1.z + ( v2.z - v1.z ) * alpha; + this.w = v1.w + ( v2.w - v1.w ) * alpha; + + return this; + + } + + equals( v ) { + + return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) && ( v.w === this.w ) ); + + } + + fromArray( array, offset = 0 ) { + + this.x = array[ offset ]; + this.y = array[ offset + 1 ]; + this.z = array[ offset + 2 ]; + this.w = array[ offset + 3 ]; + + return this; + + } + + toArray( array = [], offset = 0 ) { + + array[ offset ] = this.x; + array[ offset + 1 ] = this.y; + array[ offset + 2 ] = this.z; + array[ offset + 3 ] = this.w; + + return array; + + } + + fromBufferAttribute( attribute, index ) { + + this.x = attribute.getX( index ); + this.y = attribute.getY( index ); + this.z = attribute.getZ( index ); + this.w = attribute.getW( index ); + + return this; + + } + + random() { + + this.x = Math.random(); + this.y = Math.random(); + this.z = Math.random(); + this.w = Math.random(); + + return this; + + } + + *[ Symbol.iterator ]() { + + yield this.x; + yield this.y; + yield this.z; + yield this.w; + + } + +} + +/* + In options, we can specify: + * Texture parameters for an auto-generated target texture + * depthBuffer/stencilBuffer: Booleans to indicate if we should generate these buffers +*/ +class RenderTarget extends EventDispatcher { + + constructor( width = 1, height = 1, options = {} ) { + + super(); + + this.isRenderTarget = true; + + this.width = width; + this.height = height; + this.depth = 1; + + this.scissor = new Vector4( 0, 0, width, height ); + this.scissorTest = false; + + this.viewport = new Vector4( 0, 0, width, height ); + + const image = { width: width, height: height, depth: 1 }; + + options = Object.assign( { + generateMipmaps: false, + internalFormat: null, + minFilter: LinearFilter, + depthBuffer: true, + stencilBuffer: false, + resolveDepthBuffer: true, + resolveStencilBuffer: true, + depthTexture: null, + samples: 0, + count: 1 + }, options ); + + const texture = new Texture( image, options.mapping, options.wrapS, options.wrapT, options.magFilter, options.minFilter, options.format, options.type, options.anisotropy, options.colorSpace ); + + texture.flipY = false; + texture.generateMipmaps = options.generateMipmaps; + texture.internalFormat = options.internalFormat; + + this.textures = []; + + const count = options.count; + for ( let i = 0; i < count; i ++ ) { + + this.textures[ i ] = texture.clone(); + this.textures[ i ].isRenderTargetTexture = true; + + } + + this.depthBuffer = options.depthBuffer; + this.stencilBuffer = options.stencilBuffer; + + this.resolveDepthBuffer = options.resolveDepthBuffer; + this.resolveStencilBuffer = options.resolveStencilBuffer; + + this.depthTexture = options.depthTexture; + + this.samples = options.samples; + + } + + get texture() { + + return this.textures[ 0 ]; + + } + + set texture( value ) { + + this.textures[ 0 ] = value; + + } + + setSize( width, height, depth = 1 ) { + + if ( this.width !== width || this.height !== height || this.depth !== depth ) { + + this.width = width; + this.height = height; + this.depth = depth; + + for ( let i = 0, il = this.textures.length; i < il; i ++ ) { + + this.textures[ i ].image.width = width; + this.textures[ i ].image.height = height; + this.textures[ i ].image.depth = depth; + + } + + this.dispose(); + + } + + this.viewport.set( 0, 0, width, height ); + this.scissor.set( 0, 0, width, height ); + + } + + clone() { + + return new this.constructor().copy( this ); + + } + + copy( source ) { + + this.width = source.width; + this.height = source.height; + this.depth = source.depth; + + this.scissor.copy( source.scissor ); + this.scissorTest = source.scissorTest; + + this.viewport.copy( source.viewport ); + + this.textures.length = 0; + + for ( let i = 0, il = source.textures.length; i < il; i ++ ) { + + this.textures[ i ] = source.textures[ i ].clone(); + this.textures[ i ].isRenderTargetTexture = true; + + } + + // ensure image object is not shared, see #20328 + + const image = Object.assign( {}, source.texture.image ); + this.texture.source = new Source( image ); + + this.depthBuffer = source.depthBuffer; + this.stencilBuffer = source.stencilBuffer; + + this.resolveDepthBuffer = source.resolveDepthBuffer; + this.resolveStencilBuffer = source.resolveStencilBuffer; + + if ( source.depthTexture !== null ) this.depthTexture = source.depthTexture.clone(); + + this.samples = source.samples; + + return this; + + } + + dispose() { + + this.dispatchEvent( { type: 'dispose' } ); + + } + +} + +class WebGLRenderTarget extends RenderTarget { + + constructor( width = 1, height = 1, options = {} ) { + + super( width, height, options ); + + this.isWebGLRenderTarget = true; + + } + +} + +class DataArrayTexture extends Texture { + + constructor( data = null, width = 1, height = 1, depth = 1 ) { + + super( null ); + + this.isDataArrayTexture = true; + + this.image = { data, width, height, depth }; + + this.magFilter = NearestFilter; + this.minFilter = NearestFilter; + + this.wrapR = ClampToEdgeWrapping; + + this.generateMipmaps = false; + this.flipY = false; + this.unpackAlignment = 1; + + } + +} + +class WebGLArrayRenderTarget extends WebGLRenderTarget { + + constructor( width = 1, height = 1, depth = 1, options = {} ) { + + super( width, height, options ); + + this.isWebGLArrayRenderTarget = true; + + this.depth = depth; + + this.texture = new DataArrayTexture( null, width, height, depth ); + + this.texture.isRenderTargetTexture = true; + + } + +} + +class Data3DTexture extends Texture { + + constructor( data = null, width = 1, height = 1, depth = 1 ) { + + // We're going to add .setXXX() methods for setting properties later. + // Users can still set in DataTexture3D directly. + // + // const texture = new THREE.DataTexture3D( data, width, height, depth ); + // texture.anisotropy = 16; + // + // See #14839 + + super( null ); + + this.isData3DTexture = true; + + this.image = { data, width, height, depth }; + + this.magFilter = NearestFilter; + this.minFilter = NearestFilter; + + this.wrapR = ClampToEdgeWrapping; + + this.generateMipmaps = false; + this.flipY = false; + this.unpackAlignment = 1; + + } + +} + +class WebGL3DRenderTarget extends WebGLRenderTarget { + + constructor( width = 1, height = 1, depth = 1, options = {} ) { + + super( width, height, options ); + + this.isWebGL3DRenderTarget = true; + + this.depth = depth; + + this.texture = new Data3DTexture( null, width, height, depth ); + + this.texture.isRenderTargetTexture = true; + + } + +} + +class Quaternion { + + constructor( x = 0, y = 0, z = 0, w = 1 ) { + + this.isQuaternion = true; + + this._x = x; + this._y = y; + this._z = z; + this._w = w; + + } + + static slerpFlat( dst, dstOffset, src0, srcOffset0, src1, srcOffset1, t ) { + + // fuzz-free, array-based Quaternion SLERP operation + + let x0 = src0[ srcOffset0 + 0 ], + y0 = src0[ srcOffset0 + 1 ], + z0 = src0[ srcOffset0 + 2 ], + w0 = src0[ srcOffset0 + 3 ]; + + const x1 = src1[ srcOffset1 + 0 ], + y1 = src1[ srcOffset1 + 1 ], + z1 = src1[ srcOffset1 + 2 ], + w1 = src1[ srcOffset1 + 3 ]; + + if ( t === 0 ) { + + dst[ dstOffset + 0 ] = x0; + dst[ dstOffset + 1 ] = y0; + dst[ dstOffset + 2 ] = z0; + dst[ dstOffset + 3 ] = w0; + return; + + } + + if ( t === 1 ) { + + dst[ dstOffset + 0 ] = x1; + dst[ dstOffset + 1 ] = y1; + dst[ dstOffset + 2 ] = z1; + dst[ dstOffset + 3 ] = w1; + return; + + } + + if ( w0 !== w1 || x0 !== x1 || y0 !== y1 || z0 !== z1 ) { + + let s = 1 - t; + const cos = x0 * x1 + y0 * y1 + z0 * z1 + w0 * w1, + dir = ( cos >= 0 ? 1 : - 1 ), + sqrSin = 1 - cos * cos; + + // Skip the Slerp for tiny steps to avoid numeric problems: + if ( sqrSin > Number.EPSILON ) { + + const sin = Math.sqrt( sqrSin ), + len = Math.atan2( sin, cos * dir ); + + s = Math.sin( s * len ) / sin; + t = Math.sin( t * len ) / sin; + + } + + const tDir = t * dir; + + x0 = x0 * s + x1 * tDir; + y0 = y0 * s + y1 * tDir; + z0 = z0 * s + z1 * tDir; + w0 = w0 * s + w1 * tDir; + + // Normalize in case we just did a lerp: + if ( s === 1 - t ) { + + const f = 1 / Math.sqrt( x0 * x0 + y0 * y0 + z0 * z0 + w0 * w0 ); + + x0 *= f; + y0 *= f; + z0 *= f; + w0 *= f; + + } + + } + + dst[ dstOffset ] = x0; + dst[ dstOffset + 1 ] = y0; + dst[ dstOffset + 2 ] = z0; + dst[ dstOffset + 3 ] = w0; + + } + + static multiplyQuaternionsFlat( dst, dstOffset, src0, srcOffset0, src1, srcOffset1 ) { + + const x0 = src0[ srcOffset0 ]; + const y0 = src0[ srcOffset0 + 1 ]; + const z0 = src0[ srcOffset0 + 2 ]; + const w0 = src0[ srcOffset0 + 3 ]; + + const x1 = src1[ srcOffset1 ]; + const y1 = src1[ srcOffset1 + 1 ]; + const z1 = src1[ srcOffset1 + 2 ]; + const w1 = src1[ srcOffset1 + 3 ]; + + dst[ dstOffset ] = x0 * w1 + w0 * x1 + y0 * z1 - z0 * y1; + dst[ dstOffset + 1 ] = y0 * w1 + w0 * y1 + z0 * x1 - x0 * z1; + dst[ dstOffset + 2 ] = z0 * w1 + w0 * z1 + x0 * y1 - y0 * x1; + dst[ dstOffset + 3 ] = w0 * w1 - x0 * x1 - y0 * y1 - z0 * z1; + + return dst; + + } + + get x() { + + return this._x; + + } + + set x( value ) { + + this._x = value; + this._onChangeCallback(); + + } + + get y() { + + return this._y; + + } + + set y( value ) { + + this._y = value; + this._onChangeCallback(); + + } + + get z() { + + return this._z; + + } + + set z( value ) { + + this._z = value; + this._onChangeCallback(); + + } + + get w() { + + return this._w; + + } + + set w( value ) { + + this._w = value; + this._onChangeCallback(); + + } + + set( x, y, z, w ) { + + this._x = x; + this._y = y; + this._z = z; + this._w = w; + + this._onChangeCallback(); + + return this; + + } + + clone() { + + return new this.constructor( this._x, this._y, this._z, this._w ); + + } + + copy( quaternion ) { + + this._x = quaternion.x; + this._y = quaternion.y; + this._z = quaternion.z; + this._w = quaternion.w; + + this._onChangeCallback(); + + return this; + + } + + setFromEuler( euler, update = true ) { + + const x = euler._x, y = euler._y, z = euler._z, order = euler._order; + + // http://www.mathworks.com/matlabcentral/fileexchange/ + // 20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/ + // content/SpinCalc.m + + const cos = Math.cos; + const sin = Math.sin; + + const c1 = cos( x / 2 ); + const c2 = cos( y / 2 ); + const c3 = cos( z / 2 ); + + const s1 = sin( x / 2 ); + const s2 = sin( y / 2 ); + const s3 = sin( z / 2 ); + + switch ( order ) { + + case 'XYZ': + this._x = s1 * c2 * c3 + c1 * s2 * s3; + this._y = c1 * s2 * c3 - s1 * c2 * s3; + this._z = c1 * c2 * s3 + s1 * s2 * c3; + this._w = c1 * c2 * c3 - s1 * s2 * s3; + break; + + case 'YXZ': + this._x = s1 * c2 * c3 + c1 * s2 * s3; + this._y = c1 * s2 * c3 - s1 * c2 * s3; + this._z = c1 * c2 * s3 - s1 * s2 * c3; + this._w = c1 * c2 * c3 + s1 * s2 * s3; + break; + + case 'ZXY': + this._x = s1 * c2 * c3 - c1 * s2 * s3; + this._y = c1 * s2 * c3 + s1 * c2 * s3; + this._z = c1 * c2 * s3 + s1 * s2 * c3; + this._w = c1 * c2 * c3 - s1 * s2 * s3; + break; + + case 'ZYX': + this._x = s1 * c2 * c3 - c1 * s2 * s3; + this._y = c1 * s2 * c3 + s1 * c2 * s3; + this._z = c1 * c2 * s3 - s1 * s2 * c3; + this._w = c1 * c2 * c3 + s1 * s2 * s3; + break; + + case 'YZX': + this._x = s1 * c2 * c3 + c1 * s2 * s3; + this._y = c1 * s2 * c3 + s1 * c2 * s3; + this._z = c1 * c2 * s3 - s1 * s2 * c3; + this._w = c1 * c2 * c3 - s1 * s2 * s3; + break; + + case 'XZY': + this._x = s1 * c2 * c3 - c1 * s2 * s3; + this._y = c1 * s2 * c3 - s1 * c2 * s3; + this._z = c1 * c2 * s3 + s1 * s2 * c3; + this._w = c1 * c2 * c3 + s1 * s2 * s3; + break; + + default: + console.warn( 'THREE.Quaternion: .setFromEuler() encountered an unknown order: ' + order ); + + } + + if ( update === true ) this._onChangeCallback(); + + return this; + + } + + setFromAxisAngle( axis, angle ) { + + // http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm + + // assumes axis is normalized + + const halfAngle = angle / 2, s = Math.sin( halfAngle ); + + this._x = axis.x * s; + this._y = axis.y * s; + this._z = axis.z * s; + this._w = Math.cos( halfAngle ); + + this._onChangeCallback(); + + return this; + + } + + setFromRotationMatrix( m ) { + + // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm + + // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled) + + const te = m.elements, + + m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ], + m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ], + m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ], + + trace = m11 + m22 + m33; + + if ( trace > 0 ) { + + const s = 0.5 / Math.sqrt( trace + 1.0 ); + + this._w = 0.25 / s; + this._x = ( m32 - m23 ) * s; + this._y = ( m13 - m31 ) * s; + this._z = ( m21 - m12 ) * s; + + } else if ( m11 > m22 && m11 > m33 ) { + + const s = 2.0 * Math.sqrt( 1.0 + m11 - m22 - m33 ); + + this._w = ( m32 - m23 ) / s; + this._x = 0.25 * s; + this._y = ( m12 + m21 ) / s; + this._z = ( m13 + m31 ) / s; + + } else if ( m22 > m33 ) { + + const s = 2.0 * Math.sqrt( 1.0 + m22 - m11 - m33 ); + + this._w = ( m13 - m31 ) / s; + this._x = ( m12 + m21 ) / s; + this._y = 0.25 * s; + this._z = ( m23 + m32 ) / s; + + } else { + + const s = 2.0 * Math.sqrt( 1.0 + m33 - m11 - m22 ); + + this._w = ( m21 - m12 ) / s; + this._x = ( m13 + m31 ) / s; + this._y = ( m23 + m32 ) / s; + this._z = 0.25 * s; + + } + + this._onChangeCallback(); + + return this; + + } + + setFromUnitVectors( vFrom, vTo ) { + + // assumes direction vectors vFrom and vTo are normalized + + let r = vFrom.dot( vTo ) + 1; + + if ( r < Number.EPSILON ) { + + // vFrom and vTo point in opposite directions + + r = 0; + + if ( Math.abs( vFrom.x ) > Math.abs( vFrom.z ) ) { + + this._x = - vFrom.y; + this._y = vFrom.x; + this._z = 0; + this._w = r; + + } else { + + this._x = 0; + this._y = - vFrom.z; + this._z = vFrom.y; + this._w = r; + + } + + } else { + + // crossVectors( vFrom, vTo ); // inlined to avoid cyclic dependency on Vector3 + + this._x = vFrom.y * vTo.z - vFrom.z * vTo.y; + this._y = vFrom.z * vTo.x - vFrom.x * vTo.z; + this._z = vFrom.x * vTo.y - vFrom.y * vTo.x; + this._w = r; + + } + + return this.normalize(); + + } + + angleTo( q ) { + + return 2 * Math.acos( Math.abs( clamp( this.dot( q ), - 1, 1 ) ) ); + + } + + rotateTowards( q, step ) { + + const angle = this.angleTo( q ); + + if ( angle === 0 ) return this; + + const t = Math.min( 1, step / angle ); + + this.slerp( q, t ); + + return this; + + } + + identity() { + + return this.set( 0, 0, 0, 1 ); + + } + + invert() { + + // quaternion is assumed to have unit length + + return this.conjugate(); + + } + + conjugate() { + + this._x *= - 1; + this._y *= - 1; + this._z *= - 1; + + this._onChangeCallback(); + + return this; + + } + + dot( v ) { + + return this._x * v._x + this._y * v._y + this._z * v._z + this._w * v._w; + + } + + lengthSq() { + + return this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w; + + } + + length() { + + return Math.sqrt( this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w ); + + } + + normalize() { + + let l = this.length(); + + if ( l === 0 ) { + + this._x = 0; + this._y = 0; + this._z = 0; + this._w = 1; + + } else { + + l = 1 / l; + + this._x = this._x * l; + this._y = this._y * l; + this._z = this._z * l; + this._w = this._w * l; + + } + + this._onChangeCallback(); + + return this; + + } + + multiply( q ) { + + return this.multiplyQuaternions( this, q ); + + } + + premultiply( q ) { + + return this.multiplyQuaternions( q, this ); + + } + + multiplyQuaternions( a, b ) { + + // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm + + const qax = a._x, qay = a._y, qaz = a._z, qaw = a._w; + const qbx = b._x, qby = b._y, qbz = b._z, qbw = b._w; + + this._x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby; + this._y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz; + this._z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx; + this._w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz; + + this._onChangeCallback(); + + return this; + + } + + slerp( qb, t ) { + + if ( t === 0 ) return this; + if ( t === 1 ) return this.copy( qb ); + + const x = this._x, y = this._y, z = this._z, w = this._w; + + // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/ + + let cosHalfTheta = w * qb._w + x * qb._x + y * qb._y + z * qb._z; + + if ( cosHalfTheta < 0 ) { + + this._w = - qb._w; + this._x = - qb._x; + this._y = - qb._y; + this._z = - qb._z; + + cosHalfTheta = - cosHalfTheta; + + } else { + + this.copy( qb ); + + } + + if ( cosHalfTheta >= 1.0 ) { + + this._w = w; + this._x = x; + this._y = y; + this._z = z; + + return this; + + } + + const sqrSinHalfTheta = 1.0 - cosHalfTheta * cosHalfTheta; + + if ( sqrSinHalfTheta <= Number.EPSILON ) { + + const s = 1 - t; + this._w = s * w + t * this._w; + this._x = s * x + t * this._x; + this._y = s * y + t * this._y; + this._z = s * z + t * this._z; + + this.normalize(); // normalize calls _onChangeCallback() + + return this; + + } + + const sinHalfTheta = Math.sqrt( sqrSinHalfTheta ); + const halfTheta = Math.atan2( sinHalfTheta, cosHalfTheta ); + const ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta, + ratioB = Math.sin( t * halfTheta ) / sinHalfTheta; + + this._w = ( w * ratioA + this._w * ratioB ); + this._x = ( x * ratioA + this._x * ratioB ); + this._y = ( y * ratioA + this._y * ratioB ); + this._z = ( z * ratioA + this._z * ratioB ); + + this._onChangeCallback(); + + return this; + + } + + slerpQuaternions( qa, qb, t ) { + + return this.copy( qa ).slerp( qb, t ); + + } + + random() { + + // sets this quaternion to a uniform random unit quaternnion + + // Ken Shoemake + // Uniform random rotations + // D. Kirk, editor, Graphics Gems III, pages 124-132. Academic Press, New York, 1992. + + const theta1 = 2 * Math.PI * Math.random(); + const theta2 = 2 * Math.PI * Math.random(); + + const x0 = Math.random(); + const r1 = Math.sqrt( 1 - x0 ); + const r2 = Math.sqrt( x0 ); + + return this.set( + r1 * Math.sin( theta1 ), + r1 * Math.cos( theta1 ), + r2 * Math.sin( theta2 ), + r2 * Math.cos( theta2 ), + ); + + } + + equals( quaternion ) { + + return ( quaternion._x === this._x ) && ( quaternion._y === this._y ) && ( quaternion._z === this._z ) && ( quaternion._w === this._w ); + + } + + fromArray( array, offset = 0 ) { + + this._x = array[ offset ]; + this._y = array[ offset + 1 ]; + this._z = array[ offset + 2 ]; + this._w = array[ offset + 3 ]; + + this._onChangeCallback(); + + return this; + + } + + toArray( array = [], offset = 0 ) { + + array[ offset ] = this._x; + array[ offset + 1 ] = this._y; + array[ offset + 2 ] = this._z; + array[ offset + 3 ] = this._w; + + return array; + + } + + fromBufferAttribute( attribute, index ) { + + this._x = attribute.getX( index ); + this._y = attribute.getY( index ); + this._z = attribute.getZ( index ); + this._w = attribute.getW( index ); + + this._onChangeCallback(); + + return this; + + } + + toJSON() { + + return this.toArray(); + + } + + _onChange( callback ) { + + this._onChangeCallback = callback; + + return this; + + } + + _onChangeCallback() {} + + *[ Symbol.iterator ]() { + + yield this._x; + yield this._y; + yield this._z; + yield this._w; + + } + +} + +class Vector3 { + + constructor( x = 0, y = 0, z = 0 ) { + + Vector3.prototype.isVector3 = true; + + this.x = x; + this.y = y; + this.z = z; + + } + + set( x, y, z ) { + + if ( z === undefined ) z = this.z; // sprite.scale.set(x,y) + + this.x = x; + this.y = y; + this.z = z; + + return this; + + } + + setScalar( scalar ) { + + this.x = scalar; + this.y = scalar; + this.z = scalar; + + return this; + + } + + setX( x ) { + + this.x = x; + + return this; + + } + + setY( y ) { + + this.y = y; + + return this; + + } + + setZ( z ) { + + this.z = z; + + return this; + + } + + setComponent( index, value ) { + + switch ( index ) { + + case 0: this.x = value; break; + case 1: this.y = value; break; + case 2: this.z = value; break; + default: throw new Error( 'index is out of range: ' + index ); + + } + + return this; + + } + + getComponent( index ) { + + switch ( index ) { + + case 0: return this.x; + case 1: return this.y; + case 2: return this.z; + default: throw new Error( 'index is out of range: ' + index ); + + } + + } + + clone() { + + return new this.constructor( this.x, this.y, this.z ); + + } + + copy( v ) { + + this.x = v.x; + this.y = v.y; + this.z = v.z; + + return this; + + } + + add( v ) { + + this.x += v.x; + this.y += v.y; + this.z += v.z; + + return this; + + } + + addScalar( s ) { + + this.x += s; + this.y += s; + this.z += s; + + return this; + + } + + addVectors( a, b ) { + + this.x = a.x + b.x; + this.y = a.y + b.y; + this.z = a.z + b.z; + + return this; + + } + + addScaledVector( v, s ) { + + this.x += v.x * s; + this.y += v.y * s; + this.z += v.z * s; + + return this; + + } + + sub( v ) { + + this.x -= v.x; + this.y -= v.y; + this.z -= v.z; + + return this; + + } + + subScalar( s ) { + + this.x -= s; + this.y -= s; + this.z -= s; + + return this; + + } + + subVectors( a, b ) { + + this.x = a.x - b.x; + this.y = a.y - b.y; + this.z = a.z - b.z; + + return this; + + } + + multiply( v ) { + + this.x *= v.x; + this.y *= v.y; + this.z *= v.z; + + return this; + + } + + multiplyScalar( scalar ) { + + this.x *= scalar; + this.y *= scalar; + this.z *= scalar; + + return this; + + } + + multiplyVectors( a, b ) { + + this.x = a.x * b.x; + this.y = a.y * b.y; + this.z = a.z * b.z; + + return this; + + } + + applyEuler( euler ) { + + return this.applyQuaternion( _quaternion$4.setFromEuler( euler ) ); + + } + + applyAxisAngle( axis, angle ) { + + return this.applyQuaternion( _quaternion$4.setFromAxisAngle( axis, angle ) ); + + } + + applyMatrix3( m ) { + + const x = this.x, y = this.y, z = this.z; + const e = m.elements; + + this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ] * z; + this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ] * z; + this.z = e[ 2 ] * x + e[ 5 ] * y + e[ 8 ] * z; + + return this; + + } + + applyNormalMatrix( m ) { + + return this.applyMatrix3( m ).normalize(); + + } + + applyMatrix4( m ) { + + const x = this.x, y = this.y, z = this.z; + const e = m.elements; + + const w = 1 / ( e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] ); + + this.x = ( e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] ) * w; + this.y = ( e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] ) * w; + this.z = ( e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] ) * w; + + return this; + + } + + applyQuaternion( q ) { + + // quaternion q is assumed to have unit length + + const vx = this.x, vy = this.y, vz = this.z; + const qx = q.x, qy = q.y, qz = q.z, qw = q.w; + + // t = 2 * cross( q.xyz, v ); + const tx = 2 * ( qy * vz - qz * vy ); + const ty = 2 * ( qz * vx - qx * vz ); + const tz = 2 * ( qx * vy - qy * vx ); + + // v + q.w * t + cross( q.xyz, t ); + this.x = vx + qw * tx + qy * tz - qz * ty; + this.y = vy + qw * ty + qz * tx - qx * tz; + this.z = vz + qw * tz + qx * ty - qy * tx; + + return this; + + } + + project( camera ) { + + return this.applyMatrix4( camera.matrixWorldInverse ).applyMatrix4( camera.projectionMatrix ); + + } + + unproject( camera ) { + + return this.applyMatrix4( camera.projectionMatrixInverse ).applyMatrix4( camera.matrixWorld ); + + } + + transformDirection( m ) { + + // input: THREE.Matrix4 affine matrix + // vector interpreted as a direction + + const x = this.x, y = this.y, z = this.z; + const e = m.elements; + + this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z; + this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z; + this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z; + + return this.normalize(); + + } + + divide( v ) { + + this.x /= v.x; + this.y /= v.y; + this.z /= v.z; + + return this; + + } + + divideScalar( scalar ) { + + return this.multiplyScalar( 1 / scalar ); + + } + + min( v ) { + + this.x = Math.min( this.x, v.x ); + this.y = Math.min( this.y, v.y ); + this.z = Math.min( this.z, v.z ); + + return this; + + } + + max( v ) { + + this.x = Math.max( this.x, v.x ); + this.y = Math.max( this.y, v.y ); + this.z = Math.max( this.z, v.z ); + + return this; + + } + + clamp( min, max ) { + + // assumes min < max, componentwise + + this.x = Math.max( min.x, Math.min( max.x, this.x ) ); + this.y = Math.max( min.y, Math.min( max.y, this.y ) ); + this.z = Math.max( min.z, Math.min( max.z, this.z ) ); + + return this; + + } + + clampScalar( minVal, maxVal ) { + + this.x = Math.max( minVal, Math.min( maxVal, this.x ) ); + this.y = Math.max( minVal, Math.min( maxVal, this.y ) ); + this.z = Math.max( minVal, Math.min( maxVal, this.z ) ); + + return this; + + } + + clampLength( min, max ) { + + const length = this.length(); + + return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) ); + + } + + floor() { + + this.x = Math.floor( this.x ); + this.y = Math.floor( this.y ); + this.z = Math.floor( this.z ); + + return this; + + } + + ceil() { + + this.x = Math.ceil( this.x ); + this.y = Math.ceil( this.y ); + this.z = Math.ceil( this.z ); + + return this; + + } + + round() { + + this.x = Math.round( this.x ); + this.y = Math.round( this.y ); + this.z = Math.round( this.z ); + + return this; + + } + + roundToZero() { + + this.x = Math.trunc( this.x ); + this.y = Math.trunc( this.y ); + this.z = Math.trunc( this.z ); + + return this; + + } + + negate() { + + this.x = - this.x; + this.y = - this.y; + this.z = - this.z; + + return this; + + } + + dot( v ) { + + return this.x * v.x + this.y * v.y + this.z * v.z; + + } + + // TODO lengthSquared? + + lengthSq() { + + return this.x * this.x + this.y * this.y + this.z * this.z; + + } + + length() { + + return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z ); + + } + + manhattanLength() { + + return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ); + + } + + normalize() { + + return this.divideScalar( this.length() || 1 ); + + } + + setLength( length ) { + + return this.normalize().multiplyScalar( length ); + + } + + lerp( v, alpha ) { + + this.x += ( v.x - this.x ) * alpha; + this.y += ( v.y - this.y ) * alpha; + this.z += ( v.z - this.z ) * alpha; + + return this; + + } + + lerpVectors( v1, v2, alpha ) { + + this.x = v1.x + ( v2.x - v1.x ) * alpha; + this.y = v1.y + ( v2.y - v1.y ) * alpha; + this.z = v1.z + ( v2.z - v1.z ) * alpha; + + return this; + + } + + cross( v ) { + + return this.crossVectors( this, v ); + + } + + crossVectors( a, b ) { + + const ax = a.x, ay = a.y, az = a.z; + const bx = b.x, by = b.y, bz = b.z; + + this.x = ay * bz - az * by; + this.y = az * bx - ax * bz; + this.z = ax * by - ay * bx; + + return this; + + } + + projectOnVector( v ) { + + const denominator = v.lengthSq(); + + if ( denominator === 0 ) return this.set( 0, 0, 0 ); + + const scalar = v.dot( this ) / denominator; + + return this.copy( v ).multiplyScalar( scalar ); + + } + + projectOnPlane( planeNormal ) { + + _vector$c.copy( this ).projectOnVector( planeNormal ); + + return this.sub( _vector$c ); + + } + + reflect( normal ) { + + // reflect incident vector off plane orthogonal to normal + // normal is assumed to have unit length + + return this.sub( _vector$c.copy( normal ).multiplyScalar( 2 * this.dot( normal ) ) ); + + } + + angleTo( v ) { + + const denominator = Math.sqrt( this.lengthSq() * v.lengthSq() ); + + if ( denominator === 0 ) return Math.PI / 2; + + const theta = this.dot( v ) / denominator; + + // clamp, to handle numerical problems + + return Math.acos( clamp( theta, - 1, 1 ) ); + + } + + distanceTo( v ) { + + return Math.sqrt( this.distanceToSquared( v ) ); + + } + + distanceToSquared( v ) { + + const dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z; + + return dx * dx + dy * dy + dz * dz; + + } + + manhattanDistanceTo( v ) { + + return Math.abs( this.x - v.x ) + Math.abs( this.y - v.y ) + Math.abs( this.z - v.z ); + + } + + setFromSpherical( s ) { + + return this.setFromSphericalCoords( s.radius, s.phi, s.theta ); + + } + + setFromSphericalCoords( radius, phi, theta ) { + + const sinPhiRadius = Math.sin( phi ) * radius; + + this.x = sinPhiRadius * Math.sin( theta ); + this.y = Math.cos( phi ) * radius; + this.z = sinPhiRadius * Math.cos( theta ); + + return this; + + } + + setFromCylindrical( c ) { + + return this.setFromCylindricalCoords( c.radius, c.theta, c.y ); + + } + + setFromCylindricalCoords( radius, theta, y ) { + + this.x = radius * Math.sin( theta ); + this.y = y; + this.z = radius * Math.cos( theta ); + + return this; + + } + + setFromMatrixPosition( m ) { + + const e = m.elements; + + this.x = e[ 12 ]; + this.y = e[ 13 ]; + this.z = e[ 14 ]; + + return this; + + } + + setFromMatrixScale( m ) { + + const sx = this.setFromMatrixColumn( m, 0 ).length(); + const sy = this.setFromMatrixColumn( m, 1 ).length(); + const sz = this.setFromMatrixColumn( m, 2 ).length(); + + this.x = sx; + this.y = sy; + this.z = sz; + + return this; + + } + + setFromMatrixColumn( m, index ) { + + return this.fromArray( m.elements, index * 4 ); + + } + + setFromMatrix3Column( m, index ) { + + return this.fromArray( m.elements, index * 3 ); + + } + + setFromEuler( e ) { + + this.x = e._x; + this.y = e._y; + this.z = e._z; + + return this; + + } + + setFromColor( c ) { + + this.x = c.r; + this.y = c.g; + this.z = c.b; + + return this; + + } + + equals( v ) { + + return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) ); + + } + + fromArray( array, offset = 0 ) { + + this.x = array[ offset ]; + this.y = array[ offset + 1 ]; + this.z = array[ offset + 2 ]; + + return this; + + } + + toArray( array = [], offset = 0 ) { + + array[ offset ] = this.x; + array[ offset + 1 ] = this.y; + array[ offset + 2 ] = this.z; + + return array; + + } + + fromBufferAttribute( attribute, index ) { + + this.x = attribute.getX( index ); + this.y = attribute.getY( index ); + this.z = attribute.getZ( index ); + + return this; + + } + + random() { + + this.x = Math.random(); + this.y = Math.random(); + this.z = Math.random(); + + return this; + + } + + randomDirection() { + + // https://mathworld.wolfram.com/SpherePointPicking.html + + const theta = Math.random() * Math.PI * 2; + const u = Math.random() * 2 - 1; + const c = Math.sqrt( 1 - u * u ); + + this.x = c * Math.cos( theta ); + this.y = u; + this.z = c * Math.sin( theta ); + + return this; + + } + + *[ Symbol.iterator ]() { + + yield this.x; + yield this.y; + yield this.z; + + } + +} + +const _vector$c = /*@__PURE__*/ new Vector3(); +const _quaternion$4 = /*@__PURE__*/ new Quaternion(); + +class Box3 { + + constructor( min = new Vector3( + Infinity, + Infinity, + Infinity ), max = new Vector3( - Infinity, - Infinity, - Infinity ) ) { + + this.isBox3 = true; + + this.min = min; + this.max = max; + + } + + set( min, max ) { + + this.min.copy( min ); + this.max.copy( max ); + + return this; + + } + + setFromArray( array ) { + + this.makeEmpty(); + + for ( let i = 0, il = array.length; i < il; i += 3 ) { + + this.expandByPoint( _vector$b.fromArray( array, i ) ); + + } + + return this; + + } + + setFromBufferAttribute( attribute ) { + + this.makeEmpty(); + + for ( let i = 0, il = attribute.count; i < il; i ++ ) { + + this.expandByPoint( _vector$b.fromBufferAttribute( attribute, i ) ); + + } + + return this; + + } + + setFromPoints( points ) { + + this.makeEmpty(); + + for ( let i = 0, il = points.length; i < il; i ++ ) { + + this.expandByPoint( points[ i ] ); + + } + + return this; + + } + + setFromCenterAndSize( center, size ) { + + const halfSize = _vector$b.copy( size ).multiplyScalar( 0.5 ); + + this.min.copy( center ).sub( halfSize ); + this.max.copy( center ).add( halfSize ); + + return this; + + } + + setFromObject( object, precise = false ) { + + this.makeEmpty(); + + return this.expandByObject( object, precise ); + + } + + clone() { + + return new this.constructor().copy( this ); + + } + + copy( box ) { + + this.min.copy( box.min ); + this.max.copy( box.max ); + + return this; + + } + + makeEmpty() { + + this.min.x = this.min.y = this.min.z = + Infinity; + this.max.x = this.max.y = this.max.z = - Infinity; + + return this; + + } + + isEmpty() { + + // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes + + return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ) || ( this.max.z < this.min.z ); + + } + + getCenter( target ) { + + return this.isEmpty() ? target.set( 0, 0, 0 ) : target.addVectors( this.min, this.max ).multiplyScalar( 0.5 ); + + } + + getSize( target ) { + + return this.isEmpty() ? target.set( 0, 0, 0 ) : target.subVectors( this.max, this.min ); + + } + + expandByPoint( point ) { + + this.min.min( point ); + this.max.max( point ); + + return this; + + } + + expandByVector( vector ) { + + this.min.sub( vector ); + this.max.add( vector ); + + return this; + + } + + expandByScalar( scalar ) { + + this.min.addScalar( - scalar ); + this.max.addScalar( scalar ); + + return this; + + } + + expandByObject( object, precise = false ) { + + // Computes the world-axis-aligned bounding box of an object (including its children), + // accounting for both the object's, and children's, world transforms + + object.updateWorldMatrix( false, false ); + + const geometry = object.geometry; + + if ( geometry !== undefined ) { + + const positionAttribute = geometry.getAttribute( 'position' ); + + // precise AABB computation based on vertex data requires at least a position attribute. + // instancing isn't supported so far and uses the normal (conservative) code path. + + if ( precise === true && positionAttribute !== undefined && object.isInstancedMesh !== true ) { + + for ( let i = 0, l = positionAttribute.count; i < l; i ++ ) { + + if ( object.isMesh === true ) { + + object.getVertexPosition( i, _vector$b ); + + } else { + + _vector$b.fromBufferAttribute( positionAttribute, i ); + + } + + _vector$b.applyMatrix4( object.matrixWorld ); + this.expandByPoint( _vector$b ); + + } + + } else { + + if ( object.boundingBox !== undefined ) { + + // object-level bounding box + + if ( object.boundingBox === null ) { + + object.computeBoundingBox(); + + } + + _box$4.copy( object.boundingBox ); + + + } else { + + // geometry-level bounding box + + if ( geometry.boundingBox === null ) { + + geometry.computeBoundingBox(); + + } + + _box$4.copy( geometry.boundingBox ); + + } + + _box$4.applyMatrix4( object.matrixWorld ); + + this.union( _box$4 ); + + } + + } + + const children = object.children; + + for ( let i = 0, l = children.length; i < l; i ++ ) { + + this.expandByObject( children[ i ], precise ); + + } + + return this; + + } + + containsPoint( point ) { + + return point.x < this.min.x || point.x > this.max.x || + point.y < this.min.y || point.y > this.max.y || + point.z < this.min.z || point.z > this.max.z ? false : true; + + } + + containsBox( box ) { + + return this.min.x <= box.min.x && box.max.x <= this.max.x && + this.min.y <= box.min.y && box.max.y <= this.max.y && + this.min.z <= box.min.z && box.max.z <= this.max.z; + + } + + getParameter( point, target ) { + + // This can potentially have a divide by zero if the box + // has a size dimension of 0. + + return target.set( + ( point.x - this.min.x ) / ( this.max.x - this.min.x ), + ( point.y - this.min.y ) / ( this.max.y - this.min.y ), + ( point.z - this.min.z ) / ( this.max.z - this.min.z ) + ); + + } + + intersectsBox( box ) { + + // using 6 splitting planes to rule out intersections. + return box.max.x < this.min.x || box.min.x > this.max.x || + box.max.y < this.min.y || box.min.y > this.max.y || + box.max.z < this.min.z || box.min.z > this.max.z ? false : true; + + } + + intersectsSphere( sphere ) { + + // Find the point on the AABB closest to the sphere center. + this.clampPoint( sphere.center, _vector$b ); + + // If that point is inside the sphere, the AABB and sphere intersect. + return _vector$b.distanceToSquared( sphere.center ) <= ( sphere.radius * sphere.radius ); + + } + + intersectsPlane( plane ) { + + // We compute the minimum and maximum dot product values. If those values + // are on the same side (back or front) of the plane, then there is no intersection. + + let min, max; + + if ( plane.normal.x > 0 ) { + + min = plane.normal.x * this.min.x; + max = plane.normal.x * this.max.x; + + } else { + + min = plane.normal.x * this.max.x; + max = plane.normal.x * this.min.x; + + } + + if ( plane.normal.y > 0 ) { + + min += plane.normal.y * this.min.y; + max += plane.normal.y * this.max.y; + + } else { + + min += plane.normal.y * this.max.y; + max += plane.normal.y * this.min.y; + + } + + if ( plane.normal.z > 0 ) { + + min += plane.normal.z * this.min.z; + max += plane.normal.z * this.max.z; + + } else { + + min += plane.normal.z * this.max.z; + max += plane.normal.z * this.min.z; + + } + + return ( min <= - plane.constant && max >= - plane.constant ); + + } + + intersectsTriangle( triangle ) { + + if ( this.isEmpty() ) { + + return false; + + } + + // compute box center and extents + this.getCenter( _center ); + _extents.subVectors( this.max, _center ); + + // translate triangle to aabb origin + _v0$2.subVectors( triangle.a, _center ); + _v1$7.subVectors( triangle.b, _center ); + _v2$4.subVectors( triangle.c, _center ); + + // compute edge vectors for triangle + _f0.subVectors( _v1$7, _v0$2 ); + _f1.subVectors( _v2$4, _v1$7 ); + _f2.subVectors( _v0$2, _v2$4 ); + + // test against axes that are given by cross product combinations of the edges of the triangle and the edges of the aabb + // make an axis testing of each of the 3 sides of the aabb against each of the 3 sides of the triangle = 9 axis of separation + // axis_ij = u_i x f_j (u0, u1, u2 = face normals of aabb = x,y,z axes vectors since aabb is axis aligned) + let axes = [ + 0, - _f0.z, _f0.y, 0, - _f1.z, _f1.y, 0, - _f2.z, _f2.y, + _f0.z, 0, - _f0.x, _f1.z, 0, - _f1.x, _f2.z, 0, - _f2.x, + - _f0.y, _f0.x, 0, - _f1.y, _f1.x, 0, - _f2.y, _f2.x, 0 + ]; + if ( ! satForAxes( axes, _v0$2, _v1$7, _v2$4, _extents ) ) { + + return false; + + } + + // test 3 face normals from the aabb + axes = [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ]; + if ( ! satForAxes( axes, _v0$2, _v1$7, _v2$4, _extents ) ) { + + return false; + + } + + // finally testing the face normal of the triangle + // use already existing triangle edge vectors here + _triangleNormal.crossVectors( _f0, _f1 ); + axes = [ _triangleNormal.x, _triangleNormal.y, _triangleNormal.z ]; + + return satForAxes( axes, _v0$2, _v1$7, _v2$4, _extents ); + + } + + clampPoint( point, target ) { + + return target.copy( point ).clamp( this.min, this.max ); + + } + + distanceToPoint( point ) { + + return this.clampPoint( point, _vector$b ).distanceTo( point ); + + } + + getBoundingSphere( target ) { + + if ( this.isEmpty() ) { + + target.makeEmpty(); + + } else { + + this.getCenter( target.center ); + + target.radius = this.getSize( _vector$b ).length() * 0.5; + + } + + return target; + + } + + intersect( box ) { + + this.min.max( box.min ); + this.max.min( box.max ); + + // ensure that if there is no overlap, the result is fully empty, not slightly empty with non-inf/+inf values that will cause subsequence intersects to erroneously return valid values. + if ( this.isEmpty() ) this.makeEmpty(); + + return this; + + } + + union( box ) { + + this.min.min( box.min ); + this.max.max( box.max ); + + return this; + + } + + applyMatrix4( matrix ) { + + // transform of empty box is an empty box. + if ( this.isEmpty() ) return this; + + // NOTE: I am using a binary pattern to specify all 2^3 combinations below + _points[ 0 ].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000 + _points[ 1 ].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001 + _points[ 2 ].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010 + _points[ 3 ].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011 + _points[ 4 ].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100 + _points[ 5 ].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101 + _points[ 6 ].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110 + _points[ 7 ].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111 + + this.setFromPoints( _points ); + + return this; + + } + + translate( offset ) { + + this.min.add( offset ); + this.max.add( offset ); + + return this; + + } + + equals( box ) { + + return box.min.equals( this.min ) && box.max.equals( this.max ); + + } + +} + +const _points = [ + /*@__PURE__*/ new Vector3(), + /*@__PURE__*/ new Vector3(), + /*@__PURE__*/ new Vector3(), + /*@__PURE__*/ new Vector3(), + /*@__PURE__*/ new Vector3(), + /*@__PURE__*/ new Vector3(), + /*@__PURE__*/ new Vector3(), + /*@__PURE__*/ new Vector3() +]; + +const _vector$b = /*@__PURE__*/ new Vector3(); + +const _box$4 = /*@__PURE__*/ new Box3(); + +// triangle centered vertices + +const _v0$2 = /*@__PURE__*/ new Vector3(); +const _v1$7 = /*@__PURE__*/ new Vector3(); +const _v2$4 = /*@__PURE__*/ new Vector3(); + +// triangle edge vectors + +const _f0 = /*@__PURE__*/ new Vector3(); +const _f1 = /*@__PURE__*/ new Vector3(); +const _f2 = /*@__PURE__*/ new Vector3(); + +const _center = /*@__PURE__*/ new Vector3(); +const _extents = /*@__PURE__*/ new Vector3(); +const _triangleNormal = /*@__PURE__*/ new Vector3(); +const _testAxis = /*@__PURE__*/ new Vector3(); + +function satForAxes( axes, v0, v1, v2, extents ) { + + for ( let i = 0, j = axes.length - 3; i <= j; i += 3 ) { + + _testAxis.fromArray( axes, i ); + // project the aabb onto the separating axis + const r = extents.x * Math.abs( _testAxis.x ) + extents.y * Math.abs( _testAxis.y ) + extents.z * Math.abs( _testAxis.z ); + // project all 3 vertices of the triangle onto the separating axis + const p0 = v0.dot( _testAxis ); + const p1 = v1.dot( _testAxis ); + const p2 = v2.dot( _testAxis ); + // actual test, basically see if either of the most extreme of the triangle points intersects r + if ( Math.max( - Math.max( p0, p1, p2 ), Math.min( p0, p1, p2 ) ) > r ) { + + // points of the projected triangle are outside the projected half-length of the aabb + // the axis is separating and we can exit + return false; + + } + + } + + return true; + +} + +const _box$3 = /*@__PURE__*/ new Box3(); +const _v1$6 = /*@__PURE__*/ new Vector3(); +const _v2$3 = /*@__PURE__*/ new Vector3(); + +class Sphere { + + constructor( center = new Vector3(), radius = - 1 ) { + + this.isSphere = true; + + this.center = center; + this.radius = radius; + + } + + set( center, radius ) { + + this.center.copy( center ); + this.radius = radius; + + return this; + + } + + setFromPoints( points, optionalCenter ) { + + const center = this.center; + + if ( optionalCenter !== undefined ) { + + center.copy( optionalCenter ); + + } else { + + _box$3.setFromPoints( points ).getCenter( center ); + + } + + let maxRadiusSq = 0; + + for ( let i = 0, il = points.length; i < il; i ++ ) { + + maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) ); + + } + + this.radius = Math.sqrt( maxRadiusSq ); + + return this; + + } + + copy( sphere ) { + + this.center.copy( sphere.center ); + this.radius = sphere.radius; + + return this; + + } + + isEmpty() { + + return ( this.radius < 0 ); + + } + + makeEmpty() { + + this.center.set( 0, 0, 0 ); + this.radius = - 1; + + return this; + + } + + containsPoint( point ) { + + return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) ); + + } + + distanceToPoint( point ) { + + return ( point.distanceTo( this.center ) - this.radius ); + + } + + intersectsSphere( sphere ) { + + const radiusSum = this.radius + sphere.radius; + + return sphere.center.distanceToSquared( this.center ) <= ( radiusSum * radiusSum ); + + } + + intersectsBox( box ) { + + return box.intersectsSphere( this ); + + } + + intersectsPlane( plane ) { + + return Math.abs( plane.distanceToPoint( this.center ) ) <= this.radius; + + } + + clampPoint( point, target ) { + + const deltaLengthSq = this.center.distanceToSquared( point ); + + target.copy( point ); + + if ( deltaLengthSq > ( this.radius * this.radius ) ) { + + target.sub( this.center ).normalize(); + target.multiplyScalar( this.radius ).add( this.center ); + + } + + return target; + + } + + getBoundingBox( target ) { + + if ( this.isEmpty() ) { + + // Empty sphere produces empty bounding box + target.makeEmpty(); + return target; + + } + + target.set( this.center, this.center ); + target.expandByScalar( this.radius ); + + return target; + + } + + applyMatrix4( matrix ) { + + this.center.applyMatrix4( matrix ); + this.radius = this.radius * matrix.getMaxScaleOnAxis(); + + return this; + + } + + translate( offset ) { + + this.center.add( offset ); + + return this; + + } + + expandByPoint( point ) { + + if ( this.isEmpty() ) { + + this.center.copy( point ); + + this.radius = 0; + + return this; + + } + + _v1$6.subVectors( point, this.center ); + + const lengthSq = _v1$6.lengthSq(); + + if ( lengthSq > ( this.radius * this.radius ) ) { + + // calculate the minimal sphere + + const length = Math.sqrt( lengthSq ); + + const delta = ( length - this.radius ) * 0.5; + + this.center.addScaledVector( _v1$6, delta / length ); + + this.radius += delta; + + } + + return this; + + } + + union( sphere ) { + + if ( sphere.isEmpty() ) { + + return this; + + } + + if ( this.isEmpty() ) { + + this.copy( sphere ); + + return this; + + } + + if ( this.center.equals( sphere.center ) === true ) { + + this.radius = Math.max( this.radius, sphere.radius ); + + } else { + + _v2$3.subVectors( sphere.center, this.center ).setLength( sphere.radius ); + + this.expandByPoint( _v1$6.copy( sphere.center ).add( _v2$3 ) ); + + this.expandByPoint( _v1$6.copy( sphere.center ).sub( _v2$3 ) ); + + } + + return this; + + } + + equals( sphere ) { + + return sphere.center.equals( this.center ) && ( sphere.radius === this.radius ); + + } + + clone() { + + return new this.constructor().copy( this ); + + } + +} + +const _vector$a = /*@__PURE__*/ new Vector3(); +const _segCenter = /*@__PURE__*/ new Vector3(); +const _segDir = /*@__PURE__*/ new Vector3(); +const _diff = /*@__PURE__*/ new Vector3(); + +const _edge1 = /*@__PURE__*/ new Vector3(); +const _edge2 = /*@__PURE__*/ new Vector3(); +const _normal$1 = /*@__PURE__*/ new Vector3(); + +class Ray { + + constructor( origin = new Vector3(), direction = new Vector3( 0, 0, - 1 ) ) { + + this.origin = origin; + this.direction = direction; + + } + + set( origin, direction ) { + + this.origin.copy( origin ); + this.direction.copy( direction ); + + return this; + + } + + copy( ray ) { + + this.origin.copy( ray.origin ); + this.direction.copy( ray.direction ); + + return this; + + } + + at( t, target ) { + + return target.copy( this.origin ).addScaledVector( this.direction, t ); + + } + + lookAt( v ) { + + this.direction.copy( v ).sub( this.origin ).normalize(); + + return this; + + } + + recast( t ) { + + this.origin.copy( this.at( t, _vector$a ) ); + + return this; + + } + + closestPointToPoint( point, target ) { + + target.subVectors( point, this.origin ); + + const directionDistance = target.dot( this.direction ); + + if ( directionDistance < 0 ) { + + return target.copy( this.origin ); + + } + + return target.copy( this.origin ).addScaledVector( this.direction, directionDistance ); + + } + + distanceToPoint( point ) { + + return Math.sqrt( this.distanceSqToPoint( point ) ); + + } + + distanceSqToPoint( point ) { + + const directionDistance = _vector$a.subVectors( point, this.origin ).dot( this.direction ); + + // point behind the ray + + if ( directionDistance < 0 ) { + + return this.origin.distanceToSquared( point ); + + } + + _vector$a.copy( this.origin ).addScaledVector( this.direction, directionDistance ); + + return _vector$a.distanceToSquared( point ); + + } + + distanceSqToSegment( v0, v1, optionalPointOnRay, optionalPointOnSegment ) { + + // from https://github.com/pmjoniak/GeometricTools/blob/master/GTEngine/Include/Mathematics/GteDistRaySegment.h + // It returns the min distance between the ray and the segment + // defined by v0 and v1 + // It can also set two optional targets : + // - The closest point on the ray + // - The closest point on the segment + + _segCenter.copy( v0 ).add( v1 ).multiplyScalar( 0.5 ); + _segDir.copy( v1 ).sub( v0 ).normalize(); + _diff.copy( this.origin ).sub( _segCenter ); + + const segExtent = v0.distanceTo( v1 ) * 0.5; + const a01 = - this.direction.dot( _segDir ); + const b0 = _diff.dot( this.direction ); + const b1 = - _diff.dot( _segDir ); + const c = _diff.lengthSq(); + const det = Math.abs( 1 - a01 * a01 ); + let s0, s1, sqrDist, extDet; + + if ( det > 0 ) { + + // The ray and segment are not parallel. + + s0 = a01 * b1 - b0; + s1 = a01 * b0 - b1; + extDet = segExtent * det; + + if ( s0 >= 0 ) { + + if ( s1 >= - extDet ) { + + if ( s1 <= extDet ) { + + // region 0 + // Minimum at interior points of ray and segment. + + const invDet = 1 / det; + s0 *= invDet; + s1 *= invDet; + sqrDist = s0 * ( s0 + a01 * s1 + 2 * b0 ) + s1 * ( a01 * s0 + s1 + 2 * b1 ) + c; + + } else { + + // region 1 + + s1 = segExtent; + s0 = Math.max( 0, - ( a01 * s1 + b0 ) ); + sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c; + + } + + } else { + + // region 5 + + s1 = - segExtent; + s0 = Math.max( 0, - ( a01 * s1 + b0 ) ); + sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c; + + } + + } else { + + if ( s1 <= - extDet ) { + + // region 4 + + s0 = Math.max( 0, - ( - a01 * segExtent + b0 ) ); + s1 = ( s0 > 0 ) ? - segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent ); + sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c; + + } else if ( s1 <= extDet ) { + + // region 3 + + s0 = 0; + s1 = Math.min( Math.max( - segExtent, - b1 ), segExtent ); + sqrDist = s1 * ( s1 + 2 * b1 ) + c; + + } else { + + // region 2 + + s0 = Math.max( 0, - ( a01 * segExtent + b0 ) ); + s1 = ( s0 > 0 ) ? segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent ); + sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c; + + } + + } + + } else { + + // Ray and segment are parallel. + + s1 = ( a01 > 0 ) ? - segExtent : segExtent; + s0 = Math.max( 0, - ( a01 * s1 + b0 ) ); + sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c; + + } + + if ( optionalPointOnRay ) { + + optionalPointOnRay.copy( this.origin ).addScaledVector( this.direction, s0 ); + + } + + if ( optionalPointOnSegment ) { + + optionalPointOnSegment.copy( _segCenter ).addScaledVector( _segDir, s1 ); + + } + + return sqrDist; + + } + + intersectSphere( sphere, target ) { + + _vector$a.subVectors( sphere.center, this.origin ); + const tca = _vector$a.dot( this.direction ); + const d2 = _vector$a.dot( _vector$a ) - tca * tca; + const radius2 = sphere.radius * sphere.radius; + + if ( d2 > radius2 ) return null; + + const thc = Math.sqrt( radius2 - d2 ); + + // t0 = first intersect point - entrance on front of sphere + const t0 = tca - thc; + + // t1 = second intersect point - exit point on back of sphere + const t1 = tca + thc; + + // test to see if t1 is behind the ray - if so, return null + if ( t1 < 0 ) return null; + + // test to see if t0 is behind the ray: + // if it is, the ray is inside the sphere, so return the second exit point scaled by t1, + // in order to always return an intersect point that is in front of the ray. + if ( t0 < 0 ) return this.at( t1, target ); + + // else t0 is in front of the ray, so return the first collision point scaled by t0 + return this.at( t0, target ); + + } + + intersectsSphere( sphere ) { + + return this.distanceSqToPoint( sphere.center ) <= ( sphere.radius * sphere.radius ); + + } + + distanceToPlane( plane ) { + + const denominator = plane.normal.dot( this.direction ); + + if ( denominator === 0 ) { + + // line is coplanar, return origin + if ( plane.distanceToPoint( this.origin ) === 0 ) { + + return 0; + + } + + // Null is preferable to undefined since undefined means.... it is undefined + + return null; + + } + + const t = - ( this.origin.dot( plane.normal ) + plane.constant ) / denominator; + + // Return if the ray never intersects the plane + + return t >= 0 ? t : null; + + } + + intersectPlane( plane, target ) { + + const t = this.distanceToPlane( plane ); + + if ( t === null ) { + + return null; + + } + + return this.at( t, target ); + + } + + intersectsPlane( plane ) { + + // check if the ray lies on the plane first + + const distToPoint = plane.distanceToPoint( this.origin ); + + if ( distToPoint === 0 ) { + + return true; + + } + + const denominator = plane.normal.dot( this.direction ); + + if ( denominator * distToPoint < 0 ) { + + return true; + + } + + // ray origin is behind the plane (and is pointing behind it) + + return false; + + } + + intersectBox( box, target ) { + + let tmin, tmax, tymin, tymax, tzmin, tzmax; + + const invdirx = 1 / this.direction.x, + invdiry = 1 / this.direction.y, + invdirz = 1 / this.direction.z; + + const origin = this.origin; + + if ( invdirx >= 0 ) { + + tmin = ( box.min.x - origin.x ) * invdirx; + tmax = ( box.max.x - origin.x ) * invdirx; + + } else { + + tmin = ( box.max.x - origin.x ) * invdirx; + tmax = ( box.min.x - origin.x ) * invdirx; + + } + + if ( invdiry >= 0 ) { + + tymin = ( box.min.y - origin.y ) * invdiry; + tymax = ( box.max.y - origin.y ) * invdiry; + + } else { + + tymin = ( box.max.y - origin.y ) * invdiry; + tymax = ( box.min.y - origin.y ) * invdiry; + + } + + if ( ( tmin > tymax ) || ( tymin > tmax ) ) return null; + + if ( tymin > tmin || isNaN( tmin ) ) tmin = tymin; + + if ( tymax < tmax || isNaN( tmax ) ) tmax = tymax; + + if ( invdirz >= 0 ) { + + tzmin = ( box.min.z - origin.z ) * invdirz; + tzmax = ( box.max.z - origin.z ) * invdirz; + + } else { + + tzmin = ( box.max.z - origin.z ) * invdirz; + tzmax = ( box.min.z - origin.z ) * invdirz; + + } + + if ( ( tmin > tzmax ) || ( tzmin > tmax ) ) return null; + + if ( tzmin > tmin || tmin !== tmin ) tmin = tzmin; + + if ( tzmax < tmax || tmax !== tmax ) tmax = tzmax; + + //return point closest to the ray (positive side) + + if ( tmax < 0 ) return null; + + return this.at( tmin >= 0 ? tmin : tmax, target ); + + } + + intersectsBox( box ) { + + return this.intersectBox( box, _vector$a ) !== null; + + } + + intersectTriangle( a, b, c, backfaceCulling, target ) { + + // Compute the offset origin, edges, and normal. + + // from https://github.com/pmjoniak/GeometricTools/blob/master/GTEngine/Include/Mathematics/GteIntrRay3Triangle3.h + + _edge1.subVectors( b, a ); + _edge2.subVectors( c, a ); + _normal$1.crossVectors( _edge1, _edge2 ); + + // Solve Q + t*D = b1*E1 + b2*E2 (Q = kDiff, D = ray direction, + // E1 = kEdge1, E2 = kEdge2, N = Cross(E1,E2)) by + // |Dot(D,N)|*b1 = sign(Dot(D,N))*Dot(D,Cross(Q,E2)) + // |Dot(D,N)|*b2 = sign(Dot(D,N))*Dot(D,Cross(E1,Q)) + // |Dot(D,N)|*t = -sign(Dot(D,N))*Dot(Q,N) + let DdN = this.direction.dot( _normal$1 ); + let sign; + + if ( DdN > 0 ) { + + if ( backfaceCulling ) return null; + sign = 1; + + } else if ( DdN < 0 ) { + + sign = - 1; + DdN = - DdN; + + } else { + + return null; + + } + + _diff.subVectors( this.origin, a ); + const DdQxE2 = sign * this.direction.dot( _edge2.crossVectors( _diff, _edge2 ) ); + + // b1 < 0, no intersection + if ( DdQxE2 < 0 ) { + + return null; + + } + + const DdE1xQ = sign * this.direction.dot( _edge1.cross( _diff ) ); + + // b2 < 0, no intersection + if ( DdE1xQ < 0 ) { + + return null; + + } + + // b1+b2 > 1, no intersection + if ( DdQxE2 + DdE1xQ > DdN ) { + + return null; + + } + + // Line intersects triangle, check if ray does. + const QdN = - sign * _diff.dot( _normal$1 ); + + // t < 0, no intersection + if ( QdN < 0 ) { + + return null; + + } + + // Ray intersects triangle. + return this.at( QdN / DdN, target ); + + } + + applyMatrix4( matrix4 ) { + + this.origin.applyMatrix4( matrix4 ); + this.direction.transformDirection( matrix4 ); + + return this; + + } + + equals( ray ) { + + return ray.origin.equals( this.origin ) && ray.direction.equals( this.direction ); + + } + + clone() { + + return new this.constructor().copy( this ); + + } + +} + +class Matrix4 { + + constructor( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) { + + Matrix4.prototype.isMatrix4 = true; + + this.elements = [ + + 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1 + + ]; + + if ( n11 !== undefined ) { + + this.set( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ); + + } + + } + + set( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) { + + const te = this.elements; + + te[ 0 ] = n11; te[ 4 ] = n12; te[ 8 ] = n13; te[ 12 ] = n14; + te[ 1 ] = n21; te[ 5 ] = n22; te[ 9 ] = n23; te[ 13 ] = n24; + te[ 2 ] = n31; te[ 6 ] = n32; te[ 10 ] = n33; te[ 14 ] = n34; + te[ 3 ] = n41; te[ 7 ] = n42; te[ 11 ] = n43; te[ 15 ] = n44; + + return this; + + } + + identity() { + + this.set( + + 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1 + + ); + + return this; + + } + + clone() { + + return new Matrix4().fromArray( this.elements ); + + } + + copy( m ) { + + const te = this.elements; + const me = m.elements; + + te[ 0 ] = me[ 0 ]; te[ 1 ] = me[ 1 ]; te[ 2 ] = me[ 2 ]; te[ 3 ] = me[ 3 ]; + te[ 4 ] = me[ 4 ]; te[ 5 ] = me[ 5 ]; te[ 6 ] = me[ 6 ]; te[ 7 ] = me[ 7 ]; + te[ 8 ] = me[ 8 ]; te[ 9 ] = me[ 9 ]; te[ 10 ] = me[ 10 ]; te[ 11 ] = me[ 11 ]; + te[ 12 ] = me[ 12 ]; te[ 13 ] = me[ 13 ]; te[ 14 ] = me[ 14 ]; te[ 15 ] = me[ 15 ]; + + return this; + + } + + copyPosition( m ) { + + const te = this.elements, me = m.elements; + + te[ 12 ] = me[ 12 ]; + te[ 13 ] = me[ 13 ]; + te[ 14 ] = me[ 14 ]; + + return this; + + } + + setFromMatrix3( m ) { + + const me = m.elements; + + this.set( + + me[ 0 ], me[ 3 ], me[ 6 ], 0, + me[ 1 ], me[ 4 ], me[ 7 ], 0, + me[ 2 ], me[ 5 ], me[ 8 ], 0, + 0, 0, 0, 1 + + ); + + return this; + + } + + extractBasis( xAxis, yAxis, zAxis ) { + + xAxis.setFromMatrixColumn( this, 0 ); + yAxis.setFromMatrixColumn( this, 1 ); + zAxis.setFromMatrixColumn( this, 2 ); + + return this; + + } + + makeBasis( xAxis, yAxis, zAxis ) { + + this.set( + xAxis.x, yAxis.x, zAxis.x, 0, + xAxis.y, yAxis.y, zAxis.y, 0, + xAxis.z, yAxis.z, zAxis.z, 0, + 0, 0, 0, 1 + ); + + return this; + + } + + extractRotation( m ) { + + // this method does not support reflection matrices + + const te = this.elements; + const me = m.elements; + + const scaleX = 1 / _v1$5.setFromMatrixColumn( m, 0 ).length(); + const scaleY = 1 / _v1$5.setFromMatrixColumn( m, 1 ).length(); + const scaleZ = 1 / _v1$5.setFromMatrixColumn( m, 2 ).length(); + + te[ 0 ] = me[ 0 ] * scaleX; + te[ 1 ] = me[ 1 ] * scaleX; + te[ 2 ] = me[ 2 ] * scaleX; + te[ 3 ] = 0; + + te[ 4 ] = me[ 4 ] * scaleY; + te[ 5 ] = me[ 5 ] * scaleY; + te[ 6 ] = me[ 6 ] * scaleY; + te[ 7 ] = 0; + + te[ 8 ] = me[ 8 ] * scaleZ; + te[ 9 ] = me[ 9 ] * scaleZ; + te[ 10 ] = me[ 10 ] * scaleZ; + te[ 11 ] = 0; + + te[ 12 ] = 0; + te[ 13 ] = 0; + te[ 14 ] = 0; + te[ 15 ] = 1; + + return this; + + } + + makeRotationFromEuler( euler ) { + + const te = this.elements; + + const x = euler.x, y = euler.y, z = euler.z; + const a = Math.cos( x ), b = Math.sin( x ); + const c = Math.cos( y ), d = Math.sin( y ); + const e = Math.cos( z ), f = Math.sin( z ); + + if ( euler.order === 'XYZ' ) { + + const ae = a * e, af = a * f, be = b * e, bf = b * f; + + te[ 0 ] = c * e; + te[ 4 ] = - c * f; + te[ 8 ] = d; + + te[ 1 ] = af + be * d; + te[ 5 ] = ae - bf * d; + te[ 9 ] = - b * c; + + te[ 2 ] = bf - ae * d; + te[ 6 ] = be + af * d; + te[ 10 ] = a * c; + + } else if ( euler.order === 'YXZ' ) { + + const ce = c * e, cf = c * f, de = d * e, df = d * f; + + te[ 0 ] = ce + df * b; + te[ 4 ] = de * b - cf; + te[ 8 ] = a * d; + + te[ 1 ] = a * f; + te[ 5 ] = a * e; + te[ 9 ] = - b; + + te[ 2 ] = cf * b - de; + te[ 6 ] = df + ce * b; + te[ 10 ] = a * c; + + } else if ( euler.order === 'ZXY' ) { + + const ce = c * e, cf = c * f, de = d * e, df = d * f; + + te[ 0 ] = ce - df * b; + te[ 4 ] = - a * f; + te[ 8 ] = de + cf * b; + + te[ 1 ] = cf + de * b; + te[ 5 ] = a * e; + te[ 9 ] = df - ce * b; + + te[ 2 ] = - a * d; + te[ 6 ] = b; + te[ 10 ] = a * c; + + } else if ( euler.order === 'ZYX' ) { + + const ae = a * e, af = a * f, be = b * e, bf = b * f; + + te[ 0 ] = c * e; + te[ 4 ] = be * d - af; + te[ 8 ] = ae * d + bf; + + te[ 1 ] = c * f; + te[ 5 ] = bf * d + ae; + te[ 9 ] = af * d - be; + + te[ 2 ] = - d; + te[ 6 ] = b * c; + te[ 10 ] = a * c; + + } else if ( euler.order === 'YZX' ) { + + const ac = a * c, ad = a * d, bc = b * c, bd = b * d; + + te[ 0 ] = c * e; + te[ 4 ] = bd - ac * f; + te[ 8 ] = bc * f + ad; + + te[ 1 ] = f; + te[ 5 ] = a * e; + te[ 9 ] = - b * e; + + te[ 2 ] = - d * e; + te[ 6 ] = ad * f + bc; + te[ 10 ] = ac - bd * f; + + } else if ( euler.order === 'XZY' ) { + + const ac = a * c, ad = a * d, bc = b * c, bd = b * d; + + te[ 0 ] = c * e; + te[ 4 ] = - f; + te[ 8 ] = d * e; + + te[ 1 ] = ac * f + bd; + te[ 5 ] = a * e; + te[ 9 ] = ad * f - bc; + + te[ 2 ] = bc * f - ad; + te[ 6 ] = b * e; + te[ 10 ] = bd * f + ac; + + } + + // bottom row + te[ 3 ] = 0; + te[ 7 ] = 0; + te[ 11 ] = 0; + + // last column + te[ 12 ] = 0; + te[ 13 ] = 0; + te[ 14 ] = 0; + te[ 15 ] = 1; + + return this; + + } + + makeRotationFromQuaternion( q ) { + + return this.compose( _zero, q, _one ); + + } + + lookAt( eye, target, up ) { + + const te = this.elements; + + _z.subVectors( eye, target ); + + if ( _z.lengthSq() === 0 ) { + + // eye and target are in the same position + + _z.z = 1; + + } + + _z.normalize(); + _x.crossVectors( up, _z ); + + if ( _x.lengthSq() === 0 ) { + + // up and z are parallel + + if ( Math.abs( up.z ) === 1 ) { + + _z.x += 0.0001; + + } else { + + _z.z += 0.0001; + + } + + _z.normalize(); + _x.crossVectors( up, _z ); + + } + + _x.normalize(); + _y.crossVectors( _z, _x ); + + te[ 0 ] = _x.x; te[ 4 ] = _y.x; te[ 8 ] = _z.x; + te[ 1 ] = _x.y; te[ 5 ] = _y.y; te[ 9 ] = _z.y; + te[ 2 ] = _x.z; te[ 6 ] = _y.z; te[ 10 ] = _z.z; + + return this; + + } + + multiply( m ) { + + return this.multiplyMatrices( this, m ); + + } + + premultiply( m ) { + + return this.multiplyMatrices( m, this ); + + } + + multiplyMatrices( a, b ) { + + const ae = a.elements; + const be = b.elements; + const te = this.elements; + + const a11 = ae[ 0 ], a12 = ae[ 4 ], a13 = ae[ 8 ], a14 = ae[ 12 ]; + const a21 = ae[ 1 ], a22 = ae[ 5 ], a23 = ae[ 9 ], a24 = ae[ 13 ]; + const a31 = ae[ 2 ], a32 = ae[ 6 ], a33 = ae[ 10 ], a34 = ae[ 14 ]; + const a41 = ae[ 3 ], a42 = ae[ 7 ], a43 = ae[ 11 ], a44 = ae[ 15 ]; + + const b11 = be[ 0 ], b12 = be[ 4 ], b13 = be[ 8 ], b14 = be[ 12 ]; + const b21 = be[ 1 ], b22 = be[ 5 ], b23 = be[ 9 ], b24 = be[ 13 ]; + const b31 = be[ 2 ], b32 = be[ 6 ], b33 = be[ 10 ], b34 = be[ 14 ]; + const b41 = be[ 3 ], b42 = be[ 7 ], b43 = be[ 11 ], b44 = be[ 15 ]; + + te[ 0 ] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41; + te[ 4 ] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42; + te[ 8 ] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43; + te[ 12 ] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44; + + te[ 1 ] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41; + te[ 5 ] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42; + te[ 9 ] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43; + te[ 13 ] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44; + + te[ 2 ] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41; + te[ 6 ] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42; + te[ 10 ] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43; + te[ 14 ] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44; + + te[ 3 ] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41; + te[ 7 ] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42; + te[ 11 ] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43; + te[ 15 ] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44; + + return this; + + } + + multiplyScalar( s ) { + + const te = this.elements; + + te[ 0 ] *= s; te[ 4 ] *= s; te[ 8 ] *= s; te[ 12 ] *= s; + te[ 1 ] *= s; te[ 5 ] *= s; te[ 9 ] *= s; te[ 13 ] *= s; + te[ 2 ] *= s; te[ 6 ] *= s; te[ 10 ] *= s; te[ 14 ] *= s; + te[ 3 ] *= s; te[ 7 ] *= s; te[ 11 ] *= s; te[ 15 ] *= s; + + return this; + + } + + determinant() { + + const te = this.elements; + + const n11 = te[ 0 ], n12 = te[ 4 ], n13 = te[ 8 ], n14 = te[ 12 ]; + const n21 = te[ 1 ], n22 = te[ 5 ], n23 = te[ 9 ], n24 = te[ 13 ]; + const n31 = te[ 2 ], n32 = te[ 6 ], n33 = te[ 10 ], n34 = te[ 14 ]; + const n41 = te[ 3 ], n42 = te[ 7 ], n43 = te[ 11 ], n44 = te[ 15 ]; + + //TODO: make this more efficient + //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm ) + + return ( + n41 * ( + + n14 * n23 * n32 + - n13 * n24 * n32 + - n14 * n22 * n33 + + n12 * n24 * n33 + + n13 * n22 * n34 + - n12 * n23 * n34 + ) + + n42 * ( + + n11 * n23 * n34 + - n11 * n24 * n33 + + n14 * n21 * n33 + - n13 * n21 * n34 + + n13 * n24 * n31 + - n14 * n23 * n31 + ) + + n43 * ( + + n11 * n24 * n32 + - n11 * n22 * n34 + - n14 * n21 * n32 + + n12 * n21 * n34 + + n14 * n22 * n31 + - n12 * n24 * n31 + ) + + n44 * ( + - n13 * n22 * n31 + - n11 * n23 * n32 + + n11 * n22 * n33 + + n13 * n21 * n32 + - n12 * n21 * n33 + + n12 * n23 * n31 + ) + + ); + + } + + transpose() { + + const te = this.elements; + let tmp; + + tmp = te[ 1 ]; te[ 1 ] = te[ 4 ]; te[ 4 ] = tmp; + tmp = te[ 2 ]; te[ 2 ] = te[ 8 ]; te[ 8 ] = tmp; + tmp = te[ 6 ]; te[ 6 ] = te[ 9 ]; te[ 9 ] = tmp; + + tmp = te[ 3 ]; te[ 3 ] = te[ 12 ]; te[ 12 ] = tmp; + tmp = te[ 7 ]; te[ 7 ] = te[ 13 ]; te[ 13 ] = tmp; + tmp = te[ 11 ]; te[ 11 ] = te[ 14 ]; te[ 14 ] = tmp; + + return this; + + } + + setPosition( x, y, z ) { + + const te = this.elements; + + if ( x.isVector3 ) { + + te[ 12 ] = x.x; + te[ 13 ] = x.y; + te[ 14 ] = x.z; + + } else { + + te[ 12 ] = x; + te[ 13 ] = y; + te[ 14 ] = z; + + } + + return this; + + } + + invert() { + + // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm + const te = this.elements, + + n11 = te[ 0 ], n21 = te[ 1 ], n31 = te[ 2 ], n41 = te[ 3 ], + n12 = te[ 4 ], n22 = te[ 5 ], n32 = te[ 6 ], n42 = te[ 7 ], + n13 = te[ 8 ], n23 = te[ 9 ], n33 = te[ 10 ], n43 = te[ 11 ], + n14 = te[ 12 ], n24 = te[ 13 ], n34 = te[ 14 ], n44 = te[ 15 ], + + t11 = n23 * n34 * n42 - n24 * n33 * n42 + n24 * n32 * n43 - n22 * n34 * n43 - n23 * n32 * n44 + n22 * n33 * n44, + t12 = n14 * n33 * n42 - n13 * n34 * n42 - n14 * n32 * n43 + n12 * n34 * n43 + n13 * n32 * n44 - n12 * n33 * n44, + t13 = n13 * n24 * n42 - n14 * n23 * n42 + n14 * n22 * n43 - n12 * n24 * n43 - n13 * n22 * n44 + n12 * n23 * n44, + t14 = n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34; + + const det = n11 * t11 + n21 * t12 + n31 * t13 + n41 * t14; + + if ( det === 0 ) return this.set( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ); + + const detInv = 1 / det; + + te[ 0 ] = t11 * detInv; + te[ 1 ] = ( n24 * n33 * n41 - n23 * n34 * n41 - n24 * n31 * n43 + n21 * n34 * n43 + n23 * n31 * n44 - n21 * n33 * n44 ) * detInv; + te[ 2 ] = ( n22 * n34 * n41 - n24 * n32 * n41 + n24 * n31 * n42 - n21 * n34 * n42 - n22 * n31 * n44 + n21 * n32 * n44 ) * detInv; + te[ 3 ] = ( n23 * n32 * n41 - n22 * n33 * n41 - n23 * n31 * n42 + n21 * n33 * n42 + n22 * n31 * n43 - n21 * n32 * n43 ) * detInv; + + te[ 4 ] = t12 * detInv; + te[ 5 ] = ( n13 * n34 * n41 - n14 * n33 * n41 + n14 * n31 * n43 - n11 * n34 * n43 - n13 * n31 * n44 + n11 * n33 * n44 ) * detInv; + te[ 6 ] = ( n14 * n32 * n41 - n12 * n34 * n41 - n14 * n31 * n42 + n11 * n34 * n42 + n12 * n31 * n44 - n11 * n32 * n44 ) * detInv; + te[ 7 ] = ( n12 * n33 * n41 - n13 * n32 * n41 + n13 * n31 * n42 - n11 * n33 * n42 - n12 * n31 * n43 + n11 * n32 * n43 ) * detInv; + + te[ 8 ] = t13 * detInv; + te[ 9 ] = ( n14 * n23 * n41 - n13 * n24 * n41 - n14 * n21 * n43 + n11 * n24 * n43 + n13 * n21 * n44 - n11 * n23 * n44 ) * detInv; + te[ 10 ] = ( n12 * n24 * n41 - n14 * n22 * n41 + n14 * n21 * n42 - n11 * n24 * n42 - n12 * n21 * n44 + n11 * n22 * n44 ) * detInv; + te[ 11 ] = ( n13 * n22 * n41 - n12 * n23 * n41 - n13 * n21 * n42 + n11 * n23 * n42 + n12 * n21 * n43 - n11 * n22 * n43 ) * detInv; + + te[ 12 ] = t14 * detInv; + te[ 13 ] = ( n13 * n24 * n31 - n14 * n23 * n31 + n14 * n21 * n33 - n11 * n24 * n33 - n13 * n21 * n34 + n11 * n23 * n34 ) * detInv; + te[ 14 ] = ( n14 * n22 * n31 - n12 * n24 * n31 - n14 * n21 * n32 + n11 * n24 * n32 + n12 * n21 * n34 - n11 * n22 * n34 ) * detInv; + te[ 15 ] = ( n12 * n23 * n31 - n13 * n22 * n31 + n13 * n21 * n32 - n11 * n23 * n32 - n12 * n21 * n33 + n11 * n22 * n33 ) * detInv; + + return this; + + } + + scale( v ) { + + const te = this.elements; + const x = v.x, y = v.y, z = v.z; + + te[ 0 ] *= x; te[ 4 ] *= y; te[ 8 ] *= z; + te[ 1 ] *= x; te[ 5 ] *= y; te[ 9 ] *= z; + te[ 2 ] *= x; te[ 6 ] *= y; te[ 10 ] *= z; + te[ 3 ] *= x; te[ 7 ] *= y; te[ 11 ] *= z; + + return this; + + } + + getMaxScaleOnAxis() { + + const te = this.elements; + + const scaleXSq = te[ 0 ] * te[ 0 ] + te[ 1 ] * te[ 1 ] + te[ 2 ] * te[ 2 ]; + const scaleYSq = te[ 4 ] * te[ 4 ] + te[ 5 ] * te[ 5 ] + te[ 6 ] * te[ 6 ]; + const scaleZSq = te[ 8 ] * te[ 8 ] + te[ 9 ] * te[ 9 ] + te[ 10 ] * te[ 10 ]; + + return Math.sqrt( Math.max( scaleXSq, scaleYSq, scaleZSq ) ); + + } + + makeTranslation( x, y, z ) { + + if ( x.isVector3 ) { + + this.set( + + 1, 0, 0, x.x, + 0, 1, 0, x.y, + 0, 0, 1, x.z, + 0, 0, 0, 1 + + ); + + } else { + + this.set( + + 1, 0, 0, x, + 0, 1, 0, y, + 0, 0, 1, z, + 0, 0, 0, 1 + + ); + + } + + return this; + + } + + makeRotationX( theta ) { + + const c = Math.cos( theta ), s = Math.sin( theta ); + + this.set( + + 1, 0, 0, 0, + 0, c, - s, 0, + 0, s, c, 0, + 0, 0, 0, 1 + + ); + + return this; + + } + + makeRotationY( theta ) { + + const c = Math.cos( theta ), s = Math.sin( theta ); + + this.set( + + c, 0, s, 0, + 0, 1, 0, 0, + - s, 0, c, 0, + 0, 0, 0, 1 + + ); + + return this; + + } + + makeRotationZ( theta ) { + + const c = Math.cos( theta ), s = Math.sin( theta ); + + this.set( + + c, - s, 0, 0, + s, c, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1 + + ); + + return this; + + } + + makeRotationAxis( axis, angle ) { + + // Based on http://www.gamedev.net/reference/articles/article1199.asp + + const c = Math.cos( angle ); + const s = Math.sin( angle ); + const t = 1 - c; + const x = axis.x, y = axis.y, z = axis.z; + const tx = t * x, ty = t * y; + + this.set( + + tx * x + c, tx * y - s * z, tx * z + s * y, 0, + tx * y + s * z, ty * y + c, ty * z - s * x, 0, + tx * z - s * y, ty * z + s * x, t * z * z + c, 0, + 0, 0, 0, 1 + + ); + + return this; + + } + + makeScale( x, y, z ) { + + this.set( + + x, 0, 0, 0, + 0, y, 0, 0, + 0, 0, z, 0, + 0, 0, 0, 1 + + ); + + return this; + + } + + makeShear( xy, xz, yx, yz, zx, zy ) { + + this.set( + + 1, yx, zx, 0, + xy, 1, zy, 0, + xz, yz, 1, 0, + 0, 0, 0, 1 + + ); + + return this; + + } + + compose( position, quaternion, scale ) { + + const te = this.elements; + + const x = quaternion._x, y = quaternion._y, z = quaternion._z, w = quaternion._w; + const x2 = x + x, y2 = y + y, z2 = z + z; + const xx = x * x2, xy = x * y2, xz = x * z2; + const yy = y * y2, yz = y * z2, zz = z * z2; + const wx = w * x2, wy = w * y2, wz = w * z2; + + const sx = scale.x, sy = scale.y, sz = scale.z; + + te[ 0 ] = ( 1 - ( yy + zz ) ) * sx; + te[ 1 ] = ( xy + wz ) * sx; + te[ 2 ] = ( xz - wy ) * sx; + te[ 3 ] = 0; + + te[ 4 ] = ( xy - wz ) * sy; + te[ 5 ] = ( 1 - ( xx + zz ) ) * sy; + te[ 6 ] = ( yz + wx ) * sy; + te[ 7 ] = 0; + + te[ 8 ] = ( xz + wy ) * sz; + te[ 9 ] = ( yz - wx ) * sz; + te[ 10 ] = ( 1 - ( xx + yy ) ) * sz; + te[ 11 ] = 0; + + te[ 12 ] = position.x; + te[ 13 ] = position.y; + te[ 14 ] = position.z; + te[ 15 ] = 1; + + return this; + + } + + decompose( position, quaternion, scale ) { + + const te = this.elements; + + let sx = _v1$5.set( te[ 0 ], te[ 1 ], te[ 2 ] ).length(); + const sy = _v1$5.set( te[ 4 ], te[ 5 ], te[ 6 ] ).length(); + const sz = _v1$5.set( te[ 8 ], te[ 9 ], te[ 10 ] ).length(); + + // if determine is negative, we need to invert one scale + const det = this.determinant(); + if ( det < 0 ) sx = - sx; + + position.x = te[ 12 ]; + position.y = te[ 13 ]; + position.z = te[ 14 ]; + + // scale the rotation part + _m1$4.copy( this ); + + const invSX = 1 / sx; + const invSY = 1 / sy; + const invSZ = 1 / sz; + + _m1$4.elements[ 0 ] *= invSX; + _m1$4.elements[ 1 ] *= invSX; + _m1$4.elements[ 2 ] *= invSX; + + _m1$4.elements[ 4 ] *= invSY; + _m1$4.elements[ 5 ] *= invSY; + _m1$4.elements[ 6 ] *= invSY; + + _m1$4.elements[ 8 ] *= invSZ; + _m1$4.elements[ 9 ] *= invSZ; + _m1$4.elements[ 10 ] *= invSZ; + + quaternion.setFromRotationMatrix( _m1$4 ); + + scale.x = sx; + scale.y = sy; + scale.z = sz; + + return this; + + } + + makePerspective( left, right, top, bottom, near, far, coordinateSystem = WebGLCoordinateSystem ) { + + const te = this.elements; + const x = 2 * near / ( right - left ); + const y = 2 * near / ( top - bottom ); + + const a = ( right + left ) / ( right - left ); + const b = ( top + bottom ) / ( top - bottom ); + + let c, d; + + if ( coordinateSystem === WebGLCoordinateSystem ) { + + c = - ( far + near ) / ( far - near ); + d = ( - 2 * far * near ) / ( far - near ); + + } else if ( coordinateSystem === WebGPUCoordinateSystem ) { + + c = - far / ( far - near ); + d = ( - far * near ) / ( far - near ); + + } else { + + throw new Error( 'THREE.Matrix4.makePerspective(): Invalid coordinate system: ' + coordinateSystem ); + + } + + te[ 0 ] = x; te[ 4 ] = 0; te[ 8 ] = a; te[ 12 ] = 0; + te[ 1 ] = 0; te[ 5 ] = y; te[ 9 ] = b; te[ 13 ] = 0; + te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = c; te[ 14 ] = d; + te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = - 1; te[ 15 ] = 0; + + return this; + + } + + makeOrthographic( left, right, top, bottom, near, far, coordinateSystem = WebGLCoordinateSystem ) { + + const te = this.elements; + const w = 1.0 / ( right - left ); + const h = 1.0 / ( top - bottom ); + const p = 1.0 / ( far - near ); + + const x = ( right + left ) * w; + const y = ( top + bottom ) * h; + + let z, zInv; + + if ( coordinateSystem === WebGLCoordinateSystem ) { + + z = ( far + near ) * p; + zInv = - 2 * p; + + } else if ( coordinateSystem === WebGPUCoordinateSystem ) { + + z = near * p; + zInv = - 1 * p; + + } else { + + throw new Error( 'THREE.Matrix4.makeOrthographic(): Invalid coordinate system: ' + coordinateSystem ); + + } + + te[ 0 ] = 2 * w; te[ 4 ] = 0; te[ 8 ] = 0; te[ 12 ] = - x; + te[ 1 ] = 0; te[ 5 ] = 2 * h; te[ 9 ] = 0; te[ 13 ] = - y; + te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = zInv; te[ 14 ] = - z; + te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = 0; te[ 15 ] = 1; + + return this; + + } + + equals( matrix ) { + + const te = this.elements; + const me = matrix.elements; + + for ( let i = 0; i < 16; i ++ ) { + + if ( te[ i ] !== me[ i ] ) return false; + + } + + return true; + + } + + fromArray( array, offset = 0 ) { + + for ( let i = 0; i < 16; i ++ ) { + + this.elements[ i ] = array[ i + offset ]; + + } + + return this; + + } + + toArray( array = [], offset = 0 ) { + + const te = this.elements; + + array[ offset ] = te[ 0 ]; + array[ offset + 1 ] = te[ 1 ]; + array[ offset + 2 ] = te[ 2 ]; + array[ offset + 3 ] = te[ 3 ]; + + array[ offset + 4 ] = te[ 4 ]; + array[ offset + 5 ] = te[ 5 ]; + array[ offset + 6 ] = te[ 6 ]; + array[ offset + 7 ] = te[ 7 ]; + + array[ offset + 8 ] = te[ 8 ]; + array[ offset + 9 ] = te[ 9 ]; + array[ offset + 10 ] = te[ 10 ]; + array[ offset + 11 ] = te[ 11 ]; + + array[ offset + 12 ] = te[ 12 ]; + array[ offset + 13 ] = te[ 13 ]; + array[ offset + 14 ] = te[ 14 ]; + array[ offset + 15 ] = te[ 15 ]; + + return array; + + } + +} + +const _v1$5 = /*@__PURE__*/ new Vector3(); +const _m1$4 = /*@__PURE__*/ new Matrix4(); +const _zero = /*@__PURE__*/ new Vector3( 0, 0, 0 ); +const _one = /*@__PURE__*/ new Vector3( 1, 1, 1 ); +const _x = /*@__PURE__*/ new Vector3(); +const _y = /*@__PURE__*/ new Vector3(); +const _z = /*@__PURE__*/ new Vector3(); + +const _matrix$2 = /*@__PURE__*/ new Matrix4(); +const _quaternion$3 = /*@__PURE__*/ new Quaternion(); + +class Euler { + + constructor( x = 0, y = 0, z = 0, order = Euler.DEFAULT_ORDER ) { + + this.isEuler = true; + + this._x = x; + this._y = y; + this._z = z; + this._order = order; + + } + + get x() { + + return this._x; + + } + + set x( value ) { + + this._x = value; + this._onChangeCallback(); + + } + + get y() { + + return this._y; + + } + + set y( value ) { + + this._y = value; + this._onChangeCallback(); + + } + + get z() { + + return this._z; + + } + + set z( value ) { + + this._z = value; + this._onChangeCallback(); + + } + + get order() { + + return this._order; + + } + + set order( value ) { + + this._order = value; + this._onChangeCallback(); + + } + + set( x, y, z, order = this._order ) { + + this._x = x; + this._y = y; + this._z = z; + this._order = order; + + this._onChangeCallback(); + + return this; + + } + + clone() { + + return new this.constructor( this._x, this._y, this._z, this._order ); + + } + + copy( euler ) { + + this._x = euler._x; + this._y = euler._y; + this._z = euler._z; + this._order = euler._order; + + this._onChangeCallback(); + + return this; + + } + + setFromRotationMatrix( m, order = this._order, update = true ) { + + // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled) + + const te = m.elements; + const m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ]; + const m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ]; + const m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ]; + + switch ( order ) { + + case 'XYZ': + + this._y = Math.asin( clamp( m13, - 1, 1 ) ); + + if ( Math.abs( m13 ) < 0.9999999 ) { + + this._x = Math.atan2( - m23, m33 ); + this._z = Math.atan2( - m12, m11 ); + + } else { + + this._x = Math.atan2( m32, m22 ); + this._z = 0; + + } + + break; + + case 'YXZ': + + this._x = Math.asin( - clamp( m23, - 1, 1 ) ); + + if ( Math.abs( m23 ) < 0.9999999 ) { + + this._y = Math.atan2( m13, m33 ); + this._z = Math.atan2( m21, m22 ); + + } else { + + this._y = Math.atan2( - m31, m11 ); + this._z = 0; + + } + + break; + + case 'ZXY': + + this._x = Math.asin( clamp( m32, - 1, 1 ) ); + + if ( Math.abs( m32 ) < 0.9999999 ) { + + this._y = Math.atan2( - m31, m33 ); + this._z = Math.atan2( - m12, m22 ); + + } else { + + this._y = 0; + this._z = Math.atan2( m21, m11 ); + + } + + break; + + case 'ZYX': + + this._y = Math.asin( - clamp( m31, - 1, 1 ) ); + + if ( Math.abs( m31 ) < 0.9999999 ) { + + this._x = Math.atan2( m32, m33 ); + this._z = Math.atan2( m21, m11 ); + + } else { + + this._x = 0; + this._z = Math.atan2( - m12, m22 ); + + } + + break; + + case 'YZX': + + this._z = Math.asin( clamp( m21, - 1, 1 ) ); + + if ( Math.abs( m21 ) < 0.9999999 ) { + + this._x = Math.atan2( - m23, m22 ); + this._y = Math.atan2( - m31, m11 ); + + } else { + + this._x = 0; + this._y = Math.atan2( m13, m33 ); + + } + + break; + + case 'XZY': + + this._z = Math.asin( - clamp( m12, - 1, 1 ) ); + + if ( Math.abs( m12 ) < 0.9999999 ) { + + this._x = Math.atan2( m32, m22 ); + this._y = Math.atan2( m13, m11 ); + + } else { + + this._x = Math.atan2( - m23, m33 ); + this._y = 0; + + } + + break; + + default: + + console.warn( 'THREE.Euler: .setFromRotationMatrix() encountered an unknown order: ' + order ); + + } + + this._order = order; + + if ( update === true ) this._onChangeCallback(); + + return this; + + } + + setFromQuaternion( q, order, update ) { + + _matrix$2.makeRotationFromQuaternion( q ); + + return this.setFromRotationMatrix( _matrix$2, order, update ); + + } + + setFromVector3( v, order = this._order ) { + + return this.set( v.x, v.y, v.z, order ); + + } + + reorder( newOrder ) { + + // WARNING: this discards revolution information -bhouston + + _quaternion$3.setFromEuler( this ); + + return this.setFromQuaternion( _quaternion$3, newOrder ); + + } + + equals( euler ) { + + return ( euler._x === this._x ) && ( euler._y === this._y ) && ( euler._z === this._z ) && ( euler._order === this._order ); + + } + + fromArray( array ) { + + this._x = array[ 0 ]; + this._y = array[ 1 ]; + this._z = array[ 2 ]; + if ( array[ 3 ] !== undefined ) this._order = array[ 3 ]; + + this._onChangeCallback(); + + return this; + + } + + toArray( array = [], offset = 0 ) { + + array[ offset ] = this._x; + array[ offset + 1 ] = this._y; + array[ offset + 2 ] = this._z; + array[ offset + 3 ] = this._order; + + return array; + + } + + _onChange( callback ) { + + this._onChangeCallback = callback; + + return this; + + } + + _onChangeCallback() {} + + *[ Symbol.iterator ]() { + + yield this._x; + yield this._y; + yield this._z; + yield this._order; + + } + +} + +Euler.DEFAULT_ORDER = 'XYZ'; + +class Layers { + + constructor() { + + this.mask = 1 | 0; + + } + + set( channel ) { + + this.mask = ( 1 << channel | 0 ) >>> 0; + + } + + enable( channel ) { + + this.mask |= 1 << channel | 0; + + } + + enableAll() { + + this.mask = 0xffffffff | 0; + + } + + toggle( channel ) { + + this.mask ^= 1 << channel | 0; + + } + + disable( channel ) { + + this.mask &= ~ ( 1 << channel | 0 ); + + } + + disableAll() { + + this.mask = 0; + + } + + test( layers ) { + + return ( this.mask & layers.mask ) !== 0; + + } + + isEnabled( channel ) { + + return ( this.mask & ( 1 << channel | 0 ) ) !== 0; + + } + +} + +let _object3DId = 0; + +const _v1$4 = /*@__PURE__*/ new Vector3(); +const _q1 = /*@__PURE__*/ new Quaternion(); +const _m1$3 = /*@__PURE__*/ new Matrix4(); +const _target = /*@__PURE__*/ new Vector3(); + +const _position$3 = /*@__PURE__*/ new Vector3(); +const _scale$2 = /*@__PURE__*/ new Vector3(); +const _quaternion$2 = /*@__PURE__*/ new Quaternion(); + +const _xAxis = /*@__PURE__*/ new Vector3( 1, 0, 0 ); +const _yAxis = /*@__PURE__*/ new Vector3( 0, 1, 0 ); +const _zAxis = /*@__PURE__*/ new Vector3( 0, 0, 1 ); + +const _addedEvent = { type: 'added' }; +const _removedEvent = { type: 'removed' }; + +const _childaddedEvent = { type: 'childadded', child: null }; +const _childremovedEvent = { type: 'childremoved', child: null }; + +class Object3D extends EventDispatcher { + + constructor() { + + super(); + + this.isObject3D = true; + + Object.defineProperty( this, 'id', { value: _object3DId ++ } ); + + this.uuid = generateUUID(); + + this.name = ''; + this.type = 'Object3D'; + + this.parent = null; + this.children = []; + + this.up = Object3D.DEFAULT_UP.clone(); + + const position = new Vector3(); + const rotation = new Euler(); + const quaternion = new Quaternion(); + const scale = new Vector3( 1, 1, 1 ); + + function onRotationChange() { + + quaternion.setFromEuler( rotation, false ); + + } + + function onQuaternionChange() { + + rotation.setFromQuaternion( quaternion, undefined, false ); + + } + + rotation._onChange( onRotationChange ); + quaternion._onChange( onQuaternionChange ); + + Object.defineProperties( this, { + position: { + configurable: true, + enumerable: true, + value: position + }, + rotation: { + configurable: true, + enumerable: true, + value: rotation + }, + quaternion: { + configurable: true, + enumerable: true, + value: quaternion + }, + scale: { + configurable: true, + enumerable: true, + value: scale + }, + modelViewMatrix: { + value: new Matrix4() + }, + normalMatrix: { + value: new Matrix3() + } + } ); + + this.matrix = new Matrix4(); + this.matrixWorld = new Matrix4(); + + this.matrixAutoUpdate = Object3D.DEFAULT_MATRIX_AUTO_UPDATE; + + this.matrixWorldAutoUpdate = Object3D.DEFAULT_MATRIX_WORLD_AUTO_UPDATE; // checked by the renderer + this.matrixWorldNeedsUpdate = false; + + this.layers = new Layers(); + this.visible = true; + + this.castShadow = false; + this.receiveShadow = false; + + this.frustumCulled = true; + this.renderOrder = 0; + + this.animations = []; + + this.userData = {}; + + } + + onBeforeShadow( /* renderer, object, camera, shadowCamera, geometry, depthMaterial, group */ ) {} + + onAfterShadow( /* renderer, object, camera, shadowCamera, geometry, depthMaterial, group */ ) {} + + onBeforeRender( /* renderer, scene, camera, geometry, material, group */ ) {} + + onAfterRender( /* renderer, scene, camera, geometry, material, group */ ) {} + + applyMatrix4( matrix ) { + + if ( this.matrixAutoUpdate ) this.updateMatrix(); + + this.matrix.premultiply( matrix ); + + this.matrix.decompose( this.position, this.quaternion, this.scale ); + + } + + applyQuaternion( q ) { + + this.quaternion.premultiply( q ); + + return this; + + } + + setRotationFromAxisAngle( axis, angle ) { + + // assumes axis is normalized + + this.quaternion.setFromAxisAngle( axis, angle ); + + } + + setRotationFromEuler( euler ) { + + this.quaternion.setFromEuler( euler, true ); + + } + + setRotationFromMatrix( m ) { + + // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled) + + this.quaternion.setFromRotationMatrix( m ); + + } + + setRotationFromQuaternion( q ) { + + // assumes q is normalized + + this.quaternion.copy( q ); + + } + + rotateOnAxis( axis, angle ) { + + // rotate object on axis in object space + // axis is assumed to be normalized + + _q1.setFromAxisAngle( axis, angle ); + + this.quaternion.multiply( _q1 ); + + return this; + + } + + rotateOnWorldAxis( axis, angle ) { + + // rotate object on axis in world space + // axis is assumed to be normalized + // method assumes no rotated parent + + _q1.setFromAxisAngle( axis, angle ); + + this.quaternion.premultiply( _q1 ); + + return this; + + } + + rotateX( angle ) { + + return this.rotateOnAxis( _xAxis, angle ); + + } + + rotateY( angle ) { + + return this.rotateOnAxis( _yAxis, angle ); + + } + + rotateZ( angle ) { + + return this.rotateOnAxis( _zAxis, angle ); + + } + + translateOnAxis( axis, distance ) { + + // translate object by distance along axis in object space + // axis is assumed to be normalized + + _v1$4.copy( axis ).applyQuaternion( this.quaternion ); + + this.position.add( _v1$4.multiplyScalar( distance ) ); + + return this; + + } + + translateX( distance ) { + + return this.translateOnAxis( _xAxis, distance ); + + } + + translateY( distance ) { + + return this.translateOnAxis( _yAxis, distance ); + + } + + translateZ( distance ) { + + return this.translateOnAxis( _zAxis, distance ); + + } + + localToWorld( vector ) { + + this.updateWorldMatrix( true, false ); + + return vector.applyMatrix4( this.matrixWorld ); + + } + + worldToLocal( vector ) { + + this.updateWorldMatrix( true, false ); + + return vector.applyMatrix4( _m1$3.copy( this.matrixWorld ).invert() ); + + } + + lookAt( x, y, z ) { + + // This method does not support objects having non-uniformly-scaled parent(s) + + if ( x.isVector3 ) { + + _target.copy( x ); + + } else { + + _target.set( x, y, z ); + + } + + const parent = this.parent; + + this.updateWorldMatrix( true, false ); + + _position$3.setFromMatrixPosition( this.matrixWorld ); + + if ( this.isCamera || this.isLight ) { + + _m1$3.lookAt( _position$3, _target, this.up ); + + } else { + + _m1$3.lookAt( _target, _position$3, this.up ); + + } + + this.quaternion.setFromRotationMatrix( _m1$3 ); + + if ( parent ) { + + _m1$3.extractRotation( parent.matrixWorld ); + _q1.setFromRotationMatrix( _m1$3 ); + this.quaternion.premultiply( _q1.invert() ); + + } + + } + + add( object ) { + + if ( arguments.length > 1 ) { + + for ( let i = 0; i < arguments.length; i ++ ) { + + this.add( arguments[ i ] ); + + } + + return this; + + } + + if ( object === this ) { + + console.error( 'THREE.Object3D.add: object can\'t be added as a child of itself.', object ); + return this; + + } + + if ( object && object.isObject3D ) { + + object.removeFromParent(); + object.parent = this; + this.children.push( object ); + + object.dispatchEvent( _addedEvent ); + + _childaddedEvent.child = object; + this.dispatchEvent( _childaddedEvent ); + _childaddedEvent.child = null; + + } else { + + console.error( 'THREE.Object3D.add: object not an instance of THREE.Object3D.', object ); + + } + + return this; + + } + + remove( object ) { + + if ( arguments.length > 1 ) { + + for ( let i = 0; i < arguments.length; i ++ ) { + + this.remove( arguments[ i ] ); + + } + + return this; + + } + + const index = this.children.indexOf( object ); + + if ( index !== - 1 ) { + + object.parent = null; + this.children.splice( index, 1 ); + + object.dispatchEvent( _removedEvent ); + + _childremovedEvent.child = object; + this.dispatchEvent( _childremovedEvent ); + _childremovedEvent.child = null; + + } + + return this; + + } + + removeFromParent() { + + const parent = this.parent; + + if ( parent !== null ) { + + parent.remove( this ); + + } + + return this; + + } + + clear() { + + return this.remove( ... this.children ); + + } + + attach( object ) { + + // adds object as a child of this, while maintaining the object's world transform + + // Note: This method does not support scene graphs having non-uniformly-scaled nodes(s) + + this.updateWorldMatrix( true, false ); + + _m1$3.copy( this.matrixWorld ).invert(); + + if ( object.parent !== null ) { + + object.parent.updateWorldMatrix( true, false ); + + _m1$3.multiply( object.parent.matrixWorld ); + + } + + object.applyMatrix4( _m1$3 ); + + object.removeFromParent(); + object.parent = this; + this.children.push( object ); + + object.updateWorldMatrix( false, true ); + + object.dispatchEvent( _addedEvent ); + + _childaddedEvent.child = object; + this.dispatchEvent( _childaddedEvent ); + _childaddedEvent.child = null; + + return this; + + } + + getObjectById( id ) { + + return this.getObjectByProperty( 'id', id ); + + } + + getObjectByName( name ) { + + return this.getObjectByProperty( 'name', name ); + + } + + getObjectByProperty( name, value ) { + + if ( this[ name ] === value ) return this; + + for ( let i = 0, l = this.children.length; i < l; i ++ ) { + + const child = this.children[ i ]; + const object = child.getObjectByProperty( name, value ); + + if ( object !== undefined ) { + + return object; + + } + + } + + return undefined; + + } + + getObjectsByProperty( name, value, result = [] ) { + + if ( this[ name ] === value ) result.push( this ); + + const children = this.children; + + for ( let i = 0, l = children.length; i < l; i ++ ) { + + children[ i ].getObjectsByProperty( name, value, result ); + + } + + return result; + + } + + getWorldPosition( target ) { + + this.updateWorldMatrix( true, false ); + + return target.setFromMatrixPosition( this.matrixWorld ); + + } + + getWorldQuaternion( target ) { + + this.updateWorldMatrix( true, false ); + + this.matrixWorld.decompose( _position$3, target, _scale$2 ); + + return target; + + } + + getWorldScale( target ) { + + this.updateWorldMatrix( true, false ); + + this.matrixWorld.decompose( _position$3, _quaternion$2, target ); + + return target; + + } + + getWorldDirection( target ) { + + this.updateWorldMatrix( true, false ); + + const e = this.matrixWorld.elements; + + return target.set( e[ 8 ], e[ 9 ], e[ 10 ] ).normalize(); + + } + + raycast( /* raycaster, intersects */ ) {} + + traverse( callback ) { + + callback( this ); + + const children = this.children; + + for ( let i = 0, l = children.length; i < l; i ++ ) { + + children[ i ].traverse( callback ); + + } + + } + + traverseVisible( callback ) { + + if ( this.visible === false ) return; + + callback( this ); + + const children = this.children; + + for ( let i = 0, l = children.length; i < l; i ++ ) { + + children[ i ].traverseVisible( callback ); + + } + + } + + traverseAncestors( callback ) { + + const parent = this.parent; + + if ( parent !== null ) { + + callback( parent ); + + parent.traverseAncestors( callback ); + + } + + } + + updateMatrix() { + + this.matrix.compose( this.position, this.quaternion, this.scale ); + + this.matrixWorldNeedsUpdate = true; + + } + + updateMatrixWorld( force ) { + + if ( this.matrixAutoUpdate ) this.updateMatrix(); + + if ( this.matrixWorldNeedsUpdate || force ) { + + if ( this.parent === null ) { + + this.matrixWorld.copy( this.matrix ); + + } else { + + this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix ); + + } + + this.matrixWorldNeedsUpdate = false; + + force = true; + + } + + // update children + + const children = this.children; + + for ( let i = 0, l = children.length; i < l; i ++ ) { + + const child = children[ i ]; + + if ( child.matrixWorldAutoUpdate === true || force === true ) { + + child.updateMatrixWorld( force ); + + } + + } + + } + + updateWorldMatrix( updateParents, updateChildren ) { + + const parent = this.parent; + + if ( updateParents === true && parent !== null && parent.matrixWorldAutoUpdate === true ) { + + parent.updateWorldMatrix( true, false ); + + } + + if ( this.matrixAutoUpdate ) this.updateMatrix(); + + if ( this.parent === null ) { + + this.matrixWorld.copy( this.matrix ); + + } else { + + this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix ); + + } + + // update children + + if ( updateChildren === true ) { + + const children = this.children; + + for ( let i = 0, l = children.length; i < l; i ++ ) { + + const child = children[ i ]; + + if ( child.matrixWorldAutoUpdate === true ) { + + child.updateWorldMatrix( false, true ); + + } + + } + + } + + } + + toJSON( meta ) { + + // meta is a string when called from JSON.stringify + const isRootObject = ( meta === undefined || typeof meta === 'string' ); + + const output = {}; + + // meta is a hash used to collect geometries, materials. + // not providing it implies that this is the root object + // being serialized. + if ( isRootObject ) { + + // initialize meta obj + meta = { + geometries: {}, + materials: {}, + textures: {}, + images: {}, + shapes: {}, + skeletons: {}, + animations: {}, + nodes: {} + }; + + output.metadata = { + version: 4.6, + type: 'Object', + generator: 'Object3D.toJSON' + }; + + } + + // standard Object3D serialization + + const object = {}; + + object.uuid = this.uuid; + object.type = this.type; + + if ( this.name !== '' ) object.name = this.name; + if ( this.castShadow === true ) object.castShadow = true; + if ( this.receiveShadow === true ) object.receiveShadow = true; + if ( this.visible === false ) object.visible = false; + if ( this.frustumCulled === false ) object.frustumCulled = false; + if ( this.renderOrder !== 0 ) object.renderOrder = this.renderOrder; + if ( Object.keys( this.userData ).length > 0 ) object.userData = this.userData; + + object.layers = this.layers.mask; + object.matrix = this.matrix.toArray(); + object.up = this.up.toArray(); + + if ( this.matrixAutoUpdate === false ) object.matrixAutoUpdate = false; + + // object specific properties + + if ( this.isInstancedMesh ) { + + object.type = 'InstancedMesh'; + object.count = this.count; + object.instanceMatrix = this.instanceMatrix.toJSON(); + if ( this.instanceColor !== null ) object.instanceColor = this.instanceColor.toJSON(); + + } + + if ( this.isBatchedMesh ) { + + object.type = 'BatchedMesh'; + object.perObjectFrustumCulled = this.perObjectFrustumCulled; + object.sortObjects = this.sortObjects; + + object.drawRanges = this._drawRanges; + object.reservedRanges = this._reservedRanges; + + object.visibility = this._visibility; + object.active = this._active; + object.bounds = this._bounds.map( bound => ( { + boxInitialized: bound.boxInitialized, + boxMin: bound.box.min.toArray(), + boxMax: bound.box.max.toArray(), + + sphereInitialized: bound.sphereInitialized, + sphereRadius: bound.sphere.radius, + sphereCenter: bound.sphere.center.toArray() + } ) ); + + object.maxGeometryCount = this._maxGeometryCount; + object.maxVertexCount = this._maxVertexCount; + object.maxIndexCount = this._maxIndexCount; + + object.geometryInitialized = this._geometryInitialized; + object.geometryCount = this._geometryCount; + + object.matricesTexture = this._matricesTexture.toJSON( meta ); + + if ( this.boundingSphere !== null ) { + + object.boundingSphere = { + center: object.boundingSphere.center.toArray(), + radius: object.boundingSphere.radius + }; + + } + + if ( this.boundingBox !== null ) { + + object.boundingBox = { + min: object.boundingBox.min.toArray(), + max: object.boundingBox.max.toArray() + }; + + } + + } + + // + + function serialize( library, element ) { + + if ( library[ element.uuid ] === undefined ) { + + library[ element.uuid ] = element.toJSON( meta ); + + } + + return element.uuid; + + } + + if ( this.isScene ) { + + if ( this.background ) { + + if ( this.background.isColor ) { + + object.background = this.background.toJSON(); + + } else if ( this.background.isTexture ) { + + object.background = this.background.toJSON( meta ).uuid; + + } + + } + + if ( this.environment && this.environment.isTexture && this.environment.isRenderTargetTexture !== true ) { + + object.environment = this.environment.toJSON( meta ).uuid; + + } + + } else if ( this.isMesh || this.isLine || this.isPoints ) { + + object.geometry = serialize( meta.geometries, this.geometry ); + + const parameters = this.geometry.parameters; + + if ( parameters !== undefined && parameters.shapes !== undefined ) { + + const shapes = parameters.shapes; + + if ( Array.isArray( shapes ) ) { + + for ( let i = 0, l = shapes.length; i < l; i ++ ) { + + const shape = shapes[ i ]; + + serialize( meta.shapes, shape ); + + } + + } else { + + serialize( meta.shapes, shapes ); + + } + + } + + } + + if ( this.isSkinnedMesh ) { + + object.bindMode = this.bindMode; + object.bindMatrix = this.bindMatrix.toArray(); + + if ( this.skeleton !== undefined ) { + + serialize( meta.skeletons, this.skeleton ); + + object.skeleton = this.skeleton.uuid; + + } + + } + + if ( this.material !== undefined ) { + + if ( Array.isArray( this.material ) ) { + + const uuids = []; + + for ( let i = 0, l = this.material.length; i < l; i ++ ) { + + uuids.push( serialize( meta.materials, this.material[ i ] ) ); + + } + + object.material = uuids; + + } else { + + object.material = serialize( meta.materials, this.material ); + + } + + } + + // + + if ( this.children.length > 0 ) { + + object.children = []; + + for ( let i = 0; i < this.children.length; i ++ ) { + + object.children.push( this.children[ i ].toJSON( meta ).object ); + + } + + } + + // + + if ( this.animations.length > 0 ) { + + object.animations = []; + + for ( let i = 0; i < this.animations.length; i ++ ) { + + const animation = this.animations[ i ]; + + object.animations.push( serialize( meta.animations, animation ) ); + + } + + } + + if ( isRootObject ) { + + const geometries = extractFromCache( meta.geometries ); + const materials = extractFromCache( meta.materials ); + const textures = extractFromCache( meta.textures ); + const images = extractFromCache( meta.images ); + const shapes = extractFromCache( meta.shapes ); + const skeletons = extractFromCache( meta.skeletons ); + const animations = extractFromCache( meta.animations ); + const nodes = extractFromCache( meta.nodes ); + + if ( geometries.length > 0 ) output.geometries = geometries; + if ( materials.length > 0 ) output.materials = materials; + if ( textures.length > 0 ) output.textures = textures; + if ( images.length > 0 ) output.images = images; + if ( shapes.length > 0 ) output.shapes = shapes; + if ( skeletons.length > 0 ) output.skeletons = skeletons; + if ( animations.length > 0 ) output.animations = animations; + if ( nodes.length > 0 ) output.nodes = nodes; + + } + + output.object = object; + + return output; + + // extract data from the cache hash + // remove metadata on each item + // and return as array + function extractFromCache( cache ) { + + const values = []; + for ( const key in cache ) { + + const data = cache[ key ]; + delete data.metadata; + values.push( data ); + + } + + return values; + + } + + } + + clone( recursive ) { + + return new this.constructor().copy( this, recursive ); + + } + + copy( source, recursive = true ) { + + this.name = source.name; + + this.up.copy( source.up ); + + this.position.copy( source.position ); + this.rotation.order = source.rotation.order; + this.quaternion.copy( source.quaternion ); + this.scale.copy( source.scale ); + + this.matrix.copy( source.matrix ); + this.matrixWorld.copy( source.matrixWorld ); + + this.matrixAutoUpdate = source.matrixAutoUpdate; + + this.matrixWorldAutoUpdate = source.matrixWorldAutoUpdate; + this.matrixWorldNeedsUpdate = source.matrixWorldNeedsUpdate; + + this.layers.mask = source.layers.mask; + this.visible = source.visible; + + this.castShadow = source.castShadow; + this.receiveShadow = source.receiveShadow; + + this.frustumCulled = source.frustumCulled; + this.renderOrder = source.renderOrder; + + this.animations = source.animations.slice(); + + this.userData = JSON.parse( JSON.stringify( source.userData ) ); + + if ( recursive === true ) { + + for ( let i = 0; i < source.children.length; i ++ ) { + + const child = source.children[ i ]; + this.add( child.clone() ); + + } + + } + + return this; + + } + +} + +Object3D.DEFAULT_UP = /*@__PURE__*/ new Vector3( 0, 1, 0 ); +Object3D.DEFAULT_MATRIX_AUTO_UPDATE = true; +Object3D.DEFAULT_MATRIX_WORLD_AUTO_UPDATE = true; + +const _v0$1 = /*@__PURE__*/ new Vector3(); +const _v1$3 = /*@__PURE__*/ new Vector3(); +const _v2$2 = /*@__PURE__*/ new Vector3(); +const _v3$2 = /*@__PURE__*/ new Vector3(); + +const _vab = /*@__PURE__*/ new Vector3(); +const _vac = /*@__PURE__*/ new Vector3(); +const _vbc = /*@__PURE__*/ new Vector3(); +const _vap = /*@__PURE__*/ new Vector3(); +const _vbp = /*@__PURE__*/ new Vector3(); +const _vcp = /*@__PURE__*/ new Vector3(); + +class Triangle { + + constructor( a = new Vector3(), b = new Vector3(), c = new Vector3() ) { + + this.a = a; + this.b = b; + this.c = c; + + } + + static getNormal( a, b, c, target ) { + + target.subVectors( c, b ); + _v0$1.subVectors( a, b ); + target.cross( _v0$1 ); + + const targetLengthSq = target.lengthSq(); + if ( targetLengthSq > 0 ) { + + return target.multiplyScalar( 1 / Math.sqrt( targetLengthSq ) ); + + } + + return target.set( 0, 0, 0 ); + + } + + // static/instance method to calculate barycentric coordinates + // based on: http://www.blackpawn.com/texts/pointinpoly/default.html + static getBarycoord( point, a, b, c, target ) { + + _v0$1.subVectors( c, a ); + _v1$3.subVectors( b, a ); + _v2$2.subVectors( point, a ); + + const dot00 = _v0$1.dot( _v0$1 ); + const dot01 = _v0$1.dot( _v1$3 ); + const dot02 = _v0$1.dot( _v2$2 ); + const dot11 = _v1$3.dot( _v1$3 ); + const dot12 = _v1$3.dot( _v2$2 ); + + const denom = ( dot00 * dot11 - dot01 * dot01 ); + + // collinear or singular triangle + if ( denom === 0 ) { + + target.set( 0, 0, 0 ); + return null; + + } + + const invDenom = 1 / denom; + const u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom; + const v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom; + + // barycentric coordinates must always sum to 1 + return target.set( 1 - u - v, v, u ); + + } + + static containsPoint( point, a, b, c ) { + + // if the triangle is degenerate then we can't contain a point + if ( this.getBarycoord( point, a, b, c, _v3$2 ) === null ) { + + return false; + + } + + return ( _v3$2.x >= 0 ) && ( _v3$2.y >= 0 ) && ( ( _v3$2.x + _v3$2.y ) <= 1 ); + + } + + static getInterpolation( point, p1, p2, p3, v1, v2, v3, target ) { + + if ( this.getBarycoord( point, p1, p2, p3, _v3$2 ) === null ) { + + target.x = 0; + target.y = 0; + if ( 'z' in target ) target.z = 0; + if ( 'w' in target ) target.w = 0; + return null; + + } + + target.setScalar( 0 ); + target.addScaledVector( v1, _v3$2.x ); + target.addScaledVector( v2, _v3$2.y ); + target.addScaledVector( v3, _v3$2.z ); + + return target; + + } + + static isFrontFacing( a, b, c, direction ) { + + _v0$1.subVectors( c, b ); + _v1$3.subVectors( a, b ); + + // strictly front facing + return ( _v0$1.cross( _v1$3 ).dot( direction ) < 0 ) ? true : false; + + } + + set( a, b, c ) { + + this.a.copy( a ); + this.b.copy( b ); + this.c.copy( c ); + + return this; + + } + + setFromPointsAndIndices( points, i0, i1, i2 ) { + + this.a.copy( points[ i0 ] ); + this.b.copy( points[ i1 ] ); + this.c.copy( points[ i2 ] ); + + return this; + + } + + setFromAttributeAndIndices( attribute, i0, i1, i2 ) { + + this.a.fromBufferAttribute( attribute, i0 ); + this.b.fromBufferAttribute( attribute, i1 ); + this.c.fromBufferAttribute( attribute, i2 ); + + return this; + + } + + clone() { + + return new this.constructor().copy( this ); + + } + + copy( triangle ) { + + this.a.copy( triangle.a ); + this.b.copy( triangle.b ); + this.c.copy( triangle.c ); + + return this; + + } + + getArea() { + + _v0$1.subVectors( this.c, this.b ); + _v1$3.subVectors( this.a, this.b ); + + return _v0$1.cross( _v1$3 ).length() * 0.5; + + } + + getMidpoint( target ) { + + return target.addVectors( this.a, this.b ).add( this.c ).multiplyScalar( 1 / 3 ); + + } + + getNormal( target ) { + + return Triangle.getNormal( this.a, this.b, this.c, target ); + + } + + getPlane( target ) { + + return target.setFromCoplanarPoints( this.a, this.b, this.c ); + + } + + getBarycoord( point, target ) { + + return Triangle.getBarycoord( point, this.a, this.b, this.c, target ); + + } + + getInterpolation( point, v1, v2, v3, target ) { + + return Triangle.getInterpolation( point, this.a, this.b, this.c, v1, v2, v3, target ); + + } + + containsPoint( point ) { + + return Triangle.containsPoint( point, this.a, this.b, this.c ); + + } + + isFrontFacing( direction ) { + + return Triangle.isFrontFacing( this.a, this.b, this.c, direction ); + + } + + intersectsBox( box ) { + + return box.intersectsTriangle( this ); + + } + + closestPointToPoint( p, target ) { + + const a = this.a, b = this.b, c = this.c; + let v, w; + + // algorithm thanks to Real-Time Collision Detection by Christer Ericson, + // published by Morgan Kaufmann Publishers, (c) 2005 Elsevier Inc., + // under the accompanying license; see chapter 5.1.5 for detailed explanation. + // basically, we're distinguishing which of the voronoi regions of the triangle + // the point lies in with the minimum amount of redundant computation. + + _vab.subVectors( b, a ); + _vac.subVectors( c, a ); + _vap.subVectors( p, a ); + const d1 = _vab.dot( _vap ); + const d2 = _vac.dot( _vap ); + if ( d1 <= 0 && d2 <= 0 ) { + + // vertex region of A; barycentric coords (1, 0, 0) + return target.copy( a ); + + } + + _vbp.subVectors( p, b ); + const d3 = _vab.dot( _vbp ); + const d4 = _vac.dot( _vbp ); + if ( d3 >= 0 && d4 <= d3 ) { + + // vertex region of B; barycentric coords (0, 1, 0) + return target.copy( b ); + + } + + const vc = d1 * d4 - d3 * d2; + if ( vc <= 0 && d1 >= 0 && d3 <= 0 ) { + + v = d1 / ( d1 - d3 ); + // edge region of AB; barycentric coords (1-v, v, 0) + return target.copy( a ).addScaledVector( _vab, v ); + + } + + _vcp.subVectors( p, c ); + const d5 = _vab.dot( _vcp ); + const d6 = _vac.dot( _vcp ); + if ( d6 >= 0 && d5 <= d6 ) { + + // vertex region of C; barycentric coords (0, 0, 1) + return target.copy( c ); + + } + + const vb = d5 * d2 - d1 * d6; + if ( vb <= 0 && d2 >= 0 && d6 <= 0 ) { + + w = d2 / ( d2 - d6 ); + // edge region of AC; barycentric coords (1-w, 0, w) + return target.copy( a ).addScaledVector( _vac, w ); + + } + + const va = d3 * d6 - d5 * d4; + if ( va <= 0 && ( d4 - d3 ) >= 0 && ( d5 - d6 ) >= 0 ) { + + _vbc.subVectors( c, b ); + w = ( d4 - d3 ) / ( ( d4 - d3 ) + ( d5 - d6 ) ); + // edge region of BC; barycentric coords (0, 1-w, w) + return target.copy( b ).addScaledVector( _vbc, w ); // edge region of BC + + } + + // face region + const denom = 1 / ( va + vb + vc ); + // u = va * denom + v = vb * denom; + w = vc * denom; + + return target.copy( a ).addScaledVector( _vab, v ).addScaledVector( _vac, w ); + + } + + equals( triangle ) { + + return triangle.a.equals( this.a ) && triangle.b.equals( this.b ) && triangle.c.equals( this.c ); + + } + +} + +const _colorKeywords = { 'aliceblue': 0xF0F8FF, 'antiquewhite': 0xFAEBD7, 'aqua': 0x00FFFF, 'aquamarine': 0x7FFFD4, 'azure': 0xF0FFFF, + 'beige': 0xF5F5DC, 'bisque': 0xFFE4C4, 'black': 0x000000, 'blanchedalmond': 0xFFEBCD, 'blue': 0x0000FF, 'blueviolet': 0x8A2BE2, + 'brown': 0xA52A2A, 'burlywood': 0xDEB887, 'cadetblue': 0x5F9EA0, 'chartreuse': 0x7FFF00, 'chocolate': 0xD2691E, 'coral': 0xFF7F50, + 'cornflowerblue': 0x6495ED, 'cornsilk': 0xFFF8DC, 'crimson': 0xDC143C, 'cyan': 0x00FFFF, 'darkblue': 0x00008B, 'darkcyan': 0x008B8B, + 'darkgoldenrod': 0xB8860B, 'darkgray': 0xA9A9A9, 'darkgreen': 0x006400, 'darkgrey': 0xA9A9A9, 'darkkhaki': 0xBDB76B, 'darkmagenta': 0x8B008B, + 'darkolivegreen': 0x556B2F, 'darkorange': 0xFF8C00, 'darkorchid': 0x9932CC, 'darkred': 0x8B0000, 'darksalmon': 0xE9967A, 'darkseagreen': 0x8FBC8F, + 'darkslateblue': 0x483D8B, 'darkslategray': 0x2F4F4F, 'darkslategrey': 0x2F4F4F, 'darkturquoise': 0x00CED1, 'darkviolet': 0x9400D3, + 'deeppink': 0xFF1493, 'deepskyblue': 0x00BFFF, 'dimgray': 0x696969, 'dimgrey': 0x696969, 'dodgerblue': 0x1E90FF, 'firebrick': 0xB22222, + 'floralwhite': 0xFFFAF0, 'forestgreen': 0x228B22, 'fuchsia': 0xFF00FF, 'gainsboro': 0xDCDCDC, 'ghostwhite': 0xF8F8FF, 'gold': 0xFFD700, + 'goldenrod': 0xDAA520, 'gray': 0x808080, 'green': 0x008000, 'greenyellow': 0xADFF2F, 'grey': 0x808080, 'honeydew': 0xF0FFF0, 'hotpink': 0xFF69B4, + 'indianred': 0xCD5C5C, 'indigo': 0x4B0082, 'ivory': 0xFFFFF0, 'khaki': 0xF0E68C, 'lavender': 0xE6E6FA, 'lavenderblush': 0xFFF0F5, 'lawngreen': 0x7CFC00, + 'lemonchiffon': 0xFFFACD, 'lightblue': 0xADD8E6, 'lightcoral': 0xF08080, 'lightcyan': 0xE0FFFF, 'lightgoldenrodyellow': 0xFAFAD2, 'lightgray': 0xD3D3D3, + 'lightgreen': 0x90EE90, 'lightgrey': 0xD3D3D3, 'lightpink': 0xFFB6C1, 'lightsalmon': 0xFFA07A, 'lightseagreen': 0x20B2AA, 'lightskyblue': 0x87CEFA, + 'lightslategray': 0x778899, 'lightslategrey': 0x778899, 'lightsteelblue': 0xB0C4DE, 'lightyellow': 0xFFFFE0, 'lime': 0x00FF00, 'limegreen': 0x32CD32, + 'linen': 0xFAF0E6, 'magenta': 0xFF00FF, 'maroon': 0x800000, 'mediumaquamarine': 0x66CDAA, 'mediumblue': 0x0000CD, 'mediumorchid': 0xBA55D3, + 'mediumpurple': 0x9370DB, 'mediumseagreen': 0x3CB371, 'mediumslateblue': 0x7B68EE, 'mediumspringgreen': 0x00FA9A, 'mediumturquoise': 0x48D1CC, + 'mediumvioletred': 0xC71585, 'midnightblue': 0x191970, 'mintcream': 0xF5FFFA, 'mistyrose': 0xFFE4E1, 'moccasin': 0xFFE4B5, 'navajowhite': 0xFFDEAD, + 'navy': 0x000080, 'oldlace': 0xFDF5E6, 'olive': 0x808000, 'olivedrab': 0x6B8E23, 'orange': 0xFFA500, 'orangered': 0xFF4500, 'orchid': 0xDA70D6, + 'palegoldenrod': 0xEEE8AA, 'palegreen': 0x98FB98, 'paleturquoise': 0xAFEEEE, 'palevioletred': 0xDB7093, 'papayawhip': 0xFFEFD5, 'peachpuff': 0xFFDAB9, + 'peru': 0xCD853F, 'pink': 0xFFC0CB, 'plum': 0xDDA0DD, 'powderblue': 0xB0E0E6, 'purple': 0x800080, 'rebeccapurple': 0x663399, 'red': 0xFF0000, 'rosybrown': 0xBC8F8F, + 'royalblue': 0x4169E1, 'saddlebrown': 0x8B4513, 'salmon': 0xFA8072, 'sandybrown': 0xF4A460, 'seagreen': 0x2E8B57, 'seashell': 0xFFF5EE, + 'sienna': 0xA0522D, 'silver': 0xC0C0C0, 'skyblue': 0x87CEEB, 'slateblue': 0x6A5ACD, 'slategray': 0x708090, 'slategrey': 0x708090, 'snow': 0xFFFAFA, + 'springgreen': 0x00FF7F, 'steelblue': 0x4682B4, 'tan': 0xD2B48C, 'teal': 0x008080, 'thistle': 0xD8BFD8, 'tomato': 0xFF6347, 'turquoise': 0x40E0D0, + 'violet': 0xEE82EE, 'wheat': 0xF5DEB3, 'white': 0xFFFFFF, 'whitesmoke': 0xF5F5F5, 'yellow': 0xFFFF00, 'yellowgreen': 0x9ACD32 }; + +const _hslA = { h: 0, s: 0, l: 0 }; +const _hslB = { h: 0, s: 0, l: 0 }; + +function hue2rgb( p, q, t ) { + + if ( t < 0 ) t += 1; + if ( t > 1 ) t -= 1; + if ( t < 1 / 6 ) return p + ( q - p ) * 6 * t; + if ( t < 1 / 2 ) return q; + if ( t < 2 / 3 ) return p + ( q - p ) * 6 * ( 2 / 3 - t ); + return p; + +} + +class Color { + + constructor( r, g, b ) { + + this.isColor = true; + + this.r = 1; + this.g = 1; + this.b = 1; + + return this.set( r, g, b ); + + } + + set( r, g, b ) { + + if ( g === undefined && b === undefined ) { + + // r is THREE.Color, hex or string + + const value = r; + + if ( value && value.isColor ) { + + this.copy( value ); + + } else if ( typeof value === 'number' ) { + + this.setHex( value ); + + } else if ( typeof value === 'string' ) { + + this.setStyle( value ); + + } + + } else { + + this.setRGB( r, g, b ); + + } + + return this; + + } + + setScalar( scalar ) { + + this.r = scalar; + this.g = scalar; + this.b = scalar; + + return this; + + } + + setHex( hex, colorSpace = SRGBColorSpace ) { + + hex = Math.floor( hex ); + + this.r = ( hex >> 16 & 255 ) / 255; + this.g = ( hex >> 8 & 255 ) / 255; + this.b = ( hex & 255 ) / 255; + + ColorManagement.toWorkingColorSpace( this, colorSpace ); + + return this; + + } + + setRGB( r, g, b, colorSpace = ColorManagement.workingColorSpace ) { + + this.r = r; + this.g = g; + this.b = b; + + ColorManagement.toWorkingColorSpace( this, colorSpace ); + + return this; + + } + + setHSL( h, s, l, colorSpace = ColorManagement.workingColorSpace ) { + + // h,s,l ranges are in 0.0 - 1.0 + h = euclideanModulo( h, 1 ); + s = clamp( s, 0, 1 ); + l = clamp( l, 0, 1 ); + + if ( s === 0 ) { + + this.r = this.g = this.b = l; + + } else { + + const p = l <= 0.5 ? l * ( 1 + s ) : l + s - ( l * s ); + const q = ( 2 * l ) - p; + + this.r = hue2rgb( q, p, h + 1 / 3 ); + this.g = hue2rgb( q, p, h ); + this.b = hue2rgb( q, p, h - 1 / 3 ); + + } + + ColorManagement.toWorkingColorSpace( this, colorSpace ); + + return this; + + } + + setStyle( style, colorSpace = SRGBColorSpace ) { + + function handleAlpha( string ) { + + if ( string === undefined ) return; + + if ( parseFloat( string ) < 1 ) { + + console.warn( 'THREE.Color: Alpha component of ' + style + ' will be ignored.' ); + + } + + } + + + let m; + + if ( m = /^(\w+)\(([^\)]*)\)/.exec( style ) ) { + + // rgb / hsl + + let color; + const name = m[ 1 ]; + const components = m[ 2 ]; + + switch ( name ) { + + case 'rgb': + case 'rgba': + + if ( color = /^\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*(\d*\.?\d+)\s*)?$/.exec( components ) ) { + + // rgb(255,0,0) rgba(255,0,0,0.5) + + handleAlpha( color[ 4 ] ); + + return this.setRGB( + Math.min( 255, parseInt( color[ 1 ], 10 ) ) / 255, + Math.min( 255, parseInt( color[ 2 ], 10 ) ) / 255, + Math.min( 255, parseInt( color[ 3 ], 10 ) ) / 255, + colorSpace + ); + + } + + if ( color = /^\s*(\d+)\%\s*,\s*(\d+)\%\s*,\s*(\d+)\%\s*(?:,\s*(\d*\.?\d+)\s*)?$/.exec( components ) ) { + + // rgb(100%,0%,0%) rgba(100%,0%,0%,0.5) + + handleAlpha( color[ 4 ] ); + + return this.setRGB( + Math.min( 100, parseInt( color[ 1 ], 10 ) ) / 100, + Math.min( 100, parseInt( color[ 2 ], 10 ) ) / 100, + Math.min( 100, parseInt( color[ 3 ], 10 ) ) / 100, + colorSpace + ); + + } + + break; + + case 'hsl': + case 'hsla': + + if ( color = /^\s*(\d*\.?\d+)\s*,\s*(\d*\.?\d+)\%\s*,\s*(\d*\.?\d+)\%\s*(?:,\s*(\d*\.?\d+)\s*)?$/.exec( components ) ) { + + // hsl(120,50%,50%) hsla(120,50%,50%,0.5) + + handleAlpha( color[ 4 ] ); + + return this.setHSL( + parseFloat( color[ 1 ] ) / 360, + parseFloat( color[ 2 ] ) / 100, + parseFloat( color[ 3 ] ) / 100, + colorSpace + ); + + } + + break; + + default: + + console.warn( 'THREE.Color: Unknown color model ' + style ); + + } + + } else if ( m = /^\#([A-Fa-f\d]+)$/.exec( style ) ) { + + // hex color + + const hex = m[ 1 ]; + const size = hex.length; + + if ( size === 3 ) { + + // #ff0 + return this.setRGB( + parseInt( hex.charAt( 0 ), 16 ) / 15, + parseInt( hex.charAt( 1 ), 16 ) / 15, + parseInt( hex.charAt( 2 ), 16 ) / 15, + colorSpace + ); + + } else if ( size === 6 ) { + + // #ff0000 + return this.setHex( parseInt( hex, 16 ), colorSpace ); + + } else { + + console.warn( 'THREE.Color: Invalid hex color ' + style ); + + } + + } else if ( style && style.length > 0 ) { + + return this.setColorName( style, colorSpace ); + + } + + return this; + + } + + setColorName( style, colorSpace = SRGBColorSpace ) { + + // color keywords + const hex = _colorKeywords[ style.toLowerCase() ]; + + if ( hex !== undefined ) { + + // red + this.setHex( hex, colorSpace ); + + } else { + + // unknown color + console.warn( 'THREE.Color: Unknown color ' + style ); + + } + + return this; + + } + + clone() { + + return new this.constructor( this.r, this.g, this.b ); + + } + + copy( color ) { + + this.r = color.r; + this.g = color.g; + this.b = color.b; + + return this; + + } + + copySRGBToLinear( color ) { + + this.r = SRGBToLinear( color.r ); + this.g = SRGBToLinear( color.g ); + this.b = SRGBToLinear( color.b ); + + return this; + + } + + copyLinearToSRGB( color ) { + + this.r = LinearToSRGB( color.r ); + this.g = LinearToSRGB( color.g ); + this.b = LinearToSRGB( color.b ); + + return this; + + } + + convertSRGBToLinear() { + + this.copySRGBToLinear( this ); + + return this; + + } + + convertLinearToSRGB() { + + this.copyLinearToSRGB( this ); + + return this; + + } + + getHex( colorSpace = SRGBColorSpace ) { + + ColorManagement.fromWorkingColorSpace( _color.copy( this ), colorSpace ); + + return Math.round( clamp( _color.r * 255, 0, 255 ) ) * 65536 + Math.round( clamp( _color.g * 255, 0, 255 ) ) * 256 + Math.round( clamp( _color.b * 255, 0, 255 ) ); + + } + + getHexString( colorSpace = SRGBColorSpace ) { + + return ( '000000' + this.getHex( colorSpace ).toString( 16 ) ).slice( - 6 ); + + } + + getHSL( target, colorSpace = ColorManagement.workingColorSpace ) { + + // h,s,l ranges are in 0.0 - 1.0 + + ColorManagement.fromWorkingColorSpace( _color.copy( this ), colorSpace ); + + const r = _color.r, g = _color.g, b = _color.b; + + const max = Math.max( r, g, b ); + const min = Math.min( r, g, b ); + + let hue, saturation; + const lightness = ( min + max ) / 2.0; + + if ( min === max ) { + + hue = 0; + saturation = 0; + + } else { + + const delta = max - min; + + saturation = lightness <= 0.5 ? delta / ( max + min ) : delta / ( 2 - max - min ); + + switch ( max ) { + + case r: hue = ( g - b ) / delta + ( g < b ? 6 : 0 ); break; + case g: hue = ( b - r ) / delta + 2; break; + case b: hue = ( r - g ) / delta + 4; break; + + } + + hue /= 6; + + } + + target.h = hue; + target.s = saturation; + target.l = lightness; + + return target; + + } + + getRGB( target, colorSpace = ColorManagement.workingColorSpace ) { + + ColorManagement.fromWorkingColorSpace( _color.copy( this ), colorSpace ); + + target.r = _color.r; + target.g = _color.g; + target.b = _color.b; + + return target; + + } + + getStyle( colorSpace = SRGBColorSpace ) { + + ColorManagement.fromWorkingColorSpace( _color.copy( this ), colorSpace ); + + const r = _color.r, g = _color.g, b = _color.b; + + if ( colorSpace !== SRGBColorSpace ) { + + // Requires CSS Color Module Level 4 (https://www.w3.org/TR/css-color-4/). + return `color(${ colorSpace } ${ r.toFixed( 3 ) } ${ g.toFixed( 3 ) } ${ b.toFixed( 3 ) })`; + + } + + return `rgb(${ Math.round( r * 255 ) },${ Math.round( g * 255 ) },${ Math.round( b * 255 ) })`; + + } + + offsetHSL( h, s, l ) { + + this.getHSL( _hslA ); + + return this.setHSL( _hslA.h + h, _hslA.s + s, _hslA.l + l ); + + } + + add( color ) { + + this.r += color.r; + this.g += color.g; + this.b += color.b; + + return this; + + } + + addColors( color1, color2 ) { + + this.r = color1.r + color2.r; + this.g = color1.g + color2.g; + this.b = color1.b + color2.b; + + return this; + + } + + addScalar( s ) { + + this.r += s; + this.g += s; + this.b += s; + + return this; + + } + + sub( color ) { + + this.r = Math.max( 0, this.r - color.r ); + this.g = Math.max( 0, this.g - color.g ); + this.b = Math.max( 0, this.b - color.b ); + + return this; + + } + + multiply( color ) { + + this.r *= color.r; + this.g *= color.g; + this.b *= color.b; + + return this; + + } + + multiplyScalar( s ) { + + this.r *= s; + this.g *= s; + this.b *= s; + + return this; + + } + + lerp( color, alpha ) { + + this.r += ( color.r - this.r ) * alpha; + this.g += ( color.g - this.g ) * alpha; + this.b += ( color.b - this.b ) * alpha; + + return this; + + } + + lerpColors( color1, color2, alpha ) { + + this.r = color1.r + ( color2.r - color1.r ) * alpha; + this.g = color1.g + ( color2.g - color1.g ) * alpha; + this.b = color1.b + ( color2.b - color1.b ) * alpha; + + return this; + + } + + lerpHSL( color, alpha ) { + + this.getHSL( _hslA ); + color.getHSL( _hslB ); + + const h = lerp( _hslA.h, _hslB.h, alpha ); + const s = lerp( _hslA.s, _hslB.s, alpha ); + const l = lerp( _hslA.l, _hslB.l, alpha ); + + this.setHSL( h, s, l ); + + return this; + + } + + setFromVector3( v ) { + + this.r = v.x; + this.g = v.y; + this.b = v.z; + + return this; + + } + + applyMatrix3( m ) { + + const r = this.r, g = this.g, b = this.b; + const e = m.elements; + + this.r = e[ 0 ] * r + e[ 3 ] * g + e[ 6 ] * b; + this.g = e[ 1 ] * r + e[ 4 ] * g + e[ 7 ] * b; + this.b = e[ 2 ] * r + e[ 5 ] * g + e[ 8 ] * b; + + return this; + + } + + equals( c ) { + + return ( c.r === this.r ) && ( c.g === this.g ) && ( c.b === this.b ); + + } + + fromArray( array, offset = 0 ) { + + this.r = array[ offset ]; + this.g = array[ offset + 1 ]; + this.b = array[ offset + 2 ]; + + return this; + + } + + toArray( array = [], offset = 0 ) { + + array[ offset ] = this.r; + array[ offset + 1 ] = this.g; + array[ offset + 2 ] = this.b; + + return array; + + } + + fromBufferAttribute( attribute, index ) { + + this.r = attribute.getX( index ); + this.g = attribute.getY( index ); + this.b = attribute.getZ( index ); + + return this; + + } + + toJSON() { + + return this.getHex(); + + } + + *[ Symbol.iterator ]() { + + yield this.r; + yield this.g; + yield this.b; + + } + +} + +const _color = /*@__PURE__*/ new Color(); + +Color.NAMES = _colorKeywords; + +let _materialId = 0; + +class Material extends EventDispatcher { + + constructor() { + + super(); + + this.isMaterial = true; + + Object.defineProperty( this, 'id', { value: _materialId ++ } ); + + this.uuid = generateUUID(); + + this.name = ''; + this.type = 'Material'; + + this.blending = NormalBlending; + this.side = FrontSide; + this.vertexColors = false; + + this.opacity = 1; + this.transparent = false; + this.alphaHash = false; + + this.blendSrc = SrcAlphaFactor; + this.blendDst = OneMinusSrcAlphaFactor; + this.blendEquation = AddEquation; + this.blendSrcAlpha = null; + this.blendDstAlpha = null; + this.blendEquationAlpha = null; + this.blendColor = new Color( 0, 0, 0 ); + this.blendAlpha = 0; + + this.depthFunc = LessEqualDepth; + this.depthTest = true; + this.depthWrite = true; + + this.stencilWriteMask = 0xff; + this.stencilFunc = AlwaysStencilFunc; + this.stencilRef = 0; + this.stencilFuncMask = 0xff; + this.stencilFail = KeepStencilOp; + this.stencilZFail = KeepStencilOp; + this.stencilZPass = KeepStencilOp; + this.stencilWrite = false; + + this.clippingPlanes = null; + this.clipIntersection = false; + this.clipShadows = false; + + this.shadowSide = null; + + this.colorWrite = true; + + this.precision = null; // override the renderer's default precision for this material + + this.polygonOffset = false; + this.polygonOffsetFactor = 0; + this.polygonOffsetUnits = 0; + + this.dithering = false; + + this.alphaToCoverage = false; + this.premultipliedAlpha = false; + this.forceSinglePass = false; + + this.visible = true; + + this.toneMapped = true; + + this.userData = {}; + + this.version = 0; + + this._alphaTest = 0; + + } + + get alphaTest() { + + return this._alphaTest; + + } + + set alphaTest( value ) { + + if ( this._alphaTest > 0 !== value > 0 ) { + + this.version ++; + + } + + this._alphaTest = value; + + } + + onBuild( /* shaderobject, renderer */ ) {} + + onBeforeRender( /* renderer, scene, camera, geometry, object, group */ ) {} + + onBeforeCompile( /* shaderobject, renderer */ ) {} + + customProgramCacheKey() { + + return this.onBeforeCompile.toString(); + + } + + setValues( values ) { + + if ( values === undefined ) return; + + for ( const key in values ) { + + const newValue = values[ key ]; + + if ( newValue === undefined ) { + + console.warn( `THREE.Material: parameter '${ key }' has value of undefined.` ); + continue; + + } + + const currentValue = this[ key ]; + + if ( currentValue === undefined ) { + + console.warn( `THREE.Material: '${ key }' is not a property of THREE.${ this.type }.` ); + continue; + + } + + if ( currentValue && currentValue.isColor ) { + + currentValue.set( newValue ); + + } else if ( ( currentValue && currentValue.isVector3 ) && ( newValue && newValue.isVector3 ) ) { + + currentValue.copy( newValue ); + + } else { + + this[ key ] = newValue; + + } + + } + + } + + toJSON( meta ) { + + const isRootObject = ( meta === undefined || typeof meta === 'string' ); + + if ( isRootObject ) { + + meta = { + textures: {}, + images: {} + }; + + } + + const data = { + metadata: { + version: 4.6, + type: 'Material', + generator: 'Material.toJSON' + } + }; + + // standard Material serialization + data.uuid = this.uuid; + data.type = this.type; + + if ( this.name !== '' ) data.name = this.name; + + if ( this.color && this.color.isColor ) data.color = this.color.getHex(); + + if ( this.roughness !== undefined ) data.roughness = this.roughness; + if ( this.metalness !== undefined ) data.metalness = this.metalness; + + if ( this.sheen !== undefined ) data.sheen = this.sheen; + if ( this.sheenColor && this.sheenColor.isColor ) data.sheenColor = this.sheenColor.getHex(); + if ( this.sheenRoughness !== undefined ) data.sheenRoughness = this.sheenRoughness; + if ( this.emissive && this.emissive.isColor ) data.emissive = this.emissive.getHex(); + if ( this.emissiveIntensity !== undefined && this.emissiveIntensity !== 1 ) data.emissiveIntensity = this.emissiveIntensity; + + if ( this.specular && this.specular.isColor ) data.specular = this.specular.getHex(); + if ( this.specularIntensity !== undefined ) data.specularIntensity = this.specularIntensity; + if ( this.specularColor && this.specularColor.isColor ) data.specularColor = this.specularColor.getHex(); + if ( this.shininess !== undefined ) data.shininess = this.shininess; + if ( this.clearcoat !== undefined ) data.clearcoat = this.clearcoat; + if ( this.clearcoatRoughness !== undefined ) data.clearcoatRoughness = this.clearcoatRoughness; + + if ( this.clearcoatMap && this.clearcoatMap.isTexture ) { + + data.clearcoatMap = this.clearcoatMap.toJSON( meta ).uuid; + + } + + if ( this.clearcoatRoughnessMap && this.clearcoatRoughnessMap.isTexture ) { + + data.clearcoatRoughnessMap = this.clearcoatRoughnessMap.toJSON( meta ).uuid; + + } + + if ( this.clearcoatNormalMap && this.clearcoatNormalMap.isTexture ) { + + data.clearcoatNormalMap = this.clearcoatNormalMap.toJSON( meta ).uuid; + data.clearcoatNormalScale = this.clearcoatNormalScale.toArray(); + + } + + if ( this.dispersion !== undefined ) data.dispersion = this.dispersion; + + if ( this.iridescence !== undefined ) data.iridescence = this.iridescence; + if ( this.iridescenceIOR !== undefined ) data.iridescenceIOR = this.iridescenceIOR; + if ( this.iridescenceThicknessRange !== undefined ) data.iridescenceThicknessRange = this.iridescenceThicknessRange; + + if ( this.iridescenceMap && this.iridescenceMap.isTexture ) { + + data.iridescenceMap = this.iridescenceMap.toJSON( meta ).uuid; + + } + + if ( this.iridescenceThicknessMap && this.iridescenceThicknessMap.isTexture ) { + + data.iridescenceThicknessMap = this.iridescenceThicknessMap.toJSON( meta ).uuid; + + } + + if ( this.anisotropy !== undefined ) data.anisotropy = this.anisotropy; + if ( this.anisotropyRotation !== undefined ) data.anisotropyRotation = this.anisotropyRotation; + + if ( this.anisotropyMap && this.anisotropyMap.isTexture ) { + + data.anisotropyMap = this.anisotropyMap.toJSON( meta ).uuid; + + } + + if ( this.map && this.map.isTexture ) data.map = this.map.toJSON( meta ).uuid; + if ( this.matcap && this.matcap.isTexture ) data.matcap = this.matcap.toJSON( meta ).uuid; + if ( this.alphaMap && this.alphaMap.isTexture ) data.alphaMap = this.alphaMap.toJSON( meta ).uuid; + + if ( this.lightMap && this.lightMap.isTexture ) { + + data.lightMap = this.lightMap.toJSON( meta ).uuid; + data.lightMapIntensity = this.lightMapIntensity; + + } + + if ( this.aoMap && this.aoMap.isTexture ) { + + data.aoMap = this.aoMap.toJSON( meta ).uuid; + data.aoMapIntensity = this.aoMapIntensity; + + } + + if ( this.bumpMap && this.bumpMap.isTexture ) { + + data.bumpMap = this.bumpMap.toJSON( meta ).uuid; + data.bumpScale = this.bumpScale; + + } + + if ( this.normalMap && this.normalMap.isTexture ) { + + data.normalMap = this.normalMap.toJSON( meta ).uuid; + data.normalMapType = this.normalMapType; + data.normalScale = this.normalScale.toArray(); + + } + + if ( this.displacementMap && this.displacementMap.isTexture ) { + + data.displacementMap = this.displacementMap.toJSON( meta ).uuid; + data.displacementScale = this.displacementScale; + data.displacementBias = this.displacementBias; + + } + + if ( this.roughnessMap && this.roughnessMap.isTexture ) data.roughnessMap = this.roughnessMap.toJSON( meta ).uuid; + if ( this.metalnessMap && this.metalnessMap.isTexture ) data.metalnessMap = this.metalnessMap.toJSON( meta ).uuid; + + if ( this.emissiveMap && this.emissiveMap.isTexture ) data.emissiveMap = this.emissiveMap.toJSON( meta ).uuid; + if ( this.specularMap && this.specularMap.isTexture ) data.specularMap = this.specularMap.toJSON( meta ).uuid; + if ( this.specularIntensityMap && this.specularIntensityMap.isTexture ) data.specularIntensityMap = this.specularIntensityMap.toJSON( meta ).uuid; + if ( this.specularColorMap && this.specularColorMap.isTexture ) data.specularColorMap = this.specularColorMap.toJSON( meta ).uuid; + + if ( this.envMap && this.envMap.isTexture ) { + + data.envMap = this.envMap.toJSON( meta ).uuid; + + if ( this.combine !== undefined ) data.combine = this.combine; + + } + + if ( this.envMapRotation !== undefined ) data.envMapRotation = this.envMapRotation.toArray(); + if ( this.envMapIntensity !== undefined ) data.envMapIntensity = this.envMapIntensity; + if ( this.reflectivity !== undefined ) data.reflectivity = this.reflectivity; + if ( this.refractionRatio !== undefined ) data.refractionRatio = this.refractionRatio; + + if ( this.gradientMap && this.gradientMap.isTexture ) { + + data.gradientMap = this.gradientMap.toJSON( meta ).uuid; + + } + + if ( this.transmission !== undefined ) data.transmission = this.transmission; + if ( this.transmissionMap && this.transmissionMap.isTexture ) data.transmissionMap = this.transmissionMap.toJSON( meta ).uuid; + if ( this.thickness !== undefined ) data.thickness = this.thickness; + if ( this.thicknessMap && this.thicknessMap.isTexture ) data.thicknessMap = this.thicknessMap.toJSON( meta ).uuid; + if ( this.attenuationDistance !== undefined && this.attenuationDistance !== Infinity ) data.attenuationDistance = this.attenuationDistance; + if ( this.attenuationColor !== undefined ) data.attenuationColor = this.attenuationColor.getHex(); + + if ( this.size !== undefined ) data.size = this.size; + if ( this.shadowSide !== null ) data.shadowSide = this.shadowSide; + if ( this.sizeAttenuation !== undefined ) data.sizeAttenuation = this.sizeAttenuation; + + if ( this.blending !== NormalBlending ) data.blending = this.blending; + if ( this.side !== FrontSide ) data.side = this.side; + if ( this.vertexColors === true ) data.vertexColors = true; + + if ( this.opacity < 1 ) data.opacity = this.opacity; + if ( this.transparent === true ) data.transparent = true; + + if ( this.blendSrc !== SrcAlphaFactor ) data.blendSrc = this.blendSrc; + if ( this.blendDst !== OneMinusSrcAlphaFactor ) data.blendDst = this.blendDst; + if ( this.blendEquation !== AddEquation ) data.blendEquation = this.blendEquation; + if ( this.blendSrcAlpha !== null ) data.blendSrcAlpha = this.blendSrcAlpha; + if ( this.blendDstAlpha !== null ) data.blendDstAlpha = this.blendDstAlpha; + if ( this.blendEquationAlpha !== null ) data.blendEquationAlpha = this.blendEquationAlpha; + if ( this.blendColor && this.blendColor.isColor ) data.blendColor = this.blendColor.getHex(); + if ( this.blendAlpha !== 0 ) data.blendAlpha = this.blendAlpha; + + if ( this.depthFunc !== LessEqualDepth ) data.depthFunc = this.depthFunc; + if ( this.depthTest === false ) data.depthTest = this.depthTest; + if ( this.depthWrite === false ) data.depthWrite = this.depthWrite; + if ( this.colorWrite === false ) data.colorWrite = this.colorWrite; + + if ( this.stencilWriteMask !== 0xff ) data.stencilWriteMask = this.stencilWriteMask; + if ( this.stencilFunc !== AlwaysStencilFunc ) data.stencilFunc = this.stencilFunc; + if ( this.stencilRef !== 0 ) data.stencilRef = this.stencilRef; + if ( this.stencilFuncMask !== 0xff ) data.stencilFuncMask = this.stencilFuncMask; + if ( this.stencilFail !== KeepStencilOp ) data.stencilFail = this.stencilFail; + if ( this.stencilZFail !== KeepStencilOp ) data.stencilZFail = this.stencilZFail; + if ( this.stencilZPass !== KeepStencilOp ) data.stencilZPass = this.stencilZPass; + if ( this.stencilWrite === true ) data.stencilWrite = this.stencilWrite; + + // rotation (SpriteMaterial) + if ( this.rotation !== undefined && this.rotation !== 0 ) data.rotation = this.rotation; + + if ( this.polygonOffset === true ) data.polygonOffset = true; + if ( this.polygonOffsetFactor !== 0 ) data.polygonOffsetFactor = this.polygonOffsetFactor; + if ( this.polygonOffsetUnits !== 0 ) data.polygonOffsetUnits = this.polygonOffsetUnits; + + if ( this.linewidth !== undefined && this.linewidth !== 1 ) data.linewidth = this.linewidth; + if ( this.dashSize !== undefined ) data.dashSize = this.dashSize; + if ( this.gapSize !== undefined ) data.gapSize = this.gapSize; + if ( this.scale !== undefined ) data.scale = this.scale; + + if ( this.dithering === true ) data.dithering = true; + + if ( this.alphaTest > 0 ) data.alphaTest = this.alphaTest; + if ( this.alphaHash === true ) data.alphaHash = true; + if ( this.alphaToCoverage === true ) data.alphaToCoverage = true; + if ( this.premultipliedAlpha === true ) data.premultipliedAlpha = true; + if ( this.forceSinglePass === true ) data.forceSinglePass = true; + + if ( this.wireframe === true ) data.wireframe = true; + if ( this.wireframeLinewidth > 1 ) data.wireframeLinewidth = this.wireframeLinewidth; + if ( this.wireframeLinecap !== 'round' ) data.wireframeLinecap = this.wireframeLinecap; + if ( this.wireframeLinejoin !== 'round' ) data.wireframeLinejoin = this.wireframeLinejoin; + + if ( this.flatShading === true ) data.flatShading = true; + + if ( this.visible === false ) data.visible = false; + + if ( this.toneMapped === false ) data.toneMapped = false; + + if ( this.fog === false ) data.fog = false; + + if ( Object.keys( this.userData ).length > 0 ) data.userData = this.userData; + + // TODO: Copied from Object3D.toJSON + + function extractFromCache( cache ) { + + const values = []; + + for ( const key in cache ) { + + const data = cache[ key ]; + delete data.metadata; + values.push( data ); + + } + + return values; + + } + + if ( isRootObject ) { + + const textures = extractFromCache( meta.textures ); + const images = extractFromCache( meta.images ); + + if ( textures.length > 0 ) data.textures = textures; + if ( images.length > 0 ) data.images = images; + + } + + return data; + + } + + clone() { + + return new this.constructor().copy( this ); + + } + + copy( source ) { + + this.name = source.name; + + this.blending = source.blending; + this.side = source.side; + this.vertexColors = source.vertexColors; + + this.opacity = source.opacity; + this.transparent = source.transparent; + + this.blendSrc = source.blendSrc; + this.blendDst = source.blendDst; + this.blendEquation = source.blendEquation; + this.blendSrcAlpha = source.blendSrcAlpha; + this.blendDstAlpha = source.blendDstAlpha; + this.blendEquationAlpha = source.blendEquationAlpha; + this.blendColor.copy( source.blendColor ); + this.blendAlpha = source.blendAlpha; + + this.depthFunc = source.depthFunc; + this.depthTest = source.depthTest; + this.depthWrite = source.depthWrite; + + this.stencilWriteMask = source.stencilWriteMask; + this.stencilFunc = source.stencilFunc; + this.stencilRef = source.stencilRef; + this.stencilFuncMask = source.stencilFuncMask; + this.stencilFail = source.stencilFail; + this.stencilZFail = source.stencilZFail; + this.stencilZPass = source.stencilZPass; + this.stencilWrite = source.stencilWrite; + + const srcPlanes = source.clippingPlanes; + let dstPlanes = null; + + if ( srcPlanes !== null ) { + + const n = srcPlanes.length; + dstPlanes = new Array( n ); + + for ( let i = 0; i !== n; ++ i ) { + + dstPlanes[ i ] = srcPlanes[ i ].clone(); + + } + + } + + this.clippingPlanes = dstPlanes; + this.clipIntersection = source.clipIntersection; + this.clipShadows = source.clipShadows; + + this.shadowSide = source.shadowSide; + + this.colorWrite = source.colorWrite; + + this.precision = source.precision; + + this.polygonOffset = source.polygonOffset; + this.polygonOffsetFactor = source.polygonOffsetFactor; + this.polygonOffsetUnits = source.polygonOffsetUnits; + + this.dithering = source.dithering; + + this.alphaTest = source.alphaTest; + this.alphaHash = source.alphaHash; + this.alphaToCoverage = source.alphaToCoverage; + this.premultipliedAlpha = source.premultipliedAlpha; + this.forceSinglePass = source.forceSinglePass; + + this.visible = source.visible; + + this.toneMapped = source.toneMapped; + + this.userData = JSON.parse( JSON.stringify( source.userData ) ); + + return this; + + } + + dispose() { + + this.dispatchEvent( { type: 'dispose' } ); + + } + + set needsUpdate( value ) { + + if ( value === true ) this.version ++; + + } + +} + +class MeshBasicMaterial extends Material { + + constructor( parameters ) { + + super(); + + this.isMeshBasicMaterial = true; + + this.type = 'MeshBasicMaterial'; + + this.color = new Color( 0xffffff ); // emissive + + this.map = null; + + this.lightMap = null; + this.lightMapIntensity = 1.0; + + this.aoMap = null; + this.aoMapIntensity = 1.0; + + this.specularMap = null; + + this.alphaMap = null; + + this.envMap = null; + this.envMapRotation = new Euler(); + this.combine = MultiplyOperation; + this.reflectivity = 1; + this.refractionRatio = 0.98; + + this.wireframe = false; + this.wireframeLinewidth = 1; + this.wireframeLinecap = 'round'; + this.wireframeLinejoin = 'round'; + + this.fog = true; + + this.setValues( parameters ); + + } + + copy( source ) { + + super.copy( source ); + + this.color.copy( source.color ); + + this.map = source.map; + + this.lightMap = source.lightMap; + this.lightMapIntensity = source.lightMapIntensity; + + this.aoMap = source.aoMap; + this.aoMapIntensity = source.aoMapIntensity; + + this.specularMap = source.specularMap; + + this.alphaMap = source.alphaMap; + + this.envMap = source.envMap; + this.envMapRotation.copy( source.envMapRotation ); + this.combine = source.combine; + this.reflectivity = source.reflectivity; + this.refractionRatio = source.refractionRatio; + + this.wireframe = source.wireframe; + this.wireframeLinewidth = source.wireframeLinewidth; + this.wireframeLinecap = source.wireframeLinecap; + this.wireframeLinejoin = source.wireframeLinejoin; + + this.fog = source.fog; + + return this; + + } + +} + +// Fast Half Float Conversions, http://www.fox-toolkit.org/ftp/fasthalffloatconversion.pdf + +const _tables = /*@__PURE__*/ _generateTables(); + +function _generateTables() { + + // float32 to float16 helpers + + const buffer = new ArrayBuffer( 4 ); + const floatView = new Float32Array( buffer ); + const uint32View = new Uint32Array( buffer ); + + const baseTable = new Uint32Array( 512 ); + const shiftTable = new Uint32Array( 512 ); + + for ( let i = 0; i < 256; ++ i ) { + + const e = i - 127; + + // very small number (0, -0) + + if ( e < - 27 ) { + + baseTable[ i ] = 0x0000; + baseTable[ i | 0x100 ] = 0x8000; + shiftTable[ i ] = 24; + shiftTable[ i | 0x100 ] = 24; + + // small number (denorm) + + } else if ( e < - 14 ) { + + baseTable[ i ] = 0x0400 >> ( - e - 14 ); + baseTable[ i | 0x100 ] = ( 0x0400 >> ( - e - 14 ) ) | 0x8000; + shiftTable[ i ] = - e - 1; + shiftTable[ i | 0x100 ] = - e - 1; + + // normal number + + } else if ( e <= 15 ) { + + baseTable[ i ] = ( e + 15 ) << 10; + baseTable[ i | 0x100 ] = ( ( e + 15 ) << 10 ) | 0x8000; + shiftTable[ i ] = 13; + shiftTable[ i | 0x100 ] = 13; + + // large number (Infinity, -Infinity) + + } else if ( e < 128 ) { + + baseTable[ i ] = 0x7c00; + baseTable[ i | 0x100 ] = 0xfc00; + shiftTable[ i ] = 24; + shiftTable[ i | 0x100 ] = 24; + + // stay (NaN, Infinity, -Infinity) + + } else { + + baseTable[ i ] = 0x7c00; + baseTable[ i | 0x100 ] = 0xfc00; + shiftTable[ i ] = 13; + shiftTable[ i | 0x100 ] = 13; + + } + + } + + // float16 to float32 helpers + + const mantissaTable = new Uint32Array( 2048 ); + const exponentTable = new Uint32Array( 64 ); + const offsetTable = new Uint32Array( 64 ); + + for ( let i = 1; i < 1024; ++ i ) { + + let m = i << 13; // zero pad mantissa bits + let e = 0; // zero exponent + + // normalized + while ( ( m & 0x00800000 ) === 0 ) { + + m <<= 1; + e -= 0x00800000; // decrement exponent + + } + + m &= ~ 0x00800000; // clear leading 1 bit + e += 0x38800000; // adjust bias + + mantissaTable[ i ] = m | e; + + } + + for ( let i = 1024; i < 2048; ++ i ) { + + mantissaTable[ i ] = 0x38000000 + ( ( i - 1024 ) << 13 ); + + } + + for ( let i = 1; i < 31; ++ i ) { + + exponentTable[ i ] = i << 23; + + } + + exponentTable[ 31 ] = 0x47800000; + exponentTable[ 32 ] = 0x80000000; + + for ( let i = 33; i < 63; ++ i ) { + + exponentTable[ i ] = 0x80000000 + ( ( i - 32 ) << 23 ); + + } + + exponentTable[ 63 ] = 0xc7800000; + + for ( let i = 1; i < 64; ++ i ) { + + if ( i !== 32 ) { + + offsetTable[ i ] = 1024; + + } + + } + + return { + floatView: floatView, + uint32View: uint32View, + baseTable: baseTable, + shiftTable: shiftTable, + mantissaTable: mantissaTable, + exponentTable: exponentTable, + offsetTable: offsetTable + }; + +} + +// float32 to float16 + +function toHalfFloat( val ) { + + if ( Math.abs( val ) > 65504 ) console.warn( 'THREE.DataUtils.toHalfFloat(): Value out of range.' ); + + val = clamp( val, - 65504, 65504 ); + + _tables.floatView[ 0 ] = val; + const f = _tables.uint32View[ 0 ]; + const e = ( f >> 23 ) & 0x1ff; + return _tables.baseTable[ e ] + ( ( f & 0x007fffff ) >> _tables.shiftTable[ e ] ); + +} + +// float16 to float32 + +function fromHalfFloat( val ) { + + const m = val >> 10; + _tables.uint32View[ 0 ] = _tables.mantissaTable[ _tables.offsetTable[ m ] + ( val & 0x3ff ) ] + _tables.exponentTable[ m ]; + return _tables.floatView[ 0 ]; + +} + +const DataUtils = { + toHalfFloat: toHalfFloat, + fromHalfFloat: fromHalfFloat, +}; + +const _vector$9 = /*@__PURE__*/ new Vector3(); +const _vector2$1 = /*@__PURE__*/ new Vector2(); + +class BufferAttribute { + + constructor( array, itemSize, normalized = false ) { + + if ( Array.isArray( array ) ) { + + throw new TypeError( 'THREE.BufferAttribute: array should be a Typed Array.' ); + + } + + this.isBufferAttribute = true; + + this.name = ''; + + this.array = array; + this.itemSize = itemSize; + this.count = array !== undefined ? array.length / itemSize : 0; + this.normalized = normalized; + + this.usage = StaticDrawUsage; + this._updateRange = { offset: 0, count: - 1 }; + this.updateRanges = []; + this.gpuType = FloatType; + + this.version = 0; + + } + + onUploadCallback() {} + + set needsUpdate( value ) { + + if ( value === true ) this.version ++; + + } + + get updateRange() { + + warnOnce( 'THREE.BufferAttribute: updateRange() is deprecated and will be removed in r169. Use addUpdateRange() instead.' ); // @deprecated, r159 + return this._updateRange; + + } + + setUsage( value ) { + + this.usage = value; + + return this; + + } + + addUpdateRange( start, count ) { + + this.updateRanges.push( { start, count } ); + + } + + clearUpdateRanges() { + + this.updateRanges.length = 0; + + } + + copy( source ) { + + this.name = source.name; + this.array = new source.array.constructor( source.array ); + this.itemSize = source.itemSize; + this.count = source.count; + this.normalized = source.normalized; + + this.usage = source.usage; + this.gpuType = source.gpuType; + + return this; + + } + + copyAt( index1, attribute, index2 ) { + + index1 *= this.itemSize; + index2 *= attribute.itemSize; + + for ( let i = 0, l = this.itemSize; i < l; i ++ ) { + + this.array[ index1 + i ] = attribute.array[ index2 + i ]; + + } + + return this; + + } + + copyArray( array ) { + + this.array.set( array ); + + return this; + + } + + applyMatrix3( m ) { + + if ( this.itemSize === 2 ) { + + for ( let i = 0, l = this.count; i < l; i ++ ) { + + _vector2$1.fromBufferAttribute( this, i ); + _vector2$1.applyMatrix3( m ); + + this.setXY( i, _vector2$1.x, _vector2$1.y ); + + } + + } else if ( this.itemSize === 3 ) { + + for ( let i = 0, l = this.count; i < l; i ++ ) { + + _vector$9.fromBufferAttribute( this, i ); + _vector$9.applyMatrix3( m ); + + this.setXYZ( i, _vector$9.x, _vector$9.y, _vector$9.z ); + + } + + } + + return this; + + } + + applyMatrix4( m ) { + + for ( let i = 0, l = this.count; i < l; i ++ ) { + + _vector$9.fromBufferAttribute( this, i ); + + _vector$9.applyMatrix4( m ); + + this.setXYZ( i, _vector$9.x, _vector$9.y, _vector$9.z ); + + } + + return this; + + } + + applyNormalMatrix( m ) { + + for ( let i = 0, l = this.count; i < l; i ++ ) { + + _vector$9.fromBufferAttribute( this, i ); + + _vector$9.applyNormalMatrix( m ); + + this.setXYZ( i, _vector$9.x, _vector$9.y, _vector$9.z ); + + } + + return this; + + } + + transformDirection( m ) { + + for ( let i = 0, l = this.count; i < l; i ++ ) { + + _vector$9.fromBufferAttribute( this, i ); + + _vector$9.transformDirection( m ); + + this.setXYZ( i, _vector$9.x, _vector$9.y, _vector$9.z ); + + } + + return this; + + } + + set( value, offset = 0 ) { + + // Matching BufferAttribute constructor, do not normalize the array. + this.array.set( value, offset ); + + return this; + + } + + getComponent( index, component ) { + + let value = this.array[ index * this.itemSize + component ]; + + if ( this.normalized ) value = denormalize( value, this.array ); + + return value; + + } + + setComponent( index, component, value ) { + + if ( this.normalized ) value = normalize( value, this.array ); + + this.array[ index * this.itemSize + component ] = value; + + return this; + + } + + getX( index ) { + + let x = this.array[ index * this.itemSize ]; + + if ( this.normalized ) x = denormalize( x, this.array ); + + return x; + + } + + setX( index, x ) { + + if ( this.normalized ) x = normalize( x, this.array ); + + this.array[ index * this.itemSize ] = x; + + return this; + + } + + getY( index ) { + + let y = this.array[ index * this.itemSize + 1 ]; + + if ( this.normalized ) y = denormalize( y, this.array ); + + return y; + + } + + setY( index, y ) { + + if ( this.normalized ) y = normalize( y, this.array ); + + this.array[ index * this.itemSize + 1 ] = y; + + return this; + + } + + getZ( index ) { + + let z = this.array[ index * this.itemSize + 2 ]; + + if ( this.normalized ) z = denormalize( z, this.array ); + + return z; + + } + + setZ( index, z ) { + + if ( this.normalized ) z = normalize( z, this.array ); + + this.array[ index * this.itemSize + 2 ] = z; + + return this; + + } + + getW( index ) { + + let w = this.array[ index * this.itemSize + 3 ]; + + if ( this.normalized ) w = denormalize( w, this.array ); + + return w; + + } + + setW( index, w ) { + + if ( this.normalized ) w = normalize( w, this.array ); + + this.array[ index * this.itemSize + 3 ] = w; + + return this; + + } + + setXY( index, x, y ) { + + index *= this.itemSize; + + if ( this.normalized ) { + + x = normalize( x, this.array ); + y = normalize( y, this.array ); + + } + + this.array[ index + 0 ] = x; + this.array[ index + 1 ] = y; + + return this; + + } + + setXYZ( index, x, y, z ) { + + index *= this.itemSize; + + if ( this.normalized ) { + + x = normalize( x, this.array ); + y = normalize( y, this.array ); + z = normalize( z, this.array ); + + } + + this.array[ index + 0 ] = x; + this.array[ index + 1 ] = y; + this.array[ index + 2 ] = z; + + return this; + + } + + setXYZW( index, x, y, z, w ) { + + index *= this.itemSize; + + if ( this.normalized ) { + + x = normalize( x, this.array ); + y = normalize( y, this.array ); + z = normalize( z, this.array ); + w = normalize( w, this.array ); + + } + + this.array[ index + 0 ] = x; + this.array[ index + 1 ] = y; + this.array[ index + 2 ] = z; + this.array[ index + 3 ] = w; + + return this; + + } + + onUpload( callback ) { + + this.onUploadCallback = callback; + + return this; + + } + + clone() { + + return new this.constructor( this.array, this.itemSize ).copy( this ); + + } + + toJSON() { + + const data = { + itemSize: this.itemSize, + type: this.array.constructor.name, + array: Array.from( this.array ), + normalized: this.normalized + }; + + if ( this.name !== '' ) data.name = this.name; + if ( this.usage !== StaticDrawUsage ) data.usage = this.usage; + + return data; + + } + +} + +// + +class Int8BufferAttribute extends BufferAttribute { + + constructor( array, itemSize, normalized ) { + + super( new Int8Array( array ), itemSize, normalized ); + + } + +} + +class Uint8BufferAttribute extends BufferAttribute { + + constructor( array, itemSize, normalized ) { + + super( new Uint8Array( array ), itemSize, normalized ); + + } + +} + +class Uint8ClampedBufferAttribute extends BufferAttribute { + + constructor( array, itemSize, normalized ) { + + super( new Uint8ClampedArray( array ), itemSize, normalized ); + + } + +} + +class Int16BufferAttribute extends BufferAttribute { + + constructor( array, itemSize, normalized ) { + + super( new Int16Array( array ), itemSize, normalized ); + + } + +} + +class Uint16BufferAttribute extends BufferAttribute { + + constructor( array, itemSize, normalized ) { + + super( new Uint16Array( array ), itemSize, normalized ); + + } + +} + +class Int32BufferAttribute extends BufferAttribute { + + constructor( array, itemSize, normalized ) { + + super( new Int32Array( array ), itemSize, normalized ); + + } + +} + +class Uint32BufferAttribute extends BufferAttribute { + + constructor( array, itemSize, normalized ) { + + super( new Uint32Array( array ), itemSize, normalized ); + + } + +} + +class Float16BufferAttribute extends BufferAttribute { + + constructor( array, itemSize, normalized ) { + + super( new Uint16Array( array ), itemSize, normalized ); + + this.isFloat16BufferAttribute = true; + + } + + getX( index ) { + + let x = fromHalfFloat( this.array[ index * this.itemSize ] ); + + if ( this.normalized ) x = denormalize( x, this.array ); + + return x; + + } + + setX( index, x ) { + + if ( this.normalized ) x = normalize( x, this.array ); + + this.array[ index * this.itemSize ] = toHalfFloat( x ); + + return this; + + } + + getY( index ) { + + let y = fromHalfFloat( this.array[ index * this.itemSize + 1 ] ); + + if ( this.normalized ) y = denormalize( y, this.array ); + + return y; + + } + + setY( index, y ) { + + if ( this.normalized ) y = normalize( y, this.array ); + + this.array[ index * this.itemSize + 1 ] = toHalfFloat( y ); + + return this; + + } + + getZ( index ) { + + let z = fromHalfFloat( this.array[ index * this.itemSize + 2 ] ); + + if ( this.normalized ) z = denormalize( z, this.array ); + + return z; + + } + + setZ( index, z ) { + + if ( this.normalized ) z = normalize( z, this.array ); + + this.array[ index * this.itemSize + 2 ] = toHalfFloat( z ); + + return this; + + } + + getW( index ) { + + let w = fromHalfFloat( this.array[ index * this.itemSize + 3 ] ); + + if ( this.normalized ) w = denormalize( w, this.array ); + + return w; + + } + + setW( index, w ) { + + if ( this.normalized ) w = normalize( w, this.array ); + + this.array[ index * this.itemSize + 3 ] = toHalfFloat( w ); + + return this; + + } + + setXY( index, x, y ) { + + index *= this.itemSize; + + if ( this.normalized ) { + + x = normalize( x, this.array ); + y = normalize( y, this.array ); + + } + + this.array[ index + 0 ] = toHalfFloat( x ); + this.array[ index + 1 ] = toHalfFloat( y ); + + return this; + + } + + setXYZ( index, x, y, z ) { + + index *= this.itemSize; + + if ( this.normalized ) { + + x = normalize( x, this.array ); + y = normalize( y, this.array ); + z = normalize( z, this.array ); + + } + + this.array[ index + 0 ] = toHalfFloat( x ); + this.array[ index + 1 ] = toHalfFloat( y ); + this.array[ index + 2 ] = toHalfFloat( z ); + + return this; + + } + + setXYZW( index, x, y, z, w ) { + + index *= this.itemSize; + + if ( this.normalized ) { + + x = normalize( x, this.array ); + y = normalize( y, this.array ); + z = normalize( z, this.array ); + w = normalize( w, this.array ); + + } + + this.array[ index + 0 ] = toHalfFloat( x ); + this.array[ index + 1 ] = toHalfFloat( y ); + this.array[ index + 2 ] = toHalfFloat( z ); + this.array[ index + 3 ] = toHalfFloat( w ); + + return this; + + } + +} + + +class Float32BufferAttribute extends BufferAttribute { + + constructor( array, itemSize, normalized ) { + + super( new Float32Array( array ), itemSize, normalized ); + + } + +} + +let _id$2 = 0; + +const _m1$2 = /*@__PURE__*/ new Matrix4(); +const _obj = /*@__PURE__*/ new Object3D(); +const _offset = /*@__PURE__*/ new Vector3(); +const _box$2 = /*@__PURE__*/ new Box3(); +const _boxMorphTargets = /*@__PURE__*/ new Box3(); +const _vector$8 = /*@__PURE__*/ new Vector3(); + +class BufferGeometry extends EventDispatcher { + + constructor() { + + super(); + + this.isBufferGeometry = true; + + Object.defineProperty( this, 'id', { value: _id$2 ++ } ); + + this.uuid = generateUUID(); + + this.name = ''; + this.type = 'BufferGeometry'; + + this.index = null; + this.attributes = {}; + + this.morphAttributes = {}; + this.morphTargetsRelative = false; + + this.groups = []; + + this.boundingBox = null; + this.boundingSphere = null; + + this.drawRange = { start: 0, count: Infinity }; + + this.userData = {}; + + } + + getIndex() { + + return this.index; + + } + + setIndex( index ) { + + if ( Array.isArray( index ) ) { + + this.index = new ( arrayNeedsUint32( index ) ? Uint32BufferAttribute : Uint16BufferAttribute )( index, 1 ); + + } else { + + this.index = index; + + } + + return this; + + } + + getAttribute( name ) { + + return this.attributes[ name ]; + + } + + setAttribute( name, attribute ) { + + this.attributes[ name ] = attribute; + + return this; + + } + + deleteAttribute( name ) { + + delete this.attributes[ name ]; + + return this; + + } + + hasAttribute( name ) { + + return this.attributes[ name ] !== undefined; + + } + + addGroup( start, count, materialIndex = 0 ) { + + this.groups.push( { + + start: start, + count: count, + materialIndex: materialIndex + + } ); + + } + + clearGroups() { + + this.groups = []; + + } + + setDrawRange( start, count ) { + + this.drawRange.start = start; + this.drawRange.count = count; + + } + + applyMatrix4( matrix ) { + + const position = this.attributes.position; + + if ( position !== undefined ) { + + position.applyMatrix4( matrix ); + + position.needsUpdate = true; + + } + + const normal = this.attributes.normal; + + if ( normal !== undefined ) { + + const normalMatrix = new Matrix3().getNormalMatrix( matrix ); + + normal.applyNormalMatrix( normalMatrix ); + + normal.needsUpdate = true; + + } + + const tangent = this.attributes.tangent; + + if ( tangent !== undefined ) { + + tangent.transformDirection( matrix ); + + tangent.needsUpdate = true; + + } + + if ( this.boundingBox !== null ) { + + this.computeBoundingBox(); + + } + + if ( this.boundingSphere !== null ) { + + this.computeBoundingSphere(); + + } + + return this; + + } + + applyQuaternion( q ) { + + _m1$2.makeRotationFromQuaternion( q ); + + this.applyMatrix4( _m1$2 ); + + return this; + + } + + rotateX( angle ) { + + // rotate geometry around world x-axis + + _m1$2.makeRotationX( angle ); + + this.applyMatrix4( _m1$2 ); + + return this; + + } + + rotateY( angle ) { + + // rotate geometry around world y-axis + + _m1$2.makeRotationY( angle ); + + this.applyMatrix4( _m1$2 ); + + return this; + + } + + rotateZ( angle ) { + + // rotate geometry around world z-axis + + _m1$2.makeRotationZ( angle ); + + this.applyMatrix4( _m1$2 ); + + return this; + + } + + translate( x, y, z ) { + + // translate geometry + + _m1$2.makeTranslation( x, y, z ); + + this.applyMatrix4( _m1$2 ); + + return this; + + } + + scale( x, y, z ) { + + // scale geometry + + _m1$2.makeScale( x, y, z ); + + this.applyMatrix4( _m1$2 ); + + return this; + + } + + lookAt( vector ) { + + _obj.lookAt( vector ); + + _obj.updateMatrix(); + + this.applyMatrix4( _obj.matrix ); + + return this; + + } + + center() { + + this.computeBoundingBox(); + + this.boundingBox.getCenter( _offset ).negate(); + + this.translate( _offset.x, _offset.y, _offset.z ); + + return this; + + } + + setFromPoints( points ) { + + const position = []; + + for ( let i = 0, l = points.length; i < l; i ++ ) { + + const point = points[ i ]; + position.push( point.x, point.y, point.z || 0 ); + + } + + this.setAttribute( 'position', new Float32BufferAttribute( position, 3 ) ); + + return this; + + } + + computeBoundingBox() { + + if ( this.boundingBox === null ) { + + this.boundingBox = new Box3(); + + } + + const position = this.attributes.position; + const morphAttributesPosition = this.morphAttributes.position; + + if ( position && position.isGLBufferAttribute ) { + + console.error( 'THREE.BufferGeometry.computeBoundingBox(): GLBufferAttribute requires a manual bounding box.', this ); + + this.boundingBox.set( + new Vector3( - Infinity, - Infinity, - Infinity ), + new Vector3( + Infinity, + Infinity, + Infinity ) + ); + + return; + + } + + if ( position !== undefined ) { + + this.boundingBox.setFromBufferAttribute( position ); + + // process morph attributes if present + + if ( morphAttributesPosition ) { + + for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) { + + const morphAttribute = morphAttributesPosition[ i ]; + _box$2.setFromBufferAttribute( morphAttribute ); + + if ( this.morphTargetsRelative ) { + + _vector$8.addVectors( this.boundingBox.min, _box$2.min ); + this.boundingBox.expandByPoint( _vector$8 ); + + _vector$8.addVectors( this.boundingBox.max, _box$2.max ); + this.boundingBox.expandByPoint( _vector$8 ); + + } else { + + this.boundingBox.expandByPoint( _box$2.min ); + this.boundingBox.expandByPoint( _box$2.max ); + + } + + } + + } + + } else { + + this.boundingBox.makeEmpty(); + + } + + if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) { + + console.error( 'THREE.BufferGeometry.computeBoundingBox(): Computed min/max have NaN values. The "position" attribute is likely to have NaN values.', this ); + + } + + } + + computeBoundingSphere() { + + if ( this.boundingSphere === null ) { + + this.boundingSphere = new Sphere(); + + } + + const position = this.attributes.position; + const morphAttributesPosition = this.morphAttributes.position; + + if ( position && position.isGLBufferAttribute ) { + + console.error( 'THREE.BufferGeometry.computeBoundingSphere(): GLBufferAttribute requires a manual bounding sphere.', this ); + + this.boundingSphere.set( new Vector3(), Infinity ); + + return; + + } + + if ( position ) { + + // first, find the center of the bounding sphere + + const center = this.boundingSphere.center; + + _box$2.setFromBufferAttribute( position ); + + // process morph attributes if present + + if ( morphAttributesPosition ) { + + for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) { + + const morphAttribute = morphAttributesPosition[ i ]; + _boxMorphTargets.setFromBufferAttribute( morphAttribute ); + + if ( this.morphTargetsRelative ) { + + _vector$8.addVectors( _box$2.min, _boxMorphTargets.min ); + _box$2.expandByPoint( _vector$8 ); + + _vector$8.addVectors( _box$2.max, _boxMorphTargets.max ); + _box$2.expandByPoint( _vector$8 ); + + } else { + + _box$2.expandByPoint( _boxMorphTargets.min ); + _box$2.expandByPoint( _boxMorphTargets.max ); + + } + + } + + } + + _box$2.getCenter( center ); + + // second, try to find a boundingSphere with a radius smaller than the + // boundingSphere of the boundingBox: sqrt(3) smaller in the best case + + let maxRadiusSq = 0; + + for ( let i = 0, il = position.count; i < il; i ++ ) { + + _vector$8.fromBufferAttribute( position, i ); + + maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector$8 ) ); + + } + + // process morph attributes if present + + if ( morphAttributesPosition ) { + + for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) { + + const morphAttribute = morphAttributesPosition[ i ]; + const morphTargetsRelative = this.morphTargetsRelative; + + for ( let j = 0, jl = morphAttribute.count; j < jl; j ++ ) { + + _vector$8.fromBufferAttribute( morphAttribute, j ); + + if ( morphTargetsRelative ) { + + _offset.fromBufferAttribute( position, j ); + _vector$8.add( _offset ); + + } + + maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector$8 ) ); + + } + + } + + } + + this.boundingSphere.radius = Math.sqrt( maxRadiusSq ); + + if ( isNaN( this.boundingSphere.radius ) ) { + + console.error( 'THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.', this ); + + } + + } + + } + + computeTangents() { + + const index = this.index; + const attributes = this.attributes; + + // based on http://www.terathon.com/code/tangent.html + // (per vertex tangents) + + if ( index === null || + attributes.position === undefined || + attributes.normal === undefined || + attributes.uv === undefined ) { + + console.error( 'THREE.BufferGeometry: .computeTangents() failed. Missing required attributes (index, position, normal or uv)' ); + return; + + } + + const positionAttribute = attributes.position; + const normalAttribute = attributes.normal; + const uvAttribute = attributes.uv; + + if ( this.hasAttribute( 'tangent' ) === false ) { + + this.setAttribute( 'tangent', new BufferAttribute( new Float32Array( 4 * positionAttribute.count ), 4 ) ); + + } + + const tangentAttribute = this.getAttribute( 'tangent' ); + + const tan1 = [], tan2 = []; + + for ( let i = 0; i < positionAttribute.count; i ++ ) { + + tan1[ i ] = new Vector3(); + tan2[ i ] = new Vector3(); + + } + + const vA = new Vector3(), + vB = new Vector3(), + vC = new Vector3(), + + uvA = new Vector2(), + uvB = new Vector2(), + uvC = new Vector2(), + + sdir = new Vector3(), + tdir = new Vector3(); + + function handleTriangle( a, b, c ) { + + vA.fromBufferAttribute( positionAttribute, a ); + vB.fromBufferAttribute( positionAttribute, b ); + vC.fromBufferAttribute( positionAttribute, c ); + + uvA.fromBufferAttribute( uvAttribute, a ); + uvB.fromBufferAttribute( uvAttribute, b ); + uvC.fromBufferAttribute( uvAttribute, c ); + + vB.sub( vA ); + vC.sub( vA ); + + uvB.sub( uvA ); + uvC.sub( uvA ); + + const r = 1.0 / ( uvB.x * uvC.y - uvC.x * uvB.y ); + + // silently ignore degenerate uv triangles having coincident or colinear vertices + + if ( ! isFinite( r ) ) return; + + sdir.copy( vB ).multiplyScalar( uvC.y ).addScaledVector( vC, - uvB.y ).multiplyScalar( r ); + tdir.copy( vC ).multiplyScalar( uvB.x ).addScaledVector( vB, - uvC.x ).multiplyScalar( r ); + + tan1[ a ].add( sdir ); + tan1[ b ].add( sdir ); + tan1[ c ].add( sdir ); + + tan2[ a ].add( tdir ); + tan2[ b ].add( tdir ); + tan2[ c ].add( tdir ); + + } + + let groups = this.groups; + + if ( groups.length === 0 ) { + + groups = [ { + start: 0, + count: index.count + } ]; + + } + + for ( let i = 0, il = groups.length; i < il; ++ i ) { + + const group = groups[ i ]; + + const start = group.start; + const count = group.count; + + for ( let j = start, jl = start + count; j < jl; j += 3 ) { + + handleTriangle( + index.getX( j + 0 ), + index.getX( j + 1 ), + index.getX( j + 2 ) + ); + + } + + } + + const tmp = new Vector3(), tmp2 = new Vector3(); + const n = new Vector3(), n2 = new Vector3(); + + function handleVertex( v ) { + + n.fromBufferAttribute( normalAttribute, v ); + n2.copy( n ); + + const t = tan1[ v ]; + + // Gram-Schmidt orthogonalize + + tmp.copy( t ); + tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize(); + + // Calculate handedness + + tmp2.crossVectors( n2, t ); + const test = tmp2.dot( tan2[ v ] ); + const w = ( test < 0.0 ) ? - 1.0 : 1.0; + + tangentAttribute.setXYZW( v, tmp.x, tmp.y, tmp.z, w ); + + } + + for ( let i = 0, il = groups.length; i < il; ++ i ) { + + const group = groups[ i ]; + + const start = group.start; + const count = group.count; + + for ( let j = start, jl = start + count; j < jl; j += 3 ) { + + handleVertex( index.getX( j + 0 ) ); + handleVertex( index.getX( j + 1 ) ); + handleVertex( index.getX( j + 2 ) ); + + } + + } + + } + + computeVertexNormals() { + + const index = this.index; + const positionAttribute = this.getAttribute( 'position' ); + + if ( positionAttribute !== undefined ) { + + let normalAttribute = this.getAttribute( 'normal' ); + + if ( normalAttribute === undefined ) { + + normalAttribute = new BufferAttribute( new Float32Array( positionAttribute.count * 3 ), 3 ); + this.setAttribute( 'normal', normalAttribute ); + + } else { + + // reset existing normals to zero + + for ( let i = 0, il = normalAttribute.count; i < il; i ++ ) { + + normalAttribute.setXYZ( i, 0, 0, 0 ); + + } + + } + + const pA = new Vector3(), pB = new Vector3(), pC = new Vector3(); + const nA = new Vector3(), nB = new Vector3(), nC = new Vector3(); + const cb = new Vector3(), ab = new Vector3(); + + // indexed elements + + if ( index ) { + + for ( let i = 0, il = index.count; i < il; i += 3 ) { + + const vA = index.getX( i + 0 ); + const vB = index.getX( i + 1 ); + const vC = index.getX( i + 2 ); + + pA.fromBufferAttribute( positionAttribute, vA ); + pB.fromBufferAttribute( positionAttribute, vB ); + pC.fromBufferAttribute( positionAttribute, vC ); + + cb.subVectors( pC, pB ); + ab.subVectors( pA, pB ); + cb.cross( ab ); + + nA.fromBufferAttribute( normalAttribute, vA ); + nB.fromBufferAttribute( normalAttribute, vB ); + nC.fromBufferAttribute( normalAttribute, vC ); + + nA.add( cb ); + nB.add( cb ); + nC.add( cb ); + + normalAttribute.setXYZ( vA, nA.x, nA.y, nA.z ); + normalAttribute.setXYZ( vB, nB.x, nB.y, nB.z ); + normalAttribute.setXYZ( vC, nC.x, nC.y, nC.z ); + + } + + } else { + + // non-indexed elements (unconnected triangle soup) + + for ( let i = 0, il = positionAttribute.count; i < il; i += 3 ) { + + pA.fromBufferAttribute( positionAttribute, i + 0 ); + pB.fromBufferAttribute( positionAttribute, i + 1 ); + pC.fromBufferAttribute( positionAttribute, i + 2 ); + + cb.subVectors( pC, pB ); + ab.subVectors( pA, pB ); + cb.cross( ab ); + + normalAttribute.setXYZ( i + 0, cb.x, cb.y, cb.z ); + normalAttribute.setXYZ( i + 1, cb.x, cb.y, cb.z ); + normalAttribute.setXYZ( i + 2, cb.x, cb.y, cb.z ); + + } + + } + + this.normalizeNormals(); + + normalAttribute.needsUpdate = true; + + } + + } + + normalizeNormals() { + + const normals = this.attributes.normal; + + for ( let i = 0, il = normals.count; i < il; i ++ ) { + + _vector$8.fromBufferAttribute( normals, i ); + + _vector$8.normalize(); + + normals.setXYZ( i, _vector$8.x, _vector$8.y, _vector$8.z ); + + } + + } + + toNonIndexed() { + + function convertBufferAttribute( attribute, indices ) { + + const array = attribute.array; + const itemSize = attribute.itemSize; + const normalized = attribute.normalized; + + const array2 = new array.constructor( indices.length * itemSize ); + + let index = 0, index2 = 0; + + for ( let i = 0, l = indices.length; i < l; i ++ ) { + + if ( attribute.isInterleavedBufferAttribute ) { + + index = indices[ i ] * attribute.data.stride + attribute.offset; + + } else { + + index = indices[ i ] * itemSize; + + } + + for ( let j = 0; j < itemSize; j ++ ) { + + array2[ index2 ++ ] = array[ index ++ ]; + + } + + } + + return new BufferAttribute( array2, itemSize, normalized ); + + } + + // + + if ( this.index === null ) { + + console.warn( 'THREE.BufferGeometry.toNonIndexed(): BufferGeometry is already non-indexed.' ); + return this; + + } + + const geometry2 = new BufferGeometry(); + + const indices = this.index.array; + const attributes = this.attributes; + + // attributes + + for ( const name in attributes ) { + + const attribute = attributes[ name ]; + + const newAttribute = convertBufferAttribute( attribute, indices ); + + geometry2.setAttribute( name, newAttribute ); + + } + + // morph attributes + + const morphAttributes = this.morphAttributes; + + for ( const name in morphAttributes ) { + + const morphArray = []; + const morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes + + for ( let i = 0, il = morphAttribute.length; i < il; i ++ ) { + + const attribute = morphAttribute[ i ]; + + const newAttribute = convertBufferAttribute( attribute, indices ); + + morphArray.push( newAttribute ); + + } + + geometry2.morphAttributes[ name ] = morphArray; + + } + + geometry2.morphTargetsRelative = this.morphTargetsRelative; + + // groups + + const groups = this.groups; + + for ( let i = 0, l = groups.length; i < l; i ++ ) { + + const group = groups[ i ]; + geometry2.addGroup( group.start, group.count, group.materialIndex ); + + } + + return geometry2; + + } + + toJSON() { + + const data = { + metadata: { + version: 4.6, + type: 'BufferGeometry', + generator: 'BufferGeometry.toJSON' + } + }; + + // standard BufferGeometry serialization + + data.uuid = this.uuid; + data.type = this.type; + if ( this.name !== '' ) data.name = this.name; + if ( Object.keys( this.userData ).length > 0 ) data.userData = this.userData; + + if ( this.parameters !== undefined ) { + + const parameters = this.parameters; + + for ( const key in parameters ) { + + if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ]; + + } + + return data; + + } + + // for simplicity the code assumes attributes are not shared across geometries, see #15811 + + data.data = { attributes: {} }; + + const index = this.index; + + if ( index !== null ) { + + data.data.index = { + type: index.array.constructor.name, + array: Array.prototype.slice.call( index.array ) + }; + + } + + const attributes = this.attributes; + + for ( const key in attributes ) { + + const attribute = attributes[ key ]; + + data.data.attributes[ key ] = attribute.toJSON( data.data ); + + } + + const morphAttributes = {}; + let hasMorphAttributes = false; + + for ( const key in this.morphAttributes ) { + + const attributeArray = this.morphAttributes[ key ]; + + const array = []; + + for ( let i = 0, il = attributeArray.length; i < il; i ++ ) { + + const attribute = attributeArray[ i ]; + + array.push( attribute.toJSON( data.data ) ); + + } + + if ( array.length > 0 ) { + + morphAttributes[ key ] = array; + + hasMorphAttributes = true; + + } + + } + + if ( hasMorphAttributes ) { + + data.data.morphAttributes = morphAttributes; + data.data.morphTargetsRelative = this.morphTargetsRelative; + + } + + const groups = this.groups; + + if ( groups.length > 0 ) { + + data.data.groups = JSON.parse( JSON.stringify( groups ) ); + + } + + const boundingSphere = this.boundingSphere; + + if ( boundingSphere !== null ) { + + data.data.boundingSphere = { + center: boundingSphere.center.toArray(), + radius: boundingSphere.radius + }; + + } + + return data; + + } + + clone() { + + return new this.constructor().copy( this ); + + } + + copy( source ) { + + // reset + + this.index = null; + this.attributes = {}; + this.morphAttributes = {}; + this.groups = []; + this.boundingBox = null; + this.boundingSphere = null; + + // used for storing cloned, shared data + + const data = {}; + + // name + + this.name = source.name; + + // index + + const index = source.index; + + if ( index !== null ) { + + this.setIndex( index.clone( data ) ); + + } + + // attributes + + const attributes = source.attributes; + + for ( const name in attributes ) { + + const attribute = attributes[ name ]; + this.setAttribute( name, attribute.clone( data ) ); + + } + + // morph attributes + + const morphAttributes = source.morphAttributes; + + for ( const name in morphAttributes ) { + + const array = []; + const morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes + + for ( let i = 0, l = morphAttribute.length; i < l; i ++ ) { + + array.push( morphAttribute[ i ].clone( data ) ); + + } + + this.morphAttributes[ name ] = array; + + } + + this.morphTargetsRelative = source.morphTargetsRelative; + + // groups + + const groups = source.groups; + + for ( let i = 0, l = groups.length; i < l; i ++ ) { + + const group = groups[ i ]; + this.addGroup( group.start, group.count, group.materialIndex ); + + } + + // bounding box + + const boundingBox = source.boundingBox; + + if ( boundingBox !== null ) { + + this.boundingBox = boundingBox.clone(); + + } + + // bounding sphere + + const boundingSphere = source.boundingSphere; + + if ( boundingSphere !== null ) { + + this.boundingSphere = boundingSphere.clone(); + + } + + // draw range + + this.drawRange.start = source.drawRange.start; + this.drawRange.count = source.drawRange.count; + + // user data + + this.userData = source.userData; + + return this; + + } + + dispose() { + + this.dispatchEvent( { type: 'dispose' } ); + + } + +} + +const _inverseMatrix$3 = /*@__PURE__*/ new Matrix4(); +const _ray$3 = /*@__PURE__*/ new Ray(); +const _sphere$6 = /*@__PURE__*/ new Sphere(); +const _sphereHitAt = /*@__PURE__*/ new Vector3(); + +const _vA$1 = /*@__PURE__*/ new Vector3(); +const _vB$1 = /*@__PURE__*/ new Vector3(); +const _vC$1 = /*@__PURE__*/ new Vector3(); + +const _tempA = /*@__PURE__*/ new Vector3(); +const _morphA = /*@__PURE__*/ new Vector3(); + +const _uvA$1 = /*@__PURE__*/ new Vector2(); +const _uvB$1 = /*@__PURE__*/ new Vector2(); +const _uvC$1 = /*@__PURE__*/ new Vector2(); + +const _normalA = /*@__PURE__*/ new Vector3(); +const _normalB = /*@__PURE__*/ new Vector3(); +const _normalC = /*@__PURE__*/ new Vector3(); + +const _intersectionPoint = /*@__PURE__*/ new Vector3(); +const _intersectionPointWorld = /*@__PURE__*/ new Vector3(); + +class Mesh extends Object3D { + + constructor( geometry = new BufferGeometry(), material = new MeshBasicMaterial() ) { + + super(); + + this.isMesh = true; + + this.type = 'Mesh'; + + this.geometry = geometry; + this.material = material; + + this.updateMorphTargets(); + + } + + copy( source, recursive ) { + + super.copy( source, recursive ); + + if ( source.morphTargetInfluences !== undefined ) { + + this.morphTargetInfluences = source.morphTargetInfluences.slice(); + + } + + if ( source.morphTargetDictionary !== undefined ) { + + this.morphTargetDictionary = Object.assign( {}, source.morphTargetDictionary ); + + } + + this.material = Array.isArray( source.material ) ? source.material.slice() : source.material; + this.geometry = source.geometry; + + return this; + + } + + updateMorphTargets() { + + const geometry = this.geometry; + + const morphAttributes = geometry.morphAttributes; + const keys = Object.keys( morphAttributes ); + + if ( keys.length > 0 ) { + + const morphAttribute = morphAttributes[ keys[ 0 ] ]; + + if ( morphAttribute !== undefined ) { + + this.morphTargetInfluences = []; + this.morphTargetDictionary = {}; + + for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) { + + const name = morphAttribute[ m ].name || String( m ); + + this.morphTargetInfluences.push( 0 ); + this.morphTargetDictionary[ name ] = m; + + } + + } + + } + + } + + getVertexPosition( index, target ) { + + const geometry = this.geometry; + const position = geometry.attributes.position; + const morphPosition = geometry.morphAttributes.position; + const morphTargetsRelative = geometry.morphTargetsRelative; + + target.fromBufferAttribute( position, index ); + + const morphInfluences = this.morphTargetInfluences; + + if ( morphPosition && morphInfluences ) { + + _morphA.set( 0, 0, 0 ); + + for ( let i = 0, il = morphPosition.length; i < il; i ++ ) { + + const influence = morphInfluences[ i ]; + const morphAttribute = morphPosition[ i ]; + + if ( influence === 0 ) continue; + + _tempA.fromBufferAttribute( morphAttribute, index ); + + if ( morphTargetsRelative ) { + + _morphA.addScaledVector( _tempA, influence ); + + } else { + + _morphA.addScaledVector( _tempA.sub( target ), influence ); + + } + + } + + target.add( _morphA ); + + } + + return target; + + } + + raycast( raycaster, intersects ) { + + const geometry = this.geometry; + const material = this.material; + const matrixWorld = this.matrixWorld; + + if ( material === undefined ) return; + + // test with bounding sphere in world space + + if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere(); + + _sphere$6.copy( geometry.boundingSphere ); + _sphere$6.applyMatrix4( matrixWorld ); + + // check distance from ray origin to bounding sphere + + _ray$3.copy( raycaster.ray ).recast( raycaster.near ); + + if ( _sphere$6.containsPoint( _ray$3.origin ) === false ) { + + if ( _ray$3.intersectSphere( _sphere$6, _sphereHitAt ) === null ) return; + + if ( _ray$3.origin.distanceToSquared( _sphereHitAt ) > ( raycaster.far - raycaster.near ) ** 2 ) return; + + } + + // convert ray to local space of mesh + + _inverseMatrix$3.copy( matrixWorld ).invert(); + _ray$3.copy( raycaster.ray ).applyMatrix4( _inverseMatrix$3 ); + + // test with bounding box in local space + + if ( geometry.boundingBox !== null ) { + + if ( _ray$3.intersectsBox( geometry.boundingBox ) === false ) return; + + } + + // test for intersections with geometry + + this._computeIntersections( raycaster, intersects, _ray$3 ); + + } + + _computeIntersections( raycaster, intersects, rayLocalSpace ) { + + let intersection; + + const geometry = this.geometry; + const material = this.material; + + const index = geometry.index; + const position = geometry.attributes.position; + const uv = geometry.attributes.uv; + const uv1 = geometry.attributes.uv1; + const normal = geometry.attributes.normal; + const groups = geometry.groups; + const drawRange = geometry.drawRange; + + if ( index !== null ) { + + // indexed buffer geometry + + if ( Array.isArray( material ) ) { + + for ( let i = 0, il = groups.length; i < il; i ++ ) { + + const group = groups[ i ]; + const groupMaterial = material[ group.materialIndex ]; + + const start = Math.max( group.start, drawRange.start ); + const end = Math.min( index.count, Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) ) ); + + for ( let j = start, jl = end; j < jl; j += 3 ) { + + const a = index.getX( j ); + const b = index.getX( j + 1 ); + const c = index.getX( j + 2 ); + + intersection = checkGeometryIntersection( this, groupMaterial, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c ); + + if ( intersection ) { + + intersection.faceIndex = Math.floor( j / 3 ); // triangle number in indexed buffer semantics + intersection.face.materialIndex = group.materialIndex; + intersects.push( intersection ); + + } + + } + + } + + } else { + + const start = Math.max( 0, drawRange.start ); + const end = Math.min( index.count, ( drawRange.start + drawRange.count ) ); + + for ( let i = start, il = end; i < il; i += 3 ) { + + const a = index.getX( i ); + const b = index.getX( i + 1 ); + const c = index.getX( i + 2 ); + + intersection = checkGeometryIntersection( this, material, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c ); + + if ( intersection ) { + + intersection.faceIndex = Math.floor( i / 3 ); // triangle number in indexed buffer semantics + intersects.push( intersection ); + + } + + } + + } + + } else if ( position !== undefined ) { + + // non-indexed buffer geometry + + if ( Array.isArray( material ) ) { + + for ( let i = 0, il = groups.length; i < il; i ++ ) { + + const group = groups[ i ]; + const groupMaterial = material[ group.materialIndex ]; + + const start = Math.max( group.start, drawRange.start ); + const end = Math.min( position.count, Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) ) ); + + for ( let j = start, jl = end; j < jl; j += 3 ) { + + const a = j; + const b = j + 1; + const c = j + 2; + + intersection = checkGeometryIntersection( this, groupMaterial, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c ); + + if ( intersection ) { + + intersection.faceIndex = Math.floor( j / 3 ); // triangle number in non-indexed buffer semantics + intersection.face.materialIndex = group.materialIndex; + intersects.push( intersection ); + + } + + } + + } + + } else { + + const start = Math.max( 0, drawRange.start ); + const end = Math.min( position.count, ( drawRange.start + drawRange.count ) ); + + for ( let i = start, il = end; i < il; i += 3 ) { + + const a = i; + const b = i + 1; + const c = i + 2; + + intersection = checkGeometryIntersection( this, material, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c ); + + if ( intersection ) { + + intersection.faceIndex = Math.floor( i / 3 ); // triangle number in non-indexed buffer semantics + intersects.push( intersection ); + + } + + } + + } + + } + + } + +} + +function checkIntersection$1( object, material, raycaster, ray, pA, pB, pC, point ) { + + let intersect; + + if ( material.side === BackSide ) { + + intersect = ray.intersectTriangle( pC, pB, pA, true, point ); + + } else { + + intersect = ray.intersectTriangle( pA, pB, pC, ( material.side === FrontSide ), point ); + + } + + if ( intersect === null ) return null; + + _intersectionPointWorld.copy( point ); + _intersectionPointWorld.applyMatrix4( object.matrixWorld ); + + const distance = raycaster.ray.origin.distanceTo( _intersectionPointWorld ); + + if ( distance < raycaster.near || distance > raycaster.far ) return null; + + return { + distance: distance, + point: _intersectionPointWorld.clone(), + object: object + }; + +} + +function checkGeometryIntersection( object, material, raycaster, ray, uv, uv1, normal, a, b, c ) { + + object.getVertexPosition( a, _vA$1 ); + object.getVertexPosition( b, _vB$1 ); + object.getVertexPosition( c, _vC$1 ); + + const intersection = checkIntersection$1( object, material, raycaster, ray, _vA$1, _vB$1, _vC$1, _intersectionPoint ); + + if ( intersection ) { + + if ( uv ) { + + _uvA$1.fromBufferAttribute( uv, a ); + _uvB$1.fromBufferAttribute( uv, b ); + _uvC$1.fromBufferAttribute( uv, c ); + + intersection.uv = Triangle.getInterpolation( _intersectionPoint, _vA$1, _vB$1, _vC$1, _uvA$1, _uvB$1, _uvC$1, new Vector2() ); + + } + + if ( uv1 ) { + + _uvA$1.fromBufferAttribute( uv1, a ); + _uvB$1.fromBufferAttribute( uv1, b ); + _uvC$1.fromBufferAttribute( uv1, c ); + + intersection.uv1 = Triangle.getInterpolation( _intersectionPoint, _vA$1, _vB$1, _vC$1, _uvA$1, _uvB$1, _uvC$1, new Vector2() ); + + } + + if ( normal ) { + + _normalA.fromBufferAttribute( normal, a ); + _normalB.fromBufferAttribute( normal, b ); + _normalC.fromBufferAttribute( normal, c ); + + intersection.normal = Triangle.getInterpolation( _intersectionPoint, _vA$1, _vB$1, _vC$1, _normalA, _normalB, _normalC, new Vector3() ); + + if ( intersection.normal.dot( ray.direction ) > 0 ) { + + intersection.normal.multiplyScalar( - 1 ); + + } + + } + + const face = { + a: a, + b: b, + c: c, + normal: new Vector3(), + materialIndex: 0 + }; + + Triangle.getNormal( _vA$1, _vB$1, _vC$1, face.normal ); + + intersection.face = face; + + } + + return intersection; + +} + +class BoxGeometry extends BufferGeometry { + + constructor( width = 1, height = 1, depth = 1, widthSegments = 1, heightSegments = 1, depthSegments = 1 ) { + + super(); + + this.type = 'BoxGeometry'; + + this.parameters = { + width: width, + height: height, + depth: depth, + widthSegments: widthSegments, + heightSegments: heightSegments, + depthSegments: depthSegments + }; + + const scope = this; + + // segments + + widthSegments = Math.floor( widthSegments ); + heightSegments = Math.floor( heightSegments ); + depthSegments = Math.floor( depthSegments ); + + // buffers + + const indices = []; + const vertices = []; + const normals = []; + const uvs = []; + + // helper variables + + let numberOfVertices = 0; + let groupStart = 0; + + // build each side of the box geometry + + buildPlane( 'z', 'y', 'x', - 1, - 1, depth, height, width, depthSegments, heightSegments, 0 ); // px + buildPlane( 'z', 'y', 'x', 1, - 1, depth, height, - width, depthSegments, heightSegments, 1 ); // nx + buildPlane( 'x', 'z', 'y', 1, 1, width, depth, height, widthSegments, depthSegments, 2 ); // py + buildPlane( 'x', 'z', 'y', 1, - 1, width, depth, - height, widthSegments, depthSegments, 3 ); // ny + buildPlane( 'x', 'y', 'z', 1, - 1, width, height, depth, widthSegments, heightSegments, 4 ); // pz + buildPlane( 'x', 'y', 'z', - 1, - 1, width, height, - depth, widthSegments, heightSegments, 5 ); // nz + + // build geometry + + this.setIndex( indices ); + this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); + this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); + this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); + + function buildPlane( u, v, w, udir, vdir, width, height, depth, gridX, gridY, materialIndex ) { + + const segmentWidth = width / gridX; + const segmentHeight = height / gridY; + + const widthHalf = width / 2; + const heightHalf = height / 2; + const depthHalf = depth / 2; + + const gridX1 = gridX + 1; + const gridY1 = gridY + 1; + + let vertexCounter = 0; + let groupCount = 0; + + const vector = new Vector3(); + + // generate vertices, normals and uvs + + for ( let iy = 0; iy < gridY1; iy ++ ) { + + const y = iy * segmentHeight - heightHalf; + + for ( let ix = 0; ix < gridX1; ix ++ ) { + + const x = ix * segmentWidth - widthHalf; + + // set values to correct vector component + + vector[ u ] = x * udir; + vector[ v ] = y * vdir; + vector[ w ] = depthHalf; + + // now apply vector to vertex buffer + + vertices.push( vector.x, vector.y, vector.z ); + + // set values to correct vector component + + vector[ u ] = 0; + vector[ v ] = 0; + vector[ w ] = depth > 0 ? 1 : - 1; + + // now apply vector to normal buffer + + normals.push( vector.x, vector.y, vector.z ); + + // uvs + + uvs.push( ix / gridX ); + uvs.push( 1 - ( iy / gridY ) ); + + // counters + + vertexCounter += 1; + + } + + } + + // indices + + // 1. you need three indices to draw a single face + // 2. a single segment consists of two faces + // 3. so we need to generate six (2*3) indices per segment + + for ( let iy = 0; iy < gridY; iy ++ ) { + + for ( let ix = 0; ix < gridX; ix ++ ) { + + const a = numberOfVertices + ix + gridX1 * iy; + const b = numberOfVertices + ix + gridX1 * ( iy + 1 ); + const c = numberOfVertices + ( ix + 1 ) + gridX1 * ( iy + 1 ); + const d = numberOfVertices + ( ix + 1 ) + gridX1 * iy; + + // faces + + indices.push( a, b, d ); + indices.push( b, c, d ); + + // increase counter + + groupCount += 6; + + } + + } + + // add a group to the geometry. this will ensure multi material support + + scope.addGroup( groupStart, groupCount, materialIndex ); + + // calculate new start value for groups + + groupStart += groupCount; + + // update total number of vertices + + numberOfVertices += vertexCounter; + + } + + } + + copy( source ) { + + super.copy( source ); + + this.parameters = Object.assign( {}, source.parameters ); + + return this; + + } + + static fromJSON( data ) { + + return new BoxGeometry( data.width, data.height, data.depth, data.widthSegments, data.heightSegments, data.depthSegments ); + + } + +} + +/** + * Uniform Utilities + */ + +function cloneUniforms( src ) { + + const dst = {}; + + for ( const u in src ) { + + dst[ u ] = {}; + + for ( const p in src[ u ] ) { + + const property = src[ u ][ p ]; + + if ( property && ( property.isColor || + property.isMatrix3 || property.isMatrix4 || + property.isVector2 || property.isVector3 || property.isVector4 || + property.isTexture || property.isQuaternion ) ) { + + if ( property.isRenderTargetTexture ) { + + console.warn( 'UniformsUtils: Textures of render targets cannot be cloned via cloneUniforms() or mergeUniforms().' ); + dst[ u ][ p ] = null; + + } else { + + dst[ u ][ p ] = property.clone(); + + } + + } else if ( Array.isArray( property ) ) { + + dst[ u ][ p ] = property.slice(); + + } else { + + dst[ u ][ p ] = property; + + } + + } + + } + + return dst; + +} + +function mergeUniforms( uniforms ) { + + const merged = {}; + + for ( let u = 0; u < uniforms.length; u ++ ) { + + const tmp = cloneUniforms( uniforms[ u ] ); + + for ( const p in tmp ) { + + merged[ p ] = tmp[ p ]; + + } + + } + + return merged; + +} + +function cloneUniformsGroups( src ) { + + const dst = []; + + for ( let u = 0; u < src.length; u ++ ) { + + dst.push( src[ u ].clone() ); + + } + + return dst; + +} + +function getUnlitUniformColorSpace( renderer ) { + + const currentRenderTarget = renderer.getRenderTarget(); + + if ( currentRenderTarget === null ) { + + // https://github.com/mrdoob/three.js/pull/23937#issuecomment-1111067398 + return renderer.outputColorSpace; + + } + + // https://github.com/mrdoob/three.js/issues/27868 + if ( currentRenderTarget.isXRRenderTarget === true ) { + + return currentRenderTarget.texture.colorSpace; + + } + + return ColorManagement.workingColorSpace; + +} + +// Legacy + +const UniformsUtils = { clone: cloneUniforms, merge: mergeUniforms }; + +var default_vertex = "void main() {\n\tgl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}"; + +var default_fragment = "void main() {\n\tgl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 );\n}"; + +class ShaderMaterial extends Material { + + constructor( parameters ) { + + super(); + + this.isShaderMaterial = true; + + this.type = 'ShaderMaterial'; + + this.defines = {}; + this.uniforms = {}; + this.uniformsGroups = []; + + this.vertexShader = default_vertex; + this.fragmentShader = default_fragment; + + this.linewidth = 1; + + this.wireframe = false; + this.wireframeLinewidth = 1; + + this.fog = false; // set to use scene fog + this.lights = false; // set to use scene lights + this.clipping = false; // set to use user-defined clipping planes + + this.forceSinglePass = true; + + this.extensions = { + clipCullDistance: false, // set to use vertex shader clipping + multiDraw: false // set to use vertex shader multi_draw / enable gl_DrawID + }; + + // When rendered geometry doesn't include these attributes but the material does, + // use these default values in WebGL. This avoids errors when buffer data is missing. + this.defaultAttributeValues = { + 'color': [ 1, 1, 1 ], + 'uv': [ 0, 0 ], + 'uv1': [ 0, 0 ] + }; + + this.index0AttributeName = undefined; + this.uniformsNeedUpdate = false; + + this.glslVersion = null; + + if ( parameters !== undefined ) { + + this.setValues( parameters ); + + } + + } + + copy( source ) { + + super.copy( source ); + + this.fragmentShader = source.fragmentShader; + this.vertexShader = source.vertexShader; + + this.uniforms = cloneUniforms( source.uniforms ); + this.uniformsGroups = cloneUniformsGroups( source.uniformsGroups ); + + this.defines = Object.assign( {}, source.defines ); + + this.wireframe = source.wireframe; + this.wireframeLinewidth = source.wireframeLinewidth; + + this.fog = source.fog; + this.lights = source.lights; + this.clipping = source.clipping; + + this.extensions = Object.assign( {}, source.extensions ); + + this.glslVersion = source.glslVersion; + + return this; + + } + + toJSON( meta ) { + + const data = super.toJSON( meta ); + + data.glslVersion = this.glslVersion; + data.uniforms = {}; + + for ( const name in this.uniforms ) { + + const uniform = this.uniforms[ name ]; + const value = uniform.value; + + if ( value && value.isTexture ) { + + data.uniforms[ name ] = { + type: 't', + value: value.toJSON( meta ).uuid + }; + + } else if ( value && value.isColor ) { + + data.uniforms[ name ] = { + type: 'c', + value: value.getHex() + }; + + } else if ( value && value.isVector2 ) { + + data.uniforms[ name ] = { + type: 'v2', + value: value.toArray() + }; + + } else if ( value && value.isVector3 ) { + + data.uniforms[ name ] = { + type: 'v3', + value: value.toArray() + }; + + } else if ( value && value.isVector4 ) { + + data.uniforms[ name ] = { + type: 'v4', + value: value.toArray() + }; + + } else if ( value && value.isMatrix3 ) { + + data.uniforms[ name ] = { + type: 'm3', + value: value.toArray() + }; + + } else if ( value && value.isMatrix4 ) { + + data.uniforms[ name ] = { + type: 'm4', + value: value.toArray() + }; + + } else { + + data.uniforms[ name ] = { + value: value + }; + + // note: the array variants v2v, v3v, v4v, m4v and tv are not supported so far + + } + + } + + if ( Object.keys( this.defines ).length > 0 ) data.defines = this.defines; + + data.vertexShader = this.vertexShader; + data.fragmentShader = this.fragmentShader; + + data.lights = this.lights; + data.clipping = this.clipping; + + const extensions = {}; + + for ( const key in this.extensions ) { + + if ( this.extensions[ key ] === true ) extensions[ key ] = true; + + } + + if ( Object.keys( extensions ).length > 0 ) data.extensions = extensions; + + return data; + + } + +} + +class Camera extends Object3D { + + constructor() { + + super(); + + this.isCamera = true; + + this.type = 'Camera'; + + this.matrixWorldInverse = new Matrix4(); + + this.projectionMatrix = new Matrix4(); + this.projectionMatrixInverse = new Matrix4(); + + this.coordinateSystem = WebGLCoordinateSystem; + + } + + copy( source, recursive ) { + + super.copy( source, recursive ); + + this.matrixWorldInverse.copy( source.matrixWorldInverse ); + + this.projectionMatrix.copy( source.projectionMatrix ); + this.projectionMatrixInverse.copy( source.projectionMatrixInverse ); + + this.coordinateSystem = source.coordinateSystem; + + return this; + + } + + getWorldDirection( target ) { + + return super.getWorldDirection( target ).negate(); + + } + + updateMatrixWorld( force ) { + + super.updateMatrixWorld( force ); + + this.matrixWorldInverse.copy( this.matrixWorld ).invert(); + + } + + updateWorldMatrix( updateParents, updateChildren ) { + + super.updateWorldMatrix( updateParents, updateChildren ); + + this.matrixWorldInverse.copy( this.matrixWorld ).invert(); + + } + + clone() { + + return new this.constructor().copy( this ); + + } + +} + +const _v3$1 = /*@__PURE__*/ new Vector3(); +const _minTarget = /*@__PURE__*/ new Vector2(); +const _maxTarget = /*@__PURE__*/ new Vector2(); + + +class PerspectiveCamera extends Camera { + + constructor( fov = 50, aspect = 1, near = 0.1, far = 2000 ) { + + super(); + + this.isPerspectiveCamera = true; + + this.type = 'PerspectiveCamera'; + + this.fov = fov; + this.zoom = 1; + + this.near = near; + this.far = far; + this.focus = 10; + + this.aspect = aspect; + this.view = null; + + this.filmGauge = 35; // width of the film (default in millimeters) + this.filmOffset = 0; // horizontal film offset (same unit as gauge) + + this.updateProjectionMatrix(); + + } + + copy( source, recursive ) { + + super.copy( source, recursive ); + + this.fov = source.fov; + this.zoom = source.zoom; + + this.near = source.near; + this.far = source.far; + this.focus = source.focus; + + this.aspect = source.aspect; + this.view = source.view === null ? null : Object.assign( {}, source.view ); + + this.filmGauge = source.filmGauge; + this.filmOffset = source.filmOffset; + + return this; + + } + + /** + * Sets the FOV by focal length in respect to the current .filmGauge. + * + * The default film gauge is 35, so that the focal length can be specified for + * a 35mm (full frame) camera. + * + * Values for focal length and film gauge must have the same unit. + */ + setFocalLength( focalLength ) { + + /** see {@link http://www.bobatkins.com/photography/technical/field_of_view.html} */ + const vExtentSlope = 0.5 * this.getFilmHeight() / focalLength; + + this.fov = RAD2DEG * 2 * Math.atan( vExtentSlope ); + this.updateProjectionMatrix(); + + } + + /** + * Calculates the focal length from the current .fov and .filmGauge. + */ + getFocalLength() { + + const vExtentSlope = Math.tan( DEG2RAD * 0.5 * this.fov ); + + return 0.5 * this.getFilmHeight() / vExtentSlope; + + } + + getEffectiveFOV() { + + return RAD2DEG * 2 * Math.atan( + Math.tan( DEG2RAD * 0.5 * this.fov ) / this.zoom ); + + } + + getFilmWidth() { + + // film not completely covered in portrait format (aspect < 1) + return this.filmGauge * Math.min( this.aspect, 1 ); + + } + + getFilmHeight() { + + // film not completely covered in landscape format (aspect > 1) + return this.filmGauge / Math.max( this.aspect, 1 ); + + } + + /** + * Computes the 2D bounds of the camera's viewable rectangle at a given distance along the viewing direction. + * Sets minTarget and maxTarget to the coordinates of the lower-left and upper-right corners of the view rectangle. + */ + getViewBounds( distance, minTarget, maxTarget ) { + + _v3$1.set( - 1, - 1, 0.5 ).applyMatrix4( this.projectionMatrixInverse ); + + minTarget.set( _v3$1.x, _v3$1.y ).multiplyScalar( - distance / _v3$1.z ); + + _v3$1.set( 1, 1, 0.5 ).applyMatrix4( this.projectionMatrixInverse ); + + maxTarget.set( _v3$1.x, _v3$1.y ).multiplyScalar( - distance / _v3$1.z ); + + } + + /** + * Computes the width and height of the camera's viewable rectangle at a given distance along the viewing direction. + * Copies the result into the target Vector2, where x is width and y is height. + */ + getViewSize( distance, target ) { + + this.getViewBounds( distance, _minTarget, _maxTarget ); + + return target.subVectors( _maxTarget, _minTarget ); + + } + + /** + * Sets an offset in a larger frustum. This is useful for multi-window or + * multi-monitor/multi-machine setups. + * + * For example, if you have 3x2 monitors and each monitor is 1920x1080 and + * the monitors are in grid like this + * + * +---+---+---+ + * | A | B | C | + * +---+---+---+ + * | D | E | F | + * +---+---+---+ + * + * then for each monitor you would call it like this + * + * const w = 1920; + * const h = 1080; + * const fullWidth = w * 3; + * const fullHeight = h * 2; + * + * --A-- + * camera.setViewOffset( fullWidth, fullHeight, w * 0, h * 0, w, h ); + * --B-- + * camera.setViewOffset( fullWidth, fullHeight, w * 1, h * 0, w, h ); + * --C-- + * camera.setViewOffset( fullWidth, fullHeight, w * 2, h * 0, w, h ); + * --D-- + * camera.setViewOffset( fullWidth, fullHeight, w * 0, h * 1, w, h ); + * --E-- + * camera.setViewOffset( fullWidth, fullHeight, w * 1, h * 1, w, h ); + * --F-- + * camera.setViewOffset( fullWidth, fullHeight, w * 2, h * 1, w, h ); + * + * Note there is no reason monitors have to be the same size or in a grid. + */ + setViewOffset( fullWidth, fullHeight, x, y, width, height ) { + + this.aspect = fullWidth / fullHeight; + + if ( this.view === null ) { + + this.view = { + enabled: true, + fullWidth: 1, + fullHeight: 1, + offsetX: 0, + offsetY: 0, + width: 1, + height: 1 + }; + + } + + this.view.enabled = true; + this.view.fullWidth = fullWidth; + this.view.fullHeight = fullHeight; + this.view.offsetX = x; + this.view.offsetY = y; + this.view.width = width; + this.view.height = height; + + this.updateProjectionMatrix(); + + } + + clearViewOffset() { + + if ( this.view !== null ) { + + this.view.enabled = false; + + } + + this.updateProjectionMatrix(); + + } + + updateProjectionMatrix() { + + const near = this.near; + let top = near * Math.tan( DEG2RAD * 0.5 * this.fov ) / this.zoom; + let height = 2 * top; + let width = this.aspect * height; + let left = - 0.5 * width; + const view = this.view; + + if ( this.view !== null && this.view.enabled ) { + + const fullWidth = view.fullWidth, + fullHeight = view.fullHeight; + + left += view.offsetX * width / fullWidth; + top -= view.offsetY * height / fullHeight; + width *= view.width / fullWidth; + height *= view.height / fullHeight; + + } + + const skew = this.filmOffset; + if ( skew !== 0 ) left += near * skew / this.getFilmWidth(); + + this.projectionMatrix.makePerspective( left, left + width, top, top - height, near, this.far, this.coordinateSystem ); + + this.projectionMatrixInverse.copy( this.projectionMatrix ).invert(); + + } + + toJSON( meta ) { + + const data = super.toJSON( meta ); + + data.object.fov = this.fov; + data.object.zoom = this.zoom; + + data.object.near = this.near; + data.object.far = this.far; + data.object.focus = this.focus; + + data.object.aspect = this.aspect; + + if ( this.view !== null ) data.object.view = Object.assign( {}, this.view ); + + data.object.filmGauge = this.filmGauge; + data.object.filmOffset = this.filmOffset; + + return data; + + } + +} + +const fov = - 90; // negative fov is not an error +const aspect = 1; + +class CubeCamera extends Object3D { + + constructor( near, far, renderTarget ) { + + super(); + + this.type = 'CubeCamera'; + + this.renderTarget = renderTarget; + this.coordinateSystem = null; + this.activeMipmapLevel = 0; + + const cameraPX = new PerspectiveCamera( fov, aspect, near, far ); + cameraPX.layers = this.layers; + this.add( cameraPX ); + + const cameraNX = new PerspectiveCamera( fov, aspect, near, far ); + cameraNX.layers = this.layers; + this.add( cameraNX ); + + const cameraPY = new PerspectiveCamera( fov, aspect, near, far ); + cameraPY.layers = this.layers; + this.add( cameraPY ); + + const cameraNY = new PerspectiveCamera( fov, aspect, near, far ); + cameraNY.layers = this.layers; + this.add( cameraNY ); + + const cameraPZ = new PerspectiveCamera( fov, aspect, near, far ); + cameraPZ.layers = this.layers; + this.add( cameraPZ ); + + const cameraNZ = new PerspectiveCamera( fov, aspect, near, far ); + cameraNZ.layers = this.layers; + this.add( cameraNZ ); + + } + + updateCoordinateSystem() { + + const coordinateSystem = this.coordinateSystem; + + const cameras = this.children.concat(); + + const [ cameraPX, cameraNX, cameraPY, cameraNY, cameraPZ, cameraNZ ] = cameras; + + for ( const camera of cameras ) this.remove( camera ); + + if ( coordinateSystem === WebGLCoordinateSystem ) { + + cameraPX.up.set( 0, 1, 0 ); + cameraPX.lookAt( 1, 0, 0 ); + + cameraNX.up.set( 0, 1, 0 ); + cameraNX.lookAt( - 1, 0, 0 ); + + cameraPY.up.set( 0, 0, - 1 ); + cameraPY.lookAt( 0, 1, 0 ); + + cameraNY.up.set( 0, 0, 1 ); + cameraNY.lookAt( 0, - 1, 0 ); + + cameraPZ.up.set( 0, 1, 0 ); + cameraPZ.lookAt( 0, 0, 1 ); + + cameraNZ.up.set( 0, 1, 0 ); + cameraNZ.lookAt( 0, 0, - 1 ); + + } else if ( coordinateSystem === WebGPUCoordinateSystem ) { + + cameraPX.up.set( 0, - 1, 0 ); + cameraPX.lookAt( - 1, 0, 0 ); + + cameraNX.up.set( 0, - 1, 0 ); + cameraNX.lookAt( 1, 0, 0 ); + + cameraPY.up.set( 0, 0, 1 ); + cameraPY.lookAt( 0, 1, 0 ); + + cameraNY.up.set( 0, 0, - 1 ); + cameraNY.lookAt( 0, - 1, 0 ); + + cameraPZ.up.set( 0, - 1, 0 ); + cameraPZ.lookAt( 0, 0, 1 ); + + cameraNZ.up.set( 0, - 1, 0 ); + cameraNZ.lookAt( 0, 0, - 1 ); + + } else { + + throw new Error( 'THREE.CubeCamera.updateCoordinateSystem(): Invalid coordinate system: ' + coordinateSystem ); + + } + + for ( const camera of cameras ) { + + this.add( camera ); + + camera.updateMatrixWorld(); + + } + + } + + update( renderer, scene ) { + + if ( this.parent === null ) this.updateMatrixWorld(); + + const { renderTarget, activeMipmapLevel } = this; + + if ( this.coordinateSystem !== renderer.coordinateSystem ) { + + this.coordinateSystem = renderer.coordinateSystem; + + this.updateCoordinateSystem(); + + } + + const [ cameraPX, cameraNX, cameraPY, cameraNY, cameraPZ, cameraNZ ] = this.children; + + const currentRenderTarget = renderer.getRenderTarget(); + const currentActiveCubeFace = renderer.getActiveCubeFace(); + const currentActiveMipmapLevel = renderer.getActiveMipmapLevel(); + + const currentXrEnabled = renderer.xr.enabled; + + renderer.xr.enabled = false; + + const generateMipmaps = renderTarget.texture.generateMipmaps; + + renderTarget.texture.generateMipmaps = false; + + renderer.setRenderTarget( renderTarget, 0, activeMipmapLevel ); + renderer.render( scene, cameraPX ); + + renderer.setRenderTarget( renderTarget, 1, activeMipmapLevel ); + renderer.render( scene, cameraNX ); + + renderer.setRenderTarget( renderTarget, 2, activeMipmapLevel ); + renderer.render( scene, cameraPY ); + + renderer.setRenderTarget( renderTarget, 3, activeMipmapLevel ); + renderer.render( scene, cameraNY ); + + renderer.setRenderTarget( renderTarget, 4, activeMipmapLevel ); + renderer.render( scene, cameraPZ ); + + // mipmaps are generated during the last call of render() + // at this point, all sides of the cube render target are defined + + renderTarget.texture.generateMipmaps = generateMipmaps; + + renderer.setRenderTarget( renderTarget, 5, activeMipmapLevel ); + renderer.render( scene, cameraNZ ); + + renderer.setRenderTarget( currentRenderTarget, currentActiveCubeFace, currentActiveMipmapLevel ); + + renderer.xr.enabled = currentXrEnabled; + + renderTarget.texture.needsPMREMUpdate = true; + + } + +} + +class CubeTexture extends Texture { + + constructor( images, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, colorSpace ) { + + images = images !== undefined ? images : []; + mapping = mapping !== undefined ? mapping : CubeReflectionMapping; + + super( images, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, colorSpace ); + + this.isCubeTexture = true; + + this.flipY = false; + + } + + get images() { + + return this.image; + + } + + set images( value ) { + + this.image = value; + + } + +} + +class WebGLCubeRenderTarget extends WebGLRenderTarget { + + constructor( size = 1, options = {} ) { + + super( size, size, options ); + + this.isWebGLCubeRenderTarget = true; + + const image = { width: size, height: size, depth: 1 }; + const images = [ image, image, image, image, image, image ]; + + this.texture = new CubeTexture( images, options.mapping, options.wrapS, options.wrapT, options.magFilter, options.minFilter, options.format, options.type, options.anisotropy, options.colorSpace ); + + // By convention -- likely based on the RenderMan spec from the 1990's -- cube maps are specified by WebGL (and three.js) + // in a coordinate system in which positive-x is to the right when looking up the positive-z axis -- in other words, + // in a left-handed coordinate system. By continuing this convention, preexisting cube maps continued to render correctly. + + // three.js uses a right-handed coordinate system. So environment maps used in three.js appear to have px and nx swapped + // and the flag isRenderTargetTexture controls this conversion. The flip is not required when using WebGLCubeRenderTarget.texture + // as a cube texture (this is detected when isRenderTargetTexture is set to true for cube textures). + + this.texture.isRenderTargetTexture = true; + + this.texture.generateMipmaps = options.generateMipmaps !== undefined ? options.generateMipmaps : false; + this.texture.minFilter = options.minFilter !== undefined ? options.minFilter : LinearFilter; + + } + + fromEquirectangularTexture( renderer, texture ) { + + this.texture.type = texture.type; + this.texture.colorSpace = texture.colorSpace; + + this.texture.generateMipmaps = texture.generateMipmaps; + this.texture.minFilter = texture.minFilter; + this.texture.magFilter = texture.magFilter; + + const shader = { + + uniforms: { + tEquirect: { value: null }, + }, + + vertexShader: /* glsl */` + + varying vec3 vWorldDirection; + + vec3 transformDirection( in vec3 dir, in mat4 matrix ) { + + return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz ); + + } + + void main() { + + vWorldDirection = transformDirection( position, modelMatrix ); + + #include + #include + + } + `, + + fragmentShader: /* glsl */` + + uniform sampler2D tEquirect; + + varying vec3 vWorldDirection; + + #include + + void main() { + + vec3 direction = normalize( vWorldDirection ); + + vec2 sampleUV = equirectUv( direction ); + + gl_FragColor = texture2D( tEquirect, sampleUV ); + + } + ` + }; + + const geometry = new BoxGeometry( 5, 5, 5 ); + + const material = new ShaderMaterial( { + + name: 'CubemapFromEquirect', + + uniforms: cloneUniforms( shader.uniforms ), + vertexShader: shader.vertexShader, + fragmentShader: shader.fragmentShader, + side: BackSide, + blending: NoBlending + + } ); + + material.uniforms.tEquirect.value = texture; + + const mesh = new Mesh( geometry, material ); + + const currentMinFilter = texture.minFilter; + + // Avoid blurred poles + if ( texture.minFilter === LinearMipmapLinearFilter ) texture.minFilter = LinearFilter; + + const camera = new CubeCamera( 1, 10, this ); + camera.update( renderer, mesh ); + + texture.minFilter = currentMinFilter; + + mesh.geometry.dispose(); + mesh.material.dispose(); + + return this; + + } + + clear( renderer, color, depth, stencil ) { + + const currentRenderTarget = renderer.getRenderTarget(); + + for ( let i = 0; i < 6; i ++ ) { + + renderer.setRenderTarget( this, i ); + + renderer.clear( color, depth, stencil ); + + } + + renderer.setRenderTarget( currentRenderTarget ); + + } + +} + +const _vector1 = /*@__PURE__*/ new Vector3(); +const _vector2 = /*@__PURE__*/ new Vector3(); +const _normalMatrix = /*@__PURE__*/ new Matrix3(); + +class Plane { + + constructor( normal = new Vector3( 1, 0, 0 ), constant = 0 ) { + + this.isPlane = true; + + // normal is assumed to be normalized + + this.normal = normal; + this.constant = constant; + + } + + set( normal, constant ) { + + this.normal.copy( normal ); + this.constant = constant; + + return this; + + } + + setComponents( x, y, z, w ) { + + this.normal.set( x, y, z ); + this.constant = w; + + return this; + + } + + setFromNormalAndCoplanarPoint( normal, point ) { + + this.normal.copy( normal ); + this.constant = - point.dot( this.normal ); + + return this; + + } + + setFromCoplanarPoints( a, b, c ) { + + const normal = _vector1.subVectors( c, b ).cross( _vector2.subVectors( a, b ) ).normalize(); + + // Q: should an error be thrown if normal is zero (e.g. degenerate plane)? + + this.setFromNormalAndCoplanarPoint( normal, a ); + + return this; + + } + + copy( plane ) { + + this.normal.copy( plane.normal ); + this.constant = plane.constant; + + return this; + + } + + normalize() { + + // Note: will lead to a divide by zero if the plane is invalid. + + const inverseNormalLength = 1.0 / this.normal.length(); + this.normal.multiplyScalar( inverseNormalLength ); + this.constant *= inverseNormalLength; + + return this; + + } + + negate() { + + this.constant *= - 1; + this.normal.negate(); + + return this; + + } + + distanceToPoint( point ) { + + return this.normal.dot( point ) + this.constant; + + } + + distanceToSphere( sphere ) { + + return this.distanceToPoint( sphere.center ) - sphere.radius; + + } + + projectPoint( point, target ) { + + return target.copy( point ).addScaledVector( this.normal, - this.distanceToPoint( point ) ); + + } + + intersectLine( line, target ) { + + const direction = line.delta( _vector1 ); + + const denominator = this.normal.dot( direction ); + + if ( denominator === 0 ) { + + // line is coplanar, return origin + if ( this.distanceToPoint( line.start ) === 0 ) { + + return target.copy( line.start ); + + } + + // Unsure if this is the correct method to handle this case. + return null; + + } + + const t = - ( line.start.dot( this.normal ) + this.constant ) / denominator; + + if ( t < 0 || t > 1 ) { + + return null; + + } + + return target.copy( line.start ).addScaledVector( direction, t ); + + } + + intersectsLine( line ) { + + // Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it. + + const startSign = this.distanceToPoint( line.start ); + const endSign = this.distanceToPoint( line.end ); + + return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 ); + + } + + intersectsBox( box ) { + + return box.intersectsPlane( this ); + + } + + intersectsSphere( sphere ) { + + return sphere.intersectsPlane( this ); + + } + + coplanarPoint( target ) { + + return target.copy( this.normal ).multiplyScalar( - this.constant ); + + } + + applyMatrix4( matrix, optionalNormalMatrix ) { + + const normalMatrix = optionalNormalMatrix || _normalMatrix.getNormalMatrix( matrix ); + + const referencePoint = this.coplanarPoint( _vector1 ).applyMatrix4( matrix ); + + const normal = this.normal.applyMatrix3( normalMatrix ).normalize(); + + this.constant = - referencePoint.dot( normal ); + + return this; + + } + + translate( offset ) { + + this.constant -= offset.dot( this.normal ); + + return this; + + } + + equals( plane ) { + + return plane.normal.equals( this.normal ) && ( plane.constant === this.constant ); + + } + + clone() { + + return new this.constructor().copy( this ); + + } + +} + +const _sphere$5 = /*@__PURE__*/ new Sphere(); +const _vector$7 = /*@__PURE__*/ new Vector3(); + +class Frustum { + + constructor( p0 = new Plane(), p1 = new Plane(), p2 = new Plane(), p3 = new Plane(), p4 = new Plane(), p5 = new Plane() ) { + + this.planes = [ p0, p1, p2, p3, p4, p5 ]; + + } + + set( p0, p1, p2, p3, p4, p5 ) { + + const planes = this.planes; + + planes[ 0 ].copy( p0 ); + planes[ 1 ].copy( p1 ); + planes[ 2 ].copy( p2 ); + planes[ 3 ].copy( p3 ); + planes[ 4 ].copy( p4 ); + planes[ 5 ].copy( p5 ); + + return this; + + } + + copy( frustum ) { + + const planes = this.planes; + + for ( let i = 0; i < 6; i ++ ) { + + planes[ i ].copy( frustum.planes[ i ] ); + + } + + return this; + + } + + setFromProjectionMatrix( m, coordinateSystem = WebGLCoordinateSystem ) { + + const planes = this.planes; + const me = m.elements; + const me0 = me[ 0 ], me1 = me[ 1 ], me2 = me[ 2 ], me3 = me[ 3 ]; + const me4 = me[ 4 ], me5 = me[ 5 ], me6 = me[ 6 ], me7 = me[ 7 ]; + const me8 = me[ 8 ], me9 = me[ 9 ], me10 = me[ 10 ], me11 = me[ 11 ]; + const me12 = me[ 12 ], me13 = me[ 13 ], me14 = me[ 14 ], me15 = me[ 15 ]; + + planes[ 0 ].setComponents( me3 - me0, me7 - me4, me11 - me8, me15 - me12 ).normalize(); + planes[ 1 ].setComponents( me3 + me0, me7 + me4, me11 + me8, me15 + me12 ).normalize(); + planes[ 2 ].setComponents( me3 + me1, me7 + me5, me11 + me9, me15 + me13 ).normalize(); + planes[ 3 ].setComponents( me3 - me1, me7 - me5, me11 - me9, me15 - me13 ).normalize(); + planes[ 4 ].setComponents( me3 - me2, me7 - me6, me11 - me10, me15 - me14 ).normalize(); + + if ( coordinateSystem === WebGLCoordinateSystem ) { + + planes[ 5 ].setComponents( me3 + me2, me7 + me6, me11 + me10, me15 + me14 ).normalize(); + + } else if ( coordinateSystem === WebGPUCoordinateSystem ) { + + planes[ 5 ].setComponents( me2, me6, me10, me14 ).normalize(); + + } else { + + throw new Error( 'THREE.Frustum.setFromProjectionMatrix(): Invalid coordinate system: ' + coordinateSystem ); + + } + + return this; + + } + + intersectsObject( object ) { + + if ( object.boundingSphere !== undefined ) { + + if ( object.boundingSphere === null ) object.computeBoundingSphere(); + + _sphere$5.copy( object.boundingSphere ).applyMatrix4( object.matrixWorld ); + + } else { + + const geometry = object.geometry; + + if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere(); + + _sphere$5.copy( geometry.boundingSphere ).applyMatrix4( object.matrixWorld ); + + } + + return this.intersectsSphere( _sphere$5 ); + + } + + intersectsSprite( sprite ) { + + _sphere$5.center.set( 0, 0, 0 ); + _sphere$5.radius = 0.7071067811865476; + _sphere$5.applyMatrix4( sprite.matrixWorld ); + + return this.intersectsSphere( _sphere$5 ); + + } + + intersectsSphere( sphere ) { + + const planes = this.planes; + const center = sphere.center; + const negRadius = - sphere.radius; + + for ( let i = 0; i < 6; i ++ ) { + + const distance = planes[ i ].distanceToPoint( center ); + + if ( distance < negRadius ) { + + return false; + + } + + } + + return true; + + } + + intersectsBox( box ) { + + const planes = this.planes; + + for ( let i = 0; i < 6; i ++ ) { + + const plane = planes[ i ]; + + // corner at max distance + + _vector$7.x = plane.normal.x > 0 ? box.max.x : box.min.x; + _vector$7.y = plane.normal.y > 0 ? box.max.y : box.min.y; + _vector$7.z = plane.normal.z > 0 ? box.max.z : box.min.z; + + if ( plane.distanceToPoint( _vector$7 ) < 0 ) { + + return false; + + } + + } + + return true; + + } + + containsPoint( point ) { + + const planes = this.planes; + + for ( let i = 0; i < 6; i ++ ) { + + if ( planes[ i ].distanceToPoint( point ) < 0 ) { + + return false; + + } + + } + + return true; + + } + + clone() { + + return new this.constructor().copy( this ); + + } + +} + +function WebGLAnimation() { + + let context = null; + let isAnimating = false; + let animationLoop = null; + let requestId = null; + + function onAnimationFrame( time, frame ) { + + animationLoop( time, frame ); + + requestId = context.requestAnimationFrame( onAnimationFrame ); + + } + + return { + + start: function () { + + if ( isAnimating === true ) return; + if ( animationLoop === null ) return; + + requestId = context.requestAnimationFrame( onAnimationFrame ); + + isAnimating = true; + + }, + + stop: function () { + + context.cancelAnimationFrame( requestId ); + + isAnimating = false; + + }, + + setAnimationLoop: function ( callback ) { + + animationLoop = callback; + + }, + + setContext: function ( value ) { + + context = value; + + } + + }; + +} + +function WebGLAttributes( gl ) { + + const buffers = new WeakMap(); + + function createBuffer( attribute, bufferType ) { + + const array = attribute.array; + const usage = attribute.usage; + const size = array.byteLength; + + const buffer = gl.createBuffer(); + + gl.bindBuffer( bufferType, buffer ); + gl.bufferData( bufferType, array, usage ); + + attribute.onUploadCallback(); + + let type; + + if ( array instanceof Float32Array ) { + + type = gl.FLOAT; + + } else if ( array instanceof Uint16Array ) { + + if ( attribute.isFloat16BufferAttribute ) { + + type = gl.HALF_FLOAT; + + } else { + + type = gl.UNSIGNED_SHORT; + + } + + } else if ( array instanceof Int16Array ) { + + type = gl.SHORT; + + } else if ( array instanceof Uint32Array ) { + + type = gl.UNSIGNED_INT; + + } else if ( array instanceof Int32Array ) { + + type = gl.INT; + + } else if ( array instanceof Int8Array ) { + + type = gl.BYTE; + + } else if ( array instanceof Uint8Array ) { + + type = gl.UNSIGNED_BYTE; + + } else if ( array instanceof Uint8ClampedArray ) { + + type = gl.UNSIGNED_BYTE; + + } else { + + throw new Error( 'THREE.WebGLAttributes: Unsupported buffer data format: ' + array ); + + } + + return { + buffer: buffer, + type: type, + bytesPerElement: array.BYTES_PER_ELEMENT, + version: attribute.version, + size: size + }; + + } + + function updateBuffer( buffer, attribute, bufferType ) { + + const array = attribute.array; + const updateRange = attribute._updateRange; // @deprecated, r159 + const updateRanges = attribute.updateRanges; + + gl.bindBuffer( bufferType, buffer ); + + if ( updateRange.count === - 1 && updateRanges.length === 0 ) { + + // Not using update ranges + gl.bufferSubData( bufferType, 0, array ); + + } + + if ( updateRanges.length !== 0 ) { + + for ( let i = 0, l = updateRanges.length; i < l; i ++ ) { + + const range = updateRanges[ i ]; + + gl.bufferSubData( bufferType, range.start * array.BYTES_PER_ELEMENT, + array, range.start, range.count ); + + } + + attribute.clearUpdateRanges(); + + } + + // @deprecated, r159 + if ( updateRange.count !== - 1 ) { + + gl.bufferSubData( bufferType, updateRange.offset * array.BYTES_PER_ELEMENT, + array, updateRange.offset, updateRange.count ); + + updateRange.count = - 1; // reset range + + } + + attribute.onUploadCallback(); + + } + + // + + function get( attribute ) { + + if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data; + + return buffers.get( attribute ); + + } + + function remove( attribute ) { + + if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data; + + const data = buffers.get( attribute ); + + if ( data ) { + + gl.deleteBuffer( data.buffer ); + + buffers.delete( attribute ); + + } + + } + + function update( attribute, bufferType ) { + + if ( attribute.isGLBufferAttribute ) { + + const cached = buffers.get( attribute ); + + if ( ! cached || cached.version < attribute.version ) { + + buffers.set( attribute, { + buffer: attribute.buffer, + type: attribute.type, + bytesPerElement: attribute.elementSize, + version: attribute.version + } ); + + } + + return; + + } + + if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data; + + const data = buffers.get( attribute ); + + if ( data === undefined ) { + + buffers.set( attribute, createBuffer( attribute, bufferType ) ); + + } else if ( data.version < attribute.version ) { + + if ( data.size !== attribute.array.byteLength ) { + + throw new Error( 'THREE.WebGLAttributes: The size of the buffer attribute\'s array buffer does not match the original size. Resizing buffer attributes is not supported.' ); + + } + + updateBuffer( data.buffer, attribute, bufferType ); + + data.version = attribute.version; + + } + + } + + return { + + get: get, + remove: remove, + update: update + + }; + +} + +class PlaneGeometry extends BufferGeometry { + + constructor( width = 1, height = 1, widthSegments = 1, heightSegments = 1 ) { + + super(); + + this.type = 'PlaneGeometry'; + + this.parameters = { + width: width, + height: height, + widthSegments: widthSegments, + heightSegments: heightSegments + }; + + const width_half = width / 2; + const height_half = height / 2; + + const gridX = Math.floor( widthSegments ); + const gridY = Math.floor( heightSegments ); + + const gridX1 = gridX + 1; + const gridY1 = gridY + 1; + + const segment_width = width / gridX; + const segment_height = height / gridY; + + // + + const indices = []; + const vertices = []; + const normals = []; + const uvs = []; + + for ( let iy = 0; iy < gridY1; iy ++ ) { + + const y = iy * segment_height - height_half; + + for ( let ix = 0; ix < gridX1; ix ++ ) { + + const x = ix * segment_width - width_half; + + vertices.push( x, - y, 0 ); + + normals.push( 0, 0, 1 ); + + uvs.push( ix / gridX ); + uvs.push( 1 - ( iy / gridY ) ); + + } + + } + + for ( let iy = 0; iy < gridY; iy ++ ) { + + for ( let ix = 0; ix < gridX; ix ++ ) { + + const a = ix + gridX1 * iy; + const b = ix + gridX1 * ( iy + 1 ); + const c = ( ix + 1 ) + gridX1 * ( iy + 1 ); + const d = ( ix + 1 ) + gridX1 * iy; + + indices.push( a, b, d ); + indices.push( b, c, d ); + + } + + } + + this.setIndex( indices ); + this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); + this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); + this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); + + } + + copy( source ) { + + super.copy( source ); + + this.parameters = Object.assign( {}, source.parameters ); + + return this; + + } + + static fromJSON( data ) { + + return new PlaneGeometry( data.width, data.height, data.widthSegments, data.heightSegments ); + + } + +} + +var alphahash_fragment = "#ifdef USE_ALPHAHASH\n\tif ( diffuseColor.a < getAlphaHashThreshold( vPosition ) ) discard;\n#endif"; + +var alphahash_pars_fragment = "#ifdef USE_ALPHAHASH\n\tconst float ALPHA_HASH_SCALE = 0.05;\n\tfloat hash2D( vec2 value ) {\n\t\treturn fract( 1.0e4 * sin( 17.0 * value.x + 0.1 * value.y ) * ( 0.1 + abs( sin( 13.0 * value.y + value.x ) ) ) );\n\t}\n\tfloat hash3D( vec3 value ) {\n\t\treturn hash2D( vec2( hash2D( value.xy ), value.z ) );\n\t}\n\tfloat getAlphaHashThreshold( vec3 position ) {\n\t\tfloat maxDeriv = max(\n\t\t\tlength( dFdx( position.xyz ) ),\n\t\t\tlength( dFdy( position.xyz ) )\n\t\t);\n\t\tfloat pixScale = 1.0 / ( ALPHA_HASH_SCALE * maxDeriv );\n\t\tvec2 pixScales = vec2(\n\t\t\texp2( floor( log2( pixScale ) ) ),\n\t\t\texp2( ceil( log2( pixScale ) ) )\n\t\t);\n\t\tvec2 alpha = vec2(\n\t\t\thash3D( floor( pixScales.x * position.xyz ) ),\n\t\t\thash3D( floor( pixScales.y * position.xyz ) )\n\t\t);\n\t\tfloat lerpFactor = fract( log2( pixScale ) );\n\t\tfloat x = ( 1.0 - lerpFactor ) * alpha.x + lerpFactor * alpha.y;\n\t\tfloat a = min( lerpFactor, 1.0 - lerpFactor );\n\t\tvec3 cases = vec3(\n\t\t\tx * x / ( 2.0 * a * ( 1.0 - a ) ),\n\t\t\t( x - 0.5 * a ) / ( 1.0 - a ),\n\t\t\t1.0 - ( ( 1.0 - x ) * ( 1.0 - x ) / ( 2.0 * a * ( 1.0 - a ) ) )\n\t\t);\n\t\tfloat threshold = ( x < ( 1.0 - a ) )\n\t\t\t? ( ( x < a ) ? cases.x : cases.y )\n\t\t\t: cases.z;\n\t\treturn clamp( threshold , 1.0e-6, 1.0 );\n\t}\n#endif"; + +var alphamap_fragment = "#ifdef USE_ALPHAMAP\n\tdiffuseColor.a *= texture2D( alphaMap, vAlphaMapUv ).g;\n#endif"; + +var alphamap_pars_fragment = "#ifdef USE_ALPHAMAP\n\tuniform sampler2D alphaMap;\n#endif"; + +var alphatest_fragment = "#ifdef USE_ALPHATEST\n\t#ifdef ALPHA_TO_COVERAGE\n\tdiffuseColor.a = smoothstep( alphaTest, alphaTest + fwidth( diffuseColor.a ), diffuseColor.a );\n\tif ( diffuseColor.a == 0.0 ) discard;\n\t#else\n\tif ( diffuseColor.a < alphaTest ) discard;\n\t#endif\n#endif"; + +var alphatest_pars_fragment = "#ifdef USE_ALPHATEST\n\tuniform float alphaTest;\n#endif"; + +var aomap_fragment = "#ifdef USE_AOMAP\n\tfloat ambientOcclusion = ( texture2D( aoMap, vAoMapUv ).r - 1.0 ) * aoMapIntensity + 1.0;\n\treflectedLight.indirectDiffuse *= ambientOcclusion;\n\t#if defined( USE_CLEARCOAT ) \n\t\tclearcoatSpecularIndirect *= ambientOcclusion;\n\t#endif\n\t#if defined( USE_SHEEN ) \n\t\tsheenSpecularIndirect *= ambientOcclusion;\n\t#endif\n\t#if defined( USE_ENVMAP ) && defined( STANDARD )\n\t\tfloat dotNV = saturate( dot( geometryNormal, geometryViewDir ) );\n\t\treflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, ambientOcclusion, material.roughness );\n\t#endif\n#endif"; + +var aomap_pars_fragment = "#ifdef USE_AOMAP\n\tuniform sampler2D aoMap;\n\tuniform float aoMapIntensity;\n#endif"; + +var batching_pars_vertex = "#ifdef USE_BATCHING\n\tattribute float batchId;\n\tuniform highp sampler2D batchingTexture;\n\tmat4 getBatchingMatrix( const in float i ) {\n\t\tint size = textureSize( batchingTexture, 0 ).x;\n\t\tint j = int( i ) * 4;\n\t\tint x = j % size;\n\t\tint y = j / size;\n\t\tvec4 v1 = texelFetch( batchingTexture, ivec2( x, y ), 0 );\n\t\tvec4 v2 = texelFetch( batchingTexture, ivec2( x + 1, y ), 0 );\n\t\tvec4 v3 = texelFetch( batchingTexture, ivec2( x + 2, y ), 0 );\n\t\tvec4 v4 = texelFetch( batchingTexture, ivec2( x + 3, y ), 0 );\n\t\treturn mat4( v1, v2, v3, v4 );\n\t}\n#endif"; + +var batching_vertex = "#ifdef USE_BATCHING\n\tmat4 batchingMatrix = getBatchingMatrix( batchId );\n#endif"; + +var begin_vertex = "vec3 transformed = vec3( position );\n#ifdef USE_ALPHAHASH\n\tvPosition = vec3( position );\n#endif"; + +var beginnormal_vertex = "vec3 objectNormal = vec3( normal );\n#ifdef USE_TANGENT\n\tvec3 objectTangent = vec3( tangent.xyz );\n#endif"; + +var bsdfs = "float G_BlinnPhong_Implicit( ) {\n\treturn 0.25;\n}\nfloat D_BlinnPhong( const in float shininess, const in float dotNH ) {\n\treturn RECIPROCAL_PI * ( shininess * 0.5 + 1.0 ) * pow( dotNH, shininess );\n}\nvec3 BRDF_BlinnPhong( const in vec3 lightDir, const in vec3 viewDir, const in vec3 normal, const in vec3 specularColor, const in float shininess ) {\n\tvec3 halfDir = normalize( lightDir + viewDir );\n\tfloat dotNH = saturate( dot( normal, halfDir ) );\n\tfloat dotVH = saturate( dot( viewDir, halfDir ) );\n\tvec3 F = F_Schlick( specularColor, 1.0, dotVH );\n\tfloat G = G_BlinnPhong_Implicit( );\n\tfloat D = D_BlinnPhong( shininess, dotNH );\n\treturn F * ( G * D );\n} // validated"; + +var iridescence_fragment = "#ifdef USE_IRIDESCENCE\n\tconst mat3 XYZ_TO_REC709 = mat3(\n\t\t 3.2404542, -0.9692660, 0.0556434,\n\t\t-1.5371385, 1.8760108, -0.2040259,\n\t\t-0.4985314, 0.0415560, 1.0572252\n\t);\n\tvec3 Fresnel0ToIor( vec3 fresnel0 ) {\n\t\tvec3 sqrtF0 = sqrt( fresnel0 );\n\t\treturn ( vec3( 1.0 ) + sqrtF0 ) / ( vec3( 1.0 ) - sqrtF0 );\n\t}\n\tvec3 IorToFresnel0( vec3 transmittedIor, float incidentIor ) {\n\t\treturn pow2( ( transmittedIor - vec3( incidentIor ) ) / ( transmittedIor + vec3( incidentIor ) ) );\n\t}\n\tfloat IorToFresnel0( float transmittedIor, float incidentIor ) {\n\t\treturn pow2( ( transmittedIor - incidentIor ) / ( transmittedIor + incidentIor ));\n\t}\n\tvec3 evalSensitivity( float OPD, vec3 shift ) {\n\t\tfloat phase = 2.0 * PI * OPD * 1.0e-9;\n\t\tvec3 val = vec3( 5.4856e-13, 4.4201e-13, 5.2481e-13 );\n\t\tvec3 pos = vec3( 1.6810e+06, 1.7953e+06, 2.2084e+06 );\n\t\tvec3 var = vec3( 4.3278e+09, 9.3046e+09, 6.6121e+09 );\n\t\tvec3 xyz = val * sqrt( 2.0 * PI * var ) * cos( pos * phase + shift ) * exp( - pow2( phase ) * var );\n\t\txyz.x += 9.7470e-14 * sqrt( 2.0 * PI * 4.5282e+09 ) * cos( 2.2399e+06 * phase + shift[ 0 ] ) * exp( - 4.5282e+09 * pow2( phase ) );\n\t\txyz /= 1.0685e-7;\n\t\tvec3 rgb = XYZ_TO_REC709 * xyz;\n\t\treturn rgb;\n\t}\n\tvec3 evalIridescence( float outsideIOR, float eta2, float cosTheta1, float thinFilmThickness, vec3 baseF0 ) {\n\t\tvec3 I;\n\t\tfloat iridescenceIOR = mix( outsideIOR, eta2, smoothstep( 0.0, 0.03, thinFilmThickness ) );\n\t\tfloat sinTheta2Sq = pow2( outsideIOR / iridescenceIOR ) * ( 1.0 - pow2( cosTheta1 ) );\n\t\tfloat cosTheta2Sq = 1.0 - sinTheta2Sq;\n\t\tif ( cosTheta2Sq < 0.0 ) {\n\t\t\treturn vec3( 1.0 );\n\t\t}\n\t\tfloat cosTheta2 = sqrt( cosTheta2Sq );\n\t\tfloat R0 = IorToFresnel0( iridescenceIOR, outsideIOR );\n\t\tfloat R12 = F_Schlick( R0, 1.0, cosTheta1 );\n\t\tfloat T121 = 1.0 - R12;\n\t\tfloat phi12 = 0.0;\n\t\tif ( iridescenceIOR < outsideIOR ) phi12 = PI;\n\t\tfloat phi21 = PI - phi12;\n\t\tvec3 baseIOR = Fresnel0ToIor( clamp( baseF0, 0.0, 0.9999 ) );\t\tvec3 R1 = IorToFresnel0( baseIOR, iridescenceIOR );\n\t\tvec3 R23 = F_Schlick( R1, 1.0, cosTheta2 );\n\t\tvec3 phi23 = vec3( 0.0 );\n\t\tif ( baseIOR[ 0 ] < iridescenceIOR ) phi23[ 0 ] = PI;\n\t\tif ( baseIOR[ 1 ] < iridescenceIOR ) phi23[ 1 ] = PI;\n\t\tif ( baseIOR[ 2 ] < iridescenceIOR ) phi23[ 2 ] = PI;\n\t\tfloat OPD = 2.0 * iridescenceIOR * thinFilmThickness * cosTheta2;\n\t\tvec3 phi = vec3( phi21 ) + phi23;\n\t\tvec3 R123 = clamp( R12 * R23, 1e-5, 0.9999 );\n\t\tvec3 r123 = sqrt( R123 );\n\t\tvec3 Rs = pow2( T121 ) * R23 / ( vec3( 1.0 ) - R123 );\n\t\tvec3 C0 = R12 + Rs;\n\t\tI = C0;\n\t\tvec3 Cm = Rs - T121;\n\t\tfor ( int m = 1; m <= 2; ++ m ) {\n\t\t\tCm *= r123;\n\t\t\tvec3 Sm = 2.0 * evalSensitivity( float( m ) * OPD, float( m ) * phi );\n\t\t\tI += Cm * Sm;\n\t\t}\n\t\treturn max( I, vec3( 0.0 ) );\n\t}\n#endif"; + +var bumpmap_pars_fragment = "#ifdef USE_BUMPMAP\n\tuniform sampler2D bumpMap;\n\tuniform float bumpScale;\n\tvec2 dHdxy_fwd() {\n\t\tvec2 dSTdx = dFdx( vBumpMapUv );\n\t\tvec2 dSTdy = dFdy( vBumpMapUv );\n\t\tfloat Hll = bumpScale * texture2D( bumpMap, vBumpMapUv ).x;\n\t\tfloat dBx = bumpScale * texture2D( bumpMap, vBumpMapUv + dSTdx ).x - Hll;\n\t\tfloat dBy = bumpScale * texture2D( bumpMap, vBumpMapUv + dSTdy ).x - Hll;\n\t\treturn vec2( dBx, dBy );\n\t}\n\tvec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy, float faceDirection ) {\n\t\tvec3 vSigmaX = normalize( dFdx( surf_pos.xyz ) );\n\t\tvec3 vSigmaY = normalize( dFdy( surf_pos.xyz ) );\n\t\tvec3 vN = surf_norm;\n\t\tvec3 R1 = cross( vSigmaY, vN );\n\t\tvec3 R2 = cross( vN, vSigmaX );\n\t\tfloat fDet = dot( vSigmaX, R1 ) * faceDirection;\n\t\tvec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 );\n\t\treturn normalize( abs( fDet ) * surf_norm - vGrad );\n\t}\n#endif"; + +var clipping_planes_fragment = "#if NUM_CLIPPING_PLANES > 0\n\tvec4 plane;\n\t#ifdef ALPHA_TO_COVERAGE\n\t\tfloat distanceToPlane, distanceGradient;\n\t\tfloat clipOpacity = 1.0;\n\t\t#pragma unroll_loop_start\n\t\tfor ( int i = 0; i < UNION_CLIPPING_PLANES; i ++ ) {\n\t\t\tplane = clippingPlanes[ i ];\n\t\t\tdistanceToPlane = - dot( vClipPosition, plane.xyz ) + plane.w;\n\t\t\tdistanceGradient = fwidth( distanceToPlane ) / 2.0;\n\t\t\tclipOpacity *= smoothstep( - distanceGradient, distanceGradient, distanceToPlane );\n\t\t\tif ( clipOpacity == 0.0 ) discard;\n\t\t}\n\t\t#pragma unroll_loop_end\n\t\t#if UNION_CLIPPING_PLANES < NUM_CLIPPING_PLANES\n\t\t\tfloat unionClipOpacity = 1.0;\n\t\t\t#pragma unroll_loop_start\n\t\t\tfor ( int i = UNION_CLIPPING_PLANES; i < NUM_CLIPPING_PLANES; i ++ ) {\n\t\t\t\tplane = clippingPlanes[ i ];\n\t\t\t\tdistanceToPlane = - dot( vClipPosition, plane.xyz ) + plane.w;\n\t\t\t\tdistanceGradient = fwidth( distanceToPlane ) / 2.0;\n\t\t\t\tunionClipOpacity *= 1.0 - smoothstep( - distanceGradient, distanceGradient, distanceToPlane );\n\t\t\t}\n\t\t\t#pragma unroll_loop_end\n\t\t\tclipOpacity *= 1.0 - unionClipOpacity;\n\t\t#endif\n\t\tdiffuseColor.a *= clipOpacity;\n\t\tif ( diffuseColor.a == 0.0 ) discard;\n\t#else\n\t\t#pragma unroll_loop_start\n\t\tfor ( int i = 0; i < UNION_CLIPPING_PLANES; i ++ ) {\n\t\t\tplane = clippingPlanes[ i ];\n\t\t\tif ( dot( vClipPosition, plane.xyz ) > plane.w ) discard;\n\t\t}\n\t\t#pragma unroll_loop_end\n\t\t#if UNION_CLIPPING_PLANES < NUM_CLIPPING_PLANES\n\t\t\tbool clipped = true;\n\t\t\t#pragma unroll_loop_start\n\t\t\tfor ( int i = UNION_CLIPPING_PLANES; i < NUM_CLIPPING_PLANES; i ++ ) {\n\t\t\t\tplane = clippingPlanes[ i ];\n\t\t\t\tclipped = ( dot( vClipPosition, plane.xyz ) > plane.w ) && clipped;\n\t\t\t}\n\t\t\t#pragma unroll_loop_end\n\t\t\tif ( clipped ) discard;\n\t\t#endif\n\t#endif\n#endif"; + +var clipping_planes_pars_fragment = "#if NUM_CLIPPING_PLANES > 0\n\tvarying vec3 vClipPosition;\n\tuniform vec4 clippingPlanes[ NUM_CLIPPING_PLANES ];\n#endif"; + +var clipping_planes_pars_vertex = "#if NUM_CLIPPING_PLANES > 0\n\tvarying vec3 vClipPosition;\n#endif"; + +var clipping_planes_vertex = "#if NUM_CLIPPING_PLANES > 0\n\tvClipPosition = - mvPosition.xyz;\n#endif"; + +var color_fragment = "#if defined( USE_COLOR_ALPHA )\n\tdiffuseColor *= vColor;\n#elif defined( USE_COLOR )\n\tdiffuseColor.rgb *= vColor;\n#endif"; + +var color_pars_fragment = "#if defined( USE_COLOR_ALPHA )\n\tvarying vec4 vColor;\n#elif defined( USE_COLOR )\n\tvarying vec3 vColor;\n#endif"; + +var color_pars_vertex = "#if defined( USE_COLOR_ALPHA )\n\tvarying vec4 vColor;\n#elif defined( USE_COLOR ) || defined( USE_INSTANCING_COLOR )\n\tvarying vec3 vColor;\n#endif"; + +var color_vertex = "#if defined( USE_COLOR_ALPHA )\n\tvColor = vec4( 1.0 );\n#elif defined( USE_COLOR ) || defined( USE_INSTANCING_COLOR )\n\tvColor = vec3( 1.0 );\n#endif\n#ifdef USE_COLOR\n\tvColor *= color;\n#endif\n#ifdef USE_INSTANCING_COLOR\n\tvColor.xyz *= instanceColor.xyz;\n#endif"; + +var common = "#define PI 3.141592653589793\n#define PI2 6.283185307179586\n#define PI_HALF 1.5707963267948966\n#define RECIPROCAL_PI 0.3183098861837907\n#define RECIPROCAL_PI2 0.15915494309189535\n#define EPSILON 1e-6\n#ifndef saturate\n#define saturate( a ) clamp( a, 0.0, 1.0 )\n#endif\n#define whiteComplement( a ) ( 1.0 - saturate( a ) )\nfloat pow2( const in float x ) { return x*x; }\nvec3 pow2( const in vec3 x ) { return x*x; }\nfloat pow3( const in float x ) { return x*x*x; }\nfloat pow4( const in float x ) { float x2 = x*x; return x2*x2; }\nfloat max3( const in vec3 v ) { return max( max( v.x, v.y ), v.z ); }\nfloat average( const in vec3 v ) { return dot( v, vec3( 0.3333333 ) ); }\nhighp float rand( const in vec2 uv ) {\n\tconst highp float a = 12.9898, b = 78.233, c = 43758.5453;\n\thighp float dt = dot( uv.xy, vec2( a,b ) ), sn = mod( dt, PI );\n\treturn fract( sin( sn ) * c );\n}\n#ifdef HIGH_PRECISION\n\tfloat precisionSafeLength( vec3 v ) { return length( v ); }\n#else\n\tfloat precisionSafeLength( vec3 v ) {\n\t\tfloat maxComponent = max3( abs( v ) );\n\t\treturn length( v / maxComponent ) * maxComponent;\n\t}\n#endif\nstruct IncidentLight {\n\tvec3 color;\n\tvec3 direction;\n\tbool visible;\n};\nstruct ReflectedLight {\n\tvec3 directDiffuse;\n\tvec3 directSpecular;\n\tvec3 indirectDiffuse;\n\tvec3 indirectSpecular;\n};\n#ifdef USE_ALPHAHASH\n\tvarying vec3 vPosition;\n#endif\nvec3 transformDirection( in vec3 dir, in mat4 matrix ) {\n\treturn normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );\n}\nvec3 inverseTransformDirection( in vec3 dir, in mat4 matrix ) {\n\treturn normalize( ( vec4( dir, 0.0 ) * matrix ).xyz );\n}\nmat3 transposeMat3( const in mat3 m ) {\n\tmat3 tmp;\n\ttmp[ 0 ] = vec3( m[ 0 ].x, m[ 1 ].x, m[ 2 ].x );\n\ttmp[ 1 ] = vec3( m[ 0 ].y, m[ 1 ].y, m[ 2 ].y );\n\ttmp[ 2 ] = vec3( m[ 0 ].z, m[ 1 ].z, m[ 2 ].z );\n\treturn tmp;\n}\nfloat luminance( const in vec3 rgb ) {\n\tconst vec3 weights = vec3( 0.2126729, 0.7151522, 0.0721750 );\n\treturn dot( weights, rgb );\n}\nbool isPerspectiveMatrix( mat4 m ) {\n\treturn m[ 2 ][ 3 ] == - 1.0;\n}\nvec2 equirectUv( in vec3 dir ) {\n\tfloat u = atan( dir.z, dir.x ) * RECIPROCAL_PI2 + 0.5;\n\tfloat v = asin( clamp( dir.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;\n\treturn vec2( u, v );\n}\nvec3 BRDF_Lambert( const in vec3 diffuseColor ) {\n\treturn RECIPROCAL_PI * diffuseColor;\n}\nvec3 F_Schlick( const in vec3 f0, const in float f90, const in float dotVH ) {\n\tfloat fresnel = exp2( ( - 5.55473 * dotVH - 6.98316 ) * dotVH );\n\treturn f0 * ( 1.0 - fresnel ) + ( f90 * fresnel );\n}\nfloat F_Schlick( const in float f0, const in float f90, const in float dotVH ) {\n\tfloat fresnel = exp2( ( - 5.55473 * dotVH - 6.98316 ) * dotVH );\n\treturn f0 * ( 1.0 - fresnel ) + ( f90 * fresnel );\n} // validated"; + +var cube_uv_reflection_fragment = "#ifdef ENVMAP_TYPE_CUBE_UV\n\t#define cubeUV_minMipLevel 4.0\n\t#define cubeUV_minTileSize 16.0\n\tfloat getFace( vec3 direction ) {\n\t\tvec3 absDirection = abs( direction );\n\t\tfloat face = - 1.0;\n\t\tif ( absDirection.x > absDirection.z ) {\n\t\t\tif ( absDirection.x > absDirection.y )\n\t\t\t\tface = direction.x > 0.0 ? 0.0 : 3.0;\n\t\t\telse\n\t\t\t\tface = direction.y > 0.0 ? 1.0 : 4.0;\n\t\t} else {\n\t\t\tif ( absDirection.z > absDirection.y )\n\t\t\t\tface = direction.z > 0.0 ? 2.0 : 5.0;\n\t\t\telse\n\t\t\t\tface = direction.y > 0.0 ? 1.0 : 4.0;\n\t\t}\n\t\treturn face;\n\t}\n\tvec2 getUV( vec3 direction, float face ) {\n\t\tvec2 uv;\n\t\tif ( face == 0.0 ) {\n\t\t\tuv = vec2( direction.z, direction.y ) / abs( direction.x );\n\t\t} else if ( face == 1.0 ) {\n\t\t\tuv = vec2( - direction.x, - direction.z ) / abs( direction.y );\n\t\t} else if ( face == 2.0 ) {\n\t\t\tuv = vec2( - direction.x, direction.y ) / abs( direction.z );\n\t\t} else if ( face == 3.0 ) {\n\t\t\tuv = vec2( - direction.z, direction.y ) / abs( direction.x );\n\t\t} else if ( face == 4.0 ) {\n\t\t\tuv = vec2( - direction.x, direction.z ) / abs( direction.y );\n\t\t} else {\n\t\t\tuv = vec2( direction.x, direction.y ) / abs( direction.z );\n\t\t}\n\t\treturn 0.5 * ( uv + 1.0 );\n\t}\n\tvec3 bilinearCubeUV( sampler2D envMap, vec3 direction, float mipInt ) {\n\t\tfloat face = getFace( direction );\n\t\tfloat filterInt = max( cubeUV_minMipLevel - mipInt, 0.0 );\n\t\tmipInt = max( mipInt, cubeUV_minMipLevel );\n\t\tfloat faceSize = exp2( mipInt );\n\t\thighp vec2 uv = getUV( direction, face ) * ( faceSize - 2.0 ) + 1.0;\n\t\tif ( face > 2.0 ) {\n\t\t\tuv.y += faceSize;\n\t\t\tface -= 3.0;\n\t\t}\n\t\tuv.x += face * faceSize;\n\t\tuv.x += filterInt * 3.0 * cubeUV_minTileSize;\n\t\tuv.y += 4.0 * ( exp2( CUBEUV_MAX_MIP ) - faceSize );\n\t\tuv.x *= CUBEUV_TEXEL_WIDTH;\n\t\tuv.y *= CUBEUV_TEXEL_HEIGHT;\n\t\t#ifdef texture2DGradEXT\n\t\t\treturn texture2DGradEXT( envMap, uv, vec2( 0.0 ), vec2( 0.0 ) ).rgb;\n\t\t#else\n\t\t\treturn texture2D( envMap, uv ).rgb;\n\t\t#endif\n\t}\n\t#define cubeUV_r0 1.0\n\t#define cubeUV_m0 - 2.0\n\t#define cubeUV_r1 0.8\n\t#define cubeUV_m1 - 1.0\n\t#define cubeUV_r4 0.4\n\t#define cubeUV_m4 2.0\n\t#define cubeUV_r5 0.305\n\t#define cubeUV_m5 3.0\n\t#define cubeUV_r6 0.21\n\t#define cubeUV_m6 4.0\n\tfloat roughnessToMip( float roughness ) {\n\t\tfloat mip = 0.0;\n\t\tif ( roughness >= cubeUV_r1 ) {\n\t\t\tmip = ( cubeUV_r0 - roughness ) * ( cubeUV_m1 - cubeUV_m0 ) / ( cubeUV_r0 - cubeUV_r1 ) + cubeUV_m0;\n\t\t} else if ( roughness >= cubeUV_r4 ) {\n\t\t\tmip = ( cubeUV_r1 - roughness ) * ( cubeUV_m4 - cubeUV_m1 ) / ( cubeUV_r1 - cubeUV_r4 ) + cubeUV_m1;\n\t\t} else if ( roughness >= cubeUV_r5 ) {\n\t\t\tmip = ( cubeUV_r4 - roughness ) * ( cubeUV_m5 - cubeUV_m4 ) / ( cubeUV_r4 - cubeUV_r5 ) + cubeUV_m4;\n\t\t} else if ( roughness >= cubeUV_r6 ) {\n\t\t\tmip = ( cubeUV_r5 - roughness ) * ( cubeUV_m6 - cubeUV_m5 ) / ( cubeUV_r5 - cubeUV_r6 ) + cubeUV_m5;\n\t\t} else {\n\t\t\tmip = - 2.0 * log2( 1.16 * roughness );\t\t}\n\t\treturn mip;\n\t}\n\tvec4 textureCubeUV( sampler2D envMap, vec3 sampleDir, float roughness ) {\n\t\tfloat mip = clamp( roughnessToMip( roughness ), cubeUV_m0, CUBEUV_MAX_MIP );\n\t\tfloat mipF = fract( mip );\n\t\tfloat mipInt = floor( mip );\n\t\tvec3 color0 = bilinearCubeUV( envMap, sampleDir, mipInt );\n\t\tif ( mipF == 0.0 ) {\n\t\t\treturn vec4( color0, 1.0 );\n\t\t} else {\n\t\t\tvec3 color1 = bilinearCubeUV( envMap, sampleDir, mipInt + 1.0 );\n\t\t\treturn vec4( mix( color0, color1, mipF ), 1.0 );\n\t\t}\n\t}\n#endif"; + +var defaultnormal_vertex = "vec3 transformedNormal = objectNormal;\n#ifdef USE_TANGENT\n\tvec3 transformedTangent = objectTangent;\n#endif\n#ifdef USE_BATCHING\n\tmat3 bm = mat3( batchingMatrix );\n\ttransformedNormal /= vec3( dot( bm[ 0 ], bm[ 0 ] ), dot( bm[ 1 ], bm[ 1 ] ), dot( bm[ 2 ], bm[ 2 ] ) );\n\ttransformedNormal = bm * transformedNormal;\n\t#ifdef USE_TANGENT\n\t\ttransformedTangent = bm * transformedTangent;\n\t#endif\n#endif\n#ifdef USE_INSTANCING\n\tmat3 im = mat3( instanceMatrix );\n\ttransformedNormal /= vec3( dot( im[ 0 ], im[ 0 ] ), dot( im[ 1 ], im[ 1 ] ), dot( im[ 2 ], im[ 2 ] ) );\n\ttransformedNormal = im * transformedNormal;\n\t#ifdef USE_TANGENT\n\t\ttransformedTangent = im * transformedTangent;\n\t#endif\n#endif\ntransformedNormal = normalMatrix * transformedNormal;\n#ifdef FLIP_SIDED\n\ttransformedNormal = - transformedNormal;\n#endif\n#ifdef USE_TANGENT\n\ttransformedTangent = ( modelViewMatrix * vec4( transformedTangent, 0.0 ) ).xyz;\n\t#ifdef FLIP_SIDED\n\t\ttransformedTangent = - transformedTangent;\n\t#endif\n#endif"; + +var displacementmap_pars_vertex = "#ifdef USE_DISPLACEMENTMAP\n\tuniform sampler2D displacementMap;\n\tuniform float displacementScale;\n\tuniform float displacementBias;\n#endif"; + +var displacementmap_vertex = "#ifdef USE_DISPLACEMENTMAP\n\ttransformed += normalize( objectNormal ) * ( texture2D( displacementMap, vDisplacementMapUv ).x * displacementScale + displacementBias );\n#endif"; + +var emissivemap_fragment = "#ifdef USE_EMISSIVEMAP\n\tvec4 emissiveColor = texture2D( emissiveMap, vEmissiveMapUv );\n\ttotalEmissiveRadiance *= emissiveColor.rgb;\n#endif"; + +var emissivemap_pars_fragment = "#ifdef USE_EMISSIVEMAP\n\tuniform sampler2D emissiveMap;\n#endif"; + +var colorspace_fragment = "gl_FragColor = linearToOutputTexel( gl_FragColor );"; + +var colorspace_pars_fragment = "\nconst mat3 LINEAR_SRGB_TO_LINEAR_DISPLAY_P3 = mat3(\n\tvec3( 0.8224621, 0.177538, 0.0 ),\n\tvec3( 0.0331941, 0.9668058, 0.0 ),\n\tvec3( 0.0170827, 0.0723974, 0.9105199 )\n);\nconst mat3 LINEAR_DISPLAY_P3_TO_LINEAR_SRGB = mat3(\n\tvec3( 1.2249401, - 0.2249404, 0.0 ),\n\tvec3( - 0.0420569, 1.0420571, 0.0 ),\n\tvec3( - 0.0196376, - 0.0786361, 1.0982735 )\n);\nvec4 LinearSRGBToLinearDisplayP3( in vec4 value ) {\n\treturn vec4( value.rgb * LINEAR_SRGB_TO_LINEAR_DISPLAY_P3, value.a );\n}\nvec4 LinearDisplayP3ToLinearSRGB( in vec4 value ) {\n\treturn vec4( value.rgb * LINEAR_DISPLAY_P3_TO_LINEAR_SRGB, value.a );\n}\nvec4 LinearTransferOETF( in vec4 value ) {\n\treturn value;\n}\nvec4 sRGBTransferOETF( in vec4 value ) {\n\treturn vec4( mix( pow( value.rgb, vec3( 0.41666 ) ) * 1.055 - vec3( 0.055 ), value.rgb * 12.92, vec3( lessThanEqual( value.rgb, vec3( 0.0031308 ) ) ) ), value.a );\n}\nvec4 LinearToLinear( in vec4 value ) {\n\treturn value;\n}\nvec4 LinearTosRGB( in vec4 value ) {\n\treturn sRGBTransferOETF( value );\n}"; + +var envmap_fragment = "#ifdef USE_ENVMAP\n\t#ifdef ENV_WORLDPOS\n\t\tvec3 cameraToFrag;\n\t\tif ( isOrthographic ) {\n\t\t\tcameraToFrag = normalize( vec3( - viewMatrix[ 0 ][ 2 ], - viewMatrix[ 1 ][ 2 ], - viewMatrix[ 2 ][ 2 ] ) );\n\t\t} else {\n\t\t\tcameraToFrag = normalize( vWorldPosition - cameraPosition );\n\t\t}\n\t\tvec3 worldNormal = inverseTransformDirection( normal, viewMatrix );\n\t\t#ifdef ENVMAP_MODE_REFLECTION\n\t\t\tvec3 reflectVec = reflect( cameraToFrag, worldNormal );\n\t\t#else\n\t\t\tvec3 reflectVec = refract( cameraToFrag, worldNormal, refractionRatio );\n\t\t#endif\n\t#else\n\t\tvec3 reflectVec = vReflect;\n\t#endif\n\t#ifdef ENVMAP_TYPE_CUBE\n\t\tvec4 envColor = textureCube( envMap, envMapRotation * vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );\n\t#else\n\t\tvec4 envColor = vec4( 0.0 );\n\t#endif\n\t#ifdef ENVMAP_BLENDING_MULTIPLY\n\t\toutgoingLight = mix( outgoingLight, outgoingLight * envColor.xyz, specularStrength * reflectivity );\n\t#elif defined( ENVMAP_BLENDING_MIX )\n\t\toutgoingLight = mix( outgoingLight, envColor.xyz, specularStrength * reflectivity );\n\t#elif defined( ENVMAP_BLENDING_ADD )\n\t\toutgoingLight += envColor.xyz * specularStrength * reflectivity;\n\t#endif\n#endif"; + +var envmap_common_pars_fragment = "#ifdef USE_ENVMAP\n\tuniform float envMapIntensity;\n\tuniform float flipEnvMap;\n\tuniform mat3 envMapRotation;\n\t#ifdef ENVMAP_TYPE_CUBE\n\t\tuniform samplerCube envMap;\n\t#else\n\t\tuniform sampler2D envMap;\n\t#endif\n\t\n#endif"; + +var envmap_pars_fragment = "#ifdef USE_ENVMAP\n\tuniform float reflectivity;\n\t#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG ) || defined( LAMBERT )\n\t\t#define ENV_WORLDPOS\n\t#endif\n\t#ifdef ENV_WORLDPOS\n\t\tvarying vec3 vWorldPosition;\n\t\tuniform float refractionRatio;\n\t#else\n\t\tvarying vec3 vReflect;\n\t#endif\n#endif"; + +var envmap_pars_vertex = "#ifdef USE_ENVMAP\n\t#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG ) || defined( LAMBERT )\n\t\t#define ENV_WORLDPOS\n\t#endif\n\t#ifdef ENV_WORLDPOS\n\t\t\n\t\tvarying vec3 vWorldPosition;\n\t#else\n\t\tvarying vec3 vReflect;\n\t\tuniform float refractionRatio;\n\t#endif\n#endif"; + +var envmap_vertex = "#ifdef USE_ENVMAP\n\t#ifdef ENV_WORLDPOS\n\t\tvWorldPosition = worldPosition.xyz;\n\t#else\n\t\tvec3 cameraToVertex;\n\t\tif ( isOrthographic ) {\n\t\t\tcameraToVertex = normalize( vec3( - viewMatrix[ 0 ][ 2 ], - viewMatrix[ 1 ][ 2 ], - viewMatrix[ 2 ][ 2 ] ) );\n\t\t} else {\n\t\t\tcameraToVertex = normalize( worldPosition.xyz - cameraPosition );\n\t\t}\n\t\tvec3 worldNormal = inverseTransformDirection( transformedNormal, viewMatrix );\n\t\t#ifdef ENVMAP_MODE_REFLECTION\n\t\t\tvReflect = reflect( cameraToVertex, worldNormal );\n\t\t#else\n\t\t\tvReflect = refract( cameraToVertex, worldNormal, refractionRatio );\n\t\t#endif\n\t#endif\n#endif"; + +var fog_vertex = "#ifdef USE_FOG\n\tvFogDepth = - mvPosition.z;\n#endif"; + +var fog_pars_vertex = "#ifdef USE_FOG\n\tvarying float vFogDepth;\n#endif"; + +var fog_fragment = "#ifdef USE_FOG\n\t#ifdef FOG_EXP2\n\t\tfloat fogFactor = 1.0 - exp( - fogDensity * fogDensity * vFogDepth * vFogDepth );\n\t#else\n\t\tfloat fogFactor = smoothstep( fogNear, fogFar, vFogDepth );\n\t#endif\n\tgl_FragColor.rgb = mix( gl_FragColor.rgb, fogColor, fogFactor );\n#endif"; + +var fog_pars_fragment = "#ifdef USE_FOG\n\tuniform vec3 fogColor;\n\tvarying float vFogDepth;\n\t#ifdef FOG_EXP2\n\t\tuniform float fogDensity;\n\t#else\n\t\tuniform float fogNear;\n\t\tuniform float fogFar;\n\t#endif\n#endif"; + +var gradientmap_pars_fragment = "#ifdef USE_GRADIENTMAP\n\tuniform sampler2D gradientMap;\n#endif\nvec3 getGradientIrradiance( vec3 normal, vec3 lightDirection ) {\n\tfloat dotNL = dot( normal, lightDirection );\n\tvec2 coord = vec2( dotNL * 0.5 + 0.5, 0.0 );\n\t#ifdef USE_GRADIENTMAP\n\t\treturn vec3( texture2D( gradientMap, coord ).r );\n\t#else\n\t\tvec2 fw = fwidth( coord ) * 0.5;\n\t\treturn mix( vec3( 0.7 ), vec3( 1.0 ), smoothstep( 0.7 - fw.x, 0.7 + fw.x, coord.x ) );\n\t#endif\n}"; + +var lightmap_pars_fragment = "#ifdef USE_LIGHTMAP\n\tuniform sampler2D lightMap;\n\tuniform float lightMapIntensity;\n#endif"; + +var lights_lambert_fragment = "LambertMaterial material;\nmaterial.diffuseColor = diffuseColor.rgb;\nmaterial.specularStrength = specularStrength;"; + +var lights_lambert_pars_fragment = "varying vec3 vViewPosition;\nstruct LambertMaterial {\n\tvec3 diffuseColor;\n\tfloat specularStrength;\n};\nvoid RE_Direct_Lambert( const in IncidentLight directLight, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in LambertMaterial material, inout ReflectedLight reflectedLight ) {\n\tfloat dotNL = saturate( dot( geometryNormal, directLight.direction ) );\n\tvec3 irradiance = dotNL * directLight.color;\n\treflectedLight.directDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );\n}\nvoid RE_IndirectDiffuse_Lambert( const in vec3 irradiance, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in LambertMaterial material, inout ReflectedLight reflectedLight ) {\n\treflectedLight.indirectDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );\n}\n#define RE_Direct\t\t\t\tRE_Direct_Lambert\n#define RE_IndirectDiffuse\t\tRE_IndirectDiffuse_Lambert"; + +var lights_pars_begin = "uniform bool receiveShadow;\nuniform vec3 ambientLightColor;\n#if defined( USE_LIGHT_PROBES )\n\tuniform vec3 lightProbe[ 9 ];\n#endif\nvec3 shGetIrradianceAt( in vec3 normal, in vec3 shCoefficients[ 9 ] ) {\n\tfloat x = normal.x, y = normal.y, z = normal.z;\n\tvec3 result = shCoefficients[ 0 ] * 0.886227;\n\tresult += shCoefficients[ 1 ] * 2.0 * 0.511664 * y;\n\tresult += shCoefficients[ 2 ] * 2.0 * 0.511664 * z;\n\tresult += shCoefficients[ 3 ] * 2.0 * 0.511664 * x;\n\tresult += shCoefficients[ 4 ] * 2.0 * 0.429043 * x * y;\n\tresult += shCoefficients[ 5 ] * 2.0 * 0.429043 * y * z;\n\tresult += shCoefficients[ 6 ] * ( 0.743125 * z * z - 0.247708 );\n\tresult += shCoefficients[ 7 ] * 2.0 * 0.429043 * x * z;\n\tresult += shCoefficients[ 8 ] * 0.429043 * ( x * x - y * y );\n\treturn result;\n}\nvec3 getLightProbeIrradiance( const in vec3 lightProbe[ 9 ], const in vec3 normal ) {\n\tvec3 worldNormal = inverseTransformDirection( normal, viewMatrix );\n\tvec3 irradiance = shGetIrradianceAt( worldNormal, lightProbe );\n\treturn irradiance;\n}\nvec3 getAmbientLightIrradiance( const in vec3 ambientLightColor ) {\n\tvec3 irradiance = ambientLightColor;\n\treturn irradiance;\n}\nfloat getDistanceAttenuation( const in float lightDistance, const in float cutoffDistance, const in float decayExponent ) {\n\t#if defined ( LEGACY_LIGHTS )\n\t\tif ( cutoffDistance > 0.0 && decayExponent > 0.0 ) {\n\t\t\treturn pow( saturate( - lightDistance / cutoffDistance + 1.0 ), decayExponent );\n\t\t}\n\t\treturn 1.0;\n\t#else\n\t\tfloat distanceFalloff = 1.0 / max( pow( lightDistance, decayExponent ), 0.01 );\n\t\tif ( cutoffDistance > 0.0 ) {\n\t\t\tdistanceFalloff *= pow2( saturate( 1.0 - pow4( lightDistance / cutoffDistance ) ) );\n\t\t}\n\t\treturn distanceFalloff;\n\t#endif\n}\nfloat getSpotAttenuation( const in float coneCosine, const in float penumbraCosine, const in float angleCosine ) {\n\treturn smoothstep( coneCosine, penumbraCosine, angleCosine );\n}\n#if NUM_DIR_LIGHTS > 0\n\tstruct DirectionalLight {\n\t\tvec3 direction;\n\t\tvec3 color;\n\t};\n\tuniform DirectionalLight directionalLights[ NUM_DIR_LIGHTS ];\n\tvoid getDirectionalLightInfo( const in DirectionalLight directionalLight, out IncidentLight light ) {\n\t\tlight.color = directionalLight.color;\n\t\tlight.direction = directionalLight.direction;\n\t\tlight.visible = true;\n\t}\n#endif\n#if NUM_POINT_LIGHTS > 0\n\tstruct PointLight {\n\t\tvec3 position;\n\t\tvec3 color;\n\t\tfloat distance;\n\t\tfloat decay;\n\t};\n\tuniform PointLight pointLights[ NUM_POINT_LIGHTS ];\n\tvoid getPointLightInfo( const in PointLight pointLight, const in vec3 geometryPosition, out IncidentLight light ) {\n\t\tvec3 lVector = pointLight.position - geometryPosition;\n\t\tlight.direction = normalize( lVector );\n\t\tfloat lightDistance = length( lVector );\n\t\tlight.color = pointLight.color;\n\t\tlight.color *= getDistanceAttenuation( lightDistance, pointLight.distance, pointLight.decay );\n\t\tlight.visible = ( light.color != vec3( 0.0 ) );\n\t}\n#endif\n#if NUM_SPOT_LIGHTS > 0\n\tstruct SpotLight {\n\t\tvec3 position;\n\t\tvec3 direction;\n\t\tvec3 color;\n\t\tfloat distance;\n\t\tfloat decay;\n\t\tfloat coneCos;\n\t\tfloat penumbraCos;\n\t};\n\tuniform SpotLight spotLights[ NUM_SPOT_LIGHTS ];\n\tvoid getSpotLightInfo( const in SpotLight spotLight, const in vec3 geometryPosition, out IncidentLight light ) {\n\t\tvec3 lVector = spotLight.position - geometryPosition;\n\t\tlight.direction = normalize( lVector );\n\t\tfloat angleCos = dot( light.direction, spotLight.direction );\n\t\tfloat spotAttenuation = getSpotAttenuation( spotLight.coneCos, spotLight.penumbraCos, angleCos );\n\t\tif ( spotAttenuation > 0.0 ) {\n\t\t\tfloat lightDistance = length( lVector );\n\t\t\tlight.color = spotLight.color * spotAttenuation;\n\t\t\tlight.color *= getDistanceAttenuation( lightDistance, spotLight.distance, spotLight.decay );\n\t\t\tlight.visible = ( light.color != vec3( 0.0 ) );\n\t\t} else {\n\t\t\tlight.color = vec3( 0.0 );\n\t\t\tlight.visible = false;\n\t\t}\n\t}\n#endif\n#if NUM_RECT_AREA_LIGHTS > 0\n\tstruct RectAreaLight {\n\t\tvec3 color;\n\t\tvec3 position;\n\t\tvec3 halfWidth;\n\t\tvec3 halfHeight;\n\t};\n\tuniform sampler2D ltc_1;\tuniform sampler2D ltc_2;\n\tuniform RectAreaLight rectAreaLights[ NUM_RECT_AREA_LIGHTS ];\n#endif\n#if NUM_HEMI_LIGHTS > 0\n\tstruct HemisphereLight {\n\t\tvec3 direction;\n\t\tvec3 skyColor;\n\t\tvec3 groundColor;\n\t};\n\tuniform HemisphereLight hemisphereLights[ NUM_HEMI_LIGHTS ];\n\tvec3 getHemisphereLightIrradiance( const in HemisphereLight hemiLight, const in vec3 normal ) {\n\t\tfloat dotNL = dot( normal, hemiLight.direction );\n\t\tfloat hemiDiffuseWeight = 0.5 * dotNL + 0.5;\n\t\tvec3 irradiance = mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight );\n\t\treturn irradiance;\n\t}\n#endif"; + +var envmap_physical_pars_fragment = "#ifdef USE_ENVMAP\n\tvec3 getIBLIrradiance( const in vec3 normal ) {\n\t\t#ifdef ENVMAP_TYPE_CUBE_UV\n\t\t\tvec3 worldNormal = inverseTransformDirection( normal, viewMatrix );\n\t\t\tvec4 envMapColor = textureCubeUV( envMap, envMapRotation * worldNormal, 1.0 );\n\t\t\treturn PI * envMapColor.rgb * envMapIntensity;\n\t\t#else\n\t\t\treturn vec3( 0.0 );\n\t\t#endif\n\t}\n\tvec3 getIBLRadiance( const in vec3 viewDir, const in vec3 normal, const in float roughness ) {\n\t\t#ifdef ENVMAP_TYPE_CUBE_UV\n\t\t\tvec3 reflectVec = reflect( - viewDir, normal );\n\t\t\treflectVec = normalize( mix( reflectVec, normal, roughness * roughness) );\n\t\t\treflectVec = inverseTransformDirection( reflectVec, viewMatrix );\n\t\t\tvec4 envMapColor = textureCubeUV( envMap, envMapRotation * reflectVec, roughness );\n\t\t\treturn envMapColor.rgb * envMapIntensity;\n\t\t#else\n\t\t\treturn vec3( 0.0 );\n\t\t#endif\n\t}\n\t#ifdef USE_ANISOTROPY\n\t\tvec3 getIBLAnisotropyRadiance( const in vec3 viewDir, const in vec3 normal, const in float roughness, const in vec3 bitangent, const in float anisotropy ) {\n\t\t\t#ifdef ENVMAP_TYPE_CUBE_UV\n\t\t\t\tvec3 bentNormal = cross( bitangent, viewDir );\n\t\t\t\tbentNormal = normalize( cross( bentNormal, bitangent ) );\n\t\t\t\tbentNormal = normalize( mix( bentNormal, normal, pow2( pow2( 1.0 - anisotropy * ( 1.0 - roughness ) ) ) ) );\n\t\t\t\treturn getIBLRadiance( viewDir, bentNormal, roughness );\n\t\t\t#else\n\t\t\t\treturn vec3( 0.0 );\n\t\t\t#endif\n\t\t}\n\t#endif\n#endif"; + +var lights_toon_fragment = "ToonMaterial material;\nmaterial.diffuseColor = diffuseColor.rgb;"; + +var lights_toon_pars_fragment = "varying vec3 vViewPosition;\nstruct ToonMaterial {\n\tvec3 diffuseColor;\n};\nvoid RE_Direct_Toon( const in IncidentLight directLight, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in ToonMaterial material, inout ReflectedLight reflectedLight ) {\n\tvec3 irradiance = getGradientIrradiance( geometryNormal, directLight.direction ) * directLight.color;\n\treflectedLight.directDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );\n}\nvoid RE_IndirectDiffuse_Toon( const in vec3 irradiance, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in ToonMaterial material, inout ReflectedLight reflectedLight ) {\n\treflectedLight.indirectDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );\n}\n#define RE_Direct\t\t\t\tRE_Direct_Toon\n#define RE_IndirectDiffuse\t\tRE_IndirectDiffuse_Toon"; + +var lights_phong_fragment = "BlinnPhongMaterial material;\nmaterial.diffuseColor = diffuseColor.rgb;\nmaterial.specularColor = specular;\nmaterial.specularShininess = shininess;\nmaterial.specularStrength = specularStrength;"; + +var lights_phong_pars_fragment = "varying vec3 vViewPosition;\nstruct BlinnPhongMaterial {\n\tvec3 diffuseColor;\n\tvec3 specularColor;\n\tfloat specularShininess;\n\tfloat specularStrength;\n};\nvoid RE_Direct_BlinnPhong( const in IncidentLight directLight, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) {\n\tfloat dotNL = saturate( dot( geometryNormal, directLight.direction ) );\n\tvec3 irradiance = dotNL * directLight.color;\n\treflectedLight.directDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );\n\treflectedLight.directSpecular += irradiance * BRDF_BlinnPhong( directLight.direction, geometryViewDir, geometryNormal, material.specularColor, material.specularShininess ) * material.specularStrength;\n}\nvoid RE_IndirectDiffuse_BlinnPhong( const in vec3 irradiance, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) {\n\treflectedLight.indirectDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );\n}\n#define RE_Direct\t\t\t\tRE_Direct_BlinnPhong\n#define RE_IndirectDiffuse\t\tRE_IndirectDiffuse_BlinnPhong"; + +var lights_physical_fragment = "PhysicalMaterial material;\nmaterial.diffuseColor = diffuseColor.rgb * ( 1.0 - metalnessFactor );\nvec3 dxy = max( abs( dFdx( nonPerturbedNormal ) ), abs( dFdy( nonPerturbedNormal ) ) );\nfloat geometryRoughness = max( max( dxy.x, dxy.y ), dxy.z );\nmaterial.roughness = max( roughnessFactor, 0.0525 );material.roughness += geometryRoughness;\nmaterial.roughness = min( material.roughness, 1.0 );\n#ifdef IOR\n\tmaterial.ior = ior;\n\t#ifdef USE_SPECULAR\n\t\tfloat specularIntensityFactor = specularIntensity;\n\t\tvec3 specularColorFactor = specularColor;\n\t\t#ifdef USE_SPECULAR_COLORMAP\n\t\t\tspecularColorFactor *= texture2D( specularColorMap, vSpecularColorMapUv ).rgb;\n\t\t#endif\n\t\t#ifdef USE_SPECULAR_INTENSITYMAP\n\t\t\tspecularIntensityFactor *= texture2D( specularIntensityMap, vSpecularIntensityMapUv ).a;\n\t\t#endif\n\t\tmaterial.specularF90 = mix( specularIntensityFactor, 1.0, metalnessFactor );\n\t#else\n\t\tfloat specularIntensityFactor = 1.0;\n\t\tvec3 specularColorFactor = vec3( 1.0 );\n\t\tmaterial.specularF90 = 1.0;\n\t#endif\n\tmaterial.specularColor = mix( min( pow2( ( material.ior - 1.0 ) / ( material.ior + 1.0 ) ) * specularColorFactor, vec3( 1.0 ) ) * specularIntensityFactor, diffuseColor.rgb, metalnessFactor );\n#else\n\tmaterial.specularColor = mix( vec3( 0.04 ), diffuseColor.rgb, metalnessFactor );\n\tmaterial.specularF90 = 1.0;\n#endif\n#ifdef USE_CLEARCOAT\n\tmaterial.clearcoat = clearcoat;\n\tmaterial.clearcoatRoughness = clearcoatRoughness;\n\tmaterial.clearcoatF0 = vec3( 0.04 );\n\tmaterial.clearcoatF90 = 1.0;\n\t#ifdef USE_CLEARCOATMAP\n\t\tmaterial.clearcoat *= texture2D( clearcoatMap, vClearcoatMapUv ).x;\n\t#endif\n\t#ifdef USE_CLEARCOAT_ROUGHNESSMAP\n\t\tmaterial.clearcoatRoughness *= texture2D( clearcoatRoughnessMap, vClearcoatRoughnessMapUv ).y;\n\t#endif\n\tmaterial.clearcoat = saturate( material.clearcoat );\tmaterial.clearcoatRoughness = max( material.clearcoatRoughness, 0.0525 );\n\tmaterial.clearcoatRoughness += geometryRoughness;\n\tmaterial.clearcoatRoughness = min( material.clearcoatRoughness, 1.0 );\n#endif\n#ifdef USE_DISPERSION\n\tmaterial.dispersion = dispersion;\n#endif\n#ifdef USE_IRIDESCENCE\n\tmaterial.iridescence = iridescence;\n\tmaterial.iridescenceIOR = iridescenceIOR;\n\t#ifdef USE_IRIDESCENCEMAP\n\t\tmaterial.iridescence *= texture2D( iridescenceMap, vIridescenceMapUv ).r;\n\t#endif\n\t#ifdef USE_IRIDESCENCE_THICKNESSMAP\n\t\tmaterial.iridescenceThickness = (iridescenceThicknessMaximum - iridescenceThicknessMinimum) * texture2D( iridescenceThicknessMap, vIridescenceThicknessMapUv ).g + iridescenceThicknessMinimum;\n\t#else\n\t\tmaterial.iridescenceThickness = iridescenceThicknessMaximum;\n\t#endif\n#endif\n#ifdef USE_SHEEN\n\tmaterial.sheenColor = sheenColor;\n\t#ifdef USE_SHEEN_COLORMAP\n\t\tmaterial.sheenColor *= texture2D( sheenColorMap, vSheenColorMapUv ).rgb;\n\t#endif\n\tmaterial.sheenRoughness = clamp( sheenRoughness, 0.07, 1.0 );\n\t#ifdef USE_SHEEN_ROUGHNESSMAP\n\t\tmaterial.sheenRoughness *= texture2D( sheenRoughnessMap, vSheenRoughnessMapUv ).a;\n\t#endif\n#endif\n#ifdef USE_ANISOTROPY\n\t#ifdef USE_ANISOTROPYMAP\n\t\tmat2 anisotropyMat = mat2( anisotropyVector.x, anisotropyVector.y, - anisotropyVector.y, anisotropyVector.x );\n\t\tvec3 anisotropyPolar = texture2D( anisotropyMap, vAnisotropyMapUv ).rgb;\n\t\tvec2 anisotropyV = anisotropyMat * normalize( 2.0 * anisotropyPolar.rg - vec2( 1.0 ) ) * anisotropyPolar.b;\n\t#else\n\t\tvec2 anisotropyV = anisotropyVector;\n\t#endif\n\tmaterial.anisotropy = length( anisotropyV );\n\tif( material.anisotropy == 0.0 ) {\n\t\tanisotropyV = vec2( 1.0, 0.0 );\n\t} else {\n\t\tanisotropyV /= material.anisotropy;\n\t\tmaterial.anisotropy = saturate( material.anisotropy );\n\t}\n\tmaterial.alphaT = mix( pow2( material.roughness ), 1.0, pow2( material.anisotropy ) );\n\tmaterial.anisotropyT = tbn[ 0 ] * anisotropyV.x + tbn[ 1 ] * anisotropyV.y;\n\tmaterial.anisotropyB = tbn[ 1 ] * anisotropyV.x - tbn[ 0 ] * anisotropyV.y;\n#endif"; + +var lights_physical_pars_fragment = "struct PhysicalMaterial {\n\tvec3 diffuseColor;\n\tfloat roughness;\n\tvec3 specularColor;\n\tfloat specularF90;\n\tfloat dispersion;\n\t#ifdef USE_CLEARCOAT\n\t\tfloat clearcoat;\n\t\tfloat clearcoatRoughness;\n\t\tvec3 clearcoatF0;\n\t\tfloat clearcoatF90;\n\t#endif\n\t#ifdef USE_IRIDESCENCE\n\t\tfloat iridescence;\n\t\tfloat iridescenceIOR;\n\t\tfloat iridescenceThickness;\n\t\tvec3 iridescenceFresnel;\n\t\tvec3 iridescenceF0;\n\t#endif\n\t#ifdef USE_SHEEN\n\t\tvec3 sheenColor;\n\t\tfloat sheenRoughness;\n\t#endif\n\t#ifdef IOR\n\t\tfloat ior;\n\t#endif\n\t#ifdef USE_TRANSMISSION\n\t\tfloat transmission;\n\t\tfloat transmissionAlpha;\n\t\tfloat thickness;\n\t\tfloat attenuationDistance;\n\t\tvec3 attenuationColor;\n\t#endif\n\t#ifdef USE_ANISOTROPY\n\t\tfloat anisotropy;\n\t\tfloat alphaT;\n\t\tvec3 anisotropyT;\n\t\tvec3 anisotropyB;\n\t#endif\n};\nvec3 clearcoatSpecularDirect = vec3( 0.0 );\nvec3 clearcoatSpecularIndirect = vec3( 0.0 );\nvec3 sheenSpecularDirect = vec3( 0.0 );\nvec3 sheenSpecularIndirect = vec3(0.0 );\nvec3 Schlick_to_F0( const in vec3 f, const in float f90, const in float dotVH ) {\n float x = clamp( 1.0 - dotVH, 0.0, 1.0 );\n float x2 = x * x;\n float x5 = clamp( x * x2 * x2, 0.0, 0.9999 );\n return ( f - vec3( f90 ) * x5 ) / ( 1.0 - x5 );\n}\nfloat V_GGX_SmithCorrelated( const in float alpha, const in float dotNL, const in float dotNV ) {\n\tfloat a2 = pow2( alpha );\n\tfloat gv = dotNL * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) );\n\tfloat gl = dotNV * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) );\n\treturn 0.5 / max( gv + gl, EPSILON );\n}\nfloat D_GGX( const in float alpha, const in float dotNH ) {\n\tfloat a2 = pow2( alpha );\n\tfloat denom = pow2( dotNH ) * ( a2 - 1.0 ) + 1.0;\n\treturn RECIPROCAL_PI * a2 / pow2( denom );\n}\n#ifdef USE_ANISOTROPY\n\tfloat V_GGX_SmithCorrelated_Anisotropic( const in float alphaT, const in float alphaB, const in float dotTV, const in float dotBV, const in float dotTL, const in float dotBL, const in float dotNV, const in float dotNL ) {\n\t\tfloat gv = dotNL * length( vec3( alphaT * dotTV, alphaB * dotBV, dotNV ) );\n\t\tfloat gl = dotNV * length( vec3( alphaT * dotTL, alphaB * dotBL, dotNL ) );\n\t\tfloat v = 0.5 / ( gv + gl );\n\t\treturn saturate(v);\n\t}\n\tfloat D_GGX_Anisotropic( const in float alphaT, const in float alphaB, const in float dotNH, const in float dotTH, const in float dotBH ) {\n\t\tfloat a2 = alphaT * alphaB;\n\t\thighp vec3 v = vec3( alphaB * dotTH, alphaT * dotBH, a2 * dotNH );\n\t\thighp float v2 = dot( v, v );\n\t\tfloat w2 = a2 / v2;\n\t\treturn RECIPROCAL_PI * a2 * pow2 ( w2 );\n\t}\n#endif\n#ifdef USE_CLEARCOAT\n\tvec3 BRDF_GGX_Clearcoat( const in vec3 lightDir, const in vec3 viewDir, const in vec3 normal, const in PhysicalMaterial material) {\n\t\tvec3 f0 = material.clearcoatF0;\n\t\tfloat f90 = material.clearcoatF90;\n\t\tfloat roughness = material.clearcoatRoughness;\n\t\tfloat alpha = pow2( roughness );\n\t\tvec3 halfDir = normalize( lightDir + viewDir );\n\t\tfloat dotNL = saturate( dot( normal, lightDir ) );\n\t\tfloat dotNV = saturate( dot( normal, viewDir ) );\n\t\tfloat dotNH = saturate( dot( normal, halfDir ) );\n\t\tfloat dotVH = saturate( dot( viewDir, halfDir ) );\n\t\tvec3 F = F_Schlick( f0, f90, dotVH );\n\t\tfloat V = V_GGX_SmithCorrelated( alpha, dotNL, dotNV );\n\t\tfloat D = D_GGX( alpha, dotNH );\n\t\treturn F * ( V * D );\n\t}\n#endif\nvec3 BRDF_GGX( const in vec3 lightDir, const in vec3 viewDir, const in vec3 normal, const in PhysicalMaterial material ) {\n\tvec3 f0 = material.specularColor;\n\tfloat f90 = material.specularF90;\n\tfloat roughness = material.roughness;\n\tfloat alpha = pow2( roughness );\n\tvec3 halfDir = normalize( lightDir + viewDir );\n\tfloat dotNL = saturate( dot( normal, lightDir ) );\n\tfloat dotNV = saturate( dot( normal, viewDir ) );\n\tfloat dotNH = saturate( dot( normal, halfDir ) );\n\tfloat dotVH = saturate( dot( viewDir, halfDir ) );\n\tvec3 F = F_Schlick( f0, f90, dotVH );\n\t#ifdef USE_IRIDESCENCE\n\t\tF = mix( F, material.iridescenceFresnel, material.iridescence );\n\t#endif\n\t#ifdef USE_ANISOTROPY\n\t\tfloat dotTL = dot( material.anisotropyT, lightDir );\n\t\tfloat dotTV = dot( material.anisotropyT, viewDir );\n\t\tfloat dotTH = dot( material.anisotropyT, halfDir );\n\t\tfloat dotBL = dot( material.anisotropyB, lightDir );\n\t\tfloat dotBV = dot( material.anisotropyB, viewDir );\n\t\tfloat dotBH = dot( material.anisotropyB, halfDir );\n\t\tfloat V = V_GGX_SmithCorrelated_Anisotropic( material.alphaT, alpha, dotTV, dotBV, dotTL, dotBL, dotNV, dotNL );\n\t\tfloat D = D_GGX_Anisotropic( material.alphaT, alpha, dotNH, dotTH, dotBH );\n\t#else\n\t\tfloat V = V_GGX_SmithCorrelated( alpha, dotNL, dotNV );\n\t\tfloat D = D_GGX( alpha, dotNH );\n\t#endif\n\treturn F * ( V * D );\n}\nvec2 LTC_Uv( const in vec3 N, const in vec3 V, const in float roughness ) {\n\tconst float LUT_SIZE = 64.0;\n\tconst float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE;\n\tconst float LUT_BIAS = 0.5 / LUT_SIZE;\n\tfloat dotNV = saturate( dot( N, V ) );\n\tvec2 uv = vec2( roughness, sqrt( 1.0 - dotNV ) );\n\tuv = uv * LUT_SCALE + LUT_BIAS;\n\treturn uv;\n}\nfloat LTC_ClippedSphereFormFactor( const in vec3 f ) {\n\tfloat l = length( f );\n\treturn max( ( l * l + f.z ) / ( l + 1.0 ), 0.0 );\n}\nvec3 LTC_EdgeVectorFormFactor( const in vec3 v1, const in vec3 v2 ) {\n\tfloat x = dot( v1, v2 );\n\tfloat y = abs( x );\n\tfloat a = 0.8543985 + ( 0.4965155 + 0.0145206 * y ) * y;\n\tfloat b = 3.4175940 + ( 4.1616724 + y ) * y;\n\tfloat v = a / b;\n\tfloat theta_sintheta = ( x > 0.0 ) ? v : 0.5 * inversesqrt( max( 1.0 - x * x, 1e-7 ) ) - v;\n\treturn cross( v1, v2 ) * theta_sintheta;\n}\nvec3 LTC_Evaluate( const in vec3 N, const in vec3 V, const in vec3 P, const in mat3 mInv, const in vec3 rectCoords[ 4 ] ) {\n\tvec3 v1 = rectCoords[ 1 ] - rectCoords[ 0 ];\n\tvec3 v2 = rectCoords[ 3 ] - rectCoords[ 0 ];\n\tvec3 lightNormal = cross( v1, v2 );\n\tif( dot( lightNormal, P - rectCoords[ 0 ] ) < 0.0 ) return vec3( 0.0 );\n\tvec3 T1, T2;\n\tT1 = normalize( V - N * dot( V, N ) );\n\tT2 = - cross( N, T1 );\n\tmat3 mat = mInv * transposeMat3( mat3( T1, T2, N ) );\n\tvec3 coords[ 4 ];\n\tcoords[ 0 ] = mat * ( rectCoords[ 0 ] - P );\n\tcoords[ 1 ] = mat * ( rectCoords[ 1 ] - P );\n\tcoords[ 2 ] = mat * ( rectCoords[ 2 ] - P );\n\tcoords[ 3 ] = mat * ( rectCoords[ 3 ] - P );\n\tcoords[ 0 ] = normalize( coords[ 0 ] );\n\tcoords[ 1 ] = normalize( coords[ 1 ] );\n\tcoords[ 2 ] = normalize( coords[ 2 ] );\n\tcoords[ 3 ] = normalize( coords[ 3 ] );\n\tvec3 vectorFormFactor = vec3( 0.0 );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 0 ], coords[ 1 ] );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 1 ], coords[ 2 ] );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 2 ], coords[ 3 ] );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 3 ], coords[ 0 ] );\n\tfloat result = LTC_ClippedSphereFormFactor( vectorFormFactor );\n\treturn vec3( result );\n}\n#if defined( USE_SHEEN )\nfloat D_Charlie( float roughness, float dotNH ) {\n\tfloat alpha = pow2( roughness );\n\tfloat invAlpha = 1.0 / alpha;\n\tfloat cos2h = dotNH * dotNH;\n\tfloat sin2h = max( 1.0 - cos2h, 0.0078125 );\n\treturn ( 2.0 + invAlpha ) * pow( sin2h, invAlpha * 0.5 ) / ( 2.0 * PI );\n}\nfloat V_Neubelt( float dotNV, float dotNL ) {\n\treturn saturate( 1.0 / ( 4.0 * ( dotNL + dotNV - dotNL * dotNV ) ) );\n}\nvec3 BRDF_Sheen( const in vec3 lightDir, const in vec3 viewDir, const in vec3 normal, vec3 sheenColor, const in float sheenRoughness ) {\n\tvec3 halfDir = normalize( lightDir + viewDir );\n\tfloat dotNL = saturate( dot( normal, lightDir ) );\n\tfloat dotNV = saturate( dot( normal, viewDir ) );\n\tfloat dotNH = saturate( dot( normal, halfDir ) );\n\tfloat D = D_Charlie( sheenRoughness, dotNH );\n\tfloat V = V_Neubelt( dotNV, dotNL );\n\treturn sheenColor * ( D * V );\n}\n#endif\nfloat IBLSheenBRDF( const in vec3 normal, const in vec3 viewDir, const in float roughness ) {\n\tfloat dotNV = saturate( dot( normal, viewDir ) );\n\tfloat r2 = roughness * roughness;\n\tfloat a = roughness < 0.25 ? -339.2 * r2 + 161.4 * roughness - 25.9 : -8.48 * r2 + 14.3 * roughness - 9.95;\n\tfloat b = roughness < 0.25 ? 44.0 * r2 - 23.7 * roughness + 3.26 : 1.97 * r2 - 3.27 * roughness + 0.72;\n\tfloat DG = exp( a * dotNV + b ) + ( roughness < 0.25 ? 0.0 : 0.1 * ( roughness - 0.25 ) );\n\treturn saturate( DG * RECIPROCAL_PI );\n}\nvec2 DFGApprox( const in vec3 normal, const in vec3 viewDir, const in float roughness ) {\n\tfloat dotNV = saturate( dot( normal, viewDir ) );\n\tconst vec4 c0 = vec4( - 1, - 0.0275, - 0.572, 0.022 );\n\tconst vec4 c1 = vec4( 1, 0.0425, 1.04, - 0.04 );\n\tvec4 r = roughness * c0 + c1;\n\tfloat a004 = min( r.x * r.x, exp2( - 9.28 * dotNV ) ) * r.x + r.y;\n\tvec2 fab = vec2( - 1.04, 1.04 ) * a004 + r.zw;\n\treturn fab;\n}\nvec3 EnvironmentBRDF( const in vec3 normal, const in vec3 viewDir, const in vec3 specularColor, const in float specularF90, const in float roughness ) {\n\tvec2 fab = DFGApprox( normal, viewDir, roughness );\n\treturn specularColor * fab.x + specularF90 * fab.y;\n}\n#ifdef USE_IRIDESCENCE\nvoid computeMultiscatteringIridescence( const in vec3 normal, const in vec3 viewDir, const in vec3 specularColor, const in float specularF90, const in float iridescence, const in vec3 iridescenceF0, const in float roughness, inout vec3 singleScatter, inout vec3 multiScatter ) {\n#else\nvoid computeMultiscattering( const in vec3 normal, const in vec3 viewDir, const in vec3 specularColor, const in float specularF90, const in float roughness, inout vec3 singleScatter, inout vec3 multiScatter ) {\n#endif\n\tvec2 fab = DFGApprox( normal, viewDir, roughness );\n\t#ifdef USE_IRIDESCENCE\n\t\tvec3 Fr = mix( specularColor, iridescenceF0, iridescence );\n\t#else\n\t\tvec3 Fr = specularColor;\n\t#endif\n\tvec3 FssEss = Fr * fab.x + specularF90 * fab.y;\n\tfloat Ess = fab.x + fab.y;\n\tfloat Ems = 1.0 - Ess;\n\tvec3 Favg = Fr + ( 1.0 - Fr ) * 0.047619;\tvec3 Fms = FssEss * Favg / ( 1.0 - Ems * Favg );\n\tsingleScatter += FssEss;\n\tmultiScatter += Fms * Ems;\n}\n#if NUM_RECT_AREA_LIGHTS > 0\n\tvoid RE_Direct_RectArea_Physical( const in RectAreaLight rectAreaLight, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {\n\t\tvec3 normal = geometryNormal;\n\t\tvec3 viewDir = geometryViewDir;\n\t\tvec3 position = geometryPosition;\n\t\tvec3 lightPos = rectAreaLight.position;\n\t\tvec3 halfWidth = rectAreaLight.halfWidth;\n\t\tvec3 halfHeight = rectAreaLight.halfHeight;\n\t\tvec3 lightColor = rectAreaLight.color;\n\t\tfloat roughness = material.roughness;\n\t\tvec3 rectCoords[ 4 ];\n\t\trectCoords[ 0 ] = lightPos + halfWidth - halfHeight;\t\trectCoords[ 1 ] = lightPos - halfWidth - halfHeight;\n\t\trectCoords[ 2 ] = lightPos - halfWidth + halfHeight;\n\t\trectCoords[ 3 ] = lightPos + halfWidth + halfHeight;\n\t\tvec2 uv = LTC_Uv( normal, viewDir, roughness );\n\t\tvec4 t1 = texture2D( ltc_1, uv );\n\t\tvec4 t2 = texture2D( ltc_2, uv );\n\t\tmat3 mInv = mat3(\n\t\t\tvec3( t1.x, 0, t1.y ),\n\t\t\tvec3( 0, 1, 0 ),\n\t\t\tvec3( t1.z, 0, t1.w )\n\t\t);\n\t\tvec3 fresnel = ( material.specularColor * t2.x + ( vec3( 1.0 ) - material.specularColor ) * t2.y );\n\t\treflectedLight.directSpecular += lightColor * fresnel * LTC_Evaluate( normal, viewDir, position, mInv, rectCoords );\n\t\treflectedLight.directDiffuse += lightColor * material.diffuseColor * LTC_Evaluate( normal, viewDir, position, mat3( 1.0 ), rectCoords );\n\t}\n#endif\nvoid RE_Direct_Physical( const in IncidentLight directLight, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {\n\tfloat dotNL = saturate( dot( geometryNormal, directLight.direction ) );\n\tvec3 irradiance = dotNL * directLight.color;\n\t#ifdef USE_CLEARCOAT\n\t\tfloat dotNLcc = saturate( dot( geometryClearcoatNormal, directLight.direction ) );\n\t\tvec3 ccIrradiance = dotNLcc * directLight.color;\n\t\tclearcoatSpecularDirect += ccIrradiance * BRDF_GGX_Clearcoat( directLight.direction, geometryViewDir, geometryClearcoatNormal, material );\n\t#endif\n\t#ifdef USE_SHEEN\n\t\tsheenSpecularDirect += irradiance * BRDF_Sheen( directLight.direction, geometryViewDir, geometryNormal, material.sheenColor, material.sheenRoughness );\n\t#endif\n\treflectedLight.directSpecular += irradiance * BRDF_GGX( directLight.direction, geometryViewDir, geometryNormal, material );\n\treflectedLight.directDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );\n}\nvoid RE_IndirectDiffuse_Physical( const in vec3 irradiance, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {\n\treflectedLight.indirectDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );\n}\nvoid RE_IndirectSpecular_Physical( const in vec3 radiance, const in vec3 irradiance, const in vec3 clearcoatRadiance, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in PhysicalMaterial material, inout ReflectedLight reflectedLight) {\n\t#ifdef USE_CLEARCOAT\n\t\tclearcoatSpecularIndirect += clearcoatRadiance * EnvironmentBRDF( geometryClearcoatNormal, geometryViewDir, material.clearcoatF0, material.clearcoatF90, material.clearcoatRoughness );\n\t#endif\n\t#ifdef USE_SHEEN\n\t\tsheenSpecularIndirect += irradiance * material.sheenColor * IBLSheenBRDF( geometryNormal, geometryViewDir, material.sheenRoughness );\n\t#endif\n\tvec3 singleScattering = vec3( 0.0 );\n\tvec3 multiScattering = vec3( 0.0 );\n\tvec3 cosineWeightedIrradiance = irradiance * RECIPROCAL_PI;\n\t#ifdef USE_IRIDESCENCE\n\t\tcomputeMultiscatteringIridescence( geometryNormal, geometryViewDir, material.specularColor, material.specularF90, material.iridescence, material.iridescenceFresnel, material.roughness, singleScattering, multiScattering );\n\t#else\n\t\tcomputeMultiscattering( geometryNormal, geometryViewDir, material.specularColor, material.specularF90, material.roughness, singleScattering, multiScattering );\n\t#endif\n\tvec3 totalScattering = singleScattering + multiScattering;\n\tvec3 diffuse = material.diffuseColor * ( 1.0 - max( max( totalScattering.r, totalScattering.g ), totalScattering.b ) );\n\treflectedLight.indirectSpecular += radiance * singleScattering;\n\treflectedLight.indirectSpecular += multiScattering * cosineWeightedIrradiance;\n\treflectedLight.indirectDiffuse += diffuse * cosineWeightedIrradiance;\n}\n#define RE_Direct\t\t\t\tRE_Direct_Physical\n#define RE_Direct_RectArea\t\tRE_Direct_RectArea_Physical\n#define RE_IndirectDiffuse\t\tRE_IndirectDiffuse_Physical\n#define RE_IndirectSpecular\t\tRE_IndirectSpecular_Physical\nfloat computeSpecularOcclusion( const in float dotNV, const in float ambientOcclusion, const in float roughness ) {\n\treturn saturate( pow( dotNV + ambientOcclusion, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + ambientOcclusion );\n}"; + +var lights_fragment_begin = "\nvec3 geometryPosition = - vViewPosition;\nvec3 geometryNormal = normal;\nvec3 geometryViewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );\nvec3 geometryClearcoatNormal = vec3( 0.0 );\n#ifdef USE_CLEARCOAT\n\tgeometryClearcoatNormal = clearcoatNormal;\n#endif\n#ifdef USE_IRIDESCENCE\n\tfloat dotNVi = saturate( dot( normal, geometryViewDir ) );\n\tif ( material.iridescenceThickness == 0.0 ) {\n\t\tmaterial.iridescence = 0.0;\n\t} else {\n\t\tmaterial.iridescence = saturate( material.iridescence );\n\t}\n\tif ( material.iridescence > 0.0 ) {\n\t\tmaterial.iridescenceFresnel = evalIridescence( 1.0, material.iridescenceIOR, dotNVi, material.iridescenceThickness, material.specularColor );\n\t\tmaterial.iridescenceF0 = Schlick_to_F0( material.iridescenceFresnel, 1.0, dotNVi );\n\t}\n#endif\nIncidentLight directLight;\n#if ( NUM_POINT_LIGHTS > 0 ) && defined( RE_Direct )\n\tPointLight pointLight;\n\t#if defined( USE_SHADOWMAP ) && NUM_POINT_LIGHT_SHADOWS > 0\n\tPointLightShadow pointLightShadow;\n\t#endif\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {\n\t\tpointLight = pointLights[ i ];\n\t\tgetPointLightInfo( pointLight, geometryPosition, directLight );\n\t\t#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_POINT_LIGHT_SHADOWS )\n\t\tpointLightShadow = pointLightShadows[ i ];\n\t\tdirectLight.color *= ( directLight.visible && receiveShadow ) ? getPointShadow( pointShadowMap[ i ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ i ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;\n\t\t#endif\n\t\tRE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if ( NUM_SPOT_LIGHTS > 0 ) && defined( RE_Direct )\n\tSpotLight spotLight;\n\tvec4 spotColor;\n\tvec3 spotLightCoord;\n\tbool inSpotLightMap;\n\t#if defined( USE_SHADOWMAP ) && NUM_SPOT_LIGHT_SHADOWS > 0\n\tSpotLightShadow spotLightShadow;\n\t#endif\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {\n\t\tspotLight = spotLights[ i ];\n\t\tgetSpotLightInfo( spotLight, geometryPosition, directLight );\n\t\t#if ( UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS_WITH_MAPS )\n\t\t#define SPOT_LIGHT_MAP_INDEX UNROLLED_LOOP_INDEX\n\t\t#elif ( UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS )\n\t\t#define SPOT_LIGHT_MAP_INDEX NUM_SPOT_LIGHT_MAPS\n\t\t#else\n\t\t#define SPOT_LIGHT_MAP_INDEX ( UNROLLED_LOOP_INDEX - NUM_SPOT_LIGHT_SHADOWS + NUM_SPOT_LIGHT_SHADOWS_WITH_MAPS )\n\t\t#endif\n\t\t#if ( SPOT_LIGHT_MAP_INDEX < NUM_SPOT_LIGHT_MAPS )\n\t\t\tspotLightCoord = vSpotLightCoord[ i ].xyz / vSpotLightCoord[ i ].w;\n\t\t\tinSpotLightMap = all( lessThan( abs( spotLightCoord * 2. - 1. ), vec3( 1.0 ) ) );\n\t\t\tspotColor = texture2D( spotLightMap[ SPOT_LIGHT_MAP_INDEX ], spotLightCoord.xy );\n\t\t\tdirectLight.color = inSpotLightMap ? directLight.color * spotColor.rgb : directLight.color;\n\t\t#endif\n\t\t#undef SPOT_LIGHT_MAP_INDEX\n\t\t#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS )\n\t\tspotLightShadow = spotLightShadows[ i ];\n\t\tdirectLight.color *= ( directLight.visible && receiveShadow ) ? getShadow( spotShadowMap[ i ], spotLightShadow.shadowMapSize, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotLightCoord[ i ] ) : 1.0;\n\t\t#endif\n\t\tRE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if ( NUM_DIR_LIGHTS > 0 ) && defined( RE_Direct )\n\tDirectionalLight directionalLight;\n\t#if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0\n\tDirectionalLightShadow directionalLightShadow;\n\t#endif\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {\n\t\tdirectionalLight = directionalLights[ i ];\n\t\tgetDirectionalLightInfo( directionalLight, directLight );\n\t\t#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )\n\t\tdirectionalLightShadow = directionalLightShadows[ i ];\n\t\tdirectLight.color *= ( directLight.visible && receiveShadow ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;\n\t\t#endif\n\t\tRE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if ( NUM_RECT_AREA_LIGHTS > 0 ) && defined( RE_Direct_RectArea )\n\tRectAreaLight rectAreaLight;\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_RECT_AREA_LIGHTS; i ++ ) {\n\t\trectAreaLight = rectAreaLights[ i ];\n\t\tRE_Direct_RectArea( rectAreaLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if defined( RE_IndirectDiffuse )\n\tvec3 iblIrradiance = vec3( 0.0 );\n\tvec3 irradiance = getAmbientLightIrradiance( ambientLightColor );\n\t#if defined( USE_LIGHT_PROBES )\n\t\tirradiance += getLightProbeIrradiance( lightProbe, geometryNormal );\n\t#endif\n\t#if ( NUM_HEMI_LIGHTS > 0 )\n\t\t#pragma unroll_loop_start\n\t\tfor ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {\n\t\t\tirradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometryNormal );\n\t\t}\n\t\t#pragma unroll_loop_end\n\t#endif\n#endif\n#if defined( RE_IndirectSpecular )\n\tvec3 radiance = vec3( 0.0 );\n\tvec3 clearcoatRadiance = vec3( 0.0 );\n#endif"; + +var lights_fragment_maps = "#if defined( RE_IndirectDiffuse )\n\t#ifdef USE_LIGHTMAP\n\t\tvec4 lightMapTexel = texture2D( lightMap, vLightMapUv );\n\t\tvec3 lightMapIrradiance = lightMapTexel.rgb * lightMapIntensity;\n\t\tirradiance += lightMapIrradiance;\n\t#endif\n\t#if defined( USE_ENVMAP ) && defined( STANDARD ) && defined( ENVMAP_TYPE_CUBE_UV )\n\t\tiblIrradiance += getIBLIrradiance( geometryNormal );\n\t#endif\n#endif\n#if defined( USE_ENVMAP ) && defined( RE_IndirectSpecular )\n\t#ifdef USE_ANISOTROPY\n\t\tradiance += getIBLAnisotropyRadiance( geometryViewDir, geometryNormal, material.roughness, material.anisotropyB, material.anisotropy );\n\t#else\n\t\tradiance += getIBLRadiance( geometryViewDir, geometryNormal, material.roughness );\n\t#endif\n\t#ifdef USE_CLEARCOAT\n\t\tclearcoatRadiance += getIBLRadiance( geometryViewDir, geometryClearcoatNormal, material.clearcoatRoughness );\n\t#endif\n#endif"; + +var lights_fragment_end = "#if defined( RE_IndirectDiffuse )\n\tRE_IndirectDiffuse( irradiance, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );\n#endif\n#if defined( RE_IndirectSpecular )\n\tRE_IndirectSpecular( radiance, iblIrradiance, clearcoatRadiance, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );\n#endif"; + +var logdepthbuf_fragment = "#if defined( USE_LOGDEPTHBUF )\n\tgl_FragDepth = vIsPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;\n#endif"; + +var logdepthbuf_pars_fragment = "#if defined( USE_LOGDEPTHBUF )\n\tuniform float logDepthBufFC;\n\tvarying float vFragDepth;\n\tvarying float vIsPerspective;\n#endif"; + +var logdepthbuf_pars_vertex = "#ifdef USE_LOGDEPTHBUF\n\tvarying float vFragDepth;\n\tvarying float vIsPerspective;\n#endif"; + +var logdepthbuf_vertex = "#ifdef USE_LOGDEPTHBUF\n\tvFragDepth = 1.0 + gl_Position.w;\n\tvIsPerspective = float( isPerspectiveMatrix( projectionMatrix ) );\n#endif"; + +var map_fragment = "#ifdef USE_MAP\n\tvec4 sampledDiffuseColor = texture2D( map, vMapUv );\n\t#ifdef DECODE_VIDEO_TEXTURE\n\t\tsampledDiffuseColor = vec4( mix( pow( sampledDiffuseColor.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), sampledDiffuseColor.rgb * 0.0773993808, vec3( lessThanEqual( sampledDiffuseColor.rgb, vec3( 0.04045 ) ) ) ), sampledDiffuseColor.w );\n\t\n\t#endif\n\tdiffuseColor *= sampledDiffuseColor;\n#endif"; + +var map_pars_fragment = "#ifdef USE_MAP\n\tuniform sampler2D map;\n#endif"; + +var map_particle_fragment = "#if defined( USE_MAP ) || defined( USE_ALPHAMAP )\n\t#if defined( USE_POINTS_UV )\n\t\tvec2 uv = vUv;\n\t#else\n\t\tvec2 uv = ( uvTransform * vec3( gl_PointCoord.x, 1.0 - gl_PointCoord.y, 1 ) ).xy;\n\t#endif\n#endif\n#ifdef USE_MAP\n\tdiffuseColor *= texture2D( map, uv );\n#endif\n#ifdef USE_ALPHAMAP\n\tdiffuseColor.a *= texture2D( alphaMap, uv ).g;\n#endif"; + +var map_particle_pars_fragment = "#if defined( USE_POINTS_UV )\n\tvarying vec2 vUv;\n#else\n\t#if defined( USE_MAP ) || defined( USE_ALPHAMAP )\n\t\tuniform mat3 uvTransform;\n\t#endif\n#endif\n#ifdef USE_MAP\n\tuniform sampler2D map;\n#endif\n#ifdef USE_ALPHAMAP\n\tuniform sampler2D alphaMap;\n#endif"; + +var metalnessmap_fragment = "float metalnessFactor = metalness;\n#ifdef USE_METALNESSMAP\n\tvec4 texelMetalness = texture2D( metalnessMap, vMetalnessMapUv );\n\tmetalnessFactor *= texelMetalness.b;\n#endif"; + +var metalnessmap_pars_fragment = "#ifdef USE_METALNESSMAP\n\tuniform sampler2D metalnessMap;\n#endif"; + +var morphinstance_vertex = "#ifdef USE_INSTANCING_MORPH\n\tfloat morphTargetInfluences[MORPHTARGETS_COUNT];\n\tfloat morphTargetBaseInfluence = texelFetch( morphTexture, ivec2( 0, gl_InstanceID ), 0 ).r;\n\tfor ( int i = 0; i < MORPHTARGETS_COUNT; i ++ ) {\n\t\tmorphTargetInfluences[i] = texelFetch( morphTexture, ivec2( i + 1, gl_InstanceID ), 0 ).r;\n\t}\n#endif"; + +var morphcolor_vertex = "#if defined( USE_MORPHCOLORS ) && defined( MORPHTARGETS_TEXTURE )\n\tvColor *= morphTargetBaseInfluence;\n\tfor ( int i = 0; i < MORPHTARGETS_COUNT; i ++ ) {\n\t\t#if defined( USE_COLOR_ALPHA )\n\t\t\tif ( morphTargetInfluences[ i ] != 0.0 ) vColor += getMorph( gl_VertexID, i, 2 ) * morphTargetInfluences[ i ];\n\t\t#elif defined( USE_COLOR )\n\t\t\tif ( morphTargetInfluences[ i ] != 0.0 ) vColor += getMorph( gl_VertexID, i, 2 ).rgb * morphTargetInfluences[ i ];\n\t\t#endif\n\t}\n#endif"; + +var morphnormal_vertex = "#ifdef USE_MORPHNORMALS\n\tobjectNormal *= morphTargetBaseInfluence;\n\t#ifdef MORPHTARGETS_TEXTURE\n\t\tfor ( int i = 0; i < MORPHTARGETS_COUNT; i ++ ) {\n\t\t\tif ( morphTargetInfluences[ i ] != 0.0 ) objectNormal += getMorph( gl_VertexID, i, 1 ).xyz * morphTargetInfluences[ i ];\n\t\t}\n\t#else\n\t\tobjectNormal += morphNormal0 * morphTargetInfluences[ 0 ];\n\t\tobjectNormal += morphNormal1 * morphTargetInfluences[ 1 ];\n\t\tobjectNormal += morphNormal2 * morphTargetInfluences[ 2 ];\n\t\tobjectNormal += morphNormal3 * morphTargetInfluences[ 3 ];\n\t#endif\n#endif"; + +var morphtarget_pars_vertex = "#ifdef USE_MORPHTARGETS\n\t#ifndef USE_INSTANCING_MORPH\n\t\tuniform float morphTargetBaseInfluence;\n\t#endif\n\t#ifdef MORPHTARGETS_TEXTURE\n\t\t#ifndef USE_INSTANCING_MORPH\n\t\t\tuniform float morphTargetInfluences[ MORPHTARGETS_COUNT ];\n\t\t#endif\n\t\tuniform sampler2DArray morphTargetsTexture;\n\t\tuniform ivec2 morphTargetsTextureSize;\n\t\tvec4 getMorph( const in int vertexIndex, const in int morphTargetIndex, const in int offset ) {\n\t\t\tint texelIndex = vertexIndex * MORPHTARGETS_TEXTURE_STRIDE + offset;\n\t\t\tint y = texelIndex / morphTargetsTextureSize.x;\n\t\t\tint x = texelIndex - y * morphTargetsTextureSize.x;\n\t\t\tivec3 morphUV = ivec3( x, y, morphTargetIndex );\n\t\t\treturn texelFetch( morphTargetsTexture, morphUV, 0 );\n\t\t}\n\t#else\n\t\t#ifndef USE_MORPHNORMALS\n\t\t\tuniform float morphTargetInfluences[ 8 ];\n\t\t#else\n\t\t\tuniform float morphTargetInfluences[ 4 ];\n\t\t#endif\n\t#endif\n#endif"; + +var morphtarget_vertex = "#ifdef USE_MORPHTARGETS\n\ttransformed *= morphTargetBaseInfluence;\n\t#ifdef MORPHTARGETS_TEXTURE\n\t\tfor ( int i = 0; i < MORPHTARGETS_COUNT; i ++ ) {\n\t\t\tif ( morphTargetInfluences[ i ] != 0.0 ) transformed += getMorph( gl_VertexID, i, 0 ).xyz * morphTargetInfluences[ i ];\n\t\t}\n\t#else\n\t\ttransformed += morphTarget0 * morphTargetInfluences[ 0 ];\n\t\ttransformed += morphTarget1 * morphTargetInfluences[ 1 ];\n\t\ttransformed += morphTarget2 * morphTargetInfluences[ 2 ];\n\t\ttransformed += morphTarget3 * morphTargetInfluences[ 3 ];\n\t\t#ifndef USE_MORPHNORMALS\n\t\t\ttransformed += morphTarget4 * morphTargetInfluences[ 4 ];\n\t\t\ttransformed += morphTarget5 * morphTargetInfluences[ 5 ];\n\t\t\ttransformed += morphTarget6 * morphTargetInfluences[ 6 ];\n\t\t\ttransformed += morphTarget7 * morphTargetInfluences[ 7 ];\n\t\t#endif\n\t#endif\n#endif"; + +var normal_fragment_begin = "float faceDirection = gl_FrontFacing ? 1.0 : - 1.0;\n#ifdef FLAT_SHADED\n\tvec3 fdx = dFdx( vViewPosition );\n\tvec3 fdy = dFdy( vViewPosition );\n\tvec3 normal = normalize( cross( fdx, fdy ) );\n#else\n\tvec3 normal = normalize( vNormal );\n\t#ifdef DOUBLE_SIDED\n\t\tnormal *= faceDirection;\n\t#endif\n#endif\n#if defined( USE_NORMALMAP_TANGENTSPACE ) || defined( USE_CLEARCOAT_NORMALMAP ) || defined( USE_ANISOTROPY )\n\t#ifdef USE_TANGENT\n\t\tmat3 tbn = mat3( normalize( vTangent ), normalize( vBitangent ), normal );\n\t#else\n\t\tmat3 tbn = getTangentFrame( - vViewPosition, normal,\n\t\t#if defined( USE_NORMALMAP )\n\t\t\tvNormalMapUv\n\t\t#elif defined( USE_CLEARCOAT_NORMALMAP )\n\t\t\tvClearcoatNormalMapUv\n\t\t#else\n\t\t\tvUv\n\t\t#endif\n\t\t);\n\t#endif\n\t#if defined( DOUBLE_SIDED ) && ! defined( FLAT_SHADED )\n\t\ttbn[0] *= faceDirection;\n\t\ttbn[1] *= faceDirection;\n\t#endif\n#endif\n#ifdef USE_CLEARCOAT_NORMALMAP\n\t#ifdef USE_TANGENT\n\t\tmat3 tbn2 = mat3( normalize( vTangent ), normalize( vBitangent ), normal );\n\t#else\n\t\tmat3 tbn2 = getTangentFrame( - vViewPosition, normal, vClearcoatNormalMapUv );\n\t#endif\n\t#if defined( DOUBLE_SIDED ) && ! defined( FLAT_SHADED )\n\t\ttbn2[0] *= faceDirection;\n\t\ttbn2[1] *= faceDirection;\n\t#endif\n#endif\nvec3 nonPerturbedNormal = normal;"; + +var normal_fragment_maps = "#ifdef USE_NORMALMAP_OBJECTSPACE\n\tnormal = texture2D( normalMap, vNormalMapUv ).xyz * 2.0 - 1.0;\n\t#ifdef FLIP_SIDED\n\t\tnormal = - normal;\n\t#endif\n\t#ifdef DOUBLE_SIDED\n\t\tnormal = normal * faceDirection;\n\t#endif\n\tnormal = normalize( normalMatrix * normal );\n#elif defined( USE_NORMALMAP_TANGENTSPACE )\n\tvec3 mapN = texture2D( normalMap, vNormalMapUv ).xyz * 2.0 - 1.0;\n\tmapN.xy *= normalScale;\n\tnormal = normalize( tbn * mapN );\n#elif defined( USE_BUMPMAP )\n\tnormal = perturbNormalArb( - vViewPosition, normal, dHdxy_fwd(), faceDirection );\n#endif"; + +var normal_pars_fragment = "#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n\t#ifdef USE_TANGENT\n\t\tvarying vec3 vTangent;\n\t\tvarying vec3 vBitangent;\n\t#endif\n#endif"; + +var normal_pars_vertex = "#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n\t#ifdef USE_TANGENT\n\t\tvarying vec3 vTangent;\n\t\tvarying vec3 vBitangent;\n\t#endif\n#endif"; + +var normal_vertex = "#ifndef FLAT_SHADED\n\tvNormal = normalize( transformedNormal );\n\t#ifdef USE_TANGENT\n\t\tvTangent = normalize( transformedTangent );\n\t\tvBitangent = normalize( cross( vNormal, vTangent ) * tangent.w );\n\t#endif\n#endif"; + +var normalmap_pars_fragment = "#ifdef USE_NORMALMAP\n\tuniform sampler2D normalMap;\n\tuniform vec2 normalScale;\n#endif\n#ifdef USE_NORMALMAP_OBJECTSPACE\n\tuniform mat3 normalMatrix;\n#endif\n#if ! defined ( USE_TANGENT ) && ( defined ( USE_NORMALMAP_TANGENTSPACE ) || defined ( USE_CLEARCOAT_NORMALMAP ) || defined( USE_ANISOTROPY ) )\n\tmat3 getTangentFrame( vec3 eye_pos, vec3 surf_norm, vec2 uv ) {\n\t\tvec3 q0 = dFdx( eye_pos.xyz );\n\t\tvec3 q1 = dFdy( eye_pos.xyz );\n\t\tvec2 st0 = dFdx( uv.st );\n\t\tvec2 st1 = dFdy( uv.st );\n\t\tvec3 N = surf_norm;\n\t\tvec3 q1perp = cross( q1, N );\n\t\tvec3 q0perp = cross( N, q0 );\n\t\tvec3 T = q1perp * st0.x + q0perp * st1.x;\n\t\tvec3 B = q1perp * st0.y + q0perp * st1.y;\n\t\tfloat det = max( dot( T, T ), dot( B, B ) );\n\t\tfloat scale = ( det == 0.0 ) ? 0.0 : inversesqrt( det );\n\t\treturn mat3( T * scale, B * scale, N );\n\t}\n#endif"; + +var clearcoat_normal_fragment_begin = "#ifdef USE_CLEARCOAT\n\tvec3 clearcoatNormal = nonPerturbedNormal;\n#endif"; + +var clearcoat_normal_fragment_maps = "#ifdef USE_CLEARCOAT_NORMALMAP\n\tvec3 clearcoatMapN = texture2D( clearcoatNormalMap, vClearcoatNormalMapUv ).xyz * 2.0 - 1.0;\n\tclearcoatMapN.xy *= clearcoatNormalScale;\n\tclearcoatNormal = normalize( tbn2 * clearcoatMapN );\n#endif"; + +var clearcoat_pars_fragment = "#ifdef USE_CLEARCOATMAP\n\tuniform sampler2D clearcoatMap;\n#endif\n#ifdef USE_CLEARCOAT_NORMALMAP\n\tuniform sampler2D clearcoatNormalMap;\n\tuniform vec2 clearcoatNormalScale;\n#endif\n#ifdef USE_CLEARCOAT_ROUGHNESSMAP\n\tuniform sampler2D clearcoatRoughnessMap;\n#endif"; + +var iridescence_pars_fragment = "#ifdef USE_IRIDESCENCEMAP\n\tuniform sampler2D iridescenceMap;\n#endif\n#ifdef USE_IRIDESCENCE_THICKNESSMAP\n\tuniform sampler2D iridescenceThicknessMap;\n#endif"; + +var opaque_fragment = "#ifdef OPAQUE\ndiffuseColor.a = 1.0;\n#endif\n#ifdef USE_TRANSMISSION\ndiffuseColor.a *= material.transmissionAlpha;\n#endif\ngl_FragColor = vec4( outgoingLight, diffuseColor.a );"; + +var packing = "vec3 packNormalToRGB( const in vec3 normal ) {\n\treturn normalize( normal ) * 0.5 + 0.5;\n}\nvec3 unpackRGBToNormal( const in vec3 rgb ) {\n\treturn 2.0 * rgb.xyz - 1.0;\n}\nconst float PackUpscale = 256. / 255.;const float UnpackDownscale = 255. / 256.;\nconst vec3 PackFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );\nconst vec4 UnpackFactors = UnpackDownscale / vec4( PackFactors, 1. );\nconst float ShiftRight8 = 1. / 256.;\nvec4 packDepthToRGBA( const in float v ) {\n\tvec4 r = vec4( fract( v * PackFactors ), v );\n\tr.yzw -= r.xyz * ShiftRight8;\treturn r * PackUpscale;\n}\nfloat unpackRGBAToDepth( const in vec4 v ) {\n\treturn dot( v, UnpackFactors );\n}\nvec2 packDepthToRG( in highp float v ) {\n\treturn packDepthToRGBA( v ).yx;\n}\nfloat unpackRGToDepth( const in highp vec2 v ) {\n\treturn unpackRGBAToDepth( vec4( v.xy, 0.0, 0.0 ) );\n}\nvec4 pack2HalfToRGBA( vec2 v ) {\n\tvec4 r = vec4( v.x, fract( v.x * 255.0 ), v.y, fract( v.y * 255.0 ) );\n\treturn vec4( r.x - r.y / 255.0, r.y, r.z - r.w / 255.0, r.w );\n}\nvec2 unpackRGBATo2Half( vec4 v ) {\n\treturn vec2( v.x + ( v.y / 255.0 ), v.z + ( v.w / 255.0 ) );\n}\nfloat viewZToOrthographicDepth( const in float viewZ, const in float near, const in float far ) {\n\treturn ( viewZ + near ) / ( near - far );\n}\nfloat orthographicDepthToViewZ( const in float depth, const in float near, const in float far ) {\n\treturn depth * ( near - far ) - near;\n}\nfloat viewZToPerspectiveDepth( const in float viewZ, const in float near, const in float far ) {\n\treturn ( ( near + viewZ ) * far ) / ( ( far - near ) * viewZ );\n}\nfloat perspectiveDepthToViewZ( const in float depth, const in float near, const in float far ) {\n\treturn ( near * far ) / ( ( far - near ) * depth - far );\n}"; + +var premultiplied_alpha_fragment = "#ifdef PREMULTIPLIED_ALPHA\n\tgl_FragColor.rgb *= gl_FragColor.a;\n#endif"; + +var project_vertex = "vec4 mvPosition = vec4( transformed, 1.0 );\n#ifdef USE_BATCHING\n\tmvPosition = batchingMatrix * mvPosition;\n#endif\n#ifdef USE_INSTANCING\n\tmvPosition = instanceMatrix * mvPosition;\n#endif\nmvPosition = modelViewMatrix * mvPosition;\ngl_Position = projectionMatrix * mvPosition;"; + +var dithering_fragment = "#ifdef DITHERING\n\tgl_FragColor.rgb = dithering( gl_FragColor.rgb );\n#endif"; + +var dithering_pars_fragment = "#ifdef DITHERING\n\tvec3 dithering( vec3 color ) {\n\t\tfloat grid_position = rand( gl_FragCoord.xy );\n\t\tvec3 dither_shift_RGB = vec3( 0.25 / 255.0, -0.25 / 255.0, 0.25 / 255.0 );\n\t\tdither_shift_RGB = mix( 2.0 * dither_shift_RGB, -2.0 * dither_shift_RGB, grid_position );\n\t\treturn color + dither_shift_RGB;\n\t}\n#endif"; + +var roughnessmap_fragment = "float roughnessFactor = roughness;\n#ifdef USE_ROUGHNESSMAP\n\tvec4 texelRoughness = texture2D( roughnessMap, vRoughnessMapUv );\n\troughnessFactor *= texelRoughness.g;\n#endif"; + +var roughnessmap_pars_fragment = "#ifdef USE_ROUGHNESSMAP\n\tuniform sampler2D roughnessMap;\n#endif"; + +var shadowmap_pars_fragment = "#if NUM_SPOT_LIGHT_COORDS > 0\n\tvarying vec4 vSpotLightCoord[ NUM_SPOT_LIGHT_COORDS ];\n#endif\n#if NUM_SPOT_LIGHT_MAPS > 0\n\tuniform sampler2D spotLightMap[ NUM_SPOT_LIGHT_MAPS ];\n#endif\n#ifdef USE_SHADOWMAP\n\t#if NUM_DIR_LIGHT_SHADOWS > 0\n\t\tuniform sampler2D directionalShadowMap[ NUM_DIR_LIGHT_SHADOWS ];\n\t\tvarying vec4 vDirectionalShadowCoord[ NUM_DIR_LIGHT_SHADOWS ];\n\t\tstruct DirectionalLightShadow {\n\t\t\tfloat shadowBias;\n\t\t\tfloat shadowNormalBias;\n\t\t\tfloat shadowRadius;\n\t\t\tvec2 shadowMapSize;\n\t\t};\n\t\tuniform DirectionalLightShadow directionalLightShadows[ NUM_DIR_LIGHT_SHADOWS ];\n\t#endif\n\t#if NUM_SPOT_LIGHT_SHADOWS > 0\n\t\tuniform sampler2D spotShadowMap[ NUM_SPOT_LIGHT_SHADOWS ];\n\t\tstruct SpotLightShadow {\n\t\t\tfloat shadowBias;\n\t\t\tfloat shadowNormalBias;\n\t\t\tfloat shadowRadius;\n\t\t\tvec2 shadowMapSize;\n\t\t};\n\t\tuniform SpotLightShadow spotLightShadows[ NUM_SPOT_LIGHT_SHADOWS ];\n\t#endif\n\t#if NUM_POINT_LIGHT_SHADOWS > 0\n\t\tuniform sampler2D pointShadowMap[ NUM_POINT_LIGHT_SHADOWS ];\n\t\tvarying vec4 vPointShadowCoord[ NUM_POINT_LIGHT_SHADOWS ];\n\t\tstruct PointLightShadow {\n\t\t\tfloat shadowBias;\n\t\t\tfloat shadowNormalBias;\n\t\t\tfloat shadowRadius;\n\t\t\tvec2 shadowMapSize;\n\t\t\tfloat shadowCameraNear;\n\t\t\tfloat shadowCameraFar;\n\t\t};\n\t\tuniform PointLightShadow pointLightShadows[ NUM_POINT_LIGHT_SHADOWS ];\n\t#endif\n\tfloat texture2DCompare( sampler2D depths, vec2 uv, float compare ) {\n\t\treturn step( compare, unpackRGBAToDepth( texture2D( depths, uv ) ) );\n\t}\n\tvec2 texture2DDistribution( sampler2D shadow, vec2 uv ) {\n\t\treturn unpackRGBATo2Half( texture2D( shadow, uv ) );\n\t}\n\tfloat VSMShadow (sampler2D shadow, vec2 uv, float compare ){\n\t\tfloat occlusion = 1.0;\n\t\tvec2 distribution = texture2DDistribution( shadow, uv );\n\t\tfloat hard_shadow = step( compare , distribution.x );\n\t\tif (hard_shadow != 1.0 ) {\n\t\t\tfloat distance = compare - distribution.x ;\n\t\t\tfloat variance = max( 0.00000, distribution.y * distribution.y );\n\t\t\tfloat softness_probability = variance / (variance + distance * distance );\t\t\tsoftness_probability = clamp( ( softness_probability - 0.3 ) / ( 0.95 - 0.3 ), 0.0, 1.0 );\t\t\tocclusion = clamp( max( hard_shadow, softness_probability ), 0.0, 1.0 );\n\t\t}\n\t\treturn occlusion;\n\t}\n\tfloat getShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord ) {\n\t\tfloat shadow = 1.0;\n\t\tshadowCoord.xyz /= shadowCoord.w;\n\t\tshadowCoord.z += shadowBias;\n\t\tbool inFrustum = shadowCoord.x >= 0.0 && shadowCoord.x <= 1.0 && shadowCoord.y >= 0.0 && shadowCoord.y <= 1.0;\n\t\tbool frustumTest = inFrustum && shadowCoord.z <= 1.0;\n\t\tif ( frustumTest ) {\n\t\t#if defined( SHADOWMAP_TYPE_PCF )\n\t\t\tvec2 texelSize = vec2( 1.0 ) / shadowMapSize;\n\t\t\tfloat dx0 = - texelSize.x * shadowRadius;\n\t\t\tfloat dy0 = - texelSize.y * shadowRadius;\n\t\t\tfloat dx1 = + texelSize.x * shadowRadius;\n\t\t\tfloat dy1 = + texelSize.y * shadowRadius;\n\t\t\tfloat dx2 = dx0 / 2.0;\n\t\t\tfloat dy2 = dy0 / 2.0;\n\t\t\tfloat dx3 = dx1 / 2.0;\n\t\t\tfloat dy3 = dy1 / 2.0;\n\t\t\tshadow = (\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, dy0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, dy0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx2, dy2 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy2 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx3, dy2 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, 0.0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx2, 0.0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy, shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx3, 0.0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, 0.0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx2, dy3 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy3 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx3, dy3 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, dy1 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy1 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, dy1 ), shadowCoord.z )\n\t\t\t) * ( 1.0 / 17.0 );\n\t\t#elif defined( SHADOWMAP_TYPE_PCF_SOFT )\n\t\t\tvec2 texelSize = vec2( 1.0 ) / shadowMapSize;\n\t\t\tfloat dx = texelSize.x;\n\t\t\tfloat dy = texelSize.y;\n\t\t\tvec2 uv = shadowCoord.xy;\n\t\t\tvec2 f = fract( uv * shadowMapSize + 0.5 );\n\t\t\tuv -= f * texelSize;\n\t\t\tshadow = (\n\t\t\t\ttexture2DCompare( shadowMap, uv, shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, uv + vec2( dx, 0.0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, uv + vec2( 0.0, dy ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, uv + texelSize, shadowCoord.z ) +\n\t\t\t\tmix( texture2DCompare( shadowMap, uv + vec2( -dx, 0.0 ), shadowCoord.z ),\n\t\t\t\t\t texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, 0.0 ), shadowCoord.z ),\n\t\t\t\t\t f.x ) +\n\t\t\t\tmix( texture2DCompare( shadowMap, uv + vec2( -dx, dy ), shadowCoord.z ),\n\t\t\t\t\t texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, dy ), shadowCoord.z ),\n\t\t\t\t\t f.x ) +\n\t\t\t\tmix( texture2DCompare( shadowMap, uv + vec2( 0.0, -dy ), shadowCoord.z ),\n\t\t\t\t\t texture2DCompare( shadowMap, uv + vec2( 0.0, 2.0 * dy ), shadowCoord.z ),\n\t\t\t\t\t f.y ) +\n\t\t\t\tmix( texture2DCompare( shadowMap, uv + vec2( dx, -dy ), shadowCoord.z ),\n\t\t\t\t\t texture2DCompare( shadowMap, uv + vec2( dx, 2.0 * dy ), shadowCoord.z ),\n\t\t\t\t\t f.y ) +\n\t\t\t\tmix( mix( texture2DCompare( shadowMap, uv + vec2( -dx, -dy ), shadowCoord.z ),\n\t\t\t\t\t\t texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, -dy ), shadowCoord.z ),\n\t\t\t\t\t\t f.x ),\n\t\t\t\t\t mix( texture2DCompare( shadowMap, uv + vec2( -dx, 2.0 * dy ), shadowCoord.z ),\n\t\t\t\t\t\t texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, 2.0 * dy ), shadowCoord.z ),\n\t\t\t\t\t\t f.x ),\n\t\t\t\t\t f.y )\n\t\t\t) * ( 1.0 / 9.0 );\n\t\t#elif defined( SHADOWMAP_TYPE_VSM )\n\t\t\tshadow = VSMShadow( shadowMap, shadowCoord.xy, shadowCoord.z );\n\t\t#else\n\t\t\tshadow = texture2DCompare( shadowMap, shadowCoord.xy, shadowCoord.z );\n\t\t#endif\n\t\t}\n\t\treturn shadow;\n\t}\n\tvec2 cubeToUV( vec3 v, float texelSizeY ) {\n\t\tvec3 absV = abs( v );\n\t\tfloat scaleToCube = 1.0 / max( absV.x, max( absV.y, absV.z ) );\n\t\tabsV *= scaleToCube;\n\t\tv *= scaleToCube * ( 1.0 - 2.0 * texelSizeY );\n\t\tvec2 planar = v.xy;\n\t\tfloat almostATexel = 1.5 * texelSizeY;\n\t\tfloat almostOne = 1.0 - almostATexel;\n\t\tif ( absV.z >= almostOne ) {\n\t\t\tif ( v.z > 0.0 )\n\t\t\t\tplanar.x = 4.0 - v.x;\n\t\t} else if ( absV.x >= almostOne ) {\n\t\t\tfloat signX = sign( v.x );\n\t\t\tplanar.x = v.z * signX + 2.0 * signX;\n\t\t} else if ( absV.y >= almostOne ) {\n\t\t\tfloat signY = sign( v.y );\n\t\t\tplanar.x = v.x + 2.0 * signY + 2.0;\n\t\t\tplanar.y = v.z * signY - 2.0;\n\t\t}\n\t\treturn vec2( 0.125, 0.25 ) * planar + vec2( 0.375, 0.75 );\n\t}\n\tfloat getPointShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord, float shadowCameraNear, float shadowCameraFar ) {\n\t\tfloat shadow = 1.0;\n\t\tvec3 lightToPosition = shadowCoord.xyz;\n\t\t\n\t\tfloat lightToPositionLength = length( lightToPosition );\n\t\tif ( lightToPositionLength - shadowCameraFar <= 0.0 && lightToPositionLength - shadowCameraNear >= 0.0 ) {\n\t\t\tfloat dp = ( lightToPositionLength - shadowCameraNear ) / ( shadowCameraFar - shadowCameraNear );\t\t\tdp += shadowBias;\n\t\t\tvec3 bd3D = normalize( lightToPosition );\n\t\t\tvec2 texelSize = vec2( 1.0 ) / ( shadowMapSize * vec2( 4.0, 2.0 ) );\n\t\t\t#if defined( SHADOWMAP_TYPE_PCF ) || defined( SHADOWMAP_TYPE_PCF_SOFT ) || defined( SHADOWMAP_TYPE_VSM )\n\t\t\t\tvec2 offset = vec2( - 1, 1 ) * shadowRadius * texelSize.y;\n\t\t\t\tshadow = (\n\t\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyy, texelSize.y ), dp ) +\n\t\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.yyy, texelSize.y ), dp ) +\n\t\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyx, texelSize.y ), dp ) +\n\t\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.yyx, texelSize.y ), dp ) +\n\t\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D, texelSize.y ), dp ) +\n\t\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.xxy, texelSize.y ), dp ) +\n\t\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.yxy, texelSize.y ), dp ) +\n\t\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.xxx, texelSize.y ), dp ) +\n\t\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.yxx, texelSize.y ), dp )\n\t\t\t\t) * ( 1.0 / 9.0 );\n\t\t\t#else\n\t\t\t\tshadow = texture2DCompare( shadowMap, cubeToUV( bd3D, texelSize.y ), dp );\n\t\t\t#endif\n\t\t}\n\t\treturn shadow;\n\t}\n#endif"; + +var shadowmap_pars_vertex = "#if NUM_SPOT_LIGHT_COORDS > 0\n\tuniform mat4 spotLightMatrix[ NUM_SPOT_LIGHT_COORDS ];\n\tvarying vec4 vSpotLightCoord[ NUM_SPOT_LIGHT_COORDS ];\n#endif\n#ifdef USE_SHADOWMAP\n\t#if NUM_DIR_LIGHT_SHADOWS > 0\n\t\tuniform mat4 directionalShadowMatrix[ NUM_DIR_LIGHT_SHADOWS ];\n\t\tvarying vec4 vDirectionalShadowCoord[ NUM_DIR_LIGHT_SHADOWS ];\n\t\tstruct DirectionalLightShadow {\n\t\t\tfloat shadowBias;\n\t\t\tfloat shadowNormalBias;\n\t\t\tfloat shadowRadius;\n\t\t\tvec2 shadowMapSize;\n\t\t};\n\t\tuniform DirectionalLightShadow directionalLightShadows[ NUM_DIR_LIGHT_SHADOWS ];\n\t#endif\n\t#if NUM_SPOT_LIGHT_SHADOWS > 0\n\t\tstruct SpotLightShadow {\n\t\t\tfloat shadowBias;\n\t\t\tfloat shadowNormalBias;\n\t\t\tfloat shadowRadius;\n\t\t\tvec2 shadowMapSize;\n\t\t};\n\t\tuniform SpotLightShadow spotLightShadows[ NUM_SPOT_LIGHT_SHADOWS ];\n\t#endif\n\t#if NUM_POINT_LIGHT_SHADOWS > 0\n\t\tuniform mat4 pointShadowMatrix[ NUM_POINT_LIGHT_SHADOWS ];\n\t\tvarying vec4 vPointShadowCoord[ NUM_POINT_LIGHT_SHADOWS ];\n\t\tstruct PointLightShadow {\n\t\t\tfloat shadowBias;\n\t\t\tfloat shadowNormalBias;\n\t\t\tfloat shadowRadius;\n\t\t\tvec2 shadowMapSize;\n\t\t\tfloat shadowCameraNear;\n\t\t\tfloat shadowCameraFar;\n\t\t};\n\t\tuniform PointLightShadow pointLightShadows[ NUM_POINT_LIGHT_SHADOWS ];\n\t#endif\n#endif"; + +var shadowmap_vertex = "#if ( defined( USE_SHADOWMAP ) && ( NUM_DIR_LIGHT_SHADOWS > 0 || NUM_POINT_LIGHT_SHADOWS > 0 ) ) || ( NUM_SPOT_LIGHT_COORDS > 0 )\n\tvec3 shadowWorldNormal = inverseTransformDirection( transformedNormal, viewMatrix );\n\tvec4 shadowWorldPosition;\n#endif\n#if defined( USE_SHADOWMAP )\n\t#if NUM_DIR_LIGHT_SHADOWS > 0\n\t\t#pragma unroll_loop_start\n\t\tfor ( int i = 0; i < NUM_DIR_LIGHT_SHADOWS; i ++ ) {\n\t\t\tshadowWorldPosition = worldPosition + vec4( shadowWorldNormal * directionalLightShadows[ i ].shadowNormalBias, 0 );\n\t\t\tvDirectionalShadowCoord[ i ] = directionalShadowMatrix[ i ] * shadowWorldPosition;\n\t\t}\n\t\t#pragma unroll_loop_end\n\t#endif\n\t#if NUM_POINT_LIGHT_SHADOWS > 0\n\t\t#pragma unroll_loop_start\n\t\tfor ( int i = 0; i < NUM_POINT_LIGHT_SHADOWS; i ++ ) {\n\t\t\tshadowWorldPosition = worldPosition + vec4( shadowWorldNormal * pointLightShadows[ i ].shadowNormalBias, 0 );\n\t\t\tvPointShadowCoord[ i ] = pointShadowMatrix[ i ] * shadowWorldPosition;\n\t\t}\n\t\t#pragma unroll_loop_end\n\t#endif\n#endif\n#if NUM_SPOT_LIGHT_COORDS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_SPOT_LIGHT_COORDS; i ++ ) {\n\t\tshadowWorldPosition = worldPosition;\n\t\t#if ( defined( USE_SHADOWMAP ) && UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS )\n\t\t\tshadowWorldPosition.xyz += shadowWorldNormal * spotLightShadows[ i ].shadowNormalBias;\n\t\t#endif\n\t\tvSpotLightCoord[ i ] = spotLightMatrix[ i ] * shadowWorldPosition;\n\t}\n\t#pragma unroll_loop_end\n#endif"; + +var shadowmask_pars_fragment = "float getShadowMask() {\n\tfloat shadow = 1.0;\n\t#ifdef USE_SHADOWMAP\n\t#if NUM_DIR_LIGHT_SHADOWS > 0\n\tDirectionalLightShadow directionalLight;\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_DIR_LIGHT_SHADOWS; i ++ ) {\n\t\tdirectionalLight = directionalLightShadows[ i ];\n\t\tshadow *= receiveShadow ? getShadow( directionalShadowMap[ i ], directionalLight.shadowMapSize, directionalLight.shadowBias, directionalLight.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n\t#if NUM_SPOT_LIGHT_SHADOWS > 0\n\tSpotLightShadow spotLight;\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_SPOT_LIGHT_SHADOWS; i ++ ) {\n\t\tspotLight = spotLightShadows[ i ];\n\t\tshadow *= receiveShadow ? getShadow( spotShadowMap[ i ], spotLight.shadowMapSize, spotLight.shadowBias, spotLight.shadowRadius, vSpotLightCoord[ i ] ) : 1.0;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n\t#if NUM_POINT_LIGHT_SHADOWS > 0\n\tPointLightShadow pointLight;\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_POINT_LIGHT_SHADOWS; i ++ ) {\n\t\tpointLight = pointLightShadows[ i ];\n\t\tshadow *= receiveShadow ? getPointShadow( pointShadowMap[ i ], pointLight.shadowMapSize, pointLight.shadowBias, pointLight.shadowRadius, vPointShadowCoord[ i ], pointLight.shadowCameraNear, pointLight.shadowCameraFar ) : 1.0;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n\t#endif\n\treturn shadow;\n}"; + +var skinbase_vertex = "#ifdef USE_SKINNING\n\tmat4 boneMatX = getBoneMatrix( skinIndex.x );\n\tmat4 boneMatY = getBoneMatrix( skinIndex.y );\n\tmat4 boneMatZ = getBoneMatrix( skinIndex.z );\n\tmat4 boneMatW = getBoneMatrix( skinIndex.w );\n#endif"; + +var skinning_pars_vertex = "#ifdef USE_SKINNING\n\tuniform mat4 bindMatrix;\n\tuniform mat4 bindMatrixInverse;\n\tuniform highp sampler2D boneTexture;\n\tmat4 getBoneMatrix( const in float i ) {\n\t\tint size = textureSize( boneTexture, 0 ).x;\n\t\tint j = int( i ) * 4;\n\t\tint x = j % size;\n\t\tint y = j / size;\n\t\tvec4 v1 = texelFetch( boneTexture, ivec2( x, y ), 0 );\n\t\tvec4 v2 = texelFetch( boneTexture, ivec2( x + 1, y ), 0 );\n\t\tvec4 v3 = texelFetch( boneTexture, ivec2( x + 2, y ), 0 );\n\t\tvec4 v4 = texelFetch( boneTexture, ivec2( x + 3, y ), 0 );\n\t\treturn mat4( v1, v2, v3, v4 );\n\t}\n#endif"; + +var skinning_vertex = "#ifdef USE_SKINNING\n\tvec4 skinVertex = bindMatrix * vec4( transformed, 1.0 );\n\tvec4 skinned = vec4( 0.0 );\n\tskinned += boneMatX * skinVertex * skinWeight.x;\n\tskinned += boneMatY * skinVertex * skinWeight.y;\n\tskinned += boneMatZ * skinVertex * skinWeight.z;\n\tskinned += boneMatW * skinVertex * skinWeight.w;\n\ttransformed = ( bindMatrixInverse * skinned ).xyz;\n#endif"; + +var skinnormal_vertex = "#ifdef USE_SKINNING\n\tmat4 skinMatrix = mat4( 0.0 );\n\tskinMatrix += skinWeight.x * boneMatX;\n\tskinMatrix += skinWeight.y * boneMatY;\n\tskinMatrix += skinWeight.z * boneMatZ;\n\tskinMatrix += skinWeight.w * boneMatW;\n\tskinMatrix = bindMatrixInverse * skinMatrix * bindMatrix;\n\tobjectNormal = vec4( skinMatrix * vec4( objectNormal, 0.0 ) ).xyz;\n\t#ifdef USE_TANGENT\n\t\tobjectTangent = vec4( skinMatrix * vec4( objectTangent, 0.0 ) ).xyz;\n\t#endif\n#endif"; + +var specularmap_fragment = "float specularStrength;\n#ifdef USE_SPECULARMAP\n\tvec4 texelSpecular = texture2D( specularMap, vSpecularMapUv );\n\tspecularStrength = texelSpecular.r;\n#else\n\tspecularStrength = 1.0;\n#endif"; + +var specularmap_pars_fragment = "#ifdef USE_SPECULARMAP\n\tuniform sampler2D specularMap;\n#endif"; + +var tonemapping_fragment = "#if defined( TONE_MAPPING )\n\tgl_FragColor.rgb = toneMapping( gl_FragColor.rgb );\n#endif"; + +var tonemapping_pars_fragment = "#ifndef saturate\n#define saturate( a ) clamp( a, 0.0, 1.0 )\n#endif\nuniform float toneMappingExposure;\nvec3 LinearToneMapping( vec3 color ) {\n\treturn saturate( toneMappingExposure * color );\n}\nvec3 ReinhardToneMapping( vec3 color ) {\n\tcolor *= toneMappingExposure;\n\treturn saturate( color / ( vec3( 1.0 ) + color ) );\n}\nvec3 OptimizedCineonToneMapping( vec3 color ) {\n\tcolor *= toneMappingExposure;\n\tcolor = max( vec3( 0.0 ), color - 0.004 );\n\treturn pow( ( color * ( 6.2 * color + 0.5 ) ) / ( color * ( 6.2 * color + 1.7 ) + 0.06 ), vec3( 2.2 ) );\n}\nvec3 RRTAndODTFit( vec3 v ) {\n\tvec3 a = v * ( v + 0.0245786 ) - 0.000090537;\n\tvec3 b = v * ( 0.983729 * v + 0.4329510 ) + 0.238081;\n\treturn a / b;\n}\nvec3 ACESFilmicToneMapping( vec3 color ) {\n\tconst mat3 ACESInputMat = mat3(\n\t\tvec3( 0.59719, 0.07600, 0.02840 ),\t\tvec3( 0.35458, 0.90834, 0.13383 ),\n\t\tvec3( 0.04823, 0.01566, 0.83777 )\n\t);\n\tconst mat3 ACESOutputMat = mat3(\n\t\tvec3( 1.60475, -0.10208, -0.00327 ),\t\tvec3( -0.53108, 1.10813, -0.07276 ),\n\t\tvec3( -0.07367, -0.00605, 1.07602 )\n\t);\n\tcolor *= toneMappingExposure / 0.6;\n\tcolor = ACESInputMat * color;\n\tcolor = RRTAndODTFit( color );\n\tcolor = ACESOutputMat * color;\n\treturn saturate( color );\n}\nconst mat3 LINEAR_REC2020_TO_LINEAR_SRGB = mat3(\n\tvec3( 1.6605, - 0.1246, - 0.0182 ),\n\tvec3( - 0.5876, 1.1329, - 0.1006 ),\n\tvec3( - 0.0728, - 0.0083, 1.1187 )\n);\nconst mat3 LINEAR_SRGB_TO_LINEAR_REC2020 = mat3(\n\tvec3( 0.6274, 0.0691, 0.0164 ),\n\tvec3( 0.3293, 0.9195, 0.0880 ),\n\tvec3( 0.0433, 0.0113, 0.8956 )\n);\nvec3 agxDefaultContrastApprox( vec3 x ) {\n\tvec3 x2 = x * x;\n\tvec3 x4 = x2 * x2;\n\treturn + 15.5 * x4 * x2\n\t\t- 40.14 * x4 * x\n\t\t+ 31.96 * x4\n\t\t- 6.868 * x2 * x\n\t\t+ 0.4298 * x2\n\t\t+ 0.1191 * x\n\t\t- 0.00232;\n}\nvec3 AgXToneMapping( vec3 color ) {\n\tconst mat3 AgXInsetMatrix = mat3(\n\t\tvec3( 0.856627153315983, 0.137318972929847, 0.11189821299995 ),\n\t\tvec3( 0.0951212405381588, 0.761241990602591, 0.0767994186031903 ),\n\t\tvec3( 0.0482516061458583, 0.101439036467562, 0.811302368396859 )\n\t);\n\tconst mat3 AgXOutsetMatrix = mat3(\n\t\tvec3( 1.1271005818144368, - 0.1413297634984383, - 0.14132976349843826 ),\n\t\tvec3( - 0.11060664309660323, 1.157823702216272, - 0.11060664309660294 ),\n\t\tvec3( - 0.016493938717834573, - 0.016493938717834257, 1.2519364065950405 )\n\t);\n\tconst float AgxMinEv = - 12.47393;\tconst float AgxMaxEv = 4.026069;\n\tcolor *= toneMappingExposure;\n\tcolor = LINEAR_SRGB_TO_LINEAR_REC2020 * color;\n\tcolor = AgXInsetMatrix * color;\n\tcolor = max( color, 1e-10 );\tcolor = log2( color );\n\tcolor = ( color - AgxMinEv ) / ( AgxMaxEv - AgxMinEv );\n\tcolor = clamp( color, 0.0, 1.0 );\n\tcolor = agxDefaultContrastApprox( color );\n\tcolor = AgXOutsetMatrix * color;\n\tcolor = pow( max( vec3( 0.0 ), color ), vec3( 2.2 ) );\n\tcolor = LINEAR_REC2020_TO_LINEAR_SRGB * color;\n\tcolor = clamp( color, 0.0, 1.0 );\n\treturn color;\n}\nvec3 NeutralToneMapping( vec3 color ) {\n\tconst float StartCompression = 0.8 - 0.04;\n\tconst float Desaturation = 0.15;\n\tcolor *= toneMappingExposure;\n\tfloat x = min( color.r, min( color.g, color.b ) );\n\tfloat offset = x < 0.08 ? x - 6.25 * x * x : 0.04;\n\tcolor -= offset;\n\tfloat peak = max( color.r, max( color.g, color.b ) );\n\tif ( peak < StartCompression ) return color;\n\tfloat d = 1. - StartCompression;\n\tfloat newPeak = 1. - d * d / ( peak + d - StartCompression );\n\tcolor *= newPeak / peak;\n\tfloat g = 1. - 1. / ( Desaturation * ( peak - newPeak ) + 1. );\n\treturn mix( color, vec3( newPeak ), g );\n}\nvec3 CustomToneMapping( vec3 color ) { return color; }"; + +var transmission_fragment = "#ifdef USE_TRANSMISSION\n\tmaterial.transmission = transmission;\n\tmaterial.transmissionAlpha = 1.0;\n\tmaterial.thickness = thickness;\n\tmaterial.attenuationDistance = attenuationDistance;\n\tmaterial.attenuationColor = attenuationColor;\n\t#ifdef USE_TRANSMISSIONMAP\n\t\tmaterial.transmission *= texture2D( transmissionMap, vTransmissionMapUv ).r;\n\t#endif\n\t#ifdef USE_THICKNESSMAP\n\t\tmaterial.thickness *= texture2D( thicknessMap, vThicknessMapUv ).g;\n\t#endif\n\tvec3 pos = vWorldPosition;\n\tvec3 v = normalize( cameraPosition - pos );\n\tvec3 n = inverseTransformDirection( normal, viewMatrix );\n\tvec4 transmitted = getIBLVolumeRefraction(\n\t\tn, v, material.roughness, material.diffuseColor, material.specularColor, material.specularF90,\n\t\tpos, modelMatrix, viewMatrix, projectionMatrix, material.dispersion, material.ior, material.thickness,\n\t\tmaterial.attenuationColor, material.attenuationDistance );\n\tmaterial.transmissionAlpha = mix( material.transmissionAlpha, transmitted.a, material.transmission );\n\ttotalDiffuse = mix( totalDiffuse, transmitted.rgb, material.transmission );\n#endif"; + +var transmission_pars_fragment = "#ifdef USE_TRANSMISSION\n\tuniform float transmission;\n\tuniform float thickness;\n\tuniform float attenuationDistance;\n\tuniform vec3 attenuationColor;\n\t#ifdef USE_TRANSMISSIONMAP\n\t\tuniform sampler2D transmissionMap;\n\t#endif\n\t#ifdef USE_THICKNESSMAP\n\t\tuniform sampler2D thicknessMap;\n\t#endif\n\tuniform vec2 transmissionSamplerSize;\n\tuniform sampler2D transmissionSamplerMap;\n\tuniform mat4 modelMatrix;\n\tuniform mat4 projectionMatrix;\n\tvarying vec3 vWorldPosition;\n\tfloat w0( float a ) {\n\t\treturn ( 1.0 / 6.0 ) * ( a * ( a * ( - a + 3.0 ) - 3.0 ) + 1.0 );\n\t}\n\tfloat w1( float a ) {\n\t\treturn ( 1.0 / 6.0 ) * ( a * a * ( 3.0 * a - 6.0 ) + 4.0 );\n\t}\n\tfloat w2( float a ){\n\t\treturn ( 1.0 / 6.0 ) * ( a * ( a * ( - 3.0 * a + 3.0 ) + 3.0 ) + 1.0 );\n\t}\n\tfloat w3( float a ) {\n\t\treturn ( 1.0 / 6.0 ) * ( a * a * a );\n\t}\n\tfloat g0( float a ) {\n\t\treturn w0( a ) + w1( a );\n\t}\n\tfloat g1( float a ) {\n\t\treturn w2( a ) + w3( a );\n\t}\n\tfloat h0( float a ) {\n\t\treturn - 1.0 + w1( a ) / ( w0( a ) + w1( a ) );\n\t}\n\tfloat h1( float a ) {\n\t\treturn 1.0 + w3( a ) / ( w2( a ) + w3( a ) );\n\t}\n\tvec4 bicubic( sampler2D tex, vec2 uv, vec4 texelSize, float lod ) {\n\t\tuv = uv * texelSize.zw + 0.5;\n\t\tvec2 iuv = floor( uv );\n\t\tvec2 fuv = fract( uv );\n\t\tfloat g0x = g0( fuv.x );\n\t\tfloat g1x = g1( fuv.x );\n\t\tfloat h0x = h0( fuv.x );\n\t\tfloat h1x = h1( fuv.x );\n\t\tfloat h0y = h0( fuv.y );\n\t\tfloat h1y = h1( fuv.y );\n\t\tvec2 p0 = ( vec2( iuv.x + h0x, iuv.y + h0y ) - 0.5 ) * texelSize.xy;\n\t\tvec2 p1 = ( vec2( iuv.x + h1x, iuv.y + h0y ) - 0.5 ) * texelSize.xy;\n\t\tvec2 p2 = ( vec2( iuv.x + h0x, iuv.y + h1y ) - 0.5 ) * texelSize.xy;\n\t\tvec2 p3 = ( vec2( iuv.x + h1x, iuv.y + h1y ) - 0.5 ) * texelSize.xy;\n\t\treturn g0( fuv.y ) * ( g0x * textureLod( tex, p0, lod ) + g1x * textureLod( tex, p1, lod ) ) +\n\t\t\tg1( fuv.y ) * ( g0x * textureLod( tex, p2, lod ) + g1x * textureLod( tex, p3, lod ) );\n\t}\n\tvec4 textureBicubic( sampler2D sampler, vec2 uv, float lod ) {\n\t\tvec2 fLodSize = vec2( textureSize( sampler, int( lod ) ) );\n\t\tvec2 cLodSize = vec2( textureSize( sampler, int( lod + 1.0 ) ) );\n\t\tvec2 fLodSizeInv = 1.0 / fLodSize;\n\t\tvec2 cLodSizeInv = 1.0 / cLodSize;\n\t\tvec4 fSample = bicubic( sampler, uv, vec4( fLodSizeInv, fLodSize ), floor( lod ) );\n\t\tvec4 cSample = bicubic( sampler, uv, vec4( cLodSizeInv, cLodSize ), ceil( lod ) );\n\t\treturn mix( fSample, cSample, fract( lod ) );\n\t}\n\tvec3 getVolumeTransmissionRay( const in vec3 n, const in vec3 v, const in float thickness, const in float ior, const in mat4 modelMatrix ) {\n\t\tvec3 refractionVector = refract( - v, normalize( n ), 1.0 / ior );\n\t\tvec3 modelScale;\n\t\tmodelScale.x = length( vec3( modelMatrix[ 0 ].xyz ) );\n\t\tmodelScale.y = length( vec3( modelMatrix[ 1 ].xyz ) );\n\t\tmodelScale.z = length( vec3( modelMatrix[ 2 ].xyz ) );\n\t\treturn normalize( refractionVector ) * thickness * modelScale;\n\t}\n\tfloat applyIorToRoughness( const in float roughness, const in float ior ) {\n\t\treturn roughness * clamp( ior * 2.0 - 2.0, 0.0, 1.0 );\n\t}\n\tvec4 getTransmissionSample( const in vec2 fragCoord, const in float roughness, const in float ior ) {\n\t\tfloat lod = log2( transmissionSamplerSize.x ) * applyIorToRoughness( roughness, ior );\n\t\treturn textureBicubic( transmissionSamplerMap, fragCoord.xy, lod );\n\t}\n\tvec3 volumeAttenuation( const in float transmissionDistance, const in vec3 attenuationColor, const in float attenuationDistance ) {\n\t\tif ( isinf( attenuationDistance ) ) {\n\t\t\treturn vec3( 1.0 );\n\t\t} else {\n\t\t\tvec3 attenuationCoefficient = -log( attenuationColor ) / attenuationDistance;\n\t\t\tvec3 transmittance = exp( - attenuationCoefficient * transmissionDistance );\t\t\treturn transmittance;\n\t\t}\n\t}\n\tvec4 getIBLVolumeRefraction( const in vec3 n, const in vec3 v, const in float roughness, const in vec3 diffuseColor,\n\t\tconst in vec3 specularColor, const in float specularF90, const in vec3 position, const in mat4 modelMatrix,\n\t\tconst in mat4 viewMatrix, const in mat4 projMatrix, const in float dispersion, const in float ior, const in float thickness,\n\t\tconst in vec3 attenuationColor, const in float attenuationDistance ) {\n\t\tvec4 transmittedLight;\n\t\tvec3 transmittance;\n\t\t#ifdef USE_DISPERSION\n\t\t\tfloat halfSpread = ( ior - 1.0 ) * 0.025 * dispersion;\n\t\t\tvec3 iors = vec3( ior - halfSpread, ior, ior + halfSpread );\n\t\t\tfor ( int i = 0; i < 3; i ++ ) {\n\t\t\t\tvec3 transmissionRay = getVolumeTransmissionRay( n, v, thickness, iors[ i ], modelMatrix );\n\t\t\t\tvec3 refractedRayExit = position + transmissionRay;\n\t\t\n\t\t\t\tvec4 ndcPos = projMatrix * viewMatrix * vec4( refractedRayExit, 1.0 );\n\t\t\t\tvec2 refractionCoords = ndcPos.xy / ndcPos.w;\n\t\t\t\trefractionCoords += 1.0;\n\t\t\t\trefractionCoords /= 2.0;\n\t\t\n\t\t\t\tvec4 transmissionSample = getTransmissionSample( refractionCoords, roughness, iors[ i ] );\n\t\t\t\ttransmittedLight[ i ] = transmissionSample[ i ];\n\t\t\t\ttransmittedLight.a += transmissionSample.a;\n\t\t\t\ttransmittance[ i ] = diffuseColor[ i ] * volumeAttenuation( length( transmissionRay ), attenuationColor, attenuationDistance )[ i ];\n\t\t\t}\n\t\t\ttransmittedLight.a /= 3.0;\n\t\t\n\t\t#else\n\t\t\n\t\t\tvec3 transmissionRay = getVolumeTransmissionRay( n, v, thickness, ior, modelMatrix );\n\t\t\tvec3 refractedRayExit = position + transmissionRay;\n\t\t\tvec4 ndcPos = projMatrix * viewMatrix * vec4( refractedRayExit, 1.0 );\n\t\t\tvec2 refractionCoords = ndcPos.xy / ndcPos.w;\n\t\t\trefractionCoords += 1.0;\n\t\t\trefractionCoords /= 2.0;\n\t\t\ttransmittedLight = getTransmissionSample( refractionCoords, roughness, ior );\n\t\t\ttransmittance = diffuseColor * volumeAttenuation( length( transmissionRay ), attenuationColor, attenuationDistance );\n\t\t\n\t\t#endif\n\t\tvec3 attenuatedColor = transmittance * transmittedLight.rgb;\n\t\tvec3 F = EnvironmentBRDF( n, v, specularColor, specularF90, roughness );\n\t\tfloat transmittanceFactor = ( transmittance.r + transmittance.g + transmittance.b ) / 3.0;\n\t\treturn vec4( ( 1.0 - F ) * attenuatedColor, 1.0 - ( 1.0 - transmittedLight.a ) * transmittanceFactor );\n\t}\n#endif"; + +var uv_pars_fragment = "#if defined( USE_UV ) || defined( USE_ANISOTROPY )\n\tvarying vec2 vUv;\n#endif\n#ifdef USE_MAP\n\tvarying vec2 vMapUv;\n#endif\n#ifdef USE_ALPHAMAP\n\tvarying vec2 vAlphaMapUv;\n#endif\n#ifdef USE_LIGHTMAP\n\tvarying vec2 vLightMapUv;\n#endif\n#ifdef USE_AOMAP\n\tvarying vec2 vAoMapUv;\n#endif\n#ifdef USE_BUMPMAP\n\tvarying vec2 vBumpMapUv;\n#endif\n#ifdef USE_NORMALMAP\n\tvarying vec2 vNormalMapUv;\n#endif\n#ifdef USE_EMISSIVEMAP\n\tvarying vec2 vEmissiveMapUv;\n#endif\n#ifdef USE_METALNESSMAP\n\tvarying vec2 vMetalnessMapUv;\n#endif\n#ifdef USE_ROUGHNESSMAP\n\tvarying vec2 vRoughnessMapUv;\n#endif\n#ifdef USE_ANISOTROPYMAP\n\tvarying vec2 vAnisotropyMapUv;\n#endif\n#ifdef USE_CLEARCOATMAP\n\tvarying vec2 vClearcoatMapUv;\n#endif\n#ifdef USE_CLEARCOAT_NORMALMAP\n\tvarying vec2 vClearcoatNormalMapUv;\n#endif\n#ifdef USE_CLEARCOAT_ROUGHNESSMAP\n\tvarying vec2 vClearcoatRoughnessMapUv;\n#endif\n#ifdef USE_IRIDESCENCEMAP\n\tvarying vec2 vIridescenceMapUv;\n#endif\n#ifdef USE_IRIDESCENCE_THICKNESSMAP\n\tvarying vec2 vIridescenceThicknessMapUv;\n#endif\n#ifdef USE_SHEEN_COLORMAP\n\tvarying vec2 vSheenColorMapUv;\n#endif\n#ifdef USE_SHEEN_ROUGHNESSMAP\n\tvarying vec2 vSheenRoughnessMapUv;\n#endif\n#ifdef USE_SPECULARMAP\n\tvarying vec2 vSpecularMapUv;\n#endif\n#ifdef USE_SPECULAR_COLORMAP\n\tvarying vec2 vSpecularColorMapUv;\n#endif\n#ifdef USE_SPECULAR_INTENSITYMAP\n\tvarying vec2 vSpecularIntensityMapUv;\n#endif\n#ifdef USE_TRANSMISSIONMAP\n\tuniform mat3 transmissionMapTransform;\n\tvarying vec2 vTransmissionMapUv;\n#endif\n#ifdef USE_THICKNESSMAP\n\tuniform mat3 thicknessMapTransform;\n\tvarying vec2 vThicknessMapUv;\n#endif"; + +var uv_pars_vertex = "#if defined( USE_UV ) || defined( USE_ANISOTROPY )\n\tvarying vec2 vUv;\n#endif\n#ifdef USE_MAP\n\tuniform mat3 mapTransform;\n\tvarying vec2 vMapUv;\n#endif\n#ifdef USE_ALPHAMAP\n\tuniform mat3 alphaMapTransform;\n\tvarying vec2 vAlphaMapUv;\n#endif\n#ifdef USE_LIGHTMAP\n\tuniform mat3 lightMapTransform;\n\tvarying vec2 vLightMapUv;\n#endif\n#ifdef USE_AOMAP\n\tuniform mat3 aoMapTransform;\n\tvarying vec2 vAoMapUv;\n#endif\n#ifdef USE_BUMPMAP\n\tuniform mat3 bumpMapTransform;\n\tvarying vec2 vBumpMapUv;\n#endif\n#ifdef USE_NORMALMAP\n\tuniform mat3 normalMapTransform;\n\tvarying vec2 vNormalMapUv;\n#endif\n#ifdef USE_DISPLACEMENTMAP\n\tuniform mat3 displacementMapTransform;\n\tvarying vec2 vDisplacementMapUv;\n#endif\n#ifdef USE_EMISSIVEMAP\n\tuniform mat3 emissiveMapTransform;\n\tvarying vec2 vEmissiveMapUv;\n#endif\n#ifdef USE_METALNESSMAP\n\tuniform mat3 metalnessMapTransform;\n\tvarying vec2 vMetalnessMapUv;\n#endif\n#ifdef USE_ROUGHNESSMAP\n\tuniform mat3 roughnessMapTransform;\n\tvarying vec2 vRoughnessMapUv;\n#endif\n#ifdef USE_ANISOTROPYMAP\n\tuniform mat3 anisotropyMapTransform;\n\tvarying vec2 vAnisotropyMapUv;\n#endif\n#ifdef USE_CLEARCOATMAP\n\tuniform mat3 clearcoatMapTransform;\n\tvarying vec2 vClearcoatMapUv;\n#endif\n#ifdef USE_CLEARCOAT_NORMALMAP\n\tuniform mat3 clearcoatNormalMapTransform;\n\tvarying vec2 vClearcoatNormalMapUv;\n#endif\n#ifdef USE_CLEARCOAT_ROUGHNESSMAP\n\tuniform mat3 clearcoatRoughnessMapTransform;\n\tvarying vec2 vClearcoatRoughnessMapUv;\n#endif\n#ifdef USE_SHEEN_COLORMAP\n\tuniform mat3 sheenColorMapTransform;\n\tvarying vec2 vSheenColorMapUv;\n#endif\n#ifdef USE_SHEEN_ROUGHNESSMAP\n\tuniform mat3 sheenRoughnessMapTransform;\n\tvarying vec2 vSheenRoughnessMapUv;\n#endif\n#ifdef USE_IRIDESCENCEMAP\n\tuniform mat3 iridescenceMapTransform;\n\tvarying vec2 vIridescenceMapUv;\n#endif\n#ifdef USE_IRIDESCENCE_THICKNESSMAP\n\tuniform mat3 iridescenceThicknessMapTransform;\n\tvarying vec2 vIridescenceThicknessMapUv;\n#endif\n#ifdef USE_SPECULARMAP\n\tuniform mat3 specularMapTransform;\n\tvarying vec2 vSpecularMapUv;\n#endif\n#ifdef USE_SPECULAR_COLORMAP\n\tuniform mat3 specularColorMapTransform;\n\tvarying vec2 vSpecularColorMapUv;\n#endif\n#ifdef USE_SPECULAR_INTENSITYMAP\n\tuniform mat3 specularIntensityMapTransform;\n\tvarying vec2 vSpecularIntensityMapUv;\n#endif\n#ifdef USE_TRANSMISSIONMAP\n\tuniform mat3 transmissionMapTransform;\n\tvarying vec2 vTransmissionMapUv;\n#endif\n#ifdef USE_THICKNESSMAP\n\tuniform mat3 thicknessMapTransform;\n\tvarying vec2 vThicknessMapUv;\n#endif"; + +var uv_vertex = "#if defined( USE_UV ) || defined( USE_ANISOTROPY )\n\tvUv = vec3( uv, 1 ).xy;\n#endif\n#ifdef USE_MAP\n\tvMapUv = ( mapTransform * vec3( MAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_ALPHAMAP\n\tvAlphaMapUv = ( alphaMapTransform * vec3( ALPHAMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_LIGHTMAP\n\tvLightMapUv = ( lightMapTransform * vec3( LIGHTMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_AOMAP\n\tvAoMapUv = ( aoMapTransform * vec3( AOMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_BUMPMAP\n\tvBumpMapUv = ( bumpMapTransform * vec3( BUMPMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_NORMALMAP\n\tvNormalMapUv = ( normalMapTransform * vec3( NORMALMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_DISPLACEMENTMAP\n\tvDisplacementMapUv = ( displacementMapTransform * vec3( DISPLACEMENTMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_EMISSIVEMAP\n\tvEmissiveMapUv = ( emissiveMapTransform * vec3( EMISSIVEMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_METALNESSMAP\n\tvMetalnessMapUv = ( metalnessMapTransform * vec3( METALNESSMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_ROUGHNESSMAP\n\tvRoughnessMapUv = ( roughnessMapTransform * vec3( ROUGHNESSMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_ANISOTROPYMAP\n\tvAnisotropyMapUv = ( anisotropyMapTransform * vec3( ANISOTROPYMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_CLEARCOATMAP\n\tvClearcoatMapUv = ( clearcoatMapTransform * vec3( CLEARCOATMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_CLEARCOAT_NORMALMAP\n\tvClearcoatNormalMapUv = ( clearcoatNormalMapTransform * vec3( CLEARCOAT_NORMALMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_CLEARCOAT_ROUGHNESSMAP\n\tvClearcoatRoughnessMapUv = ( clearcoatRoughnessMapTransform * vec3( CLEARCOAT_ROUGHNESSMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_IRIDESCENCEMAP\n\tvIridescenceMapUv = ( iridescenceMapTransform * vec3( IRIDESCENCEMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_IRIDESCENCE_THICKNESSMAP\n\tvIridescenceThicknessMapUv = ( iridescenceThicknessMapTransform * vec3( IRIDESCENCE_THICKNESSMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_SHEEN_COLORMAP\n\tvSheenColorMapUv = ( sheenColorMapTransform * vec3( SHEEN_COLORMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_SHEEN_ROUGHNESSMAP\n\tvSheenRoughnessMapUv = ( sheenRoughnessMapTransform * vec3( SHEEN_ROUGHNESSMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_SPECULARMAP\n\tvSpecularMapUv = ( specularMapTransform * vec3( SPECULARMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_SPECULAR_COLORMAP\n\tvSpecularColorMapUv = ( specularColorMapTransform * vec3( SPECULAR_COLORMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_SPECULAR_INTENSITYMAP\n\tvSpecularIntensityMapUv = ( specularIntensityMapTransform * vec3( SPECULAR_INTENSITYMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_TRANSMISSIONMAP\n\tvTransmissionMapUv = ( transmissionMapTransform * vec3( TRANSMISSIONMAP_UV, 1 ) ).xy;\n#endif\n#ifdef USE_THICKNESSMAP\n\tvThicknessMapUv = ( thicknessMapTransform * vec3( THICKNESSMAP_UV, 1 ) ).xy;\n#endif"; + +var worldpos_vertex = "#if defined( USE_ENVMAP ) || defined( DISTANCE ) || defined ( USE_SHADOWMAP ) || defined ( USE_TRANSMISSION ) || NUM_SPOT_LIGHT_COORDS > 0\n\tvec4 worldPosition = vec4( transformed, 1.0 );\n\t#ifdef USE_BATCHING\n\t\tworldPosition = batchingMatrix * worldPosition;\n\t#endif\n\t#ifdef USE_INSTANCING\n\t\tworldPosition = instanceMatrix * worldPosition;\n\t#endif\n\tworldPosition = modelMatrix * worldPosition;\n#endif"; + +const vertex$h = "varying vec2 vUv;\nuniform mat3 uvTransform;\nvoid main() {\n\tvUv = ( uvTransform * vec3( uv, 1 ) ).xy;\n\tgl_Position = vec4( position.xy, 1.0, 1.0 );\n}"; + +const fragment$h = "uniform sampler2D t2D;\nuniform float backgroundIntensity;\nvarying vec2 vUv;\nvoid main() {\n\tvec4 texColor = texture2D( t2D, vUv );\n\t#ifdef DECODE_VIDEO_TEXTURE\n\t\ttexColor = vec4( mix( pow( texColor.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), texColor.rgb * 0.0773993808, vec3( lessThanEqual( texColor.rgb, vec3( 0.04045 ) ) ) ), texColor.w );\n\t#endif\n\ttexColor.rgb *= backgroundIntensity;\n\tgl_FragColor = texColor;\n\t#include \n\t#include \n}"; + +const vertex$g = "varying vec3 vWorldDirection;\n#include \nvoid main() {\n\tvWorldDirection = transformDirection( position, modelMatrix );\n\t#include \n\t#include \n\tgl_Position.z = gl_Position.w;\n}"; + +const fragment$g = "#ifdef ENVMAP_TYPE_CUBE\n\tuniform samplerCube envMap;\n#elif defined( ENVMAP_TYPE_CUBE_UV )\n\tuniform sampler2D envMap;\n#endif\nuniform float flipEnvMap;\nuniform float backgroundBlurriness;\nuniform float backgroundIntensity;\nuniform mat3 backgroundRotation;\nvarying vec3 vWorldDirection;\n#include \nvoid main() {\n\t#ifdef ENVMAP_TYPE_CUBE\n\t\tvec4 texColor = textureCube( envMap, backgroundRotation * vec3( flipEnvMap * vWorldDirection.x, vWorldDirection.yz ) );\n\t#elif defined( ENVMAP_TYPE_CUBE_UV )\n\t\tvec4 texColor = textureCubeUV( envMap, backgroundRotation * vWorldDirection, backgroundBlurriness );\n\t#else\n\t\tvec4 texColor = vec4( 0.0, 0.0, 0.0, 1.0 );\n\t#endif\n\ttexColor.rgb *= backgroundIntensity;\n\tgl_FragColor = texColor;\n\t#include \n\t#include \n}"; + +const vertex$f = "varying vec3 vWorldDirection;\n#include \nvoid main() {\n\tvWorldDirection = transformDirection( position, modelMatrix );\n\t#include \n\t#include \n\tgl_Position.z = gl_Position.w;\n}"; + +const fragment$f = "uniform samplerCube tCube;\nuniform float tFlip;\nuniform float opacity;\nvarying vec3 vWorldDirection;\nvoid main() {\n\tvec4 texColor = textureCube( tCube, vec3( tFlip * vWorldDirection.x, vWorldDirection.yz ) );\n\tgl_FragColor = texColor;\n\tgl_FragColor.a *= opacity;\n\t#include \n\t#include \n}"; + +const vertex$e = "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvarying vec2 vHighPrecisionZW;\nvoid main() {\n\t#include \n\t#include \n\t#include \n\t#include \n\t#ifdef USE_DISPLACEMENTMAP\n\t\t#include \n\t\t#include \n\t\t#include \n\t#endif\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvHighPrecisionZW = gl_Position.zw;\n}"; + +const fragment$e = "#if DEPTH_PACKING == 3200\n\tuniform float opacity;\n#endif\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvarying vec2 vHighPrecisionZW;\nvoid main() {\n\tvec4 diffuseColor = vec4( 1.0 );\n\t#include \n\t#if DEPTH_PACKING == 3200\n\t\tdiffuseColor.a = opacity;\n\t#endif\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tfloat fragCoordZ = 0.5 * vHighPrecisionZW[0] / vHighPrecisionZW[1] + 0.5;\n\t#if DEPTH_PACKING == 3200\n\t\tgl_FragColor = vec4( vec3( 1.0 - fragCoordZ ), opacity );\n\t#elif DEPTH_PACKING == 3201\n\t\tgl_FragColor = packDepthToRGBA( fragCoordZ );\n\t#endif\n}"; + +const vertex$d = "#define DISTANCE\nvarying vec3 vWorldPosition;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\t#include \n\t#include \n\t#include \n\t#ifdef USE_DISPLACEMENTMAP\n\t\t#include \n\t\t#include \n\t\t#include \n\t#endif\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvWorldPosition = worldPosition.xyz;\n}"; + +const fragment$d = "#define DISTANCE\nuniform vec3 referencePosition;\nuniform float nearDistance;\nuniform float farDistance;\nvarying vec3 vWorldPosition;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main () {\n\tvec4 diffuseColor = vec4( 1.0 );\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tfloat dist = length( vWorldPosition - referencePosition );\n\tdist = ( dist - nearDistance ) / ( farDistance - nearDistance );\n\tdist = saturate( dist );\n\tgl_FragColor = packDepthToRGBA( dist );\n}"; + +const vertex$c = "varying vec3 vWorldDirection;\n#include \nvoid main() {\n\tvWorldDirection = transformDirection( position, modelMatrix );\n\t#include \n\t#include \n}"; + +const fragment$c = "uniform sampler2D tEquirect;\nvarying vec3 vWorldDirection;\n#include \nvoid main() {\n\tvec3 direction = normalize( vWorldDirection );\n\tvec2 sampleUV = equirectUv( direction );\n\tgl_FragColor = texture2D( tEquirect, sampleUV );\n\t#include \n\t#include \n}"; + +const vertex$b = "uniform float scale;\nattribute float lineDistance;\nvarying float vLineDistance;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\tvLineDistance = scale * lineDistance;\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; + +const fragment$b = "uniform vec3 diffuse;\nuniform float opacity;\nuniform float dashSize;\nuniform float totalSize;\nvarying float vLineDistance;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include \n\tif ( mod( vLineDistance, totalSize ) > dashSize ) {\n\t\tdiscard;\n\t}\n\tvec3 outgoingLight = vec3( 0.0 );\n\t#include \n\t#include \n\t#include \n\toutgoingLight = diffuseColor.rgb;\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; + +const vertex$a = "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#if defined ( USE_ENVMAP ) || defined ( USE_SKINNING )\n\t\t#include \n\t\t#include \n\t\t#include \n\t\t#include \n\t\t#include \n\t#endif\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; + +const fragment$a = "uniform vec3 diffuse;\nuniform float opacity;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );\n\t#ifdef USE_LIGHTMAP\n\t\tvec4 lightMapTexel = texture2D( lightMap, vLightMapUv );\n\t\treflectedLight.indirectDiffuse += lightMapTexel.rgb * lightMapIntensity * RECIPROCAL_PI;\n\t#else\n\t\treflectedLight.indirectDiffuse += vec3( 1.0 );\n\t#endif\n\t#include \n\treflectedLight.indirectDiffuse *= diffuseColor.rgb;\n\tvec3 outgoingLight = reflectedLight.indirectDiffuse;\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; + +const vertex$9 = "#define LAMBERT\nvarying vec3 vViewPosition;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvViewPosition = - mvPosition.xyz;\n\t#include \n\t#include \n\t#include \n\t#include \n}"; + +const fragment$9 = "#define LAMBERT\nuniform vec3 diffuse;\nuniform vec3 emissive;\nuniform float opacity;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include \n\tReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );\n\tvec3 totalEmissiveRadiance = emissive;\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + totalEmissiveRadiance;\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; + +const vertex$8 = "#define MATCAP\nvarying vec3 vViewPosition;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvViewPosition = - mvPosition.xyz;\n}"; + +const fragment$8 = "#define MATCAP\nuniform vec3 diffuse;\nuniform float opacity;\nuniform sampler2D matcap;\nvarying vec3 vViewPosition;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvec3 viewDir = normalize( vViewPosition );\n\tvec3 x = normalize( vec3( viewDir.z, 0.0, - viewDir.x ) );\n\tvec3 y = cross( viewDir, x );\n\tvec2 uv = vec2( dot( x, normal ), dot( y, normal ) ) * 0.495 + 0.5;\n\t#ifdef USE_MATCAP\n\t\tvec4 matcapColor = texture2D( matcap, uv );\n\t#else\n\t\tvec4 matcapColor = vec4( vec3( mix( 0.2, 0.8, uv.y ) ), 1.0 );\n\t#endif\n\tvec3 outgoingLight = diffuseColor.rgb * matcapColor.rgb;\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; + +const vertex$7 = "#define NORMAL\n#if defined( FLAT_SHADED ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP_TANGENTSPACE )\n\tvarying vec3 vViewPosition;\n#endif\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n#if defined( FLAT_SHADED ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP_TANGENTSPACE )\n\tvViewPosition = - mvPosition.xyz;\n#endif\n}"; + +const fragment$7 = "#define NORMAL\nuniform float opacity;\n#if defined( FLAT_SHADED ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP_TANGENTSPACE )\n\tvarying vec3 vViewPosition;\n#endif\n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\tvec4 diffuseColor = vec4( 0.0, 0.0, 0.0, opacity );\n\t#include \n\t#include \n\t#include \n\t#include \n\tgl_FragColor = vec4( packNormalToRGB( normal ), diffuseColor.a );\n\t#ifdef OPAQUE\n\t\tgl_FragColor.a = 1.0;\n\t#endif\n}"; + +const vertex$6 = "#define PHONG\nvarying vec3 vViewPosition;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvViewPosition = - mvPosition.xyz;\n\t#include \n\t#include \n\t#include \n\t#include \n}"; + +const fragment$6 = "#define PHONG\nuniform vec3 diffuse;\nuniform vec3 emissive;\nuniform vec3 specular;\nuniform float shininess;\nuniform float opacity;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include \n\tReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );\n\tvec3 totalEmissiveRadiance = emissive;\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; + +const vertex$5 = "#define STANDARD\nvarying vec3 vViewPosition;\n#ifdef USE_TRANSMISSION\n\tvarying vec3 vWorldPosition;\n#endif\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvViewPosition = - mvPosition.xyz;\n\t#include \n\t#include \n\t#include \n#ifdef USE_TRANSMISSION\n\tvWorldPosition = worldPosition.xyz;\n#endif\n}"; + +const fragment$5 = "#define STANDARD\n#ifdef PHYSICAL\n\t#define IOR\n\t#define USE_SPECULAR\n#endif\nuniform vec3 diffuse;\nuniform vec3 emissive;\nuniform float roughness;\nuniform float metalness;\nuniform float opacity;\n#ifdef IOR\n\tuniform float ior;\n#endif\n#ifdef USE_SPECULAR\n\tuniform float specularIntensity;\n\tuniform vec3 specularColor;\n\t#ifdef USE_SPECULAR_COLORMAP\n\t\tuniform sampler2D specularColorMap;\n\t#endif\n\t#ifdef USE_SPECULAR_INTENSITYMAP\n\t\tuniform sampler2D specularIntensityMap;\n\t#endif\n#endif\n#ifdef USE_CLEARCOAT\n\tuniform float clearcoat;\n\tuniform float clearcoatRoughness;\n#endif\n#ifdef USE_DISPERSION\n\tuniform float dispersion;\n#endif\n#ifdef USE_IRIDESCENCE\n\tuniform float iridescence;\n\tuniform float iridescenceIOR;\n\tuniform float iridescenceThicknessMinimum;\n\tuniform float iridescenceThicknessMaximum;\n#endif\n#ifdef USE_SHEEN\n\tuniform vec3 sheenColor;\n\tuniform float sheenRoughness;\n\t#ifdef USE_SHEEN_COLORMAP\n\t\tuniform sampler2D sheenColorMap;\n\t#endif\n\t#ifdef USE_SHEEN_ROUGHNESSMAP\n\t\tuniform sampler2D sheenRoughnessMap;\n\t#endif\n#endif\n#ifdef USE_ANISOTROPY\n\tuniform vec2 anisotropyVector;\n\t#ifdef USE_ANISOTROPYMAP\n\t\tuniform sampler2D anisotropyMap;\n\t#endif\n#endif\nvarying vec3 vViewPosition;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include \n\tReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );\n\tvec3 totalEmissiveRadiance = emissive;\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvec3 totalDiffuse = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse;\n\tvec3 totalSpecular = reflectedLight.directSpecular + reflectedLight.indirectSpecular;\n\t#include \n\tvec3 outgoingLight = totalDiffuse + totalSpecular + totalEmissiveRadiance;\n\t#ifdef USE_SHEEN\n\t\tfloat sheenEnergyComp = 1.0 - 0.157 * max3( material.sheenColor );\n\t\toutgoingLight = outgoingLight * sheenEnergyComp + sheenSpecularDirect + sheenSpecularIndirect;\n\t#endif\n\t#ifdef USE_CLEARCOAT\n\t\tfloat dotNVcc = saturate( dot( geometryClearcoatNormal, geometryViewDir ) );\n\t\tvec3 Fcc = F_Schlick( material.clearcoatF0, material.clearcoatF90, dotNVcc );\n\t\toutgoingLight = outgoingLight * ( 1.0 - material.clearcoat * Fcc ) + ( clearcoatSpecularDirect + clearcoatSpecularIndirect ) * material.clearcoat;\n\t#endif\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; + +const vertex$4 = "#define TOON\nvarying vec3 vViewPosition;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvViewPosition = - mvPosition.xyz;\n\t#include \n\t#include \n\t#include \n}"; + +const fragment$4 = "#define TOON\nuniform vec3 diffuse;\nuniform vec3 emissive;\nuniform float opacity;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include \n\tReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );\n\tvec3 totalEmissiveRadiance = emissive;\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + totalEmissiveRadiance;\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; + +const vertex$3 = "uniform float size;\nuniform float scale;\n#include \n#include \n#include \n#include \n#include \n#include \n#ifdef USE_POINTS_UV\n\tvarying vec2 vUv;\n\tuniform mat3 uvTransform;\n#endif\nvoid main() {\n\t#ifdef USE_POINTS_UV\n\t\tvUv = ( uvTransform * vec3( uv, 1 ) ).xy;\n\t#endif\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tgl_PointSize = size;\n\t#ifdef USE_SIZEATTENUATION\n\t\tbool isPerspective = isPerspectiveMatrix( projectionMatrix );\n\t\tif ( isPerspective ) gl_PointSize *= ( scale / - mvPosition.z );\n\t#endif\n\t#include \n\t#include \n\t#include \n\t#include \n}"; + +const fragment$3 = "uniform vec3 diffuse;\nuniform float opacity;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include \n\tvec3 outgoingLight = vec3( 0.0 );\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\toutgoingLight = diffuseColor.rgb;\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; + +const vertex$2 = "#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; + +const fragment$2 = "uniform vec3 color;\nuniform float opacity;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\tgl_FragColor = vec4( color, opacity * ( 1.0 - getShadowMask() ) );\n\t#include \n\t#include \n\t#include \n}"; + +const vertex$1 = "uniform float rotation;\nuniform vec2 center;\n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\tvec4 mvPosition = modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );\n\tvec2 scale;\n\tscale.x = length( vec3( modelMatrix[ 0 ].x, modelMatrix[ 0 ].y, modelMatrix[ 0 ].z ) );\n\tscale.y = length( vec3( modelMatrix[ 1 ].x, modelMatrix[ 1 ].y, modelMatrix[ 1 ].z ) );\n\t#ifndef USE_SIZEATTENUATION\n\t\tbool isPerspective = isPerspectiveMatrix( projectionMatrix );\n\t\tif ( isPerspective ) scale *= - mvPosition.z;\n\t#endif\n\tvec2 alignedPosition = ( position.xy - ( center - vec2( 0.5 ) ) ) * scale;\n\tvec2 rotatedPosition;\n\trotatedPosition.x = cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y;\n\trotatedPosition.y = sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y;\n\tmvPosition.xy += rotatedPosition;\n\tgl_Position = projectionMatrix * mvPosition;\n\t#include \n\t#include \n\t#include \n}"; + +const fragment$1 = "uniform vec3 diffuse;\nuniform float opacity;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include \n\tvec3 outgoingLight = vec3( 0.0 );\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\toutgoingLight = diffuseColor.rgb;\n\t#include \n\t#include \n\t#include \n\t#include \n}"; + +const ShaderChunk = { + alphahash_fragment: alphahash_fragment, + alphahash_pars_fragment: alphahash_pars_fragment, + alphamap_fragment: alphamap_fragment, + alphamap_pars_fragment: alphamap_pars_fragment, + alphatest_fragment: alphatest_fragment, + alphatest_pars_fragment: alphatest_pars_fragment, + aomap_fragment: aomap_fragment, + aomap_pars_fragment: aomap_pars_fragment, + batching_pars_vertex: batching_pars_vertex, + batching_vertex: batching_vertex, + begin_vertex: begin_vertex, + beginnormal_vertex: beginnormal_vertex, + bsdfs: bsdfs, + iridescence_fragment: iridescence_fragment, + bumpmap_pars_fragment: bumpmap_pars_fragment, + clipping_planes_fragment: clipping_planes_fragment, + clipping_planes_pars_fragment: clipping_planes_pars_fragment, + clipping_planes_pars_vertex: clipping_planes_pars_vertex, + clipping_planes_vertex: clipping_planes_vertex, + color_fragment: color_fragment, + color_pars_fragment: color_pars_fragment, + color_pars_vertex: color_pars_vertex, + color_vertex: color_vertex, + common: common, + cube_uv_reflection_fragment: cube_uv_reflection_fragment, + defaultnormal_vertex: defaultnormal_vertex, + displacementmap_pars_vertex: displacementmap_pars_vertex, + displacementmap_vertex: displacementmap_vertex, + emissivemap_fragment: emissivemap_fragment, + emissivemap_pars_fragment: emissivemap_pars_fragment, + colorspace_fragment: colorspace_fragment, + colorspace_pars_fragment: colorspace_pars_fragment, + envmap_fragment: envmap_fragment, + envmap_common_pars_fragment: envmap_common_pars_fragment, + envmap_pars_fragment: envmap_pars_fragment, + envmap_pars_vertex: envmap_pars_vertex, + envmap_physical_pars_fragment: envmap_physical_pars_fragment, + envmap_vertex: envmap_vertex, + fog_vertex: fog_vertex, + fog_pars_vertex: fog_pars_vertex, + fog_fragment: fog_fragment, + fog_pars_fragment: fog_pars_fragment, + gradientmap_pars_fragment: gradientmap_pars_fragment, + lightmap_pars_fragment: lightmap_pars_fragment, + lights_lambert_fragment: lights_lambert_fragment, + lights_lambert_pars_fragment: lights_lambert_pars_fragment, + lights_pars_begin: lights_pars_begin, + lights_toon_fragment: lights_toon_fragment, + lights_toon_pars_fragment: lights_toon_pars_fragment, + lights_phong_fragment: lights_phong_fragment, + lights_phong_pars_fragment: lights_phong_pars_fragment, + lights_physical_fragment: lights_physical_fragment, + lights_physical_pars_fragment: lights_physical_pars_fragment, + lights_fragment_begin: lights_fragment_begin, + lights_fragment_maps: lights_fragment_maps, + lights_fragment_end: lights_fragment_end, + logdepthbuf_fragment: logdepthbuf_fragment, + logdepthbuf_pars_fragment: logdepthbuf_pars_fragment, + logdepthbuf_pars_vertex: logdepthbuf_pars_vertex, + logdepthbuf_vertex: logdepthbuf_vertex, + map_fragment: map_fragment, + map_pars_fragment: map_pars_fragment, + map_particle_fragment: map_particle_fragment, + map_particle_pars_fragment: map_particle_pars_fragment, + metalnessmap_fragment: metalnessmap_fragment, + metalnessmap_pars_fragment: metalnessmap_pars_fragment, + morphinstance_vertex: morphinstance_vertex, + morphcolor_vertex: morphcolor_vertex, + morphnormal_vertex: morphnormal_vertex, + morphtarget_pars_vertex: morphtarget_pars_vertex, + morphtarget_vertex: morphtarget_vertex, + normal_fragment_begin: normal_fragment_begin, + normal_fragment_maps: normal_fragment_maps, + normal_pars_fragment: normal_pars_fragment, + normal_pars_vertex: normal_pars_vertex, + normal_vertex: normal_vertex, + normalmap_pars_fragment: normalmap_pars_fragment, + clearcoat_normal_fragment_begin: clearcoat_normal_fragment_begin, + clearcoat_normal_fragment_maps: clearcoat_normal_fragment_maps, + clearcoat_pars_fragment: clearcoat_pars_fragment, + iridescence_pars_fragment: iridescence_pars_fragment, + opaque_fragment: opaque_fragment, + packing: packing, + premultiplied_alpha_fragment: premultiplied_alpha_fragment, + project_vertex: project_vertex, + dithering_fragment: dithering_fragment, + dithering_pars_fragment: dithering_pars_fragment, + roughnessmap_fragment: roughnessmap_fragment, + roughnessmap_pars_fragment: roughnessmap_pars_fragment, + shadowmap_pars_fragment: shadowmap_pars_fragment, + shadowmap_pars_vertex: shadowmap_pars_vertex, + shadowmap_vertex: shadowmap_vertex, + shadowmask_pars_fragment: shadowmask_pars_fragment, + skinbase_vertex: skinbase_vertex, + skinning_pars_vertex: skinning_pars_vertex, + skinning_vertex: skinning_vertex, + skinnormal_vertex: skinnormal_vertex, + specularmap_fragment: specularmap_fragment, + specularmap_pars_fragment: specularmap_pars_fragment, + tonemapping_fragment: tonemapping_fragment, + tonemapping_pars_fragment: tonemapping_pars_fragment, + transmission_fragment: transmission_fragment, + transmission_pars_fragment: transmission_pars_fragment, + uv_pars_fragment: uv_pars_fragment, + uv_pars_vertex: uv_pars_vertex, + uv_vertex: uv_vertex, + worldpos_vertex: worldpos_vertex, + + background_vert: vertex$h, + background_frag: fragment$h, + backgroundCube_vert: vertex$g, + backgroundCube_frag: fragment$g, + cube_vert: vertex$f, + cube_frag: fragment$f, + depth_vert: vertex$e, + depth_frag: fragment$e, + distanceRGBA_vert: vertex$d, + distanceRGBA_frag: fragment$d, + equirect_vert: vertex$c, + equirect_frag: fragment$c, + linedashed_vert: vertex$b, + linedashed_frag: fragment$b, + meshbasic_vert: vertex$a, + meshbasic_frag: fragment$a, + meshlambert_vert: vertex$9, + meshlambert_frag: fragment$9, + meshmatcap_vert: vertex$8, + meshmatcap_frag: fragment$8, + meshnormal_vert: vertex$7, + meshnormal_frag: fragment$7, + meshphong_vert: vertex$6, + meshphong_frag: fragment$6, + meshphysical_vert: vertex$5, + meshphysical_frag: fragment$5, + meshtoon_vert: vertex$4, + meshtoon_frag: fragment$4, + points_vert: vertex$3, + points_frag: fragment$3, + shadow_vert: vertex$2, + shadow_frag: fragment$2, + sprite_vert: vertex$1, + sprite_frag: fragment$1 +}; + +/** + * Uniforms library for shared webgl shaders + */ + +const UniformsLib = { + + common: { + + diffuse: { value: /*@__PURE__*/ new Color( 0xffffff ) }, + opacity: { value: 1.0 }, + + map: { value: null }, + mapTransform: { value: /*@__PURE__*/ new Matrix3() }, + + alphaMap: { value: null }, + alphaMapTransform: { value: /*@__PURE__*/ new Matrix3() }, + + alphaTest: { value: 0 } + + }, + + specularmap: { + + specularMap: { value: null }, + specularMapTransform: { value: /*@__PURE__*/ new Matrix3() } + + }, + + envmap: { + + envMap: { value: null }, + envMapRotation: { value: /*@__PURE__*/ new Matrix3() }, + flipEnvMap: { value: - 1 }, + reflectivity: { value: 1.0 }, // basic, lambert, phong + ior: { value: 1.5 }, // physical + refractionRatio: { value: 0.98 }, // basic, lambert, phong + + }, + + aomap: { + + aoMap: { value: null }, + aoMapIntensity: { value: 1 }, + aoMapTransform: { value: /*@__PURE__*/ new Matrix3() } + + }, + + lightmap: { + + lightMap: { value: null }, + lightMapIntensity: { value: 1 }, + lightMapTransform: { value: /*@__PURE__*/ new Matrix3() } + + }, + + bumpmap: { + + bumpMap: { value: null }, + bumpMapTransform: { value: /*@__PURE__*/ new Matrix3() }, + bumpScale: { value: 1 } + + }, + + normalmap: { + + normalMap: { value: null }, + normalMapTransform: { value: /*@__PURE__*/ new Matrix3() }, + normalScale: { value: /*@__PURE__*/ new Vector2( 1, 1 ) } + + }, + + displacementmap: { + + displacementMap: { value: null }, + displacementMapTransform: { value: /*@__PURE__*/ new Matrix3() }, + displacementScale: { value: 1 }, + displacementBias: { value: 0 } + + }, + + emissivemap: { + + emissiveMap: { value: null }, + emissiveMapTransform: { value: /*@__PURE__*/ new Matrix3() } + + }, + + metalnessmap: { + + metalnessMap: { value: null }, + metalnessMapTransform: { value: /*@__PURE__*/ new Matrix3() } + + }, + + roughnessmap: { + + roughnessMap: { value: null }, + roughnessMapTransform: { value: /*@__PURE__*/ new Matrix3() } + + }, + + gradientmap: { + + gradientMap: { value: null } + + }, + + fog: { + + fogDensity: { value: 0.00025 }, + fogNear: { value: 1 }, + fogFar: { value: 2000 }, + fogColor: { value: /*@__PURE__*/ new Color( 0xffffff ) } + + }, + + lights: { + + ambientLightColor: { value: [] }, + + lightProbe: { value: [] }, + + directionalLights: { value: [], properties: { + direction: {}, + color: {} + } }, + + directionalLightShadows: { value: [], properties: { + shadowBias: {}, + shadowNormalBias: {}, + shadowRadius: {}, + shadowMapSize: {} + } }, + + directionalShadowMap: { value: [] }, + directionalShadowMatrix: { value: [] }, + + spotLights: { value: [], properties: { + color: {}, + position: {}, + direction: {}, + distance: {}, + coneCos: {}, + penumbraCos: {}, + decay: {} + } }, + + spotLightShadows: { value: [], properties: { + shadowBias: {}, + shadowNormalBias: {}, + shadowRadius: {}, + shadowMapSize: {} + } }, + + spotLightMap: { value: [] }, + spotShadowMap: { value: [] }, + spotLightMatrix: { value: [] }, + + pointLights: { value: [], properties: { + color: {}, + position: {}, + decay: {}, + distance: {} + } }, + + pointLightShadows: { value: [], properties: { + shadowBias: {}, + shadowNormalBias: {}, + shadowRadius: {}, + shadowMapSize: {}, + shadowCameraNear: {}, + shadowCameraFar: {} + } }, + + pointShadowMap: { value: [] }, + pointShadowMatrix: { value: [] }, + + hemisphereLights: { value: [], properties: { + direction: {}, + skyColor: {}, + groundColor: {} + } }, + + // TODO (abelnation): RectAreaLight BRDF data needs to be moved from example to main src + rectAreaLights: { value: [], properties: { + color: {}, + position: {}, + width: {}, + height: {} + } }, + + ltc_1: { value: null }, + ltc_2: { value: null } + + }, + + points: { + + diffuse: { value: /*@__PURE__*/ new Color( 0xffffff ) }, + opacity: { value: 1.0 }, + size: { value: 1.0 }, + scale: { value: 1.0 }, + map: { value: null }, + alphaMap: { value: null }, + alphaMapTransform: { value: /*@__PURE__*/ new Matrix3() }, + alphaTest: { value: 0 }, + uvTransform: { value: /*@__PURE__*/ new Matrix3() } + + }, + + sprite: { + + diffuse: { value: /*@__PURE__*/ new Color( 0xffffff ) }, + opacity: { value: 1.0 }, + center: { value: /*@__PURE__*/ new Vector2( 0.5, 0.5 ) }, + rotation: { value: 0.0 }, + map: { value: null }, + mapTransform: { value: /*@__PURE__*/ new Matrix3() }, + alphaMap: { value: null }, + alphaMapTransform: { value: /*@__PURE__*/ new Matrix3() }, + alphaTest: { value: 0 } + + } + +}; + +const ShaderLib = { + + basic: { + + uniforms: /*@__PURE__*/ mergeUniforms( [ + UniformsLib.common, + UniformsLib.specularmap, + UniformsLib.envmap, + UniformsLib.aomap, + UniformsLib.lightmap, + UniformsLib.fog + ] ), + + vertexShader: ShaderChunk.meshbasic_vert, + fragmentShader: ShaderChunk.meshbasic_frag + + }, + + lambert: { + + uniforms: /*@__PURE__*/ mergeUniforms( [ + UniformsLib.common, + UniformsLib.specularmap, + UniformsLib.envmap, + UniformsLib.aomap, + UniformsLib.lightmap, + UniformsLib.emissivemap, + UniformsLib.bumpmap, + UniformsLib.normalmap, + UniformsLib.displacementmap, + UniformsLib.fog, + UniformsLib.lights, + { + emissive: { value: /*@__PURE__*/ new Color( 0x000000 ) } + } + ] ), + + vertexShader: ShaderChunk.meshlambert_vert, + fragmentShader: ShaderChunk.meshlambert_frag + + }, + + phong: { + + uniforms: /*@__PURE__*/ mergeUniforms( [ + UniformsLib.common, + UniformsLib.specularmap, + UniformsLib.envmap, + UniformsLib.aomap, + UniformsLib.lightmap, + UniformsLib.emissivemap, + UniformsLib.bumpmap, + UniformsLib.normalmap, + UniformsLib.displacementmap, + UniformsLib.fog, + UniformsLib.lights, + { + emissive: { value: /*@__PURE__*/ new Color( 0x000000 ) }, + specular: { value: /*@__PURE__*/ new Color( 0x111111 ) }, + shininess: { value: 30 } + } + ] ), + + vertexShader: ShaderChunk.meshphong_vert, + fragmentShader: ShaderChunk.meshphong_frag + + }, + + standard: { + + uniforms: /*@__PURE__*/ mergeUniforms( [ + UniformsLib.common, + UniformsLib.envmap, + UniformsLib.aomap, + UniformsLib.lightmap, + UniformsLib.emissivemap, + UniformsLib.bumpmap, + UniformsLib.normalmap, + UniformsLib.displacementmap, + UniformsLib.roughnessmap, + UniformsLib.metalnessmap, + UniformsLib.fog, + UniformsLib.lights, + { + emissive: { value: /*@__PURE__*/ new Color( 0x000000 ) }, + roughness: { value: 1.0 }, + metalness: { value: 0.0 }, + envMapIntensity: { value: 1 } + } + ] ), + + vertexShader: ShaderChunk.meshphysical_vert, + fragmentShader: ShaderChunk.meshphysical_frag + + }, + + toon: { + + uniforms: /*@__PURE__*/ mergeUniforms( [ + UniformsLib.common, + UniformsLib.aomap, + UniformsLib.lightmap, + UniformsLib.emissivemap, + UniformsLib.bumpmap, + UniformsLib.normalmap, + UniformsLib.displacementmap, + UniformsLib.gradientmap, + UniformsLib.fog, + UniformsLib.lights, + { + emissive: { value: /*@__PURE__*/ new Color( 0x000000 ) } + } + ] ), + + vertexShader: ShaderChunk.meshtoon_vert, + fragmentShader: ShaderChunk.meshtoon_frag + + }, + + matcap: { + + uniforms: /*@__PURE__*/ mergeUniforms( [ + UniformsLib.common, + UniformsLib.bumpmap, + UniformsLib.normalmap, + UniformsLib.displacementmap, + UniformsLib.fog, + { + matcap: { value: null } + } + ] ), + + vertexShader: ShaderChunk.meshmatcap_vert, + fragmentShader: ShaderChunk.meshmatcap_frag + + }, + + points: { + + uniforms: /*@__PURE__*/ mergeUniforms( [ + UniformsLib.points, + UniformsLib.fog + ] ), + + vertexShader: ShaderChunk.points_vert, + fragmentShader: ShaderChunk.points_frag + + }, + + dashed: { + + uniforms: /*@__PURE__*/ mergeUniforms( [ + UniformsLib.common, + UniformsLib.fog, + { + scale: { value: 1 }, + dashSize: { value: 1 }, + totalSize: { value: 2 } + } + ] ), + + vertexShader: ShaderChunk.linedashed_vert, + fragmentShader: ShaderChunk.linedashed_frag + + }, + + depth: { + + uniforms: /*@__PURE__*/ mergeUniforms( [ + UniformsLib.common, + UniformsLib.displacementmap + ] ), + + vertexShader: ShaderChunk.depth_vert, + fragmentShader: ShaderChunk.depth_frag + + }, + + normal: { + + uniforms: /*@__PURE__*/ mergeUniforms( [ + UniformsLib.common, + UniformsLib.bumpmap, + UniformsLib.normalmap, + UniformsLib.displacementmap, + { + opacity: { value: 1.0 } + } + ] ), + + vertexShader: ShaderChunk.meshnormal_vert, + fragmentShader: ShaderChunk.meshnormal_frag + + }, + + sprite: { + + uniforms: /*@__PURE__*/ mergeUniforms( [ + UniformsLib.sprite, + UniformsLib.fog + ] ), + + vertexShader: ShaderChunk.sprite_vert, + fragmentShader: ShaderChunk.sprite_frag + + }, + + background: { + + uniforms: { + uvTransform: { value: /*@__PURE__*/ new Matrix3() }, + t2D: { value: null }, + backgroundIntensity: { value: 1 } + }, + + vertexShader: ShaderChunk.background_vert, + fragmentShader: ShaderChunk.background_frag + + }, + + backgroundCube: { + + uniforms: { + envMap: { value: null }, + flipEnvMap: { value: - 1 }, + backgroundBlurriness: { value: 0 }, + backgroundIntensity: { value: 1 }, + backgroundRotation: { value: /*@__PURE__*/ new Matrix3() } + }, + + vertexShader: ShaderChunk.backgroundCube_vert, + fragmentShader: ShaderChunk.backgroundCube_frag + + }, + + cube: { + + uniforms: { + tCube: { value: null }, + tFlip: { value: - 1 }, + opacity: { value: 1.0 } + }, + + vertexShader: ShaderChunk.cube_vert, + fragmentShader: ShaderChunk.cube_frag + + }, + + equirect: { + + uniforms: { + tEquirect: { value: null }, + }, + + vertexShader: ShaderChunk.equirect_vert, + fragmentShader: ShaderChunk.equirect_frag + + }, + + distanceRGBA: { + + uniforms: /*@__PURE__*/ mergeUniforms( [ + UniformsLib.common, + UniformsLib.displacementmap, + { + referencePosition: { value: /*@__PURE__*/ new Vector3() }, + nearDistance: { value: 1 }, + farDistance: { value: 1000 } + } + ] ), + + vertexShader: ShaderChunk.distanceRGBA_vert, + fragmentShader: ShaderChunk.distanceRGBA_frag + + }, + + shadow: { + + uniforms: /*@__PURE__*/ mergeUniforms( [ + UniformsLib.lights, + UniformsLib.fog, + { + color: { value: /*@__PURE__*/ new Color( 0x00000 ) }, + opacity: { value: 1.0 } + }, + ] ), + + vertexShader: ShaderChunk.shadow_vert, + fragmentShader: ShaderChunk.shadow_frag + + } + +}; + +ShaderLib.physical = { + + uniforms: /*@__PURE__*/ mergeUniforms( [ + ShaderLib.standard.uniforms, + { + clearcoat: { value: 0 }, + clearcoatMap: { value: null }, + clearcoatMapTransform: { value: /*@__PURE__*/ new Matrix3() }, + clearcoatNormalMap: { value: null }, + clearcoatNormalMapTransform: { value: /*@__PURE__*/ new Matrix3() }, + clearcoatNormalScale: { value: /*@__PURE__*/ new Vector2( 1, 1 ) }, + clearcoatRoughness: { value: 0 }, + clearcoatRoughnessMap: { value: null }, + clearcoatRoughnessMapTransform: { value: /*@__PURE__*/ new Matrix3() }, + dispersion: { value: 0 }, + iridescence: { value: 0 }, + iridescenceMap: { value: null }, + iridescenceMapTransform: { value: /*@__PURE__*/ new Matrix3() }, + iridescenceIOR: { value: 1.3 }, + iridescenceThicknessMinimum: { value: 100 }, + iridescenceThicknessMaximum: { value: 400 }, + iridescenceThicknessMap: { value: null }, + iridescenceThicknessMapTransform: { value: /*@__PURE__*/ new Matrix3() }, + sheen: { value: 0 }, + sheenColor: { value: /*@__PURE__*/ new Color( 0x000000 ) }, + sheenColorMap: { value: null }, + sheenColorMapTransform: { value: /*@__PURE__*/ new Matrix3() }, + sheenRoughness: { value: 1 }, + sheenRoughnessMap: { value: null }, + sheenRoughnessMapTransform: { value: /*@__PURE__*/ new Matrix3() }, + transmission: { value: 0 }, + transmissionMap: { value: null }, + transmissionMapTransform: { value: /*@__PURE__*/ new Matrix3() }, + transmissionSamplerSize: { value: /*@__PURE__*/ new Vector2() }, + transmissionSamplerMap: { value: null }, + thickness: { value: 0 }, + thicknessMap: { value: null }, + thicknessMapTransform: { value: /*@__PURE__*/ new Matrix3() }, + attenuationDistance: { value: 0 }, + attenuationColor: { value: /*@__PURE__*/ new Color( 0x000000 ) }, + specularColor: { value: /*@__PURE__*/ new Color( 1, 1, 1 ) }, + specularColorMap: { value: null }, + specularColorMapTransform: { value: /*@__PURE__*/ new Matrix3() }, + specularIntensity: { value: 1 }, + specularIntensityMap: { value: null }, + specularIntensityMapTransform: { value: /*@__PURE__*/ new Matrix3() }, + anisotropyVector: { value: /*@__PURE__*/ new Vector2() }, + anisotropyMap: { value: null }, + anisotropyMapTransform: { value: /*@__PURE__*/ new Matrix3() }, + } + ] ), + + vertexShader: ShaderChunk.meshphysical_vert, + fragmentShader: ShaderChunk.meshphysical_frag + +}; + +const _rgb = { r: 0, b: 0, g: 0 }; +const _e1$1 = /*@__PURE__*/ new Euler(); +const _m1$1 = /*@__PURE__*/ new Matrix4(); + +function WebGLBackground( renderer, cubemaps, cubeuvmaps, state, objects, alpha, premultipliedAlpha ) { + + const clearColor = new Color( 0x000000 ); + let clearAlpha = alpha === true ? 0 : 1; + + let planeMesh; + let boxMesh; + + let currentBackground = null; + let currentBackgroundVersion = 0; + let currentTonemapping = null; + + function getBackground( scene ) { + + let background = scene.isScene === true ? scene.background : null; + + if ( background && background.isTexture ) { + + const usePMREM = scene.backgroundBlurriness > 0; // use PMREM if the user wants to blur the background + background = ( usePMREM ? cubeuvmaps : cubemaps ).get( background ); + + } + + return background; + + } + + function render( scene ) { + + let forceClear = false; + const background = getBackground( scene ); + + if ( background === null ) { + + setClear( clearColor, clearAlpha ); + + } else if ( background && background.isColor ) { + + setClear( background, 1 ); + forceClear = true; + + } + + const environmentBlendMode = renderer.xr.getEnvironmentBlendMode(); + + if ( environmentBlendMode === 'additive' ) { + + state.buffers.color.setClear( 0, 0, 0, 1, premultipliedAlpha ); + + } else if ( environmentBlendMode === 'alpha-blend' ) { + + state.buffers.color.setClear( 0, 0, 0, 0, premultipliedAlpha ); + + } + + if ( renderer.autoClear || forceClear ) { + + renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil ); + + } + + } + + function addToRenderList( renderList, scene ) { + + const background = getBackground( scene ); + + if ( background && ( background.isCubeTexture || background.mapping === CubeUVReflectionMapping ) ) { + + if ( boxMesh === undefined ) { + + boxMesh = new Mesh( + new BoxGeometry( 1, 1, 1 ), + new ShaderMaterial( { + name: 'BackgroundCubeMaterial', + uniforms: cloneUniforms( ShaderLib.backgroundCube.uniforms ), + vertexShader: ShaderLib.backgroundCube.vertexShader, + fragmentShader: ShaderLib.backgroundCube.fragmentShader, + side: BackSide, + depthTest: false, + depthWrite: false, + fog: false + } ) + ); + + boxMesh.geometry.deleteAttribute( 'normal' ); + boxMesh.geometry.deleteAttribute( 'uv' ); + + boxMesh.onBeforeRender = function ( renderer, scene, camera ) { + + this.matrixWorld.copyPosition( camera.matrixWorld ); + + }; + + // add "envMap" material property so the renderer can evaluate it like for built-in materials + Object.defineProperty( boxMesh.material, 'envMap', { + + get: function () { + + return this.uniforms.envMap.value; + + } + + } ); + + objects.update( boxMesh ); + + } + + _e1$1.copy( scene.backgroundRotation ); + + // accommodate left-handed frame + _e1$1.x *= - 1; _e1$1.y *= - 1; _e1$1.z *= - 1; + + if ( background.isCubeTexture && background.isRenderTargetTexture === false ) { + + // environment maps which are not cube render targets or PMREMs follow a different convention + _e1$1.y *= - 1; + _e1$1.z *= - 1; + + } + + boxMesh.material.uniforms.envMap.value = background; + boxMesh.material.uniforms.flipEnvMap.value = ( background.isCubeTexture && background.isRenderTargetTexture === false ) ? - 1 : 1; + boxMesh.material.uniforms.backgroundBlurriness.value = scene.backgroundBlurriness; + boxMesh.material.uniforms.backgroundIntensity.value = scene.backgroundIntensity; + boxMesh.material.uniforms.backgroundRotation.value.setFromMatrix4( _m1$1.makeRotationFromEuler( _e1$1 ) ); + boxMesh.material.toneMapped = ColorManagement.getTransfer( background.colorSpace ) !== SRGBTransfer; + + if ( currentBackground !== background || + currentBackgroundVersion !== background.version || + currentTonemapping !== renderer.toneMapping ) { + + boxMesh.material.needsUpdate = true; + + currentBackground = background; + currentBackgroundVersion = background.version; + currentTonemapping = renderer.toneMapping; + + } + + boxMesh.layers.enableAll(); + + // push to the pre-sorted opaque render list + renderList.unshift( boxMesh, boxMesh.geometry, boxMesh.material, 0, 0, null ); + + } else if ( background && background.isTexture ) { + + if ( planeMesh === undefined ) { + + planeMesh = new Mesh( + new PlaneGeometry( 2, 2 ), + new ShaderMaterial( { + name: 'BackgroundMaterial', + uniforms: cloneUniforms( ShaderLib.background.uniforms ), + vertexShader: ShaderLib.background.vertexShader, + fragmentShader: ShaderLib.background.fragmentShader, + side: FrontSide, + depthTest: false, + depthWrite: false, + fog: false + } ) + ); + + planeMesh.geometry.deleteAttribute( 'normal' ); + + // add "map" material property so the renderer can evaluate it like for built-in materials + Object.defineProperty( planeMesh.material, 'map', { + + get: function () { + + return this.uniforms.t2D.value; + + } + + } ); + + objects.update( planeMesh ); + + } + + planeMesh.material.uniforms.t2D.value = background; + planeMesh.material.uniforms.backgroundIntensity.value = scene.backgroundIntensity; + planeMesh.material.toneMapped = ColorManagement.getTransfer( background.colorSpace ) !== SRGBTransfer; + + if ( background.matrixAutoUpdate === true ) { + + background.updateMatrix(); + + } + + planeMesh.material.uniforms.uvTransform.value.copy( background.matrix ); + + if ( currentBackground !== background || + currentBackgroundVersion !== background.version || + currentTonemapping !== renderer.toneMapping ) { + + planeMesh.material.needsUpdate = true; + + currentBackground = background; + currentBackgroundVersion = background.version; + currentTonemapping = renderer.toneMapping; + + } + + planeMesh.layers.enableAll(); + + // push to the pre-sorted opaque render list + renderList.unshift( planeMesh, planeMesh.geometry, planeMesh.material, 0, 0, null ); + + } + + } + + function setClear( color, alpha ) { + + color.getRGB( _rgb, getUnlitUniformColorSpace( renderer ) ); + + state.buffers.color.setClear( _rgb.r, _rgb.g, _rgb.b, alpha, premultipliedAlpha ); + + } + + return { + + getClearColor: function () { + + return clearColor; + + }, + setClearColor: function ( color, alpha = 1 ) { + + clearColor.set( color ); + clearAlpha = alpha; + setClear( clearColor, clearAlpha ); + + }, + getClearAlpha: function () { + + return clearAlpha; + + }, + setClearAlpha: function ( alpha ) { + + clearAlpha = alpha; + setClear( clearColor, clearAlpha ); + + }, + render: render, + addToRenderList: addToRenderList + + }; + +} + +function WebGLBindingStates( gl, attributes ) { + + const maxVertexAttributes = gl.getParameter( gl.MAX_VERTEX_ATTRIBS ); + + const bindingStates = {}; + + const defaultState = createBindingState( null ); + let currentState = defaultState; + let forceUpdate = false; + + function setup( object, material, program, geometry, index ) { + + let updateBuffers = false; + + const state = getBindingState( geometry, program, material ); + + if ( currentState !== state ) { + + currentState = state; + bindVertexArrayObject( currentState.object ); + + } + + updateBuffers = needsUpdate( object, geometry, program, index ); + + if ( updateBuffers ) saveCache( object, geometry, program, index ); + + if ( index !== null ) { + + attributes.update( index, gl.ELEMENT_ARRAY_BUFFER ); + + } + + if ( updateBuffers || forceUpdate ) { + + forceUpdate = false; + + setupVertexAttributes( object, material, program, geometry ); + + if ( index !== null ) { + + gl.bindBuffer( gl.ELEMENT_ARRAY_BUFFER, attributes.get( index ).buffer ); + + } + + } + + } + + function createVertexArrayObject() { + + return gl.createVertexArray(); + + } + + function bindVertexArrayObject( vao ) { + + return gl.bindVertexArray( vao ); + + } + + function deleteVertexArrayObject( vao ) { + + return gl.deleteVertexArray( vao ); + + } + + function getBindingState( geometry, program, material ) { + + const wireframe = ( material.wireframe === true ); + + let programMap = bindingStates[ geometry.id ]; + + if ( programMap === undefined ) { + + programMap = {}; + bindingStates[ geometry.id ] = programMap; + + } + + let stateMap = programMap[ program.id ]; + + if ( stateMap === undefined ) { + + stateMap = {}; + programMap[ program.id ] = stateMap; + + } + + let state = stateMap[ wireframe ]; + + if ( state === undefined ) { + + state = createBindingState( createVertexArrayObject() ); + stateMap[ wireframe ] = state; + + } + + return state; + + } + + function createBindingState( vao ) { + + const newAttributes = []; + const enabledAttributes = []; + const attributeDivisors = []; + + for ( let i = 0; i < maxVertexAttributes; i ++ ) { + + newAttributes[ i ] = 0; + enabledAttributes[ i ] = 0; + attributeDivisors[ i ] = 0; + + } + + return { + + // for backward compatibility on non-VAO support browser + geometry: null, + program: null, + wireframe: false, + + newAttributes: newAttributes, + enabledAttributes: enabledAttributes, + attributeDivisors: attributeDivisors, + object: vao, + attributes: {}, + index: null + + }; + + } + + function needsUpdate( object, geometry, program, index ) { + + const cachedAttributes = currentState.attributes; + const geometryAttributes = geometry.attributes; + + let attributesNum = 0; + + const programAttributes = program.getAttributes(); + + for ( const name in programAttributes ) { + + const programAttribute = programAttributes[ name ]; + + if ( programAttribute.location >= 0 ) { + + const cachedAttribute = cachedAttributes[ name ]; + let geometryAttribute = geometryAttributes[ name ]; + + if ( geometryAttribute === undefined ) { + + if ( name === 'instanceMatrix' && object.instanceMatrix ) geometryAttribute = object.instanceMatrix; + if ( name === 'instanceColor' && object.instanceColor ) geometryAttribute = object.instanceColor; + + } + + if ( cachedAttribute === undefined ) return true; + + if ( cachedAttribute.attribute !== geometryAttribute ) return true; + + if ( geometryAttribute && cachedAttribute.data !== geometryAttribute.data ) return true; + + attributesNum ++; + + } + + } + + if ( currentState.attributesNum !== attributesNum ) return true; + + if ( currentState.index !== index ) return true; + + return false; + + } + + function saveCache( object, geometry, program, index ) { + + const cache = {}; + const attributes = geometry.attributes; + let attributesNum = 0; + + const programAttributes = program.getAttributes(); + + for ( const name in programAttributes ) { + + const programAttribute = programAttributes[ name ]; + + if ( programAttribute.location >= 0 ) { + + let attribute = attributes[ name ]; + + if ( attribute === undefined ) { + + if ( name === 'instanceMatrix' && object.instanceMatrix ) attribute = object.instanceMatrix; + if ( name === 'instanceColor' && object.instanceColor ) attribute = object.instanceColor; + + } + + const data = {}; + data.attribute = attribute; + + if ( attribute && attribute.data ) { + + data.data = attribute.data; + + } + + cache[ name ] = data; + + attributesNum ++; + + } + + } + + currentState.attributes = cache; + currentState.attributesNum = attributesNum; + + currentState.index = index; + + } + + function initAttributes() { + + const newAttributes = currentState.newAttributes; + + for ( let i = 0, il = newAttributes.length; i < il; i ++ ) { + + newAttributes[ i ] = 0; + + } + + } + + function enableAttribute( attribute ) { + + enableAttributeAndDivisor( attribute, 0 ); + + } + + function enableAttributeAndDivisor( attribute, meshPerAttribute ) { + + const newAttributes = currentState.newAttributes; + const enabledAttributes = currentState.enabledAttributes; + const attributeDivisors = currentState.attributeDivisors; + + newAttributes[ attribute ] = 1; + + if ( enabledAttributes[ attribute ] === 0 ) { + + gl.enableVertexAttribArray( attribute ); + enabledAttributes[ attribute ] = 1; + + } + + if ( attributeDivisors[ attribute ] !== meshPerAttribute ) { + + gl.vertexAttribDivisor( attribute, meshPerAttribute ); + attributeDivisors[ attribute ] = meshPerAttribute; + + } + + } + + function disableUnusedAttributes() { + + const newAttributes = currentState.newAttributes; + const enabledAttributes = currentState.enabledAttributes; + + for ( let i = 0, il = enabledAttributes.length; i < il; i ++ ) { + + if ( enabledAttributes[ i ] !== newAttributes[ i ] ) { + + gl.disableVertexAttribArray( i ); + enabledAttributes[ i ] = 0; + + } + + } + + } + + function vertexAttribPointer( index, size, type, normalized, stride, offset, integer ) { + + if ( integer === true ) { + + gl.vertexAttribIPointer( index, size, type, stride, offset ); + + } else { + + gl.vertexAttribPointer( index, size, type, normalized, stride, offset ); + + } + + } + + function setupVertexAttributes( object, material, program, geometry ) { + + initAttributes(); + + const geometryAttributes = geometry.attributes; + + const programAttributes = program.getAttributes(); + + const materialDefaultAttributeValues = material.defaultAttributeValues; + + for ( const name in programAttributes ) { + + const programAttribute = programAttributes[ name ]; + + if ( programAttribute.location >= 0 ) { + + let geometryAttribute = geometryAttributes[ name ]; + + if ( geometryAttribute === undefined ) { + + if ( name === 'instanceMatrix' && object.instanceMatrix ) geometryAttribute = object.instanceMatrix; + if ( name === 'instanceColor' && object.instanceColor ) geometryAttribute = object.instanceColor; + + } + + if ( geometryAttribute !== undefined ) { + + const normalized = geometryAttribute.normalized; + const size = geometryAttribute.itemSize; + + const attribute = attributes.get( geometryAttribute ); + + // TODO Attribute may not be available on context restore + + if ( attribute === undefined ) continue; + + const buffer = attribute.buffer; + const type = attribute.type; + const bytesPerElement = attribute.bytesPerElement; + + // check for integer attributes + + const integer = ( type === gl.INT || type === gl.UNSIGNED_INT || geometryAttribute.gpuType === IntType ); + + if ( geometryAttribute.isInterleavedBufferAttribute ) { + + const data = geometryAttribute.data; + const stride = data.stride; + const offset = geometryAttribute.offset; + + if ( data.isInstancedInterleavedBuffer ) { + + for ( let i = 0; i < programAttribute.locationSize; i ++ ) { + + enableAttributeAndDivisor( programAttribute.location + i, data.meshPerAttribute ); + + } + + if ( object.isInstancedMesh !== true && geometry._maxInstanceCount === undefined ) { + + geometry._maxInstanceCount = data.meshPerAttribute * data.count; + + } + + } else { + + for ( let i = 0; i < programAttribute.locationSize; i ++ ) { + + enableAttribute( programAttribute.location + i ); + + } + + } + + gl.bindBuffer( gl.ARRAY_BUFFER, buffer ); + + for ( let i = 0; i < programAttribute.locationSize; i ++ ) { + + vertexAttribPointer( + programAttribute.location + i, + size / programAttribute.locationSize, + type, + normalized, + stride * bytesPerElement, + ( offset + ( size / programAttribute.locationSize ) * i ) * bytesPerElement, + integer + ); + + } + + } else { + + if ( geometryAttribute.isInstancedBufferAttribute ) { + + for ( let i = 0; i < programAttribute.locationSize; i ++ ) { + + enableAttributeAndDivisor( programAttribute.location + i, geometryAttribute.meshPerAttribute ); + + } + + if ( object.isInstancedMesh !== true && geometry._maxInstanceCount === undefined ) { + + geometry._maxInstanceCount = geometryAttribute.meshPerAttribute * geometryAttribute.count; + + } + + } else { + + for ( let i = 0; i < programAttribute.locationSize; i ++ ) { + + enableAttribute( programAttribute.location + i ); + + } + + } + + gl.bindBuffer( gl.ARRAY_BUFFER, buffer ); + + for ( let i = 0; i < programAttribute.locationSize; i ++ ) { + + vertexAttribPointer( + programAttribute.location + i, + size / programAttribute.locationSize, + type, + normalized, + size * bytesPerElement, + ( size / programAttribute.locationSize ) * i * bytesPerElement, + integer + ); + + } + + } + + } else if ( materialDefaultAttributeValues !== undefined ) { + + const value = materialDefaultAttributeValues[ name ]; + + if ( value !== undefined ) { + + switch ( value.length ) { + + case 2: + gl.vertexAttrib2fv( programAttribute.location, value ); + break; + + case 3: + gl.vertexAttrib3fv( programAttribute.location, value ); + break; + + case 4: + gl.vertexAttrib4fv( programAttribute.location, value ); + break; + + default: + gl.vertexAttrib1fv( programAttribute.location, value ); + + } + + } + + } + + } + + } + + disableUnusedAttributes(); + + } + + function dispose() { + + reset(); + + for ( const geometryId in bindingStates ) { + + const programMap = bindingStates[ geometryId ]; + + for ( const programId in programMap ) { + + const stateMap = programMap[ programId ]; + + for ( const wireframe in stateMap ) { + + deleteVertexArrayObject( stateMap[ wireframe ].object ); + + delete stateMap[ wireframe ]; + + } + + delete programMap[ programId ]; + + } + + delete bindingStates[ geometryId ]; + + } + + } + + function releaseStatesOfGeometry( geometry ) { + + if ( bindingStates[ geometry.id ] === undefined ) return; + + const programMap = bindingStates[ geometry.id ]; + + for ( const programId in programMap ) { + + const stateMap = programMap[ programId ]; + + for ( const wireframe in stateMap ) { + + deleteVertexArrayObject( stateMap[ wireframe ].object ); + + delete stateMap[ wireframe ]; + + } + + delete programMap[ programId ]; + + } + + delete bindingStates[ geometry.id ]; + + } + + function releaseStatesOfProgram( program ) { + + for ( const geometryId in bindingStates ) { + + const programMap = bindingStates[ geometryId ]; + + if ( programMap[ program.id ] === undefined ) continue; + + const stateMap = programMap[ program.id ]; + + for ( const wireframe in stateMap ) { + + deleteVertexArrayObject( stateMap[ wireframe ].object ); + + delete stateMap[ wireframe ]; + + } + + delete programMap[ program.id ]; + + } + + } + + function reset() { + + resetDefaultState(); + forceUpdate = true; + + if ( currentState === defaultState ) return; + + currentState = defaultState; + bindVertexArrayObject( currentState.object ); + + } + + // for backward-compatibility + + function resetDefaultState() { + + defaultState.geometry = null; + defaultState.program = null; + defaultState.wireframe = false; + + } + + return { + + setup: setup, + reset: reset, + resetDefaultState: resetDefaultState, + dispose: dispose, + releaseStatesOfGeometry: releaseStatesOfGeometry, + releaseStatesOfProgram: releaseStatesOfProgram, + + initAttributes: initAttributes, + enableAttribute: enableAttribute, + disableUnusedAttributes: disableUnusedAttributes + + }; + +} + +function WebGLBufferRenderer( gl, extensions, info ) { + + let mode; + + function setMode( value ) { + + mode = value; + + } + + function render( start, count ) { + + gl.drawArrays( mode, start, count ); + + info.update( count, mode, 1 ); + + } + + function renderInstances( start, count, primcount ) { + + if ( primcount === 0 ) return; + + gl.drawArraysInstanced( mode, start, count, primcount ); + + info.update( count, mode, primcount ); + + } + + function renderMultiDraw( starts, counts, drawCount ) { + + if ( drawCount === 0 ) return; + + const extension = extensions.get( 'WEBGL_multi_draw' ); + + if ( extension === null ) { + + for ( let i = 0; i < drawCount; i ++ ) { + + this.render( starts[ i ], counts[ i ] ); + + } + + } else { + + extension.multiDrawArraysWEBGL( mode, starts, 0, counts, 0, drawCount ); + + let elementCount = 0; + for ( let i = 0; i < drawCount; i ++ ) { + + elementCount += counts[ i ]; + + } + + info.update( elementCount, mode, 1 ); + + } + + } + + function renderMultiDrawInstances( starts, counts, drawCount, primcount ) { + + if ( drawCount === 0 ) return; + + const extension = extensions.get( 'WEBGL_multi_draw' ); + + if ( extension === null ) { + + for ( let i = 0; i < starts.length; i ++ ) { + + renderInstances( starts[ i ], counts[ i ], primcount[ i ] ); + + } + + } else { + + extension.multiDrawArraysInstancedWEBGL( mode, starts, 0, counts, 0, primcount, 0, drawCount ); + + let elementCount = 0; + for ( let i = 0; i < drawCount; i ++ ) { + + elementCount += counts[ i ]; + + } + + for ( let i = 0; i < primcount.length; i ++ ) { + + info.update( elementCount, mode, primcount[ i ] ); + + } + + } + + } + + // + + this.setMode = setMode; + this.render = render; + this.renderInstances = renderInstances; + this.renderMultiDraw = renderMultiDraw; + this.renderMultiDrawInstances = renderMultiDrawInstances; + +} + +function WebGLCapabilities( gl, extensions, parameters, utils ) { + + let maxAnisotropy; + + function getMaxAnisotropy() { + + if ( maxAnisotropy !== undefined ) return maxAnisotropy; + + if ( extensions.has( 'EXT_texture_filter_anisotropic' ) === true ) { + + const extension = extensions.get( 'EXT_texture_filter_anisotropic' ); + + maxAnisotropy = gl.getParameter( extension.MAX_TEXTURE_MAX_ANISOTROPY_EXT ); + + } else { + + maxAnisotropy = 0; + + } + + return maxAnisotropy; + + } + + function textureFormatReadable( textureFormat ) { + + if ( textureFormat !== RGBAFormat && utils.convert( textureFormat ) !== gl.getParameter( gl.IMPLEMENTATION_COLOR_READ_FORMAT ) ) { + + return false; + + } + + return true; + + } + + function textureTypeReadable( textureType ) { + + const halfFloatSupportedByExt = ( textureType === HalfFloatType ) && ( extensions.has( 'EXT_color_buffer_half_float' ) || extensions.has( 'EXT_color_buffer_float' ) ); + + if ( textureType !== UnsignedByteType && utils.convert( textureType ) !== gl.getParameter( gl.IMPLEMENTATION_COLOR_READ_TYPE ) && // Edge and Chrome Mac < 52 (#9513) + textureType !== FloatType && ! halfFloatSupportedByExt ) { + + return false; + + } + + return true; + + } + + function getMaxPrecision( precision ) { + + if ( precision === 'highp' ) { + + if ( gl.getShaderPrecisionFormat( gl.VERTEX_SHADER, gl.HIGH_FLOAT ).precision > 0 && + gl.getShaderPrecisionFormat( gl.FRAGMENT_SHADER, gl.HIGH_FLOAT ).precision > 0 ) { + + return 'highp'; + + } + + precision = 'mediump'; + + } + + if ( precision === 'mediump' ) { + + if ( gl.getShaderPrecisionFormat( gl.VERTEX_SHADER, gl.MEDIUM_FLOAT ).precision > 0 && + gl.getShaderPrecisionFormat( gl.FRAGMENT_SHADER, gl.MEDIUM_FLOAT ).precision > 0 ) { + + return 'mediump'; + + } + + } + + return 'lowp'; + + } + + let precision = parameters.precision !== undefined ? parameters.precision : 'highp'; + const maxPrecision = getMaxPrecision( precision ); + + if ( maxPrecision !== precision ) { + + console.warn( 'THREE.WebGLRenderer:', precision, 'not supported, using', maxPrecision, 'instead.' ); + precision = maxPrecision; + + } + + const logarithmicDepthBuffer = parameters.logarithmicDepthBuffer === true; + + const maxTextures = gl.getParameter( gl.MAX_TEXTURE_IMAGE_UNITS ); + const maxVertexTextures = gl.getParameter( gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS ); + const maxTextureSize = gl.getParameter( gl.MAX_TEXTURE_SIZE ); + const maxCubemapSize = gl.getParameter( gl.MAX_CUBE_MAP_TEXTURE_SIZE ); + + const maxAttributes = gl.getParameter( gl.MAX_VERTEX_ATTRIBS ); + const maxVertexUniforms = gl.getParameter( gl.MAX_VERTEX_UNIFORM_VECTORS ); + const maxVaryings = gl.getParameter( gl.MAX_VARYING_VECTORS ); + const maxFragmentUniforms = gl.getParameter( gl.MAX_FRAGMENT_UNIFORM_VECTORS ); + + const vertexTextures = maxVertexTextures > 0; + + const maxSamples = gl.getParameter( gl.MAX_SAMPLES ); + + return { + + isWebGL2: true, // keeping this for backwards compatibility + + getMaxAnisotropy: getMaxAnisotropy, + getMaxPrecision: getMaxPrecision, + + textureFormatReadable: textureFormatReadable, + textureTypeReadable: textureTypeReadable, + + precision: precision, + logarithmicDepthBuffer: logarithmicDepthBuffer, + + maxTextures: maxTextures, + maxVertexTextures: maxVertexTextures, + maxTextureSize: maxTextureSize, + maxCubemapSize: maxCubemapSize, + + maxAttributes: maxAttributes, + maxVertexUniforms: maxVertexUniforms, + maxVaryings: maxVaryings, + maxFragmentUniforms: maxFragmentUniforms, + + vertexTextures: vertexTextures, + + maxSamples: maxSamples + + }; + +} + +function WebGLClipping( properties ) { + + const scope = this; + + let globalState = null, + numGlobalPlanes = 0, + localClippingEnabled = false, + renderingShadows = false; + + const plane = new Plane(), + viewNormalMatrix = new Matrix3(), + + uniform = { value: null, needsUpdate: false }; + + this.uniform = uniform; + this.numPlanes = 0; + this.numIntersection = 0; + + this.init = function ( planes, enableLocalClipping ) { + + const enabled = + planes.length !== 0 || + enableLocalClipping || + // enable state of previous frame - the clipping code has to + // run another frame in order to reset the state: + numGlobalPlanes !== 0 || + localClippingEnabled; + + localClippingEnabled = enableLocalClipping; + + numGlobalPlanes = planes.length; + + return enabled; + + }; + + this.beginShadows = function () { + + renderingShadows = true; + projectPlanes( null ); + + }; + + this.endShadows = function () { + + renderingShadows = false; + + }; + + this.setGlobalState = function ( planes, camera ) { + + globalState = projectPlanes( planes, camera, 0 ); + + }; + + this.setState = function ( material, camera, useCache ) { + + const planes = material.clippingPlanes, + clipIntersection = material.clipIntersection, + clipShadows = material.clipShadows; + + const materialProperties = properties.get( material ); + + if ( ! localClippingEnabled || planes === null || planes.length === 0 || renderingShadows && ! clipShadows ) { + + // there's no local clipping + + if ( renderingShadows ) { + + // there's no global clipping + + projectPlanes( null ); + + } else { + + resetGlobalState(); + + } + + } else { + + const nGlobal = renderingShadows ? 0 : numGlobalPlanes, + lGlobal = nGlobal * 4; + + let dstArray = materialProperties.clippingState || null; + + uniform.value = dstArray; // ensure unique state + + dstArray = projectPlanes( planes, camera, lGlobal, useCache ); + + for ( let i = 0; i !== lGlobal; ++ i ) { + + dstArray[ i ] = globalState[ i ]; + + } + + materialProperties.clippingState = dstArray; + this.numIntersection = clipIntersection ? this.numPlanes : 0; + this.numPlanes += nGlobal; + + } + + + }; + + function resetGlobalState() { + + if ( uniform.value !== globalState ) { + + uniform.value = globalState; + uniform.needsUpdate = numGlobalPlanes > 0; + + } + + scope.numPlanes = numGlobalPlanes; + scope.numIntersection = 0; + + } + + function projectPlanes( planes, camera, dstOffset, skipTransform ) { + + const nPlanes = planes !== null ? planes.length : 0; + let dstArray = null; + + if ( nPlanes !== 0 ) { + + dstArray = uniform.value; + + if ( skipTransform !== true || dstArray === null ) { + + const flatSize = dstOffset + nPlanes * 4, + viewMatrix = camera.matrixWorldInverse; + + viewNormalMatrix.getNormalMatrix( viewMatrix ); + + if ( dstArray === null || dstArray.length < flatSize ) { + + dstArray = new Float32Array( flatSize ); + + } + + for ( let i = 0, i4 = dstOffset; i !== nPlanes; ++ i, i4 += 4 ) { + + plane.copy( planes[ i ] ).applyMatrix4( viewMatrix, viewNormalMatrix ); + + plane.normal.toArray( dstArray, i4 ); + dstArray[ i4 + 3 ] = plane.constant; + + } + + } + + uniform.value = dstArray; + uniform.needsUpdate = true; + + } + + scope.numPlanes = nPlanes; + scope.numIntersection = 0; + + return dstArray; + + } + +} + +function WebGLCubeMaps( renderer ) { + + let cubemaps = new WeakMap(); + + function mapTextureMapping( texture, mapping ) { + + if ( mapping === EquirectangularReflectionMapping ) { + + texture.mapping = CubeReflectionMapping; + + } else if ( mapping === EquirectangularRefractionMapping ) { + + texture.mapping = CubeRefractionMapping; + + } + + return texture; + + } + + function get( texture ) { + + if ( texture && texture.isTexture ) { + + const mapping = texture.mapping; + + if ( mapping === EquirectangularReflectionMapping || mapping === EquirectangularRefractionMapping ) { + + if ( cubemaps.has( texture ) ) { + + const cubemap = cubemaps.get( texture ).texture; + return mapTextureMapping( cubemap, texture.mapping ); + + } else { + + const image = texture.image; + + if ( image && image.height > 0 ) { + + const renderTarget = new WebGLCubeRenderTarget( image.height ); + renderTarget.fromEquirectangularTexture( renderer, texture ); + cubemaps.set( texture, renderTarget ); + + texture.addEventListener( 'dispose', onTextureDispose ); + + return mapTextureMapping( renderTarget.texture, texture.mapping ); + + } else { + + // image not yet ready. try the conversion next frame + + return null; + + } + + } + + } + + } + + return texture; + + } + + function onTextureDispose( event ) { + + const texture = event.target; + + texture.removeEventListener( 'dispose', onTextureDispose ); + + const cubemap = cubemaps.get( texture ); + + if ( cubemap !== undefined ) { + + cubemaps.delete( texture ); + cubemap.dispose(); + + } + + } + + function dispose() { + + cubemaps = new WeakMap(); + + } + + return { + get: get, + dispose: dispose + }; + +} + +class OrthographicCamera extends Camera { + + constructor( left = - 1, right = 1, top = 1, bottom = - 1, near = 0.1, far = 2000 ) { + + super(); + + this.isOrthographicCamera = true; + + this.type = 'OrthographicCamera'; + + this.zoom = 1; + this.view = null; + + this.left = left; + this.right = right; + this.top = top; + this.bottom = bottom; + + this.near = near; + this.far = far; + + this.updateProjectionMatrix(); + + } + + copy( source, recursive ) { + + super.copy( source, recursive ); + + this.left = source.left; + this.right = source.right; + this.top = source.top; + this.bottom = source.bottom; + this.near = source.near; + this.far = source.far; + + this.zoom = source.zoom; + this.view = source.view === null ? null : Object.assign( {}, source.view ); + + return this; + + } + + setViewOffset( fullWidth, fullHeight, x, y, width, height ) { + + if ( this.view === null ) { + + this.view = { + enabled: true, + fullWidth: 1, + fullHeight: 1, + offsetX: 0, + offsetY: 0, + width: 1, + height: 1 + }; + + } + + this.view.enabled = true; + this.view.fullWidth = fullWidth; + this.view.fullHeight = fullHeight; + this.view.offsetX = x; + this.view.offsetY = y; + this.view.width = width; + this.view.height = height; + + this.updateProjectionMatrix(); + + } + + clearViewOffset() { + + if ( this.view !== null ) { + + this.view.enabled = false; + + } + + this.updateProjectionMatrix(); + + } + + updateProjectionMatrix() { + + const dx = ( this.right - this.left ) / ( 2 * this.zoom ); + const dy = ( this.top - this.bottom ) / ( 2 * this.zoom ); + const cx = ( this.right + this.left ) / 2; + const cy = ( this.top + this.bottom ) / 2; + + let left = cx - dx; + let right = cx + dx; + let top = cy + dy; + let bottom = cy - dy; + + if ( this.view !== null && this.view.enabled ) { + + const scaleW = ( this.right - this.left ) / this.view.fullWidth / this.zoom; + const scaleH = ( this.top - this.bottom ) / this.view.fullHeight / this.zoom; + + left += scaleW * this.view.offsetX; + right = left + scaleW * this.view.width; + top -= scaleH * this.view.offsetY; + bottom = top - scaleH * this.view.height; + + } + + this.projectionMatrix.makeOrthographic( left, right, top, bottom, this.near, this.far, this.coordinateSystem ); + + this.projectionMatrixInverse.copy( this.projectionMatrix ).invert(); + + } + + toJSON( meta ) { + + const data = super.toJSON( meta ); + + data.object.zoom = this.zoom; + data.object.left = this.left; + data.object.right = this.right; + data.object.top = this.top; + data.object.bottom = this.bottom; + data.object.near = this.near; + data.object.far = this.far; + + if ( this.view !== null ) data.object.view = Object.assign( {}, this.view ); + + return data; + + } + +} + +const LOD_MIN = 4; + +// The standard deviations (radians) associated with the extra mips. These are +// chosen to approximate a Trowbridge-Reitz distribution function times the +// geometric shadowing function. These sigma values squared must match the +// variance #defines in cube_uv_reflection_fragment.glsl.js. +const EXTRA_LOD_SIGMA = [ 0.125, 0.215, 0.35, 0.446, 0.526, 0.582 ]; + +// The maximum length of the blur for loop. Smaller sigmas will use fewer +// samples and exit early, but not recompile the shader. +const MAX_SAMPLES = 20; + +const _flatCamera = /*@__PURE__*/ new OrthographicCamera(); +const _clearColor = /*@__PURE__*/ new Color(); +let _oldTarget = null; +let _oldActiveCubeFace = 0; +let _oldActiveMipmapLevel = 0; +let _oldXrEnabled = false; + +// Golden Ratio +const PHI = ( 1 + Math.sqrt( 5 ) ) / 2; +const INV_PHI = 1 / PHI; + +// Vertices of a dodecahedron (except the opposites, which represent the +// same axis), used as axis directions evenly spread on a sphere. +const _axisDirections = [ + /*@__PURE__*/ new Vector3( - PHI, INV_PHI, 0 ), + /*@__PURE__*/ new Vector3( PHI, INV_PHI, 0 ), + /*@__PURE__*/ new Vector3( - INV_PHI, 0, PHI ), + /*@__PURE__*/ new Vector3( INV_PHI, 0, PHI ), + /*@__PURE__*/ new Vector3( 0, PHI, - INV_PHI ), + /*@__PURE__*/ new Vector3( 0, PHI, INV_PHI ), + /*@__PURE__*/ new Vector3( - 1, 1, - 1 ), + /*@__PURE__*/ new Vector3( 1, 1, - 1 ), + /*@__PURE__*/ new Vector3( - 1, 1, 1 ), + /*@__PURE__*/ new Vector3( 1, 1, 1 ) ]; + +/** + * This class generates a Prefiltered, Mipmapped Radiance Environment Map + * (PMREM) from a cubeMap environment texture. This allows different levels of + * blur to be quickly accessed based on material roughness. It is packed into a + * special CubeUV format that allows us to perform custom interpolation so that + * we can support nonlinear formats such as RGBE. Unlike a traditional mipmap + * chain, it only goes down to the LOD_MIN level (above), and then creates extra + * even more filtered 'mips' at the same LOD_MIN resolution, associated with + * higher roughness levels. In this way we maintain resolution to smoothly + * interpolate diffuse lighting while limiting sampling computation. + * + * Paper: Fast, Accurate Image-Based Lighting + * https://drive.google.com/file/d/15y8r_UpKlU9SvV4ILb0C3qCPecS8pvLz/view +*/ + +class PMREMGenerator { + + constructor( renderer ) { + + this._renderer = renderer; + this._pingPongRenderTarget = null; + + this._lodMax = 0; + this._cubeSize = 0; + this._lodPlanes = []; + this._sizeLods = []; + this._sigmas = []; + + this._blurMaterial = null; + this._cubemapMaterial = null; + this._equirectMaterial = null; + + this._compileMaterial( this._blurMaterial ); + + } + + /** + * Generates a PMREM from a supplied Scene, which can be faster than using an + * image if networking bandwidth is low. Optional sigma specifies a blur radius + * in radians to be applied to the scene before PMREM generation. Optional near + * and far planes ensure the scene is rendered in its entirety (the cubeCamera + * is placed at the origin). + */ + fromScene( scene, sigma = 0, near = 0.1, far = 100 ) { + + _oldTarget = this._renderer.getRenderTarget(); + _oldActiveCubeFace = this._renderer.getActiveCubeFace(); + _oldActiveMipmapLevel = this._renderer.getActiveMipmapLevel(); + _oldXrEnabled = this._renderer.xr.enabled; + + this._renderer.xr.enabled = false; + + this._setSize( 256 ); + + const cubeUVRenderTarget = this._allocateTargets(); + cubeUVRenderTarget.depthBuffer = true; + + this._sceneToCubeUV( scene, near, far, cubeUVRenderTarget ); + + if ( sigma > 0 ) { + + this._blur( cubeUVRenderTarget, 0, 0, sigma ); + + } + + this._applyPMREM( cubeUVRenderTarget ); + this._cleanup( cubeUVRenderTarget ); + + return cubeUVRenderTarget; + + } + + /** + * Generates a PMREM from an equirectangular texture, which can be either LDR + * or HDR. The ideal input image size is 1k (1024 x 512), + * as this matches best with the 256 x 256 cubemap output. + * The smallest supported equirectangular image size is 64 x 32. + */ + fromEquirectangular( equirectangular, renderTarget = null ) { + + return this._fromTexture( equirectangular, renderTarget ); + + } + + /** + * Generates a PMREM from an cubemap texture, which can be either LDR + * or HDR. The ideal input cube size is 256 x 256, + * as this matches best with the 256 x 256 cubemap output. + * The smallest supported cube size is 16 x 16. + */ + fromCubemap( cubemap, renderTarget = null ) { + + return this._fromTexture( cubemap, renderTarget ); + + } + + /** + * Pre-compiles the cubemap shader. You can get faster start-up by invoking this method during + * your texture's network fetch for increased concurrency. + */ + compileCubemapShader() { + + if ( this._cubemapMaterial === null ) { + + this._cubemapMaterial = _getCubemapMaterial(); + this._compileMaterial( this._cubemapMaterial ); + + } + + } + + /** + * Pre-compiles the equirectangular shader. You can get faster start-up by invoking this method during + * your texture's network fetch for increased concurrency. + */ + compileEquirectangularShader() { + + if ( this._equirectMaterial === null ) { + + this._equirectMaterial = _getEquirectMaterial(); + this._compileMaterial( this._equirectMaterial ); + + } + + } + + /** + * Disposes of the PMREMGenerator's internal memory. Note that PMREMGenerator is a static class, + * so you should not need more than one PMREMGenerator object. If you do, calling dispose() on + * one of them will cause any others to also become unusable. + */ + dispose() { + + this._dispose(); + + if ( this._cubemapMaterial !== null ) this._cubemapMaterial.dispose(); + if ( this._equirectMaterial !== null ) this._equirectMaterial.dispose(); + + } + + // private interface + + _setSize( cubeSize ) { + + this._lodMax = Math.floor( Math.log2( cubeSize ) ); + this._cubeSize = Math.pow( 2, this._lodMax ); + + } + + _dispose() { + + if ( this._blurMaterial !== null ) this._blurMaterial.dispose(); + + if ( this._pingPongRenderTarget !== null ) this._pingPongRenderTarget.dispose(); + + for ( let i = 0; i < this._lodPlanes.length; i ++ ) { + + this._lodPlanes[ i ].dispose(); + + } + + } + + _cleanup( outputTarget ) { + + this._renderer.setRenderTarget( _oldTarget, _oldActiveCubeFace, _oldActiveMipmapLevel ); + this._renderer.xr.enabled = _oldXrEnabled; + + outputTarget.scissorTest = false; + _setViewport( outputTarget, 0, 0, outputTarget.width, outputTarget.height ); + + } + + _fromTexture( texture, renderTarget ) { + + if ( texture.mapping === CubeReflectionMapping || texture.mapping === CubeRefractionMapping ) { + + this._setSize( texture.image.length === 0 ? 16 : ( texture.image[ 0 ].width || texture.image[ 0 ].image.width ) ); + + } else { // Equirectangular + + this._setSize( texture.image.width / 4 ); + + } + + _oldTarget = this._renderer.getRenderTarget(); + _oldActiveCubeFace = this._renderer.getActiveCubeFace(); + _oldActiveMipmapLevel = this._renderer.getActiveMipmapLevel(); + _oldXrEnabled = this._renderer.xr.enabled; + + this._renderer.xr.enabled = false; + + const cubeUVRenderTarget = renderTarget || this._allocateTargets(); + this._textureToCubeUV( texture, cubeUVRenderTarget ); + this._applyPMREM( cubeUVRenderTarget ); + this._cleanup( cubeUVRenderTarget ); + + return cubeUVRenderTarget; + + } + + _allocateTargets() { + + const width = 3 * Math.max( this._cubeSize, 16 * 7 ); + const height = 4 * this._cubeSize; + + const params = { + magFilter: LinearFilter, + minFilter: LinearFilter, + generateMipmaps: false, + type: HalfFloatType, + format: RGBAFormat, + colorSpace: LinearSRGBColorSpace, + depthBuffer: false + }; + + const cubeUVRenderTarget = _createRenderTarget( width, height, params ); + + if ( this._pingPongRenderTarget === null || this._pingPongRenderTarget.width !== width || this._pingPongRenderTarget.height !== height ) { + + if ( this._pingPongRenderTarget !== null ) { + + this._dispose(); + + } + + this._pingPongRenderTarget = _createRenderTarget( width, height, params ); + + const { _lodMax } = this; + ( { sizeLods: this._sizeLods, lodPlanes: this._lodPlanes, sigmas: this._sigmas } = _createPlanes( _lodMax ) ); + + this._blurMaterial = _getBlurShader( _lodMax, width, height ); + + } + + return cubeUVRenderTarget; + + } + + _compileMaterial( material ) { + + const tmpMesh = new Mesh( this._lodPlanes[ 0 ], material ); + this._renderer.compile( tmpMesh, _flatCamera ); + + } + + _sceneToCubeUV( scene, near, far, cubeUVRenderTarget ) { + + const fov = 90; + const aspect = 1; + const cubeCamera = new PerspectiveCamera( fov, aspect, near, far ); + const upSign = [ 1, - 1, 1, 1, 1, 1 ]; + const forwardSign = [ 1, 1, 1, - 1, - 1, - 1 ]; + const renderer = this._renderer; + + const originalAutoClear = renderer.autoClear; + const toneMapping = renderer.toneMapping; + renderer.getClearColor( _clearColor ); + + renderer.toneMapping = NoToneMapping; + renderer.autoClear = false; + + const backgroundMaterial = new MeshBasicMaterial( { + name: 'PMREM.Background', + side: BackSide, + depthWrite: false, + depthTest: false, + } ); + + const backgroundBox = new Mesh( new BoxGeometry(), backgroundMaterial ); + + let useSolidColor = false; + const background = scene.background; + + if ( background ) { + + if ( background.isColor ) { + + backgroundMaterial.color.copy( background ); + scene.background = null; + useSolidColor = true; + + } + + } else { + + backgroundMaterial.color.copy( _clearColor ); + useSolidColor = true; + + } + + for ( let i = 0; i < 6; i ++ ) { + + const col = i % 3; + + if ( col === 0 ) { + + cubeCamera.up.set( 0, upSign[ i ], 0 ); + cubeCamera.lookAt( forwardSign[ i ], 0, 0 ); + + } else if ( col === 1 ) { + + cubeCamera.up.set( 0, 0, upSign[ i ] ); + cubeCamera.lookAt( 0, forwardSign[ i ], 0 ); + + } else { + + cubeCamera.up.set( 0, upSign[ i ], 0 ); + cubeCamera.lookAt( 0, 0, forwardSign[ i ] ); + + } + + const size = this._cubeSize; + + _setViewport( cubeUVRenderTarget, col * size, i > 2 ? size : 0, size, size ); + + renderer.setRenderTarget( cubeUVRenderTarget ); + + if ( useSolidColor ) { + + renderer.render( backgroundBox, cubeCamera ); + + } + + renderer.render( scene, cubeCamera ); + + } + + backgroundBox.geometry.dispose(); + backgroundBox.material.dispose(); + + renderer.toneMapping = toneMapping; + renderer.autoClear = originalAutoClear; + scene.background = background; + + } + + _textureToCubeUV( texture, cubeUVRenderTarget ) { + + const renderer = this._renderer; + + const isCubeTexture = ( texture.mapping === CubeReflectionMapping || texture.mapping === CubeRefractionMapping ); + + if ( isCubeTexture ) { + + if ( this._cubemapMaterial === null ) { + + this._cubemapMaterial = _getCubemapMaterial(); + + } + + this._cubemapMaterial.uniforms.flipEnvMap.value = ( texture.isRenderTargetTexture === false ) ? - 1 : 1; + + } else { + + if ( this._equirectMaterial === null ) { + + this._equirectMaterial = _getEquirectMaterial(); + + } + + } + + const material = isCubeTexture ? this._cubemapMaterial : this._equirectMaterial; + const mesh = new Mesh( this._lodPlanes[ 0 ], material ); + + const uniforms = material.uniforms; + + uniforms[ 'envMap' ].value = texture; + + const size = this._cubeSize; + + _setViewport( cubeUVRenderTarget, 0, 0, 3 * size, 2 * size ); + + renderer.setRenderTarget( cubeUVRenderTarget ); + renderer.render( mesh, _flatCamera ); + + } + + _applyPMREM( cubeUVRenderTarget ) { + + const renderer = this._renderer; + const autoClear = renderer.autoClear; + renderer.autoClear = false; + const n = this._lodPlanes.length; + + for ( let i = 1; i < n; i ++ ) { + + const sigma = Math.sqrt( this._sigmas[ i ] * this._sigmas[ i ] - this._sigmas[ i - 1 ] * this._sigmas[ i - 1 ] ); + + const poleAxis = _axisDirections[ ( n - i - 1 ) % _axisDirections.length ]; + + this._blur( cubeUVRenderTarget, i - 1, i, sigma, poleAxis ); + + } + + renderer.autoClear = autoClear; + + } + + /** + * This is a two-pass Gaussian blur for a cubemap. Normally this is done + * vertically and horizontally, but this breaks down on a cube. Here we apply + * the blur latitudinally (around the poles), and then longitudinally (towards + * the poles) to approximate the orthogonally-separable blur. It is least + * accurate at the poles, but still does a decent job. + */ + _blur( cubeUVRenderTarget, lodIn, lodOut, sigma, poleAxis ) { + + const pingPongRenderTarget = this._pingPongRenderTarget; + + this._halfBlur( + cubeUVRenderTarget, + pingPongRenderTarget, + lodIn, + lodOut, + sigma, + 'latitudinal', + poleAxis ); + + this._halfBlur( + pingPongRenderTarget, + cubeUVRenderTarget, + lodOut, + lodOut, + sigma, + 'longitudinal', + poleAxis ); + + } + + _halfBlur( targetIn, targetOut, lodIn, lodOut, sigmaRadians, direction, poleAxis ) { + + const renderer = this._renderer; + const blurMaterial = this._blurMaterial; + + if ( direction !== 'latitudinal' && direction !== 'longitudinal' ) { + + console.error( + 'blur direction must be either latitudinal or longitudinal!' ); + + } + + // Number of standard deviations at which to cut off the discrete approximation. + const STANDARD_DEVIATIONS = 3; + + const blurMesh = new Mesh( this._lodPlanes[ lodOut ], blurMaterial ); + const blurUniforms = blurMaterial.uniforms; + + const pixels = this._sizeLods[ lodIn ] - 1; + const radiansPerPixel = isFinite( sigmaRadians ) ? Math.PI / ( 2 * pixels ) : 2 * Math.PI / ( 2 * MAX_SAMPLES - 1 ); + const sigmaPixels = sigmaRadians / radiansPerPixel; + const samples = isFinite( sigmaRadians ) ? 1 + Math.floor( STANDARD_DEVIATIONS * sigmaPixels ) : MAX_SAMPLES; + + if ( samples > MAX_SAMPLES ) { + + console.warn( `sigmaRadians, ${ + sigmaRadians}, is too large and will clip, as it requested ${ + samples} samples when the maximum is set to ${MAX_SAMPLES}` ); + + } + + const weights = []; + let sum = 0; + + for ( let i = 0; i < MAX_SAMPLES; ++ i ) { + + const x = i / sigmaPixels; + const weight = Math.exp( - x * x / 2 ); + weights.push( weight ); + + if ( i === 0 ) { + + sum += weight; + + } else if ( i < samples ) { + + sum += 2 * weight; + + } + + } + + for ( let i = 0; i < weights.length; i ++ ) { + + weights[ i ] = weights[ i ] / sum; + + } + + blurUniforms[ 'envMap' ].value = targetIn.texture; + blurUniforms[ 'samples' ].value = samples; + blurUniforms[ 'weights' ].value = weights; + blurUniforms[ 'latitudinal' ].value = direction === 'latitudinal'; + + if ( poleAxis ) { + + blurUniforms[ 'poleAxis' ].value = poleAxis; + + } + + const { _lodMax } = this; + blurUniforms[ 'dTheta' ].value = radiansPerPixel; + blurUniforms[ 'mipInt' ].value = _lodMax - lodIn; + + const outputSize = this._sizeLods[ lodOut ]; + const x = 3 * outputSize * ( lodOut > _lodMax - LOD_MIN ? lodOut - _lodMax + LOD_MIN : 0 ); + const y = 4 * ( this._cubeSize - outputSize ); + + _setViewport( targetOut, x, y, 3 * outputSize, 2 * outputSize ); + renderer.setRenderTarget( targetOut ); + renderer.render( blurMesh, _flatCamera ); + + } + +} + + + +function _createPlanes( lodMax ) { + + const lodPlanes = []; + const sizeLods = []; + const sigmas = []; + + let lod = lodMax; + + const totalLods = lodMax - LOD_MIN + 1 + EXTRA_LOD_SIGMA.length; + + for ( let i = 0; i < totalLods; i ++ ) { + + const sizeLod = Math.pow( 2, lod ); + sizeLods.push( sizeLod ); + let sigma = 1.0 / sizeLod; + + if ( i > lodMax - LOD_MIN ) { + + sigma = EXTRA_LOD_SIGMA[ i - lodMax + LOD_MIN - 1 ]; + + } else if ( i === 0 ) { + + sigma = 0; + + } + + sigmas.push( sigma ); + + const texelSize = 1.0 / ( sizeLod - 2 ); + const min = - texelSize; + const max = 1 + texelSize; + const uv1 = [ min, min, max, min, max, max, min, min, max, max, min, max ]; + + const cubeFaces = 6; + const vertices = 6; + const positionSize = 3; + const uvSize = 2; + const faceIndexSize = 1; + + const position = new Float32Array( positionSize * vertices * cubeFaces ); + const uv = new Float32Array( uvSize * vertices * cubeFaces ); + const faceIndex = new Float32Array( faceIndexSize * vertices * cubeFaces ); + + for ( let face = 0; face < cubeFaces; face ++ ) { + + const x = ( face % 3 ) * 2 / 3 - 1; + const y = face > 2 ? 0 : - 1; + const coordinates = [ + x, y, 0, + x + 2 / 3, y, 0, + x + 2 / 3, y + 1, 0, + x, y, 0, + x + 2 / 3, y + 1, 0, + x, y + 1, 0 + ]; + position.set( coordinates, positionSize * vertices * face ); + uv.set( uv1, uvSize * vertices * face ); + const fill = [ face, face, face, face, face, face ]; + faceIndex.set( fill, faceIndexSize * vertices * face ); + + } + + const planes = new BufferGeometry(); + planes.setAttribute( 'position', new BufferAttribute( position, positionSize ) ); + planes.setAttribute( 'uv', new BufferAttribute( uv, uvSize ) ); + planes.setAttribute( 'faceIndex', new BufferAttribute( faceIndex, faceIndexSize ) ); + lodPlanes.push( planes ); + + if ( lod > LOD_MIN ) { + + lod --; + + } + + } + + return { lodPlanes, sizeLods, sigmas }; + +} + +function _createRenderTarget( width, height, params ) { + + const cubeUVRenderTarget = new WebGLRenderTarget( width, height, params ); + cubeUVRenderTarget.texture.mapping = CubeUVReflectionMapping; + cubeUVRenderTarget.texture.name = 'PMREM.cubeUv'; + cubeUVRenderTarget.scissorTest = true; + return cubeUVRenderTarget; + +} + +function _setViewport( target, x, y, width, height ) { + + target.viewport.set( x, y, width, height ); + target.scissor.set( x, y, width, height ); + +} + +function _getBlurShader( lodMax, width, height ) { + + const weights = new Float32Array( MAX_SAMPLES ); + const poleAxis = new Vector3( 0, 1, 0 ); + const shaderMaterial = new ShaderMaterial( { + + name: 'SphericalGaussianBlur', + + defines: { + 'n': MAX_SAMPLES, + 'CUBEUV_TEXEL_WIDTH': 1.0 / width, + 'CUBEUV_TEXEL_HEIGHT': 1.0 / height, + 'CUBEUV_MAX_MIP': `${lodMax}.0`, + }, + + uniforms: { + 'envMap': { value: null }, + 'samples': { value: 1 }, + 'weights': { value: weights }, + 'latitudinal': { value: false }, + 'dTheta': { value: 0 }, + 'mipInt': { value: 0 }, + 'poleAxis': { value: poleAxis } + }, + + vertexShader: _getCommonVertexShader(), + + fragmentShader: /* glsl */` + + precision mediump float; + precision mediump int; + + varying vec3 vOutputDirection; + + uniform sampler2D envMap; + uniform int samples; + uniform float weights[ n ]; + uniform bool latitudinal; + uniform float dTheta; + uniform float mipInt; + uniform vec3 poleAxis; + + #define ENVMAP_TYPE_CUBE_UV + #include + + vec3 getSample( float theta, vec3 axis ) { + + float cosTheta = cos( theta ); + // Rodrigues' axis-angle rotation + vec3 sampleDirection = vOutputDirection * cosTheta + + cross( axis, vOutputDirection ) * sin( theta ) + + axis * dot( axis, vOutputDirection ) * ( 1.0 - cosTheta ); + + return bilinearCubeUV( envMap, sampleDirection, mipInt ); + + } + + void main() { + + vec3 axis = latitudinal ? poleAxis : cross( poleAxis, vOutputDirection ); + + if ( all( equal( axis, vec3( 0.0 ) ) ) ) { + + axis = vec3( vOutputDirection.z, 0.0, - vOutputDirection.x ); + + } + + axis = normalize( axis ); + + gl_FragColor = vec4( 0.0, 0.0, 0.0, 1.0 ); + gl_FragColor.rgb += weights[ 0 ] * getSample( 0.0, axis ); + + for ( int i = 1; i < n; i++ ) { + + if ( i >= samples ) { + + break; + + } + + float theta = dTheta * float( i ); + gl_FragColor.rgb += weights[ i ] * getSample( -1.0 * theta, axis ); + gl_FragColor.rgb += weights[ i ] * getSample( theta, axis ); + + } + + } + `, + + blending: NoBlending, + depthTest: false, + depthWrite: false + + } ); + + return shaderMaterial; + +} + +function _getEquirectMaterial() { + + return new ShaderMaterial( { + + name: 'EquirectangularToCubeUV', + + uniforms: { + 'envMap': { value: null } + }, + + vertexShader: _getCommonVertexShader(), + + fragmentShader: /* glsl */` + + precision mediump float; + precision mediump int; + + varying vec3 vOutputDirection; + + uniform sampler2D envMap; + + #include + + void main() { + + vec3 outputDirection = normalize( vOutputDirection ); + vec2 uv = equirectUv( outputDirection ); + + gl_FragColor = vec4( texture2D ( envMap, uv ).rgb, 1.0 ); + + } + `, + + blending: NoBlending, + depthTest: false, + depthWrite: false + + } ); + +} + +function _getCubemapMaterial() { + + return new ShaderMaterial( { + + name: 'CubemapToCubeUV', + + uniforms: { + 'envMap': { value: null }, + 'flipEnvMap': { value: - 1 } + }, + + vertexShader: _getCommonVertexShader(), + + fragmentShader: /* glsl */` + + precision mediump float; + precision mediump int; + + uniform float flipEnvMap; + + varying vec3 vOutputDirection; + + uniform samplerCube envMap; + + void main() { + + gl_FragColor = textureCube( envMap, vec3( flipEnvMap * vOutputDirection.x, vOutputDirection.yz ) ); + + } + `, + + blending: NoBlending, + depthTest: false, + depthWrite: false + + } ); + +} + +function _getCommonVertexShader() { + + return /* glsl */` + + precision mediump float; + precision mediump int; + + attribute float faceIndex; + + varying vec3 vOutputDirection; + + // RH coordinate system; PMREM face-indexing convention + vec3 getDirection( vec2 uv, float face ) { + + uv = 2.0 * uv - 1.0; + + vec3 direction = vec3( uv, 1.0 ); + + if ( face == 0.0 ) { + + direction = direction.zyx; // ( 1, v, u ) pos x + + } else if ( face == 1.0 ) { + + direction = direction.xzy; + direction.xz *= -1.0; // ( -u, 1, -v ) pos y + + } else if ( face == 2.0 ) { + + direction.x *= -1.0; // ( -u, v, 1 ) pos z + + } else if ( face == 3.0 ) { + + direction = direction.zyx; + direction.xz *= -1.0; // ( -1, v, -u ) neg x + + } else if ( face == 4.0 ) { + + direction = direction.xzy; + direction.xy *= -1.0; // ( -u, -1, v ) neg y + + } else if ( face == 5.0 ) { + + direction.z *= -1.0; // ( u, v, -1 ) neg z + + } + + return direction; + + } + + void main() { + + vOutputDirection = getDirection( uv, faceIndex ); + gl_Position = vec4( position, 1.0 ); + + } + `; + +} + +function WebGLCubeUVMaps( renderer ) { + + let cubeUVmaps = new WeakMap(); + + let pmremGenerator = null; + + function get( texture ) { + + if ( texture && texture.isTexture ) { + + const mapping = texture.mapping; + + const isEquirectMap = ( mapping === EquirectangularReflectionMapping || mapping === EquirectangularRefractionMapping ); + const isCubeMap = ( mapping === CubeReflectionMapping || mapping === CubeRefractionMapping ); + + // equirect/cube map to cubeUV conversion + + if ( isEquirectMap || isCubeMap ) { + + let renderTarget = cubeUVmaps.get( texture ); + + const currentPMREMVersion = renderTarget !== undefined ? renderTarget.texture.pmremVersion : 0; + + if ( texture.isRenderTargetTexture && texture.pmremVersion !== currentPMREMVersion ) { + + if ( pmremGenerator === null ) pmremGenerator = new PMREMGenerator( renderer ); + + renderTarget = isEquirectMap ? pmremGenerator.fromEquirectangular( texture, renderTarget ) : pmremGenerator.fromCubemap( texture, renderTarget ); + renderTarget.texture.pmremVersion = texture.pmremVersion; + + cubeUVmaps.set( texture, renderTarget ); + + return renderTarget.texture; + + } else { + + if ( renderTarget !== undefined ) { + + return renderTarget.texture; + + } else { + + const image = texture.image; + + if ( ( isEquirectMap && image && image.height > 0 ) || ( isCubeMap && image && isCubeTextureComplete( image ) ) ) { + + if ( pmremGenerator === null ) pmremGenerator = new PMREMGenerator( renderer ); + + renderTarget = isEquirectMap ? pmremGenerator.fromEquirectangular( texture ) : pmremGenerator.fromCubemap( texture ); + renderTarget.texture.pmremVersion = texture.pmremVersion; + + cubeUVmaps.set( texture, renderTarget ); + + texture.addEventListener( 'dispose', onTextureDispose ); + + return renderTarget.texture; + + } else { + + // image not yet ready. try the conversion next frame + + return null; + + } + + } + + } + + } + + } + + return texture; + + } + + function isCubeTextureComplete( image ) { + + let count = 0; + const length = 6; + + for ( let i = 0; i < length; i ++ ) { + + if ( image[ i ] !== undefined ) count ++; + + } + + return count === length; + + + } + + function onTextureDispose( event ) { + + const texture = event.target; + + texture.removeEventListener( 'dispose', onTextureDispose ); + + const cubemapUV = cubeUVmaps.get( texture ); + + if ( cubemapUV !== undefined ) { + + cubeUVmaps.delete( texture ); + cubemapUV.dispose(); + + } + + } + + function dispose() { + + cubeUVmaps = new WeakMap(); + + if ( pmremGenerator !== null ) { + + pmremGenerator.dispose(); + pmremGenerator = null; + + } + + } + + return { + get: get, + dispose: dispose + }; + +} + +function WebGLExtensions( gl ) { + + const extensions = {}; + + function getExtension( name ) { + + if ( extensions[ name ] !== undefined ) { + + return extensions[ name ]; + + } + + let extension; + + switch ( name ) { + + case 'WEBGL_depth_texture': + extension = gl.getExtension( 'WEBGL_depth_texture' ) || gl.getExtension( 'MOZ_WEBGL_depth_texture' ) || gl.getExtension( 'WEBKIT_WEBGL_depth_texture' ); + break; + + case 'EXT_texture_filter_anisotropic': + extension = gl.getExtension( 'EXT_texture_filter_anisotropic' ) || gl.getExtension( 'MOZ_EXT_texture_filter_anisotropic' ) || gl.getExtension( 'WEBKIT_EXT_texture_filter_anisotropic' ); + break; + + case 'WEBGL_compressed_texture_s3tc': + extension = gl.getExtension( 'WEBGL_compressed_texture_s3tc' ) || gl.getExtension( 'MOZ_WEBGL_compressed_texture_s3tc' ) || gl.getExtension( 'WEBKIT_WEBGL_compressed_texture_s3tc' ); + break; + + case 'WEBGL_compressed_texture_pvrtc': + extension = gl.getExtension( 'WEBGL_compressed_texture_pvrtc' ) || gl.getExtension( 'WEBKIT_WEBGL_compressed_texture_pvrtc' ); + break; + + default: + extension = gl.getExtension( name ); + + } + + extensions[ name ] = extension; + + return extension; + + } + + return { + + has: function ( name ) { + + return getExtension( name ) !== null; + + }, + + init: function () { + + getExtension( 'EXT_color_buffer_float' ); + getExtension( 'WEBGL_clip_cull_distance' ); + getExtension( 'OES_texture_float_linear' ); + getExtension( 'EXT_color_buffer_half_float' ); + getExtension( 'WEBGL_multisampled_render_to_texture' ); + getExtension( 'WEBGL_render_shared_exponent' ); + + }, + + get: function ( name ) { + + const extension = getExtension( name ); + + if ( extension === null ) { + + console.warn( 'THREE.WebGLRenderer: ' + name + ' extension not supported.' ); + + } + + return extension; + + } + + }; + +} + +function WebGLGeometries( gl, attributes, info, bindingStates ) { + + const geometries = {}; + const wireframeAttributes = new WeakMap(); + + function onGeometryDispose( event ) { + + const geometry = event.target; + + if ( geometry.index !== null ) { + + attributes.remove( geometry.index ); + + } + + for ( const name in geometry.attributes ) { + + attributes.remove( geometry.attributes[ name ] ); + + } + + for ( const name in geometry.morphAttributes ) { + + const array = geometry.morphAttributes[ name ]; + + for ( let i = 0, l = array.length; i < l; i ++ ) { + + attributes.remove( array[ i ] ); + + } + + } + + geometry.removeEventListener( 'dispose', onGeometryDispose ); + + delete geometries[ geometry.id ]; + + const attribute = wireframeAttributes.get( geometry ); + + if ( attribute ) { + + attributes.remove( attribute ); + wireframeAttributes.delete( geometry ); + + } + + bindingStates.releaseStatesOfGeometry( geometry ); + + if ( geometry.isInstancedBufferGeometry === true ) { + + delete geometry._maxInstanceCount; + + } + + // + + info.memory.geometries --; + + } + + function get( object, geometry ) { + + if ( geometries[ geometry.id ] === true ) return geometry; + + geometry.addEventListener( 'dispose', onGeometryDispose ); + + geometries[ geometry.id ] = true; + + info.memory.geometries ++; + + return geometry; + + } + + function update( geometry ) { + + const geometryAttributes = geometry.attributes; + + // Updating index buffer in VAO now. See WebGLBindingStates. + + for ( const name in geometryAttributes ) { + + attributes.update( geometryAttributes[ name ], gl.ARRAY_BUFFER ); + + } + + // morph targets + + const morphAttributes = geometry.morphAttributes; + + for ( const name in morphAttributes ) { + + const array = morphAttributes[ name ]; + + for ( let i = 0, l = array.length; i < l; i ++ ) { + + attributes.update( array[ i ], gl.ARRAY_BUFFER ); + + } + + } + + } + + function updateWireframeAttribute( geometry ) { + + const indices = []; + + const geometryIndex = geometry.index; + const geometryPosition = geometry.attributes.position; + let version = 0; + + if ( geometryIndex !== null ) { + + const array = geometryIndex.array; + version = geometryIndex.version; + + for ( let i = 0, l = array.length; i < l; i += 3 ) { + + const a = array[ i + 0 ]; + const b = array[ i + 1 ]; + const c = array[ i + 2 ]; + + indices.push( a, b, b, c, c, a ); + + } + + } else if ( geometryPosition !== undefined ) { + + const array = geometryPosition.array; + version = geometryPosition.version; + + for ( let i = 0, l = ( array.length / 3 ) - 1; i < l; i += 3 ) { + + const a = i + 0; + const b = i + 1; + const c = i + 2; + + indices.push( a, b, b, c, c, a ); + + } + + } else { + + return; + + } + + const attribute = new ( arrayNeedsUint32( indices ) ? Uint32BufferAttribute : Uint16BufferAttribute )( indices, 1 ); + attribute.version = version; + + // Updating index buffer in VAO now. See WebGLBindingStates + + // + + const previousAttribute = wireframeAttributes.get( geometry ); + + if ( previousAttribute ) attributes.remove( previousAttribute ); + + // + + wireframeAttributes.set( geometry, attribute ); + + } + + function getWireframeAttribute( geometry ) { + + const currentAttribute = wireframeAttributes.get( geometry ); + + if ( currentAttribute ) { + + const geometryIndex = geometry.index; + + if ( geometryIndex !== null ) { + + // if the attribute is obsolete, create a new one + + if ( currentAttribute.version < geometryIndex.version ) { + + updateWireframeAttribute( geometry ); + + } + + } + + } else { + + updateWireframeAttribute( geometry ); + + } + + return wireframeAttributes.get( geometry ); + + } + + return { + + get: get, + update: update, + + getWireframeAttribute: getWireframeAttribute + + }; + +} + +function WebGLIndexedBufferRenderer( gl, extensions, info ) { + + let mode; + + function setMode( value ) { + + mode = value; + + } + + let type, bytesPerElement; + + function setIndex( value ) { + + type = value.type; + bytesPerElement = value.bytesPerElement; + + } + + function render( start, count ) { + + gl.drawElements( mode, count, type, start * bytesPerElement ); + + info.update( count, mode, 1 ); + + } + + function renderInstances( start, count, primcount ) { + + if ( primcount === 0 ) return; + + gl.drawElementsInstanced( mode, count, type, start * bytesPerElement, primcount ); + + info.update( count, mode, primcount ); + + } + + function renderMultiDraw( starts, counts, drawCount ) { + + if ( drawCount === 0 ) return; + + const extension = extensions.get( 'WEBGL_multi_draw' ); + + if ( extension === null ) { + + for ( let i = 0; i < drawCount; i ++ ) { + + this.render( starts[ i ] / bytesPerElement, counts[ i ] ); + + } + + } else { + + extension.multiDrawElementsWEBGL( mode, counts, 0, type, starts, 0, drawCount ); + + let elementCount = 0; + for ( let i = 0; i < drawCount; i ++ ) { + + elementCount += counts[ i ]; + + } + + info.update( elementCount, mode, 1 ); + + } + + } + + function renderMultiDrawInstances( starts, counts, drawCount, primcount ) { + + if ( drawCount === 0 ) return; + + const extension = extensions.get( 'WEBGL_multi_draw' ); + + if ( extension === null ) { + + for ( let i = 0; i < starts.length; i ++ ) { + + renderInstances( starts[ i ] / bytesPerElement, counts[ i ], primcount[ i ] ); + + } + + } else { + + extension.multiDrawElementsInstancedWEBGL( mode, counts, 0, type, starts, 0, primcount, 0, drawCount ); + + let elementCount = 0; + for ( let i = 0; i < drawCount; i ++ ) { + + elementCount += counts[ i ]; + + } + + for ( let i = 0; i < primcount.length; i ++ ) { + + info.update( elementCount, mode, primcount[ i ] ); + + } + + } + + } + + // + + this.setMode = setMode; + this.setIndex = setIndex; + this.render = render; + this.renderInstances = renderInstances; + this.renderMultiDraw = renderMultiDraw; + this.renderMultiDrawInstances = renderMultiDrawInstances; + +} + +function WebGLInfo( gl ) { + + const memory = { + geometries: 0, + textures: 0 + }; + + const render = { + frame: 0, + calls: 0, + triangles: 0, + points: 0, + lines: 0 + }; + + function update( count, mode, instanceCount ) { + + render.calls ++; + + switch ( mode ) { + + case gl.TRIANGLES: + render.triangles += instanceCount * ( count / 3 ); + break; + + case gl.LINES: + render.lines += instanceCount * ( count / 2 ); + break; + + case gl.LINE_STRIP: + render.lines += instanceCount * ( count - 1 ); + break; + + case gl.LINE_LOOP: + render.lines += instanceCount * count; + break; + + case gl.POINTS: + render.points += instanceCount * count; + break; + + default: + console.error( 'THREE.WebGLInfo: Unknown draw mode:', mode ); + break; + + } + + } + + function reset() { + + render.calls = 0; + render.triangles = 0; + render.points = 0; + render.lines = 0; + + } + + return { + memory: memory, + render: render, + programs: null, + autoReset: true, + reset: reset, + update: update + }; + +} + +function WebGLMorphtargets( gl, capabilities, textures ) { + + const morphTextures = new WeakMap(); + const morph = new Vector4(); + + function update( object, geometry, program ) { + + const objectInfluences = object.morphTargetInfluences; + + // instead of using attributes, the WebGL 2 code path encodes morph targets + // into an array of data textures. Each layer represents a single morph target. + + const morphAttribute = geometry.morphAttributes.position || geometry.morphAttributes.normal || geometry.morphAttributes.color; + const morphTargetsCount = ( morphAttribute !== undefined ) ? morphAttribute.length : 0; + + let entry = morphTextures.get( geometry ); + + if ( entry === undefined || entry.count !== morphTargetsCount ) { + + if ( entry !== undefined ) entry.texture.dispose(); + + const hasMorphPosition = geometry.morphAttributes.position !== undefined; + const hasMorphNormals = geometry.morphAttributes.normal !== undefined; + const hasMorphColors = geometry.morphAttributes.color !== undefined; + + const morphTargets = geometry.morphAttributes.position || []; + const morphNormals = geometry.morphAttributes.normal || []; + const morphColors = geometry.morphAttributes.color || []; + + let vertexDataCount = 0; + + if ( hasMorphPosition === true ) vertexDataCount = 1; + if ( hasMorphNormals === true ) vertexDataCount = 2; + if ( hasMorphColors === true ) vertexDataCount = 3; + + let width = geometry.attributes.position.count * vertexDataCount; + let height = 1; + + if ( width > capabilities.maxTextureSize ) { + + height = Math.ceil( width / capabilities.maxTextureSize ); + width = capabilities.maxTextureSize; + + } + + const buffer = new Float32Array( width * height * 4 * morphTargetsCount ); + + const texture = new DataArrayTexture( buffer, width, height, morphTargetsCount ); + texture.type = FloatType; + texture.needsUpdate = true; + + // fill buffer + + const vertexDataStride = vertexDataCount * 4; + + for ( let i = 0; i < morphTargetsCount; i ++ ) { + + const morphTarget = morphTargets[ i ]; + const morphNormal = morphNormals[ i ]; + const morphColor = morphColors[ i ]; + + const offset = width * height * 4 * i; + + for ( let j = 0; j < morphTarget.count; j ++ ) { + + const stride = j * vertexDataStride; + + if ( hasMorphPosition === true ) { + + morph.fromBufferAttribute( morphTarget, j ); + + buffer[ offset + stride + 0 ] = morph.x; + buffer[ offset + stride + 1 ] = morph.y; + buffer[ offset + stride + 2 ] = morph.z; + buffer[ offset + stride + 3 ] = 0; + + } + + if ( hasMorphNormals === true ) { + + morph.fromBufferAttribute( morphNormal, j ); + + buffer[ offset + stride + 4 ] = morph.x; + buffer[ offset + stride + 5 ] = morph.y; + buffer[ offset + stride + 6 ] = morph.z; + buffer[ offset + stride + 7 ] = 0; + + } + + if ( hasMorphColors === true ) { + + morph.fromBufferAttribute( morphColor, j ); + + buffer[ offset + stride + 8 ] = morph.x; + buffer[ offset + stride + 9 ] = morph.y; + buffer[ offset + stride + 10 ] = morph.z; + buffer[ offset + stride + 11 ] = ( morphColor.itemSize === 4 ) ? morph.w : 1; + + } + + } + + } + + entry = { + count: morphTargetsCount, + texture: texture, + size: new Vector2( width, height ) + }; + + morphTextures.set( geometry, entry ); + + function disposeTexture() { + + texture.dispose(); + + morphTextures.delete( geometry ); + + geometry.removeEventListener( 'dispose', disposeTexture ); + + } + + geometry.addEventListener( 'dispose', disposeTexture ); + + } + + // + if ( object.isInstancedMesh === true && object.morphTexture !== null ) { + + program.getUniforms().setValue( gl, 'morphTexture', object.morphTexture, textures ); + + } else { + + let morphInfluencesSum = 0; + + for ( let i = 0; i < objectInfluences.length; i ++ ) { + + morphInfluencesSum += objectInfluences[ i ]; + + } + + const morphBaseInfluence = geometry.morphTargetsRelative ? 1 : 1 - morphInfluencesSum; + + + program.getUniforms().setValue( gl, 'morphTargetBaseInfluence', morphBaseInfluence ); + program.getUniforms().setValue( gl, 'morphTargetInfluences', objectInfluences ); + + } + + program.getUniforms().setValue( gl, 'morphTargetsTexture', entry.texture, textures ); + program.getUniforms().setValue( gl, 'morphTargetsTextureSize', entry.size ); + + } + + return { + + update: update + + }; + +} + +function WebGLObjects( gl, geometries, attributes, info ) { + + let updateMap = new WeakMap(); + + function update( object ) { + + const frame = info.render.frame; + + const geometry = object.geometry; + const buffergeometry = geometries.get( object, geometry ); + + // Update once per frame + + if ( updateMap.get( buffergeometry ) !== frame ) { + + geometries.update( buffergeometry ); + + updateMap.set( buffergeometry, frame ); + + } + + if ( object.isInstancedMesh ) { + + if ( object.hasEventListener( 'dispose', onInstancedMeshDispose ) === false ) { + + object.addEventListener( 'dispose', onInstancedMeshDispose ); + + } + + if ( updateMap.get( object ) !== frame ) { + + attributes.update( object.instanceMatrix, gl.ARRAY_BUFFER ); + + if ( object.instanceColor !== null ) { + + attributes.update( object.instanceColor, gl.ARRAY_BUFFER ); + + } + + updateMap.set( object, frame ); + + } + + } + + if ( object.isSkinnedMesh ) { + + const skeleton = object.skeleton; + + if ( updateMap.get( skeleton ) !== frame ) { + + skeleton.update(); + + updateMap.set( skeleton, frame ); + + } + + } + + return buffergeometry; + + } + + function dispose() { + + updateMap = new WeakMap(); + + } + + function onInstancedMeshDispose( event ) { + + const instancedMesh = event.target; + + instancedMesh.removeEventListener( 'dispose', onInstancedMeshDispose ); + + attributes.remove( instancedMesh.instanceMatrix ); + + if ( instancedMesh.instanceColor !== null ) attributes.remove( instancedMesh.instanceColor ); + + } + + return { + + update: update, + dispose: dispose + + }; + +} + +class DepthTexture extends Texture { + + constructor( width, height, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy, format ) { + + format = format !== undefined ? format : DepthFormat; + + if ( format !== DepthFormat && format !== DepthStencilFormat ) { + + throw new Error( 'DepthTexture format must be either THREE.DepthFormat or THREE.DepthStencilFormat' ); + + } + + if ( type === undefined && format === DepthFormat ) type = UnsignedIntType; + if ( type === undefined && format === DepthStencilFormat ) type = UnsignedInt248Type; + + super( null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ); + + this.isDepthTexture = true; + + this.image = { width: width, height: height }; + + this.magFilter = magFilter !== undefined ? magFilter : NearestFilter; + this.minFilter = minFilter !== undefined ? minFilter : NearestFilter; + + this.flipY = false; + this.generateMipmaps = false; + + this.compareFunction = null; + + } + + + copy( source ) { + + super.copy( source ); + + this.compareFunction = source.compareFunction; + + return this; + + } + + toJSON( meta ) { + + const data = super.toJSON( meta ); + + if ( this.compareFunction !== null ) data.compareFunction = this.compareFunction; + + return data; + + } + +} + +/** + * Uniforms of a program. + * Those form a tree structure with a special top-level container for the root, + * which you get by calling 'new WebGLUniforms( gl, program )'. + * + * + * Properties of inner nodes including the top-level container: + * + * .seq - array of nested uniforms + * .map - nested uniforms by name + * + * + * Methods of all nodes except the top-level container: + * + * .setValue( gl, value, [textures] ) + * + * uploads a uniform value(s) + * the 'textures' parameter is needed for sampler uniforms + * + * + * Static methods of the top-level container (textures factorizations): + * + * .upload( gl, seq, values, textures ) + * + * sets uniforms in 'seq' to 'values[id].value' + * + * .seqWithValue( seq, values ) : filteredSeq + * + * filters 'seq' entries with corresponding entry in values + * + * + * Methods of the top-level container (textures factorizations): + * + * .setValue( gl, name, value, textures ) + * + * sets uniform with name 'name' to 'value' + * + * .setOptional( gl, obj, prop ) + * + * like .set for an optional property of the object + * + */ + + +const emptyTexture = /*@__PURE__*/ new Texture(); + +const emptyShadowTexture = /*@__PURE__*/ new DepthTexture( 1, 1 ); +emptyShadowTexture.compareFunction = LessEqualCompare; + +const emptyArrayTexture = /*@__PURE__*/ new DataArrayTexture(); +const empty3dTexture = /*@__PURE__*/ new Data3DTexture(); +const emptyCubeTexture = /*@__PURE__*/ new CubeTexture(); + +// --- Utilities --- + +// Array Caches (provide typed arrays for temporary by size) + +const arrayCacheF32 = []; +const arrayCacheI32 = []; + +// Float32Array caches used for uploading Matrix uniforms + +const mat4array = new Float32Array( 16 ); +const mat3array = new Float32Array( 9 ); +const mat2array = new Float32Array( 4 ); + +// Flattening for arrays of vectors and matrices + +function flatten( array, nBlocks, blockSize ) { + + const firstElem = array[ 0 ]; + + if ( firstElem <= 0 || firstElem > 0 ) return array; + // unoptimized: ! isNaN( firstElem ) + // see http://jacksondunstan.com/articles/983 + + const n = nBlocks * blockSize; + let r = arrayCacheF32[ n ]; + + if ( r === undefined ) { + + r = new Float32Array( n ); + arrayCacheF32[ n ] = r; + + } + + if ( nBlocks !== 0 ) { + + firstElem.toArray( r, 0 ); + + for ( let i = 1, offset = 0; i !== nBlocks; ++ i ) { + + offset += blockSize; + array[ i ].toArray( r, offset ); + + } + + } + + return r; + +} + +function arraysEqual( a, b ) { + + if ( a.length !== b.length ) return false; + + for ( let i = 0, l = a.length; i < l; i ++ ) { + + if ( a[ i ] !== b[ i ] ) return false; + + } + + return true; + +} + +function copyArray( a, b ) { + + for ( let i = 0, l = b.length; i < l; i ++ ) { + + a[ i ] = b[ i ]; + + } + +} + +// Texture unit allocation + +function allocTexUnits( textures, n ) { + + let r = arrayCacheI32[ n ]; + + if ( r === undefined ) { + + r = new Int32Array( n ); + arrayCacheI32[ n ] = r; + + } + + for ( let i = 0; i !== n; ++ i ) { + + r[ i ] = textures.allocateTextureUnit(); + + } + + return r; + +} + +// --- Setters --- + +// Note: Defining these methods externally, because they come in a bunch +// and this way their names minify. + +// Single scalar + +function setValueV1f( gl, v ) { + + const cache = this.cache; + + if ( cache[ 0 ] === v ) return; + + gl.uniform1f( this.addr, v ); + + cache[ 0 ] = v; + +} + +// Single float vector (from flat array or THREE.VectorN) + +function setValueV2f( gl, v ) { + + const cache = this.cache; + + if ( v.x !== undefined ) { + + if ( cache[ 0 ] !== v.x || cache[ 1 ] !== v.y ) { + + gl.uniform2f( this.addr, v.x, v.y ); + + cache[ 0 ] = v.x; + cache[ 1 ] = v.y; + + } + + } else { + + if ( arraysEqual( cache, v ) ) return; + + gl.uniform2fv( this.addr, v ); + + copyArray( cache, v ); + + } + +} + +function setValueV3f( gl, v ) { + + const cache = this.cache; + + if ( v.x !== undefined ) { + + if ( cache[ 0 ] !== v.x || cache[ 1 ] !== v.y || cache[ 2 ] !== v.z ) { + + gl.uniform3f( this.addr, v.x, v.y, v.z ); + + cache[ 0 ] = v.x; + cache[ 1 ] = v.y; + cache[ 2 ] = v.z; + + } + + } else if ( v.r !== undefined ) { + + if ( cache[ 0 ] !== v.r || cache[ 1 ] !== v.g || cache[ 2 ] !== v.b ) { + + gl.uniform3f( this.addr, v.r, v.g, v.b ); + + cache[ 0 ] = v.r; + cache[ 1 ] = v.g; + cache[ 2 ] = v.b; + + } + + } else { + + if ( arraysEqual( cache, v ) ) return; + + gl.uniform3fv( this.addr, v ); + + copyArray( cache, v ); + + } + +} + +function setValueV4f( gl, v ) { + + const cache = this.cache; + + if ( v.x !== undefined ) { + + if ( cache[ 0 ] !== v.x || cache[ 1 ] !== v.y || cache[ 2 ] !== v.z || cache[ 3 ] !== v.w ) { + + gl.uniform4f( this.addr, v.x, v.y, v.z, v.w ); + + cache[ 0 ] = v.x; + cache[ 1 ] = v.y; + cache[ 2 ] = v.z; + cache[ 3 ] = v.w; + + } + + } else { + + if ( arraysEqual( cache, v ) ) return; + + gl.uniform4fv( this.addr, v ); + + copyArray( cache, v ); + + } + +} + +// Single matrix (from flat array or THREE.MatrixN) + +function setValueM2( gl, v ) { + + const cache = this.cache; + const elements = v.elements; + + if ( elements === undefined ) { + + if ( arraysEqual( cache, v ) ) return; + + gl.uniformMatrix2fv( this.addr, false, v ); + + copyArray( cache, v ); + + } else { + + if ( arraysEqual( cache, elements ) ) return; + + mat2array.set( elements ); + + gl.uniformMatrix2fv( this.addr, false, mat2array ); + + copyArray( cache, elements ); + + } + +} + +function setValueM3( gl, v ) { + + const cache = this.cache; + const elements = v.elements; + + if ( elements === undefined ) { + + if ( arraysEqual( cache, v ) ) return; + + gl.uniformMatrix3fv( this.addr, false, v ); + + copyArray( cache, v ); + + } else { + + if ( arraysEqual( cache, elements ) ) return; + + mat3array.set( elements ); + + gl.uniformMatrix3fv( this.addr, false, mat3array ); + + copyArray( cache, elements ); + + } + +} + +function setValueM4( gl, v ) { + + const cache = this.cache; + const elements = v.elements; + + if ( elements === undefined ) { + + if ( arraysEqual( cache, v ) ) return; + + gl.uniformMatrix4fv( this.addr, false, v ); + + copyArray( cache, v ); + + } else { + + if ( arraysEqual( cache, elements ) ) return; + + mat4array.set( elements ); + + gl.uniformMatrix4fv( this.addr, false, mat4array ); + + copyArray( cache, elements ); + + } + +} + +// Single integer / boolean + +function setValueV1i( gl, v ) { + + const cache = this.cache; + + if ( cache[ 0 ] === v ) return; + + gl.uniform1i( this.addr, v ); + + cache[ 0 ] = v; + +} + +// Single integer / boolean vector (from flat array or THREE.VectorN) + +function setValueV2i( gl, v ) { + + const cache = this.cache; + + if ( v.x !== undefined ) { + + if ( cache[ 0 ] !== v.x || cache[ 1 ] !== v.y ) { + + gl.uniform2i( this.addr, v.x, v.y ); + + cache[ 0 ] = v.x; + cache[ 1 ] = v.y; + + } + + } else { + + if ( arraysEqual( cache, v ) ) return; + + gl.uniform2iv( this.addr, v ); + + copyArray( cache, v ); + + } + +} + +function setValueV3i( gl, v ) { + + const cache = this.cache; + + if ( v.x !== undefined ) { + + if ( cache[ 0 ] !== v.x || cache[ 1 ] !== v.y || cache[ 2 ] !== v.z ) { + + gl.uniform3i( this.addr, v.x, v.y, v.z ); + + cache[ 0 ] = v.x; + cache[ 1 ] = v.y; + cache[ 2 ] = v.z; + + } + + } else { + + if ( arraysEqual( cache, v ) ) return; + + gl.uniform3iv( this.addr, v ); + + copyArray( cache, v ); + + } + +} + +function setValueV4i( gl, v ) { + + const cache = this.cache; + + if ( v.x !== undefined ) { + + if ( cache[ 0 ] !== v.x || cache[ 1 ] !== v.y || cache[ 2 ] !== v.z || cache[ 3 ] !== v.w ) { + + gl.uniform4i( this.addr, v.x, v.y, v.z, v.w ); + + cache[ 0 ] = v.x; + cache[ 1 ] = v.y; + cache[ 2 ] = v.z; + cache[ 3 ] = v.w; + + } + + } else { + + if ( arraysEqual( cache, v ) ) return; + + gl.uniform4iv( this.addr, v ); + + copyArray( cache, v ); + + } + +} + +// Single unsigned integer + +function setValueV1ui( gl, v ) { + + const cache = this.cache; + + if ( cache[ 0 ] === v ) return; + + gl.uniform1ui( this.addr, v ); + + cache[ 0 ] = v; + +} + +// Single unsigned integer vector (from flat array or THREE.VectorN) + +function setValueV2ui( gl, v ) { + + const cache = this.cache; + + if ( v.x !== undefined ) { + + if ( cache[ 0 ] !== v.x || cache[ 1 ] !== v.y ) { + + gl.uniform2ui( this.addr, v.x, v.y ); + + cache[ 0 ] = v.x; + cache[ 1 ] = v.y; + + } + + } else { + + if ( arraysEqual( cache, v ) ) return; + + gl.uniform2uiv( this.addr, v ); + + copyArray( cache, v ); + + } + +} + +function setValueV3ui( gl, v ) { + + const cache = this.cache; + + if ( v.x !== undefined ) { + + if ( cache[ 0 ] !== v.x || cache[ 1 ] !== v.y || cache[ 2 ] !== v.z ) { + + gl.uniform3ui( this.addr, v.x, v.y, v.z ); + + cache[ 0 ] = v.x; + cache[ 1 ] = v.y; + cache[ 2 ] = v.z; + + } + + } else { + + if ( arraysEqual( cache, v ) ) return; + + gl.uniform3uiv( this.addr, v ); + + copyArray( cache, v ); + + } + +} + +function setValueV4ui( gl, v ) { + + const cache = this.cache; + + if ( v.x !== undefined ) { + + if ( cache[ 0 ] !== v.x || cache[ 1 ] !== v.y || cache[ 2 ] !== v.z || cache[ 3 ] !== v.w ) { + + gl.uniform4ui( this.addr, v.x, v.y, v.z, v.w ); + + cache[ 0 ] = v.x; + cache[ 1 ] = v.y; + cache[ 2 ] = v.z; + cache[ 3 ] = v.w; + + } + + } else { + + if ( arraysEqual( cache, v ) ) return; + + gl.uniform4uiv( this.addr, v ); + + copyArray( cache, v ); + + } + +} + + +// Single texture (2D / Cube) + +function setValueT1( gl, v, textures ) { + + const cache = this.cache; + const unit = textures.allocateTextureUnit(); + + if ( cache[ 0 ] !== unit ) { + + gl.uniform1i( this.addr, unit ); + cache[ 0 ] = unit; + + } + + const emptyTexture2D = ( this.type === gl.SAMPLER_2D_SHADOW ) ? emptyShadowTexture : emptyTexture; + + textures.setTexture2D( v || emptyTexture2D, unit ); + +} + +function setValueT3D1( gl, v, textures ) { + + const cache = this.cache; + const unit = textures.allocateTextureUnit(); + + if ( cache[ 0 ] !== unit ) { + + gl.uniform1i( this.addr, unit ); + cache[ 0 ] = unit; + + } + + textures.setTexture3D( v || empty3dTexture, unit ); + +} + +function setValueT6( gl, v, textures ) { + + const cache = this.cache; + const unit = textures.allocateTextureUnit(); + + if ( cache[ 0 ] !== unit ) { + + gl.uniform1i( this.addr, unit ); + cache[ 0 ] = unit; + + } + + textures.setTextureCube( v || emptyCubeTexture, unit ); + +} + +function setValueT2DArray1( gl, v, textures ) { + + const cache = this.cache; + const unit = textures.allocateTextureUnit(); + + if ( cache[ 0 ] !== unit ) { + + gl.uniform1i( this.addr, unit ); + cache[ 0 ] = unit; + + } + + textures.setTexture2DArray( v || emptyArrayTexture, unit ); + +} + +// Helper to pick the right setter for the singular case + +function getSingularSetter( type ) { + + switch ( type ) { + + case 0x1406: return setValueV1f; // FLOAT + case 0x8b50: return setValueV2f; // _VEC2 + case 0x8b51: return setValueV3f; // _VEC3 + case 0x8b52: return setValueV4f; // _VEC4 + + case 0x8b5a: return setValueM2; // _MAT2 + case 0x8b5b: return setValueM3; // _MAT3 + case 0x8b5c: return setValueM4; // _MAT4 + + case 0x1404: case 0x8b56: return setValueV1i; // INT, BOOL + case 0x8b53: case 0x8b57: return setValueV2i; // _VEC2 + case 0x8b54: case 0x8b58: return setValueV3i; // _VEC3 + case 0x8b55: case 0x8b59: return setValueV4i; // _VEC4 + + case 0x1405: return setValueV1ui; // UINT + case 0x8dc6: return setValueV2ui; // _VEC2 + case 0x8dc7: return setValueV3ui; // _VEC3 + case 0x8dc8: return setValueV4ui; // _VEC4 + + case 0x8b5e: // SAMPLER_2D + case 0x8d66: // SAMPLER_EXTERNAL_OES + case 0x8dca: // INT_SAMPLER_2D + case 0x8dd2: // UNSIGNED_INT_SAMPLER_2D + case 0x8b62: // SAMPLER_2D_SHADOW + return setValueT1; + + case 0x8b5f: // SAMPLER_3D + case 0x8dcb: // INT_SAMPLER_3D + case 0x8dd3: // UNSIGNED_INT_SAMPLER_3D + return setValueT3D1; + + case 0x8b60: // SAMPLER_CUBE + case 0x8dcc: // INT_SAMPLER_CUBE + case 0x8dd4: // UNSIGNED_INT_SAMPLER_CUBE + case 0x8dc5: // SAMPLER_CUBE_SHADOW + return setValueT6; + + case 0x8dc1: // SAMPLER_2D_ARRAY + case 0x8dcf: // INT_SAMPLER_2D_ARRAY + case 0x8dd7: // UNSIGNED_INT_SAMPLER_2D_ARRAY + case 0x8dc4: // SAMPLER_2D_ARRAY_SHADOW + return setValueT2DArray1; + + } + +} + + +// Array of scalars + +function setValueV1fArray( gl, v ) { + + gl.uniform1fv( this.addr, v ); + +} + +// Array of vectors (from flat array or array of THREE.VectorN) + +function setValueV2fArray( gl, v ) { + + const data = flatten( v, this.size, 2 ); + + gl.uniform2fv( this.addr, data ); + +} + +function setValueV3fArray( gl, v ) { + + const data = flatten( v, this.size, 3 ); + + gl.uniform3fv( this.addr, data ); + +} + +function setValueV4fArray( gl, v ) { + + const data = flatten( v, this.size, 4 ); + + gl.uniform4fv( this.addr, data ); + +} + +// Array of matrices (from flat array or array of THREE.MatrixN) + +function setValueM2Array( gl, v ) { + + const data = flatten( v, this.size, 4 ); + + gl.uniformMatrix2fv( this.addr, false, data ); + +} + +function setValueM3Array( gl, v ) { + + const data = flatten( v, this.size, 9 ); + + gl.uniformMatrix3fv( this.addr, false, data ); + +} + +function setValueM4Array( gl, v ) { + + const data = flatten( v, this.size, 16 ); + + gl.uniformMatrix4fv( this.addr, false, data ); + +} + +// Array of integer / boolean + +function setValueV1iArray( gl, v ) { + + gl.uniform1iv( this.addr, v ); + +} + +// Array of integer / boolean vectors (from flat array) + +function setValueV2iArray( gl, v ) { + + gl.uniform2iv( this.addr, v ); + +} + +function setValueV3iArray( gl, v ) { + + gl.uniform3iv( this.addr, v ); + +} + +function setValueV4iArray( gl, v ) { + + gl.uniform4iv( this.addr, v ); + +} + +// Array of unsigned integer + +function setValueV1uiArray( gl, v ) { + + gl.uniform1uiv( this.addr, v ); + +} + +// Array of unsigned integer vectors (from flat array) + +function setValueV2uiArray( gl, v ) { + + gl.uniform2uiv( this.addr, v ); + +} + +function setValueV3uiArray( gl, v ) { + + gl.uniform3uiv( this.addr, v ); + +} + +function setValueV4uiArray( gl, v ) { + + gl.uniform4uiv( this.addr, v ); + +} + + +// Array of textures (2D / 3D / Cube / 2DArray) + +function setValueT1Array( gl, v, textures ) { + + const cache = this.cache; + + const n = v.length; + + const units = allocTexUnits( textures, n ); + + if ( ! arraysEqual( cache, units ) ) { + + gl.uniform1iv( this.addr, units ); + + copyArray( cache, units ); + + } + + for ( let i = 0; i !== n; ++ i ) { + + textures.setTexture2D( v[ i ] || emptyTexture, units[ i ] ); + + } + +} + +function setValueT3DArray( gl, v, textures ) { + + const cache = this.cache; + + const n = v.length; + + const units = allocTexUnits( textures, n ); + + if ( ! arraysEqual( cache, units ) ) { + + gl.uniform1iv( this.addr, units ); + + copyArray( cache, units ); + + } + + for ( let i = 0; i !== n; ++ i ) { + + textures.setTexture3D( v[ i ] || empty3dTexture, units[ i ] ); + + } + +} + +function setValueT6Array( gl, v, textures ) { + + const cache = this.cache; + + const n = v.length; + + const units = allocTexUnits( textures, n ); + + if ( ! arraysEqual( cache, units ) ) { + + gl.uniform1iv( this.addr, units ); + + copyArray( cache, units ); + + } + + for ( let i = 0; i !== n; ++ i ) { + + textures.setTextureCube( v[ i ] || emptyCubeTexture, units[ i ] ); + + } + +} + +function setValueT2DArrayArray( gl, v, textures ) { + + const cache = this.cache; + + const n = v.length; + + const units = allocTexUnits( textures, n ); + + if ( ! arraysEqual( cache, units ) ) { + + gl.uniform1iv( this.addr, units ); + + copyArray( cache, units ); + + } + + for ( let i = 0; i !== n; ++ i ) { + + textures.setTexture2DArray( v[ i ] || emptyArrayTexture, units[ i ] ); + + } + +} + + +// Helper to pick the right setter for a pure (bottom-level) array + +function getPureArraySetter( type ) { + + switch ( type ) { + + case 0x1406: return setValueV1fArray; // FLOAT + case 0x8b50: return setValueV2fArray; // _VEC2 + case 0x8b51: return setValueV3fArray; // _VEC3 + case 0x8b52: return setValueV4fArray; // _VEC4 + + case 0x8b5a: return setValueM2Array; // _MAT2 + case 0x8b5b: return setValueM3Array; // _MAT3 + case 0x8b5c: return setValueM4Array; // _MAT4 + + case 0x1404: case 0x8b56: return setValueV1iArray; // INT, BOOL + case 0x8b53: case 0x8b57: return setValueV2iArray; // _VEC2 + case 0x8b54: case 0x8b58: return setValueV3iArray; // _VEC3 + case 0x8b55: case 0x8b59: return setValueV4iArray; // _VEC4 + + case 0x1405: return setValueV1uiArray; // UINT + case 0x8dc6: return setValueV2uiArray; // _VEC2 + case 0x8dc7: return setValueV3uiArray; // _VEC3 + case 0x8dc8: return setValueV4uiArray; // _VEC4 + + case 0x8b5e: // SAMPLER_2D + case 0x8d66: // SAMPLER_EXTERNAL_OES + case 0x8dca: // INT_SAMPLER_2D + case 0x8dd2: // UNSIGNED_INT_SAMPLER_2D + case 0x8b62: // SAMPLER_2D_SHADOW + return setValueT1Array; + + case 0x8b5f: // SAMPLER_3D + case 0x8dcb: // INT_SAMPLER_3D + case 0x8dd3: // UNSIGNED_INT_SAMPLER_3D + return setValueT3DArray; + + case 0x8b60: // SAMPLER_CUBE + case 0x8dcc: // INT_SAMPLER_CUBE + case 0x8dd4: // UNSIGNED_INT_SAMPLER_CUBE + case 0x8dc5: // SAMPLER_CUBE_SHADOW + return setValueT6Array; + + case 0x8dc1: // SAMPLER_2D_ARRAY + case 0x8dcf: // INT_SAMPLER_2D_ARRAY + case 0x8dd7: // UNSIGNED_INT_SAMPLER_2D_ARRAY + case 0x8dc4: // SAMPLER_2D_ARRAY_SHADOW + return setValueT2DArrayArray; + + } + +} + +// --- Uniform Classes --- + +class SingleUniform { + + constructor( id, activeInfo, addr ) { + + this.id = id; + this.addr = addr; + this.cache = []; + this.type = activeInfo.type; + this.setValue = getSingularSetter( activeInfo.type ); + + // this.path = activeInfo.name; // DEBUG + + } + +} + +class PureArrayUniform { + + constructor( id, activeInfo, addr ) { + + this.id = id; + this.addr = addr; + this.cache = []; + this.type = activeInfo.type; + this.size = activeInfo.size; + this.setValue = getPureArraySetter( activeInfo.type ); + + // this.path = activeInfo.name; // DEBUG + + } + +} + +class StructuredUniform { + + constructor( id ) { + + this.id = id; + + this.seq = []; + this.map = {}; + + } + + setValue( gl, value, textures ) { + + const seq = this.seq; + + for ( let i = 0, n = seq.length; i !== n; ++ i ) { + + const u = seq[ i ]; + u.setValue( gl, value[ u.id ], textures ); + + } + + } + +} + +// --- Top-level --- + +// Parser - builds up the property tree from the path strings + +const RePathPart = /(\w+)(\])?(\[|\.)?/g; + +// extracts +// - the identifier (member name or array index) +// - followed by an optional right bracket (found when array index) +// - followed by an optional left bracket or dot (type of subscript) +// +// Note: These portions can be read in a non-overlapping fashion and +// allow straightforward parsing of the hierarchy that WebGL encodes +// in the uniform names. + +function addUniform( container, uniformObject ) { + + container.seq.push( uniformObject ); + container.map[ uniformObject.id ] = uniformObject; + +} + +function parseUniform( activeInfo, addr, container ) { + + const path = activeInfo.name, + pathLength = path.length; + + // reset RegExp object, because of the early exit of a previous run + RePathPart.lastIndex = 0; + + while ( true ) { + + const match = RePathPart.exec( path ), + matchEnd = RePathPart.lastIndex; + + let id = match[ 1 ]; + const idIsIndex = match[ 2 ] === ']', + subscript = match[ 3 ]; + + if ( idIsIndex ) id = id | 0; // convert to integer + + if ( subscript === undefined || subscript === '[' && matchEnd + 2 === pathLength ) { + + // bare name or "pure" bottom-level array "[0]" suffix + + addUniform( container, subscript === undefined ? + new SingleUniform( id, activeInfo, addr ) : + new PureArrayUniform( id, activeInfo, addr ) ); + + break; + + } else { + + // step into inner node / create it in case it doesn't exist + + const map = container.map; + let next = map[ id ]; + + if ( next === undefined ) { + + next = new StructuredUniform( id ); + addUniform( container, next ); + + } + + container = next; + + } + + } + +} + +// Root Container + +class WebGLUniforms { + + constructor( gl, program ) { + + this.seq = []; + this.map = {}; + + const n = gl.getProgramParameter( program, gl.ACTIVE_UNIFORMS ); + + for ( let i = 0; i < n; ++ i ) { + + const info = gl.getActiveUniform( program, i ), + addr = gl.getUniformLocation( program, info.name ); + + parseUniform( info, addr, this ); + + } + + } + + setValue( gl, name, value, textures ) { + + const u = this.map[ name ]; + + if ( u !== undefined ) u.setValue( gl, value, textures ); + + } + + setOptional( gl, object, name ) { + + const v = object[ name ]; + + if ( v !== undefined ) this.setValue( gl, name, v ); + + } + + static upload( gl, seq, values, textures ) { + + for ( let i = 0, n = seq.length; i !== n; ++ i ) { + + const u = seq[ i ], + v = values[ u.id ]; + + if ( v.needsUpdate !== false ) { + + // note: always updating when .needsUpdate is undefined + u.setValue( gl, v.value, textures ); + + } + + } + + } + + static seqWithValue( seq, values ) { + + const r = []; + + for ( let i = 0, n = seq.length; i !== n; ++ i ) { + + const u = seq[ i ]; + if ( u.id in values ) r.push( u ); + + } + + return r; + + } + +} + +function WebGLShader( gl, type, string ) { + + const shader = gl.createShader( type ); + + gl.shaderSource( shader, string ); + gl.compileShader( shader ); + + return shader; + +} + +// From https://www.khronos.org/registry/webgl/extensions/KHR_parallel_shader_compile/ +const COMPLETION_STATUS_KHR = 0x91B1; + +let programIdCount = 0; + +function handleSource( string, errorLine ) { + + const lines = string.split( '\n' ); + const lines2 = []; + + const from = Math.max( errorLine - 6, 0 ); + const to = Math.min( errorLine + 6, lines.length ); + + for ( let i = from; i < to; i ++ ) { + + const line = i + 1; + lines2.push( `${line === errorLine ? '>' : ' '} ${line}: ${lines[ i ]}` ); + + } + + return lines2.join( '\n' ); + +} + +function getEncodingComponents( colorSpace ) { + + const workingPrimaries = ColorManagement.getPrimaries( ColorManagement.workingColorSpace ); + const encodingPrimaries = ColorManagement.getPrimaries( colorSpace ); + + let gamutMapping; + + if ( workingPrimaries === encodingPrimaries ) { + + gamutMapping = ''; + + } else if ( workingPrimaries === P3Primaries && encodingPrimaries === Rec709Primaries ) { + + gamutMapping = 'LinearDisplayP3ToLinearSRGB'; + + } else if ( workingPrimaries === Rec709Primaries && encodingPrimaries === P3Primaries ) { + + gamutMapping = 'LinearSRGBToLinearDisplayP3'; + + } + + switch ( colorSpace ) { + + case LinearSRGBColorSpace: + case LinearDisplayP3ColorSpace: + return [ gamutMapping, 'LinearTransferOETF' ]; + + case SRGBColorSpace: + case DisplayP3ColorSpace: + return [ gamutMapping, 'sRGBTransferOETF' ]; + + default: + console.warn( 'THREE.WebGLProgram: Unsupported color space:', colorSpace ); + return [ gamutMapping, 'LinearTransferOETF' ]; + + } + +} + +function getShaderErrors( gl, shader, type ) { + + const status = gl.getShaderParameter( shader, gl.COMPILE_STATUS ); + const errors = gl.getShaderInfoLog( shader ).trim(); + + if ( status && errors === '' ) return ''; + + const errorMatches = /ERROR: 0:(\d+)/.exec( errors ); + if ( errorMatches ) { + + // --enable-privileged-webgl-extension + // console.log( '**' + type + '**', gl.getExtension( 'WEBGL_debug_shaders' ).getTranslatedShaderSource( shader ) ); + + const errorLine = parseInt( errorMatches[ 1 ] ); + return type.toUpperCase() + '\n\n' + errors + '\n\n' + handleSource( gl.getShaderSource( shader ), errorLine ); + + } else { + + return errors; + + } + +} + +function getTexelEncodingFunction( functionName, colorSpace ) { + + const components = getEncodingComponents( colorSpace ); + return `vec4 ${functionName}( vec4 value ) { return ${components[ 0 ]}( ${components[ 1 ]}( value ) ); }`; + +} + +function getToneMappingFunction( functionName, toneMapping ) { + + let toneMappingName; + + switch ( toneMapping ) { + + case LinearToneMapping: + toneMappingName = 'Linear'; + break; + + case ReinhardToneMapping: + toneMappingName = 'Reinhard'; + break; + + case CineonToneMapping: + toneMappingName = 'OptimizedCineon'; + break; + + case ACESFilmicToneMapping: + toneMappingName = 'ACESFilmic'; + break; + + case AgXToneMapping: + toneMappingName = 'AgX'; + break; + + case NeutralToneMapping: + toneMappingName = 'Neutral'; + break; + + case CustomToneMapping: + toneMappingName = 'Custom'; + break; + + default: + console.warn( 'THREE.WebGLProgram: Unsupported toneMapping:', toneMapping ); + toneMappingName = 'Linear'; + + } + + return 'vec3 ' + functionName + '( vec3 color ) { return ' + toneMappingName + 'ToneMapping( color ); }'; + +} + +function generateVertexExtensions( parameters ) { + + const chunks = [ + parameters.extensionClipCullDistance ? '#extension GL_ANGLE_clip_cull_distance : require' : '', + parameters.extensionMultiDraw ? '#extension GL_ANGLE_multi_draw : require' : '', + ]; + + return chunks.filter( filterEmptyLine ).join( '\n' ); + +} + +function generateDefines( defines ) { + + const chunks = []; + + for ( const name in defines ) { + + const value = defines[ name ]; + + if ( value === false ) continue; + + chunks.push( '#define ' + name + ' ' + value ); + + } + + return chunks.join( '\n' ); + +} + +function fetchAttributeLocations( gl, program ) { + + const attributes = {}; + + const n = gl.getProgramParameter( program, gl.ACTIVE_ATTRIBUTES ); + + for ( let i = 0; i < n; i ++ ) { + + const info = gl.getActiveAttrib( program, i ); + const name = info.name; + + let locationSize = 1; + if ( info.type === gl.FLOAT_MAT2 ) locationSize = 2; + if ( info.type === gl.FLOAT_MAT3 ) locationSize = 3; + if ( info.type === gl.FLOAT_MAT4 ) locationSize = 4; + + // console.log( 'THREE.WebGLProgram: ACTIVE VERTEX ATTRIBUTE:', name, i ); + + attributes[ name ] = { + type: info.type, + location: gl.getAttribLocation( program, name ), + locationSize: locationSize + }; + + } + + return attributes; + +} + +function filterEmptyLine( string ) { + + return string !== ''; + +} + +function replaceLightNums( string, parameters ) { + + const numSpotLightCoords = parameters.numSpotLightShadows + parameters.numSpotLightMaps - parameters.numSpotLightShadowsWithMaps; + + return string + .replace( /NUM_DIR_LIGHTS/g, parameters.numDirLights ) + .replace( /NUM_SPOT_LIGHTS/g, parameters.numSpotLights ) + .replace( /NUM_SPOT_LIGHT_MAPS/g, parameters.numSpotLightMaps ) + .replace( /NUM_SPOT_LIGHT_COORDS/g, numSpotLightCoords ) + .replace( /NUM_RECT_AREA_LIGHTS/g, parameters.numRectAreaLights ) + .replace( /NUM_POINT_LIGHTS/g, parameters.numPointLights ) + .replace( /NUM_HEMI_LIGHTS/g, parameters.numHemiLights ) + .replace( /NUM_DIR_LIGHT_SHADOWS/g, parameters.numDirLightShadows ) + .replace( /NUM_SPOT_LIGHT_SHADOWS_WITH_MAPS/g, parameters.numSpotLightShadowsWithMaps ) + .replace( /NUM_SPOT_LIGHT_SHADOWS/g, parameters.numSpotLightShadows ) + .replace( /NUM_POINT_LIGHT_SHADOWS/g, parameters.numPointLightShadows ); + +} + +function replaceClippingPlaneNums( string, parameters ) { + + return string + .replace( /NUM_CLIPPING_PLANES/g, parameters.numClippingPlanes ) + .replace( /UNION_CLIPPING_PLANES/g, ( parameters.numClippingPlanes - parameters.numClipIntersection ) ); + +} + +// Resolve Includes + +const includePattern = /^[ \t]*#include +<([\w\d./]+)>/gm; + +function resolveIncludes( string ) { + + return string.replace( includePattern, includeReplacer ); + +} + +const shaderChunkMap = new Map(); + +function includeReplacer( match, include ) { + + let string = ShaderChunk[ include ]; + + if ( string === undefined ) { + + const newInclude = shaderChunkMap.get( include ); + + if ( newInclude !== undefined ) { + + string = ShaderChunk[ newInclude ]; + console.warn( 'THREE.WebGLRenderer: Shader chunk "%s" has been deprecated. Use "%s" instead.', include, newInclude ); + + } else { + + throw new Error( 'Can not resolve #include <' + include + '>' ); + + } + + } + + return resolveIncludes( string ); + +} + +// Unroll Loops + +const unrollLoopPattern = /#pragma unroll_loop_start\s+for\s*\(\s*int\s+i\s*=\s*(\d+)\s*;\s*i\s*<\s*(\d+)\s*;\s*i\s*\+\+\s*\)\s*{([\s\S]+?)}\s+#pragma unroll_loop_end/g; + +function unrollLoops( string ) { + + return string.replace( unrollLoopPattern, loopReplacer ); + +} + +function loopReplacer( match, start, end, snippet ) { + + let string = ''; + + for ( let i = parseInt( start ); i < parseInt( end ); i ++ ) { + + string += snippet + .replace( /\[\s*i\s*\]/g, '[ ' + i + ' ]' ) + .replace( /UNROLLED_LOOP_INDEX/g, i ); + + } + + return string; + +} + +// + +function generatePrecision( parameters ) { + + let precisionstring = `precision ${parameters.precision} float; + precision ${parameters.precision} int; + precision ${parameters.precision} sampler2D; + precision ${parameters.precision} samplerCube; + precision ${parameters.precision} sampler3D; + precision ${parameters.precision} sampler2DArray; + precision ${parameters.precision} sampler2DShadow; + precision ${parameters.precision} samplerCubeShadow; + precision ${parameters.precision} sampler2DArrayShadow; + precision ${parameters.precision} isampler2D; + precision ${parameters.precision} isampler3D; + precision ${parameters.precision} isamplerCube; + precision ${parameters.precision} isampler2DArray; + precision ${parameters.precision} usampler2D; + precision ${parameters.precision} usampler3D; + precision ${parameters.precision} usamplerCube; + precision ${parameters.precision} usampler2DArray; + `; + + if ( parameters.precision === 'highp' ) { + + precisionstring += '\n#define HIGH_PRECISION'; + + } else if ( parameters.precision === 'mediump' ) { + + precisionstring += '\n#define MEDIUM_PRECISION'; + + } else if ( parameters.precision === 'lowp' ) { + + precisionstring += '\n#define LOW_PRECISION'; + + } + + return precisionstring; + +} + +function generateShadowMapTypeDefine( parameters ) { + + let shadowMapTypeDefine = 'SHADOWMAP_TYPE_BASIC'; + + if ( parameters.shadowMapType === PCFShadowMap ) { + + shadowMapTypeDefine = 'SHADOWMAP_TYPE_PCF'; + + } else if ( parameters.shadowMapType === PCFSoftShadowMap ) { + + shadowMapTypeDefine = 'SHADOWMAP_TYPE_PCF_SOFT'; + + } else if ( parameters.shadowMapType === VSMShadowMap ) { + + shadowMapTypeDefine = 'SHADOWMAP_TYPE_VSM'; + + } + + return shadowMapTypeDefine; + +} + +function generateEnvMapTypeDefine( parameters ) { + + let envMapTypeDefine = 'ENVMAP_TYPE_CUBE'; + + if ( parameters.envMap ) { + + switch ( parameters.envMapMode ) { + + case CubeReflectionMapping: + case CubeRefractionMapping: + envMapTypeDefine = 'ENVMAP_TYPE_CUBE'; + break; + + case CubeUVReflectionMapping: + envMapTypeDefine = 'ENVMAP_TYPE_CUBE_UV'; + break; + + } + + } + + return envMapTypeDefine; + +} + +function generateEnvMapModeDefine( parameters ) { + + let envMapModeDefine = 'ENVMAP_MODE_REFLECTION'; + + if ( parameters.envMap ) { + + switch ( parameters.envMapMode ) { + + case CubeRefractionMapping: + + envMapModeDefine = 'ENVMAP_MODE_REFRACTION'; + break; + + } + + } + + return envMapModeDefine; + +} + +function generateEnvMapBlendingDefine( parameters ) { + + let envMapBlendingDefine = 'ENVMAP_BLENDING_NONE'; + + if ( parameters.envMap ) { + + switch ( parameters.combine ) { + + case MultiplyOperation: + envMapBlendingDefine = 'ENVMAP_BLENDING_MULTIPLY'; + break; + + case MixOperation: + envMapBlendingDefine = 'ENVMAP_BLENDING_MIX'; + break; + + case AddOperation: + envMapBlendingDefine = 'ENVMAP_BLENDING_ADD'; + break; + + } + + } + + return envMapBlendingDefine; + +} + +function generateCubeUVSize( parameters ) { + + const imageHeight = parameters.envMapCubeUVHeight; + + if ( imageHeight === null ) return null; + + const maxMip = Math.log2( imageHeight ) - 2; + + const texelHeight = 1.0 / imageHeight; + + const texelWidth = 1.0 / ( 3 * Math.max( Math.pow( 2, maxMip ), 7 * 16 ) ); + + return { texelWidth, texelHeight, maxMip }; + +} + +function WebGLProgram( renderer, cacheKey, parameters, bindingStates ) { + + // TODO Send this event to Three.js DevTools + // console.log( 'WebGLProgram', cacheKey ); + + const gl = renderer.getContext(); + + const defines = parameters.defines; + + let vertexShader = parameters.vertexShader; + let fragmentShader = parameters.fragmentShader; + + const shadowMapTypeDefine = generateShadowMapTypeDefine( parameters ); + const envMapTypeDefine = generateEnvMapTypeDefine( parameters ); + const envMapModeDefine = generateEnvMapModeDefine( parameters ); + const envMapBlendingDefine = generateEnvMapBlendingDefine( parameters ); + const envMapCubeUVSize = generateCubeUVSize( parameters ); + + const customVertexExtensions = generateVertexExtensions( parameters ); + + const customDefines = generateDefines( defines ); + + const program = gl.createProgram(); + + let prefixVertex, prefixFragment; + let versionString = parameters.glslVersion ? '#version ' + parameters.glslVersion + '\n' : ''; + + if ( parameters.isRawShaderMaterial ) { + + prefixVertex = [ + + '#define SHADER_TYPE ' + parameters.shaderType, + '#define SHADER_NAME ' + parameters.shaderName, + + customDefines + + ].filter( filterEmptyLine ).join( '\n' ); + + if ( prefixVertex.length > 0 ) { + + prefixVertex += '\n'; + + } + + prefixFragment = [ + + '#define SHADER_TYPE ' + parameters.shaderType, + '#define SHADER_NAME ' + parameters.shaderName, + + customDefines + + ].filter( filterEmptyLine ).join( '\n' ); + + if ( prefixFragment.length > 0 ) { + + prefixFragment += '\n'; + + } + + } else { + + prefixVertex = [ + + generatePrecision( parameters ), + + '#define SHADER_TYPE ' + parameters.shaderType, + '#define SHADER_NAME ' + parameters.shaderName, + + customDefines, + + parameters.extensionClipCullDistance ? '#define USE_CLIP_DISTANCE' : '', + parameters.batching ? '#define USE_BATCHING' : '', + parameters.instancing ? '#define USE_INSTANCING' : '', + parameters.instancingColor ? '#define USE_INSTANCING_COLOR' : '', + parameters.instancingMorph ? '#define USE_INSTANCING_MORPH' : '', + + parameters.useFog && parameters.fog ? '#define USE_FOG' : '', + parameters.useFog && parameters.fogExp2 ? '#define FOG_EXP2' : '', + + parameters.map ? '#define USE_MAP' : '', + parameters.envMap ? '#define USE_ENVMAP' : '', + parameters.envMap ? '#define ' + envMapModeDefine : '', + parameters.lightMap ? '#define USE_LIGHTMAP' : '', + parameters.aoMap ? '#define USE_AOMAP' : '', + parameters.bumpMap ? '#define USE_BUMPMAP' : '', + parameters.normalMap ? '#define USE_NORMALMAP' : '', + parameters.normalMapObjectSpace ? '#define USE_NORMALMAP_OBJECTSPACE' : '', + parameters.normalMapTangentSpace ? '#define USE_NORMALMAP_TANGENTSPACE' : '', + parameters.displacementMap ? '#define USE_DISPLACEMENTMAP' : '', + parameters.emissiveMap ? '#define USE_EMISSIVEMAP' : '', + + parameters.anisotropy ? '#define USE_ANISOTROPY' : '', + parameters.anisotropyMap ? '#define USE_ANISOTROPYMAP' : '', + + parameters.clearcoatMap ? '#define USE_CLEARCOATMAP' : '', + parameters.clearcoatRoughnessMap ? '#define USE_CLEARCOAT_ROUGHNESSMAP' : '', + parameters.clearcoatNormalMap ? '#define USE_CLEARCOAT_NORMALMAP' : '', + + parameters.iridescenceMap ? '#define USE_IRIDESCENCEMAP' : '', + parameters.iridescenceThicknessMap ? '#define USE_IRIDESCENCE_THICKNESSMAP' : '', + + parameters.specularMap ? '#define USE_SPECULARMAP' : '', + parameters.specularColorMap ? '#define USE_SPECULAR_COLORMAP' : '', + parameters.specularIntensityMap ? '#define USE_SPECULAR_INTENSITYMAP' : '', + + parameters.roughnessMap ? '#define USE_ROUGHNESSMAP' : '', + parameters.metalnessMap ? '#define USE_METALNESSMAP' : '', + parameters.alphaMap ? '#define USE_ALPHAMAP' : '', + parameters.alphaHash ? '#define USE_ALPHAHASH' : '', + + parameters.transmission ? '#define USE_TRANSMISSION' : '', + parameters.transmissionMap ? '#define USE_TRANSMISSIONMAP' : '', + parameters.thicknessMap ? '#define USE_THICKNESSMAP' : '', + + parameters.sheenColorMap ? '#define USE_SHEEN_COLORMAP' : '', + parameters.sheenRoughnessMap ? '#define USE_SHEEN_ROUGHNESSMAP' : '', + + // + + parameters.mapUv ? '#define MAP_UV ' + parameters.mapUv : '', + parameters.alphaMapUv ? '#define ALPHAMAP_UV ' + parameters.alphaMapUv : '', + parameters.lightMapUv ? '#define LIGHTMAP_UV ' + parameters.lightMapUv : '', + parameters.aoMapUv ? '#define AOMAP_UV ' + parameters.aoMapUv : '', + parameters.emissiveMapUv ? '#define EMISSIVEMAP_UV ' + parameters.emissiveMapUv : '', + parameters.bumpMapUv ? '#define BUMPMAP_UV ' + parameters.bumpMapUv : '', + parameters.normalMapUv ? '#define NORMALMAP_UV ' + parameters.normalMapUv : '', + parameters.displacementMapUv ? '#define DISPLACEMENTMAP_UV ' + parameters.displacementMapUv : '', + + parameters.metalnessMapUv ? '#define METALNESSMAP_UV ' + parameters.metalnessMapUv : '', + parameters.roughnessMapUv ? '#define ROUGHNESSMAP_UV ' + parameters.roughnessMapUv : '', + + parameters.anisotropyMapUv ? '#define ANISOTROPYMAP_UV ' + parameters.anisotropyMapUv : '', + + parameters.clearcoatMapUv ? '#define CLEARCOATMAP_UV ' + parameters.clearcoatMapUv : '', + parameters.clearcoatNormalMapUv ? '#define CLEARCOAT_NORMALMAP_UV ' + parameters.clearcoatNormalMapUv : '', + parameters.clearcoatRoughnessMapUv ? '#define CLEARCOAT_ROUGHNESSMAP_UV ' + parameters.clearcoatRoughnessMapUv : '', + + parameters.iridescenceMapUv ? '#define IRIDESCENCEMAP_UV ' + parameters.iridescenceMapUv : '', + parameters.iridescenceThicknessMapUv ? '#define IRIDESCENCE_THICKNESSMAP_UV ' + parameters.iridescenceThicknessMapUv : '', + + parameters.sheenColorMapUv ? '#define SHEEN_COLORMAP_UV ' + parameters.sheenColorMapUv : '', + parameters.sheenRoughnessMapUv ? '#define SHEEN_ROUGHNESSMAP_UV ' + parameters.sheenRoughnessMapUv : '', + + parameters.specularMapUv ? '#define SPECULARMAP_UV ' + parameters.specularMapUv : '', + parameters.specularColorMapUv ? '#define SPECULAR_COLORMAP_UV ' + parameters.specularColorMapUv : '', + parameters.specularIntensityMapUv ? '#define SPECULAR_INTENSITYMAP_UV ' + parameters.specularIntensityMapUv : '', + + parameters.transmissionMapUv ? '#define TRANSMISSIONMAP_UV ' + parameters.transmissionMapUv : '', + parameters.thicknessMapUv ? '#define THICKNESSMAP_UV ' + parameters.thicknessMapUv : '', + + // + + parameters.vertexTangents && parameters.flatShading === false ? '#define USE_TANGENT' : '', + parameters.vertexColors ? '#define USE_COLOR' : '', + parameters.vertexAlphas ? '#define USE_COLOR_ALPHA' : '', + parameters.vertexUv1s ? '#define USE_UV1' : '', + parameters.vertexUv2s ? '#define USE_UV2' : '', + parameters.vertexUv3s ? '#define USE_UV3' : '', + + parameters.pointsUvs ? '#define USE_POINTS_UV' : '', + + parameters.flatShading ? '#define FLAT_SHADED' : '', + + parameters.skinning ? '#define USE_SKINNING' : '', + + parameters.morphTargets ? '#define USE_MORPHTARGETS' : '', + parameters.morphNormals && parameters.flatShading === false ? '#define USE_MORPHNORMALS' : '', + ( parameters.morphColors ) ? '#define USE_MORPHCOLORS' : '', + ( parameters.morphTargetsCount > 0 ) ? '#define MORPHTARGETS_TEXTURE' : '', + ( parameters.morphTargetsCount > 0 ) ? '#define MORPHTARGETS_TEXTURE_STRIDE ' + parameters.morphTextureStride : '', + ( parameters.morphTargetsCount > 0 ) ? '#define MORPHTARGETS_COUNT ' + parameters.morphTargetsCount : '', + parameters.doubleSided ? '#define DOUBLE_SIDED' : '', + parameters.flipSided ? '#define FLIP_SIDED' : '', + + parameters.shadowMapEnabled ? '#define USE_SHADOWMAP' : '', + parameters.shadowMapEnabled ? '#define ' + shadowMapTypeDefine : '', + + parameters.sizeAttenuation ? '#define USE_SIZEATTENUATION' : '', + + parameters.numLightProbes > 0 ? '#define USE_LIGHT_PROBES' : '', + + parameters.useLegacyLights ? '#define LEGACY_LIGHTS' : '', + + parameters.logarithmicDepthBuffer ? '#define USE_LOGDEPTHBUF' : '', + + 'uniform mat4 modelMatrix;', + 'uniform mat4 modelViewMatrix;', + 'uniform mat4 projectionMatrix;', + 'uniform mat4 viewMatrix;', + 'uniform mat3 normalMatrix;', + 'uniform vec3 cameraPosition;', + 'uniform bool isOrthographic;', + + '#ifdef USE_INSTANCING', + + ' attribute mat4 instanceMatrix;', + + '#endif', + + '#ifdef USE_INSTANCING_COLOR', + + ' attribute vec3 instanceColor;', + + '#endif', + + '#ifdef USE_INSTANCING_MORPH', + + ' uniform sampler2D morphTexture;', + + '#endif', + + 'attribute vec3 position;', + 'attribute vec3 normal;', + 'attribute vec2 uv;', + + '#ifdef USE_UV1', + + ' attribute vec2 uv1;', + + '#endif', + + '#ifdef USE_UV2', + + ' attribute vec2 uv2;', + + '#endif', + + '#ifdef USE_UV3', + + ' attribute vec2 uv3;', + + '#endif', + + '#ifdef USE_TANGENT', + + ' attribute vec4 tangent;', + + '#endif', + + '#if defined( USE_COLOR_ALPHA )', + + ' attribute vec4 color;', + + '#elif defined( USE_COLOR )', + + ' attribute vec3 color;', + + '#endif', + + '#if ( defined( USE_MORPHTARGETS ) && ! defined( MORPHTARGETS_TEXTURE ) )', + + ' attribute vec3 morphTarget0;', + ' attribute vec3 morphTarget1;', + ' attribute vec3 morphTarget2;', + ' attribute vec3 morphTarget3;', + + ' #ifdef USE_MORPHNORMALS', + + ' attribute vec3 morphNormal0;', + ' attribute vec3 morphNormal1;', + ' attribute vec3 morphNormal2;', + ' attribute vec3 morphNormal3;', + + ' #else', + + ' attribute vec3 morphTarget4;', + ' attribute vec3 morphTarget5;', + ' attribute vec3 morphTarget6;', + ' attribute vec3 morphTarget7;', + + ' #endif', + + '#endif', + + '#ifdef USE_SKINNING', + + ' attribute vec4 skinIndex;', + ' attribute vec4 skinWeight;', + + '#endif', + + '\n' + + ].filter( filterEmptyLine ).join( '\n' ); + + prefixFragment = [ + + generatePrecision( parameters ), + + '#define SHADER_TYPE ' + parameters.shaderType, + '#define SHADER_NAME ' + parameters.shaderName, + + customDefines, + + parameters.useFog && parameters.fog ? '#define USE_FOG' : '', + parameters.useFog && parameters.fogExp2 ? '#define FOG_EXP2' : '', + + parameters.alphaToCoverage ? '#define ALPHA_TO_COVERAGE' : '', + parameters.map ? '#define USE_MAP' : '', + parameters.matcap ? '#define USE_MATCAP' : '', + parameters.envMap ? '#define USE_ENVMAP' : '', + parameters.envMap ? '#define ' + envMapTypeDefine : '', + parameters.envMap ? '#define ' + envMapModeDefine : '', + parameters.envMap ? '#define ' + envMapBlendingDefine : '', + envMapCubeUVSize ? '#define CUBEUV_TEXEL_WIDTH ' + envMapCubeUVSize.texelWidth : '', + envMapCubeUVSize ? '#define CUBEUV_TEXEL_HEIGHT ' + envMapCubeUVSize.texelHeight : '', + envMapCubeUVSize ? '#define CUBEUV_MAX_MIP ' + envMapCubeUVSize.maxMip + '.0' : '', + parameters.lightMap ? '#define USE_LIGHTMAP' : '', + parameters.aoMap ? '#define USE_AOMAP' : '', + parameters.bumpMap ? '#define USE_BUMPMAP' : '', + parameters.normalMap ? '#define USE_NORMALMAP' : '', + parameters.normalMapObjectSpace ? '#define USE_NORMALMAP_OBJECTSPACE' : '', + parameters.normalMapTangentSpace ? '#define USE_NORMALMAP_TANGENTSPACE' : '', + parameters.emissiveMap ? '#define USE_EMISSIVEMAP' : '', + + parameters.anisotropy ? '#define USE_ANISOTROPY' : '', + parameters.anisotropyMap ? '#define USE_ANISOTROPYMAP' : '', + + parameters.clearcoat ? '#define USE_CLEARCOAT' : '', + parameters.clearcoatMap ? '#define USE_CLEARCOATMAP' : '', + parameters.clearcoatRoughnessMap ? '#define USE_CLEARCOAT_ROUGHNESSMAP' : '', + parameters.clearcoatNormalMap ? '#define USE_CLEARCOAT_NORMALMAP' : '', + + parameters.dispersion ? '#define USE_DISPERSION' : '', + + parameters.iridescence ? '#define USE_IRIDESCENCE' : '', + parameters.iridescenceMap ? '#define USE_IRIDESCENCEMAP' : '', + parameters.iridescenceThicknessMap ? '#define USE_IRIDESCENCE_THICKNESSMAP' : '', + + parameters.specularMap ? '#define USE_SPECULARMAP' : '', + parameters.specularColorMap ? '#define USE_SPECULAR_COLORMAP' : '', + parameters.specularIntensityMap ? '#define USE_SPECULAR_INTENSITYMAP' : '', + + parameters.roughnessMap ? '#define USE_ROUGHNESSMAP' : '', + parameters.metalnessMap ? '#define USE_METALNESSMAP' : '', + + parameters.alphaMap ? '#define USE_ALPHAMAP' : '', + parameters.alphaTest ? '#define USE_ALPHATEST' : '', + parameters.alphaHash ? '#define USE_ALPHAHASH' : '', + + parameters.sheen ? '#define USE_SHEEN' : '', + parameters.sheenColorMap ? '#define USE_SHEEN_COLORMAP' : '', + parameters.sheenRoughnessMap ? '#define USE_SHEEN_ROUGHNESSMAP' : '', + + parameters.transmission ? '#define USE_TRANSMISSION' : '', + parameters.transmissionMap ? '#define USE_TRANSMISSIONMAP' : '', + parameters.thicknessMap ? '#define USE_THICKNESSMAP' : '', + + parameters.vertexTangents && parameters.flatShading === false ? '#define USE_TANGENT' : '', + parameters.vertexColors || parameters.instancingColor ? '#define USE_COLOR' : '', + parameters.vertexAlphas ? '#define USE_COLOR_ALPHA' : '', + parameters.vertexUv1s ? '#define USE_UV1' : '', + parameters.vertexUv2s ? '#define USE_UV2' : '', + parameters.vertexUv3s ? '#define USE_UV3' : '', + + parameters.pointsUvs ? '#define USE_POINTS_UV' : '', + + parameters.gradientMap ? '#define USE_GRADIENTMAP' : '', + + parameters.flatShading ? '#define FLAT_SHADED' : '', + + parameters.doubleSided ? '#define DOUBLE_SIDED' : '', + parameters.flipSided ? '#define FLIP_SIDED' : '', + + parameters.shadowMapEnabled ? '#define USE_SHADOWMAP' : '', + parameters.shadowMapEnabled ? '#define ' + shadowMapTypeDefine : '', + + parameters.premultipliedAlpha ? '#define PREMULTIPLIED_ALPHA' : '', + + parameters.numLightProbes > 0 ? '#define USE_LIGHT_PROBES' : '', + + parameters.useLegacyLights ? '#define LEGACY_LIGHTS' : '', + + parameters.decodeVideoTexture ? '#define DECODE_VIDEO_TEXTURE' : '', + + parameters.logarithmicDepthBuffer ? '#define USE_LOGDEPTHBUF' : '', + + 'uniform mat4 viewMatrix;', + 'uniform vec3 cameraPosition;', + 'uniform bool isOrthographic;', + + ( parameters.toneMapping !== NoToneMapping ) ? '#define TONE_MAPPING' : '', + ( parameters.toneMapping !== NoToneMapping ) ? ShaderChunk[ 'tonemapping_pars_fragment' ] : '', // this code is required here because it is used by the toneMapping() function defined below + ( parameters.toneMapping !== NoToneMapping ) ? getToneMappingFunction( 'toneMapping', parameters.toneMapping ) : '', + + parameters.dithering ? '#define DITHERING' : '', + parameters.opaque ? '#define OPAQUE' : '', + + ShaderChunk[ 'colorspace_pars_fragment' ], // this code is required here because it is used by the various encoding/decoding function defined below + getTexelEncodingFunction( 'linearToOutputTexel', parameters.outputColorSpace ), + + parameters.useDepthPacking ? '#define DEPTH_PACKING ' + parameters.depthPacking : '', + + '\n' + + ].filter( filterEmptyLine ).join( '\n' ); + + } + + vertexShader = resolveIncludes( vertexShader ); + vertexShader = replaceLightNums( vertexShader, parameters ); + vertexShader = replaceClippingPlaneNums( vertexShader, parameters ); + + fragmentShader = resolveIncludes( fragmentShader ); + fragmentShader = replaceLightNums( fragmentShader, parameters ); + fragmentShader = replaceClippingPlaneNums( fragmentShader, parameters ); + + vertexShader = unrollLoops( vertexShader ); + fragmentShader = unrollLoops( fragmentShader ); + + if ( parameters.isRawShaderMaterial !== true ) { + + // GLSL 3.0 conversion for built-in materials and ShaderMaterial + + versionString = '#version 300 es\n'; + + prefixVertex = [ + customVertexExtensions, + '#define attribute in', + '#define varying out', + '#define texture2D texture' + ].join( '\n' ) + '\n' + prefixVertex; + + prefixFragment = [ + '#define varying in', + ( parameters.glslVersion === GLSL3 ) ? '' : 'layout(location = 0) out highp vec4 pc_fragColor;', + ( parameters.glslVersion === GLSL3 ) ? '' : '#define gl_FragColor pc_fragColor', + '#define gl_FragDepthEXT gl_FragDepth', + '#define texture2D texture', + '#define textureCube texture', + '#define texture2DProj textureProj', + '#define texture2DLodEXT textureLod', + '#define texture2DProjLodEXT textureProjLod', + '#define textureCubeLodEXT textureLod', + '#define texture2DGradEXT textureGrad', + '#define texture2DProjGradEXT textureProjGrad', + '#define textureCubeGradEXT textureGrad' + ].join( '\n' ) + '\n' + prefixFragment; + + } + + const vertexGlsl = versionString + prefixVertex + vertexShader; + const fragmentGlsl = versionString + prefixFragment + fragmentShader; + + // console.log( '*VERTEX*', vertexGlsl ); + // console.log( '*FRAGMENT*', fragmentGlsl ); + + const glVertexShader = WebGLShader( gl, gl.VERTEX_SHADER, vertexGlsl ); + const glFragmentShader = WebGLShader( gl, gl.FRAGMENT_SHADER, fragmentGlsl ); + + gl.attachShader( program, glVertexShader ); + gl.attachShader( program, glFragmentShader ); + + // Force a particular attribute to index 0. + + if ( parameters.index0AttributeName !== undefined ) { + + gl.bindAttribLocation( program, 0, parameters.index0AttributeName ); + + } else if ( parameters.morphTargets === true ) { + + // programs with morphTargets displace position out of attribute 0 + gl.bindAttribLocation( program, 0, 'position' ); + + } + + gl.linkProgram( program ); + + function onFirstUse( self ) { + + // check for link errors + if ( renderer.debug.checkShaderErrors ) { + + const programLog = gl.getProgramInfoLog( program ).trim(); + const vertexLog = gl.getShaderInfoLog( glVertexShader ).trim(); + const fragmentLog = gl.getShaderInfoLog( glFragmentShader ).trim(); + + let runnable = true; + let haveDiagnostics = true; + + if ( gl.getProgramParameter( program, gl.LINK_STATUS ) === false ) { + + runnable = false; + + if ( typeof renderer.debug.onShaderError === 'function' ) { + + renderer.debug.onShaderError( gl, program, glVertexShader, glFragmentShader ); + + } else { + + // default error reporting + + const vertexErrors = getShaderErrors( gl, glVertexShader, 'vertex' ); + const fragmentErrors = getShaderErrors( gl, glFragmentShader, 'fragment' ); + + console.error( + 'THREE.WebGLProgram: Shader Error ' + gl.getError() + ' - ' + + 'VALIDATE_STATUS ' + gl.getProgramParameter( program, gl.VALIDATE_STATUS ) + '\n\n' + + 'Material Name: ' + self.name + '\n' + + 'Material Type: ' + self.type + '\n\n' + + 'Program Info Log: ' + programLog + '\n' + + vertexErrors + '\n' + + fragmentErrors + ); + + } + + } else if ( programLog !== '' ) { + + console.warn( 'THREE.WebGLProgram: Program Info Log:', programLog ); + + } else if ( vertexLog === '' || fragmentLog === '' ) { + + haveDiagnostics = false; + + } + + if ( haveDiagnostics ) { + + self.diagnostics = { + + runnable: runnable, + + programLog: programLog, + + vertexShader: { + + log: vertexLog, + prefix: prefixVertex + + }, + + fragmentShader: { + + log: fragmentLog, + prefix: prefixFragment + + } + + }; + + } + + } + + // Clean up + + // Crashes in iOS9 and iOS10. #18402 + // gl.detachShader( program, glVertexShader ); + // gl.detachShader( program, glFragmentShader ); + + gl.deleteShader( glVertexShader ); + gl.deleteShader( glFragmentShader ); + + cachedUniforms = new WebGLUniforms( gl, program ); + cachedAttributes = fetchAttributeLocations( gl, program ); + + } + + // set up caching for uniform locations + + let cachedUniforms; + + this.getUniforms = function () { + + if ( cachedUniforms === undefined ) { + + // Populates cachedUniforms and cachedAttributes + onFirstUse( this ); + + } + + return cachedUniforms; + + }; + + // set up caching for attribute locations + + let cachedAttributes; + + this.getAttributes = function () { + + if ( cachedAttributes === undefined ) { + + // Populates cachedAttributes and cachedUniforms + onFirstUse( this ); + + } + + return cachedAttributes; + + }; + + // indicate when the program is ready to be used. if the KHR_parallel_shader_compile extension isn't supported, + // flag the program as ready immediately. It may cause a stall when it's first used. + + let programReady = ( parameters.rendererExtensionParallelShaderCompile === false ); + + this.isReady = function () { + + if ( programReady === false ) { + + programReady = gl.getProgramParameter( program, COMPLETION_STATUS_KHR ); + + } + + return programReady; + + }; + + // free resource + + this.destroy = function () { + + bindingStates.releaseStatesOfProgram( this ); + + gl.deleteProgram( program ); + this.program = undefined; + + }; + + // + + this.type = parameters.shaderType; + this.name = parameters.shaderName; + this.id = programIdCount ++; + this.cacheKey = cacheKey; + this.usedTimes = 1; + this.program = program; + this.vertexShader = glVertexShader; + this.fragmentShader = glFragmentShader; + + return this; + +} + +let _id$1 = 0; + +class WebGLShaderCache { + + constructor() { + + this.shaderCache = new Map(); + this.materialCache = new Map(); + + } + + update( material ) { + + const vertexShader = material.vertexShader; + const fragmentShader = material.fragmentShader; + + const vertexShaderStage = this._getShaderStage( vertexShader ); + const fragmentShaderStage = this._getShaderStage( fragmentShader ); + + const materialShaders = this._getShaderCacheForMaterial( material ); + + if ( materialShaders.has( vertexShaderStage ) === false ) { + + materialShaders.add( vertexShaderStage ); + vertexShaderStage.usedTimes ++; + + } + + if ( materialShaders.has( fragmentShaderStage ) === false ) { + + materialShaders.add( fragmentShaderStage ); + fragmentShaderStage.usedTimes ++; + + } + + return this; + + } + + remove( material ) { + + const materialShaders = this.materialCache.get( material ); + + for ( const shaderStage of materialShaders ) { + + shaderStage.usedTimes --; + + if ( shaderStage.usedTimes === 0 ) this.shaderCache.delete( shaderStage.code ); + + } + + this.materialCache.delete( material ); + + return this; + + } + + getVertexShaderID( material ) { + + return this._getShaderStage( material.vertexShader ).id; + + } + + getFragmentShaderID( material ) { + + return this._getShaderStage( material.fragmentShader ).id; + + } + + dispose() { + + this.shaderCache.clear(); + this.materialCache.clear(); + + } + + _getShaderCacheForMaterial( material ) { + + const cache = this.materialCache; + let set = cache.get( material ); + + if ( set === undefined ) { + + set = new Set(); + cache.set( material, set ); + + } + + return set; + + } + + _getShaderStage( code ) { + + const cache = this.shaderCache; + let stage = cache.get( code ); + + if ( stage === undefined ) { + + stage = new WebGLShaderStage( code ); + cache.set( code, stage ); + + } + + return stage; + + } + +} + +class WebGLShaderStage { + + constructor( code ) { + + this.id = _id$1 ++; + + this.code = code; + this.usedTimes = 0; + + } + +} + +function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities, bindingStates, clipping ) { + + const _programLayers = new Layers(); + const _customShaders = new WebGLShaderCache(); + const _activeChannels = new Set(); + const programs = []; + + const logarithmicDepthBuffer = capabilities.logarithmicDepthBuffer; + const SUPPORTS_VERTEX_TEXTURES = capabilities.vertexTextures; + + let precision = capabilities.precision; + + const shaderIDs = { + MeshDepthMaterial: 'depth', + MeshDistanceMaterial: 'distanceRGBA', + MeshNormalMaterial: 'normal', + MeshBasicMaterial: 'basic', + MeshLambertMaterial: 'lambert', + MeshPhongMaterial: 'phong', + MeshToonMaterial: 'toon', + MeshStandardMaterial: 'physical', + MeshPhysicalMaterial: 'physical', + MeshMatcapMaterial: 'matcap', + LineBasicMaterial: 'basic', + LineDashedMaterial: 'dashed', + PointsMaterial: 'points', + ShadowMaterial: 'shadow', + SpriteMaterial: 'sprite' + }; + + function getChannel( value ) { + + _activeChannels.add( value ); + + if ( value === 0 ) return 'uv'; + + return `uv${ value }`; + + } + + function getParameters( material, lights, shadows, scene, object ) { + + const fog = scene.fog; + const geometry = object.geometry; + const environment = material.isMeshStandardMaterial ? scene.environment : null; + + const envMap = ( material.isMeshStandardMaterial ? cubeuvmaps : cubemaps ).get( material.envMap || environment ); + const envMapCubeUVHeight = ( !! envMap ) && ( envMap.mapping === CubeUVReflectionMapping ) ? envMap.image.height : null; + + const shaderID = shaderIDs[ material.type ]; + + // heuristics to create shader parameters according to lights in the scene + // (not to blow over maxLights budget) + + if ( material.precision !== null ) { + + precision = capabilities.getMaxPrecision( material.precision ); + + if ( precision !== material.precision ) { + + console.warn( 'THREE.WebGLProgram.getParameters:', material.precision, 'not supported, using', precision, 'instead.' ); + + } + + } + + // + + const morphAttribute = geometry.morphAttributes.position || geometry.morphAttributes.normal || geometry.morphAttributes.color; + const morphTargetsCount = ( morphAttribute !== undefined ) ? morphAttribute.length : 0; + + let morphTextureStride = 0; + + if ( geometry.morphAttributes.position !== undefined ) morphTextureStride = 1; + if ( geometry.morphAttributes.normal !== undefined ) morphTextureStride = 2; + if ( geometry.morphAttributes.color !== undefined ) morphTextureStride = 3; + + // + + let vertexShader, fragmentShader; + let customVertexShaderID, customFragmentShaderID; + + if ( shaderID ) { + + const shader = ShaderLib[ shaderID ]; + + vertexShader = shader.vertexShader; + fragmentShader = shader.fragmentShader; + + } else { + + vertexShader = material.vertexShader; + fragmentShader = material.fragmentShader; + + _customShaders.update( material ); + + customVertexShaderID = _customShaders.getVertexShaderID( material ); + customFragmentShaderID = _customShaders.getFragmentShaderID( material ); + + } + + const currentRenderTarget = renderer.getRenderTarget(); + + const IS_INSTANCEDMESH = object.isInstancedMesh === true; + const IS_BATCHEDMESH = object.isBatchedMesh === true; + + const HAS_MAP = !! material.map; + const HAS_MATCAP = !! material.matcap; + const HAS_ENVMAP = !! envMap; + const HAS_AOMAP = !! material.aoMap; + const HAS_LIGHTMAP = !! material.lightMap; + const HAS_BUMPMAP = !! material.bumpMap; + const HAS_NORMALMAP = !! material.normalMap; + const HAS_DISPLACEMENTMAP = !! material.displacementMap; + const HAS_EMISSIVEMAP = !! material.emissiveMap; + + const HAS_METALNESSMAP = !! material.metalnessMap; + const HAS_ROUGHNESSMAP = !! material.roughnessMap; + + const HAS_ANISOTROPY = material.anisotropy > 0; + const HAS_CLEARCOAT = material.clearcoat > 0; + const HAS_DISPERSION = material.dispersion > 0; + const HAS_IRIDESCENCE = material.iridescence > 0; + const HAS_SHEEN = material.sheen > 0; + const HAS_TRANSMISSION = material.transmission > 0; + + const HAS_ANISOTROPYMAP = HAS_ANISOTROPY && !! material.anisotropyMap; + + const HAS_CLEARCOATMAP = HAS_CLEARCOAT && !! material.clearcoatMap; + const HAS_CLEARCOAT_NORMALMAP = HAS_CLEARCOAT && !! material.clearcoatNormalMap; + const HAS_CLEARCOAT_ROUGHNESSMAP = HAS_CLEARCOAT && !! material.clearcoatRoughnessMap; + + const HAS_IRIDESCENCEMAP = HAS_IRIDESCENCE && !! material.iridescenceMap; + const HAS_IRIDESCENCE_THICKNESSMAP = HAS_IRIDESCENCE && !! material.iridescenceThicknessMap; + + const HAS_SHEEN_COLORMAP = HAS_SHEEN && !! material.sheenColorMap; + const HAS_SHEEN_ROUGHNESSMAP = HAS_SHEEN && !! material.sheenRoughnessMap; + + const HAS_SPECULARMAP = !! material.specularMap; + const HAS_SPECULAR_COLORMAP = !! material.specularColorMap; + const HAS_SPECULAR_INTENSITYMAP = !! material.specularIntensityMap; + + const HAS_TRANSMISSIONMAP = HAS_TRANSMISSION && !! material.transmissionMap; + const HAS_THICKNESSMAP = HAS_TRANSMISSION && !! material.thicknessMap; + + const HAS_GRADIENTMAP = !! material.gradientMap; + + const HAS_ALPHAMAP = !! material.alphaMap; + + const HAS_ALPHATEST = material.alphaTest > 0; + + const HAS_ALPHAHASH = !! material.alphaHash; + + const HAS_EXTENSIONS = !! material.extensions; + + let toneMapping = NoToneMapping; + + if ( material.toneMapped ) { + + if ( currentRenderTarget === null || currentRenderTarget.isXRRenderTarget === true ) { + + toneMapping = renderer.toneMapping; + + } + + } + + const parameters = { + + shaderID: shaderID, + shaderType: material.type, + shaderName: material.name, + + vertexShader: vertexShader, + fragmentShader: fragmentShader, + defines: material.defines, + + customVertexShaderID: customVertexShaderID, + customFragmentShaderID: customFragmentShaderID, + + isRawShaderMaterial: material.isRawShaderMaterial === true, + glslVersion: material.glslVersion, + + precision: precision, + + batching: IS_BATCHEDMESH, + instancing: IS_INSTANCEDMESH, + instancingColor: IS_INSTANCEDMESH && object.instanceColor !== null, + instancingMorph: IS_INSTANCEDMESH && object.morphTexture !== null, + + supportsVertexTextures: SUPPORTS_VERTEX_TEXTURES, + outputColorSpace: ( currentRenderTarget === null ) ? renderer.outputColorSpace : ( currentRenderTarget.isXRRenderTarget === true ? currentRenderTarget.texture.colorSpace : LinearSRGBColorSpace ), + alphaToCoverage: !! material.alphaToCoverage, + + map: HAS_MAP, + matcap: HAS_MATCAP, + envMap: HAS_ENVMAP, + envMapMode: HAS_ENVMAP && envMap.mapping, + envMapCubeUVHeight: envMapCubeUVHeight, + aoMap: HAS_AOMAP, + lightMap: HAS_LIGHTMAP, + bumpMap: HAS_BUMPMAP, + normalMap: HAS_NORMALMAP, + displacementMap: SUPPORTS_VERTEX_TEXTURES && HAS_DISPLACEMENTMAP, + emissiveMap: HAS_EMISSIVEMAP, + + normalMapObjectSpace: HAS_NORMALMAP && material.normalMapType === ObjectSpaceNormalMap, + normalMapTangentSpace: HAS_NORMALMAP && material.normalMapType === TangentSpaceNormalMap, + + metalnessMap: HAS_METALNESSMAP, + roughnessMap: HAS_ROUGHNESSMAP, + + anisotropy: HAS_ANISOTROPY, + anisotropyMap: HAS_ANISOTROPYMAP, + + clearcoat: HAS_CLEARCOAT, + clearcoatMap: HAS_CLEARCOATMAP, + clearcoatNormalMap: HAS_CLEARCOAT_NORMALMAP, + clearcoatRoughnessMap: HAS_CLEARCOAT_ROUGHNESSMAP, + + dispersion: HAS_DISPERSION, + + iridescence: HAS_IRIDESCENCE, + iridescenceMap: HAS_IRIDESCENCEMAP, + iridescenceThicknessMap: HAS_IRIDESCENCE_THICKNESSMAP, + + sheen: HAS_SHEEN, + sheenColorMap: HAS_SHEEN_COLORMAP, + sheenRoughnessMap: HAS_SHEEN_ROUGHNESSMAP, + + specularMap: HAS_SPECULARMAP, + specularColorMap: HAS_SPECULAR_COLORMAP, + specularIntensityMap: HAS_SPECULAR_INTENSITYMAP, + + transmission: HAS_TRANSMISSION, + transmissionMap: HAS_TRANSMISSIONMAP, + thicknessMap: HAS_THICKNESSMAP, + + gradientMap: HAS_GRADIENTMAP, + + opaque: material.transparent === false && material.blending === NormalBlending && material.alphaToCoverage === false, + + alphaMap: HAS_ALPHAMAP, + alphaTest: HAS_ALPHATEST, + alphaHash: HAS_ALPHAHASH, + + combine: material.combine, + + // + + mapUv: HAS_MAP && getChannel( material.map.channel ), + aoMapUv: HAS_AOMAP && getChannel( material.aoMap.channel ), + lightMapUv: HAS_LIGHTMAP && getChannel( material.lightMap.channel ), + bumpMapUv: HAS_BUMPMAP && getChannel( material.bumpMap.channel ), + normalMapUv: HAS_NORMALMAP && getChannel( material.normalMap.channel ), + displacementMapUv: HAS_DISPLACEMENTMAP && getChannel( material.displacementMap.channel ), + emissiveMapUv: HAS_EMISSIVEMAP && getChannel( material.emissiveMap.channel ), + + metalnessMapUv: HAS_METALNESSMAP && getChannel( material.metalnessMap.channel ), + roughnessMapUv: HAS_ROUGHNESSMAP && getChannel( material.roughnessMap.channel ), + + anisotropyMapUv: HAS_ANISOTROPYMAP && getChannel( material.anisotropyMap.channel ), + + clearcoatMapUv: HAS_CLEARCOATMAP && getChannel( material.clearcoatMap.channel ), + clearcoatNormalMapUv: HAS_CLEARCOAT_NORMALMAP && getChannel( material.clearcoatNormalMap.channel ), + clearcoatRoughnessMapUv: HAS_CLEARCOAT_ROUGHNESSMAP && getChannel( material.clearcoatRoughnessMap.channel ), + + iridescenceMapUv: HAS_IRIDESCENCEMAP && getChannel( material.iridescenceMap.channel ), + iridescenceThicknessMapUv: HAS_IRIDESCENCE_THICKNESSMAP && getChannel( material.iridescenceThicknessMap.channel ), + + sheenColorMapUv: HAS_SHEEN_COLORMAP && getChannel( material.sheenColorMap.channel ), + sheenRoughnessMapUv: HAS_SHEEN_ROUGHNESSMAP && getChannel( material.sheenRoughnessMap.channel ), + + specularMapUv: HAS_SPECULARMAP && getChannel( material.specularMap.channel ), + specularColorMapUv: HAS_SPECULAR_COLORMAP && getChannel( material.specularColorMap.channel ), + specularIntensityMapUv: HAS_SPECULAR_INTENSITYMAP && getChannel( material.specularIntensityMap.channel ), + + transmissionMapUv: HAS_TRANSMISSIONMAP && getChannel( material.transmissionMap.channel ), + thicknessMapUv: HAS_THICKNESSMAP && getChannel( material.thicknessMap.channel ), + + alphaMapUv: HAS_ALPHAMAP && getChannel( material.alphaMap.channel ), + + // + + vertexTangents: !! geometry.attributes.tangent && ( HAS_NORMALMAP || HAS_ANISOTROPY ), + vertexColors: material.vertexColors, + vertexAlphas: material.vertexColors === true && !! geometry.attributes.color && geometry.attributes.color.itemSize === 4, + + pointsUvs: object.isPoints === true && !! geometry.attributes.uv && ( HAS_MAP || HAS_ALPHAMAP ), + + fog: !! fog, + useFog: material.fog === true, + fogExp2: ( !! fog && fog.isFogExp2 ), + + flatShading: material.flatShading === true, + + sizeAttenuation: material.sizeAttenuation === true, + logarithmicDepthBuffer: logarithmicDepthBuffer, + + skinning: object.isSkinnedMesh === true, + + morphTargets: geometry.morphAttributes.position !== undefined, + morphNormals: geometry.morphAttributes.normal !== undefined, + morphColors: geometry.morphAttributes.color !== undefined, + morphTargetsCount: morphTargetsCount, + morphTextureStride: morphTextureStride, + + numDirLights: lights.directional.length, + numPointLights: lights.point.length, + numSpotLights: lights.spot.length, + numSpotLightMaps: lights.spotLightMap.length, + numRectAreaLights: lights.rectArea.length, + numHemiLights: lights.hemi.length, + + numDirLightShadows: lights.directionalShadowMap.length, + numPointLightShadows: lights.pointShadowMap.length, + numSpotLightShadows: lights.spotShadowMap.length, + numSpotLightShadowsWithMaps: lights.numSpotLightShadowsWithMaps, + + numLightProbes: lights.numLightProbes, + + numClippingPlanes: clipping.numPlanes, + numClipIntersection: clipping.numIntersection, + + dithering: material.dithering, + + shadowMapEnabled: renderer.shadowMap.enabled && shadows.length > 0, + shadowMapType: renderer.shadowMap.type, + + toneMapping: toneMapping, + useLegacyLights: renderer._useLegacyLights, + + decodeVideoTexture: HAS_MAP && ( material.map.isVideoTexture === true ) && ( ColorManagement.getTransfer( material.map.colorSpace ) === SRGBTransfer ), + + premultipliedAlpha: material.premultipliedAlpha, + + doubleSided: material.side === DoubleSide, + flipSided: material.side === BackSide, + + useDepthPacking: material.depthPacking >= 0, + depthPacking: material.depthPacking || 0, + + index0AttributeName: material.index0AttributeName, + + extensionClipCullDistance: HAS_EXTENSIONS && material.extensions.clipCullDistance === true && extensions.has( 'WEBGL_clip_cull_distance' ), + extensionMultiDraw: HAS_EXTENSIONS && material.extensions.multiDraw === true && extensions.has( 'WEBGL_multi_draw' ), + + rendererExtensionParallelShaderCompile: extensions.has( 'KHR_parallel_shader_compile' ), + + customProgramCacheKey: material.customProgramCacheKey() + + }; + + // the usage of getChannel() determines the active texture channels for this shader + + parameters.vertexUv1s = _activeChannels.has( 1 ); + parameters.vertexUv2s = _activeChannels.has( 2 ); + parameters.vertexUv3s = _activeChannels.has( 3 ); + + _activeChannels.clear(); + + return parameters; + + } + + function getProgramCacheKey( parameters ) { + + const array = []; + + if ( parameters.shaderID ) { + + array.push( parameters.shaderID ); + + } else { + + array.push( parameters.customVertexShaderID ); + array.push( parameters.customFragmentShaderID ); + + } + + if ( parameters.defines !== undefined ) { + + for ( const name in parameters.defines ) { + + array.push( name ); + array.push( parameters.defines[ name ] ); + + } + + } + + if ( parameters.isRawShaderMaterial === false ) { + + getProgramCacheKeyParameters( array, parameters ); + getProgramCacheKeyBooleans( array, parameters ); + array.push( renderer.outputColorSpace ); + + } + + array.push( parameters.customProgramCacheKey ); + + return array.join(); + + } + + function getProgramCacheKeyParameters( array, parameters ) { + + array.push( parameters.precision ); + array.push( parameters.outputColorSpace ); + array.push( parameters.envMapMode ); + array.push( parameters.envMapCubeUVHeight ); + array.push( parameters.mapUv ); + array.push( parameters.alphaMapUv ); + array.push( parameters.lightMapUv ); + array.push( parameters.aoMapUv ); + array.push( parameters.bumpMapUv ); + array.push( parameters.normalMapUv ); + array.push( parameters.displacementMapUv ); + array.push( parameters.emissiveMapUv ); + array.push( parameters.metalnessMapUv ); + array.push( parameters.roughnessMapUv ); + array.push( parameters.anisotropyMapUv ); + array.push( parameters.clearcoatMapUv ); + array.push( parameters.clearcoatNormalMapUv ); + array.push( parameters.clearcoatRoughnessMapUv ); + array.push( parameters.iridescenceMapUv ); + array.push( parameters.iridescenceThicknessMapUv ); + array.push( parameters.sheenColorMapUv ); + array.push( parameters.sheenRoughnessMapUv ); + array.push( parameters.specularMapUv ); + array.push( parameters.specularColorMapUv ); + array.push( parameters.specularIntensityMapUv ); + array.push( parameters.transmissionMapUv ); + array.push( parameters.thicknessMapUv ); + array.push( parameters.combine ); + array.push( parameters.fogExp2 ); + array.push( parameters.sizeAttenuation ); + array.push( parameters.morphTargetsCount ); + array.push( parameters.morphAttributeCount ); + array.push( parameters.numDirLights ); + array.push( parameters.numPointLights ); + array.push( parameters.numSpotLights ); + array.push( parameters.numSpotLightMaps ); + array.push( parameters.numHemiLights ); + array.push( parameters.numRectAreaLights ); + array.push( parameters.numDirLightShadows ); + array.push( parameters.numPointLightShadows ); + array.push( parameters.numSpotLightShadows ); + array.push( parameters.numSpotLightShadowsWithMaps ); + array.push( parameters.numLightProbes ); + array.push( parameters.shadowMapType ); + array.push( parameters.toneMapping ); + array.push( parameters.numClippingPlanes ); + array.push( parameters.numClipIntersection ); + array.push( parameters.depthPacking ); + + } + + function getProgramCacheKeyBooleans( array, parameters ) { + + _programLayers.disableAll(); + + if ( parameters.supportsVertexTextures ) + _programLayers.enable( 0 ); + if ( parameters.instancing ) + _programLayers.enable( 1 ); + if ( parameters.instancingColor ) + _programLayers.enable( 2 ); + if ( parameters.instancingMorph ) + _programLayers.enable( 3 ); + if ( parameters.matcap ) + _programLayers.enable( 4 ); + if ( parameters.envMap ) + _programLayers.enable( 5 ); + if ( parameters.normalMapObjectSpace ) + _programLayers.enable( 6 ); + if ( parameters.normalMapTangentSpace ) + _programLayers.enable( 7 ); + if ( parameters.clearcoat ) + _programLayers.enable( 8 ); + if ( parameters.iridescence ) + _programLayers.enable( 9 ); + if ( parameters.alphaTest ) + _programLayers.enable( 10 ); + if ( parameters.vertexColors ) + _programLayers.enable( 11 ); + if ( parameters.vertexAlphas ) + _programLayers.enable( 12 ); + if ( parameters.vertexUv1s ) + _programLayers.enable( 13 ); + if ( parameters.vertexUv2s ) + _programLayers.enable( 14 ); + if ( parameters.vertexUv3s ) + _programLayers.enable( 15 ); + if ( parameters.vertexTangents ) + _programLayers.enable( 16 ); + if ( parameters.anisotropy ) + _programLayers.enable( 17 ); + if ( parameters.alphaHash ) + _programLayers.enable( 18 ); + if ( parameters.batching ) + _programLayers.enable( 19 ); + if ( parameters.dispersion ) + _programLayers.enable( 20 ); + + array.push( _programLayers.mask ); + _programLayers.disableAll(); + + if ( parameters.fog ) + _programLayers.enable( 0 ); + if ( parameters.useFog ) + _programLayers.enable( 1 ); + if ( parameters.flatShading ) + _programLayers.enable( 2 ); + if ( parameters.logarithmicDepthBuffer ) + _programLayers.enable( 3 ); + if ( parameters.skinning ) + _programLayers.enable( 4 ); + if ( parameters.morphTargets ) + _programLayers.enable( 5 ); + if ( parameters.morphNormals ) + _programLayers.enable( 6 ); + if ( parameters.morphColors ) + _programLayers.enable( 7 ); + if ( parameters.premultipliedAlpha ) + _programLayers.enable( 8 ); + if ( parameters.shadowMapEnabled ) + _programLayers.enable( 9 ); + if ( parameters.useLegacyLights ) + _programLayers.enable( 10 ); + if ( parameters.doubleSided ) + _programLayers.enable( 11 ); + if ( parameters.flipSided ) + _programLayers.enable( 12 ); + if ( parameters.useDepthPacking ) + _programLayers.enable( 13 ); + if ( parameters.dithering ) + _programLayers.enable( 14 ); + if ( parameters.transmission ) + _programLayers.enable( 15 ); + if ( parameters.sheen ) + _programLayers.enable( 16 ); + if ( parameters.opaque ) + _programLayers.enable( 17 ); + if ( parameters.pointsUvs ) + _programLayers.enable( 18 ); + if ( parameters.decodeVideoTexture ) + _programLayers.enable( 19 ); + if ( parameters.alphaToCoverage ) + _programLayers.enable( 20 ); + + array.push( _programLayers.mask ); + + } + + function getUniforms( material ) { + + const shaderID = shaderIDs[ material.type ]; + let uniforms; + + if ( shaderID ) { + + const shader = ShaderLib[ shaderID ]; + uniforms = UniformsUtils.clone( shader.uniforms ); + + } else { + + uniforms = material.uniforms; + + } + + return uniforms; + + } + + function acquireProgram( parameters, cacheKey ) { + + let program; + + // Check if code has been already compiled + for ( let p = 0, pl = programs.length; p < pl; p ++ ) { + + const preexistingProgram = programs[ p ]; + + if ( preexistingProgram.cacheKey === cacheKey ) { + + program = preexistingProgram; + ++ program.usedTimes; + + break; + + } + + } + + if ( program === undefined ) { + + program = new WebGLProgram( renderer, cacheKey, parameters, bindingStates ); + programs.push( program ); + + } + + return program; + + } + + function releaseProgram( program ) { + + if ( -- program.usedTimes === 0 ) { + + // Remove from unordered set + const i = programs.indexOf( program ); + programs[ i ] = programs[ programs.length - 1 ]; + programs.pop(); + + // Free WebGL resources + program.destroy(); + + } + + } + + function releaseShaderCache( material ) { + + _customShaders.remove( material ); + + } + + function dispose() { + + _customShaders.dispose(); + + } + + return { + getParameters: getParameters, + getProgramCacheKey: getProgramCacheKey, + getUniforms: getUniforms, + acquireProgram: acquireProgram, + releaseProgram: releaseProgram, + releaseShaderCache: releaseShaderCache, + // Exposed for resource monitoring & error feedback via renderer.info: + programs: programs, + dispose: dispose + }; + +} + +function WebGLProperties() { + + let properties = new WeakMap(); + + function get( object ) { + + let map = properties.get( object ); + + if ( map === undefined ) { + + map = {}; + properties.set( object, map ); + + } + + return map; + + } + + function remove( object ) { + + properties.delete( object ); + + } + + function update( object, key, value ) { + + properties.get( object )[ key ] = value; + + } + + function dispose() { + + properties = new WeakMap(); + + } + + return { + get: get, + remove: remove, + update: update, + dispose: dispose + }; + +} + +function painterSortStable( a, b ) { + + if ( a.groupOrder !== b.groupOrder ) { + + return a.groupOrder - b.groupOrder; + + } else if ( a.renderOrder !== b.renderOrder ) { + + return a.renderOrder - b.renderOrder; + + } else if ( a.material.id !== b.material.id ) { + + return a.material.id - b.material.id; + + } else if ( a.z !== b.z ) { + + return a.z - b.z; + + } else { + + return a.id - b.id; + + } + +} + +function reversePainterSortStable( a, b ) { + + if ( a.groupOrder !== b.groupOrder ) { + + return a.groupOrder - b.groupOrder; + + } else if ( a.renderOrder !== b.renderOrder ) { + + return a.renderOrder - b.renderOrder; + + } else if ( a.z !== b.z ) { + + return b.z - a.z; + + } else { + + return a.id - b.id; + + } + +} + + +function WebGLRenderList() { + + const renderItems = []; + let renderItemsIndex = 0; + + const opaque = []; + const transmissive = []; + const transparent = []; + + function init() { + + renderItemsIndex = 0; + + opaque.length = 0; + transmissive.length = 0; + transparent.length = 0; + + } + + function getNextRenderItem( object, geometry, material, groupOrder, z, group ) { + + let renderItem = renderItems[ renderItemsIndex ]; + + if ( renderItem === undefined ) { + + renderItem = { + id: object.id, + object: object, + geometry: geometry, + material: material, + groupOrder: groupOrder, + renderOrder: object.renderOrder, + z: z, + group: group + }; + + renderItems[ renderItemsIndex ] = renderItem; + + } else { + + renderItem.id = object.id; + renderItem.object = object; + renderItem.geometry = geometry; + renderItem.material = material; + renderItem.groupOrder = groupOrder; + renderItem.renderOrder = object.renderOrder; + renderItem.z = z; + renderItem.group = group; + + } + + renderItemsIndex ++; + + return renderItem; + + } + + function push( object, geometry, material, groupOrder, z, group ) { + + const renderItem = getNextRenderItem( object, geometry, material, groupOrder, z, group ); + + if ( material.transmission > 0.0 ) { + + transmissive.push( renderItem ); + + } else if ( material.transparent === true ) { + + transparent.push( renderItem ); + + } else { + + opaque.push( renderItem ); + + } + + } + + function unshift( object, geometry, material, groupOrder, z, group ) { + + const renderItem = getNextRenderItem( object, geometry, material, groupOrder, z, group ); + + if ( material.transmission > 0.0 ) { + + transmissive.unshift( renderItem ); + + } else if ( material.transparent === true ) { + + transparent.unshift( renderItem ); + + } else { + + opaque.unshift( renderItem ); + + } + + } + + function sort( customOpaqueSort, customTransparentSort ) { + + if ( opaque.length > 1 ) opaque.sort( customOpaqueSort || painterSortStable ); + if ( transmissive.length > 1 ) transmissive.sort( customTransparentSort || reversePainterSortStable ); + if ( transparent.length > 1 ) transparent.sort( customTransparentSort || reversePainterSortStable ); + + } + + function finish() { + + // Clear references from inactive renderItems in the list + + for ( let i = renderItemsIndex, il = renderItems.length; i < il; i ++ ) { + + const renderItem = renderItems[ i ]; + + if ( renderItem.id === null ) break; + + renderItem.id = null; + renderItem.object = null; + renderItem.geometry = null; + renderItem.material = null; + renderItem.group = null; + + } + + } + + return { + + opaque: opaque, + transmissive: transmissive, + transparent: transparent, + + init: init, + push: push, + unshift: unshift, + finish: finish, + + sort: sort + }; + +} + +function WebGLRenderLists() { + + let lists = new WeakMap(); + + function get( scene, renderCallDepth ) { + + const listArray = lists.get( scene ); + let list; + + if ( listArray === undefined ) { + + list = new WebGLRenderList(); + lists.set( scene, [ list ] ); + + } else { + + if ( renderCallDepth >= listArray.length ) { + + list = new WebGLRenderList(); + listArray.push( list ); + + } else { + + list = listArray[ renderCallDepth ]; + + } + + } + + return list; + + } + + function dispose() { + + lists = new WeakMap(); + + } + + return { + get: get, + dispose: dispose + }; + +} + +function UniformsCache() { + + const lights = {}; + + return { + + get: function ( light ) { + + if ( lights[ light.id ] !== undefined ) { + + return lights[ light.id ]; + + } + + let uniforms; + + switch ( light.type ) { + + case 'DirectionalLight': + uniforms = { + direction: new Vector3(), + color: new Color() + }; + break; + + case 'SpotLight': + uniforms = { + position: new Vector3(), + direction: new Vector3(), + color: new Color(), + distance: 0, + coneCos: 0, + penumbraCos: 0, + decay: 0 + }; + break; + + case 'PointLight': + uniforms = { + position: new Vector3(), + color: new Color(), + distance: 0, + decay: 0 + }; + break; + + case 'HemisphereLight': + uniforms = { + direction: new Vector3(), + skyColor: new Color(), + groundColor: new Color() + }; + break; + + case 'RectAreaLight': + uniforms = { + color: new Color(), + position: new Vector3(), + halfWidth: new Vector3(), + halfHeight: new Vector3() + }; + break; + + } + + lights[ light.id ] = uniforms; + + return uniforms; + + } + + }; + +} + +function ShadowUniformsCache() { + + const lights = {}; + + return { + + get: function ( light ) { + + if ( lights[ light.id ] !== undefined ) { + + return lights[ light.id ]; + + } + + let uniforms; + + switch ( light.type ) { + + case 'DirectionalLight': + uniforms = { + shadowBias: 0, + shadowNormalBias: 0, + shadowRadius: 1, + shadowMapSize: new Vector2() + }; + break; + + case 'SpotLight': + uniforms = { + shadowBias: 0, + shadowNormalBias: 0, + shadowRadius: 1, + shadowMapSize: new Vector2() + }; + break; + + case 'PointLight': + uniforms = { + shadowBias: 0, + shadowNormalBias: 0, + shadowRadius: 1, + shadowMapSize: new Vector2(), + shadowCameraNear: 1, + shadowCameraFar: 1000 + }; + break; + + // TODO (abelnation): set RectAreaLight shadow uniforms + + } + + lights[ light.id ] = uniforms; + + return uniforms; + + } + + }; + +} + + + +let nextVersion = 0; + +function shadowCastingAndTexturingLightsFirst( lightA, lightB ) { + + return ( lightB.castShadow ? 2 : 0 ) - ( lightA.castShadow ? 2 : 0 ) + ( lightB.map ? 1 : 0 ) - ( lightA.map ? 1 : 0 ); + +} + +function WebGLLights( extensions ) { + + const cache = new UniformsCache(); + + const shadowCache = ShadowUniformsCache(); + + const state = { + + version: 0, + + hash: { + directionalLength: - 1, + pointLength: - 1, + spotLength: - 1, + rectAreaLength: - 1, + hemiLength: - 1, + + numDirectionalShadows: - 1, + numPointShadows: - 1, + numSpotShadows: - 1, + numSpotMaps: - 1, + + numLightProbes: - 1 + }, + + ambient: [ 0, 0, 0 ], + probe: [], + directional: [], + directionalShadow: [], + directionalShadowMap: [], + directionalShadowMatrix: [], + spot: [], + spotLightMap: [], + spotShadow: [], + spotShadowMap: [], + spotLightMatrix: [], + rectArea: [], + rectAreaLTC1: null, + rectAreaLTC2: null, + point: [], + pointShadow: [], + pointShadowMap: [], + pointShadowMatrix: [], + hemi: [], + numSpotLightShadowsWithMaps: 0, + numLightProbes: 0 + + }; + + for ( let i = 0; i < 9; i ++ ) state.probe.push( new Vector3() ); + + const vector3 = new Vector3(); + const matrix4 = new Matrix4(); + const matrix42 = new Matrix4(); + + function setup( lights, useLegacyLights ) { + + let r = 0, g = 0, b = 0; + + for ( let i = 0; i < 9; i ++ ) state.probe[ i ].set( 0, 0, 0 ); + + let directionalLength = 0; + let pointLength = 0; + let spotLength = 0; + let rectAreaLength = 0; + let hemiLength = 0; + + let numDirectionalShadows = 0; + let numPointShadows = 0; + let numSpotShadows = 0; + let numSpotMaps = 0; + let numSpotShadowsWithMaps = 0; + + let numLightProbes = 0; + + // ordering : [shadow casting + map texturing, map texturing, shadow casting, none ] + lights.sort( shadowCastingAndTexturingLightsFirst ); + + // artist-friendly light intensity scaling factor + const scaleFactor = ( useLegacyLights === true ) ? Math.PI : 1; + + for ( let i = 0, l = lights.length; i < l; i ++ ) { + + const light = lights[ i ]; + + const color = light.color; + const intensity = light.intensity; + const distance = light.distance; + + const shadowMap = ( light.shadow && light.shadow.map ) ? light.shadow.map.texture : null; + + if ( light.isAmbientLight ) { + + r += color.r * intensity * scaleFactor; + g += color.g * intensity * scaleFactor; + b += color.b * intensity * scaleFactor; + + } else if ( light.isLightProbe ) { + + for ( let j = 0; j < 9; j ++ ) { + + state.probe[ j ].addScaledVector( light.sh.coefficients[ j ], intensity ); + + } + + numLightProbes ++; + + } else if ( light.isDirectionalLight ) { + + const uniforms = cache.get( light ); + + uniforms.color.copy( light.color ).multiplyScalar( light.intensity * scaleFactor ); + + if ( light.castShadow ) { + + const shadow = light.shadow; + + const shadowUniforms = shadowCache.get( light ); + + shadowUniforms.shadowBias = shadow.bias; + shadowUniforms.shadowNormalBias = shadow.normalBias; + shadowUniforms.shadowRadius = shadow.radius; + shadowUniforms.shadowMapSize = shadow.mapSize; + + state.directionalShadow[ directionalLength ] = shadowUniforms; + state.directionalShadowMap[ directionalLength ] = shadowMap; + state.directionalShadowMatrix[ directionalLength ] = light.shadow.matrix; + + numDirectionalShadows ++; + + } + + state.directional[ directionalLength ] = uniforms; + + directionalLength ++; + + } else if ( light.isSpotLight ) { + + const uniforms = cache.get( light ); + + uniforms.position.setFromMatrixPosition( light.matrixWorld ); + + uniforms.color.copy( color ).multiplyScalar( intensity * scaleFactor ); + uniforms.distance = distance; + + uniforms.coneCos = Math.cos( light.angle ); + uniforms.penumbraCos = Math.cos( light.angle * ( 1 - light.penumbra ) ); + uniforms.decay = light.decay; + + state.spot[ spotLength ] = uniforms; + + const shadow = light.shadow; + + if ( light.map ) { + + state.spotLightMap[ numSpotMaps ] = light.map; + numSpotMaps ++; + + // make sure the lightMatrix is up to date + // TODO : do it if required only + shadow.updateMatrices( light ); + + if ( light.castShadow ) numSpotShadowsWithMaps ++; + + } + + state.spotLightMatrix[ spotLength ] = shadow.matrix; + + if ( light.castShadow ) { + + const shadowUniforms = shadowCache.get( light ); + + shadowUniforms.shadowBias = shadow.bias; + shadowUniforms.shadowNormalBias = shadow.normalBias; + shadowUniforms.shadowRadius = shadow.radius; + shadowUniforms.shadowMapSize = shadow.mapSize; + + state.spotShadow[ spotLength ] = shadowUniforms; + state.spotShadowMap[ spotLength ] = shadowMap; + + numSpotShadows ++; + + } + + spotLength ++; + + } else if ( light.isRectAreaLight ) { + + const uniforms = cache.get( light ); + + uniforms.color.copy( color ).multiplyScalar( intensity ); + + uniforms.halfWidth.set( light.width * 0.5, 0.0, 0.0 ); + uniforms.halfHeight.set( 0.0, light.height * 0.5, 0.0 ); + + state.rectArea[ rectAreaLength ] = uniforms; + + rectAreaLength ++; + + } else if ( light.isPointLight ) { + + const uniforms = cache.get( light ); + + uniforms.color.copy( light.color ).multiplyScalar( light.intensity * scaleFactor ); + uniforms.distance = light.distance; + uniforms.decay = light.decay; + + if ( light.castShadow ) { + + const shadow = light.shadow; + + const shadowUniforms = shadowCache.get( light ); + + shadowUniforms.shadowBias = shadow.bias; + shadowUniforms.shadowNormalBias = shadow.normalBias; + shadowUniforms.shadowRadius = shadow.radius; + shadowUniforms.shadowMapSize = shadow.mapSize; + shadowUniforms.shadowCameraNear = shadow.camera.near; + shadowUniforms.shadowCameraFar = shadow.camera.far; + + state.pointShadow[ pointLength ] = shadowUniforms; + state.pointShadowMap[ pointLength ] = shadowMap; + state.pointShadowMatrix[ pointLength ] = light.shadow.matrix; + + numPointShadows ++; + + } + + state.point[ pointLength ] = uniforms; + + pointLength ++; + + } else if ( light.isHemisphereLight ) { + + const uniforms = cache.get( light ); + + uniforms.skyColor.copy( light.color ).multiplyScalar( intensity * scaleFactor ); + uniforms.groundColor.copy( light.groundColor ).multiplyScalar( intensity * scaleFactor ); + + state.hemi[ hemiLength ] = uniforms; + + hemiLength ++; + + } + + } + + if ( rectAreaLength > 0 ) { + + if ( extensions.has( 'OES_texture_float_linear' ) === true ) { + + state.rectAreaLTC1 = UniformsLib.LTC_FLOAT_1; + state.rectAreaLTC2 = UniformsLib.LTC_FLOAT_2; + + } else { + + state.rectAreaLTC1 = UniformsLib.LTC_HALF_1; + state.rectAreaLTC2 = UniformsLib.LTC_HALF_2; + + } + + } + + state.ambient[ 0 ] = r; + state.ambient[ 1 ] = g; + state.ambient[ 2 ] = b; + + const hash = state.hash; + + if ( hash.directionalLength !== directionalLength || + hash.pointLength !== pointLength || + hash.spotLength !== spotLength || + hash.rectAreaLength !== rectAreaLength || + hash.hemiLength !== hemiLength || + hash.numDirectionalShadows !== numDirectionalShadows || + hash.numPointShadows !== numPointShadows || + hash.numSpotShadows !== numSpotShadows || + hash.numSpotMaps !== numSpotMaps || + hash.numLightProbes !== numLightProbes ) { + + state.directional.length = directionalLength; + state.spot.length = spotLength; + state.rectArea.length = rectAreaLength; + state.point.length = pointLength; + state.hemi.length = hemiLength; + + state.directionalShadow.length = numDirectionalShadows; + state.directionalShadowMap.length = numDirectionalShadows; + state.pointShadow.length = numPointShadows; + state.pointShadowMap.length = numPointShadows; + state.spotShadow.length = numSpotShadows; + state.spotShadowMap.length = numSpotShadows; + state.directionalShadowMatrix.length = numDirectionalShadows; + state.pointShadowMatrix.length = numPointShadows; + state.spotLightMatrix.length = numSpotShadows + numSpotMaps - numSpotShadowsWithMaps; + state.spotLightMap.length = numSpotMaps; + state.numSpotLightShadowsWithMaps = numSpotShadowsWithMaps; + state.numLightProbes = numLightProbes; + + hash.directionalLength = directionalLength; + hash.pointLength = pointLength; + hash.spotLength = spotLength; + hash.rectAreaLength = rectAreaLength; + hash.hemiLength = hemiLength; + + hash.numDirectionalShadows = numDirectionalShadows; + hash.numPointShadows = numPointShadows; + hash.numSpotShadows = numSpotShadows; + hash.numSpotMaps = numSpotMaps; + + hash.numLightProbes = numLightProbes; + + state.version = nextVersion ++; + + } + + } + + function setupView( lights, camera ) { + + let directionalLength = 0; + let pointLength = 0; + let spotLength = 0; + let rectAreaLength = 0; + let hemiLength = 0; + + const viewMatrix = camera.matrixWorldInverse; + + for ( let i = 0, l = lights.length; i < l; i ++ ) { + + const light = lights[ i ]; + + if ( light.isDirectionalLight ) { + + const uniforms = state.directional[ directionalLength ]; + + uniforms.direction.setFromMatrixPosition( light.matrixWorld ); + vector3.setFromMatrixPosition( light.target.matrixWorld ); + uniforms.direction.sub( vector3 ); + uniforms.direction.transformDirection( viewMatrix ); + + directionalLength ++; + + } else if ( light.isSpotLight ) { + + const uniforms = state.spot[ spotLength ]; + + uniforms.position.setFromMatrixPosition( light.matrixWorld ); + uniforms.position.applyMatrix4( viewMatrix ); + + uniforms.direction.setFromMatrixPosition( light.matrixWorld ); + vector3.setFromMatrixPosition( light.target.matrixWorld ); + uniforms.direction.sub( vector3 ); + uniforms.direction.transformDirection( viewMatrix ); + + spotLength ++; + + } else if ( light.isRectAreaLight ) { + + const uniforms = state.rectArea[ rectAreaLength ]; + + uniforms.position.setFromMatrixPosition( light.matrixWorld ); + uniforms.position.applyMatrix4( viewMatrix ); + + // extract local rotation of light to derive width/height half vectors + matrix42.identity(); + matrix4.copy( light.matrixWorld ); + matrix4.premultiply( viewMatrix ); + matrix42.extractRotation( matrix4 ); + + uniforms.halfWidth.set( light.width * 0.5, 0.0, 0.0 ); + uniforms.halfHeight.set( 0.0, light.height * 0.5, 0.0 ); + + uniforms.halfWidth.applyMatrix4( matrix42 ); + uniforms.halfHeight.applyMatrix4( matrix42 ); + + rectAreaLength ++; + + } else if ( light.isPointLight ) { + + const uniforms = state.point[ pointLength ]; + + uniforms.position.setFromMatrixPosition( light.matrixWorld ); + uniforms.position.applyMatrix4( viewMatrix ); + + pointLength ++; + + } else if ( light.isHemisphereLight ) { + + const uniforms = state.hemi[ hemiLength ]; + + uniforms.direction.setFromMatrixPosition( light.matrixWorld ); + uniforms.direction.transformDirection( viewMatrix ); + + hemiLength ++; + + } + + } + + } + + return { + setup: setup, + setupView: setupView, + state: state + }; + +} + +function WebGLRenderState( extensions ) { + + const lights = new WebGLLights( extensions ); + + const lightsArray = []; + const shadowsArray = []; + + function init( camera ) { + + state.camera = camera; + + lightsArray.length = 0; + shadowsArray.length = 0; + + } + + function pushLight( light ) { + + lightsArray.push( light ); + + } + + function pushShadow( shadowLight ) { + + shadowsArray.push( shadowLight ); + + } + + function setupLights( useLegacyLights ) { + + lights.setup( lightsArray, useLegacyLights ); + + } + + function setupLightsView( camera ) { + + lights.setupView( lightsArray, camera ); + + } + + const state = { + lightsArray: lightsArray, + shadowsArray: shadowsArray, + + camera: null, + + lights: lights, + + transmissionRenderTarget: {} + }; + + return { + init: init, + state: state, + setupLights: setupLights, + setupLightsView: setupLightsView, + + pushLight: pushLight, + pushShadow: pushShadow + }; + +} + +function WebGLRenderStates( extensions ) { + + let renderStates = new WeakMap(); + + function get( scene, renderCallDepth = 0 ) { + + const renderStateArray = renderStates.get( scene ); + let renderState; + + if ( renderStateArray === undefined ) { + + renderState = new WebGLRenderState( extensions ); + renderStates.set( scene, [ renderState ] ); + + } else { + + if ( renderCallDepth >= renderStateArray.length ) { + + renderState = new WebGLRenderState( extensions ); + renderStateArray.push( renderState ); + + } else { + + renderState = renderStateArray[ renderCallDepth ]; + + } + + } + + return renderState; + + } + + function dispose() { + + renderStates = new WeakMap(); + + } + + return { + get: get, + dispose: dispose + }; + +} + +class MeshDepthMaterial extends Material { + + constructor( parameters ) { + + super(); + + this.isMeshDepthMaterial = true; + + this.type = 'MeshDepthMaterial'; + + this.depthPacking = BasicDepthPacking; + + this.map = null; + + this.alphaMap = null; + + this.displacementMap = null; + this.displacementScale = 1; + this.displacementBias = 0; + + this.wireframe = false; + this.wireframeLinewidth = 1; + + this.setValues( parameters ); + + } + + copy( source ) { + + super.copy( source ); + + this.depthPacking = source.depthPacking; + + this.map = source.map; + + this.alphaMap = source.alphaMap; + + this.displacementMap = source.displacementMap; + this.displacementScale = source.displacementScale; + this.displacementBias = source.displacementBias; + + this.wireframe = source.wireframe; + this.wireframeLinewidth = source.wireframeLinewidth; + + return this; + + } + +} + +class MeshDistanceMaterial extends Material { + + constructor( parameters ) { + + super(); + + this.isMeshDistanceMaterial = true; + + this.type = 'MeshDistanceMaterial'; + + this.map = null; + + this.alphaMap = null; + + this.displacementMap = null; + this.displacementScale = 1; + this.displacementBias = 0; + + this.setValues( parameters ); + + } + + copy( source ) { + + super.copy( source ); + + this.map = source.map; + + this.alphaMap = source.alphaMap; + + this.displacementMap = source.displacementMap; + this.displacementScale = source.displacementScale; + this.displacementBias = source.displacementBias; + + return this; + + } + +} + +const vertex = "void main() {\n\tgl_Position = vec4( position, 1.0 );\n}"; + +const fragment = "uniform sampler2D shadow_pass;\nuniform vec2 resolution;\nuniform float radius;\n#include \nvoid main() {\n\tconst float samples = float( VSM_SAMPLES );\n\tfloat mean = 0.0;\n\tfloat squared_mean = 0.0;\n\tfloat uvStride = samples <= 1.0 ? 0.0 : 2.0 / ( samples - 1.0 );\n\tfloat uvStart = samples <= 1.0 ? 0.0 : - 1.0;\n\tfor ( float i = 0.0; i < samples; i ++ ) {\n\t\tfloat uvOffset = uvStart + i * uvStride;\n\t\t#ifdef HORIZONTAL_PASS\n\t\t\tvec2 distribution = unpackRGBATo2Half( texture2D( shadow_pass, ( gl_FragCoord.xy + vec2( uvOffset, 0.0 ) * radius ) / resolution ) );\n\t\t\tmean += distribution.x;\n\t\t\tsquared_mean += distribution.y * distribution.y + distribution.x * distribution.x;\n\t\t#else\n\t\t\tfloat depth = unpackRGBAToDepth( texture2D( shadow_pass, ( gl_FragCoord.xy + vec2( 0.0, uvOffset ) * radius ) / resolution ) );\n\t\t\tmean += depth;\n\t\t\tsquared_mean += depth * depth;\n\t\t#endif\n\t}\n\tmean = mean / samples;\n\tsquared_mean = squared_mean / samples;\n\tfloat std_dev = sqrt( squared_mean - mean * mean );\n\tgl_FragColor = pack2HalfToRGBA( vec2( mean, std_dev ) );\n}"; + +function WebGLShadowMap( renderer, objects, capabilities ) { + + let _frustum = new Frustum(); + + const _shadowMapSize = new Vector2(), + _viewportSize = new Vector2(), + + _viewport = new Vector4(), + + _depthMaterial = new MeshDepthMaterial( { depthPacking: RGBADepthPacking } ), + _distanceMaterial = new MeshDistanceMaterial(), + + _materialCache = {}, + + _maxTextureSize = capabilities.maxTextureSize; + + const shadowSide = { [ FrontSide ]: BackSide, [ BackSide ]: FrontSide, [ DoubleSide ]: DoubleSide }; + + const shadowMaterialVertical = new ShaderMaterial( { + defines: { + VSM_SAMPLES: 8 + }, + uniforms: { + shadow_pass: { value: null }, + resolution: { value: new Vector2() }, + radius: { value: 4.0 } + }, + + vertexShader: vertex, + fragmentShader: fragment + + } ); + + const shadowMaterialHorizontal = shadowMaterialVertical.clone(); + shadowMaterialHorizontal.defines.HORIZONTAL_PASS = 1; + + const fullScreenTri = new BufferGeometry(); + fullScreenTri.setAttribute( + 'position', + new BufferAttribute( + new Float32Array( [ - 1, - 1, 0.5, 3, - 1, 0.5, - 1, 3, 0.5 ] ), + 3 + ) + ); + + const fullScreenMesh = new Mesh( fullScreenTri, shadowMaterialVertical ); + + const scope = this; + + this.enabled = false; + + this.autoUpdate = true; + this.needsUpdate = false; + + this.type = PCFShadowMap; + let _previousType = this.type; + + this.render = function ( lights, scene, camera ) { + + if ( scope.enabled === false ) return; + if ( scope.autoUpdate === false && scope.needsUpdate === false ) return; + + if ( lights.length === 0 ) return; + + const currentRenderTarget = renderer.getRenderTarget(); + const activeCubeFace = renderer.getActiveCubeFace(); + const activeMipmapLevel = renderer.getActiveMipmapLevel(); + + const _state = renderer.state; + + // Set GL state for depth map. + _state.setBlending( NoBlending ); + _state.buffers.color.setClear( 1, 1, 1, 1 ); + _state.buffers.depth.setTest( true ); + _state.setScissorTest( false ); + + // check for shadow map type changes + + const toVSM = ( _previousType !== VSMShadowMap && this.type === VSMShadowMap ); + const fromVSM = ( _previousType === VSMShadowMap && this.type !== VSMShadowMap ); + + // render depth map + + for ( let i = 0, il = lights.length; i < il; i ++ ) { + + const light = lights[ i ]; + const shadow = light.shadow; + + if ( shadow === undefined ) { + + console.warn( 'THREE.WebGLShadowMap:', light, 'has no shadow.' ); + continue; + + } + + if ( shadow.autoUpdate === false && shadow.needsUpdate === false ) continue; + + _shadowMapSize.copy( shadow.mapSize ); + + const shadowFrameExtents = shadow.getFrameExtents(); + + _shadowMapSize.multiply( shadowFrameExtents ); + + _viewportSize.copy( shadow.mapSize ); + + if ( _shadowMapSize.x > _maxTextureSize || _shadowMapSize.y > _maxTextureSize ) { + + if ( _shadowMapSize.x > _maxTextureSize ) { + + _viewportSize.x = Math.floor( _maxTextureSize / shadowFrameExtents.x ); + _shadowMapSize.x = _viewportSize.x * shadowFrameExtents.x; + shadow.mapSize.x = _viewportSize.x; + + } + + if ( _shadowMapSize.y > _maxTextureSize ) { + + _viewportSize.y = Math.floor( _maxTextureSize / shadowFrameExtents.y ); + _shadowMapSize.y = _viewportSize.y * shadowFrameExtents.y; + shadow.mapSize.y = _viewportSize.y; + + } + + } + + if ( shadow.map === null || toVSM === true || fromVSM === true ) { + + const pars = ( this.type !== VSMShadowMap ) ? { minFilter: NearestFilter, magFilter: NearestFilter } : {}; + + if ( shadow.map !== null ) { + + shadow.map.dispose(); + + } + + shadow.map = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars ); + shadow.map.texture.name = light.name + '.shadowMap'; + + shadow.camera.updateProjectionMatrix(); + + } + + renderer.setRenderTarget( shadow.map ); + renderer.clear(); + + const viewportCount = shadow.getViewportCount(); + + for ( let vp = 0; vp < viewportCount; vp ++ ) { + + const viewport = shadow.getViewport( vp ); + + _viewport.set( + _viewportSize.x * viewport.x, + _viewportSize.y * viewport.y, + _viewportSize.x * viewport.z, + _viewportSize.y * viewport.w + ); + + _state.viewport( _viewport ); + + shadow.updateMatrices( light, vp ); + + _frustum = shadow.getFrustum(); + + renderObject( scene, camera, shadow.camera, light, this.type ); + + } + + // do blur pass for VSM + + if ( shadow.isPointLightShadow !== true && this.type === VSMShadowMap ) { + + VSMPass( shadow, camera ); + + } + + shadow.needsUpdate = false; + + } + + _previousType = this.type; + + scope.needsUpdate = false; + + renderer.setRenderTarget( currentRenderTarget, activeCubeFace, activeMipmapLevel ); + + }; + + function VSMPass( shadow, camera ) { + + const geometry = objects.update( fullScreenMesh ); + + if ( shadowMaterialVertical.defines.VSM_SAMPLES !== shadow.blurSamples ) { + + shadowMaterialVertical.defines.VSM_SAMPLES = shadow.blurSamples; + shadowMaterialHorizontal.defines.VSM_SAMPLES = shadow.blurSamples; + + shadowMaterialVertical.needsUpdate = true; + shadowMaterialHorizontal.needsUpdate = true; + + } + + if ( shadow.mapPass === null ) { + + shadow.mapPass = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y ); + + } + + // vertical pass + + shadowMaterialVertical.uniforms.shadow_pass.value = shadow.map.texture; + shadowMaterialVertical.uniforms.resolution.value = shadow.mapSize; + shadowMaterialVertical.uniforms.radius.value = shadow.radius; + renderer.setRenderTarget( shadow.mapPass ); + renderer.clear(); + renderer.renderBufferDirect( camera, null, geometry, shadowMaterialVertical, fullScreenMesh, null ); + + // horizontal pass + + shadowMaterialHorizontal.uniforms.shadow_pass.value = shadow.mapPass.texture; + shadowMaterialHorizontal.uniforms.resolution.value = shadow.mapSize; + shadowMaterialHorizontal.uniforms.radius.value = shadow.radius; + renderer.setRenderTarget( shadow.map ); + renderer.clear(); + renderer.renderBufferDirect( camera, null, geometry, shadowMaterialHorizontal, fullScreenMesh, null ); + + } + + function getDepthMaterial( object, material, light, type ) { + + let result = null; + + const customMaterial = ( light.isPointLight === true ) ? object.customDistanceMaterial : object.customDepthMaterial; + + if ( customMaterial !== undefined ) { + + result = customMaterial; + + } else { + + result = ( light.isPointLight === true ) ? _distanceMaterial : _depthMaterial; + + if ( ( renderer.localClippingEnabled && material.clipShadows === true && Array.isArray( material.clippingPlanes ) && material.clippingPlanes.length !== 0 ) || + ( material.displacementMap && material.displacementScale !== 0 ) || + ( material.alphaMap && material.alphaTest > 0 ) || + ( material.map && material.alphaTest > 0 ) ) { + + // in this case we need a unique material instance reflecting the + // appropriate state + + const keyA = result.uuid, keyB = material.uuid; + + let materialsForVariant = _materialCache[ keyA ]; + + if ( materialsForVariant === undefined ) { + + materialsForVariant = {}; + _materialCache[ keyA ] = materialsForVariant; + + } + + let cachedMaterial = materialsForVariant[ keyB ]; + + if ( cachedMaterial === undefined ) { + + cachedMaterial = result.clone(); + materialsForVariant[ keyB ] = cachedMaterial; + material.addEventListener( 'dispose', onMaterialDispose ); + + } + + result = cachedMaterial; + + } + + } + + result.visible = material.visible; + result.wireframe = material.wireframe; + + if ( type === VSMShadowMap ) { + + result.side = ( material.shadowSide !== null ) ? material.shadowSide : material.side; + + } else { + + result.side = ( material.shadowSide !== null ) ? material.shadowSide : shadowSide[ material.side ]; + + } + + result.alphaMap = material.alphaMap; + result.alphaTest = material.alphaTest; + result.map = material.map; + + result.clipShadows = material.clipShadows; + result.clippingPlanes = material.clippingPlanes; + result.clipIntersection = material.clipIntersection; + + result.displacementMap = material.displacementMap; + result.displacementScale = material.displacementScale; + result.displacementBias = material.displacementBias; + + result.wireframeLinewidth = material.wireframeLinewidth; + result.linewidth = material.linewidth; + + if ( light.isPointLight === true && result.isMeshDistanceMaterial === true ) { + + const materialProperties = renderer.properties.get( result ); + materialProperties.light = light; + + } + + return result; + + } + + function renderObject( object, camera, shadowCamera, light, type ) { + + if ( object.visible === false ) return; + + const visible = object.layers.test( camera.layers ); + + if ( visible && ( object.isMesh || object.isLine || object.isPoints ) ) { + + if ( ( object.castShadow || ( object.receiveShadow && type === VSMShadowMap ) ) && ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) ) { + + object.modelViewMatrix.multiplyMatrices( shadowCamera.matrixWorldInverse, object.matrixWorld ); + + const geometry = objects.update( object ); + const material = object.material; + + if ( Array.isArray( material ) ) { + + const groups = geometry.groups; + + for ( let k = 0, kl = groups.length; k < kl; k ++ ) { + + const group = groups[ k ]; + const groupMaterial = material[ group.materialIndex ]; + + if ( groupMaterial && groupMaterial.visible ) { + + const depthMaterial = getDepthMaterial( object, groupMaterial, light, type ); + + object.onBeforeShadow( renderer, object, camera, shadowCamera, geometry, depthMaterial, group ); + + renderer.renderBufferDirect( shadowCamera, null, geometry, depthMaterial, object, group ); + + object.onAfterShadow( renderer, object, camera, shadowCamera, geometry, depthMaterial, group ); + + } + + } + + } else if ( material.visible ) { + + const depthMaterial = getDepthMaterial( object, material, light, type ); + + object.onBeforeShadow( renderer, object, camera, shadowCamera, geometry, depthMaterial, null ); + + renderer.renderBufferDirect( shadowCamera, null, geometry, depthMaterial, object, null ); + + object.onAfterShadow( renderer, object, camera, shadowCamera, geometry, depthMaterial, null ); + + } + + } + + } + + const children = object.children; + + for ( let i = 0, l = children.length; i < l; i ++ ) { + + renderObject( children[ i ], camera, shadowCamera, light, type ); + + } + + } + + function onMaterialDispose( event ) { + + const material = event.target; + + material.removeEventListener( 'dispose', onMaterialDispose ); + + // make sure to remove the unique distance/depth materials used for shadow map rendering + + for ( const id in _materialCache ) { + + const cache = _materialCache[ id ]; + + const uuid = event.target.uuid; + + if ( uuid in cache ) { + + const shadowMaterial = cache[ uuid ]; + shadowMaterial.dispose(); + delete cache[ uuid ]; + + } + + } + + } + +} + +function WebGLState( gl ) { + + function ColorBuffer() { + + let locked = false; + + const color = new Vector4(); + let currentColorMask = null; + const currentColorClear = new Vector4( 0, 0, 0, 0 ); + + return { + + setMask: function ( colorMask ) { + + if ( currentColorMask !== colorMask && ! locked ) { + + gl.colorMask( colorMask, colorMask, colorMask, colorMask ); + currentColorMask = colorMask; + + } + + }, + + setLocked: function ( lock ) { + + locked = lock; + + }, + + setClear: function ( r, g, b, a, premultipliedAlpha ) { + + if ( premultipliedAlpha === true ) { + + r *= a; g *= a; b *= a; + + } + + color.set( r, g, b, a ); + + if ( currentColorClear.equals( color ) === false ) { + + gl.clearColor( r, g, b, a ); + currentColorClear.copy( color ); + + } + + }, + + reset: function () { + + locked = false; + + currentColorMask = null; + currentColorClear.set( - 1, 0, 0, 0 ); // set to invalid state + + } + + }; + + } + + function DepthBuffer() { + + let locked = false; + + let currentDepthMask = null; + let currentDepthFunc = null; + let currentDepthClear = null; + + return { + + setTest: function ( depthTest ) { + + if ( depthTest ) { + + enable( gl.DEPTH_TEST ); + + } else { + + disable( gl.DEPTH_TEST ); + + } + + }, + + setMask: function ( depthMask ) { + + if ( currentDepthMask !== depthMask && ! locked ) { + + gl.depthMask( depthMask ); + currentDepthMask = depthMask; + + } + + }, + + setFunc: function ( depthFunc ) { + + if ( currentDepthFunc !== depthFunc ) { + + switch ( depthFunc ) { + + case NeverDepth: + + gl.depthFunc( gl.NEVER ); + break; + + case AlwaysDepth: + + gl.depthFunc( gl.ALWAYS ); + break; + + case LessDepth: + + gl.depthFunc( gl.LESS ); + break; + + case LessEqualDepth: + + gl.depthFunc( gl.LEQUAL ); + break; + + case EqualDepth: + + gl.depthFunc( gl.EQUAL ); + break; + + case GreaterEqualDepth: + + gl.depthFunc( gl.GEQUAL ); + break; + + case GreaterDepth: + + gl.depthFunc( gl.GREATER ); + break; + + case NotEqualDepth: + + gl.depthFunc( gl.NOTEQUAL ); + break; + + default: + + gl.depthFunc( gl.LEQUAL ); + + } + + currentDepthFunc = depthFunc; + + } + + }, + + setLocked: function ( lock ) { + + locked = lock; + + }, + + setClear: function ( depth ) { + + if ( currentDepthClear !== depth ) { + + gl.clearDepth( depth ); + currentDepthClear = depth; + + } + + }, + + reset: function () { + + locked = false; + + currentDepthMask = null; + currentDepthFunc = null; + currentDepthClear = null; + + } + + }; + + } + + function StencilBuffer() { + + let locked = false; + + let currentStencilMask = null; + let currentStencilFunc = null; + let currentStencilRef = null; + let currentStencilFuncMask = null; + let currentStencilFail = null; + let currentStencilZFail = null; + let currentStencilZPass = null; + let currentStencilClear = null; + + return { + + setTest: function ( stencilTest ) { + + if ( ! locked ) { + + if ( stencilTest ) { + + enable( gl.STENCIL_TEST ); + + } else { + + disable( gl.STENCIL_TEST ); + + } + + } + + }, + + setMask: function ( stencilMask ) { + + if ( currentStencilMask !== stencilMask && ! locked ) { + + gl.stencilMask( stencilMask ); + currentStencilMask = stencilMask; + + } + + }, + + setFunc: function ( stencilFunc, stencilRef, stencilMask ) { + + if ( currentStencilFunc !== stencilFunc || + currentStencilRef !== stencilRef || + currentStencilFuncMask !== stencilMask ) { + + gl.stencilFunc( stencilFunc, stencilRef, stencilMask ); + + currentStencilFunc = stencilFunc; + currentStencilRef = stencilRef; + currentStencilFuncMask = stencilMask; + + } + + }, + + setOp: function ( stencilFail, stencilZFail, stencilZPass ) { + + if ( currentStencilFail !== stencilFail || + currentStencilZFail !== stencilZFail || + currentStencilZPass !== stencilZPass ) { + + gl.stencilOp( stencilFail, stencilZFail, stencilZPass ); + + currentStencilFail = stencilFail; + currentStencilZFail = stencilZFail; + currentStencilZPass = stencilZPass; + + } + + }, + + setLocked: function ( lock ) { + + locked = lock; + + }, + + setClear: function ( stencil ) { + + if ( currentStencilClear !== stencil ) { + + gl.clearStencil( stencil ); + currentStencilClear = stencil; + + } + + }, + + reset: function () { + + locked = false; + + currentStencilMask = null; + currentStencilFunc = null; + currentStencilRef = null; + currentStencilFuncMask = null; + currentStencilFail = null; + currentStencilZFail = null; + currentStencilZPass = null; + currentStencilClear = null; + + } + + }; + + } + + // + + const colorBuffer = new ColorBuffer(); + const depthBuffer = new DepthBuffer(); + const stencilBuffer = new StencilBuffer(); + + const uboBindings = new WeakMap(); + const uboProgramMap = new WeakMap(); + + let enabledCapabilities = {}; + + let currentBoundFramebuffers = {}; + let currentDrawbuffers = new WeakMap(); + let defaultDrawbuffers = []; + + let currentProgram = null; + + let currentBlendingEnabled = false; + let currentBlending = null; + let currentBlendEquation = null; + let currentBlendSrc = null; + let currentBlendDst = null; + let currentBlendEquationAlpha = null; + let currentBlendSrcAlpha = null; + let currentBlendDstAlpha = null; + let currentBlendColor = new Color( 0, 0, 0 ); + let currentBlendAlpha = 0; + let currentPremultipledAlpha = false; + + let currentFlipSided = null; + let currentCullFace = null; + + let currentLineWidth = null; + + let currentPolygonOffsetFactor = null; + let currentPolygonOffsetUnits = null; + + const maxTextures = gl.getParameter( gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS ); + + let lineWidthAvailable = false; + let version = 0; + const glVersion = gl.getParameter( gl.VERSION ); + + if ( glVersion.indexOf( 'WebGL' ) !== - 1 ) { + + version = parseFloat( /^WebGL (\d)/.exec( glVersion )[ 1 ] ); + lineWidthAvailable = ( version >= 1.0 ); + + } else if ( glVersion.indexOf( 'OpenGL ES' ) !== - 1 ) { + + version = parseFloat( /^OpenGL ES (\d)/.exec( glVersion )[ 1 ] ); + lineWidthAvailable = ( version >= 2.0 ); + + } + + let currentTextureSlot = null; + let currentBoundTextures = {}; + + const scissorParam = gl.getParameter( gl.SCISSOR_BOX ); + const viewportParam = gl.getParameter( gl.VIEWPORT ); + + const currentScissor = new Vector4().fromArray( scissorParam ); + const currentViewport = new Vector4().fromArray( viewportParam ); + + function createTexture( type, target, count, dimensions ) { + + const data = new Uint8Array( 4 ); // 4 is required to match default unpack alignment of 4. + const texture = gl.createTexture(); + + gl.bindTexture( type, texture ); + gl.texParameteri( type, gl.TEXTURE_MIN_FILTER, gl.NEAREST ); + gl.texParameteri( type, gl.TEXTURE_MAG_FILTER, gl.NEAREST ); + + for ( let i = 0; i < count; i ++ ) { + + if ( type === gl.TEXTURE_3D || type === gl.TEXTURE_2D_ARRAY ) { + + gl.texImage3D( target, 0, gl.RGBA, 1, 1, dimensions, 0, gl.RGBA, gl.UNSIGNED_BYTE, data ); + + } else { + + gl.texImage2D( target + i, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, data ); + + } + + } + + return texture; + + } + + const emptyTextures = {}; + emptyTextures[ gl.TEXTURE_2D ] = createTexture( gl.TEXTURE_2D, gl.TEXTURE_2D, 1 ); + emptyTextures[ gl.TEXTURE_CUBE_MAP ] = createTexture( gl.TEXTURE_CUBE_MAP, gl.TEXTURE_CUBE_MAP_POSITIVE_X, 6 ); + emptyTextures[ gl.TEXTURE_2D_ARRAY ] = createTexture( gl.TEXTURE_2D_ARRAY, gl.TEXTURE_2D_ARRAY, 1, 1 ); + emptyTextures[ gl.TEXTURE_3D ] = createTexture( gl.TEXTURE_3D, gl.TEXTURE_3D, 1, 1 ); + + // init + + colorBuffer.setClear( 0, 0, 0, 1 ); + depthBuffer.setClear( 1 ); + stencilBuffer.setClear( 0 ); + + enable( gl.DEPTH_TEST ); + depthBuffer.setFunc( LessEqualDepth ); + + setFlipSided( false ); + setCullFace( CullFaceBack ); + enable( gl.CULL_FACE ); + + setBlending( NoBlending ); + + // + + function enable( id ) { + + if ( enabledCapabilities[ id ] !== true ) { + + gl.enable( id ); + enabledCapabilities[ id ] = true; + + } + + } + + function disable( id ) { + + if ( enabledCapabilities[ id ] !== false ) { + + gl.disable( id ); + enabledCapabilities[ id ] = false; + + } + + } + + function bindFramebuffer( target, framebuffer ) { + + if ( currentBoundFramebuffers[ target ] !== framebuffer ) { + + gl.bindFramebuffer( target, framebuffer ); + + currentBoundFramebuffers[ target ] = framebuffer; + + // gl.DRAW_FRAMEBUFFER is equivalent to gl.FRAMEBUFFER + + if ( target === gl.DRAW_FRAMEBUFFER ) { + + currentBoundFramebuffers[ gl.FRAMEBUFFER ] = framebuffer; + + } + + if ( target === gl.FRAMEBUFFER ) { + + currentBoundFramebuffers[ gl.DRAW_FRAMEBUFFER ] = framebuffer; + + } + + return true; + + } + + return false; + + } + + function drawBuffers( renderTarget, framebuffer ) { + + let drawBuffers = defaultDrawbuffers; + + let needsUpdate = false; + + if ( renderTarget ) { + + drawBuffers = currentDrawbuffers.get( framebuffer ); + + if ( drawBuffers === undefined ) { + + drawBuffers = []; + currentDrawbuffers.set( framebuffer, drawBuffers ); + + } + + const textures = renderTarget.textures; + + if ( drawBuffers.length !== textures.length || drawBuffers[ 0 ] !== gl.COLOR_ATTACHMENT0 ) { + + for ( let i = 0, il = textures.length; i < il; i ++ ) { + + drawBuffers[ i ] = gl.COLOR_ATTACHMENT0 + i; + + } + + drawBuffers.length = textures.length; + + needsUpdate = true; + + } + + } else { + + if ( drawBuffers[ 0 ] !== gl.BACK ) { + + drawBuffers[ 0 ] = gl.BACK; + + needsUpdate = true; + + } + + } + + if ( needsUpdate ) { + + gl.drawBuffers( drawBuffers ); + + } + + } + + function useProgram( program ) { + + if ( currentProgram !== program ) { + + gl.useProgram( program ); + + currentProgram = program; + + return true; + + } + + return false; + + } + + const equationToGL = { + [ AddEquation ]: gl.FUNC_ADD, + [ SubtractEquation ]: gl.FUNC_SUBTRACT, + [ ReverseSubtractEquation ]: gl.FUNC_REVERSE_SUBTRACT + }; + + equationToGL[ MinEquation ] = gl.MIN; + equationToGL[ MaxEquation ] = gl.MAX; + + const factorToGL = { + [ ZeroFactor ]: gl.ZERO, + [ OneFactor ]: gl.ONE, + [ SrcColorFactor ]: gl.SRC_COLOR, + [ SrcAlphaFactor ]: gl.SRC_ALPHA, + [ SrcAlphaSaturateFactor ]: gl.SRC_ALPHA_SATURATE, + [ DstColorFactor ]: gl.DST_COLOR, + [ DstAlphaFactor ]: gl.DST_ALPHA, + [ OneMinusSrcColorFactor ]: gl.ONE_MINUS_SRC_COLOR, + [ OneMinusSrcAlphaFactor ]: gl.ONE_MINUS_SRC_ALPHA, + [ OneMinusDstColorFactor ]: gl.ONE_MINUS_DST_COLOR, + [ OneMinusDstAlphaFactor ]: gl.ONE_MINUS_DST_ALPHA, + [ ConstantColorFactor ]: gl.CONSTANT_COLOR, + [ OneMinusConstantColorFactor ]: gl.ONE_MINUS_CONSTANT_COLOR, + [ ConstantAlphaFactor ]: gl.CONSTANT_ALPHA, + [ OneMinusConstantAlphaFactor ]: gl.ONE_MINUS_CONSTANT_ALPHA + }; + + function setBlending( blending, blendEquation, blendSrc, blendDst, blendEquationAlpha, blendSrcAlpha, blendDstAlpha, blendColor, blendAlpha, premultipliedAlpha ) { + + if ( blending === NoBlending ) { + + if ( currentBlendingEnabled === true ) { + + disable( gl.BLEND ); + currentBlendingEnabled = false; + + } + + return; + + } + + if ( currentBlendingEnabled === false ) { + + enable( gl.BLEND ); + currentBlendingEnabled = true; + + } + + if ( blending !== CustomBlending ) { + + if ( blending !== currentBlending || premultipliedAlpha !== currentPremultipledAlpha ) { + + if ( currentBlendEquation !== AddEquation || currentBlendEquationAlpha !== AddEquation ) { + + gl.blendEquation( gl.FUNC_ADD ); + + currentBlendEquation = AddEquation; + currentBlendEquationAlpha = AddEquation; + + } + + if ( premultipliedAlpha ) { + + switch ( blending ) { + + case NormalBlending: + gl.blendFuncSeparate( gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA ); + break; + + case AdditiveBlending: + gl.blendFunc( gl.ONE, gl.ONE ); + break; + + case SubtractiveBlending: + gl.blendFuncSeparate( gl.ZERO, gl.ONE_MINUS_SRC_COLOR, gl.ZERO, gl.ONE ); + break; + + case MultiplyBlending: + gl.blendFuncSeparate( gl.ZERO, gl.SRC_COLOR, gl.ZERO, gl.SRC_ALPHA ); + break; + + default: + console.error( 'THREE.WebGLState: Invalid blending: ', blending ); + break; + + } + + } else { + + switch ( blending ) { + + case NormalBlending: + gl.blendFuncSeparate( gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA ); + break; + + case AdditiveBlending: + gl.blendFunc( gl.SRC_ALPHA, gl.ONE ); + break; + + case SubtractiveBlending: + gl.blendFuncSeparate( gl.ZERO, gl.ONE_MINUS_SRC_COLOR, gl.ZERO, gl.ONE ); + break; + + case MultiplyBlending: + gl.blendFunc( gl.ZERO, gl.SRC_COLOR ); + break; + + default: + console.error( 'THREE.WebGLState: Invalid blending: ', blending ); + break; + + } + + } + + currentBlendSrc = null; + currentBlendDst = null; + currentBlendSrcAlpha = null; + currentBlendDstAlpha = null; + currentBlendColor.set( 0, 0, 0 ); + currentBlendAlpha = 0; + + currentBlending = blending; + currentPremultipledAlpha = premultipliedAlpha; + + } + + return; + + } + + // custom blending + + blendEquationAlpha = blendEquationAlpha || blendEquation; + blendSrcAlpha = blendSrcAlpha || blendSrc; + blendDstAlpha = blendDstAlpha || blendDst; + + if ( blendEquation !== currentBlendEquation || blendEquationAlpha !== currentBlendEquationAlpha ) { + + gl.blendEquationSeparate( equationToGL[ blendEquation ], equationToGL[ blendEquationAlpha ] ); + + currentBlendEquation = blendEquation; + currentBlendEquationAlpha = blendEquationAlpha; + + } + + if ( blendSrc !== currentBlendSrc || blendDst !== currentBlendDst || blendSrcAlpha !== currentBlendSrcAlpha || blendDstAlpha !== currentBlendDstAlpha ) { + + gl.blendFuncSeparate( factorToGL[ blendSrc ], factorToGL[ blendDst ], factorToGL[ blendSrcAlpha ], factorToGL[ blendDstAlpha ] ); + + currentBlendSrc = blendSrc; + currentBlendDst = blendDst; + currentBlendSrcAlpha = blendSrcAlpha; + currentBlendDstAlpha = blendDstAlpha; + + } + + if ( blendColor.equals( currentBlendColor ) === false || blendAlpha !== currentBlendAlpha ) { + + gl.blendColor( blendColor.r, blendColor.g, blendColor.b, blendAlpha ); + + currentBlendColor.copy( blendColor ); + currentBlendAlpha = blendAlpha; + + } + + currentBlending = blending; + currentPremultipledAlpha = false; + + } + + function setMaterial( material, frontFaceCW ) { + + material.side === DoubleSide + ? disable( gl.CULL_FACE ) + : enable( gl.CULL_FACE ); + + let flipSided = ( material.side === BackSide ); + if ( frontFaceCW ) flipSided = ! flipSided; + + setFlipSided( flipSided ); + + ( material.blending === NormalBlending && material.transparent === false ) + ? setBlending( NoBlending ) + : setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst, material.blendEquationAlpha, material.blendSrcAlpha, material.blendDstAlpha, material.blendColor, material.blendAlpha, material.premultipliedAlpha ); + + depthBuffer.setFunc( material.depthFunc ); + depthBuffer.setTest( material.depthTest ); + depthBuffer.setMask( material.depthWrite ); + colorBuffer.setMask( material.colorWrite ); + + const stencilWrite = material.stencilWrite; + stencilBuffer.setTest( stencilWrite ); + if ( stencilWrite ) { + + stencilBuffer.setMask( material.stencilWriteMask ); + stencilBuffer.setFunc( material.stencilFunc, material.stencilRef, material.stencilFuncMask ); + stencilBuffer.setOp( material.stencilFail, material.stencilZFail, material.stencilZPass ); + + } + + setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits ); + + material.alphaToCoverage === true + ? enable( gl.SAMPLE_ALPHA_TO_COVERAGE ) + : disable( gl.SAMPLE_ALPHA_TO_COVERAGE ); + + } + + // + + function setFlipSided( flipSided ) { + + if ( currentFlipSided !== flipSided ) { + + if ( flipSided ) { + + gl.frontFace( gl.CW ); + + } else { + + gl.frontFace( gl.CCW ); + + } + + currentFlipSided = flipSided; + + } + + } + + function setCullFace( cullFace ) { + + if ( cullFace !== CullFaceNone ) { + + enable( gl.CULL_FACE ); + + if ( cullFace !== currentCullFace ) { + + if ( cullFace === CullFaceBack ) { + + gl.cullFace( gl.BACK ); + + } else if ( cullFace === CullFaceFront ) { + + gl.cullFace( gl.FRONT ); + + } else { + + gl.cullFace( gl.FRONT_AND_BACK ); + + } + + } + + } else { + + disable( gl.CULL_FACE ); + + } + + currentCullFace = cullFace; + + } + + function setLineWidth( width ) { + + if ( width !== currentLineWidth ) { + + if ( lineWidthAvailable ) gl.lineWidth( width ); + + currentLineWidth = width; + + } + + } + + function setPolygonOffset( polygonOffset, factor, units ) { + + if ( polygonOffset ) { + + enable( gl.POLYGON_OFFSET_FILL ); + + if ( currentPolygonOffsetFactor !== factor || currentPolygonOffsetUnits !== units ) { + + gl.polygonOffset( factor, units ); + + currentPolygonOffsetFactor = factor; + currentPolygonOffsetUnits = units; + + } + + } else { + + disable( gl.POLYGON_OFFSET_FILL ); + + } + + } + + function setScissorTest( scissorTest ) { + + if ( scissorTest ) { + + enable( gl.SCISSOR_TEST ); + + } else { + + disable( gl.SCISSOR_TEST ); + + } + + } + + // texture + + function activeTexture( webglSlot ) { + + if ( webglSlot === undefined ) webglSlot = gl.TEXTURE0 + maxTextures - 1; + + if ( currentTextureSlot !== webglSlot ) { + + gl.activeTexture( webglSlot ); + currentTextureSlot = webglSlot; + + } + + } + + function bindTexture( webglType, webglTexture, webglSlot ) { + + if ( webglSlot === undefined ) { + + if ( currentTextureSlot === null ) { + + webglSlot = gl.TEXTURE0 + maxTextures - 1; + + } else { + + webglSlot = currentTextureSlot; + + } + + } + + let boundTexture = currentBoundTextures[ webglSlot ]; + + if ( boundTexture === undefined ) { + + boundTexture = { type: undefined, texture: undefined }; + currentBoundTextures[ webglSlot ] = boundTexture; + + } + + if ( boundTexture.type !== webglType || boundTexture.texture !== webglTexture ) { + + if ( currentTextureSlot !== webglSlot ) { + + gl.activeTexture( webglSlot ); + currentTextureSlot = webglSlot; + + } + + gl.bindTexture( webglType, webglTexture || emptyTextures[ webglType ] ); + + boundTexture.type = webglType; + boundTexture.texture = webglTexture; + + } + + } + + function unbindTexture() { + + const boundTexture = currentBoundTextures[ currentTextureSlot ]; + + if ( boundTexture !== undefined && boundTexture.type !== undefined ) { + + gl.bindTexture( boundTexture.type, null ); + + boundTexture.type = undefined; + boundTexture.texture = undefined; + + } + + } + + function compressedTexImage2D() { + + try { + + gl.compressedTexImage2D.apply( gl, arguments ); + + } catch ( error ) { + + console.error( 'THREE.WebGLState:', error ); + + } + + } + + function compressedTexImage3D() { + + try { + + gl.compressedTexImage3D.apply( gl, arguments ); + + } catch ( error ) { + + console.error( 'THREE.WebGLState:', error ); + + } + + } + + function texSubImage2D() { + + try { + + gl.texSubImage2D.apply( gl, arguments ); + + } catch ( error ) { + + console.error( 'THREE.WebGLState:', error ); + + } + + } + + function texSubImage3D() { + + try { + + gl.texSubImage3D.apply( gl, arguments ); + + } catch ( error ) { + + console.error( 'THREE.WebGLState:', error ); + + } + + } + + function compressedTexSubImage2D() { + + try { + + gl.compressedTexSubImage2D.apply( gl, arguments ); + + } catch ( error ) { + + console.error( 'THREE.WebGLState:', error ); + + } + + } + + function compressedTexSubImage3D() { + + try { + + gl.compressedTexSubImage3D.apply( gl, arguments ); + + } catch ( error ) { + + console.error( 'THREE.WebGLState:', error ); + + } + + } + + function texStorage2D() { + + try { + + gl.texStorage2D.apply( gl, arguments ); + + } catch ( error ) { + + console.error( 'THREE.WebGLState:', error ); + + } + + } + + function texStorage3D() { + + try { + + gl.texStorage3D.apply( gl, arguments ); + + } catch ( error ) { + + console.error( 'THREE.WebGLState:', error ); + + } + + } + + function texImage2D() { + + try { + + gl.texImage2D.apply( gl, arguments ); + + } catch ( error ) { + + console.error( 'THREE.WebGLState:', error ); + + } + + } + + function texImage3D() { + + try { + + gl.texImage3D.apply( gl, arguments ); + + } catch ( error ) { + + console.error( 'THREE.WebGLState:', error ); + + } + + } + + // + + function scissor( scissor ) { + + if ( currentScissor.equals( scissor ) === false ) { + + gl.scissor( scissor.x, scissor.y, scissor.z, scissor.w ); + currentScissor.copy( scissor ); + + } + + } + + function viewport( viewport ) { + + if ( currentViewport.equals( viewport ) === false ) { + + gl.viewport( viewport.x, viewport.y, viewport.z, viewport.w ); + currentViewport.copy( viewport ); + + } + + } + + function updateUBOMapping( uniformsGroup, program ) { + + let mapping = uboProgramMap.get( program ); + + if ( mapping === undefined ) { + + mapping = new WeakMap(); + + uboProgramMap.set( program, mapping ); + + } + + let blockIndex = mapping.get( uniformsGroup ); + + if ( blockIndex === undefined ) { + + blockIndex = gl.getUniformBlockIndex( program, uniformsGroup.name ); + + mapping.set( uniformsGroup, blockIndex ); + + } + + } + + function uniformBlockBinding( uniformsGroup, program ) { + + const mapping = uboProgramMap.get( program ); + const blockIndex = mapping.get( uniformsGroup ); + + if ( uboBindings.get( program ) !== blockIndex ) { + + // bind shader specific block index to global block point + gl.uniformBlockBinding( program, blockIndex, uniformsGroup.__bindingPointIndex ); + + uboBindings.set( program, blockIndex ); + + } + + } + + // + + function reset() { + + // reset state + + gl.disable( gl.BLEND ); + gl.disable( gl.CULL_FACE ); + gl.disable( gl.DEPTH_TEST ); + gl.disable( gl.POLYGON_OFFSET_FILL ); + gl.disable( gl.SCISSOR_TEST ); + gl.disable( gl.STENCIL_TEST ); + gl.disable( gl.SAMPLE_ALPHA_TO_COVERAGE ); + + gl.blendEquation( gl.FUNC_ADD ); + gl.blendFunc( gl.ONE, gl.ZERO ); + gl.blendFuncSeparate( gl.ONE, gl.ZERO, gl.ONE, gl.ZERO ); + gl.blendColor( 0, 0, 0, 0 ); + + gl.colorMask( true, true, true, true ); + gl.clearColor( 0, 0, 0, 0 ); + + gl.depthMask( true ); + gl.depthFunc( gl.LESS ); + gl.clearDepth( 1 ); + + gl.stencilMask( 0xffffffff ); + gl.stencilFunc( gl.ALWAYS, 0, 0xffffffff ); + gl.stencilOp( gl.KEEP, gl.KEEP, gl.KEEP ); + gl.clearStencil( 0 ); + + gl.cullFace( gl.BACK ); + gl.frontFace( gl.CCW ); + + gl.polygonOffset( 0, 0 ); + + gl.activeTexture( gl.TEXTURE0 ); + + gl.bindFramebuffer( gl.FRAMEBUFFER, null ); + gl.bindFramebuffer( gl.DRAW_FRAMEBUFFER, null ); + gl.bindFramebuffer( gl.READ_FRAMEBUFFER, null ); + + gl.useProgram( null ); + + gl.lineWidth( 1 ); + + gl.scissor( 0, 0, gl.canvas.width, gl.canvas.height ); + gl.viewport( 0, 0, gl.canvas.width, gl.canvas.height ); + + // reset internals + + enabledCapabilities = {}; + + currentTextureSlot = null; + currentBoundTextures = {}; + + currentBoundFramebuffers = {}; + currentDrawbuffers = new WeakMap(); + defaultDrawbuffers = []; + + currentProgram = null; + + currentBlendingEnabled = false; + currentBlending = null; + currentBlendEquation = null; + currentBlendSrc = null; + currentBlendDst = null; + currentBlendEquationAlpha = null; + currentBlendSrcAlpha = null; + currentBlendDstAlpha = null; + currentBlendColor = new Color( 0, 0, 0 ); + currentBlendAlpha = 0; + currentPremultipledAlpha = false; + + currentFlipSided = null; + currentCullFace = null; + + currentLineWidth = null; + + currentPolygonOffsetFactor = null; + currentPolygonOffsetUnits = null; + + currentScissor.set( 0, 0, gl.canvas.width, gl.canvas.height ); + currentViewport.set( 0, 0, gl.canvas.width, gl.canvas.height ); + + colorBuffer.reset(); + depthBuffer.reset(); + stencilBuffer.reset(); + + } + + return { + + buffers: { + color: colorBuffer, + depth: depthBuffer, + stencil: stencilBuffer + }, + + enable: enable, + disable: disable, + + bindFramebuffer: bindFramebuffer, + drawBuffers: drawBuffers, + + useProgram: useProgram, + + setBlending: setBlending, + setMaterial: setMaterial, + + setFlipSided: setFlipSided, + setCullFace: setCullFace, + + setLineWidth: setLineWidth, + setPolygonOffset: setPolygonOffset, + + setScissorTest: setScissorTest, + + activeTexture: activeTexture, + bindTexture: bindTexture, + unbindTexture: unbindTexture, + compressedTexImage2D: compressedTexImage2D, + compressedTexImage3D: compressedTexImage3D, + texImage2D: texImage2D, + texImage3D: texImage3D, + + updateUBOMapping: updateUBOMapping, + uniformBlockBinding: uniformBlockBinding, + + texStorage2D: texStorage2D, + texStorage3D: texStorage3D, + texSubImage2D: texSubImage2D, + texSubImage3D: texSubImage3D, + compressedTexSubImage2D: compressedTexSubImage2D, + compressedTexSubImage3D: compressedTexSubImage3D, + + scissor: scissor, + viewport: viewport, + + reset: reset + + }; + +} + +function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, info ) { + + const multisampledRTTExt = extensions.has( 'WEBGL_multisampled_render_to_texture' ) ? extensions.get( 'WEBGL_multisampled_render_to_texture' ) : null; + const supportsInvalidateFramebuffer = typeof navigator === 'undefined' ? false : /OculusBrowser/g.test( navigator.userAgent ); + + const _imageDimensions = new Vector2(); + const _videoTextures = new WeakMap(); + let _canvas; + + const _sources = new WeakMap(); // maps WebglTexture objects to instances of Source + + // cordova iOS (as of 5.0) still uses UIWebView, which provides OffscreenCanvas, + // also OffscreenCanvas.getContext("webgl"), but not OffscreenCanvas.getContext("2d")! + // Some implementations may only implement OffscreenCanvas partially (e.g. lacking 2d). + + let useOffscreenCanvas = false; + + try { + + useOffscreenCanvas = typeof OffscreenCanvas !== 'undefined' + // eslint-disable-next-line compat/compat + && ( new OffscreenCanvas( 1, 1 ).getContext( '2d' ) ) !== null; + + } catch ( err ) { + + // Ignore any errors + + } + + function createCanvas( width, height ) { + + // Use OffscreenCanvas when available. Specially needed in web workers + + return useOffscreenCanvas ? + // eslint-disable-next-line compat/compat + new OffscreenCanvas( width, height ) : createElementNS( 'canvas' ); + + } + + function resizeImage( image, needsNewCanvas, maxSize ) { + + let scale = 1; + + const dimensions = getDimensions( image ); + + // handle case if texture exceeds max size + + if ( dimensions.width > maxSize || dimensions.height > maxSize ) { + + scale = maxSize / Math.max( dimensions.width, dimensions.height ); + + } + + // only perform resize if necessary + + if ( scale < 1 ) { + + // only perform resize for certain image types + + if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) || + ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) || + ( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap ) || + ( typeof VideoFrame !== 'undefined' && image instanceof VideoFrame ) ) { + + const width = Math.floor( scale * dimensions.width ); + const height = Math.floor( scale * dimensions.height ); + + if ( _canvas === undefined ) _canvas = createCanvas( width, height ); + + // cube textures can't reuse the same canvas + + const canvas = needsNewCanvas ? createCanvas( width, height ) : _canvas; + + canvas.width = width; + canvas.height = height; + + const context = canvas.getContext( '2d' ); + context.drawImage( image, 0, 0, width, height ); + + console.warn( 'THREE.WebGLRenderer: Texture has been resized from (' + dimensions.width + 'x' + dimensions.height + ') to (' + width + 'x' + height + ').' ); + + return canvas; + + } else { + + if ( 'data' in image ) { + + console.warn( 'THREE.WebGLRenderer: Image in DataTexture is too big (' + dimensions.width + 'x' + dimensions.height + ').' ); + + } + + return image; + + } + + } + + return image; + + } + + function textureNeedsGenerateMipmaps( texture ) { + + return texture.generateMipmaps && texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter; + + } + + function generateMipmap( target ) { + + _gl.generateMipmap( target ); + + } + + function getInternalFormat( internalFormatName, glFormat, glType, colorSpace, forceLinearTransfer = false ) { + + if ( internalFormatName !== null ) { + + if ( _gl[ internalFormatName ] !== undefined ) return _gl[ internalFormatName ]; + + console.warn( 'THREE.WebGLRenderer: Attempt to use non-existing WebGL internal format \'' + internalFormatName + '\'' ); + + } + + let internalFormat = glFormat; + + if ( glFormat === _gl.RED ) { + + if ( glType === _gl.FLOAT ) internalFormat = _gl.R32F; + if ( glType === _gl.HALF_FLOAT ) internalFormat = _gl.R16F; + if ( glType === _gl.UNSIGNED_BYTE ) internalFormat = _gl.R8; + + } + + if ( glFormat === _gl.RED_INTEGER ) { + + if ( glType === _gl.UNSIGNED_BYTE ) internalFormat = _gl.R8UI; + if ( glType === _gl.UNSIGNED_SHORT ) internalFormat = _gl.R16UI; + if ( glType === _gl.UNSIGNED_INT ) internalFormat = _gl.R32UI; + if ( glType === _gl.BYTE ) internalFormat = _gl.R8I; + if ( glType === _gl.SHORT ) internalFormat = _gl.R16I; + if ( glType === _gl.INT ) internalFormat = _gl.R32I; + + } + + if ( glFormat === _gl.RG ) { + + if ( glType === _gl.FLOAT ) internalFormat = _gl.RG32F; + if ( glType === _gl.HALF_FLOAT ) internalFormat = _gl.RG16F; + if ( glType === _gl.UNSIGNED_BYTE ) internalFormat = _gl.RG8; + + } + + if ( glFormat === _gl.RG_INTEGER ) { + + if ( glType === _gl.UNSIGNED_BYTE ) internalFormat = _gl.RG8UI; + if ( glType === _gl.UNSIGNED_SHORT ) internalFormat = _gl.RG16UI; + if ( glType === _gl.UNSIGNED_INT ) internalFormat = _gl.RG32UI; + if ( glType === _gl.BYTE ) internalFormat = _gl.RG8I; + if ( glType === _gl.SHORT ) internalFormat = _gl.RG16I; + if ( glType === _gl.INT ) internalFormat = _gl.RG32I; + + } + + if ( glFormat === _gl.RGB ) { + + if ( glType === _gl.UNSIGNED_INT_5_9_9_9_REV ) internalFormat = _gl.RGB9_E5; + + } + + if ( glFormat === _gl.RGBA ) { + + const transfer = forceLinearTransfer ? LinearTransfer : ColorManagement.getTransfer( colorSpace ); + + if ( glType === _gl.FLOAT ) internalFormat = _gl.RGBA32F; + if ( glType === _gl.HALF_FLOAT ) internalFormat = _gl.RGBA16F; + if ( glType === _gl.UNSIGNED_BYTE ) internalFormat = ( transfer === SRGBTransfer ) ? _gl.SRGB8_ALPHA8 : _gl.RGBA8; + if ( glType === _gl.UNSIGNED_SHORT_4_4_4_4 ) internalFormat = _gl.RGBA4; + if ( glType === _gl.UNSIGNED_SHORT_5_5_5_1 ) internalFormat = _gl.RGB5_A1; + + } + + if ( internalFormat === _gl.R16F || internalFormat === _gl.R32F || + internalFormat === _gl.RG16F || internalFormat === _gl.RG32F || + internalFormat === _gl.RGBA16F || internalFormat === _gl.RGBA32F ) { + + extensions.get( 'EXT_color_buffer_float' ); + + } + + return internalFormat; + + } + + function getMipLevels( texture, image ) { + + if ( textureNeedsGenerateMipmaps( texture ) === true || ( texture.isFramebufferTexture && texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter ) ) { + + return Math.log2( Math.max( image.width, image.height ) ) + 1; + + } else if ( texture.mipmaps !== undefined && texture.mipmaps.length > 0 ) { + + // user-defined mipmaps + + return texture.mipmaps.length; + + } else if ( texture.isCompressedTexture && Array.isArray( texture.image ) ) { + + return image.mipmaps.length; + + } else { + + // texture without mipmaps (only base level) + + return 1; + + } + + } + + // + + function onTextureDispose( event ) { + + const texture = event.target; + + texture.removeEventListener( 'dispose', onTextureDispose ); + + deallocateTexture( texture ); + + if ( texture.isVideoTexture ) { + + _videoTextures.delete( texture ); + + } + + } + + function onRenderTargetDispose( event ) { + + const renderTarget = event.target; + + renderTarget.removeEventListener( 'dispose', onRenderTargetDispose ); + + deallocateRenderTarget( renderTarget ); + + } + + // + + function deallocateTexture( texture ) { + + const textureProperties = properties.get( texture ); + + if ( textureProperties.__webglInit === undefined ) return; + + // check if it's necessary to remove the WebGLTexture object + + const source = texture.source; + const webglTextures = _sources.get( source ); + + if ( webglTextures ) { + + const webglTexture = webglTextures[ textureProperties.__cacheKey ]; + webglTexture.usedTimes --; + + // the WebGLTexture object is not used anymore, remove it + + if ( webglTexture.usedTimes === 0 ) { + + deleteTexture( texture ); + + } + + // remove the weak map entry if no WebGLTexture uses the source anymore + + if ( Object.keys( webglTextures ).length === 0 ) { + + _sources.delete( source ); + + } + + } + + properties.remove( texture ); + + } + + function deleteTexture( texture ) { + + const textureProperties = properties.get( texture ); + _gl.deleteTexture( textureProperties.__webglTexture ); + + const source = texture.source; + const webglTextures = _sources.get( source ); + delete webglTextures[ textureProperties.__cacheKey ]; + + info.memory.textures --; + + } + + function deallocateRenderTarget( renderTarget ) { + + const renderTargetProperties = properties.get( renderTarget ); + + if ( renderTarget.depthTexture ) { + + renderTarget.depthTexture.dispose(); + + } + + if ( renderTarget.isWebGLCubeRenderTarget ) { + + for ( let i = 0; i < 6; i ++ ) { + + if ( Array.isArray( renderTargetProperties.__webglFramebuffer[ i ] ) ) { + + for ( let level = 0; level < renderTargetProperties.__webglFramebuffer[ i ].length; level ++ ) _gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer[ i ][ level ] ); + + } else { + + _gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer[ i ] ); + + } + + if ( renderTargetProperties.__webglDepthbuffer ) _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthbuffer[ i ] ); + + } + + } else { + + if ( Array.isArray( renderTargetProperties.__webglFramebuffer ) ) { + + for ( let level = 0; level < renderTargetProperties.__webglFramebuffer.length; level ++ ) _gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer[ level ] ); + + } else { + + _gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer ); + + } + + if ( renderTargetProperties.__webglDepthbuffer ) _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthbuffer ); + if ( renderTargetProperties.__webglMultisampledFramebuffer ) _gl.deleteFramebuffer( renderTargetProperties.__webglMultisampledFramebuffer ); + + if ( renderTargetProperties.__webglColorRenderbuffer ) { + + for ( let i = 0; i < renderTargetProperties.__webglColorRenderbuffer.length; i ++ ) { + + if ( renderTargetProperties.__webglColorRenderbuffer[ i ] ) _gl.deleteRenderbuffer( renderTargetProperties.__webglColorRenderbuffer[ i ] ); + + } + + } + + if ( renderTargetProperties.__webglDepthRenderbuffer ) _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthRenderbuffer ); + + } + + const textures = renderTarget.textures; + + for ( let i = 0, il = textures.length; i < il; i ++ ) { + + const attachmentProperties = properties.get( textures[ i ] ); + + if ( attachmentProperties.__webglTexture ) { + + _gl.deleteTexture( attachmentProperties.__webglTexture ); + + info.memory.textures --; + + } + + properties.remove( textures[ i ] ); + + } + + properties.remove( renderTarget ); + + } + + // + + let textureUnits = 0; + + function resetTextureUnits() { + + textureUnits = 0; + + } + + function allocateTextureUnit() { + + const textureUnit = textureUnits; + + if ( textureUnit >= capabilities.maxTextures ) { + + console.warn( 'THREE.WebGLTextures: Trying to use ' + textureUnit + ' texture units while this GPU supports only ' + capabilities.maxTextures ); + + } + + textureUnits += 1; + + return textureUnit; + + } + + function getTextureCacheKey( texture ) { + + const array = []; + + array.push( texture.wrapS ); + array.push( texture.wrapT ); + array.push( texture.wrapR || 0 ); + array.push( texture.magFilter ); + array.push( texture.minFilter ); + array.push( texture.anisotropy ); + array.push( texture.internalFormat ); + array.push( texture.format ); + array.push( texture.type ); + array.push( texture.generateMipmaps ); + array.push( texture.premultiplyAlpha ); + array.push( texture.flipY ); + array.push( texture.unpackAlignment ); + array.push( texture.colorSpace ); + + return array.join(); + + } + + // + + function setTexture2D( texture, slot ) { + + const textureProperties = properties.get( texture ); + + if ( texture.isVideoTexture ) updateVideoTexture( texture ); + + if ( texture.isRenderTargetTexture === false && texture.version > 0 && textureProperties.__version !== texture.version ) { + + const image = texture.image; + + if ( image === null ) { + + console.warn( 'THREE.WebGLRenderer: Texture marked for update but no image data found.' ); + + } else if ( image.complete === false ) { + + console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is incomplete' ); + + } else { + + uploadTexture( textureProperties, texture, slot ); + return; + + } + + } + + state.bindTexture( _gl.TEXTURE_2D, textureProperties.__webglTexture, _gl.TEXTURE0 + slot ); + + } + + function setTexture2DArray( texture, slot ) { + + const textureProperties = properties.get( texture ); + + if ( texture.version > 0 && textureProperties.__version !== texture.version ) { + + uploadTexture( textureProperties, texture, slot ); + return; + + } + + state.bindTexture( _gl.TEXTURE_2D_ARRAY, textureProperties.__webglTexture, _gl.TEXTURE0 + slot ); + + } + + function setTexture3D( texture, slot ) { + + const textureProperties = properties.get( texture ); + + if ( texture.version > 0 && textureProperties.__version !== texture.version ) { + + uploadTexture( textureProperties, texture, slot ); + return; + + } + + state.bindTexture( _gl.TEXTURE_3D, textureProperties.__webglTexture, _gl.TEXTURE0 + slot ); + + } + + function setTextureCube( texture, slot ) { + + const textureProperties = properties.get( texture ); + + if ( texture.version > 0 && textureProperties.__version !== texture.version ) { + + uploadCubeTexture( textureProperties, texture, slot ); + return; + + } + + state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__webglTexture, _gl.TEXTURE0 + slot ); + + } + + const wrappingToGL = { + [ RepeatWrapping ]: _gl.REPEAT, + [ ClampToEdgeWrapping ]: _gl.CLAMP_TO_EDGE, + [ MirroredRepeatWrapping ]: _gl.MIRRORED_REPEAT + }; + + const filterToGL = { + [ NearestFilter ]: _gl.NEAREST, + [ NearestMipmapNearestFilter ]: _gl.NEAREST_MIPMAP_NEAREST, + [ NearestMipmapLinearFilter ]: _gl.NEAREST_MIPMAP_LINEAR, + + [ LinearFilter ]: _gl.LINEAR, + [ LinearMipmapNearestFilter ]: _gl.LINEAR_MIPMAP_NEAREST, + [ LinearMipmapLinearFilter ]: _gl.LINEAR_MIPMAP_LINEAR + }; + + const compareToGL = { + [ NeverCompare ]: _gl.NEVER, + [ AlwaysCompare ]: _gl.ALWAYS, + [ LessCompare ]: _gl.LESS, + [ LessEqualCompare ]: _gl.LEQUAL, + [ EqualCompare ]: _gl.EQUAL, + [ GreaterEqualCompare ]: _gl.GEQUAL, + [ GreaterCompare ]: _gl.GREATER, + [ NotEqualCompare ]: _gl.NOTEQUAL + }; + + function setTextureParameters( textureType, texture ) { + + if ( texture.type === FloatType && extensions.has( 'OES_texture_float_linear' ) === false && + ( texture.magFilter === LinearFilter || texture.magFilter === LinearMipmapNearestFilter || texture.magFilter === NearestMipmapLinearFilter || texture.magFilter === LinearMipmapLinearFilter || + texture.minFilter === LinearFilter || texture.minFilter === LinearMipmapNearestFilter || texture.minFilter === NearestMipmapLinearFilter || texture.minFilter === LinearMipmapLinearFilter ) ) { + + console.warn( 'THREE.WebGLRenderer: Unable to use linear filtering with floating point textures. OES_texture_float_linear not supported on this device.' ); + + } + + _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, wrappingToGL[ texture.wrapS ] ); + _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, wrappingToGL[ texture.wrapT ] ); + + if ( textureType === _gl.TEXTURE_3D || textureType === _gl.TEXTURE_2D_ARRAY ) { + + _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_R, wrappingToGL[ texture.wrapR ] ); + + } + + _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, filterToGL[ texture.magFilter ] ); + _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, filterToGL[ texture.minFilter ] ); + + if ( texture.compareFunction ) { + + _gl.texParameteri( textureType, _gl.TEXTURE_COMPARE_MODE, _gl.COMPARE_REF_TO_TEXTURE ); + _gl.texParameteri( textureType, _gl.TEXTURE_COMPARE_FUNC, compareToGL[ texture.compareFunction ] ); + + } + + if ( extensions.has( 'EXT_texture_filter_anisotropic' ) === true ) { + + if ( texture.magFilter === NearestFilter ) return; + if ( texture.minFilter !== NearestMipmapLinearFilter && texture.minFilter !== LinearMipmapLinearFilter ) return; + if ( texture.type === FloatType && extensions.has( 'OES_texture_float_linear' ) === false ) return; // verify extension + + if ( texture.anisotropy > 1 || properties.get( texture ).__currentAnisotropy ) { + + const extension = extensions.get( 'EXT_texture_filter_anisotropic' ); + _gl.texParameterf( textureType, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, capabilities.getMaxAnisotropy() ) ); + properties.get( texture ).__currentAnisotropy = texture.anisotropy; + + } + + } + + } + + function initTexture( textureProperties, texture ) { + + let forceUpload = false; + + if ( textureProperties.__webglInit === undefined ) { + + textureProperties.__webglInit = true; + + texture.addEventListener( 'dispose', onTextureDispose ); + + } + + // create Source <-> WebGLTextures mapping if necessary + + const source = texture.source; + let webglTextures = _sources.get( source ); + + if ( webglTextures === undefined ) { + + webglTextures = {}; + _sources.set( source, webglTextures ); + + } + + // check if there is already a WebGLTexture object for the given texture parameters + + const textureCacheKey = getTextureCacheKey( texture ); + + if ( textureCacheKey !== textureProperties.__cacheKey ) { + + // if not, create a new instance of WebGLTexture + + if ( webglTextures[ textureCacheKey ] === undefined ) { + + // create new entry + + webglTextures[ textureCacheKey ] = { + texture: _gl.createTexture(), + usedTimes: 0 + }; + + info.memory.textures ++; + + // when a new instance of WebGLTexture was created, a texture upload is required + // even if the image contents are identical + + forceUpload = true; + + } + + webglTextures[ textureCacheKey ].usedTimes ++; + + // every time the texture cache key changes, it's necessary to check if an instance of + // WebGLTexture can be deleted in order to avoid a memory leak. + + const webglTexture = webglTextures[ textureProperties.__cacheKey ]; + + if ( webglTexture !== undefined ) { + + webglTextures[ textureProperties.__cacheKey ].usedTimes --; + + if ( webglTexture.usedTimes === 0 ) { + + deleteTexture( texture ); + + } + + } + + // store references to cache key and WebGLTexture object + + textureProperties.__cacheKey = textureCacheKey; + textureProperties.__webglTexture = webglTextures[ textureCacheKey ].texture; + + } + + return forceUpload; + + } + + function uploadTexture( textureProperties, texture, slot ) { + + let textureType = _gl.TEXTURE_2D; + + if ( texture.isDataArrayTexture || texture.isCompressedArrayTexture ) textureType = _gl.TEXTURE_2D_ARRAY; + if ( texture.isData3DTexture ) textureType = _gl.TEXTURE_3D; + + const forceUpload = initTexture( textureProperties, texture ); + const source = texture.source; + + state.bindTexture( textureType, textureProperties.__webglTexture, _gl.TEXTURE0 + slot ); + + const sourceProperties = properties.get( source ); + + if ( source.version !== sourceProperties.__version || forceUpload === true ) { + + state.activeTexture( _gl.TEXTURE0 + slot ); + + const workingPrimaries = ColorManagement.getPrimaries( ColorManagement.workingColorSpace ); + const texturePrimaries = texture.colorSpace === NoColorSpace ? null : ColorManagement.getPrimaries( texture.colorSpace ); + const unpackConversion = texture.colorSpace === NoColorSpace || workingPrimaries === texturePrimaries ? _gl.NONE : _gl.BROWSER_DEFAULT_WEBGL; + + _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY ); + _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha ); + _gl.pixelStorei( _gl.UNPACK_ALIGNMENT, texture.unpackAlignment ); + _gl.pixelStorei( _gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, unpackConversion ); + + let image = resizeImage( texture.image, false, capabilities.maxTextureSize ); + image = verifyColorSpace( texture, image ); + + const glFormat = utils.convert( texture.format, texture.colorSpace ); + + const glType = utils.convert( texture.type ); + let glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.colorSpace, texture.isVideoTexture ); + + setTextureParameters( textureType, texture ); + + let mipmap; + const mipmaps = texture.mipmaps; + + const useTexStorage = ( texture.isVideoTexture !== true ); + const allocateMemory = ( sourceProperties.__version === undefined ) || ( forceUpload === true ); + const dataReady = source.dataReady; + const levels = getMipLevels( texture, image ); + + if ( texture.isDepthTexture ) { + + // populate depth texture with dummy data + + glInternalFormat = _gl.DEPTH_COMPONENT16; + + if ( texture.type === FloatType ) { + + glInternalFormat = _gl.DEPTH_COMPONENT32F; + + } else if ( texture.type === UnsignedIntType ) { + + glInternalFormat = _gl.DEPTH_COMPONENT24; + + } else if ( texture.type === UnsignedInt248Type ) { + + glInternalFormat = _gl.DEPTH24_STENCIL8; + + } + + // + + if ( allocateMemory ) { + + if ( useTexStorage ) { + + state.texStorage2D( _gl.TEXTURE_2D, 1, glInternalFormat, image.width, image.height ); + + } else { + + state.texImage2D( _gl.TEXTURE_2D, 0, glInternalFormat, image.width, image.height, 0, glFormat, glType, null ); + + } + + } + + } else if ( texture.isDataTexture ) { + + // use manually created mipmaps if available + // if there are no manual mipmaps + // set 0 level mipmap and then use GL to generate other mipmap levels + + if ( mipmaps.length > 0 ) { + + if ( useTexStorage && allocateMemory ) { + + state.texStorage2D( _gl.TEXTURE_2D, levels, glInternalFormat, mipmaps[ 0 ].width, mipmaps[ 0 ].height ); + + } + + for ( let i = 0, il = mipmaps.length; i < il; i ++ ) { + + mipmap = mipmaps[ i ]; + + if ( useTexStorage ) { + + if ( dataReady ) { + + state.texSubImage2D( _gl.TEXTURE_2D, i, 0, 0, mipmap.width, mipmap.height, glFormat, glType, mipmap.data ); + + } + + } else { + + state.texImage2D( _gl.TEXTURE_2D, i, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data ); + + } + + } + + texture.generateMipmaps = false; + + } else { + + if ( useTexStorage ) { + + if ( allocateMemory ) { + + state.texStorage2D( _gl.TEXTURE_2D, levels, glInternalFormat, image.width, image.height ); + + } + + if ( dataReady ) { + + state.texSubImage2D( _gl.TEXTURE_2D, 0, 0, 0, image.width, image.height, glFormat, glType, image.data ); + + } + + } else { + + state.texImage2D( _gl.TEXTURE_2D, 0, glInternalFormat, image.width, image.height, 0, glFormat, glType, image.data ); + + } + + } + + } else if ( texture.isCompressedTexture ) { + + if ( texture.isCompressedArrayTexture ) { + + if ( useTexStorage && allocateMemory ) { + + state.texStorage3D( _gl.TEXTURE_2D_ARRAY, levels, glInternalFormat, mipmaps[ 0 ].width, mipmaps[ 0 ].height, image.depth ); + + } + + for ( let i = 0, il = mipmaps.length; i < il; i ++ ) { + + mipmap = mipmaps[ i ]; + + if ( texture.format !== RGBAFormat ) { + + if ( glFormat !== null ) { + + if ( useTexStorage ) { + + if ( dataReady ) { + + state.compressedTexSubImage3D( _gl.TEXTURE_2D_ARRAY, i, 0, 0, 0, mipmap.width, mipmap.height, image.depth, glFormat, mipmap.data, 0, 0 ); + + } + + } else { + + state.compressedTexImage3D( _gl.TEXTURE_2D_ARRAY, i, glInternalFormat, mipmap.width, mipmap.height, image.depth, 0, mipmap.data, 0, 0 ); + + } + + } else { + + console.warn( 'THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .uploadTexture()' ); + + } + + } else { + + if ( useTexStorage ) { + + if ( dataReady ) { + + state.texSubImage3D( _gl.TEXTURE_2D_ARRAY, i, 0, 0, 0, mipmap.width, mipmap.height, image.depth, glFormat, glType, mipmap.data ); + + } + + } else { + + state.texImage3D( _gl.TEXTURE_2D_ARRAY, i, glInternalFormat, mipmap.width, mipmap.height, image.depth, 0, glFormat, glType, mipmap.data ); + + } + + } + + } + + } else { + + if ( useTexStorage && allocateMemory ) { + + state.texStorage2D( _gl.TEXTURE_2D, levels, glInternalFormat, mipmaps[ 0 ].width, mipmaps[ 0 ].height ); + + } + + for ( let i = 0, il = mipmaps.length; i < il; i ++ ) { + + mipmap = mipmaps[ i ]; + + if ( texture.format !== RGBAFormat ) { + + if ( glFormat !== null ) { + + if ( useTexStorage ) { + + if ( dataReady ) { + + state.compressedTexSubImage2D( _gl.TEXTURE_2D, i, 0, 0, mipmap.width, mipmap.height, glFormat, mipmap.data ); + + } + + } else { + + state.compressedTexImage2D( _gl.TEXTURE_2D, i, glInternalFormat, mipmap.width, mipmap.height, 0, mipmap.data ); + + } + + } else { + + console.warn( 'THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .uploadTexture()' ); + + } + + } else { + + if ( useTexStorage ) { + + if ( dataReady ) { + + state.texSubImage2D( _gl.TEXTURE_2D, i, 0, 0, mipmap.width, mipmap.height, glFormat, glType, mipmap.data ); + + } + + } else { + + state.texImage2D( _gl.TEXTURE_2D, i, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data ); + + } + + } + + } + + } + + } else if ( texture.isDataArrayTexture ) { + + if ( useTexStorage ) { + + if ( allocateMemory ) { + + state.texStorage3D( _gl.TEXTURE_2D_ARRAY, levels, glInternalFormat, image.width, image.height, image.depth ); + + } + + if ( dataReady ) { + + state.texSubImage3D( _gl.TEXTURE_2D_ARRAY, 0, 0, 0, 0, image.width, image.height, image.depth, glFormat, glType, image.data ); + + } + + } else { + + state.texImage3D( _gl.TEXTURE_2D_ARRAY, 0, glInternalFormat, image.width, image.height, image.depth, 0, glFormat, glType, image.data ); + + } + + } else if ( texture.isData3DTexture ) { + + if ( useTexStorage ) { + + if ( allocateMemory ) { + + state.texStorage3D( _gl.TEXTURE_3D, levels, glInternalFormat, image.width, image.height, image.depth ); + + } + + if ( dataReady ) { + + state.texSubImage3D( _gl.TEXTURE_3D, 0, 0, 0, 0, image.width, image.height, image.depth, glFormat, glType, image.data ); + + } + + } else { + + state.texImage3D( _gl.TEXTURE_3D, 0, glInternalFormat, image.width, image.height, image.depth, 0, glFormat, glType, image.data ); + + } + + } else if ( texture.isFramebufferTexture ) { + + if ( allocateMemory ) { + + if ( useTexStorage ) { + + state.texStorage2D( _gl.TEXTURE_2D, levels, glInternalFormat, image.width, image.height ); + + } else { + + let width = image.width, height = image.height; + + for ( let i = 0; i < levels; i ++ ) { + + state.texImage2D( _gl.TEXTURE_2D, i, glInternalFormat, width, height, 0, glFormat, glType, null ); + + width >>= 1; + height >>= 1; + + } + + } + + } + + } else { + + // regular Texture (image, video, canvas) + + // use manually created mipmaps if available + // if there are no manual mipmaps + // set 0 level mipmap and then use GL to generate other mipmap levels + + if ( mipmaps.length > 0 ) { + + if ( useTexStorage && allocateMemory ) { + + const dimensions = getDimensions( mipmaps[ 0 ] ); + + state.texStorage2D( _gl.TEXTURE_2D, levels, glInternalFormat, dimensions.width, dimensions.height ); + + } + + for ( let i = 0, il = mipmaps.length; i < il; i ++ ) { + + mipmap = mipmaps[ i ]; + + if ( useTexStorage ) { + + if ( dataReady ) { + + state.texSubImage2D( _gl.TEXTURE_2D, i, 0, 0, glFormat, glType, mipmap ); + + } + + } else { + + state.texImage2D( _gl.TEXTURE_2D, i, glInternalFormat, glFormat, glType, mipmap ); + + } + + } + + texture.generateMipmaps = false; + + } else { + + if ( useTexStorage ) { + + if ( allocateMemory ) { + + const dimensions = getDimensions( image ); + + state.texStorage2D( _gl.TEXTURE_2D, levels, glInternalFormat, dimensions.width, dimensions.height ); + + } + + if ( dataReady ) { + + state.texSubImage2D( _gl.TEXTURE_2D, 0, 0, 0, glFormat, glType, image ); + + } + + } else { + + state.texImage2D( _gl.TEXTURE_2D, 0, glInternalFormat, glFormat, glType, image ); + + } + + } + + } + + if ( textureNeedsGenerateMipmaps( texture ) ) { + + generateMipmap( textureType ); + + } + + sourceProperties.__version = source.version; + + if ( texture.onUpdate ) texture.onUpdate( texture ); + + } + + textureProperties.__version = texture.version; + + } + + function uploadCubeTexture( textureProperties, texture, slot ) { + + if ( texture.image.length !== 6 ) return; + + const forceUpload = initTexture( textureProperties, texture ); + const source = texture.source; + + state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__webglTexture, _gl.TEXTURE0 + slot ); + + const sourceProperties = properties.get( source ); + + if ( source.version !== sourceProperties.__version || forceUpload === true ) { + + state.activeTexture( _gl.TEXTURE0 + slot ); + + const workingPrimaries = ColorManagement.getPrimaries( ColorManagement.workingColorSpace ); + const texturePrimaries = texture.colorSpace === NoColorSpace ? null : ColorManagement.getPrimaries( texture.colorSpace ); + const unpackConversion = texture.colorSpace === NoColorSpace || workingPrimaries === texturePrimaries ? _gl.NONE : _gl.BROWSER_DEFAULT_WEBGL; + + _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY ); + _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha ); + _gl.pixelStorei( _gl.UNPACK_ALIGNMENT, texture.unpackAlignment ); + _gl.pixelStorei( _gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, unpackConversion ); + + const isCompressed = ( texture.isCompressedTexture || texture.image[ 0 ].isCompressedTexture ); + const isDataTexture = ( texture.image[ 0 ] && texture.image[ 0 ].isDataTexture ); + + const cubeImage = []; + + for ( let i = 0; i < 6; i ++ ) { + + if ( ! isCompressed && ! isDataTexture ) { + + cubeImage[ i ] = resizeImage( texture.image[ i ], true, capabilities.maxCubemapSize ); + + } else { + + cubeImage[ i ] = isDataTexture ? texture.image[ i ].image : texture.image[ i ]; + + } + + cubeImage[ i ] = verifyColorSpace( texture, cubeImage[ i ] ); + + } + + const image = cubeImage[ 0 ], + glFormat = utils.convert( texture.format, texture.colorSpace ), + glType = utils.convert( texture.type ), + glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.colorSpace ); + + const useTexStorage = ( texture.isVideoTexture !== true ); + const allocateMemory = ( sourceProperties.__version === undefined ) || ( forceUpload === true ); + const dataReady = source.dataReady; + let levels = getMipLevels( texture, image ); + + setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture ); + + let mipmaps; + + if ( isCompressed ) { + + if ( useTexStorage && allocateMemory ) { + + state.texStorage2D( _gl.TEXTURE_CUBE_MAP, levels, glInternalFormat, image.width, image.height ); + + } + + for ( let i = 0; i < 6; i ++ ) { + + mipmaps = cubeImage[ i ].mipmaps; + + for ( let j = 0; j < mipmaps.length; j ++ ) { + + const mipmap = mipmaps[ j ]; + + if ( texture.format !== RGBAFormat ) { + + if ( glFormat !== null ) { + + if ( useTexStorage ) { + + if ( dataReady ) { + + state.compressedTexSubImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, 0, 0, mipmap.width, mipmap.height, glFormat, mipmap.data ); + + } + + } else { + + state.compressedTexImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glInternalFormat, mipmap.width, mipmap.height, 0, mipmap.data ); + + } + + } else { + + console.warn( 'THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .setTextureCube()' ); + + } + + } else { + + if ( useTexStorage ) { + + if ( dataReady ) { + + state.texSubImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, 0, 0, mipmap.width, mipmap.height, glFormat, glType, mipmap.data ); + + } + + } else { + + state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data ); + + } + + } + + } + + } + + } else { + + mipmaps = texture.mipmaps; + + if ( useTexStorage && allocateMemory ) { + + // TODO: Uniformly handle mipmap definitions + // Normal textures and compressed cube textures define base level + mips with their mipmap array + // Uncompressed cube textures use their mipmap array only for mips (no base level) + + if ( mipmaps.length > 0 ) levels ++; + + const dimensions = getDimensions( cubeImage[ 0 ] ); + + state.texStorage2D( _gl.TEXTURE_CUBE_MAP, levels, glInternalFormat, dimensions.width, dimensions.height ); + + } + + for ( let i = 0; i < 6; i ++ ) { + + if ( isDataTexture ) { + + if ( useTexStorage ) { + + if ( dataReady ) { + + state.texSubImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, 0, 0, cubeImage[ i ].width, cubeImage[ i ].height, glFormat, glType, cubeImage[ i ].data ); + + } + + } else { + + state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glInternalFormat, cubeImage[ i ].width, cubeImage[ i ].height, 0, glFormat, glType, cubeImage[ i ].data ); + + } + + for ( let j = 0; j < mipmaps.length; j ++ ) { + + const mipmap = mipmaps[ j ]; + const mipmapImage = mipmap.image[ i ].image; + + if ( useTexStorage ) { + + if ( dataReady ) { + + state.texSubImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j + 1, 0, 0, mipmapImage.width, mipmapImage.height, glFormat, glType, mipmapImage.data ); + + } + + } else { + + state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j + 1, glInternalFormat, mipmapImage.width, mipmapImage.height, 0, glFormat, glType, mipmapImage.data ); + + } + + } + + } else { + + if ( useTexStorage ) { + + if ( dataReady ) { + + state.texSubImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, 0, 0, glFormat, glType, cubeImage[ i ] ); + + } + + } else { + + state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glInternalFormat, glFormat, glType, cubeImage[ i ] ); + + } + + for ( let j = 0; j < mipmaps.length; j ++ ) { + + const mipmap = mipmaps[ j ]; + + if ( useTexStorage ) { + + if ( dataReady ) { + + state.texSubImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j + 1, 0, 0, glFormat, glType, mipmap.image[ i ] ); + + } + + } else { + + state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j + 1, glInternalFormat, glFormat, glType, mipmap.image[ i ] ); + + } + + } + + } + + } + + } + + if ( textureNeedsGenerateMipmaps( texture ) ) { + + // We assume images for cube map have the same size. + generateMipmap( _gl.TEXTURE_CUBE_MAP ); + + } + + sourceProperties.__version = source.version; + + if ( texture.onUpdate ) texture.onUpdate( texture ); + + } + + textureProperties.__version = texture.version; + + } + + // Render targets + + // Setup storage for target texture and bind it to correct framebuffer + function setupFrameBufferTexture( framebuffer, renderTarget, texture, attachment, textureTarget, level ) { + + const glFormat = utils.convert( texture.format, texture.colorSpace ); + const glType = utils.convert( texture.type ); + const glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.colorSpace ); + const renderTargetProperties = properties.get( renderTarget ); + + if ( ! renderTargetProperties.__hasExternalTextures ) { + + const width = Math.max( 1, renderTarget.width >> level ); + const height = Math.max( 1, renderTarget.height >> level ); + + if ( textureTarget === _gl.TEXTURE_3D || textureTarget === _gl.TEXTURE_2D_ARRAY ) { + + state.texImage3D( textureTarget, level, glInternalFormat, width, height, renderTarget.depth, 0, glFormat, glType, null ); + + } else { + + state.texImage2D( textureTarget, level, glInternalFormat, width, height, 0, glFormat, glType, null ); + + } + + } + + state.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer ); + + if ( useMultisampledRTT( renderTarget ) ) { + + multisampledRTTExt.framebufferTexture2DMultisampleEXT( _gl.FRAMEBUFFER, attachment, textureTarget, properties.get( texture ).__webglTexture, 0, getRenderTargetSamples( renderTarget ) ); + + } else if ( textureTarget === _gl.TEXTURE_2D || ( textureTarget >= _gl.TEXTURE_CUBE_MAP_POSITIVE_X && textureTarget <= _gl.TEXTURE_CUBE_MAP_NEGATIVE_Z ) ) { // see #24753 + + _gl.framebufferTexture2D( _gl.FRAMEBUFFER, attachment, textureTarget, properties.get( texture ).__webglTexture, level ); + + } + + state.bindFramebuffer( _gl.FRAMEBUFFER, null ); + + } + + + // Setup storage for internal depth/stencil buffers and bind to correct framebuffer + function setupRenderBufferStorage( renderbuffer, renderTarget, isMultisample ) { + + _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderbuffer ); + + if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) { + + let glInternalFormat = _gl.DEPTH_COMPONENT24; + + if ( isMultisample || useMultisampledRTT( renderTarget ) ) { + + const depthTexture = renderTarget.depthTexture; + + if ( depthTexture && depthTexture.isDepthTexture ) { + + if ( depthTexture.type === FloatType ) { + + glInternalFormat = _gl.DEPTH_COMPONENT32F; + + } else if ( depthTexture.type === UnsignedIntType ) { + + glInternalFormat = _gl.DEPTH_COMPONENT24; + + } + + } + + const samples = getRenderTargetSamples( renderTarget ); + + if ( useMultisampledRTT( renderTarget ) ) { + + multisampledRTTExt.renderbufferStorageMultisampleEXT( _gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height ); + + } else { + + _gl.renderbufferStorageMultisample( _gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height ); + + } + + } else { + + _gl.renderbufferStorage( _gl.RENDERBUFFER, glInternalFormat, renderTarget.width, renderTarget.height ); + + } + + _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer ); + + } else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) { + + const samples = getRenderTargetSamples( renderTarget ); + + if ( isMultisample && useMultisampledRTT( renderTarget ) === false ) { + + _gl.renderbufferStorageMultisample( _gl.RENDERBUFFER, samples, _gl.DEPTH24_STENCIL8, renderTarget.width, renderTarget.height ); + + } else if ( useMultisampledRTT( renderTarget ) ) { + + multisampledRTTExt.renderbufferStorageMultisampleEXT( _gl.RENDERBUFFER, samples, _gl.DEPTH24_STENCIL8, renderTarget.width, renderTarget.height ); + + } else { + + _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_STENCIL, renderTarget.width, renderTarget.height ); + + } + + + _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer ); + + } else { + + const textures = renderTarget.textures; + + for ( let i = 0; i < textures.length; i ++ ) { + + const texture = textures[ i ]; + + const glFormat = utils.convert( texture.format, texture.colorSpace ); + const glType = utils.convert( texture.type ); + const glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.colorSpace ); + const samples = getRenderTargetSamples( renderTarget ); + + if ( isMultisample && useMultisampledRTT( renderTarget ) === false ) { + + _gl.renderbufferStorageMultisample( _gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height ); + + } else if ( useMultisampledRTT( renderTarget ) ) { + + multisampledRTTExt.renderbufferStorageMultisampleEXT( _gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height ); + + } else { + + _gl.renderbufferStorage( _gl.RENDERBUFFER, glInternalFormat, renderTarget.width, renderTarget.height ); + + } + + } + + } + + _gl.bindRenderbuffer( _gl.RENDERBUFFER, null ); + + } + + // Setup resources for a Depth Texture for a FBO (needs an extension) + function setupDepthTexture( framebuffer, renderTarget ) { + + const isCube = ( renderTarget && renderTarget.isWebGLCubeRenderTarget ); + if ( isCube ) throw new Error( 'Depth Texture with cube render targets is not supported' ); + + state.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer ); + + if ( ! ( renderTarget.depthTexture && renderTarget.depthTexture.isDepthTexture ) ) { + + throw new Error( 'renderTarget.depthTexture must be an instance of THREE.DepthTexture' ); + + } + + // upload an empty depth texture with framebuffer size + if ( ! properties.get( renderTarget.depthTexture ).__webglTexture || + renderTarget.depthTexture.image.width !== renderTarget.width || + renderTarget.depthTexture.image.height !== renderTarget.height ) { + + renderTarget.depthTexture.image.width = renderTarget.width; + renderTarget.depthTexture.image.height = renderTarget.height; + renderTarget.depthTexture.needsUpdate = true; + + } + + setTexture2D( renderTarget.depthTexture, 0 ); + + const webglDepthTexture = properties.get( renderTarget.depthTexture ).__webglTexture; + const samples = getRenderTargetSamples( renderTarget ); + + if ( renderTarget.depthTexture.format === DepthFormat ) { + + if ( useMultisampledRTT( renderTarget ) ) { + + multisampledRTTExt.framebufferTexture2DMultisampleEXT( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0, samples ); + + } else { + + _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0 ); + + } + + } else if ( renderTarget.depthTexture.format === DepthStencilFormat ) { + + if ( useMultisampledRTT( renderTarget ) ) { + + multisampledRTTExt.framebufferTexture2DMultisampleEXT( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0, samples ); + + } else { + + _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0 ); + + } + + } else { + + throw new Error( 'Unknown depthTexture format' ); + + } + + } + + // Setup GL resources for a non-texture depth buffer + function setupDepthRenderbuffer( renderTarget ) { + + const renderTargetProperties = properties.get( renderTarget ); + const isCube = ( renderTarget.isWebGLCubeRenderTarget === true ); + + if ( renderTarget.depthTexture && ! renderTargetProperties.__autoAllocateDepthBuffer ) { + + if ( isCube ) throw new Error( 'target.depthTexture not supported in Cube render targets' ); + + setupDepthTexture( renderTargetProperties.__webglFramebuffer, renderTarget ); + + } else { + + if ( isCube ) { + + renderTargetProperties.__webglDepthbuffer = []; + + for ( let i = 0; i < 6; i ++ ) { + + state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer[ i ] ); + renderTargetProperties.__webglDepthbuffer[ i ] = _gl.createRenderbuffer(); + setupRenderBufferStorage( renderTargetProperties.__webglDepthbuffer[ i ], renderTarget, false ); + + } + + } else { + + state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer ); + renderTargetProperties.__webglDepthbuffer = _gl.createRenderbuffer(); + setupRenderBufferStorage( renderTargetProperties.__webglDepthbuffer, renderTarget, false ); + + } + + } + + state.bindFramebuffer( _gl.FRAMEBUFFER, null ); + + } + + // rebind framebuffer with external textures + function rebindTextures( renderTarget, colorTexture, depthTexture ) { + + const renderTargetProperties = properties.get( renderTarget ); + + if ( colorTexture !== undefined ) { + + setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer, renderTarget, renderTarget.texture, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_2D, 0 ); + + } + + if ( depthTexture !== undefined ) { + + setupDepthRenderbuffer( renderTarget ); + + } + + } + + // Set up GL resources for the render target + function setupRenderTarget( renderTarget ) { + + const texture = renderTarget.texture; + + const renderTargetProperties = properties.get( renderTarget ); + const textureProperties = properties.get( texture ); + + renderTarget.addEventListener( 'dispose', onRenderTargetDispose ); + + const textures = renderTarget.textures; + + const isCube = ( renderTarget.isWebGLCubeRenderTarget === true ); + const isMultipleRenderTargets = ( textures.length > 1 ); + + if ( ! isMultipleRenderTargets ) { + + if ( textureProperties.__webglTexture === undefined ) { + + textureProperties.__webglTexture = _gl.createTexture(); + + } + + textureProperties.__version = texture.version; + info.memory.textures ++; + + } + + // Setup framebuffer + + if ( isCube ) { + + renderTargetProperties.__webglFramebuffer = []; + + for ( let i = 0; i < 6; i ++ ) { + + if ( texture.mipmaps && texture.mipmaps.length > 0 ) { + + renderTargetProperties.__webglFramebuffer[ i ] = []; + + for ( let level = 0; level < texture.mipmaps.length; level ++ ) { + + renderTargetProperties.__webglFramebuffer[ i ][ level ] = _gl.createFramebuffer(); + + } + + } else { + + renderTargetProperties.__webglFramebuffer[ i ] = _gl.createFramebuffer(); + + } + + } + + } else { + + if ( texture.mipmaps && texture.mipmaps.length > 0 ) { + + renderTargetProperties.__webglFramebuffer = []; + + for ( let level = 0; level < texture.mipmaps.length; level ++ ) { + + renderTargetProperties.__webglFramebuffer[ level ] = _gl.createFramebuffer(); + + } + + } else { + + renderTargetProperties.__webglFramebuffer = _gl.createFramebuffer(); + + } + + if ( isMultipleRenderTargets ) { + + for ( let i = 0, il = textures.length; i < il; i ++ ) { + + const attachmentProperties = properties.get( textures[ i ] ); + + if ( attachmentProperties.__webglTexture === undefined ) { + + attachmentProperties.__webglTexture = _gl.createTexture(); + + info.memory.textures ++; + + } + + } + + } + + if ( ( renderTarget.samples > 0 ) && useMultisampledRTT( renderTarget ) === false ) { + + renderTargetProperties.__webglMultisampledFramebuffer = _gl.createFramebuffer(); + renderTargetProperties.__webglColorRenderbuffer = []; + + state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer ); + + for ( let i = 0; i < textures.length; i ++ ) { + + const texture = textures[ i ]; + renderTargetProperties.__webglColorRenderbuffer[ i ] = _gl.createRenderbuffer(); + + _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderTargetProperties.__webglColorRenderbuffer[ i ] ); + + const glFormat = utils.convert( texture.format, texture.colorSpace ); + const glType = utils.convert( texture.type ); + const glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.colorSpace, renderTarget.isXRRenderTarget === true ); + const samples = getRenderTargetSamples( renderTarget ); + _gl.renderbufferStorageMultisample( _gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height ); + + _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0 + i, _gl.RENDERBUFFER, renderTargetProperties.__webglColorRenderbuffer[ i ] ); + + } + + _gl.bindRenderbuffer( _gl.RENDERBUFFER, null ); + + if ( renderTarget.depthBuffer ) { + + renderTargetProperties.__webglDepthRenderbuffer = _gl.createRenderbuffer(); + setupRenderBufferStorage( renderTargetProperties.__webglDepthRenderbuffer, renderTarget, true ); + + } + + state.bindFramebuffer( _gl.FRAMEBUFFER, null ); + + } + + } + + // Setup color buffer + + if ( isCube ) { + + state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__webglTexture ); + setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture ); + + for ( let i = 0; i < 6; i ++ ) { + + if ( texture.mipmaps && texture.mipmaps.length > 0 ) { + + for ( let level = 0; level < texture.mipmaps.length; level ++ ) { + + setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer[ i ][ level ], renderTarget, texture, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, level ); + + } + + } else { + + setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer[ i ], renderTarget, texture, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0 ); + + } + + } + + if ( textureNeedsGenerateMipmaps( texture ) ) { + + generateMipmap( _gl.TEXTURE_CUBE_MAP ); + + } + + state.unbindTexture(); + + } else if ( isMultipleRenderTargets ) { + + for ( let i = 0, il = textures.length; i < il; i ++ ) { + + const attachment = textures[ i ]; + const attachmentProperties = properties.get( attachment ); + + state.bindTexture( _gl.TEXTURE_2D, attachmentProperties.__webglTexture ); + setTextureParameters( _gl.TEXTURE_2D, attachment ); + setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer, renderTarget, attachment, _gl.COLOR_ATTACHMENT0 + i, _gl.TEXTURE_2D, 0 ); + + if ( textureNeedsGenerateMipmaps( attachment ) ) { + + generateMipmap( _gl.TEXTURE_2D ); + + } + + } + + state.unbindTexture(); + + } else { + + let glTextureType = _gl.TEXTURE_2D; + + if ( renderTarget.isWebGL3DRenderTarget || renderTarget.isWebGLArrayRenderTarget ) { + + glTextureType = renderTarget.isWebGL3DRenderTarget ? _gl.TEXTURE_3D : _gl.TEXTURE_2D_ARRAY; + + } + + state.bindTexture( glTextureType, textureProperties.__webglTexture ); + setTextureParameters( glTextureType, texture ); + + if ( texture.mipmaps && texture.mipmaps.length > 0 ) { + + for ( let level = 0; level < texture.mipmaps.length; level ++ ) { + + setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer[ level ], renderTarget, texture, _gl.COLOR_ATTACHMENT0, glTextureType, level ); + + } + + } else { + + setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer, renderTarget, texture, _gl.COLOR_ATTACHMENT0, glTextureType, 0 ); + + } + + if ( textureNeedsGenerateMipmaps( texture ) ) { + + generateMipmap( glTextureType ); + + } + + state.unbindTexture(); + + } + + // Setup depth and stencil buffers + + if ( renderTarget.depthBuffer ) { + + setupDepthRenderbuffer( renderTarget ); + + } + + } + + function updateRenderTargetMipmap( renderTarget ) { + + const textures = renderTarget.textures; + + for ( let i = 0, il = textures.length; i < il; i ++ ) { + + const texture = textures[ i ]; + + if ( textureNeedsGenerateMipmaps( texture ) ) { + + const target = renderTarget.isWebGLCubeRenderTarget ? _gl.TEXTURE_CUBE_MAP : _gl.TEXTURE_2D; + const webglTexture = properties.get( texture ).__webglTexture; + + state.bindTexture( target, webglTexture ); + generateMipmap( target ); + state.unbindTexture(); + + } + + } + + } + + const invalidationArrayRead = []; + const invalidationArrayDraw = []; + + function updateMultisampleRenderTarget( renderTarget ) { + + if ( renderTarget.samples > 0 ) { + + if ( useMultisampledRTT( renderTarget ) === false ) { + + const textures = renderTarget.textures; + const width = renderTarget.width; + const height = renderTarget.height; + let mask = _gl.COLOR_BUFFER_BIT; + const depthStyle = renderTarget.stencilBuffer ? _gl.DEPTH_STENCIL_ATTACHMENT : _gl.DEPTH_ATTACHMENT; + const renderTargetProperties = properties.get( renderTarget ); + const isMultipleRenderTargets = ( textures.length > 1 ); + + // If MRT we need to remove FBO attachments + if ( isMultipleRenderTargets ) { + + for ( let i = 0; i < textures.length; i ++ ) { + + state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer ); + _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0 + i, _gl.RENDERBUFFER, null ); + + state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer ); + _gl.framebufferTexture2D( _gl.DRAW_FRAMEBUFFER, _gl.COLOR_ATTACHMENT0 + i, _gl.TEXTURE_2D, null, 0 ); + + } + + } + + state.bindFramebuffer( _gl.READ_FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer ); + state.bindFramebuffer( _gl.DRAW_FRAMEBUFFER, renderTargetProperties.__webglFramebuffer ); + + for ( let i = 0; i < textures.length; i ++ ) { + + if ( renderTarget.resolveDepthBuffer ) { + + if ( renderTarget.depthBuffer ) mask |= _gl.DEPTH_BUFFER_BIT; + + // resolving stencil is slow with a D3D backend. disable it for all transmission render targets (see #27799) + + if ( renderTarget.stencilBuffer && renderTarget.resolveStencilBuffer ) mask |= _gl.STENCIL_BUFFER_BIT; + + } + + if ( isMultipleRenderTargets ) { + + _gl.framebufferRenderbuffer( _gl.READ_FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.RENDERBUFFER, renderTargetProperties.__webglColorRenderbuffer[ i ] ); + + const webglTexture = properties.get( textures[ i ] ).__webglTexture; + _gl.framebufferTexture2D( _gl.DRAW_FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_2D, webglTexture, 0 ); + + } + + _gl.blitFramebuffer( 0, 0, width, height, 0, 0, width, height, mask, _gl.NEAREST ); + + if ( supportsInvalidateFramebuffer === true ) { + + invalidationArrayRead.length = 0; + invalidationArrayDraw.length = 0; + + invalidationArrayRead.push( _gl.COLOR_ATTACHMENT0 + i ); + + if ( renderTarget.depthBuffer && renderTarget.resolveDepthBuffer === false ) { + + invalidationArrayRead.push( depthStyle ); + invalidationArrayDraw.push( depthStyle ); + + _gl.invalidateFramebuffer( _gl.DRAW_FRAMEBUFFER, invalidationArrayDraw ); + + } + + _gl.invalidateFramebuffer( _gl.READ_FRAMEBUFFER, invalidationArrayRead ); + + } + + } + + state.bindFramebuffer( _gl.READ_FRAMEBUFFER, null ); + state.bindFramebuffer( _gl.DRAW_FRAMEBUFFER, null ); + + // If MRT since pre-blit we removed the FBO we need to reconstruct the attachments + if ( isMultipleRenderTargets ) { + + for ( let i = 0; i < textures.length; i ++ ) { + + state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer ); + _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0 + i, _gl.RENDERBUFFER, renderTargetProperties.__webglColorRenderbuffer[ i ] ); + + const webglTexture = properties.get( textures[ i ] ).__webglTexture; + + state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer ); + _gl.framebufferTexture2D( _gl.DRAW_FRAMEBUFFER, _gl.COLOR_ATTACHMENT0 + i, _gl.TEXTURE_2D, webglTexture, 0 ); + + } + + } + + state.bindFramebuffer( _gl.DRAW_FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer ); + + } else { + + if ( renderTarget.depthBuffer && renderTarget.resolveDepthBuffer === false && supportsInvalidateFramebuffer ) { + + const depthStyle = renderTarget.stencilBuffer ? _gl.DEPTH_STENCIL_ATTACHMENT : _gl.DEPTH_ATTACHMENT; + + _gl.invalidateFramebuffer( _gl.DRAW_FRAMEBUFFER, [ depthStyle ] ); + + } + + } + + } + + } + + function getRenderTargetSamples( renderTarget ) { + + return Math.min( capabilities.maxSamples, renderTarget.samples ); + + } + + function useMultisampledRTT( renderTarget ) { + + const renderTargetProperties = properties.get( renderTarget ); + + return renderTarget.samples > 0 && extensions.has( 'WEBGL_multisampled_render_to_texture' ) === true && renderTargetProperties.__useRenderToTexture !== false; + + } + + function updateVideoTexture( texture ) { + + const frame = info.render.frame; + + // Check the last frame we updated the VideoTexture + + if ( _videoTextures.get( texture ) !== frame ) { + + _videoTextures.set( texture, frame ); + texture.update(); + + } + + } + + function verifyColorSpace( texture, image ) { + + const colorSpace = texture.colorSpace; + const format = texture.format; + const type = texture.type; + + if ( texture.isCompressedTexture === true || texture.isVideoTexture === true ) return image; + + if ( colorSpace !== LinearSRGBColorSpace && colorSpace !== NoColorSpace ) { + + // sRGB + + if ( ColorManagement.getTransfer( colorSpace ) === SRGBTransfer ) { + + // in WebGL 2 uncompressed textures can only be sRGB encoded if they have the RGBA8 format + + if ( format !== RGBAFormat || type !== UnsignedByteType ) { + + console.warn( 'THREE.WebGLTextures: sRGB encoded textures have to use RGBAFormat and UnsignedByteType.' ); + + } + + } else { + + console.error( 'THREE.WebGLTextures: Unsupported texture color space:', colorSpace ); + + } + + } + + return image; + + } + + function getDimensions( image ) { + + if ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) { + + // if intrinsic data are not available, fallback to width/height + + _imageDimensions.width = image.naturalWidth || image.width; + _imageDimensions.height = image.naturalHeight || image.height; + + } else if ( typeof VideoFrame !== 'undefined' && image instanceof VideoFrame ) { + + _imageDimensions.width = image.displayWidth; + _imageDimensions.height = image.displayHeight; + + } else { + + _imageDimensions.width = image.width; + _imageDimensions.height = image.height; + + } + + return _imageDimensions; + + } + + // + + this.allocateTextureUnit = allocateTextureUnit; + this.resetTextureUnits = resetTextureUnits; + + this.setTexture2D = setTexture2D; + this.setTexture2DArray = setTexture2DArray; + this.setTexture3D = setTexture3D; + this.setTextureCube = setTextureCube; + this.rebindTextures = rebindTextures; + this.setupRenderTarget = setupRenderTarget; + this.updateRenderTargetMipmap = updateRenderTargetMipmap; + this.updateMultisampleRenderTarget = updateMultisampleRenderTarget; + this.setupDepthRenderbuffer = setupDepthRenderbuffer; + this.setupFrameBufferTexture = setupFrameBufferTexture; + this.useMultisampledRTT = useMultisampledRTT; + +} + +function WebGLUtils( gl, extensions ) { + + function convert( p, colorSpace = NoColorSpace ) { + + let extension; + + const transfer = ColorManagement.getTransfer( colorSpace ); + + if ( p === UnsignedByteType ) return gl.UNSIGNED_BYTE; + if ( p === UnsignedShort4444Type ) return gl.UNSIGNED_SHORT_4_4_4_4; + if ( p === UnsignedShort5551Type ) return gl.UNSIGNED_SHORT_5_5_5_1; + if ( p === UnsignedInt5999Type ) return gl.UNSIGNED_INT_5_9_9_9_REV; + + if ( p === ByteType ) return gl.BYTE; + if ( p === ShortType ) return gl.SHORT; + if ( p === UnsignedShortType ) return gl.UNSIGNED_SHORT; + if ( p === IntType ) return gl.INT; + if ( p === UnsignedIntType ) return gl.UNSIGNED_INT; + if ( p === FloatType ) return gl.FLOAT; + if ( p === HalfFloatType ) return gl.HALF_FLOAT; + + if ( p === AlphaFormat ) return gl.ALPHA; + if ( p === RGBFormat ) return gl.RGB; + if ( p === RGBAFormat ) return gl.RGBA; + if ( p === LuminanceFormat ) return gl.LUMINANCE; + if ( p === LuminanceAlphaFormat ) return gl.LUMINANCE_ALPHA; + if ( p === DepthFormat ) return gl.DEPTH_COMPONENT; + if ( p === DepthStencilFormat ) return gl.DEPTH_STENCIL; + + // WebGL2 formats. + + if ( p === RedFormat ) return gl.RED; + if ( p === RedIntegerFormat ) return gl.RED_INTEGER; + if ( p === RGFormat ) return gl.RG; + if ( p === RGIntegerFormat ) return gl.RG_INTEGER; + if ( p === RGBAIntegerFormat ) return gl.RGBA_INTEGER; + + // S3TC + + if ( p === RGB_S3TC_DXT1_Format || p === RGBA_S3TC_DXT1_Format || p === RGBA_S3TC_DXT3_Format || p === RGBA_S3TC_DXT5_Format ) { + + if ( transfer === SRGBTransfer ) { + + extension = extensions.get( 'WEBGL_compressed_texture_s3tc_srgb' ); + + if ( extension !== null ) { + + if ( p === RGB_S3TC_DXT1_Format ) return extension.COMPRESSED_SRGB_S3TC_DXT1_EXT; + if ( p === RGBA_S3TC_DXT1_Format ) return extension.COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT; + if ( p === RGBA_S3TC_DXT3_Format ) return extension.COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT; + if ( p === RGBA_S3TC_DXT5_Format ) return extension.COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT; + + } else { + + return null; + + } + + } else { + + extension = extensions.get( 'WEBGL_compressed_texture_s3tc' ); + + if ( extension !== null ) { + + if ( p === RGB_S3TC_DXT1_Format ) return extension.COMPRESSED_RGB_S3TC_DXT1_EXT; + if ( p === RGBA_S3TC_DXT1_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT1_EXT; + if ( p === RGBA_S3TC_DXT3_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT3_EXT; + if ( p === RGBA_S3TC_DXT5_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT5_EXT; + + } else { + + return null; + + } + + } + + } + + // PVRTC + + if ( p === RGB_PVRTC_4BPPV1_Format || p === RGB_PVRTC_2BPPV1_Format || p === RGBA_PVRTC_4BPPV1_Format || p === RGBA_PVRTC_2BPPV1_Format ) { + + extension = extensions.get( 'WEBGL_compressed_texture_pvrtc' ); + + if ( extension !== null ) { + + if ( p === RGB_PVRTC_4BPPV1_Format ) return extension.COMPRESSED_RGB_PVRTC_4BPPV1_IMG; + if ( p === RGB_PVRTC_2BPPV1_Format ) return extension.COMPRESSED_RGB_PVRTC_2BPPV1_IMG; + if ( p === RGBA_PVRTC_4BPPV1_Format ) return extension.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG; + if ( p === RGBA_PVRTC_2BPPV1_Format ) return extension.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG; + + } else { + + return null; + + } + + } + + // ETC + + if ( p === RGB_ETC1_Format || p === RGB_ETC2_Format || p === RGBA_ETC2_EAC_Format ) { + + extension = extensions.get( 'WEBGL_compressed_texture_etc' ); + + if ( extension !== null ) { + + if ( p === RGB_ETC1_Format || p === RGB_ETC2_Format ) return ( transfer === SRGBTransfer ) ? extension.COMPRESSED_SRGB8_ETC2 : extension.COMPRESSED_RGB8_ETC2; + if ( p === RGBA_ETC2_EAC_Format ) return ( transfer === SRGBTransfer ) ? extension.COMPRESSED_SRGB8_ALPHA8_ETC2_EAC : extension.COMPRESSED_RGBA8_ETC2_EAC; + + } else { + + return null; + + } + + } + + // ASTC + + if ( p === RGBA_ASTC_4x4_Format || p === RGBA_ASTC_5x4_Format || p === RGBA_ASTC_5x5_Format || + p === RGBA_ASTC_6x5_Format || p === RGBA_ASTC_6x6_Format || p === RGBA_ASTC_8x5_Format || + p === RGBA_ASTC_8x6_Format || p === RGBA_ASTC_8x8_Format || p === RGBA_ASTC_10x5_Format || + p === RGBA_ASTC_10x6_Format || p === RGBA_ASTC_10x8_Format || p === RGBA_ASTC_10x10_Format || + p === RGBA_ASTC_12x10_Format || p === RGBA_ASTC_12x12_Format ) { + + extension = extensions.get( 'WEBGL_compressed_texture_astc' ); + + if ( extension !== null ) { + + if ( p === RGBA_ASTC_4x4_Format ) return ( transfer === SRGBTransfer ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR : extension.COMPRESSED_RGBA_ASTC_4x4_KHR; + if ( p === RGBA_ASTC_5x4_Format ) return ( transfer === SRGBTransfer ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR : extension.COMPRESSED_RGBA_ASTC_5x4_KHR; + if ( p === RGBA_ASTC_5x5_Format ) return ( transfer === SRGBTransfer ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR : extension.COMPRESSED_RGBA_ASTC_5x5_KHR; + if ( p === RGBA_ASTC_6x5_Format ) return ( transfer === SRGBTransfer ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR : extension.COMPRESSED_RGBA_ASTC_6x5_KHR; + if ( p === RGBA_ASTC_6x6_Format ) return ( transfer === SRGBTransfer ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR : extension.COMPRESSED_RGBA_ASTC_6x6_KHR; + if ( p === RGBA_ASTC_8x5_Format ) return ( transfer === SRGBTransfer ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR : extension.COMPRESSED_RGBA_ASTC_8x5_KHR; + if ( p === RGBA_ASTC_8x6_Format ) return ( transfer === SRGBTransfer ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR : extension.COMPRESSED_RGBA_ASTC_8x6_KHR; + if ( p === RGBA_ASTC_8x8_Format ) return ( transfer === SRGBTransfer ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR : extension.COMPRESSED_RGBA_ASTC_8x8_KHR; + if ( p === RGBA_ASTC_10x5_Format ) return ( transfer === SRGBTransfer ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR : extension.COMPRESSED_RGBA_ASTC_10x5_KHR; + if ( p === RGBA_ASTC_10x6_Format ) return ( transfer === SRGBTransfer ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR : extension.COMPRESSED_RGBA_ASTC_10x6_KHR; + if ( p === RGBA_ASTC_10x8_Format ) return ( transfer === SRGBTransfer ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR : extension.COMPRESSED_RGBA_ASTC_10x8_KHR; + if ( p === RGBA_ASTC_10x10_Format ) return ( transfer === SRGBTransfer ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR : extension.COMPRESSED_RGBA_ASTC_10x10_KHR; + if ( p === RGBA_ASTC_12x10_Format ) return ( transfer === SRGBTransfer ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR : extension.COMPRESSED_RGBA_ASTC_12x10_KHR; + if ( p === RGBA_ASTC_12x12_Format ) return ( transfer === SRGBTransfer ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR : extension.COMPRESSED_RGBA_ASTC_12x12_KHR; + + } else { + + return null; + + } + + } + + // BPTC + + if ( p === RGBA_BPTC_Format || p === RGB_BPTC_SIGNED_Format || p === RGB_BPTC_UNSIGNED_Format ) { + + extension = extensions.get( 'EXT_texture_compression_bptc' ); + + if ( extension !== null ) { + + if ( p === RGBA_BPTC_Format ) return ( transfer === SRGBTransfer ) ? extension.COMPRESSED_SRGB_ALPHA_BPTC_UNORM_EXT : extension.COMPRESSED_RGBA_BPTC_UNORM_EXT; + if ( p === RGB_BPTC_SIGNED_Format ) return extension.COMPRESSED_RGB_BPTC_SIGNED_FLOAT_EXT; + if ( p === RGB_BPTC_UNSIGNED_Format ) return extension.COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_EXT; + + } else { + + return null; + + } + + } + + // RGTC + + if ( p === RED_RGTC1_Format || p === SIGNED_RED_RGTC1_Format || p === RED_GREEN_RGTC2_Format || p === SIGNED_RED_GREEN_RGTC2_Format ) { + + extension = extensions.get( 'EXT_texture_compression_rgtc' ); + + if ( extension !== null ) { + + if ( p === RGBA_BPTC_Format ) return extension.COMPRESSED_RED_RGTC1_EXT; + if ( p === SIGNED_RED_RGTC1_Format ) return extension.COMPRESSED_SIGNED_RED_RGTC1_EXT; + if ( p === RED_GREEN_RGTC2_Format ) return extension.COMPRESSED_RED_GREEN_RGTC2_EXT; + if ( p === SIGNED_RED_GREEN_RGTC2_Format ) return extension.COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT; + + } else { + + return null; + + } + + } + + // + + if ( p === UnsignedInt248Type ) return gl.UNSIGNED_INT_24_8; + + // if "p" can't be resolved, assume the user defines a WebGL constant as a string (fallback/workaround for packed RGB formats) + + return ( gl[ p ] !== undefined ) ? gl[ p ] : null; + + } + + return { convert: convert }; + +} + +class ArrayCamera extends PerspectiveCamera { + + constructor( array = [] ) { + + super(); + + this.isArrayCamera = true; + + this.cameras = array; + + } + +} + +class Group extends Object3D { + + constructor() { + + super(); + + this.isGroup = true; + + this.type = 'Group'; + + } + +} + +const _moveEvent = { type: 'move' }; + +class WebXRController { + + constructor() { + + this._targetRay = null; + this._grip = null; + this._hand = null; + + } + + getHandSpace() { + + if ( this._hand === null ) { + + this._hand = new Group(); + this._hand.matrixAutoUpdate = false; + this._hand.visible = false; + + this._hand.joints = {}; + this._hand.inputState = { pinching: false }; + + } + + return this._hand; + + } + + getTargetRaySpace() { + + if ( this._targetRay === null ) { + + this._targetRay = new Group(); + this._targetRay.matrixAutoUpdate = false; + this._targetRay.visible = false; + this._targetRay.hasLinearVelocity = false; + this._targetRay.linearVelocity = new Vector3(); + this._targetRay.hasAngularVelocity = false; + this._targetRay.angularVelocity = new Vector3(); + + } + + return this._targetRay; + + } + + getGripSpace() { + + if ( this._grip === null ) { + + this._grip = new Group(); + this._grip.matrixAutoUpdate = false; + this._grip.visible = false; + this._grip.hasLinearVelocity = false; + this._grip.linearVelocity = new Vector3(); + this._grip.hasAngularVelocity = false; + this._grip.angularVelocity = new Vector3(); + + } + + return this._grip; + + } + + dispatchEvent( event ) { + + if ( this._targetRay !== null ) { + + this._targetRay.dispatchEvent( event ); + + } + + if ( this._grip !== null ) { + + this._grip.dispatchEvent( event ); + + } + + if ( this._hand !== null ) { + + this._hand.dispatchEvent( event ); + + } + + return this; + + } + + connect( inputSource ) { + + if ( inputSource && inputSource.hand ) { + + const hand = this._hand; + + if ( hand ) { + + for ( const inputjoint of inputSource.hand.values() ) { + + // Initialize hand with joints when connected + this._getHandJoint( hand, inputjoint ); + + } + + } + + } + + this.dispatchEvent( { type: 'connected', data: inputSource } ); + + return this; + + } + + disconnect( inputSource ) { + + this.dispatchEvent( { type: 'disconnected', data: inputSource } ); + + if ( this._targetRay !== null ) { + + this._targetRay.visible = false; + + } + + if ( this._grip !== null ) { + + this._grip.visible = false; + + } + + if ( this._hand !== null ) { + + this._hand.visible = false; + + } + + return this; + + } + + update( inputSource, frame, referenceSpace ) { + + let inputPose = null; + let gripPose = null; + let handPose = null; + + const targetRay = this._targetRay; + const grip = this._grip; + const hand = this._hand; + + if ( inputSource && frame.session.visibilityState !== 'visible-blurred' ) { + + if ( hand && inputSource.hand ) { + + handPose = true; + + for ( const inputjoint of inputSource.hand.values() ) { + + // Update the joints groups with the XRJoint poses + const jointPose = frame.getJointPose( inputjoint, referenceSpace ); + + // The transform of this joint will be updated with the joint pose on each frame + const joint = this._getHandJoint( hand, inputjoint ); + + if ( jointPose !== null ) { + + joint.matrix.fromArray( jointPose.transform.matrix ); + joint.matrix.decompose( joint.position, joint.rotation, joint.scale ); + joint.matrixWorldNeedsUpdate = true; + joint.jointRadius = jointPose.radius; + + } + + joint.visible = jointPose !== null; + + } + + // Custom events + + // Check pinchz + const indexTip = hand.joints[ 'index-finger-tip' ]; + const thumbTip = hand.joints[ 'thumb-tip' ]; + const distance = indexTip.position.distanceTo( thumbTip.position ); + + const distanceToPinch = 0.02; + const threshold = 0.005; + + if ( hand.inputState.pinching && distance > distanceToPinch + threshold ) { + + hand.inputState.pinching = false; + this.dispatchEvent( { + type: 'pinchend', + handedness: inputSource.handedness, + target: this + } ); + + } else if ( ! hand.inputState.pinching && distance <= distanceToPinch - threshold ) { + + hand.inputState.pinching = true; + this.dispatchEvent( { + type: 'pinchstart', + handedness: inputSource.handedness, + target: this + } ); + + } + + } else { + + if ( grip !== null && inputSource.gripSpace ) { + + gripPose = frame.getPose( inputSource.gripSpace, referenceSpace ); + + if ( gripPose !== null ) { + + grip.matrix.fromArray( gripPose.transform.matrix ); + grip.matrix.decompose( grip.position, grip.rotation, grip.scale ); + grip.matrixWorldNeedsUpdate = true; + + if ( gripPose.linearVelocity ) { + + grip.hasLinearVelocity = true; + grip.linearVelocity.copy( gripPose.linearVelocity ); + + } else { + + grip.hasLinearVelocity = false; + + } + + if ( gripPose.angularVelocity ) { + + grip.hasAngularVelocity = true; + grip.angularVelocity.copy( gripPose.angularVelocity ); + + } else { + + grip.hasAngularVelocity = false; + + } + + } + + } + + } + + if ( targetRay !== null ) { + + inputPose = frame.getPose( inputSource.targetRaySpace, referenceSpace ); + + // Some runtimes (namely Vive Cosmos with Vive OpenXR Runtime) have only grip space and ray space is equal to it + if ( inputPose === null && gripPose !== null ) { + + inputPose = gripPose; + + } + + if ( inputPose !== null ) { + + targetRay.matrix.fromArray( inputPose.transform.matrix ); + targetRay.matrix.decompose( targetRay.position, targetRay.rotation, targetRay.scale ); + targetRay.matrixWorldNeedsUpdate = true; + + if ( inputPose.linearVelocity ) { + + targetRay.hasLinearVelocity = true; + targetRay.linearVelocity.copy( inputPose.linearVelocity ); + + } else { + + targetRay.hasLinearVelocity = false; + + } + + if ( inputPose.angularVelocity ) { + + targetRay.hasAngularVelocity = true; + targetRay.angularVelocity.copy( inputPose.angularVelocity ); + + } else { + + targetRay.hasAngularVelocity = false; + + } + + this.dispatchEvent( _moveEvent ); + + } + + } + + + } + + if ( targetRay !== null ) { + + targetRay.visible = ( inputPose !== null ); + + } + + if ( grip !== null ) { + + grip.visible = ( gripPose !== null ); + + } + + if ( hand !== null ) { + + hand.visible = ( handPose !== null ); + + } + + return this; + + } + + // private method + + _getHandJoint( hand, inputjoint ) { + + if ( hand.joints[ inputjoint.jointName ] === undefined ) { + + const joint = new Group(); + joint.matrixAutoUpdate = false; + joint.visible = false; + hand.joints[ inputjoint.jointName ] = joint; + + hand.add( joint ); + + } + + return hand.joints[ inputjoint.jointName ]; + + } + +} + +const _occlusion_vertex = ` +void main() { + + gl_Position = vec4( position, 1.0 ); + +}`; + +const _occlusion_fragment = ` +uniform sampler2DArray depthColor; +uniform float depthWidth; +uniform float depthHeight; + +void main() { + + vec2 coord = vec2( gl_FragCoord.x / depthWidth, gl_FragCoord.y / depthHeight ); + + if ( coord.x >= 1.0 ) { + + gl_FragDepth = texture( depthColor, vec3( coord.x - 1.0, coord.y, 1 ) ).r; + + } else { + + gl_FragDepth = texture( depthColor, vec3( coord.x, coord.y, 0 ) ).r; + + } + +}`; + +class WebXRDepthSensing { + + constructor() { + + this.texture = null; + this.mesh = null; + + this.depthNear = 0; + this.depthFar = 0; + + } + + init( renderer, depthData, renderState ) { + + if ( this.texture === null ) { + + const texture = new Texture(); + + const texProps = renderer.properties.get( texture ); + texProps.__webglTexture = depthData.texture; + + if ( ( depthData.depthNear != renderState.depthNear ) || ( depthData.depthFar != renderState.depthFar ) ) { + + this.depthNear = depthData.depthNear; + this.depthFar = depthData.depthFar; + + } + + this.texture = texture; + + } + + } + + render( renderer, cameraXR ) { + + if ( this.texture !== null ) { + + if ( this.mesh === null ) { + + const viewport = cameraXR.cameras[ 0 ].viewport; + const material = new ShaderMaterial( { + vertexShader: _occlusion_vertex, + fragmentShader: _occlusion_fragment, + uniforms: { + depthColor: { value: this.texture }, + depthWidth: { value: viewport.z }, + depthHeight: { value: viewport.w } + } + } ); + + this.mesh = new Mesh( new PlaneGeometry( 20, 20 ), material ); + + } + + renderer.render( this.mesh, cameraXR ); + + } + + } + + reset() { + + this.texture = null; + this.mesh = null; + + } + +} + +class WebXRManager extends EventDispatcher { + + constructor( renderer, gl ) { + + super(); + + const scope = this; + + let session = null; + + let framebufferScaleFactor = 1.0; + + let referenceSpace = null; + let referenceSpaceType = 'local-floor'; + // Set default foveation to maximum. + let foveation = 1.0; + let customReferenceSpace = null; + + let pose = null; + let glBinding = null; + let glProjLayer = null; + let glBaseLayer = null; + let xrFrame = null; + + const depthSensing = new WebXRDepthSensing(); + const attributes = gl.getContextAttributes(); + + let initialRenderTarget = null; + let newRenderTarget = null; + + const controllers = []; + const controllerInputSources = []; + + const currentSize = new Vector2(); + let currentPixelRatio = null; + + // + + const cameraL = new PerspectiveCamera(); + cameraL.layers.enable( 1 ); + cameraL.viewport = new Vector4(); + + const cameraR = new PerspectiveCamera(); + cameraR.layers.enable( 2 ); + cameraR.viewport = new Vector4(); + + const cameras = [ cameraL, cameraR ]; + + const cameraXR = new ArrayCamera(); + cameraXR.layers.enable( 1 ); + cameraXR.layers.enable( 2 ); + + let _currentDepthNear = null; + let _currentDepthFar = null; + + // + + this.cameraAutoUpdate = true; + this.enabled = false; + + this.isPresenting = false; + + this.getController = function ( index ) { + + let controller = controllers[ index ]; + + if ( controller === undefined ) { + + controller = new WebXRController(); + controllers[ index ] = controller; + + } + + return controller.getTargetRaySpace(); + + }; + + this.getControllerGrip = function ( index ) { + + let controller = controllers[ index ]; + + if ( controller === undefined ) { + + controller = new WebXRController(); + controllers[ index ] = controller; + + } + + return controller.getGripSpace(); + + }; + + this.getHand = function ( index ) { + + let controller = controllers[ index ]; + + if ( controller === undefined ) { + + controller = new WebXRController(); + controllers[ index ] = controller; + + } + + return controller.getHandSpace(); + + }; + + // + + function onSessionEvent( event ) { + + const controllerIndex = controllerInputSources.indexOf( event.inputSource ); + + if ( controllerIndex === - 1 ) { + + return; + + } + + const controller = controllers[ controllerIndex ]; + + if ( controller !== undefined ) { + + controller.update( event.inputSource, event.frame, customReferenceSpace || referenceSpace ); + controller.dispatchEvent( { type: event.type, data: event.inputSource } ); + + } + + } + + function onSessionEnd() { + + session.removeEventListener( 'select', onSessionEvent ); + session.removeEventListener( 'selectstart', onSessionEvent ); + session.removeEventListener( 'selectend', onSessionEvent ); + session.removeEventListener( 'squeeze', onSessionEvent ); + session.removeEventListener( 'squeezestart', onSessionEvent ); + session.removeEventListener( 'squeezeend', onSessionEvent ); + session.removeEventListener( 'end', onSessionEnd ); + session.removeEventListener( 'inputsourceschange', onInputSourcesChange ); + + for ( let i = 0; i < controllers.length; i ++ ) { + + const inputSource = controllerInputSources[ i ]; + + if ( inputSource === null ) continue; + + controllerInputSources[ i ] = null; + + controllers[ i ].disconnect( inputSource ); + + } + + _currentDepthNear = null; + _currentDepthFar = null; + + depthSensing.reset(); + + // restore framebuffer/rendering state + + renderer.setRenderTarget( initialRenderTarget ); + + glBaseLayer = null; + glProjLayer = null; + glBinding = null; + session = null; + newRenderTarget = null; + + // + + animation.stop(); + + scope.isPresenting = false; + + renderer.setPixelRatio( currentPixelRatio ); + renderer.setSize( currentSize.width, currentSize.height, false ); + + scope.dispatchEvent( { type: 'sessionend' } ); + + } + + this.setFramebufferScaleFactor = function ( value ) { + + framebufferScaleFactor = value; + + if ( scope.isPresenting === true ) { + + console.warn( 'THREE.WebXRManager: Cannot change framebuffer scale while presenting.' ); + + } + + }; + + this.setReferenceSpaceType = function ( value ) { + + referenceSpaceType = value; + + if ( scope.isPresenting === true ) { + + console.warn( 'THREE.WebXRManager: Cannot change reference space type while presenting.' ); + + } + + }; + + this.getReferenceSpace = function () { + + return customReferenceSpace || referenceSpace; + + }; + + this.setReferenceSpace = function ( space ) { + + customReferenceSpace = space; + + }; + + this.getBaseLayer = function () { + + return glProjLayer !== null ? glProjLayer : glBaseLayer; + + }; + + this.getBinding = function () { + + return glBinding; + + }; + + this.getFrame = function () { + + return xrFrame; + + }; + + this.getSession = function () { + + return session; + + }; + + this.setSession = async function ( value ) { + + session = value; + + if ( session !== null ) { + + initialRenderTarget = renderer.getRenderTarget(); + + session.addEventListener( 'select', onSessionEvent ); + session.addEventListener( 'selectstart', onSessionEvent ); + session.addEventListener( 'selectend', onSessionEvent ); + session.addEventListener( 'squeeze', onSessionEvent ); + session.addEventListener( 'squeezestart', onSessionEvent ); + session.addEventListener( 'squeezeend', onSessionEvent ); + session.addEventListener( 'end', onSessionEnd ); + session.addEventListener( 'inputsourceschange', onInputSourcesChange ); + + if ( attributes.xrCompatible !== true ) { + + await gl.makeXRCompatible(); + + } + + currentPixelRatio = renderer.getPixelRatio(); + renderer.getSize( currentSize ); + + if ( session.renderState.layers === undefined ) { + + const layerInit = { + antialias: attributes.antialias, + alpha: true, + depth: attributes.depth, + stencil: attributes.stencil, + framebufferScaleFactor: framebufferScaleFactor + }; + + glBaseLayer = new XRWebGLLayer( session, gl, layerInit ); + + session.updateRenderState( { baseLayer: glBaseLayer } ); + + renderer.setPixelRatio( 1 ); + renderer.setSize( glBaseLayer.framebufferWidth, glBaseLayer.framebufferHeight, false ); + + newRenderTarget = new WebGLRenderTarget( + glBaseLayer.framebufferWidth, + glBaseLayer.framebufferHeight, + { + format: RGBAFormat, + type: UnsignedByteType, + colorSpace: renderer.outputColorSpace, + stencilBuffer: attributes.stencil + } + ); + + } else { + + let depthFormat = null; + let depthType = null; + let glDepthFormat = null; + + if ( attributes.depth ) { + + glDepthFormat = attributes.stencil ? gl.DEPTH24_STENCIL8 : gl.DEPTH_COMPONENT24; + depthFormat = attributes.stencil ? DepthStencilFormat : DepthFormat; + depthType = attributes.stencil ? UnsignedInt248Type : UnsignedIntType; + + } + + const projectionlayerInit = { + colorFormat: gl.RGBA8, + depthFormat: glDepthFormat, + scaleFactor: framebufferScaleFactor + }; + + glBinding = new XRWebGLBinding( session, gl ); + + glProjLayer = glBinding.createProjectionLayer( projectionlayerInit ); + + session.updateRenderState( { layers: [ glProjLayer ] } ); + + renderer.setPixelRatio( 1 ); + renderer.setSize( glProjLayer.textureWidth, glProjLayer.textureHeight, false ); + + newRenderTarget = new WebGLRenderTarget( + glProjLayer.textureWidth, + glProjLayer.textureHeight, + { + format: RGBAFormat, + type: UnsignedByteType, + depthTexture: new DepthTexture( glProjLayer.textureWidth, glProjLayer.textureHeight, depthType, undefined, undefined, undefined, undefined, undefined, undefined, depthFormat ), + stencilBuffer: attributes.stencil, + colorSpace: renderer.outputColorSpace, + samples: attributes.antialias ? 4 : 0, + resolveDepthBuffer: ( glProjLayer.ignoreDepthValues === false ) + } ); + + } + + newRenderTarget.isXRRenderTarget = true; // TODO Remove this when possible, see #23278 + + this.setFoveation( foveation ); + + customReferenceSpace = null; + referenceSpace = await session.requestReferenceSpace( referenceSpaceType ); + + animation.setContext( session ); + animation.start(); + + scope.isPresenting = true; + + scope.dispatchEvent( { type: 'sessionstart' } ); + + } + + }; + + this.getEnvironmentBlendMode = function () { + + if ( session !== null ) { + + return session.environmentBlendMode; + + } + + }; + + function onInputSourcesChange( event ) { + + // Notify disconnected + + for ( let i = 0; i < event.removed.length; i ++ ) { + + const inputSource = event.removed[ i ]; + const index = controllerInputSources.indexOf( inputSource ); + + if ( index >= 0 ) { + + controllerInputSources[ index ] = null; + controllers[ index ].disconnect( inputSource ); + + } + + } + + // Notify connected + + for ( let i = 0; i < event.added.length; i ++ ) { + + const inputSource = event.added[ i ]; + + let controllerIndex = controllerInputSources.indexOf( inputSource ); + + if ( controllerIndex === - 1 ) { + + // Assign input source a controller that currently has no input source + + for ( let i = 0; i < controllers.length; i ++ ) { + + if ( i >= controllerInputSources.length ) { + + controllerInputSources.push( inputSource ); + controllerIndex = i; + break; + + } else if ( controllerInputSources[ i ] === null ) { + + controllerInputSources[ i ] = inputSource; + controllerIndex = i; + break; + + } + + } + + // If all controllers do currently receive input we ignore new ones + + if ( controllerIndex === - 1 ) break; + + } + + const controller = controllers[ controllerIndex ]; + + if ( controller ) { + + controller.connect( inputSource ); + + } + + } + + } + + // + + const cameraLPos = new Vector3(); + const cameraRPos = new Vector3(); + + /** + * Assumes 2 cameras that are parallel and share an X-axis, and that + * the cameras' projection and world matrices have already been set. + * And that near and far planes are identical for both cameras. + * Visualization of this technique: https://computergraphics.stackexchange.com/a/4765 + */ + function setProjectionFromUnion( camera, cameraL, cameraR ) { + + cameraLPos.setFromMatrixPosition( cameraL.matrixWorld ); + cameraRPos.setFromMatrixPosition( cameraR.matrixWorld ); + + const ipd = cameraLPos.distanceTo( cameraRPos ); + + const projL = cameraL.projectionMatrix.elements; + const projR = cameraR.projectionMatrix.elements; + + // VR systems will have identical far and near planes, and + // most likely identical top and bottom frustum extents. + // Use the left camera for these values. + const near = projL[ 14 ] / ( projL[ 10 ] - 1 ); + const far = projL[ 14 ] / ( projL[ 10 ] + 1 ); + const topFov = ( projL[ 9 ] + 1 ) / projL[ 5 ]; + const bottomFov = ( projL[ 9 ] - 1 ) / projL[ 5 ]; + + const leftFov = ( projL[ 8 ] - 1 ) / projL[ 0 ]; + const rightFov = ( projR[ 8 ] + 1 ) / projR[ 0 ]; + const left = near * leftFov; + const right = near * rightFov; + + // Calculate the new camera's position offset from the + // left camera. xOffset should be roughly half `ipd`. + const zOffset = ipd / ( - leftFov + rightFov ); + const xOffset = zOffset * - leftFov; + + // TODO: Better way to apply this offset? + cameraL.matrixWorld.decompose( camera.position, camera.quaternion, camera.scale ); + camera.translateX( xOffset ); + camera.translateZ( zOffset ); + camera.matrixWorld.compose( camera.position, camera.quaternion, camera.scale ); + camera.matrixWorldInverse.copy( camera.matrixWorld ).invert(); + + // Find the union of the frustum values of the cameras and scale + // the values so that the near plane's position does not change in world space, + // although must now be relative to the new union camera. + const near2 = near + zOffset; + const far2 = far + zOffset; + const left2 = left - xOffset; + const right2 = right + ( ipd - xOffset ); + const top2 = topFov * far / far2 * near2; + const bottom2 = bottomFov * far / far2 * near2; + + camera.projectionMatrix.makePerspective( left2, right2, top2, bottom2, near2, far2 ); + camera.projectionMatrixInverse.copy( camera.projectionMatrix ).invert(); + + } + + function updateCamera( camera, parent ) { + + if ( parent === null ) { + + camera.matrixWorld.copy( camera.matrix ); + + } else { + + camera.matrixWorld.multiplyMatrices( parent.matrixWorld, camera.matrix ); + + } + + camera.matrixWorldInverse.copy( camera.matrixWorld ).invert(); + + } + + this.updateCamera = function ( camera ) { + + if ( session === null ) return; + + if ( depthSensing.texture !== null ) { + + camera.near = depthSensing.depthNear; + camera.far = depthSensing.depthFar; + + } + + cameraXR.near = cameraR.near = cameraL.near = camera.near; + cameraXR.far = cameraR.far = cameraL.far = camera.far; + + if ( _currentDepthNear !== cameraXR.near || _currentDepthFar !== cameraXR.far ) { + + // Note that the new renderState won't apply until the next frame. See #18320 + + session.updateRenderState( { + depthNear: cameraXR.near, + depthFar: cameraXR.far + } ); + + _currentDepthNear = cameraXR.near; + _currentDepthFar = cameraXR.far; + + cameraL.near = _currentDepthNear; + cameraL.far = _currentDepthFar; + cameraR.near = _currentDepthNear; + cameraR.far = _currentDepthFar; + + cameraL.updateProjectionMatrix(); + cameraR.updateProjectionMatrix(); + camera.updateProjectionMatrix(); + + } + + const parent = camera.parent; + const cameras = cameraXR.cameras; + + updateCamera( cameraXR, parent ); + + for ( let i = 0; i < cameras.length; i ++ ) { + + updateCamera( cameras[ i ], parent ); + + } + + // update projection matrix for proper view frustum culling + + if ( cameras.length === 2 ) { + + setProjectionFromUnion( cameraXR, cameraL, cameraR ); + + } else { + + // assume single camera setup (AR) + + cameraXR.projectionMatrix.copy( cameraL.projectionMatrix ); + + } + + // update user camera and its children + + updateUserCamera( camera, cameraXR, parent ); + + }; + + function updateUserCamera( camera, cameraXR, parent ) { + + if ( parent === null ) { + + camera.matrix.copy( cameraXR.matrixWorld ); + + } else { + + camera.matrix.copy( parent.matrixWorld ); + camera.matrix.invert(); + camera.matrix.multiply( cameraXR.matrixWorld ); + + } + + camera.matrix.decompose( camera.position, camera.quaternion, camera.scale ); + camera.updateMatrixWorld( true ); + + camera.projectionMatrix.copy( cameraXR.projectionMatrix ); + camera.projectionMatrixInverse.copy( cameraXR.projectionMatrixInverse ); + + if ( camera.isPerspectiveCamera ) { + + camera.fov = RAD2DEG * 2 * Math.atan( 1 / camera.projectionMatrix.elements[ 5 ] ); + camera.zoom = 1; + + } + + } + + this.getCamera = function () { + + return cameraXR; + + }; + + this.getFoveation = function () { + + if ( glProjLayer === null && glBaseLayer === null ) { + + return undefined; + + } + + return foveation; + + }; + + this.setFoveation = function ( value ) { + + // 0 = no foveation = full resolution + // 1 = maximum foveation = the edges render at lower resolution + + foveation = value; + + if ( glProjLayer !== null ) { + + glProjLayer.fixedFoveation = value; + + } + + if ( glBaseLayer !== null && glBaseLayer.fixedFoveation !== undefined ) { + + glBaseLayer.fixedFoveation = value; + + } + + }; + + this.hasDepthSensing = function () { + + return depthSensing.texture !== null; + + }; + + // Animation Loop + + let onAnimationFrameCallback = null; + + function onAnimationFrame( time, frame ) { + + pose = frame.getViewerPose( customReferenceSpace || referenceSpace ); + xrFrame = frame; + + if ( pose !== null ) { + + const views = pose.views; + + if ( glBaseLayer !== null ) { + + renderer.setRenderTargetFramebuffer( newRenderTarget, glBaseLayer.framebuffer ); + renderer.setRenderTarget( newRenderTarget ); + + } + + let cameraXRNeedsUpdate = false; + + // check if it's necessary to rebuild cameraXR's camera list + + if ( views.length !== cameraXR.cameras.length ) { + + cameraXR.cameras.length = 0; + cameraXRNeedsUpdate = true; + + } + + for ( let i = 0; i < views.length; i ++ ) { + + const view = views[ i ]; + + let viewport = null; + + if ( glBaseLayer !== null ) { + + viewport = glBaseLayer.getViewport( view ); + + } else { + + const glSubImage = glBinding.getViewSubImage( glProjLayer, view ); + viewport = glSubImage.viewport; + + // For side-by-side projection, we only produce a single texture for both eyes. + if ( i === 0 ) { + + renderer.setRenderTargetTextures( + newRenderTarget, + glSubImage.colorTexture, + glProjLayer.ignoreDepthValues ? undefined : glSubImage.depthStencilTexture ); + + renderer.setRenderTarget( newRenderTarget ); + + } + + } + + let camera = cameras[ i ]; + + if ( camera === undefined ) { + + camera = new PerspectiveCamera(); + camera.layers.enable( i ); + camera.viewport = new Vector4(); + cameras[ i ] = camera; + + } + + camera.matrix.fromArray( view.transform.matrix ); + camera.matrix.decompose( camera.position, camera.quaternion, camera.scale ); + camera.projectionMatrix.fromArray( view.projectionMatrix ); + camera.projectionMatrixInverse.copy( camera.projectionMatrix ).invert(); + camera.viewport.set( viewport.x, viewport.y, viewport.width, viewport.height ); + + if ( i === 0 ) { + + cameraXR.matrix.copy( camera.matrix ); + cameraXR.matrix.decompose( cameraXR.position, cameraXR.quaternion, cameraXR.scale ); + + } + + if ( cameraXRNeedsUpdate === true ) { + + cameraXR.cameras.push( camera ); + + } + + } + + // + + const enabledFeatures = session.enabledFeatures; + + if ( enabledFeatures && enabledFeatures.includes( 'depth-sensing' ) ) { + + const depthData = glBinding.getDepthInformation( views[ 0 ] ); + + if ( depthData && depthData.isValid && depthData.texture ) { + + depthSensing.init( renderer, depthData, session.renderState ); + + } + + } + + } + + // + + for ( let i = 0; i < controllers.length; i ++ ) { + + const inputSource = controllerInputSources[ i ]; + const controller = controllers[ i ]; + + if ( inputSource !== null && controller !== undefined ) { + + controller.update( inputSource, frame, customReferenceSpace || referenceSpace ); + + } + + } + + depthSensing.render( renderer, cameraXR ); + + if ( onAnimationFrameCallback ) onAnimationFrameCallback( time, frame ); + + if ( frame.detectedPlanes ) { + + scope.dispatchEvent( { type: 'planesdetected', data: frame } ); + + } + + xrFrame = null; + + } + + const animation = new WebGLAnimation(); + + animation.setAnimationLoop( onAnimationFrame ); + + this.setAnimationLoop = function ( callback ) { + + onAnimationFrameCallback = callback; + + }; + + this.dispose = function () {}; + + } + +} + +const _e1 = /*@__PURE__*/ new Euler(); +const _m1 = /*@__PURE__*/ new Matrix4(); + +function WebGLMaterials( renderer, properties ) { + + function refreshTransformUniform( map, uniform ) { + + if ( map.matrixAutoUpdate === true ) { + + map.updateMatrix(); + + } + + uniform.value.copy( map.matrix ); + + } + + function refreshFogUniforms( uniforms, fog ) { + + fog.color.getRGB( uniforms.fogColor.value, getUnlitUniformColorSpace( renderer ) ); + + if ( fog.isFog ) { + + uniforms.fogNear.value = fog.near; + uniforms.fogFar.value = fog.far; + + } else if ( fog.isFogExp2 ) { + + uniforms.fogDensity.value = fog.density; + + } + + } + + function refreshMaterialUniforms( uniforms, material, pixelRatio, height, transmissionRenderTarget ) { + + if ( material.isMeshBasicMaterial ) { + + refreshUniformsCommon( uniforms, material ); + + } else if ( material.isMeshLambertMaterial ) { + + refreshUniformsCommon( uniforms, material ); + + } else if ( material.isMeshToonMaterial ) { + + refreshUniformsCommon( uniforms, material ); + refreshUniformsToon( uniforms, material ); + + } else if ( material.isMeshPhongMaterial ) { + + refreshUniformsCommon( uniforms, material ); + refreshUniformsPhong( uniforms, material ); + + } else if ( material.isMeshStandardMaterial ) { + + refreshUniformsCommon( uniforms, material ); + refreshUniformsStandard( uniforms, material ); + + if ( material.isMeshPhysicalMaterial ) { + + refreshUniformsPhysical( uniforms, material, transmissionRenderTarget ); + + } + + } else if ( material.isMeshMatcapMaterial ) { + + refreshUniformsCommon( uniforms, material ); + refreshUniformsMatcap( uniforms, material ); + + } else if ( material.isMeshDepthMaterial ) { + + refreshUniformsCommon( uniforms, material ); + + } else if ( material.isMeshDistanceMaterial ) { + + refreshUniformsCommon( uniforms, material ); + refreshUniformsDistance( uniforms, material ); + + } else if ( material.isMeshNormalMaterial ) { + + refreshUniformsCommon( uniforms, material ); + + } else if ( material.isLineBasicMaterial ) { + + refreshUniformsLine( uniforms, material ); + + if ( material.isLineDashedMaterial ) { + + refreshUniformsDash( uniforms, material ); + + } + + } else if ( material.isPointsMaterial ) { + + refreshUniformsPoints( uniforms, material, pixelRatio, height ); + + } else if ( material.isSpriteMaterial ) { + + refreshUniformsSprites( uniforms, material ); + + } else if ( material.isShadowMaterial ) { + + uniforms.color.value.copy( material.color ); + uniforms.opacity.value = material.opacity; + + } else if ( material.isShaderMaterial ) { + + material.uniformsNeedUpdate = false; // #15581 + + } + + } + + function refreshUniformsCommon( uniforms, material ) { + + uniforms.opacity.value = material.opacity; + + if ( material.color ) { + + uniforms.diffuse.value.copy( material.color ); + + } + + if ( material.emissive ) { + + uniforms.emissive.value.copy( material.emissive ).multiplyScalar( material.emissiveIntensity ); + + } + + if ( material.map ) { + + uniforms.map.value = material.map; + + refreshTransformUniform( material.map, uniforms.mapTransform ); + + } + + if ( material.alphaMap ) { + + uniforms.alphaMap.value = material.alphaMap; + + refreshTransformUniform( material.alphaMap, uniforms.alphaMapTransform ); + + } + + if ( material.bumpMap ) { + + uniforms.bumpMap.value = material.bumpMap; + + refreshTransformUniform( material.bumpMap, uniforms.bumpMapTransform ); + + uniforms.bumpScale.value = material.bumpScale; + + if ( material.side === BackSide ) { + + uniforms.bumpScale.value *= - 1; + + } + + } + + if ( material.normalMap ) { + + uniforms.normalMap.value = material.normalMap; + + refreshTransformUniform( material.normalMap, uniforms.normalMapTransform ); + + uniforms.normalScale.value.copy( material.normalScale ); + + if ( material.side === BackSide ) { + + uniforms.normalScale.value.negate(); + + } + + } + + if ( material.displacementMap ) { + + uniforms.displacementMap.value = material.displacementMap; + + refreshTransformUniform( material.displacementMap, uniforms.displacementMapTransform ); + + uniforms.displacementScale.value = material.displacementScale; + uniforms.displacementBias.value = material.displacementBias; + + } + + if ( material.emissiveMap ) { + + uniforms.emissiveMap.value = material.emissiveMap; + + refreshTransformUniform( material.emissiveMap, uniforms.emissiveMapTransform ); + + } + + if ( material.specularMap ) { + + uniforms.specularMap.value = material.specularMap; + + refreshTransformUniform( material.specularMap, uniforms.specularMapTransform ); + + } + + if ( material.alphaTest > 0 ) { + + uniforms.alphaTest.value = material.alphaTest; + + } + + const materialProperties = properties.get( material ); + + const envMap = materialProperties.envMap; + const envMapRotation = materialProperties.envMapRotation; + + if ( envMap ) { + + uniforms.envMap.value = envMap; + + _e1.copy( envMapRotation ); + + // accommodate left-handed frame + _e1.x *= - 1; _e1.y *= - 1; _e1.z *= - 1; + + if ( envMap.isCubeTexture && envMap.isRenderTargetTexture === false ) { + + // environment maps which are not cube render targets or PMREMs follow a different convention + _e1.y *= - 1; + _e1.z *= - 1; + + } + + uniforms.envMapRotation.value.setFromMatrix4( _m1.makeRotationFromEuler( _e1 ) ); + + uniforms.flipEnvMap.value = ( envMap.isCubeTexture && envMap.isRenderTargetTexture === false ) ? - 1 : 1; + + uniforms.reflectivity.value = material.reflectivity; + uniforms.ior.value = material.ior; + uniforms.refractionRatio.value = material.refractionRatio; + + } + + if ( material.lightMap ) { + + uniforms.lightMap.value = material.lightMap; + + // artist-friendly light intensity scaling factor + const scaleFactor = ( renderer._useLegacyLights === true ) ? Math.PI : 1; + + uniforms.lightMapIntensity.value = material.lightMapIntensity * scaleFactor; + + refreshTransformUniform( material.lightMap, uniforms.lightMapTransform ); + + } + + if ( material.aoMap ) { + + uniforms.aoMap.value = material.aoMap; + uniforms.aoMapIntensity.value = material.aoMapIntensity; + + refreshTransformUniform( material.aoMap, uniforms.aoMapTransform ); + + } + + } + + function refreshUniformsLine( uniforms, material ) { + + uniforms.diffuse.value.copy( material.color ); + uniforms.opacity.value = material.opacity; + + if ( material.map ) { + + uniforms.map.value = material.map; + + refreshTransformUniform( material.map, uniforms.mapTransform ); + + } + + } + + function refreshUniformsDash( uniforms, material ) { + + uniforms.dashSize.value = material.dashSize; + uniforms.totalSize.value = material.dashSize + material.gapSize; + uniforms.scale.value = material.scale; + + } + + function refreshUniformsPoints( uniforms, material, pixelRatio, height ) { + + uniforms.diffuse.value.copy( material.color ); + uniforms.opacity.value = material.opacity; + uniforms.size.value = material.size * pixelRatio; + uniforms.scale.value = height * 0.5; + + if ( material.map ) { + + uniforms.map.value = material.map; + + refreshTransformUniform( material.map, uniforms.uvTransform ); + + } + + if ( material.alphaMap ) { + + uniforms.alphaMap.value = material.alphaMap; + + refreshTransformUniform( material.alphaMap, uniforms.alphaMapTransform ); + + } + + if ( material.alphaTest > 0 ) { + + uniforms.alphaTest.value = material.alphaTest; + + } + + } + + function refreshUniformsSprites( uniforms, material ) { + + uniforms.diffuse.value.copy( material.color ); + uniforms.opacity.value = material.opacity; + uniforms.rotation.value = material.rotation; + + if ( material.map ) { + + uniforms.map.value = material.map; + + refreshTransformUniform( material.map, uniforms.mapTransform ); + + } + + if ( material.alphaMap ) { + + uniforms.alphaMap.value = material.alphaMap; + + refreshTransformUniform( material.alphaMap, uniforms.alphaMapTransform ); + + } + + if ( material.alphaTest > 0 ) { + + uniforms.alphaTest.value = material.alphaTest; + + } + + } + + function refreshUniformsPhong( uniforms, material ) { + + uniforms.specular.value.copy( material.specular ); + uniforms.shininess.value = Math.max( material.shininess, 1e-4 ); // to prevent pow( 0.0, 0.0 ) + + } + + function refreshUniformsToon( uniforms, material ) { + + if ( material.gradientMap ) { + + uniforms.gradientMap.value = material.gradientMap; + + } + + } + + function refreshUniformsStandard( uniforms, material ) { + + uniforms.metalness.value = material.metalness; + + if ( material.metalnessMap ) { + + uniforms.metalnessMap.value = material.metalnessMap; + + refreshTransformUniform( material.metalnessMap, uniforms.metalnessMapTransform ); + + } + + uniforms.roughness.value = material.roughness; + + if ( material.roughnessMap ) { + + uniforms.roughnessMap.value = material.roughnessMap; + + refreshTransformUniform( material.roughnessMap, uniforms.roughnessMapTransform ); + + } + + if ( material.envMap ) { + + //uniforms.envMap.value = material.envMap; // part of uniforms common + + uniforms.envMapIntensity.value = material.envMapIntensity; + + } + + } + + function refreshUniformsPhysical( uniforms, material, transmissionRenderTarget ) { + + uniforms.ior.value = material.ior; // also part of uniforms common + + if ( material.sheen > 0 ) { + + uniforms.sheenColor.value.copy( material.sheenColor ).multiplyScalar( material.sheen ); + + uniforms.sheenRoughness.value = material.sheenRoughness; + + if ( material.sheenColorMap ) { + + uniforms.sheenColorMap.value = material.sheenColorMap; + + refreshTransformUniform( material.sheenColorMap, uniforms.sheenColorMapTransform ); + + } + + if ( material.sheenRoughnessMap ) { + + uniforms.sheenRoughnessMap.value = material.sheenRoughnessMap; + + refreshTransformUniform( material.sheenRoughnessMap, uniforms.sheenRoughnessMapTransform ); + + } + + } + + if ( material.clearcoat > 0 ) { + + uniforms.clearcoat.value = material.clearcoat; + uniforms.clearcoatRoughness.value = material.clearcoatRoughness; + + if ( material.clearcoatMap ) { + + uniforms.clearcoatMap.value = material.clearcoatMap; + + refreshTransformUniform( material.clearcoatMap, uniforms.clearcoatMapTransform ); + + } + + if ( material.clearcoatRoughnessMap ) { + + uniforms.clearcoatRoughnessMap.value = material.clearcoatRoughnessMap; + + refreshTransformUniform( material.clearcoatRoughnessMap, uniforms.clearcoatRoughnessMapTransform ); + + } + + if ( material.clearcoatNormalMap ) { + + uniforms.clearcoatNormalMap.value = material.clearcoatNormalMap; + + refreshTransformUniform( material.clearcoatNormalMap, uniforms.clearcoatNormalMapTransform ); + + uniforms.clearcoatNormalScale.value.copy( material.clearcoatNormalScale ); + + if ( material.side === BackSide ) { + + uniforms.clearcoatNormalScale.value.negate(); + + } + + } + + } + + if ( material.dispersion > 0 ) { + + uniforms.dispersion.value = material.dispersion; + + } + + if ( material.iridescence > 0 ) { + + uniforms.iridescence.value = material.iridescence; + uniforms.iridescenceIOR.value = material.iridescenceIOR; + uniforms.iridescenceThicknessMinimum.value = material.iridescenceThicknessRange[ 0 ]; + uniforms.iridescenceThicknessMaximum.value = material.iridescenceThicknessRange[ 1 ]; + + if ( material.iridescenceMap ) { + + uniforms.iridescenceMap.value = material.iridescenceMap; + + refreshTransformUniform( material.iridescenceMap, uniforms.iridescenceMapTransform ); + + } + + if ( material.iridescenceThicknessMap ) { + + uniforms.iridescenceThicknessMap.value = material.iridescenceThicknessMap; + + refreshTransformUniform( material.iridescenceThicknessMap, uniforms.iridescenceThicknessMapTransform ); + + } + + } + + if ( material.transmission > 0 ) { + + uniforms.transmission.value = material.transmission; + uniforms.transmissionSamplerMap.value = transmissionRenderTarget.texture; + uniforms.transmissionSamplerSize.value.set( transmissionRenderTarget.width, transmissionRenderTarget.height ); + + if ( material.transmissionMap ) { + + uniforms.transmissionMap.value = material.transmissionMap; + + refreshTransformUniform( material.transmissionMap, uniforms.transmissionMapTransform ); + + } + + uniforms.thickness.value = material.thickness; + + if ( material.thicknessMap ) { + + uniforms.thicknessMap.value = material.thicknessMap; + + refreshTransformUniform( material.thicknessMap, uniforms.thicknessMapTransform ); + + } + + uniforms.attenuationDistance.value = material.attenuationDistance; + uniforms.attenuationColor.value.copy( material.attenuationColor ); + + } + + if ( material.anisotropy > 0 ) { + + uniforms.anisotropyVector.value.set( material.anisotropy * Math.cos( material.anisotropyRotation ), material.anisotropy * Math.sin( material.anisotropyRotation ) ); + + if ( material.anisotropyMap ) { + + uniforms.anisotropyMap.value = material.anisotropyMap; + + refreshTransformUniform( material.anisotropyMap, uniforms.anisotropyMapTransform ); + + } + + } + + uniforms.specularIntensity.value = material.specularIntensity; + uniforms.specularColor.value.copy( material.specularColor ); + + if ( material.specularColorMap ) { + + uniforms.specularColorMap.value = material.specularColorMap; + + refreshTransformUniform( material.specularColorMap, uniforms.specularColorMapTransform ); + + } + + if ( material.specularIntensityMap ) { + + uniforms.specularIntensityMap.value = material.specularIntensityMap; + + refreshTransformUniform( material.specularIntensityMap, uniforms.specularIntensityMapTransform ); + + } + + } + + function refreshUniformsMatcap( uniforms, material ) { + + if ( material.matcap ) { + + uniforms.matcap.value = material.matcap; + + } + + } + + function refreshUniformsDistance( uniforms, material ) { + + const light = properties.get( material ).light; + + uniforms.referencePosition.value.setFromMatrixPosition( light.matrixWorld ); + uniforms.nearDistance.value = light.shadow.camera.near; + uniforms.farDistance.value = light.shadow.camera.far; + + } + + return { + refreshFogUniforms: refreshFogUniforms, + refreshMaterialUniforms: refreshMaterialUniforms + }; + +} + +function WebGLUniformsGroups( gl, info, capabilities, state ) { + + let buffers = {}; + let updateList = {}; + let allocatedBindingPoints = []; + + const maxBindingPoints = gl.getParameter( gl.MAX_UNIFORM_BUFFER_BINDINGS ); // binding points are global whereas block indices are per shader program + + function bind( uniformsGroup, program ) { + + const webglProgram = program.program; + state.uniformBlockBinding( uniformsGroup, webglProgram ); + + } + + function update( uniformsGroup, program ) { + + let buffer = buffers[ uniformsGroup.id ]; + + if ( buffer === undefined ) { + + prepareUniformsGroup( uniformsGroup ); + + buffer = createBuffer( uniformsGroup ); + buffers[ uniformsGroup.id ] = buffer; + + uniformsGroup.addEventListener( 'dispose', onUniformsGroupsDispose ); + + } + + // ensure to update the binding points/block indices mapping for this program + + const webglProgram = program.program; + state.updateUBOMapping( uniformsGroup, webglProgram ); + + // update UBO once per frame + + const frame = info.render.frame; + + if ( updateList[ uniformsGroup.id ] !== frame ) { + + updateBufferData( uniformsGroup ); + + updateList[ uniformsGroup.id ] = frame; + + } + + } + + function createBuffer( uniformsGroup ) { + + // the setup of an UBO is independent of a particular shader program but global + + const bindingPointIndex = allocateBindingPointIndex(); + uniformsGroup.__bindingPointIndex = bindingPointIndex; + + const buffer = gl.createBuffer(); + const size = uniformsGroup.__size; + const usage = uniformsGroup.usage; + + gl.bindBuffer( gl.UNIFORM_BUFFER, buffer ); + gl.bufferData( gl.UNIFORM_BUFFER, size, usage ); + gl.bindBuffer( gl.UNIFORM_BUFFER, null ); + gl.bindBufferBase( gl.UNIFORM_BUFFER, bindingPointIndex, buffer ); + + return buffer; + + } + + function allocateBindingPointIndex() { + + for ( let i = 0; i < maxBindingPoints; i ++ ) { + + if ( allocatedBindingPoints.indexOf( i ) === - 1 ) { + + allocatedBindingPoints.push( i ); + return i; + + } + + } + + console.error( 'THREE.WebGLRenderer: Maximum number of simultaneously usable uniforms groups reached.' ); + + return 0; + + } + + function updateBufferData( uniformsGroup ) { + + const buffer = buffers[ uniformsGroup.id ]; + const uniforms = uniformsGroup.uniforms; + const cache = uniformsGroup.__cache; + + gl.bindBuffer( gl.UNIFORM_BUFFER, buffer ); + + for ( let i = 0, il = uniforms.length; i < il; i ++ ) { + + const uniformArray = Array.isArray( uniforms[ i ] ) ? uniforms[ i ] : [ uniforms[ i ] ]; + + for ( let j = 0, jl = uniformArray.length; j < jl; j ++ ) { + + const uniform = uniformArray[ j ]; + + if ( hasUniformChanged( uniform, i, j, cache ) === true ) { + + const offset = uniform.__offset; + + const values = Array.isArray( uniform.value ) ? uniform.value : [ uniform.value ]; + + let arrayOffset = 0; + + for ( let k = 0; k < values.length; k ++ ) { + + const value = values[ k ]; + + const info = getUniformSize( value ); + + // TODO add integer and struct support + if ( typeof value === 'number' || typeof value === 'boolean' ) { + + uniform.__data[ 0 ] = value; + gl.bufferSubData( gl.UNIFORM_BUFFER, offset + arrayOffset, uniform.__data ); + + } else if ( value.isMatrix3 ) { + + // manually converting 3x3 to 3x4 + + uniform.__data[ 0 ] = value.elements[ 0 ]; + uniform.__data[ 1 ] = value.elements[ 1 ]; + uniform.__data[ 2 ] = value.elements[ 2 ]; + uniform.__data[ 3 ] = 0; + uniform.__data[ 4 ] = value.elements[ 3 ]; + uniform.__data[ 5 ] = value.elements[ 4 ]; + uniform.__data[ 6 ] = value.elements[ 5 ]; + uniform.__data[ 7 ] = 0; + uniform.__data[ 8 ] = value.elements[ 6 ]; + uniform.__data[ 9 ] = value.elements[ 7 ]; + uniform.__data[ 10 ] = value.elements[ 8 ]; + uniform.__data[ 11 ] = 0; + + } else { + + value.toArray( uniform.__data, arrayOffset ); + + arrayOffset += info.storage / Float32Array.BYTES_PER_ELEMENT; + + } + + } + + gl.bufferSubData( gl.UNIFORM_BUFFER, offset, uniform.__data ); + + } + + } + + } + + gl.bindBuffer( gl.UNIFORM_BUFFER, null ); + + } + + function hasUniformChanged( uniform, index, indexArray, cache ) { + + const value = uniform.value; + const indexString = index + '_' + indexArray; + + if ( cache[ indexString ] === undefined ) { + + // cache entry does not exist so far + + if ( typeof value === 'number' || typeof value === 'boolean' ) { + + cache[ indexString ] = value; + + } else { + + cache[ indexString ] = value.clone(); + + } + + return true; + + } else { + + const cachedObject = cache[ indexString ]; + + // compare current value with cached entry + + if ( typeof value === 'number' || typeof value === 'boolean' ) { + + if ( cachedObject !== value ) { + + cache[ indexString ] = value; + return true; + + } + + } else { + + if ( cachedObject.equals( value ) === false ) { + + cachedObject.copy( value ); + return true; + + } + + } + + } + + return false; + + } + + function prepareUniformsGroup( uniformsGroup ) { + + // determine total buffer size according to the STD140 layout + // Hint: STD140 is the only supported layout in WebGL 2 + + const uniforms = uniformsGroup.uniforms; + + let offset = 0; // global buffer offset in bytes + const chunkSize = 16; // size of a chunk in bytes + + for ( let i = 0, l = uniforms.length; i < l; i ++ ) { + + const uniformArray = Array.isArray( uniforms[ i ] ) ? uniforms[ i ] : [ uniforms[ i ] ]; + + for ( let j = 0, jl = uniformArray.length; j < jl; j ++ ) { + + const uniform = uniformArray[ j ]; + + const values = Array.isArray( uniform.value ) ? uniform.value : [ uniform.value ]; + + for ( let k = 0, kl = values.length; k < kl; k ++ ) { + + const value = values[ k ]; + + const info = getUniformSize( value ); + + // Calculate the chunk offset + const chunkOffsetUniform = offset % chunkSize; + + // Check for chunk overflow + if ( chunkOffsetUniform !== 0 && ( chunkSize - chunkOffsetUniform ) < info.boundary ) { + + // Add padding and adjust offset + offset += ( chunkSize - chunkOffsetUniform ); + + } + + // the following two properties will be used for partial buffer updates + + uniform.__data = new Float32Array( info.storage / Float32Array.BYTES_PER_ELEMENT ); + uniform.__offset = offset; + + + // Update the global offset + offset += info.storage; + + + } + + } + + } + + // ensure correct final padding + + const chunkOffset = offset % chunkSize; + + if ( chunkOffset > 0 ) offset += ( chunkSize - chunkOffset ); + + // + + uniformsGroup.__size = offset; + uniformsGroup.__cache = {}; + + return this; + + } + + function getUniformSize( value ) { + + const info = { + boundary: 0, // bytes + storage: 0 // bytes + }; + + // determine sizes according to STD140 + + if ( typeof value === 'number' || typeof value === 'boolean' ) { + + // float/int/bool + + info.boundary = 4; + info.storage = 4; + + } else if ( value.isVector2 ) { + + // vec2 + + info.boundary = 8; + info.storage = 8; + + } else if ( value.isVector3 || value.isColor ) { + + // vec3 + + info.boundary = 16; + info.storage = 12; // evil: vec3 must start on a 16-byte boundary but it only consumes 12 bytes + + } else if ( value.isVector4 ) { + + // vec4 + + info.boundary = 16; + info.storage = 16; + + } else if ( value.isMatrix3 ) { + + // mat3 (in STD140 a 3x3 matrix is represented as 3x4) + + info.boundary = 48; + info.storage = 48; + + } else if ( value.isMatrix4 ) { + + // mat4 + + info.boundary = 64; + info.storage = 64; + + } else if ( value.isTexture ) { + + console.warn( 'THREE.WebGLRenderer: Texture samplers can not be part of an uniforms group.' ); + + } else { + + console.warn( 'THREE.WebGLRenderer: Unsupported uniform value type.', value ); + + } + + return info; + + } + + function onUniformsGroupsDispose( event ) { + + const uniformsGroup = event.target; + + uniformsGroup.removeEventListener( 'dispose', onUniformsGroupsDispose ); + + const index = allocatedBindingPoints.indexOf( uniformsGroup.__bindingPointIndex ); + allocatedBindingPoints.splice( index, 1 ); + + gl.deleteBuffer( buffers[ uniformsGroup.id ] ); + + delete buffers[ uniformsGroup.id ]; + delete updateList[ uniformsGroup.id ]; + + } + + function dispose() { + + for ( const id in buffers ) { + + gl.deleteBuffer( buffers[ id ] ); + + } + + allocatedBindingPoints = []; + buffers = {}; + updateList = {}; + + } + + return { + + bind: bind, + update: update, + + dispose: dispose + + }; + +} + +class WebGLRenderer { + + constructor( parameters = {} ) { + + const { + canvas = createCanvasElement(), + context = null, + depth = true, + stencil = false, + alpha = false, + antialias = false, + premultipliedAlpha = true, + preserveDrawingBuffer = false, + powerPreference = 'default', + failIfMajorPerformanceCaveat = false, + } = parameters; + + this.isWebGLRenderer = true; + + let _alpha; + + if ( context !== null ) { + + if ( typeof WebGLRenderingContext !== 'undefined' && context instanceof WebGLRenderingContext ) { + + throw new Error( 'THREE.WebGLRenderer: WebGL 1 is not supported since r163.' ); + + } + + _alpha = context.getContextAttributes().alpha; + + } else { + + _alpha = alpha; + + } + + const uintClearColor = new Uint32Array( 4 ); + const intClearColor = new Int32Array( 4 ); + + let currentRenderList = null; + let currentRenderState = null; + + // render() can be called from within a callback triggered by another render. + // We track this so that the nested render call gets its list and state isolated from the parent render call. + + const renderListStack = []; + const renderStateStack = []; + + // public properties + + this.domElement = canvas; + + // Debug configuration container + this.debug = { + + /** + * Enables error checking and reporting when shader programs are being compiled + * @type {boolean} + */ + checkShaderErrors: true, + /** + * Callback for custom error reporting. + * @type {?Function} + */ + onShaderError: null + }; + + // clearing + + this.autoClear = true; + this.autoClearColor = true; + this.autoClearDepth = true; + this.autoClearStencil = true; + + // scene graph + + this.sortObjects = true; + + // user-defined clipping + + this.clippingPlanes = []; + this.localClippingEnabled = false; + + // physically based shading + + this._outputColorSpace = SRGBColorSpace; + + // physical lights + + this._useLegacyLights = false; + + // tone mapping + + this.toneMapping = NoToneMapping; + this.toneMappingExposure = 1.0; + + // internal properties + + const _this = this; + + let _isContextLost = false; + + // internal state cache + + let _currentActiveCubeFace = 0; + let _currentActiveMipmapLevel = 0; + let _currentRenderTarget = null; + let _currentMaterialId = - 1; + + let _currentCamera = null; + + const _currentViewport = new Vector4(); + const _currentScissor = new Vector4(); + let _currentScissorTest = null; + + const _currentClearColor = new Color( 0x000000 ); + let _currentClearAlpha = 0; + + // + + let _width = canvas.width; + let _height = canvas.height; + + let _pixelRatio = 1; + let _opaqueSort = null; + let _transparentSort = null; + + const _viewport = new Vector4( 0, 0, _width, _height ); + const _scissor = new Vector4( 0, 0, _width, _height ); + let _scissorTest = false; + + // frustum + + const _frustum = new Frustum(); + + // clipping + + let _clippingEnabled = false; + let _localClippingEnabled = false; + + // camera matrices cache + + const _projScreenMatrix = new Matrix4(); + + const _vector3 = new Vector3(); + + const _emptyScene = { background: null, fog: null, environment: null, overrideMaterial: null, isScene: true }; + + function getTargetPixelRatio() { + + return _currentRenderTarget === null ? _pixelRatio : 1; + + } + + // initialize + + let _gl = context; + + function getContext( contextName, contextAttributes ) { + + return canvas.getContext( contextName, contextAttributes ); + + } + + try { + + const contextAttributes = { + alpha: true, + depth, + stencil, + antialias, + premultipliedAlpha, + preserveDrawingBuffer, + powerPreference, + failIfMajorPerformanceCaveat, + }; + + // OffscreenCanvas does not have setAttribute, see #22811 + if ( 'setAttribute' in canvas ) canvas.setAttribute( 'data-engine', `three.js r${REVISION}` ); + + // event listeners must be registered before WebGL context is created, see #12753 + canvas.addEventListener( 'webglcontextlost', onContextLost, false ); + canvas.addEventListener( 'webglcontextrestored', onContextRestore, false ); + canvas.addEventListener( 'webglcontextcreationerror', onContextCreationError, false ); + + if ( _gl === null ) { + + const contextName = 'webgl2'; + + _gl = getContext( contextName, contextAttributes ); + + if ( _gl === null ) { + + if ( getContext( contextName ) ) { + + throw new Error( 'Error creating WebGL context with your selected attributes.' ); + + } else { + + throw new Error( 'Error creating WebGL context.' ); + + } + + } + + } + + } catch ( error ) { + + console.error( 'THREE.WebGLRenderer: ' + error.message ); + throw error; + + } + + let extensions, capabilities, state, info; + let properties, textures, cubemaps, cubeuvmaps, attributes, geometries, objects; + let programCache, materials, renderLists, renderStates, clipping, shadowMap; + + let background, morphtargets, bufferRenderer, indexedBufferRenderer; + + let utils, bindingStates, uniformsGroups; + + function initGLContext() { + + extensions = new WebGLExtensions( _gl ); + extensions.init(); + + utils = new WebGLUtils( _gl, extensions ); + + capabilities = new WebGLCapabilities( _gl, extensions, parameters, utils ); + + state = new WebGLState( _gl ); + + info = new WebGLInfo( _gl ); + properties = new WebGLProperties(); + textures = new WebGLTextures( _gl, extensions, state, properties, capabilities, utils, info ); + cubemaps = new WebGLCubeMaps( _this ); + cubeuvmaps = new WebGLCubeUVMaps( _this ); + attributes = new WebGLAttributes( _gl ); + bindingStates = new WebGLBindingStates( _gl, attributes ); + geometries = new WebGLGeometries( _gl, attributes, info, bindingStates ); + objects = new WebGLObjects( _gl, geometries, attributes, info ); + morphtargets = new WebGLMorphtargets( _gl, capabilities, textures ); + clipping = new WebGLClipping( properties ); + programCache = new WebGLPrograms( _this, cubemaps, cubeuvmaps, extensions, capabilities, bindingStates, clipping ); + materials = new WebGLMaterials( _this, properties ); + renderLists = new WebGLRenderLists(); + renderStates = new WebGLRenderStates( extensions ); + background = new WebGLBackground( _this, cubemaps, cubeuvmaps, state, objects, _alpha, premultipliedAlpha ); + shadowMap = new WebGLShadowMap( _this, objects, capabilities ); + uniformsGroups = new WebGLUniformsGroups( _gl, info, capabilities, state ); + + bufferRenderer = new WebGLBufferRenderer( _gl, extensions, info ); + indexedBufferRenderer = new WebGLIndexedBufferRenderer( _gl, extensions, info ); + + info.programs = programCache.programs; + + _this.capabilities = capabilities; + _this.extensions = extensions; + _this.properties = properties; + _this.renderLists = renderLists; + _this.shadowMap = shadowMap; + _this.state = state; + _this.info = info; + + } + + initGLContext(); + + // xr + + const xr = new WebXRManager( _this, _gl ); + + this.xr = xr; + + // API + + this.getContext = function () { + + return _gl; + + }; + + this.getContextAttributes = function () { + + return _gl.getContextAttributes(); + + }; + + this.forceContextLoss = function () { + + const extension = extensions.get( 'WEBGL_lose_context' ); + if ( extension ) extension.loseContext(); + + }; + + this.forceContextRestore = function () { + + const extension = extensions.get( 'WEBGL_lose_context' ); + if ( extension ) extension.restoreContext(); + + }; + + this.getPixelRatio = function () { + + return _pixelRatio; + + }; + + this.setPixelRatio = function ( value ) { + + if ( value === undefined ) return; + + _pixelRatio = value; + + this.setSize( _width, _height, false ); + + }; + + this.getSize = function ( target ) { + + return target.set( _width, _height ); + + }; + + this.setSize = function ( width, height, updateStyle = true ) { + + if ( xr.isPresenting ) { + + console.warn( 'THREE.WebGLRenderer: Can\'t change size while VR device is presenting.' ); + return; + + } + + _width = width; + _height = height; + + canvas.width = Math.floor( width * _pixelRatio ); + canvas.height = Math.floor( height * _pixelRatio ); + + if ( updateStyle === true ) { + + canvas.style.width = width + 'px'; + canvas.style.height = height + 'px'; + + } + + this.setViewport( 0, 0, width, height ); + + }; + + this.getDrawingBufferSize = function ( target ) { + + return target.set( _width * _pixelRatio, _height * _pixelRatio ).floor(); + + }; + + this.setDrawingBufferSize = function ( width, height, pixelRatio ) { + + _width = width; + _height = height; + + _pixelRatio = pixelRatio; + + canvas.width = Math.floor( width * pixelRatio ); + canvas.height = Math.floor( height * pixelRatio ); + + this.setViewport( 0, 0, width, height ); + + }; + + this.getCurrentViewport = function ( target ) { + + return target.copy( _currentViewport ); + + }; + + this.getViewport = function ( target ) { + + return target.copy( _viewport ); + + }; + + this.setViewport = function ( x, y, width, height ) { + + if ( x.isVector4 ) { + + _viewport.set( x.x, x.y, x.z, x.w ); + + } else { + + _viewport.set( x, y, width, height ); + + } + + state.viewport( _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ).round() ); + + }; + + this.getScissor = function ( target ) { + + return target.copy( _scissor ); + + }; + + this.setScissor = function ( x, y, width, height ) { + + if ( x.isVector4 ) { + + _scissor.set( x.x, x.y, x.z, x.w ); + + } else { + + _scissor.set( x, y, width, height ); + + } + + state.scissor( _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ).round() ); + + }; + + this.getScissorTest = function () { + + return _scissorTest; + + }; + + this.setScissorTest = function ( boolean ) { + + state.setScissorTest( _scissorTest = boolean ); + + }; + + this.setOpaqueSort = function ( method ) { + + _opaqueSort = method; + + }; + + this.setTransparentSort = function ( method ) { + + _transparentSort = method; + + }; + + // Clearing + + this.getClearColor = function ( target ) { + + return target.copy( background.getClearColor() ); + + }; + + this.setClearColor = function () { + + background.setClearColor.apply( background, arguments ); + + }; + + this.getClearAlpha = function () { + + return background.getClearAlpha(); + + }; + + this.setClearAlpha = function () { + + background.setClearAlpha.apply( background, arguments ); + + }; + + this.clear = function ( color = true, depth = true, stencil = true ) { + + let bits = 0; + + if ( color ) { + + // check if we're trying to clear an integer target + let isIntegerFormat = false; + if ( _currentRenderTarget !== null ) { + + const targetFormat = _currentRenderTarget.texture.format; + isIntegerFormat = targetFormat === RGBAIntegerFormat || + targetFormat === RGIntegerFormat || + targetFormat === RedIntegerFormat; + + } + + // use the appropriate clear functions to clear the target if it's a signed + // or unsigned integer target + if ( isIntegerFormat ) { + + const targetType = _currentRenderTarget.texture.type; + const isUnsignedType = targetType === UnsignedByteType || + targetType === UnsignedIntType || + targetType === UnsignedShortType || + targetType === UnsignedInt248Type || + targetType === UnsignedShort4444Type || + targetType === UnsignedShort5551Type; + + const clearColor = background.getClearColor(); + const a = background.getClearAlpha(); + const r = clearColor.r; + const g = clearColor.g; + const b = clearColor.b; + + if ( isUnsignedType ) { + + uintClearColor[ 0 ] = r; + uintClearColor[ 1 ] = g; + uintClearColor[ 2 ] = b; + uintClearColor[ 3 ] = a; + _gl.clearBufferuiv( _gl.COLOR, 0, uintClearColor ); + + } else { + + intClearColor[ 0 ] = r; + intClearColor[ 1 ] = g; + intClearColor[ 2 ] = b; + intClearColor[ 3 ] = a; + _gl.clearBufferiv( _gl.COLOR, 0, intClearColor ); + + } + + } else { + + bits |= _gl.COLOR_BUFFER_BIT; + + } + + } + + if ( depth ) bits |= _gl.DEPTH_BUFFER_BIT; + if ( stencil ) { + + bits |= _gl.STENCIL_BUFFER_BIT; + this.state.buffers.stencil.setMask( 0xffffffff ); + + } + + _gl.clear( bits ); + + }; + + this.clearColor = function () { + + this.clear( true, false, false ); + + }; + + this.clearDepth = function () { + + this.clear( false, true, false ); + + }; + + this.clearStencil = function () { + + this.clear( false, false, true ); + + }; + + // + + this.dispose = function () { + + canvas.removeEventListener( 'webglcontextlost', onContextLost, false ); + canvas.removeEventListener( 'webglcontextrestored', onContextRestore, false ); + canvas.removeEventListener( 'webglcontextcreationerror', onContextCreationError, false ); + + renderLists.dispose(); + renderStates.dispose(); + properties.dispose(); + cubemaps.dispose(); + cubeuvmaps.dispose(); + objects.dispose(); + bindingStates.dispose(); + uniformsGroups.dispose(); + programCache.dispose(); + + xr.dispose(); + + xr.removeEventListener( 'sessionstart', onXRSessionStart ); + xr.removeEventListener( 'sessionend', onXRSessionEnd ); + + animation.stop(); + + }; + + // Events + + function onContextLost( event ) { + + event.preventDefault(); + + console.log( 'THREE.WebGLRenderer: Context Lost.' ); + + _isContextLost = true; + + } + + function onContextRestore( /* event */ ) { + + console.log( 'THREE.WebGLRenderer: Context Restored.' ); + + _isContextLost = false; + + const infoAutoReset = info.autoReset; + const shadowMapEnabled = shadowMap.enabled; + const shadowMapAutoUpdate = shadowMap.autoUpdate; + const shadowMapNeedsUpdate = shadowMap.needsUpdate; + const shadowMapType = shadowMap.type; + + initGLContext(); + + info.autoReset = infoAutoReset; + shadowMap.enabled = shadowMapEnabled; + shadowMap.autoUpdate = shadowMapAutoUpdate; + shadowMap.needsUpdate = shadowMapNeedsUpdate; + shadowMap.type = shadowMapType; + + } + + function onContextCreationError( event ) { + + console.error( 'THREE.WebGLRenderer: A WebGL context could not be created. Reason: ', event.statusMessage ); + + } + + function onMaterialDispose( event ) { + + const material = event.target; + + material.removeEventListener( 'dispose', onMaterialDispose ); + + deallocateMaterial( material ); + + } + + // Buffer deallocation + + function deallocateMaterial( material ) { + + releaseMaterialProgramReferences( material ); + + properties.remove( material ); + + } + + + function releaseMaterialProgramReferences( material ) { + + const programs = properties.get( material ).programs; + + if ( programs !== undefined ) { + + programs.forEach( function ( program ) { + + programCache.releaseProgram( program ); + + } ); + + if ( material.isShaderMaterial ) { + + programCache.releaseShaderCache( material ); + + } + + } + + } + + // Buffer rendering + + this.renderBufferDirect = function ( camera, scene, geometry, material, object, group ) { + + if ( scene === null ) scene = _emptyScene; // renderBufferDirect second parameter used to be fog (could be null) + + const frontFaceCW = ( object.isMesh && object.matrixWorld.determinant() < 0 ); + + const program = setProgram( camera, scene, geometry, material, object ); + + state.setMaterial( material, frontFaceCW ); + + // + + let index = geometry.index; + let rangeFactor = 1; + + if ( material.wireframe === true ) { + + index = geometries.getWireframeAttribute( geometry ); + + if ( index === undefined ) return; + + rangeFactor = 2; + + } + + // + + const drawRange = geometry.drawRange; + const position = geometry.attributes.position; + + let drawStart = drawRange.start * rangeFactor; + let drawEnd = ( drawRange.start + drawRange.count ) * rangeFactor; + + if ( group !== null ) { + + drawStart = Math.max( drawStart, group.start * rangeFactor ); + drawEnd = Math.min( drawEnd, ( group.start + group.count ) * rangeFactor ); + + } + + if ( index !== null ) { + + drawStart = Math.max( drawStart, 0 ); + drawEnd = Math.min( drawEnd, index.count ); + + } else if ( position !== undefined && position !== null ) { + + drawStart = Math.max( drawStart, 0 ); + drawEnd = Math.min( drawEnd, position.count ); + + } + + const drawCount = drawEnd - drawStart; + + if ( drawCount < 0 || drawCount === Infinity ) return; + + // + + bindingStates.setup( object, material, program, geometry, index ); + + let attribute; + let renderer = bufferRenderer; + + if ( index !== null ) { + + attribute = attributes.get( index ); + + renderer = indexedBufferRenderer; + renderer.setIndex( attribute ); + + } + + // + + if ( object.isMesh ) { + + if ( material.wireframe === true ) { + + state.setLineWidth( material.wireframeLinewidth * getTargetPixelRatio() ); + renderer.setMode( _gl.LINES ); + + } else { + + renderer.setMode( _gl.TRIANGLES ); + + } + + } else if ( object.isLine ) { + + let lineWidth = material.linewidth; + + if ( lineWidth === undefined ) lineWidth = 1; // Not using Line*Material + + state.setLineWidth( lineWidth * getTargetPixelRatio() ); + + if ( object.isLineSegments ) { + + renderer.setMode( _gl.LINES ); + + } else if ( object.isLineLoop ) { + + renderer.setMode( _gl.LINE_LOOP ); + + } else { + + renderer.setMode( _gl.LINE_STRIP ); + + } + + } else if ( object.isPoints ) { + + renderer.setMode( _gl.POINTS ); + + } else if ( object.isSprite ) { + + renderer.setMode( _gl.TRIANGLES ); + + } + + if ( object.isBatchedMesh ) { + + if ( object._multiDrawInstances !== null ) { + + renderer.renderMultiDrawInstances( object._multiDrawStarts, object._multiDrawCounts, object._multiDrawCount, object._multiDrawInstances ); + + } else { + + renderer.renderMultiDraw( object._multiDrawStarts, object._multiDrawCounts, object._multiDrawCount ); + + } + + } else if ( object.isInstancedMesh ) { + + renderer.renderInstances( drawStart, drawCount, object.count ); + + } else if ( geometry.isInstancedBufferGeometry ) { + + const maxInstanceCount = geometry._maxInstanceCount !== undefined ? geometry._maxInstanceCount : Infinity; + const instanceCount = Math.min( geometry.instanceCount, maxInstanceCount ); + + renderer.renderInstances( drawStart, drawCount, instanceCount ); + + } else { + + renderer.render( drawStart, drawCount ); + + } + + }; + + // Compile + + function prepareMaterial( material, scene, object ) { + + if ( material.transparent === true && material.side === DoubleSide && material.forceSinglePass === false ) { + + material.side = BackSide; + material.needsUpdate = true; + getProgram( material, scene, object ); + + material.side = FrontSide; + material.needsUpdate = true; + getProgram( material, scene, object ); + + material.side = DoubleSide; + + } else { + + getProgram( material, scene, object ); + + } + + } + + this.compile = function ( scene, camera, targetScene = null ) { + + if ( targetScene === null ) targetScene = scene; + + currentRenderState = renderStates.get( targetScene ); + currentRenderState.init( camera ); + + renderStateStack.push( currentRenderState ); + + // gather lights from both the target scene and the new object that will be added to the scene. + + targetScene.traverseVisible( function ( object ) { + + if ( object.isLight && object.layers.test( camera.layers ) ) { + + currentRenderState.pushLight( object ); + + if ( object.castShadow ) { + + currentRenderState.pushShadow( object ); + + } + + } + + } ); + + if ( scene !== targetScene ) { + + scene.traverseVisible( function ( object ) { + + if ( object.isLight && object.layers.test( camera.layers ) ) { + + currentRenderState.pushLight( object ); + + if ( object.castShadow ) { + + currentRenderState.pushShadow( object ); + + } + + } + + } ); + + } + + currentRenderState.setupLights( _this._useLegacyLights ); + + // Only initialize materials in the new scene, not the targetScene. + + const materials = new Set(); + + scene.traverse( function ( object ) { + + const material = object.material; + + if ( material ) { + + if ( Array.isArray( material ) ) { + + for ( let i = 0; i < material.length; i ++ ) { + + const material2 = material[ i ]; + + prepareMaterial( material2, targetScene, object ); + materials.add( material2 ); + + } + + } else { + + prepareMaterial( material, targetScene, object ); + materials.add( material ); + + } + + } + + } ); + + renderStateStack.pop(); + currentRenderState = null; + + return materials; + + }; + + // compileAsync + + this.compileAsync = function ( scene, camera, targetScene = null ) { + + const materials = this.compile( scene, camera, targetScene ); + + // Wait for all the materials in the new object to indicate that they're + // ready to be used before resolving the promise. + + return new Promise( ( resolve ) => { + + function checkMaterialsReady() { + + materials.forEach( function ( material ) { + + const materialProperties = properties.get( material ); + const program = materialProperties.currentProgram; + + if ( program.isReady() ) { + + // remove any programs that report they're ready to use from the list + materials.delete( material ); + + } + + } ); + + // once the list of compiling materials is empty, call the callback + + if ( materials.size === 0 ) { + + resolve( scene ); + return; + + } + + // if some materials are still not ready, wait a bit and check again + + setTimeout( checkMaterialsReady, 10 ); + + } + + if ( extensions.get( 'KHR_parallel_shader_compile' ) !== null ) { + + // If we can check the compilation status of the materials without + // blocking then do so right away. + + checkMaterialsReady(); + + } else { + + // Otherwise start by waiting a bit to give the materials we just + // initialized a chance to finish. + + setTimeout( checkMaterialsReady, 10 ); + + } + + } ); + + }; + + // Animation Loop + + let onAnimationFrameCallback = null; + + function onAnimationFrame( time ) { + + if ( onAnimationFrameCallback ) onAnimationFrameCallback( time ); + + } + + function onXRSessionStart() { + + animation.stop(); + + } + + function onXRSessionEnd() { + + animation.start(); + + } + + const animation = new WebGLAnimation(); + animation.setAnimationLoop( onAnimationFrame ); + + if ( typeof self !== 'undefined' ) animation.setContext( self ); + + this.setAnimationLoop = function ( callback ) { + + onAnimationFrameCallback = callback; + xr.setAnimationLoop( callback ); + + ( callback === null ) ? animation.stop() : animation.start(); + + }; + + xr.addEventListener( 'sessionstart', onXRSessionStart ); + xr.addEventListener( 'sessionend', onXRSessionEnd ); + + // Rendering + + this.render = function ( scene, camera ) { + + if ( camera !== undefined && camera.isCamera !== true ) { + + console.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' ); + return; + + } + + if ( _isContextLost === true ) return; + + // update scene graph + + if ( scene.matrixWorldAutoUpdate === true ) scene.updateMatrixWorld(); + + // update camera matrices and frustum + + if ( camera.parent === null && camera.matrixWorldAutoUpdate === true ) camera.updateMatrixWorld(); + + if ( xr.enabled === true && xr.isPresenting === true ) { + + if ( xr.cameraAutoUpdate === true ) xr.updateCamera( camera ); + + camera = xr.getCamera(); // use XR camera for rendering + + } + + // + if ( scene.isScene === true ) scene.onBeforeRender( _this, scene, camera, _currentRenderTarget ); + + currentRenderState = renderStates.get( scene, renderStateStack.length ); + currentRenderState.init( camera ); + + renderStateStack.push( currentRenderState ); + + _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse ); + _frustum.setFromProjectionMatrix( _projScreenMatrix ); + + _localClippingEnabled = this.localClippingEnabled; + _clippingEnabled = clipping.init( this.clippingPlanes, _localClippingEnabled ); + + currentRenderList = renderLists.get( scene, renderListStack.length ); + currentRenderList.init(); + + renderListStack.push( currentRenderList ); + + projectObject( scene, camera, 0, _this.sortObjects ); + + currentRenderList.finish(); + + if ( _this.sortObjects === true ) { + + currentRenderList.sort( _opaqueSort, _transparentSort ); + + } + + const renderBackground = xr.enabled === false || xr.isPresenting === false || xr.hasDepthSensing() === false; + if ( renderBackground ) { + + background.addToRenderList( currentRenderList, scene ); + + } + + // + + this.info.render.frame ++; + + if ( _clippingEnabled === true ) clipping.beginShadows(); + + const shadowsArray = currentRenderState.state.shadowsArray; + + shadowMap.render( shadowsArray, scene, camera ); + + if ( _clippingEnabled === true ) clipping.endShadows(); + + // + + if ( this.info.autoReset === true ) this.info.reset(); + + // render scene + + const opaqueObjects = currentRenderList.opaque; + const transmissiveObjects = currentRenderList.transmissive; + + currentRenderState.setupLights( _this._useLegacyLights ); + + if ( camera.isArrayCamera ) { + + const cameras = camera.cameras; + + if ( transmissiveObjects.length > 0 ) { + + for ( let i = 0, l = cameras.length; i < l; i ++ ) { + + const camera2 = cameras[ i ]; + + renderTransmissionPass( opaqueObjects, transmissiveObjects, scene, camera2 ); + + } + + } + + if ( renderBackground ) background.render( scene ); + + for ( let i = 0, l = cameras.length; i < l; i ++ ) { + + const camera2 = cameras[ i ]; + + renderScene( currentRenderList, scene, camera2, camera2.viewport ); + + } + + } else { + + if ( transmissiveObjects.length > 0 ) renderTransmissionPass( opaqueObjects, transmissiveObjects, scene, camera ); + + if ( renderBackground ) background.render( scene ); + + renderScene( currentRenderList, scene, camera ); + + } + + // + + if ( _currentRenderTarget !== null ) { + + // resolve multisample renderbuffers to a single-sample texture if necessary + + textures.updateMultisampleRenderTarget( _currentRenderTarget ); + + // Generate mipmap if we're using any kind of mipmap filtering + + textures.updateRenderTargetMipmap( _currentRenderTarget ); + + } + + // + + if ( scene.isScene === true ) scene.onAfterRender( _this, scene, camera ); + + // _gl.finish(); + + bindingStates.resetDefaultState(); + _currentMaterialId = - 1; + _currentCamera = null; + + renderStateStack.pop(); + + if ( renderStateStack.length > 0 ) { + + currentRenderState = renderStateStack[ renderStateStack.length - 1 ]; + + if ( _clippingEnabled === true ) clipping.setGlobalState( _this.clippingPlanes, currentRenderState.state.camera ); + + } else { + + currentRenderState = null; + + } + + renderListStack.pop(); + + if ( renderListStack.length > 0 ) { + + currentRenderList = renderListStack[ renderListStack.length - 1 ]; + + } else { + + currentRenderList = null; + + } + + }; + + function projectObject( object, camera, groupOrder, sortObjects ) { + + if ( object.visible === false ) return; + + const visible = object.layers.test( camera.layers ); + + if ( visible ) { + + if ( object.isGroup ) { + + groupOrder = object.renderOrder; + + } else if ( object.isLOD ) { + + if ( object.autoUpdate === true ) object.update( camera ); + + } else if ( object.isLight ) { + + currentRenderState.pushLight( object ); + + if ( object.castShadow ) { + + currentRenderState.pushShadow( object ); + + } + + } else if ( object.isSprite ) { + + if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) { + + if ( sortObjects ) { + + _vector3.setFromMatrixPosition( object.matrixWorld ) + .applyMatrix4( _projScreenMatrix ); + + } + + const geometry = objects.update( object ); + const material = object.material; + + if ( material.visible ) { + + currentRenderList.push( object, geometry, material, groupOrder, _vector3.z, null ); + + } + + } + + } else if ( object.isMesh || object.isLine || object.isPoints ) { + + if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) { + + const geometry = objects.update( object ); + const material = object.material; + + if ( sortObjects ) { + + if ( object.boundingSphere !== undefined ) { + + if ( object.boundingSphere === null ) object.computeBoundingSphere(); + _vector3.copy( object.boundingSphere.center ); + + } else { + + if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere(); + _vector3.copy( geometry.boundingSphere.center ); + + } + + _vector3 + .applyMatrix4( object.matrixWorld ) + .applyMatrix4( _projScreenMatrix ); + + } + + if ( Array.isArray( material ) ) { + + const groups = geometry.groups; + + for ( let i = 0, l = groups.length; i < l; i ++ ) { + + const group = groups[ i ]; + const groupMaterial = material[ group.materialIndex ]; + + if ( groupMaterial && groupMaterial.visible ) { + + currentRenderList.push( object, geometry, groupMaterial, groupOrder, _vector3.z, group ); + + } + + } + + } else if ( material.visible ) { + + currentRenderList.push( object, geometry, material, groupOrder, _vector3.z, null ); + + } + + } + + } + + } + + const children = object.children; + + for ( let i = 0, l = children.length; i < l; i ++ ) { + + projectObject( children[ i ], camera, groupOrder, sortObjects ); + + } + + } + + function renderScene( currentRenderList, scene, camera, viewport ) { + + const opaqueObjects = currentRenderList.opaque; + const transmissiveObjects = currentRenderList.transmissive; + const transparentObjects = currentRenderList.transparent; + + currentRenderState.setupLightsView( camera ); + + if ( _clippingEnabled === true ) clipping.setGlobalState( _this.clippingPlanes, camera ); + + if ( viewport ) state.viewport( _currentViewport.copy( viewport ) ); + + if ( opaqueObjects.length > 0 ) renderObjects( opaqueObjects, scene, camera ); + if ( transmissiveObjects.length > 0 ) renderObjects( transmissiveObjects, scene, camera ); + if ( transparentObjects.length > 0 ) renderObjects( transparentObjects, scene, camera ); + + // Ensure depth buffer writing is enabled so it can be cleared on next render + + state.buffers.depth.setTest( true ); + state.buffers.depth.setMask( true ); + state.buffers.color.setMask( true ); + + state.setPolygonOffset( false ); + + } + + function renderTransmissionPass( opaqueObjects, transmissiveObjects, scene, camera ) { + + const overrideMaterial = scene.isScene === true ? scene.overrideMaterial : null; + + if ( overrideMaterial !== null ) { + + return; + + } + + if ( currentRenderState.state.transmissionRenderTarget[ camera.id ] === undefined ) { + + currentRenderState.state.transmissionRenderTarget[ camera.id ] = new WebGLRenderTarget( 1, 1, { + generateMipmaps: true, + type: ( extensions.has( 'EXT_color_buffer_half_float' ) || extensions.has( 'EXT_color_buffer_float' ) ) ? HalfFloatType : UnsignedByteType, + minFilter: LinearMipmapLinearFilter, + samples: 4, + stencilBuffer: stencil, + resolveDepthBuffer: false, + resolveStencilBuffer: false + } ); + + // debug + + /* + const geometry = new PlaneGeometry(); + const material = new MeshBasicMaterial( { map: _transmissionRenderTarget.texture } ); + + const mesh = new Mesh( geometry, material ); + scene.add( mesh ); + */ + + } + + const transmissionRenderTarget = currentRenderState.state.transmissionRenderTarget[ camera.id ]; + + const activeViewport = camera.viewport || _currentViewport; + transmissionRenderTarget.setSize( activeViewport.z, activeViewport.w ); + + // + + const currentRenderTarget = _this.getRenderTarget(); + _this.setRenderTarget( transmissionRenderTarget ); + + _this.getClearColor( _currentClearColor ); + _currentClearAlpha = _this.getClearAlpha(); + if ( _currentClearAlpha < 1 ) _this.setClearColor( 0xffffff, 0.5 ); + + _this.clear(); + + // Turn off the features which can affect the frag color for opaque objects pass. + // Otherwise they are applied twice in opaque objects pass and transmission objects pass. + const currentToneMapping = _this.toneMapping; + _this.toneMapping = NoToneMapping; + + // Remove viewport from camera to avoid nested render calls resetting viewport to it (e.g Reflector). + // Transmission render pass requires viewport to match the transmissionRenderTarget. + const currentCameraViewport = camera.viewport; + if ( camera.viewport !== undefined ) camera.viewport = undefined; + + currentRenderState.setupLightsView( camera ); + + if ( _clippingEnabled === true ) clipping.setGlobalState( _this.clippingPlanes, camera ); + + renderObjects( opaqueObjects, scene, camera ); + + textures.updateMultisampleRenderTarget( transmissionRenderTarget ); + textures.updateRenderTargetMipmap( transmissionRenderTarget ); + + if ( extensions.has( 'WEBGL_multisampled_render_to_texture' ) === false ) { // see #28131 + + let renderTargetNeedsUpdate = false; + + for ( let i = 0, l = transmissiveObjects.length; i < l; i ++ ) { + + const renderItem = transmissiveObjects[ i ]; + + const object = renderItem.object; + const geometry = renderItem.geometry; + const material = renderItem.material; + const group = renderItem.group; + + if ( material.side === DoubleSide && object.layers.test( camera.layers ) ) { + + const currentSide = material.side; + + material.side = BackSide; + material.needsUpdate = true; + + renderObject( object, scene, camera, geometry, material, group ); + + material.side = currentSide; + material.needsUpdate = true; + + renderTargetNeedsUpdate = true; + + } + + } + + if ( renderTargetNeedsUpdate === true ) { + + textures.updateMultisampleRenderTarget( transmissionRenderTarget ); + textures.updateRenderTargetMipmap( transmissionRenderTarget ); + + } + + } + + _this.setRenderTarget( currentRenderTarget ); + + _this.setClearColor( _currentClearColor, _currentClearAlpha ); + + if ( currentCameraViewport !== undefined ) camera.viewport = currentCameraViewport; + + _this.toneMapping = currentToneMapping; + + } + + function renderObjects( renderList, scene, camera ) { + + const overrideMaterial = scene.isScene === true ? scene.overrideMaterial : null; + + for ( let i = 0, l = renderList.length; i < l; i ++ ) { + + const renderItem = renderList[ i ]; + + const object = renderItem.object; + const geometry = renderItem.geometry; + const material = overrideMaterial === null ? renderItem.material : overrideMaterial; + const group = renderItem.group; + + if ( object.layers.test( camera.layers ) ) { + + renderObject( object, scene, camera, geometry, material, group ); + + } + + } + + } + + function renderObject( object, scene, camera, geometry, material, group ) { + + object.onBeforeRender( _this, scene, camera, geometry, material, group ); + + object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld ); + object.normalMatrix.getNormalMatrix( object.modelViewMatrix ); + + material.onBeforeRender( _this, scene, camera, geometry, object, group ); + + if ( material.transparent === true && material.side === DoubleSide && material.forceSinglePass === false ) { + + material.side = BackSide; + material.needsUpdate = true; + _this.renderBufferDirect( camera, scene, geometry, material, object, group ); + + material.side = FrontSide; + material.needsUpdate = true; + _this.renderBufferDirect( camera, scene, geometry, material, object, group ); + + material.side = DoubleSide; + + } else { + + _this.renderBufferDirect( camera, scene, geometry, material, object, group ); + + } + + object.onAfterRender( _this, scene, camera, geometry, material, group ); + + } + + function getProgram( material, scene, object ) { + + if ( scene.isScene !== true ) scene = _emptyScene; // scene could be a Mesh, Line, Points, ... + + const materialProperties = properties.get( material ); + + const lights = currentRenderState.state.lights; + const shadowsArray = currentRenderState.state.shadowsArray; + + const lightsStateVersion = lights.state.version; + + const parameters = programCache.getParameters( material, lights.state, shadowsArray, scene, object ); + const programCacheKey = programCache.getProgramCacheKey( parameters ); + + let programs = materialProperties.programs; + + // always update environment and fog - changing these trigger an getProgram call, but it's possible that the program doesn't change + + materialProperties.environment = material.isMeshStandardMaterial ? scene.environment : null; + materialProperties.fog = scene.fog; + materialProperties.envMap = ( material.isMeshStandardMaterial ? cubeuvmaps : cubemaps ).get( material.envMap || materialProperties.environment ); + materialProperties.envMapRotation = ( materialProperties.environment !== null && material.envMap === null ) ? scene.environmentRotation : material.envMapRotation; + + if ( programs === undefined ) { + + // new material + + material.addEventListener( 'dispose', onMaterialDispose ); + + programs = new Map(); + materialProperties.programs = programs; + + } + + let program = programs.get( programCacheKey ); + + if ( program !== undefined ) { + + // early out if program and light state is identical + + if ( materialProperties.currentProgram === program && materialProperties.lightsStateVersion === lightsStateVersion ) { + + updateCommonMaterialProperties( material, parameters ); + + return program; + + } + + } else { + + parameters.uniforms = programCache.getUniforms( material ); + + material.onBuild( object, parameters, _this ); + + material.onBeforeCompile( parameters, _this ); + + program = programCache.acquireProgram( parameters, programCacheKey ); + programs.set( programCacheKey, program ); + + materialProperties.uniforms = parameters.uniforms; + + } + + const uniforms = materialProperties.uniforms; + + if ( ( ! material.isShaderMaterial && ! material.isRawShaderMaterial ) || material.clipping === true ) { + + uniforms.clippingPlanes = clipping.uniform; + + } + + updateCommonMaterialProperties( material, parameters ); + + // store the light setup it was created for + + materialProperties.needsLights = materialNeedsLights( material ); + materialProperties.lightsStateVersion = lightsStateVersion; + + if ( materialProperties.needsLights ) { + + // wire up the material to this renderer's lighting state + + uniforms.ambientLightColor.value = lights.state.ambient; + uniforms.lightProbe.value = lights.state.probe; + uniforms.directionalLights.value = lights.state.directional; + uniforms.directionalLightShadows.value = lights.state.directionalShadow; + uniforms.spotLights.value = lights.state.spot; + uniforms.spotLightShadows.value = lights.state.spotShadow; + uniforms.rectAreaLights.value = lights.state.rectArea; + uniforms.ltc_1.value = lights.state.rectAreaLTC1; + uniforms.ltc_2.value = lights.state.rectAreaLTC2; + uniforms.pointLights.value = lights.state.point; + uniforms.pointLightShadows.value = lights.state.pointShadow; + uniforms.hemisphereLights.value = lights.state.hemi; + + uniforms.directionalShadowMap.value = lights.state.directionalShadowMap; + uniforms.directionalShadowMatrix.value = lights.state.directionalShadowMatrix; + uniforms.spotShadowMap.value = lights.state.spotShadowMap; + uniforms.spotLightMatrix.value = lights.state.spotLightMatrix; + uniforms.spotLightMap.value = lights.state.spotLightMap; + uniforms.pointShadowMap.value = lights.state.pointShadowMap; + uniforms.pointShadowMatrix.value = lights.state.pointShadowMatrix; + // TODO (abelnation): add area lights shadow info to uniforms + + } + + materialProperties.currentProgram = program; + materialProperties.uniformsList = null; + + return program; + + } + + function getUniformList( materialProperties ) { + + if ( materialProperties.uniformsList === null ) { + + const progUniforms = materialProperties.currentProgram.getUniforms(); + materialProperties.uniformsList = WebGLUniforms.seqWithValue( progUniforms.seq, materialProperties.uniforms ); + + } + + return materialProperties.uniformsList; + + } + + function updateCommonMaterialProperties( material, parameters ) { + + const materialProperties = properties.get( material ); + + materialProperties.outputColorSpace = parameters.outputColorSpace; + materialProperties.batching = parameters.batching; + materialProperties.instancing = parameters.instancing; + materialProperties.instancingColor = parameters.instancingColor; + materialProperties.instancingMorph = parameters.instancingMorph; + materialProperties.skinning = parameters.skinning; + materialProperties.morphTargets = parameters.morphTargets; + materialProperties.morphNormals = parameters.morphNormals; + materialProperties.morphColors = parameters.morphColors; + materialProperties.morphTargetsCount = parameters.morphTargetsCount; + materialProperties.numClippingPlanes = parameters.numClippingPlanes; + materialProperties.numIntersection = parameters.numClipIntersection; + materialProperties.vertexAlphas = parameters.vertexAlphas; + materialProperties.vertexTangents = parameters.vertexTangents; + materialProperties.toneMapping = parameters.toneMapping; + + } + + function setProgram( camera, scene, geometry, material, object ) { + + if ( scene.isScene !== true ) scene = _emptyScene; // scene could be a Mesh, Line, Points, ... + + textures.resetTextureUnits(); + + const fog = scene.fog; + const environment = material.isMeshStandardMaterial ? scene.environment : null; + const colorSpace = ( _currentRenderTarget === null ) ? _this.outputColorSpace : ( _currentRenderTarget.isXRRenderTarget === true ? _currentRenderTarget.texture.colorSpace : LinearSRGBColorSpace ); + const envMap = ( material.isMeshStandardMaterial ? cubeuvmaps : cubemaps ).get( material.envMap || environment ); + const vertexAlphas = material.vertexColors === true && !! geometry.attributes.color && geometry.attributes.color.itemSize === 4; + const vertexTangents = !! geometry.attributes.tangent && ( !! material.normalMap || material.anisotropy > 0 ); + const morphTargets = !! geometry.morphAttributes.position; + const morphNormals = !! geometry.morphAttributes.normal; + const morphColors = !! geometry.morphAttributes.color; + + let toneMapping = NoToneMapping; + + if ( material.toneMapped ) { + + if ( _currentRenderTarget === null || _currentRenderTarget.isXRRenderTarget === true ) { + + toneMapping = _this.toneMapping; + + } + + } + + const morphAttribute = geometry.morphAttributes.position || geometry.morphAttributes.normal || geometry.morphAttributes.color; + const morphTargetsCount = ( morphAttribute !== undefined ) ? morphAttribute.length : 0; + + const materialProperties = properties.get( material ); + const lights = currentRenderState.state.lights; + + if ( _clippingEnabled === true ) { + + if ( _localClippingEnabled === true || camera !== _currentCamera ) { + + const useCache = + camera === _currentCamera && + material.id === _currentMaterialId; + + // we might want to call this function with some ClippingGroup + // object instead of the material, once it becomes feasible + // (#8465, #8379) + clipping.setState( material, camera, useCache ); + + } + + } + + // + + let needsProgramChange = false; + + if ( material.version === materialProperties.__version ) { + + if ( materialProperties.needsLights && ( materialProperties.lightsStateVersion !== lights.state.version ) ) { + + needsProgramChange = true; + + } else if ( materialProperties.outputColorSpace !== colorSpace ) { + + needsProgramChange = true; + + } else if ( object.isBatchedMesh && materialProperties.batching === false ) { + + needsProgramChange = true; + + } else if ( ! object.isBatchedMesh && materialProperties.batching === true ) { + + needsProgramChange = true; + + } else if ( object.isInstancedMesh && materialProperties.instancing === false ) { + + needsProgramChange = true; + + } else if ( ! object.isInstancedMesh && materialProperties.instancing === true ) { + + needsProgramChange = true; + + } else if ( object.isSkinnedMesh && materialProperties.skinning === false ) { + + needsProgramChange = true; + + } else if ( ! object.isSkinnedMesh && materialProperties.skinning === true ) { + + needsProgramChange = true; + + } else if ( object.isInstancedMesh && materialProperties.instancingColor === true && object.instanceColor === null ) { + + needsProgramChange = true; + + } else if ( object.isInstancedMesh && materialProperties.instancingColor === false && object.instanceColor !== null ) { + + needsProgramChange = true; + + } else if ( object.isInstancedMesh && materialProperties.instancingMorph === true && object.morphTexture === null ) { + + needsProgramChange = true; + + } else if ( object.isInstancedMesh && materialProperties.instancingMorph === false && object.morphTexture !== null ) { + + needsProgramChange = true; + + } else if ( materialProperties.envMap !== envMap ) { + + needsProgramChange = true; + + } else if ( material.fog === true && materialProperties.fog !== fog ) { + + needsProgramChange = true; + + } else if ( materialProperties.numClippingPlanes !== undefined && + ( materialProperties.numClippingPlanes !== clipping.numPlanes || + materialProperties.numIntersection !== clipping.numIntersection ) ) { + + needsProgramChange = true; + + } else if ( materialProperties.vertexAlphas !== vertexAlphas ) { + + needsProgramChange = true; + + } else if ( materialProperties.vertexTangents !== vertexTangents ) { + + needsProgramChange = true; + + } else if ( materialProperties.morphTargets !== morphTargets ) { + + needsProgramChange = true; + + } else if ( materialProperties.morphNormals !== morphNormals ) { + + needsProgramChange = true; + + } else if ( materialProperties.morphColors !== morphColors ) { + + needsProgramChange = true; + + } else if ( materialProperties.toneMapping !== toneMapping ) { + + needsProgramChange = true; + + } else if ( materialProperties.morphTargetsCount !== morphTargetsCount ) { + + needsProgramChange = true; + + } + + } else { + + needsProgramChange = true; + materialProperties.__version = material.version; + + } + + // + + let program = materialProperties.currentProgram; + + if ( needsProgramChange === true ) { + + program = getProgram( material, scene, object ); + + } + + let refreshProgram = false; + let refreshMaterial = false; + let refreshLights = false; + + const p_uniforms = program.getUniforms(), + m_uniforms = materialProperties.uniforms; + + if ( state.useProgram( program.program ) ) { + + refreshProgram = true; + refreshMaterial = true; + refreshLights = true; + + } + + if ( material.id !== _currentMaterialId ) { + + _currentMaterialId = material.id; + + refreshMaterial = true; + + } + + if ( refreshProgram || _currentCamera !== camera ) { + + // common camera uniforms + + p_uniforms.setValue( _gl, 'projectionMatrix', camera.projectionMatrix ); + p_uniforms.setValue( _gl, 'viewMatrix', camera.matrixWorldInverse ); + + const uCamPos = p_uniforms.map.cameraPosition; + + if ( uCamPos !== undefined ) { + + uCamPos.setValue( _gl, _vector3.setFromMatrixPosition( camera.matrixWorld ) ); + + } + + if ( capabilities.logarithmicDepthBuffer ) { + + p_uniforms.setValue( _gl, 'logDepthBufFC', + 2.0 / ( Math.log( camera.far + 1.0 ) / Math.LN2 ) ); + + } + + // consider moving isOrthographic to UniformLib and WebGLMaterials, see https://github.com/mrdoob/three.js/pull/26467#issuecomment-1645185067 + + if ( material.isMeshPhongMaterial || + material.isMeshToonMaterial || + material.isMeshLambertMaterial || + material.isMeshBasicMaterial || + material.isMeshStandardMaterial || + material.isShaderMaterial ) { + + p_uniforms.setValue( _gl, 'isOrthographic', camera.isOrthographicCamera === true ); + + } + + if ( _currentCamera !== camera ) { + + _currentCamera = camera; + + // lighting uniforms depend on the camera so enforce an update + // now, in case this material supports lights - or later, when + // the next material that does gets activated: + + refreshMaterial = true; // set to true on material change + refreshLights = true; // remains set until update done + + } + + } + + // skinning and morph target uniforms must be set even if material didn't change + // auto-setting of texture unit for bone and morph texture must go before other textures + // otherwise textures used for skinning and morphing can take over texture units reserved for other material textures + + if ( object.isSkinnedMesh ) { + + p_uniforms.setOptional( _gl, object, 'bindMatrix' ); + p_uniforms.setOptional( _gl, object, 'bindMatrixInverse' ); + + const skeleton = object.skeleton; + + if ( skeleton ) { + + if ( skeleton.boneTexture === null ) skeleton.computeBoneTexture(); + + p_uniforms.setValue( _gl, 'boneTexture', skeleton.boneTexture, textures ); + + } + + } + + if ( object.isBatchedMesh ) { + + p_uniforms.setOptional( _gl, object, 'batchingTexture' ); + p_uniforms.setValue( _gl, 'batchingTexture', object._matricesTexture, textures ); + + } + + const morphAttributes = geometry.morphAttributes; + + if ( morphAttributes.position !== undefined || morphAttributes.normal !== undefined || ( morphAttributes.color !== undefined ) ) { + + morphtargets.update( object, geometry, program ); + + } + + if ( refreshMaterial || materialProperties.receiveShadow !== object.receiveShadow ) { + + materialProperties.receiveShadow = object.receiveShadow; + p_uniforms.setValue( _gl, 'receiveShadow', object.receiveShadow ); + + } + + // https://github.com/mrdoob/three.js/pull/24467#issuecomment-1209031512 + + if ( material.isMeshGouraudMaterial && material.envMap !== null ) { + + m_uniforms.envMap.value = envMap; + + m_uniforms.flipEnvMap.value = ( envMap.isCubeTexture && envMap.isRenderTargetTexture === false ) ? - 1 : 1; + + } + + if ( material.isMeshStandardMaterial && material.envMap === null && scene.environment !== null ) { + + m_uniforms.envMapIntensity.value = scene.environmentIntensity; + + } + + if ( refreshMaterial ) { + + p_uniforms.setValue( _gl, 'toneMappingExposure', _this.toneMappingExposure ); + + if ( materialProperties.needsLights ) { + + // the current material requires lighting info + + // note: all lighting uniforms are always set correctly + // they simply reference the renderer's state for their + // values + // + // use the current material's .needsUpdate flags to set + // the GL state when required + + markUniformsLightsNeedsUpdate( m_uniforms, refreshLights ); + + } + + // refresh uniforms common to several materials + + if ( fog && material.fog === true ) { + + materials.refreshFogUniforms( m_uniforms, fog ); + + } + + materials.refreshMaterialUniforms( m_uniforms, material, _pixelRatio, _height, currentRenderState.state.transmissionRenderTarget[ camera.id ] ); + + WebGLUniforms.upload( _gl, getUniformList( materialProperties ), m_uniforms, textures ); + + } + + if ( material.isShaderMaterial && material.uniformsNeedUpdate === true ) { + + WebGLUniforms.upload( _gl, getUniformList( materialProperties ), m_uniforms, textures ); + material.uniformsNeedUpdate = false; + + } + + if ( material.isSpriteMaterial ) { + + p_uniforms.setValue( _gl, 'center', object.center ); + + } + + // common matrices + + p_uniforms.setValue( _gl, 'modelViewMatrix', object.modelViewMatrix ); + p_uniforms.setValue( _gl, 'normalMatrix', object.normalMatrix ); + p_uniforms.setValue( _gl, 'modelMatrix', object.matrixWorld ); + + // UBOs + + if ( material.isShaderMaterial || material.isRawShaderMaterial ) { + + const groups = material.uniformsGroups; + + for ( let i = 0, l = groups.length; i < l; i ++ ) { + + const group = groups[ i ]; + + uniformsGroups.update( group, program ); + uniformsGroups.bind( group, program ); + + } + + } + + return program; + + } + + // If uniforms are marked as clean, they don't need to be loaded to the GPU. + + function markUniformsLightsNeedsUpdate( uniforms, value ) { + + uniforms.ambientLightColor.needsUpdate = value; + uniforms.lightProbe.needsUpdate = value; + + uniforms.directionalLights.needsUpdate = value; + uniforms.directionalLightShadows.needsUpdate = value; + uniforms.pointLights.needsUpdate = value; + uniforms.pointLightShadows.needsUpdate = value; + uniforms.spotLights.needsUpdate = value; + uniforms.spotLightShadows.needsUpdate = value; + uniforms.rectAreaLights.needsUpdate = value; + uniforms.hemisphereLights.needsUpdate = value; + + } + + function materialNeedsLights( material ) { + + return material.isMeshLambertMaterial || material.isMeshToonMaterial || material.isMeshPhongMaterial || + material.isMeshStandardMaterial || material.isShadowMaterial || + ( material.isShaderMaterial && material.lights === true ); + + } + + this.getActiveCubeFace = function () { + + return _currentActiveCubeFace; + + }; + + this.getActiveMipmapLevel = function () { + + return _currentActiveMipmapLevel; + + }; + + this.getRenderTarget = function () { + + return _currentRenderTarget; + + }; + + this.setRenderTargetTextures = function ( renderTarget, colorTexture, depthTexture ) { + + properties.get( renderTarget.texture ).__webglTexture = colorTexture; + properties.get( renderTarget.depthTexture ).__webglTexture = depthTexture; + + const renderTargetProperties = properties.get( renderTarget ); + renderTargetProperties.__hasExternalTextures = true; + + renderTargetProperties.__autoAllocateDepthBuffer = depthTexture === undefined; + + if ( ! renderTargetProperties.__autoAllocateDepthBuffer ) { + + // The multisample_render_to_texture extension doesn't work properly if there + // are midframe flushes and an external depth buffer. Disable use of the extension. + if ( extensions.has( 'WEBGL_multisampled_render_to_texture' ) === true ) { + + console.warn( 'THREE.WebGLRenderer: Render-to-texture extension was disabled because an external texture was provided' ); + renderTargetProperties.__useRenderToTexture = false; + + } + + } + + }; + + this.setRenderTargetFramebuffer = function ( renderTarget, defaultFramebuffer ) { + + const renderTargetProperties = properties.get( renderTarget ); + renderTargetProperties.__webglFramebuffer = defaultFramebuffer; + renderTargetProperties.__useDefaultFramebuffer = defaultFramebuffer === undefined; + + }; + + this.setRenderTarget = function ( renderTarget, activeCubeFace = 0, activeMipmapLevel = 0 ) { + + _currentRenderTarget = renderTarget; + _currentActiveCubeFace = activeCubeFace; + _currentActiveMipmapLevel = activeMipmapLevel; + + let useDefaultFramebuffer = true; + let framebuffer = null; + let isCube = false; + let isRenderTarget3D = false; + + if ( renderTarget ) { + + const renderTargetProperties = properties.get( renderTarget ); + + if ( renderTargetProperties.__useDefaultFramebuffer !== undefined ) { + + // We need to make sure to rebind the framebuffer. + state.bindFramebuffer( _gl.FRAMEBUFFER, null ); + useDefaultFramebuffer = false; + + } else if ( renderTargetProperties.__webglFramebuffer === undefined ) { + + textures.setupRenderTarget( renderTarget ); + + } else if ( renderTargetProperties.__hasExternalTextures ) { + + // Color and depth texture must be rebound in order for the swapchain to update. + textures.rebindTextures( renderTarget, properties.get( renderTarget.texture ).__webglTexture, properties.get( renderTarget.depthTexture ).__webglTexture ); + + } + + const texture = renderTarget.texture; + + if ( texture.isData3DTexture || texture.isDataArrayTexture || texture.isCompressedArrayTexture ) { + + isRenderTarget3D = true; + + } + + const __webglFramebuffer = properties.get( renderTarget ).__webglFramebuffer; + + if ( renderTarget.isWebGLCubeRenderTarget ) { + + if ( Array.isArray( __webglFramebuffer[ activeCubeFace ] ) ) { + + framebuffer = __webglFramebuffer[ activeCubeFace ][ activeMipmapLevel ]; + + } else { + + framebuffer = __webglFramebuffer[ activeCubeFace ]; + + } + + isCube = true; + + } else if ( ( renderTarget.samples > 0 ) && textures.useMultisampledRTT( renderTarget ) === false ) { + + framebuffer = properties.get( renderTarget ).__webglMultisampledFramebuffer; + + } else { + + if ( Array.isArray( __webglFramebuffer ) ) { + + framebuffer = __webglFramebuffer[ activeMipmapLevel ]; + + } else { + + framebuffer = __webglFramebuffer; + + } + + } + + _currentViewport.copy( renderTarget.viewport ); + _currentScissor.copy( renderTarget.scissor ); + _currentScissorTest = renderTarget.scissorTest; + + } else { + + _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ).floor(); + _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ).floor(); + _currentScissorTest = _scissorTest; + + } + + const framebufferBound = state.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer ); + + if ( framebufferBound && useDefaultFramebuffer ) { + + state.drawBuffers( renderTarget, framebuffer ); + + } + + state.viewport( _currentViewport ); + state.scissor( _currentScissor ); + state.setScissorTest( _currentScissorTest ); + + if ( isCube ) { + + const textureProperties = properties.get( renderTarget.texture ); + _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + activeCubeFace, textureProperties.__webglTexture, activeMipmapLevel ); + + } else if ( isRenderTarget3D ) { + + const textureProperties = properties.get( renderTarget.texture ); + const layer = activeCubeFace || 0; + _gl.framebufferTextureLayer( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, textureProperties.__webglTexture, activeMipmapLevel || 0, layer ); + + } + + _currentMaterialId = - 1; // reset current material to ensure correct uniform bindings + + }; + + this.readRenderTargetPixels = function ( renderTarget, x, y, width, height, buffer, activeCubeFaceIndex ) { + + if ( ! ( renderTarget && renderTarget.isWebGLRenderTarget ) ) { + + console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not THREE.WebGLRenderTarget.' ); + return; + + } + + let framebuffer = properties.get( renderTarget ).__webglFramebuffer; + + if ( renderTarget.isWebGLCubeRenderTarget && activeCubeFaceIndex !== undefined ) { + + framebuffer = framebuffer[ activeCubeFaceIndex ]; + + } + + if ( framebuffer ) { + + state.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer ); + + try { + + const texture = renderTarget.texture; + const textureFormat = texture.format; + const textureType = texture.type; + + if ( ! capabilities.textureFormatReadable( textureFormat ) ) { + + console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not in RGBA or implementation defined format.' ); + return; + + } + + if ( ! capabilities.textureTypeReadable( textureType ) ) { + + console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not in UnsignedByteType or implementation defined type.' ); + return; + + } + + // the following if statement ensures valid read requests (no out-of-bounds pixels, see #8604) + + if ( ( x >= 0 && x <= ( renderTarget.width - width ) ) && ( y >= 0 && y <= ( renderTarget.height - height ) ) ) { + + _gl.readPixels( x, y, width, height, utils.convert( textureFormat ), utils.convert( textureType ), buffer ); + + } + + } finally { + + // restore framebuffer of current render target if necessary + + const framebuffer = ( _currentRenderTarget !== null ) ? properties.get( _currentRenderTarget ).__webglFramebuffer : null; + state.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer ); + + } + + } + + }; + + this.copyFramebufferToTexture = function ( position, texture, level = 0 ) { + + const levelScale = Math.pow( 2, - level ); + const width = Math.floor( texture.image.width * levelScale ); + const height = Math.floor( texture.image.height * levelScale ); + + textures.setTexture2D( texture, 0 ); + + _gl.copyTexSubImage2D( _gl.TEXTURE_2D, level, 0, 0, position.x, position.y, width, height ); + + state.unbindTexture(); + + }; + + this.copyTextureToTexture = function ( position, srcTexture, dstTexture, level = 0 ) { + + const width = srcTexture.image.width; + const height = srcTexture.image.height; + const glFormat = utils.convert( dstTexture.format ); + const glType = utils.convert( dstTexture.type ); + + textures.setTexture2D( dstTexture, 0 ); + + // As another texture upload may have changed pixelStorei + // parameters, make sure they are correct for the dstTexture + _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, dstTexture.flipY ); + _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, dstTexture.premultiplyAlpha ); + _gl.pixelStorei( _gl.UNPACK_ALIGNMENT, dstTexture.unpackAlignment ); + + if ( srcTexture.isDataTexture ) { + + _gl.texSubImage2D( _gl.TEXTURE_2D, level, position.x, position.y, width, height, glFormat, glType, srcTexture.image.data ); + + } else { + + if ( srcTexture.isCompressedTexture ) { + + _gl.compressedTexSubImage2D( _gl.TEXTURE_2D, level, position.x, position.y, srcTexture.mipmaps[ 0 ].width, srcTexture.mipmaps[ 0 ].height, glFormat, srcTexture.mipmaps[ 0 ].data ); + + } else { + + _gl.texSubImage2D( _gl.TEXTURE_2D, level, position.x, position.y, glFormat, glType, srcTexture.image ); + + } + + } + + // Generate mipmaps only when copying level 0 + if ( level === 0 && dstTexture.generateMipmaps ) _gl.generateMipmap( _gl.TEXTURE_2D ); + + state.unbindTexture(); + + }; + + this.copyTextureToTexture3D = function ( sourceBox, position, srcTexture, dstTexture, level = 0 ) { + + const width = sourceBox.max.x - sourceBox.min.x; + const height = sourceBox.max.y - sourceBox.min.y; + const depth = sourceBox.max.z - sourceBox.min.z; + const glFormat = utils.convert( dstTexture.format ); + const glType = utils.convert( dstTexture.type ); + let glTarget; + + if ( dstTexture.isData3DTexture ) { + + textures.setTexture3D( dstTexture, 0 ); + glTarget = _gl.TEXTURE_3D; + + } else if ( dstTexture.isDataArrayTexture || dstTexture.isCompressedArrayTexture ) { + + textures.setTexture2DArray( dstTexture, 0 ); + glTarget = _gl.TEXTURE_2D_ARRAY; + + } else { + + console.warn( 'THREE.WebGLRenderer.copyTextureToTexture3D: only supports THREE.DataTexture3D and THREE.DataTexture2DArray.' ); + return; + + } + + _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, dstTexture.flipY ); + _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, dstTexture.premultiplyAlpha ); + _gl.pixelStorei( _gl.UNPACK_ALIGNMENT, dstTexture.unpackAlignment ); + + const unpackRowLen = _gl.getParameter( _gl.UNPACK_ROW_LENGTH ); + const unpackImageHeight = _gl.getParameter( _gl.UNPACK_IMAGE_HEIGHT ); + const unpackSkipPixels = _gl.getParameter( _gl.UNPACK_SKIP_PIXELS ); + const unpackSkipRows = _gl.getParameter( _gl.UNPACK_SKIP_ROWS ); + const unpackSkipImages = _gl.getParameter( _gl.UNPACK_SKIP_IMAGES ); + + const image = srcTexture.isCompressedTexture ? srcTexture.mipmaps[ level ] : srcTexture.image; + + _gl.pixelStorei( _gl.UNPACK_ROW_LENGTH, image.width ); + _gl.pixelStorei( _gl.UNPACK_IMAGE_HEIGHT, image.height ); + _gl.pixelStorei( _gl.UNPACK_SKIP_PIXELS, sourceBox.min.x ); + _gl.pixelStorei( _gl.UNPACK_SKIP_ROWS, sourceBox.min.y ); + _gl.pixelStorei( _gl.UNPACK_SKIP_IMAGES, sourceBox.min.z ); + + if ( srcTexture.isDataTexture || srcTexture.isData3DTexture ) { + + _gl.texSubImage3D( glTarget, level, position.x, position.y, position.z, width, height, depth, glFormat, glType, image.data ); + + } else { + + if ( dstTexture.isCompressedArrayTexture ) { + + _gl.compressedTexSubImage3D( glTarget, level, position.x, position.y, position.z, width, height, depth, glFormat, image.data ); + + } else { + + _gl.texSubImage3D( glTarget, level, position.x, position.y, position.z, width, height, depth, glFormat, glType, image ); + + } + + } + + _gl.pixelStorei( _gl.UNPACK_ROW_LENGTH, unpackRowLen ); + _gl.pixelStorei( _gl.UNPACK_IMAGE_HEIGHT, unpackImageHeight ); + _gl.pixelStorei( _gl.UNPACK_SKIP_PIXELS, unpackSkipPixels ); + _gl.pixelStorei( _gl.UNPACK_SKIP_ROWS, unpackSkipRows ); + _gl.pixelStorei( _gl.UNPACK_SKIP_IMAGES, unpackSkipImages ); + + // Generate mipmaps only when copying level 0 + if ( level === 0 && dstTexture.generateMipmaps ) _gl.generateMipmap( glTarget ); + + state.unbindTexture(); + + }; + + this.initTexture = function ( texture ) { + + if ( texture.isCubeTexture ) { + + textures.setTextureCube( texture, 0 ); + + } else if ( texture.isData3DTexture ) { + + textures.setTexture3D( texture, 0 ); + + } else if ( texture.isDataArrayTexture || texture.isCompressedArrayTexture ) { + + textures.setTexture2DArray( texture, 0 ); + + } else { + + textures.setTexture2D( texture, 0 ); + + } + + state.unbindTexture(); + + }; + + this.resetState = function () { + + _currentActiveCubeFace = 0; + _currentActiveMipmapLevel = 0; + _currentRenderTarget = null; + + state.reset(); + bindingStates.reset(); + + }; + + if ( typeof __THREE_DEVTOOLS__ !== 'undefined' ) { + + __THREE_DEVTOOLS__.dispatchEvent( new CustomEvent( 'observe', { detail: this } ) ); + + } + + } + + get coordinateSystem() { + + return WebGLCoordinateSystem; + + } + + get outputColorSpace() { + + return this._outputColorSpace; + + } + + set outputColorSpace( colorSpace ) { + + this._outputColorSpace = colorSpace; + + const gl = this.getContext(); + gl.drawingBufferColorSpace = colorSpace === DisplayP3ColorSpace ? 'display-p3' : 'srgb'; + gl.unpackColorSpace = ColorManagement.workingColorSpace === LinearDisplayP3ColorSpace ? 'display-p3' : 'srgb'; + + } + + get useLegacyLights() { // @deprecated, r155 + + console.warn( 'THREE.WebGLRenderer: The property .useLegacyLights has been deprecated. Migrate your lighting according to the following guide: https://discourse.threejs.org/t/updates-to-lighting-in-three-js-r155/53733.' ); + return this._useLegacyLights; + + } + + set useLegacyLights( value ) { // @deprecated, r155 + + console.warn( 'THREE.WebGLRenderer: The property .useLegacyLights has been deprecated. Migrate your lighting according to the following guide: https://discourse.threejs.org/t/updates-to-lighting-in-three-js-r155/53733.' ); + this._useLegacyLights = value; + + } + +} + +class FogExp2 { + + constructor( color, density = 0.00025 ) { + + this.isFogExp2 = true; + + this.name = ''; + + this.color = new Color( color ); + this.density = density; + + } + + clone() { + + return new FogExp2( this.color, this.density ); + + } + + toJSON( /* meta */ ) { + + return { + type: 'FogExp2', + name: this.name, + color: this.color.getHex(), + density: this.density + }; + + } + +} + +class Fog { + + constructor( color, near = 1, far = 1000 ) { + + this.isFog = true; + + this.name = ''; + + this.color = new Color( color ); + + this.near = near; + this.far = far; + + } + + clone() { + + return new Fog( this.color, this.near, this.far ); + + } + + toJSON( /* meta */ ) { + + return { + type: 'Fog', + name: this.name, + color: this.color.getHex(), + near: this.near, + far: this.far + }; + + } + +} + +class Scene extends Object3D { + + constructor() { + + super(); + + this.isScene = true; + + this.type = 'Scene'; + + this.background = null; + this.environment = null; + this.fog = null; + + this.backgroundBlurriness = 0; + this.backgroundIntensity = 1; + this.backgroundRotation = new Euler(); + + this.environmentIntensity = 1; + this.environmentRotation = new Euler(); + + this.overrideMaterial = null; + + if ( typeof __THREE_DEVTOOLS__ !== 'undefined' ) { + + __THREE_DEVTOOLS__.dispatchEvent( new CustomEvent( 'observe', { detail: this } ) ); + + } + + } + + copy( source, recursive ) { + + super.copy( source, recursive ); + + if ( source.background !== null ) this.background = source.background.clone(); + if ( source.environment !== null ) this.environment = source.environment.clone(); + if ( source.fog !== null ) this.fog = source.fog.clone(); + + this.backgroundBlurriness = source.backgroundBlurriness; + this.backgroundIntensity = source.backgroundIntensity; + this.backgroundRotation.copy( source.backgroundRotation ); + + this.environmentIntensity = source.environmentIntensity; + this.environmentRotation.copy( source.environmentRotation ); + + if ( source.overrideMaterial !== null ) this.overrideMaterial = source.overrideMaterial.clone(); + + this.matrixAutoUpdate = source.matrixAutoUpdate; + + return this; + + } + + toJSON( meta ) { + + const data = super.toJSON( meta ); + + if ( this.fog !== null ) data.object.fog = this.fog.toJSON(); + + if ( this.backgroundBlurriness > 0 ) data.object.backgroundBlurriness = this.backgroundBlurriness; + if ( this.backgroundIntensity !== 1 ) data.object.backgroundIntensity = this.backgroundIntensity; + data.object.backgroundRotation = this.backgroundRotation.toArray(); + + if ( this.environmentIntensity !== 1 ) data.object.environmentIntensity = this.environmentIntensity; + data.object.environmentRotation = this.environmentRotation.toArray(); + + return data; + + } + +} + +class InterleavedBuffer { + + constructor( array, stride ) { + + this.isInterleavedBuffer = true; + + this.array = array; + this.stride = stride; + this.count = array !== undefined ? array.length / stride : 0; + + this.usage = StaticDrawUsage; + this._updateRange = { offset: 0, count: - 1 }; + this.updateRanges = []; + + this.version = 0; + + this.uuid = generateUUID(); + + } + + onUploadCallback() {} + + set needsUpdate( value ) { + + if ( value === true ) this.version ++; + + } + + get updateRange() { + + warnOnce( 'THREE.InterleavedBuffer: updateRange() is deprecated and will be removed in r169. Use addUpdateRange() instead.' ); // @deprecated, r159 + return this._updateRange; + + } + + setUsage( value ) { + + this.usage = value; + + return this; + + } + + addUpdateRange( start, count ) { + + this.updateRanges.push( { start, count } ); + + } + + clearUpdateRanges() { + + this.updateRanges.length = 0; + + } + + copy( source ) { + + this.array = new source.array.constructor( source.array ); + this.count = source.count; + this.stride = source.stride; + this.usage = source.usage; + + return this; + + } + + copyAt( index1, attribute, index2 ) { + + index1 *= this.stride; + index2 *= attribute.stride; + + for ( let i = 0, l = this.stride; i < l; i ++ ) { + + this.array[ index1 + i ] = attribute.array[ index2 + i ]; + + } + + return this; + + } + + set( value, offset = 0 ) { + + this.array.set( value, offset ); + + return this; + + } + + clone( data ) { + + if ( data.arrayBuffers === undefined ) { + + data.arrayBuffers = {}; + + } + + if ( this.array.buffer._uuid === undefined ) { + + this.array.buffer._uuid = generateUUID(); + + } + + if ( data.arrayBuffers[ this.array.buffer._uuid ] === undefined ) { + + data.arrayBuffers[ this.array.buffer._uuid ] = this.array.slice( 0 ).buffer; + + } + + const array = new this.array.constructor( data.arrayBuffers[ this.array.buffer._uuid ] ); + + const ib = new this.constructor( array, this.stride ); + ib.setUsage( this.usage ); + + return ib; + + } + + onUpload( callback ) { + + this.onUploadCallback = callback; + + return this; + + } + + toJSON( data ) { + + if ( data.arrayBuffers === undefined ) { + + data.arrayBuffers = {}; + + } + + // generate UUID for array buffer if necessary + + if ( this.array.buffer._uuid === undefined ) { + + this.array.buffer._uuid = generateUUID(); + + } + + if ( data.arrayBuffers[ this.array.buffer._uuid ] === undefined ) { + + data.arrayBuffers[ this.array.buffer._uuid ] = Array.from( new Uint32Array( this.array.buffer ) ); + + } + + // + + return { + uuid: this.uuid, + buffer: this.array.buffer._uuid, + type: this.array.constructor.name, + stride: this.stride + }; + + } + +} + +const _vector$6 = /*@__PURE__*/ new Vector3(); + +class InterleavedBufferAttribute { + + constructor( interleavedBuffer, itemSize, offset, normalized = false ) { + + this.isInterleavedBufferAttribute = true; + + this.name = ''; + + this.data = interleavedBuffer; + this.itemSize = itemSize; + this.offset = offset; + + this.normalized = normalized; + + } + + get count() { + + return this.data.count; + + } + + get array() { + + return this.data.array; + + } + + set needsUpdate( value ) { + + this.data.needsUpdate = value; + + } + + applyMatrix4( m ) { + + for ( let i = 0, l = this.data.count; i < l; i ++ ) { + + _vector$6.fromBufferAttribute( this, i ); + + _vector$6.applyMatrix4( m ); + + this.setXYZ( i, _vector$6.x, _vector$6.y, _vector$6.z ); + + } + + return this; + + } + + applyNormalMatrix( m ) { + + for ( let i = 0, l = this.count; i < l; i ++ ) { + + _vector$6.fromBufferAttribute( this, i ); + + _vector$6.applyNormalMatrix( m ); + + this.setXYZ( i, _vector$6.x, _vector$6.y, _vector$6.z ); + + } + + return this; + + } + + transformDirection( m ) { + + for ( let i = 0, l = this.count; i < l; i ++ ) { + + _vector$6.fromBufferAttribute( this, i ); + + _vector$6.transformDirection( m ); + + this.setXYZ( i, _vector$6.x, _vector$6.y, _vector$6.z ); + + } + + return this; + + } + + getComponent( index, component ) { + + let value = this.array[ index * this.data.stride + this.offset + component ]; + + if ( this.normalized ) value = denormalize( value, this.array ); + + return value; + + } + + setComponent( index, component, value ) { + + if ( this.normalized ) value = normalize( value, this.array ); + + this.data.array[ index * this.data.stride + this.offset + component ] = value; + + return this; + + } + + setX( index, x ) { + + if ( this.normalized ) x = normalize( x, this.array ); + + this.data.array[ index * this.data.stride + this.offset ] = x; + + return this; + + } + + setY( index, y ) { + + if ( this.normalized ) y = normalize( y, this.array ); + + this.data.array[ index * this.data.stride + this.offset + 1 ] = y; + + return this; + + } + + setZ( index, z ) { + + if ( this.normalized ) z = normalize( z, this.array ); + + this.data.array[ index * this.data.stride + this.offset + 2 ] = z; + + return this; + + } + + setW( index, w ) { + + if ( this.normalized ) w = normalize( w, this.array ); + + this.data.array[ index * this.data.stride + this.offset + 3 ] = w; + + return this; + + } + + getX( index ) { + + let x = this.data.array[ index * this.data.stride + this.offset ]; + + if ( this.normalized ) x = denormalize( x, this.array ); + + return x; + + } + + getY( index ) { + + let y = this.data.array[ index * this.data.stride + this.offset + 1 ]; + + if ( this.normalized ) y = denormalize( y, this.array ); + + return y; + + } + + getZ( index ) { + + let z = this.data.array[ index * this.data.stride + this.offset + 2 ]; + + if ( this.normalized ) z = denormalize( z, this.array ); + + return z; + + } + + getW( index ) { + + let w = this.data.array[ index * this.data.stride + this.offset + 3 ]; + + if ( this.normalized ) w = denormalize( w, this.array ); + + return w; + + } + + setXY( index, x, y ) { + + index = index * this.data.stride + this.offset; + + if ( this.normalized ) { + + x = normalize( x, this.array ); + y = normalize( y, this.array ); + + } + + this.data.array[ index + 0 ] = x; + this.data.array[ index + 1 ] = y; + + return this; + + } + + setXYZ( index, x, y, z ) { + + index = index * this.data.stride + this.offset; + + if ( this.normalized ) { + + x = normalize( x, this.array ); + y = normalize( y, this.array ); + z = normalize( z, this.array ); + + } + + this.data.array[ index + 0 ] = x; + this.data.array[ index + 1 ] = y; + this.data.array[ index + 2 ] = z; + + return this; + + } + + setXYZW( index, x, y, z, w ) { + + index = index * this.data.stride + this.offset; + + if ( this.normalized ) { + + x = normalize( x, this.array ); + y = normalize( y, this.array ); + z = normalize( z, this.array ); + w = normalize( w, this.array ); + + } + + this.data.array[ index + 0 ] = x; + this.data.array[ index + 1 ] = y; + this.data.array[ index + 2 ] = z; + this.data.array[ index + 3 ] = w; + + return this; + + } + + clone( data ) { + + if ( data === undefined ) { + + console.log( 'THREE.InterleavedBufferAttribute.clone(): Cloning an interleaved buffer attribute will de-interleave buffer data.' ); + + const array = []; + + for ( let i = 0; i < this.count; i ++ ) { + + const index = i * this.data.stride + this.offset; + + for ( let j = 0; j < this.itemSize; j ++ ) { + + array.push( this.data.array[ index + j ] ); + + } + + } + + return new BufferAttribute( new this.array.constructor( array ), this.itemSize, this.normalized ); + + } else { + + if ( data.interleavedBuffers === undefined ) { + + data.interleavedBuffers = {}; + + } + + if ( data.interleavedBuffers[ this.data.uuid ] === undefined ) { + + data.interleavedBuffers[ this.data.uuid ] = this.data.clone( data ); + + } + + return new InterleavedBufferAttribute( data.interleavedBuffers[ this.data.uuid ], this.itemSize, this.offset, this.normalized ); + + } + + } + + toJSON( data ) { + + if ( data === undefined ) { + + console.log( 'THREE.InterleavedBufferAttribute.toJSON(): Serializing an interleaved buffer attribute will de-interleave buffer data.' ); + + const array = []; + + for ( let i = 0; i < this.count; i ++ ) { + + const index = i * this.data.stride + this.offset; + + for ( let j = 0; j < this.itemSize; j ++ ) { + + array.push( this.data.array[ index + j ] ); + + } + + } + + // de-interleave data and save it as an ordinary buffer attribute for now + + return { + itemSize: this.itemSize, + type: this.array.constructor.name, + array: array, + normalized: this.normalized + }; + + } else { + + // save as true interleaved attribute + + if ( data.interleavedBuffers === undefined ) { + + data.interleavedBuffers = {}; + + } + + if ( data.interleavedBuffers[ this.data.uuid ] === undefined ) { + + data.interleavedBuffers[ this.data.uuid ] = this.data.toJSON( data ); + + } + + return { + isInterleavedBufferAttribute: true, + itemSize: this.itemSize, + data: this.data.uuid, + offset: this.offset, + normalized: this.normalized + }; + + } + + } + +} + +class SpriteMaterial extends Material { + + constructor( parameters ) { + + super(); + + this.isSpriteMaterial = true; + + this.type = 'SpriteMaterial'; + + this.color = new Color( 0xffffff ); + + this.map = null; + + this.alphaMap = null; + + this.rotation = 0; + + this.sizeAttenuation = true; + + this.transparent = true; + + this.fog = true; + + this.setValues( parameters ); + + } + + copy( source ) { + + super.copy( source ); + + this.color.copy( source.color ); + + this.map = source.map; + + this.alphaMap = source.alphaMap; + + this.rotation = source.rotation; + + this.sizeAttenuation = source.sizeAttenuation; + + this.fog = source.fog; + + return this; + + } + +} + +let _geometry; + +const _intersectPoint = /*@__PURE__*/ new Vector3(); +const _worldScale = /*@__PURE__*/ new Vector3(); +const _mvPosition = /*@__PURE__*/ new Vector3(); + +const _alignedPosition = /*@__PURE__*/ new Vector2(); +const _rotatedPosition = /*@__PURE__*/ new Vector2(); +const _viewWorldMatrix = /*@__PURE__*/ new Matrix4(); + +const _vA = /*@__PURE__*/ new Vector3(); +const _vB = /*@__PURE__*/ new Vector3(); +const _vC = /*@__PURE__*/ new Vector3(); + +const _uvA = /*@__PURE__*/ new Vector2(); +const _uvB = /*@__PURE__*/ new Vector2(); +const _uvC = /*@__PURE__*/ new Vector2(); + +class Sprite extends Object3D { + + constructor( material = new SpriteMaterial() ) { + + super(); + + this.isSprite = true; + + this.type = 'Sprite'; + + if ( _geometry === undefined ) { + + _geometry = new BufferGeometry(); + + const float32Array = new Float32Array( [ + - 0.5, - 0.5, 0, 0, 0, + 0.5, - 0.5, 0, 1, 0, + 0.5, 0.5, 0, 1, 1, + - 0.5, 0.5, 0, 0, 1 + ] ); + + const interleavedBuffer = new InterleavedBuffer( float32Array, 5 ); + + _geometry.setIndex( [ 0, 1, 2, 0, 2, 3 ] ); + _geometry.setAttribute( 'position', new InterleavedBufferAttribute( interleavedBuffer, 3, 0, false ) ); + _geometry.setAttribute( 'uv', new InterleavedBufferAttribute( interleavedBuffer, 2, 3, false ) ); + + } + + this.geometry = _geometry; + this.material = material; + + this.center = new Vector2( 0.5, 0.5 ); + + } + + raycast( raycaster, intersects ) { + + if ( raycaster.camera === null ) { + + console.error( 'THREE.Sprite: "Raycaster.camera" needs to be set in order to raycast against sprites.' ); + + } + + _worldScale.setFromMatrixScale( this.matrixWorld ); + + _viewWorldMatrix.copy( raycaster.camera.matrixWorld ); + this.modelViewMatrix.multiplyMatrices( raycaster.camera.matrixWorldInverse, this.matrixWorld ); + + _mvPosition.setFromMatrixPosition( this.modelViewMatrix ); + + if ( raycaster.camera.isPerspectiveCamera && this.material.sizeAttenuation === false ) { + + _worldScale.multiplyScalar( - _mvPosition.z ); + + } + + const rotation = this.material.rotation; + let sin, cos; + + if ( rotation !== 0 ) { + + cos = Math.cos( rotation ); + sin = Math.sin( rotation ); + + } + + const center = this.center; + + transformVertex( _vA.set( - 0.5, - 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos ); + transformVertex( _vB.set( 0.5, - 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos ); + transformVertex( _vC.set( 0.5, 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos ); + + _uvA.set( 0, 0 ); + _uvB.set( 1, 0 ); + _uvC.set( 1, 1 ); + + // check first triangle + let intersect = raycaster.ray.intersectTriangle( _vA, _vB, _vC, false, _intersectPoint ); + + if ( intersect === null ) { + + // check second triangle + transformVertex( _vB.set( - 0.5, 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos ); + _uvB.set( 0, 1 ); + + intersect = raycaster.ray.intersectTriangle( _vA, _vC, _vB, false, _intersectPoint ); + if ( intersect === null ) { + + return; + + } + + } + + const distance = raycaster.ray.origin.distanceTo( _intersectPoint ); + + if ( distance < raycaster.near || distance > raycaster.far ) return; + + intersects.push( { + + distance: distance, + point: _intersectPoint.clone(), + uv: Triangle.getInterpolation( _intersectPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2() ), + face: null, + object: this + + } ); + + } + + copy( source, recursive ) { + + super.copy( source, recursive ); + + if ( source.center !== undefined ) this.center.copy( source.center ); + + this.material = source.material; + + return this; + + } + +} + +function transformVertex( vertexPosition, mvPosition, center, scale, sin, cos ) { + + // compute position in camera space + _alignedPosition.subVectors( vertexPosition, center ).addScalar( 0.5 ).multiply( scale ); + + // to check if rotation is not zero + if ( sin !== undefined ) { + + _rotatedPosition.x = ( cos * _alignedPosition.x ) - ( sin * _alignedPosition.y ); + _rotatedPosition.y = ( sin * _alignedPosition.x ) + ( cos * _alignedPosition.y ); + + } else { + + _rotatedPosition.copy( _alignedPosition ); + + } + + + vertexPosition.copy( mvPosition ); + vertexPosition.x += _rotatedPosition.x; + vertexPosition.y += _rotatedPosition.y; + + // transform to world space + vertexPosition.applyMatrix4( _viewWorldMatrix ); + +} + +const _v1$2 = /*@__PURE__*/ new Vector3(); +const _v2$1 = /*@__PURE__*/ new Vector3(); + +class LOD extends Object3D { + + constructor() { + + super(); + + this._currentLevel = 0; + + this.type = 'LOD'; + + Object.defineProperties( this, { + levels: { + enumerable: true, + value: [] + }, + isLOD: { + value: true, + } + } ); + + this.autoUpdate = true; + + } + + copy( source ) { + + super.copy( source, false ); + + const levels = source.levels; + + for ( let i = 0, l = levels.length; i < l; i ++ ) { + + const level = levels[ i ]; + + this.addLevel( level.object.clone(), level.distance, level.hysteresis ); + + } + + this.autoUpdate = source.autoUpdate; + + return this; + + } + + addLevel( object, distance = 0, hysteresis = 0 ) { + + distance = Math.abs( distance ); + + const levels = this.levels; + + let l; + + for ( l = 0; l < levels.length; l ++ ) { + + if ( distance < levels[ l ].distance ) { + + break; + + } + + } + + levels.splice( l, 0, { distance: distance, hysteresis: hysteresis, object: object } ); + + this.add( object ); + + return this; + + } + + getCurrentLevel() { + + return this._currentLevel; + + } + + + + getObjectForDistance( distance ) { + + const levels = this.levels; + + if ( levels.length > 0 ) { + + let i, l; + + for ( i = 1, l = levels.length; i < l; i ++ ) { + + let levelDistance = levels[ i ].distance; + + if ( levels[ i ].object.visible ) { + + levelDistance -= levelDistance * levels[ i ].hysteresis; + + } + + if ( distance < levelDistance ) { + + break; + + } + + } + + return levels[ i - 1 ].object; + + } + + return null; + + } + + raycast( raycaster, intersects ) { + + const levels = this.levels; + + if ( levels.length > 0 ) { + + _v1$2.setFromMatrixPosition( this.matrixWorld ); + + const distance = raycaster.ray.origin.distanceTo( _v1$2 ); + + this.getObjectForDistance( distance ).raycast( raycaster, intersects ); + + } + + } + + update( camera ) { + + const levels = this.levels; + + if ( levels.length > 1 ) { + + _v1$2.setFromMatrixPosition( camera.matrixWorld ); + _v2$1.setFromMatrixPosition( this.matrixWorld ); + + const distance = _v1$2.distanceTo( _v2$1 ) / camera.zoom; + + levels[ 0 ].object.visible = true; + + let i, l; + + for ( i = 1, l = levels.length; i < l; i ++ ) { + + let levelDistance = levels[ i ].distance; + + if ( levels[ i ].object.visible ) { + + levelDistance -= levelDistance * levels[ i ].hysteresis; + + } + + if ( distance >= levelDistance ) { + + levels[ i - 1 ].object.visible = false; + levels[ i ].object.visible = true; + + } else { + + break; + + } + + } + + this._currentLevel = i - 1; + + for ( ; i < l; i ++ ) { + + levels[ i ].object.visible = false; + + } + + } + + } + + toJSON( meta ) { + + const data = super.toJSON( meta ); + + if ( this.autoUpdate === false ) data.object.autoUpdate = false; + + data.object.levels = []; + + const levels = this.levels; + + for ( let i = 0, l = levels.length; i < l; i ++ ) { + + const level = levels[ i ]; + + data.object.levels.push( { + object: level.object.uuid, + distance: level.distance, + hysteresis: level.hysteresis + } ); + + } + + return data; + + } + +} + +const _basePosition = /*@__PURE__*/ new Vector3(); + +const _skinIndex = /*@__PURE__*/ new Vector4(); +const _skinWeight = /*@__PURE__*/ new Vector4(); + +const _vector3 = /*@__PURE__*/ new Vector3(); +const _matrix4 = /*@__PURE__*/ new Matrix4(); +const _vertex = /*@__PURE__*/ new Vector3(); + +const _sphere$4 = /*@__PURE__*/ new Sphere(); +const _inverseMatrix$2 = /*@__PURE__*/ new Matrix4(); +const _ray$2 = /*@__PURE__*/ new Ray(); + +class SkinnedMesh extends Mesh { + + constructor( geometry, material ) { + + super( geometry, material ); + + this.isSkinnedMesh = true; + + this.type = 'SkinnedMesh'; + + this.bindMode = AttachedBindMode; + this.bindMatrix = new Matrix4(); + this.bindMatrixInverse = new Matrix4(); + + this.boundingBox = null; + this.boundingSphere = null; + + } + + computeBoundingBox() { + + const geometry = this.geometry; + + if ( this.boundingBox === null ) { + + this.boundingBox = new Box3(); + + } + + this.boundingBox.makeEmpty(); + + const positionAttribute = geometry.getAttribute( 'position' ); + + for ( let i = 0; i < positionAttribute.count; i ++ ) { + + this.getVertexPosition( i, _vertex ); + this.boundingBox.expandByPoint( _vertex ); + + } + + } + + computeBoundingSphere() { + + const geometry = this.geometry; + + if ( this.boundingSphere === null ) { + + this.boundingSphere = new Sphere(); + + } + + this.boundingSphere.makeEmpty(); + + const positionAttribute = geometry.getAttribute( 'position' ); + + for ( let i = 0; i < positionAttribute.count; i ++ ) { + + this.getVertexPosition( i, _vertex ); + this.boundingSphere.expandByPoint( _vertex ); + + } + + } + + copy( source, recursive ) { + + super.copy( source, recursive ); + + this.bindMode = source.bindMode; + this.bindMatrix.copy( source.bindMatrix ); + this.bindMatrixInverse.copy( source.bindMatrixInverse ); + + this.skeleton = source.skeleton; + + if ( source.boundingBox !== null ) this.boundingBox = source.boundingBox.clone(); + if ( source.boundingSphere !== null ) this.boundingSphere = source.boundingSphere.clone(); + + return this; + + } + + raycast( raycaster, intersects ) { + + const material = this.material; + const matrixWorld = this.matrixWorld; + + if ( material === undefined ) return; + + // test with bounding sphere in world space + + if ( this.boundingSphere === null ) this.computeBoundingSphere(); + + _sphere$4.copy( this.boundingSphere ); + _sphere$4.applyMatrix4( matrixWorld ); + + if ( raycaster.ray.intersectsSphere( _sphere$4 ) === false ) return; + + // convert ray to local space of skinned mesh + + _inverseMatrix$2.copy( matrixWorld ).invert(); + _ray$2.copy( raycaster.ray ).applyMatrix4( _inverseMatrix$2 ); + + // test with bounding box in local space + + if ( this.boundingBox !== null ) { + + if ( _ray$2.intersectsBox( this.boundingBox ) === false ) return; + + } + + // test for intersections with geometry + + this._computeIntersections( raycaster, intersects, _ray$2 ); + + } + + getVertexPosition( index, target ) { + + super.getVertexPosition( index, target ); + + this.applyBoneTransform( index, target ); + + return target; + + } + + bind( skeleton, bindMatrix ) { + + this.skeleton = skeleton; + + if ( bindMatrix === undefined ) { + + this.updateMatrixWorld( true ); + + this.skeleton.calculateInverses(); + + bindMatrix = this.matrixWorld; + + } + + this.bindMatrix.copy( bindMatrix ); + this.bindMatrixInverse.copy( bindMatrix ).invert(); + + } + + pose() { + + this.skeleton.pose(); + + } + + normalizeSkinWeights() { + + const vector = new Vector4(); + + const skinWeight = this.geometry.attributes.skinWeight; + + for ( let i = 0, l = skinWeight.count; i < l; i ++ ) { + + vector.fromBufferAttribute( skinWeight, i ); + + const scale = 1.0 / vector.manhattanLength(); + + if ( scale !== Infinity ) { + + vector.multiplyScalar( scale ); + + } else { + + vector.set( 1, 0, 0, 0 ); // do something reasonable + + } + + skinWeight.setXYZW( i, vector.x, vector.y, vector.z, vector.w ); + + } + + } + + updateMatrixWorld( force ) { + + super.updateMatrixWorld( force ); + + if ( this.bindMode === AttachedBindMode ) { + + this.bindMatrixInverse.copy( this.matrixWorld ).invert(); + + } else if ( this.bindMode === DetachedBindMode ) { + + this.bindMatrixInverse.copy( this.bindMatrix ).invert(); + + } else { + + console.warn( 'THREE.SkinnedMesh: Unrecognized bindMode: ' + this.bindMode ); + + } + + } + + applyBoneTransform( index, vector ) { + + const skeleton = this.skeleton; + const geometry = this.geometry; + + _skinIndex.fromBufferAttribute( geometry.attributes.skinIndex, index ); + _skinWeight.fromBufferAttribute( geometry.attributes.skinWeight, index ); + + _basePosition.copy( vector ).applyMatrix4( this.bindMatrix ); + + vector.set( 0, 0, 0 ); + + for ( let i = 0; i < 4; i ++ ) { + + const weight = _skinWeight.getComponent( i ); + + if ( weight !== 0 ) { + + const boneIndex = _skinIndex.getComponent( i ); + + _matrix4.multiplyMatrices( skeleton.bones[ boneIndex ].matrixWorld, skeleton.boneInverses[ boneIndex ] ); + + vector.addScaledVector( _vector3.copy( _basePosition ).applyMatrix4( _matrix4 ), weight ); + + } + + } + + return vector.applyMatrix4( this.bindMatrixInverse ); + + } + +} + +class Bone extends Object3D { + + constructor() { + + super(); + + this.isBone = true; + + this.type = 'Bone'; + + } + +} + +class DataTexture extends Texture { + + constructor( data = null, width = 1, height = 1, format, type, mapping, wrapS, wrapT, magFilter = NearestFilter, minFilter = NearestFilter, anisotropy, colorSpace ) { + + super( null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, colorSpace ); + + this.isDataTexture = true; + + this.image = { data: data, width: width, height: height }; + + this.generateMipmaps = false; + this.flipY = false; + this.unpackAlignment = 1; + + } + +} + +const _offsetMatrix = /*@__PURE__*/ new Matrix4(); +const _identityMatrix$1 = /*@__PURE__*/ new Matrix4(); + +class Skeleton { + + constructor( bones = [], boneInverses = [] ) { + + this.uuid = generateUUID(); + + this.bones = bones.slice( 0 ); + this.boneInverses = boneInverses; + this.boneMatrices = null; + + this.boneTexture = null; + + this.init(); + + } + + init() { + + const bones = this.bones; + const boneInverses = this.boneInverses; + + this.boneMatrices = new Float32Array( bones.length * 16 ); + + // calculate inverse bone matrices if necessary + + if ( boneInverses.length === 0 ) { + + this.calculateInverses(); + + } else { + + // handle special case + + if ( bones.length !== boneInverses.length ) { + + console.warn( 'THREE.Skeleton: Number of inverse bone matrices does not match amount of bones.' ); + + this.boneInverses = []; + + for ( let i = 0, il = this.bones.length; i < il; i ++ ) { + + this.boneInverses.push( new Matrix4() ); + + } + + } + + } + + } + + calculateInverses() { + + this.boneInverses.length = 0; + + for ( let i = 0, il = this.bones.length; i < il; i ++ ) { + + const inverse = new Matrix4(); + + if ( this.bones[ i ] ) { + + inverse.copy( this.bones[ i ].matrixWorld ).invert(); + + } + + this.boneInverses.push( inverse ); + + } + + } + + pose() { + + // recover the bind-time world matrices + + for ( let i = 0, il = this.bones.length; i < il; i ++ ) { + + const bone = this.bones[ i ]; + + if ( bone ) { + + bone.matrixWorld.copy( this.boneInverses[ i ] ).invert(); + + } + + } + + // compute the local matrices, positions, rotations and scales + + for ( let i = 0, il = this.bones.length; i < il; i ++ ) { + + const bone = this.bones[ i ]; + + if ( bone ) { + + if ( bone.parent && bone.parent.isBone ) { + + bone.matrix.copy( bone.parent.matrixWorld ).invert(); + bone.matrix.multiply( bone.matrixWorld ); + + } else { + + bone.matrix.copy( bone.matrixWorld ); + + } + + bone.matrix.decompose( bone.position, bone.quaternion, bone.scale ); + + } + + } + + } + + update() { + + const bones = this.bones; + const boneInverses = this.boneInverses; + const boneMatrices = this.boneMatrices; + const boneTexture = this.boneTexture; + + // flatten bone matrices to array + + for ( let i = 0, il = bones.length; i < il; i ++ ) { + + // compute the offset between the current and the original transform + + const matrix = bones[ i ] ? bones[ i ].matrixWorld : _identityMatrix$1; + + _offsetMatrix.multiplyMatrices( matrix, boneInverses[ i ] ); + _offsetMatrix.toArray( boneMatrices, i * 16 ); + + } + + if ( boneTexture !== null ) { + + boneTexture.needsUpdate = true; + + } + + } + + clone() { + + return new Skeleton( this.bones, this.boneInverses ); + + } + + computeBoneTexture() { + + // layout (1 matrix = 4 pixels) + // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4) + // with 8x8 pixel texture max 16 bones * 4 pixels = (8 * 8) + // 16x16 pixel texture max 64 bones * 4 pixels = (16 * 16) + // 32x32 pixel texture max 256 bones * 4 pixels = (32 * 32) + // 64x64 pixel texture max 1024 bones * 4 pixels = (64 * 64) + + let size = Math.sqrt( this.bones.length * 4 ); // 4 pixels needed for 1 matrix + size = Math.ceil( size / 4 ) * 4; + size = Math.max( size, 4 ); + + const boneMatrices = new Float32Array( size * size * 4 ); // 4 floats per RGBA pixel + boneMatrices.set( this.boneMatrices ); // copy current values + + const boneTexture = new DataTexture( boneMatrices, size, size, RGBAFormat, FloatType ); + boneTexture.needsUpdate = true; + + this.boneMatrices = boneMatrices; + this.boneTexture = boneTexture; + + return this; + + } + + getBoneByName( name ) { + + for ( let i = 0, il = this.bones.length; i < il; i ++ ) { + + const bone = this.bones[ i ]; + + if ( bone.name === name ) { + + return bone; + + } + + } + + return undefined; + + } + + dispose( ) { + + if ( this.boneTexture !== null ) { + + this.boneTexture.dispose(); + + this.boneTexture = null; + + } + + } + + fromJSON( json, bones ) { + + this.uuid = json.uuid; + + for ( let i = 0, l = json.bones.length; i < l; i ++ ) { + + const uuid = json.bones[ i ]; + let bone = bones[ uuid ]; + + if ( bone === undefined ) { + + console.warn( 'THREE.Skeleton: No bone found with UUID:', uuid ); + bone = new Bone(); + + } + + this.bones.push( bone ); + this.boneInverses.push( new Matrix4().fromArray( json.boneInverses[ i ] ) ); + + } + + this.init(); + + return this; + + } + + toJSON() { + + const data = { + metadata: { + version: 4.6, + type: 'Skeleton', + generator: 'Skeleton.toJSON' + }, + bones: [], + boneInverses: [] + }; + + data.uuid = this.uuid; + + const bones = this.bones; + const boneInverses = this.boneInverses; + + for ( let i = 0, l = bones.length; i < l; i ++ ) { + + const bone = bones[ i ]; + data.bones.push( bone.uuid ); + + const boneInverse = boneInverses[ i ]; + data.boneInverses.push( boneInverse.toArray() ); + + } + + return data; + + } + +} + +class InstancedBufferAttribute extends BufferAttribute { + + constructor( array, itemSize, normalized, meshPerAttribute = 1 ) { + + super( array, itemSize, normalized ); + + this.isInstancedBufferAttribute = true; + + this.meshPerAttribute = meshPerAttribute; + + } + + copy( source ) { + + super.copy( source ); + + this.meshPerAttribute = source.meshPerAttribute; + + return this; + + } + + toJSON() { + + const data = super.toJSON(); + + data.meshPerAttribute = this.meshPerAttribute; + + data.isInstancedBufferAttribute = true; + + return data; + + } + +} + +const _instanceLocalMatrix = /*@__PURE__*/ new Matrix4(); +const _instanceWorldMatrix = /*@__PURE__*/ new Matrix4(); + +const _instanceIntersects = []; + +const _box3 = /*@__PURE__*/ new Box3(); +const _identity = /*@__PURE__*/ new Matrix4(); +const _mesh$1 = /*@__PURE__*/ new Mesh(); +const _sphere$3 = /*@__PURE__*/ new Sphere(); + +class InstancedMesh extends Mesh { + + constructor( geometry, material, count ) { + + super( geometry, material ); + + this.isInstancedMesh = true; + + this.instanceMatrix = new InstancedBufferAttribute( new Float32Array( count * 16 ), 16 ); + this.instanceColor = null; + this.morphTexture = null; + + this.count = count; + + this.boundingBox = null; + this.boundingSphere = null; + + for ( let i = 0; i < count; i ++ ) { + + this.setMatrixAt( i, _identity ); + + } + + } + + computeBoundingBox() { + + const geometry = this.geometry; + const count = this.count; + + if ( this.boundingBox === null ) { + + this.boundingBox = new Box3(); + + } + + if ( geometry.boundingBox === null ) { + + geometry.computeBoundingBox(); + + } + + this.boundingBox.makeEmpty(); + + for ( let i = 0; i < count; i ++ ) { + + this.getMatrixAt( i, _instanceLocalMatrix ); + + _box3.copy( geometry.boundingBox ).applyMatrix4( _instanceLocalMatrix ); + + this.boundingBox.union( _box3 ); + + } + + } + + computeBoundingSphere() { + + const geometry = this.geometry; + const count = this.count; + + if ( this.boundingSphere === null ) { + + this.boundingSphere = new Sphere(); + + } + + if ( geometry.boundingSphere === null ) { + + geometry.computeBoundingSphere(); + + } + + this.boundingSphere.makeEmpty(); + + for ( let i = 0; i < count; i ++ ) { + + this.getMatrixAt( i, _instanceLocalMatrix ); + + _sphere$3.copy( geometry.boundingSphere ).applyMatrix4( _instanceLocalMatrix ); + + this.boundingSphere.union( _sphere$3 ); + + } + + } + + copy( source, recursive ) { + + super.copy( source, recursive ); + + this.instanceMatrix.copy( source.instanceMatrix ); + + if ( source.morphTexture !== null ) this.morphTexture = source.morphTexture.clone(); + if ( source.instanceColor !== null ) this.instanceColor = source.instanceColor.clone(); + + this.count = source.count; + + if ( source.boundingBox !== null ) this.boundingBox = source.boundingBox.clone(); + if ( source.boundingSphere !== null ) this.boundingSphere = source.boundingSphere.clone(); + + return this; + + } + + getColorAt( index, color ) { + + color.fromArray( this.instanceColor.array, index * 3 ); + + } + + getMatrixAt( index, matrix ) { + + matrix.fromArray( this.instanceMatrix.array, index * 16 ); + + } + + getMorphAt( index, object ) { + + const objectInfluences = object.morphTargetInfluences; + + const array = this.morphTexture.source.data.data; + + const len = objectInfluences.length + 1; // All influences + the baseInfluenceSum + + const dataIndex = index * len + 1; // Skip the baseInfluenceSum at the beginning + + for ( let i = 0; i < objectInfluences.length; i ++ ) { + + objectInfluences[ i ] = array[ dataIndex + i ]; + + } + + } + + raycast( raycaster, intersects ) { + + const matrixWorld = this.matrixWorld; + const raycastTimes = this.count; + + _mesh$1.geometry = this.geometry; + _mesh$1.material = this.material; + + if ( _mesh$1.material === undefined ) return; + + // test with bounding sphere first + + if ( this.boundingSphere === null ) this.computeBoundingSphere(); + + _sphere$3.copy( this.boundingSphere ); + _sphere$3.applyMatrix4( matrixWorld ); + + if ( raycaster.ray.intersectsSphere( _sphere$3 ) === false ) return; + + // now test each instance + + for ( let instanceId = 0; instanceId < raycastTimes; instanceId ++ ) { + + // calculate the world matrix for each instance + + this.getMatrixAt( instanceId, _instanceLocalMatrix ); + + _instanceWorldMatrix.multiplyMatrices( matrixWorld, _instanceLocalMatrix ); + + // the mesh represents this single instance + + _mesh$1.matrixWorld = _instanceWorldMatrix; + + _mesh$1.raycast( raycaster, _instanceIntersects ); + + // process the result of raycast + + for ( let i = 0, l = _instanceIntersects.length; i < l; i ++ ) { + + const intersect = _instanceIntersects[ i ]; + intersect.instanceId = instanceId; + intersect.object = this; + intersects.push( intersect ); + + } + + _instanceIntersects.length = 0; + + } + + } + + setColorAt( index, color ) { + + if ( this.instanceColor === null ) { + + this.instanceColor = new InstancedBufferAttribute( new Float32Array( this.instanceMatrix.count * 3 ), 3 ); + + } + + color.toArray( this.instanceColor.array, index * 3 ); + + } + + setMatrixAt( index, matrix ) { + + matrix.toArray( this.instanceMatrix.array, index * 16 ); + + } + + setMorphAt( index, object ) { + + const objectInfluences = object.morphTargetInfluences; + + const len = objectInfluences.length + 1; // morphBaseInfluence + all influences + + if ( this.morphTexture === null ) { + + this.morphTexture = new DataTexture( new Float32Array( len * this.count ), len, this.count, RedFormat, FloatType ); + + } + + const array = this.morphTexture.source.data.data; + + let morphInfluencesSum = 0; + + for ( let i = 0; i < objectInfluences.length; i ++ ) { + + morphInfluencesSum += objectInfluences[ i ]; + + } + + const morphBaseInfluence = this.geometry.morphTargetsRelative ? 1 : 1 - morphInfluencesSum; + + const dataIndex = len * index; + + array[ dataIndex ] = morphBaseInfluence; + + array.set( objectInfluences, dataIndex + 1 ); + + } + + updateMorphTargets() { + + } + + dispose() { + + this.dispatchEvent( { type: 'dispose' } ); + + if ( this.morphTexture !== null ) { + + this.morphTexture.dispose(); + this.morphTexture = null; + + } + + return this; + + } + +} + +function sortOpaque( a, b ) { + + return a.z - b.z; + +} + +function sortTransparent( a, b ) { + + return b.z - a.z; + +} + +class MultiDrawRenderList { + + constructor() { + + this.index = 0; + this.pool = []; + this.list = []; + + } + + push( drawRange, z ) { + + const pool = this.pool; + const list = this.list; + if ( this.index >= pool.length ) { + + pool.push( { + + start: - 1, + count: - 1, + z: - 1, + + } ); + + } + + const item = pool[ this.index ]; + list.push( item ); + this.index ++; + + item.start = drawRange.start; + item.count = drawRange.count; + item.z = z; + + } + + reset() { + + this.list.length = 0; + this.index = 0; + + } + +} + +const ID_ATTR_NAME = 'batchId'; +const _matrix$1 = /*@__PURE__*/ new Matrix4(); +const _invMatrixWorld = /*@__PURE__*/ new Matrix4(); +const _identityMatrix = /*@__PURE__*/ new Matrix4(); +const _projScreenMatrix$2 = /*@__PURE__*/ new Matrix4(); +const _frustum = /*@__PURE__*/ new Frustum(); +const _box$1 = /*@__PURE__*/ new Box3(); +const _sphere$2 = /*@__PURE__*/ new Sphere(); +const _vector$5 = /*@__PURE__*/ new Vector3(); +const _renderList = /*@__PURE__*/ new MultiDrawRenderList(); +const _mesh = /*@__PURE__*/ new Mesh(); +const _batchIntersects = []; + +// @TODO: SkinnedMesh support? +// @TODO: geometry.groups support? +// @TODO: geometry.drawRange support? +// @TODO: geometry.morphAttributes support? +// @TODO: Support uniform parameter per geometry +// @TODO: Add an "optimize" function to pack geometry and remove data gaps + +// copies data from attribute "src" into "target" starting at "targetOffset" +function copyAttributeData( src, target, targetOffset = 0 ) { + + const itemSize = target.itemSize; + if ( src.isInterleavedBufferAttribute || src.array.constructor !== target.array.constructor ) { + + // use the component getters and setters if the array data cannot + // be copied directly + const vertexCount = src.count; + for ( let i = 0; i < vertexCount; i ++ ) { + + for ( let c = 0; c < itemSize; c ++ ) { + + target.setComponent( i + targetOffset, c, src.getComponent( i, c ) ); + + } + + } + + } else { + + // faster copy approach using typed array set function + target.array.set( src.array, targetOffset * itemSize ); + + } + + target.needsUpdate = true; + +} + +class BatchedMesh extends Mesh { + + get maxGeometryCount() { + + return this._maxGeometryCount; + + } + + constructor( maxGeometryCount, maxVertexCount, maxIndexCount = maxVertexCount * 2, material ) { + + super( new BufferGeometry(), material ); + + this.isBatchedMesh = true; + this.perObjectFrustumCulled = true; + this.sortObjects = true; + this.boundingBox = null; + this.boundingSphere = null; + this.customSort = null; + + this._drawRanges = []; + this._reservedRanges = []; + + this._visibility = []; + this._active = []; + this._bounds = []; + + this._maxGeometryCount = maxGeometryCount; + this._maxVertexCount = maxVertexCount; + this._maxIndexCount = maxIndexCount; + + this._geometryInitialized = false; + this._geometryCount = 0; + this._multiDrawCounts = new Int32Array( maxGeometryCount ); + this._multiDrawStarts = new Int32Array( maxGeometryCount ); + this._multiDrawCount = 0; + this._multiDrawInstances = null; + this._visibilityChanged = true; + + // Local matrix per geometry by using data texture + this._matricesTexture = null; + + this._initMatricesTexture(); + + } + + _initMatricesTexture() { + + // layout (1 matrix = 4 pixels) + // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4) + // with 8x8 pixel texture max 16 matrices * 4 pixels = (8 * 8) + // 16x16 pixel texture max 64 matrices * 4 pixels = (16 * 16) + // 32x32 pixel texture max 256 matrices * 4 pixels = (32 * 32) + // 64x64 pixel texture max 1024 matrices * 4 pixels = (64 * 64) + + let size = Math.sqrt( this._maxGeometryCount * 4 ); // 4 pixels needed for 1 matrix + size = Math.ceil( size / 4 ) * 4; + size = Math.max( size, 4 ); + + const matricesArray = new Float32Array( size * size * 4 ); // 4 floats per RGBA pixel + const matricesTexture = new DataTexture( matricesArray, size, size, RGBAFormat, FloatType ); + + this._matricesTexture = matricesTexture; + + } + + _initializeGeometry( reference ) { + + const geometry = this.geometry; + const maxVertexCount = this._maxVertexCount; + const maxGeometryCount = this._maxGeometryCount; + const maxIndexCount = this._maxIndexCount; + if ( this._geometryInitialized === false ) { + + for ( const attributeName in reference.attributes ) { + + const srcAttribute = reference.getAttribute( attributeName ); + const { array, itemSize, normalized } = srcAttribute; + + const dstArray = new array.constructor( maxVertexCount * itemSize ); + const dstAttribute = new BufferAttribute( dstArray, itemSize, normalized ); + + geometry.setAttribute( attributeName, dstAttribute ); + + } + + if ( reference.getIndex() !== null ) { + + const indexArray = maxVertexCount > 65536 + ? new Uint32Array( maxIndexCount ) + : new Uint16Array( maxIndexCount ); + + geometry.setIndex( new BufferAttribute( indexArray, 1 ) ); + + } + + const idArray = maxGeometryCount > 65536 + ? new Uint32Array( maxVertexCount ) + : new Uint16Array( maxVertexCount ); + geometry.setAttribute( ID_ATTR_NAME, new BufferAttribute( idArray, 1 ) ); + + this._geometryInitialized = true; + + } + + } + + // Make sure the geometry is compatible with the existing combined geometry attributes + _validateGeometry( geometry ) { + + // check that the geometry doesn't have a version of our reserved id attribute + if ( geometry.getAttribute( ID_ATTR_NAME ) ) { + + throw new Error( `BatchedMesh: Geometry cannot use attribute "${ ID_ATTR_NAME }"` ); + + } + + // check to ensure the geometries are using consistent attributes and indices + const batchGeometry = this.geometry; + if ( Boolean( geometry.getIndex() ) !== Boolean( batchGeometry.getIndex() ) ) { + + throw new Error( 'BatchedMesh: All geometries must consistently have "index".' ); + + } + + for ( const attributeName in batchGeometry.attributes ) { + + if ( attributeName === ID_ATTR_NAME ) { + + continue; + + } + + if ( ! geometry.hasAttribute( attributeName ) ) { + + throw new Error( `BatchedMesh: Added geometry missing "${ attributeName }". All geometries must have consistent attributes.` ); + + } + + const srcAttribute = geometry.getAttribute( attributeName ); + const dstAttribute = batchGeometry.getAttribute( attributeName ); + if ( srcAttribute.itemSize !== dstAttribute.itemSize || srcAttribute.normalized !== dstAttribute.normalized ) { + + throw new Error( 'BatchedMesh: All attributes must have a consistent itemSize and normalized value.' ); + + } + + } + + } + + setCustomSort( func ) { + + this.customSort = func; + return this; + + } + + computeBoundingBox() { + + if ( this.boundingBox === null ) { + + this.boundingBox = new Box3(); + + } + + const geometryCount = this._geometryCount; + const boundingBox = this.boundingBox; + const active = this._active; + + boundingBox.makeEmpty(); + for ( let i = 0; i < geometryCount; i ++ ) { + + if ( active[ i ] === false ) continue; + + this.getMatrixAt( i, _matrix$1 ); + this.getBoundingBoxAt( i, _box$1 ).applyMatrix4( _matrix$1 ); + boundingBox.union( _box$1 ); + + } + + } + + computeBoundingSphere() { + + if ( this.boundingSphere === null ) { + + this.boundingSphere = new Sphere(); + + } + + const geometryCount = this._geometryCount; + const boundingSphere = this.boundingSphere; + const active = this._active; + + boundingSphere.makeEmpty(); + for ( let i = 0; i < geometryCount; i ++ ) { + + if ( active[ i ] === false ) continue; + + this.getMatrixAt( i, _matrix$1 ); + this.getBoundingSphereAt( i, _sphere$2 ).applyMatrix4( _matrix$1 ); + boundingSphere.union( _sphere$2 ); + + } + + } + + addGeometry( geometry, vertexCount = - 1, indexCount = - 1 ) { + + this._initializeGeometry( geometry ); + + this._validateGeometry( geometry ); + + // ensure we're not over geometry + if ( this._geometryCount >= this._maxGeometryCount ) { + + throw new Error( 'BatchedMesh: Maximum geometry count reached.' ); + + } + + // get the necessary range fo the geometry + const reservedRange = { + vertexStart: - 1, + vertexCount: - 1, + indexStart: - 1, + indexCount: - 1, + }; + + let lastRange = null; + const reservedRanges = this._reservedRanges; + const drawRanges = this._drawRanges; + const bounds = this._bounds; + if ( this._geometryCount !== 0 ) { + + lastRange = reservedRanges[ reservedRanges.length - 1 ]; + + } + + if ( vertexCount === - 1 ) { + + reservedRange.vertexCount = geometry.getAttribute( 'position' ).count; + + } else { + + reservedRange.vertexCount = vertexCount; + + } + + if ( lastRange === null ) { + + reservedRange.vertexStart = 0; + + } else { + + reservedRange.vertexStart = lastRange.vertexStart + lastRange.vertexCount; + + } + + const index = geometry.getIndex(); + const hasIndex = index !== null; + if ( hasIndex ) { + + if ( indexCount === - 1 ) { + + reservedRange.indexCount = index.count; + + } else { + + reservedRange.indexCount = indexCount; + + } + + if ( lastRange === null ) { + + reservedRange.indexStart = 0; + + } else { + + reservedRange.indexStart = lastRange.indexStart + lastRange.indexCount; + + } + + } + + if ( + reservedRange.indexStart !== - 1 && + reservedRange.indexStart + reservedRange.indexCount > this._maxIndexCount || + reservedRange.vertexStart + reservedRange.vertexCount > this._maxVertexCount + ) { + + throw new Error( 'BatchedMesh: Reserved space request exceeds the maximum buffer size.' ); + + } + + const visibility = this._visibility; + const active = this._active; + const matricesTexture = this._matricesTexture; + const matricesArray = this._matricesTexture.image.data; + + // push new visibility states + visibility.push( true ); + active.push( true ); + + // update id + const geometryId = this._geometryCount; + this._geometryCount ++; + + // initialize matrix information + _identityMatrix.toArray( matricesArray, geometryId * 16 ); + matricesTexture.needsUpdate = true; + + // add the reserved range and draw range objects + reservedRanges.push( reservedRange ); + drawRanges.push( { + start: hasIndex ? reservedRange.indexStart : reservedRange.vertexStart, + count: - 1 + } ); + bounds.push( { + boxInitialized: false, + box: new Box3(), + + sphereInitialized: false, + sphere: new Sphere() + } ); + + // set the id for the geometry + const idAttribute = this.geometry.getAttribute( ID_ATTR_NAME ); + for ( let i = 0; i < reservedRange.vertexCount; i ++ ) { + + idAttribute.setX( reservedRange.vertexStart + i, geometryId ); + + } + + idAttribute.needsUpdate = true; + + // update the geometry + this.setGeometryAt( geometryId, geometry ); + + return geometryId; + + } + + setGeometryAt( id, geometry ) { + + if ( id >= this._geometryCount ) { + + throw new Error( 'BatchedMesh: Maximum geometry count reached.' ); + + } + + this._validateGeometry( geometry ); + + const batchGeometry = this.geometry; + const hasIndex = batchGeometry.getIndex() !== null; + const dstIndex = batchGeometry.getIndex(); + const srcIndex = geometry.getIndex(); + const reservedRange = this._reservedRanges[ id ]; + if ( + hasIndex && + srcIndex.count > reservedRange.indexCount || + geometry.attributes.position.count > reservedRange.vertexCount + ) { + + throw new Error( 'BatchedMesh: Reserved space not large enough for provided geometry.' ); + + } + + // copy geometry over + const vertexStart = reservedRange.vertexStart; + const vertexCount = reservedRange.vertexCount; + for ( const attributeName in batchGeometry.attributes ) { + + if ( attributeName === ID_ATTR_NAME ) { + + continue; + + } + + // copy attribute data + const srcAttribute = geometry.getAttribute( attributeName ); + const dstAttribute = batchGeometry.getAttribute( attributeName ); + copyAttributeData( srcAttribute, dstAttribute, vertexStart ); + + // fill the rest in with zeroes + const itemSize = srcAttribute.itemSize; + for ( let i = srcAttribute.count, l = vertexCount; i < l; i ++ ) { + + const index = vertexStart + i; + for ( let c = 0; c < itemSize; c ++ ) { + + dstAttribute.setComponent( index, c, 0 ); + + } + + } + + dstAttribute.needsUpdate = true; + dstAttribute.addUpdateRange( vertexStart * itemSize, vertexCount * itemSize ); + + } + + // copy index + if ( hasIndex ) { + + const indexStart = reservedRange.indexStart; + + // copy index data over + for ( let i = 0; i < srcIndex.count; i ++ ) { + + dstIndex.setX( indexStart + i, vertexStart + srcIndex.getX( i ) ); + + } + + // fill the rest in with zeroes + for ( let i = srcIndex.count, l = reservedRange.indexCount; i < l; i ++ ) { + + dstIndex.setX( indexStart + i, vertexStart ); + + } + + dstIndex.needsUpdate = true; + dstIndex.addUpdateRange( indexStart, reservedRange.indexCount ); + + } + + // store the bounding boxes + const bound = this._bounds[ id ]; + if ( geometry.boundingBox !== null ) { + + bound.box.copy( geometry.boundingBox ); + bound.boxInitialized = true; + + } else { + + bound.boxInitialized = false; + + } + + if ( geometry.boundingSphere !== null ) { + + bound.sphere.copy( geometry.boundingSphere ); + bound.sphereInitialized = true; + + } else { + + bound.sphereInitialized = false; + + } + + // set drawRange count + const drawRange = this._drawRanges[ id ]; + const posAttr = geometry.getAttribute( 'position' ); + drawRange.count = hasIndex ? srcIndex.count : posAttr.count; + this._visibilityChanged = true; + + return id; + + } + + deleteGeometry( geometryId ) { + + // Note: User needs to call optimize() afterward to pack the data. + + const active = this._active; + if ( geometryId >= active.length || active[ geometryId ] === false ) { + + return this; + + } + + active[ geometryId ] = false; + this._visibilityChanged = true; + + return this; + + } + + getInstanceCountAt( id ) { + + if ( this._multiDrawInstances === null ) return null; + + return this._multiDrawInstances[ id ]; + + } + + setInstanceCountAt( id, instanceCount ) { + + if ( this._multiDrawInstances === null ) { + + this._multiDrawInstances = new Int32Array( this._maxGeometryCount ).fill( 1 ); + + } + + this._multiDrawInstances[ id ] = instanceCount; + + return id; + + } + + // get bounding box and compute it if it doesn't exist + getBoundingBoxAt( id, target ) { + + const active = this._active; + if ( active[ id ] === false ) { + + return null; + + } + + // compute bounding box + const bound = this._bounds[ id ]; + const box = bound.box; + const geometry = this.geometry; + if ( bound.boxInitialized === false ) { + + box.makeEmpty(); + + const index = geometry.index; + const position = geometry.attributes.position; + const drawRange = this._drawRanges[ id ]; + for ( let i = drawRange.start, l = drawRange.start + drawRange.count; i < l; i ++ ) { + + let iv = i; + if ( index ) { + + iv = index.getX( iv ); + + } + + box.expandByPoint( _vector$5.fromBufferAttribute( position, iv ) ); + + } + + bound.boxInitialized = true; + + } + + target.copy( box ); + return target; + + } + + // get bounding sphere and compute it if it doesn't exist + getBoundingSphereAt( id, target ) { + + const active = this._active; + if ( active[ id ] === false ) { + + return null; + + } + + // compute bounding sphere + const bound = this._bounds[ id ]; + const sphere = bound.sphere; + const geometry = this.geometry; + if ( bound.sphereInitialized === false ) { + + sphere.makeEmpty(); + + this.getBoundingBoxAt( id, _box$1 ); + _box$1.getCenter( sphere.center ); + + const index = geometry.index; + const position = geometry.attributes.position; + const drawRange = this._drawRanges[ id ]; + + let maxRadiusSq = 0; + for ( let i = drawRange.start, l = drawRange.start + drawRange.count; i < l; i ++ ) { + + let iv = i; + if ( index ) { + + iv = index.getX( iv ); + + } + + _vector$5.fromBufferAttribute( position, iv ); + maxRadiusSq = Math.max( maxRadiusSq, sphere.center.distanceToSquared( _vector$5 ) ); + + } + + sphere.radius = Math.sqrt( maxRadiusSq ); + bound.sphereInitialized = true; + + } + + target.copy( sphere ); + return target; + + } + + setMatrixAt( geometryId, matrix ) { + + // @TODO: Map geometryId to index of the arrays because + // optimize() can make geometryId mismatch the index + + const active = this._active; + const matricesTexture = this._matricesTexture; + const matricesArray = this._matricesTexture.image.data; + const geometryCount = this._geometryCount; + if ( geometryId >= geometryCount || active[ geometryId ] === false ) { + + return this; + + } + + matrix.toArray( matricesArray, geometryId * 16 ); + matricesTexture.needsUpdate = true; + + return this; + + } + + getMatrixAt( geometryId, matrix ) { + + const active = this._active; + const matricesArray = this._matricesTexture.image.data; + const geometryCount = this._geometryCount; + if ( geometryId >= geometryCount || active[ geometryId ] === false ) { + + return null; + + } + + return matrix.fromArray( matricesArray, geometryId * 16 ); + + } + + setVisibleAt( geometryId, value ) { + + const visibility = this._visibility; + const active = this._active; + const geometryCount = this._geometryCount; + + // if the geometry is out of range, not active, or visibility state + // does not change then return early + if ( + geometryId >= geometryCount || + active[ geometryId ] === false || + visibility[ geometryId ] === value + ) { + + return this; + + } + + visibility[ geometryId ] = value; + this._visibilityChanged = true; + + return this; + + } + + getVisibleAt( geometryId ) { + + const visibility = this._visibility; + const active = this._active; + const geometryCount = this._geometryCount; + + // return early if the geometry is out of range or not active + if ( geometryId >= geometryCount || active[ geometryId ] === false ) { + + return false; + + } + + return visibility[ geometryId ]; + + } + + raycast( raycaster, intersects ) { + + const visibility = this._visibility; + const active = this._active; + const drawRanges = this._drawRanges; + const geometryCount = this._geometryCount; + const matrixWorld = this.matrixWorld; + const batchGeometry = this.geometry; + + // iterate over each geometry + _mesh.material = this.material; + _mesh.geometry.index = batchGeometry.index; + _mesh.geometry.attributes = batchGeometry.attributes; + if ( _mesh.geometry.boundingBox === null ) { + + _mesh.geometry.boundingBox = new Box3(); + + } + + if ( _mesh.geometry.boundingSphere === null ) { + + _mesh.geometry.boundingSphere = new Sphere(); + + } + + for ( let i = 0; i < geometryCount; i ++ ) { + + if ( ! visibility[ i ] || ! active[ i ] ) { + + continue; + + } + + const drawRange = drawRanges[ i ]; + _mesh.geometry.setDrawRange( drawRange.start, drawRange.count ); + + // ge the intersects + this.getMatrixAt( i, _mesh.matrixWorld ).premultiply( matrixWorld ); + this.getBoundingBoxAt( i, _mesh.geometry.boundingBox ); + this.getBoundingSphereAt( i, _mesh.geometry.boundingSphere ); + _mesh.raycast( raycaster, _batchIntersects ); + + // add batch id to the intersects + for ( let j = 0, l = _batchIntersects.length; j < l; j ++ ) { + + const intersect = _batchIntersects[ j ]; + intersect.object = this; + intersect.batchId = i; + intersects.push( intersect ); + + } + + _batchIntersects.length = 0; + + } + + _mesh.material = null; + _mesh.geometry.index = null; + _mesh.geometry.attributes = {}; + _mesh.geometry.setDrawRange( 0, Infinity ); + + } + + copy( source ) { + + super.copy( source ); + + this.geometry = source.geometry.clone(); + this.perObjectFrustumCulled = source.perObjectFrustumCulled; + this.sortObjects = source.sortObjects; + this.boundingBox = source.boundingBox !== null ? source.boundingBox.clone() : null; + this.boundingSphere = source.boundingSphere !== null ? source.boundingSphere.clone() : null; + + this._drawRanges = source._drawRanges.map( range => ( { ...range } ) ); + this._reservedRanges = source._reservedRanges.map( range => ( { ...range } ) ); + + this._visibility = source._visibility.slice(); + this._active = source._active.slice(); + this._bounds = source._bounds.map( bound => ( { + boxInitialized: bound.boxInitialized, + box: bound.box.clone(), + + sphereInitialized: bound.sphereInitialized, + sphere: bound.sphere.clone() + } ) ); + + this._maxGeometryCount = source._maxGeometryCount; + this._maxVertexCount = source._maxVertexCount; + this._maxIndexCount = source._maxIndexCount; + + this._geometryInitialized = source._geometryInitialized; + this._geometryCount = source._geometryCount; + this._multiDrawCounts = source._multiDrawCounts.slice(); + this._multiDrawStarts = source._multiDrawStarts.slice(); + + this._matricesTexture = source._matricesTexture.clone(); + this._matricesTexture.image.data = this._matricesTexture.image.slice(); + + return this; + + } + + dispose() { + + // Assuming the geometry is not shared with other meshes + this.geometry.dispose(); + + this._matricesTexture.dispose(); + this._matricesTexture = null; + return this; + + } + + onBeforeRender( renderer, scene, camera, geometry, material/*, _group*/ ) { + + // if visibility has not changed and frustum culling and object sorting is not required + // then skip iterating over all items + if ( ! this._visibilityChanged && ! this.perObjectFrustumCulled && ! this.sortObjects ) { + + return; + + } + + // the indexed version of the multi draw function requires specifying the start + // offset in bytes. + const index = geometry.getIndex(); + const bytesPerElement = index === null ? 1 : index.array.BYTES_PER_ELEMENT; + + const active = this._active; + const visibility = this._visibility; + const multiDrawStarts = this._multiDrawStarts; + const multiDrawCounts = this._multiDrawCounts; + const drawRanges = this._drawRanges; + const perObjectFrustumCulled = this.perObjectFrustumCulled; + + // prepare the frustum in the local frame + if ( perObjectFrustumCulled ) { + + _projScreenMatrix$2 + .multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse ) + .multiply( this.matrixWorld ); + _frustum.setFromProjectionMatrix( + _projScreenMatrix$2, + renderer.coordinateSystem + ); + + } + + let count = 0; + if ( this.sortObjects ) { + + // get the camera position in the local frame + _invMatrixWorld.copy( this.matrixWorld ).invert(); + _vector$5.setFromMatrixPosition( camera.matrixWorld ).applyMatrix4( _invMatrixWorld ); + + for ( let i = 0, l = visibility.length; i < l; i ++ ) { + + if ( visibility[ i ] && active[ i ] ) { + + // get the bounds in world space + this.getMatrixAt( i, _matrix$1 ); + this.getBoundingSphereAt( i, _sphere$2 ).applyMatrix4( _matrix$1 ); + + // determine whether the batched geometry is within the frustum + let culled = false; + if ( perObjectFrustumCulled ) { + + culled = ! _frustum.intersectsSphere( _sphere$2 ); + + } + + if ( ! culled ) { + + // get the distance from camera used for sorting + const z = _vector$5.distanceTo( _sphere$2.center ); + _renderList.push( drawRanges[ i ], z ); + + } + + } + + } + + // Sort the draw ranges and prep for rendering + const list = _renderList.list; + const customSort = this.customSort; + if ( customSort === null ) { + + list.sort( material.transparent ? sortTransparent : sortOpaque ); + + } else { + + customSort.call( this, list, camera ); + + } + + for ( let i = 0, l = list.length; i < l; i ++ ) { + + const item = list[ i ]; + multiDrawStarts[ count ] = item.start * bytesPerElement; + multiDrawCounts[ count ] = item.count; + count ++; + + } + + _renderList.reset(); + + } else { + + for ( let i = 0, l = visibility.length; i < l; i ++ ) { + + if ( visibility[ i ] && active[ i ] ) { + + // determine whether the batched geometry is within the frustum + let culled = false; + if ( perObjectFrustumCulled ) { + + // get the bounds in world space + this.getMatrixAt( i, _matrix$1 ); + this.getBoundingSphereAt( i, _sphere$2 ).applyMatrix4( _matrix$1 ); + culled = ! _frustum.intersectsSphere( _sphere$2 ); + + } + + if ( ! culled ) { + + const range = drawRanges[ i ]; + multiDrawStarts[ count ] = range.start * bytesPerElement; + multiDrawCounts[ count ] = range.count; + count ++; + + } + + } + + } + + } + + this._multiDrawCount = count; + this._visibilityChanged = false; + + } + + onBeforeShadow( renderer, object, camera, shadowCamera, geometry, depthMaterial/* , group */ ) { + + this.onBeforeRender( renderer, null, shadowCamera, geometry, depthMaterial ); + + } + +} + +class LineBasicMaterial extends Material { + + constructor( parameters ) { + + super(); + + this.isLineBasicMaterial = true; + + this.type = 'LineBasicMaterial'; + + this.color = new Color( 0xffffff ); + + this.map = null; + + this.linewidth = 1; + this.linecap = 'round'; + this.linejoin = 'round'; + + this.fog = true; + + this.setValues( parameters ); + + } + + + copy( source ) { + + super.copy( source ); + + this.color.copy( source.color ); + + this.map = source.map; + + this.linewidth = source.linewidth; + this.linecap = source.linecap; + this.linejoin = source.linejoin; + + this.fog = source.fog; + + return this; + + } + +} + +const _vStart = /*@__PURE__*/ new Vector3(); +const _vEnd = /*@__PURE__*/ new Vector3(); + +const _inverseMatrix$1 = /*@__PURE__*/ new Matrix4(); +const _ray$1 = /*@__PURE__*/ new Ray(); +const _sphere$1 = /*@__PURE__*/ new Sphere(); + +const _intersectPointOnRay = /*@__PURE__*/ new Vector3(); +const _intersectPointOnSegment = /*@__PURE__*/ new Vector3(); + +class Line extends Object3D { + + constructor( geometry = new BufferGeometry(), material = new LineBasicMaterial() ) { + + super(); + + this.isLine = true; + + this.type = 'Line'; + + this.geometry = geometry; + this.material = material; + + this.updateMorphTargets(); + + } + + copy( source, recursive ) { + + super.copy( source, recursive ); + + this.material = Array.isArray( source.material ) ? source.material.slice() : source.material; + this.geometry = source.geometry; + + return this; + + } + + computeLineDistances() { + + const geometry = this.geometry; + + // we assume non-indexed geometry + + if ( geometry.index === null ) { + + const positionAttribute = geometry.attributes.position; + const lineDistances = [ 0 ]; + + for ( let i = 1, l = positionAttribute.count; i < l; i ++ ) { + + _vStart.fromBufferAttribute( positionAttribute, i - 1 ); + _vEnd.fromBufferAttribute( positionAttribute, i ); + + lineDistances[ i ] = lineDistances[ i - 1 ]; + lineDistances[ i ] += _vStart.distanceTo( _vEnd ); + + } + + geometry.setAttribute( 'lineDistance', new Float32BufferAttribute( lineDistances, 1 ) ); + + } else { + + console.warn( 'THREE.Line.computeLineDistances(): Computation only possible with non-indexed BufferGeometry.' ); + + } + + return this; + + } + + raycast( raycaster, intersects ) { + + const geometry = this.geometry; + const matrixWorld = this.matrixWorld; + const threshold = raycaster.params.Line.threshold; + const drawRange = geometry.drawRange; + + // Checking boundingSphere distance to ray + + if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere(); + + _sphere$1.copy( geometry.boundingSphere ); + _sphere$1.applyMatrix4( matrixWorld ); + _sphere$1.radius += threshold; + + if ( raycaster.ray.intersectsSphere( _sphere$1 ) === false ) return; + + // + + _inverseMatrix$1.copy( matrixWorld ).invert(); + _ray$1.copy( raycaster.ray ).applyMatrix4( _inverseMatrix$1 ); + + const localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 ); + const localThresholdSq = localThreshold * localThreshold; + + const step = this.isLineSegments ? 2 : 1; + + const index = geometry.index; + const attributes = geometry.attributes; + const positionAttribute = attributes.position; + + if ( index !== null ) { + + const start = Math.max( 0, drawRange.start ); + const end = Math.min( index.count, ( drawRange.start + drawRange.count ) ); + + for ( let i = start, l = end - 1; i < l; i += step ) { + + const a = index.getX( i ); + const b = index.getX( i + 1 ); + + const intersect = checkIntersection( this, raycaster, _ray$1, localThresholdSq, a, b ); + + if ( intersect ) { + + intersects.push( intersect ); + + } + + } + + if ( this.isLineLoop ) { + + const a = index.getX( end - 1 ); + const b = index.getX( start ); + + const intersect = checkIntersection( this, raycaster, _ray$1, localThresholdSq, a, b ); + + if ( intersect ) { + + intersects.push( intersect ); + + } + + } + + } else { + + const start = Math.max( 0, drawRange.start ); + const end = Math.min( positionAttribute.count, ( drawRange.start + drawRange.count ) ); + + for ( let i = start, l = end - 1; i < l; i += step ) { + + const intersect = checkIntersection( this, raycaster, _ray$1, localThresholdSq, i, i + 1 ); + + if ( intersect ) { + + intersects.push( intersect ); + + } + + } + + if ( this.isLineLoop ) { + + const intersect = checkIntersection( this, raycaster, _ray$1, localThresholdSq, end - 1, start ); + + if ( intersect ) { + + intersects.push( intersect ); + + } + + } + + } + + } + + updateMorphTargets() { + + const geometry = this.geometry; + + const morphAttributes = geometry.morphAttributes; + const keys = Object.keys( morphAttributes ); + + if ( keys.length > 0 ) { + + const morphAttribute = morphAttributes[ keys[ 0 ] ]; + + if ( morphAttribute !== undefined ) { + + this.morphTargetInfluences = []; + this.morphTargetDictionary = {}; + + for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) { + + const name = morphAttribute[ m ].name || String( m ); + + this.morphTargetInfluences.push( 0 ); + this.morphTargetDictionary[ name ] = m; + + } + + } + + } + + } + +} + +function checkIntersection( object, raycaster, ray, thresholdSq, a, b ) { + + const positionAttribute = object.geometry.attributes.position; + + _vStart.fromBufferAttribute( positionAttribute, a ); + _vEnd.fromBufferAttribute( positionAttribute, b ); + + const distSq = ray.distanceSqToSegment( _vStart, _vEnd, _intersectPointOnRay, _intersectPointOnSegment ); + + if ( distSq > thresholdSq ) return; + + _intersectPointOnRay.applyMatrix4( object.matrixWorld ); // Move back to world space for distance calculation + + const distance = raycaster.ray.origin.distanceTo( _intersectPointOnRay ); + + if ( distance < raycaster.near || distance > raycaster.far ) return; + + return { + + distance: distance, + // What do we want? intersection point on the ray or on the segment?? + // point: raycaster.ray.at( distance ), + point: _intersectPointOnSegment.clone().applyMatrix4( object.matrixWorld ), + index: a, + face: null, + faceIndex: null, + object: object + + }; + +} + +const _start = /*@__PURE__*/ new Vector3(); +const _end = /*@__PURE__*/ new Vector3(); + +class LineSegments extends Line { + + constructor( geometry, material ) { + + super( geometry, material ); + + this.isLineSegments = true; + + this.type = 'LineSegments'; + + } + + computeLineDistances() { + + const geometry = this.geometry; + + // we assume non-indexed geometry + + if ( geometry.index === null ) { + + const positionAttribute = geometry.attributes.position; + const lineDistances = []; + + for ( let i = 0, l = positionAttribute.count; i < l; i += 2 ) { + + _start.fromBufferAttribute( positionAttribute, i ); + _end.fromBufferAttribute( positionAttribute, i + 1 ); + + lineDistances[ i ] = ( i === 0 ) ? 0 : lineDistances[ i - 1 ]; + lineDistances[ i + 1 ] = lineDistances[ i ] + _start.distanceTo( _end ); + + } + + geometry.setAttribute( 'lineDistance', new Float32BufferAttribute( lineDistances, 1 ) ); + + } else { + + console.warn( 'THREE.LineSegments.computeLineDistances(): Computation only possible with non-indexed BufferGeometry.' ); + + } + + return this; + + } + +} + +class LineLoop extends Line { + + constructor( geometry, material ) { + + super( geometry, material ); + + this.isLineLoop = true; + + this.type = 'LineLoop'; + + } + +} + +class PointsMaterial extends Material { + + constructor( parameters ) { + + super(); + + this.isPointsMaterial = true; + + this.type = 'PointsMaterial'; + + this.color = new Color( 0xffffff ); + + this.map = null; + + this.alphaMap = null; + + this.size = 1; + this.sizeAttenuation = true; + + this.fog = true; + + this.setValues( parameters ); + + } + + copy( source ) { + + super.copy( source ); + + this.color.copy( source.color ); + + this.map = source.map; + + this.alphaMap = source.alphaMap; + + this.size = source.size; + this.sizeAttenuation = source.sizeAttenuation; + + this.fog = source.fog; + + return this; + + } + +} + +const _inverseMatrix = /*@__PURE__*/ new Matrix4(); +const _ray = /*@__PURE__*/ new Ray(); +const _sphere = /*@__PURE__*/ new Sphere(); +const _position$2 = /*@__PURE__*/ new Vector3(); + +class Points extends Object3D { + + constructor( geometry = new BufferGeometry(), material = new PointsMaterial() ) { + + super(); + + this.isPoints = true; + + this.type = 'Points'; + + this.geometry = geometry; + this.material = material; + + this.updateMorphTargets(); + + } + + copy( source, recursive ) { + + super.copy( source, recursive ); + + this.material = Array.isArray( source.material ) ? source.material.slice() : source.material; + this.geometry = source.geometry; + + return this; + + } + + raycast( raycaster, intersects ) { + + const geometry = this.geometry; + const matrixWorld = this.matrixWorld; + const threshold = raycaster.params.Points.threshold; + const drawRange = geometry.drawRange; + + // Checking boundingSphere distance to ray + + if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere(); + + _sphere.copy( geometry.boundingSphere ); + _sphere.applyMatrix4( matrixWorld ); + _sphere.radius += threshold; + + if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return; + + // + + _inverseMatrix.copy( matrixWorld ).invert(); + _ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix ); + + const localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 ); + const localThresholdSq = localThreshold * localThreshold; + + const index = geometry.index; + const attributes = geometry.attributes; + const positionAttribute = attributes.position; + + if ( index !== null ) { + + const start = Math.max( 0, drawRange.start ); + const end = Math.min( index.count, ( drawRange.start + drawRange.count ) ); + + for ( let i = start, il = end; i < il; i ++ ) { + + const a = index.getX( i ); + + _position$2.fromBufferAttribute( positionAttribute, a ); + + testPoint( _position$2, a, localThresholdSq, matrixWorld, raycaster, intersects, this ); + + } + + } else { + + const start = Math.max( 0, drawRange.start ); + const end = Math.min( positionAttribute.count, ( drawRange.start + drawRange.count ) ); + + for ( let i = start, l = end; i < l; i ++ ) { + + _position$2.fromBufferAttribute( positionAttribute, i ); + + testPoint( _position$2, i, localThresholdSq, matrixWorld, raycaster, intersects, this ); + + } + + } + + } + + updateMorphTargets() { + + const geometry = this.geometry; + + const morphAttributes = geometry.morphAttributes; + const keys = Object.keys( morphAttributes ); + + if ( keys.length > 0 ) { + + const morphAttribute = morphAttributes[ keys[ 0 ] ]; + + if ( morphAttribute !== undefined ) { + + this.morphTargetInfluences = []; + this.morphTargetDictionary = {}; + + for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) { + + const name = morphAttribute[ m ].name || String( m ); + + this.morphTargetInfluences.push( 0 ); + this.morphTargetDictionary[ name ] = m; + + } + + } + + } + + } + +} + +function testPoint( point, index, localThresholdSq, matrixWorld, raycaster, intersects, object ) { + + const rayPointDistanceSq = _ray.distanceSqToPoint( point ); + + if ( rayPointDistanceSq < localThresholdSq ) { + + const intersectPoint = new Vector3(); + + _ray.closestPointToPoint( point, intersectPoint ); + intersectPoint.applyMatrix4( matrixWorld ); + + const distance = raycaster.ray.origin.distanceTo( intersectPoint ); + + if ( distance < raycaster.near || distance > raycaster.far ) return; + + intersects.push( { + + distance: distance, + distanceToRay: Math.sqrt( rayPointDistanceSq ), + point: intersectPoint, + index: index, + face: null, + object: object + + } ); + + } + +} + +class VideoTexture extends Texture { + + constructor( video, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ) { + + super( video, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ); + + this.isVideoTexture = true; + + this.minFilter = minFilter !== undefined ? minFilter : LinearFilter; + this.magFilter = magFilter !== undefined ? magFilter : LinearFilter; + + this.generateMipmaps = false; + + const scope = this; + + function updateVideo() { + + scope.needsUpdate = true; + video.requestVideoFrameCallback( updateVideo ); + + } + + if ( 'requestVideoFrameCallback' in video ) { + + video.requestVideoFrameCallback( updateVideo ); + + } + + } + + clone() { + + return new this.constructor( this.image ).copy( this ); + + } + + update() { + + const video = this.image; + const hasVideoFrameCallback = 'requestVideoFrameCallback' in video; + + if ( hasVideoFrameCallback === false && video.readyState >= video.HAVE_CURRENT_DATA ) { + + this.needsUpdate = true; + + } + + } + +} + +class FramebufferTexture extends Texture { + + constructor( width, height ) { + + super( { width, height } ); + + this.isFramebufferTexture = true; + + this.magFilter = NearestFilter; + this.minFilter = NearestFilter; + + this.generateMipmaps = false; + + this.needsUpdate = true; + + } + +} + +class CompressedTexture extends Texture { + + constructor( mipmaps, width, height, format, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy, colorSpace ) { + + super( null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, colorSpace ); + + this.isCompressedTexture = true; + + this.image = { width: width, height: height }; + this.mipmaps = mipmaps; + + // no flipping for cube textures + // (also flipping doesn't work for compressed textures ) + + this.flipY = false; + + // can't generate mipmaps for compressed textures + // mips must be embedded in DDS files + + this.generateMipmaps = false; + + } + +} + +class CompressedArrayTexture extends CompressedTexture { + + constructor( mipmaps, width, height, depth, format, type ) { + + super( mipmaps, width, height, format, type ); + + this.isCompressedArrayTexture = true; + this.image.depth = depth; + this.wrapR = ClampToEdgeWrapping; + + } + +} + +class CompressedCubeTexture extends CompressedTexture { + + constructor( images, format, type ) { + + super( undefined, images[ 0 ].width, images[ 0 ].height, format, type, CubeReflectionMapping ); + + this.isCompressedCubeTexture = true; + this.isCubeTexture = true; + + this.image = images; + + } + +} + +class CanvasTexture extends Texture { + + constructor( canvas, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ) { + + super( canvas, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ); + + this.isCanvasTexture = true; + + this.needsUpdate = true; + + } + +} + +/** + * Extensible curve object. + * + * Some common of curve methods: + * .getPoint( t, optionalTarget ), .getTangent( t, optionalTarget ) + * .getPointAt( u, optionalTarget ), .getTangentAt( u, optionalTarget ) + * .getPoints(), .getSpacedPoints() + * .getLength() + * .updateArcLengths() + * + * This following curves inherit from THREE.Curve: + * + * -- 2D curves -- + * THREE.ArcCurve + * THREE.CubicBezierCurve + * THREE.EllipseCurve + * THREE.LineCurve + * THREE.QuadraticBezierCurve + * THREE.SplineCurve + * + * -- 3D curves -- + * THREE.CatmullRomCurve3 + * THREE.CubicBezierCurve3 + * THREE.LineCurve3 + * THREE.QuadraticBezierCurve3 + * + * A series of curves can be represented as a THREE.CurvePath. + * + **/ + +class Curve { + + constructor() { + + this.type = 'Curve'; + + this.arcLengthDivisions = 200; + + } + + // Virtual base class method to overwrite and implement in subclasses + // - t [0 .. 1] + + getPoint( /* t, optionalTarget */ ) { + + console.warn( 'THREE.Curve: .getPoint() not implemented.' ); + return null; + + } + + // Get point at relative position in curve according to arc length + // - u [0 .. 1] + + getPointAt( u, optionalTarget ) { + + const t = this.getUtoTmapping( u ); + return this.getPoint( t, optionalTarget ); + + } + + // Get sequence of points using getPoint( t ) + + getPoints( divisions = 5 ) { + + const points = []; + + for ( let d = 0; d <= divisions; d ++ ) { + + points.push( this.getPoint( d / divisions ) ); + + } + + return points; + + } + + // Get sequence of points using getPointAt( u ) + + getSpacedPoints( divisions = 5 ) { + + const points = []; + + for ( let d = 0; d <= divisions; d ++ ) { + + points.push( this.getPointAt( d / divisions ) ); + + } + + return points; + + } + + // Get total curve arc length + + getLength() { + + const lengths = this.getLengths(); + return lengths[ lengths.length - 1 ]; + + } + + // Get list of cumulative segment lengths + + getLengths( divisions = this.arcLengthDivisions ) { + + if ( this.cacheArcLengths && + ( this.cacheArcLengths.length === divisions + 1 ) && + ! this.needsUpdate ) { + + return this.cacheArcLengths; + + } + + this.needsUpdate = false; + + const cache = []; + let current, last = this.getPoint( 0 ); + let sum = 0; + + cache.push( 0 ); + + for ( let p = 1; p <= divisions; p ++ ) { + + current = this.getPoint( p / divisions ); + sum += current.distanceTo( last ); + cache.push( sum ); + last = current; + + } + + this.cacheArcLengths = cache; + + return cache; // { sums: cache, sum: sum }; Sum is in the last element. + + } + + updateArcLengths() { + + this.needsUpdate = true; + this.getLengths(); + + } + + // Given u ( 0 .. 1 ), get a t to find p. This gives you points which are equidistant + + getUtoTmapping( u, distance ) { + + const arcLengths = this.getLengths(); + + let i = 0; + const il = arcLengths.length; + + let targetArcLength; // The targeted u distance value to get + + if ( distance ) { + + targetArcLength = distance; + + } else { + + targetArcLength = u * arcLengths[ il - 1 ]; + + } + + // binary search for the index with largest value smaller than target u distance + + let low = 0, high = il - 1, comparison; + + while ( low <= high ) { + + i = Math.floor( low + ( high - low ) / 2 ); // less likely to overflow, though probably not issue here, JS doesn't really have integers, all numbers are floats + + comparison = arcLengths[ i ] - targetArcLength; + + if ( comparison < 0 ) { + + low = i + 1; + + } else if ( comparison > 0 ) { + + high = i - 1; + + } else { + + high = i; + break; + + // DONE + + } + + } + + i = high; + + if ( arcLengths[ i ] === targetArcLength ) { + + return i / ( il - 1 ); + + } + + // we could get finer grain at lengths, or use simple interpolation between two points + + const lengthBefore = arcLengths[ i ]; + const lengthAfter = arcLengths[ i + 1 ]; + + const segmentLength = lengthAfter - lengthBefore; + + // determine where we are between the 'before' and 'after' points + + const segmentFraction = ( targetArcLength - lengthBefore ) / segmentLength; + + // add that fractional amount to t + + const t = ( i + segmentFraction ) / ( il - 1 ); + + return t; + + } + + // Returns a unit vector tangent at t + // In case any sub curve does not implement its tangent derivation, + // 2 points a small delta apart will be used to find its gradient + // which seems to give a reasonable approximation + + getTangent( t, optionalTarget ) { + + const delta = 0.0001; + let t1 = t - delta; + let t2 = t + delta; + + // Capping in case of danger + + if ( t1 < 0 ) t1 = 0; + if ( t2 > 1 ) t2 = 1; + + const pt1 = this.getPoint( t1 ); + const pt2 = this.getPoint( t2 ); + + const tangent = optionalTarget || ( ( pt1.isVector2 ) ? new Vector2() : new Vector3() ); + + tangent.copy( pt2 ).sub( pt1 ).normalize(); + + return tangent; + + } + + getTangentAt( u, optionalTarget ) { + + const t = this.getUtoTmapping( u ); + return this.getTangent( t, optionalTarget ); + + } + + computeFrenetFrames( segments, closed ) { + + // see http://www.cs.indiana.edu/pub/techreports/TR425.pdf + + const normal = new Vector3(); + + const tangents = []; + const normals = []; + const binormals = []; + + const vec = new Vector3(); + const mat = new Matrix4(); + + // compute the tangent vectors for each segment on the curve + + for ( let i = 0; i <= segments; i ++ ) { + + const u = i / segments; + + tangents[ i ] = this.getTangentAt( u, new Vector3() ); + + } + + // select an initial normal vector perpendicular to the first tangent vector, + // and in the direction of the minimum tangent xyz component + + normals[ 0 ] = new Vector3(); + binormals[ 0 ] = new Vector3(); + let min = Number.MAX_VALUE; + const tx = Math.abs( tangents[ 0 ].x ); + const ty = Math.abs( tangents[ 0 ].y ); + const tz = Math.abs( tangents[ 0 ].z ); + + if ( tx <= min ) { + + min = tx; + normal.set( 1, 0, 0 ); + + } + + if ( ty <= min ) { + + min = ty; + normal.set( 0, 1, 0 ); + + } + + if ( tz <= min ) { + + normal.set( 0, 0, 1 ); + + } + + vec.crossVectors( tangents[ 0 ], normal ).normalize(); + + normals[ 0 ].crossVectors( tangents[ 0 ], vec ); + binormals[ 0 ].crossVectors( tangents[ 0 ], normals[ 0 ] ); + + + // compute the slowly-varying normal and binormal vectors for each segment on the curve + + for ( let i = 1; i <= segments; i ++ ) { + + normals[ i ] = normals[ i - 1 ].clone(); + + binormals[ i ] = binormals[ i - 1 ].clone(); + + vec.crossVectors( tangents[ i - 1 ], tangents[ i ] ); + + if ( vec.length() > Number.EPSILON ) { + + vec.normalize(); + + const theta = Math.acos( clamp( tangents[ i - 1 ].dot( tangents[ i ] ), - 1, 1 ) ); // clamp for floating pt errors + + normals[ i ].applyMatrix4( mat.makeRotationAxis( vec, theta ) ); + + } + + binormals[ i ].crossVectors( tangents[ i ], normals[ i ] ); + + } + + // if the curve is closed, postprocess the vectors so the first and last normal vectors are the same + + if ( closed === true ) { + + let theta = Math.acos( clamp( normals[ 0 ].dot( normals[ segments ] ), - 1, 1 ) ); + theta /= segments; + + if ( tangents[ 0 ].dot( vec.crossVectors( normals[ 0 ], normals[ segments ] ) ) > 0 ) { + + theta = - theta; + + } + + for ( let i = 1; i <= segments; i ++ ) { + + // twist a little... + normals[ i ].applyMatrix4( mat.makeRotationAxis( tangents[ i ], theta * i ) ); + binormals[ i ].crossVectors( tangents[ i ], normals[ i ] ); + + } + + } + + return { + tangents: tangents, + normals: normals, + binormals: binormals + }; + + } + + clone() { + + return new this.constructor().copy( this ); + + } + + copy( source ) { + + this.arcLengthDivisions = source.arcLengthDivisions; + + return this; + + } + + toJSON() { + + const data = { + metadata: { + version: 4.6, + type: 'Curve', + generator: 'Curve.toJSON' + } + }; + + data.arcLengthDivisions = this.arcLengthDivisions; + data.type = this.type; + + return data; + + } + + fromJSON( json ) { + + this.arcLengthDivisions = json.arcLengthDivisions; + + return this; + + } + +} + +class EllipseCurve extends Curve { + + constructor( aX = 0, aY = 0, xRadius = 1, yRadius = 1, aStartAngle = 0, aEndAngle = Math.PI * 2, aClockwise = false, aRotation = 0 ) { + + super(); + + this.isEllipseCurve = true; + + this.type = 'EllipseCurve'; + + this.aX = aX; + this.aY = aY; + + this.xRadius = xRadius; + this.yRadius = yRadius; + + this.aStartAngle = aStartAngle; + this.aEndAngle = aEndAngle; + + this.aClockwise = aClockwise; + + this.aRotation = aRotation; + + } + + getPoint( t, optionalTarget = new Vector2() ) { + + const point = optionalTarget; + + const twoPi = Math.PI * 2; + let deltaAngle = this.aEndAngle - this.aStartAngle; + const samePoints = Math.abs( deltaAngle ) < Number.EPSILON; + + // ensures that deltaAngle is 0 .. 2 PI + while ( deltaAngle < 0 ) deltaAngle += twoPi; + while ( deltaAngle > twoPi ) deltaAngle -= twoPi; + + if ( deltaAngle < Number.EPSILON ) { + + if ( samePoints ) { + + deltaAngle = 0; + + } else { + + deltaAngle = twoPi; + + } + + } + + if ( this.aClockwise === true && ! samePoints ) { + + if ( deltaAngle === twoPi ) { + + deltaAngle = - twoPi; + + } else { + + deltaAngle = deltaAngle - twoPi; + + } + + } + + const angle = this.aStartAngle + t * deltaAngle; + let x = this.aX + this.xRadius * Math.cos( angle ); + let y = this.aY + this.yRadius * Math.sin( angle ); + + if ( this.aRotation !== 0 ) { + + const cos = Math.cos( this.aRotation ); + const sin = Math.sin( this.aRotation ); + + const tx = x - this.aX; + const ty = y - this.aY; + + // Rotate the point about the center of the ellipse. + x = tx * cos - ty * sin + this.aX; + y = tx * sin + ty * cos + this.aY; + + } + + return point.set( x, y ); + + } + + copy( source ) { + + super.copy( source ); + + this.aX = source.aX; + this.aY = source.aY; + + this.xRadius = source.xRadius; + this.yRadius = source.yRadius; + + this.aStartAngle = source.aStartAngle; + this.aEndAngle = source.aEndAngle; + + this.aClockwise = source.aClockwise; + + this.aRotation = source.aRotation; + + return this; + + } + + toJSON() { + + const data = super.toJSON(); + + data.aX = this.aX; + data.aY = this.aY; + + data.xRadius = this.xRadius; + data.yRadius = this.yRadius; + + data.aStartAngle = this.aStartAngle; + data.aEndAngle = this.aEndAngle; + + data.aClockwise = this.aClockwise; + + data.aRotation = this.aRotation; + + return data; + + } + + fromJSON( json ) { + + super.fromJSON( json ); + + this.aX = json.aX; + this.aY = json.aY; + + this.xRadius = json.xRadius; + this.yRadius = json.yRadius; + + this.aStartAngle = json.aStartAngle; + this.aEndAngle = json.aEndAngle; + + this.aClockwise = json.aClockwise; + + this.aRotation = json.aRotation; + + return this; + + } + +} + +class ArcCurve extends EllipseCurve { + + constructor( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) { + + super( aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise ); + + this.isArcCurve = true; + + this.type = 'ArcCurve'; + + } + +} + +/** + * Centripetal CatmullRom Curve - which is useful for avoiding + * cusps and self-intersections in non-uniform catmull rom curves. + * http://www.cemyuksel.com/research/catmullrom_param/catmullrom.pdf + * + * curve.type accepts centripetal(default), chordal and catmullrom + * curve.tension is used for catmullrom which defaults to 0.5 + */ + + +/* +Based on an optimized c++ solution in + - http://stackoverflow.com/questions/9489736/catmull-rom-curve-with-no-cusps-and-no-self-intersections/ + - http://ideone.com/NoEbVM + +This CubicPoly class could be used for reusing some variables and calculations, +but for three.js curve use, it could be possible inlined and flatten into a single function call +which can be placed in CurveUtils. +*/ + +function CubicPoly() { + + let c0 = 0, c1 = 0, c2 = 0, c3 = 0; + + /* + * Compute coefficients for a cubic polynomial + * p(s) = c0 + c1*s + c2*s^2 + c3*s^3 + * such that + * p(0) = x0, p(1) = x1 + * and + * p'(0) = t0, p'(1) = t1. + */ + function init( x0, x1, t0, t1 ) { + + c0 = x0; + c1 = t0; + c2 = - 3 * x0 + 3 * x1 - 2 * t0 - t1; + c3 = 2 * x0 - 2 * x1 + t0 + t1; + + } + + return { + + initCatmullRom: function ( x0, x1, x2, x3, tension ) { + + init( x1, x2, tension * ( x2 - x0 ), tension * ( x3 - x1 ) ); + + }, + + initNonuniformCatmullRom: function ( x0, x1, x2, x3, dt0, dt1, dt2 ) { + + // compute tangents when parameterized in [t1,t2] + let t1 = ( x1 - x0 ) / dt0 - ( x2 - x0 ) / ( dt0 + dt1 ) + ( x2 - x1 ) / dt1; + let t2 = ( x2 - x1 ) / dt1 - ( x3 - x1 ) / ( dt1 + dt2 ) + ( x3 - x2 ) / dt2; + + // rescale tangents for parametrization in [0,1] + t1 *= dt1; + t2 *= dt1; + + init( x1, x2, t1, t2 ); + + }, + + calc: function ( t ) { + + const t2 = t * t; + const t3 = t2 * t; + return c0 + c1 * t + c2 * t2 + c3 * t3; + + } + + }; + +} + +// + +const tmp = /*@__PURE__*/ new Vector3(); +const px = /*@__PURE__*/ new CubicPoly(); +const py = /*@__PURE__*/ new CubicPoly(); +const pz = /*@__PURE__*/ new CubicPoly(); + +class CatmullRomCurve3 extends Curve { + + constructor( points = [], closed = false, curveType = 'centripetal', tension = 0.5 ) { + + super(); + + this.isCatmullRomCurve3 = true; + + this.type = 'CatmullRomCurve3'; + + this.points = points; + this.closed = closed; + this.curveType = curveType; + this.tension = tension; + + } + + getPoint( t, optionalTarget = new Vector3() ) { + + const point = optionalTarget; + + const points = this.points; + const l = points.length; + + const p = ( l - ( this.closed ? 0 : 1 ) ) * t; + let intPoint = Math.floor( p ); + let weight = p - intPoint; + + if ( this.closed ) { + + intPoint += intPoint > 0 ? 0 : ( Math.floor( Math.abs( intPoint ) / l ) + 1 ) * l; + + } else if ( weight === 0 && intPoint === l - 1 ) { + + intPoint = l - 2; + weight = 1; + + } + + let p0, p3; // 4 points (p1 & p2 defined below) + + if ( this.closed || intPoint > 0 ) { + + p0 = points[ ( intPoint - 1 ) % l ]; + + } else { + + // extrapolate first point + tmp.subVectors( points[ 0 ], points[ 1 ] ).add( points[ 0 ] ); + p0 = tmp; + + } + + const p1 = points[ intPoint % l ]; + const p2 = points[ ( intPoint + 1 ) % l ]; + + if ( this.closed || intPoint + 2 < l ) { + + p3 = points[ ( intPoint + 2 ) % l ]; + + } else { + + // extrapolate last point + tmp.subVectors( points[ l - 1 ], points[ l - 2 ] ).add( points[ l - 1 ] ); + p3 = tmp; + + } + + if ( this.curveType === 'centripetal' || this.curveType === 'chordal' ) { + + // init Centripetal / Chordal Catmull-Rom + const pow = this.curveType === 'chordal' ? 0.5 : 0.25; + let dt0 = Math.pow( p0.distanceToSquared( p1 ), pow ); + let dt1 = Math.pow( p1.distanceToSquared( p2 ), pow ); + let dt2 = Math.pow( p2.distanceToSquared( p3 ), pow ); + + // safety check for repeated points + if ( dt1 < 1e-4 ) dt1 = 1.0; + if ( dt0 < 1e-4 ) dt0 = dt1; + if ( dt2 < 1e-4 ) dt2 = dt1; + + px.initNonuniformCatmullRom( p0.x, p1.x, p2.x, p3.x, dt0, dt1, dt2 ); + py.initNonuniformCatmullRom( p0.y, p1.y, p2.y, p3.y, dt0, dt1, dt2 ); + pz.initNonuniformCatmullRom( p0.z, p1.z, p2.z, p3.z, dt0, dt1, dt2 ); + + } else if ( this.curveType === 'catmullrom' ) { + + px.initCatmullRom( p0.x, p1.x, p2.x, p3.x, this.tension ); + py.initCatmullRom( p0.y, p1.y, p2.y, p3.y, this.tension ); + pz.initCatmullRom( p0.z, p1.z, p2.z, p3.z, this.tension ); + + } + + point.set( + px.calc( weight ), + py.calc( weight ), + pz.calc( weight ) + ); + + return point; + + } + + copy( source ) { + + super.copy( source ); + + this.points = []; + + for ( let i = 0, l = source.points.length; i < l; i ++ ) { + + const point = source.points[ i ]; + + this.points.push( point.clone() ); + + } + + this.closed = source.closed; + this.curveType = source.curveType; + this.tension = source.tension; + + return this; + + } + + toJSON() { + + const data = super.toJSON(); + + data.points = []; + + for ( let i = 0, l = this.points.length; i < l; i ++ ) { + + const point = this.points[ i ]; + data.points.push( point.toArray() ); + + } + + data.closed = this.closed; + data.curveType = this.curveType; + data.tension = this.tension; + + return data; + + } + + fromJSON( json ) { + + super.fromJSON( json ); + + this.points = []; + + for ( let i = 0, l = json.points.length; i < l; i ++ ) { + + const point = json.points[ i ]; + this.points.push( new Vector3().fromArray( point ) ); + + } + + this.closed = json.closed; + this.curveType = json.curveType; + this.tension = json.tension; + + return this; + + } + +} + +/** + * Bezier Curves formulas obtained from + * https://en.wikipedia.org/wiki/B%C3%A9zier_curve + */ + +function CatmullRom( t, p0, p1, p2, p3 ) { + + const v0 = ( p2 - p0 ) * 0.5; + const v1 = ( p3 - p1 ) * 0.5; + const t2 = t * t; + const t3 = t * t2; + return ( 2 * p1 - 2 * p2 + v0 + v1 ) * t3 + ( - 3 * p1 + 3 * p2 - 2 * v0 - v1 ) * t2 + v0 * t + p1; + +} + +// + +function QuadraticBezierP0( t, p ) { + + const k = 1 - t; + return k * k * p; + +} + +function QuadraticBezierP1( t, p ) { + + return 2 * ( 1 - t ) * t * p; + +} + +function QuadraticBezierP2( t, p ) { + + return t * t * p; + +} + +function QuadraticBezier( t, p0, p1, p2 ) { + + return QuadraticBezierP0( t, p0 ) + QuadraticBezierP1( t, p1 ) + + QuadraticBezierP2( t, p2 ); + +} + +// + +function CubicBezierP0( t, p ) { + + const k = 1 - t; + return k * k * k * p; + +} + +function CubicBezierP1( t, p ) { + + const k = 1 - t; + return 3 * k * k * t * p; + +} + +function CubicBezierP2( t, p ) { + + return 3 * ( 1 - t ) * t * t * p; + +} + +function CubicBezierP3( t, p ) { + + return t * t * t * p; + +} + +function CubicBezier( t, p0, p1, p2, p3 ) { + + return CubicBezierP0( t, p0 ) + CubicBezierP1( t, p1 ) + CubicBezierP2( t, p2 ) + + CubicBezierP3( t, p3 ); + +} + +class CubicBezierCurve extends Curve { + + constructor( v0 = new Vector2(), v1 = new Vector2(), v2 = new Vector2(), v3 = new Vector2() ) { + + super(); + + this.isCubicBezierCurve = true; + + this.type = 'CubicBezierCurve'; + + this.v0 = v0; + this.v1 = v1; + this.v2 = v2; + this.v3 = v3; + + } + + getPoint( t, optionalTarget = new Vector2() ) { + + const point = optionalTarget; + + const v0 = this.v0, v1 = this.v1, v2 = this.v2, v3 = this.v3; + + point.set( + CubicBezier( t, v0.x, v1.x, v2.x, v3.x ), + CubicBezier( t, v0.y, v1.y, v2.y, v3.y ) + ); + + return point; + + } + + copy( source ) { + + super.copy( source ); + + this.v0.copy( source.v0 ); + this.v1.copy( source.v1 ); + this.v2.copy( source.v2 ); + this.v3.copy( source.v3 ); + + return this; + + } + + toJSON() { + + const data = super.toJSON(); + + data.v0 = this.v0.toArray(); + data.v1 = this.v1.toArray(); + data.v2 = this.v2.toArray(); + data.v3 = this.v3.toArray(); + + return data; + + } + + fromJSON( json ) { + + super.fromJSON( json ); + + this.v0.fromArray( json.v0 ); + this.v1.fromArray( json.v1 ); + this.v2.fromArray( json.v2 ); + this.v3.fromArray( json.v3 ); + + return this; + + } + +} + +class CubicBezierCurve3 extends Curve { + + constructor( v0 = new Vector3(), v1 = new Vector3(), v2 = new Vector3(), v3 = new Vector3() ) { + + super(); + + this.isCubicBezierCurve3 = true; + + this.type = 'CubicBezierCurve3'; + + this.v0 = v0; + this.v1 = v1; + this.v2 = v2; + this.v3 = v3; + + } + + getPoint( t, optionalTarget = new Vector3() ) { + + const point = optionalTarget; + + const v0 = this.v0, v1 = this.v1, v2 = this.v2, v3 = this.v3; + + point.set( + CubicBezier( t, v0.x, v1.x, v2.x, v3.x ), + CubicBezier( t, v0.y, v1.y, v2.y, v3.y ), + CubicBezier( t, v0.z, v1.z, v2.z, v3.z ) + ); + + return point; + + } + + copy( source ) { + + super.copy( source ); + + this.v0.copy( source.v0 ); + this.v1.copy( source.v1 ); + this.v2.copy( source.v2 ); + this.v3.copy( source.v3 ); + + return this; + + } + + toJSON() { + + const data = super.toJSON(); + + data.v0 = this.v0.toArray(); + data.v1 = this.v1.toArray(); + data.v2 = this.v2.toArray(); + data.v3 = this.v3.toArray(); + + return data; + + } + + fromJSON( json ) { + + super.fromJSON( json ); + + this.v0.fromArray( json.v0 ); + this.v1.fromArray( json.v1 ); + this.v2.fromArray( json.v2 ); + this.v3.fromArray( json.v3 ); + + return this; + + } + +} + +class LineCurve extends Curve { + + constructor( v1 = new Vector2(), v2 = new Vector2() ) { + + super(); + + this.isLineCurve = true; + + this.type = 'LineCurve'; + + this.v1 = v1; + this.v2 = v2; + + } + + getPoint( t, optionalTarget = new Vector2() ) { + + const point = optionalTarget; + + if ( t === 1 ) { + + point.copy( this.v2 ); + + } else { + + point.copy( this.v2 ).sub( this.v1 ); + point.multiplyScalar( t ).add( this.v1 ); + + } + + return point; + + } + + // Line curve is linear, so we can overwrite default getPointAt + getPointAt( u, optionalTarget ) { + + return this.getPoint( u, optionalTarget ); + + } + + getTangent( t, optionalTarget = new Vector2() ) { + + return optionalTarget.subVectors( this.v2, this.v1 ).normalize(); + + } + + getTangentAt( u, optionalTarget ) { + + return this.getTangent( u, optionalTarget ); + + } + + copy( source ) { + + super.copy( source ); + + this.v1.copy( source.v1 ); + this.v2.copy( source.v2 ); + + return this; + + } + + toJSON() { + + const data = super.toJSON(); + + data.v1 = this.v1.toArray(); + data.v2 = this.v2.toArray(); + + return data; + + } + + fromJSON( json ) { + + super.fromJSON( json ); + + this.v1.fromArray( json.v1 ); + this.v2.fromArray( json.v2 ); + + return this; + + } + +} + +class LineCurve3 extends Curve { + + constructor( v1 = new Vector3(), v2 = new Vector3() ) { + + super(); + + this.isLineCurve3 = true; + + this.type = 'LineCurve3'; + + this.v1 = v1; + this.v2 = v2; + + } + + getPoint( t, optionalTarget = new Vector3() ) { + + const point = optionalTarget; + + if ( t === 1 ) { + + point.copy( this.v2 ); + + } else { + + point.copy( this.v2 ).sub( this.v1 ); + point.multiplyScalar( t ).add( this.v1 ); + + } + + return point; + + } + + // Line curve is linear, so we can overwrite default getPointAt + getPointAt( u, optionalTarget ) { + + return this.getPoint( u, optionalTarget ); + + } + + getTangent( t, optionalTarget = new Vector3() ) { + + return optionalTarget.subVectors( this.v2, this.v1 ).normalize(); + + } + + getTangentAt( u, optionalTarget ) { + + return this.getTangent( u, optionalTarget ); + + } + + copy( source ) { + + super.copy( source ); + + this.v1.copy( source.v1 ); + this.v2.copy( source.v2 ); + + return this; + + } + + toJSON() { + + const data = super.toJSON(); + + data.v1 = this.v1.toArray(); + data.v2 = this.v2.toArray(); + + return data; + + } + + fromJSON( json ) { + + super.fromJSON( json ); + + this.v1.fromArray( json.v1 ); + this.v2.fromArray( json.v2 ); + + return this; + + } + +} + +class QuadraticBezierCurve extends Curve { + + constructor( v0 = new Vector2(), v1 = new Vector2(), v2 = new Vector2() ) { + + super(); + + this.isQuadraticBezierCurve = true; + + this.type = 'QuadraticBezierCurve'; + + this.v0 = v0; + this.v1 = v1; + this.v2 = v2; + + } + + getPoint( t, optionalTarget = new Vector2() ) { + + const point = optionalTarget; + + const v0 = this.v0, v1 = this.v1, v2 = this.v2; + + point.set( + QuadraticBezier( t, v0.x, v1.x, v2.x ), + QuadraticBezier( t, v0.y, v1.y, v2.y ) + ); + + return point; + + } + + copy( source ) { + + super.copy( source ); + + this.v0.copy( source.v0 ); + this.v1.copy( source.v1 ); + this.v2.copy( source.v2 ); + + return this; + + } + + toJSON() { + + const data = super.toJSON(); + + data.v0 = this.v0.toArray(); + data.v1 = this.v1.toArray(); + data.v2 = this.v2.toArray(); + + return data; + + } + + fromJSON( json ) { + + super.fromJSON( json ); + + this.v0.fromArray( json.v0 ); + this.v1.fromArray( json.v1 ); + this.v2.fromArray( json.v2 ); + + return this; + + } + +} + +class QuadraticBezierCurve3 extends Curve { + + constructor( v0 = new Vector3(), v1 = new Vector3(), v2 = new Vector3() ) { + + super(); + + this.isQuadraticBezierCurve3 = true; + + this.type = 'QuadraticBezierCurve3'; + + this.v0 = v0; + this.v1 = v1; + this.v2 = v2; + + } + + getPoint( t, optionalTarget = new Vector3() ) { + + const point = optionalTarget; + + const v0 = this.v0, v1 = this.v1, v2 = this.v2; + + point.set( + QuadraticBezier( t, v0.x, v1.x, v2.x ), + QuadraticBezier( t, v0.y, v1.y, v2.y ), + QuadraticBezier( t, v0.z, v1.z, v2.z ) + ); + + return point; + + } + + copy( source ) { + + super.copy( source ); + + this.v0.copy( source.v0 ); + this.v1.copy( source.v1 ); + this.v2.copy( source.v2 ); + + return this; + + } + + toJSON() { + + const data = super.toJSON(); + + data.v0 = this.v0.toArray(); + data.v1 = this.v1.toArray(); + data.v2 = this.v2.toArray(); + + return data; + + } + + fromJSON( json ) { + + super.fromJSON( json ); + + this.v0.fromArray( json.v0 ); + this.v1.fromArray( json.v1 ); + this.v2.fromArray( json.v2 ); + + return this; + + } + +} + +class SplineCurve extends Curve { + + constructor( points = [] ) { + + super(); + + this.isSplineCurve = true; + + this.type = 'SplineCurve'; + + this.points = points; + + } + + getPoint( t, optionalTarget = new Vector2() ) { + + const point = optionalTarget; + + const points = this.points; + const p = ( points.length - 1 ) * t; + + const intPoint = Math.floor( p ); + const weight = p - intPoint; + + const p0 = points[ intPoint === 0 ? intPoint : intPoint - 1 ]; + const p1 = points[ intPoint ]; + const p2 = points[ intPoint > points.length - 2 ? points.length - 1 : intPoint + 1 ]; + const p3 = points[ intPoint > points.length - 3 ? points.length - 1 : intPoint + 2 ]; + + point.set( + CatmullRom( weight, p0.x, p1.x, p2.x, p3.x ), + CatmullRom( weight, p0.y, p1.y, p2.y, p3.y ) + ); + + return point; + + } + + copy( source ) { + + super.copy( source ); + + this.points = []; + + for ( let i = 0, l = source.points.length; i < l; i ++ ) { + + const point = source.points[ i ]; + + this.points.push( point.clone() ); + + } + + return this; + + } + + toJSON() { + + const data = super.toJSON(); + + data.points = []; + + for ( let i = 0, l = this.points.length; i < l; i ++ ) { + + const point = this.points[ i ]; + data.points.push( point.toArray() ); + + } + + return data; + + } + + fromJSON( json ) { + + super.fromJSON( json ); + + this.points = []; + + for ( let i = 0, l = json.points.length; i < l; i ++ ) { + + const point = json.points[ i ]; + this.points.push( new Vector2().fromArray( point ) ); + + } + + return this; + + } + +} + +var Curves = /*#__PURE__*/Object.freeze({ + __proto__: null, + ArcCurve: ArcCurve, + CatmullRomCurve3: CatmullRomCurve3, + CubicBezierCurve: CubicBezierCurve, + CubicBezierCurve3: CubicBezierCurve3, + EllipseCurve: EllipseCurve, + LineCurve: LineCurve, + LineCurve3: LineCurve3, + QuadraticBezierCurve: QuadraticBezierCurve, + QuadraticBezierCurve3: QuadraticBezierCurve3, + SplineCurve: SplineCurve +}); + +/************************************************************** + * Curved Path - a curve path is simply a array of connected + * curves, but retains the api of a curve + **************************************************************/ + +class CurvePath extends Curve { + + constructor() { + + super(); + + this.type = 'CurvePath'; + + this.curves = []; + this.autoClose = false; // Automatically closes the path + + } + + add( curve ) { + + this.curves.push( curve ); + + } + + closePath() { + + // Add a line curve if start and end of lines are not connected + const startPoint = this.curves[ 0 ].getPoint( 0 ); + const endPoint = this.curves[ this.curves.length - 1 ].getPoint( 1 ); + + if ( ! startPoint.equals( endPoint ) ) { + + const lineType = ( startPoint.isVector2 === true ) ? 'LineCurve' : 'LineCurve3'; + this.curves.push( new Curves[ lineType ]( endPoint, startPoint ) ); + + } + + return this; + + } + + // To get accurate point with reference to + // entire path distance at time t, + // following has to be done: + + // 1. Length of each sub path have to be known + // 2. Locate and identify type of curve + // 3. Get t for the curve + // 4. Return curve.getPointAt(t') + + getPoint( t, optionalTarget ) { + + const d = t * this.getLength(); + const curveLengths = this.getCurveLengths(); + let i = 0; + + // To think about boundaries points. + + while ( i < curveLengths.length ) { + + if ( curveLengths[ i ] >= d ) { + + const diff = curveLengths[ i ] - d; + const curve = this.curves[ i ]; + + const segmentLength = curve.getLength(); + const u = segmentLength === 0 ? 0 : 1 - diff / segmentLength; + + return curve.getPointAt( u, optionalTarget ); + + } + + i ++; + + } + + return null; + + // loop where sum != 0, sum > d , sum+1 1 && ! points[ points.length - 1 ].equals( points[ 0 ] ) ) { + + points.push( points[ 0 ] ); + + } + + return points; + + } + + copy( source ) { + + super.copy( source ); + + this.curves = []; + + for ( let i = 0, l = source.curves.length; i < l; i ++ ) { + + const curve = source.curves[ i ]; + + this.curves.push( curve.clone() ); + + } + + this.autoClose = source.autoClose; + + return this; + + } + + toJSON() { + + const data = super.toJSON(); + + data.autoClose = this.autoClose; + data.curves = []; + + for ( let i = 0, l = this.curves.length; i < l; i ++ ) { + + const curve = this.curves[ i ]; + data.curves.push( curve.toJSON() ); + + } + + return data; + + } + + fromJSON( json ) { + + super.fromJSON( json ); + + this.autoClose = json.autoClose; + this.curves = []; + + for ( let i = 0, l = json.curves.length; i < l; i ++ ) { + + const curve = json.curves[ i ]; + this.curves.push( new Curves[ curve.type ]().fromJSON( curve ) ); + + } + + return this; + + } + +} + +class Path extends CurvePath { + + constructor( points ) { + + super(); + + this.type = 'Path'; + + this.currentPoint = new Vector2(); + + if ( points ) { + + this.setFromPoints( points ); + + } + + } + + setFromPoints( points ) { + + this.moveTo( points[ 0 ].x, points[ 0 ].y ); + + for ( let i = 1, l = points.length; i < l; i ++ ) { + + this.lineTo( points[ i ].x, points[ i ].y ); + + } + + return this; + + } + + moveTo( x, y ) { + + this.currentPoint.set( x, y ); // TODO consider referencing vectors instead of copying? + + return this; + + } + + lineTo( x, y ) { + + const curve = new LineCurve( this.currentPoint.clone(), new Vector2( x, y ) ); + this.curves.push( curve ); + + this.currentPoint.set( x, y ); + + return this; + + } + + quadraticCurveTo( aCPx, aCPy, aX, aY ) { + + const curve = new QuadraticBezierCurve( + this.currentPoint.clone(), + new Vector2( aCPx, aCPy ), + new Vector2( aX, aY ) + ); + + this.curves.push( curve ); + + this.currentPoint.set( aX, aY ); + + return this; + + } + + bezierCurveTo( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY ) { + + const curve = new CubicBezierCurve( + this.currentPoint.clone(), + new Vector2( aCP1x, aCP1y ), + new Vector2( aCP2x, aCP2y ), + new Vector2( aX, aY ) + ); + + this.curves.push( curve ); + + this.currentPoint.set( aX, aY ); + + return this; + + } + + splineThru( pts /*Array of Vector*/ ) { + + const npts = [ this.currentPoint.clone() ].concat( pts ); + + const curve = new SplineCurve( npts ); + this.curves.push( curve ); + + this.currentPoint.copy( pts[ pts.length - 1 ] ); + + return this; + + } + + arc( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) { + + const x0 = this.currentPoint.x; + const y0 = this.currentPoint.y; + + this.absarc( aX + x0, aY + y0, aRadius, + aStartAngle, aEndAngle, aClockwise ); + + return this; + + } + + absarc( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) { + + this.absellipse( aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise ); + + return this; + + } + + ellipse( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) { + + const x0 = this.currentPoint.x; + const y0 = this.currentPoint.y; + + this.absellipse( aX + x0, aY + y0, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ); + + return this; + + } + + absellipse( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) { + + const curve = new EllipseCurve( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ); + + if ( this.curves.length > 0 ) { + + // if a previous curve is present, attempt to join + const firstPoint = curve.getPoint( 0 ); + + if ( ! firstPoint.equals( this.currentPoint ) ) { + + this.lineTo( firstPoint.x, firstPoint.y ); + + } + + } + + this.curves.push( curve ); + + const lastPoint = curve.getPoint( 1 ); + this.currentPoint.copy( lastPoint ); + + return this; + + } + + copy( source ) { + + super.copy( source ); + + this.currentPoint.copy( source.currentPoint ); + + return this; + + } + + toJSON() { + + const data = super.toJSON(); + + data.currentPoint = this.currentPoint.toArray(); + + return data; + + } + + fromJSON( json ) { + + super.fromJSON( json ); + + this.currentPoint.fromArray( json.currentPoint ); + + return this; + + } + +} + +class LatheGeometry extends BufferGeometry { + + constructor( points = [ new Vector2( 0, - 0.5 ), new Vector2( 0.5, 0 ), new Vector2( 0, 0.5 ) ], segments = 12, phiStart = 0, phiLength = Math.PI * 2 ) { + + super(); + + this.type = 'LatheGeometry'; + + this.parameters = { + points: points, + segments: segments, + phiStart: phiStart, + phiLength: phiLength + }; + + segments = Math.floor( segments ); + + // clamp phiLength so it's in range of [ 0, 2PI ] + + phiLength = clamp( phiLength, 0, Math.PI * 2 ); + + // buffers + + const indices = []; + const vertices = []; + const uvs = []; + const initNormals = []; + const normals = []; + + // helper variables + + const inverseSegments = 1.0 / segments; + const vertex = new Vector3(); + const uv = new Vector2(); + const normal = new Vector3(); + const curNormal = new Vector3(); + const prevNormal = new Vector3(); + let dx = 0; + let dy = 0; + + // pre-compute normals for initial "meridian" + + for ( let j = 0; j <= ( points.length - 1 ); j ++ ) { + + switch ( j ) { + + case 0: // special handling for 1st vertex on path + + dx = points[ j + 1 ].x - points[ j ].x; + dy = points[ j + 1 ].y - points[ j ].y; + + normal.x = dy * 1.0; + normal.y = - dx; + normal.z = dy * 0.0; + + prevNormal.copy( normal ); + + normal.normalize(); + + initNormals.push( normal.x, normal.y, normal.z ); + + break; + + case ( points.length - 1 ): // special handling for last Vertex on path + + initNormals.push( prevNormal.x, prevNormal.y, prevNormal.z ); + + break; + + default: // default handling for all vertices in between + + dx = points[ j + 1 ].x - points[ j ].x; + dy = points[ j + 1 ].y - points[ j ].y; + + normal.x = dy * 1.0; + normal.y = - dx; + normal.z = dy * 0.0; + + curNormal.copy( normal ); + + normal.x += prevNormal.x; + normal.y += prevNormal.y; + normal.z += prevNormal.z; + + normal.normalize(); + + initNormals.push( normal.x, normal.y, normal.z ); + + prevNormal.copy( curNormal ); + + } + + } + + // generate vertices, uvs and normals + + for ( let i = 0; i <= segments; i ++ ) { + + const phi = phiStart + i * inverseSegments * phiLength; + + const sin = Math.sin( phi ); + const cos = Math.cos( phi ); + + for ( let j = 0; j <= ( points.length - 1 ); j ++ ) { + + // vertex + + vertex.x = points[ j ].x * sin; + vertex.y = points[ j ].y; + vertex.z = points[ j ].x * cos; + + vertices.push( vertex.x, vertex.y, vertex.z ); + + // uv + + uv.x = i / segments; + uv.y = j / ( points.length - 1 ); + + uvs.push( uv.x, uv.y ); + + // normal + + const x = initNormals[ 3 * j + 0 ] * sin; + const y = initNormals[ 3 * j + 1 ]; + const z = initNormals[ 3 * j + 0 ] * cos; + + normals.push( x, y, z ); + + } + + } + + // indices + + for ( let i = 0; i < segments; i ++ ) { + + for ( let j = 0; j < ( points.length - 1 ); j ++ ) { + + const base = j + i * points.length; + + const a = base; + const b = base + points.length; + const c = base + points.length + 1; + const d = base + 1; + + // faces + + indices.push( a, b, d ); + indices.push( c, d, b ); + + } + + } + + // build geometry + + this.setIndex( indices ); + this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); + this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); + this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); + + } + + copy( source ) { + + super.copy( source ); + + this.parameters = Object.assign( {}, source.parameters ); + + return this; + + } + + static fromJSON( data ) { + + return new LatheGeometry( data.points, data.segments, data.phiStart, data.phiLength ); + + } + +} + +class CapsuleGeometry extends LatheGeometry { + + constructor( radius = 1, length = 1, capSegments = 4, radialSegments = 8 ) { + + const path = new Path(); + path.absarc( 0, - length / 2, radius, Math.PI * 1.5, 0 ); + path.absarc( 0, length / 2, radius, 0, Math.PI * 0.5 ); + + super( path.getPoints( capSegments ), radialSegments ); + + this.type = 'CapsuleGeometry'; + + this.parameters = { + radius: radius, + length: length, + capSegments: capSegments, + radialSegments: radialSegments, + }; + + } + + static fromJSON( data ) { + + return new CapsuleGeometry( data.radius, data.length, data.capSegments, data.radialSegments ); + + } + +} + +class CircleGeometry extends BufferGeometry { + + constructor( radius = 1, segments = 32, thetaStart = 0, thetaLength = Math.PI * 2 ) { + + super(); + + this.type = 'CircleGeometry'; + + this.parameters = { + radius: radius, + segments: segments, + thetaStart: thetaStart, + thetaLength: thetaLength + }; + + segments = Math.max( 3, segments ); + + // buffers + + const indices = []; + const vertices = []; + const normals = []; + const uvs = []; + + // helper variables + + const vertex = new Vector3(); + const uv = new Vector2(); + + // center point + + vertices.push( 0, 0, 0 ); + normals.push( 0, 0, 1 ); + uvs.push( 0.5, 0.5 ); + + for ( let s = 0, i = 3; s <= segments; s ++, i += 3 ) { + + const segment = thetaStart + s / segments * thetaLength; + + // vertex + + vertex.x = radius * Math.cos( segment ); + vertex.y = radius * Math.sin( segment ); + + vertices.push( vertex.x, vertex.y, vertex.z ); + + // normal + + normals.push( 0, 0, 1 ); + + // uvs + + uv.x = ( vertices[ i ] / radius + 1 ) / 2; + uv.y = ( vertices[ i + 1 ] / radius + 1 ) / 2; + + uvs.push( uv.x, uv.y ); + + } + + // indices + + for ( let i = 1; i <= segments; i ++ ) { + + indices.push( i, i + 1, 0 ); + + } + + // build geometry + + this.setIndex( indices ); + this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); + this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); + this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); + + } + + copy( source ) { + + super.copy( source ); + + this.parameters = Object.assign( {}, source.parameters ); + + return this; + + } + + static fromJSON( data ) { + + return new CircleGeometry( data.radius, data.segments, data.thetaStart, data.thetaLength ); + + } + +} + +class CylinderGeometry extends BufferGeometry { + + constructor( radiusTop = 1, radiusBottom = 1, height = 1, radialSegments = 32, heightSegments = 1, openEnded = false, thetaStart = 0, thetaLength = Math.PI * 2 ) { + + super(); + + this.type = 'CylinderGeometry'; + + this.parameters = { + radiusTop: radiusTop, + radiusBottom: radiusBottom, + height: height, + radialSegments: radialSegments, + heightSegments: heightSegments, + openEnded: openEnded, + thetaStart: thetaStart, + thetaLength: thetaLength + }; + + const scope = this; + + radialSegments = Math.floor( radialSegments ); + heightSegments = Math.floor( heightSegments ); + + // buffers + + const indices = []; + const vertices = []; + const normals = []; + const uvs = []; + + // helper variables + + let index = 0; + const indexArray = []; + const halfHeight = height / 2; + let groupStart = 0; + + // generate geometry + + generateTorso(); + + if ( openEnded === false ) { + + if ( radiusTop > 0 ) generateCap( true ); + if ( radiusBottom > 0 ) generateCap( false ); + + } + + // build geometry + + this.setIndex( indices ); + this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); + this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); + this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); + + function generateTorso() { + + const normal = new Vector3(); + const vertex = new Vector3(); + + let groupCount = 0; + + // this will be used to calculate the normal + const slope = ( radiusBottom - radiusTop ) / height; + + // generate vertices, normals and uvs + + for ( let y = 0; y <= heightSegments; y ++ ) { + + const indexRow = []; + + const v = y / heightSegments; + + // calculate the radius of the current row + + const radius = v * ( radiusBottom - radiusTop ) + radiusTop; + + for ( let x = 0; x <= radialSegments; x ++ ) { + + const u = x / radialSegments; + + const theta = u * thetaLength + thetaStart; + + const sinTheta = Math.sin( theta ); + const cosTheta = Math.cos( theta ); + + // vertex + + vertex.x = radius * sinTheta; + vertex.y = - v * height + halfHeight; + vertex.z = radius * cosTheta; + vertices.push( vertex.x, vertex.y, vertex.z ); + + // normal + + normal.set( sinTheta, slope, cosTheta ).normalize(); + normals.push( normal.x, normal.y, normal.z ); + + // uv + + uvs.push( u, 1 - v ); + + // save index of vertex in respective row + + indexRow.push( index ++ ); + + } + + // now save vertices of the row in our index array + + indexArray.push( indexRow ); + + } + + // generate indices + + for ( let x = 0; x < radialSegments; x ++ ) { + + for ( let y = 0; y < heightSegments; y ++ ) { + + // we use the index array to access the correct indices + + const a = indexArray[ y ][ x ]; + const b = indexArray[ y + 1 ][ x ]; + const c = indexArray[ y + 1 ][ x + 1 ]; + const d = indexArray[ y ][ x + 1 ]; + + // faces + + indices.push( a, b, d ); + indices.push( b, c, d ); + + // update group counter + + groupCount += 6; + + } + + } + + // add a group to the geometry. this will ensure multi material support + + scope.addGroup( groupStart, groupCount, 0 ); + + // calculate new start value for groups + + groupStart += groupCount; + + } + + function generateCap( top ) { + + // save the index of the first center vertex + const centerIndexStart = index; + + const uv = new Vector2(); + const vertex = new Vector3(); + + let groupCount = 0; + + const radius = ( top === true ) ? radiusTop : radiusBottom; + const sign = ( top === true ) ? 1 : - 1; + + // first we generate the center vertex data of the cap. + // because the geometry needs one set of uvs per face, + // we must generate a center vertex per face/segment + + for ( let x = 1; x <= radialSegments; x ++ ) { + + // vertex + + vertices.push( 0, halfHeight * sign, 0 ); + + // normal + + normals.push( 0, sign, 0 ); + + // uv + + uvs.push( 0.5, 0.5 ); + + // increase index + + index ++; + + } + + // save the index of the last center vertex + const centerIndexEnd = index; + + // now we generate the surrounding vertices, normals and uvs + + for ( let x = 0; x <= radialSegments; x ++ ) { + + const u = x / radialSegments; + const theta = u * thetaLength + thetaStart; + + const cosTheta = Math.cos( theta ); + const sinTheta = Math.sin( theta ); + + // vertex + + vertex.x = radius * sinTheta; + vertex.y = halfHeight * sign; + vertex.z = radius * cosTheta; + vertices.push( vertex.x, vertex.y, vertex.z ); + + // normal + + normals.push( 0, sign, 0 ); + + // uv + + uv.x = ( cosTheta * 0.5 ) + 0.5; + uv.y = ( sinTheta * 0.5 * sign ) + 0.5; + uvs.push( uv.x, uv.y ); + + // increase index + + index ++; + + } + + // generate indices + + for ( let x = 0; x < radialSegments; x ++ ) { + + const c = centerIndexStart + x; + const i = centerIndexEnd + x; + + if ( top === true ) { + + // face top + + indices.push( i, i + 1, c ); + + } else { + + // face bottom + + indices.push( i + 1, i, c ); + + } + + groupCount += 3; + + } + + // add a group to the geometry. this will ensure multi material support + + scope.addGroup( groupStart, groupCount, top === true ? 1 : 2 ); + + // calculate new start value for groups + + groupStart += groupCount; + + } + + } + + copy( source ) { + + super.copy( source ); + + this.parameters = Object.assign( {}, source.parameters ); + + return this; + + } + + static fromJSON( data ) { + + return new CylinderGeometry( data.radiusTop, data.radiusBottom, data.height, data.radialSegments, data.heightSegments, data.openEnded, data.thetaStart, data.thetaLength ); + + } + +} + +class ConeGeometry extends CylinderGeometry { + + constructor( radius = 1, height = 1, radialSegments = 32, heightSegments = 1, openEnded = false, thetaStart = 0, thetaLength = Math.PI * 2 ) { + + super( 0, radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ); + + this.type = 'ConeGeometry'; + + this.parameters = { + radius: radius, + height: height, + radialSegments: radialSegments, + heightSegments: heightSegments, + openEnded: openEnded, + thetaStart: thetaStart, + thetaLength: thetaLength + }; + + } + + static fromJSON( data ) { + + return new ConeGeometry( data.radius, data.height, data.radialSegments, data.heightSegments, data.openEnded, data.thetaStart, data.thetaLength ); + + } + +} + +class PolyhedronGeometry extends BufferGeometry { + + constructor( vertices = [], indices = [], radius = 1, detail = 0 ) { + + super(); + + this.type = 'PolyhedronGeometry'; + + this.parameters = { + vertices: vertices, + indices: indices, + radius: radius, + detail: detail + }; + + // default buffer data + + const vertexBuffer = []; + const uvBuffer = []; + + // the subdivision creates the vertex buffer data + + subdivide( detail ); + + // all vertices should lie on a conceptual sphere with a given radius + + applyRadius( radius ); + + // finally, create the uv data + + generateUVs(); + + // build non-indexed geometry + + this.setAttribute( 'position', new Float32BufferAttribute( vertexBuffer, 3 ) ); + this.setAttribute( 'normal', new Float32BufferAttribute( vertexBuffer.slice(), 3 ) ); + this.setAttribute( 'uv', new Float32BufferAttribute( uvBuffer, 2 ) ); + + if ( detail === 0 ) { + + this.computeVertexNormals(); // flat normals + + } else { + + this.normalizeNormals(); // smooth normals + + } + + // helper functions + + function subdivide( detail ) { + + const a = new Vector3(); + const b = new Vector3(); + const c = new Vector3(); + + // iterate over all faces and apply a subdivision with the given detail value + + for ( let i = 0; i < indices.length; i += 3 ) { + + // get the vertices of the face + + getVertexByIndex( indices[ i + 0 ], a ); + getVertexByIndex( indices[ i + 1 ], b ); + getVertexByIndex( indices[ i + 2 ], c ); + + // perform subdivision + + subdivideFace( a, b, c, detail ); + + } + + } + + function subdivideFace( a, b, c, detail ) { + + const cols = detail + 1; + + // we use this multidimensional array as a data structure for creating the subdivision + + const v = []; + + // construct all of the vertices for this subdivision + + for ( let i = 0; i <= cols; i ++ ) { + + v[ i ] = []; + + const aj = a.clone().lerp( c, i / cols ); + const bj = b.clone().lerp( c, i / cols ); + + const rows = cols - i; + + for ( let j = 0; j <= rows; j ++ ) { + + if ( j === 0 && i === cols ) { + + v[ i ][ j ] = aj; + + } else { + + v[ i ][ j ] = aj.clone().lerp( bj, j / rows ); + + } + + } + + } + + // construct all of the faces + + for ( let i = 0; i < cols; i ++ ) { + + for ( let j = 0; j < 2 * ( cols - i ) - 1; j ++ ) { + + const k = Math.floor( j / 2 ); + + if ( j % 2 === 0 ) { + + pushVertex( v[ i ][ k + 1 ] ); + pushVertex( v[ i + 1 ][ k ] ); + pushVertex( v[ i ][ k ] ); + + } else { + + pushVertex( v[ i ][ k + 1 ] ); + pushVertex( v[ i + 1 ][ k + 1 ] ); + pushVertex( v[ i + 1 ][ k ] ); + + } + + } + + } + + } + + function applyRadius( radius ) { + + const vertex = new Vector3(); + + // iterate over the entire buffer and apply the radius to each vertex + + for ( let i = 0; i < vertexBuffer.length; i += 3 ) { + + vertex.x = vertexBuffer[ i + 0 ]; + vertex.y = vertexBuffer[ i + 1 ]; + vertex.z = vertexBuffer[ i + 2 ]; + + vertex.normalize().multiplyScalar( radius ); + + vertexBuffer[ i + 0 ] = vertex.x; + vertexBuffer[ i + 1 ] = vertex.y; + vertexBuffer[ i + 2 ] = vertex.z; + + } + + } + + function generateUVs() { + + const vertex = new Vector3(); + + for ( let i = 0; i < vertexBuffer.length; i += 3 ) { + + vertex.x = vertexBuffer[ i + 0 ]; + vertex.y = vertexBuffer[ i + 1 ]; + vertex.z = vertexBuffer[ i + 2 ]; + + const u = azimuth( vertex ) / 2 / Math.PI + 0.5; + const v = inclination( vertex ) / Math.PI + 0.5; + uvBuffer.push( u, 1 - v ); + + } + + correctUVs(); + + correctSeam(); + + } + + function correctSeam() { + + // handle case when face straddles the seam, see #3269 + + for ( let i = 0; i < uvBuffer.length; i += 6 ) { + + // uv data of a single face + + const x0 = uvBuffer[ i + 0 ]; + const x1 = uvBuffer[ i + 2 ]; + const x2 = uvBuffer[ i + 4 ]; + + const max = Math.max( x0, x1, x2 ); + const min = Math.min( x0, x1, x2 ); + + // 0.9 is somewhat arbitrary + + if ( max > 0.9 && min < 0.1 ) { + + if ( x0 < 0.2 ) uvBuffer[ i + 0 ] += 1; + if ( x1 < 0.2 ) uvBuffer[ i + 2 ] += 1; + if ( x2 < 0.2 ) uvBuffer[ i + 4 ] += 1; + + } + + } + + } + + function pushVertex( vertex ) { + + vertexBuffer.push( vertex.x, vertex.y, vertex.z ); + + } + + function getVertexByIndex( index, vertex ) { + + const stride = index * 3; + + vertex.x = vertices[ stride + 0 ]; + vertex.y = vertices[ stride + 1 ]; + vertex.z = vertices[ stride + 2 ]; + + } + + function correctUVs() { + + const a = new Vector3(); + const b = new Vector3(); + const c = new Vector3(); + + const centroid = new Vector3(); + + const uvA = new Vector2(); + const uvB = new Vector2(); + const uvC = new Vector2(); + + for ( let i = 0, j = 0; i < vertexBuffer.length; i += 9, j += 6 ) { + + a.set( vertexBuffer[ i + 0 ], vertexBuffer[ i + 1 ], vertexBuffer[ i + 2 ] ); + b.set( vertexBuffer[ i + 3 ], vertexBuffer[ i + 4 ], vertexBuffer[ i + 5 ] ); + c.set( vertexBuffer[ i + 6 ], vertexBuffer[ i + 7 ], vertexBuffer[ i + 8 ] ); + + uvA.set( uvBuffer[ j + 0 ], uvBuffer[ j + 1 ] ); + uvB.set( uvBuffer[ j + 2 ], uvBuffer[ j + 3 ] ); + uvC.set( uvBuffer[ j + 4 ], uvBuffer[ j + 5 ] ); + + centroid.copy( a ).add( b ).add( c ).divideScalar( 3 ); + + const azi = azimuth( centroid ); + + correctUV( uvA, j + 0, a, azi ); + correctUV( uvB, j + 2, b, azi ); + correctUV( uvC, j + 4, c, azi ); + + } + + } + + function correctUV( uv, stride, vector, azimuth ) { + + if ( ( azimuth < 0 ) && ( uv.x === 1 ) ) { + + uvBuffer[ stride ] = uv.x - 1; + + } + + if ( ( vector.x === 0 ) && ( vector.z === 0 ) ) { + + uvBuffer[ stride ] = azimuth / 2 / Math.PI + 0.5; + + } + + } + + // Angle around the Y axis, counter-clockwise when looking from above. + + function azimuth( vector ) { + + return Math.atan2( vector.z, - vector.x ); + + } + + + // Angle above the XZ plane. + + function inclination( vector ) { + + return Math.atan2( - vector.y, Math.sqrt( ( vector.x * vector.x ) + ( vector.z * vector.z ) ) ); + + } + + } + + copy( source ) { + + super.copy( source ); + + this.parameters = Object.assign( {}, source.parameters ); + + return this; + + } + + static fromJSON( data ) { + + return new PolyhedronGeometry( data.vertices, data.indices, data.radius, data.details ); + + } + +} + +class DodecahedronGeometry extends PolyhedronGeometry { + + constructor( radius = 1, detail = 0 ) { + + const t = ( 1 + Math.sqrt( 5 ) ) / 2; + const r = 1 / t; + + const vertices = [ + + // (±1, ±1, ±1) + - 1, - 1, - 1, - 1, - 1, 1, + - 1, 1, - 1, - 1, 1, 1, + 1, - 1, - 1, 1, - 1, 1, + 1, 1, - 1, 1, 1, 1, + + // (0, ±1/φ, ±φ) + 0, - r, - t, 0, - r, t, + 0, r, - t, 0, r, t, + + // (±1/φ, ±φ, 0) + - r, - t, 0, - r, t, 0, + r, - t, 0, r, t, 0, + + // (±φ, 0, ±1/φ) + - t, 0, - r, t, 0, - r, + - t, 0, r, t, 0, r + ]; + + const indices = [ + 3, 11, 7, 3, 7, 15, 3, 15, 13, + 7, 19, 17, 7, 17, 6, 7, 6, 15, + 17, 4, 8, 17, 8, 10, 17, 10, 6, + 8, 0, 16, 8, 16, 2, 8, 2, 10, + 0, 12, 1, 0, 1, 18, 0, 18, 16, + 6, 10, 2, 6, 2, 13, 6, 13, 15, + 2, 16, 18, 2, 18, 3, 2, 3, 13, + 18, 1, 9, 18, 9, 11, 18, 11, 3, + 4, 14, 12, 4, 12, 0, 4, 0, 8, + 11, 9, 5, 11, 5, 19, 11, 19, 7, + 19, 5, 14, 19, 14, 4, 19, 4, 17, + 1, 12, 14, 1, 14, 5, 1, 5, 9 + ]; + + super( vertices, indices, radius, detail ); + + this.type = 'DodecahedronGeometry'; + + this.parameters = { + radius: radius, + detail: detail + }; + + } + + static fromJSON( data ) { + + return new DodecahedronGeometry( data.radius, data.detail ); + + } + +} + +const _v0 = /*@__PURE__*/ new Vector3(); +const _v1$1 = /*@__PURE__*/ new Vector3(); +const _normal = /*@__PURE__*/ new Vector3(); +const _triangle = /*@__PURE__*/ new Triangle(); + +class EdgesGeometry extends BufferGeometry { + + constructor( geometry = null, thresholdAngle = 1 ) { + + super(); + + this.type = 'EdgesGeometry'; + + this.parameters = { + geometry: geometry, + thresholdAngle: thresholdAngle + }; + + if ( geometry !== null ) { + + const precisionPoints = 4; + const precision = Math.pow( 10, precisionPoints ); + const thresholdDot = Math.cos( DEG2RAD * thresholdAngle ); + + const indexAttr = geometry.getIndex(); + const positionAttr = geometry.getAttribute( 'position' ); + const indexCount = indexAttr ? indexAttr.count : positionAttr.count; + + const indexArr = [ 0, 0, 0 ]; + const vertKeys = [ 'a', 'b', 'c' ]; + const hashes = new Array( 3 ); + + const edgeData = {}; + const vertices = []; + for ( let i = 0; i < indexCount; i += 3 ) { + + if ( indexAttr ) { + + indexArr[ 0 ] = indexAttr.getX( i ); + indexArr[ 1 ] = indexAttr.getX( i + 1 ); + indexArr[ 2 ] = indexAttr.getX( i + 2 ); + + } else { + + indexArr[ 0 ] = i; + indexArr[ 1 ] = i + 1; + indexArr[ 2 ] = i + 2; + + } + + const { a, b, c } = _triangle; + a.fromBufferAttribute( positionAttr, indexArr[ 0 ] ); + b.fromBufferAttribute( positionAttr, indexArr[ 1 ] ); + c.fromBufferAttribute( positionAttr, indexArr[ 2 ] ); + _triangle.getNormal( _normal ); + + // create hashes for the edge from the vertices + hashes[ 0 ] = `${ Math.round( a.x * precision ) },${ Math.round( a.y * precision ) },${ Math.round( a.z * precision ) }`; + hashes[ 1 ] = `${ Math.round( b.x * precision ) },${ Math.round( b.y * precision ) },${ Math.round( b.z * precision ) }`; + hashes[ 2 ] = `${ Math.round( c.x * precision ) },${ Math.round( c.y * precision ) },${ Math.round( c.z * precision ) }`; + + // skip degenerate triangles + if ( hashes[ 0 ] === hashes[ 1 ] || hashes[ 1 ] === hashes[ 2 ] || hashes[ 2 ] === hashes[ 0 ] ) { + + continue; + + } + + // iterate over every edge + for ( let j = 0; j < 3; j ++ ) { + + // get the first and next vertex making up the edge + const jNext = ( j + 1 ) % 3; + const vecHash0 = hashes[ j ]; + const vecHash1 = hashes[ jNext ]; + const v0 = _triangle[ vertKeys[ j ] ]; + const v1 = _triangle[ vertKeys[ jNext ] ]; + + const hash = `${ vecHash0 }_${ vecHash1 }`; + const reverseHash = `${ vecHash1 }_${ vecHash0 }`; + + if ( reverseHash in edgeData && edgeData[ reverseHash ] ) { + + // if we found a sibling edge add it into the vertex array if + // it meets the angle threshold and delete the edge from the map. + if ( _normal.dot( edgeData[ reverseHash ].normal ) <= thresholdDot ) { + + vertices.push( v0.x, v0.y, v0.z ); + vertices.push( v1.x, v1.y, v1.z ); + + } + + edgeData[ reverseHash ] = null; + + } else if ( ! ( hash in edgeData ) ) { + + // if we've already got an edge here then skip adding a new one + edgeData[ hash ] = { + + index0: indexArr[ j ], + index1: indexArr[ jNext ], + normal: _normal.clone(), + + }; + + } + + } + + } + + // iterate over all remaining, unmatched edges and add them to the vertex array + for ( const key in edgeData ) { + + if ( edgeData[ key ] ) { + + const { index0, index1 } = edgeData[ key ]; + _v0.fromBufferAttribute( positionAttr, index0 ); + _v1$1.fromBufferAttribute( positionAttr, index1 ); + + vertices.push( _v0.x, _v0.y, _v0.z ); + vertices.push( _v1$1.x, _v1$1.y, _v1$1.z ); + + } + + } + + this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); + + } + + } + + copy( source ) { + + super.copy( source ); + + this.parameters = Object.assign( {}, source.parameters ); + + return this; + + } + +} + +class Shape extends Path { + + constructor( points ) { + + super( points ); + + this.uuid = generateUUID(); + + this.type = 'Shape'; + + this.holes = []; + + } + + getPointsHoles( divisions ) { + + const holesPts = []; + + for ( let i = 0, l = this.holes.length; i < l; i ++ ) { + + holesPts[ i ] = this.holes[ i ].getPoints( divisions ); + + } + + return holesPts; + + } + + // get points of shape and holes (keypoints based on segments parameter) + + extractPoints( divisions ) { + + return { + + shape: this.getPoints( divisions ), + holes: this.getPointsHoles( divisions ) + + }; + + } + + copy( source ) { + + super.copy( source ); + + this.holes = []; + + for ( let i = 0, l = source.holes.length; i < l; i ++ ) { + + const hole = source.holes[ i ]; + + this.holes.push( hole.clone() ); + + } + + return this; + + } + + toJSON() { + + const data = super.toJSON(); + + data.uuid = this.uuid; + data.holes = []; + + for ( let i = 0, l = this.holes.length; i < l; i ++ ) { + + const hole = this.holes[ i ]; + data.holes.push( hole.toJSON() ); + + } + + return data; + + } + + fromJSON( json ) { + + super.fromJSON( json ); + + this.uuid = json.uuid; + this.holes = []; + + for ( let i = 0, l = json.holes.length; i < l; i ++ ) { + + const hole = json.holes[ i ]; + this.holes.push( new Path().fromJSON( hole ) ); + + } + + return this; + + } + +} + +/** + * Port from https://github.com/mapbox/earcut (v2.2.4) + */ + +const Earcut = { + + triangulate: function ( data, holeIndices, dim = 2 ) { + + const hasHoles = holeIndices && holeIndices.length; + const outerLen = hasHoles ? holeIndices[ 0 ] * dim : data.length; + let outerNode = linkedList( data, 0, outerLen, dim, true ); + const triangles = []; + + if ( ! outerNode || outerNode.next === outerNode.prev ) return triangles; + + let minX, minY, maxX, maxY, x, y, invSize; + + if ( hasHoles ) outerNode = eliminateHoles( data, holeIndices, outerNode, dim ); + + // if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox + if ( data.length > 80 * dim ) { + + minX = maxX = data[ 0 ]; + minY = maxY = data[ 1 ]; + + for ( let i = dim; i < outerLen; i += dim ) { + + x = data[ i ]; + y = data[ i + 1 ]; + if ( x < minX ) minX = x; + if ( y < minY ) minY = y; + if ( x > maxX ) maxX = x; + if ( y > maxY ) maxY = y; + + } + + // minX, minY and invSize are later used to transform coords into integers for z-order calculation + invSize = Math.max( maxX - minX, maxY - minY ); + invSize = invSize !== 0 ? 32767 / invSize : 0; + + } + + earcutLinked( outerNode, triangles, dim, minX, minY, invSize, 0 ); + + return triangles; + + } + +}; + +// create a circular doubly linked list from polygon points in the specified winding order +function linkedList( data, start, end, dim, clockwise ) { + + let i, last; + + if ( clockwise === ( signedArea( data, start, end, dim ) > 0 ) ) { + + for ( i = start; i < end; i += dim ) last = insertNode( i, data[ i ], data[ i + 1 ], last ); + + } else { + + for ( i = end - dim; i >= start; i -= dim ) last = insertNode( i, data[ i ], data[ i + 1 ], last ); + + } + + if ( last && equals( last, last.next ) ) { + + removeNode( last ); + last = last.next; + + } + + return last; + +} + +// eliminate colinear or duplicate points +function filterPoints( start, end ) { + + if ( ! start ) return start; + if ( ! end ) end = start; + + let p = start, + again; + do { + + again = false; + + if ( ! p.steiner && ( equals( p, p.next ) || area( p.prev, p, p.next ) === 0 ) ) { + + removeNode( p ); + p = end = p.prev; + if ( p === p.next ) break; + again = true; + + } else { + + p = p.next; + + } + + } while ( again || p !== end ); + + return end; + +} + +// main ear slicing loop which triangulates a polygon (given as a linked list) +function earcutLinked( ear, triangles, dim, minX, minY, invSize, pass ) { + + if ( ! ear ) return; + + // interlink polygon nodes in z-order + if ( ! pass && invSize ) indexCurve( ear, minX, minY, invSize ); + + let stop = ear, + prev, next; + + // iterate through ears, slicing them one by one + while ( ear.prev !== ear.next ) { + + prev = ear.prev; + next = ear.next; + + if ( invSize ? isEarHashed( ear, minX, minY, invSize ) : isEar( ear ) ) { + + // cut off the triangle + triangles.push( prev.i / dim | 0 ); + triangles.push( ear.i / dim | 0 ); + triangles.push( next.i / dim | 0 ); + + removeNode( ear ); + + // skipping the next vertex leads to less sliver triangles + ear = next.next; + stop = next.next; + + continue; + + } + + ear = next; + + // if we looped through the whole remaining polygon and can't find any more ears + if ( ear === stop ) { + + // try filtering points and slicing again + if ( ! pass ) { + + earcutLinked( filterPoints( ear ), triangles, dim, minX, minY, invSize, 1 ); + + // if this didn't work, try curing all small self-intersections locally + + } else if ( pass === 1 ) { + + ear = cureLocalIntersections( filterPoints( ear ), triangles, dim ); + earcutLinked( ear, triangles, dim, minX, minY, invSize, 2 ); + + // as a last resort, try splitting the remaining polygon into two + + } else if ( pass === 2 ) { + + splitEarcut( ear, triangles, dim, minX, minY, invSize ); + + } + + break; + + } + + } + +} + +// check whether a polygon node forms a valid ear with adjacent nodes +function isEar( ear ) { + + const a = ear.prev, + b = ear, + c = ear.next; + + if ( area( a, b, c ) >= 0 ) return false; // reflex, can't be an ear + + // now make sure we don't have other points inside the potential ear + const ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y; + + // triangle bbox; min & max are calculated like this for speed + const x0 = ax < bx ? ( ax < cx ? ax : cx ) : ( bx < cx ? bx : cx ), + y0 = ay < by ? ( ay < cy ? ay : cy ) : ( by < cy ? by : cy ), + x1 = ax > bx ? ( ax > cx ? ax : cx ) : ( bx > cx ? bx : cx ), + y1 = ay > by ? ( ay > cy ? ay : cy ) : ( by > cy ? by : cy ); + + let p = c.next; + while ( p !== a ) { + + if ( p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && + pointInTriangle( ax, ay, bx, by, cx, cy, p.x, p.y ) && + area( p.prev, p, p.next ) >= 0 ) return false; + p = p.next; + + } + + return true; + +} + +function isEarHashed( ear, minX, minY, invSize ) { + + const a = ear.prev, + b = ear, + c = ear.next; + + if ( area( a, b, c ) >= 0 ) return false; // reflex, can't be an ear + + const ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y; + + // triangle bbox; min & max are calculated like this for speed + const x0 = ax < bx ? ( ax < cx ? ax : cx ) : ( bx < cx ? bx : cx ), + y0 = ay < by ? ( ay < cy ? ay : cy ) : ( by < cy ? by : cy ), + x1 = ax > bx ? ( ax > cx ? ax : cx ) : ( bx > cx ? bx : cx ), + y1 = ay > by ? ( ay > cy ? ay : cy ) : ( by > cy ? by : cy ); + + // z-order range for the current triangle bbox; + const minZ = zOrder( x0, y0, minX, minY, invSize ), + maxZ = zOrder( x1, y1, minX, minY, invSize ); + + let p = ear.prevZ, + n = ear.nextZ; + + // look for points inside the triangle in both directions + while ( p && p.z >= minZ && n && n.z <= maxZ ) { + + if ( p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c && + pointInTriangle( ax, ay, bx, by, cx, cy, p.x, p.y ) && area( p.prev, p, p.next ) >= 0 ) return false; + p = p.prevZ; + + if ( n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c && + pointInTriangle( ax, ay, bx, by, cx, cy, n.x, n.y ) && area( n.prev, n, n.next ) >= 0 ) return false; + n = n.nextZ; + + } + + // look for remaining points in decreasing z-order + while ( p && p.z >= minZ ) { + + if ( p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c && + pointInTriangle( ax, ay, bx, by, cx, cy, p.x, p.y ) && area( p.prev, p, p.next ) >= 0 ) return false; + p = p.prevZ; + + } + + // look for remaining points in increasing z-order + while ( n && n.z <= maxZ ) { + + if ( n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c && + pointInTriangle( ax, ay, bx, by, cx, cy, n.x, n.y ) && area( n.prev, n, n.next ) >= 0 ) return false; + n = n.nextZ; + + } + + return true; + +} + +// go through all polygon nodes and cure small local self-intersections +function cureLocalIntersections( start, triangles, dim ) { + + let p = start; + do { + + const a = p.prev, + b = p.next.next; + + if ( ! equals( a, b ) && intersects( a, p, p.next, b ) && locallyInside( a, b ) && locallyInside( b, a ) ) { + + triangles.push( a.i / dim | 0 ); + triangles.push( p.i / dim | 0 ); + triangles.push( b.i / dim | 0 ); + + // remove two nodes involved + removeNode( p ); + removeNode( p.next ); + + p = start = b; + + } + + p = p.next; + + } while ( p !== start ); + + return filterPoints( p ); + +} + +// try splitting polygon into two and triangulate them independently +function splitEarcut( start, triangles, dim, minX, minY, invSize ) { + + // look for a valid diagonal that divides the polygon into two + let a = start; + do { + + let b = a.next.next; + while ( b !== a.prev ) { + + if ( a.i !== b.i && isValidDiagonal( a, b ) ) { + + // split the polygon in two by the diagonal + let c = splitPolygon( a, b ); + + // filter colinear points around the cuts + a = filterPoints( a, a.next ); + c = filterPoints( c, c.next ); + + // run earcut on each half + earcutLinked( a, triangles, dim, minX, minY, invSize, 0 ); + earcutLinked( c, triangles, dim, minX, minY, invSize, 0 ); + return; + + } + + b = b.next; + + } + + a = a.next; + + } while ( a !== start ); + +} + +// link every hole into the outer loop, producing a single-ring polygon without holes +function eliminateHoles( data, holeIndices, outerNode, dim ) { + + const queue = []; + let i, len, start, end, list; + + for ( i = 0, len = holeIndices.length; i < len; i ++ ) { + + start = holeIndices[ i ] * dim; + end = i < len - 1 ? holeIndices[ i + 1 ] * dim : data.length; + list = linkedList( data, start, end, dim, false ); + if ( list === list.next ) list.steiner = true; + queue.push( getLeftmost( list ) ); + + } + + queue.sort( compareX ); + + // process holes from left to right + for ( i = 0; i < queue.length; i ++ ) { + + outerNode = eliminateHole( queue[ i ], outerNode ); + + } + + return outerNode; + +} + +function compareX( a, b ) { + + return a.x - b.x; + +} + +// find a bridge between vertices that connects hole with an outer ring and link it +function eliminateHole( hole, outerNode ) { + + const bridge = findHoleBridge( hole, outerNode ); + if ( ! bridge ) { + + return outerNode; + + } + + const bridgeReverse = splitPolygon( bridge, hole ); + + // filter collinear points around the cuts + filterPoints( bridgeReverse, bridgeReverse.next ); + return filterPoints( bridge, bridge.next ); + +} + +// David Eberly's algorithm for finding a bridge between hole and outer polygon +function findHoleBridge( hole, outerNode ) { + + let p = outerNode, + qx = - Infinity, + m; + + const hx = hole.x, hy = hole.y; + + // find a segment intersected by a ray from the hole's leftmost point to the left; + // segment's endpoint with lesser x will be potential connection point + do { + + if ( hy <= p.y && hy >= p.next.y && p.next.y !== p.y ) { + + const x = p.x + ( hy - p.y ) * ( p.next.x - p.x ) / ( p.next.y - p.y ); + if ( x <= hx && x > qx ) { + + qx = x; + m = p.x < p.next.x ? p : p.next; + if ( x === hx ) return m; // hole touches outer segment; pick leftmost endpoint + + } + + } + + p = p.next; + + } while ( p !== outerNode ); + + if ( ! m ) return null; + + // look for points inside the triangle of hole point, segment intersection and endpoint; + // if there are no points found, we have a valid connection; + // otherwise choose the point of the minimum angle with the ray as connection point + + const stop = m, + mx = m.x, + my = m.y; + let tanMin = Infinity, tan; + + p = m; + + do { + + if ( hx >= p.x && p.x >= mx && hx !== p.x && + pointInTriangle( hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y ) ) { + + tan = Math.abs( hy - p.y ) / ( hx - p.x ); // tangential + + if ( locallyInside( p, hole ) && ( tan < tanMin || ( tan === tanMin && ( p.x > m.x || ( p.x === m.x && sectorContainsSector( m, p ) ) ) ) ) ) { + + m = p; + tanMin = tan; + + } + + } + + p = p.next; + + } while ( p !== stop ); + + return m; + +} + +// whether sector in vertex m contains sector in vertex p in the same coordinates +function sectorContainsSector( m, p ) { + + return area( m.prev, m, p.prev ) < 0 && area( p.next, m, m.next ) < 0; + +} + +// interlink polygon nodes in z-order +function indexCurve( start, minX, minY, invSize ) { + + let p = start; + do { + + if ( p.z === 0 ) p.z = zOrder( p.x, p.y, minX, minY, invSize ); + p.prevZ = p.prev; + p.nextZ = p.next; + p = p.next; + + } while ( p !== start ); + + p.prevZ.nextZ = null; + p.prevZ = null; + + sortLinked( p ); + +} + +// Simon Tatham's linked list merge sort algorithm +// http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html +function sortLinked( list ) { + + let i, p, q, e, tail, numMerges, pSize, qSize, + inSize = 1; + + do { + + p = list; + list = null; + tail = null; + numMerges = 0; + + while ( p ) { + + numMerges ++; + q = p; + pSize = 0; + for ( i = 0; i < inSize; i ++ ) { + + pSize ++; + q = q.nextZ; + if ( ! q ) break; + + } + + qSize = inSize; + + while ( pSize > 0 || ( qSize > 0 && q ) ) { + + if ( pSize !== 0 && ( qSize === 0 || ! q || p.z <= q.z ) ) { + + e = p; + p = p.nextZ; + pSize --; + + } else { + + e = q; + q = q.nextZ; + qSize --; + + } + + if ( tail ) tail.nextZ = e; + else list = e; + + e.prevZ = tail; + tail = e; + + } + + p = q; + + } + + tail.nextZ = null; + inSize *= 2; + + } while ( numMerges > 1 ); + + return list; + +} + +// z-order of a point given coords and inverse of the longer side of data bbox +function zOrder( x, y, minX, minY, invSize ) { + + // coords are transformed into non-negative 15-bit integer range + x = ( x - minX ) * invSize | 0; + y = ( y - minY ) * invSize | 0; + + x = ( x | ( x << 8 ) ) & 0x00FF00FF; + x = ( x | ( x << 4 ) ) & 0x0F0F0F0F; + x = ( x | ( x << 2 ) ) & 0x33333333; + x = ( x | ( x << 1 ) ) & 0x55555555; + + y = ( y | ( y << 8 ) ) & 0x00FF00FF; + y = ( y | ( y << 4 ) ) & 0x0F0F0F0F; + y = ( y | ( y << 2 ) ) & 0x33333333; + y = ( y | ( y << 1 ) ) & 0x55555555; + + return x | ( y << 1 ); + +} + +// find the leftmost node of a polygon ring +function getLeftmost( start ) { + + let p = start, + leftmost = start; + do { + + if ( p.x < leftmost.x || ( p.x === leftmost.x && p.y < leftmost.y ) ) leftmost = p; + p = p.next; + + } while ( p !== start ); + + return leftmost; + +} + +// check if a point lies within a convex triangle +function pointInTriangle( ax, ay, bx, by, cx, cy, px, py ) { + + return ( cx - px ) * ( ay - py ) >= ( ax - px ) * ( cy - py ) && + ( ax - px ) * ( by - py ) >= ( bx - px ) * ( ay - py ) && + ( bx - px ) * ( cy - py ) >= ( cx - px ) * ( by - py ); + +} + +// check if a diagonal between two polygon nodes is valid (lies in polygon interior) +function isValidDiagonal( a, b ) { + + return a.next.i !== b.i && a.prev.i !== b.i && ! intersectsPolygon( a, b ) && // dones't intersect other edges + ( locallyInside( a, b ) && locallyInside( b, a ) && middleInside( a, b ) && // locally visible + ( area( a.prev, a, b.prev ) || area( a, b.prev, b ) ) || // does not create opposite-facing sectors + equals( a, b ) && area( a.prev, a, a.next ) > 0 && area( b.prev, b, b.next ) > 0 ); // special zero-length case + +} + +// signed area of a triangle +function area( p, q, r ) { + + return ( q.y - p.y ) * ( r.x - q.x ) - ( q.x - p.x ) * ( r.y - q.y ); + +} + +// check if two points are equal +function equals( p1, p2 ) { + + return p1.x === p2.x && p1.y === p2.y; + +} + +// check if two segments intersect +function intersects( p1, q1, p2, q2 ) { + + const o1 = sign( area( p1, q1, p2 ) ); + const o2 = sign( area( p1, q1, q2 ) ); + const o3 = sign( area( p2, q2, p1 ) ); + const o4 = sign( area( p2, q2, q1 ) ); + + if ( o1 !== o2 && o3 !== o4 ) return true; // general case + + if ( o1 === 0 && onSegment( p1, p2, q1 ) ) return true; // p1, q1 and p2 are collinear and p2 lies on p1q1 + if ( o2 === 0 && onSegment( p1, q2, q1 ) ) return true; // p1, q1 and q2 are collinear and q2 lies on p1q1 + if ( o3 === 0 && onSegment( p2, p1, q2 ) ) return true; // p2, q2 and p1 are collinear and p1 lies on p2q2 + if ( o4 === 0 && onSegment( p2, q1, q2 ) ) return true; // p2, q2 and q1 are collinear and q1 lies on p2q2 + + return false; + +} + +// for collinear points p, q, r, check if point q lies on segment pr +function onSegment( p, q, r ) { + + return q.x <= Math.max( p.x, r.x ) && q.x >= Math.min( p.x, r.x ) && q.y <= Math.max( p.y, r.y ) && q.y >= Math.min( p.y, r.y ); + +} + +function sign( num ) { + + return num > 0 ? 1 : num < 0 ? - 1 : 0; + +} + +// check if a polygon diagonal intersects any polygon segments +function intersectsPolygon( a, b ) { + + let p = a; + do { + + if ( p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i && + intersects( p, p.next, a, b ) ) return true; + p = p.next; + + } while ( p !== a ); + + return false; + +} + +// check if a polygon diagonal is locally inside the polygon +function locallyInside( a, b ) { + + return area( a.prev, a, a.next ) < 0 ? + area( a, b, a.next ) >= 0 && area( a, a.prev, b ) >= 0 : + area( a, b, a.prev ) < 0 || area( a, a.next, b ) < 0; + +} + +// check if the middle point of a polygon diagonal is inside the polygon +function middleInside( a, b ) { + + let p = a, + inside = false; + const px = ( a.x + b.x ) / 2, + py = ( a.y + b.y ) / 2; + do { + + if ( ( ( p.y > py ) !== ( p.next.y > py ) ) && p.next.y !== p.y && + ( px < ( p.next.x - p.x ) * ( py - p.y ) / ( p.next.y - p.y ) + p.x ) ) + inside = ! inside; + p = p.next; + + } while ( p !== a ); + + return inside; + +} + +// link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two; +// if one belongs to the outer ring and another to a hole, it merges it into a single ring +function splitPolygon( a, b ) { + + const a2 = new Node( a.i, a.x, a.y ), + b2 = new Node( b.i, b.x, b.y ), + an = a.next, + bp = b.prev; + + a.next = b; + b.prev = a; + + a2.next = an; + an.prev = a2; + + b2.next = a2; + a2.prev = b2; + + bp.next = b2; + b2.prev = bp; + + return b2; + +} + +// create a node and optionally link it with previous one (in a circular doubly linked list) +function insertNode( i, x, y, last ) { + + const p = new Node( i, x, y ); + + if ( ! last ) { + + p.prev = p; + p.next = p; + + } else { + + p.next = last.next; + p.prev = last; + last.next.prev = p; + last.next = p; + + } + + return p; + +} + +function removeNode( p ) { + + p.next.prev = p.prev; + p.prev.next = p.next; + + if ( p.prevZ ) p.prevZ.nextZ = p.nextZ; + if ( p.nextZ ) p.nextZ.prevZ = p.prevZ; + +} + +function Node( i, x, y ) { + + // vertex index in coordinates array + this.i = i; + + // vertex coordinates + this.x = x; + this.y = y; + + // previous and next vertex nodes in a polygon ring + this.prev = null; + this.next = null; + + // z-order curve value + this.z = 0; + + // previous and next nodes in z-order + this.prevZ = null; + this.nextZ = null; + + // indicates whether this is a steiner point + this.steiner = false; + +} + +function signedArea( data, start, end, dim ) { + + let sum = 0; + for ( let i = start, j = end - dim; i < end; i += dim ) { + + sum += ( data[ j ] - data[ i ] ) * ( data[ i + 1 ] + data[ j + 1 ] ); + j = i; + + } + + return sum; + +} + +class ShapeUtils { + + // calculate area of the contour polygon + + static area( contour ) { + + const n = contour.length; + let a = 0.0; + + for ( let p = n - 1, q = 0; q < n; p = q ++ ) { + + a += contour[ p ].x * contour[ q ].y - contour[ q ].x * contour[ p ].y; + + } + + return a * 0.5; + + } + + static isClockWise( pts ) { + + return ShapeUtils.area( pts ) < 0; + + } + + static triangulateShape( contour, holes ) { + + const vertices = []; // flat array of vertices like [ x0,y0, x1,y1, x2,y2, ... ] + const holeIndices = []; // array of hole indices + const faces = []; // final array of vertex indices like [ [ a,b,d ], [ b,c,d ] ] + + removeDupEndPts( contour ); + addContour( vertices, contour ); + + // + + let holeIndex = contour.length; + + holes.forEach( removeDupEndPts ); + + for ( let i = 0; i < holes.length; i ++ ) { + + holeIndices.push( holeIndex ); + holeIndex += holes[ i ].length; + addContour( vertices, holes[ i ] ); + + } + + // + + const triangles = Earcut.triangulate( vertices, holeIndices ); + + // + + for ( let i = 0; i < triangles.length; i += 3 ) { + + faces.push( triangles.slice( i, i + 3 ) ); + + } + + return faces; + + } + +} + +function removeDupEndPts( points ) { + + const l = points.length; + + if ( l > 2 && points[ l - 1 ].equals( points[ 0 ] ) ) { + + points.pop(); + + } + +} + +function addContour( vertices, contour ) { + + for ( let i = 0; i < contour.length; i ++ ) { + + vertices.push( contour[ i ].x ); + vertices.push( contour[ i ].y ); + + } + +} + +/** + * Creates extruded geometry from a path shape. + * + * parameters = { + * + * curveSegments: , // number of points on the curves + * steps: , // number of points for z-side extrusions / used for subdividing segments of extrude spline too + * depth: , // Depth to extrude the shape + * + * bevelEnabled: , // turn on bevel + * bevelThickness: , // how deep into the original shape bevel goes + * bevelSize: , // how far from shape outline (including bevelOffset) is bevel + * bevelOffset: , // how far from shape outline does bevel start + * bevelSegments: , // number of bevel layers + * + * extrudePath: // curve to extrude shape along + * + * UVGenerator: // object that provides UV generator functions + * + * } + */ + + +class ExtrudeGeometry extends BufferGeometry { + + constructor( shapes = new Shape( [ new Vector2( 0.5, 0.5 ), new Vector2( - 0.5, 0.5 ), new Vector2( - 0.5, - 0.5 ), new Vector2( 0.5, - 0.5 ) ] ), options = {} ) { + + super(); + + this.type = 'ExtrudeGeometry'; + + this.parameters = { + shapes: shapes, + options: options + }; + + shapes = Array.isArray( shapes ) ? shapes : [ shapes ]; + + const scope = this; + + const verticesArray = []; + const uvArray = []; + + for ( let i = 0, l = shapes.length; i < l; i ++ ) { + + const shape = shapes[ i ]; + addShape( shape ); + + } + + // build geometry + + this.setAttribute( 'position', new Float32BufferAttribute( verticesArray, 3 ) ); + this.setAttribute( 'uv', new Float32BufferAttribute( uvArray, 2 ) ); + + this.computeVertexNormals(); + + // functions + + function addShape( shape ) { + + const placeholder = []; + + // options + + const curveSegments = options.curveSegments !== undefined ? options.curveSegments : 12; + const steps = options.steps !== undefined ? options.steps : 1; + const depth = options.depth !== undefined ? options.depth : 1; + + let bevelEnabled = options.bevelEnabled !== undefined ? options.bevelEnabled : true; + let bevelThickness = options.bevelThickness !== undefined ? options.bevelThickness : 0.2; + let bevelSize = options.bevelSize !== undefined ? options.bevelSize : bevelThickness - 0.1; + let bevelOffset = options.bevelOffset !== undefined ? options.bevelOffset : 0; + let bevelSegments = options.bevelSegments !== undefined ? options.bevelSegments : 3; + + const extrudePath = options.extrudePath; + + const uvgen = options.UVGenerator !== undefined ? options.UVGenerator : WorldUVGenerator; + + // + + let extrudePts, extrudeByPath = false; + let splineTube, binormal, normal, position2; + + if ( extrudePath ) { + + extrudePts = extrudePath.getSpacedPoints( steps ); + + extrudeByPath = true; + bevelEnabled = false; // bevels not supported for path extrusion + + // SETUP TNB variables + + // TODO1 - have a .isClosed in spline? + + splineTube = extrudePath.computeFrenetFrames( steps, false ); + + // console.log(splineTube, 'splineTube', splineTube.normals.length, 'steps', steps, 'extrudePts', extrudePts.length); + + binormal = new Vector3(); + normal = new Vector3(); + position2 = new Vector3(); + + } + + // Safeguards if bevels are not enabled + + if ( ! bevelEnabled ) { + + bevelSegments = 0; + bevelThickness = 0; + bevelSize = 0; + bevelOffset = 0; + + } + + // Variables initialization + + const shapePoints = shape.extractPoints( curveSegments ); + + let vertices = shapePoints.shape; + const holes = shapePoints.holes; + + const reverse = ! ShapeUtils.isClockWise( vertices ); + + if ( reverse ) { + + vertices = vertices.reverse(); + + // Maybe we should also check if holes are in the opposite direction, just to be safe ... + + for ( let h = 0, hl = holes.length; h < hl; h ++ ) { + + const ahole = holes[ h ]; + + if ( ShapeUtils.isClockWise( ahole ) ) { + + holes[ h ] = ahole.reverse(); + + } + + } + + } + + + const faces = ShapeUtils.triangulateShape( vertices, holes ); + + /* Vertices */ + + const contour = vertices; // vertices has all points but contour has only points of circumference + + for ( let h = 0, hl = holes.length; h < hl; h ++ ) { + + const ahole = holes[ h ]; + + vertices = vertices.concat( ahole ); + + } + + + function scalePt2( pt, vec, size ) { + + if ( ! vec ) console.error( 'THREE.ExtrudeGeometry: vec does not exist' ); + + return pt.clone().addScaledVector( vec, size ); + + } + + const vlen = vertices.length, flen = faces.length; + + + // Find directions for point movement + + + function getBevelVec( inPt, inPrev, inNext ) { + + // computes for inPt the corresponding point inPt' on a new contour + // shifted by 1 unit (length of normalized vector) to the left + // if we walk along contour clockwise, this new contour is outside the old one + // + // inPt' is the intersection of the two lines parallel to the two + // adjacent edges of inPt at a distance of 1 unit on the left side. + + let v_trans_x, v_trans_y, shrink_by; // resulting translation vector for inPt + + // good reading for geometry algorithms (here: line-line intersection) + // http://geomalgorithms.com/a05-_intersect-1.html + + const v_prev_x = inPt.x - inPrev.x, + v_prev_y = inPt.y - inPrev.y; + const v_next_x = inNext.x - inPt.x, + v_next_y = inNext.y - inPt.y; + + const v_prev_lensq = ( v_prev_x * v_prev_x + v_prev_y * v_prev_y ); + + // check for collinear edges + const collinear0 = ( v_prev_x * v_next_y - v_prev_y * v_next_x ); + + if ( Math.abs( collinear0 ) > Number.EPSILON ) { + + // not collinear + + // length of vectors for normalizing + + const v_prev_len = Math.sqrt( v_prev_lensq ); + const v_next_len = Math.sqrt( v_next_x * v_next_x + v_next_y * v_next_y ); + + // shift adjacent points by unit vectors to the left + + const ptPrevShift_x = ( inPrev.x - v_prev_y / v_prev_len ); + const ptPrevShift_y = ( inPrev.y + v_prev_x / v_prev_len ); + + const ptNextShift_x = ( inNext.x - v_next_y / v_next_len ); + const ptNextShift_y = ( inNext.y + v_next_x / v_next_len ); + + // scaling factor for v_prev to intersection point + + const sf = ( ( ptNextShift_x - ptPrevShift_x ) * v_next_y - + ( ptNextShift_y - ptPrevShift_y ) * v_next_x ) / + ( v_prev_x * v_next_y - v_prev_y * v_next_x ); + + // vector from inPt to intersection point + + v_trans_x = ( ptPrevShift_x + v_prev_x * sf - inPt.x ); + v_trans_y = ( ptPrevShift_y + v_prev_y * sf - inPt.y ); + + // Don't normalize!, otherwise sharp corners become ugly + // but prevent crazy spikes + const v_trans_lensq = ( v_trans_x * v_trans_x + v_trans_y * v_trans_y ); + if ( v_trans_lensq <= 2 ) { + + return new Vector2( v_trans_x, v_trans_y ); + + } else { + + shrink_by = Math.sqrt( v_trans_lensq / 2 ); + + } + + } else { + + // handle special case of collinear edges + + let direction_eq = false; // assumes: opposite + + if ( v_prev_x > Number.EPSILON ) { + + if ( v_next_x > Number.EPSILON ) { + + direction_eq = true; + + } + + } else { + + if ( v_prev_x < - Number.EPSILON ) { + + if ( v_next_x < - Number.EPSILON ) { + + direction_eq = true; + + } + + } else { + + if ( Math.sign( v_prev_y ) === Math.sign( v_next_y ) ) { + + direction_eq = true; + + } + + } + + } + + if ( direction_eq ) { + + // console.log("Warning: lines are a straight sequence"); + v_trans_x = - v_prev_y; + v_trans_y = v_prev_x; + shrink_by = Math.sqrt( v_prev_lensq ); + + } else { + + // console.log("Warning: lines are a straight spike"); + v_trans_x = v_prev_x; + v_trans_y = v_prev_y; + shrink_by = Math.sqrt( v_prev_lensq / 2 ); + + } + + } + + return new Vector2( v_trans_x / shrink_by, v_trans_y / shrink_by ); + + } + + + const contourMovements = []; + + for ( let i = 0, il = contour.length, j = il - 1, k = i + 1; i < il; i ++, j ++, k ++ ) { + + if ( j === il ) j = 0; + if ( k === il ) k = 0; + + // (j)---(i)---(k) + // console.log('i,j,k', i, j , k) + + contourMovements[ i ] = getBevelVec( contour[ i ], contour[ j ], contour[ k ] ); + + } + + const holesMovements = []; + let oneHoleMovements, verticesMovements = contourMovements.concat(); + + for ( let h = 0, hl = holes.length; h < hl; h ++ ) { + + const ahole = holes[ h ]; + + oneHoleMovements = []; + + for ( let i = 0, il = ahole.length, j = il - 1, k = i + 1; i < il; i ++, j ++, k ++ ) { + + if ( j === il ) j = 0; + if ( k === il ) k = 0; + + // (j)---(i)---(k) + oneHoleMovements[ i ] = getBevelVec( ahole[ i ], ahole[ j ], ahole[ k ] ); + + } + + holesMovements.push( oneHoleMovements ); + verticesMovements = verticesMovements.concat( oneHoleMovements ); + + } + + + // Loop bevelSegments, 1 for the front, 1 for the back + + for ( let b = 0; b < bevelSegments; b ++ ) { + + //for ( b = bevelSegments; b > 0; b -- ) { + + const t = b / bevelSegments; + const z = bevelThickness * Math.cos( t * Math.PI / 2 ); + const bs = bevelSize * Math.sin( t * Math.PI / 2 ) + bevelOffset; + + // contract shape + + for ( let i = 0, il = contour.length; i < il; i ++ ) { + + const vert = scalePt2( contour[ i ], contourMovements[ i ], bs ); + + v( vert.x, vert.y, - z ); + + } + + // expand holes + + for ( let h = 0, hl = holes.length; h < hl; h ++ ) { + + const ahole = holes[ h ]; + oneHoleMovements = holesMovements[ h ]; + + for ( let i = 0, il = ahole.length; i < il; i ++ ) { + + const vert = scalePt2( ahole[ i ], oneHoleMovements[ i ], bs ); + + v( vert.x, vert.y, - z ); + + } + + } + + } + + const bs = bevelSize + bevelOffset; + + // Back facing vertices + + for ( let i = 0; i < vlen; i ++ ) { + + const vert = bevelEnabled ? scalePt2( vertices[ i ], verticesMovements[ i ], bs ) : vertices[ i ]; + + if ( ! extrudeByPath ) { + + v( vert.x, vert.y, 0 ); + + } else { + + // v( vert.x, vert.y + extrudePts[ 0 ].y, extrudePts[ 0 ].x ); + + normal.copy( splineTube.normals[ 0 ] ).multiplyScalar( vert.x ); + binormal.copy( splineTube.binormals[ 0 ] ).multiplyScalar( vert.y ); + + position2.copy( extrudePts[ 0 ] ).add( normal ).add( binormal ); + + v( position2.x, position2.y, position2.z ); + + } + + } + + // Add stepped vertices... + // Including front facing vertices + + for ( let s = 1; s <= steps; s ++ ) { + + for ( let i = 0; i < vlen; i ++ ) { + + const vert = bevelEnabled ? scalePt2( vertices[ i ], verticesMovements[ i ], bs ) : vertices[ i ]; + + if ( ! extrudeByPath ) { + + v( vert.x, vert.y, depth / steps * s ); + + } else { + + // v( vert.x, vert.y + extrudePts[ s - 1 ].y, extrudePts[ s - 1 ].x ); + + normal.copy( splineTube.normals[ s ] ).multiplyScalar( vert.x ); + binormal.copy( splineTube.binormals[ s ] ).multiplyScalar( vert.y ); + + position2.copy( extrudePts[ s ] ).add( normal ).add( binormal ); + + v( position2.x, position2.y, position2.z ); + + } + + } + + } + + + // Add bevel segments planes + + //for ( b = 1; b <= bevelSegments; b ++ ) { + for ( let b = bevelSegments - 1; b >= 0; b -- ) { + + const t = b / bevelSegments; + const z = bevelThickness * Math.cos( t * Math.PI / 2 ); + const bs = bevelSize * Math.sin( t * Math.PI / 2 ) + bevelOffset; + + // contract shape + + for ( let i = 0, il = contour.length; i < il; i ++ ) { + + const vert = scalePt2( contour[ i ], contourMovements[ i ], bs ); + v( vert.x, vert.y, depth + z ); + + } + + // expand holes + + for ( let h = 0, hl = holes.length; h < hl; h ++ ) { + + const ahole = holes[ h ]; + oneHoleMovements = holesMovements[ h ]; + + for ( let i = 0, il = ahole.length; i < il; i ++ ) { + + const vert = scalePt2( ahole[ i ], oneHoleMovements[ i ], bs ); + + if ( ! extrudeByPath ) { + + v( vert.x, vert.y, depth + z ); + + } else { + + v( vert.x, vert.y + extrudePts[ steps - 1 ].y, extrudePts[ steps - 1 ].x + z ); + + } + + } + + } + + } + + /* Faces */ + + // Top and bottom faces + + buildLidFaces(); + + // Sides faces + + buildSideFaces(); + + + ///// Internal functions + + function buildLidFaces() { + + const start = verticesArray.length / 3; + + if ( bevelEnabled ) { + + let layer = 0; // steps + 1 + let offset = vlen * layer; + + // Bottom faces + + for ( let i = 0; i < flen; i ++ ) { + + const face = faces[ i ]; + f3( face[ 2 ] + offset, face[ 1 ] + offset, face[ 0 ] + offset ); + + } + + layer = steps + bevelSegments * 2; + offset = vlen * layer; + + // Top faces + + for ( let i = 0; i < flen; i ++ ) { + + const face = faces[ i ]; + f3( face[ 0 ] + offset, face[ 1 ] + offset, face[ 2 ] + offset ); + + } + + } else { + + // Bottom faces + + for ( let i = 0; i < flen; i ++ ) { + + const face = faces[ i ]; + f3( face[ 2 ], face[ 1 ], face[ 0 ] ); + + } + + // Top faces + + for ( let i = 0; i < flen; i ++ ) { + + const face = faces[ i ]; + f3( face[ 0 ] + vlen * steps, face[ 1 ] + vlen * steps, face[ 2 ] + vlen * steps ); + + } + + } + + scope.addGroup( start, verticesArray.length / 3 - start, 0 ); + + } + + // Create faces for the z-sides of the shape + + function buildSideFaces() { + + const start = verticesArray.length / 3; + let layeroffset = 0; + sidewalls( contour, layeroffset ); + layeroffset += contour.length; + + for ( let h = 0, hl = holes.length; h < hl; h ++ ) { + + const ahole = holes[ h ]; + sidewalls( ahole, layeroffset ); + + //, true + layeroffset += ahole.length; + + } + + + scope.addGroup( start, verticesArray.length / 3 - start, 1 ); + + + } + + function sidewalls( contour, layeroffset ) { + + let i = contour.length; + + while ( -- i >= 0 ) { + + const j = i; + let k = i - 1; + if ( k < 0 ) k = contour.length - 1; + + //console.log('b', i,j, i-1, k,vertices.length); + + for ( let s = 0, sl = ( steps + bevelSegments * 2 ); s < sl; s ++ ) { + + const slen1 = vlen * s; + const slen2 = vlen * ( s + 1 ); + + const a = layeroffset + j + slen1, + b = layeroffset + k + slen1, + c = layeroffset + k + slen2, + d = layeroffset + j + slen2; + + f4( a, b, c, d ); + + } + + } + + } + + function v( x, y, z ) { + + placeholder.push( x ); + placeholder.push( y ); + placeholder.push( z ); + + } + + + function f3( a, b, c ) { + + addVertex( a ); + addVertex( b ); + addVertex( c ); + + const nextIndex = verticesArray.length / 3; + const uvs = uvgen.generateTopUV( scope, verticesArray, nextIndex - 3, nextIndex - 2, nextIndex - 1 ); + + addUV( uvs[ 0 ] ); + addUV( uvs[ 1 ] ); + addUV( uvs[ 2 ] ); + + } + + function f4( a, b, c, d ) { + + addVertex( a ); + addVertex( b ); + addVertex( d ); + + addVertex( b ); + addVertex( c ); + addVertex( d ); + + + const nextIndex = verticesArray.length / 3; + const uvs = uvgen.generateSideWallUV( scope, verticesArray, nextIndex - 6, nextIndex - 3, nextIndex - 2, nextIndex - 1 ); + + addUV( uvs[ 0 ] ); + addUV( uvs[ 1 ] ); + addUV( uvs[ 3 ] ); + + addUV( uvs[ 1 ] ); + addUV( uvs[ 2 ] ); + addUV( uvs[ 3 ] ); + + } + + function addVertex( index ) { + + verticesArray.push( placeholder[ index * 3 + 0 ] ); + verticesArray.push( placeholder[ index * 3 + 1 ] ); + verticesArray.push( placeholder[ index * 3 + 2 ] ); + + } + + + function addUV( vector2 ) { + + uvArray.push( vector2.x ); + uvArray.push( vector2.y ); + + } + + } + + } + + copy( source ) { + + super.copy( source ); + + this.parameters = Object.assign( {}, source.parameters ); + + return this; + + } + + toJSON() { + + const data = super.toJSON(); + + const shapes = this.parameters.shapes; + const options = this.parameters.options; + + return toJSON$1( shapes, options, data ); + + } + + static fromJSON( data, shapes ) { + + const geometryShapes = []; + + for ( let j = 0, jl = data.shapes.length; j < jl; j ++ ) { + + const shape = shapes[ data.shapes[ j ] ]; + + geometryShapes.push( shape ); + + } + + const extrudePath = data.options.extrudePath; + + if ( extrudePath !== undefined ) { + + data.options.extrudePath = new Curves[ extrudePath.type ]().fromJSON( extrudePath ); + + } + + return new ExtrudeGeometry( geometryShapes, data.options ); + + } + +} + +const WorldUVGenerator = { + + generateTopUV: function ( geometry, vertices, indexA, indexB, indexC ) { + + const a_x = vertices[ indexA * 3 ]; + const a_y = vertices[ indexA * 3 + 1 ]; + const b_x = vertices[ indexB * 3 ]; + const b_y = vertices[ indexB * 3 + 1 ]; + const c_x = vertices[ indexC * 3 ]; + const c_y = vertices[ indexC * 3 + 1 ]; + + return [ + new Vector2( a_x, a_y ), + new Vector2( b_x, b_y ), + new Vector2( c_x, c_y ) + ]; + + }, + + generateSideWallUV: function ( geometry, vertices, indexA, indexB, indexC, indexD ) { + + const a_x = vertices[ indexA * 3 ]; + const a_y = vertices[ indexA * 3 + 1 ]; + const a_z = vertices[ indexA * 3 + 2 ]; + const b_x = vertices[ indexB * 3 ]; + const b_y = vertices[ indexB * 3 + 1 ]; + const b_z = vertices[ indexB * 3 + 2 ]; + const c_x = vertices[ indexC * 3 ]; + const c_y = vertices[ indexC * 3 + 1 ]; + const c_z = vertices[ indexC * 3 + 2 ]; + const d_x = vertices[ indexD * 3 ]; + const d_y = vertices[ indexD * 3 + 1 ]; + const d_z = vertices[ indexD * 3 + 2 ]; + + if ( Math.abs( a_y - b_y ) < Math.abs( a_x - b_x ) ) { + + return [ + new Vector2( a_x, 1 - a_z ), + new Vector2( b_x, 1 - b_z ), + new Vector2( c_x, 1 - c_z ), + new Vector2( d_x, 1 - d_z ) + ]; + + } else { + + return [ + new Vector2( a_y, 1 - a_z ), + new Vector2( b_y, 1 - b_z ), + new Vector2( c_y, 1 - c_z ), + new Vector2( d_y, 1 - d_z ) + ]; + + } + + } + +}; + +function toJSON$1( shapes, options, data ) { + + data.shapes = []; + + if ( Array.isArray( shapes ) ) { + + for ( let i = 0, l = shapes.length; i < l; i ++ ) { + + const shape = shapes[ i ]; + + data.shapes.push( shape.uuid ); + + } + + } else { + + data.shapes.push( shapes.uuid ); + + } + + data.options = Object.assign( {}, options ); + + if ( options.extrudePath !== undefined ) data.options.extrudePath = options.extrudePath.toJSON(); + + return data; + +} + +class IcosahedronGeometry extends PolyhedronGeometry { + + constructor( radius = 1, detail = 0 ) { + + const t = ( 1 + Math.sqrt( 5 ) ) / 2; + + const vertices = [ + - 1, t, 0, 1, t, 0, - 1, - t, 0, 1, - t, 0, + 0, - 1, t, 0, 1, t, 0, - 1, - t, 0, 1, - t, + t, 0, - 1, t, 0, 1, - t, 0, - 1, - t, 0, 1 + ]; + + const indices = [ + 0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 0, 10, 11, + 1, 5, 9, 5, 11, 4, 11, 10, 2, 10, 7, 6, 7, 1, 8, + 3, 9, 4, 3, 4, 2, 3, 2, 6, 3, 6, 8, 3, 8, 9, + 4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7, 9, 8, 1 + ]; + + super( vertices, indices, radius, detail ); + + this.type = 'IcosahedronGeometry'; + + this.parameters = { + radius: radius, + detail: detail + }; + + } + + static fromJSON( data ) { + + return new IcosahedronGeometry( data.radius, data.detail ); + + } + +} + +class OctahedronGeometry extends PolyhedronGeometry { + + constructor( radius = 1, detail = 0 ) { + + const vertices = [ + 1, 0, 0, - 1, 0, 0, 0, 1, 0, + 0, - 1, 0, 0, 0, 1, 0, 0, - 1 + ]; + + const indices = [ + 0, 2, 4, 0, 4, 3, 0, 3, 5, + 0, 5, 2, 1, 2, 5, 1, 5, 3, + 1, 3, 4, 1, 4, 2 + ]; + + super( vertices, indices, radius, detail ); + + this.type = 'OctahedronGeometry'; + + this.parameters = { + radius: radius, + detail: detail + }; + + } + + static fromJSON( data ) { + + return new OctahedronGeometry( data.radius, data.detail ); + + } + +} + +class RingGeometry extends BufferGeometry { + + constructor( innerRadius = 0.5, outerRadius = 1, thetaSegments = 32, phiSegments = 1, thetaStart = 0, thetaLength = Math.PI * 2 ) { + + super(); + + this.type = 'RingGeometry'; + + this.parameters = { + innerRadius: innerRadius, + outerRadius: outerRadius, + thetaSegments: thetaSegments, + phiSegments: phiSegments, + thetaStart: thetaStart, + thetaLength: thetaLength + }; + + thetaSegments = Math.max( 3, thetaSegments ); + phiSegments = Math.max( 1, phiSegments ); + + // buffers + + const indices = []; + const vertices = []; + const normals = []; + const uvs = []; + + // some helper variables + + let radius = innerRadius; + const radiusStep = ( ( outerRadius - innerRadius ) / phiSegments ); + const vertex = new Vector3(); + const uv = new Vector2(); + + // generate vertices, normals and uvs + + for ( let j = 0; j <= phiSegments; j ++ ) { + + for ( let i = 0; i <= thetaSegments; i ++ ) { + + // values are generate from the inside of the ring to the outside + + const segment = thetaStart + i / thetaSegments * thetaLength; + + // vertex + + vertex.x = radius * Math.cos( segment ); + vertex.y = radius * Math.sin( segment ); + + vertices.push( vertex.x, vertex.y, vertex.z ); + + // normal + + normals.push( 0, 0, 1 ); + + // uv + + uv.x = ( vertex.x / outerRadius + 1 ) / 2; + uv.y = ( vertex.y / outerRadius + 1 ) / 2; + + uvs.push( uv.x, uv.y ); + + } + + // increase the radius for next row of vertices + + radius += radiusStep; + + } + + // indices + + for ( let j = 0; j < phiSegments; j ++ ) { + + const thetaSegmentLevel = j * ( thetaSegments + 1 ); + + for ( let i = 0; i < thetaSegments; i ++ ) { + + const segment = i + thetaSegmentLevel; + + const a = segment; + const b = segment + thetaSegments + 1; + const c = segment + thetaSegments + 2; + const d = segment + 1; + + // faces + + indices.push( a, b, d ); + indices.push( b, c, d ); + + } + + } + + // build geometry + + this.setIndex( indices ); + this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); + this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); + this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); + + } + + copy( source ) { + + super.copy( source ); + + this.parameters = Object.assign( {}, source.parameters ); + + return this; + + } + + static fromJSON( data ) { + + return new RingGeometry( data.innerRadius, data.outerRadius, data.thetaSegments, data.phiSegments, data.thetaStart, data.thetaLength ); + + } + +} + +class ShapeGeometry extends BufferGeometry { + + constructor( shapes = new Shape( [ new Vector2( 0, 0.5 ), new Vector2( - 0.5, - 0.5 ), new Vector2( 0.5, - 0.5 ) ] ), curveSegments = 12 ) { + + super(); + + this.type = 'ShapeGeometry'; + + this.parameters = { + shapes: shapes, + curveSegments: curveSegments + }; + + // buffers + + const indices = []; + const vertices = []; + const normals = []; + const uvs = []; + + // helper variables + + let groupStart = 0; + let groupCount = 0; + + // allow single and array values for "shapes" parameter + + if ( Array.isArray( shapes ) === false ) { + + addShape( shapes ); + + } else { + + for ( let i = 0; i < shapes.length; i ++ ) { + + addShape( shapes[ i ] ); + + this.addGroup( groupStart, groupCount, i ); // enables MultiMaterial support + + groupStart += groupCount; + groupCount = 0; + + } + + } + + // build geometry + + this.setIndex( indices ); + this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); + this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); + this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); + + + // helper functions + + function addShape( shape ) { + + const indexOffset = vertices.length / 3; + const points = shape.extractPoints( curveSegments ); + + let shapeVertices = points.shape; + const shapeHoles = points.holes; + + // check direction of vertices + + if ( ShapeUtils.isClockWise( shapeVertices ) === false ) { + + shapeVertices = shapeVertices.reverse(); + + } + + for ( let i = 0, l = shapeHoles.length; i < l; i ++ ) { + + const shapeHole = shapeHoles[ i ]; + + if ( ShapeUtils.isClockWise( shapeHole ) === true ) { + + shapeHoles[ i ] = shapeHole.reverse(); + + } + + } + + const faces = ShapeUtils.triangulateShape( shapeVertices, shapeHoles ); + + // join vertices of inner and outer paths to a single array + + for ( let i = 0, l = shapeHoles.length; i < l; i ++ ) { + + const shapeHole = shapeHoles[ i ]; + shapeVertices = shapeVertices.concat( shapeHole ); + + } + + // vertices, normals, uvs + + for ( let i = 0, l = shapeVertices.length; i < l; i ++ ) { + + const vertex = shapeVertices[ i ]; + + vertices.push( vertex.x, vertex.y, 0 ); + normals.push( 0, 0, 1 ); + uvs.push( vertex.x, vertex.y ); // world uvs + + } + + // indices + + for ( let i = 0, l = faces.length; i < l; i ++ ) { + + const face = faces[ i ]; + + const a = face[ 0 ] + indexOffset; + const b = face[ 1 ] + indexOffset; + const c = face[ 2 ] + indexOffset; + + indices.push( a, b, c ); + groupCount += 3; + + } + + } + + } + + copy( source ) { + + super.copy( source ); + + this.parameters = Object.assign( {}, source.parameters ); + + return this; + + } + + toJSON() { + + const data = super.toJSON(); + + const shapes = this.parameters.shapes; + + return toJSON( shapes, data ); + + } + + static fromJSON( data, shapes ) { + + const geometryShapes = []; + + for ( let j = 0, jl = data.shapes.length; j < jl; j ++ ) { + + const shape = shapes[ data.shapes[ j ] ]; + + geometryShapes.push( shape ); + + } + + return new ShapeGeometry( geometryShapes, data.curveSegments ); + + } + +} + +function toJSON( shapes, data ) { + + data.shapes = []; + + if ( Array.isArray( shapes ) ) { + + for ( let i = 0, l = shapes.length; i < l; i ++ ) { + + const shape = shapes[ i ]; + + data.shapes.push( shape.uuid ); + + } + + } else { + + data.shapes.push( shapes.uuid ); + + } + + return data; + +} + +class SphereGeometry extends BufferGeometry { + + constructor( radius = 1, widthSegments = 32, heightSegments = 16, phiStart = 0, phiLength = Math.PI * 2, thetaStart = 0, thetaLength = Math.PI ) { + + super(); + + this.type = 'SphereGeometry'; + + this.parameters = { + radius: radius, + widthSegments: widthSegments, + heightSegments: heightSegments, + phiStart: phiStart, + phiLength: phiLength, + thetaStart: thetaStart, + thetaLength: thetaLength + }; + + widthSegments = Math.max( 3, Math.floor( widthSegments ) ); + heightSegments = Math.max( 2, Math.floor( heightSegments ) ); + + const thetaEnd = Math.min( thetaStart + thetaLength, Math.PI ); + + let index = 0; + const grid = []; + + const vertex = new Vector3(); + const normal = new Vector3(); + + // buffers + + const indices = []; + const vertices = []; + const normals = []; + const uvs = []; + + // generate vertices, normals and uvs + + for ( let iy = 0; iy <= heightSegments; iy ++ ) { + + const verticesRow = []; + + const v = iy / heightSegments; + + // special case for the poles + + let uOffset = 0; + + if ( iy === 0 && thetaStart === 0 ) { + + uOffset = 0.5 / widthSegments; + + } else if ( iy === heightSegments && thetaEnd === Math.PI ) { + + uOffset = - 0.5 / widthSegments; + + } + + for ( let ix = 0; ix <= widthSegments; ix ++ ) { + + const u = ix / widthSegments; + + // vertex + + vertex.x = - radius * Math.cos( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength ); + vertex.y = radius * Math.cos( thetaStart + v * thetaLength ); + vertex.z = radius * Math.sin( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength ); + + vertices.push( vertex.x, vertex.y, vertex.z ); + + // normal + + normal.copy( vertex ).normalize(); + normals.push( normal.x, normal.y, normal.z ); + + // uv + + uvs.push( u + uOffset, 1 - v ); + + verticesRow.push( index ++ ); + + } + + grid.push( verticesRow ); + + } + + // indices + + for ( let iy = 0; iy < heightSegments; iy ++ ) { + + for ( let ix = 0; ix < widthSegments; ix ++ ) { + + const a = grid[ iy ][ ix + 1 ]; + const b = grid[ iy ][ ix ]; + const c = grid[ iy + 1 ][ ix ]; + const d = grid[ iy + 1 ][ ix + 1 ]; + + if ( iy !== 0 || thetaStart > 0 ) indices.push( a, b, d ); + if ( iy !== heightSegments - 1 || thetaEnd < Math.PI ) indices.push( b, c, d ); + + } + + } + + // build geometry + + this.setIndex( indices ); + this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); + this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); + this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); + + } + + copy( source ) { + + super.copy( source ); + + this.parameters = Object.assign( {}, source.parameters ); + + return this; + + } + + static fromJSON( data ) { + + return new SphereGeometry( data.radius, data.widthSegments, data.heightSegments, data.phiStart, data.phiLength, data.thetaStart, data.thetaLength ); + + } + +} + +class TetrahedronGeometry extends PolyhedronGeometry { + + constructor( radius = 1, detail = 0 ) { + + const vertices = [ + 1, 1, 1, - 1, - 1, 1, - 1, 1, - 1, 1, - 1, - 1 + ]; + + const indices = [ + 2, 1, 0, 0, 3, 2, 1, 3, 0, 2, 3, 1 + ]; + + super( vertices, indices, radius, detail ); + + this.type = 'TetrahedronGeometry'; + + this.parameters = { + radius: radius, + detail: detail + }; + + } + + static fromJSON( data ) { + + return new TetrahedronGeometry( data.radius, data.detail ); + + } + +} + +class TorusGeometry extends BufferGeometry { + + constructor( radius = 1, tube = 0.4, radialSegments = 12, tubularSegments = 48, arc = Math.PI * 2 ) { + + super(); + + this.type = 'TorusGeometry'; + + this.parameters = { + radius: radius, + tube: tube, + radialSegments: radialSegments, + tubularSegments: tubularSegments, + arc: arc + }; + + radialSegments = Math.floor( radialSegments ); + tubularSegments = Math.floor( tubularSegments ); + + // buffers + + const indices = []; + const vertices = []; + const normals = []; + const uvs = []; + + // helper variables + + const center = new Vector3(); + const vertex = new Vector3(); + const normal = new Vector3(); + + // generate vertices, normals and uvs + + for ( let j = 0; j <= radialSegments; j ++ ) { + + for ( let i = 0; i <= tubularSegments; i ++ ) { + + const u = i / tubularSegments * arc; + const v = j / radialSegments * Math.PI * 2; + + // vertex + + vertex.x = ( radius + tube * Math.cos( v ) ) * Math.cos( u ); + vertex.y = ( radius + tube * Math.cos( v ) ) * Math.sin( u ); + vertex.z = tube * Math.sin( v ); + + vertices.push( vertex.x, vertex.y, vertex.z ); + + // normal + + center.x = radius * Math.cos( u ); + center.y = radius * Math.sin( u ); + normal.subVectors( vertex, center ).normalize(); + + normals.push( normal.x, normal.y, normal.z ); + + // uv + + uvs.push( i / tubularSegments ); + uvs.push( j / radialSegments ); + + } + + } + + // generate indices + + for ( let j = 1; j <= radialSegments; j ++ ) { + + for ( let i = 1; i <= tubularSegments; i ++ ) { + + // indices + + const a = ( tubularSegments + 1 ) * j + i - 1; + const b = ( tubularSegments + 1 ) * ( j - 1 ) + i - 1; + const c = ( tubularSegments + 1 ) * ( j - 1 ) + i; + const d = ( tubularSegments + 1 ) * j + i; + + // faces + + indices.push( a, b, d ); + indices.push( b, c, d ); + + } + + } + + // build geometry + + this.setIndex( indices ); + this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); + this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); + this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); + + } + + copy( source ) { + + super.copy( source ); + + this.parameters = Object.assign( {}, source.parameters ); + + return this; + + } + + static fromJSON( data ) { + + return new TorusGeometry( data.radius, data.tube, data.radialSegments, data.tubularSegments, data.arc ); + + } + +} + +class TorusKnotGeometry extends BufferGeometry { + + constructor( radius = 1, tube = 0.4, tubularSegments = 64, radialSegments = 8, p = 2, q = 3 ) { + + super(); + + this.type = 'TorusKnotGeometry'; + + this.parameters = { + radius: radius, + tube: tube, + tubularSegments: tubularSegments, + radialSegments: radialSegments, + p: p, + q: q + }; + + tubularSegments = Math.floor( tubularSegments ); + radialSegments = Math.floor( radialSegments ); + + // buffers + + const indices = []; + const vertices = []; + const normals = []; + const uvs = []; + + // helper variables + + const vertex = new Vector3(); + const normal = new Vector3(); + + const P1 = new Vector3(); + const P2 = new Vector3(); + + const B = new Vector3(); + const T = new Vector3(); + const N = new Vector3(); + + // generate vertices, normals and uvs + + for ( let i = 0; i <= tubularSegments; ++ i ) { + + // the radian "u" is used to calculate the position on the torus curve of the current tubular segment + + const u = i / tubularSegments * p * Math.PI * 2; + + // now we calculate two points. P1 is our current position on the curve, P2 is a little farther ahead. + // these points are used to create a special "coordinate space", which is necessary to calculate the correct vertex positions + + calculatePositionOnCurve( u, p, q, radius, P1 ); + calculatePositionOnCurve( u + 0.01, p, q, radius, P2 ); + + // calculate orthonormal basis + + T.subVectors( P2, P1 ); + N.addVectors( P2, P1 ); + B.crossVectors( T, N ); + N.crossVectors( B, T ); + + // normalize B, N. T can be ignored, we don't use it + + B.normalize(); + N.normalize(); + + for ( let j = 0; j <= radialSegments; ++ j ) { + + // now calculate the vertices. they are nothing more than an extrusion of the torus curve. + // because we extrude a shape in the xy-plane, there is no need to calculate a z-value. + + const v = j / radialSegments * Math.PI * 2; + const cx = - tube * Math.cos( v ); + const cy = tube * Math.sin( v ); + + // now calculate the final vertex position. + // first we orient the extrusion with our basis vectors, then we add it to the current position on the curve + + vertex.x = P1.x + ( cx * N.x + cy * B.x ); + vertex.y = P1.y + ( cx * N.y + cy * B.y ); + vertex.z = P1.z + ( cx * N.z + cy * B.z ); + + vertices.push( vertex.x, vertex.y, vertex.z ); + + // normal (P1 is always the center/origin of the extrusion, thus we can use it to calculate the normal) + + normal.subVectors( vertex, P1 ).normalize(); + + normals.push( normal.x, normal.y, normal.z ); + + // uv + + uvs.push( i / tubularSegments ); + uvs.push( j / radialSegments ); + + } + + } + + // generate indices + + for ( let j = 1; j <= tubularSegments; j ++ ) { + + for ( let i = 1; i <= radialSegments; i ++ ) { + + // indices + + const a = ( radialSegments + 1 ) * ( j - 1 ) + ( i - 1 ); + const b = ( radialSegments + 1 ) * j + ( i - 1 ); + const c = ( radialSegments + 1 ) * j + i; + const d = ( radialSegments + 1 ) * ( j - 1 ) + i; + + // faces + + indices.push( a, b, d ); + indices.push( b, c, d ); + + } + + } + + // build geometry + + this.setIndex( indices ); + this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); + this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); + this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); + + // this function calculates the current position on the torus curve + + function calculatePositionOnCurve( u, p, q, radius, position ) { + + const cu = Math.cos( u ); + const su = Math.sin( u ); + const quOverP = q / p * u; + const cs = Math.cos( quOverP ); + + position.x = radius * ( 2 + cs ) * 0.5 * cu; + position.y = radius * ( 2 + cs ) * su * 0.5; + position.z = radius * Math.sin( quOverP ) * 0.5; + + } + + } + + copy( source ) { + + super.copy( source ); + + this.parameters = Object.assign( {}, source.parameters ); + + return this; + + } + + static fromJSON( data ) { + + return new TorusKnotGeometry( data.radius, data.tube, data.tubularSegments, data.radialSegments, data.p, data.q ); + + } + +} + +class TubeGeometry extends BufferGeometry { + + constructor( path = new QuadraticBezierCurve3( new Vector3( - 1, - 1, 0 ), new Vector3( - 1, 1, 0 ), new Vector3( 1, 1, 0 ) ), tubularSegments = 64, radius = 1, radialSegments = 8, closed = false ) { + + super(); + + this.type = 'TubeGeometry'; + + this.parameters = { + path: path, + tubularSegments: tubularSegments, + radius: radius, + radialSegments: radialSegments, + closed: closed + }; + + const frames = path.computeFrenetFrames( tubularSegments, closed ); + + // expose internals + + this.tangents = frames.tangents; + this.normals = frames.normals; + this.binormals = frames.binormals; + + // helper variables + + const vertex = new Vector3(); + const normal = new Vector3(); + const uv = new Vector2(); + let P = new Vector3(); + + // buffer + + const vertices = []; + const normals = []; + const uvs = []; + const indices = []; + + // create buffer data + + generateBufferData(); + + // build geometry + + this.setIndex( indices ); + this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); + this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); + this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); + + // functions + + function generateBufferData() { + + for ( let i = 0; i < tubularSegments; i ++ ) { + + generateSegment( i ); + + } + + // if the geometry is not closed, generate the last row of vertices and normals + // at the regular position on the given path + // + // if the geometry is closed, duplicate the first row of vertices and normals (uvs will differ) + + generateSegment( ( closed === false ) ? tubularSegments : 0 ); + + // uvs are generated in a separate function. + // this makes it easy compute correct values for closed geometries + + generateUVs(); + + // finally create faces + + generateIndices(); + + } + + function generateSegment( i ) { + + // we use getPointAt to sample evenly distributed points from the given path + + P = path.getPointAt( i / tubularSegments, P ); + + // retrieve corresponding normal and binormal + + const N = frames.normals[ i ]; + const B = frames.binormals[ i ]; + + // generate normals and vertices for the current segment + + for ( let j = 0; j <= radialSegments; j ++ ) { + + const v = j / radialSegments * Math.PI * 2; + + const sin = Math.sin( v ); + const cos = - Math.cos( v ); + + // normal + + normal.x = ( cos * N.x + sin * B.x ); + normal.y = ( cos * N.y + sin * B.y ); + normal.z = ( cos * N.z + sin * B.z ); + normal.normalize(); + + normals.push( normal.x, normal.y, normal.z ); + + // vertex + + vertex.x = P.x + radius * normal.x; + vertex.y = P.y + radius * normal.y; + vertex.z = P.z + radius * normal.z; + + vertices.push( vertex.x, vertex.y, vertex.z ); + + } + + } + + function generateIndices() { + + for ( let j = 1; j <= tubularSegments; j ++ ) { + + for ( let i = 1; i <= radialSegments; i ++ ) { + + const a = ( radialSegments + 1 ) * ( j - 1 ) + ( i - 1 ); + const b = ( radialSegments + 1 ) * j + ( i - 1 ); + const c = ( radialSegments + 1 ) * j + i; + const d = ( radialSegments + 1 ) * ( j - 1 ) + i; + + // faces + + indices.push( a, b, d ); + indices.push( b, c, d ); + + } + + } + + } + + function generateUVs() { + + for ( let i = 0; i <= tubularSegments; i ++ ) { + + for ( let j = 0; j <= radialSegments; j ++ ) { + + uv.x = i / tubularSegments; + uv.y = j / radialSegments; + + uvs.push( uv.x, uv.y ); + + } + + } + + } + + } + + copy( source ) { + + super.copy( source ); + + this.parameters = Object.assign( {}, source.parameters ); + + return this; + + } + + toJSON() { + + const data = super.toJSON(); + + data.path = this.parameters.path.toJSON(); + + return data; + + } + + static fromJSON( data ) { + + // This only works for built-in curves (e.g. CatmullRomCurve3). + // User defined curves or instances of CurvePath will not be deserialized. + return new TubeGeometry( + new Curves[ data.path.type ]().fromJSON( data.path ), + data.tubularSegments, + data.radius, + data.radialSegments, + data.closed + ); + + } + +} + +class WireframeGeometry extends BufferGeometry { + + constructor( geometry = null ) { + + super(); + + this.type = 'WireframeGeometry'; + + this.parameters = { + geometry: geometry + }; + + if ( geometry !== null ) { + + // buffer + + const vertices = []; + const edges = new Set(); + + // helper variables + + const start = new Vector3(); + const end = new Vector3(); + + if ( geometry.index !== null ) { + + // indexed BufferGeometry + + const position = geometry.attributes.position; + const indices = geometry.index; + let groups = geometry.groups; + + if ( groups.length === 0 ) { + + groups = [ { start: 0, count: indices.count, materialIndex: 0 } ]; + + } + + // create a data structure that contains all edges without duplicates + + for ( let o = 0, ol = groups.length; o < ol; ++ o ) { + + const group = groups[ o ]; + + const groupStart = group.start; + const groupCount = group.count; + + for ( let i = groupStart, l = ( groupStart + groupCount ); i < l; i += 3 ) { + + for ( let j = 0; j < 3; j ++ ) { + + const index1 = indices.getX( i + j ); + const index2 = indices.getX( i + ( j + 1 ) % 3 ); + + start.fromBufferAttribute( position, index1 ); + end.fromBufferAttribute( position, index2 ); + + if ( isUniqueEdge( start, end, edges ) === true ) { + + vertices.push( start.x, start.y, start.z ); + vertices.push( end.x, end.y, end.z ); + + } + + } + + } + + } + + } else { + + // non-indexed BufferGeometry + + const position = geometry.attributes.position; + + for ( let i = 0, l = ( position.count / 3 ); i < l; i ++ ) { + + for ( let j = 0; j < 3; j ++ ) { + + // three edges per triangle, an edge is represented as (index1, index2) + // e.g. the first triangle has the following edges: (0,1),(1,2),(2,0) + + const index1 = 3 * i + j; + const index2 = 3 * i + ( ( j + 1 ) % 3 ); + + start.fromBufferAttribute( position, index1 ); + end.fromBufferAttribute( position, index2 ); + + if ( isUniqueEdge( start, end, edges ) === true ) { + + vertices.push( start.x, start.y, start.z ); + vertices.push( end.x, end.y, end.z ); + + } + + } + + } + + } + + // build geometry + + this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); + + } + + } + + copy( source ) { + + super.copy( source ); + + this.parameters = Object.assign( {}, source.parameters ); + + return this; + + } + +} + +function isUniqueEdge( start, end, edges ) { + + const hash1 = `${start.x},${start.y},${start.z}-${end.x},${end.y},${end.z}`; + const hash2 = `${end.x},${end.y},${end.z}-${start.x},${start.y},${start.z}`; // coincident edge + + if ( edges.has( hash1 ) === true || edges.has( hash2 ) === true ) { + + return false; + + } else { + + edges.add( hash1 ); + edges.add( hash2 ); + return true; + + } + +} + +var Geometries = /*#__PURE__*/Object.freeze({ + __proto__: null, + BoxGeometry: BoxGeometry, + CapsuleGeometry: CapsuleGeometry, + CircleGeometry: CircleGeometry, + ConeGeometry: ConeGeometry, + CylinderGeometry: CylinderGeometry, + DodecahedronGeometry: DodecahedronGeometry, + EdgesGeometry: EdgesGeometry, + ExtrudeGeometry: ExtrudeGeometry, + IcosahedronGeometry: IcosahedronGeometry, + LatheGeometry: LatheGeometry, + OctahedronGeometry: OctahedronGeometry, + PlaneGeometry: PlaneGeometry, + PolyhedronGeometry: PolyhedronGeometry, + RingGeometry: RingGeometry, + ShapeGeometry: ShapeGeometry, + SphereGeometry: SphereGeometry, + TetrahedronGeometry: TetrahedronGeometry, + TorusGeometry: TorusGeometry, + TorusKnotGeometry: TorusKnotGeometry, + TubeGeometry: TubeGeometry, + WireframeGeometry: WireframeGeometry +}); + +class ShadowMaterial extends Material { + + constructor( parameters ) { + + super(); + + this.isShadowMaterial = true; + + this.type = 'ShadowMaterial'; + + this.color = new Color( 0x000000 ); + this.transparent = true; + + this.fog = true; + + this.setValues( parameters ); + + } + + copy( source ) { + + super.copy( source ); + + this.color.copy( source.color ); + + this.fog = source.fog; + + return this; + + } + +} + +class RawShaderMaterial extends ShaderMaterial { + + constructor( parameters ) { + + super( parameters ); + + this.isRawShaderMaterial = true; + + this.type = 'RawShaderMaterial'; + + } + +} + +class MeshStandardMaterial extends Material { + + constructor( parameters ) { + + super(); + + this.isMeshStandardMaterial = true; + + this.defines = { 'STANDARD': '' }; + + this.type = 'MeshStandardMaterial'; + + this.color = new Color( 0xffffff ); // diffuse + this.roughness = 1.0; + this.metalness = 0.0; + + this.map = null; + + this.lightMap = null; + this.lightMapIntensity = 1.0; + + this.aoMap = null; + this.aoMapIntensity = 1.0; + + this.emissive = new Color( 0x000000 ); + this.emissiveIntensity = 1.0; + this.emissiveMap = null; + + this.bumpMap = null; + this.bumpScale = 1; + + this.normalMap = null; + this.normalMapType = TangentSpaceNormalMap; + this.normalScale = new Vector2( 1, 1 ); + + this.displacementMap = null; + this.displacementScale = 1; + this.displacementBias = 0; + + this.roughnessMap = null; + + this.metalnessMap = null; + + this.alphaMap = null; + + this.envMap = null; + this.envMapRotation = new Euler(); + this.envMapIntensity = 1.0; + + this.wireframe = false; + this.wireframeLinewidth = 1; + this.wireframeLinecap = 'round'; + this.wireframeLinejoin = 'round'; + + this.flatShading = false; + + this.fog = true; + + this.setValues( parameters ); + + } + + copy( source ) { + + super.copy( source ); + + this.defines = { 'STANDARD': '' }; + + this.color.copy( source.color ); + this.roughness = source.roughness; + this.metalness = source.metalness; + + this.map = source.map; + + this.lightMap = source.lightMap; + this.lightMapIntensity = source.lightMapIntensity; + + this.aoMap = source.aoMap; + this.aoMapIntensity = source.aoMapIntensity; + + this.emissive.copy( source.emissive ); + this.emissiveMap = source.emissiveMap; + this.emissiveIntensity = source.emissiveIntensity; + + this.bumpMap = source.bumpMap; + this.bumpScale = source.bumpScale; + + this.normalMap = source.normalMap; + this.normalMapType = source.normalMapType; + this.normalScale.copy( source.normalScale ); + + this.displacementMap = source.displacementMap; + this.displacementScale = source.displacementScale; + this.displacementBias = source.displacementBias; + + this.roughnessMap = source.roughnessMap; + + this.metalnessMap = source.metalnessMap; + + this.alphaMap = source.alphaMap; + + this.envMap = source.envMap; + this.envMapRotation.copy( source.envMapRotation ); + this.envMapIntensity = source.envMapIntensity; + + this.wireframe = source.wireframe; + this.wireframeLinewidth = source.wireframeLinewidth; + this.wireframeLinecap = source.wireframeLinecap; + this.wireframeLinejoin = source.wireframeLinejoin; + + this.flatShading = source.flatShading; + + this.fog = source.fog; + + return this; + + } + +} + +class MeshPhysicalMaterial extends MeshStandardMaterial { + + constructor( parameters ) { + + super(); + + this.isMeshPhysicalMaterial = true; + + this.defines = { + + 'STANDARD': '', + 'PHYSICAL': '' + + }; + + this.type = 'MeshPhysicalMaterial'; + + this.anisotropyRotation = 0; + this.anisotropyMap = null; + + this.clearcoatMap = null; + this.clearcoatRoughness = 0.0; + this.clearcoatRoughnessMap = null; + this.clearcoatNormalScale = new Vector2( 1, 1 ); + this.clearcoatNormalMap = null; + + this.ior = 1.5; + + Object.defineProperty( this, 'reflectivity', { + get: function () { + + return ( clamp( 2.5 * ( this.ior - 1 ) / ( this.ior + 1 ), 0, 1 ) ); + + }, + set: function ( reflectivity ) { + + this.ior = ( 1 + 0.4 * reflectivity ) / ( 1 - 0.4 * reflectivity ); + + } + } ); + + this.iridescenceMap = null; + this.iridescenceIOR = 1.3; + this.iridescenceThicknessRange = [ 100, 400 ]; + this.iridescenceThicknessMap = null; + + this.sheenColor = new Color( 0x000000 ); + this.sheenColorMap = null; + this.sheenRoughness = 1.0; + this.sheenRoughnessMap = null; + + this.transmissionMap = null; + + this.thickness = 0; + this.thicknessMap = null; + this.attenuationDistance = Infinity; + this.attenuationColor = new Color( 1, 1, 1 ); + + this.specularIntensity = 1.0; + this.specularIntensityMap = null; + this.specularColor = new Color( 1, 1, 1 ); + this.specularColorMap = null; + + this._anisotropy = 0; + this._clearcoat = 0; + this._dispersion = 0; + this._iridescence = 0; + this._sheen = 0.0; + this._transmission = 0; + + this.setValues( parameters ); + + } + + get anisotropy() { + + return this._anisotropy; + + } + + set anisotropy( value ) { + + if ( this._anisotropy > 0 !== value > 0 ) { + + this.version ++; + + } + + this._anisotropy = value; + + } + + get clearcoat() { + + return this._clearcoat; + + } + + set clearcoat( value ) { + + if ( this._clearcoat > 0 !== value > 0 ) { + + this.version ++; + + } + + this._clearcoat = value; + + } + + get iridescence() { + + return this._iridescence; + + } + + set iridescence( value ) { + + if ( this._iridescence > 0 !== value > 0 ) { + + this.version ++; + + } + + this._iridescence = value; + + } + + get dispersion() { + + return this._dispersion; + + } + + set dispersion( value ) { + + if ( this._dispersion > 0 !== value > 0 ) { + + this.version ++; + + } + + this._dispersion = value; + + } + + get sheen() { + + return this._sheen; + + } + + set sheen( value ) { + + if ( this._sheen > 0 !== value > 0 ) { + + this.version ++; + + } + + this._sheen = value; + + } + + get transmission() { + + return this._transmission; + + } + + set transmission( value ) { + + if ( this._transmission > 0 !== value > 0 ) { + + this.version ++; + + } + + this._transmission = value; + + } + + copy( source ) { + + super.copy( source ); + + this.defines = { + + 'STANDARD': '', + 'PHYSICAL': '' + + }; + + this.anisotropy = source.anisotropy; + this.anisotropyRotation = source.anisotropyRotation; + this.anisotropyMap = source.anisotropyMap; + + this.clearcoat = source.clearcoat; + this.clearcoatMap = source.clearcoatMap; + this.clearcoatRoughness = source.clearcoatRoughness; + this.clearcoatRoughnessMap = source.clearcoatRoughnessMap; + this.clearcoatNormalMap = source.clearcoatNormalMap; + this.clearcoatNormalScale.copy( source.clearcoatNormalScale ); + + this.dispersion = source.dispersion; + this.ior = source.ior; + + this.iridescence = source.iridescence; + this.iridescenceMap = source.iridescenceMap; + this.iridescenceIOR = source.iridescenceIOR; + this.iridescenceThicknessRange = [ ...source.iridescenceThicknessRange ]; + this.iridescenceThicknessMap = source.iridescenceThicknessMap; + + this.sheen = source.sheen; + this.sheenColor.copy( source.sheenColor ); + this.sheenColorMap = source.sheenColorMap; + this.sheenRoughness = source.sheenRoughness; + this.sheenRoughnessMap = source.sheenRoughnessMap; + + this.transmission = source.transmission; + this.transmissionMap = source.transmissionMap; + + this.thickness = source.thickness; + this.thicknessMap = source.thicknessMap; + this.attenuationDistance = source.attenuationDistance; + this.attenuationColor.copy( source.attenuationColor ); + + this.specularIntensity = source.specularIntensity; + this.specularIntensityMap = source.specularIntensityMap; + this.specularColor.copy( source.specularColor ); + this.specularColorMap = source.specularColorMap; + + return this; + + } + +} + +class MeshPhongMaterial extends Material { + + constructor( parameters ) { + + super(); + + this.isMeshPhongMaterial = true; + + this.type = 'MeshPhongMaterial'; + + this.color = new Color( 0xffffff ); // diffuse + this.specular = new Color( 0x111111 ); + this.shininess = 30; + + this.map = null; + + this.lightMap = null; + this.lightMapIntensity = 1.0; + + this.aoMap = null; + this.aoMapIntensity = 1.0; + + this.emissive = new Color( 0x000000 ); + this.emissiveIntensity = 1.0; + this.emissiveMap = null; + + this.bumpMap = null; + this.bumpScale = 1; + + this.normalMap = null; + this.normalMapType = TangentSpaceNormalMap; + this.normalScale = new Vector2( 1, 1 ); + + this.displacementMap = null; + this.displacementScale = 1; + this.displacementBias = 0; + + this.specularMap = null; + + this.alphaMap = null; + + this.envMap = null; + this.envMapRotation = new Euler(); + this.combine = MultiplyOperation; + this.reflectivity = 1; + this.refractionRatio = 0.98; + + this.wireframe = false; + this.wireframeLinewidth = 1; + this.wireframeLinecap = 'round'; + this.wireframeLinejoin = 'round'; + + this.flatShading = false; + + this.fog = true; + + this.setValues( parameters ); + + } + + copy( source ) { + + super.copy( source ); + + this.color.copy( source.color ); + this.specular.copy( source.specular ); + this.shininess = source.shininess; + + this.map = source.map; + + this.lightMap = source.lightMap; + this.lightMapIntensity = source.lightMapIntensity; + + this.aoMap = source.aoMap; + this.aoMapIntensity = source.aoMapIntensity; + + this.emissive.copy( source.emissive ); + this.emissiveMap = source.emissiveMap; + this.emissiveIntensity = source.emissiveIntensity; + + this.bumpMap = source.bumpMap; + this.bumpScale = source.bumpScale; + + this.normalMap = source.normalMap; + this.normalMapType = source.normalMapType; + this.normalScale.copy( source.normalScale ); + + this.displacementMap = source.displacementMap; + this.displacementScale = source.displacementScale; + this.displacementBias = source.displacementBias; + + this.specularMap = source.specularMap; + + this.alphaMap = source.alphaMap; + + this.envMap = source.envMap; + this.envMapRotation.copy( source.envMapRotation ); + this.combine = source.combine; + this.reflectivity = source.reflectivity; + this.refractionRatio = source.refractionRatio; + + this.wireframe = source.wireframe; + this.wireframeLinewidth = source.wireframeLinewidth; + this.wireframeLinecap = source.wireframeLinecap; + this.wireframeLinejoin = source.wireframeLinejoin; + + this.flatShading = source.flatShading; + + this.fog = source.fog; + + return this; + + } + +} + +class MeshToonMaterial extends Material { + + constructor( parameters ) { + + super(); + + this.isMeshToonMaterial = true; + + this.defines = { 'TOON': '' }; + + this.type = 'MeshToonMaterial'; + + this.color = new Color( 0xffffff ); + + this.map = null; + this.gradientMap = null; + + this.lightMap = null; + this.lightMapIntensity = 1.0; + + this.aoMap = null; + this.aoMapIntensity = 1.0; + + this.emissive = new Color( 0x000000 ); + this.emissiveIntensity = 1.0; + this.emissiveMap = null; + + this.bumpMap = null; + this.bumpScale = 1; + + this.normalMap = null; + this.normalMapType = TangentSpaceNormalMap; + this.normalScale = new Vector2( 1, 1 ); + + this.displacementMap = null; + this.displacementScale = 1; + this.displacementBias = 0; + + this.alphaMap = null; + + this.wireframe = false; + this.wireframeLinewidth = 1; + this.wireframeLinecap = 'round'; + this.wireframeLinejoin = 'round'; + + this.fog = true; + + this.setValues( parameters ); + + } + + copy( source ) { + + super.copy( source ); + + this.color.copy( source.color ); + + this.map = source.map; + this.gradientMap = source.gradientMap; + + this.lightMap = source.lightMap; + this.lightMapIntensity = source.lightMapIntensity; + + this.aoMap = source.aoMap; + this.aoMapIntensity = source.aoMapIntensity; + + this.emissive.copy( source.emissive ); + this.emissiveMap = source.emissiveMap; + this.emissiveIntensity = source.emissiveIntensity; + + this.bumpMap = source.bumpMap; + this.bumpScale = source.bumpScale; + + this.normalMap = source.normalMap; + this.normalMapType = source.normalMapType; + this.normalScale.copy( source.normalScale ); + + this.displacementMap = source.displacementMap; + this.displacementScale = source.displacementScale; + this.displacementBias = source.displacementBias; + + this.alphaMap = source.alphaMap; + + this.wireframe = source.wireframe; + this.wireframeLinewidth = source.wireframeLinewidth; + this.wireframeLinecap = source.wireframeLinecap; + this.wireframeLinejoin = source.wireframeLinejoin; + + this.fog = source.fog; + + return this; + + } + +} + +class MeshNormalMaterial extends Material { + + constructor( parameters ) { + + super(); + + this.isMeshNormalMaterial = true; + + this.type = 'MeshNormalMaterial'; + + this.bumpMap = null; + this.bumpScale = 1; + + this.normalMap = null; + this.normalMapType = TangentSpaceNormalMap; + this.normalScale = new Vector2( 1, 1 ); + + this.displacementMap = null; + this.displacementScale = 1; + this.displacementBias = 0; + + this.wireframe = false; + this.wireframeLinewidth = 1; + + this.flatShading = false; + + this.setValues( parameters ); + + } + + copy( source ) { + + super.copy( source ); + + this.bumpMap = source.bumpMap; + this.bumpScale = source.bumpScale; + + this.normalMap = source.normalMap; + this.normalMapType = source.normalMapType; + this.normalScale.copy( source.normalScale ); + + this.displacementMap = source.displacementMap; + this.displacementScale = source.displacementScale; + this.displacementBias = source.displacementBias; + + this.wireframe = source.wireframe; + this.wireframeLinewidth = source.wireframeLinewidth; + + this.flatShading = source.flatShading; + + return this; + + } + +} + +class MeshLambertMaterial extends Material { + + constructor( parameters ) { + + super(); + + this.isMeshLambertMaterial = true; + + this.type = 'MeshLambertMaterial'; + + this.color = new Color( 0xffffff ); // diffuse + + this.map = null; + + this.lightMap = null; + this.lightMapIntensity = 1.0; + + this.aoMap = null; + this.aoMapIntensity = 1.0; + + this.emissive = new Color( 0x000000 ); + this.emissiveIntensity = 1.0; + this.emissiveMap = null; + + this.bumpMap = null; + this.bumpScale = 1; + + this.normalMap = null; + this.normalMapType = TangentSpaceNormalMap; + this.normalScale = new Vector2( 1, 1 ); + + this.displacementMap = null; + this.displacementScale = 1; + this.displacementBias = 0; + + this.specularMap = null; + + this.alphaMap = null; + + this.envMap = null; + this.envMapRotation = new Euler(); + this.combine = MultiplyOperation; + this.reflectivity = 1; + this.refractionRatio = 0.98; + + this.wireframe = false; + this.wireframeLinewidth = 1; + this.wireframeLinecap = 'round'; + this.wireframeLinejoin = 'round'; + + this.flatShading = false; + + this.fog = true; + + this.setValues( parameters ); + + } + + copy( source ) { + + super.copy( source ); + + this.color.copy( source.color ); + + this.map = source.map; + + this.lightMap = source.lightMap; + this.lightMapIntensity = source.lightMapIntensity; + + this.aoMap = source.aoMap; + this.aoMapIntensity = source.aoMapIntensity; + + this.emissive.copy( source.emissive ); + this.emissiveMap = source.emissiveMap; + this.emissiveIntensity = source.emissiveIntensity; + + this.bumpMap = source.bumpMap; + this.bumpScale = source.bumpScale; + + this.normalMap = source.normalMap; + this.normalMapType = source.normalMapType; + this.normalScale.copy( source.normalScale ); + + this.displacementMap = source.displacementMap; + this.displacementScale = source.displacementScale; + this.displacementBias = source.displacementBias; + + this.specularMap = source.specularMap; + + this.alphaMap = source.alphaMap; + + this.envMap = source.envMap; + this.envMapRotation.copy( source.envMapRotation ); + this.combine = source.combine; + this.reflectivity = source.reflectivity; + this.refractionRatio = source.refractionRatio; + + this.wireframe = source.wireframe; + this.wireframeLinewidth = source.wireframeLinewidth; + this.wireframeLinecap = source.wireframeLinecap; + this.wireframeLinejoin = source.wireframeLinejoin; + + this.flatShading = source.flatShading; + + this.fog = source.fog; + + return this; + + } + +} + +class MeshMatcapMaterial extends Material { + + constructor( parameters ) { + + super(); + + this.isMeshMatcapMaterial = true; + + this.defines = { 'MATCAP': '' }; + + this.type = 'MeshMatcapMaterial'; + + this.color = new Color( 0xffffff ); // diffuse + + this.matcap = null; + + this.map = null; + + this.bumpMap = null; + this.bumpScale = 1; + + this.normalMap = null; + this.normalMapType = TangentSpaceNormalMap; + this.normalScale = new Vector2( 1, 1 ); + + this.displacementMap = null; + this.displacementScale = 1; + this.displacementBias = 0; + + this.alphaMap = null; + + this.flatShading = false; + + this.fog = true; + + this.setValues( parameters ); + + } + + + copy( source ) { + + super.copy( source ); + + this.defines = { 'MATCAP': '' }; + + this.color.copy( source.color ); + + this.matcap = source.matcap; + + this.map = source.map; + + this.bumpMap = source.bumpMap; + this.bumpScale = source.bumpScale; + + this.normalMap = source.normalMap; + this.normalMapType = source.normalMapType; + this.normalScale.copy( source.normalScale ); + + this.displacementMap = source.displacementMap; + this.displacementScale = source.displacementScale; + this.displacementBias = source.displacementBias; + + this.alphaMap = source.alphaMap; + + this.flatShading = source.flatShading; + + this.fog = source.fog; + + return this; + + } + +} + +class LineDashedMaterial extends LineBasicMaterial { + + constructor( parameters ) { + + super(); + + this.isLineDashedMaterial = true; + + this.type = 'LineDashedMaterial'; + + this.scale = 1; + this.dashSize = 3; + this.gapSize = 1; + + this.setValues( parameters ); + + } + + copy( source ) { + + super.copy( source ); + + this.scale = source.scale; + this.dashSize = source.dashSize; + this.gapSize = source.gapSize; + + return this; + + } + +} + +// converts an array to a specific type +function convertArray( array, type, forceClone ) { + + if ( ! array || // let 'undefined' and 'null' pass + ! forceClone && array.constructor === type ) return array; + + if ( typeof type.BYTES_PER_ELEMENT === 'number' ) { + + return new type( array ); // create typed array + + } + + return Array.prototype.slice.call( array ); // create Array + +} + +function isTypedArray( object ) { + + return ArrayBuffer.isView( object ) && + ! ( object instanceof DataView ); + +} + +// returns an array by which times and values can be sorted +function getKeyframeOrder( times ) { + + function compareTime( i, j ) { + + return times[ i ] - times[ j ]; + + } + + const n = times.length; + const result = new Array( n ); + for ( let i = 0; i !== n; ++ i ) result[ i ] = i; + + result.sort( compareTime ); + + return result; + +} + +// uses the array previously returned by 'getKeyframeOrder' to sort data +function sortedArray( values, stride, order ) { + + const nValues = values.length; + const result = new values.constructor( nValues ); + + for ( let i = 0, dstOffset = 0; dstOffset !== nValues; ++ i ) { + + const srcOffset = order[ i ] * stride; + + for ( let j = 0; j !== stride; ++ j ) { + + result[ dstOffset ++ ] = values[ srcOffset + j ]; + + } + + } + + return result; + +} + +// function for parsing AOS keyframe formats +function flattenJSON( jsonKeys, times, values, valuePropertyName ) { + + let i = 1, key = jsonKeys[ 0 ]; + + while ( key !== undefined && key[ valuePropertyName ] === undefined ) { + + key = jsonKeys[ i ++ ]; + + } + + if ( key === undefined ) return; // no data + + let value = key[ valuePropertyName ]; + if ( value === undefined ) return; // no data + + if ( Array.isArray( value ) ) { + + do { + + value = key[ valuePropertyName ]; + + if ( value !== undefined ) { + + times.push( key.time ); + values.push.apply( values, value ); // push all elements + + } + + key = jsonKeys[ i ++ ]; + + } while ( key !== undefined ); + + } else if ( value.toArray !== undefined ) { + + // ...assume THREE.Math-ish + + do { + + value = key[ valuePropertyName ]; + + if ( value !== undefined ) { + + times.push( key.time ); + value.toArray( values, values.length ); + + } + + key = jsonKeys[ i ++ ]; + + } while ( key !== undefined ); + + } else { + + // otherwise push as-is + + do { + + value = key[ valuePropertyName ]; + + if ( value !== undefined ) { + + times.push( key.time ); + values.push( value ); + + } + + key = jsonKeys[ i ++ ]; + + } while ( key !== undefined ); + + } + +} + +function subclip( sourceClip, name, startFrame, endFrame, fps = 30 ) { + + const clip = sourceClip.clone(); + + clip.name = name; + + const tracks = []; + + for ( let i = 0; i < clip.tracks.length; ++ i ) { + + const track = clip.tracks[ i ]; + const valueSize = track.getValueSize(); + + const times = []; + const values = []; + + for ( let j = 0; j < track.times.length; ++ j ) { + + const frame = track.times[ j ] * fps; + + if ( frame < startFrame || frame >= endFrame ) continue; + + times.push( track.times[ j ] ); + + for ( let k = 0; k < valueSize; ++ k ) { + + values.push( track.values[ j * valueSize + k ] ); + + } + + } + + if ( times.length === 0 ) continue; + + track.times = convertArray( times, track.times.constructor ); + track.values = convertArray( values, track.values.constructor ); + + tracks.push( track ); + + } + + clip.tracks = tracks; + + // find minimum .times value across all tracks in the trimmed clip + + let minStartTime = Infinity; + + for ( let i = 0; i < clip.tracks.length; ++ i ) { + + if ( minStartTime > clip.tracks[ i ].times[ 0 ] ) { + + minStartTime = clip.tracks[ i ].times[ 0 ]; + + } + + } + + // shift all tracks such that clip begins at t=0 + + for ( let i = 0; i < clip.tracks.length; ++ i ) { + + clip.tracks[ i ].shift( - 1 * minStartTime ); + + } + + clip.resetDuration(); + + return clip; + +} + +function makeClipAdditive( targetClip, referenceFrame = 0, referenceClip = targetClip, fps = 30 ) { + + if ( fps <= 0 ) fps = 30; + + const numTracks = referenceClip.tracks.length; + const referenceTime = referenceFrame / fps; + + // Make each track's values relative to the values at the reference frame + for ( let i = 0; i < numTracks; ++ i ) { + + const referenceTrack = referenceClip.tracks[ i ]; + const referenceTrackType = referenceTrack.ValueTypeName; + + // Skip this track if it's non-numeric + if ( referenceTrackType === 'bool' || referenceTrackType === 'string' ) continue; + + // Find the track in the target clip whose name and type matches the reference track + const targetTrack = targetClip.tracks.find( function ( track ) { + + return track.name === referenceTrack.name + && track.ValueTypeName === referenceTrackType; + + } ); + + if ( targetTrack === undefined ) continue; + + let referenceOffset = 0; + const referenceValueSize = referenceTrack.getValueSize(); + + if ( referenceTrack.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline ) { + + referenceOffset = referenceValueSize / 3; + + } + + let targetOffset = 0; + const targetValueSize = targetTrack.getValueSize(); + + if ( targetTrack.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline ) { + + targetOffset = targetValueSize / 3; + + } + + const lastIndex = referenceTrack.times.length - 1; + let referenceValue; + + // Find the value to subtract out of the track + if ( referenceTime <= referenceTrack.times[ 0 ] ) { + + // Reference frame is earlier than the first keyframe, so just use the first keyframe + const startIndex = referenceOffset; + const endIndex = referenceValueSize - referenceOffset; + referenceValue = referenceTrack.values.slice( startIndex, endIndex ); + + } else if ( referenceTime >= referenceTrack.times[ lastIndex ] ) { + + // Reference frame is after the last keyframe, so just use the last keyframe + const startIndex = lastIndex * referenceValueSize + referenceOffset; + const endIndex = startIndex + referenceValueSize - referenceOffset; + referenceValue = referenceTrack.values.slice( startIndex, endIndex ); + + } else { + + // Interpolate to the reference value + const interpolant = referenceTrack.createInterpolant(); + const startIndex = referenceOffset; + const endIndex = referenceValueSize - referenceOffset; + interpolant.evaluate( referenceTime ); + referenceValue = interpolant.resultBuffer.slice( startIndex, endIndex ); + + } + + // Conjugate the quaternion + if ( referenceTrackType === 'quaternion' ) { + + const referenceQuat = new Quaternion().fromArray( referenceValue ).normalize().conjugate(); + referenceQuat.toArray( referenceValue ); + + } + + // Subtract the reference value from all of the track values + + const numTimes = targetTrack.times.length; + for ( let j = 0; j < numTimes; ++ j ) { + + const valueStart = j * targetValueSize + targetOffset; + + if ( referenceTrackType === 'quaternion' ) { + + // Multiply the conjugate for quaternion track types + Quaternion.multiplyQuaternionsFlat( + targetTrack.values, + valueStart, + referenceValue, + 0, + targetTrack.values, + valueStart + ); + + } else { + + const valueEnd = targetValueSize - targetOffset * 2; + + // Subtract each value for all other numeric track types + for ( let k = 0; k < valueEnd; ++ k ) { + + targetTrack.values[ valueStart + k ] -= referenceValue[ k ]; + + } + + } + + } + + } + + targetClip.blendMode = AdditiveAnimationBlendMode; + + return targetClip; + +} + +const AnimationUtils = { + convertArray: convertArray, + isTypedArray: isTypedArray, + getKeyframeOrder: getKeyframeOrder, + sortedArray: sortedArray, + flattenJSON: flattenJSON, + subclip: subclip, + makeClipAdditive: makeClipAdditive +}; + +/** + * Abstract base class of interpolants over parametric samples. + * + * The parameter domain is one dimensional, typically the time or a path + * along a curve defined by the data. + * + * The sample values can have any dimensionality and derived classes may + * apply special interpretations to the data. + * + * This class provides the interval seek in a Template Method, deferring + * the actual interpolation to derived classes. + * + * Time complexity is O(1) for linear access crossing at most two points + * and O(log N) for random access, where N is the number of positions. + * + * References: + * + * http://www.oodesign.com/template-method-pattern.html + * + */ + +class Interpolant { + + constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) { + + this.parameterPositions = parameterPositions; + this._cachedIndex = 0; + + this.resultBuffer = resultBuffer !== undefined ? + resultBuffer : new sampleValues.constructor( sampleSize ); + this.sampleValues = sampleValues; + this.valueSize = sampleSize; + + this.settings = null; + this.DefaultSettings_ = {}; + + } + + evaluate( t ) { + + const pp = this.parameterPositions; + let i1 = this._cachedIndex, + t1 = pp[ i1 ], + t0 = pp[ i1 - 1 ]; + + validate_interval: { + + seek: { + + let right; + + linear_scan: { + + //- See http://jsperf.com/comparison-to-undefined/3 + //- slower code: + //- + //- if ( t >= t1 || t1 === undefined ) { + forward_scan: if ( ! ( t < t1 ) ) { + + for ( let giveUpAt = i1 + 2; ; ) { + + if ( t1 === undefined ) { + + if ( t < t0 ) break forward_scan; + + // after end + + i1 = pp.length; + this._cachedIndex = i1; + return this.copySampleValue_( i1 - 1 ); + + } + + if ( i1 === giveUpAt ) break; // this loop + + t0 = t1; + t1 = pp[ ++ i1 ]; + + if ( t < t1 ) { + + // we have arrived at the sought interval + break seek; + + } + + } + + // prepare binary search on the right side of the index + right = pp.length; + break linear_scan; + + } + + //- slower code: + //- if ( t < t0 || t0 === undefined ) { + if ( ! ( t >= t0 ) ) { + + // looping? + + const t1global = pp[ 1 ]; + + if ( t < t1global ) { + + i1 = 2; // + 1, using the scan for the details + t0 = t1global; + + } + + // linear reverse scan + + for ( let giveUpAt = i1 - 2; ; ) { + + if ( t0 === undefined ) { + + // before start + + this._cachedIndex = 0; + return this.copySampleValue_( 0 ); + + } + + if ( i1 === giveUpAt ) break; // this loop + + t1 = t0; + t0 = pp[ -- i1 - 1 ]; + + if ( t >= t0 ) { + + // we have arrived at the sought interval + break seek; + + } + + } + + // prepare binary search on the left side of the index + right = i1; + i1 = 0; + break linear_scan; + + } + + // the interval is valid + + break validate_interval; + + } // linear scan + + // binary search + + while ( i1 < right ) { + + const mid = ( i1 + right ) >>> 1; + + if ( t < pp[ mid ] ) { + + right = mid; + + } else { + + i1 = mid + 1; + + } + + } + + t1 = pp[ i1 ]; + t0 = pp[ i1 - 1 ]; + + // check boundary cases, again + + if ( t0 === undefined ) { + + this._cachedIndex = 0; + return this.copySampleValue_( 0 ); + + } + + if ( t1 === undefined ) { + + i1 = pp.length; + this._cachedIndex = i1; + return this.copySampleValue_( i1 - 1 ); + + } + + } // seek + + this._cachedIndex = i1; + + this.intervalChanged_( i1, t0, t1 ); + + } // validate_interval + + return this.interpolate_( i1, t0, t, t1 ); + + } + + getSettings_() { + + return this.settings || this.DefaultSettings_; + + } + + copySampleValue_( index ) { + + // copies a sample value to the result buffer + + const result = this.resultBuffer, + values = this.sampleValues, + stride = this.valueSize, + offset = index * stride; + + for ( let i = 0; i !== stride; ++ i ) { + + result[ i ] = values[ offset + i ]; + + } + + return result; + + } + + // Template methods for derived classes: + + interpolate_( /* i1, t0, t, t1 */ ) { + + throw new Error( 'call to abstract method' ); + // implementations shall return this.resultBuffer + + } + + intervalChanged_( /* i1, t0, t1 */ ) { + + // empty + + } + +} + +/** + * Fast and simple cubic spline interpolant. + * + * It was derived from a Hermitian construction setting the first derivative + * at each sample position to the linear slope between neighboring positions + * over their parameter interval. + */ + +class CubicInterpolant extends Interpolant { + + constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) { + + super( parameterPositions, sampleValues, sampleSize, resultBuffer ); + + this._weightPrev = - 0; + this._offsetPrev = - 0; + this._weightNext = - 0; + this._offsetNext = - 0; + + this.DefaultSettings_ = { + + endingStart: ZeroCurvatureEnding, + endingEnd: ZeroCurvatureEnding + + }; + + } + + intervalChanged_( i1, t0, t1 ) { + + const pp = this.parameterPositions; + let iPrev = i1 - 2, + iNext = i1 + 1, + + tPrev = pp[ iPrev ], + tNext = pp[ iNext ]; + + if ( tPrev === undefined ) { + + switch ( this.getSettings_().endingStart ) { + + case ZeroSlopeEnding: + + // f'(t0) = 0 + iPrev = i1; + tPrev = 2 * t0 - t1; + + break; + + case WrapAroundEnding: + + // use the other end of the curve + iPrev = pp.length - 2; + tPrev = t0 + pp[ iPrev ] - pp[ iPrev + 1 ]; + + break; + + default: // ZeroCurvatureEnding + + // f''(t0) = 0 a.k.a. Natural Spline + iPrev = i1; + tPrev = t1; + + } + + } + + if ( tNext === undefined ) { + + switch ( this.getSettings_().endingEnd ) { + + case ZeroSlopeEnding: + + // f'(tN) = 0 + iNext = i1; + tNext = 2 * t1 - t0; + + break; + + case WrapAroundEnding: + + // use the other end of the curve + iNext = 1; + tNext = t1 + pp[ 1 ] - pp[ 0 ]; + + break; + + default: // ZeroCurvatureEnding + + // f''(tN) = 0, a.k.a. Natural Spline + iNext = i1 - 1; + tNext = t0; + + } + + } + + const halfDt = ( t1 - t0 ) * 0.5, + stride = this.valueSize; + + this._weightPrev = halfDt / ( t0 - tPrev ); + this._weightNext = halfDt / ( tNext - t1 ); + this._offsetPrev = iPrev * stride; + this._offsetNext = iNext * stride; + + } + + interpolate_( i1, t0, t, t1 ) { + + const result = this.resultBuffer, + values = this.sampleValues, + stride = this.valueSize, + + o1 = i1 * stride, o0 = o1 - stride, + oP = this._offsetPrev, oN = this._offsetNext, + wP = this._weightPrev, wN = this._weightNext, + + p = ( t - t0 ) / ( t1 - t0 ), + pp = p * p, + ppp = pp * p; + + // evaluate polynomials + + const sP = - wP * ppp + 2 * wP * pp - wP * p; + const s0 = ( 1 + wP ) * ppp + ( - 1.5 - 2 * wP ) * pp + ( - 0.5 + wP ) * p + 1; + const s1 = ( - 1 - wN ) * ppp + ( 1.5 + wN ) * pp + 0.5 * p; + const sN = wN * ppp - wN * pp; + + // combine data linearly + + for ( let i = 0; i !== stride; ++ i ) { + + result[ i ] = + sP * values[ oP + i ] + + s0 * values[ o0 + i ] + + s1 * values[ o1 + i ] + + sN * values[ oN + i ]; + + } + + return result; + + } + +} + +class LinearInterpolant extends Interpolant { + + constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) { + + super( parameterPositions, sampleValues, sampleSize, resultBuffer ); + + } + + interpolate_( i1, t0, t, t1 ) { + + const result = this.resultBuffer, + values = this.sampleValues, + stride = this.valueSize, + + offset1 = i1 * stride, + offset0 = offset1 - stride, + + weight1 = ( t - t0 ) / ( t1 - t0 ), + weight0 = 1 - weight1; + + for ( let i = 0; i !== stride; ++ i ) { + + result[ i ] = + values[ offset0 + i ] * weight0 + + values[ offset1 + i ] * weight1; + + } + + return result; + + } + +} + +/** + * + * Interpolant that evaluates to the sample value at the position preceding + * the parameter. + */ + +class DiscreteInterpolant extends Interpolant { + + constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) { + + super( parameterPositions, sampleValues, sampleSize, resultBuffer ); + + } + + interpolate_( i1 /*, t0, t, t1 */ ) { + + return this.copySampleValue_( i1 - 1 ); + + } + +} + +class KeyframeTrack { + + constructor( name, times, values, interpolation ) { + + if ( name === undefined ) throw new Error( 'THREE.KeyframeTrack: track name is undefined' ); + if ( times === undefined || times.length === 0 ) throw new Error( 'THREE.KeyframeTrack: no keyframes in track named ' + name ); + + this.name = name; + + this.times = convertArray( times, this.TimeBufferType ); + this.values = convertArray( values, this.ValueBufferType ); + + this.setInterpolation( interpolation || this.DefaultInterpolation ); + + } + + // Serialization (in static context, because of constructor invocation + // and automatic invocation of .toJSON): + + static toJSON( track ) { + + const trackType = track.constructor; + + let json; + + // derived classes can define a static toJSON method + if ( trackType.toJSON !== this.toJSON ) { + + json = trackType.toJSON( track ); + + } else { + + // by default, we assume the data can be serialized as-is + json = { + + 'name': track.name, + 'times': convertArray( track.times, Array ), + 'values': convertArray( track.values, Array ) + + }; + + const interpolation = track.getInterpolation(); + + if ( interpolation !== track.DefaultInterpolation ) { + + json.interpolation = interpolation; + + } + + } + + json.type = track.ValueTypeName; // mandatory + + return json; + + } + + InterpolantFactoryMethodDiscrete( result ) { + + return new DiscreteInterpolant( this.times, this.values, this.getValueSize(), result ); + + } + + InterpolantFactoryMethodLinear( result ) { + + return new LinearInterpolant( this.times, this.values, this.getValueSize(), result ); + + } + + InterpolantFactoryMethodSmooth( result ) { + + return new CubicInterpolant( this.times, this.values, this.getValueSize(), result ); + + } + + setInterpolation( interpolation ) { + + let factoryMethod; + + switch ( interpolation ) { + + case InterpolateDiscrete: + + factoryMethod = this.InterpolantFactoryMethodDiscrete; + + break; + + case InterpolateLinear: + + factoryMethod = this.InterpolantFactoryMethodLinear; + + break; + + case InterpolateSmooth: + + factoryMethod = this.InterpolantFactoryMethodSmooth; + + break; + + } + + if ( factoryMethod === undefined ) { + + const message = 'unsupported interpolation for ' + + this.ValueTypeName + ' keyframe track named ' + this.name; + + if ( this.createInterpolant === undefined ) { + + // fall back to default, unless the default itself is messed up + if ( interpolation !== this.DefaultInterpolation ) { + + this.setInterpolation( this.DefaultInterpolation ); + + } else { + + throw new Error( message ); // fatal, in this case + + } + + } + + console.warn( 'THREE.KeyframeTrack:', message ); + return this; + + } + + this.createInterpolant = factoryMethod; + + return this; + + } + + getInterpolation() { + + switch ( this.createInterpolant ) { + + case this.InterpolantFactoryMethodDiscrete: + + return InterpolateDiscrete; + + case this.InterpolantFactoryMethodLinear: + + return InterpolateLinear; + + case this.InterpolantFactoryMethodSmooth: + + return InterpolateSmooth; + + } + + } + + getValueSize() { + + return this.values.length / this.times.length; + + } + + // move all keyframes either forwards or backwards in time + shift( timeOffset ) { + + if ( timeOffset !== 0.0 ) { + + const times = this.times; + + for ( let i = 0, n = times.length; i !== n; ++ i ) { + + times[ i ] += timeOffset; + + } + + } + + return this; + + } + + // scale all keyframe times by a factor (useful for frame <-> seconds conversions) + scale( timeScale ) { + + if ( timeScale !== 1.0 ) { + + const times = this.times; + + for ( let i = 0, n = times.length; i !== n; ++ i ) { + + times[ i ] *= timeScale; + + } + + } + + return this; + + } + + // removes keyframes before and after animation without changing any values within the range [startTime, endTime]. + // IMPORTANT: We do not shift around keys to the start of the track time, because for interpolated keys this will change their values + trim( startTime, endTime ) { + + const times = this.times, + nKeys = times.length; + + let from = 0, + to = nKeys - 1; + + while ( from !== nKeys && times[ from ] < startTime ) { + + ++ from; + + } + + while ( to !== - 1 && times[ to ] > endTime ) { + + -- to; + + } + + ++ to; // inclusive -> exclusive bound + + if ( from !== 0 || to !== nKeys ) { + + // empty tracks are forbidden, so keep at least one keyframe + if ( from >= to ) { + + to = Math.max( to, 1 ); + from = to - 1; + + } + + const stride = this.getValueSize(); + this.times = times.slice( from, to ); + this.values = this.values.slice( from * stride, to * stride ); + + } + + return this; + + } + + // ensure we do not get a GarbageInGarbageOut situation, make sure tracks are at least minimally viable + validate() { + + let valid = true; + + const valueSize = this.getValueSize(); + if ( valueSize - Math.floor( valueSize ) !== 0 ) { + + console.error( 'THREE.KeyframeTrack: Invalid value size in track.', this ); + valid = false; + + } + + const times = this.times, + values = this.values, + + nKeys = times.length; + + if ( nKeys === 0 ) { + + console.error( 'THREE.KeyframeTrack: Track is empty.', this ); + valid = false; + + } + + let prevTime = null; + + for ( let i = 0; i !== nKeys; i ++ ) { + + const currTime = times[ i ]; + + if ( typeof currTime === 'number' && isNaN( currTime ) ) { + + console.error( 'THREE.KeyframeTrack: Time is not a valid number.', this, i, currTime ); + valid = false; + break; + + } + + if ( prevTime !== null && prevTime > currTime ) { + + console.error( 'THREE.KeyframeTrack: Out of order keys.', this, i, currTime, prevTime ); + valid = false; + break; + + } + + prevTime = currTime; + + } + + if ( values !== undefined ) { + + if ( isTypedArray( values ) ) { + + for ( let i = 0, n = values.length; i !== n; ++ i ) { + + const value = values[ i ]; + + if ( isNaN( value ) ) { + + console.error( 'THREE.KeyframeTrack: Value is not a valid number.', this, i, value ); + valid = false; + break; + + } + + } + + } + + } + + return valid; + + } + + // removes equivalent sequential keys as common in morph target sequences + // (0,0,0,0,1,1,1,0,0,0,0,0,0,0) --> (0,0,1,1,0,0) + optimize() { + + // times or values may be shared with other tracks, so overwriting is unsafe + const times = this.times.slice(), + values = this.values.slice(), + stride = this.getValueSize(), + + smoothInterpolation = this.getInterpolation() === InterpolateSmooth, + + lastIndex = times.length - 1; + + let writeIndex = 1; + + for ( let i = 1; i < lastIndex; ++ i ) { + + let keep = false; + + const time = times[ i ]; + const timeNext = times[ i + 1 ]; + + // remove adjacent keyframes scheduled at the same time + + if ( time !== timeNext && ( i !== 1 || time !== times[ 0 ] ) ) { + + if ( ! smoothInterpolation ) { + + // remove unnecessary keyframes same as their neighbors + + const offset = i * stride, + offsetP = offset - stride, + offsetN = offset + stride; + + for ( let j = 0; j !== stride; ++ j ) { + + const value = values[ offset + j ]; + + if ( value !== values[ offsetP + j ] || + value !== values[ offsetN + j ] ) { + + keep = true; + break; + + } + + } + + } else { + + keep = true; + + } + + } + + // in-place compaction + + if ( keep ) { + + if ( i !== writeIndex ) { + + times[ writeIndex ] = times[ i ]; + + const readOffset = i * stride, + writeOffset = writeIndex * stride; + + for ( let j = 0; j !== stride; ++ j ) { + + values[ writeOffset + j ] = values[ readOffset + j ]; + + } + + } + + ++ writeIndex; + + } + + } + + // flush last keyframe (compaction looks ahead) + + if ( lastIndex > 0 ) { + + times[ writeIndex ] = times[ lastIndex ]; + + for ( let readOffset = lastIndex * stride, writeOffset = writeIndex * stride, j = 0; j !== stride; ++ j ) { + + values[ writeOffset + j ] = values[ readOffset + j ]; + + } + + ++ writeIndex; + + } + + if ( writeIndex !== times.length ) { + + this.times = times.slice( 0, writeIndex ); + this.values = values.slice( 0, writeIndex * stride ); + + } else { + + this.times = times; + this.values = values; + + } + + return this; + + } + + clone() { + + const times = this.times.slice(); + const values = this.values.slice(); + + const TypedKeyframeTrack = this.constructor; + const track = new TypedKeyframeTrack( this.name, times, values ); + + // Interpolant argument to constructor is not saved, so copy the factory method directly. + track.createInterpolant = this.createInterpolant; + + return track; + + } + +} + +KeyframeTrack.prototype.TimeBufferType = Float32Array; +KeyframeTrack.prototype.ValueBufferType = Float32Array; +KeyframeTrack.prototype.DefaultInterpolation = InterpolateLinear; + +/** + * A Track of Boolean keyframe values. + */ +class BooleanKeyframeTrack extends KeyframeTrack {} + +BooleanKeyframeTrack.prototype.ValueTypeName = 'bool'; +BooleanKeyframeTrack.prototype.ValueBufferType = Array; +BooleanKeyframeTrack.prototype.DefaultInterpolation = InterpolateDiscrete; +BooleanKeyframeTrack.prototype.InterpolantFactoryMethodLinear = undefined; +BooleanKeyframeTrack.prototype.InterpolantFactoryMethodSmooth = undefined; + +/** + * A Track of keyframe values that represent color. + */ +class ColorKeyframeTrack extends KeyframeTrack {} + +ColorKeyframeTrack.prototype.ValueTypeName = 'color'; + +/** + * A Track of numeric keyframe values. + */ +class NumberKeyframeTrack extends KeyframeTrack {} + +NumberKeyframeTrack.prototype.ValueTypeName = 'number'; + +/** + * Spherical linear unit quaternion interpolant. + */ + +class QuaternionLinearInterpolant extends Interpolant { + + constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) { + + super( parameterPositions, sampleValues, sampleSize, resultBuffer ); + + } + + interpolate_( i1, t0, t, t1 ) { + + const result = this.resultBuffer, + values = this.sampleValues, + stride = this.valueSize, + + alpha = ( t - t0 ) / ( t1 - t0 ); + + let offset = i1 * stride; + + for ( let end = offset + stride; offset !== end; offset += 4 ) { + + Quaternion.slerpFlat( result, 0, values, offset - stride, values, offset, alpha ); + + } + + return result; + + } + +} + +/** + * A Track of quaternion keyframe values. + */ +class QuaternionKeyframeTrack extends KeyframeTrack { + + InterpolantFactoryMethodLinear( result ) { + + return new QuaternionLinearInterpolant( this.times, this.values, this.getValueSize(), result ); + + } + +} + +QuaternionKeyframeTrack.prototype.ValueTypeName = 'quaternion'; +// ValueBufferType is inherited +QuaternionKeyframeTrack.prototype.DefaultInterpolation = InterpolateLinear; +QuaternionKeyframeTrack.prototype.InterpolantFactoryMethodSmooth = undefined; + +/** + * A Track that interpolates Strings + */ +class StringKeyframeTrack extends KeyframeTrack {} + +StringKeyframeTrack.prototype.ValueTypeName = 'string'; +StringKeyframeTrack.prototype.ValueBufferType = Array; +StringKeyframeTrack.prototype.DefaultInterpolation = InterpolateDiscrete; +StringKeyframeTrack.prototype.InterpolantFactoryMethodLinear = undefined; +StringKeyframeTrack.prototype.InterpolantFactoryMethodSmooth = undefined; + +/** + * A Track of vectored keyframe values. + */ +class VectorKeyframeTrack extends KeyframeTrack {} + +VectorKeyframeTrack.prototype.ValueTypeName = 'vector'; + +class AnimationClip { + + constructor( name = '', duration = - 1, tracks = [], blendMode = NormalAnimationBlendMode ) { + + this.name = name; + this.tracks = tracks; + this.duration = duration; + this.blendMode = blendMode; + + this.uuid = generateUUID(); + + // this means it should figure out its duration by scanning the tracks + if ( this.duration < 0 ) { + + this.resetDuration(); + + } + + } + + + static parse( json ) { + + const tracks = [], + jsonTracks = json.tracks, + frameTime = 1.0 / ( json.fps || 1.0 ); + + for ( let i = 0, n = jsonTracks.length; i !== n; ++ i ) { + + tracks.push( parseKeyframeTrack( jsonTracks[ i ] ).scale( frameTime ) ); + + } + + const clip = new this( json.name, json.duration, tracks, json.blendMode ); + clip.uuid = json.uuid; + + return clip; + + } + + static toJSON( clip ) { + + const tracks = [], + clipTracks = clip.tracks; + + const json = { + + 'name': clip.name, + 'duration': clip.duration, + 'tracks': tracks, + 'uuid': clip.uuid, + 'blendMode': clip.blendMode + + }; + + for ( let i = 0, n = clipTracks.length; i !== n; ++ i ) { + + tracks.push( KeyframeTrack.toJSON( clipTracks[ i ] ) ); + + } + + return json; + + } + + static CreateFromMorphTargetSequence( name, morphTargetSequence, fps, noLoop ) { + + const numMorphTargets = morphTargetSequence.length; + const tracks = []; + + for ( let i = 0; i < numMorphTargets; i ++ ) { + + let times = []; + let values = []; + + times.push( + ( i + numMorphTargets - 1 ) % numMorphTargets, + i, + ( i + 1 ) % numMorphTargets ); + + values.push( 0, 1, 0 ); + + const order = getKeyframeOrder( times ); + times = sortedArray( times, 1, order ); + values = sortedArray( values, 1, order ); + + // if there is a key at the first frame, duplicate it as the + // last frame as well for perfect loop. + if ( ! noLoop && times[ 0 ] === 0 ) { + + times.push( numMorphTargets ); + values.push( values[ 0 ] ); + + } + + tracks.push( + new NumberKeyframeTrack( + '.morphTargetInfluences[' + morphTargetSequence[ i ].name + ']', + times, values + ).scale( 1.0 / fps ) ); + + } + + return new this( name, - 1, tracks ); + + } + + static findByName( objectOrClipArray, name ) { + + let clipArray = objectOrClipArray; + + if ( ! Array.isArray( objectOrClipArray ) ) { + + const o = objectOrClipArray; + clipArray = o.geometry && o.geometry.animations || o.animations; + + } + + for ( let i = 0; i < clipArray.length; i ++ ) { + + if ( clipArray[ i ].name === name ) { + + return clipArray[ i ]; + + } + + } + + return null; + + } + + static CreateClipsFromMorphTargetSequences( morphTargets, fps, noLoop ) { + + const animationToMorphTargets = {}; + + // tested with https://regex101.com/ on trick sequences + // such flamingo_flyA_003, flamingo_run1_003, crdeath0059 + const pattern = /^([\w-]*?)([\d]+)$/; + + // sort morph target names into animation groups based + // patterns like Walk_001, Walk_002, Run_001, Run_002 + for ( let i = 0, il = morphTargets.length; i < il; i ++ ) { + + const morphTarget = morphTargets[ i ]; + const parts = morphTarget.name.match( pattern ); + + if ( parts && parts.length > 1 ) { + + const name = parts[ 1 ]; + + let animationMorphTargets = animationToMorphTargets[ name ]; + + if ( ! animationMorphTargets ) { + + animationToMorphTargets[ name ] = animationMorphTargets = []; + + } + + animationMorphTargets.push( morphTarget ); + + } + + } + + const clips = []; + + for ( const name in animationToMorphTargets ) { + + clips.push( this.CreateFromMorphTargetSequence( name, animationToMorphTargets[ name ], fps, noLoop ) ); + + } + + return clips; + + } + + // parse the animation.hierarchy format + static parseAnimation( animation, bones ) { + + if ( ! animation ) { + + console.error( 'THREE.AnimationClip: No animation in JSONLoader data.' ); + return null; + + } + + const addNonemptyTrack = function ( trackType, trackName, animationKeys, propertyName, destTracks ) { + + // only return track if there are actually keys. + if ( animationKeys.length !== 0 ) { + + const times = []; + const values = []; + + flattenJSON( animationKeys, times, values, propertyName ); + + // empty keys are filtered out, so check again + if ( times.length !== 0 ) { + + destTracks.push( new trackType( trackName, times, values ) ); + + } + + } + + }; + + const tracks = []; + + const clipName = animation.name || 'default'; + const fps = animation.fps || 30; + const blendMode = animation.blendMode; + + // automatic length determination in AnimationClip. + let duration = animation.length || - 1; + + const hierarchyTracks = animation.hierarchy || []; + + for ( let h = 0; h < hierarchyTracks.length; h ++ ) { + + const animationKeys = hierarchyTracks[ h ].keys; + + // skip empty tracks + if ( ! animationKeys || animationKeys.length === 0 ) continue; + + // process morph targets + if ( animationKeys[ 0 ].morphTargets ) { + + // figure out all morph targets used in this track + const morphTargetNames = {}; + + let k; + + for ( k = 0; k < animationKeys.length; k ++ ) { + + if ( animationKeys[ k ].morphTargets ) { + + for ( let m = 0; m < animationKeys[ k ].morphTargets.length; m ++ ) { + + morphTargetNames[ animationKeys[ k ].morphTargets[ m ] ] = - 1; + + } + + } + + } + + // create a track for each morph target with all zero + // morphTargetInfluences except for the keys in which + // the morphTarget is named. + for ( const morphTargetName in morphTargetNames ) { + + const times = []; + const values = []; + + for ( let m = 0; m !== animationKeys[ k ].morphTargets.length; ++ m ) { + + const animationKey = animationKeys[ k ]; + + times.push( animationKey.time ); + values.push( ( animationKey.morphTarget === morphTargetName ) ? 1 : 0 ); + + } + + tracks.push( new NumberKeyframeTrack( '.morphTargetInfluence[' + morphTargetName + ']', times, values ) ); + + } + + duration = morphTargetNames.length * fps; + + } else { + + // ...assume skeletal animation + + const boneName = '.bones[' + bones[ h ].name + ']'; + + addNonemptyTrack( + VectorKeyframeTrack, boneName + '.position', + animationKeys, 'pos', tracks ); + + addNonemptyTrack( + QuaternionKeyframeTrack, boneName + '.quaternion', + animationKeys, 'rot', tracks ); + + addNonemptyTrack( + VectorKeyframeTrack, boneName + '.scale', + animationKeys, 'scl', tracks ); + + } + + } + + if ( tracks.length === 0 ) { + + return null; + + } + + const clip = new this( clipName, duration, tracks, blendMode ); + + return clip; + + } + + resetDuration() { + + const tracks = this.tracks; + let duration = 0; + + for ( let i = 0, n = tracks.length; i !== n; ++ i ) { + + const track = this.tracks[ i ]; + + duration = Math.max( duration, track.times[ track.times.length - 1 ] ); + + } + + this.duration = duration; + + return this; + + } + + trim() { + + for ( let i = 0; i < this.tracks.length; i ++ ) { + + this.tracks[ i ].trim( 0, this.duration ); + + } + + return this; + + } + + validate() { + + let valid = true; + + for ( let i = 0; i < this.tracks.length; i ++ ) { + + valid = valid && this.tracks[ i ].validate(); + + } + + return valid; + + } + + optimize() { + + for ( let i = 0; i < this.tracks.length; i ++ ) { + + this.tracks[ i ].optimize(); + + } + + return this; + + } + + clone() { + + const tracks = []; + + for ( let i = 0; i < this.tracks.length; i ++ ) { + + tracks.push( this.tracks[ i ].clone() ); + + } + + return new this.constructor( this.name, this.duration, tracks, this.blendMode ); + + } + + toJSON() { + + return this.constructor.toJSON( this ); + + } + +} + +function getTrackTypeForValueTypeName( typeName ) { + + switch ( typeName.toLowerCase() ) { + + case 'scalar': + case 'double': + case 'float': + case 'number': + case 'integer': + + return NumberKeyframeTrack; + + case 'vector': + case 'vector2': + case 'vector3': + case 'vector4': + + return VectorKeyframeTrack; + + case 'color': + + return ColorKeyframeTrack; + + case 'quaternion': + + return QuaternionKeyframeTrack; + + case 'bool': + case 'boolean': + + return BooleanKeyframeTrack; + + case 'string': + + return StringKeyframeTrack; + + } + + throw new Error( 'THREE.KeyframeTrack: Unsupported typeName: ' + typeName ); + +} + +function parseKeyframeTrack( json ) { + + if ( json.type === undefined ) { + + throw new Error( 'THREE.KeyframeTrack: track type undefined, can not parse' ); + + } + + const trackType = getTrackTypeForValueTypeName( json.type ); + + if ( json.times === undefined ) { + + const times = [], values = []; + + flattenJSON( json.keys, times, values, 'value' ); + + json.times = times; + json.values = values; + + } + + // derived classes can define a static parse method + if ( trackType.parse !== undefined ) { + + return trackType.parse( json ); + + } else { + + // by default, we assume a constructor compatible with the base + return new trackType( json.name, json.times, json.values, json.interpolation ); + + } + +} + +const Cache = { + + enabled: false, + + files: {}, + + add: function ( key, file ) { + + if ( this.enabled === false ) return; + + // console.log( 'THREE.Cache', 'Adding key:', key ); + + this.files[ key ] = file; + + }, + + get: function ( key ) { + + if ( this.enabled === false ) return; + + // console.log( 'THREE.Cache', 'Checking key:', key ); + + return this.files[ key ]; + + }, + + remove: function ( key ) { + + delete this.files[ key ]; + + }, + + clear: function () { + + this.files = {}; + + } + +}; + +class LoadingManager { + + constructor( onLoad, onProgress, onError ) { + + const scope = this; + + let isLoading = false; + let itemsLoaded = 0; + let itemsTotal = 0; + let urlModifier = undefined; + const handlers = []; + + // Refer to #5689 for the reason why we don't set .onStart + // in the constructor + + this.onStart = undefined; + this.onLoad = onLoad; + this.onProgress = onProgress; + this.onError = onError; + + this.itemStart = function ( url ) { + + itemsTotal ++; + + if ( isLoading === false ) { + + if ( scope.onStart !== undefined ) { + + scope.onStart( url, itemsLoaded, itemsTotal ); + + } + + } + + isLoading = true; + + }; + + this.itemEnd = function ( url ) { + + itemsLoaded ++; + + if ( scope.onProgress !== undefined ) { + + scope.onProgress( url, itemsLoaded, itemsTotal ); + + } + + if ( itemsLoaded === itemsTotal ) { + + isLoading = false; + + if ( scope.onLoad !== undefined ) { + + scope.onLoad(); + + } + + } + + }; + + this.itemError = function ( url ) { + + if ( scope.onError !== undefined ) { + + scope.onError( url ); + + } + + }; + + this.resolveURL = function ( url ) { + + if ( urlModifier ) { + + return urlModifier( url ); + + } + + return url; + + }; + + this.setURLModifier = function ( transform ) { + + urlModifier = transform; + + return this; + + }; + + this.addHandler = function ( regex, loader ) { + + handlers.push( regex, loader ); + + return this; + + }; + + this.removeHandler = function ( regex ) { + + const index = handlers.indexOf( regex ); + + if ( index !== - 1 ) { + + handlers.splice( index, 2 ); + + } + + return this; + + }; + + this.getHandler = function ( file ) { + + for ( let i = 0, l = handlers.length; i < l; i += 2 ) { + + const regex = handlers[ i ]; + const loader = handlers[ i + 1 ]; + + if ( regex.global ) regex.lastIndex = 0; // see #17920 + + if ( regex.test( file ) ) { + + return loader; + + } + + } + + return null; + + }; + + } + +} + +const DefaultLoadingManager = /*@__PURE__*/ new LoadingManager(); + +class Loader { + + constructor( manager ) { + + this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager; + + this.crossOrigin = 'anonymous'; + this.withCredentials = false; + this.path = ''; + this.resourcePath = ''; + this.requestHeader = {}; + + } + + load( /* url, onLoad, onProgress, onError */ ) {} + + loadAsync( url, onProgress ) { + + const scope = this; + + return new Promise( function ( resolve, reject ) { + + scope.load( url, resolve, onProgress, reject ); + + } ); + + } + + parse( /* data */ ) {} + + setCrossOrigin( crossOrigin ) { + + this.crossOrigin = crossOrigin; + return this; + + } + + setWithCredentials( value ) { + + this.withCredentials = value; + return this; + + } + + setPath( path ) { + + this.path = path; + return this; + + } + + setResourcePath( resourcePath ) { + + this.resourcePath = resourcePath; + return this; + + } + + setRequestHeader( requestHeader ) { + + this.requestHeader = requestHeader; + return this; + + } + +} + +Loader.DEFAULT_MATERIAL_NAME = '__DEFAULT'; + +const loading = {}; + +class HttpError extends Error { + + constructor( message, response ) { + + super( message ); + this.response = response; + + } + +} + +class FileLoader extends Loader { + + constructor( manager ) { + + super( manager ); + + } + + load( url, onLoad, onProgress, onError ) { + + if ( url === undefined ) url = ''; + + if ( this.path !== undefined ) url = this.path + url; + + url = this.manager.resolveURL( url ); + + const cached = Cache.get( url ); + + if ( cached !== undefined ) { + + this.manager.itemStart( url ); + + setTimeout( () => { + + if ( onLoad ) onLoad( cached ); + + this.manager.itemEnd( url ); + + }, 0 ); + + return cached; + + } + + // Check if request is duplicate + + if ( loading[ url ] !== undefined ) { + + loading[ url ].push( { + + onLoad: onLoad, + onProgress: onProgress, + onError: onError + + } ); + + return; + + } + + // Initialise array for duplicate requests + loading[ url ] = []; + + loading[ url ].push( { + onLoad: onLoad, + onProgress: onProgress, + onError: onError, + } ); + + // create request + const req = new Request( url, { + headers: new Headers( this.requestHeader ), + credentials: this.withCredentials ? 'include' : 'same-origin', + // An abort controller could be added within a future PR + } ); + + // record states ( avoid data race ) + const mimeType = this.mimeType; + const responseType = this.responseType; + + // start the fetch + fetch( req ) + .then( response => { + + if ( response.status === 200 || response.status === 0 ) { + + // Some browsers return HTTP Status 0 when using non-http protocol + // e.g. 'file://' or 'data://'. Handle as success. + + if ( response.status === 0 ) { + + console.warn( 'THREE.FileLoader: HTTP Status 0 received.' ); + + } + + // Workaround: Checking if response.body === undefined for Alipay browser #23548 + + if ( typeof ReadableStream === 'undefined' || response.body === undefined || response.body.getReader === undefined ) { + + return response; + + } + + const callbacks = loading[ url ]; + const reader = response.body.getReader(); + + // Nginx needs X-File-Size check + // https://serverfault.com/questions/482875/why-does-nginx-remove-content-length-header-for-chunked-content + const contentLength = response.headers.get( 'X-File-Size' ) || response.headers.get( 'Content-Length' ); + const total = contentLength ? parseInt( contentLength ) : 0; + const lengthComputable = total !== 0; + let loaded = 0; + + // periodically read data into the new stream tracking while download progress + const stream = new ReadableStream( { + start( controller ) { + + readData(); + + function readData() { + + reader.read().then( ( { done, value } ) => { + + if ( done ) { + + controller.close(); + + } else { + + loaded += value.byteLength; + + const event = new ProgressEvent( 'progress', { lengthComputable, loaded, total } ); + for ( let i = 0, il = callbacks.length; i < il; i ++ ) { + + const callback = callbacks[ i ]; + if ( callback.onProgress ) callback.onProgress( event ); + + } + + controller.enqueue( value ); + readData(); + + } + + } ); + + } + + } + + } ); + + return new Response( stream ); + + } else { + + throw new HttpError( `fetch for "${response.url}" responded with ${response.status}: ${response.statusText}`, response ); + + } + + } ) + .then( response => { + + switch ( responseType ) { + + case 'arraybuffer': + + return response.arrayBuffer(); + + case 'blob': + + return response.blob(); + + case 'document': + + return response.text() + .then( text => { + + const parser = new DOMParser(); + return parser.parseFromString( text, mimeType ); + + } ); + + case 'json': + + return response.json(); + + default: + + if ( mimeType === undefined ) { + + return response.text(); + + } else { + + // sniff encoding + const re = /charset="?([^;"\s]*)"?/i; + const exec = re.exec( mimeType ); + const label = exec && exec[ 1 ] ? exec[ 1 ].toLowerCase() : undefined; + const decoder = new TextDecoder( label ); + return response.arrayBuffer().then( ab => decoder.decode( ab ) ); + + } + + } + + } ) + .then( data => { + + // Add to cache only on HTTP success, so that we do not cache + // error response bodies as proper responses to requests. + Cache.add( url, data ); + + const callbacks = loading[ url ]; + delete loading[ url ]; + + for ( let i = 0, il = callbacks.length; i < il; i ++ ) { + + const callback = callbacks[ i ]; + if ( callback.onLoad ) callback.onLoad( data ); + + } + + } ) + .catch( err => { + + // Abort errors and other errors are handled the same + + const callbacks = loading[ url ]; + + if ( callbacks === undefined ) { + + // When onLoad was called and url was deleted in `loading` + this.manager.itemError( url ); + throw err; + + } + + delete loading[ url ]; + + for ( let i = 0, il = callbacks.length; i < il; i ++ ) { + + const callback = callbacks[ i ]; + if ( callback.onError ) callback.onError( err ); + + } + + this.manager.itemError( url ); + + } ) + .finally( () => { + + this.manager.itemEnd( url ); + + } ); + + this.manager.itemStart( url ); + + } + + setResponseType( value ) { + + this.responseType = value; + return this; + + } + + setMimeType( value ) { + + this.mimeType = value; + return this; + + } + +} + +class AnimationLoader extends Loader { + + constructor( manager ) { + + super( manager ); + + } + + load( url, onLoad, onProgress, onError ) { + + const scope = this; + + const loader = new FileLoader( this.manager ); + loader.setPath( this.path ); + loader.setRequestHeader( this.requestHeader ); + loader.setWithCredentials( this.withCredentials ); + loader.load( url, function ( text ) { + + try { + + onLoad( scope.parse( JSON.parse( text ) ) ); + + } catch ( e ) { + + if ( onError ) { + + onError( e ); + + } else { + + console.error( e ); + + } + + scope.manager.itemError( url ); + + } + + }, onProgress, onError ); + + } + + parse( json ) { + + const animations = []; + + for ( let i = 0; i < json.length; i ++ ) { + + const clip = AnimationClip.parse( json[ i ] ); + + animations.push( clip ); + + } + + return animations; + + } + +} + +/** + * Abstract Base class to block based textures loader (dds, pvr, ...) + * + * Sub classes have to implement the parse() method which will be used in load(). + */ + +class CompressedTextureLoader extends Loader { + + constructor( manager ) { + + super( manager ); + + } + + load( url, onLoad, onProgress, onError ) { + + const scope = this; + + const images = []; + + const texture = new CompressedTexture(); + + const loader = new FileLoader( this.manager ); + loader.setPath( this.path ); + loader.setResponseType( 'arraybuffer' ); + loader.setRequestHeader( this.requestHeader ); + loader.setWithCredentials( scope.withCredentials ); + + let loaded = 0; + + function loadTexture( i ) { + + loader.load( url[ i ], function ( buffer ) { + + const texDatas = scope.parse( buffer, true ); + + images[ i ] = { + width: texDatas.width, + height: texDatas.height, + format: texDatas.format, + mipmaps: texDatas.mipmaps + }; + + loaded += 1; + + if ( loaded === 6 ) { + + if ( texDatas.mipmapCount === 1 ) texture.minFilter = LinearFilter; + + texture.image = images; + texture.format = texDatas.format; + texture.needsUpdate = true; + + if ( onLoad ) onLoad( texture ); + + } + + }, onProgress, onError ); + + } + + if ( Array.isArray( url ) ) { + + for ( let i = 0, il = url.length; i < il; ++ i ) { + + loadTexture( i ); + + } + + } else { + + // compressed cubemap texture stored in a single DDS file + + loader.load( url, function ( buffer ) { + + const texDatas = scope.parse( buffer, true ); + + if ( texDatas.isCubemap ) { + + const faces = texDatas.mipmaps.length / texDatas.mipmapCount; + + for ( let f = 0; f < faces; f ++ ) { + + images[ f ] = { mipmaps: [] }; + + for ( let i = 0; i < texDatas.mipmapCount; i ++ ) { + + images[ f ].mipmaps.push( texDatas.mipmaps[ f * texDatas.mipmapCount + i ] ); + images[ f ].format = texDatas.format; + images[ f ].width = texDatas.width; + images[ f ].height = texDatas.height; + + } + + } + + texture.image = images; + + } else { + + texture.image.width = texDatas.width; + texture.image.height = texDatas.height; + texture.mipmaps = texDatas.mipmaps; + + } + + if ( texDatas.mipmapCount === 1 ) { + + texture.minFilter = LinearFilter; + + } + + texture.format = texDatas.format; + texture.needsUpdate = true; + + if ( onLoad ) onLoad( texture ); + + }, onProgress, onError ); + + } + + return texture; + + } + +} + +class ImageLoader extends Loader { + + constructor( manager ) { + + super( manager ); + + } + + load( url, onLoad, onProgress, onError ) { + + if ( this.path !== undefined ) url = this.path + url; + + url = this.manager.resolveURL( url ); + + const scope = this; + + const cached = Cache.get( url ); + + if ( cached !== undefined ) { + + scope.manager.itemStart( url ); + + setTimeout( function () { + + if ( onLoad ) onLoad( cached ); + + scope.manager.itemEnd( url ); + + }, 0 ); + + return cached; + + } + + const image = createElementNS( 'img' ); + + function onImageLoad() { + + removeEventListeners(); + + Cache.add( url, this ); + + if ( onLoad ) onLoad( this ); + + scope.manager.itemEnd( url ); + + } + + function onImageError( event ) { + + removeEventListeners(); + + if ( onError ) onError( event ); + + scope.manager.itemError( url ); + scope.manager.itemEnd( url ); + + } + + function removeEventListeners() { + + image.removeEventListener( 'load', onImageLoad, false ); + image.removeEventListener( 'error', onImageError, false ); + + } + + image.addEventListener( 'load', onImageLoad, false ); + image.addEventListener( 'error', onImageError, false ); + + if ( url.slice( 0, 5 ) !== 'data:' ) { + + if ( this.crossOrigin !== undefined ) image.crossOrigin = this.crossOrigin; + + } + + scope.manager.itemStart( url ); + + image.src = url; + + return image; + + } + +} + +class CubeTextureLoader extends Loader { + + constructor( manager ) { + + super( manager ); + + } + + load( urls, onLoad, onProgress, onError ) { + + const texture = new CubeTexture(); + texture.colorSpace = SRGBColorSpace; + + const loader = new ImageLoader( this.manager ); + loader.setCrossOrigin( this.crossOrigin ); + loader.setPath( this.path ); + + let loaded = 0; + + function loadTexture( i ) { + + loader.load( urls[ i ], function ( image ) { + + texture.images[ i ] = image; + + loaded ++; + + if ( loaded === 6 ) { + + texture.needsUpdate = true; + + if ( onLoad ) onLoad( texture ); + + } + + }, undefined, onError ); + + } + + for ( let i = 0; i < urls.length; ++ i ) { + + loadTexture( i ); + + } + + return texture; + + } + +} + +/** + * Abstract Base class to load generic binary textures formats (rgbe, hdr, ...) + * + * Sub classes have to implement the parse() method which will be used in load(). + */ + +class DataTextureLoader extends Loader { + + constructor( manager ) { + + super( manager ); + + } + + load( url, onLoad, onProgress, onError ) { + + const scope = this; + + const texture = new DataTexture(); + + const loader = new FileLoader( this.manager ); + loader.setResponseType( 'arraybuffer' ); + loader.setRequestHeader( this.requestHeader ); + loader.setPath( this.path ); + loader.setWithCredentials( scope.withCredentials ); + loader.load( url, function ( buffer ) { + + let texData; + + try { + + texData = scope.parse( buffer ); + + } catch ( error ) { + + if ( onError !== undefined ) { + + onError( error ); + + } else { + + console.error( error ); + return; + + } + + } + + if ( texData.image !== undefined ) { + + texture.image = texData.image; + + } else if ( texData.data !== undefined ) { + + texture.image.width = texData.width; + texture.image.height = texData.height; + texture.image.data = texData.data; + + } + + texture.wrapS = texData.wrapS !== undefined ? texData.wrapS : ClampToEdgeWrapping; + texture.wrapT = texData.wrapT !== undefined ? texData.wrapT : ClampToEdgeWrapping; + + texture.magFilter = texData.magFilter !== undefined ? texData.magFilter : LinearFilter; + texture.minFilter = texData.minFilter !== undefined ? texData.minFilter : LinearFilter; + + texture.anisotropy = texData.anisotropy !== undefined ? texData.anisotropy : 1; + + if ( texData.colorSpace !== undefined ) { + + texture.colorSpace = texData.colorSpace; + + } + + if ( texData.flipY !== undefined ) { + + texture.flipY = texData.flipY; + + } + + if ( texData.format !== undefined ) { + + texture.format = texData.format; + + } + + if ( texData.type !== undefined ) { + + texture.type = texData.type; + + } + + if ( texData.mipmaps !== undefined ) { + + texture.mipmaps = texData.mipmaps; + texture.minFilter = LinearMipmapLinearFilter; // presumably... + + } + + if ( texData.mipmapCount === 1 ) { + + texture.minFilter = LinearFilter; + + } + + if ( texData.generateMipmaps !== undefined ) { + + texture.generateMipmaps = texData.generateMipmaps; + + } + + texture.needsUpdate = true; + + if ( onLoad ) onLoad( texture, texData ); + + }, onProgress, onError ); + + + return texture; + + } + +} + +class TextureLoader extends Loader { + + constructor( manager ) { + + super( manager ); + + } + + load( url, onLoad, onProgress, onError ) { + + const texture = new Texture(); + + const loader = new ImageLoader( this.manager ); + loader.setCrossOrigin( this.crossOrigin ); + loader.setPath( this.path ); + + loader.load( url, function ( image ) { + + texture.image = image; + texture.needsUpdate = true; + + if ( onLoad !== undefined ) { + + onLoad( texture ); + + } + + }, onProgress, onError ); + + return texture; + + } + +} + +class Light extends Object3D { + + constructor( color, intensity = 1 ) { + + super(); + + this.isLight = true; + + this.type = 'Light'; + + this.color = new Color( color ); + this.intensity = intensity; + + } + + dispose() { + + // Empty here in base class; some subclasses override. + + } + + copy( source, recursive ) { + + super.copy( source, recursive ); + + this.color.copy( source.color ); + this.intensity = source.intensity; + + return this; + + } + + toJSON( meta ) { + + const data = super.toJSON( meta ); + + data.object.color = this.color.getHex(); + data.object.intensity = this.intensity; + + if ( this.groundColor !== undefined ) data.object.groundColor = this.groundColor.getHex(); + + if ( this.distance !== undefined ) data.object.distance = this.distance; + if ( this.angle !== undefined ) data.object.angle = this.angle; + if ( this.decay !== undefined ) data.object.decay = this.decay; + if ( this.penumbra !== undefined ) data.object.penumbra = this.penumbra; + + if ( this.shadow !== undefined ) data.object.shadow = this.shadow.toJSON(); + + return data; + + } + +} + +class HemisphereLight extends Light { + + constructor( skyColor, groundColor, intensity ) { + + super( skyColor, intensity ); + + this.isHemisphereLight = true; + + this.type = 'HemisphereLight'; + + this.position.copy( Object3D.DEFAULT_UP ); + this.updateMatrix(); + + this.groundColor = new Color( groundColor ); + + } + + copy( source, recursive ) { + + super.copy( source, recursive ); + + this.groundColor.copy( source.groundColor ); + + return this; + + } + +} + +const _projScreenMatrix$1 = /*@__PURE__*/ new Matrix4(); +const _lightPositionWorld$1 = /*@__PURE__*/ new Vector3(); +const _lookTarget$1 = /*@__PURE__*/ new Vector3(); + +class LightShadow { + + constructor( camera ) { + + this.camera = camera; + + this.bias = 0; + this.normalBias = 0; + this.radius = 1; + this.blurSamples = 8; + + this.mapSize = new Vector2( 512, 512 ); + + this.map = null; + this.mapPass = null; + this.matrix = new Matrix4(); + + this.autoUpdate = true; + this.needsUpdate = false; + + this._frustum = new Frustum(); + this._frameExtents = new Vector2( 1, 1 ); + + this._viewportCount = 1; + + this._viewports = [ + + new Vector4( 0, 0, 1, 1 ) + + ]; + + } + + getViewportCount() { + + return this._viewportCount; + + } + + getFrustum() { + + return this._frustum; + + } + + updateMatrices( light ) { + + const shadowCamera = this.camera; + const shadowMatrix = this.matrix; + + _lightPositionWorld$1.setFromMatrixPosition( light.matrixWorld ); + shadowCamera.position.copy( _lightPositionWorld$1 ); + + _lookTarget$1.setFromMatrixPosition( light.target.matrixWorld ); + shadowCamera.lookAt( _lookTarget$1 ); + shadowCamera.updateMatrixWorld(); + + _projScreenMatrix$1.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse ); + this._frustum.setFromProjectionMatrix( _projScreenMatrix$1 ); + + shadowMatrix.set( + 0.5, 0.0, 0.0, 0.5, + 0.0, 0.5, 0.0, 0.5, + 0.0, 0.0, 0.5, 0.5, + 0.0, 0.0, 0.0, 1.0 + ); + + shadowMatrix.multiply( _projScreenMatrix$1 ); + + } + + getViewport( viewportIndex ) { + + return this._viewports[ viewportIndex ]; + + } + + getFrameExtents() { + + return this._frameExtents; + + } + + dispose() { + + if ( this.map ) { + + this.map.dispose(); + + } + + if ( this.mapPass ) { + + this.mapPass.dispose(); + + } + + } + + copy( source ) { + + this.camera = source.camera.clone(); + + this.bias = source.bias; + this.radius = source.radius; + + this.mapSize.copy( source.mapSize ); + + return this; + + } + + clone() { + + return new this.constructor().copy( this ); + + } + + toJSON() { + + const object = {}; + + if ( this.bias !== 0 ) object.bias = this.bias; + if ( this.normalBias !== 0 ) object.normalBias = this.normalBias; + if ( this.radius !== 1 ) object.radius = this.radius; + if ( this.mapSize.x !== 512 || this.mapSize.y !== 512 ) object.mapSize = this.mapSize.toArray(); + + object.camera = this.camera.toJSON( false ).object; + delete object.camera.matrix; + + return object; + + } + +} + +class SpotLightShadow extends LightShadow { + + constructor() { + + super( new PerspectiveCamera( 50, 1, 0.5, 500 ) ); + + this.isSpotLightShadow = true; + + this.focus = 1; + + } + + updateMatrices( light ) { + + const camera = this.camera; + + const fov = RAD2DEG * 2 * light.angle * this.focus; + const aspect = this.mapSize.width / this.mapSize.height; + const far = light.distance || camera.far; + + if ( fov !== camera.fov || aspect !== camera.aspect || far !== camera.far ) { + + camera.fov = fov; + camera.aspect = aspect; + camera.far = far; + camera.updateProjectionMatrix(); + + } + + super.updateMatrices( light ); + + } + + copy( source ) { + + super.copy( source ); + + this.focus = source.focus; + + return this; + + } + +} + +class SpotLight extends Light { + + constructor( color, intensity, distance = 0, angle = Math.PI / 3, penumbra = 0, decay = 2 ) { + + super( color, intensity ); + + this.isSpotLight = true; + + this.type = 'SpotLight'; + + this.position.copy( Object3D.DEFAULT_UP ); + this.updateMatrix(); + + this.target = new Object3D(); + + this.distance = distance; + this.angle = angle; + this.penumbra = penumbra; + this.decay = decay; + + this.map = null; + + this.shadow = new SpotLightShadow(); + + } + + get power() { + + // compute the light's luminous power (in lumens) from its intensity (in candela) + // by convention for a spotlight, luminous power (lm) = π * luminous intensity (cd) + return this.intensity * Math.PI; + + } + + set power( power ) { + + // set the light's intensity (in candela) from the desired luminous power (in lumens) + this.intensity = power / Math.PI; + + } + + dispose() { + + this.shadow.dispose(); + + } + + copy( source, recursive ) { + + super.copy( source, recursive ); + + this.distance = source.distance; + this.angle = source.angle; + this.penumbra = source.penumbra; + this.decay = source.decay; + + this.target = source.target.clone(); + + this.shadow = source.shadow.clone(); + + return this; + + } + +} + +const _projScreenMatrix = /*@__PURE__*/ new Matrix4(); +const _lightPositionWorld = /*@__PURE__*/ new Vector3(); +const _lookTarget = /*@__PURE__*/ new Vector3(); + +class PointLightShadow extends LightShadow { + + constructor() { + + super( new PerspectiveCamera( 90, 1, 0.5, 500 ) ); + + this.isPointLightShadow = true; + + this._frameExtents = new Vector2( 4, 2 ); + + this._viewportCount = 6; + + this._viewports = [ + // These viewports map a cube-map onto a 2D texture with the + // following orientation: + // + // xzXZ + // y Y + // + // X - Positive x direction + // x - Negative x direction + // Y - Positive y direction + // y - Negative y direction + // Z - Positive z direction + // z - Negative z direction + + // positive X + new Vector4( 2, 1, 1, 1 ), + // negative X + new Vector4( 0, 1, 1, 1 ), + // positive Z + new Vector4( 3, 1, 1, 1 ), + // negative Z + new Vector4( 1, 1, 1, 1 ), + // positive Y + new Vector4( 3, 0, 1, 1 ), + // negative Y + new Vector4( 1, 0, 1, 1 ) + ]; + + this._cubeDirections = [ + new Vector3( 1, 0, 0 ), new Vector3( - 1, 0, 0 ), new Vector3( 0, 0, 1 ), + new Vector3( 0, 0, - 1 ), new Vector3( 0, 1, 0 ), new Vector3( 0, - 1, 0 ) + ]; + + this._cubeUps = [ + new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ), + new Vector3( 0, 1, 0 ), new Vector3( 0, 0, 1 ), new Vector3( 0, 0, - 1 ) + ]; + + } + + updateMatrices( light, viewportIndex = 0 ) { + + const camera = this.camera; + const shadowMatrix = this.matrix; + + const far = light.distance || camera.far; + + if ( far !== camera.far ) { + + camera.far = far; + camera.updateProjectionMatrix(); + + } + + _lightPositionWorld.setFromMatrixPosition( light.matrixWorld ); + camera.position.copy( _lightPositionWorld ); + + _lookTarget.copy( camera.position ); + _lookTarget.add( this._cubeDirections[ viewportIndex ] ); + camera.up.copy( this._cubeUps[ viewportIndex ] ); + camera.lookAt( _lookTarget ); + camera.updateMatrixWorld(); + + shadowMatrix.makeTranslation( - _lightPositionWorld.x, - _lightPositionWorld.y, - _lightPositionWorld.z ); + + _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse ); + this._frustum.setFromProjectionMatrix( _projScreenMatrix ); + + } + +} + +class PointLight extends Light { + + constructor( color, intensity, distance = 0, decay = 2 ) { + + super( color, intensity ); + + this.isPointLight = true; + + this.type = 'PointLight'; + + this.distance = distance; + this.decay = decay; + + this.shadow = new PointLightShadow(); + + } + + get power() { + + // compute the light's luminous power (in lumens) from its intensity (in candela) + // for an isotropic light source, luminous power (lm) = 4 π luminous intensity (cd) + return this.intensity * 4 * Math.PI; + + } + + set power( power ) { + + // set the light's intensity (in candela) from the desired luminous power (in lumens) + this.intensity = power / ( 4 * Math.PI ); + + } + + dispose() { + + this.shadow.dispose(); + + } + + copy( source, recursive ) { + + super.copy( source, recursive ); + + this.distance = source.distance; + this.decay = source.decay; + + this.shadow = source.shadow.clone(); + + return this; + + } + +} + +class DirectionalLightShadow extends LightShadow { + + constructor() { + + super( new OrthographicCamera( - 5, 5, 5, - 5, 0.5, 500 ) ); + + this.isDirectionalLightShadow = true; + + } + +} + +class DirectionalLight extends Light { + + constructor( color, intensity ) { + + super( color, intensity ); + + this.isDirectionalLight = true; + + this.type = 'DirectionalLight'; + + this.position.copy( Object3D.DEFAULT_UP ); + this.updateMatrix(); + + this.target = new Object3D(); + + this.shadow = new DirectionalLightShadow(); + + } + + dispose() { + + this.shadow.dispose(); + + } + + copy( source ) { + + super.copy( source ); + + this.target = source.target.clone(); + this.shadow = source.shadow.clone(); + + return this; + + } + +} + +class AmbientLight extends Light { + + constructor( color, intensity ) { + + super( color, intensity ); + + this.isAmbientLight = true; + + this.type = 'AmbientLight'; + + } + +} + +class RectAreaLight extends Light { + + constructor( color, intensity, width = 10, height = 10 ) { + + super( color, intensity ); + + this.isRectAreaLight = true; + + this.type = 'RectAreaLight'; + + this.width = width; + this.height = height; + + } + + get power() { + + // compute the light's luminous power (in lumens) from its intensity (in nits) + return this.intensity * this.width * this.height * Math.PI; + + } + + set power( power ) { + + // set the light's intensity (in nits) from the desired luminous power (in lumens) + this.intensity = power / ( this.width * this.height * Math.PI ); + + } + + copy( source ) { + + super.copy( source ); + + this.width = source.width; + this.height = source.height; + + return this; + + } + + toJSON( meta ) { + + const data = super.toJSON( meta ); + + data.object.width = this.width; + data.object.height = this.height; + + return data; + + } + +} + +/** + * Primary reference: + * https://graphics.stanford.edu/papers/envmap/envmap.pdf + * + * Secondary reference: + * https://www.ppsloan.org/publications/StupidSH36.pdf + */ + +// 3-band SH defined by 9 coefficients + +class SphericalHarmonics3 { + + constructor() { + + this.isSphericalHarmonics3 = true; + + this.coefficients = []; + + for ( let i = 0; i < 9; i ++ ) { + + this.coefficients.push( new Vector3() ); + + } + + } + + set( coefficients ) { + + for ( let i = 0; i < 9; i ++ ) { + + this.coefficients[ i ].copy( coefficients[ i ] ); + + } + + return this; + + } + + zero() { + + for ( let i = 0; i < 9; i ++ ) { + + this.coefficients[ i ].set( 0, 0, 0 ); + + } + + return this; + + } + + // get the radiance in the direction of the normal + // target is a Vector3 + getAt( normal, target ) { + + // normal is assumed to be unit length + + const x = normal.x, y = normal.y, z = normal.z; + + const coeff = this.coefficients; + + // band 0 + target.copy( coeff[ 0 ] ).multiplyScalar( 0.282095 ); + + // band 1 + target.addScaledVector( coeff[ 1 ], 0.488603 * y ); + target.addScaledVector( coeff[ 2 ], 0.488603 * z ); + target.addScaledVector( coeff[ 3 ], 0.488603 * x ); + + // band 2 + target.addScaledVector( coeff[ 4 ], 1.092548 * ( x * y ) ); + target.addScaledVector( coeff[ 5 ], 1.092548 * ( y * z ) ); + target.addScaledVector( coeff[ 6 ], 0.315392 * ( 3.0 * z * z - 1.0 ) ); + target.addScaledVector( coeff[ 7 ], 1.092548 * ( x * z ) ); + target.addScaledVector( coeff[ 8 ], 0.546274 * ( x * x - y * y ) ); + + return target; + + } + + // get the irradiance (radiance convolved with cosine lobe) in the direction of the normal + // target is a Vector3 + // https://graphics.stanford.edu/papers/envmap/envmap.pdf + getIrradianceAt( normal, target ) { + + // normal is assumed to be unit length + + const x = normal.x, y = normal.y, z = normal.z; + + const coeff = this.coefficients; + + // band 0 + target.copy( coeff[ 0 ] ).multiplyScalar( 0.886227 ); // π * 0.282095 + + // band 1 + target.addScaledVector( coeff[ 1 ], 2.0 * 0.511664 * y ); // ( 2 * π / 3 ) * 0.488603 + target.addScaledVector( coeff[ 2 ], 2.0 * 0.511664 * z ); + target.addScaledVector( coeff[ 3 ], 2.0 * 0.511664 * x ); + + // band 2 + target.addScaledVector( coeff[ 4 ], 2.0 * 0.429043 * x * y ); // ( π / 4 ) * 1.092548 + target.addScaledVector( coeff[ 5 ], 2.0 * 0.429043 * y * z ); + target.addScaledVector( coeff[ 6 ], 0.743125 * z * z - 0.247708 ); // ( π / 4 ) * 0.315392 * 3 + target.addScaledVector( coeff[ 7 ], 2.0 * 0.429043 * x * z ); + target.addScaledVector( coeff[ 8 ], 0.429043 * ( x * x - y * y ) ); // ( π / 4 ) * 0.546274 + + return target; + + } + + add( sh ) { + + for ( let i = 0; i < 9; i ++ ) { + + this.coefficients[ i ].add( sh.coefficients[ i ] ); + + } + + return this; + + } + + addScaledSH( sh, s ) { + + for ( let i = 0; i < 9; i ++ ) { + + this.coefficients[ i ].addScaledVector( sh.coefficients[ i ], s ); + + } + + return this; + + } + + scale( s ) { + + for ( let i = 0; i < 9; i ++ ) { + + this.coefficients[ i ].multiplyScalar( s ); + + } + + return this; + + } + + lerp( sh, alpha ) { + + for ( let i = 0; i < 9; i ++ ) { + + this.coefficients[ i ].lerp( sh.coefficients[ i ], alpha ); + + } + + return this; + + } + + equals( sh ) { + + for ( let i = 0; i < 9; i ++ ) { + + if ( ! this.coefficients[ i ].equals( sh.coefficients[ i ] ) ) { + + return false; + + } + + } + + return true; + + } + + copy( sh ) { + + return this.set( sh.coefficients ); + + } + + clone() { + + return new this.constructor().copy( this ); + + } + + fromArray( array, offset = 0 ) { + + const coefficients = this.coefficients; + + for ( let i = 0; i < 9; i ++ ) { + + coefficients[ i ].fromArray( array, offset + ( i * 3 ) ); + + } + + return this; + + } + + toArray( array = [], offset = 0 ) { + + const coefficients = this.coefficients; + + for ( let i = 0; i < 9; i ++ ) { + + coefficients[ i ].toArray( array, offset + ( i * 3 ) ); + + } + + return array; + + } + + // evaluate the basis functions + // shBasis is an Array[ 9 ] + static getBasisAt( normal, shBasis ) { + + // normal is assumed to be unit length + + const x = normal.x, y = normal.y, z = normal.z; + + // band 0 + shBasis[ 0 ] = 0.282095; + + // band 1 + shBasis[ 1 ] = 0.488603 * y; + shBasis[ 2 ] = 0.488603 * z; + shBasis[ 3 ] = 0.488603 * x; + + // band 2 + shBasis[ 4 ] = 1.092548 * x * y; + shBasis[ 5 ] = 1.092548 * y * z; + shBasis[ 6 ] = 0.315392 * ( 3 * z * z - 1 ); + shBasis[ 7 ] = 1.092548 * x * z; + shBasis[ 8 ] = 0.546274 * ( x * x - y * y ); + + } + +} + +class LightProbe extends Light { + + constructor( sh = new SphericalHarmonics3(), intensity = 1 ) { + + super( undefined, intensity ); + + this.isLightProbe = true; + + this.sh = sh; + + } + + copy( source ) { + + super.copy( source ); + + this.sh.copy( source.sh ); + + return this; + + } + + fromJSON( json ) { + + this.intensity = json.intensity; // TODO: Move this bit to Light.fromJSON(); + this.sh.fromArray( json.sh ); + + return this; + + } + + toJSON( meta ) { + + const data = super.toJSON( meta ); + + data.object.sh = this.sh.toArray(); + + return data; + + } + +} + +class MaterialLoader extends Loader { + + constructor( manager ) { + + super( manager ); + this.textures = {}; + + } + + load( url, onLoad, onProgress, onError ) { + + const scope = this; + + const loader = new FileLoader( scope.manager ); + loader.setPath( scope.path ); + loader.setRequestHeader( scope.requestHeader ); + loader.setWithCredentials( scope.withCredentials ); + loader.load( url, function ( text ) { + + try { + + onLoad( scope.parse( JSON.parse( text ) ) ); + + } catch ( e ) { + + if ( onError ) { + + onError( e ); + + } else { + + console.error( e ); + + } + + scope.manager.itemError( url ); + + } + + }, onProgress, onError ); + + } + + parse( json ) { + + const textures = this.textures; + + function getTexture( name ) { + + if ( textures[ name ] === undefined ) { + + console.warn( 'THREE.MaterialLoader: Undefined texture', name ); + + } + + return textures[ name ]; + + } + + const material = MaterialLoader.createMaterialFromType( json.type ); + + if ( json.uuid !== undefined ) material.uuid = json.uuid; + if ( json.name !== undefined ) material.name = json.name; + if ( json.color !== undefined && material.color !== undefined ) material.color.setHex( json.color ); + if ( json.roughness !== undefined ) material.roughness = json.roughness; + if ( json.metalness !== undefined ) material.metalness = json.metalness; + if ( json.sheen !== undefined ) material.sheen = json.sheen; + if ( json.sheenColor !== undefined ) material.sheenColor = new Color().setHex( json.sheenColor ); + if ( json.sheenRoughness !== undefined ) material.sheenRoughness = json.sheenRoughness; + if ( json.emissive !== undefined && material.emissive !== undefined ) material.emissive.setHex( json.emissive ); + if ( json.specular !== undefined && material.specular !== undefined ) material.specular.setHex( json.specular ); + if ( json.specularIntensity !== undefined ) material.specularIntensity = json.specularIntensity; + if ( json.specularColor !== undefined && material.specularColor !== undefined ) material.specularColor.setHex( json.specularColor ); + if ( json.shininess !== undefined ) material.shininess = json.shininess; + if ( json.clearcoat !== undefined ) material.clearcoat = json.clearcoat; + if ( json.clearcoatRoughness !== undefined ) material.clearcoatRoughness = json.clearcoatRoughness; + if ( json.dispersion !== undefined ) material.dispersion = json.dispersion; + if ( json.iridescence !== undefined ) material.iridescence = json.iridescence; + if ( json.iridescenceIOR !== undefined ) material.iridescenceIOR = json.iridescenceIOR; + if ( json.iridescenceThicknessRange !== undefined ) material.iridescenceThicknessRange = json.iridescenceThicknessRange; + if ( json.transmission !== undefined ) material.transmission = json.transmission; + if ( json.thickness !== undefined ) material.thickness = json.thickness; + if ( json.attenuationDistance !== undefined ) material.attenuationDistance = json.attenuationDistance; + if ( json.attenuationColor !== undefined && material.attenuationColor !== undefined ) material.attenuationColor.setHex( json.attenuationColor ); + if ( json.anisotropy !== undefined ) material.anisotropy = json.anisotropy; + if ( json.anisotropyRotation !== undefined ) material.anisotropyRotation = json.anisotropyRotation; + if ( json.fog !== undefined ) material.fog = json.fog; + if ( json.flatShading !== undefined ) material.flatShading = json.flatShading; + if ( json.blending !== undefined ) material.blending = json.blending; + if ( json.combine !== undefined ) material.combine = json.combine; + if ( json.side !== undefined ) material.side = json.side; + if ( json.shadowSide !== undefined ) material.shadowSide = json.shadowSide; + if ( json.opacity !== undefined ) material.opacity = json.opacity; + if ( json.transparent !== undefined ) material.transparent = json.transparent; + if ( json.alphaTest !== undefined ) material.alphaTest = json.alphaTest; + if ( json.alphaHash !== undefined ) material.alphaHash = json.alphaHash; + if ( json.depthFunc !== undefined ) material.depthFunc = json.depthFunc; + if ( json.depthTest !== undefined ) material.depthTest = json.depthTest; + if ( json.depthWrite !== undefined ) material.depthWrite = json.depthWrite; + if ( json.colorWrite !== undefined ) material.colorWrite = json.colorWrite; + if ( json.blendSrc !== undefined ) material.blendSrc = json.blendSrc; + if ( json.blendDst !== undefined ) material.blendDst = json.blendDst; + if ( json.blendEquation !== undefined ) material.blendEquation = json.blendEquation; + if ( json.blendSrcAlpha !== undefined ) material.blendSrcAlpha = json.blendSrcAlpha; + if ( json.blendDstAlpha !== undefined ) material.blendDstAlpha = json.blendDstAlpha; + if ( json.blendEquationAlpha !== undefined ) material.blendEquationAlpha = json.blendEquationAlpha; + if ( json.blendColor !== undefined && material.blendColor !== undefined ) material.blendColor.setHex( json.blendColor ); + if ( json.blendAlpha !== undefined ) material.blendAlpha = json.blendAlpha; + if ( json.stencilWriteMask !== undefined ) material.stencilWriteMask = json.stencilWriteMask; + if ( json.stencilFunc !== undefined ) material.stencilFunc = json.stencilFunc; + if ( json.stencilRef !== undefined ) material.stencilRef = json.stencilRef; + if ( json.stencilFuncMask !== undefined ) material.stencilFuncMask = json.stencilFuncMask; + if ( json.stencilFail !== undefined ) material.stencilFail = json.stencilFail; + if ( json.stencilZFail !== undefined ) material.stencilZFail = json.stencilZFail; + if ( json.stencilZPass !== undefined ) material.stencilZPass = json.stencilZPass; + if ( json.stencilWrite !== undefined ) material.stencilWrite = json.stencilWrite; + + if ( json.wireframe !== undefined ) material.wireframe = json.wireframe; + if ( json.wireframeLinewidth !== undefined ) material.wireframeLinewidth = json.wireframeLinewidth; + if ( json.wireframeLinecap !== undefined ) material.wireframeLinecap = json.wireframeLinecap; + if ( json.wireframeLinejoin !== undefined ) material.wireframeLinejoin = json.wireframeLinejoin; + + if ( json.rotation !== undefined ) material.rotation = json.rotation; + + if ( json.linewidth !== undefined ) material.linewidth = json.linewidth; + if ( json.dashSize !== undefined ) material.dashSize = json.dashSize; + if ( json.gapSize !== undefined ) material.gapSize = json.gapSize; + if ( json.scale !== undefined ) material.scale = json.scale; + + if ( json.polygonOffset !== undefined ) material.polygonOffset = json.polygonOffset; + if ( json.polygonOffsetFactor !== undefined ) material.polygonOffsetFactor = json.polygonOffsetFactor; + if ( json.polygonOffsetUnits !== undefined ) material.polygonOffsetUnits = json.polygonOffsetUnits; + + if ( json.dithering !== undefined ) material.dithering = json.dithering; + + if ( json.alphaToCoverage !== undefined ) material.alphaToCoverage = json.alphaToCoverage; + if ( json.premultipliedAlpha !== undefined ) material.premultipliedAlpha = json.premultipliedAlpha; + if ( json.forceSinglePass !== undefined ) material.forceSinglePass = json.forceSinglePass; + + if ( json.visible !== undefined ) material.visible = json.visible; + + if ( json.toneMapped !== undefined ) material.toneMapped = json.toneMapped; + + if ( json.userData !== undefined ) material.userData = json.userData; + + if ( json.vertexColors !== undefined ) { + + if ( typeof json.vertexColors === 'number' ) { + + material.vertexColors = ( json.vertexColors > 0 ) ? true : false; + + } else { + + material.vertexColors = json.vertexColors; + + } + + } + + // Shader Material + + if ( json.uniforms !== undefined ) { + + for ( const name in json.uniforms ) { + + const uniform = json.uniforms[ name ]; + + material.uniforms[ name ] = {}; + + switch ( uniform.type ) { + + case 't': + material.uniforms[ name ].value = getTexture( uniform.value ); + break; + + case 'c': + material.uniforms[ name ].value = new Color().setHex( uniform.value ); + break; + + case 'v2': + material.uniforms[ name ].value = new Vector2().fromArray( uniform.value ); + break; + + case 'v3': + material.uniforms[ name ].value = new Vector3().fromArray( uniform.value ); + break; + + case 'v4': + material.uniforms[ name ].value = new Vector4().fromArray( uniform.value ); + break; + + case 'm3': + material.uniforms[ name ].value = new Matrix3().fromArray( uniform.value ); + break; + + case 'm4': + material.uniforms[ name ].value = new Matrix4().fromArray( uniform.value ); + break; + + default: + material.uniforms[ name ].value = uniform.value; + + } + + } + + } + + if ( json.defines !== undefined ) material.defines = json.defines; + if ( json.vertexShader !== undefined ) material.vertexShader = json.vertexShader; + if ( json.fragmentShader !== undefined ) material.fragmentShader = json.fragmentShader; + if ( json.glslVersion !== undefined ) material.glslVersion = json.glslVersion; + + if ( json.extensions !== undefined ) { + + for ( const key in json.extensions ) { + + material.extensions[ key ] = json.extensions[ key ]; + + } + + } + + if ( json.lights !== undefined ) material.lights = json.lights; + if ( json.clipping !== undefined ) material.clipping = json.clipping; + + // for PointsMaterial + + if ( json.size !== undefined ) material.size = json.size; + if ( json.sizeAttenuation !== undefined ) material.sizeAttenuation = json.sizeAttenuation; + + // maps + + if ( json.map !== undefined ) material.map = getTexture( json.map ); + if ( json.matcap !== undefined ) material.matcap = getTexture( json.matcap ); + + if ( json.alphaMap !== undefined ) material.alphaMap = getTexture( json.alphaMap ); + + if ( json.bumpMap !== undefined ) material.bumpMap = getTexture( json.bumpMap ); + if ( json.bumpScale !== undefined ) material.bumpScale = json.bumpScale; + + if ( json.normalMap !== undefined ) material.normalMap = getTexture( json.normalMap ); + if ( json.normalMapType !== undefined ) material.normalMapType = json.normalMapType; + if ( json.normalScale !== undefined ) { + + let normalScale = json.normalScale; + + if ( Array.isArray( normalScale ) === false ) { + + // Blender exporter used to export a scalar. See #7459 + + normalScale = [ normalScale, normalScale ]; + + } + + material.normalScale = new Vector2().fromArray( normalScale ); + + } + + if ( json.displacementMap !== undefined ) material.displacementMap = getTexture( json.displacementMap ); + if ( json.displacementScale !== undefined ) material.displacementScale = json.displacementScale; + if ( json.displacementBias !== undefined ) material.displacementBias = json.displacementBias; + + if ( json.roughnessMap !== undefined ) material.roughnessMap = getTexture( json.roughnessMap ); + if ( json.metalnessMap !== undefined ) material.metalnessMap = getTexture( json.metalnessMap ); + + if ( json.emissiveMap !== undefined ) material.emissiveMap = getTexture( json.emissiveMap ); + if ( json.emissiveIntensity !== undefined ) material.emissiveIntensity = json.emissiveIntensity; + + if ( json.specularMap !== undefined ) material.specularMap = getTexture( json.specularMap ); + if ( json.specularIntensityMap !== undefined ) material.specularIntensityMap = getTexture( json.specularIntensityMap ); + if ( json.specularColorMap !== undefined ) material.specularColorMap = getTexture( json.specularColorMap ); + + if ( json.envMap !== undefined ) material.envMap = getTexture( json.envMap ); + if ( json.envMapRotation !== undefined ) material.envMapRotation.fromArray( json.envMapRotation ); + if ( json.envMapIntensity !== undefined ) material.envMapIntensity = json.envMapIntensity; + + if ( json.reflectivity !== undefined ) material.reflectivity = json.reflectivity; + if ( json.refractionRatio !== undefined ) material.refractionRatio = json.refractionRatio; + + if ( json.lightMap !== undefined ) material.lightMap = getTexture( json.lightMap ); + if ( json.lightMapIntensity !== undefined ) material.lightMapIntensity = json.lightMapIntensity; + + if ( json.aoMap !== undefined ) material.aoMap = getTexture( json.aoMap ); + if ( json.aoMapIntensity !== undefined ) material.aoMapIntensity = json.aoMapIntensity; + + if ( json.gradientMap !== undefined ) material.gradientMap = getTexture( json.gradientMap ); + + if ( json.clearcoatMap !== undefined ) material.clearcoatMap = getTexture( json.clearcoatMap ); + if ( json.clearcoatRoughnessMap !== undefined ) material.clearcoatRoughnessMap = getTexture( json.clearcoatRoughnessMap ); + if ( json.clearcoatNormalMap !== undefined ) material.clearcoatNormalMap = getTexture( json.clearcoatNormalMap ); + if ( json.clearcoatNormalScale !== undefined ) material.clearcoatNormalScale = new Vector2().fromArray( json.clearcoatNormalScale ); + + if ( json.iridescenceMap !== undefined ) material.iridescenceMap = getTexture( json.iridescenceMap ); + if ( json.iridescenceThicknessMap !== undefined ) material.iridescenceThicknessMap = getTexture( json.iridescenceThicknessMap ); + + if ( json.transmissionMap !== undefined ) material.transmissionMap = getTexture( json.transmissionMap ); + if ( json.thicknessMap !== undefined ) material.thicknessMap = getTexture( json.thicknessMap ); + + if ( json.anisotropyMap !== undefined ) material.anisotropyMap = getTexture( json.anisotropyMap ); + + if ( json.sheenColorMap !== undefined ) material.sheenColorMap = getTexture( json.sheenColorMap ); + if ( json.sheenRoughnessMap !== undefined ) material.sheenRoughnessMap = getTexture( json.sheenRoughnessMap ); + + return material; + + } + + setTextures( value ) { + + this.textures = value; + return this; + + } + + static createMaterialFromType( type ) { + + const materialLib = { + ShadowMaterial, + SpriteMaterial, + RawShaderMaterial, + ShaderMaterial, + PointsMaterial, + MeshPhysicalMaterial, + MeshStandardMaterial, + MeshPhongMaterial, + MeshToonMaterial, + MeshNormalMaterial, + MeshLambertMaterial, + MeshDepthMaterial, + MeshDistanceMaterial, + MeshBasicMaterial, + MeshMatcapMaterial, + LineDashedMaterial, + LineBasicMaterial, + Material + }; + + return new materialLib[ type ](); + + } + +} + +class LoaderUtils { + + static decodeText( array ) { + + if ( typeof TextDecoder !== 'undefined' ) { + + return new TextDecoder().decode( array ); + + } + + // Avoid the String.fromCharCode.apply(null, array) shortcut, which + // throws a "maximum call stack size exceeded" error for large arrays. + + let s = ''; + + for ( let i = 0, il = array.length; i < il; i ++ ) { + + // Implicitly assumes little-endian. + s += String.fromCharCode( array[ i ] ); + + } + + try { + + // merges multi-byte utf-8 characters. + + return decodeURIComponent( escape( s ) ); + + } catch ( e ) { // see #16358 + + return s; + + } + + } + + static extractUrlBase( url ) { + + const index = url.lastIndexOf( '/' ); + + if ( index === - 1 ) return './'; + + return url.slice( 0, index + 1 ); + + } + + static resolveURL( url, path ) { + + // Invalid URL + if ( typeof url !== 'string' || url === '' ) return ''; + + // Host Relative URL + if ( /^https?:\/\//i.test( path ) && /^\//.test( url ) ) { + + path = path.replace( /(^https?:\/\/[^\/]+).*/i, '$1' ); + + } + + // Absolute URL http://,https://,// + if ( /^(https?:)?\/\//i.test( url ) ) return url; + + // Data URI + if ( /^data:.*,.*$/i.test( url ) ) return url; + + // Blob URL + if ( /^blob:.*$/i.test( url ) ) return url; + + // Relative URL + return path + url; + + } + +} + +class InstancedBufferGeometry extends BufferGeometry { + + constructor() { + + super(); + + this.isInstancedBufferGeometry = true; + + this.type = 'InstancedBufferGeometry'; + this.instanceCount = Infinity; + + } + + copy( source ) { + + super.copy( source ); + + this.instanceCount = source.instanceCount; + + return this; + + } + + toJSON() { + + const data = super.toJSON(); + + data.instanceCount = this.instanceCount; + + data.isInstancedBufferGeometry = true; + + return data; + + } + +} + +class BufferGeometryLoader extends Loader { + + constructor( manager ) { + + super( manager ); + + } + + load( url, onLoad, onProgress, onError ) { + + const scope = this; + + const loader = new FileLoader( scope.manager ); + loader.setPath( scope.path ); + loader.setRequestHeader( scope.requestHeader ); + loader.setWithCredentials( scope.withCredentials ); + loader.load( url, function ( text ) { + + try { + + onLoad( scope.parse( JSON.parse( text ) ) ); + + } catch ( e ) { + + if ( onError ) { + + onError( e ); + + } else { + + console.error( e ); + + } + + scope.manager.itemError( url ); + + } + + }, onProgress, onError ); + + } + + parse( json ) { + + const interleavedBufferMap = {}; + const arrayBufferMap = {}; + + function getInterleavedBuffer( json, uuid ) { + + if ( interleavedBufferMap[ uuid ] !== undefined ) return interleavedBufferMap[ uuid ]; + + const interleavedBuffers = json.interleavedBuffers; + const interleavedBuffer = interleavedBuffers[ uuid ]; + + const buffer = getArrayBuffer( json, interleavedBuffer.buffer ); + + const array = getTypedArray( interleavedBuffer.type, buffer ); + const ib = new InterleavedBuffer( array, interleavedBuffer.stride ); + ib.uuid = interleavedBuffer.uuid; + + interleavedBufferMap[ uuid ] = ib; + + return ib; + + } + + function getArrayBuffer( json, uuid ) { + + if ( arrayBufferMap[ uuid ] !== undefined ) return arrayBufferMap[ uuid ]; + + const arrayBuffers = json.arrayBuffers; + const arrayBuffer = arrayBuffers[ uuid ]; + + const ab = new Uint32Array( arrayBuffer ).buffer; + + arrayBufferMap[ uuid ] = ab; + + return ab; + + } + + const geometry = json.isInstancedBufferGeometry ? new InstancedBufferGeometry() : new BufferGeometry(); + + const index = json.data.index; + + if ( index !== undefined ) { + + const typedArray = getTypedArray( index.type, index.array ); + geometry.setIndex( new BufferAttribute( typedArray, 1 ) ); + + } + + const attributes = json.data.attributes; + + for ( const key in attributes ) { + + const attribute = attributes[ key ]; + let bufferAttribute; + + if ( attribute.isInterleavedBufferAttribute ) { + + const interleavedBuffer = getInterleavedBuffer( json.data, attribute.data ); + bufferAttribute = new InterleavedBufferAttribute( interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized ); + + } else { + + const typedArray = getTypedArray( attribute.type, attribute.array ); + const bufferAttributeConstr = attribute.isInstancedBufferAttribute ? InstancedBufferAttribute : BufferAttribute; + bufferAttribute = new bufferAttributeConstr( typedArray, attribute.itemSize, attribute.normalized ); + + } + + if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name; + if ( attribute.usage !== undefined ) bufferAttribute.setUsage( attribute.usage ); + + geometry.setAttribute( key, bufferAttribute ); + + } + + const morphAttributes = json.data.morphAttributes; + + if ( morphAttributes ) { + + for ( const key in morphAttributes ) { + + const attributeArray = morphAttributes[ key ]; + + const array = []; + + for ( let i = 0, il = attributeArray.length; i < il; i ++ ) { + + const attribute = attributeArray[ i ]; + let bufferAttribute; + + if ( attribute.isInterleavedBufferAttribute ) { + + const interleavedBuffer = getInterleavedBuffer( json.data, attribute.data ); + bufferAttribute = new InterleavedBufferAttribute( interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized ); + + } else { + + const typedArray = getTypedArray( attribute.type, attribute.array ); + bufferAttribute = new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized ); + + } + + if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name; + array.push( bufferAttribute ); + + } + + geometry.morphAttributes[ key ] = array; + + } + + } + + const morphTargetsRelative = json.data.morphTargetsRelative; + + if ( morphTargetsRelative ) { + + geometry.morphTargetsRelative = true; + + } + + const groups = json.data.groups || json.data.drawcalls || json.data.offsets; + + if ( groups !== undefined ) { + + for ( let i = 0, n = groups.length; i !== n; ++ i ) { + + const group = groups[ i ]; + + geometry.addGroup( group.start, group.count, group.materialIndex ); + + } + + } + + const boundingSphere = json.data.boundingSphere; + + if ( boundingSphere !== undefined ) { + + const center = new Vector3(); + + if ( boundingSphere.center !== undefined ) { + + center.fromArray( boundingSphere.center ); + + } + + geometry.boundingSphere = new Sphere( center, boundingSphere.radius ); + + } + + if ( json.name ) geometry.name = json.name; + if ( json.userData ) geometry.userData = json.userData; + + return geometry; + + } + +} + +class ObjectLoader extends Loader { + + constructor( manager ) { + + super( manager ); + + } + + load( url, onLoad, onProgress, onError ) { + + const scope = this; + + const path = ( this.path === '' ) ? LoaderUtils.extractUrlBase( url ) : this.path; + this.resourcePath = this.resourcePath || path; + + const loader = new FileLoader( this.manager ); + loader.setPath( this.path ); + loader.setRequestHeader( this.requestHeader ); + loader.setWithCredentials( this.withCredentials ); + loader.load( url, function ( text ) { + + let json = null; + + try { + + json = JSON.parse( text ); + + } catch ( error ) { + + if ( onError !== undefined ) onError( error ); + + console.error( 'THREE:ObjectLoader: Can\'t parse ' + url + '.', error.message ); + + return; + + } + + const metadata = json.metadata; + + if ( metadata === undefined || metadata.type === undefined || metadata.type.toLowerCase() === 'geometry' ) { + + if ( onError !== undefined ) onError( new Error( 'THREE.ObjectLoader: Can\'t load ' + url ) ); + + console.error( 'THREE.ObjectLoader: Can\'t load ' + url ); + return; + + } + + scope.parse( json, onLoad ); + + }, onProgress, onError ); + + } + + async loadAsync( url, onProgress ) { + + const scope = this; + + const path = ( this.path === '' ) ? LoaderUtils.extractUrlBase( url ) : this.path; + this.resourcePath = this.resourcePath || path; + + const loader = new FileLoader( this.manager ); + loader.setPath( this.path ); + loader.setRequestHeader( this.requestHeader ); + loader.setWithCredentials( this.withCredentials ); + + const text = await loader.loadAsync( url, onProgress ); + + const json = JSON.parse( text ); + + const metadata = json.metadata; + + if ( metadata === undefined || metadata.type === undefined || metadata.type.toLowerCase() === 'geometry' ) { + + throw new Error( 'THREE.ObjectLoader: Can\'t load ' + url ); + + } + + return await scope.parseAsync( json ); + + } + + parse( json, onLoad ) { + + const animations = this.parseAnimations( json.animations ); + const shapes = this.parseShapes( json.shapes ); + const geometries = this.parseGeometries( json.geometries, shapes ); + + const images = this.parseImages( json.images, function () { + + if ( onLoad !== undefined ) onLoad( object ); + + } ); + + const textures = this.parseTextures( json.textures, images ); + const materials = this.parseMaterials( json.materials, textures ); + + const object = this.parseObject( json.object, geometries, materials, textures, animations ); + const skeletons = this.parseSkeletons( json.skeletons, object ); + + this.bindSkeletons( object, skeletons ); + + // + + if ( onLoad !== undefined ) { + + let hasImages = false; + + for ( const uuid in images ) { + + if ( images[ uuid ].data instanceof HTMLImageElement ) { + + hasImages = true; + break; + + } + + } + + if ( hasImages === false ) onLoad( object ); + + } + + return object; + + } + + async parseAsync( json ) { + + const animations = this.parseAnimations( json.animations ); + const shapes = this.parseShapes( json.shapes ); + const geometries = this.parseGeometries( json.geometries, shapes ); + + const images = await this.parseImagesAsync( json.images ); + + const textures = this.parseTextures( json.textures, images ); + const materials = this.parseMaterials( json.materials, textures ); + + const object = this.parseObject( json.object, geometries, materials, textures, animations ); + const skeletons = this.parseSkeletons( json.skeletons, object ); + + this.bindSkeletons( object, skeletons ); + + return object; + + } + + parseShapes( json ) { + + const shapes = {}; + + if ( json !== undefined ) { + + for ( let i = 0, l = json.length; i < l; i ++ ) { + + const shape = new Shape().fromJSON( json[ i ] ); + + shapes[ shape.uuid ] = shape; + + } + + } + + return shapes; + + } + + parseSkeletons( json, object ) { + + const skeletons = {}; + const bones = {}; + + // generate bone lookup table + + object.traverse( function ( child ) { + + if ( child.isBone ) bones[ child.uuid ] = child; + + } ); + + // create skeletons + + if ( json !== undefined ) { + + for ( let i = 0, l = json.length; i < l; i ++ ) { + + const skeleton = new Skeleton().fromJSON( json[ i ], bones ); + + skeletons[ skeleton.uuid ] = skeleton; + + } + + } + + return skeletons; + + } + + parseGeometries( json, shapes ) { + + const geometries = {}; + + if ( json !== undefined ) { + + const bufferGeometryLoader = new BufferGeometryLoader(); + + for ( let i = 0, l = json.length; i < l; i ++ ) { + + let geometry; + const data = json[ i ]; + + switch ( data.type ) { + + case 'BufferGeometry': + case 'InstancedBufferGeometry': + + geometry = bufferGeometryLoader.parse( data ); + break; + + default: + + if ( data.type in Geometries ) { + + geometry = Geometries[ data.type ].fromJSON( data, shapes ); + + } else { + + console.warn( `THREE.ObjectLoader: Unsupported geometry type "${ data.type }"` ); + + } + + } + + geometry.uuid = data.uuid; + + if ( data.name !== undefined ) geometry.name = data.name; + if ( data.userData !== undefined ) geometry.userData = data.userData; + + geometries[ data.uuid ] = geometry; + + } + + } + + return geometries; + + } + + parseMaterials( json, textures ) { + + const cache = {}; // MultiMaterial + const materials = {}; + + if ( json !== undefined ) { + + const loader = new MaterialLoader(); + loader.setTextures( textures ); + + for ( let i = 0, l = json.length; i < l; i ++ ) { + + const data = json[ i ]; + + if ( cache[ data.uuid ] === undefined ) { + + cache[ data.uuid ] = loader.parse( data ); + + } + + materials[ data.uuid ] = cache[ data.uuid ]; + + } + + } + + return materials; + + } + + parseAnimations( json ) { + + const animations = {}; + + if ( json !== undefined ) { + + for ( let i = 0; i < json.length; i ++ ) { + + const data = json[ i ]; + + const clip = AnimationClip.parse( data ); + + animations[ clip.uuid ] = clip; + + } + + } + + return animations; + + } + + parseImages( json, onLoad ) { + + const scope = this; + const images = {}; + + let loader; + + function loadImage( url ) { + + scope.manager.itemStart( url ); + + return loader.load( url, function () { + + scope.manager.itemEnd( url ); + + }, undefined, function () { + + scope.manager.itemError( url ); + scope.manager.itemEnd( url ); + + } ); + + } + + function deserializeImage( image ) { + + if ( typeof image === 'string' ) { + + const url = image; + + const path = /^(\/\/)|([a-z]+:(\/\/)?)/i.test( url ) ? url : scope.resourcePath + url; + + return loadImage( path ); + + } else { + + if ( image.data ) { + + return { + data: getTypedArray( image.type, image.data ), + width: image.width, + height: image.height + }; + + } else { + + return null; + + } + + } + + } + + if ( json !== undefined && json.length > 0 ) { + + const manager = new LoadingManager( onLoad ); + + loader = new ImageLoader( manager ); + loader.setCrossOrigin( this.crossOrigin ); + + for ( let i = 0, il = json.length; i < il; i ++ ) { + + const image = json[ i ]; + const url = image.url; + + if ( Array.isArray( url ) ) { + + // load array of images e.g CubeTexture + + const imageArray = []; + + for ( let j = 0, jl = url.length; j < jl; j ++ ) { + + const currentUrl = url[ j ]; + + const deserializedImage = deserializeImage( currentUrl ); + + if ( deserializedImage !== null ) { + + if ( deserializedImage instanceof HTMLImageElement ) { + + imageArray.push( deserializedImage ); + + } else { + + // special case: handle array of data textures for cube textures + + imageArray.push( new DataTexture( deserializedImage.data, deserializedImage.width, deserializedImage.height ) ); + + } + + } + + } + + images[ image.uuid ] = new Source( imageArray ); + + } else { + + // load single image + + const deserializedImage = deserializeImage( image.url ); + images[ image.uuid ] = new Source( deserializedImage ); + + + } + + } + + } + + return images; + + } + + async parseImagesAsync( json ) { + + const scope = this; + const images = {}; + + let loader; + + async function deserializeImage( image ) { + + if ( typeof image === 'string' ) { + + const url = image; + + const path = /^(\/\/)|([a-z]+:(\/\/)?)/i.test( url ) ? url : scope.resourcePath + url; + + return await loader.loadAsync( path ); + + } else { + + if ( image.data ) { + + return { + data: getTypedArray( image.type, image.data ), + width: image.width, + height: image.height + }; + + } else { + + return null; + + } + + } + + } + + if ( json !== undefined && json.length > 0 ) { + + loader = new ImageLoader( this.manager ); + loader.setCrossOrigin( this.crossOrigin ); + + for ( let i = 0, il = json.length; i < il; i ++ ) { + + const image = json[ i ]; + const url = image.url; + + if ( Array.isArray( url ) ) { + + // load array of images e.g CubeTexture + + const imageArray = []; + + for ( let j = 0, jl = url.length; j < jl; j ++ ) { + + const currentUrl = url[ j ]; + + const deserializedImage = await deserializeImage( currentUrl ); + + if ( deserializedImage !== null ) { + + if ( deserializedImage instanceof HTMLImageElement ) { + + imageArray.push( deserializedImage ); + + } else { + + // special case: handle array of data textures for cube textures + + imageArray.push( new DataTexture( deserializedImage.data, deserializedImage.width, deserializedImage.height ) ); + + } + + } + + } + + images[ image.uuid ] = new Source( imageArray ); + + } else { + + // load single image + + const deserializedImage = await deserializeImage( image.url ); + images[ image.uuid ] = new Source( deserializedImage ); + + } + + } + + } + + return images; + + } + + parseTextures( json, images ) { + + function parseConstant( value, type ) { + + if ( typeof value === 'number' ) return value; + + console.warn( 'THREE.ObjectLoader.parseTexture: Constant should be in numeric form.', value ); + + return type[ value ]; + + } + + const textures = {}; + + if ( json !== undefined ) { + + for ( let i = 0, l = json.length; i < l; i ++ ) { + + const data = json[ i ]; + + if ( data.image === undefined ) { + + console.warn( 'THREE.ObjectLoader: No "image" specified for', data.uuid ); + + } + + if ( images[ data.image ] === undefined ) { + + console.warn( 'THREE.ObjectLoader: Undefined image', data.image ); + + } + + const source = images[ data.image ]; + const image = source.data; + + let texture; + + if ( Array.isArray( image ) ) { + + texture = new CubeTexture(); + + if ( image.length === 6 ) texture.needsUpdate = true; + + } else { + + if ( image && image.data ) { + + texture = new DataTexture(); + + } else { + + texture = new Texture(); + + } + + if ( image ) texture.needsUpdate = true; // textures can have undefined image data + + } + + texture.source = source; + + texture.uuid = data.uuid; + + if ( data.name !== undefined ) texture.name = data.name; + + if ( data.mapping !== undefined ) texture.mapping = parseConstant( data.mapping, TEXTURE_MAPPING ); + if ( data.channel !== undefined ) texture.channel = data.channel; + + if ( data.offset !== undefined ) texture.offset.fromArray( data.offset ); + if ( data.repeat !== undefined ) texture.repeat.fromArray( data.repeat ); + if ( data.center !== undefined ) texture.center.fromArray( data.center ); + if ( data.rotation !== undefined ) texture.rotation = data.rotation; + + if ( data.wrap !== undefined ) { + + texture.wrapS = parseConstant( data.wrap[ 0 ], TEXTURE_WRAPPING ); + texture.wrapT = parseConstant( data.wrap[ 1 ], TEXTURE_WRAPPING ); + + } + + if ( data.format !== undefined ) texture.format = data.format; + if ( data.internalFormat !== undefined ) texture.internalFormat = data.internalFormat; + if ( data.type !== undefined ) texture.type = data.type; + if ( data.colorSpace !== undefined ) texture.colorSpace = data.colorSpace; + + if ( data.minFilter !== undefined ) texture.minFilter = parseConstant( data.minFilter, TEXTURE_FILTER ); + if ( data.magFilter !== undefined ) texture.magFilter = parseConstant( data.magFilter, TEXTURE_FILTER ); + if ( data.anisotropy !== undefined ) texture.anisotropy = data.anisotropy; + + if ( data.flipY !== undefined ) texture.flipY = data.flipY; + + if ( data.generateMipmaps !== undefined ) texture.generateMipmaps = data.generateMipmaps; + if ( data.premultiplyAlpha !== undefined ) texture.premultiplyAlpha = data.premultiplyAlpha; + if ( data.unpackAlignment !== undefined ) texture.unpackAlignment = data.unpackAlignment; + if ( data.compareFunction !== undefined ) texture.compareFunction = data.compareFunction; + + if ( data.userData !== undefined ) texture.userData = data.userData; + + textures[ data.uuid ] = texture; + + } + + } + + return textures; + + } + + parseObject( data, geometries, materials, textures, animations ) { + + let object; + + function getGeometry( name ) { + + if ( geometries[ name ] === undefined ) { + + console.warn( 'THREE.ObjectLoader: Undefined geometry', name ); + + } + + return geometries[ name ]; + + } + + function getMaterial( name ) { + + if ( name === undefined ) return undefined; + + if ( Array.isArray( name ) ) { + + const array = []; + + for ( let i = 0, l = name.length; i < l; i ++ ) { + + const uuid = name[ i ]; + + if ( materials[ uuid ] === undefined ) { + + console.warn( 'THREE.ObjectLoader: Undefined material', uuid ); + + } + + array.push( materials[ uuid ] ); + + } + + return array; + + } + + if ( materials[ name ] === undefined ) { + + console.warn( 'THREE.ObjectLoader: Undefined material', name ); + + } + + return materials[ name ]; + + } + + function getTexture( uuid ) { + + if ( textures[ uuid ] === undefined ) { + + console.warn( 'THREE.ObjectLoader: Undefined texture', uuid ); + + } + + return textures[ uuid ]; + + } + + let geometry, material; + + switch ( data.type ) { + + case 'Scene': + + object = new Scene(); + + if ( data.background !== undefined ) { + + if ( Number.isInteger( data.background ) ) { + + object.background = new Color( data.background ); + + } else { + + object.background = getTexture( data.background ); + + } + + } + + if ( data.environment !== undefined ) { + + object.environment = getTexture( data.environment ); + + } + + if ( data.fog !== undefined ) { + + if ( data.fog.type === 'Fog' ) { + + object.fog = new Fog( data.fog.color, data.fog.near, data.fog.far ); + + } else if ( data.fog.type === 'FogExp2' ) { + + object.fog = new FogExp2( data.fog.color, data.fog.density ); + + } + + if ( data.fog.name !== '' ) { + + object.fog.name = data.fog.name; + + } + + } + + if ( data.backgroundBlurriness !== undefined ) object.backgroundBlurriness = data.backgroundBlurriness; + if ( data.backgroundIntensity !== undefined ) object.backgroundIntensity = data.backgroundIntensity; + if ( data.backgroundRotation !== undefined ) object.backgroundRotation.fromArray( data.backgroundRotation ); + + if ( data.environmentIntensity !== undefined ) object.environmentIntensity = data.environmentIntensity; + if ( data.environmentRotation !== undefined ) object.environmentRotation.fromArray( data.environmentRotation ); + + break; + + case 'PerspectiveCamera': + + object = new PerspectiveCamera( data.fov, data.aspect, data.near, data.far ); + + if ( data.focus !== undefined ) object.focus = data.focus; + if ( data.zoom !== undefined ) object.zoom = data.zoom; + if ( data.filmGauge !== undefined ) object.filmGauge = data.filmGauge; + if ( data.filmOffset !== undefined ) object.filmOffset = data.filmOffset; + if ( data.view !== undefined ) object.view = Object.assign( {}, data.view ); + + break; + + case 'OrthographicCamera': + + object = new OrthographicCamera( data.left, data.right, data.top, data.bottom, data.near, data.far ); + + if ( data.zoom !== undefined ) object.zoom = data.zoom; + if ( data.view !== undefined ) object.view = Object.assign( {}, data.view ); + + break; + + case 'AmbientLight': + + object = new AmbientLight( data.color, data.intensity ); + + break; + + case 'DirectionalLight': + + object = new DirectionalLight( data.color, data.intensity ); + + break; + + case 'PointLight': + + object = new PointLight( data.color, data.intensity, data.distance, data.decay ); + + break; + + case 'RectAreaLight': + + object = new RectAreaLight( data.color, data.intensity, data.width, data.height ); + + break; + + case 'SpotLight': + + object = new SpotLight( data.color, data.intensity, data.distance, data.angle, data.penumbra, data.decay ); + + break; + + case 'HemisphereLight': + + object = new HemisphereLight( data.color, data.groundColor, data.intensity ); + + break; + + case 'LightProbe': + + object = new LightProbe().fromJSON( data ); + + break; + + case 'SkinnedMesh': + + geometry = getGeometry( data.geometry ); + material = getMaterial( data.material ); + + object = new SkinnedMesh( geometry, material ); + + if ( data.bindMode !== undefined ) object.bindMode = data.bindMode; + if ( data.bindMatrix !== undefined ) object.bindMatrix.fromArray( data.bindMatrix ); + if ( data.skeleton !== undefined ) object.skeleton = data.skeleton; + + break; + + case 'Mesh': + + geometry = getGeometry( data.geometry ); + material = getMaterial( data.material ); + + object = new Mesh( geometry, material ); + + break; + + case 'InstancedMesh': + + geometry = getGeometry( data.geometry ); + material = getMaterial( data.material ); + const count = data.count; + const instanceMatrix = data.instanceMatrix; + const instanceColor = data.instanceColor; + + object = new InstancedMesh( geometry, material, count ); + object.instanceMatrix = new InstancedBufferAttribute( new Float32Array( instanceMatrix.array ), 16 ); + if ( instanceColor !== undefined ) object.instanceColor = new InstancedBufferAttribute( new Float32Array( instanceColor.array ), instanceColor.itemSize ); + + break; + + case 'BatchedMesh': + + geometry = getGeometry( data.geometry ); + material = getMaterial( data.material ); + + object = new BatchedMesh( data.maxGeometryCount, data.maxVertexCount, data.maxIndexCount, material ); + object.geometry = geometry; + object.perObjectFrustumCulled = data.perObjectFrustumCulled; + object.sortObjects = data.sortObjects; + + object._drawRanges = data.drawRanges; + object._reservedRanges = data.reservedRanges; + + object._visibility = data.visibility; + object._active = data.active; + object._bounds = data.bounds.map( bound => { + + const box = new Box3(); + box.min.fromArray( bound.boxMin ); + box.max.fromArray( bound.boxMax ); + + const sphere = new Sphere(); + sphere.radius = bound.sphereRadius; + sphere.center.fromArray( bound.sphereCenter ); + + return { + boxInitialized: bound.boxInitialized, + box: box, + + sphereInitialized: bound.sphereInitialized, + sphere: sphere + }; + + } ); + + object._maxGeometryCount = data.maxGeometryCount; + object._maxVertexCount = data.maxVertexCount; + object._maxIndexCount = data.maxIndexCount; + + object._geometryInitialized = data.geometryInitialized; + object._geometryCount = data.geometryCount; + + object._matricesTexture = getTexture( data.matricesTexture.uuid ); + + break; + + case 'LOD': + + object = new LOD(); + + break; + + case 'Line': + + object = new Line( getGeometry( data.geometry ), getMaterial( data.material ) ); + + break; + + case 'LineLoop': + + object = new LineLoop( getGeometry( data.geometry ), getMaterial( data.material ) ); + + break; + + case 'LineSegments': + + object = new LineSegments( getGeometry( data.geometry ), getMaterial( data.material ) ); + + break; + + case 'PointCloud': + case 'Points': + + object = new Points( getGeometry( data.geometry ), getMaterial( data.material ) ); + + break; + + case 'Sprite': + + object = new Sprite( getMaterial( data.material ) ); + + break; + + case 'Group': + + object = new Group(); + + break; + + case 'Bone': + + object = new Bone(); + + break; + + default: + + object = new Object3D(); + + } + + object.uuid = data.uuid; + + if ( data.name !== undefined ) object.name = data.name; + + if ( data.matrix !== undefined ) { + + object.matrix.fromArray( data.matrix ); + + if ( data.matrixAutoUpdate !== undefined ) object.matrixAutoUpdate = data.matrixAutoUpdate; + if ( object.matrixAutoUpdate ) object.matrix.decompose( object.position, object.quaternion, object.scale ); + + } else { + + if ( data.position !== undefined ) object.position.fromArray( data.position ); + if ( data.rotation !== undefined ) object.rotation.fromArray( data.rotation ); + if ( data.quaternion !== undefined ) object.quaternion.fromArray( data.quaternion ); + if ( data.scale !== undefined ) object.scale.fromArray( data.scale ); + + } + + if ( data.up !== undefined ) object.up.fromArray( data.up ); + + if ( data.castShadow !== undefined ) object.castShadow = data.castShadow; + if ( data.receiveShadow !== undefined ) object.receiveShadow = data.receiveShadow; + + if ( data.shadow ) { + + if ( data.shadow.bias !== undefined ) object.shadow.bias = data.shadow.bias; + if ( data.shadow.normalBias !== undefined ) object.shadow.normalBias = data.shadow.normalBias; + if ( data.shadow.radius !== undefined ) object.shadow.radius = data.shadow.radius; + if ( data.shadow.mapSize !== undefined ) object.shadow.mapSize.fromArray( data.shadow.mapSize ); + if ( data.shadow.camera !== undefined ) object.shadow.camera = this.parseObject( data.shadow.camera ); + + } + + if ( data.visible !== undefined ) object.visible = data.visible; + if ( data.frustumCulled !== undefined ) object.frustumCulled = data.frustumCulled; + if ( data.renderOrder !== undefined ) object.renderOrder = data.renderOrder; + if ( data.userData !== undefined ) object.userData = data.userData; + if ( data.layers !== undefined ) object.layers.mask = data.layers; + + if ( data.children !== undefined ) { + + const children = data.children; + + for ( let i = 0; i < children.length; i ++ ) { + + object.add( this.parseObject( children[ i ], geometries, materials, textures, animations ) ); + + } + + } + + if ( data.animations !== undefined ) { + + const objectAnimations = data.animations; + + for ( let i = 0; i < objectAnimations.length; i ++ ) { + + const uuid = objectAnimations[ i ]; + + object.animations.push( animations[ uuid ] ); + + } + + } + + if ( data.type === 'LOD' ) { + + if ( data.autoUpdate !== undefined ) object.autoUpdate = data.autoUpdate; + + const levels = data.levels; + + for ( let l = 0; l < levels.length; l ++ ) { + + const level = levels[ l ]; + const child = object.getObjectByProperty( 'uuid', level.object ); + + if ( child !== undefined ) { + + object.addLevel( child, level.distance, level.hysteresis ); + + } + + } + + } + + return object; + + } + + bindSkeletons( object, skeletons ) { + + if ( Object.keys( skeletons ).length === 0 ) return; + + object.traverse( function ( child ) { + + if ( child.isSkinnedMesh === true && child.skeleton !== undefined ) { + + const skeleton = skeletons[ child.skeleton ]; + + if ( skeleton === undefined ) { + + console.warn( 'THREE.ObjectLoader: No skeleton found with UUID:', child.skeleton ); + + } else { + + child.bind( skeleton, child.bindMatrix ); + + } + + } + + } ); + + } + +} + +const TEXTURE_MAPPING = { + UVMapping: UVMapping, + CubeReflectionMapping: CubeReflectionMapping, + CubeRefractionMapping: CubeRefractionMapping, + EquirectangularReflectionMapping: EquirectangularReflectionMapping, + EquirectangularRefractionMapping: EquirectangularRefractionMapping, + CubeUVReflectionMapping: CubeUVReflectionMapping +}; + +const TEXTURE_WRAPPING = { + RepeatWrapping: RepeatWrapping, + ClampToEdgeWrapping: ClampToEdgeWrapping, + MirroredRepeatWrapping: MirroredRepeatWrapping +}; + +const TEXTURE_FILTER = { + NearestFilter: NearestFilter, + NearestMipmapNearestFilter: NearestMipmapNearestFilter, + NearestMipmapLinearFilter: NearestMipmapLinearFilter, + LinearFilter: LinearFilter, + LinearMipmapNearestFilter: LinearMipmapNearestFilter, + LinearMipmapLinearFilter: LinearMipmapLinearFilter +}; + +class ImageBitmapLoader extends Loader { + + constructor( manager ) { + + super( manager ); + + this.isImageBitmapLoader = true; + + if ( typeof createImageBitmap === 'undefined' ) { + + console.warn( 'THREE.ImageBitmapLoader: createImageBitmap() not supported.' ); + + } + + if ( typeof fetch === 'undefined' ) { + + console.warn( 'THREE.ImageBitmapLoader: fetch() not supported.' ); + + } + + this.options = { premultiplyAlpha: 'none' }; + + } + + setOptions( options ) { + + this.options = options; + + return this; + + } + + load( url, onLoad, onProgress, onError ) { + + if ( url === undefined ) url = ''; + + if ( this.path !== undefined ) url = this.path + url; + + url = this.manager.resolveURL( url ); + + const scope = this; + + const cached = Cache.get( url ); + + if ( cached !== undefined ) { + + scope.manager.itemStart( url ); + + // If cached is a promise, wait for it to resolve + if ( cached.then ) { + + cached.then( imageBitmap => { + + if ( onLoad ) onLoad( imageBitmap ); + + scope.manager.itemEnd( url ); + + } ).catch( e => { + + if ( onError ) onError( e ); + + } ); + return; + + } + + // If cached is not a promise (i.e., it's already an imageBitmap) + setTimeout( function () { + + if ( onLoad ) onLoad( cached ); + + scope.manager.itemEnd( url ); + + }, 0 ); + + return cached; + + } + + const fetchOptions = {}; + fetchOptions.credentials = ( this.crossOrigin === 'anonymous' ) ? 'same-origin' : 'include'; + fetchOptions.headers = this.requestHeader; + + const promise = fetch( url, fetchOptions ).then( function ( res ) { + + return res.blob(); + + } ).then( function ( blob ) { + + return createImageBitmap( blob, Object.assign( scope.options, { colorSpaceConversion: 'none' } ) ); + + } ).then( function ( imageBitmap ) { + + Cache.add( url, imageBitmap ); + + if ( onLoad ) onLoad( imageBitmap ); + + scope.manager.itemEnd( url ); + + return imageBitmap; + + } ).catch( function ( e ) { + + if ( onError ) onError( e ); + + Cache.remove( url ); + + scope.manager.itemError( url ); + scope.manager.itemEnd( url ); + + } ); + + Cache.add( url, promise ); + scope.manager.itemStart( url ); + + } + +} + +let _context; + +class AudioContext { + + static getContext() { + + if ( _context === undefined ) { + + _context = new ( window.AudioContext || window.webkitAudioContext )(); + + } + + return _context; + + } + + static setContext( value ) { + + _context = value; + + } + +} + +class AudioLoader extends Loader { + + constructor( manager ) { + + super( manager ); + + } + + load( url, onLoad, onProgress, onError ) { + + const scope = this; + + const loader = new FileLoader( this.manager ); + loader.setResponseType( 'arraybuffer' ); + loader.setPath( this.path ); + loader.setRequestHeader( this.requestHeader ); + loader.setWithCredentials( this.withCredentials ); + loader.load( url, function ( buffer ) { + + try { + + // Create a copy of the buffer. The `decodeAudioData` method + // detaches the buffer when complete, preventing reuse. + const bufferCopy = buffer.slice( 0 ); + + const context = AudioContext.getContext(); + context.decodeAudioData( bufferCopy, function ( audioBuffer ) { + + onLoad( audioBuffer ); + + } ).catch( handleError ); + + } catch ( e ) { + + handleError( e ); + + } + + }, onProgress, onError ); + + function handleError( e ) { + + if ( onError ) { + + onError( e ); + + } else { + + console.error( e ); + + } + + scope.manager.itemError( url ); + + } + + } + +} + +const _eyeRight = /*@__PURE__*/ new Matrix4(); +const _eyeLeft = /*@__PURE__*/ new Matrix4(); +const _projectionMatrix = /*@__PURE__*/ new Matrix4(); + +class StereoCamera { + + constructor() { + + this.type = 'StereoCamera'; + + this.aspect = 1; + + this.eyeSep = 0.064; + + this.cameraL = new PerspectiveCamera(); + this.cameraL.layers.enable( 1 ); + this.cameraL.matrixAutoUpdate = false; + + this.cameraR = new PerspectiveCamera(); + this.cameraR.layers.enable( 2 ); + this.cameraR.matrixAutoUpdate = false; + + this._cache = { + focus: null, + fov: null, + aspect: null, + near: null, + far: null, + zoom: null, + eyeSep: null + }; + + } + + update( camera ) { + + const cache = this._cache; + + const needsUpdate = cache.focus !== camera.focus || cache.fov !== camera.fov || + cache.aspect !== camera.aspect * this.aspect || cache.near !== camera.near || + cache.far !== camera.far || cache.zoom !== camera.zoom || cache.eyeSep !== this.eyeSep; + + if ( needsUpdate ) { + + cache.focus = camera.focus; + cache.fov = camera.fov; + cache.aspect = camera.aspect * this.aspect; + cache.near = camera.near; + cache.far = camera.far; + cache.zoom = camera.zoom; + cache.eyeSep = this.eyeSep; + + // Off-axis stereoscopic effect based on + // http://paulbourke.net/stereographics/stereorender/ + + _projectionMatrix.copy( camera.projectionMatrix ); + const eyeSepHalf = cache.eyeSep / 2; + const eyeSepOnProjection = eyeSepHalf * cache.near / cache.focus; + const ymax = ( cache.near * Math.tan( DEG2RAD * cache.fov * 0.5 ) ) / cache.zoom; + let xmin, xmax; + + // translate xOffset + + _eyeLeft.elements[ 12 ] = - eyeSepHalf; + _eyeRight.elements[ 12 ] = eyeSepHalf; + + // for left eye + + xmin = - ymax * cache.aspect + eyeSepOnProjection; + xmax = ymax * cache.aspect + eyeSepOnProjection; + + _projectionMatrix.elements[ 0 ] = 2 * cache.near / ( xmax - xmin ); + _projectionMatrix.elements[ 8 ] = ( xmax + xmin ) / ( xmax - xmin ); + + this.cameraL.projectionMatrix.copy( _projectionMatrix ); + + // for right eye + + xmin = - ymax * cache.aspect - eyeSepOnProjection; + xmax = ymax * cache.aspect - eyeSepOnProjection; + + _projectionMatrix.elements[ 0 ] = 2 * cache.near / ( xmax - xmin ); + _projectionMatrix.elements[ 8 ] = ( xmax + xmin ) / ( xmax - xmin ); + + this.cameraR.projectionMatrix.copy( _projectionMatrix ); + + } + + this.cameraL.matrixWorld.copy( camera.matrixWorld ).multiply( _eyeLeft ); + this.cameraR.matrixWorld.copy( camera.matrixWorld ).multiply( _eyeRight ); + + } + +} + +class Clock { + + constructor( autoStart = true ) { + + this.autoStart = autoStart; + + this.startTime = 0; + this.oldTime = 0; + this.elapsedTime = 0; + + this.running = false; + + } + + start() { + + this.startTime = now(); + + this.oldTime = this.startTime; + this.elapsedTime = 0; + this.running = true; + + } + + stop() { + + this.getElapsedTime(); + this.running = false; + this.autoStart = false; + + } + + getElapsedTime() { + + this.getDelta(); + return this.elapsedTime; + + } + + getDelta() { + + let diff = 0; + + if ( this.autoStart && ! this.running ) { + + this.start(); + return 0; + + } + + if ( this.running ) { + + const newTime = now(); + + diff = ( newTime - this.oldTime ) / 1000; + this.oldTime = newTime; + + this.elapsedTime += diff; + + } + + return diff; + + } + +} + +function now() { + + return ( typeof performance === 'undefined' ? Date : performance ).now(); // see #10732 + +} + +const _position$1 = /*@__PURE__*/ new Vector3(); +const _quaternion$1 = /*@__PURE__*/ new Quaternion(); +const _scale$1 = /*@__PURE__*/ new Vector3(); +const _orientation$1 = /*@__PURE__*/ new Vector3(); + +class AudioListener extends Object3D { + + constructor() { + + super(); + + this.type = 'AudioListener'; + + this.context = AudioContext.getContext(); + + this.gain = this.context.createGain(); + this.gain.connect( this.context.destination ); + + this.filter = null; + + this.timeDelta = 0; + + // private + + this._clock = new Clock(); + + } + + getInput() { + + return this.gain; + + } + + removeFilter() { + + if ( this.filter !== null ) { + + this.gain.disconnect( this.filter ); + this.filter.disconnect( this.context.destination ); + this.gain.connect( this.context.destination ); + this.filter = null; + + } + + return this; + + } + + getFilter() { + + return this.filter; + + } + + setFilter( value ) { + + if ( this.filter !== null ) { + + this.gain.disconnect( this.filter ); + this.filter.disconnect( this.context.destination ); + + } else { + + this.gain.disconnect( this.context.destination ); + + } + + this.filter = value; + this.gain.connect( this.filter ); + this.filter.connect( this.context.destination ); + + return this; + + } + + getMasterVolume() { + + return this.gain.gain.value; + + } + + setMasterVolume( value ) { + + this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 ); + + return this; + + } + + updateMatrixWorld( force ) { + + super.updateMatrixWorld( force ); + + const listener = this.context.listener; + const up = this.up; + + this.timeDelta = this._clock.getDelta(); + + this.matrixWorld.decompose( _position$1, _quaternion$1, _scale$1 ); + + _orientation$1.set( 0, 0, - 1 ).applyQuaternion( _quaternion$1 ); + + if ( listener.positionX ) { + + // code path for Chrome (see #14393) + + const endTime = this.context.currentTime + this.timeDelta; + + listener.positionX.linearRampToValueAtTime( _position$1.x, endTime ); + listener.positionY.linearRampToValueAtTime( _position$1.y, endTime ); + listener.positionZ.linearRampToValueAtTime( _position$1.z, endTime ); + listener.forwardX.linearRampToValueAtTime( _orientation$1.x, endTime ); + listener.forwardY.linearRampToValueAtTime( _orientation$1.y, endTime ); + listener.forwardZ.linearRampToValueAtTime( _orientation$1.z, endTime ); + listener.upX.linearRampToValueAtTime( up.x, endTime ); + listener.upY.linearRampToValueAtTime( up.y, endTime ); + listener.upZ.linearRampToValueAtTime( up.z, endTime ); + + } else { + + listener.setPosition( _position$1.x, _position$1.y, _position$1.z ); + listener.setOrientation( _orientation$1.x, _orientation$1.y, _orientation$1.z, up.x, up.y, up.z ); + + } + + } + +} + +class Audio extends Object3D { + + constructor( listener ) { + + super(); + + this.type = 'Audio'; + + this.listener = listener; + this.context = listener.context; + + this.gain = this.context.createGain(); + this.gain.connect( listener.getInput() ); + + this.autoplay = false; + + this.buffer = null; + this.detune = 0; + this.loop = false; + this.loopStart = 0; + this.loopEnd = 0; + this.offset = 0; + this.duration = undefined; + this.playbackRate = 1; + this.isPlaying = false; + this.hasPlaybackControl = true; + this.source = null; + this.sourceType = 'empty'; + + this._startedAt = 0; + this._progress = 0; + this._connected = false; + + this.filters = []; + + } + + getOutput() { + + return this.gain; + + } + + setNodeSource( audioNode ) { + + this.hasPlaybackControl = false; + this.sourceType = 'audioNode'; + this.source = audioNode; + this.connect(); + + return this; + + } + + setMediaElementSource( mediaElement ) { + + this.hasPlaybackControl = false; + this.sourceType = 'mediaNode'; + this.source = this.context.createMediaElementSource( mediaElement ); + this.connect(); + + return this; + + } + + setMediaStreamSource( mediaStream ) { + + this.hasPlaybackControl = false; + this.sourceType = 'mediaStreamNode'; + this.source = this.context.createMediaStreamSource( mediaStream ); + this.connect(); + + return this; + + } + + setBuffer( audioBuffer ) { + + this.buffer = audioBuffer; + this.sourceType = 'buffer'; + + if ( this.autoplay ) this.play(); + + return this; + + } + + play( delay = 0 ) { + + if ( this.isPlaying === true ) { + + console.warn( 'THREE.Audio: Audio is already playing.' ); + return; + + } + + if ( this.hasPlaybackControl === false ) { + + console.warn( 'THREE.Audio: this Audio has no playback control.' ); + return; + + } + + this._startedAt = this.context.currentTime + delay; + + const source = this.context.createBufferSource(); + source.buffer = this.buffer; + source.loop = this.loop; + source.loopStart = this.loopStart; + source.loopEnd = this.loopEnd; + source.onended = this.onEnded.bind( this ); + source.start( this._startedAt, this._progress + this.offset, this.duration ); + + this.isPlaying = true; + + this.source = source; + + this.setDetune( this.detune ); + this.setPlaybackRate( this.playbackRate ); + + return this.connect(); + + } + + pause() { + + if ( this.hasPlaybackControl === false ) { + + console.warn( 'THREE.Audio: this Audio has no playback control.' ); + return; + + } + + if ( this.isPlaying === true ) { + + // update current progress + + this._progress += Math.max( this.context.currentTime - this._startedAt, 0 ) * this.playbackRate; + + if ( this.loop === true ) { + + // ensure _progress does not exceed duration with looped audios + + this._progress = this._progress % ( this.duration || this.buffer.duration ); + + } + + this.source.stop(); + this.source.onended = null; + + this.isPlaying = false; + + } + + return this; + + } + + stop() { + + if ( this.hasPlaybackControl === false ) { + + console.warn( 'THREE.Audio: this Audio has no playback control.' ); + return; + + } + + this._progress = 0; + + if ( this.source !== null ) { + + this.source.stop(); + this.source.onended = null; + + } + + this.isPlaying = false; + + return this; + + } + + connect() { + + if ( this.filters.length > 0 ) { + + this.source.connect( this.filters[ 0 ] ); + + for ( let i = 1, l = this.filters.length; i < l; i ++ ) { + + this.filters[ i - 1 ].connect( this.filters[ i ] ); + + } + + this.filters[ this.filters.length - 1 ].connect( this.getOutput() ); + + } else { + + this.source.connect( this.getOutput() ); + + } + + this._connected = true; + + return this; + + } + + disconnect() { + + if ( this._connected === false ) { + + return; + + } + + if ( this.filters.length > 0 ) { + + this.source.disconnect( this.filters[ 0 ] ); + + for ( let i = 1, l = this.filters.length; i < l; i ++ ) { + + this.filters[ i - 1 ].disconnect( this.filters[ i ] ); + + } + + this.filters[ this.filters.length - 1 ].disconnect( this.getOutput() ); + + } else { + + this.source.disconnect( this.getOutput() ); + + } + + this._connected = false; + + return this; + + } + + getFilters() { + + return this.filters; + + } + + setFilters( value ) { + + if ( ! value ) value = []; + + if ( this._connected === true ) { + + this.disconnect(); + this.filters = value.slice(); + this.connect(); + + } else { + + this.filters = value.slice(); + + } + + return this; + + } + + setDetune( value ) { + + this.detune = value; + + if ( this.isPlaying === true && this.source.detune !== undefined ) { + + this.source.detune.setTargetAtTime( this.detune, this.context.currentTime, 0.01 ); + + } + + return this; + + } + + getDetune() { + + return this.detune; + + } + + getFilter() { + + return this.getFilters()[ 0 ]; + + } + + setFilter( filter ) { + + return this.setFilters( filter ? [ filter ] : [] ); + + } + + setPlaybackRate( value ) { + + if ( this.hasPlaybackControl === false ) { + + console.warn( 'THREE.Audio: this Audio has no playback control.' ); + return; + + } + + this.playbackRate = value; + + if ( this.isPlaying === true ) { + + this.source.playbackRate.setTargetAtTime( this.playbackRate, this.context.currentTime, 0.01 ); + + } + + return this; + + } + + getPlaybackRate() { + + return this.playbackRate; + + } + + onEnded() { + + this.isPlaying = false; + + } + + getLoop() { + + if ( this.hasPlaybackControl === false ) { + + console.warn( 'THREE.Audio: this Audio has no playback control.' ); + return false; + + } + + return this.loop; + + } + + setLoop( value ) { + + if ( this.hasPlaybackControl === false ) { + + console.warn( 'THREE.Audio: this Audio has no playback control.' ); + return; + + } + + this.loop = value; + + if ( this.isPlaying === true ) { + + this.source.loop = this.loop; + + } + + return this; + + } + + setLoopStart( value ) { + + this.loopStart = value; + + return this; + + } + + setLoopEnd( value ) { + + this.loopEnd = value; + + return this; + + } + + getVolume() { + + return this.gain.gain.value; + + } + + setVolume( value ) { + + this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 ); + + return this; + + } + +} + +const _position = /*@__PURE__*/ new Vector3(); +const _quaternion = /*@__PURE__*/ new Quaternion(); +const _scale = /*@__PURE__*/ new Vector3(); +const _orientation = /*@__PURE__*/ new Vector3(); + +class PositionalAudio extends Audio { + + constructor( listener ) { + + super( listener ); + + this.panner = this.context.createPanner(); + this.panner.panningModel = 'HRTF'; + this.panner.connect( this.gain ); + + } + + connect() { + + super.connect(); + + this.panner.connect( this.gain ); + + } + + disconnect() { + + super.disconnect(); + + this.panner.disconnect( this.gain ); + + } + + getOutput() { + + return this.panner; + + } + + getRefDistance() { + + return this.panner.refDistance; + + } + + setRefDistance( value ) { + + this.panner.refDistance = value; + + return this; + + } + + getRolloffFactor() { + + return this.panner.rolloffFactor; + + } + + setRolloffFactor( value ) { + + this.panner.rolloffFactor = value; + + return this; + + } + + getDistanceModel() { + + return this.panner.distanceModel; + + } + + setDistanceModel( value ) { + + this.panner.distanceModel = value; + + return this; + + } + + getMaxDistance() { + + return this.panner.maxDistance; + + } + + setMaxDistance( value ) { + + this.panner.maxDistance = value; + + return this; + + } + + setDirectionalCone( coneInnerAngle, coneOuterAngle, coneOuterGain ) { + + this.panner.coneInnerAngle = coneInnerAngle; + this.panner.coneOuterAngle = coneOuterAngle; + this.panner.coneOuterGain = coneOuterGain; + + return this; + + } + + updateMatrixWorld( force ) { + + super.updateMatrixWorld( force ); + + if ( this.hasPlaybackControl === true && this.isPlaying === false ) return; + + this.matrixWorld.decompose( _position, _quaternion, _scale ); + + _orientation.set( 0, 0, 1 ).applyQuaternion( _quaternion ); + + const panner = this.panner; + + if ( panner.positionX ) { + + // code path for Chrome and Firefox (see #14393) + + const endTime = this.context.currentTime + this.listener.timeDelta; + + panner.positionX.linearRampToValueAtTime( _position.x, endTime ); + panner.positionY.linearRampToValueAtTime( _position.y, endTime ); + panner.positionZ.linearRampToValueAtTime( _position.z, endTime ); + panner.orientationX.linearRampToValueAtTime( _orientation.x, endTime ); + panner.orientationY.linearRampToValueAtTime( _orientation.y, endTime ); + panner.orientationZ.linearRampToValueAtTime( _orientation.z, endTime ); + + } else { + + panner.setPosition( _position.x, _position.y, _position.z ); + panner.setOrientation( _orientation.x, _orientation.y, _orientation.z ); + + } + + } + +} + +class AudioAnalyser { + + constructor( audio, fftSize = 2048 ) { + + this.analyser = audio.context.createAnalyser(); + this.analyser.fftSize = fftSize; + + this.data = new Uint8Array( this.analyser.frequencyBinCount ); + + audio.getOutput().connect( this.analyser ); + + } + + + getFrequencyData() { + + this.analyser.getByteFrequencyData( this.data ); + + return this.data; + + } + + getAverageFrequency() { + + let value = 0; + const data = this.getFrequencyData(); + + for ( let i = 0; i < data.length; i ++ ) { + + value += data[ i ]; + + } + + return value / data.length; + + } + +} + +class PropertyMixer { + + constructor( binding, typeName, valueSize ) { + + this.binding = binding; + this.valueSize = valueSize; + + let mixFunction, + mixFunctionAdditive, + setIdentity; + + // buffer layout: [ incoming | accu0 | accu1 | orig | addAccu | (optional work) ] + // + // interpolators can use .buffer as their .result + // the data then goes to 'incoming' + // + // 'accu0' and 'accu1' are used frame-interleaved for + // the cumulative result and are compared to detect + // changes + // + // 'orig' stores the original state of the property + // + // 'add' is used for additive cumulative results + // + // 'work' is optional and is only present for quaternion types. It is used + // to store intermediate quaternion multiplication results + + switch ( typeName ) { + + case 'quaternion': + mixFunction = this._slerp; + mixFunctionAdditive = this._slerpAdditive; + setIdentity = this._setAdditiveIdentityQuaternion; + + this.buffer = new Float64Array( valueSize * 6 ); + this._workIndex = 5; + break; + + case 'string': + case 'bool': + mixFunction = this._select; + + // Use the regular mix function and for additive on these types, + // additive is not relevant for non-numeric types + mixFunctionAdditive = this._select; + + setIdentity = this._setAdditiveIdentityOther; + + this.buffer = new Array( valueSize * 5 ); + break; + + default: + mixFunction = this._lerp; + mixFunctionAdditive = this._lerpAdditive; + setIdentity = this._setAdditiveIdentityNumeric; + + this.buffer = new Float64Array( valueSize * 5 ); + + } + + this._mixBufferRegion = mixFunction; + this._mixBufferRegionAdditive = mixFunctionAdditive; + this._setIdentity = setIdentity; + this._origIndex = 3; + this._addIndex = 4; + + this.cumulativeWeight = 0; + this.cumulativeWeightAdditive = 0; + + this.useCount = 0; + this.referenceCount = 0; + + } + + // accumulate data in the 'incoming' region into 'accu' + accumulate( accuIndex, weight ) { + + // note: happily accumulating nothing when weight = 0, the caller knows + // the weight and shouldn't have made the call in the first place + + const buffer = this.buffer, + stride = this.valueSize, + offset = accuIndex * stride + stride; + + let currentWeight = this.cumulativeWeight; + + if ( currentWeight === 0 ) { + + // accuN := incoming * weight + + for ( let i = 0; i !== stride; ++ i ) { + + buffer[ offset + i ] = buffer[ i ]; + + } + + currentWeight = weight; + + } else { + + // accuN := accuN + incoming * weight + + currentWeight += weight; + const mix = weight / currentWeight; + this._mixBufferRegion( buffer, offset, 0, mix, stride ); + + } + + this.cumulativeWeight = currentWeight; + + } + + // accumulate data in the 'incoming' region into 'add' + accumulateAdditive( weight ) { + + const buffer = this.buffer, + stride = this.valueSize, + offset = stride * this._addIndex; + + if ( this.cumulativeWeightAdditive === 0 ) { + + // add = identity + + this._setIdentity(); + + } + + // add := add + incoming * weight + + this._mixBufferRegionAdditive( buffer, offset, 0, weight, stride ); + this.cumulativeWeightAdditive += weight; + + } + + // apply the state of 'accu' to the binding when accus differ + apply( accuIndex ) { + + const stride = this.valueSize, + buffer = this.buffer, + offset = accuIndex * stride + stride, + + weight = this.cumulativeWeight, + weightAdditive = this.cumulativeWeightAdditive, + + binding = this.binding; + + this.cumulativeWeight = 0; + this.cumulativeWeightAdditive = 0; + + if ( weight < 1 ) { + + // accuN := accuN + original * ( 1 - cumulativeWeight ) + + const originalValueOffset = stride * this._origIndex; + + this._mixBufferRegion( + buffer, offset, originalValueOffset, 1 - weight, stride ); + + } + + if ( weightAdditive > 0 ) { + + // accuN := accuN + additive accuN + + this._mixBufferRegionAdditive( buffer, offset, this._addIndex * stride, 1, stride ); + + } + + for ( let i = stride, e = stride + stride; i !== e; ++ i ) { + + if ( buffer[ i ] !== buffer[ i + stride ] ) { + + // value has changed -> update scene graph + + binding.setValue( buffer, offset ); + break; + + } + + } + + } + + // remember the state of the bound property and copy it to both accus + saveOriginalState() { + + const binding = this.binding; + + const buffer = this.buffer, + stride = this.valueSize, + + originalValueOffset = stride * this._origIndex; + + binding.getValue( buffer, originalValueOffset ); + + // accu[0..1] := orig -- initially detect changes against the original + for ( let i = stride, e = originalValueOffset; i !== e; ++ i ) { + + buffer[ i ] = buffer[ originalValueOffset + ( i % stride ) ]; + + } + + // Add to identity for additive + this._setIdentity(); + + this.cumulativeWeight = 0; + this.cumulativeWeightAdditive = 0; + + } + + // apply the state previously taken via 'saveOriginalState' to the binding + restoreOriginalState() { + + const originalValueOffset = this.valueSize * 3; + this.binding.setValue( this.buffer, originalValueOffset ); + + } + + _setAdditiveIdentityNumeric() { + + const startIndex = this._addIndex * this.valueSize; + const endIndex = startIndex + this.valueSize; + + for ( let i = startIndex; i < endIndex; i ++ ) { + + this.buffer[ i ] = 0; + + } + + } + + _setAdditiveIdentityQuaternion() { + + this._setAdditiveIdentityNumeric(); + this.buffer[ this._addIndex * this.valueSize + 3 ] = 1; + + } + + _setAdditiveIdentityOther() { + + const startIndex = this._origIndex * this.valueSize; + const targetIndex = this._addIndex * this.valueSize; + + for ( let i = 0; i < this.valueSize; i ++ ) { + + this.buffer[ targetIndex + i ] = this.buffer[ startIndex + i ]; + + } + + } + + + // mix functions + + _select( buffer, dstOffset, srcOffset, t, stride ) { + + if ( t >= 0.5 ) { + + for ( let i = 0; i !== stride; ++ i ) { + + buffer[ dstOffset + i ] = buffer[ srcOffset + i ]; + + } + + } + + } + + _slerp( buffer, dstOffset, srcOffset, t ) { + + Quaternion.slerpFlat( buffer, dstOffset, buffer, dstOffset, buffer, srcOffset, t ); + + } + + _slerpAdditive( buffer, dstOffset, srcOffset, t, stride ) { + + const workOffset = this._workIndex * stride; + + // Store result in intermediate buffer offset + Quaternion.multiplyQuaternionsFlat( buffer, workOffset, buffer, dstOffset, buffer, srcOffset ); + + // Slerp to the intermediate result + Quaternion.slerpFlat( buffer, dstOffset, buffer, dstOffset, buffer, workOffset, t ); + + } + + _lerp( buffer, dstOffset, srcOffset, t, stride ) { + + const s = 1 - t; + + for ( let i = 0; i !== stride; ++ i ) { + + const j = dstOffset + i; + + buffer[ j ] = buffer[ j ] * s + buffer[ srcOffset + i ] * t; + + } + + } + + _lerpAdditive( buffer, dstOffset, srcOffset, t, stride ) { + + for ( let i = 0; i !== stride; ++ i ) { + + const j = dstOffset + i; + + buffer[ j ] = buffer[ j ] + buffer[ srcOffset + i ] * t; + + } + + } + +} + +// Characters [].:/ are reserved for track binding syntax. +const _RESERVED_CHARS_RE = '\\[\\]\\.:\\/'; +const _reservedRe = new RegExp( '[' + _RESERVED_CHARS_RE + ']', 'g' ); + +// Attempts to allow node names from any language. ES5's `\w` regexp matches +// only latin characters, and the unicode \p{L} is not yet supported. So +// instead, we exclude reserved characters and match everything else. +const _wordChar = '[^' + _RESERVED_CHARS_RE + ']'; +const _wordCharOrDot = '[^' + _RESERVED_CHARS_RE.replace( '\\.', '' ) + ']'; + +// Parent directories, delimited by '/' or ':'. Currently unused, but must +// be matched to parse the rest of the track name. +const _directoryRe = /*@__PURE__*/ /((?:WC+[\/:])*)/.source.replace( 'WC', _wordChar ); + +// Target node. May contain word characters (a-zA-Z0-9_) and '.' or '-'. +const _nodeRe = /*@__PURE__*/ /(WCOD+)?/.source.replace( 'WCOD', _wordCharOrDot ); + +// Object on target node, and accessor. May not contain reserved +// characters. Accessor may contain any character except closing bracket. +const _objectRe = /*@__PURE__*/ /(?:\.(WC+)(?:\[(.+)\])?)?/.source.replace( 'WC', _wordChar ); + +// Property and accessor. May not contain reserved characters. Accessor may +// contain any non-bracket characters. +const _propertyRe = /*@__PURE__*/ /\.(WC+)(?:\[(.+)\])?/.source.replace( 'WC', _wordChar ); + +const _trackRe = new RegExp( '' + + '^' + + _directoryRe + + _nodeRe + + _objectRe + + _propertyRe + + '$' +); + +const _supportedObjectNames = [ 'material', 'materials', 'bones', 'map' ]; + +class Composite { + + constructor( targetGroup, path, optionalParsedPath ) { + + const parsedPath = optionalParsedPath || PropertyBinding.parseTrackName( path ); + + this._targetGroup = targetGroup; + this._bindings = targetGroup.subscribe_( path, parsedPath ); + + } + + getValue( array, offset ) { + + this.bind(); // bind all binding + + const firstValidIndex = this._targetGroup.nCachedObjects_, + binding = this._bindings[ firstValidIndex ]; + + // and only call .getValue on the first + if ( binding !== undefined ) binding.getValue( array, offset ); + + } + + setValue( array, offset ) { + + const bindings = this._bindings; + + for ( let i = this._targetGroup.nCachedObjects_, n = bindings.length; i !== n; ++ i ) { + + bindings[ i ].setValue( array, offset ); + + } + + } + + bind() { + + const bindings = this._bindings; + + for ( let i = this._targetGroup.nCachedObjects_, n = bindings.length; i !== n; ++ i ) { + + bindings[ i ].bind(); + + } + + } + + unbind() { + + const bindings = this._bindings; + + for ( let i = this._targetGroup.nCachedObjects_, n = bindings.length; i !== n; ++ i ) { + + bindings[ i ].unbind(); + + } + + } + +} + +// Note: This class uses a State pattern on a per-method basis: +// 'bind' sets 'this.getValue' / 'setValue' and shadows the +// prototype version of these methods with one that represents +// the bound state. When the property is not found, the methods +// become no-ops. +class PropertyBinding { + + constructor( rootNode, path, parsedPath ) { + + this.path = path; + this.parsedPath = parsedPath || PropertyBinding.parseTrackName( path ); + + this.node = PropertyBinding.findNode( rootNode, this.parsedPath.nodeName ); + + this.rootNode = rootNode; + + // initial state of these methods that calls 'bind' + this.getValue = this._getValue_unbound; + this.setValue = this._setValue_unbound; + + } + + + static create( root, path, parsedPath ) { + + if ( ! ( root && root.isAnimationObjectGroup ) ) { + + return new PropertyBinding( root, path, parsedPath ); + + } else { + + return new PropertyBinding.Composite( root, path, parsedPath ); + + } + + } + + /** + * Replaces spaces with underscores and removes unsupported characters from + * node names, to ensure compatibility with parseTrackName(). + * + * @param {string} name Node name to be sanitized. + * @return {string} + */ + static sanitizeNodeName( name ) { + + return name.replace( /\s/g, '_' ).replace( _reservedRe, '' ); + + } + + static parseTrackName( trackName ) { + + const matches = _trackRe.exec( trackName ); + + if ( matches === null ) { + + throw new Error( 'PropertyBinding: Cannot parse trackName: ' + trackName ); + + } + + const results = { + // directoryName: matches[ 1 ], // (tschw) currently unused + nodeName: matches[ 2 ], + objectName: matches[ 3 ], + objectIndex: matches[ 4 ], + propertyName: matches[ 5 ], // required + propertyIndex: matches[ 6 ] + }; + + const lastDot = results.nodeName && results.nodeName.lastIndexOf( '.' ); + + if ( lastDot !== undefined && lastDot !== - 1 ) { + + const objectName = results.nodeName.substring( lastDot + 1 ); + + // Object names must be checked against an allowlist. Otherwise, there + // is no way to parse 'foo.bar.baz': 'baz' must be a property, but + // 'bar' could be the objectName, or part of a nodeName (which can + // include '.' characters). + if ( _supportedObjectNames.indexOf( objectName ) !== - 1 ) { + + results.nodeName = results.nodeName.substring( 0, lastDot ); + results.objectName = objectName; + + } + + } + + if ( results.propertyName === null || results.propertyName.length === 0 ) { + + throw new Error( 'PropertyBinding: can not parse propertyName from trackName: ' + trackName ); + + } + + return results; + + } + + static findNode( root, nodeName ) { + + if ( nodeName === undefined || nodeName === '' || nodeName === '.' || nodeName === - 1 || nodeName === root.name || nodeName === root.uuid ) { + + return root; + + } + + // search into skeleton bones. + if ( root.skeleton ) { + + const bone = root.skeleton.getBoneByName( nodeName ); + + if ( bone !== undefined ) { + + return bone; + + } + + } + + // search into node subtree. + if ( root.children ) { + + const searchNodeSubtree = function ( children ) { + + for ( let i = 0; i < children.length; i ++ ) { + + const childNode = children[ i ]; + + if ( childNode.name === nodeName || childNode.uuid === nodeName ) { + + return childNode; + + } + + const result = searchNodeSubtree( childNode.children ); + + if ( result ) return result; + + } + + return null; + + }; + + const subTreeNode = searchNodeSubtree( root.children ); + + if ( subTreeNode ) { + + return subTreeNode; + + } + + } + + return null; + + } + + // these are used to "bind" a nonexistent property + _getValue_unavailable() {} + _setValue_unavailable() {} + + // Getters + + _getValue_direct( buffer, offset ) { + + buffer[ offset ] = this.targetObject[ this.propertyName ]; + + } + + _getValue_array( buffer, offset ) { + + const source = this.resolvedProperty; + + for ( let i = 0, n = source.length; i !== n; ++ i ) { + + buffer[ offset ++ ] = source[ i ]; + + } + + } + + _getValue_arrayElement( buffer, offset ) { + + buffer[ offset ] = this.resolvedProperty[ this.propertyIndex ]; + + } + + _getValue_toArray( buffer, offset ) { + + this.resolvedProperty.toArray( buffer, offset ); + + } + + // Direct + + _setValue_direct( buffer, offset ) { + + this.targetObject[ this.propertyName ] = buffer[ offset ]; + + } + + _setValue_direct_setNeedsUpdate( buffer, offset ) { + + this.targetObject[ this.propertyName ] = buffer[ offset ]; + this.targetObject.needsUpdate = true; + + } + + _setValue_direct_setMatrixWorldNeedsUpdate( buffer, offset ) { + + this.targetObject[ this.propertyName ] = buffer[ offset ]; + this.targetObject.matrixWorldNeedsUpdate = true; + + } + + // EntireArray + + _setValue_array( buffer, offset ) { + + const dest = this.resolvedProperty; + + for ( let i = 0, n = dest.length; i !== n; ++ i ) { + + dest[ i ] = buffer[ offset ++ ]; + + } + + } + + _setValue_array_setNeedsUpdate( buffer, offset ) { + + const dest = this.resolvedProperty; + + for ( let i = 0, n = dest.length; i !== n; ++ i ) { + + dest[ i ] = buffer[ offset ++ ]; + + } + + this.targetObject.needsUpdate = true; + + } + + _setValue_array_setMatrixWorldNeedsUpdate( buffer, offset ) { + + const dest = this.resolvedProperty; + + for ( let i = 0, n = dest.length; i !== n; ++ i ) { + + dest[ i ] = buffer[ offset ++ ]; + + } + + this.targetObject.matrixWorldNeedsUpdate = true; + + } + + // ArrayElement + + _setValue_arrayElement( buffer, offset ) { + + this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ]; + + } + + _setValue_arrayElement_setNeedsUpdate( buffer, offset ) { + + this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ]; + this.targetObject.needsUpdate = true; + + } + + _setValue_arrayElement_setMatrixWorldNeedsUpdate( buffer, offset ) { + + this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ]; + this.targetObject.matrixWorldNeedsUpdate = true; + + } + + // HasToFromArray + + _setValue_fromArray( buffer, offset ) { + + this.resolvedProperty.fromArray( buffer, offset ); + + } + + _setValue_fromArray_setNeedsUpdate( buffer, offset ) { + + this.resolvedProperty.fromArray( buffer, offset ); + this.targetObject.needsUpdate = true; + + } + + _setValue_fromArray_setMatrixWorldNeedsUpdate( buffer, offset ) { + + this.resolvedProperty.fromArray( buffer, offset ); + this.targetObject.matrixWorldNeedsUpdate = true; + + } + + _getValue_unbound( targetArray, offset ) { + + this.bind(); + this.getValue( targetArray, offset ); + + } + + _setValue_unbound( sourceArray, offset ) { + + this.bind(); + this.setValue( sourceArray, offset ); + + } + + // create getter / setter pair for a property in the scene graph + bind() { + + let targetObject = this.node; + const parsedPath = this.parsedPath; + + const objectName = parsedPath.objectName; + const propertyName = parsedPath.propertyName; + let propertyIndex = parsedPath.propertyIndex; + + if ( ! targetObject ) { + + targetObject = PropertyBinding.findNode( this.rootNode, parsedPath.nodeName ); + + this.node = targetObject; + + } + + // set fail state so we can just 'return' on error + this.getValue = this._getValue_unavailable; + this.setValue = this._setValue_unavailable; + + // ensure there is a value node + if ( ! targetObject ) { + + console.warn( 'THREE.PropertyBinding: No target node found for track: ' + this.path + '.' ); + return; + + } + + if ( objectName ) { + + let objectIndex = parsedPath.objectIndex; + + // special cases were we need to reach deeper into the hierarchy to get the face materials.... + switch ( objectName ) { + + case 'materials': + + if ( ! targetObject.material ) { + + console.error( 'THREE.PropertyBinding: Can not bind to material as node does not have a material.', this ); + return; + + } + + if ( ! targetObject.material.materials ) { + + console.error( 'THREE.PropertyBinding: Can not bind to material.materials as node.material does not have a materials array.', this ); + return; + + } + + targetObject = targetObject.material.materials; + + break; + + case 'bones': + + if ( ! targetObject.skeleton ) { + + console.error( 'THREE.PropertyBinding: Can not bind to bones as node does not have a skeleton.', this ); + return; + + } + + // potential future optimization: skip this if propertyIndex is already an integer + // and convert the integer string to a true integer. + + targetObject = targetObject.skeleton.bones; + + // support resolving morphTarget names into indices. + for ( let i = 0; i < targetObject.length; i ++ ) { + + if ( targetObject[ i ].name === objectIndex ) { + + objectIndex = i; + break; + + } + + } + + break; + + case 'map': + + if ( 'map' in targetObject ) { + + targetObject = targetObject.map; + break; + + } + + if ( ! targetObject.material ) { + + console.error( 'THREE.PropertyBinding: Can not bind to material as node does not have a material.', this ); + return; + + } + + if ( ! targetObject.material.map ) { + + console.error( 'THREE.PropertyBinding: Can not bind to material.map as node.material does not have a map.', this ); + return; + + } + + targetObject = targetObject.material.map; + break; + + default: + + if ( targetObject[ objectName ] === undefined ) { + + console.error( 'THREE.PropertyBinding: Can not bind to objectName of node undefined.', this ); + return; + + } + + targetObject = targetObject[ objectName ]; + + } + + + if ( objectIndex !== undefined ) { + + if ( targetObject[ objectIndex ] === undefined ) { + + console.error( 'THREE.PropertyBinding: Trying to bind to objectIndex of objectName, but is undefined.', this, targetObject ); + return; + + } + + targetObject = targetObject[ objectIndex ]; + + } + + } + + // resolve property + const nodeProperty = targetObject[ propertyName ]; + + if ( nodeProperty === undefined ) { + + const nodeName = parsedPath.nodeName; + + console.error( 'THREE.PropertyBinding: Trying to update property for track: ' + nodeName + + '.' + propertyName + ' but it wasn\'t found.', targetObject ); + return; + + } + + // determine versioning scheme + let versioning = this.Versioning.None; + + this.targetObject = targetObject; + + if ( targetObject.needsUpdate !== undefined ) { // material + + versioning = this.Versioning.NeedsUpdate; + + } else if ( targetObject.matrixWorldNeedsUpdate !== undefined ) { // node transform + + versioning = this.Versioning.MatrixWorldNeedsUpdate; + + } + + // determine how the property gets bound + let bindingType = this.BindingType.Direct; + + if ( propertyIndex !== undefined ) { + + // access a sub element of the property array (only primitives are supported right now) + + if ( propertyName === 'morphTargetInfluences' ) { + + // potential optimization, skip this if propertyIndex is already an integer, and convert the integer string to a true integer. + + // support resolving morphTarget names into indices. + if ( ! targetObject.geometry ) { + + console.error( 'THREE.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry.', this ); + return; + + } + + if ( ! targetObject.geometry.morphAttributes ) { + + console.error( 'THREE.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry.morphAttributes.', this ); + return; + + } + + if ( targetObject.morphTargetDictionary[ propertyIndex ] !== undefined ) { + + propertyIndex = targetObject.morphTargetDictionary[ propertyIndex ]; + + } + + } + + bindingType = this.BindingType.ArrayElement; + + this.resolvedProperty = nodeProperty; + this.propertyIndex = propertyIndex; + + } else if ( nodeProperty.fromArray !== undefined && nodeProperty.toArray !== undefined ) { + + // must use copy for Object3D.Euler/Quaternion + + bindingType = this.BindingType.HasFromToArray; + + this.resolvedProperty = nodeProperty; + + } else if ( Array.isArray( nodeProperty ) ) { + + bindingType = this.BindingType.EntireArray; + + this.resolvedProperty = nodeProperty; + + } else { + + this.propertyName = propertyName; + + } + + // select getter / setter + this.getValue = this.GetterByBindingType[ bindingType ]; + this.setValue = this.SetterByBindingTypeAndVersioning[ bindingType ][ versioning ]; + + } + + unbind() { + + this.node = null; + + // back to the prototype version of getValue / setValue + // note: avoiding to mutate the shape of 'this' via 'delete' + this.getValue = this._getValue_unbound; + this.setValue = this._setValue_unbound; + + } + +} + +PropertyBinding.Composite = Composite; + +PropertyBinding.prototype.BindingType = { + Direct: 0, + EntireArray: 1, + ArrayElement: 2, + HasFromToArray: 3 +}; + +PropertyBinding.prototype.Versioning = { + None: 0, + NeedsUpdate: 1, + MatrixWorldNeedsUpdate: 2 +}; + +PropertyBinding.prototype.GetterByBindingType = [ + + PropertyBinding.prototype._getValue_direct, + PropertyBinding.prototype._getValue_array, + PropertyBinding.prototype._getValue_arrayElement, + PropertyBinding.prototype._getValue_toArray, + +]; + +PropertyBinding.prototype.SetterByBindingTypeAndVersioning = [ + + [ + // Direct + PropertyBinding.prototype._setValue_direct, + PropertyBinding.prototype._setValue_direct_setNeedsUpdate, + PropertyBinding.prototype._setValue_direct_setMatrixWorldNeedsUpdate, + + ], [ + + // EntireArray + + PropertyBinding.prototype._setValue_array, + PropertyBinding.prototype._setValue_array_setNeedsUpdate, + PropertyBinding.prototype._setValue_array_setMatrixWorldNeedsUpdate, + + ], [ + + // ArrayElement + PropertyBinding.prototype._setValue_arrayElement, + PropertyBinding.prototype._setValue_arrayElement_setNeedsUpdate, + PropertyBinding.prototype._setValue_arrayElement_setMatrixWorldNeedsUpdate, + + ], [ + + // HasToFromArray + PropertyBinding.prototype._setValue_fromArray, + PropertyBinding.prototype._setValue_fromArray_setNeedsUpdate, + PropertyBinding.prototype._setValue_fromArray_setMatrixWorldNeedsUpdate, + + ] + +]; + +/** + * + * A group of objects that receives a shared animation state. + * + * Usage: + * + * - Add objects you would otherwise pass as 'root' to the + * constructor or the .clipAction method of AnimationMixer. + * + * - Instead pass this object as 'root'. + * + * - You can also add and remove objects later when the mixer + * is running. + * + * Note: + * + * Objects of this class appear as one object to the mixer, + * so cache control of the individual objects must be done + * on the group. + * + * Limitation: + * + * - The animated properties must be compatible among the + * all objects in the group. + * + * - A single property can either be controlled through a + * target group or directly, but not both. + */ + +class AnimationObjectGroup { + + constructor() { + + this.isAnimationObjectGroup = true; + + this.uuid = generateUUID(); + + // cached objects followed by the active ones + this._objects = Array.prototype.slice.call( arguments ); + + this.nCachedObjects_ = 0; // threshold + // note: read by PropertyBinding.Composite + + const indices = {}; + this._indicesByUUID = indices; // for bookkeeping + + for ( let i = 0, n = arguments.length; i !== n; ++ i ) { + + indices[ arguments[ i ].uuid ] = i; + + } + + this._paths = []; // inside: string + this._parsedPaths = []; // inside: { we don't care, here } + this._bindings = []; // inside: Array< PropertyBinding > + this._bindingsIndicesByPath = {}; // inside: indices in these arrays + + const scope = this; + + this.stats = { + + objects: { + get total() { + + return scope._objects.length; + + }, + get inUse() { + + return this.total - scope.nCachedObjects_; + + } + }, + get bindingsPerObject() { + + return scope._bindings.length; + + } + + }; + + } + + add() { + + const objects = this._objects, + indicesByUUID = this._indicesByUUID, + paths = this._paths, + parsedPaths = this._parsedPaths, + bindings = this._bindings, + nBindings = bindings.length; + + let knownObject = undefined, + nObjects = objects.length, + nCachedObjects = this.nCachedObjects_; + + for ( let i = 0, n = arguments.length; i !== n; ++ i ) { + + const object = arguments[ i ], + uuid = object.uuid; + let index = indicesByUUID[ uuid ]; + + if ( index === undefined ) { + + // unknown object -> add it to the ACTIVE region + + index = nObjects ++; + indicesByUUID[ uuid ] = index; + objects.push( object ); + + // accounting is done, now do the same for all bindings + + for ( let j = 0, m = nBindings; j !== m; ++ j ) { + + bindings[ j ].push( new PropertyBinding( object, paths[ j ], parsedPaths[ j ] ) ); + + } + + } else if ( index < nCachedObjects ) { + + knownObject = objects[ index ]; + + // move existing object to the ACTIVE region + + const firstActiveIndex = -- nCachedObjects, + lastCachedObject = objects[ firstActiveIndex ]; + + indicesByUUID[ lastCachedObject.uuid ] = index; + objects[ index ] = lastCachedObject; + + indicesByUUID[ uuid ] = firstActiveIndex; + objects[ firstActiveIndex ] = object; + + // accounting is done, now do the same for all bindings + + for ( let j = 0, m = nBindings; j !== m; ++ j ) { + + const bindingsForPath = bindings[ j ], + lastCached = bindingsForPath[ firstActiveIndex ]; + + let binding = bindingsForPath[ index ]; + + bindingsForPath[ index ] = lastCached; + + if ( binding === undefined ) { + + // since we do not bother to create new bindings + // for objects that are cached, the binding may + // or may not exist + + binding = new PropertyBinding( object, paths[ j ], parsedPaths[ j ] ); + + } + + bindingsForPath[ firstActiveIndex ] = binding; + + } + + } else if ( objects[ index ] !== knownObject ) { + + console.error( 'THREE.AnimationObjectGroup: Different objects with the same UUID ' + + 'detected. Clean the caches or recreate your infrastructure when reloading scenes.' ); + + } // else the object is already where we want it to be + + } // for arguments + + this.nCachedObjects_ = nCachedObjects; + + } + + remove() { + + const objects = this._objects, + indicesByUUID = this._indicesByUUID, + bindings = this._bindings, + nBindings = bindings.length; + + let nCachedObjects = this.nCachedObjects_; + + for ( let i = 0, n = arguments.length; i !== n; ++ i ) { + + const object = arguments[ i ], + uuid = object.uuid, + index = indicesByUUID[ uuid ]; + + if ( index !== undefined && index >= nCachedObjects ) { + + // move existing object into the CACHED region + + const lastCachedIndex = nCachedObjects ++, + firstActiveObject = objects[ lastCachedIndex ]; + + indicesByUUID[ firstActiveObject.uuid ] = index; + objects[ index ] = firstActiveObject; + + indicesByUUID[ uuid ] = lastCachedIndex; + objects[ lastCachedIndex ] = object; + + // accounting is done, now do the same for all bindings + + for ( let j = 0, m = nBindings; j !== m; ++ j ) { + + const bindingsForPath = bindings[ j ], + firstActive = bindingsForPath[ lastCachedIndex ], + binding = bindingsForPath[ index ]; + + bindingsForPath[ index ] = firstActive; + bindingsForPath[ lastCachedIndex ] = binding; + + } + + } + + } // for arguments + + this.nCachedObjects_ = nCachedObjects; + + } + + // remove & forget + uncache() { + + const objects = this._objects, + indicesByUUID = this._indicesByUUID, + bindings = this._bindings, + nBindings = bindings.length; + + let nCachedObjects = this.nCachedObjects_, + nObjects = objects.length; + + for ( let i = 0, n = arguments.length; i !== n; ++ i ) { + + const object = arguments[ i ], + uuid = object.uuid, + index = indicesByUUID[ uuid ]; + + if ( index !== undefined ) { + + delete indicesByUUID[ uuid ]; + + if ( index < nCachedObjects ) { + + // object is cached, shrink the CACHED region + + const firstActiveIndex = -- nCachedObjects, + lastCachedObject = objects[ firstActiveIndex ], + lastIndex = -- nObjects, + lastObject = objects[ lastIndex ]; + + // last cached object takes this object's place + indicesByUUID[ lastCachedObject.uuid ] = index; + objects[ index ] = lastCachedObject; + + // last object goes to the activated slot and pop + indicesByUUID[ lastObject.uuid ] = firstActiveIndex; + objects[ firstActiveIndex ] = lastObject; + objects.pop(); + + // accounting is done, now do the same for all bindings + + for ( let j = 0, m = nBindings; j !== m; ++ j ) { + + const bindingsForPath = bindings[ j ], + lastCached = bindingsForPath[ firstActiveIndex ], + last = bindingsForPath[ lastIndex ]; + + bindingsForPath[ index ] = lastCached; + bindingsForPath[ firstActiveIndex ] = last; + bindingsForPath.pop(); + + } + + } else { + + // object is active, just swap with the last and pop + + const lastIndex = -- nObjects, + lastObject = objects[ lastIndex ]; + + if ( lastIndex > 0 ) { + + indicesByUUID[ lastObject.uuid ] = index; + + } + + objects[ index ] = lastObject; + objects.pop(); + + // accounting is done, now do the same for all bindings + + for ( let j = 0, m = nBindings; j !== m; ++ j ) { + + const bindingsForPath = bindings[ j ]; + + bindingsForPath[ index ] = bindingsForPath[ lastIndex ]; + bindingsForPath.pop(); + + } + + } // cached or active + + } // if object is known + + } // for arguments + + this.nCachedObjects_ = nCachedObjects; + + } + + // Internal interface used by befriended PropertyBinding.Composite: + + subscribe_( path, parsedPath ) { + + // returns an array of bindings for the given path that is changed + // according to the contained objects in the group + + const indicesByPath = this._bindingsIndicesByPath; + let index = indicesByPath[ path ]; + const bindings = this._bindings; + + if ( index !== undefined ) return bindings[ index ]; + + const paths = this._paths, + parsedPaths = this._parsedPaths, + objects = this._objects, + nObjects = objects.length, + nCachedObjects = this.nCachedObjects_, + bindingsForPath = new Array( nObjects ); + + index = bindings.length; + + indicesByPath[ path ] = index; + + paths.push( path ); + parsedPaths.push( parsedPath ); + bindings.push( bindingsForPath ); + + for ( let i = nCachedObjects, n = objects.length; i !== n; ++ i ) { + + const object = objects[ i ]; + bindingsForPath[ i ] = new PropertyBinding( object, path, parsedPath ); + + } + + return bindingsForPath; + + } + + unsubscribe_( path ) { + + // tells the group to forget about a property path and no longer + // update the array previously obtained with 'subscribe_' + + const indicesByPath = this._bindingsIndicesByPath, + index = indicesByPath[ path ]; + + if ( index !== undefined ) { + + const paths = this._paths, + parsedPaths = this._parsedPaths, + bindings = this._bindings, + lastBindingsIndex = bindings.length - 1, + lastBindings = bindings[ lastBindingsIndex ], + lastBindingsPath = path[ lastBindingsIndex ]; + + indicesByPath[ lastBindingsPath ] = index; + + bindings[ index ] = lastBindings; + bindings.pop(); + + parsedPaths[ index ] = parsedPaths[ lastBindingsIndex ]; + parsedPaths.pop(); + + paths[ index ] = paths[ lastBindingsIndex ]; + paths.pop(); + + } + + } + +} + +class AnimationAction { + + constructor( mixer, clip, localRoot = null, blendMode = clip.blendMode ) { + + this._mixer = mixer; + this._clip = clip; + this._localRoot = localRoot; + this.blendMode = blendMode; + + const tracks = clip.tracks, + nTracks = tracks.length, + interpolants = new Array( nTracks ); + + const interpolantSettings = { + endingStart: ZeroCurvatureEnding, + endingEnd: ZeroCurvatureEnding + }; + + for ( let i = 0; i !== nTracks; ++ i ) { + + const interpolant = tracks[ i ].createInterpolant( null ); + interpolants[ i ] = interpolant; + interpolant.settings = interpolantSettings; + + } + + this._interpolantSettings = interpolantSettings; + + this._interpolants = interpolants; // bound by the mixer + + // inside: PropertyMixer (managed by the mixer) + this._propertyBindings = new Array( nTracks ); + + this._cacheIndex = null; // for the memory manager + this._byClipCacheIndex = null; // for the memory manager + + this._timeScaleInterpolant = null; + this._weightInterpolant = null; + + this.loop = LoopRepeat; + this._loopCount = - 1; + + // global mixer time when the action is to be started + // it's set back to 'null' upon start of the action + this._startTime = null; + + // scaled local time of the action + // gets clamped or wrapped to 0..clip.duration according to loop + this.time = 0; + + this.timeScale = 1; + this._effectiveTimeScale = 1; + + this.weight = 1; + this._effectiveWeight = 1; + + this.repetitions = Infinity; // no. of repetitions when looping + + this.paused = false; // true -> zero effective time scale + this.enabled = true; // false -> zero effective weight + + this.clampWhenFinished = false;// keep feeding the last frame? + + this.zeroSlopeAtStart = true;// for smooth interpolation w/o separate + this.zeroSlopeAtEnd = true;// clips for start, loop and end + + } + + // State & Scheduling + + play() { + + this._mixer._activateAction( this ); + + return this; + + } + + stop() { + + this._mixer._deactivateAction( this ); + + return this.reset(); + + } + + reset() { + + this.paused = false; + this.enabled = true; + + this.time = 0; // restart clip + this._loopCount = - 1;// forget previous loops + this._startTime = null;// forget scheduling + + return this.stopFading().stopWarping(); + + } + + isRunning() { + + return this.enabled && ! this.paused && this.timeScale !== 0 && + this._startTime === null && this._mixer._isActiveAction( this ); + + } + + // return true when play has been called + isScheduled() { + + return this._mixer._isActiveAction( this ); + + } + + startAt( time ) { + + this._startTime = time; + + return this; + + } + + setLoop( mode, repetitions ) { + + this.loop = mode; + this.repetitions = repetitions; + + return this; + + } + + // Weight + + // set the weight stopping any scheduled fading + // although .enabled = false yields an effective weight of zero, this + // method does *not* change .enabled, because it would be confusing + setEffectiveWeight( weight ) { + + this.weight = weight; + + // note: same logic as when updated at runtime + this._effectiveWeight = this.enabled ? weight : 0; + + return this.stopFading(); + + } + + // return the weight considering fading and .enabled + getEffectiveWeight() { + + return this._effectiveWeight; + + } + + fadeIn( duration ) { + + return this._scheduleFading( duration, 0, 1 ); + + } + + fadeOut( duration ) { + + return this._scheduleFading( duration, 1, 0 ); + + } + + crossFadeFrom( fadeOutAction, duration, warp ) { + + fadeOutAction.fadeOut( duration ); + this.fadeIn( duration ); + + if ( warp ) { + + const fadeInDuration = this._clip.duration, + fadeOutDuration = fadeOutAction._clip.duration, + + startEndRatio = fadeOutDuration / fadeInDuration, + endStartRatio = fadeInDuration / fadeOutDuration; + + fadeOutAction.warp( 1.0, startEndRatio, duration ); + this.warp( endStartRatio, 1.0, duration ); + + } + + return this; + + } + + crossFadeTo( fadeInAction, duration, warp ) { + + return fadeInAction.crossFadeFrom( this, duration, warp ); + + } + + stopFading() { + + const weightInterpolant = this._weightInterpolant; + + if ( weightInterpolant !== null ) { + + this._weightInterpolant = null; + this._mixer._takeBackControlInterpolant( weightInterpolant ); + + } + + return this; + + } + + // Time Scale Control + + // set the time scale stopping any scheduled warping + // although .paused = true yields an effective time scale of zero, this + // method does *not* change .paused, because it would be confusing + setEffectiveTimeScale( timeScale ) { + + this.timeScale = timeScale; + this._effectiveTimeScale = this.paused ? 0 : timeScale; + + return this.stopWarping(); + + } + + // return the time scale considering warping and .paused + getEffectiveTimeScale() { + + return this._effectiveTimeScale; + + } + + setDuration( duration ) { + + this.timeScale = this._clip.duration / duration; + + return this.stopWarping(); + + } + + syncWith( action ) { + + this.time = action.time; + this.timeScale = action.timeScale; + + return this.stopWarping(); + + } + + halt( duration ) { + + return this.warp( this._effectiveTimeScale, 0, duration ); + + } + + warp( startTimeScale, endTimeScale, duration ) { + + const mixer = this._mixer, + now = mixer.time, + timeScale = this.timeScale; + + let interpolant = this._timeScaleInterpolant; + + if ( interpolant === null ) { + + interpolant = mixer._lendControlInterpolant(); + this._timeScaleInterpolant = interpolant; + + } + + const times = interpolant.parameterPositions, + values = interpolant.sampleValues; + + times[ 0 ] = now; + times[ 1 ] = now + duration; + + values[ 0 ] = startTimeScale / timeScale; + values[ 1 ] = endTimeScale / timeScale; + + return this; + + } + + stopWarping() { + + const timeScaleInterpolant = this._timeScaleInterpolant; + + if ( timeScaleInterpolant !== null ) { + + this._timeScaleInterpolant = null; + this._mixer._takeBackControlInterpolant( timeScaleInterpolant ); + + } + + return this; + + } + + // Object Accessors + + getMixer() { + + return this._mixer; + + } + + getClip() { + + return this._clip; + + } + + getRoot() { + + return this._localRoot || this._mixer._root; + + } + + // Interna + + _update( time, deltaTime, timeDirection, accuIndex ) { + + // called by the mixer + + if ( ! this.enabled ) { + + // call ._updateWeight() to update ._effectiveWeight + + this._updateWeight( time ); + return; + + } + + const startTime = this._startTime; + + if ( startTime !== null ) { + + // check for scheduled start of action + + const timeRunning = ( time - startTime ) * timeDirection; + if ( timeRunning < 0 || timeDirection === 0 ) { + + deltaTime = 0; + + } else { + + + this._startTime = null; // unschedule + deltaTime = timeDirection * timeRunning; + + } + + } + + // apply time scale and advance time + + deltaTime *= this._updateTimeScale( time ); + const clipTime = this._updateTime( deltaTime ); + + // note: _updateTime may disable the action resulting in + // an effective weight of 0 + + const weight = this._updateWeight( time ); + + if ( weight > 0 ) { + + const interpolants = this._interpolants; + const propertyMixers = this._propertyBindings; + + switch ( this.blendMode ) { + + case AdditiveAnimationBlendMode: + + for ( let j = 0, m = interpolants.length; j !== m; ++ j ) { + + interpolants[ j ].evaluate( clipTime ); + propertyMixers[ j ].accumulateAdditive( weight ); + + } + + break; + + case NormalAnimationBlendMode: + default: + + for ( let j = 0, m = interpolants.length; j !== m; ++ j ) { + + interpolants[ j ].evaluate( clipTime ); + propertyMixers[ j ].accumulate( accuIndex, weight ); + + } + + } + + } + + } + + _updateWeight( time ) { + + let weight = 0; + + if ( this.enabled ) { + + weight = this.weight; + const interpolant = this._weightInterpolant; + + if ( interpolant !== null ) { + + const interpolantValue = interpolant.evaluate( time )[ 0 ]; + + weight *= interpolantValue; + + if ( time > interpolant.parameterPositions[ 1 ] ) { + + this.stopFading(); + + if ( interpolantValue === 0 ) { + + // faded out, disable + this.enabled = false; + + } + + } + + } + + } + + this._effectiveWeight = weight; + return weight; + + } + + _updateTimeScale( time ) { + + let timeScale = 0; + + if ( ! this.paused ) { + + timeScale = this.timeScale; + + const interpolant = this._timeScaleInterpolant; + + if ( interpolant !== null ) { + + const interpolantValue = interpolant.evaluate( time )[ 0 ]; + + timeScale *= interpolantValue; + + if ( time > interpolant.parameterPositions[ 1 ] ) { + + this.stopWarping(); + + if ( timeScale === 0 ) { + + // motion has halted, pause + this.paused = true; + + } else { + + // warp done - apply final time scale + this.timeScale = timeScale; + + } + + } + + } + + } + + this._effectiveTimeScale = timeScale; + return timeScale; + + } + + _updateTime( deltaTime ) { + + const duration = this._clip.duration; + const loop = this.loop; + + let time = this.time + deltaTime; + let loopCount = this._loopCount; + + const pingPong = ( loop === LoopPingPong ); + + if ( deltaTime === 0 ) { + + if ( loopCount === - 1 ) return time; + + return ( pingPong && ( loopCount & 1 ) === 1 ) ? duration - time : time; + + } + + if ( loop === LoopOnce ) { + + if ( loopCount === - 1 ) { + + // just started + + this._loopCount = 0; + this._setEndings( true, true, false ); + + } + + handle_stop: { + + if ( time >= duration ) { + + time = duration; + + } else if ( time < 0 ) { + + time = 0; + + } else { + + this.time = time; + + break handle_stop; + + } + + if ( this.clampWhenFinished ) this.paused = true; + else this.enabled = false; + + this.time = time; + + this._mixer.dispatchEvent( { + type: 'finished', action: this, + direction: deltaTime < 0 ? - 1 : 1 + } ); + + } + + } else { // repetitive Repeat or PingPong + + if ( loopCount === - 1 ) { + + // just started + + if ( deltaTime >= 0 ) { + + loopCount = 0; + + this._setEndings( true, this.repetitions === 0, pingPong ); + + } else { + + // when looping in reverse direction, the initial + // transition through zero counts as a repetition, + // so leave loopCount at -1 + + this._setEndings( this.repetitions === 0, true, pingPong ); + + } + + } + + if ( time >= duration || time < 0 ) { + + // wrap around + + const loopDelta = Math.floor( time / duration ); // signed + time -= duration * loopDelta; + + loopCount += Math.abs( loopDelta ); + + const pending = this.repetitions - loopCount; + + if ( pending <= 0 ) { + + // have to stop (switch state, clamp time, fire event) + + if ( this.clampWhenFinished ) this.paused = true; + else this.enabled = false; + + time = deltaTime > 0 ? duration : 0; + + this.time = time; + + this._mixer.dispatchEvent( { + type: 'finished', action: this, + direction: deltaTime > 0 ? 1 : - 1 + } ); + + } else { + + // keep running + + if ( pending === 1 ) { + + // entering the last round + + const atStart = deltaTime < 0; + this._setEndings( atStart, ! atStart, pingPong ); + + } else { + + this._setEndings( false, false, pingPong ); + + } + + this._loopCount = loopCount; + + this.time = time; + + this._mixer.dispatchEvent( { + type: 'loop', action: this, loopDelta: loopDelta + } ); + + } + + } else { + + this.time = time; + + } + + if ( pingPong && ( loopCount & 1 ) === 1 ) { + + // invert time for the "pong round" + + return duration - time; + + } + + } + + return time; + + } + + _setEndings( atStart, atEnd, pingPong ) { + + const settings = this._interpolantSettings; + + if ( pingPong ) { + + settings.endingStart = ZeroSlopeEnding; + settings.endingEnd = ZeroSlopeEnding; + + } else { + + // assuming for LoopOnce atStart == atEnd == true + + if ( atStart ) { + + settings.endingStart = this.zeroSlopeAtStart ? ZeroSlopeEnding : ZeroCurvatureEnding; + + } else { + + settings.endingStart = WrapAroundEnding; + + } + + if ( atEnd ) { + + settings.endingEnd = this.zeroSlopeAtEnd ? ZeroSlopeEnding : ZeroCurvatureEnding; + + } else { + + settings.endingEnd = WrapAroundEnding; + + } + + } + + } + + _scheduleFading( duration, weightNow, weightThen ) { + + const mixer = this._mixer, now = mixer.time; + let interpolant = this._weightInterpolant; + + if ( interpolant === null ) { + + interpolant = mixer._lendControlInterpolant(); + this._weightInterpolant = interpolant; + + } + + const times = interpolant.parameterPositions, + values = interpolant.sampleValues; + + times[ 0 ] = now; + values[ 0 ] = weightNow; + times[ 1 ] = now + duration; + values[ 1 ] = weightThen; + + return this; + + } + +} + +const _controlInterpolantsResultBuffer = new Float32Array( 1 ); + + +class AnimationMixer extends EventDispatcher { + + constructor( root ) { + + super(); + + this._root = root; + this._initMemoryManager(); + this._accuIndex = 0; + this.time = 0; + this.timeScale = 1.0; + + } + + _bindAction( action, prototypeAction ) { + + const root = action._localRoot || this._root, + tracks = action._clip.tracks, + nTracks = tracks.length, + bindings = action._propertyBindings, + interpolants = action._interpolants, + rootUuid = root.uuid, + bindingsByRoot = this._bindingsByRootAndName; + + let bindingsByName = bindingsByRoot[ rootUuid ]; + + if ( bindingsByName === undefined ) { + + bindingsByName = {}; + bindingsByRoot[ rootUuid ] = bindingsByName; + + } + + for ( let i = 0; i !== nTracks; ++ i ) { + + const track = tracks[ i ], + trackName = track.name; + + let binding = bindingsByName[ trackName ]; + + if ( binding !== undefined ) { + + ++ binding.referenceCount; + bindings[ i ] = binding; + + } else { + + binding = bindings[ i ]; + + if ( binding !== undefined ) { + + // existing binding, make sure the cache knows + + if ( binding._cacheIndex === null ) { + + ++ binding.referenceCount; + this._addInactiveBinding( binding, rootUuid, trackName ); + + } + + continue; + + } + + const path = prototypeAction && prototypeAction. + _propertyBindings[ i ].binding.parsedPath; + + binding = new PropertyMixer( + PropertyBinding.create( root, trackName, path ), + track.ValueTypeName, track.getValueSize() ); + + ++ binding.referenceCount; + this._addInactiveBinding( binding, rootUuid, trackName ); + + bindings[ i ] = binding; + + } + + interpolants[ i ].resultBuffer = binding.buffer; + + } + + } + + _activateAction( action ) { + + if ( ! this._isActiveAction( action ) ) { + + if ( action._cacheIndex === null ) { + + // this action has been forgotten by the cache, but the user + // appears to be still using it -> rebind + + const rootUuid = ( action._localRoot || this._root ).uuid, + clipUuid = action._clip.uuid, + actionsForClip = this._actionsByClip[ clipUuid ]; + + this._bindAction( action, + actionsForClip && actionsForClip.knownActions[ 0 ] ); + + this._addInactiveAction( action, clipUuid, rootUuid ); + + } + + const bindings = action._propertyBindings; + + // increment reference counts / sort out state + for ( let i = 0, n = bindings.length; i !== n; ++ i ) { + + const binding = bindings[ i ]; + + if ( binding.useCount ++ === 0 ) { + + this._lendBinding( binding ); + binding.saveOriginalState(); + + } + + } + + this._lendAction( action ); + + } + + } + + _deactivateAction( action ) { + + if ( this._isActiveAction( action ) ) { + + const bindings = action._propertyBindings; + + // decrement reference counts / sort out state + for ( let i = 0, n = bindings.length; i !== n; ++ i ) { + + const binding = bindings[ i ]; + + if ( -- binding.useCount === 0 ) { + + binding.restoreOriginalState(); + this._takeBackBinding( binding ); + + } + + } + + this._takeBackAction( action ); + + } + + } + + // Memory manager + + _initMemoryManager() { + + this._actions = []; // 'nActiveActions' followed by inactive ones + this._nActiveActions = 0; + + this._actionsByClip = {}; + // inside: + // { + // knownActions: Array< AnimationAction > - used as prototypes + // actionByRoot: AnimationAction - lookup + // } + + + this._bindings = []; // 'nActiveBindings' followed by inactive ones + this._nActiveBindings = 0; + + this._bindingsByRootAndName = {}; // inside: Map< name, PropertyMixer > + + + this._controlInterpolants = []; // same game as above + this._nActiveControlInterpolants = 0; + + const scope = this; + + this.stats = { + + actions: { + get total() { + + return scope._actions.length; + + }, + get inUse() { + + return scope._nActiveActions; + + } + }, + bindings: { + get total() { + + return scope._bindings.length; + + }, + get inUse() { + + return scope._nActiveBindings; + + } + }, + controlInterpolants: { + get total() { + + return scope._controlInterpolants.length; + + }, + get inUse() { + + return scope._nActiveControlInterpolants; + + } + } + + }; + + } + + // Memory management for AnimationAction objects + + _isActiveAction( action ) { + + const index = action._cacheIndex; + return index !== null && index < this._nActiveActions; + + } + + _addInactiveAction( action, clipUuid, rootUuid ) { + + const actions = this._actions, + actionsByClip = this._actionsByClip; + + let actionsForClip = actionsByClip[ clipUuid ]; + + if ( actionsForClip === undefined ) { + + actionsForClip = { + + knownActions: [ action ], + actionByRoot: {} + + }; + + action._byClipCacheIndex = 0; + + actionsByClip[ clipUuid ] = actionsForClip; + + } else { + + const knownActions = actionsForClip.knownActions; + + action._byClipCacheIndex = knownActions.length; + knownActions.push( action ); + + } + + action._cacheIndex = actions.length; + actions.push( action ); + + actionsForClip.actionByRoot[ rootUuid ] = action; + + } + + _removeInactiveAction( action ) { + + const actions = this._actions, + lastInactiveAction = actions[ actions.length - 1 ], + cacheIndex = action._cacheIndex; + + lastInactiveAction._cacheIndex = cacheIndex; + actions[ cacheIndex ] = lastInactiveAction; + actions.pop(); + + action._cacheIndex = null; + + + const clipUuid = action._clip.uuid, + actionsByClip = this._actionsByClip, + actionsForClip = actionsByClip[ clipUuid ], + knownActionsForClip = actionsForClip.knownActions, + + lastKnownAction = + knownActionsForClip[ knownActionsForClip.length - 1 ], + + byClipCacheIndex = action._byClipCacheIndex; + + lastKnownAction._byClipCacheIndex = byClipCacheIndex; + knownActionsForClip[ byClipCacheIndex ] = lastKnownAction; + knownActionsForClip.pop(); + + action._byClipCacheIndex = null; + + + const actionByRoot = actionsForClip.actionByRoot, + rootUuid = ( action._localRoot || this._root ).uuid; + + delete actionByRoot[ rootUuid ]; + + if ( knownActionsForClip.length === 0 ) { + + delete actionsByClip[ clipUuid ]; + + } + + this._removeInactiveBindingsForAction( action ); + + } + + _removeInactiveBindingsForAction( action ) { + + const bindings = action._propertyBindings; + + for ( let i = 0, n = bindings.length; i !== n; ++ i ) { + + const binding = bindings[ i ]; + + if ( -- binding.referenceCount === 0 ) { + + this._removeInactiveBinding( binding ); + + } + + } + + } + + _lendAction( action ) { + + // [ active actions | inactive actions ] + // [ active actions >| inactive actions ] + // s a + // <-swap-> + // a s + + const actions = this._actions, + prevIndex = action._cacheIndex, + + lastActiveIndex = this._nActiveActions ++, + + firstInactiveAction = actions[ lastActiveIndex ]; + + action._cacheIndex = lastActiveIndex; + actions[ lastActiveIndex ] = action; + + firstInactiveAction._cacheIndex = prevIndex; + actions[ prevIndex ] = firstInactiveAction; + + } + + _takeBackAction( action ) { + + // [ active actions | inactive actions ] + // [ active actions |< inactive actions ] + // a s + // <-swap-> + // s a + + const actions = this._actions, + prevIndex = action._cacheIndex, + + firstInactiveIndex = -- this._nActiveActions, + + lastActiveAction = actions[ firstInactiveIndex ]; + + action._cacheIndex = firstInactiveIndex; + actions[ firstInactiveIndex ] = action; + + lastActiveAction._cacheIndex = prevIndex; + actions[ prevIndex ] = lastActiveAction; + + } + + // Memory management for PropertyMixer objects + + _addInactiveBinding( binding, rootUuid, trackName ) { + + const bindingsByRoot = this._bindingsByRootAndName, + bindings = this._bindings; + + let bindingByName = bindingsByRoot[ rootUuid ]; + + if ( bindingByName === undefined ) { + + bindingByName = {}; + bindingsByRoot[ rootUuid ] = bindingByName; + + } + + bindingByName[ trackName ] = binding; + + binding._cacheIndex = bindings.length; + bindings.push( binding ); + + } + + _removeInactiveBinding( binding ) { + + const bindings = this._bindings, + propBinding = binding.binding, + rootUuid = propBinding.rootNode.uuid, + trackName = propBinding.path, + bindingsByRoot = this._bindingsByRootAndName, + bindingByName = bindingsByRoot[ rootUuid ], + + lastInactiveBinding = bindings[ bindings.length - 1 ], + cacheIndex = binding._cacheIndex; + + lastInactiveBinding._cacheIndex = cacheIndex; + bindings[ cacheIndex ] = lastInactiveBinding; + bindings.pop(); + + delete bindingByName[ trackName ]; + + if ( Object.keys( bindingByName ).length === 0 ) { + + delete bindingsByRoot[ rootUuid ]; + + } + + } + + _lendBinding( binding ) { + + const bindings = this._bindings, + prevIndex = binding._cacheIndex, + + lastActiveIndex = this._nActiveBindings ++, + + firstInactiveBinding = bindings[ lastActiveIndex ]; + + binding._cacheIndex = lastActiveIndex; + bindings[ lastActiveIndex ] = binding; + + firstInactiveBinding._cacheIndex = prevIndex; + bindings[ prevIndex ] = firstInactiveBinding; + + } + + _takeBackBinding( binding ) { + + const bindings = this._bindings, + prevIndex = binding._cacheIndex, + + firstInactiveIndex = -- this._nActiveBindings, + + lastActiveBinding = bindings[ firstInactiveIndex ]; + + binding._cacheIndex = firstInactiveIndex; + bindings[ firstInactiveIndex ] = binding; + + lastActiveBinding._cacheIndex = prevIndex; + bindings[ prevIndex ] = lastActiveBinding; + + } + + + // Memory management of Interpolants for weight and time scale + + _lendControlInterpolant() { + + const interpolants = this._controlInterpolants, + lastActiveIndex = this._nActiveControlInterpolants ++; + + let interpolant = interpolants[ lastActiveIndex ]; + + if ( interpolant === undefined ) { + + interpolant = new LinearInterpolant( + new Float32Array( 2 ), new Float32Array( 2 ), + 1, _controlInterpolantsResultBuffer ); + + interpolant.__cacheIndex = lastActiveIndex; + interpolants[ lastActiveIndex ] = interpolant; + + } + + return interpolant; + + } + + _takeBackControlInterpolant( interpolant ) { + + const interpolants = this._controlInterpolants, + prevIndex = interpolant.__cacheIndex, + + firstInactiveIndex = -- this._nActiveControlInterpolants, + + lastActiveInterpolant = interpolants[ firstInactiveIndex ]; + + interpolant.__cacheIndex = firstInactiveIndex; + interpolants[ firstInactiveIndex ] = interpolant; + + lastActiveInterpolant.__cacheIndex = prevIndex; + interpolants[ prevIndex ] = lastActiveInterpolant; + + } + + // return an action for a clip optionally using a custom root target + // object (this method allocates a lot of dynamic memory in case a + // previously unknown clip/root combination is specified) + clipAction( clip, optionalRoot, blendMode ) { + + const root = optionalRoot || this._root, + rootUuid = root.uuid; + + let clipObject = typeof clip === 'string' ? AnimationClip.findByName( root, clip ) : clip; + + const clipUuid = clipObject !== null ? clipObject.uuid : clip; + + const actionsForClip = this._actionsByClip[ clipUuid ]; + let prototypeAction = null; + + if ( blendMode === undefined ) { + + if ( clipObject !== null ) { + + blendMode = clipObject.blendMode; + + } else { + + blendMode = NormalAnimationBlendMode; + + } + + } + + if ( actionsForClip !== undefined ) { + + const existingAction = actionsForClip.actionByRoot[ rootUuid ]; + + if ( existingAction !== undefined && existingAction.blendMode === blendMode ) { + + return existingAction; + + } + + // we know the clip, so we don't have to parse all + // the bindings again but can just copy + prototypeAction = actionsForClip.knownActions[ 0 ]; + + // also, take the clip from the prototype action + if ( clipObject === null ) + clipObject = prototypeAction._clip; + + } + + // clip must be known when specified via string + if ( clipObject === null ) return null; + + // allocate all resources required to run it + const newAction = new AnimationAction( this, clipObject, optionalRoot, blendMode ); + + this._bindAction( newAction, prototypeAction ); + + // and make the action known to the memory manager + this._addInactiveAction( newAction, clipUuid, rootUuid ); + + return newAction; + + } + + // get an existing action + existingAction( clip, optionalRoot ) { + + const root = optionalRoot || this._root, + rootUuid = root.uuid, + + clipObject = typeof clip === 'string' ? + AnimationClip.findByName( root, clip ) : clip, + + clipUuid = clipObject ? clipObject.uuid : clip, + + actionsForClip = this._actionsByClip[ clipUuid ]; + + if ( actionsForClip !== undefined ) { + + return actionsForClip.actionByRoot[ rootUuid ] || null; + + } + + return null; + + } + + // deactivates all previously scheduled actions + stopAllAction() { + + const actions = this._actions, + nActions = this._nActiveActions; + + for ( let i = nActions - 1; i >= 0; -- i ) { + + actions[ i ].stop(); + + } + + return this; + + } + + // advance the time and update apply the animation + update( deltaTime ) { + + deltaTime *= this.timeScale; + + const actions = this._actions, + nActions = this._nActiveActions, + + time = this.time += deltaTime, + timeDirection = Math.sign( deltaTime ), + + accuIndex = this._accuIndex ^= 1; + + // run active actions + + for ( let i = 0; i !== nActions; ++ i ) { + + const action = actions[ i ]; + + action._update( time, deltaTime, timeDirection, accuIndex ); + + } + + // update scene graph + + const bindings = this._bindings, + nBindings = this._nActiveBindings; + + for ( let i = 0; i !== nBindings; ++ i ) { + + bindings[ i ].apply( accuIndex ); + + } + + return this; + + } + + // Allows you to seek to a specific time in an animation. + setTime( timeInSeconds ) { + + this.time = 0; // Zero out time attribute for AnimationMixer object; + for ( let i = 0; i < this._actions.length; i ++ ) { + + this._actions[ i ].time = 0; // Zero out time attribute for all associated AnimationAction objects. + + } + + return this.update( timeInSeconds ); // Update used to set exact time. Returns "this" AnimationMixer object. + + } + + // return this mixer's root target object + getRoot() { + + return this._root; + + } + + // free all resources specific to a particular clip + uncacheClip( clip ) { + + const actions = this._actions, + clipUuid = clip.uuid, + actionsByClip = this._actionsByClip, + actionsForClip = actionsByClip[ clipUuid ]; + + if ( actionsForClip !== undefined ) { + + // note: just calling _removeInactiveAction would mess up the + // iteration state and also require updating the state we can + // just throw away + + const actionsToRemove = actionsForClip.knownActions; + + for ( let i = 0, n = actionsToRemove.length; i !== n; ++ i ) { + + const action = actionsToRemove[ i ]; + + this._deactivateAction( action ); + + const cacheIndex = action._cacheIndex, + lastInactiveAction = actions[ actions.length - 1 ]; + + action._cacheIndex = null; + action._byClipCacheIndex = null; + + lastInactiveAction._cacheIndex = cacheIndex; + actions[ cacheIndex ] = lastInactiveAction; + actions.pop(); + + this._removeInactiveBindingsForAction( action ); + + } + + delete actionsByClip[ clipUuid ]; + + } + + } + + // free all resources specific to a particular root target object + uncacheRoot( root ) { + + const rootUuid = root.uuid, + actionsByClip = this._actionsByClip; + + for ( const clipUuid in actionsByClip ) { + + const actionByRoot = actionsByClip[ clipUuid ].actionByRoot, + action = actionByRoot[ rootUuid ]; + + if ( action !== undefined ) { + + this._deactivateAction( action ); + this._removeInactiveAction( action ); + + } + + } + + const bindingsByRoot = this._bindingsByRootAndName, + bindingByName = bindingsByRoot[ rootUuid ]; + + if ( bindingByName !== undefined ) { + + for ( const trackName in bindingByName ) { + + const binding = bindingByName[ trackName ]; + binding.restoreOriginalState(); + this._removeInactiveBinding( binding ); + + } + + } + + } + + // remove a targeted clip from the cache + uncacheAction( clip, optionalRoot ) { + + const action = this.existingAction( clip, optionalRoot ); + + if ( action !== null ) { + + this._deactivateAction( action ); + this._removeInactiveAction( action ); + + } + + } + +} + +class Uniform { + + constructor( value ) { + + this.value = value; + + } + + clone() { + + return new Uniform( this.value.clone === undefined ? this.value : this.value.clone() ); + + } + +} + +let _id = 0; + +class UniformsGroup extends EventDispatcher { + + constructor() { + + super(); + + this.isUniformsGroup = true; + + Object.defineProperty( this, 'id', { value: _id ++ } ); + + this.name = ''; + + this.usage = StaticDrawUsage; + this.uniforms = []; + + } + + add( uniform ) { + + this.uniforms.push( uniform ); + + return this; + + } + + remove( uniform ) { + + const index = this.uniforms.indexOf( uniform ); + + if ( index !== - 1 ) this.uniforms.splice( index, 1 ); + + return this; + + } + + setName( name ) { + + this.name = name; + + return this; + + } + + setUsage( value ) { + + this.usage = value; + + return this; + + } + + dispose() { + + this.dispatchEvent( { type: 'dispose' } ); + + return this; + + } + + copy( source ) { + + this.name = source.name; + this.usage = source.usage; + + const uniformsSource = source.uniforms; + + this.uniforms.length = 0; + + for ( let i = 0, l = uniformsSource.length; i < l; i ++ ) { + + const uniforms = Array.isArray( uniformsSource[ i ] ) ? uniformsSource[ i ] : [ uniformsSource[ i ] ]; + + for ( let j = 0; j < uniforms.length; j ++ ) { + + this.uniforms.push( uniforms[ j ].clone() ); + + } + + } + + return this; + + } + + clone() { + + return new this.constructor().copy( this ); + + } + +} + +class InstancedInterleavedBuffer extends InterleavedBuffer { + + constructor( array, stride, meshPerAttribute = 1 ) { + + super( array, stride ); + + this.isInstancedInterleavedBuffer = true; + + this.meshPerAttribute = meshPerAttribute; + + } + + copy( source ) { + + super.copy( source ); + + this.meshPerAttribute = source.meshPerAttribute; + + return this; + + } + + clone( data ) { + + const ib = super.clone( data ); + + ib.meshPerAttribute = this.meshPerAttribute; + + return ib; + + } + + toJSON( data ) { + + const json = super.toJSON( data ); + + json.isInstancedInterleavedBuffer = true; + json.meshPerAttribute = this.meshPerAttribute; + + return json; + + } + +} + +class GLBufferAttribute { + + constructor( buffer, type, itemSize, elementSize, count ) { + + this.isGLBufferAttribute = true; + + this.name = ''; + + this.buffer = buffer; + this.type = type; + this.itemSize = itemSize; + this.elementSize = elementSize; + this.count = count; + + this.version = 0; + + } + + set needsUpdate( value ) { + + if ( value === true ) this.version ++; + + } + + setBuffer( buffer ) { + + this.buffer = buffer; + + return this; + + } + + setType( type, elementSize ) { + + this.type = type; + this.elementSize = elementSize; + + return this; + + } + + setItemSize( itemSize ) { + + this.itemSize = itemSize; + + return this; + + } + + setCount( count ) { + + this.count = count; + + return this; + + } + +} + +const _matrix = /*@__PURE__*/ new Matrix4(); + +class Raycaster { + + constructor( origin, direction, near = 0, far = Infinity ) { + + this.ray = new Ray( origin, direction ); + // direction is assumed to be normalized (for accurate distance calculations) + + this.near = near; + this.far = far; + this.camera = null; + this.layers = new Layers(); + + this.params = { + Mesh: {}, + Line: { threshold: 1 }, + LOD: {}, + Points: { threshold: 1 }, + Sprite: {} + }; + + } + + set( origin, direction ) { + + // direction is assumed to be normalized (for accurate distance calculations) + + this.ray.set( origin, direction ); + + } + + setFromCamera( coords, camera ) { + + if ( camera.isPerspectiveCamera ) { + + this.ray.origin.setFromMatrixPosition( camera.matrixWorld ); + this.ray.direction.set( coords.x, coords.y, 0.5 ).unproject( camera ).sub( this.ray.origin ).normalize(); + this.camera = camera; + + } else if ( camera.isOrthographicCamera ) { + + this.ray.origin.set( coords.x, coords.y, ( camera.near + camera.far ) / ( camera.near - camera.far ) ).unproject( camera ); // set origin in plane of camera + this.ray.direction.set( 0, 0, - 1 ).transformDirection( camera.matrixWorld ); + this.camera = camera; + + } else { + + console.error( 'THREE.Raycaster: Unsupported camera type: ' + camera.type ); + + } + + } + + setFromXRController( controller ) { + + _matrix.identity().extractRotation( controller.matrixWorld ); + + this.ray.origin.setFromMatrixPosition( controller.matrixWorld ); + this.ray.direction.set( 0, 0, - 1 ).applyMatrix4( _matrix ); + + return this; + + } + + intersectObject( object, recursive = true, intersects = [] ) { + + intersect( object, this, intersects, recursive ); + + intersects.sort( ascSort ); + + return intersects; + + } + + intersectObjects( objects, recursive = true, intersects = [] ) { + + for ( let i = 0, l = objects.length; i < l; i ++ ) { + + intersect( objects[ i ], this, intersects, recursive ); + + } + + intersects.sort( ascSort ); + + return intersects; + + } + +} + +function ascSort( a, b ) { + + return a.distance - b.distance; + +} + +function intersect( object, raycaster, intersects, recursive ) { + + if ( object.layers.test( raycaster.layers ) ) { + + object.raycast( raycaster, intersects ); + + } + + if ( recursive === true ) { + + const children = object.children; + + for ( let i = 0, l = children.length; i < l; i ++ ) { + + intersect( children[ i ], raycaster, intersects, true ); + + } + + } + +} + +/** + * Ref: https://en.wikipedia.org/wiki/Spherical_coordinate_system + * + * phi (the polar angle) is measured from the positive y-axis. The positive y-axis is up. + * theta (the azimuthal angle) is measured from the positive z-axis. + */ +class Spherical { + + constructor( radius = 1, phi = 0, theta = 0 ) { + + this.radius = radius; + this.phi = phi; // polar angle + this.theta = theta; // azimuthal angle + + return this; + + } + + set( radius, phi, theta ) { + + this.radius = radius; + this.phi = phi; + this.theta = theta; + + return this; + + } + + copy( other ) { + + this.radius = other.radius; + this.phi = other.phi; + this.theta = other.theta; + + return this; + + } + + // restrict phi to be between EPS and PI-EPS + makeSafe() { + + const EPS = 0.000001; + this.phi = Math.max( EPS, Math.min( Math.PI - EPS, this.phi ) ); + + return this; + + } + + setFromVector3( v ) { + + return this.setFromCartesianCoords( v.x, v.y, v.z ); + + } + + setFromCartesianCoords( x, y, z ) { + + this.radius = Math.sqrt( x * x + y * y + z * z ); + + if ( this.radius === 0 ) { + + this.theta = 0; + this.phi = 0; + + } else { + + this.theta = Math.atan2( x, z ); + this.phi = Math.acos( clamp( y / this.radius, - 1, 1 ) ); + + } + + return this; + + } + + clone() { + + return new this.constructor().copy( this ); + + } + +} + +/** + * Ref: https://en.wikipedia.org/wiki/Cylindrical_coordinate_system + */ + +class Cylindrical { + + constructor( radius = 1, theta = 0, y = 0 ) { + + this.radius = radius; // distance from the origin to a point in the x-z plane + this.theta = theta; // counterclockwise angle in the x-z plane measured in radians from the positive z-axis + this.y = y; // height above the x-z plane + + return this; + + } + + set( radius, theta, y ) { + + this.radius = radius; + this.theta = theta; + this.y = y; + + return this; + + } + + copy( other ) { + + this.radius = other.radius; + this.theta = other.theta; + this.y = other.y; + + return this; + + } + + setFromVector3( v ) { + + return this.setFromCartesianCoords( v.x, v.y, v.z ); + + } + + setFromCartesianCoords( x, y, z ) { + + this.radius = Math.sqrt( x * x + z * z ); + this.theta = Math.atan2( x, z ); + this.y = y; + + return this; + + } + + clone() { + + return new this.constructor().copy( this ); + + } + +} + +const _vector$4 = /*@__PURE__*/ new Vector2(); + +class Box2 { + + constructor( min = new Vector2( + Infinity, + Infinity ), max = new Vector2( - Infinity, - Infinity ) ) { + + this.isBox2 = true; + + this.min = min; + this.max = max; + + } + + set( min, max ) { + + this.min.copy( min ); + this.max.copy( max ); + + return this; + + } + + setFromPoints( points ) { + + this.makeEmpty(); + + for ( let i = 0, il = points.length; i < il; i ++ ) { + + this.expandByPoint( points[ i ] ); + + } + + return this; + + } + + setFromCenterAndSize( center, size ) { + + const halfSize = _vector$4.copy( size ).multiplyScalar( 0.5 ); + this.min.copy( center ).sub( halfSize ); + this.max.copy( center ).add( halfSize ); + + return this; + + } + + clone() { + + return new this.constructor().copy( this ); + + } + + copy( box ) { + + this.min.copy( box.min ); + this.max.copy( box.max ); + + return this; + + } + + makeEmpty() { + + this.min.x = this.min.y = + Infinity; + this.max.x = this.max.y = - Infinity; + + return this; + + } + + isEmpty() { + + // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes + + return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ); + + } + + getCenter( target ) { + + return this.isEmpty() ? target.set( 0, 0 ) : target.addVectors( this.min, this.max ).multiplyScalar( 0.5 ); + + } + + getSize( target ) { + + return this.isEmpty() ? target.set( 0, 0 ) : target.subVectors( this.max, this.min ); + + } + + expandByPoint( point ) { + + this.min.min( point ); + this.max.max( point ); + + return this; + + } + + expandByVector( vector ) { + + this.min.sub( vector ); + this.max.add( vector ); + + return this; + + } + + expandByScalar( scalar ) { + + this.min.addScalar( - scalar ); + this.max.addScalar( scalar ); + + return this; + + } + + containsPoint( point ) { + + return point.x < this.min.x || point.x > this.max.x || + point.y < this.min.y || point.y > this.max.y ? false : true; + + } + + containsBox( box ) { + + return this.min.x <= box.min.x && box.max.x <= this.max.x && + this.min.y <= box.min.y && box.max.y <= this.max.y; + + } + + getParameter( point, target ) { + + // This can potentially have a divide by zero if the box + // has a size dimension of 0. + + return target.set( + ( point.x - this.min.x ) / ( this.max.x - this.min.x ), + ( point.y - this.min.y ) / ( this.max.y - this.min.y ) + ); + + } + + intersectsBox( box ) { + + // using 4 splitting planes to rule out intersections + + return box.max.x < this.min.x || box.min.x > this.max.x || + box.max.y < this.min.y || box.min.y > this.max.y ? false : true; + + } + + clampPoint( point, target ) { + + return target.copy( point ).clamp( this.min, this.max ); + + } + + distanceToPoint( point ) { + + return this.clampPoint( point, _vector$4 ).distanceTo( point ); + + } + + intersect( box ) { + + this.min.max( box.min ); + this.max.min( box.max ); + + if ( this.isEmpty() ) this.makeEmpty(); + + return this; + + } + + union( box ) { + + this.min.min( box.min ); + this.max.max( box.max ); + + return this; + + } + + translate( offset ) { + + this.min.add( offset ); + this.max.add( offset ); + + return this; + + } + + equals( box ) { + + return box.min.equals( this.min ) && box.max.equals( this.max ); + + } + +} + +const _startP = /*@__PURE__*/ new Vector3(); +const _startEnd = /*@__PURE__*/ new Vector3(); + +class Line3 { + + constructor( start = new Vector3(), end = new Vector3() ) { + + this.start = start; + this.end = end; + + } + + set( start, end ) { + + this.start.copy( start ); + this.end.copy( end ); + + return this; + + } + + copy( line ) { + + this.start.copy( line.start ); + this.end.copy( line.end ); + + return this; + + } + + getCenter( target ) { + + return target.addVectors( this.start, this.end ).multiplyScalar( 0.5 ); + + } + + delta( target ) { + + return target.subVectors( this.end, this.start ); + + } + + distanceSq() { + + return this.start.distanceToSquared( this.end ); + + } + + distance() { + + return this.start.distanceTo( this.end ); + + } + + at( t, target ) { + + return this.delta( target ).multiplyScalar( t ).add( this.start ); + + } + + closestPointToPointParameter( point, clampToLine ) { + + _startP.subVectors( point, this.start ); + _startEnd.subVectors( this.end, this.start ); + + const startEnd2 = _startEnd.dot( _startEnd ); + const startEnd_startP = _startEnd.dot( _startP ); + + let t = startEnd_startP / startEnd2; + + if ( clampToLine ) { + + t = clamp( t, 0, 1 ); + + } + + return t; + + } + + closestPointToPoint( point, clampToLine, target ) { + + const t = this.closestPointToPointParameter( point, clampToLine ); + + return this.delta( target ).multiplyScalar( t ).add( this.start ); + + } + + applyMatrix4( matrix ) { + + this.start.applyMatrix4( matrix ); + this.end.applyMatrix4( matrix ); + + return this; + + } + + equals( line ) { + + return line.start.equals( this.start ) && line.end.equals( this.end ); + + } + + clone() { + + return new this.constructor().copy( this ); + + } + +} + +const _vector$3 = /*@__PURE__*/ new Vector3(); + +class SpotLightHelper extends Object3D { + + constructor( light, color ) { + + super(); + + this.light = light; + + this.matrixAutoUpdate = false; + + this.color = color; + + this.type = 'SpotLightHelper'; + + const geometry = new BufferGeometry(); + + const positions = [ + 0, 0, 0, 0, 0, 1, + 0, 0, 0, 1, 0, 1, + 0, 0, 0, - 1, 0, 1, + 0, 0, 0, 0, 1, 1, + 0, 0, 0, 0, - 1, 1 + ]; + + for ( let i = 0, j = 1, l = 32; i < l; i ++, j ++ ) { + + const p1 = ( i / l ) * Math.PI * 2; + const p2 = ( j / l ) * Math.PI * 2; + + positions.push( + Math.cos( p1 ), Math.sin( p1 ), 1, + Math.cos( p2 ), Math.sin( p2 ), 1 + ); + + } + + geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) ); + + const material = new LineBasicMaterial( { fog: false, toneMapped: false } ); + + this.cone = new LineSegments( geometry, material ); + this.add( this.cone ); + + this.update(); + + } + + dispose() { + + this.cone.geometry.dispose(); + this.cone.material.dispose(); + + } + + update() { + + this.light.updateWorldMatrix( true, false ); + this.light.target.updateWorldMatrix( true, false ); + + // update the local matrix based on the parent and light target transforms + if ( this.parent ) { + + this.parent.updateWorldMatrix( true ); + + this.matrix + .copy( this.parent.matrixWorld ) + .invert() + .multiply( this.light.matrixWorld ); + + } else { + + this.matrix.copy( this.light.matrixWorld ); + + } + + this.matrixWorld.copy( this.light.matrixWorld ); + + const coneLength = this.light.distance ? this.light.distance : 1000; + const coneWidth = coneLength * Math.tan( this.light.angle ); + + this.cone.scale.set( coneWidth, coneWidth, coneLength ); + + _vector$3.setFromMatrixPosition( this.light.target.matrixWorld ); + + this.cone.lookAt( _vector$3 ); + + if ( this.color !== undefined ) { + + this.cone.material.color.set( this.color ); + + } else { + + this.cone.material.color.copy( this.light.color ); + + } + + } + +} + +const _vector$2 = /*@__PURE__*/ new Vector3(); +const _boneMatrix = /*@__PURE__*/ new Matrix4(); +const _matrixWorldInv = /*@__PURE__*/ new Matrix4(); + + +class SkeletonHelper extends LineSegments { + + constructor( object ) { + + const bones = getBoneList( object ); + + const geometry = new BufferGeometry(); + + const vertices = []; + const colors = []; + + const color1 = new Color( 0, 0, 1 ); + const color2 = new Color( 0, 1, 0 ); + + for ( let i = 0; i < bones.length; i ++ ) { + + const bone = bones[ i ]; + + if ( bone.parent && bone.parent.isBone ) { + + vertices.push( 0, 0, 0 ); + vertices.push( 0, 0, 0 ); + colors.push( color1.r, color1.g, color1.b ); + colors.push( color2.r, color2.g, color2.b ); + + } + + } + + geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); + geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) ); + + const material = new LineBasicMaterial( { vertexColors: true, depthTest: false, depthWrite: false, toneMapped: false, transparent: true } ); + + super( geometry, material ); + + this.isSkeletonHelper = true; + + this.type = 'SkeletonHelper'; + + this.root = object; + this.bones = bones; + + this.matrix = object.matrixWorld; + this.matrixAutoUpdate = false; + + } + + updateMatrixWorld( force ) { + + const bones = this.bones; + + const geometry = this.geometry; + const position = geometry.getAttribute( 'position' ); + + _matrixWorldInv.copy( this.root.matrixWorld ).invert(); + + for ( let i = 0, j = 0; i < bones.length; i ++ ) { + + const bone = bones[ i ]; + + if ( bone.parent && bone.parent.isBone ) { + + _boneMatrix.multiplyMatrices( _matrixWorldInv, bone.matrixWorld ); + _vector$2.setFromMatrixPosition( _boneMatrix ); + position.setXYZ( j, _vector$2.x, _vector$2.y, _vector$2.z ); + + _boneMatrix.multiplyMatrices( _matrixWorldInv, bone.parent.matrixWorld ); + _vector$2.setFromMatrixPosition( _boneMatrix ); + position.setXYZ( j + 1, _vector$2.x, _vector$2.y, _vector$2.z ); + + j += 2; + + } + + } + + geometry.getAttribute( 'position' ).needsUpdate = true; + + super.updateMatrixWorld( force ); + + } + + dispose() { + + this.geometry.dispose(); + this.material.dispose(); + + } + +} + + +function getBoneList( object ) { + + const boneList = []; + + if ( object.isBone === true ) { + + boneList.push( object ); + + } + + for ( let i = 0; i < object.children.length; i ++ ) { + + boneList.push.apply( boneList, getBoneList( object.children[ i ] ) ); + + } + + return boneList; + +} + +class PointLightHelper extends Mesh { + + constructor( light, sphereSize, color ) { + + const geometry = new SphereGeometry( sphereSize, 4, 2 ); + const material = new MeshBasicMaterial( { wireframe: true, fog: false, toneMapped: false } ); + + super( geometry, material ); + + this.light = light; + + this.color = color; + + this.type = 'PointLightHelper'; + + this.matrix = this.light.matrixWorld; + this.matrixAutoUpdate = false; + + this.update(); + + + /* + // TODO: delete this comment? + const distanceGeometry = new THREE.IcosahedronGeometry( 1, 2 ); + const distanceMaterial = new THREE.MeshBasicMaterial( { color: hexColor, fog: false, wireframe: true, opacity: 0.1, transparent: true } ); + + this.lightSphere = new THREE.Mesh( bulbGeometry, bulbMaterial ); + this.lightDistance = new THREE.Mesh( distanceGeometry, distanceMaterial ); + + const d = light.distance; + + if ( d === 0.0 ) { + + this.lightDistance.visible = false; + + } else { + + this.lightDistance.scale.set( d, d, d ); + + } + + this.add( this.lightDistance ); + */ + + } + + dispose() { + + this.geometry.dispose(); + this.material.dispose(); + + } + + update() { + + this.light.updateWorldMatrix( true, false ); + + if ( this.color !== undefined ) { + + this.material.color.set( this.color ); + + } else { + + this.material.color.copy( this.light.color ); + + } + + /* + const d = this.light.distance; + + if ( d === 0.0 ) { + + this.lightDistance.visible = false; + + } else { + + this.lightDistance.visible = true; + this.lightDistance.scale.set( d, d, d ); + + } + */ + + } + +} + +const _vector$1 = /*@__PURE__*/ new Vector3(); +const _color1 = /*@__PURE__*/ new Color(); +const _color2 = /*@__PURE__*/ new Color(); + +class HemisphereLightHelper extends Object3D { + + constructor( light, size, color ) { + + super(); + + this.light = light; + + this.matrix = light.matrixWorld; + this.matrixAutoUpdate = false; + + this.color = color; + + this.type = 'HemisphereLightHelper'; + + const geometry = new OctahedronGeometry( size ); + geometry.rotateY( Math.PI * 0.5 ); + + this.material = new MeshBasicMaterial( { wireframe: true, fog: false, toneMapped: false } ); + if ( this.color === undefined ) this.material.vertexColors = true; + + const position = geometry.getAttribute( 'position' ); + const colors = new Float32Array( position.count * 3 ); + + geometry.setAttribute( 'color', new BufferAttribute( colors, 3 ) ); + + this.add( new Mesh( geometry, this.material ) ); + + this.update(); + + } + + dispose() { + + this.children[ 0 ].geometry.dispose(); + this.children[ 0 ].material.dispose(); + + } + + update() { + + const mesh = this.children[ 0 ]; + + if ( this.color !== undefined ) { + + this.material.color.set( this.color ); + + } else { + + const colors = mesh.geometry.getAttribute( 'color' ); + + _color1.copy( this.light.color ); + _color2.copy( this.light.groundColor ); + + for ( let i = 0, l = colors.count; i < l; i ++ ) { + + const color = ( i < ( l / 2 ) ) ? _color1 : _color2; + + colors.setXYZ( i, color.r, color.g, color.b ); + + } + + colors.needsUpdate = true; + + } + + this.light.updateWorldMatrix( true, false ); + + mesh.lookAt( _vector$1.setFromMatrixPosition( this.light.matrixWorld ).negate() ); + + } + +} + +class GridHelper extends LineSegments { + + constructor( size = 10, divisions = 10, color1 = 0x444444, color2 = 0x888888 ) { + + color1 = new Color( color1 ); + color2 = new Color( color2 ); + + const center = divisions / 2; + const step = size / divisions; + const halfSize = size / 2; + + const vertices = [], colors = []; + + for ( let i = 0, j = 0, k = - halfSize; i <= divisions; i ++, k += step ) { + + vertices.push( - halfSize, 0, k, halfSize, 0, k ); + vertices.push( k, 0, - halfSize, k, 0, halfSize ); + + const color = i === center ? color1 : color2; + + color.toArray( colors, j ); j += 3; + color.toArray( colors, j ); j += 3; + color.toArray( colors, j ); j += 3; + color.toArray( colors, j ); j += 3; + + } + + const geometry = new BufferGeometry(); + geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); + geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) ); + + const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } ); + + super( geometry, material ); + + this.type = 'GridHelper'; + + } + + dispose() { + + this.geometry.dispose(); + this.material.dispose(); + + } + +} + +class PolarGridHelper extends LineSegments { + + constructor( radius = 10, sectors = 16, rings = 8, divisions = 64, color1 = 0x444444, color2 = 0x888888 ) { + + color1 = new Color( color1 ); + color2 = new Color( color2 ); + + const vertices = []; + const colors = []; + + // create the sectors + + if ( sectors > 1 ) { + + for ( let i = 0; i < sectors; i ++ ) { + + const v = ( i / sectors ) * ( Math.PI * 2 ); + + const x = Math.sin( v ) * radius; + const z = Math.cos( v ) * radius; + + vertices.push( 0, 0, 0 ); + vertices.push( x, 0, z ); + + const color = ( i & 1 ) ? color1 : color2; + + colors.push( color.r, color.g, color.b ); + colors.push( color.r, color.g, color.b ); + + } + + } + + // create the rings + + for ( let i = 0; i < rings; i ++ ) { + + const color = ( i & 1 ) ? color1 : color2; + + const r = radius - ( radius / rings * i ); + + for ( let j = 0; j < divisions; j ++ ) { + + // first vertex + + let v = ( j / divisions ) * ( Math.PI * 2 ); + + let x = Math.sin( v ) * r; + let z = Math.cos( v ) * r; + + vertices.push( x, 0, z ); + colors.push( color.r, color.g, color.b ); + + // second vertex + + v = ( ( j + 1 ) / divisions ) * ( Math.PI * 2 ); + + x = Math.sin( v ) * r; + z = Math.cos( v ) * r; + + vertices.push( x, 0, z ); + colors.push( color.r, color.g, color.b ); + + } + + } + + const geometry = new BufferGeometry(); + geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); + geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) ); + + const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } ); + + super( geometry, material ); + + this.type = 'PolarGridHelper'; + + } + + dispose() { + + this.geometry.dispose(); + this.material.dispose(); + + } + +} + +const _v1 = /*@__PURE__*/ new Vector3(); +const _v2 = /*@__PURE__*/ new Vector3(); +const _v3 = /*@__PURE__*/ new Vector3(); + +class DirectionalLightHelper extends Object3D { + + constructor( light, size, color ) { + + super(); + + this.light = light; + + this.matrix = light.matrixWorld; + this.matrixAutoUpdate = false; + + this.color = color; + + this.type = 'DirectionalLightHelper'; + + if ( size === undefined ) size = 1; + + let geometry = new BufferGeometry(); + geometry.setAttribute( 'position', new Float32BufferAttribute( [ + - size, size, 0, + size, size, 0, + size, - size, 0, + - size, - size, 0, + - size, size, 0 + ], 3 ) ); + + const material = new LineBasicMaterial( { fog: false, toneMapped: false } ); + + this.lightPlane = new Line( geometry, material ); + this.add( this.lightPlane ); + + geometry = new BufferGeometry(); + geometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 0, 1 ], 3 ) ); + + this.targetLine = new Line( geometry, material ); + this.add( this.targetLine ); + + this.update(); + + } + + dispose() { + + this.lightPlane.geometry.dispose(); + this.lightPlane.material.dispose(); + this.targetLine.geometry.dispose(); + this.targetLine.material.dispose(); + + } + + update() { + + this.light.updateWorldMatrix( true, false ); + this.light.target.updateWorldMatrix( true, false ); + + _v1.setFromMatrixPosition( this.light.matrixWorld ); + _v2.setFromMatrixPosition( this.light.target.matrixWorld ); + _v3.subVectors( _v2, _v1 ); + + this.lightPlane.lookAt( _v2 ); + + if ( this.color !== undefined ) { + + this.lightPlane.material.color.set( this.color ); + this.targetLine.material.color.set( this.color ); + + } else { + + this.lightPlane.material.color.copy( this.light.color ); + this.targetLine.material.color.copy( this.light.color ); + + } + + this.targetLine.lookAt( _v2 ); + this.targetLine.scale.z = _v3.length(); + + } + +} + +const _vector = /*@__PURE__*/ new Vector3(); +const _camera = /*@__PURE__*/ new Camera(); + +/** + * - shows frustum, line of sight and up of the camera + * - suitable for fast updates + * - based on frustum visualization in lightgl.js shadowmap example + * https://github.com/evanw/lightgl.js/blob/master/tests/shadowmap.html + */ + +class CameraHelper extends LineSegments { + + constructor( camera ) { + + const geometry = new BufferGeometry(); + const material = new LineBasicMaterial( { color: 0xffffff, vertexColors: true, toneMapped: false } ); + + const vertices = []; + const colors = []; + + const pointMap = {}; + + // near + + addLine( 'n1', 'n2' ); + addLine( 'n2', 'n4' ); + addLine( 'n4', 'n3' ); + addLine( 'n3', 'n1' ); + + // far + + addLine( 'f1', 'f2' ); + addLine( 'f2', 'f4' ); + addLine( 'f4', 'f3' ); + addLine( 'f3', 'f1' ); + + // sides + + addLine( 'n1', 'f1' ); + addLine( 'n2', 'f2' ); + addLine( 'n3', 'f3' ); + addLine( 'n4', 'f4' ); + + // cone + + addLine( 'p', 'n1' ); + addLine( 'p', 'n2' ); + addLine( 'p', 'n3' ); + addLine( 'p', 'n4' ); + + // up + + addLine( 'u1', 'u2' ); + addLine( 'u2', 'u3' ); + addLine( 'u3', 'u1' ); + + // target + + addLine( 'c', 't' ); + addLine( 'p', 'c' ); + + // cross + + addLine( 'cn1', 'cn2' ); + addLine( 'cn3', 'cn4' ); + + addLine( 'cf1', 'cf2' ); + addLine( 'cf3', 'cf4' ); + + function addLine( a, b ) { + + addPoint( a ); + addPoint( b ); + + } + + function addPoint( id ) { + + vertices.push( 0, 0, 0 ); + colors.push( 0, 0, 0 ); + + if ( pointMap[ id ] === undefined ) { + + pointMap[ id ] = []; + + } + + pointMap[ id ].push( ( vertices.length / 3 ) - 1 ); + + } + + geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); + geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) ); + + super( geometry, material ); + + this.type = 'CameraHelper'; + + this.camera = camera; + if ( this.camera.updateProjectionMatrix ) this.camera.updateProjectionMatrix(); + + this.matrix = camera.matrixWorld; + this.matrixAutoUpdate = false; + + this.pointMap = pointMap; + + this.update(); + + // colors + + const colorFrustum = new Color( 0xffaa00 ); + const colorCone = new Color( 0xff0000 ); + const colorUp = new Color( 0x00aaff ); + const colorTarget = new Color( 0xffffff ); + const colorCross = new Color( 0x333333 ); + + this.setColors( colorFrustum, colorCone, colorUp, colorTarget, colorCross ); + + } + + setColors( frustum, cone, up, target, cross ) { + + const geometry = this.geometry; + + const colorAttribute = geometry.getAttribute( 'color' ); + + // near + + colorAttribute.setXYZ( 0, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 1, frustum.r, frustum.g, frustum.b ); // n1, n2 + colorAttribute.setXYZ( 2, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 3, frustum.r, frustum.g, frustum.b ); // n2, n4 + colorAttribute.setXYZ( 4, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 5, frustum.r, frustum.g, frustum.b ); // n4, n3 + colorAttribute.setXYZ( 6, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 7, frustum.r, frustum.g, frustum.b ); // n3, n1 + + // far + + colorAttribute.setXYZ( 8, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 9, frustum.r, frustum.g, frustum.b ); // f1, f2 + colorAttribute.setXYZ( 10, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 11, frustum.r, frustum.g, frustum.b ); // f2, f4 + colorAttribute.setXYZ( 12, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 13, frustum.r, frustum.g, frustum.b ); // f4, f3 + colorAttribute.setXYZ( 14, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 15, frustum.r, frustum.g, frustum.b ); // f3, f1 + + // sides + + colorAttribute.setXYZ( 16, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 17, frustum.r, frustum.g, frustum.b ); // n1, f1 + colorAttribute.setXYZ( 18, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 19, frustum.r, frustum.g, frustum.b ); // n2, f2 + colorAttribute.setXYZ( 20, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 21, frustum.r, frustum.g, frustum.b ); // n3, f3 + colorAttribute.setXYZ( 22, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 23, frustum.r, frustum.g, frustum.b ); // n4, f4 + + // cone + + colorAttribute.setXYZ( 24, cone.r, cone.g, cone.b ); colorAttribute.setXYZ( 25, cone.r, cone.g, cone.b ); // p, n1 + colorAttribute.setXYZ( 26, cone.r, cone.g, cone.b ); colorAttribute.setXYZ( 27, cone.r, cone.g, cone.b ); // p, n2 + colorAttribute.setXYZ( 28, cone.r, cone.g, cone.b ); colorAttribute.setXYZ( 29, cone.r, cone.g, cone.b ); // p, n3 + colorAttribute.setXYZ( 30, cone.r, cone.g, cone.b ); colorAttribute.setXYZ( 31, cone.r, cone.g, cone.b ); // p, n4 + + // up + + colorAttribute.setXYZ( 32, up.r, up.g, up.b ); colorAttribute.setXYZ( 33, up.r, up.g, up.b ); // u1, u2 + colorAttribute.setXYZ( 34, up.r, up.g, up.b ); colorAttribute.setXYZ( 35, up.r, up.g, up.b ); // u2, u3 + colorAttribute.setXYZ( 36, up.r, up.g, up.b ); colorAttribute.setXYZ( 37, up.r, up.g, up.b ); // u3, u1 + + // target + + colorAttribute.setXYZ( 38, target.r, target.g, target.b ); colorAttribute.setXYZ( 39, target.r, target.g, target.b ); // c, t + colorAttribute.setXYZ( 40, cross.r, cross.g, cross.b ); colorAttribute.setXYZ( 41, cross.r, cross.g, cross.b ); // p, c + + // cross + + colorAttribute.setXYZ( 42, cross.r, cross.g, cross.b ); colorAttribute.setXYZ( 43, cross.r, cross.g, cross.b ); // cn1, cn2 + colorAttribute.setXYZ( 44, cross.r, cross.g, cross.b ); colorAttribute.setXYZ( 45, cross.r, cross.g, cross.b ); // cn3, cn4 + + colorAttribute.setXYZ( 46, cross.r, cross.g, cross.b ); colorAttribute.setXYZ( 47, cross.r, cross.g, cross.b ); // cf1, cf2 + colorAttribute.setXYZ( 48, cross.r, cross.g, cross.b ); colorAttribute.setXYZ( 49, cross.r, cross.g, cross.b ); // cf3, cf4 + + colorAttribute.needsUpdate = true; + + } + + update() { + + const geometry = this.geometry; + const pointMap = this.pointMap; + + const w = 1, h = 1; + + // we need just camera projection matrix inverse + // world matrix must be identity + + _camera.projectionMatrixInverse.copy( this.camera.projectionMatrixInverse ); + + // center / target + + setPoint( 'c', pointMap, geometry, _camera, 0, 0, - 1 ); + setPoint( 't', pointMap, geometry, _camera, 0, 0, 1 ); + + // near + + setPoint( 'n1', pointMap, geometry, _camera, - w, - h, - 1 ); + setPoint( 'n2', pointMap, geometry, _camera, w, - h, - 1 ); + setPoint( 'n3', pointMap, geometry, _camera, - w, h, - 1 ); + setPoint( 'n4', pointMap, geometry, _camera, w, h, - 1 ); + + // far + + setPoint( 'f1', pointMap, geometry, _camera, - w, - h, 1 ); + setPoint( 'f2', pointMap, geometry, _camera, w, - h, 1 ); + setPoint( 'f3', pointMap, geometry, _camera, - w, h, 1 ); + setPoint( 'f4', pointMap, geometry, _camera, w, h, 1 ); + + // up + + setPoint( 'u1', pointMap, geometry, _camera, w * 0.7, h * 1.1, - 1 ); + setPoint( 'u2', pointMap, geometry, _camera, - w * 0.7, h * 1.1, - 1 ); + setPoint( 'u3', pointMap, geometry, _camera, 0, h * 2, - 1 ); + + // cross + + setPoint( 'cf1', pointMap, geometry, _camera, - w, 0, 1 ); + setPoint( 'cf2', pointMap, geometry, _camera, w, 0, 1 ); + setPoint( 'cf3', pointMap, geometry, _camera, 0, - h, 1 ); + setPoint( 'cf4', pointMap, geometry, _camera, 0, h, 1 ); + + setPoint( 'cn1', pointMap, geometry, _camera, - w, 0, - 1 ); + setPoint( 'cn2', pointMap, geometry, _camera, w, 0, - 1 ); + setPoint( 'cn3', pointMap, geometry, _camera, 0, - h, - 1 ); + setPoint( 'cn4', pointMap, geometry, _camera, 0, h, - 1 ); + + geometry.getAttribute( 'position' ).needsUpdate = true; + + } + + dispose() { + + this.geometry.dispose(); + this.material.dispose(); + + } + +} + + +function setPoint( point, pointMap, geometry, camera, x, y, z ) { + + _vector.set( x, y, z ).unproject( camera ); + + const points = pointMap[ point ]; + + if ( points !== undefined ) { + + const position = geometry.getAttribute( 'position' ); + + for ( let i = 0, l = points.length; i < l; i ++ ) { + + position.setXYZ( points[ i ], _vector.x, _vector.y, _vector.z ); + + } + + } + +} + +const _box = /*@__PURE__*/ new Box3(); + +class BoxHelper extends LineSegments { + + constructor( object, color = 0xffff00 ) { + + const indices = new Uint16Array( [ 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 ] ); + const positions = new Float32Array( 8 * 3 ); + + const geometry = new BufferGeometry(); + geometry.setIndex( new BufferAttribute( indices, 1 ) ); + geometry.setAttribute( 'position', new BufferAttribute( positions, 3 ) ); + + super( geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) ); + + this.object = object; + this.type = 'BoxHelper'; + + this.matrixAutoUpdate = false; + + this.update(); + + } + + update( object ) { + + if ( object !== undefined ) { + + console.warn( 'THREE.BoxHelper: .update() has no longer arguments.' ); + + } + + if ( this.object !== undefined ) { + + _box.setFromObject( this.object ); + + } + + if ( _box.isEmpty() ) return; + + const min = _box.min; + const max = _box.max; + + /* + 5____4 + 1/___0/| + | 6__|_7 + 2/___3/ + + 0: max.x, max.y, max.z + 1: min.x, max.y, max.z + 2: min.x, min.y, max.z + 3: max.x, min.y, max.z + 4: max.x, max.y, min.z + 5: min.x, max.y, min.z + 6: min.x, min.y, min.z + 7: max.x, min.y, min.z + */ + + const position = this.geometry.attributes.position; + const array = position.array; + + array[ 0 ] = max.x; array[ 1 ] = max.y; array[ 2 ] = max.z; + array[ 3 ] = min.x; array[ 4 ] = max.y; array[ 5 ] = max.z; + array[ 6 ] = min.x; array[ 7 ] = min.y; array[ 8 ] = max.z; + array[ 9 ] = max.x; array[ 10 ] = min.y; array[ 11 ] = max.z; + array[ 12 ] = max.x; array[ 13 ] = max.y; array[ 14 ] = min.z; + array[ 15 ] = min.x; array[ 16 ] = max.y; array[ 17 ] = min.z; + array[ 18 ] = min.x; array[ 19 ] = min.y; array[ 20 ] = min.z; + array[ 21 ] = max.x; array[ 22 ] = min.y; array[ 23 ] = min.z; + + position.needsUpdate = true; + + this.geometry.computeBoundingSphere(); + + } + + setFromObject( object ) { + + this.object = object; + this.update(); + + return this; + + } + + copy( source, recursive ) { + + super.copy( source, recursive ); + + this.object = source.object; + + return this; + + } + + dispose() { + + this.geometry.dispose(); + this.material.dispose(); + + } + +} + +class Box3Helper extends LineSegments { + + constructor( box, color = 0xffff00 ) { + + const indices = new Uint16Array( [ 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 ] ); + + const positions = [ 1, 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, - 1, 1, 1, 1, - 1, - 1, 1, - 1, - 1, - 1, - 1, 1, - 1, - 1 ]; + + const geometry = new BufferGeometry(); + + geometry.setIndex( new BufferAttribute( indices, 1 ) ); + + geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) ); + + super( geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) ); + + this.box = box; + + this.type = 'Box3Helper'; + + this.geometry.computeBoundingSphere(); + + } + + updateMatrixWorld( force ) { + + const box = this.box; + + if ( box.isEmpty() ) return; + + box.getCenter( this.position ); + + box.getSize( this.scale ); + + this.scale.multiplyScalar( 0.5 ); + + super.updateMatrixWorld( force ); + + } + + dispose() { + + this.geometry.dispose(); + this.material.dispose(); + + } + +} + +class PlaneHelper extends Line { + + constructor( plane, size = 1, hex = 0xffff00 ) { + + const color = hex; + + const positions = [ 1, - 1, 0, - 1, 1, 0, - 1, - 1, 0, 1, 1, 0, - 1, 1, 0, - 1, - 1, 0, 1, - 1, 0, 1, 1, 0 ]; + + const geometry = new BufferGeometry(); + geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) ); + geometry.computeBoundingSphere(); + + super( geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) ); + + this.type = 'PlaneHelper'; + + this.plane = plane; + + this.size = size; + + const positions2 = [ 1, 1, 0, - 1, 1, 0, - 1, - 1, 0, 1, 1, 0, - 1, - 1, 0, 1, - 1, 0 ]; + + const geometry2 = new BufferGeometry(); + geometry2.setAttribute( 'position', new Float32BufferAttribute( positions2, 3 ) ); + geometry2.computeBoundingSphere(); + + this.add( new Mesh( geometry2, new MeshBasicMaterial( { color: color, opacity: 0.2, transparent: true, depthWrite: false, toneMapped: false } ) ) ); + + } + + updateMatrixWorld( force ) { + + this.position.set( 0, 0, 0 ); + + this.scale.set( 0.5 * this.size, 0.5 * this.size, 1 ); + + this.lookAt( this.plane.normal ); + + this.translateZ( - this.plane.constant ); + + super.updateMatrixWorld( force ); + + } + + dispose() { + + this.geometry.dispose(); + this.material.dispose(); + this.children[ 0 ].geometry.dispose(); + this.children[ 0 ].material.dispose(); + + } + +} + +const _axis = /*@__PURE__*/ new Vector3(); +let _lineGeometry, _coneGeometry; + +class ArrowHelper extends Object3D { + + // dir is assumed to be normalized + + constructor( dir = new Vector3( 0, 0, 1 ), origin = new Vector3( 0, 0, 0 ), length = 1, color = 0xffff00, headLength = length * 0.2, headWidth = headLength * 0.2 ) { + + super(); + + this.type = 'ArrowHelper'; + + if ( _lineGeometry === undefined ) { + + _lineGeometry = new BufferGeometry(); + _lineGeometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 1, 0 ], 3 ) ); + + _coneGeometry = new CylinderGeometry( 0, 0.5, 1, 5, 1 ); + _coneGeometry.translate( 0, - 0.5, 0 ); + + } + + this.position.copy( origin ); + + this.line = new Line( _lineGeometry, new LineBasicMaterial( { color: color, toneMapped: false } ) ); + this.line.matrixAutoUpdate = false; + this.add( this.line ); + + this.cone = new Mesh( _coneGeometry, new MeshBasicMaterial( { color: color, toneMapped: false } ) ); + this.cone.matrixAutoUpdate = false; + this.add( this.cone ); + + this.setDirection( dir ); + this.setLength( length, headLength, headWidth ); + + } + + setDirection( dir ) { + + // dir is assumed to be normalized + + if ( dir.y > 0.99999 ) { + + this.quaternion.set( 0, 0, 0, 1 ); + + } else if ( dir.y < - 0.99999 ) { + + this.quaternion.set( 1, 0, 0, 0 ); + + } else { + + _axis.set( dir.z, 0, - dir.x ).normalize(); + + const radians = Math.acos( dir.y ); + + this.quaternion.setFromAxisAngle( _axis, radians ); + + } + + } + + setLength( length, headLength = length * 0.2, headWidth = headLength * 0.2 ) { + + this.line.scale.set( 1, Math.max( 0.0001, length - headLength ), 1 ); // see #17458 + this.line.updateMatrix(); + + this.cone.scale.set( headWidth, headLength, headWidth ); + this.cone.position.y = length; + this.cone.updateMatrix(); + + } + + setColor( color ) { + + this.line.material.color.set( color ); + this.cone.material.color.set( color ); + + } + + copy( source ) { + + super.copy( source, false ); + + this.line.copy( source.line ); + this.cone.copy( source.cone ); + + return this; + + } + + dispose() { + + this.line.geometry.dispose(); + this.line.material.dispose(); + this.cone.geometry.dispose(); + this.cone.material.dispose(); + + } + +} + +class AxesHelper extends LineSegments { + + constructor( size = 1 ) { + + const vertices = [ + 0, 0, 0, size, 0, 0, + 0, 0, 0, 0, size, 0, + 0, 0, 0, 0, 0, size + ]; + + const colors = [ + 1, 0, 0, 1, 0.6, 0, + 0, 1, 0, 0.6, 1, 0, + 0, 0, 1, 0, 0.6, 1 + ]; + + const geometry = new BufferGeometry(); + geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); + geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) ); + + const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } ); + + super( geometry, material ); + + this.type = 'AxesHelper'; + + } + + setColors( xAxisColor, yAxisColor, zAxisColor ) { + + const color = new Color(); + const array = this.geometry.attributes.color.array; + + color.set( xAxisColor ); + color.toArray( array, 0 ); + color.toArray( array, 3 ); + + color.set( yAxisColor ); + color.toArray( array, 6 ); + color.toArray( array, 9 ); + + color.set( zAxisColor ); + color.toArray( array, 12 ); + color.toArray( array, 15 ); + + this.geometry.attributes.color.needsUpdate = true; + + return this; + + } + + dispose() { + + this.geometry.dispose(); + this.material.dispose(); + + } + +} + +class ShapePath { + + constructor() { + + this.type = 'ShapePath'; + + this.color = new Color(); + + this.subPaths = []; + this.currentPath = null; + + } + + moveTo( x, y ) { + + this.currentPath = new Path(); + this.subPaths.push( this.currentPath ); + this.currentPath.moveTo( x, y ); + + return this; + + } + + lineTo( x, y ) { + + this.currentPath.lineTo( x, y ); + + return this; + + } + + quadraticCurveTo( aCPx, aCPy, aX, aY ) { + + this.currentPath.quadraticCurveTo( aCPx, aCPy, aX, aY ); + + return this; + + } + + bezierCurveTo( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY ) { + + this.currentPath.bezierCurveTo( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY ); + + return this; + + } + + splineThru( pts ) { + + this.currentPath.splineThru( pts ); + + return this; + + } + + toShapes( isCCW ) { + + function toShapesNoHoles( inSubpaths ) { + + const shapes = []; + + for ( let i = 0, l = inSubpaths.length; i < l; i ++ ) { + + const tmpPath = inSubpaths[ i ]; + + const tmpShape = new Shape(); + tmpShape.curves = tmpPath.curves; + + shapes.push( tmpShape ); + + } + + return shapes; + + } + + function isPointInsidePolygon( inPt, inPolygon ) { + + const polyLen = inPolygon.length; + + // inPt on polygon contour => immediate success or + // toggling of inside/outside at every single! intersection point of an edge + // with the horizontal line through inPt, left of inPt + // not counting lowerY endpoints of edges and whole edges on that line + let inside = false; + for ( let p = polyLen - 1, q = 0; q < polyLen; p = q ++ ) { + + let edgeLowPt = inPolygon[ p ]; + let edgeHighPt = inPolygon[ q ]; + + let edgeDx = edgeHighPt.x - edgeLowPt.x; + let edgeDy = edgeHighPt.y - edgeLowPt.y; + + if ( Math.abs( edgeDy ) > Number.EPSILON ) { + + // not parallel + if ( edgeDy < 0 ) { + + edgeLowPt = inPolygon[ q ]; edgeDx = - edgeDx; + edgeHighPt = inPolygon[ p ]; edgeDy = - edgeDy; + + } + + if ( ( inPt.y < edgeLowPt.y ) || ( inPt.y > edgeHighPt.y ) ) continue; + + if ( inPt.y === edgeLowPt.y ) { + + if ( inPt.x === edgeLowPt.x ) return true; // inPt is on contour ? + // continue; // no intersection or edgeLowPt => doesn't count !!! + + } else { + + const perpEdge = edgeDy * ( inPt.x - edgeLowPt.x ) - edgeDx * ( inPt.y - edgeLowPt.y ); + if ( perpEdge === 0 ) return true; // inPt is on contour ? + if ( perpEdge < 0 ) continue; + inside = ! inside; // true intersection left of inPt + + } + + } else { + + // parallel or collinear + if ( inPt.y !== edgeLowPt.y ) continue; // parallel + // edge lies on the same horizontal line as inPt + if ( ( ( edgeHighPt.x <= inPt.x ) && ( inPt.x <= edgeLowPt.x ) ) || + ( ( edgeLowPt.x <= inPt.x ) && ( inPt.x <= edgeHighPt.x ) ) ) return true; // inPt: Point on contour ! + // continue; + + } + + } + + return inside; + + } + + const isClockWise = ShapeUtils.isClockWise; + + const subPaths = this.subPaths; + if ( subPaths.length === 0 ) return []; + + let solid, tmpPath, tmpShape; + const shapes = []; + + if ( subPaths.length === 1 ) { + + tmpPath = subPaths[ 0 ]; + tmpShape = new Shape(); + tmpShape.curves = tmpPath.curves; + shapes.push( tmpShape ); + return shapes; + + } + + let holesFirst = ! isClockWise( subPaths[ 0 ].getPoints() ); + holesFirst = isCCW ? ! holesFirst : holesFirst; + + // console.log("Holes first", holesFirst); + + const betterShapeHoles = []; + const newShapes = []; + let newShapeHoles = []; + let mainIdx = 0; + let tmpPoints; + + newShapes[ mainIdx ] = undefined; + newShapeHoles[ mainIdx ] = []; + + for ( let i = 0, l = subPaths.length; i < l; i ++ ) { + + tmpPath = subPaths[ i ]; + tmpPoints = tmpPath.getPoints(); + solid = isClockWise( tmpPoints ); + solid = isCCW ? ! solid : solid; + + if ( solid ) { + + if ( ( ! holesFirst ) && ( newShapes[ mainIdx ] ) ) mainIdx ++; + + newShapes[ mainIdx ] = { s: new Shape(), p: tmpPoints }; + newShapes[ mainIdx ].s.curves = tmpPath.curves; + + if ( holesFirst ) mainIdx ++; + newShapeHoles[ mainIdx ] = []; + + //console.log('cw', i); + + } else { + + newShapeHoles[ mainIdx ].push( { h: tmpPath, p: tmpPoints[ 0 ] } ); + + //console.log('ccw', i); + + } + + } + + // only Holes? -> probably all Shapes with wrong orientation + if ( ! newShapes[ 0 ] ) return toShapesNoHoles( subPaths ); + + + if ( newShapes.length > 1 ) { + + let ambiguous = false; + let toChange = 0; + + for ( let sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx ++ ) { + + betterShapeHoles[ sIdx ] = []; + + } + + for ( let sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx ++ ) { + + const sho = newShapeHoles[ sIdx ]; + + for ( let hIdx = 0; hIdx < sho.length; hIdx ++ ) { + + const ho = sho[ hIdx ]; + let hole_unassigned = true; + + for ( let s2Idx = 0; s2Idx < newShapes.length; s2Idx ++ ) { + + if ( isPointInsidePolygon( ho.p, newShapes[ s2Idx ].p ) ) { + + if ( sIdx !== s2Idx ) toChange ++; + + if ( hole_unassigned ) { + + hole_unassigned = false; + betterShapeHoles[ s2Idx ].push( ho ); + + } else { + + ambiguous = true; + + } + + } + + } + + if ( hole_unassigned ) { + + betterShapeHoles[ sIdx ].push( ho ); + + } + + } + + } + + if ( toChange > 0 && ambiguous === false ) { + + newShapeHoles = betterShapeHoles; + + } + + } + + let tmpHoles; + + for ( let i = 0, il = newShapes.length; i < il; i ++ ) { + + tmpShape = newShapes[ i ].s; + shapes.push( tmpShape ); + tmpHoles = newShapeHoles[ i ]; + + for ( let j = 0, jl = tmpHoles.length; j < jl; j ++ ) { + + tmpShape.holes.push( tmpHoles[ j ].h ); + + } + + } + + //console.log("shape", shapes); + + return shapes; + + } + +} + +class WebGLMultipleRenderTargets extends WebGLRenderTarget { // @deprecated, r162 + + constructor( width = 1, height = 1, count = 1, options = {} ) { + + console.warn( 'THREE.WebGLMultipleRenderTargets has been deprecated and will be removed in r172. Use THREE.WebGLRenderTarget and set the "count" parameter to enable MRT.' ); + + super( width, height, { ...options, count } ); + + this.isWebGLMultipleRenderTargets = true; + + } + + get texture() { + + return this.textures; + + } + +} + +if ( typeof __THREE_DEVTOOLS__ !== 'undefined' ) { + + __THREE_DEVTOOLS__.dispatchEvent( new CustomEvent( 'register', { detail: { + revision: REVISION, + } } ) ); + +} + +if ( typeof window !== 'undefined' ) { + + if ( window.__THREE__ ) { + + console.warn( 'WARNING: Multiple instances of Three.js being imported.' ); + + } else { + + window.__THREE__ = REVISION; + + } + +} + +export { ACESFilmicToneMapping, AddEquation, AddOperation, AdditiveAnimationBlendMode, AdditiveBlending, AgXToneMapping, AlphaFormat, AlwaysCompare, AlwaysDepth, AlwaysStencilFunc, AmbientLight, AnimationAction, AnimationClip, AnimationLoader, AnimationMixer, AnimationObjectGroup, AnimationUtils, ArcCurve, ArrayCamera, ArrowHelper, AttachedBindMode, Audio, AudioAnalyser, AudioContext, AudioListener, AudioLoader, AxesHelper, BackSide, BasicDepthPacking, BasicShadowMap, BatchedMesh, Bone, BooleanKeyframeTrack, Box2, Box3, Box3Helper, BoxGeometry, BoxHelper, BufferAttribute, BufferGeometry, BufferGeometryLoader, ByteType, Cache, Camera, CameraHelper, CanvasTexture, CapsuleGeometry, CatmullRomCurve3, CineonToneMapping, CircleGeometry, ClampToEdgeWrapping, Clock, Color, ColorKeyframeTrack, ColorManagement, CompressedArrayTexture, CompressedCubeTexture, CompressedTexture, CompressedTextureLoader, ConeGeometry, ConstantAlphaFactor, ConstantColorFactor, CubeCamera, CubeReflectionMapping, CubeRefractionMapping, CubeTexture, CubeTextureLoader, CubeUVReflectionMapping, CubicBezierCurve, CubicBezierCurve3, CubicInterpolant, CullFaceBack, CullFaceFront, CullFaceFrontBack, CullFaceNone, Curve, CurvePath, CustomBlending, CustomToneMapping, CylinderGeometry, Cylindrical, Data3DTexture, DataArrayTexture, DataTexture, DataTextureLoader, DataUtils, DecrementStencilOp, DecrementWrapStencilOp, DefaultLoadingManager, DepthFormat, DepthStencilFormat, DepthTexture, DetachedBindMode, DirectionalLight, DirectionalLightHelper, DiscreteInterpolant, DisplayP3ColorSpace, DodecahedronGeometry, DoubleSide, DstAlphaFactor, DstColorFactor, DynamicCopyUsage, DynamicDrawUsage, DynamicReadUsage, EdgesGeometry, EllipseCurve, EqualCompare, EqualDepth, EqualStencilFunc, EquirectangularReflectionMapping, EquirectangularRefractionMapping, Euler, EventDispatcher, ExtrudeGeometry, FileLoader, Float16BufferAttribute, Float32BufferAttribute, FloatType, Fog, FogExp2, FramebufferTexture, FrontSide, Frustum, GLBufferAttribute, GLSL1, GLSL3, GreaterCompare, GreaterDepth, GreaterEqualCompare, GreaterEqualDepth, GreaterEqualStencilFunc, GreaterStencilFunc, GridHelper, Group, HalfFloatType, HemisphereLight, HemisphereLightHelper, IcosahedronGeometry, ImageBitmapLoader, ImageLoader, ImageUtils, IncrementStencilOp, IncrementWrapStencilOp, InstancedBufferAttribute, InstancedBufferGeometry, InstancedInterleavedBuffer, InstancedMesh, Int16BufferAttribute, Int32BufferAttribute, Int8BufferAttribute, IntType, InterleavedBuffer, InterleavedBufferAttribute, Interpolant, InterpolateDiscrete, InterpolateLinear, InterpolateSmooth, InvertStencilOp, KeepStencilOp, KeyframeTrack, LOD, LatheGeometry, Layers, LessCompare, LessDepth, LessEqualCompare, LessEqualDepth, LessEqualStencilFunc, LessStencilFunc, Light, LightProbe, Line, Line3, LineBasicMaterial, LineCurve, LineCurve3, LineDashedMaterial, LineLoop, LineSegments, LinearDisplayP3ColorSpace, LinearFilter, LinearInterpolant, LinearMipMapLinearFilter, LinearMipMapNearestFilter, LinearMipmapLinearFilter, LinearMipmapNearestFilter, LinearSRGBColorSpace, LinearToneMapping, LinearTransfer, Loader, LoaderUtils, LoadingManager, LoopOnce, LoopPingPong, LoopRepeat, LuminanceAlphaFormat, LuminanceFormat, MOUSE, Material, MaterialLoader, MathUtils, Matrix3, Matrix4, MaxEquation, Mesh, MeshBasicMaterial, MeshDepthMaterial, MeshDistanceMaterial, MeshLambertMaterial, MeshMatcapMaterial, MeshNormalMaterial, MeshPhongMaterial, MeshPhysicalMaterial, MeshStandardMaterial, MeshToonMaterial, MinEquation, MirroredRepeatWrapping, MixOperation, MultiplyBlending, MultiplyOperation, NearestFilter, NearestMipMapLinearFilter, NearestMipMapNearestFilter, NearestMipmapLinearFilter, NearestMipmapNearestFilter, NeutralToneMapping, NeverCompare, NeverDepth, NeverStencilFunc, NoBlending, NoColorSpace, NoToneMapping, NormalAnimationBlendMode, NormalBlending, NotEqualCompare, NotEqualDepth, NotEqualStencilFunc, NumberKeyframeTrack, Object3D, ObjectLoader, ObjectSpaceNormalMap, OctahedronGeometry, OneFactor, OneMinusConstantAlphaFactor, OneMinusConstantColorFactor, OneMinusDstAlphaFactor, OneMinusDstColorFactor, OneMinusSrcAlphaFactor, OneMinusSrcColorFactor, OrthographicCamera, P3Primaries, PCFShadowMap, PCFSoftShadowMap, PMREMGenerator, Path, PerspectiveCamera, Plane, PlaneGeometry, PlaneHelper, PointLight, PointLightHelper, Points, PointsMaterial, PolarGridHelper, PolyhedronGeometry, PositionalAudio, PropertyBinding, PropertyMixer, QuadraticBezierCurve, QuadraticBezierCurve3, Quaternion, QuaternionKeyframeTrack, QuaternionLinearInterpolant, RED_GREEN_RGTC2_Format, RED_RGTC1_Format, REVISION, RGBADepthPacking, RGBAFormat, RGBAIntegerFormat, RGBA_ASTC_10x10_Format, RGBA_ASTC_10x5_Format, RGBA_ASTC_10x6_Format, RGBA_ASTC_10x8_Format, RGBA_ASTC_12x10_Format, RGBA_ASTC_12x12_Format, RGBA_ASTC_4x4_Format, RGBA_ASTC_5x4_Format, RGBA_ASTC_5x5_Format, RGBA_ASTC_6x5_Format, RGBA_ASTC_6x6_Format, RGBA_ASTC_8x5_Format, RGBA_ASTC_8x6_Format, RGBA_ASTC_8x8_Format, RGBA_BPTC_Format, RGBA_ETC2_EAC_Format, RGBA_PVRTC_2BPPV1_Format, RGBA_PVRTC_4BPPV1_Format, RGBA_S3TC_DXT1_Format, RGBA_S3TC_DXT3_Format, RGBA_S3TC_DXT5_Format, RGBFormat, RGB_BPTC_SIGNED_Format, RGB_BPTC_UNSIGNED_Format, RGB_ETC1_Format, RGB_ETC2_Format, RGB_PVRTC_2BPPV1_Format, RGB_PVRTC_4BPPV1_Format, RGB_S3TC_DXT1_Format, RGFormat, RGIntegerFormat, RawShaderMaterial, Ray, Raycaster, Rec709Primaries, RectAreaLight, RedFormat, RedIntegerFormat, ReinhardToneMapping, RenderTarget, RepeatWrapping, ReplaceStencilOp, ReverseSubtractEquation, RingGeometry, SIGNED_RED_GREEN_RGTC2_Format, SIGNED_RED_RGTC1_Format, SRGBColorSpace, SRGBTransfer, Scene, ShaderChunk, ShaderLib, ShaderMaterial, ShadowMaterial, Shape, ShapeGeometry, ShapePath, ShapeUtils, ShortType, Skeleton, SkeletonHelper, SkinnedMesh, Source, Sphere, SphereGeometry, Spherical, SphericalHarmonics3, SplineCurve, SpotLight, SpotLightHelper, Sprite, SpriteMaterial, SrcAlphaFactor, SrcAlphaSaturateFactor, SrcColorFactor, StaticCopyUsage, StaticDrawUsage, StaticReadUsage, StereoCamera, StreamCopyUsage, StreamDrawUsage, StreamReadUsage, StringKeyframeTrack, SubtractEquation, SubtractiveBlending, TOUCH, TangentSpaceNormalMap, TetrahedronGeometry, Texture, TextureLoader, TorusGeometry, TorusKnotGeometry, Triangle, TriangleFanDrawMode, TriangleStripDrawMode, TrianglesDrawMode, TubeGeometry, UVMapping, Uint16BufferAttribute, Uint32BufferAttribute, Uint8BufferAttribute, Uint8ClampedBufferAttribute, Uniform, UniformsGroup, UniformsLib, UniformsUtils, UnsignedByteType, UnsignedInt248Type, UnsignedInt5999Type, UnsignedIntType, UnsignedShort4444Type, UnsignedShort5551Type, UnsignedShortType, VSMShadowMap, Vector2, Vector3, Vector4, VectorKeyframeTrack, VideoTexture, WebGL3DRenderTarget, WebGLArrayRenderTarget, WebGLCoordinateSystem, WebGLCubeRenderTarget, WebGLMultipleRenderTargets, WebGLRenderTarget, WebGLRenderer, WebGLUtils, WebGPUCoordinateSystem, WireframeGeometry, WrapAroundEnding, ZeroCurvatureEnding, ZeroFactor, ZeroSlopeEnding, ZeroStencilOp, createCanvasElement }; diff --git a/src/box-winUI/Assets/home-globe/world-civilizations-globe.html b/src/box-winUI/Assets/home-globe/world-civilizations-globe.html new file mode 100644 index 0000000..7f369bf --- /dev/null +++ b/src/box-winUI/Assets/home-globe/world-civilizations-globe.html @@ -0,0 +1,1026 @@ + + + + + + + 世界文明古国 + + + + +
+
+ +
+

世界文明古国

+

从代表性遗址定位文明坐标,呈现早期国家、城市与知识体系的形成脉络。

+
+ + + + + +
+
+
+ 初始化 3D 场景 + 0% +
+
+
正在从本地资源读取 Three.js、Draco 解码器与地球模型。
+
+
+
+ + + + diff --git a/src/box-winUI/Assets/home-globe/world-civilizations.js b/src/box-winUI/Assets/home-globe/world-civilizations.js new file mode 100644 index 0000000..b8cd0e9 --- /dev/null +++ b/src/box-winUI/Assets/home-globe/world-civilizations.js @@ -0,0 +1,114 @@ +export const WORLD_CIVILIZATIONS = [ + { + id: 'china-erlitou', + name: '中国早期文明', + period: '约公元前 1750-前 1520 年', + site: '二里头遗址(河南偃师)', + lat: 34.6840, + lon: 112.7040, + summary: '二里头遗址常被视为探索夏文化与中国早期国家形成的重要地点。大型宫殿基址、青铜礼器和手工业作坊显示,中原地区在这一时期已经出现较复杂的都邑组织与礼制表达。', + facts: [ + '代表坐标取河南偃师二里头遗址所在区域,作为中国早期王朝文明的地理标记。', + '遗址反映了青铜铸造、玉器制作、道路与宫城布局等早期国家能力。', + '它连接了新石器晚期区域文化与商周礼制传统之间的关键阶段。' + ] + }, + { + id: 'egypt-giza', + name: '古埃及文明', + period: '古王国时期,约公元前 2600-前 2500 年', + site: '吉萨金字塔群', + lat: 29.9792, + lon: 31.1342, + summary: '尼罗河流域孕育了稳定的农业、王权与书写体系。吉萨金字塔群是古埃及国家组织能力、宗教观念与工程技术的集中体现,也是理解法老王权和来世信仰的重要坐标。', + facts: [ + '代表坐标取吉萨高原金字塔群位置。', + '古埃及以象形文字、太阳历、测量学与大型石构建筑闻名。', + '尼罗河周期性泛滥为农业税收、劳役组织和城市发展提供基础。' + ] + }, + { + id: 'mesopotamia-uruk', + name: '苏美尔与两河文明', + period: '约公元前 4000-前 3100 年形成早期城市', + site: '乌鲁克遗址', + lat: 31.3222, + lon: 45.6361, + summary: '幼发拉底河与底格里斯河之间的冲积平原出现了世界最早的城市化浪潮之一。乌鲁克以神庙区、行政印章、泥板记事和复杂分工,展示了城市、文字和制度同步成长的过程。', + facts: [ + '代表坐标取伊拉克南部乌鲁克遗址位置。', + '楔形文字的早期形态与会计、仓储和行政管理密切相关。', + '城邦、神庙经济与灌溉农业共同塑造了两河文明的社会结构。' + ] + }, + { + id: 'indus-mohenjo', + name: '印度河文明', + period: '约公元前 2600-前 1900 年', + site: '摩亨佐-达罗', + lat: 27.3247, + lon: 68.1350, + summary: '印度河文明以规划严整的城市、排水系统、标准化度量衡和远距离贸易著称。摩亨佐-达罗呈现出成熟城市社会的公共工程能力,也保留了仍待破译的印章文字线索。', + facts: [ + '代表坐标取巴基斯坦信德省摩亨佐-达罗遗址位置。', + '城市街区、排水设施和公共浴池体现了高度组织化的城市管理。', + '该文明与两河地区存在贸易往来,出土印章显示了复杂的商业网络。' + ] + }, + { + id: 'greece-acropolis', + name: '古希腊文明', + period: '公元前 5 世纪达到古典时期高峰', + site: '雅典卫城', + lat: 37.9715, + lon: 23.7257, + summary: '古希腊城邦孕育了哲学、戏剧、史学、数学与公民政治传统。雅典卫城是古典时代建筑、雕刻和城邦公共精神的代表性空间,集中体现了希腊文明对地中海世界的影响。', + facts: [ + '代表坐标取雅典卫城位置。', + '帕特农神庙体现了古典柱式、比例和雕塑叙事的综合成就。', + '城邦制度、哲学辩论和公共演说影响了后来的政治与思想史。' + ] + }, + { + id: 'persia-persepolis', + name: '古波斯文明', + period: '阿契美尼德王朝,约公元前 518 年起营建', + site: '波斯波利斯', + lat: 29.9355, + lon: 52.8916, + summary: '阿契美尼德波斯连接西亚、中亚、埃及和小亚细亚,形成跨区域帝国治理体系。波斯波利斯的台基、宫殿浮雕和朝贡图像体现了多民族帝国的政治秩序与礼仪视觉。', + facts: [ + '代表坐标取伊朗法尔斯省波斯波利斯遗址位置。', + '帝国道路、驿站和行政区划支撑了广阔疆域的治理。', + '浮雕中不同地区使节形象反映了多元族群被纳入王权秩序。' + ] + }, + { + id: 'maya-tikal', + name: '玛雅文明', + period: '公元前后至公元 9 世纪繁盛', + site: '蒂卡尔', + lat: 17.2220, + lon: -89.6237, + summary: '玛雅文明在中美洲发展出复杂历法、象形文字、天文观测和城邦网络。蒂卡尔拥有高耸金字塔神庙、王朝铭文和城市广场,是理解古典期玛雅政治与宇宙观的重要遗址。', + facts: [ + '代表坐标取危地马拉北部蒂卡尔遗址位置。', + '玛雅长纪历与天文观测体系对金星周期、日食和仪式时间有精细记录。', + '城市之间既有联盟与贸易,也存在长期战争和王权竞争。' + ] + }, + { + id: 'rome-forum', + name: '古罗马文明', + period: '共和国至帝国时期核心公共空间', + site: '罗马广场', + lat: 41.8925, + lon: 12.4853, + summary: '罗马从城邦扩展为跨地中海帝国,法律、道路、军团、城市工程和拉丁文化影响深远。罗马广场是政治集会、司法、宗教仪式与纪念建筑集中的核心空间。', + facts: [ + '代表坐标取意大利罗马广场位置。', + '罗马道路、渡槽、拱券和混凝土工程改变了城市基础设施形态。', + '罗马法和共和制度记忆对后世欧洲政治法律传统影响长期存在。' + ] + } +]; diff --git a/src/box-winUI/Assets/startup-splash/index.html b/src/box-winUI/Assets/startup-splash/index.html new file mode 100644 index 0000000..fa32148 --- /dev/null +++ b/src/box-winUI/Assets/startup-splash/index.html @@ -0,0 +1,43 @@ + + + + + + YMhut Box Startup + + + +
+
+
+ +
+

YMhut Box

+

启动自检

+
+
0%
+
+ + + +
+ +
+

正在准备启动自检...

+ 请稍候,正在载入必要组件。 +
+
+ + +
+
+ + + diff --git a/src/box-winUI/Assets/startup-splash/splash.css b/src/box-winUI/Assets/startup-splash/splash.css new file mode 100644 index 0000000..5a70d86 --- /dev/null +++ b/src/box-winUI/Assets/startup-splash/splash.css @@ -0,0 +1,248 @@ +:root { + color-scheme: light dark; + --bg: #f6f8fb; + --surface: #ffffff; + --surface-alt: #eef3f7; + --stroke: #d6dde5; + --text: #111827; + --muted: #536171; + --accent: #2563eb; + --accent-soft: #dbeafe; + --warning: #b45309; + --danger: #b91c1c; + --radius: 22px; + --shadow: none; +} + +body[data-theme="dark"] { + --bg: #111418; + --surface: #1b1f25; + --surface-alt: #252b33; + --stroke: #39414d; + --text: #f8fafc; + --muted: #b4bdc9; + --accent: #60a5fa; + --accent-soft: #1e3a5f; + --warning: #f59e0b; + --danger: #f87171; + --shadow: none; +} + +@media (forced-colors: active) { + :root { + --bg: Canvas; + --surface: Canvas; + --surface-alt: Canvas; + --stroke: ButtonBorder; + --text: CanvasText; + --muted: CanvasText; + --accent: Highlight; + --accent-soft: Canvas; + --warning: CanvasText; + --danger: CanvasText; + --shadow: none; + } +} + +* { + box-sizing: border-box; +} + +html, +body { + width: 100%; + height: 100%; + margin: 0; +} + +body { + overflow: hidden; + background: transparent; + color: var(--text); + font-family: "Segoe UI Variable", "Segoe UI", system-ui, sans-serif; +} + +.splash { + width: 100%; + height: 100%; + display: grid; + place-items: stretch; + padding: 0; + overflow: hidden; + border-radius: var(--radius); + background: transparent; +} + +.shell { + width: 100%; + height: 100%; + display: grid; + gap: 20px; + padding: 24px; + border: 1px solid var(--stroke); + border-radius: var(--radius); + background: var(--surface); + box-shadow: var(--shadow); + overflow: hidden; +} + +.brand { + display: grid; + grid-template-columns: auto 1fr auto; + gap: 16px; + align-items: center; +} + +.mark { + position: relative; + width: 64px; + height: 64px; + display: grid; + place-items: center; + border-radius: 14px; + border: 1px solid var(--stroke); + background: var(--accent-soft); + color: var(--accent); + font-size: 28px; + font-weight: 700; +} + +.ring { + position: absolute; + inset: 7px; + border: 2px solid color-mix(in srgb, var(--accent) 30%, transparent); + border-top-color: var(--accent); + border-radius: 50%; + animation: spin 1.8s linear infinite; +} + +.brandText h1 { + margin: 0; + color: var(--text); + font-size: 28px; + font-weight: 650; + letter-spacing: 0; +} + +.brandText p { + margin: 4px 0 0; + color: var(--muted); + font-size: 14px; + font-weight: 500; +} + +.percent { + min-width: 54px; + text-align: right; + color: var(--accent); + font-size: 17px; + font-weight: 650; +} + +.progressWrap { + display: grid; + gap: 8px; +} + +.progressTrack { + height: 8px; + overflow: hidden; + border-radius: 999px; + background: var(--surface-alt); + border: 1px solid var(--stroke); +} + +.progressFill { + width: 0%; + height: 100%; + border-radius: inherit; + background: var(--accent); +} + +.statusRow { + display: grid; + grid-template-columns: auto 1fr; + gap: 10px; + align-items: start; +} + +.stateDot { + width: 10px; + height: 10px; + margin-top: 6px; + border-radius: 50%; + background: var(--accent); + box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 16%, transparent); + animation: pulse 1.6s ease-in-out infinite; +} + +.stateDot.warning { + background: var(--warning); + box-shadow: 0 0 0 4px color-mix(in srgb, var(--warning) 18%, transparent); +} + +.stateDot.danger { + background: var(--danger); + box-shadow: 0 0 0 4px color-mix(in srgb, var(--danger) 18%, transparent); +} + +.statusText p { + margin: 0; + color: var(--text); + font-size: 15px; + font-weight: 600; +} + +.statusText span { + display: block; + margin-top: 4px; + color: var(--muted); + font-size: 12.5px; + line-height: 1.45; +} + +.closeButton { + display: none; + justify-self: end; + min-height: 34px; + padding: 6px 14px; + border: 1px solid var(--stroke); + border-radius: 7px; + background: var(--surface-alt); + color: var(--text); + font: inherit; + font-size: 13px; + font-weight: 600; +} + +.closeButton.visible { + display: inline-flex; +} + +.closeButton:focus-visible { + outline: 2px solid var(--accent); + outline-offset: 2px; +} + +@keyframes spin { + to { + transform: rotate(360deg); + } +} + +@keyframes pulse { + 0%, + 100% { + opacity: 0.75; + } + 50% { + opacity: 1; + } +} + +@media (prefers-reduced-motion: reduce) { + .ring, + .stateDot { + animation: none; + } +} diff --git a/src/box-winUI/Assets/startup-splash/splash.js b/src/box-winUI/Assets/startup-splash/splash.js new file mode 100644 index 0000000..6a07d45 --- /dev/null +++ b/src/box-winUI/Assets/startup-splash/splash.js @@ -0,0 +1,127 @@ +(function () { + const progressFill = document.getElementById('progressFill'); + const percent = document.getElementById('percent'); + const status = document.getElementById('status'); + const detail = document.getElementById('detail'); + const stageName = document.getElementById('stageName'); + const stateDot = document.getElementById('stateDot'); + const closeButton = document.getElementById('closeButton'); + + let currentProgress = 0; + let targetProgress = 0; + let reducedMotion = false; + let frame = 0; + + function clamp(value, min, max) { + return Math.max(min, Math.min(max, Number.isFinite(value) ? value : min)); + } + + function setProgress(value) { + targetProgress = clamp(value, 0, 100); + if (reducedMotion) { + currentProgress = targetProgress; + renderProgress(); + return; + } + + if (!frame) { + frame = requestAnimationFrame(tick); + } + } + + function tick() { + const diff = targetProgress - currentProgress; + if (Math.abs(diff) > 0.15) { + currentProgress += diff * 0.12; + renderProgress(); + frame = requestAnimationFrame(tick); + return; + } + + currentProgress = targetProgress; + renderProgress(); + frame = 0; + } + + function renderProgress() { + const value = clamp(currentProgress, 0, 100); + progressFill.style.width = `${value}%`; + percent.textContent = `${Math.floor(value)}%`; + } + + function applySeverity(severity) { + stateDot.classList.remove('warning', 'danger'); + if (severity === 'critical' || severity === 'danger' || severity === 'error') { + stateDot.classList.add('danger'); + } else if (severity === 'warning') { + stateDot.classList.add('warning'); + } + } + + function applyTheme(theme) { + const normalized = String(theme || '').toLowerCase(); + if (normalized === 'dark' || normalized === 'light') { + document.body.dataset.theme = normalized; + } + } + + function handleMessage(message) { + if (!message || typeof message !== 'object') { + return; + } + + if (message.theme) { + applyTheme(message.theme); + } + + if (typeof message.reducedMotion === 'boolean') { + reducedMotion = message.reducedMotion; + document.body.classList.toggle('reducedMotion', reducedMotion); + } + + switch (message.type) { + case 'progress': + stageName.textContent = message.stageName || '启动自检'; + status.textContent = message.status || message.stageName || '正在检查...'; + detail.textContent = message.detail || `${message.stepIndex || 0}/${message.stepCount || 0}`; + applySeverity(message.severity); + setProgress(message.isIndeterminate ? targetProgress : message.progress); + break; + case 'complete': + stageName.textContent = '启动自检完成'; + status.textContent = message.hasIssues ? '发现问题' : '未发现问题'; + detail.textContent = message.hasIssues + ? `发现 ${message.visibleIssueCount || 0} 个需要关注的问题,详情可在结果页查看。` + : '本次启动预检未发现需要处理的问题。'; + applySeverity(message.hasIssues ? 'warning' : 'info'); + setProgress(100); + break; + case 'fatal': + stageName.textContent = '启动失败'; + status.textContent = message.status || '启动初始化失败'; + detail.textContent = message.detail || '请查看启动日志或重新安装。'; + applySeverity('critical'); + setProgress(100); + closeButton.classList.add('visible'); + break; + case 'themeChanged': + applyTheme(message.theme); + break; + case 'showClose': + closeButton.classList.add('visible'); + break; + default: + break; + } + } + + window.chrome?.webview?.addEventListener('message', event => handleMessage(event.data)); + closeButton.addEventListener('click', () => { + window.chrome?.webview?.postMessage({ type: 'closeRequested' }); + }); + + const prefersReduced = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches; + reducedMotion = prefersReduced; + renderProgress(); + window.chrome?.webview?.postMessage({ type: 'ready' }); +})(); diff --git a/src/box-winUI/Assets/tool-pages/index.html b/src/box-winUI/Assets/tool-pages/index.html new file mode 100644 index 0000000..ad5bcd8 --- /dev/null +++ b/src/box-winUI/Assets/tool-pages/index.html @@ -0,0 +1,18 @@ + + + + + + YMhut Tool Page + + + +
+
+
+

正在加载工具界面...

+
+
+ + + diff --git a/src/box-winUI/Assets/tool-pages/tool-page.css b/src/box-winUI/Assets/tool-pages/tool-page.css new file mode 100644 index 0000000..df348b0 --- /dev/null +++ b/src/box-winUI/Assets/tool-pages/tool-page.css @@ -0,0 +1,1155 @@ +:root { + color-scheme: light; + --bg: #f7f8fa; + --panel: rgba(255, 255, 255, .92); + --panel-strong: #ffffff; + --soft: #f1f5f9; + --line: rgba(15, 23, 42, .12); + --text: #111827; + --muted: #5b6472; + --accent: #0078d4; + --accent-soft: rgba(0, 120, 212, .11); + --danger: #c42b1c; + --success: #107c10; + --warn: #f7630c; + --radius: 8px; + font-family: "Segoe UI", "Microsoft YaHei UI", Arial, sans-serif; +} + +html[data-theme="dark"] { + color-scheme: dark; + --bg: #101114; + --panel: rgba(31, 31, 35, .88); + --panel-strong: #25262b; + --soft: rgba(255, 255, 255, .07); + --line: rgba(255, 255, 255, .14); + --text: #f7f7f8; + --muted: #a7adb8; + --accent-soft: rgba(96, 165, 250, .16); +} + +* { box-sizing: border-box; } + +.drag-light *, +.drag-light *::before, +.drag-light *::after { + animation: none !important; + transition: none !important; + scroll-behavior: auto !important; +} + +html { + min-height: 100%; + overflow: auto; +} + +body { + margin: 0; + min-height: 100vh; + background: var(--bg); + color: var(--text); + overflow: auto; +} + +button, input, textarea, select { + font: inherit; +} + +button { + min-height: 34px; + border: 1px solid var(--line); + border-radius: 7px; + background: var(--panel-strong); + color: var(--text); + padding: 7px 12px; + cursor: pointer; + transition: border-color 160ms ease, background-color 160ms ease, color 160ms ease; +} + +button:hover, button:focus-visible { + border-color: var(--accent); + outline: 2px solid color-mix(in srgb, var(--accent) 24%, transparent); + outline-offset: 1px; +} + +button.primary { + color: #fff; + background: var(--accent); + border-color: var(--accent); +} + +button.ghost { + background: transparent; +} + +button:disabled { + opacity: .56; + cursor: default; +} + +.page { + display: grid; + gap: 14px; +} + +.tool-chrome, +.surface, +.result-surface, +.result-card, +.result-details { + border: 1px solid var(--line); + border-radius: var(--radius); + background: var(--panel); +} + +.tool-chrome { + display: grid; + grid-template-columns: minmax(0, 1fr) auto; + gap: 14px; + align-items: center; + padding: 14px 16px; +} + +.tool-identity { + display: flex; + align-items: center; + gap: 12px; + min-width: 0; +} + +.tool-mark { + width: 40px; + height: 40px; + display: grid; + place-items: center; + border-radius: 10px; + color: var(--accent); + background: var(--accent-soft); + border: 1px solid color-mix(in srgb, var(--accent) 34%, transparent); + font-weight: 800; + flex: 0 0 auto; +} + +.tool-title, +.surface-head, +.result-toolbar { + min-width: 0; +} + +.tool-title h1, +.surface-head h2, +.result-toolbar h2 { + font-size: clamp(1rem, 1.25vw, 1.35rem); + line-height: 1.18; + margin: 0; +} + +.meta-row, +.action-row, +.result-actions, +.link-actions, +.segmented, +.toolbar-chips { + display: flex; + flex-wrap: wrap; + gap: 8px; +} + +.tool-actions, +.result-actions { + justify-content: flex-end; + align-items: center; +} + +.quiet-badge, +.status, +.badge, +.toggle-chip { + display: inline-flex; + align-items: center; + min-height: 24px; + padding: 0 8px; + border-radius: 8px; + border: 1px solid color-mix(in srgb, var(--accent) 26%, transparent); + background: color-mix(in srgb, var(--accent-soft) 90%, transparent); + color: var(--accent); + font-size: .74rem; + font-weight: 700; +} + +.tool-chrome .status { + color: var(--muted); + background: color-mix(in srgb, var(--panel-strong) 82%, var(--soft)); +} + +.workbench { + display: grid; + grid-template-columns: minmax(320px, .42fr) minmax(0, 1fr); + gap: 14px; +} + +.surface, +.result-surface, +.result-card, +.result-details { + padding: 14px; +} + +.surface, +.result-surface { + display: grid; + gap: 12px; + align-content: start; +} + +.surface-head, +.result-toolbar, +.card-head { + display: flex; + justify-content: space-between; + gap: 10px; + align-items: flex-start; +} + +.compact-copy, +.option-help, +.inline-note { + line-height: 1.45; +} + +.input-grid, +.field-grid, +.options-stack { + display: grid; + gap: 10px; +} + +.field-grid { + grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); +} + +.field { + display: grid; + gap: 6px; +} + +.field.primary-field textarea.editor { + min-height: 220px; +} + +.options-drawer { + border: 1px solid var(--line); + border-radius: 10px; + background: color-mix(in srgb, var(--panel-strong) 84%, var(--soft)); + padding: 10px 12px; +} + +.options-drawer > summary { + list-style: none; + display: flex; + align-items: center; + justify-content: space-between; + gap: 10px; + cursor: pointer; + font-weight: 700; + color: var(--text); +} + +.options-drawer > summary::-webkit-details-marker { + display: none; +} + +.options-inline { + display: grid; + gap: 10px; +} + +.rule-field { + display: grid; + gap: 6px; +} + +.segmented { + display: inline-flex; + flex-wrap: wrap; + gap: 6px; +} + +.segmented button, +.toggle-chip { + min-height: 30px; + padding: 5px 10px; + background: var(--panel-strong); +} + +.segmented button.active, +.toggle-chip.active { + border-color: var(--accent); + background: var(--accent-soft); + color: var(--accent); +} + +.result-toolbar { + padding-bottom: 4px; + border-bottom: 1px solid var(--line); +} + +.result-details { + padding: 0; + overflow: hidden; +} + +.result-details > summary { + cursor: pointer; + list-style: none; + display: flex; + justify-content: space-between; + gap: 10px; + align-items: center; + padding: 12px 14px; + font-weight: 700; +} + +.result-details > summary::-webkit-details-marker { + display: none; +} + +.result-details[open] > summary { + border-bottom: 1px solid var(--line); +} + +.result-details .metrics, +.result-details .raw-card { + margin: 0; + padding: 14px; + border: 0; + border-top: 1px solid var(--line); + border-radius: 0; +} + +.empty-state { + min-height: 280px; + display: grid; + place-content: center; + gap: 8px; + text-align: center; + color: var(--muted); +} + +.fatal { + min-height: 50vh; +} + +.error-card { + border-color: color-mix(in srgb, var(--danger) 44%, var(--line)); +} + +textarea, input, select { + width: 100%; + color: var(--text); + background: var(--panel-strong); + border: 1px solid var(--line); + border-radius: 8px; + padding: 10px 12px; + outline: none; +} + +.compact-copy, +.muted, +.result-toolbar p, +.surface-head p { + color: var(--muted); +} + +textarea, +input, +select, +button { + font: inherit; +} + +textarea { + min-height: 140px; + resize: vertical; + font-family: "Cascadia Mono", Consolas, monospace; + line-height: 1.5; +} + +textarea.editor { + min-height: 220px; +} + +input:focus, +textarea:focus, +select:focus { + border-color: var(--accent); + box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 18%, transparent); +} + +input[type="color"] { + min-height: 46px; + padding: 6px; + cursor: pointer; +} + +button.primary, +button.active { + color: #fff; + background: var(--accent); + border-color: var(--accent); +} + +button.ghost { + background: transparent; +} + +button.compact { + min-height: 30px; + padding: 5px 10px; +} + +.tool-actions button, +.action-row button, +.result-actions button { + white-space: nowrap; +} + +.shell { + min-height: 100vh; + padding: clamp(12px, 2vw, 24px); +} + +.page { + display: grid; + gap: 14px; +} + +.hero, .workspace, .panel, .result-card { + border: 1px solid var(--line); + border-radius: var(--radius); + background: var(--panel); +} + +.hero { + display: grid; + grid-template-columns: minmax(0, 1fr) auto; + gap: 16px; + padding: clamp(14px, 2vw, 20px); + align-items: center; +} + +.identity { + display: flex; + align-items: center; + gap: 12px; + min-width: 0; +} + +.mark { + width: 48px; + height: 48px; + display: grid; + place-items: center; + border-radius: var(--radius); + color: var(--accent); + background: var(--accent-soft); + border: 1px solid color-mix(in srgb, var(--accent) 36%, transparent); + font-weight: 800; +} + +.title { + min-width: 0; +} + +h1, h2, h3, p { margin: 0; } +h1 { font-size: clamp(1.3rem, 2.6vw, 2rem); line-height: 1.12; } +h2 { font-size: 1rem; } +.muted, .eyebrow { color: var(--muted); } +.eyebrow { font-size: .78rem; font-weight: 700; text-transform: uppercase; letter-spacing: .02em; } + +.hero-actions, .actions, .chips { + display: flex; + gap: 8px; + flex-wrap: wrap; +} + +.hero-actions { justify-content: flex-end; } + +.workspace { + display: grid; + grid-template-columns: minmax(280px, .42fr) minmax(0, 1fr); + gap: 14px; + padding: 14px; + background: color-mix(in srgb, var(--panel) 72%, var(--soft)); +} + +.panel { + padding: 14px; + display: grid; + gap: 12px; + align-content: start; +} + +.panel-head { + display: flex; + justify-content: space-between; + gap: 10px; + align-items: flex-start; +} + +.input-grid, .field-grid { + display: grid; + gap: 10px; +} + +.field-grid { + grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); +} + +.field { + display: grid; + gap: 5px; +} + +label { + font-size: .82rem; + color: var(--muted); + font-weight: 700; +} + +textarea, input, select { + width: 100%; + color: var(--text); + background: var(--panel-strong); + border: 1px solid var(--line); + border-radius: 7px; + padding: 9px 10px; + outline: none; +} + +textarea { + min-height: 132px; + resize: vertical; + font-family: "Cascadia Mono", Consolas, monospace; + line-height: 1.5; +} + +textarea.editor { + min-height: 230px; +} + +input:focus, textarea:focus, select:focus { + border-color: var(--accent); + box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 18%, transparent); +} + +input[type="color"] { + min-height: 48px; + padding: 5px; + cursor: pointer; +} + +.option-grid { + display: grid; + gap: 8px; + grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); +} + +.option { + min-height: 42px; + text-align: left; +} + +.option.active { + border-color: var(--accent); + background: var(--accent-soft); + color: var(--accent); + font-weight: 700; +} + +.status { + display: flex; + align-items: center; + gap: 8px; + min-height: 32px; + color: var(--muted); +} + +.dot { + width: 8px; + height: 8px; + border-radius: 99px; + background: var(--accent); +} + +.dot.running { animation: pulse 1.1s ease-in-out infinite; } +.dot.error { background: var(--danger); } +.dot.success { background: var(--success); } +.dot.canceled { background: var(--muted); } + +.result-area { + display: grid; + gap: 12px; + align-content: start; +} + +.metrics { + display: grid; + grid-template-columns: repeat(4, minmax(0, 1fr)); + gap: 8px; +} + +.runtime-metrics { + opacity: .82; +} + +.metric, .kv, .list-item { + border: 1px solid var(--line); + border-radius: 7px; + background: var(--panel-strong); +} + +.metric { + padding: 10px; + min-height: 68px; +} + +.metric strong { + display: block; + margin-top: 4px; + font-size: 1.35rem; +} + +.result-card { + padding: 12px; + overflow: hidden; +} + +.result-card.priority-high, +.result-card.kind-metric.priority-high { + border-color: color-mix(in srgb, var(--accent) 38%, var(--line)); + background: linear-gradient(180deg, color-mix(in srgb, var(--panel-strong) 84%, var(--accent-soft)), var(--panel)); +} + +.result-card.mode-source, +.result-card.priority-low, +.raw-card { + background: color-mix(in srgb, var(--panel) 84%, var(--soft)); + color: color-mix(in srgb, var(--text) 82%, var(--muted)); +} + +.card-head { + display: flex; + justify-content: space-between; + gap: 12px; + margin-bottom: 10px; + align-items: center; +} + +.card-title { + display: flex; + align-items: center; + gap: 8px; + min-width: 0; +} + +.block-icon { + width: 20px; + height: 20px; + flex: 0 0 auto; + color: var(--accent); +} + +.badge { + display: inline-flex; + align-items: center; + min-height: 22px; + padding: 0 7px; + border-radius: 7px; + border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent); + color: var(--accent); + background: var(--accent-soft); + font-size: .74rem; + font-weight: 700; +} + +.kv-grid { + display: grid; + grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); + gap: 8px; +} + +.kv { + padding: 10px; + min-width: 0; +} + +.kv span, .list-item span { + display: block; + color: var(--muted); + font-size: .76rem; +} + +.kv strong, .list-item strong { + display: block; + word-break: break-word; +} + +.metric-grid .primary-kv { + grid-column: 1 / -1; + min-height: 104px; + display: grid; + align-content: center; + border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); + background: linear-gradient(135deg, color-mix(in srgb, var(--accent-soft) 72%, var(--panel-strong)), var(--panel-strong)); +} + +.metric-grid .primary-kv span { + font-size: .82rem; +} + +.metric-grid .primary-kv strong { + margin-top: 6px; + color: var(--accent); + font-size: clamp(2rem, 5vw, 3.2rem); + line-height: 1.08; + letter-spacing: 0; +} + +.source-kv strong { + color: var(--muted); + font-weight: 600; +} + +.table-wrap, .code-wrap { + overflow: auto; + border: 1px solid var(--line); + border-radius: 7px; + background: var(--panel-strong); +} + +table { + width: 100%; + min-width: 520px; + border-collapse: collapse; +} + +td, th { + border-bottom: 1px solid var(--line); + padding: 9px; + text-align: left; + vertical-align: top; +} + +th { + color: var(--accent); + font-weight: 700; + background: color-mix(in srgb, var(--accent-soft) 55%, transparent); +} + +.list { + display: grid; + gap: 8px; +} + +.list-item { + display: grid; + grid-template-columns: minmax(34px, auto) minmax(0, 1fr) auto; + gap: 10px; + align-items: start; + padding: 10px; +} + +.ranked-list { + grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); +} + +.card-list { + grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); +} + +.ranked-item, +.card-list .list-item { + grid-template-columns: 44px minmax(0, 1fr); + min-height: 82px; + border-color: color-mix(in srgb, var(--accent) 20%, var(--line)); + background: linear-gradient(180deg, var(--panel-strong), color-mix(in srgb, var(--panel-strong) 82%, var(--accent-soft))); +} + +.list-item.clickable { + cursor: pointer; +} + +.list-item.clickable:hover, +.list-item.clickable:focus-visible { + border-color: color-mix(in srgb, var(--accent) 52%, var(--line)); + background: color-mix(in srgb, var(--panel-strong) 82%, var(--accent-soft)); + outline: 2px solid color-mix(in srgb, var(--accent) 20%, transparent); + outline-offset: 1px; +} + +.rank-badge { + width: 34px; + height: 34px; + position: relative; + display: inline-grid; + place-items: center; + border-radius: 7px; + overflow: hidden; + border: 1px solid color-mix(in srgb, var(--accent) 38%, transparent); + color: var(--accent); + background: var(--accent-soft); + font-weight: 800; + font-size: .86rem; +} + +.rank-badge::before { + content: ""; + position: absolute; + inset: -42%; + opacity: 0; + transform: translateX(-45%) rotate(24deg); + background: linear-gradient(90deg, transparent, rgba(255, 255, 255, .72), transparent); +} + +.rank-core { + position: relative; + z-index: 1; +} + +.rank-1 { + color: #4a3300; + border-color: color-mix(in srgb, #d99b16 72%, #fff); + background: linear-gradient(145deg, #fff2b8, #e1a21c 58%, #9f6611); + box-shadow: 0 0 0 2px color-mix(in srgb, #d99b16 18%, transparent); +} + +.rank-2 { + color: #26303b; + border-color: color-mix(in srgb, #8b95a3 72%, #fff); + background: linear-gradient(145deg, #f9fbff, #b8c0cc 58%, #687386); +} + +.rank-3 { + color: #3e1f0d; + border-color: color-mix(in srgb, #b7652d 72%, #fff); + background: linear-gradient(145deg, #ffe0c2, #c87536 58%, #7e3c1c); +} + +.rank-badge.plain { + border-radius: 7px; +} + +.item-body { + min-width: 0; +} + +.item-body strong { + display: -webkit-box; + -webkit-line-clamp: 2; + -webkit-box-orient: vertical; + overflow: hidden; +} + +.link-actions { + display: flex; + flex-wrap: wrap; + gap: 8px; + justify-content: flex-end; +} + +button.compact { + min-height: 30px; + padding: 5px 9px; + color: var(--muted); +} + +.item-subtitle { + margin-top: 4px; + white-space: pre-line; + display: -webkit-box; + -webkit-line-clamp: 3; + -webkit-box-orient: vertical; + overflow: hidden; +} + +.status-error { + border-color: color-mix(in srgb, var(--danger) 45%, var(--line)); +} + +.status-ok { + border-color: color-mix(in srgb, var(--success) 45%, var(--line)); +} + +.raw-card summary { + cursor: pointer; + margin-bottom: 0; +} + +.raw-card[open] summary { + margin-bottom: 10px; +} + +pre { + margin: 0; + white-space: pre-wrap; + word-break: break-word; + padding: 12px; + font-family: "Cascadia Mono", Consolas, monospace; + font-size: .86rem; + line-height: 1.55; +} + +.media-frame { + display: grid; + place-items: center; + min-height: 260px; + border: 1px solid var(--line); + border-radius: 7px; + background: + linear-gradient(45deg, color-mix(in srgb, var(--soft) 74%, transparent) 25%, transparent 25%), + linear-gradient(-45deg, color-mix(in srgb, var(--soft) 74%, transparent) 25%, transparent 25%), + linear-gradient(45deg, transparent 75%, color-mix(in srgb, var(--soft) 74%, transparent) 75%), + linear-gradient(-45deg, transparent 75%, color-mix(in srgb, var(--soft) 74%, transparent) 75%); + background-size: 24px 24px; + background-position: 0 0, 0 12px, 12px -12px, -12px 0; + overflow: hidden; +} + +img, video { + display: block; + max-width: 100%; + max-height: 420px; + margin: auto; + border-radius: 7px; +} + +.media-frame img { + width: min(360px, 86%); + height: auto; + object-fit: contain; + background: #fff; + padding: 14px; +} + +.swatch { + min-height: 150px; + border-radius: 7px; + border: 1px solid var(--line); +} + +.empty { + min-height: 50vh; + display: grid; + place-content: center; + gap: 10px; + color: var(--muted); + text-align: center; +} + +.pulse { + width: 14px; + height: 14px; + margin: auto; + border-radius: 99px; + background: var(--accent); +} + +.page.editor-workbench .workspace { + grid-template-columns: minmax(340px, .52fr) minmax(0, .48fr); +} + +.page.editor-workbench textarea.editor, +.page.secret-entry input { + background: color-mix(in srgb, var(--panel-strong) 86%, #0f172a); + border-color: color-mix(in srgb, var(--accent) 28%, var(--line)); +} + +.page.search-console .input-panel, +.page.preset-browser .input-panel { + border-top: 3px solid var(--accent); +} + +.page.numeric-calculator .field-grid, +.page.date-range-form .field-grid { + grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); +} + +.page.numeric-calculator .field input, +.page.date-range-form .field input { + min-height: 44px; + font-weight: 700; +} + +.page.file-dropzone .input-panel { + border-style: dashed; + background: color-mix(in srgb, var(--panel) 78%, var(--accent-soft)); +} + +.page.file-dropzone .field input { + min-height: 52px; + border-style: dashed; +} + +.page.color-studio .mark, +.page.color-studio .swatch { + box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--accent) 36%, transparent); +} + +.page.auto-dashboard .input-panel { + align-content: center; + min-height: 220px; +} + +.page.structured-json-tree .code-wrap, +.page.code-code-panel .code-wrap { + background: #101827; + color: #dbeafe; + border-color: rgba(96, 165, 250, .28); +} + +.page.structured-json-tree pre, +.page.code-code-panel pre { + font-size: .82rem; +} + +.page.calculation-data-table .metric, +.page.calculator-data-table .metric { + border-left: 3px solid var(--accent); +} + +.page.live-news-board .result-card:not(.mode-source):not(.raw-card), +.page.reference-ranked-lane .result-card:not(.mode-source):not(.raw-card), +.page.live-ranked-lane .result-card:not(.mode-source):not(.raw-card) { + background: linear-gradient(180deg, color-mix(in srgb, var(--panel) 88%, var(--accent-soft)), var(--panel)); +} + +.page.reference-ranked-lane .list-item, +.page.live-ranked-lane .list-item { + grid-template-columns: 48px minmax(0, 1fr) auto; +} + +.page.reference-ranked-lane .list-item .badge, +.page.live-ranked-lane .list-item .badge { + justify-content: center; + min-height: 34px; +} + +.page.fileflow-file-rail .list-item { + border-style: dashed; +} + +.page.security-status-timeline .list-item, +.page.system-status-timeline .list-item { + border-left: 3px solid var(--accent); +} + +.page.design-color-lab .swatch, +.page.image-media-stage img, +.page.image-media-wall img { + min-height: 220px; +} + +@keyframes pulse { + 0%, 100% { opacity: 1; transform: scale(1); } + 50% { opacity: .42; transform: scale(.72); } +} + +@keyframes rankGlow { + 0%, 100% { transform: translateY(0); } + 50% { transform: translateY(-1px); } +} + +@keyframes rankShimmer { + 0%, 45% { opacity: 0; transform: translateX(-45%) rotate(24deg); } + 60% { opacity: .75; } + 100% { opacity: 0; transform: translateX(45%) rotate(24deg); } +} + +@media (prefers-reduced-motion: no-preference) { + .rank-1, + .rank-2, + .rank-3 { + animation: rankGlow 2.8s ease-in-out infinite; + } + + .rank-1::before, + .rank-2::before, + .rank-3::before { + animation: rankShimmer 3.2s ease-in-out infinite; + } +} + +@media (max-width: 960px) { + .workspace, .hero, .workbench, .tool-chrome { grid-template-columns: 1fr; } + .hero-actions, .tool-actions, .result-actions { justify-content: flex-start; } +} + +@media (max-width: 560px) { + .shell { padding: 10px; } + .workspace { padding: 10px; } + .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); } + .ranked-list { grid-template-columns: 1fr; } + .card-list { grid-template-columns: 1fr; } + .list-item { grid-template-columns: 1fr; } + .ranked-item { grid-template-columns: 38px minmax(0, 1fr); } + .link-actions { justify-content: flex-start; } + .tool-chrome, .surface, .result-surface, .result-card { padding: 12px; } + .tool-identity { align-items: flex-start; } + .action-row, .tool-actions, .result-actions { width: 100%; } + .action-row button, .tool-actions button, .result-actions button { flex: 1 1 130px; } +} + +@media (max-height: 760px) { + .page { + gap: 10px; + } + + .tool-chrome, + .surface, + .result-surface, + .result-card { + padding: 12px; + } + + .tool-title h1, + .surface-head h2, + .result-toolbar h2 { + font-size: 1rem; + } + + .field.primary-field textarea.editor { + min-height: 160px; + } + + .result-details .metrics, + .result-details .raw-card { + padding: 12px; + } +} + +.drag-strong .tool-chrome, +.drag-strong .surface, +.drag-strong .result-surface, +.drag-strong .result-card, +.drag-strong .result-details, +.drag-strong .list-item, +.drag-strong .kv, +.drag-strong .table-wrap, +.drag-strong .code-wrap { + box-shadow: none !important; + filter: none !important; + backdrop-filter: none !important; +} + +.drag-strong .rank-badge::before { + display: none !important; +} + +.drag-strong .list-item.clickable:hover { + background: var(--panel-strong); +} + +@media (prefers-reduced-motion: reduce) { + *, .dot.running { animation: none !important; transition: none !important; } +} diff --git a/src/box-winUI/Assets/tool-pages/tool-page.js b/src/box-winUI/Assets/tool-pages/tool-page.js new file mode 100644 index 0000000..1819ce8 --- /dev/null +++ b/src/box-winUI/Assets/tool-pages/tool-page.js @@ -0,0 +1,989 @@ +const app = document.getElementById('app'); +let page = null; +let currentResult = null; +let runState = null; + +const labels = { + zh: { + input: '任务输入', + result: '结果', + run: '运行', + rerun: '再次运行', + chooseFile: '选择文件', + clear: '清空', + copyResult: '复制结果', + copyRaw: '复制原始输出', + copyJson: '复制 JSON', + raw: '原始输出', + details: '运行详情', + options: '更多选项', + blocks: '结果块', + lines: '行数', + chars: '字符', + mode: '模式', + status: '状态', + source: '来源', + open: '安全浏览器', + openSystem: '系统浏览器', + openFolder: '打开文件夹', + on: '开启', + off: '关闭', + ready: '就绪', + running: '正在运行', + empty: '暂无结果', + error: '运行失败', + canceled: '已取消', + preview: '预览', + noResult: '运行后会在这里显示结构化结果。', + noInput: '此工具无需输入,可直接运行或等待自动加载。', + copy: '复制', + toolInfo: '工具说明' + }, + en: { + input: 'Task input', + result: 'Result', + run: 'Run', + rerun: 'Run again', + chooseFile: 'Choose file', + clear: 'Clear', + copyResult: 'Copy result', + copyRaw: 'Copy raw output', + copyJson: 'Copy JSON', + raw: 'Raw output', + details: 'Run details', + options: 'More options', + blocks: 'Blocks', + lines: 'Lines', + chars: 'Chars', + mode: 'Mode', + status: 'Status', + source: 'Source', + open: 'Safe browser', + openSystem: 'System browser', + openFolder: 'Open folder', + on: 'On', + off: 'Off', + ready: 'Ready', + running: 'Running', + empty: 'No result yet', + error: 'Run failed', + canceled: 'Canceled', + preview: 'Preview', + noResult: 'Structured results will appear here after the tool runs.', + noInput: 'This tool does not require input. Run it or wait for auto load.', + copy: 'Copy', + toolInfo: 'Tool info' + } +}; + +window.chrome?.webview?.addEventListener('message', event => receive(event.data)); +window.addEventListener('message', event => receive(event.data)); + +function receive(message) { + if (!message) return; + try { + if (message.type === 'performanceMode') { + document.documentElement.classList.toggle('drag-light', !!message.lightMode); + document.documentElement.classList.toggle('drag-strong', !!message.lightMode && message.level === 'strong'); + return; + } + + if (message.type === 'toolPagePayload') { + page = message.payload; + currentResult = page.result || null; + runState = page.runState || null; + render(); + return; + } + + if (message.type === 'toolRunState') { + runState = message.runState; + render(); + return; + } + + if (message.type === 'toolResult') { + currentResult = message.document; + runState = message.runState; + render(); + return; + } + + if (message.type === 'settingsChanged' && page) { + page.theme = message.theme || page.theme; + page.language = message.language || page.language; + render(); + return; + } + + if (message.type === 'fileSelected' && page) { + setPrimaryText(message.path || ''); + render(); + } + } catch (error) { + renderFatal(error); + } +} + +function isEnglish() { + return (page?.language || '').toLowerCase().startsWith('en'); +} + +function t(key) { + return (isEnglish() ? labels.en : labels.zh)[key] || labels.en[key] || key; +} + +function post(action, value, payload) { + window.chrome?.webview?.postMessage({ action, value, payload }); +} + +function text(value) { + return value === null || value === undefined ? '' : String(value); +} + +function el(tag, className, content) { + const node = document.createElement(tag); + if (className) node.className = className; + if (content !== undefined) node.textContent = content; + return node; +} + +function render() { + try { + if (!page) return; + const theme = (page.theme || 'Light').toLowerCase().includes('dark') ? 'dark' : 'light'; + document.documentElement.dataset.theme = theme; + document.documentElement.lang = isEnglish() ? 'en-US' : 'zh-CN'; + document.documentElement.style.setProperty('--accent', page.experience?.accent || '#2563eb'); + + app.innerHTML = ''; + const root = el('section', `page ${page.experience?.inputLayout || ''} ${page.experience?.resultLayout || ''}`); + root.append(toolbar()); + + const workbench = el('section', 'workbench'); + workbench.append(inputPanel(), resultPanel()); + root.append(workbench); + app.append(root); + } catch (error) { + renderFatal(error); + } +} + +function renderFatal(error) { + app.innerHTML = ''; + const root = el('section', 'empty-state fatal'); + root.append(el('h2', '', t('error'))); + root.append(el('p', 'muted', error?.message || String(error || 'Render failed'))); + app.append(root); +} + +function toolbar() { + const root = el('section', 'tool-chrome'); + const identity = el('div', 'tool-identity'); + const mark = el('div', 'tool-mark', (page.toolName || page.toolId || 'T').trim().slice(0, 2).toUpperCase()); + const title = el('div', 'tool-title'); + title.append(el('h1', '', page.toolName || page.toolId || 'Tool')); + title.append(chromeMeta()); + if (page.toolDescription) { + title.title = page.toolDescription; + } + identity.append(mark, title); + + const actions = el('div', 'tool-actions'); + actions.append(actionButton(primaryActionText(), 'primary', runTool)); + if (page.availableActions?.includes('chooseFile')) { + actions.append(actionButton(t('chooseFile'), '', () => post('chooseFile'))); + } + actions.append(actionButton(t('copyResult'), 'ghost', () => post('copyResult'))); + + root.append(identity, actions); + return root; +} + +function chromeMeta() { + const row = el('div', 'meta-row'); + row.append(statusBadge()); + const category = page.runtimeMetadata?.category; + if (category) row.append(el('span', 'quiet-badge', category)); + const layout = resultLayoutLabel(); + if (layout) row.append(el('span', 'quiet-badge', layout)); + return row; +} + +function inputPanel() { + const root = el('section', 'surface input-surface'); + const head = el('div', 'surface-head'); + const title = el('div'); + title.append(el('h2', '', t('input'))); + title.append(el('p', 'muted compact-copy', inputHint())); + head.append(title, statusBadge()); + root.append(head); + + const grid = el('div', 'input-grid'); + appendFields(grid); + const options = optionsSurface(); + if (options) grid.append(options); + + const actions = el('div', 'action-row'); + actions.append(actionButton(primaryActionText(), 'primary', runTool)); + if (page.availableActions?.includes('chooseFile')) { + actions.append(actionButton(t('chooseFile'), '', () => post('chooseFile'))); + } + if (page.availableActions?.includes('clear')) { + actions.append(actionButton(t('clear'), 'ghost', clearInputs)); + } + grid.append(actions); + root.append(grid); + return root; +} + +function appendFields(root) { + const fields = page.input?.fields || []; + if (!fields.length) { + root.append(el('p', 'muted inline-note', page.spec?.autoRunOnOpen ? t('ready') : t('noInput'))); + return; + } + + const primary = fields.find(field => field.key === 'input' || field.key === 'query' || field.key === 'path' || field.kind === 'textarea') || fields[0]; + const primaryKinds = ['textarea', 'text', 'url', 'token', 'file', 'color']; + + if (primary && primaryKinds.includes(primary.kind)) { + root.append(fieldElement(primary, true)); + const secondary = fields.filter(field => field !== primary); + if (secondary.length) { + const secondaryGrid = el('div', 'field-grid'); + for (const field of secondary) secondaryGrid.append(fieldElement(field, false)); + root.append(secondaryGrid); + } + return; + } + + const fieldGrid = el('div', 'field-grid'); + for (const field of fields) fieldGrid.append(fieldElement(field, false)); + root.append(fieldGrid); +} + +function fieldElement(field, primary) { + const wrap = el('div', `field ${primary ? 'primary-field' : ''}`); + const label = el('label', '', field.label || field.key); + label.htmlFor = `field-${field.key}`; + wrap.append(label); + + let control; + if (field.kind === 'textarea' || (primary && page.spec?.primaryInput === 'MultilineText')) { + control = el('textarea', primary ? 'editor' : ''); + control.value = field.value || ''; + } else if (field.kind === 'select') { + control = el('select'); + for (const option of field.options || []) { + const item = el('option', '', option.label || option.value); + item.value = option.value || ''; + item.selected = item.value === (field.value || ''); + control.append(item); + } + } else { + control = el('input'); + control.type = field.kind === 'number' + ? 'number' + : field.kind === 'date' + ? 'date' + : field.kind === 'color' + ? 'color' + : field.kind === 'token' + ? 'password' + : 'text'; + control.value = field.value || ''; + if (field.minimum !== null && field.minimum !== undefined) control.min = field.minimum; + if (field.maximum !== null && field.maximum !== undefined) control.max = field.maximum; + } + + control.id = `field-${field.key}`; + control.name = field.key; + control.placeholder = field.placeholder || ''; + control.addEventListener('input', () => updateField(field.key, control.value)); + control.addEventListener('change', () => updateField(field.key, control.value)); + wrap.append(control); + return wrap; +} + +function optionsSurface() { + const rules = page.spec?.rules || []; + const description = page.toolDescription || page.input?.metadata?.inputDescription || ''; + if (!rules.length && !description) return null; + + const content = el('div', rules.length <= 2 ? 'options-inline' : 'options-stack'); + for (const rule of rules) content.append(ruleElement(rule)); + if (description) { + const info = el('p', 'muted option-help', localizeHint(description)); + content.append(info); + } + + if (rules.length <= 1 && !description) { + return content; + } + + const drawer = el('details', 'options-drawer'); + const summary = el('summary'); + summary.append(el('span', '', t('options'))); + summary.append(el('span', 'muted', rules.length ? `${rules.length}` : t('toolInfo'))); + drawer.append(summary, content); + return drawer; +} + +function ruleElement(rule) { + const wrap = el('div', 'rule-field'); + wrap.append(el('label', '', localized(rule.localizedLabel, rule.label || rule.key))); + const options = rule.options || []; + const current = (page.input?.rules || {})[rule.key] ?? options[0]?.value ?? 'false'; + + if (!options.length) { + const toggle = actionButton(current === 'true' ? t('on') : t('off'), `toggle-chip ${current === 'true' ? 'active' : ''}`, () => { + page.input.rules = { ...(page.input.rules || {}), [rule.key]: current === 'true' ? 'false' : 'true' }; + render(); + }); + wrap.append(toggle); + return wrap; + } + + if (options.length <= 4) { + const group = el('div', 'segmented'); + for (const option of options) { + const value = option.value || ''; + const label = localized(option.localizedLabel, option.label || value); + group.append(actionButton(label, current === value ? 'active' : '', () => { + page.input.rules = { ...(page.input.rules || {}), [rule.key]: value }; + render(); + })); + } + wrap.append(group); + return wrap; + } + + const select = el('select'); + for (const option of options) { + const item = el('option', '', localized(option.localizedLabel, option.label || option.value)); + item.value = option.value || ''; + item.selected = item.value === current; + select.append(item); + } + select.addEventListener('change', () => { + page.input.rules = { ...(page.input.rules || {}), [rule.key]: select.value }; + render(); + }); + wrap.append(select); + return wrap; +} + +function resultPanel() { + const root = el('section', 'result-surface'); + root.append(resultToolbar()); + + if (currentResult) { + const blocks = Array.isArray(currentResult.blocks) ? currentResult.blocks : []; + const ordered = prioritizeBlocks(blocks); + if (ordered.length) { + for (const block of ordered) root.append(renderBlockSafe(block)); + } else { + root.append(emptyResult()); + } + root.append(resultDetails(blocks)); + } else { + root.append(emptyResult(runState?.state === 'error' ? t('error') : t('empty'), runState?.message || t('noResult'))); + } + + return root; +} + +function resultToolbar() { + const root = el('section', 'result-toolbar'); + const title = el('div'); + title.append(el('h2', '', t('result'))); + title.append(el('p', 'muted compact-copy', resultLayoutLabel())); + const actions = el('div', 'result-actions'); + actions.append(statusBadge()); + actions.append(actionButton(t('copyRaw'), 'ghost compact', () => post('copy', currentResult?.rawText || ''))); + root.append(title, actions); + return root; +} + +function emptyResult(title = t('empty'), message = t('noResult')) { + const empty = el('section', 'empty-state'); + empty.append(el('h2', '', title)); + empty.append(el('p', 'muted', message)); + return empty; +} + +function resultDetails(blocks) { + const root = el('details', 'result-details'); + const summary = el('summary'); + summary.append(el('span', '', t('details'))); + summary.append(el('span', 'muted', `${blocks.length} ${t('blocks')}`)); + root.append(summary); + root.append(runtimeMetrics(blocks)); + root.append(rawCard()); + return root; +} + +function statusBadge() { + const state = runState?.state || currentResult?.status || 'ready'; + const status = el('div', 'status'); + status.append(el('span', `dot ${state}`)); + status.append(el('span', '', statusText(state))); + return status; +} + +function runtimeMetrics(blocks) { + const raw = currentResult?.rawText || ''; + const values = [ + [t('status'), statusText(runState?.state || currentResult?.status || 'success')], + [t('blocks'), blocks.length], + [t('lines'), raw ? raw.replace(/\r\n/g, '\n').split('\n').length : 0], + [t('mode'), primitiveLabel(page.experience?.resultPrimitives?.[0] || page.resultKind || 'result')] + ]; + const root = el('section', 'metrics runtime-metrics'); + for (const [label, value] of values) { + const metric = el('div', 'metric'); + metric.append(el('span', 'muted', label), el('strong', '', value)); + root.append(metric); + } + return root; +} + +function prioritizeBlocks(blocks) { + const score = block => { + const kind = String(block?.kind || '').toLowerCase(); + const meta = block?.metadata || {}; + const mode = String(meta.displayMode || '').toLowerCase(); + const priority = String(meta.priority || '').toLowerCase(); + if (priority === 'high') return 0; + if (kind === 'metric') return 1; + if (['rankedlist', 'newslist', 'cardlist'].includes(kind)) return 2; + if (kind === 'table' || kind === 'linechart') return 3; + if (['image', 'media', 'color'].includes(kind)) return 4; + if (['keyvalue', 'status', 'timeline', 'diff'].includes(kind)) return 5; + if (mode === 'source' || priority === 'low') return 8; + if (['raw', 'text'].includes(kind)) return 7; + return 6; + }; + return blocks.map((block, index) => ({ block, index })) + .sort((a, b) => score(a.block) - score(b.block) || a.index - b.index) + .map(item => item.block); +} + +function renderBlockSafe(block) { + try { + return renderBlock(block || {}); + } catch (error) { + const root = el('article', 'result-card error-card'); + root.append(el('h2', '', t('error'))); + root.append(el('p', 'muted', error?.message || String(error || 'Block render failed'))); + if (block?.text) root.append(code(redact(block.text))); + return root; + } +} + +function renderBlock(block) { + const kind = String(block.kind || '').toLowerCase(); + const metadata = block.metadata || {}; + const mode = String(metadata.displayMode || '').toLowerCase(); + const priority = String(metadata.priority || '').toLowerCase(); + const root = el('article', `result-card kind-${kind || 'block'} mode-${mode || 'default'} priority-${priority || 'normal'}`); + const head = el('div', 'card-head'); + const title = el('div', 'card-title'); + title.append(blockIcon(kind), el('h2', '', block.title || kindLabel(kind) || t('result'))); + head.append(title, el('span', 'badge', kindLabel(kind))); + root.append(head); + + const pairs = Array.isArray(block.pairs) ? block.pairs : []; + const rows = Array.isArray(block.rows) ? block.rows : []; + const items = Array.isArray(block.items) ? block.items : []; + if (kind === 'keyvalue' || kind === 'metric') root.append(kvGrid(pairs, kind, metadata)); + else if (kind === 'table' || kind === 'linechart') root.append(table(rows)); + else if (['rankedlist', 'newslist', 'cardlist'].includes(kind)) root.append(rankedList(items, kind)); + else if (['diff', 'status', 'timeline'].includes(kind)) root.append(statusList(items, kind)); + else if (kind === 'text') root.append(items.length ? statusList(items, kind) : code(redact(block.text || ''))); + else if (['code', 'json', 'jsontree', 'raw'].includes(kind)) root.append(code(redact(block.text || ''))); + else if (kind === 'file') root.append(fileBlock(block)); + else if (kind === 'link') root.append(linkBlock(block)); + else if (kind === 'image' || kind === 'media') root.append(mediaBlock(block)); + else if (kind === 'color') root.append(colorBlock(block)); + else root.append(el('p', '', redact(block.text || ''))); + return root; +} + +function kvGrid(pairs, kind, metadata) { + const grid = el('div', `kv-grid ${kind === 'metric' ? 'metric-grid' : ''}`); + const visible = pairs.slice(0, 80); + for (const [index, pair] of visible.entries()) { + const item = el('div', `kv ${kind === 'metric' && index === 0 ? 'primary-kv' : ''} ${metadata?.displayMode === 'source' ? 'source-kv' : ''}`); + item.append(el('span', '', pair.key || ''), el('strong', '', redact(pair.value || ''))); + grid.append(item); + } + if (!visible.length) grid.append(el('p', 'muted', t('noResult'))); + return grid; +} + +function table(rows) { + const wrap = el('div', 'table-wrap'); + const tableNode = el('table'); + const visible = rows.slice(0, 180); + for (const [rowIndex, row] of visible.entries()) { + const tr = el('tr'); + const cells = Array.isArray(row) ? row : Object.values(row || {}); + for (const cell of cells.slice(0, 8)) tr.append(el(rowIndex === 0 ? 'th' : 'td', '', redact(cell))); + tableNode.append(tr); + } + if (!visible.length) { + const tr = el('tr'); + tr.append(el('td', 'muted', t('noResult'))); + tableNode.append(tr); + } + wrap.append(tableNode); + return wrap; +} + +function rankedList(items, kind) { + const root = el('div', `list ${kind === 'rankedlist' ? 'ranked-list' : 'card-list'} ${kind}`); + for (const [index, item] of items.slice(0, 220).entries()) { + const normalized = normalizeListItem(item, index); + const row = el('div', `list-item ${kind === 'rankedlist' ? 'ranked-item' : ''}`); + row.append(rankBadge(normalized.rank, normalized.leading, kind)); + const body = el('div', 'item-body'); + body.append(el('strong', '', redact(normalized.title))); + if (normalized.subtitle) body.append(el('span', 'item-subtitle', redact(normalized.subtitle))); + row.append(body); + makeCardLink(row, normalized.uri, normalized.title); + root.append(row); + } + if (!items.length) root.append(el('p', 'muted', t('noResult'))); + return root; +} + +function statusList(items, kind) { + const root = el('div', `list status-list ${kind}`); + for (const [index, item] of items.slice(0, 220).entries()) { + const normalized = normalizeListItem(item, index); + const row = el('div', `list-item status-${normalized.status || 'info'}`); + row.append(el('span', 'badge', normalized.leading)); + const body = el('div', 'item-body'); + body.append(el('strong', '', redact(normalized.title))); + if (normalized.subtitle) body.append(el('span', 'item-subtitle', redact(normalized.subtitle))); + row.append(body); + if (normalized.uri) row.append(linkActions(normalized.uri)); + root.append(row); + } + if (!items.length) root.append(el('p', 'muted', t('noResult'))); + return root; +} + +function normalizeListItem(item, index) { + let title = text(item?.title || item?.text || ''); + let subtitle = text(item?.subtitle || ''); + const uri = text(item?.uri || ''); + const status = text(item?.status || ''); + const leading = text(item?.leading || status || index + 1); + const rank = Number.parseInt(leading, 10) || index + 1; + const parts = title.split(/\s+\/\s+/).filter(Boolean); + if (parts.length > 1) { + title = parts[0]; + subtitle = subtitle ? `${subtitle}\n${parts.slice(1).join(' / ')}` : parts.slice(1).join(' / '); + } + const heat = title.match(/^(?.*?)\s+(?<meta>(?:热度|播放|指数|人数|票房|状态)[::]?.+)$/); + if (heat?.groups?.title) { + title = heat.groups.title; + subtitle = subtitle ? `${subtitle}\n${heat.groups.meta}` : heat.groups.meta; + } + if (!title) title = t('noResult'); + return { rank, leading, title, subtitle, uri, status }; +} + +function rankBadge(rank, label, kind) { + const badge = el('span', `rank-badge rank-${rank <= 3 && kind === 'rankedlist' ? rank : 'other'} ${kind !== 'rankedlist' ? 'plain' : ''}`); + badge.append(el('span', 'rank-core', rank <= 3 && kind === 'rankedlist' ? String(rank) : label)); + return badge; +} + +function linkActions(uri) { + const actions = el('div', 'link-actions'); + actions.append(actionButton(t('open'), '', () => post('openLink', uri))); + actions.append(actionButton(t('openSystem'), 'ghost compact', () => post('openSystemBrowser', uri))); + return actions; +} + +function makeCardLink(row, uri, label) { + if (!uri) return; + row.classList.add('clickable'); + row.tabIndex = 0; + row.role = 'link'; + row.title = uri; + row.setAttribute('aria-label', `${t('open')} ${label || uri}`); + row.addEventListener('click', event => { + event.preventDefault(); + post('openLink', uri); + }); + row.addEventListener('keydown', event => { + if (event.key === 'Enter' || event.key === ' ') { + event.preventDefault(); + post('openLink', uri); + } + }); +} + +function code(value) { + const wrap = el('div', 'code-wrap'); + const pre = el('pre'); + pre.textContent = value; + wrap.append(pre); + return wrap; +} + +function fileBlock(block) { + const row = el('div', 'list-item file-card'); + row.append(el('span', 'badge', 'FILE')); + const body = el('div', 'item-body'); + body.append(el('strong', '', block.text || block.path || 'File')); + body.append(el('span', '', redact(block.path || ''))); + row.append(body, actionButton(t('openFolder'), '', () => post('openFolder', block.path))); + return row; +} + +function linkBlock(block) { + const row = el('div', 'list-item link-card'); + row.append(el('span', 'badge', 'URL')); + const body = el('div', 'item-body'); + body.append(el('strong', '', redact(block.text || block.uri || 'Link'))); + body.append(el('span', '', redact(block.uri || ''))); + row.append(body, linkActions(block.uri)); + return row; +} + +function mediaBlock(block) { + const metadata = block.metadata || {}; + const uri = block.uri || block.text || ''; + const pngBase64 = metadata.pngBase64 || metadata.pngbase64 || ''; + const rawSvg = metadata.rawSvg || metadata.rawsvg || (String(uri).trim().startsWith('<svg') ? uri : ''); + const source = pngBase64 + ? `data:image/png;base64,${pngBase64}` + : rawSvg + ? `data:image/svg+xml;charset=utf-8,${encodeURIComponent(rawSvg)}` + : uri; + if (/\.(png|jpe?g|gif|webp|bmp|svg)(\?|$)/i.test(source) || source.startsWith('data:image')) { + const frame = el('div', 'media-frame'); + const img = new Image(); + img.alt = block.title || t('preview'); + img.loading = 'eager'; + img.decoding = 'async'; + img.src = source; + frame.append(img); + return frame; + } + return linkBlock(block); +} + +function colorBlock(block) { + const swatch = el('div', 'swatch'); + swatch.style.background = block.text || block.uri || page.experience?.accent || '#2563eb'; + return swatch; +} + +function rawCard() { + const root = el('section', 'raw-card'); + const head = el('div', 'card-head'); + head.append(el('h2', '', t('raw')), actionButton(t('copyRaw'), 'ghost compact', () => post('copy', currentResult?.rawText || ''))); + root.append(head, code(redact(currentResult?.rawText || ''))); + return root; +} + +function runTool() { + const input = buildInputText(); + post('run', input, { + text: input, + fields: page.input?.fields || [], + rules: page.input?.rules || {} + }); +} + +function buildInputText() { + const fields = page.input?.fields || []; + const spec = page.spec || {}; + const byKey = Object.fromEntries(fields.map(field => [field.key, field.value || ''])); + if (spec.primaryInput === 'DateRange') { + const start = byKey.start || byKey.date || new Date().toISOString().slice(0, 10); + return byKey.end ? `${start} ${byKey.end}` : start; + } + if (['Number', 'NumberPair', 'NumberTriple', 'NumberQuad'].includes(spec.primaryInput)) { + return fields.filter(field => field.kind === 'number').map(field => field.value || '0').join(' '); + } + const preset = fields.find(field => field.key === 'preset'); + if (preset && spec.primaryInput === 'FixedOptions') return preset.value || page.input?.text || ''; + const primary = fields.find(field => ['input', 'query', 'path', 'token'].includes(field.key)) || fields[0]; + return primary?.value || page.input?.text || ''; +} + +function clearInputs() { + for (const field of page.input?.fields || []) field.value = ''; + page.input.text = ''; + post('clear'); + render(); +} + +function updateField(key, value) { + for (const field of page.input?.fields || []) { + if (field.key === key) field.value = value; + } + if (['input', 'query', 'path', 'token'].includes(key)) page.input.text = value; +} + +function setPrimaryText(value) { + const fields = page.input?.fields || []; + const primary = fields.find(field => ['input', 'query', 'path', 'token'].includes(field.key)) || fields[0]; + if (primary) primary.value = value; + page.input.text = value; +} + +function redact(value) { + let output = text(value); + return output.replace(/https?:\/\/[^\s\]\)"'<>]+/gi, (match, offset, source) => { + return shouldRedactUrl(match, offset, source) ? redactedLabel() : match; + }); +} + +function redactedLabel() { + return isEnglish() ? '[YMhut endpoint hidden]' : '[YMhut 接口已隐藏]'; +} + +function shouldRedactUrl(url, offset, source) { + const host = safeHost(url); + const hints = page?.privacyPolicy?.redactedHostHints || []; + if (host && hints.some(hint => host.toLowerCase().includes(String(hint).toLowerCase()))) return true; + const context = source.slice(Math.max(0, offset - 64), offset); + return /(?:api[_\-\s]*url|request[_\-\s]*url|api\s+request|endpoint|接口地址|请求地址)\s*[:=:]?\s*$/i.test(context); +} + +function safeHost(url) { + try { + return new URL(url).host; + } catch { + return ''; + } +} + +function resultLayoutLabel() { + const layout = page.experience?.resultLayout || page.experience?.experienceId || ''; + const primitive = page.experience?.resultPrimitives?.[0] || page.resultKind || ''; + const zh = { + 'data-table': '数据表', + 'ranked-lane': '排行榜', + 'news-board': '新闻卡片', + 'json-tree': 'JSON', + 'code-panel': '代码预览', + 'metric-grid': '指标', + 'media-stage': '媒体预览', + 'media-wall': '媒体', + 'file-rail': '文件', + 'status-timeline': '状态', + 'color-lab': '颜色', + 'text-stream': '文本', + 'generator-strip': '生成结果' + }; + const en = { + 'data-table': 'Data table', + 'ranked-lane': 'Ranking', + 'news-board': 'News cards', + 'json-tree': 'JSON', + 'code-panel': 'Code preview', + 'metric-grid': 'Metrics', + 'media-stage': 'Media preview', + 'media-wall': 'Media', + 'file-rail': 'Files', + 'status-timeline': 'Status', + 'color-lab': 'Color', + 'text-stream': 'Text', + 'generator-strip': 'Generated' + }; + const key = Object.keys(zh).find(item => layout.includes(item) || String(primitive).includes(item)); + const lang = isEnglish() ? en : zh; + return key ? lang[key] : primitiveLabel(primitive || layout); +} + +function primitiveLabel(value) { + const key = String(value || '').toLowerCase(); + const zh = { + 'data-table': '数据表', + 'ranked-lane': '排行榜', + 'news-board': '新闻', + 'summary-metrics': '摘要', + 'raw-drawer': '原始输出', + 'virtual-list': '列表', + 'column-scan': '列扫描', + 'keyvaluecards': '键值卡片', + 'calculatortable': '计算表', + 'rankedlist': '排行榜', + 'newscards': '新闻卡片', + 'table': '表格' + }; + const en = { + 'data-table': 'Data table', + 'ranked-lane': 'Ranking', + 'news-board': 'News', + 'summary-metrics': 'Summary', + 'raw-drawer': 'Raw output', + 'virtual-list': 'List', + 'column-scan': 'Column scan', + 'keyvaluecards': 'Key-value cards', + 'calculatortable': 'Calculation table', + 'rankedlist': 'Ranking', + 'newscards': 'News cards', + 'table': 'Data table' + }; + const lang = isEnglish() ? en : zh; + return lang[key] || value || t('result'); +} + +function kindLabel(kind) { + const key = String(kind || '').toLowerCase(); + const zh = { + keyvalue: '详情', + metric: '指标', + table: '表格', + linechart: '趋势', + rankedlist: '榜单', + newslist: '新闻', + cardlist: '卡片', + timeline: '时间线', + status: '状态', + diff: '差异', + text: '文本', + code: '代码', + json: 'JSON', + jsontree: 'JSON', + raw: '原始', + file: '文件', + link: '链接', + image: '图像', + media: '媒体', + color: '颜色' + }; + const en = { + keyvalue: 'Details', + metric: 'Metric', + table: 'Table', + linechart: 'Trend', + rankedlist: 'Rank', + newslist: 'News', + cardlist: 'Cards', + timeline: 'Timeline', + status: 'Status', + diff: 'Diff', + text: 'Text', + code: 'Code', + json: 'JSON', + jsontree: 'JSON', + raw: 'Raw', + file: 'File', + link: 'Link', + image: 'Image', + media: 'Media', + color: 'Color' + }; + const lang = isEnglish() ? en : zh; + return lang[key] || key || t('result'); +} + +function blockIcon(kind) { + const node = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); + node.setAttribute('class', 'block-icon'); + node.setAttribute('viewBox', '0 0 24 24'); + node.setAttribute('aria-hidden', 'true'); + const path = document.createElementNS('http://www.w3.org/2000/svg', 'path'); + path.setAttribute('fill', 'none'); + path.setAttribute('stroke', 'currentColor'); + path.setAttribute('stroke-width', '1.8'); + path.setAttribute('stroke-linecap', 'round'); + path.setAttribute('stroke-linejoin', 'round'); + const key = String(kind || '').toLowerCase(); + path.setAttribute('d', { + metric: 'M4 19V5m0 14h16M8 16V9m4 7V6m4 10v-4', + keyvalue: 'M5 7h5M5 12h14M5 17h10', + table: 'M4 5h16v14H4zM4 11h16M10 5v14', + linechart: 'M4 17l5-5 4 3 7-8', + rankedlist: 'M7 17h10M8 13h8M10 9h4M12 4l2 3 3 .5-2.2 2.1.5 3.2L12 11l-3.3 1.8.5-3.2L7 7.5 10 7z', + newslist: 'M5 5h14v14H5zM8 9h8M8 13h8M8 17h5', + cardlist: 'M5 6h14M5 12h14M5 18h14', + timeline: 'M12 5v14M8 7h8M8 17h8', + status: 'M5 12l4 4L19 6', + diff: 'M7 8h10M7 16h10M12 4v8', + code: 'M9 18l-6-6 6-6M15 6l6 6-6 6', + json: 'M8 5H6v14h2M16 5h2v14h-2', + jsontree: 'M8 5H6v14h2M16 5h2v14h-2', + file: 'M6 3h8l4 4v14H6zM14 3v5h5', + link: 'M10 13a5 5 0 0 0 7 0l2-2a5 5 0 0 0-7-7l-1 1M14 11a5 5 0 0 0-7 0l-2 2a5 5 0 0 0 7 7l1-1', + image: 'M4 5h16v14H4zM8 13l3-3 3 4 2-2 4 5', + media: 'M5 5l14 7-14 7z', + color: 'M12 3a9 9 0 0 0 0 18 3 3 0 0 0 3-3c0-1.7-1.3-3-3-3H9a6 6 0 0 1 3-12z' + }[key] || 'M5 6h14M5 12h14M5 18h14'); + node.append(path); + return node; +} + +function localized(pair, fallback) { + if (!pair) return fallback || ''; + return isEnglish() ? (pair.en || fallback || '') : (pair.zh || fallback || ''); +} + +function inputHint() { + const input = page.spec?.primaryInput || ''; + if (input === 'None') return page.spec?.autoRunOnOpen ? t('ready') : t('run'); + if (input === 'FilePath') return t('chooseFile'); + return localizeHint(page.input?.metadata?.hint || page.spec?.emptyState || ''); +} + +function localizeHint(value) { + if (isEnglish()) return value; + return { + 'Open this tool to refresh and render live data.': '打开工具后会刷新并展示实时数据。', + 'Enter values or choose options, then run the tool.': '输入内容或选择选项后运行工具。', + 'The tool could not finish. Check the input and try again.': '工具未能完成,请检查输入后重试。' + }[value] || value; +} + +function primaryActionText() { + if (runState?.state === 'success') return t('rerun'); + const label = page.spec?.primaryActionLabel || t('run'); + return isEnglish() ? mapActionEn(label) : label; +} + +function mapActionEn(label) { + const map = { + '格式化': 'Format', + '转换': 'Convert', + '生成': 'Generate', + '查询': 'Query', + '计算': 'Calculate', + '处理': 'Process', + '重新加载': 'Reload', + '打开': 'Open', + '扫描': 'Scan' + }; + return map[label] || label || t('run'); +} + +function statusText(state) { + if (runState?.message) return runState.message; + return { + running: t('running'), + loading: t('running'), + success: t('ready'), + ok: t('ready'), + error: t('error'), + canceled: t('canceled'), + ready: t('ready') + }[state] || t('ready'); +} + +function actionButton(label, className, onClick) { + const node = el('button', className || '', label); + node.type = 'button'; + node.addEventListener('click', onClick); + return node; +} diff --git a/src/box-winUI/Assets/tool-results/index.html b/src/box-winUI/Assets/tool-results/index.html new file mode 100644 index 0000000..5622e21 --- /dev/null +++ b/src/box-winUI/Assets/tool-results/index.html @@ -0,0 +1,19 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>YMhut Tool Result + + + +
+
+
+

Preparing result

+

Waiting for the local bridge.

+
+
+ + + diff --git a/src/box-winUI/Assets/tool-results/result.css b/src/box-winUI/Assets/tool-results/result.css new file mode 100644 index 0000000..fdc4f9f --- /dev/null +++ b/src/box-winUI/Assets/tool-results/result.css @@ -0,0 +1,754 @@ +:root { + color-scheme: light; + --bg: #f6f7f8; + --panel: rgba(255, 255, 255, .94); + --panel-strong: #ffffff; + --soft: #eef2f6; + --line: rgba(17, 24, 39, .12); + --text: #111827; + --muted: #5b6472; + --accent: #0d9488; + --accent-soft: color-mix(in srgb, var(--accent) 12%, transparent); + --danger: #c42b1c; + --success: #107c10; + --warn: #f97316; + --gold: #d99b16; + --silver: #8b95a3; + --bronze: #b7652d; + --radius: 8px; + font-family: "Segoe UI", "Microsoft YaHei UI", Arial, sans-serif; +} + +html[data-theme="dark"] { + color-scheme: dark; + --bg: #111315; + --panel: rgba(33, 35, 39, .9); + --panel-strong: #272a2f; + --soft: rgba(255, 255, 255, .07); + --line: rgba(255, 255, 255, .14); + --text: #f7f7f8; + --muted: #a9b0bb; + --accent-soft: color-mix(in srgb, var(--accent) 18%, transparent); +} + +* { box-sizing: border-box; } + +.drag-light *, +.drag-light *::before, +.drag-light *::after { + animation: none !important; + transition: none !important; + scroll-behavior: auto !important; +} + +html { + min-height: 100%; + overflow: auto; +} + +html, body { + margin: 0; + min-height: 100%; + background: var(--bg); + color: var(--text); + overflow: auto; +} + +button { + min-height: 34px; + border: 1px solid var(--line); + border-radius: 7px; + background: var(--panel-strong); + color: var(--text); + padding: 7px 12px; + cursor: pointer; + transition: border-color 160ms ease, background-color 160ms ease, color 160ms ease; +} + +button:hover, +button:focus-visible { + border-color: var(--accent); + outline: 2px solid color-mix(in srgb, var(--accent) 24%, transparent); + outline-offset: 1px; +} + +button.ghost { + background: transparent; +} + +button.compact { + min-height: 30px; + padding: 5px 9px; + color: var(--muted); +} + +.result-page { + display: grid; + gap: 14px; +} + +.result-chrome, +.details-drawer, +.block, +.metric, +.table-wrap, +.code-wrap { + border: 1px solid var(--line); + border-radius: var(--radius); + background: var(--panel); +} + +.result-chrome { + display: grid; + grid-template-columns: minmax(0, 1fr) auto; + gap: 12px; + align-items: center; + padding: 14px 16px; +} + +.result-chrome .identity { + display: flex; + align-items: center; + gap: 12px; + min-width: 0; +} + +.result-chrome .mark { + width: 40px; + height: 40px; + display: grid; + place-items: center; + border: 1px solid color-mix(in srgb, var(--accent) 34%, transparent); + border-radius: 10px; + background: var(--accent-soft); + color: var(--accent); + font-weight: 800; + flex: 0 0 auto; +} + +.result-chrome .title { + min-width: 0; +} + +.result-chrome .title h1 { + margin: 0; + font-size: clamp(1.05rem, 1.25vw, 1.45rem); + line-height: 1.18; +} + +.result-chrome .meta, +.result-chrome .muted { + color: var(--muted); +} + +.result-chrome .actions { + display: flex; + flex-wrap: wrap; + gap: 8px; + justify-content: flex-end; +} + +.result-chrome .metrics { + grid-column: 1 / -1; + margin: 0; +} + +.blocks { + display: grid; + gap: 12px; +} + +.details-drawer { + overflow: hidden; + padding: 0; +} + +.details-drawer > summary { + list-style: none; + display: flex; + justify-content: space-between; + gap: 10px; + align-items: center; + padding: 12px 14px; + cursor: pointer; + font-weight: 700; +} + +.details-drawer > summary::-webkit-details-marker { + display: none; +} + +.details-drawer[open] > summary { + border-bottom: 1px solid var(--line); +} + +.details-drawer .raw-card { + border: 0; + border-radius: 0; + border-top: 1px solid var(--line); +} + +.result-shell { + min-height: 100vh; + padding: clamp(12px, 2vw, 24px); +} + +.hero, +.metric, +.block, +.table-wrap, +.code-wrap { + border: 1px solid var(--line); + border-radius: var(--radius); + background: var(--panel); +} + +.hero { + display: grid; + grid-template-columns: minmax(0, 1fr) auto; + gap: 16px; + align-items: center; + padding: clamp(14px, 2vw, 20px); +} + +.identity { + display: flex; + gap: 12px; + align-items: center; + min-width: 0; +} + +.mark { + width: 48px; + height: 48px; + display: grid; + place-items: center; + border: 1px solid color-mix(in srgb, var(--accent) 36%, transparent); + border-radius: var(--radius); + background: var(--accent-soft); + color: var(--accent); + font-weight: 800; +} + +.title { + min-width: 0; +} + +.eyebrow, +.meta, +.muted { + color: var(--muted); +} + +.eyebrow { + font-size: .78rem; + font-weight: 800; + text-transform: uppercase; + letter-spacing: 0; +} + +h1, +h2, +h3, +p { margin: 0; } + +h1 { + font-size: clamp(1.35rem, 2.4vw, 2.1rem); + line-height: 1.12; +} + +h2 { + font-size: 1rem; +} + +.actions, +.link-actions { + display: flex; + gap: 8px; + flex-wrap: wrap; +} + +.actions { + justify-content: flex-end; +} + +.metrics { + display: grid; + grid-template-columns: repeat(4, minmax(0, 1fr)); + gap: 10px; + margin: 12px 0; +} + +.metric { + padding: 12px; + min-height: 74px; +} + +.metric strong { + display: block; + margin-top: 4px; + font-size: clamp(1.1rem, 2vw, 1.55rem); + overflow-wrap: anywhere; +} + +.content { + display: grid; + grid-template-columns: minmax(0, 1fr) minmax(280px, .32fr); + gap: 12px; +} + +.blocks, +.side { + display: grid; + gap: 12px; + align-content: start; +} + +.block { + padding: 14px; + overflow: hidden; +} + +.side-card { + background: color-mix(in srgb, var(--panel) 84%, var(--soft)); +} + +.block-head { + display: flex; + align-items: center; + justify-content: space-between; + gap: 12px; + margin-bottom: 10px; +} + +.block-title { + display: flex; + align-items: center; + gap: 8px; + min-width: 0; +} + +.block-icon { + width: 20px; + height: 20px; + color: var(--accent); + flex: 0 0 auto; +} + +.badge { + display: inline-flex; + align-items: center; + justify-content: center; + min-height: 22px; + padding: 0 7px; + border-radius: 7px; + border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent); + color: var(--accent); + background: var(--accent-soft); + font-size: .74rem; + font-weight: 800; +} + +.kv-grid { + display: grid; + grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); + gap: 8px; +} + +.kv, +.list-item { + border: 1px solid var(--line); + border-radius: 7px; + background: var(--panel-strong); +} + +.kv { + padding: 10px; + min-width: 0; +} + +.kv span, +.list-item span { + display: block; + color: var(--muted); + font-size: .76rem; +} + +.kv strong, +.list-item strong { + display: block; + overflow-wrap: anywhere; +} + +.metric-grid .primary-kv { + grid-column: 1 / -1; + border-color: color-mix(in srgb, var(--accent) 42%, var(--line)); + background: color-mix(in srgb, var(--panel-strong) 84%, var(--accent-soft)); +} + +.metric-grid .primary-kv strong { + color: var(--accent); + font-size: clamp(1.8rem, 4vw, 2.8rem); + line-height: 1.08; +} + +.table-wrap { + overflow: auto; +} + +table { + width: 100%; + min-width: 520px; + border-collapse: collapse; +} + +td, +th { + padding: 9px; + border-bottom: 1px solid var(--line); + text-align: left; + vertical-align: top; +} + +th { + color: var(--accent); + background: var(--accent-soft); + font-weight: 800; +} + +.list { + display: grid; + gap: 8px; +} + +.card-list, +.ranked-list { + grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); +} + +.list-item { + display: grid; + grid-template-columns: 42px minmax(0, 1fr) auto; + gap: 10px; + align-items: start; + padding: 10px; +} + +.ranked-item { + grid-template-columns: 42px minmax(0, 1fr); + min-height: 84px; + border-color: color-mix(in srgb, var(--accent) 22%, var(--line)); +} + +.card-list .list-item { + grid-template-columns: 42px minmax(0, 1fr); +} + +.list-item.clickable { + cursor: pointer; +} + +.list-item.clickable:hover, +.list-item.clickable:focus-visible { + border-color: color-mix(in srgb, var(--accent) 56%, var(--line)); + background: color-mix(in srgb, var(--panel-strong) 84%, var(--accent-soft)); + outline: 2px solid color-mix(in srgb, var(--accent) 20%, transparent); + outline-offset: 1px; +} + +.rank-badge { + width: 36px; + height: 36px; + position: relative; + display: inline-grid; + place-items: center; + border-radius: 8px; + overflow: hidden; + border: 1px solid color-mix(in srgb, var(--accent) 38%, transparent); + color: var(--accent); + background: var(--accent-soft); + font-weight: 900; + font-size: .88rem; +} + +.rank-badge::before { + content: ""; + position: absolute; + inset: -40%; + opacity: 0; + transform: translateX(-45%) rotate(24deg); + background: linear-gradient(90deg, transparent, rgba(255, 255, 255, .7), transparent); +} + +.rank-core { + position: relative; + z-index: 1; +} + +.rank-1 { + color: #4a3300; + border-color: color-mix(in srgb, var(--gold) 72%, #fff); + background: linear-gradient(145deg, #fff2b8, #e1a21c 58%, #9f6611); + box-shadow: 0 0 0 2px color-mix(in srgb, var(--gold) 18%, transparent); +} + +.rank-2 { + color: #26303b; + border-color: color-mix(in srgb, var(--silver) 72%, #fff); + background: linear-gradient(145deg, #f9fbff, #b8c0cc 58%, #687386); +} + +.rank-3 { + color: #3e1f0d; + border-color: color-mix(in srgb, var(--bronze) 72%, #fff); + background: linear-gradient(145deg, #ffe0c2, #c87536 58%, #7e3c1c); +} + +.rank-badge.plain { + border-radius: 7px; +} + +.item-body { + min-width: 0; +} + +.item-body strong { + display: -webkit-box; + -webkit-line-clamp: 2; + -webkit-box-orient: vertical; + overflow: hidden; +} + +.item-subtitle { + margin-top: 4px; + white-space: pre-line; + display: -webkit-box; + -webkit-line-clamp: 3; + -webkit-box-orient: vertical; + overflow: hidden; +} + +.status-error { + border-color: color-mix(in srgb, var(--danger) 45%, var(--line)); +} + +.status-ok { + border-color: color-mix(in srgb, var(--success) 45%, var(--line)); +} + +.file-card, +.link-card { + grid-template-columns: 48px minmax(0, 1fr) auto; +} + +.code-wrap { + max-height: 420px; + overflow: auto; + background: var(--panel-strong); +} + +pre { + margin: 0; + white-space: pre-wrap; + overflow-wrap: anywhere; + padding: 12px; + font-family: "Cascadia Mono", Consolas, monospace; + font-size: .84rem; + line-height: 1.55; +} + +.media-frame { + display: grid; + place-items: center; + min-height: 260px; + border: 1px solid var(--line); + border-radius: 7px; + background: + linear-gradient(45deg, color-mix(in srgb, var(--soft) 72%, transparent) 25%, transparent 25%), + linear-gradient(-45deg, color-mix(in srgb, var(--soft) 72%, transparent) 25%, transparent 25%), + linear-gradient(45deg, transparent 75%, color-mix(in srgb, var(--soft) 72%, transparent) 75%), + linear-gradient(-45deg, transparent 75%, color-mix(in srgb, var(--soft) 72%, transparent) 75%); + background-size: 24px 24px; + background-position: 0 0, 0 12px, 12px -12px, -12px 0; + overflow: hidden; +} + +img, +video { + display: block; + max-width: 100%; + max-height: 440px; + margin: auto; + border-radius: 7px; +} + +.media-frame img { + width: min(420px, 88%); + height: auto; + object-fit: contain; + background: #fff; + padding: 10px; +} + +.swatch { + width: 100%; + min-height: 150px; + border-radius: 7px; + border: 1px solid var(--line); +} + +.raw summary { + cursor: pointer; + margin-bottom: 0; +} + +.raw[open] summary { + margin-bottom: 10px; +} + +.empty-card { + min-height: 180px; + display: grid; + align-content: center; + gap: 8px; +} + +.empty-state { + min-height: 280px; + display: grid; + place-content: center; + gap: 8px; + text-align: center; + color: var(--muted); +} + +@media (prefers-reduced-motion: no-preference) { + .rank-1, + .rank-2, + .rank-3 { + animation: rankGlow 2.8s ease-in-out infinite; + } + + .rank-1::before, + .rank-2::before, + .rank-3::before { + animation: rankShimmer 3.2s ease-in-out infinite; + } + + @keyframes rankGlow { + 0%, 100% { transform: translateY(0); } + 50% { transform: translateY(-1px); } + } + + @keyframes rankShimmer { + 0%, 45% { opacity: 0; transform: translateX(-45%) rotate(24deg); } + 60% { opacity: .75; } + 100% { opacity: 0; transform: translateX(45%) rotate(24deg); } + } +} + +@media (max-width: 900px) { + .hero, + .content, + .result-chrome { + grid-template-columns: 1fr; + } + + .actions { + justify-content: flex-start; + } + + .metrics { + grid-template-columns: repeat(2, minmax(0, 1fr)); + } +} + +@media (max-width: 560px) { + .result-shell { + padding: 10px; + } + + .metrics, + .card-list, + .ranked-list { + grid-template-columns: 1fr; + } + + .list-item, + .file-card, + .link-card { + grid-template-columns: 1fr; + } + + .link-actions { + width: 100%; + } + + .result-chrome { + padding: 12px; + } + + .result-chrome .actions, + .actions { + justify-content: flex-start; + } +} + +@media (max-height: 760px) { + .result-shell { + padding: 10px; + } + + .result-chrome, + .block, + .metric, + .table-wrap, + .code-wrap { + padding: 12px; + } + + .metrics { + margin: 10px 0; + } + + .block-head { + margin-bottom: 8px; + } +} + +@media (max-width: 560px) { + .card-list { + grid-template-columns: 1fr; + } +} + +.drag-strong .result-chrome, +.drag-strong .details-drawer, +.drag-strong .block, +.drag-strong .metric, +.drag-strong .table-wrap, +.drag-strong .code-wrap, +.drag-strong .kv, +.drag-strong .list-item { + box-shadow: none !important; + filter: none !important; + backdrop-filter: none !important; +} + +.drag-strong .rank-badge::before { + display: none !important; +} + +.drag-strong .list-item.clickable:hover { + background: var(--panel-strong); +} + +@media (prefers-reduced-motion: reduce) { + *, + .rank-1, + .rank-2, + .rank-3 { + animation: none !important; + transition: none !important; + } +} diff --git a/src/box-winUI/Assets/tool-results/result.js b/src/box-winUI/Assets/tool-results/result.js new file mode 100644 index 0000000..caed7a4 --- /dev/null +++ b/src/box-winUI/Assets/tool-results/result.js @@ -0,0 +1,469 @@ +const app = document.getElementById('app'); +let payload = null; + +const labels = { + zh: { + copied: '复制', + copyRaw: '复制原文', + copyJson: '复制 JSON', + openSafe: '安全浏览器', + openSystem: '系统浏览器', + openFolder: '打开文件夹', + raw: '原始输出', + details: '详情', + emptyTitle: '暂无结构化结果', + emptyText: '工具运行后,整理后的卡片会显示在这里。', + blocks: '区块', + lines: '行数', + chars: '字符', + mode: '模式', + media: '媒体预览', + file: '文件结果', + link: '链接' + }, + en: { + copied: 'Copy', + copyRaw: 'Copy raw', + copyJson: 'Copy JSON', + openSafe: 'Safe browser', + openSystem: 'System browser', + openFolder: 'Open folder', + raw: 'Raw output', + details: 'Details', + emptyTitle: 'No structured result', + emptyText: 'Structured cards will appear here after the tool runs.', + blocks: 'Blocks', + lines: 'Lines', + chars: 'Chars', + mode: 'Mode', + media: 'Media preview', + file: 'File result', + link: 'Link' + } +}; + +window.chrome?.webview?.addEventListener('message', event => receive(event.data)); + +window.addEventListener('message', event => { + if (receive(event.data)) return; + if (event.data?.type === 'tool-result-payload') { + payload = event.data.payload; + render(payload); + } +}); + +function receive(data) { + if (!data) return false; + if (data.type === 'performanceMode') { + document.documentElement.classList.toggle('drag-light', !!data.lightMode); + document.documentElement.classList.toggle('drag-strong', !!data.lightMode && data.level === 'strong'); + return true; + } + + if (data.type) return false; + payload = data; + render(payload); + return true; +} + +function isEnglish() { + return (payload?.language || '').toLowerCase().startsWith('en'); +} + +function t(key) { + return (isEnglish() ? labels.en : labels.zh)[key] || labels.en[key] || key; +} + +function post(action, value) { + window.chrome?.webview?.postMessage({ action, value }); +} + +function text(value) { + return value === null || value === undefined ? '' : String(value); +} + +function el(tag, className, content) { + const node = document.createElement(tag); + if (className) node.className = className; + if (content !== undefined) node.textContent = content; + return node; +} + +function render(data) { + if (!data) return; + const theme = (data.theme || '').toLowerCase().includes('dark') ? 'dark' : 'light'; + document.documentElement.dataset.theme = theme; + document.documentElement.lang = (data.language || '').toLowerCase().startsWith('en') ? 'en-US' : 'zh-CN'; + document.documentElement.style.setProperty('--accent', data.experience?.accent || '#2563eb'); + + app.innerHTML = ''; + const root = el('section', 'result-page'); + root.append(chrome(data)); + + const blocks = el('section', 'blocks'); + for (const block of (data.resultDocument?.blocks || [])) { + blocks.append(renderBlock(block, data)); + } + if (!blocks.childElementCount) { + blocks.append(emptyBlock(data)); + } + root.append(blocks, detailsDrawer(data)); + app.append(root); +} + +function chrome(data) { + const root = el('section', 'result-chrome'); + const identity = el('div', 'identity'); + const mark = el('div', 'mark', (data.toolName || data.toolId || 'T').trim().slice(0, 2).toUpperCase()); + const title = el('div', 'title'); + title.append(el('h1', '', data.toolName || data.toolId || 'Tool result')); + title.append(el('p', 'meta', `${data.experience?.primaryPrimitive || data.experience?.layout || 'result'} / ${data.runtimeMetadata?.durationMs ?? 0} ms`)); + identity.append(mark, title); + + const actions = el('div', 'actions'); + actions.append(button(t('copyRaw'), () => post('copy', data.resultDocument?.rawText || ''))); + actions.append(button(t('copyJson'), () => post('copy', JSON.stringify(data.resultDocument || {}, null, 2)), 'ghost')); + + root.append(identity, actions, metrics(data)); + return root; +} + +function metrics(data) { + const doc = data.resultDocument || {}; + const raw = doc.rawText || ''; + const rows = raw ? raw.replace(/\r\n/g, '\n').split('\n').length : 0; + const values = [ + [t('blocks'), (doc.blocks || []).length], + [t('lines'), rows], + [t('chars'), raw.length], + [t('mode'), data.experience?.primaryPrimitive || 'custom'] + ]; + const root = el('section', 'metrics'); + for (const [label, value] of values) { + const metric = el('div', 'metric'); + metric.append(el('span', 'muted', label), el('strong', '', value)); + root.append(metric); + } + return root; +} + +function renderBlock(block, data) { + const kind = (block.kind || '').toLowerCase(); + const root = el('article', `block kind-${kind || 'text'}`); + const head = el('div', 'block-head'); + const title = el('div', 'block-title'); + title.append(blockIcon(kind), el('h2', '', block.title || kindLabel(kind))); + head.append(title, el('span', 'badge', kindLabel(kind))); + root.append(head); + + if (kind === 'keyvalue' || kind === 'metric') root.append(kvGrid(block.pairs || [], data, kind)); + else if (kind === 'table' || kind === 'linechart') root.append(table(block.rows || [], data)); + else if (['rankedlist', 'newslist', 'cardlist'].includes(kind)) root.append(list(block.items || [], data, kind)); + else if (['text', 'diff', 'status', 'timeline'].includes(kind)) root.append(statusList(block.items || [], data, block.text)); + else if (['code', 'json', 'jsontree', 'raw'].includes(kind)) root.append(code(redact(block.text || '', data.privacyPolicy))); + else if (kind === 'file') root.append(fileBlock(block, data)); + else if (kind === 'link') root.append(linkBlock(block, data)); + else if (kind === 'image' || kind === 'media') root.append(mediaBlock(block, data)); + else if (kind === 'color') root.append(colorBlock(block)); + else root.append(el('p', 'paragraph', redact(block.text || '', data.privacyPolicy))); + return root; +} + +function kvGrid(pairs, data, kind) { + const grid = el('div', `kv-grid ${kind === 'metric' ? 'metric-grid' : ''}`); + for (const [index, pair] of pairs.slice(0, 80).entries()) { + const item = el('div', `kv ${index === 0 && kind === 'metric' ? 'primary-kv' : ''}`); + item.append(el('span', '', pair.key || ''), el('strong', '', redact(pair.value || '', data.privacyPolicy))); + grid.append(item); + } + if (!pairs.length) grid.append(el('p', 'muted', t('emptyText'))); + return grid; +} + +function table(rows, data) { + const wrap = el('div', 'table-wrap'); + const tableNode = el('table'); + for (const [rowIndex, row] of rows.slice(0, 180).entries()) { + const tr = el('tr'); + const cells = Array.isArray(row) ? row : Object.values(row || {}); + for (const cell of cells.slice(0, 8)) tr.append(el(rowIndex === 0 ? 'th' : 'td', '', redact(cell, data.privacyPolicy))); + tableNode.append(tr); + } + if (!rows.length) { + const tr = el('tr'); + tr.append(el('td', 'muted', t('emptyText'))); + tableNode.append(tr); + } + wrap.append(tableNode); + return wrap; +} + +function list(items, data, kind) { + const root = el('div', `list ${kind === 'rankedlist' ? 'ranked-list' : 'card-list'} ${kind}`); + for (const [index, item] of items.slice(0, 220).entries()) { + const normalized = normalizeListItem(item, index); + const row = el('div', `list-item ${kind === 'rankedlist' ? 'ranked-item' : ''}`); + row.append(rankBadge(normalized.rank, normalized.leading, kind)); + const body = el('div', 'item-body'); + body.append(el('strong', '', redact(normalized.title, data.privacyPolicy))); + if (normalized.subtitle) body.append(el('span', 'item-subtitle', redact(normalized.subtitle, data.privacyPolicy))); + row.append(body); + makeCardLink(row, normalized.uri, normalized.title); + root.append(row); + } + if (!items.length) root.append(el('p', 'muted', t('emptyText'))); + return root; +} + +function statusList(items, data, fallbackText) { + if (!items.length) return code(redact(fallbackText || '', data.privacyPolicy)); + const root = el('div', 'list status-list'); + for (const [index, item] of items.slice(0, 220).entries()) { + const normalized = normalizeListItem(item, index); + const row = el('div', `list-item status-${normalized.status || 'info'}`); + row.append(el('span', 'badge', normalized.leading)); + const body = el('div', 'item-body'); + body.append(el('strong', '', redact(normalized.title, data.privacyPolicy))); + if (normalized.subtitle) body.append(el('span', 'item-subtitle', redact(normalized.subtitle, data.privacyPolicy))); + row.append(body); + if (normalized.uri) row.append(linkActions(normalized.uri)); + root.append(row); + } + return root; +} + +function normalizeListItem(item, index) { + let title = text(item?.title || item?.text || t('emptyTitle')); + let subtitle = text(item?.subtitle || ''); + const leading = text(item?.leading || item?.status || index + 1); + const rank = Number.parseInt(leading, 10) || index + 1; + const parts = title.split(/\s+\/\s+/).filter(Boolean); + if (parts.length > 1) { + title = parts[0]; + subtitle = subtitle ? `${subtitle}\n${parts.slice(1).join(' / ')}` : parts.slice(1).join(' / '); + } + return { + rank, + leading, + title, + subtitle, + uri: text(item?.uri || ''), + status: text(item?.status || '') + }; +} + +function rankBadge(rank, label, kind) { + const badge = el('span', `rank-badge rank-${rank <= 3 && kind === 'rankedlist' ? rank : 'other'} ${kind !== 'rankedlist' ? 'plain' : ''}`); + badge.append(el('span', 'rank-core', rank <= 3 && kind === 'rankedlist' ? String(rank) : label)); + return badge; +} + +function linkActions(uri) { + const actions = el('div', 'link-actions'); + actions.append(button(t('openSafe'), () => post('openLink', uri))); + actions.append(button(t('openSystem'), () => post('openSystemBrowser', uri), 'ghost compact')); + return actions; +} + +function makeCardLink(row, uri, label) { + if (!uri) return; + row.classList.add('clickable'); + row.tabIndex = 0; + row.role = 'link'; + row.title = uri; + row.setAttribute('aria-label', `${t('openSafe')} ${label || uri}`); + row.addEventListener('click', event => { + event.preventDefault(); + post('openLink', uri); + }); + row.addEventListener('keydown', event => { + if (event.key === 'Enter' || event.key === ' ') { + event.preventDefault(); + post('openLink', uri); + } + }); +} + +function code(value) { + const wrap = el('div', 'code-wrap'); + const pre = el('pre'); + pre.textContent = value; + wrap.append(pre); + return wrap; +} + +function fileBlock(block, data) { + const row = el('div', 'list-item file-card'); + row.append(el('span', 'badge', 'FILE')); + const body = el('div', 'item-body'); + body.append(el('strong', '', block.text || block.path || t('file'))); + body.append(el('span', '', redact(block.path || '', data.privacyPolicy))); + row.append(body, button(t('openFolder'), () => post('openFolder', block.path))); + return row; +} + +function linkBlock(block, data) { + const row = el('div', 'list-item link-card'); + row.append(el('span', 'badge', 'URL')); + const body = el('div', 'item-body'); + body.append(el('strong', '', redact(block.text || block.uri || t('link'), data.privacyPolicy))); + body.append(el('span', '', redact(block.uri || '', data.privacyPolicy))); + row.append(body, linkActions(block.uri)); + return row; +} + +function mediaBlock(block, data) { + const uri = block.uri || block.text || ''; + const metadata = block.metadata || {}; + const pngBase64 = metadata.pngBase64 || metadata.pngbase64 || ''; + const rawSvg = metadata.rawSvg || metadata.rawsvg || (String(uri).trim().startsWith(' { + event.preventDefault?.(); + event.stopPropagation?.(); + post('copy', data.resultDocument?.rawText || ''); + }, 'ghost compact')); + root.append(head, code(redact(data.resultDocument?.rawText || '', data.privacyPolicy))); + return root; +} + +function emptyBlock(data) { + const root = el('article', 'block empty-card'); + root.append(el('h2', '', t('emptyTitle'))); + root.append(el('p', 'muted', t('emptyText'))); + const raw = data.resultDocument?.rawText || ''; + if (raw) root.append(code(redact(raw, data.privacyPolicy))); + return root; +} + +function redact(value, policy = payload?.privacyPolicy) { + let output = text(value); + for (const hint of policy?.redactedHostHints || []) { + const escaped = String(hint).replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + output = output.replace(new RegExp(`https?://[^\\s"'<>]*${escaped}[^\\s"'<>]*`, 'gi'), isEnglish() ? '[YMhut endpoint hidden]' : '[YMhut 接口已隐藏]'); + } + return output; +} + +function kindLabel(kind) { + const key = String(kind || '').toLowerCase(); + const zh = { + keyvalue: '详情', + metric: '指标', + table: '表格', + linechart: '趋势', + rankedlist: '排行', + newslist: '新闻', + cardlist: '卡片', + timeline: '时间线', + status: '状态', + diff: '差异', + text: '文本', + code: '代码', + json: 'JSON', + jsontree: 'JSON', + raw: '原文', + file: '文件', + link: '链接', + image: '图像', + media: '媒体', + color: '颜色' + }; + const en = { + keyvalue: 'Details', + metric: 'Metric', + table: 'Table', + linechart: 'Trend', + rankedlist: 'Ranking', + newslist: 'News', + cardlist: 'Cards', + timeline: 'Timeline', + status: 'Status', + diff: 'Diff', + text: 'Text', + code: 'Code', + json: 'JSON', + jsontree: 'JSON', + raw: 'Raw', + file: 'File', + link: 'Link', + image: 'Image', + media: 'Media', + color: 'Color' + }; + const lang = isEnglish() ? en : zh; + return lang[key] || key || t('details'); +} + +function blockIcon(kind) { + const node = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); + node.setAttribute('class', 'block-icon'); + node.setAttribute('viewBox', '0 0 24 24'); + node.setAttribute('aria-hidden', 'true'); + const path = document.createElementNS('http://www.w3.org/2000/svg', 'path'); + path.setAttribute('fill', 'none'); + path.setAttribute('stroke', 'currentColor'); + path.setAttribute('stroke-width', '1.8'); + path.setAttribute('stroke-linecap', 'round'); + path.setAttribute('stroke-linejoin', 'round'); + path.setAttribute('d', { + rankedlist: 'M7 17h10M8 13h8M10 9h4M12 4l2 3 3 .5-2.2 2.1.5 3.2L12 11l-3.3 1.8.5-3.2L7 7.5 10 7z', + newslist: 'M5 5h14v14H5zM8 9h8M8 13h8M8 17h5', + table: 'M4 5h16v14H4zM4 11h16M10 5v14', + link: 'M10 13a5 5 0 0 0 7.1 0l1.4-1.4a5 5 0 0 0-7.1-7.1L10.7 5M14 11a5 5 0 0 0-7.1 0l-1.4 1.4a5 5 0 0 0 7.1 7.1l.7-.7', + file: 'M6 3h8l4 4v14H6zM14 3v5h5', + media: 'M4 7h16v10H4zM9 10l5 2-5 2z', + image: 'M4 5h16v14H4zM8 14l2-2 3 3 2-2 3 4', + status: 'M5 12l4 4L19 6', + code: 'M9 18l-6-6 6-6M15 6l6 6-6 6', + json: 'M8 5H6v14h2M16 5h2v14h-2', + jsontree: 'M8 5H6v14h2M16 5h2v14h-2', + color: 'M12 3a9 9 0 0 0 0 18 3 3 0 0 0 3-3c0-1.7-1.3-3-3-3H9a6 6 0 0 1 3-12z', + default: 'M5 7h14M5 12h14M5 17h10' + }[kind] || 'M5 7h14M5 12h14M5 17h10'); + node.append(path); + return node; +} + +function button(label, onClick, className = '') { + const node = el('button', className, label); + node.type = 'button'; + node.addEventListener('click', onClick); + return node; +} diff --git a/src/box-winUI/Controls/AnimatedWeatherIconControl.cs b/src/box-winUI/Controls/AnimatedWeatherIconControl.cs new file mode 100644 index 0000000..74f2435 --- /dev/null +++ b/src/box-winUI/Controls/AnimatedWeatherIconControl.cs @@ -0,0 +1,382 @@ +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Media; +using Microsoft.UI.Xaml.Media.Animation; +using Microsoft.UI.Xaml.Shapes; +using Windows.Foundation; +using Windows.UI.ViewManagement; +using YMhut.Box.WinUI.Services; + +namespace YMhut.Box.WinUI.Controls; + +public sealed class AnimatedWeatherIconControl : UserControl +{ + private readonly Canvas _canvas = new(); + private readonly AccessibilitySettings _accessibility = new(); + private Storyboard? _storyboard; + private WeatherVisualKind _kind = WeatherVisualKind.Unknown; + private WeatherIntensity _intensity = WeatherIntensity.None; + private bool _animationEnabled = true; + + public AnimatedWeatherIconControl(double size = 20) + { + Width = size; + Height = size; + MinWidth = size; + MinHeight = size; + _canvas.Width = size; + _canvas.Height = size; + _canvas.IsHitTestVisible = false; + Content = _canvas; + Loaded += (_, _) => Render(); + SizeChanged += (_, _) => Render(); + Unloaded += (_, _) => StopStoryboard(); + } + + public void Update(TitleWeatherSnapshot snapshot, bool animationEnabled) + { + _kind = snapshot.VisualKind; + _intensity = snapshot.Intensity; + _animationEnabled = animationEnabled; + Render(); + } + + private void Render() + { + StopStoryboard(); + _canvas.Children.Clear(); + + var size = Math.Max(16, ActualWidth > 0 ? ActualWidth : Width > 0 ? Width : 20); + _canvas.Width = size; + _canvas.Height = size; + _canvas.Clip = new RectangleGeometry { Rect = new Rect(0, 0, size, size) }; + _storyboard = new Storyboard(); + + switch (_kind) + { + case WeatherVisualKind.Clear: + AddSun(size, 0.5, 0.5, 0.23, animate: true); + break; + case WeatherVisualKind.PartlyCloudy: + AddSun(size, 0.34, 0.35, 0.18, animate: true, opacity: 0.9); + AddCloud(size, 0.20, 0.38, 0.68, 0.38, opacity: 0.95, animate: true); + break; + case WeatherVisualKind.Cloudy: + AddCloud(size, 0.13, 0.34, 0.74, 0.42, opacity: 1, animate: true); + break; + case WeatherVisualKind.Fog: + AddCloud(size, 0.16, 0.25, 0.70, 0.34, opacity: 0.9, animate: false); + AddFog(size); + break; + case WeatherVisualKind.Drizzle: + case WeatherVisualKind.Rain: + case WeatherVisualKind.FreezingRain: + case WeatherVisualKind.Showers: + AddCloud(size, 0.14, 0.20, 0.72, 0.36, opacity: 1, animate: false); + AddRain(size, DropsFor(_intensity), _kind == WeatherVisualKind.Drizzle); + break; + case WeatherVisualKind.Snow: + case WeatherVisualKind.SnowGrains: + case WeatherVisualKind.SnowShowers: + AddCloud(size, 0.14, 0.20, 0.72, 0.36, opacity: 1, animate: false); + AddSnow(size, DropsFor(_intensity)); + break; + case WeatherVisualKind.Thunderstorm: + AddCloud(size, 0.14, 0.18, 0.72, 0.36, opacity: 1, animate: false); + AddRain(size, Math.Max(3, DropsFor(_intensity)), drizzle: false); + AddBolt(size); + break; + default: + AddCloud(size, 0.18, 0.30, 0.64, 0.38, opacity: 0.72, animate: true); + AddUnknownMark(size); + break; + } + + if (CanAnimate() && _storyboard.Children.Count > 0) + { + _storyboard.Begin(); + } + } + + private void AddSun(double size, double cx, double cy, double radius, bool animate, double opacity = 1) + { + var centerX = size * cx; + var centerY = size * cy; + var r = size * radius; + var sun = AddEllipse(centerX - r, centerY - r, r * 2, r * 2, ModernUi.Bronze, opacity); + + for (var i = 0; i < 8; i++) + { + var angle = i * Math.PI / 4; + var inner = r * 1.35; + var outer = r * 1.75; + AddLine( + centerX + Math.Cos(angle) * inner, + centerY + Math.Sin(angle) * inner, + centerX + Math.Cos(angle) * outer, + centerY + Math.Sin(angle) * outer, + ModernUi.Bronze, + Math.Max(1.1, size * 0.055), + opacity * 0.72); + } + + if (animate && CanAnimate()) + { + AnimatePulse(sun, 0.76, 1, 1800); + } + } + + private void AddCloud(double size, double left, double top, double width, double height, double opacity, bool animate) + { + var x = size * left; + var y = size * top; + var w = size * width; + var h = size * height; + var fill = CloudBrush(); + var parts = new UIElement[] + { + AddEllipse(x + w * 0.05, y + h * 0.35, w * 0.32, h * 0.46, fill, opacity), + AddEllipse(x + w * 0.24, y + h * 0.10, w * 0.36, h * 0.62, fill, opacity), + AddEllipse(x + w * 0.48, y + h * 0.24, w * 0.34, h * 0.52, fill, opacity), + AddRoundedRect(x + w * 0.10, y + h * 0.48, w * 0.74, h * 0.34, fill, opacity) + }; + + if (animate && CanAnimate()) + { + foreach (var part in parts) + { + var transform = new TranslateTransform(); + part.RenderTransform = transform; + AnimateDrift(transform, size * 0.035, 2300, 0); + } + } + } + + private void AddFog(double size) + { + for (var index = 0; index < 3; index++) + { + var band = AddRoundedRect( + size * (0.18 + index * 0.04), + size * (0.58 + index * 0.12), + size * (0.62 - index * 0.03), + Math.Max(1.5, size * 0.065), + ModernUi.TextSecondary, + 0.62); + if (CanAnimate()) + { + var transform = new TranslateTransform(); + band.RenderTransform = transform; + AnimateDrift(transform, size * 0.06, 1800 + index * 180, index * 90); + } + } + } + + private void AddRain(double size, int count, bool drizzle) + { + var duration = _intensity == WeatherIntensity.Heavy ? 520 : _intensity == WeatherIntensity.Moderate ? 720 : 940; + for (var index = 0; index < count; index++) + { + var drop = AddRoundedRect( + size * (0.28 + index * 0.12), + size * (0.58 + index % 2 * 0.04), + Math.Max(1.1, size * 0.055), + size * (drizzle ? 0.14 : 0.22), + ModernUi.Accent, + 0.82); + RotateElement(drop, -15); + if (CanAnimate()) + { + var transform = new TranslateTransform(); + drop.RenderTransform = transform; + AnimateDrop(transform, drop, size * 0.16, duration, index * 120); + } + } + } + + private void AddSnow(double size, int count) + { + var duration = _intensity == WeatherIntensity.Heavy ? 780 : _intensity == WeatherIntensity.Moderate ? 980 : 1250; + for (var index = 0; index < count; index++) + { + var flake = AddEllipse( + size * (0.25 + index * 0.13), + size * (0.60 + index % 2 * 0.06), + Math.Max(2.2, size * 0.10), + Math.Max(2.2, size * 0.10), + ModernUi.Accent, + 0.78); + if (CanAnimate()) + { + var transform = new TranslateTransform(); + flake.RenderTransform = transform; + AnimateDrop(transform, flake, size * 0.14, duration, index * 150); + } + } + } + + private void AddBolt(double size) + { + var bolt = new Polyline + { + Stroke = ModernUi.Bronze, + StrokeThickness = Math.Max(1.7, size * 0.08), + StrokeLineJoin = PenLineJoin.Round, + Points = + { + new Point(size * 0.52, size * 0.48), + new Point(size * 0.42, size * 0.70), + new Point(size * 0.55, size * 0.68), + new Point(size * 0.48, size * 0.90) + }, + Opacity = 0.95 + }; + _canvas.Children.Add(bolt); + if (CanAnimate()) + { + AnimatePulse(bolt, 0.28, 1, 620); + } + } + + private void AddUnknownMark(double size) + { + AddLine(size * 0.40, size * 0.44, size * 0.60, size * 0.64, ModernUi.TextSecondary, Math.Max(1.4, size * 0.07), 0.72); + AddLine(size * 0.60, size * 0.44, size * 0.40, size * 0.64, ModernUi.TextSecondary, Math.Max(1.4, size * 0.07), 0.72); + } + + private Ellipse AddEllipse(double left, double top, double width, double height, Brush fill, double opacity) + { + var shape = new Ellipse + { + Width = width, + Height = height, + Fill = fill, + Opacity = opacity, + RenderTransformOrigin = new Point(0.5, 0.5) + }; + Canvas.SetLeft(shape, left); + Canvas.SetTop(shape, top); + _canvas.Children.Add(shape); + return shape; + } + + private Border AddRoundedRect(double left, double top, double width, double height, Brush fill, double opacity) + { + var border = new Border + { + Width = width, + Height = height, + CornerRadius = new CornerRadius(Math.Max(2, height / 2)), + Background = fill, + Opacity = opacity, + RenderTransformOrigin = new Point(0.5, 0.5) + }; + Canvas.SetLeft(border, left); + Canvas.SetTop(border, top); + _canvas.Children.Add(border); + return border; + } + + private Line AddLine(double x1, double y1, double x2, double y2, Brush stroke, double thickness, double opacity) + { + var line = new Line + { + X1 = x1, + Y1 = y1, + X2 = x2, + Y2 = y2, + Stroke = stroke, + StrokeThickness = thickness, + StrokeStartLineCap = PenLineCap.Round, + StrokeEndLineCap = PenLineCap.Round, + Opacity = opacity + }; + _canvas.Children.Add(line); + return line; + } + + private void RotateElement(UIElement element, double angle) + { + element.RenderTransformOrigin = new Point(0.5, 0.5); + element.RenderTransform = new RotateTransform { Angle = angle }; + } + + private bool CanAnimate() + => _animationEnabled && !_accessibility.HighContrast; + + private void AnimatePulse(UIElement target, double from, double to, double durationMs) + { + var animation = new DoubleAnimation + { + From = from, + To = to, + AutoReverse = true, + Duration = new Duration(TimeSpan.FromMilliseconds(durationMs)), + RepeatBehavior = RepeatBehavior.Forever + }; + Storyboard.SetTarget(animation, target); + Storyboard.SetTargetProperty(animation, "Opacity"); + _storyboard?.Children.Add(animation); + } + + private void AnimateDrop(TranslateTransform transform, UIElement target, double distance, double durationMs, double delayMs) + { + var move = new DoubleAnimation + { + From = -distance * 0.35, + To = distance, + BeginTime = TimeSpan.FromMilliseconds(delayMs), + Duration = new Duration(TimeSpan.FromMilliseconds(durationMs)), + RepeatBehavior = RepeatBehavior.Forever + }; + Storyboard.SetTarget(move, transform); + Storyboard.SetTargetProperty(move, "Y"); + _storyboard?.Children.Add(move); + + var fade = new DoubleAnimation + { + From = 0.2, + To = 0.9, + AutoReverse = true, + BeginTime = TimeSpan.FromMilliseconds(delayMs), + Duration = new Duration(TimeSpan.FromMilliseconds(durationMs * 0.55)), + RepeatBehavior = RepeatBehavior.Forever + }; + Storyboard.SetTarget(fade, target); + Storyboard.SetTargetProperty(fade, "Opacity"); + _storyboard?.Children.Add(fade); + } + + private void AnimateDrift(TranslateTransform transform, double distance, double durationMs, double delayMs) + { + var move = new DoubleAnimation + { + From = -distance, + To = distance, + AutoReverse = true, + BeginTime = TimeSpan.FromMilliseconds(delayMs), + Duration = new Duration(TimeSpan.FromMilliseconds(durationMs)), + RepeatBehavior = RepeatBehavior.Forever + }; + Storyboard.SetTarget(move, transform); + Storyboard.SetTargetProperty(move, "X"); + _storyboard?.Children.Add(move); + } + + private void StopStoryboard() + { + _storyboard?.Stop(); + _storyboard = null; + } + + private static int DropsFor(WeatherIntensity intensity) => intensity switch + { + WeatherIntensity.Heavy => 5, + WeatherIntensity.Moderate => 4, + WeatherIntensity.Light => 3, + _ => 2 + }; + + private static Brush CloudBrush() + => ModernUi.TextSecondary; +} diff --git a/src/box-winUI/Controls/MetricChartControl.cs b/src/box-winUI/Controls/MetricChartControl.cs new file mode 100644 index 0000000..df87b38 --- /dev/null +++ b/src/box-winUI/Controls/MetricChartControl.cs @@ -0,0 +1,204 @@ +using Microsoft.UI.Text; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Media; +using Microsoft.UI.Xaml.Shapes; +using Windows.Foundation; + +namespace YMhut.Box.WinUI.Controls; + +public sealed class MetricChartControl : UserControl +{ + private const int MaxSamples = 90; + + private readonly TextBlock _valueText; + private readonly TextBlock _detailText; + private readonly ProgressBar _bar; + private readonly Canvas _canvas = new() { MinHeight = 142, Height = 152 }; + private readonly Polyline _line = new() + { + Stroke = ModernUi.Accent, + StrokeThickness = 2.2 + }; + private readonly Queue _samples = new(); + + public MetricChartControl(string title, string subtitle, string glyph) + { + _valueText = ModernUi.Text("--", 24, FontWeights.SemiBold, maxLines: 1); + _valueText.TextAlignment = TextAlignment.Right; + _detailText = ModernUi.Text(subtitle, 12, foreground: ModernUi.TextSecondary, maxLines: 2); + _bar = new ProgressBar + { + Minimum = 0, + Maximum = 100, + Height = 5, + Foreground = ModernUi.Accent, + Background = ModernUi.SurfaceAlt + }; + + _canvas.SizeChanged += (_, _) => Redraw(); + + var header = new Grid { ColumnSpacing = 12 }; + header.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); + header.ColumnDefinitions.Add(new ColumnDefinition()); + header.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); + header.Children.Add(ModernUi.IconTile(glyph, 38, ModernUi.SurfaceAlt, ModernUi.Accent, 17)); + + var titleStack = new StackPanel + { + Spacing = 2, + Children = + { + ModernUi.Text(title, 17, FontWeights.SemiBold, maxLines: 1), + _detailText + } + }; + Grid.SetColumn(titleStack, 1); + header.Children.Add(titleStack); + Grid.SetColumn(_valueText, 2); + header.Children.Add(_valueText); + + Content = ModernUi.Card(new StackPanel + { + Spacing = 12, + Children = + { + header, + _bar, + _canvas + } + }, new Thickness(16), radius: 8); + } + + public void Update(double? percent, string valueText, string detailText) + { + _valueText.Text = valueText; + _detailText.Text = detailText; + if (percent is double value) + { + var clamped = Math.Clamp(value, 0, 100); + _bar.IsIndeterminate = false; + _bar.Value = clamped; + _samples.Enqueue(clamped); + while (_samples.Count > MaxSamples) + { + _samples.Dequeue(); + } + } + else + { + _bar.Value = 0; + } + + Redraw(); + } + + private void Redraw() + { + var width = _canvas.ActualWidth > 0 ? _canvas.ActualWidth : 320; + var height = _canvas.ActualHeight > 0 ? _canvas.ActualHeight : _canvas.Height; + if (double.IsNaN(height) || height <= 0) + { + height = 152; + } + + _canvas.Children.Clear(); + _canvas.Children.Add(new Border + { + Width = width, + Height = height, + Background = ChartBackground(), + CornerRadius = new CornerRadius(8) + }); + + var left = 34d; + var right = 10d; + var top = 10d; + var bottom = 20d; + var plotWidth = Math.Max(12, width - left - right); + var plotHeight = Math.Max(12, height - top - bottom); + DrawGrid(left, top, plotWidth, plotHeight); + + var values = _samples.ToArray(); + var points = new PointCollection(); + if (values.Length == 0 || width <= 0 || height <= 0) + { + _line.Points = points; + _canvas.Children.Add(_line); + return; + } + + for (var index = 0; index < values.Length; index++) + { + var x = left + (values.Length == 1 ? plotWidth : index * plotWidth / (values.Length - 1)); + var y = top + (100 - values[index]) / 100 * plotHeight; + points.Add(new Point(x, Math.Clamp(y, top, top + plotHeight))); + } + + _line.Points = points; + _line.Stroke = ModernUi.Accent; + _canvas.Children.Add(_line); + } + + private void DrawGrid(double left, double top, double width, double height) + { + var gridBrush = GridBrush(); + var labelBrush = LabelBrush(); + foreach (var value in new[] { 100, 75, 50, 25, 0 }) + { + var y = top + (100 - value) / 100d * height; + var line = new Line + { + X1 = left, + X2 = left + width, + Y1 = y, + Y2 = y, + Stroke = gridBrush, + StrokeThickness = 1, + Opacity = value is 0 or 100 ? 0.42 : 0.28 + }; + _canvas.Children.Add(line); + + var label = new TextBlock + { + Text = value.ToString(), + FontSize = 10.5, + Foreground = labelBrush, + Opacity = 0.78 + }; + Canvas.SetLeft(label, 6); + Canvas.SetTop(label, Math.Clamp(y - 8, 2, top + height - 12)); + _canvas.Children.Add(label); + } + + for (var index = 0; index <= 5; index++) + { + var x = left + index / 5d * width; + _canvas.Children.Add(new Line + { + X1 = x, + X2 = x, + Y1 = top, + Y2 = top + height, + Stroke = gridBrush, + StrokeThickness = 1, + Opacity = index is 0 or 5 ? 0.32 : 0.18 + }); + } + } + + private static SolidColorBrush ChartBackground() + => IsDarkTheme() ? ModernUi.Brush("#121820") : ModernUi.Brush("#202833"); + + private static SolidColorBrush GridBrush() + => IsDarkTheme() ? ModernUi.Brush("#7EA6C7") : ModernUi.Brush("#9FB4C8"); + + private static SolidColorBrush LabelBrush() + => IsDarkTheme() ? ModernUi.Brush("#C9D8E6") : ModernUi.Brush("#D7E4F0"); + + private static bool IsDarkTheme() + { + var color = ModernUi.AppBackground.Color; + return color.R + color.G + color.B < 384; + } +} diff --git a/src/box-winUI/Controls/MetricStripControl.cs b/src/box-winUI/Controls/MetricStripControl.cs new file mode 100644 index 0000000..271da58 --- /dev/null +++ b/src/box-winUI/Controls/MetricStripControl.cs @@ -0,0 +1,160 @@ +using Microsoft.UI.Text; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Automation; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Media; +using Microsoft.UI.Xaml.Shapes; +using Windows.Foundation; + +namespace YMhut.Box.WinUI.Controls; + +public sealed class MetricStripControl : UserControl +{ + private const int MaxSamples = 30; + + private readonly TextBlock _valueText; + private readonly ProgressBar _bar; + private readonly Canvas _trendCanvas = new() { Width = 58, Height = 18 }; + private readonly Polyline _trendLine = new() + { + Stroke = ModernUi.Accent, + StrokeThickness = 1.5 + }; + private readonly Queue _samples = new(); + + public MetricStripControl(string title, string glyph) + { + var icon = ModernUi.IconTile(glyph, 28, ModernUi.SurfaceAlt, ModernUi.TextSecondary, 12); + _valueText = ModernUi.Text("--", 13, FontWeights.SemiBold, maxLines: 1); + _valueText.TextAlignment = TextAlignment.Right; + _bar = new ProgressBar + { + Minimum = 0, + Maximum = 100, + Height = 4, + IsIndeterminate = false, + Foreground = ModernUi.Accent, + Background = ModernUi.SurfaceAlt + }; + + _trendCanvas.SizeChanged += (_, _) => RedrawTrend(); + + var header = new Grid { ColumnSpacing = 8 }; + header.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); + header.ColumnDefinitions.Add(new ColumnDefinition()); + header.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); + header.Children.Add(icon); + + var titleText = ModernUi.Text(title, 12, FontWeights.SemiBold, ModernUi.TextSecondary, maxLines: 1); + Grid.SetColumn(titleText, 1); + header.Children.Add(titleText); + Grid.SetColumn(_valueText, 2); + header.Children.Add(_valueText); + + var bottom = new Grid { ColumnSpacing = 8 }; + bottom.ColumnDefinitions.Add(new ColumnDefinition()); + bottom.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); + bottom.Children.Add(_bar); + Grid.SetColumn(_trendCanvas, 1); + bottom.Children.Add(_trendCanvas); + + Content = new StackPanel + { + Spacing = 5, + Children = + { + header, + bottom + } + }; + AutomationProperties.SetName(this, title); + } + + public void Update(double? percent, string valueText) + { + _valueText.Text = valueText; + if (percent is double value) + { + var clamped = Math.Clamp(value, 0, 100); + _bar.IsIndeterminate = false; + _bar.Value = clamped; + _samples.Enqueue(clamped); + while (_samples.Count > MaxSamples) + { + _samples.Dequeue(); + } + } + else + { + _bar.Value = 0; + } + + RedrawTrend(); + } + + private void RedrawTrend() + { + var width = _trendCanvas.ActualWidth > 0 ? _trendCanvas.ActualWidth : _trendCanvas.Width; + var height = _trendCanvas.ActualHeight > 0 ? _trendCanvas.ActualHeight : _trendCanvas.Height; + _trendCanvas.Children.Clear(); + DrawTrendGrid(width, height); + var values = _samples.ToArray(); + var points = new PointCollection(); + if (values.Length == 0 || width <= 0 || height <= 0) + { + _trendLine.Points = points; + _trendCanvas.Children.Add(_trendLine); + return; + } + + for (var index = 0; index < values.Length; index++) + { + var x = values.Length == 1 ? width : index * width / (values.Length - 1); + var y = height - (values[index] / 100 * height); + points.Add(new Point(x, Math.Clamp(y, 1, height - 1))); + } + + _trendLine.Points = points; + _trendLine.Stroke = ModernUi.Accent; + _trendCanvas.Children.Add(_trendLine); + } + + private void DrawTrendGrid(double width, double height) + { + if (width <= 0 || height <= 0) + { + return; + } + + var gridBrush = ModernUi.TextSecondary; + foreach (var fraction in new[] { 0.25, 0.5, 0.75 }) + { + var y = height * fraction; + _trendCanvas.Children.Add(new Line + { + X1 = 0, + X2 = width, + Y1 = y, + Y2 = y, + Stroke = gridBrush, + StrokeThickness = 1, + Opacity = 0.18 + }); + } + + foreach (var fraction in new[] { 0.33, 0.66 }) + { + var x = width * fraction; + _trendCanvas.Children.Add(new Line + { + X1 = x, + X2 = x, + Y1 = 0, + Y2 = height, + Stroke = gridBrush, + StrokeThickness = 1, + Opacity = 0.12 + }); + } + } +} diff --git a/src/box-winUI/Controls/WeatherCapsuleControl.cs b/src/box-winUI/Controls/WeatherCapsuleControl.cs new file mode 100644 index 0000000..a2bc1fe --- /dev/null +++ b/src/box-winUI/Controls/WeatherCapsuleControl.cs @@ -0,0 +1,268 @@ +using Microsoft.UI.Text; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Automation; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Controls.Primitives; +using Microsoft.UI.Xaml.Media; +using YMhut.Box.Core.Settings; +using YMhut.Box.WinUI.Services; + +namespace YMhut.Box.WinUI.Controls; + +public sealed class WeatherCapsuleControl : UserControl +{ + private readonly ITitleWeatherService _weatherService; + private readonly ISettingsService _settingsService = AppServices.GetRequiredService(); + private readonly Button _button; + private readonly AnimatedWeatherIconControl _weatherIcon; + private readonly ProgressRing _loadingRing; + private readonly TextBlock _locationText; + private readonly TextBlock _tempText; + private readonly TextBlock _conditionText; + private readonly Flyout _flyout; + + private TitleWeatherSnapshot _snapshot = TitleWeatherSnapshot.Loading; + private TitleWeatherSnapshot? _lastAvailableSnapshot; + private int _loadVersion; + + public WeatherCapsuleControl(ITitleWeatherService weatherService) + { + _weatherService = weatherService; + HorizontalAlignment = HorizontalAlignment.Right; + VerticalAlignment = VerticalAlignment.Center; + + _weatherIcon = new AnimatedWeatherIconControl(20) + { + HorizontalAlignment = HorizontalAlignment.Center, + VerticalAlignment = VerticalAlignment.Center + }; + _loadingRing = new ProgressRing + { + Width = 18, + Height = 18, + IsActive = true, + Visibility = Visibility.Collapsed + }; + _locationText = ModernUi.Text(_snapshot.Location, 12, FontWeights.SemiBold, ModernUi.TextSecondary, maxLines: 1); + _conditionText = ModernUi.Text(_snapshot.Condition, 10.5, foreground: ModernUi.TextSecondary, maxLines: 1); + _tempText = ModernUi.Text(_snapshot.TemperatureText, 15, FontWeights.SemiBold, ModernUi.TextPrimary, maxLines: 1); + ConfigureCompactLine(_locationText, 14); + ConfigureCompactLine(_conditionText, 13); + ConfigureCompactLine(_tempText, 18); + + var visual = new Grid + { + ColumnSpacing = 9, + VerticalAlignment = VerticalAlignment.Center, + ColumnDefinitions = + { + new ColumnDefinition { Width = GridLength.Auto }, + new ColumnDefinition(), + new ColumnDefinition { Width = GridLength.Auto } + } + }; + var iconHost = new Grid { Width = 22, Height = 22, Children = { _weatherIcon, _loadingRing } }; + visual.Children.Add(iconHost); + var text = new Grid + { + MinWidth = 76, + MaxWidth = 130, + VerticalAlignment = VerticalAlignment.Center, + RowDefinitions = + { + new RowDefinition { Height = GridLength.Auto }, + new RowDefinition { Height = GridLength.Auto } + } + }; + text.Children.Add(_locationText); + Grid.SetRow(_conditionText, 1); + text.Children.Add(_conditionText); + Grid.SetColumn(text, 1); + visual.Children.Add(text); + Grid.SetColumn(_tempText, 2); + visual.Children.Add(_tempText); + + _flyout = new Flyout { Placement = FlyoutPlacementMode.BottomEdgeAlignedRight }; + _flyout.Opening += (_, _) => _flyout.Content = BuildFlyoutContent(); + + _button = new Button + { + Height = 38, + MinWidth = 168, + MaxWidth = 242, + Margin = new Thickness(0, 4, 0, 4), + Padding = new Thickness(12, 4, 12, 4), + CornerRadius = new CornerRadius(19), + Background = ModernUi.Surface, + BorderBrush = ModernUi.Stroke, + BorderThickness = new Thickness(1), + Content = visual, + Flyout = _flyout + }; + AutomationProperties.SetName(_button, AppLocalizer.T("天气", "Weather")); + ToolTipService.SetToolTip(_button, AppLocalizer.T("查看天气详情", "Show weather details")); + Content = _button; + ApplySnapshot(_snapshot); + } + + public async Task LoadAsync(CancellationToken cancellationToken = default) + { + await RefreshAsync(cancellationToken).ConfigureAwait(false); + } + + public async Task RefreshAsync(CancellationToken cancellationToken = default) + { + var loadVersion = Interlocked.Increment(ref _loadVersion); + DispatcherQueue.TryEnqueue(() => ApplySnapshot(TitleWeatherSnapshot.Loading, loadingOverlay: true)); + var snapshot = await _weatherService.GetCurrentAsync(cancellationToken).ConfigureAwait(false); + if (loadVersion != _loadVersion) + { + return; + } + + DispatcherQueue.TryEnqueue(() => ApplySnapshot(snapshot)); + } + + public void RefreshLanguage() + { + ApplySnapshot(_snapshot); + } + + private void ApplySnapshot(TitleWeatherSnapshot snapshot, bool loadingOverlay = false) + { + var displaySnapshot = loadingOverlay && _lastAvailableSnapshot is not null + ? _lastAvailableSnapshot + : snapshot; + _snapshot = displaySnapshot; + if (displaySnapshot.IsAvailable) + { + _lastAvailableSnapshot = displaySnapshot; + } + + var loading = loadingOverlay || ReferenceEquals(snapshot, TitleWeatherSnapshot.Loading); + _loadingRing.IsActive = loading; + _loadingRing.Visibility = loading ? Visibility.Visible : Visibility.Collapsed; + _loadingRing.Opacity = loading ? 0.78 : 0; + _weatherIcon.Visibility = Visibility.Visible; + _weatherIcon.Opacity = loading ? 0.62 : 1; + _weatherIcon.Update(displaySnapshot, _settingsService.Current.AnimationsEnabled); + _locationText.Text = displaySnapshot.Location; + _conditionText.Text = displaySnapshot.Condition; + _tempText.Text = displaySnapshot.TemperatureText; + _button.Background = displaySnapshot.IsAvailable ? ModernUi.Surface : ModernUi.SurfaceAlt; + _button.BorderBrush = displaySnapshot.IsAvailable ? ModernUi.Stroke : ModernUi.StrokeStrong; + ToolTipService.SetToolTip(_button, loading ? AppLocalizer.T("天气正在刷新", "Weather is refreshing") : BuildTooltip(displaySnapshot)); + AutomationProperties.SetName(_button, loading ? AppLocalizer.T("天气正在刷新", "Weather is refreshing") : BuildTooltip(displaySnapshot)); + } + + private string BuildTooltip(TitleWeatherSnapshot snapshot) + { + return snapshot.IsAvailable + ? $"{snapshot.Location} {snapshot.Condition} {snapshot.TemperatureText}" + : AppLocalizer.T("天气暂不可用,点击重试", "Weather unavailable. Click to retry."); + } + + private UIElement BuildFlyoutContent() + { + var snapshot = _snapshot; + var refresh = ModernUi.PillButton(AppLocalizer.T("刷新", "Refresh"), "\uE72C", async () => await RefreshAsync(), primary: true); + refresh.HorizontalAlignment = HorizontalAlignment.Right; + Grid.SetColumn(refresh, 2); + + return new StackPanel + { + Width = 280, + Padding = new Thickness(4), + Spacing = 12, + Children = + { + new Grid + { + ColumnSpacing = 12, + ColumnDefinitions = + { + new ColumnDefinition { Width = GridLength.Auto }, + new ColumnDefinition(), + new ColumnDefinition { Width = GridLength.Auto } + }, + Children = + { + BuildFlyoutIcon(snapshot), + BuildTitleBlock(snapshot), + refresh + } + }, + BuildDetailLine(AppLocalizer.T("体感", "Feels like"), snapshot.FeelsLikeText, "\uE706"), + BuildDetailLine(AppLocalizer.T("湿度", "Humidity"), snapshot.HumidityText, "\uE81F"), + BuildDetailLine(AppLocalizer.T("风速", "Wind"), snapshot.WindText, "\uE9CA"), + BuildDetailLine(AppLocalizer.T("今日温度", "Today"), snapshot.RangeText, "\uE787"), + BuildDetailLine(AppLocalizer.T("更新时间", "Updated"), snapshot.UpdatedText, "\uE823"), + snapshot.ErrorMessage is null + ? new Border { Height = 0 } + : ModernUi.Card( + ModernUi.Text(snapshot.ErrorMessage, 12, foreground: ModernUi.TextSecondary, maxLines: 3), + new Thickness(10), + radius: 8, + background: ModernUi.SurfaceAlt) + } + }; + } + + private static StackPanel BuildTitleBlock(TitleWeatherSnapshot snapshot) + { + var panel = new StackPanel + { + Spacing = 1, + VerticalAlignment = VerticalAlignment.Center, + Children = + { + ModernUi.Text(snapshot.Location, 17, FontWeights.SemiBold, maxLines: 1), + ModernUi.Text($"{snapshot.Condition} · {snapshot.TemperatureText} · {snapshot.QueryLevel}", 13, foreground: ModernUi.TextSecondary, maxLines: 1) + } + }; + Grid.SetColumn(panel, 1); + return panel; + } + + private UIElement BuildFlyoutIcon(TitleWeatherSnapshot snapshot) + { + var icon = new AnimatedWeatherIconControl(44); + icon.Update(snapshot, _settingsService.Current.AnimationsEnabled); + return new Border + { + Width = 52, + Height = 52, + CornerRadius = new CornerRadius(8), + Background = ModernUi.AccentSoft, + BorderBrush = ModernUi.Stroke, + BorderThickness = new Thickness(1), + Child = icon + }; + } + + private static void ConfigureCompactLine(TextBlock text, double lineHeight) + { + text.TextWrapping = TextWrapping.NoWrap; + text.TextTrimming = TextTrimming.CharacterEllipsis; + text.LineHeight = lineHeight; + text.VerticalAlignment = VerticalAlignment.Center; + } + + private static UIElement BuildDetailLine(string label, string value, string glyph) + { + var grid = new Grid { ColumnSpacing = 10 }; + grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); + grid.ColumnDefinitions.Add(new ColumnDefinition()); + grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); + grid.Children.Add(ModernUi.IconTile(glyph, 30, ModernUi.SurfaceAlt, ModernUi.TextSecondary, 13)); + + var title = ModernUi.Text(label, 13, FontWeights.SemiBold, ModernUi.TextSecondary, maxLines: 1); + Grid.SetColumn(title, 1); + grid.Children.Add(title); + + var text = ModernUi.Text(value, 13, FontWeights.SemiBold, ModernUi.TextPrimary, maxLines: 1); + Grid.SetColumn(text, 2); + grid.Children.Add(text); + return grid; + } +} diff --git a/src/box-winUI/CrashLog.cs b/src/box-winUI/CrashLog.cs new file mode 100644 index 0000000..b8063f6 --- /dev/null +++ b/src/box-winUI/CrashLog.cs @@ -0,0 +1,38 @@ +using System.Text; + +namespace YMhut.Box.WinUI; + +public static class CrashLog +{ + private static readonly object Gate = new(); + + public static string LogPath + { + get + { + var root = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), + "YMhut Box", + "WinUI", + "Logs"); + Directory.CreateDirectory(root); + return Path.Combine(root, "startup-crash.log"); + } + } + + public static void Write(string message) + { + lock (Gate) + { + File.AppendAllText( + LogPath, + $"[{DateTimeOffset.Now:O}] {message}{Environment.NewLine}", + Encoding.UTF8); + } + } + + public static void Write(Exception? exception) + { + Write(exception?.ToString() ?? "Unknown exception."); + } +} diff --git a/src/box-winUI/MainWindow.xaml b/src/box-winUI/MainWindow.xaml new file mode 100644 index 0000000..69b6c4a --- /dev/null +++ b/src/box-winUI/MainWindow.xaml @@ -0,0 +1,110 @@ + + + + + + + + +