コード例 #1
0
        public static void Process(BuildingDef def)
        {
            if (CustomizeBuildingsState.StateManager.State.AdvancedSettings == null)
            {
                return;
            }

            Dictionary <string, Dictionary <string, object> > buildingEntry;
            bool foundBuilding = CustomizeBuildingsState.StateManager.State.AdvancedSettings.TryGetValue(def.PrefabID, out buildingEntry);

            if (foundBuilding)
            {
                Debug.Log("Applying settings for: " + def.PrefabID);
                foreach (KeyValuePair <string, Dictionary <string, object> > componentEntry in buildingEntry)
                {
                    if (componentEntry.Key.Length < 4)
                    {
                        continue;
                    }

                    Type componentType;
                    UnityEngine.Object component;

                    if (componentEntry.Key == "BASE")
                    { //edit BuildingDef instead
                        componentType = def.GetType();
                        component     = def;
                    }
                    else if (componentEntry.Key.StartsWith("ADD:"))
                    { //addicomponent
                        componentType = Type.GetType(componentEntry.Key.Substring(4) + ", Assembly-CSharp", false);
                        if (componentType == null)
                        {
                            PostBootDialog.ErrorList.Add(def.PrefabID + ": component type does not exist: " + componentEntry.Key);
                            continue;
                        }

                        component = def.BuildingComplete.AddComponent(componentType);
                        if (component == null)
                        {
                            PostBootDialog.ErrorList.Add(def.PrefabID + ": could not add component: " + componentEntry.Key);
                            continue;
                        }
                    }
                    else if (componentEntry.Key.StartsWith("DEL:"))
                    { //delete component
                        componentType = Type.GetType(componentEntry.Key.Substring(4) + ", Assembly-CSharp", false);
                        if (componentType == null)
                        {
                            PostBootDialog.ErrorList.Add(def.PrefabID + ": component type does not exist: " + componentEntry.Key);
                            continue;
                        }

                        component = def.BuildingComplete.GetComponent(componentType);
                        if (component == null)
                        {
                            PostBootDialog.ErrorList.Add(def.PrefabID + ": could not get component for deletion: " + componentEntry.Key);
                            continue;
                        }
                        UnityEngine.Object.DestroyImmediate(component);
                        continue;
                    }
                    else if (Regex.IsMatch(componentEntry.Key, "AT.:"))
                    { //edit component at index
                        int index;
                        if (!Int32.TryParse(componentEntry.Key.Substring(2, 2), out index))
                        {
                            PostBootDialog.ErrorList.Add(def.PrefabID + ": index could not be parsed: " + componentEntry.Key);
                            continue;
                        }

                        componentType = Type.GetType(componentEntry.Key + ", Assembly-CSharp", false);
                        if (componentType == null)
                        {
                            PostBootDialog.ErrorList.Add(def.PrefabID + ": component type does not exist: " + componentEntry.Key);
                            continue;
                        }

                        component = def.BuildingComplete.GetComponent(componentType);
                        if (component == null)
                        {
                            PostBootDialog.ErrorList.Add(def.PrefabID + ": does not have component: " + componentEntry.Key);
                            continue;
                        }
                    }
                    else
                    { //edit component
                        componentType = Type.GetType(componentEntry.Key + ", Assembly-CSharp", false);
                        if (componentType == null)
                        {
                            PostBootDialog.ErrorList.Add(def.PrefabID + ": component type does not exist: " + componentEntry.Key);
                            continue;
                        }

                        component = def.BuildingComplete.GetComponent(componentType);
                        if (component == null)
                        {
                            PostBootDialog.ErrorList.Add(def.PrefabID + ": does not have component: " + componentEntry.Key);
                            continue;
                        }
                    }

                    foreach (KeyValuePair <string, object> fieldEntry in componentEntry.Value)
                    {
                        object setValue = fieldEntry.Value;
                        Debug.Log("PROCESSING: value=" + setValue.ToString() + " type=" + setValue.GetType().ToString());


                        if (setValue.GetType() == typeof(double))
                        {
                            setValue = Convert.ToSingle((double)setValue);
                        }
                        else if (setValue.GetType() == typeof(string))
                        {
                            Debug.Log("It's a string.");
                            string strValue = (string)setValue; //convert string in enum if possible

                            int index = strValue.LastIndexOf(".");
                            if (index > 0 && strValue.Length > 2)
                            {
                                string enumName  = strValue.Substring(0, index);
                                string enumConst = strValue.Substring(index + 1);
                                Type   enumType  = Type.GetType(enumName + ", Assembly-CSharp");
                                if (enumType != null)
                                {
                                    Debug.Log("It's an enum.");
                                    try
                                    {
                                        setValue = Enum.Parse(enumType, enumConst);
                                    }
                                    catch (Exception)
                                    {
                                        PostBootDialog.ErrorList.Add("Enum does not exist: " + enumConst);
                                        continue;
                                    }
                                }
                            }
                        }

                        try
                        {
                            //Type valueType = fieldEntry.Value.GetType();
                            AccessTools.Field(componentType, fieldEntry.Key).SetValue(component, setValue);
                        }
                        catch (Exception e)
                        {
                            PostBootDialog.ErrorList.Add(def.PrefabID + ", " + componentEntry.Key + ", " + fieldEntry.Key + " encountered an error: '" + e.Message + "' when trying to write: " + fieldEntry.Value.ToString());
                        }
                    }
                }
                CustomizeBuildingsState.StateManager.State.AdvancedSettings.Remove(def.PrefabID);
            }
        }