Beispiel #1
0
 /// <summary>
 /// Creates a dummy stub for <seealso cref="ModnixCallback"/> type if api is null.
 /// </summary>
 public static void EnsureAPI(ref ModnixCallback api)
 {
     if (api is null)
     {
         api = (str, obj) => null;
     }
 }
Beispiel #2
0
        /// <summary>
        /// Entry point for Modnix.
        /// </summary>
        public static void MainMod(ModnixCallback api = null)
        {
            PPDefModifier.api = api;
            PPDefLogger.SetLogger(api);

            bool foundMod = false;

            // First search the original config file.
            if (File.Exists(fileName))
            {
                foundMod = true;
                ModFile f = new ModFile(fileName);
                f.ApplyMods();
            }

            // If there is a config directory, process all json files within it.
            if (Directory.Exists(directoryName))
            {
                var files = Directory.GetFiles(directoryName, "*.json", SearchOption.AllDirectories);
                if (files != null)
                {
                    foundMod = true;
                    foreach (var f in files)
                    {
                        ModFile m = new ModFile(f);
                        m.ApplyMods();
                    }
                }
            }

            if (!foundMod)
            {
                logger.Log("PPDefModifier: No config files found.");
            }
        }
        /// <summary>
        /// Called very early, just after main assemblies are loaded, before logos. Saves have not been scanned and most game data are unavailable.
        /// Full info at https://github.com/Sheep-y/Modnix/wiki/DLL-Specs#SplashMod
        /// </summary>
        /// <param name="api">First param (string) is the query/action. Second param (object) and result (object) varies by action.</param>
        public void SplashMod(ModnixCallback api = null)
        {
            // Technically, SplashMod isn't the right place to init this mod. However, it ensures a VERY
            // early load, and it doesn't cause any issues. This means PPDefModifier can modify it.
            harmInst = HarmonyInstance.Create(typeof(MyMod).FullName);
            harmInst.PatchAll();
            FileLog.logPath = "./Mods/PPWB_Harmony.log";
            FileLog.Log("PPWB...");
            BasicUtil.EnsureAPI(ref api);
            BasicUtil.GetConfig(ref Config, api);
            storedAPI = api;

            DefRepository definitions_repo = GameUtl.GameComponent<DefRepository>();
            List<WeaponDef> WeaponList = definitions_repo.GetAllDefs<WeaponDef>().ToList();
            List<DamageKeywordDef> damageKeywords = definitions_repo.GetAllDefs<DamageKeywordDef>().ToList();

            BasicUtil.Log($"Found {WeaponList.Count} weapons loaded into the game.", api);
            BasicUtil.Log($"Found {damageKeywords.Count} damage types loaded into the game.", api);

            #region DamageTypeSetup
            List<DamageKeywordPair> damageKeywordPairsToAdd = new List<DamageKeywordPair>();
            if (Config.add_bleed) {
                damageKeywordPairsToAdd.Add(new DamageKeywordPair() {
                    Value = 0,
                    DamageKeywordDef = damageKeywords.Find(x => x.name.Contains("Bleed"))
                });
            }
            if (Config.add_pierce) {
                damageKeywordPairsToAdd.Add(new DamageKeywordPair() {
                    Value = 0,
                    DamageKeywordDef = damageKeywords.Find(x => x.name.Contains("Pierc")) // Because it's truncated "Piercing"
                });
            }
            if (Config.add_shred) {
                damageKeywordPairsToAdd.Add(new DamageKeywordPair() {
                    Value = 0,
                    DamageKeywordDef = damageKeywords.Find(x => x.name.Contains("Shred"))
                });
            };
            #endregion

            BasicUtil.Log("Adding damage types to all weapons.", api);
            foreach (WeaponDef weapon in WeaponList) {
                foreach (DamageKeywordPair dkp in damageKeywordPairsToAdd) {
                    if (!weapon.DamagePayload.DamageKeywords.Exists(x => x.DamageKeywordDef == dkp.DamageKeywordDef)) {
                        weapon.DamagePayload.DamageKeywords.Add(dkp.Clone());
#if DEBUG
                        BasicUtil.Log($"Had to add damage type {dkp.DamageKeywordDef.Visuals.DisplayName1.Localize()} to {weapon.GetDisplayName().Localize()}", api);
#endif
                    }
                }
                weapon.DamagePayload.DamageKeywords.Sort( (x, y) => x.CompareTo(y));
            }

            BasicUtil.Log("Done adding damage types.", api);
        }
Beispiel #4
0
 /// <summary>
 /// Switch to ModnixLogger if provided api is not <see langword="null"/>.
 /// </summary>
 public static void SetLogger(ModnixCallback api)
 {
     if (api != null && !(_logger[0] is ModnixLogger))
     {
         lock (_logger)
         {
             _logger[0] = new ModnixLogger(api);
         }
     }
 }
