Beispiel #1
0
        public void Load(string path)
        {
            FilePath   = path;
            _container = new FileContainer(path, _logger);

            _logger?.LogInformation($"Loading file: {Path.GetFileName(path)}");

            var entities = _container.GetEntities();
            var parts    = _container
                           .ReadRawData(entities)
                           .Select(
                (entityRaw, index) =>
            {
                try
                {
                    var convert       = Convert(entityRaw, _container.Endian, _container.Header.Version);
                    entityRaw.RawData = null;
                    entityRaw.Entity.Dispose();
                    return(convert);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    _logger?.LogCritical(
                        "Cant load parts {index}.",
                        index
                        );
                }

                return(null);
            }
                )
                           .Where(part => part != null)
                           .ToArray();

            ShapeCount = parts.Length;

            Shapes = parts
                     .OfType <I3DShape>()
                     .ToArray();
            Splines = parts
                      .OfType <Spline>()
                      .ToArray();
        }
Beispiel #2
0
        public void LoadTypedShape <T>(FarmSimulatorVersion version, string shapeFileName, int rawType, Func <BinaryReader, int, T> loadShape)
        {
            var gameMapPath = GamePaths.GetGameMapsPath(version);

            if (!Directory.Exists(gameMapPath))
            {
                var message = $"Game map path not found: \"{version}\".";
                Trace.WriteLine(message);
                Assert.Inconclusive(message);
            }

            var mapPath = Path.Combine(gameMapPath, shapeFileName);

            if (!File.Exists(mapPath))
            {
                var message = $"Map not found \"{version}\": \"{mapPath}\".";
                Trace.WriteLine(message);
                Assert.Inconclusive(message);
            }

            // Clear directory by error shapes
            var errorOutputPath = Path.Combine(OutputErrorShapes, version.ToString(), shapeFileName);

            if (Directory.Exists(errorOutputPath))
            {
                Directory.Delete(errorOutputPath, true);
            }

            var fileContainer = new FileContainer(mapPath);
            var entities      = fileContainer.GetEntities();
            var error         = false;

            fileContainer.ReadRawData(entities)
            .Where(v => v.Entity.Type == rawType)
            .ForEach(
                v =>
            {
                try
                {
                    using var stream = new MemoryStream(v.RawData);
                    using var reader = new EndianBinaryReader(stream, fileContainer.Endian);
                    var shape        = loadShape(reader, fileContainer.Header.Version);
                }
                catch (Exception ex)
                {
                    error            = true;
                    using var stream = new MemoryStream(v.RawData);
                    using var reader = new EndianBinaryReader(stream, fileContainer.Endian);
                    Trace.WriteLine($"Error load shape.");
                    var errorShape2 = new ReverseEngineeringNamedShape1Object(
                        v.Entity.Type,
                        reader,
                        fileContainer.Endian
                        );
                    Save(errorOutputPath, version, errorShape2, v.RawData);
                }
            }
                );

            if (error)
            {
                Assert.Fail();
            }
        }
Beispiel #3
0
        public void ReadAllShapes(FarmSimulatorVersion version)
        {
            var hasError = false;
            var gamePath = GamePaths.GetGamePath(version);

            if (gamePath == null || !Directory.Exists(gamePath))
            {
                Assert.Inconclusive($"Game path not found: {version}");
            }

            var shapeFiles = Directory.GetFiles(gamePath, $"*{GameConstants.SchapesFileExtension}", SearchOption.AllDirectories);

            shapeFiles
            .AsParallel()
            .WithDegreeOfParallelism(Environment.ProcessorCount)
            .ForEach(
                filePath =>
            {
                try
                {
                    var container = new FileContainer(filePath);
                    var entities  = container.GetEntities();
                    foreach (var valueTuple in container.ReadRawData(entities))
                    {
                        using (var stream = new MemoryStream(valueTuple.RawData))
                        {
                            try
                            {
                                using (var reader = new EndianBinaryReader(stream, container.Endian, true))
                                {
                                    switch (valueTuple.Entity.Type)
                                    {
                                    case 1:
                                        var shape = new Shape(reader, container.Header.Version);
                                        break;

                                    case 2:
                                        var spline = new Spline(reader);
                                        break;

                                    case 3:
                                        var mesh = new NavMesh(reader);
                                        break;
                                    }
                                }
                            }
                            catch
                            {
                                hasError = true;
                                stream.Seek(0, SeekOrigin.Begin);
                                using (var reader = new EndianBinaryReader(stream, container.Endian))
                                {
                                    SaveErrorShape(
                                        version,
                                        container.Header.Version,
                                        filePath,
                                        new RawNamedShapeObject(valueTuple.Entity.Type, reader, container.Endian)
                                        );
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    hasError = true;
                }
            }
                );

            Assert.IsFalse(hasError);
        }
Beispiel #4
0
        public void ReadAllShapes()
        {
            var versions = new[]
            {
                FarmSimulatorVersion.FarmingSimulator2013,
                FarmSimulatorVersion.FarmingSimulator2015,
                FarmSimulatorVersion.FarmingSimulator2017,
                FarmSimulatorVersion.FarmingSimulator2019,
            };
            var hasError = false;

            versions
            .Select(version => (Version: version, Path: GamePaths.GetGamePath(version)))
            .Where(v => v.Path != null)
            .SelectMany(
                v =>
            {
                var shapeFiles = Directory.GetFiles(v.Path, $"*{GameConstants.SchapesFileExtension}", SearchOption.AllDirectories);
                return(shapeFiles.Select(file => (Version: v.Version, FilePath: file)));
            }
                )
            .AsParallel()
            .WithDegreeOfParallelism(Environment.ProcessorCount)
            .ForEach(
                v =>
            {
                try
                {
                    var container = new FileContainer(v.FilePath);
                    var entities  = container.GetEntities();
                    foreach (var valueTuple in container.ReadRawData(entities))
                    {
                        using (var stream = new MemoryStream(valueTuple.RawData))
                        {
                            try
                            {
                                using (var reader = new EndianBinaryReader(stream, container.Endian, true))
                                {
                                    switch (valueTuple.Entity.Type)
                                    {
                                    case 1:
                                        var shape = new Shape(reader, container.Header.Version);
                                        break;

                                    case 2:
                                        var spline = new Spline(reader);
                                        break;

                                    case 3:
                                        var mesh = new NavMesh(reader);
                                        break;
                                    }
                                }
                            }
                            catch
                            {
                                hasError = true;
                                stream.Seek(0, SeekOrigin.Begin);
                                using (var reader = new EndianBinaryReader(stream, container.Endian))
                                {
                                    SaveErrorShape(
                                        v.Version,
                                        v.FilePath,
                                        new RawNamedShapeObject(valueTuple.Entity.Type, reader, container.Endian)
                                        );
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    hasError = true;
                }
            }
                );

            Assert.False(hasError);
        }