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

patch spawn/despawn vehicle using new harmony #864

Closed
kianzarrin opened this issue Apr 25, 2020 · 21 comments · Fixed by #1060
Closed

patch spawn/despawn vehicle using new harmony #864

kianzarrin opened this issue Apr 25, 2020 · 21 comments · Fixed by #1060
Assignees
Labels
code cleanup Refactor code, remove old code, improve maintainability enhancement Improve existing feature Harmony
Milestone

Comments

@kianzarrin
Copy link
Collaborator

this code is commented out:

// TODO this does currently not work with Harmony v1.1

// TODO this does currently not work with Harmony v1.1

Once I moved to harmony 2 should we uncomment this?

VehicleStateManager no longer exists but we can call OnSpawnVehicle(); instead. that method is already being called from CustomVehicle.CustomSpawn() so maybe I should delete Vehicle.Spawn/DespawnPatch

@kianzarrin kianzarrin added enhancement Improve existing feature triage Awaiting issue categorisation code cleanup Refactor code, remove old code, improve maintainability labels Apr 25, 2020
@kianzarrin
Copy link
Collaborator Author

if CustomSpawn is copy of the original method then we we should replace CustomSpawn with the harmony patch.

@kianzarrin
Copy link
Collaborator Author

its a bit hard to compare the code because of the differences in decompiler naming and spacing style.
does anyone know a solution?
Screenshot (923)

@pcfantasy
Copy link
Contributor

@kianzarrin

No quick way.

I think we have to read carefully about these codes and find out the difference.

@kianzarrin
Copy link
Collaborator Author

But the original code was also copy pasted from decompiler why should it be so hard to compare decompiler code with decompiler code?

There is a shit ton of code out there so any automation we develop could be useful and reduce the risk of human error

@kianzarrin
Copy link
Collaborator Author

kianzarrin commented Apr 26, 2020

Maybe we can look at decompiler tmpe code using same decompiler. That should solve almost all problems.

EDIT: Using this technique its becomes easily obvious that custom spawn only calls OnSpawn afterwards.
Screenshot (925)

Its important to compare to older CS dlls if any other differences were found.

@originalfoo
Copy link
Member

Seems I spotted that some time ago: #462

I think it would be very useful for TMPE to know when vehicles spawn/unspawn (no idea which is best approach for that).

For example, if we eventually start reserving parking spaces for vehicles, we will need to know if they despawn unexpectedly so we can un-reserve their parking space.

We could also maybe detect many vehicles despawning at same location = likely broken node bug (or at least we could stack trace to get some idea of the code path that led to despawning).

@kianzarrin
Copy link
Collaborator Author

@aubergine10 we already receive notification when vehicle spawn/despawns using redirection. I am saying we are now able to do it with harmony

@originalfoo
Copy link
Member

originalfoo commented Apr 26, 2020

Yup, it looks like we can do it harmony, but note there's two lines of custom code there:

image

I assume we'd just patch the vanilla .Spawn()?

image

@kianzarrin
Copy link
Collaborator Author

kianzarrin commented Apr 27, 2020

@aubergine10 There is only one line of custom code (functionally speaking) the method is recursive so where the original code calls Spawn, the redirected method calls custom spawn. so its equivalent.

All we need to do is a post fix (which is what I suspect from almost every custom AI)

EDIT: It is going to feel really good deleting a shit ton of code. maybe keep the original as text since the code is easier to read than the de-compiler?

@originalfoo
Copy link
Member

I don't think we should keep the original tbh, as it will go stale if base game gets updated.

Hopefully if we're just doing postfixes on .Spawn() methods we'll never even need to look at that vanilla code again.

@kianzarrin
Copy link
Collaborator Author

then why do we keep stockPathFind.cs ? is it particularly important?

<AdditionalFiles Include="Custom\PathFinding\StockPathFind.cs" />

@originalfoo
Copy link
Member

originalfoo commented Apr 27, 2020

Re: StockPathFind.cs: We keep it so we can do diffs when new game version is released.

BTW, I looking at the code in screenshots above, note that there are two different call contexts:

  • CustomSpawn() is called for vehicle and each trailer (if applicable) - ie. it can be called more than once per invocation of parent method
  • OnSpawnVehicle() is called only once, regardless of how many trailers

So I think we need two postfixes? One for the vanilla .Spawn() and one for whatever the parent method is of the code in the screenshots above.