Beispiel #5
0
 /// <summary>
 /// Get the config from Modnix or a new, default config.
 /// </summary>
 /// <typeparam name="GenericConfig">Derived config type</typeparam>
 /// <returns>True if the config was able to be loaded from Modnix.</returns>
 public static bool GetConfig <GenericConfig>(ref GenericConfig config, ModnixCallback api)
     where GenericConfig : class, new()
 {
     // api is not passed by ref to GetConfig, so this only ensures it's locally non-null
     EnsureAPI(ref api);
     config = api(ModnixAPIActions.Config.load, new GenericConfig()) as GenericConfig;
     if (config is null)
     {
         config = new GenericConfig();
         return(false);
     }
     return(true);
 }
Beispiel #6
0
 public ModnixLogger(ModnixCallback api)
 {
     this.logger = api("logger", "TraceEventType") as ModnixLogFunction;
 }
Beispiel #7
0
        /// <summary>
        /// Called after basic assets are loaded, before the hottest year cinematic. Virtually the same time as PPML.
        /// Full info at https://github.com/Sheep-y/Modnix/wiki/DLL-Specs#MainMod
        /// </summary>
        /// <param name="api">First param (string) is the query/action. Second param (object) and result (object) varies by action.</param>
        public static void MainMod(ModnixCallback api = null)
        {
            BasicUtil.EnsureAPI(ref api);
            BasicUtil.GetConfig(ref Config, api);

            api("log info", "New MainMod initialized");
            DefRepository gameRootDef = GameUtl.GameComponent <DefRepository>();

            List <TacticalItemDef> tacticalItems = gameRootDef.GetAllDefs <TacticalItemDef>().ToList().FindAll(
                new Predicate <TacticalItemDef>(FilterDefList)
                );

            BasicUtil.Log($"Readied {tacticalItems.Count} Independent tactical items.", api);

            string guid = "";

            I2.Loc.LanguageSourceData langDB = I2.Loc.LocalizationManager.Sources[0];
            int englishKey = langDB.GetLanguageIndex("English");

            foreach (ItemDef item in tacticalItems)
            {
                bool isWeapon = item.GetType() == typeof(WeaponDef);
#if DEBUG
                BasicUtil.Log($"Making Reverse Engineer for: {item.GetDisplayName().Localize()} - {item}", api);
#endif
                ResearchTagDef optional              = (ResearchTagDef)gameRootDef.GetDef("08191866-ff38-9e74-abd7-cb484188911a");
                GeoFactionDef  PhoenixPointFaction   = (GeoFactionDef)gameRootDef.GetDef("8be7e872-0ad2-a2a4-7bee-c980ed304a8a");
                GeoFactionDef  DisciplesOfAnuFaction = (GeoFactionDef)gameRootDef.GetDef("edc6783a-be00-1a84-2b97-2fe1e0fc5448");
                GeoFactionDef  NewJerichoFaction     = (GeoFactionDef)gameRootDef.GetDef("d31c78b9-ff0e-8b94-ab96-9672da73da54");
                GeoFactionDef  SynedrionFaction      = (GeoFactionDef)gameRootDef.GetDef("0e6dc218-e157-5954-c9ab-1a0606e0d914");

                // 1 + length of compatible ammo list
                int       researchUnlockLength = 1 + ((isWeapon) ? ((WeaponDef)item).CompatibleAmmunition.Length : 0);
                ItemDef[] researchUnocks       = new ItemDef[researchUnlockLength];
                researchUnocks[0] = item;
                for (int i = 1; i < researchUnlockLength; i++)
                {
                    researchUnocks[i] = ((WeaponDef)item).CompatibleAmmunition[i - 1];
                }

                string[] guidparts = item.Guid.Split('-');
                string   guidBase  = string.Join("-", guidparts.Take(3));
                string   guidTail  = "deadbeefbabe";
                int      typeInt;

                #region Generate reverse engineering def
                typeInt = (int)ResearchGUIDSegments.ReceiveItemResearchRequirement;
                ReceiveItemResearchRequirementDef rirrDef = ScriptableObject.CreateInstance <ReceiveItemResearchRequirementDef>();
                rirrDef.name    = item.name + "_ReceiveItemResearchRequirementDef";
                rirrDef.Guid    = $"{guidBase}-{typeInt:x4}-{guidTail}";
                rirrDef.ItemDef = item;

                typeInt = (int)ResearchGUIDSegments.ItemResearchCost;
                string I2Key = $"REVERSE_ENGINEER_COSTDEF_{item.name}";
                ItemResearchCostDef ircDef = ScriptableObject.CreateInstance <ItemResearchCostDef>();
                ircDef.name             = item.name + "_ItemResearchCostDef";
                ircDef.Guid             = $"{guidBase}-{typeInt:x4}-{guidTail}";
                ircDef.ItemDef          = item;
                ircDef.Amount           = 1;
                ircDef.LocalizationText = new Base.UI.LocalizedTextBind()
                {
                    LocalizationKey = I2Key
                };
                langDB.AddTerm($"REVERSE_ENGINEER_COSTDEF_{item.name}", I2.Loc.eTermType.Text);
                langDB.GetTermData(I2Key).Languages[englishKey] = "Needed items {0}/{1}";

                typeInt = (int)ResearchGUIDSegments.ManufactureResearchReward;
                ManufactureResearchRewardDef mrdDef = ScriptableObject.CreateInstance <ManufactureResearchRewardDef>();
                mrdDef.name  = item.name + "_ManufactureResearchRewardDef";
                mrdDef.Guid  = $"{guidBase}-{typeInt:x4}-{guidTail}";
                mrdDef.Items = researchUnocks;

                typeInt = (int)ResearchGUIDSegments.Research;
                string      rName = item.name + "_ResearchDef";
                ResearchDef reverseEngineerDef = ScriptableObject.CreateInstance <ResearchDef>();
                reverseEngineerDef.name             = rName;
                reverseEngineerDef.Guid             = $"{guidBase}-{typeInt:X4}-{guidTail}";
                reverseEngineerDef.Id               = rName;
                reverseEngineerDef.Faction          = PhoenixPointFaction;
                reverseEngineerDef.Costs            = new ResearchCostDef[] { ircDef };
                reverseEngineerDef.ResearchCost     = 100;
                reverseEngineerDef.Tags             = new ResearchTagDef[] { optional };
                reverseEngineerDef.ValidForFactions = new List <GeoFactionDef> {
                    PhoenixPointFaction
                };
                reverseEngineerDef.Unlocks       = new ResearchRewardDef[] { mrdDef };
                reverseEngineerDef.InitialStates = new ResearchDef.InitialResearchState[] {
                    new ResearchDef.InitialResearchState {
                        Faction = PhoenixPointFaction,
                        State   = ResearchState.Hidden
                    },
                    new ResearchDef.InitialResearchState {
                        Faction = DisciplesOfAnuFaction,
                        State   = ResearchState.Hidden
                    },
                    new ResearchDef.InitialResearchState {
                        Faction = NewJerichoFaction,
                        State   = ResearchState.Hidden
                    },
                    new ResearchDef.InitialResearchState {
                        Faction = SynedrionFaction,
                        State   = ResearchState.Hidden
                    }
                };
                reverseEngineerDef.RevealRequirements.Container = new ReseachRequirementDefOpContainer[] {
                    new ReseachRequirementDefOpContainer()
                    {
                        Operation    = ResearchContainerOperation.ALL,
                        Requirements = new ResearchRequirementDef[] {
                            rirrDef
                        }
                    }
                };
#if DEBUG
                BasicUtil.Log($"{researchUnocks.Length} items prepared for the rDef.", api);
                BasicUtil.Log(ircDef.LocalizationText.Localize(), api);
#if NOISY
                BasicUtil.Log(reverseEngineerDef.Repr(), api);
#endif
#endif
                #endregion
                gameRootDef.CreateRuntimeDef(rirrDef, rirrDef.GetType(), rirrDef.Guid);
                gameRootDef.CreateRuntimeDef(ircDef, ircDef.GetType(), ircDef.Guid);
                gameRootDef.CreateRuntimeDef(mrdDef, mrdDef.GetType(), mrdDef.Guid);
                gameRootDef.CreateRuntimeDef(reverseEngineerDef, guid: reverseEngineerDef.Guid);
                guid = reverseEngineerDef.Guid;
            }
#if DEBUG
            foreach (GeoFactionDef fact in gameRootDef.GetAllDefs <GeoFactionDef>().ToList())
            {
                BasicUtil.Log(fact.Render(), api);
            }
            BasicUtil.Log($"Looking for GUID: {guid}", api);
            DefRepository temp = GameUtl.GameComponent <DefRepository>();
            ResearchDef   rDef = (ResearchDef)temp.GetDef(guid);
            BasicUtil.Log(rDef.name, api);
            BasicUtil.Log(rDef.Guid, api);
            BasicUtil.Log(rDef.Costs[0].Guid, api);
            BasicUtil.Log(rDef.Unlocks[0].Guid, api);
            BasicUtil.Log(rDef.RevealRequirements.Container[0].Requirements[0].Guid, api);

            BasicUtil.Log(
                rDef.Repr(),
                api);
            BasicUtil.Log(
                // Reverse Engineering example
                ((ResearchDef)temp.GetDef("15d2170b-469b-0341-22d4-a8b4d90eefb8")).Repr(),
                api);
#if COMPAREMANY
            BasicUtil.Log(
                // Capture research
                ((ResearchDef)temp.GetDef("c7b3f8ab-8c90-0343-823b-c966d2c4edb8")).Repr(),
                api);
            BasicUtil.Log(
                // Research that rewards multiple resources
                temp.GetAllDefs <ResearchDef>().ToList().Find(x => x.Resources.Count > 1).Repr(),
                api);
#endif
#endif
        }