using System;
using System.Text;

namespace SharpQuake
{
	internal static class Cbuf
	{
		public static void Init()
		{
		}

		public static void AddText(string text)
		{
			if (string.IsNullOrEmpty(text))
			{
				return;
			}
			int length = text.Length;
			if (Cbuf._Buf.Length + length > Cbuf._Buf.Capacity)
			{
				Con.Print("Cbuf.AddText: overflow!\n", Array.Empty<object>());
				return;
			}
			Cbuf._Buf.Append(text);
		}

		public static void InsertText(string text)
		{
			Cbuf._Buf.Insert(0, text);
		}

		public static void Execute()
		{
			while (Cbuf._Buf.Length > 0)
			{
				string text = Cbuf._Buf.ToString();
				int num = 0;
				int i;
				for (i = 0; i < text.Length; i++)
				{
					if (text[i] == '"')
					{
						num++;
					}
					if (((num & 1) == 0 && text[i] == ';') || text[i] == '\n')
					{
						break;
					}
				}
				string text2 = text.Substring(0, i).TrimEnd(new char[]
				{
					'\n',
					';'
				});
				if (i == Cbuf._Buf.Length)
				{
					Cbuf._Buf.Length = 0;
				}
				else
				{
					Cbuf._Buf.Remove(0, i + 1);
				}
				if (!string.IsNullOrEmpty(text2))
				{
					cmd.ExecuteString(text2, cmd_source_t.src_command);
					if (Cbuf._Wait)
					{
						Cbuf._Wait = false;
						return;
					}
				}
			}
		}

		public static void Cmd_Wait_f()
		{
			Cbuf._Wait = true;
		}

		private static StringBuilder _Buf = new StringBuilder(8192);

		private static bool _Wait;
	}
}
