Files
2026-03-04 09:37:33 +08:00

133 lines
2.5 KiB
C#

using System;
using UnityEngine;
namespace DebuggingEssentials
{
public static class Parser
{
public static bool TryParse(Type t, FastQueue<string> paramQueue, out object result)
{
bool flag = true;
if (t == typeof(Vector2))
{
Vector2 vector = default(Vector2);
for (int i = 0; i < 2; i++)
{
flag = ChangeType(typeof(float), paramQueue, out result);
if (flag)
{
vector[i] = (float)result;
continue;
}
return false;
}
result = vector;
}
else if (t == typeof(Vector3))
{
Vector3 vector2 = default(Vector3);
for (int j = 0; j < 3; j++)
{
flag = ChangeType(typeof(float), paramQueue, out result);
if (flag)
{
vector2[j] = (float)result;
continue;
}
return false;
}
result = vector2;
}
else if (t == typeof(Vector4))
{
Vector4 vector3 = default(Vector4);
for (int k = 0; k < 4; k++)
{
flag = ChangeType(typeof(float), paramQueue, out result);
if (flag)
{
vector3[k] = (float)result;
continue;
}
return false;
}
result = vector3;
}
else if (t == typeof(Quaternion))
{
Quaternion quaternion = default(Quaternion);
for (int l = 0; l < 4; l++)
{
flag = ChangeType(typeof(float), paramQueue, out result);
if (flag)
{
quaternion[l] = (float)result;
continue;
}
return false;
}
result = quaternion;
}
else
{
flag = ChangeType(t, paramQueue, out result);
}
return flag;
}
private static bool ChangeType(Type t, FastQueue<string> paramQueue, out object result)
{
if (paramQueue.Count == 0)
{
RuntimeConsole.LogResultError("Not enough parameters");
result = null;
return false;
}
string value = paramQueue.Dequeue();
return ChangeType(t, value, out result);
}
public static bool ChangeType(Type t, string value, out object result, bool logError = true)
{
value = value.Trim();
if (t == typeof(string))
{
result = value;
return true;
}
if (t.IsEnum)
{
try
{
result = Enum.Parse(t, value, ignoreCase: true);
return true;
}
catch (Exception)
{
if (logError)
{
RuntimeConsole.LogResultError("Cannot find '" + value + "'");
}
}
}
else
{
try
{
result = Convert.ChangeType(value, t);
return true;
}
catch (Exception)
{
if (logError)
{
RuntimeConsole.LogResultError("Cannot parse " + value);
}
}
}
result = null;
return false;
}
}
}