Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup environment project #518

Merged
merged 13 commits into from
Aug 8, 2023

Conversation

lahm86
Copy link
Collaborator

@lahm86 lahm86 commented Aug 8, 2023

Cleanup tasks for the TREnvironmentEditor project.
Part of #425.

@lahm86 lahm86 added the internal label Aug 8, 2023
@lahm86 lahm86 added this to the 1.8.0 milestone Aug 8, 2023
@lahm86 lahm86 self-assigned this Aug 8, 2023
@lahm86 lahm86 requested review from rr-, chreden and makotocchi August 8, 2023 09:44
Copy link

@rr- rr- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the new syntactic sugar.

{
if (sector.FDIndex != 0)
if (control.Entries[sector.FDIndex].Find(e => e is FDTriggerEntry) is FDTriggerEntry trigger
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know about you, but this variant is less readable to me than the original; if it's normal in C# world or the IDE complains that it should be coded this way, let's leave it as-is.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's used like this in a few places across the solution, and there was a message about it here.

EMType.NOOP => JsonConvert.DeserializeObject<EMPlaceholderFunction>(jo.ToString(), this),

_ => throw new InvalidOperationException(),
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be coded in a more beautiful way, with the switch only mapping T instead of the entire result?

Something such as this

EMType type =;
Type resultType = type switch {
    EMType.MirrorModel => EMImportModelFunction,,
};
return (
    typeof(JsonConvert)
    .GetMethod("DeserializeObject")
    .MakeGenericMethod(resultType)
    .Invoke(jo.ToString(), this)
);

or something similar? (Maybe there's a compile-time friendly alternative to GetMethod in 2023, too)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Newtonsoft method is as follows, but there are several overloads.

public static T? DeserializeObject<T>(string value, params JsonConverter[] converters)

The following works, but it feels too dependent on reflection and difficult to read imo.

return typeof(JsonConvert)
    .GetMethods()
    .Where(m => m.Name == nameof(JsonConvert.DeserializeObject) && m.IsGenericMethod && m.GetParameters().Length == 2)
    .First()
    .MakeGenericMethod(emType)
    .Invoke(null, new object[] { jo.ToString(), new JsonConverter[] { this } });

Another task we plan for is to move to System.Text.Json (#506) and to be honest it is this environment deserialization that will be the sticking point. What do you think about re-addressing it there? I've done a little looking into it and the JObject approach won't work out of the box anyway, so we're going to have to come up with a different parsing technique.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, if there are plans to migrate to something else anyway, there's no use to waste time on reworking this approach.

@lahm86 lahm86 merged commit 09988e9 into LostArtefacts:master Aug 8, 2023
@lahm86 lahm86 deleted the issue-425-net6-cleanup-stage2 branch August 8, 2023 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants