-
Notifications
You must be signed in to change notification settings - Fork 109
feat: Created a converter for the AttachmentIdentifier structure and a few other minor changes #799
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
base: master
Are you sure you want to change the base?
Changes from all commits
151684c
44fcfd4
9c3b16b
beff5aa
71bffd7
d338816
0cd21ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="AttachmentIdentifierConverter.cs" company="ExMod Team"> | ||
| // Copyright (c) ExMod Team. All rights reserved. | ||
| // Licensed under the CC BY-SA 3.0 license. | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| namespace Exiled.Loader.Features.Configs.CustomConverters | ||
| { | ||
| using System; | ||
| using System.Linq; | ||
|
|
||
| using API.Structs; | ||
|
|
||
| using Exiled.API.Enums; | ||
| using Exiled.API.Features.Items; | ||
|
|
||
| using InventorySystem.Items.Firearms.Attachments; | ||
|
|
||
| using YamlDotNet.Core; | ||
| using YamlDotNet.Core.Events; | ||
| using YamlDotNet.Serialization; | ||
|
|
||
| /// <summary> | ||
| /// Converter <see cref="FirearmType"/> and <see cref="AttachmentName"/> to <see cref="AttachmentIdentifier"/>. | ||
| /// </summary> | ||
| public sealed class AttachmentIdentifierConverter : IYamlTypeConverter | ||
| { | ||
| /// <inheritdoc cref="IYamlTypeConverter" /> | ||
| public bool Accepts(Type type) => type == typeof(AttachmentIdentifier); | ||
|
|
||
| /// <inheritdoc cref="IYamlTypeConverter" /> | ||
| public object ReadYaml(IParser parser, Type type) | ||
| { | ||
| if (!parser.TryConsume(out Scalar scalar)) | ||
| throw new InvalidOperationException($"Expected a scalar for {nameof(AttachmentIdentifier)}."); | ||
|
|
||
| string[] parts = scalar?.Value?.Split(':'); | ||
| if (parts.Length != 3) | ||
| throw new InvalidOperationException($"Invalid AttachmentIdentifier format: '{scalar?.Value}'. Expected \"AttachmentName:AttachmentSlot:AttachmentCode\"."); | ||
|
|
||
| if (!Enum.TryParse(parts[0], true, out AttachmentName attachmentName)) | ||
| throw new InvalidOperationException($"Invalid AttachmentName: '{parts[0]}'."); | ||
|
|
||
| if (!Enum.TryParse(parts[1], true, out AttachmentSlot attachmentSlot)) | ||
| throw new InvalidOperationException($"Invalid AttachmentSlot: '{parts[1]}'."); | ||
|
|
||
| if (!uint.TryParse(parts[2], out uint code)) | ||
| throw new InvalidOperationException($"Invalid AttachmentCode: '{parts[2]}'."); | ||
|
|
||
| return Firearm.AvailableAttachments | ||
| .SelectMany(kvp => kvp.Value) | ||
| .FirstOrDefault(att => att.Name == attachmentName && att.Slot == attachmentSlot && att.Code == code); | ||
| } | ||
|
|
||
| /// <inheritdoc cref="IYamlTypeConverter" /> | ||
| public void WriteYaml(IEmitter emitter, object value, Type type) | ||
| { | ||
| AttachmentIdentifier attId = default; | ||
|
|
||
| if (value is AttachmentIdentifier castAttId) | ||
| attId = castAttId; | ||
|
|
||
| // If anyone ever looks at this code and doesn't understand why it's implemented the way it is, here's an explanation. The problem is that the NW code doesn't provide a way to properly serialize attachments into a string so that this string can then be deserialized back into an object while maintaining integrity. Therefore, literally the only way to obtain an object is to store it as three properties. Storing only "AttachmentName" will cause problems. Storing it as "FirearmType:AttachmentName" will also cause problems. | ||
| // https://discord.com/channels/656673194693885975/1002713309876854924/1488655471697989682 | ||
| emitter.Emit(new Scalar($"{attId.Name}:{attId.Slot}:{attId.Code}")); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="AttachmentIdentifiersConverter.cs" company="ExMod Team"> | ||
| // <copyright file="AttachmentNameConverter.cs" company="ExMod Team"> | ||
| // Copyright (c) ExMod Team. All rights reserved. | ||
| // Licensed under the CC BY-SA 3.0 license. | ||
| // </copyright> | ||
|
|
@@ -22,7 +22,7 @@ namespace Exiled.Loader.Features.Configs.CustomConverters | |
| /// <summary> | ||
| /// Converts a <see cref="IEnumerable{T}"/> of <see cref="AttachmentName"/> to Yaml configs and vice versa. | ||
| /// </summary> | ||
| public sealed class AttachmentIdentifiersConverter : IYamlTypeConverter | ||
| public sealed class AttachmentNameConverter : IYamlTypeConverter | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is another breaking change. Albeit more minor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't affect anything at all. Not only was the name incorrect, it also got in the way of developing a new feature. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If any Exiled plugin whatsoever used this converter in their own code, after this change they'd get a TypeLoadException. So I'm not approving of this PR until it's either set to target Dev, or if these breaking changes are removed |
||
| { | ||
| /// <inheritdoc cref="IYamlTypeConverter" /> | ||
| public bool Accepts(Type type) => type == typeof(AttachmentName); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a breaking change. Either keep this method here or change the target branch to Dev (which will only be released when Exiled 10 comes out)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The existence of this method itself is a critical bug, not the fact that I removed it in this PR. https://discord.com/channels/656673194693885975/1002713309876854924/1488609613258559681 There's not a single reference to this method in the code. It also seems unlikely that anyone has ever used it. Do you really want to keep this bug around until the next version of the game is released?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for taking so long to respond to this. But to answer your question, yes I do want to keep this even if it's buggy as some other plugin could have used this method and then that entire plugin would throw a MissingMethodException whenever it tries to call this