Example #1
0
        private async Task GetDependenciesInternal()
        {
            /*bool loftsChanged = GetDirectoryChanged(Path.Combine(RoutePath, "Networks", "Loft Tiles"), ref SavedRoute.LoftLastWrite, ref SavedRoute.LoftChecksum);
             * bool roadsChanged = GetDirectoryChanged(Path.Combine(RoutePath, "Networks", "Road Tiles"), ref SavedRoute.RoadLastWrite, ref SavedRoute.LoftChecksum);
             * bool tracksChanged = GetDirectoryChanged(Path.Combine(RoutePath, "Networks", "Track Tiles"), ref SavedRoute.TrackLastWrite, ref SavedRoute.TrackChecksum);
             * bool sceneryChanged = GetDirectoryChanged(Path.Combine(RoutePath, "Scenery"), ref SavedRoute.SceneryLastWrite, ref SavedRoute.SceneryChecksum);
             * bool rpChanged = GetFileChanged(Path.Combine(RoutePath, "RouteProperties.xml"), ref SavedRoute.RoutePropertiesLastWrite, ref SavedRoute.RoutePropertiesChecksum);
             * bool scenariosChanged = GetDirectoryChanged(Path.Combine(RoutePath, "Scenarios"), ref SavedRoute.ScenariosLastWrite, ref SavedRoute.ScenariosChecksum);*/

            lock (Dependencies)
            {
                if (SavedRoute.Dependencies != null)
                {
                    lock (Dependencies)
                        Dependencies.UnionWith(SavedRoute.Dependencies);
                }

                if (SavedRoute.ScenarioDeps != null)
                {
                    lock (ScenarioDeps)
                        ScenarioDeps.UnionWith(SavedRoute.ScenarioDeps);
                }
            }

            if (LoftsChanged || RoadsChanged || TracksChanged || SceneryChanged || RPchanged || ScenariosChanged)
            {
                Task n1   = null;
                Task n2   = null;
                Task n3   = null;
                Task s    = null;
                Task t    = null;
                Task prop = null;

                if (RPchanged)
                {
                    prop = GetSingleFileDependencies(Utils.FindFile(RoutePath, "RouteProperties.*"));
                }

                Parallel.ForEach(Directory.GetDirectories(RoutePath), dir =>
                {
                    switch (Path.GetFileName(dir).ToLower())
                    {
                    case "networks":
                        // Foreach all network directories
                        Parallel.ForEach(Directory.GetDirectories(dir), network_dir =>
                        {
                            switch (Path.GetFileName(network_dir).ToLower())
                            {
                            case "loft tiles":
                                if (LoftsChanged)
                                {
                                    n1 = GetTilesDependencies(network_dir);
                                }
                                break;

                            case "road tiles":
                                if (RoadsChanged)
                                {
                                    n2 = GetTilesDependencies(network_dir);
                                }
                                break;

                            case "track tiles":
                                if (TracksChanged)
                                {
                                    n3 = GetTilesDependencies(network_dir);
                                }
                                break;
                            }
                        });
                        break;

                    case "scenarios":
                        if (ScenariosChanged)
                        {
                            s = GetScenariosDependencies(dir);
                        }
                        break;

                    case "scenery":
                        if (SceneryChanged)
                        {
                            t = GetTilesDependencies(dir);
                        }
                        break;
                    }
                });

                if (n1 != null)
                {
                    await n1;
                }
                if (n2 != null)
                {
                    await n2;
                }
                if (n3 != null)
                {
                    await n3;
                }
                if (s != null)
                {
                    await s;
                }
                if (t != null)
                {
                    await t;
                }
                if (prop != null)
                {
                    await prop;
                }
            }

            SavedRoute.Dependencies = Dependencies;
            SavedRoute.ScenarioDeps = ScenarioDeps;

            Thread tt = new Thread(() =>
            {
                RouteSaving?.Invoke(false);
                Adapter.SaveRoute(SavedRoute);
                Adapter.FlushToFile();
                RouteSaving?.Invoke(true);
            });

            tt.Start();
        }
        private async Task GetDependenciesInternal()
        {
            bool loftsChanged     = GetDirectoryMD5(Path.Combine(RoutePath, "Networks", "Loft Tiles")) != SavedRoute.LoftChecksum;
            bool roadsChanged     = GetDirectoryMD5(Path.Combine(RoutePath, "Networks", "Road Tiles")) != SavedRoute.RoadChecksum;
            bool tracksChanged    = GetDirectoryMD5(Path.Combine(RoutePath, "Networks", "Track Tiles")) != SavedRoute.TrackChecksum;
            bool sceneryChanged   = GetDirectoryMD5(Path.Combine(RoutePath, "Scenery")) != SavedRoute.SceneryChecksum;
            bool rpChanged        = GetFileMD5(Path.Combine(RoutePath, "RouteProperties.xml")) != SavedRoute.RoutePropertiesChecksum;
            bool scenariosChanged = GetDirectoryMD5(Path.Combine(RoutePath, "Scenarios")) != SavedRoute.ScenariosChecksum;

            Task md5 = ComputeChecksums();

            lock (Dependencies)
            {
                if (SavedRoute.Dependencies != null)
                {
                    lock (Dependencies)
                        Dependencies.UnionWith(SavedRoute.Dependencies);
                }

                if (SavedRoute.ScenarioDeps != null)
                {
                    lock (ScenarioDeps)
                        ScenarioDeps.UnionWith(SavedRoute.ScenarioDeps);
                }
            }

            if (loftsChanged || roadsChanged || tracksChanged || sceneryChanged || rpChanged || scenariosChanged)
            {
                Task n1 = null;
                Task n2 = null;
                Task n3 = null;
                Task s  = null;
                Task t  = null;
                Task <List <string> > prop = null;

                if (rpChanged)
                {
                    prop = GetRoutePropertiesDependencies(Path.Combine(RoutePath, "RouteProperties.xml"));
                }

                Parallel.ForEach(Directory.GetDirectories(RoutePath), dir =>
                {
                    switch (Path.GetFileName(dir).ToLower())
                    {
                    case "networks":
                        // Foreach all network directories
                        Parallel.ForEach(Directory.GetDirectories(dir), network_dir =>
                        {
                            switch (Path.GetFileName(network_dir).ToLower())
                            {
                            case "loft tiles":
                                if (loftsChanged)
                                {
                                    n1 = GetNetworkDependencies(network_dir);
                                }
                                break;

                            case "road tiles":
                                if (roadsChanged)
                                {
                                    n2 = GetNetworkDependencies(network_dir);
                                }
                                break;

                            case "track tiles":
                                if (tracksChanged)
                                {
                                    n3 = GetNetworkDependencies(network_dir);
                                }
                                break;
                            }
                        });
                        break;

                    case "scenarios":
                        if (scenariosChanged)
                        {
                            s = GetScenariosDependencies(dir);
                        }
                        break;

                    case "scenery":
                        if (sceneryChanged)
                        {
                            t = GetSceneryDependencies(dir);
                        }
                        break;
                    }
                });

                if (n1 != null)
                {
                    await n1;
                }
                if (n2 != null)
                {
                    await n2;
                }
                if (n3 != null)
                {
                    await n3;
                }
                if (s != null)
                {
                    await s;
                }
                if (t != null)
                {
                    await t;
                }

                List <string> routeProperties = new List <string>();
                if (prop != null)
                {
                    routeProperties = await prop;

                    lock (Dependencies)
                        Dependencies.UnionWith(routeProperties);
                }
            }

            await md5;

            SavedRoute.Dependencies = Dependencies;
            SavedRoute.ScenarioDeps = ScenarioDeps;

            Thread tt = new Thread(() =>
            {
                RouteSaving?.Invoke(false);
                Adapter.SaveRoute(SavedRoute);
                Adapter.FlushToFile();
                RouteSaving?.Invoke(true);
            });

            tt.Start();
        }