<commit_before>﻿using ModComponentAPI;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace ModComponentMapper
{
    public class MappedItem
    {
        private GameObject gameObject;

        internal MappedItem(GameObject gameObject)
        {
            this.gameObject = gameObject;
        }

        public MappedItem AddToLootTable(LootTableName lootTableName, int weight)
        {
            LootTableEntry entry = new LootTableEntry();
            entry.PrefabName = gameObject.name;
            entry.Weight = weight;
            GearSpawner.AddLootTableEntry(lootTableName.ToString(), entry);

            return this;
        }

        public MappedItem RegisterInConsole(string displayName)
        {
            ModUtils.RegisterConsoleGearName(displayName, gameObject.name);

            return this;
        }

        public MappedItem SpawnAt(SceneName sceneName, Vector3 position, Quaternion rotation, float chance = 100)
        {
            GearSpawnInfo spawnInfo = new GearSpawnInfo();
            spawnInfo.PrefabName = gameObject.name;
            spawnInfo.Position = position;
            spawnInfo.Rotation = rotation;
            spawnInfo.SpawnChance = chance;
            GearSpawner.AddGearSpawnInfo(sceneName.ToString(), spawnInfo);

            return this;
        }
    }

    public class Mapper
    {
        private static List<ModComponent> mappedItems = new List<ModComponent>();

        public static MappedItem Map(string prefabName)
        {
            return Map((GameObject)Resources.Load(prefabName));
        }

        public static MappedItem Map(GameObject prefab)
        {
            if (prefab == null)
            {
                throw new ArgumentException("The prefab was NULL.");
            }

            ModComponent modComponent = ModUtils.GetModComponent(prefab);
            if (modComponent == null)
            {
                throw new ArgumentException("Prefab " + prefab.name + " does not contain a ModComponent.");
            }

            if (prefab.GetComponent<GearItem>() == null)
            {
                Log("Mapping {0}", prefab.name);

                ConfigureInspect(modComponent);
                ConfigureHarvestable(modComponent);
                ConfigureRepairable(modComponent);

                ConfigureEquippable(modComponent);
                ConfigureFood(modComponent);
                ConfigureCookable(modComponent);
                ConfigureRifle(modComponent);
                ConfigureClothing(modComponent);
                ConfigureGearItem(modComponent);

                mappedItems.Add(modComponent);
            }

            PostProcess(modComponent);

            return new MappedItem(prefab);
        }

        private static void ConfigureClothing(ModComponent modComponent)
        {
            ModClothingComponent modClothingItem = modComponent as ModClothingComponent;
            if (modClothingItem == null)
            {
                return;
            }

            ClothingItem clothingItem = ModUtils.GetOrCreateComponent<ClothingItem>(modClothingItem);

            clothingItem.m_DailyHPDecayWhenWornInside = GetDailyDecay(modClothingItem.DaysToDecayWornInside, modClothingItem.MaxHP);
            clothingItem.m_DailyHPDecayWhenWornOutside = GetDailyDecay(modClothingItem.DaysToDecayWornOutside, modClothingItem.MaxHP);
            clothingItem.m_DryBonusWhenNotWorn = 1.5f;
            clothingItem.m_DryPercentPerHour = 100f / modClothingItem.HoursToDryNearFire;
            clothingItem.m_DryPercentPerHourNoFire = 100f / modClothingItem.HoursToDryWithoutFire;
            clothingItem.m_FreezePercentPerHour = 100f / modClothingItem.HoursToFreeze;

            clothingItem.m_Region = ModUtils.TranslateEnumValue<ClothingRegion, Region>(modClothingItem.Region);
            clothingItem.m_MaxLayer = ModUtils.TranslateEnumValue<ClothingLayer, Layer>(modClothingItem.MaxLayer);
            clothingItem.m_MinLayer = ModUtils.TranslateEnumValue<ClothingLayer, Layer>(modClothingItem.MinLayer);
            clothingItem.m_FootwearType = ModUtils.TranslateEnumValue<FootwearType, Footwear>(modClothingItem.Footwear);
            clothingItem.m_WornMovementSoundCategory = ModUtils.TranslateEnumValue<ClothingMovementSound, MovementSound>(modClothingItem.MovementSound);

            clothingItem.m_PaperDollTextureName = modClothingItem.MainTexture;
            clothingItem.m_PaperDollBlendmapName = modClothingItem.BlendTexture;

            clothingItem.m_Warmth = modClothingItem.Warmth;
            clothingItem.m_WarmthWhenWet = modClothingItem.WarmthWhenWet;
            clothingItem.m_Waterproofness = modClothingItem.Waterproofness / 100f;
            clothingItem.m_Windproof = modClothingItem.Windproof;
            clothingItem.m_SprintBarReductionPercent = modClothingItem.SprintBarReduction;
            clothingItem.m_Toughness = modClothingItem.Toughness;
        }

        private static void ConfigureCookable(ModComponent modComponent)
        {
            ModCookableComponent modCookableComponent = modComponent as ModCookableComponent;
            if (modCookableComponent == null || !modCookableComponent.Cooking)
            {
                return;
            }

            Cookable cookable = ModUtils.GetOrCreateComponent<Cookable>(modCookableComponent);

            cookable.m_CookTimeMinutes = modCookableComponent.CookingMinutes;
            cookable.m_NumUnitsRequired = modCookableComponent.CookingUnitsRequired;
            cookable.m_PotableWaterRequiredLiters = modCookableComponent.CookingWaterRequired;
            cookable.m_CookAudio = ModUtils.DefaultIfEmpty(modCookableComponent.CookingAudio, "PLAY_BOILINGLIGHT");

            // either just heat or convert, but not both
            if (modCookableComponent.CookingResult == null)
            {
                // no conversion, just heating
                FoodItem foodItem = ModUtils.GetComponent<FoodItem>(modCookableComponent);
                if (foodItem != null)
                {
                    foodItem.m_HeatedWhenCooked = true;
                }
            }
            else
            {
                // no heating, but instead convert the item when cooking completes
                GearItem cookedGearItem = modCookableComponent.CookingResult.GetComponent<GearItem>();
                if (cookedGearItem == null)
                {
                    // not mapped yet, do it now
                    Mapper.Map(modCookableComponent.CookingResult);
                    cookedGearItem = modCookableComponent.CookingResult.GetComponent<GearItem>();
                }

                if (cookedGearItem == null)
                {
                    throw new ArgumentException("CookingResult does not map to GearItem for prefab " + modCookableComponent.name);
                }

                cookable.m_CookedPrefab = cookedGearItem;
            }
        }

        private static FirstPersonItem ConfiguredRifleFirstPersonItem(ModRifleComponent modRifleComponent)
        {
            FirstPersonItem result = ModUtils.GetOrCreateComponent<FirstPersonItem>(modRifleComponent);

            FirstPersonItem template = Resources.Load<GameObject>("GEAR_Rifle").GetComponent<FirstPersonItem>();

            result.m_FirstPersonObjectName = ModUtils.NormalizeName(modRifleComponent.name);
            result.m_UnWieldAudio = template.m_UnWieldAudio;
            result.m_WieldAudio = template.m_WieldAudio;
            result.m_PlayerStateTransitions = UnityEngine.Object.Instantiate(template.m_PlayerStateTransitions);
            result.Awake();

            return result;
        }

        private static void ConfigureEquippable(ModComponent modComponent)
        {
            EquippableModComponent equippableModComponent = modComponent as EquippableModComponent;
            if (equippableModComponent == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(equippableModComponent.InventoryActionLocalizationId))
            {
                equippableModComponent.InventoryActionLocalizationId = "GAMEPLAY_Equip";
            }
        }

        private static void ConfigureFood(ModComponent modComponent)
        {
            ModFoodComponent modFoodComponent = modComponent as ModFoodComponent;
            if (modFoodComponent == null)
            {
                return;
            }

            FoodItem foodItem = ModUtils.GetOrCreateComponent<FoodItem>(modFoodComponent);

            foodItem.m_CaloriesTotal = modFoodComponent.Calories;
            foodItem.m_CaloriesRemaining = modFoodComponent.Calories;
            foodItem.m_ReduceThirst = modFoodComponent.ThirstEffect;

            foodItem.m_ChanceFoodPoisoning = Mathf.Clamp01(modFoodComponent.FoodPoisoning / 100f);
            foodItem.m_ChanceFoodPoisoningLowCondition = Mathf.Clamp01(modFoodComponent.FoodPoisoningLowCondition / 100f);
            foodItem.m_DailyHPDecayInside = modFoodComponent.DaysToDecayIndoors > 0 ? modFoodComponent.MaxHP / modFoodComponent.DaysToDecayIndoors : 0;
            foodItem.m_DailyHPDecayOutside = modFoodComponent.DaysToDecayOutdoors > 0 ? modFoodComponent.MaxHP / modFoodComponent.DaysToDecayOutdoors : 0;

            foodItem.m_TimeToEatSeconds = Mathf.Clamp(1, modFoodComponent.EatingTime, 10);<commit_msg>only set a default equip action when no implementation is provided<commit_after>﻿using ModComponentAPI;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace ModComponentMapper
{
    public class MappedItem
    {
        private GameObject gameObject;

        internal MappedItem(GameObject gameObject)
        {
            this.gameObject = gameObject;
        }

        public MappedItem AddToLootTable(LootTableName lootTableName, int weight)
        {
            LootTableEntry entry = new LootTableEntry();
            entry.PrefabName = gameObject.name;
            entry.Weight = weight;
            GearSpawner.AddLootTableEntry(lootTableName.ToString(), entry);

            return this;
        }

        public MappedItem RegisterInConsole(string displayName)
        {
            ModUtils.RegisterConsoleGearName(displayName, gameObject.name);

            return this;
        }

        public MappedItem SpawnAt(SceneName sceneName, Vector3 position, Quaternion rotation, float chance = 100)
        {
            GearSpawnInfo spawnInfo = new GearSpawnInfo();
            spawnInfo.PrefabName = gameObject.name;
            spawnInfo.Position = position;
            spawnInfo.Rotation = rotation;
            spawnInfo.SpawnChance = chance;
            GearSpawner.AddGearSpawnInfo(sceneName.ToString(), spawnInfo);

            return this;
        }
    }

    public class Mapper
    {
        private static List<ModComponent> mappedItems = new List<ModComponent>();

        public static MappedItem Map(string prefabName)
        {
            return Map((GameObject)Resources.Load(prefabName));
        }

        public static MappedItem Map(GameObject prefab)
        {
            if (prefab == null)
            {
                throw new ArgumentException("The prefab was NULL.");
            }

            ModComponent modComponent = ModUtils.GetModComponent(prefab);
            if (modComponent == null)
            {
                throw new ArgumentException("Prefab " + prefab.name + " does not contain a ModComponent.");
            }

            if (prefab.GetComponent<GearItem>() == null)
            {
                Log("Mapping {0}", prefab.name);

                ConfigureInspect(modComponent);
                ConfigureHarvestable(modComponent);
                ConfigureRepairable(modComponent);

                ConfigureEquippable(modComponent);
                ConfigureFood(modComponent);
                ConfigureCookable(modComponent);
                ConfigureRifle(modComponent);
                ConfigureClothing(modComponent);
                ConfigureGearItem(modComponent);

                mappedItems.Add(modComponent);
            }

            PostProcess(modComponent);

            return new MappedItem(prefab);
        }

        private static void ConfigureClothing(ModComponent modComponent)
        {
            ModClothingComponent modClothingItem = modComponent as ModClothingComponent;
            if (modClothingItem == null)
            {
                return;
            }

            ClothingItem clothingItem = ModUtils.GetOrCreateComponent<ClothingItem>(modClothingItem);

            clothingItem.m_DailyHPDecayWhenWornInside = GetDailyDecay(modClothingItem.DaysToDecayWornInside, modClothingItem.MaxHP);
            clothingItem.m_DailyHPDecayWhenWornOutside = GetDailyDecay(modClothingItem.DaysToDecayWornOutside, modClothingItem.MaxHP);
            clothingItem.m_DryBonusWhenNotWorn = 1.5f;
            clothingItem.m_DryPercentPerHour = 100f / modClothingItem.HoursToDryNearFire;
            clothingItem.m_DryPercentPerHourNoFire = 100f / modClothingItem.HoursToDryWithoutFire;
            clothingItem.m_FreezePercentPerHour = 100f / modClothingItem.HoursToFreeze;

            clothingItem.m_Region = ModUtils.TranslateEnumValue<ClothingRegion, Region>(modClothingItem.Region);
            clothingItem.m_MaxLayer = ModUtils.TranslateEnumValue<ClothingLayer, Layer>(modClothingItem.MaxLayer);
            clothingItem.m_MinLayer = ModUtils.TranslateEnumValue<ClothingLayer, Layer>(modClothingItem.MinLayer);
            clothingItem.m_FootwearType = ModUtils.TranslateEnumValue<FootwearType, Footwear>(modClothingItem.Footwear);
            clothingItem.m_WornMovementSoundCategory = ModUtils.TranslateEnumValue<ClothingMovementSound, MovementSound>(modClothingItem.MovementSound);

            clothingItem.m_PaperDollTextureName = modClothingItem.MainTexture;
            clothingItem.m_PaperDollBlendmapName = modClothingItem.BlendTexture;

            clothingItem.m_Warmth = modClothingItem.Warmth;
            clothingItem.m_WarmthWhenWet = modClothingItem.WarmthWhenWet;
            clothingItem.m_Waterproofness = modClothingItem.Waterproofness / 100f;
            clothingItem.m_Windproof = modClothingItem.Windproof;
            clothingItem.m_SprintBarReductionPercent = modClothingItem.SprintBarReduction;
            clothingItem.m_Toughness = modClothingItem.Toughness;
        }

        private static void ConfigureCookable(ModComponent modComponent)
        {
            ModCookableComponent modCookableComponent = modComponent as ModCookableComponent;
            if (modCookableComponent == null || !modCookableComponent.Cooking)
            {
                return;
            }

            Cookable cookable = ModUtils.GetOrCreateComponent<Cookable>(modCookableComponent);

            cookable.m_CookTimeMinutes = modCookableComponent.CookingMinutes;
            cookable.m_NumUnitsRequired = modCookableComponent.CookingUnitsRequired;
            cookable.m_PotableWaterRequiredLiters = modCookableComponent.CookingWaterRequired;
            cookable.m_CookAudio = ModUtils.DefaultIfEmpty(modCookableComponent.CookingAudio, "PLAY_BOILINGLIGHT");

            // either just heat or convert, but not both
            if (modCookableComponent.CookingResult == null)
            {
                // no conversion, just heating
                FoodItem foodItem = ModUtils.GetComponent<FoodItem>(modCookableComponent);
                if (foodItem != null)
                {
                    foodItem.m_HeatedWhenCooked = true;
                }
            }
            else
            {
                // no heating, but instead convert the item when cooking completes
                GearItem cookedGearItem = modCookableComponent.CookingResult.GetComponent<GearItem>();
                if (cookedGearItem == null)
                {
                    // not mapped yet, do it now
                    Mapper.Map(modCookableComponent.CookingResult);
                    cookedGearItem = modCookableComponent.CookingResult.GetComponent<GearItem>();
                }

                if (cookedGearItem == null)
                {
                    throw new ArgumentException("CookingResult does not map to GearItem for prefab " + modCookableComponent.name);
                }

                cookable.m_CookedPrefab = cookedGearItem;
            }
        }

        private static FirstPersonItem ConfiguredRifleFirstPersonItem(ModRifleComponent modRifleComponent)
        {
            FirstPersonItem result = ModUtils.GetOrCreateComponent<FirstPersonItem>(modRifleComponent);

            FirstPersonItem template = Resources.Load<GameObject>("GEAR_Rifle").GetComponent<FirstPersonItem>();

            result.m_FirstPersonObjectName = ModUtils.NormalizeName(modRifleComponent.name);
            result.m_UnWieldAudio = template.m_UnWieldAudio;
            result.m_WieldAudio = template.m_WieldAudio;
            result.m_PlayerStateTransitions = UnityEngine.Object.Instantiate(template.m_PlayerStateTransitions);
            result.Awake();

            return result;
        }

        private static void ConfigureEquippable(ModComponent modComponent)
        {
            EquippableModComponent equippableModComponent = modComponent as EquippableModComponent;
            if (equippableModComponent == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(equippableModComponent.InventoryActionLocalizationId) && string.IsNullOrEmpty(equippableModComponent.ImplementationType))
            {
                equippableModComponent.InventoryActionLocalizationId = "GAMEPLAY_Equip";
            }
        }

        private static void ConfigureFood(ModComponent modComponent)
        {
            ModFoodComponent modFoodComponent = modComponent as ModFoodComponent;
            if (modFoodComponent == null)
            {
                return;
            }

            FoodItem foodItem = ModUtils.GetOrCreateComponent<FoodItem>(modFoodComponent);

            foodItem.m_CaloriesTotal = modFoodComponent.Calories;
            foodItem.m_CaloriesRemaining = modFoodComponent.Calories;
            foodItem.m_ReduceThirst = modFoodComponent.ThirstEffect;

            foodItem.m_ChanceFoodPoisoning = Mathf.Clamp01(modFoodComponent.FoodPoisoning / 100f);
            foodItem.m_ChanceFoodPoisoningLowCondition = Mathf.Clamp01(modFoodComponent.FoodPoisoningLowCondition / 100f);
            foodItem.m_DailyHPDecayInside = modFoodComponent.DaysToDecayIndoors > 0 ? modFoodComponent.MaxHP / modFoodComponent.DaysToDecayIndoors : 0;
            foodItem.m_DailyHPDecayOutside = modFoodComponent.DaysToDecayOutdoors > 0 ? modFoodComponent.MaxHP / modFoodComponent.DaysToDecayOutdoors : 0;

            foodItem.m_TimeToEatSeconds = Mathf.Clamp(1, modFoodComponent.EatingTime, 10);
{'fraction_non_alphanumeric': 0.0666384150863439, 'fraction_numerical': 0.0038139633435745313, 'mean_word_length': 2.388799138395261, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2953376', 'n_tokens_mistral': 5218, 'n_tokens_neox': 4880, 'n_words': 774}