@originalfoo originalfoo removed the triage Awaiting issue categorisation label Apr 27, 2020
@kianzarrin
Copy link
Collaborator Author

@aubergine10 no. only one change (unless if I am making a mistake)

CustomSpawn() is called for vehicle and each trailer (if applicable) - ie. it can be called more than once per invocation of parent method

Note that on the left there is an equivalent recursive call for vehicle and each trailer (if applicable) - ie. it can be called more than once per invocation of parent method. So this line provides no new logic and does not need to be patched. Actually It would have made no difference if this line called Spawn() instead of CustomSpawn() considering Spawn is redirected to CustomSpawn()anyways.

@originalfoo
Copy link
Member

originalfoo commented Apr 27, 2020

Based on the current way the code works:

Let say vehicle has 2 trailers. That's 3 times CustomSpawn() that will be called (once for front vehicle, then once for each trailer). After that "spawn loop" completes, the OnSpawnVehicle() is called once.

So I don't see how we can replicate that with only one patch around the vanilla .Spawn() method, because that method won't know if trailers need adding.

Unless the plan is to call OnSpawnVehicle() multiple times, once for each 'part' of the vehicle, then we will need two postfixes.

The current way it works, OnSpawnVehicle() is only called once, after all parts of the vehicle (front + trailers, if applicable) have been spawned.

@originalfoo
Copy link
Member

More clarity: If there is a truck with 2 trailers attached, TMPE will currently see:

CustomSpawn() // truck
CustomSpawn() // trailer 1
CustomSpawn() // trailer 2
OnSpawnVehicle() // only called after all parts of the vehicle are spawned

@kianzarrin
Copy link
Collaborator Author

kianzarrin commented Apr 27, 2020

More clarity: If there is a truck with 2 trailers attached, TMPE will currently see:
CustomSpawn() // truck
CustomSpawn() // trailer 1
CustomSpawn() // trailer 2
OnSpawnVehicle() // only called after all parts of the vehicle are spawned

I think you are wrong. You are confused about how recursive calls work. in the custom code OnSpawnVehicle is being called multiple times.

CustomSpawn() // truck
CustomSpawn() // trailer 1
OnSpawnVehicle() // trailer 1
CustomSpawn() // trailer 2
OnSpawnVehicle() // trailer 2
OnSpawnVehicle() // truck

EDIT: This is what happens if the truck has 4 trailers:
https://youtu.be/nbleC0-YJ88?t=1603

@originalfoo
Copy link
Member

Ah, the parent method is CustomSpawn(). Ok, that makes more sense.

Bah, there's actually two loops that contain recursive spawn. The more I look at the vanilla Spawn() method, the more crazy it becomes. Specifically, does it actually need to be recursive given how the game actually works?

As far as I know, there's never anything that has side-vehicles or vertical trailers. Assuming it's always a start vehicle followed by zero or more trailers, it should be possible to just to create and configure those vehicles with a few simple iterations without going recursive and thus reduce call stack and gc.

@originalfoo
Copy link
Member

Ok, so there are some wierd vehicle configs that require that extra code in the vanilla Spawn() methods.

So yeah, there's no need for TMPE to replicate those methods, just postfix the event. Earlier I didn't realise it was recursive method (snippets in images didn't show what the parent method was) hence confusion.

@kianzarrin kianzarrin self-assigned this May 19, 2020
kianzarrin added a commit that referenced this issue Jun 3, 2020
#864 replaced CustomVehicle redirection with harmony patches.
@originalfoo originalfoo added this to the 11.6.0 milestone Jun 9, 2020
kianzarrin added a commit that referenced this issue Jun 15, 2020
partial migration of VehicleAI.StartPathFind into harmony (#895)
migrate vehicle Spawn/Unspawn to harmony (#864)
@krzychu124 krzychu124 mentioned this issue Feb 20, 2021
4 tasks
@originalfoo
Copy link
Member

originalfoo commented Dec 20, 2021

Considering #921 and #1060 are merged, I think this #864 can be closed?

@kianzarrin
Copy link
Collaborator Author

damn I fixed this ages ago!

@kianzarrin
Copy link
Collaborator Author

kianzarrin commented Dec 20, 2021

TMPE is now harmony only.

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 enhancement Improve existing feature Harmony
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants