313 Commits

Author SHA1 Message Date
Kawe Mazidjatari
fce3b1f46a CCrashHandler: small code cleanup 2023-09-09 20:57:23 +02:00
Kawe Mazidjatari
b85003a9ac Replace JSON library with RapidJSON
Less copies and overall CPU time parsing/constructing JSON's as we are mostly still using string pointers rather than STL string container.
2023-09-07 11:17:05 +02:00
Kawe Mazidjatari
71b0781715 Utilize 'DevMsg()' for uncertain builds only
Only uncertain builds will contain DevMsg()/DevWarning() prints. For retail, Msg() and Warning() should be used instead.
2023-08-21 19:12:29 +02:00
Kawe Mazidjatari
195b2a11d7 Variable rename
Its a suffix.
2023-08-01 14:48:08 +02:00
Kawe Mazidjatari
a4da4afd19 FormatBytes function bug fixes
- Use snprintf instead to avoid stack buffer overflow.
- Make sure nBytes has a value before using it to create an index for the suffix.
2023-08-01 14:45:55 +02:00
Kawe Mazidjatari
eb14469178 Create utility function for formatting bytes
Byte count to prettified representation as string.
2023-07-31 11:43:25 +02:00
Kawe Mazidjatari
b7b42786ab Add STL version of Appendslash 2023-07-28 14:45:04 +02:00
Kawe Mazidjatari
2b05edd374 Fix using incorrect member as bounds for looping exporteds
Fixes a crasher on certain modules as it could read oob; must use 'NumberOfNames' to iterate over export names.
2023-07-23 20:02:12 +02:00
Kawe Mazidjatari
c5e7c3e05d Move 'CPUInformation' struct
Move to the header it actually belongs to.
2023-07-15 10:28:49 +02:00
Kawe Mazidjatari
6f441292d0 CModule construction optimization
Moved construction logic to separate method, and call that from constructor instead. When willing to change the entire context on the same object, you can now just call 'InitFromXXXX()'. Previously, a whole new object would be generated and copied into ours, and then deleted again.
2023-07-10 13:54:00 +02:00
Kawe Mazidjatari
8b0fecf8bc Add aligned memalloc overrides
Make sure these are getting overridden with our implementation as well.
2023-07-10 13:51:14 +02:00
Kawe Mazidjatari
8cb46354e6 Permanently fix aligned memalloc crash
It was still crashing as the thisptr should actually be passed into the alloc callback. Changed and the function call is now identical to engine's impl. Tested in Debug, Profile and Release, Release has also been tested with LTCG.
2023-07-10 13:49:36 +02:00
Kawe Mazidjatari
248efc114b Fix aligned memalloc crash when compiled with LTCG
Crash occurred as the arguments to the alloc/free callbacks would involve the 'this' pointer, and therefore, the registers would shift and misalign. The fix is to just make the functions static and call the pointers directly from the singleton exposed by the engine. Additional cleanup has been performed by adding typedefs for the function pointers.
2023-07-09 20:03:57 +02:00
Kawe Mazidjatari
b4cd72034e CModule: light cleanup
Remove the option to disable dynamic init, it was broken anyways as the string was getting constructed, and therefore, dynamic memory could be allocated if string exceeds a certain size where it has to be moved from stack to heap. The only functions we were interested in, while not allocating anything have been made static instead in the last refactor round.
2023-07-08 03:01:54 +02:00
Kawe Mazidjatari
2afec89c2f CCrashHandler: light cleanup 2023-07-08 02:44:33 +02:00
Kawe Mazidjatari
819b3aeb16 Move and rename 'IsValidUTF8', 'UTF8CharLength'
Moved to strtools (tier1); utility will be renamed and decoupled into 'stl_utility' soon.
2023-07-04 23:01:13 +02:00
Kawe Mazidjatari
87958822b9 Fix debug compiler errors
Compiler errors when compiling in debug.
2023-07-03 10:59:29 +02:00
Kawe Mazidjatari
f6f3c7f9a2 More code cleanup
- Set character set to multi-byte (this is because the game is also build with it).
- Utility function 'FileExists' now takes a raw string pointer, this avoids having to construct a fs::path each time its getting called (which is quite a lot!).
- Performed overall cleanup to code containing logic to override load paths. Mostly removing unnecessary copy constructions.
2023-07-03 02:14:39 +02:00
Kawe Mazidjatari
789ecbc234 Fully disable memory expansion
The game's memalloc does not support memory expansion, the base expansion function couldn't be shadowed properly without linker errors. However we want to make sure no instances of it get compiled into the module, ever. Therefore, the duplicate symbol is kept in memoverride (_expand_base), and the only usage has been removed from LZHAM's 'lzham_mem'. If a linkage is attempted to anything utilizing '_expand', a linker error will be emitted and no image will be generated.
2023-07-03 00:25:15 +02:00
Kawe Mazidjatari
87f9420889 Globally reduce the use of auto for function pointer declarations
Find regex pattern:
inline auto ([a-zA-Z0-9_]+) = ([a-zA-Z0-9_]+)\.RCast<([a-zA-Z0-9_:<>*]+) *\(\*\)\(([^)]*)\)>\(\);
Replace regex pattern:
inline $3(*$1)($4);

