Beispiel #1
0
 public DownloadTarget[] GetVersionFilesInfo(FullVersionId fullVersionId)
 {
     try {
         return(InternalGetVersionFilesInfo(fullVersionId));
     }
     catch (Exception e) {
         _log.Error("Error: " + e.Message);
         throw;
     }
 }
        public void UpdateInstalledFiles(string profileId, FullVersionId versionId)
        {
            var profileDir     = Path.Combine(_profilesPath, profileId);
            var filesIndexPath = Path.Combine(profileDir, FilesIndexName);
            var dataIndexPath  = Path.Combine(_versionsPath, versionId.Prefix, versionId.Version, DataIndexName);

            var dataIndex = _json.ReadFile <VersionDataIndex>(dataIndexPath);

            if (File.Exists(filesIndexPath))
            {
                var filesIndex    = _json.ReadFile <FilesIndex>(filesIndexPath);
                var obsoleteFiles = dataIndex.Files != null?
                                    filesIndex.Installed.Except(dataIndex.Files.Index.Keys) :
                                        filesIndex.Installed;

                foreach (var fileName in obsoleteFiles)
                {
                    File.Delete(Path.Combine(profileDir, fileName));
                }
            }

            if (dataIndex.Files == null)
            {
                File.Delete(filesIndexPath);
                return;
            }

            foreach (var(fileName, fileInfo) in dataIndex.Files.Index)
            {
                var src = Path.Combine(_versionsPath, versionId.Prefix, versionId.Version, "files", fileName);
                var dst = Path.Combine(profileDir, fileName);

                var isMutable = dataIndex.Files.Mutables?.Contains(fileName) ?? false;
                if (IsSameFileExists(dst, fileInfo.Size, fileInfo.Hash, isMutable))
                {
                    continue;
                }

                var baseDir = Path.GetDirectoryName(dst);
                if (!Directory.Exists(baseDir))
                {
                    Directory.CreateDirectory(baseDir);
                }

                File.Copy(src, dst, true);
            }

            _json.WriteFile(new FilesIndex {
                Installed = dataIndex.Files.Index.Keys.ToArray()
            }, filesIndexPath);
        }
Beispiel #3
0
        public async Task FetchVersionIndexes(FullVersionId fullVersionId)
        {
            _log.Info($"Fetching {fullVersionId}...");

            try {
                await InternalFetchVersionIndexes(fullVersionId);
            }
            catch (Exception e) {
                _log.Error($"Fail! Can't fetch {fullVersionId}: {e.Message}");
                throw;
            }

            _log.Info($"Success! Version {fullVersionId} is updated!");
        }
Beispiel #4
0
        private void HandleAddProfile()
        {
            if (_versions.Prefixes.Length <= 0)
            {
                _ui.ShowErrorMessage(_tr._("There are no known client versions!"));
                return;
            }

            var prefix  = _versions.Prefixes[0];
            var version = new FullVersionId(prefix.Id, prefix.LatestVersion);
            var data    = new ProfileData {
                FullVersion = version
            };

            _ui.ShowProfile(_tr._("New profile"), data, _versions.Prefixes, TryCreateProfile);
        }
Beispiel #5
0
        private async void TryBecomeOnline()
        {
            _log.Info("Trying to switch to the online mode...");

            _ui.SetInteractable(false);
            _ui.OfflineMode = !await TryFetchPrefixes();

            _ui.SetInteractable(true);

            var state = _ui.OfflineMode ? "offline" : "online";

            _log.Info($"Is {state} now!");

            if (_profiles.IsEmpty)
            {
                foreach (var prefix in _versions.Prefixes)
                {
                    var fullVersion = new FullVersionId(prefix.Id, IndexTool.VersionAliasLatest);
                    try {
                        _profiles.Create(prefix.About, new ProfileData {
                            FullVersion = fullVersion
                        });
                        if (!_profiles.Contains(_settings.Profile))
                        {
                            _settings.Profile = prefix.About;
                        }
                    }
                    catch (Exception e) {
                        _log.Error("Can't create default profile: " + e.Message);
                    }
                }

                _ui.SetProfiles(_profiles.Names, _settings.Profile);
            }
            else if (!_profiles.Contains(_settings.Profile))
            {
                _settings.Profile = _profiles.Names[0];
                _ui.SetProfiles(_profiles.Names, _settings.Profile);
            }

            if (_ui.OfflineMode)
            {
                _ui.ShowErrorMessage(_tr._("Failed to switch into online mode!"));
            }
        }
