internal override AsyncOperationHandle <IList <IResourceLocation> > LoadTableLocationsAsyncInternal(string tableName, LocaleIdentifier id, Type type)
        {
            if (LocalizationSettings.Instance.IsPlaying)
            {
                return(base.LoadTableLocationsAsyncInternal(tableName, id, type));
            }

            LocalizationTable table = null;
            var locations           = new List <IResourceLocation>();

            if (type == typeof(AssetTable))
            {
                table = LocalizationEditorSettings.GetAssetTableCollection(tableName)?.GetTable(id);
            }
            else if (type == typeof(StringTable))
            {
                table = LocalizationEditorSettings.GetStringTableCollection(tableName)?.GetTable(id);
            }
            else
            {
                Debug.LogError($"Unknown table type {type}");
            }

            if (table != null)
            {
                locations.Add(new ResourceLocationBase(tableName, AssetDatabase.GetAssetPath(table), nameof(EditorAddressablesInterface), type));
            }

            return(m_ResourceManager.CreateCompletedOperation <IList <IResourceLocation> >(locations, null));
        }
 static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions _)
 {
     if (assetPath.EndsWith("asset"))
     {
         var assetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath);
         if (typeof(LocalizationTableCollection).IsAssignableFrom(assetType))
         {
             var tableCollection = AssetDatabase.LoadAssetAtPath <LocalizationTableCollection>(assetPath);
             tableCollection.RemoveCollectionFromProject();
         }
         else if (typeof(SharedTableData).IsAssignableFrom(assetType))
         {
             var sharedData = AssetDatabase.LoadAssetAtPath <SharedTableData>(assetPath);
             var collection = LocalizationEditorSettings.GetCollectionForSharedTableData(sharedData);
             collection?.RemoveCollectionFromProject();
         }
         else if (typeof(LocalizationTable).IsAssignableFrom(assetType))
         {
             var table      = AssetDatabase.LoadAssetAtPath <LocalizationTable>(assetPath);
             var collection = LocalizationEditorSettings.GetCollectionFromTable(table);
             collection?.RemoveTable(table);
         }
         else if (typeof(Locale).IsAssignableFrom(assetType))
         {
             var locale = AssetDatabase.LoadAssetAtPath <Locale>(assetPath);
             LocalizationEditorSettings.RemoveLocale(locale, false);
         }
     }
     return(AssetDeleteResult.DidNotDelete);
 }
        /// <summary>
        /// Remove the asset mapping from the table entry and also cleans up the Addressables if necessary.
        /// </summary>
        /// <param name="table"></param>
        /// <param name="entryReference"></param>
        /// <param name="createUndo"></param>
        public void RemoveAssetFromTable(AssetTable table, TableEntryReference entryReference, bool createUndo = false)
        {
            using (new UndoScope("Remove asset from table", createUndo))
            {
                // Clear the asset but keep the key
                var tableEntry = table.GetEntryFromReference(entryReference);
                if (tableEntry == null)
                {
                    return;
                }

                var removedAssetGuid = tableEntry.Guid;
                tableEntry.Guid = string.Empty;

                var aaSettings = LocalizationEditorSettings.Instance.GetAddressableAssetSettings(false);
                if (aaSettings == null)
                {
                    return;
                }

                EditorUtility.SetDirty(table);
                EditorUtility.SetDirty(table.SharedData);

                RemoveEntryAssetType(tableEntry.KeyId, table.LocaleIdentifier.Code);

                // If the entry has metadata then we will leave an empty entry otherwise we just remove the whole thing.
                if (tableEntry.MetadataEntries.Count == 0)
                {
                    table.RemoveEntry(tableEntry.KeyId);
                }

                // Determine if the asset is being referenced by any entries or tables with the same locale, if not then we can
                // remove the locale label and if no other labels exist also remove the asset from the Addressables system.
                var assetTableCollections = LocalizationEditorSettings.GetAssetTableCollections();
                foreach (var collection in assetTableCollections)
                {
                    if (collection.GetTable(table.LocaleIdentifier) is AssetTable tableWithMatchingLocaleId && tableWithMatchingLocaleId.ContainsValue(removedAssetGuid))
                    {
                        // The asset is referenced elsewhere by a table with the same Locale so we can not remove the locale label or asset.
                        return;
                    }
                }

                // Remove the locale label for this asset
                var assetEntry = aaSettings.FindAssetEntry(removedAssetGuid);
                if (assetEntry != null)
                {
                    if (createUndo)
                    {
                        Undo.RecordObject(assetEntry.parentGroup, "Remove asset from table");
                    }

                    var assetLabel = AddressHelper.FormatAssetLabel(table.LocaleIdentifier);
                    assetEntry.SetLabel(assetLabel, false);
                    UpdateAssetGroup(aaSettings, assetEntry, createUndo);
                }

                LocalizationEditorSettings.EditorEvents.RaiseAssetTableEntryRemoved(this, table, tableEntry, removedAssetGuid);
            }
        }
Example #4
0
        #pragma warning disable CA1801 // CA1801 Review unused parameters
        static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            // TODO: Handle deleted assets. We should use the asset guid to determine the asset type and what we need to do if anything.
            foreach (var assetPath in deletedAssets)
            {
                if (assetPath.EndsWith("asset"))
                {
                    LocalizationEditorSettings.Instance.TableCollectionCache.Clear();
                    break;
                }
            }

            foreach (var assetPath in importedAssets)
            {
                if (assetPath.EndsWith("asset"))
                {
                    var assetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath);
                    if (typeof(LocalizationTableCollection).IsAssignableFrom(assetType))
                    {
                        var collection = AssetDatabase.LoadAssetAtPath <LocalizationTableCollection>(assetPath);
                        collection.ImportCollectionIntoProject();
                    }
                    else if (typeof(SharedTableData).IsAssignableFrom(assetType))
                    {
                        var sharedTableData = AssetDatabase.LoadAssetAtPath <SharedTableData>(assetPath);
                        if (sharedTableData != null)
                        {
                            Debug.Assert(AssetDatabase.TryGetGUIDAndLocalFileIdentifier(sharedTableData, out string sharedTableDataGuid, out long _), "Failed to extract SharedTableData Guid", sharedTableData);
                            var guid = Guid.Parse(sharedTableDataGuid);

                            if (sharedTableData.TableCollectionNameGuid != Guid.Empty)
                            {
                                Debug.Assert(sharedTableData.TableCollectionNameGuid == guid, "SharedTableData Name Guid does not match the assets Guid. This may cause issues matching the correct TableCollectionName.", sharedTableData);
                            }
                            else
                            {
                                sharedTableData.TableCollectionNameGuid = guid;
                            }
                        }
                    }
                    else if (typeof(Locale).IsAssignableFrom(assetType))
                    {
                        var locale = AssetDatabase.LoadAssetAtPath <Locale>(assetPath);
                        Debug.Assert(locale != null, "Failed to load Locale asset.");
                        LocalizationEditorSettings.AddLocale(locale, false);
                    }
                }
            }
        }
Example #5
0
        /// <summary>
        /// Remove the asset mapping from the table entry and also cleans up the Addressables if necessary.
        /// </summary>
        /// <param name="table"></param>
        /// <param name="entryReference"></param>
        /// <param name="createUndo"></param>
        public void RemoveAssetFromTable(AssetTable table, TableEntryReference entryReference, bool createUndo = false)
        {
            var undoGroup = Undo.GetCurrentGroup();

            if (createUndo)
            {
                Undo.RecordObject(table, "Remove asset from table");            // We modify the table entry.
                Undo.RecordObject(table.SharedData, "Remove asset from table"); // We modify the shared table metadata.
            }
            //else // Asset changes are not being saved correctly at the moment when using Undo. (LOC-82)
            {
                EditorUtility.SetDirty(table);
                EditorUtility.SetDirty(table.SharedData);
            }

            // Clear the asset but keep the key
            var tableEntry = table.GetEntryFromReference(entryReference);

            if (tableEntry == null)
            {
                return;
            }

            var removedAssetGuid = tableEntry.Guid;

            tableEntry.Guid = string.Empty;

            var aaSettings = LocalizationEditorSettings.Instance.GetAddressableAssetSettings(false);

            if (aaSettings == null)
            {
                return;
            }

            // Update type metadata
            // We cant use a foreach here as we are sometimes inside of a loop and exceptions will be thrown (Collection was modified).
            for (int i = 0; i < table.SharedData.Metadata.MetadataEntries.Count; ++i)
            {
                var md = table.SharedData.Metadata.MetadataEntries[i];
                if (md is AssetTypeMetadata at)
                {
                    if (at.Contains(tableEntry.KeyId))
                    {
                        tableEntry.RemoveSharedMetadata(at);
                    }
                }
            }

            // If the entry has metadata then we will leave an empty entry otherwise we just remove the whole thing.
            if (tableEntry.MetadataEntries.Count == 0)
            {
                table.RemoveEntry(tableEntry.KeyId);
            }

            // Determine if the asset is being referenced by any entries or tables with the same locale, if not then we can
            // remove the locale label and if no other labels exist also remove the asset from the Addressables system.
            var assetTableCollections = LocalizationEditorSettings.GetAssetTableCollections();

            foreach (var collection in assetTableCollections)
            {
                var tableWithMatchingLocaleId = collection.GetTable(table.LocaleIdentifier) as AssetTable;
                if (tableWithMatchingLocaleId == null)
                {
                    continue;
                }

                if (tableWithMatchingLocaleId.ContainsValue(removedAssetGuid))
                {
                    // The asset is referenced elsewhere so we can not remove the label or asset.
                    return;
                }
            }

            // Remove the locale label for this asset
            var assetEntry = aaSettings.FindAssetEntry(removedAssetGuid);

            if (assetEntry != null)
            {
                if (createUndo)
                {
                    Undo.RecordObject(assetEntry.parentGroup, "Remove asset from table");
                }

                var assetLabel = AddressHelper.FormatAssetLabel(table.LocaleIdentifier);
                assetEntry.SetLabel(assetLabel, false);
                UpdateAssetGroup(aaSettings, assetEntry, createUndo);
            }

            if (createUndo)
            {
                Undo.CollapseUndoOperations(undoGroup);
            }

            LocalizationEditorSettings.EditorEvents.RaiseAssetTableEntryRemoved(this, table, tableEntry, removedAssetGuid);
        }
        #pragma warning disable CA1801 // CA1801 Review unused parameters
        static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            // TODO: Handle deleted assets. We should use the asset guid to determine the asset type and what we need to do if anything.
            foreach (var assetPath in deletedAssets)
            {
                if (assetPath.EndsWith("asset"))
                {
                    LocalizationEditorSettings.Instance.TableCollectionCache.Clear();
                    break;
                }
            }

            foreach (var assetPath in importedAssets)
            {
                if (assetPath.EndsWith("asset"))
                {
                    var assetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath);
                    if (typeof(LocalizationTableCollection).IsAssignableFrom(assetType))
                    {
                        var collection = AssetDatabase.LoadAssetAtPath <LocalizationTableCollection>(assetPath);
                        collection.ImportCollectionIntoProject();
                    }
                    else if (typeof(SharedTableData).IsAssignableFrom(assetType))
                    {
                        var sharedTableData = AssetDatabase.LoadAssetAtPath <SharedTableData>(assetPath);
                        if (sharedTableData != null)
                        {
                            Debug.Assert(AssetDatabase.TryGetGUIDAndLocalFileIdentifier(sharedTableData, out string sharedTableDataGuid, out long _), "Failed to extract SharedTableData Guid", sharedTableData);
                            var guid = Guid.Parse(sharedTableDataGuid);

                            if (sharedTableData.TableCollectionNameGuid != Guid.Empty)
                            {
                                Debug.Assert(sharedTableData.TableCollectionNameGuid == guid, "SharedTableData Name Guid does not match the assets Guid. This may cause issues matching the correct TableCollectionName.", sharedTableData);
                            }
                            else
                            {
                                sharedTableData.TableCollectionNameGuid = guid;
                            }

                            // If the collection asset was deleted and then restored we may have a collection using an invalid handle (LOC-182)
                            var collection = LocalizationEditorSettings.GetCollectionForSharedTableData(sharedTableData);
                            if (collection != null)
                            {
                                bool modified = false;
                                if (collection.SharedData == null)
                                {
                                    modified = true;
                                    collection.SharedData = sharedTableData;
                                }

                                foreach (var tableReference in collection.Tables)
                                {
                                    if (tableReference.asset == null && tableReference.asset.SharedData != null)
                                    {
                                        continue;
                                    }

                                    modified = true;
                                    tableReference.asset.SharedData = sharedTableData;
                                }

                                if (modified)
                                {
                                    LocalizationEditorSettings.EditorEvents.RaiseCollectionModified(null, collection);
                                }
                            }
                        }
                    }
                    else if (typeof(Locale).IsAssignableFrom(assetType))
                    {
                        var locale = AssetDatabase.LoadAssetAtPath <Locale>(assetPath);
                        Debug.Assert(locale != null, "Failed to load Locale asset.");
                        LocalizationEditorSettings.AddLocale(locale, false);
                    }
                }
            }
        }
