Пример #1
0
        /// <summary>
        /// Download assets from repos. The download manifest is also updated.
        /// </summary>
        /// <param name="manifest">Download manifest</param>
        /// <param name="assetsInfo">Assets to download</param>
        /// <param name="downloadProgress">Download progress interface</param>
        /// <returns></returns>
        private async Task <bool> DownloadFromReposInternalAsync(
            AssetsManifest manifest, Dictionary <string, AssetInfo> assetsInfo, IDownloadProgress downloadProgress = null)
        {
            var downloadedAsset = false;

            foreach (var entry in assetsInfo)
            {
                // Download asset
                var success = await DownloadAssetFromGitHubRepoAsync(entry.Key, entry.Value, downloadProgress);

                if (success)
                {
                    // Update manifest
                    manifest.Metadata[entry.Key] = entry.Value;
                    downloadedAsset = true;
                }
            }

            if (downloadedAsset)
            {
                // Persist manifest
                SaveManifest(manifest);
            }

            return(downloadedAsset);
        }
Пример #2
0
        /// <summary>
        /// Asynchronously download assets from configured repos.
        /// </summary>
        /// <param name="config">Current config</param>
        /// <param name="downloadProgress">Download progress interface</param>
        /// <returns></returns>
        private async Task <bool> DownloadFromReposAsync(Config config, IDownloadProgress downloadProgress = null, bool forceCheck = false)
        {
            if (null == config.downloadRepos)
            {
                // No repos configured, so return
                return(false);
            }

            // Load the download manifest. This is used to compare if we're up-to-date or not.
            AssetsManifest manifest = LoadManifest();

            if (null == manifest)
            {
                manifest = new AssetsManifest();
            }

            // Rate limit update checks to one per couple of minutes, to avoid GitHub's rate limit
            if (!forceCheck && DateTime.Now.Subtract(manifest.LastUpdated).TotalMinutes < RateLimitInMins)
            {
                Debug.LogFormat("Exceeded rate limit of {0} mins - last checked for update on {1}", RateLimitInMins, manifest.LastUpdated);
                return(false);
            }

            // Mark that we've just checked for updates & update manifest
            manifest.LastUpdated = DateTime.Now;
            SaveManifest(manifest);

            if (null != downloadProgress)
            {
                downloadProgress.OnCheckingForUpdates();
            }

            // Download the assets metadata - used to determine whether we are up-to-date or not
            var assetsInfo = await DownloadAssetsMetadata(config, manifest, downloadProgress, forceCheck);

            if (assetsInfo.Count == 0)
            {
                // No updates have been found, so return
                Debug.Log("No updates found");
                if (null != downloadProgress)
                {
                    downloadProgress.OnNoUpdatesAvailable();
                }

                return(false);
            }

            // Download the assets
            var downloadedAssets = await DownloadFromReposInternalAsync(manifest, assetsInfo, downloadProgress);

            if (null != downloadProgress)
            {
                downloadProgress.OnUpdateFinish();
            }

            return(downloadedAssets);
        }
 private void ProcessingAssetInfos(List <AssetReference> assetInfos,
                                   AssetsManifest <AssetInfo> manifest)
 {
     foreach (var assetReference in assetInfos)
     {
         if (!_ignoreList.Contains(assetReference.fileNameAndExtension))
         {
             var assetInfo = new AssetInfo();
             assetInfo.name = ProcessPathFromManifest(assetReference.path).Replace(assetReference.extension, "");
             assetInfo.ext  = assetReference.extension;
             manifest._assetsInfos.Add(assetInfo.name, assetInfo);
         }
     }
 }
Пример #4
0
        /// <summary>
        /// Persist download manifest
        /// </summary>
        /// <param name="manifest">Download manifest to persist</param>
        static private void SaveManifest(AssetsManifest manifest)
        {
            var manifestPath = Path.Combine(GetOrCreateDownloadPath(), DownloadManifestFile);

            Debug.Log("Saving manifest: " + manifestPath);

            try
            {
                File.WriteAllText(manifestPath, JsonConvert.SerializeObject(manifest, Formatting.Indented));
            }
            catch (Exception e)
            {
                Debug.Log(string.Format("Failed to write manifest: {0}", e.Message));
            }
        }
