Add Router Hook example#7
Conversation
ArchangelWTF
left a comment
There was a problem hiding this comment.
Some small nits, I would be happy to merge once those are resolved
| private static ISptLogger<RouterHook> _logger; | ||
|
|
||
| public RouterHook( | ||
| JsonUtil jsonUtil, | ||
| ISptLogger<RouterHook> logger) : base( | ||
| jsonUtil, | ||
| // Add an array of routes to which we want to listen | ||
| GetRouteListeners() | ||
| ) | ||
| { | ||
| _logger = logger; | ||
| } | ||
|
|
||
| private static List<RouteAction> GetRouteListeners() |
There was a problem hiding this comment.
Routes should ideally not be static, you can create them in the constructor of the class (See: https://github.com/sp-tarkov/server-csharp/blob/eaf383bd129eb2c8014130a8cbd1ea52d932804b/Libraries/SPTarkov.Server.Core/Routers/Static/BuildStaticRouter.cs#L12 )
There was a problem hiding this comment.
I based this on the the 10CustomRoute example
. Should that be non-static as well? Or is there a difference here that I don't understand?There was a problem hiding this comment.
Nah, ideally a lot of the mod examples were never updated so 10 should receive an update too to match what we do in SPT. But I think starting here would be a good thing
There was a problem hiding this comment.
I generally aim for single purpose PRs so would you like me to add that in to this PR since they're kinda related or leave for another PR?
There was a problem hiding this comment.
I would love to keep the current PR within it's current scope, I would be open to accepting a new one for updating the other examples that don't follow the standard of what we currently do in SPT however
| private static ISptLogger<RouterHook> _logger; | ||
|
|
||
| public RouterHook( | ||
| JsonUtil jsonUtil, | ||
| ISptLogger<RouterHook> logger) : base( | ||
| jsonUtil, | ||
| // Add an array of routes to which we want to listen | ||
| GetRouteListeners() |
There was a problem hiding this comment.
I would recommend doing these in the primary constructor, then they are available and you don't have to make the _logger static to make it available
There was a problem hiding this comment.
Similar to my other comment, I also based this on another example that used logger, I believe it was the ReadJsonConfig example
server-mod-examples/5ReadCustomJsonConfig/ReadJsonConfig.cs
Lines 37 to 48 in 2d1d2d0
There was a problem hiding this comment.
Primary constructor is mostly used throughout SPT and should be used in most of the mod examples too, it's an easier way to use those across the entire class rather than to be forced to assign it in a separate constructor
Thanks for the feedback! I did some additional testing and added a few replies. I hope they don't come across as pushing back, I'd just really like to understand the best way to implement this as I'll be using it immediately in updating https://github.com/robpneu/SPT-EventAutoProfileBackup for 4.0 |
3749c91 to
82c7c2a
Compare
|
I struggled a bit to update this to use a primary constructor and remove the static, particularly with removing the static. Which of these is closer to what you had in mind? Separate Callbacks classPutting the route callback in a separate class and then passing that class into the constructor is the only way I found to not require that the Uses primary constructor but
|
I noticed there was no equivalent to the old Router Hooks example, https://github.com/sp-tarkov/mod-examples/blob/master/TypeScript/9RouterHooks/src/mod.ts, so this is my attempt at creating one. I expect this will start/continue a conversation from a few days ago in the mods-development channel about the best way to implement router hooks (I am FriedEngineer in discord).
First off, I've written very little C# so I am likely just missing some basic/background knowledge here. Second, this code is minimally functional however a few things feel...weird to me, but I didn't see another way to handle them.
new ValueTask<string>(output)with the output that the hooked route was sendingRouteActionseems to be blocking the "original" RouteActionHandleRouteAsyncexample to showcase non-blocking mod code. We could remove theHandleRouteSyncmethod entirely and just trust that people will generally use it, so that the hooked route is not blocked, but I could see some use case where blocking would be desired.Feel free to rip this code to shreds as well, I won't take it personally. If this helps at all to arrive at the point of having a Router Hooks example in this repo, I will be happy.