Skip to content

Commit

Permalink
Merge pull request #54 from CyberAgentGameEntertainment/feature/issue_53
Browse files Browse the repository at this point in the history
Fixed a bug where entries in groups not under control get deleted.
  • Loading branch information
Haruma-K authored Jan 25, 2024
2 parents ddc7d71 + 7980074 commit e6a5c6c
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ public sealed class ApplyLayoutRuleService
private readonly LayoutRule _layoutRule;
private readonly IVersionExpressionParser _versionExpressionParser;

public ApplyLayoutRuleService(LayoutRule layoutRule,
public ApplyLayoutRuleService(
LayoutRule layoutRule,
IVersionExpressionParser versionExpressionParser,
IAddressableAssetSettingsAdapter addressableSettingsAdapter,
IAssetDatabaseAdapter assetDatabaseAdapter)
IAssetDatabaseAdapter assetDatabaseAdapter
)
{
_layoutRule = layoutRule;
_addressableSettingsAdapter = addressableSettingsAdapter;
Expand All @@ -42,7 +44,7 @@ public void Setup()
/// <summary>
/// Apply the layout rule to the addressable settings for all assets.
/// </summary>
public void UpdateAllEntries()
public void ApplyAll()
{
Setup();

Expand All @@ -56,14 +58,43 @@ public void UpdateAllEntries()
if (!result)
removeTargetAssetGuids.Add(guid);
}

// Remove all entries that are not under control of this layout rule (if the entry is exists).

// If the address is not assigned by the LayoutRule and the entry belongs to the AddressableGroup under Control, remove the entry.
var controlGroupNames = _layoutRule
.AddressRules
.Where(x => x.Control.Value)
.Select(x => x.AddressableGroup.Name)
.ToArray();
foreach (var guid in removeTargetAssetGuids)
_addressableSettingsAdapter.RemoveEntry(guid, false);

{
var entryAdapter = _addressableSettingsAdapter.FindAssetEntry(guid);
if (entryAdapter == null)
continue;

if (controlGroupNames.Contains(entryAdapter.GroupName))
_addressableSettingsAdapter.RemoveEntry(guid, false);
}

_addressableSettingsAdapter.InvokeBatchModificationEvent();
}

public void Apply(string assetGuid, bool doSetup, bool invokeModificationEvent, string versionExpression = null)
{
var result = TryAddEntry(assetGuid, doSetup, invokeModificationEvent, versionExpression);

// If the address is not assigned by the LayoutRule and the entry belongs to the AddressableGroup under Control, remove the entry.
var controlGroupNames = _layoutRule
.AddressRules
.Where(x => x.Control.Value)
.Select(x => x.AddressableGroup.Name);
if (!result)
{
var entryAdapter = _addressableSettingsAdapter.FindAssetEntry(assetGuid);
if (entryAdapter != null && controlGroupNames.Contains(entryAdapter.GroupName))
_addressableSettingsAdapter.RemoveEntry(assetGuid, invokeModificationEvent);
}
}

/// <summary>
/// Apply the layout rule to the addressable settings.
/// </summary>
Expand All @@ -82,16 +113,24 @@ public void UpdateAllEntries()
/// If the layout rule was applied to the addressable asset system, return true.
/// Returns false if no suitable layout rule was found.
/// </returns>
public bool TryAddEntry(string assetGuid, bool doSetup, bool invokeModificationEvent, string versionExpression = null)
private bool TryAddEntry(
string assetGuid,
bool doSetup,
bool invokeModificationEvent,
string versionExpression = null
)
{
var assetPath = _assetDatabaseAdapter.GUIDToAssetPath(assetGuid);
var assetType = _assetDatabaseAdapter.GetMainAssetTypeAtPath(assetPath);
var isFolder = _assetDatabaseAdapter.IsValidFolder(assetPath);

// If the layout rule was not found, return false.
if (!_layoutRule.TryProvideAddressAndAddressableGroup(assetPath, assetType, isFolder, doSetup,
out var address,
out var addressableGroup))
if (!_layoutRule.TryProvideAddressAndAddressableGroup(assetPath,
assetType,
isFolder,
doSetup,
out var address,
out var addressableGroup))
return false;

// If the layout rule is found but the addressable asset group has already been destroyed, return false.
Expand All @@ -117,7 +156,8 @@ public bool TryAddEntry(string assetGuid, bool doSetup, bool invokeModificationE
}

// Set group and address.
var entryAdapter = _addressableSettingsAdapter.CreateOrMoveEntry(addressableGroupName, assetGuid, invokeModificationEvent);
var entryAdapter =
_addressableSettingsAdapter.CreateOrMoveEntry(addressableGroupName, assetGuid, invokeModificationEvent);
entryAdapter.SetAddress(address);

// Add labels to addressable settings if not exists.
Expand All @@ -138,7 +178,7 @@ public bool TryAddEntry(string assetGuid, bool doSetup, bool invokeModificationE

return true;
}

public void InvokeBatchModificationEvent()
{
_addressableSettingsAdapter.InvokeBatchModificationEvent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ void Apply()
var addressableSettingsAdapter = new AddressableAssetSettingsAdapter(addressableSettings);
var applyService = new ApplyLayoutRuleService(layoutRule, versionExpressionParser,
addressableSettingsAdapter, assetDatabaseAdapter);
applyService.UpdateAllEntries();
applyService.ApplyAll();
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private void OnLostFocus()
var addressableSettingsAdapter = new AddressableAssetSettingsAdapter(addressableSettings);
var applyService = new ApplyLayoutRuleService(layoutRule, versionExpressionParser,
addressableSettingsAdapter, assetDatabaseAdapter);
applyService.UpdateAllEntries();
applyService.ApplyAll();
}

_hasAnyDataChanged = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static void ApplyRules()
// Apply the layout rules to the addressable asset system.
var applyService = new ApplyLayoutRuleService(layoutRule, versionExpressionParser,
addressableSettingsAdapter, assetDatabaseAdapter);
applyService.UpdateAllEntries();
applyService.ApplyAll();

EditorApplication.Exit(ErrorLevelNone);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ private static void OnPostprocessAllAssets(string[] importedAssetPaths, string[]
foreach (var importedAssetPath in importedAssetPaths)
{
var guid = AssetDatabase.AssetPathToGUID(importedAssetPath);
applyService.TryAddEntry(guid, false, false, versionExpression);
applyService.Apply(guid, false, true, versionExpression);
}

foreach (var movedAssetPath in movedAssetPaths)
{
var guid = AssetDatabase.AssetPathToGUID(movedAssetPath);
applyService.TryAddEntry(guid, false, false, versionExpression);
applyService.Apply(guid, false, true, versionExpression);
}

applyService.InvokeBatchModificationEvent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override void OnGUI(string searchContext)
var addressableSettingsAdapter = new AddressableAssetSettingsAdapter(addressableSettings);
var applyService = new ApplyLayoutRuleService(layoutRule, versionExpressionParser,
addressableSettingsAdapter, assetDatabaseAdapter);
applyService.UpdateAllEntries();
applyService.ApplyAll();
}
else
{
Expand Down
Loading

0 comments on commit e6a5c6c

Please sign in to comment.