Пример #5
0
        private static void MarkAssetsFromABData()
        {
            AssetsManifest assetsManifest = BuildScript.GetManifest();

            assetsManifest.Clear();
            ABPathData pathData = BuildScript.GetAsset <ABPathData>("Assets/AssetPathData.asset");

            for (int i = 0; i < pathData.assetPaths.Count; i++)
            {
                if (pathData.assetPaths[i].type == AssetPathType.FOLDER)
                {
                    MarkAssetsWithDir(pathData.assetPaths[i].path);
                }
                else
                {
                    MarkAssetsWithFile(pathData.assetPaths[i].path);
                }
            }
        }
    public static void createBundleManifest()
    {
        var manifest1 = new AssetsManifest();
        var asset = new ExpanseAsset("Capsule");
        manifest1.addPlacibleAsset( asset );
        asset = new ExpanseAsset("Sphere");
        manifest1.addPlacibleAsset( asset );
        var jsonString =  serialize(typeof(AssetsManifest), manifest1);
        Debug.Log(jsonString);

        var manifest2 = new AssetsManifest();
        asset = new ExpanseAsset("Cylinder");
        manifest2.addPlacibleAsset(asset);
        jsonString = serialize(typeof(AssetsManifest), manifest2);
        Debug.Log(jsonString);

        Debug.Log("stuff :: " + Application.dataPath);

        File.WriteAllText(Application.dataPath + @"/assets_to_bundle", jsonString);
        //		File.WriteAllText(@"C:\", jsonString);
        //
    }
    public override void Build(BuildTarget target, Model.NodeData node, IEnumerable <PerformGraph.AssetGroups> incoming,
                               IEnumerable <Model.ConnectionData> connectionsToOutput, PerformGraph.Output outputFunc,
                               Action <Model.NodeData, string, float> progressFunc)
    {
        var customManifest = new AssetsManifest <AssetInfo>();

        foreach (var assetGroups in incoming)
        {
            foreach (var ag in assetGroups.assetGroups)
            {
                var assetInfos = ag.Value;
                ProcessingAssetInfos(assetInfos, customManifest);
            }
        }

        var jsonSerializer =
            new JsonSerializer(new JsonSerializerSettings {
            Formatting = Formatting.Indented
        }, Encoding.UTF8);
        var manifestAsJson = jsonSerializer.SerializeString(customManifest);

        var rootFolder = Path.Combine(Directory.GetParent(Application.dataPath).ToString(),
                                      _manifestDirectoryInternalRelative);

        if (!Directory.Exists(rootFolder))
        {
            Directory.CreateDirectory(rootFolder);
        }

        if (_copyManifestExternal && !String.IsNullOrEmpty(_manifestDirectoryExport))
        {
            File.WriteAllText(GetCustomManifestPath(_manifestDirectoryExport), manifestAsJson);
            Process.Start(_manifestDirectoryExport);
        }
        else
        {
            File.WriteAllText(GetCustomManifestPath(rootFolder), manifestAsJson);
        }
    }
Пример #8
0
        /// <summary>
        /// Load download manifest
        /// </summary>
        /// <returns>Download manifest</returns>
        static private AssetsManifest LoadManifest()
        {
            var manifestPath = Path.Combine(GetOrCreateDownloadPath(), DownloadManifestFile);

            if (!File.Exists(manifestPath))
            {
                return(null);
            }

            Debug.Log("Found manifest: " + manifestPath);
            var jsonManifest = File.ReadAllText(manifestPath);

            try
            {
                AssetsManifest manifest = new AssetsManifest();
                return(JsonConvert.DeserializeObject <AssetsManifest>(jsonManifest));
            }
            catch (Exception e)
            {
                Debug.Log(string.Format("Failed to read manifest: {0}", e.Message));
            }

            return(null);
        }
