Example #1
0
    public IEnumerator InitCoroutine()
    {
        WorldSetup worldSetup = null;

        if (!World.CanLoadFromUrl())
        {
            object[] size = new object[] { "Generating procedural map of size ", World.Size, " with seed ", World.Seed };
            UnityEngine.Debug.Log(string.Concat(size));
        }
        else
        {
            UnityEngine.Debug.Log(string.Concat("Loading custom map from ", World.Url));
        }
        ProceduralComponent[] componentsInChildren = worldSetup.GetComponentsInChildren <ProceduralComponent>(true);
        Timing timing = Timing.Start("Downloading World");

        if (World.Procedural && !World.CanLoadFromDisk() && World.CanLoadFromUrl())
        {
            LoadingScreen.Update("DOWNLOADING WORLD");
            yield return(CoroutineEx.waitForEndOfFrame);

            yield return(CoroutineEx.waitForEndOfFrame);

            yield return(CoroutineEx.waitForEndOfFrame);

            UnityWebRequest downloadHandlerBuffer = UnityWebRequest.Get(World.Url);
            downloadHandlerBuffer.downloadHandler = new DownloadHandlerBuffer();
            downloadHandlerBuffer.Send();
            while (!downloadHandlerBuffer.isDone)
            {
                float single = downloadHandlerBuffer.downloadProgress * 100f;
                LoadingScreen.Update(string.Concat("DOWNLOADING WORLD ", single.ToString("0.0"), "%"));
                yield return(CoroutineEx.waitForEndOfFrame);
            }
            if (downloadHandlerBuffer.isHttpError || downloadHandlerBuffer.isNetworkError)
            {
                string[] name = new string[] { "Couldn't Download Level: ", World.Name, " (", downloadHandlerBuffer.error, ")" };
                worldSetup.CancelSetup(string.Concat(name));
            }
            else
            {
                File.WriteAllBytes(string.Concat(World.MapFolderName, "/", World.MapFileName), downloadHandlerBuffer.downloadHandler.data);
            }
            downloadHandlerBuffer = null;
        }
        timing.End();
        Timing timing1 = Timing.Start("Loading World");

        if (World.Procedural && World.CanLoadFromDisk())
        {
            LoadingScreen.Update("LOADING WORLD");
            yield return(CoroutineEx.waitForEndOfFrame);

            yield return(CoroutineEx.waitForEndOfFrame);

            yield return(CoroutineEx.waitForEndOfFrame);

            World.Serialization.Load(string.Concat(World.MapFolderName, "/", World.MapFileName));
            World.Cached = true;
        }
        timing1.End();
        if (World.Cached && 9 != World.Serialization.Version)
        {
            object[] version = new object[] { "World cache version mismatch: ", (uint)9, " != ", World.Serialization.Version };
            UnityEngine.Debug.LogWarning(string.Concat(version));
            World.Serialization.Clear();
            World.Cached = false;
            if (World.CanLoadFromUrl())
            {
                worldSetup.CancelSetup(string.Concat("World File Outdated: ", World.Name));
            }
        }
        if (World.Cached && string.IsNullOrEmpty(World.Checksum))
        {
            World.Checksum = World.Serialization.Checksum;
        }
        if (World.Cached)
        {
            World.InitSize(World.Serialization.world.size);
        }
        if (worldSetup.terrain)
        {
            TerrainGenerator terrainGenerator = worldSetup.terrain.GetComponent <TerrainGenerator>();
            if (terrainGenerator)
            {
                worldSetup.terrain     = terrainGenerator.CreateTerrain();
                worldSetup.terrainMeta = worldSetup.terrain.GetComponent <TerrainMeta>();
                worldSetup.terrainMeta.Init(null, null);
                worldSetup.terrainMeta.SetupComponents();
                worldSetup.CreateObject(worldSetup.decorPrefab);
                worldSetup.CreateObject(worldSetup.grassPrefab);
                worldSetup.CreateObject(worldSetup.spawnPrefab);
            }
        }
        Timing timing2 = Timing.Start("Spawning World");

        if (World.Cached)
        {
            LoadingScreen.Update("SPAWNING WORLD");
            yield return(CoroutineEx.waitForEndOfFrame);

            yield return(CoroutineEx.waitForEndOfFrame);

            yield return(CoroutineEx.waitForEndOfFrame);

            TerrainMeta.HeightMap.FromByteArray(World.GetMap("terrain"));
            TerrainMeta.SplatMap.FromByteArray(World.GetMap("splat"));
            TerrainMeta.BiomeMap.FromByteArray(World.GetMap("biome"));
            TerrainMeta.TopologyMap.FromByteArray(World.GetMap("topology"));
            TerrainMeta.AlphaMap.FromByteArray(World.GetMap("alpha"));
            TerrainMeta.WaterMap.FromByteArray(World.GetMap("water"));
            IEnumerator enumerator = World.Spawn(0.2f, (string str) => LoadingScreen.Update(str));
            while (enumerator.MoveNext())
            {
                yield return(enumerator.Current);
            }
            TerrainMeta.Path.Clear();
            TerrainMeta.Path.Roads.AddRange(World.GetPaths("Road"));
            TerrainMeta.Path.Rivers.AddRange(World.GetPaths("River"));
            TerrainMeta.Path.Powerlines.AddRange(World.GetPaths("Powerline"));
            enumerator = null;
        }
        timing2.End();
        Timing timing3 = Timing.Start("Processing World");

        if (componentsInChildren.Length != 0)
        {
            for (int i = 0; i < (int)componentsInChildren.Length; i++)
            {
                ProceduralComponent proceduralComponent = componentsInChildren[i];
                if (proceduralComponent && proceduralComponent.ShouldRun())
                {
                    uint num = (uint)((ulong)World.Seed + (long)i);
                    LoadingScreen.Update(proceduralComponent.Description.ToUpper());
                    yield return(CoroutineEx.waitForEndOfFrame);

                    yield return(CoroutineEx.waitForEndOfFrame);

                    yield return(CoroutineEx.waitForEndOfFrame);

                    Timing timing4 = Timing.Start(proceduralComponent.Description);
                    if (proceduralComponent)
                    {
                        proceduralComponent.Process(num);
                    }
                    timing4.End();
                    proceduralComponent = null;
                }
            }
        }
        timing3.End();
        Timing timing5 = Timing.Start("Saving World");

        if (ConVar.World.cache && World.Procedural && !World.Cached)
        {
            LoadingScreen.Update("SAVING WORLD");
            yield return(CoroutineEx.waitForEndOfFrame);

            yield return(CoroutineEx.waitForEndOfFrame);

            yield return(CoroutineEx.waitForEndOfFrame);

            World.Serialization.world.size = World.Size;
            World.AddPaths(TerrainMeta.Path.Roads);
            World.AddPaths(TerrainMeta.Path.Rivers);
            World.AddPaths(TerrainMeta.Path.Powerlines);
            World.Serialization.Save(string.Concat(World.MapFolderName, "/", World.MapFileName));
        }
        timing5.End();
        Timing timing6 = Timing.Start("Calculating Checksum");

        if (string.IsNullOrEmpty(World.Serialization.Checksum))
        {
            LoadingScreen.Update("CALCULATING CHECKSUM");
            yield return(CoroutineEx.waitForEndOfFrame);

            yield return(CoroutineEx.waitForEndOfFrame);

            yield return(CoroutineEx.waitForEndOfFrame);

            World.Serialization.CalculateChecksum();
        }
        timing6.End();
        if (string.IsNullOrEmpty(World.Checksum))
        {
            World.Checksum = World.Serialization.Checksum;
        }
        Timing timing7 = Timing.Start("Ocean Patrol Paths");

        LoadingScreen.Update("OCEAN PATROL PATHS");
        yield return(CoroutineEx.waitForEndOfFrame);

        yield return(CoroutineEx.waitForEndOfFrame);

        yield return(CoroutineEx.waitForEndOfFrame);

        if (!BaseBoat.generate_paths || !(TerrainMeta.Path != null))
        {
            UnityEngine.Debug.Log("Skipping ocean patrol paths, baseboat.generate_paths == false");
        }
        else
        {
            TerrainMeta.Path.OceanPatrolFar = BaseBoat.GenerateOceanPatrolPath(200f, 8f);
        }
        timing7.End();
        Timing timing8 = Timing.Start("Finalizing World");

        LoadingScreen.Update("FINALIZING WORLD");
        yield return(CoroutineEx.waitForEndOfFrame);

        yield return(CoroutineEx.waitForEndOfFrame);

        yield return(CoroutineEx.waitForEndOfFrame);

        if (worldSetup.terrainMeta)
        {
            worldSetup.terrainMeta.BindShaderProperties();
            worldSetup.terrainMeta.PostSetupComponents();
            TerrainMargin.Create();
        }
        World.Serialization.Clear();
        timing8.End();
        LoadingScreen.Update("DONE");
        yield return(CoroutineEx.waitForEndOfFrame);

        yield return(CoroutineEx.waitForEndOfFrame);

        yield return(CoroutineEx.waitForEndOfFrame);

        if (worldSetup)
        {
            GameManager.Destroy(worldSetup.gameObject, 0f);
        }
    }
