Beispiel #1
0
            /// <summary>
            /// Adds a new mapping for the given relative content path.
            /// </summary>
            /// <param name="path">The relative asset path.</param>
            /// <param name="metadata">The matching mod asset meta object.</param>
            /// <returns>The passed mod asset meta object.</returns>
            public static ModAsset Add(string path, ModAsset metadata)
            {
                path = path.Replace('\\', '/');

                if (metadata.Type == null)
                {
                    path = GuessType(path, out metadata.Type, out metadata.Format);
                }

                metadata.PathVirtual = path;

                // We want our new mapping to replace the previous one, but need to replace the previous one in the shadow structure.
                ModAsset metadataPrev;

                if (!Map.TryGetValue(path, out metadataPrev))
                {
                    metadataPrev = null;
                }

                // Hardcoded case: Handle directories separately.
                if (metadata.Type == typeof(AssetTypeDirectory))
                {
                    MapDirs[path] = metadata;
                }
                else
                {
                    Map[path] = metadata;
                }

                // If we're not already the highest level shadow "node"...
                if (path != "")
                {
                    // Add directories automatically.
                    string   pathDir = Path.GetDirectoryName(path).Replace('\\', '/');
                    ModAsset metadataDir;
                    if (!MapDirs.TryGetValue(pathDir, out metadataDir))
                    {
                        metadataDir = new ModAssetBranch {
                            PathVirtual = pathDir,
                            Type        = typeof(AssetTypeDirectory)
                        };
                        Add(pathDir, metadataDir);
                    }
                    // If a previous mapping exists, replace it in the shadow structure.
                    int metadataPrevIndex = metadataDir.Children.IndexOf(metadataPrev);
                    if (metadataPrevIndex != -1)
                    {
                        metadataDir.Children[metadataPrevIndex] = metadata;
                    }
                    else
                    {
                        metadataDir.Children.Add(metadata);
                    }
                }

                return(metadata);
            }
Beispiel #2
0
 private static string ReadText(ModAsset asset)
 {
     if (asset == null)
     {
         return(null);
     }
     using (StreamReader reader = new StreamReader(asset.Stream))
         return(reader.ReadToEnd());
 }
            /// <summary>
            /// Gets the ModAsset mapped to the given relative path.
            /// </summary>
            /// <param name="path">The relative asset path.</param>
            /// <param name="metadata">The resulting mod asset meta object.</param>
            /// <param name="includeDirs">Whether to include directories or not.</param>
            /// <returns>True if a mapping for the given path is present, false otherwise.</returns>
            public static bool TryGet <T>(string path, out ModAsset metadata, bool includeDirs = false)
            {
                path = path.Replace('\\', '/');

                if (includeDirs && MapDirs.TryGetValue(path, out metadata) && metadata.AssetType == typeof(T))
                {
                    return(true);
                }
                if (Map.TryGetValue(path, out metadata) && metadata.AssetType == typeof(T))
                {
                    return(true);
                }

                metadata = null;
                return(false);
            }
Beispiel #4
0
            /// <summary>
            /// Gets the ModAsset mapped to the given relative path.
            /// </summary>
            /// <param name="path">The relative asset path.</param>
            /// <param name="metadata">The resulting mod asset meta object.</param>
            /// <param name="includeDirs">Whether to include directories or not.</param>
            /// <returns>True if a mapping for the given path is present, false otherwise.</returns>
            public static bool TryGet(string path, out ModAsset metadata, bool includeDirs = false)
            {
                path = path.Replace('\\', '/');

                if (includeDirs)
                {
                    if (MapDirs.TryGetValue(path, out metadata))
                    {
                        return(true);
                    }
                }
                if (Map.TryGetValue(path, out metadata))
                {
                    return(true);
                }
                return(false);
            }
Beispiel #5
0
            /// <summary>
            /// Process an asset and register it for further reprocessing in the future.
            /// Apply any mod-related changes to the asset based on the existing mod asset meta map.
            /// </summary>
            /// <param name="asset">The asset to process.</param>
            /// <param name="assetNameFull">The "full name" of the asset, preferable the relative asset path.</param>
            /// <returns>The processed asset.</returns>
            public static object Process(object asset, string assetNameFull)
            {
                if (DumpOnLoad)
                {
                    Dump(assetNameFull, asset);
                }

                string assetName = assetNameFull;

                if (assetName.StartsWith(PathContentOrig))
                {
                    assetName = assetName.Substring(PathContentOrig.Length + 1);
                }

                int loadedIndex = LoadedAssetPaths.IndexOf(assetName);

                if (loadedIndex == -1)
                {
                    LoadedAssetPaths.Add(assetName);
                    LoadedAssetFullPaths.Add(assetNameFull);
                    LoadedAssets.Add(new WeakReference(asset));
                }
                else
                {
                    LoadedAssets[loadedIndex] = new WeakReference(asset);
                }

                if (asset is Atlas)
                {
                    Atlas atlas = asset as Atlas;

                    ModAsset mapping = Get(assetName, true);
                    if (mapping == null || mapping.AssetType != typeof(AssetTypeDirectory))
                    {
                        return(asset);
                    }

                    atlas.Ingest(mapping);
                }

                return(OnProcess?.InvokePassing(asset, assetNameFull) ?? asset);
            }
            /// <summary>
            /// Adds a new mapping for the given relative content path.
            /// </summary>
            /// <param name="path">The relative asset path.</param>
            /// <param name="metadata">The matching mod asset meta object.</param>
            /// <returns>The passed mod asset meta object.</returns>
            public static ModAsset Add(string path, ModAsset metadata)
            {
                path = path.Replace('\\', '/');

                if (metadata.AssetType == null)
                {
                    path = GuessType(path, out metadata.AssetType, out metadata.AssetFormat);
                }

                metadata.PathMapped = path;

                // We want our new mapping to replace the previous one, but need to replace the previous one in the shadow structure.
                ModAsset metadataPrev;

                if (!Map.TryGetValue(path, out metadataPrev))
                {
                    metadataPrev = null;
                }

                // Hardcoded case: Store audio banks in a list of banks to preload.
                if (metadata.AssetType == typeof(AssetTypeBank))
                {
                    ListBanks.Add(metadata);
                }

                // Hardcoded case: Handle dialog .txts separately.
                if (metadata.AssetType == typeof(AssetTypeDialog))
                {
                    // Store multiple AssetMetadatas in a list per path.
                    List <ModAsset> dialogs;
                    if (!MapDialogs.TryGetValue(path, out dialogs))
                    {
                        dialogs          = new List <ModAsset>();
                        MapDialogs[path] = dialogs;
                    }
                    dialogs.Add(metadata);
                }
                // Hardcoded case: Handle maps separately.
                else if (metadata.AssetType == typeof(AssetTypeMap))
                {
                    // Store all maps in a single shared list.
                    // Note that we only store the last added item.
                    int index = ListMaps.FindIndex(other => other.PathMapped == metadata.PathMapped);
                    if (index == -1)
                    {
                        index = ListMaps.Count;
                    }
                    ListMaps.Insert(index, metadata);
                    // We also store the metadata as usual.
                    Map[path] = metadata;
                }
                // Hardcoded case: Handle directories separately.
                else if (metadata.AssetType == typeof(AssetTypeDirectory))
                {
                    MapDirs[path] = metadata;
                }
                else
                {
                    Map[path] = metadata;
                }

                // If we're not already the highest level shadow "node"...
                if (path != "")
                {
                    // Add directories automatically.
                    string   pathDir = Path.GetDirectoryName(path).Replace('\\', '/');
                    ModAsset metadataDir;
                    if (!MapDirs.TryGetValue(pathDir, out metadataDir))
                    {
                        metadataDir = new ModAsset(pathDir)
                        {
                            Source    = ModAsset.SourceType.None,
                            AssetType = typeof(AssetTypeDirectory)
                        };
                        Add(pathDir, metadataDir);
                    }
                    // If a previous mapping exists, replace it in the shadow structure.
                    int metadataPrevIndex = metadataDir.Children.IndexOf(metadataPrev);
                    if (metadataPrevIndex != -1)
                    {
                        metadataDir.Children[metadataPrevIndex] = metadata;
                    }
                    else
                    {
                        metadataDir.Children.Add(metadata);
                    }
                }

                return(metadata);
            }
Beispiel #7
0
 protected virtual void Add(string path, ModAsset asset)
 {
     asset = Everest.Content.Add(path, asset);
     List.Add(asset);
     Map[asset.PathVirtual] = asset;
 }