justalemon / PlayerCompanion

Mod for GTA V that adds some extra features for Addon Peds and the Player Characters
MIT License
12 stars 1 forks source link

ColorManager saves value in an incorrect format #3

Closed WithLithum closed 3 years ago

WithLithum commented 3 years ago

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 commented 3 years ago

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

https://github.com/justalemon/PlayerCompanion/blob/7b0cea5ee0f776280c8c9e2f203ee8dccff08a61/PlayerCompanion/ColorManager.cs#L125

When they are saved, the converter is not used.

https://github.com/justalemon/PlayerCompanion/blob/7b0cea5ee0f776280c8c9e2f203ee8dccff08a61/PlayerCompanion/ColorManager.cs#L163

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.

https://github.com/justalemon/PlayerCompanion/blob/7b0cea5ee0f776280c8c9e2f203ee8dccff08a61/PlayerCompanion/ColorManager.cs#L165-L169