Пример #9
0
        /// 传入manifestCache表示外部负责文件保存,避免 loop 中多次调用AssetDatabase.SaveAssets影响速度
        public static void SetAssetBundleNameAndVariant(string assetPath, string bundleName, string variant, AssetsManifest manifestCache = null)
        {
            var manifest = manifestCache == null?GetManifest() : manifestCache;

            var dir      = Path.GetDirectoryName(assetPath).Replace("\\", "/");
            var dirs     = manifest.dirs;
            var dirIndex = ArrayUtility.FindIndex(dirs, (string obj) => { return(obj == dir); });

            if (dirIndex == -1)
            {
                ArrayUtility.Add(ref manifest.dirs, dir);
                dirIndex = manifest.dirs.Length - 1;
                dirs     = manifest.dirs;
            }

            var assetBundleNames = manifest.bundles;
            var bundleIndex      = ArrayUtility.FindIndex(assetBundleNames, (string obj) => { return(obj == bundleName); });

            if (bundleIndex == -1)
            {
                ArrayUtility.Add(ref manifest.bundles, bundleName);
                assetBundleNames = manifest.bundles;
                bundleIndex      = assetBundleNames.Length - 1;
            }

            var variantNames = manifest.activeVariants;
            var variantIndex = ArrayUtility.FindIndex(variantNames, (string obj) => { return(obj == variant); });

            if (variantIndex == -1 && !string.IsNullOrEmpty(variant))
            {
                ArrayUtility.Add(ref manifest.activeVariants, variant);
                variantNames = manifest.activeVariants;
                variantIndex = variantNames.Length - 1;
            }

            var assets     = manifest.assets;
            var assetIndex = ArrayUtility.FindIndex(assets, (AssetData obj) =>
            {
                var path = dirs[obj.dir] + "/" + obj.name;
                return(path == assetPath);
            });

            if (assetIndex == -1)
            {
                var info = new AssetData();
                ArrayUtility.Add(ref manifest.assets, info);
                assetIndex = manifest.assets.Length - 1;
                assets     = manifest.assets;
            }

            var asset = assets[assetIndex];

            asset.name    = Path.GetFileName(assetPath);
            asset.bundle  = bundleIndex;
            asset.variant = variantIndex;
            asset.dir     = dirIndex;
            if (manifestCache == null)
            {
                EditorUtility.SetDirty(manifest);
                AssetDatabase.SaveAssets();
            }
        }
