Skip to content
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

ColorManager saves value in an incorrect format #3

Closed
WithLithum opened this issue Feb 19, 2021 · 1 comment
Closed

ColorManager saves value in an incorrect format #3

WithLithum opened this issue Feb 19, 2021 · 1 comment
Labels
priority: p1 high A bug that might cause crashes or a highly needed feature status: completed The feature has been completed of the bug has been patched type: bug This is a bug

Comments

@WithLithum
Copy link

WithLithum commented Feb 19, 2021

Sorry for bad English.

To be clear, I'm using the latest release version of the mod, and also the NuGet package for developer reference.

I'm working on a mod called NAL, and replaced internal Money, Weapons Inventory mechanism from NAL's own save file to PlayerCompanion. However, when I'm using

Companion.Colors.Current = Color.FromArgb(255, 0, 114, 188);

and then reload all scripts from console, Companion script crashes. When I looks into log, it shows Newtonsoft.Json caused the exception:

[09:12:16] [ERROR] Failed to instantiate script PlayerCompanion.Companion because constructor threw an exception: System.TypeInitializationException: The type initializer for 'PlayerCompanion.Companion' threw an exception. ---> 
Newtonsoft.Json.JsonReaderException: Error reading JObject from JsonReader. Current JsonReader item is not an object: String. Path '0x3FB5C3D3', line 1, position 27.
   at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings)
   at PlayerCompanion.ColorConverter.ReadJson(JsonReader reader, Type objectType, Color existingValue, Boolean hasExistingValue, JsonSerializer serializer)
   at Newtonsoft.Json.JsonConverter`1.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateDictionary(IDictionary dictionary, JsonReader reader, JsonDictionaryContract contract, JsonProperty containerProperty, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonConverter[] converters)
   at PlayerCompanion.ColorManager..ctor()
   at PlayerCompanion.Companion..cctor()
   --- End of inner exception stacktrace ---
   at PlayerCompanion.Companion..ctor()

When I checks the Colors.json, it looks like this:

{"0xEE79AF16":"0, 114, 188","0x3FB5C3D3":"0, 114, 188"}

It doesn't looks like the new format. I have to manually correct it to

{
    "0xEE79AF16": {
        "r": "0",
        "g": "114",
        "b": "188"
    }
}

to prevent the crash from happening. Some exception messages has been translated to help you better understand this issue.

@justalemon justalemon added priority: p1 high A bug that might cause crashes or a highly needed feature status: acknowledged We are aware of the bug or feature request type: bug This is a bug labels Feb 19, 2021
@justalemon
Copy link
Owner

When the colors are loaded, the custom converter is used.

Dictionary<string, Color> values = JsonConvert.DeserializeObject<Dictionary<string, Color>>(contents, new ColorConverter());

When they are saved, the converter is not used.

File.WriteAllText(Path.Combine(Companion.location, "Colors.json"), JsonConvert.SerializeObject(colors));

Also, a quick warning: Setting the color of the user will permanently override the color that the user has set. If you are trying to set it temporarily, then I should probably make Apply(Color) public.

/// <summary>
/// Applies a color in the HUD:
/// </summary>
/// <param name="color">The color to apply.</param>
internal void Apply(Color color)

@justalemon justalemon added status: completed The feature has been completed of the bug has been patched and removed status: acknowledged We are aware of the bug or feature request labels Feb 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p1 high A bug that might cause crashes or a highly needed feature status: completed The feature has been completed of the bug has been patched type: bug This is a bug
Projects
None yet
Development

No branches or pull requests

2 participants