diff --git a/project/SamSWAT.HeliCrash/GeneratedContainer.cs b/project/SamSWAT.HeliCrash/GeneratedContainer.cs deleted file mode 100644 index 8418f08..0000000 --- a/project/SamSWAT.HeliCrash/GeneratedContainer.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Newtonsoft.Json.Linq; -using System.Collections.Generic; - -namespace SamSWAT.HeliCrash.Json -{ - public class GeneratedContainer - { - public string Id { get; set; } - public string Root { get; set; } - public List Items { get; set; } - } - - public class ItemInfo - { - public string _id { get; set; } - public string _tpl { get; set; } - public string parentId { get; set; } - public string slotId { get; set; } - public LocationInGrid location { get; set; } - public JObject upd { get; set; } - } -} diff --git a/project/SamSWAT.HeliCrash/HeliCrash.cs b/project/SamSWAT.HeliCrash/HeliCrash.cs index 9c83a1b..9068928 100644 --- a/project/SamSWAT.HeliCrash/HeliCrash.cs +++ b/project/SamSWAT.HeliCrash/HeliCrash.cs @@ -1,82 +1,37 @@ -using Aki.Common.Http; -using Comfort.Common; +using Comfort.Common; using EFT; using EFT.Interactive; -using EFT.InventoryLogic; -using Newtonsoft.Json; -using SamSWAT.HeliCrash.Json; -using System; using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; +using Aki.Custom.Airdrops.Utils; using UnityEngine; namespace SamSWAT.HeliCrash { public class HeliCrash : MonoBehaviour { - private AssetBundle _uh60Bundle; - private LootableContainer _choppaContainer; - private List _resources; - private string _playerLocation; + private AssetBundle _heliBundle; public async void Init(string location) { - _playerLocation = location; + var heliLocation = GetHeliCrashLocation(location); + var choppa = Instantiate(await LoadChoppaAsync(), heliLocation.Position, Quaternion.Euler(heliLocation.Rotation)); + var container = choppa.GetComponentInChildren(); - Location heliLocation = GetHeliCrashLocation(); - var _choppa = Instantiate(await LoadChoppaAsync(), heliLocation.Position, Quaternion.Euler(heliLocation.Rotation)); - _choppaContainer = _choppa.GetComponentInChildren(); + var itemCrate = Utils.CreateItem("goofyahcontainer", "6223349b3136504a544d1608"); + LootItem.CreateLootContainer(container, itemCrate, "Heavy crate", Singleton.Instance); - Item itemCrate = Singleton.Instance.CreateItem("goofyahcontainer", "61a89e5445a2672acf66c877", null); - LootItem.CreateLootContainer(_choppaContainer, itemCrate, "Heavy crate", Singleton.Instance); - - await Task.Run(() => GenerateLoot()); - await Singleton.Instance.LoadBundlesAndCreatePools(PoolManager.PoolsCategory.Raid, PoolManager.AssemblyType.Local, _resources.ToArray(), JobPriority.Low, null, default); + new ItemFactoryUtil().AddLoot(container); } - void OnDestroy() + private void OnDestroy() { - _uh60Bundle.Unload(true); + _heliBundle.Unload(true); } - private void GenerateLoot() + private Location GetHeliCrashLocation(string location) { - var itemFactory = Singleton.Instance; - var json = RequestHandler.GetJson("/client/helicrash/getLoot"); - var akiContainer = JsonConvert.DeserializeObject(json); - _resources = new List(); - - for (int i = 1; i < akiContainer.Items.Count; i++) - { - var item = akiContainer.Items[i]; - - if (item.slotId != "main") - continue; - - Item actualItem; - - if (Array.Exists(itemFactory.SavedPresets, x => x.Encyclopedia == item._tpl)) - { - actualItem = itemFactory.GetPresetItem(item._tpl); - _resources.AddRange(actualItem.GetAllItems().Select(x => x.Template).SelectMany(x => x.AllResources)); - } - else - { - actualItem = itemFactory.CreateItem(item._id, item._tpl, null); - _resources.AddRange(actualItem.Template.AllResources); - } - - if (item.upd != null) - actualItem.StackObjectsCount = item.upd.Value("StackObjectsCount"); - - _choppaContainer.ItemOwner.MainStorage[0].Add(actualItem, item.location, false); - } - } - - private Location GetHeliCrashLocation() - { - switch (_playerLocation) + switch (location) { case "bigmap": { @@ -102,6 +57,10 @@ namespace SamSWAT.HeliCrash { return PickRandom(Plugin.HeliCrashLocations.Lighthouse); } + case "TarkovStreets": + { + return PickRandom(Plugin.HeliCrashLocations.StreetsOfTarkov); + } case "develop": { return PickRandom(Plugin.HeliCrashLocations.Develop); @@ -119,15 +78,15 @@ namespace SamSWAT.HeliCrash while (!bundleLoadRequest.isDone) await Task.Yield(); - _uh60Bundle = bundleLoadRequest.assetBundle; + _heliBundle = bundleLoadRequest.assetBundle; - if (_uh60Bundle == null) + if (_heliBundle == null) { Debug.LogError("[SamSWAT.HeliCrash]: Can't load UH-60 Blackhawk bundle"); return null; } - var assetLoadRequest = _uh60Bundle.LoadAllAssetsAsync(); + var assetLoadRequest = _heliBundle.LoadAllAssetsAsync(); while (!assetLoadRequest.isDone) await Task.Yield(); @@ -145,7 +104,7 @@ namespace SamSWAT.HeliCrash private T PickRandom(List list) { - return list[UnityEngine.Random.Range(0, list.Count)]; + return list[Random.Range(0, list.Count)]; } } } diff --git a/project/SamSWAT.HeliCrash/HeliCrashLocations.cs b/project/SamSWAT.HeliCrash/HeliCrashLocations.cs index 7537295..0b72622 100644 --- a/project/SamSWAT.HeliCrash/HeliCrashLocations.cs +++ b/project/SamSWAT.HeliCrash/HeliCrashLocations.cs @@ -17,6 +17,7 @@ namespace SamSWAT.HeliCrash public List Lighthouse { get; set; } public List Rezerv { get; set; } public List Shoreline { get; set; } + public List StreetsOfTarkov { get; set; } public List Develop { get; set; } } } diff --git a/project/SamSWAT.HeliCrash/HeliCrashPatch.cs b/project/SamSWAT.HeliCrash/HeliCrashPatch.cs index 0d44b2c..36b5507 100644 --- a/project/SamSWAT.HeliCrash/HeliCrashPatch.cs +++ b/project/SamSWAT.HeliCrash/HeliCrashPatch.cs @@ -13,7 +13,7 @@ namespace SamSWAT.HeliCrash { protected override MethodBase GetTargetMethod() { - return PatchConstants.LocalGameType.BaseType.GetMethod("method_11", BindingFlags.NonPublic | BindingFlags.Instance); + return PatchConstants.LocalGameType.BaseType.GetMethod("method_10", BindingFlags.NonPublic | BindingFlags.Instance); } [PatchPostfix] diff --git a/project/SamSWAT.HeliCrash/Plugin.cs b/project/SamSWAT.HeliCrash/Plugin.cs index 3fbe4cb..d070475 100644 --- a/project/SamSWAT.HeliCrash/Plugin.cs +++ b/project/SamSWAT.HeliCrash/Plugin.cs @@ -5,7 +5,7 @@ using System.IO; namespace SamSWAT.HeliCrash { - [BepInPlugin("com.SamSWAT.HeliCrash", "SamSWAT.HeliCrash", "1.2.0")] + [BepInPlugin("com.SamSWAT.HeliCrash", "SamSWAT.HeliCrash", "2.0.0")] public class Plugin : BaseUnityPlugin { public static HeliCrashLocations HeliCrashLocations; @@ -16,7 +16,7 @@ namespace SamSWAT.HeliCrash { Directory = Path.Combine(BepInEx.Paths.PluginPath, "SamSWAT.HeliCrash/").Replace("\\", "/"); new HeliCrashPatch().Enable(); - var json = new StreamReader(Directory + "HeliCrashLocations.json").ReadToEnd(); + var json = File.ReadAllText(Directory + "HeliCrashLocations.json"); HeliCrashLocations = JsonConvert.DeserializeObject(json); HeliCrashChance = Config.Bind( diff --git a/project/SamSWAT.HeliCrash/References/aki-custom.dll b/project/SamSWAT.HeliCrash/References/aki-custom.dll new file mode 100644 index 0000000..da8345c Binary files /dev/null and b/project/SamSWAT.HeliCrash/References/aki-custom.dll differ diff --git a/project/SamSWAT.HeliCrash/SamSWAT.HeliCrash.csproj b/project/SamSWAT.HeliCrash/SamSWAT.HeliCrash.csproj index 6731128..8c6de5f 100644 --- a/project/SamSWAT.HeliCrash/SamSWAT.HeliCrash.csproj +++ b/project/SamSWAT.HeliCrash/SamSWAT.HeliCrash.csproj @@ -31,9 +31,8 @@ 4 - - False - References\Aki.Common.dll + + References\aki-custom.dll References\Aki.Reflection.dll @@ -63,12 +62,12 @@ - + \ No newline at end of file diff --git a/project/SamSWAT.HeliCrash/Utils.cs b/project/SamSWAT.HeliCrash/Utils.cs new file mode 100644 index 0000000..f3934e3 --- /dev/null +++ b/project/SamSWAT.HeliCrash/Utils.cs @@ -0,0 +1,131 @@ +using System; +using System.Linq; +using System.Reflection; +using System.Reflection.Emit; +using Comfort.Common; +using EFT.InventoryLogic; + +namespace SamSWAT.HeliCrash +{ + public static class Utils + { + private static CreateItemDelegate _createItemDelegate; + private delegate Item CreateItemDelegate(object instance, string id, string tplId, object diff = null); + + static Utils() + { + var createItemMethod = typeof(ItemFactory).GetMethod("CreateItem"); + _createItemDelegate = MethodDelegate(createItemMethod, false); + } + + public static Item CreateItem(string id, string tplId) + { + return _createItemDelegate(Singleton.Instance, id, tplId); + } + + //stolen from kmyuhkyuk + private static DelegateType MethodDelegate(MethodInfo method, bool virtualCall = true) where DelegateType : Delegate + { + if (method == null) + { + throw new ArgumentNullException(nameof(method)); + } + + var delegateType = typeof(DelegateType); + + var declaringType = method.DeclaringType; + + var delegateMethod = delegateType.GetMethod("Invoke"); + var delegateParameters = delegateMethod.GetParameters(); + var delegateParameterTypes = delegateParameters.Select(x => x.ParameterType).ToArray(); + + Type returnType; + bool needBox; + + if (delegateMethod.ReturnType == typeof(object) && method.ReturnType.IsValueType) + { + returnType = typeof(object); + + needBox = true; + } + else + { + returnType = method.ReturnType; + + needBox = false; + } + + var dmd = new DynamicMethod("OpenInstanceDelegate_" + method.Name, returnType, delegateParameterTypes); + + var ilGen = dmd.GetILGenerator(); + + Type[] parameterTypes; + int num; + + if (!method.IsStatic) + { + var parameters = method.GetParameters(); + var numParameters = parameters.Length; + parameterTypes = new Type[numParameters + 1]; + parameterTypes[0] = typeof(object); + + for (int i = 0; i < numParameters; i++) + { + parameterTypes[i + 1] = parameters[i].ParameterType; + } + + if (declaringType != null && declaringType.IsValueType) + { + ilGen.Emit(OpCodes.Ldarga_S, 0); + } + else + { + ilGen.Emit(OpCodes.Ldarg_0); + } + + ilGen.Emit(OpCodes.Castclass, declaringType); + + num = 1; + } + else + { + parameterTypes = method.GetParameters().Select(x => x.ParameterType).ToArray(); + + num = 0; + } + + for (int i = num; i < parameterTypes.Length; i++) + { + ilGen.Emit(OpCodes.Ldarg, i); + var parameterType = parameterTypes[i]; + var isValueType = parameterType.IsValueType; + if (!isValueType) + { + ilGen.Emit(OpCodes.Castclass, parameterType); + } + else if (delegateParameterTypes[i] == typeof(object)) + { + ilGen.Emit(OpCodes.Unbox_Any, parameterType); + } + } + + if (method.IsStatic || !virtualCall) + { + ilGen.Emit(OpCodes.Call, method); + } + else + { + ilGen.Emit(OpCodes.Callvirt, method); + } + + if (needBox) + { + ilGen.Emit(OpCodes.Box, method.ReturnType); + } + + ilGen.Emit(OpCodes.Ret); + + return (DelegateType)dmd.CreateDelegate(delegateType); + } + } +} \ No newline at end of file