Пример #10
0
        /// <summary>
        /// Downloads assets metadata. This is used to determine whether our assets are up-to-date.
        /// </summary>
        /// <param name="config">Current config</param>
        /// <param name="downloadProgress">Download progress interface</param>
        /// <returns></returns>
        private async Task <Dictionary <string, AssetInfo> > DownloadAssetsMetadata(
            Config config, AssetsManifest manifest, IDownloadProgress downloadProgress = null, bool forceCheck = false)
        {
            // Get asset info from repos
            var assetsInfo = new Dictionary <string, AssetInfo>(StringComparer.OrdinalIgnoreCase);

            // Get the set of repo URIs (removing any duplicates)
            var configRepos = new HashSet <string>();

            foreach (var item in config.downloadRepos)
            {
                if (null == item.type || !string.Equals(item.type, Config.DownloadRepo_Type_GitHub, StringComparison.OrdinalIgnoreCase))
                {
                    // For now, we only support GitHub repos
                    continue;
                }

                configRepos.Add(item.repoUri);
            }

            var reposLoaded = new HashSet <string>();

            foreach (var repoUri in configRepos)
            {
                // Get assets from the GitHub repo
                var repoLoaded = await GetAssetsInfoFromGithubRepoAsync(repoUri, assetsInfo, downloadProgress);

                if (repoLoaded)
                {
                    reposLoaded.Add(repoUri);
                }
            }

            Debug.LogFormat("Assets info contains {0} entries", assetsInfo.Count);

            // Enumerate asset metadata in our download manifest
            var deletedAssets = new HashSet <string>();

            foreach (var entry in manifest.Metadata)
            {
                if (assetsInfo.ContainsKey(entry.Key))
                {
                    if (string.Equals(entry.Value.UpdatedAt, assetsInfo[entry.Key].UpdatedAt, StringComparison.OrdinalIgnoreCase) &&
                        File.Exists(Path.Combine(GetOrCreateDownloadPath(), entry.Key)))
                    {
                        // Matched on file name and updated-at timestamp, so we're up to date. Skip this one.
                        Debug.LogFormat("Asset {0} is already up to date ({1})", entry.Key, entry.Value.UpdatedAt);
                        assetsInfo.Remove(entry.Key);
                    }
                }
                else if (reposLoaded.Contains(entry.Value.RepoUri))
                {
                    // The repo no longer has the cached file, so nuke it to keep things in sync
                    var filePath = Path.Combine(GetOrCreateDownloadPath(), entry.Key);
                    if (File.Exists(filePath))
                    {
                        Debug.LogFormat("Deleting cached file that no longer exists on server: {0} @ {1}", filePath, entry.Value.RepoUri);
                        File.Delete(filePath);
                    }
                    deletedAssets.Add(entry.Key);
                }
            }

            // Remove old cached files
            foreach (var entry in deletedAssets)
            {
                manifest.Metadata.Remove(entry);
            }

            if (deletedAssets.Count > 0)
            {
                // Persist manifest
                SaveManifest(manifest);
            }

            return(assetsInfo);
        }
        public void DownloadAssets()
        {
            // UpdateStatusBarAndLog("Checking game assets...");
            jsinit.InstallBarProgress("Проверка ресурсов...", 40);
            VersionManifest selectedVersionManifest = VersionManifest.ParseVersion(
                new DirectoryInfo(_configuration.McVersions + @"\" +
                                  (_versionToLaunch ??
                                   (_selectedProfile.SelectedVersion ?? GetLatestVersion(_selectedProfile)))));

            if (selectedVersionManifest.InheritsFrom != null)
            {
                selectedVersionManifest = selectedVersionManifest.InheritableVersionManifest;
            }
            string file = string.Format(@"{0}\assets\indexes\{1}.json", _configuration.McDirectory,
                                        selectedVersionManifest.AssetsIndex ?? "legacy");

            if (!File.Exists(file))
            {
                if (!Directory.Exists(Path.GetDirectoryName(file)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(file));
                }
                new WebClient().DownloadFile(selectedVersionManifest.GetAssetIndexDownloadUrl(), file);
            }
            AssetsManifest manifest = AssetsManifest.Parse(file);

            StatusBarValue = 0;
            SetStatusBarMaxValue(manifest.Objects.Select(pair => pair.Value.Hash.GetFullPath()).Count(filename => !File.Exists(_configuration.McDirectory + @"\assets\objects\" +
                                                                                                                               filename) || _restoreVersion) + 1);
            int maxlen = manifest.Objects.Select(pair => pair.Value).Where(asset => !File.Exists(_configuration.McDirectory + @"\assets\objects\" +
                                                                                                 asset.Hash.GetFullPath())).Count();
            //MessageBox.Show(maxlen.ToString());
            int step = 0;

            foreach (Asset asset in manifest.Objects.Select(pair => pair.Value).Where(asset => !File.Exists(_configuration.McDirectory + @"\assets\objects\" +
                                                                                                            asset.Hash.GetFullPath()) || _restoreVersion))
            {
                string directory = _configuration.McDirectory + @"\assets\objects\" + asset.Hash.GetDirectoryName();
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }
                try
                {
                    // AppendDebug($"Downloading {asset.Hash}...");
                    step++;
                    int stepf = Convert.ToInt32(Math.Truncate(step * (50F / (float)maxlen)));
                    jsinit.InstallBarProgress("Загрузка ресурса " + asset.AssociatedName, 40 + stepf);
                    new WebClient().DownloadFile(@"http://resources.download.minecraft.net/" + asset.Hash.GetFullPath(),
                                                 _configuration.McDirectory + @"\assets\objects\" + asset.Hash.GetFullPath());
                }
                catch (Exception ex)
                {
                    //  AppendException(ex.ToString());
                }
                StatusBarValue++;
            }
            jsinit.InstallBarProgress("Проверка ресурсов завершена", 90);
            if (selectedVersionManifest.AssetsIndex == null || selectedVersionManifest.AssetsIndex == "legacy")
            {
                StatusBarValue = 0;
                SetStatusBarMaxValue(manifest.Objects.Select(pair => pair.Value.AssociatedName)
                                     .Count(
                                         filename =>
                                         !File.Exists(_configuration.McDirectory + @"\assets\virtual\legacy\" +
                                                      filename) || _restoreVersion) + 1);
                jsinit.InstallBarProgress("Конвертация ресурсов...", 90);
                foreach (Asset asset in manifest.Objects.Select(pair => pair.Value)
                         .Where(asset =>
                                !File.Exists(_configuration.McDirectory + @"\assets\virtual\legacy\" +
                                             asset.AssociatedName) || _restoreVersion))
                {
                    string filename = _configuration.McDirectory + @"\assets\virtual\legacy\" + asset.AssociatedName;
                    try
                    {
                        if (!Directory.Exists(new FileInfo(filename).DirectoryName))
                        {
                            Directory.CreateDirectory(new FileInfo(filename).DirectoryName);
                        }

                        File.Copy(_configuration.McDirectory + @"\assets\objects\" + asset.Hash.GetFullPath(),
                                  filename);
                    }
                    catch (Exception ex)
                    {
                        //  AppendLog(ex.ToString());
                    }
                    StatusBarValue++;
                }
                jsinit.InstallBarProgress("Конвертация ресурсов завершена", 60);
            }
            SetStatusBarVisibility(false);
        }