Page 1 of 1

Bug when saving List to cache

Posted: Thu Jul 06, 2023 7:45 pm
by TuckerBane
I recently had to fix a bug in Easy Save 3 so I thought I should share it with everyone. When you set easy save's save location to Cache it'll misidentify Lists (or, I suspect, any templated type) as just being of type Object and thus fail to load them correctly.

I'm pretty sure that the issue is that in ES3File.cs on line 163 we get typeof(T) instead of calling value.GetType(), and template functions don't always capture the full runtime type.

(current version)
cache[key] = new ES3Data(ES3TypeMgr.GetOrCreateES3Type(typeof(T)), ES3.Serialize(value, unencryptedSettings));

(simple fix version)
cache[key] = new ES3Data(ES3TypeMgr.GetOrCreateES3Type(value.GetType()), ES3.Serialize(value, unencryptedSettings));

There's even a comment saying that you need to do this for objects two lines up:
// If T is object, use the value to get it's type. Otherwise, use T so that it works with inheritence.

That fixed the issue for me, but for broader use cases you may need to follow the comment's advice and check if it's an object before trying to get its type.

I downloaded and imported the latest version of Easy Save 3 and this issue was still present.

Re: Bug when saving List to cache

Posted: Fri Jul 07, 2023 8:02 am
by Joel
Hi there, and thanks for the report.

Looks like Unity Versioning did an incomplete merge or something on our release branch as it's missing a few lines versus our development branch, and the comment is from an earlier version.

Note that your modification will also need a null check in case the data you're saving is null (in which case value.GetType() will throw a NullReferenceException). If you private message me your invoice number I'm happy to send over the updated version.

All the best,
Joel