Пример #1
0
        static pTexture fromDebugLocations(string filename)
        {
            if (fswUi == null)
            {
                if (fswFailed)
                {
                    return(null);
                }

                try
                {
                    fswUi = new FileSystemWatcher(Path.Combine("..", "..", "..", "..", "osu!ui", "Resources"))
                    {
                        EnableRaisingEvents = true
                    };
                    fswGameplay = new FileSystemWatcher(Path.Combine("..", "..", "..", "..", "osu!gameplay", "Resources"))
                    {
                        EnableRaisingEvents = true
                    };
                }
                catch
                {
                    fswFailed = true;
                }
            }

            pTexture tex = null;

            bool   x2     = GameBase.UseHighResolutionSprites;
            string suffix = GameBase.UseHighResolutionSprites ? @"@2x.png" : @".png";

            string directFile = Path.Combine("..", "..", "..", "..", "osu!ui", "Resources", filename + suffix);

            tex = pTexture.FromFile(directFile);
            if (tex != null)
            {
                fswUi.Renamed += delegate(object sender, RenamedEventArgs e)
                {
                    if (Path.GetFileName(directFile) != e.Name)
                    {
                        return;
                    }
                    while (true)
                    {
                        try
                        {
                            using (Stream stream = new FileStream(directFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                                using (Bitmap b = (Bitmap)Image.FromStream(stream, false, false))
                                {
                                    tex.SetData(b);
                                    tex.UploadNextFrame();
                                }

                            break;
                        }
                        catch { }
                    }
                };

                tex.Filename = directFile;
                tex.DpiScale = x2 ? 2 : 1;
                return(tex);
            }

            directFile = Path.Combine("..", "..", "..", "..", "osu!gameplay", "Resources", filename + suffix);
            tex        = pTexture.FromFile(directFile);
            if (tex != null)
            {
                fswGameplay.Renamed += delegate(object sender, RenamedEventArgs e)
                {
                    if (Path.GetFileName(directFile) != e.Name)
                    {
                        return;
                    }
                    while (true)
                    {
                        try
                        {
                            using (Stream stream = new FileStream(directFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                                using (Bitmap b = (Bitmap)Image.FromStream(stream, false, false))
                                {
                                    tex.SetData(b);
                                    tex.UploadNextFrame();
                                }

                            break;
                        }
                        catch { }
                    }
                };

                tex.Filename = directFile;
                tex.DpiScale = x2 ? 2 : 1;
                return(tex);
            }

            return(tex);
        }
Пример #2
0
        internal static pTexture Load(string name, SkinSource source = SkinSource.All, TextureAtlas atlas = null)
        {
            if (name == null)
            {
                return(null);
            }

            if (SkinManager.IgnoreBeatmapSkin && source != SkinSource.Beatmap)
            {
                source &= ~SkinSource.Beatmap;
            }

            if (source == SkinSource.All)
            {
                switch (GameBase.Mode)
                {
                case OsuModes.Play:
                case OsuModes.Edit:
                    break;

                default:
                    //As a default, don't load from the beatmap unless we are in play or edit mode.
                    source &= ~SkinSource.Beatmap;
                    break;
                }
            }

            pTexture tex = null;

            Dictionary <string, pTexture> dic = null;

            if (!LookupCache.TryGetValue(source, out dic))
            {
                dic = LookupCache[source] = new Dictionary <string, pTexture>();
            }

            if (dic.TryGetValue(name, out tex))
            {
                return(tex);
            }

            try
            {
                //Check for beatmap-specific sprite availability
                Beatmap current = null;
                if ((source & SkinSource.Beatmap) > 0 && (current = BeatmapManager.Current) != null)
                {
                    if (BeatmapSpriteCache.TryGetValue(name, out tex))
                    {
                        //Cached sprite is available.
                        //Note that we must check null *inside* this if block because a cached null means this source has no available file.
                        //If we find a null, we don't want to enter the else block and check for a file every time, so we quietly ignore this source here.
                        if (tex != null)
                        {
                            return(tex);
                        }
                    }
                    else
                    {
                        string filename = name.IndexOf('.') < 0 ? name + @".png" : name;
                        using (Stream stream = current.GetFileStream(filename))
                        {
                            if (stream != null)
                            {
                                tex = pTexture.FromStream(stream, atlas);
                                if (tex != null)
                                {
                                    tex.Filename  = filename;
                                    tex.AssetName = name;
                                    tex.Source    = SkinSource.Beatmap;
                                    tex.UploadNextFrame();
                                }

                                BeatmapSpriteCache[name] = tex;

                                if (atlas != null)
                                {
                                    BeatmapTextureAtlases.Add(atlas);
                                }

                                return(tex);
                            }
                        }

                        BeatmapSpriteCache[name] = null;
                    }
                }

                if ((source & SkinSource.Skin) > 0 && !SkinManager.IsDefault)
                {
                    if (SkinSpriteCache.TryGetValue(name, out tex))
                    {
                        //Cached sprite is available.
                        //Note that we must check null *inside* this if block because a cached null means this source has no available file.
                        //If we find a null, we don't want to enter the else block and check for a file every time, so we quietly ignore this source here.
                        if (tex != null)
                        {
                            return(tex);
                        }
                    }
                    else
                    {
                        string filename = Path.Combine(SkinManager.Current.FullPath, name.IndexOf('.') < 0 ? name + @".png" : name);

                        string filename2x = filename.Insert(filename.LastIndexOf('.'), @"@2x");
                        if (GameBase.UseHighResolutionSprites && File.Exists(filename2x))
                        {
                            tex = pTexture.FromFile(filename2x, atlas);
                            SkinSpriteCache[name] = tex;

                            if (atlas != null)
                            {
                                SkinTextureAtlases.Add(atlas);
                            }

                            if (tex != null)
                            {
                                tex.DpiScale  = 2;
                                tex.Source    = SkinSource.Skin;
                                tex.AssetName = name;
                                tex.UploadNextFrame();
                            }
                            return(tex);
                        }

                        if (File.Exists(filename))
                        {
                            tex = pTexture.FromFile(filename, atlas);
                            SkinSpriteCache[name] = tex;

                            if (atlas != null)
                            {
                                SkinTextureAtlases.Add(atlas);
                            }

                            if (tex != null)
                            {
                                tex.Source    = SkinSource.Skin;
                                tex.AssetName = name;
                                tex.UploadNextFrame();
                            }
                            return(tex);
                        }

                        SkinSpriteCache[name] = null;
                    }
                }

                if ((source & SkinSource.Osu) == 0)
                {
                    return(null);
                }

                try
                {
                    if (DefaultSpriteCache.TryGetValue(name, out tex))
                    {
                        return(tex);
                    }

#if LOAD_LOCAL
                    if ((tex = Load(name, SkinSource.Skin)) != null)
                    {
                        return(tex);
                    }
#endif

                    name = GeneralHelper.PathSanitise(name);
                    tex  = name.IndexOf(Path.DirectorySeparatorChar) > 0 ? pTexture.FromFile(name, atlas) : pTexture.FromResourceStore(GeneralHelper.StripExtension(name), atlas);

                    DefaultSpriteCache[name] = tex;

                    if (atlas != null)
                    {
                        DefaultTextureAtlases.Add(atlas);
                    }

                    if (tex != null)
                    {
                        tex.Source    = SkinSource.Osu;
                        tex.AssetName = name;
                        tex.UploadNextFrame();
                    }
                    return(tex);
                }
                catch
                {
                    return(null);
                }
            }
            finally
            {
                dic[name] = tex;
            }
        }