using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;

namespace SharpQuake
{
	internal static class host
	{
		public static quakeparms_t Params
		{
			get
			{
				return host._Params;
			}
		}

		public static bool IsDedicated
		{
			get
			{
				return false;
			}
		}

		public static bool IsInitialized
		{
			get
			{
				return host._IsInitialized;
			}
		}

		public static double Time
		{
			get
			{
				return host._Time;
			}
		}

		public static int FrameCount
		{
			get
			{
				return host._FrameCount;
			}
		}

		public static bool IsDeveloper
		{
			get
			{
				return host._Developer != null && host._Developer.Value != 0f;
			}
		}

		public static byte[] ColorMap
		{
			get
			{
				return host._ColorMap;
			}
		}

		public static float TeamPlay
		{
			get
			{
				return host._Teamplay.Value;
			}
		}

		public static byte[] BasePal
		{
			get
			{
				return host._BasePal;
			}
		}

		public static bool IsCoop
		{
			get
			{
				return host._Coop.Value != 0f;
			}
		}

		public static float Skill
		{
			get
			{
				return host._Skill.Value;
			}
		}

		public static float Deathmatch
		{
			get
			{
				return host._Deathmatch.Value;
			}
		}

		public static int ClientNum
		{
			get
			{
				return Array.IndexOf<client_t>(server.svs.clients, host.HostClient);
			}
		}

		public static float FragLimit
		{
			get
			{
				return host._FragLimit.Value;
			}
		}

		public static float TimeLimit
		{
			get
			{
				return host._TimeLimit.Value;
			}
		}

		public static BinaryReader VcrReader
		{
			get
			{
				return host._VcrReader;
			}
		}

		public static BinaryWriter VcrWriter
		{
			get
			{
				return host._VcrWriter;
			}
		}

		public static void ClearMemory()
		{
			Con.DPrint("Clearing memory\n", Array.Empty<object>());
			Mod.ClearAll();
			client.cls.signon = 0;
			server.sv.Clear();
			client.cl.Clear();
		}

		public static void ServerFrame()
		{
			progs.GlobalStruct.frametime = (float)host.FrameTime;
			server.ClearDatagram();
			server.CheckForNewClients();
			server.RunClients();
			if (!server.sv.paused && (server.svs.maxclients > 1 || Key.Destination == keydest_t.key_game))
			{
				server.Physics();
			}
			server.SendClientMessages();
		}

		public static void Init(quakeparms_t parms)
		{
			host._Params = parms;
			Cache.Init(536870912);
			Cbuf.Init();
			cmd.Init();
			view.Init();
			chase.Init();
			host.InitVCR(parms);
			common.Init(parms.basedir, parms.argv);
			host.InitLocal();
			wad.LoadWadFile("gfx.wad");
			Key.Init();
			Con.Init();
			menu.Init();
			progs.Init();
			Mod.Init();
			net.Init();
			server.Init();
			render.InitTextures();
			if (client.cls.state != cactive_t.ca_dedicated)
			{
				host._BasePal = common.LoadFile("gfx/palette.lmp");
				if (host._BasePal == null)
				{
					sys.Error("Couldn't load gfx/palette.lmp", Array.Empty<object>());
				}
				host._ColorMap = common.LoadFile("gfx/colormap.lmp");
				if (host._ColorMap == null)
				{
					sys.Error("Couldn't load gfx/colormap.lmp", Array.Empty<object>());
				}
				input.Init();
				vid.Init(host._BasePal);
				Drawer.Init();
				Scr.Init();
				render.Init();
				snd.Init();
				cd_audio.Init();
				sbar.Init();
				client.Init();
			}
			Cbuf.InsertText("exec quake.rc\n");
			host._IsInitialized = true;
			Con.DPrint("========Quake Initialized=========\n", Array.Empty<object>());
		}

		public static void Shutdown()
		{
			host._ShutdownDepth++;
			try
			{
				if (host._ShutdownDepth <= 1)
				{
					Scr.IsDisabledForLoading = true;
					host.WriteConfiguration();
					cd_audio.Shutdown();
					net.Shutdown();
					snd.Shutdown();
					input.Shutdown();
					if (host._VcrWriter != null)
					{
						Con.Print("Closing vcrfile.\n", Array.Empty<object>());
						host._VcrWriter.Close();
						host._VcrWriter = null;
					}
					if (host._VcrReader != null)
					{
						Con.Print("Closing vcrfile.\n", Array.Empty<object>());
						host._VcrReader.Close();
						host._VcrReader = null;
					}
					if (client.cls.state != cactive_t.ca_dedicated)
					{
						vid.Shutdown();
					}
					Con.Shutdown();
				}
			}
			finally
			{
				host._ShutdownDepth--;
			}
		}

		public static void Error(string error, params object[] args)
		{
			host._ErrorDepth++;
			try
			{
				if (host._ErrorDepth > 1)
				{
					sys.Error("Host_Error: recursively entered. " + error, args);
				}
				Scr.EndLoadingPlaque();
				string text = (args.Length != 0) ? string.Format(error, args) : error;
				Con.Print("Host_Error: {0}\n", new object[]
				{
					text
				});
				if (server.sv.active)
				{
					host.ShutdownServer(false);
				}
				if (client.cls.state == cactive_t.ca_dedicated)
				{
					sys.Error("Host_Error: {0}\n", new object[]
					{
						text
					});
				}
				client.Disconnect();
				client.cls.demonum = -1;
				throw new EndGameException();
			}
			finally
			{
				host._ErrorDepth--;
			}
		}

		public static void EndGame(string message, params object[] args)
		{
			string text = string.Format(message, args);
			Con.DPrint("Host_EndGame: {0}\n", new object[]
			{
				text
			});
			if (server.IsActive)
			{
				host.ShutdownServer(false);
			}
			if (client.cls.state == cactive_t.ca_dedicated)
			{
				sys.Error("Host_EndGame: {0}\n", new object[]
				{
					text
				});
			}
			if (client.cls.demonum != -1)
			{
				client.NextDemo();
			}
			else
			{
				client.Disconnect();
			}
			throw new EndGameException();
		}

		public static void Frame(double time)
		{
			if (host._ServerProfile.Value == 0f)
			{
				host.InternalFrame(time);
				return;
			}
			double floatTime = sys.GetFloatTime();
			host.InternalFrame(time);
			double floatTime2 = sys.GetFloatTime();
			host._TimeTotal += floatTime2 - floatTime;
			host._TimeCount++;
			if (host._TimeCount < 1000)
			{
				return;
			}
			int num = (int)(host._TimeTotal * 1000.0 / (double)host._TimeCount);
			host._TimeCount = 0;
			host._TimeTotal = 0.0;
			int num2 = 0;
			client_t[] clients = server.svs.clients;
			for (int i = 0; i < clients.Length; i++)
			{
				if (clients[i].active)
				{
					num2++;
				}
			}
			Con.Print("serverprofile: {0,2:d} clients {1,2:d} msec\n", new object[]
			{
				num2,
				num
			});
		}