Beispiel #6
0
        private DownloadTarget[] InternalGetVersionFilesInfo(FullVersionId fullVersionId)
        {
            _log.Info($"Collecting files info for version {fullVersionId}...");
            var result  = new List <DownloadTarget>(1024);
            var prefix  = fullVersionId.Prefix;
            var version = fullVersionId.Version;

            var basePath = Path.Combine(_versionsPath, prefix, version);
            var baseUrl  = $"{_storeUrl}/{prefix}/{version}";

            _log.Info($"Add {version}.jar to list");
            var dataIndex = _json.ReadFile <VersionDataIndex>(Path.Combine(basePath, "data.json"));

            var jarPath = Path.Combine(basePath, $"{version}.jar");
            var jarUrl  = $"{baseUrl}/{version}.jar";

            result.Add(new DownloadTarget(jarPath, jarUrl, dataIndex.Main.Size, dataIndex.Main.Hash));

            if (dataIndex.Files?.Index != null)
            {
                _log.Info("Add custom files to list");
                foreach (var(relativePath, info) in dataIndex.Files.Index)
                {
                    var path = Path.Combine(basePath, "files", relativePath);
                    var url  = $"{baseUrl}/files/{relativePath}";
                    result.Add(new DownloadTarget(path, url, info.Size, info.Hash));
                }
            }

            _log.Info("Add libraries to list");
            var versionIndex = _json.ReadFile <VersionIndex>(Path.Combine(basePath, $"{version}.json"));

            AppendLibsInfo(versionIndex, dataIndex, result);

            _log.Info("Add assets to list");
            var assetsIndexRelativePath = IndexTool.GetAssetIndexPath(versionIndex);
            var assetsIndexPath         = Path.Combine(_assetsPath, "indexes", assetsIndexRelativePath);
            var assetsIndex             = _json.ReadFile <AssetsIndex>(assetsIndexPath);

            AppendAssets(assetsIndex.Objects, result);

            _log.Info($"Complete! Total files: {result.Count}");
            return(result.ToArray());
        }
Beispiel #7
0
        private async Task InternalFetchVersionIndexes(FullVersionId fullVersionId)
        {
            var basePath = Path.Combine(_versionsPath, fullVersionId.Prefix, fullVersionId.Version);
            var baseUrl  = $"{_storeUrl}/{fullVersionId.Prefix}/{fullVersionId.Version}";

            var versionIndexName = $"{fullVersionId.Version}.json";

            foreach (var fileName in new[] { versionIndexName, "data.json" })
            {
                var url  = $"{baseUrl}/{fileName}";
                var path = Path.Combine(basePath, fileName);
                await DownloadFile(path, url);
            }

            var versionIndex = _json.ReadFile <VersionIndex>(Path.Combine(basePath, versionIndexName));

            var assetsIndexRelativePath = IndexTool.GetAssetIndexPath(versionIndex);
            var assetsIndexPath         = Path.Combine(_assetsPath, "indexes", assetsIndexRelativePath);
            var assetsIndexUrl          = $"{_storeUrl}/assets/indexes/{assetsIndexRelativePath}";

            await DownloadFile(assetsIndexPath, assetsIndexUrl);
        }
 private static bool ValidateVersion(FullVersionId version)
 {
     return(version.Prefix != null && version.Version != null);
 }
Beispiel #9
0
        public bool Contains(FullVersionId id)
        {
            var prefixId = Array.FindIndex(Prefixes, p => p.Id == id.Prefix);

            return(prefixId >= 0 && Prefixes[prefixId].Versions.Any(v => v == id.Version));
        }