Beispiel #1
0
    private IEnumerator InitLayers()
    {
        if (dataLayers == null)
        {
            CloseProgressDialog();
            yield break;
        }

#if !UNITY_WEBGL
        if (!Directory.Exists(Paths.Sites))
        {
            Debug.LogError("Data path '" + Paths.Sites + "' doesn't exist. Trying to create it.");
            CloseProgressDialog();
            Directory.CreateDirectory(Paths.Sites);
            yield break;
        }
#endif


#if UNITY_WEBGL
        // Show progress message
        progressDialog.SetMessage(Translator.Get("Downloading data") + "...");
        progressDialog.SetProgress(0);

        yield return(PatchDataIO.StartReadingHeaders());

        progressDialog.SetProgress(0.5f);
        yield return(null);

        var paths = new List <string>();
        yield return(FileRequest.GetText(Paths.DataWebDBPatches, (tr) =>
        {
            string line;
            while ((line = tr.ReadLine()) != null)
            {
                paths.Add(line);
            }
        }));
#else
        // Find valid data directories
        var paths = GetDataDirectories();
#endif

        // Show progress message
        progressDialog.SetMessage(Translator.Get("Loading") + " ...");
        progressDialog.SetProgress(0);

        yield return(null);

        // Count the layers
        int layerCount = 0;
        foreach (var group in groups)
        {
            foreach (var layer in group.layers)
            {
                layerCount++;
            }
        }

        // Rebuild the UI
        dataLayers.Show(false);

        // Create the patches/sites for each layer
        float index         = 0;
        float time          = 0;
        float invLayerCount = 1f / layerCount;
        var   bounds        = map.MapCoordBounds;
        foreach (var group in groups)
        {
            var groupController = dataLayers.AddLayerGroup(group.name);
            foreach (var layer in group.layers)
            {
                dataLayers.AddLayer(layer, groupController);

                List <string> patchFiles = null;
                var           filesIt    = layer.GetPatchFiles(paths, (pf) => patchFiles = pf);
                if (filesIt.MoveNext())
                {
                    do
                    {
                        yield return(null);
                    } while (filesIt.MoveNext());
                    time = Time.realtimeSinceStartup + MaxProcessingTimePerFrame;
                }
#if UNITY_EDITOR
                if (findUnusedLayers && patchFiles.Count == 0)
                {
                    Debug.LogWarning("Layer " + layer.Name + " doesn't have any files.");
                }
#endif

                if (patchFiles.Count > 0)
                {
                    int   patchCount    = 0;
                    float invPatchCount = 1f / patchFiles.Count;
                    var   patches       = layer.CreatePatches(patchFiles);
                    while (patches.MoveNext())
                    {
                        patchCount++;
                        progressDialog.SetProgress((index + (patchCount * invPatchCount)) * invLayerCount);
                        if (patches.RunThruChildren())
                        {
                            do
                            {
                                if (Time.realtimeSinceStartup > time)
                                {
                                    yield return(null);

                                    time = Time.realtimeSinceStartup + MaxProcessingTimePerFrame;
                                }
                            }while (patches.RunThru());
                        }
                        else if (Time.realtimeSinceStartup > time)
                        {
                            yield return(null);

                            time = Time.realtimeSinceStartup + MaxProcessingTimePerFrame;
                        }
                    }
                }
                else if (Time.realtimeSinceStartup > time)
                {
                    yield return(null);

                    time = Time.realtimeSinceStartup + MaxProcessingTimePerFrame;
                }
                index++;
            }
        }

        PatchDataIO.StopParsingThread();

#if UNITY_EDITOR && !UNITY_WEBGL
        if (findUnusedPatches)
        {
            var extensions = new string[] { "*.csv", "*.bin" };
            var dirs       = GetDataDirectories();
            foreach (var dir in dirs)
            {
                foreach (var ext in extensions)
                {
                    var files = Directory.GetFiles(dir, ext);
                    foreach (var file in files)
                    {
                        if (Path.GetFileName(file).StartsWith("_"))
                        {
                            continue;
                        }
                        var layerName = Patch.GetFileNameLayer(file);
                        if (!dataLayers.HasLayer(layerName))
                        {
                            Debug.LogWarning("Patch is being ignored: " + file);
                        }
                    }
                }
            }
        }
#endif

        dataLayers.Show(true);
        yield return(null);

        // Update DataLayers UI (activate those layers that have sites/patches within view bounds)
        dataLayers.UpdateLayers();

        // Hide Message
        CloseProgressDialog();

        // Clean up
#if UNITY_WEBGL
        PatchDataIO.FinishReadingHeaders();
#endif

        OnDataLoaded?.Invoke();
    }
