using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Robust.Shared.Console;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Utility;
namespace Robust.Shared.Configuration
{
public static class CVarCommandUtil
{
///
/// Parses a string into an object of the given type.
///
/// Thrown if the string could not be parsed into the given type.
/// Thrown if the type is not supported.
public static object ParseObject(Type type, string input)
{
if (type == typeof(bool))
{
if (bool.TryParse(input, out var val))
return val;
if (Parse.TryInt32(input, out var intVal))
{
if (intVal == 0) return false;
if (intVal == 1) return true;
}
throw new FormatException($"Could not parse bool value: {input}");
}
if (type == typeof(string))
{
return input;
}
if (type == typeof(int))
{
return Parse.Int32(input);
}
if (type == typeof(float))
{
return Parse.Float(input);
}
if (type == typeof(long))
{
return long.Parse(input);
}
if (type == typeof(ushort))
{
return ushort.Parse(input);
}
throw new NotSupportedException();
}
internal static IEnumerable GetCVarCompletionOptions(IConfigurationManager cfg)
{
return cfg.GetRegisteredCVars()
.Select(c => new CompletionOption(c, GetCVarValueHint(cfg, c)));
}
private static string GetCVarValueHint(IConfigurationManager cfg, string cVar)
{
var flags = cfg.GetCVarFlags(cVar);
if ((flags & CVar.CONFIDENTIAL) != 0)
return Loc.GetString("cmd-cvar-value-hidden");
var value = cfg.GetCVar