Bug when saving List to cache

Discussion and help for Easy Save 3
Post Reply
TuckerBane
Posts: 1
Joined: Thu Jul 06, 2023 6:56 pm

Bug when saving List to cache

Post 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.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Bug when saving List to cache

Post 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
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply