コード例 #1
0
 public ModConfigShowcaseDataTypes()
 {
     // Doing the initialization of defaults for reference types in a constructor is also acceptable.
     SomeClassA = new SimpleData()
     {
         percent = .85f
     };
     itemDefinitionExample = new ItemDefinition("Terraria GoldOre");             // EntityDefinition uses ItemID field names rather than the numbers themselves for readability.
 }
コード例 #2
0
        public ModConfigShowcaseDataTypes()
        {
            // Doing the initialization of defaults for reference types in a constructor is also acceptable.
            SomeClassA = new SimpleData()
            {
                percent = .85f
            };

            CustomKey.Add(new ClassUsedAsKey()
            {
                SomeBool   = true,
                SomeNumber = 42
            },
                          new Color(1, 2, 3, 4));

            itemDefinitionExample = new ItemDefinition("Terraria GoldOre");             // EntityDefinition uses ItemID field names rather than the numbers themselves for readability.
        }
コード例 #3
0
        internal void OnDeserializedMethod(StreamingContext context)
        {
            // We use a method marked OnDeserialized to initialize default values of reference types since we can't do that with the DefaultValue attribute.
            if (StringPairDictionary == null)
            {
                StringPairDictionary = new Dictionary <string, Pair>();
            }
            if (JsonItemFloatDictionary == null)
            {
                JsonItemFloatDictionary = new Dictionary <ItemDefinition, float>();
            }
            itemSet           = itemSet ?? new HashSet <ItemDefinition>();
            simpleDataExample = simpleDataExample ?? new SimpleData();
            if (simpleDataExample2 == null)             // This won't auto initialize in UI if set to null via UI
            {
                simpleDataExample2         = new SimpleData();
                simpleDataExample2.boost   = 32;
                simpleDataExample2.percent = 2f;
            }

            complexData = complexData ?? new ComplexData();
            // It is possible for nestedSimple to be null
            //complexData.nestedSimple = complexData.nestedSimple ?? new SimpleData();


            // If you change ModConfig fields between versions, your users might notice their configuration is lost when they update their mod.
            // We can use [JsonExtensionData] to capture un-de-serialized data and manually restore them to new fields.
            // Imagine in a previous version of this mod, we had a field "OldListOfInts" and we want to preserve that data in "ListOfInts".
            // To test this, insert the following into ExampleMod_ModConfigShowcase.json: "OldListOfInts": [ 99, 999],

            /*if (_additionalData.TryGetValue("OldListOfInts", out var token))
             * {
             *      var OldListOfInts = token.ToObject<List<int>>();
             *      if (ListOfInts == null) ListOfInts = new List<int>();
             *      ListOfInts.AddRange(OldListOfInts);
             * }*/
            _additionalData.Clear();             // make sure to clear this or it'll crash.
        }
コード例 #4
0
 internal void OnDeserializedMethod(StreamingContext context)
 {
     // NonNull annotation for UI and post Deserialize?
     nestedSimple = nestedSimple ?? new SimpleData();
 }