2022-09-02 02:36:59 +03:00

152 lines
5.2 KiB
C#

using Aki.Common.Http;
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 UnityEngine;
namespace SamSWAT.HeliCrash
{
public class HeliCrash : MonoBehaviour
{
private AssetBundle _uh60Bundle;
private LootableContainer _choppaContainer;
private List<ResourceKey> _resources;
private string _playerLocation;
public async void Init(string location)
{
_playerLocation = location;
Location heliLocation = GetHeliCrashLocation();
var _choppa = Instantiate(await LoadChoppaAsync(), heliLocation.Position, Quaternion.Euler(heliLocation.Rotation));
_choppaContainer = _choppa.GetComponentInChildren<LootableContainer>();
Item itemCrate = Singleton<ItemFactory>.Instance.CreateItem("goofyahcontainer", "61a89e5445a2672acf66c877", null);
LootItem.CreateLootContainer(_choppaContainer, itemCrate, "Heavy crate", Singleton<GameWorld>.Instance);
await Task.Run(() => GenerateLoot());
await Singleton<PoolManager>.Instance.LoadBundlesAndCreatePools(PoolManager.PoolsCategory.Raid, PoolManager.AssemblyType.Local, _resources.ToArray(), JobPriority.Low, null, default);
}
void OnDestroy()
{
_uh60Bundle.Unload(true);
}
private void GenerateLoot()
{
var itemFactory = Singleton<ItemFactory>.Instance;
var json = RequestHandler.GetJson("/client/helicrash/getLoot");
var akiContainer = JsonConvert.DeserializeObject<GeneratedContainer>(json);
_resources = new List<ResourceKey>();
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<int>("StackObjectsCount");
_choppaContainer.ItemOwner.MainStorage[0].Add(actualItem, item.location, false);
}
}
private Location GetHeliCrashLocation()
{
switch (_playerLocation)
{
case "bigmap":
{
return PickRandom(Plugin.HeliCrashLocations.Customs);
}
case "Interchange":
{
return PickRandom(Plugin.HeliCrashLocations.Interchange);
}
case "RezervBase":
{
return PickRandom(Plugin.HeliCrashLocations.Rezerv);
}
case "Shoreline":
{
return PickRandom(Plugin.HeliCrashLocations.Shoreline);
}
case "Woods":
{
return PickRandom(Plugin.HeliCrashLocations.Woods);
}
case "Lighthouse":
{
return PickRandom(Plugin.HeliCrashLocations.Lighthouse);
}
case "develop":
{
return PickRandom(Plugin.HeliCrashLocations.Develop);
}
default: return new Location();
}
}
private async Task<GameObject> LoadChoppaAsync()
{
string path = Plugin.Directory + "Assets/Content/Vehicles/sikorsky_uh60_blackhawk.bundle";
var bundleLoadRequest = AssetBundle.LoadFromFileAsync(path);
while (!bundleLoadRequest.isDone)
await Task.Yield();
_uh60Bundle = bundleLoadRequest.assetBundle;
if (_uh60Bundle == null)
{
Debug.LogError("[SamSWAT.HeliCrash]: Can't load UH-60 Blackhawk bundle");
return null;
}
var assetLoadRequest = _uh60Bundle.LoadAllAssetsAsync<GameObject>();
while (!assetLoadRequest.isDone)
await Task.Yield();
var requestedGO = assetLoadRequest.allAssets[0] as GameObject;
if (requestedGO == null)
{
Debug.LogError("[SamSWAT.HeliCrash]: failed to load asset");
return null;
}
return requestedGO;
}
private T PickRandom<T>(List<T> list)
{
return list[UnityEngine.Random.Range(0, list.Count)];
}
}
}