Exemple #1
0
        internal bool GetCapturedData(ICapturedDataFilter filter, out CapturedSettingsData capturedData)
        {
            if (filter is null)
            {
                throw new ArgumentNullException(nameof(filter));
            }

            lock (_syncpoint)
            {
                _captured.EnvironmentVariables = new List <CapturedSettingsEnvironmentVariables>(3);

                // Ensure the special GCM_DEBUG environment variable is set.
                if (!_variables[EnvironmentVariableTarget.Process].ContainsKey("GCM_DEBUG"))
                {
                    _variables[EnvironmentVariableTarget.Process].Add("GCM_DEBUG", null);
                }

                foreach (var target in _variables.Keys)
                {
                    var variables = new CapturedSettingsEnvironmentVariables
                    {
                        Target = (int)target,
                        Values = new List <CapturedSettingsEnvironmentVariable>(),
                    };

                    foreach (var kvp in _variables[target])
                    {
                        if (kvp.Key is null)
                        {
                            continue;
                        }

                        if (OrdinalIgnoreCase.Equals("PATH", kvp.Key))
                        {
                            var items = kvp.Value?.Split(';');
                            var keeps = new List <string>(items.Length);

                            for (int i = 0; i < items.Length; i += 1)
                            {
                                foreach (var legal in AllowedCapturedPathValues)
                                {
                                    if (legal.IsMatch(items[i]))
                                    {
                                        keeps.Add(items[i]);
                                        break;
                                    }
                                }
                            }

                            if (keeps.Count > 0)
                            {
                                var name     = kvp.Key;
                                var variable = string.Join(";", keeps);

                                var entry = new CapturedSettingsEnvironmentVariable
                                {
                                    Name     = name,
                                    Variable = variable,
                                };

                                variables.Values.Add(entry);
                            }
                        }
                        else if (AllowedCapturedVariables.TryGetValue(kvp.Key, out string variable))
                        {
                            var name = kvp.Key;

                            var entry = new CapturedSettingsEnvironmentVariable
                            {
                                Name     = name,
                                Variable = variable,
                            };

                            variables.Values.Add(entry);
                        }
                        else
                        {
                            var name = kvp.Key;
                            variable = kvp.Value;

                            foreach (var allowed in AllowedCapturedVariableNames)
                            {
                                if (allowed.IsMatch(name))
                                {
                                    variable = filter.ApplyFilter(variable);

                                    var entry = new CapturedSettingsEnvironmentVariable
                                    {
                                        Name     = name,
                                        Variable = variable,
                                    };

                                    variables.Values.Add(entry);

                                    break;
                                }
                            }
                        }
                    }

                    _captured.EnvironmentVariables.Add(variables);
                }

                _captured.ExpandVariables = new List <CapturedSettingsExpandVariable>();

                foreach (var kvp in _expandedVariables)
                {
                    if (kvp.Key is null)
                    {
                        continue;
                    }

                    var expanded = kvp.Value;
                    var original = kvp.Key;

                    expanded = filter.ApplyFilter(expanded);
                    original = filter.ApplyFilter(original);

                    var query = new CapturedSettingsExpandVariable
                    {
                        Expanded = expanded,
                        Original = original,
                    };

                    _captured.ExpandVariables.Add(query);
                }

                _captured.SpecialFolders = new List <CapturedSettingsSpecialFolder>();

                foreach (var kvp in _specialFolders)
                {
                    if (kvp.Value is null)
                    {
                        continue;
                    }

                    var path = kvp.Value;

                    path = filter.ApplyFilter(path);

                    var folder = new CapturedSettingsSpecialFolder
                    {
                        SpecialFolder = (int)kvp.Key,
                        Path          = path,
                    };

                    _captured.SpecialFolders.Add(folder);
                }

                _captured.CurrentDirectory = filter.ApplyFilter(_captured.CurrentDirectory);

                capturedData = _captured;
                return(true);
            }
        }
Exemple #2
0
        public static bool TryDeserialize(object serializedData, out CapturedSettingsData settingsData)
        {
            if (serializedData is JObject jSettingsData)
            {
                settingsData = jSettingsData.ToObject <CapturedSettingsData>();

#if Deserialize_Manually
                if (jsettings["EnvironmentVariables"] is JArray jEnvironmentVariables)
                {
                    settingsData.EnvironmentVariables = new List <CapturedSettingsEnvironmentVariables>();

                    foreach (var token in jEnvironmentVariables)
                    {
                        var item = new CapturedSettingsEnvironmentVariables
                        {
                            Target = token["Target"].Value <int>(),
                            Values = new List <CapturedSettingsEnvironmentVariable>(),
                        };

                        if (token["Values"] is JArray jValues)
                        {
                            foreach (var jitem in jValues)
                            {
                                var envvar = new CapturedSettingsEnvironmentVariable
                                {
                                    Name     = jitem["Name"].Value <string>(),
                                    Variable = jitem["Variable"].Value <string>(),
                                };

                                item.Values.Add(envvar);
                            }
                        }
                    }
                }

                if (jsettings["CommandLineArgs"] is JArray jCommandLineArgs)
                {
                    settingsData.CommandLineArgs = jCommandLineArgs.ToObject <string[]>();
                }

                if (jsettings["SpecialFolders"] is JArray jSpecialFolders)
                {
                    settingsData.SpecialFolders = new List <CapturedSettingsSpecialFolder>();

                    foreach (var jSpecialFolder in jSpecialFolders)
                    {
                        var specialFolder = new CapturedSettingsSpecialFolder
                        {
                            Path          = jSpecialFolder["Path"].Value <string>(),
                            SpecialFolder = jSpecialFolder["SpecialFolder"].Value <int>(),
                        };

                        settingsData.SpecialFolders.Add(specialFolder);
                    }
                }
#endif

                return(true);
            }

            settingsData = default(CapturedSettingsData);
            return(false);
        }