Example #1
0
        /// <summary>
        /// Host_Savegame_f
        /// </summary>
        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");
                return;
            }

            if (Client.cl.intermission != 0)
            {
                Con.Print("Can't save in intermission.\n");
                return;
            }

            if (Server.svs.maxclients != 1)
            {
                Con.Print("Can't save multiplayer games.\n");
                return;
            }

            if (Cmd.Argc != 2)
            {
                Con.Print("save <savename> : save a game\n");
                return;
            }

            if (Cmd.Argv(1).Contains(".."))
            {
                Con.Print("Relative pathnames are not allowed.\n");
                return;
            }

            for (int i = 0; i < Server.svs.maxclients; i++)
            {
                if (Server.svs.clients[i].active && (Server.svs.clients[i].edict.v.health <= 0))
                {
                    Con.Print("Can't savegame with a dead player\n");
                    return;
                }
            }

            string name = Path.ChangeExtension(Path.Combine(Common.GameDir, Cmd.Argv(1)), ".sav");

            Con.Print("Saving game to {0}...\n", name);
            FileStream fs = Sys.FileOpenWrite(name, true);

            if (fs == null)
            {
                Con.Print("ERROR: couldn't open.\n");
                return;
            }
            using (StreamWriter writer = new StreamWriter(fs, Encoding.ASCII))
            {
                writer.WriteLine(SAVEGAME_VERSION);
                writer.WriteLine(SavegameComment());

                for (int i = 0; i < Server.NUM_SPAWN_PARMS; i++)
                {
                    writer.WriteLine(Server.svs.clients[0].spawn_parms[i].ToString("F6",
                                                                                   CultureInfo.InvariantCulture.NumberFormat));
                }

                writer.WriteLine(Host.CurrentSkill);
                writer.WriteLine(Server.sv.name);
                writer.WriteLine(Server.sv.time.ToString("F6",
                                                         CultureInfo.InvariantCulture.NumberFormat));

                // write the light styles

                for (int i = 0; i < QDef.MAX_LIGHTSTYLES; i++)
                {
                    if (!String.IsNullOrEmpty(Server.sv.lightstyles[i]))
                    {
                        writer.WriteLine(Server.sv.lightstyles[i]);
                    }
                    else
                    {
                        writer.WriteLine("m");
                    }
                }

                Progs.WriteGlobals(writer);
                for (int i = 0; i < Server.sv.num_edicts; i++)
                {
                    Progs.WriteEdict(writer, Server.EdictNum(i));
                    writer.Flush();
                }
            }
            Con.Print("done.\n");
        }