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

More of the same - ToCitizenInstance #1400

Merged
merged 3 commits into from
Feb 15, 2022
Merged

Conversation

DaEgi01
Copy link
Contributor

@DaEgi01 DaEgi01 commented Feb 13, 2022

Converted more lines to ToCitizenInstance().
Added more convenience methods to CitizenInstanceExtensions.

Added more convenience methods to CitizenInstanceExtensions.
@DaEgi01 DaEgi01 added the code cleanup Refactor code, remove old code, improve maintainability label Feb 13, 2022
@@ -63,7 +63,8 @@ public class AdvancedParkingManager
case ExtPathMode.DrivingToTarget: {
// citizen instance requires a car but is walking: release instance to
// prevent it from floating
if ((Singleton<CitizenManager>.instance.m_instances.m_buffer[(ushort)citizenInstanceId].m_flags & CitizenInstance.Flags.Character) != 0) {
ref CitizenInstance citizenInstance = ref ((ushort)citizenInstanceId).ToCitizenInstance();
Copy link
Member

Choose a reason for hiding this comment

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

Would it be worth adding an extension that accepts this uint citizenInstanceId and automatically casts to ushort internally?

internal static ref CitizenInstance ToCitizenInstance(this uint citizenInstance)
    => ref _citizenInstanceBuffer[(ushort)citizenInstance];

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, probably.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mmmh shit ... now that I thought about it and double checked, this casting actually suxx bad and could lead to errors.
valueoutside_ushort casted to ushort will always produce a ushort, even for values outside ushort. it will just overflow and by default overflow is not checked :/
So we can either do the checked {} thing, which will cost us some perf, or we can just hope for the best, in which case it does not matter if I do cast or don't at all it seems to me :/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok so I added the uint extension method but without the cast.
As far as I can tell array indexes are always int. In .net framework for sure, in Mono I did not find the code XD
Which means that all values given to it will be coverted to int anyway.
Precasting it would cause errors in case we give it a value > ushort.MaxValue and we get an overflow.
So I'd rather rely on an IndexOutOfRangeException in case we mess up instead getting an overflow, not realizing it and manipulating the wrong data.
Please correct me if you think my logic is wrong :D

for (int i = 1; i < CitizenManager.MAX_INSTANCE_COUNT; ++i) {
if ((citManager.m_instances.m_buffer[i].m_flags &
CitizenInstance.Flags.Character) == CitizenInstance.Flags.None) {
for (uint citizenInstanceId = 1; citizenInstanceId < CitizenManager.MAX_INSTANCE_COUNT; ++citizenInstanceId) {
Copy link
Member

Choose a reason for hiding this comment

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

Does this get resolved at compile time? If so, will it not cause problems at later date if a mod somehow extends number of citizen instances? Should we instead query m_length (or whatever) on the buffer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I remember there was a PR that fixed that? Or so I thought. Maybe for other constants only.

Copy link
Member

Choose a reason for hiding this comment

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

if field is not const and just readonly then it shouldn't get resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Anyway, its not part of the "ToCitizenInstance". But we could and should to a separate refactoring here to remove those constants and make a runtime call instead.

@originalfoo originalfoo added this to the 11.6.5 milestone Feb 13, 2022
# Conflicts:
#	TLM/TLM/Patch/_VehicleAI/_PassengerCarAI/GetLocalizedStatusPatch.cs
Copy link
Member

@originalfoo originalfoo left a comment

Choose a reason for hiding this comment

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

👍

@DaEgi01 DaEgi01 merged commit e8793b2 into master Feb 15, 2022
@DaEgi01 DaEgi01 deleted the refactoring/ToCitizenInstance branch February 15, 2022 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code cleanup Refactor code, remove old code, improve maintainability
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants