Commit graph

78 commits

Author SHA1 Message Date
Kamron Batman
b9d63e4160
fix: Fixes ObjectPropertyList double return issue (#1969)
- Fixes double return issue with object property list that is causing corruption.
- Adds DEBUG_ARRAYPOOL define constant which will crash on double return or invalid return scenarios.

> [!IMPORTANT]
> **Developer Notes**
> STArrayPool rented arrays **MUST NOT** be returned **ONLY ONCE** otherwise there will be corruption from double-use.
> Use `DEBUG_ARRAYPOOL` to test potential broken STArrayPool use cases.

> [!NOTE]
> **Why can't I enable the debug all the time?**
> Other than the fact that it will crash due to bad code, the actual tracking system is highly detrimental/problematic for performance and memory consumption by creating objects that have a stack trace.
2024-10-07 21:21:21 -07:00
Kamron Batman
db6e852c91
fix: Fixes reading incoming string packets (#1819) 2024-06-03 14:49:26 -07:00
Kamron Batman
aeba2261d2
fix: Fixes reading multiple strings (books) (#1814) 2024-06-02 15:01:18 -07:00
Kamron Batman
65532ea887
feat: Adds optimized dynamic/static layout gumps (#1652)
# New Gump API
We are pleased to release a new API that is faster, allocates nearly zero memory, and still feels very similar to the original API. The API is broken into 3 types of gumps, dynamic, static with placeholders, and static without placeholders. 

### Dynamic Gumps
These gumps will inherit `DynamicGump` and are meant for gumps that have a dynamic layout. This includes specifying dynamic arguments to HtmlLocalized entries.

## Static Gumps 
Static gumps are those where the function to the build the layout is called only once and cached forever. They can optionally have placeholders. These placeholders allow the developer to specify the string values later, dynamically in a `BuildStrings` method on the gump. If a gump does not have any placeholders, the string entries will also be cached forever.

## Benchmarks
To make sure we were going in the right direction and not wasting time, we took copious benchmarks. Here are the final benchmarks for a really simple gump. 

Note:
* The majority of creating a gump is compressing the layout and the strings. Compressing each section takes ~6,000ns (12us total).

```cs
| Method                         | Mean         | Error        | StdDev       | Median       | Ratio | RatioSD | Gen0   | Allocated | Alloc Ratio |
|------------------------------- |-------------:|-------------:|-------------:|-------------:|------:|--------:|-------:|----------:|------------:|
| OldGump                        | 13,308.29 ns | 1,059.695 ns | 1,883.608 ns | 14,330.72 ns | 1.000 |    0.00 | 0.1526 |    2400 B |        1.00 |
| DynamicLayoutGump              | 13,357.86 ns |   129.144 ns |   226.185 ns | 13,323.60 ns | 1.029 |    0.17 |      - |      48 B |        0.02 |
| StaticLayoutDynamicStringsGump |  6,653.10 ns |    81.815 ns |   143.292 ns |  6,617.45 ns | 0.514 |    0.09 |      - |      40 B |        0.02 |
| StaticLayoutGump               |     86.33 ns |     0.760 ns |     1.350 ns |     86.07 ns | 0.007 |    0.00 | 0.0020 |      32 B |        0.01 |
```

# Non-Breaking Changes
* All gump components in the core have been moved to `Gumps/Legacy`.
* All legacy gumps will still inherit `Gump`, which now inherits `BaseGump`

# Special Thanks

Thank you to @stefanomerotta for considerable contributions/benchmarking/testing to make this effort a reality! We collectively went through over 10 iterations, but it is finally ready.
2024-04-25 22:40:30 -07:00
Kamron Batman
7a160bb0fd
fix: Fixes STArray size and tests (#1737) 2024-04-23 16:18:24 -07:00
Kamron Batman
3a27ab8810
fix: Improves STArray and SpanWriter with packets (#1736) 2024-04-21 20:44:25 -07:00
Kamron Batman
2e4668dbe5
feat: Updates to .NET 8. (#1542)
### Breaking Changes
- Updating to .NET 8 - Required O/S's have slightly changed.
2023-11-14 17:08:09 -08:00
Kamron Batman
1ebaf5390d
fix: Fixes span reader ReadString (#1539) 2023-10-11 13:41:24 -07:00
Kamron Batman
10a69bf754
feat: Adds a memory mirrored ring buffer for networking. (#1533)
## Breaking Changes

Incoming packet registration signature has changed to:
```cs
delegate* void OnReceiveCallback(NetState state, SpanReader reader, int packetLength);

IncomingPackets.Register(int packetID, int length, bool ingame, OnReceiveCallback onReceive);
```

For example, an incoming packet handler signature would now look like this:
```cs
public static void SomeIncomingPacket(NetState state, SpanReader reader, int packetLength)
{
    // Parse the data
}
```

## Summary

Updates the network Pipe class to use a mirrored memory technique. This technique involves mapping the same physical memory to two contiguous virtual memory spaces so the byte buffer appears duplicated. This allows writing to a double-sized array to wrap around without the need for the `CircularBuffer` classes.

In practice this allows us to use `Span<byte>` as if the buffer was a regular array.


### Bug Fixes

- [X] Fixes bad fixed length string parsing
2023-10-09 00:57:53 -07:00
Kamron Batman
8389bfacfe
chore: Updates copyright (#1448) 2023-08-09 09:09:26 -07:00
mdodkins
a9a2a89908
feat: Customize expansion and set maps on first boot (#1425) 2023-07-31 20:46:49 -07:00
Kamron Batman
f24c6a08dd
fix: Fixes string interpolation in value string builder (#1339) 2023-02-13 23:09:50 -08:00
Kamron Batman
e8396d25ec
fix: Fixes ValueStringBuilder infinite loop (#1311) 2022-12-27 16:56:59 -08:00
Kamron Batman
f268d5d4e2
fix: Cleans up core code (#1187)
**Only one functional change**
* Fixes a bug in LogFactory where `Warning` is being logged as `Information`

Non-functional changes:
* Updates/Fixes copyright headers
* Removes namespace scopes for core files.

View with [whitespace off](https://github.com/modernuo/ModernUO/pull/1187/files?w=1).
2022-10-10 21:47:08 -07:00
mark1145
b8146f26f1
fix: Fixes mobile titles not displaying properly (#1149) 2022-08-27 13:31:22 -07:00
Kamron Batman
d6d02de296
fix: Fixes spell mechanics and misc bugs (#1118)
- [X] Fixes NPE from account tags.
- [X] Fixes bad skill check due to missing cast to double.
- [X] Fixes water elemental duration.
- [X] Standardizes spell summon duration by expansion.
2022-07-14 22:01:59 -07:00
Kamron Batman
8303c74379
fix: Fixes ValueStringBuilder initial rented buffer (#1111)
Fixes issue with ValueStringBuilder having an empty initial buffer.
2022-07-03 08:14:54 -07:00
Kamron Batman
2fb660040b
fix: Cleans up namespaces (#1069) 2022-06-15 19:52:23 -07:00
Kamron Batman
5f00330a66
fix: Adds optimized formatter for clilocs (#1045)
Adds an optimized formatter for localization. Example:
```cs
string localizationText = Localization.Format(1050039, "enu", $"{m_Amount}\t{LabelNumber:#}");
```

Fixes #1044
2022-06-04 11:53:32 -07:00
Kamron Batman
ecbee17690
fix: Optimizes OPL using string interpolation (#1041)
## Breaking Changes (New API)
ObjectPropertyList supports the following API:
```cs
list.Add(500000);
list.Add(500001, stringArgument);
list.Add("Some text");
list.Add($"Some text with {argument}");
list.Add(500002, $"{arg1}\t{arg2}");
```

## Notes
1. All API uses that require a formatter like this:
    ```cs
    list.Add(500002, "{0}\t{1}", arg1, arg2);
    ```
    Should be changed to use string interpolation, for example:
    ```cs
    list.Add(500002, $"{arg1}\t{arg2}");
    ```
2. The following paradigm should no longer be used:
    ```cs
    list.Add(1061170, prop.ToString()); // strength requirement ~1_val~
    ```
    The new string interpolation API will avoid having to convert the argument to a string before writing it to the packet. Instead use the following:
    ```cs
    list.Add(1061170, $"{prop}"); // strength requirement ~1_val~
    ```

### Benchmarks
```cs
|                         Method |     Mean |   Error |  StdDev |  Gen 0 | Allocated |
|------------------------------- |---------:|--------:|--------:|-------:|----------:|
|                BenchmarkOldOPL | 241.0 ns | 0.56 ns | 0.47 ns | 0.0105 |      88 B |
| BenchmarkStringInterpolatedOPL | 199.9 ns | 2.44 ns | 2.39 ns |      - |         - |
```

### Changes
- [X] Removes crash in STArray.Return when array is null.
- [X] Fixes NPE in OPL when entity is null. Serial in packet will be 0 when entity is null.
- [X] Fixes NPE in AosAttributes when Parent is null.
- [X] Changes OPL to use string interpolation.
- [X] Introduces `IPropertyList` to allow extending PropertyList for other uses.
2022-06-02 10:09:53 -07:00
Kamron Batman
1e6ab794fe
fix: Fixes ValueStringBuilder dispose incorrectly (#1037) 2022-05-29 15:01:39 -07:00
Kamron Batman
3b7e3c2fb7
fix: Fixes ValueStringBuilder empty ctor issue (#1035) 2022-05-27 17:49:36 -07:00
Stefano Merotta
fe225003c6
fix: Adds multithread optional flag to ValueStringBuilder (#1033)
Added multithread optional flag to ValueStringBuilder for multithread contexts.
Enabled new flag for VerifyType method.

Resolves #1032
2022-05-27 10:25:30 -07:00
Kamron Batman
87b63b38a5
fix: Eliminates string allocations while writing gump packets (#1017)
* Introduces `RawInterpolatedStringHandler` which is exactly the same as `DefaultInterpolatedStringHandler` except it _unsafely exposes_ it's `ReadOnlySpan<char>` buffer. This is useful for writing the string's data without actually building the string.
* Uses this new string interpolation handler in `SpanWriter` to eliminate intermediate strings built. This is immensely useful in eliminating string allocations in writing Gump packets.
2022-05-10 18:50:23 -07:00
Kamron Batman
be7da3a3dc
feat: Adds cliloc support and fixes valuestringbuilder ctor (#1013)
- [X] Adds cliloc support using the following API:
```cs
public static class Localization
{
    string GetText(int number);
    string GetText(int number, string lang);
    string Format(int number, params object[] args);
    string Format(int number, string lang, params object[] args);

    string Format(int number, $"{arg1}{arg2}{arg3}");
    string Format(int number, lang, $"{arg1}{arg2}{arg3}");

    bool TryGetLocalization(int number, out LocalizationEntry entry);
    bool TryGetLocalization(int number, string lang, out LocalizationEntry entry);
}

public class LocalizationEntry
{
    string Language { get; }
    string Number { get; }
    string Text { get; }
    string?[] TextSlices { get; } // Used for string building
    string StringFormatter { get; }

    string Format(params object[] args);
    string Format($"{arg1}{arg2}{arg3}");
}
```

- [X] Fixes a bug with ValueStringBuilder and default initialization size
- [X] Optimizes ValueStringBuilder to use `ISpanFormattable`
- [X] Adds `Append<T>(T value);` support ValueStringBuilder
- [X] Adds `Append($"");` string interpolation support to ValueStringBuider
2022-05-08 21:24:32 -07:00
Kamron Batman
e9f986f55b
fix: Adds Gen2 callback for each STArrayPool (#971)
Adds. Gen2Callback for STArrayPool so it can purge the internal array stacks properly. This only happens if significant timed has passed, 2 Gen 2's have been run, and memory pressure exceeds a threshold.
2022-03-26 13:57:42 -07:00
Kamron Batman
14b63ca48e
fix: Updates ArrayPool to STArrayPool for performance. (#968) 2022-03-22 20:07:32 -07:00
Kamron Batman
2890f5c3ec
fix: Adds packet logging (#836)
### Enabling Packet Logging
`[packetlogging on` and target the user.
Supports command modifiers such as:
`[online packetlogging on where accesslevel = player`

Logs are saved in `path/<ip address>/packets.log`. It is a simple append-format log with no date splitting.
2021-10-31 10:22:22 -07:00
Kamron Batman
0dc4acc164
fix(core): Removes implicit cast between Serial and uint (#728)
* Fixes spellbooks using serial ctor
* Fixes misc items where someone thought they had an amount and it didn't
* Fixes all `Food` types.
2021-08-25 00:27:19 -07:00
Kamron Batman
6e9477ea13
fix(core): Updates to serialization (#590)
- [X] Adds `AdhocPersistence` which replaces RunUO `Persistence`
- [X] Fixes a few minor bugs with EntityPersistence deserialization
- [X] Reverts/updates some serialization changes
2021-05-09 00:10:39 -07:00
Kamron Batman
a0893b68c0
fix(core): Updates slice with range selectors (#583)
- [X] Replaces Span slicing with range selection
- [X] Replaces string slicing with range selection
2021-04-23 20:57:24 -07:00
Kamron Batman
c50322c0e3
fix(network): Adds UOG & Fixes ConnectUO packet (#552)
- [X] Adds UOG Extended and Compact (0xF1 0x51 packet)
- [X] Fixes ConnectUO bad length
2021-03-15 16:18:28 -07:00
Kamron Batman
5e3b42bf9c
fix(network): Adds ConnectUO (#551)
- [X] Adds ability to create new protocol extension packets
- [X] Adds freeshard protocol extension support (packet 0xF1)
- [X] Adds ConnectUO support
- [X] Adds arbitrary read for `CircularBufferReader` and `SpanReader`
2021-03-15 14:14:33 -07:00
Kamron Batman
ec1cc10821
fix(network): Converts House Design Detailed Packet (#538)
- [X] Converts design state detailed packet
- [X] Removes `Packet` class
- [X] Removes `Send(Packet)` function signatures
- [X] Removes `PacketWriter` class
- [X] Updates dependencies.
2021-03-03 02:09:15 -08:00
Kamron Batman
ea5d09a7d7
fix(core): Fixes map issues at New Haven (#509)
- [X] Fixes map issues at New Haven by turning off static diffs
- [X] Some code cleanup and reformatting of tile matrix, tile matrix patch, and tile data
- [X] Adds NetState.Flush for generating spawners so it doesn't feel like the server is frozen
- [X] Reverts NativeReader changes from a while back.
- [X] Adds more string reading for BufferReader.


Notes:
BufferReader is still `little endian` compared to `SpanReader` which is `big endian` (for packets). To that end, the RunUO deserialization `ReadString()` was made obsolete since it is ambiguous, and contains extra fields other than simply reading a string. Furthermore, we shouldn't be using UTF8 (for now) since it is slow.
2021-02-14 16:06:49 -08:00
Kamron Batman
d070efeab3
fix(houses): Converts house packets (1 of 2) (#502)
- [X] Adds `ToSpan()` for SpanWriter.
- [X] Moves house files to UOContent/Multis/Houses
- [X] Adds house packets

### SpanWriter.ToSpan()
`ToSpan()` returns an `SpanOwner` struct which allows access to the buffer as a Span by calling `Span`.

Example:
```cs
public SpanOwner CreatePacket()
{
  var writer = new SpanWriter(0x400);
  // write some stuff
  return writer.ToSpan();
}
```
In this example, we are creating a packet and returning the `SpanOwner` for caching. This means in the future we can do something like this:
```cs
ns.Send(_cachedPacket.Span);
```
Where `_cachedPacket` is the `SpanOwner`

Notes:
Do not use a SpanWriter, or attempt to dispose of it after calling `ToSpan()`. Doing so will probably cause an NPE.
2021-02-13 09:18:12 -08:00
Kamron Batman
952a3e0265
fix(core): Handles a rare NPE with LoginTimer (#508) 2021-02-11 23:47:02 -08:00
Jabin
52df92c452
fix(core): Fixes SpanReader moving position before read (#468) 2021-02-05 23:45:44 -08:00
Kamron Batman
079eeb1b96
fix(core): Fixes nullables being disabled (#466) 2021-02-05 14:09:57 -08:00
Kamron Batman
6f0e1a22f9
fix(core): Core cleanup, adds event loop synchronization, and fixes gumps (#429)
- [X] Cleans up gump packets
- [X] Adds event loop task synchronization
- [X] Moves timer pause and cleans up the delay task timer class
- [X] Cleans up the conserve cpu
- [X] Renames variables to make them consistent with the new style
- [X] Removes process delta recursion checking.
- [X] Properly diposes net states. This fixes edge cases that may cause hanging connections to stay open longer than they should.
- [X] Fixes an issue with gump items not compiling properly

Users can now utilize `Timer.Pause()` since the code will be executed on the proper thread.

```cs
public void async void Talk()
{
    _canTalk = false;
    await Timer.Pause(Utility.RandomMinMax(5000, 8000)); // Talk after 5-8 seconds
    DoTalk();
    await Timer.Pause(Utility.RandomMinMax(12000, 25000)); // Reset ability to talk after 12-25 seconds
    _canTalk = true;
}
```
2021-01-25 21:06:46 -08:00
Kamron Batman
87764ca968
fix(build): Attempt to fix possible nullable warnings as errors with releases (#418) 2021-01-18 11:52:31 -08:00
Kamron Batman
a146955f6c
fix(core): Converts book packets (#413)
- [X] Splits out BaseBook
- [X] Converts book packets
- [X] Makes FixHtml faster
2021-01-16 18:49:53 -08:00
Kamron Batman
c58aad733d
fix(core): Converts BB packets (#406)
- [X] Organizes and splits up bulletin boards
- [X] Converts packets
2021-01-13 22:28:31 -08:00
Kamron Batman
211c2ba8ee
fix(core): Streamlines text encoding (#407)
- [X] Streamlines text encoding
2021-01-13 18:56:32 -08:00
Kamron Batman
5882b1ab3f
fix(core): Fixes OPL packets (#395)
- [X] Fixes an issue with OPL packets using the wrong endianness and not updating the position properly.
- [X] Fixes an issue with SpanWriter not updating bytes written when it is used adhoc.
- [X] Moves packet creation for OPL inside the SendInfoTo function.

Bumps release version
2021-01-08 23:55:23 -08:00
Kamron Batman
dfe89024ca
Makes gump code a little more readable (#390)
- [X] Adds a WriteAscii for individual characters
- [X] Updates gump code to use it so it is a little more readable
2021-01-06 01:17:34 -08:00
Kamron Batman
03bdff2293
fix(content): Converts buff icon packets (#389)
- [X] Converts buff icon packets, and avoids string formatting.
2021-01-06 01:01:21 -08:00
Kamron Batman
30ef0761cb
chore(core): Cleans up networking leftovers from packet conversions (#387)
- [X] Removes span writing extensions
- [X] Removes span reading extensions
- [X] Cleans up OPL and uses uninitialized arrays

Bumps release version
2021-01-05 01:31:52 -08:00
Kamron Batman
a278623f38
fix(core): Converts Gump Packets (v1) (#379)
- [X] Converts gump packets
2021-01-05 01:17:34 -08:00
Kamron Batman
ac76c57c39
fix(core): Converts account packets to spanwriter (#381)
- [X] Converts account packets to spanwriter
2021-01-04 18:47:20 -08:00