private ResourceHandle *GetResourceHandler(bool isSync, ResourceManager *resourceManager, ResourceCategory *categoryId,
                                               ResourceType *resourceType, int *resourceHash, byte *path, void *unk, bool isUnk)
    {
        if (!Utf8GamePath.FromPointer(path, out var gamePath))
        {
            PluginLog.Error("Could not create GamePath from resource path.");
            return(CallOriginalHandler(isSync, resourceManager, categoryId, resourceType, resourceHash, path, unk, isUnk));
        }

        CompareHash(gamePath.Path.Crc32, *resourceHash, gamePath);

        ResourceRequested?.Invoke(gamePath, isSync);

        // If no replacements are being made, we still want to be able to trigger the event.
        var(resolvedPath, data) = ResolvePath(gamePath, *categoryId, *resourceType, *resourceHash);
        PathResolved?.Invoke(gamePath, resolvedPath, data);
        if (resolvedPath == null)
        {
            var retUnmodified = CallOriginalHandler(isSync, resourceManager, categoryId, resourceType, resourceHash, path, unk, isUnk);
            ResourceLoaded?.Invoke((Structs.ResourceHandle *)retUnmodified, gamePath, null, data);
            return(retUnmodified);
        }

        // Replace the hash and path with the correct one for the replacement.
        *resourceHash = resolvedPath.Value.InternalName.Crc32;
        path = resolvedPath.Value.InternalName.Path;
        var retModified = CallOriginalHandler(isSync, resourceManager, categoryId, resourceType, resourceHash, path, unk, isUnk);

        ResourceLoaded?.Invoke((Structs.ResourceHandle *)retModified, gamePath, resolvedPath.Value, data);
        return(retModified);
    }
Example #2
0
 public static void OnResourceLoaded()
 {
     ResourceLoaded?.Invoke();
 }
Example #3
0
 protected virtual void OnResourceLoaded(ResourceLoadedEventArgs args)
 {
     ResourceLoaded?.Invoke(this, args);
 }
Example #4
0
        /// <summary>
        /// Loads the resource data.
        /// </summary>
        /// <param name="resourceName">Name of the resource.</param>
        public GameResource LoadResource(Type resourceType, string resourceName)
        {
            GameResource retval = null;

            if (string.IsNullOrEmpty(resourceName))
            {
                return(retval);
            }

            lock (_resourceLock)
            {
                var resourceTypeName = resourceType.FullName;

                // get the resource list
                Dictionary <string, GameResource> typeResources;
                if (!_resources.TryGetValue(resourceTypeName, out typeResources))
                {
                    typeResources = new Dictionary <string, GameResource>(StringComparer.OrdinalIgnoreCase);
                    _resources.Add(resourceTypeName, typeResources);
                }

                if (!typeResources.TryGetValue(resourceName, out retval))
                {
                    var args = new ResourceLoadEventArgs();
                    args.ResourceName = resourceName;
                    args.ResourceType = resourceType;

                    TraceSource.TraceEvent(TraceEventType.Verbose, 0, Strings.LoadingResource, resourceTypeName, resourceName);

                    if (resourceType == typeof(Sprite))
                    {
                        // load the sprite
                        retval = LoadSprite(resourceName);
                    }
                    else if (resourceType == typeof(Map))
                    {
                        var mapService = _engine.GetService <IMapService>();
                        retval = mapService.LoadMap(Path.Combine(GetResourcePath <Map>(), resourceName + ".tmx"));
                    }
                    else if (resourceType == typeof(Sound))
                    {
                        var sound = new Sound()
                        {
                            Name = resourceName
                        };
                        retval = sound;
                    }

                    args.LoadedResource = retval;
                    retval.ResourceId   = _nextResourceId++;

                    typeResources.Add(resourceName, retval);
                    _resourcesById.Add(retval.ResourceId, retval);

                    // the engine has loaded what it needs, but the renderer might want to load image files etc.
                    ResourceLoaded?.Invoke(this, args);
                }
            }

            return(retval);
        }