public void RegisterBuilding(IBuildingConfig config)
    {
        BuildingDef buildingDef = config.CreateBuildingDef();

        configTable[config] = buildingDef;
        GameObject gameObject = UnityEngine.Object.Instantiate(baseTemplate);

        UnityEngine.Object.DontDestroyOnLoad(gameObject);
        gameObject.GetComponent <KPrefabID>().PrefabTag = buildingDef.Tag;
        gameObject.name = buildingDef.PrefabID + "Template";
        gameObject.GetComponent <Building>().Def = buildingDef;
        gameObject.GetComponent <OccupyArea>().OccupiedCellsOffsets = buildingDef.PlacementOffsets;
        if (buildingDef.Deprecated)
        {
            gameObject.GetComponent <KPrefabID>().AddTag(GameTags.DeprecatedContent, false);
        }
        config.ConfigureBuildingTemplate(gameObject, buildingDef.Tag);
        buildingDef.BuildingComplete = BuildingLoader.Instance.CreateBuildingComplete(gameObject, buildingDef);
        bool flag = true;

        for (int i = 0; i < NonBuildableBuildings.Length; i++)
        {
            if (buildingDef.PrefabID == NonBuildableBuildings[i])
            {
                flag = false;
                break;
            }
        }
        if (flag)
        {
            buildingDef.BuildingUnderConstruction      = BuildingLoader.Instance.CreateBuildingUnderConstruction(buildingDef);
            buildingDef.BuildingUnderConstruction.name = GetUnderConstructionName(buildingDef.BuildingUnderConstruction.name);
            buildingDef.BuildingPreview = BuildingLoader.Instance.CreateBuildingPreview(buildingDef);
            GameObject buildingPreview = buildingDef.BuildingPreview;
            buildingPreview.name += "Preview";
        }
        buildingDef.PostProcess();
        config.DoPostConfigureComplete(buildingDef.BuildingComplete);
        if (flag)
        {
            config.DoPostConfigurePreview(buildingDef, buildingDef.BuildingPreview);
            config.DoPostConfigureUnderConstruction(buildingDef.BuildingUnderConstruction);
        }
        Assets.AddBuildingDef(buildingDef);
    }
Esempio n. 2
0
        private static bool RegisterBuilding(BuildingConfigManager __instance, IBuildingConfig config, BuildingDef buildingDef)
        {
            //Debug.Log(config.CreateBuildingDef().PrefabID);
            if (!Helper.Config.Enabled)
            {
                return(true);
            }

            Helper.Log(" === [BuildingModifier] BuildingModifierMod_BuildingConfigManager_RegisterBuilding Prefix === ");

            //CreateBuildingDef
            //BuildingDef buildingDef = config.CreateBuildingDef();
            // Check if there is a config for the current building
            if (!Helper.Config.Modifiers.ContainsKey(buildingDef.PrefabID))
            {
                Helper.Log(" === [BuildingModifier] Ignoring: " + buildingDef.PrefabID);
                return(true);
            }

            Debug.Log(" === [BuildingModifier] Processing: " + buildingDef.PrefabID);

            Helper.Log(" === [BuildingModifier] CreateBuildingDef === ");
            Helper.Process(buildingDef, null);
            buildingDef.GenerateOffsets();  // Remake the offsets after modification


            // Create gameobject
            //configTable[config] = buildingDef;
            ((Dictionary <IBuildingConfig, BuildingDef>)configTableF.GetValue(__instance))[config] = buildingDef;
            //GameObject gameObject = Object.Instantiate(baseTemplate);
            GameObject gameObject = UnityEngine.Object.Instantiate((GameObject)baseTemplateF.GetValue(__instance));

            UnityEngine.Object.DontDestroyOnLoad(gameObject);
            gameObject.GetComponent <KPrefabID>().PrefabTag = buildingDef.Tag;
            gameObject.name = buildingDef.PrefabID + "Template";
            gameObject.GetComponent <Building>().Def = buildingDef;
            gameObject.GetComponent <OccupyArea>().OccupiedCellsOffsets = buildingDef.PlacementOffsets;
            if (buildingDef.Deprecated)
            {
                gameObject.GetComponent <KPrefabID>().AddTag(GameTags.DeprecatedContent);
            }

            //ConfigureBuildingTemplate
            config.ConfigureBuildingTemplate(gameObject, buildingDef.Tag);
            Helper.Log(" === [BuildingModifier] ConfigureBuildingTemplate === ");
            Helper.Process(buildingDef, gameObject);

            //ConfigureBuildingTemplate
            buildingDef.BuildingComplete = BuildingLoader.Instance.CreateBuildingComplete(gameObject, buildingDef);
            Helper.Log(" === [BuildingModifier] CreateBuildingComplete === ");
            Helper.Process(buildingDef, gameObject);

            bool flag = true;

            //for (int i = 0; i < NonBuildableBuildings.Length; i++)
            for (int i = 0; i < ((string[])NonBuildableBuildingsF.GetValue(__instance)).Length; i++)
            {
                //if (buildingDef.PrefabID == NonBuildableBuildings[i])
                if (buildingDef.PrefabID == ((string[])NonBuildableBuildingsF.GetValue(__instance))[i])
                {
                    flag = false;
                    break;
                }
            }

            // Previews
            if (flag)
            {
                buildingDef.BuildingUnderConstruction      = BuildingLoader.Instance.CreateBuildingUnderConstruction(buildingDef);
                buildingDef.BuildingUnderConstruction.name = BuildingConfigManager.GetUnderConstructionName(buildingDef.BuildingUnderConstruction.name);
                buildingDef.BuildingPreview       = BuildingLoader.Instance.CreateBuildingPreview(buildingDef);
                buildingDef.BuildingPreview.name += "Preview";
            }

            buildingDef.PostProcess();

            // Try to avoid errors if the gameobject doesn't have RequiereInputs

            /*
             * if (gameObject.GetComponent<RequireInputs>() == null)
             *  gameObject.AddOrGet<RequireInputs>();
             */

            //DoPostConfigureComplete
            config.DoPostConfigureComplete(buildingDef.BuildingComplete);
            Helper.Log(" === [BuildingModifier] DoPostConfigureComplete === ");
            Helper.Process(buildingDef, gameObject);

            // Previews
            if (flag)
            {
                config.DoPostConfigurePreview(buildingDef, buildingDef.BuildingPreview);
                Helper.Log(" === [BuildingModifier] CreateBuildingUnderConstruction === ");
                Helper.Process(buildingDef, gameObject);

                config.DoPostConfigureUnderConstruction(buildingDef.BuildingUnderConstruction);
                Helper.Log(" === [BuildingModifier] CreateBuildingPreview === ");
                Helper.Process(buildingDef, gameObject);
            }

            Assets.AddBuildingDef(buildingDef);

            return(false);
        }