		public static void ClientCommands(string fmt, params object[] args)
		{
			string s = string.Format(fmt, args);
			host.HostClient.message.WriteByte(9);
			host.HostClient.message.WriteString(s);
		}

		public static void ShutdownServer(bool crash)
		{
			if (!server.IsActive)
			{
				return;
			}
			server.sv.active = false;
			if (client.cls.state == cactive_t.ca_connected)
			{
				client.Disconnect();
			}
			double floatTime = sys.GetFloatTime();
			int num;
			do
			{
				num = 0;
				for (int i = 0; i < server.svs.maxclients; i++)
				{
					host.HostClient = server.svs.clients[i];
					if (host.HostClient.active && !host.HostClient.message.IsEmpty)
					{
						if (net.CanSendMessage(host.HostClient.netconnection))
						{
							net.SendMessage(host.HostClient.netconnection, host.HostClient.message);
							host.HostClient.message.Clear();
						}
						else
						{
							net.GetMessage(host.HostClient.netconnection);
							num++;
						}
					}
				}
			}
			while (sys.GetFloatTime() - floatTime <= 3.0 && num > 0);
			MsgWriter msgWriter = new MsgWriter(4);
			msgWriter.WriteByte(2);
			num = net.SendToAll(msgWriter, 5);
			if (num != 0)
			{
				Con.Print("Host_ShutdownServer: NET_SendToAll failed for {0} clients\n", new object[]
				{
					num
				});
			}
			for (int j = 0; j < server.svs.maxclients; j++)
			{
				host.HostClient = server.svs.clients[j];
				if (host.HostClient.active)
				{
					server.DropClient(crash);
				}
			}
			server.sv.Clear();
			for (int k = 0; k < server.svs.clients.Length; k++)
			{
				server.svs.clients[k].Clear();
			}
		}

		private static void WriteConfiguration()
		{
			if (host._IsInitialized & !host.IsDedicated)
			{
				using (FileStream fileStream = sys.FileOpenWrite(Path.Combine(common.GameDir, "config.cfg"), true))
				{
					if (fileStream != null)
					{
						Key.WriteBindings(fileStream);
						cvar.WriteVariables(fileStream);
					}
				}
			}
		}

		private static void InitLocal()
		{
			host.InitCommands();
			if (host._Sys_TickRate == null)
			{
				host._Sys_TickRate = new cvar("sys_ticrate", "0.05");
				host._Developer = new cvar("developer", "0");
				host._FrameRate = new cvar("host_framerate", "0");
				host._Speeds = new cvar("host_speeds", "0");
				host._ServerProfile = new cvar("serverprofile", "0");
				host._FragLimit = new cvar("fraglimit", "0", false, true);
				host._TimeLimit = new cvar("timelimit", "0", false, true);
				host._Teamplay = new cvar("teamplay", "0", false, true);
				host._SameLevel = new cvar("samelevel", "0");
				host._NoExit = new cvar("noexit", "0", false, true);
				host._Skill = new cvar("skill", "1");
				host._Deathmatch = new cvar("deathmatch", "0");
				host._Coop = new cvar("coop", "0");
				host._Pausable = new cvar("pausable", "1");
				host._Temp1 = new cvar("temp1", "0");
			}
			host.FindMaxClients();
			host._Time = 1.0;
		}

		private static void FindMaxClients()
		{
			server_static_t svs = server.svs;
			client_static_t cls = client.cls;
			svs.maxclients = 1;
			int i = common.CheckParm("-dedicated");
			if (i > 0)
			{
				cls.state = cactive_t.ca_dedicated;
				if (i != common.Argc - 1)
				{
					svs.maxclients = common.atoi(common.Argv(i + 1));
				}
				else
				{
					svs.maxclients = 8;
				}
			}
			else
			{
				cls.state = cactive_t.ca_disconnected;
			}
			i = common.CheckParm("-listen");
			if (i > 0)
			{
				if (cls.state == cactive_t.ca_dedicated)
				{
					sys.Error("Only one of -dedicated or -listen can be specified", Array.Empty<object>());
				}
				if (i != common.Argc - 1)
				{
					svs.maxclients = common.atoi(common.Argv(i + 1));
				}
				else
				{
					svs.maxclients = 8;
				}
			}
			if (svs.maxclients < 1)
			{
				svs.maxclients = 8;
			}
			else if (svs.maxclients > 16)
			{
				svs.maxclients = 16;
			}
			svs.maxclientslimit = svs.maxclients;
			if (svs.maxclientslimit < 4)
			{
				svs.maxclientslimit = 4;
			}
			svs.clients = new client_t[svs.maxclientslimit];
			for (i = 0; i < svs.clients.Length; i++)
			{
				svs.clients[i] = new client_t();
			}
			if (svs.maxclients > 1)
			{
				cvar.Set("deathmatch", 1f);
				return;
			}
			cvar.Set("deathmatch", 0f);
		}

		private static void InitVCR(quakeparms_t parms)
		{
			if (common.HasParam("-playback"))
			{
				if (common.Argc != 2)
				{
					sys.Error("No other parameters allowed with -playback\n", Array.Empty<object>());
				}
				FileStream fileStream = sys.FileOpenRead("quake.vcr");
				if (fileStream == null)
				{
					sys.Error("playback file not found\n", Array.Empty<object>());
				}
				host._VcrReader = new BinaryReader(fileStream, Encoding.ASCII);
				if (host._VcrReader.ReadInt32() != 1447252529)
				{
					sys.Error("Invalid signature in vcr file\n", Array.Empty<object>());
				}
				string[] array = new string[host._VcrReader.ReadInt32() + 1];
				array[0] = parms.argv[0];
				for (int i = 1; i < array.Length; i++)
				{
					array[i] = sys.ReadString(host._VcrReader);
				}
				common.Args = array;
				parms.argv = array;
			}
			int num = common.CheckParm("-record");
			if (num != 0)
			{
				host._VcrWriter = new BinaryWriter(sys.FileOpenWrite("quake.vcr", false), Encoding.ASCII);
				host._VcrWriter.Write(1447252529);
				host._VcrWriter.Write(common.Argc - 1);
				for (int j = 1; j < common.Argc; j++)
				{
					if (j == num)
					{
						sys.WriteString(host._VcrWriter, "-playback");
					}
					else
					{
						sys.WriteString(host._VcrWriter, common.Argv(j));
					}
				}
			}
		}

