Ejemplo n.º 1
0
        protected async override Task OnAfterRenderAsync(bool isFirstRender)
        {
            await JSRuntime.InvokeVoidAsync("doUserSelectDir");

            var queryString = QueryHelpers.ParseQuery(new Uri(NavigationManager.Uri).Query);

            ResourceReader = await BeatmapHelper.LoadNetworkResources(94790);

            /*
             * if (queryString.TryGetValue("sid", out var beatmapSetId))
             *  ResourceReader = await BeatmapHelper.LoadNetworkResources(int.Parse(beatmapSetId.ToString()));
             * else
             *  //尝试发起本地上传文件请求
             *  ResourceReader = await BeatmapHelper.LoadLocalResources();
             *
             * if (ResourceReader is null)
             * {
             *  //错误处理
             *  return;
             * }
             */

            Console.WriteLine("Start to select a .osb file and a .osu file (if it exist.)");
            var osbFilePath = ResourceReader.EnumeratePath("*.osb").FirstOrDefault();
            var osuFilePath = ResourceReader.EnumeratePath("*.osu").FirstOrDefault();

            Console.WriteLine("osu file : " + osuFilePath);
            Console.WriteLine("osb file : " + osbFilePath);

            var updater = StoryboardHelper.ParseStoryboard(ResourceReader.ReadFile(osuFilePath), ResourceReader.ReadFile(osbFilePath));

            Console.WriteLine($"Storyboard objects count : {updater.StoryboardObjectList.Count}");

            Console.WriteLine($"Start load render resource...");
            await StoryboardWindow.PrepareRenderResource(updater, ResourceReader);

            Console.WriteLine($"Render resource loading DONE!");

            StoryboardWindow.Play();

            Console.WriteLine($"OnAfterRenderAsync() end");
        }
        public async Task PrepareRenderResource(StoryboardUpdater updater, IDirectoryReader reader)
        {
            storyboardUpdater = updater;

            var textureResourceMap = new Dictionary <string, TextureResource>();

            foreach (var obj in updater.StoryboardObjectList)
            {
                switch (obj)
                {
                case StoryboardBackgroundObject background:
                    var resource = await _get(obj.ImageFilePath);

                    if (resource.IsValid)
                    {
                        background.AdjustScale(resource.Size.Height);
                    }
                    else
                    {
                        Log.Warn($"not found image:{obj.ImageFilePath}");
                    }

                    break;

                case StoryboardAnimation animation:
                    for (int index = 0; index < animation.FrameCount; index++)
                    {
                        string path = animation.FrameBaseImagePath + index + animation.FrameFileExtension;

                        if (!(await _get(path)).IsValid)
                        {
                            Log.Warn($"not found image:{path}");
                            continue;
                        }
                    }
                    break;

                default:
                    if (!(await _get(obj.ImageFilePath)).IsValid)
                    {
                        Log.Warn($"not found image:{obj.ImageFilePath}");
                    }
                    break;
                }
            }

            Console.WriteLine($"--------textureResourceMap--------");
            foreach (var pair in textureResourceMap)
            {
                Console.WriteLine($"{pair.Key}  \b  {pair.Value.Texture.Id}");
            }
            Console.WriteLine($"----------------------------------");

            RenderKernel.ApplyRenderResource(glContext, textureResourceMap);

            async Task <TextureResource> _get(string image_name)
            {
                var fix_image = image_name;

                //for Flex
                if (string.IsNullOrWhiteSpace(Path.GetExtension(fix_image)))
                {
                    fix_image += ".png";
                }

                if (textureResourceMap.TryGetValue(image_name, out var resource))
                {
                    return(resource);
                }

                //load
                string file_path = fix_image;

                resource = await _load_tex(file_path);

                if (!resource.IsValid)
                {
                    //todo: 从皮肤文件夹获取皮肤文件 file_path = Path.Combine(PlayerSetting.UserSkinPath ?? string.Empty, fix_image);

                    /*
                     * if (!_load_tex(file_path, out resource))
                     * {
                     *  if ((!image_name.EndsWith("-0")) && _get(image_name + "-0", out resource))
                     *      return true;
                     * }*/
                }

                if (resource.IsValid)
                {
                    textureResourceMap[image_name] = resource;
                    Log.Debug($"Created Storyboard sprite instance from image file :{fix_image}");
                }

                return(resource);
            }

            async Task <TextureResource> _load_tex(string file_path)
            {
                try
                {
                    var stream = reader.ReadFile(file_path);

                    if (stream is null)
                    {
                        return(default);