Example #2
0
    public IEnumerator InitCoroutine()
    {
        WorldSetup worldSetup = this;

        if (World.CanLoadFromUrl())
        {
            Debug.Log((object)("Loading custom map from " + World.Url));
        }
        else
        {
            Debug.Log((object)("Generating procedural map of size " + (object)World.Size + " with seed " + (object)World.Seed));
        }
        ProceduralComponent[] components = (ProceduralComponent[])((Component)worldSetup).GetComponentsInChildren <ProceduralComponent>(true);
        Timing downloadTimer             = Timing.Start("Downloading World");

        if (World.Procedural && !World.CanLoadFromDisk() && World.CanLoadFromUrl())
        {
            LoadingScreen.Update("DOWNLOADING WORLD");
            yield return((object)CoroutineEx.waitForEndOfFrame);

            yield return((object)CoroutineEx.waitForEndOfFrame);

            yield return((object)CoroutineEx.waitForEndOfFrame);

            UnityWebRequest request = UnityWebRequest.Get(World.Url);
            request.set_downloadHandler((DownloadHandler) new DownloadHandlerBuffer());
            request.Send();
            while (!request.get_isDone())
            {
                LoadingScreen.Update("DOWNLOADING WORLD " + (request.get_downloadProgress() * 100f).ToString("0.0") + "%");
                yield return((object)CoroutineEx.waitForEndOfFrame);
            }
            if (!request.get_isHttpError() && !request.get_isNetworkError())
            {
                File.WriteAllBytes(World.MapFolderName + "/" + World.MapFileName, request.get_downloadHandler().get_data());
            }
            else
            {
                worldSetup.CancelSetup("Couldn't Download Level: " + World.Name + " (" + request.get_error() + ")");
            }
            request = (UnityWebRequest)null;
        }
        downloadTimer.End();
        Timing loadTimer = Timing.Start("Loading World");

        if (World.Procedural && World.CanLoadFromDisk())
        {
            LoadingScreen.Update("LOADING WORLD");
            yield return((object)CoroutineEx.waitForEndOfFrame);

            yield return((object)CoroutineEx.waitForEndOfFrame);

            yield return((object)CoroutineEx.waitForEndOfFrame);

            World.Serialization.Load(World.MapFolderName + "/" + World.MapFileName);
            World.Cached = true;
        }
        loadTimer.End();
        if (World.Cached && 8U != World.Serialization.get_Version())
        {
            Debug.LogWarning((object)("World cache version mismatch: " + (object)8U + " != " + (object)World.Serialization.get_Version()));
            World.Serialization.Clear();
            World.Cached = false;
            if (World.CanLoadFromUrl())
            {
                worldSetup.CancelSetup("World File Outdated: " + World.Name);
            }
        }
        if (World.Cached && string.IsNullOrEmpty(World.Checksum))
        {
            World.Checksum = World.Serialization.get_Checksum();
        }
        if (World.Cached)
        {
            World.InitSize((uint)((WorldSerialization.WorldData)World.Serialization.world).size);
        }
        if (Object.op_Implicit((Object)worldSetup.terrain))
        {
            TerrainGenerator component = (TerrainGenerator)worldSetup.terrain.GetComponent <TerrainGenerator>();
            if (Object.op_Implicit((Object)component))
            {
                worldSetup.terrain     = component.CreateTerrain();
                worldSetup.terrainMeta = (TerrainMeta)worldSetup.terrain.GetComponent <TerrainMeta>();
                worldSetup.terrainMeta.Init((Terrain)null, (TerrainConfig)null);
                worldSetup.terrainMeta.SetupComponents();
                worldSetup.CreateObject(worldSetup.decorPrefab);
                worldSetup.CreateObject(worldSetup.grassPrefab);
                worldSetup.CreateObject(worldSetup.spawnPrefab);
            }
        }
        Timing spawnTimer = Timing.Start("Spawning World");

        if (World.Cached)
        {
            LoadingScreen.Update("SPAWNING WORLD");
            yield return((object)CoroutineEx.waitForEndOfFrame);

            yield return((object)CoroutineEx.waitForEndOfFrame);

            yield return((object)CoroutineEx.waitForEndOfFrame);

            TerrainMeta.HeightMap.FromByteArray(World.GetMap("terrain"));
            TerrainMeta.SplatMap.FromByteArray(World.GetMap("splat"));
            TerrainMeta.BiomeMap.FromByteArray(World.GetMap("biome"));
            TerrainMeta.TopologyMap.FromByteArray(World.GetMap("topology"));
            TerrainMeta.AlphaMap.FromByteArray(World.GetMap("alpha"));
            TerrainMeta.WaterMap.FromByteArray(World.GetMap("water"));
            IEnumerator worldSpawn = World.Spawn(0.2f, (Action <string>)(str => LoadingScreen.Update(str)));
            while (worldSpawn.MoveNext())
            {
                yield return(worldSpawn.Current);
            }
            TerrainMeta.Path.Clear();
            TerrainMeta.Path.Roads.AddRange(World.GetPaths("Road"));
            TerrainMeta.Path.Rivers.AddRange(World.GetPaths("River"));
            TerrainMeta.Path.Powerlines.AddRange(World.GetPaths("Powerline"));
            worldSpawn = (IEnumerator)null;
        }
        spawnTimer.End();
        Timing procgenTimer = Timing.Start("Processing World");

        if (components.Length != 0)
        {
            for (int i = 0; i < components.Length; ++i)
            {
                ProceduralComponent component = components[i];
                if (Object.op_Implicit((Object)component) && component.ShouldRun())
                {
                    uint seed = (uint)((ulong)World.Seed + (ulong)i);
                    LoadingScreen.Update(component.Description.ToUpper());
                    yield return((object)CoroutineEx.waitForEndOfFrame);

                    yield return((object)CoroutineEx.waitForEndOfFrame);

                    yield return((object)CoroutineEx.waitForEndOfFrame);

                    Timing timing = Timing.Start(component.Description);
                    if (Object.op_Implicit((Object)component))
                    {
                        component.Process(seed);
                    }
                    timing.End();
                    component = (ProceduralComponent)null;
                }
            }
        }
        procgenTimer.End();
        Timing saveTimer = Timing.Start("Saving World");

        if (ConVar.World.cache && World.Procedural && !World.Cached)
        {
            LoadingScreen.Update("SAVING WORLD");
            yield return((object)CoroutineEx.waitForEndOfFrame);

            yield return((object)CoroutineEx.waitForEndOfFrame);

            yield return((object)CoroutineEx.waitForEndOfFrame);

            ((WorldSerialization.WorldData)World.Serialization.world).size = (__Null)(int)World.Size;
            World.AddPaths((IEnumerable <PathList>)TerrainMeta.Path.Roads);
            World.AddPaths((IEnumerable <PathList>)TerrainMeta.Path.Rivers);
            World.AddPaths((IEnumerable <PathList>)TerrainMeta.Path.Powerlines);
            World.Serialization.Save(World.MapFolderName + "/" + World.MapFileName);
        }
        saveTimer.End();
        Timing checksumTimer = Timing.Start("Calculating Checksum");

        if (string.IsNullOrEmpty(World.Serialization.get_Checksum()))
        {
            LoadingScreen.Update("CALCULATING CHECKSUM");
            yield return((object)CoroutineEx.waitForEndOfFrame);

            yield return((object)CoroutineEx.waitForEndOfFrame);

            yield return((object)CoroutineEx.waitForEndOfFrame);

            World.Serialization.CalculateChecksum();
        }
        checksumTimer.End();
        if (string.IsNullOrEmpty(World.Checksum))
        {
            World.Checksum = World.Serialization.get_Checksum();
        }
        Timing oceanTimer = Timing.Start("Ocean Patrol Paths");

        LoadingScreen.Update("OCEAN PATROL PATHS");
        yield return((object)CoroutineEx.waitForEndOfFrame);

        yield return((object)CoroutineEx.waitForEndOfFrame);

        yield return((object)CoroutineEx.waitForEndOfFrame);

        if (BaseBoat.generate_paths && Object.op_Inequality((Object)TerrainMeta.Path, (Object)null))
        {
            TerrainMeta.Path.OceanPatrolFar = BaseBoat.GenerateOceanPatrolPath(200f, 8f);
        }
        else
        {
            Debug.Log((object)"Skipping ocean patrol paths, baseboat.generate_paths == false");
        }
        oceanTimer.End();
        Timing finalizeTimer = Timing.Start("Finalizing World");

        LoadingScreen.Update("FINALIZING WORLD");
        yield return((object)CoroutineEx.waitForEndOfFrame);

        yield return((object)CoroutineEx.waitForEndOfFrame);

        yield return((object)CoroutineEx.waitForEndOfFrame);

        if (Object.op_Implicit((Object)worldSetup.terrainMeta))
        {
            worldSetup.terrainMeta.BindShaderProperties();
            worldSetup.terrainMeta.PostSetupComponents();
            TerrainMargin.Create();
        }
        World.Serialization.Clear();
        finalizeTimer.End();
        LoadingScreen.Update("DONE");
        yield return((object)CoroutineEx.waitForEndOfFrame);

        yield return((object)CoroutineEx.waitForEndOfFrame);

        yield return((object)CoroutineEx.waitForEndOfFrame);

        if (Object.op_Implicit((Object)worldSetup))
        {
            GameManager.Destroy(((Component)worldSetup).get_gameObject(), 0.0f);
        }
    }