Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: C# resource manager  (Read 5239 times)

0 Members and 1 Guest are viewing this topic.

cpolymeris

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
    • Email
C# resource manager
« on: June 27, 2013, 02:40:22 am »
Hello,
I need a resource manager for my project, to make sure resources are only loaded once and to make them easily editable by users. Apart from textures, fonts and strings it's very important to me that this manager supports custom types. E.g. I have a "Mosaic" type which is basically a multilayered map that loads and unloads textures on demand. Ideally, mosaics would be defined in the resources somewhat like this:

<resources>
        <!-- other resources... textures, fonts, strings, etc -->
        <mosaic name="background">
                <tileset name="background_small"
                                        basename="resources/back_small_{n}.png"
                                        width="4" height="2"/>
                <tileset name="background_medium"
                                        basename="resources/back_medium_{n}.png"
                                        width="8" height="4"/>
                <tileset name="background_large"
                                        basename="resources/back_large_{n}.png"
                                        width="20" height="10"/>
        </mosaic>
        <!-- other resources... -->
</resources>

The question is: Does a class exist that does this for me? That Microsoft resx thing, would that fit the bill? Or would you write your own?

Sorry if the question seems basic, I am quite new to .NET and thanks for your help.

cpolymeris

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
    • Email
Re: C# resource manager
« Reply #1 on: June 27, 2013, 10:46:56 am »
Ok. I ended up rolling my own "resource manager". (EDIT: See new version below.)

I haven't fully tested it yet, but it seems to work well. You can read resources from files like this:

<?xml version="1.0" encoding="UTF-8" ?>
<assets>
        <int id="number6">6</int>
        <int id="othernumber">-8</int>
        <string id="somestring">Text, text, text...</string>
        <section id="something">
                <texture id="sometexture" path="../texture.png"/>
        </section>
</assets>

And define your own "factories" (resource loaders), like this:

RegisterFactory("texture", typeof(SFML.Graphics.Texture), delegate(XElement el)
{
    string filename = el.Attribute("path").Value;
    return new SFML.Graphics.Texture(filename);
});

Feel free to use it, and (even better) improve upon it. GPL3.

EDIT: Of course, using it to load strings and ints isn't very efficient... but for textures and sounds it should be a Good Thing.
« Last Edit: June 28, 2013, 11:46:37 am by cpolymeris »

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: C# resource manager
« Reply #2 on: June 28, 2013, 08:05:55 am »
This might be a dumb question, but what purpose do the attributes serve other than listing the path for the texture?

I'd say the element name and value is more than enough info for loading the data, and it would make your xml files that much smaller.
DSFML - SFML for the D Programming Language.

cpolymeris

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
    • Email
Re: C# resource manager
« Reply #3 on: June 28, 2013, 10:52:34 am »
I am not sure I understand you correctly. You mean, this:
<texture id="sometexture">../texture.png</texture>

 is better than this:
<texture id="sometexture" path="../texture.png"/>
?

cpolymeris

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
    • Email
Re: C# resource manager
« Reply #4 on: June 28, 2013, 11:15:55 am »
I have improved and simplified the resource manager a bit: Manager.cs
Also created Factories for all relevant SFML resources: SFMLFactories.cs They work from file paths (using the "src" attribute) or from embedded resources (using "res").

Pretty easy, now, IMHO, to describe resources in a file:
<?xml version="1.0" encoding="UTF-8" ?>
<assets>
    <section id="ui">
        <texture id="background" res="Pax.Client.Assets.Textures.background.png" repeated="true"/>
        <font id="roman_font" res="Pax.Client.Assets.Fonts.GenBkBasR.ttf"/>
        <font id="bold_font" res="Pax.Client.Assets.Fonts.GenBkBasB.ttf"/>
    </section>
</assets>

Still not sure what should be done if a resource is not found. For now I am just raising an exception.
« Last Edit: June 28, 2013, 11:49:30 am by cpolymeris »

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: C# resource manager
« Reply #5 on: June 28, 2013, 04:10:45 pm »
What I actually meant was that:
<texture>../texture.png</texture>

was better than
<texture id="sometexture" path="../texture.png"/>

Since it didn't look like this was useful data at first glance to your second post. :P


I see now that you actually DO use it so nevermind!
DSFML - SFML for the D Programming Language.

 

anything