示例#1
0
        public void OnApplicationStart()
        {
            SceneManager.activeSceneChanged += SceneManager_activeSceneChanged;
            SceneManager.sceneLoaded        += SceneManager_sceneLoaded;

            var harmony = HarmonyInstance.Create("com.andruzzzhka.MapLoader");

            harmony.PatchAll();

            HashSystem.Load();
            SaveSystem.Load();
        }
示例#2
0
        public async void PreLoadLevels()
        {
            if (!isPreloadingLevels)
            {
                isPreloadingLevels = true;
                preloadingProgress = 0f;

                loadedLevels.Clear();

                Directory.CreateDirectory(customLevelsPath);

                var allLevels = GetAllLevels(customLevelsPath);

                Console.WriteLine($"Found {allLevels.Count} levels!");

                int counter     = 0;
                int totalLevels = allLevels.Count;

                foreach (var path in allLevels)
                {
                    var result = await LoadLevelInfoAsync(path);

                    if (result.success)
                    {
                        await HashSystem.GetHashForLevel(result.info);

                        loadedLevels.Add(result.info);
                    }

                    counter++;
                    preloadingProgress = ((float)counter) / totalLevels;
                }

                isPreloadingLevels = false;
            }
        }
        private IEnumerator LoadLevelCoroutine(LevelInfo info)
        {
            isLoadingLevel  = true;
            loadingProgress = 0f;
            currentLevel    = info;
            currentStage    = 0;

            if (_loadedAssetBundles.Count > 0)
            {
                UnloadAssets();
            }

            ModsCheckResult check = info.metadata.CheckRequiredMods();

            if (!check.satisfied)
            {
                Console.WriteLine("Unable to load level! Mods requirements not satisfied:");
                if (check.missingMods.Count > 0)
                {
                    Console.WriteLine("Missing mods:");
                    foreach (var missingMod in check.missingMods)
                    {
                        Console.WriteLine(missingMod);
                    }
                }
                if (check.oudatedMods.Count > 0)
                {
                    Console.WriteLine("Outdated mods:");
                    foreach (var outdatedMod in check.oudatedMods)
                    {
                        Console.WriteLine(outdatedMod);
                    }
                }

                loadingProgress = 0f;
                isLoadingLevel  = false;

                yield break;
            }

            if (_loadingCamera == null)
            {
                CreateLoadingCamera(currentLevel.cover);
            }

            var task = HashSystem.RecalculateHashForLevel(info);

            yield return(new WaitUntil(() => task.IsCompleted));

            loadingProgress = 0.05f;

            FileStream stream = File.OpenRead(info.path);

            stream.Position = info.assetsArchiveOffset;

            ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Read);

            var assetsManifestRequest = AssetBundle.LoadFromStreamAsync(archive.GetEntry(info.metadata.levelId).ExtractToStream());

            while (!assetsManifestRequest.isDone)
            {
                loadingProgress = 0.05f + assetsManifestRequest.progress * 0.05f;

                yield return(null);
            }
            loadingProgress = 0.1f;

            if (assetsManifestRequest.assetBundle == null)
            {
                Console.WriteLine("Failed to load AssetBundle!");
                loadingProgress = 0f;
                isLoadingLevel  = false;
                if (_loadingCamera != null)
                {
                    Destroy(_loadingCamera.gameObject);
                }
                yield break;
            }
            _loadedAssetBundles.Add(assetsManifestRequest.assetBundle);

            var manifest = assetsManifestRequest.assetBundle.LoadAsset <AssetBundleManifest>("assetbundlemanifest");

            List <string> foundScenes = new List <string>();

            int totalBundles  = manifest.GetAllAssetBundles().Length;
            int currentBundle = 0;

            foreach (var assetBundleName in manifest.GetAllAssetBundles())
            {
                var assetBundleRequest = AssetBundle.LoadFromStreamAsync(archive.Entries.First(x => x.Name.ToLower() == assetBundleName).ExtractToStream());

                while (!assetBundleRequest.isDone)
                {
                    loadingProgress = 0.1f + ((currentBundle + assetBundleRequest.progress) / totalBundles * 0.15f);

                    yield return(null);
                }

                if (assetBundleRequest.assetBundle != null)
                {
                    _loadedAssetBundles.Add(assetBundleRequest.assetBundle);

                    var scenes = assetBundleRequest.assetBundle.GetAllScenePaths();

                    if (scenes.Length != 0)
                    {
                        foundScenes.AddRange(scenes);
                        Console.WriteLine($"Found {scenes.Length} scenes!");
                    }
                }
            }

            if (foundScenes.Count == 0)
            {
                Console.WriteLine("No scenes found!");
                loadingProgress = 0f;
                isLoadingLevel  = false;
                if (_loadingCamera != null)
                {
                    Destroy(_loadingCamera.gameObject);
                }
                yield break;
            }

            Console.WriteLine("Loading scenes...");

            var sceneLoadingOperation = SceneManager.LoadSceneAsync(info.metadata.stages[0], LoadSceneMode.Single);

            while (!sceneLoadingOperation.isDone)
            {
                loadingProgress = 0.25f + sceneLoadingOperation.progress * 0.5f;
                yield return(null);
            }

            Console.WriteLine("Loaded custom scene!");

            SpawnPrefabs();
        }