<commit_before>﻿using Microsoft.Xna.Framework;
using OctoAwesome.Runtime;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Serialization;

namespace OctoAwesome.Client.Components
{
    internal sealed class SimulationComponent : GameComponent
    {
        public World World { get; private set; }

        public ActorHost Player { get; private set; }
        
        public SimulationComponent(Game game) : base(game) { }

        public override void Initialize()
        {
            World = new World();

            var p = Load();
            Player = World.InjectPlayer(p);
            Player.Initialize();

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            World.Update(gameTime);
        }

        internal void Save()
        {
            Save(Player.Player);
        }

        public void Save(Player player)
        {
            var root = GetRoot();

            string filename = "player.info";
            using (Stream stream = File.Open(root.FullName + Path.DirectorySeparatorChar + filename, FileMode.Create, FileAccess.Write))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Player));
                serializer.Serialize(stream, player);
            }
        }

        public Player Load()
        {
            var root = GetRoot();
            string filename = "player.info";

            if (!File.Exists(root.FullName + Path.DirectorySeparatorChar + filename))
                return null;

            using (Stream stream = File.Open(root.FullName + Path.DirectorySeparatorChar + filename, FileMode.Open, FileAccess.Read))
            {
                try
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(Player));
                    return (Player)serializer.Deserialize(stream);
                }
                catch (Exception) { }

                return new Player();
            }
        }

        private DirectoryInfo GetRoot()
        {
            string appconfig = ConfigurationManager.AppSettings["ChunkRoot"];
            if (!string.IsNullOrEmpty(appconfig))
            {
                return new DirectoryInfo(appconfig);
            }
            else
            {
                var exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                DirectoryInfo root = new DirectoryInfo(exePath + Path.DirectorySeparatorChar + "OctoMap");
                if (!root.Exists) root.Create();
                return root;
            }
        }<commit_msg>Bugfix Spielstart bei keiner player.info-Datei<commit_after>﻿using Microsoft.Xna.Framework;
using OctoAwesome.Runtime;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Serialization;

namespace OctoAwesome.Client.Components
{
    internal sealed class SimulationComponent : GameComponent
    {
        public World World { get; private set; }

        public ActorHost Player { get; private set; }
        
        public SimulationComponent(Game game) : base(game) { }

        public override void Initialize()
        {
            World = new World();

            var p = Load();
            Player = World.InjectPlayer(p);
            Player.Initialize();

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            World.Update(gameTime);
        }

        internal void Save()
        {
            Save(Player.Player);
        }

        public void Save(Player player)
        {
            var root = GetRoot();

            string filename = "player.info";
            using (Stream stream = File.Open(root.FullName + Path.DirectorySeparatorChar + filename, FileMode.Create, FileAccess.Write))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Player));
                serializer.Serialize(stream, player);
            }
        }

        public Player Load()
        {
            var root = GetRoot();
            string filename = "player.info";

            if (!File.Exists(root.FullName + Path.DirectorySeparatorChar + filename))
                return new Player();

            using (Stream stream = File.Open(root.FullName + Path.DirectorySeparatorChar + filename, FileMode.Open, FileAccess.Read))
            {
                try
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(Player));
                    return (Player)serializer.Deserialize(stream);
                }
                catch (Exception) { }

                return new Player();
            }
        }

        private DirectoryInfo GetRoot()
        {
            string appconfig = ConfigurationManager.AppSettings["ChunkRoot"];
            if (!string.IsNullOrEmpty(appconfig))
            {
                return new DirectoryInfo(appconfig);
            }
            else
            {
                var exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                DirectoryInfo root = new DirectoryInfo(exePath + Path.DirectorySeparatorChar + "OctoMap");
                if (!root.Exists) root.Create();
                return root;
            }
        }
{'fraction_non_alphanumeric': 0.08822991030569284, 'fraction_numerical': 0.0, 'mean_word_length': 1.7265469061876249, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5043573', 'n_tokens_mistral': 1337, 'n_tokens_neox': 1276, 'n_words': 309}