This commit also removes the unnecessary initialization (which was required to type the auto variables),
and therefore removed 6kb of unnecessary dynamic initialization code.
2023-07-02 23:01:29 +02:00
Kawe Mazidjatari
a0419eec11 Add method for obtaining PEB
Inline method for obtaining process environment block.
2023-07-02 22:02:31 +02:00
Kawe Mazidjatari
9b74ac2e53 Improve threadtools
* Added and utilized auto lock/unlock mechanism.
* Rebuild CThreadFastMutex's 'Lock' and 'Unlock' methods in the SDK.
* Forced thread intrinsics and ID checkers as inline.
2023-07-02 11:38:36 +02:00
Kawe Mazidjatari
c1a0276951 Fix unresolved external symbol errors for loader.dll
Moved CModule statics to a separate translation unit, preventing the linker to link unused stuff into the loader module, thus allowing us to drop the linkage of unused libraries.
2023-07-01 22:29:34 +02:00
Kawe Mazidjatari
d37f12aa4f Don't recreate the command line
Don't recreate it in the engine (LauncherMain) if its already created in the SDK. This solution toggles a hybrid between creating it in the SDK, or in the engine when -noworkerdll is passed to the loader.
2023-07-01 02:11:39 +02:00
Kawe Mazidjatari
dfb61aff74 Attempt to load the startup CFG from platform
Attempt to load it from there as well on failure.
2023-07-01 01:04:23 +02:00
Kawe Mazidjatari
e9518d8ff1 CCommandLine improvements
* Make accessor inline.
* Add method 'AppendParametersFromFile' (will be used in a future init refactor).
2023-06-30 21:59:16 +02:00
Kawe Mazidjatari
bb28e160cf Override debug malloc functions and fix bug
* Override debug malloc functions, doesn't seem to be properly supported by the visual studio libraries, but it has to be done, else we crash trying to initialize the DLL (must use the same allocator as the game), a linker flag '/FORCE:MULTIPLE' had to be set to make this work, this should be set for only DEBUG builds in the future!.
* Fixed a bug in '_recalloc_base' where the allocation size was not calculated properly. Only the size was taken into account, but it should had been multiplied by the count.
* Stubbed additional CRT debug only memory debugging code, it will crash on our implementation in debug.
2023-06-29 00:24:22 +02:00
Kawe Mazidjatari
99fe5b794a Make memoverride a separate lib
Else everything linked against tier0 will have to use the game's memalloc implementation, which requires it to run with the game process. Only limit this to DLL's that run with the game process.
2023-06-28 20:00:38 +02:00
Amos
939e101fe6 Properly override all malloc wrappers
This covers the entire source code, including thirdparty libraries if memstd.cpp is compiled.
Things left to be done:
- Utilize the debug methods of the CStdMemAlloc class by routing the debug variants to those.
- Move this to a standalone library, so tools and stuff not interfacing directly with the game engine can still link against tier0.
2023-06-28 10:03:10 +02:00
Kawe Mazidjatari
84e3414d7c Add 'recalloc' replacement
Added recalloc. _expand should be replaced as well in the nearby future. The functions should override the _xxxx_base variants instead, and be moved to a separate lib to cover the executable globally.
2023-06-27 21:37:15 +02:00
Kawe Mazidjatari
ef69611435 Replace memalloc calls throughout entire SDK
Global 'direct' usage of 'MemAllocSingleton()' has been jettisoned. Where possible, smart pointers were used instead. During the refactor, the following bugs were addressed and fixed:
- The virtual destructor of 'CCVarIteratorInternal' was NOT called on destruction.
- Class function 'KeyValues::MakeCopy' did NOT calculate the buffer size of the wide string correctly, the original calculation was 'len+1*sizeof(wchar_t)', but should've been '(len+1)*sizeof(wchar_t)'.

