75 lines
1.7 KiB
C#
75 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Cabrito
|
|
{
|
|
[Flags]
|
|
public enum ConsoleFlags
|
|
{
|
|
NoSerialize = 1, // Value does not persist
|
|
|
|
Alias = NoSerialize,
|
|
}
|
|
|
|
internal struct ConsoleVariable
|
|
{
|
|
public string name { get; private set; }
|
|
public ConsoleFlags flags { get; private set; }
|
|
|
|
private FieldInfo field;
|
|
private MethodInfo getter;
|
|
private MethodInfo setter;
|
|
|
|
public ConsoleVariable(string name, ConsoleFlags flags, FieldInfo field)
|
|
{
|
|
this.name = name;
|
|
this.flags = flags;
|
|
this.field = field;
|
|
this.getter = null;
|
|
this.setter = null;
|
|
}
|
|
|
|
public ConsoleVariable(string name, ConsoleFlags flags, MethodInfo getter, MethodInfo setter)
|
|
{
|
|
this.name = name;
|
|
this.flags = flags;
|
|
this.field = null;
|
|
this.getter = getter;
|
|
this.setter = setter;
|
|
}
|
|
|
|
public string GetValueString()
|
|
{
|
|
var type = field != null ? field.FieldType : getter.ReturnType;
|
|
if (type == typeof(string))
|
|
{
|
|
if (field != null)
|
|
return (string) field.GetValue(null);
|
|
else if (setter != null)
|
|
return (string) getter.Invoke(null, null);
|
|
}
|
|
else
|
|
throw new Exception("cvar is not type of string");
|
|
|
|
throw new Exception("GetValueString no field or getter specified");
|
|
}
|
|
|
|
public void SetValue(string value)
|
|
{
|
|
var type = field != null ? field.FieldType : getter.ReturnType;
|
|
if (type == typeof(string))
|
|
{
|
|
if (field != null)
|
|
field.SetValue(null, value);
|
|
else if (setter != null)
|
|
setter.Invoke(null, new object[] {value});
|
|
}
|
|
else
|
|
throw new Exception("Unsupported type for SetValue: " + type.Name);
|
|
}
|
|
}
|
|
} |