Пример #1
0
        /// <summary>
        /// Saves the current character to file
        /// </summary>
        /// <param name="backup_existing">If an current character file already exists, this flag indicates whether to try to back it up</param>
        public static void SaveCurrentCharacter(bool backup_existing = true)
        {
            ICharacter character;

            if (GameManager.Manager.TryGet <ICharacter>(out character))
            {
                var target = System.IO.Path.ChangeExtension(Properties.Resources.CharacterFileName, SerializableCharacter.CharacterFileExtension);

                if (backup_existing)
                {
                    if (System.IO.File.Exists(target))
                    {
                        try
                        {
                            var c = SerializableCharacter.Load(target);

                            var bck_up_trgt = System.IO.Path.ChangeExtension("Char" + c.ID.ToString(), SerializableCharacter.CharacterFileExtension);

                            if (System.IO.File.Exists(bck_up_trgt))
                            {
                                System.IO.File.Delete(bck_up_trgt);
                            }

                            System.IO.File.Move(target, bck_up_trgt);

                            logger.InfoFormat("Existing character file backup up to {0}", bck_up_trgt);
                        }
                        catch (Exception e)
                        {
                            logger.Error("Could not backup existing character file.", e);
                        }
                    }
                    else
                    {
                        logger.InfoFormat("No existing file was found @ {0}, so no backup was made.", target);
                    }
                }
                else
                {
                    logger.Info("No backup was requested when saving the current character");

                    if (System.IO.File.Exists(target))
                    {
                        logger.InfoFormat("An existing character file was found and will be overwritten @ {0}", target);
                    }
                }

                character.Serialize(target);

                logger.InfoFormat("Current character was saved to the default character file @ {0}", target);
            }
            else
            {
                logger.Warn("Could not find an existing character instance to save as the interface was not registered");
            }
        }
Пример #2
0
        /// <summary>
        /// Registers an entier TypeRegister with the GameManager
        /// </summary>
        /// <param name="registery"></param>
        /// <param name="overwrite_existing_interface_keys"></param>
        public void RegisterTypeRegister(TypeRegister registery, bool overwrite_existing_interface_keys)
        {
            foreach (var entry in registery.Entries.OrderBy(x => x.CreationIndex))
            {
                if (entry.Value.InterfaceType.Equals(typeof(ICharacter)))
                {
                    var inner = this.GetInstance <ISerializableCharacter>();

                    entry.Value.Instance = new Character(inner);
                }
                else if (entry.Value.InterfaceType.Equals(typeof(ISerializableCharacter)))
                {
                    var target = System.IO.Path.ChangeExtension(Properties.Resources.CharacterFileName, SerializableCharacter.CharacterFileExtension);

                    if (System.IO.File.Exists(target))
                    {
                        entry.Value.Instance = SerializableCharacter.Load(target);
                    }
                }

                this.RegisterTypeEntry(entry.Value, overwrite_existing_interface_keys);
            }
        }