Some other code changes include:
- Tier0 include 'memstd.h' has been moved above all thirdparty includes, to make sure the memalloc functions get shadowed with ours in third party libraries as well.
- RPak file paths string literals are now defines.
- 'DestroyOverlay' has been refactored to match the assembly of the game.
2023-06-26 22:34:24 +02:00
Kawe Mazidjatari
708a249507 Internal memalloc replacement
This commit replaces the standard memalloc system with that of the game. This means we can share the same allocated memory objects between the game and SDK without having to use 'MemAllocSingleton()' and manually call the constructors/destructors. This also means we can create ConVar's and ConCommands in the global scope, and a bunch more cool stuff. The explanation in 'r5dev\loader\loader.cpp' documents the new loading system.
2023-06-25 11:37:52 +02:00
Kawe Mazidjatari
b76f4aa3bd CModule class improvements
*Use unordered_map to get mpdule sections instead, as this is more performant than comparing strings.
* Removed 'm_SectionName' field from ModuleSections_t, as the unordered map now keeps track of them.
* Removed all extraneous module section copies.
* Renamed 'GetImportedFunction' to 'GetImportedSymbol'.
* Renamed 'GetExportedFunction' to 'GetExportedSymbol'.
*Made a static version of 'GetImportedSymbol' and 'GetExportedSymbol', so it could be used on raw module base addresses.
*Created inlines for getting the DOS and NT headers.
*Improved formatting so the code could be read more easily on a vertical monitor.
2023-06-25 10:29:42 +02:00
Kawe Mazidjatari
bc995230fa Avoid extraneous string copy
Take raw pointer directly, this is currently the only way these functions get called with.
2023-06-19 22:36:46 +02:00
Kawe Mazidjatari
18e4a262c8 Fix double crash dialog boxes bug
This occurs when the game's unhandled exception handler is getting called after ours. both will create a crashmsg process. This happened as CCrashHandler::End() was called before the in-game exception filter was fired, and therefore CCrashHandler::Handled would return false, and this fire the in-game exception filter. This commit removes the additional check, we just use our vectored exception handler entirely over the game's one, as this one captures everything an unhandled exception handler will capture, and more. The 'Handled' function/fields in CCrashHandler have been renamed to 'Handling', as this is a more appropriate name.
2023-06-19 01:35:10 +02:00
Kawe Mazidjatari
2ae1f88311 Improve crash handler file formatting
Only display hex comment on decimal ALU register if its above 9, and format it as such that it fits perfectly next to a register that is equal or lower than one million. Added additional comments.
2023-06-19 01:18:27 +02:00
Kawe Mazidjatari
0a6fc26529 Comment assert out
Verbose, libcurl free's null blocks.
2023-06-18 12:06:28 +02:00
Kawe Mazidjatari
4f7280ab4c Make inline
These type of functions should always be inline.
2023-06-18 01:25:49 +02:00
Kawe Mazidjatari
982fe66570 Add memory callbacks similar to the game's implementation
Required for overriding memalloc callbacks in libraries to feature the game's implementation instead. Named using the 'R_' prefix, as the 'V_' versions were already used.
2023-06-18 01:16:24 +02:00
Kawe Mazidjatari
3f8baf6f68 Allow setting compile options for specific targets
Previously, it was all controlled from the global init (applied to all projects), but some projects need different options. With these changes, you can disable the common options applied in the 'add_module' macro, and set your own if desired.
2023-06-17 00:45:27 +02:00
Kawe Mazidjatari
975c40876a CModule and CMemory class improvements
This commit significantly reduces output code size, and a noticeable increase in performance. Changes are:
- Removed all extraneous std::string and std::vector copy constructions; use raw pointers instead to boost performance.
- Marked simple getters in CModule inline.
- Marked several functions in CModule const.
- Slightly reordered CModule class.
- 'CMemory::CheckOpCodes' and 'CMemory::Patch' now take a const reference.
2023-06-12 18:40:16 +02:00
Kawe Mazidjatari
8acfad5556 Fix aligned memalloc singleton
Properly fix the aligned memalloc singleton in the SDK; the implementation now uses a callback based approach for calling the allocator and deallocator.
2023-05-15 20:47:17 +02:00
Kawe Mazidjatari
11864f8902 Add all public headers to CMake projects
Added all public headers to CMake projects, also moved some files around in the public directory. Translation units have been moved to the libraries that were responsible for implementing them, as this game is monolithic.
2023-05-15 14:47:03 +02:00
Kawe Mazidjatari
9f44bcc818 Treat compiler warnings as errors
Treat them as errors globally. Most of the time a warning is a bug, or problem in code that could be solved in a different (better) manner. Thirdparty code have this disabled. The warnings as errors option can be globally disabled through the CMake GUI, but this is not recommended.
2023-05-15 09:44:26 +02:00
Kawe Mazidjatari
852d48256e Fix compiler warnings from tier0
Function with internal linkage has no references, and is getting removed.
2023-05-15 00:53:46 +02:00
Kawe Mazidjatari
6a86ae2606 Cleanup CMakeLists files
Light cleanup in CMakeLists files. Split protocols and improve filter names.
2023-05-14 17:36:08 +02:00
Kawe Mazidjatari
d809a9f633 Use PCH generated from VPC globally
This reduced output code size with 40KiB, improved compile times and appeared to improve runtime performance as well.
2023-05-14 02:10:17 +02:00
Kawe Mazidjatari
59d8f970da Cleanup PCH files
Decoupled them to improve compile times.
2023-05-14 01:24:33 +02:00
Kawe Mazidjatari
8dbc2024c6 CMake code improvements
Use the 'add_module' macro to add modules without creating duplicate code. This macro also takes a reuse PCH as parameter, so modules that need a precompiled header, could reuse those from different targets that compile them. This commit also restructures the group order of the generated solution files for easier code navigation.
2023-05-13 00:14:53 +02:00
Kawe Mazidjatari
f120354e96 Initial port to CMake
* All libraries have been isolated from each other, and build into separate artifacts.
* Project has been restructured to support isolating libraries.
* CCrashHandler now calls a callback on crash (setup from core/dllmain.cpp, this can be setup in any way for any project. This callback is getting called when the apllication crashes. Useful for flushing buffers before closing handles to logging files for example).
* Tier0 'CoreMsgV' function now calls a callback sink, which could be set by the user (currently setup to the SDK's internal logger in core/dllmain.cpp).

TODO:
* Add a batch file to autogenerate all projects.
* Add support for dedicated server.
* Add support for client dll.

Bugs:
* Game crashes on the title screen after the UI script compiler has finished (root cause unknown).
* Curl error messages are getting logged twice for the dedicated server due to the removal of all "DEDICATED" preprocessor directives to support isolating projects. This has to be fixed properly!
2023-05-10 00:05:38 +02:00