-
Notifications
You must be signed in to change notification settings - Fork 85
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
Eliminate all static variables and methods #211
Comments
As a novice learning C#, may I ask why? (it will be useful knowledge of a mod I'm writing). |
Also, could we get a linter (or whatever) set up to help us with this task? |
I don't see the purpose in this. There's always a need every once in a while for a static variable and/or method. Just look in Colossal Order's code; They use them often for things that don't require an instance. |
Ah, I think I get it - if user has 2 copies of TMPE installed (eg. Labs and Stable, or one of those and local build, or one of those and pcfantasy mods that replicate chunks of TMPE), it's all using the same namespaces so the static stuff from one is overwriting that of the other? |
@aubergine10 Exactly, issue #210 proofs that C:S is unable to handle multiple mod instances correctly regarding static variables/methods. The second instance of TMPE (the one from the AJR mod) tries to access a static variable (Options.turnOnRed) that is present in AJR's TMPE but not in the other instance. By removing all static members (except for a very limited & controlled set, in case we need it) we also can make the mod 100% hot-swappable. Then, we would not have to restart the game on every code change. |
Could you add an issue for this? Maybe also add a linter / rule scheme that we can use. |
I have never had to restart the game on a code change, I've only had to exit and re-enter the world. If you want I could start work on this while you guys work on Harmony integration, and then once that's done I can work on EVE |
I assume instantiation occurs automatically due to classes / members being defined as How would such things be invalidated when unloading? |
@aubergine10 Probably setting them as null in the classes, giving them a value at load, and setting them back to null and unload |
Another reason: I would like to use a mocking framework (probably Moq since it's free) for unit testing. However, it does not support mocking static methods. |
Note that 'true' hot swapping is impossible. The game uses a single app domain both for the game itself and for all mods. Unloading an assembly from an app domain is not supported on Mono (it will be supported only on .NET Core 3.0 and .NET 5). Even if there will be no But even that won't help much, because the game uses assembly resolution based on assembly names. See the |
What is wrong with static methods? |
@kvakvs Not to challenge your thoughts, but I'd love to see some references for the claims that they're 10 times faster. |
@FireController1847 We were discussing terrible |
With the next couple of updates to mod checker (#434 and #439) issues with zombie TM:PE assemblies will be significantly reduced. That being the case, and factoring in that hot-swappability can't reliably be achieved (as per Dymanoid's comment above), should we be rethinking our stance on static methods, classes, etc? |
There is nothing wrong with static methods, especially when they are pure functions (read the definition of that if you are not sure about this term). However, they don't allow polymorphism. If you are running into performance troubles because of virtual method calls only, please let me see such an application. In 99.999% of the cases the reason is somewhere else. The measurements in the referenced blog post are crap, they measure everything but the method call performance. Just tell me if you want more info. The static fields, properties, and methods were suspected in the original post to prevent the hot swapping, but the real reason has nothing to do with static members and methods. I explained it in my previous comment above. However, static fields and properties are evil from design point of view. They should be avoided. They make the code less testable, they reduce the code maintainability and break many software design principles, like e. g. SOLID. |
As of v11-alpha3, the mod checker will always report duplicate instances of TM:PE even if the mod checker has been disabled by user. |
fixed Reload, Hot-Swap, Memleaks: - #211 : in-game Hot-Swap supported (with assembly version display) - Fixed Loading another game while in game creates multiple instances of UI. - Fixed TMPE memory leaks in cleanup path. - Post build events prints time and assembly version.
Static variables usage has been reduced. Hot-reload has been implemented some time ago and works without issues(the satisfying degree of usability). Closing. |
As a developer I want my application to be hot-swappable to allow for a faster development time.
Assumptions/Preconditions
Tasks
Acceptance criteria
The text was updated successfully, but these errors were encountered: