Cannot save system array of string

Easy Save 2 has been replaced by Easy Save 3, so is no longer supported.
Locked
Largelabs
Posts: 4
Joined: Wed Feb 07, 2018 11:13 am

Cannot save system array of string

Post by Largelabs »

Hi,

I've been trying recently to save and load arrays of strings with ES2. From what I understood in your documentation, system arrays are already handled without needing to add them via "Manage types". However, each time I try to save an array of strings, I get the following error :
Easy Save does not support saving of this type. For more information on supported types, and how to add support for your own types, please go to http://docs.moodkie.com/easy-save-2/supported-types/
And the save file in which I try to save ends up being corrupted and unreadable in the file editor.

Saving is done though this function that uses ES2Writer :

Code: Select all

bool saveFile(string i_fileName)
        {
            if (string.IsNullOrEmpty(i_fileName))
                return false;

            bool didSave = false;

            Dictionary<string, object> buffers = null;
            if (true == saveBuffer.TryGetValue(i_fileName, out buffers))
            {
                using (ES2Writer writer = ES2Writer.Create(i_fileName))
                {
                    foreach (KeyValuePair<string, object> pair in buffers)
                    {
                        writer.Write(pair.Value, pair.Key);

                    }

                    writer.Save();

                    didSave = true;
                    buffers.Clear();
                }
            }

            return didSave;
        }
By digging in the code, it appears that the write method cannot retrieve the type of system array. Is there something I need to do to make it available ? Or am I simply using the wrong function of your API ?

Many thanks,
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Cannot save system array of string

Post by Joel »

Hi there,

Your 'pair.Value' variable is of type 'System.Object'. if it's a string, you will need to cast it to a string.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Largelabs
Posts: 4
Joined: Wed Feb 07, 2018 11:13 am

Re: Cannot save system array of string

Post by Largelabs »

It's actually going to be a string[] in that case.
Does that mean that I should cast all objects to their original types in the write method ? I thought this was somehow done internally when the type is retrieved from ES2Types.

Regards,

EDIT :

So far, I've been saving primitive types a lot with this function. Trying to make things as generic as possible. Including strings, so far I've been able to pass them to that dictionary and save the files that way. Is the procedure different for arrays ? I probably missed something in the way the plugin works :)

Many thanks,
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Cannot save system array of string

Post by Joel »

Hi there,

The Write method is generic, and uses the generic parameter to get the ES2Type. So in your case, the generic parameter would be System.Object, not string.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Largelabs
Posts: 4
Joined: Wed Feb 07, 2018 11:13 am

Re: Cannot save system array of string

Post by Largelabs »

Hey Joel,

This is precisely what I'm doing in the function I use for saving, the one I quoted in my first message.

I populate a Dictionary<string, object> with data to save later. The key is the tag and the value is a System.object (strings, integers, floats... Whatever I give it). When it's time to save, I loop in this dictionary and call the Write method. I've been using this so far to save all kinds of types that derive from System.Object.

My problem is that when I try to save a string[] with this code, I get an error ("Easy Save does no support saving of this type"). This error occurs in WriteHeader when the type is null (meaning that it wasn't found in ES2TypeManager). My guess was that I should add support for string system arrays in "Manage types" but I can't find an entry for it.

Could you help me figure this out ? :)

Thanks again,
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Cannot save system array of string

Post by Joel »

I think I understand now.

There are separate Write methods for non-collections and each type of collection. i.e.
  • public void Write<T>(T param)
  • public void Write<T>(T[] param)
  • public void Write<T>(T[,] param)
  • public void Write<T>(List<T> param)
  • etc ...
So Write(System.Object) will route through to the public void Write<T>(T param) method, not the public void Write<T>(T[] param) method as it should, causing the error you're getting.

In this case, you will need to create an edge case for when writing arrays. Something like this should do the job:
foreach (KeyValuePair<string, object> pair in buffers)
{
    if(pair.Value.GetType() == typeof(string[]))
        writer.Write((string[])pair.Value, pair.Key);
    else
        writer.Write(pair.Value, pair.Key);
}
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Largelabs
Posts: 4
Joined: Wed Feb 07, 2018 11:13 am

Re: Cannot save system array of string

Post by Largelabs »

Works like a charm!

So bottom line, if I want to save collections of different types, I should implement an edge case in order to call to correct Write function.

Thank you so much,
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Cannot save system array of string

Post by Joel »

Glad that worked for you!

You will indeed need to implement an edge case for each collection type you plan on saving using the method.

Using method overloading provides a bit of a performance boost as we don't have to manually reflect the type. But unfortunately, in cases like yours where method overloading isn't possible, edge cases are necessary to route through to the correct method.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Locked