public ConfigurableImageShell(ImageShellConfiguration configuration)
        {
            InitializeComponent();

            Configuration = configuration;

            Opacity = Configuration.Opacity;
            Width   = Configuration.Width;
            Height  = Configuration.Height;

            foreach (Emotion emotion in Enum.GetValues(typeof(Emotion)))
            {
                _images.Add(emotion, new List <BitmapImage>());

                foreach (var file in new DirectoryInfo(configuration.ImageFolder).GetFiles($"{emotion}*.*"))
                {
                    _images[emotion].Add(new BitmapImage(new Uri(file.FullName, UriKind.Absolute)));
                }
            }

            _emotionTimer.Tick += EmotionTimer_Tick;

            TheImage.Source        = _images[Emotion.Normal][_random.Next(_images[Emotion.Normal].Count)];
            _emotionTimer.Interval = GetRandomTimeSpan(Emotion.Normal);

            _emotionTimer.Start();
        }
Exemplo n.º 2
0
        private static ImageShellConfiguration LoadConfiguration()
        {
            var configuration = new ImageShellConfiguration();

            var studioPetPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "StudioPet");
            var configFile    = Path.Combine(studioPetPath, "studiopet.config");

            if (!Directory.Exists(studioPetPath))
            {
                Directory.CreateDirectory(studioPetPath);
                using (var file = File.CreateText(configFile))
                {
                    var serializer = new JsonSerializer {
                        Formatting = Formatting.Indented
                    };
                    serializer.Converters.Add(new StringEnumConverter());
                    serializer.Serialize(file, configuration);
                }


                var extensionPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                if (extensionPath != null)
                {
                    foreach (var imageFile in new DirectoryInfo(Path.Combine(extensionPath, "PictureSets")).GetFiles("*.*", SearchOption.AllDirectories))
                    {
                        var destFile = new FileInfo(imageFile.FullName.Replace(extensionPath, studioPetPath));
                        if (destFile.Directory != null && !destFile.Directory.Exists)
                        {
                            destFile.Directory.Create();
                        }
                        File.Copy(imageFile.FullName, destFile.FullName);
                    }
                }
            }

            if (File.Exists(configFile))
            {
                try
                {
                    var serializer = new JsonSerializer();
                    serializer.Converters.Add(new StringEnumConverter());

                    using (var file = File.OpenText(configFile))
                    {
                        using (var reader = new JsonTextReader(file))
                        {
                            configuration = serializer.Deserialize <ImageShellConfiguration>(reader);
                        }
                    }
                }
                catch (Exception err)
                {
                    var errorLog =
                        $"{DateTime.Now:dd-MM-yyyy HH:mm:ss}{Environment.NewLine}{err.GetType()}: {err.Message}{Environment.NewLine}{err.StackTrace}";
                    File.WriteAllText(Path.Combine(studioPetPath, "error.log"), errorLog);
                }
            }

            configuration.ImageFolder = Path.Combine(studioPetPath, "PictureSets", configuration.PictureSet);
            return(configuration);
        }