예제 #1
0
        /// <summary>
        ///     Loads base info about resources that game can load.
        /// </summary>
        public static bool LoadResourcesInfo()
        {
            var stopwatch = Stopwatch.StartNew();
            var cfg       = JEMGameResourcesConfiguration.Get();

            JEMLogger.Log($"New resources info load process has been started at directory {cfg.PackDirectory}");

            if (Directory.Exists(cfg.PackDirectory))
            {
                foreach (var file in Directory.GetFiles(cfg.PackDirectory))
                {
                    if (!file.EndsWith(cfg.PackExtension))
                    {
                        continue;
                    }

                    var fileStopWatch = Stopwatch.StartNew();
                    JEMLogger.Log($"Reading File -> {file}");
                    var packName        = JEMGameResourcesUtility.ResolveResourceNameFromPath(file);
                    var packDescription = string.Empty;
                    var packInfoFile    = JEMGameResourcesUtility.ResolveMapDescPath(file);
                    JEMLogger.Log($"Searching For -> {packInfoFile}");
                    var packInfoFileInfo = new FileInfo(packInfoFile);
                    if (packInfoFileInfo.Exists)
                    {
                        var lines = File.ReadAllLines(packInfoFile);
                        if (lines.Length != 0)
                        {
                            packName = JEMGameResourcesUtility.ResolveResourceName(lines[0]);
                            var list = new List <string>(lines);
                            list.RemoveAt(0);
                            packDescription = string.Join(Environment.NewLine, list.ToArray());
                        }
                    }

                    InternalGameResourcePacks.Add(new JEMGameResourcePack(packName, packDescription, file));

                    fileStopWatch.Stop();
                    JEMLogger.Log($"TOOK -> {fileStopWatch.Elapsed.TotalSeconds:0.0}s");
                }
            }
            else
            {
                JEMLogger.LogError(
                    $"Unable to load game resources info. Base resources patch `{cfg.PackDirectory}` not exist.");
                return(false);
            }

            stopwatch.Stop();
            JEMLogger.Log(
                $"Resources info loaded in -> {stopwatch.Elapsed.TotalSeconds:0.0}s. Total resources loaded: {InternalGameResourcePacks.Count}");

            return(true);
        }
예제 #2
0
        public IEnumerator InternalLoadResourcesAssets(string[] packList, bool async,
                                                       JEMGameResources.ResourcesLoaded loadedEvent,
                                                       JEMGameResources.ResourcesLoadProgress progressEvent)
        {
            if (packList == null || packList.Length == 0)
            {
                JEMLogger.LogWarning("New resources load process has been received but packList is null or empty.");
                yield return(new WaitForEndOfFrame());

                loadedEvent?.Invoke(JEMGameResources.ResourcesLoadingEffect.Success, new JEMGameResourcePack[0]);
                yield break;
            }

            var stopwatch = Stopwatch.StartNew();
            var cfg       = JEMGameResourcesConfiguration.Get();
            var packs     = new List <JEMGameResourcePack>();

            JEMLogger.Log($"New resources load process has been started at directory {cfg.PackDirectory}");

            if (Directory.Exists(cfg.PackDirectory))
            {
                foreach (var file in packList)
                {
                    yield return(new WaitForEndOfFrame());

                    var packData = JEMGameResources.GetPack(file);
                    if (packData.Equals(default(JEMGameResourcePack)))
                    {
                        JEMLogger.LogError(
                            $"System was unable to load pack {file}. Not exists in loaded list of resources.");
                        loadedEvent?.Invoke(JEMGameResources.ResourcesLoadingEffect.RegisteredResNotExist,
                                            new JEMGameResourcePack[0]);
                    }
                    else
                    {
                        if (packData.Loaded)
                        {
                            packs.Add(packData);
                            JEMLogger.Log($"Resource pack {file} exists and has been already loaded. Skipping!");
                            continue;
                        }

                        var packFile     = JEMGameResourcesUtility.ResolveMapPath(file);
                        var packFileInfo = new FileInfo(packFile);
                        if (packFileInfo.Exists)
                        {
                            JEMLogger.Log($"Loading -> {packFileInfo.FullName}");
                            var fileStopWatch = Stopwatch.StartNew();
                            if (async)
                            {
                                var assetBundlesRequest = AssetBundle.LoadFromFileAsync(packFileInfo.FullName);
                                yield return(assetBundlesRequest);

                                yield return(new WaitForEndOfFrame());

                                if (assetBundlesRequest == null || assetBundlesRequest.assetBundle == null)
                                {
                                    JEMLogger.LogError($"Resource {packFile} received but without assetBundle data.");
                                    loadedEvent?.Invoke(JEMGameResources.ResourcesLoadingEffect.RegisteredResNotExist,
                                                        new JEMGameResourcePack[0]);
                                    yield break;
                                }

                                packData.Bundle = assetBundlesRequest.assetBundle;
                                packData.Loaded = true;
                            }
                            else
                            {
                                var assetBundle = AssetBundle.LoadFromFile(packFileInfo.FullName);
                                if (assetBundle == null)
                                {
                                    JEMLogger.LogError($"Resource {packFile} received but without assetBundle data.");
                                    loadedEvent?.Invoke(JEMGameResources.ResourcesLoadingEffect.RegisteredResNotExist,
                                                        new JEMGameResourcePack[0]);
                                    yield break;
                                }

                                packData.Bundle = assetBundle;
                                packData.Loaded = true;
                            }

                            yield return(new WaitForEndOfFrame());

                            packs.Add(packData);
                            fileStopWatch.Stop();
                            JEMLogger.Log($"TOOK -> {fileStopWatch.Elapsed.TotalSeconds:0.0}s");
                        }
                        else
                        {
                            JEMLogger.LogError($"Unable to load game resources. Given file {packFile} not exist.");
                            loadedEvent?.Invoke(JEMGameResources.ResourcesLoadingEffect.RegisteredResNotExist,
                                                new JEMGameResourcePack[0]);
                            yield break;
                        }
                    }
                }
            }
            else
            {
                JEMLogger.LogError(
                    $"Unable to load game resources. Base resources patch `{cfg.PackDirectory}` not exist.");
                loadedEvent?.Invoke(JEMGameResources.ResourcesLoadingEffect.BaseDirNotExist, new JEMGameResourcePack[0]);
                yield break;
            }

            stopwatch.Stop();
            JEMLogger.Log($"Resources loaded in -> {stopwatch.Elapsed.TotalSeconds:0.0}s");
            yield return(new WaitForEndOfFrame());

            loadedEvent?.Invoke(JEMGameResources.ResourcesLoadingEffect.Success,
                                packs.Count == 0 ? new JEMGameResourcePack[0] : packs.ToArray());
        }