		private static void InternalFrame(double time)
		{
			sys.Random();
			if (!host.FilterTime(time))
			{
				return;
			}
			sys.SendKeyEvents();
			input.Commands();
			Cbuf.Execute();
			net.Poll();
			if (server.sv.active)
			{
				client.SendCmd();
			}
			host.GetConsoleCommands();
			if (server.sv.active)
			{
				host.ServerFrame();
			}
			if (!server.sv.active)
			{
				client.SendCmd();
			}
			host._Time += host.FrameTime;
			if (client.cls.state == cactive_t.ca_connected)
			{
				client.ReadFromServer();
			}
			if (host._Speeds.Value != 0f)
			{
				host._Time1 = sys.GetFloatTime();
			}
			Scr.UpdateScreen();
			if (host._Speeds.Value != 0f)
			{
				host._Time2 = sys.GetFloatTime();
			}
			if (client.cls.signon == 4)
			{
				snd.Update(ref render.Origin, ref render.ViewPn, ref render.ViewRight, ref render.ViewUp);
				client.DecayLights();
			}
			else
			{
				snd.Update(ref common.ZeroVector, ref common.ZeroVector, ref common.ZeroVector, ref common.ZeroVector);
			}
			cd_audio.Update();
			if (host._Speeds.Value != 0f)
			{
				int num = (int)((host._Time1 - host._Time3) * 1000.0);
				host._Time3 = sys.GetFloatTime();
				int num2 = (int)((host._Time2 - host._Time1) * 1000.0);
				int num3 = (int)((host._Time3 - host._Time2) * 1000.0);
				Con.Print("{0,3} tot {1,3} server {2,3} gfx {3,3} snd\n", new object[]
				{
					num + num2 + num3,
					num,
					num2,
					num3
				});
			}
			host._FrameCount++;
		}

		private static bool FilterTime(double time)
		{
			host.RealTime += time;
			if (!client.cls.timedemo && host.RealTime - host._OldRealTime < 0.013888888888888888)
			{
				return false;
			}
			host.FrameTime = host.RealTime - host._OldRealTime;
			host._OldRealTime = host.RealTime;
			if (host._FrameRate.Value > 0f)
			{
				host.FrameTime = (double)host._FrameRate.Value;
			}
			else
			{
				if (host.FrameTime > 0.1)
				{
					host.FrameTime = 0.1;
				}
				if (host.FrameTime < 0.001)
				{
					host.FrameTime = 0.001;
				}
			}
			return true;
		}

		private static void GetConsoleCommands()
		{
			for (;;)
			{
				string text = sys.ConsoleInput();
				if (string.IsNullOrEmpty(text))
				{
					break;
				}
				Cbuf.AddText(text);
			}
		}

		public static void Quit_f()
		{
			if (Key.Destination != keydest_t.key_console && client.cls.state != cactive_t.ca_dedicated)
			{
				MenuBase.QuitMenu.Show();
				return;
			}
			client.Disconnect();
			host.ShutdownServer(false);
			sys.Quit();
		}

		private static void InitCommands()
		{
			cmd.Add("status", new xcommand_t(host.Status_f));
			cmd.Add("quit", new xcommand_t(host.Quit_f));
			cmd.Add("god", new xcommand_t(host.God_f));
			cmd.Add("notarget", new xcommand_t(host.Notarget_f));
			cmd.Add("fly", new xcommand_t(host.Fly_f));
			cmd.Add("map", new xcommand_t(host.Map_f));
			cmd.Add("restart", new xcommand_t(host.Restart_f));
			cmd.Add("changelevel", new xcommand_t(host.Changelevel_f));
			cmd.Add("connect", new xcommand_t(host.Connect_f));
			cmd.Add("reconnect", new xcommand_t(host.Reconnect_f));
			cmd.Add("name", new xcommand_t(host.Name_f));
			cmd.Add("noclip", new xcommand_t(host.Noclip_f));
			cmd.Add("version", new xcommand_t(host.Version_f));
			cmd.Add("say", new xcommand_t(host.Say_f));
			cmd.Add("say_team", new xcommand_t(host.Say_Team_f));
			cmd.Add("tell", new xcommand_t(host.Tell_f));
			cmd.Add("color", new xcommand_t(host.Color_f));
			cmd.Add("kill", new xcommand_t(host.Kill_f));
			cmd.Add("pause", new xcommand_t(host.Pause_f));
			cmd.Add("spawn", new xcommand_t(host.Spawn_f));
			cmd.Add("begin", new xcommand_t(host.Begin_f));
			cmd.Add("prespawn", new xcommand_t(host.PreSpawn_f));
			cmd.Add("kick", new xcommand_t(host.Kick_f));
			cmd.Add("ping", new xcommand_t(host.Ping_f));
			cmd.Add("load", new xcommand_t(host.Loadgame_f));
			cmd.Add("save", new xcommand_t(host.Savegame_f));
			cmd.Add("give", new xcommand_t(host.Give_f));
			cmd.Add("startdemos", new xcommand_t(host.Startdemos_f));
			cmd.Add("demos", new xcommand_t(host.Demos_f));
			cmd.Add("stopdemo", new xcommand_t(host.Stopdemo_f));
			cmd.Add("viewmodel", new xcommand_t(host.Viewmodel_f));
			cmd.Add("viewframe", new xcommand_t(host.Viewframe_f));
			cmd.Add("viewnext", new xcommand_t(host.Viewnext_f));
			cmd.Add("viewprev", new xcommand_t(host.Viewprev_f));
			cmd.Add("mcache", new xcommand_t(Mod.Print));
		}

		private static void Status_f()
		{
			bool flag = true;
			if (cmd.Source == cmd_source_t.src_command)
			{
				if (!server.sv.active)
				{
					cmd.ForwardToServer();
					return;
				}
			}
			else
			{
				flag = false;
			}
			StringBuilder stringBuilder = new StringBuilder(256);
			stringBuilder.Append(string.Format("host:    {0}\n", cvar.GetString("hostname")));
			stringBuilder.Append(string.Format("version: {0:F2}\n", 1.09f));
			if (net.TcpIpAvailable)
			{
				stringBuilder.Append("tcp/ip:  ");
				stringBuilder.Append(net.MyTcpIpAddress);
				stringBuilder.Append('\n');
			}
			stringBuilder.Append("map:     ");
			stringBuilder.Append(server.sv.name);
			stringBuilder.Append('\n');
			stringBuilder.Append(string.Format("players: {0} active ({1} max)\n\n", net.ActiveConnections, server.svs.maxclients));
			for (int i = 0; i < server.svs.maxclients; i++)
			{
				client_t client_t = server.svs.clients[i];
				if (client_t.active)
				{
					int num = (int)(net.Time - client_t.netconnection.connecttime);
					int num2 = num / 60;
					int num3;
					if (num2 > 0)
					{
						num -= num2 * 60;
						num3 = num2 / 60;
						if (num3 > 0)
						{
							num2 -= num3 * 60;
						}
					}
					else
					{
						num3 = 0;
					}
					stringBuilder.Append(string.Format("#{0,-2} {1,-16}  {2}  {2}:{4,2}:{5,2}", new object[]
					{
						i + 1,
						client_t.name,
						(int)client_t.edict.v.frags,
						num3,
						num2,
						num
					}));
					stringBuilder.Append("   ");
					stringBuilder.Append(client_t.netconnection.address);
					stringBuilder.Append('\n');
				}
			}
			if (flag)
			{
				Con.Print(stringBuilder.ToString(), Array.Empty<object>());
				return;
			}
			server.ClientPrint(stringBuilder.ToString(), Array.Empty<object>());
		}

