Example #1
0
        private DownloadInfo ResolveDownloadInfo(ApiAsset asset)
        {
            if (asset == null)
            {
                throw new ArgumentNullException(nameof(asset));
            }

            switch (asset.Type)
            {
            case AssetType.Blueprint:
            case AssetType.Savegame:
            case AssetType.Scenario:
                return(new DownloadInfo(asset.Resource.Data.Url, null, null));

            case AssetType.Mod:
                var repoUrl      = asset.Resource.Data.Source;
                var repoUrlParts = repoUrl.Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                var repo         = $"{repoUrlParts[repoUrlParts.Length - 2]}/{repoUrlParts[repoUrlParts.Length - 1]}";

                if (asset.Resource.Data.ZipBall == null)
                {
                    throw new Exception("mod has not yet been released(tagged)");
                }
                return(new DownloadInfo(asset.Resource.Data.ZipBall, repo, asset.Resource.Data.Version));

            default:
                throw new Exception("unsupported mod type");
            }
        }
Example #2
0
 public IActionResult Index(string assetInputId)
 {
     if (assetInputId != null)
     {
         int       assetId   = int.Parse(assetInputId);
         ApiClient apiClient = new ApiClient();
         ApiAsset  asset     = apiClient.GetAsset(assetId).Result;
         return(View(asset));
     }
     return(View());
 }
    public void GenerateAssets(OpenApiDocument doc)
    {
        string assetsPath = EditorUtility.OpenFolderPanel("Select assets folder", _lastAssetPath, "");

        _lastAssetPath = assetsPath;
        assetsPath     = assetsPath.Substring(assetsPath.IndexOf("Assets"));
        ApiAsset apiAsset = AssetsHelper.GetOrCreateScriptableObject <ApiAsset>(assetsPath, doc.Info.Title);

        #region ApiAsset update
        apiAsset.info = new OAInfo()
        {
            Title          = doc.Info.Title,
            Description    = doc.Info.Description,
            Version        = doc.Info.Version,
            TermsOfService = doc.Info.TermsOfService == null ? "" : doc.Info.TermsOfService.ToString(),
            Contact        = CreateContact(doc.Info.Contact),
            License        = CreateLicence(doc.Info.License),
        };

        apiAsset.externalDocs = CreateExternalDocs(doc.ExternalDocs);

        apiAsset.servers = doc.Servers.Select(s => CreateAOServer(s)).ToList();


        apiAsset.pathItemAssets = new List <PathItemAsset>();

        #endregion

        foreach (var p in doc.Paths)
        {
            string        fileName = p.Key.Replace('/', '_');
            PathItemAsset a        = AssetsHelper.GetOrCreateScriptableObject <PathItemAsset>(assetsPath, fileName);
            a.ApiAsset = apiAsset;

            #region path item update


            a.Path = p.Key;

            a.Summary     = p.Value.Summary;
            a.Description = p.Value.Description;
            a.Parameters  = p.Value.Parameters.Select(par => CreateAOParameter(par)).ToList();
            a.Operations  = p.Value.Operations.Select(o => CreateAOOperation(o.Key, o.Value, a)).ToList();
            a.Servers     = p.Value.Servers.Select(s => CreateAOServer(s)).ToList();

            #endregion

            apiAsset.pathItemAssets.Add(a);
        }



        AssetDatabase.SaveAssets();
    }
Example #4
0
        public async Task <ApiAsset> GetAsset(int id)
        {
            string url      = BaseURL + id + Code;
            var    response = await client.GetAsync(new Uri(url));

            if (response.IsSuccessStatusCode)
            {
                var result = await response.Content.ReadAsStringAsync();

                ApiAsset apiAsset = JsonConvert.DeserializeObject <ApiAsset>(result);
                return(apiAsset);
            }
            return(null);
        }
Example #5
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="DownloadedAsset" /> class.
 /// </summary>
 /// <param name="fileName">Name of the file.</param>
 /// <param name="apiAsset"></param>
 /// <param name="info"></param>
 /// <param name="stream">The stream.</param>
 /// <exception cref="ArgumentNullException">fileName or stream is null.</exception>
 public DownloadedAsset(string fileName, ApiAsset apiAsset, DownloadInfo info, Stream stream)
 {
     if (fileName == null)
     {
         throw new ArgumentNullException(nameof(fileName));
     }
     if (apiAsset == null)
     {
         throw new ArgumentNullException(nameof(apiAsset));
     }
     if (stream == null)
     {
         throw new ArgumentNullException(nameof(stream));
     }
     FileName = fileName;
     ApiAsset = apiAsset;
     Info     = info;
     Stream   = stream;
 }
Example #6
0
        /// <summary>
        ///     Downloads the asset.
        /// </summary>
        /// <param name="asset">The asset.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="Exception">the specified asset id is invalid</exception>
        public async Task <IDownloadedAsset> DownloadAsset(ApiAsset asset)
        {
            if (asset == null)
            {
                throw new ArgumentNullException(nameof(asset));
            }

            var downloadInfo = ResolveDownloadInfo(asset);

            using (var webClient = _webClientFactory.CreateWebClient(asset.Type != AssetType.Mod))
            {
                using (var stream = await webClient.OpenReadTaskAsync(downloadInfo.Url))
                {
                    // Read content information from the headers.
                    var contentDispositionHeader = webClient.ResponseHeaders.GetValues("Content-Disposition").First();
                    var contentLengthHeader      = webClient.ResponseHeaders.GetValues("Content-Length").First();
                    var contentTypeHeader        = webClient.ResponseHeaders.GetValues("Content-Type").First();

                    // Ensure the required content headers exist.
                    if (string.IsNullOrWhiteSpace(contentDispositionHeader) ||
                        string.IsNullOrWhiteSpace(contentTypeHeader))
                    {
                        throw new Exception("invalid headers");
                    }

                    // Parse the content length header to an integer.
                    var contentLength = 0;
                    if (contentLengthHeader != null && !int.TryParse(contentLengthHeader, out contentLength))
                    {
                        throw new Exception("invalid headers");
                    }

                    // Get asset information for the asset type specified in the url.
                    var assetInfo = asset.Type.GetCustomAttribute <AssetInfoAttribute>();

                    // Ensure the type of the received content matches the expected content type.
                    if (assetInfo == null || !assetInfo.ContentType.Contains(contentTypeHeader.Split(';').FirstOrDefault()))
                    {
                        throw new Exception("invalid response type");
                    }

                    // Extract the filename of the asset from the content disposition header.
                    var fileNameMatch = Regex.Match(contentDispositionHeader, @"attachment; filename=(""?)(.*)\1");

                    if (fileNameMatch == null || !fileNameMatch.Success)
                    {
                        throw new Exception("invalid headers");
                    }

                    var fileName = fileNameMatch.Groups[2].Value;

                    // Copy the contents of the downloaded stream to a memory stream.
                    var memoryStream = new MemoryStream();
                    await stream.CopyToAsync(memoryStream);

                    // Verify we received all content.
                    if (contentLengthHeader != null && memoryStream.Length != contentLength)
                    {
                        throw new Exception("unexpected end of stream");
                    }

                    // Create an instance of ParkitectAsset with the received content and data.
                    return(new DownloadedAsset(fileName, asset, downloadInfo, memoryStream));
                }
            }
        }