internal Screen(GameProfile gameProfile, string name, bool useAdvanced, Geometry geometry)
 {
     GameProfile = gameProfile;
     Name        = name;
     UseAdvanced = useAdvanced;
     Geometry    = geometry;
 }
 public static GameProfile FromXmlStream(Stream stream)
 {
     using (StreamReader reader = new StreamReader(stream))
     {
         XmlSerializer serializer = new XmlSerializer(typeof(GameProfile));
         GameProfile   gp         = (GameProfile)serializer.Deserialize(reader);
         gp.ReSyncRelationships();
         return(gp);
     }
 }
Exemple #3
0
        public CompiledFeatures(GameProfile gameProfile, Geometry cropGeometry, int pixelLimit = INIT_PIXEL_LIMIT)
        {
            CaptureGeometry = cropGeometry;
            _HasDupeCheck   = false;
            PixelLimit      = pixelLimit; // Todo: Implement resizing when (total) PixelCount exceeds PixelLimit. It won't be easy.
            PixelCount      = 0;

            var nameDictionary = new Dictionary <string, List <int> >();

            var cWatchZones = new CWatchZone[gameProfile.WatchZones.Count];
            var indexCount  = 0;

            // Using this until full implementation of multiple screens.
            foreach (var s in gameProfile.Screens)
            {
                s.CropGeometry = CaptureGeometry;
            }

            for (int i1 = 0; i1 < gameProfile.WatchZones.Count; i1++)
            {
                WatchZone watchZone = gameProfile.WatchZones[i1];
                Screen    screen    = watchZone.Screen;

                var CWatches = new CWatcher[watchZone.Watches.Count];

                var gameGeo   = screen.GameGeometry.HasSize ? screen.GameGeometry : screen.Geometry;
                var wzCropGeo = watchZone.WithoutScale(gameGeo);
                wzCropGeo.RemoveAnchor(gameGeo);
                wzCropGeo.ResizeTo(screen.CropGeometry, gameGeo);
                wzCropGeo.Adjust(screen.CropGeometry.X, screen.CropGeometry.Y);
                wzCropGeo = wzCropGeo.Clamp(MIN_GEOMETRY, MAX_GEOMETRY);

                for (int i2 = 0; i2 < watchZone.Watches.Count; i2++)
                {
                    Watcher watcher = watchZone.Watches[i2];

                    if (watcher.WatcherType == WatcherType.Standard)
                    {
                        var wiCount      = watcher.WatchImages.Count;
                        var CWatchImages = new CWatchImage[wiCount];

                        if (wiCount <= 0)
                        {
                            throw new ArgumentException("Standard Watchers require at least one image to compare against.");
                        }

                        for (int i3 = 0; i3 < wiCount; i3++)
                        {
                            WatchImage watchImage = watcher.WatchImages[i3];
                            var        mi         = new MagickImage(watchImage.Image)
                            {
                                ColorSpace = watcher.ColorSpace
                            };

                            GetComposedImage(ref mi, watcher.Channel);
                            StandardResize(ref mi, wzCropGeo);
                            //PreciseResize(ref mi, watchZone.Geometry, gameGeo, screen.CropGeometry, watcher.ColorSpace);
                            if (watcher.Equalize)
                            {
                                mi.Equalize();
                            }

                            PixelCount += (int)Math.Round(wzCropGeo.Width) * (int)Math.Round(wzCropGeo.Height); // Todo: Un-hardcode the rounding

                            var metricUpperBound = watcher.ErrorMetric.UpperBound(PixelCount, mi.ChannelCount);

                            CWatchImages[i3] = new CWatchImage(watchImage.Name, indexCount, mi, metricUpperBound);

                            AddIndexName(nameDictionary, indexCount, watchZone.Name, watcher.Name, watchImage.FileName);

                            indexCount++;
                        }

                        CWatches[i2] = new CWatcher(CWatchImages, watcher);
                    }
                    else if (watcher.WatcherType == WatcherType.DuplicateFrame)
                    {
                        _HasDupeCheck = true;

                        CWatches[i2] = new CWatcher(new CWatchImage[] { new CWatchImage(watcher.Name, indexCount) }, watcher);

                        AddIndexName(nameDictionary, indexCount, watchZone.Name, watcher.Name, string.Empty);
                        PixelCount += (int)Math.Round(wzCropGeo.Width) * (int)Math.Round(wzCropGeo.Height); // Todo: Un-hardcode the rounding
                        indexCount++;
                    }
                    else
                    {
                        throw new NotImplementedException("WatcherType is not set or does not exist. This really shouldn't happen.");
                    }
                }

                cWatchZones[i1] = new CWatchZone(watchZone.Name, wzCropGeo, CWatches);
            }

            var nameDicArray = new Dictionary <string, IEnumerable <int> >();

            foreach (var entry in nameDictionary)
            {
                nameDicArray.Add(entry.Key, entry.Value.Distinct().OrderBy(x => x));
            }

            IndexNames   = nameDicArray;
            FeatureCount = indexCount;
            CWatchZones  = cWatchZones;
        }
        private static GameProfile FromZip(string filePath)
        {
            var archive = ZipFile.OpenRead(filePath);
            var entries = archive.Entries;

            var search   = ".xml";
            var xmlFiles = entries.Where(z => z.Name.ToLower().Contains(search));

            if (xmlFiles.Count() == 0)
            {
                throw new Exception("Game Profile XML is missing");
            }
            else if (xmlFiles.Count() > 1)
            {
                throw new Exception("Multiple XML files found, we only need one.");
            }
            var xmlStream = xmlFiles.First().Open();


            GameProfile gp = FromXmlStream(xmlStream);


            search = "script.asl"; // To rename
            var scriptFiles = entries.Where(z => z.Name.ToLower().Contains(search));

            if (scriptFiles.Count() == 0)
            {
                throw new Exception("Script file (script.asl) is missing.");
            }
            else if (scriptFiles.Count() > 1)
            {
                throw new Exception("Multiple script files found, we only need one.");
            }
            var scriptStream = scriptFiles.First().Open();

            gp.RawScript = new StreamReader(scriptStream).ReadToEnd();

            foreach (var s in gp.Screens)
            {
                search = s.Name.ToLower() + "_autofit.png";
                var files = entries.Where(z => z.Name.ToLower().Contains(search));
                if (files.Count() == 0)
                {
                    continue;
                }
                else if (files.Count() > 1)
                {
                    throw new Exception("Multiple autofit images found for " + s.Name + ", only one per screen is currently supported.");
                }
                var imageStream = files.First().Open();
                s.Autofitter.Image = new Bitmap(imageStream);
            }
            foreach (var wi in gp.WatchImages)
            {
                search = wi.FilePath.Replace('\\', '/').ToLower();
                var files = entries.Where(z => z.FullName.ToLower().Contains(search));
                if (files.Count() == 0)
                {
                    throw new Exception("Image for " + search + " not found.");
                }
                else if (files.Count() > 1)
                {
                    throw new Exception("Multiple images found for " + search + ", somehow.");
                }
                var imageStream = files.First().Open();
                wi.Image = new Bitmap(imageStream);
            }

            return(gp);
        }
        private static GameProfile FromFolder(string folderPath)
        {
            var files = Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories);

            var search   = "*.xml";
            var xmlFiles = Directory.GetFiles(folderPath, search, SearchOption.TopDirectoryOnly);

            if (xmlFiles.Count() == 0)
            {
                throw new Exception("Game Profile XML is missing");
            }
            else if (xmlFiles.Count() > 1)
            {
                throw new Exception("Multiple XML files found, we only need one.");
            }
            var xmlFile = xmlFiles.First();


            GameProfile gp = FromXmlStream(new FileStream(xmlFile, FileMode.Open, FileAccess.Read));


            search = "script.asl"; // To rename
            var scriptFiles = Directory.GetFiles(folderPath, search, SearchOption.TopDirectoryOnly);

            if (scriptFiles.Count() == 0)
            {
                throw new Exception("Script file (script.asl) is missing.");
            }
            else if (scriptFiles.Count() > 1)
            {
                Log.Warning("Multiple script files found, we only need one. Using first one.");
            }
            using (var scriptStream = File.Open(scriptFiles.First(), FileMode.Open))
                gp.RawScript = new StreamReader(scriptStream).ReadToEnd();

            foreach (var s in gp.Screens)
            {
                search = s.Name.ToLower() + "_autofit.png";
                var autofitFiles = Directory.GetFiles(folderPath, search, SearchOption.AllDirectories);
                if (autofitFiles.Count() == 0)
                {
                    continue;
                }
                else if (autofitFiles.Count() > 1)
                {
                    Log.Warning("Multiple autofit images found for screen " + s.Name + ". Using first one.");
                }
                var autofitFile = autofitFiles.First();
                s.Autofitter.Image = new Bitmap(autofitFile);
            }
            foreach (var wi in gp.WatchImages)
            {
                search = wi.FilePath.Replace('\\', '/').ToLower();
                var imageFiles = Directory.GetFiles(folderPath, search, SearchOption.AllDirectories);
                if (imageFiles.Count() == 0)
                {
                    throw new Exception("Image for " + search + " not found.");
                }
                else if (imageFiles.Count() > 1)
                {
                    Log.Warning("Multiple images found for " + wi.FilePath + ", somehow. Using first one.");
                }
                var imageFile = imageFiles.First();
                wi.Image = new Bitmap(imageFile);
            }

            return(gp);
        }