		private static void God_f()
		{
			if (cmd.Source == cmd_source_t.src_command)
			{
				cmd.ForwardToServer();
				return;
			}
			if (progs.GlobalStruct.deathmatch != 0f && !host.HostClient.privileged)
			{
				return;
			}
			server.Player.v.flags = (float)((int)server.Player.v.flags ^ 64);
			if (((int)server.Player.v.flags & 64) == 0)
			{
				server.ClientPrint("godmode OFF\n", Array.Empty<object>());
				return;
			}
			server.ClientPrint("godmode ON\n", Array.Empty<object>());
		}

		private static void Notarget_f()
		{
			if (cmd.Source == cmd_source_t.src_command)
			{
				cmd.ForwardToServer();
				return;
			}
			if (progs.GlobalStruct.deathmatch != 0f && !host.HostClient.privileged)
			{
				return;
			}
			server.Player.v.flags = (float)((int)server.Player.v.flags ^ 128);
			if (((int)server.Player.v.flags & 128) == 0)
			{
				server.ClientPrint("notarget OFF\n", Array.Empty<object>());
				return;
			}
			server.ClientPrint("notarget ON\n", Array.Empty<object>());
		}

		private static void Noclip_f()
		{
			if (cmd.Source == cmd_source_t.src_command)
			{
				cmd.ForwardToServer();
				return;
			}
			if (progs.GlobalStruct.deathmatch > 0f && !host.HostClient.privileged)
			{
				return;
			}
			if (server.Player.v.movetype != 8f)
			{
				host.NoClipAngleHack = true;
				server.Player.v.movetype = 8f;
				server.ClientPrint("noclip ON\n", Array.Empty<object>());
				return;
			}
			host.NoClipAngleHack = false;
			server.Player.v.movetype = 3f;
			server.ClientPrint("noclip OFF\n", Array.Empty<object>());
		}

		private static void Fly_f()
		{
			if (cmd.Source == cmd_source_t.src_command)
			{
				cmd.ForwardToServer();
				return;
			}
			if (progs.GlobalStruct.deathmatch > 0f && !host.HostClient.privileged)
			{
				return;
			}
			if (server.Player.v.movetype != 5f)
			{
				server.Player.v.movetype = 5f;
				server.ClientPrint("flymode ON\n", Array.Empty<object>());
				return;
			}
			server.Player.v.movetype = 3f;
			server.ClientPrint("flymode OFF\n", Array.Empty<object>());
		}

		private static void Ping_f()
		{
			if (cmd.Source == cmd_source_t.src_command)
			{
				cmd.ForwardToServer();
				return;
			}
			server.ClientPrint("Client ping times:\n", Array.Empty<object>());
			for (int i = 0; i < server.svs.maxclients; i++)
			{
				client_t client_t = server.svs.clients[i];
				if (client_t.active)
				{
					float num = 0f;
					for (int j = 0; j < 16; j++)
					{
						num += client_t.ping_times[j];
					}
					num /= 16f;
					server.ClientPrint("{0,4} {1}\n", new object[]
					{
						(int)(num * 1000f),
						client_t.name
					});
				}
			}
		}

		private static void Map_f()
		{
			if (cmd.Source != cmd_source_t.src_command)
			{
				return;
			}
			client.cls.demonum = -1;
			client.Disconnect();
			host.ShutdownServer(false);
			Key.Destination = keydest_t.key_game;
			Scr.BeginLoadingPlaque();
			client.cls.mapstring = cmd.JoinArgv() + "\n";
			server.svs.serverflags = 0;
			server.SpawnServer(cmd.Argv(1));
			if (!server.IsActive)
			{
				return;
			}
			if (client.cls.state != cactive_t.ca_dedicated)
			{
				client.cls.spawnparms = cmd.JoinArgv();
				cmd.ExecuteString("connect local", cmd_source_t.src_command);
			}
		}

		private static void Changelevel_f()
		{
			if (cmd.Argc != 2)
			{
				Con.Print("changelevel <levelname> : continue game on a new level\n", Array.Empty<object>());
				return;
			}
			if (!server.sv.active || client.cls.demoplayback)
			{
				Con.Print("Only the server may changelevel\n", Array.Empty<object>());
				return;
			}
			server.SaveSpawnparms();
			server.SpawnServer(cmd.Argv(1));
		}

		private static void Restart_f()
		{
			if (client.cls.demoplayback || !server.IsActive)
			{
				return;
			}
			if (cmd.Source != cmd_source_t.src_command)
			{
				return;
			}
			server.SpawnServer(server.sv.name);
		}

		private static void Reconnect_f()
		{
			Scr.BeginLoadingPlaque();
			client.cls.signon = 0;
		}

		private static void Connect_f()
		{
			client.cls.demonum = -1;
			if (client.cls.demoplayback)
			{
				client.StopPlayback();
				client.Disconnect();
			}
			client.EstablishConnection(cmd.Argv(1));
			host.Reconnect_f();
		}

		private static string SavegameComment()
		{
			string text = string.Format("{0} kills:{1,3}/{2,3}", client.cl.levelname, client.cl.stats[QStats.STAT_MONSTERS], client.cl.stats[QStats.STAT_TOTALMONSTERS]);
			text = text.Replace(' ', '_');
			if (text.Length < 38)
			{
				text = text.PadRight(38, '_');
			}
			if (text.Length > 38)
			{
				text = text.Remove(37);
			}
			return text + "\0";
		}