Beispiel #2
0
    private IEnumerator InitLayers()
    {
        if (dataLayers == null)
        {
            messageController.Show(false);
            yield break;
        }

#if !UNITY_WEBGL
        if (!Directory.Exists(Paths.Sites))
        {
            Debug.LogError("Data path '" + Paths.Sites + "' doesn't exist");
            messageController.Show(false);
            yield break;
        }
#endif

        // Show Message
        messageController.Show(true);
        messageController.SetProgress(0);

#if UNITY_WEBGL
        messageController.SetMessage("Downloading database ...");

        yield return(PatchDataIO.StartReadingHeaders());

        messageController.SetProgress(0.5f);
        yield return(null);

        allPatchFiles.Clear();
        yield return(FileRequest.GetText(Paths.DataWebDBPatches, (tr) =>
        {
            string line;
            while ((line = tr.ReadLine()) != null)
            {
                allPatchFiles.Add(line);
            }
        }));
#else
        // Find valid data directories
        InitValidDataDirs();
#endif

        messageController.SetMessage("Initializing database ...");
        messageController.SetProgress(0);
        yield return(null);

        // Rebuild the UI
        int groupCount = 0;
        int layerCount = 0;
        dataLayers.Show(false);
        foreach (var group in groups)
        {
            var groupController = dataLayers.AddLayerGroup(group.name);
            foreach (var layer in group.layers)
            {
                dataLayers.AddLayer(layer, groupController);
                layer.BuildFileList();
                layerCount++;
            }
            groupCount++;
        }
        dataLayers.Show(true);

        yield return(null);

        // Create the patches/sites for each layer
        float index         = 0;
        int   counter       = 0;
        float invLayerCount = 1f / layerCount;
        var   bounds        = map.MapCoordBounds;
        foreach (var group in groups)
        {
            foreach (var layer in group.layers)
            {
                if (layer.PatchCount > 0)
                {
                    int   patchCount    = 0;
                    float invPatchCount = 1f / layer.PatchCount;
                    var   patches       = layer.CreatePatches();
                    while (patches.MoveNext())
                    {
                        patchCount++;
                        messageController.SetProgress((index + (patchCount * invPatchCount)) * invLayerCount);
                        while (patches.RunThruChildren())
                        {
                            counter = 0;
                            yield return(null);
                        }
                        if (++counter % 50 == 0)
                        {
                            yield return(null);
                        }
                    }
                }

#if SAFETY_CHECK
                if (layers.ContainsKey(layer.name))
                {
                    Debug.LogError(layer.name + " layer already exists. Looks like a duplicate line in layers.csv");
                }
                else
#endif
                layers.Add(layer.name, layer);

                index++;
            }
        }

        CreateSitesList();

        // Update DataLayers UI (activate those layers that have sites/patches within view bounds)
        dataLayers.UpdateLayers();

        // Hide Message
        messageController.Show(false);

        // Clean up the cached files & directories
#if UNITY_WEBGL
        PatchDataIO.FinishReadingHeaders();
        allPatchFiles.Clear();
        allPatchFiles = null;
#else
        DataDirs = null;
#endif

        if (OnDataLoaded != null)
        {
            OnDataLoaded();
        }
    }