Example #7
0
        #pragma warning disable CA1801 // CA1801 Review unused parameters
        static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            // We need to disable Addresables settings creation here.
            // When doing a full project import, there is no guarantee of OnPostprocessAllAssets order
            // and the Addressables may be called after Localization which would cause assets to be
            // re-added to groups that become invalid and a general corruption of Addressables data.
            LocalizationEditorSettings.EnableAddressablesCreation = false;

            try
            {
                // TODO: Handle deleted assets. We should use the asset guid to determine the asset type and what we need to do if anything.
                foreach (var assetPath in deletedAssets)
                {
                    if (assetPath.EndsWith("asset"))
                    {
                        LocalizationEditorSettings.Instance.TableCollectionCache.Clear();
                        break;
                    }
                }

                foreach (var assetPath in importedAssets)
                {
                    if (assetPath.EndsWith("asset"))
                    {
                        var assetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath);
                        if (typeof(LocalizedTableCollection).IsAssignableFrom(assetType))
                        {
                            var collection = AssetDatabase.LoadAssetAtPath <LocalizedTableCollection>(assetPath);
                            collection.ImportCollectionIntoProject();
                        }
                        else if (typeof(SharedTableData).IsAssignableFrom(assetType))
                        {
                            var sharedTableData = AssetDatabase.LoadAssetAtPath <SharedTableData>(assetPath);
                            if (sharedTableData != null)
                            {
                                Debug.Assert(AssetDatabase.TryGetGUIDAndLocalFileIdentifier(sharedTableData, out string sharedTableDataGuid, out long _), "Failed to extract SharedTableData Guid", sharedTableData);
                                var guid = Guid.Parse(sharedTableDataGuid);

                                if (sharedTableData.TableCollectionNameGuid != Guid.Empty)
                                {
                                    Debug.Assert(sharedTableData.TableCollectionNameGuid == guid, "SharedTableData Name Guid does not match the assets Guid. This may cause issues matching the correct TableCollectionName.", sharedTableData);
                                }
                                else
                                {
                                    sharedTableData.TableCollectionNameGuid = guid;
                                }
                            }
                        }
                        else if (typeof(Locale).IsAssignableFrom(assetType))
                        {
                            var locale = AssetDatabase.LoadAssetAtPath <Locale>(assetPath);
                            Debug.Assert(locale != null, "Failed to load Locale asset.");
                            LocalizationEditorSettings.AddLocale(locale, false);
                        }
                    }
                }
            }
            finally
            {
                LocalizationEditorSettings.EnableAddressablesCreation = true;
            }
        }