		private static void Savegame_f()
		{
			if (cmd.Source != cmd_source_t.src_command)
			{
				return;
			}
			if (!server.sv.active)
			{
				Con.Print("Not playing a local game.\n", Array.Empty<object>());
				return;
			}
			if (client.cl.intermission != 0)
			{
				Con.Print("Can't save in intermission.\n", Array.Empty<object>());
				return;
			}
			if (server.svs.maxclients != 1)
			{
				Con.Print("Can't save multiplayer games.\n", Array.Empty<object>());
				return;
			}
			if (cmd.Argc != 2)
			{
				Con.Print("save <savename> : save a game\n", Array.Empty<object>());
				return;
			}
			if (cmd.Argv(1).Contains(".."))
			{
				Con.Print("Relative pathnames are not allowed.\n", Array.Empty<object>());
				return;
			}
			for (int i = 0; i < server.svs.maxclients; i++)
			{
				if (server.svs.clients[i].active && server.svs.clients[i].edict.v.health <= 0f)
				{
					Con.Print("Can't savegame with a dead player\n", Array.Empty<object>());
					return;
				}
			}
			string text = Path.ChangeExtension(Path.Combine(common.GameDir, cmd.Argv(1)), ".sav");
			Con.Print("Saving game to {0}...\n", new object[]
			{
				text
			});
			FileStream fileStream = sys.FileOpenWrite(text, true);
			if (fileStream == null)
			{
				Con.Print("ERROR: couldn't open.\n", Array.Empty<object>());
				return;
			}
			using (StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.ASCII))
			{
				streamWriter.WriteLine(5);
				streamWriter.WriteLine(host.SavegameComment());
				for (int j = 0; j < 16; j++)
				{
					streamWriter.WriteLine(server.svs.clients[0].spawn_parms[j].ToString("F6", CultureInfo.InvariantCulture.NumberFormat));
				}
				streamWriter.WriteLine(host.CurrentSkill);
				streamWriter.WriteLine(server.sv.name);
				streamWriter.WriteLine(server.sv.time.ToString("F6", CultureInfo.InvariantCulture.NumberFormat));
				for (int k = 0; k < 64; k++)
				{
					if (!string.IsNullOrEmpty(server.sv.lightstyles[k]))
					{
						streamWriter.WriteLine(server.sv.lightstyles[k]);
					}
					else
					{
						streamWriter.WriteLine("m");
					}
				}
				progs.WriteGlobals(streamWriter);
				for (int l = 0; l < server.sv.num_edicts; l++)
				{
					progs.WriteEdict(streamWriter, server.EdictNum(l));
					streamWriter.Flush();
				}
			}
			Con.Print("done.\n", Array.Empty<object>());
		}

		private static void Loadgame_f()
		{
			if (cmd.Source != cmd_source_t.src_command)
			{
				return;
			}
			if (cmd.Argc != 2)
			{
				Con.Print("load <savename> : load a game\n", Array.Empty<object>());
				return;
			}
			client.cls.demonum = -1;
			string text = Path.ChangeExtension(Path.Combine(common.GameDir, cmd.Argv(1)), ".sav");
			Con.Print("Loading game from {0}...\n", new object[]
			{
				text
			});
			FileStream fileStream = sys.FileOpenRead(text);
			if (fileStream == null)
			{
				Con.Print("ERROR: couldn't open.\n", Array.Empty<object>());
				return;
			}
			using (StreamReader streamReader = new StreamReader(fileStream, Encoding.ASCII))
			{
				string text2 = streamReader.ReadLine();
				int num = common.atoi(text2);
				if (num != 5)
				{
					Con.Print("Savegame is version {0}, not {1}\n", new object[]
					{
						num,
						5
					});
					return;
				}
				text2 = streamReader.ReadLine();
				float[] array = new float[16];
				for (int i = 0; i < array.Length; i++)
				{
					text2 = streamReader.ReadLine();
					array[i] = common.atof(text2);
				}
				text2 = streamReader.ReadLine();
				host.CurrentSkill = (int)((double)common.atof(text2) + 0.1);
				cvar.Set("skill", (float)host.CurrentSkill);
				string server = streamReader.ReadLine();
				text2 = streamReader.ReadLine();
				float num2 = common.atof(text2);
				client.Disconnect_f();
				server.SpawnServer(server);
				if (!server.sv.active)
				{
					Con.Print("Couldn't load map\n", Array.Empty<object>());
					return;
				}
				server.sv.paused = true;
				server.sv.loadgame = true;
				for (int j = 0; j < 64; j++)
				{
					text2 = streamReader.ReadLine();
					server.sv.lightstyles[j] = text2;
				}
				int num3 = -1;
				StringBuilder stringBuilder = new StringBuilder(32768);
				while (!streamReader.EndOfStream)
				{
					text2 = streamReader.ReadLine();
					if (text2 == null)
					{
						sys.Error("EOF without closing brace", Array.Empty<object>());
					}
					stringBuilder.AppendLine(text2);
					int num4 = text2.IndexOf('}');
					if (num4 != -1)
					{
						int length = 1 + stringBuilder.Length - (text2.Length - num4);
						string data = common.Parse(stringBuilder.ToString(0, length));
						if (string.IsNullOrEmpty(common.Token))
						{
							break;
						}
						if (common.Token != "{")
						{
							sys.Error("First token isn't a brace", Array.Empty<object>());
						}
						if (num3 == -1)
						{
							progs.ParseGlobals(data);
						}
						else
						{
							edict_t edict_t = server.EdictNum(num3);
							edict_t.Clear();
							progs.ParseEdict(data, edict_t);
							if (!edict_t.free)
							{
								server.LinkEdict(edict_t, false);
							}
						}
						num3++;
						stringBuilder.Remove(0, length);
					}
				}
				server.sv.num_edicts = num3;
				server.sv.time = (double)num2;
				for (int k = 0; k < 16; k++)
				{
					server.svs.clients[0].spawn_parms[k] = array[k];
				}
			}
			if (client.cls.state != cactive_t.ca_dedicated)
			{
				client.EstablishConnection("local");
				host.Reconnect_f();
			}
		}

		private static void Name_f()
		{
			if (cmd.Argc == 1)
			{
				Con.Print("\"name\" is \"{0}\"\n", new object[]
				{
					client.Name
				});
				return;
			}
			string text;
			if (cmd.Argc == 2)
			{
				text = cmd.Argv(1);
			}
			else
			{
				text = cmd.Args;
			}
			if (text.Length > 16)
			{
				text = text.Remove(15);
			}
			if (cmd.Source != cmd_source_t.src_command)
			{
				if (!string.IsNullOrEmpty(host.HostClient.name) && host.HostClient.name != "unconnected" && host.HostClient.name != text)
				{
					Con.Print("{0} renamed to {1}\n", new object[]
					{
						host.HostClient.name,
						text
					});
				}
				host.HostClient.name = text;
				host.HostClient.edict.v.netname = progs.NewString(text);
				MsgWriter reliable_datagram = server.sv.reliable_datagram;
				reliable_datagram.WriteByte(13);
				reliable_datagram.WriteByte(host.ClientNum);
				reliable_datagram.WriteString(text);
				return;
			}
			if (client.Name == text)
			{
				return;
			}
			cvar.Set("_cl_name", text);
			if (client.cls.state == cactive_t.ca_connected)
			{
				cmd.ForwardToServer();
			}
		}

		private static void Version_f()
		{
			Con.Print("Version {0}\n", new object[]
			{
				1.09f
			});
			Con.Print("Exe hash code: {0}\n", new object[]
			{
				Assembly.GetExecutingAssembly().GetHashCode()
			});
		}

