示例#1
0
        private async Task <ScriptList> LoadScriptList(IDictionary <string, Script> scripts, ConcurrentBag <SavesError> errors, string scriptListFile, bool shouldTryLoadingReferences)
        {
            var scriptRefs = new List <Script>();

            try
            {
                var scriptRefPaths = await _scriptListSerializer.GetScriptsAsync(scriptListFile);

                foreach (var scriptRefRelativePath in scriptRefPaths)
                {
                    string fullPath = GetScriptListReferenceFullPath(scriptListFile, scriptRefRelativePath);
                    if (scripts.TryGetValue(fullPath, out var scriptRef))
                    {
                        scripts.Remove(fullPath);
                        scriptRefs.Add(scriptRef);
                    }
                    else if (shouldTryLoadingReferences)
                    {
                        var script = await LoadScript(fullPath, errors);

                        scriptRefs.Add(script);
                    }
                    else
                    {
                        errors.Add(new SavesError(scriptListFile, $"Script that does not exist: '{fullPath}'", SavesErrorLevel.Warning));
                        scriptRefs = null;
                        break;
                    }
                }

                if (scriptRefs == null || scriptRefs.Count <= 0)
                {
                    return(null);
                }

                return(new ScriptList(scriptListFile, Hashing.GetHash(scriptRefPaths), scriptRefs.ToArray()));
            }
            catch (Exception exc)
            {
                errors.Add(new SavesError(scriptListFile, exc.Message, SavesErrorLevel.Error));
                return(new ScriptList(scriptListFile, null, scriptRefs.ToArray()));
            }
        }
示例#2
0
        private async Task <LocalScriptListFile> LoadScriptListAsync(IDictionary <string, LocalScriptFile> scripts, string scriptListFile, bool shouldTryLoadingReferences)
        {
            _logger.Verbose($"{scriptListFile}: Loading");
            var scriptRefs = new List <LocalScriptFile>();

            try
            {
                var scriptRefPaths = await _scriptListSerializer.GetScriptsAsync(scriptListFile);

                foreach (var scriptRefRelativePath in scriptRefPaths)
                {
                    string fullPath = GetReferenceFullPath(scriptListFile, scriptRefRelativePath);
                    if (scripts.TryGetValue(fullPath, out var scriptRef))
                    {
                        scripts.Remove(fullPath);
                        scriptRefs.Add(scriptRef);
                    }
                    else if (_ignoredPaths.Any(p => fullPath.StartsWith(p)))
                    {
                        continue;
                    }
                    else if (shouldTryLoadingReferences)
                    {
                        var script = await LoadScriptAsync(fullPath);

                        scriptRefs.Add(script);
                    }
                    else
                    {
                        var script = new LocalScriptFile(fullPath, null);
                        script.AddError($"Script that does not exist: '{fullPath}'", LocalFileErrorLevel.Error);
                        scriptRefs.Add(script);
                    }
                }

                if (scriptRefs == null || scriptRefs.Count <= 0)
                {
                    return(null);
                }

                var scriptsWithErrors = scriptRefs.Where(s => s.Status > LocalFileErrorLevel.None).ToList();
                var scriptList        = new LocalScriptListFile(scriptListFile, Hashing.GetHash(scriptRefPaths), scriptRefs.ToArray());
                if (scriptsWithErrors.Count == 1)
                {
                    scriptList.AddError($"Script {scriptsWithErrors[0].FullPath} has an issue: {scriptsWithErrors[0].Errors[0].Error}", scriptsWithErrors[0].Status);
                }
                else if (scriptsWithErrors.Count > 1)
                {
                    scriptList.AddError($"{scriptsWithErrors.Count} scripts have issues. First issue: {scriptsWithErrors[0].Errors[0].Error}", scriptsWithErrors[0].Status);
                }

                return(scriptList);
            }
            catch (Exception exc)
            {
                var scriptList = new LocalScriptListFile(scriptListFile, null, scriptRefs.ToArray());
                scriptList.AddError(exc.Message, LocalFileErrorLevel.Error);
                _logger.Error($"{scriptListFile}: {exc.Message}");
                return(scriptList);
            }
        }