Skip to content

Commit

Permalink
複数のLayoutRuleをまとめてApplyできるようにする
Browse files Browse the repository at this point in the history
  • Loading branch information
hkmt-mmy committed Jan 28, 2025
1 parent 96394ae commit 506cd3f
Show file tree
Hide file tree
Showing 17 changed files with 267 additions and 142 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using UnityEngine;

namespace SmartAddresser.Editor.Core.Models.LayoutRules
{
public abstract class BaseLayoutRuleData : ScriptableObject
{
public abstract IEnumerable<LayoutRule> LayoutRules { get; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace SmartAddresser.Editor.Core.Models.LayoutRules
{
[CreateAssetMenu(fileName = "CompositeLayoutRuleData", menuName = "Smart Addresser/Composite Layout Rule Data")]
public sealed class CompositeLayoutRuleData : BaseLayoutRuleData
{
[SerializeField] private LayoutRuleData[] _layoutRules;

public override IEnumerable<LayoutRule> LayoutRules => _layoutRules.SelectMany(x => x.LayoutRules);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using SmartAddresser.Editor.Core.Tools.Shared;
using UnityEditor;
using UnityEngine;

namespace SmartAddresser.Editor.Core.Models.LayoutRules
{
[CustomEditor(typeof(CompositeLayoutRuleData))]
internal sealed class CompositeLayoutRuleDataEditor : UnityEditor.Editor
{
public override void OnInspectorGUI()
{
var data = (CompositeLayoutRuleData)target;

if (GUILayout.Button("Apply"))
MenuActions.ApplyAction(data);

GUI.enabled = false;
base.OnInspectorGUI();
GUI.enabled = true;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Collections.Generic;
using UnityEngine;

namespace SmartAddresser.Editor.Core.Models.LayoutRules
{
[CreateAssetMenu(fileName = "LayoutRuleData", menuName = "Smart Addresser/Layout Rule Data")]
public sealed class LayoutRuleData : ScriptableObject
public sealed class LayoutRuleData : BaseLayoutRuleData
{
[SerializeField] private LayoutRule _layoutRule = new LayoutRule();

Expand All @@ -12,5 +13,10 @@ public LayoutRule LayoutRule
get => _layoutRule;
set => _layoutRule = value;
}

public override IEnumerable<LayoutRule> LayoutRules
{
get { yield return _layoutRule; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,26 @@ public sealed class ApplyLayoutRuleService
{
private readonly IAddressableAssetSettingsAdapter _addressableSettingsAdapter;
private readonly IAssetDatabaseAdapter _assetDatabaseAdapter;
private readonly LayoutRule _layoutRule;
private readonly LayoutRule[] _layoutRules;
private readonly IVersionExpressionParser _versionExpressionParser;

public ApplyLayoutRuleService(
LayoutRule layoutRule,
IVersionExpressionParser versionExpressionParser,
IAddressableAssetSettingsAdapter addressableSettingsAdapter,
IAssetDatabaseAdapter assetDatabaseAdapter
) : this(new[] { layoutRule }, versionExpressionParser, addressableSettingsAdapter, assetDatabaseAdapter)
{
}

public ApplyLayoutRuleService(
IEnumerable<LayoutRule> layoutRules,
IVersionExpressionParser versionExpressionParser,
IAddressableAssetSettingsAdapter addressableSettingsAdapter,
IAssetDatabaseAdapter assetDatabaseAdapter
)
{
_layoutRule = layoutRule;
_layoutRules = layoutRules.ToArray();
_addressableSettingsAdapter = addressableSettingsAdapter;
_versionExpressionParser = versionExpressionParser;
_assetDatabaseAdapter = assetDatabaseAdapter;
Expand All @@ -34,54 +43,63 @@ IAssetDatabaseAdapter assetDatabaseAdapter
/// </summary>
public void ApplyAll(bool doSetup)
{
if (doSetup)
_layoutRule.Setup();

// Add all entries to the addressable asset system.
var removeTargetAssetGuids = new List<string>();
var versionExpression = _layoutRule.Settings.VersionExpression;
foreach (var assetPath in _assetDatabaseAdapter.GetAllAssetPaths())
foreach (var layoutRule in _layoutRules)
{
var guid = _assetDatabaseAdapter.AssetPathToGUID(assetPath);
var result = TryAddEntry(guid, false, false, versionExpression.Value);
if (!result)
removeTargetAssetGuids.Add(guid);
}

// 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)
{
var entryAdapter = _addressableSettingsAdapter.FindAssetEntry(guid);
if (entryAdapter == null)
continue;

if (controlGroupNames.Contains(entryAdapter.GroupName))
_addressableSettingsAdapter.RemoveEntry(guid, false);
if (doSetup)
layoutRule.Setup();

// Add all entries to the addressable asset system.
var removeTargetAssetGuids = new List<string>();
var versionExpression = layoutRule.Settings.VersionExpression;
foreach (var assetPath in _assetDatabaseAdapter.GetAllAssetPaths())
{
var guid = _assetDatabaseAdapter.AssetPathToGUID(assetPath);
var result = TryAddEntry(layoutRule, guid, false, false, versionExpression.Value);
if (!result)
removeTargetAssetGuids.Add(guid);
}

// 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)
{
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)
foreach (var layoutRule in _layoutRules)
{
var entryAdapter = _addressableSettingsAdapter.FindAssetEntry(assetGuid);
if (entryAdapter != null && controlGroupNames.Contains(entryAdapter.GroupName))
_addressableSettingsAdapter.RemoveEntry(assetGuid, invokeModificationEvent);
var result = TryAddEntry(layoutRule, assetGuid, doSetup, false, 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, false);
}
}

if (invokeModificationEvent)
_addressableSettingsAdapter.InvokeBatchModificationEvent();
}

/// <summary>
Expand All @@ -103,6 +121,7 @@ public void Apply(string assetGuid, bool doSetup, bool invokeModificationEvent,
/// Returns false if no suitable layout rule was found.
/// </returns>
private bool TryAddEntry(
LayoutRule layoutRule,
string assetGuid,
bool doSetup,
bool invokeModificationEvent,
Expand All @@ -114,12 +133,12 @@ private bool TryAddEntry(
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 @@ -132,9 +151,9 @@ private bool TryAddEntry(
if (!string.IsNullOrEmpty(versionExpression))
{
var comparator = _versionExpressionParser.CreateComparator(versionExpression);
var versionText = _layoutRule.ProvideVersion(assetPath, assetType, isFolder, doSetup);
var versionText = layoutRule.ProvideVersion(assetPath, assetType, isFolder, doSetup);

if (string.IsNullOrEmpty(versionText) && _layoutRule.Settings.ExcludeUnversioned.Value)
if (string.IsNullOrEmpty(versionText) && layoutRule.Settings.ExcludeUnversioned.Value)
return false;

// If the version is not satisfied, return false.
Expand All @@ -150,7 +169,7 @@ private bool TryAddEntry(
entryAdapter.SetAddress(address, invokeModificationEvent);

// Add labels to addressable settings if not exists.
var labels = _layoutRule.ProvideLabels(assetPath, assetType, isFolder, doSetup);
var labels = layoutRule.ProvideLabels(assetPath, assetType, isFolder, doSetup);
var addressableLabels = _addressableSettingsAdapter.GetLabels();
foreach (var label in labels)
if (!addressableLabels.Contains(label))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,56 +249,7 @@ void OnMenuButtonClicked()

menu.AddItem(new GUIContent("Apply to Addressables"),
false,
() =>
{
var projectSettings = SmartAddresserProjectSettings.instance;
var primaryData = projectSettings.PrimaryData;

if (primaryData == null)
{
Apply();
return;
}

if (primaryData == _editingData.Value)
{
Apply();
return;
}

// If the primary data is not the same as the editing data, ask the user to confirm.
// If the user confirms, remove the primary data and apply the editing data.
var dialogMessage =
$"The {nameof(projectSettings.PrimaryData)} of the Project Settings is not the same as the data you are applying. Do you want to remove the {nameof(projectSettings.PrimaryData)} from Project Settings and apply the editing data?";
if (EditorUtility.DisplayDialog("Confirm", dialogMessage, "Remove & Apply", "Cancel"))
{
projectSettings.PrimaryData = null;
Apply();
}

void Apply()
{
var layoutRule = _editingData.Value.LayoutRule;
layoutRule.Setup();

// Validate the layout rule.
var validateService = new ValidateAndExportLayoutRuleService(layoutRule);
var ruleErrorHandleType = projectSettings.LayoutRuleErrorSettings.HandleType;
validateService.Execute(false, ruleErrorHandleType, out _);

// Apply the layout rules to the addressable asset system.
var versionExpressionParser = new VersionExpressionParserRepository().Load();
var assetDatabaseAdapter = new AssetDatabaseAdapter();
var addressableSettings = AddressableAssetSettingsDefaultObject.Settings;
var addressableSettingsAdapter = new AddressableAssetSettingsAdapter(addressableSettings);
var applyService = new ApplyLayoutRuleService(layoutRule,
versionExpressionParser,
addressableSettingsAdapter,
assetDatabaseAdapter);

applyService.ApplyAll(false);
}
});
() => MenuActions.ApplyAction(_editingData.Value));

menu.AddItem(new GUIContent("Open Layout Viewer"), false, LayoutViewerWindow.Open);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using SmartAddresser.Editor.Core.Models.Services;
using SmartAddresser.Editor.Core.Tools.Addresser.LayoutRuleEditor.AddressRuleEditor;
using SmartAddresser.Editor.Core.Tools.Addresser.LayoutRuleEditor.LabelRuleEditor;
Expand Down Expand Up @@ -74,20 +75,19 @@ private void OnLostFocus()
var primaryData = projectSettings.PrimaryData;
if (primaryData != null)
{
var layoutRule = primaryData.LayoutRule;
layoutRule.Setup();
var layoutRules = primaryData.LayoutRules.ToArray();

// Validate the layout rule.
var validateService = new ValidateAndExportLayoutRuleService(layoutRule);
var validateService = new ValidateAndExportLayoutRuleService(layoutRules);
var ruleErrorHandleType = projectSettings.LayoutRuleErrorSettings.HandleType;
validateService.Execute(false, ruleErrorHandleType, out _);
validateService.Execute(true, ruleErrorHandleType, out _);

// Apply the layout rule to the addressable asset system.
var versionExpressionParser = new VersionExpressionParserRepository().Load();
var assetDatabaseAdapter = new AssetDatabaseAdapter();
var addressableSettings = AddressableAssetSettingsDefaultObject.Settings;
var addressableSettingsAdapter = new AddressableAssetSettingsAdapter(addressableSettings);
var applyService = new ApplyLayoutRuleService(layoutRule,
var applyService = new ApplyLayoutRuleService(layoutRules,
versionExpressionParser,
addressableSettingsAdapter,
assetDatabaseAdapter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace SmartAddresser.Editor.Core.Tools.Addresser.Shared
{
public sealed class LayoutRuleDataRepository : ILayoutRuleDataRepository
{
public LayoutRuleData PrimaryData => SmartAddresserProjectSettings.instance.PrimaryData;
public BaseLayoutRuleData PrimaryData => SmartAddresserProjectSettings.instance.PrimaryData;

public IReadOnlyObservableProperty<LayoutRuleData> EditingData =>
SmartAddresserPreferences.instance.EditingData;
Expand Down
Loading

0 comments on commit 506cd3f

Please sign in to comment.