		private static void Say(bool teamonly)
		{
			bool flag = false;
			if (cmd.Source == cmd_source_t.src_command)
			{
				if (client.cls.state != cactive_t.ca_dedicated)
				{
					cmd.ForwardToServer();
					return;
				}
				flag = true;
				teamonly = false;
			}
			if (cmd.Argc < 2)
			{
				return;
			}
			client_t hostClient = host.HostClient;
			string text = cmd.Args;
			if (text.StartsWith("\""))
			{
				text = text.Substring(1, text.Length - 2);
			}
			string text2;
			if (!flag)
			{
				text2 = "\u0001" + hostClient.name + ": ";
			}
			else
			{
				text2 = "\u0001<" + net.HostName + "> ";
			}
			text2 = text2 + text + "\n";
			for (int i = 0; i < server.svs.maxclients; i++)
			{
				client_t client_t = server.svs.clients[i];
				if (client_t != null && client_t.active && client_t.spawned && (host.TeamPlay == 0f || !teamonly || client_t.edict.v.team == hostClient.edict.v.team))
				{
					host.HostClient = client_t;
					server.ClientPrint(text2, Array.Empty<object>());
				}
			}
			host.HostClient = hostClient;
		}

		private static void Say_f()
		{
			host.Say(false);
		}

		private static void Say_Team_f()
		{
			host.Say(true);
		}

		private static void Tell_f()
		{
			if (cmd.Source == cmd_source_t.src_command)
			{
				cmd.ForwardToServer();
				return;
			}
			if (cmd.Argc < 3)
			{
				return;
			}
			string text = host.HostClient.name + ": ";
			string text2 = cmd.Args;
			if (text2.StartsWith("\""))
			{
				text2 = text2.Substring(1, text2.Length - 2);
			}
			text = text + text2 + "\n";
			client_t hostClient = host.HostClient;
			for (int i = 0; i < server.svs.maxclients; i++)
			{
				client_t client_t = server.svs.clients[i];
				if (client_t.active && client_t.spawned && !(client_t.name == cmd.Argv(1)))
				{
					host.HostClient = client_t;
					server.ClientPrint(text, Array.Empty<object>());
					break;
				}
			}
			host.HostClient = hostClient;
		}

		private static void Color_f()
		{
			if (cmd.Argc == 1)
			{
				Con.Print("\"color\" is \"{0} {1}\"\n", new object[]
				{
					(int)client.Color >> 4,
					(int)client.Color & 15
				});
				Con.Print("color <0-13> [0-13]\n", Array.Empty<object>());
				return;
			}
			int num2;
			int num;
			if (cmd.Argc == 2)
			{
				num = (num2 = common.atoi(cmd.Argv(1)));
			}
			else
			{
				num2 = common.atoi(cmd.Argv(1));
				num = common.atoi(cmd.Argv(2));
			}
			num2 &= 15;
			if (num2 > 13)
			{
				num2 = 13;
			}
			num &= 15;
			if (num > 13)
			{
				num = 13;
			}
			int num3 = num2 * 16 + num;
			if (cmd.Source == cmd_source_t.src_command)
			{
				cvar.Set("_cl_color", (float)num3);
				if (client.cls.state == cactive_t.ca_connected)
				{
					cmd.ForwardToServer();
				}
				return;
			}
			host.HostClient.colors = num3;
			host.HostClient.edict.v.team = (float)(num + 1);
			MsgWriter reliable_datagram = server.sv.reliable_datagram;
			reliable_datagram.WriteByte(17);
			reliable_datagram.WriteByte(host.ClientNum);
			reliable_datagram.WriteByte(host.HostClient.colors);
		}

		private static void Kill_f()
		{
			if (cmd.Source == cmd_source_t.src_command)
			{
				cmd.ForwardToServer();
				return;
			}
			if (server.Player.v.health <= 0f)
			{
				server.ClientPrint("Can't suicide -- allready dead!\n", Array.Empty<object>());
				return;
			}
			progs.GlobalStruct.time = (float)server.sv.time;
			progs.GlobalStruct.self = server.EdictToProg(server.Player);
			progs.Execute(progs.GlobalStruct.ClientKill);
		}

		private static void Pause_f()
		{
			if (cmd.Source == cmd_source_t.src_command)
			{
				cmd.ForwardToServer();
				return;
			}
			if (host._Pausable.Value == 0f)
			{
				server.ClientPrint("Pause not allowed.\n", Array.Empty<object>());
				return;
			}
			server.sv.paused = !server.sv.paused;
			if (server.sv.paused)
			{
				server.BroadcastPrint("{0} paused the game\n", new object[]
				{
					progs.GetString(server.Player.v.netname)
				});
			}
			else
			{
				server.BroadcastPrint("{0} unpaused the game\n", new object[]
				{
					progs.GetString(server.Player.v.netname)
				});
			}
			server.sv.reliable_datagram.WriteByte(24);
			server.sv.reliable_datagram.WriteByte(server.sv.paused ? 1 : 0);
		}

		private static void PreSpawn_f()
		{
			if (cmd.Source == cmd_source_t.src_command)
			{
				Con.Print("prespawn is not valid from the console\n", Array.Empty<object>());
				return;
			}
			if (host.HostClient.spawned)
			{
				Con.Print("prespawn not valid -- allready spawned\n", Array.Empty<object>());
				return;
			}
			MsgWriter message = host.HostClient.message;
			message.Write(server.sv.signon.Data, 0, server.sv.signon.Length);
			message.WriteByte(25);
			message.WriteByte(2);
			host.HostClient.sendsignon = true;
		}

