Skip to content

Commit

Permalink
Fix new redirect stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
negrifelipe committed Jul 20, 2024
1 parent b3470bf commit eeab741
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 33 deletions.
49 changes: 37 additions & 12 deletions UnturnedImages/Items/ItemImageDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,24 @@ public UniTask HandleEventAsync(object? sender, PluginConfigurationChangedEvent
/// <inheritdoc />
public string? GetItemImageUrlSync(ushort id, bool includeWorkshop)
{
var asset = Assets.find(EAssetType.ITEM, id);
var asset = GetItemAsset(id);

if (asset == null)
return null;

return GetItemImageUrlSync(asset, includeWorkshop);
}

/// <inheritdoc />
public string? GetItemImageUrlSync(Guid guid, bool includeWorkshop = true)
{
var asset = GetItemAsset(guid);

if (asset == null) return null;

return GetItemImageUrlSync(asset, includeWorkshop);
}


/// <inheritdoc />
public Task<string?> GetItemImageUrlAsync(ushort id, bool includeWorkshop)
Expand All @@ -132,17 +142,6 @@ public UniTask HandleEventAsync(object? sender, PluginConfigurationChangedEvent
return Task.FromResult(GetItemImageUrlSync(guid, includeWorkshop));
}

/// <inheritdoc />
public string? GetItemImageUrlSync(Guid guid, bool includeWorkshop = true)
{
var asset = Assets.find<ItemAsset>(guid);

if (asset == null)
return null;

return GetItemImageUrlSync(asset, includeWorkshop);
}

private static readonly FieldInfo AssetOrigin = typeof(Asset).GetField("origin", BindingFlags.Instance | BindingFlags.NonPublic);

private string? GetItemImageUrlSync(Asset asset, bool includeWorkshop = true)
Expand All @@ -161,5 +160,31 @@ public UniTask HandleEventAsync(object? sender, PluginConfigurationChangedEvent

return repository == null ? null : Smart.Format(repository, new { ItemId = asset.GUID });
}

private ItemAsset? GetItemAsset(ushort id)
{
var asset = Assets.find(EAssetType.ITEM, id);

if(asset == null) return null;

return GetItemAsset(asset.GUID);
}

private ItemAsset? GetItemAsset(Guid guid)
{
var asset = Assets.find(guid);

if(asset is ItemAsset itemAsset)
{
return itemAsset;
}

if(asset is RedirectorAsset redirectorAsset)
{
return Assets.find<ItemAsset>(redirectorAsset.TargetGuid);
}

return null;
}
}
}
77 changes: 57 additions & 20 deletions UnturnedImages/Vehicles/VehicleImageDirectory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
extern alias JetBrainsAnnotations;
using Cysharp.Threading.Tasks;
using Cysharp.Threading.Tasks.Triggers;
using JetBrainsAnnotations::JetBrains.Annotations;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -16,7 +15,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnturnedImages.API.Vehicles;
Expand Down Expand Up @@ -114,23 +112,12 @@ public UniTask HandleEventAsync(object? sender, PluginConfigurationChangedEvent
/// <inheritdoc />
public string? GetVehicleImageUrlSync(ushort id, bool includeWorkshop)
{
var asset = Assets.find(EAssetType.VEHICLE, id);
var asset = GetVehicleAsset(id, out var color);

if (asset == null)
return null;

Color32 paintColor = new Color(0,0,0,0);

if(asset is VehicleRedirectorAsset redirectorAsset)
{
paintColor = redirectorAsset.SpawnPaintColor ?? redirectorAsset.LoadPaintColor ?? new Color32(0,0,0,0);
}
else if(asset is VehicleAsset vehicleAsset && vehicleAsset.SupportsPaintColor)
{
paintColor = vehicleAsset.DefaultPaintColors[0];
}

return GetVehicleImageUrlSync(asset.GUID, paintColor, includeWorkshop);
return GetVehicleImageUrlSync(asset, color, includeWorkshop);
}


Expand All @@ -155,13 +142,18 @@ public UniTask HandleEventAsync(object? sender, PluginConfigurationChangedEvent
/// <inheritdoc />
public string? GetVehicleImageUrlSync(Guid guid, bool includeWorkshop = true)
{
return GetVehicleImageUrlSync(guid, new Color32(0, 0, 0, 0), includeWorkshop);
var asset = GetVehicleAsset(guid, out var color);

if (asset == null)
return null;

return GetVehicleImageUrlSync(asset, color, includeWorkshop);
}

/// <inheritdoc />
public string? GetVehicleImageUrlSync(Guid guid, Color32 paintColor,bool includeWorkshop = true)
{
var asset = Assets.find<VehicleAsset>(guid);
var asset = GetVehicleAsset(guid, out _);

if (asset == null)
return null;
Expand All @@ -171,7 +163,7 @@ public UniTask HandleEventAsync(object? sender, PluginConfigurationChangedEvent

private static readonly FieldInfo AssetOrigin = typeof(Asset).GetField("origin", BindingFlags.Instance | BindingFlags.NonPublic);

private string? GetVehicleImageUrlSync(VehicleAsset asset, Color32 paintColor, bool includeWorkshop = true)
private string? GetVehicleImageUrlSync(VehicleAsset asset, Color32? paintColor, bool includeWorkshop = true)
{
if (AssetOrigin.GetValue(asset) is not AssetOrigin origin)
return null;
Expand All @@ -187,12 +179,57 @@ public UniTask HandleEventAsync(object? sender, PluginConfigurationChangedEvent

string id = asset.GUID.ToString();

if(paintColor.r != 0 || paintColor.g != 0 || paintColor.b != 0)
// if paint color is null use 0 default paint color
// if paint color is not null but not valid use 0 paint color
if((paintColor == null && asset.SupportsPaintColor) ||
(asset.SupportsPaintColor && paintColor != null && !asset.DefaultPaintColors.Any(x => x.r == paintColor.Value.r && x.g == paintColor.Value.g && x.b == paintColor.Value.b)))
{
id += $"-{paintColor.r}-{paintColor.g}-{paintColor.b}";
paintColor = asset.DefaultPaintColors[0];
}

if(paintColor != null)
{
id += $"-{paintColor.Value.r}-{paintColor.Value.g}-{paintColor.Value.b}";
}

return repository == null ? null : Smart.Format(repository, new { VehicleId = id });
}

private VehicleAsset? GetVehicleAsset(ushort id, out Color32? color)
{
var asset = Assets.find(EAssetType.VEHICLE, id);

if (asset == null)
{
color = null;
return null;
}

return GetVehicleAsset(asset.GUID, out color);
}

private VehicleAsset? GetVehicleAsset(Guid guid, out Color32? color)
{
var asset = Assets.find(guid);
color = null;

if (asset is VehicleAsset vehicleAsset)
{
return vehicleAsset;
}

if (asset is RedirectorAsset redirectorAsset)
{
return Assets.find<VehicleAsset>(redirectorAsset.TargetGuid);
}

if(asset is VehicleRedirectorAsset vehicleRedirectorAsset)
{
color = vehicleRedirectorAsset.LoadPaintColor ?? vehicleRedirectorAsset.SpawnPaintColor;
return vehicleRedirectorAsset.TargetVehicle.Find();
}

return null;
}
}
}
2 changes: 1 addition & 1 deletion tools/UnturnedImages.Module/Images/CustomVehicleTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private void Update()

_camera.position = cameraPosition;

if(!vehicleAsset.IsPaintable)
if(!vehicleAsset.SupportsPaintColor)
{
Texture2D texture = CustomImageTool.CaptureIcon(vehicleAsset.GUID, 0, vehicle, _camera,
vehicleIconInfo.Width, vehicleIconInfo.Height, orthographicSize, true);
Expand Down

0 comments on commit eeab741

Please sign in to comment.