ValveKeyValue icon indicating copy to clipboard operation
ValveKeyValue copied to clipboard

Improve handling of `#base` and `#include` directives

Open Abrahamic-God opened this issue 3 years ago • 7 comments

Hello! 👋

This PR makes two relatively small changes to improve handling of #base and #include directives.

I've been working with some .res files that have #base or #include directives which point to files that don't exist, and this PR silently skips those files. To my knowledge, this is how the Source engine treats such directives. Additionally, I've been working with some files that consist ONLY of these directives, which is also valid in the Source engine. This PR handles both cases.

Let me know if you'd like any changes or additions to this PR. I thought about adding tests but was kind of scared off by the test structure. I'd appreciate some help on that front if tests are desired.

Thanks!

Abrahamic-God avatar Sep 28 '22 22:09 Abrahamic-God

Does it not work if your FileLoader simply returns an empty object? This needs a test too.

xPaw avatar Sep 29 '22 07:09 xPaw

Does it not work if your FileLoader simply returns an empty object?

It does not, an error is thrown:

Unhandled exception. System.InvalidOperationException: Stack empty.
   at System.Collections.Generic.Stack`1.ThrowForEmptyStack()
   at System.Collections.Generic.Stack`1.Peek()
   at ValveKeyValue.Deserialization.KVMergingObjectBuilder.FinalizeState() in C:\Users\vanca\Documents\git\SteamDatabase\ValveKeyValue\ValveKeyValue\ValveKeyValue\Deserialization\KVMergingObjectBuilder.cs:line 20
   at ValveKeyValue.Deserialization.KVObjectBuilder.GetObject() in C:\Users\vanca\Documents\git\SteamDatabase\ValveKeyValue\ValveKeyValue\ValveKeyValue\Deserialization\KVObjectBuilder.cs:line 20
   at ValveKeyValue.KVSerializer.Deserialize(Stream stream, KVSerializerOptions options) in C:\Users\vanca\Documents\git\SteamDatabase\ValveKeyValue\ValveKeyValue\ValveKeyValue\KVSerializer.cs:line 45
   at REDACTED

This needs a test too.

Tests have been added 🙂

Abrahamic-God avatar Sep 29 '22 14:09 Abrahamic-God

Perhaps instead of removing all the checks there should be a way for file loader to accept empty objects.

Have you tried an empty root object too?

xPaw avatar Sep 29 '22 14:09 xPaw

Perhaps instead of removing all the checks there should be a way for file loader to accept empty objects.

I'm not sure what you mean.

Have you tried an empty root object too?

Yes. If a file that has #base or #include directives has even an empty root object, it would succeed without this PR. This PR only helps in the case where a file has only #base and #include directives with no root object.

Abrahamic-God avatar Sep 29 '22 14:09 Abrahamic-God

I'd rather keep the checks that you removed, and instead explicitly support empty files/objects (consumers can provide empty root object at the moment).

@yaakov-h what do you think

xPaw avatar Sep 29 '22 15:09 xPaw

I'd rather keep the checks that you removed, and instead explicitly support empty files/objects (consumers can provide empty root object at the moment).

What checks are you referring to? I don't think this PR removes any checks.

Abrahamic-God avatar Sep 29 '22 15:09 Abrahamic-God

I'm gonna have to dig out my VKV-vs-tier1 validation tool and check exactly how Valve's handling works.

yaakov-h avatar Sep 30 '22 01:09 yaakov-h