		private static void Spawn_f()
		{
			if (cmd.Source == cmd_source_t.src_command)
			{
				Con.Print("spawn is not valid from the console\n", Array.Empty<object>());
				return;
			}
			if (host.HostClient.spawned)
			{
				Con.Print("Spawn not valid -- allready spawned\n", Array.Empty<object>());
				return;
			}
			edict_t edict_t;
			if (server.sv.loadgame)
			{
				server.sv.paused = false;
			}
			else
			{
				edict_t = host.HostClient.edict;
				edict_t.Clear();
				edict_t.v.colormap = (float)server.NumForEdict(edict_t);
				edict_t.v.team = (float)((host.HostClient.colors & 15) + 1);
				edict_t.v.netname = progs.NewString(host.HostClient.name);
				progs.GlobalStruct.SetParams(host.HostClient.spawn_parms);
				progs.GlobalStruct.time = (float)server.sv.time;
				progs.GlobalStruct.self = server.EdictToProg(server.Player);
				progs.Execute(progs.GlobalStruct.ClientConnect);
				if (sys.GetFloatTime() - host.HostClient.netconnection.connecttime <= server.sv.time)
				{
					Con.DPrint("{0} entered the game\n", new object[]
					{
						host.HostClient.name
					});
				}
				progs.Execute(progs.GlobalStruct.PutClientInServer);
			}
			MsgWriter message = host.HostClient.message;
			message.Clear();
			message.WriteByte(7);
			message.WriteFloat((float)server.sv.time);
			for (int i = 0; i < server.svs.maxclients; i++)
			{
				client_t client_t = server.svs.clients[i];
				message.WriteByte(13);
				message.WriteByte(i);
				message.WriteString(client_t.name);
				message.WriteByte(14);
				message.WriteByte(i);
				message.WriteShort(client_t.old_frags);
				message.WriteByte(17);
				message.WriteByte(i);
				message.WriteByte(client_t.colors);
			}
			for (int j = 0; j < 64; j++)
			{
				message.WriteByte(12);
				message.WriteByte((int)((ushort)j));
				message.WriteString(server.sv.lightstyles[j]);
			}
			message.WriteByte(3);
			message.WriteByte(QStats.STAT_TOTALSECRETS);
			message.WriteLong((int)progs.GlobalStruct.total_secrets);
			message.WriteByte(3);
			message.WriteByte(QStats.STAT_TOTALMONSTERS);
			message.WriteLong((int)progs.GlobalStruct.total_monsters);
			message.WriteByte(3);
			message.WriteByte(QStats.STAT_SECRETS);
			message.WriteLong((int)progs.GlobalStruct.found_secrets);
			message.WriteByte(3);
			message.WriteByte(QStats.STAT_MONSTERS);
			message.WriteLong((int)progs.GlobalStruct.killed_monsters);
			edict_t = server.EdictNum(1 + host.ClientNum);
			message.WriteByte(10);
			message.WriteAngle(edict_t.v.angles.x);
			message.WriteAngle(edict_t.v.angles.y);
			message.WriteAngle(0f);
			server.WriteClientDataToMessage(server.Player, host.HostClient.message);
			message.WriteByte(25);
			message.WriteByte(3);
			host.HostClient.sendsignon = true;
		}

		private static void Begin_f()
		{
			if (cmd.Source == cmd_source_t.src_command)
			{
				Con.Print("begin is not valid from the console\n", Array.Empty<object>());
				return;
			}
			host.HostClient.spawned = true;
		}

		private static void Kick_f()
		{
			if (cmd.Source == cmd_source_t.src_command)
			{
				if (!server.sv.active)
				{
					cmd.ForwardToServer();
					return;
				}
			}
			else if (progs.GlobalStruct.deathmatch != 0f && !host.HostClient.privileged)
			{
				return;
			}
			client_t hostClient = host.HostClient;
			bool flag = false;
			int i;
			if (cmd.Argc > 2 && cmd.Argv(1) == "#")
			{
				i = (int)common.atof(cmd.Argv(2)) - 1;
				if (i < 0 || i >= server.svs.maxclients)
				{
					return;
				}
				if (!server.svs.clients[i].active)
				{
					return;
				}
				host.HostClient = server.svs.clients[i];
				flag = true;
			}
			else
			{
				for (i = 0; i < server.svs.maxclients; i++)
				{
					host.HostClient = server.svs.clients[i];
					if (host.HostClient.active && common.SameText(host.HostClient.name, cmd.Argv(1)))
					{
						break;
					}
				}
			}
			if (i < server.svs.maxclients)
			{
				string text;
				if (cmd.Source == cmd_source_t.src_command)
				{
					if (client.cls.state == cactive_t.ca_dedicated)
					{
						text = "Console";
					}
					else
					{
						text = client.Name;
					}
				}
				else
				{
					text = hostClient.name;
				}
				if (host.HostClient == hostClient)
				{
					return;
				}
				string text2 = null;
				if (cmd.Argc > 2)
				{
					text2 = common.Parse(cmd.Args);
					if (flag)
					{
						text2 = text2.Substring(1);
						text2 = text2.Trim();
						text2 = text2.Substring(cmd.Argv(2).Length);
					}
					text2 = text2.Trim();
				}
				if (!string.IsNullOrEmpty(text2))
				{
					server.ClientPrint("Kicked by {0}: {1}\n", new object[]
					{
						text,
						text2
					});
				}
				else
				{
					server.ClientPrint("Kicked by {0}\n", new object[]
					{
						text
					});
				}
				server.DropClient(false);
			}
			host.HostClient = hostClient;
		}

		private static void Give_f()
		{
			if (cmd.Source == cmd_source_t.src_command)
			{
				cmd.ForwardToServer();
				return;
			}
			if (progs.GlobalStruct.deathmatch != 0f && !host.HostClient.privileged)
			{
				return;
			}
			string text = cmd.Argv(1);
			int num = common.atoi(cmd.Argv(2));
			if (string.IsNullOrEmpty(text))
			{
				return;
			}
			char c = text[0];
			switch (c)
			{
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
				if (common.GameKind == GameKind.Hipnotic)
				{
					if (text[0] == '6')
					{
						if (text[1] == 'a')
						{
							server.Player.v.items = (float)((int)server.Player.v.items | QItems.HIT_PROXIMITY_GUN);
							return;
						}
						server.Player.v.items = (float)((int)server.Player.v.items | QItems.IT_GRENADE_LAUNCHER);
						return;
					}
					else
					{
						if (text[0] == '9')
						{
							server.Player.v.items = (float)((int)server.Player.v.items | QItems.HIT_LASER_CANNON);
							return;
						}
						if (text[0] == '0')
						{
							server.Player.v.items = (float)((int)server.Player.v.items | QItems.HIT_MJOLNIR);
							return;
						}
						if (text[0] >= '2')
						{
							server.Player.v.items = (float)((int)server.Player.v.items | QItems.IT_SHOTGUN << (int)(text[0] - '2'));
							return;
						}
					}
				}
				else if (text[0] >= '2')
				{
					server.Player.v.items = (float)((int)server.Player.v.items | QItems.IT_SHOTGUN << (int)(text[0] - '2'));
					return;
				}
				break;
			default:
				if (c != 'c')
				{
					switch (c)
					{
					case 'h':
						server.Player.v.health = (float)num;
						return;
					case 'i':
					case 'j':
					case 'k':
					case 'o':
					case 'q':
						break;
					case 'l':
						if (common.GameKind == GameKind.Rogue && progs.SetEdictFieldFloat(server.Player, "ammo_lava_nails", (float)num) && server.Player.v.weapon > (float)QItems.IT_LIGHTNING)
						{
							server.Player.v.ammo_nails = (float)num;
							return;
						}
						break;
					case 'm':
						if (common.GameKind == GameKind.Rogue && progs.SetEdictFieldFloat(server.Player, "ammo_multi_rockets", (float)num) && server.Player.v.weapon > (float)QItems.IT_LIGHTNING)
						{
							server.Player.v.ammo_rockets = (float)num;
							return;
						}
						break;
					case 'n':
						if (common.GameKind != GameKind.Rogue)
						{
							server.Player.v.ammo_nails = (float)num;
							return;
						}
						if (progs.SetEdictFieldFloat(server.Player, "ammo_nails1", (float)num) && server.Player.v.weapon <= (float)QItems.IT_LIGHTNING)
						{
							server.Player.v.ammo_nails = (float)num;
							return;
						}
						break;
					case 'p':
						if (common.GameKind == GameKind.Rogue && progs.SetEdictFieldFloat(server.Player, "ammo_plasma", (float)num) && server.Player.v.weapon > (float)QItems.IT_LIGHTNING)
						{
							server.Player.v.ammo_cells = (float)num;
						}
						break;
					case 'r':
						if (common.GameKind != GameKind.Rogue)
						{
							server.Player.v.ammo_rockets = (float)num;
							return;
						}
						if (progs.SetEdictFieldFloat(server.Player, "ammo_rockets1", (float)num) && server.Player.v.weapon <= (float)QItems.IT_LIGHTNING)
						{
							server.Player.v.ammo_rockets = (float)num;
							return;
						}
						break;
					case 's':
						if (common.GameKind == GameKind.Rogue)
						{
							progs.SetEdictFieldFloat(server.Player, "ammo_shells1", (float)num);
						}
						server.Player.v.ammo_shells = (float)num;
						return;
					default:
						return;
					}
				}
				else
				{
					if (common.GameKind != GameKind.Rogue)
					{
						server.Player.v.ammo_cells = (float)num;
						return;
					}
					if (progs.SetEdictFieldFloat(server.Player, "ammo_cells1", (float)num) && server.Player.v.weapon <= (float)QItems.IT_LIGHTNING)
					{
						server.Player.v.ammo_cells = (float)num;
						return;
					}
				}
				break;
			}
		}

		private static edict_t FindViewthing()
		{
			for (int i = 0; i < server.sv.num_edicts; i++)
			{
				edict_t edict_t = server.EdictNum(i);
				if (progs.GetString(edict_t.v.classname) == "viewthing")
				{
					return edict_t;
				}
			}
			Con.Print("No viewthing on map\n", Array.Empty<object>());
			return null;
		}

		private static void Viewmodel_f()
		{
			edict_t edict_t = host.FindViewthing();
			if (edict_t == null)
			{
				return;
			}
			model_t model_t = Mod.ForName(cmd.Argv(1), false);
			if (model_t == null)
			{
				Con.Print("Can't load {0}\n", new object[]
				{
					cmd.Argv(1)
				});
				return;
			}
			edict_t.v.frame = 0f;
			client.cl.model_precache[(int)edict_t.v.modelindex] = model_t;
		}

		private static void Viewframe_f()
		{
			edict_t edict_t = host.FindViewthing();
			if (edict_t == null)
			{
				return;
			}
			model_t model_t = client.cl.model_precache[(int)edict_t.v.modelindex];
			int num = common.atoi(cmd.Argv(1));
			if (num >= model_t.numframes)
			{
				num = model_t.numframes - 1;
			}
			edict_t.v.frame = (float)num;
		}

		private static void PrintFrameName(model_t m, int frame)
		{
			aliashdr_t extraData = Mod.GetExtraData(m);
			if (extraData == null)
			{
				return;
			}
			Con.Print("frame {0}: {1}\n", new object[]
			{
				frame,
				extraData.frames[frame].name
			});
		}

		private static void Viewnext_f()
		{
			edict_t edict_t = host.FindViewthing();
			if (edict_t == null)
			{
				return;
			}
			model_t model_t = client.cl.model_precache[(int)edict_t.v.modelindex];
			edict_t.v.frame = edict_t.v.frame + 1f;
			if (edict_t.v.frame >= (float)model_t.numframes)
			{
				edict_t.v.frame = (float)(model_t.numframes - 1);
			}
			host.PrintFrameName(model_t, (int)edict_t.v.frame);
		}

		private static void Viewprev_f()
		{
			edict_t edict_t = host.FindViewthing();
			if (edict_t == null)
			{
				return;
			}
			model_t m = client.cl.model_precache[(int)edict_t.v.modelindex];
			edict_t.v.frame = edict_t.v.frame - 1f;
			if (edict_t.v.frame < 0f)
			{
				edict_t.v.frame = 0f;
			}
			host.PrintFrameName(m, (int)edict_t.v.frame);
		}

		private static void Startdemos_f()
		{
			if (client.cls.state == cactive_t.ca_dedicated)
			{
				if (!server.sv.active)
				{
					Cbuf.AddText("map start\n");
				}
				return;
			}
			int num = cmd.Argc - 1;
			if (num > 8)
			{
				Con.Print("Max {0} demos in demoloop\n", new object[]
				{
					8
				});
				num = 8;
			}
			Con.Print("{0} demo(s) in loop\n", new object[]
			{
				num
			});
			for (int i = 1; i < num + 1; i++)
			{
				client.cls.demos[i - 1] = common.Copy(cmd.Argv(i), 16);
			}
			if (!server.sv.active && client.cls.demonum != -1 && !client.cls.demoplayback)
			{
				client.cls.demonum = 0;
				client.NextDemo();
				return;
			}
			client.cls.demonum = -1;
		}

		private static void Demos_f()
		{
			if (client.cls.state == cactive_t.ca_dedicated)
			{
				return;
			}
			if (client.cls.demonum == -1)
			{
				client.cls.demonum = 1;
			}
			client.Disconnect_f();
			client.NextDemo();
		}

		private static void Stopdemo_f()
		{
			if (client.cls.state == cactive_t.ca_dedicated)
			{
				return;
			}
			if (!client.cls.demoplayback)
			{
				return;
			}
			client.StopPlayback();
			client.Disconnect();
		}

		public static double FrameTime;

		public static double RealTime;

		public static int CurrentSkill;

		public static bool NoClipAngleHack;

		public static client_t HostClient;

		private const int VCR_SIGNATURE = 1447252529;

		private const int SAVEGAME_VERSION = 5;

		private static quakeparms_t _Params;

		private static cvar _Sys_TickRate;

		private static cvar _Developer;

		private static cvar _FrameRate;

		private static cvar _Speeds;

		private static cvar _ServerProfile;

		private static cvar _FragLimit;

		private static cvar _TimeLimit;

		private static cvar _Teamplay;

		private static cvar _SameLevel;

		private static cvar _NoExit;

		private static cvar _Skill;

		private static cvar _Deathmatch;

		private static cvar _Coop;

		private static cvar _Pausable;

		private static cvar _Temp1;

		private static bool _IsInitialized;

		private static int _FrameCount;

		private static byte[] _BasePal;

		private static byte[] _ColorMap;

		private static double _Time;

		private static double _OldRealTime;

		private static BinaryReader _VcrReader;

		private static BinaryWriter _VcrWriter;

		private static double _TimeTotal;

		private static int _TimeCount;

		private static double _Time1;

		private static double _Time2;

		private static double _Time3;

		private static int _ShutdownDepth;

		private static int _ErrorDepth;
	}
}
