using System; using System.Collections; using System.Collections.Generic; using MoleMole.Config; using UnityEngine; namespace MoleMole { public static class WeaponData { public static Dictionary _weaponIDMap; private static List _configPathList; private static Action _loadJsonConfigCallback = null; private static BackGroundWorker _loadDataBackGroundWorker = new BackGroundWorker(); public static void ReloadFromFile() { _weaponIDMap = new Dictionary(); string[] weaponRegistryPathes = GlobalDataManager.metaConfig.weaponRegistryPathes; foreach (string jsonPath in weaponRegistryPathes) { ConfigWeaponRegistry configWeaponRegistry = ConfigUtil.LoadJSONConfig(jsonPath); foreach (ConfigWeapon item in configWeaponRegistry) { _weaponIDMap.Add(item.WeaponID, item); } } ReloadWeaponConfig(); } public static IEnumerator ReloadFromFileAsync(float progressSpan = 0f, Action moveOneStepCallback = null, Action finishCallback = null) { _loadJsonConfigCallback = finishCallback; _configPathList = new List(); _weaponIDMap = new Dictionary(); string[] pathes = GlobalDataManager.metaConfig.weaponRegistryPathes; if (pathes.Length == 0) { if (_loadJsonConfigCallback != null) { _loadJsonConfigCallback("WeaponData"); _loadJsonConfigCallback = null; } yield break; } string[] array = pathes; foreach (string weaponRegistryPath in array) { _configPathList.Add(weaponRegistryPath); } float step = progressSpan / (float)pathes.Length; _loadDataBackGroundWorker.StartBackGroundWork("WeaponData"); string[] array2 = pathes; foreach (string weaponRegistryPath2 in array2) { AsyncAssetRequst asyncRequest = ConfigUtil.LoadJsonConfigAsync(weaponRegistryPath2); SuperDebug.VeryImportantAssert(asyncRequest != null, "assetRequest is null weaponRegistryPath :" + weaponRegistryPath2); if (asyncRequest != null) { yield return asyncRequest.operation; if (moveOneStepCallback != null) { moveOneStepCallback(step); } ConfigUtil.LoadJSONStrConfigMultiThread(asyncRequest.asset.ToString(), _loadDataBackGroundWorker, OnLoadOneJsonConfigFinish, weaponRegistryPath2); } } } private static void OnLoadOneJsonConfigFinish(ConfigWeaponRegistry weaponList, string configPath) { _configPathList.Remove(configPath); foreach (ConfigWeapon weapon in weaponList) { _weaponIDMap.Add(weapon.WeaponID, weapon); } if (_configPathList.Count == 0) { _loadDataBackGroundWorker.StopBackGroundWork(false); ReloadWeaponConfig(); if (_loadJsonConfigCallback != null) { _loadJsonConfigCallback("WeaponData"); _loadJsonConfigCallback = null; } } } private static void ReloadWeaponConfig() { foreach (ConfigWeapon value in _weaponIDMap.Values) { WeaponMetaData weaponMetaData = WeaponMetaDataReader.TryGetWeaponMetaDataByKey(value.WeaponID); if (weaponMetaData != null) { value.Attach.PrefabPath = weaponMetaData.bodyMod; value.Meta = weaponMetaData; } } } public static ConfigWeapon GetWeaponConfig(int weaponID) { return _weaponIDMap[weaponID]; } public static Dictionary GetAllWeaponConfigs() { return _weaponIDMap; } public static void AddAvatarWeaponAdditionalAbilities(int weaponID, AvatarActor avatar) { ConfigWeapon weaponConfig = GetWeaponConfig(weaponID); for (int i = 0; i < weaponConfig.AdditionalAbilities.Length; i++) { ConfigAbility abilityConfig = AbilityData.GetAbilityConfig(weaponConfig.AdditionalAbilities[i].AbilityName, weaponConfig.AdditionalAbilities[i].AbilityOverride); avatar.CreateAppliedAbility(abilityConfig); if (!string.IsNullOrEmpty(weaponConfig.AdditionalAbilities[i].AbilityReplaceID)) { avatar.abilityIDMap[weaponConfig.AdditionalAbilities[i].AbilityReplaceID] = abilityConfig.AbilityName; } } } public static void WeaponModelAndEffectAttach(int weaponID, string avatarType, BaseMonoAnimatorEntity entity) { ConfigWeapon weaponConfig = GetWeaponConfig(weaponID); Transform transform = Miscs.LoadResource(weaponConfig.Attach.PrefabPath).transform; WeaponAttach.AttachWeaponMesh(weaponConfig, entity, transform, avatarType); MonoEffectOverrideSetting component = transform.GetComponent(); if (component == null && weaponConfig.EffectOverlays == null) { return; } MonoEffectOverride monoEffectOverride = entity.GetComponent(); if (monoEffectOverride == null) { monoEffectOverride = entity.gameObject.AddComponent(); } if (component != null) { for (int i = 0; i < component.materialOverrides.Length; i++) { MaterialOverrideEntry materialOverrideEntry = component.materialOverrides[i]; monoEffectOverride.materialOverrides.Add(materialOverrideEntry.materialOverrideKey, materialOverrideEntry.material); } for (int j = 0; j < component.colorOverrides.Length; j++) { ColorOverrideEntry colorOverrideEntry = component.colorOverrides[j]; monoEffectOverride.colorOverrides.Add(colorOverrideEntry.colorOverrideKey, colorOverrideEntry.color); } for (int k = 0; k < component.floatOverrides.Length; k++) { FloatOverrideEntry floatOverrideEntry = component.floatOverrides[k]; monoEffectOverride.floatOverrides.Add(floatOverrideEntry.floatOverrideKey, floatOverrideEntry.value); } } if (weaponConfig.EffectOverlays.Length > 0) { for (int l = 0; l < weaponConfig.EffectOverlays.Length; l++) { monoEffectOverride.effectOverlays.Add(weaponConfig.EffectOverlays[l].EffectOverrideKey, weaponConfig.EffectOverlays[l].EffectPattern); } } if (weaponConfig.EffectOverrides.Length > 0) { for (int m = 0; m < weaponConfig.EffectOverrides.Length; m++) { monoEffectOverride.effectOverrides.Add(weaponConfig.EffectOverrides[m].EffectOverrideKey, weaponConfig.EffectOverrides[m].EffectPattern); } } weaponConfig.Attach.GetRuntimeWeaponAttachHandler()(weaponConfig, transform, entity, avatarType); } public static int GetFirstWeaponIDForRole(EntityRoleName role) { foreach (ConfigWeapon value in _weaponIDMap.Values) { if (value.OwnerRole == role) { return value.WeaponID; } } return 0; } } }